]> git.proxmox.com Git - mirror_edk2.git/commitdiff
SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU
authorStar Zeng <star.zeng@intel.com>
Wed, 14 Mar 2018 09:12:22 +0000 (17:12 +0800)
committerStar Zeng <star.zeng@intel.com>
Thu, 15 Mar 2018 06:14:26 +0000 (14:14 +0800)
de8373fa07f87ca735139bb86c51e2c29fb1d956 could not handle two cases.
1. For the case that the USB3 debug port instance and DMA buffers are
from PEI HOB with IOMMU enabled, it was to reallocate the DMA buffers
by AllocateAddress with the memory type accessible by SMM environment.
But reallocating the DMA buffers by AllocateAddress may fail.

2. At S3 resume, after the code is transferred to PiSmmCpuDxeSmm from
S3Resume2Pei, HOB is still needed to be used for DMA operation, but
PiSmmCpuDxeSmm has no way to get the HOB at S3 resume.

The patch is to re-support IOMMU.
For PEI, allocate granted DMA buffer from IOMMU PPI, register IOMMU PPI
notification to reinitialize hardware with granted DMA buffer if IOMMU
PPI is not present yet.
For DXE, map DMA buffer by PciIo in PciIo notification for early DXE,
and register DxeSmmReadyToLock notification to reinitialize hardware
with granted DXE DMA buffer accessible by SMM environment for late DXE.

DebugAgentLib has been managing the instance as Handle in
HOB/SystemTable. The Handle(instance) from DebugAgentLib can be used
directly in DebugCommunicationLibUsb3. Then DebugCommunicationLibUsb3
could get consistent Handle(instance) from DebugAgentLib.

Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Hao Wu <hao.a.wu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Hao Wu <hao.a.wu@intel.com>
SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c
SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.inf
SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Internal.h
SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.c
SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.inf

index 37f49ad822f56fb6668bb17d4794db0481e9568d..fb9ca84fc7bc09d649b0ebf896521e920df49ac2 100644 (file)
 \r
 #include "DebugCommunicationLibUsb3Internal.h"\r
 \r
-//\r
-// The global variable which can be used after memory is ready.\r
-//\r
-USB3_DEBUG_PORT_HANDLE     mDebugCommunicationLibUsb3DebugPortHandle;\r
-\r
 UINT16   mString0Desc[] = {\r
   //  String Descriptor Type + Length\r
   ( USB_DESC_TYPE_STRING << 8 ) + STRING0_DESC_LEN,\r
@@ -150,6 +145,28 @@ XhcSetDebugRegBit (
   XhcWriteDebugReg (Handle, Offset, Data);\r
 }\r
 \r
+/**\r
+  Clear one bit of the debug register while keeping other bits.\r
+\r
+  @param  Handle       Debug port handle.\r
+  @param  Offset       The offset of the debug register.\r
+  @param  Bit          The bit mask of the register to clear.\r
+\r
+**/\r
+VOID\r
+XhcClearDebugRegBit (\r
+  IN USB3_DEBUG_PORT_HANDLE   *Handle,\r
+  IN UINT32                   Offset,\r
+  IN UINT32                   Bit\r
+  )\r
+{\r
+  UINT32                  Data;\r
+\r
+  Data  = XhcReadDebugReg (Handle, Offset);\r
+  Data  &= ~Bit;\r
+  XhcWriteDebugReg (Handle, Offset, Data);\r
+}\r
+\r
 /**\r
   Program and eanble XHCI MMIO base address.\r
 \r
@@ -199,7 +216,7 @@ UpdateXhcResource (
   IN EFI_PHYSICAL_ADDRESS                   XhciMmioBase\r
   )\r
 {\r
-  if ((Handle == NULL) || (Handle->XhciMmioBase == XhciMmioBase)) {\r
+  if (Handle == NULL) {\r
     return;\r
   }\r
 \r
@@ -236,6 +253,14 @@ CalculateUsbDebugPortMmioBase (
   EFI_PHYSICAL_ADDRESS            CapabilityPointer;\r
   UINT8                           CapLength;\r
 \r
+  if (Handle->Initialized != USB3DBG_UNINITIALIZED) {\r
+    if (Handle->Initialized == USB3DBG_NO_DBG_CAB) {\r
+      return RETURN_UNSUPPORTED;\r
+    } else {\r
+      return RETURN_SUCCESS;\r
+    }\r
+  }\r
+\r
   VendorId = PciRead16 (PcdGet32(PcdUsbXhciPciAddress) + PCI_VENDOR_ID_OFFSET);\r
   DeviceId = PciRead16 (PcdGet32(PcdUsbXhciPciAddress) + PCI_DEVICE_ID_OFFSET);\r
   \r
@@ -288,6 +313,7 @@ CalculateUsbDebugPortMmioBase (
   Handle->DebugCapabilityBase   = CapabilityPointer;\r
   Handle->DebugCapabilityOffset = CapabilityPointer - Handle->XhciMmioBase;\r
   Handle->XhciOpRegister        = Handle->XhciMmioBase + CapLength;\r
+  Handle->DebugSupport = TRUE;\r
   Handle->Initialized = USB3DBG_DBG_CAB;\r
   return RETURN_SUCCESS;\r
 \r
@@ -326,6 +352,9 @@ NeedReinitializeHardware(
   Dcctrl = XhcReadDebugReg (Handle, XHC_DC_DCCTRL);\r
   if ((Dcctrl & BIT0) == 0) {\r
     Result = TRUE;\r
+  } else if (!Handle->Ready) {\r
+    Handle->Ready = TRUE;\r
+    Handle->Initialized = USB3DBG_ENABLED;\r
   }\r
 \r
   return Result;\r
@@ -677,6 +706,13 @@ InitializeUsbDebugHardware (
     MicroSecondDelay (10 * 1000);\r
   }\r
 \r
+  //\r
+  // Clear DCE bit and LSE bit in DCCTRL\r
+  //\r
+  if ((XhcReadDebugReg (Handle, XHC_DC_DCCTRL) & (BIT1|BIT31)) == (BIT1|BIT31)) {\r
+    XhcClearDebugRegBit (Handle, XHC_DC_DCCTRL, BIT1|BIT31);\r
+  }\r
+\r
   //\r
   // Construct the buffer for read, poll and write.\r
   //\r
@@ -745,6 +781,73 @@ Enable:
   return Status;\r
 }\r
 \r
+/**\r
+  Discover and initialize usb debug port.\r
+\r
+  @param Handle                 Debug port handle.\r
+\r
+**/\r
+VOID\r
+DiscoverInitializeUsbDebugPort (\r
+  IN USB3_DEBUG_PORT_HANDLE     *Handle\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  EFI_PHYSICAL_ADDRESS          XhciMmioBase;\r
+\r
+  //\r
+  // Read 64-bit MMIO base address\r
+  //\r
+  XhciMmioBase = ProgramXhciBaseAddress ();\r
+  Handle->XhciMmioBase = XhciMmioBase;\r
+\r
+  Status = CalculateUsbDebugPortMmioBase (Handle);\r
+  if (!RETURN_ERROR (Status)) {\r
+    UpdateXhcResource (Handle, XhciMmioBase);\r
+    if (NeedReinitializeHardware (Handle)) {\r
+      InitializeUsbDebugHardware (Handle);\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Set USB3 debug instance address.\r
+\r
+  @param[in] Instance           Debug port instance.\r
+\r
+**/  \r
+VOID\r
+SetUsb3DebugPortInstance (\r
+  IN USB3_DEBUG_PORT_HANDLE     *Instance\r
+  )\r
+{\r
+  EFI_PHYSICAL_ADDRESS          *AddrPtr;\r
+\r
+  AddrPtr = GetUsb3DebugPortInstanceAddrPtr ();\r
+  ASSERT (AddrPtr != NULL);\r
+  *AddrPtr = (EFI_PHYSICAL_ADDRESS) (UINTN) Instance;\r
+}\r
+\r
+/**\r
+  Return USB3 debug instance address.\r
+\r
+**/  \r
+USB3_DEBUG_PORT_HANDLE *\r
+GetUsb3DebugPortInstance (\r
+  VOID\r
+  )\r
+{\r
+  EFI_PHYSICAL_ADDRESS          *AddrPtr;\r
+  USB3_DEBUG_PORT_HANDLE        *Instance;\r
+\r
+  AddrPtr = GetUsb3DebugPortInstanceAddrPtr ();\r
+  ASSERT (AddrPtr != NULL);\r
+\r
+  Instance = (USB3_DEBUG_PORT_HANDLE *) (UINTN) *AddrPtr;\r
+\r
+  return Instance;\r
+}\r
+\r
 /**\r
   Read data from debug device and save the data in buffer.\r
 \r
@@ -772,7 +875,6 @@ DebugPortReadBuffer (
   )\r
 {\r
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;\r
-  RETURN_STATUS             Status;\r
   UINT8                     Index;\r
   UINT8                     *Data;\r
 \r
@@ -781,24 +883,27 @@ DebugPortReadBuffer (
   }\r
 \r
   //\r
-  // If Handle is NULL, it means memory is ready for use.\r
-  // Use global variable to store handle value.\r
+  // If Handle is NULL, get own instance.\r
+  // If Handle is not NULL, use it and set the instance.\r
   //\r
-  if (Handle == NULL) {\r
-    UsbDebugPortHandle = &mDebugCommunicationLibUsb3DebugPortHandle;\r
+  if (Handle != NULL) {\r
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *) Handle;\r
+    SetUsb3DebugPortInstance (UsbDebugPortHandle);\r
   } else {\r
-    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Handle;\r
+    UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
   }\r
-  \r
-  if (UsbDebugPortHandle->Initialized == USB3DBG_NO_DBG_CAB) {\r
+  if (UsbDebugPortHandle == NULL) {\r
     return 0;\r
   }\r
-  \r
-  if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
-    Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
-    if (RETURN_ERROR(Status)) {\r
-      return 0;\r
-    }\r
+\r
+  if (UsbDebugPortHandle->InNotify) {\r
+    return 0;\r
+  }\r
+\r
+  DiscoverInitializeUsbDebugPort (UsbDebugPortHandle);\r
+\r
+  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
+    return 0;\r
   }\r
 \r
   Data = (UINT8 *)(UINTN)UsbDebugPortHandle->Data;\r
@@ -848,10 +953,8 @@ DebugPortWriteBuffer (
   )\r
 {\r
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;\r
-  RETURN_STATUS             Status;\r
   UINTN                     Sent;\r
   UINTN                     Total;\r
-  EFI_PHYSICAL_ADDRESS      XhciMmioBase;\r
 \r
   if (NumberOfBytes == 0 || Buffer == NULL) {\r
     return 0;\r
@@ -861,38 +964,34 @@ DebugPortWriteBuffer (
   Total = 0;\r
 \r
   //\r
-  // If Handle is NULL, it means memory is ready for use.\r
-  // Use global variable to store handle value.\r
+  // If Handle is NULL, get own instance.\r
+  // If Handle is not NULL, use it and set the instance.\r
   //\r
-  if (Handle == NULL) {\r
-    UsbDebugPortHandle = &mDebugCommunicationLibUsb3DebugPortHandle;\r
+  if (Handle != NULL) {\r
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *) Handle;\r
+    SetUsb3DebugPortInstance (UsbDebugPortHandle);\r
   } else {\r
-    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Handle;\r
+    UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
   }\r
-  \r
-  if (UsbDebugPortHandle->Initialized == USB3DBG_NO_DBG_CAB) {\r
+  if (UsbDebugPortHandle == NULL) {\r
     return 0;\r
   }\r
 \r
-  //\r
-  // MMIO base address is possible to clear, set it if it is cleared. (XhciMemorySpaceClose in PchUsbCommon.c)\r
-  //\r
-  XhciMmioBase = ProgramXhciBaseAddress ();\r
+  if (UsbDebugPortHandle->InNotify) {\r
+    return 0;\r
+  }\r
 \r
-  UpdateXhcResource (UsbDebugPortHandle, XhciMmioBase);\r
+  DiscoverInitializeUsbDebugPort (UsbDebugPortHandle);\r
 \r
-  if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
-    Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
-    if (RETURN_ERROR(Status)) {\r
-      return 0;\r
-    }\r
+  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
+    return 0;\r
   }\r
 \r
   //\r
   // When host is trying to send data, write will be blocked.\r
   // Poll to see if there is any data sent by host at first.\r
   //\r
-  DebugPortPollBuffer (Handle);\r
+  DebugPortPollBuffer (UsbDebugPortHandle);\r
 \r
   while ((Total < NumberOfBytes)) {\r
     if (NumberOfBytes - Total > USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE) {\r
@@ -900,7 +999,7 @@ DebugPortWriteBuffer (
     } else {\r
       Sent = (UINT8)(NumberOfBytes - Total);\r
     }\r
-    Status = XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataOut, Buffer + Total, &Sent, DATA_TRANSFER_WRITE_TIMEOUT);\r
+    XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataOut, Buffer + Total, &Sent, DATA_TRANSFER_WRITE_TIMEOUT);\r
     Total += Sent;\r
   }\r
   \r
@@ -928,33 +1027,31 @@ DebugPortPollBuffer (
 {\r
   USB3_DEBUG_PORT_HANDLE     *UsbDebugPortHandle;\r
   UINTN                     Length;\r
-  RETURN_STATUS             Status;\r
-  EFI_PHYSICAL_ADDRESS      XhciMmioBase;\r
 \r
   //\r
-  // If Handle is NULL, it means memory is ready for use.\r
-  // Use global variable to store handle value.\r
+  // If Handle is NULL, get own instance.\r
+  // If Handle is not NULL, use it and set the instance.\r
   //\r
-  if (Handle == NULL) {\r
-    UsbDebugPortHandle = &mDebugCommunicationLibUsb3DebugPortHandle;\r
+  if (Handle != NULL) {\r
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *) Handle;\r
+    SetUsb3DebugPortInstance (UsbDebugPortHandle);\r
   } else {\r
-    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Handle;\r
+    UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  }\r
+  if (UsbDebugPortHandle == NULL) {\r
+    return FALSE;\r
   }\r
 \r
-  if (UsbDebugPortHandle->Initialized == USB3DBG_NO_DBG_CAB) {\r
-    return 0;\r
+  if (UsbDebugPortHandle->InNotify) {\r
+    return FALSE;\r
   }\r
 \r
-  XhciMmioBase = ProgramXhciBaseAddress ();\r
-  UpdateXhcResource (UsbDebugPortHandle, XhciMmioBase);\r
-  \r
-  if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
-    Status = InitializeUsbDebugHardware(UsbDebugPortHandle);\r
-    if (RETURN_ERROR(Status)) {\r
-      return FALSE;\r
-    }\r
+  DiscoverInitializeUsbDebugPort (UsbDebugPortHandle);\r
+\r
+  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
+    return FALSE;\r
   }\r
-  \r
+\r
   //\r
   // If the data buffer is not empty, then return TRUE directly.\r
   // Otherwise initialize a usb read transaction and read data to internal data buffer.\r
@@ -967,7 +1064,7 @@ DebugPortPollBuffer (
   // Read data as much as we can\r
   //\r
   Length = XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE;\r
-  XhcDataTransfer (Handle, EfiUsbDataIn, (VOID *)(UINTN)UsbDebugPortHandle->Data, &Length, DATA_TRANSFER_POLL_TIMEOUT);\r
+  XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataIn, (VOID *)(UINTN)UsbDebugPortHandle->Data, &Length, DATA_TRANSFER_POLL_TIMEOUT);\r
 \r
   if (Length > XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE) {\r
     return FALSE;\r
@@ -1015,8 +1112,6 @@ DebugPortInitialize (
   IN DEBUG_PORT_CONTINUE  Function\r
   )\r
 {\r
-  RETURN_STATUS             Status;\r
-  USB3_DEBUG_PORT_HANDLE    Handle;\r
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;\r
 \r
   //\r
@@ -1025,40 +1120,19 @@ DebugPortInitialize (
   ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize) == sizeof (USB3_DEBUG_PORT_HANDLE));\r
 \r
   if (Function == NULL && Context != NULL) {\r
-    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Context;\r
-  } else {\r
-    ZeroMem(&Handle, sizeof (USB3_DEBUG_PORT_HANDLE));\r
-    UsbDebugPortHandle = &Handle;\r
+    SetUsb3DebugPortInstance ((USB3_DEBUG_PORT_HANDLE *) Context);\r
+    return (DEBUG_PORT_HANDLE) Context;\r
   }\r
-\r
-  if (Function == NULL && Context != NULL) {\r
-    return (DEBUG_PORT_HANDLE *) Context;\r
+  UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  if (UsbDebugPortHandle == NULL) {\r
+    return NULL;\r
   }\r
-  \r
-  //\r
-  // Read 64-bit MMIO base address\r
-  //\r
-  UsbDebugPortHandle->XhciMmioBase = ProgramXhciBaseAddress ();\r
 \r
-  Status = CalculateUsbDebugPortMmioBase (UsbDebugPortHandle);\r
-  if (RETURN_ERROR (Status)) {\r
-    goto Exit;\r
-  }\r
-\r
-  if (NeedReinitializeHardware(&Handle)) {\r
-    Status = InitializeUsbDebugHardware (&Handle);\r
-    if (RETURN_ERROR(Status)) {\r
-      goto Exit;\r
-    }\r
-  }\r
-\r
-Exit:\r
+  DiscoverInitializeUsbDebugPort (UsbDebugPortHandle);\r
 \r
   if (Function != NULL) {\r
-    Function (Context, &Handle);\r
-  } else {\r
-    CopyMem(&mDebugCommunicationLibUsb3DebugPortHandle, &Handle, sizeof (USB3_DEBUG_PORT_HANDLE));\r
+    Function (Context, (DEBUG_PORT_HANDLE) UsbDebugPortHandle);\r
   }\r
 \r
-  return (DEBUG_PORT_HANDLE)(UINTN)&mDebugCommunicationLibUsb3DebugPortHandle;\r
+  return (DEBUG_PORT_HANDLE) UsbDebugPortHandle;\r
 }\r
index c4a8a476829957b5e16c741f31db96e5ecf5cdd8..0955aa6f0e9f144b77ac9e34158172bdcd29fad0 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Debug Port Library implementation based on usb3 debug port.\r
 \r
-  Copyright (c) 2014 - 2016, 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
 #include <Base.h>\r
 #include <PiDxe.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/HobLib.h>\r
+#include <Protocol/PciIo.h>\r
+#include <Protocol/IoMmu.h>\r
+#include <Protocol/DxeSmmReadyToLock.h>\r
 #include "DebugCommunicationLibUsb3Internal.h"\r
 \r
+GUID                        gUsb3DbgGuid =  USB3_DBG_GUID;\r
+\r
+USB3_DEBUG_PORT_HANDLE      mUsb3Instance = {USB3DBG_UNINITIALIZED};\r
+EFI_PHYSICAL_ADDRESS        mUsb3InstanceAddr = 0;\r
+EFI_PHYSICAL_ADDRESS        *mUsb3InstanceAddrPtr = NULL;\r
+EFI_PCI_IO_PROTOCOL         *mUsb3PciIo = NULL;\r
+\r
+/**\r
+  Creates a named event that can be signaled.\r
+\r
+  This function creates an event using NotifyTpl, NoifyFunction.\r
+  If Name is NULL, then ASSERT().\r
+  If NotifyTpl is not a legal TPL value, then ASSERT().\r
+  If NotifyFunction is NULL, then ASSERT().\r
+\r
+  @param  Name                  Supplies the GUID name of the event.\r
+  @param  NotifyTpl             Supplies the task priority level of the event notifications.\r
+  @param  NotifyFunction        Supplies the function to notify when the event is signaled.\r
+  @param  Event                 A pointer to the event created.\r
+\r
+  @retval EFI_SUCCESS           A named event was created.\r
+  @retval EFI_OUT_OF_RESOURCES  There are not enough resource to create the named event.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Usb3NamedEventListen (\r
+  IN CONST EFI_GUID    *Name,\r
+  IN EFI_TPL           NotifyTpl,\r
+  IN EFI_EVENT_NOTIFY  NotifyFunction,\r
+  IN EFI_EVENT         *Event\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  VOID        *RegistrationLocal;\r
+\r
+  ASSERT (Name != NULL);\r
+  ASSERT (NotifyFunction != NULL);\r
+  ASSERT (NotifyTpl <= TPL_HIGH_LEVEL);\r
+\r
+  //\r
+  // Create event\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  NotifyTpl,\r
+                  NotifyFunction,\r
+                  NULL,\r
+                  Event\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Register for an installation of protocol interface\r
+  //\r
+  Status = gBS->RegisterProtocolNotify (\r
+                  (EFI_GUID *) Name,\r
+                  *Event,\r
+                  &RegistrationLocal\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  USB3 map one DMA buffer.\r
+\r
+  @param PciIo          Pointer to PciIo for USB3 debug port.\r
+  @param Address        DMA buffer address to be mapped.\r
+  @param NumberOfBytes  Number of bytes to be mapped.\r
+\r
+**/\r
+VOID\r
+Usb3MapOneDmaBuffer (\r
+  IN EFI_PCI_IO_PROTOCOL        *PciIo,\r
+  IN EFI_PHYSICAL_ADDRESS       Address,\r
+  IN UINTN                      NumberOfBytes\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  VOID                          *HostAddress;\r
+  EFI_PHYSICAL_ADDRESS          DeviceAddress;\r
+  VOID                          *Mapping;\r
+\r
+  HostAddress = (VOID *) (UINTN) Address;\r
+  Status = PciIo->Map (\r
+                    PciIo,\r
+                    EfiPciIoOperationBusMasterCommonBuffer,\r
+                    HostAddress,\r
+                    &NumberOfBytes,\r
+                    &DeviceAddress,\r
+                    &Mapping\r
+                    );\r
+  ASSERT_EFI_ERROR (Status);\r
+  ASSERT (DeviceAddress == ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress));\r
+}\r
+\r
+/**\r
+  USB3 map DMA buffers.\r
+\r
+  @param Instance       Pointer to USB3 debug port instance.\r
+  @param PciIo          Pointer to PciIo for USB3 debug port.\r
+\r
+**/\r
+VOID\r
+Usb3MapDmaBuffers (\r
+  IN USB3_DEBUG_PORT_HANDLE     *Instance,\r
+  IN EFI_PCI_IO_PROTOCOL        *PciIo\r
+  )\r
+{\r
+  Usb3MapOneDmaBuffer (\r
+    PciIo,\r
+    Instance->UrbIn.Data,\r
+    XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE * 2 + USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE\r
+    );\r
+\r
+  Usb3MapOneDmaBuffer (\r
+    PciIo,\r
+    Instance->TransferRingIn.RingSeg0,\r
+    sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER\r
+    );\r
+\r
+  Usb3MapOneDmaBuffer (\r
+    PciIo,\r
+    Instance->TransferRingOut.RingSeg0,\r
+    sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER\r
+    );\r
+\r
+  Usb3MapOneDmaBuffer (\r
+    PciIo,\r
+    Instance->EventRing.EventRingSeg0,\r
+    sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER\r
+    );\r
+\r
+  Usb3MapOneDmaBuffer (\r
+    PciIo,\r
+    Instance->EventRing.ERSTBase,\r
+    sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER\r
+    );\r
+\r
+  Usb3MapOneDmaBuffer (\r
+    PciIo,\r
+    Instance->DebugCapabilityContext,\r
+    sizeof (XHC_DC_CONTEXT)\r
+    );\r
+\r
+  Usb3MapOneDmaBuffer (\r
+    PciIo,\r
+    ((XHC_DC_CONTEXT *) (UINTN) Instance->DebugCapabilityContext)->DbcInfoContext.String0DescAddress,\r
+    STRING0_DESC_LEN + MANU_DESC_LEN + PRODUCT_DESC_LEN + SERIAL_DESC_LEN\r
+    );\r
+}\r
+\r
+/**\r
+  Invoke a notification event\r
+\r
+  @param[in] Event              Event whose notification function is being invoked.\r
+  @param[in] Context            The pointer to the notification function's context,\r
+                                which is implementation-dependent.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+Usb3DxeSmmReadyToLockNotify (\r
+  IN EFI_EVENT                  Event,\r
+  IN VOID                       *Context\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
+\r
+  Instance->InNotify = TRUE;\r
+\r
+  //\r
+  // For the case that the USB3 debug port instance and DMA buffers are\r
+  // from PEI HOB with IOMMU enabled.\r
+  // Reinitialize USB3 debug port with granted DXE DMA buffer accessible\r
+  // by SMM environment.\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
+  gBS->CloseEvent (Event);\r
+}\r
+\r
+/**\r
+  USB3 get IOMMU protocol.\r
+\r
+  @return Pointer to IOMMU protocol.\r
+\r
+**/\r
+EDKII_IOMMU_PROTOCOL *\r
+Usb3GetIoMmu (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  EDKII_IOMMU_PROTOCOL      *IoMmu;\r
+\r
+  IoMmu = NULL;\r
+  Status = gBS->LocateProtocol (\r
+             &gEdkiiIoMmuProtocolGuid,\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
+  Invoke a notification event\r
+\r
+  @param[in] Event              Event whose notification function is being invoked.\r
+  @param[in] Context            The pointer to the notification function's context,\r
+                                which is implementation-dependent.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+Usb3PciIoNotify (\r
+  IN  EFI_EVENT                 Event,\r
+  IN  VOID                      *Context\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  UINTN                         PciIoHandleCount;\r
+  EFI_HANDLE                    *PciIoHandleBuffer;\r
+  UINTN                         Index;\r
+  EFI_PCI_IO_PROTOCOL           *PciIo;\r
+  UINTN                         PciSegment;\r
+  UINTN                         PciBusNumber;\r
+  UINTN                         PciDeviceNumber;\r
+  UINTN                         PciFunctionNumber;\r
+  UINT32                        PciAddress;\r
+  USB3_DEBUG_PORT_HANDLE        *Instance;\r
+  EFI_EVENT                     SmmReadyToLockEvent;\r
+\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiPciIoProtocolGuid,\r
+                  NULL,\r
+                  &PciIoHandleCount,\r
+                  &PciIoHandleBuffer\r
+                  );\r
+  if (!EFI_ERROR (Status) &&\r
+      (PciIoHandleBuffer != NULL) &&\r
+      (PciIoHandleCount != 0)) { \r
+    for (Index = 0; Index < PciIoHandleCount; Index++) {\r
+      Status = gBS->HandleProtocol (\r
+                      PciIoHandleBuffer[Index],\r
+                      &gEfiPciIoProtocolGuid,\r
+                      (VOID **) &PciIo\r
+                      );\r
+      ASSERT_EFI_ERROR (Status);\r
+      Status = PciIo->GetLocation (PciIo, &PciSegment, &PciBusNumber, &PciDeviceNumber, &PciFunctionNumber);\r
+      ASSERT_EFI_ERROR (Status);\r
+      PciAddress = (UINT32) ((PciBusNumber << 20) | (PciDeviceNumber << 15) | (PciFunctionNumber << 12));\r
+      if (PciAddress == PcdGet32(PcdUsbXhciPciAddress)) {\r
+        //\r
+        // Found the PciIo for USB3 debug port.\r
+        //\r
+        DEBUG ((DEBUG_INFO, "%a()\n", __FUNCTION__));\r
+        if (Usb3GetIoMmu () != NULL) {\r
+          Instance = GetUsb3DebugPortInstance ();\r
+          ASSERT (Instance != NULL);\r
+          if (Instance->Ready) {\r
+            Instance->InNotify = TRUE;\r
+            Usb3MapDmaBuffers (Instance, PciIo);\r
+            Instance->InNotify = FALSE;\r
+\r
+            if (Instance->FromHob) {\r
+              mUsb3PciIo = PciIo;\r
+              Usb3NamedEventListen (\r
+                &gEfiDxeSmmReadyToLockProtocolGuid,\r
+                TPL_NOTIFY,\r
+                Usb3DxeSmmReadyToLockNotify,\r
+                &SmmReadyToLockEvent\r
+                );\r
+            }\r
+          }\r
+        }\r
+        gBS->CloseEvent (Event);\r
+        break;\r
+      }\r
+    }\r
+\r
+    gBS->FreePool (PciIoHandleBuffer);\r
+  }\r
+}\r
+\r
+/**\r
+  Return USB3 debug instance address pointer.\r
+\r
+**/  \r
+EFI_PHYSICAL_ADDRESS *\r
+GetUsb3DebugPortInstanceAddrPtr (\r
+  VOID\r
+  )\r
+{\r
+  if (mUsb3InstanceAddrPtr == NULL) {\r
+    //\r
+    // Use the local variables temporarily.\r
+    //\r
+    mUsb3InstanceAddr = (EFI_PHYSICAL_ADDRESS) (UINTN) &mUsb3Instance;\r
+    mUsb3InstanceAddrPtr = &mUsb3InstanceAddr;\r
+  }\r
+  return mUsb3InstanceAddrPtr;\r
+}\r
+\r
+/**\r
+  Allocates pages that are suitable for an OperationBusMasterCommonBuffer or\r
+  OperationBusMasterCommonBuffer64 mapping.\r
+\r
+  @param PciIo                  Pointer to PciIo for USB3 debug port.\r
+  @param Pages                  The number of pages to allocate.\r
+  @param Address                A pointer to store the base system memory address of the\r
+                                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 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
+Usb3AllocateDmaBuffer (\r
+  IN EFI_PCI_IO_PROTOCOL    *PciIo,\r
+  IN UINTN                  Pages,\r
+  OUT VOID                  **Address\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+\r
+  *Address = NULL;\r
+  Status = PciIo->AllocateBuffer (\r
+                    PciIo,\r
+                    AllocateAnyPages,\r
+                    EfiRuntimeServicesData,\r
+                    Pages,\r
+                    Address,\r
+                    0\r
+                    );\r
+  if (!EFI_ERROR (Status)) {\r
+    Usb3MapOneDmaBuffer (\r
+      PciIo,\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) *Address,\r
+      EFI_PAGES_TO_SIZE (Pages)\r
+      );\r
+  }\r
+  return Status;\r
+}\r
+\r
 /**\r
   Allocate aligned memory for XHC's usage.\r
 \r
@@ -37,17 +407,131 @@ AllocateAlignBuffer (
   Buf = NULL;\r
   \r
   if (gBS != NULL) {\r
-    TmpAddr = 0xFFFFFFFF;\r
+    if (mUsb3PciIo != NULL) {\r
+      Usb3AllocateDmaBuffer (\r
+        mUsb3PciIo,\r
+        EFI_SIZE_TO_PAGES (BufferSize),\r
+        &Buf\r
+        );\r
+    } else {\r
+      TmpAddr = 0xFFFFFFFF;\r
+      Status = gBS->AllocatePages (\r
+                      AllocateMaxAddress,\r
+                      EfiACPIMemoryNVS,\r
+                      EFI_SIZE_TO_PAGES (BufferSize),\r
+                      &TmpAddr\r
+                      );\r
+      if (!EFI_ERROR (Status)) {\r
+        Buf = (VOID *) (UINTN) TmpAddr;\r
+      }\r
+    }\r
+  }\r
+\r
+  return Buf;\r
+}\r
+\r
+/**\r
+  The constructor function initialize USB3 debug port.\r
+\r
+  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
+  @param  SystemTable   A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DebugCommunicationUsb3DxeConstructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_PHYSICAL_ADDRESS          *AddrPtr;\r
+  USB3_DEBUG_PORT_HANDLE        *Instance;\r
+  EFI_PHYSICAL_ADDRESS          Address;\r
+  EFI_STATUS                    Status;\r
+  EFI_EVENT                     Event;\r
+\r
+  Status = EfiGetSystemConfigurationTable (&gUsb3DbgGuid, (VOID **) &AddrPtr);\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // Instead of using local variables, install system configuration table for\r
+    // the local instance and the buffer to save instance address pointer.\r
+    //\r
+    Address = SIZE_4GB;\r
     Status = gBS->AllocatePages (\r
-               AllocateMaxAddress,\r
-               EfiACPIMemoryNVS,\r
-               EFI_SIZE_TO_PAGES (BufferSize),\r
-               &TmpAddr\r
+                    AllocateMaxAddress,\r
+                    EfiACPIMemoryNVS,\r
+                    EFI_SIZE_TO_PAGES (sizeof (EFI_PHYSICAL_ADDRESS) + sizeof (USB3_DEBUG_PORT_HANDLE)),\r
+                    &Address\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    AddrPtr = (EFI_PHYSICAL_ADDRESS *) (UINTN) Address;\r
+    ZeroMem (AddrPtr, sizeof (EFI_PHYSICAL_ADDRESS) + sizeof (USB3_DEBUG_PORT_HANDLE));\r
+    Instance = (USB3_DEBUG_PORT_HANDLE *) (AddrPtr + 1);\r
+    CopyMem (Instance, &mUsb3Instance, sizeof (USB3_DEBUG_PORT_HANDLE));\r
+    *AddrPtr = (EFI_PHYSICAL_ADDRESS) (UINTN) Instance;\r
+\r
+    Status = gBS->InstallConfigurationTable (&gUsb3DbgGuid, AddrPtr);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  if (mUsb3InstanceAddrPtr != NULL) {\r
+    *AddrPtr = *mUsb3InstanceAddrPtr;\r
+  }\r
+  mUsb3InstanceAddrPtr = AddrPtr;\r
+\r
+  Instance = GetUsb3DebugPortInstance ();\r
+  ASSERT (Instance != NULL);\r
+\r
+  if (Instance->PciIoEvent == 0) {\r
+    Status = Usb3NamedEventListen (\r
+               &gEfiPciIoProtocolGuid,\r
+               TPL_NOTIFY,\r
+               Usb3PciIoNotify,\r
+               &Event\r
                );\r
     if (!EFI_ERROR (Status)) {\r
-      Buf = (VOID *) (UINTN) TmpAddr;\r
+      Instance->PciIoEvent = (EFI_PHYSICAL_ADDRESS) (UINTN) Event;\r
     }\r
   }\r
 \r
-  return Buf;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  The destructor function.\r
+\r
+  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
+  @param  SystemTable   A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS   The destructor always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DebugCommunicationUsb3DxeDestructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  USB3_DEBUG_PORT_HANDLE        *Instance;\r
+\r
+  Instance = GetUsb3DebugPortInstance ();\r
+  ASSERT (Instance != NULL);\r
+\r
+  if (Instance->PciIoEvent != 0) {\r
+    //\r
+    // Close the event created.\r
+    //\r
+    gBS->CloseEvent ((EFI_EVENT) (UINTN) Instance->PciIoEvent);\r
+    Instance->PciIoEvent = 0;\r
+  }\r
+  return EFI_SUCCESS;\r
 }\r
+\r
index 08b9ec12d298ba795ff5c62e4c9b0a39cb7651bf..683d9572e9f7c5acf812211132b86f0d15fb162e 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 #  Debug Communication Library instance 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
 #\r
 #  This program and the accompanying materials\r
 #  are licensed and made available under the terms and conditions of the BSD License\r
@@ -21,6 +21,8 @@
   MODULE_TYPE                    = DXE_DRIVER\r
   VERSION_STRING                 = 1.0\r
   LIBRARY_CLASS                  = DebugCommunicationLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER SMM_CORE\r
+  CONSTRUCTOR                    = DebugCommunicationUsb3DxeConstructor\r
+  DESTRUCTOR                     = DebugCommunicationUsb3DxeDestructor\r
 \r
 #\r
 # The following information is for reference only and not required by the build tools.\r
 \r
   # The value of data buffer size used for USB debug port handle.\r
   # It should be equal to sizeof (USB3_DEBUG_PORT_HANDLE).\r
-  gEfiSourceLevelDebugPkgTokenSpaceGuid.PcdDebugPortHandleBufferSize|239   ## SOMETIMES_CONSUMES\r
+  gEfiSourceLevelDebugPkgTokenSpaceGuid.PcdDebugPortHandleBufferSize|249   ## SOMETIMES_CONSUMES\r
+\r
+[Protocols]\r
+   ## NOTIFY\r
+   ## SOMETIMES_CONSUMES\r
+  gEfiPciIoProtocolGuid\r
+  gEdkiiIoMmuProtocolGuid               ## SOMETIMES_CONSUMES\r
+   ## NOTIFY\r
+   ## SOMETIMES_CONSUMES\r
+  gEfiDxeSmmReadyToLockProtocolGuid\r
 \r
 [LibraryClasses]\r
   BaseLib\r
@@ -65,4 +76,4 @@
   UefiBootServicesTableLib\r
   UefiLib\r
   BaseMemoryLib\r
-  
\ No newline at end of file
+  HobLib\r
index 9da49f96440133224ecf53c77a04e1d39d03b11c..8673dad6c55e72cc7fd6a4ee1b0e9a1e53930133 100644 (file)
@@ -46,6 +46,7 @@
 #define USB3DBG_DBG_CAB       1   // The XHCI host controller supports debug capability\r
 #define USB3DBG_ENABLED       2   // The XHCI debug device is enabled\r
 #define USB3DBG_NOT_ENABLED   4   // The XHCI debug device is not enabled\r
+#define USB3DBG_UNINITIALIZED 255 // The XHCI debug device is uninitialized\r
 \r
 #define USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE 0x08\r
 \r
@@ -456,7 +457,7 @@ typedef struct _USB3_DEBUG_PORT_INSTANCE {
   UINT8                                   Initialized;\r
 \r
   //\r
-  // The flag indicates debug device is ready\r
+  // The flag indicates debug capability is supported\r
   //\r
   BOOLEAN                                 DebugSupport;\r
   \r
@@ -465,6 +466,21 @@ typedef struct _USB3_DEBUG_PORT_INSTANCE {
   //\r
   BOOLEAN                                 Ready;\r
 \r
+  //\r
+  // The flag indicates the instance is from HOB\r
+  //\r
+  BOOLEAN                                 FromHob;\r
+\r
+  //\r
+  // Prevent notification being interrupted by debug timer\r
+  //\r
+  BOOLEAN                                 InNotify;\r
+\r
+  //\r
+  // PciIo protocol event\r
+  //\r
+  EFI_PHYSICAL_ADDRESS                    PciIoEvent;\r
+\r
   //\r
   // The flag indicates if USB 3.0 ports has been turn off/on power\r
   //  \r
@@ -691,4 +707,37 @@ XhcDataTransfer (
   IN     UINTN                               Timeout\r
   );\r
 \r
-#endif //__USB3_DEBUG_PORT_LIB_INTERNAL__\r
+/**\r
+  Initialize usb debug port hardware.\r
+\r
+  @param  Handle           Debug port handle.\r
+\r
+  @retval TRUE             The usb debug port hardware configuration is changed.\r
+  @retval FALSE            The usb debug port hardware configuration is not changed.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+InitializeUsbDebugHardware (\r
+  IN USB3_DEBUG_PORT_HANDLE *Handle\r
+  );\r
+\r
+/**\r
+  Return USB3 debug instance address pointer.\r
+\r
+**/  \r
+EFI_PHYSICAL_ADDRESS *\r
+GetUsb3DebugPortInstanceAddrPtr (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Return USB3 debug instance address.\r
+\r
+**/  \r
+USB3_DEBUG_PORT_HANDLE *\r
+GetUsb3DebugPortInstance (\r
+  VOID\r
+  );\r
+\r
+#endif //__SERIAL_PORT_LIB_USB__\r
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
index 2108face50d21919445eb3af8037ac5a54a938d8..12257bce7da736a26813ef62fb43b90cb3233e5f 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 #  Debug Communication Library instance 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
 #\r
 #  This program and the accompanying materials\r
 #  are licensed and made available under the terms and conditions of the BSD License\r
@@ -41,6 +41,7 @@
 \r
 [Ppis]\r
   gEfiPeiMemoryDiscoveredPpiGuid                ## CONSUMES\r
+  gEdkiiIoMmuPpiGuid                            ## SOMETIMES_CONSUMES\r
 \r
 [Pcd]\r
   # The memory BAR of ehci host controller, in which usb debug feature is enabled.\r
@@ -57,7 +58,7 @@
 \r
   # The value of data buffer size used for USB debug port handle.\r
   # It should be equal to sizeof (USB3_DEBUG_PORT_HANDLE).\r
-  gEfiSourceLevelDebugPkgTokenSpaceGuid.PcdDebugPortHandleBufferSize|239   ## SOMETIMES_CONSUMES\r
+  gEfiSourceLevelDebugPkgTokenSpaceGuid.PcdDebugPortHandleBufferSize|249   ## SOMETIMES_CONSUMES\r
 \r
 [LibraryClasses]\r
   BaseLib\r
@@ -67,6 +68,7 @@
   TimerLib\r
   BaseMemoryLib\r
   PeiServicesLib\r
-  \r
+  HobLib\r
+\r
 [Depex.common.PEIM]\r
   gEfiPeiMemoryDiscoveredPpiGuid\r