]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelSiliconPkg/IntelVTdDxe/VtdReg.c
IntelSiliconPkg/IntelVTdDxe: Improve performance.
[mirror_edk2.git] / IntelSiliconPkg / IntelVTdDxe / VtdReg.c
index f36e3dec06714490fade14cb35cafc3f610f9738..b1178b70744d84e3ee39c083f4ae37152e9d8fb8 100644 (file)
@@ -20,43 +20,62 @@ VTD_UNIT_INFORMATION             *mVtdUnitInformation;
 BOOLEAN  mVtdEnabled;\r
 \r
 /**\r
-  Invalid VTd global IOTLB.\r
+  Flush VTD page table and context table memory.\r
 \r
-  @param[in]  VtdIndex              The index of VTd engine.\r
+  This action is to make sure the IOMMU engine can get final data in memory.\r
 \r
-  @retval EFI_SUCCESS           VTd global IOTLB is invalidated.\r
-  @retval EFI_DEVICE_ERROR      VTd global IOTLB is not invalidated.\r
+  @param[in]  VtdIndex          The index used to identify a VTd engine.\r
+  @param[in]  Base              The base address of memory to be flushed.\r
+  @param[in]  Size              The size of memory in bytes to be flushed.\r
 **/\r
-EFI_STATUS\r
-InvalidateVtdIOTLBGlobal (\r
+VOID\r
+FlushPageTableMemory (\r
+  IN UINTN  VtdIndex,\r
+  IN UINTN  Base,\r
+  IN UINTN  Size\r
+  )\r
+{\r
+  if (mVtdUnitInformation[VtdIndex].ECapReg.Bits.C == 0) {\r
+    WriteBackDataCacheRange ((VOID *)Base, Size);\r
+  }\r
+}\r
+\r
+/**\r
+  Flush VTd engine write buffer.\r
+\r
+  @param[in]  VtdIndex          The index used to identify a VTd engine.\r
+**/\r
+VOID\r
+FlushWriteBuffer (\r
   IN UINTN  VtdIndex\r
   )\r
 {\r
-  UINT64  Reg64;\r
   UINT32  Reg32;\r
 \r
-  if (!mVtdEnabled) {\r
-    return EFI_SUCCESS;\r
+  if (mVtdUnitInformation[VtdIndex].CapReg.Bits.RWBF != 0) {\r
+    Reg32 = MmioRead32 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_GSTS_REG);\r
+    MmioWrite32 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_GCMD_REG, Reg32 | B_GMCD_REG_WBF);\r
+    do {\r
+      Reg32 = MmioRead32 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_GSTS_REG);\r
+    } while ((Reg32 & B_GSTS_REG_WBF) != 0);\r
   }\r
+}\r
 \r
-  DEBUG((DEBUG_VERBOSE, "InvalidateVtdIOTLBGlobal(%d)\n", VtdIndex));\r
-\r
-  AsmWbinvd();\r
+/**\r
+  Invalidate VTd context cache.\r
 \r
-  //\r
-  // Write Buffer Flush before invalidation\r
-  //\r
-  Reg32 = MmioRead32 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_CAP_REG);\r
-  if ((Reg32 & B_CAP_REG_RWBF) != 0) {\r
-    MmioWrite32 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_GCMD_REG, B_GMCD_REG_WBF);\r
-  }\r
+  @param[in]  VtdIndex          The index used to identify a VTd engine.\r
+**/\r
+EFI_STATUS\r
+InvalidateContextCache (\r
+  IN UINTN  VtdIndex\r
+  )\r
+{\r
+  UINT64  Reg64;\r
 \r
-  //\r
-  // Invalidate the context cache\r
-  //\r
   Reg64 = MmioRead64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_CCMD_REG);\r
   if ((Reg64 & B_CCMD_REG_ICC) != 0) {\r
-    DEBUG ((DEBUG_ERROR,"ERROR: InvalidateVtdIOTLBGlobal: B_CCMD_REG_ICC is set for VTD(%d)\n",VtdIndex));\r
+    DEBUG ((DEBUG_ERROR,"ERROR: InvalidateContextCache: B_CCMD_REG_ICC is set for VTD(%d)\n",VtdIndex));\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
@@ -68,97 +87,29 @@ InvalidateVtdIOTLBGlobal (
     Reg64 = MmioRead64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_CCMD_REG);\r
   } while ((Reg64 & B_CCMD_REG_ICC) != 0);\r
 \r
-  //\r
-  // Invalidate the IOTLB cache\r
-  //\r
-\r
-  Reg64 = MmioRead64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + (mVtdUnitInformation[VtdIndex].ECapReg.Bits.IRO * 16) + R_IOTLB_REG);\r
-  if ((Reg64 & B_IOTLB_REG_IVT) != 0) {\r
-    DEBUG ((DEBUG_ERROR,"ERROR: InvalidateVtdIOTLBGlobal: B_IOTLB_REG_IVT is set for VTD(%d)\n", VtdIndex));\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\r
-  Reg64 &= ((~B_IOTLB_REG_IVT) & (~B_IOTLB_REG_IIRG_MASK));\r
-  Reg64 |= (B_IOTLB_REG_IVT | V_IOTLB_REG_IIRG_GLOBAL);\r
-  MmioWrite64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + (mVtdUnitInformation[VtdIndex].ECapReg.Bits.IRO * 16) + R_IOTLB_REG, Reg64);\r
-\r
-  do {\r
-    Reg64 = MmioRead64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + (mVtdUnitInformation[VtdIndex].ECapReg.Bits.IRO * 16) + R_IOTLB_REG);\r
-  } while ((Reg64 & B_IOTLB_REG_IVT) != 0);\r
-\r
-  //\r
-  // Disable VTd\r
-  //\r
-  MmioWrite32 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_GCMD_REG, B_GMCD_REG_SRTP);\r
-  do {\r
-    Reg32 = MmioRead32 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_GSTS_REG);\r
-  } while((Reg32 & B_GSTS_REG_RTPS) == 0);\r
-\r
-  //\r
-  // Enable VTd\r
-  //\r
-  MmioWrite32 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_GCMD_REG, B_GMCD_REG_TE);\r
-  do {\r
-    Reg32 = MmioRead32 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_GSTS_REG);\r
-  } while ((Reg32 & B_GSTS_REG_TE) == 0);\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  Invalid VTd IOTLB domain.\r
-\r
-  @param[in]  VtdIndex              The index of VTd engine.\r
-  @param[in]  DomainIdentifier      The domain ID of the source.\r
+  Invalidate VTd IOTLB.\r
 \r
-  @retval EFI_SUCCESS           VTd IOTLB domain is invalidated.\r
-  @retval EFI_DEVICE_ERROR      VTd IOTLB domain is not invalidated.\r
+  @param[in]  VtdIndex          The index used to identify a VTd engine.\r
 **/\r
 EFI_STATUS\r
-InvalidateVtdIOTLBDomain (\r
-  IN UINTN  VtdIndex,\r
-  IN UINT16 DomainIdentifier\r
+InvalidateIOTLB (\r
+  IN UINTN  VtdIndex\r
   )\r
 {\r
   UINT64  Reg64;\r
 \r
-  if (!mVtdEnabled) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  DEBUG((DEBUG_VERBOSE, "InvalidateVtdIOTLBDomain(%d): 0x%016lx (0x%04x)\n", VtdIndex, DomainIdentifier));\r
-\r
-  //\r
-  // Invalidate the context cache\r
-  //\r
-  Reg64 = MmioRead64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_CCMD_REG);\r
-  if ((Reg64 & B_CCMD_REG_ICC) != 0) {\r
-    DEBUG ((DEBUG_ERROR,"ERROR: InvalidateVtdIOTLBDomain: B_CCMD_REG_ICC is set for VTD(%d)\n",VtdIndex));\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\r
-  Reg64 &= ((~B_CCMD_REG_ICC) & (~B_CCMD_REG_CIRG_MASK));\r
-  Reg64 |= (B_CCMD_REG_ICC | V_CCMD_REG_CIRG_DOMAIN);\r
-  Reg64 |= (B_CCMD_REG_ICC | V_CCMD_REG_CIRG_DOMAIN);\r
-  MmioWrite64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_CCMD_REG, Reg64);\r
-\r
-  do {\r
-    Reg64 = MmioRead64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + R_CCMD_REG);\r
-  } while ((Reg64 & B_CCMD_REG_ICC) != 0);\r
-\r
-  //\r
-  // Invalidate the IOTLB cache\r
-  //\r
-\r
   Reg64 = MmioRead64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + (mVtdUnitInformation[VtdIndex].ECapReg.Bits.IRO * 16) + R_IOTLB_REG);\r
   if ((Reg64 & B_IOTLB_REG_IVT) != 0) {\r
-    DEBUG ((DEBUG_ERROR,"ERROR: InvalidateVtdIOTLBDomain: B_IOTLB_REG_IVT is set for VTD(%d)\n", VtdIndex));\r
+    DEBUG ((DEBUG_ERROR,"ERROR: InvalidateIOTLB: B_IOTLB_REG_IVT is set for VTD(%d)\n", VtdIndex));\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
   Reg64 &= ((~B_IOTLB_REG_IVT) & (~B_IOTLB_REG_IIRG_MASK));\r
-  Reg64 |= (B_IOTLB_REG_IVT | V_IOTLB_REG_IIRG_DOMAIN);\r
-  Reg64 |= LShiftU64 (DomainIdentifier, 32);\r
+  Reg64 |= (B_IOTLB_REG_IVT | V_IOTLB_REG_IIRG_GLOBAL);\r
   MmioWrite64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + (mVtdUnitInformation[VtdIndex].ECapReg.Bits.IRO * 16) + R_IOTLB_REG, Reg64);\r
 \r
   do {\r
@@ -169,53 +120,41 @@ InvalidateVtdIOTLBDomain (
 }\r
 \r
 /**\r
-  Invalid VTd IOTLB page.\r
+  Invalid VTd global IOTLB.\r
 \r
   @param[in]  VtdIndex              The index of VTd engine.\r
-  @param[in]  Address               The address of IOTLB page.\r
-  @param[in]  AddressMode           The address mode of IOTLB page.\r
-  @param[in]  DomainIdentifier      The domain ID of the source.\r
 \r
-  @retval EFI_SUCCESS           VTd IOTLB page is invalidated.\r
-  @retval EFI_DEVICE_ERROR      VTd IOTLB page is not invalidated.\r
+  @retval EFI_SUCCESS           VTd global IOTLB is invalidated.\r
+  @retval EFI_DEVICE_ERROR      VTd global IOTLB is not invalidated.\r
 **/\r
 EFI_STATUS\r
-InvalidateVtdIOTLBPage (\r
-  IN UINTN  VtdIndex,\r
-  IN UINT64 Address,\r
-  IN UINT8  AddressMode,\r
-  IN UINT16 DomainIdentifier\r
+InvalidateVtdIOTLBGlobal (\r
+  IN UINTN  VtdIndex\r
   )\r
 {\r
-  UINT64  Reg64;\r
-  UINT64  Data64;\r
-\r
   if (!mVtdEnabled) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
-  DEBUG((DEBUG_VERBOSE, "InvalidateVtdIOTLBPage(%d): 0x%016lx (0x%02x)\n", VtdIndex, Address, AddressMode));\r
-\r
-  if (mVtdUnitInformation[VtdIndex].CapReg.Bits.PSI != 0) {\r
-    Reg64 = MmioRead64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + (mVtdUnitInformation[VtdIndex].ECapReg.Bits.IRO * 16) + R_IOTLB_REG);\r
-    if ((Reg64 & B_IOTLB_REG_IVT) != 0) {\r
-      DEBUG ((DEBUG_ERROR,"ERROR: InvalidateVtdIOTLBPage: B_IOTLB_REG_IVT is set for VTD(%d)\n", VtdIndex));\r
-      return EFI_DEVICE_ERROR;\r
-    }\r
+  DEBUG((DEBUG_VERBOSE, "InvalidateVtdIOTLBGlobal(%d)\n", VtdIndex));\r
 \r
-    Data64 = Address | AddressMode;\r
-    MmioWrite64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + (mVtdUnitInformation[VtdIndex].ECapReg.Bits.IRO * 16) + R_IVA_REG, Data64);\r
+  //\r
+  // Write Buffer Flush before invalidation\r
+  //\r
+  FlushWriteBuffer (VtdIndex);\r
 \r
-    Reg64 &= ((~B_IOTLB_REG_IVT) & (~B_IOTLB_REG_IIRG_MASK));\r
-    Reg64 |= (B_IOTLB_REG_IVT | V_IOTLB_REG_IIRG_PAGE);\r
-    Reg64 |= LShiftU64 (DomainIdentifier, 32);\r
-    MmioWrite64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + (mVtdUnitInformation[VtdIndex].ECapReg.Bits.IRO * 16) + R_IOTLB_REG, Reg64);\r
+  //\r
+  // Invalidate the context cache\r
+  //\r
+  if (mVtdUnitInformation[VtdIndex].HasDirtyContext) {\r
+    InvalidateContextCache (VtdIndex);\r
+  }\r
 \r
-    do {\r
-      Reg64 = MmioRead64 (mVtdUnitInformation[VtdIndex].VtdUnitBaseAddress + (mVtdUnitInformation[VtdIndex].ECapReg.Bits.IRO * 16) + R_IOTLB_REG);\r
-    } while ((Reg64 & B_IOTLB_REG_IVT) != 0);\r
-  } else {\r
-    InvalidateVtdIOTLBGlobal (VtdIndex);\r
+  //\r
+  // Invalidate the IOTLB cache\r
+  //\r
+  if (mVtdUnitInformation[VtdIndex].HasDirtyContext || mVtdUnitInformation[VtdIndex].HasDirtyPages) {\r
+    InvalidateIOTLB (VtdIndex);\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -268,11 +207,8 @@ EnableDmar (
   )\r
 {\r
   UINTN     Index;\r
-  UINT64    Reg64;\r
   UINT32    Reg32;\r
 \r
-  AsmWbinvd();\r
-\r
   for (Index = 0; Index < mVtdUnitNumber; Index++) {\r
     DEBUG((DEBUG_INFO, ">>>>>>EnableDmar() for engine [%d] \n", Index));\r
 \r
@@ -299,48 +235,17 @@ EnableDmar (
     //\r
     // Write Buffer Flush before invalidation\r
     //\r
-    Reg32 = MmioRead32 (mVtdUnitInformation[Index].VtdUnitBaseAddress + R_CAP_REG);\r
-    if ((Reg32 & B_CAP_REG_RWBF) != 0) {\r
-      MmioWrite32 (mVtdUnitInformation[Index].VtdUnitBaseAddress + R_GCMD_REG, B_GMCD_REG_WBF);\r
-    }\r
+    FlushWriteBuffer (Index);\r
 \r
     //\r
     // Invalidate the context cache\r
     //\r
-    Reg64 = MmioRead64 (mVtdUnitInformation[Index].VtdUnitBaseAddress + R_CCMD_REG);\r
-    if ((Reg64 & B_CCMD_REG_ICC) != 0) {\r
-      DEBUG ((DEBUG_INFO,"ERROR: EnableDmar: B_CCMD_REG_ICC is set for VTD(%d)\n",Index));\r
-      return EFI_DEVICE_ERROR;\r
-    }\r
-\r
-    Reg64 &= ((~B_CCMD_REG_ICC) & (~B_CCMD_REG_CIRG_MASK));\r
-    Reg64 |= (B_CCMD_REG_ICC | V_CCMD_REG_CIRG_GLOBAL);\r
-    MmioWrite64 (mVtdUnitInformation[Index].VtdUnitBaseAddress + R_CCMD_REG, Reg64);\r
-\r
-    DEBUG((DEBUG_INFO, "EnableDmar: Waiting B_CCMD_REG_ICC ...\n"));\r
-    do {\r
-      Reg64 = MmioRead64 (mVtdUnitInformation[Index].VtdUnitBaseAddress + R_CCMD_REG);\r
-    } while ((Reg64 & B_CCMD_REG_ICC) != 0);\r
+    InvalidateContextCache (Index);\r
 \r
     //\r
     // Invalidate the IOTLB cache\r
     //\r
-    DEBUG((DEBUG_INFO, "EnableDmar: IRO 0x%x\n", mVtdUnitInformation[Index].ECapReg.Bits.IRO));\r
-\r
-    Reg64 = MmioRead64 (mVtdUnitInformation[Index].VtdUnitBaseAddress + (mVtdUnitInformation[Index].ECapReg.Bits.IRO * 16) + R_IOTLB_REG);\r
-    if ((Reg64 & B_IOTLB_REG_IVT) != 0) {\r
-      DEBUG ((DEBUG_INFO,"ERROR: EnableDmar: B_IOTLB_REG_IVT is set for VTD(%d)\n", Index));\r
-      return EFI_DEVICE_ERROR;\r
-    }\r
-\r
-    Reg64 &= ((~B_IOTLB_REG_IVT) & (~B_IOTLB_REG_IIRG_MASK));\r
-    Reg64 |= (B_IOTLB_REG_IVT | V_IOTLB_REG_IIRG_GLOBAL);\r
-    MmioWrite64 (mVtdUnitInformation[Index].VtdUnitBaseAddress + (mVtdUnitInformation[Index].ECapReg.Bits.IRO * 16) + R_IOTLB_REG, Reg64);\r
-\r
-    DEBUG((DEBUG_INFO, "EnableDmar: Waiting B_IOTLB_REG_IVT ...\n"));\r
-    do {\r
-      Reg64 = MmioRead64 (mVtdUnitInformation[Index].VtdUnitBaseAddress + (mVtdUnitInformation[Index].ECapReg.Bits.IRO * 16) + R_IOTLB_REG);\r
-    } while ((Reg64 & B_IOTLB_REG_IVT) != 0);\r
+    InvalidateIOTLB (Index);\r
 \r
     //\r
     // Enable VTd\r
@@ -371,20 +276,16 @@ DisableDmar (
   )\r
 {\r
   UINTN     Index;\r
+  UINTN     SubIndex;\r
   UINT32    Reg32;\r
 \r
-  AsmWbinvd();\r
-\r
   for (Index = 0; Index < mVtdUnitNumber; Index++) {\r
     DEBUG((DEBUG_INFO, ">>>>>>DisableDmar() for engine [%d] \n", Index));\r
 \r
     //\r
     // Write Buffer Flush before invalidation\r
     //\r
-    Reg32 = MmioRead32 (mVtdUnitInformation[Index].VtdUnitBaseAddress + R_CAP_REG);\r
-    if ((Reg32 & B_CAP_REG_RWBF) != 0) {\r
-      MmioWrite32 (mVtdUnitInformation[Index].VtdUnitBaseAddress + R_GCMD_REG, B_GMCD_REG_WBF);\r
-    }\r
+    FlushWriteBuffer (Index);\r
 \r
     //\r
     // Disable VTd\r
@@ -402,6 +303,19 @@ DisableDmar (
 \r
   mVtdEnabled = FALSE;\r
 \r
+  for (Index = 0; Index < mVtdUnitNumber; Index++) {\r
+    DEBUG((DEBUG_INFO, "engine [%d] access\n", Index));\r
+    for (SubIndex = 0; SubIndex < mVtdUnitInformation[Index].PciDeviceInfo.PciDescriptorNumber; SubIndex++) {\r
+      DEBUG ((DEBUG_INFO, "  PCI S%04X B%02x D%02x F%02x - %d\n",\r
+        mVtdUnitInformation[Index].Segment,\r
+        mVtdUnitInformation[Index].PciDeviceInfo.PciDescriptors[SubIndex].Bits.Bus,\r
+        mVtdUnitInformation[Index].PciDeviceInfo.PciDescriptors[SubIndex].Bits.Device,\r
+        mVtdUnitInformation[Index].PciDeviceInfo.PciDescriptors[SubIndex].Bits.Function,\r
+        mVtdUnitInformation[Index].PciDeviceInfo.AccessCount[SubIndex]\r
+        ));\r
+    }\r
+  }\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r