memmap.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common EFI memory map functions.
  4. */
  5. #define pr_fmt(fmt) "efi: " fmt
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/efi.h>
  9. #include <linux/io.h>
  10. #include <asm/early_ioremap.h>
  11. #include <linux/memblock.h>
  12. #include <linux/slab.h>
  13. static phys_addr_t __init __efi_memmap_alloc_early(unsigned long size)
  14. {
  15. return memblock_phys_alloc(size, SMP_CACHE_BYTES);
  16. }
  17. static phys_addr_t __init __efi_memmap_alloc_late(unsigned long size)
  18. {
  19. unsigned int order = get_order(size);
  20. struct page *p = alloc_pages(GFP_KERNEL, order);
  21. if (!p)
  22. return 0;
  23. return PFN_PHYS(page_to_pfn(p));
  24. }
  25. void __init __efi_memmap_free(u64 phys, unsigned long size, unsigned long flags)
  26. {
  27. if (flags & EFI_MEMMAP_MEMBLOCK) {
  28. if (slab_is_available())
  29. memblock_free_late(phys, size);
  30. else
  31. memblock_phys_free(phys, size);
  32. } else if (flags & EFI_MEMMAP_SLAB) {
  33. struct page *p = pfn_to_page(PHYS_PFN(phys));
  34. unsigned int order = get_order(size);
  35. free_pages((unsigned long) page_address(p), order);
  36. }
  37. }
  38. static void __init efi_memmap_free(void)
  39. {
  40. __efi_memmap_free(efi.memmap.phys_map,
  41. efi.memmap.desc_size * efi.memmap.nr_map,
  42. efi.memmap.flags);
  43. }
  44. /**
  45. * efi_memmap_alloc - Allocate memory for the EFI memory map
  46. * @num_entries: Number of entries in the allocated map.
  47. * @data: efi memmap installation parameters
  48. *
  49. * Depending on whether mm_init() has already been invoked or not,
  50. * either memblock or "normal" page allocation is used.
  51. *
  52. * Returns zero on success, a negative error code on failure.
  53. */
  54. int __init efi_memmap_alloc(unsigned int num_entries,
  55. struct efi_memory_map_data *data)
  56. {
  57. /* Expect allocation parameters are zero initialized */
  58. WARN_ON(data->phys_map || data->size);
  59. data->size = num_entries * efi.memmap.desc_size;
  60. data->desc_version = efi.memmap.desc_version;
  61. data->desc_size = efi.memmap.desc_size;
  62. data->flags &= ~(EFI_MEMMAP_SLAB | EFI_MEMMAP_MEMBLOCK);
  63. data->flags |= efi.memmap.flags & EFI_MEMMAP_LATE;
  64. if (slab_is_available()) {
  65. data->flags |= EFI_MEMMAP_SLAB;
  66. data->phys_map = __efi_memmap_alloc_late(data->size);
  67. } else {
  68. data->flags |= EFI_MEMMAP_MEMBLOCK;
  69. data->phys_map = __efi_memmap_alloc_early(data->size);
  70. }
  71. if (!data->phys_map)
  72. return -ENOMEM;
  73. return 0;
  74. }
  75. /**
  76. * __efi_memmap_init - Common code for mapping the EFI memory map
  77. * @data: EFI memory map data
  78. *
  79. * This function takes care of figuring out which function to use to
  80. * map the EFI memory map in efi.memmap based on how far into the boot
  81. * we are.
  82. *
  83. * During bootup EFI_MEMMAP_LATE in data->flags should be clear since we
  84. * only have access to the early_memremap*() functions as the vmalloc
  85. * space isn't setup. Once the kernel is fully booted we can fallback
  86. * to the more robust memremap*() API.
  87. *
  88. * Returns zero on success, a negative error code on failure.
  89. */
  90. static int __init __efi_memmap_init(struct efi_memory_map_data *data)
  91. {
  92. struct efi_memory_map map;
  93. phys_addr_t phys_map;
  94. if (efi_enabled(EFI_PARAVIRT))
  95. return 0;
  96. phys_map = data->phys_map;
  97. if (data->flags & EFI_MEMMAP_LATE)
  98. map.map = memremap(phys_map, data->size, MEMREMAP_WB);
  99. else
  100. map.map = early_memremap(phys_map, data->size);
  101. if (!map.map) {
  102. pr_err("Could not map the memory map!\n");
  103. return -ENOMEM;
  104. }
  105. /* NOP if data->flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB) == 0 */
  106. efi_memmap_free();
  107. map.phys_map = data->phys_map;
  108. map.nr_map = data->size / data->desc_size;
  109. map.map_end = map.map + data->size;
  110. map.desc_version = data->desc_version;
  111. map.desc_size = data->desc_size;
  112. map.flags = data->flags;
  113. set_bit(EFI_MEMMAP, &efi.flags);
  114. efi.memmap = map;
  115. return 0;
  116. }
  117. /**
  118. * efi_memmap_init_early - Map the EFI memory map data structure
  119. * @data: EFI memory map data
  120. *
  121. * Use early_memremap() to map the passed in EFI memory map and assign
  122. * it to efi.memmap.
  123. */
  124. int __init efi_memmap_init_early(struct efi_memory_map_data *data)
  125. {
  126. /* Cannot go backwards */
  127. WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
  128. data->flags = 0;
  129. return __efi_memmap_init(data);
  130. }
  131. void __init efi_memmap_unmap(void)
  132. {
  133. if (!efi_enabled(EFI_MEMMAP))
  134. return;
  135. if (!(efi.memmap.flags & EFI_MEMMAP_LATE)) {
  136. unsigned long size;
  137. size = efi.memmap.desc_size * efi.memmap.nr_map;
  138. early_memunmap(efi.memmap.map, size);
  139. } else {
  140. memunmap(efi.memmap.map);
  141. }
  142. efi.memmap.map = NULL;
  143. clear_bit(EFI_MEMMAP, &efi.flags);
  144. }
  145. /**
  146. * efi_memmap_init_late - Map efi.memmap with memremap()
  147. * @phys_addr: Physical address of the new EFI memory map
  148. * @size: Size in bytes of the new EFI memory map
  149. *
  150. * Setup a mapping of the EFI memory map using ioremap_cache(). This
  151. * function should only be called once the vmalloc space has been
  152. * setup and is therefore not suitable for calling during early EFI
  153. * initialise, e.g. in efi_init(). Additionally, it expects
  154. * efi_memmap_init_early() to have already been called.
  155. *
  156. * The reason there are two EFI memmap initialisation
  157. * (efi_memmap_init_early() and this late version) is because the
  158. * early EFI memmap should be explicitly unmapped once EFI
  159. * initialisation is complete as the fixmap space used to map the EFI
  160. * memmap (via early_memremap()) is a scarce resource.
  161. *
  162. * This late mapping is intended to persist for the duration of
  163. * runtime so that things like efi_mem_desc_lookup() and
  164. * efi_mem_attributes() always work.
  165. *
  166. * Returns zero on success, a negative error code on failure.
  167. */
  168. int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size)
  169. {
  170. struct efi_memory_map_data data = {
  171. .phys_map = addr,
  172. .size = size,
  173. .flags = EFI_MEMMAP_LATE,
  174. };
  175. /* Did we forget to unmap the early EFI memmap? */
  176. WARN_ON(efi.memmap.map);
  177. /* Were we already called? */
  178. WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
  179. /*
  180. * It makes no sense to allow callers to register different
  181. * values for the following fields. Copy them out of the
  182. * existing early EFI memmap.
  183. */
  184. data.desc_version = efi.memmap.desc_version;
  185. data.desc_size = efi.memmap.desc_size;
  186. return __efi_memmap_init(&data);
  187. }
  188. /**
  189. * efi_memmap_install - Install a new EFI memory map in efi.memmap
  190. * @ctx: map allocation parameters (address, size, flags)
  191. *
  192. * Unlike efi_memmap_init_*(), this function does not allow the caller
  193. * to switch from early to late mappings. It simply uses the existing
  194. * mapping function and installs the new memmap.
  195. *
  196. * Returns zero on success, a negative error code on failure.
  197. */
  198. int __init efi_memmap_install(struct efi_memory_map_data *data)
  199. {
  200. efi_memmap_unmap();
  201. return __efi_memmap_init(data);
  202. }
  203. /**
  204. * efi_memmap_split_count - Count number of additional EFI memmap entries
  205. * @md: EFI memory descriptor to split
  206. * @range: Address range (start, end) to split around
  207. *
  208. * Returns the number of additional EFI memmap entries required to
  209. * accommodate @range.
  210. */
  211. int __init efi_memmap_split_count(efi_memory_desc_t *md, struct range *range)
  212. {
  213. u64 m_start, m_end;
  214. u64 start, end;
  215. int count = 0;
  216. start = md->phys_addr;
  217. end = start + (md->num_pages << EFI_PAGE_SHIFT) - 1;
  218. /* modifying range */
  219. m_start = range->start;
  220. m_end = range->end;
  221. if (m_start <= start) {
  222. /* split into 2 parts */
  223. if (start < m_end && m_end < end)
  224. count++;
  225. }
  226. if (start < m_start && m_start < end) {
  227. /* split into 3 parts */
  228. if (m_end < end)
  229. count += 2;
  230. /* split into 2 parts */
  231. if (end <= m_end)
  232. count++;
  233. }
  234. return count;
  235. }
  236. /**
  237. * efi_memmap_insert - Insert a memory region in an EFI memmap
  238. * @old_memmap: The existing EFI memory map structure
  239. * @buf: Address of buffer to store new map
  240. * @mem: Memory map entry to insert
  241. *
  242. * It is suggested that you call efi_memmap_split_count() first
  243. * to see how large @buf needs to be.
  244. */
  245. void __init efi_memmap_insert(struct efi_memory_map *old_memmap, void *buf,
  246. struct efi_mem_range *mem)
  247. {
  248. u64 m_start, m_end, m_attr;
  249. efi_memory_desc_t *md;
  250. u64 start, end;
  251. void *old, *new;
  252. /* modifying range */
  253. m_start = mem->range.start;
  254. m_end = mem->range.end;
  255. m_attr = mem->attribute;
  256. /*
  257. * The EFI memory map deals with regions in EFI_PAGE_SIZE
  258. * units. Ensure that the region described by 'mem' is aligned
  259. * correctly.
  260. */
  261. if (!IS_ALIGNED(m_start, EFI_PAGE_SIZE) ||
  262. !IS_ALIGNED(m_end + 1, EFI_PAGE_SIZE)) {
  263. WARN_ON(1);
  264. return;
  265. }
  266. for (old = old_memmap->map, new = buf;
  267. old < old_memmap->map_end;
  268. old += old_memmap->desc_size, new += old_memmap->desc_size) {
  269. /* copy original EFI memory descriptor */
  270. memcpy(new, old, old_memmap->desc_size);
  271. md = new;
  272. start = md->phys_addr;
  273. end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
  274. if (m_start <= start && end <= m_end)
  275. md->attribute |= m_attr;
  276. if (m_start <= start &&
  277. (start < m_end && m_end < end)) {
  278. /* first part */
  279. md->attribute |= m_attr;
  280. md->num_pages = (m_end - md->phys_addr + 1) >>
  281. EFI_PAGE_SHIFT;
  282. /* latter part */
  283. new += old_memmap->desc_size;
  284. memcpy(new, old, old_memmap->desc_size);
  285. md = new;
  286. md->phys_addr = m_end + 1;
  287. md->num_pages = (end - md->phys_addr + 1) >>
  288. EFI_PAGE_SHIFT;
  289. }
  290. if ((start < m_start && m_start < end) && m_end < end) {
  291. /* first part */
  292. md->num_pages = (m_start - md->phys_addr) >>
  293. EFI_PAGE_SHIFT;
  294. /* middle part */
  295. new += old_memmap->desc_size;
  296. memcpy(new, old, old_memmap->desc_size);
  297. md = new;
  298. md->attribute |= m_attr;
  299. md->phys_addr = m_start;
  300. md->num_pages = (m_end - m_start + 1) >>
  301. EFI_PAGE_SHIFT;
  302. /* last part */
  303. new += old_memmap->desc_size;
  304. memcpy(new, old, old_memmap->desc_size);
  305. md = new;
  306. md->phys_addr = m_end + 1;
  307. md->num_pages = (end - m_end) >>
  308. EFI_PAGE_SHIFT;
  309. }
  310. if ((start < m_start && m_start < end) &&
  311. (end <= m_end)) {
  312. /* first part */
  313. md->num_pages = (m_start - md->phys_addr) >>
  314. EFI_PAGE_SHIFT;
  315. /* latter part */
  316. new += old_memmap->desc_size;
  317. memcpy(new, old, old_memmap->desc_size);
  318. md = new;
  319. md->phys_addr = m_start;
  320. md->num_pages = (end - md->phys_addr + 1) >>
  321. EFI_PAGE_SHIFT;
  322. md->attribute |= m_attr;
  323. }
  324. }
  325. }