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