init_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PowerPC version
  4. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  5. *
  6. * Modifications by Paul Mackerras (PowerMac) ([email protected])
  7. * and Cort Dougan (PReP) ([email protected])
  8. * Copyright (C) 1996 Paul Mackerras
  9. *
  10. * Derived from "arch/i386/mm/init.c"
  11. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  12. *
  13. * Dave Engebretsen <[email protected]>
  14. * Rework for PPC64 port.
  15. */
  16. #undef DEBUG
  17. #include <linux/signal.h>
  18. #include <linux/sched.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/types.h>
  23. #include <linux/mman.h>
  24. #include <linux/mm.h>
  25. #include <linux/swap.h>
  26. #include <linux/stddef.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/init.h>
  29. #include <linux/delay.h>
  30. #include <linux/highmem.h>
  31. #include <linux/idr.h>
  32. #include <linux/nodemask.h>
  33. #include <linux/module.h>
  34. #include <linux/poison.h>
  35. #include <linux/memblock.h>
  36. #include <linux/hugetlb.h>
  37. #include <linux/slab.h>
  38. #include <linux/of_fdt.h>
  39. #include <linux/libfdt.h>
  40. #include <linux/memremap.h>
  41. #include <asm/pgalloc.h>
  42. #include <asm/page.h>
  43. #include <asm/prom.h>
  44. #include <asm/rtas.h>
  45. #include <asm/io.h>
  46. #include <asm/mmu_context.h>
  47. #include <asm/mmu.h>
  48. #include <linux/uaccess.h>
  49. #include <asm/smp.h>
  50. #include <asm/machdep.h>
  51. #include <asm/tlb.h>
  52. #include <asm/eeh.h>
  53. #include <asm/processor.h>
  54. #include <asm/mmzone.h>
  55. #include <asm/cputable.h>
  56. #include <asm/sections.h>
  57. #include <asm/iommu.h>
  58. #include <asm/vdso.h>
  59. #include <asm/hugetlb.h>
  60. #include <mm/mmu_decl.h>
  61. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  62. /*
  63. * Given an address within the vmemmap, determine the page that
  64. * represents the start of the subsection it is within. Note that we have to
  65. * do this by hand as the proffered address may not be correctly aligned.
  66. * Subtraction of non-aligned pointers produces undefined results.
  67. */
  68. static struct page * __meminit vmemmap_subsection_start(unsigned long vmemmap_addr)
  69. {
  70. unsigned long start_pfn;
  71. unsigned long offset = vmemmap_addr - ((unsigned long)(vmemmap));
  72. /* Return the pfn of the start of the section. */
  73. start_pfn = (offset / sizeof(struct page)) & PAGE_SUBSECTION_MASK;
  74. return pfn_to_page(start_pfn);
  75. }
  76. /*
  77. * Since memory is added in sub-section chunks, before creating a new vmemmap
  78. * mapping, the kernel should check whether there is an existing memmap mapping
  79. * covering the new subsection added. This is needed because kernel can map
  80. * vmemmap area using 16MB pages which will cover a memory range of 16G. Such
  81. * a range covers multiple subsections (2M)
  82. *
  83. * If any subsection in the 16G range mapped by vmemmap is valid we consider the
  84. * vmemmap populated (There is a page table entry already present). We can't do
  85. * a page table lookup here because with the hash translation we don't keep
  86. * vmemmap details in linux page table.
  87. */
  88. static int __meminit vmemmap_populated(unsigned long vmemmap_addr, int vmemmap_map_size)
  89. {
  90. struct page *start;
  91. unsigned long vmemmap_end = vmemmap_addr + vmemmap_map_size;
  92. start = vmemmap_subsection_start(vmemmap_addr);
  93. for (; (unsigned long)start < vmemmap_end; start += PAGES_PER_SUBSECTION)
  94. /*
  95. * pfn valid check here is intended to really check
  96. * whether we have any subsection already initialized
  97. * in this range.
  98. */
  99. if (pfn_valid(page_to_pfn(start)))
  100. return 1;
  101. return 0;
  102. }
  103. /*
  104. * vmemmap virtual address space management does not have a traditional page
  105. * table to track which virtual struct pages are backed by physical mapping.
  106. * The virtual to physical mappings are tracked in a simple linked list
  107. * format. 'vmemmap_list' maintains the entire vmemmap physical mapping at
  108. * all times where as the 'next' list maintains the available
  109. * vmemmap_backing structures which have been deleted from the
  110. * 'vmemmap_global' list during system runtime (memory hotplug remove
  111. * operation). The freed 'vmemmap_backing' structures are reused later when
  112. * new requests come in without allocating fresh memory. This pointer also
  113. * tracks the allocated 'vmemmap_backing' structures as we allocate one
  114. * full page memory at a time when we dont have any.
  115. */
  116. struct vmemmap_backing *vmemmap_list;
  117. static struct vmemmap_backing *next;
  118. /*
  119. * The same pointer 'next' tracks individual chunks inside the allocated
  120. * full page during the boot time and again tracks the freed nodes during
  121. * runtime. It is racy but it does not happen as they are separated by the
  122. * boot process. Will create problem if some how we have memory hotplug
  123. * operation during boot !!
  124. */
  125. static int num_left;
  126. static int num_freed;
  127. static __meminit struct vmemmap_backing * vmemmap_list_alloc(int node)
  128. {
  129. struct vmemmap_backing *vmem_back;
  130. /* get from freed entries first */
  131. if (num_freed) {
  132. num_freed--;
  133. vmem_back = next;
  134. next = next->list;
  135. return vmem_back;
  136. }
  137. /* allocate a page when required and hand out chunks */
  138. if (!num_left) {
  139. next = vmemmap_alloc_block(PAGE_SIZE, node);
  140. if (unlikely(!next)) {
  141. WARN_ON(1);
  142. return NULL;
  143. }
  144. num_left = PAGE_SIZE / sizeof(struct vmemmap_backing);
  145. }
  146. num_left--;
  147. return next++;
  148. }
  149. static __meminit int vmemmap_list_populate(unsigned long phys,
  150. unsigned long start,
  151. int node)
  152. {
  153. struct vmemmap_backing *vmem_back;
  154. vmem_back = vmemmap_list_alloc(node);
  155. if (unlikely(!vmem_back)) {
  156. pr_debug("vmemap list allocation failed\n");
  157. return -ENOMEM;
  158. }
  159. vmem_back->phys = phys;
  160. vmem_back->virt_addr = start;
  161. vmem_back->list = vmemmap_list;
  162. vmemmap_list = vmem_back;
  163. return 0;
  164. }
  165. static bool altmap_cross_boundary(struct vmem_altmap *altmap, unsigned long start,
  166. unsigned long page_size)
  167. {
  168. unsigned long nr_pfn = page_size / sizeof(struct page);
  169. unsigned long start_pfn = page_to_pfn((struct page *)start);
  170. if ((start_pfn + nr_pfn - 1) > altmap->end_pfn)
  171. return true;
  172. if (start_pfn < altmap->base_pfn)
  173. return true;
  174. return false;
  175. }
  176. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  177. struct vmem_altmap *altmap)
  178. {
  179. bool altmap_alloc;
  180. unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
  181. /* Align to the page size of the linear mapping. */
  182. start = ALIGN_DOWN(start, page_size);
  183. pr_debug("vmemmap_populate %lx..%lx, node %d\n", start, end, node);
  184. for (; start < end; start += page_size) {
  185. void *p = NULL;
  186. int rc;
  187. /*
  188. * This vmemmap range is backing different subsections. If any
  189. * of that subsection is marked valid, that means we already
  190. * have initialized a page table covering this range and hence
  191. * the vmemmap range is populated.
  192. */
  193. if (vmemmap_populated(start, page_size))
  194. continue;
  195. /*
  196. * Allocate from the altmap first if we have one. This may
  197. * fail due to alignment issues when using 16MB hugepages, so
  198. * fall back to system memory if the altmap allocation fail.
  199. */
  200. if (altmap && !altmap_cross_boundary(altmap, start, page_size)) {
  201. p = vmemmap_alloc_block_buf(page_size, node, altmap);
  202. if (!p)
  203. pr_debug("altmap block allocation failed, falling back to system memory");
  204. else
  205. altmap_alloc = true;
  206. }
  207. if (!p) {
  208. p = vmemmap_alloc_block_buf(page_size, node, NULL);
  209. altmap_alloc = false;
  210. }
  211. if (!p)
  212. return -ENOMEM;
  213. if (vmemmap_list_populate(__pa(p), start, node)) {
  214. /*
  215. * If we don't populate vmemap list, we don't have
  216. * the ability to free the allocated vmemmap
  217. * pages in section_deactivate. Hence free them
  218. * here.
  219. */
  220. int nr_pfns = page_size >> PAGE_SHIFT;
  221. unsigned long page_order = get_order(page_size);
  222. if (altmap_alloc)
  223. vmem_altmap_free(altmap, nr_pfns);
  224. else
  225. free_pages((unsigned long)p, page_order);
  226. return -ENOMEM;
  227. }
  228. pr_debug(" * %016lx..%016lx allocated at %p\n",
  229. start, start + page_size, p);
  230. rc = vmemmap_create_mapping(start, page_size, __pa(p));
  231. if (rc < 0) {
  232. pr_warn("%s: Unable to create vmemmap mapping: %d\n",
  233. __func__, rc);
  234. return -EFAULT;
  235. }
  236. }
  237. return 0;
  238. }
  239. #ifdef CONFIG_MEMORY_HOTPLUG
  240. static unsigned long vmemmap_list_free(unsigned long start)
  241. {
  242. struct vmemmap_backing *vmem_back, *vmem_back_prev;
  243. vmem_back_prev = vmem_back = vmemmap_list;
  244. /* look for it with prev pointer recorded */
  245. for (; vmem_back; vmem_back = vmem_back->list) {
  246. if (vmem_back->virt_addr == start)
  247. break;
  248. vmem_back_prev = vmem_back;
  249. }
  250. if (unlikely(!vmem_back))
  251. return 0;
  252. /* remove it from vmemmap_list */
  253. if (vmem_back == vmemmap_list) /* remove head */
  254. vmemmap_list = vmem_back->list;
  255. else
  256. vmem_back_prev->list = vmem_back->list;
  257. /* next point to this freed entry */
  258. vmem_back->list = next;
  259. next = vmem_back;
  260. num_freed++;
  261. return vmem_back->phys;
  262. }
  263. void __ref vmemmap_free(unsigned long start, unsigned long end,
  264. struct vmem_altmap *altmap)
  265. {
  266. unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
  267. unsigned long page_order = get_order(page_size);
  268. unsigned long alt_start = ~0, alt_end = ~0;
  269. unsigned long base_pfn;
  270. start = ALIGN_DOWN(start, page_size);
  271. if (altmap) {
  272. alt_start = altmap->base_pfn;
  273. alt_end = altmap->base_pfn + altmap->reserve + altmap->free;
  274. }
  275. pr_debug("vmemmap_free %lx...%lx\n", start, end);
  276. for (; start < end; start += page_size) {
  277. unsigned long nr_pages, addr;
  278. struct page *page;
  279. /*
  280. * We have already marked the subsection we are trying to remove
  281. * invalid. So if we want to remove the vmemmap range, we
  282. * need to make sure there is no subsection marked valid
  283. * in this range.
  284. */
  285. if (vmemmap_populated(start, page_size))
  286. continue;
  287. addr = vmemmap_list_free(start);
  288. if (!addr)
  289. continue;
  290. page = pfn_to_page(addr >> PAGE_SHIFT);
  291. nr_pages = 1 << page_order;
  292. base_pfn = PHYS_PFN(addr);
  293. if (base_pfn >= alt_start && base_pfn < alt_end) {
  294. vmem_altmap_free(altmap, nr_pages);
  295. } else if (PageReserved(page)) {
  296. /* allocated from bootmem */
  297. if (page_size < PAGE_SIZE) {
  298. /*
  299. * this shouldn't happen, but if it is
  300. * the case, leave the memory there
  301. */
  302. WARN_ON_ONCE(1);
  303. } else {
  304. while (nr_pages--)
  305. free_reserved_page(page++);
  306. }
  307. } else {
  308. free_pages((unsigned long)(__va(addr)), page_order);
  309. }
  310. vmemmap_remove_mapping(start, page_size);
  311. }
  312. }
  313. #endif
  314. void register_page_bootmem_memmap(unsigned long section_nr,
  315. struct page *start_page, unsigned long size)
  316. {
  317. }
  318. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  319. #ifdef CONFIG_PPC_BOOK3S_64
  320. unsigned int mmu_lpid_bits;
  321. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  322. EXPORT_SYMBOL_GPL(mmu_lpid_bits);
  323. #endif
  324. unsigned int mmu_pid_bits;
  325. static bool disable_radix = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
  326. static int __init parse_disable_radix(char *p)
  327. {
  328. bool val;
  329. if (!p)
  330. val = true;
  331. else if (kstrtobool(p, &val))
  332. return -EINVAL;
  333. disable_radix = val;
  334. return 0;
  335. }
  336. early_param("disable_radix", parse_disable_radix);
  337. /*
  338. * If we're running under a hypervisor, we need to check the contents of
  339. * /chosen/ibm,architecture-vec-5 to see if the hypervisor is willing to do
  340. * radix. If not, we clear the radix feature bit so we fall back to hash.
  341. */
  342. static void __init early_check_vec5(void)
  343. {
  344. unsigned long root, chosen;
  345. int size;
  346. const u8 *vec5;
  347. u8 mmu_supported;
  348. root = of_get_flat_dt_root();
  349. chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
  350. if (chosen == -FDT_ERR_NOTFOUND) {
  351. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  352. return;
  353. }
  354. vec5 = of_get_flat_dt_prop(chosen, "ibm,architecture-vec-5", &size);
  355. if (!vec5) {
  356. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  357. return;
  358. }
  359. if (size <= OV5_INDX(OV5_MMU_SUPPORT)) {
  360. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  361. return;
  362. }
  363. /* Check for supported configuration */
  364. mmu_supported = vec5[OV5_INDX(OV5_MMU_SUPPORT)] &
  365. OV5_FEAT(OV5_MMU_SUPPORT);
  366. if (mmu_supported == OV5_FEAT(OV5_MMU_RADIX)) {
  367. /* Hypervisor only supports radix - check enabled && GTSE */
  368. if (!early_radix_enabled()) {
  369. pr_warn("WARNING: Ignoring cmdline option disable_radix\n");
  370. }
  371. if (!(vec5[OV5_INDX(OV5_RADIX_GTSE)] &
  372. OV5_FEAT(OV5_RADIX_GTSE))) {
  373. cur_cpu_spec->mmu_features &= ~MMU_FTR_GTSE;
  374. } else
  375. cur_cpu_spec->mmu_features |= MMU_FTR_GTSE;
  376. /* Do radix anyway - the hypervisor said we had to */
  377. cur_cpu_spec->mmu_features |= MMU_FTR_TYPE_RADIX;
  378. } else if (mmu_supported == OV5_FEAT(OV5_MMU_HASH)) {
  379. /* Hypervisor only supports hash - disable radix */
  380. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  381. cur_cpu_spec->mmu_features &= ~MMU_FTR_GTSE;
  382. }
  383. }
  384. static int __init dt_scan_mmu_pid_width(unsigned long node,
  385. const char *uname, int depth,
  386. void *data)
  387. {
  388. int size = 0;
  389. const __be32 *prop;
  390. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  391. /* We are scanning "cpu" nodes only */
  392. if (type == NULL || strcmp(type, "cpu") != 0)
  393. return 0;
  394. /* Find MMU LPID, PID register size */
  395. prop = of_get_flat_dt_prop(node, "ibm,mmu-lpid-bits", &size);
  396. if (prop && size == 4)
  397. mmu_lpid_bits = be32_to_cpup(prop);
  398. prop = of_get_flat_dt_prop(node, "ibm,mmu-pid-bits", &size);
  399. if (prop && size == 4)
  400. mmu_pid_bits = be32_to_cpup(prop);
  401. if (!mmu_pid_bits && !mmu_lpid_bits)
  402. return 0;
  403. return 1;
  404. }
  405. void __init mmu_early_init_devtree(void)
  406. {
  407. bool hvmode = !!(mfmsr() & MSR_HV);
  408. /* Disable radix mode based on kernel command line. */
  409. if (disable_radix) {
  410. if (IS_ENABLED(CONFIG_PPC_64S_HASH_MMU))
  411. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  412. else
  413. pr_warn("WARNING: Ignoring cmdline option disable_radix\n");
  414. }
  415. of_scan_flat_dt(dt_scan_mmu_pid_width, NULL);
  416. if (hvmode && !mmu_lpid_bits) {
  417. if (early_cpu_has_feature(CPU_FTR_ARCH_207S))
  418. mmu_lpid_bits = 12; /* POWER8-10 */
  419. else
  420. mmu_lpid_bits = 10; /* POWER7 */
  421. }
  422. if (!mmu_pid_bits) {
  423. if (early_cpu_has_feature(CPU_FTR_ARCH_300))
  424. mmu_pid_bits = 20; /* POWER9-10 */
  425. }
  426. /*
  427. * Check /chosen/ibm,architecture-vec-5 if running as a guest.
  428. * When running bare-metal, we can use radix if we like
  429. * even though the ibm,architecture-vec-5 property created by
  430. * skiboot doesn't have the necessary bits set.
  431. */
  432. if (!hvmode)
  433. early_check_vec5();
  434. if (early_radix_enabled()) {
  435. radix__early_init_devtree();
  436. /*
  437. * We have finalized the translation we are going to use by now.
  438. * Radix mode is not limited by RMA / VRMA addressing.
  439. * Hence don't limit memblock allocations.
  440. */
  441. ppc64_rma_size = ULONG_MAX;
  442. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  443. } else
  444. hash__early_init_devtree();
  445. if (IS_ENABLED(CONFIG_HUGETLB_PAGE_SIZE_VARIABLE))
  446. hugetlbpage_init_defaultsize();
  447. if (!(cur_cpu_spec->mmu_features & MMU_FTR_HPTE_TABLE) &&
  448. !(cur_cpu_spec->mmu_features & MMU_FTR_TYPE_RADIX))
  449. panic("kernel does not support any MMU type offered by platform");
  450. }
  451. #endif /* CONFIG_PPC_BOOK3S_64 */