]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.c
SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugCommunicationLibUsb3 / DebugCommunicationLibUsb3Pei.c
index 902a3b626accf8a740b89b917fd0afb6168678ec..dfdbfb0b7c2e37b00588353d54ca454e7dee4084 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Debug Port Library implementation based on usb3 debug port.\r
 \r
-  Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2014 - 2018, 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
 \r
 #include <PiPei.h>\r
 #include <Library/PeiServicesLib.h>\r
+#include <Library/HobLib.h>\r
 #include <Ppi/MemoryDiscovered.h>\r
+#include <Ppi/IoMmu.h>\r
 #include "DebugCommunicationLibUsb3Internal.h"\r
 \r
+GUID                    gUsb3DbgGuid = USB3_DBG_GUID;\r
+\r
+/**\r
+  USB3 IOMMU PPI notify.\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
+Usb3IoMmuPpiNotify (\r
+  IN EFI_PEI_SERVICES           **PeiServices,\r
+  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDesc,\r
+  IN VOID                       *Ppi\r
+  )\r
+{\r
+  USB3_DEBUG_PORT_HANDLE        *Instance;\r
+\r
+  DEBUG ((DEBUG_INFO, "%a()\n", __FUNCTION__));\r
+\r
+  Instance = GetUsb3DebugPortInstance ();\r
+  ASSERT (Instance != NULL);\r
+  if (!Instance->Ready) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  Instance->InNotify = TRUE;\r
+\r
+  //\r
+  // Reinitialize USB3 debug port with granted DMA buffer from IOMMU PPI.\r
+  //\r
+  InitializeUsbDebugHardware (Instance);\r
+\r
+  //\r
+  // Wait some time for host to be ready after re-initialization.\r
+  //\r
+  MicroSecondDelay (1000000);\r
+\r
+  Instance->InNotify = FALSE;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_PEI_NOTIFY_DESCRIPTOR mUsb3IoMmuPpiNotifyDesc = {\r
+  (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+  &gEdkiiIoMmuPpiGuid,\r
+  Usb3IoMmuPpiNotify\r
+};\r
+\r
+/**\r
+  Allocates pages that are suitable for an OperationBusMasterCommonBuffer or\r
+  OperationBusMasterCommonBuffer64 mapping.\r
+\r
+  @param IoMmu                  Pointer to IOMMU PPI.\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 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 requested memory pages were allocated.\r
+  @retval EFI_UNSUPPORTED       Attributes is unsupported. The only legal attribute bits are\r
+                                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
+IoMmuAllocateBuffer (\r
+  IN EDKII_IOMMU_PPI        *IoMmu,\r
+  IN UINTN                  Pages,\r
+  OUT VOID                  **HostAddress,\r
+  OUT EFI_PHYSICAL_ADDRESS  *DeviceAddress,\r
+  OUT VOID                  **Mapping\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  UINTN                 NumberOfBytes;\r
+\r
+  *HostAddress = NULL;\r
+  *DeviceAddress = 0;\r
+  *Mapping = NULL;\r
+\r
+  Status = IoMmu->AllocateBuffer (\r
+                    IoMmu,\r
+                    EfiRuntimeServicesData,\r
+                    Pages,\r
+                    HostAddress,\r
+                    0\r
+                    );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  NumberOfBytes = EFI_PAGES_TO_SIZE (Pages);\r
+  Status = IoMmu->Map (\r
+                    IoMmu,\r
+                    EdkiiIoMmuOperationBusMasterCommonBuffer,\r
+                    *HostAddress,\r
+                    &NumberOfBytes,\r
+                    DeviceAddress,\r
+                    Mapping\r
+                    );\r
+  if (EFI_ERROR (Status)) {\r
+    IoMmu->FreeBuffer (IoMmu, Pages, *HostAddress);\r
+    *HostAddress = NULL;\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  Status = IoMmu->SetAttribute (\r
+                    IoMmu,\r
+                    *Mapping,\r
+                    EDKII_IOMMU_ACCESS_READ | EDKII_IOMMU_ACCESS_WRITE\r
+                    );\r
+  if (EFI_ERROR (Status)) {\r
+    IoMmu->Unmap (IoMmu, *Mapping);\r
+    IoMmu->FreeBuffer (IoMmu, Pages, *HostAddress);\r
+    *Mapping = NULL;\r
+    *HostAddress = NULL;\r
+    return Status;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  USB3 get IOMMU PPI.\r
+\r
+  @return Pointer to IOMMU PPI.\r
+\r
+**/\r
+EDKII_IOMMU_PPI *\r
+Usb3GetIoMmu (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  EDKII_IOMMU_PPI           *IoMmu;\r
+\r
+  IoMmu = NULL;\r
+  Status = PeiServicesLocatePpi (\r
+             &gEdkiiIoMmuPpiGuid,\r
+             0,\r
+             NULL,\r
+             (VOID **) &IoMmu\r
+             );\r
+  if (!EFI_ERROR (Status) && (IoMmu != NULL)) {\r
+    return IoMmu;\r
+  }\r
+\r
+  return NULL;\r
+}\r
+\r
+/**\r
+  Return USB3 debug instance address pointer.\r
+\r
+**/  \r
+EFI_PHYSICAL_ADDRESS *\r
+GetUsb3DebugPortInstanceAddrPtr (\r
+  VOID\r
+  )\r
+{\r
+  USB3_DEBUG_PORT_HANDLE        *Instance;\r
+  EFI_PHYSICAL_ADDRESS          *AddrPtr;\r
+  EFI_PEI_HOB_POINTERS          Hob;\r
+  EFI_STATUS                    Status;\r
+\r
+  Hob.Raw = GetFirstGuidHob (&gUsb3DbgGuid);\r
+  if (Hob.Raw == NULL) {\r
+    //\r
+    // Build HOB for the local instance and the buffer to save instance address pointer.\r
+    // Use the local instance in HOB temporarily.\r
+    //\r
+    AddrPtr = BuildGuidHob (\r
+                &gUsb3DbgGuid,\r
+                sizeof (EFI_PHYSICAL_ADDRESS) + sizeof (USB3_DEBUG_PORT_HANDLE)\r
+                );\r
+    ASSERT (AddrPtr != NULL);\r
+    ZeroMem (AddrPtr, sizeof (EFI_PHYSICAL_ADDRESS) + sizeof (USB3_DEBUG_PORT_HANDLE));\r
+    Instance = (USB3_DEBUG_PORT_HANDLE *) (AddrPtr + 1);\r
+    *AddrPtr = (EFI_PHYSICAL_ADDRESS) (UINTN) Instance;\r
+    Instance->FromHob = TRUE;\r
+    Instance->Initialized = USB3DBG_UNINITIALIZED;\r
+    if (Usb3GetIoMmu () == NULL) {\r
+      Status = PeiServicesNotifyPpi (&mUsb3IoMmuPpiNotifyDesc);\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+  } else {\r
+    AddrPtr = GET_GUID_HOB_DATA (Hob.Guid);\r
+  }\r
+\r
+  return AddrPtr;\r
+}\r
+\r
 /**\r
   Allocate aligned memory for XHC's usage.\r
 \r
-  @param  BufferSize      The size, in bytes, of the Buffer.\r
+  @param BufferSize     The size, in bytes, of the Buffer.\r
   \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
 \r
@@ -34,6 +234,9 @@ AllocateAlignBuffer (
   EFI_PHYSICAL_ADDRESS     Address;\r
   EFI_STATUS               Status;\r
   VOID                     *MemoryDiscoveredPpi;\r
+  EDKII_IOMMU_PPI          *IoMmu;\r
+  VOID                     *HostAddress;\r
+  VOID                     *Mapping;\r
 \r
   Buf = NULL;\r
 \r
@@ -47,9 +250,28 @@ AllocateAlignBuffer (
              (VOID **) &MemoryDiscoveredPpi\r
              );\r
   if (!EFI_ERROR (Status)) {\r
-    Status = PeiServicesAllocatePages (EfiACPIMemoryNVS, EFI_SIZE_TO_PAGES (BufferSize), &Address);\r
-    if (!EFI_ERROR (Status)) {\r
-      Buf = (VOID *)(UINTN) Address;\r
+    IoMmu = Usb3GetIoMmu ();\r
+    if (IoMmu != NULL) {\r
+      Status = IoMmuAllocateBuffer (\r
+                 IoMmu,\r
+                 EFI_SIZE_TO_PAGES (BufferSize),\r
+                 &HostAddress,\r
+                 &Address,\r
+                 &Mapping\r
+                 );\r
+      if (!EFI_ERROR (Status)) {\r
+        ASSERT (Address == ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress));\r
+        Buf = (VOID *)(UINTN) Address;\r
+      }\r
+    } else {\r
+      Status = PeiServicesAllocatePages (\r
+                 EfiACPIMemoryNVS,\r
+                 EFI_SIZE_TO_PAGES (BufferSize),\r
+                 &Address\r
+                 );\r
+      if (!EFI_ERROR (Status)) {\r
+        Buf = (VOID *)(UINTN) Address;\r
+      }\r
     }\r
   }\r
   return Buf;\r