PageRenderTime 50ms CodeModel.GetById 24ms app.highlight 21ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/sparc/mm/init_32.c

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