blob: 22217fe2650b61276b82a280ea9eecc18b48b613 [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>
12#include <linux/ptrace.h>
13#include <linux/swap.h>
14#include <linux/init.h>
15#include <linux/bootmem.h>
16#include <linux/mman.h>
17#include <linux/nodemask.h>
18#include <linux/initrd.h>
19
20#include <asm/mach-types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/setup.h>
Russell King74d02fb2006-04-04 21:47:43 +010022#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/tlb.h>
24
25#include <asm/mach/arch.h>
26#include <asm/mach/map.h>
27
Russell King1b2e2b72006-08-21 17:06:38 +010028#include "mm.h"
29
Russell Kingd111e8f2006-09-27 15:27:33 +010030extern void _text, _etext, __data_start, _end, __init_begin, __init_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031extern unsigned long phys_initrd_start;
32extern unsigned long phys_initrd_size;
33
34/*
35 * The sole use of this is to pass memory configuration
36 * data from paging_init to mem_init.
37 */
38static struct meminfo meminfo __initdata = { 0, };
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040void show_mem(void)
41{
42 int free = 0, total = 0, reserved = 0;
43 int shared = 0, cached = 0, slab = 0, node;
44
45 printk("Mem-info:\n");
46 show_free_areas();
47 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
48
49 for_each_online_node(node) {
50 struct page *page, *end;
51
52 page = NODE_MEM_MAP(node);
53 end = page + NODE_DATA(node)->node_spanned_pages;
54
55 do {
56 total++;
57 if (PageReserved(page))
58 reserved++;
59 else if (PageSwapCache(page))
60 cached++;
61 else if (PageSlab(page))
62 slab++;
63 else if (!page_count(page))
64 free++;
65 else
66 shared += page_count(page) - 1;
67 page++;
68 } while (page < end);
69 }
70
71 printk("%d pages of RAM\n", total);
72 printk("%d free pages\n", free);
73 printk("%d reserved pages\n", reserved);
74 printk("%d slab pages\n", slab);
75 printk("%d pages shared\n", shared);
76 printk("%d pages swap cached\n", cached);
77}
78
Russell King90072052005-10-28 14:48:37 +010079#define for_each_nodebank(iter,mi,no) \
80 for (iter = 0; iter < mi->nr_banks; iter++) \
81 if (mi->bank[iter].node == no)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * FIXME: We really want to avoid allocating the bootmap bitmap
85 * over the top of the initrd. Hopefully, this is located towards
86 * the start of a bank, so if we allocate the bootmap bitmap at
87 * the end, we won't clash.
88 */
89static unsigned int __init
90find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
91{
92 unsigned int start_pfn, bank, bootmap_pfn;
93
Russell King90072052005-10-28 14:48:37 +010094 start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 bootmap_pfn = 0;
96
Russell King90072052005-10-28 14:48:37 +010097 for_each_nodebank(bank, mi, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned int start, end;
99
Russell King92a8cbe2005-06-22 21:47:25 +0100100 start = mi->bank[bank].start >> PAGE_SHIFT;
101 end = (mi->bank[bank].size +
102 mi->bank[bank].start) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (end < start_pfn)
105 continue;
106
107 if (start < start_pfn)
108 start = start_pfn;
109
110 if (end <= start)
111 continue;
112
113 if (end - start >= bootmap_pages) {
114 bootmap_pfn = start;
115 break;
116 }
117 }
118
119 if (bootmap_pfn == 0)
120 BUG();
121
122 return bootmap_pfn;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int __init check_initrd(struct meminfo *mi)
126{
127 int initrd_node = -2;
128#ifdef CONFIG_BLK_DEV_INITRD
129 unsigned long end = phys_initrd_start + phys_initrd_size;
130
131 /*
132 * Make sure that the initrd is within a valid area of
133 * memory.
134 */
135 if (phys_initrd_size) {
136 unsigned int i;
137
138 initrd_node = -1;
139
140 for (i = 0; i < mi->nr_banks; i++) {
141 unsigned long bank_end;
142
143 bank_end = mi->bank[i].start + mi->bank[i].size;
144
145 if (mi->bank[i].start <= phys_initrd_start &&
146 end <= bank_end)
147 initrd_node = mi->bank[i].node;
148 }
149 }
150
151 if (initrd_node == -1) {
152 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
153 "physical memory - disabling initrd\n",
154 phys_initrd_start, end);
155 phys_initrd_start = phys_initrd_size = 0;
156 }
157#endif
158
159 return initrd_node;
160}
161
Russell King456335e2006-09-27 10:00:54 +0100162static inline void map_memory_bank(struct membank *bank)
163{
Russell Kingd111e8f2006-09-27 15:27:33 +0100164#ifdef CONFIG_MMU
Russell King456335e2006-09-27 10:00:54 +0100165 struct map_desc map;
166
167 map.pfn = __phys_to_pfn(bank->start);
168 map.virtual = __phys_to_virt(bank->start);
169 map.length = bank->size;
170 map.type = MT_MEMORY;
171
172 create_mapping(&map);
Russell Kingd111e8f2006-09-27 15:27:33 +0100173#endif
Russell King456335e2006-09-27 10:00:54 +0100174}
175
Russell King90072052005-10-28 14:48:37 +0100176static unsigned long __init
177bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Russell King90072052005-10-28 14:48:37 +0100179 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
180 unsigned long start_pfn, end_pfn, boot_pfn;
181 unsigned int boot_pages;
182 pg_data_t *pgdat;
183 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Russell King90072052005-10-28 14:48:37 +0100185 start_pfn = -1UL;
186 end_pfn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /*
Russell King90072052005-10-28 14:48:37 +0100189 * Calculate the pfn range, and map the memory banks for this node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
Russell King90072052005-10-28 14:48:37 +0100191 for_each_nodebank(i, mi, node) {
Russell King456335e2006-09-27 10:00:54 +0100192 struct membank *bank = &mi->bank[i];
Russell King90072052005-10-28 14:48:37 +0100193 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Russell King456335e2006-09-27 10:00:54 +0100195 start = bank->start >> PAGE_SHIFT;
196 end = (bank->start + bank->size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Russell King90072052005-10-28 14:48:37 +0100198 if (start_pfn > start)
199 start_pfn = start;
200 if (end_pfn < end)
201 end_pfn = end;
202
Russell King456335e2006-09-27 10:00:54 +0100203 map_memory_bank(bank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
Russell King90072052005-10-28 14:48:37 +0100206 /*
207 * If there is no memory in this node, ignore it.
208 */
209 if (end_pfn == 0)
210 return end_pfn;
211
212 /*
213 * Allocate the bootmem bitmap page.
214 */
215 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
216 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
217
218 /*
219 * Initialise the bootmem allocator for this node, handing the
220 * memory banks over to bootmem.
221 */
222 node_set_online(node);
223 pgdat = NODE_DATA(node);
224 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
225
226 for_each_nodebank(i, mi, node)
227 free_bootmem_node(pgdat, mi->bank[i].start, mi->bank[i].size);
228
229 /*
230 * Reserve the bootmem bitmap for this node.
231 */
232 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
233 boot_pages << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235#ifdef CONFIG_BLK_DEV_INITRD
Russell King90072052005-10-28 14:48:37 +0100236 /*
237 * If the initrd is in this node, reserve its memory.
238 */
239 if (node == initrd_node) {
240 reserve_bootmem_node(pgdat, phys_initrd_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 phys_initrd_size);
242 initrd_start = __phys_to_virt(phys_initrd_start);
243 initrd_end = initrd_start + phys_initrd_size;
244 }
245#endif
246
Russell King90072052005-10-28 14:48:37 +0100247 /*
248 * Finally, reserve any node zero regions.
249 */
250 if (node == 0)
251 reserve_node_zero(pgdat);
252
253 /*
254 * initialise the zones within this node.
255 */
256 memset(zone_size, 0, sizeof(zone_size));
257 memset(zhole_size, 0, sizeof(zhole_size));
258
259 /*
260 * The size of this node has already been determined. If we need
261 * to do anything fancy with the allocation of this memory to the
262 * zones, now is the time to do it.
263 */
264 zone_size[0] = end_pfn - start_pfn;
265
266 /*
267 * For each bank in this node, calculate the size of the holes.
268 * holes = node_size - sum(bank_sizes_in_node)
269 */
270 zhole_size[0] = zone_size[0];
271 for_each_nodebank(i, mi, node)
272 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
273
274 /*
275 * Adjust the sizes according to any special requirements for
276 * this machine type.
277 */
278 arch_adjust_zones(node, zone_size, zhole_size);
279
280 free_area_init_node(node, pgdat, zone_size, start_pfn, zhole_size);
281
282 return end_pfn;
283}
284
Russell Kingd111e8f2006-09-27 15:27:33 +0100285void __init bootmem_init(struct meminfo *mi)
Russell King90072052005-10-28 14:48:37 +0100286{
Russell King456335e2006-09-27 10:00:54 +0100287 unsigned long memend_pfn = 0;
Russell King90072052005-10-28 14:48:37 +0100288 int node, initrd_node, i;
289
290 /*
291 * Invalidate the node number for empty or invalid memory banks
292 */
293 for (i = 0; i < mi->nr_banks; i++)
294 if (mi->bank[i].size == 0 || mi->bank[i].node >= MAX_NUMNODES)
295 mi->bank[i].node = -1;
296
297 memcpy(&meminfo, mi, sizeof(meminfo));
298
Russell King90072052005-10-28 14:48:37 +0100299 /*
300 * Locate which node contains the ramdisk image, if any.
301 */
302 initrd_node = check_initrd(mi);
303
304 /*
305 * Run through each node initialising the bootmem allocator.
306 */
307 for_each_node(node) {
308 unsigned long end_pfn;
309
310 end_pfn = bootmem_init_node(node, initrd_node, mi);
311
312 /*
313 * Remember the highest memory PFN.
314 */
315 if (end_pfn > memend_pfn)
316 memend_pfn = end_pfn;
317 }
318
319 high_memory = __va(memend_pfn << PAGE_SHIFT);
320
321 /*
322 * This doesn't seem to be used by the Linux memory manager any
323 * more, but is used by ll_rw_block. If we can get rid of it, we
324 * also get rid of some of the stuff above as well.
325 *
326 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
327 * the system, not the maximum PFN.
328 */
329 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static inline void free_area(unsigned long addr, unsigned long end, char *s)
333{
334 unsigned int size = (end - addr) >> 10;
335
336 for (; addr < end; addr += PAGE_SIZE) {
337 struct page *page = virt_to_page(addr);
338 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800339 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 free_page(addr);
341 totalram_pages++;
342 }
343
344 if (size && s)
345 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
346}
347
Russell Kinga0130532005-06-27 14:16:47 +0100348static inline void
349free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
350{
351 struct page *start_pg, *end_pg;
352 unsigned long pg, pgend;
353
354 /*
355 * Convert start_pfn/end_pfn to a struct page pointer.
356 */
357 start_pg = pfn_to_page(start_pfn);
358 end_pg = pfn_to_page(end_pfn);
359
360 /*
361 * Convert to physical addresses, and
362 * round start upwards and end downwards.
363 */
364 pg = PAGE_ALIGN(__pa(start_pg));
365 pgend = __pa(end_pg) & PAGE_MASK;
366
367 /*
368 * If there are free pages between these,
369 * free the section of the memmap array.
370 */
371 if (pg < pgend)
372 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
373}
374
375/*
376 * The mem_map array can get very big. Free the unused area of the memory map.
377 */
378static void __init free_unused_memmap_node(int node, struct meminfo *mi)
379{
380 unsigned long bank_start, prev_bank_end = 0;
381 unsigned int i;
382
383 /*
384 * [FIXME] This relies on each bank being in address order. This
385 * may not be the case, especially if the user has provided the
386 * information on the command line.
387 */
Russell King90072052005-10-28 14:48:37 +0100388 for_each_nodebank(i, mi, node) {
Russell Kinga0130532005-06-27 14:16:47 +0100389 bank_start = mi->bank[i].start >> PAGE_SHIFT;
390 if (bank_start < prev_bank_end) {
391 printk(KERN_ERR "MEM: unordered memory banks. "
392 "Not freeing memmap.\n");
393 break;
394 }
395
396 /*
397 * If we had a previous bank, and there is a space
398 * between the current bank and the previous, free it.
399 */
400 if (prev_bank_end && prev_bank_end != bank_start)
401 free_memmap(node, prev_bank_end, bank_start);
402
403 prev_bank_end = (mi->bank[i].start +
404 mi->bank[i].size) >> PAGE_SHIFT;
405 }
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
409 * mem_init() marks the free areas in the mem_map and tells us how much
410 * memory is free. This is done after various parts of the system have
411 * claimed their memory after the kernel image.
412 */
413void __init mem_init(void)
414{
415 unsigned int codepages, datapages, initpages;
416 int i, node;
417
418 codepages = &_etext - &_text;
419 datapages = &_end - &__data_start;
420 initpages = &__init_end - &__init_begin;
421
422#ifndef CONFIG_DISCONTIGMEM
423 max_mapnr = virt_to_page(high_memory) - mem_map;
424#endif
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* this will put all unused low memory onto the freelists */
427 for_each_online_node(node) {
428 pg_data_t *pgdat = NODE_DATA(node);
429
Russell Kinga0130532005-06-27 14:16:47 +0100430 free_unused_memmap_node(node, &meminfo);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (pgdat->node_spanned_pages != 0)
433 totalram_pages += free_all_bootmem_node(pgdat);
434 }
435
436#ifdef CONFIG_SA1111
437 /* now that our DMA memory is actually so designated, we can free it */
438 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
439#endif
440
441 /*
442 * Since our memory may not be contiguous, calculate the
443 * real number of pages we have in this system
444 */
445 printk(KERN_INFO "Memory:");
446
447 num_physpages = 0;
448 for (i = 0; i < meminfo.nr_banks; i++) {
449 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
450 printk(" %ldMB", meminfo.bank[i].size >> 20);
451 }
452
453 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
454 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
455 "%dK data, %dK init)\n",
456 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
457 codepages >> 10, datapages >> 10, initpages >> 10);
458
459 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
460 extern int sysctl_overcommit_memory;
461 /*
462 * On a machine this small we won't get
463 * anywhere without overcommit, so turn
464 * it on by default.
465 */
466 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
467 }
468}
469
470void free_initmem(void)
471{
472 if (!machine_is_integrator() && !machine_is_cintegrator()) {
473 free_area((unsigned long)(&__init_begin),
474 (unsigned long)(&__init_end),
475 "init");
476 }
477}
478
479#ifdef CONFIG_BLK_DEV_INITRD
480
481static int keep_initrd;
482
483void free_initrd_mem(unsigned long start, unsigned long end)
484{
485 if (!keep_initrd)
486 free_area(start, end, "initrd");
487}
488
489static int __init keepinitrd_setup(char *__unused)
490{
491 keep_initrd = 1;
492 return 1;
493}
494
495__setup("keepinitrd", keepinitrd_setup);
496#endif