| 1 | /* |
| 2 | * linux/arch/m68k/mm/cf-mmu.c |
| 3 | * |
| 4 | * Based upon linux/arch/m68k/mm/sun3mmu.c |
| 5 | * Based upon linux/arch/ppc/mm/mmu_context.c |
| 6 | * |
| 7 | * Implementations of mm routines specific to the Coldfire MMU. |
| 8 | * |
| 9 | * Copyright (c) 2008 Freescale Semiconductor, Inc. |
| 10 | * Copyright Freescale Semiconductor, Inc. 2008-2009 |
| 11 | * Jason Jin Jason.Jin@freescale.com |
| 12 | * Shrek Wu B16972@freescale.com |
| 13 | */ |
| 14 | |
| 15 | #include <linux/signal.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/swap.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/init.h> |
| 23 | #ifdef CONFIG_BLK_DEV_RAM |
| 24 | #include <linux/blkdev.h> |
| 25 | #endif |
| 26 | #include <linux/bootmem.h> |
| 27 | |
| 28 | #include <asm/setup.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/page.h> |
| 31 | #include <asm/pgtable.h> |
| 32 | #include <asm/system.h> |
| 33 | #include <asm/machdep.h> |
| 34 | #include <asm/io.h> |
| 35 | #include <asm/mmu_context.h> |
| 36 | #include <asm/cf_pgalloc.h> |
| 37 | |
| 38 | #include <asm/coldfire.h> |
| 39 | #include <asm/tlbflush.h> |
| 40 | |
| 41 | #define KMAPAREA(x) ((x >= VMALLOC_START) && (x < KMAP_END)) |
| 42 | |
| 43 | #undef DEBUG |
| 44 | |
| 45 | #ifdef CONFIG_VDSO |
| 46 | unsigned long next_mmu_context; |
| 47 | #else |
| 48 | mm_context_t next_mmu_context; |
| 49 | #endif |
| 50 | |
| 51 | unsigned long context_map[LAST_CONTEXT / BITS_PER_LONG + 1]; |
| 52 | |
| 53 | atomic_t nr_free_contexts; |
| 54 | struct mm_struct *context_mm[LAST_CONTEXT+1]; |
| 55 | void steal_context(void); |
| 56 | #ifdef CONFIG_M5445X |
| 57 | void m68k_setup_node(int); |
| 58 | #endif |
| 59 | const char bad_pmd_string[] = "Bad pmd in pte_alloc: %08lx\n"; |
| 60 | |
| 61 | extern unsigned long empty_bad_page_table; |
| 62 | extern unsigned long empty_bad_page; |
| 63 | extern unsigned long num_pages; |
| 64 | #ifdef CONFIG_M5445X |
| 65 | extern unsigned long availmem; |
| 66 | #endif |
| 67 | extern char __init_begin, __init_end; |
| 68 | |
| 69 | /* |
| 70 | * Free memory used for system initialization. |
| 71 | */ |
| 72 | void free_initmem(void) |
| 73 | { |
| 74 | #if 0 |
| 75 | unsigned long addr; |
| 76 | unsigned long start = (unsigned long)&__init_begin; |
| 77 | unsigned long end = (unsigned long)&__init_end; |
| 78 | |
| 79 | printk(KERN_INFO "free_initmem: __init_begin = 0x%lx __init_end = 0x%lx\n", start, end); |
| 80 | |
| 81 | addr = (unsigned long)&__init_begin; |
| 82 | for (; addr < (unsigned long)&__init_end; addr += PAGE_SIZE) { |
| 83 | /* not currently used */ |
| 84 | virt_to_page(addr)->flags &= ~(1 << PG_reserved); |
| 85 | init_page_count(virt_to_page(addr)); |
| 86 | free_page(addr); |
| 87 | totalram_pages++; |
| 88 | } |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Initialize the paging system. |
| 94 | */ |
| 95 | void __init paging_init(void) |
| 96 | { |
| 97 | pgd_t * pg_dir; |
| 98 | pte_t * pg_table; |
| 99 | int i; |
| 100 | unsigned long address; |
| 101 | unsigned long next_pgtable; |
| 102 | unsigned long zones_size[MAX_NR_ZONES]; |
| 103 | unsigned long size; |
| 104 | enum zone_type zone; |
| 105 | |
| 106 | /* allocate zero page */ |
| 107 | empty_zero_page = (void *)alloc_bootmem_pages(PAGE_SIZE); |
| 108 | memset((void *)empty_zero_page, 0, PAGE_SIZE); |
| 109 | |
| 110 | /* zero kernel page directory */ |
| 111 | pg_dir = swapper_pg_dir; |
| 112 | memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir)); |
| 113 | /* |
| 114 | * setup page tables for PHYSRAM |
| 115 | */ |
| 116 | |
| 117 | /* starting loc in page directory */ |
| 118 | pg_dir += PAGE_OFFSET >> PGDIR_SHIFT; |
| 119 | |
| 120 | /* allocate page tables */ |
| 121 | size = num_pages * sizeof(pte_t); |
| 122 | size = (size + PAGE_SIZE) & ~(PAGE_SIZE-1); |
| 123 | next_pgtable = (unsigned long)alloc_bootmem_pages(size); |
| 124 | address = PAGE_OFFSET; |
| 125 | while (address < (unsigned long)high_memory) { |
| 126 | /* setup page table in page directory */ |
| 127 | pg_table = (pte_t *)next_pgtable; |
| 128 | next_pgtable += PTRS_PER_PTE * sizeof(pte_t); |
| 129 | pgd_val(*pg_dir) = (unsigned long)pg_table; |
| 130 | pg_dir++; |
| 131 | |
| 132 | /* create PTEs in page table */ |
| 133 | for (i=0; i<PTRS_PER_PTE; ++i, ++pg_table) { |
| 134 | pte_t pte = pfn_pte(virt_to_pfn(address), PAGE_INIT); |
| 135 | if (address >= (unsigned long)high_memory) |
| 136 | pte_val (pte) = 0; |
| 137 | |
| 138 | set_pte(pg_table, pte); |
| 139 | address += PAGE_SIZE; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * setup page tables for DMA area |
| 145 | */ |
| 146 | |
| 147 | /* starting loc in page directory */ |
| 148 | pg_dir = swapper_pg_dir; |
| 149 | pg_dir += CONFIG_DMA_BASE >> PGDIR_SHIFT; |
| 150 | |
| 151 | /* allocate page tables */ |
| 152 | size = (CONFIG_DMA_SIZE >> PAGE_SHIFT) * sizeof(pte_t); |
| 153 | size = (size + PAGE_SIZE) & ~(PAGE_SIZE-1); |
| 154 | next_pgtable = (unsigned long)alloc_bootmem_pages(size); |
| 155 | address = CONFIG_DMA_BASE; |
| 156 | while (address < (CONFIG_DMA_BASE + CONFIG_DMA_SIZE)) { |
| 157 | /* setup page table in page directory */ |
| 158 | pg_table = (pte_t *)next_pgtable; |
| 159 | next_pgtable += PTRS_PER_PTE * sizeof(pte_t); |
| 160 | pgd_val(*pg_dir) = (unsigned long)pg_table; |
| 161 | pg_dir++; |
| 162 | |
| 163 | /* create PTEs in page table */ |
| 164 | for (i=0; i<PTRS_PER_PTE; ++i, ++pg_table) { |
| 165 | pte_t pte = pfn_pte(virt_to_pfn(address), PAGE_INIT); |
| 166 | if (address >= (CONFIG_DMA_BASE + CONFIG_DMA_SIZE)) |
| 167 | pte_val (pte) = 0; |
| 168 | |
| 169 | set_pte(pg_table, pte); |
| 170 | address += PAGE_SIZE; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * setup zones |
| 176 | */ |
| 177 | |
| 178 | current->mm = NULL; |
| 179 | |
| 180 | /* clear zones */ |
| 181 | for (zone = 0; zone < MAX_NR_ZONES; zone++) |
| 182 | zones_size[zone] = 0x0; |
| 183 | |
| 184 | zones_size[ZONE_DMA] = CONFIG_DMA_SIZE >> PAGE_SHIFT; |
| 185 | zones_size[ZONE_NORMAL] = (((unsigned long)high_memory - |
| 186 | PAGE_OFFSET) >> PAGE_SHIFT) - |
| 187 | zones_size[ZONE_DMA]; |
| 188 | |
| 189 | free_area_init(zones_size); |
| 190 | } |
| 191 | /* |
| 192 | * Handle a missed TLB |
| 193 | */ |
| 194 | int cf_tlb_miss(struct pt_regs *regs, int write, int dtlb, int extension_word) |
| 195 | { |
| 196 | struct mm_struct *mm; |
| 197 | pgd_t *pgd; |
| 198 | pmd_t *pmd; |
| 199 | pte_t *pte; |
| 200 | unsigned long mmuar; |
| 201 | int asid; |
| 202 | int flags; |
| 203 | |
| 204 | local_save_flags(flags); |
| 205 | local_irq_disable(); |
| 206 | |
| 207 | mmuar = ( dtlb ) ? regs->mmuar |
| 208 | : regs->pc + (extension_word * sizeof(long)); |
| 209 | |
| 210 | mm = (!user_mode(regs) && KMAPAREA(mmuar)) ? &init_mm : current->mm; |
| 211 | |
| 212 | if (!mm) { |
| 213 | local_irq_restore(flags); |
| 214 | return (-1); |
| 215 | } |
| 216 | |
| 217 | pgd = pgd_offset(mm, mmuar); |
| 218 | if (pgd_none(*pgd)) { |
| 219 | local_irq_restore(flags); |
| 220 | return (-1); |
| 221 | } |
| 222 | |
| 223 | pmd = pmd_offset(pgd, mmuar); |
| 224 | if (pmd_none(*pmd)) { |
| 225 | local_irq_restore(flags); |
| 226 | return (-1); |
| 227 | } |
| 228 | |
| 229 | pte = (KMAPAREA(mmuar)) ? pte_offset_kernel(pmd, mmuar) |
| 230 | : pte_offset_map(pmd, mmuar); |
| 231 | if (pte_none(*pte) || !pte_present(*pte)) { |
| 232 | local_irq_restore(flags); |
| 233 | return (-1); |
| 234 | } |
| 235 | |
| 236 | if (write) { |
| 237 | if (!pte_write(*pte)) { |
| 238 | local_irq_restore(flags); |
| 239 | return (-1); |
| 240 | } |
| 241 | set_pte(pte, pte_mkdirty(*pte)); |
| 242 | } |
| 243 | |
| 244 | set_pte(pte, pte_mkyoung(*pte)); |
| 245 | asid = cpu_context(mm) & 0xff; |
| 246 | if (!pte_dirty(*pte) && !KMAPAREA(mmuar)) |
| 247 | set_pte(pte, pte_wrprotect(*pte)); |
| 248 | |
| 249 | *MMUTR = (mmuar & PAGE_MASK) | (asid << CF_ASID_MMU_SHIFT) |
| 250 | | (((int)(pte->pte) & (int)CF_PAGE_MMUTR_MASK ) >> CF_PAGE_MMUTR_SHIFT) |
| 251 | | MMUTR_V; |
| 252 | |
| 253 | *MMUDR = (pte_val(*pte) & PAGE_MASK) |
| 254 | | ((pte->pte) & CF_PAGE_MMUDR_MASK) |
| 255 | | MMUDR_SZ8K | MMUDR_X; |
| 256 | |
| 257 | if ( dtlb ) |
| 258 | *MMUOR = MMUOR_ACC | MMUOR_UAA; |
| 259 | else |
| 260 | *MMUOR = MMUOR_ITLB | MMUOR_ACC | MMUOR_UAA; |
| 261 | |
| 262 | asm("nop"); |
| 263 | |
| 264 | #ifdef DEBUG |
| 265 | printk("cf_tlb_miss: va=%lx, pa=%lx\n", (mmuar & PAGE_MASK), |
| 266 | (pte_val(*pte) & PAGE_MASK)); |
| 267 | #endif |
| 268 | local_irq_restore(flags); |
| 269 | return (0); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /* |
| 274 | * Context Management |
| 275 | * |
| 276 | * Based on arch/ppc/mmu_context.c |
| 277 | */ |
| 278 | |
| 279 | /* |
| 280 | * Initialize the context management system. |
| 281 | */ |
| 282 | void __init mmu_context_init(void) |
| 283 | { |
| 284 | /* |
| 285 | * Some processors have too few contexts to reserve one for |
| 286 | * init_mm, and require using context 0 for a normal task. |
| 287 | * Other processors reserve the use of context zero for the kernel. |
| 288 | * This code assumes FIRST_CONTEXT < 32. |
| 289 | */ |
| 290 | context_map[0] = (1 << FIRST_CONTEXT) - 1; |
| 291 | next_mmu_context = FIRST_CONTEXT; |
| 292 | atomic_set(&nr_free_contexts, LAST_CONTEXT - FIRST_CONTEXT + 1); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Steal a context from a task that has one at the moment. |
| 297 | * This is only used on 8xx and 4xx and we presently assume that |
| 298 | * they don't do SMP. If they do then thicfpgalloc.hs will have to check |
| 299 | * whether the MM we steal is in use. |
| 300 | * We also assume that this is only used on systems that don't |
| 301 | * use an MMU hash table - this is true for 8xx and 4xx. |
| 302 | * This isn't an LRU system, it just frees up each context in |
| 303 | * turn (sort-of pseudo-random replacement :). This would be the |
| 304 | * place to implement an LRU scheme if anyone was motivated to do it. |
| 305 | * -- paulus |
| 306 | */ |
| 307 | void steal_context(void) |
| 308 | { |
| 309 | struct mm_struct *mm; |
| 310 | /* free up context `next_mmu_context' */ |
| 311 | /* if we shouldn't free context 0, don't... */ |
| 312 | if (next_mmu_context < FIRST_CONTEXT) |
| 313 | next_mmu_context = FIRST_CONTEXT; |
| 314 | mm = context_mm[next_mmu_context]; |
| 315 | flush_tlb_mm(mm); |
| 316 | destroy_context(mm); |
| 317 | } |
| 318 | |