2 * This file contains the routines for initializing the MMU
3 * on the 8xx series of chips.
6 * Derived from arch/powerpc/mm/40x_mmu.c:
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
15 #include <linux/memblock.h>
16 #include <linux/mmu_context.h>
17 #include <asm/fixmap.h>
18 #include <asm/code-patching.h>
22 #define IMMR_SIZE (FIX_IMMR_SIZE << PAGE_SHIFT)
24 extern int __map_without_ltlbs;
26 static unsigned long block_mapped_ram;
29 * Return PA for this VA if it is in an area mapped with LTLBs.
30 * Otherwise, returns 0
32 phys_addr_t v_block_mapped(unsigned long va)
34 unsigned long p = PHYS_IMMR_BASE;
36 if (__map_without_ltlbs)
38 if (va >= VIRT_IMMR_BASE && va < VIRT_IMMR_BASE + IMMR_SIZE)
39 return p + va - VIRT_IMMR_BASE;
40 if (va >= PAGE_OFFSET && va < PAGE_OFFSET + block_mapped_ram)
46 * Return VA for a given PA mapped with LTLBs or 0 if not mapped
48 unsigned long p_block_mapped(phys_addr_t pa)
50 unsigned long p = PHYS_IMMR_BASE;
52 if (__map_without_ltlbs)
54 if (pa >= p && pa < p + IMMR_SIZE)
55 return VIRT_IMMR_BASE + pa - p;
56 if (pa < block_mapped_ram)
57 return (unsigned long)__va(pa);
61 #define LARGE_PAGE_SIZE_8M (1<<23)
64 * MMU_init_hw does the chip-specific initialization of the MMU hardware.
66 void __init MMU_init_hw(void)
68 /* PIN up to the 3 first 8Mb after IMMR in DTLB table */
69 #ifdef CONFIG_PIN_TLB_DATA
70 unsigned long ctr = mfspr(SPRN_MD_CTR) & 0xfe000000;
71 unsigned long flags = 0xf0 | MD_SPS16K | _PAGE_SH | _PAGE_DIRTY;
72 #ifdef CONFIG_PIN_TLB_IMMR
77 unsigned long addr = 0;
78 unsigned long mem = total_lowmem;
80 for (; i < 32 && mem >= LARGE_PAGE_SIZE_8M; i++) {
81 mtspr(SPRN_MD_CTR, ctr | (i << 8));
82 mtspr(SPRN_MD_EPN, (unsigned long)__va(addr) | MD_EVALID);
83 mtspr(SPRN_MD_TWC, MD_PS8MEG | MD_SVALID);
84 mtspr(SPRN_MD_RPN, addr | flags | _PAGE_PRESENT);
85 addr += LARGE_PAGE_SIZE_8M;
86 mem -= LARGE_PAGE_SIZE_8M;
91 static void __init mmu_mapin_immr(void)
93 unsigned long p = PHYS_IMMR_BASE;
94 unsigned long v = VIRT_IMMR_BASE;
97 for (offset = 0; offset < IMMR_SIZE; offset += PAGE_SIZE)
98 map_kernel_page(v + offset, p + offset, PAGE_KERNEL_NCG);
101 static void __init mmu_patch_cmp_limit(s32 *site, unsigned long mapped)
103 unsigned int instr = *(unsigned int *)patch_site_addr(site);
106 instr |= (unsigned long)__va(mapped) >> 16;
107 patch_instruction_site(site, instr);
110 unsigned long __init mmu_mapin_ram(unsigned long top)
112 unsigned long mapped;
114 if (__map_without_ltlbs) {
117 #ifndef CONFIG_PIN_TLB_IMMR
118 patch_instruction_site(&patch__dtlbmiss_immr_jmp, PPC_INST_NOP);
120 #ifndef CONFIG_PIN_TLB_TEXT
121 mmu_patch_cmp_limit(&patch__itlbmiss_linmem_top, 0);
124 mapped = top & ~(LARGE_PAGE_SIZE_8M - 1);
127 mmu_patch_cmp_limit(&patch__dtlbmiss_linmem_top, mapped);
128 mmu_patch_cmp_limit(&patch__fixupdar_linmem_top, mapped);
130 /* If the size of RAM is not an exact power of two, we may not
131 * have covered RAM in its entirety with 8 MiB
132 * pages. Consequently, restrict the top end of RAM currently
133 * allocable so that calls to the MEMBLOCK to allocate PTEs for "tail"
134 * coverage with normal-sized pages (or other reasons) do not
135 * attempt to allocate outside the allowed range.
138 memblock_set_current_limit(mapped);
140 block_mapped_ram = mapped;
145 void __init setup_initial_memory_limit(phys_addr_t first_memblock_base,
146 phys_addr_t first_memblock_size)
148 /* We don't currently support the first MEMBLOCK not mapping 0
149 * physical on those processors
151 BUG_ON(first_memblock_base != 0);
153 /* 8xx can only access 24MB at the moment */
154 memblock_set_current_limit(min_t(u64, first_memblock_size, 0x01800000));
158 * Set up to use a given MMU context.
159 * id is context number, pgd is PGD pointer.
161 * We place the physical address of the new task page directory loaded
162 * into the MMU base register, and set the ASID compare register with
165 void set_context(unsigned long id, pgd_t *pgd)
167 s16 offset = (s16)(__pa(swapper_pg_dir));
169 #ifdef CONFIG_BDI_SWITCH
170 pgd_t **ptr = *(pgd_t ***)(KERNELBASE + 0xf0);
172 /* Context switch the PTE pointer for the Abatron BDI2000.
173 * The PGDIR is passed as second argument.
178 /* Register M_TW will contain base address of level 1 table minus the
179 * lower part of the kernel PGDIR base address, so that all accesses to
180 * level 1 table are done relative to lower part of kernel PGDIR base
183 mtspr(SPRN_M_TW, __pa(pgd) - offset);
186 mtspr(SPRN_M_CASID, id - 1);
191 void flush_instruction_cache(void)
194 mtspr(SPRN_IC_CST, IDC_INVALL);