Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2008 Michal Simek <monstr@monstr.eu> |
| 3 | * Copyright (C) 2006 Atmark Techno, Inc. |
| 4 | * |
| 5 | * This file is subject to the terms and conditions of the GNU General Public |
| 6 | * License. See the file "COPYING" in the main directory of this archive |
| 7 | * for more details. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/bootmem.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 13 | #include <linux/memblock.h> |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 14 | #include <linux/mm.h> /* mem_init */ |
| 15 | #include <linux/initrd.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/pfn.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 19 | #include <linux/swap.h> |
| 20 | |
| 21 | #include <asm/page.h> |
| 22 | #include <asm/mmu_context.h> |
| 23 | #include <asm/pgalloc.h> |
| 24 | #include <asm/sections.h> |
| 25 | #include <asm/tlb.h> |
| 26 | |
Michal Simek | 79bf3a1 | 2010-01-20 15:17:08 +0100 | [diff] [blame] | 27 | /* Use for MMU and noMMU because of PCI generic code */ |
| 28 | int mem_init_done; |
| 29 | |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 30 | #ifndef CONFIG_MMU |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 31 | unsigned int __page_offset; |
Arnd Bergmann | 5af7fa6 | 2009-05-01 21:48:15 +0000 | [diff] [blame] | 32 | EXPORT_SYMBOL(__page_offset); |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 33 | |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 34 | #else |
| 35 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
| 36 | |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 37 | static int init_bootmem_done; |
| 38 | #endif /* CONFIG_MMU */ |
| 39 | |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 40 | char *klimit = _end; |
| 41 | |
| 42 | /* |
| 43 | * Initialize the bootmem system and give it all the memory we |
| 44 | * have available. |
| 45 | */ |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 46 | unsigned long memory_start; |
Michal Simek | fd6ed51 | 2009-07-23 08:23:53 +0200 | [diff] [blame] | 47 | EXPORT_SYMBOL(memory_start); |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 48 | unsigned long memory_end; /* due to mm/nommu.c */ |
| 49 | unsigned long memory_size; |
Michal Simek | ee4bcdf | 2010-05-13 12:11:42 +0200 | [diff] [blame] | 50 | EXPORT_SYMBOL(memory_size); |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * paging_init() sets up the page tables - in fact we've already done this. |
| 54 | */ |
| 55 | static void __init paging_init(void) |
| 56 | { |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 57 | unsigned long zones_size[MAX_NR_ZONES]; |
| 58 | |
Steve Magnani | 5af9043 | 2009-05-18 03:22:40 +0200 | [diff] [blame] | 59 | /* Clean every zones */ |
| 60 | memset(zones_size, 0, sizeof(zones_size)); |
| 61 | |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 62 | /* |
| 63 | * old: we can DMA to/from any address.put all page into ZONE_DMA |
| 64 | * We use only ZONE_NORMAL |
| 65 | */ |
| 66 | zones_size[ZONE_NORMAL] = max_mapnr; |
| 67 | |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 68 | free_area_init(zones_size); |
| 69 | } |
| 70 | |
| 71 | void __init setup_memory(void) |
| 72 | { |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 73 | unsigned long map_size; |
Benjamin Herrenschmidt | 76bfcc8 | 2010-08-04 14:13:06 +1000 | [diff] [blame] | 74 | struct memblock_region *reg; |
| 75 | |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 76 | #ifndef CONFIG_MMU |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 77 | u32 kernel_align_start, kernel_align_size; |
| 78 | |
| 79 | /* Find main memory where is the kernel */ |
Benjamin Herrenschmidt | 76bfcc8 | 2010-08-04 14:13:06 +1000 | [diff] [blame] | 80 | for_each_memblock(memory, reg) { |
| 81 | memory_start = (u32)reg->base; |
| 82 | memory_end = (u32) reg->base + reg->size; |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 83 | if ((memory_start <= (u32)_text) && |
| 84 | ((u32)_text <= memory_end)) { |
| 85 | memory_size = memory_end - memory_start; |
| 86 | PAGE_OFFSET = memory_start; |
| 87 | printk(KERN_INFO "%s: Main mem: 0x%x-0x%x, " |
Michal Simek | db6e3f9 | 2009-07-06 08:21:09 +0200 | [diff] [blame] | 88 | "size 0x%08x\n", __func__, (u32) memory_start, |
| 89 | (u32) memory_end, (u32) memory_size); |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (!memory_start || !memory_end) { |
| 95 | panic("%s: Missing memory setting 0x%08x-0x%08x\n", |
Michal Simek | db6e3f9 | 2009-07-06 08:21:09 +0200 | [diff] [blame] | 96 | __func__, (u32) memory_start, (u32) memory_end); |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* reservation of region where is the kernel */ |
| 100 | kernel_align_start = PAGE_DOWN((u32)_text); |
| 101 | /* ALIGN can be remove because _end in vmlinux.lds.S is align */ |
| 102 | kernel_align_size = PAGE_UP((u32)klimit) - kernel_align_start; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 103 | memblock_reserve(kernel_align_start, kernel_align_size); |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 104 | printk(KERN_INFO "%s: kernel addr=0x%08x-0x%08x size=0x%08x\n", |
| 105 | __func__, kernel_align_start, kernel_align_start |
| 106 | + kernel_align_size, kernel_align_size); |
| 107 | |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 108 | #endif |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 109 | /* |
| 110 | * Kernel: |
| 111 | * start: base phys address of kernel - page align |
| 112 | * end: base phys address of kernel - page align |
| 113 | * |
| 114 | * min_low_pfn - the first page (mm/bootmem.c - node_boot_start) |
| 115 | * max_low_pfn |
| 116 | * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn) |
| 117 | * num_physpages - number of all pages |
| 118 | */ |
| 119 | |
| 120 | /* memory start is from the kernel end (aligned) to higher addr */ |
| 121 | min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */ |
| 122 | /* RAM is assumed contiguous */ |
| 123 | num_physpages = max_mapnr = memory_size >> PAGE_SHIFT; |
| 124 | max_pfn = max_low_pfn = memory_end >> PAGE_SHIFT; |
| 125 | |
| 126 | printk(KERN_INFO "%s: max_mapnr: %#lx\n", __func__, max_mapnr); |
| 127 | printk(KERN_INFO "%s: min_low_pfn: %#lx\n", __func__, min_low_pfn); |
| 128 | printk(KERN_INFO "%s: max_low_pfn: %#lx\n", __func__, max_low_pfn); |
| 129 | |
| 130 | /* |
| 131 | * Find an area to use for the bootmem bitmap. |
| 132 | * We look for the first area which is at least |
| 133 | * 128kB in length (128kB is enough for a bitmap |
| 134 | * for 4GB of memory, using 4kB pages), plus 1 page |
| 135 | * (in case the address isn't page-aligned). |
| 136 | */ |
Michal Simek | e058166 | 2010-05-27 11:17:35 +0200 | [diff] [blame] | 137 | map_size = init_bootmem_node(NODE_DATA(0), |
Michal Simek | 8f37b6c | 2009-08-11 12:36:12 +0200 | [diff] [blame] | 138 | PFN_UP(TOPHYS((u32)klimit)), min_low_pfn, max_low_pfn); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 139 | memblock_reserve(PFN_UP(TOPHYS((u32)klimit)) << PAGE_SHIFT, map_size); |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 140 | |
| 141 | /* free bootmem is whole main memory */ |
| 142 | free_bootmem(memory_start, memory_size); |
| 143 | |
| 144 | /* reserve allocate blocks */ |
Benjamin Herrenschmidt | 76bfcc8 | 2010-08-04 14:13:06 +1000 | [diff] [blame] | 145 | for_each_memblock(reserved, reg) { |
| 146 | pr_debug("reserved - 0x%08x-0x%08x\n", |
| 147 | (u32) reg->base, (u32) reg->size); |
| 148 | reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT); |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 149 | } |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 150 | #ifdef CONFIG_MMU |
| 151 | init_bootmem_done = 1; |
| 152 | #endif |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 153 | paging_init(); |
| 154 | } |
| 155 | |
| 156 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
| 157 | { |
| 158 | unsigned long addr; |
| 159 | |
| 160 | for (addr = begin; addr < end; addr += PAGE_SIZE) { |
| 161 | ClearPageReserved(virt_to_page(addr)); |
| 162 | init_page_count(virt_to_page(addr)); |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 163 | free_page(addr); |
| 164 | totalram_pages++; |
| 165 | } |
| 166 | printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10); |
| 167 | } |
| 168 | |
| 169 | #ifdef CONFIG_BLK_DEV_INITRD |
| 170 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 171 | { |
| 172 | int pages = 0; |
| 173 | for (; start < end; start += PAGE_SIZE) { |
| 174 | ClearPageReserved(virt_to_page(start)); |
| 175 | init_page_count(virt_to_page(start)); |
| 176 | free_page(start); |
| 177 | totalram_pages++; |
| 178 | pages++; |
| 179 | } |
Lennart Sorensen | d6f6177 | 2009-09-17 11:47:06 -0400 | [diff] [blame] | 180 | printk(KERN_NOTICE "Freeing initrd memory: %dk freed\n", |
| 181 | (int)(pages * (PAGE_SIZE / 1024))); |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 182 | } |
| 183 | #endif |
| 184 | |
| 185 | void free_initmem(void) |
| 186 | { |
| 187 | free_init_pages("unused kernel memory", |
| 188 | (unsigned long)(&__init_begin), |
| 189 | (unsigned long)(&__init_end)); |
| 190 | } |
| 191 | |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 192 | void __init mem_init(void) |
| 193 | { |
| 194 | high_memory = (void *)__va(memory_end); |
| 195 | /* this will put all memory onto the freelists */ |
| 196 | totalram_pages += free_all_bootmem(); |
| 197 | |
| 198 | printk(KERN_INFO "Memory: %luk/%luk available\n", |
Geert Uytterhoeven | cc013a8 | 2009-09-21 17:02:36 -0700 | [diff] [blame] | 199 | nr_free_pages() << (PAGE_SHIFT-10), |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 200 | num_physpages << (PAGE_SHIFT-10)); |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 201 | mem_init_done = 1; |
Michal Simek | a95d0e1 | 2009-03-27 14:25:29 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 204 | #ifndef CONFIG_MMU |
Michal Simek | 79bf3a1 | 2010-01-20 15:17:08 +0100 | [diff] [blame] | 205 | int page_is_ram(unsigned long pfn) |
| 206 | { |
| 207 | return __range_ok(pfn, 0); |
| 208 | } |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 209 | #else |
| 210 | int page_is_ram(unsigned long pfn) |
| 211 | { |
| 212 | return pfn < max_low_pfn; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Check for command-line options that affect what MMU_init will do. |
| 217 | */ |
| 218 | static void mm_cmdline_setup(void) |
| 219 | { |
| 220 | unsigned long maxmem = 0; |
| 221 | char *p = cmd_line; |
| 222 | |
| 223 | /* Look for mem= option on command line */ |
| 224 | p = strstr(cmd_line, "mem="); |
| 225 | if (p) { |
| 226 | p += 4; |
| 227 | maxmem = memparse(p, &p); |
| 228 | if (maxmem && memory_size > maxmem) { |
| 229 | memory_size = maxmem; |
| 230 | memory_end = memory_start + memory_size; |
Michal Simek | da5ab11 | 2010-09-11 00:07:06 -0700 | [diff] [blame] | 231 | memblock.memory.regions[0].size = memory_size; |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * MMU_init_hw does the chip-specific initialization of the MMU hardware. |
| 238 | */ |
| 239 | static void __init mmu_init_hw(void) |
| 240 | { |
| 241 | /* |
| 242 | * The Zone Protection Register (ZPR) defines how protection will |
| 243 | * be applied to every page which is a member of a given zone. At |
| 244 | * present, we utilize only two of the zones. |
| 245 | * The zone index bits (of ZSEL) in the PTE are used for software |
| 246 | * indicators, except the LSB. For user access, zone 1 is used, |
| 247 | * for kernel access, zone 0 is used. We set all but zone 1 |
| 248 | * to zero, allowing only kernel access as indicated in the PTE. |
| 249 | * For zone 1, we set a 01 binary (a value of 10 will not work) |
| 250 | * to allow user access as indicated in the PTE. This also allows |
| 251 | * kernel access as indicated in the PTE. |
| 252 | */ |
| 253 | __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \ |
| 254 | "mts rzpr, r11;" |
| 255 | : : : "r11"); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * MMU_init sets up the basic memory mappings for the kernel, |
| 260 | * including both RAM and possibly some I/O regions, |
| 261 | * and sets up the page tables and the MMU hardware ready to go. |
| 262 | */ |
| 263 | |
| 264 | /* called from head.S */ |
| 265 | asmlinkage void __init mmu_init(void) |
| 266 | { |
| 267 | unsigned int kstart, ksize; |
| 268 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 269 | if (!memblock.reserved.cnt) { |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 270 | printk(KERN_EMERG "Error memory count\n"); |
| 271 | machine_restart(NULL); |
| 272 | } |
| 273 | |
Michal Simek | da5ab11 | 2010-09-11 00:07:06 -0700 | [diff] [blame] | 274 | if ((u32) memblock.memory.regions[0].size < 0x1000000) { |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 275 | printk(KERN_EMERG "Memory must be greater than 16MB\n"); |
| 276 | machine_restart(NULL); |
| 277 | } |
| 278 | /* Find main memory where the kernel is */ |
Michal Simek | da5ab11 | 2010-09-11 00:07:06 -0700 | [diff] [blame] | 279 | memory_start = (u32) memblock.memory.regions[0].base; |
| 280 | memory_end = (u32) memblock.memory.regions[0].base + |
| 281 | (u32) memblock.memory.regions[0].size; |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 282 | memory_size = memory_end - memory_start; |
| 283 | |
| 284 | mm_cmdline_setup(); /* FIXME parse args from command line - not used */ |
| 285 | |
| 286 | /* |
| 287 | * Map out the kernel text/data/bss from the available physical |
| 288 | * memory. |
| 289 | */ |
| 290 | kstart = __pa(CONFIG_KERNEL_START); /* kernel start */ |
| 291 | /* kernel size */ |
| 292 | ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START)); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 293 | memblock_reserve(kstart, ksize); |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 294 | |
| 295 | #if defined(CONFIG_BLK_DEV_INITRD) |
| 296 | /* Remove the init RAM disk from the available memory. */ |
| 297 | /* if (initrd_start) { |
| 298 | mem_pieces_remove(&phys_avail, __pa(initrd_start), |
| 299 | initrd_end - initrd_start, 1); |
| 300 | }*/ |
| 301 | #endif /* CONFIG_BLK_DEV_INITRD */ |
| 302 | |
| 303 | /* Initialize the MMU hardware */ |
| 304 | mmu_init_hw(); |
| 305 | |
| 306 | /* Map in all of RAM starting at CONFIG_KERNEL_START */ |
| 307 | mapin_ram(); |
| 308 | |
| 309 | #ifdef HIGHMEM_START_BOOL |
| 310 | ioremap_base = HIGHMEM_START; |
| 311 | #else |
| 312 | ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */ |
| 313 | #endif /* CONFIG_HIGHMEM */ |
| 314 | ioremap_bot = ioremap_base; |
| 315 | |
| 316 | /* Initialize the context management stuff */ |
| 317 | mmu_context_init(); |
| 318 | } |
| 319 | |
| 320 | /* This is only called until mem_init is done. */ |
| 321 | void __init *early_get_page(void) |
| 322 | { |
| 323 | void *p; |
| 324 | if (init_bootmem_done) { |
| 325 | p = alloc_bootmem_pages(PAGE_SIZE); |
| 326 | } else { |
| 327 | /* |
| 328 | * Mem start + 32MB -> here is limit |
| 329 | * because of mem mapping from head.S |
| 330 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 331 | p = __va(memblock_alloc_base(PAGE_SIZE, PAGE_SIZE, |
Michal Simek | 4dc6083 | 2009-05-26 16:30:12 +0200 | [diff] [blame] | 332 | memory_start + 0x2000000)); |
| 333 | } |
| 334 | return p; |
| 335 | } |
Michal Simek | a84642a | 2010-01-14 17:03:49 +0100 | [diff] [blame] | 336 | |
Michal Simek | 79bf3a1 | 2010-01-20 15:17:08 +0100 | [diff] [blame] | 337 | #endif /* CONFIG_MMU */ |
| 338 | |
Michal Simek | a84642a | 2010-01-14 17:03:49 +0100 | [diff] [blame] | 339 | void * __init_refok alloc_maybe_bootmem(size_t size, gfp_t mask) |
| 340 | { |
| 341 | if (mem_init_done) |
| 342 | return kmalloc(size, mask); |
| 343 | else |
| 344 | return alloc_bootmem(size); |
| 345 | } |
| 346 | |
| 347 | void * __init_refok zalloc_maybe_bootmem(size_t size, gfp_t mask) |
| 348 | { |
| 349 | void *p; |
| 350 | |
| 351 | if (mem_init_done) |
| 352 | p = kzalloc(size, mask); |
| 353 | else { |
| 354 | p = alloc_bootmem(size); |
| 355 | if (p) |
| 356 | memset(p, 0, size); |
| 357 | } |
| 358 | return p; |
| 359 | } |