13f65ce4dSChris Zankel /* 23f65ce4dSChris Zankel * arch/xtensa/mm/init.c 33f65ce4dSChris Zankel * 43f65ce4dSChris Zankel * Derived from MIPS, PPC. 53f65ce4dSChris Zankel * 63f65ce4dSChris Zankel * This file is subject to the terms and conditions of the GNU General Public 73f65ce4dSChris Zankel * License. See the file "COPYING" in the main directory of this archive 83f65ce4dSChris Zankel * for more details. 93f65ce4dSChris Zankel * 103f65ce4dSChris Zankel * Copyright (C) 2001 - 2005 Tensilica Inc. 113f65ce4dSChris Zankel * 123f65ce4dSChris Zankel * Chris Zankel <chris@zankel.net> 133f65ce4dSChris Zankel * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com> 143f65ce4dSChris Zankel * Marc Gauthier 153f65ce4dSChris Zankel * Kevin Chea 163f65ce4dSChris Zankel */ 173f65ce4dSChris Zankel 183f65ce4dSChris Zankel #include <linux/kernel.h> 193f65ce4dSChris Zankel #include <linux/errno.h> 203f65ce4dSChris Zankel #include <linux/bootmem.h> 213f65ce4dSChris Zankel #include <linux/swap.h> 226656920bSChris Zankel #include <linux/mman.h> 236656920bSChris Zankel #include <linux/nodemask.h> 246656920bSChris Zankel #include <linux/mm.h> 256656920bSChris Zankel #include <linux/slab.h> 263f65ce4dSChris Zankel 273f65ce4dSChris Zankel #include <asm/pgtable.h> 283f65ce4dSChris Zankel #include <asm/bootparam.h> 293f65ce4dSChris Zankel #include <asm/mmu_context.h> 303f65ce4dSChris Zankel #include <asm/tlb.h> 313f65ce4dSChris Zankel #include <asm/page.h> 323f65ce4dSChris Zankel #include <asm/pgalloc.h> 333f65ce4dSChris Zankel 343f65ce4dSChris Zankel 353f65ce4dSChris Zankel DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); 363f65ce4dSChris Zankel 373f65ce4dSChris Zankel /* References to section boundaries */ 383f65ce4dSChris Zankel 393f65ce4dSChris Zankel extern char _ftext, _etext, _fdata, _edata, _rodata_end; 403f65ce4dSChris Zankel extern char __init_begin, __init_end; 413f65ce4dSChris Zankel 423f65ce4dSChris Zankel /* 433f65ce4dSChris Zankel * mem_reserve(start, end, must_exist) 443f65ce4dSChris Zankel * 453f65ce4dSChris Zankel * Reserve some memory from the memory pool. 463f65ce4dSChris Zankel * 473f65ce4dSChris Zankel * Parameters: 483f65ce4dSChris Zankel * start Start of region, 493f65ce4dSChris Zankel * end End of region, 503f65ce4dSChris Zankel * must_exist Must exist in memory pool. 513f65ce4dSChris Zankel * 523f65ce4dSChris Zankel * Returns: 533f65ce4dSChris Zankel * 0 (memory area couldn't be mapped) 543f65ce4dSChris Zankel * -1 (success) 553f65ce4dSChris Zankel */ 563f65ce4dSChris Zankel 573f65ce4dSChris Zankel int __init mem_reserve(unsigned long start, unsigned long end, int must_exist) 583f65ce4dSChris Zankel { 593f65ce4dSChris Zankel int i; 603f65ce4dSChris Zankel 613f65ce4dSChris Zankel if (start == end) 623f65ce4dSChris Zankel return 0; 633f65ce4dSChris Zankel 643f65ce4dSChris Zankel start = start & PAGE_MASK; 653f65ce4dSChris Zankel end = PAGE_ALIGN(end); 663f65ce4dSChris Zankel 673f65ce4dSChris Zankel for (i = 0; i < sysmem.nr_banks; i++) 683f65ce4dSChris Zankel if (start < sysmem.bank[i].end 693f65ce4dSChris Zankel && end >= sysmem.bank[i].start) 703f65ce4dSChris Zankel break; 713f65ce4dSChris Zankel 723f65ce4dSChris Zankel if (i == sysmem.nr_banks) { 733f65ce4dSChris Zankel if (must_exist) 743f65ce4dSChris Zankel printk (KERN_WARNING "mem_reserve: [0x%0lx, 0x%0lx) " 753f65ce4dSChris Zankel "not in any region!\n", start, end); 763f65ce4dSChris Zankel return 0; 773f65ce4dSChris Zankel } 783f65ce4dSChris Zankel 793f65ce4dSChris Zankel if (start > sysmem.bank[i].start) { 803f65ce4dSChris Zankel if (end < sysmem.bank[i].end) { 813f65ce4dSChris Zankel /* split entry */ 823f65ce4dSChris Zankel if (sysmem.nr_banks >= SYSMEM_BANKS_MAX) 833f65ce4dSChris Zankel panic("meminfo overflow\n"); 843f65ce4dSChris Zankel sysmem.bank[sysmem.nr_banks].start = end; 853f65ce4dSChris Zankel sysmem.bank[sysmem.nr_banks].end = sysmem.bank[i].end; 863f65ce4dSChris Zankel sysmem.nr_banks++; 873f65ce4dSChris Zankel } 883f65ce4dSChris Zankel sysmem.bank[i].end = start; 893f65ce4dSChris Zankel } else { 903f65ce4dSChris Zankel if (end < sysmem.bank[i].end) 913f65ce4dSChris Zankel sysmem.bank[i].start = end; 923f65ce4dSChris Zankel else { 933f65ce4dSChris Zankel /* remove entry */ 943f65ce4dSChris Zankel sysmem.nr_banks--; 953f65ce4dSChris Zankel sysmem.bank[i].start = sysmem.bank[sysmem.nr_banks].start; 963f65ce4dSChris Zankel sysmem.bank[i].end = sysmem.bank[sysmem.nr_banks].end; 973f65ce4dSChris Zankel } 983f65ce4dSChris Zankel } 993f65ce4dSChris Zankel return -1; 1003f65ce4dSChris Zankel } 1013f65ce4dSChris Zankel 1023f65ce4dSChris Zankel 1033f65ce4dSChris Zankel /* 1043f65ce4dSChris Zankel * Initialize the bootmem system and give it all the memory we have available. 1053f65ce4dSChris Zankel */ 1063f65ce4dSChris Zankel 1073f65ce4dSChris Zankel void __init bootmem_init(void) 1083f65ce4dSChris Zankel { 1093f65ce4dSChris Zankel unsigned long pfn; 1103f65ce4dSChris Zankel unsigned long bootmap_start, bootmap_size; 1113f65ce4dSChris Zankel int i; 1123f65ce4dSChris Zankel 1133f65ce4dSChris Zankel max_low_pfn = max_pfn = 0; 1143f65ce4dSChris Zankel min_low_pfn = ~0; 1153f65ce4dSChris Zankel 1163f65ce4dSChris Zankel for (i=0; i < sysmem.nr_banks; i++) { 1173f65ce4dSChris Zankel pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT; 1183f65ce4dSChris Zankel if (pfn < min_low_pfn) 1193f65ce4dSChris Zankel min_low_pfn = pfn; 1203f65ce4dSChris Zankel pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT; 1213f65ce4dSChris Zankel if (pfn > max_pfn) 1223f65ce4dSChris Zankel max_pfn = pfn; 1233f65ce4dSChris Zankel } 1243f65ce4dSChris Zankel 1253f65ce4dSChris Zankel if (min_low_pfn > max_pfn) 1263f65ce4dSChris Zankel panic("No memory found!\n"); 1273f65ce4dSChris Zankel 128173d6681SChris Zankel max_low_pfn = max_pfn < MAX_MEM_PFN >> PAGE_SHIFT ? 129173d6681SChris Zankel max_pfn : MAX_MEM_PFN >> PAGE_SHIFT; 1303f65ce4dSChris Zankel 1313f65ce4dSChris Zankel /* Find an area to use for the bootmem bitmap. */ 1323f65ce4dSChris Zankel 1333f65ce4dSChris Zankel bootmap_size = bootmem_bootmap_pages(max_low_pfn) << PAGE_SHIFT; 1343f65ce4dSChris Zankel bootmap_start = ~0; 1353f65ce4dSChris Zankel 1363f65ce4dSChris Zankel for (i=0; i<sysmem.nr_banks; i++) 1373f65ce4dSChris Zankel if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) { 1383f65ce4dSChris Zankel bootmap_start = sysmem.bank[i].start; 1393f65ce4dSChris Zankel break; 1403f65ce4dSChris Zankel } 1413f65ce4dSChris Zankel 1423f65ce4dSChris Zankel if (bootmap_start == ~0UL) 1433f65ce4dSChris Zankel panic("Cannot find %ld bytes for bootmap\n", bootmap_size); 1443f65ce4dSChris Zankel 1453f65ce4dSChris Zankel /* Reserve the bootmem bitmap area */ 1463f65ce4dSChris Zankel 1473f65ce4dSChris Zankel mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1); 1480bef42e5SJohannes Weiner bootmap_size = init_bootmem_node(NODE_DATA(0), 1493f65ce4dSChris Zankel bootmap_start >> PAGE_SHIFT, 1500bef42e5SJohannes Weiner min_low_pfn, 1513f65ce4dSChris Zankel max_low_pfn); 1523f65ce4dSChris Zankel 1533f65ce4dSChris Zankel /* Add all remaining memory pieces into the bootmem map */ 1543f65ce4dSChris Zankel 1553f65ce4dSChris Zankel for (i=0; i<sysmem.nr_banks; i++) 1563f65ce4dSChris Zankel free_bootmem(sysmem.bank[i].start, 1573f65ce4dSChris Zankel sysmem.bank[i].end - sysmem.bank[i].start); 1583f65ce4dSChris Zankel 1593f65ce4dSChris Zankel } 1603f65ce4dSChris Zankel 1613f65ce4dSChris Zankel 1623f65ce4dSChris Zankel void __init paging_init(void) 1633f65ce4dSChris Zankel { 1643f65ce4dSChris Zankel unsigned long zones_size[MAX_NR_ZONES]; 1653f65ce4dSChris Zankel int i; 1663f65ce4dSChris Zankel 1673f65ce4dSChris Zankel /* All pages are DMA-able, so we put them all in the DMA zone. */ 1683f65ce4dSChris Zankel 1693f65ce4dSChris Zankel zones_size[ZONE_DMA] = max_low_pfn; 1703f65ce4dSChris Zankel for (i = 1; i < MAX_NR_ZONES; i++) 1713f65ce4dSChris Zankel zones_size[i] = 0; 1723f65ce4dSChris Zankel 1733f65ce4dSChris Zankel #ifdef CONFIG_HIGHMEM 1743f65ce4dSChris Zankel zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn; 1753f65ce4dSChris Zankel #endif 1763f65ce4dSChris Zankel 1773f65ce4dSChris Zankel /* Initialize the kernel's page tables. */ 1783f65ce4dSChris Zankel 1793f65ce4dSChris Zankel memset(swapper_pg_dir, 0, PAGE_SIZE); 1803f65ce4dSChris Zankel 1813f65ce4dSChris Zankel free_area_init(zones_size); 1823f65ce4dSChris Zankel } 1833f65ce4dSChris Zankel 1843f65ce4dSChris Zankel /* 1853f65ce4dSChris Zankel * Flush the mmu and reset associated register to default values. 1863f65ce4dSChris Zankel */ 1873f65ce4dSChris Zankel 1883f65ce4dSChris Zankel void __init init_mmu (void) 1893f65ce4dSChris Zankel { 1903f65ce4dSChris Zankel /* Writing zeros to the <t>TLBCFG special registers ensure 1913f65ce4dSChris Zankel * that valid values exist in the register. For existing 1923f65ce4dSChris Zankel * PGSZID<w> fields, zero selects the first element of the 1934af410a8SChris Zankel * page-size array. For nonexistent PGSZID<w> fields, zero is 1943f65ce4dSChris Zankel * the best value to write. Also, when changing PGSZID<w> 1953f65ce4dSChris Zankel * fields, the corresponding TLB must be flushed. 1963f65ce4dSChris Zankel */ 1973f65ce4dSChris Zankel set_itlbcfg_register (0); 1983f65ce4dSChris Zankel set_dtlbcfg_register (0); 1993f65ce4dSChris Zankel flush_tlb_all (); 2003f65ce4dSChris Zankel 2013f65ce4dSChris Zankel /* Set rasid register to a known value. */ 2023f65ce4dSChris Zankel 203173d6681SChris Zankel set_rasid_register (ASID_USER_FIRST); 2043f65ce4dSChris Zankel 2053f65ce4dSChris Zankel /* Set PTEVADDR special register to the start of the page 2063f65ce4dSChris Zankel * table, which is in kernel mappable space (ie. not 2073f65ce4dSChris Zankel * statically mapped). This register's value is undefined on 2083f65ce4dSChris Zankel * reset. 2093f65ce4dSChris Zankel */ 2103f65ce4dSChris Zankel set_ptevaddr_register (PGTABLE_START); 2113f65ce4dSChris Zankel } 2123f65ce4dSChris Zankel 2133f65ce4dSChris Zankel /* 2143f65ce4dSChris Zankel * Initialize memory pages. 2153f65ce4dSChris Zankel */ 2163f65ce4dSChris Zankel 2173f65ce4dSChris Zankel void __init mem_init(void) 2183f65ce4dSChris Zankel { 2193f65ce4dSChris Zankel unsigned long codesize, reservedpages, datasize, initsize; 2203f65ce4dSChris Zankel unsigned long highmemsize, tmp, ram; 2213f65ce4dSChris Zankel 2223f65ce4dSChris Zankel max_mapnr = num_physpages = max_low_pfn; 2233f65ce4dSChris Zankel high_memory = (void *) __va(max_mapnr << PAGE_SHIFT); 2243f65ce4dSChris Zankel highmemsize = 0; 2253f65ce4dSChris Zankel 226288a60cfSChris Zankel #ifdef CONFIG_HIGHMEM 2273f65ce4dSChris Zankel #error HIGHGMEM not implemented in init.c 2283f65ce4dSChris Zankel #endif 2293f65ce4dSChris Zankel 2303f65ce4dSChris Zankel totalram_pages += free_all_bootmem(); 2313f65ce4dSChris Zankel 2323f65ce4dSChris Zankel reservedpages = ram = 0; 2333f65ce4dSChris Zankel for (tmp = 0; tmp < max_low_pfn; tmp++) { 2343f65ce4dSChris Zankel ram++; 2353f65ce4dSChris Zankel if (PageReserved(mem_map+tmp)) 2363f65ce4dSChris Zankel reservedpages++; 2373f65ce4dSChris Zankel } 2383f65ce4dSChris Zankel 2393f65ce4dSChris Zankel codesize = (unsigned long) &_etext - (unsigned long) &_ftext; 2403f65ce4dSChris Zankel datasize = (unsigned long) &_edata - (unsigned long) &_fdata; 2413f65ce4dSChris Zankel initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; 2423f65ce4dSChris Zankel 2433f65ce4dSChris Zankel printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, " 2443f65ce4dSChris Zankel "%ldk data, %ldk init %ldk highmem)\n", 2453f65ce4dSChris Zankel (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), 2463f65ce4dSChris Zankel ram << (PAGE_SHIFT-10), 2473f65ce4dSChris Zankel codesize >> 10, 2483f65ce4dSChris Zankel reservedpages << (PAGE_SHIFT-10), 2493f65ce4dSChris Zankel datasize >> 10, 2503f65ce4dSChris Zankel initsize >> 10, 2513f65ce4dSChris Zankel highmemsize >> 10); 2523f65ce4dSChris Zankel } 2533f65ce4dSChris Zankel 2543f65ce4dSChris Zankel void 2553f65ce4dSChris Zankel free_reserved_mem(void *start, void *end) 2563f65ce4dSChris Zankel { 2573f65ce4dSChris Zankel for (; start < end; start += PAGE_SIZE) { 2583f65ce4dSChris Zankel ClearPageReserved(virt_to_page(start)); 2597835e98bSNick Piggin init_page_count(virt_to_page(start)); 2603f65ce4dSChris Zankel free_page((unsigned long)start); 2613f65ce4dSChris Zankel totalram_pages++; 2623f65ce4dSChris Zankel } 2633f65ce4dSChris Zankel } 2643f65ce4dSChris Zankel 2653f65ce4dSChris Zankel #ifdef CONFIG_BLK_DEV_INITRD 2663f65ce4dSChris Zankel extern int initrd_is_mapped; 2673f65ce4dSChris Zankel 2683f65ce4dSChris Zankel void free_initrd_mem(unsigned long start, unsigned long end) 2693f65ce4dSChris Zankel { 2703f65ce4dSChris Zankel if (initrd_is_mapped) { 2713f65ce4dSChris Zankel free_reserved_mem((void*)start, (void*)end); 2723f65ce4dSChris Zankel printk ("Freeing initrd memory: %ldk freed\n",(end-start)>>10); 2733f65ce4dSChris Zankel } 2743f65ce4dSChris Zankel } 2753f65ce4dSChris Zankel #endif 2763f65ce4dSChris Zankel 2773f65ce4dSChris Zankel void free_initmem(void) 2783f65ce4dSChris Zankel { 2793f65ce4dSChris Zankel free_reserved_mem(&__init_begin, &__init_end); 2803f65ce4dSChris Zankel printk("Freeing unused kernel memory: %dk freed\n", 2813f65ce4dSChris Zankel (&__init_end - &__init_begin) >> 10); 2823f65ce4dSChris Zankel } 2833f65ce4dSChris Zankel 2846656920bSChris Zankel struct kmem_cache *pgtable_cache __read_mostly; 2853f65ce4dSChris Zankel 28651cc5068SAlexey Dobriyan static void pgd_ctor(void* addr) 2873f65ce4dSChris Zankel { 2886656920bSChris Zankel pte_t* ptep = (pte_t*)addr; 2893f65ce4dSChris Zankel int i; 2903f65ce4dSChris Zankel 2913f65ce4dSChris Zankel for (i = 0; i < 1024; i++, ptep++) 2926656920bSChris Zankel pte_clear(NULL, 0, ptep); 2933f65ce4dSChris Zankel 2943f65ce4dSChris Zankel } 2953f65ce4dSChris Zankel 2966656920bSChris Zankel void __init pgtable_cache_init(void) 2973f65ce4dSChris Zankel { 2986656920bSChris Zankel pgtable_cache = kmem_cache_create("pgd", 2996656920bSChris Zankel PAGE_SIZE, PAGE_SIZE, 3006656920bSChris Zankel SLAB_HWCACHE_ALIGN, 3016656920bSChris Zankel pgd_ctor); 3023f65ce4dSChris Zankel } 303