mem.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/efi.h>
  3. #include <asm/efi.h>
  4. #include "efistub.h"
  5. /**
  6. * efi_get_memory_map() - get memory map
  7. * @map: pointer to memory map pointer to which to assign the
  8. * newly allocated memory map
  9. * @install_cfg_tbl: whether or not to install the boot memory map as a
  10. * configuration table
  11. *
  12. * Retrieve the UEFI memory map. The allocated memory leaves room for
  13. * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.
  14. *
  15. * Return: status code
  16. */
  17. efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,
  18. bool install_cfg_tbl)
  19. {
  20. int memtype = install_cfg_tbl ? EFI_ACPI_RECLAIM_MEMORY
  21. : EFI_LOADER_DATA;
  22. efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID;
  23. struct efi_boot_memmap *m, tmp;
  24. efi_status_t status;
  25. unsigned long size;
  26. tmp.map_size = 0;
  27. status = efi_bs_call(get_memory_map, &tmp.map_size, NULL, &tmp.map_key,
  28. &tmp.desc_size, &tmp.desc_ver);
  29. if (status != EFI_BUFFER_TOO_SMALL)
  30. return EFI_LOAD_ERROR;
  31. size = tmp.map_size + tmp.desc_size * EFI_MMAP_NR_SLACK_SLOTS;
  32. status = efi_bs_call(allocate_pool, memtype, sizeof(*m) + size,
  33. (void **)&m);
  34. if (status != EFI_SUCCESS)
  35. return status;
  36. if (install_cfg_tbl) {
  37. /*
  38. * Installing a configuration table might allocate memory, and
  39. * this may modify the memory map. This means we should install
  40. * the configuration table first, and re-install or delete it
  41. * as needed.
  42. */
  43. status = efi_bs_call(install_configuration_table, &tbl_guid, m);
  44. if (status != EFI_SUCCESS)
  45. goto free_map;
  46. }
  47. m->buff_size = m->map_size = size;
  48. status = efi_bs_call(get_memory_map, &m->map_size, m->map, &m->map_key,
  49. &m->desc_size, &m->desc_ver);
  50. if (status != EFI_SUCCESS)
  51. goto uninstall_table;
  52. *map = m;
  53. return EFI_SUCCESS;
  54. uninstall_table:
  55. if (install_cfg_tbl)
  56. efi_bs_call(install_configuration_table, &tbl_guid, NULL);
  57. free_map:
  58. efi_bs_call(free_pool, m);
  59. return status;
  60. }
  61. /**
  62. * efi_allocate_pages() - Allocate memory pages
  63. * @size: minimum number of bytes to allocate
  64. * @addr: On return the address of the first allocated page. The first
  65. * allocated page has alignment EFI_ALLOC_ALIGN which is an
  66. * architecture dependent multiple of the page size.
  67. * @max: the address that the last allocated memory page shall not
  68. * exceed
  69. *
  70. * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
  71. * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
  72. * given by @max.
  73. *
  74. * Return: status code
  75. */
  76. efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
  77. unsigned long max)
  78. {
  79. efi_physical_addr_t alloc_addr;
  80. efi_status_t status;
  81. if (EFI_ALLOC_ALIGN > EFI_PAGE_SIZE)
  82. return efi_allocate_pages_aligned(size, addr, max,
  83. EFI_ALLOC_ALIGN);
  84. alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
  85. status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
  86. EFI_LOADER_DATA, DIV_ROUND_UP(size, EFI_PAGE_SIZE),
  87. &alloc_addr);
  88. if (status != EFI_SUCCESS)
  89. return status;
  90. *addr = alloc_addr;
  91. return EFI_SUCCESS;
  92. }
  93. /**
  94. * efi_free() - free memory pages
  95. * @size: size of the memory area to free in bytes
  96. * @addr: start of the memory area to free (must be EFI_PAGE_SIZE
  97. * aligned)
  98. *
  99. * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
  100. * architecture specific multiple of EFI_PAGE_SIZE. So this function should
  101. * only be used to return pages allocated with efi_allocate_pages() or
  102. * efi_low_alloc_above().
  103. */
  104. void efi_free(unsigned long size, unsigned long addr)
  105. {
  106. unsigned long nr_pages;
  107. if (!size)
  108. return;
  109. nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
  110. efi_bs_call(free_pages, addr, nr_pages);
  111. }