xref: /linux/arch/powerpc/mm/init_64.c (revision 51cc50685a4275c6a02653670af9f108a64e01cf)
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>
43d9b2b2a2SDavid S. Miller #include <linux/lmb.h>
4470d64ceaSPaul Mackerras 
4570d64ceaSPaul Mackerras #include <asm/pgalloc.h>
4670d64ceaSPaul Mackerras #include <asm/page.h>
4770d64ceaSPaul Mackerras #include <asm/prom.h>
4870d64ceaSPaul Mackerras #include <asm/rtas.h>
4970d64ceaSPaul Mackerras #include <asm/io.h>
5070d64ceaSPaul Mackerras #include <asm/mmu_context.h>
5170d64ceaSPaul Mackerras #include <asm/pgtable.h>
5270d64ceaSPaul Mackerras #include <asm/mmu.h>
5370d64ceaSPaul Mackerras #include <asm/uaccess.h>
5470d64ceaSPaul Mackerras #include <asm/smp.h>
5570d64ceaSPaul Mackerras #include <asm/machdep.h>
5670d64ceaSPaul Mackerras #include <asm/tlb.h>
5770d64ceaSPaul Mackerras #include <asm/eeh.h>
5870d64ceaSPaul Mackerras #include <asm/processor.h>
5970d64ceaSPaul Mackerras #include <asm/mmzone.h>
6070d64ceaSPaul Mackerras #include <asm/cputable.h>
6170d64ceaSPaul Mackerras #include <asm/sections.h>
6270d64ceaSPaul Mackerras #include <asm/system.h>
6370d64ceaSPaul Mackerras #include <asm/iommu.h>
6470d64ceaSPaul Mackerras #include <asm/abs_addr.h>
6570d64ceaSPaul Mackerras #include <asm/vdso.h>
66800fc3eeSDavid Gibson 
67800fc3eeSDavid Gibson #include "mmu_decl.h"
6870d64ceaSPaul Mackerras 
6970d64ceaSPaul Mackerras #if PGTABLE_RANGE > USER_VSID_RANGE
7070d64ceaSPaul Mackerras #warning Limited user VSID range means pagetable space is wasted
7170d64ceaSPaul Mackerras #endif
7270d64ceaSPaul Mackerras 
7370d64ceaSPaul Mackerras #if (TASK_SIZE_USER64 < PGTABLE_RANGE) && (TASK_SIZE_USER64 < USER_VSID_RANGE)
7470d64ceaSPaul Mackerras #warning TASK_SIZE is smaller than it needs to be.
7570d64ceaSPaul Mackerras #endif
7670d64ceaSPaul Mackerras 
7737dd2badSKumar Gala phys_addr_t memstart_addr = ~0;
7837dd2badSKumar Gala phys_addr_t kernstart_addr;
79d7917ba7SKumar Gala 
8070d64ceaSPaul Mackerras void free_initmem(void)
8170d64ceaSPaul Mackerras {
8270d64ceaSPaul Mackerras 	unsigned long addr;
8370d64ceaSPaul Mackerras 
8470d64ceaSPaul Mackerras 	addr = (unsigned long)__init_begin;
8570d64ceaSPaul Mackerras 	for (; addr < (unsigned long)__init_end; addr += PAGE_SIZE) {
86c9cf5528SRandy Dunlap 		memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
8770d64ceaSPaul Mackerras 		ClearPageReserved(virt_to_page(addr));
887835e98bSNick Piggin 		init_page_count(virt_to_page(addr));
8970d64ceaSPaul Mackerras 		free_page(addr);
9070d64ceaSPaul Mackerras 		totalram_pages++;
9170d64ceaSPaul Mackerras 	}
9270d64ceaSPaul Mackerras 	printk ("Freeing unused kernel memory: %luk freed\n",
9370d64ceaSPaul Mackerras 		((unsigned long)__init_end - (unsigned long)__init_begin) >> 10);
9470d64ceaSPaul Mackerras }
9570d64ceaSPaul Mackerras 
9670d64ceaSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
9770d64ceaSPaul Mackerras void free_initrd_mem(unsigned long start, unsigned long end)
9870d64ceaSPaul Mackerras {
9970d64ceaSPaul Mackerras 	if (start < end)
10070d64ceaSPaul Mackerras 		printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
10170d64ceaSPaul Mackerras 	for (; start < end; start += PAGE_SIZE) {
10270d64ceaSPaul Mackerras 		ClearPageReserved(virt_to_page(start));
1037835e98bSNick Piggin 		init_page_count(virt_to_page(start));
10470d64ceaSPaul Mackerras 		free_page(start);
10570d64ceaSPaul Mackerras 		totalram_pages++;
10670d64ceaSPaul Mackerras 	}
10770d64ceaSPaul Mackerras }
10870d64ceaSPaul Mackerras #endif
10970d64ceaSPaul Mackerras 
110df174e3bSEd Swarthout #ifdef CONFIG_PROC_KCORE
11170d64ceaSPaul Mackerras static struct kcore_list kcore_vmem;
11270d64ceaSPaul Mackerras 
11370d64ceaSPaul Mackerras static int __init setup_kcore(void)
11470d64ceaSPaul Mackerras {
11570d64ceaSPaul Mackerras 	int i;
11670d64ceaSPaul Mackerras 
11770d64ceaSPaul Mackerras 	for (i=0; i < lmb.memory.cnt; i++) {
11870d64ceaSPaul Mackerras 		unsigned long base, size;
11970d64ceaSPaul Mackerras 		struct kcore_list *kcore_mem;
12070d64ceaSPaul Mackerras 
12170d64ceaSPaul Mackerras 		base = lmb.memory.region[i].base;
12270d64ceaSPaul Mackerras 		size = lmb.memory.region[i].size;
12370d64ceaSPaul Mackerras 
12470d64ceaSPaul Mackerras 		/* GFP_ATOMIC to avoid might_sleep warnings during boot */
12570d64ceaSPaul Mackerras 		kcore_mem = kmalloc(sizeof(struct kcore_list), GFP_ATOMIC);
12670d64ceaSPaul Mackerras 		if (!kcore_mem)
127e48b1b45SHarvey Harrison 			panic("%s: kmalloc failed\n", __func__);
12870d64ceaSPaul Mackerras 
12970d64ceaSPaul Mackerras 		kclist_add(kcore_mem, __va(base), size);
13070d64ceaSPaul Mackerras 	}
13170d64ceaSPaul Mackerras 
13270d64ceaSPaul Mackerras 	kclist_add(&kcore_vmem, (void *)VMALLOC_START, VMALLOC_END-VMALLOC_START);
13370d64ceaSPaul Mackerras 
13470d64ceaSPaul Mackerras 	return 0;
13570d64ceaSPaul Mackerras }
13670d64ceaSPaul Mackerras module_init(setup_kcore);
137df174e3bSEd Swarthout #endif
13870d64ceaSPaul Mackerras 
139*51cc5068SAlexey Dobriyan static void pgd_ctor(void *addr)
14070d64ceaSPaul Mackerras {
141*51cc5068SAlexey Dobriyan 	memset(addr, 0, PGD_TABLE_SIZE);
142*51cc5068SAlexey Dobriyan }
143*51cc5068SAlexey Dobriyan 
144*51cc5068SAlexey Dobriyan static void pmd_ctor(void *addr)
145*51cc5068SAlexey Dobriyan {
146*51cc5068SAlexey Dobriyan 	memset(addr, 0, PMD_TABLE_SIZE);
14770d64ceaSPaul Mackerras }
14870d64ceaSPaul Mackerras 
14987655ff2SBenjamin Herrenschmidt static const unsigned int pgtable_cache_size[2] = {
150517e2263SHugh Dickins 	PGD_TABLE_SIZE, PMD_TABLE_SIZE
15170d64ceaSPaul Mackerras };
15270d64ceaSPaul Mackerras static const char *pgtable_cache_name[ARRAY_SIZE(pgtable_cache_size)] = {
153517e2263SHugh Dickins #ifdef CONFIG_PPC_64K_PAGES
154517e2263SHugh Dickins 	"pgd_cache", "pmd_cache",
155517e2263SHugh Dickins #else
156517e2263SHugh Dickins 	"pgd_cache", "pud_pmd_cache",
1573c726f8dSBenjamin Herrenschmidt #endif /* CONFIG_PPC_64K_PAGES */
158517e2263SHugh Dickins };
15970d64ceaSPaul Mackerras 
160f10a04c0SDavid Gibson #ifdef CONFIG_HUGETLB_PAGE
1610d9ea754SJon Tollefson /* Hugepages need an extra cache per hugepagesize, initialized in
1620d9ea754SJon Tollefson  * hugetlbpage.c.  We can't put into the tables above, because HPAGE_SHIFT
1630d9ea754SJon Tollefson  * is not compile time constant. */
1640d9ea754SJon Tollefson struct kmem_cache *pgtable_cache[ARRAY_SIZE(pgtable_cache_size)+MMU_PAGE_COUNT];
165f10a04c0SDavid Gibson #else
166e18b890bSChristoph Lameter struct kmem_cache *pgtable_cache[ARRAY_SIZE(pgtable_cache_size)];
167f10a04c0SDavid Gibson #endif
16870d64ceaSPaul Mackerras 
16970d64ceaSPaul Mackerras void pgtable_cache_init(void)
17070d64ceaSPaul Mackerras {
171*51cc5068SAlexey Dobriyan 	pgtable_cache[0] = kmem_cache_create(pgtable_cache_name[0], PGD_TABLE_SIZE, PGD_TABLE_SIZE, SLAB_PANIC, pgd_ctor);
172*51cc5068SAlexey Dobriyan 	pgtable_cache[1] = kmem_cache_create(pgtable_cache_name[1], PMD_TABLE_SIZE, PMD_TABLE_SIZE, SLAB_PANIC, pmd_ctor);
17370d64ceaSPaul Mackerras }
174d29eff7bSAndy Whitcroft 
175d29eff7bSAndy Whitcroft #ifdef CONFIG_SPARSEMEM_VMEMMAP
176d29eff7bSAndy Whitcroft /*
177d29eff7bSAndy Whitcroft  * Given an address within the vmemmap, determine the pfn of the page that
178d29eff7bSAndy Whitcroft  * represents the start of the section it is within.  Note that we have to
179d29eff7bSAndy Whitcroft  * do this by hand as the proffered address may not be correctly aligned.
180d29eff7bSAndy Whitcroft  * Subtraction of non-aligned pointers produces undefined results.
181d29eff7bSAndy Whitcroft  */
18209de9ff8SMichael Ellerman static unsigned long __meminit vmemmap_section_start(unsigned long page)
183d29eff7bSAndy Whitcroft {
184d29eff7bSAndy Whitcroft 	unsigned long offset = page - ((unsigned long)(vmemmap));
185d29eff7bSAndy Whitcroft 
186d29eff7bSAndy Whitcroft 	/* Return the pfn of the start of the section. */
187d29eff7bSAndy Whitcroft 	return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
188d29eff7bSAndy Whitcroft }
189d29eff7bSAndy Whitcroft 
190d29eff7bSAndy Whitcroft /*
191d29eff7bSAndy Whitcroft  * Check if this vmemmap page is already initialised.  If any section
192d29eff7bSAndy Whitcroft  * which overlaps this vmemmap page is initialised then this page is
193d29eff7bSAndy Whitcroft  * initialised already.
194d29eff7bSAndy Whitcroft  */
19509de9ff8SMichael Ellerman static int __meminit vmemmap_populated(unsigned long start, int page_size)
196d29eff7bSAndy Whitcroft {
197d29eff7bSAndy Whitcroft 	unsigned long end = start + page_size;
198d29eff7bSAndy Whitcroft 
199d29eff7bSAndy Whitcroft 	for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
200d29eff7bSAndy Whitcroft 		if (pfn_valid(vmemmap_section_start(start)))
201d29eff7bSAndy Whitcroft 			return 1;
202d29eff7bSAndy Whitcroft 
203d29eff7bSAndy Whitcroft 	return 0;
204d29eff7bSAndy Whitcroft }
205d29eff7bSAndy Whitcroft 
206d29eff7bSAndy Whitcroft int __meminit vmemmap_populate(struct page *start_page,
207d29eff7bSAndy Whitcroft 			       unsigned long nr_pages, int node)
208d29eff7bSAndy Whitcroft {
209d29eff7bSAndy Whitcroft 	unsigned long mode_rw;
210d29eff7bSAndy Whitcroft 	unsigned long start = (unsigned long)start_page;
211d29eff7bSAndy Whitcroft 	unsigned long end = (unsigned long)(start_page + nr_pages);
212cec08e7aSBenjamin Herrenschmidt 	unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
213d29eff7bSAndy Whitcroft 
214d29eff7bSAndy Whitcroft 	mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
215d29eff7bSAndy Whitcroft 
216d29eff7bSAndy Whitcroft 	/* Align to the page size of the linear mapping. */
217d29eff7bSAndy Whitcroft 	start = _ALIGN_DOWN(start, page_size);
218d29eff7bSAndy Whitcroft 
219d29eff7bSAndy Whitcroft 	for (; start < end; start += page_size) {
220d29eff7bSAndy Whitcroft 		int mapped;
221d29eff7bSAndy Whitcroft 		void *p;
222d29eff7bSAndy Whitcroft 
223d29eff7bSAndy Whitcroft 		if (vmemmap_populated(start, page_size))
224d29eff7bSAndy Whitcroft 			continue;
225d29eff7bSAndy Whitcroft 
226d29eff7bSAndy Whitcroft 		p = vmemmap_alloc_block(page_size, node);
227d29eff7bSAndy Whitcroft 		if (!p)
228d29eff7bSAndy Whitcroft 			return -ENOMEM;
229d29eff7bSAndy Whitcroft 
2306548d83aSStephen Rothwell 		pr_debug("vmemmap %08lx allocated at %p, physical %08lx.\n",
2316548d83aSStephen Rothwell 			start, p, __pa(p));
232d29eff7bSAndy Whitcroft 
233d29eff7bSAndy Whitcroft 		mapped = htab_bolt_mapping(start, start + page_size,
234cec08e7aSBenjamin Herrenschmidt 					__pa(p), mode_rw, mmu_vmemmap_psize,
235e95206abSAnton Blanchard 					mmu_kernel_ssize);
236d29eff7bSAndy Whitcroft 		BUG_ON(mapped < 0);
237d29eff7bSAndy Whitcroft 	}
238d29eff7bSAndy Whitcroft 
239d29eff7bSAndy Whitcroft 	return 0;
240d29eff7bSAndy Whitcroft }
241cec08e7aSBenjamin Herrenschmidt #endif /* CONFIG_SPARSEMEM_VMEMMAP */
242