efi_memory.c 21 KB

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