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/of_fdt.h> 19 #include <linux/highmem.h> 20 #include <linux/gfp.h> 21 #include <linux/memblock.h> 22 #include <linux/sort.h> 23 24 #include <asm/mach-types.h> 25 #include <asm/sections.h> 26 #include <asm/setup.h> 27 #include <asm/sizes.h> 28 #include <asm/tlb.h> 29 #include <asm/fixmap.h> 30 31 #include <asm/mach/arch.h> 32 #include <asm/mach/map.h> 33 34 #include "mm.h" 35 36 static unsigned long phys_initrd_start __initdata = 0; 37 static unsigned long phys_initrd_size __initdata = 0; 38 39 static int __init early_initrd(char *p) 40 { 41 unsigned long start, size; 42 char *endp; 43 44 start = memparse(p, &endp); 45 if (*endp == ',') { 46 size = memparse(endp + 1, NULL); 47 48 phys_initrd_start = start; 49 phys_initrd_size = size; 50 } 51 return 0; 52 } 53 early_param("initrd", early_initrd); 54 55 static int __init parse_tag_initrd(const struct tag *tag) 56 { 57 printk(KERN_WARNING "ATAG_INITRD is deprecated; " 58 "please update your bootloader.\n"); 59 phys_initrd_start = __virt_to_phys(tag->u.initrd.start); 60 phys_initrd_size = tag->u.initrd.size; 61 return 0; 62 } 63 64 __tagtable(ATAG_INITRD, parse_tag_initrd); 65 66 static int __init parse_tag_initrd2(const struct tag *tag) 67 { 68 phys_initrd_start = tag->u.initrd.start; 69 phys_initrd_size = tag->u.initrd.size; 70 return 0; 71 } 72 73 __tagtable(ATAG_INITRD2, parse_tag_initrd2); 74 75 #ifdef CONFIG_OF_FLATTREE 76 void __init early_init_dt_setup_initrd_arch(unsigned long start, unsigned long end) 77 { 78 phys_initrd_start = start; 79 phys_initrd_size = end - start; 80 } 81 #endif /* CONFIG_OF_FLATTREE */ 82 83 /* 84 * This keeps memory configuration data used by a couple memory 85 * initialization functions, as well as show_mem() for the skipping 86 * of holes in the memory map. It is populated by arm_add_memory(). 87 */ 88 struct meminfo meminfo; 89 90 void show_mem(unsigned int filter) 91 { 92 int free = 0, total = 0, reserved = 0; 93 int shared = 0, cached = 0, slab = 0, i; 94 struct meminfo * mi = &meminfo; 95 96 printk("Mem-info:\n"); 97 show_free_areas(); 98 99 for_each_bank (i, mi) { 100 struct membank *bank = &mi->bank[i]; 101 unsigned int pfn1, pfn2; 102 struct page *page, *end; 103 104 pfn1 = bank_pfn_start(bank); 105 pfn2 = bank_pfn_end(bank); 106 107 page = pfn_to_page(pfn1); 108 end = pfn_to_page(pfn2 - 1) + 1; 109 110 do { 111 total++; 112 if (PageReserved(page)) 113 reserved++; 114 else if (PageSwapCache(page)) 115 cached++; 116 else if (PageSlab(page)) 117 slab++; 118 else if (!page_count(page)) 119 free++; 120 else 121 shared += page_count(page) - 1; 122 page++; 123 } while (page < end); 124 } 125 126 printk("%d pages of RAM\n", total); 127 printk("%d free pages\n", free); 128 printk("%d reserved pages\n", reserved); 129 printk("%d slab pages\n", slab); 130 printk("%d pages shared\n", shared); 131 printk("%d pages swap cached\n", cached); 132 } 133 134 static void __init find_limits(unsigned long *min, unsigned long *max_low, 135 unsigned long *max_high) 136 { 137 struct meminfo *mi = &meminfo; 138 int i; 139 140 *min = -1UL; 141 *max_low = *max_high = 0; 142 143 for_each_bank (i, mi) { 144 struct membank *bank = &mi->bank[i]; 145 unsigned long start, end; 146 147 start = bank_pfn_start(bank); 148 end = bank_pfn_end(bank); 149 150 if (*min > start) 151 *min = start; 152 if (*max_high < end) 153 *max_high = end; 154 if (bank->highmem) 155 continue; 156 if (*max_low < end) 157 *max_low = end; 158 } 159 } 160 161 static void __init arm_bootmem_init(unsigned long start_pfn, 162 unsigned long end_pfn) 163 { 164 struct memblock_region *reg; 165 unsigned int boot_pages; 166 phys_addr_t bitmap; 167 pg_data_t *pgdat; 168 169 /* 170 * Allocate the bootmem bitmap page. This must be in a region 171 * of memory which has already been mapped. 172 */ 173 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn); 174 bitmap = memblock_alloc_base(boot_pages << PAGE_SHIFT, L1_CACHE_BYTES, 175 __pfn_to_phys(end_pfn)); 176 177 /* 178 * Initialise the bootmem allocator, handing the 179 * memory banks over to bootmem. 180 */ 181 node_set_online(0); 182 pgdat = NODE_DATA(0); 183 init_bootmem_node(pgdat, __phys_to_pfn(bitmap), start_pfn, end_pfn); 184 185 /* Free the lowmem regions from memblock into bootmem. */ 186 for_each_memblock(memory, reg) { 187 unsigned long start = memblock_region_memory_base_pfn(reg); 188 unsigned long end = memblock_region_memory_end_pfn(reg); 189 190 if (end >= end_pfn) 191 end = end_pfn; 192 if (start >= end) 193 break; 194 195 free_bootmem(__pfn_to_phys(start), (end - start) << PAGE_SHIFT); 196 } 197 198 /* Reserve the lowmem memblock reserved regions in bootmem. */ 199 for_each_memblock(reserved, reg) { 200 unsigned long start = memblock_region_reserved_base_pfn(reg); 201 unsigned long end = memblock_region_reserved_end_pfn(reg); 202 203 if (end >= end_pfn) 204 end = end_pfn; 205 if (start >= end) 206 break; 207 208 reserve_bootmem(__pfn_to_phys(start), 209 (end - start) << PAGE_SHIFT, BOOTMEM_DEFAULT); 210 } 211 } 212 213 static void __init arm_bootmem_free(unsigned long min, unsigned long max_low, 214 unsigned long max_high) 215 { 216 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; 217 struct memblock_region *reg; 218 219 /* 220 * initialise the zones. 221 */ 222 memset(zone_size, 0, sizeof(zone_size)); 223 224 /* 225 * The memory size has already been determined. If we need 226 * to do anything fancy with the allocation of this memory 227 * to the zones, now is the time to do it. 228 */ 229 zone_size[0] = max_low - min; 230 #ifdef CONFIG_HIGHMEM 231 zone_size[ZONE_HIGHMEM] = max_high - max_low; 232 #endif 233 234 /* 235 * Calculate the size of the holes. 236 * holes = node_size - sum(bank_sizes) 237 */ 238 memcpy(zhole_size, zone_size, sizeof(zhole_size)); 239 for_each_memblock(memory, reg) { 240 unsigned long start = memblock_region_memory_base_pfn(reg); 241 unsigned long end = memblock_region_memory_end_pfn(reg); 242 243 if (start < max_low) { 244 unsigned long low_end = min(end, max_low); 245 zhole_size[0] -= low_end - start; 246 } 247 #ifdef CONFIG_HIGHMEM 248 if (end > max_low) { 249 unsigned long high_start = max(start, max_low); 250 zhole_size[ZONE_HIGHMEM] -= end - high_start; 251 } 252 #endif 253 } 254 255 /* 256 * Adjust the sizes according to any special requirements for 257 * this machine type. 258 */ 259 arch_adjust_zones(zone_size, zhole_size); 260 261 free_area_init_node(0, zone_size, min, zhole_size); 262 } 263 264 #ifndef CONFIG_SPARSEMEM 265 int pfn_valid(unsigned long pfn) 266 { 267 return memblock_is_memory(pfn << PAGE_SHIFT); 268 } 269 EXPORT_SYMBOL(pfn_valid); 270 271 static void arm_memory_present(void) 272 { 273 } 274 #else 275 static void arm_memory_present(void) 276 { 277 struct memblock_region *reg; 278 279 for_each_memblock(memory, reg) 280 memory_present(0, memblock_region_memory_base_pfn(reg), 281 memblock_region_memory_end_pfn(reg)); 282 } 283 #endif 284 285 static int __init meminfo_cmp(const void *_a, const void *_b) 286 { 287 const struct membank *a = _a, *b = _b; 288 long cmp = bank_pfn_start(a) - bank_pfn_start(b); 289 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0; 290 } 291 292 void __init arm_memblock_init(struct meminfo *mi, struct machine_desc *mdesc) 293 { 294 int i; 295 296 sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]), meminfo_cmp, NULL); 297 298 memblock_init(); 299 for (i = 0; i < mi->nr_banks; i++) 300 memblock_add(mi->bank[i].start, mi->bank[i].size); 301 302 /* Register the kernel text, kernel data and initrd with memblock. */ 303 #ifdef CONFIG_XIP_KERNEL 304 memblock_reserve(__pa(_sdata), _end - _sdata); 305 #else 306 memblock_reserve(__pa(_stext), _end - _stext); 307 #endif 308 #ifdef CONFIG_BLK_DEV_INITRD 309 if (phys_initrd_size && 310 memblock_is_region_reserved(phys_initrd_start, phys_initrd_size)) { 311 pr_err("INITRD: 0x%08lx+0x%08lx overlaps in-use memory region - disabling initrd\n", 312 phys_initrd_start, phys_initrd_size); 313 phys_initrd_start = phys_initrd_size = 0; 314 } 315 if (phys_initrd_size) { 316 memblock_reserve(phys_initrd_start, phys_initrd_size); 317 318 /* Now convert initrd to virtual addresses */ 319 initrd_start = __phys_to_virt(phys_initrd_start); 320 initrd_end = initrd_start + phys_initrd_size; 321 } 322 #endif 323 324 arm_mm_memblock_reserve(); 325 326 /* reserve any platform specific memblock areas */ 327 if (mdesc->reserve) 328 mdesc->reserve(); 329 330 memblock_analyze(); 331 memblock_dump_all(); 332 } 333 334 void __init bootmem_init(void) 335 { 336 unsigned long min, max_low, max_high; 337 338 max_low = max_high = 0; 339 340 find_limits(&min, &max_low, &max_high); 341 342 arm_bootmem_init(min, max_low); 343 344 /* 345 * Sparsemem tries to allocate bootmem in memory_present(), 346 * so must be done after the fixed reservations 347 */ 348 arm_memory_present(); 349 350 /* 351 * sparse_init() needs the bootmem allocator up and running. 352 */ 353 sparse_init(); 354 355 /* 356 * Now free the memory - free_area_init_node needs 357 * the sparse mem_map arrays initialized by sparse_init() 358 * for memmap_init_zone(), otherwise all PFNs are invalid. 359 */ 360 arm_bootmem_free(min, max_low, max_high); 361 362 high_memory = __va(((phys_addr_t)max_low << PAGE_SHIFT) - 1) + 1; 363 364 /* 365 * This doesn't seem to be used by the Linux memory manager any 366 * more, but is used by ll_rw_block. If we can get rid of it, we 367 * also get rid of some of the stuff above as well. 368 * 369 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in 370 * the system, not the maximum PFN. 371 */ 372 max_low_pfn = max_low - PHYS_PFN_OFFSET; 373 max_pfn = max_high - PHYS_PFN_OFFSET; 374 } 375 376 static inline int free_area(unsigned long pfn, unsigned long end, char *s) 377 { 378 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10); 379 380 for (; pfn < end; pfn++) { 381 struct page *page = pfn_to_page(pfn); 382 ClearPageReserved(page); 383 init_page_count(page); 384 __free_page(page); 385 pages++; 386 } 387 388 if (size && s) 389 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size); 390 391 return pages; 392 } 393 394 static inline void 395 free_memmap(unsigned long start_pfn, unsigned long end_pfn) 396 { 397 struct page *start_pg, *end_pg; 398 unsigned long pg, pgend; 399 400 /* 401 * Convert start_pfn/end_pfn to a struct page pointer. 402 */ 403 start_pg = pfn_to_page(start_pfn - 1) + 1; 404 end_pg = pfn_to_page(end_pfn); 405 406 /* 407 * Convert to physical addresses, and 408 * round start upwards and end downwards. 409 */ 410 pg = (unsigned long)PAGE_ALIGN(__pa(start_pg)); 411 pgend = (unsigned long)__pa(end_pg) & PAGE_MASK; 412 413 /* 414 * If there are free pages between these, 415 * free the section of the memmap array. 416 */ 417 if (pg < pgend) 418 free_bootmem(pg, pgend - pg); 419 } 420 421 /* 422 * The mem_map array can get very big. Free the unused area of the memory map. 423 */ 424 static void __init free_unused_memmap(struct meminfo *mi) 425 { 426 unsigned long bank_start, prev_bank_end = 0; 427 unsigned int i; 428 429 /* 430 * This relies on each bank being in address order. 431 * The banks are sorted previously in bootmem_init(). 432 */ 433 for_each_bank(i, mi) { 434 struct membank *bank = &mi->bank[i]; 435 436 bank_start = bank_pfn_start(bank); 437 438 /* 439 * If we had a previous bank, and there is a space 440 * between the current bank and the previous, free it. 441 */ 442 if (prev_bank_end && prev_bank_end < bank_start) 443 free_memmap(prev_bank_end, bank_start); 444 445 /* 446 * Align up here since the VM subsystem insists that the 447 * memmap entries are valid from the bank end aligned to 448 * MAX_ORDER_NR_PAGES. 449 */ 450 prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES); 451 } 452 } 453 454 static void __init free_highpages(void) 455 { 456 #ifdef CONFIG_HIGHMEM 457 unsigned long max_low = max_low_pfn + PHYS_PFN_OFFSET; 458 struct memblock_region *mem, *res; 459 460 /* set highmem page free */ 461 for_each_memblock(memory, mem) { 462 unsigned long start = memblock_region_memory_base_pfn(mem); 463 unsigned long end = memblock_region_memory_end_pfn(mem); 464 465 /* Ignore complete lowmem entries */ 466 if (end <= max_low) 467 continue; 468 469 /* Truncate partial highmem entries */ 470 if (start < max_low) 471 start = max_low; 472 473 /* Find and exclude any reserved regions */ 474 for_each_memblock(reserved, res) { 475 unsigned long res_start, res_end; 476 477 res_start = memblock_region_reserved_base_pfn(res); 478 res_end = memblock_region_reserved_end_pfn(res); 479 480 if (res_end < start) 481 continue; 482 if (res_start < start) 483 res_start = start; 484 if (res_start > end) 485 res_start = end; 486 if (res_end > end) 487 res_end = end; 488 if (res_start != start) 489 totalhigh_pages += free_area(start, res_start, 490 NULL); 491 start = res_end; 492 if (start == end) 493 break; 494 } 495 496 /* And now free anything which remains */ 497 if (start < end) 498 totalhigh_pages += free_area(start, end, NULL); 499 } 500 totalram_pages += totalhigh_pages; 501 #endif 502 } 503 504 /* 505 * mem_init() marks the free areas in the mem_map and tells us how much 506 * memory is free. This is done after various parts of the system have 507 * claimed their memory after the kernel image. 508 */ 509 void __init mem_init(void) 510 { 511 unsigned long reserved_pages, free_pages; 512 struct memblock_region *reg; 513 int i; 514 #ifdef CONFIG_HAVE_TCM 515 /* These pointers are filled in on TCM detection */ 516 extern u32 dtcm_end; 517 extern u32 itcm_end; 518 #endif 519 520 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map; 521 522 /* this will put all unused low memory onto the freelists */ 523 free_unused_memmap(&meminfo); 524 525 totalram_pages += free_all_bootmem(); 526 527 #ifdef CONFIG_SA1111 528 /* now that our DMA memory is actually so designated, we can free it */ 529 totalram_pages += free_area(PHYS_PFN_OFFSET, 530 __phys_to_pfn(__pa(swapper_pg_dir)), NULL); 531 #endif 532 533 free_highpages(); 534 535 reserved_pages = free_pages = 0; 536 537 for_each_bank(i, &meminfo) { 538 struct membank *bank = &meminfo.bank[i]; 539 unsigned int pfn1, pfn2; 540 struct page *page, *end; 541 542 pfn1 = bank_pfn_start(bank); 543 pfn2 = bank_pfn_end(bank); 544 545 page = pfn_to_page(pfn1); 546 end = pfn_to_page(pfn2 - 1) + 1; 547 548 do { 549 if (PageReserved(page)) 550 reserved_pages++; 551 else if (!page_count(page)) 552 free_pages++; 553 page++; 554 } while (page < end); 555 } 556 557 /* 558 * Since our memory may not be contiguous, calculate the 559 * real number of pages we have in this system 560 */ 561 printk(KERN_INFO "Memory:"); 562 num_physpages = 0; 563 for_each_memblock(memory, reg) { 564 unsigned long pages = memblock_region_memory_end_pfn(reg) - 565 memblock_region_memory_base_pfn(reg); 566 num_physpages += pages; 567 printk(" %ldMB", pages >> (20 - PAGE_SHIFT)); 568 } 569 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT)); 570 571 printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n", 572 nr_free_pages() << (PAGE_SHIFT-10), 573 free_pages << (PAGE_SHIFT-10), 574 reserved_pages << (PAGE_SHIFT-10), 575 totalhigh_pages << (PAGE_SHIFT-10)); 576 577 #define MLK(b, t) b, t, ((t) - (b)) >> 10 578 #define MLM(b, t) b, t, ((t) - (b)) >> 20 579 #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K) 580 581 printk(KERN_NOTICE "Virtual kernel memory layout:\n" 582 " vector : 0x%08lx - 0x%08lx (%4ld kB)\n" 583 #ifdef CONFIG_HAVE_TCM 584 " DTCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 585 " ITCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 586 #endif 587 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n" 588 #ifdef CONFIG_MMU 589 " DMA : 0x%08lx - 0x%08lx (%4ld MB)\n" 590 #endif 591 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n" 592 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n" 593 #ifdef CONFIG_HIGHMEM 594 " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n" 595 #endif 596 " modules : 0x%08lx - 0x%08lx (%4ld MB)\n" 597 " .init : 0x%p" " - 0x%p" " (%4d kB)\n" 598 " .text : 0x%p" " - 0x%p" " (%4d kB)\n" 599 " .data : 0x%p" " - 0x%p" " (%4d kB)\n", 600 601 MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) + 602 (PAGE_SIZE)), 603 #ifdef CONFIG_HAVE_TCM 604 MLK(DTCM_OFFSET, (unsigned long) dtcm_end), 605 MLK(ITCM_OFFSET, (unsigned long) itcm_end), 606 #endif 607 MLK(FIXADDR_START, FIXADDR_TOP), 608 #ifdef CONFIG_MMU 609 MLM(CONSISTENT_BASE, CONSISTENT_END), 610 #endif 611 MLM(VMALLOC_START, VMALLOC_END), 612 MLM(PAGE_OFFSET, (unsigned long)high_memory), 613 #ifdef CONFIG_HIGHMEM 614 MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) * 615 (PAGE_SIZE)), 616 #endif 617 MLM(MODULES_VADDR, MODULES_END), 618 619 MLK_ROUNDUP(__init_begin, __init_end), 620 MLK_ROUNDUP(_text, _etext), 621 MLK_ROUNDUP(_sdata, _edata)); 622 623 #undef MLK 624 #undef MLM 625 #undef MLK_ROUNDUP 626 627 /* 628 * Check boundaries twice: Some fundamental inconsistencies can 629 * be detected at build time already. 630 */ 631 #ifdef CONFIG_MMU 632 BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE); 633 BUG_ON(VMALLOC_END > CONSISTENT_BASE); 634 635 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR); 636 BUG_ON(TASK_SIZE > MODULES_VADDR); 637 #endif 638 639 #ifdef CONFIG_HIGHMEM 640 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 641 BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 642 #endif 643 644 if (PAGE_SIZE >= 16384 && num_physpages <= 128) { 645 extern int sysctl_overcommit_memory; 646 /* 647 * On a machine this small we won't get 648 * anywhere without overcommit, so turn 649 * it on by default. 650 */ 651 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; 652 } 653 } 654 655 void free_initmem(void) 656 { 657 #ifdef CONFIG_HAVE_TCM 658 extern char __tcm_start, __tcm_end; 659 660 totalram_pages += free_area(__phys_to_pfn(__pa(&__tcm_start)), 661 __phys_to_pfn(__pa(&__tcm_end)), 662 "TCM link"); 663 #endif 664 665 if (!machine_is_integrator() && !machine_is_cintegrator()) 666 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)), 667 __phys_to_pfn(__pa(__init_end)), 668 "init"); 669 } 670 671 #ifdef CONFIG_BLK_DEV_INITRD 672 673 static int keep_initrd; 674 675 void free_initrd_mem(unsigned long start, unsigned long end) 676 { 677 if (!keep_initrd) 678 totalram_pages += free_area(__phys_to_pfn(__pa(start)), 679 __phys_to_pfn(__pa(end)), 680 "initrd"); 681 } 682 683 static int __init keepinitrd_setup(char *__unused) 684 { 685 keep_initrd = 1; 686 return 1; 687 } 688 689 __setup("keepinitrd", keepinitrd_setup); 690 #endif 691