EmbeddedPkg/NonCoherentDmaLib: Utilize AllocateAlignedPagesEx

This commit is contained in:
Marvin Häuser 2023-03-26 23:06:23 +02:00 committed by Mikhail Krichanov
parent aba3723d24
commit c225c0c4ed
1 changed files with 11 additions and 72 deletions
EmbeddedPkg/Library/NonCoherentDmaLib

View File

@ -15,6 +15,7 @@
#include <Library/DmaLib.h> #include <Library/DmaLib.h>
#include <Library/DxeServicesTableLib.h> #include <Library/DxeServicesTableLib.h>
#include <Library/MemoryAllocationLib.h> #include <Library/MemoryAllocationLib.h>
#include <Library/MemoryAllocationLibEx.h>
#include <Library/UefiBootServicesTableLib.h> #include <Library/UefiBootServicesTableLib.h>
#include <Library/IoLib.h> #include <Library/IoLib.h>
#include <Library/BaseMemoryLib.h> #include <Library/BaseMemoryLib.h>
@ -81,82 +82,20 @@ InternalAllocateAlignedPages (
{ {
EFI_STATUS Status; EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Memory; EFI_PHYSICAL_ADDRESS Memory;
UINTN AlignedMemory;
UINTN AlignmentMask;
UINTN UnalignedPages;
UINTN RealPages;
// Memory = mDmaHostAddressLimit;
// Alignment must be a power of two or zero. Status = AllocateAlignedPagesEx (
// AllocateMaxAddress,
ASSERT ((Alignment & (Alignment - 1)) == 0); MemoryType,
Pages,
if (Pages == 0) { Alignment,
&Memory
);
if (EFI_ERROR (Status)) {
return NULL; return NULL;
} }
if (Alignment > EFI_PAGE_SIZE) { return (VOID *)(UINTN)Memory;
//
// Calculate the total number of pages since alignment is larger than page
// size.
//
AlignmentMask = Alignment - 1;
RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);
//
// Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not
// overflow.
//
ASSERT (RealPages > Pages);
Memory = mDmaHostAddressLimit;
Status = gBS->AllocatePages (
AllocateMaxAddress,
MemoryType,
RealPages,
&Memory
);
if (EFI_ERROR (Status)) {
return NULL;
}
AlignedMemory = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;
UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);
if (UnalignedPages > 0) {
//
// Free first unaligned page(s).
//
Status = gBS->FreePages (Memory, UnalignedPages);
ASSERT_EFI_ERROR (Status);
}
Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);
UnalignedPages = RealPages - Pages - UnalignedPages;
if (UnalignedPages > 0) {
//
// Free last unaligned page(s).
//
Status = gBS->FreePages (Memory, UnalignedPages);
ASSERT_EFI_ERROR (Status);
}
} else {
//
// Do not over-allocate pages in this case.
//
Memory = mDmaHostAddressLimit;
Status = gBS->AllocatePages (
AllocateMaxAddress,
MemoryType,
Pages,
&Memory
);
if (EFI_ERROR (Status)) {
return NULL;
}
AlignedMemory = (UINTN)Memory;
}
return (VOID *)AlignedMemory;
} }
/** /**