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