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).
10 * Derived from "arch/i386/mm/init.c"
11 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
13 * Dave Engebretsen <engebret@us.ibm.com>
14 * Rework for PPC64 port.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
23 #include <linux/config.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/mman.h>
32 #include <linux/swap.h>
33 #include <linux/stddef.h>
34 #include <linux/vmalloc.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/bootmem.h>
38 #include <linux/highmem.h>
39 #include <linux/idr.h>
41 #include <asm/pgalloc.h>
43 #include <asm/abs_addr.h>
48 #include <asm/mmu_context.h>
49 #include <asm/pgtable.h>
51 #include <asm/uaccess.h>
53 #include <asm/machdep.h>
57 #include <asm/processor.h>
58 #include <asm/mmzone.h>
59 #include <asm/cputable.h>
60 #include <asm/ppcdebug.h>
61 #include <asm/sections.h>
62 #include <asm/system.h>
63 #include <asm/iommu.h>
64 #include <asm/abs_addr.h>
67 unsigned long ioremap_bot = IMALLOC_BASE;
68 static unsigned long phbs_io_bot = PHBS_IO_BASE;
70 extern pgd_t swapper_pg_dir[];
71 extern struct task_struct *current_set[NR_CPUS];
73 extern pgd_t ioremap_dir[];
74 pgd_t * ioremap_pgd = (pgd_t *)&ioremap_dir;
76 unsigned long klimit = (unsigned long)_end;
78 unsigned long _SDR1=0;
81 /* max amount of RAM to use */
82 unsigned long __max_memory;
84 /* info on what we think the IO hole is */
85 unsigned long io_hole_start;
86 unsigned long io_hole_size;
90 unsigned long total = 0, reserved = 0;
91 unsigned long shared = 0, cached = 0;
96 printk("Mem-info:\n");
98 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
99 for_each_pgdat(pgdat) {
100 for (i = 0; i < pgdat->node_spanned_pages; i++) {
101 page = pgdat->node_mem_map + i;
103 if (PageReserved(page))
105 else if (PageSwapCache(page))
107 else if (page_count(page))
108 shared += page_count(page) - 1;
111 printk("%ld pages of RAM\n", total);
112 printk("%ld reserved pages\n", reserved);
113 printk("%ld pages shared\n", shared);
114 printk("%ld pages swap cached\n", cached);
117 #ifdef CONFIG_PPC_ISERIES
119 void __iomem *ioremap(unsigned long addr, unsigned long size)
121 return (void __iomem *)addr;
124 extern void __iomem *__ioremap(unsigned long addr, unsigned long size,
127 return (void __iomem *)addr;
130 void iounmap(volatile void __iomem *addr)
138 * map_io_page currently only called by __ioremap
139 * map_io_page adds an entry to the ioremap page table
140 * and adds an entry to the HPT, possibly bolting it
142 static void map_io_page(unsigned long ea, unsigned long pa, int flags)
150 spin_lock(&ioremap_mm.page_table_lock);
151 pgdp = pgd_offset_i(ea);
152 pmdp = pmd_alloc(&ioremap_mm, pgdp, ea);
153 ptep = pte_alloc_kernel(&ioremap_mm, pmdp, ea);
155 pa = abs_to_phys(pa);
156 set_pte(ptep, pfn_pte(pa >> PAGE_SHIFT, __pgprot(flags)));
157 spin_unlock(&ioremap_mm.page_table_lock);
159 unsigned long va, vpn, hash, hpteg;
162 * If the mm subsystem is not fully up, we cannot create a
163 * linux page table entry for this mapping. Simply bolt an
164 * entry in the hardware page table.
166 vsid = get_kernel_vsid(ea);
167 va = (vsid << 28) | (ea & 0xFFFFFFF);
168 vpn = va >> PAGE_SHIFT;
170 hash = hpt_hash(vpn, 0);
172 hpteg = ((hash & htab_data.htab_hash_mask)*HPTES_PER_GROUP);
174 /* Panic if a pte grpup is full */
175 if (ppc_md.hpte_insert(hpteg, va, pa >> PAGE_SHIFT, 0,
176 _PAGE_NO_CACHE|_PAGE_GUARDED|PP_RWXX,
178 panic("map_io_page: could not insert mapping");
184 static void __iomem * __ioremap_com(unsigned long addr, unsigned long pa,
185 unsigned long ea, unsigned long size,
190 if ((flags & _PAGE_PRESENT) == 0)
191 flags |= pgprot_val(PAGE_KERNEL);
192 if (flags & (_PAGE_NO_CACHE | _PAGE_WRITETHRU))
193 flags |= _PAGE_GUARDED;
195 for (i = 0; i < size; i += PAGE_SIZE) {
196 map_io_page(ea+i, pa+i, flags);
199 return (void __iomem *) (ea + (addr & ~PAGE_MASK));
204 ioremap(unsigned long addr, unsigned long size)
206 return __ioremap(addr, size, _PAGE_NO_CACHE);
210 __ioremap(unsigned long addr, unsigned long size, unsigned long flags)
212 unsigned long pa, ea;
215 * Choose an address to map it to.
216 * Once the imalloc system is running, we use it.
217 * Before that, we map using addresses going
218 * up from ioremap_bot. imalloc will use
219 * the addresses from ioremap_bot through
220 * IMALLOC_END (0xE000001fffffffff)
223 pa = addr & PAGE_MASK;
224 size = PAGE_ALIGN(addr + size) - pa;
230 struct vm_struct *area;
231 area = im_get_free_area(size);
234 ea = (unsigned long)(area->addr);
240 return __ioremap_com(addr, pa, ea, size, flags);
243 #define IS_PAGE_ALIGNED(_val) ((_val) == ((_val) & PAGE_MASK))
245 int __ioremap_explicit(unsigned long pa, unsigned long ea,
246 unsigned long size, unsigned long flags)
248 struct vm_struct *area;
250 /* For now, require page-aligned values for pa, ea, and size */
251 if (!IS_PAGE_ALIGNED(pa) || !IS_PAGE_ALIGNED(ea) ||
252 !IS_PAGE_ALIGNED(size)) {
253 printk(KERN_ERR "unaligned value in %s\n", __FUNCTION__);
257 if (!mem_init_done) {
258 /* Two things to consider in this case:
259 * 1) No records will be kept (imalloc, etc) that the region
261 * 2) It won't be easy to iounmap() the region later (because
266 area = im_get_area(ea, size, IM_REGION_UNUSED|IM_REGION_SUBSET);
268 printk(KERN_ERR "could not obtain imalloc area for ea 0x%lx\n", ea);
271 if (ea != (unsigned long) area->addr) {
272 printk(KERN_ERR "unexpected addr return from im_get_area\n");
277 if (__ioremap_com(pa, pa, ea, size, flags) != (void *) ea) {
278 printk(KERN_ERR "__ioremap_com() returned unexpected addr\n");
285 static void unmap_im_area_pte(pmd_t *pmd, unsigned long address,
299 pte = pte_offset_kernel(pmd, address);
300 address &= ~PMD_MASK;
301 end = address + size;
307 page = ptep_get_and_clear(pte);
308 address += PAGE_SIZE;
312 if (pte_present(page))
314 printk(KERN_CRIT "Whee.. Swapped out page in kernel page table\n");
315 } while (address < end);
318 static void unmap_im_area_pmd(pgd_t *dir, unsigned long address,
332 pmd = pmd_offset(dir, address);
333 address &= ~PGDIR_MASK;
334 end = address + size;
335 if (end > PGDIR_SIZE)
339 unmap_im_area_pte(pmd, address, end - address);
340 address = (address + PMD_SIZE) & PMD_MASK;
342 } while (address < end);
346 * Unmap an IO region and remove it from imalloc'd list.
347 * Access to IO memory should be serialized by driver.
348 * This code is modeled after vmalloc code - unmap_vm_area()
350 * XXX what about calls before mem_init_done (ie python_countermeasures())
352 void iounmap(volatile void __iomem *token)
354 unsigned long address, start, end, size;
355 struct mm_struct *mm;
359 if (!mem_init_done) {
363 addr = (void *) ((unsigned long __force) token & PAGE_MASK);
365 if ((size = im_free(addr)) == 0) {
369 address = (unsigned long)addr;
371 end = address + size;
374 spin_lock(&mm->page_table_lock);
376 dir = pgd_offset_i(address);
377 flush_cache_vunmap(address, end);
379 unmap_im_area_pmd(dir, address, end - address);
380 address = (address + PGDIR_SIZE) & PGDIR_MASK;
382 } while (address && (address < end));
383 flush_tlb_kernel_range(start, end);
385 spin_unlock(&mm->page_table_lock);
389 static int iounmap_subset_regions(unsigned long addr, unsigned long size)
391 struct vm_struct *area;
393 /* Check whether subsets of this region exist */
394 area = im_get_area(addr, size, IM_REGION_SUPERSET);
399 iounmap((void __iomem *) area->addr);
400 area = im_get_area(addr, size,
407 int iounmap_explicit(volatile void __iomem *start, unsigned long size)
409 struct vm_struct *area;
413 addr = (unsigned long __force) start & PAGE_MASK;
415 /* Verify that the region either exists or is a subset of an existing
416 * region. In the latter case, split the parent region to create
419 area = im_get_area(addr, size,
420 IM_REGION_EXISTS | IM_REGION_SUBSET);
422 /* Determine whether subset regions exist. If so, unmap */
423 rc = iounmap_subset_regions(addr, size);
426 "%s() cannot unmap nonexistent range 0x%lx\n",
431 iounmap((void __iomem *) area->addr);
434 * FIXME! This can't be right:
436 * Maybe it should be "iounmap(area);"
443 void free_initmem(void)
447 addr = (unsigned long)__init_begin;
448 for (; addr < (unsigned long)__init_end; addr += PAGE_SIZE) {
449 ClearPageReserved(virt_to_page(addr));
450 set_page_count(virt_to_page(addr), 1);
454 printk ("Freeing unused kernel memory: %luk freed\n",
455 ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10);
458 #ifdef CONFIG_BLK_DEV_INITRD
459 void free_initrd_mem(unsigned long start, unsigned long end)
462 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
463 for (; start < end; start += PAGE_SIZE) {
464 ClearPageReserved(virt_to_page(start));
465 set_page_count(virt_to_page(start), 1);
472 static spinlock_t mmu_context_lock = SPIN_LOCK_UNLOCKED;
473 static DEFINE_IDR(mmu_context_idr);
475 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
481 if (!idr_pre_get(&mmu_context_idr, GFP_KERNEL))
484 spin_lock(&mmu_context_lock);
485 err = idr_get_new(&mmu_context_idr, NULL, &index);
486 spin_unlock(&mmu_context_lock);
493 if (index > MAX_CONTEXT) {
494 idr_remove(&mmu_context_idr, index);
498 mm->context.id = index;
503 void destroy_context(struct mm_struct *mm)
505 spin_lock(&mmu_context_lock);
506 idr_remove(&mmu_context_idr, mm->context.id);
507 spin_unlock(&mmu_context_lock);
509 mm->context.id = NO_CONTEXT;
512 static int __init mmu_context_init(void)
516 /* Reserve the first (invalid) context*/
517 idr_pre_get(&mmu_context_idr, GFP_KERNEL);
518 idr_get_new(&mmu_context_idr, NULL, &index);
523 arch_initcall(mmu_context_init);
526 * Do very early mm setup.
528 void __init mm_init_ppc64(void)
530 #ifndef CONFIG_PPC_ISERIES
534 ppc64_boot_msg(0x100, "MM Init");
536 /* This is the story of the IO hole... please, keep seated,
537 * unfortunately, we are out of oxygen masks at the moment.
538 * So we need some rough way to tell where your big IO hole
539 * is. On pmac, it's between 2G and 4G, on POWER3, it's around
540 * that area as well, on POWER4 we don't have one, etc...
541 * We need that as a "hint" when sizing the TCE table on POWER3
542 * So far, the simplest way that seem work well enough for us it
543 * to just assume that the first discontinuity in our physical
544 * RAM layout is the IO hole. That may not be correct in the future
545 * (and isn't on iSeries but then we don't care ;)
548 #ifndef CONFIG_PPC_ISERIES
549 for (i = 1; i < lmb.memory.cnt; i++) {
550 unsigned long base, prevbase, prevsize;
552 prevbase = lmb.memory.region[i-1].physbase;
553 prevsize = lmb.memory.region[i-1].size;
554 base = lmb.memory.region[i].physbase;
555 if (base > (prevbase + prevsize)) {
556 io_hole_start = prevbase + prevsize;
557 io_hole_size = base - (prevbase + prevsize);
561 #endif /* CONFIG_PPC_ISERIES */
563 printk("IO Hole assumed to be %lx -> %lx\n",
564 io_hole_start, io_hole_start + io_hole_size - 1);
566 ppc64_boot_msg(0x100, "MM Init Done");
570 * This is called by /dev/mem to know if a given address has to
571 * be mapped non-cacheable or not
573 int page_is_ram(unsigned long pfn)
576 unsigned long paddr = (pfn << PAGE_SHIFT);
578 for (i=0; i < lmb.memory.cnt; i++) {
581 #ifdef CONFIG_MSCHUNKS
582 base = lmb.memory.region[i].physbase;
584 base = lmb.memory.region[i].base;
586 if ((paddr >= base) &&
587 (paddr < (base + lmb.memory.region[i].size))) {
594 EXPORT_SYMBOL(page_is_ram);
597 * Initialize the bootmem system and give it all the memory we
600 #ifndef CONFIG_DISCONTIGMEM
601 void __init do_init_bootmem(void)
604 unsigned long start, bootmap_pages;
605 unsigned long total_pages = lmb_end_of_DRAM() >> PAGE_SHIFT;
609 * Find an area to use for the bootmem bitmap. Calculate the size of
610 * bitmap required as (Total Memory) / PAGE_SIZE / BITS_PER_BYTE.
611 * Add 1 additional page in case the address isn't page-aligned.
613 bootmap_pages = bootmem_bootmap_pages(total_pages);
615 start = abs_to_phys(lmb_alloc(bootmap_pages<<PAGE_SHIFT, PAGE_SIZE));
618 boot_mapsize = init_bootmem(start >> PAGE_SHIFT, total_pages);
620 max_pfn = max_low_pfn;
622 /* add all physical memory to the bootmem map. Also find the first */
623 for (i=0; i < lmb.memory.cnt; i++) {
624 unsigned long physbase, size;
626 physbase = lmb.memory.region[i].physbase;
627 size = lmb.memory.region[i].size;
628 free_bootmem(physbase, size);
631 /* reserve the sections we're already using */
632 for (i=0; i < lmb.reserved.cnt; i++) {
633 unsigned long physbase = lmb.reserved.region[i].physbase;
634 unsigned long size = lmb.reserved.region[i].size;
636 reserve_bootmem(physbase, size);
641 * paging_init() sets up the page tables - in fact we've already done this.
643 void __init paging_init(void)
645 unsigned long zones_size[MAX_NR_ZONES];
646 unsigned long zholes_size[MAX_NR_ZONES];
647 unsigned long total_ram = lmb_phys_mem_size();
648 unsigned long top_of_ram = lmb_end_of_DRAM();
650 printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
651 top_of_ram, total_ram);
652 printk(KERN_INFO "Memory hole size: %ldMB\n",
653 (top_of_ram - total_ram) >> 20);
655 * All pages are DMA-able so we put them all in the DMA zone.
657 memset(zones_size, 0, sizeof(zones_size));
658 memset(zholes_size, 0, sizeof(zholes_size));
660 zones_size[ZONE_DMA] = top_of_ram >> PAGE_SHIFT;
661 zholes_size[ZONE_DMA] = (top_of_ram - total_ram) >> PAGE_SHIFT;
663 free_area_init_node(0, &contig_page_data, zones_size,
664 __pa(PAGE_OFFSET) >> PAGE_SHIFT, zholes_size);
665 mem_map = contig_page_data.node_mem_map;
667 #endif /* CONFIG_DISCONTIGMEM */
669 static struct kcore_list kcore_vmem;
671 static int __init setup_kcore(void)
675 for (i=0; i < lmb.memory.cnt; i++) {
676 unsigned long physbase, size;
677 struct kcore_list *kcore_mem;
679 physbase = lmb.memory.region[i].physbase;
680 size = lmb.memory.region[i].size;
682 /* GFP_ATOMIC to avoid might_sleep warnings during boot */
683 kcore_mem = kmalloc(sizeof(struct kcore_list), GFP_ATOMIC);
685 panic("mem_init: kmalloc failed\n");
687 kclist_add(kcore_mem, __va(physbase), size);
690 kclist_add(&kcore_vmem, (void *)VMALLOC_START, VMALLOC_END-VMALLOC_START);
694 module_init(setup_kcore);
696 void __init mem_init(void)
698 #ifdef CONFIG_DISCONTIGMEM
704 unsigned long reservedpages = 0, codesize, initsize, datasize, bsssize;
706 num_physpages = max_low_pfn; /* RAM is assumed contiguous */
707 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
709 #ifdef CONFIG_DISCONTIGMEM
710 for (nid = 0; nid < numnodes; nid++) {
711 if (NODE_DATA(nid)->node_spanned_pages != 0) {
712 printk("freeing bootmem node %x\n", nid);
714 free_all_bootmem_node(NODE_DATA(nid));
718 max_mapnr = num_physpages;
719 totalram_pages += free_all_bootmem();
722 for_each_pgdat(pgdat) {
723 for (i = 0; i < pgdat->node_spanned_pages; i++) {
724 page = pgdat->node_mem_map + i;
725 if (PageReserved(page))
730 codesize = (unsigned long)&_etext - (unsigned long)&_stext;
731 initsize = (unsigned long)&__init_end - (unsigned long)&__init_begin;
732 datasize = (unsigned long)&_edata - (unsigned long)&__init_end;
733 bsssize = (unsigned long)&__bss_stop - (unsigned long)&__bss_start;
735 printk(KERN_INFO "Memory: %luk/%luk available (%luk kernel code, "
736 "%luk reserved, %luk data, %luk bss, %luk init)\n",
737 (unsigned long)nr_free_pages() << (PAGE_SHIFT-10),
738 num_physpages << (PAGE_SHIFT-10),
740 reservedpages << (PAGE_SHIFT-10),
747 #ifdef CONFIG_PPC_ISERIES
753 * This is called when a page has been modified by the kernel.
754 * It just marks the page as not i-cache clean. We do the i-cache
755 * flush later when the page is given to a user process, if necessary.
757 void flush_dcache_page(struct page *page)
759 if (cur_cpu_spec->cpu_features & CPU_FTR_COHERENT_ICACHE)
761 /* avoid an atomic op if possible */
762 if (test_bit(PG_arch_1, &page->flags))
763 clear_bit(PG_arch_1, &page->flags);
766 void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
770 if (cur_cpu_spec->cpu_features & CPU_FTR_COHERENT_ICACHE)
773 * We shouldnt have to do this, but some versions of glibc
774 * require it (ld.so assumes zero filled pages are icache clean)
778 /* avoid an atomic op if possible */
779 if (test_bit(PG_arch_1, &pg->flags))
780 clear_bit(PG_arch_1, &pg->flags);
783 void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
786 copy_page(vto, vfrom);
789 * We should be able to use the following optimisation, however
790 * there are two problems.
791 * Firstly a bug in some versions of binutils meant PLT sections
792 * were not marked executable.
793 * Secondly the first word in the GOT section is blrl, used
794 * to establish the GOT address. Until recently the GOT was
795 * not marked executable.
799 if (!vma->vm_file && ((vma->vm_flags & VM_EXEC) == 0))
803 if (cur_cpu_spec->cpu_features & CPU_FTR_COHERENT_ICACHE)
806 /* avoid an atomic op if possible */
807 if (test_bit(PG_arch_1, &pg->flags))
808 clear_bit(PG_arch_1, &pg->flags);
811 void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
812 unsigned long addr, int len)
816 maddr = (unsigned long)page_address(page) + (addr & ~PAGE_MASK);
817 flush_icache_range(maddr, maddr + len);
821 * This is called at the end of handling a user page fault, when the
822 * fault has been handled by updating a PTE in the linux page tables.
823 * We use it to preload an HPTE into the hash table corresponding to
824 * the updated linux PTE.
826 * This must always be called with the mm->page_table_lock held
828 void update_mmu_cache(struct vm_area_struct *vma, unsigned long ea,
838 /* handle i-cache coherency */
839 if (!(cur_cpu_spec->cpu_features & CPU_FTR_COHERENT_ICACHE) &&
840 !(cur_cpu_spec->cpu_features & CPU_FTR_NOEXECUTE)) {
841 unsigned long pfn = pte_pfn(pte);
842 if (pfn_valid(pfn)) {
843 struct page *page = pfn_to_page(pfn);
844 if (!PageReserved(page)
845 && !test_bit(PG_arch_1, &page->flags)) {
846 __flush_dcache_icache(page_address(page));
847 set_bit(PG_arch_1, &page->flags);
852 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
856 pgdir = vma->vm_mm->pgd;
860 ptep = find_linux_pte(pgdir, ea);
864 vsid = get_vsid(vma->vm_mm->context.id, ea);
866 local_irq_save(flags);
867 tmp = cpumask_of_cpu(smp_processor_id());
868 if (cpus_equal(vma->vm_mm->cpu_vm_mask, tmp))
871 __hash_page(ea, pte_val(pte) & (_PAGE_USER|_PAGE_RW), vsid, ptep,
873 local_irq_restore(flags);
876 void * reserve_phb_iospace(unsigned long size)
880 if (phbs_io_bot >= IMALLOC_BASE)
881 panic("reserve_phb_iospace(): phb io space overflow\n");
883 virt_addr = (void *) phbs_io_bot;
889 kmem_cache_t *zero_cache;
891 static void zero_ctor(void *pte, kmem_cache_t *cache, unsigned long flags)
893 memset(pte, 0, PAGE_SIZE);
896 void pgtable_cache_init(void)
898 zero_cache = kmem_cache_create("zero",
901 SLAB_HWCACHE_ALIGN | SLAB_MUST_HWCACHE_ALIGN,
905 panic("pgtable_cache_init(): could not create zero_cache!\n");