init_64.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  6. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  7. * Copyright (C) 1996 Paul Mackerras
  8. *
  9. * Derived from "arch/i386/mm/init.c"
  10. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  11. *
  12. * Dave Engebretsen <engebret@us.ibm.com>
  13. * Rework for PPC64 port.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. */
  21. #undef DEBUG
  22. #include <linux/signal.h>
  23. #include <linux/sched.h>
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/string.h>
  27. #include <linux/types.h>
  28. #include <linux/mman.h>
  29. #include <linux/mm.h>
  30. #include <linux/swap.h>
  31. #include <linux/stddef.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/init.h>
  34. #include <linux/delay.h>
  35. #include <linux/highmem.h>
  36. #include <linux/idr.h>
  37. #include <linux/nodemask.h>
  38. #include <linux/module.h>
  39. #include <linux/poison.h>
  40. #include <linux/memblock.h>
  41. #include <linux/hugetlb.h>
  42. #include <linux/slab.h>
  43. #include <linux/of_fdt.h>
  44. #include <linux/libfdt.h>
  45. #include <linux/memremap.h>
  46. #include <asm/pgalloc.h>
  47. #include <asm/page.h>
  48. #include <asm/prom.h>
  49. #include <asm/rtas.h>
  50. #include <asm/io.h>
  51. #include <asm/mmu_context.h>
  52. #include <asm/pgtable.h>
  53. #include <asm/mmu.h>
  54. #include <linux/uaccess.h>
  55. #include <asm/smp.h>
  56. #include <asm/machdep.h>
  57. #include <asm/tlb.h>
  58. #include <asm/eeh.h>
  59. #include <asm/processor.h>
  60. #include <asm/mmzone.h>
  61. #include <asm/cputable.h>
  62. #include <asm/sections.h>
  63. #include <asm/iommu.h>
  64. #include <asm/vdso.h>
  65. #include "mmu_decl.h"
  66. phys_addr_t memstart_addr = ~0;
  67. EXPORT_SYMBOL_GPL(memstart_addr);
  68. phys_addr_t kernstart_addr;
  69. EXPORT_SYMBOL_GPL(kernstart_addr);
  70. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  71. /*
  72. * Given an address within the vmemmap, determine the pfn of the page that
  73. * represents the start of the section it is within. Note that we have to
  74. * do this by hand as the proffered address may not be correctly aligned.
  75. * Subtraction of non-aligned pointers produces undefined results.
  76. */
  77. static unsigned long __meminit vmemmap_section_start(unsigned long page)
  78. {
  79. unsigned long offset = page - ((unsigned long)(vmemmap));
  80. /* Return the pfn of the start of the section. */
  81. return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
  82. }
  83. /*
  84. * Check if this vmemmap page is already initialised. If any section
  85. * which overlaps this vmemmap page is initialised then this page is
  86. * initialised already.
  87. */
  88. static int __meminit vmemmap_populated(unsigned long start, int page_size)
  89. {
  90. unsigned long end = start + page_size;
  91. start = (unsigned long)(pfn_to_page(vmemmap_section_start(start)));
  92. for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
  93. if (pfn_valid(page_to_pfn((struct page *)start)))
  94. return 1;
  95. return 0;
  96. }
  97. /*
  98. * vmemmap virtual address space management does not have a traditonal page
  99. * table to track which virtual struct pages are backed by physical mapping.
  100. * The virtual to physical mappings are tracked in a simple linked list
  101. * format. 'vmemmap_list' maintains the entire vmemmap physical mapping at
  102. * all times where as the 'next' list maintains the available
  103. * vmemmap_backing structures which have been deleted from the
  104. * 'vmemmap_global' list during system runtime (memory hotplug remove
  105. * operation). The freed 'vmemmap_backing' structures are reused later when
  106. * new requests come in without allocating fresh memory. This pointer also
  107. * tracks the allocated 'vmemmap_backing' structures as we allocate one
  108. * full page memory at a time when we dont have any.
  109. */
  110. struct vmemmap_backing *vmemmap_list;
  111. static struct vmemmap_backing *next;
  112. /*
  113. * The same pointer 'next' tracks individual chunks inside the allocated
  114. * full page during the boot time and again tracks the freeed nodes during
  115. * runtime. It is racy but it does not happen as they are separated by the
  116. * boot process. Will create problem if some how we have memory hotplug
  117. * operation during boot !!
  118. */
  119. static int num_left;
  120. static int num_freed;
  121. static __meminit struct vmemmap_backing * vmemmap_list_alloc(int node)
  122. {
  123. struct vmemmap_backing *vmem_back;
  124. /* get from freed entries first */
  125. if (num_freed) {
  126. num_freed--;
  127. vmem_back = next;
  128. next = next->list;
  129. return vmem_back;
  130. }
  131. /* allocate a page when required and hand out chunks */
  132. if (!num_left) {
  133. next = vmemmap_alloc_block(PAGE_SIZE, node);
  134. if (unlikely(!next)) {
  135. WARN_ON(1);
  136. return NULL;
  137. }
  138. num_left = PAGE_SIZE / sizeof(struct vmemmap_backing);
  139. }
  140. num_left--;
  141. return next++;
  142. }
  143. static __meminit void vmemmap_list_populate(unsigned long phys,
  144. unsigned long start,
  145. int node)
  146. {
  147. struct vmemmap_backing *vmem_back;
  148. vmem_back = vmemmap_list_alloc(node);
  149. if (unlikely(!vmem_back)) {
  150. WARN_ON(1);
  151. return;
  152. }
  153. vmem_back->phys = phys;
  154. vmem_back->virt_addr = start;
  155. vmem_back->list = vmemmap_list;
  156. vmemmap_list = vmem_back;
  157. }
  158. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  159. struct vmem_altmap *altmap)
  160. {
  161. unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
  162. /* Align to the page size of the linear mapping. */
  163. start = _ALIGN_DOWN(start, page_size);
  164. pr_debug("vmemmap_populate %lx..%lx, node %d\n", start, end, node);
  165. for (; start < end; start += page_size) {
  166. void *p = NULL;
  167. int rc;
  168. if (vmemmap_populated(start, page_size))
  169. continue;
  170. /*
  171. * Allocate from the altmap first if we have one. This may
  172. * fail due to alignment issues when using 16MB hugepages, so
  173. * fall back to system memory if the altmap allocation fail.
  174. */
  175. if (altmap)
  176. p = altmap_alloc_block_buf(page_size, altmap);
  177. if (!p)
  178. p = vmemmap_alloc_block_buf(page_size, node);
  179. if (!p)
  180. return -ENOMEM;
  181. vmemmap_list_populate(__pa(p), start, node);
  182. pr_debug(" * %016lx..%016lx allocated at %p\n",
  183. start, start + page_size, p);
  184. rc = vmemmap_create_mapping(start, page_size, __pa(p));
  185. if (rc < 0) {
  186. pr_warn("%s: Unable to create vmemmap mapping: %d\n",
  187. __func__, rc);
  188. return -EFAULT;
  189. }
  190. }
  191. return 0;
  192. }
  193. #ifdef CONFIG_MEMORY_HOTPLUG
  194. static unsigned long vmemmap_list_free(unsigned long start)
  195. {
  196. struct vmemmap_backing *vmem_back, *vmem_back_prev;
  197. vmem_back_prev = vmem_back = vmemmap_list;
  198. /* look for it with prev pointer recorded */
  199. for (; vmem_back; vmem_back = vmem_back->list) {
  200. if (vmem_back->virt_addr == start)
  201. break;
  202. vmem_back_prev = vmem_back;
  203. }
  204. if (unlikely(!vmem_back)) {
  205. WARN_ON(1);
  206. return 0;
  207. }
  208. /* remove it from vmemmap_list */
  209. if (vmem_back == vmemmap_list) /* remove head */
  210. vmemmap_list = vmem_back->list;
  211. else
  212. vmem_back_prev->list = vmem_back->list;
  213. /* next point to this freed entry */
  214. vmem_back->list = next;
  215. next = vmem_back;
  216. num_freed++;
  217. return vmem_back->phys;
  218. }
  219. void __ref vmemmap_free(unsigned long start, unsigned long end,
  220. struct vmem_altmap *altmap)
  221. {
  222. unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
  223. unsigned long page_order = get_order(page_size);
  224. unsigned long alt_start = ~0, alt_end = ~0;
  225. unsigned long base_pfn;
  226. start = _ALIGN_DOWN(start, page_size);
  227. if (altmap) {
  228. alt_start = altmap->base_pfn;
  229. alt_end = altmap->base_pfn + altmap->reserve +
  230. altmap->free + altmap->alloc + altmap->align;
  231. }
  232. pr_debug("vmemmap_free %lx...%lx\n", start, end);
  233. for (; start < end; start += page_size) {
  234. unsigned long nr_pages, addr;
  235. struct page *section_base;
  236. struct page *page;
  237. /*
  238. * the section has already be marked as invalid, so
  239. * vmemmap_populated() true means some other sections still
  240. * in this page, so skip it.
  241. */
  242. if (vmemmap_populated(start, page_size))
  243. continue;
  244. addr = vmemmap_list_free(start);
  245. if (!addr)
  246. continue;
  247. page = pfn_to_page(addr >> PAGE_SHIFT);
  248. section_base = pfn_to_page(vmemmap_section_start(start));
  249. nr_pages = 1 << page_order;
  250. base_pfn = PHYS_PFN(addr);
  251. if (base_pfn >= alt_start && base_pfn < alt_end) {
  252. vmem_altmap_free(altmap, nr_pages);
  253. } else if (PageReserved(page)) {
  254. /* allocated from bootmem */
  255. if (page_size < PAGE_SIZE) {
  256. /*
  257. * this shouldn't happen, but if it is
  258. * the case, leave the memory there
  259. */
  260. WARN_ON_ONCE(1);
  261. } else {
  262. while (nr_pages--)
  263. free_reserved_page(page++);
  264. }
  265. } else {
  266. free_pages((unsigned long)(__va(addr)), page_order);
  267. }
  268. vmemmap_remove_mapping(start, page_size);
  269. }
  270. }
  271. #endif
  272. void register_page_bootmem_memmap(unsigned long section_nr,
  273. struct page *start_page, unsigned long size)
  274. {
  275. }
  276. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  277. #ifdef CONFIG_PPC_BOOK3S_64
  278. static bool disable_radix = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
  279. static int __init parse_disable_radix(char *p)
  280. {
  281. bool val;
  282. if (!p)
  283. val = true;
  284. else if (kstrtobool(p, &val))
  285. return -EINVAL;
  286. disable_radix = val;
  287. return 0;
  288. }
  289. early_param("disable_radix", parse_disable_radix);
  290. /*
  291. * If we're running under a hypervisor, we need to check the contents of
  292. * /chosen/ibm,architecture-vec-5 to see if the hypervisor is willing to do
  293. * radix. If not, we clear the radix feature bit so we fall back to hash.
  294. */
  295. static void __init early_check_vec5(void)
  296. {
  297. unsigned long root, chosen;
  298. int size;
  299. const u8 *vec5;
  300. u8 mmu_supported;
  301. root = of_get_flat_dt_root();
  302. chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
  303. if (chosen == -FDT_ERR_NOTFOUND) {
  304. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  305. return;
  306. }
  307. vec5 = of_get_flat_dt_prop(chosen, "ibm,architecture-vec-5", &size);
  308. if (!vec5) {
  309. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  310. return;
  311. }
  312. if (size <= OV5_INDX(OV5_MMU_SUPPORT)) {
  313. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  314. return;
  315. }
  316. /* Check for supported configuration */
  317. mmu_supported = vec5[OV5_INDX(OV5_MMU_SUPPORT)] &
  318. OV5_FEAT(OV5_MMU_SUPPORT);
  319. if (mmu_supported == OV5_FEAT(OV5_MMU_RADIX)) {
  320. /* Hypervisor only supports radix - check enabled && GTSE */
  321. if (!early_radix_enabled()) {
  322. pr_warn("WARNING: Ignoring cmdline option disable_radix\n");
  323. }
  324. if (!(vec5[OV5_INDX(OV5_RADIX_GTSE)] &
  325. OV5_FEAT(OV5_RADIX_GTSE))) {
  326. pr_warn("WARNING: Hypervisor doesn't support RADIX with GTSE\n");
  327. }
  328. /* Do radix anyway - the hypervisor said we had to */
  329. cur_cpu_spec->mmu_features |= MMU_FTR_TYPE_RADIX;
  330. } else if (mmu_supported == OV5_FEAT(OV5_MMU_HASH)) {
  331. /* Hypervisor only supports hash - disable radix */
  332. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  333. }
  334. }
  335. void __init mmu_early_init_devtree(void)
  336. {
  337. /* Disable radix mode based on kernel command line. */
  338. if (disable_radix)
  339. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  340. /*
  341. * Check /chosen/ibm,architecture-vec-5 if running as a guest.
  342. * When running bare-metal, we can use radix if we like
  343. * even though the ibm,architecture-vec-5 property created by
  344. * skiboot doesn't have the necessary bits set.
  345. */
  346. if (!(mfmsr() & MSR_HV))
  347. early_check_vec5();
  348. if (early_radix_enabled())
  349. radix__early_init_devtree();
  350. else
  351. hash__early_init_devtree();
  352. }
  353. #endif /* CONFIG_PPC_BOOK3S_64 */