public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU
@ 2018-03-14  9:33 Star Zeng
  2018-03-14  9:33 ` [PATCH V2 1/6] Revert "DebugUsb3: Check mUsb3Instance before dereferencing it" Star Zeng
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Star Zeng @ 2018-03-14  9:33 UTC (permalink / raw)
  To: edk2-devel; +Cc: Star Zeng, Ruiyu Ni, Hao Wu

The patch series is also at
https://github.com/lzeng14/edk2 DebugCommUsb3AfterIOMMUV2 branch.

Based on the feedbacks from Ray and Hao.
It is V2 of
https://lists.01.org/pipermail/edk2-devel/2018-March/022586.html.
It has no essential difference with V1 about the final code, but
re-arranges the patches to revert old IOMMU support patches and
then re-support IOMMU.

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>

Star Zeng (6):
  Revert "DebugUsb3: Check mUsb3Instance before dereferencing it"
  Revert "DebugUsb3: Fix GCC build failures"
  Revert "DebugUsb3: Support IOMMU"
  SourceLevelDebugPkg DebugUsb3: Re-Fix GCC build failures
  SourceLevelDebugPkg DebugCommUsb3: Refine some formats/comments
  SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU

 .../DebugCommunicationLibUsb3Common.c              | 110 +++++-
 .../DebugCommunicationLibUsb3Dxe.c                 | 375 ++++++++++++---------
 .../DebugCommunicationLibUsb3Dxe.inf               |  11 +-
 .../DebugCommunicationLibUsb3Internal.h            |  60 +---
 .../DebugCommunicationLibUsb3Pei.c                 |  50 ++-
 .../DebugCommunicationLibUsb3Pei.inf               |   4 +-
 6 files changed, 351 insertions(+), 259 deletions(-)

-- 
2.7.0.windows.1



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH V2 1/6] Revert "DebugUsb3: Check mUsb3Instance before dereferencing it"
  2018-03-14  9:33 [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
@ 2018-03-14  9:33 ` Star Zeng
  2018-03-14  9:33 ` [PATCH V2 2/6] Revert "DebugUsb3: Fix GCC build failures" Star Zeng
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Star Zeng @ 2018-03-14  9:33 UTC (permalink / raw)
  To: edk2-devel; +Cc: Star Zeng, Ruiyu Ni, Hao Wu

This reverts commit 6ef394ffe29bbc67038fc16ed540bfe6eed10e16.

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>
---
 .../Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c
index 29cec56f39dc..1582b9a8d6de 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c
@@ -443,7 +443,7 @@ DebugCommunicationUsb3DxeConstructor (
   }
 
 Done:
-  if ((mUsb3Instance != NULL) && mUsb3Instance->Ready && (mUsb3Instance->PciIoEvent == 0)) {
+  if (mUsb3Instance->Ready && (mUsb3Instance->PciIoEvent == 0)) {
     Status = Usb3NamedEventListen (
                &gEfiPciIoProtocolGuid,
                TPL_NOTIFY,
-- 
2.7.0.windows.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH V2 2/6] Revert "DebugUsb3: Fix GCC build failures"
  2018-03-14  9:33 [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
  2018-03-14  9:33 ` [PATCH V2 1/6] Revert "DebugUsb3: Check mUsb3Instance before dereferencing it" Star Zeng
@ 2018-03-14  9:33 ` Star Zeng
  2018-03-14  9:33 ` [PATCH V2 3/6] Revert "DebugUsb3: Support IOMMU" Star Zeng
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Star Zeng @ 2018-03-14  9:33 UTC (permalink / raw)
  To: edk2-devel; +Cc: Star Zeng, Ruiyu Ni, Hao Wu

This reverts commit ad6040ec9b5bbc462762331f9738b8e42c0b9c80.

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>
---
 .../DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c       | 2 ++
 .../DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c     | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
index c577df7dea97..87fb0265489b 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
@@ -906,6 +906,7 @@ DebugPortWriteBuffer (
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;
   UINTN                     Sent;
   UINTN                     Total;
+  UINTN                     Index;
 
   if (NumberOfBytes == 0 || Buffer == NULL) {
     return 0;
@@ -933,6 +934,7 @@ DebugPortWriteBuffer (
   //
   DebugPortPollBuffer (UsbDebugPortHandle);
 
+  Index = 0;
   while ((Total < NumberOfBytes)) {
     if (NumberOfBytes - Total > USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE) {
       Sent = USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE;
diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c
index 1b6645bd1c6d..dbff49362407 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c
@@ -1,7 +1,7 @@
 /** @file
   Debug Port Library implementation based on usb3 debug port.
 
-  Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD License
   which accompanies this distribution.  The full text of the license may be found at
@@ -145,9 +145,11 @@ XhcCheckNewEvent (
   )
 {
   EFI_STATUS          Status;
+  TRB_TEMPLATE        *EvtTrb;
 
   ASSERT (EvtRing != NULL);
 
+  EvtTrb     = (TRB_TEMPLATE *)(UINTN) EvtRing->EventRingDequeue;
   *NewEvtTrb = (TRB_TEMPLATE *)(UINTN) EvtRing->EventRingDequeue;
 
   if (EvtRing->EventRingDequeue == EvtRing->EventRingEnqueue) {
-- 
2.7.0.windows.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH V2 3/6] Revert "DebugUsb3: Support IOMMU"
  2018-03-14  9:33 [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
  2018-03-14  9:33 ` [PATCH V2 1/6] Revert "DebugUsb3: Check mUsb3Instance before dereferencing it" Star Zeng
  2018-03-14  9:33 ` [PATCH V2 2/6] Revert "DebugUsb3: Fix GCC build failures" Star Zeng
@ 2018-03-14  9:33 ` Star Zeng
  2018-03-14  9:33 ` [PATCH V2 4/6] SourceLevelDebugPkg DebugUsb3: Re-Fix GCC build failures Star Zeng
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Star Zeng @ 2018-03-14  9:33 UTC (permalink / raw)
  To: edk2-devel; +Cc: Star Zeng, Ruiyu Ni, Hao Wu

This reverts commit de8373fa07f87ca735139bb86c51e2c29fb1d956.

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>
---
 .../DebugCommunicationLibUsb3Common.c              | 222 ++++++-----
 .../DebugCommunicationLibUsb3Dxe.c                 | 435 +--------------------
 .../DebugCommunicationLibUsb3Dxe.inf               |  12 +-
 .../DebugCommunicationLibUsb3Internal.h            |  60 +--
 .../DebugCommunicationLibUsb3Pei.c                 | 234 +----------
 .../DebugCommunicationLibUsb3Pei.inf               |   8 +-
 6 files changed, 129 insertions(+), 842 deletions(-)

diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
index 87fb0265489b..49bad6b5864d 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
@@ -14,6 +14,11 @@
 
 #include "DebugCommunicationLibUsb3Internal.h"
 
+//
+// The global variable which can be used after memory is ready.
+//
+USB3_DEBUG_PORT_HANDLE     mDebugCommunicationLibUsb3DebugPortHandle;
+
 UINT16   mString0Desc[] = {
   //  String Descriptor Type + Length
   ( USB_DESC_TYPE_STRING << 8 ) + STRING0_DESC_LEN,
@@ -80,7 +85,7 @@ XhcClearR32Bit(
   Write the data to the XHCI debug register.
 
   @param  Handle       Debug port handle.
-  @param  Offset       The offset of the debug register.
+  @param  Offset       The offset of the runtime register.
   @param  Data         The data to write.
 
 **/
@@ -124,16 +129,16 @@ XhcReadDebugReg (
 }
 
 /**
-  Set one bit of the debug register while keeping other bits.
+  Set one bit of the runtime register while keeping other bits.
 
   @param  Handle       Debug port handle.
-  @param  Offset       The offset of the debug register.
+  @param  Offset       The offset of the runtime register.
   @param  Bit          The bit mask of the register to set.
 
 **/
 VOID
 XhcSetDebugRegBit (
-  IN USB3_DEBUG_PORT_HANDLE   *Handle,
+  IN USB3_DEBUG_PORT_HANDLE *Handle,
   IN UINT32                   Offset,
   IN UINT32                   Bit
   )
@@ -146,28 +151,6 @@ XhcSetDebugRegBit (
 }
 
 /**
-  Clear one bit of the debug register while keeping other bits.
-
-  @param  Handle       Debug port handle.
-  @param  Offset       The offset of the debug register.
-  @param  Bit          The bit mask of the register to clear.
-
-**/
-VOID
-XhcClearDebugRegBit (
-  IN USB3_DEBUG_PORT_HANDLE   *Handle,
-  IN UINT32                   Offset,
-  IN UINT32                   Bit
-  )
-{
-  UINT32                  Data;
-
-  Data  = XhcReadDebugReg (Handle, Offset);
-  Data  &= ~Bit;
-  XhcWriteDebugReg (Handle, Offset, Data);
-}
-
-/**
   Program and eanble XHCI MMIO base address.
 
   @return XHCI MMIO base address.
@@ -216,7 +199,7 @@ UpdateXhcResource (
   IN EFI_PHYSICAL_ADDRESS                   XhciMmioBase
   )
 {
-  if (Handle == NULL) {
+  if ((Handle == NULL) || (Handle->XhciMmioBase == XhciMmioBase)) {
     return;
   }
 
@@ -233,7 +216,7 @@ UpdateXhcResource (
 
   @param  Handle             Debug port handle.
 
-  @retval RETURN_UNSUPPORTED The usb host controller does not support usb debug port capability.
+  @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.
   @retval RETURN_SUCCESS     Get bar and offset successfully.
 
 **/
@@ -253,14 +236,6 @@ CalculateUsbDebugPortMmioBase (
   EFI_PHYSICAL_ADDRESS            CapabilityPointer;
   UINT8                           CapLength;
 
-  if (Handle->Initialized != USB3DBG_UNINITIALIZED) {
-    if (Handle->Initialized == USB3DBG_NO_DBG_CAB) {
-      return RETURN_UNSUPPORTED;
-    } else {
-      return RETURN_SUCCESS;
-    }
-  }
-
   VendorId = PciRead16 (PcdGet32(PcdUsbXhciPciAddress) + PCI_VENDOR_ID_OFFSET);
   DeviceId = PciRead16 (PcdGet32(PcdUsbXhciPciAddress) + PCI_DEVICE_ID_OFFSET);
   
@@ -313,7 +288,6 @@ CalculateUsbDebugPortMmioBase (
   Handle->DebugCapabilityBase   = CapabilityPointer;
   Handle->DebugCapabilityOffset = CapabilityPointer - Handle->XhciMmioBase;
   Handle->XhciOpRegister        = Handle->XhciMmioBase + CapLength;
-  Handle->DebugSupport = TRUE;
   Handle->Initialized = USB3DBG_DBG_CAB;
   return RETURN_SUCCESS;
 
@@ -352,9 +326,6 @@ NeedReinitializeHardware(
   Dcctrl = XhcReadDebugReg (Handle, XHC_DC_DCCTRL);
   if ((Dcctrl & BIT0) == 0) {
     Result = TRUE;
-  } else if (!Handle->Ready) {
-    Handle->Ready = TRUE;
-    Handle->Initialized = USB3DBG_ENABLED;
   }
 
   return Result;
@@ -707,13 +678,6 @@ InitializeUsbDebugHardware (
   }
 
   //
-  // Clear DCE bit and LSE bit in DCCTRL
-  //
-  if ((XhcReadDebugReg (Handle, XHC_DC_DCCTRL) & (BIT1|BIT31)) == (BIT1|BIT31)) {
-    XhcClearDebugRegBit (Handle, XHC_DC_DCCTRL, BIT1|BIT31);
-  }
-
-  //
   // Construct the buffer for read, poll and write.
   //
   Handle->UrbIn.Data  = (EFI_PHYSICAL_ADDRESS)(UINTN) Buffer;
@@ -782,35 +746,6 @@ Enable:
 }
 
 /**
-  Discover and initialize usb debug port.
-
-  @param Handle                 Debug port handle.
-
-**/
-VOID
-DiscoverInitializeUsbDebugPort (
-  IN USB3_DEBUG_PORT_HANDLE     *Handle
-  )
-{
-  EFI_STATUS                    Status;
-  EFI_PHYSICAL_ADDRESS          XhciMmioBase;
-
-  //
-  // Read 64-bit MMIO base address
-  //
-  XhciMmioBase = ProgramXhciBaseAddress ();
-  Handle->XhciMmioBase = XhciMmioBase;
-
-  Status = CalculateUsbDebugPortMmioBase (Handle);
-  if (!RETURN_ERROR (Status)) {
-    UpdateXhcResource (Handle, XhciMmioBase);
-    if (NeedReinitializeHardware (Handle)) {
-      InitializeUsbDebugHardware (Handle);
-    }
-  }
-}
-
-/**
   Read data from debug device and save the data in buffer.
 
   Reads NumberOfBytes data bytes from a debug device into the buffer
@@ -837,6 +772,7 @@ DebugPortReadBuffer (
   )
 {
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;
+  RETURN_STATUS             Status;
   UINT8                     Index;
   UINT8                     *Data;
 
@@ -844,17 +780,25 @@ DebugPortReadBuffer (
     return 0;
   }
 
-  UsbDebugPortHandle = GetUsb3DebugPortInstance ();
-  if (UsbDebugPortHandle == NULL) {
-    return 0;
+  //
+  // If Handle is NULL, it means memory is ready for use.
+  // Use global variable to store handle value.
+  //
+  if (Handle == NULL) {
+    UsbDebugPortHandle = &mDebugCommunicationLibUsb3DebugPortHandle;
+  } else {
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Handle;
   }
-
-  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {
+  
+  if (UsbDebugPortHandle->Initialized == USB3DBG_NO_DBG_CAB) {
     return 0;
   }
-
-  if (UsbDebugPortHandle->InNotify) {
-    return 0;
+  
+  if (NeedReinitializeHardware(UsbDebugPortHandle)) {
+    Status = InitializeUsbDebugHardware (UsbDebugPortHandle);
+    if (RETURN_ERROR(Status)) {
+      return 0;
+    }
   }
 
   Data = (UINT8 *)(UINTN)UsbDebugPortHandle->Data;
@@ -904,8 +848,10 @@ DebugPortWriteBuffer (
   )
 {
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;
+  RETURN_STATUS             Status;
   UINTN                     Sent;
   UINTN                     Total;
+  EFI_PHYSICAL_ADDRESS      XhciMmioBase;
   UINTN                     Index;
 
   if (NumberOfBytes == 0 || Buffer == NULL) {
@@ -915,24 +861,39 @@ DebugPortWriteBuffer (
   Sent  = 0;
   Total = 0;
 
-  UsbDebugPortHandle = GetUsb3DebugPortInstance ();
-  if (UsbDebugPortHandle == NULL) {
-    return 0;
+  //
+  // If Handle is NULL, it means memory is ready for use.
+  // Use global variable to store handle value.
+  //
+  if (Handle == NULL) {
+    UsbDebugPortHandle = &mDebugCommunicationLibUsb3DebugPortHandle;
+  } else {
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Handle;
   }
-
-  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {
+  
+  if (UsbDebugPortHandle->Initialized == USB3DBG_NO_DBG_CAB) {
     return 0;
   }
 
-  if (UsbDebugPortHandle->InNotify) {
-    return 0;
+  //
+  // MMIO base address is possible to clear, set it if it is cleared. (XhciMemorySpaceClose in PchUsbCommon.c)
+  //
+  XhciMmioBase = ProgramXhciBaseAddress ();
+
+  UpdateXhcResource (UsbDebugPortHandle, XhciMmioBase);
+
+  if (NeedReinitializeHardware(UsbDebugPortHandle)) {
+    Status = InitializeUsbDebugHardware (UsbDebugPortHandle);
+    if (RETURN_ERROR(Status)) {
+      return 0;
+    }
   }
 
   //
   // When host is trying to send data, write will be blocked.
   // Poll to see if there is any data sent by host at first.
   //
-  DebugPortPollBuffer (UsbDebugPortHandle);
+  DebugPortPollBuffer (Handle);
 
   Index = 0;
   while ((Total < NumberOfBytes)) {
@@ -941,7 +902,7 @@ DebugPortWriteBuffer (
     } else {
       Sent = (UINT8)(NumberOfBytes - Total);
     }
-    XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataOut, Buffer + Total, &Sent, DATA_TRANSFER_WRITE_TIMEOUT);
+    Status = XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataOut, Buffer + Total, &Sent, DATA_TRANSFER_WRITE_TIMEOUT);
     Total += Sent;
   }
   
@@ -969,20 +930,33 @@ DebugPortPollBuffer (
 {
   USB3_DEBUG_PORT_HANDLE     *UsbDebugPortHandle;
   UINTN                     Length;
+  RETURN_STATUS             Status;
+  EFI_PHYSICAL_ADDRESS      XhciMmioBase;
 
-  UsbDebugPortHandle = GetUsb3DebugPortInstance ();
-  if (UsbDebugPortHandle == NULL) {
-    return FALSE;
+  //
+  // If Handle is NULL, it means memory is ready for use.
+  // Use global variable to store handle value.
+  //
+  if (Handle == NULL) {
+    UsbDebugPortHandle = &mDebugCommunicationLibUsb3DebugPortHandle;
+  } else {
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Handle;
   }
 
-  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {
-    return FALSE;
+  if (UsbDebugPortHandle->Initialized == USB3DBG_NO_DBG_CAB) {
+    return 0;
   }
 
-  if (UsbDebugPortHandle->InNotify) {
-    return FALSE;
+  XhciMmioBase = ProgramXhciBaseAddress ();
+  UpdateXhcResource (UsbDebugPortHandle, XhciMmioBase);
+  
+  if (NeedReinitializeHardware(UsbDebugPortHandle)) {
+    Status = InitializeUsbDebugHardware(UsbDebugPortHandle);
+    if (RETURN_ERROR(Status)) {
+      return FALSE;
+    }
   }
-
+  
   //
   // If the data buffer is not empty, then return TRUE directly.
   // Otherwise initialize a usb read transaction and read data to internal data buffer.
@@ -995,7 +969,7 @@ DebugPortPollBuffer (
   // Read data as much as we can
   //
   Length = XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE;
-  XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataIn, (VOID *)(UINTN)UsbDebugPortHandle->Data, &Length, DATA_TRANSFER_POLL_TIMEOUT);
+  XhcDataTransfer (Handle, EfiUsbDataIn, (VOID *)(UINTN)UsbDebugPortHandle->Data, &Length, DATA_TRANSFER_POLL_TIMEOUT);
 
   if (Length > XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE) {
     return FALSE;
@@ -1043,16 +1017,50 @@ DebugPortInitialize (
   IN DEBUG_PORT_CONTINUE  Function
   )
 {
+  RETURN_STATUS             Status;
+  USB3_DEBUG_PORT_HANDLE    Handle;
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;
 
-  UsbDebugPortHandle = GetUsb3DebugPortInstance ();
-  if (UsbDebugPortHandle == NULL) {
-    return NULL;
+  //
+  // Validate the PCD PcdDebugPortHandleBufferSize value 
+  //
+  ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize) == sizeof (USB3_DEBUG_PORT_HANDLE));
+
+  if (Function == NULL && Context != NULL) {
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Context;
+  } else {
+    ZeroMem(&Handle, sizeof (USB3_DEBUG_PORT_HANDLE));
+    UsbDebugPortHandle = &Handle;
   }
 
+  if (Function == NULL && Context != NULL) {
+    return (DEBUG_PORT_HANDLE *) Context;
+  }
+  
+  //
+  // Read 64-bit MMIO base address
+  //
+  UsbDebugPortHandle->XhciMmioBase = ProgramXhciBaseAddress ();
+
+  Status = CalculateUsbDebugPortMmioBase (UsbDebugPortHandle);
+  if (RETURN_ERROR (Status)) {
+    goto Exit;
+  }
+
+  if (NeedReinitializeHardware(&Handle)) {
+    Status = InitializeUsbDebugHardware (&Handle);
+    if (RETURN_ERROR(Status)) {
+      goto Exit;
+    }
+  }
+
+Exit:
+
   if (Function != NULL) {
-    Function (Context, UsbDebugPortHandle);
+    Function (Context, &Handle);
+  } else {
+    CopyMem(&mDebugCommunicationLibUsb3DebugPortHandle, &Handle, sizeof (USB3_DEBUG_PORT_HANDLE));
   }
 
-  return (DEBUG_PORT_HANDLE)(UINTN)UsbDebugPortHandle;
+  return (DEBUG_PORT_HANDLE)(UINTN)&mDebugCommunicationLibUsb3DebugPortHandle;
 }
diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c
index 1582b9a8d6de..c4a8a4768299 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.c
@@ -1,7 +1,7 @@
 /** @file
   Debug Port Library implementation based on usb3 debug port.
 
-  Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD License
   which accompanies this distribution.  The full text of the license may be found at
@@ -15,331 +15,8 @@
 #include <Base.h>
 #include <PiDxe.h>
 #include <Library/UefiBootServicesTableLib.h>
-#include <Library/HobLib.h>
-#include <Protocol/PciIo.h>
-#include <Protocol/IoMmu.h>
 #include "DebugCommunicationLibUsb3Internal.h"
 
-GUID                        gUsb3DbgGuid =  USB3_DBG_GUID;
-
-USB3_DEBUG_PORT_HANDLE      *mUsb3Instance = NULL;
-
-/**
-  Creates a named event that can be signaled.
-
-  This function creates an event using NotifyTpl, NoifyFunction.
-  If Name is NULL, then ASSERT().
-  If NotifyTpl is not a legal TPL value, then ASSERT().
-  If NotifyFunction is NULL, then ASSERT().
-
-  @param  Name                  Supplies the GUID name of the event.
-  @param  NotifyTpl             Supplies the task priority level of the event notifications.
-  @param  NotifyFunction        Supplies the function to notify when the event is signaled.
-  @param  Event                 A pointer to the event created.
-
-  @retval EFI_SUCCESS           A named event was created.
-  @retval EFI_OUT_OF_RESOURCES  There are not enough resource to create the named event.
-
-**/
-EFI_STATUS
-EFIAPI
-Usb3NamedEventListen (
-  IN CONST EFI_GUID    *Name,
-  IN EFI_TPL           NotifyTpl,
-  IN EFI_EVENT_NOTIFY  NotifyFunction,
-  IN EFI_EVENT         *Event
-  )
-{
-  EFI_STATUS  Status;
-  VOID        *RegistrationLocal;
-
-  ASSERT (Name != NULL);
-  ASSERT (NotifyFunction != NULL);
-  ASSERT (NotifyTpl <= TPL_HIGH_LEVEL);
-
-  //
-  // Create event
-  //
-  Status = gBS->CreateEvent (
-                  EVT_NOTIFY_SIGNAL,
-                  NotifyTpl,
-                  NotifyFunction,
-                  NULL,
-                  Event
-                  );
-  ASSERT_EFI_ERROR (Status);
-
-  //
-  // Register for an installation of protocol interface
-  //
-  Status = gBS->RegisterProtocolNotify (
-                  (EFI_GUID *) Name,
-                  *Event,
-                  &RegistrationLocal
-                  );
-  ASSERT_EFI_ERROR (Status);
-
-  return Status;
-}
-
-/**
-  USB3 map one DMA buffer.
-
-  @param Instance       Pointer to USB3 debug port instance.
-  @param PciIo          Pointer to PciIo for USB3 debug port.
-  @param Address        DMA buffer address to be mapped.
-  @param NumberOfBytes  Number of bytes to be mapped.
-  @param BackupBuffer   Backup buffer address.
-
-**/
-VOID
-Usb3MapOneDmaBuffer (
-  IN USB3_DEBUG_PORT_HANDLE     *Instance,
-  IN EFI_PCI_IO_PROTOCOL        *PciIo,
-  IN EFI_PHYSICAL_ADDRESS       Address,
-  IN UINTN                      NumberOfBytes,
-  IN EFI_PHYSICAL_ADDRESS       BackupBuffer
-  )
-{
-  EFI_STATUS                    Status;
-  VOID                          *HostAddress;
-  EFI_PHYSICAL_ADDRESS          DeviceAddress;
-  VOID                          *Mapping;
-
-  HostAddress = (VOID *) (UINTN) Address;
-  Status = PciIo->Map (
-                    PciIo,
-                    EfiPciIoOperationBusMasterCommonBuffer,
-                    HostAddress,
-                    &NumberOfBytes,
-                    &DeviceAddress,
-                    &Mapping
-                    );
-  ASSERT_EFI_ERROR (Status);
-  ASSERT (DeviceAddress == ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress));
-  if (Instance->FromHob) {
-    //
-    // Reallocate the DMA buffer by AllocateAddress with
-    // the memory type accessible by SMM.
-    //
-    CopyMem ((VOID *) (UINTN) BackupBuffer, (VOID *) (UINTN) Address, NumberOfBytes);
-    Status = gBS->FreePages (Address, EFI_SIZE_TO_PAGES (NumberOfBytes));
-    ASSERT_EFI_ERROR (Status);
-    Status = gBS->AllocatePages (
-                    AllocateAddress,
-                    EfiACPIMemoryNVS,
-                    EFI_SIZE_TO_PAGES (NumberOfBytes),
-                    &Address
-                    );
-    ASSERT_EFI_ERROR (Status);
-    CopyMem ((VOID *) (UINTN) Address, (VOID *) (UINTN) BackupBuffer, NumberOfBytes);
-  }
-}
-
-/**
-  USB3 map DMA buffers.
-
-  @param Instance       Pointer to USB3 debug port instance.
-  @param PciIo          Pointer to PciIo for USB3 debug port.
-
-**/
-VOID
-Usb3MapDmaBuffers (
-  IN USB3_DEBUG_PORT_HANDLE     *Instance,
-  IN EFI_PCI_IO_PROTOCOL        *PciIo
-  )
-{
-  EFI_STATUS                    Status;
-  EDKII_IOMMU_PROTOCOL          *IoMmu;
-  EFI_PHYSICAL_ADDRESS          BackupBuffer;
-  UINTN                         BackupBufferSize;
-
-  IoMmu = NULL;
-  Status = gBS->LocateProtocol (&gEdkiiIoMmuProtocolGuid, NULL, (VOID **) &IoMmu);
-  if (EFI_ERROR (Status) || (IoMmu == NULL)) {
-    //
-    // No need to map the DMA buffers.
-    //
-    return;
-  }
-
-  //
-  // Allocate backup buffer for the case that the USB3
-  // debug port instance and DMA buffers are from PEI HOB.
-  // For this case, the DMA buffers need to be reallocated
-  // by AllocateAddress with the memory type accessible by
-  // SMM.
-  //
-  BackupBufferSize = MAX (XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE * 2 + USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE,
-                          MAX (sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER,
-                               MAX (sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER,
-                                    MAX (sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER,
-                                         MAX (sizeof (XHC_DC_CONTEXT),
-                                              STRING0_DESC_LEN + MANU_DESC_LEN + PRODUCT_DESC_LEN + SERIAL_DESC_LEN)))));
-
-  Status = gBS->AllocatePages (
-                  AllocateAnyPages,
-                  EfiBootServicesData,
-                  EFI_SIZE_TO_PAGES (BackupBufferSize),
-                  &BackupBuffer
-                  );
-  ASSERT_EFI_ERROR (Status);
-
-  Usb3MapOneDmaBuffer (
-    Instance,
-    PciIo,
-    Instance->UrbIn.Data,
-    XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE * 2 + USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE,
-    BackupBuffer
-    );
-
-  Usb3MapOneDmaBuffer (
-    Instance,
-    PciIo,
-    Instance->TransferRingIn.RingSeg0,
-    sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER,
-    BackupBuffer
-    );
-
-  Usb3MapOneDmaBuffer (
-    Instance,
-    PciIo,
-    Instance->TransferRingOut.RingSeg0,
-    sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER,
-    BackupBuffer
-    );
-
-  Usb3MapOneDmaBuffer (
-    Instance,
-    PciIo,
-    Instance->EventRing.EventRingSeg0,
-    sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER,
-    BackupBuffer
-    );
-
-  Usb3MapOneDmaBuffer (
-    Instance,
-    PciIo,
-    Instance->EventRing.ERSTBase,
-    sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER,
-    BackupBuffer
-    );
-
-  Usb3MapOneDmaBuffer (
-    Instance,
-    PciIo,
-    Instance->DebugCapabilityContext,
-    sizeof (XHC_DC_CONTEXT),
-    BackupBuffer
-    );
-
-  Usb3MapOneDmaBuffer (
-    Instance,
-    PciIo,
-    ((XHC_DC_CONTEXT *) (UINTN) Instance->DebugCapabilityContext)->DbcInfoContext.String0DescAddress,
-    STRING0_DESC_LEN + MANU_DESC_LEN + PRODUCT_DESC_LEN + SERIAL_DESC_LEN,
-    BackupBuffer
-    );
-
-  gBS->FreePages (BackupBuffer, EFI_SIZE_TO_PAGES (BackupBufferSize));
-}
-
-/**
-  Invoke a notification event
-
-  @param[in]  Event                 Event whose notification function is being invoked.
-  @param[in]  Context               The pointer to the notification function's context,
-                                    which is implementation-dependent.
-
-**/
-VOID
-EFIAPI
-Usb3PciIoNotify (
-  IN  EFI_EVENT                Event,
-  IN  VOID                     *Context
-  )
-{
-  EFI_STATUS                    Status;
-  UINTN                         PciIoHandleCount;
-  EFI_HANDLE                    *PciIoHandleBuffer;
-  UINTN                         Index;
-  EFI_PCI_IO_PROTOCOL           *PciIo;
-  UINTN                         PciSegment;
-  UINTN                         PciBusNumber;
-  UINTN                         PciDeviceNumber;
-  UINTN                         PciFunctionNumber;
-  UINT32                        PciAddress;
-
-  ASSERT (mUsb3Instance != NULL);
-
-  Status = gBS->LocateHandleBuffer (
-                  ByProtocol,
-                  &gEfiPciIoProtocolGuid,
-                  NULL,
-                  &PciIoHandleCount,
-                  &PciIoHandleBuffer
-                  );
-  if (!EFI_ERROR (Status) &&
-      (PciIoHandleBuffer != NULL) &&
-      (PciIoHandleCount != 0)) { 
-    for (Index = 0; Index < PciIoHandleCount; Index++) {
-      Status = gBS->HandleProtocol (
-                      PciIoHandleBuffer[Index],
-                      &gEfiPciIoProtocolGuid,
-                      (VOID **) &PciIo
-                      );
-      ASSERT_EFI_ERROR (Status);
-      Status = PciIo->GetLocation (PciIo, &PciSegment, &PciBusNumber, &PciDeviceNumber, &PciFunctionNumber);
-      ASSERT_EFI_ERROR (Status);
-      PciAddress = (UINT32) ((PciBusNumber << 20) | (PciDeviceNumber << 15) | (PciFunctionNumber << 12));
-      if (PciAddress == PcdGet32(PcdUsbXhciPciAddress)) {
-        //
-        // Found the PciIo for USB3 debug port.
-        //
-        DEBUG ((DEBUG_INFO, "%a()\n", __FUNCTION__));
-        mUsb3Instance->InNotify = TRUE;
-        Usb3MapDmaBuffers (mUsb3Instance, PciIo);
-        mUsb3Instance->InNotify = FALSE;
-        gBS->CloseEvent ((EFI_EVENT) (UINTN) mUsb3Instance->PciIoEvent);
-        break;
-      }
-    }
-
-    gBS->FreePool (PciIoHandleBuffer);
-  }
-}
-
-/**
-  Return USB3 debug instance address.
-
-**/  
-USB3_DEBUG_PORT_HANDLE *
-GetUsb3DebugPortInstance (
-  VOID
-  )
-{
-  USB3_DEBUG_PORT_HANDLE          *Instance;
-  EFI_PEI_HOB_POINTERS            Hob;
-
-  Instance = NULL;
-
-  if (mUsb3Instance != NULL) {
-    Instance = mUsb3Instance;
-    goto Done;
-  }
-
-  Hob.Raw = GetFirstGuidHob (&gUsb3DbgGuid);
-  if (Hob.Raw != NULL) {
-    Instance = GET_GUID_HOB_DATA (Hob.Guid);
-  }
-
-Done:
-  if (Instance != NULL) {
-    DiscoverInitializeUsbDebugPort (Instance);
-  }
-  return Instance;
-}
-
 /**
   Allocate aligned memory for XHC's usage.
 
@@ -374,113 +51,3 @@ AllocateAlignBuffer (
 
   return Buf;
 }
-
-/**
-  The constructor function initialize USB3 debug port.
-
-  @param  ImageHandle   The firmware allocated handle for the EFI image.
-  @param  SystemTable   A pointer to the EFI System Table.
-
-  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
-
-**/
-EFI_STATUS
-EFIAPI
-DebugCommunicationUsb3DxeConstructor (
-  IN EFI_HANDLE        ImageHandle,
-  IN EFI_SYSTEM_TABLE  *SystemTable
-  )
-{
-  USB3_DEBUG_PORT_HANDLE        UsbDbg;
-  USB3_DEBUG_PORT_HANDLE        *Instance;
-  EFI_PHYSICAL_ADDRESS          Address;
-  EFI_STATUS                    Status;
-  EFI_EVENT                     Event;
-
-  Instance = GetUsb3DebugPortInstance ();
-
-  Status = EfiGetSystemConfigurationTable (&gUsb3DbgGuid, (VOID **) &mUsb3Instance);
-  if (!EFI_ERROR (Status)) {
-    goto Done;
-  }
-
-  if (Instance == NULL) {
-    //
-    // Initialize USB debug
-    //
-    ZeroMem (&UsbDbg, sizeof (UsbDbg));
-    UsbDbg.Initialized = USB3DBG_UNINITIALIZED;
-
-    DiscoverInitializeUsbDebugPort (&UsbDbg);
-
-    Instance = &UsbDbg;
-  }
-
-  //
-  // It is first time to run DXE instance, copy Instance from Hob to ACPINvs.
-  //
-  Address = SIZE_4GB;
-  Status = gBS->AllocatePages (
-                  AllocateMaxAddress,
-                  EfiACPIMemoryNVS,
-                  EFI_SIZE_TO_PAGES (sizeof (USB3_DEBUG_PORT_HANDLE)),
-                  &Address
-                  );
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  CopyMem (
-    (VOID *)(UINTN)Address,
-    Instance,
-    sizeof (USB3_DEBUG_PORT_HANDLE)
-    );
-  mUsb3Instance = (USB3_DEBUG_PORT_HANDLE *)(UINTN)Address;
-
-  Status = gBS->InstallConfigurationTable (&gUsb3DbgGuid, mUsb3Instance);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-Done:
-  if (mUsb3Instance->Ready && (mUsb3Instance->PciIoEvent == 0)) {
-    Status = Usb3NamedEventListen (
-               &gEfiPciIoProtocolGuid,
-               TPL_NOTIFY,
-               Usb3PciIoNotify,
-               &Event
-               );
-    if (!EFI_ERROR (Status)) {
-      mUsb3Instance->PciIoEvent = (EFI_PHYSICAL_ADDRESS) (UINTN) Event;
-    }
-  }
-
-  return EFI_SUCCESS;
-}
-
-/**
-  The destructor function.
-
-  @param  ImageHandle   The firmware allocated handle for the EFI image.
-  @param  SystemTable   A pointer to the EFI System Table.
-
-  @retval EFI_SUCCESS   The destructor always returns EFI_SUCCESS.
-
-**/
-EFI_STATUS
-EFIAPI
-DebugCommunicationUsb3DxeDestructor (
-  IN EFI_HANDLE        ImageHandle,
-  IN EFI_SYSTEM_TABLE  *SystemTable
-  )
-{
-  if ((mUsb3Instance != NULL) && (mUsb3Instance->PciIoEvent != 0)) {
-    //
-    // Close the event created.
-    //
-    gBS->CloseEvent ((EFI_EVENT) (UINTN) mUsb3Instance->PciIoEvent);
-    mUsb3Instance->PciIoEvent = 0;
-  }
-  return EFI_SUCCESS;
-}
-
diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.inf b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.inf
index c4e4282c98b4..08b9ec12d298 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.inf
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Dxe.inf
@@ -1,7 +1,7 @@
 ## @file
 #  Debug Communication Library instance based on usb3 debug port.
 #
-#  Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>
 #
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD License
@@ -21,8 +21,6 @@ [Defines]
   MODULE_TYPE                    = DXE_DRIVER
   VERSION_STRING                 = 1.0
   LIBRARY_CLASS                  = DebugCommunicationLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER SMM_CORE
-  CONSTRUCTOR                    = DebugCommunicationUsb3DxeConstructor
-  DESTRUCTOR                     = DebugCommunicationUsb3DxeDestructor
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -56,11 +54,7 @@ [Pcd]
 
   # The value of data buffer size used for USB debug port handle.
   # It should be equal to sizeof (USB3_DEBUG_PORT_HANDLE).
-  gEfiSourceLevelDebugPkgTokenSpaceGuid.PcdDebugPortHandleBufferSize|250   ## SOMETIMES_CONSUMES
-
-[Protocols]
-  gEfiPciIoProtocolGuid                 ## CONSUMES
-  gEdkiiIoMmuProtocolGuid               ## CONSUMES
+  gEfiSourceLevelDebugPkgTokenSpaceGuid.PcdDebugPortHandleBufferSize|239   ## SOMETIMES_CONSUMES
 
 [LibraryClasses]
   BaseLib
@@ -71,4 +65,4 @@ [LibraryClasses]
   UefiBootServicesTableLib
   UefiLib
   BaseMemoryLib
-  HobLib
+  
\ No newline at end of file
diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Internal.h b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Internal.h
index 66757dafaebe..356485c5f697 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Internal.h
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Internal.h
@@ -1,7 +1,7 @@
 /** @file
   Debug Port Library implementation based on usb3 debug port.
 
-  Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD License
   which accompanies this distribution.  The full text of the license may be found at
@@ -46,7 +46,6 @@
 #define USB3DBG_DBG_CAB       1   // The XHCI host controller supports debug capability
 #define USB3DBG_ENABLED       2   // The XHCI debug device is enabled
 #define USB3DBG_NOT_ENABLED   4   // The XHCI debug device is not enabled
-#define USB3DBG_UNINITIALIZED 255 // The XHCI debug device is uninitialized
 
 #define USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE 0x08
 
@@ -457,7 +456,7 @@ typedef struct _USB3_DEBUG_PORT_INSTANCE {
   UINT8                                   Initialized;
 
   //
-  // The flag indicates debug capability is supported
+  // The flag indicates debug device is ready
   //
   BOOLEAN                                 DebugSupport;
   
@@ -467,26 +466,6 @@ typedef struct _USB3_DEBUG_PORT_INSTANCE {
   BOOLEAN                                 Ready;
 
   //
-  // The flag indicates the instance is from HOB
-  //
-  BOOLEAN                                 FromHob;
-
-  //
-  // IOMMU PPI Notify registered
-  //
-  BOOLEAN                                 PpiNotifyRegistered;
-
-  //
-  // Prevent notification being interrupted by debug timer
-  //
-  BOOLEAN                                 InNotify;
-
-  //
-  // PciIo protocol event
-  //
-  EFI_PHYSICAL_ADDRESS                    PciIoEvent;
-
-  //
   // The flag indicates if USB 3.0 ports has been turn off/on power
   //  
   BOOLEAN                                 ChangePortPower;
@@ -749,39 +728,4 @@ XhcDataTransfer (
   IN     UINTN                               Timeout
   );
 
-/**
-  Initialize usb debug port hardware.
-
-  @param  Handle           Debug port handle.
-
-  @retval TRUE             The usb debug port hardware configuration is changed.
-  @retval FALSE            The usb debug port hardware configuration is not changed.
-
-**/
-RETURN_STATUS
-EFIAPI
-InitializeUsbDebugHardware (
-  IN USB3_DEBUG_PORT_HANDLE *Handle
-  );
-
-/**
-  Discover and initialize usb debug port.
-
-  @param Handle                  Debug port handle.
-
-**/
-VOID
-DiscoverInitializeUsbDebugPort (
-  IN USB3_DEBUG_PORT_HANDLE     *Handle
-  );
-
-/**
-  Return USB3 debug instance address.
-
-**/  
-USB3_DEBUG_PORT_HANDLE *
-GetUsb3DebugPortInstance (
-  VOID
-  );
-
 #endif //__SERIAL_PORT_LIB_USB__
diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.c
index 40e9b6406b2e..902a3b626acc 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.c
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.c
@@ -1,7 +1,7 @@
 /** @file
   Debug Port Library implementation based on usb3 debug port.
 
-  Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD License
   which accompanies this distribution.  The full text of the license may be found at
@@ -14,215 +14,13 @@
 
 #include <PiPei.h>
 #include <Library/PeiServicesLib.h>
-#include <Library/HobLib.h>
 #include <Ppi/MemoryDiscovered.h>
-#include <Ppi/IoMmu.h>
 #include "DebugCommunicationLibUsb3Internal.h"
 
-GUID                    gUsb3DbgGuid = USB3_DBG_GUID;
-
-/**
-  USB3 IOMMU PPI notify.
-
-  @param[in] PeiServices    Pointer to PEI Services Table.
-  @param[in] NotifyDesc     Pointer to the descriptor for the Notification event that
-                            caused this function to execute.
-  @param[in] Ppi            Pointer to the PPI data associated with this function.
-
-  @retval EFI_STATUS        Always return EFI_SUCCESS
-**/
-EFI_STATUS
-EFIAPI
-Usb3IoMmuPpiNotify (
-  IN EFI_PEI_SERVICES           **PeiServices,
-  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDesc,
-  IN VOID                       *Ppi
-  )
-{
-  USB3_DEBUG_PORT_HANDLE        *Instance;
-
-  DEBUG ((DEBUG_INFO, "%a()\n", __FUNCTION__));
-
-  Instance = GetUsb3DebugPortInstance ();
-  ASSERT (Instance != NULL);
-
-  Instance->InNotify = TRUE;
-
-  //
-  // Reinitialize USB3 debug port with granted DMA buffer from IOMMU PPI.
-  //
-  InitializeUsbDebugHardware (Instance);
-
-  //
-  // Wait some time for host to be ready after re-initialization.
-  //
-  MicroSecondDelay (1000000);
-
-  Instance->InNotify = FALSE;
-
-  return EFI_SUCCESS;
-}
-
-EFI_PEI_NOTIFY_DESCRIPTOR mUsb3IoMmuPpiNotifyDesc = {
-  (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
-  &gEdkiiIoMmuPpiGuid,
-  Usb3IoMmuPpiNotify
-};
-
-/**
-  Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
-  OperationBusMasterCommonBuffer64 mapping.
-
-  @param IoMmu                  Pointer to IOMMU PPI.
-  @param Pages                  The number of pages to allocate.
-  @param HostAddress            A pointer to store the base system memory address of the
-                                allocated range.
-  @param DeviceAddress          The resulting map address for the bus master PCI controller to use to
-                                access the hosts HostAddress.
-  @param Mapping                A resulting value to pass to Unmap().
-
-  @retval EFI_SUCCESS           The requested memory pages were allocated.
-  @retval EFI_UNSUPPORTED       Attributes is unsupported. The only legal attribute bits are
-                                MEMORY_WRITE_COMBINE and MEMORY_CACHED.
-  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
-  @retval EFI_OUT_OF_RESOURCES  The memory pages could not be allocated.
-
-**/
-EFI_STATUS
-IoMmuAllocateBuffer (
-  IN EDKII_IOMMU_PPI        *IoMmu,
-  IN UINTN                  Pages,
-  OUT VOID                  **HostAddress,
-  OUT EFI_PHYSICAL_ADDRESS  *DeviceAddress,
-  OUT VOID                  **Mapping
-  )
-{
-  EFI_STATUS            Status;
-  UINTN                 NumberOfBytes;
-
-  *HostAddress = NULL;
-  *DeviceAddress = 0;
-  *Mapping = NULL;
-
-  Status = IoMmu->AllocateBuffer (
-                    IoMmu,
-                    EfiBootServicesData,
-                    Pages,
-                    HostAddress,
-                    0
-                    );
-  if (EFI_ERROR (Status)) {
-    return EFI_OUT_OF_RESOURCES;
-  }
-
-  NumberOfBytes = EFI_PAGES_TO_SIZE (Pages);
-  Status = IoMmu->Map (
-                    IoMmu,
-                    EdkiiIoMmuOperationBusMasterCommonBuffer,
-                    *HostAddress,
-                    &NumberOfBytes,
-                    DeviceAddress,
-                    Mapping
-                    );
-  if (EFI_ERROR (Status)) {
-    IoMmu->FreeBuffer (IoMmu, Pages, *HostAddress);
-    *HostAddress = NULL;
-    return EFI_OUT_OF_RESOURCES;
-  }
-  Status = IoMmu->SetAttribute (
-                    IoMmu,
-                    *Mapping,
-                    EDKII_IOMMU_ACCESS_READ | EDKII_IOMMU_ACCESS_WRITE
-                    );
-  if (EFI_ERROR (Status)) {
-    IoMmu->Unmap (IoMmu, *Mapping);
-    IoMmu->FreeBuffer (IoMmu, Pages, *HostAddress);
-    *Mapping = NULL;
-    *HostAddress = NULL;
-    return Status;
-  }
-
-  return Status;
-}
-
-/**
-  USB3 get IOMMU PPI.
-
-  @return Pointer to IOMMU PPI.
-
-**/
-EDKII_IOMMU_PPI *
-Usb3GetIoMmu (
-  VOID
-  )
-{
-  EFI_STATUS                Status;
-  EDKII_IOMMU_PPI           *IoMmu;
-
-  IoMmu = NULL;
-  Status = PeiServicesLocatePpi (
-             &gEdkiiIoMmuPpiGuid,
-             0,
-             NULL,
-             (VOID **) &IoMmu
-             );
-  if (!EFI_ERROR (Status) && (IoMmu != NULL)) {
-    return IoMmu;
-  }
-
-  return NULL;
-}
-
-/**
-  Return USB3 debug instance address.
-
-**/  
-USB3_DEBUG_PORT_HANDLE *
-GetUsb3DebugPortInstance (
-  VOID
-  )
-{
-  USB3_DEBUG_PORT_HANDLE        *Instance;
-  EFI_PEI_HOB_POINTERS          Hob;
-  EFI_STATUS                    Status;
-
-  Hob.Raw = GetFirstGuidHob (&gUsb3DbgGuid);
-  if (Hob.Raw == NULL) {
-    //
-    // Save Instance into HOB
-    //
-    Instance = BuildGuidHob (
-                 &gUsb3DbgGuid,
-                 sizeof (USB3_DEBUG_PORT_HANDLE)
-                 );
-    ASSERT (Instance != NULL);
-    ZeroMem (Instance, sizeof (USB3_DEBUG_PORT_HANDLE));
-
-    Instance->FromHob = TRUE;
-    Instance->Initialized = USB3DBG_UNINITIALIZED;
-  } else {
-    Instance = GET_GUID_HOB_DATA (Hob.Guid);
-  }
-
-  if (!Instance->InNotify) {
-    DiscoverInitializeUsbDebugPort (Instance);
-  }
-
-  if (Instance->Ready &&
-      !Instance->PpiNotifyRegistered &&
-      (Usb3GetIoMmu () == NULL)) {
-    Status = PeiServicesNotifyPpi (&mUsb3IoMmuPpiNotifyDesc);
-    ASSERT_EFI_ERROR (Status);
-    Instance->PpiNotifyRegistered = TRUE;
-  }
-
-  return Instance;
-}
-
 /**
   Allocate aligned memory for XHC's usage.
 
-  @param BufferSize     The size, in bytes, of the Buffer.
+  @param  BufferSize      The size, in bytes, of the Buffer.
   
   @return A pointer to the allocated buffer or NULL if allocation fails.
 
@@ -236,9 +34,6 @@ AllocateAlignBuffer (
   EFI_PHYSICAL_ADDRESS     Address;
   EFI_STATUS               Status;
   VOID                     *MemoryDiscoveredPpi;
-  EDKII_IOMMU_PPI          *IoMmu;
-  VOID                     *HostAddress;
-  VOID                     *Mapping;
 
   Buf = NULL;
 
@@ -252,28 +47,9 @@ AllocateAlignBuffer (
              (VOID **) &MemoryDiscoveredPpi
              );
   if (!EFI_ERROR (Status)) {
-    IoMmu = Usb3GetIoMmu ();
-    if (IoMmu != NULL) {
-      Status = IoMmuAllocateBuffer (
-                 IoMmu,
-                 EFI_SIZE_TO_PAGES (BufferSize),
-                 &HostAddress,
-                 &Address,
-                 &Mapping
-                 );
-      if (!EFI_ERROR (Status)) {
-        ASSERT (Address == ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress));
-        Buf = (VOID *)(UINTN) Address;
-      }
-    } else {
-      Status = PeiServicesAllocatePages (
-                 EfiACPIMemoryNVS,
-                 EFI_SIZE_TO_PAGES (BufferSize),
-                 &Address
-                 );
-      if (!EFI_ERROR (Status)) {
-        Buf = (VOID *)(UINTN) Address;
-      }
+    Status = PeiServicesAllocatePages (EfiACPIMemoryNVS, EFI_SIZE_TO_PAGES (BufferSize), &Address);
+    if (!EFI_ERROR (Status)) {
+      Buf = (VOID *)(UINTN) Address;
     }
   }
   return Buf;
diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.inf b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.inf
index 33074db49a78..2108face50d2 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.inf
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Pei.inf
@@ -1,7 +1,7 @@
 ## @file
 #  Debug Communication Library instance based on usb3 debug port.
 #
-#  Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>
 #
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD License
@@ -41,7 +41,6 @@ [Packages]
 
 [Ppis]
   gEfiPeiMemoryDiscoveredPpiGuid                ## CONSUMES
-  gEdkiiIoMmuPpiGuid                            ## CONSUMES
 
 [Pcd]
   # The memory BAR of ehci host controller, in which usb debug feature is enabled.
@@ -58,7 +57,7 @@ [Pcd]
 
   # The value of data buffer size used for USB debug port handle.
   # It should be equal to sizeof (USB3_DEBUG_PORT_HANDLE).
-  gEfiSourceLevelDebugPkgTokenSpaceGuid.PcdDebugPortHandleBufferSize|250   ## SOMETIMES_CONSUMES
+  gEfiSourceLevelDebugPkgTokenSpaceGuid.PcdDebugPortHandleBufferSize|239   ## SOMETIMES_CONSUMES
 
 [LibraryClasses]
   BaseLib
@@ -68,7 +67,6 @@ [LibraryClasses]
   TimerLib
   BaseMemoryLib
   PeiServicesLib
-  HobLib
-
+  
 [Depex.common.PEIM]
   gEfiPeiMemoryDiscoveredPpiGuid
-- 
2.7.0.windows.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH V2 4/6] SourceLevelDebugPkg DebugUsb3: Re-Fix GCC build failures
  2018-03-14  9:33 [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
                   ` (2 preceding siblings ...)
  2018-03-14  9:33 ` [PATCH V2 3/6] Revert "DebugUsb3: Support IOMMU" Star Zeng
@ 2018-03-14  9:33 ` Star Zeng
  2018-03-14  9:33 ` [PATCH V2 5/6] SourceLevelDebugPkg DebugCommUsb3: Refine some formats/comments Star Zeng
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Star Zeng @ 2018-03-14  9:33 UTC (permalink / raw)
  To: edk2-devel; +Cc: Star Zeng, Ruiyu Ni, Hao Wu

Fix GCC build failures below.
variable 'EvtTrb' set but not used [-Werror=unused-but-set-variable]
variable 'Index' set but not used [-Werror=unused-but-set-variable]

The build failure could only be caught with -D SOURCE_DEBUG_USE_USB3
build flag.

ad6040ec9b5bbc462762331f9738b8e42c0b9c80 needs to be also reverted
when reverting IOMMU support patches, otherwise there will be conflict.
This patch is to re-do ad6040ec9b5bbc462762331f9738b8e42c0b9c80.

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>
---
 .../DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c       | 2 --
 .../DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c     | 4 +---
 2 files changed, 1 insertion(+), 5 deletions(-)

diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
index 49bad6b5864d..3b0f81cf88fd 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
@@ -852,7 +852,6 @@ DebugPortWriteBuffer (
   UINTN                     Sent;
   UINTN                     Total;
   EFI_PHYSICAL_ADDRESS      XhciMmioBase;
-  UINTN                     Index;
 
   if (NumberOfBytes == 0 || Buffer == NULL) {
     return 0;
@@ -895,7 +894,6 @@ DebugPortWriteBuffer (
   //
   DebugPortPollBuffer (Handle);
 
-  Index = 0;
   while ((Total < NumberOfBytes)) {
     if (NumberOfBytes - Total > USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE) {
       Sent = USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE;
diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c
index dbff49362407..1b6645bd1c6d 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Transfer.c
@@ -1,7 +1,7 @@
 /** @file
   Debug Port Library implementation based on usb3 debug port.
 
-  Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD License
   which accompanies this distribution.  The full text of the license may be found at
@@ -145,11 +145,9 @@ XhcCheckNewEvent (
   )
 {
   EFI_STATUS          Status;
-  TRB_TEMPLATE        *EvtTrb;
 
   ASSERT (EvtRing != NULL);
 
-  EvtTrb     = (TRB_TEMPLATE *)(UINTN) EvtRing->EventRingDequeue;
   *NewEvtTrb = (TRB_TEMPLATE *)(UINTN) EvtRing->EventRingDequeue;
 
   if (EvtRing->EventRingDequeue == EvtRing->EventRingEnqueue) {
-- 
2.7.0.windows.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH V2 5/6] SourceLevelDebugPkg DebugCommUsb3: Refine some formats/comments
  2018-03-14  9:33 [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
                   ` (3 preceding siblings ...)
  2018-03-14  9:33 ` [PATCH V2 4/6] SourceLevelDebugPkg DebugUsb3: Re-Fix GCC build failures Star Zeng
@ 2018-03-14  9:33 ` Star Zeng
  2018-03-14  9:33 ` [PATCH V2 6/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Star Zeng @ 2018-03-14  9:33 UTC (permalink / raw)
  To: edk2-devel; +Cc: Star Zeng, Ruiyu Ni, Hao Wu

Refine some formats/comments and remove some unused prototypes.

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>
---
 .../DebugCommunicationLibUsb3Common.c              | 16 ++++----
 .../DebugCommunicationLibUsb3Internal.h            | 47 +++-------------------
 2 files changed, 13 insertions(+), 50 deletions(-)

diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
index 3b0f81cf88fd..37f49ad822f5 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
@@ -85,15 +85,15 @@ XhcClearR32Bit(
   Write the data to the XHCI debug register.
 
   @param  Handle       Debug port handle.
-  @param  Offset       The offset of the runtime register.
+  @param  Offset       The offset of the debug register.
   @param  Data         The data to write.
 
 **/
 VOID
 XhcWriteDebugReg (
   IN USB3_DEBUG_PORT_HANDLE  *Handle,
-  IN UINT32                   Offset,
-  IN UINT32                   Data
+  IN UINT32                  Offset,
+  IN UINT32                  Data
   )
 {
   EFI_PHYSICAL_ADDRESS  DebugCapabilityBase;
@@ -116,7 +116,7 @@ XhcWriteDebugReg (
 UINT32
 XhcReadDebugReg (
   IN  USB3_DEBUG_PORT_HANDLE *Handle,
-  IN  UINT32                   Offset
+  IN  UINT32                 Offset
   )
 {
   UINT32                  Data;
@@ -129,16 +129,16 @@ XhcReadDebugReg (
 }
 
 /**
-  Set one bit of the runtime register while keeping other bits.
+  Set one bit of the debug register while keeping other bits.
 
   @param  Handle       Debug port handle.
-  @param  Offset       The offset of the runtime register.
+  @param  Offset       The offset of the debug register.
   @param  Bit          The bit mask of the register to set.
 
 **/
 VOID
 XhcSetDebugRegBit (
-  IN USB3_DEBUG_PORT_HANDLE *Handle,
+  IN USB3_DEBUG_PORT_HANDLE   *Handle,
   IN UINT32                   Offset,
   IN UINT32                   Bit
   )
@@ -216,7 +216,7 @@ UpdateXhcResource (
 
   @param  Handle             Debug port handle.
 
-  @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.
+  @retval RETURN_UNSUPPORTED The usb host controller does not support usb debug port capability.
   @retval RETURN_SUCCESS     Get bar and offset successfully.
 
 **/
diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Internal.h b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Internal.h
index 356485c5f697..9da49f964401 100644
--- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Internal.h
+++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Internal.h
@@ -1,7 +1,7 @@
 /** @file
   Debug Port Library implementation based on usb3 debug port.
 
-  Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD License
   which accompanies this distribution.  The full text of the license may be found at
@@ -540,7 +540,7 @@ typedef struct _USB3_DEBUG_PORT_INSTANCE {
 UINT32
 XhcReadDebugReg (
   IN  USB3_DEBUG_PORT_HANDLE    *Handle,
-  IN  UINT32                      Offset
+  IN  UINT32                    Offset
   );
 
 /**
@@ -554,8 +554,8 @@ XhcReadDebugReg (
 VOID
 XhcSetDebugRegBit (
   IN USB3_DEBUG_PORT_HANDLE  *Handle,
-  IN UINT32                   Offset,
-  IN UINT32                   Bit
+  IN UINT32                  Offset,
+  IN UINT32                  Bit
   );
   
 /**
@@ -574,43 +574,6 @@ XhcWriteDebugReg (
   );
 
 /**
-  Discover the USB3 debug device.
-  
-  @param  Handle                Debug port handle.
-  
-  @retval RETURN_SUCCESS        The serial device was initialized.
-  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
-
-**/
-RETURN_STATUS
-DiscoverUsb3DebugPort(
-  USB3_DEBUG_PORT_HANDLE  *Handle
-  );
-  
-/**
-  Initialize the Serial Device hardware.
-  
-  @param  Handle            Debug port handle.
-
-  @retval RETURN_SUCCESS    The serial device was initialized successfully.
-  @retval !RETURN_SUCCESS   Error.
-
-**/
-RETURN_STATUS
-InitializeUsb3DebugPort (
-  USB3_DEBUG_PORT_HANDLE  *Handle
-  );
-
-/**
-  Return XHCI MMIO base address.
-
-**/
-EFI_PHYSICAL_ADDRESS
-GetXhciBaseAddress (
-  VOID
-  );
-
-/**
   Verifies if the bit positions specified by a mask are set in a register.
 
   @param[in, out] Register    UNITN register
@@ -728,4 +691,4 @@ XhcDataTransfer (
   IN     UINTN                               Timeout
   );
 
-#endif //__SERIAL_PORT_LIB_USB__
+#endif //__USB3_DEBUG_PORT_LIB_INTERNAL__
-- 
2.7.0.windows.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH V2 6/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU
  2018-03-14  9:33 [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
                   ` (4 preceding siblings ...)
  2018-03-14  9:33 ` [PATCH V2 5/6] SourceLevelDebugPkg DebugCommUsb3: Refine some formats/comments Star Zeng
@ 2018-03-14  9:33 ` Star Zeng
  2018-03-14  9:47 ` [PATCH V2 0/6] " Ni, Ruiyu
  2018-03-14 11:35 ` Wu, Hao A
  7 siblings, 0 replies; 9+ messages in thread
From: Star Zeng @ 2018-03-14  9:33 UTC (permalink / raw)
  To: edk2-devel; +Cc: Star Zeng, Ruiyu Ni, Hao Wu

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>
---
 .../DebugCommunicationLibUsb3Common.c              | 256 +++++++----
 .../DebugCommunicationLibUsb3Dxe.c                 | 500 ++++++++++++++++++++-
 .../DebugCommunicationLibUsb3Dxe.inf               |  17 +-
 .../DebugCommunicationLibUsb3Internal.h            |  53 ++-
 .../DebugCommunicationLibUsb3Pei.c                 | 232 +++++++++-
 .../DebugCommunicationLibUsb3Pei.inf               |   8 +-
 6 files changed, 954 insertions(+), 112 deletions(-)

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



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU
  2018-03-14  9:33 [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
                   ` (5 preceding siblings ...)
  2018-03-14  9:33 ` [PATCH V2 6/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
@ 2018-03-14  9:47 ` Ni, Ruiyu
  2018-03-14 11:35 ` Wu, Hao A
  7 siblings, 0 replies; 9+ messages in thread
From: Ni, Ruiyu @ 2018-03-14  9:47 UTC (permalink / raw)
  To: Star Zeng, edk2-devel; +Cc: Hao Wu

On 3/14/2018 5:33 PM, Star Zeng wrote:
> The patch series is also at
> https://github.com/lzeng14/edk2 DebugCommUsb3AfterIOMMUV2 branch.
> 
> Based on the feedbacks from Ray and Hao.
> It is V2 of
> https://lists.01.org/pipermail/edk2-devel/2018-March/022586.html.
> It has no essential difference with V1 about the final code, but
> re-arranges the patches to revert old IOMMU support patches and
> then re-support IOMMU.
> 
> 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>
> 
> Star Zeng (6):
>    Revert "DebugUsb3: Check mUsb3Instance before dereferencing it"
>    Revert "DebugUsb3: Fix GCC build failures"
>    Revert "DebugUsb3: Support IOMMU"
>    SourceLevelDebugPkg DebugUsb3: Re-Fix GCC build failures
>    SourceLevelDebugPkg DebugCommUsb3: Refine some formats/comments
>    SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU
> 
>   .../DebugCommunicationLibUsb3Common.c              | 110 +++++-
>   .../DebugCommunicationLibUsb3Dxe.c                 | 375 ++++++++++++---------
>   .../DebugCommunicationLibUsb3Dxe.inf               |  11 +-
>   .../DebugCommunicationLibUsb3Internal.h            |  60 +---
>   .../DebugCommunicationLibUsb3Pei.c                 |  50 ++-
>   .../DebugCommunicationLibUsb3Pei.inf               |   4 +-
>   6 files changed, 351 insertions(+), 259 deletions(-)
> 
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>

-- 
Thanks,
Ray


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU
  2018-03-14  9:33 [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
                   ` (6 preceding siblings ...)
  2018-03-14  9:47 ` [PATCH V2 0/6] " Ni, Ruiyu
@ 2018-03-14 11:35 ` Wu, Hao A
  7 siblings, 0 replies; 9+ messages in thread
From: Wu, Hao A @ 2018-03-14 11:35 UTC (permalink / raw)
  To: Zeng, Star, edk2-devel@lists.01.org; +Cc: Ni, Ruiyu, Zeng, Star

The series is good to me.
Reviewed-by: Hao Wu <hao.a.wu@intel.com>


Best Regards,
Hao Wu


> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Star
> Zeng
> Sent: Wednesday, March 14, 2018 5:34 PM
> To: edk2-devel@lists.01.org
> Cc: Ni, Ruiyu; Wu, Hao A; Zeng, Star
> Subject: [edk2] [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support
> IOMMU
> 
> The patch series is also at
> https://github.com/lzeng14/edk2 DebugCommUsb3AfterIOMMUV2 branch.
> 
> Based on the feedbacks from Ray and Hao.
> It is V2 of
> https://lists.01.org/pipermail/edk2-devel/2018-March/022586.html.
> It has no essential difference with V1 about the final code, but
> re-arranges the patches to revert old IOMMU support patches and
> then re-support IOMMU.
> 
> 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>
> 
> Star Zeng (6):
>   Revert "DebugUsb3: Check mUsb3Instance before dereferencing it"
>   Revert "DebugUsb3: Fix GCC build failures"
>   Revert "DebugUsb3: Support IOMMU"
>   SourceLevelDebugPkg DebugUsb3: Re-Fix GCC build failures
>   SourceLevelDebugPkg DebugCommUsb3: Refine some formats/comments
>   SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU
> 
>  .../DebugCommunicationLibUsb3Common.c              | 110 +++++-
>  .../DebugCommunicationLibUsb3Dxe.c                 | 375 ++++++++++++---------
>  .../DebugCommunicationLibUsb3Dxe.inf               |  11 +-
>  .../DebugCommunicationLibUsb3Internal.h            |  60 +---
>  .../DebugCommunicationLibUsb3Pei.c                 |  50 ++-
>  .../DebugCommunicationLibUsb3Pei.inf               |   4 +-
>  6 files changed, 351 insertions(+), 259 deletions(-)
> 
> --
> 2.7.0.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2018-03-14 11:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-14  9:33 [PATCH V2 0/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
2018-03-14  9:33 ` [PATCH V2 1/6] Revert "DebugUsb3: Check mUsb3Instance before dereferencing it" Star Zeng
2018-03-14  9:33 ` [PATCH V2 2/6] Revert "DebugUsb3: Fix GCC build failures" Star Zeng
2018-03-14  9:33 ` [PATCH V2 3/6] Revert "DebugUsb3: Support IOMMU" Star Zeng
2018-03-14  9:33 ` [PATCH V2 4/6] SourceLevelDebugPkg DebugUsb3: Re-Fix GCC build failures Star Zeng
2018-03-14  9:33 ` [PATCH V2 5/6] SourceLevelDebugPkg DebugCommUsb3: Refine some formats/comments Star Zeng
2018-03-14  9:33 ` [PATCH V2 6/6] SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU Star Zeng
2018-03-14  9:47 ` [PATCH V2 0/6] " Ni, Ruiyu
2018-03-14 11:35 ` Wu, Hao A

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox