memmap.c 9.9 KB

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