blob: 8bbb9a972e7158cad14584e395b9b315383be08b [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>
Nicolas Pitre3835f6c2008-09-17 15:21:55 -040018#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/mach-types.h>
Russell King37efe642008-12-01 11:53:07 +000021#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/setup.h>
Russell King74d02fb2006-04-04 21:47:43 +010023#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/tlb.h>
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +010025#include <asm/fixmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
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
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010035static int __init early_initrd(char *p)
Russell King012d1f42008-09-06 10:57:03 +010036{
37 unsigned long start, size;
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010038 char *endp;
Russell King012d1f42008-09-06 10:57:03 +010039
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010040 start = memparse(p, &endp);
41 if (*endp == ',') {
42 size = memparse(endp + 1, NULL);
Russell King012d1f42008-09-06 10:57:03 +010043
44 phys_initrd_start = start;
45 phys_initrd_size = size;
46 }
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010047 return 0;
Russell King012d1f42008-09-06 10:57:03 +010048}
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010049early_param("initrd", early_initrd);
Russell King012d1f42008-09-06 10:57:03 +010050
51static int __init parse_tag_initrd(const struct tag *tag)
52{
53 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
54 "please update your bootloader.\n");
55 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
56 phys_initrd_size = tag->u.initrd.size;
57 return 0;
58}
59
60__tagtable(ATAG_INITRD, parse_tag_initrd);
61
62static int __init parse_tag_initrd2(const struct tag *tag)
63{
64 phys_initrd_start = tag->u.initrd.start;
65 phys_initrd_size = tag->u.initrd.size;
66 return 0;
67}
68
69__tagtable(ATAG_INITRD2, parse_tag_initrd2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040072 * This keeps memory configuration data used by a couple memory
73 * initialization functions, as well as show_mem() for the skipping
74 * of holes in the memory map. It is populated by arm_add_memory().
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040076struct meminfo meminfo;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078void show_mem(void)
79{
80 int free = 0, total = 0, reserved = 0;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010081 int shared = 0, cached = 0, slab = 0, node, i;
82 struct meminfo * mi = &meminfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 printk("Mem-info:\n");
85 show_free_areas();
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 for_each_online_node(node) {
Russell King204ecae2007-01-16 14:01:47 +000087 pg_data_t *n = NODE_DATA(node);
Russell Kingb7a69ac2008-10-01 16:58:32 +010088 struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn;
Russell King204ecae2007-01-16 14:01:47 +000089
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010090 for_each_nodebank (i,mi,node) {
Russell Kingd2a38ef2008-10-01 16:56:15 +010091 struct membank *bank = &mi->bank[i];
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010092 unsigned int pfn1, pfn2;
93 struct page *page, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Russell Kingd2a38ef2008-10-01 16:56:15 +010095 pfn1 = bank_pfn_start(bank);
96 pfn2 = bank_pfn_end(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Russell King204ecae2007-01-16 14:01:47 +000098 page = map + pfn1;
99 end = map + pfn2;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +0100100
101 do {
102 total++;
103 if (PageReserved(page))
104 reserved++;
105 else if (PageSwapCache(page))
106 cached++;
107 else if (PageSlab(page))
108 slab++;
109 else if (!page_count(page))
110 free++;
111 else
112 shared += page_count(page) - 1;
113 page++;
114 } while (page < end);
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
118 printk("%d pages of RAM\n", total);
119 printk("%d free pages\n", free);
120 printk("%d reserved pages\n", reserved);
121 printk("%d slab pages\n", slab);
122 printk("%d pages shared\n", shared);
123 printk("%d pages swap cached\n", cached);
124}
125
Russell Kingdde58282009-08-15 12:36:00 +0100126static void __init find_node_limits(int node, struct meminfo *mi,
127 unsigned long *min, unsigned long *max_low, unsigned long *max_high)
128{
129 int i;
130
131 *min = -1UL;
132 *max_low = *max_high = 0;
133
134 for_each_nodebank(i, mi, node) {
135 struct membank *bank = &mi->bank[i];
136 unsigned long start, end;
137
138 start = bank_pfn_start(bank);
139 end = bank_pfn_end(bank);
140
141 if (*min > start)
142 *min = start;
143 if (*max_high < end)
144 *max_high = end;
145 if (bank->highmem)
146 continue;
147 if (*max_low < end)
148 *max_low = end;
149 }
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
153 * FIXME: We really want to avoid allocating the bootmap bitmap
154 * over the top of the initrd. Hopefully, this is located towards
155 * the start of a bank, so if we allocate the bootmap bitmap at
156 * the end, we won't clash.
157 */
158static unsigned int __init
159find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
160{
Russell Kingd2a38ef2008-10-01 16:56:15 +0100161 unsigned int start_pfn, i, bootmap_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Russell King37efe642008-12-01 11:53:07 +0000163 start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 bootmap_pfn = 0;
165
Russell Kingd2a38ef2008-10-01 16:56:15 +0100166 for_each_nodebank(i, mi, node) {
167 struct membank *bank = &mi->bank[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int start, end;
169
Russell Kingd2a38ef2008-10-01 16:56:15 +0100170 start = bank_pfn_start(bank);
171 end = bank_pfn_end(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 if (end < start_pfn)
174 continue;
175
176 if (start < start_pfn)
177 start = start_pfn;
178
179 if (end <= start)
180 continue;
181
182 if (end - start >= bootmap_pages) {
183 bootmap_pfn = start;
184 break;
185 }
186 }
187
188 if (bootmap_pfn == 0)
189 BUG();
190
191 return bootmap_pfn;
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static int __init check_initrd(struct meminfo *mi)
195{
196 int initrd_node = -2;
197#ifdef CONFIG_BLK_DEV_INITRD
198 unsigned long end = phys_initrd_start + phys_initrd_size;
199
200 /*
201 * Make sure that the initrd is within a valid area of
202 * memory.
203 */
204 if (phys_initrd_size) {
205 unsigned int i;
206
207 initrd_node = -1;
208
209 for (i = 0; i < mi->nr_banks; i++) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100210 struct membank *bank = &mi->bank[i];
211 if (bank_phys_start(bank) <= phys_initrd_start &&
212 end <= bank_phys_end(bank))
213 initrd_node = bank->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 }
216
217 if (initrd_node == -1) {
Russell Kingb962a282008-07-30 21:24:56 +0100218 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 "physical memory - disabling initrd\n",
Russell Kingb962a282008-07-30 21:24:56 +0100220 phys_initrd_start, phys_initrd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 phys_initrd_start = phys_initrd_size = 0;
222 }
223#endif
224
225 return initrd_node;
226}
227
Russell King456335e2006-09-27 10:00:54 +0100228static inline void map_memory_bank(struct membank *bank)
229{
Russell Kingd111e8f2006-09-27 15:27:33 +0100230#ifdef CONFIG_MMU
Russell King456335e2006-09-27 10:00:54 +0100231 struct map_desc map;
232
Russell Kingd2a38ef2008-10-01 16:56:15 +0100233 map.pfn = bank_pfn_start(bank);
234 map.virtual = __phys_to_virt(bank_phys_start(bank));
235 map.length = bank_phys_size(bank);
Russell King456335e2006-09-27 10:00:54 +0100236 map.type = MT_MEMORY;
237
238 create_mapping(&map);
Russell Kingd111e8f2006-09-27 15:27:33 +0100239#endif
Russell King456335e2006-09-27 10:00:54 +0100240}
241
Russell Kingdde58282009-08-15 12:36:00 +0100242static void __init bootmem_init_node(int node, struct meminfo *mi,
243 unsigned long start_pfn, unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Russell Kingdde58282009-08-15 12:36:00 +0100245 unsigned long boot_pfn;
Russell King90072052005-10-28 14:48:37 +0100246 unsigned int boot_pages;
247 pg_data_t *pgdat;
248 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /*
Russell Kingdde58282009-08-15 12:36:00 +0100251 * Map the memory banks for this node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
Russell King90072052005-10-28 14:48:37 +0100253 for_each_nodebank(i, mi, node) {
Russell King456335e2006-09-27 10:00:54 +0100254 struct membank *bank = &mi->bank[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Russell Kingdde58282009-08-15 12:36:00 +0100256 if (!bank->highmem)
257 map_memory_bank(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
Russell King90072052005-10-28 14:48:37 +0100260 /*
Russell King90072052005-10-28 14:48:37 +0100261 * Allocate the bootmem bitmap page.
262 */
263 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
264 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
265
266 /*
267 * Initialise the bootmem allocator for this node, handing the
268 * memory banks over to bootmem.
269 */
270 node_set_online(node);
271 pgdat = NODE_DATA(node);
272 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
273
Russell Kingd2a38ef2008-10-01 16:56:15 +0100274 for_each_nodebank(i, mi, node) {
275 struct membank *bank = &mi->bank[i];
Russell Kingdde58282009-08-15 12:36:00 +0100276 if (!bank->highmem)
277 free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
Russell Kingd2a38ef2008-10-01 16:56:15 +0100278 }
Russell King90072052005-10-28 14:48:37 +0100279
280 /*
281 * Reserve the bootmem bitmap for this node.
282 */
283 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800284 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
Russell Kingb7a69ac2008-10-01 16:58:32 +0100285}
Russell Kingb962a282008-07-30 21:24:56 +0100286
Russell Kingb7a69ac2008-10-01 16:58:32 +0100287static void __init bootmem_reserve_initrd(int node)
288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289#ifdef CONFIG_BLK_DEV_INITRD
Russell Kingb7a69ac2008-10-01 16:58:32 +0100290 pg_data_t *pgdat = NODE_DATA(node);
291 int res;
Russell Kingb962a282008-07-30 21:24:56 +0100292
Russell Kingb7a69ac2008-10-01 16:58:32 +0100293 res = reserve_bootmem_node(pgdat, phys_initrd_start,
294 phys_initrd_size, BOOTMEM_EXCLUSIVE);
295
296 if (res == 0) {
297 initrd_start = __phys_to_virt(phys_initrd_start);
298 initrd_end = initrd_start + phys_initrd_size;
299 } else {
300 printk(KERN_ERR
301 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
302 "memory region - disabling initrd\n",
303 phys_initrd_start, phys_initrd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305#endif
Russell Kingb7a69ac2008-10-01 16:58:32 +0100306}
307
308static void __init bootmem_free_node(int node, struct meminfo *mi)
309{
310 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
Russell Kingdde58282009-08-15 12:36:00 +0100311 unsigned long min, max_low, max_high;
Russell Kingb7a69ac2008-10-01 16:58:32 +0100312 int i;
313
Russell Kingdde58282009-08-15 12:36:00 +0100314 find_node_limits(node, mi, &min, &max_low, &max_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Russell King90072052005-10-28 14:48:37 +0100316 /*
Russell King90072052005-10-28 14:48:37 +0100317 * initialise the zones within this node.
318 */
319 memset(zone_size, 0, sizeof(zone_size));
Russell King90072052005-10-28 14:48:37 +0100320
321 /*
322 * The size of this node has already been determined. If we need
323 * to do anything fancy with the allocation of this memory to the
324 * zones, now is the time to do it.
325 */
Russell Kingdde58282009-08-15 12:36:00 +0100326 zone_size[0] = max_low - min;
327#ifdef CONFIG_HIGHMEM
328 zone_size[ZONE_HIGHMEM] = max_high - max_low;
329#endif
Russell King90072052005-10-28 14:48:37 +0100330
331 /*
332 * For each bank in this node, calculate the size of the holes.
333 * holes = node_size - sum(bank_sizes_in_node)
334 */
Russell Kingdde58282009-08-15 12:36:00 +0100335 memcpy(zhole_size, zone_size, sizeof(zhole_size));
336 for_each_nodebank(i, mi, node) {
337 int idx = 0;
338#ifdef CONFIG_HIGHMEM
339 if (mi->bank[i].highmem)
340 idx = ZONE_HIGHMEM;
341#endif
342 zhole_size[idx] -= bank_pfn_size(&mi->bank[i]);
343 }
Russell King90072052005-10-28 14:48:37 +0100344
345 /*
346 * Adjust the sizes according to any special requirements for
347 * this machine type.
348 */
349 arch_adjust_zones(node, zone_size, zhole_size);
350
Russell Kingdde58282009-08-15 12:36:00 +0100351 free_area_init_node(node, zone_size, min, zhole_size);
Russell King90072052005-10-28 14:48:37 +0100352}
353
Russell Kingb7cfda92009-09-07 15:06:42 +0100354#ifndef CONFIG_SPARSEMEM
355int pfn_valid(unsigned long pfn)
356{
357 struct meminfo *mi = &meminfo;
358 unsigned int left = 0, right = mi->nr_banks;
359
360 do {
361 unsigned int mid = (right + left) / 2;
362 struct membank *bank = &mi->bank[mid];
363
364 if (pfn < bank_pfn_start(bank))
365 right = mid;
366 else if (pfn >= bank_pfn_end(bank))
367 left = mid + 1;
368 else
369 return 1;
370 } while (left < right);
371 return 0;
372}
373EXPORT_SYMBOL(pfn_valid);
Russell King657e12f2009-10-29 17:06:17 +0000374
375static void arm_memory_present(struct meminfo *mi, int node)
376{
377}
378#else
379static void arm_memory_present(struct meminfo *mi, int node)
380{
381 int i;
382 for_each_nodebank(i, mi, node) {
383 struct membank *bank = &mi->bank[i];
384 memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
385 }
386}
Russell Kingb7cfda92009-09-07 15:06:42 +0100387#endif
388
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -0400389void __init bootmem_init(void)
Russell King90072052005-10-28 14:48:37 +0100390{
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -0400391 struct meminfo *mi = &meminfo;
Russell Kingdde58282009-08-15 12:36:00 +0100392 unsigned long min, max_low, max_high;
Russell Kingeca73212008-09-30 19:29:25 +0100393 int node, initrd_node;
Russell King90072052005-10-28 14:48:37 +0100394
Russell King90072052005-10-28 14:48:37 +0100395 /*
396 * Locate which node contains the ramdisk image, if any.
397 */
398 initrd_node = check_initrd(mi);
399
Russell Kingdde58282009-08-15 12:36:00 +0100400 max_low = max_high = 0;
401
Russell King90072052005-10-28 14:48:37 +0100402 /*
403 * Run through each node initialising the bootmem allocator.
404 */
405 for_each_node(node) {
Russell Kingdde58282009-08-15 12:36:00 +0100406 unsigned long node_low, node_high;
407
408 find_node_limits(node, mi, &min, &node_low, &node_high);
409
410 if (node_low > max_low)
411 max_low = node_low;
412 if (node_high > max_high)
413 max_high = node_high;
414
415 /*
416 * If there is no memory in this node, ignore it.
417 * (We can't have nodes which have no lowmem)
418 */
419 if (node_low == 0)
420 continue;
421
422 bootmem_init_node(node, mi, min, node_low);
Russell King90072052005-10-28 14:48:37 +0100423
Russell Kingb7a69ac2008-10-01 16:58:32 +0100424 /*
425 * Reserve any special node zero regions.
426 */
427 if (node == 0)
428 reserve_node_zero(NODE_DATA(node));
429
430 /*
431 * If the initrd is in this node, reserve its memory.
432 */
433 if (node == initrd_node)
434 bootmem_reserve_initrd(node);
Russell King657e12f2009-10-29 17:06:17 +0000435
436 /*
437 * Sparsemem tries to allocate bootmem in memory_present(),
438 * so must be done after the fixed reservations
439 */
440 arm_memory_present(mi, node);
Russell King90072052005-10-28 14:48:37 +0100441 }
442
Russell Kingb7a69ac2008-10-01 16:58:32 +0100443 /*
444 * sparse_init() needs the bootmem allocator up and running.
445 */
446 sparse_init();
447
448 /*
449 * Now free memory in each node - free_area_init_node needs
450 * the sparse mem_map arrays initialized by sparse_init()
451 * for memmap_init_zone(), otherwise all PFNs are invalid.
452 */
453 for_each_node(node)
454 bootmem_free_node(node, mi);
455
Russell Kingdde58282009-08-15 12:36:00 +0100456 high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
Russell King90072052005-10-28 14:48:37 +0100457
458 /*
459 * This doesn't seem to be used by the Linux memory manager any
460 * more, but is used by ll_rw_block. If we can get rid of it, we
461 * also get rid of some of the stuff above as well.
462 *
463 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
464 * the system, not the maximum PFN.
465 */
Russell Kingdde58282009-08-15 12:36:00 +0100466 max_low_pfn = max_low - PHYS_PFN_OFFSET;
467 max_pfn = max_high - PHYS_PFN_OFFSET;
Russell King90072052005-10-28 14:48:37 +0100468}
469
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400470static inline int free_area(unsigned long pfn, unsigned long end, char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400472 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400474 for (; pfn < end; pfn++) {
475 struct page *page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800477 init_page_count(page);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400478 __free_page(page);
479 pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 if (size && s)
483 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400484
485 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Russell Kinga0130532005-06-27 14:16:47 +0100488static inline void
489free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
490{
491 struct page *start_pg, *end_pg;
492 unsigned long pg, pgend;
493
494 /*
495 * Convert start_pfn/end_pfn to a struct page pointer.
496 */
Catalin Marinas3257f432009-10-06 17:57:22 +0100497 start_pg = pfn_to_page(start_pfn - 1) + 1;
Russell Kinga0130532005-06-27 14:16:47 +0100498 end_pg = pfn_to_page(end_pfn);
499
500 /*
501 * Convert to physical addresses, and
502 * round start upwards and end downwards.
503 */
504 pg = PAGE_ALIGN(__pa(start_pg));
505 pgend = __pa(end_pg) & PAGE_MASK;
506
507 /*
508 * If there are free pages between these,
509 * free the section of the memmap array.
510 */
511 if (pg < pgend)
512 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
513}
514
515/*
516 * The mem_map array can get very big. Free the unused area of the memory map.
517 */
518static void __init free_unused_memmap_node(int node, struct meminfo *mi)
519{
520 unsigned long bank_start, prev_bank_end = 0;
521 unsigned int i;
522
523 /*
524 * [FIXME] This relies on each bank being in address order. This
525 * may not be the case, especially if the user has provided the
526 * information on the command line.
527 */
Russell King90072052005-10-28 14:48:37 +0100528 for_each_nodebank(i, mi, node) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100529 struct membank *bank = &mi->bank[i];
530
531 bank_start = bank_pfn_start(bank);
Russell Kinga0130532005-06-27 14:16:47 +0100532 if (bank_start < prev_bank_end) {
533 printk(KERN_ERR "MEM: unordered memory banks. "
534 "Not freeing memmap.\n");
535 break;
536 }
537
538 /*
539 * If we had a previous bank, and there is a space
540 * between the current bank and the previous, free it.
541 */
542 if (prev_bank_end && prev_bank_end != bank_start)
543 free_memmap(node, prev_bank_end, bank_start);
544
Russell Kingd2a38ef2008-10-01 16:56:15 +0100545 prev_bank_end = bank_pfn_end(bank);
Russell Kinga0130532005-06-27 14:16:47 +0100546 }
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/*
550 * mem_init() marks the free areas in the mem_map and tells us how much
551 * memory is free. This is done after various parts of the system have
552 * claimed their memory after the kernel image.
553 */
554void __init mem_init(void)
555{
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100556 unsigned long reserved_pages, free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int i, node;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#ifndef CONFIG_DISCONTIGMEM
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400560 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561#endif
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* this will put all unused low memory onto the freelists */
564 for_each_online_node(node) {
565 pg_data_t *pgdat = NODE_DATA(node);
566
Russell Kinga0130532005-06-27 14:16:47 +0100567 free_unused_memmap_node(node, &meminfo);
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (pgdat->node_spanned_pages != 0)
570 totalram_pages += free_all_bootmem_node(pgdat);
571 }
572
573#ifdef CONFIG_SA1111
574 /* now that our DMA memory is actually so designated, we can free it */
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400575 totalram_pages += free_area(PHYS_PFN_OFFSET,
576 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif
578
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400579#ifdef CONFIG_HIGHMEM
580 /* set highmem page free */
581 for_each_online_node(node) {
582 for_each_nodebank (i, &meminfo, node) {
583 unsigned long start = bank_pfn_start(&meminfo.bank[i]);
584 unsigned long end = bank_pfn_end(&meminfo.bank[i]);
585 if (start >= max_low_pfn + PHYS_PFN_OFFSET)
586 totalhigh_pages += free_area(start, end, NULL);
587 }
588 }
589 totalram_pages += totalhigh_pages;
590#endif
591
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100592 reserved_pages = free_pages = 0;
593
594 for_each_online_node(node) {
595 pg_data_t *n = NODE_DATA(node);
596 struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn;
597
598 for_each_nodebank(i, &meminfo, node) {
599 struct membank *bank = &meminfo.bank[i];
600 unsigned int pfn1, pfn2;
601 struct page *page, *end;
602
603 pfn1 = bank_pfn_start(bank);
604 pfn2 = bank_pfn_end(bank);
605
606 page = map + pfn1;
607 end = map + pfn2;
608
609 do {
610 if (PageReserved(page))
611 reserved_pages++;
612 else if (!page_count(page))
613 free_pages++;
614 page++;
615 } while (page < end);
616 }
617 }
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /*
620 * Since our memory may not be contiguous, calculate the
621 * real number of pages we have in this system
622 */
623 printk(KERN_INFO "Memory:");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 num_physpages = 0;
625 for (i = 0; i < meminfo.nr_banks; i++) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100626 num_physpages += bank_pfn_size(&meminfo.bank[i]);
627 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400630
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100631 printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
632 nr_free_pages() << (PAGE_SHIFT-10),
633 free_pages << (PAGE_SHIFT-10),
634 reserved_pages << (PAGE_SHIFT-10),
Andreas Fenkart4b529402010-01-08 14:42:31 -0800635 totalhigh_pages << (PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100637#define MLK(b, t) b, t, ((t) - (b)) >> 10
638#define MLM(b, t) b, t, ((t) - (b)) >> 20
639#define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
640
641 printk(KERN_NOTICE "Virtual kernel memory layout:\n"
642 " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
643 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Fenkart/Bostandzhyana7bd08c2010-02-07 21:46:33 +0100644#ifdef CONFIG_MMU
645 " DMA : 0x%08lx - 0x%08lx (%4ld MB)\n"
646#endif
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100647 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
648 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
649#ifdef CONFIG_HIGHMEM
650 " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n"
651#endif
652 " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
653 " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
654 " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
655 " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
656
657 MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
658 (PAGE_SIZE)),
659 MLK(FIXADDR_START, FIXADDR_TOP),
Fenkart/Bostandzhyana7bd08c2010-02-07 21:46:33 +0100660#ifdef CONFIG_MMU
661 MLM(CONSISTENT_BASE, CONSISTENT_END),
662#endif
Fenkart/Bostandzhyanc931b4f2010-02-07 21:47:17 +0100663 MLM(VMALLOC_START, VMALLOC_END),
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100664 MLM(PAGE_OFFSET, (unsigned long)high_memory),
665#ifdef CONFIG_HIGHMEM
666 MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
667 (PAGE_SIZE)),
668#endif
669 MLM(MODULES_VADDR, MODULES_END),
670
671 MLK_ROUNDUP(__init_begin, __init_end),
672 MLK_ROUNDUP(_text, _etext),
673 MLK_ROUNDUP(_data, _edata));
674
675#undef MLK
676#undef MLM
677#undef MLK_ROUNDUP
678
Fenkart/Bostandzhyana1839272010-02-07 21:47:58 +0100679 /*
680 * Check boundaries twice: Some fundamental inconsistencies can
681 * be detected at build time already.
682 */
683#ifdef CONFIG_MMU
684 BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
685 BUG_ON(VMALLOC_END > CONSISTENT_BASE);
686
687 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
688 BUG_ON(TASK_SIZE > MODULES_VADDR);
689#endif
690
691#ifdef CONFIG_HIGHMEM
692 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
693 BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
694#endif
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
697 extern int sysctl_overcommit_memory;
698 /*
699 * On a machine this small we won't get
700 * anywhere without overcommit, so turn
701 * it on by default.
702 */
703 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
704 }
705}
706
707void free_initmem(void)
708{
Linus Walleijbc581772009-09-15 17:30:37 +0100709#ifdef CONFIG_HAVE_TCM
710 extern char *__tcm_start, *__tcm_end;
711
712 totalram_pages += free_area(__phys_to_pfn(__pa(__tcm_start)),
713 __phys_to_pfn(__pa(__tcm_end)),
714 "TCM link");
715#endif
716
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400717 if (!machine_is_integrator() && !machine_is_cintegrator())
Russell King37efe642008-12-01 11:53:07 +0000718 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
719 __phys_to_pfn(__pa(__init_end)),
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400720 "init");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723#ifdef CONFIG_BLK_DEV_INITRD
724
725static int keep_initrd;
726
727void free_initrd_mem(unsigned long start, unsigned long end)
728{
729 if (!keep_initrd)
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400730 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
731 __phys_to_pfn(__pa(end)),
732 "initrd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735static int __init keepinitrd_setup(char *__unused)
736{
737 keep_initrd = 1;
738 return 1;
739}
740
741__setup("keepinitrd", keepinitrd_setup);
742#endif