efi_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 <linux/list_sort.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. struct efi_mem_list {
  15. struct list_head link;
  16. struct efi_mem_desc desc;
  17. };
  18. #define EFI_CARVE_NO_OVERLAP -1
  19. #define EFI_CARVE_LOOP_AGAIN -2
  20. #define EFI_CARVE_OVERLAPS_NONRAM -3
  21. /* This list contains all memory map items */
  22. LIST_HEAD(efi_mem);
  23. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  24. void *efi_bounce_buffer;
  25. #endif
  26. /*
  27. * U-Boot services each EFI AllocatePool request as a separate
  28. * (multiple) page allocation. We have to track the number of pages
  29. * to be able to free the correct amount later.
  30. * EFI requires 8 byte alignment for pool allocations, so we can
  31. * prepend each allocation with an 64 bit header tracking the
  32. * allocation size, and hand out the remainder to the caller.
  33. */
  34. struct efi_pool_allocation {
  35. u64 num_pages;
  36. char data[] __aligned(ARCH_DMA_MINALIGN);
  37. };
  38. /*
  39. * Sorts the memory list from highest address to lowest address
  40. *
  41. * When allocating memory we should always start from the highest
  42. * address chunk, so sort the memory list such that the first list
  43. * iterator gets the highest address and goes lower from there.
  44. */
  45. static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
  46. {
  47. struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
  48. struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
  49. if (mema->desc.physical_start == memb->desc.physical_start)
  50. return 0;
  51. else if (mema->desc.physical_start < memb->desc.physical_start)
  52. return 1;
  53. else
  54. return -1;
  55. }
  56. static void efi_mem_sort(void)
  57. {
  58. list_sort(NULL, &efi_mem, efi_mem_cmp);
  59. }
  60. /*
  61. * Unmaps all memory occupied by the carve_desc region from the
  62. * list entry pointed to by map.
  63. *
  64. * Returns EFI_CARVE_NO_OVERLAP if the regions don't overlap.
  65. * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap,
  66. * and the map contains anything but free ram.
  67. * (only when overlap_only_ram is true)
  68. * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed
  69. * again, as it has been altered
  70. * Returns the number of overlapping pages. The pages are removed from
  71. * the mapping list.
  72. *
  73. * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
  74. * to readd the already carved out pages to the mapping.
  75. */
  76. static int efi_mem_carve_out(struct efi_mem_list *map,
  77. struct efi_mem_desc *carve_desc,
  78. bool overlap_only_ram)
  79. {
  80. struct efi_mem_list *newmap;
  81. struct efi_mem_desc *map_desc = &map->desc;
  82. uint64_t map_start = map_desc->physical_start;
  83. uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
  84. uint64_t carve_start = carve_desc->physical_start;
  85. uint64_t carve_end = carve_start +
  86. (carve_desc->num_pages << EFI_PAGE_SHIFT);
  87. /* check whether we're overlapping */
  88. if ((carve_end <= map_start) || (carve_start >= map_end))
  89. return EFI_CARVE_NO_OVERLAP;
  90. /* We're overlapping with non-RAM, warn the caller if desired */
  91. if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
  92. return EFI_CARVE_OVERLAPS_NONRAM;
  93. /* Sanitize carve_start and carve_end to lie within our bounds */
  94. carve_start = max(carve_start, map_start);
  95. carve_end = min(carve_end, map_end);
  96. /* Carving at the beginning of our map? Just move it! */
  97. if (carve_start == map_start) {
  98. if (map_end == carve_end) {
  99. /* Full overlap, just remove map */
  100. list_del(&map->link);
  101. free(map);
  102. } else {
  103. map->desc.physical_start = carve_end;
  104. map->desc.num_pages = (map_end - carve_end)
  105. >> EFI_PAGE_SHIFT;
  106. }
  107. return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
  108. }
  109. /*
  110. * Overlapping maps, just split the list map at carve_start,
  111. * it will get moved or removed in the next iteration.
  112. *
  113. * [ map_desc |__carve_start__| newmap ]
  114. */
  115. /* Create a new map from [ carve_start ... map_end ] */
  116. newmap = calloc(1, sizeof(*newmap));
  117. newmap->desc = map->desc;
  118. newmap->desc.physical_start = carve_start;
  119. newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
  120. /* Insert before current entry (descending address order) */
  121. list_add_tail(&newmap->link, &map->link);
  122. /* Shrink the map to [ map_start ... carve_start ] */
  123. map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
  124. return EFI_CARVE_LOOP_AGAIN;
  125. }
  126. uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
  127. bool overlap_only_ram)
  128. {
  129. struct list_head *lhandle;
  130. struct efi_mem_list *newlist;
  131. bool carve_again;
  132. uint64_t carved_pages = 0;
  133. debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
  134. start, pages, memory_type, overlap_only_ram ? "yes" : "no");
  135. if (!pages)
  136. return start;
  137. newlist = calloc(1, sizeof(*newlist));
  138. newlist->desc.type = memory_type;
  139. newlist->desc.physical_start = start;
  140. newlist->desc.virtual_start = start;
  141. newlist->desc.num_pages = pages;
  142. switch (memory_type) {
  143. case EFI_RUNTIME_SERVICES_CODE:
  144. case EFI_RUNTIME_SERVICES_DATA:
  145. newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
  146. (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
  147. break;
  148. case EFI_MMAP_IO:
  149. newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
  150. break;
  151. default:
  152. newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
  153. break;
  154. }
  155. /* Add our new map */
  156. do {
  157. carve_again = false;
  158. list_for_each(lhandle, &efi_mem) {
  159. struct efi_mem_list *lmem;
  160. int r;
  161. lmem = list_entry(lhandle, struct efi_mem_list, link);
  162. r = efi_mem_carve_out(lmem, &newlist->desc,
  163. overlap_only_ram);
  164. switch (r) {
  165. case EFI_CARVE_OVERLAPS_NONRAM:
  166. /*
  167. * The user requested to only have RAM overlaps,
  168. * but we hit a non-RAM region. Error out.
  169. */
  170. return 0;
  171. case EFI_CARVE_NO_OVERLAP:
  172. /* Just ignore this list entry */
  173. break;
  174. case EFI_CARVE_LOOP_AGAIN:
  175. /*
  176. * We split an entry, but need to loop through
  177. * the list again to actually carve it.
  178. */
  179. carve_again = true;
  180. break;
  181. default:
  182. /* We carved a number of pages */
  183. carved_pages += r;
  184. carve_again = true;
  185. break;
  186. }
  187. if (carve_again) {
  188. /* The list changed, we need to start over */
  189. break;
  190. }
  191. }
  192. } while (carve_again);
  193. if (overlap_only_ram && (carved_pages != pages)) {
  194. /*
  195. * The payload wanted to have RAM overlaps, but we overlapped
  196. * with an unallocated region. Error out.
  197. */
  198. return 0;
  199. }
  200. /* Add our new map */
  201. list_add_tail(&newlist->link, &efi_mem);
  202. /* And make sure memory is listed in descending order */
  203. efi_mem_sort();
  204. return start;
  205. }
  206. static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
  207. {
  208. struct list_head *lhandle;
  209. list_for_each(lhandle, &efi_mem) {
  210. struct efi_mem_list *lmem = list_entry(lhandle,
  211. struct efi_mem_list, link);
  212. struct efi_mem_desc *desc = &lmem->desc;
  213. uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
  214. uint64_t desc_end = desc->physical_start + desc_len;
  215. uint64_t curmax = min(max_addr, desc_end);
  216. uint64_t ret = curmax - len;
  217. /* We only take memory from free RAM */
  218. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  219. continue;
  220. /* Out of bounds for max_addr */
  221. if ((ret + len) > max_addr)
  222. continue;
  223. /* Out of bounds for upper map limit */
  224. if ((ret + len) > desc_end)
  225. continue;
  226. /* Out of bounds for lower map limit */
  227. if (ret < desc->physical_start)
  228. continue;
  229. /* Return the highest address in this map within bounds */
  230. return ret;
  231. }
  232. return 0;
  233. }
  234. /*
  235. * Allocate memory pages.
  236. *
  237. * @type type of allocation to be performed
  238. * @memory_type usage type of the allocated memory
  239. * @pages number of pages to be allocated
  240. * @memory allocated memory
  241. * @return status code
  242. */
  243. efi_status_t efi_allocate_pages(int type, int memory_type,
  244. efi_uintn_t pages, uint64_t *memory)
  245. {
  246. u64 len = pages << EFI_PAGE_SHIFT;
  247. efi_status_t r = EFI_SUCCESS;
  248. uint64_t addr;
  249. switch (type) {
  250. case EFI_ALLOCATE_ANY_PAGES:
  251. /* Any page */
  252. addr = efi_find_free_memory(len, gd->start_addr_sp);
  253. if (!addr) {
  254. r = EFI_NOT_FOUND;
  255. break;
  256. }
  257. break;
  258. case EFI_ALLOCATE_MAX_ADDRESS:
  259. /* Max address */
  260. addr = efi_find_free_memory(len, *memory);
  261. if (!addr) {
  262. r = EFI_NOT_FOUND;
  263. break;
  264. }
  265. break;
  266. case EFI_ALLOCATE_ADDRESS:
  267. /* Exact address, reserve it. The addr is already in *memory. */
  268. addr = *memory;
  269. break;
  270. default:
  271. /* UEFI doesn't specify other allocation types */
  272. r = EFI_INVALID_PARAMETER;
  273. break;
  274. }
  275. if (r == EFI_SUCCESS) {
  276. uint64_t ret;
  277. /* Reserve that map in our memory maps */
  278. ret = efi_add_memory_map(addr, pages, memory_type, true);
  279. if (ret == addr) {
  280. *memory = addr;
  281. } else {
  282. /* Map would overlap, bail out */
  283. r = EFI_OUT_OF_RESOURCES;
  284. }
  285. }
  286. return r;
  287. }
  288. void *efi_alloc(uint64_t len, int memory_type)
  289. {
  290. uint64_t ret = 0;
  291. uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
  292. efi_status_t r;
  293. r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
  294. &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(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
  334. &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(EFI_ALLOCATE_MAX_ADDRESS, 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. }