]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
SourceLevelDebugPkg DebugUsb3: Re-Support IOMMU
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugCommunicationLibUsb3 / DebugCommunicationLibUsb3Common.c
index c577df7dea97e09947f6b562d85e0244a66315bc..fb9ca84fc7bc09d649b0ebf896521e920df49ac2 100644 (file)
@@ -87,8 +87,8 @@ XhcClearR32Bit(
 VOID\r
 XhcWriteDebugReg (\r
   IN USB3_DEBUG_PORT_HANDLE  *Handle,\r
-  IN UINT32                   Offset,\r
-  IN UINT32                   Data\r
+  IN UINT32                  Offset,\r
+  IN UINT32                  Data\r
   )\r
 {\r
   EFI_PHYSICAL_ADDRESS  DebugCapabilityBase;\r
@@ -111,7 +111,7 @@ XhcWriteDebugReg (
 UINT32\r
 XhcReadDebugReg (\r
   IN  USB3_DEBUG_PORT_HANDLE *Handle,\r
-  IN  UINT32                   Offset\r
+  IN  UINT32                 Offset\r
   )\r
 {\r
   UINT32                  Data;\r
@@ -810,6 +810,44 @@ DiscoverInitializeUsbDebugPort (
   }\r
 }\r
 \r
+/**\r
+  Set USB3 debug instance address.\r
+\r
+  @param[in] Instance           Debug port instance.\r
+\r
+**/  \r
+VOID\r
+SetUsb3DebugPortInstance (\r
+  IN USB3_DEBUG_PORT_HANDLE     *Instance\r
+  )\r
+{\r
+  EFI_PHYSICAL_ADDRESS          *AddrPtr;\r
+\r
+  AddrPtr = GetUsb3DebugPortInstanceAddrPtr ();\r
+  ASSERT (AddrPtr != NULL);\r
+  *AddrPtr = (EFI_PHYSICAL_ADDRESS) (UINTN) Instance;\r
+}\r
+\r
+/**\r
+  Return USB3 debug instance address.\r
+\r
+**/  \r
+USB3_DEBUG_PORT_HANDLE *\r
+GetUsb3DebugPortInstance (\r
+  VOID\r
+  )\r
+{\r
+  EFI_PHYSICAL_ADDRESS          *AddrPtr;\r
+  USB3_DEBUG_PORT_HANDLE        *Instance;\r
+\r
+  AddrPtr = GetUsb3DebugPortInstanceAddrPtr ();\r
+  ASSERT (AddrPtr != NULL);\r
+\r
+  Instance = (USB3_DEBUG_PORT_HANDLE *) (UINTN) *AddrPtr;\r
+\r
+  return Instance;\r
+}\r
+\r
 /**\r
   Read data from debug device and save the data in buffer.\r
 \r
@@ -844,16 +882,27 @@ DebugPortReadBuffer (
     return 0;\r
   }\r
 \r
-  UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  //\r
+  // If Handle is NULL, get own instance.\r
+  // If Handle is not NULL, use it and set the instance.\r
+  //\r
+  if (Handle != NULL) {\r
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *) Handle;\r
+    SetUsb3DebugPortInstance (UsbDebugPortHandle);\r
+  } else {\r
+    UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  }\r
   if (UsbDebugPortHandle == NULL) {\r
     return 0;\r
   }\r
 \r
-  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
+  if (UsbDebugPortHandle->InNotify) {\r
     return 0;\r
   }\r
 \r
-  if (UsbDebugPortHandle->InNotify) {\r
+  DiscoverInitializeUsbDebugPort (UsbDebugPortHandle);\r
+\r
+  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
     return 0;\r
   }\r
 \r
@@ -914,16 +963,27 @@ DebugPortWriteBuffer (
   Sent  = 0;\r
   Total = 0;\r
 \r
-  UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  //\r
+  // If Handle is NULL, get own instance.\r
+  // If Handle is not NULL, use it and set the instance.\r
+  //\r
+  if (Handle != NULL) {\r
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *) Handle;\r
+    SetUsb3DebugPortInstance (UsbDebugPortHandle);\r
+  } else {\r
+    UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  }\r
   if (UsbDebugPortHandle == NULL) {\r
     return 0;\r
   }\r
 \r
-  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
+  if (UsbDebugPortHandle->InNotify) {\r
     return 0;\r
   }\r
 \r
-  if (UsbDebugPortHandle->InNotify) {\r
+  DiscoverInitializeUsbDebugPort (UsbDebugPortHandle);\r
+\r
+  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
     return 0;\r
   }\r
 \r
@@ -968,16 +1028,27 @@ DebugPortPollBuffer (
   USB3_DEBUG_PORT_HANDLE     *UsbDebugPortHandle;\r
   UINTN                     Length;\r
 \r
-  UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  //\r
+  // If Handle is NULL, get own instance.\r
+  // If Handle is not NULL, use it and set the instance.\r
+  //\r
+  if (Handle != NULL) {\r
+    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *) Handle;\r
+    SetUsb3DebugPortInstance (UsbDebugPortHandle);\r
+  } else {\r
+    UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  }\r
   if (UsbDebugPortHandle == NULL) {\r
     return FALSE;\r
   }\r
 \r
-  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
+  if (UsbDebugPortHandle->InNotify) {\r
     return FALSE;\r
   }\r
 \r
-  if (UsbDebugPortHandle->InNotify) {\r
+  DiscoverInitializeUsbDebugPort (UsbDebugPortHandle);\r
+\r
+  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
     return FALSE;\r
   }\r
 \r
@@ -1043,14 +1114,25 @@ DebugPortInitialize (
 {\r
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;\r
 \r
+  //\r
+  // Validate the PCD PcdDebugPortHandleBufferSize value \r
+  //\r
+  ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize) == sizeof (USB3_DEBUG_PORT_HANDLE));\r
+\r
+  if (Function == NULL && Context != NULL) {\r
+    SetUsb3DebugPortInstance ((USB3_DEBUG_PORT_HANDLE *) Context);\r
+    return (DEBUG_PORT_HANDLE) Context;\r
+  }\r
   UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
   if (UsbDebugPortHandle == NULL) {\r
     return NULL;\r
   }\r
 \r
+  DiscoverInitializeUsbDebugPort (UsbDebugPortHandle);\r
+\r
   if (Function != NULL) {\r
-    Function (Context, UsbDebugPortHandle);\r
+    Function (Context, (DEBUG_PORT_HANDLE) UsbDebugPortHandle);\r
   }\r
 \r
-  return (DEBUG_PORT_HANDLE)(UINTN)UsbDebugPortHandle;\r
+  return (DEBUG_PORT_HANDLE) UsbDebugPortHandle;\r
 }\r