blob: 7da8a520567874d13f8f6ee619e56ac52d7e9317 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm26/mm/init.c
3 *
4 * Copyright (C) 1995-2002 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/config.h>
11#include <linux/signal.h>
12#include <linux/sched.h>
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/string.h>
16#include <linux/types.h>
17#include <linux/ptrace.h>
18#include <linux/mman.h>
19#include <linux/mm.h>
20#include <linux/swap.h>
21#include <linux/smp.h>
22#include <linux/init.h>
23#include <linux/initrd.h>
24#include <linux/bootmem.h>
25#include <linux/blkdev.h>
Dave Hansen22a98352006-03-27 01:16:04 -080026#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/segment.h>
29#include <asm/mach-types.h>
30#include <asm/dma.h>
31#include <asm/hardware.h>
32#include <asm/setup.h>
33#include <asm/tlb.h>
34
35#include <asm/map.h>
36
37
38#define TABLE_SIZE PTRS_PER_PTE * sizeof(pte_t))
39
40struct mmu_gather mmu_gathers[NR_CPUS];
41
42extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
43extern char _stext, _text, _etext, _end, __init_begin, __init_end;
44#ifdef CONFIG_XIP_KERNEL
45extern char _endtext, _sdata;
46#endif
47extern unsigned long phys_initrd_start;
48extern unsigned long phys_initrd_size;
49
50/*
51 * The sole use of this is to pass memory configuration
52 * data from paging_init to mem_init.
53 */
54static struct meminfo meminfo __initdata = { 0, };
55
56/*
57 * empty_zero_page is a special page that is used for
58 * zero-initialized data and COW.
59 */
60struct page *empty_zero_page;
61
62void show_mem(void)
63{
64 int free = 0, total = 0, reserved = 0;
65 int shared = 0, cached = 0, slab = 0;
66 struct page *page, *end;
67
68 printk("Mem-info:\n");
69 show_free_areas();
70 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
71
72
73 page = NODE_MEM_MAP(0);
74 end = page + NODE_DATA(0)->node_spanned_pages;
75
76 do {
77 total++;
78 if (PageReserved(page))
79 reserved++;
80 else if (PageSwapCache(page))
81 cached++;
82 else if (PageSlab(page))
83 slab++;
84 else if (!page_count(page))
85 free++;
86 else
87 shared += page_count(page) - 1;
88 page++;
89 } while (page < end);
90
91 printk("%d pages of RAM\n", total);
92 printk("%d free pages\n", free);
93 printk("%d reserved pages\n", reserved);
94 printk("%d slab pages\n", slab);
95 printk("%d pages shared\n", shared);
96 printk("%d pages swap cached\n", cached);
97}
98
99struct node_info {
100 unsigned int start;
101 unsigned int end;
102 int bootmap_pages;
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * FIXME: We really want to avoid allocating the bootmap bitmap
107 * over the top of the initrd. Hopefully, this is located towards
108 * the start of a bank, so if we allocate the bootmap bitmap at
109 * the end, we won't clash.
110 */
111static unsigned int __init
112find_bootmap_pfn(struct meminfo *mi, unsigned int bootmap_pages)
113{
114 unsigned int start_pfn, bootmap_pfn;
115 unsigned int start, end;
116
117 start_pfn = PFN_UP((unsigned long)&_end);
118 bootmap_pfn = 0;
119
120 /* ARM26 machines only have one node */
121 if (mi->bank->node != 0)
122 BUG();
123
124 start = PFN_UP(mi->bank->start);
125 end = PFN_DOWN(mi->bank->size + mi->bank->start);
126
127 if (start < start_pfn)
128 start = start_pfn;
129
130 if (end <= start)
131 BUG();
132
133 if (end - start >= bootmap_pages)
134 bootmap_pfn = start;
135 else
136 BUG();
137
138 return bootmap_pfn;
139}
140
141/*
142 * Scan the memory info structure and pull out:
143 * - the end of memory
144 * - the number of nodes
145 * - the pfn range of each node
146 * - the number of bootmem bitmap pages
147 */
148static void __init
149find_memend_and_nodes(struct meminfo *mi, struct node_info *np)
150{
151 unsigned int memend_pfn = 0;
152
153 nodes_clear(node_online_map);
154 node_set_online(0);
155
156 np->bootmap_pages = 0;
157
158 if (mi->bank->size == 0) {
159 BUG();
160 }
161
162 /*
163 * Get the start and end pfns for this bank
164 */
165 np->start = PFN_UP(mi->bank->start);
166 np->end = PFN_DOWN(mi->bank->start + mi->bank->size);
167
168 if (memend_pfn < np->end)
169 memend_pfn = np->end;
170
171 /*
172 * Calculate the number of pages we require to
173 * store the bootmem bitmaps.
174 */
175 np->bootmap_pages = bootmem_bootmap_pages(np->end - np->start);
176
177 /*
178 * This doesn't seem to be used by the Linux memory
179 * manager any more. If we can get rid of it, we
180 * also get rid of some of the stuff above as well.
181 */
182 max_low_pfn = memend_pfn - PFN_DOWN(PHYS_OFFSET);
183 max_pfn = memend_pfn - PFN_DOWN(PHYS_OFFSET);
184 mi->end = memend_pfn << PAGE_SHIFT;
185
186}
187
188/*
189 * Initialise the bootmem allocator for all nodes. This is called
190 * early during the architecture specific initialisation.
191 */
192void __init bootmem_init(struct meminfo *mi)
193{
194 struct node_info node_info;
195 unsigned int bootmap_pfn;
196 pg_data_t *pgdat = NODE_DATA(0);
197
198 find_memend_and_nodes(mi, &node_info);
199
200 bootmap_pfn = find_bootmap_pfn(mi, node_info.bootmap_pages);
201
202 /*
203 * Note that node 0 must always have some pages.
204 */
205 if (node_info.end == 0)
206 BUG();
207
208 /*
209 * Initialise the bootmem allocator.
210 */
211 init_bootmem_node(pgdat, bootmap_pfn, node_info.start, node_info.end);
212
213 /*
214 * Register all available RAM in this node with the bootmem allocator.
215 */
216 free_bootmem_node(pgdat, mi->bank->start, mi->bank->size);
217
218 /*
219 * Register the kernel text and data with bootmem.
220 * Note: with XIP we dont register .text since
221 * its in ROM.
222 */
223#ifdef CONFIG_XIP_KERNEL
224 reserve_bootmem_node(pgdat, __pa(&_sdata), &_end - &_sdata);
225#else
226 reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
227#endif
228
229 /*
230 * And don't forget to reserve the allocator bitmap,
231 * which will be freed later.
232 */
233 reserve_bootmem_node(pgdat, bootmap_pfn << PAGE_SHIFT,
234 node_info.bootmap_pages << PAGE_SHIFT);
235
236 /*
237 * These should likewise go elsewhere. They pre-reserve
238 * the screen memory region at the start of main system
239 * memory. FIXME - screen RAM is not 512K!
240 */
241 reserve_bootmem_node(pgdat, 0x02000000, 0x00080000);
242
243#ifdef CONFIG_BLK_DEV_INITRD
244 initrd_start = phys_initrd_start;
245 initrd_end = initrd_start + phys_initrd_size;
246
247 /* Achimedes machines only have one node, so initrd is in node 0 */
248#ifdef CONFIG_XIP_KERNEL
249 /* Only reserve initrd space if it is in RAM */
250 if(initrd_start && initrd_start < 0x03000000){
251#else
252 if(initrd_start){
253#endif
254 reserve_bootmem_node(pgdat, __pa(initrd_start),
255 initrd_end - initrd_start);
256 }
257#endif /* CONFIG_BLK_DEV_INITRD */
258
259
260}
261
262/*
263 * paging_init() sets up the page tables, initialises the zone memory
264 * maps, and sets up the zero page, bad page and bad page tables.
265 */
266void __init paging_init(struct meminfo *mi)
267{
268 void *zero_page;
269 unsigned long zone_size[MAX_NR_ZONES];
270 unsigned long zhole_size[MAX_NR_ZONES];
271 struct bootmem_data *bdata;
272 pg_data_t *pgdat;
273 int i;
274
275 memcpy(&meminfo, mi, sizeof(meminfo));
276
277 /*
278 * allocate the zero page. Note that we count on this going ok.
279 */
280 zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
281
282 /*
283 * initialise the page tables.
284 */
285 memtable_init(mi);
286 flush_tlb_all();
287
288 /*
289 * initialise the zones in node 0 (archimedes have only 1 node)
290 */
291
292 for (i = 0; i < MAX_NR_ZONES; i++) {
293 zone_size[i] = 0;
294 zhole_size[i] = 0;
295 }
296
297 pgdat = NODE_DATA(0);
298 bdata = pgdat->bdata;
299 zone_size[0] = bdata->node_low_pfn -
300 (bdata->node_boot_start >> PAGE_SHIFT);
301 if (!zone_size[0])
302 BUG();
303 pgdat->node_mem_map = NULL;
304 free_area_init_node(0, pgdat, zone_size,
305 bdata->node_boot_start >> PAGE_SHIFT, zhole_size);
306
307 /*
308 * finish off the bad pages once
309 * the mem_map is initialised
310 */
311 memzero(zero_page, PAGE_SIZE);
312 empty_zero_page = virt_to_page(zero_page);
313}
314
315static inline void free_area(unsigned long addr, unsigned long end, char *s)
316{
317 unsigned int size = (end - addr) >> 10;
318
319 for (; addr < end; addr += PAGE_SIZE) {
320 struct page *page = virt_to_page(addr);
321 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800322 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 free_page(addr);
324 totalram_pages++;
325 }
326
327 if (size && s)
328 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
329}
330
331/*
332 * mem_init() marks the free areas in the mem_map and tells us how much
333 * memory is free. This is done after various parts of the system have
334 * claimed their memory after the kernel image.
335 */
336void __init mem_init(void)
337{
338 unsigned int codepages, datapages, initpages;
339 pg_data_t *pgdat = NODE_DATA(0);
340 extern int sysctl_overcommit_memory;
341
342
343 /* Note: data pages includes BSS */
344#ifdef CONFIG_XIP_KERNEL
345 codepages = &_endtext - &_text;
346 datapages = &_end - &_sdata;
347#else
348 codepages = &_etext - &_text;
349 datapages = &_end - &_etext;
350#endif
351 initpages = &__init_end - &__init_begin;
352
353 high_memory = (void *)__va(meminfo.end);
354 max_mapnr = virt_to_page(high_memory) - mem_map;
355
356 /* this will put all unused low memory onto the freelists */
357 if (pgdat->node_spanned_pages != 0)
358 totalram_pages += free_all_bootmem_node(pgdat);
359
360 num_physpages = meminfo.bank[0].size >> PAGE_SHIFT;
361
362 printk(KERN_INFO "Memory: %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
363 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
364 "%dK data, %dK init)\n",
365 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
366 codepages >> 10, datapages >> 10, initpages >> 10);
367
368 /*
369 * Turn on overcommit on tiny machines
370 */
371 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
372 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
373 printk("Turning on overcommit\n");
374 }
375}
376
377void free_initmem(void){
378#ifndef CONFIG_XIP_KERNEL
379 free_area((unsigned long)(&__init_begin),
380 (unsigned long)(&__init_end),
381 "init");
382#endif
383}
384
385#ifdef CONFIG_BLK_DEV_INITRD
386
387static int keep_initrd;
388
389void free_initrd_mem(unsigned long start, unsigned long end)
390{
391#ifdef CONFIG_XIP_KERNEL
392 /* Only bin initrd if it is in RAM... */
393 if(!keep_initrd && start < 0x03000000)
394#else
395 if (!keep_initrd)
396#endif
397 free_area(start, end, "initrd");
398}
399
400static int __init keepinitrd_setup(char *__unused)
401{
402 keep_initrd = 1;
403 return 1;
404}
405
406__setup("keepinitrd", keepinitrd_setup);
407#endif