Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / arch / powerpc / mm / init_64.c
blobfdb424a29f0358743ed2d03907065f75a9bc1e72
1 /*
2 * PowerPC version
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
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
9 * Derived from "arch/i386/mm/init.c"
10 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
12 * Dave Engebretsen <engebret@us.ibm.com>
13 * Rework for PPC64 port.
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.
22 #undef DEBUG
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/mman.h>
31 #include <linux/mm.h>
32 #include <linux/swap.h>
33 #include <linux/stddef.h>
34 #include <linux/vmalloc.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/highmem.h>
38 #include <linux/idr.h>
39 #include <linux/nodemask.h>
40 #include <linux/module.h>
41 #include <linux/poison.h>
42 #include <linux/memblock.h>
43 #include <linux/hugetlb.h>
44 #include <linux/slab.h>
45 #include <linux/of_fdt.h>
46 #include <linux/libfdt.h>
47 #include <linux/memremap.h>
49 #include <asm/pgalloc.h>
50 #include <asm/page.h>
51 #include <asm/prom.h>
52 #include <asm/rtas.h>
53 #include <asm/io.h>
54 #include <asm/mmu_context.h>
55 #include <asm/pgtable.h>
56 #include <asm/mmu.h>
57 #include <linux/uaccess.h>
58 #include <asm/smp.h>
59 #include <asm/machdep.h>
60 #include <asm/tlb.h>
61 #include <asm/eeh.h>
62 #include <asm/processor.h>
63 #include <asm/mmzone.h>
64 #include <asm/cputable.h>
65 #include <asm/sections.h>
66 #include <asm/iommu.h>
67 #include <asm/vdso.h>
69 #include "mmu_decl.h"
71 #ifdef CONFIG_PPC_BOOK3S_64
72 #if H_PGTABLE_RANGE > USER_VSID_RANGE
73 #warning Limited user VSID range means pagetable space is wasted
74 #endif
75 #endif /* CONFIG_PPC_BOOK3S_64 */
77 phys_addr_t memstart_addr = ~0;
78 EXPORT_SYMBOL_GPL(memstart_addr);
79 phys_addr_t kernstart_addr;
80 EXPORT_SYMBOL_GPL(kernstart_addr);
82 #ifdef CONFIG_SPARSEMEM_VMEMMAP
84 * Given an address within the vmemmap, determine the pfn of the page that
85 * represents the start of the section it is within. Note that we have to
86 * do this by hand as the proffered address may not be correctly aligned.
87 * Subtraction of non-aligned pointers produces undefined results.
89 static unsigned long __meminit vmemmap_section_start(unsigned long page)
91 unsigned long offset = page - ((unsigned long)(vmemmap));
93 /* Return the pfn of the start of the section. */
94 return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
98 * Check if this vmemmap page is already initialised. If any section
99 * which overlaps this vmemmap page is initialised then this page is
100 * initialised already.
102 static int __meminit vmemmap_populated(unsigned long start, int page_size)
104 unsigned long end = start + page_size;
105 start = (unsigned long)(pfn_to_page(vmemmap_section_start(start)));
107 for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
108 if (pfn_valid(page_to_pfn((struct page *)start)))
109 return 1;
111 return 0;
115 * vmemmap virtual address space management does not have a traditonal page
116 * table to track which virtual struct pages are backed by physical mapping.
117 * The virtual to physical mappings are tracked in a simple linked list
118 * format. 'vmemmap_list' maintains the entire vmemmap physical mapping at
119 * all times where as the 'next' list maintains the available
120 * vmemmap_backing structures which have been deleted from the
121 * 'vmemmap_global' list during system runtime (memory hotplug remove
122 * operation). The freed 'vmemmap_backing' structures are reused later when
123 * new requests come in without allocating fresh memory. This pointer also
124 * tracks the allocated 'vmemmap_backing' structures as we allocate one
125 * full page memory at a time when we dont have any.
127 struct vmemmap_backing *vmemmap_list;
128 static struct vmemmap_backing *next;
131 * The same pointer 'next' tracks individual chunks inside the allocated
132 * full page during the boot time and again tracks the freeed nodes during
133 * runtime. It is racy but it does not happen as they are separated by the
134 * boot process. Will create problem if some how we have memory hotplug
135 * operation during boot !!
137 static int num_left;
138 static int num_freed;
140 static __meminit struct vmemmap_backing * vmemmap_list_alloc(int node)
142 struct vmemmap_backing *vmem_back;
143 /* get from freed entries first */
144 if (num_freed) {
145 num_freed--;
146 vmem_back = next;
147 next = next->list;
149 return vmem_back;
152 /* allocate a page when required and hand out chunks */
153 if (!num_left) {
154 next = vmemmap_alloc_block(PAGE_SIZE, node);
155 if (unlikely(!next)) {
156 WARN_ON(1);
157 return NULL;
159 num_left = PAGE_SIZE / sizeof(struct vmemmap_backing);
162 num_left--;
164 return next++;
167 static __meminit void vmemmap_list_populate(unsigned long phys,
168 unsigned long start,
169 int node)
171 struct vmemmap_backing *vmem_back;
173 vmem_back = vmemmap_list_alloc(node);
174 if (unlikely(!vmem_back)) {
175 WARN_ON(1);
176 return;
179 vmem_back->phys = phys;
180 vmem_back->virt_addr = start;
181 vmem_back->list = vmemmap_list;
183 vmemmap_list = vmem_back;
186 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
187 struct vmem_altmap *altmap)
189 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
191 /* Align to the page size of the linear mapping. */
192 start = _ALIGN_DOWN(start, page_size);
194 pr_debug("vmemmap_populate %lx..%lx, node %d\n", start, end, node);
196 for (; start < end; start += page_size) {
197 void *p;
198 int rc;
200 if (vmemmap_populated(start, page_size))
201 continue;
203 if (altmap)
204 p = altmap_alloc_block_buf(page_size, altmap);
205 else
206 p = vmemmap_alloc_block_buf(page_size, node);
207 if (!p)
208 return -ENOMEM;
210 vmemmap_list_populate(__pa(p), start, node);
212 pr_debug(" * %016lx..%016lx allocated at %p\n",
213 start, start + page_size, p);
215 rc = vmemmap_create_mapping(start, page_size, __pa(p));
216 if (rc < 0) {
217 pr_warn("%s: Unable to create vmemmap mapping: %d\n",
218 __func__, rc);
219 return -EFAULT;
223 return 0;
226 #ifdef CONFIG_MEMORY_HOTPLUG
227 static unsigned long vmemmap_list_free(unsigned long start)
229 struct vmemmap_backing *vmem_back, *vmem_back_prev;
231 vmem_back_prev = vmem_back = vmemmap_list;
233 /* look for it with prev pointer recorded */
234 for (; vmem_back; vmem_back = vmem_back->list) {
235 if (vmem_back->virt_addr == start)
236 break;
237 vmem_back_prev = vmem_back;
240 if (unlikely(!vmem_back)) {
241 WARN_ON(1);
242 return 0;
245 /* remove it from vmemmap_list */
246 if (vmem_back == vmemmap_list) /* remove head */
247 vmemmap_list = vmem_back->list;
248 else
249 vmem_back_prev->list = vmem_back->list;
251 /* next point to this freed entry */
252 vmem_back->list = next;
253 next = vmem_back;
254 num_freed++;
256 return vmem_back->phys;
259 void __ref vmemmap_free(unsigned long start, unsigned long end,
260 struct vmem_altmap *altmap)
262 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
263 unsigned long page_order = get_order(page_size);
265 start = _ALIGN_DOWN(start, page_size);
267 pr_debug("vmemmap_free %lx...%lx\n", start, end);
269 for (; start < end; start += page_size) {
270 unsigned long nr_pages, addr;
271 struct page *section_base;
272 struct page *page;
275 * the section has already be marked as invalid, so
276 * vmemmap_populated() true means some other sections still
277 * in this page, so skip it.
279 if (vmemmap_populated(start, page_size))
280 continue;
282 addr = vmemmap_list_free(start);
283 if (!addr)
284 continue;
286 page = pfn_to_page(addr >> PAGE_SHIFT);
287 section_base = pfn_to_page(vmemmap_section_start(start));
288 nr_pages = 1 << page_order;
290 if (altmap) {
291 vmem_altmap_free(altmap, nr_pages);
292 } else if (PageReserved(page)) {
293 /* allocated from bootmem */
294 if (page_size < PAGE_SIZE) {
296 * this shouldn't happen, but if it is
297 * the case, leave the memory there
299 WARN_ON_ONCE(1);
300 } else {
301 while (nr_pages--)
302 free_reserved_page(page++);
304 } else {
305 free_pages((unsigned long)(__va(addr)), page_order);
308 vmemmap_remove_mapping(start, page_size);
311 #endif
312 void register_page_bootmem_memmap(unsigned long section_nr,
313 struct page *start_page, unsigned long size)
318 * We do not have access to the sparsemem vmemmap, so we fallback to
319 * walking the list of sparsemem blocks which we already maintain for
320 * the sake of crashdump. In the long run, we might want to maintain
321 * a tree if performance of that linear walk becomes a problem.
323 * realmode_pfn_to_page functions can fail due to:
324 * 1) As real sparsemem blocks do not lay in RAM continously (they
325 * are in virtual address space which is not available in the real mode),
326 * the requested page struct can be split between blocks so get_page/put_page
327 * may fail.
328 * 2) When huge pages are used, the get_page/put_page API will fail
329 * in real mode as the linked addresses in the page struct are virtual
330 * too.
332 struct page *realmode_pfn_to_page(unsigned long pfn)
334 struct vmemmap_backing *vmem_back;
335 struct page *page;
336 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
337 unsigned long pg_va = (unsigned long) pfn_to_page(pfn);
339 for (vmem_back = vmemmap_list; vmem_back; vmem_back = vmem_back->list) {
340 if (pg_va < vmem_back->virt_addr)
341 continue;
343 /* After vmemmap_list entry free is possible, need check all */
344 if ((pg_va + sizeof(struct page)) <=
345 (vmem_back->virt_addr + page_size)) {
346 page = (struct page *) (vmem_back->phys + pg_va -
347 vmem_back->virt_addr);
348 return page;
352 /* Probably that page struct is split between real pages */
353 return NULL;
355 EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
357 #else
359 struct page *realmode_pfn_to_page(unsigned long pfn)
361 struct page *page = pfn_to_page(pfn);
362 return page;
364 EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
366 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
368 #ifdef CONFIG_PPC_BOOK3S_64
369 static bool disable_radix = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
371 static int __init parse_disable_radix(char *p)
373 bool val;
375 if (strlen(p) == 0)
376 val = true;
377 else if (kstrtobool(p, &val))
378 return -EINVAL;
380 disable_radix = val;
382 return 0;
384 early_param("disable_radix", parse_disable_radix);
387 * If we're running under a hypervisor, we need to check the contents of
388 * /chosen/ibm,architecture-vec-5 to see if the hypervisor is willing to do
389 * radix. If not, we clear the radix feature bit so we fall back to hash.
391 static void __init early_check_vec5(void)
393 unsigned long root, chosen;
394 int size;
395 const u8 *vec5;
396 u8 mmu_supported;
398 root = of_get_flat_dt_root();
399 chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
400 if (chosen == -FDT_ERR_NOTFOUND) {
401 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
402 return;
404 vec5 = of_get_flat_dt_prop(chosen, "ibm,architecture-vec-5", &size);
405 if (!vec5) {
406 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
407 return;
409 if (size <= OV5_INDX(OV5_MMU_SUPPORT)) {
410 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
411 return;
414 /* Check for supported configuration */
415 mmu_supported = vec5[OV5_INDX(OV5_MMU_SUPPORT)] &
416 OV5_FEAT(OV5_MMU_SUPPORT);
417 if (mmu_supported == OV5_FEAT(OV5_MMU_RADIX)) {
418 /* Hypervisor only supports radix - check enabled && GTSE */
419 if (!early_radix_enabled()) {
420 pr_warn("WARNING: Ignoring cmdline option disable_radix\n");
422 if (!(vec5[OV5_INDX(OV5_RADIX_GTSE)] &
423 OV5_FEAT(OV5_RADIX_GTSE))) {
424 pr_warn("WARNING: Hypervisor doesn't support RADIX with GTSE\n");
426 /* Do radix anyway - the hypervisor said we had to */
427 cur_cpu_spec->mmu_features |= MMU_FTR_TYPE_RADIX;
428 } else if (mmu_supported == OV5_FEAT(OV5_MMU_HASH)) {
429 /* Hypervisor only supports hash - disable radix */
430 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
434 void __init mmu_early_init_devtree(void)
436 /* Disable radix mode based on kernel command line. */
437 if (disable_radix)
438 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
441 * Check /chosen/ibm,architecture-vec-5 if running as a guest.
442 * When running bare-metal, we can use radix if we like
443 * even though the ibm,architecture-vec-5 property created by
444 * skiboot doesn't have the necessary bits set.
446 if (!(mfmsr() & MSR_HV))
447 early_check_vec5();
449 if (early_radix_enabled())
450 radix__early_init_devtree();
451 else
452 hash__early_init_devtree();
454 #endif /* CONFIG_PPC_BOOK3S_64 */