]> git.proxmox.com Git - mirror_edk2.git/commitdiff
EmbeddedPkg/DmaLib: add routine to allocate aligned buffers
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Thu, 24 Aug 2017 19:05:48 +0000 (20:05 +0100)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Tue, 29 Aug 2017 16:54:36 +0000 (17:54 +0100)
DmaLib's purpose is to manage memory that is shared between the host
and DMA capable devices. In some cases, this requires a larger alignment
than page size, and we currently don't cater for that in DmaLib. So add
a variant of DmaAllocateBuffer () that takes an alignment parameter.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Leif Lindholm <leif.lindholm@linaro.org>
EmbeddedPkg/Include/Library/DmaLib.h
EmbeddedPkg/Library/NullDmaLib/NullDmaLib.c

index 3814291c2875a26e34db78f6c8a1b168d56a112c..1843814c65cad4b27e729631a7563215f4565872 100644 (file)
@@ -155,5 +155,33 @@ DmaFreeBuffer (
   );\r
 \r
 \r
-#endif\r
+/**\r
+  Allocates pages that are suitable for an DmaMap() of type\r
+  MapOperationBusMasterCommonBuffer mapping, at the requested alignment.\r
+\r
+  @param  MemoryType            The type of memory to allocate, EfiBootServicesData or\r
+                                EfiRuntimeServicesData.\r
+  @param  Pages                 The number of pages to allocate.\r
+  @param  Alignment             Alignment in bytes of the base of the returned\r
+                                buffer (must be a power of 2)\r
+  @param  HostAddress           A pointer to store the base system memory address of the\r
+                                allocated range.\r
 \r
+  @retval EFI_SUCCESS           The requested memory pages were allocated.\r
+  @retval EFI_UNSUPPORTED       Attributes is unsupported. The only legal attribute bits are\r
+                                MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
+  @retval EFI_OUT_OF_RESOURCES  The memory pages could not be allocated.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DmaAllocateAlignedBuffer (\r
+  IN  EFI_MEMORY_TYPE              MemoryType,\r
+  IN  UINTN                        Pages,\r
+  IN  UINTN                        Alignment,\r
+  OUT VOID                         **HostAddress\r
+  );\r
+\r
+\r
+#endif\r
index a0bb57541d6039c6ae6328b73addc083b81ff70e..4cbe349190a99c534ed124203cb5d4f2634706dd 100644 (file)
@@ -100,23 +100,61 @@ DmaAllocateBuffer (
   OUT VOID                         **HostAddress\r
   )\r
 {\r
-  if (HostAddress == NULL) {\r
+  return DmaAllocateAlignedBuffer (MemoryType, Pages, 0, HostAddress);\r
+}\r
+\r
+\r
+/**\r
+  Allocates pages that are suitable for an DmaMap() of type\r
+  MapOperationBusMasterCommonBuffer mapping, at the requested alignment.\r
+\r
+  @param  MemoryType            The type of memory to allocate, EfiBootServicesData or\r
+                                EfiRuntimeServicesData.\r
+  @param  Pages                 The number of pages to allocate.\r
+  @param  Alignment             Alignment in bytes of the base of the returned\r
+                                buffer (must be a power of 2)\r
+  @param  HostAddress           A pointer to store the base system memory address of the\r
+                                allocated range.\r
+\r
+  @retval EFI_SUCCESS           The requested memory pages were allocated.\r
+  @retval EFI_UNSUPPORTED       Attributes is unsupported. The only legal attribute bits are\r
+                                MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
+  @retval EFI_OUT_OF_RESOURCES  The memory pages could not be allocated.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DmaAllocateAlignedBuffer (\r
+  IN  EFI_MEMORY_TYPE              MemoryType,\r
+  IN  UINTN                        Pages,\r
+  IN  UINTN                        Alignment,\r
+  OUT VOID                         **HostAddress\r
+  )\r
+{\r
+  if (Alignment == 0) {\r
+    Alignment = EFI_PAGE_SIZE;\r
+  }\r
+\r
+  if (HostAddress == NULL ||\r
+      (Alignment & (Alignment - 1)) != 0) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   //\r
   // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData\r
   //\r
-  // We used uncached memory to keep coherency\r
-  //\r
   if (MemoryType == EfiBootServicesData) {\r
-    *HostAddress = AllocatePages (Pages);\r
+    *HostAddress = AllocateAlignedPages (Pages, Alignment);\r
   } else if (MemoryType != EfiRuntimeServicesData) {\r
-    *HostAddress = AllocateRuntimePages (Pages);\r
+    *HostAddress = AllocateAlignedRuntimePages (Pages, Alignment);\r
   } else {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (*HostAddress == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
   return EFI_SUCCESS;\r
 }\r
 \r