IntelSiliconPkg IntelVTdPmrPei: Install IoMmu PPI before enabling PMR
authorStar Zeng <star.zeng@intel.com>
Fri, 19 Jan 2018 11:07:37 +0000 (19:07 +0800)
committerStar Zeng <star.zeng@intel.com>
Wed, 7 Feb 2018 10:28:11 +0000 (18:28 +0800)
Then the consumer of IoMmu PPI has opportunity to get granted DMA
buffer (by callback) to replace old buffer before it is forbidden
by enabling PMR.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
IntelSiliconPkg/Feature/VTd/IntelVTdPmrPei/IntelVTdPmrPei.c

index 63ba94d..87708cf 100644 (file)
@@ -381,17 +381,13 @@ CONST EFI_PEI_PPI_DESCRIPTOR mIoMmuPpiList = {
   Initialize DMA protection.\r
 \r
   @param VTdInfo        The VTd engine context information.\r
-  @param DmaBufferSize  the DMA buffer size\r
-  @param DmaBufferBase  the DMA buffer base\r
 \r
   @retval EFI_SUCCESS           the DMA protection is initialized.\r
   @retval EFI_OUT_OF_RESOURCES  no enough resource to initialize DMA protection.\r
 **/\r
 EFI_STATUS\r
 InitDmaProtection (\r
-  IN   VTD_INFO                    *VTdInfo,\r
-  IN   UINTN                       DmaBufferSize,\r
-  OUT  UINTN                       *DmaBufferBase\r
+  IN   VTD_INFO                    *VTdInfo\r
   )\r
 {\r
   EFI_STATUS                  Status;\r
@@ -402,6 +398,13 @@ InitDmaProtection (
   UINTN                       LowTop;\r
   UINTN                       HighBottom;\r
   UINT64                      HighTop;\r
+  DMA_BUFFER_INFO             *DmaBufferInfo;\r
+  VOID                        *Hob;\r
+\r
+  Hob = GetFirstGuidHob (&mDmaBufferInfoGuid);\r
+  DmaBufferInfo = GET_GUID_HOB_DATA(Hob);\r
+\r
+  DEBUG ((DEBUG_INFO, " DmaBufferSize : 0x%x\n", DmaBufferInfo->DmaBufferSize));\r
 \r
   LowMemoryAlignment = GetLowMemoryAlignment (VTdInfo, VTdInfo->EngineMask);\r
   HighMemoryAlignment = GetHighMemoryAlignment (VTdInfo, VTdInfo->EngineMask);\r
@@ -410,17 +413,28 @@ InitDmaProtection (
   } else {\r
     MemoryAlignment = LowMemoryAlignment;\r
   }\r
-  ASSERT (DmaBufferSize == ALIGN_VALUE(DmaBufferSize, MemoryAlignment));\r
-  *DmaBufferBase = (UINTN)AllocateAlignedPages (EFI_SIZE_TO_PAGES(DmaBufferSize), MemoryAlignment);\r
-  ASSERT (*DmaBufferBase != 0);\r
-  if (*DmaBufferBase == 0) {\r
+  ASSERT (DmaBufferInfo->DmaBufferSize == ALIGN_VALUE(DmaBufferInfo->DmaBufferSize, MemoryAlignment));\r
+  DmaBufferInfo->DmaBufferBase = (UINTN)AllocateAlignedPages (EFI_SIZE_TO_PAGES(DmaBufferInfo->DmaBufferSize), MemoryAlignment);\r
+  ASSERT (DmaBufferInfo->DmaBufferBase != 0);\r
+  if (DmaBufferInfo->DmaBufferBase == 0) {\r
     DEBUG ((DEBUG_INFO, " InitDmaProtection : OutOfResource\n"));\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
+  DEBUG ((DEBUG_INFO, " DmaBufferBase : 0x%x\n", DmaBufferInfo->DmaBufferBase));\r
+\r
+  DmaBufferInfo->DmaBufferCurrentTop = DmaBufferInfo->DmaBufferBase + DmaBufferInfo->DmaBufferSize;\r
+  DmaBufferInfo->DmaBufferCurrentBottom = DmaBufferInfo->DmaBufferBase;\r
+\r
+  //\r
+  // Install PPI.\r
+  //\r
+  Status = PeiServicesInstallPpi (&mIoMmuPpiList);\r
+  ASSERT_EFI_ERROR(Status);\r
+\r
   LowBottom = 0;\r
-  LowTop = *DmaBufferBase;\r
-  HighBottom = *DmaBufferBase + DmaBufferSize;\r
+  LowTop = DmaBufferInfo->DmaBufferBase;\r
+  HighBottom = DmaBufferInfo->DmaBufferBase + DmaBufferInfo->DmaBufferSize;\r
   HighTop = LShiftU64 (1, VTdInfo->HostAddressWidth + 1);\r
 \r
   Status = SetDmaProtectedRange (\r
@@ -433,7 +447,7 @@ InitDmaProtection (
              );\r
 \r
   if (EFI_ERROR(Status)) {\r
-    FreePages ((VOID *)*DmaBufferBase, EFI_SIZE_TO_PAGES(DmaBufferSize));\r
+    FreePages ((VOID *)DmaBufferInfo->DmaBufferBase, EFI_SIZE_TO_PAGES(DmaBufferInfo->DmaBufferSize));\r
   }\r
 \r
   return Status;\r
@@ -543,7 +557,6 @@ InitVTdPmrForDma (
   EFI_STATUS                  Status;\r
   VOID                        *Hob;\r
   VTD_INFO                    *VTdInfo;\r
-  DMA_BUFFER_INFO             *DmaBufferInfo;\r
 \r
   Hob = GetFirstGuidHob (&mVTdInfoGuid);\r
   VTdInfo = GET_GUID_HOB_DATA(Hob);\r
@@ -553,29 +566,11 @@ InitVTdPmrForDma (
   //\r
   ParseDmarAcpiTableRmrr (VTdInfo);\r
 \r
-  Hob = GetFirstGuidHob (&mDmaBufferInfoGuid);\r
-  DmaBufferInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
-  DEBUG ((DEBUG_INFO, " DmaBufferSize : 0x%x\n", DmaBufferInfo->DmaBufferSize));\r
-  //\r
-  // Find a pre-memory in resource hob as DMA buffer\r
-  // Mark PEI memory to be DMA protected.\r
-  //\r
-  Status = InitDmaProtection (VTdInfo, DmaBufferInfo->DmaBufferSize, &DmaBufferInfo->DmaBufferBase);\r
-  if (EFI_ERROR(Status)) {\r
-    return Status;\r
-  }\r
-\r
-  DEBUG ((DEBUG_INFO, " DmaBufferBase : 0x%x\n", DmaBufferInfo->DmaBufferBase));\r
-\r
-  DmaBufferInfo->DmaBufferCurrentTop = DmaBufferInfo->DmaBufferBase + DmaBufferInfo->DmaBufferSize;\r
-  DmaBufferInfo->DmaBufferCurrentBottom = DmaBufferInfo->DmaBufferBase;\r
-\r
   //\r
-  // Install PPI.\r
+  // Allocate a range in PEI memory as DMA buffer\r
+  // Mark others to be DMA protected.\r
   //\r
-  Status = PeiServicesInstallPpi (&mIoMmuPpiList);\r
-  ASSERT_EFI_ERROR(Status);\r
+  Status = InitDmaProtection (VTdInfo);\r
 \r
   return Status;\r
 }\r