Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / arch / sh / mm / init.c
blobb4cec79c6c51fb985d7275ab1822fdf87f9803b0
1 /* $Id: init.c,v 1.11 2003/05/27 16:21:23 lethal Exp $
3 * linux/arch/sh/mm/init.c
5 * Copyright (C) 1999 Niibe Yutaka
6 * Copyright (C) 2002 Paul Mundt
8 * Based on linux/arch/i386/mm/init.c:
9 * Copyright (C) 1995 Linus Torvalds
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 #ifdef CONFIG_BLK_DEV_INITRD
26 #include <linux/blk.h>
27 #endif
28 #include <linux/highmem.h>
29 #include <linux/bootmem.h>
31 #include <asm/processor.h>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34 #include <asm/pgtable.h>
35 #include <asm/pgalloc.h>
36 #include <asm/mmu_context.h>
37 #include <asm/io.h>
38 #include <asm/tlb.h>
39 #include <asm/cacheflush.h>
40 #include <asm/cache.h>
42 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
43 pgd_t swapper_pg_dir[PTRS_PER_PGD];
46 * Cache of MMU context last used.
48 unsigned long mmu_context_cache = NO_CONTEXT;
50 #ifdef CONFIG_MMU
51 /* It'd be good if these lines were in the standard header file. */
52 #define START_PFN (NODE_DATA(0)->bdata->node_boot_start >> PAGE_SHIFT)
53 #define MAX_LOW_PFN (NODE_DATA(0)->bdata->node_low_pfn)
54 #endif
56 #ifdef CONFIG_DISCONTIGMEM
57 pg_data_t discontig_page_data[NR_NODES];
58 bootmem_data_t discontig_node_bdata[NR_NODES];
59 #endif
61 void show_mem(void)
63 int i, total = 0, reserved = 0;
64 int shared = 0, cached = 0;
66 printk("Mem-info:\n");
67 show_free_areas();
68 printk("Free swap: %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
69 i = max_mapnr;
70 while (i-- > 0) {
71 total++;
72 if (PageReserved(mem_map+i))
73 reserved++;
74 else if (PageSwapCache(mem_map+i))
75 cached++;
76 else if (page_count(mem_map+i))
77 shared += page_count(mem_map+i) - 1;
79 printk("%d pages of RAM\n",total);
80 printk("%d reserved pages\n",reserved);
81 printk("%d pages shared\n",shared);
82 printk("%d pages swap cached\n",cached);
85 /* References to section boundaries */
87 extern char _text, _etext, _edata, __bss_start, _end;
88 extern char __init_begin, __init_end;
91 * paging_init() sets up the page tables
93 * This routines also unmaps the page at virtual kernel address 0, so
94 * that we can trap those pesky NULL-reference errors in the kernel.
96 void __init paging_init(void)
98 unsigned long zones_size[MAX_NR_ZONES] = { 0, };
101 * Setup some defaults for the zone sizes.. these should be safe
102 * regardless of distcontiguous memory or MMU settings.
104 zones_size[ZONE_DMA] = 0 >> PAGE_SHIFT;
105 zones_size[ZONE_NORMAL] = __MEMORY_SIZE >> PAGE_SHIFT;
106 #ifdef CONFIG_HIGHMEM
107 zones_size[ZONE_HIGHMEM] = 0 >> PAGE_SHIFT;
108 #endif
110 #ifdef CONFIG_MMU
112 * If we have an MMU, and want to be using it .. we need to adjust
113 * the zone sizes accordingly, in addition to turning it on.
116 unsigned long max_dma, low, start_pfn;
117 pgd_t *pg_dir;
118 int i;
120 /* We don't need kernel mapping as hardware support that. */
121 pg_dir = swapper_pg_dir;
123 for (i = 0; i < PTRS_PER_PGD; i++)
124 pgd_val(pg_dir[i]) = 0;
126 /* Turn on the MMU */
127 enable_mmu();
129 /* Fixup the zone sizes */
130 start_pfn = START_PFN;
131 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
132 low = MAX_LOW_PFN;
134 if (low < max_dma) {
135 zones_size[ZONE_DMA] = low - start_pfn;
136 } else {
137 zones_size[ZONE_DMA] = max_dma - start_pfn;
138 zones_size[ZONE_NORMAL] = low - max_dma;
141 #elif defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
143 * If we don't have CONFIG_MMU set and the processor in question
144 * still has an MMU, care needs to be taken to make sure it doesn't
145 * stay on.. Since the boot loader could have potentially already
146 * turned it on, and we clearly don't want it, we simply turn it off.
148 * We don't need to do anything special for the zone sizes, since the
149 * default values that were already configured up above should be
150 * satisfactory.
152 disable_mmu();
153 #endif
155 free_area_init_node(0, NODE_DATA(0), 0, zones_size, __MEMORY_START >> PAGE_SHIFT, 0);
156 /* XXX: MRB-remove - this doesn't seem sane, should this be done somewhere else ?*/
157 mem_map = NODE_DATA(0)->node_mem_map;
159 #ifdef CONFIG_DISCONTIGMEM
161 * And for discontig, do some more fixups on the zone sizes..
163 zones_size[ZONE_DMA] = __MEMORY_SIZE_2ND >> PAGE_SHIFT;
164 zones_size[ZONE_NORMAL] = 0;
165 free_area_init_node(1, NODE_DATA(1), 0, zones_size, __MEMORY_START_2ND >> PAGE_SHIFT, 0);
166 #endif
169 void __init mem_init(void)
171 extern unsigned long empty_zero_page[1024];
172 int codesize, reservedpages, datasize, initsize;
173 int tmp;
175 #ifdef CONFIG_MMU
176 high_memory = (void *)__va(MAX_LOW_PFN * PAGE_SIZE);
177 #else
178 extern unsigned long memory_end;
180 high_memory = (void *)(memory_end & PAGE_MASK);
181 #endif
183 max_mapnr = num_physpages = MAP_NR(high_memory);
185 /* clear the zero-page */
186 memset(empty_zero_page, 0, PAGE_SIZE);
187 __flush_wback_region(empty_zero_page, PAGE_SIZE);
189 /* this will put all low memory onto the freelists */
190 totalram_pages += free_all_bootmem_node(NODE_DATA(0));
191 #ifdef CONFIG_DISCONTIGMEM
192 totalram_pages += free_all_bootmem_node(NODE_DATA(1));
193 #endif
194 reservedpages = 0;
195 for (tmp = 0; tmp < num_physpages; tmp++)
197 * Only count reserved RAM pages
199 if (PageReserved(mem_map+tmp))
200 reservedpages++;
201 codesize = (unsigned long) &_etext - (unsigned long) &_text;
202 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
203 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
205 printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init)\n",
206 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
207 max_mapnr << (PAGE_SHIFT-10),
208 codesize >> 10,
209 reservedpages << (PAGE_SHIFT-10),
210 datasize >> 10,
211 initsize >> 10);
213 p3_cache_init();
216 void free_initmem(void)
218 unsigned long addr;
220 addr = (unsigned long)(&__init_begin);
221 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
222 ClearPageReserved(virt_to_page(addr));
223 set_page_count(virt_to_page(addr), 1);
224 free_page(addr);
225 totalram_pages++;
227 printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
230 #ifdef CONFIG_BLK_DEV_INITRD
231 void free_initrd_mem(unsigned long start, unsigned long end)
233 unsigned long p;
234 for (p = start; p < end; p += PAGE_SIZE) {
235 ClearPageReserved(virt_to_page(p));
236 set_page_count(virt_to_page(p), 1);
237 free_page(p);
238 totalram_pages++;
240 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
242 #endif
245 * Generic first-level cache init
247 void __init sh_cache_init(void)
249 extern int detect_cpu_and_cache_system(void);
250 unsigned long ccr, flags = 0;
252 detect_cpu_and_cache_system();
254 if (cpu_data->type == CPU_SH_NONE)
255 panic("Unknown CPU");
257 jump_to_P2();
258 ccr = ctrl_inl(CCR);
261 * If the cache is already enabled .. flush it.
263 if (ccr & CCR_CACHE_ENABLE) {
264 unsigned long entries, i, j;
266 entries = cpu_data->dcache.sets;
269 * If the OC is already in RAM mode, we only have
270 * half of the entries to flush..
272 if (ccr & CCR_CACHE_ORA)
273 entries >>= 1;
275 for (i = 0; i < entries; i++) {
276 for (j = 0; j < cpu_data->dcache.ways; j++) {
277 unsigned long data, addr;
279 addr = CACHE_OC_ADDRESS_ARRAY |
280 (j << cpu_data->dcache.way_shift) |
281 (i << cpu_data->dcache.entry_shift);
283 data = ctrl_inl(addr);
285 if ((data & (SH_CACHE_UPDATED | SH_CACHE_VALID))
286 == (SH_CACHE_UPDATED | SH_CACHE_VALID))
287 ctrl_outl(data & ~SH_CACHE_UPDATED, addr);
293 * Default CCR values .. enable the caches
294 * and flush them immediately..
296 flags |= CCR_CACHE_ENABLE | CCR_CACHE_INVALIDATE | (ccr & CCR_CACHE_EMODE);
298 #ifdef CONFIG_SH_WRITETHROUGH
299 /* Turn on Write-through caching */
300 flags |= CCR_CACHE_WT;
301 #else
302 /* .. or default to Write-back */
303 flags |= CCR_CACHE_CB;
304 #endif
306 #ifdef CONFIG_SH_OCRAM
307 /* Turn on OCRAM -- halve the OC */
308 flags |= CCR_CACHE_ORA;
309 cpu_data->dcache.sets >>= 1;
310 #endif
312 ctrl_outl(flags, CCR);
313 back_to_P1();