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