Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/efi.h>
0004 #include <asm/efi.h>
0005 
0006 #include "efistub.h"
0007 
0008 /**
0009  * efi_allocate_pages_aligned() - Allocate memory pages
0010  * @size:   minimum number of bytes to allocate
0011  * @addr:   On return the address of the first allocated page. The first
0012  *      allocated page has alignment EFI_ALLOC_ALIGN which is an
0013  *      architecture dependent multiple of the page size.
0014  * @max:    the address that the last allocated memory page shall not
0015  *      exceed
0016  * @align:  minimum alignment of the base of the allocation
0017  *
0018  * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
0019  * to @align, which should be >= EFI_ALLOC_ALIGN. The last allocated page will
0020  * not exceed the address given by @max.
0021  *
0022  * Return:  status code
0023  */
0024 efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
0025                     unsigned long max, unsigned long align)
0026 {
0027     efi_physical_addr_t alloc_addr;
0028     efi_status_t status;
0029     int slack;
0030 
0031     if (align < EFI_ALLOC_ALIGN)
0032         align = EFI_ALLOC_ALIGN;
0033 
0034     alloc_addr = ALIGN_DOWN(max + 1, align) - 1;
0035     size = round_up(size, EFI_ALLOC_ALIGN);
0036     slack = align / EFI_PAGE_SIZE - 1;
0037 
0038     status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
0039                  EFI_LOADER_DATA, size / EFI_PAGE_SIZE + slack,
0040                  &alloc_addr);
0041     if (status != EFI_SUCCESS)
0042         return status;
0043 
0044     *addr = ALIGN((unsigned long)alloc_addr, align);
0045 
0046     if (slack > 0) {
0047         int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;
0048 
0049         if (l) {
0050             efi_bs_call(free_pages, alloc_addr, slack - l + 1);
0051             slack = l - 1;
0052         }
0053         if (slack)
0054             efi_bs_call(free_pages, *addr + size, slack);
0055     }
0056     return EFI_SUCCESS;
0057 }