efi_memory.c 20 KB

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