ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sh / mm / init.c
1 /* $Id: init.c,v 1.19 2004/02/21 04:42:16 kkojima Exp $
2  *
3  *  linux/arch/sh/mm/init.c
4  *
5  *  Copyright (C) 1999  Niibe Yutaka
6  *  Copyright (C) 2002  Paul Mundt
7  *
8  *  Based on linux/arch/i386/mm/init.c:
9  *   Copyright (C) 1995  Linus Torvalds
10  */
11
12 #include <linux/config.h>
13 #include <linux/signal.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/ptrace.h>
20 #include <linux/mman.h>
21 #include <linux/mm.h>
22 #include <linux/swap.h>
23 #include <linux/smp.h>
24 #include <linux/init.h>
25 #include <linux/highmem.h>
26 #include <linux/bootmem.h>
27 #include <linux/pagemap.h>
28
29 #include <asm/processor.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgtable.h>
33 #include <asm/pgalloc.h>
34 #include <asm/mmu_context.h>
35 #include <asm/io.h>
36 #include <asm/tlb.h>
37 #include <asm/cacheflush.h>
38 #include <asm/cache.h>
39
40 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
41 pgd_t swapper_pg_dir[PTRS_PER_PGD];
42
43 /*
44  * Cache of MMU context last used.
45  */
46 unsigned long mmu_context_cache = NO_CONTEXT;
47
48 #ifdef CONFIG_MMU
49 /* It'd be good if these lines were in the standard header file. */
50 #define START_PFN       (NODE_DATA(0)->bdata->node_boot_start >> PAGE_SHIFT)
51 #define MAX_LOW_PFN     (NODE_DATA(0)->bdata->node_low_pfn)
52 #endif
53
54 #ifdef CONFIG_DISCONTIGMEM
55 pg_data_t discontig_page_data[MAX_NUMNODES];
56 bootmem_data_t discontig_node_bdata[MAX_NUMNODES];
57 #endif
58
59 void (*copy_page)(void *from, void *to);
60 void (*clear_page)(void *to);
61
62 void show_mem(void)
63 {
64         int i, total = 0, reserved = 0;
65         int shared = 0, cached = 0;
66
67         printk("Mem-info:\n");
68         show_free_areas();
69         printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
70         i = max_mapnr;
71         while (i-- > 0) {
72                 total++;
73                 if (PageReserved(mem_map+i))
74                         reserved++;
75                 else if (PageSwapCache(mem_map+i))
76                         cached++;
77                 else if (page_count(mem_map+i))
78                         shared += page_count(mem_map+i) - 1;
79         }
80         printk("%d pages of RAM\n",total);
81         printk("%d reserved pages\n",reserved);
82         printk("%d pages shared\n",shared);
83         printk("%d pages swap cached\n",cached);
84 }
85
86 /* References to section boundaries */
87
88 extern char _text, _etext, _edata, __bss_start, _end;
89 extern char __init_begin, __init_end;
90
91 /*
92  * paging_init() sets up the page tables
93  *
94  * This routines also unmaps the page at virtual kernel address 0, so
95  * that we can trap those pesky NULL-reference errors in the kernel.
96  */
97 void __init paging_init(void)
98 {
99         unsigned long zones_size[MAX_NR_ZONES] = { 0, };
100
101         /*
102          * Setup some defaults for the zone sizes.. these should be safe
103          * regardless of distcontiguous memory or MMU settings.
104          */
105         zones_size[ZONE_DMA] = 0 >> PAGE_SHIFT;
106         zones_size[ZONE_NORMAL] = __MEMORY_SIZE >> PAGE_SHIFT;
107 #ifdef CONFIG_HIGHMEM
108         zones_size[ZONE_HIGHMEM] = 0 >> PAGE_SHIFT;
109 #endif
110
111 #ifdef CONFIG_MMU
112         /*
113          * If we have an MMU, and want to be using it .. we need to adjust
114          * the zone sizes accordingly, in addition to turning it on.
115          */
116         {
117                 unsigned long max_dma, low, start_pfn;
118                 pgd_t *pg_dir;
119                 int i;
120
121                 /* We don't need kernel mapping as hardware support that. */
122                 pg_dir = swapper_pg_dir;
123
124                 for (i = 0; i < PTRS_PER_PGD; i++)
125                         pgd_val(pg_dir[i]) = 0;
126
127                 /* Turn on the MMU */
128                 enable_mmu();
129
130                 /* Fixup the zone sizes */
131                 start_pfn = START_PFN;
132                 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
133                 low = MAX_LOW_PFN;
134
135                 if (low < max_dma) {
136                         zones_size[ZONE_DMA] = low - start_pfn;
137                         zones_size[ZONE_NORMAL] = 0;
138                 } else {
139                         zones_size[ZONE_DMA] = max_dma - start_pfn;
140                         zones_size[ZONE_NORMAL] = low - max_dma;
141                 }
142         }
143
144 #elif defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
145         /*
146          * If we don't have CONFIG_MMU set and the processor in question
147          * still has an MMU, care needs to be taken to make sure it doesn't
148          * stay on.. Since the boot loader could have potentially already
149          * turned it on, and we clearly don't want it, we simply turn it off.
150          *
151          * We don't need to do anything special for the zone sizes, since the
152          * default values that were already configured up above should be
153          * satisfactory.
154          */
155         disable_mmu();
156 #endif
157
158         free_area_init_node(0, NODE_DATA(0), 0, zones_size, __MEMORY_START >> PAGE_SHIFT, 0);
159         /* XXX: MRB-remove - this doesn't seem sane, should this be done somewhere else ?*/
160         mem_map = NODE_DATA(0)->node_mem_map;
161
162 #ifdef CONFIG_DISCONTIGMEM
163         /*
164          * And for discontig, do some more fixups on the zone sizes..
165          */
166         zones_size[ZONE_DMA] = __MEMORY_SIZE_2ND >> PAGE_SHIFT;
167         zones_size[ZONE_NORMAL] = 0;
168         free_area_init_node(1, NODE_DATA(1), 0, zones_size, __MEMORY_START_2ND >> PAGE_SHIFT, 0);
169 #endif
170 }
171
172 void __init mem_init(void)
173 {
174         extern unsigned long empty_zero_page[1024];
175         int codesize, reservedpages, datasize, initsize;
176         int tmp;
177         extern unsigned long memory_start;
178
179 #ifdef CONFIG_MMU
180         high_memory = (void *)__va(MAX_LOW_PFN * PAGE_SIZE);
181 #else
182         extern unsigned long memory_end;
183
184         high_memory = (void *)(memory_end & PAGE_MASK);
185 #endif
186
187         max_mapnr = num_physpages = MAP_NR(high_memory) - MAP_NR(memory_start);
188
189         /* clear the zero-page */
190         memset(empty_zero_page, 0, PAGE_SIZE);
191         __flush_wback_region(empty_zero_page, PAGE_SIZE);
192
193         /* 
194          * Setup wrappers for copy/clear_page(), these will get overridden
195          * later in the boot process if a better method is available.
196          */
197         copy_page = copy_page_slow;
198         clear_page = clear_page_slow;
199
200         /* this will put all low memory onto the freelists */
201         totalram_pages += free_all_bootmem_node(NODE_DATA(0));
202 #ifdef CONFIG_DISCONTIGMEM
203         totalram_pages += free_all_bootmem_node(NODE_DATA(1));
204 #endif
205         reservedpages = 0;
206         for (tmp = 0; tmp < num_physpages; tmp++)
207                 /*
208                  * Only count reserved RAM pages
209                  */
210                 if (PageReserved(mem_map+tmp))
211                         reservedpages++;
212
213         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
214         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
215         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
216
217         printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init)\n",
218                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
219                 max_mapnr << (PAGE_SHIFT-10),
220                 codesize >> 10,
221                 reservedpages << (PAGE_SHIFT-10),
222                 datasize >> 10,
223                 initsize >> 10);
224
225         p3_cache_init();
226 }
227
228 void free_initmem(void)
229 {
230         unsigned long addr;
231         
232         addr = (unsigned long)(&__init_begin);
233         for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
234                 ClearPageReserved(virt_to_page(addr));
235                 set_page_count(virt_to_page(addr), 1);
236                 free_page(addr);
237                 totalram_pages++;
238         }
239         printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
240 }
241
242 #ifdef CONFIG_BLK_DEV_INITRD
243 void free_initrd_mem(unsigned long start, unsigned long end)
244 {
245         unsigned long p;
246         for (p = start; p < end; p += PAGE_SIZE) {
247                 ClearPageReserved(virt_to_page(p));
248                 set_page_count(virt_to_page(p), 1);
249                 free_page(p);
250                 totalram_pages++;
251         }
252         printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
253 }
254 #endif
255