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