]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_memory.c
efi: Update some comments related to smbios tables
[u-boot] / lib / efi_loader / efi_memory.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI application memory management
4  *
5  *  Copyright (c) 2016 Alexander Graf
6  */
7
8 #include <common.h>
9 #include <efi_loader.h>
10 #include <inttypes.h>
11 #include <malloc.h>
12 #include <watchdog.h>
13 #include <linux/list_sort.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 struct efi_mem_list {
18         struct list_head link;
19         struct efi_mem_desc desc;
20 };
21
22 #define EFI_CARVE_NO_OVERLAP            -1
23 #define EFI_CARVE_LOOP_AGAIN            -2
24 #define EFI_CARVE_OVERLAPS_NONRAM       -3
25
26 /* This list contains all memory map items */
27 LIST_HEAD(efi_mem);
28
29 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
30 void *efi_bounce_buffer;
31 #endif
32
33 /*
34  * U-Boot services each EFI AllocatePool request as a separate
35  * (multiple) page allocation.  We have to track the number of pages
36  * to be able to free the correct amount later.
37  * EFI requires 8 byte alignment for pool allocations, so we can
38  * prepend each allocation with an 64 bit header tracking the
39  * allocation size, and hand out the remainder to the caller.
40  */
41 struct efi_pool_allocation {
42         u64 num_pages;
43         char data[] __aligned(ARCH_DMA_MINALIGN);
44 };
45
46 /*
47  * Sorts the memory list from highest address to lowest address
48  *
49  * When allocating memory we should always start from the highest
50  * address chunk, so sort the memory list such that the first list
51  * iterator gets the highest address and goes lower from there.
52  */
53 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
54 {
55         struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
56         struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
57
58         if (mema->desc.physical_start == memb->desc.physical_start)
59                 return 0;
60         else if (mema->desc.physical_start < memb->desc.physical_start)
61                 return 1;
62         else
63                 return -1;
64 }
65
66 static void efi_mem_sort(void)
67 {
68         list_sort(NULL, &efi_mem, efi_mem_cmp);
69 }
70
71 /*
72  * Unmaps all memory occupied by the carve_desc region from the
73  * list entry pointed to by map.
74  *
75  * Returns EFI_CARVE_NO_OVERLAP if the regions don't overlap.
76  * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap,
77  *    and the map contains anything but free ram.
78  *    (only when overlap_only_ram is true)
79  * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed
80  *    again, as it has been altered
81  * Returns the number of overlapping pages. The pages are removed from
82  *     the mapping list.
83  *
84  * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
85  * to readd the already carved out pages to the mapping.
86  */
87 static int efi_mem_carve_out(struct efi_mem_list *map,
88                              struct efi_mem_desc *carve_desc,
89                              bool overlap_only_ram)
90 {
91         struct efi_mem_list *newmap;
92         struct efi_mem_desc *map_desc = &map->desc;
93         uint64_t map_start = map_desc->physical_start;
94         uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
95         uint64_t carve_start = carve_desc->physical_start;
96         uint64_t carve_end = carve_start +
97                              (carve_desc->num_pages << EFI_PAGE_SHIFT);
98
99         /* check whether we're overlapping */
100         if ((carve_end <= map_start) || (carve_start >= map_end))
101                 return EFI_CARVE_NO_OVERLAP;
102
103         /* We're overlapping with non-RAM, warn the caller if desired */
104         if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
105                 return EFI_CARVE_OVERLAPS_NONRAM;
106
107         /* Sanitize carve_start and carve_end to lie within our bounds */
108         carve_start = max(carve_start, map_start);
109         carve_end = min(carve_end, map_end);
110
111         /* Carving at the beginning of our map? Just move it! */
112         if (carve_start == map_start) {
113                 if (map_end == carve_end) {
114                         /* Full overlap, just remove map */
115                         list_del(&map->link);
116                         free(map);
117                 } else {
118                         map->desc.physical_start = carve_end;
119                         map->desc.num_pages = (map_end - carve_end)
120                                               >> EFI_PAGE_SHIFT;
121                 }
122
123                 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
124         }
125
126         /*
127          * Overlapping maps, just split the list map at carve_start,
128          * it will get moved or removed in the next iteration.
129          *
130          * [ map_desc |__carve_start__| newmap ]
131          */
132
133         /* Create a new map from [ carve_start ... map_end ] */
134         newmap = calloc(1, sizeof(*newmap));
135         newmap->desc = map->desc;
136         newmap->desc.physical_start = carve_start;
137         newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
138         /* Insert before current entry (descending address order) */
139         list_add_tail(&newmap->link, &map->link);
140
141         /* Shrink the map to [ map_start ... carve_start ] */
142         map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
143
144         return EFI_CARVE_LOOP_AGAIN;
145 }
146
147 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
148                             bool overlap_only_ram)
149 {
150         struct list_head *lhandle;
151         struct efi_mem_list *newlist;
152         bool carve_again;
153         uint64_t carved_pages = 0;
154
155         debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
156               start, pages, memory_type, overlap_only_ram ? "yes" : "no");
157
158         if (!pages)
159                 return start;
160
161         newlist = calloc(1, sizeof(*newlist));
162         newlist->desc.type = memory_type;
163         newlist->desc.physical_start = start;
164         newlist->desc.virtual_start = start;
165         newlist->desc.num_pages = pages;
166
167         switch (memory_type) {
168         case EFI_RUNTIME_SERVICES_CODE:
169         case EFI_RUNTIME_SERVICES_DATA:
170                 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
171                                           (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
172                 break;
173         case EFI_MMAP_IO:
174                 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
175                 break;
176         default:
177                 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
178                 break;
179         }
180
181         /* Add our new map */
182         do {
183                 carve_again = false;
184                 list_for_each(lhandle, &efi_mem) {
185                         struct efi_mem_list *lmem;
186                         int r;
187
188                         lmem = list_entry(lhandle, struct efi_mem_list, link);
189                         r = efi_mem_carve_out(lmem, &newlist->desc,
190                                               overlap_only_ram);
191                         switch (r) {
192                         case EFI_CARVE_OVERLAPS_NONRAM:
193                                 /*
194                                  * The user requested to only have RAM overlaps,
195                                  * but we hit a non-RAM region. Error out.
196                                  */
197                                 return 0;
198                         case EFI_CARVE_NO_OVERLAP:
199                                 /* Just ignore this list entry */
200                                 break;
201                         case EFI_CARVE_LOOP_AGAIN:
202                                 /*
203                                  * We split an entry, but need to loop through
204                                  * the list again to actually carve it.
205                                  */
206                                 carve_again = true;
207                                 break;
208                         default:
209                                 /* We carved a number of pages */
210                                 carved_pages += r;
211                                 carve_again = true;
212                                 break;
213                         }
214
215                         if (carve_again) {
216                                 /* The list changed, we need to start over */
217                                 break;
218                         }
219                 }
220         } while (carve_again);
221
222         if (overlap_only_ram && (carved_pages != pages)) {
223                 /*
224                  * The payload wanted to have RAM overlaps, but we overlapped
225                  * with an unallocated region. Error out.
226                  */
227                 return 0;
228         }
229
230         /* Add our new map */
231         list_add_tail(&newlist->link, &efi_mem);
232
233         /* And make sure memory is listed in descending order */
234         efi_mem_sort();
235
236         return start;
237 }
238
239 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
240 {
241         struct list_head *lhandle;
242
243         list_for_each(lhandle, &efi_mem) {
244                 struct efi_mem_list *lmem = list_entry(lhandle,
245                         struct efi_mem_list, link);
246                 struct efi_mem_desc *desc = &lmem->desc;
247                 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
248                 uint64_t desc_end = desc->physical_start + desc_len;
249                 uint64_t curmax = min(max_addr, desc_end);
250                 uint64_t ret = curmax - len;
251
252                 /* We only take memory from free RAM */
253                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
254                         continue;
255
256                 /* Out of bounds for max_addr */
257                 if ((ret + len) > max_addr)
258                         continue;
259
260                 /* Out of bounds for upper map limit */
261                 if ((ret + len) > desc_end)
262                         continue;
263
264                 /* Out of bounds for lower map limit */
265                 if (ret < desc->physical_start)
266                         continue;
267
268                 /* Return the highest address in this map within bounds */
269                 return ret;
270         }
271
272         return 0;
273 }
274
275 /*
276  * Allocate memory pages.
277  *
278  * @type                type of allocation to be performed
279  * @memory_type         usage type of the allocated memory
280  * @pages               number of pages to be allocated
281  * @memory              allocated memory
282  * @return              status code
283  */
284 efi_status_t efi_allocate_pages(int type, int memory_type,
285                                 efi_uintn_t pages, uint64_t *memory)
286 {
287         u64 len = pages << EFI_PAGE_SHIFT;
288         efi_status_t r = EFI_SUCCESS;
289         uint64_t addr;
290
291         switch (type) {
292         case EFI_ALLOCATE_ANY_PAGES:
293                 /* Any page */
294                 addr = efi_find_free_memory(len, gd->start_addr_sp);
295                 if (!addr) {
296                         r = EFI_NOT_FOUND;
297                         break;
298                 }
299                 break;
300         case EFI_ALLOCATE_MAX_ADDRESS:
301                 /* Max address */
302                 addr = efi_find_free_memory(len, *memory);
303                 if (!addr) {
304                         r = EFI_NOT_FOUND;
305                         break;
306                 }
307                 break;
308         case EFI_ALLOCATE_ADDRESS:
309                 /* Exact address, reserve it. The addr is already in *memory. */
310                 addr = *memory;
311                 break;
312         default:
313                 /* UEFI doesn't specify other allocation types */
314                 r = EFI_INVALID_PARAMETER;
315                 break;
316         }
317
318         if (r == EFI_SUCCESS) {
319                 uint64_t ret;
320
321                 /* Reserve that map in our memory maps */
322                 ret = efi_add_memory_map(addr, pages, memory_type, true);
323                 if (ret == addr) {
324                         *memory = addr;
325                 } else {
326                         /* Map would overlap, bail out */
327                         r = EFI_OUT_OF_RESOURCES;
328                 }
329         }
330
331         return r;
332 }
333
334 void *efi_alloc(uint64_t len, int memory_type)
335 {
336         uint64_t ret = 0;
337         uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
338         efi_status_t r;
339
340         r = efi_allocate_pages(0, memory_type, pages, &ret);
341         if (r == EFI_SUCCESS)
342                 return (void*)(uintptr_t)ret;
343
344         return NULL;
345 }
346
347 /*
348  * Free memory pages.
349  *
350  * @memory      start of the memory area to be freed
351  * @pages       number of pages to be freed
352  * @return      status code
353  */
354 efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
355 {
356         uint64_t r = 0;
357
358         r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
359         /* Merging of adjacent free regions is missing */
360
361         if (r == memory)
362                 return EFI_SUCCESS;
363
364         return EFI_NOT_FOUND;
365 }
366
367 /*
368  * Allocate memory from pool.
369  *
370  * @pool_type   type of the pool from which memory is to be allocated
371  * @size        number of bytes to be allocated
372  * @buffer      allocated memory
373  * @return      status code
374  */
375 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
376 {
377         efi_status_t r;
378         efi_physical_addr_t t;
379         u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
380                          EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
381
382         if (size == 0) {
383                 *buffer = NULL;
384                 return EFI_SUCCESS;
385         }
386
387         r = efi_allocate_pages(0, pool_type, num_pages, &t);
388
389         if (r == EFI_SUCCESS) {
390                 struct efi_pool_allocation *alloc = (void *)(uintptr_t)t;
391                 alloc->num_pages = num_pages;
392                 *buffer = alloc->data;
393         }
394
395         return r;
396 }
397
398 /*
399  * Free memory from pool.
400  *
401  * @buffer      start of memory to be freed
402  * @return      status code
403  */
404 efi_status_t efi_free_pool(void *buffer)
405 {
406         efi_status_t r;
407         struct efi_pool_allocation *alloc;
408
409         if (buffer == NULL)
410                 return EFI_INVALID_PARAMETER;
411
412         alloc = container_of(buffer, struct efi_pool_allocation, data);
413         /* Sanity check, was the supplied address returned by allocate_pool */
414         assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
415
416         r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
417
418         return r;
419 }
420
421 /*
422  * Get map describing memory usage.
423  *
424  * @memory_map_size     on entry the size, in bytes, of the memory map buffer,
425  *                      on exit the size of the copied memory map
426  * @memory_map          buffer to which the memory map is written
427  * @map_key             key for the memory map
428  * @descriptor_size     size of an individual memory descriptor
429  * @descriptor_version  version number of the memory descriptor structure
430  * @return              status code
431  */
432 efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
433                                 struct efi_mem_desc *memory_map,
434                                 efi_uintn_t *map_key,
435                                 efi_uintn_t *descriptor_size,
436                                 uint32_t *descriptor_version)
437 {
438         efi_uintn_t map_size = 0;
439         int map_entries = 0;
440         struct list_head *lhandle;
441         efi_uintn_t provided_map_size = *memory_map_size;
442
443         list_for_each(lhandle, &efi_mem)
444                 map_entries++;
445
446         map_size = map_entries * sizeof(struct efi_mem_desc);
447
448         *memory_map_size = map_size;
449
450         if (provided_map_size < map_size)
451                 return EFI_BUFFER_TOO_SMALL;
452
453         if (descriptor_size)
454                 *descriptor_size = sizeof(struct efi_mem_desc);
455
456         if (descriptor_version)
457                 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
458
459         /* Copy list into array */
460         if (memory_map) {
461                 /* Return the list in ascending order */
462                 memory_map = &memory_map[map_entries - 1];
463                 list_for_each(lhandle, &efi_mem) {
464                         struct efi_mem_list *lmem;
465
466                         lmem = list_entry(lhandle, struct efi_mem_list, link);
467                         *memory_map = lmem->desc;
468                         memory_map--;
469                 }
470         }
471
472         *map_key = 0;
473
474         return EFI_SUCCESS;
475 }
476
477 __weak void efi_add_known_memory(void)
478 {
479         int i;
480
481         /* Add RAM */
482         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
483                 u64 ram_start = gd->bd->bi_dram[i].start;
484                 u64 ram_size = gd->bd->bi_dram[i].size;
485                 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
486                 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
487
488                 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
489                                    false);
490         }
491 }
492
493 int efi_memory_init(void)
494 {
495         unsigned long runtime_start, runtime_end, runtime_pages;
496         unsigned long uboot_start, uboot_pages;
497         unsigned long uboot_stack_size = 16 * 1024 * 1024;
498
499         efi_add_known_memory();
500
501         /* Add U-Boot */
502         uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
503         uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
504         efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
505
506         /* Add Runtime Services */
507         runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
508         runtime_end = (ulong)&__efi_runtime_stop;
509         runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
510         runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
511         efi_add_memory_map(runtime_start, runtime_pages,
512                            EFI_RUNTIME_SERVICES_CODE, false);
513
514 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
515         /* Request a 32bit 64MB bounce buffer region */
516         uint64_t efi_bounce_buffer_addr = 0xffffffff;
517
518         if (efi_allocate_pages(1, EFI_LOADER_DATA,
519                                (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
520                                &efi_bounce_buffer_addr) != EFI_SUCCESS)
521                 return -1;
522
523         efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
524 #endif
525
526         return 0;
527 }