init_64.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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;
  167. int rc;
  168. if (vmemmap_populated(start, page_size))
  169. continue;
  170. if (altmap)
  171. p = altmap_alloc_block_buf(page_size, altmap);
  172. else
  173. p = vmemmap_alloc_block_buf(page_size, node);
  174. if (!p)
  175. return -ENOMEM;
  176. vmemmap_list_populate(__pa(p), start, node);
  177. pr_debug(" * %016lx..%016lx allocated at %p\n",
  178. start, start + page_size, p);
  179. rc = vmemmap_create_mapping(start, page_size, __pa(p));
  180. if (rc < 0) {
  181. pr_warn("%s: Unable to create vmemmap mapping: %d\n",
  182. __func__, rc);
  183. return -EFAULT;
  184. }
  185. }
  186. return 0;
  187. }
  188. #ifdef CONFIG_MEMORY_HOTPLUG
  189. static unsigned long vmemmap_list_free(unsigned long start)
  190. {
  191. struct vmemmap_backing *vmem_back, *vmem_back_prev;
  192. vmem_back_prev = vmem_back = vmemmap_list;
  193. /* look for it with prev pointer recorded */
  194. for (; vmem_back; vmem_back = vmem_back->list) {
  195. if (vmem_back->virt_addr == start)
  196. break;
  197. vmem_back_prev = vmem_back;
  198. }
  199. if (unlikely(!vmem_back)) {
  200. WARN_ON(1);
  201. return 0;
  202. }
  203. /* remove it from vmemmap_list */
  204. if (vmem_back == vmemmap_list) /* remove head */
  205. vmemmap_list = vmem_back->list;
  206. else
  207. vmem_back_prev->list = vmem_back->list;
  208. /* next point to this freed entry */
  209. vmem_back->list = next;
  210. next = vmem_back;
  211. num_freed++;
  212. return vmem_back->phys;
  213. }
  214. void __ref vmemmap_free(unsigned long start, unsigned long end,
  215. struct vmem_altmap *altmap)
  216. {
  217. unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
  218. unsigned long page_order = get_order(page_size);
  219. start = _ALIGN_DOWN(start, page_size);
  220. pr_debug("vmemmap_free %lx...%lx\n", start, end);
  221. for (; start < end; start += page_size) {
  222. unsigned long nr_pages, addr;
  223. struct page *section_base;
  224. struct page *page;
  225. /*
  226. * the section has already be marked as invalid, so
  227. * vmemmap_populated() true means some other sections still
  228. * in this page, so skip it.
  229. */
  230. if (vmemmap_populated(start, page_size))
  231. continue;
  232. addr = vmemmap_list_free(start);
  233. if (!addr)
  234. continue;
  235. page = pfn_to_page(addr >> PAGE_SHIFT);
  236. section_base = pfn_to_page(vmemmap_section_start(start));
  237. nr_pages = 1 << page_order;
  238. if (altmap) {
  239. vmem_altmap_free(altmap, nr_pages);
  240. } else if (PageReserved(page)) {
  241. /* allocated from bootmem */
  242. if (page_size < PAGE_SIZE) {
  243. /*
  244. * this shouldn't happen, but if it is
  245. * the case, leave the memory there
  246. */
  247. WARN_ON_ONCE(1);
  248. } else {
  249. while (nr_pages--)
  250. free_reserved_page(page++);
  251. }
  252. } else {
  253. free_pages((unsigned long)(__va(addr)), page_order);
  254. }
  255. vmemmap_remove_mapping(start, page_size);
  256. }
  257. }
  258. #endif
  259. void register_page_bootmem_memmap(unsigned long section_nr,
  260. struct page *start_page, unsigned long size)
  261. {
  262. }
  263. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  264. #ifdef CONFIG_PPC_BOOK3S_64
  265. static bool disable_radix = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
  266. static int __init parse_disable_radix(char *p)
  267. {
  268. bool val;
  269. if (!p)
  270. val = true;
  271. else if (kstrtobool(p, &val))
  272. return -EINVAL;
  273. disable_radix = val;
  274. return 0;
  275. }
  276. early_param("disable_radix", parse_disable_radix);
  277. /*
  278. * If we're running under a hypervisor, we need to check the contents of
  279. * /chosen/ibm,architecture-vec-5 to see if the hypervisor is willing to do
  280. * radix. If not, we clear the radix feature bit so we fall back to hash.
  281. */
  282. static void __init early_check_vec5(void)
  283. {
  284. unsigned long root, chosen;
  285. int size;
  286. const u8 *vec5;
  287. u8 mmu_supported;
  288. root = of_get_flat_dt_root();
  289. chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
  290. if (chosen == -FDT_ERR_NOTFOUND) {
  291. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  292. return;
  293. }
  294. vec5 = of_get_flat_dt_prop(chosen, "ibm,architecture-vec-5", &size);
  295. if (!vec5) {
  296. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  297. return;
  298. }
  299. if (size <= OV5_INDX(OV5_MMU_SUPPORT)) {
  300. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  301. return;
  302. }
  303. /* Check for supported configuration */
  304. mmu_supported = vec5[OV5_INDX(OV5_MMU_SUPPORT)] &
  305. OV5_FEAT(OV5_MMU_SUPPORT);
  306. if (mmu_supported == OV5_FEAT(OV5_MMU_RADIX)) {
  307. /* Hypervisor only supports radix - check enabled && GTSE */
  308. if (!early_radix_enabled()) {
  309. pr_warn("WARNING: Ignoring cmdline option disable_radix\n");
  310. }
  311. if (!(vec5[OV5_INDX(OV5_RADIX_GTSE)] &
  312. OV5_FEAT(OV5_RADIX_GTSE))) {
  313. pr_warn("WARNING: Hypervisor doesn't support RADIX with GTSE\n");
  314. }
  315. /* Do radix anyway - the hypervisor said we had to */
  316. cur_cpu_spec->mmu_features |= MMU_FTR_TYPE_RADIX;
  317. } else if (mmu_supported == OV5_FEAT(OV5_MMU_HASH)) {
  318. /* Hypervisor only supports hash - disable radix */
  319. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  320. }
  321. }
  322. void __init mmu_early_init_devtree(void)
  323. {
  324. /* Disable radix mode based on kernel command line. */
  325. if (disable_radix)
  326. cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
  327. /*
  328. * Check /chosen/ibm,architecture-vec-5 if running as a guest.
  329. * When running bare-metal, we can use radix if we like
  330. * even though the ibm,architecture-vec-5 property created by
  331. * skiboot doesn't have the necessary bits set.
  332. */
  333. if (!(mfmsr() & MSR_HV))
  334. early_check_vec5();
  335. if (early_radix_enabled())
  336. radix__early_init_devtree();
  337. else
  338. hash__early_init_devtree();
  339. }
  340. #endif /* CONFIG_PPC_BOOK3S_64 */