efi_memory.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 <inttypes.h>
  10. #include <malloc.h>
  11. #include <watchdog.h>
  12. #include <asm/global_data.h>
  13. #include <linux/list_sort.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. struct efi_mem_list {
  16. struct list_head link;
  17. struct efi_mem_desc desc;
  18. };
  19. #define EFI_CARVE_NO_OVERLAP -1
  20. #define EFI_CARVE_LOOP_AGAIN -2
  21. #define EFI_CARVE_OVERLAPS_NONRAM -3
  22. /* This list contains all memory map items */
  23. LIST_HEAD(efi_mem);
  24. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  25. void *efi_bounce_buffer;
  26. #endif
  27. /*
  28. * U-Boot services each EFI AllocatePool request as a separate
  29. * (multiple) page allocation. We have to track the number of pages
  30. * to be able to free the correct amount later.
  31. * EFI requires 8 byte alignment for pool allocations, so we can
  32. * prepend each allocation with an 64 bit header tracking the
  33. * allocation size, and hand out the remainder to the caller.
  34. */
  35. struct efi_pool_allocation {
  36. u64 num_pages;
  37. char data[] __aligned(ARCH_DMA_MINALIGN);
  38. };
  39. /*
  40. * Sorts the memory list from highest address to lowest address
  41. *
  42. * When allocating memory we should always start from the highest
  43. * address chunk, so sort the memory list such that the first list
  44. * iterator gets the highest address and goes lower from there.
  45. */
  46. static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
  47. {
  48. struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
  49. struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
  50. if (mema->desc.physical_start == memb->desc.physical_start)
  51. return 0;
  52. else if (mema->desc.physical_start < memb->desc.physical_start)
  53. return 1;
  54. else
  55. return -1;
  56. }
  57. static void efi_mem_sort(void)
  58. {
  59. list_sort(NULL, &efi_mem, efi_mem_cmp);
  60. }
  61. /*
  62. * Unmaps all memory occupied by the carve_desc region from the
  63. * list entry pointed to by map.
  64. *
  65. * Returns EFI_CARVE_NO_OVERLAP if the regions don't overlap.
  66. * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap,
  67. * and the map contains anything but free ram.
  68. * (only when overlap_only_ram is true)
  69. * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed
  70. * again, as it has been altered
  71. * Returns the number of overlapping pages. The pages are removed from
  72. * the mapping list.
  73. *
  74. * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
  75. * to readd the already carved out pages to the mapping.
  76. */
  77. static int efi_mem_carve_out(struct efi_mem_list *map,
  78. struct efi_mem_desc *carve_desc,
  79. bool overlap_only_ram)
  80. {
  81. struct efi_mem_list *newmap;
  82. struct efi_mem_desc *map_desc = &map->desc;
  83. uint64_t map_start = map_desc->physical_start;
  84. uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
  85. uint64_t carve_start = carve_desc->physical_start;
  86. uint64_t carve_end = carve_start +
  87. (carve_desc->num_pages << EFI_PAGE_SHIFT);
  88. /* check whether we're overlapping */
  89. if ((carve_end <= map_start) || (carve_start >= map_end))
  90. return EFI_CARVE_NO_OVERLAP;
  91. /* We're overlapping with non-RAM, warn the caller if desired */
  92. if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
  93. return EFI_CARVE_OVERLAPS_NONRAM;
  94. /* Sanitize carve_start and carve_end to lie within our bounds */
  95. carve_start = max(carve_start, map_start);
  96. carve_end = min(carve_end, map_end);
  97. /* Carving at the beginning of our map? Just move it! */
  98. if (carve_start == map_start) {
  99. if (map_end == carve_end) {
  100. /* Full overlap, just remove map */
  101. list_del(&map->link);
  102. free(map);
  103. } else {
  104. map->desc.physical_start = carve_end;
  105. map->desc.num_pages = (map_end - carve_end)
  106. >> EFI_PAGE_SHIFT;
  107. }
  108. return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
  109. }
  110. /*
  111. * Overlapping maps, just split the list map at carve_start,
  112. * it will get moved or removed in the next iteration.
  113. *
  114. * [ map_desc |__carve_start__| newmap ]
  115. */
  116. /* Create a new map from [ carve_start ... map_end ] */
  117. newmap = calloc(1, sizeof(*newmap));
  118. newmap->desc = map->desc;
  119. newmap->desc.physical_start = carve_start;
  120. newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
  121. /* Insert before current entry (descending address order) */
  122. list_add_tail(&newmap->link, &map->link);
  123. /* Shrink the map to [ map_start ... carve_start ] */
  124. map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
  125. return EFI_CARVE_LOOP_AGAIN;
  126. }
  127. uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
  128. bool overlap_only_ram)
  129. {
  130. struct list_head *lhandle;
  131. struct efi_mem_list *newlist;
  132. bool carve_again;
  133. uint64_t carved_pages = 0;
  134. debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
  135. start, pages, memory_type, overlap_only_ram ? "yes" : "no");
  136. if (!pages)
  137. return start;
  138. newlist = calloc(1, sizeof(*newlist));
  139. newlist->desc.type = memory_type;
  140. newlist->desc.physical_start = start;
  141. newlist->desc.virtual_start = start;
  142. newlist->desc.num_pages = pages;
  143. switch (memory_type) {
  144. case EFI_RUNTIME_SERVICES_CODE:
  145. case EFI_RUNTIME_SERVICES_DATA:
  146. newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
  147. (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
  148. break;
  149. case EFI_MMAP_IO:
  150. newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
  151. break;
  152. default:
  153. newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
  154. break;
  155. }
  156. /* Add our new map */
  157. do {
  158. carve_again = false;
  159. list_for_each(lhandle, &efi_mem) {
  160. struct efi_mem_list *lmem;
  161. int r;
  162. lmem = list_entry(lhandle, struct efi_mem_list, link);
  163. r = efi_mem_carve_out(lmem, &newlist->desc,
  164. overlap_only_ram);
  165. switch (r) {
  166. case EFI_CARVE_OVERLAPS_NONRAM:
  167. /*
  168. * The user requested to only have RAM overlaps,
  169. * but we hit a non-RAM region. Error out.
  170. */
  171. return 0;
  172. case EFI_CARVE_NO_OVERLAP:
  173. /* Just ignore this list entry */
  174. break;
  175. case EFI_CARVE_LOOP_AGAIN:
  176. /*
  177. * We split an entry, but need to loop through
  178. * the list again to actually carve it.
  179. */
  180. carve_again = true;
  181. break;
  182. default:
  183. /* We carved a number of pages */
  184. carved_pages += r;
  185. carve_again = true;
  186. break;
  187. }
  188. if (carve_again) {
  189. /* The list changed, we need to start over */
  190. break;
  191. }
  192. }
  193. } while (carve_again);
  194. if (overlap_only_ram && (carved_pages != pages)) {
  195. /*
  196. * The payload wanted to have RAM overlaps, but we overlapped
  197. * with an unallocated region. Error out.
  198. */
  199. return 0;
  200. }
  201. /* Add our new map */
  202. list_add_tail(&newlist->link, &efi_mem);
  203. /* And make sure memory is listed in descending order */
  204. efi_mem_sort();
  205. return start;
  206. }
  207. static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
  208. {
  209. struct list_head *lhandle;
  210. list_for_each(lhandle, &efi_mem) {
  211. struct efi_mem_list *lmem = list_entry(lhandle,
  212. struct efi_mem_list, link);
  213. struct efi_mem_desc *desc = &lmem->desc;
  214. uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
  215. uint64_t desc_end = desc->physical_start + desc_len;
  216. uint64_t curmax = min(max_addr, desc_end);
  217. uint64_t ret = curmax - len;
  218. /* We only take memory from free RAM */
  219. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  220. continue;
  221. /* Out of bounds for max_addr */
  222. if ((ret + len) > max_addr)
  223. continue;
  224. /* Out of bounds for upper map limit */
  225. if ((ret + len) > desc_end)
  226. continue;
  227. /* Out of bounds for lower map limit */
  228. if (ret < desc->physical_start)
  229. continue;
  230. /* Return the highest address in this map within bounds */
  231. return ret;
  232. }
  233. return 0;
  234. }
  235. /*
  236. * Allocate memory pages.
  237. *
  238. * @type type of allocation to be performed
  239. * @memory_type usage type of the allocated memory
  240. * @pages number of pages to be allocated
  241. * @memory allocated memory
  242. * @return status code
  243. */
  244. efi_status_t efi_allocate_pages(int type, int memory_type,
  245. efi_uintn_t pages, uint64_t *memory)
  246. {
  247. u64 len = pages << EFI_PAGE_SHIFT;
  248. efi_status_t r = EFI_SUCCESS;
  249. uint64_t addr;
  250. switch (type) {
  251. case EFI_ALLOCATE_ANY_PAGES:
  252. /* Any page */
  253. addr = efi_find_free_memory(len, gd->start_addr_sp);
  254. if (!addr) {
  255. r = EFI_NOT_FOUND;
  256. break;
  257. }
  258. break;
  259. case EFI_ALLOCATE_MAX_ADDRESS:
  260. /* Max address */
  261. addr = efi_find_free_memory(len, *memory);
  262. if (!addr) {
  263. r = EFI_NOT_FOUND;
  264. break;
  265. }
  266. break;
  267. case EFI_ALLOCATE_ADDRESS:
  268. /* Exact address, reserve it. The addr is already in *memory. */
  269. addr = *memory;
  270. break;
  271. default:
  272. /* UEFI doesn't specify other allocation types */
  273. r = EFI_INVALID_PARAMETER;
  274. break;
  275. }
  276. if (r == EFI_SUCCESS) {
  277. uint64_t ret;
  278. /* Reserve that map in our memory maps */
  279. ret = efi_add_memory_map(addr, pages, memory_type, true);
  280. if (ret == addr) {
  281. *memory = addr;
  282. } else {
  283. /* Map would overlap, bail out */
  284. r = EFI_OUT_OF_RESOURCES;
  285. }
  286. }
  287. return r;
  288. }
  289. void *efi_alloc(uint64_t len, int memory_type)
  290. {
  291. uint64_t ret = 0;
  292. uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
  293. efi_status_t r;
  294. r = efi_allocate_pages(0, memory_type, pages, &ret);
  295. if (r == EFI_SUCCESS)
  296. return (void*)(uintptr_t)ret;
  297. return NULL;
  298. }
  299. /*
  300. * Free memory pages.
  301. *
  302. * @memory start of the memory area to be freed
  303. * @pages number of pages to be freed
  304. * @return status code
  305. */
  306. efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
  307. {
  308. uint64_t r = 0;
  309. r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
  310. /* Merging of adjacent free regions is missing */
  311. if (r == memory)
  312. return EFI_SUCCESS;
  313. return EFI_NOT_FOUND;
  314. }
  315. /*
  316. * Allocate memory from pool.
  317. *
  318. * @pool_type type of the pool from which memory is to be allocated
  319. * @size number of bytes to be allocated
  320. * @buffer allocated memory
  321. * @return status code
  322. */
  323. efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
  324. {
  325. efi_status_t r;
  326. efi_physical_addr_t t;
  327. u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
  328. EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
  329. if (size == 0) {
  330. *buffer = NULL;
  331. return EFI_SUCCESS;
  332. }
  333. r = efi_allocate_pages(0, pool_type, num_pages, &t);
  334. if (r == EFI_SUCCESS) {
  335. struct efi_pool_allocation *alloc = (void *)(uintptr_t)t;
  336. alloc->num_pages = num_pages;
  337. *buffer = alloc->data;
  338. }
  339. return r;
  340. }
  341. /*
  342. * Free memory from pool.
  343. *
  344. * @buffer start of memory to be freed
  345. * @return status code
  346. */
  347. efi_status_t efi_free_pool(void *buffer)
  348. {
  349. efi_status_t r;
  350. struct efi_pool_allocation *alloc;
  351. if (buffer == NULL)
  352. return EFI_INVALID_PARAMETER;
  353. alloc = container_of(buffer, struct efi_pool_allocation, data);
  354. /* Sanity check, was the supplied address returned by allocate_pool */
  355. assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
  356. r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
  357. return r;
  358. }
  359. /*
  360. * Get map describing memory usage.
  361. *
  362. * @memory_map_size on entry the size, in bytes, of the memory map buffer,
  363. * on exit the size of the copied memory map
  364. * @memory_map buffer to which the memory map is written
  365. * @map_key key for the memory map
  366. * @descriptor_size size of an individual memory descriptor
  367. * @descriptor_version version number of the memory descriptor structure
  368. * @return status code
  369. */
  370. efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
  371. struct efi_mem_desc *memory_map,
  372. efi_uintn_t *map_key,
  373. efi_uintn_t *descriptor_size,
  374. uint32_t *descriptor_version)
  375. {
  376. efi_uintn_t map_size = 0;
  377. int map_entries = 0;
  378. struct list_head *lhandle;
  379. efi_uintn_t provided_map_size = *memory_map_size;
  380. list_for_each(lhandle, &efi_mem)
  381. map_entries++;
  382. map_size = map_entries * sizeof(struct efi_mem_desc);
  383. *memory_map_size = map_size;
  384. if (provided_map_size < map_size)
  385. return EFI_BUFFER_TOO_SMALL;
  386. if (descriptor_size)
  387. *descriptor_size = sizeof(struct efi_mem_desc);
  388. if (descriptor_version)
  389. *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
  390. /* Copy list into array */
  391. if (memory_map) {
  392. /* Return the list in ascending order */
  393. memory_map = &memory_map[map_entries - 1];
  394. list_for_each(lhandle, &efi_mem) {
  395. struct efi_mem_list *lmem;
  396. lmem = list_entry(lhandle, struct efi_mem_list, link);
  397. *memory_map = lmem->desc;
  398. memory_map--;
  399. }
  400. }
  401. *map_key = 0;
  402. return EFI_SUCCESS;
  403. }
  404. __weak void efi_add_known_memory(void)
  405. {
  406. int i;
  407. /* Add RAM */
  408. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  409. u64 ram_start = gd->bd->bi_dram[i].start;
  410. u64 ram_size = gd->bd->bi_dram[i].size;
  411. u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
  412. u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
  413. efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
  414. false);
  415. }
  416. }
  417. int efi_memory_init(void)
  418. {
  419. unsigned long runtime_start, runtime_end, runtime_pages;
  420. unsigned long uboot_start, uboot_pages;
  421. unsigned long uboot_stack_size = 16 * 1024 * 1024;
  422. efi_add_known_memory();
  423. /* Add U-Boot */
  424. uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
  425. uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
  426. efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
  427. /* Add Runtime Services */
  428. runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
  429. runtime_end = (ulong)&__efi_runtime_stop;
  430. runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
  431. runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
  432. efi_add_memory_map(runtime_start, runtime_pages,
  433. EFI_RUNTIME_SERVICES_CODE, false);
  434. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  435. /* Request a 32bit 64MB bounce buffer region */
  436. uint64_t efi_bounce_buffer_addr = 0xffffffff;
  437. if (efi_allocate_pages(1, EFI_LOADER_DATA,
  438. (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
  439. &efi_bounce_buffer_addr) != EFI_SUCCESS)
  440. return -1;
  441. efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
  442. #endif
  443. return 0;
  444. }