xref: /linux/arch/powerpc/mm/init_64.c (revision f940f5289873af2ad2c4e73f88c24ad2b8fe3f87)
170d64ceaSPaul Mackerras /*
270d64ceaSPaul Mackerras  *  PowerPC version
370d64ceaSPaul Mackerras  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
470d64ceaSPaul Mackerras  *
570d64ceaSPaul Mackerras  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
670d64ceaSPaul Mackerras  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
770d64ceaSPaul Mackerras  *    Copyright (C) 1996 Paul Mackerras
870d64ceaSPaul Mackerras  *
970d64ceaSPaul Mackerras  *  Derived from "arch/i386/mm/init.c"
1070d64ceaSPaul Mackerras  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
1170d64ceaSPaul Mackerras  *
1270d64ceaSPaul Mackerras  *  Dave Engebretsen <engebret@us.ibm.com>
1370d64ceaSPaul Mackerras  *      Rework for PPC64 port.
1470d64ceaSPaul Mackerras  *
1570d64ceaSPaul Mackerras  *  This program is free software; you can redistribute it and/or
1670d64ceaSPaul Mackerras  *  modify it under the terms of the GNU General Public License
1770d64ceaSPaul Mackerras  *  as published by the Free Software Foundation; either version
1870d64ceaSPaul Mackerras  *  2 of the License, or (at your option) any later version.
1970d64ceaSPaul Mackerras  *
2070d64ceaSPaul Mackerras  */
2170d64ceaSPaul Mackerras 
22cec08e7aSBenjamin Herrenschmidt #undef DEBUG
23cec08e7aSBenjamin Herrenschmidt 
2470d64ceaSPaul Mackerras #include <linux/signal.h>
2570d64ceaSPaul Mackerras #include <linux/sched.h>
2670d64ceaSPaul Mackerras #include <linux/kernel.h>
2770d64ceaSPaul Mackerras #include <linux/errno.h>
2870d64ceaSPaul Mackerras #include <linux/string.h>
2970d64ceaSPaul Mackerras #include <linux/types.h>
3070d64ceaSPaul Mackerras #include <linux/mman.h>
3170d64ceaSPaul Mackerras #include <linux/mm.h>
3270d64ceaSPaul Mackerras #include <linux/swap.h>
3370d64ceaSPaul Mackerras #include <linux/stddef.h>
3470d64ceaSPaul Mackerras #include <linux/vmalloc.h>
3570d64ceaSPaul Mackerras #include <linux/init.h>
3670d64ceaSPaul Mackerras #include <linux/delay.h>
3770d64ceaSPaul Mackerras #include <linux/bootmem.h>
3870d64ceaSPaul Mackerras #include <linux/highmem.h>
3970d64ceaSPaul Mackerras #include <linux/idr.h>
4070d64ceaSPaul Mackerras #include <linux/nodemask.h>
4170d64ceaSPaul Mackerras #include <linux/module.h>
42c9cf5528SRandy Dunlap #include <linux/poison.h>
4395f72d1eSYinghai Lu #include <linux/memblock.h>
44a4fe3ce7SDavid Gibson #include <linux/hugetlb.h>
455a0e3ad6STejun Heo #include <linux/slab.h>
4670d64ceaSPaul Mackerras 
4770d64ceaSPaul Mackerras #include <asm/pgalloc.h>
4870d64ceaSPaul Mackerras #include <asm/page.h>
4970d64ceaSPaul Mackerras #include <asm/prom.h>
5070d64ceaSPaul Mackerras #include <asm/rtas.h>
5170d64ceaSPaul Mackerras #include <asm/io.h>
5270d64ceaSPaul Mackerras #include <asm/mmu_context.h>
5370d64ceaSPaul Mackerras #include <asm/pgtable.h>
5470d64ceaSPaul Mackerras #include <asm/mmu.h>
5570d64ceaSPaul Mackerras #include <asm/uaccess.h>
5670d64ceaSPaul Mackerras #include <asm/smp.h>
5770d64ceaSPaul Mackerras #include <asm/machdep.h>
5870d64ceaSPaul Mackerras #include <asm/tlb.h>
5970d64ceaSPaul Mackerras #include <asm/eeh.h>
6070d64ceaSPaul Mackerras #include <asm/processor.h>
6170d64ceaSPaul Mackerras #include <asm/mmzone.h>
6270d64ceaSPaul Mackerras #include <asm/cputable.h>
6370d64ceaSPaul Mackerras #include <asm/sections.h>
6470d64ceaSPaul Mackerras #include <asm/iommu.h>
6570d64ceaSPaul Mackerras #include <asm/vdso.h>
66800fc3eeSDavid Gibson 
67800fc3eeSDavid Gibson #include "mmu_decl.h"
6870d64ceaSPaul Mackerras 
6994491685SBenjamin Herrenschmidt #ifdef CONFIG_PPC_STD_MMU_64
7070d64ceaSPaul Mackerras #if PGTABLE_RANGE > USER_VSID_RANGE
7170d64ceaSPaul Mackerras #warning Limited user VSID range means pagetable space is wasted
7270d64ceaSPaul Mackerras #endif
7370d64ceaSPaul Mackerras 
7470d64ceaSPaul Mackerras #if (TASK_SIZE_USER64 < PGTABLE_RANGE) && (TASK_SIZE_USER64 < USER_VSID_RANGE)
7570d64ceaSPaul Mackerras #warning TASK_SIZE is smaller than it needs to be.
7670d64ceaSPaul Mackerras #endif
7794491685SBenjamin Herrenschmidt #endif /* CONFIG_PPC_STD_MMU_64 */
7870d64ceaSPaul Mackerras 
7937dd2badSKumar Gala phys_addr_t memstart_addr = ~0;
8079c3095fSSonny Rao EXPORT_SYMBOL_GPL(memstart_addr);
8137dd2badSKumar Gala phys_addr_t kernstart_addr;
8279c3095fSSonny Rao EXPORT_SYMBOL_GPL(kernstart_addr);
83d7917ba7SKumar Gala 
8451cc5068SAlexey Dobriyan static void pgd_ctor(void *addr)
8570d64ceaSPaul Mackerras {
8651cc5068SAlexey Dobriyan 	memset(addr, 0, PGD_TABLE_SIZE);
8751cc5068SAlexey Dobriyan }
8851cc5068SAlexey Dobriyan 
8951cc5068SAlexey Dobriyan static void pmd_ctor(void *addr)
9051cc5068SAlexey Dobriyan {
91*f940f528SAneesh Kumar K.V #ifdef CONFIG_TRANSPARENT_HUGEPAGE
92*f940f528SAneesh Kumar K.V 	memset(addr, 0, PMD_TABLE_SIZE * 2);
93*f940f528SAneesh Kumar K.V #else
9451cc5068SAlexey Dobriyan 	memset(addr, 0, PMD_TABLE_SIZE);
95*f940f528SAneesh Kumar K.V #endif
9670d64ceaSPaul Mackerras }
9770d64ceaSPaul Mackerras 
98a0668cdcSDavid Gibson struct kmem_cache *pgtable_cache[MAX_PGTABLE_INDEX_SIZE];
9970d64ceaSPaul Mackerras 
100a0668cdcSDavid Gibson /*
101a0668cdcSDavid Gibson  * Create a kmem_cache() for pagetables.  This is not used for PTE
102a0668cdcSDavid Gibson  * pages - they're linked to struct page, come from the normal free
103a0668cdcSDavid Gibson  * pages pool and have a different entry size (see real_pte_t) to
104a0668cdcSDavid Gibson  * everything else.  Caches created by this function are used for all
105a0668cdcSDavid Gibson  * the higher level pagetables, and for hugepage pagetables.
106a0668cdcSDavid Gibson  */
107a0668cdcSDavid Gibson void pgtable_cache_add(unsigned shift, void (*ctor)(void *))
108a0668cdcSDavid Gibson {
109a0668cdcSDavid Gibson 	char *name;
110a0668cdcSDavid Gibson 	unsigned long table_size = sizeof(void *) << shift;
111a0668cdcSDavid Gibson 	unsigned long align = table_size;
112a0668cdcSDavid Gibson 
113a0668cdcSDavid Gibson 	/* When batching pgtable pointers for RCU freeing, we store
114a0668cdcSDavid Gibson 	 * the index size in the low bits.  Table alignment must be
115a4fe3ce7SDavid Gibson 	 * big enough to fit it.
116a4fe3ce7SDavid Gibson 	 *
117a4fe3ce7SDavid Gibson 	 * Likewise, hugeapge pagetable pointers contain a (different)
118a4fe3ce7SDavid Gibson 	 * shift value in the low bits.  All tables must be aligned so
119a4fe3ce7SDavid Gibson 	 * as to leave enough 0 bits in the address to contain it. */
120a4fe3ce7SDavid Gibson 	unsigned long minalign = max(MAX_PGTABLE_INDEX_SIZE + 1,
121a4fe3ce7SDavid Gibson 				     HUGEPD_SHIFT_MASK + 1);
122a0668cdcSDavid Gibson 	struct kmem_cache *new;
123a0668cdcSDavid Gibson 
124a0668cdcSDavid Gibson 	/* It would be nice if this was a BUILD_BUG_ON(), but at the
125a0668cdcSDavid Gibson 	 * moment, gcc doesn't seem to recognize is_power_of_2 as a
126a0668cdcSDavid Gibson 	 * constant expression, so so much for that. */
127a0668cdcSDavid Gibson 	BUG_ON(!is_power_of_2(minalign));
128a0668cdcSDavid Gibson 	BUG_ON((shift < 1) || (shift > MAX_PGTABLE_INDEX_SIZE));
129a0668cdcSDavid Gibson 
130a0668cdcSDavid Gibson 	if (PGT_CACHE(shift))
131a0668cdcSDavid Gibson 		return; /* Already have a cache of this size */
132a0668cdcSDavid Gibson 
133a0668cdcSDavid Gibson 	align = max_t(unsigned long, align, minalign);
134a0668cdcSDavid Gibson 	name = kasprintf(GFP_KERNEL, "pgtable-2^%d", shift);
135a0668cdcSDavid Gibson 	new = kmem_cache_create(name, table_size, align, 0, ctor);
136cf9427b8SAneesh Kumar K.V 	pgtable_cache[shift - 1] = new;
137a0668cdcSDavid Gibson 	pr_debug("Allocated pgtable cache for order %d\n", shift);
138a0668cdcSDavid Gibson }
139a0668cdcSDavid Gibson 
14070d64ceaSPaul Mackerras 
14170d64ceaSPaul Mackerras void pgtable_cache_init(void)
14270d64ceaSPaul Mackerras {
143a0668cdcSDavid Gibson 	pgtable_cache_add(PGD_INDEX_SIZE, pgd_ctor);
144*f940f528SAneesh Kumar K.V 	pgtable_cache_add(PMD_CACHE_INDEX, pmd_ctor);
145*f940f528SAneesh Kumar K.V 	if (!PGT_CACHE(PGD_INDEX_SIZE) || !PGT_CACHE(PMD_CACHE_INDEX))
146a0668cdcSDavid Gibson 		panic("Couldn't allocate pgtable caches");
147a0668cdcSDavid Gibson 	/* In all current configs, when the PUD index exists it's the
148a0668cdcSDavid Gibson 	 * same size as either the pgd or pmd index.  Verify that the
149a0668cdcSDavid Gibson 	 * initialization above has also created a PUD cache.  This
150a0668cdcSDavid Gibson 	 * will need re-examiniation if we add new possibilities for
151a0668cdcSDavid Gibson 	 * the pagetable layout. */
152a0668cdcSDavid Gibson 	BUG_ON(PUD_INDEX_SIZE && !PGT_CACHE(PUD_INDEX_SIZE));
15370d64ceaSPaul Mackerras }
154d29eff7bSAndy Whitcroft 
155d29eff7bSAndy Whitcroft #ifdef CONFIG_SPARSEMEM_VMEMMAP
156d29eff7bSAndy Whitcroft /*
157d29eff7bSAndy Whitcroft  * Given an address within the vmemmap, determine the pfn of the page that
158d29eff7bSAndy Whitcroft  * represents the start of the section it is within.  Note that we have to
159d29eff7bSAndy Whitcroft  * do this by hand as the proffered address may not be correctly aligned.
160d29eff7bSAndy Whitcroft  * Subtraction of non-aligned pointers produces undefined results.
161d29eff7bSAndy Whitcroft  */
16209de9ff8SMichael Ellerman static unsigned long __meminit vmemmap_section_start(unsigned long page)
163d29eff7bSAndy Whitcroft {
164d29eff7bSAndy Whitcroft 	unsigned long offset = page - ((unsigned long)(vmemmap));
165d29eff7bSAndy Whitcroft 
166d29eff7bSAndy Whitcroft 	/* Return the pfn of the start of the section. */
167d29eff7bSAndy Whitcroft 	return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
168d29eff7bSAndy Whitcroft }
169d29eff7bSAndy Whitcroft 
170d29eff7bSAndy Whitcroft /*
171d29eff7bSAndy Whitcroft  * Check if this vmemmap page is already initialised.  If any section
172d29eff7bSAndy Whitcroft  * which overlaps this vmemmap page is initialised then this page is
173d29eff7bSAndy Whitcroft  * initialised already.
174d29eff7bSAndy Whitcroft  */
17509de9ff8SMichael Ellerman static int __meminit vmemmap_populated(unsigned long start, int page_size)
176d29eff7bSAndy Whitcroft {
177d29eff7bSAndy Whitcroft 	unsigned long end = start + page_size;
178d29eff7bSAndy Whitcroft 
179d29eff7bSAndy Whitcroft 	for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
180d29eff7bSAndy Whitcroft 		if (pfn_valid(vmemmap_section_start(start)))
181d29eff7bSAndy Whitcroft 			return 1;
182d29eff7bSAndy Whitcroft 
183d29eff7bSAndy Whitcroft 	return 0;
184d29eff7bSAndy Whitcroft }
185d29eff7bSAndy Whitcroft 
18632a74949SBenjamin Herrenschmidt /* On hash-based CPUs, the vmemmap is bolted in the hash table.
18732a74949SBenjamin Herrenschmidt  *
18832a74949SBenjamin Herrenschmidt  * On Book3E CPUs, the vmemmap is currently mapped in the top half of
18932a74949SBenjamin Herrenschmidt  * the vmalloc space using normal page tables, though the size of
19032a74949SBenjamin Herrenschmidt  * pages encoded in the PTEs can be different
19132a74949SBenjamin Herrenschmidt  */
19232a74949SBenjamin Herrenschmidt 
19332a74949SBenjamin Herrenschmidt #ifdef CONFIG_PPC_BOOK3E
19432a74949SBenjamin Herrenschmidt static void __meminit vmemmap_create_mapping(unsigned long start,
19532a74949SBenjamin Herrenschmidt 					     unsigned long page_size,
19632a74949SBenjamin Herrenschmidt 					     unsigned long phys)
19732a74949SBenjamin Herrenschmidt {
19832a74949SBenjamin Herrenschmidt 	/* Create a PTE encoding without page size */
19932a74949SBenjamin Herrenschmidt 	unsigned long i, flags = _PAGE_PRESENT | _PAGE_ACCESSED |
20032a74949SBenjamin Herrenschmidt 		_PAGE_KERNEL_RW;
20132a74949SBenjamin Herrenschmidt 
20232a74949SBenjamin Herrenschmidt 	/* PTEs only contain page size encodings up to 32M */
20332a74949SBenjamin Herrenschmidt 	BUG_ON(mmu_psize_defs[mmu_vmemmap_psize].enc > 0xf);
20432a74949SBenjamin Herrenschmidt 
20532a74949SBenjamin Herrenschmidt 	/* Encode the size in the PTE */
20632a74949SBenjamin Herrenschmidt 	flags |= mmu_psize_defs[mmu_vmemmap_psize].enc << 8;
20732a74949SBenjamin Herrenschmidt 
20832a74949SBenjamin Herrenschmidt 	/* For each PTE for that area, map things. Note that we don't
20932a74949SBenjamin Herrenschmidt 	 * increment phys because all PTEs are of the large size and
21032a74949SBenjamin Herrenschmidt 	 * thus must have the low bits clear
21132a74949SBenjamin Herrenschmidt 	 */
21232a74949SBenjamin Herrenschmidt 	for (i = 0; i < page_size; i += PAGE_SIZE)
21332a74949SBenjamin Herrenschmidt 		BUG_ON(map_kernel_page(start + i, phys, flags));
21432a74949SBenjamin Herrenschmidt }
21532a74949SBenjamin Herrenschmidt #else /* CONFIG_PPC_BOOK3E */
21632a74949SBenjamin Herrenschmidt static void __meminit vmemmap_create_mapping(unsigned long start,
21732a74949SBenjamin Herrenschmidt 					     unsigned long page_size,
21832a74949SBenjamin Herrenschmidt 					     unsigned long phys)
21932a74949SBenjamin Herrenschmidt {
22032a74949SBenjamin Herrenschmidt 	int  mapped = htab_bolt_mapping(start, start + page_size, phys,
22183d5e64bSAneesh Kumar K.V 					pgprot_val(PAGE_KERNEL),
22283d5e64bSAneesh Kumar K.V 					mmu_vmemmap_psize,
22332a74949SBenjamin Herrenschmidt 					mmu_kernel_ssize);
22432a74949SBenjamin Herrenschmidt 	BUG_ON(mapped < 0);
22532a74949SBenjamin Herrenschmidt }
22632a74949SBenjamin Herrenschmidt #endif /* CONFIG_PPC_BOOK3E */
22732a74949SBenjamin Herrenschmidt 
22891eea67cSMark Nelson struct vmemmap_backing *vmemmap_list;
22991eea67cSMark Nelson 
23091eea67cSMark Nelson static __meminit struct vmemmap_backing * vmemmap_list_alloc(int node)
23191eea67cSMark Nelson {
23291eea67cSMark Nelson 	static struct vmemmap_backing *next;
23391eea67cSMark Nelson 	static int num_left;
23491eea67cSMark Nelson 
23591eea67cSMark Nelson 	/* allocate a page when required and hand out chunks */
23691eea67cSMark Nelson 	if (!next || !num_left) {
23791eea67cSMark Nelson 		next = vmemmap_alloc_block(PAGE_SIZE, node);
23891eea67cSMark Nelson 		if (unlikely(!next)) {
23991eea67cSMark Nelson 			WARN_ON(1);
24091eea67cSMark Nelson 			return NULL;
24191eea67cSMark Nelson 		}
24291eea67cSMark Nelson 		num_left = PAGE_SIZE / sizeof(struct vmemmap_backing);
24391eea67cSMark Nelson 	}
24491eea67cSMark Nelson 
24591eea67cSMark Nelson 	num_left--;
24691eea67cSMark Nelson 
24791eea67cSMark Nelson 	return next++;
24891eea67cSMark Nelson }
24991eea67cSMark Nelson 
25091eea67cSMark Nelson static __meminit void vmemmap_list_populate(unsigned long phys,
25191eea67cSMark Nelson 					    unsigned long start,
25291eea67cSMark Nelson 					    int node)
25391eea67cSMark Nelson {
25491eea67cSMark Nelson 	struct vmemmap_backing *vmem_back;
25591eea67cSMark Nelson 
25691eea67cSMark Nelson 	vmem_back = vmemmap_list_alloc(node);
25791eea67cSMark Nelson 	if (unlikely(!vmem_back)) {
25891eea67cSMark Nelson 		WARN_ON(1);
25991eea67cSMark Nelson 		return;
26091eea67cSMark Nelson 	}
26191eea67cSMark Nelson 
26291eea67cSMark Nelson 	vmem_back->phys = phys;
26391eea67cSMark Nelson 	vmem_back->virt_addr = start;
26491eea67cSMark Nelson 	vmem_back->list = vmemmap_list;
26591eea67cSMark Nelson 
26691eea67cSMark Nelson 	vmemmap_list = vmem_back;
26791eea67cSMark Nelson }
26891eea67cSMark Nelson 
2690aad818bSJohannes Weiner int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
270d29eff7bSAndy Whitcroft {
271cec08e7aSBenjamin Herrenschmidt 	unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
272d29eff7bSAndy Whitcroft 
273d29eff7bSAndy Whitcroft 	/* Align to the page size of the linear mapping. */
274d29eff7bSAndy Whitcroft 	start = _ALIGN_DOWN(start, page_size);
275d29eff7bSAndy Whitcroft 
2760aad818bSJohannes Weiner 	pr_debug("vmemmap_populate %lx..%lx, node %d\n", start, end, node);
27732a74949SBenjamin Herrenschmidt 
278d29eff7bSAndy Whitcroft 	for (; start < end; start += page_size) {
279d29eff7bSAndy Whitcroft 		void *p;
280d29eff7bSAndy Whitcroft 
281d29eff7bSAndy Whitcroft 		if (vmemmap_populated(start, page_size))
282d29eff7bSAndy Whitcroft 			continue;
283d29eff7bSAndy Whitcroft 
284d29eff7bSAndy Whitcroft 		p = vmemmap_alloc_block(page_size, node);
285d29eff7bSAndy Whitcroft 		if (!p)
286d29eff7bSAndy Whitcroft 			return -ENOMEM;
287d29eff7bSAndy Whitcroft 
28891eea67cSMark Nelson 		vmemmap_list_populate(__pa(p), start, node);
28991eea67cSMark Nelson 
29032a74949SBenjamin Herrenschmidt 		pr_debug("      * %016lx..%016lx allocated at %p\n",
29132a74949SBenjamin Herrenschmidt 			 start, start + page_size, p);
292d29eff7bSAndy Whitcroft 
29332a74949SBenjamin Herrenschmidt 		vmemmap_create_mapping(start, page_size, __pa(p));
294d29eff7bSAndy Whitcroft 	}
295d29eff7bSAndy Whitcroft 
296d29eff7bSAndy Whitcroft 	return 0;
297d29eff7bSAndy Whitcroft }
29846723bfaSYasuaki Ishimatsu 
2990aad818bSJohannes Weiner void vmemmap_free(unsigned long start, unsigned long end)
3000197518cSTang Chen {
3010197518cSTang Chen }
3020197518cSTang Chen 
303cec08e7aSBenjamin Herrenschmidt #endif /* CONFIG_SPARSEMEM_VMEMMAP */
304cd3db0c4SBenjamin Herrenschmidt 
305