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