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

/arch/ppc/mm/init.c

https://bitbucket.org/evzijst/gittest
C | 667 lines | 460 code | 87 blank | 120 comment | 87 complexity | 6613a9b8db558b61620963b3ceb2030c MD5 | raw file
  1/*
  2 *  PowerPC version
  3 *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4 *
  5 *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  6 *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
  7 *    Copyright (C) 1996 Paul Mackerras
  8 *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  9 *  PPC44x/36-bit changes by Matt Porter (mporter@mvista.com)
 10 *
 11 *  Derived from "arch/i386/mm/init.c"
 12 *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
 13 *
 14 *  This program is free software; you can redistribute it and/or
 15 *  modify it under the terms of the GNU General Public License
 16 *  as published by the Free Software Foundation; either version
 17 *  2 of the License, or (at your option) any later version.
 18 *
 19 */
 20
 21#include <linux/config.h>
 22#include <linux/module.h>
 23#include <linux/sched.h>
 24#include <linux/kernel.h>
 25#include <linux/errno.h>
 26#include <linux/string.h>
 27#include <linux/types.h>
 28#include <linux/mm.h>
 29#include <linux/stddef.h>
 30#include <linux/init.h>
 31#include <linux/bootmem.h>
 32#include <linux/highmem.h>
 33#include <linux/initrd.h>
 34#include <linux/pagemap.h>
 35
 36#include <asm/pgalloc.h>
 37#include <asm/prom.h>
 38#include <asm/io.h>
 39#include <asm/mmu_context.h>
 40#include <asm/pgtable.h>
 41#include <asm/mmu.h>
 42#include <asm/smp.h>
 43#include <asm/machdep.h>
 44#include <asm/btext.h>
 45#include <asm/tlb.h>
 46#include <asm/bootinfo.h>
 47
 48#include "mem_pieces.h"
 49#include "mmu_decl.h"
 50
 51#if defined(CONFIG_KERNEL_START_BOOL) || defined(CONFIG_LOWMEM_SIZE_BOOL)
 52/* The ammount of lowmem must be within 0xF0000000 - KERNELBASE. */
 53#if (CONFIG_LOWMEM_SIZE > (0xF0000000 - KERNELBASE))
 54#error "You must adjust CONFIG_LOWMEM_SIZE or CONFIG_START_KERNEL"
 55#endif
 56#endif
 57#define MAX_LOW_MEM	CONFIG_LOWMEM_SIZE
 58
 59DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
 60
 61unsigned long total_memory;
 62unsigned long total_lowmem;
 63
 64unsigned long ppc_memstart;
 65unsigned long ppc_memoffset = PAGE_OFFSET;
 66
 67int mem_init_done;
 68int init_bootmem_done;
 69int boot_mapsize;
 70#ifdef CONFIG_PPC_PMAC
 71unsigned long agp_special_page;
 72#endif
 73
 74extern char _end[];
 75extern char etext[], _stext[];
 76extern char __init_begin, __init_end;
 77extern char __prep_begin, __prep_end;
 78extern char __chrp_begin, __chrp_end;
 79extern char __pmac_begin, __pmac_end;
 80extern char __openfirmware_begin, __openfirmware_end;
 81
 82#ifdef CONFIG_HIGHMEM
 83pte_t *kmap_pte;
 84pgprot_t kmap_prot;
 85
 86EXPORT_SYMBOL(kmap_prot);
 87EXPORT_SYMBOL(kmap_pte);
 88#endif
 89
 90void MMU_init(void);
 91void set_phys_avail(unsigned long total_ram);
 92
 93/* XXX should be in current.h  -- paulus */
 94extern struct task_struct *current_set[NR_CPUS];
 95
 96char *klimit = _end;
 97struct mem_pieces phys_avail;
 98
 99extern char *sysmap;
100extern unsigned long sysmap_size;
101
102/*
103 * this tells the system to map all of ram with the segregs
104 * (i.e. page tables) instead of the bats.
105 * -- Cort
106 */
107int __map_without_bats;
108int __map_without_ltlbs;
109
110/* max amount of RAM to use */
111unsigned long __max_memory;
112/* max amount of low RAM to map in */
113unsigned long __max_low_memory = MAX_LOW_MEM;
114
115void show_mem(void)
116{
117	int i,free = 0,total = 0,reserved = 0;
118	int shared = 0, cached = 0;
119	int highmem = 0;
120
121	printk("Mem-info:\n");
122	show_free_areas();
123	printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
124	i = max_mapnr;
125	while (i-- > 0) {
126		total++;
127		if (PageHighMem(mem_map+i))
128			highmem++;
129		if (PageReserved(mem_map+i))
130			reserved++;
131		else if (PageSwapCache(mem_map+i))
132			cached++;
133		else if (!page_count(mem_map+i))
134			free++;
135		else
136			shared += page_count(mem_map+i) - 1;
137	}
138	printk("%d pages of RAM\n",total);
139	printk("%d pages of HIGHMEM\n", highmem);
140	printk("%d free pages\n",free);
141	printk("%d reserved pages\n",reserved);
142	printk("%d pages shared\n",shared);
143	printk("%d pages swap cached\n",cached);
144}
145
146/* Free up now-unused memory */
147static void free_sec(unsigned long start, unsigned long end, const char *name)
148{
149	unsigned long cnt = 0;
150
151	while (start < end) {
152		ClearPageReserved(virt_to_page(start));
153		set_page_count(virt_to_page(start), 1);
154		free_page(start);
155		cnt++;
156		start += PAGE_SIZE;
157 	}
158	if (cnt) {
159		printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
160		totalram_pages += cnt;
161	}
162}
163
164void free_initmem(void)
165{
166#define FREESEC(TYPE) \
167	free_sec((unsigned long)(&__ ## TYPE ## _begin), \
168		 (unsigned long)(&__ ## TYPE ## _end), \
169		 #TYPE);
170
171	printk ("Freeing unused kernel memory:");
172	FREESEC(init);
173	if (_machine != _MACH_Pmac)
174		FREESEC(pmac);
175	if (_machine != _MACH_chrp)
176		FREESEC(chrp);
177	if (_machine != _MACH_prep)
178		FREESEC(prep);
179	if (!have_of)
180		FREESEC(openfirmware);
181 	printk("\n");
182#undef FREESEC
183}
184
185#ifdef CONFIG_BLK_DEV_INITRD
186void free_initrd_mem(unsigned long start, unsigned long end)
187{
188	printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
189
190	for (; start < end; start += PAGE_SIZE) {
191		ClearPageReserved(virt_to_page(start));
192		set_page_count(virt_to_page(start), 1);
193		free_page(start);
194		totalram_pages++;
195	}
196}
197#endif
198
199/*
200 * Check for command-line options that affect what MMU_init will do.
201 */
202void MMU_setup(void)
203{
204	/* Check for nobats option (used in mapin_ram). */
205	if (strstr(cmd_line, "nobats")) {
206		__map_without_bats = 1;
207	}
208
209	if (strstr(cmd_line, "noltlbs")) {
210		__map_without_ltlbs = 1;
211	}
212
213	/* Look for mem= option on command line */
214	if (strstr(cmd_line, "mem=")) {
215		char *p, *q;
216		unsigned long maxmem = 0;
217
218		for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
219			q = p + 4;
220			if (p > cmd_line && p[-1] != ' ')
221				continue;
222			maxmem = simple_strtoul(q, &q, 0);
223			if (*q == 'k' || *q == 'K') {
224				maxmem <<= 10;
225				++q;
226			} else if (*q == 'm' || *q == 'M') {
227				maxmem <<= 20;
228				++q;
229			}
230		}
231		__max_memory = maxmem;
232	}
233}
234
235/*
236 * MMU_init sets up the basic memory mappings for the kernel,
237 * including both RAM and possibly some I/O regions,
238 * and sets up the page tables and the MMU hardware ready to go.
239 */
240void __init MMU_init(void)
241{
242	if (ppc_md.progress)
243		ppc_md.progress("MMU:enter", 0x111);
244
245	/* parse args from command line */
246	MMU_setup();
247
248	/*
249	 * Figure out how much memory we have, how much
250	 * is lowmem, and how much is highmem.  If we were
251	 * passed the total memory size from the bootloader,
252	 * just use it.
253	 */
254	if (boot_mem_size)
255		total_memory = boot_mem_size;
256	else
257		total_memory = ppc_md.find_end_of_memory();
258
259	if (__max_memory && total_memory > __max_memory)
260		total_memory = __max_memory;
261	total_lowmem = total_memory;
262#ifdef CONFIG_FSL_BOOKE
263	/* Freescale Book-E parts expect lowmem to be mapped by fixed TLB
264	 * entries, so we need to adjust lowmem to match the amount we can map
265	 * in the fixed entries */
266	adjust_total_lowmem();
267#endif /* CONFIG_FSL_BOOKE */
268	if (total_lowmem > __max_low_memory) {
269		total_lowmem = __max_low_memory;
270#ifndef CONFIG_HIGHMEM
271		total_memory = total_lowmem;
272#endif /* CONFIG_HIGHMEM */
273	}
274	set_phys_avail(total_lowmem);
275
276	/* Initialize the MMU hardware */
277	if (ppc_md.progress)
278		ppc_md.progress("MMU:hw init", 0x300);
279	MMU_init_hw();
280
281	/* Map in all of RAM starting at KERNELBASE */
282	if (ppc_md.progress)
283		ppc_md.progress("MMU:mapin", 0x301);
284	mapin_ram();
285
286#ifdef CONFIG_HIGHMEM
287	ioremap_base = PKMAP_BASE;
288#else
289	ioremap_base = 0xfe000000UL;	/* for now, could be 0xfffff000 */
290#endif /* CONFIG_HIGHMEM */
291	ioremap_bot = ioremap_base;
292
293	/* Map in I/O resources */
294	if (ppc_md.progress)
295		ppc_md.progress("MMU:setio", 0x302);
296	if (ppc_md.setup_io_mappings)
297		ppc_md.setup_io_mappings();
298
299	/* Initialize the context management stuff */
300	mmu_context_init();
301
302	if (ppc_md.progress)
303		ppc_md.progress("MMU:exit", 0x211);
304
305#ifdef CONFIG_BOOTX_TEXT
306	/* By default, we are no longer mapped */
307       	boot_text_mapped = 0;
308	/* Must be done last, or ppc_md.progress will die. */
309	map_boot_text();
310#endif
311}
312
313/* This is only called until mem_init is done. */
314void __init *early_get_page(void)
315{
316	void *p;
317
318	if (init_bootmem_done) {
319		p = alloc_bootmem_pages(PAGE_SIZE);
320	} else {
321		p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
322	}
323	return p;
324}
325
326/*
327 * Initialize the bootmem system and give it all the memory we
328 * have available.
329 */
330void __init do_init_bootmem(void)
331{
332	unsigned long start, size;
333	int i;
334
335	/*
336	 * Find an area to use for the bootmem bitmap.
337	 * We look for the first area which is at least
338	 * 128kB in length (128kB is enough for a bitmap
339	 * for 4GB of memory, using 4kB pages), plus 1 page
340	 * (in case the address isn't page-aligned).
341	 */
342	start = 0;
343	size = 0;
344	for (i = 0; i < phys_avail.n_regions; ++i) {
345		unsigned long a = phys_avail.regions[i].address;
346		unsigned long s = phys_avail.regions[i].size;
347		if (s <= size)
348			continue;
349		start = a;
350		size = s;
351		if (s >= 33 * PAGE_SIZE)
352			break;
353	}
354	start = PAGE_ALIGN(start);
355
356	min_low_pfn = start >> PAGE_SHIFT;
357	max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
358	max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT;
359	boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
360					 PPC_MEMSTART >> PAGE_SHIFT,
361					 max_low_pfn);
362
363	/* remove the bootmem bitmap from the available memory */
364	mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
365
366	/* add everything in phys_avail into the bootmem map */
367	for (i = 0; i < phys_avail.n_regions; ++i)
368		free_bootmem(phys_avail.regions[i].address,
369			     phys_avail.regions[i].size);
370
371	init_bootmem_done = 1;
372}
373
374/*
375 * paging_init() sets up the page tables - in fact we've already done this.
376 */
377void __init paging_init(void)
378{
379	unsigned long zones_size[MAX_NR_ZONES], i;
380
381#ifdef CONFIG_HIGHMEM
382	map_page(PKMAP_BASE, 0, 0);	/* XXX gross */
383	pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k
384			(PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
385	map_page(KMAP_FIX_BEGIN, 0, 0);	/* XXX gross */
386	kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k
387			(KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
388	kmap_prot = PAGE_KERNEL;
389#endif /* CONFIG_HIGHMEM */
390
391	/*
392	 * All pages are DMA-able so we put them all in the DMA zone.
393	 */
394	zones_size[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
395	for (i = 1; i < MAX_NR_ZONES; i++)
396		zones_size[i] = 0;
397
398#ifdef CONFIG_HIGHMEM
399	zones_size[ZONE_HIGHMEM] = (total_memory - total_lowmem) >> PAGE_SHIFT;
400#endif /* CONFIG_HIGHMEM */
401
402	free_area_init(zones_size);
403}
404
405void __init mem_init(void)
406{
407	unsigned long addr;
408	int codepages = 0;
409	int datapages = 0;
410	int initpages = 0;
411#ifdef CONFIG_HIGHMEM
412	unsigned long highmem_mapnr;
413
414	highmem_mapnr = total_lowmem >> PAGE_SHIFT;
415#endif /* CONFIG_HIGHMEM */
416	max_mapnr = total_memory >> PAGE_SHIFT;
417
418	high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
419	num_physpages = max_mapnr;	/* RAM is assumed contiguous */
420
421	totalram_pages += free_all_bootmem();
422
423#ifdef CONFIG_BLK_DEV_INITRD
424	/* if we are booted from BootX with an initial ramdisk,
425	   make sure the ramdisk pages aren't reserved. */
426	if (initrd_start) {
427		for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE)
428			ClearPageReserved(virt_to_page(addr));
429	}
430#endif /* CONFIG_BLK_DEV_INITRD */
431
432#ifdef CONFIG_PPC_OF
433	/* mark the RTAS pages as reserved */
434	if ( rtas_data )
435		for (addr = (ulong)__va(rtas_data);
436		     addr < PAGE_ALIGN((ulong)__va(rtas_data)+rtas_size) ;
437		     addr += PAGE_SIZE)
438			SetPageReserved(virt_to_page(addr));
439#endif
440#ifdef CONFIG_PPC_PMAC
441	if (agp_special_page)
442		SetPageReserved(virt_to_page(agp_special_page));
443#endif
444	if ( sysmap )
445		for (addr = (unsigned long)sysmap;
446		     addr < PAGE_ALIGN((unsigned long)sysmap+sysmap_size) ;
447		     addr += PAGE_SIZE)
448			SetPageReserved(virt_to_page(addr));
449
450	for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory;
451	     addr += PAGE_SIZE) {
452		if (!PageReserved(virt_to_page(addr)))
453			continue;
454		if (addr < (ulong) etext)
455			codepages++;
456		else if (addr >= (unsigned long)&__init_begin
457			 && addr < (unsigned long)&__init_end)
458			initpages++;
459		else if (addr < (ulong) klimit)
460			datapages++;
461	}
462
463#ifdef CONFIG_HIGHMEM
464	{
465		unsigned long pfn;
466
467		for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
468			struct page *page = mem_map + pfn;
469
470			ClearPageReserved(page);
471			set_bit(PG_highmem, &page->flags);
472			set_page_count(page, 1);
473			__free_page(page);
474			totalhigh_pages++;
475		}
476		totalram_pages += totalhigh_pages;
477	}
478#endif /* CONFIG_HIGHMEM */
479
480        printk("Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
481	       (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10),
482	       codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10),
483	       initpages<< (PAGE_SHIFT-10),
484	       (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
485	if (sysmap)
486		printk("System.map loaded at 0x%08x for debugger, size: %ld bytes\n",
487			(unsigned int)sysmap, sysmap_size);
488#ifdef CONFIG_PPC_PMAC
489	if (agp_special_page)
490		printk(KERN_INFO "AGP special page: 0x%08lx\n", agp_special_page);
491#endif
492
493	mem_init_done = 1;
494}
495
496/*
497 * Set phys_avail to the amount of physical memory,
498 * less the kernel text/data/bss.
499 */
500void __init
501set_phys_avail(unsigned long total_memory)
502{
503	unsigned long kstart, ksize;
504
505	/*
506	 * Initially, available physical memory is equivalent to all
507	 * physical memory.
508	 */
509
510	phys_avail.regions[0].address = PPC_MEMSTART;
511	phys_avail.regions[0].size = total_memory;
512	phys_avail.n_regions = 1;
513
514	/*
515	 * Map out the kernel text/data/bss from the available physical
516	 * memory.
517	 */
518
519	kstart = __pa(_stext);	/* should be 0 */
520	ksize = PAGE_ALIGN(klimit - _stext);
521
522	mem_pieces_remove(&phys_avail, kstart, ksize, 0);
523	mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
524
525#if defined(CONFIG_BLK_DEV_INITRD)
526	/* Remove the init RAM disk from the available memory. */
527	if (initrd_start) {
528		mem_pieces_remove(&phys_avail, __pa(initrd_start),
529				  initrd_end - initrd_start, 1);
530	}
531#endif /* CONFIG_BLK_DEV_INITRD */
532#ifdef CONFIG_PPC_OF
533	/* remove the RTAS pages from the available memory */
534	if (rtas_data)
535		mem_pieces_remove(&phys_avail, rtas_data, rtas_size, 1);
536#endif
537	/* remove the sysmap pages from the available memory */
538	if (sysmap)
539		mem_pieces_remove(&phys_avail, __pa(sysmap), sysmap_size, 1);
540#ifdef CONFIG_PPC_PMAC
541	/* Because of some uninorth weirdness, we need a page of
542	 * memory as high as possible (it must be outside of the
543	 * bus address seen as the AGP aperture). It will be used
544	 * by the r128 DRM driver
545	 *
546	 * FIXME: We need to make sure that page doesn't overlap any of the\
547	 * above. This could be done by improving mem_pieces_find to be able
548	 * to do a backward search from the end of the list.
549	 */
550	if (_machine == _MACH_Pmac && find_devices("uni-north-agp")) {
551		agp_special_page = (total_memory - PAGE_SIZE);
552		mem_pieces_remove(&phys_avail, agp_special_page, PAGE_SIZE, 0);
553		agp_special_page = (unsigned long)__va(agp_special_page);
554	}
555#endif /* CONFIG_PPC_PMAC */
556}
557
558/* Mark some memory as reserved by removing it from phys_avail. */
559void __init reserve_phys_mem(unsigned long start, unsigned long size)
560{
561	mem_pieces_remove(&phys_avail, start, size, 1);
562}
563
564/*
565 * This is called when a page has been modified by the kernel.
566 * It just marks the page as not i-cache clean.  We do the i-cache
567 * flush later when the page is given to a user process, if necessary.
568 */
569void flush_dcache_page(struct page *page)
570{
571	clear_bit(PG_arch_1, &page->flags);
572}
573
574void flush_dcache_icache_page(struct page *page)
575{
576#ifdef CONFIG_BOOKE
577	__flush_dcache_icache(kmap(page));
578	kunmap(page);
579#else
580	__flush_dcache_icache_phys(page_to_pfn(page) << PAGE_SHIFT);
581#endif
582
583}
584void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
585{
586	clear_page(page);
587	clear_bit(PG_arch_1, &pg->flags);
588}
589
590void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
591		    struct page *pg)
592{
593	copy_page(vto, vfrom);
594	clear_bit(PG_arch_1, &pg->flags);
595}
596
597void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
598			     unsigned long addr, int len)
599{
600	unsigned long maddr;
601
602	maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
603	flush_icache_range(maddr, maddr + len);
604	kunmap(page);
605}
606
607/*
608 * This is called at the end of handling a user page fault, when the
609 * fault has been handled by updating a PTE in the linux page tables.
610 * We use it to preload an HPTE into the hash table corresponding to
611 * the updated linux PTE.
612 */
613void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
614		      pte_t pte)
615{
616	/* handle i-cache coherency */
617	unsigned long pfn = pte_pfn(pte);
618
619	if (pfn_valid(pfn)) {
620		struct page *page = pfn_to_page(pfn);
621		if (!PageReserved(page)
622		    && !test_bit(PG_arch_1, &page->flags)) {
623			if (vma->vm_mm == current->active_mm)
624				__flush_dcache_icache((void *) address);
625			else
626				flush_dcache_icache_page(page);
627			set_bit(PG_arch_1, &page->flags);
628		}
629	}
630
631#ifdef CONFIG_PPC_STD_MMU
632	/* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
633	if (Hash != 0 && pte_young(pte)) {
634		struct mm_struct *mm;
635		pmd_t *pmd;
636
637		mm = (address < TASK_SIZE)? vma->vm_mm: &init_mm;
638		pmd = pmd_offset(pgd_offset(mm, address), address);
639		if (!pmd_none(*pmd))
640			add_hash_page(mm->context, address, pmd_val(*pmd));
641	}
642#endif
643}
644
645/*
646 * This is called by /dev/mem to know if a given address has to
647 * be mapped non-cacheable or not
648 */
649int page_is_ram(unsigned long pfn)
650{
651	unsigned long paddr = (pfn << PAGE_SHIFT);
652
653	return paddr < __pa(high_memory);
654}
655
656pgprot_t phys_mem_access_prot(struct file *file, unsigned long addr,
657			      unsigned long size, pgprot_t vma_prot)
658{
659	if (ppc_md.phys_mem_access_prot)
660		return ppc_md.phys_mem_access_prot(file, addr, size, vma_prot);
661
662	if (!page_is_ram(addr >> PAGE_SHIFT))
663		vma_prot = __pgprot(pgprot_val(vma_prot)
664				    | _PAGE_GUARDED | _PAGE_NO_CACHE);
665	return vma_prot;
666}
667EXPORT_SYMBOL(phys_mem_access_prot);