alignedmem.c 1.6 KB

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