xref: /linux/arch/mips/mm/init.c (revision f06a96844a577c43249fce25809a4fae07407f46)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1994 - 2000 Ralf Baechle
7  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
9  * Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
10  */
11 #include <linux/init.h>
12 #include <linux/module.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/pagemap.h>
20 #include <linux/ptrace.h>
21 #include <linux/mman.h>
22 #include <linux/mm.h>
23 #include <linux/bootmem.h>
24 #include <linux/highmem.h>
25 #include <linux/swap.h>
26 #include <linux/proc_fs.h>
27 #include <linux/pfn.h>
28 
29 #include <asm/bootinfo.h>
30 #include <asm/cachectl.h>
31 #include <asm/cpu.h>
32 #include <asm/dma.h>
33 #include <asm/mmu_context.h>
34 #include <asm/sections.h>
35 #include <asm/pgtable.h>
36 #include <asm/pgalloc.h>
37 #include <asm/tlb.h>
38 
39 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
40 
41 unsigned long highstart_pfn, highend_pfn;
42 
43 /*
44  * We have up to 8 empty zeroed pages so we can map one of the right colour
45  * when needed.  This is necessary only on R4000 / R4400 SC and MC versions
46  * where we have to avoid VCED / VECI exceptions for good performance at
47  * any price.  Since page is never written to after the initialization we
48  * don't have to care about aliases on other CPUs.
49  */
50 unsigned long empty_zero_page, zero_page_mask;
51 
52 /*
53  * Not static inline because used by IP27 special magic initialization code
54  */
55 unsigned long setup_zero_pages(void)
56 {
57 	unsigned int order;
58 	unsigned long size;
59 	struct page *page;
60 
61 	if (cpu_has_vce)
62 		order = 3;
63 	else
64 		order = 0;
65 
66 	empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
67 	if (!empty_zero_page)
68 		panic("Oh boy, that early out of memory?");
69 
70 	page = virt_to_page(empty_zero_page);
71 	split_page(page, order);
72 	while (page < virt_to_page(empty_zero_page + (PAGE_SIZE << order))) {
73 		SetPageReserved(page);
74 		page++;
75 	}
76 
77 	size = PAGE_SIZE << order;
78 	zero_page_mask = (size - 1) & PAGE_MASK;
79 
80 	return 1UL << order;
81 }
82 
83 #ifdef CONFIG_HIGHMEM
84 pte_t *kmap_pte;
85 pgprot_t kmap_prot;
86 
87 #define kmap_get_fixmap_pte(vaddr)					\
88 	pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr)), (vaddr))
89 
90 static void __init kmap_init(void)
91 {
92 	unsigned long kmap_vstart;
93 
94 	/* cache the first kmap pte */
95 	kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
96 	kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
97 
98 	kmap_prot = PAGE_KERNEL;
99 }
100 
101 #ifdef CONFIG_32BIT
102 void __init fixrange_init(unsigned long start, unsigned long end,
103 	pgd_t *pgd_base)
104 {
105 	pgd_t *pgd;
106 	pud_t *pud;
107 	pmd_t *pmd;
108 	pte_t *pte;
109 	int i, j, k;
110 	unsigned long vaddr;
111 
112 	vaddr = start;
113 	i = __pgd_offset(vaddr);
114 	j = __pud_offset(vaddr);
115 	k = __pmd_offset(vaddr);
116 	pgd = pgd_base + i;
117 
118 	for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
119 		pud = (pud_t *)pgd;
120 		for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
121 			pmd = (pmd_t *)pud;
122 			for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
123 				if (pmd_none(*pmd)) {
124 					pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
125 					set_pmd(pmd, __pmd(pte));
126 					if (pte != pte_offset_kernel(pmd, 0))
127 						BUG();
128 				}
129 				vaddr += PMD_SIZE;
130 			}
131 			k = 0;
132 		}
133 		j = 0;
134 	}
135 }
136 #endif /* CONFIG_32BIT */
137 #endif /* CONFIG_HIGHMEM */
138 
139 #ifndef CONFIG_NEED_MULTIPLE_NODES
140 extern void pagetable_init(void);
141 
142 static int __init page_is_ram(unsigned long pagenr)
143 {
144 	int i;
145 
146 	for (i = 0; i < boot_mem_map.nr_map; i++) {
147 		unsigned long addr, end;
148 
149 		if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
150 			/* not usable memory */
151 			continue;
152 
153 		addr = PFN_UP(boot_mem_map.map[i].addr);
154 		end = PFN_DOWN(boot_mem_map.map[i].addr +
155 			       boot_mem_map.map[i].size);
156 
157 		if (pagenr >= addr && pagenr < end)
158 			return 1;
159 	}
160 
161 	return 0;
162 }
163 
164 void __init paging_init(void)
165 {
166 	unsigned long zones_size[] = { 0, };
167 	unsigned long max_dma, high, low;
168 #ifndef CONFIG_FLATMEM
169 	unsigned long zholes_size[] = { 0, };
170 	unsigned long i, j, pfn;
171 #endif
172 
173 	pagetable_init();
174 
175 #ifdef CONFIG_HIGHMEM
176 	kmap_init();
177 #endif
178 
179 	max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
180 	low = max_low_pfn;
181 	high = highend_pfn;
182 
183 #ifdef CONFIG_ISA
184 	if (low < max_dma)
185 		zones_size[ZONE_DMA] = low;
186 	else {
187 		zones_size[ZONE_DMA] = max_dma;
188 		zones_size[ZONE_NORMAL] = low - max_dma;
189 	}
190 #else
191 	zones_size[ZONE_DMA] = low;
192 #endif
193 #ifdef CONFIG_HIGHMEM
194 	if (cpu_has_dc_aliases) {
195 		printk(KERN_WARNING "This processor doesn't support highmem.");
196 		if (high - low)
197 			printk(" %ldk highmem ignored", high - low);
198 		printk("\n");
199 	} else
200 		zones_size[ZONE_HIGHMEM] = high - low;
201 #endif
202 
203 #ifdef CONFIG_FLATMEM
204 	free_area_init(zones_size);
205 #else
206 	pfn = 0;
207 	for (i = 0; i < MAX_NR_ZONES; i++)
208 		for (j = 0; j < zones_size[i]; j++, pfn++)
209 			if (!page_is_ram(pfn))
210 				zholes_size[i]++;
211 	free_area_init_node(0, NODE_DATA(0), zones_size, 0, zholes_size);
212 #endif
213 }
214 
215 static struct kcore_list kcore_mem, kcore_vmalloc;
216 #ifdef CONFIG_64BIT
217 static struct kcore_list kcore_kseg0;
218 #endif
219 
220 void __init mem_init(void)
221 {
222 	unsigned long codesize, reservedpages, datasize, initsize;
223 	unsigned long tmp, ram;
224 
225 #ifdef CONFIG_HIGHMEM
226 #ifdef CONFIG_DISCONTIGMEM
227 #error "CONFIG_HIGHMEM and CONFIG_DISCONTIGMEM dont work together yet"
228 #endif
229 	max_mapnr = highend_pfn;
230 #else
231 	max_mapnr = max_low_pfn;
232 #endif
233 	high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
234 
235 	totalram_pages += free_all_bootmem();
236 	totalram_pages -= setup_zero_pages();	/* Setup zeroed pages.  */
237 
238 	reservedpages = ram = 0;
239 	for (tmp = 0; tmp < max_low_pfn; tmp++)
240 		if (page_is_ram(tmp)) {
241 			ram++;
242 			if (PageReserved(pfn_to_page(tmp)))
243 				reservedpages++;
244 		}
245 	num_physpages = ram;
246 
247 #ifdef CONFIG_HIGHMEM
248 	for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
249 		struct page *page = mem_map + tmp;
250 
251 		if (!page_is_ram(tmp)) {
252 			SetPageReserved(page);
253 			continue;
254 		}
255 		ClearPageReserved(page);
256 #ifdef CONFIG_LIMITED_DMA
257 		set_page_address(page, lowmem_page_address(page));
258 #endif
259 		init_page_count(page);
260 		__free_page(page);
261 		totalhigh_pages++;
262 	}
263 	totalram_pages += totalhigh_pages;
264 	num_physpages += totalhigh_pages;
265 #endif
266 
267 	codesize =  (unsigned long) &_etext - (unsigned long) &_text;
268 	datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
269 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
270 
271 #ifdef CONFIG_64BIT
272 	if ((unsigned long) &_text > (unsigned long) CKSEG0)
273 		/* The -4 is a hack so that user tools don't have to handle
274 		   the overflow.  */
275 		kclist_add(&kcore_kseg0, (void *) CKSEG0, 0x80000000 - 4);
276 #endif
277 	kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
278 	kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
279 		   VMALLOC_END-VMALLOC_START);
280 
281 	printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
282 	       "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
283 	       (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
284 	       ram << (PAGE_SHIFT-10),
285 	       codesize >> 10,
286 	       reservedpages << (PAGE_SHIFT-10),
287 	       datasize >> 10,
288 	       initsize >> 10,
289 	       (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
290 }
291 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
292 
293 void free_init_pages(char *what, unsigned long begin, unsigned long end)
294 {
295 	unsigned long addr;
296 
297 	for (addr = begin; addr < end; addr += PAGE_SIZE) {
298 		ClearPageReserved(virt_to_page(addr));
299 		init_page_count(virt_to_page(addr));
300 		memset((void *)addr, 0xcc, PAGE_SIZE);
301 		free_page(addr);
302 		totalram_pages++;
303 	}
304 	printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
305 }
306 
307 #ifdef CONFIG_BLK_DEV_INITRD
308 void free_initrd_mem(unsigned long start, unsigned long end)
309 {
310 #ifdef CONFIG_64BIT
311 	/* Switch from KSEG0 to XKPHYS addresses */
312 	start = (unsigned long)phys_to_virt(CPHYSADDR(start));
313 	end = (unsigned long)phys_to_virt(CPHYSADDR(end));
314 #endif
315 	free_init_pages("initrd memory", start, end);
316 }
317 #endif
318 
319 extern unsigned long prom_free_prom_memory(void);
320 
321 void free_initmem(void)
322 {
323 	unsigned long start, end, freed;
324 
325 	freed = prom_free_prom_memory();
326 	if (freed)
327 		printk(KERN_INFO "Freeing firmware memory: %ldk freed\n",freed);
328 
329 	start = (unsigned long)(&__init_begin);
330 	end = (unsigned long)(&__init_end);
331 #ifdef CONFIG_64BIT
332 	start = PAGE_OFFSET | CPHYSADDR(start);
333 	end = PAGE_OFFSET | CPHYSADDR(end);
334 #endif
335 	free_init_pages("unused kernel memory", start, end);
336 }
337