xref: /linux/arch/powerpc/mm/init_64.c (revision e95206ab2c8cd6ea11318a5fe96b4ace534be750)
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 
2287655ff2SBenjamin Herrenschmidt #undef DEBUG
2387655ff2SBenjamin 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>
4370d64ceaSPaul Mackerras 
4470d64ceaSPaul Mackerras #include <asm/pgalloc.h>
4570d64ceaSPaul Mackerras #include <asm/page.h>
4670d64ceaSPaul Mackerras #include <asm/prom.h>
4770d64ceaSPaul Mackerras #include <asm/lmb.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 
6987655ff2SBenjamin Herrenschmidt #ifdef DEBUG
7087655ff2SBenjamin Herrenschmidt #define DBG(fmt...) printk(fmt)
7187655ff2SBenjamin Herrenschmidt #else
7287655ff2SBenjamin Herrenschmidt #define DBG(fmt...)
7387655ff2SBenjamin Herrenschmidt #endif
7487655ff2SBenjamin Herrenschmidt 
7570d64ceaSPaul Mackerras #if PGTABLE_RANGE > USER_VSID_RANGE
7670d64ceaSPaul Mackerras #warning Limited user VSID range means pagetable space is wasted
7770d64ceaSPaul Mackerras #endif
7870d64ceaSPaul Mackerras 
7970d64ceaSPaul Mackerras #if (TASK_SIZE_USER64 < PGTABLE_RANGE) && (TASK_SIZE_USER64 < USER_VSID_RANGE)
8070d64ceaSPaul Mackerras #warning TASK_SIZE is smaller than it needs to be.
8170d64ceaSPaul Mackerras #endif
8270d64ceaSPaul Mackerras 
8370d64ceaSPaul Mackerras /* max amount of RAM to use */
8470d64ceaSPaul Mackerras unsigned long __max_memory;
8570d64ceaSPaul Mackerras 
8670d64ceaSPaul Mackerras void free_initmem(void)
8770d64ceaSPaul Mackerras {
8870d64ceaSPaul Mackerras 	unsigned long addr;
8970d64ceaSPaul Mackerras 
9070d64ceaSPaul Mackerras 	addr = (unsigned long)__init_begin;
9170d64ceaSPaul Mackerras 	for (; addr < (unsigned long)__init_end; addr += PAGE_SIZE) {
92c9cf5528SRandy Dunlap 		memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
9370d64ceaSPaul Mackerras 		ClearPageReserved(virt_to_page(addr));
947835e98bSNick Piggin 		init_page_count(virt_to_page(addr));
9570d64ceaSPaul Mackerras 		free_page(addr);
9670d64ceaSPaul Mackerras 		totalram_pages++;
9770d64ceaSPaul Mackerras 	}
9870d64ceaSPaul Mackerras 	printk ("Freeing unused kernel memory: %luk freed\n",
9970d64ceaSPaul Mackerras 		((unsigned long)__init_end - (unsigned long)__init_begin) >> 10);
10070d64ceaSPaul Mackerras }
10170d64ceaSPaul Mackerras 
10270d64ceaSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
10370d64ceaSPaul Mackerras void free_initrd_mem(unsigned long start, unsigned long end)
10470d64ceaSPaul Mackerras {
10570d64ceaSPaul Mackerras 	if (start < end)
10670d64ceaSPaul Mackerras 		printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
10770d64ceaSPaul Mackerras 	for (; start < end; start += PAGE_SIZE) {
10870d64ceaSPaul Mackerras 		ClearPageReserved(virt_to_page(start));
1097835e98bSNick Piggin 		init_page_count(virt_to_page(start));
11070d64ceaSPaul Mackerras 		free_page(start);
11170d64ceaSPaul Mackerras 		totalram_pages++;
11270d64ceaSPaul Mackerras 	}
11370d64ceaSPaul Mackerras }
11470d64ceaSPaul Mackerras #endif
11570d64ceaSPaul Mackerras 
116df174e3bSEd Swarthout #ifdef CONFIG_PROC_KCORE
11770d64ceaSPaul Mackerras static struct kcore_list kcore_vmem;
11870d64ceaSPaul Mackerras 
11970d64ceaSPaul Mackerras static int __init setup_kcore(void)
12070d64ceaSPaul Mackerras {
12170d64ceaSPaul Mackerras 	int i;
12270d64ceaSPaul Mackerras 
12370d64ceaSPaul Mackerras 	for (i=0; i < lmb.memory.cnt; i++) {
12470d64ceaSPaul Mackerras 		unsigned long base, size;
12570d64ceaSPaul Mackerras 		struct kcore_list *kcore_mem;
12670d64ceaSPaul Mackerras 
12770d64ceaSPaul Mackerras 		base = lmb.memory.region[i].base;
12870d64ceaSPaul Mackerras 		size = lmb.memory.region[i].size;
12970d64ceaSPaul Mackerras 
13070d64ceaSPaul Mackerras 		/* GFP_ATOMIC to avoid might_sleep warnings during boot */
13170d64ceaSPaul Mackerras 		kcore_mem = kmalloc(sizeof(struct kcore_list), GFP_ATOMIC);
13270d64ceaSPaul Mackerras 		if (!kcore_mem)
133adaa3a79SGeert Uytterhoeven 			panic("%s: kmalloc failed\n", __FUNCTION__);
13470d64ceaSPaul Mackerras 
13570d64ceaSPaul Mackerras 		kclist_add(kcore_mem, __va(base), size);
13670d64ceaSPaul Mackerras 	}
13770d64ceaSPaul Mackerras 
13870d64ceaSPaul Mackerras 	kclist_add(&kcore_vmem, (void *)VMALLOC_START, VMALLOC_END-VMALLOC_START);
13970d64ceaSPaul Mackerras 
14070d64ceaSPaul Mackerras 	return 0;
14170d64ceaSPaul Mackerras }
14270d64ceaSPaul Mackerras module_init(setup_kcore);
143df174e3bSEd Swarthout #endif
14470d64ceaSPaul Mackerras 
145e18b890bSChristoph Lameter static void zero_ctor(void *addr, struct kmem_cache *cache, unsigned long flags)
14670d64ceaSPaul Mackerras {
14770d64ceaSPaul Mackerras 	memset(addr, 0, kmem_cache_size(cache));
14870d64ceaSPaul Mackerras }
14970d64ceaSPaul Mackerras 
15087655ff2SBenjamin Herrenschmidt static const unsigned int pgtable_cache_size[2] = {
151517e2263SHugh Dickins 	PGD_TABLE_SIZE, PMD_TABLE_SIZE
15270d64ceaSPaul Mackerras };
15370d64ceaSPaul Mackerras static const char *pgtable_cache_name[ARRAY_SIZE(pgtable_cache_size)] = {
154517e2263SHugh Dickins #ifdef CONFIG_PPC_64K_PAGES
155517e2263SHugh Dickins 	"pgd_cache", "pmd_cache",
156517e2263SHugh Dickins #else
157517e2263SHugh Dickins 	"pgd_cache", "pud_pmd_cache",
1583c726f8dSBenjamin Herrenschmidt #endif /* CONFIG_PPC_64K_PAGES */
159517e2263SHugh Dickins };
16070d64ceaSPaul Mackerras 
161f10a04c0SDavid Gibson #ifdef CONFIG_HUGETLB_PAGE
162f10a04c0SDavid Gibson /* Hugepages need one extra cache, initialized in hugetlbpage.c.  We
163f10a04c0SDavid Gibson  * can't put into the tables above, because HPAGE_SHIFT is not compile
164f10a04c0SDavid Gibson  * time constant. */
165e18b890bSChristoph Lameter struct kmem_cache *pgtable_cache[ARRAY_SIZE(pgtable_cache_size)+1];
166f10a04c0SDavid Gibson #else
167e18b890bSChristoph Lameter struct kmem_cache *pgtable_cache[ARRAY_SIZE(pgtable_cache_size)];
168f10a04c0SDavid Gibson #endif
16970d64ceaSPaul Mackerras 
17070d64ceaSPaul Mackerras void pgtable_cache_init(void)
17170d64ceaSPaul Mackerras {
17270d64ceaSPaul Mackerras 	int i;
17370d64ceaSPaul Mackerras 
17470d64ceaSPaul Mackerras 	for (i = 0; i < ARRAY_SIZE(pgtable_cache_size); i++) {
17570d64ceaSPaul Mackerras 		int size = pgtable_cache_size[i];
17670d64ceaSPaul Mackerras 		const char *name = pgtable_cache_name[i];
17770d64ceaSPaul Mackerras 
17887655ff2SBenjamin Herrenschmidt 		DBG("Allocating page table cache %s (#%d) "
17987655ff2SBenjamin Herrenschmidt 		    "for size: %08x...\n", name, i, size);
18070d64ceaSPaul Mackerras 		pgtable_cache[i] = kmem_cache_create(name,
18170d64ceaSPaul Mackerras 						     size, size,
1820e6b9c98SAkinobu Mita 						     SLAB_PANIC,
18320c2df83SPaul Mundt 						     zero_ctor);
18470d64ceaSPaul Mackerras 	}
18570d64ceaSPaul Mackerras }
186d29eff7bSAndy Whitcroft 
187d29eff7bSAndy Whitcroft #ifdef CONFIG_SPARSEMEM_VMEMMAP
188d29eff7bSAndy Whitcroft /*
189d29eff7bSAndy Whitcroft  * Given an address within the vmemmap, determine the pfn of the page that
190d29eff7bSAndy Whitcroft  * represents the start of the section it is within.  Note that we have to
191d29eff7bSAndy Whitcroft  * do this by hand as the proffered address may not be correctly aligned.
192d29eff7bSAndy Whitcroft  * Subtraction of non-aligned pointers produces undefined results.
193d29eff7bSAndy Whitcroft  */
194d29eff7bSAndy Whitcroft unsigned long __meminit vmemmap_section_start(unsigned long page)
195d29eff7bSAndy Whitcroft {
196d29eff7bSAndy Whitcroft 	unsigned long offset = page - ((unsigned long)(vmemmap));
197d29eff7bSAndy Whitcroft 
198d29eff7bSAndy Whitcroft 	/* Return the pfn of the start of the section. */
199d29eff7bSAndy Whitcroft 	return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
200d29eff7bSAndy Whitcroft }
201d29eff7bSAndy Whitcroft 
202d29eff7bSAndy Whitcroft /*
203d29eff7bSAndy Whitcroft  * Check if this vmemmap page is already initialised.  If any section
204d29eff7bSAndy Whitcroft  * which overlaps this vmemmap page is initialised then this page is
205d29eff7bSAndy Whitcroft  * initialised already.
206d29eff7bSAndy Whitcroft  */
207d29eff7bSAndy Whitcroft int __meminit vmemmap_populated(unsigned long start, int page_size)
208d29eff7bSAndy Whitcroft {
209d29eff7bSAndy Whitcroft 	unsigned long end = start + page_size;
210d29eff7bSAndy Whitcroft 
211d29eff7bSAndy Whitcroft 	for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
212d29eff7bSAndy Whitcroft 		if (pfn_valid(vmemmap_section_start(start)))
213d29eff7bSAndy Whitcroft 			return 1;
214d29eff7bSAndy Whitcroft 
215d29eff7bSAndy Whitcroft 	return 0;
216d29eff7bSAndy Whitcroft }
217d29eff7bSAndy Whitcroft 
218d29eff7bSAndy Whitcroft int __meminit vmemmap_populate(struct page *start_page,
219d29eff7bSAndy Whitcroft 					unsigned long nr_pages, int node)
220d29eff7bSAndy Whitcroft {
221d29eff7bSAndy Whitcroft 	unsigned long mode_rw;
222d29eff7bSAndy Whitcroft 	unsigned long start = (unsigned long)start_page;
223d29eff7bSAndy Whitcroft 	unsigned long end = (unsigned long)(start_page + nr_pages);
224d29eff7bSAndy Whitcroft 	unsigned long page_size = 1 << mmu_psize_defs[mmu_linear_psize].shift;
225d29eff7bSAndy Whitcroft 
226d29eff7bSAndy Whitcroft 	mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
227d29eff7bSAndy Whitcroft 
228d29eff7bSAndy Whitcroft 	/* Align to the page size of the linear mapping. */
229d29eff7bSAndy Whitcroft 	start = _ALIGN_DOWN(start, page_size);
230d29eff7bSAndy Whitcroft 
231d29eff7bSAndy Whitcroft 	for (; start < end; start += page_size) {
232d29eff7bSAndy Whitcroft 		int mapped;
233d29eff7bSAndy Whitcroft 		void *p;
234d29eff7bSAndy Whitcroft 
235d29eff7bSAndy Whitcroft 		if (vmemmap_populated(start, page_size))
236d29eff7bSAndy Whitcroft 			continue;
237d29eff7bSAndy Whitcroft 
238d29eff7bSAndy Whitcroft 		p = vmemmap_alloc_block(page_size, node);
239d29eff7bSAndy Whitcroft 		if (!p)
240d29eff7bSAndy Whitcroft 			return -ENOMEM;
241d29eff7bSAndy Whitcroft 
242d29eff7bSAndy Whitcroft 		printk(KERN_WARNING "vmemmap %08lx allocated at %p, "
243d29eff7bSAndy Whitcroft 					"physical %p.\n", start, p, __pa(p));
244d29eff7bSAndy Whitcroft 
245d29eff7bSAndy Whitcroft 		mapped = htab_bolt_mapping(start, start + page_size,
246*e95206abSAnton Blanchard 					__pa(p), mode_rw, mmu_linear_psize,
247*e95206abSAnton Blanchard 					mmu_kernel_ssize);
248d29eff7bSAndy Whitcroft 		BUG_ON(mapped < 0);
249d29eff7bSAndy Whitcroft 	}
250d29eff7bSAndy Whitcroft 
251d29eff7bSAndy Whitcroft 	return 0;
252d29eff7bSAndy Whitcroft }
253d29eff7bSAndy Whitcroft #endif
254