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 091bca8bc8
commit f0ac2e86e3
1 changed files with 11 additions and 72 deletions

View File

@ -15,6 +15,7 @@
#include <Library/DmaLib.h>
#include <Library/DxeServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/MemoryAllocationLibEx.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/IoLib.h>
#include <Library/BaseMemoryLib.h>
@ -81,82 +82,20 @@ InternalAllocateAlignedPages (
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Memory;
UINTN AlignedMemory;
UINTN AlignmentMask;
UINTN UnalignedPages;
UINTN RealPages;
//
// Alignment must be a power of two or zero.
//
ASSERT ((Alignment & (Alignment - 1)) == 0);
if (Pages == 0) {
Memory = mDmaHostAddressLimit;
Status = AllocateAlignedPagesEx (
AllocateMaxAddress,
MemoryType,
Pages,
Alignment,
&Memory
);
if (EFI_ERROR (Status)) {
return NULL;
}
if (Alignment > EFI_PAGE_SIZE) {
//
// 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;
return (VOID *)(UINTN)Memory;
}
/**