1 // SPDX-License-Identifier: GPL-2.0
8 static inline bool mmap_has_headroom(unsigned long buff_size,
9 unsigned long map_size,
10 unsigned long desc_size)
12 unsigned long slack = buff_size - map_size;
14 return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
18 * efi_get_memory_map() - get memory map
19 * @map: on return pointer to memory map
21 * Retrieve the UEFI memory map. The allocated memory leaves room for
22 * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.
26 efi_status_t efi_get_memory_map(struct efi_boot_memmap *map)
28 efi_memory_desc_t *m = NULL;
33 *map->desc_size = sizeof(*m);
34 *map->map_size = *map->desc_size * 32;
35 *map->buff_size = *map->map_size;
37 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
38 *map->map_size, (void **)&m);
39 if (status != EFI_SUCCESS)
44 status = efi_bs_call(get_memory_map, map->map_size, m,
45 &key, map->desc_size, &desc_version);
46 if (status == EFI_BUFFER_TOO_SMALL ||
47 !mmap_has_headroom(*map->buff_size, *map->map_size,
49 efi_bs_call(free_pool, m);
51 * Make sure there is some entries of headroom so that the
52 * buffer can be reused for a new map after allocations are
53 * no longer permitted. Its unlikely that the map will grow to
54 * exceed this headroom once we are ready to trigger
57 *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
58 *map->buff_size = *map->map_size;
62 if (status == EFI_SUCCESS) {
66 *map->desc_ver = desc_version;
68 efi_bs_call(free_pool, m);
77 * efi_allocate_pages() - Allocate memory pages
78 * @size: minimum number of bytes to allocate
79 * @addr: On return the address of the first allocated page. The first
80 * allocated page has alignment EFI_ALLOC_ALIGN which is an
81 * architecture dependent multiple of the page size.
82 * @max: the address that the last allocated memory page shall not
85 * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
86 * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
91 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
94 efi_physical_addr_t alloc_addr;
97 if (EFI_ALLOC_ALIGN > EFI_PAGE_SIZE)
98 return efi_allocate_pages_aligned(size, addr, max,
101 alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
102 status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
103 EFI_LOADER_DATA, DIV_ROUND_UP(size, EFI_PAGE_SIZE),
105 if (status != EFI_SUCCESS)
113 * efi_free() - free memory pages
114 * @size: size of the memory area to free in bytes
115 * @addr: start of the memory area to free (must be EFI_PAGE_SIZE
118 * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
119 * architecture specific multiple of EFI_PAGE_SIZE. So this function should
120 * only be used to return pages allocated with efi_allocate_pages() or
121 * efi_low_alloc_above().
123 void efi_free(unsigned long size, unsigned long addr)
125 unsigned long nr_pages;
130 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
131 efi_bs_call(free_pages, addr, nr_pages);