1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/efi.h> 4 #include <asm/efi.h> 5 6 #include "efistub.h" 7 8 /** 9 * efi_get_memory_map() - get memory map 10 * @map: pointer to memory map pointer to which to assign the 11 * newly allocated memory map 12 * @install_cfg_tbl: whether or not to install the boot memory map as a 13 * configuration table 14 * 15 * Retrieve the UEFI memory map. The allocated memory leaves room for 16 * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries. 17 * 18 * Return: status code 19 */ 20 efi_status_t efi_get_memory_map(struct efi_boot_memmap **map, 21 bool install_cfg_tbl) 22 { 23 struct efi_boot_memmap tmp, *m __free(efi_pool) = NULL; 24 int memtype = install_cfg_tbl ? EFI_ACPI_RECLAIM_MEMORY 25 : EFI_LOADER_DATA; 26 efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID; 27 efi_status_t status; 28 unsigned long size; 29 30 tmp.map_size = 0; 31 status = efi_bs_call(get_memory_map, &tmp.map_size, NULL, &tmp.map_key, 32 &tmp.desc_size, &tmp.desc_ver); 33 if (status != EFI_BUFFER_TOO_SMALL) 34 return EFI_LOAD_ERROR; 35 36 size = tmp.map_size + tmp.desc_size * EFI_MMAP_NR_SLACK_SLOTS; 37 status = efi_bs_call(allocate_pool, memtype, sizeof(*m) + size, 38 (void **)&m); 39 if (status != EFI_SUCCESS) 40 return status; 41 42 if (install_cfg_tbl) { 43 /* 44 * Installing a configuration table might allocate memory, and 45 * this may modify the memory map. This means we should install 46 * the configuration table first, and re-install or delete it 47 * as needed. 48 */ 49 status = efi_bs_call(install_configuration_table, &tbl_guid, m); 50 if (status != EFI_SUCCESS) 51 return status; 52 } 53 54 m->buff_size = m->map_size = size; 55 status = efi_bs_call(get_memory_map, &m->map_size, m->map, &m->map_key, 56 &m->desc_size, &m->desc_ver); 57 if (status != EFI_SUCCESS) { 58 if (install_cfg_tbl) 59 efi_bs_call(install_configuration_table, &tbl_guid, NULL); 60 return status; 61 } 62 63 *map = no_free_ptr(m); 64 return EFI_SUCCESS; 65 } 66 67 /** 68 * efi_allocate_pages() - Allocate memory pages 69 * @size: minimum number of bytes to allocate 70 * @addr: On return the address of the first allocated page. The first 71 * allocated page has alignment EFI_ALLOC_ALIGN which is an 72 * architecture dependent multiple of the page size. 73 * @max: the address that the last allocated memory page shall not 74 * exceed 75 * 76 * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according 77 * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address 78 * given by @max. 79 * 80 * Return: status code 81 */ 82 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr, 83 unsigned long max) 84 { 85 efi_physical_addr_t alloc_addr; 86 efi_status_t status; 87 88 max = min(max, EFI_ALLOC_LIMIT); 89 90 if (EFI_ALLOC_ALIGN > EFI_PAGE_SIZE) 91 return efi_allocate_pages_aligned(size, addr, max, 92 EFI_ALLOC_ALIGN, 93 EFI_LOADER_DATA); 94 95 alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1; 96 status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS, 97 EFI_LOADER_DATA, DIV_ROUND_UP(size, EFI_PAGE_SIZE), 98 &alloc_addr); 99 if (status != EFI_SUCCESS) 100 return status; 101 102 *addr = alloc_addr; 103 return EFI_SUCCESS; 104 } 105 106 /** 107 * efi_free() - free memory pages 108 * @size: size of the memory area to free in bytes 109 * @addr: start of the memory area to free (must be EFI_PAGE_SIZE 110 * aligned) 111 * 112 * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an 113 * architecture specific multiple of EFI_PAGE_SIZE. So this function should 114 * only be used to return pages allocated with efi_allocate_pages() or 115 * efi_low_alloc_above(). 116 */ 117 void efi_free(unsigned long size, unsigned long addr) 118 { 119 unsigned long nr_pages; 120 121 if (!size) 122 return; 123 124 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE; 125 efi_bs_call(free_pages, addr, nr_pages); 126 } 127 128 /** 129 * efi_low_alloc_above() - allocate pages at or above given address 130 * @size: size of the memory area to allocate 131 * @align: minimum alignment of the allocated memory area. It should 132 * a power of two. 133 * @addr: on exit the address of the allocated memory 134 * @min: minimum address to used for the memory allocation 135 * 136 * Allocate at the lowest possible address that is not below @min as 137 * EFI_LOADER_DATA. The allocated pages are aligned according to @align but at 138 * least EFI_ALLOC_ALIGN. The first allocated page will not below the address 139 * given by @min. 140 * 141 * Return: status code 142 */ 143 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align, 144 unsigned long *addr, unsigned long min) 145 { 146 struct efi_boot_memmap *map __free(efi_pool) = NULL; 147 efi_status_t status; 148 unsigned long nr_pages; 149 int i; 150 151 status = efi_get_memory_map(&map, false); 152 if (status != EFI_SUCCESS) 153 return status; 154 155 /* 156 * Enforce minimum alignment that EFI or Linux requires when 157 * requesting a specific address. We are doing page-based (or 158 * larger) allocations, and both the address and size must meet 159 * alignment constraints. 160 */ 161 if (align < EFI_ALLOC_ALIGN) 162 align = EFI_ALLOC_ALIGN; 163 164 size = round_up(size, EFI_ALLOC_ALIGN); 165 nr_pages = size / EFI_PAGE_SIZE; 166 for (i = 0; i < map->map_size / map->desc_size; i++) { 167 efi_memory_desc_t *desc; 168 unsigned long m = (unsigned long)map->map; 169 u64 start, end; 170 171 desc = efi_memdesc_ptr(m, map->desc_size, i); 172 173 if (desc->type != EFI_CONVENTIONAL_MEMORY) 174 continue; 175 176 if (desc->attribute & EFI_MEMORY_HOT_PLUGGABLE) 177 continue; 178 179 if (efi_soft_reserve_enabled() && 180 (desc->attribute & EFI_MEMORY_SP)) 181 continue; 182 183 if (desc->num_pages < nr_pages) 184 continue; 185 186 start = desc->phys_addr; 187 end = start + desc->num_pages * EFI_PAGE_SIZE; 188 189 if (start < min) 190 start = min; 191 192 start = round_up(start, align); 193 if ((start + size) > end) 194 continue; 195 196 status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, 197 EFI_LOADER_DATA, nr_pages, &start); 198 if (status == EFI_SUCCESS) { 199 *addr = start; 200 break; 201 } 202 } 203 204 if (i == map->map_size / map->desc_size) 205 return EFI_NOT_FOUND; 206 207 return EFI_SUCCESS; 208 } 209