1 /*  $Id: init.c,v 1.103 2001/11/19 19:03:08 davem Exp $
2  *  linux/arch/sparc/mm/init.c
3  *
4  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  *  Copyright (C) 1995 Eddie C. Dost (ecd@skynet.be)
6  *  Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7  *  Copyright (C) 2000 Anton Blanchard (anton@samba.org)
8  */
9 
10 #include <linux/config.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/ptrace.h>
18 #include <linux/mman.h>
19 #include <linux/mm.h>
20 #include <linux/swap.h>
21 #include <linux/swapctl.h>
22 #ifdef CONFIG_BLK_DEV_INITRD
23 #include <linux/blk.h>
24 #endif
25 #include <linux/init.h>
26 #include <linux/highmem.h>
27 #include <linux/bootmem.h>
28 
29 #include <asm/system.h>
30 #include <asm/segment.h>
31 #include <asm/vac-ops.h>
32 #include <asm/page.h>
33 #include <asm/pgtable.h>
34 #include <asm/vaddrs.h>
35 #include <asm/tlb.h>
36 
37 mmu_gather_t mmu_gathers[NR_CPUS];
38 
39 unsigned long *sparc_valid_addr_bitmap;
40 
41 unsigned long phys_base;
42 
43 unsigned long page_kernel;
44 
45 struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
46 unsigned long sparc_unmapped_base;
47 
48 struct pgtable_cache_struct pgt_quicklists;
49 
50 /* References to section boundaries */
51 extern char __init_begin, __init_end, _start, _end, etext , edata;
52 
53 /* Initial ramdisk setup */
54 extern unsigned int sparc_ramdisk_image;
55 extern unsigned int sparc_ramdisk_size;
56 
57 unsigned long highstart_pfn, highend_pfn;
58 unsigned long totalram_pages;
59 unsigned long totalhigh_pages;
60 
61 pte_t *kmap_pte;
62 pgprot_t kmap_prot;
63 
64 /* These are set in {srmmu,sun4c}_paging_init() */
65 unsigned long fix_kmap_begin;
66 unsigned long fix_kmap_end;
67 
68 #define kmap_get_fixed_pte(vaddr) \
69 	pte_offset(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
70 
kmap_init(void)71 void __init kmap_init(void)
72 {
73 	/* cache the first kmap pte */
74 	kmap_pte = kmap_get_fixed_pte(fix_kmap_begin);
75 	kmap_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV | SRMMU_CACHE);
76 }
77 
show_mem(void)78 void show_mem(void)
79 {
80 	printk("Mem-info:\n");
81 	show_free_areas();
82 	printk("Free swap:       %6dkB\n",
83 	       nr_swap_pages << (PAGE_SHIFT-10));
84 	printk("%ld pages of RAM\n", totalram_pages);
85 	printk("%d free pages\n", nr_free_pages());
86 	printk("%ld pages in page table cache\n",pgtable_cache_size);
87 #ifndef CONFIG_SMP
88 	if (sparc_cpu_model == sun4m || sparc_cpu_model == sun4d)
89 		printk("%ld entries in page dir cache\n",pgd_cache_size);
90 #endif
91 	show_buffers();
92 }
93 
94 extern pgprot_t protection_map[16];
95 
sparc_context_init(int numctx)96 void __init sparc_context_init(int numctx)
97 {
98 	int ctx;
99 
100 	ctx_list_pool = __alloc_bootmem(numctx * sizeof(struct ctx_list), SMP_CACHE_BYTES, 0UL);
101 
102 	for(ctx = 0; ctx < numctx; ctx++) {
103 		struct ctx_list *clist;
104 
105 		clist = (ctx_list_pool + ctx);
106 		clist->ctx_number = ctx;
107 		clist->ctx_mm = 0;
108 	}
109 	ctx_free.next = ctx_free.prev = &ctx_free;
110 	ctx_used.next = ctx_used.prev = &ctx_used;
111 	for(ctx = 0; ctx < numctx; ctx++)
112 		add_to_free_ctxlist(ctx_list_pool + ctx);
113 }
114 
115 #define DEBUG_BOOTMEM
116 
117 extern unsigned long cmdline_memory_size;
118 unsigned long last_valid_pfn;
119 
calc_highpages(void)120 unsigned long calc_highpages(void)
121 {
122 	int i;
123 	int nr = 0;
124 
125 	for (i = 0; sp_banks[i].num_bytes != 0; i++) {
126 		unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
127 		unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
128 
129 		if (end_pfn <= max_low_pfn)
130 			continue;
131 
132 		if (start_pfn < max_low_pfn)
133 			start_pfn = max_low_pfn;
134 
135 		nr += end_pfn - start_pfn;
136 	}
137 
138 	return nr;
139 }
140 
calc_max_low_pfn(void)141 unsigned long calc_max_low_pfn(void)
142 {
143 	int i;
144 	unsigned long tmp = (SRMMU_MAXMEM >> PAGE_SHIFT);
145 	unsigned long curr_pfn, last_pfn;
146 
147 	last_pfn = (sp_banks[0].base_addr + sp_banks[0].num_bytes) >> PAGE_SHIFT;
148 	for (i = 1; sp_banks[i].num_bytes != 0; i++) {
149 		curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
150 
151 		if (curr_pfn >= tmp) {
152 			if (last_pfn < tmp)
153 				tmp = last_pfn;
154 			break;
155 		}
156 
157 		last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
158 	}
159 
160 	return tmp;
161 }
162 
bootmem_init(unsigned long * pages_avail)163 unsigned long __init bootmem_init(unsigned long *pages_avail)
164 {
165 	unsigned long bootmap_size, start_pfn, max_pfn;
166 	unsigned long end_of_phys_memory = 0UL;
167 	unsigned long bootmap_pfn, bytes_avail, size;
168 	int i;
169 
170 #ifdef DEBUG_BOOTMEM
171 	prom_printf("bootmem_init: Scan sp_banks,  ");
172 #endif
173 	bytes_avail = 0UL;
174 	for (i = 0; sp_banks[i].num_bytes != 0; i++) {
175 		end_of_phys_memory = sp_banks[i].base_addr +
176 			sp_banks[i].num_bytes;
177 		bytes_avail += sp_banks[i].num_bytes;
178 		if (cmdline_memory_size) {
179 			if (bytes_avail > cmdline_memory_size) {
180 				unsigned long slack = bytes_avail - cmdline_memory_size;
181 
182 				bytes_avail -= slack;
183 				end_of_phys_memory -= slack;
184 
185 				sp_banks[i].num_bytes -= slack;
186 				if (sp_banks[i].num_bytes == 0) {
187 					sp_banks[i].base_addr = 0xdeadbeef;
188 				} else {
189 					sp_banks[i+1].num_bytes = 0;
190 					sp_banks[i+1].base_addr = 0xdeadbeef;
191 				}
192 				break;
193 			}
194 		}
195 	}
196 
197 	/* Start with page aligned address of last symbol in kernel
198 	 * image.
199 	 */
200 	start_pfn  = (unsigned long)__pa(PAGE_ALIGN((unsigned long) &_end));
201 
202 	/* Adjust up to the physical address where the kernel begins. */
203 	start_pfn += phys_base;
204 
205 	/* Now shift down to get the real physical page frame number. */
206 	start_pfn >>= PAGE_SHIFT;
207 
208 	bootmap_pfn = start_pfn;
209 
210 	max_pfn = end_of_phys_memory >> PAGE_SHIFT;
211 
212 	max_low_pfn = max_pfn;
213 	highstart_pfn = highend_pfn = max_pfn;
214 
215 	if (max_low_pfn > (SRMMU_MAXMEM >> PAGE_SHIFT)) {
216 		highstart_pfn = (SRMMU_MAXMEM >> PAGE_SHIFT);
217 		max_low_pfn = calc_max_low_pfn();
218 		printk(KERN_NOTICE "%ldMB HIGHMEM available.\n", calc_highpages());
219 	}
220 
221 #ifdef CONFIG_BLK_DEV_INITRD
222 	/* Now have to check initial ramdisk, so that bootmap does not overwrite it */
223 	if (sparc_ramdisk_image) {
224 		if (sparc_ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
225 			sparc_ramdisk_image -= KERNBASE;
226 		initrd_start = sparc_ramdisk_image + phys_base;
227 		initrd_end = initrd_start + sparc_ramdisk_size;
228 		if (initrd_end > end_of_phys_memory) {
229 			printk(KERN_CRIT "initrd extends beyond end of memory "
230 		                 	 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
231 			       initrd_end, end_of_phys_memory);
232 			initrd_start = 0;
233 		}
234 		if (initrd_start) {
235 			if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
236 			    initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
237 				bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
238 		}
239 	}
240 #endif
241 	/* Initialize the boot-time allocator. */
242 #ifdef DEBUG_BOOTMEM
243 	prom_printf("init_bootmem(spfn[%lx],bpfn[%lx],mlpfn[%lx])\n",
244 		    start_pfn, bootmap_pfn, max_low_pfn);
245 #endif
246 	bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, phys_base>>PAGE_SHIFT, max_low_pfn);
247 
248 	/* Now register the available physical memory with the
249 	 * allocator.
250 	 */
251 	*pages_avail = 0;
252 	for (i = 0; sp_banks[i].num_bytes != 0; i++) {
253 		unsigned long curr_pfn, last_pfn;
254 
255 		curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
256 		if (curr_pfn >= max_low_pfn)
257 			break;
258 
259 		last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
260 		if (last_pfn > max_low_pfn)
261 			last_pfn = max_low_pfn;
262 
263 		/*
264 		 * .. finally, did all the rounding and playing
265 		 * around just make the area go away?
266 		 */
267 		if (last_pfn <= curr_pfn)
268 			continue;
269 
270 		size = (last_pfn - curr_pfn) << PAGE_SHIFT;
271 		*pages_avail += last_pfn - curr_pfn;
272 #ifdef DEBUG_BOOTMEM
273 		prom_printf("free_bootmem: base[%lx] size[%lx]\n",
274 			    sp_banks[i].base_addr,
275 			    size);
276 #endif
277 		free_bootmem(sp_banks[i].base_addr,
278 			     size);
279 	}
280 
281 #ifdef CONFIG_BLK_DEV_INITRD
282 	if (initrd_start) {
283 		size = initrd_end - initrd_start;
284 #ifdef DEBUG_BOOTMEM
285 		prom_printf("reserve_bootmem: base[%lx] size[%lx]\n",
286 			     initrd_start, size);
287 #endif
288 		/* Reserve the initrd image area. */
289 		reserve_bootmem(initrd_start, size);
290 		*pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
291 
292 		initrd_start += PAGE_OFFSET;
293 		initrd_end += PAGE_OFFSET;
294 	}
295 #endif
296 	/* Reserve the kernel text/data/bss. */
297 	size = (start_pfn << PAGE_SHIFT) - phys_base;
298 #ifdef DEBUG_BOOTMEM
299 	prom_printf("reserve_bootmem: base[%lx] size[%lx]\n", phys_base, size);
300 #endif
301 	reserve_bootmem(phys_base, size);
302 	*pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
303 
304 	/* Reserve the bootmem map.   We do not account for it
305 	 * in pages_avail because we will release that memory
306 	 * in free_all_bootmem.
307 	 */
308 	size = bootmap_size;
309 #ifdef DEBUG_BOOTMEM
310 	prom_printf("reserve_bootmem: base[%lx] size[%lx]\n",
311 		    (bootmap_pfn << PAGE_SHIFT), size);
312 #endif
313 	reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
314 	*pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
315 
316 	return max_pfn;
317 }
318 
319 /*
320  * paging_init() sets up the page tables: We call the MMU specific
321  * init routine based upon the Sun model type on the Sparc.
322  *
323  */
324 extern void sun4c_paging_init(void);
325 extern void srmmu_paging_init(void);
326 extern void device_scan(void);
327 
paging_init(void)328 void __init paging_init(void)
329 {
330 	switch(sparc_cpu_model) {
331 	case sun4c:
332 	case sun4e:
333 	case sun4:
334 		sun4c_paging_init();
335 		sparc_unmapped_base = 0xe0000000;
336 		BTFIXUPSET_SETHI(sparc_unmapped_base, 0xe0000000);
337 		break;
338 	case sun4m:
339 	case sun4d:
340 		srmmu_paging_init();
341 		sparc_unmapped_base = 0x50000000;
342 		BTFIXUPSET_SETHI(sparc_unmapped_base, 0x50000000);
343 		break;
344 	default:
345 		prom_printf("paging_init: Cannot init paging on this Sparc\n");
346 		prom_printf("paging_init: sparc_cpu_model = %d\n", sparc_cpu_model);
347 		prom_printf("paging_init: Halting...\n");
348 		prom_halt();
349 	};
350 
351 	/* Initialize the protection map with non-constant, MMU dependent values. */
352 	protection_map[0] = PAGE_NONE;
353 	protection_map[1] = PAGE_READONLY;
354 	protection_map[2] = PAGE_COPY;
355 	protection_map[3] = PAGE_COPY;
356 	protection_map[4] = PAGE_READONLY;
357 	protection_map[5] = PAGE_READONLY;
358 	protection_map[6] = PAGE_COPY;
359 	protection_map[7] = PAGE_COPY;
360 	protection_map[8] = PAGE_NONE;
361 	protection_map[9] = PAGE_READONLY;
362 	protection_map[10] = PAGE_SHARED;
363 	protection_map[11] = PAGE_SHARED;
364 	protection_map[12] = PAGE_READONLY;
365 	protection_map[13] = PAGE_READONLY;
366 	protection_map[14] = PAGE_SHARED;
367 	protection_map[15] = PAGE_SHARED;
368 	btfixup();
369 	device_scan();
370 }
371 
372 struct cache_palias *sparc_aliases;
373 
taint_real_pages(void)374 static void __init taint_real_pages(void)
375 {
376 	int i;
377 
378 	for (i = 0; sp_banks[i].num_bytes; i++) {
379 		unsigned long start, end;
380 
381 		start = sp_banks[i].base_addr;
382 		end = start + sp_banks[i].num_bytes;
383 
384 		while (start < end) {
385 			set_bit (start >> 20,
386 				sparc_valid_addr_bitmap);
387 				start += PAGE_SIZE;
388 		}
389 	}
390 }
391 
map_high_region(unsigned long start_pfn,unsigned long end_pfn)392 void map_high_region(unsigned long start_pfn, unsigned long end_pfn)
393 {
394 	unsigned long tmp;
395 
396 #ifdef DEBUG_HIGHMEM
397 	printk("mapping high region %08lx - %08lx\n", start_pfn, end_pfn);
398 #endif
399 
400 	for (tmp = start_pfn; tmp < end_pfn; tmp++) {
401 		struct page *page = mem_map + tmp;
402 
403 		ClearPageReserved(page);
404 		set_bit(PG_highmem, &page->flags);
405 		atomic_set(&page->count, 1);
406 		__free_page(page);
407 		totalhigh_pages++;
408 	}
409 }
410 
mem_init(void)411 void __init mem_init(void)
412 {
413 	int codepages = 0;
414 	int datapages = 0;
415 	int initpages = 0;
416 	int i;
417 
418 	highmem_start_page = mem_map + highstart_pfn;
419 
420 	/* Saves us work later. */
421 	memset((void *)&empty_zero_page, 0, PAGE_SIZE);
422 
423 	i = last_valid_pfn >> ((20 - PAGE_SHIFT) + 5);
424 	i += 1;
425 	sparc_valid_addr_bitmap = (unsigned long *)
426 		__alloc_bootmem(i << 2, SMP_CACHE_BYTES, 0UL);
427 
428 	if (sparc_valid_addr_bitmap == NULL) {
429 		prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
430 		prom_halt();
431 	}
432 	memset(sparc_valid_addr_bitmap, 0, i << 2);
433 
434 	taint_real_pages();
435 
436 	max_mapnr = last_valid_pfn - (phys_base >> PAGE_SHIFT);
437 	high_memory = __va(max_low_pfn << PAGE_SHIFT);
438 
439 #ifdef DEBUG_BOOTMEM
440 	prom_printf("mem_init: Calling free_all_bootmem().\n");
441 #endif
442 	num_physpages = totalram_pages = free_all_bootmem();
443 
444 	for (i = 0; sp_banks[i].num_bytes != 0; i++) {
445 		unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
446 		unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
447 
448 		if (end_pfn <= highstart_pfn)
449 			continue;
450 
451 		if (start_pfn < highstart_pfn)
452 			start_pfn = highstart_pfn;
453 
454 		map_high_region(start_pfn, end_pfn);
455 	}
456 
457 	totalram_pages += totalhigh_pages;
458 
459 	codepages = (((unsigned long) &etext) - ((unsigned long)&_start));
460 	codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
461 	datapages = (((unsigned long) &edata) - ((unsigned long)&etext));
462 	datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
463 	initpages = (((unsigned long) &__init_end) - ((unsigned long) &__init_begin));
464 	initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
465 
466 	printk(KERN_INFO "Memory: %dk available (%dk kernel code, %dk data, %dk init, %ldk highmem) [%08lx,%08lx]\n",
467 	       nr_free_pages() << (PAGE_SHIFT-10),
468 	       codepages << (PAGE_SHIFT-10),
469 	       datapages << (PAGE_SHIFT-10),
470 	       initpages << (PAGE_SHIFT-10),
471 	       totalhigh_pages << (PAGE_SHIFT-10),
472 	       (unsigned long)PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
473 }
474 
free_initmem(void)475 void free_initmem (void)
476 {
477 	unsigned long addr;
478 
479 	addr = (unsigned long)(&__init_begin);
480 	for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
481 		unsigned long page;
482 		struct page *p;
483 
484 		page = addr + phys_base;
485 		p = virt_to_page(page);
486 
487 		ClearPageReserved(p);
488 		set_page_count(p, 1);
489 		__free_page(p);
490 		totalram_pages++;
491 		num_physpages++;
492 	}
493 	printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
494 }
495 
496 #ifdef CONFIG_BLK_DEV_INITRD
free_initrd_mem(unsigned long start,unsigned long end)497 void free_initrd_mem(unsigned long start, unsigned long end)
498 {
499 	if (start < end)
500 		printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
501 	for (; start < end; start += PAGE_SIZE) {
502 		struct page *p = virt_to_page(start);
503 
504 		ClearPageReserved(p);
505 		set_page_count(p, 1);
506 		__free_page(p);
507 		num_physpages++;
508 	}
509 }
510 #endif
511 
si_meminfo(struct sysinfo * val)512 void si_meminfo(struct sysinfo *val)
513 {
514 	val->totalram = totalram_pages;
515 	val->sharedram = 0;
516 	val->freeram = nr_free_pages();
517 	val->bufferram = atomic_read(&buffermem_pages);
518 	val->totalhigh = totalhigh_pages;
519 	val->freehigh = nr_free_highpages();
520 
521 	val->mem_unit = PAGE_SIZE;
522 }
523 
flush_page_to_ram(struct page * page)524 void flush_page_to_ram(struct page *page)
525 {
526 	unsigned long vaddr = (unsigned long)page_address(page);
527 
528 	if (vaddr)
529 		__flush_page_to_ram(vaddr);
530 }
531