initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / ppc / mm / init.c
blobcbed55208445780be258dc135065c5e7ab168641
1 /*
2 * PowerPC version
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
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)
11 * Derived from "arch/i386/mm/init.c"
12 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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.
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>
35 #include <asm/pgalloc.h>
36 #include <asm/prom.h>
37 #include <asm/io.h>
38 #include <asm/mmu_context.h>
39 #include <asm/pgtable.h>
40 #include <asm/mmu.h>
41 #include <asm/smp.h>
42 #include <asm/machdep.h>
43 #include <asm/btext.h>
44 #include <asm/tlb.h>
45 #include <asm/bootinfo.h>
47 #include "mem_pieces.h"
48 #include "mmu_decl.h"
50 #if defined(CONFIG_KERNEL_START_BOOL) || defined(CONFIG_LOWMEM_SIZE_BOOL)
51 /* The ammount of lowmem must be within 0xF0000000 - KERNELBASE. */
52 #if (CONFIG_LOWMEM_SIZE > (0xF0000000 - KERNELBASE))
53 #error "You must adjust CONFIG_LOWMEM_SIZE or CONFIG_START_KERNEL"
54 #endif
55 #endif
56 #define MAX_LOW_MEM CONFIG_LOWMEM_SIZE
58 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
60 unsigned long total_memory;
61 unsigned long total_lowmem;
63 unsigned long ppc_memstart;
64 unsigned long ppc_memoffset = PAGE_OFFSET;
66 int mem_init_done;
67 int init_bootmem_done;
68 int boot_mapsize;
69 #ifdef CONFIG_PPC_PMAC
70 unsigned long agp_special_page;
71 #endif
73 extern char _end[];
74 extern char etext[], _stext[];
75 extern char __init_begin, __init_end;
76 extern char __prep_begin, __prep_end;
77 extern char __chrp_begin, __chrp_end;
78 extern char __pmac_begin, __pmac_end;
79 extern char __openfirmware_begin, __openfirmware_end;
81 #ifdef CONFIG_HIGHMEM
82 pte_t *kmap_pte;
83 pgprot_t kmap_prot;
85 EXPORT_SYMBOL(kmap_prot);
86 EXPORT_SYMBOL(kmap_pte);
87 #endif
89 void MMU_init(void);
90 void set_phys_avail(unsigned long total_ram);
92 /* XXX should be in current.h -- paulus */
93 extern struct task_struct *current_set[NR_CPUS];
95 char *klimit = _end;
96 struct mem_pieces phys_avail;
98 extern char *sysmap;
99 extern unsigned long sysmap_size;
102 * this tells the system to map all of ram with the segregs
103 * (i.e. page tables) instead of the bats.
104 * -- Cort
106 int __map_without_bats;
107 int __map_without_ltlbs;
109 /* max amount of RAM to use */
110 unsigned long __max_memory;
111 /* max amount of low RAM to map in */
112 unsigned long __max_low_memory = MAX_LOW_MEM;
114 void show_mem(void)
116 int i,free = 0,total = 0,reserved = 0;
117 int shared = 0, cached = 0;
118 int highmem = 0;
120 printk("Mem-info:\n");
121 show_free_areas();
122 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
123 i = max_mapnr;
124 while (i-- > 0) {
125 total++;
126 if (PageHighMem(mem_map+i))
127 highmem++;
128 if (PageReserved(mem_map+i))
129 reserved++;
130 else if (PageSwapCache(mem_map+i))
131 cached++;
132 else if (!page_count(mem_map+i))
133 free++;
134 else
135 shared += page_count(mem_map+i) - 1;
137 printk("%d pages of RAM\n",total);
138 printk("%d pages of HIGHMEM\n", highmem);
139 printk("%d free pages\n",free);
140 printk("%d reserved pages\n",reserved);
141 printk("%d pages shared\n",shared);
142 printk("%d pages swap cached\n",cached);
145 /* Free up now-unused memory */
146 static void free_sec(unsigned long start, unsigned long end, const char *name)
148 unsigned long cnt = 0;
150 while (start < end) {
151 ClearPageReserved(virt_to_page(start));
152 set_page_count(virt_to_page(start), 1);
153 free_page(start);
154 cnt++;
155 start += PAGE_SIZE;
157 if (cnt) {
158 printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
159 totalram_pages += cnt;
163 void free_initmem(void)
165 #define FREESEC(TYPE) \
166 free_sec((unsigned long)(&__ ## TYPE ## _begin), \
167 (unsigned long)(&__ ## TYPE ## _end), \
168 #TYPE);
170 printk ("Freeing unused kernel memory:");
171 FREESEC(init);
172 if (_machine != _MACH_Pmac)
173 FREESEC(pmac);
174 if (_machine != _MACH_chrp)
175 FREESEC(chrp);
176 if (_machine != _MACH_prep)
177 FREESEC(prep);
178 if (!have_of)
179 FREESEC(openfirmware);
180 printk("\n");
181 #undef FREESEC
184 #ifdef CONFIG_BLK_DEV_INITRD
185 void free_initrd_mem(unsigned long start, unsigned long end)
187 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
189 for (; start < end; start += PAGE_SIZE) {
190 ClearPageReserved(virt_to_page(start));
191 set_page_count(virt_to_page(start), 1);
192 free_page(start);
193 totalram_pages++;
196 #endif
199 * Check for command-line options that affect what MMU_init will do.
201 void MMU_setup(void)
203 /* Check for nobats option (used in mapin_ram). */
204 if (strstr(cmd_line, "nobats")) {
205 __map_without_bats = 1;
208 if (strstr(cmd_line, "noltlbs")) {
209 __map_without_ltlbs = 1;
212 /* Look for mem= option on command line */
213 if (strstr(cmd_line, "mem=")) {
214 char *p, *q;
215 unsigned long maxmem = 0;
217 for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
218 q = p + 4;
219 if (p > cmd_line && p[-1] != ' ')
220 continue;
221 maxmem = simple_strtoul(q, &q, 0);
222 if (*q == 'k' || *q == 'K') {
223 maxmem <<= 10;
224 ++q;
225 } else if (*q == 'm' || *q == 'M') {
226 maxmem <<= 20;
227 ++q;
230 __max_memory = maxmem;
235 * MMU_init sets up the basic memory mappings for the kernel,
236 * including both RAM and possibly some I/O regions,
237 * and sets up the page tables and the MMU hardware ready to go.
239 void __init MMU_init(void)
241 if (ppc_md.progress)
242 ppc_md.progress("MMU:enter", 0x111);
244 /* parse args from command line */
245 MMU_setup();
248 * Figure out how much memory we have, how much
249 * is lowmem, and how much is highmem. If we were
250 * passed the total memory size from the bootloader,
251 * just use it.
253 if (boot_mem_size)
254 total_memory = boot_mem_size;
255 else
256 total_memory = ppc_md.find_end_of_memory();
258 if (__max_memory && total_memory > __max_memory)
259 total_memory = __max_memory;
260 total_lowmem = total_memory;
261 #ifdef CONFIG_FSL_BOOKE
262 /* Freescale Book-E parts expect lowmem to be mapped by fixed TLB
263 * entries, so we need to adjust lowmem to match the amount we can map
264 * in the fixed entries */
265 adjust_total_lowmem();
266 #endif /* CONFIG_FSL_BOOKE */
267 if (total_lowmem > __max_low_memory) {
268 total_lowmem = __max_low_memory;
269 #ifndef CONFIG_HIGHMEM
270 total_memory = total_lowmem;
271 #endif /* CONFIG_HIGHMEM */
273 set_phys_avail(total_lowmem);
275 /* Initialize the MMU hardware */
276 if (ppc_md.progress)
277 ppc_md.progress("MMU:hw init", 0x300);
278 MMU_init_hw();
280 /* Map in all of RAM starting at KERNELBASE */
281 if (ppc_md.progress)
282 ppc_md.progress("MMU:mapin", 0x301);
283 mapin_ram();
285 #ifdef CONFIG_HIGHMEM
286 ioremap_base = PKMAP_BASE;
287 #else
288 ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */
289 #endif /* CONFIG_HIGHMEM */
290 ioremap_bot = ioremap_base;
292 /* Map in I/O resources */
293 if (ppc_md.progress)
294 ppc_md.progress("MMU:setio", 0x302);
295 if (ppc_md.setup_io_mappings)
296 ppc_md.setup_io_mappings();
298 /* Initialize the context management stuff */
299 mmu_context_init();
301 if (ppc_md.progress)
302 ppc_md.progress("MMU:exit", 0x211);
304 #ifdef CONFIG_BOOTX_TEXT
305 /* By default, we are no longer mapped */
306 boot_text_mapped = 0;
307 /* Must be done last, or ppc_md.progress will die. */
308 map_boot_text();
309 #endif
312 /* This is only called until mem_init is done. */
313 void __init *early_get_page(void)
315 void *p;
317 if (init_bootmem_done) {
318 p = alloc_bootmem_pages(PAGE_SIZE);
319 } else {
320 p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
322 return p;
326 * Initialize the bootmem system and give it all the memory we
327 * have available.
329 void __init do_init_bootmem(void)
331 unsigned long start, size;
332 int i;
335 * Find an area to use for the bootmem bitmap.
336 * We look for the first area which is at least
337 * 128kB in length (128kB is enough for a bitmap
338 * for 4GB of memory, using 4kB pages), plus 1 page
339 * (in case the address isn't page-aligned).
341 start = 0;
342 size = 0;
343 for (i = 0; i < phys_avail.n_regions; ++i) {
344 unsigned long a = phys_avail.regions[i].address;
345 unsigned long s = phys_avail.regions[i].size;
346 if (s <= size)
347 continue;
348 start = a;
349 size = s;
350 if (s >= 33 * PAGE_SIZE)
351 break;
353 start = PAGE_ALIGN(start);
355 min_low_pfn = start >> PAGE_SHIFT;
356 max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
357 max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT;
358 boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
359 PPC_MEMSTART >> PAGE_SHIFT,
360 max_low_pfn);
362 /* remove the bootmem bitmap from the available memory */
363 mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
365 /* add everything in phys_avail into the bootmem map */
366 for (i = 0; i < phys_avail.n_regions; ++i)
367 free_bootmem(phys_avail.regions[i].address,
368 phys_avail.regions[i].size);
370 init_bootmem_done = 1;
374 * paging_init() sets up the page tables - in fact we've already done this.
376 void __init paging_init(void)
378 unsigned long zones_size[MAX_NR_ZONES], i;
380 #ifdef CONFIG_HIGHMEM
381 map_page(PKMAP_BASE, 0, 0); /* XXX gross */
382 pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k
383 (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
384 map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
385 kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k
386 (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
387 kmap_prot = PAGE_KERNEL;
388 #endif /* CONFIG_HIGHMEM */
391 * All pages are DMA-able so we put them all in the DMA zone.
393 zones_size[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
394 for (i = 1; i < MAX_NR_ZONES; i++)
395 zones_size[i] = 0;
397 #ifdef CONFIG_HIGHMEM
398 zones_size[ZONE_HIGHMEM] = (total_memory - total_lowmem) >> PAGE_SHIFT;
399 #endif /* CONFIG_HIGHMEM */
401 free_area_init(zones_size);
404 void __init mem_init(void)
406 unsigned long addr;
407 int codepages = 0;
408 int datapages = 0;
409 int initpages = 0;
410 #ifdef CONFIG_HIGHMEM
411 unsigned long highmem_mapnr;
413 highmem_mapnr = total_lowmem >> PAGE_SHIFT;
414 highmem_start_page = mem_map + highmem_mapnr;
415 #endif /* CONFIG_HIGHMEM */
416 max_mapnr = total_memory >> PAGE_SHIFT;
418 high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
419 num_physpages = max_mapnr; /* RAM is assumed contiguous */
421 totalram_pages += free_all_bootmem();
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));
430 #endif /* CONFIG_BLK_DEV_INITRD */
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));
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++;
463 #ifdef CONFIG_HIGHMEM
465 unsigned long pfn;
467 for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
468 struct page *page = mem_map + pfn;
470 ClearPageReserved(page);
471 set_bit(PG_highmem, &page->flags);
472 set_page_count(page, 1);
473 __free_page(page);
474 totalhigh_pages++;
476 totalram_pages += totalhigh_pages;
478 #endif /* CONFIG_HIGHMEM */
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
493 /* Make sure all our pagetable pages have page->mapping
494 and page->index set correctly. */
495 for (addr = KERNELBASE; addr != 0; addr += PGDIR_SIZE) {
496 struct page *pg;
497 pmd_t *pmd = pmd_offset(pgd_offset_k(addr), addr);
498 if (pmd_present(*pmd)) {
499 pg = pmd_page(*pmd);
500 pg->mapping = (void *) &init_mm;
501 pg->index = addr;
505 mem_init_done = 1;
509 * Set phys_avail to the amount of physical memory,
510 * less the kernel text/data/bss.
512 void __init
513 set_phys_avail(unsigned long total_memory)
515 unsigned long kstart, ksize;
518 * Initially, available physical memory is equivalent to all
519 * physical memory.
522 phys_avail.regions[0].address = PPC_MEMSTART;
523 phys_avail.regions[0].size = total_memory;
524 phys_avail.n_regions = 1;
527 * Map out the kernel text/data/bss from the available physical
528 * memory.
531 kstart = __pa(_stext); /* should be 0 */
532 ksize = PAGE_ALIGN(klimit - _stext);
534 mem_pieces_remove(&phys_avail, kstart, ksize, 0);
535 mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
537 #if defined(CONFIG_BLK_DEV_INITRD)
538 /* Remove the init RAM disk from the available memory. */
539 if (initrd_start) {
540 mem_pieces_remove(&phys_avail, __pa(initrd_start),
541 initrd_end - initrd_start, 1);
543 #endif /* CONFIG_BLK_DEV_INITRD */
544 #ifdef CONFIG_PPC_OF
545 /* remove the RTAS pages from the available memory */
546 if (rtas_data)
547 mem_pieces_remove(&phys_avail, rtas_data, rtas_size, 1);
548 #endif
549 /* remove the sysmap pages from the available memory */
550 if (sysmap)
551 mem_pieces_remove(&phys_avail, __pa(sysmap), sysmap_size, 1);
552 #ifdef CONFIG_PPC_PMAC
553 /* Because of some uninorth weirdness, we need a page of
554 * memory as high as possible (it must be outside of the
555 * bus address seen as the AGP aperture). It will be used
556 * by the r128 DRM driver
558 * FIXME: We need to make sure that page doesn't overlap any of the\
559 * above. This could be done by improving mem_pieces_find to be able
560 * to do a backward search from the end of the list.
562 if (_machine == _MACH_Pmac && find_devices("uni-north-agp")) {
563 agp_special_page = (total_memory - PAGE_SIZE);
564 mem_pieces_remove(&phys_avail, agp_special_page, PAGE_SIZE, 0);
565 agp_special_page = (unsigned long)__va(agp_special_page);
567 #endif /* CONFIG_PPC_PMAC */
570 /* Mark some memory as reserved by removing it from phys_avail. */
571 void __init reserve_phys_mem(unsigned long start, unsigned long size)
573 mem_pieces_remove(&phys_avail, start, size, 1);
577 * This is called when a page has been modified by the kernel.
578 * It just marks the page as not i-cache clean. We do the i-cache
579 * flush later when the page is given to a user process, if necessary.
581 void flush_dcache_page(struct page *page)
583 clear_bit(PG_arch_1, &page->flags);
586 void flush_dcache_icache_page(struct page *page)
588 #ifdef CONFIG_BOOKE
589 __flush_dcache_icache(kmap(page));
590 kunmap(page);
591 #else
592 __flush_dcache_icache_phys(page_to_pfn(page) << PAGE_SHIFT);
593 #endif
596 void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
598 clear_page(page);
599 clear_bit(PG_arch_1, &pg->flags);
602 void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
603 struct page *pg)
605 copy_page(vto, vfrom);
606 clear_bit(PG_arch_1, &pg->flags);
609 void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
610 unsigned long addr, int len)
612 unsigned long maddr;
614 maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
615 flush_icache_range(maddr, maddr + len);
616 kunmap(page);
620 * This is called at the end of handling a user page fault, when the
621 * fault has been handled by updating a PTE in the linux page tables.
622 * We use it to preload an HPTE into the hash table corresponding to
623 * the updated linux PTE.
625 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
626 pte_t pte)
628 /* handle i-cache coherency */
629 unsigned long pfn = pte_pfn(pte);
631 if (pfn_valid(pfn)) {
632 struct page *page = pfn_to_page(pfn);
633 if (!PageReserved(page)
634 && !test_bit(PG_arch_1, &page->flags)) {
635 if (vma->vm_mm == current->active_mm)
636 __flush_dcache_icache((void *) address);
637 else
638 flush_dcache_icache_page(page);
639 set_bit(PG_arch_1, &page->flags);
643 #ifdef CONFIG_PPC_STD_MMU
644 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
645 if (Hash != 0 && pte_young(pte)) {
646 struct mm_struct *mm;
647 pmd_t *pmd;
649 mm = (address < TASK_SIZE)? vma->vm_mm: &init_mm;
650 pmd = pmd_offset(pgd_offset(mm, address), address);
651 if (!pmd_none(*pmd))
652 add_hash_page(mm->context, address, pmd_val(*pmd));
654 #endif