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