7 * Copyright (C) 1996 Paul Mackerras
9 * Derived from "arch/i386/mm/init.c"
10 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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.
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>
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>
54 #include <asm/mmu_context.h>
55 #include <asm/pgtable.h>
57 #include <linux/uaccess.h>
59 #include <asm/machdep.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>
71 phys_addr_t memstart_addr = ~0;
72 EXPORT_SYMBOL_GPL(memstart_addr);
73 phys_addr_t kernstart_addr;
74 EXPORT_SYMBOL_GPL(kernstart_addr);
76 #ifdef CONFIG_SPARSEMEM_VMEMMAP
78 * Given an address within the vmemmap, determine the pfn of the page that
79 * represents the start of the section it is within. Note that we have to
80 * do this by hand as the proffered address may not be correctly aligned.
81 * Subtraction of non-aligned pointers produces undefined results.
83 static unsigned long __meminit vmemmap_section_start(unsigned long page)
85 unsigned long offset = page - ((unsigned long)(vmemmap));
87 /* Return the pfn of the start of the section. */
88 return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
92 * Check if this vmemmap page is already initialised. If any section
93 * which overlaps this vmemmap page is initialised then this page is
94 * initialised already.
96 static int __meminit vmemmap_populated(unsigned long start, int page_size)
98 unsigned long end = start + page_size;
99 start = (unsigned long)(pfn_to_page(vmemmap_section_start(start)));
101 for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
102 if (pfn_valid(page_to_pfn((struct page *)start)))
109 * vmemmap virtual address space management does not have a traditonal page
110 * table to track which virtual struct pages are backed by physical mapping.
111 * The virtual to physical mappings are tracked in a simple linked list
112 * format. 'vmemmap_list' maintains the entire vmemmap physical mapping at
113 * all times where as the 'next' list maintains the available
114 * vmemmap_backing structures which have been deleted from the
115 * 'vmemmap_global' list during system runtime (memory hotplug remove
116 * operation). The freed 'vmemmap_backing' structures are reused later when
117 * new requests come in without allocating fresh memory. This pointer also
118 * tracks the allocated 'vmemmap_backing' structures as we allocate one
119 * full page memory at a time when we dont have any.
121 struct vmemmap_backing *vmemmap_list;
122 static struct vmemmap_backing *next;
125 * The same pointer 'next' tracks individual chunks inside the allocated
126 * full page during the boot time and again tracks the freeed nodes during
127 * runtime. It is racy but it does not happen as they are separated by the
128 * boot process. Will create problem if some how we have memory hotplug
129 * operation during boot !!
132 static int num_freed;
134 static __meminit struct vmemmap_backing * vmemmap_list_alloc(int node)
136 struct vmemmap_backing *vmem_back;
137 /* get from freed entries first */
146 /* allocate a page when required and hand out chunks */
148 next = vmemmap_alloc_block(PAGE_SIZE, node);
149 if (unlikely(!next)) {
153 num_left = PAGE_SIZE / sizeof(struct vmemmap_backing);
161 static __meminit void vmemmap_list_populate(unsigned long phys,
165 struct vmemmap_backing *vmem_back;
167 vmem_back = vmemmap_list_alloc(node);
168 if (unlikely(!vmem_back)) {
173 vmem_back->phys = phys;
174 vmem_back->virt_addr = start;
175 vmem_back->list = vmemmap_list;
177 vmemmap_list = vmem_back;
180 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
181 struct vmem_altmap *altmap)
183 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
185 /* Align to the page size of the linear mapping. */
186 start = _ALIGN_DOWN(start, page_size);
188 pr_debug("vmemmap_populate %lx..%lx, node %d\n", start, end, node);
190 for (; start < end; start += page_size) {
194 if (vmemmap_populated(start, page_size))
198 p = altmap_alloc_block_buf(page_size, altmap);
200 p = vmemmap_alloc_block_buf(page_size, node);
204 vmemmap_list_populate(__pa(p), start, node);
206 pr_debug(" * %016lx..%016lx allocated at %p\n",
207 start, start + page_size, p);
209 rc = vmemmap_create_mapping(start, page_size, __pa(p));
211 pr_warn("%s: Unable to create vmemmap mapping: %d\n",
220 #ifdef CONFIG_MEMORY_HOTPLUG
221 static unsigned long vmemmap_list_free(unsigned long start)
223 struct vmemmap_backing *vmem_back, *vmem_back_prev;
225 vmem_back_prev = vmem_back = vmemmap_list;
227 /* look for it with prev pointer recorded */
228 for (; vmem_back; vmem_back = vmem_back->list) {
229 if (vmem_back->virt_addr == start)
231 vmem_back_prev = vmem_back;
234 if (unlikely(!vmem_back)) {
239 /* remove it from vmemmap_list */
240 if (vmem_back == vmemmap_list) /* remove head */
241 vmemmap_list = vmem_back->list;
243 vmem_back_prev->list = vmem_back->list;
245 /* next point to this freed entry */
246 vmem_back->list = next;
250 return vmem_back->phys;
253 void __ref vmemmap_free(unsigned long start, unsigned long end,
254 struct vmem_altmap *altmap)
256 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
257 unsigned long page_order = get_order(page_size);
259 start = _ALIGN_DOWN(start, page_size);
261 pr_debug("vmemmap_free %lx...%lx\n", start, end);
263 for (; start < end; start += page_size) {
264 unsigned long nr_pages, addr;
265 struct page *section_base;
269 * the section has already be marked as invalid, so
270 * vmemmap_populated() true means some other sections still
271 * in this page, so skip it.
273 if (vmemmap_populated(start, page_size))
276 addr = vmemmap_list_free(start);
280 page = pfn_to_page(addr >> PAGE_SHIFT);
281 section_base = pfn_to_page(vmemmap_section_start(start));
282 nr_pages = 1 << page_order;
285 vmem_altmap_free(altmap, nr_pages);
286 } else if (PageReserved(page)) {
287 /* allocated from bootmem */
288 if (page_size < PAGE_SIZE) {
290 * this shouldn't happen, but if it is
291 * the case, leave the memory there
296 free_reserved_page(page++);
299 free_pages((unsigned long)(__va(addr)), page_order);
302 vmemmap_remove_mapping(start, page_size);
306 void register_page_bootmem_memmap(unsigned long section_nr,
307 struct page *start_page, unsigned long size)
312 * We do not have access to the sparsemem vmemmap, so we fallback to
313 * walking the list of sparsemem blocks which we already maintain for
314 * the sake of crashdump. In the long run, we might want to maintain
315 * a tree if performance of that linear walk becomes a problem.
317 * realmode_pfn_to_page functions can fail due to:
318 * 1) As real sparsemem blocks do not lay in RAM continously (they
319 * are in virtual address space which is not available in the real mode),
320 * the requested page struct can be split between blocks so get_page/put_page
322 * 2) When huge pages are used, the get_page/put_page API will fail
323 * in real mode as the linked addresses in the page struct are virtual
326 struct page *realmode_pfn_to_page(unsigned long pfn)
328 struct vmemmap_backing *vmem_back;
330 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
331 unsigned long pg_va = (unsigned long) pfn_to_page(pfn);
333 for (vmem_back = vmemmap_list; vmem_back; vmem_back = vmem_back->list) {
334 if (pg_va < vmem_back->virt_addr)
337 /* After vmemmap_list entry free is possible, need check all */
338 if ((pg_va + sizeof(struct page)) <=
339 (vmem_back->virt_addr + page_size)) {
340 page = (struct page *) (vmem_back->phys + pg_va -
341 vmem_back->virt_addr);
346 /* Probably that page struct is split between real pages */
349 EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
353 struct page *realmode_pfn_to_page(unsigned long pfn)
355 struct page *page = pfn_to_page(pfn);
358 EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
360 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
362 #ifdef CONFIG_PPC_BOOK3S_64
363 static bool disable_radix = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
365 static int __init parse_disable_radix(char *p)
371 else if (kstrtobool(p, &val))
378 early_param("disable_radix", parse_disable_radix);
381 * If we're running under a hypervisor, we need to check the contents of
382 * /chosen/ibm,architecture-vec-5 to see if the hypervisor is willing to do
383 * radix. If not, we clear the radix feature bit so we fall back to hash.
385 static void __init early_check_vec5(void)
387 unsigned long root, chosen;
392 root = of_get_flat_dt_root();
393 chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
394 if (chosen == -FDT_ERR_NOTFOUND) {
395 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
398 vec5 = of_get_flat_dt_prop(chosen, "ibm,architecture-vec-5", &size);
400 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
403 if (size <= OV5_INDX(OV5_MMU_SUPPORT)) {
404 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
408 /* Check for supported configuration */
409 mmu_supported = vec5[OV5_INDX(OV5_MMU_SUPPORT)] &
410 OV5_FEAT(OV5_MMU_SUPPORT);
411 if (mmu_supported == OV5_FEAT(OV5_MMU_RADIX)) {
412 /* Hypervisor only supports radix - check enabled && GTSE */
413 if (!early_radix_enabled()) {
414 pr_warn("WARNING: Ignoring cmdline option disable_radix\n");
416 if (!(vec5[OV5_INDX(OV5_RADIX_GTSE)] &
417 OV5_FEAT(OV5_RADIX_GTSE))) {
418 pr_warn("WARNING: Hypervisor doesn't support RADIX with GTSE\n");
420 /* Do radix anyway - the hypervisor said we had to */
421 cur_cpu_spec->mmu_features |= MMU_FTR_TYPE_RADIX;
422 } else if (mmu_supported == OV5_FEAT(OV5_MMU_HASH)) {
423 /* Hypervisor only supports hash - disable radix */
424 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
428 void __init mmu_early_init_devtree(void)
430 /* Disable radix mode based on kernel command line. */
432 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
435 * Check /chosen/ibm,architecture-vec-5 if running as a guest.
436 * When running bare-metal, we can use radix if we like
437 * even though the ibm,architecture-vec-5 property created by
438 * skiboot doesn't have the necessary bits set.
440 if (!(mfmsr() & MSR_HV))
443 if (early_radix_enabled())
444 radix__early_init_devtree();
446 hash__early_init_devtree();
448 #endif /* CONFIG_PPC_BOOK3S_64 */