--- /dev/null
+/** @file\r
+ Platform VTd Sample driver.\r
+\r
+ Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
+ This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <IndustryStandard/Vtd.h>\r
+#include <Protocol/PlatformVtdPolicy.h>\r
+#include <Protocol/PciIo.h>\r
+#include <Protocol/DevicePath.h>\r
+\r
+#include <Library/IoLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DevicePathLib.h>\r
+\r
+#include <IndustryStandard/DmaRemappingReportingTable.h>\r
+\r
+typedef struct {\r
+ ACPI_EXTENDED_HID_DEVICE_PATH I2cController;\r
+ UINT8 HidStr[8];\r
+ UINT8 UidStr[1];\r
+ UINT8 CidStr[8];\r
+} PLATFORM_I2C_CONTROLLER_DEVICE_PATH;\r
+\r
+typedef struct {\r
+ ACPI_EXTENDED_HID_DEVICE_PATH I2cDevice;\r
+ UINT8 HidStr[13];\r
+ UINT8 UidStr[1];\r
+ UINT8 CidStr[13];\r
+} PLATFORM_I2C_DEVICE_DEVICE_PATH;\r
+\r
+typedef struct {\r
+ PLATFORM_I2C_CONTROLLER_DEVICE_PATH I2cController;\r
+ PLATFORM_I2C_DEVICE_DEVICE_PATH I2cDevice;\r
+ EFI_DEVICE_PATH_PROTOCOL End;\r
+} PLATFORM_I2C_DEVICE_PATH;\r
+\r
+typedef struct {\r
+ ACPI_HID_DEVICE_PATH PciRootBridge;\r
+ PCI_DEVICE_PATH PciDevice;\r
+ EFI_DEVICE_PATH_PROTOCOL EndDevicePath;\r
+} PLATFORM_PCI_DEVICE_PATH;\r
+\r
+typedef struct {\r
+ ACPI_HID_DEVICE_PATH PciRootBridge;\r
+ PCI_DEVICE_PATH PciBridge;\r
+ PCI_DEVICE_PATH PciDevice;\r
+ EFI_DEVICE_PATH_PROTOCOL EndDevicePath;\r
+} PLATFORM_PCI_BRIDGE_DEVICE_PATH;\r
+\r
+typedef struct {\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ UINT16 Segment;\r
+ VTD_SOURCE_ID SourceId;\r
+} PLATFORM_ACPI_DEVICE_MAPPING;\r
+\r
+#define PLATFORM_PCI_ROOT_BRIDGE \\r
+ { \\r
+ { \\r
+ ACPI_DEVICE_PATH, \\r
+ ACPI_DP, \\r
+ { \\r
+ (UINT8) (sizeof (ACPI_HID_DEVICE_PATH)), \\r
+ (UINT8) ((sizeof (ACPI_HID_DEVICE_PATH)) >> 8) \\r
+ }, \\r
+ }, \\r
+ EISA_PNP_ID (0x0A03), \\r
+ 0 \\r
+ }\r
+\r
+#define PLATFORM_END_ENTIRE \\r
+ { \\r
+ END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { END_DEVICE_PATH_LENGTH, 0 } \\r
+ }\r
+\r
+#define PLATFORM_PCI(Device, Function) \\r
+ { \\r
+ { \\r
+ HARDWARE_DEVICE_PATH, \\r
+ HW_PCI_DP, \\r
+ { \\r
+ (UINT8) (sizeof (PCI_DEVICE_PATH)), \\r
+ (UINT8) ((sizeof (PCI_DEVICE_PATH)) >> 8) \\r
+ } \\r
+ }, \\r
+ (Function), \\r
+ (Device) \\r
+ }\r
+\r
+#define PLATFORM_I2C(Hid, Uid, Cid, HidStr, UidStr, CidStr) \\r
+ { \\r
+ { \\r
+ { \\r
+ ACPI_DEVICE_PATH, \\r
+ ACPI_EXTENDED_DP, \\r
+ {sizeof(ACPI_EXTENDED_HID_DEVICE_PATH) + sizeof(HidStr) + sizeof(UidStr) + sizeof(CidStr), 0} \\r
+ }, \\r
+ Hid, \\r
+ Uid, \\r
+ Cid \\r
+ }, \\r
+ HidStr, \\r
+ UidStr, \\r
+ CidStr \\r
+ }\r
+\r
+PLATFORM_I2C_DEVICE_PATH mPlatformI2CDevicePath = {\r
+ PLATFORM_I2C(0, 2, 0, "INT33C3", "", "INT33C3"),\r
+ PLATFORM_I2C(0, 1, 0, "I2C01\\TPANEL", "", "I2C01\\TPANEL"),\r
+ PLATFORM_END_ENTIRE\r
+};\r
+\r
+PLATFORM_ACPI_DEVICE_MAPPING mAcpiDeviceMapping[] = {\r
+ {\r
+ (EFI_DEVICE_PATH_PROTOCOL *)&mPlatformI2CDevicePath,\r
+ 0x0, // Segment\r
+ {{0x01, 0x15, 0x00}} // Function, Device, Bus\r
+ }\r
+};\r
+\r
+PLATFORM_PCI_BRIDGE_DEVICE_PATH mPlatformPciBridgeDevicePath = {\r
+ PLATFORM_PCI_ROOT_BRIDGE,\r
+ PLATFORM_PCI(0x1C, 1),\r
+ PLATFORM_PCI(0, 0),\r
+ PLATFORM_END_ENTIRE\r
+};\r
+\r
+#pragma pack(1)\r
+\r
+typedef struct {\r
+ EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO ExceptionDeviceInfo;\r
+ EDKII_PLATFORM_VTD_DEVICE_SCOPE DeviceScope;\r
+ EFI_ACPI_DMAR_PCI_PATH PciBridge;\r
+ EFI_ACPI_DMAR_PCI_PATH PciDevice;\r
+} PLATFORM_EXCEPTION_DEVICE_SCOPE_STRUCT;\r
+\r
+typedef struct {\r
+ EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO ExceptionDeviceInfo;\r
+ EDKII_PLATFORM_VTD_PCI_DEVICE_ID PciDeviceId;\r
+} PLATFORM_EXCEPTION_PCI_DEVICE_ID_STRUCT;\r
+\r
+#pragma pack()\r
+\r
+PLATFORM_EXCEPTION_DEVICE_SCOPE_STRUCT mExceptionDeviceScopeList[] = {\r
+ {\r
+ {\r
+ EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO_TYPE_DEVICE_SCOPE,\r
+ sizeof(PLATFORM_EXCEPTION_DEVICE_SCOPE_STRUCT)\r
+ }, // ExceptionDeviceInfo\r
+ {\r
+ 0, // SegmentNumber\r
+ {\r
+ EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_ENDPOINT, // Type\r
+ sizeof(EFI_ACPI_DMAR_DEVICE_SCOPE_STRUCTURE_HEADER) +\r
+ 2 * sizeof(EFI_ACPI_DMAR_PCI_PATH), // Length\r
+ 0, // Reserved2\r
+ 0, // EnumerationId\r
+ 0, // StartBusNumber\r
+ },\r
+ }, // DeviceScope\r
+ { 0x1C, 1 }, // PciBridge\r
+ { 0x0, 0 }, // PciDevice\r
+ },\r
+};\r
+\r
+PLATFORM_EXCEPTION_PCI_DEVICE_ID_STRUCT mExceptionPciDeviceIdList[] = {\r
+ {\r
+ {\r
+ EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO_TYPE_PCI_DEVICE_ID,\r
+ sizeof(PLATFORM_EXCEPTION_PCI_DEVICE_ID_STRUCT)\r
+ }, // ExceptionDeviceInfo\r
+ {\r
+ 0x8086, // VendorId\r
+ 0x9D2F, // DeviceId\r
+ 0x21, // RevisionId\r
+ 0x8086, // SubsystemVendorId\r
+ 0x7270, // SubsystemDeviceId\r
+ },\r
+ },\r
+};\r
+\r
+/**\r
+ Compares 2 device path.\r
+\r
+ @param[in] DevicePath1 A device path with EndDevicePath node.\r
+ @param[in] DevicePath2 A device path with EndDevicePath node.\r
+\r
+ @retval TRUE 2 device path are identical.\r
+ @retval FALSE 2 device path are not identical.\r
+**/\r
+BOOLEAN\r
+CompareDevicePath (\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath1,\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath2\r
+ )\r
+{\r
+ UINTN Size1;\r
+ UINTN Size2;\r
+\r
+ Size1 = GetDevicePathSize (DevicePath1);\r
+ Size2 = GetDevicePathSize (DevicePath2);\r
+ if (Size1 != Size2) {\r
+ return FALSE;\r
+ }\r
+ if (CompareMem (DevicePath1, DevicePath2, Size1) != 0) {\r
+ return FALSE;\r
+ }\r
+ return TRUE;\r
+}\r
+\r
+/**\r
+ Get the VTD SourceId from the device handler.\r
+ This function is required for non PCI device handler.\r
+\r
+ Pseudo-algo in Intel VTd driver:\r
+ Status = PlatformGetVTdDeviceId ();\r
+ if (EFI_ERROR(Status)) {\r
+ if (DeviceHandle is PCI) {\r
+ Get SourceId from Bus/Device/Function\r
+ } else {\r
+ return EFI_UNSUPPORTED\r
+ }\r
+ }\r
+ Get VTd engine by Segment/Bus/Device/Function.\r
+\r
+ @param[in] This The protocol instance pointer.\r
+ @param[in] DeviceHandle Device Identifier in UEFI.\r
+ @param[out] DeviceInfo DeviceInfo for indentify the VTd engine in ACPI Table\r
+ and the VTd page entry.\r
+\r
+ @retval EFI_SUCCESS The VtdIndex and SourceId are returned.\r
+ @retval EFI_INVALID_PARAMETER DeviceHandle is not a valid handler.\r
+ @retval EFI_INVALID_PARAMETER DeviceInfo is NULL.\r
+ @retval EFI_NOT_FOUND The Segment or SourceId information is NOT found.\r
+ @retval EFI_UNSUPPORTED This function is not supported.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PlatformVTdGetDeviceId (\r
+ IN EDKII_PLATFORM_VTD_POLICY_PROTOCOL *This,\r
+ IN EFI_HANDLE DeviceHandle,\r
+ OUT EDKII_PLATFORM_VTD_DEVICE_INFO *DeviceInfo\r
+ )\r
+{\r
+ EFI_PCI_IO_PROTOCOL *PciIo;\r
+ UINTN Seg;\r
+ UINTN Bus;\r
+ UINTN Dev;\r
+ UINTN Func;\r
+ EFI_STATUS Status;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ UINTN Index;\r
+\r
+ DEBUG ((DEBUG_VERBOSE, "PlatformVTdGetDeviceId\n"));\r
+\r
+ if (DeviceInfo == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (DeviceHandle == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Handle PCI device\r
+ //\r
+ Status = gBS->HandleProtocol (DeviceHandle, &gEfiPciIoProtocolGuid, (VOID **)&PciIo);\r
+ if (!EFI_ERROR(Status)) {\r
+ Status = PciIo->GetLocation (PciIo, &Seg, &Bus, &Dev, &Func);\r
+ if (EFI_ERROR(Status)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ DeviceInfo->Segment = (UINT16)Seg;\r
+ DeviceInfo->SourceId.Bits.Bus = (UINT8)Bus;\r
+ DeviceInfo->SourceId.Bits.Device = (UINT8)Dev;\r
+ DeviceInfo->SourceId.Bits.Function = (UINT8)Func;\r
+\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ //\r
+ // Handle ACPI device\r
+ //\r
+ Status = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePath);\r
+ if (!EFI_ERROR(Status)) {\r
+ for (Index = 0; Index < ARRAY_SIZE(mAcpiDeviceMapping); Index++) {\r
+ if (CompareDevicePath (mAcpiDeviceMapping[Index].DevicePath, DevicePath)) {\r
+ DeviceInfo->Segment = mAcpiDeviceMapping[Index].Segment;\r
+ DeviceInfo->SourceId = mAcpiDeviceMapping[Index].SourceId;\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+ }\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+ Get a list of the exception devices.\r
+\r
+ The VTd driver should always set ALLOW for the device in this list.\r
+\r
+ @param[in] This The protocol instance pointer.\r
+ @param[out] DeviceInfoCount The count of the list of DeviceInfo.\r
+ @param[out] DeviceInfo A callee allocated buffer to hold a list of DeviceInfo.\r
+ Each DeviceInfo pointer points to EDKII_PLATFORM_VTD_EXCEPTION_DEVICE_INFO.\r
+\r
+ @retval EFI_SUCCESS The DeviceInfoCount and DeviceInfo are returned.\r
+ @retval EFI_INVALID_PARAMETER DeviceInfoCount is NULL, or DeviceInfo is NULL.\r
+ @retval EFI_UNSUPPORTED This function is not supported.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PlatformVTdGetExceptionDeviceList (\r
+ IN EDKII_PLATFORM_VTD_POLICY_PROTOCOL *This,\r
+ OUT UINTN *DeviceInfoCount,\r
+ OUT VOID **DeviceInfo\r
+ )\r
+{\r
+ DEBUG ((DEBUG_VERBOSE, "PlatformVTdGetExceptionDeviceList\n"));\r
+\r
+ if (DeviceInfoCount == NULL || DeviceInfo == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Sample codes for device scope based exception list.\r
+ // Uncomment to take affect and comment the sample codes for PCI vendor id\r
+ // based exception list.\r
+ //\r
+ /*\r
+ *DeviceInfo = AllocateZeroPool (sizeof(mExceptionDeviceScopeList));\r
+ if (*DeviceInfo == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ CopyMem (*DeviceInfo, mExceptionDeviceScopeList, sizeof(mExceptionDeviceScopeList));\r
+\r
+ *DeviceInfoCount = ARRAY_SIZE(mExceptionDeviceScopeList);\r
+ */\r
+\r
+ //\r
+ // Sample codes for PCI vendor id based exception list.\r
+ // Uncomment to take affect and comment the sample codes for device scope\r
+ // based exception list.\r
+ //\r
+ *DeviceInfo = AllocateZeroPool (sizeof(mExceptionPciDeviceIdList));\r
+ if (*DeviceInfo == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ CopyMem (*DeviceInfo, mExceptionPciDeviceIdList, sizeof(mExceptionPciDeviceIdList));\r
+\r
+ *DeviceInfoCount = ARRAY_SIZE(mExceptionPciDeviceIdList);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EDKII_PLATFORM_VTD_POLICY_PROTOCOL mPlatformVTdSample = {\r
+ EDKII_PLATFORM_VTD_POLICY_PROTOCOL_REVISION,\r
+ PlatformVTdGetDeviceId,\r
+ PlatformVTdGetExceptionDeviceList,\r
+};\r
+\r
+/**\r
+ Platform VTd sample driver.\r
+\r
+ @param[in] ImageHandle ImageHandle of the loaded driver\r
+ @param[in] SystemTable Pointer to the System Table\r
+\r
+ @retval EFI_SUCCESS The Protocol is installed.\r
+ @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.\r
+ @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PlatformVTdSampleInitialize (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_HANDLE Handle;\r
+\r
+ Handle = NULL;\r
+ Status = gBS->InstallMultipleProtocolInterfaces (\r
+ &Handle,\r
+ &gEdkiiPlatformVTdPolicyProtocolGuid, &mPlatformVTdSample,\r
+ NULL\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ return Status;\r
+}\r