]>
Commit | Line | Data |
---|---|---|
f739fcd8 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
5d00995c AG |
2 | /* |
3 | * EFI application memory management | |
4 | * | |
5 | * Copyright (c) 2016 Alexander Graf | |
5d00995c AG |
6 | */ |
7 | ||
5d00995c AG |
8 | #include <common.h> |
9 | #include <efi_loader.h> | |
10 | #include <malloc.h> | |
282a06cb | 11 | #include <mapmem.h> |
bdecaebd | 12 | #include <watchdog.h> |
38ce65e1 | 13 | #include <linux/list_sort.h> |
7a82c305 | 14 | #include <linux/sizes.h> |
5d00995c AG |
15 | |
16 | DECLARE_GLOBAL_DATA_PTR; | |
17 | ||
2c3ec289 HS |
18 | /* Magic number identifying memory allocated from pool */ |
19 | #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2 | |
20 | ||
1fcb7ea2 HS |
21 | efi_uintn_t efi_memory_map_key; |
22 | ||
5d00995c AG |
23 | struct efi_mem_list { |
24 | struct list_head link; | |
25 | struct efi_mem_desc desc; | |
26 | }; | |
27 | ||
74c16acc AG |
28 | #define EFI_CARVE_NO_OVERLAP -1 |
29 | #define EFI_CARVE_LOOP_AGAIN -2 | |
30 | #define EFI_CARVE_OVERLAPS_NONRAM -3 | |
31 | ||
5d00995c AG |
32 | /* This list contains all memory map items */ |
33 | LIST_HEAD(efi_mem); | |
34 | ||
51735ae0 AG |
35 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
36 | void *efi_bounce_buffer; | |
37 | #endif | |
38 | ||
2c3ec289 HS |
39 | /** |
40 | * efi_pool_allocation - memory block allocated from pool | |
41 | * | |
42 | * @num_pages: number of pages allocated | |
43 | * @checksum: checksum | |
44 | * | |
42417bc8 SB |
45 | * U-Boot services each EFI AllocatePool request as a separate |
46 | * (multiple) page allocation. We have to track the number of pages | |
47 | * to be able to free the correct amount later. | |
48 | * EFI requires 8 byte alignment for pool allocations, so we can | |
49 | * prepend each allocation with an 64 bit header tracking the | |
50 | * allocation size, and hand out the remainder to the caller. | |
51 | */ | |
52 | struct efi_pool_allocation { | |
53 | u64 num_pages; | |
2c3ec289 | 54 | u64 checksum; |
946160f3 | 55 | char data[] __aligned(ARCH_DMA_MINALIGN); |
42417bc8 SB |
56 | }; |
57 | ||
2c3ec289 HS |
58 | /** |
59 | * checksum() - calculate checksum for memory allocated from pool | |
60 | * | |
61 | * @alloc: allocation header | |
62 | * Return: checksum, always non-zero | |
63 | */ | |
64 | static u64 checksum(struct efi_pool_allocation *alloc) | |
65 | { | |
66 | u64 addr = (uintptr_t)alloc; | |
67 | u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^ | |
68 | EFI_ALLOC_POOL_MAGIC; | |
69 | if (!ret) | |
70 | ++ret; | |
71 | return ret; | |
72 | } | |
73 | ||
38ce65e1 AG |
74 | /* |
75 | * Sorts the memory list from highest address to lowest address | |
76 | * | |
77 | * When allocating memory we should always start from the highest | |
78 | * address chunk, so sort the memory list such that the first list | |
79 | * iterator gets the highest address and goes lower from there. | |
80 | */ | |
81 | static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) | |
82 | { | |
83 | struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link); | |
84 | struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link); | |
85 | ||
86 | if (mema->desc.physical_start == memb->desc.physical_start) | |
87 | return 0; | |
88 | else if (mema->desc.physical_start < memb->desc.physical_start) | |
89 | return 1; | |
90 | else | |
91 | return -1; | |
92 | } | |
93 | ||
7b05667c AG |
94 | static uint64_t desc_get_end(struct efi_mem_desc *desc) |
95 | { | |
96 | return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT); | |
97 | } | |
98 | ||
38ce65e1 AG |
99 | static void efi_mem_sort(void) |
100 | { | |
7b05667c AG |
101 | struct list_head *lhandle; |
102 | struct efi_mem_list *prevmem = NULL; | |
103 | bool merge_again = true; | |
104 | ||
38ce65e1 | 105 | list_sort(NULL, &efi_mem, efi_mem_cmp); |
7b05667c AG |
106 | |
107 | /* Now merge entries that can be merged */ | |
108 | while (merge_again) { | |
109 | merge_again = false; | |
110 | list_for_each(lhandle, &efi_mem) { | |
111 | struct efi_mem_list *lmem; | |
112 | struct efi_mem_desc *prev = &prevmem->desc; | |
113 | struct efi_mem_desc *cur; | |
114 | uint64_t pages; | |
115 | ||
116 | lmem = list_entry(lhandle, struct efi_mem_list, link); | |
117 | if (!prevmem) { | |
118 | prevmem = lmem; | |
119 | continue; | |
120 | } | |
121 | ||
122 | cur = &lmem->desc; | |
123 | ||
124 | if ((desc_get_end(cur) == prev->physical_start) && | |
125 | (prev->type == cur->type) && | |
126 | (prev->attribute == cur->attribute)) { | |
127 | /* There is an existing map before, reuse it */ | |
128 | pages = cur->num_pages; | |
129 | prev->num_pages += pages; | |
130 | prev->physical_start -= pages << EFI_PAGE_SHIFT; | |
131 | prev->virtual_start -= pages << EFI_PAGE_SHIFT; | |
132 | list_del(&lmem->link); | |
133 | free(lmem); | |
134 | ||
135 | merge_again = true; | |
136 | break; | |
137 | } | |
138 | ||
139 | prevmem = lmem; | |
140 | } | |
141 | } | |
38ce65e1 AG |
142 | } |
143 | ||
32826140 HS |
144 | /** efi_mem_carve_out - unmap memory region |
145 | * | |
146 | * @map: memory map | |
147 | * @carve_desc: memory region to unmap | |
148 | * @overlap_only_ram: the carved out region may only overlap RAM | |
149 | * Return Value: the number of overlapping pages which have been | |
150 | * removed from the map, | |
151 | * EFI_CARVE_NO_OVERLAP, if the regions don't overlap, | |
152 | * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap, | |
153 | * and the map contains anything but free ram | |
154 | * (only when overlap_only_ram is true), | |
155 | * EFI_CARVE_LOOP_AGAIN, if the mapping list should be | |
156 | * traversed again, as it has been altered. | |
5d00995c | 157 | * |
32826140 HS |
158 | * Unmaps all memory occupied by the carve_desc region from the list entry |
159 | * pointed to by map. | |
852efbf5 SB |
160 | * |
161 | * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility | |
32826140 | 162 | * to re-add the already carved out pages to the mapping. |
5d00995c | 163 | */ |
32826140 | 164 | static s64 efi_mem_carve_out(struct efi_mem_list *map, |
5d00995c AG |
165 | struct efi_mem_desc *carve_desc, |
166 | bool overlap_only_ram) | |
167 | { | |
168 | struct efi_mem_list *newmap; | |
169 | struct efi_mem_desc *map_desc = &map->desc; | |
170 | uint64_t map_start = map_desc->physical_start; | |
171 | uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT); | |
172 | uint64_t carve_start = carve_desc->physical_start; | |
173 | uint64_t carve_end = carve_start + | |
174 | (carve_desc->num_pages << EFI_PAGE_SHIFT); | |
175 | ||
176 | /* check whether we're overlapping */ | |
177 | if ((carve_end <= map_start) || (carve_start >= map_end)) | |
74c16acc | 178 | return EFI_CARVE_NO_OVERLAP; |
5d00995c AG |
179 | |
180 | /* We're overlapping with non-RAM, warn the caller if desired */ | |
181 | if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) | |
74c16acc | 182 | return EFI_CARVE_OVERLAPS_NONRAM; |
5d00995c AG |
183 | |
184 | /* Sanitize carve_start and carve_end to lie within our bounds */ | |
185 | carve_start = max(carve_start, map_start); | |
186 | carve_end = min(carve_end, map_end); | |
187 | ||
188 | /* Carving at the beginning of our map? Just move it! */ | |
189 | if (carve_start == map_start) { | |
190 | if (map_end == carve_end) { | |
191 | /* Full overlap, just remove map */ | |
192 | list_del(&map->link); | |
511d0b97 SB |
193 | free(map); |
194 | } else { | |
195 | map->desc.physical_start = carve_end; | |
9631fa0f | 196 | map->desc.virtual_start = carve_end; |
511d0b97 SB |
197 | map->desc.num_pages = (map_end - carve_end) |
198 | >> EFI_PAGE_SHIFT; | |
5d00995c AG |
199 | } |
200 | ||
74c16acc | 201 | return (carve_end - carve_start) >> EFI_PAGE_SHIFT; |
5d00995c AG |
202 | } |
203 | ||
204 | /* | |
205 | * Overlapping maps, just split the list map at carve_start, | |
206 | * it will get moved or removed in the next iteration. | |
207 | * | |
208 | * [ map_desc |__carve_start__| newmap ] | |
209 | */ | |
210 | ||
211 | /* Create a new map from [ carve_start ... map_end ] */ | |
212 | newmap = calloc(1, sizeof(*newmap)); | |
213 | newmap->desc = map->desc; | |
214 | newmap->desc.physical_start = carve_start; | |
9631fa0f | 215 | newmap->desc.virtual_start = carve_start; |
5d00995c | 216 | newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; |
b6a95172 SB |
217 | /* Insert before current entry (descending address order) */ |
218 | list_add_tail(&newmap->link, &map->link); | |
5d00995c AG |
219 | |
220 | /* Shrink the map to [ map_start ... carve_start ] */ | |
221 | map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; | |
222 | ||
74c16acc | 223 | return EFI_CARVE_LOOP_AGAIN; |
5d00995c AG |
224 | } |
225 | ||
226 | uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, | |
227 | bool overlap_only_ram) | |
228 | { | |
229 | struct list_head *lhandle; | |
230 | struct efi_mem_list *newlist; | |
74c16acc AG |
231 | bool carve_again; |
232 | uint64_t carved_pages = 0; | |
e80474ad | 233 | struct efi_event *evt; |
5d00995c | 234 | |
e301e024 HS |
235 | EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__, |
236 | start, pages, memory_type, overlap_only_ram ? "yes" : "no"); | |
c933ed94 | 237 | |
1fcb7ea2 HS |
238 | if (memory_type >= EFI_MAX_MEMORY_TYPE) |
239 | return EFI_INVALID_PARAMETER; | |
240 | ||
5d00995c AG |
241 | if (!pages) |
242 | return start; | |
243 | ||
1fcb7ea2 | 244 | ++efi_memory_map_key; |
5d00995c AG |
245 | newlist = calloc(1, sizeof(*newlist)); |
246 | newlist->desc.type = memory_type; | |
247 | newlist->desc.physical_start = start; | |
248 | newlist->desc.virtual_start = start; | |
249 | newlist->desc.num_pages = pages; | |
250 | ||
251 | switch (memory_type) { | |
252 | case EFI_RUNTIME_SERVICES_CODE: | |
253 | case EFI_RUNTIME_SERVICES_DATA: | |
9b89183b | 254 | newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME; |
5d00995c AG |
255 | break; |
256 | case EFI_MMAP_IO: | |
9b89183b | 257 | newlist->desc.attribute = EFI_MEMORY_RUNTIME; |
5d00995c AG |
258 | break; |
259 | default: | |
9b89183b | 260 | newlist->desc.attribute = EFI_MEMORY_WB; |
5d00995c AG |
261 | break; |
262 | } | |
263 | ||
264 | /* Add our new map */ | |
265 | do { | |
74c16acc | 266 | carve_again = false; |
5d00995c AG |
267 | list_for_each(lhandle, &efi_mem) { |
268 | struct efi_mem_list *lmem; | |
32826140 | 269 | s64 r; |
5d00995c AG |
270 | |
271 | lmem = list_entry(lhandle, struct efi_mem_list, link); | |
272 | r = efi_mem_carve_out(lmem, &newlist->desc, | |
273 | overlap_only_ram); | |
74c16acc AG |
274 | switch (r) { |
275 | case EFI_CARVE_OVERLAPS_NONRAM: | |
276 | /* | |
277 | * The user requested to only have RAM overlaps, | |
278 | * but we hit a non-RAM region. Error out. | |
279 | */ | |
5d00995c | 280 | return 0; |
74c16acc AG |
281 | case EFI_CARVE_NO_OVERLAP: |
282 | /* Just ignore this list entry */ | |
283 | break; | |
284 | case EFI_CARVE_LOOP_AGAIN: | |
285 | /* | |
286 | * We split an entry, but need to loop through | |
287 | * the list again to actually carve it. | |
288 | */ | |
289 | carve_again = true; | |
290 | break; | |
291 | default: | |
292 | /* We carved a number of pages */ | |
293 | carved_pages += r; | |
294 | carve_again = true; | |
295 | break; | |
296 | } | |
297 | ||
298 | if (carve_again) { | |
299 | /* The list changed, we need to start over */ | |
5d00995c AG |
300 | break; |
301 | } | |
302 | } | |
74c16acc AG |
303 | } while (carve_again); |
304 | ||
305 | if (overlap_only_ram && (carved_pages != pages)) { | |
306 | /* | |
307 | * The payload wanted to have RAM overlaps, but we overlapped | |
308 | * with an unallocated region. Error out. | |
309 | */ | |
310 | return 0; | |
311 | } | |
5d00995c AG |
312 | |
313 | /* Add our new map */ | |
314 | list_add_tail(&newlist->link, &efi_mem); | |
315 | ||
38ce65e1 AG |
316 | /* And make sure memory is listed in descending order */ |
317 | efi_mem_sort(); | |
318 | ||
e80474ad HS |
319 | /* Notify that the memory map was changed */ |
320 | list_for_each_entry(evt, &efi_events, link) { | |
321 | if (evt->group && | |
322 | !guidcmp(evt->group, | |
323 | &efi_guid_event_group_memory_map_change)) { | |
7eaa900e | 324 | efi_signal_event(evt); |
e80474ad HS |
325 | break; |
326 | } | |
327 | } | |
328 | ||
5d00995c AG |
329 | return start; |
330 | } | |
331 | ||
7d3af58e HS |
332 | /** |
333 | * efi_check_allocated() - validate address to be freed | |
334 | * | |
335 | * Check that the address is within allocated memory: | |
336 | * | |
7d3af58e HS |
337 | * * The address must be in a range of the memory map. |
338 | * * The address may not point to EFI_CONVENTIONAL_MEMORY. | |
339 | * | |
340 | * Page alignment is not checked as this is not a requirement of | |
341 | * efi_free_pool(). | |
342 | * | |
f756fe83 HS |
343 | * @addr: address of page to be freed |
344 | * @must_be_allocated: return success if the page is allocated | |
345 | * Return: status code | |
7d3af58e | 346 | */ |
f756fe83 | 347 | static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated) |
7d3af58e HS |
348 | { |
349 | struct efi_mem_list *item; | |
350 | ||
7d3af58e HS |
351 | list_for_each_entry(item, &efi_mem, link) { |
352 | u64 start = item->desc.physical_start; | |
353 | u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT); | |
354 | ||
355 | if (addr >= start && addr < end) { | |
f756fe83 HS |
356 | if (must_be_allocated ^ |
357 | (item->desc.type == EFI_CONVENTIONAL_MEMORY)) | |
7d3af58e HS |
358 | return EFI_SUCCESS; |
359 | else | |
360 | return EFI_NOT_FOUND; | |
361 | } | |
362 | } | |
363 | ||
364 | return EFI_NOT_FOUND; | |
365 | } | |
366 | ||
5d00995c AG |
367 | static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) |
368 | { | |
369 | struct list_head *lhandle; | |
370 | ||
c2e1ad70 AG |
371 | /* |
372 | * Prealign input max address, so we simplify our matching | |
373 | * logic below and can just reuse it as return pointer. | |
374 | */ | |
375 | max_addr &= ~EFI_PAGE_MASK; | |
376 | ||
5d00995c AG |
377 | list_for_each(lhandle, &efi_mem) { |
378 | struct efi_mem_list *lmem = list_entry(lhandle, | |
379 | struct efi_mem_list, link); | |
380 | struct efi_mem_desc *desc = &lmem->desc; | |
381 | uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; | |
382 | uint64_t desc_end = desc->physical_start + desc_len; | |
383 | uint64_t curmax = min(max_addr, desc_end); | |
384 | uint64_t ret = curmax - len; | |
385 | ||
386 | /* We only take memory from free RAM */ | |
387 | if (desc->type != EFI_CONVENTIONAL_MEMORY) | |
388 | continue; | |
389 | ||
390 | /* Out of bounds for max_addr */ | |
391 | if ((ret + len) > max_addr) | |
392 | continue; | |
393 | ||
394 | /* Out of bounds for upper map limit */ | |
395 | if ((ret + len) > desc_end) | |
396 | continue; | |
397 | ||
398 | /* Out of bounds for lower map limit */ | |
399 | if (ret < desc->physical_start) | |
400 | continue; | |
401 | ||
402 | /* Return the highest address in this map within bounds */ | |
403 | return ret; | |
404 | } | |
405 | ||
406 | return 0; | |
407 | } | |
408 | ||
474a6f5a HS |
409 | /* |
410 | * Allocate memory pages. | |
411 | * | |
412 | * @type type of allocation to be performed | |
413 | * @memory_type usage type of the allocated memory | |
414 | * @pages number of pages to be allocated | |
415 | * @memory allocated memory | |
416 | * @return status code | |
417 | */ | |
5d00995c | 418 | efi_status_t efi_allocate_pages(int type, int memory_type, |
f5a2a938 | 419 | efi_uintn_t pages, uint64_t *memory) |
5d00995c AG |
420 | { |
421 | u64 len = pages << EFI_PAGE_SHIFT; | |
8ae39857 | 422 | efi_status_t ret; |
5d00995c AG |
423 | uint64_t addr; |
424 | ||
f12bcc91 HS |
425 | /* Check import parameters */ |
426 | if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE && | |
427 | memory_type <= 0x6FFFFFFF) | |
428 | return EFI_INVALID_PARAMETER; | |
4d5e071e HS |
429 | if (!memory) |
430 | return EFI_INVALID_PARAMETER; | |
431 | ||
5d00995c | 432 | switch (type) { |
7c92fd69 | 433 | case EFI_ALLOCATE_ANY_PAGES: |
5d00995c | 434 | /* Any page */ |
14deb5e6 | 435 | addr = efi_find_free_memory(len, -1ULL); |
8ae39857 HS |
436 | if (!addr) |
437 | return EFI_OUT_OF_RESOURCES; | |
5d00995c | 438 | break; |
7c92fd69 | 439 | case EFI_ALLOCATE_MAX_ADDRESS: |
5d00995c AG |
440 | /* Max address */ |
441 | addr = efi_find_free_memory(len, *memory); | |
8ae39857 HS |
442 | if (!addr) |
443 | return EFI_OUT_OF_RESOURCES; | |
5d00995c | 444 | break; |
7c92fd69 | 445 | case EFI_ALLOCATE_ADDRESS: |
5d00995c | 446 | /* Exact address, reserve it. The addr is already in *memory. */ |
8ae39857 HS |
447 | ret = efi_check_allocated(*memory, false); |
448 | if (ret != EFI_SUCCESS) | |
449 | return EFI_NOT_FOUND; | |
5d00995c AG |
450 | addr = *memory; |
451 | break; | |
452 | default: | |
453 | /* UEFI doesn't specify other allocation types */ | |
8ae39857 | 454 | return EFI_INVALID_PARAMETER; |
5d00995c AG |
455 | } |
456 | ||
8ae39857 HS |
457 | /* Reserve that map in our memory maps */ |
458 | if (efi_add_memory_map(addr, pages, memory_type, true) != addr) | |
459 | /* Map would overlap, bail out */ | |
460 | return EFI_OUT_OF_RESOURCES; | |
5d00995c | 461 | |
8ae39857 | 462 | *memory = addr; |
5d00995c | 463 | |
8ae39857 | 464 | return EFI_SUCCESS; |
5d00995c AG |
465 | } |
466 | ||
467 | void *efi_alloc(uint64_t len, int memory_type) | |
468 | { | |
469 | uint64_t ret = 0; | |
c3772ca1 | 470 | uint64_t pages = efi_size_in_pages(len); |
5d00995c AG |
471 | efi_status_t r; |
472 | ||
e09159c8 HS |
473 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages, |
474 | &ret); | |
5d00995c AG |
475 | if (r == EFI_SUCCESS) |
476 | return (void*)(uintptr_t)ret; | |
477 | ||
478 | return NULL; | |
479 | } | |
480 | ||
2c3ec289 HS |
481 | /** |
482 | * efi_free_pages() - free memory pages | |
474a6f5a | 483 | * |
2c3ec289 HS |
484 | * @memory: start of the memory area to be freed |
485 | * @pages: number of pages to be freed | |
486 | * Return: status code | |
474a6f5a | 487 | */ |
f5a2a938 | 488 | efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages) |
5d00995c | 489 | { |
b61d857b | 490 | uint64_t r = 0; |
7d3af58e HS |
491 | efi_status_t ret; |
492 | ||
f756fe83 | 493 | ret = efi_check_allocated(memory, true); |
7d3af58e HS |
494 | if (ret != EFI_SUCCESS) |
495 | return ret; | |
b61d857b | 496 | |
2c3ec289 | 497 | /* Sanity check */ |
e00b82db | 498 | if (!memory || (memory & EFI_PAGE_MASK) || !pages) { |
2c3ec289 HS |
499 | printf("%s: illegal free 0x%llx, 0x%zx\n", __func__, |
500 | memory, pages); | |
501 | return EFI_INVALID_PARAMETER; | |
502 | } | |
503 | ||
49759743 | 504 | r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false); |
b61d857b SB |
505 | /* Merging of adjacent free regions is missing */ |
506 | ||
49759743 | 507 | if (r == memory) |
b61d857b SB |
508 | return EFI_SUCCESS; |
509 | ||
510 | return EFI_NOT_FOUND; | |
5d00995c AG |
511 | } |
512 | ||
2c3ec289 HS |
513 | /** |
514 | * efi_allocate_pool - allocate memory from pool | |
474a6f5a | 515 | * |
2c3ec289 HS |
516 | * @pool_type: type of the pool from which memory is to be allocated |
517 | * @size: number of bytes to be allocated | |
518 | * @buffer: allocated memory | |
519 | * Return: status code | |
474a6f5a HS |
520 | */ |
521 | efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) | |
ead1274b SB |
522 | { |
523 | efi_status_t r; | |
306b1671 | 524 | u64 addr; |
282a06cb | 525 | struct efi_pool_allocation *alloc; |
c3772ca1 HS |
526 | u64 num_pages = efi_size_in_pages(size + |
527 | sizeof(struct efi_pool_allocation)); | |
42417bc8 | 528 | |
4d5e071e HS |
529 | if (!buffer) |
530 | return EFI_INVALID_PARAMETER; | |
531 | ||
42417bc8 SB |
532 | if (size == 0) { |
533 | *buffer = NULL; | |
534 | return EFI_SUCCESS; | |
535 | } | |
ead1274b | 536 | |
e09159c8 | 537 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages, |
306b1671 | 538 | &addr); |
42417bc8 | 539 | if (r == EFI_SUCCESS) { |
306b1671 | 540 | alloc = (struct efi_pool_allocation *)(uintptr_t)addr; |
42417bc8 | 541 | alloc->num_pages = num_pages; |
2c3ec289 | 542 | alloc->checksum = checksum(alloc); |
42417bc8 SB |
543 | *buffer = alloc->data; |
544 | } | |
545 | ||
546 | return r; | |
547 | } | |
548 | ||
2c3ec289 HS |
549 | /** |
550 | * efi_free_pool() - free memory from pool | |
474a6f5a | 551 | * |
2c3ec289 HS |
552 | * @buffer: start of memory to be freed |
553 | * Return: status code | |
474a6f5a | 554 | */ |
42417bc8 SB |
555 | efi_status_t efi_free_pool(void *buffer) |
556 | { | |
7d3af58e | 557 | efi_status_t ret; |
42417bc8 SB |
558 | struct efi_pool_allocation *alloc; |
559 | ||
0e22c7cb HS |
560 | if (!buffer) |
561 | return EFI_INVALID_PARAMETER; | |
562 | ||
f756fe83 | 563 | ret = efi_check_allocated((uintptr_t)buffer, true); |
7d3af58e HS |
564 | if (ret != EFI_SUCCESS) |
565 | return ret; | |
71275a3e | 566 | |
42417bc8 | 567 | alloc = container_of(buffer, struct efi_pool_allocation, data); |
2c3ec289 HS |
568 | |
569 | /* Check that this memory was allocated by efi_allocate_pool() */ | |
570 | if (((uintptr_t)alloc & EFI_PAGE_MASK) || | |
571 | alloc->checksum != checksum(alloc)) { | |
572 | printf("%s: illegal free 0x%p\n", __func__, buffer); | |
573 | return EFI_INVALID_PARAMETER; | |
574 | } | |
575 | /* Avoid double free */ | |
576 | alloc->checksum = 0; | |
42417bc8 | 577 | |
7d3af58e | 578 | ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages); |
ead1274b | 579 | |
7d3af58e | 580 | return ret; |
ead1274b SB |
581 | } |
582 | ||
474a6f5a HS |
583 | /* |
584 | * Get map describing memory usage. | |
585 | * | |
586 | * @memory_map_size on entry the size, in bytes, of the memory map buffer, | |
587 | * on exit the size of the copied memory map | |
588 | * @memory_map buffer to which the memory map is written | |
589 | * @map_key key for the memory map | |
590 | * @descriptor_size size of an individual memory descriptor | |
591 | * @descriptor_version version number of the memory descriptor structure | |
592 | * @return status code | |
593 | */ | |
f5a2a938 HS |
594 | efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size, |
595 | struct efi_mem_desc *memory_map, | |
596 | efi_uintn_t *map_key, | |
597 | efi_uintn_t *descriptor_size, | |
598 | uint32_t *descriptor_version) | |
5d00995c | 599 | { |
f5a2a938 | 600 | efi_uintn_t map_size = 0; |
cee752fa | 601 | int map_entries = 0; |
5d00995c | 602 | struct list_head *lhandle; |
fa995d0d | 603 | efi_uintn_t provided_map_size; |
5d00995c | 604 | |
8e835554 HS |
605 | if (!memory_map_size) |
606 | return EFI_INVALID_PARAMETER; | |
607 | ||
fa995d0d HS |
608 | provided_map_size = *memory_map_size; |
609 | ||
5d00995c | 610 | list_for_each(lhandle, &efi_mem) |
cee752fa AG |
611 | map_entries++; |
612 | ||
613 | map_size = map_entries * sizeof(struct efi_mem_desc); | |
5d00995c | 614 | |
a1b24823 RC |
615 | *memory_map_size = map_size; |
616 | ||
0ecba5db HS |
617 | if (provided_map_size < map_size) |
618 | return EFI_BUFFER_TOO_SMALL; | |
619 | ||
8e835554 HS |
620 | if (!memory_map) |
621 | return EFI_INVALID_PARAMETER; | |
622 | ||
5d00995c AG |
623 | if (descriptor_size) |
624 | *descriptor_size = sizeof(struct efi_mem_desc); | |
625 | ||
4c02c11d MYK |
626 | if (descriptor_version) |
627 | *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; | |
628 | ||
5d00995c | 629 | /* Copy list into array */ |
8e835554 HS |
630 | /* Return the list in ascending order */ |
631 | memory_map = &memory_map[map_entries - 1]; | |
632 | list_for_each(lhandle, &efi_mem) { | |
633 | struct efi_mem_list *lmem; | |
5d00995c | 634 | |
8e835554 HS |
635 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
636 | *memory_map = lmem->desc; | |
637 | memory_map--; | |
5d00995c AG |
638 | } |
639 | ||
8e835554 | 640 | if (map_key) |
1fcb7ea2 | 641 | *map_key = efi_memory_map_key; |
c6e3c3e6 | 642 | |
5d00995c AG |
643 | return EFI_SUCCESS; |
644 | } | |
645 | ||
42633745 | 646 | __weak void efi_add_known_memory(void) |
5d00995c | 647 | { |
7b78d643 | 648 | u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK; |
5d00995c AG |
649 | int i; |
650 | ||
23f5f4ab HS |
651 | /* |
652 | * ram_top is just outside mapped memory. So use an offset of one for | |
653 | * mapping the sandbox address. | |
654 | */ | |
655 | ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1; | |
656 | ||
7b78d643 AG |
657 | /* Fix for 32bit targets with ram_top at 4G */ |
658 | if (!ram_top) | |
659 | ram_top = 0x100000000ULL; | |
660 | ||
5d00995c AG |
661 | /* Add RAM */ |
662 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { | |
108bdff8 | 663 | u64 ram_end, ram_start, pages; |
5d00995c | 664 | |
49759743 | 665 | ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0); |
108bdff8 HS |
666 | ram_end = ram_start + gd->bd->bi_dram[i].size; |
667 | ||
668 | /* Remove partial pages */ | |
669 | ram_end &= ~EFI_PAGE_MASK; | |
670 | ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; | |
671 | ||
7b78d643 AG |
672 | if (ram_end <= ram_start) { |
673 | /* Invalid mapping, keep going. */ | |
674 | continue; | |
675 | } | |
676 | ||
677 | pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT; | |
108bdff8 | 678 | |
7b78d643 AG |
679 | efi_add_memory_map(ram_start, pages, |
680 | EFI_CONVENTIONAL_MEMORY, false); | |
681 | ||
682 | /* | |
683 | * Boards may indicate to the U-Boot memory core that they | |
684 | * can not support memory above ram_top. Let's honor this | |
685 | * in the efi_loader subsystem too by declaring any memory | |
686 | * above ram_top as "already occupied by firmware". | |
687 | */ | |
688 | if (ram_top < ram_start) { | |
689 | /* ram_top is before this region, reserve all */ | |
108bdff8 | 690 | efi_add_memory_map(ram_start, pages, |
7b78d643 AG |
691 | EFI_BOOT_SERVICES_DATA, true); |
692 | } else if ((ram_top >= ram_start) && (ram_top < ram_end)) { | |
693 | /* ram_top is inside this region, reserve parts */ | |
694 | pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT; | |
695 | ||
696 | efi_add_memory_map(ram_top, pages, | |
697 | EFI_BOOT_SERVICES_DATA, true); | |
108bdff8 | 698 | } |
5d00995c | 699 | } |
42633745 YS |
700 | } |
701 | ||
69259b83 SG |
702 | /* Add memory regions for U-Boot's memory and for the runtime services code */ |
703 | static void add_u_boot_and_runtime(void) | |
42633745 YS |
704 | { |
705 | unsigned long runtime_start, runtime_end, runtime_pages; | |
7a82c305 | 706 | unsigned long runtime_mask = EFI_PAGE_MASK; |
42633745 YS |
707 | unsigned long uboot_start, uboot_pages; |
708 | unsigned long uboot_stack_size = 16 * 1024 * 1024; | |
709 | ||
5d00995c AG |
710 | /* Add U-Boot */ |
711 | uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK; | |
712 | uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT; | |
713 | efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false); | |
714 | ||
7a82c305 AG |
715 | #if defined(__aarch64__) |
716 | /* | |
717 | * Runtime Services must be 64KiB aligned according to the | |
718 | * "AArch64 Platforms" section in the UEFI spec (2.7+). | |
719 | */ | |
720 | ||
721 | runtime_mask = SZ_64K - 1; | |
722 | #endif | |
723 | ||
724 | /* | |
725 | * Add Runtime Services. We mark surrounding boottime code as runtime as | |
726 | * well to fulfill the runtime alignment constraints but avoid padding. | |
727 | */ | |
728 | runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask; | |
5d00995c | 729 | runtime_end = (ulong)&__efi_runtime_stop; |
7a82c305 | 730 | runtime_end = (runtime_end + runtime_mask) & ~runtime_mask; |
5d00995c AG |
731 | runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; |
732 | efi_add_memory_map(runtime_start, runtime_pages, | |
733 | EFI_RUNTIME_SERVICES_CODE, false); | |
69259b83 SG |
734 | } |
735 | ||
736 | int efi_memory_init(void) | |
737 | { | |
738 | efi_add_known_memory(); | |
739 | ||
740 | if (!IS_ENABLED(CONFIG_SANDBOX)) | |
741 | add_u_boot_and_runtime(); | |
5d00995c | 742 | |
51735ae0 AG |
743 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
744 | /* Request a 32bit 64MB bounce buffer region */ | |
745 | uint64_t efi_bounce_buffer_addr = 0xffffffff; | |
746 | ||
e09159c8 | 747 | if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA, |
51735ae0 AG |
748 | (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, |
749 | &efi_bounce_buffer_addr) != EFI_SUCCESS) | |
750 | return -1; | |
751 | ||
752 | efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; | |
753 | #endif | |
754 | ||
5d00995c AG |
755 | return 0; |
756 | } |