blob: be642f12a927ff20193dd703974e98220c20e082 [file] [log] [blame]
Alexander Graf8623f922016-03-04 01:10:04 +01001/*
2 * EFI application memory management
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Alexander Graf8623f922016-03-04 01:10:04 +01009#include <common.h>
10#include <efi_loader.h>
11#include <malloc.h>
12#include <asm/global_data.h>
13#include <libfdt_env.h>
Alexander Grafde2a13b2016-03-30 16:38:29 +020014#include <linux/list_sort.h>
Alexander Graf8623f922016-03-04 01:10:04 +010015#include <inttypes.h>
16#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20struct efi_mem_list {
21 struct list_head link;
22 struct efi_mem_desc desc;
23};
24
Alexander Graf66d86232016-05-27 12:25:03 +020025#define EFI_CARVE_NO_OVERLAP -1
26#define EFI_CARVE_LOOP_AGAIN -2
27#define EFI_CARVE_OVERLAPS_NONRAM -3
28
Alexander Graf8623f922016-03-04 01:10:04 +010029/* This list contains all memory map items */
30LIST_HEAD(efi_mem);
31
Alexander Graf7c00a3c2016-05-11 18:25:48 +020032#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
33void *efi_bounce_buffer;
34#endif
35
Alexander Graf8623f922016-03-04 01:10:04 +010036/*
Alexander Grafde2a13b2016-03-30 16:38:29 +020037 * Sorts the memory list from highest address to lowest address
38 *
39 * When allocating memory we should always start from the highest
40 * address chunk, so sort the memory list such that the first list
41 * iterator gets the highest address and goes lower from there.
42 */
43static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
44{
45 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
46 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
47
48 if (mema->desc.physical_start == memb->desc.physical_start)
49 return 0;
50 else if (mema->desc.physical_start < memb->desc.physical_start)
51 return 1;
52 else
53 return -1;
54}
55
56static void efi_mem_sort(void)
57{
58 list_sort(NULL, &efi_mem, efi_mem_cmp);
59}
60
61/*
Alexander Graf8623f922016-03-04 01:10:04 +010062 * Unmaps all memory occupied by the carve_desc region from the
63 * list entry pointed to by map.
64 *
Stefan Brüns1b00dc12016-10-01 23:32:23 +020065 * Returns EFI_CARVE_NO_OVERLAP if the regions don't overlap.
66 * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap,
67 * and the map contains anything but free ram.
68 * (only when overlap_only_ram is true)
69 * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed
70 * again, as it has been altered
71 * Returns the number of overlapping pages. The pages are removed from
72 * the mapping list.
73 *
74 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
75 * to readd the already carved out pages to the mapping.
Alexander Graf8623f922016-03-04 01:10:04 +010076 */
77static int efi_mem_carve_out(struct efi_mem_list *map,
78 struct efi_mem_desc *carve_desc,
79 bool overlap_only_ram)
80{
81 struct efi_mem_list *newmap;
82 struct efi_mem_desc *map_desc = &map->desc;
83 uint64_t map_start = map_desc->physical_start;
84 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
85 uint64_t carve_start = carve_desc->physical_start;
86 uint64_t carve_end = carve_start +
87 (carve_desc->num_pages << EFI_PAGE_SHIFT);
88
89 /* check whether we're overlapping */
90 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf66d86232016-05-27 12:25:03 +020091 return EFI_CARVE_NO_OVERLAP;
Alexander Graf8623f922016-03-04 01:10:04 +010092
93 /* We're overlapping with non-RAM, warn the caller if desired */
94 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf66d86232016-05-27 12:25:03 +020095 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf8623f922016-03-04 01:10:04 +010096
97 /* Sanitize carve_start and carve_end to lie within our bounds */
98 carve_start = max(carve_start, map_start);
99 carve_end = min(carve_end, map_end);
100
101 /* Carving at the beginning of our map? Just move it! */
102 if (carve_start == map_start) {
103 if (map_end == carve_end) {
104 /* Full overlap, just remove map */
105 list_del(&map->link);
106 }
107
108 map_desc->physical_start = carve_end;
109 map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT;
Alexander Graf66d86232016-05-27 12:25:03 +0200110 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100111 }
112
113 /*
114 * Overlapping maps, just split the list map at carve_start,
115 * it will get moved or removed in the next iteration.
116 *
117 * [ map_desc |__carve_start__| newmap ]
118 */
119
120 /* Create a new map from [ carve_start ... map_end ] */
121 newmap = calloc(1, sizeof(*newmap));
122 newmap->desc = map->desc;
123 newmap->desc.physical_start = carve_start;
124 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
125 list_add_tail(&newmap->link, &efi_mem);
126
127 /* Shrink the map to [ map_start ... carve_start ] */
128 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
129
Alexander Graf66d86232016-05-27 12:25:03 +0200130 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf8623f922016-03-04 01:10:04 +0100131}
132
133uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
134 bool overlap_only_ram)
135{
136 struct list_head *lhandle;
137 struct efi_mem_list *newlist;
Alexander Graf66d86232016-05-27 12:25:03 +0200138 bool carve_again;
139 uint64_t carved_pages = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100140
Andreas Färber102f8bd2016-07-17 06:57:11 +0200141 debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
142 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
143
Alexander Graf8623f922016-03-04 01:10:04 +0100144 if (!pages)
145 return start;
146
147 newlist = calloc(1, sizeof(*newlist));
148 newlist->desc.type = memory_type;
149 newlist->desc.physical_start = start;
150 newlist->desc.virtual_start = start;
151 newlist->desc.num_pages = pages;
152
153 switch (memory_type) {
154 case EFI_RUNTIME_SERVICES_CODE:
155 case EFI_RUNTIME_SERVICES_DATA:
156 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
157 (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
158 break;
159 case EFI_MMAP_IO:
160 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
161 break;
162 default:
163 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
164 break;
165 }
166
167 /* Add our new map */
168 do {
Alexander Graf66d86232016-05-27 12:25:03 +0200169 carve_again = false;
Alexander Graf8623f922016-03-04 01:10:04 +0100170 list_for_each(lhandle, &efi_mem) {
171 struct efi_mem_list *lmem;
172 int r;
173
174 lmem = list_entry(lhandle, struct efi_mem_list, link);
175 r = efi_mem_carve_out(lmem, &newlist->desc,
176 overlap_only_ram);
Alexander Graf66d86232016-05-27 12:25:03 +0200177 switch (r) {
178 case EFI_CARVE_OVERLAPS_NONRAM:
179 /*
180 * The user requested to only have RAM overlaps,
181 * but we hit a non-RAM region. Error out.
182 */
Alexander Graf8623f922016-03-04 01:10:04 +0100183 return 0;
Alexander Graf66d86232016-05-27 12:25:03 +0200184 case EFI_CARVE_NO_OVERLAP:
185 /* Just ignore this list entry */
186 break;
187 case EFI_CARVE_LOOP_AGAIN:
188 /*
189 * We split an entry, but need to loop through
190 * the list again to actually carve it.
191 */
192 carve_again = true;
193 break;
194 default:
195 /* We carved a number of pages */
196 carved_pages += r;
197 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100198 break;
199 }
Alexander Graf66d86232016-05-27 12:25:03 +0200200
201 if (carve_again) {
202 /* The list changed, we need to start over */
203 break;
204 }
Alexander Graf8623f922016-03-04 01:10:04 +0100205 }
Alexander Graf66d86232016-05-27 12:25:03 +0200206 } while (carve_again);
207
208 if (overlap_only_ram && (carved_pages != pages)) {
209 /*
210 * The payload wanted to have RAM overlaps, but we overlapped
211 * with an unallocated region. Error out.
212 */
213 return 0;
214 }
Alexander Graf8623f922016-03-04 01:10:04 +0100215
216 /* Add our new map */
217 list_add_tail(&newlist->link, &efi_mem);
218
Alexander Grafde2a13b2016-03-30 16:38:29 +0200219 /* And make sure memory is listed in descending order */
220 efi_mem_sort();
221
Alexander Graf8623f922016-03-04 01:10:04 +0100222 return start;
223}
224
225static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
226{
227 struct list_head *lhandle;
228
229 list_for_each(lhandle, &efi_mem) {
230 struct efi_mem_list *lmem = list_entry(lhandle,
231 struct efi_mem_list, link);
232 struct efi_mem_desc *desc = &lmem->desc;
233 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
234 uint64_t desc_end = desc->physical_start + desc_len;
235 uint64_t curmax = min(max_addr, desc_end);
236 uint64_t ret = curmax - len;
237
238 /* We only take memory from free RAM */
239 if (desc->type != EFI_CONVENTIONAL_MEMORY)
240 continue;
241
242 /* Out of bounds for max_addr */
243 if ((ret + len) > max_addr)
244 continue;
245
246 /* Out of bounds for upper map limit */
247 if ((ret + len) > desc_end)
248 continue;
249
250 /* Out of bounds for lower map limit */
251 if (ret < desc->physical_start)
252 continue;
253
254 /* Return the highest address in this map within bounds */
255 return ret;
256 }
257
258 return 0;
259}
260
261efi_status_t efi_allocate_pages(int type, int memory_type,
262 unsigned long pages, uint64_t *memory)
263{
264 u64 len = pages << EFI_PAGE_SHIFT;
265 efi_status_t r = EFI_SUCCESS;
266 uint64_t addr;
267
268 switch (type) {
269 case 0:
270 /* Any page */
Andreas Färberd40f71e2016-04-13 14:04:38 +0200271 addr = efi_find_free_memory(len, gd->start_addr_sp);
Alexander Graf8623f922016-03-04 01:10:04 +0100272 if (!addr) {
273 r = EFI_NOT_FOUND;
274 break;
275 }
276 break;
277 case 1:
278 /* Max address */
279 addr = efi_find_free_memory(len, *memory);
280 if (!addr) {
281 r = EFI_NOT_FOUND;
282 break;
283 }
284 break;
285 case 2:
286 /* Exact address, reserve it. The addr is already in *memory. */
287 addr = *memory;
288 break;
289 default:
290 /* UEFI doesn't specify other allocation types */
291 r = EFI_INVALID_PARAMETER;
292 break;
293 }
294
295 if (r == EFI_SUCCESS) {
296 uint64_t ret;
297
298 /* Reserve that map in our memory maps */
299 ret = efi_add_memory_map(addr, pages, memory_type, true);
300 if (ret == addr) {
301 *memory = addr;
302 } else {
303 /* Map would overlap, bail out */
304 r = EFI_OUT_OF_RESOURCES;
305 }
306 }
307
308 return r;
309}
310
311void *efi_alloc(uint64_t len, int memory_type)
312{
313 uint64_t ret = 0;
314 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
315 efi_status_t r;
316
317 r = efi_allocate_pages(0, memory_type, pages, &ret);
318 if (r == EFI_SUCCESS)
319 return (void*)(uintptr_t)ret;
320
321 return NULL;
322}
323
324efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
325{
326 /* We don't free, let's cross our fingers we have plenty RAM */
327 return EFI_SUCCESS;
328}
329
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200330efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
331 void **buffer)
332{
333 efi_status_t r;
334 efi_physical_addr_t t;
335 u64 num_pages = (size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
336
337 r = efi_allocate_pages(0, pool_type, num_pages, &t);
338 if (r == EFI_SUCCESS)
339 *buffer = (void *)(uintptr_t)t;
340
341 return r;
342}
343
Alexander Graf8623f922016-03-04 01:10:04 +0100344efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
345 struct efi_mem_desc *memory_map,
346 unsigned long *map_key,
347 unsigned long *descriptor_size,
348 uint32_t *descriptor_version)
349{
350 ulong map_size = 0;
Alexander Graf9b591022016-04-11 23:51:02 +0200351 int map_entries = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100352 struct list_head *lhandle;
Stefan Brünsf6258552016-10-09 22:17:07 +0200353 unsigned long provided_map_size = *memory_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100354
355 list_for_each(lhandle, &efi_mem)
Alexander Graf9b591022016-04-11 23:51:02 +0200356 map_entries++;
357
358 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100359
360 *memory_map_size = map_size;
361
362 if (descriptor_size)
363 *descriptor_size = sizeof(struct efi_mem_desc);
364
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200365 if (descriptor_version)
366 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
367
Stefan Brünsf6258552016-10-09 22:17:07 +0200368 if (provided_map_size < map_size)
Alexander Graf8623f922016-03-04 01:10:04 +0100369 return EFI_BUFFER_TOO_SMALL;
370
371 /* Copy list into array */
372 if (memory_map) {
Alexander Graf9b591022016-04-11 23:51:02 +0200373 /* Return the list in ascending order */
374 memory_map = &memory_map[map_entries - 1];
Alexander Graf8623f922016-03-04 01:10:04 +0100375 list_for_each(lhandle, &efi_mem) {
376 struct efi_mem_list *lmem;
377
378 lmem = list_entry(lhandle, struct efi_mem_list, link);
379 *memory_map = lmem->desc;
Alexander Graf9b591022016-04-11 23:51:02 +0200380 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100381 }
382 }
383
384 return EFI_SUCCESS;
385}
386
387int efi_memory_init(void)
388{
Andreas Färberd40f71e2016-04-13 14:04:38 +0200389 unsigned long runtime_start, runtime_end, runtime_pages;
390 unsigned long uboot_start, uboot_pages;
391 unsigned long uboot_stack_size = 16 * 1024 * 1024;
Alexander Graf8623f922016-03-04 01:10:04 +0100392 int i;
393
394 /* Add RAM */
395 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
396 u64 ram_start = gd->bd->bi_dram[i].start;
397 u64 ram_size = gd->bd->bi_dram[i].size;
398 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
399 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
400
401 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
402 false);
403 }
404
405 /* Add U-Boot */
406 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
407 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
408 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
409
410 /* Add Runtime Services */
411 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
412 runtime_end = (ulong)&__efi_runtime_stop;
413 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
414 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
415 efi_add_memory_map(runtime_start, runtime_pages,
416 EFI_RUNTIME_SERVICES_CODE, false);
417
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200418#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
419 /* Request a 32bit 64MB bounce buffer region */
420 uint64_t efi_bounce_buffer_addr = 0xffffffff;
421
422 if (efi_allocate_pages(1, EFI_LOADER_DATA,
423 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
424 &efi_bounce_buffer_addr) != EFI_SUCCESS)
425 return -1;
426
427 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
428#endif
429
Alexander Graf8623f922016-03-04 01:10:04 +0100430 return 0;
431}