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