]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg XhciDxe: Use common buffer for AsyncInterruptTransfer
authorStar Zeng <star.zeng@intel.com>
Thu, 25 Oct 2018 08:20:27 +0000 (16:20 +0800)
committerStar Zeng <star.zeng@intel.com>
Sun, 28 Oct 2018 13:20:08 +0000 (21:20 +0800)
V3:
Call XhcFreeUrb after XhcCreateTransferTrb fails in XhcCreateTrb.

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1274

In current code, XhcMonitorAsyncRequests (timer handler) will do
unmap and map operations for AsyncIntTransfers to "Flush data from
PCI controller specific address to mapped system memory address".
XhcMonitorAsyncRequests
  XhcFlushAsyncIntMap
    PciIo->Unmap
      IoMmu->SetAttribute
    PciIo->Map
      IoMmu->SetAttribute

This may impact the boot performance.

Since the data buffer for XhcMonitorAsyncRequests is internal
buffer, we can allocate common buffer by PciIo->AllocateBuffer
and map the buffer with EfiPciIoOperationBusMasterCommonBuffer,
then the unmap and map operations can be removed.

///
/// Provides both read and write access to system memory by
/// both the processor and a bus master. The buffer is coherent
/// from both the processor's and the bus master's point of view.
///
EfiPciIoOperationBusMasterCommonBuffer,

Test done:
USB KB works normally.
USB disk read/write works normally.

Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Hao Wu <hao.a.wu@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Hao Wu <hao.a.wu@intel.com>
MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c
MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c
MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.h

index 7f64f9c7c982652e866f06fa4b4b2bbda7dcad48..64855a4c158c0716d6821076e207e2c7b67ebf47 100644 (file)
@@ -769,6 +769,7 @@ XhcTransfer (
           MaximumPacketLength,\r
           Type,\r
           Request,\r
+          FALSE,\r
           Data,\r
           *DataLength,\r
           NULL,\r
index 75959ae08363eab97b9411e7cfacc10289576d1a..9dd45e93a272e491f944bd28b1d7ff9e6630db18 100644 (file)
@@ -118,17 +118,18 @@ ON_EXIT:
 /**\r
   Create a new URB for a new transaction.\r
 \r
-  @param  Xhc       The XHCI Instance\r
-  @param  BusAddr   The logical device address assigned by UsbBus driver\r
-  @param  EpAddr    Endpoint addrress\r
-  @param  DevSpeed  The device speed\r
-  @param  MaxPacket The max packet length of the endpoint\r
-  @param  Type      The transaction type\r
-  @param  Request   The standard USB request for control transfer\r
-  @param  Data      The user data to transfer\r
-  @param  DataLen   The length of data buffer\r
-  @param  Callback  The function to call when data is transferred\r
-  @param  Context   The context to the callback\r
+  @param  Xhc                   The XHCI Instance\r
+  @param  BusAddr               The logical device address assigned by UsbBus driver\r
+  @param  EpAddr                Endpoint addrress\r
+  @param  DevSpeed              The device speed\r
+  @param  MaxPacket             The max packet length of the endpoint\r
+  @param  Type                  The transaction type\r
+  @param  Request               The standard USB request for control transfer\r
+  @param  AllocateCommonBuffer  Indicate whether need to allocate common buffer for data transfer\r
+  @param  Data                  The user data to transfer, NULL if AllocateCommonBuffer is TRUE\r
+  @param  DataLen               The length of data buffer\r
+  @param  Callback              The function to call when data is transferred\r
+  @param  Context               The context to the callback\r
 \r
   @return Created URB or NULL\r
 \r
@@ -142,6 +143,7 @@ XhcCreateUrb (
   IN UINTN                              MaxPacket,\r
   IN UINTN                              Type,\r
   IN EFI_USB_DEVICE_REQUEST             *Request,\r
+  IN BOOLEAN                            AllocateCommonBuffer,\r
   IN VOID                               *Data,\r
   IN UINTN                              DataLen,\r
   IN EFI_ASYNC_USB_TRANSFER_CALLBACK    Callback,\r
@@ -169,8 +171,24 @@ XhcCreateUrb (
   Ep->Type      = Type;\r
 \r
   Urb->Request  = Request;\r
+  if (AllocateCommonBuffer) {\r
+    ASSERT (Data == NULL);\r
+    Status = Xhc->PciIo->AllocateBuffer (\r
+                           Xhc->PciIo,\r
+                           AllocateAnyPages,\r
+                           EfiBootServicesData,\r
+                           EFI_SIZE_TO_PAGES (DataLen),\r
+                           &Data,\r
+                           0\r
+                           );\r
+    if (EFI_ERROR (Status) || (Data == NULL)) {\r
+      FreePool (Urb);\r
+      return NULL;\r
+    }\r
+  }\r
   Urb->Data     = Data;\r
   Urb->DataLen  = DataLen;\r
+  Urb->AllocateCommonBuffer = AllocateCommonBuffer;\r
   Urb->Callback = Callback;\r
   Urb->Context  = Context;\r
 \r
@@ -178,7 +196,7 @@ XhcCreateUrb (
   ASSERT_EFI_ERROR (Status);\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "XhcCreateUrb: XhcCreateTransferTrb Failed, Status = %r\n", Status));\r
-    FreePool (Urb);\r
+    XhcFreeUrb (Xhc, Urb);\r
     Urb = NULL;\r
   }\r
 \r
@@ -206,6 +224,14 @@ XhcFreeUrb (
     Xhc->PciIo->Unmap (Xhc->PciIo, Urb->DataMap);\r
   }\r
 \r
+  if (Urb->AllocateCommonBuffer) {\r
+    Xhc->PciIo->FreeBuffer (\r
+                  Xhc->PciIo,\r
+                  EFI_SIZE_TO_PAGES (Urb->DataLen),\r
+                  Urb->Data\r
+                  );\r
+  }\r
+\r
   FreePool (Urb);\r
 }\r
 \r
@@ -264,10 +290,14 @@ XhcCreateTransferTrb (
   // No need to remap.\r
   //\r
   if ((Urb->Data != NULL) && (Urb->DataMap == NULL)) {\r
-    if (((UINT8) (Urb->Ep.Direction)) == EfiUsbDataIn) {\r
-      MapOp = EfiPciIoOperationBusMasterWrite;\r
+    if (Urb->AllocateCommonBuffer) {\r
+      MapOp = EfiPciIoOperationBusMasterCommonBuffer;\r
     } else {\r
-      MapOp = EfiPciIoOperationBusMasterRead;\r
+      if (((UINT8) (Urb->Ep.Direction)) == EfiUsbDataIn) {\r
+        MapOp = EfiPciIoOperationBusMasterWrite;\r
+      } else {\r
+        MapOp = EfiPciIoOperationBusMasterRead;\r
+      }\r
     }\r
 \r
     Len = Urb->DataLen;\r
@@ -1367,7 +1397,6 @@ XhciDelAsyncIntTransfer (
       }\r
 \r
       RemoveEntryList (&Urb->UrbList);\r
-      FreePool (Urb->Data);\r
       XhcFreeUrb (Xhc, Urb);\r
       return EFI_SUCCESS;\r
     }\r
@@ -1405,7 +1434,6 @@ XhciDelAllAsyncIntTransfers (
     }\r
 \r
     RemoveEntryList (&Urb->UrbList);\r
-    FreePool (Urb->Data);\r
     XhcFreeUrb (Xhc, Urb);\r
   }\r
 }\r
@@ -1438,15 +1466,8 @@ XhciInsertAsyncIntTransfer (
   IN VOID                               *Context\r
   )\r
 {\r
-  VOID      *Data;\r
   URB       *Urb;\r
 \r
-  Data = AllocateZeroPool (DataLen);\r
-  if (Data == NULL) {\r
-    DEBUG ((DEBUG_ERROR, "%a: failed to allocate buffer\n", __FUNCTION__));\r
-    return NULL;\r
-  }\r
-\r
   Urb = XhcCreateUrb (\r
           Xhc,\r
           BusAddr,\r
@@ -1455,14 +1476,14 @@ XhciInsertAsyncIntTransfer (
           MaxPacket,\r
           XHC_INT_TRANSFER_ASYNC,\r
           NULL,\r
-          Data,\r
+          TRUE,\r
+          NULL,\r
           DataLen,\r
           Callback,\r
           Context\r
           );\r
   if (Urb == NULL) {\r
     DEBUG ((DEBUG_ERROR, "%a: failed to create URB\n", __FUNCTION__));\r
-    FreePool (Data);\r
     return NULL;\r
   }\r
 \r
@@ -1502,61 +1523,6 @@ XhcUpdateAsyncRequest (
   }\r
 }\r
 \r
-/**\r
-  Flush data from PCI controller specific address to mapped system\r
-  memory address.\r
-\r
-  @param  Xhc                The XHCI device.\r
-  @param  Urb                The URB to unmap.\r
-\r
-  @retval EFI_SUCCESS        Success to flush data to mapped system memory.\r
-  @retval EFI_DEVICE_ERROR   Fail to flush data to mapped system memory.\r
-\r
-**/\r
-EFI_STATUS\r
-XhcFlushAsyncIntMap (\r
-  IN  USB_XHCI_INSTANCE   *Xhc,\r
-  IN  URB                 *Urb\r
-  )\r
-{\r
-  EFI_STATUS                    Status;\r
-  EFI_PHYSICAL_ADDRESS          PhyAddr;\r
-  EFI_PCI_IO_PROTOCOL_OPERATION MapOp;\r
-  EFI_PCI_IO_PROTOCOL           *PciIo;\r
-  UINTN                         Len;\r
-  VOID                          *Map;\r
-\r
-  PciIo = Xhc->PciIo;\r
-  Len   = Urb->DataLen;\r
-\r
-  if (Urb->Ep.Direction == EfiUsbDataIn) {\r
-    MapOp = EfiPciIoOperationBusMasterWrite;\r
-  } else {\r
-    MapOp = EfiPciIoOperationBusMasterRead;\r
-  }\r
-\r
-  if (Urb->DataMap != NULL) {\r
-    Status = PciIo->Unmap (PciIo, Urb->DataMap);\r
-    if (EFI_ERROR (Status)) {\r
-      goto ON_ERROR;\r
-    }\r
-  }\r
-\r
-  Urb->DataMap = NULL;\r
-\r
-  Status = PciIo->Map (PciIo, MapOp, Urb->Data, &Len, &PhyAddr, &Map);\r
-  if (EFI_ERROR (Status) || (Len != Urb->DataLen)) {\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  Urb->DataPhy  = (VOID *) ((UINTN) PhyAddr);\r
-  Urb->DataMap  = Map;\r
-  return EFI_SUCCESS;\r
-\r
-ON_ERROR:\r
-  return EFI_DEVICE_ERROR;\r
-}\r
-\r
 /**\r
   Interrupt transfer periodic check handler.\r
 \r
@@ -1577,7 +1543,6 @@ XhcMonitorAsyncRequests (
   UINT8                   *ProcBuf;\r
   URB                     *Urb;\r
   UINT8                   SlotId;\r
-  EFI_STATUS              Status;\r
   EFI_TPL                 OldTpl;\r
 \r
   OldTpl = gBS->RaiseTPL (XHC_TPL);\r
@@ -1605,15 +1570,6 @@ XhcMonitorAsyncRequests (
       continue;\r
     }\r
 \r
-    //\r
-    // Flush any PCI posted write transactions from a PCI host\r
-    // bridge to system memory.\r
-    //\r
-    Status = XhcFlushAsyncIntMap (Xhc, Urb);\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_ERROR, "XhcMonitorAsyncRequests: Fail to Flush AsyncInt Mapped Memeory\n"));\r
-    }\r
-\r
     //\r
     // Allocate a buffer then copy the transferred data for user.\r
     // If failed to allocate the buffer, update the URB for next\r
index b5e192c3b58981d0db0e6aa97d2c934647a5f8b7..1a95d98996c16e1f5445b91daa3b16d2e92a790b 100644 (file)
@@ -172,6 +172,7 @@ typedef struct _URB {
   //\r
   USB_ENDPOINT                    Ep;\r
   EFI_USB_DEVICE_REQUEST          *Request;\r
+  BOOLEAN                         AllocateCommonBuffer;\r
   VOID                            *Data;\r
   UINTN                           DataLen;\r
   VOID                            *DataPhy;\r
@@ -1432,17 +1433,18 @@ XhcSetTrDequeuePointer (
 /**\r
   Create a new URB for a new transaction.\r
 \r
-  @param  Xhc       The XHCI Instance\r
-  @param  DevAddr   The device address\r
-  @param  EpAddr    Endpoint addrress\r
-  @param  DevSpeed  The device speed\r
-  @param  MaxPacket The max packet length of the endpoint\r
-  @param  Type      The transaction type\r
-  @param  Request   The standard USB request for control transfer\r
-  @param  Data      The user data to transfer\r
-  @param  DataLen   The length of data buffer\r
-  @param  Callback  The function to call when data is transferred\r
-  @param  Context   The context to the callback\r
+  @param  Xhc                   The XHCI Instance\r
+  @param  BusAddr               The logical device address assigned by UsbBus driver\r
+  @param  EpAddr                Endpoint addrress\r
+  @param  DevSpeed              The device speed\r
+  @param  MaxPacket             The max packet length of the endpoint\r
+  @param  Type                  The transaction type\r
+  @param  Request               The standard USB request for control transfer\r
+  @param  AllocateCommonBuffer  Indicate whether need to allocate common buffer for data transfer\r
+  @param  Data                  The user data to transfer, NULL if AllocateCommonBuffer is TRUE\r
+  @param  DataLen               The length of data buffer\r
+  @param  Callback              The function to call when data is transferred\r
+  @param  Context               The context to the callback\r
 \r
   @return Created URB or NULL\r
 \r
@@ -1450,12 +1452,13 @@ XhcSetTrDequeuePointer (
 URB*\r
 XhcCreateUrb (\r
   IN USB_XHCI_INSTANCE                  *Xhc,\r
-  IN UINT8                              DevAddr,\r
+  IN UINT8                              BusAddr,\r
   IN UINT8                              EpAddr,\r
   IN UINT8                              DevSpeed,\r
   IN UINTN                              MaxPacket,\r
   IN UINTN                              Type,\r
   IN EFI_USB_DEVICE_REQUEST             *Request,\r
+  IN BOOLEAN                            AllocateCommonBuffer,\r
   IN VOID                               *Data,\r
   IN UINTN                              DataLen,\r
   IN EFI_ASYNC_USB_TRANSFER_CALLBACK    Callback,\r