^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <asm/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "efistub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * efi_allocate_pages_aligned() - Allocate memory pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * @size: minimum number of bytes to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * @addr: On return the address of the first allocated page. The first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * allocated page has alignment EFI_ALLOC_ALIGN which is an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * architecture dependent multiple of the page size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * @max: the address that the last allocated memory page shall not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * exceed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * @align: minimum alignment of the base of the allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * to @align, which should be >= EFI_ALLOC_ALIGN. The last allocated page will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * not exceed the address given by @max.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Return: status code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned long max, unsigned long align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) efi_physical_addr_t alloc_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int slack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (align < EFI_ALLOC_ALIGN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) align = EFI_ALLOC_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) alloc_addr = ALIGN_DOWN(max + 1, align) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) size = round_up(size, EFI_ALLOC_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) slack = align / EFI_PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) EFI_LOADER_DATA, size / EFI_PAGE_SIZE + slack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) &alloc_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *addr = ALIGN((unsigned long)alloc_addr, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (slack > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) efi_bs_call(free_pages, alloc_addr, slack - l + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) slack = l - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (slack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) efi_bs_call(free_pages, *addr + size, slack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }