Merge branch 'master' of git://www.denx.de/git/u-boot-imx
[platform/kernel/u-boot.git] / lib / efi_loader / efi_memory.c
1 /*
2  *  EFI application memory management
3  *
4  *  Copyright (c) 2016 Alexander Graf
5  *
6  *  SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <efi_loader.h>
11 #include <malloc.h>
12 #include <asm/global_data.h>
13 #include <libfdt_env.h>
14 #include <linux/list_sort.h>
15 #include <inttypes.h>
16 #include <watchdog.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 struct efi_mem_list {
21         struct list_head link;
22         struct efi_mem_desc desc;
23 };
24
25 #define EFI_CARVE_NO_OVERLAP            -1
26 #define EFI_CARVE_LOOP_AGAIN            -2
27 #define EFI_CARVE_OVERLAPS_NONRAM       -3
28
29 /* This list contains all memory map items */
30 LIST_HEAD(efi_mem);
31
32 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
33 void *efi_bounce_buffer;
34 #endif
35
36 /*
37  * 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  */
43 static 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
56 static void efi_mem_sort(void)
57 {
58         list_sort(NULL, &efi_mem, efi_mem_cmp);
59 }
60
61 /*
62  * Unmaps all memory occupied by the carve_desc region from the
63  * list entry pointed to by map.
64  *
65  * Returns 1 if carving was performed or 0 if the regions don't overlap.
66  * Returns -1 if it would affect non-RAM regions but overlap_only_ram is set.
67  * Carving is only guaranteed to complete when all regions return 0.
68  */
69 static int efi_mem_carve_out(struct efi_mem_list *map,
70                              struct efi_mem_desc *carve_desc,
71                              bool overlap_only_ram)
72 {
73         struct efi_mem_list *newmap;
74         struct efi_mem_desc *map_desc = &map->desc;
75         uint64_t map_start = map_desc->physical_start;
76         uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
77         uint64_t carve_start = carve_desc->physical_start;
78         uint64_t carve_end = carve_start +
79                              (carve_desc->num_pages << EFI_PAGE_SHIFT);
80
81         /* check whether we're overlapping */
82         if ((carve_end <= map_start) || (carve_start >= map_end))
83                 return EFI_CARVE_NO_OVERLAP;
84
85         /* We're overlapping with non-RAM, warn the caller if desired */
86         if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
87                 return EFI_CARVE_OVERLAPS_NONRAM;
88
89         /* Sanitize carve_start and carve_end to lie within our bounds */
90         carve_start = max(carve_start, map_start);
91         carve_end = min(carve_end, map_end);
92
93         /* Carving at the beginning of our map? Just move it! */
94         if (carve_start == map_start) {
95                 if (map_end == carve_end) {
96                         /* Full overlap, just remove map */
97                         list_del(&map->link);
98                 }
99
100                 map_desc->physical_start = carve_end;
101                 map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT;
102                 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
103         }
104
105         /*
106          * Overlapping maps, just split the list map at carve_start,
107          * it will get moved or removed in the next iteration.
108          *
109          * [ map_desc |__carve_start__| newmap ]
110          */
111
112         /* Create a new map from [ carve_start ... map_end ] */
113         newmap = calloc(1, sizeof(*newmap));
114         newmap->desc = map->desc;
115         newmap->desc.physical_start = carve_start;
116         newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
117         list_add_tail(&newmap->link, &efi_mem);
118
119         /* Shrink the map to [ map_start ... carve_start ] */
120         map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
121
122         return EFI_CARVE_LOOP_AGAIN;
123 }
124
125 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
126                             bool overlap_only_ram)
127 {
128         struct list_head *lhandle;
129         struct efi_mem_list *newlist;
130         bool carve_again;
131         uint64_t carved_pages = 0;
132
133         debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
134               start, pages, memory_type, overlap_only_ram ? "yes" : "no");
135
136         if (!pages)
137                 return start;
138
139         newlist = calloc(1, sizeof(*newlist));
140         newlist->desc.type = memory_type;
141         newlist->desc.physical_start = start;
142         newlist->desc.virtual_start = start;
143         newlist->desc.num_pages = pages;
144
145         switch (memory_type) {
146         case EFI_RUNTIME_SERVICES_CODE:
147         case EFI_RUNTIME_SERVICES_DATA:
148                 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
149                                           (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
150                 break;
151         case EFI_MMAP_IO:
152                 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
153                 break;
154         default:
155                 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
156                 break;
157         }
158
159         /* Add our new map */
160         do {
161                 carve_again = false;
162                 list_for_each(lhandle, &efi_mem) {
163                         struct efi_mem_list *lmem;
164                         int r;
165
166                         lmem = list_entry(lhandle, struct efi_mem_list, link);
167                         r = efi_mem_carve_out(lmem, &newlist->desc,
168                                               overlap_only_ram);
169                         switch (r) {
170                         case EFI_CARVE_OVERLAPS_NONRAM:
171                                 /*
172                                  * The user requested to only have RAM overlaps,
173                                  * but we hit a non-RAM region. Error out.
174                                  */
175                                 return 0;
176                         case EFI_CARVE_NO_OVERLAP:
177                                 /* Just ignore this list entry */
178                                 break;
179                         case EFI_CARVE_LOOP_AGAIN:
180                                 /*
181                                  * We split an entry, but need to loop through
182                                  * the list again to actually carve it.
183                                  */
184                                 carve_again = true;
185                                 break;
186                         default:
187                                 /* We carved a number of pages */
188                                 carved_pages += r;
189                                 carve_again = true;
190                                 break;
191                         }
192
193                         if (carve_again) {
194                                 /* The list changed, we need to start over */
195                                 break;
196                         }
197                 }
198         } while (carve_again);
199
200         if (overlap_only_ram && (carved_pages != pages)) {
201                 /*
202                  * The payload wanted to have RAM overlaps, but we overlapped
203                  * with an unallocated region. Error out.
204                  */
205                 return 0;
206         }
207
208         /* Add our new map */
209         list_add_tail(&newlist->link, &efi_mem);
210
211         /* And make sure memory is listed in descending order */
212         efi_mem_sort();
213
214         return start;
215 }
216
217 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
218 {
219         struct list_head *lhandle;
220
221         list_for_each(lhandle, &efi_mem) {
222                 struct efi_mem_list *lmem = list_entry(lhandle,
223                         struct efi_mem_list, link);
224                 struct efi_mem_desc *desc = &lmem->desc;
225                 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
226                 uint64_t desc_end = desc->physical_start + desc_len;
227                 uint64_t curmax = min(max_addr, desc_end);
228                 uint64_t ret = curmax - len;
229
230                 /* We only take memory from free RAM */
231                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
232                         continue;
233
234                 /* Out of bounds for max_addr */
235                 if ((ret + len) > max_addr)
236                         continue;
237
238                 /* Out of bounds for upper map limit */
239                 if ((ret + len) > desc_end)
240                         continue;
241
242                 /* Out of bounds for lower map limit */
243                 if (ret < desc->physical_start)
244                         continue;
245
246                 /* Return the highest address in this map within bounds */
247                 return ret;
248         }
249
250         return 0;
251 }
252
253 efi_status_t efi_allocate_pages(int type, int memory_type,
254                                 unsigned long pages, uint64_t *memory)
255 {
256         u64 len = pages << EFI_PAGE_SHIFT;
257         efi_status_t r = EFI_SUCCESS;
258         uint64_t addr;
259
260         switch (type) {
261         case 0:
262                 /* Any page */
263                 addr = efi_find_free_memory(len, gd->start_addr_sp);
264                 if (!addr) {
265                         r = EFI_NOT_FOUND;
266                         break;
267                 }
268                 break;
269         case 1:
270                 /* Max address */
271                 addr = efi_find_free_memory(len, *memory);
272                 if (!addr) {
273                         r = EFI_NOT_FOUND;
274                         break;
275                 }
276                 break;
277         case 2:
278                 /* Exact address, reserve it. The addr is already in *memory. */
279                 addr = *memory;
280                 break;
281         default:
282                 /* UEFI doesn't specify other allocation types */
283                 r = EFI_INVALID_PARAMETER;
284                 break;
285         }
286
287         if (r == EFI_SUCCESS) {
288                 uint64_t ret;
289
290                 /* Reserve that map in our memory maps */
291                 ret = efi_add_memory_map(addr, pages, memory_type, true);
292                 if (ret == addr) {
293                         *memory = addr;
294                 } else {
295                         /* Map would overlap, bail out */
296                         r = EFI_OUT_OF_RESOURCES;
297                 }
298         }
299
300         return r;
301 }
302
303 void *efi_alloc(uint64_t len, int memory_type)
304 {
305         uint64_t ret = 0;
306         uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
307         efi_status_t r;
308
309         r = efi_allocate_pages(0, memory_type, pages, &ret);
310         if (r == EFI_SUCCESS)
311                 return (void*)(uintptr_t)ret;
312
313         return NULL;
314 }
315
316 efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
317 {
318         /* We don't free, let's cross our fingers we have plenty RAM */
319         return EFI_SUCCESS;
320 }
321
322 efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
323                                struct efi_mem_desc *memory_map,
324                                unsigned long *map_key,
325                                unsigned long *descriptor_size,
326                                uint32_t *descriptor_version)
327 {
328         ulong map_size = 0;
329         int map_entries = 0;
330         struct list_head *lhandle;
331
332         list_for_each(lhandle, &efi_mem)
333                 map_entries++;
334
335         map_size = map_entries * sizeof(struct efi_mem_desc);
336
337         *memory_map_size = map_size;
338
339         if (descriptor_size)
340                 *descriptor_size = sizeof(struct efi_mem_desc);
341
342         if (*memory_map_size < map_size)
343                 return EFI_BUFFER_TOO_SMALL;
344
345         /* Copy list into array */
346         if (memory_map) {
347                 /* Return the list in ascending order */
348                 memory_map = &memory_map[map_entries - 1];
349                 list_for_each(lhandle, &efi_mem) {
350                         struct efi_mem_list *lmem;
351
352                         lmem = list_entry(lhandle, struct efi_mem_list, link);
353                         *memory_map = lmem->desc;
354                         memory_map--;
355                 }
356         }
357
358         return EFI_SUCCESS;
359 }
360
361 int efi_memory_init(void)
362 {
363         unsigned long runtime_start, runtime_end, runtime_pages;
364         unsigned long uboot_start, uboot_pages;
365         unsigned long uboot_stack_size = 16 * 1024 * 1024;
366         int i;
367
368         /* Add RAM */
369         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
370                 u64 ram_start = gd->bd->bi_dram[i].start;
371                 u64 ram_size = gd->bd->bi_dram[i].size;
372                 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
373                 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
374
375                 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
376                                    false);
377         }
378
379         /* Add U-Boot */
380         uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
381         uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
382         efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
383
384         /* Add Runtime Services */
385         runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
386         runtime_end = (ulong)&__efi_runtime_stop;
387         runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
388         runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
389         efi_add_memory_map(runtime_start, runtime_pages,
390                            EFI_RUNTIME_SERVICES_CODE, false);
391
392 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
393         /* Request a 32bit 64MB bounce buffer region */
394         uint64_t efi_bounce_buffer_addr = 0xffffffff;
395
396         if (efi_allocate_pages(1, EFI_LOADER_DATA,
397                                (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
398                                &efi_bounce_buffer_addr) != EFI_SUCCESS)
399                 return -1;
400
401         efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
402 #endif
403
404         return 0;
405 }