]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c
MdeModulePkg/SdMmcPciHcDxe: Add V3 64b DMA Support
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / XhciPei / UsbHcMem.c
index b9ea51b38cd077db6c0dd5a2232bdaef0e389899..442a613095c565dcedc4d3aec54af67a60e1e669 100644 (file)
@@ -2,7 +2,7 @@
 PEIM to produce gPeiUsb2HostControllerPpiGuid based on gPeiUsbControllerPpiGuid\r
 which is used to enable recovery function from USB Drivers.\r
 \r
-Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions\r
@@ -31,6 +31,9 @@ UsbHcAllocMemBlock (
   )\r
 {\r
   USBHC_MEM_BLOCK       *Block;\r
+  VOID                  *BufHost;\r
+  VOID                  *Mapping;\r
+  EFI_PHYSICAL_ADDRESS  MappedAddr;\r
   EFI_STATUS            Status;\r
   UINTN                 PageNumber;\r
   EFI_PHYSICAL_ADDRESS  TempPtr;\r
@@ -71,18 +74,20 @@ UsbHcAllocMemBlock (
 \r
   Block->Bits = (UINT8 *) (UINTN) TempPtr;\r
 \r
-  Status = PeiServicesAllocatePages (\r
-             EfiBootServicesData,\r
+  Status = IoMmuAllocateBuffer (\r
              Pages,\r
-             &TempPtr\r
+             &BufHost,\r
+             &MappedAddr,\r
+             &Mapping\r
              );\r
   if (EFI_ERROR (Status)) {\r
     return NULL;\r
   }\r
-  ZeroMem ((VOID *) (UINTN) TempPtr, EFI_PAGES_TO_SIZE (Pages));\r
+  ZeroMem ((VOID *) (UINTN) BufHost, EFI_PAGES_TO_SIZE (Pages));\r
 \r
-  Block->BufHost = (UINT8 *) (UINTN) TempPtr;;\r
-  Block->Buf = (UINT8 *) (UINTN) TempPtr;\r
+  Block->BufHost = (UINT8 *) (UINTN) BufHost;\r
+  Block->Buf = (UINT8 *) (UINTN) MappedAddr;\r
+  Block->Mapping  = Mapping;\r
   Block->Next = NULL;\r
 \r
   return Block;\r
@@ -102,6 +107,9 @@ UsbHcFreeMemBlock (
   )\r
 {\r
   ASSERT ((Pool != NULL) && (Block != NULL));\r
+\r
+  IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost, Block->Mapping);\r
+\r
   //\r
   // No free memory in PEI.\r
   //\r
@@ -320,31 +328,7 @@ UsbHcIsMemBlockEmpty (
   return TRUE;\r
 }\r
 \r
-/**\r
-  Unlink the memory block from the pool's list.\r
-\r
-  @param  Head          The block list head of the memory's pool.\r
-  @param  BlockToUnlink The memory block to unlink.\r
 \r
-**/\r
-VOID\r
-UsbHcUnlinkMemBlock (\r
-  IN USBHC_MEM_BLOCK    *Head,\r
-  IN USBHC_MEM_BLOCK    *BlockToUnlink\r
-  )\r
-{\r
-  USBHC_MEM_BLOCK       *Block;\r
-\r
-  ASSERT ((Head != NULL) && (BlockToUnlink != NULL));\r
-\r
-  for (Block = Head; Block != NULL; Block = Block->Next) {\r
-    if (Block->Next == BlockToUnlink) {\r
-      Block->Next         = BlockToUnlink->Next;\r
-      BlockToUnlink->Next = NULL;\r
-      break;\r
-    }\r
-  }\r
-}\r
 \r
 /**\r
   Initialize the memory management pool for the host controller.\r
@@ -528,7 +512,7 @@ UsbHcFreeMem (
       Bit   = ((ToFree - Block->BufHost) / USBHC_MEM_UNIT) % 8;\r
 \r
       //\r
-      // reset associated bits in bit arry\r
+      // reset associated bits in bit array\r
       //\r
       for (Count = 0; Count < (AllocSize / USBHC_MEM_UNIT); Count++) {\r
         ASSERT (USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));\r
@@ -567,6 +551,7 @@ UsbHcFreeMem (
   @param  HostAddress           The system memory address to map to the PCI controller.\r
   @param  DeviceAddress         The resulting map address for the bus master PCI controller to\r
                                 use to access the hosts HostAddress.\r
+  @param  Mapping               A resulting value to pass to Unmap().\r
 \r
   @retval EFI_SUCCESS           Success to allocate aligned pages.\r
   @retval EFI_INVALID_PARAMETER Pages or Alignment is not valid.\r
@@ -578,13 +563,16 @@ UsbHcAllocateAlignedPages (
   IN UINTN                      Pages,\r
   IN UINTN                      Alignment,\r
   OUT VOID                      **HostAddress,\r
-  OUT EFI_PHYSICAL_ADDRESS      *DeviceAddress\r
+  OUT EFI_PHYSICAL_ADDRESS      *DeviceAddress,\r
+  OUT VOID                      **Mapping\r
   )\r
 {\r
   EFI_STATUS            Status;\r
-  EFI_PHYSICAL_ADDRESS  Memory;\r
+  VOID                  *Memory;\r
   UINTN                 AlignedMemory;\r
   UINTN                 AlignmentMask;\r
+  EFI_PHYSICAL_ADDRESS  DeviceMemory;\r
+  UINTN                 AlignedDeviceMemory;\r
   UINTN                 RealPages;\r
 \r
   //\r
@@ -602,7 +590,7 @@ UsbHcAllocateAlignedPages (
 \r
   if (Alignment > EFI_PAGE_SIZE) {\r
     //\r
-    // Caculate the total number of pages since alignment is larger than page size.\r
+    // Calculate the total number of pages since alignment is larger than page size.\r
     //\r
     AlignmentMask  = Alignment - 1;\r
     RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
@@ -611,32 +599,36 @@ UsbHcAllocateAlignedPages (
     //\r
     ASSERT (RealPages > Pages);\r
 \r
-    Status = PeiServicesAllocatePages (\r
-               EfiBootServicesData,\r
+    Status = IoMmuAllocateBuffer (\r
                Pages,\r
-               &Memory\r
+               &Memory,\r
+               &DeviceMemory,\r
+               Mapping\r
                );\r
     if (EFI_ERROR (Status)) {\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
     AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
+    AlignedDeviceMemory = ((UINTN) DeviceMemory + AlignmentMask) & ~AlignmentMask;\r
   } else {\r
     //\r
     // Do not over-allocate pages in this case.\r
     //\r
-    Status = PeiServicesAllocatePages (\r
-               EfiBootServicesData,\r
+    Status = IoMmuAllocateBuffer (\r
                Pages,\r
-               &Memory\r
+               &Memory,\r
+               &DeviceMemory,\r
+               Mapping\r
                );\r
     if (EFI_ERROR (Status)) {\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
     AlignedMemory = (UINTN) Memory;\r
+    AlignedDeviceMemory = (UINTN) DeviceMemory;\r
   }\r
 \r
   *HostAddress = (VOID *) AlignedMemory;\r
-  *DeviceAddress = (EFI_PHYSICAL_ADDRESS) AlignedMemory;\r
+  *DeviceAddress = (EFI_PHYSICAL_ADDRESS) AlignedDeviceMemory;\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -646,17 +638,18 @@ UsbHcAllocateAlignedPages (
 \r
   @param  HostAddress           The system memory address to map to the PCI controller.\r
   @param  Pages                 The number of pages to free.\r
+  @param  Mapping               The mapping value returned from Map().\r
 \r
 **/\r
 VOID\r
 UsbHcFreeAlignedPages (\r
   IN VOID               *HostAddress,\r
-  IN UINTN              Pages\r
+  IN UINTN              Pages,\r
+  IN VOID               *Mapping\r
   )\r
 {\r
   ASSERT (Pages != 0);\r
-  //\r
-  // No free memory in PEI.\r
-  //\r
+\r
+  IoMmuFreeBuffer (Pages, HostAddress, Mapping);\r
 }\r
 \r