+++ /dev/null
-/** @file\r
-\r
- Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>\r
-\r
- SPDX-License-Identifier: BSD-2-Clause-Patent\r
-\r
-**/\r
-\r
-#include <Uefi.h>\r
-#include <PiPei.h>\r
-#include <Library/BaseLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/IoLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/PeiServicesLib.h>\r
-#include <Library/HobLib.h>\r
-#include <IndustryStandard/Vtd.h>\r
-#include <Ppi/IoMmu.h>\r
-#include <Ppi/VtdInfo.h>\r
-#include <Ppi/MemoryDiscovered.h>\r
-#include <Ppi/EndOfPeiPhase.h>\r
-\r
-#include "IntelVTdPmrPei.h"\r
-\r
-EFI_GUID mVTdInfoGuid = {\r
- 0x222f5e30, 0x5cd, 0x49c6, { 0x8a, 0xc, 0x36, 0xd6, 0x58, 0x41, 0xe0, 0x82 }\r
-};\r
-\r
-EFI_GUID mDmaBufferInfoGuid = {\r
- 0x7b624ec7, 0xfb67, 0x4f9c, { 0xb6, 0xb0, 0x4d, 0xfa, 0x9c, 0x88, 0x20, 0x39 }\r
-};\r
-\r
-typedef struct {\r
- UINTN DmaBufferBase;\r
- UINTN DmaBufferSize;\r
- UINTN DmaBufferCurrentTop;\r
- UINTN DmaBufferCurrentBottom;\r
-} DMA_BUFFER_INFO;\r
-\r
-#define MAP_INFO_SIGNATURE SIGNATURE_32 ('D', 'M', 'A', 'P')\r
-typedef struct {\r
- UINT32 Signature;\r
- EDKII_IOMMU_OPERATION Operation;\r
- UINTN NumberOfBytes;\r
- EFI_PHYSICAL_ADDRESS HostAddress;\r
- EFI_PHYSICAL_ADDRESS DeviceAddress;\r
-} MAP_INFO;\r
-\r
-/**\r
-\r
- PEI Memory Layout:\r
-\r
- +------------------+ <=============== PHMR.Limit (+ alignment) (1 << (HostAddressWidth + 1))\r
- | Mem Resource |\r
- | |\r
-\r
- +------------------+ <------- EfiMemoryTop\r
- | PEI allocated |\r
- =========== +==================+ <=============== PHMR.Base\r
- ^ | Commom Buf |\r
- | | -------------- |\r
- DMA Buffer | * DMA FREE * |\r
- | | -------------- |\r
- V | Read/Write Buf |\r
- =========== +==================+ <=============== PLMR.Limit (+ alignment)\r
- | PEI allocated |\r
- | -------------- | <------- EfiFreeMemoryTop\r
- | * PEI FREE * |\r
- | -------------- | <------- EfiFreeMemoryBottom\r
- | hob |\r
- | -------------- |\r
- | Stack |\r
- +------------------+ <------- EfiMemoryBottom / Stack Bottom\r
-\r
- +------------------+\r
- | Mem Alloc Hob |\r
- +------------------+\r
-\r
- | |\r
- | Mem Resource |\r
- +------------------+ <=============== PLMR.Base (0)\r
-**/\r
-\r
-/**\r
- Set IOMMU attribute for a system memory.\r
-\r
- If the IOMMU PPI exists, the system memory cannot be used\r
- for DMA by default.\r
-\r
- When a device requests a DMA access for a system memory,\r
- the device driver need use SetAttribute() to update the IOMMU\r
- attribute to request DMA access (read and/or write).\r
-\r
- @param[in] This The PPI instance pointer.\r
- @param[in] Mapping The mapping value returned from Map().\r
- @param[in] IoMmuAccess The IOMMU access.\r
-\r
- @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.\r
- @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().\r
- @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.\r
- @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.\r
- @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by Mapping.\r
- @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.\r
- @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.\r
- @retval EFI_NOT_AVAILABLE_YET DMA protection has been enabled, but DMA buffer are\r
- not available to be allocated yet.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiIoMmuSetAttribute (\r
- IN EDKII_IOMMU_PPI *This,\r
- IN VOID *Mapping,\r
- IN UINT64 IoMmuAccess\r
- )\r
-{\r
- VOID *Hob;\r
- DMA_BUFFER_INFO *DmaBufferInfo;\r
-\r
- Hob = GetFirstGuidHob (&mDmaBufferInfoGuid);\r
- DmaBufferInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
- if (DmaBufferInfo->DmaBufferCurrentTop == 0) {\r
- return EFI_NOT_AVAILABLE_YET;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
- Provides the controller-specific addresses required to access system memory from a\r
- DMA bus master.\r
-\r
- @param This The PPI instance pointer.\r
- @param Operation Indicates if the bus master is going to read or write to system memory.\r
- @param HostAddress The system memory address to map to the PCI controller.\r
- @param NumberOfBytes On input the number of bytes to map. On output the number of bytes\r
- that were mapped.\r
- @param DeviceAddress The resulting map address for the bus master PCI controller to use to\r
- access the hosts HostAddress.\r
- @param Mapping A resulting value to pass to Unmap().\r
-\r
- @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r
- @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.\r
- @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
- @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
- @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.\r
- @retval EFI_NOT_AVAILABLE_YET DMA protection has been enabled, but DMA buffer are\r
- not available to be allocated yet.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiIoMmuMap (\r
- IN EDKII_IOMMU_PPI *This,\r
- IN EDKII_IOMMU_OPERATION Operation,\r
- IN VOID *HostAddress,\r
- IN OUT UINTN *NumberOfBytes,\r
- OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
- OUT VOID **Mapping\r
- )\r
-{\r
- MAP_INFO *MapInfo;\r
- UINTN Length;\r
- VOID *Hob;\r
- DMA_BUFFER_INFO *DmaBufferInfo;\r
-\r
- Hob = GetFirstGuidHob (&mDmaBufferInfoGuid);\r
- DmaBufferInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
- DEBUG ((DEBUG_VERBOSE, "PeiIoMmuMap - HostAddress - 0x%x, NumberOfBytes - %x\n", HostAddress, *NumberOfBytes));\r
- DEBUG ((DEBUG_VERBOSE, " DmaBufferCurrentTop - %x\n", DmaBufferInfo->DmaBufferCurrentTop));\r
- DEBUG ((DEBUG_VERBOSE, " DmaBufferCurrentBottom - %x\n", DmaBufferInfo->DmaBufferCurrentBottom));\r
-\r
- if (DmaBufferInfo->DmaBufferCurrentTop == 0) {\r
- return EFI_NOT_AVAILABLE_YET;\r
- }\r
-\r
- if (Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||\r
- Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {\r
- *DeviceAddress = (UINTN)HostAddress;\r
- *Mapping = NULL;\r
- return EFI_SUCCESS;\r
- }\r
-\r
- Length = *NumberOfBytes + sizeof(MAP_INFO);\r
- if (Length > DmaBufferInfo->DmaBufferCurrentTop - DmaBufferInfo->DmaBufferCurrentBottom) {\r
- DEBUG ((DEBUG_ERROR, "PeiIoMmuMap - OUT_OF_RESOURCE\n"));\r
- ASSERT (FALSE);\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
-\r
- *DeviceAddress = DmaBufferInfo->DmaBufferCurrentBottom;\r
- DmaBufferInfo->DmaBufferCurrentBottom += Length;\r
-\r
- MapInfo = (VOID *)(UINTN)(*DeviceAddress + *NumberOfBytes);\r
- MapInfo->Signature = MAP_INFO_SIGNATURE;\r
- MapInfo->Operation = Operation;\r
- MapInfo->NumberOfBytes = *NumberOfBytes;\r
- MapInfo->HostAddress = (UINTN)HostAddress;\r
- MapInfo->DeviceAddress = *DeviceAddress;\r
- *Mapping = MapInfo;\r
- DEBUG ((DEBUG_VERBOSE, " Op(%x):DeviceAddress - %x, Mapping - %x\n", Operation, (UINTN)*DeviceAddress, MapInfo));\r
-\r
- //\r
- // If this is a read operation from the Bus Master's point of view,\r
- // then copy the contents of the real buffer into the mapped buffer\r
- // so the Bus Master can read the contents of the real buffer.\r
- //\r
- if (Operation == EdkiiIoMmuOperationBusMasterRead ||\r
- Operation == EdkiiIoMmuOperationBusMasterRead64) {\r
- CopyMem (\r
- (VOID *) (UINTN) MapInfo->DeviceAddress,\r
- (VOID *) (UINTN) MapInfo->HostAddress,\r
- MapInfo->NumberOfBytes\r
- );\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
- Completes the Map() operation and releases any corresponding resources.\r
-\r
- @param This The PPI instance pointer.\r
- @param Mapping The mapping value returned from Map().\r
-\r
- @retval EFI_SUCCESS The range was unmapped.\r
- @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().\r
- @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r
- @retval EFI_NOT_AVAILABLE_YET DMA protection has been enabled, but DMA buffer are\r
- not available to be allocated yet.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiIoMmuUnmap (\r
- IN EDKII_IOMMU_PPI *This,\r
- IN VOID *Mapping\r
- )\r
-{\r
- MAP_INFO *MapInfo;\r
- UINTN Length;\r
- VOID *Hob;\r
- DMA_BUFFER_INFO *DmaBufferInfo;\r
-\r
- Hob = GetFirstGuidHob (&mDmaBufferInfoGuid);\r
- DmaBufferInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
- DEBUG ((DEBUG_VERBOSE, "PeiIoMmuUnmap - Mapping - %x\n", Mapping));\r
- DEBUG ((DEBUG_VERBOSE, " DmaBufferCurrentTop - %x\n", DmaBufferInfo->DmaBufferCurrentTop));\r
- DEBUG ((DEBUG_VERBOSE, " DmaBufferCurrentBottom - %x\n", DmaBufferInfo->DmaBufferCurrentBottom));\r
-\r
- if (DmaBufferInfo->DmaBufferCurrentTop == 0) {\r
- return EFI_NOT_AVAILABLE_YET;\r
- }\r
-\r
- if (Mapping == NULL) {\r
- return EFI_SUCCESS;\r
- }\r
-\r
- MapInfo = Mapping;\r
- ASSERT (MapInfo->Signature == MAP_INFO_SIGNATURE);\r
- DEBUG ((DEBUG_VERBOSE, " Op(%x):DeviceAddress - %x, NumberOfBytes - %x\n", MapInfo->Operation, (UINTN)MapInfo->DeviceAddress, MapInfo->NumberOfBytes));\r
-\r
- //\r
- // If this is a write operation from the Bus Master's point of view,\r
- // then copy the contents of the mapped buffer into the real buffer\r
- // so the processor can read the contents of the real buffer.\r
- //\r
- if (MapInfo->Operation == EdkiiIoMmuOperationBusMasterWrite ||\r
- MapInfo->Operation == EdkiiIoMmuOperationBusMasterWrite64) {\r
- CopyMem (\r
- (VOID *) (UINTN) MapInfo->HostAddress,\r
- (VOID *) (UINTN) MapInfo->DeviceAddress,\r
- MapInfo->NumberOfBytes\r
- );\r
- }\r
-\r
- Length = MapInfo->NumberOfBytes + sizeof(MAP_INFO);\r
- if (DmaBufferInfo->DmaBufferCurrentBottom == MapInfo->DeviceAddress + Length) {\r
- DmaBufferInfo->DmaBufferCurrentBottom -= Length;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
- Allocates pages that are suitable for an OperationBusMasterCommonBuffer or\r
- OperationBusMasterCommonBuffer64 mapping.\r
-\r
- @param This The PPI instance pointer.\r
- @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
- EfiRuntimeServicesData.\r
- @param Pages The number of pages to allocate.\r
- @param HostAddress A pointer to store the base system memory address of the\r
- allocated range.\r
- @param Attributes The requested bit mask of attributes for the allocated range.\r
-\r
- @retval EFI_SUCCESS The requested memory pages were allocated.\r
- @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
- MEMORY_WRITE_COMBINE, MEMORY_CACHED and DUAL_ADDRESS_CYCLE.\r
- @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
- @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
- @retval EFI_NOT_AVAILABLE_YET DMA protection has been enabled, but DMA buffer are\r
- not available to be allocated yet.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiIoMmuAllocateBuffer (\r
- IN EDKII_IOMMU_PPI *This,\r
- IN EFI_MEMORY_TYPE MemoryType,\r
- IN UINTN Pages,\r
- IN OUT VOID **HostAddress,\r
- IN UINT64 Attributes\r
- )\r
-{\r
- UINTN Length;\r
- VOID *Hob;\r
- DMA_BUFFER_INFO *DmaBufferInfo;\r
-\r
- Hob = GetFirstGuidHob (&mDmaBufferInfoGuid);\r
- DmaBufferInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
- DEBUG ((DEBUG_VERBOSE, "PeiIoMmuAllocateBuffer - page - %x\n", Pages));\r
- DEBUG ((DEBUG_VERBOSE, " DmaBufferCurrentTop - %x\n", DmaBufferInfo->DmaBufferCurrentTop));\r
- DEBUG ((DEBUG_VERBOSE, " DmaBufferCurrentBottom - %x\n", DmaBufferInfo->DmaBufferCurrentBottom));\r
-\r
- if (DmaBufferInfo->DmaBufferCurrentTop == 0) {\r
- return EFI_NOT_AVAILABLE_YET;\r
- }\r
-\r
- Length = EFI_PAGES_TO_SIZE(Pages);\r
- if (Length > DmaBufferInfo->DmaBufferCurrentTop - DmaBufferInfo->DmaBufferCurrentBottom) {\r
- DEBUG ((DEBUG_ERROR, "PeiIoMmuAllocateBuffer - OUT_OF_RESOURCE\n"));\r
- ASSERT (FALSE);\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- *HostAddress = (VOID *)(UINTN)(DmaBufferInfo->DmaBufferCurrentTop - Length);\r
- DmaBufferInfo->DmaBufferCurrentTop -= Length;\r
-\r
- DEBUG ((DEBUG_VERBOSE, "PeiIoMmuAllocateBuffer - allocate - %x\n", *HostAddress));\r
- return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
- Frees memory that was allocated with AllocateBuffer().\r
-\r
- @param This The PPI instance pointer.\r
- @param Pages The number of pages to free.\r
- @param HostAddress The base system memory address of the allocated range.\r
-\r
- @retval EFI_SUCCESS The requested memory pages were freed.\r
- @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r
- was not allocated with AllocateBuffer().\r
- @retval EFI_NOT_AVAILABLE_YET DMA protection has been enabled, but DMA buffer are\r
- not available to be allocated yet.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiIoMmuFreeBuffer (\r
- IN EDKII_IOMMU_PPI *This,\r
- IN UINTN Pages,\r
- IN VOID *HostAddress\r
- )\r
-{\r
- UINTN Length;\r
- VOID *Hob;\r
- DMA_BUFFER_INFO *DmaBufferInfo;\r
-\r
- Hob = GetFirstGuidHob (&mDmaBufferInfoGuid);\r
- DmaBufferInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
- DEBUG ((DEBUG_VERBOSE, "PeiIoMmuFreeBuffer - page - %x, HostAddr - %x\n", Pages, HostAddress));\r
- DEBUG ((DEBUG_VERBOSE, " DmaBufferCurrentTop - %x\n", DmaBufferInfo->DmaBufferCurrentTop));\r
- DEBUG ((DEBUG_VERBOSE, " DmaBufferCurrentBottom - %x\n", DmaBufferInfo->DmaBufferCurrentBottom));\r
-\r
- if (DmaBufferInfo->DmaBufferCurrentTop == 0) {\r
- return EFI_NOT_AVAILABLE_YET;\r
- }\r
-\r
- Length = EFI_PAGES_TO_SIZE(Pages);\r
- if ((UINTN)HostAddress == DmaBufferInfo->DmaBufferCurrentTop) {\r
- DmaBufferInfo->DmaBufferCurrentTop += Length;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EDKII_IOMMU_PPI mIoMmuPpi = {\r
- EDKII_IOMMU_PPI_REVISION,\r
- PeiIoMmuSetAttribute,\r
- PeiIoMmuMap,\r
- PeiIoMmuUnmap,\r
- PeiIoMmuAllocateBuffer,\r
- PeiIoMmuFreeBuffer,\r
-};\r
-\r
-CONST EFI_PEI_PPI_DESCRIPTOR mIoMmuPpiList = {\r
- EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
- &gEdkiiIoMmuPpiGuid,\r
- (VOID *) &mIoMmuPpi\r
-};\r
-\r
-/**\r
- Initialize DMA protection.\r
-\r
- @param VTdInfo The VTd engine context information.\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
- )\r
-{\r
- EFI_STATUS Status;\r
- UINT32 LowMemoryAlignment;\r
- UINT64 HighMemoryAlignment;\r
- UINTN MemoryAlignment;\r
- UINTN LowBottom;\r
- UINTN LowTop;\r
- UINTN HighBottom;\r
- UINT64 HighTop;\r
- DMA_BUFFER_INFO *DmaBufferInfo;\r
- VOID *Hob;\r
- EFI_PEI_PPI_DESCRIPTOR *OldDescriptor;\r
- EDKII_IOMMU_PPI *OldIoMmuPpi;\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
- if (LowMemoryAlignment < HighMemoryAlignment) {\r
- MemoryAlignment = (UINTN)HighMemoryAlignment;\r
- } else {\r
- MemoryAlignment = LowMemoryAlignment;\r
- }\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
- // (Re)Install PPI.\r
- //\r
- Status = PeiServicesLocatePpi (\r
- &gEdkiiIoMmuPpiGuid,\r
- 0,\r
- &OldDescriptor,\r
- (VOID **) &OldIoMmuPpi\r
- );\r
- if (!EFI_ERROR (Status)) {\r
- Status = PeiServicesReInstallPpi (OldDescriptor, &mIoMmuPpiList);\r
- } else {\r
- Status = PeiServicesInstallPpi (&mIoMmuPpiList);\r
- }\r
- ASSERT_EFI_ERROR (Status);\r
-\r
- LowBottom = 0;\r
- LowTop = DmaBufferInfo->DmaBufferBase;\r
- HighBottom = DmaBufferInfo->DmaBufferBase + DmaBufferInfo->DmaBufferSize;\r
- HighTop = LShiftU64 (1, VTdInfo->HostAddressWidth + 1);\r
-\r
- Status = SetDmaProtectedRange (\r
- VTdInfo,\r
- VTdInfo->EngineMask,\r
- (UINT32)LowBottom,\r
- (UINT32)(LowTop - LowBottom),\r
- HighBottom,\r
- HighTop - HighBottom\r
- );\r
-\r
- if (EFI_ERROR(Status)) {\r
- FreePages ((VOID *)DmaBufferInfo->DmaBufferBase, EFI_SIZE_TO_PAGES(DmaBufferInfo->DmaBufferSize));\r
- }\r
-\r
- return Status;\r
-}\r
-\r
-/**\r
- Initializes the Intel VTd Info.\r
-\r
- @retval EFI_SUCCESS Usb bot driver is successfully initialized.\r
- @retval EFI_OUT_OF_RESOURCES Can't initialize the driver.\r
-\r
-**/\r
-EFI_STATUS\r
-InitVTdInfo (\r
- VOID\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_ACPI_DMAR_HEADER *AcpiDmarTable;\r
- VOID *Hob;\r
-\r
- Status = PeiServicesLocatePpi (\r
- &gEdkiiVTdInfoPpiGuid,\r
- 0,\r
- NULL,\r
- (VOID **)&AcpiDmarTable\r
- );\r
- ASSERT_EFI_ERROR(Status);\r
-\r
- DumpAcpiDMAR (AcpiDmarTable);\r
-\r
- //\r
- // Clear old VTdInfo Hob.\r
- //\r
- Hob = GetFirstGuidHob (&mVTdInfoGuid);\r
- if (Hob != NULL) {\r
- ZeroMem (&((EFI_HOB_GUID_TYPE *)Hob)->Name, sizeof(EFI_GUID));\r
- }\r
-\r
- //\r
- // Get DMAR information to local VTdInfo\r
- //\r
- Status = ParseDmarAcpiTableDrhd (AcpiDmarTable);\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
-\r
- //\r
- // NOTE: Do not parse RMRR here, because RMRR may cause PMR programming.\r
- //\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
- Initializes the Intel VTd PMR for all memory.\r
-\r
- @retval EFI_SUCCESS Usb bot driver is successfully initialized.\r
- @retval EFI_OUT_OF_RESOURCES Can't initialize the driver.\r
-\r
-**/\r
-EFI_STATUS\r
-InitVTdPmrForAll (\r
- VOID\r
- )\r
-{\r
- EFI_STATUS Status;\r
- VOID *Hob;\r
- VTD_INFO *VTdInfo;\r
- UINTN LowBottom;\r
- UINTN LowTop;\r
- UINTN HighBottom;\r
- UINT64 HighTop;\r
-\r
- Hob = GetFirstGuidHob (&mVTdInfoGuid);\r
- VTdInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
- LowBottom = 0;\r
- LowTop = 0;\r
- HighBottom = 0;\r
- HighTop = LShiftU64 (1, VTdInfo->HostAddressWidth + 1);\r
-\r
- Status = SetDmaProtectedRange (\r
- VTdInfo,\r
- VTdInfo->EngineMask,\r
- (UINT32)LowBottom,\r
- (UINT32)(LowTop - LowBottom),\r
- HighBottom,\r
- HighTop - HighBottom\r
- );\r
-\r
- return Status;\r
-}\r
-\r
-/**\r
- Initializes the Intel VTd PMR for DMA buffer.\r
-\r
- @retval EFI_SUCCESS Usb bot driver is successfully initialized.\r
- @retval EFI_OUT_OF_RESOURCES Can't initialize the driver.\r
-\r
-**/\r
-EFI_STATUS\r
-InitVTdPmrForDma (\r
- VOID\r
- )\r
-{\r
- EFI_STATUS Status;\r
- VOID *Hob;\r
- VTD_INFO *VTdInfo;\r
-\r
- Hob = GetFirstGuidHob (&mVTdInfoGuid);\r
- VTdInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
- //\r
- // If there is RMRR memory, parse it here.\r
- //\r
- ParseDmarAcpiTableRmrr (VTdInfo);\r
-\r
- //\r
- // Allocate a range in PEI memory as DMA buffer\r
- // Mark others to be DMA protected.\r
- //\r
- Status = InitDmaProtection (VTdInfo);\r
-\r
- return Status;\r
-}\r
-\r
-/**\r
- This function handles S3 resume task at the end of PEI\r
-\r
- @param[in] PeiServices Pointer to PEI Services Table.\r
- @param[in] NotifyDesc Pointer to the descriptor for the Notification event that\r
- caused this function to execute.\r
- @param[in] Ppi Pointer to the PPI data associated with this function.\r
-\r
- @retval EFI_STATUS Always return EFI_SUCCESS\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-S3EndOfPeiNotify(\r
- IN EFI_PEI_SERVICES **PeiServices,\r
- IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,\r
- IN VOID *Ppi\r
- )\r
-{\r
- VOID *Hob;\r
- VTD_INFO *VTdInfo;\r
- UINT64 EngineMask;\r
-\r
- DEBUG((DEBUG_INFO, "VTdPmr S3EndOfPeiNotify\n"));\r
-\r
- if ((PcdGet8(PcdVTdPolicyPropertyMask) & BIT1) == 0) {\r
- Hob = GetFirstGuidHob (&mVTdInfoGuid);\r
- if (Hob == NULL) {\r
- return EFI_SUCCESS;\r
- }\r
- VTdInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
- EngineMask = LShiftU64 (1, VTdInfo->VTdEngineCount) - 1;\r
- DisableDmaProtection (VTdInfo, EngineMask);\r
- }\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_PEI_NOTIFY_DESCRIPTOR mS3EndOfPeiNotifyDesc = {\r
- (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
- &gEfiEndOfPeiSignalPpiGuid,\r
- S3EndOfPeiNotify\r
-};\r
-\r
-/**\r
- This function handles VTd engine setup\r
-\r
- @param[in] PeiServices Pointer to PEI Services Table.\r
- @param[in] NotifyDesc Pointer to the descriptor for the Notification event that\r
- caused this function to execute.\r
- @param[in] Ppi Pointer to the PPI data associated with this function.\r
-\r
- @retval EFI_STATUS Always return EFI_SUCCESS\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VTdInfoNotify (\r
- IN EFI_PEI_SERVICES **PeiServices,\r
- IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,\r
- IN VOID *Ppi\r
- )\r
-{\r
- EFI_STATUS Status;\r
- VOID *MemoryDiscovered;\r
- UINT64 EnabledEngineMask;\r
- VOID *Hob;\r
- VTD_INFO *VTdInfo;\r
- BOOLEAN MemoryInitialized;\r
-\r
- DEBUG ((DEBUG_INFO, "VTdInfoNotify\n"));\r
-\r
- //\r
- // Check if memory is initialized.\r
- //\r
- MemoryInitialized = FALSE;\r
- Status = PeiServicesLocatePpi (\r
- &gEfiPeiMemoryDiscoveredPpiGuid,\r
- 0,\r
- NULL,\r
- &MemoryDiscovered\r
- );\r
- if (!EFI_ERROR(Status)) {\r
- MemoryInitialized = TRUE;\r
- }\r
-\r
- DEBUG ((DEBUG_INFO, "MemoryInitialized - %x\n", MemoryInitialized));\r
-\r
- if (!MemoryInitialized) {\r
- //\r
- // If the memory is not initialized,\r
- // Protect all system memory\r
- //\r
- InitVTdInfo ();\r
- InitVTdPmrForAll ();\r
-\r
- //\r
- // Install PPI.\r
- //\r
- Status = PeiServicesInstallPpi (&mIoMmuPpiList);\r
- ASSERT_EFI_ERROR(Status);\r
- } else {\r
- //\r
- // If the memory is initialized,\r
- // Allocate DMA buffer and protect rest system memory\r
- //\r
-\r
- //\r
- // NOTE: We need reinit VTdInfo because previous information might be overriden.\r
- //\r
- InitVTdInfo ();\r
-\r
- Hob = GetFirstGuidHob (&mVTdInfoGuid);\r
- VTdInfo = GET_GUID_HOB_DATA(Hob);\r
-\r
- //\r
- // NOTE: We need check if PMR is enabled or not.\r
- //\r
- EnabledEngineMask = GetDmaProtectionEnabledEngineMask (VTdInfo, VTdInfo->EngineMask);\r
- if (EnabledEngineMask != 0) {\r
- EnableVTdTranslationProtection (VTdInfo, EnabledEngineMask);\r
- DisableDmaProtection (VTdInfo, EnabledEngineMask);\r
- }\r
- InitVTdPmrForDma ();\r
- if (EnabledEngineMask != 0) {\r
- DisableVTdTranslationProtection (VTdInfo, EnabledEngineMask);\r
- }\r
-\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_PEI_NOTIFY_DESCRIPTOR mVTdInfoNotifyDesc = {\r
- (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
- &gEdkiiVTdInfoPpiGuid,\r
- VTdInfoNotify\r
-};\r
-\r
-/**\r
- Initializes the Intel VTd PMR PEIM.\r
-\r
- @param FileHandle Handle of the file being invoked.\r
- @param PeiServices Describes the list of possible PEI Services.\r
-\r
- @retval EFI_SUCCESS Usb bot driver is successfully initialized.\r
- @retval EFI_OUT_OF_RESOURCES Can't initialize the driver.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-IntelVTdPmrInitialize (\r
- IN EFI_PEI_FILE_HANDLE FileHandle,\r
- IN CONST EFI_PEI_SERVICES **PeiServices\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_BOOT_MODE BootMode;\r
- DMA_BUFFER_INFO *DmaBufferInfo;\r
-\r
- DEBUG ((DEBUG_INFO, "IntelVTdPmrInitialize\n"));\r
-\r
- if ((PcdGet8(PcdVTdPolicyPropertyMask) & BIT0) == 0) {\r
- return EFI_UNSUPPORTED;\r
- }\r
-\r
- DmaBufferInfo = BuildGuidHob (&mDmaBufferInfoGuid, sizeof(DMA_BUFFER_INFO));\r
- ASSERT(DmaBufferInfo != NULL);\r
- if (DmaBufferInfo == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- ZeroMem (DmaBufferInfo, sizeof(DMA_BUFFER_INFO));\r
-\r
- PeiServicesGetBootMode (&BootMode);\r
-\r
- if (BootMode == BOOT_ON_S3_RESUME) {\r
- DmaBufferInfo->DmaBufferSize = PcdGet32 (PcdVTdPeiDmaBufferSizeS3);\r
- } else {\r
- DmaBufferInfo->DmaBufferSize = PcdGet32 (PcdVTdPeiDmaBufferSize);\r
- }\r
-\r
- Status = PeiServicesNotifyPpi (&mVTdInfoNotifyDesc);\r
- ASSERT_EFI_ERROR (Status);\r
-\r
- //\r
- // Register EndOfPei Notify for S3\r
- //\r
- if (BootMode == BOOT_ON_S3_RESUME) {\r
- Status = PeiServicesNotifyPpi (&mS3EndOfPeiNotifyDesc);\r
- ASSERT_EFI_ERROR (Status);\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r