| .. | .. |
|---|
| 15 | 15 | |
|---|
| 16 | 16 | static phys_addr_t __init __efi_memmap_alloc_early(unsigned long size) |
|---|
| 17 | 17 | { |
|---|
| 18 | | - return memblock_alloc(size, 0); |
|---|
| 18 | + return memblock_phys_alloc(size, SMP_CACHE_BYTES); |
|---|
| 19 | 19 | } |
|---|
| 20 | 20 | |
|---|
| 21 | 21 | static phys_addr_t __init __efi_memmap_alloc_late(unsigned long size) |
|---|
| .. | .. |
|---|
| 29 | 29 | return PFN_PHYS(page_to_pfn(p)); |
|---|
| 30 | 30 | } |
|---|
| 31 | 31 | |
|---|
| 32 | +void __init __efi_memmap_free(u64 phys, unsigned long size, unsigned long flags) |
|---|
| 33 | +{ |
|---|
| 34 | + if (flags & EFI_MEMMAP_MEMBLOCK) { |
|---|
| 35 | + if (slab_is_available()) |
|---|
| 36 | + memblock_free_late(phys, size); |
|---|
| 37 | + else |
|---|
| 38 | + memblock_free(phys, size); |
|---|
| 39 | + } else if (flags & EFI_MEMMAP_SLAB) { |
|---|
| 40 | + struct page *p = pfn_to_page(PHYS_PFN(phys)); |
|---|
| 41 | + unsigned int order = get_order(size); |
|---|
| 42 | + |
|---|
| 43 | + free_pages((unsigned long) page_address(p), order); |
|---|
| 44 | + } |
|---|
| 45 | +} |
|---|
| 46 | + |
|---|
| 47 | +static void __init efi_memmap_free(void) |
|---|
| 48 | +{ |
|---|
| 49 | + __efi_memmap_free(efi.memmap.phys_map, |
|---|
| 50 | + efi.memmap.desc_size * efi.memmap.nr_map, |
|---|
| 51 | + efi.memmap.flags); |
|---|
| 52 | +} |
|---|
| 53 | + |
|---|
| 32 | 54 | /** |
|---|
| 33 | 55 | * efi_memmap_alloc - Allocate memory for the EFI memory map |
|---|
| 34 | 56 | * @num_entries: Number of entries in the allocated map. |
|---|
| 57 | + * @data: efi memmap installation parameters |
|---|
| 35 | 58 | * |
|---|
| 36 | 59 | * Depending on whether mm_init() has already been invoked or not, |
|---|
| 37 | 60 | * either memblock or "normal" page allocation is used. |
|---|
| .. | .. |
|---|
| 39 | 62 | * Returns the physical address of the allocated memory map on |
|---|
| 40 | 63 | * success, zero on failure. |
|---|
| 41 | 64 | */ |
|---|
| 42 | | -phys_addr_t __init efi_memmap_alloc(unsigned int num_entries) |
|---|
| 65 | +int __init efi_memmap_alloc(unsigned int num_entries, |
|---|
| 66 | + struct efi_memory_map_data *data) |
|---|
| 43 | 67 | { |
|---|
| 44 | | - unsigned long size = num_entries * efi.memmap.desc_size; |
|---|
| 68 | + /* Expect allocation parameters are zero initialized */ |
|---|
| 69 | + WARN_ON(data->phys_map || data->size); |
|---|
| 45 | 70 | |
|---|
| 46 | | - if (slab_is_available()) |
|---|
| 47 | | - return __efi_memmap_alloc_late(size); |
|---|
| 71 | + data->size = num_entries * efi.memmap.desc_size; |
|---|
| 72 | + data->desc_version = efi.memmap.desc_version; |
|---|
| 73 | + data->desc_size = efi.memmap.desc_size; |
|---|
| 74 | + data->flags &= ~(EFI_MEMMAP_SLAB | EFI_MEMMAP_MEMBLOCK); |
|---|
| 75 | + data->flags |= efi.memmap.flags & EFI_MEMMAP_LATE; |
|---|
| 48 | 76 | |
|---|
| 49 | | - return __efi_memmap_alloc_early(size); |
|---|
| 77 | + if (slab_is_available()) { |
|---|
| 78 | + data->flags |= EFI_MEMMAP_SLAB; |
|---|
| 79 | + data->phys_map = __efi_memmap_alloc_late(data->size); |
|---|
| 80 | + } else { |
|---|
| 81 | + data->flags |= EFI_MEMMAP_MEMBLOCK; |
|---|
| 82 | + data->phys_map = __efi_memmap_alloc_early(data->size); |
|---|
| 83 | + } |
|---|
| 84 | + |
|---|
| 85 | + if (!data->phys_map) |
|---|
| 86 | + return -ENOMEM; |
|---|
| 87 | + return 0; |
|---|
| 50 | 88 | } |
|---|
| 51 | 89 | |
|---|
| 52 | 90 | /** |
|---|
| 53 | 91 | * __efi_memmap_init - Common code for mapping the EFI memory map |
|---|
| 54 | 92 | * @data: EFI memory map data |
|---|
| 55 | | - * @late: Use early or late mapping function? |
|---|
| 56 | 93 | * |
|---|
| 57 | 94 | * This function takes care of figuring out which function to use to |
|---|
| 58 | 95 | * map the EFI memory map in efi.memmap based on how far into the boot |
|---|
| 59 | 96 | * we are. |
|---|
| 60 | 97 | * |
|---|
| 61 | | - * During bootup @late should be %false since we only have access to |
|---|
| 62 | | - * the early_memremap*() functions as the vmalloc space isn't setup. |
|---|
| 63 | | - * Once the kernel is fully booted we can fallback to the more robust |
|---|
| 64 | | - * memremap*() API. |
|---|
| 98 | + * During bootup EFI_MEMMAP_LATE in data->flags should be clear since we |
|---|
| 99 | + * only have access to the early_memremap*() functions as the vmalloc |
|---|
| 100 | + * space isn't setup. Once the kernel is fully booted we can fallback |
|---|
| 101 | + * to the more robust memremap*() API. |
|---|
| 65 | 102 | * |
|---|
| 66 | 103 | * Returns zero on success, a negative error code on failure. |
|---|
| 67 | 104 | */ |
|---|
| 68 | | -static int __init |
|---|
| 69 | | -__efi_memmap_init(struct efi_memory_map_data *data, bool late) |
|---|
| 105 | +static int __init __efi_memmap_init(struct efi_memory_map_data *data) |
|---|
| 70 | 106 | { |
|---|
| 71 | 107 | struct efi_memory_map map; |
|---|
| 72 | 108 | phys_addr_t phys_map; |
|---|
| .. | .. |
|---|
| 76 | 112 | |
|---|
| 77 | 113 | phys_map = data->phys_map; |
|---|
| 78 | 114 | |
|---|
| 79 | | - if (late) |
|---|
| 115 | + if (data->flags & EFI_MEMMAP_LATE) |
|---|
| 80 | 116 | map.map = memremap(phys_map, data->size, MEMREMAP_WB); |
|---|
| 81 | 117 | else |
|---|
| 82 | 118 | map.map = early_memremap(phys_map, data->size); |
|---|
| .. | .. |
|---|
| 86 | 122 | return -ENOMEM; |
|---|
| 87 | 123 | } |
|---|
| 88 | 124 | |
|---|
| 125 | + /* NOP if data->flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB) == 0 */ |
|---|
| 126 | + efi_memmap_free(); |
|---|
| 127 | + |
|---|
| 89 | 128 | map.phys_map = data->phys_map; |
|---|
| 90 | 129 | map.nr_map = data->size / data->desc_size; |
|---|
| 91 | 130 | map.map_end = map.map + data->size; |
|---|
| 92 | 131 | |
|---|
| 93 | 132 | map.desc_version = data->desc_version; |
|---|
| 94 | 133 | map.desc_size = data->desc_size; |
|---|
| 95 | | - map.late = late; |
|---|
| 134 | + map.flags = data->flags; |
|---|
| 96 | 135 | |
|---|
| 97 | 136 | set_bit(EFI_MEMMAP, &efi.flags); |
|---|
| 98 | 137 | |
|---|
| .. | .. |
|---|
| 111 | 150 | int __init efi_memmap_init_early(struct efi_memory_map_data *data) |
|---|
| 112 | 151 | { |
|---|
| 113 | 152 | /* Cannot go backwards */ |
|---|
| 114 | | - WARN_ON(efi.memmap.late); |
|---|
| 153 | + WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE); |
|---|
| 115 | 154 | |
|---|
| 116 | | - return __efi_memmap_init(data, false); |
|---|
| 155 | + data->flags = 0; |
|---|
| 156 | + return __efi_memmap_init(data); |
|---|
| 117 | 157 | } |
|---|
| 118 | 158 | |
|---|
| 119 | 159 | void __init efi_memmap_unmap(void) |
|---|
| .. | .. |
|---|
| 121 | 161 | if (!efi_enabled(EFI_MEMMAP)) |
|---|
| 122 | 162 | return; |
|---|
| 123 | 163 | |
|---|
| 124 | | - if (!efi.memmap.late) { |
|---|
| 164 | + if (!(efi.memmap.flags & EFI_MEMMAP_LATE)) { |
|---|
| 125 | 165 | unsigned long size; |
|---|
| 126 | 166 | |
|---|
| 127 | 167 | size = efi.memmap.desc_size * efi.memmap.nr_map; |
|---|
| .. | .. |
|---|
| 162 | 202 | struct efi_memory_map_data data = { |
|---|
| 163 | 203 | .phys_map = addr, |
|---|
| 164 | 204 | .size = size, |
|---|
| 205 | + .flags = EFI_MEMMAP_LATE, |
|---|
| 165 | 206 | }; |
|---|
| 166 | 207 | |
|---|
| 167 | 208 | /* Did we forget to unmap the early EFI memmap? */ |
|---|
| 168 | 209 | WARN_ON(efi.memmap.map); |
|---|
| 169 | 210 | |
|---|
| 170 | 211 | /* Were we already called? */ |
|---|
| 171 | | - WARN_ON(efi.memmap.late); |
|---|
| 212 | + WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE); |
|---|
| 172 | 213 | |
|---|
| 173 | 214 | /* |
|---|
| 174 | 215 | * It makes no sense to allow callers to register different |
|---|
| .. | .. |
|---|
| 178 | 219 | data.desc_version = efi.memmap.desc_version; |
|---|
| 179 | 220 | data.desc_size = efi.memmap.desc_size; |
|---|
| 180 | 221 | |
|---|
| 181 | | - return __efi_memmap_init(&data, true); |
|---|
| 222 | + return __efi_memmap_init(&data); |
|---|
| 182 | 223 | } |
|---|
| 183 | 224 | |
|---|
| 184 | 225 | /** |
|---|
| 185 | 226 | * efi_memmap_install - Install a new EFI memory map in efi.memmap |
|---|
| 186 | | - * @addr: Physical address of the memory map |
|---|
| 187 | | - * @nr_map: Number of entries in the memory map |
|---|
| 227 | + * @ctx: map allocation parameters (address, size, flags) |
|---|
| 188 | 228 | * |
|---|
| 189 | 229 | * Unlike efi_memmap_init_*(), this function does not allow the caller |
|---|
| 190 | 230 | * to switch from early to late mappings. It simply uses the existing |
|---|
| .. | .. |
|---|
| 192 | 232 | * |
|---|
| 193 | 233 | * Returns zero on success, a negative error code on failure. |
|---|
| 194 | 234 | */ |
|---|
| 195 | | -int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map) |
|---|
| 235 | +int __init efi_memmap_install(struct efi_memory_map_data *data) |
|---|
| 196 | 236 | { |
|---|
| 197 | | - struct efi_memory_map_data data; |
|---|
| 198 | | - |
|---|
| 199 | 237 | efi_memmap_unmap(); |
|---|
| 200 | 238 | |
|---|
| 201 | | - data.phys_map = addr; |
|---|
| 202 | | - data.size = efi.memmap.desc_size * nr_map; |
|---|
| 203 | | - data.desc_version = efi.memmap.desc_version; |
|---|
| 204 | | - data.desc_size = efi.memmap.desc_size; |
|---|
| 205 | | - |
|---|
| 206 | | - return __efi_memmap_init(&data, efi.memmap.late); |
|---|
| 239 | + return __efi_memmap_init(data); |
|---|
| 207 | 240 | } |
|---|
| 208 | 241 | |
|---|
| 209 | 242 | /** |
|---|