xref: /linux/arch/sh/mm/init.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
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, 2004  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 void (*copy_page)(void *from, void *to);
55 void (*clear_page)(void *to);
56 
57 void show_mem(void)
58 {
59 	int i, total = 0, reserved = 0;
60 	int shared = 0, cached = 0;
61 
62 	printk("Mem-info:\n");
63 	show_free_areas();
64 	printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
65 	i = max_mapnr;
66 	while (i-- > 0) {
67 		total++;
68 		if (PageReserved(mem_map+i))
69 			reserved++;
70 		else if (PageSwapCache(mem_map+i))
71 			cached++;
72 		else if (page_count(mem_map+i))
73 			shared += page_count(mem_map+i) - 1;
74 	}
75 	printk("%d pages of RAM\n",total);
76 	printk("%d reserved pages\n",reserved);
77 	printk("%d pages shared\n",shared);
78 	printk("%d pages swap cached\n",cached);
79 }
80 
81 static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
82 {
83 	pgd_t *pgd;
84 	pmd_t *pmd;
85 	pte_t *pte;
86 
87 	pgd = swapper_pg_dir + pgd_index(addr);
88 	if (pgd_none(*pgd)) {
89 		pgd_ERROR(*pgd);
90 		return;
91 	}
92 
93 	pmd = pmd_offset(pgd, addr);
94 	if (pmd_none(*pmd)) {
95 		pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
96 		set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE | _PAGE_USER));
97 		if (pte != pte_offset_kernel(pmd, 0)) {
98 			pmd_ERROR(*pmd);
99 			return;
100 		}
101 	}
102 
103 	pte = pte_offset_kernel(pmd, addr);
104 	if (!pte_none(*pte)) {
105 		pte_ERROR(*pte);
106 		return;
107 	}
108 
109 	set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
110 
111 	__flush_tlb_page(get_asid(), addr);
112 }
113 
114 /*
115  * As a performance optimization, other platforms preserve the fixmap mapping
116  * across a context switch, we don't presently do this, but this could be done
117  * in a similar fashion as to the wired TLB interface that sh64 uses (by way
118  * of the memorry mapped UTLB configuration) -- this unfortunately forces us to
119  * give up a TLB entry for each mapping we want to preserve. While this may be
120  * viable for a small number of fixmaps, it's not particularly useful for
121  * everything and needs to be carefully evaluated. (ie, we may want this for
122  * the vsyscall page).
123  *
124  * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
125  * in at __set_fixmap() time to determine the appropriate behavior to follow.
126  *
127  *					 -- PFM.
128  */
129 void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
130 {
131 	unsigned long address = __fix_to_virt(idx);
132 
133 	if (idx >= __end_of_fixed_addresses) {
134 		BUG();
135 		return;
136 	}
137 
138 	set_pte_phys(address, phys, prot);
139 }
140 
141 /* References to section boundaries */
142 
143 extern char _text, _etext, _edata, __bss_start, _end;
144 extern char __init_begin, __init_end;
145 
146 /*
147  * paging_init() sets up the page tables
148  *
149  * This routines also unmaps the page at virtual kernel address 0, so
150  * that we can trap those pesky NULL-reference errors in the kernel.
151  */
152 void __init paging_init(void)
153 {
154 	unsigned long zones_size[MAX_NR_ZONES] = { 0, };
155 
156 	/*
157 	 * Setup some defaults for the zone sizes.. these should be safe
158 	 * regardless of distcontiguous memory or MMU settings.
159 	 */
160 	zones_size[ZONE_DMA] = 0 >> PAGE_SHIFT;
161 	zones_size[ZONE_NORMAL] = __MEMORY_SIZE >> PAGE_SHIFT;
162 #ifdef CONFIG_HIGHMEM
163 	zones_size[ZONE_HIGHMEM] = 0 >> PAGE_SHIFT;
164 #endif
165 
166 #ifdef CONFIG_MMU
167 	/*
168 	 * If we have an MMU, and want to be using it .. we need to adjust
169 	 * the zone sizes accordingly, in addition to turning it on.
170 	 */
171 	{
172 		unsigned long max_dma, low, start_pfn;
173 		pgd_t *pg_dir;
174 		int i;
175 
176 		/* We don't need kernel mapping as hardware support that. */
177 		pg_dir = swapper_pg_dir;
178 
179 		for (i = 0; i < PTRS_PER_PGD; i++)
180 			pgd_val(pg_dir[i]) = 0;
181 
182 		/* Turn on the MMU */
183 		enable_mmu();
184 
185 		/* Fixup the zone sizes */
186 		start_pfn = START_PFN;
187 		max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
188 		low = MAX_LOW_PFN;
189 
190 		if (low < max_dma) {
191 			zones_size[ZONE_DMA] = low - start_pfn;
192 			zones_size[ZONE_NORMAL] = 0;
193 		} else {
194 			zones_size[ZONE_DMA] = max_dma - start_pfn;
195 			zones_size[ZONE_NORMAL] = low - max_dma;
196 		}
197 	}
198 
199 #elif defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
200 	/*
201 	 * If we don't have CONFIG_MMU set and the processor in question
202 	 * still has an MMU, care needs to be taken to make sure it doesn't
203 	 * stay on.. Since the boot loader could have potentially already
204 	 * turned it on, and we clearly don't want it, we simply turn it off.
205 	 *
206 	 * We don't need to do anything special for the zone sizes, since the
207 	 * default values that were already configured up above should be
208 	 * satisfactory.
209 	 */
210 	disable_mmu();
211 #endif
212 	NODE_DATA(0)->node_mem_map = NULL;
213 	free_area_init_node(0, NODE_DATA(0), zones_size, __MEMORY_START >> PAGE_SHIFT, 0);
214 }
215 
216 void __init mem_init(void)
217 {
218 	extern unsigned long empty_zero_page[1024];
219 	int codesize, reservedpages, datasize, initsize;
220 	int tmp;
221 	extern unsigned long memory_start;
222 
223 #ifdef CONFIG_MMU
224 	high_memory = (void *)__va(MAX_LOW_PFN * PAGE_SIZE);
225 #else
226 	extern unsigned long memory_end;
227 
228 	high_memory = (void *)(memory_end & PAGE_MASK);
229 #endif
230 
231 	max_mapnr = num_physpages = MAP_NR(high_memory) - MAP_NR(memory_start);
232 
233 	/* clear the zero-page */
234 	memset(empty_zero_page, 0, PAGE_SIZE);
235 	__flush_wback_region(empty_zero_page, PAGE_SIZE);
236 
237 	/*
238 	 * Setup wrappers for copy/clear_page(), these will get overridden
239 	 * later in the boot process if a better method is available.
240 	 */
241 	copy_page = copy_page_slow;
242 	clear_page = clear_page_slow;
243 
244 	/* this will put all low memory onto the freelists */
245 	totalram_pages += free_all_bootmem_node(NODE_DATA(0));
246 	reservedpages = 0;
247 	for (tmp = 0; tmp < num_physpages; tmp++)
248 		/*
249 		 * Only count reserved RAM pages
250 		 */
251 		if (PageReserved(mem_map+tmp))
252 			reservedpages++;
253 
254 	codesize =  (unsigned long) &_etext - (unsigned long) &_text;
255 	datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
256 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
257 
258 	printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init)\n",
259 		(unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
260 		max_mapnr << (PAGE_SHIFT-10),
261 		codesize >> 10,
262 		reservedpages << (PAGE_SHIFT-10),
263 		datasize >> 10,
264 		initsize >> 10);
265 
266 	p3_cache_init();
267 }
268 
269 void free_initmem(void)
270 {
271 	unsigned long addr;
272 
273 	addr = (unsigned long)(&__init_begin);
274 	for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
275 		ClearPageReserved(virt_to_page(addr));
276 		init_page_count(virt_to_page(addr));
277 		free_page(addr);
278 		totalram_pages++;
279 	}
280 	printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
281 }
282 
283 #ifdef CONFIG_BLK_DEV_INITRD
284 void free_initrd_mem(unsigned long start, unsigned long end)
285 {
286 	unsigned long p;
287 	for (p = start; p < end; p += PAGE_SIZE) {
288 		ClearPageReserved(virt_to_page(p));
289 		init_page_count(virt_to_page(p));
290 		free_page(p);
291 		totalram_pages++;
292 	}
293 	printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
294 }
295 #endif
296 
297