ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / arm26 / mm / init.c
1 /*
2  *  linux/arch/arm/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>
26
27 #include <asm/segment.h>
28 #include <asm/mach-types.h>
29 #include <asm/pgalloc.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
40 struct mmu_gather mmu_gathers[NR_CPUS];
41
42 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
43 extern char _stext, _text, _etext, _end, __init_begin, __init_end;
44 #ifdef CONFIG_XIP_KERNEL
45 extern char _endtext, _sdata;
46 #endif
47 extern unsigned long phys_initrd_start;
48 extern 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  */
54 static 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  */
60 struct page *empty_zero_page;
61
62 void 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:       %6dkB\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 += atomic_read(&page->count) - 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
99 struct node_info {
100         unsigned int start;
101         unsigned int end;
102         int bootmap_pages;
103 };
104
105 #define PFN_DOWN(x)     ((x) >> PAGE_SHIFT)
106 #define PFN_UP(x)       (PAGE_ALIGN(x) >> PAGE_SHIFT)
107 #define PFN_SIZE(x)     ((x) >> PAGE_SHIFT)
108 #define PFN_RANGE(s,e)  PFN_SIZE(PAGE_ALIGN((unsigned long)(e)) - \
109                                 (((unsigned long)(s)) & PAGE_MASK))
110
111 /*
112  * FIXME: We really want to avoid allocating the bootmap bitmap
113  * over the top of the initrd.  Hopefully, this is located towards
114  * the start of a bank, so if we allocate the bootmap bitmap at
115  * the end, we won't clash.
116  */
117 static unsigned int __init
118 find_bootmap_pfn(struct meminfo *mi, unsigned int bootmap_pages)
119 {
120         unsigned int start_pfn, bootmap_pfn;
121         unsigned int start, end;
122
123         start_pfn   = PFN_UP((unsigned long)&_end);
124         bootmap_pfn = 0;
125
126         /* ARM26 machines only have one node */
127         if (mi->bank->node != 0)
128                 BUG();
129
130         start = PFN_UP(mi->bank->start);
131         end   = PFN_DOWN(mi->bank->size + mi->bank->start);
132
133         if (start < start_pfn)
134                 start = start_pfn;
135
136         if (end <= start)
137                 BUG();
138
139         if (end - start >= bootmap_pages) 
140                 bootmap_pfn = start;
141         else
142                 BUG();
143
144         return bootmap_pfn;
145 }
146
147 /*
148  * Scan the memory info structure and pull out:
149  *  - the end of memory
150  *  - the number of nodes
151  *  - the pfn range of each node
152  *  - the number of bootmem bitmap pages
153  */
154 static void __init
155 find_memend_and_nodes(struct meminfo *mi, struct node_info *np)
156 {
157         unsigned int memend_pfn = 0;
158
159         numnodes = 1;
160
161         np->bootmap_pages = 0;
162
163         if (mi->bank->size == 0) {
164                 BUG();
165         }
166
167         /*
168          * Get the start and end pfns for this bank
169          */
170         np->start = PFN_UP(mi->bank->start);
171         np->end   = PFN_DOWN(mi->bank->start + mi->bank->size);
172
173         if (memend_pfn < np->end)
174                 memend_pfn = np->end;
175
176         /*
177          * Calculate the number of pages we require to
178          * store the bootmem bitmaps.
179          */
180         np->bootmap_pages = bootmem_bootmap_pages(np->end - np->start);
181
182         /*
183          * This doesn't seem to be used by the Linux memory
184          * manager any more.  If we can get rid of it, we
185          * also get rid of some of the stuff above as well.
186          */
187         max_low_pfn = memend_pfn - PFN_DOWN(PHYS_OFFSET);
188         max_pfn = memend_pfn - PFN_DOWN(PHYS_OFFSET);
189         mi->end = memend_pfn << PAGE_SHIFT;
190
191 }
192
193 /*
194  * Initialise the bootmem allocator for all nodes.  This is called
195  * early during the architecture specific initialisation.
196  */
197 void __init bootmem_init(struct meminfo *mi)
198 {
199         struct node_info node_info;
200         unsigned int bootmap_pfn;
201         pg_data_t *pgdat = NODE_DATA(0);
202
203         find_memend_and_nodes(mi, &node_info);
204
205         bootmap_pfn   = find_bootmap_pfn(mi, node_info.bootmap_pages);
206
207         /*
208          * Note that node 0 must always have some pages.
209          */
210         if (node_info.end == 0)
211                 BUG();
212
213         /*
214          * Initialise the bootmem allocator.
215          */
216         init_bootmem_node(pgdat, bootmap_pfn, node_info.start, node_info.end);
217
218         /*
219          * Register all available RAM in this node with the bootmem allocator. 
220          */
221         free_bootmem_node(pgdat, mi->bank->start, mi->bank->size);
222
223         /*
224          * Register the kernel text and data with bootmem.
225          * Note: with XIP we dont register .text since
226          * its in ROM.
227          */
228 #ifdef CONFIG_XIP_KERNEL
229         reserve_bootmem_node(pgdat, __pa(&_sdata), &_end - &_sdata);
230 #else
231         reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
232 #endif
233
234         /*
235          * And don't forget to reserve the allocator bitmap,
236          * which will be freed later.
237          */
238         reserve_bootmem_node(pgdat, bootmap_pfn << PAGE_SHIFT,
239                              node_info.bootmap_pages << PAGE_SHIFT);
240
241         /*
242          * These should likewise go elsewhere.  They pre-reserve
243          * the screen memory region at the start of main system
244          * memory. FIXME - screen RAM is not 512K!
245          */
246         reserve_bootmem_node(pgdat, 0x02000000, 0x00080000);
247
248 #ifdef CONFIG_BLK_DEV_INITRD
249         initrd_start = phys_initrd_start;
250         initrd_end = initrd_start + phys_initrd_size;
251
252         /* Achimedes machines only have one node, so initrd is in node 0 */
253 #ifdef CONFIG_XIP_KERNEL
254         /* Only reserve initrd space if it is in RAM */
255         if(initrd_start && initrd_start < 0x03000000){
256 #else
257         if(initrd_start){
258 #endif
259                 reserve_bootmem_node(pgdat, __pa(initrd_start),
260                                              initrd_end - initrd_start);
261         }
262 #endif   /* CONFIG_BLK_DEV_INITRD */
263
264
265 }
266
267 /*
268  * paging_init() sets up the page tables, initialises the zone memory
269  * maps, and sets up the zero page, bad page and bad page tables.
270  */
271 void __init paging_init(struct meminfo *mi)
272 {
273         void *zero_page;
274         unsigned long zone_size[MAX_NR_ZONES];
275         unsigned long zhole_size[MAX_NR_ZONES];
276         struct bootmem_data *bdata;
277         pg_data_t *pgdat;
278         int i;
279
280         memcpy(&meminfo, mi, sizeof(meminfo));
281
282         /*
283          * allocate the zero page.  Note that we count on this going ok.
284          */
285         zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
286
287         /*
288          * initialise the page tables.
289          */
290         memtable_init(mi);
291         flush_tlb_all();
292
293         /*
294          * initialise the zones in node 0 (archimedes have only 1 node)
295          */
296
297         for (i = 0; i < MAX_NR_ZONES; i++) {
298                 zone_size[i]  = 0;
299                 zhole_size[i] = 0;
300         }
301
302         pgdat = NODE_DATA(0);
303         bdata = pgdat->bdata;
304         zone_size[0] = bdata->node_low_pfn -
305                         (bdata->node_boot_start >> PAGE_SHIFT);
306         if (!zone_size[0])
307                 BUG();
308
309         free_area_init_node(0, pgdat, 0, zone_size,
310                         bdata->node_boot_start >> PAGE_SHIFT, zhole_size);
311
312         mem_map = NODE_DATA(0)->node_mem_map;
313
314         /*
315          * finish off the bad pages once
316          * the mem_map is initialised
317          */
318         memzero(zero_page, PAGE_SIZE);
319         empty_zero_page = virt_to_page(zero_page);
320 }
321
322 static inline void free_area(unsigned long addr, unsigned long end, char *s)
323 {
324         unsigned int size = (end - addr) >> 10;
325
326         for (; addr < end; addr += PAGE_SIZE) {
327                 struct page *page = virt_to_page(addr);
328                 ClearPageReserved(page);
329                 set_page_count(page, 1);
330                 free_page(addr);
331                 totalram_pages++;
332         }
333
334         if (size && s)
335                 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
336 }
337
338 /*
339  * mem_init() marks the free areas in the mem_map and tells us how much
340  * memory is free.  This is done after various parts of the system have
341  * claimed their memory after the kernel image.
342  */
343 void __init mem_init(void)
344 {
345         unsigned int codepages, datapages, initpages;
346         pg_data_t *pgdat = NODE_DATA(0);
347         extern int sysctl_overcommit_memory;
348
349
350         /* Note: data pages includes BSS */
351 #ifdef CONFIG_XIP_KERNEL
352         codepages = &_endtext - &_text;
353         datapages = &_end - &_sdata;
354 #else
355         codepages = &_etext - &_text;
356         datapages = &_end - &_etext;
357 #endif
358         initpages = &__init_end - &__init_begin;
359
360         high_memory = (void *)__va(meminfo.end);
361         max_mapnr   = virt_to_page(high_memory) - mem_map;
362
363         /* this will put all unused low memory onto the freelists */
364         if (pgdat->node_spanned_pages != 0)
365                 totalram_pages += free_all_bootmem_node(pgdat);
366
367         num_physpages = meminfo.bank[0].size >> PAGE_SHIFT;
368
369         printk(KERN_INFO "Memory: %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
370         printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
371                 "%dK data, %dK init)\n",
372                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
373                 codepages >> 10, datapages >> 10, initpages >> 10);
374
375         /*
376          * Turn on overcommit on tiny machines
377          */
378         if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
379                 sysctl_overcommit_memory = 1;
380                 printk("Turning on overcommit\n");
381         }
382 }
383
384 void free_initmem(void){
385 #ifndef CONFIG_XIP_KERNEL
386         free_area((unsigned long)(&__init_begin),
387                   (unsigned long)(&__init_end),
388                   "init");
389 #endif
390 }
391
392 #ifdef CONFIG_BLK_DEV_INITRD
393
394 static int keep_initrd;
395
396 void free_initrd_mem(unsigned long start, unsigned long end)
397 {
398 #ifdef CONFIG_XIP_KERNEL
399         /* Only bin initrd if it is in RAM... */
400         if(!keep_initrd && start < 0x03000000)
401 #else
402         if (!keep_initrd)
403 #endif
404                 free_area(start, end, "initrd");
405 }
406
407 static int __init keepinitrd_setup(char *__unused)
408 {
409         keep_initrd = 1;
410         return 1;
411 }
412
413 __setup("keepinitrd", keepinitrd_setup);
414 #endif