[PATCH] ppc32: Add usb support to IBM stb04xxx platforms
[linux-2.6/linux-loongson.git] / arch / ppc / mm / init.c
blob33ada72c73302900a8f34a1c69a9763eb59dd6be
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>
34 #include <linux/pagemap.h>
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>
48 #include "mem_pieces.h"
49 #include "mmu_decl.h"
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
59 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
61 unsigned long total_memory;
62 unsigned long total_lowmem;
64 unsigned long ppc_memstart;
65 unsigned long ppc_memoffset = PAGE_OFFSET;
67 int mem_init_done;
68 int init_bootmem_done;
69 int boot_mapsize;
70 #ifdef CONFIG_PPC_PMAC
71 unsigned long agp_special_page;
72 #endif
74 extern char _end[];
75 extern char etext[], _stext[];
76 extern char __init_begin, __init_end;
77 extern char __prep_begin, __prep_end;
78 extern char __chrp_begin, __chrp_end;
79 extern char __pmac_begin, __pmac_end;
80 extern char __openfirmware_begin, __openfirmware_end;
82 #ifdef CONFIG_HIGHMEM
83 pte_t *kmap_pte;
84 pgprot_t kmap_prot;
86 EXPORT_SYMBOL(kmap_prot);
87 EXPORT_SYMBOL(kmap_pte);
88 #endif
90 void MMU_init(void);
91 void set_phys_avail(unsigned long total_ram);
93 /* XXX should be in current.h -- paulus */
94 extern struct task_struct *current_set[NR_CPUS];
96 char *klimit = _end;
97 struct mem_pieces phys_avail;
100 * this tells the system to map all of ram with the segregs
101 * (i.e. page tables) instead of the bats.
102 * -- Cort
104 int __map_without_bats;
105 int __map_without_ltlbs;
107 /* max amount of RAM to use */
108 unsigned long __max_memory;
109 /* max amount of low RAM to map in */
110 unsigned long __max_low_memory = MAX_LOW_MEM;
112 void show_mem(void)
114 int i,free = 0,total = 0,reserved = 0;
115 int shared = 0, cached = 0;
116 int highmem = 0;
118 printk("Mem-info:\n");
119 show_free_areas();
120 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
121 i = max_mapnr;
122 while (i-- > 0) {
123 total++;
124 if (PageHighMem(mem_map+i))
125 highmem++;
126 if (PageReserved(mem_map+i))
127 reserved++;
128 else if (PageSwapCache(mem_map+i))
129 cached++;
130 else if (!page_count(mem_map+i))
131 free++;
132 else
133 shared += page_count(mem_map+i) - 1;
135 printk("%d pages of RAM\n",total);
136 printk("%d pages of HIGHMEM\n", highmem);
137 printk("%d free pages\n",free);
138 printk("%d reserved pages\n",reserved);
139 printk("%d pages shared\n",shared);
140 printk("%d pages swap cached\n",cached);
143 /* Free up now-unused memory */
144 static void free_sec(unsigned long start, unsigned long end, const char *name)
146 unsigned long cnt = 0;
148 while (start < end) {
149 ClearPageReserved(virt_to_page(start));
150 set_page_count(virt_to_page(start), 1);
151 free_page(start);
152 cnt++;
153 start += PAGE_SIZE;
155 if (cnt) {
156 printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
157 totalram_pages += cnt;
161 void free_initmem(void)
163 #define FREESEC(TYPE) \
164 free_sec((unsigned long)(&__ ## TYPE ## _begin), \
165 (unsigned long)(&__ ## TYPE ## _end), \
166 #TYPE);
168 printk ("Freeing unused kernel memory:");
169 FREESEC(init);
170 if (_machine != _MACH_Pmac)
171 FREESEC(pmac);
172 if (_machine != _MACH_chrp)
173 FREESEC(chrp);
174 if (_machine != _MACH_prep)
175 FREESEC(prep);
176 if (!have_of)
177 FREESEC(openfirmware);
178 printk("\n");
179 ppc_md.progress = NULL;
180 #undef FREESEC
183 #ifdef CONFIG_BLK_DEV_INITRD
184 void free_initrd_mem(unsigned long start, unsigned long end)
186 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
188 for (; start < end; start += PAGE_SIZE) {
189 ClearPageReserved(virt_to_page(start));
190 set_page_count(virt_to_page(start), 1);
191 free_page(start);
192 totalram_pages++;
195 #endif
198 * Check for command-line options that affect what MMU_init will do.
200 void MMU_setup(void)
202 /* Check for nobats option (used in mapin_ram). */
203 if (strstr(cmd_line, "nobats")) {
204 __map_without_bats = 1;
207 if (strstr(cmd_line, "noltlbs")) {
208 __map_without_ltlbs = 1;
211 /* Look for mem= option on command line */
212 if (strstr(cmd_line, "mem=")) {
213 char *p, *q;
214 unsigned long maxmem = 0;
216 for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
217 q = p + 4;
218 if (p > cmd_line && p[-1] != ' ')
219 continue;
220 maxmem = simple_strtoul(q, &q, 0);
221 if (*q == 'k' || *q == 'K') {
222 maxmem <<= 10;
223 ++q;
224 } else if (*q == 'm' || *q == 'M') {
225 maxmem <<= 20;
226 ++q;
229 __max_memory = maxmem;
234 * MMU_init sets up the basic memory mappings for the kernel,
235 * including both RAM and possibly some I/O regions,
236 * and sets up the page tables and the MMU hardware ready to go.
238 void __init MMU_init(void)
240 if (ppc_md.progress)
241 ppc_md.progress("MMU:enter", 0x111);
243 /* parse args from command line */
244 MMU_setup();
247 * Figure out how much memory we have, how much
248 * is lowmem, and how much is highmem. If we were
249 * passed the total memory size from the bootloader,
250 * just use it.
252 if (boot_mem_size)
253 total_memory = boot_mem_size;
254 else
255 total_memory = ppc_md.find_end_of_memory();
257 if (__max_memory && total_memory > __max_memory)
258 total_memory = __max_memory;
259 total_lowmem = total_memory;
260 #ifdef CONFIG_FSL_BOOKE
261 /* Freescale Book-E parts expect lowmem to be mapped by fixed TLB
262 * entries, so we need to adjust lowmem to match the amount we can map
263 * in the fixed entries */
264 adjust_total_lowmem();
265 #endif /* CONFIG_FSL_BOOKE */
266 if (total_lowmem > __max_low_memory) {
267 total_lowmem = __max_low_memory;
268 #ifndef CONFIG_HIGHMEM
269 total_memory = total_lowmem;
270 #endif /* CONFIG_HIGHMEM */
272 set_phys_avail(total_lowmem);
274 /* Initialize the MMU hardware */
275 if (ppc_md.progress)
276 ppc_md.progress("MMU:hw init", 0x300);
277 MMU_init_hw();
279 /* Map in all of RAM starting at KERNELBASE */
280 if (ppc_md.progress)
281 ppc_md.progress("MMU:mapin", 0x301);
282 mapin_ram();
284 #ifdef CONFIG_HIGHMEM
285 ioremap_base = PKMAP_BASE;
286 #else
287 ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */
288 #endif /* CONFIG_HIGHMEM */
289 ioremap_bot = ioremap_base;
291 /* Map in I/O resources */
292 if (ppc_md.progress)
293 ppc_md.progress("MMU:setio", 0x302);
294 if (ppc_md.setup_io_mappings)
295 ppc_md.setup_io_mappings();
297 /* Initialize the context management stuff */
298 mmu_context_init();
300 if (ppc_md.progress)
301 ppc_md.progress("MMU:exit", 0x211);
303 #ifdef CONFIG_BOOTX_TEXT
304 /* By default, we are no longer mapped */
305 boot_text_mapped = 0;
306 /* Must be done last, or ppc_md.progress will die. */
307 map_boot_text();
308 #endif
311 /* This is only called until mem_init is done. */
312 void __init *early_get_page(void)
314 void *p;
316 if (init_bootmem_done) {
317 p = alloc_bootmem_pages(PAGE_SIZE);
318 } else {
319 p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
321 return p;
325 * Initialize the bootmem system and give it all the memory we
326 * have available.
328 void __init do_init_bootmem(void)
330 unsigned long start, size;
331 int i;
334 * Find an area to use for the bootmem bitmap.
335 * We look for the first area which is at least
336 * 128kB in length (128kB is enough for a bitmap
337 * for 4GB of memory, using 4kB pages), plus 1 page
338 * (in case the address isn't page-aligned).
340 start = 0;
341 size = 0;
342 for (i = 0; i < phys_avail.n_regions; ++i) {
343 unsigned long a = phys_avail.regions[i].address;
344 unsigned long s = phys_avail.regions[i].size;
345 if (s <= size)
346 continue;
347 start = a;
348 size = s;
349 if (s >= 33 * PAGE_SIZE)
350 break;
352 start = PAGE_ALIGN(start);
354 min_low_pfn = start >> PAGE_SHIFT;
355 max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
356 max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT;
357 boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
358 PPC_MEMSTART >> PAGE_SHIFT,
359 max_low_pfn);
361 /* remove the bootmem bitmap from the available memory */
362 mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
364 /* add everything in phys_avail into the bootmem map */
365 for (i = 0; i < phys_avail.n_regions; ++i)
366 free_bootmem(phys_avail.regions[i].address,
367 phys_avail.regions[i].size);
369 init_bootmem_done = 1;
373 * paging_init() sets up the page tables - in fact we've already done this.
375 void __init paging_init(void)
377 unsigned long zones_size[MAX_NR_ZONES], i;
379 #ifdef CONFIG_HIGHMEM
380 map_page(PKMAP_BASE, 0, 0); /* XXX gross */
381 pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k
382 (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
383 map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
384 kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k
385 (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
386 kmap_prot = PAGE_KERNEL;
387 #endif /* CONFIG_HIGHMEM */
390 * All pages are DMA-able so we put them all in the DMA zone.
392 zones_size[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
393 for (i = 1; i < MAX_NR_ZONES; i++)
394 zones_size[i] = 0;
396 #ifdef CONFIG_HIGHMEM
397 zones_size[ZONE_HIGHMEM] = (total_memory - total_lowmem) >> PAGE_SHIFT;
398 #endif /* CONFIG_HIGHMEM */
400 free_area_init(zones_size);
403 void __init mem_init(void)
405 unsigned long addr;
406 int codepages = 0;
407 int datapages = 0;
408 int initpages = 0;
409 #ifdef CONFIG_HIGHMEM
410 unsigned long highmem_mapnr;
412 highmem_mapnr = total_lowmem >> PAGE_SHIFT;
413 #endif /* CONFIG_HIGHMEM */
414 max_mapnr = total_memory >> PAGE_SHIFT;
416 high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
417 num_physpages = max_mapnr; /* RAM is assumed contiguous */
419 totalram_pages += free_all_bootmem();
421 #ifdef CONFIG_BLK_DEV_INITRD
422 /* if we are booted from BootX with an initial ramdisk,
423 make sure the ramdisk pages aren't reserved. */
424 if (initrd_start) {
425 for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE)
426 ClearPageReserved(virt_to_page(addr));
428 #endif /* CONFIG_BLK_DEV_INITRD */
430 #ifdef CONFIG_PPC_OF
431 /* mark the RTAS pages as reserved */
432 if ( rtas_data )
433 for (addr = (ulong)__va(rtas_data);
434 addr < PAGE_ALIGN((ulong)__va(rtas_data)+rtas_size) ;
435 addr += PAGE_SIZE)
436 SetPageReserved(virt_to_page(addr));
437 #endif
438 #ifdef CONFIG_PPC_PMAC
439 if (agp_special_page)
440 SetPageReserved(virt_to_page(agp_special_page));
441 #endif
442 for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory;
443 addr += PAGE_SIZE) {
444 if (!PageReserved(virt_to_page(addr)))
445 continue;
446 if (addr < (ulong) etext)
447 codepages++;
448 else if (addr >= (unsigned long)&__init_begin
449 && addr < (unsigned long)&__init_end)
450 initpages++;
451 else if (addr < (ulong) klimit)
452 datapages++;
455 #ifdef CONFIG_HIGHMEM
457 unsigned long pfn;
459 for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
460 struct page *page = mem_map + pfn;
462 ClearPageReserved(page);
463 set_page_count(page, 1);
464 __free_page(page);
465 totalhigh_pages++;
467 totalram_pages += totalhigh_pages;
469 #endif /* CONFIG_HIGHMEM */
471 printk("Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
472 (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10),
473 codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10),
474 initpages<< (PAGE_SHIFT-10),
475 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
477 #ifdef CONFIG_PPC_PMAC
478 if (agp_special_page)
479 printk(KERN_INFO "AGP special page: 0x%08lx\n", agp_special_page);
480 #endif
482 mem_init_done = 1;
486 * Set phys_avail to the amount of physical memory,
487 * less the kernel text/data/bss.
489 void __init
490 set_phys_avail(unsigned long total_memory)
492 unsigned long kstart, ksize;
495 * Initially, available physical memory is equivalent to all
496 * physical memory.
499 phys_avail.regions[0].address = PPC_MEMSTART;
500 phys_avail.regions[0].size = total_memory;
501 phys_avail.n_regions = 1;
504 * Map out the kernel text/data/bss from the available physical
505 * memory.
508 kstart = __pa(_stext); /* should be 0 */
509 ksize = PAGE_ALIGN(klimit - _stext);
511 mem_pieces_remove(&phys_avail, kstart, ksize, 0);
512 mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
514 #if defined(CONFIG_BLK_DEV_INITRD)
515 /* Remove the init RAM disk from the available memory. */
516 if (initrd_start) {
517 mem_pieces_remove(&phys_avail, __pa(initrd_start),
518 initrd_end - initrd_start, 1);
520 #endif /* CONFIG_BLK_DEV_INITRD */
521 #ifdef CONFIG_PPC_OF
522 /* remove the RTAS pages from the available memory */
523 if (rtas_data)
524 mem_pieces_remove(&phys_avail, rtas_data, rtas_size, 1);
525 #endif
526 #ifdef CONFIG_PPC_PMAC
527 /* Because of some uninorth weirdness, we need a page of
528 * memory as high as possible (it must be outside of the
529 * bus address seen as the AGP aperture). It will be used
530 * by the r128 DRM driver
532 * FIXME: We need to make sure that page doesn't overlap any of the\
533 * above. This could be done by improving mem_pieces_find to be able
534 * to do a backward search from the end of the list.
536 if (_machine == _MACH_Pmac && find_devices("uni-north-agp")) {
537 agp_special_page = (total_memory - PAGE_SIZE);
538 mem_pieces_remove(&phys_avail, agp_special_page, PAGE_SIZE, 0);
539 agp_special_page = (unsigned long)__va(agp_special_page);
541 #endif /* CONFIG_PPC_PMAC */
544 /* Mark some memory as reserved by removing it from phys_avail. */
545 void __init reserve_phys_mem(unsigned long start, unsigned long size)
547 mem_pieces_remove(&phys_avail, start, size, 1);
551 * This is called when a page has been modified by the kernel.
552 * It just marks the page as not i-cache clean. We do the i-cache
553 * flush later when the page is given to a user process, if necessary.
555 void flush_dcache_page(struct page *page)
557 clear_bit(PG_arch_1, &page->flags);
560 void flush_dcache_icache_page(struct page *page)
562 #ifdef CONFIG_BOOKE
563 __flush_dcache_icache(kmap(page));
564 kunmap(page);
565 #elif CONFIG_8xx
566 /* On 8xx there is no need to kmap since highmem is not supported */
567 __flush_dcache_icache(page_address(page));
568 #else
569 __flush_dcache_icache_phys(page_to_pfn(page) << PAGE_SHIFT);
570 #endif
573 void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
575 clear_page(page);
576 clear_bit(PG_arch_1, &pg->flags);
579 void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
580 struct page *pg)
582 copy_page(vto, vfrom);
583 clear_bit(PG_arch_1, &pg->flags);
586 void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
587 unsigned long addr, int len)
589 unsigned long maddr;
591 maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
592 flush_icache_range(maddr, maddr + len);
593 kunmap(page);
597 * This is called at the end of handling a user page fault, when the
598 * fault has been handled by updating a PTE in the linux page tables.
599 * We use it to preload an HPTE into the hash table corresponding to
600 * the updated linux PTE.
602 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
603 pte_t pte)
605 /* handle i-cache coherency */
606 unsigned long pfn = pte_pfn(pte);
608 if (pfn_valid(pfn)) {
609 struct page *page = pfn_to_page(pfn);
610 if (!PageReserved(page)
611 && !test_bit(PG_arch_1, &page->flags)) {
612 if (vma->vm_mm == current->active_mm) {
613 #ifdef CONFIG_8xx
614 /* On 8xx, cache control instructions (particularly
615 * "dcbst" from flush_dcache_icache) fault as write
616 * operation if there is an unpopulated TLB entry
617 * for the address in question. To workaround that,
618 * we invalidate the TLB here, thus avoiding dcbst
619 * misbehaviour.
621 _tlbie(address);
622 #endif
623 __flush_dcache_icache((void *) address);
624 } else
625 flush_dcache_icache_page(page);
626 set_bit(PG_arch_1, &page->flags);
630 #ifdef CONFIG_PPC_STD_MMU
631 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
632 if (Hash != 0 && pte_young(pte)) {
633 struct mm_struct *mm;
634 pmd_t *pmd;
636 mm = (address < TASK_SIZE)? vma->vm_mm: &init_mm;
637 pmd = pmd_offset(pgd_offset(mm, address), address);
638 if (!pmd_none(*pmd))
639 add_hash_page(mm->context, address, pmd_val(*pmd));
641 #endif
645 * This is called by /dev/mem to know if a given address has to
646 * be mapped non-cacheable or not
648 int page_is_ram(unsigned long pfn)
650 unsigned long paddr = (pfn << PAGE_SHIFT);
652 return paddr < __pa(high_memory);
655 pgprot_t phys_mem_access_prot(struct file *file, unsigned long addr,
656 unsigned long size, pgprot_t vma_prot)
658 if (ppc_md.phys_mem_access_prot)
659 return ppc_md.phys_mem_access_prot(file, addr, size, vma_prot);
661 if (!page_is_ram(addr >> PAGE_SHIFT))
662 vma_prot = __pgprot(pgprot_val(vma_prot)
663 | _PAGE_GUARDED | _PAGE_NO_CACHE);
664 return vma_prot;
666 EXPORT_SYMBOL(phys_mem_access_prot);