blob: 30a69d67d673ed80f122900223249e94ec6a4890 [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>
18
19#include <asm/mach-types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/setup.h>
Russell King74d02fb2006-04-04 21:47:43 +010021#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/tlb.h>
23
24#include <asm/mach/arch.h>
25#include <asm/mach/map.h>
26
Russell King1b2e2b72006-08-21 17:06:38 +010027#include "mm.h"
28
Russell Kingd111e8f2006-09-27 15:27:33 +010029extern void _text, _etext, __data_start, _end, __init_begin, __init_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030extern unsigned long phys_initrd_start;
31extern unsigned long phys_initrd_size;
32
33/*
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010034 * This is used to pass memory configuration data from paging_init
35 * to mem_init, and by show_mem() to skip holes in the memory map.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010037static struct meminfo meminfo = { 0, };
38
39#define for_each_nodebank(iter,mi,no) \
40 for (iter = 0; iter < mi->nr_banks; iter++) \
41 if (mi->bank[iter].node == no)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043void show_mem(void)
44{
45 int free = 0, total = 0, reserved = 0;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010046 int shared = 0, cached = 0, slab = 0, node, i;
47 struct meminfo * mi = &meminfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 printk("Mem-info:\n");
50 show_free_areas();
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 for_each_online_node(node) {
Russell King204ecae2007-01-16 14:01:47 +000052 pg_data_t *n = NODE_DATA(node);
53 struct page *map = n->node_mem_map - n->node_start_pfn;
54
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010055 for_each_nodebank (i,mi,node) {
56 unsigned int pfn1, pfn2;
57 struct page *page, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Russell King204ecae2007-01-16 14:01:47 +000059 pfn1 = __phys_to_pfn(mi->bank[i].start);
60 pfn2 = __phys_to_pfn(mi->bank[i].size + mi->bank[i].start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Russell King204ecae2007-01-16 14:01:47 +000062 page = map + pfn1;
63 end = map + pfn2;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010064
65 do {
66 total++;
67 if (PageReserved(page))
68 reserved++;
69 else if (PageSwapCache(page))
70 cached++;
71 else if (PageSlab(page))
72 slab++;
73 else if (!page_count(page))
74 free++;
75 else
76 shared += page_count(page) - 1;
77 page++;
78 } while (page < end);
79 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
82 printk("%d pages of RAM\n", total);
83 printk("%d free pages\n", free);
84 printk("%d reserved pages\n", reserved);
85 printk("%d slab pages\n", slab);
86 printk("%d pages shared\n", shared);
87 printk("%d pages swap cached\n", cached);
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * FIXME: We really want to avoid allocating the bootmap bitmap
92 * over the top of the initrd. Hopefully, this is located towards
93 * the start of a bank, so if we allocate the bootmap bitmap at
94 * the end, we won't clash.
95 */
96static unsigned int __init
97find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
98{
99 unsigned int start_pfn, bank, bootmap_pfn;
100
Russell King90072052005-10-28 14:48:37 +0100101 start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 bootmap_pfn = 0;
103
Russell King90072052005-10-28 14:48:37 +0100104 for_each_nodebank(bank, mi, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int start, end;
106
Russell King92a8cbe2005-06-22 21:47:25 +0100107 start = mi->bank[bank].start >> PAGE_SHIFT;
108 end = (mi->bank[bank].size +
109 mi->bank[bank].start) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 if (end < start_pfn)
112 continue;
113
114 if (start < start_pfn)
115 start = start_pfn;
116
117 if (end <= start)
118 continue;
119
120 if (end - start >= bootmap_pages) {
121 bootmap_pfn = start;
122 break;
123 }
124 }
125
126 if (bootmap_pfn == 0)
127 BUG();
128
129 return bootmap_pfn;
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static int __init check_initrd(struct meminfo *mi)
133{
134 int initrd_node = -2;
135#ifdef CONFIG_BLK_DEV_INITRD
136 unsigned long end = phys_initrd_start + phys_initrd_size;
137
138 /*
139 * Make sure that the initrd is within a valid area of
140 * memory.
141 */
142 if (phys_initrd_size) {
143 unsigned int i;
144
145 initrd_node = -1;
146
147 for (i = 0; i < mi->nr_banks; i++) {
148 unsigned long bank_end;
149
150 bank_end = mi->bank[i].start + mi->bank[i].size;
151
152 if (mi->bank[i].start <= phys_initrd_start &&
153 end <= bank_end)
154 initrd_node = mi->bank[i].node;
155 }
156 }
157
158 if (initrd_node == -1) {
Russell Kingb962a282008-07-30 21:24:56 +0100159 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 "physical memory - disabling initrd\n",
Russell Kingb962a282008-07-30 21:24:56 +0100161 phys_initrd_start, phys_initrd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 phys_initrd_start = phys_initrd_size = 0;
163 }
164#endif
165
166 return initrd_node;
167}
168
Russell King456335e2006-09-27 10:00:54 +0100169static inline void map_memory_bank(struct membank *bank)
170{
Russell Kingd111e8f2006-09-27 15:27:33 +0100171#ifdef CONFIG_MMU
Russell King456335e2006-09-27 10:00:54 +0100172 struct map_desc map;
173
174 map.pfn = __phys_to_pfn(bank->start);
175 map.virtual = __phys_to_virt(bank->start);
176 map.length = bank->size;
177 map.type = MT_MEMORY;
178
179 create_mapping(&map);
Russell Kingd111e8f2006-09-27 15:27:33 +0100180#endif
Russell King456335e2006-09-27 10:00:54 +0100181}
182
Russell King90072052005-10-28 14:48:37 +0100183static unsigned long __init
184bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Russell King90072052005-10-28 14:48:37 +0100186 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
187 unsigned long start_pfn, end_pfn, boot_pfn;
188 unsigned int boot_pages;
189 pg_data_t *pgdat;
190 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Russell King90072052005-10-28 14:48:37 +0100192 start_pfn = -1UL;
193 end_pfn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /*
Russell King90072052005-10-28 14:48:37 +0100196 * Calculate the pfn range, and map the memory banks for this node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Russell King90072052005-10-28 14:48:37 +0100198 for_each_nodebank(i, mi, node) {
Russell King456335e2006-09-27 10:00:54 +0100199 struct membank *bank = &mi->bank[i];
Russell King90072052005-10-28 14:48:37 +0100200 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Russell King456335e2006-09-27 10:00:54 +0100202 start = bank->start >> PAGE_SHIFT;
203 end = (bank->start + bank->size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Russell King90072052005-10-28 14:48:37 +0100205 if (start_pfn > start)
206 start_pfn = start;
207 if (end_pfn < end)
208 end_pfn = end;
209
Russell King456335e2006-09-27 10:00:54 +0100210 map_memory_bank(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
Russell King90072052005-10-28 14:48:37 +0100213 /*
214 * If there is no memory in this node, ignore it.
215 */
216 if (end_pfn == 0)
217 return end_pfn;
218
219 /*
220 * Allocate the bootmem bitmap page.
221 */
222 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
223 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
224
225 /*
226 * Initialise the bootmem allocator for this node, handing the
227 * memory banks over to bootmem.
228 */
229 node_set_online(node);
230 pgdat = NODE_DATA(node);
231 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
232
233 for_each_nodebank(i, mi, node)
234 free_bootmem_node(pgdat, mi->bank[i].start, mi->bank[i].size);
235
236 /*
237 * Reserve the bootmem bitmap for this node.
238 */
239 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800240 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Russell Kingb962a282008-07-30 21:24:56 +0100242 /*
243 * Reserve any special node zero regions.
244 */
245 if (node == 0)
246 reserve_node_zero(pgdat);
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#ifdef CONFIG_BLK_DEV_INITRD
Russell King90072052005-10-28 14:48:37 +0100249 /*
250 * If the initrd is in this node, reserve its memory.
251 */
252 if (node == initrd_node) {
Russell Kingb962a282008-07-30 21:24:56 +0100253 int res = reserve_bootmem_node(pgdat, phys_initrd_start,
254 phys_initrd_size, BOOTMEM_EXCLUSIVE);
255
256 if (res == 0) {
257 initrd_start = __phys_to_virt(phys_initrd_start);
258 initrd_end = initrd_start + phys_initrd_size;
259 } else {
260 printk(KERN_ERR
261 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
262 "memory region - disabling initrd\n",
263 phys_initrd_start, phys_initrd_size);
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266#endif
267
Russell King90072052005-10-28 14:48:37 +0100268 /*
Russell King90072052005-10-28 14:48:37 +0100269 * initialise the zones within this node.
270 */
271 memset(zone_size, 0, sizeof(zone_size));
272 memset(zhole_size, 0, sizeof(zhole_size));
273
274 /*
275 * The size of this node has already been determined. If we need
276 * to do anything fancy with the allocation of this memory to the
277 * zones, now is the time to do it.
278 */
279 zone_size[0] = end_pfn - start_pfn;
280
281 /*
282 * For each bank in this node, calculate the size of the holes.
283 * holes = node_size - sum(bank_sizes_in_node)
284 */
285 zhole_size[0] = zone_size[0];
286 for_each_nodebank(i, mi, node)
287 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
288
289 /*
290 * Adjust the sizes according to any special requirements for
291 * this machine type.
292 */
293 arch_adjust_zones(node, zone_size, zhole_size);
294
Johannes Weiner9109fb72008-07-23 21:27:20 -0700295 free_area_init_node(node, zone_size, start_pfn, zhole_size);
Russell King90072052005-10-28 14:48:37 +0100296
297 return end_pfn;
298}
299
Russell Kingd111e8f2006-09-27 15:27:33 +0100300void __init bootmem_init(struct meminfo *mi)
Russell King90072052005-10-28 14:48:37 +0100301{
Russell King456335e2006-09-27 10:00:54 +0100302 unsigned long memend_pfn = 0;
Russell King90072052005-10-28 14:48:37 +0100303 int node, initrd_node, i;
304
305 /*
306 * Invalidate the node number for empty or invalid memory banks
307 */
308 for (i = 0; i < mi->nr_banks; i++)
309 if (mi->bank[i].size == 0 || mi->bank[i].node >= MAX_NUMNODES)
310 mi->bank[i].node = -1;
311
312 memcpy(&meminfo, mi, sizeof(meminfo));
313
Russell King90072052005-10-28 14:48:37 +0100314 /*
315 * Locate which node contains the ramdisk image, if any.
316 */
317 initrd_node = check_initrd(mi);
318
319 /*
320 * Run through each node initialising the bootmem allocator.
321 */
322 for_each_node(node) {
323 unsigned long end_pfn;
324
325 end_pfn = bootmem_init_node(node, initrd_node, mi);
326
327 /*
328 * Remember the highest memory PFN.
329 */
330 if (end_pfn > memend_pfn)
331 memend_pfn = end_pfn;
332 }
333
334 high_memory = __va(memend_pfn << PAGE_SHIFT);
335
336 /*
337 * This doesn't seem to be used by the Linux memory manager any
338 * more, but is used by ll_rw_block. If we can get rid of it, we
339 * also get rid of some of the stuff above as well.
340 *
341 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
342 * the system, not the maximum PFN.
343 */
344 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static inline void free_area(unsigned long addr, unsigned long end, char *s)
348{
349 unsigned int size = (end - addr) >> 10;
350
351 for (; addr < end; addr += PAGE_SIZE) {
352 struct page *page = virt_to_page(addr);
353 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800354 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 free_page(addr);
356 totalram_pages++;
357 }
358
359 if (size && s)
360 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
361}
362
Russell Kinga0130532005-06-27 14:16:47 +0100363static inline void
364free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
365{
366 struct page *start_pg, *end_pg;
367 unsigned long pg, pgend;
368
369 /*
370 * Convert start_pfn/end_pfn to a struct page pointer.
371 */
372 start_pg = pfn_to_page(start_pfn);
373 end_pg = pfn_to_page(end_pfn);
374
375 /*
376 * Convert to physical addresses, and
377 * round start upwards and end downwards.
378 */
379 pg = PAGE_ALIGN(__pa(start_pg));
380 pgend = __pa(end_pg) & PAGE_MASK;
381
382 /*
383 * If there are free pages between these,
384 * free the section of the memmap array.
385 */
386 if (pg < pgend)
387 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
388}
389
390/*
391 * The mem_map array can get very big. Free the unused area of the memory map.
392 */
393static void __init free_unused_memmap_node(int node, struct meminfo *mi)
394{
395 unsigned long bank_start, prev_bank_end = 0;
396 unsigned int i;
397
398 /*
399 * [FIXME] This relies on each bank being in address order. This
400 * may not be the case, especially if the user has provided the
401 * information on the command line.
402 */
Russell King90072052005-10-28 14:48:37 +0100403 for_each_nodebank(i, mi, node) {
Russell Kinga0130532005-06-27 14:16:47 +0100404 bank_start = mi->bank[i].start >> PAGE_SHIFT;
405 if (bank_start < prev_bank_end) {
406 printk(KERN_ERR "MEM: unordered memory banks. "
407 "Not freeing memmap.\n");
408 break;
409 }
410
411 /*
412 * If we had a previous bank, and there is a space
413 * between the current bank and the previous, free it.
414 */
415 if (prev_bank_end && prev_bank_end != bank_start)
416 free_memmap(node, prev_bank_end, bank_start);
417
418 prev_bank_end = (mi->bank[i].start +
419 mi->bank[i].size) >> PAGE_SHIFT;
420 }
421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
424 * mem_init() marks the free areas in the mem_map and tells us how much
425 * memory is free. This is done after various parts of the system have
426 * claimed their memory after the kernel image.
427 */
428void __init mem_init(void)
429{
430 unsigned int codepages, datapages, initpages;
431 int i, node;
432
433 codepages = &_etext - &_text;
434 datapages = &_end - &__data_start;
435 initpages = &__init_end - &__init_begin;
436
437#ifndef CONFIG_DISCONTIGMEM
438 max_mapnr = virt_to_page(high_memory) - mem_map;
439#endif
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* this will put all unused low memory onto the freelists */
442 for_each_online_node(node) {
443 pg_data_t *pgdat = NODE_DATA(node);
444
Russell Kinga0130532005-06-27 14:16:47 +0100445 free_unused_memmap_node(node, &meminfo);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (pgdat->node_spanned_pages != 0)
448 totalram_pages += free_all_bootmem_node(pgdat);
449 }
450
451#ifdef CONFIG_SA1111
452 /* now that our DMA memory is actually so designated, we can free it */
453 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
454#endif
455
456 /*
457 * Since our memory may not be contiguous, calculate the
458 * real number of pages we have in this system
459 */
460 printk(KERN_INFO "Memory:");
461
462 num_physpages = 0;
463 for (i = 0; i < meminfo.nr_banks; i++) {
464 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
465 printk(" %ldMB", meminfo.bank[i].size >> 20);
466 }
467
468 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
469 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
470 "%dK data, %dK init)\n",
471 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
472 codepages >> 10, datapages >> 10, initpages >> 10);
473
474 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
475 extern int sysctl_overcommit_memory;
476 /*
477 * On a machine this small we won't get
478 * anywhere without overcommit, so turn
479 * it on by default.
480 */
481 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
482 }
483}
484
485void free_initmem(void)
486{
487 if (!machine_is_integrator() && !machine_is_cintegrator()) {
488 free_area((unsigned long)(&__init_begin),
489 (unsigned long)(&__init_end),
490 "init");
491 }
492}
493
494#ifdef CONFIG_BLK_DEV_INITRD
495
496static int keep_initrd;
497
498void free_initrd_mem(unsigned long start, unsigned long end)
499{
500 if (!keep_initrd)
501 free_area(start, end, "initrd");
502}
503
504static int __init keepinitrd_setup(char *__unused)
505{
506 keep_initrd = 1;
507 return 1;
508}
509
510__setup("keepinitrd", keepinitrd_setup);
511#endif