Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2006 Atmel Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/gfp.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 11 | #include <linux/mm.h> |
| 12 | #include <linux/swap.h> |
| 13 | #include <linux/init.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 14 | #include <linux/mmzone.h> |
Haavard Skinnemoen | 8bd8974 | 2008-06-08 23:28:45 +0200 | [diff] [blame] | 15 | #include <linux/module.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 16 | #include <linux/bootmem.h> |
| 17 | #include <linux/pagemap.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 18 | #include <linux/nodemask.h> |
| 19 | |
| 20 | #include <asm/page.h> |
| 21 | #include <asm/mmu_context.h> |
| 22 | #include <asm/tlb.h> |
| 23 | #include <asm/io.h> |
| 24 | #include <asm/dma.h> |
| 25 | #include <asm/setup.h> |
| 26 | #include <asm/sections.h> |
| 27 | |
Tim Abbott | abe1ee3 | 2009-09-20 18:14:15 -0400 | [diff] [blame] | 28 | pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_data; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 29 | |
| 30 | struct page *empty_zero_page; |
Haavard Skinnemoen | 8bd8974 | 2008-06-08 23:28:45 +0200 | [diff] [blame] | 31 | EXPORT_SYMBOL(empty_zero_page); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Cache of MMU context last used. |
| 35 | */ |
| 36 | unsigned long mmu_context_cache = NO_CONTEXT; |
| 37 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 38 | /* |
| 39 | * paging_init() sets up the page tables |
| 40 | * |
| 41 | * This routine also unmaps the page at virtual kernel address 0, so |
| 42 | * that we can trap those pesky NULL-reference errors in the kernel. |
| 43 | */ |
| 44 | void __init paging_init(void) |
| 45 | { |
| 46 | extern unsigned long _evba; |
| 47 | void *zero_page; |
| 48 | int nid; |
| 49 | |
| 50 | /* |
| 51 | * Make sure we can handle exceptions before enabling |
| 52 | * paging. Not that we should ever _get_ any exceptions this |
| 53 | * early, but you never know... |
| 54 | */ |
| 55 | printk("Exception vectors start at %p\n", &_evba); |
| 56 | sysreg_write(EVBA, (unsigned long)&_evba); |
| 57 | |
| 58 | /* |
| 59 | * Since we are ready to handle exceptions now, we should let |
| 60 | * the CPU generate them... |
| 61 | */ |
| 62 | __asm__ __volatile__ ("csrf %0" : : "i"(SR_EM_BIT)); |
| 63 | |
| 64 | /* |
| 65 | * Allocate the zero page. The allocator will panic if it |
| 66 | * can't satisfy the request, so no need to check. |
| 67 | */ |
| 68 | zero_page = alloc_bootmem_low_pages_node(NODE_DATA(0), |
| 69 | PAGE_SIZE); |
| 70 | |
Haavard Skinnemoen | ebe7459 | 2008-01-14 22:16:50 +0100 | [diff] [blame] | 71 | sysreg_write(PTBR, (unsigned long)swapper_pg_dir); |
| 72 | enable_mmu(); |
| 73 | printk ("CPU: Paging enabled\n"); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 74 | |
| 75 | for_each_online_node(nid) { |
| 76 | pg_data_t *pgdat = NODE_DATA(nid); |
| 77 | unsigned long zones_size[MAX_NR_ZONES]; |
| 78 | unsigned long low, start_pfn; |
| 79 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 80 | start_pfn = pgdat->bdata->node_min_pfn; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 81 | low = pgdat->bdata->node_low_pfn; |
| 82 | |
| 83 | memset(zones_size, 0, sizeof(zones_size)); |
| 84 | zones_size[ZONE_NORMAL] = low - start_pfn; |
| 85 | |
| 86 | printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n", |
| 87 | nid, start_pfn, low); |
| 88 | |
Johannes Weiner | 9109fb7 | 2008-07-23 21:27:20 -0700 | [diff] [blame] | 89 | free_area_init_node(nid, zones_size, start_pfn, NULL); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 90 | |
| 91 | printk("Node %u: mem_map starts at %p\n", |
| 92 | pgdat->node_id, pgdat->node_mem_map); |
| 93 | } |
| 94 | |
| 95 | mem_map = NODE_DATA(0)->node_mem_map; |
| 96 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 97 | empty_zero_page = virt_to_page(zero_page); |
| 98 | flush_dcache_page(empty_zero_page); |
| 99 | } |
| 100 | |
| 101 | void __init mem_init(void) |
| 102 | { |
| 103 | int codesize, reservedpages, datasize, initsize; |
| 104 | int nid, i; |
| 105 | |
| 106 | reservedpages = 0; |
| 107 | high_memory = NULL; |
| 108 | |
| 109 | /* this will put all low memory onto the freelists */ |
| 110 | for_each_online_node(nid) { |
| 111 | pg_data_t *pgdat = NODE_DATA(nid); |
| 112 | unsigned long node_pages = 0; |
| 113 | void *node_high_memory; |
| 114 | |
| 115 | num_physpages += pgdat->node_present_pages; |
| 116 | |
| 117 | if (pgdat->node_spanned_pages != 0) |
| 118 | node_pages = free_all_bootmem_node(pgdat); |
| 119 | |
| 120 | totalram_pages += node_pages; |
| 121 | |
| 122 | for (i = 0; i < node_pages; i++) |
| 123 | if (PageReserved(pgdat->node_mem_map + i)) |
| 124 | reservedpages++; |
| 125 | |
| 126 | node_high_memory = (void *)((pgdat->node_start_pfn |
| 127 | + pgdat->node_spanned_pages) |
| 128 | << PAGE_SHIFT); |
| 129 | if (node_high_memory > high_memory) |
| 130 | high_memory = node_high_memory; |
| 131 | } |
| 132 | |
| 133 | max_mapnr = MAP_NR(high_memory); |
| 134 | |
| 135 | codesize = (unsigned long)_etext - (unsigned long)_text; |
| 136 | datasize = (unsigned long)_edata - (unsigned long)_data; |
| 137 | initsize = (unsigned long)__init_end - (unsigned long)__init_begin; |
| 138 | |
| 139 | printk ("Memory: %luk/%luk available (%dk kernel code, " |
| 140 | "%dk reserved, %dk data, %dk init)\n", |
Geert Uytterhoeven | cc013a8 | 2009-09-21 17:02:36 -0700 | [diff] [blame] | 141 | nr_free_pages() << (PAGE_SHIFT - 10), |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 142 | totalram_pages << (PAGE_SHIFT - 10), |
| 143 | codesize >> 10, |
| 144 | reservedpages << (PAGE_SHIFT - 10), |
| 145 | datasize >> 10, |
| 146 | initsize >> 10); |
| 147 | } |
| 148 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 149 | void free_initmem(void) |
| 150 | { |
Jiang Liu | c15da0a | 2013-04-29 15:06:27 -0700 | [diff] [blame^] | 151 | free_initmem_default(0); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | #ifdef CONFIG_BLK_DEV_INITRD |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 155 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 156 | { |
Jiang Liu | c15da0a | 2013-04-29 15:06:27 -0700 | [diff] [blame^] | 157 | free_reserved_area(start, end, 0, "initrd"); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 158 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 159 | #endif |