blob: 7829cb5425f56e460a62390158114db98f4acc10 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mm/init.c
3 *
Russell King90072052005-10-28 14:48:37 +01004 * Copyright (C) 1995-2005 Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
11#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Russell Kingb7cfda92009-09-07 15:06:42 +010018#include <linux/sort.h>
Nicolas Pitre3835f6c2008-09-17 15:21:55 -040019#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/mach-types.h>
Russell King37efe642008-12-01 11:53:07 +000022#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/setup.h>
Russell King74d02fb2006-04-04 21:47:43 +010024#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/tlb.h>
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +010026#include <asm/fixmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/mach/arch.h>
29#include <asm/mach/map.h>
30
Russell King1b2e2b72006-08-21 17:06:38 +010031#include "mm.h"
32
Russell King012d1f42008-09-06 10:57:03 +010033static unsigned long phys_initrd_start __initdata = 0;
34static unsigned long phys_initrd_size __initdata = 0;
35
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010036static int __init early_initrd(char *p)
Russell King012d1f42008-09-06 10:57:03 +010037{
38 unsigned long start, size;
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010039 char *endp;
Russell King012d1f42008-09-06 10:57:03 +010040
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010041 start = memparse(p, &endp);
42 if (*endp == ',') {
43 size = memparse(endp + 1, NULL);
Russell King012d1f42008-09-06 10:57:03 +010044
45 phys_initrd_start = start;
46 phys_initrd_size = size;
47 }
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010048 return 0;
Russell King012d1f42008-09-06 10:57:03 +010049}
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010050early_param("initrd", early_initrd);
Russell King012d1f42008-09-06 10:57:03 +010051
52static 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
63static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040073 * 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().
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 */
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040077struct meminfo meminfo;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079void show_mem(void)
80{
81 int free = 0, total = 0, reserved = 0;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010082 int shared = 0, cached = 0, slab = 0, node, i;
83 struct meminfo * mi = &meminfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 printk("Mem-info:\n");
86 show_free_areas();
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 for_each_online_node(node) {
Russell King204ecae2007-01-16 14:01:47 +000088 pg_data_t *n = NODE_DATA(node);
Russell Kingb7a69ac2008-10-01 16:58:32 +010089 struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn;
Russell King204ecae2007-01-16 14:01:47 +000090
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010091 for_each_nodebank (i,mi,node) {
Russell Kingd2a38ef2008-10-01 16:56:15 +010092 struct membank *bank = &mi->bank[i];
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010093 unsigned int pfn1, pfn2;
94 struct page *page, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Russell Kingd2a38ef2008-10-01 16:56:15 +010096 pfn1 = bank_pfn_start(bank);
97 pfn2 = bank_pfn_end(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Russell King204ecae2007-01-16 14:01:47 +000099 page = map + pfn1;
100 end = map + pfn2;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +0100101
102 do {
103 total++;
104 if (PageReserved(page))
105 reserved++;
106 else if (PageSwapCache(page))
107 cached++;
108 else if (PageSlab(page))
109 slab++;
110 else if (!page_count(page))
111 free++;
112 else
113 shared += page_count(page) - 1;
114 page++;
115 } while (page < end);
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118
119 printk("%d pages of RAM\n", total);
120 printk("%d free pages\n", free);
121 printk("%d reserved pages\n", reserved);
122 printk("%d slab pages\n", slab);
123 printk("%d pages shared\n", shared);
124 printk("%d pages swap cached\n", cached);
125}
126
Russell Kingdde58282009-08-15 12:36:00 +0100127static void __init find_node_limits(int node, struct meminfo *mi,
128 unsigned long *min, unsigned long *max_low, unsigned long *max_high)
129{
130 int i;
131
132 *min = -1UL;
133 *max_low = *max_high = 0;
134
135 for_each_nodebank(i, mi, node) {
136 struct membank *bank = &mi->bank[i];
137 unsigned long start, end;
138
139 start = bank_pfn_start(bank);
140 end = bank_pfn_end(bank);
141
142 if (*min > start)
143 *min = start;
144 if (*max_high < end)
145 *max_high = end;
146 if (bank->highmem)
147 continue;
148 if (*max_low < end)
149 *max_low = end;
150 }
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * FIXME: We really want to avoid allocating the bootmap bitmap
155 * over the top of the initrd. Hopefully, this is located towards
156 * the start of a bank, so if we allocate the bootmap bitmap at
157 * the end, we won't clash.
158 */
159static unsigned int __init
160find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
161{
Russell Kingd2a38ef2008-10-01 16:56:15 +0100162 unsigned int start_pfn, i, bootmap_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Russell King37efe642008-12-01 11:53:07 +0000164 start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 bootmap_pfn = 0;
166
Russell Kingd2a38ef2008-10-01 16:56:15 +0100167 for_each_nodebank(i, mi, node) {
168 struct membank *bank = &mi->bank[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned int start, end;
170
Russell Kingd2a38ef2008-10-01 16:56:15 +0100171 start = bank_pfn_start(bank);
172 end = bank_pfn_end(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (end < start_pfn)
175 continue;
176
177 if (start < start_pfn)
178 start = start_pfn;
179
180 if (end <= start)
181 continue;
182
183 if (end - start >= bootmap_pages) {
184 bootmap_pfn = start;
185 break;
186 }
187 }
188
189 if (bootmap_pfn == 0)
190 BUG();
191
192 return bootmap_pfn;
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int __init check_initrd(struct meminfo *mi)
196{
197 int initrd_node = -2;
198#ifdef CONFIG_BLK_DEV_INITRD
199 unsigned long end = phys_initrd_start + phys_initrd_size;
200
201 /*
202 * Make sure that the initrd is within a valid area of
203 * memory.
204 */
205 if (phys_initrd_size) {
206 unsigned int i;
207
208 initrd_node = -1;
209
210 for (i = 0; i < mi->nr_banks; i++) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100211 struct membank *bank = &mi->bank[i];
212 if (bank_phys_start(bank) <= phys_initrd_start &&
213 end <= bank_phys_end(bank))
214 initrd_node = bank->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 }
217
218 if (initrd_node == -1) {
Russell Kingb962a282008-07-30 21:24:56 +0100219 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 "physical memory - disabling initrd\n",
Russell Kingb962a282008-07-30 21:24:56 +0100221 phys_initrd_start, phys_initrd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 phys_initrd_start = phys_initrd_size = 0;
223 }
224#endif
225
226 return initrd_node;
227}
228
Russell King456335e2006-09-27 10:00:54 +0100229static inline void map_memory_bank(struct membank *bank)
230{
Russell Kingd111e8f2006-09-27 15:27:33 +0100231#ifdef CONFIG_MMU
Russell King456335e2006-09-27 10:00:54 +0100232 struct map_desc map;
233
Russell Kingd2a38ef2008-10-01 16:56:15 +0100234 map.pfn = bank_pfn_start(bank);
235 map.virtual = __phys_to_virt(bank_phys_start(bank));
236 map.length = bank_phys_size(bank);
Russell King456335e2006-09-27 10:00:54 +0100237 map.type = MT_MEMORY;
238
239 create_mapping(&map);
Russell Kingd111e8f2006-09-27 15:27:33 +0100240#endif
Russell King456335e2006-09-27 10:00:54 +0100241}
242
Russell Kingdde58282009-08-15 12:36:00 +0100243static void __init bootmem_init_node(int node, struct meminfo *mi,
244 unsigned long start_pfn, unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Russell Kingdde58282009-08-15 12:36:00 +0100246 unsigned long boot_pfn;
Russell King90072052005-10-28 14:48:37 +0100247 unsigned int boot_pages;
248 pg_data_t *pgdat;
249 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /*
Russell Kingdde58282009-08-15 12:36:00 +0100252 * Map the memory banks for this node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
Russell King90072052005-10-28 14:48:37 +0100254 for_each_nodebank(i, mi, node) {
Russell King456335e2006-09-27 10:00:54 +0100255 struct membank *bank = &mi->bank[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Russell Kingdde58282009-08-15 12:36:00 +0100257 if (!bank->highmem)
258 map_memory_bank(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Russell King90072052005-10-28 14:48:37 +0100261 /*
Russell King90072052005-10-28 14:48:37 +0100262 * Allocate the bootmem bitmap page.
263 */
264 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
265 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
266
267 /*
268 * Initialise the bootmem allocator for this node, handing the
269 * memory banks over to bootmem.
270 */
271 node_set_online(node);
272 pgdat = NODE_DATA(node);
273 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
274
Russell Kingd2a38ef2008-10-01 16:56:15 +0100275 for_each_nodebank(i, mi, node) {
276 struct membank *bank = &mi->bank[i];
Russell Kingdde58282009-08-15 12:36:00 +0100277 if (!bank->highmem)
278 free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
Russell Kingd2a38ef2008-10-01 16:56:15 +0100279 }
Russell King90072052005-10-28 14:48:37 +0100280
281 /*
282 * Reserve the bootmem bitmap for this node.
283 */
284 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800285 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
Russell Kingb7a69ac2008-10-01 16:58:32 +0100286}
Russell Kingb962a282008-07-30 21:24:56 +0100287
Russell Kingb7a69ac2008-10-01 16:58:32 +0100288static void __init bootmem_reserve_initrd(int node)
289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#ifdef CONFIG_BLK_DEV_INITRD
Russell Kingb7a69ac2008-10-01 16:58:32 +0100291 pg_data_t *pgdat = NODE_DATA(node);
292 int res;
Russell Kingb962a282008-07-30 21:24:56 +0100293
Russell Kingb7a69ac2008-10-01 16:58:32 +0100294 res = reserve_bootmem_node(pgdat, phys_initrd_start,
295 phys_initrd_size, BOOTMEM_EXCLUSIVE);
296
297 if (res == 0) {
298 initrd_start = __phys_to_virt(phys_initrd_start);
299 initrd_end = initrd_start + phys_initrd_size;
300 } else {
301 printk(KERN_ERR
302 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
303 "memory region - disabling initrd\n",
304 phys_initrd_start, phys_initrd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306#endif
Russell Kingb7a69ac2008-10-01 16:58:32 +0100307}
308
309static void __init bootmem_free_node(int node, struct meminfo *mi)
310{
311 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
Russell Kingdde58282009-08-15 12:36:00 +0100312 unsigned long min, max_low, max_high;
Russell Kingb7a69ac2008-10-01 16:58:32 +0100313 int i;
314
Russell Kingdde58282009-08-15 12:36:00 +0100315 find_node_limits(node, mi, &min, &max_low, &max_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Russell King90072052005-10-28 14:48:37 +0100317 /*
Russell King90072052005-10-28 14:48:37 +0100318 * initialise the zones within this node.
319 */
320 memset(zone_size, 0, sizeof(zone_size));
Russell King90072052005-10-28 14:48:37 +0100321
322 /*
323 * The size of this node has already been determined. If we need
324 * to do anything fancy with the allocation of this memory to the
325 * zones, now is the time to do it.
326 */
Russell Kingdde58282009-08-15 12:36:00 +0100327 zone_size[0] = max_low - min;
328#ifdef CONFIG_HIGHMEM
329 zone_size[ZONE_HIGHMEM] = max_high - max_low;
330#endif
Russell King90072052005-10-28 14:48:37 +0100331
332 /*
333 * For each bank in this node, calculate the size of the holes.
334 * holes = node_size - sum(bank_sizes_in_node)
335 */
Russell Kingdde58282009-08-15 12:36:00 +0100336 memcpy(zhole_size, zone_size, sizeof(zhole_size));
337 for_each_nodebank(i, mi, node) {
338 int idx = 0;
339#ifdef CONFIG_HIGHMEM
340 if (mi->bank[i].highmem)
341 idx = ZONE_HIGHMEM;
342#endif
343 zhole_size[idx] -= bank_pfn_size(&mi->bank[i]);
344 }
Russell King90072052005-10-28 14:48:37 +0100345
346 /*
347 * Adjust the sizes according to any special requirements for
348 * this machine type.
349 */
350 arch_adjust_zones(node, zone_size, zhole_size);
351
Russell Kingdde58282009-08-15 12:36:00 +0100352 free_area_init_node(node, zone_size, min, zhole_size);
Russell King90072052005-10-28 14:48:37 +0100353}
354
Russell Kingb7cfda92009-09-07 15:06:42 +0100355#ifndef CONFIG_SPARSEMEM
356int pfn_valid(unsigned long pfn)
357{
358 struct meminfo *mi = &meminfo;
359 unsigned int left = 0, right = mi->nr_banks;
360
361 do {
362 unsigned int mid = (right + left) / 2;
363 struct membank *bank = &mi->bank[mid];
364
365 if (pfn < bank_pfn_start(bank))
366 right = mid;
367 else if (pfn >= bank_pfn_end(bank))
368 left = mid + 1;
369 else
370 return 1;
371 } while (left < right);
372 return 0;
373}
374EXPORT_SYMBOL(pfn_valid);
Russell King657e12f2009-10-29 17:06:17 +0000375
376static void arm_memory_present(struct meminfo *mi, int node)
377{
378}
379#else
380static void arm_memory_present(struct meminfo *mi, int node)
381{
382 int i;
383 for_each_nodebank(i, mi, node) {
384 struct membank *bank = &mi->bank[i];
385 memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
386 }
387}
Russell Kingb7cfda92009-09-07 15:06:42 +0100388#endif
389
390static int __init meminfo_cmp(const void *_a, const void *_b)
391{
392 const struct membank *a = _a, *b = _b;
393 long cmp = bank_pfn_start(a) - bank_pfn_start(b);
394 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
395}
396
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -0400397void __init bootmem_init(void)
Russell King90072052005-10-28 14:48:37 +0100398{
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -0400399 struct meminfo *mi = &meminfo;
Russell Kingdde58282009-08-15 12:36:00 +0100400 unsigned long min, max_low, max_high;
Russell Kingeca73212008-09-30 19:29:25 +0100401 int node, initrd_node;
Russell King90072052005-10-28 14:48:37 +0100402
Russell Kingb7cfda92009-09-07 15:06:42 +0100403 sort(&mi->bank, mi->nr_banks, sizeof(mi->bank[0]), meminfo_cmp, NULL);
404
Russell King90072052005-10-28 14:48:37 +0100405 /*
406 * Locate which node contains the ramdisk image, if any.
407 */
408 initrd_node = check_initrd(mi);
409
Russell Kingdde58282009-08-15 12:36:00 +0100410 max_low = max_high = 0;
411
Russell King90072052005-10-28 14:48:37 +0100412 /*
413 * Run through each node initialising the bootmem allocator.
414 */
415 for_each_node(node) {
Russell Kingdde58282009-08-15 12:36:00 +0100416 unsigned long node_low, node_high;
417
418 find_node_limits(node, mi, &min, &node_low, &node_high);
419
420 if (node_low > max_low)
421 max_low = node_low;
422 if (node_high > max_high)
423 max_high = node_high;
424
425 /*
426 * If there is no memory in this node, ignore it.
427 * (We can't have nodes which have no lowmem)
428 */
429 if (node_low == 0)
430 continue;
431
432 bootmem_init_node(node, mi, min, node_low);
Russell King90072052005-10-28 14:48:37 +0100433
Russell Kingb7a69ac2008-10-01 16:58:32 +0100434 /*
435 * Reserve any special node zero regions.
436 */
437 if (node == 0)
438 reserve_node_zero(NODE_DATA(node));
439
440 /*
441 * If the initrd is in this node, reserve its memory.
442 */
443 if (node == initrd_node)
444 bootmem_reserve_initrd(node);
Russell King657e12f2009-10-29 17:06:17 +0000445
446 /*
447 * Sparsemem tries to allocate bootmem in memory_present(),
448 * so must be done after the fixed reservations
449 */
450 arm_memory_present(mi, node);
Russell King90072052005-10-28 14:48:37 +0100451 }
452
Russell Kingb7a69ac2008-10-01 16:58:32 +0100453 /*
454 * sparse_init() needs the bootmem allocator up and running.
455 */
456 sparse_init();
457
458 /*
459 * Now free memory in each node - free_area_init_node needs
460 * the sparse mem_map arrays initialized by sparse_init()
461 * for memmap_init_zone(), otherwise all PFNs are invalid.
462 */
463 for_each_node(node)
464 bootmem_free_node(node, mi);
465
Russell Kingdde58282009-08-15 12:36:00 +0100466 high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
Russell King90072052005-10-28 14:48:37 +0100467
468 /*
469 * This doesn't seem to be used by the Linux memory manager any
470 * more, but is used by ll_rw_block. If we can get rid of it, we
471 * also get rid of some of the stuff above as well.
472 *
473 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
474 * the system, not the maximum PFN.
475 */
Russell Kingdde58282009-08-15 12:36:00 +0100476 max_low_pfn = max_low - PHYS_PFN_OFFSET;
477 max_pfn = max_high - PHYS_PFN_OFFSET;
Russell King90072052005-10-28 14:48:37 +0100478}
479
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400480static inline int free_area(unsigned long pfn, unsigned long end, char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400482 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400484 for (; pfn < end; pfn++) {
485 struct page *page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800487 init_page_count(page);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400488 __free_page(page);
489 pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 if (size && s)
493 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400494
495 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Russell Kinga0130532005-06-27 14:16:47 +0100498static inline void
499free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
500{
501 struct page *start_pg, *end_pg;
502 unsigned long pg, pgend;
503
504 /*
505 * Convert start_pfn/end_pfn to a struct page pointer.
506 */
Catalin Marinas3257f432009-10-06 17:57:22 +0100507 start_pg = pfn_to_page(start_pfn - 1) + 1;
Russell Kinga0130532005-06-27 14:16:47 +0100508 end_pg = pfn_to_page(end_pfn);
509
510 /*
511 * Convert to physical addresses, and
512 * round start upwards and end downwards.
513 */
514 pg = PAGE_ALIGN(__pa(start_pg));
515 pgend = __pa(end_pg) & PAGE_MASK;
516
517 /*
518 * If there are free pages between these,
519 * free the section of the memmap array.
520 */
521 if (pg < pgend)
522 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
523}
524
525/*
526 * The mem_map array can get very big. Free the unused area of the memory map.
527 */
528static void __init free_unused_memmap_node(int node, struct meminfo *mi)
529{
530 unsigned long bank_start, prev_bank_end = 0;
531 unsigned int i;
532
533 /*
534 * [FIXME] This relies on each bank being in address order. This
535 * may not be the case, especially if the user has provided the
536 * information on the command line.
537 */
Russell King90072052005-10-28 14:48:37 +0100538 for_each_nodebank(i, mi, node) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100539 struct membank *bank = &mi->bank[i];
540
541 bank_start = bank_pfn_start(bank);
Russell Kinga0130532005-06-27 14:16:47 +0100542 if (bank_start < prev_bank_end) {
543 printk(KERN_ERR "MEM: unordered memory banks. "
544 "Not freeing memmap.\n");
545 break;
546 }
547
548 /*
549 * If we had a previous bank, and there is a space
550 * between the current bank and the previous, free it.
551 */
552 if (prev_bank_end && prev_bank_end != bank_start)
553 free_memmap(node, prev_bank_end, bank_start);
554
Russell Kingd2a38ef2008-10-01 16:56:15 +0100555 prev_bank_end = bank_pfn_end(bank);
Russell Kinga0130532005-06-27 14:16:47 +0100556 }
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/*
560 * mem_init() marks the free areas in the mem_map and tells us how much
561 * memory is free. This is done after various parts of the system have
562 * claimed their memory after the kernel image.
563 */
564void __init mem_init(void)
565{
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100566 unsigned long reserved_pages, free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int i, node;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#ifndef CONFIG_DISCONTIGMEM
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400570 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571#endif
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* this will put all unused low memory onto the freelists */
574 for_each_online_node(node) {
575 pg_data_t *pgdat = NODE_DATA(node);
576
Russell Kinga0130532005-06-27 14:16:47 +0100577 free_unused_memmap_node(node, &meminfo);
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (pgdat->node_spanned_pages != 0)
580 totalram_pages += free_all_bootmem_node(pgdat);
581 }
582
583#ifdef CONFIG_SA1111
584 /* now that our DMA memory is actually so designated, we can free it */
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400585 totalram_pages += free_area(PHYS_PFN_OFFSET,
586 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#endif
588
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400589#ifdef CONFIG_HIGHMEM
590 /* set highmem page free */
591 for_each_online_node(node) {
592 for_each_nodebank (i, &meminfo, node) {
593 unsigned long start = bank_pfn_start(&meminfo.bank[i]);
594 unsigned long end = bank_pfn_end(&meminfo.bank[i]);
595 if (start >= max_low_pfn + PHYS_PFN_OFFSET)
596 totalhigh_pages += free_area(start, end, NULL);
597 }
598 }
599 totalram_pages += totalhigh_pages;
600#endif
601
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100602 reserved_pages = free_pages = 0;
603
604 for_each_online_node(node) {
605 pg_data_t *n = NODE_DATA(node);
606 struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn;
607
608 for_each_nodebank(i, &meminfo, node) {
609 struct membank *bank = &meminfo.bank[i];
610 unsigned int pfn1, pfn2;
611 struct page *page, *end;
612
613 pfn1 = bank_pfn_start(bank);
614 pfn2 = bank_pfn_end(bank);
615
616 page = map + pfn1;
617 end = map + pfn2;
618
619 do {
620 if (PageReserved(page))
621 reserved_pages++;
622 else if (!page_count(page))
623 free_pages++;
624 page++;
625 } while (page < end);
626 }
627 }
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * Since our memory may not be contiguous, calculate the
631 * real number of pages we have in this system
632 */
633 printk(KERN_INFO "Memory:");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 num_physpages = 0;
635 for (i = 0; i < meminfo.nr_banks; i++) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100636 num_physpages += bank_pfn_size(&meminfo.bank[i]);
637 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400640
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100641 printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
642 nr_free_pages() << (PAGE_SHIFT-10),
643 free_pages << (PAGE_SHIFT-10),
644 reserved_pages << (PAGE_SHIFT-10),
Andreas Fenkart4b529402010-01-08 14:42:31 -0800645 totalhigh_pages << (PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100647#define MLK(b, t) b, t, ((t) - (b)) >> 10
648#define MLM(b, t) b, t, ((t) - (b)) >> 20
649#define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
650
651 printk(KERN_NOTICE "Virtual kernel memory layout:\n"
652 " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
653 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Fenkart/Bostandzhyana7bd08c2010-02-07 21:46:33 +0100654#ifdef CONFIG_MMU
655 " DMA : 0x%08lx - 0x%08lx (%4ld MB)\n"
656#endif
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100657 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
658 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
659#ifdef CONFIG_HIGHMEM
660 " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n"
661#endif
662 " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
663 " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
664 " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
665 " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
666
667 MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
668 (PAGE_SIZE)),
669 MLK(FIXADDR_START, FIXADDR_TOP),
Fenkart/Bostandzhyana7bd08c2010-02-07 21:46:33 +0100670#ifdef CONFIG_MMU
671 MLM(CONSISTENT_BASE, CONSISTENT_END),
672#endif
Fenkart/Bostandzhyanc931b4f2010-02-07 21:47:17 +0100673 MLM(VMALLOC_START, VMALLOC_END),
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100674 MLM(PAGE_OFFSET, (unsigned long)high_memory),
675#ifdef CONFIG_HIGHMEM
676 MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
677 (PAGE_SIZE)),
678#endif
679 MLM(MODULES_VADDR, MODULES_END),
680
681 MLK_ROUNDUP(__init_begin, __init_end),
682 MLK_ROUNDUP(_text, _etext),
683 MLK_ROUNDUP(_data, _edata));
684
685#undef MLK
686#undef MLM
687#undef MLK_ROUNDUP
688
Fenkart/Bostandzhyana1839272010-02-07 21:47:58 +0100689 /*
690 * Check boundaries twice: Some fundamental inconsistencies can
691 * be detected at build time already.
692 */
693#ifdef CONFIG_MMU
694 BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
695 BUG_ON(VMALLOC_END > CONSISTENT_BASE);
696
697 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
698 BUG_ON(TASK_SIZE > MODULES_VADDR);
699#endif
700
701#ifdef CONFIG_HIGHMEM
702 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
703 BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
704#endif
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
707 extern int sysctl_overcommit_memory;
708 /*
709 * On a machine this small we won't get
710 * anywhere without overcommit, so turn
711 * it on by default.
712 */
713 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
714 }
715}
716
717void free_initmem(void)
718{
Linus Walleijbc581772009-09-15 17:30:37 +0100719#ifdef CONFIG_HAVE_TCM
720 extern char *__tcm_start, *__tcm_end;
721
722 totalram_pages += free_area(__phys_to_pfn(__pa(__tcm_start)),
723 __phys_to_pfn(__pa(__tcm_end)),
724 "TCM link");
725#endif
726
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400727 if (!machine_is_integrator() && !machine_is_cintegrator())
Russell King37efe642008-12-01 11:53:07 +0000728 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
729 __phys_to_pfn(__pa(__init_end)),
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400730 "init");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733#ifdef CONFIG_BLK_DEV_INITRD
734
735static int keep_initrd;
736
737void free_initrd_mem(unsigned long start, unsigned long end)
738{
739 if (!keep_initrd)
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400740 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
741 __phys_to_pfn(__pa(end)),
742 "initrd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745static int __init keepinitrd_setup(char *__unused)
746{
747 keep_initrd = 1;
748 return 1;
749}
750
751__setup("keepinitrd", keepinitrd_setup);
752#endif