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 69*94491685SBenjamin 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 77*94491685SBenjamin Herrenschmidt #endif /* CONFIG_PPC_STD_MMU_64 */ 7870d64ceaSPaul Mackerras 7937dd2badSKumar Gala phys_addr_t memstart_addr = ~0; 8037dd2badSKumar Gala phys_addr_t kernstart_addr; 81d7917ba7SKumar Gala 8270d64ceaSPaul Mackerras void free_initmem(void) 8370d64ceaSPaul Mackerras { 8470d64ceaSPaul Mackerras unsigned long addr; 8570d64ceaSPaul Mackerras 8670d64ceaSPaul Mackerras addr = (unsigned long)__init_begin; 8770d64ceaSPaul Mackerras for (; addr < (unsigned long)__init_end; addr += PAGE_SIZE) { 88c9cf5528SRandy Dunlap memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); 8970d64ceaSPaul Mackerras ClearPageReserved(virt_to_page(addr)); 907835e98bSNick Piggin init_page_count(virt_to_page(addr)); 9170d64ceaSPaul Mackerras free_page(addr); 9270d64ceaSPaul Mackerras totalram_pages++; 9370d64ceaSPaul Mackerras } 9470d64ceaSPaul Mackerras printk ("Freeing unused kernel memory: %luk freed\n", 9570d64ceaSPaul Mackerras ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10); 9670d64ceaSPaul Mackerras } 9770d64ceaSPaul Mackerras 9870d64ceaSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD 9970d64ceaSPaul Mackerras void free_initrd_mem(unsigned long start, unsigned long end) 10070d64ceaSPaul Mackerras { 10170d64ceaSPaul Mackerras if (start < end) 10270d64ceaSPaul Mackerras printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10); 10370d64ceaSPaul Mackerras for (; start < end; start += PAGE_SIZE) { 10470d64ceaSPaul Mackerras ClearPageReserved(virt_to_page(start)); 1057835e98bSNick Piggin init_page_count(virt_to_page(start)); 10670d64ceaSPaul Mackerras free_page(start); 10770d64ceaSPaul Mackerras totalram_pages++; 10870d64ceaSPaul Mackerras } 10970d64ceaSPaul Mackerras } 11070d64ceaSPaul Mackerras #endif 11170d64ceaSPaul Mackerras 112df174e3bSEd Swarthout #ifdef CONFIG_PROC_KCORE 11370d64ceaSPaul Mackerras static struct kcore_list kcore_vmem; 11470d64ceaSPaul Mackerras 11570d64ceaSPaul Mackerras static int __init setup_kcore(void) 11670d64ceaSPaul Mackerras { 11770d64ceaSPaul Mackerras int i; 11870d64ceaSPaul Mackerras 11970d64ceaSPaul Mackerras for (i=0; i < lmb.memory.cnt; i++) { 12070d64ceaSPaul Mackerras unsigned long base, size; 12170d64ceaSPaul Mackerras struct kcore_list *kcore_mem; 12270d64ceaSPaul Mackerras 12370d64ceaSPaul Mackerras base = lmb.memory.region[i].base; 12470d64ceaSPaul Mackerras size = lmb.memory.region[i].size; 12570d64ceaSPaul Mackerras 12670d64ceaSPaul Mackerras /* GFP_ATOMIC to avoid might_sleep warnings during boot */ 12770d64ceaSPaul Mackerras kcore_mem = kmalloc(sizeof(struct kcore_list), GFP_ATOMIC); 12870d64ceaSPaul Mackerras if (!kcore_mem) 129e48b1b45SHarvey Harrison panic("%s: kmalloc failed\n", __func__); 13070d64ceaSPaul Mackerras 13170d64ceaSPaul Mackerras kclist_add(kcore_mem, __va(base), size); 13270d64ceaSPaul Mackerras } 13370d64ceaSPaul Mackerras 13470d64ceaSPaul Mackerras kclist_add(&kcore_vmem, (void *)VMALLOC_START, VMALLOC_END-VMALLOC_START); 13570d64ceaSPaul Mackerras 13670d64ceaSPaul Mackerras return 0; 13770d64ceaSPaul Mackerras } 13870d64ceaSPaul Mackerras module_init(setup_kcore); 139df174e3bSEd Swarthout #endif 14070d64ceaSPaul Mackerras 14151cc5068SAlexey Dobriyan static void pgd_ctor(void *addr) 14270d64ceaSPaul Mackerras { 14351cc5068SAlexey Dobriyan memset(addr, 0, PGD_TABLE_SIZE); 14451cc5068SAlexey Dobriyan } 14551cc5068SAlexey Dobriyan 14651cc5068SAlexey Dobriyan static void pmd_ctor(void *addr) 14751cc5068SAlexey Dobriyan { 14851cc5068SAlexey Dobriyan memset(addr, 0, PMD_TABLE_SIZE); 14970d64ceaSPaul Mackerras } 15070d64ceaSPaul Mackerras 15187655ff2SBenjamin Herrenschmidt static const unsigned int pgtable_cache_size[2] = { 152517e2263SHugh Dickins PGD_TABLE_SIZE, PMD_TABLE_SIZE 15370d64ceaSPaul Mackerras }; 15470d64ceaSPaul Mackerras static const char *pgtable_cache_name[ARRAY_SIZE(pgtable_cache_size)] = { 155517e2263SHugh Dickins #ifdef CONFIG_PPC_64K_PAGES 156517e2263SHugh Dickins "pgd_cache", "pmd_cache", 157517e2263SHugh Dickins #else 158517e2263SHugh Dickins "pgd_cache", "pud_pmd_cache", 1593c726f8dSBenjamin Herrenschmidt #endif /* CONFIG_PPC_64K_PAGES */ 160517e2263SHugh Dickins }; 16170d64ceaSPaul Mackerras 162f10a04c0SDavid Gibson #ifdef CONFIG_HUGETLB_PAGE 1630d9ea754SJon Tollefson /* Hugepages need an extra cache per hugepagesize, initialized in 1640d9ea754SJon Tollefson * hugetlbpage.c. We can't put into the tables above, because HPAGE_SHIFT 1650d9ea754SJon Tollefson * is not compile time constant. */ 1660d9ea754SJon Tollefson struct kmem_cache *pgtable_cache[ARRAY_SIZE(pgtable_cache_size)+MMU_PAGE_COUNT]; 167f10a04c0SDavid Gibson #else 168e18b890bSChristoph Lameter struct kmem_cache *pgtable_cache[ARRAY_SIZE(pgtable_cache_size)]; 169f10a04c0SDavid Gibson #endif 17070d64ceaSPaul Mackerras 17170d64ceaSPaul Mackerras void pgtable_cache_init(void) 17270d64ceaSPaul Mackerras { 17351cc5068SAlexey Dobriyan pgtable_cache[0] = kmem_cache_create(pgtable_cache_name[0], PGD_TABLE_SIZE, PGD_TABLE_SIZE, SLAB_PANIC, pgd_ctor); 17451cc5068SAlexey Dobriyan pgtable_cache[1] = kmem_cache_create(pgtable_cache_name[1], PMD_TABLE_SIZE, PMD_TABLE_SIZE, SLAB_PANIC, pmd_ctor); 17570d64ceaSPaul Mackerras } 176d29eff7bSAndy Whitcroft 177d29eff7bSAndy Whitcroft #ifdef CONFIG_SPARSEMEM_VMEMMAP 178d29eff7bSAndy Whitcroft /* 179d29eff7bSAndy Whitcroft * Given an address within the vmemmap, determine the pfn of the page that 180d29eff7bSAndy Whitcroft * represents the start of the section it is within. Note that we have to 181d29eff7bSAndy Whitcroft * do this by hand as the proffered address may not be correctly aligned. 182d29eff7bSAndy Whitcroft * Subtraction of non-aligned pointers produces undefined results. 183d29eff7bSAndy Whitcroft */ 18409de9ff8SMichael Ellerman static unsigned long __meminit vmemmap_section_start(unsigned long page) 185d29eff7bSAndy Whitcroft { 186d29eff7bSAndy Whitcroft unsigned long offset = page - ((unsigned long)(vmemmap)); 187d29eff7bSAndy Whitcroft 188d29eff7bSAndy Whitcroft /* Return the pfn of the start of the section. */ 189d29eff7bSAndy Whitcroft return (offset / sizeof(struct page)) & PAGE_SECTION_MASK; 190d29eff7bSAndy Whitcroft } 191d29eff7bSAndy Whitcroft 192d29eff7bSAndy Whitcroft /* 193d29eff7bSAndy Whitcroft * Check if this vmemmap page is already initialised. If any section 194d29eff7bSAndy Whitcroft * which overlaps this vmemmap page is initialised then this page is 195d29eff7bSAndy Whitcroft * initialised already. 196d29eff7bSAndy Whitcroft */ 19709de9ff8SMichael Ellerman static int __meminit vmemmap_populated(unsigned long start, int page_size) 198d29eff7bSAndy Whitcroft { 199d29eff7bSAndy Whitcroft unsigned long end = start + page_size; 200d29eff7bSAndy Whitcroft 201d29eff7bSAndy Whitcroft for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page))) 202d29eff7bSAndy Whitcroft if (pfn_valid(vmemmap_section_start(start))) 203d29eff7bSAndy Whitcroft return 1; 204d29eff7bSAndy Whitcroft 205d29eff7bSAndy Whitcroft return 0; 206d29eff7bSAndy Whitcroft } 207d29eff7bSAndy Whitcroft 208d29eff7bSAndy Whitcroft int __meminit vmemmap_populate(struct page *start_page, 209d29eff7bSAndy Whitcroft unsigned long nr_pages, int node) 210d29eff7bSAndy Whitcroft { 211d29eff7bSAndy Whitcroft unsigned long start = (unsigned long)start_page; 212d29eff7bSAndy Whitcroft unsigned long end = (unsigned long)(start_page + nr_pages); 213cec08e7aSBenjamin Herrenschmidt unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift; 214d29eff7bSAndy Whitcroft 215d29eff7bSAndy Whitcroft /* Align to the page size of the linear mapping. */ 216d29eff7bSAndy Whitcroft start = _ALIGN_DOWN(start, page_size); 217d29eff7bSAndy Whitcroft 218d29eff7bSAndy Whitcroft for (; start < end; start += page_size) { 219d29eff7bSAndy Whitcroft int mapped; 220d29eff7bSAndy Whitcroft void *p; 221d29eff7bSAndy Whitcroft 222d29eff7bSAndy Whitcroft if (vmemmap_populated(start, page_size)) 223d29eff7bSAndy Whitcroft continue; 224d29eff7bSAndy Whitcroft 225d29eff7bSAndy Whitcroft p = vmemmap_alloc_block(page_size, node); 226d29eff7bSAndy Whitcroft if (!p) 227d29eff7bSAndy Whitcroft return -ENOMEM; 228d29eff7bSAndy Whitcroft 2296548d83aSStephen Rothwell pr_debug("vmemmap %08lx allocated at %p, physical %08lx.\n", 2306548d83aSStephen Rothwell start, p, __pa(p)); 231d29eff7bSAndy Whitcroft 232bc033b63SBenjamin Herrenschmidt mapped = htab_bolt_mapping(start, start + page_size, __pa(p), 233f5ea64dcSDavid Gibson pgprot_val(PAGE_KERNEL), 234f5ea64dcSDavid Gibson mmu_vmemmap_psize, mmu_kernel_ssize); 235d29eff7bSAndy Whitcroft BUG_ON(mapped < 0); 236d29eff7bSAndy Whitcroft } 237d29eff7bSAndy Whitcroft 238d29eff7bSAndy Whitcroft return 0; 239d29eff7bSAndy Whitcroft } 240cec08e7aSBenjamin Herrenschmidt #endif /* CONFIG_SPARSEMEM_VMEMMAP */ 241