blob: a04ffbbbe2536309ee73442db96fd271a46561a4 [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>
26
27#include <asm/mach/arch.h>
28#include <asm/mach/map.h>
29
Russell King1b2e2b72006-08-21 17:06:38 +010030#include "mm.h"
31
Russell King012d1f42008-09-06 10:57:03 +010032static unsigned long phys_initrd_start __initdata = 0;
33static unsigned long phys_initrd_size __initdata = 0;
34
35static void __init early_initrd(char **p)
36{
37 unsigned long start, size;
38
39 start = memparse(*p, p);
40 if (**p == ',') {
41 size = memparse((*p) + 1, p);
42
43 phys_initrd_start = start;
44 phys_initrd_size = size;
45 }
46}
47__early_param("initrd=", early_initrd);
48
49static int __init parse_tag_initrd(const struct tag *tag)
50{
51 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
52 "please update your bootloader.\n");
53 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
54 phys_initrd_size = tag->u.initrd.size;
55 return 0;
56}
57
58__tagtable(ATAG_INITRD, parse_tag_initrd);
59
60static int __init parse_tag_initrd2(const struct tag *tag)
61{
62 phys_initrd_start = tag->u.initrd.start;
63 phys_initrd_size = tag->u.initrd.size;
64 return 0;
65}
66
67__tagtable(ATAG_INITRD2, parse_tag_initrd2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040070 * This keeps memory configuration data used by a couple memory
71 * initialization functions, as well as show_mem() for the skipping
72 * of holes in the memory map. It is populated by arm_add_memory().
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040074struct meminfo meminfo;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076void show_mem(void)
77{
78 int free = 0, total = 0, reserved = 0;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010079 int shared = 0, cached = 0, slab = 0, node, i;
80 struct meminfo * mi = &meminfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 printk("Mem-info:\n");
83 show_free_areas();
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 for_each_online_node(node) {
Russell King204ecae2007-01-16 14:01:47 +000085 pg_data_t *n = NODE_DATA(node);
Russell Kingb7a69ac2008-10-01 16:58:32 +010086 struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn;
Russell King204ecae2007-01-16 14:01:47 +000087
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010088 for_each_nodebank (i,mi,node) {
Russell Kingd2a38ef2008-10-01 16:56:15 +010089 struct membank *bank = &mi->bank[i];
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010090 unsigned int pfn1, pfn2;
91 struct page *page, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Russell Kingd2a38ef2008-10-01 16:56:15 +010093 pfn1 = bank_pfn_start(bank);
94 pfn2 = bank_pfn_end(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Russell King204ecae2007-01-16 14:01:47 +000096 page = map + pfn1;
97 end = map + pfn2;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010098
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
116 printk("%d pages of RAM\n", total);
117 printk("%d free pages\n", free);
118 printk("%d reserved pages\n", reserved);
119 printk("%d slab pages\n", slab);
120 printk("%d pages shared\n", shared);
121 printk("%d pages swap cached\n", cached);
122}
123
Russell Kingdde58282009-08-15 12:36:00 +0100124static void __init find_node_limits(int node, struct meminfo *mi,
125 unsigned long *min, unsigned long *max_low, unsigned long *max_high)
126{
127 int i;
128
129 *min = -1UL;
130 *max_low = *max_high = 0;
131
132 for_each_nodebank(i, mi, node) {
133 struct membank *bank = &mi->bank[i];
134 unsigned long start, end;
135
136 start = bank_pfn_start(bank);
137 end = bank_pfn_end(bank);
138
139 if (*min > start)
140 *min = start;
141 if (*max_high < end)
142 *max_high = end;
143 if (bank->highmem)
144 continue;
145 if (*max_low < end)
146 *max_low = end;
147 }
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
151 * FIXME: We really want to avoid allocating the bootmap bitmap
152 * over the top of the initrd. Hopefully, this is located towards
153 * the start of a bank, so if we allocate the bootmap bitmap at
154 * the end, we won't clash.
155 */
156static unsigned int __init
157find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
158{
Russell Kingd2a38ef2008-10-01 16:56:15 +0100159 unsigned int start_pfn, i, bootmap_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Russell King37efe642008-12-01 11:53:07 +0000161 start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 bootmap_pfn = 0;
163
Russell Kingd2a38ef2008-10-01 16:56:15 +0100164 for_each_nodebank(i, mi, node) {
165 struct membank *bank = &mi->bank[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 unsigned int start, end;
167
Russell Kingd2a38ef2008-10-01 16:56:15 +0100168 start = bank_pfn_start(bank);
169 end = bank_pfn_end(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (end < start_pfn)
172 continue;
173
174 if (start < start_pfn)
175 start = start_pfn;
176
177 if (end <= start)
178 continue;
179
180 if (end - start >= bootmap_pages) {
181 bootmap_pfn = start;
182 break;
183 }
184 }
185
186 if (bootmap_pfn == 0)
187 BUG();
188
189 return bootmap_pfn;
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static int __init check_initrd(struct meminfo *mi)
193{
194 int initrd_node = -2;
195#ifdef CONFIG_BLK_DEV_INITRD
196 unsigned long end = phys_initrd_start + phys_initrd_size;
197
198 /*
199 * Make sure that the initrd is within a valid area of
200 * memory.
201 */
202 if (phys_initrd_size) {
203 unsigned int i;
204
205 initrd_node = -1;
206
207 for (i = 0; i < mi->nr_banks; i++) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100208 struct membank *bank = &mi->bank[i];
209 if (bank_phys_start(bank) <= phys_initrd_start &&
210 end <= bank_phys_end(bank))
211 initrd_node = bank->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 }
214
215 if (initrd_node == -1) {
Russell Kingb962a282008-07-30 21:24:56 +0100216 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 "physical memory - disabling initrd\n",
Russell Kingb962a282008-07-30 21:24:56 +0100218 phys_initrd_start, phys_initrd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 phys_initrd_start = phys_initrd_size = 0;
220 }
221#endif
222
223 return initrd_node;
224}
225
Russell King456335e2006-09-27 10:00:54 +0100226static inline void map_memory_bank(struct membank *bank)
227{
Russell Kingd111e8f2006-09-27 15:27:33 +0100228#ifdef CONFIG_MMU
Russell King456335e2006-09-27 10:00:54 +0100229 struct map_desc map;
230
Russell Kingd2a38ef2008-10-01 16:56:15 +0100231 map.pfn = bank_pfn_start(bank);
232 map.virtual = __phys_to_virt(bank_phys_start(bank));
233 map.length = bank_phys_size(bank);
Russell King456335e2006-09-27 10:00:54 +0100234 map.type = MT_MEMORY;
235
236 create_mapping(&map);
Russell Kingd111e8f2006-09-27 15:27:33 +0100237#endif
Russell King456335e2006-09-27 10:00:54 +0100238}
239
Russell Kingdde58282009-08-15 12:36:00 +0100240static void __init bootmem_init_node(int node, struct meminfo *mi,
241 unsigned long start_pfn, unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Russell Kingdde58282009-08-15 12:36:00 +0100243 unsigned long boot_pfn;
Russell King90072052005-10-28 14:48:37 +0100244 unsigned int boot_pages;
245 pg_data_t *pgdat;
246 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /*
Russell Kingdde58282009-08-15 12:36:00 +0100249 * Map the memory banks for this node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
Russell King90072052005-10-28 14:48:37 +0100251 for_each_nodebank(i, mi, node) {
Russell King456335e2006-09-27 10:00:54 +0100252 struct membank *bank = &mi->bank[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Russell Kingdde58282009-08-15 12:36:00 +0100254 if (!bank->highmem)
255 map_memory_bank(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Russell King90072052005-10-28 14:48:37 +0100258 /*
Russell King90072052005-10-28 14:48:37 +0100259 * Allocate the bootmem bitmap page.
260 */
261 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
262 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
263
264 /*
265 * Initialise the bootmem allocator for this node, handing the
266 * memory banks over to bootmem.
267 */
268 node_set_online(node);
269 pgdat = NODE_DATA(node);
270 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
271
Russell Kingd2a38ef2008-10-01 16:56:15 +0100272 for_each_nodebank(i, mi, node) {
273 struct membank *bank = &mi->bank[i];
Russell Kingdde58282009-08-15 12:36:00 +0100274 if (!bank->highmem)
275 free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
Russell Kingd2a38ef2008-10-01 16:56:15 +0100276 }
Russell King90072052005-10-28 14:48:37 +0100277
278 /*
279 * Reserve the bootmem bitmap for this node.
280 */
281 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800282 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
Russell Kingb7a69ac2008-10-01 16:58:32 +0100283}
Russell Kingb962a282008-07-30 21:24:56 +0100284
Russell Kingb7a69ac2008-10-01 16:58:32 +0100285static void __init bootmem_reserve_initrd(int node)
286{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287#ifdef CONFIG_BLK_DEV_INITRD
Russell Kingb7a69ac2008-10-01 16:58:32 +0100288 pg_data_t *pgdat = NODE_DATA(node);
289 int res;
Russell Kingb962a282008-07-30 21:24:56 +0100290
Russell Kingb7a69ac2008-10-01 16:58:32 +0100291 res = reserve_bootmem_node(pgdat, phys_initrd_start,
292 phys_initrd_size, BOOTMEM_EXCLUSIVE);
293
294 if (res == 0) {
295 initrd_start = __phys_to_virt(phys_initrd_start);
296 initrd_end = initrd_start + phys_initrd_size;
297 } else {
298 printk(KERN_ERR
299 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
300 "memory region - disabling initrd\n",
301 phys_initrd_start, phys_initrd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303#endif
Russell Kingb7a69ac2008-10-01 16:58:32 +0100304}
305
306static void __init bootmem_free_node(int node, struct meminfo *mi)
307{
308 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
Russell Kingdde58282009-08-15 12:36:00 +0100309 unsigned long min, max_low, max_high;
Russell Kingb7a69ac2008-10-01 16:58:32 +0100310 int i;
311
Russell Kingdde58282009-08-15 12:36:00 +0100312 find_node_limits(node, mi, &min, &max_low, &max_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Russell King90072052005-10-28 14:48:37 +0100314 /*
Russell King90072052005-10-28 14:48:37 +0100315 * initialise the zones within this node.
316 */
317 memset(zone_size, 0, sizeof(zone_size));
Russell King90072052005-10-28 14:48:37 +0100318
319 /*
320 * The size of this node has already been determined. If we need
321 * to do anything fancy with the allocation of this memory to the
322 * zones, now is the time to do it.
323 */
Russell Kingdde58282009-08-15 12:36:00 +0100324 zone_size[0] = max_low - min;
325#ifdef CONFIG_HIGHMEM
326 zone_size[ZONE_HIGHMEM] = max_high - max_low;
327#endif
Russell King90072052005-10-28 14:48:37 +0100328
329 /*
330 * For each bank in this node, calculate the size of the holes.
331 * holes = node_size - sum(bank_sizes_in_node)
332 */
Russell Kingdde58282009-08-15 12:36:00 +0100333 memcpy(zhole_size, zone_size, sizeof(zhole_size));
334 for_each_nodebank(i, mi, node) {
335 int idx = 0;
336#ifdef CONFIG_HIGHMEM
337 if (mi->bank[i].highmem)
338 idx = ZONE_HIGHMEM;
339#endif
340 zhole_size[idx] -= bank_pfn_size(&mi->bank[i]);
341 }
Russell King90072052005-10-28 14:48:37 +0100342
343 /*
344 * Adjust the sizes according to any special requirements for
345 * this machine type.
346 */
347 arch_adjust_zones(node, zone_size, zhole_size);
348
Russell Kingdde58282009-08-15 12:36:00 +0100349 free_area_init_node(node, zone_size, min, zhole_size);
Russell King90072052005-10-28 14:48:37 +0100350}
351
Russell Kingb7cfda92009-09-07 15:06:42 +0100352#ifndef CONFIG_SPARSEMEM
353int pfn_valid(unsigned long pfn)
354{
355 struct meminfo *mi = &meminfo;
356 unsigned int left = 0, right = mi->nr_banks;
357
358 do {
359 unsigned int mid = (right + left) / 2;
360 struct membank *bank = &mi->bank[mid];
361
362 if (pfn < bank_pfn_start(bank))
363 right = mid;
364 else if (pfn >= bank_pfn_end(bank))
365 left = mid + 1;
366 else
367 return 1;
368 } while (left < right);
369 return 0;
370}
371EXPORT_SYMBOL(pfn_valid);
Russell King657e12f2009-10-29 17:06:17 +0000372
373static void arm_memory_present(struct meminfo *mi, int node)
374{
375}
376#else
377static void arm_memory_present(struct meminfo *mi, int node)
378{
379 int i;
380 for_each_nodebank(i, mi, node) {
381 struct membank *bank = &mi->bank[i];
382 memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
383 }
384}
Russell Kingb7cfda92009-09-07 15:06:42 +0100385#endif
386
387static int __init meminfo_cmp(const void *_a, const void *_b)
388{
389 const struct membank *a = _a, *b = _b;
390 long cmp = bank_pfn_start(a) - bank_pfn_start(b);
391 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
392}
393
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -0400394void __init bootmem_init(void)
Russell King90072052005-10-28 14:48:37 +0100395{
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -0400396 struct meminfo *mi = &meminfo;
Russell Kingdde58282009-08-15 12:36:00 +0100397 unsigned long min, max_low, max_high;
Russell Kingeca73212008-09-30 19:29:25 +0100398 int node, initrd_node;
Russell King90072052005-10-28 14:48:37 +0100399
Russell Kingb7cfda92009-09-07 15:06:42 +0100400 sort(&mi->bank, mi->nr_banks, sizeof(mi->bank[0]), meminfo_cmp, NULL);
401
Russell King90072052005-10-28 14:48:37 +0100402 /*
403 * Locate which node contains the ramdisk image, if any.
404 */
405 initrd_node = check_initrd(mi);
406
Russell Kingdde58282009-08-15 12:36:00 +0100407 max_low = max_high = 0;
408
Russell King90072052005-10-28 14:48:37 +0100409 /*
410 * Run through each node initialising the bootmem allocator.
411 */
412 for_each_node(node) {
Russell Kingdde58282009-08-15 12:36:00 +0100413 unsigned long node_low, node_high;
414
415 find_node_limits(node, mi, &min, &node_low, &node_high);
416
417 if (node_low > max_low)
418 max_low = node_low;
419 if (node_high > max_high)
420 max_high = node_high;
421
422 /*
423 * If there is no memory in this node, ignore it.
424 * (We can't have nodes which have no lowmem)
425 */
426 if (node_low == 0)
427 continue;
428
429 bootmem_init_node(node, mi, min, node_low);
Russell King90072052005-10-28 14:48:37 +0100430
Russell Kingb7a69ac2008-10-01 16:58:32 +0100431 /*
432 * Reserve any special node zero regions.
433 */
434 if (node == 0)
435 reserve_node_zero(NODE_DATA(node));
436
437 /*
438 * If the initrd is in this node, reserve its memory.
439 */
440 if (node == initrd_node)
441 bootmem_reserve_initrd(node);
Russell King657e12f2009-10-29 17:06:17 +0000442
443 /*
444 * Sparsemem tries to allocate bootmem in memory_present(),
445 * so must be done after the fixed reservations
446 */
447 arm_memory_present(mi, node);
Russell King90072052005-10-28 14:48:37 +0100448 }
449
Russell Kingb7a69ac2008-10-01 16:58:32 +0100450 /*
451 * sparse_init() needs the bootmem allocator up and running.
452 */
453 sparse_init();
454
455 /*
456 * Now free memory in each node - free_area_init_node needs
457 * the sparse mem_map arrays initialized by sparse_init()
458 * for memmap_init_zone(), otherwise all PFNs are invalid.
459 */
460 for_each_node(node)
461 bootmem_free_node(node, mi);
462
Russell Kingdde58282009-08-15 12:36:00 +0100463 high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
Russell King90072052005-10-28 14:48:37 +0100464
465 /*
466 * This doesn't seem to be used by the Linux memory manager any
467 * more, but is used by ll_rw_block. If we can get rid of it, we
468 * also get rid of some of the stuff above as well.
469 *
470 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
471 * the system, not the maximum PFN.
472 */
Russell Kingdde58282009-08-15 12:36:00 +0100473 max_low_pfn = max_low - PHYS_PFN_OFFSET;
474 max_pfn = max_high - PHYS_PFN_OFFSET;
Russell King90072052005-10-28 14:48:37 +0100475}
476
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400477static inline int free_area(unsigned long pfn, unsigned long end, char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400479 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400481 for (; pfn < end; pfn++) {
482 struct page *page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800484 init_page_count(page);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400485 __free_page(page);
486 pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
489 if (size && s)
490 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400491
492 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Russell Kinga0130532005-06-27 14:16:47 +0100495static inline void
496free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
497{
498 struct page *start_pg, *end_pg;
499 unsigned long pg, pgend;
500
501 /*
502 * Convert start_pfn/end_pfn to a struct page pointer.
503 */
Catalin Marinas3257f432009-10-06 17:57:22 +0100504 start_pg = pfn_to_page(start_pfn - 1) + 1;
Russell Kinga0130532005-06-27 14:16:47 +0100505 end_pg = pfn_to_page(end_pfn);
506
507 /*
508 * Convert to physical addresses, and
509 * round start upwards and end downwards.
510 */
511 pg = PAGE_ALIGN(__pa(start_pg));
512 pgend = __pa(end_pg) & PAGE_MASK;
513
514 /*
515 * If there are free pages between these,
516 * free the section of the memmap array.
517 */
518 if (pg < pgend)
519 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
520}
521
522/*
523 * The mem_map array can get very big. Free the unused area of the memory map.
524 */
525static void __init free_unused_memmap_node(int node, struct meminfo *mi)
526{
527 unsigned long bank_start, prev_bank_end = 0;
528 unsigned int i;
529
530 /*
531 * [FIXME] This relies on each bank being in address order. This
532 * may not be the case, especially if the user has provided the
533 * information on the command line.
534 */
Russell King90072052005-10-28 14:48:37 +0100535 for_each_nodebank(i, mi, node) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100536 struct membank *bank = &mi->bank[i];
537
538 bank_start = bank_pfn_start(bank);
Russell Kinga0130532005-06-27 14:16:47 +0100539 if (bank_start < prev_bank_end) {
540 printk(KERN_ERR "MEM: unordered memory banks. "
541 "Not freeing memmap.\n");
542 break;
543 }
544
545 /*
546 * If we had a previous bank, and there is a space
547 * between the current bank and the previous, free it.
548 */
549 if (prev_bank_end && prev_bank_end != bank_start)
550 free_memmap(node, prev_bank_end, bank_start);
551
Russell Kingd2a38ef2008-10-01 16:56:15 +0100552 prev_bank_end = bank_pfn_end(bank);
Russell Kinga0130532005-06-27 14:16:47 +0100553 }
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
557 * mem_init() marks the free areas in the mem_map and tells us how much
558 * memory is free. This is done after various parts of the system have
559 * claimed their memory after the kernel image.
560 */
561void __init mem_init(void)
562{
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400563 unsigned int codesize, datasize, initsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int i, node;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#ifndef CONFIG_DISCONTIGMEM
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400567 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568#endif
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* this will put all unused low memory onto the freelists */
571 for_each_online_node(node) {
572 pg_data_t *pgdat = NODE_DATA(node);
573
Russell Kinga0130532005-06-27 14:16:47 +0100574 free_unused_memmap_node(node, &meminfo);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (pgdat->node_spanned_pages != 0)
577 totalram_pages += free_all_bootmem_node(pgdat);
578 }
579
580#ifdef CONFIG_SA1111
581 /* now that our DMA memory is actually so designated, we can free it */
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400582 totalram_pages += free_area(PHYS_PFN_OFFSET,
583 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584#endif
585
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400586#ifdef CONFIG_HIGHMEM
587 /* set highmem page free */
588 for_each_online_node(node) {
589 for_each_nodebank (i, &meminfo, node) {
590 unsigned long start = bank_pfn_start(&meminfo.bank[i]);
591 unsigned long end = bank_pfn_end(&meminfo.bank[i]);
592 if (start >= max_low_pfn + PHYS_PFN_OFFSET)
593 totalhigh_pages += free_area(start, end, NULL);
594 }
595 }
596 totalram_pages += totalhigh_pages;
597#endif
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /*
600 * Since our memory may not be contiguous, calculate the
601 * real number of pages we have in this system
602 */
603 printk(KERN_INFO "Memory:");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 num_physpages = 0;
605 for (i = 0; i < meminfo.nr_banks; i++) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100606 num_physpages += bank_pfn_size(&meminfo.bank[i]);
607 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400610
Russell King37efe642008-12-01 11:53:07 +0000611 codesize = _etext - _text;
612 datasize = _end - _data;
613 initsize = __init_end - __init_begin;
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400616 "%dK data, %dK init, %luK highmem)\n",
Geert Uytterhoevencc013a82009-09-21 17:02:36 -0700617 nr_free_pages() << (PAGE_SHIFT-10), codesize >> 10,
618 datasize >> 10, initsize >> 10,
Andreas Fenkart4b529402010-01-08 14:42:31 -0800619 totalhigh_pages << (PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
622 extern int sysctl_overcommit_memory;
623 /*
624 * On a machine this small we won't get
625 * anywhere without overcommit, so turn
626 * it on by default.
627 */
628 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
629 }
630}
631
632void free_initmem(void)
633{
Linus Walleijbc581772009-09-15 17:30:37 +0100634#ifdef CONFIG_HAVE_TCM
635 extern char *__tcm_start, *__tcm_end;
636
637 totalram_pages += free_area(__phys_to_pfn(__pa(__tcm_start)),
638 __phys_to_pfn(__pa(__tcm_end)),
639 "TCM link");
640#endif
641
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400642 if (!machine_is_integrator() && !machine_is_cintegrator())
Russell King37efe642008-12-01 11:53:07 +0000643 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
644 __phys_to_pfn(__pa(__init_end)),
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400645 "init");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648#ifdef CONFIG_BLK_DEV_INITRD
649
650static int keep_initrd;
651
652void free_initrd_mem(unsigned long start, unsigned long end)
653{
654 if (!keep_initrd)
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400655 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
656 __phys_to_pfn(__pa(end)),
657 "initrd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660static int __init keepinitrd_setup(char *__unused)
661{
662 keep_initrd = 1;
663 return 1;
664}
665
666__setup("keepinitrd", keepinitrd_setup);
667#endif