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> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/swap.h> |
| 12 | #include <linux/init.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 13 | #include <linux/mmzone.h> |
Haavard Skinnemoen | 8bd8974 | 2008-06-08 23:28:45 +0200 | [diff] [blame] | 14 | #include <linux/module.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 15 | #include <linux/bootmem.h> |
| 16 | #include <linux/pagemap.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 17 | #include <linux/nodemask.h> |
| 18 | |
| 19 | #include <asm/page.h> |
| 20 | #include <asm/mmu_context.h> |
| 21 | #include <asm/tlb.h> |
| 22 | #include <asm/io.h> |
| 23 | #include <asm/dma.h> |
| 24 | #include <asm/setup.h> |
| 25 | #include <asm/sections.h> |
| 26 | |
Haavard Skinnemoen | cfd23e9 | 2008-01-14 22:15:05 +0100 | [diff] [blame] | 27 | #define __page_aligned __attribute__((section(".data.page_aligned"))) |
| 28 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 29 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
| 30 | |
Haavard Skinnemoen | cfd23e9 | 2008-01-14 22:15:05 +0100 | [diff] [blame] | 31 | pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 32 | |
| 33 | struct page *empty_zero_page; |
Haavard Skinnemoen | 8bd8974 | 2008-06-08 23:28:45 +0200 | [diff] [blame] | 34 | EXPORT_SYMBOL(empty_zero_page); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Cache of MMU context last used. |
| 38 | */ |
| 39 | unsigned long mmu_context_cache = NO_CONTEXT; |
| 40 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 41 | void show_mem(void) |
| 42 | { |
| 43 | int total = 0, reserved = 0, cached = 0; |
| 44 | int slab = 0, free = 0, shared = 0; |
| 45 | pg_data_t *pgdat; |
| 46 | |
| 47 | printk("Mem-info:\n"); |
| 48 | show_free_areas(); |
| 49 | |
| 50 | for_each_online_pgdat(pgdat) { |
| 51 | struct page *page, *end; |
| 52 | |
| 53 | page = pgdat->node_mem_map; |
| 54 | end = page + pgdat->node_spanned_pages; |
| 55 | |
| 56 | do { |
| 57 | total++; |
| 58 | if (PageReserved(page)) |
| 59 | reserved++; |
| 60 | else if (PageSwapCache(page)) |
| 61 | cached++; |
| 62 | else if (PageSlab(page)) |
| 63 | slab++; |
| 64 | else if (!page_count(page)) |
| 65 | free++; |
| 66 | else |
| 67 | shared += page_count(page) - 1; |
| 68 | page++; |
| 69 | } while (page < end); |
| 70 | } |
| 71 | |
| 72 | printk ("%d pages of RAM\n", total); |
| 73 | printk ("%d free pages\n", free); |
| 74 | printk ("%d reserved pages\n", reserved); |
| 75 | printk ("%d slab pages\n", slab); |
| 76 | printk ("%d pages shared\n", shared); |
| 77 | printk ("%d pages swap cached\n", cached); |
| 78 | } |
| 79 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 80 | /* |
| 81 | * paging_init() sets up the page tables |
| 82 | * |
| 83 | * This routine also unmaps the page at virtual kernel address 0, so |
| 84 | * that we can trap those pesky NULL-reference errors in the kernel. |
| 85 | */ |
| 86 | void __init paging_init(void) |
| 87 | { |
| 88 | extern unsigned long _evba; |
| 89 | void *zero_page; |
| 90 | int nid; |
| 91 | |
| 92 | /* |
| 93 | * Make sure we can handle exceptions before enabling |
| 94 | * paging. Not that we should ever _get_ any exceptions this |
| 95 | * early, but you never know... |
| 96 | */ |
| 97 | printk("Exception vectors start at %p\n", &_evba); |
| 98 | sysreg_write(EVBA, (unsigned long)&_evba); |
| 99 | |
| 100 | /* |
| 101 | * Since we are ready to handle exceptions now, we should let |
| 102 | * the CPU generate them... |
| 103 | */ |
| 104 | __asm__ __volatile__ ("csrf %0" : : "i"(SR_EM_BIT)); |
| 105 | |
| 106 | /* |
| 107 | * Allocate the zero page. The allocator will panic if it |
| 108 | * can't satisfy the request, so no need to check. |
| 109 | */ |
| 110 | zero_page = alloc_bootmem_low_pages_node(NODE_DATA(0), |
| 111 | PAGE_SIZE); |
| 112 | |
Haavard Skinnemoen | ebe7459 | 2008-01-14 22:16:50 +0100 | [diff] [blame] | 113 | sysreg_write(PTBR, (unsigned long)swapper_pg_dir); |
| 114 | enable_mmu(); |
| 115 | printk ("CPU: Paging enabled\n"); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 116 | |
| 117 | for_each_online_node(nid) { |
| 118 | pg_data_t *pgdat = NODE_DATA(nid); |
| 119 | unsigned long zones_size[MAX_NR_ZONES]; |
| 120 | unsigned long low, start_pfn; |
| 121 | |
| 122 | start_pfn = pgdat->bdata->node_boot_start; |
| 123 | start_pfn >>= PAGE_SHIFT; |
| 124 | low = pgdat->bdata->node_low_pfn; |
| 125 | |
| 126 | memset(zones_size, 0, sizeof(zones_size)); |
| 127 | zones_size[ZONE_NORMAL] = low - start_pfn; |
| 128 | |
| 129 | printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n", |
| 130 | nid, start_pfn, low); |
| 131 | |
| 132 | free_area_init_node(nid, pgdat, zones_size, start_pfn, NULL); |
| 133 | |
| 134 | printk("Node %u: mem_map starts at %p\n", |
| 135 | pgdat->node_id, pgdat->node_mem_map); |
| 136 | } |
| 137 | |
| 138 | mem_map = NODE_DATA(0)->node_mem_map; |
| 139 | |
| 140 | memset(zero_page, 0, PAGE_SIZE); |
| 141 | empty_zero_page = virt_to_page(zero_page); |
| 142 | flush_dcache_page(empty_zero_page); |
| 143 | } |
| 144 | |
| 145 | void __init mem_init(void) |
| 146 | { |
| 147 | int codesize, reservedpages, datasize, initsize; |
| 148 | int nid, i; |
| 149 | |
| 150 | reservedpages = 0; |
| 151 | high_memory = NULL; |
| 152 | |
| 153 | /* this will put all low memory onto the freelists */ |
| 154 | for_each_online_node(nid) { |
| 155 | pg_data_t *pgdat = NODE_DATA(nid); |
| 156 | unsigned long node_pages = 0; |
| 157 | void *node_high_memory; |
| 158 | |
| 159 | num_physpages += pgdat->node_present_pages; |
| 160 | |
| 161 | if (pgdat->node_spanned_pages != 0) |
| 162 | node_pages = free_all_bootmem_node(pgdat); |
| 163 | |
| 164 | totalram_pages += node_pages; |
| 165 | |
| 166 | for (i = 0; i < node_pages; i++) |
| 167 | if (PageReserved(pgdat->node_mem_map + i)) |
| 168 | reservedpages++; |
| 169 | |
| 170 | node_high_memory = (void *)((pgdat->node_start_pfn |
| 171 | + pgdat->node_spanned_pages) |
| 172 | << PAGE_SHIFT); |
| 173 | if (node_high_memory > high_memory) |
| 174 | high_memory = node_high_memory; |
| 175 | } |
| 176 | |
| 177 | max_mapnr = MAP_NR(high_memory); |
| 178 | |
| 179 | codesize = (unsigned long)_etext - (unsigned long)_text; |
| 180 | datasize = (unsigned long)_edata - (unsigned long)_data; |
| 181 | initsize = (unsigned long)__init_end - (unsigned long)__init_begin; |
| 182 | |
| 183 | printk ("Memory: %luk/%luk available (%dk kernel code, " |
| 184 | "%dk reserved, %dk data, %dk init)\n", |
| 185 | (unsigned long)nr_free_pages() << (PAGE_SHIFT - 10), |
| 186 | totalram_pages << (PAGE_SHIFT - 10), |
| 187 | codesize >> 10, |
| 188 | reservedpages << (PAGE_SHIFT - 10), |
| 189 | datasize >> 10, |
| 190 | initsize >> 10); |
| 191 | } |
| 192 | |
| 193 | static inline void free_area(unsigned long addr, unsigned long end, char *s) |
| 194 | { |
| 195 | unsigned int size = (end - addr) >> 10; |
| 196 | |
| 197 | for (; addr < end; addr += PAGE_SIZE) { |
| 198 | struct page *page = virt_to_page(addr); |
| 199 | ClearPageReserved(page); |
| 200 | init_page_count(page); |
| 201 | free_page(addr); |
| 202 | totalram_pages++; |
| 203 | } |
| 204 | |
| 205 | if (size && s) |
| 206 | printk(KERN_INFO "Freeing %s memory: %dK (%lx - %lx)\n", |
| 207 | s, size, end - (size << 10), end); |
| 208 | } |
| 209 | |
| 210 | void free_initmem(void) |
| 211 | { |
| 212 | free_area((unsigned long)__init_begin, (unsigned long)__init_end, |
| 213 | "init"); |
| 214 | } |
| 215 | |
| 216 | #ifdef CONFIG_BLK_DEV_INITRD |
| 217 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 218 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 219 | { |
Robert P. J. Day | 193fdd1 | 2007-09-19 08:43:42 -0400 | [diff] [blame] | 220 | free_area(start, end, "initrd"); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 223 | #endif |