efi_memory.c 12 KB

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