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 by Ralf Baechle
7  * Copyright (C) 2000 Silicon Graphics, Inc.
8  *
9  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
10  * Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
11  */
12 #include <linux/config.h>
13 #include <linux/init.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/pagemap.h>
21 #include <linux/ptrace.h>
22 #include <linux/mman.h>
23 #include <linux/mm.h>
24 #include <linux/bootmem.h>
25 #include <linux/highmem.h>
26 #include <linux/swap.h>
27 #include <linux/swapctl.h>
28 #include <linux/blk.h>
29 
30 #include <asm/bootinfo.h>
31 #include <asm/cachectl.h>
32 #include <asm/cpu.h>
33 #include <asm/dma.h>
34 #include <asm/jazzdma.h>
35 #include <asm/system.h>
36 #include <asm/pgtable.h>
37 #include <asm/pgalloc.h>
38 #include <asm/mmu_context.h>
39 #include <asm/tlb.h>
40 
41 mmu_gather_t mmu_gathers[NR_CPUS];
42 unsigned long highstart_pfn, highend_pfn;
43 static unsigned long totalram_pages;
44 static unsigned long totalhigh_pages;
45 
pgd_init(unsigned long page)46 void pgd_init(unsigned long page)
47 {
48 	unsigned long *p = (unsigned long *) page;
49 	int i;
50 
51 	for (i = 0; i < USER_PTRS_PER_PGD; i+=8) {
52 		p[i + 0] = (unsigned long) invalid_pte_table;
53 		p[i + 1] = (unsigned long) invalid_pte_table;
54 		p[i + 2] = (unsigned long) invalid_pte_table;
55 		p[i + 3] = (unsigned long) invalid_pte_table;
56 		p[i + 4] = (unsigned long) invalid_pte_table;
57 		p[i + 5] = (unsigned long) invalid_pte_table;
58 		p[i + 6] = (unsigned long) invalid_pte_table;
59 		p[i + 7] = (unsigned long) invalid_pte_table;
60 	}
61 }
62 
63 /*
64  * We have upto 8 empty zeroed pages so we can map one of the right colour
65  * when needed.  This is necessary only on R4000 / R4400 SC and MC versions
66  * where we have to avoid VCED / VECI exceptions for good performance at
67  * any price.  Since page is never written to after the initialization we
68  * don't have to care about aliases on other CPUs.
69  */
70 unsigned long empty_zero_page, zero_page_mask;
71 
setup_zero_pages(void)72 static inline unsigned long setup_zero_pages(void)
73 {
74 	unsigned long order, size;
75 	struct page *page;
76 
77 	if (cpu_has_vce)
78 		order = 3;
79 	else
80 		order = 0;
81 
82 	empty_zero_page = __get_free_pages(GFP_KERNEL, order);
83 	if (!empty_zero_page)
84 		panic("Oh boy, that early out of memory?");
85 
86 	page = virt_to_page(empty_zero_page);
87 	while (page < virt_to_page(empty_zero_page + (PAGE_SIZE << order))) {
88 		set_bit(PG_reserved, &page->flags);
89 		set_page_count(page, 0);
90 		page++;
91 	}
92 
93 	size = PAGE_SIZE << order;
94 	zero_page_mask = (size - 1) & PAGE_MASK;
95 	memset((void *)empty_zero_page, 0, size);
96 
97 	return 1UL << order;
98 }
99 
do_check_pgt_cache(int low,int high)100 int do_check_pgt_cache(int low, int high)
101 {
102 	int freed = 0;
103 
104 	if (pgtable_cache_size > high) {
105 		do {
106 			if (pgd_quicklist)
107 				free_pgd_slow(get_pgd_fast()), freed++;
108 			if (pmd_quicklist)
109 				free_pmd_slow(get_pmd_fast()), freed++;
110 			if (pte_quicklist)
111 				free_pte_slow(get_pte_fast()), freed++;
112 		} while (pgtable_cache_size > low);
113 	}
114 	return freed;
115 }
116 
117 #if CONFIG_HIGHMEM
118 pte_t *kmap_pte;
119 pgprot_t kmap_prot;
120 
121 #define kmap_get_fixmap_pte(vaddr)					\
122 	pte_offset(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
123 
kmap_init(void)124 static void __init kmap_init(void)
125 {
126 	unsigned long kmap_vstart;
127 
128 	/* cache the first kmap pte */
129 	kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
130 	kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
131 
132 	kmap_prot = PAGE_KERNEL;
133 }
134 
135 #endif /* CONFIG_HIGHMEM */
136 
show_mem(void)137 void show_mem(void)
138 {
139 	int i, free = 0, total = 0, reserved = 0;
140 	int shared = 0, cached = 0;
141 
142 	printk("Mem-info:\n");
143 	show_free_areas();
144 	printk("Free swap:       %6dkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
145 	i = max_mapnr;
146 	while (i-- > 0) {
147 		total++;
148 		if (PageReserved(mem_map+i))
149 			reserved++;
150 		else if (PageSwapCache(mem_map+i))
151 			cached++;
152 		else if (!page_count(mem_map + i))
153 			free++;
154 		else
155 			shared += page_count(mem_map + i) - 1;
156 	}
157 	printk("%d pages of RAM\n", total);
158 	printk("%d reserved pages\n", reserved);
159 	printk("%d pages shared\n", shared);
160 	printk("%d pages swap cached\n",cached);
161 	printk("%ld pages in page table cache\n",pgtable_cache_size);
162 	printk("%d free pages\n", free);
163 	show_buffers();
164 }
165 
166 /* References to section boundaries */
167 
168 extern char _ftext, _etext, _fdata, _edata;
169 extern char __init_begin, __init_end;
170 
171 #ifdef CONFIG_HIGHMEM
fixrange_init(unsigned long start,unsigned long end,pgd_t * pgd_base)172 static void __init fixrange_init (unsigned long start, unsigned long end,
173 	pgd_t *pgd_base)
174 {
175 	pgd_t *pgd;
176 	pmd_t *pmd;
177 	pte_t *pte;
178 	int i, j;
179 	unsigned long vaddr;
180 
181 	vaddr = start;
182 	i = __pgd_offset(vaddr);
183 	j = __pmd_offset(vaddr);
184 	pgd = pgd_base + i;
185 
186 	for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
187 		pmd = (pmd_t *)pgd;
188 		for (; (j < PTRS_PER_PMD) && (vaddr != end); pmd++, j++) {
189 			if (pmd_none(*pmd)) {
190 				pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
191 				set_pmd(pmd, __pmd(pte));
192 				if (pte != pte_offset(pmd, 0))
193 					BUG();
194 			}
195 			vaddr += PMD_SIZE;
196 		}
197 		j = 0;
198 	}
199 }
200 #endif
201 
pagetable_init(void)202 void __init pagetable_init(void)
203 {
204 #ifdef CONFIG_HIGHMEM
205 	unsigned long vaddr;
206 	pgd_t *pgd, *pgd_base;
207 	pmd_t *pmd;
208 	pte_t *pte;
209 #endif
210 
211 	/* Initialize the entire pgd.  */
212 	pgd_init((unsigned long)swapper_pg_dir);
213 	pgd_init((unsigned long)swapper_pg_dir +
214 	         sizeof(pgd_t ) * USER_PTRS_PER_PGD);
215 
216 #ifdef CONFIG_HIGHMEM
217 	pgd_base = swapper_pg_dir;
218 
219 	/*
220 	 * Fixed mappings:
221 	 */
222 	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
223 	fixrange_init(vaddr, 0, pgd_base);
224 
225 	/*
226 	 * Permanent kmaps:
227 	 */
228 	vaddr = PKMAP_BASE;
229 	fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
230 
231 	pgd = swapper_pg_dir + __pgd_offset(vaddr);
232 	pmd = pmd_offset(pgd, vaddr);
233 	pte = pte_offset(pmd, vaddr);
234 	pkmap_page_table = pte;
235 #endif
236 }
237 
paging_init(void)238 void __init paging_init(void)
239 {
240 	unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
241 	unsigned long max_dma, high, low;
242 
243 	pagetable_init();
244 
245 #ifdef CONFIG_HIGHMEM
246 	kmap_init();
247 #endif
248 
249 	max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
250 	low = max_low_pfn;
251 	high = highend_pfn;
252 
253 #ifdef CONFIG_ISA
254 	if (low < max_dma)
255 		zones_size[ZONE_DMA] = low;
256 	else {
257 		zones_size[ZONE_DMA] = max_dma;
258 		zones_size[ZONE_NORMAL] = low - max_dma;
259 	}
260 #else
261 	zones_size[ZONE_DMA] = low;
262 #endif
263 #ifdef CONFIG_HIGHMEM
264 	if (cpu_has_dc_aliases) {
265 		printk(KERN_WARNING "This processor doesn't support highmem.");
266 		if (high - low)
267 			printk(" %dk highmem ignored", high - low);
268 		printk("\n");
269 	} else
270 		zones_size[ZONE_HIGHMEM] = high - low;
271 #endif
272 
273 	free_area_init(zones_size);
274 }
275 
276 #define PFN_UP(x)	(((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
277 #define PFN_DOWN(x)	((x) >> PAGE_SHIFT)
278 
page_is_ram(unsigned long pagenr)279 static inline int page_is_ram(unsigned long pagenr)
280 {
281 	int i;
282 
283 	for (i = 0; i < boot_mem_map.nr_map; i++) {
284 		unsigned long addr, end;
285 
286 		if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
287 			/* not usable memory */
288 			continue;
289 
290 		addr = PFN_UP(boot_mem_map.map[i].addr);
291 		end = PFN_DOWN(boot_mem_map.map[i].addr
292 			       + boot_mem_map.map[i].size);
293 
294 		if (pagenr >= addr && pagenr < end)
295 			return 1;
296 	}
297 
298 	return 0;
299 }
300 
mem_init(void)301 void __init mem_init(void)
302 {
303 	unsigned long codesize, reservedpages, datasize, initsize;
304 	unsigned long tmp, ram;
305 
306 #if defined(CONFIG_DISCONTIGMEM) && defined(CONFIG_HIGHMEM)
307 #error "CONFIG_HIGHMEM and CONFIG_DISCONTIGMEM dont work together yet"
308 #endif
309 
310 #ifdef CONFIG_HIGHMEM
311 	highstart_pfn = (KSEG1 - KSEG0) >> PAGE_SHIFT;
312 	highmem_start_page = mem_map + highstart_pfn;
313 	max_mapnr = num_physpages = highend_pfn;
314 	num_mappedpages = max_low_pfn;
315 #else
316 	max_mapnr = num_mappedpages = num_physpages = max_low_pfn;
317 #endif
318 	high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
319 
320 	totalram_pages += free_all_bootmem();
321 	totalram_pages -= setup_zero_pages();	/* Setup zeroed pages.  */
322 
323 	reservedpages = ram = 0;
324 	for (tmp = 0; tmp < max_low_pfn; tmp++)
325 		if (page_is_ram(tmp)) {
326 			ram++;
327 			if (PageReserved(mem_map+tmp))
328 				reservedpages++;
329 		}
330 
331 #ifdef CONFIG_HIGHMEM
332 	for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
333 		struct page *page = mem_map + tmp;
334 
335 		if (!page_is_ram(tmp)) {
336 			SetPageReserved(page);
337 			continue;
338 		}
339 		ClearPageReserved(page);
340 		set_bit(PG_highmem, &page->flags);
341 		atomic_set(&page->count, 1);
342 		__free_page(page);
343 		totalhigh_pages++;
344 	}
345 	totalram_pages += totalhigh_pages;
346 #endif
347 
348 	codesize =  (unsigned long) &_etext - (unsigned long) &_ftext;
349 	datasize =  (unsigned long) &_edata - (unsigned long) &_fdata;
350 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
351 
352 	printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
353 	       "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
354 	       (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
355 	       ram << (PAGE_SHIFT-10),
356 	       codesize >> 10,
357 	       reservedpages << (PAGE_SHIFT-10),
358 	       datasize >> 10,
359 	       initsize >> 10,
360 	       (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
361 }
362 
363 #ifdef CONFIG_BLK_DEV_INITRD
free_initrd_mem(unsigned long start,unsigned long end)364 void free_initrd_mem(unsigned long start, unsigned long end)
365 {
366 	if (start < end)
367 		printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
368 		       (end - start) >> 10);
369 
370 	for (; start < end; start += PAGE_SIZE) {
371 		ClearPageReserved(virt_to_page(start));
372 		set_page_count(virt_to_page(start), 1);
373 		free_page(start);
374 		totalram_pages++;
375 	}
376 }
377 #endif
378 
379 extern char __init_begin, __init_end;
380 extern void prom_free_prom_memory(void) __init;
381 
free_initmem(void)382 void free_initmem(void)
383 {
384 	unsigned long addr;
385 
386 	prom_free_prom_memory ();
387 
388 	addr = (unsigned long) &__init_begin;
389 	while (addr < (unsigned long) &__init_end) {
390 		ClearPageReserved(virt_to_page(addr));
391 		set_page_count(virt_to_page(addr), 1);
392 		free_page(addr);
393 		totalram_pages++;
394 		addr += PAGE_SIZE;
395 	}
396 	printk(KERN_INFO "Freeing unused kernel memory: %dk freed\n",
397 	       (&__init_end - &__init_begin) >> 10);
398 }
399 
si_meminfo(struct sysinfo * val)400 void si_meminfo(struct sysinfo *val)
401 {
402 	val->totalram = totalram_pages;
403 	val->sharedram = 0;
404 	val->freeram = nr_free_pages();
405 	val->bufferram = atomic_read(&buffermem_pages);
406 	val->totalhigh = totalhigh_pages;
407 	val->freehigh = nr_free_highpages();
408 	val->mem_unit = PAGE_SIZE;
409 
410 	return;
411 }
412