memmap.c 8.8 KB

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