efi_memory.c 13 KB

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