efi_memory.c 20 KB

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