]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg: Add IoMmuDxe driver
authorBrijesh Singh <brijesh.singh@amd.com>
Thu, 6 Jul 2017 13:28:40 +0000 (09:28 -0400)
committerJordan Justen <jordan.l.justen@intel.com>
Tue, 11 Jul 2017 04:17:28 +0000 (21:17 -0700)
The IOMMU protocol driver provides capabilities to set a DMA access
attribute and methods to allocate, free, map and unmap the DMA memory
for the PCI Bus devices.

Due to security reasons all DMA operations inside the SEV guest must
be performed on shared (i.e unencrypted) pages. The IOMMU protocol
driver for the SEV guest uses a bounce buffer to map guest DMA buffer
to shared pages inorder to provide the support for DMA operations inside
SEV guest.

IoMmuDxe driver looks for SEV capabilities, if present then it installs
the real IOMMU protocol otherwise it installs placeholder protocol.
Currently, PciHostBridgeDxe and QemuFWCfgLib need to know the existance
of IOMMU protocol. The modules needing to know the existance of IOMMU
support should add

  gEdkiiIoMmuProtocolGuid OR gIoMmuAbsentProtocolGuid

in their depex to ensure that platform IOMMU detection has been performed.

Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Leo Duran <leo.duran@amd.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Suggested-by: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
Acked-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg/IoMmuDxe/AmdSevIoMmu.c [new file with mode: 0644]
OvmfPkg/IoMmuDxe/AmdSevIoMmu.h [new file with mode: 0644]
OvmfPkg/IoMmuDxe/IoMmuDxe.c [new file with mode: 0644]
OvmfPkg/IoMmuDxe/IoMmuDxe.inf [new file with mode: 0644]
OvmfPkg/OvmfPkgIa32.dsc
OvmfPkg/OvmfPkgIa32.fdf
OvmfPkg/OvmfPkgIa32X64.dsc
OvmfPkg/OvmfPkgIa32X64.fdf
OvmfPkg/OvmfPkgX64.dsc
OvmfPkg/OvmfPkgX64.fdf

diff --git a/OvmfPkg/IoMmuDxe/AmdSevIoMmu.c b/OvmfPkg/IoMmuDxe/AmdSevIoMmu.c
new file mode 100644 (file)
index 0000000..9e78058
--- /dev/null
@@ -0,0 +1,459 @@
+/** @file\r
+\r
+  The protocol provides support to allocate, free, map and umap a DMA buffer for\r
+  bus master (e.g PciHostBridge). When SEV is enabled, the DMA operations must\r
+  be performed on unencrypted buffer hence we use a bounce buffer to map the guest\r
+  buffer into an unencrypted DMA buffer.\r
+\r
+  Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\r
+  Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
+\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  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 "AmdSevIoMmu.h"\r
+\r
+typedef struct {\r
+  EDKII_IOMMU_OPERATION                     Operation;\r
+  UINTN                                     NumberOfBytes;\r
+  UINTN                                     NumberOfPages;\r
+  EFI_PHYSICAL_ADDRESS                      HostAddress;\r
+  EFI_PHYSICAL_ADDRESS                      DeviceAddress;\r
+} MAP_INFO;\r
+\r
+#define NO_MAPPING             (VOID *) (UINTN) -1\r
+\r
+/**\r
+  Provides the controller-specific addresses required to access system memory from a\r
+  DMA bus master. On SEV guest, the DMA operations must be performed on shared\r
+  buffer hence we allocate a bounce buffer to map the HostAddress to a DeviceAddress.\r
+  The Encryption attribute is removed from the DeviceAddress buffer.\r
+\r
+  @param  This                  The protocol instance pointer.\r
+  @param  Operation             Indicates if the bus master is going to read or\r
+                                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\r
+                                the number of bytes\r
+                                that were mapped.\r
+  @param  DeviceAddress         The resulting map address for the bus master PCI\r
+                                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\r
+                                of resources.\r
+  @retval EFI_DEVICE_ERROR      The system hardware could not map the requested address.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+IoMmuMap (\r
+  IN     EDKII_IOMMU_PROTOCOL                       *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
+  EFI_STATUS                                        Status;\r
+  EFI_PHYSICAL_ADDRESS                              PhysicalAddress;\r
+  MAP_INFO                                          *MapInfo;\r
+  EFI_PHYSICAL_ADDRESS                              DmaMemoryTop;\r
+  EFI_ALLOCATE_TYPE                                 AllocateType;\r
+\r
+  if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL ||\r
+      Mapping == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Make sure that Operation is valid\r
+  //\r
+  if ((UINT32) Operation >= EdkiiIoMmuOperationMaximum) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress;\r
+\r
+  DmaMemoryTop = (UINTN)-1;\r
+  AllocateType = AllocateAnyPages;\r
+\r
+  if (((Operation != EdkiiIoMmuOperationBusMasterRead64 &&\r
+        Operation != EdkiiIoMmuOperationBusMasterWrite64 &&\r
+        Operation != EdkiiIoMmuOperationBusMasterCommonBuffer64)) &&\r
+      ((PhysicalAddress + *NumberOfBytes) > SIZE_4GB)) {\r
+    //\r
+    // If the root bridge or the device cannot handle performing DMA above\r
+    // 4GB but any part of the DMA transfer being mapped is above 4GB, then\r
+    // map the DMA transfer to a buffer below 4GB.\r
+    //\r
+    DmaMemoryTop = SIZE_4GB - 1;\r
+    AllocateType = AllocateMaxAddress;\r
+\r
+    if (Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||\r
+        Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {\r
+        //\r
+        // Common Buffer operations can not be remapped.  If the common buffer\r
+        // if above 4GB, then it is not possible to generate a mapping, so return\r
+        // an error.\r
+        //\r
+        return EFI_UNSUPPORTED;\r
+    }\r
+  }\r
+\r
+  //\r
+  // CommandBuffer was allocated by us (AllocateBuffer) and is already in\r
+  // unencryted buffer so no need to create bounce buffer\r
+  //\r
+  if (Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||\r
+      Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {\r
+    *Mapping = NO_MAPPING;\r
+    *DeviceAddress = PhysicalAddress;\r
+\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Allocate a MAP_INFO structure to remember the mapping when Unmap() is\r
+  // called later.\r
+  //\r
+  MapInfo = AllocatePool (sizeof (MAP_INFO));\r
+  if (MapInfo == NULL) {\r
+    *NumberOfBytes = 0;\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Initialize the MAP_INFO structure\r
+  //\r
+  MapInfo->Operation         = Operation;\r
+  MapInfo->NumberOfBytes     = *NumberOfBytes;\r
+  MapInfo->NumberOfPages     = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes);\r
+  MapInfo->HostAddress       = PhysicalAddress;\r
+  MapInfo->DeviceAddress     = DmaMemoryTop;\r
+\r
+  //\r
+  // Allocate a buffer to map the transfer to.\r
+  //\r
+  Status = gBS->AllocatePages (\r
+                  AllocateType,\r
+                  EfiBootServicesData,\r
+                  MapInfo->NumberOfPages,\r
+                  &MapInfo->DeviceAddress\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (MapInfo);\r
+    *NumberOfBytes = 0;\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Clear the memory encryption mask from the device buffer\r
+  //\r
+  Status = MemEncryptSevClearPageEncMask (0, MapInfo->DeviceAddress, MapInfo->NumberOfPages, TRUE);\r
+  ASSERT_EFI_ERROR(Status);\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
+  //\r
+  // The DeviceAddress is the address of the maped buffer below 4GB\r
+  //\r
+  *DeviceAddress = MapInfo->DeviceAddress;\r
+\r
+  //\r
+  // Return a pointer to the MAP_INFO structure in Mapping\r
+  //\r
+  *Mapping       = MapInfo;\r
+\r
+  DEBUG ((DEBUG_VERBOSE, "%a Device 0x%Lx Host 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",\r
+        __FUNCTION__, MapInfo->DeviceAddress, MapInfo->HostAddress,\r
+        MapInfo->NumberOfPages, MapInfo->NumberOfBytes));\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Completes the Map() operation and releases any corresponding resources.\r
+\r
+  @param  This                  The protocol 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
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+IoMmuUnmap (\r
+  IN  EDKII_IOMMU_PROTOCOL                     *This,\r
+  IN  VOID                                     *Mapping\r
+  )\r
+{\r
+  MAP_INFO                 *MapInfo;\r
+  EFI_STATUS               Status;\r
+\r
+  if (Mapping == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // See if the Map() operation associated with this Unmap() required a mapping\r
+  // buffer. If a mapping buffer was not required, then this function simply\r
+  // buffer. If a mapping buffer was not required, then this function simply\r
+  //\r
+  if (Mapping == NO_MAPPING) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  MapInfo = (MAP_INFO *)Mapping;\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
+  DEBUG ((DEBUG_VERBOSE, "%a Device 0x%Lx Host 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",\r
+        __FUNCTION__, MapInfo->DeviceAddress, MapInfo->HostAddress,\r
+        MapInfo->NumberOfPages, MapInfo->NumberOfBytes));\r
+  //\r
+  // Restore the memory encryption mask\r
+  //\r
+  Status = MemEncryptSevSetPageEncMask (0, MapInfo->DeviceAddress, MapInfo->NumberOfPages, TRUE);\r
+  ASSERT_EFI_ERROR(Status);\r
+\r
+  //\r
+  // Free the mapped buffer and the MAP_INFO structure.\r
+  //\r
+  gBS->FreePages (MapInfo->DeviceAddress, MapInfo->NumberOfPages);\r
+  FreePool (Mapping);\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 protocol instance pointer.\r
+  @param  Type                  This parameter is not used and must be ignored.\r
+  @param  MemoryType            The type of memory to allocate, EfiBootServicesData\r
+                                or EfiRuntimeServicesData.\r
+  @param  Pages                 The number of pages to allocate.\r
+  @param  HostAddress           A pointer to store the base system memory address\r
+                                of the 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\r
+                                bits are MEMORY_WRITE_COMBINE and MEMORY_CACHED.\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
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+IoMmuAllocateBuffer (\r
+  IN     EDKII_IOMMU_PROTOCOL                     *This,\r
+  IN     EFI_ALLOCATE_TYPE                        Type,\r
+  IN     EFI_MEMORY_TYPE                          MemoryType,\r
+  IN     UINTN                                    Pages,\r
+  IN OUT VOID                                     **HostAddress,\r
+  IN     UINT64                                   Attributes\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  EFI_PHYSICAL_ADDRESS      PhysicalAddress;\r
+\r
+  //\r
+  // Validate Attributes\r
+  //\r
+  if ((Attributes & EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Check for invalid inputs\r
+  //\r
+  if (HostAddress == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // The only valid memory types are EfiBootServicesData and\r
+  // EfiRuntimeServicesData\r
+  //\r
+  if (MemoryType != EfiBootServicesData &&\r
+      MemoryType != EfiRuntimeServicesData) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  PhysicalAddress = (UINTN)-1;\r
+  if ((Attributes & EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE) == 0) {\r
+    //\r
+    // Limit allocations to memory below 4GB\r
+    //\r
+    PhysicalAddress = SIZE_4GB - 1;\r
+  }\r
+  Status = gBS->AllocatePages (\r
+                  AllocateMaxAddress,\r
+                  MemoryType,\r
+                  Pages,\r
+                  &PhysicalAddress\r
+                  );\r
+  if (!EFI_ERROR (Status)) {\r
+    *HostAddress = (VOID *) (UINTN) PhysicalAddress;\r
+\r
+    //\r
+    // Clear memory encryption mask\r
+    //\r
+    Status = MemEncryptSevClearPageEncMask (0, PhysicalAddress, Pages, TRUE);\r
+    ASSERT_EFI_ERROR(Status);\r
+  }\r
+\r
+  DEBUG ((DEBUG_VERBOSE, "%a Address 0x%Lx Pages 0x%Lx\n", __FUNCTION__, PhysicalAddress, Pages));\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Frees memory that was allocated with AllocateBuffer().\r
+\r
+  @param  This                  The protocol 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
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+IoMmuFreeBuffer (\r
+  IN  EDKII_IOMMU_PROTOCOL                     *This,\r
+  IN  UINTN                                    Pages,\r
+  IN  VOID                                     *HostAddress\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Set memory encryption mask\r
+  //\r
+  Status = MemEncryptSevSetPageEncMask (0, (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress, Pages, TRUE);\r
+  ASSERT_EFI_ERROR(Status);\r
+\r
+  DEBUG ((DEBUG_VERBOSE, "%a Address 0x%Lx Pages 0x%Lx\n", __FUNCTION__, (UINTN)HostAddress, Pages));\r
+  return gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress, Pages);\r
+}\r
+\r
+\r
+/**\r
+  Set IOMMU attribute for a system memory.\r
+\r
+  If the IOMMU protocol 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
+  The DeviceHandle is used to identify which device submits the request.\r
+  The IOMMU implementation need translate the device path to an IOMMU device ID,\r
+  and set IOMMU hardware register accordingly.\r
+  1) DeviceHandle can be a standard PCI device.\r
+     The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.\r
+     The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.\r
+     The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.\r
+     After the memory is used, the memory need set 0 to keep it being protected.\r
+  2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).\r
+     The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.\r
+\r
+  @param[in]  This              The protocol instance pointer.\r
+  @param[in]  DeviceHandle      The device who initiates the DMA access request.\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  DeviceHandle is an invalid handle.\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        DeviceHandle is unknown by the IOMMU.\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
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+IoMmuSetAttribute (\r
+  IN EDKII_IOMMU_PROTOCOL  *This,\r
+  IN EFI_HANDLE            DeviceHandle,\r
+  IN VOID                  *Mapping,\r
+  IN UINT64                IoMmuAccess\r
+  )\r
+{\r
+  return EFI_UNSUPPORTED;\r
+}\r
+\r
+EDKII_IOMMU_PROTOCOL  mAmdSev = {\r
+  EDKII_IOMMU_PROTOCOL_REVISION,\r
+  IoMmuSetAttribute,\r
+  IoMmuMap,\r
+  IoMmuUnmap,\r
+  IoMmuAllocateBuffer,\r
+  IoMmuFreeBuffer,\r
+};\r
+\r
+/**\r
+  Initialize Iommu Protocol.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+AmdSevInstallIoMmuProtocol (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  EFI_HANDLE  Handle;\r
+\r
+  Handle = NULL;\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &Handle,\r
+                  &gEdkiiIoMmuProtocolGuid, &mAmdSev,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+}\r
diff --git a/OvmfPkg/IoMmuDxe/AmdSevIoMmu.h b/OvmfPkg/IoMmuDxe/AmdSevIoMmu.h
new file mode 100644 (file)
index 0000000..8b3962a
--- /dev/null
@@ -0,0 +1,43 @@
+/** @file\r
+\r
+  The protocol provides support to allocate, free, map and umap a DMA buffer for\r
+  bus master (e.g PciHostBridge). When SEV is enabled, the DMA operations must\r
+  be performed on unencrypted buffer hence protocol clear the encryption bit\r
+  from the DMA buffer.\r
+\r
+  Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  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
+#ifndef __AMD_SEV_IOMMU_H_\r
+#define __AMD_SEV_IOMMU_H\r
+\r
+#include <Protocol/IoMmu.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemEncryptSevLib.h>\r
+\r
+/**\r
+  Install IOMMU protocol to provide the DMA support for PciHostBridge and\r
+  MemEncryptSevLib.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+AmdSevInstallIoMmuProtocol (\r
+  VOID\r
+  );\r
+\r
+#endif\r
diff --git a/OvmfPkg/IoMmuDxe/IoMmuDxe.c b/OvmfPkg/IoMmuDxe/IoMmuDxe.c
new file mode 100644 (file)
index 0000000..101157e
--- /dev/null
@@ -0,0 +1,53 @@
+/** @file\r
+\r
+  IoMmuDxe driver installs EDKII_IOMMU_PROTOCOL to provide the support for DMA\r
+  operations when SEV is enabled.\r
+\r
+  Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\r
+\r
+  This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD\r
+  License which accompanies this distribution.  The full text of the license may\r
+  be found at 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 <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemEncryptSevLib.h>\r
+\r
+#include "AmdSevIoMmu.h"\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+IoMmuDxeEntryPoint (\r
+  IN EFI_HANDLE         ImageHandle,\r
+  IN EFI_SYSTEM_TABLE   *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS    Status = EFI_SUCCESS;\r
+  EFI_HANDLE    Handle = NULL;\r
+\r
+  //\r
+  // When SEV is enabled, install IoMmu protocol otherwise install the\r
+  // placeholder protocol so that other dependent module can run.\r
+  //\r
+  if (MemEncryptSevIsEnabled ()) {\r
+    AmdSevInstallIoMmuProtocol ();\r
+  } else {\r
+    Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &Handle,\r
+                  &gIoMmuAbsentProtocolGuid,\r
+                  NULL, NULL);\r
+  }\r
+\r
+  return Status;\r
+}\r
diff --git a/OvmfPkg/IoMmuDxe/IoMmuDxe.inf b/OvmfPkg/IoMmuDxe/IoMmuDxe.inf
new file mode 100644 (file)
index 0000000..b90dc80
--- /dev/null
@@ -0,0 +1,49 @@
+#/** @file\r
+#\r
+#  Driver provides the IOMMU protcol support for PciHostBridgeIo and others\r
+#  drivers.\r
+#\r
+#  Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\r
+#\r
+#  This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD\r
+#  License which accompanies this distribution.  The full text of the license may\r
+#  be found at 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
+[Defines]\r
+  INF_VERSION                    = 1.25\r
+  BASE_NAME                      = IoMmuDxe\r
+  FILE_GUID                      = 8657015b-ea43-440d-949a-af3be365c0fc\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  ENTRY_POINT                    = IoMmuDxeEntryPoint\r
+\r
+[Sources]\r
+  AmdSevIoMmu.c\r
+  IoMmuDxe.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  OvmfPkg/OvmfPkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  UefiLib\r
+  UefiDriverEntryPoint\r
+  UefiBootServicesTableLib\r
+  DxeServicesTableLib\r
+  DebugLib\r
+  MemEncryptSevLib\r
+\r
+[Protocols]\r
+  gEdkiiIoMmuProtocolGuid                     ## SOMETIME_PRODUCES\r
+  gIoMmuAbsentProtocolGuid                    ## SOMETIME_PRODUCES\r
+\r
+[Depex]\r
+  TRUE\r
index 9355a37ae95aad23f30be2df4992369e8ba2c965..6a3e400cab6a9ac78a45d63c1e4dc03c493027da 100644 (file)
 !endif\r
 \r
   OvmfPkg/PlatformDxe/Platform.inf\r
+  OvmfPkg/IoMmuDxe/IoMmuDxe.inf\r
 \r
 !if $(SMM_REQUIRE) == TRUE\r
   OvmfPkg/SmmAccess/SmmAccess2Dxe.inf\r
index cd91bc03a6834e850d844a570fbe7cbfb1c0f899..5e5ade2a1fcbdbae7f0ea6ae3f189d2409163d42 100644 (file)
@@ -347,6 +347,7 @@ INF  RuleOverride=CSM OvmfPkg/Csm/Csm16/Csm16.inf
 INF  OvmfPkg/QemuVideoDxe/QemuVideoDxe.inf\r
 INF  OvmfPkg/VirtioGpuDxe/VirtioGpu.inf\r
 INF  OvmfPkg/PlatformDxe/Platform.inf\r
+INF  OvmfPkg/IoMmuDxe/IoMmuDxe.inf\r
 \r
 !if $(SMM_REQUIRE) == TRUE\r
 INF  OvmfPkg/SmmAccess/SmmAccess2Dxe.inf\r
index b3a22c726b9294a426cca73f6fac87321547e07e..d6d5d7db522e128e726477d73511dc061bc349ef 100644 (file)
 \r
   OvmfPkg/PlatformDxe/Platform.inf\r
   OvmfPkg/AmdSevDxe/AmdSevDxe.inf\r
+  OvmfPkg/IoMmuDxe/IoMmuDxe.inf\r
 \r
 !if $(SMM_REQUIRE) == TRUE\r
   OvmfPkg/SmmAccess/SmmAccess2Dxe.inf\r
index 5cec9982dbe18ec1a58898add4c892488c6b054b..aa0d8c69f3f844cac23600546f9abb805f63e8a5 100644 (file)
@@ -354,6 +354,7 @@ INF  OvmfPkg/QemuVideoDxe/QemuVideoDxe.inf
 INF  OvmfPkg/VirtioGpuDxe/VirtioGpu.inf\r
 INF  OvmfPkg/PlatformDxe/Platform.inf\r
 INF  OvmfPkg/AmdSevDxe/AmdSevDxe.inf\r
+INF  OvmfPkg/IoMmuDxe/IoMmuDxe.inf\r
 \r
 !if $(SMM_REQUIRE) == TRUE\r
 INF  OvmfPkg/SmmAccess/SmmAccess2Dxe.inf\r
index aba4d907b5677bdcb74d270c4d8e8fbba33b78c3..2acf8f43622268b2087a8e8d9e98529fbecfb317 100644 (file)
 \r
   OvmfPkg/PlatformDxe/Platform.inf\r
   OvmfPkg/AmdSevDxe/AmdSevDxe.inf\r
+  OvmfPkg/IoMmuDxe/IoMmuDxe.inf\r
 \r
 !if $(SMM_REQUIRE) == TRUE\r
   OvmfPkg/SmmAccess/SmmAccess2Dxe.inf\r
index 213db6bedbe271a5a256884c553014492a80ada6..98a0cf17da90aa73cb88c9269e2f437f81de0744 100644 (file)
@@ -354,6 +354,7 @@ INF  OvmfPkg/QemuVideoDxe/QemuVideoDxe.inf
 INF  OvmfPkg/VirtioGpuDxe/VirtioGpu.inf\r
 INF  OvmfPkg/PlatformDxe/Platform.inf\r
 INF  OvmfPkg/AmdSevDxe/AmdSevDxe.inf\r
+INF  OvmfPkg/IoMmuDxe/IoMmuDxe.inf\r
 \r
 !if $(SMM_REQUIRE) == TRUE\r
 INF  OvmfPkg/SmmAccess/SmmAccess2Dxe.inf\r