blob: 0ed29bfeba1cc0d6896fe3877e8653c26d1867f8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/mach-types.h>
Russell King37efe642008-12-01 11:53:07 +000023#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/setup.h>
Russell King74d02fb2006-04-04 21:47:43 +010025#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/tlb.h>
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +010027#include <asm/fixmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/mach/arch.h>
30#include <asm/mach/map.h>
31
Russell King1b2e2b72006-08-21 17:06:38 +010032#include "mm.h"
33
Russell King012d1f42008-09-06 10:57:03 +010034static unsigned long phys_initrd_start __initdata = 0;
35static unsigned long phys_initrd_size __initdata = 0;
36
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010037static int __init early_initrd(char *p)
Russell King012d1f42008-09-06 10:57:03 +010038{
39 unsigned long start, size;
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010040 char *endp;
Russell King012d1f42008-09-06 10:57:03 +010041
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010042 start = memparse(p, &endp);
43 if (*endp == ',') {
44 size = memparse(endp + 1, NULL);
Russell King012d1f42008-09-06 10:57:03 +010045
46 phys_initrd_start = start;
47 phys_initrd_size = size;
48 }
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010049 return 0;
Russell King012d1f42008-09-06 10:57:03 +010050}
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010051early_param("initrd", early_initrd);
Russell King012d1f42008-09-06 10:57:03 +010052
53static int __init parse_tag_initrd(const struct tag *tag)
54{
55 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
56 "please update your bootloader.\n");
57 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
58 phys_initrd_size = tag->u.initrd.size;
59 return 0;
60}
61
62__tagtable(ATAG_INITRD, parse_tag_initrd);
63
64static int __init parse_tag_initrd2(const struct tag *tag)
65{
66 phys_initrd_start = tag->u.initrd.start;
67 phys_initrd_size = tag->u.initrd.size;
68 return 0;
69}
70
71__tagtable(ATAG_INITRD2, parse_tag_initrd2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040074 * This keeps memory configuration data used by a couple memory
75 * initialization functions, as well as show_mem() for the skipping
76 * of holes in the memory map. It is populated by arm_add_memory().
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040078struct meminfo meminfo;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080void show_mem(void)
81{
82 int free = 0, total = 0, reserved = 0;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010083 int shared = 0, cached = 0, slab = 0, node, i;
84 struct meminfo * mi = &meminfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 printk("Mem-info:\n");
87 show_free_areas();
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 for_each_online_node(node) {
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010089 for_each_nodebank (i,mi,node) {
Russell Kingd2a38ef2008-10-01 16:56:15 +010090 struct membank *bank = &mi->bank[i];
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010091 unsigned int pfn1, pfn2;
92 struct page *page, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Russell Kingd2a38ef2008-10-01 16:56:15 +010094 pfn1 = bank_pfn_start(bank);
95 pfn2 = bank_pfn_end(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Catalin Marinasea056df2010-05-04 17:27:43 +010097 page = pfn_to_page(pfn1);
98 end = pfn_to_page(pfn2 - 1) + 1;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010099
100 do {
101 total++;
102 if (PageReserved(page))
103 reserved++;
104 else if (PageSwapCache(page))
105 cached++;
106 else if (PageSlab(page))
107 slab++;
108 else if (!page_count(page))
109 free++;
110 else
111 shared += page_count(page) - 1;
112 page++;
113 } while (page < end);
114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
117 printk("%d pages of RAM\n", total);
118 printk("%d free pages\n", free);
119 printk("%d reserved pages\n", reserved);
120 printk("%d slab pages\n", slab);
121 printk("%d pages shared\n", shared);
122 printk("%d pages swap cached\n", cached);
123}
124
Russell Kingdde58282009-08-15 12:36:00 +0100125static void __init find_node_limits(int node, struct meminfo *mi,
126 unsigned long *min, unsigned long *max_low, unsigned long *max_high)
127{
128 int i;
129
130 *min = -1UL;
131 *max_low = *max_high = 0;
132
133 for_each_nodebank(i, mi, node) {
134 struct membank *bank = &mi->bank[i];
135 unsigned long start, end;
136
137 start = bank_pfn_start(bank);
138 end = bank_pfn_end(bank);
139
140 if (*min > start)
141 *min = start;
142 if (*max_high < end)
143 *max_high = end;
144 if (bank->highmem)
145 continue;
146 if (*max_low < end)
147 *max_low = end;
148 }
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
152 * FIXME: We really want to avoid allocating the bootmap bitmap
153 * over the top of the initrd. Hopefully, this is located towards
154 * the start of a bank, so if we allocate the bootmap bitmap at
155 * the end, we won't clash.
156 */
157static unsigned int __init
158find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
159{
Russell Kingd2a38ef2008-10-01 16:56:15 +0100160 unsigned int start_pfn, i, bootmap_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Russell King37efe642008-12-01 11:53:07 +0000162 start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 bootmap_pfn = 0;
164
Russell Kingd2a38ef2008-10-01 16:56:15 +0100165 for_each_nodebank(i, mi, node) {
166 struct membank *bank = &mi->bank[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 unsigned int start, end;
168
Russell Kingd2a38ef2008-10-01 16:56:15 +0100169 start = bank_pfn_start(bank);
170 end = bank_pfn_end(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 if (end < start_pfn)
173 continue;
174
175 if (start < start_pfn)
176 start = start_pfn;
177
178 if (end <= start)
179 continue;
180
181 if (end - start >= bootmap_pages) {
182 bootmap_pfn = start;
183 break;
184 }
185 }
186
187 if (bootmap_pfn == 0)
188 BUG();
189
190 return bootmap_pfn;
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int __init check_initrd(struct meminfo *mi)
194{
195 int initrd_node = -2;
196#ifdef CONFIG_BLK_DEV_INITRD
197 unsigned long end = phys_initrd_start + phys_initrd_size;
198
199 /*
200 * Make sure that the initrd is within a valid area of
201 * memory.
202 */
203 if (phys_initrd_size) {
204 unsigned int i;
205
206 initrd_node = -1;
207
208 for (i = 0; i < mi->nr_banks; i++) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100209 struct membank *bank = &mi->bank[i];
210 if (bank_phys_start(bank) <= phys_initrd_start &&
211 end <= bank_phys_end(bank))
212 initrd_node = bank->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 }
215
216 if (initrd_node == -1) {
Russell Kingb962a282008-07-30 21:24:56 +0100217 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 "physical memory - disabling initrd\n",
Russell Kingb962a282008-07-30 21:24:56 +0100219 phys_initrd_start, phys_initrd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 phys_initrd_start = phys_initrd_size = 0;
221 }
222#endif
223
224 return initrd_node;
225}
226
Russell King456335e2006-09-27 10:00:54 +0100227static inline void map_memory_bank(struct membank *bank)
228{
Russell Kingd111e8f2006-09-27 15:27:33 +0100229#ifdef CONFIG_MMU
Russell King456335e2006-09-27 10:00:54 +0100230 struct map_desc map;
231
Russell Kingd2a38ef2008-10-01 16:56:15 +0100232 map.pfn = bank_pfn_start(bank);
233 map.virtual = __phys_to_virt(bank_phys_start(bank));
234 map.length = bank_phys_size(bank);
Russell King456335e2006-09-27 10:00:54 +0100235 map.type = MT_MEMORY;
236
237 create_mapping(&map);
Russell Kingd111e8f2006-09-27 15:27:33 +0100238#endif
Russell King456335e2006-09-27 10:00:54 +0100239}
240
Russell Kingdde58282009-08-15 12:36:00 +0100241static void __init bootmem_init_node(int node, struct meminfo *mi,
242 unsigned long start_pfn, unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Russell Kingdde58282009-08-15 12:36:00 +0100244 unsigned long boot_pfn;
Russell King90072052005-10-28 14:48:37 +0100245 unsigned int boot_pages;
246 pg_data_t *pgdat;
247 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /*
Russell Kingdde58282009-08-15 12:36:00 +0100250 * Map the memory banks for this node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
Russell King90072052005-10-28 14:48:37 +0100252 for_each_nodebank(i, mi, node) {
Russell King456335e2006-09-27 10:00:54 +0100253 struct membank *bank = &mi->bank[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Russell Kingdde58282009-08-15 12:36:00 +0100255 if (!bank->highmem)
256 map_memory_bank(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Russell King90072052005-10-28 14:48:37 +0100259 /*
Russell King90072052005-10-28 14:48:37 +0100260 * Allocate the bootmem bitmap page.
261 */
262 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
263 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
264
265 /*
266 * Initialise the bootmem allocator for this node, handing the
267 * memory banks over to bootmem.
268 */
269 node_set_online(node);
270 pgdat = NODE_DATA(node);
271 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
272
Russell Kingd2a38ef2008-10-01 16:56:15 +0100273 for_each_nodebank(i, mi, node) {
274 struct membank *bank = &mi->bank[i];
Russell Kingdde58282009-08-15 12:36:00 +0100275 if (!bank->highmem)
276 free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
Russell Kingd2a38ef2008-10-01 16:56:15 +0100277 }
Russell King90072052005-10-28 14:48:37 +0100278
279 /*
280 * Reserve the bootmem bitmap for this node.
281 */
282 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800283 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
Russell Kingb7a69ac2008-10-01 16:58:32 +0100284}
Russell Kingb962a282008-07-30 21:24:56 +0100285
Russell Kingb7a69ac2008-10-01 16:58:32 +0100286static void __init bootmem_reserve_initrd(int node)
287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#ifdef CONFIG_BLK_DEV_INITRD
Russell Kingb7a69ac2008-10-01 16:58:32 +0100289 pg_data_t *pgdat = NODE_DATA(node);
290 int res;
Russell Kingb962a282008-07-30 21:24:56 +0100291
Russell Kingb7a69ac2008-10-01 16:58:32 +0100292 res = reserve_bootmem_node(pgdat, phys_initrd_start,
293 phys_initrd_size, BOOTMEM_EXCLUSIVE);
294
295 if (res == 0) {
296 initrd_start = __phys_to_virt(phys_initrd_start);
297 initrd_end = initrd_start + phys_initrd_size;
298 } else {
299 printk(KERN_ERR
300 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
301 "memory region - disabling initrd\n",
302 phys_initrd_start, phys_initrd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304#endif
Russell Kingb7a69ac2008-10-01 16:58:32 +0100305}
306
307static void __init bootmem_free_node(int node, struct meminfo *mi)
308{
309 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
Russell Kingdde58282009-08-15 12:36:00 +0100310 unsigned long min, max_low, max_high;
Russell Kingb7a69ac2008-10-01 16:58:32 +0100311 int i;
312
Russell Kingdde58282009-08-15 12:36:00 +0100313 find_node_limits(node, mi, &min, &max_low, &max_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Russell King90072052005-10-28 14:48:37 +0100315 /*
Russell King90072052005-10-28 14:48:37 +0100316 * initialise the zones within this node.
317 */
318 memset(zone_size, 0, sizeof(zone_size));
Russell King90072052005-10-28 14:48:37 +0100319
320 /*
321 * The size of this node has already been determined. If we need
322 * to do anything fancy with the allocation of this memory to the
323 * zones, now is the time to do it.
324 */
Russell Kingdde58282009-08-15 12:36:00 +0100325 zone_size[0] = max_low - min;
326#ifdef CONFIG_HIGHMEM
327 zone_size[ZONE_HIGHMEM] = max_high - max_low;
328#endif
Russell King90072052005-10-28 14:48:37 +0100329
330 /*
331 * For each bank in this node, calculate the size of the holes.
332 * holes = node_size - sum(bank_sizes_in_node)
333 */
Russell Kingdde58282009-08-15 12:36:00 +0100334 memcpy(zhole_size, zone_size, sizeof(zhole_size));
335 for_each_nodebank(i, mi, node) {
336 int idx = 0;
337#ifdef CONFIG_HIGHMEM
338 if (mi->bank[i].highmem)
339 idx = ZONE_HIGHMEM;
340#endif
341 zhole_size[idx] -= bank_pfn_size(&mi->bank[i]);
342 }
Russell King90072052005-10-28 14:48:37 +0100343
344 /*
345 * Adjust the sizes according to any special requirements for
346 * this machine type.
347 */
348 arch_adjust_zones(node, zone_size, zhole_size);
349
Russell Kingdde58282009-08-15 12:36:00 +0100350 free_area_init_node(node, zone_size, min, zhole_size);
Russell King90072052005-10-28 14:48:37 +0100351}
352
Russell Kingb7cfda92009-09-07 15:06:42 +0100353#ifndef CONFIG_SPARSEMEM
354int pfn_valid(unsigned long pfn)
355{
356 struct meminfo *mi = &meminfo;
357 unsigned int left = 0, right = mi->nr_banks;
358
359 do {
360 unsigned int mid = (right + left) / 2;
361 struct membank *bank = &mi->bank[mid];
362
363 if (pfn < bank_pfn_start(bank))
364 right = mid;
365 else if (pfn >= bank_pfn_end(bank))
366 left = mid + 1;
367 else
368 return 1;
369 } while (left < right);
370 return 0;
371}
372EXPORT_SYMBOL(pfn_valid);
Russell King657e12f2009-10-29 17:06:17 +0000373
374static void arm_memory_present(struct meminfo *mi, int node)
375{
376}
377#else
378static void arm_memory_present(struct meminfo *mi, int node)
379{
380 int i;
381 for_each_nodebank(i, mi, node) {
382 struct membank *bank = &mi->bank[i];
383 memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
384 }
385}
Russell Kingb7cfda92009-09-07 15:06:42 +0100386#endif
387
388static int __init meminfo_cmp(const void *_a, const void *_b)
389{
390 const struct membank *a = _a, *b = _b;
391 long cmp = bank_pfn_start(a) - bank_pfn_start(b);
392 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
393}
394
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -0400395void __init bootmem_init(void)
Russell King90072052005-10-28 14:48:37 +0100396{
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -0400397 struct meminfo *mi = &meminfo;
Russell Kingdde58282009-08-15 12:36:00 +0100398 unsigned long min, max_low, max_high;
Russell Kingeca73212008-09-30 19:29:25 +0100399 int node, initrd_node;
Russell King90072052005-10-28 14:48:37 +0100400
Russell Kingb7cfda92009-09-07 15:06:42 +0100401 sort(&mi->bank, mi->nr_banks, sizeof(mi->bank[0]), meminfo_cmp, NULL);
402
Russell King90072052005-10-28 14:48:37 +0100403 /*
404 * Locate which node contains the ramdisk image, if any.
405 */
406 initrd_node = check_initrd(mi);
407
Russell Kingdde58282009-08-15 12:36:00 +0100408 max_low = max_high = 0;
409
Russell King90072052005-10-28 14:48:37 +0100410 /*
411 * Run through each node initialising the bootmem allocator.
412 */
413 for_each_node(node) {
Russell Kingdde58282009-08-15 12:36:00 +0100414 unsigned long node_low, node_high;
415
416 find_node_limits(node, mi, &min, &node_low, &node_high);
417
418 if (node_low > max_low)
419 max_low = node_low;
420 if (node_high > max_high)
421 max_high = node_high;
422
423 /*
424 * If there is no memory in this node, ignore it.
425 * (We can't have nodes which have no lowmem)
426 */
427 if (node_low == 0)
428 continue;
429
430 bootmem_init_node(node, mi, min, node_low);
Russell King90072052005-10-28 14:48:37 +0100431
Russell Kingb7a69ac2008-10-01 16:58:32 +0100432 /*
433 * Reserve any special node zero regions.
434 */
435 if (node == 0)
436 reserve_node_zero(NODE_DATA(node));
437
438 /*
439 * If the initrd is in this node, reserve its memory.
440 */
441 if (node == initrd_node)
442 bootmem_reserve_initrd(node);
Russell King657e12f2009-10-29 17:06:17 +0000443
444 /*
445 * Sparsemem tries to allocate bootmem in memory_present(),
446 * so must be done after the fixed reservations
447 */
448 arm_memory_present(mi, node);
Russell King90072052005-10-28 14:48:37 +0100449 }
450
Russell Kingb7a69ac2008-10-01 16:58:32 +0100451 /*
452 * sparse_init() needs the bootmem allocator up and running.
453 */
454 sparse_init();
455
456 /*
457 * Now free memory in each node - free_area_init_node needs
458 * the sparse mem_map arrays initialized by sparse_init()
459 * for memmap_init_zone(), otherwise all PFNs are invalid.
460 */
461 for_each_node(node)
462 bootmem_free_node(node, mi);
463
Russell Kingdde58282009-08-15 12:36:00 +0100464 high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
Russell King90072052005-10-28 14:48:37 +0100465
466 /*
467 * This doesn't seem to be used by the Linux memory manager any
468 * more, but is used by ll_rw_block. If we can get rid of it, we
469 * also get rid of some of the stuff above as well.
470 *
471 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
472 * the system, not the maximum PFN.
473 */
Russell Kingdde58282009-08-15 12:36:00 +0100474 max_low_pfn = max_low - PHYS_PFN_OFFSET;
475 max_pfn = max_high - PHYS_PFN_OFFSET;
Russell King90072052005-10-28 14:48:37 +0100476}
477
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400478static inline int free_area(unsigned long pfn, unsigned long end, char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400480 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400482 for (; pfn < end; pfn++) {
483 struct page *page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800485 init_page_count(page);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400486 __free_page(page);
487 pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 if (size && s)
491 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400492
493 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Russell Kinga0130532005-06-27 14:16:47 +0100496static inline void
497free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
498{
499 struct page *start_pg, *end_pg;
500 unsigned long pg, pgend;
501
502 /*
503 * Convert start_pfn/end_pfn to a struct page pointer.
504 */
Catalin Marinas3257f432009-10-06 17:57:22 +0100505 start_pg = pfn_to_page(start_pfn - 1) + 1;
Russell Kinga0130532005-06-27 14:16:47 +0100506 end_pg = pfn_to_page(end_pfn);
507
508 /*
509 * Convert to physical addresses, and
510 * round start upwards and end downwards.
511 */
512 pg = PAGE_ALIGN(__pa(start_pg));
513 pgend = __pa(end_pg) & PAGE_MASK;
514
515 /*
516 * If there are free pages between these,
517 * free the section of the memmap array.
518 */
519 if (pg < pgend)
520 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
521}
522
523/*
524 * The mem_map array can get very big. Free the unused area of the memory map.
525 */
526static void __init free_unused_memmap_node(int node, struct meminfo *mi)
527{
528 unsigned long bank_start, prev_bank_end = 0;
529 unsigned int i;
530
531 /*
532 * [FIXME] This relies on each bank being in address order. This
533 * may not be the case, especially if the user has provided the
534 * information on the command line.
535 */
Russell King90072052005-10-28 14:48:37 +0100536 for_each_nodebank(i, mi, node) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100537 struct membank *bank = &mi->bank[i];
538
539 bank_start = bank_pfn_start(bank);
Russell Kinga0130532005-06-27 14:16:47 +0100540 if (bank_start < prev_bank_end) {
541 printk(KERN_ERR "MEM: unordered memory banks. "
542 "Not freeing memmap.\n");
543 break;
544 }
545
546 /*
547 * If we had a previous bank, and there is a space
548 * between the current bank and the previous, free it.
549 */
550 if (prev_bank_end && prev_bank_end != bank_start)
551 free_memmap(node, prev_bank_end, bank_start);
552
Russell Kingd2a38ef2008-10-01 16:56:15 +0100553 prev_bank_end = bank_pfn_end(bank);
Russell Kinga0130532005-06-27 14:16:47 +0100554 }
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/*
558 * mem_init() marks the free areas in the mem_map and tells us how much
559 * memory is free. This is done after various parts of the system have
560 * claimed their memory after the kernel image.
561 */
562void __init mem_init(void)
563{
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100564 unsigned long reserved_pages, free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int i, node;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#ifndef CONFIG_DISCONTIGMEM
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400568 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#endif
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* this will put all unused low memory onto the freelists */
572 for_each_online_node(node) {
573 pg_data_t *pgdat = NODE_DATA(node);
574
Russell Kinga0130532005-06-27 14:16:47 +0100575 free_unused_memmap_node(node, &meminfo);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (pgdat->node_spanned_pages != 0)
578 totalram_pages += free_all_bootmem_node(pgdat);
579 }
580
581#ifdef CONFIG_SA1111
582 /* now that our DMA memory is actually so designated, we can free it */
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400583 totalram_pages += free_area(PHYS_PFN_OFFSET,
584 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#endif
586
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400587#ifdef CONFIG_HIGHMEM
588 /* set highmem page free */
589 for_each_online_node(node) {
590 for_each_nodebank (i, &meminfo, node) {
591 unsigned long start = bank_pfn_start(&meminfo.bank[i]);
592 unsigned long end = bank_pfn_end(&meminfo.bank[i]);
593 if (start >= max_low_pfn + PHYS_PFN_OFFSET)
594 totalhigh_pages += free_area(start, end, NULL);
595 }
596 }
597 totalram_pages += totalhigh_pages;
598#endif
599
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100600 reserved_pages = free_pages = 0;
601
602 for_each_online_node(node) {
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100603 for_each_nodebank(i, &meminfo, node) {
604 struct membank *bank = &meminfo.bank[i];
605 unsigned int pfn1, pfn2;
606 struct page *page, *end;
607
608 pfn1 = bank_pfn_start(bank);
609 pfn2 = bank_pfn_end(bank);
610
Catalin Marinasea056df2010-05-04 17:27:43 +0100611 page = pfn_to_page(pfn1);
612 end = pfn_to_page(pfn2 - 1) + 1;
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100613
614 do {
615 if (PageReserved(page))
616 reserved_pages++;
617 else if (!page_count(page))
618 free_pages++;
619 page++;
620 } while (page < end);
621 }
622 }
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
625 * Since our memory may not be contiguous, calculate the
626 * real number of pages we have in this system
627 */
628 printk(KERN_INFO "Memory:");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 num_physpages = 0;
630 for (i = 0; i < meminfo.nr_banks; i++) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100631 num_physpages += bank_pfn_size(&meminfo.bank[i]);
632 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400635
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100636 printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
637 nr_free_pages() << (PAGE_SHIFT-10),
638 free_pages << (PAGE_SHIFT-10),
639 reserved_pages << (PAGE_SHIFT-10),
Andreas Fenkart4b529402010-01-08 14:42:31 -0800640 totalhigh_pages << (PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100642#define MLK(b, t) b, t, ((t) - (b)) >> 10
643#define MLM(b, t) b, t, ((t) - (b)) >> 20
644#define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
645
646 printk(KERN_NOTICE "Virtual kernel memory layout:\n"
647 " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
648 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Fenkart/Bostandzhyana7bd08c2010-02-07 21:46:33 +0100649#ifdef CONFIG_MMU
650 " DMA : 0x%08lx - 0x%08lx (%4ld MB)\n"
651#endif
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100652 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
653 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
654#ifdef CONFIG_HIGHMEM
655 " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n"
656#endif
657 " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
658 " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
659 " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
660 " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
661
662 MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
663 (PAGE_SIZE)),
664 MLK(FIXADDR_START, FIXADDR_TOP),
Fenkart/Bostandzhyana7bd08c2010-02-07 21:46:33 +0100665#ifdef CONFIG_MMU
666 MLM(CONSISTENT_BASE, CONSISTENT_END),
667#endif
Fenkart/Bostandzhyanc931b4f2010-02-07 21:47:17 +0100668 MLM(VMALLOC_START, VMALLOC_END),
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100669 MLM(PAGE_OFFSET, (unsigned long)high_memory),
670#ifdef CONFIG_HIGHMEM
671 MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
672 (PAGE_SIZE)),
673#endif
674 MLM(MODULES_VADDR, MODULES_END),
675
676 MLK_ROUNDUP(__init_begin, __init_end),
677 MLK_ROUNDUP(_text, _etext),
678 MLK_ROUNDUP(_data, _edata));
679
680#undef MLK
681#undef MLM
682#undef MLK_ROUNDUP
683
Fenkart/Bostandzhyana1839272010-02-07 21:47:58 +0100684 /*
685 * Check boundaries twice: Some fundamental inconsistencies can
686 * be detected at build time already.
687 */
688#ifdef CONFIG_MMU
689 BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
690 BUG_ON(VMALLOC_END > CONSISTENT_BASE);
691
692 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
693 BUG_ON(TASK_SIZE > MODULES_VADDR);
694#endif
695
696#ifdef CONFIG_HIGHMEM
697 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
698 BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
699#endif
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
702 extern int sysctl_overcommit_memory;
703 /*
704 * On a machine this small we won't get
705 * anywhere without overcommit, so turn
706 * it on by default.
707 */
708 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
709 }
710}
711
712void free_initmem(void)
713{
Linus Walleijbc581772009-09-15 17:30:37 +0100714#ifdef CONFIG_HAVE_TCM
715 extern char *__tcm_start, *__tcm_end;
716
717 totalram_pages += free_area(__phys_to_pfn(__pa(__tcm_start)),
718 __phys_to_pfn(__pa(__tcm_end)),
719 "TCM link");
720#endif
721
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400722 if (!machine_is_integrator() && !machine_is_cintegrator())
Russell King37efe642008-12-01 11:53:07 +0000723 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
724 __phys_to_pfn(__pa(__init_end)),
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400725 "init");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728#ifdef CONFIG_BLK_DEV_INITRD
729
730static int keep_initrd;
731
732void free_initrd_mem(unsigned long start, unsigned long end)
733{
734 if (!keep_initrd)
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400735 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
736 __phys_to_pfn(__pa(end)),
737 "initrd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740static int __init keepinitrd_setup(char *__unused)
741{
742 keep_initrd = 1;
743 return 1;
744}
745
746__setup("keepinitrd", keepinitrd_setup);
747#endif