[ARM] mem_init(): make highmem pages available for use
[deliverable/linux.git] / arch / arm / mm / init.c
CommitLineData
1da177e4
LT
1/*
2 * linux/arch/arm/mm/init.c
3 *
90072059 4 * Copyright (C) 1995-2005 Russell King
1da177e4
LT
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
1da177e4
LT
10#include <linux/kernel.h>
11#include <linux/errno.h>
1da177e4
LT
12#include <linux/swap.h>
13#include <linux/init.h>
14#include <linux/bootmem.h>
15#include <linux/mman.h>
16#include <linux/nodemask.h>
17#include <linux/initrd.h>
3835f6cb 18#include <linux/highmem.h>
1da177e4
LT
19
20#include <asm/mach-types.h>
37efe642 21#include <asm/sections.h>
1da177e4 22#include <asm/setup.h>
74d02fb9 23#include <asm/sizes.h>
1da177e4
LT
24#include <asm/tlb.h>
25
26#include <asm/mach/arch.h>
27#include <asm/mach/map.h>
28
1b2e2b73
RK
29#include "mm.h"
30
012d1f4a
RK
31static unsigned long phys_initrd_start __initdata = 0;
32static unsigned long phys_initrd_size __initdata = 0;
33
34static void __init early_initrd(char **p)
35{
36 unsigned long start, size;
37
38 start = memparse(*p, p);
39 if (**p == ',') {
40 size = memparse((*p) + 1, p);
41
42 phys_initrd_start = start;
43 phys_initrd_size = size;
44 }
45}
46__early_param("initrd=", early_initrd);
47
48static int __init parse_tag_initrd(const struct tag *tag)
49{
50 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
51 "please update your bootloader.\n");
52 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
53 phys_initrd_size = tag->u.initrd.size;
54 return 0;
55}
56
57__tagtable(ATAG_INITRD, parse_tag_initrd);
58
59static int __init parse_tag_initrd2(const struct tag *tag)
60{
61 phys_initrd_start = tag->u.initrd.start;
62 phys_initrd_size = tag->u.initrd.size;
63 return 0;
64}
65
66__tagtable(ATAG_INITRD2, parse_tag_initrd2);
1da177e4
LT
67
68/*
4b5f32ce
NP
69 * This keeps memory configuration data used by a couple memory
70 * initialization functions, as well as show_mem() for the skipping
71 * of holes in the memory map. It is populated by arm_add_memory().
1da177e4 72 */
4b5f32ce 73struct meminfo meminfo;
5e709827 74
1da177e4
LT
75void show_mem(void)
76{
77 int free = 0, total = 0, reserved = 0;
5e709827
RL
78 int shared = 0, cached = 0, slab = 0, node, i;
79 struct meminfo * mi = &meminfo;
1da177e4
LT
80
81 printk("Mem-info:\n");
82 show_free_areas();
1da177e4 83 for_each_online_node(node) {
204ecae4 84 pg_data_t *n = NODE_DATA(node);
b7a69ac3 85 struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn;
204ecae4 86
5e709827 87 for_each_nodebank (i,mi,node) {
d2a38ef9 88 struct membank *bank = &mi->bank[i];
5e709827
RL
89 unsigned int pfn1, pfn2;
90 struct page *page, *end;
91
d2a38ef9
RK
92 pfn1 = bank_pfn_start(bank);
93 pfn2 = bank_pfn_end(bank);
5e709827 94
204ecae4
RK
95 page = map + pfn1;
96 end = map + pfn2;
5e709827
RL
97
98 do {
99 total++;
100 if (PageReserved(page))
101 reserved++;
102 else if (PageSwapCache(page))
103 cached++;
104 else if (PageSlab(page))
105 slab++;
106 else if (!page_count(page))
107 free++;
108 else
109 shared += page_count(page) - 1;
110 page++;
111 } while (page < end);
112 }
1da177e4
LT
113 }
114
115 printk("%d pages of RAM\n", total);
116 printk("%d free pages\n", free);
117 printk("%d reserved pages\n", reserved);
118 printk("%d slab pages\n", slab);
119 printk("%d pages shared\n", shared);
120 printk("%d pages swap cached\n", cached);
121}
122
1da177e4
LT
123/*
124 * FIXME: We really want to avoid allocating the bootmap bitmap
125 * over the top of the initrd. Hopefully, this is located towards
126 * the start of a bank, so if we allocate the bootmap bitmap at
127 * the end, we won't clash.
128 */
129static unsigned int __init
130find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
131{
d2a38ef9 132 unsigned int start_pfn, i, bootmap_pfn;
1da177e4 133
37efe642 134 start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
1da177e4
LT
135 bootmap_pfn = 0;
136
d2a38ef9
RK
137 for_each_nodebank(i, mi, node) {
138 struct membank *bank = &mi->bank[i];
1da177e4
LT
139 unsigned int start, end;
140
d2a38ef9
RK
141 start = bank_pfn_start(bank);
142 end = bank_pfn_end(bank);
1da177e4
LT
143
144 if (end < start_pfn)
145 continue;
146
147 if (start < start_pfn)
148 start = start_pfn;
149
150 if (end <= start)
151 continue;
152
153 if (end - start >= bootmap_pages) {
154 bootmap_pfn = start;
155 break;
156 }
157 }
158
159 if (bootmap_pfn == 0)
160 BUG();
161
162 return bootmap_pfn;
163}
164
1da177e4
LT
165static int __init check_initrd(struct meminfo *mi)
166{
167 int initrd_node = -2;
168#ifdef CONFIG_BLK_DEV_INITRD
169 unsigned long end = phys_initrd_start + phys_initrd_size;
170
171 /*
172 * Make sure that the initrd is within a valid area of
173 * memory.
174 */
175 if (phys_initrd_size) {
176 unsigned int i;
177
178 initrd_node = -1;
179
180 for (i = 0; i < mi->nr_banks; i++) {
d2a38ef9
RK
181 struct membank *bank = &mi->bank[i];
182 if (bank_phys_start(bank) <= phys_initrd_start &&
183 end <= bank_phys_end(bank))
184 initrd_node = bank->node;
1da177e4
LT
185 }
186 }
187
188 if (initrd_node == -1) {
b962a286 189 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
1da177e4 190 "physical memory - disabling initrd\n",
b962a286 191 phys_initrd_start, phys_initrd_size);
1da177e4
LT
192 phys_initrd_start = phys_initrd_size = 0;
193 }
194#endif
195
196 return initrd_node;
197}
198
456335e2
RK
199static inline void map_memory_bank(struct membank *bank)
200{
d111e8f9 201#ifdef CONFIG_MMU
456335e2
RK
202 struct map_desc map;
203
d2a38ef9
RK
204 map.pfn = bank_pfn_start(bank);
205 map.virtual = __phys_to_virt(bank_phys_start(bank));
206 map.length = bank_phys_size(bank);
456335e2
RK
207 map.type = MT_MEMORY;
208
209 create_mapping(&map);
d111e8f9 210#endif
456335e2
RK
211}
212
b7a69ac3 213static unsigned long __init bootmem_init_node(int node, struct meminfo *mi)
1da177e4 214{
90072059
RK
215 unsigned long start_pfn, end_pfn, boot_pfn;
216 unsigned int boot_pages;
217 pg_data_t *pgdat;
218 int i;
1da177e4 219
90072059
RK
220 start_pfn = -1UL;
221 end_pfn = 0;
1da177e4 222
90072059
RK
223 /*
224 * Calculate the pfn range, and map the memory banks for this node.
225 */
226 for_each_nodebank(i, mi, node) {
456335e2 227 struct membank *bank = &mi->bank[i];
90072059 228 unsigned long start, end;
1da177e4 229
d2a38ef9
RK
230 start = bank_pfn_start(bank);
231 end = bank_pfn_end(bank);
1da177e4 232
90072059
RK
233 if (start_pfn > start)
234 start_pfn = start;
235 if (end_pfn < end)
236 end_pfn = end;
237
456335e2 238 map_memory_bank(bank);
90072059 239 }
1da177e4
LT
240
241 /*
90072059 242 * If there is no memory in this node, ignore it.
1da177e4 243 */
90072059
RK
244 if (end_pfn == 0)
245 return end_pfn;
1da177e4 246
90072059
RK
247 /*
248 * Allocate the bootmem bitmap page.
249 */
250 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
251 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
1da177e4 252
90072059
RK
253 /*
254 * Initialise the bootmem allocator for this node, handing the
255 * memory banks over to bootmem.
256 */
257 node_set_online(node);
258 pgdat = NODE_DATA(node);
259 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
1da177e4 260
d2a38ef9
RK
261 for_each_nodebank(i, mi, node) {
262 struct membank *bank = &mi->bank[i];
263 free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
b7a69ac3 264 memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
d2a38ef9 265 }
90072059
RK
266
267 /*
268 * Reserve the bootmem bitmap for this node.
269 */
270 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
72a7fe39 271 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
1da177e4 272
b7a69ac3
RK
273 return end_pfn;
274}
b962a286 275
b7a69ac3
RK
276static void __init bootmem_reserve_initrd(int node)
277{
1da177e4 278#ifdef CONFIG_BLK_DEV_INITRD
b7a69ac3
RK
279 pg_data_t *pgdat = NODE_DATA(node);
280 int res;
281
282 res = reserve_bootmem_node(pgdat, phys_initrd_start,
283 phys_initrd_size, BOOTMEM_EXCLUSIVE);
284
285 if (res == 0) {
286 initrd_start = __phys_to_virt(phys_initrd_start);
287 initrd_end = initrd_start + phys_initrd_size;
288 } else {
289 printk(KERN_ERR
290 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
291 "memory region - disabling initrd\n",
292 phys_initrd_start, phys_initrd_size);
1da177e4
LT
293 }
294#endif
b7a69ac3
RK
295}
296
297static void __init bootmem_free_node(int node, struct meminfo *mi)
298{
299 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
300 unsigned long start_pfn, end_pfn;
301 pg_data_t *pgdat = NODE_DATA(node);
302 int i;
303
304 start_pfn = pgdat->bdata->node_min_pfn;
305 end_pfn = pgdat->bdata->node_low_pfn;
1da177e4 306
90072059
RK
307 /*
308 * initialise the zones within this node.
309 */
310 memset(zone_size, 0, sizeof(zone_size));
311 memset(zhole_size, 0, sizeof(zhole_size));
312
313 /*
314 * The size of this node has already been determined. If we need
315 * to do anything fancy with the allocation of this memory to the
316 * zones, now is the time to do it.
317 */
318 zone_size[0] = end_pfn - start_pfn;
319
320 /*
321 * For each bank in this node, calculate the size of the holes.
322 * holes = node_size - sum(bank_sizes_in_node)
323 */
324 zhole_size[0] = zone_size[0];
325 for_each_nodebank(i, mi, node)
d2a38ef9 326 zhole_size[0] -= bank_pfn_size(&mi->bank[i]);
90072059
RK
327
328 /*
329 * Adjust the sizes according to any special requirements for
330 * this machine type.
331 */
332 arch_adjust_zones(node, zone_size, zhole_size);
333
9109fb7b 334 free_area_init_node(node, zone_size, start_pfn, zhole_size);
1da177e4
LT
335}
336
4b5f32ce 337void __init bootmem_init(void)
1da177e4 338{
4b5f32ce 339 struct meminfo *mi = &meminfo;
456335e2 340 unsigned long memend_pfn = 0;
eca73214 341 int node, initrd_node;
1da177e4 342
1da177e4 343 /*
90072059 344 * Locate which node contains the ramdisk image, if any.
1da177e4 345 */
90072059 346 initrd_node = check_initrd(mi);
1da177e4 347
90072059
RK
348 /*
349 * Run through each node initialising the bootmem allocator.
350 */
351 for_each_node(node) {
b7a69ac3 352 unsigned long end_pfn = bootmem_init_node(node, mi);
1da177e4 353
b7a69ac3
RK
354 /*
355 * Reserve any special node zero regions.
356 */
357 if (node == 0)
358 reserve_node_zero(NODE_DATA(node));
359
360 /*
361 * If the initrd is in this node, reserve its memory.
362 */
363 if (node == initrd_node)
364 bootmem_reserve_initrd(node);
1da177e4
LT
365
366 /*
90072059 367 * Remember the highest memory PFN.
1da177e4 368 */
90072059
RK
369 if (end_pfn > memend_pfn)
370 memend_pfn = end_pfn;
371 }
1da177e4 372
b7a69ac3
RK
373 /*
374 * sparse_init() needs the bootmem allocator up and running.
375 */
376 sparse_init();
377
378 /*
379 * Now free memory in each node - free_area_init_node needs
380 * the sparse mem_map arrays initialized by sparse_init()
381 * for memmap_init_zone(), otherwise all PFNs are invalid.
382 */
383 for_each_node(node)
384 bootmem_free_node(node, mi);
385
1522ac3e 386 high_memory = __va((memend_pfn << PAGE_SHIFT) - 1) + 1;
1da177e4 387
90072059
RK
388 /*
389 * This doesn't seem to be used by the Linux memory manager any
390 * more, but is used by ll_rw_block. If we can get rid of it, we
391 * also get rid of some of the stuff above as well.
392 *
393 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
394 * the system, not the maximum PFN.
395 */
396 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
397}
1da177e4 398
6db015e4 399static inline int free_area(unsigned long pfn, unsigned long end, char *s)
1da177e4 400{
6db015e4 401 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
1da177e4 402
6db015e4
NP
403 for (; pfn < end; pfn++) {
404 struct page *page = pfn_to_page(pfn);
1da177e4 405 ClearPageReserved(page);
7835e98b 406 init_page_count(page);
6db015e4
NP
407 __free_page(page);
408 pages++;
1da177e4
LT
409 }
410
411 if (size && s)
412 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
6db015e4
NP
413
414 return pages;
1da177e4
LT
415}
416
a013053d
RK
417static inline void
418free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
419{
420 struct page *start_pg, *end_pg;
421 unsigned long pg, pgend;
422
423 /*
424 * Convert start_pfn/end_pfn to a struct page pointer.
425 */
426 start_pg = pfn_to_page(start_pfn);
427 end_pg = pfn_to_page(end_pfn);
428
429 /*
430 * Convert to physical addresses, and
431 * round start upwards and end downwards.
432 */
433 pg = PAGE_ALIGN(__pa(start_pg));
434 pgend = __pa(end_pg) & PAGE_MASK;
435
436 /*
437 * If there are free pages between these,
438 * free the section of the memmap array.
439 */
440 if (pg < pgend)
441 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
442}
443
444/*
445 * The mem_map array can get very big. Free the unused area of the memory map.
446 */
447static void __init free_unused_memmap_node(int node, struct meminfo *mi)
448{
449 unsigned long bank_start, prev_bank_end = 0;
450 unsigned int i;
451
452 /*
453 * [FIXME] This relies on each bank being in address order. This
454 * may not be the case, especially if the user has provided the
455 * information on the command line.
456 */
90072059 457 for_each_nodebank(i, mi, node) {
d2a38ef9
RK
458 struct membank *bank = &mi->bank[i];
459
460 bank_start = bank_pfn_start(bank);
a013053d
RK
461 if (bank_start < prev_bank_end) {
462 printk(KERN_ERR "MEM: unordered memory banks. "
463 "Not freeing memmap.\n");
464 break;
465 }
466
467 /*
468 * If we had a previous bank, and there is a space
469 * between the current bank and the previous, free it.
470 */
471 if (prev_bank_end && prev_bank_end != bank_start)
472 free_memmap(node, prev_bank_end, bank_start);
473
d2a38ef9 474 prev_bank_end = bank_pfn_end(bank);
a013053d
RK
475 }
476}
477
1da177e4
LT
478/*
479 * mem_init() marks the free areas in the mem_map and tells us how much
480 * memory is free. This is done after various parts of the system have
481 * claimed their memory after the kernel image.
482 */
483void __init mem_init(void)
484{
6db015e4 485 unsigned int codesize, datasize, initsize;
1da177e4
LT
486 int i, node;
487
1da177e4 488#ifndef CONFIG_DISCONTIGMEM
3835f6cb 489 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
1da177e4
LT
490#endif
491
1da177e4
LT
492 /* this will put all unused low memory onto the freelists */
493 for_each_online_node(node) {
494 pg_data_t *pgdat = NODE_DATA(node);
495
a013053d
RK
496 free_unused_memmap_node(node, &meminfo);
497
1da177e4
LT
498 if (pgdat->node_spanned_pages != 0)
499 totalram_pages += free_all_bootmem_node(pgdat);
500 }
501
502#ifdef CONFIG_SA1111
503 /* now that our DMA memory is actually so designated, we can free it */
6db015e4
NP
504 totalram_pages += free_area(PHYS_PFN_OFFSET,
505 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
1da177e4
LT
506#endif
507
3835f6cb
NP
508#ifdef CONFIG_HIGHMEM
509 /* set highmem page free */
510 for_each_online_node(node) {
511 for_each_nodebank (i, &meminfo, node) {
512 unsigned long start = bank_pfn_start(&meminfo.bank[i]);
513 unsigned long end = bank_pfn_end(&meminfo.bank[i]);
514 if (start >= max_low_pfn + PHYS_PFN_OFFSET)
515 totalhigh_pages += free_area(start, end, NULL);
516 }
517 }
518 totalram_pages += totalhigh_pages;
519#endif
520
1da177e4
LT
521 /*
522 * Since our memory may not be contiguous, calculate the
523 * real number of pages we have in this system
524 */
525 printk(KERN_INFO "Memory:");
1da177e4
LT
526 num_physpages = 0;
527 for (i = 0; i < meminfo.nr_banks; i++) {
d2a38ef9
RK
528 num_physpages += bank_pfn_size(&meminfo.bank[i]);
529 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
1da177e4 530 }
1da177e4 531 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
6db015e4 532
37efe642
RK
533 codesize = _etext - _text;
534 datasize = _end - _data;
535 initsize = __init_end - __init_begin;
6db015e4 536
1da177e4 537 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
3835f6cb 538 "%dK data, %dK init, %luK highmem)\n",
1da177e4 539 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
3835f6cb
NP
540 codesize >> 10, datasize >> 10, initsize >> 10,
541 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
1da177e4
LT
542
543 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
544 extern int sysctl_overcommit_memory;
545 /*
546 * On a machine this small we won't get
547 * anywhere without overcommit, so turn
548 * it on by default.
549 */
550 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
551 }
552}
553
554void free_initmem(void)
555{
6db015e4 556 if (!machine_is_integrator() && !machine_is_cintegrator())
37efe642
RK
557 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
558 __phys_to_pfn(__pa(__init_end)),
6db015e4 559 "init");
1da177e4
LT
560}
561
562#ifdef CONFIG_BLK_DEV_INITRD
563
564static int keep_initrd;
565
566void free_initrd_mem(unsigned long start, unsigned long end)
567{
568 if (!keep_initrd)
6db015e4
NP
569 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
570 __phys_to_pfn(__pa(end)),
571 "initrd");
1da177e4
LT
572}
573
574static int __init keepinitrd_setup(char *__unused)
575{
576 keep_initrd = 1;
577 return 1;
578}
579
580__setup("keepinitrd", keepinitrd_setup);
581#endif
This page took 0.41279 seconds and 5 git commands to generate.