]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelSiliconPkg/IntelVTdDxe/DmaProtection.h
IntelSiliconPkg/dec: Add VTd policy PCD
[mirror_edk2.git] / IntelSiliconPkg / IntelVTdDxe / DmaProtection.h
index 8cfa69cb2364a670e77bc7f95b4cceb3c302cf56..f7b5292f23e83267e2997b11ae2a9433fee5a864 100644 (file)
@@ -25,6 +25,9 @@
 #include <Library/PciSegmentLib.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/UefiLib.h>\r
+#include <Library/CacheMaintenanceLib.h>\r
+#include <Library/PerformanceLib.h>\r
+#include <Library/PrintLib.h>\r
 \r
 #include <Guid/EventGroup.h>\r
 #include <Guid/Acpi.h>\r
 #define ALIGN_VALUE_LOW(Value, Alignment) ((Value) & (~((Alignment) - 1)))\r
 \r
 //\r
-// This is the initial max PCI descriptor.\r
+// This is the initial max PCI DATA number.\r
 // The number may be enlarged later.\r
 //\r
-#define MAX_PCI_DESCRIPTORS             0x100\r
+#define MAX_VTD_PCI_DATA_NUMBER             0x100\r
 \r
 typedef struct {\r
-  BOOLEAN                IncludeAllFlag;\r
-  UINTN                  PciDescriptorNumber;\r
-  UINTN                  PciDescriptorMaxNumber;\r
-  BOOLEAN                *IsRealPciDevice;\r
-  VTD_SOURCE_ID          *PciDescriptors;\r
+  UINT8                            DeviceType;\r
+  VTD_SOURCE_ID                    PciSourceId;\r
+  EDKII_PLATFORM_VTD_PCI_DEVICE_ID PciDeviceId;\r
+  // for statistic analysis\r
+  UINTN                            AccessCount;\r
+} PCI_DEVICE_DATA;\r
+\r
+typedef struct {\r
+  BOOLEAN                          IncludeAllFlag;\r
+  UINTN                            PciDeviceDataNumber;\r
+  UINTN                            PciDeviceDataMaxNumber;\r
+  PCI_DEVICE_DATA                  *PciDeviceData;\r
 } PCI_DEVICE_INFORMATION;\r
 \r
 typedef struct {\r
@@ -68,10 +78,34 @@ typedef struct {
   VTD_ROOT_ENTRY                   *RootEntryTable;\r
   VTD_EXT_ROOT_ENTRY               *ExtRootEntryTable;\r
   VTD_SECOND_LEVEL_PAGING_ENTRY    *FixedSecondLevelPagingEntry;\r
+  BOOLEAN                          HasDirtyContext;\r
   BOOLEAN                          HasDirtyPages;\r
   PCI_DEVICE_INFORMATION           PciDeviceInfo;\r
 } VTD_UNIT_INFORMATION;\r
 \r
+/**\r
+  The scan bus callback function.\r
+\r
+  It is called in PCI bus scan for each PCI device under the bus.\r
+\r
+  @param[in]  Context               The context of the callback.\r
+  @param[in]  Segment               The segment of the source.\r
+  @param[in]  Bus                   The bus of the source.\r
+  @param[in]  Device                The device of the source.\r
+  @param[in]  Function              The function of the source.\r
+\r
+  @retval EFI_SUCCESS           The specific PCI device is processed in the callback.\r
+**/\r
+typedef\r
+EFI_STATUS\r
+(EFIAPI *SCAN_BUS_FUNC_CALLBACK_FUNC) (\r
+  IN VOID           *Context,\r
+  IN UINT16         Segment,\r
+  IN UINT8          Bus,\r
+  IN UINT8          Device,\r
+  IN UINT8          Function\r
+  );\r
+\r
 extern EFI_ACPI_DMAR_HEADER  *mAcpiDmarTable;\r
 \r
 extern UINT64                           mVtdHostAddressWidthMask;\r
@@ -124,40 +158,6 @@ DisableDmar (
   VOID\r
   );\r
 \r
-/**\r
-  Invalid VTd IOTLB page.\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
-**/\r
-EFI_STATUS\r
-InvalidateVtdIOTLBPage (\r
-  IN UINTN  VtdIndex,\r
-  IN UINT64 Address,\r
-  IN UINT8  AddressMode,\r
-  IN UINT16 DomainIdentifier\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
-\r
-  @retval EFI_SUCCESS           VTd IOTLB domain is invalidated.\r
-  @retval EFI_DEVICE_ERROR      VTd IOTLB domain is not invalidated.\r
-**/\r
-EFI_STATUS\r
-InvalidateVtdIOTLBDomain (\r
-  IN UINTN  VtdIndex,\r
-  IN UINT16 DomainIdentifier\r
-  );\r
-\r
 /**\r
   Invalid VTd global IOTLB.\r
 \r
@@ -210,13 +210,12 @@ DumpVtdECapRegs (
   );\r
 \r
 /**\r
-  Register PCI device to VTd engine as PCI descriptor.\r
+  Register PCI device to VTd engine.\r
 \r
   @param[in]  VtdIndex              The index of VTd engine.\r
   @param[in]  Segment               The segment of the source.\r
   @param[in]  SourceId              The SourceId of the source.\r
-  @param[in]  IsRealPciDevice       TRUE: It is a real PCI device.\r
-                                    FALSE: It is not a real PCI device.\r
+  @param[in]  DeviceType            The DMAR device scope type.\r
   @param[in]  CheckExist            TRUE: ERROR will be returned if the PCI device is already registered.\r
                                     FALSE: SUCCESS will be returned if the PCI device is registered.\r
 \r
@@ -229,25 +228,47 @@ RegisterPciDevice (
   IN UINTN          VtdIndex,\r
   IN UINT16         Segment,\r
   IN VTD_SOURCE_ID  SourceId,\r
-  IN BOOLEAN        IsRealPciDevice,\r
+  IN UINT8          DeviceType,\r
   IN BOOLEAN        CheckExist\r
   );\r
 \r
 /**\r
-  Scan PCI bus and register PCI devices under the bus.\r
+  The scan bus callback function to always enable page attribute.\r
 \r
-  @param[in]  VtdIndex              The index of VTd engine.\r
+  @param[in]  Context               The context of the callback.\r
   @param[in]  Segment               The segment of the source.\r
   @param[in]  Bus                   The bus of the source.\r
+  @param[in]  Device                The device of the source.\r
+  @param[in]  Function              The function of the source.\r
 \r
-  @retval EFI_SUCCESS           The PCI devices under the bus are registered.\r
-  @retval EFI_OUT_OF_RESOURCES  No enough resource to register a new PCI device.\r
+  @retval EFI_SUCCESS           The VTd entry is updated to always enable all DMA access for the specific device.\r
 **/\r
 EFI_STATUS\r
-ScanPciBus (\r
-  IN UINTN          VtdIndex,\r
+EFIAPI\r
+ScanBusCallbackRegisterPciDevice (\r
+  IN VOID           *Context,\r
   IN UINT16         Segment,\r
-  IN UINT8          Bus\r
+  IN UINT8          Bus,\r
+  IN UINT8          Device,\r
+  IN UINT8          Function\r
+  );\r
+\r
+/**\r
+  Scan PCI bus and invoke callback function for each PCI devices under the bus.\r
+\r
+  @param[in]  Context               The context of the callback function.\r
+  @param[in]  Segment               The segment of the source.\r
+  @param[in]  Bus                   The bus of the source.\r
+  @param[in]  Callback              The callback function in PCI scan.\r
+\r
+  @retval EFI_SUCCESS           The PCI devices under the bus are scaned.\r
+**/\r
+EFI_STATUS\r
+ScanPciBus (\r
+  IN VOID                         *Context,\r
+  IN UINT16                       Segment,\r
+  IN UINT8                        Bus,\r
+  IN SCAN_BUS_FUNC_CALLBACK_FUNC  Callback\r
   );\r
 \r
 /**\r
@@ -268,8 +289,8 @@ DumpPciDeviceInfo (
   @param[out] ExtContextEntry       The ExtContextEntry of the source.\r
   @param[out] ContextEntry          The ContextEntry of the source.\r
 \r
-  @return The index of the PCI descriptor.\r
-  @retval (UINTN)-1  The PCI descriptor is not found.\r
+  @return The index of the VTd engine.\r
+  @retval (UINTN)-1  The VTd engine is not found.\r
 **/\r
 UINTN\r
 FindVtdIndexByPciDevice (\r
@@ -344,6 +365,7 @@ DumpSecondLevelPagingEntry (
   Set VTd attribute for a system memory.\r
 \r
   @param[in]  VtdIndex                The index used to identify a VTd engine.\r
+  @param[in]  DomainIdentifier        The domain ID of the source.\r
   @param[in]  SecondLevelPagingEntry  The second level paging entry in VTd table for the device.\r
   @param[in]  BaseAddress             The base of device memory address to be used as the DMA memory.\r
   @param[in]  Length                  The length of device memory address to be used as the DMA memory.\r
@@ -362,6 +384,7 @@ DumpSecondLevelPagingEntry (
 EFI_STATUS\r
 SetPageAttribute (\r
   IN UINTN                         VtdIndex,\r
+  IN UINT16                        DomainIdentifier,\r
   IN VTD_SECOND_LEVEL_PAGING_ENTRY *SecondLevelPagingEntry,\r
   IN UINT64                        BaseAddress,\r
   IN UINT64                        Length,\r
@@ -397,17 +420,17 @@ SetAccessAttribute (
   );\r
 \r
 /**\r
-  Return the index of PCI descriptor.\r
+  Return the index of PCI data.\r
 \r
   @param[in]  VtdIndex          The index used to identify a VTd engine.\r
   @param[in]  Segment           The Segment used to identify a VTd engine.\r
   @param[in]  SourceId          The SourceId used to identify a VTd engine and table entry.\r
 \r
-  @return The index of the PCI descriptor.\r
-  @retval (UINTN)-1  The PCI descriptor is not found.\r
+  @return The index of the PCI data.\r
+  @retval (UINTN)-1  The PCI data is not found.\r
 **/\r
 UINTN\r
-GetPciDescriptor (\r
+GetPciDataIndex (\r
   IN UINTN          VtdIndex,\r
   IN UINT16         Segment,\r
   IN VTD_SOURCE_ID  SourceId\r
@@ -500,4 +523,40 @@ AllocateZeroPages (
   IN UINTN  Pages\r
   );\r
 \r
+/**\r
+  Flush VTD page table and context table memory.\r
+\r
+  This action is to make sure the IOMMU engine can get final data in memory.\r
+\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
+VOID\r
+FlushPageTableMemory (\r
+  IN UINTN  VtdIndex,\r
+  IN UINTN  Base,\r
+  IN UINTN  Size\r
+  );\r
+\r
+/**\r
+  Get PCI device information from DMAR DevScopeEntry.\r
+\r
+  @param[in]  Segment               The segment number.\r
+  @param[in]  DmarDevScopeEntry     DMAR DevScopeEntry\r
+  @param[out] Bus                   The bus number.\r
+  @param[out] Device                The device number.\r
+  @param[out] Function              The function number.\r
+\r
+  @retval EFI_SUCCESS  The PCI device information is returned.\r
+**/\r
+EFI_STATUS\r
+GetPciBusDeviceFunction (\r
+  IN  UINT16                                      Segment,\r
+  IN  EFI_ACPI_DMAR_DEVICE_SCOPE_STRUCTURE_HEADER *DmarDevScopeEntry,\r
+  OUT UINT8                                       *Bus,\r
+  OUT UINT8                                       *Device,\r
+  OUT UINT8                                       *Function\r
+  );\r
+\r
 #endif\r