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