1 /* 2 * linux/arch/arm/mm/init.c 3 * 4 * Copyright (C) 1995-2005 Russell King 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 #include <linux/kernel.h> 11 #include <linux/errno.h> 12 #include <linux/swap.h> 13 #include <linux/init.h> 14 #include <linux/bootmem.h> 15 #include <linux/mman.h> 16 #include <linux/nodemask.h> 17 #include <linux/initrd.h> 18 19 #include <asm/mach-types.h> 20 #include <asm/sections.h> 21 #include <asm/setup.h> 22 #include <asm/sizes.h> 23 #include <asm/tlb.h> 24 25 #include <asm/mach/arch.h> 26 #include <asm/mach/map.h> 27 28 #include "mm.h" 29 30 static unsigned long phys_initrd_start __initdata = 0; 31 static unsigned long phys_initrd_size __initdata = 0; 32 33 static void __init early_initrd(char **p) 34 { 35 unsigned long start, size; 36 37 start = memparse(*p, p); 38 if (**p == ',') { 39 size = memparse((*p) + 1, p); 40 41 phys_initrd_start = start; 42 phys_initrd_size = size; 43 } 44 } 45 __early_param("initrd=", early_initrd); 46 47 static int __init parse_tag_initrd(const struct tag *tag) 48 { 49 printk(KERN_WARNING "ATAG_INITRD is deprecated; " 50 "please update your bootloader.\n"); 51 phys_initrd_start = __virt_to_phys(tag->u.initrd.start); 52 phys_initrd_size = tag->u.initrd.size; 53 return 0; 54 } 55 56 __tagtable(ATAG_INITRD, parse_tag_initrd); 57 58 static int __init parse_tag_initrd2(const struct tag *tag) 59 { 60 phys_initrd_start = tag->u.initrd.start; 61 phys_initrd_size = tag->u.initrd.size; 62 return 0; 63 } 64 65 __tagtable(ATAG_INITRD2, parse_tag_initrd2); 66 67 /* 68 * This keeps memory configuration data used by a couple memory 69 * initialization functions, as well as show_mem() for the skipping 70 * of holes in the memory map. It is populated by arm_add_memory(). 71 */ 72 struct meminfo meminfo; 73 74 void show_mem(void) 75 { 76 int free = 0, total = 0, reserved = 0; 77 int shared = 0, cached = 0, slab = 0, node, i; 78 struct meminfo * mi = &meminfo; 79 80 printk("Mem-info:\n"); 81 show_free_areas(); 82 for_each_online_node(node) { 83 pg_data_t *n = NODE_DATA(node); 84 struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn; 85 86 for_each_nodebank (i,mi,node) { 87 struct membank *bank = &mi->bank[i]; 88 unsigned int pfn1, pfn2; 89 struct page *page, *end; 90 91 pfn1 = bank_pfn_start(bank); 92 pfn2 = bank_pfn_end(bank); 93 94 page = map + pfn1; 95 end = map + pfn2; 96 97 do { 98 total++; 99 if (PageReserved(page)) 100 reserved++; 101 else if (PageSwapCache(page)) 102 cached++; 103 else if (PageSlab(page)) 104 slab++; 105 else if (!page_count(page)) 106 free++; 107 else 108 shared += page_count(page) - 1; 109 page++; 110 } while (page < end); 111 } 112 } 113 114 printk("%d pages of RAM\n", total); 115 printk("%d free pages\n", free); 116 printk("%d reserved pages\n", reserved); 117 printk("%d slab pages\n", slab); 118 printk("%d pages shared\n", shared); 119 printk("%d pages swap cached\n", cached); 120 } 121 122 /* 123 * FIXME: We really want to avoid allocating the bootmap bitmap 124 * over the top of the initrd. Hopefully, this is located towards 125 * the start of a bank, so if we allocate the bootmap bitmap at 126 * the end, we won't clash. 127 */ 128 static unsigned int __init 129 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages) 130 { 131 unsigned int start_pfn, i, bootmap_pfn; 132 133 start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT; 134 bootmap_pfn = 0; 135 136 for_each_nodebank(i, mi, node) { 137 struct membank *bank = &mi->bank[i]; 138 unsigned int start, end; 139 140 start = bank_pfn_start(bank); 141 end = bank_pfn_end(bank); 142 143 if (end < start_pfn) 144 continue; 145 146 if (start < start_pfn) 147 start = start_pfn; 148 149 if (end <= start) 150 continue; 151 152 if (end - start >= bootmap_pages) { 153 bootmap_pfn = start; 154 break; 155 } 156 } 157 158 if (bootmap_pfn == 0) 159 BUG(); 160 161 return bootmap_pfn; 162 } 163 164 static int __init check_initrd(struct meminfo *mi) 165 { 166 int initrd_node = -2; 167 #ifdef CONFIG_BLK_DEV_INITRD 168 unsigned long end = phys_initrd_start + phys_initrd_size; 169 170 /* 171 * Make sure that the initrd is within a valid area of 172 * memory. 173 */ 174 if (phys_initrd_size) { 175 unsigned int i; 176 177 initrd_node = -1; 178 179 for (i = 0; i < mi->nr_banks; i++) { 180 struct membank *bank = &mi->bank[i]; 181 if (bank_phys_start(bank) <= phys_initrd_start && 182 end <= bank_phys_end(bank)) 183 initrd_node = bank->node; 184 } 185 } 186 187 if (initrd_node == -1) { 188 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond " 189 "physical memory - disabling initrd\n", 190 phys_initrd_start, phys_initrd_size); 191 phys_initrd_start = phys_initrd_size = 0; 192 } 193 #endif 194 195 return initrd_node; 196 } 197 198 static inline void map_memory_bank(struct membank *bank) 199 { 200 #ifdef CONFIG_MMU 201 struct map_desc map; 202 203 map.pfn = bank_pfn_start(bank); 204 map.virtual = __phys_to_virt(bank_phys_start(bank)); 205 map.length = bank_phys_size(bank); 206 map.type = MT_MEMORY; 207 208 create_mapping(&map); 209 #endif 210 } 211 212 static unsigned long __init bootmem_init_node(int node, struct meminfo *mi) 213 { 214 unsigned long start_pfn, end_pfn, boot_pfn; 215 unsigned int boot_pages; 216 pg_data_t *pgdat; 217 int i; 218 219 start_pfn = -1UL; 220 end_pfn = 0; 221 222 /* 223 * Calculate the pfn range, and map the memory banks for this node. 224 */ 225 for_each_nodebank(i, mi, node) { 226 struct membank *bank = &mi->bank[i]; 227 unsigned long start, end; 228 229 start = bank_pfn_start(bank); 230 end = bank_pfn_end(bank); 231 232 if (start_pfn > start) 233 start_pfn = start; 234 if (end_pfn < end) 235 end_pfn = end; 236 237 map_memory_bank(bank); 238 } 239 240 /* 241 * If there is no memory in this node, ignore it. 242 */ 243 if (end_pfn == 0) 244 return end_pfn; 245 246 /* 247 * Allocate the bootmem bitmap page. 248 */ 249 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn); 250 boot_pfn = find_bootmap_pfn(node, mi, boot_pages); 251 252 /* 253 * Initialise the bootmem allocator for this node, handing the 254 * memory banks over to bootmem. 255 */ 256 node_set_online(node); 257 pgdat = NODE_DATA(node); 258 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn); 259 260 for_each_nodebank(i, mi, node) { 261 struct membank *bank = &mi->bank[i]; 262 free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank)); 263 memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank)); 264 } 265 266 /* 267 * Reserve the bootmem bitmap for this node. 268 */ 269 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT, 270 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT); 271 272 return end_pfn; 273 } 274 275 static void __init bootmem_reserve_initrd(int node) 276 { 277 #ifdef CONFIG_BLK_DEV_INITRD 278 pg_data_t *pgdat = NODE_DATA(node); 279 int res; 280 281 res = reserve_bootmem_node(pgdat, phys_initrd_start, 282 phys_initrd_size, BOOTMEM_EXCLUSIVE); 283 284 if (res == 0) { 285 initrd_start = __phys_to_virt(phys_initrd_start); 286 initrd_end = initrd_start + phys_initrd_size; 287 } else { 288 printk(KERN_ERR 289 "INITRD: 0x%08lx+0x%08lx overlaps in-use " 290 "memory region - disabling initrd\n", 291 phys_initrd_start, phys_initrd_size); 292 } 293 #endif 294 } 295 296 static void __init bootmem_free_node(int node, struct meminfo *mi) 297 { 298 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; 299 unsigned long start_pfn, end_pfn; 300 pg_data_t *pgdat = NODE_DATA(node); 301 int i; 302 303 start_pfn = pgdat->bdata->node_min_pfn; 304 end_pfn = pgdat->bdata->node_low_pfn; 305 306 /* 307 * initialise the zones within this node. 308 */ 309 memset(zone_size, 0, sizeof(zone_size)); 310 memset(zhole_size, 0, sizeof(zhole_size)); 311 312 /* 313 * The size of this node has already been determined. If we need 314 * to do anything fancy with the allocation of this memory to the 315 * zones, now is the time to do it. 316 */ 317 zone_size[0] = end_pfn - start_pfn; 318 319 /* 320 * For each bank in this node, calculate the size of the holes. 321 * holes = node_size - sum(bank_sizes_in_node) 322 */ 323 zhole_size[0] = zone_size[0]; 324 for_each_nodebank(i, mi, node) 325 zhole_size[0] -= bank_pfn_size(&mi->bank[i]); 326 327 /* 328 * Adjust the sizes according to any special requirements for 329 * this machine type. 330 */ 331 arch_adjust_zones(node, zone_size, zhole_size); 332 333 free_area_init_node(node, zone_size, start_pfn, zhole_size); 334 } 335 336 void __init bootmem_init(void) 337 { 338 struct meminfo *mi = &meminfo; 339 unsigned long memend_pfn = 0; 340 int node, initrd_node; 341 342 /* 343 * Locate which node contains the ramdisk image, if any. 344 */ 345 initrd_node = check_initrd(mi); 346 347 /* 348 * Run through each node initialising the bootmem allocator. 349 */ 350 for_each_node(node) { 351 unsigned long end_pfn = bootmem_init_node(node, mi); 352 353 /* 354 * Reserve any special node zero regions. 355 */ 356 if (node == 0) 357 reserve_node_zero(NODE_DATA(node)); 358 359 /* 360 * If the initrd is in this node, reserve its memory. 361 */ 362 if (node == initrd_node) 363 bootmem_reserve_initrd(node); 364 365 /* 366 * Remember the highest memory PFN. 367 */ 368 if (end_pfn > memend_pfn) 369 memend_pfn = end_pfn; 370 } 371 372 /* 373 * sparse_init() needs the bootmem allocator up and running. 374 */ 375 sparse_init(); 376 377 /* 378 * Now free memory in each node - free_area_init_node needs 379 * the sparse mem_map arrays initialized by sparse_init() 380 * for memmap_init_zone(), otherwise all PFNs are invalid. 381 */ 382 for_each_node(node) 383 bootmem_free_node(node, mi); 384 385 high_memory = __va(memend_pfn << PAGE_SHIFT); 386 387 /* 388 * This doesn't seem to be used by the Linux memory manager any 389 * more, but is used by ll_rw_block. If we can get rid of it, we 390 * also get rid of some of the stuff above as well. 391 * 392 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in 393 * the system, not the maximum PFN. 394 */ 395 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET; 396 } 397 398 static inline int free_area(unsigned long pfn, unsigned long end, char *s) 399 { 400 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10); 401 402 for (; pfn < end; pfn++) { 403 struct page *page = pfn_to_page(pfn); 404 ClearPageReserved(page); 405 init_page_count(page); 406 __free_page(page); 407 pages++; 408 } 409 410 if (size && s) 411 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size); 412 413 return pages; 414 } 415 416 static inline void 417 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn) 418 { 419 struct page *start_pg, *end_pg; 420 unsigned long pg, pgend; 421 422 /* 423 * Convert start_pfn/end_pfn to a struct page pointer. 424 */ 425 start_pg = pfn_to_page(start_pfn); 426 end_pg = pfn_to_page(end_pfn); 427 428 /* 429 * Convert to physical addresses, and 430 * round start upwards and end downwards. 431 */ 432 pg = PAGE_ALIGN(__pa(start_pg)); 433 pgend = __pa(end_pg) & PAGE_MASK; 434 435 /* 436 * If there are free pages between these, 437 * free the section of the memmap array. 438 */ 439 if (pg < pgend) 440 free_bootmem_node(NODE_DATA(node), pg, pgend - pg); 441 } 442 443 /* 444 * The mem_map array can get very big. Free the unused area of the memory map. 445 */ 446 static void __init free_unused_memmap_node(int node, struct meminfo *mi) 447 { 448 unsigned long bank_start, prev_bank_end = 0; 449 unsigned int i; 450 451 /* 452 * [FIXME] This relies on each bank being in address order. This 453 * may not be the case, especially if the user has provided the 454 * information on the command line. 455 */ 456 for_each_nodebank(i, mi, node) { 457 struct membank *bank = &mi->bank[i]; 458 459 bank_start = bank_pfn_start(bank); 460 if (bank_start < prev_bank_end) { 461 printk(KERN_ERR "MEM: unordered memory banks. " 462 "Not freeing memmap.\n"); 463 break; 464 } 465 466 /* 467 * If we had a previous bank, and there is a space 468 * between the current bank and the previous, free it. 469 */ 470 if (prev_bank_end && prev_bank_end != bank_start) 471 free_memmap(node, prev_bank_end, bank_start); 472 473 prev_bank_end = bank_pfn_end(bank); 474 } 475 } 476 477 /* 478 * mem_init() marks the free areas in the mem_map and tells us how much 479 * memory is free. This is done after various parts of the system have 480 * claimed their memory after the kernel image. 481 */ 482 void __init mem_init(void) 483 { 484 unsigned int codesize, datasize, initsize; 485 int i, node; 486 487 #ifndef CONFIG_DISCONTIGMEM 488 max_mapnr = virt_to_page(high_memory) - mem_map; 489 #endif 490 491 /* this will put all unused low memory onto the freelists */ 492 for_each_online_node(node) { 493 pg_data_t *pgdat = NODE_DATA(node); 494 495 free_unused_memmap_node(node, &meminfo); 496 497 if (pgdat->node_spanned_pages != 0) 498 totalram_pages += free_all_bootmem_node(pgdat); 499 } 500 501 #ifdef CONFIG_SA1111 502 /* now that our DMA memory is actually so designated, we can free it */ 503 totalram_pages += free_area(PHYS_PFN_OFFSET, 504 __phys_to_pfn(__pa(swapper_pg_dir)), NULL); 505 #endif 506 507 /* 508 * Since our memory may not be contiguous, calculate the 509 * real number of pages we have in this system 510 */ 511 printk(KERN_INFO "Memory:"); 512 num_physpages = 0; 513 for (i = 0; i < meminfo.nr_banks; i++) { 514 num_physpages += bank_pfn_size(&meminfo.bank[i]); 515 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20); 516 } 517 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT)); 518 519 codesize = _etext - _text; 520 datasize = _end - _data; 521 initsize = __init_end - __init_begin; 522 523 printk(KERN_NOTICE "Memory: %luKB available (%dK code, " 524 "%dK data, %dK init)\n", 525 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), 526 codesize >> 10, datasize >> 10, initsize >> 10); 527 528 if (PAGE_SIZE >= 16384 && num_physpages <= 128) { 529 extern int sysctl_overcommit_memory; 530 /* 531 * On a machine this small we won't get 532 * anywhere without overcommit, so turn 533 * it on by default. 534 */ 535 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; 536 } 537 } 538 539 void free_initmem(void) 540 { 541 if (!machine_is_integrator() && !machine_is_cintegrator()) 542 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)), 543 __phys_to_pfn(__pa(__init_end)), 544 "init"); 545 } 546 547 #ifdef CONFIG_BLK_DEV_INITRD 548 549 static int keep_initrd; 550 551 void free_initrd_mem(unsigned long start, unsigned long end) 552 { 553 if (!keep_initrd) 554 totalram_pages += free_area(__phys_to_pfn(__pa(start)), 555 __phys_to_pfn(__pa(end)), 556 "initrd"); 557 } 558 559 static int __init keepinitrd_setup(char *__unused) 560 { 561 keep_initrd = 1; 562 return 1; 563 } 564 565 __setup("keepinitrd", keepinitrd_setup); 566 #endif 567