1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Based upon linux/arch/m68k/mm/sun3mmu.c 4 * Based upon linux/arch/ppc/mm/mmu_context.c 5 * 6 * Implementations of mm routines specific to the Coldfire MMU. 7 * 8 * Copyright (c) 2008 Freescale Semiconductor, Inc. 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/types.h> 13 #include <linux/mm.h> 14 #include <linux/init.h> 15 #include <linux/string.h> 16 #include <linux/memblock.h> 17 18 #include <asm/setup.h> 19 #include <asm/page.h> 20 #include <asm/mmu_context.h> 21 #include <asm/mcf_pgalloc.h> 22 #include <asm/tlbflush.h> 23 #include <asm/pgalloc.h> 24 25 #define KMAPAREA(x) ((x >= VMALLOC_START) && (x < KMAP_END)) 26 27 mm_context_t next_mmu_context; 28 unsigned long context_map[LAST_CONTEXT / BITS_PER_LONG + 1]; 29 atomic_t nr_free_contexts; 30 struct mm_struct *context_mm[LAST_CONTEXT+1]; 31 unsigned long num_pages; 32 33 /* 34 * ColdFire paging_init derived from sun3. 35 */ 36 void __init paging_init(void) 37 { 38 pgd_t *pg_dir; 39 pte_t *pg_table; 40 unsigned long address, size; 41 unsigned long next_pgtable; 42 int i; 43 44 pg_dir = swapper_pg_dir; 45 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir)); 46 47 size = num_pages * sizeof(pte_t); 48 size = (size + PAGE_SIZE) & ~(PAGE_SIZE-1); 49 next_pgtable = (unsigned long) memblock_alloc_or_panic(size, PAGE_SIZE); 50 51 pg_dir += PAGE_OFFSET >> PGDIR_SHIFT; 52 53 address = PAGE_OFFSET; 54 while (address < (unsigned long)high_memory) { 55 pg_table = (pte_t *) next_pgtable; 56 next_pgtable += PTRS_PER_PTE * sizeof(pte_t); 57 pgd_val(*pg_dir) = (unsigned long) pg_table; 58 pg_dir++; 59 60 /* now change pg_table to kernel virtual addresses */ 61 for (i = 0; i < PTRS_PER_PTE; ++i, ++pg_table) { 62 pte_t pte = pfn_pte(virt_to_pfn((void *)address), 63 PAGE_INIT); 64 if (address >= (unsigned long) high_memory) 65 pte_val(pte) = 0; 66 67 set_pte(pg_table, pte); 68 address += PAGE_SIZE; 69 } 70 } 71 72 current->mm = NULL; 73 } 74 75 int cf_tlb_miss(struct pt_regs *regs, int write, int dtlb, int extension_word) 76 { 77 unsigned long flags, mmuar, mmutr; 78 struct mm_struct *mm; 79 pgd_t *pgd; 80 p4d_t *p4d; 81 pud_t *pud; 82 pmd_t *pmd; 83 pte_t *pte = NULL; 84 int ret = -1; 85 int asid; 86 87 local_irq_save(flags); 88 89 mmuar = (dtlb) ? mmu_read(MMUAR) : 90 regs->pc + (extension_word * sizeof(long)); 91 92 mm = (!user_mode(regs) && KMAPAREA(mmuar)) ? &init_mm : current->mm; 93 if (!mm) 94 goto out; 95 96 pgd = pgd_offset(mm, mmuar); 97 if (pgd_none(*pgd)) 98 goto out; 99 100 p4d = p4d_offset(pgd, mmuar); 101 if (p4d_none(*p4d)) 102 goto out; 103 104 pud = pud_offset(p4d, mmuar); 105 if (pud_none(*pud)) 106 goto out; 107 108 pmd = pmd_offset(pud, mmuar); 109 if (pmd_none(*pmd)) 110 goto out; 111 112 pte = (KMAPAREA(mmuar)) ? pte_offset_kernel(pmd, mmuar) 113 : pte_offset_map(pmd, mmuar); 114 if (!pte || pte_none(*pte) || !pte_present(*pte)) 115 goto out; 116 117 if (write) { 118 if (!pte_write(*pte)) 119 goto out; 120 set_pte(pte, pte_mkdirty(*pte)); 121 } 122 123 set_pte(pte, pte_mkyoung(*pte)); 124 asid = mm->context & 0xff; 125 if (!pte_dirty(*pte) && !KMAPAREA(mmuar)) 126 set_pte(pte, pte_wrprotect(*pte)); 127 128 mmutr = (mmuar & PAGE_MASK) | (asid << MMUTR_IDN) | MMUTR_V; 129 if ((mmuar < TASK_UNMAPPED_BASE) || (mmuar >= TASK_SIZE)) 130 mmutr |= (pte->pte & CF_PAGE_MMUTR_MASK) >> CF_PAGE_MMUTR_SHIFT; 131 mmu_write(MMUTR, mmutr); 132 133 mmu_write(MMUDR, (pte_val(*pte) & PAGE_MASK) | 134 ((pte->pte) & CF_PAGE_MMUDR_MASK) | MMUDR_SZ_8KB | MMUDR_X); 135 136 if (dtlb) 137 mmu_write(MMUOR, MMUOR_ACC | MMUOR_UAA); 138 else 139 mmu_write(MMUOR, MMUOR_ITLB | MMUOR_ACC | MMUOR_UAA); 140 ret = 0; 141 out: 142 if (pte && !KMAPAREA(mmuar)) 143 pte_unmap(pte); 144 local_irq_restore(flags); 145 return ret; 146 } 147 148 void __init cf_bootmem_alloc(void) 149 { 150 unsigned long memstart; 151 152 /* _rambase and _ramend will be naturally page aligned */ 153 m68k_memory[0].addr = _rambase; 154 m68k_memory[0].size = _ramend - _rambase; 155 156 memblock_add_node(m68k_memory[0].addr, m68k_memory[0].size, 0, 157 MEMBLOCK_NONE); 158 159 /* compute total pages in system */ 160 num_pages = PFN_DOWN(_ramend - _rambase); 161 162 /* page numbers */ 163 memstart = PAGE_ALIGN(_ramstart); 164 min_low_pfn = PFN_DOWN(_rambase); 165 max_pfn = max_low_pfn = PFN_DOWN(_ramend); 166 high_memory = (void *)_ramend; 167 168 /* Reserve kernel text/data/bss */ 169 memblock_reserve(_rambase, memstart - _rambase); 170 171 m68k_virt_to_node_shift = fls(_ramend - 1) - 6; 172 module_fixup(NULL, __start_fixup, __stop_fixup); 173 174 /* setup node data */ 175 m68k_setup_node(0); 176 } 177 178 /* 179 * Initialize the context management stuff. 180 * The following was taken from arch/ppc/mmu_context.c 181 */ 182 void __init cf_mmu_context_init(void) 183 { 184 /* 185 * Some processors have too few contexts to reserve one for 186 * init_mm, and require using context 0 for a normal task. 187 * Other processors reserve the use of context zero for the kernel. 188 * This code assumes FIRST_CONTEXT < 32. 189 */ 190 context_map[0] = (1 << FIRST_CONTEXT) - 1; 191 next_mmu_context = FIRST_CONTEXT; 192 atomic_set(&nr_free_contexts, LAST_CONTEXT - FIRST_CONTEXT + 1); 193 } 194 195 /* 196 * Steal a context from a task that has one at the moment. 197 * This isn't an LRU system, it just frees up each context in 198 * turn (sort-of pseudo-random replacement :). This would be the 199 * place to implement an LRU scheme if anyone was motivated to do it. 200 * -- paulus 201 */ 202 void steal_context(void) 203 { 204 struct mm_struct *mm; 205 /* 206 * free up context `next_mmu_context' 207 * if we shouldn't free context 0, don't... 208 */ 209 if (next_mmu_context < FIRST_CONTEXT) 210 next_mmu_context = FIRST_CONTEXT; 211 mm = context_mm[next_mmu_context]; 212 flush_tlb_mm(mm); 213 destroy_context(mm); 214 } 215 216 static const pgprot_t protection_map[16] = { 217 [VM_NONE] = PAGE_NONE, 218 [VM_READ] = __pgprot(CF_PAGE_VALID | 219 CF_PAGE_ACCESSED | 220 CF_PAGE_READABLE), 221 [VM_WRITE] = __pgprot(CF_PAGE_VALID | 222 CF_PAGE_ACCESSED | 223 CF_PAGE_WRITABLE), 224 [VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID | 225 CF_PAGE_ACCESSED | 226 CF_PAGE_READABLE | 227 CF_PAGE_WRITABLE), 228 [VM_EXEC] = __pgprot(CF_PAGE_VALID | 229 CF_PAGE_ACCESSED | 230 CF_PAGE_EXEC), 231 [VM_EXEC | VM_READ] = __pgprot(CF_PAGE_VALID | 232 CF_PAGE_ACCESSED | 233 CF_PAGE_READABLE | 234 CF_PAGE_EXEC), 235 [VM_EXEC | VM_WRITE] = __pgprot(CF_PAGE_VALID | 236 CF_PAGE_ACCESSED | 237 CF_PAGE_WRITABLE | 238 CF_PAGE_EXEC), 239 [VM_EXEC | VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID | 240 CF_PAGE_ACCESSED | 241 CF_PAGE_READABLE | 242 CF_PAGE_WRITABLE | 243 CF_PAGE_EXEC), 244 [VM_SHARED] = PAGE_NONE, 245 [VM_SHARED | VM_READ] = __pgprot(CF_PAGE_VALID | 246 CF_PAGE_ACCESSED | 247 CF_PAGE_READABLE), 248 [VM_SHARED | VM_WRITE] = PAGE_SHARED, 249 [VM_SHARED | VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID | 250 CF_PAGE_ACCESSED | 251 CF_PAGE_READABLE | 252 CF_PAGE_SHARED), 253 [VM_SHARED | VM_EXEC] = __pgprot(CF_PAGE_VALID | 254 CF_PAGE_ACCESSED | 255 CF_PAGE_EXEC), 256 [VM_SHARED | VM_EXEC | VM_READ] = __pgprot(CF_PAGE_VALID | 257 CF_PAGE_ACCESSED | 258 CF_PAGE_READABLE | 259 CF_PAGE_EXEC), 260 [VM_SHARED | VM_EXEC | VM_WRITE] = __pgprot(CF_PAGE_VALID | 261 CF_PAGE_ACCESSED | 262 CF_PAGE_SHARED | 263 CF_PAGE_EXEC), 264 [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID | 265 CF_PAGE_ACCESSED | 266 CF_PAGE_READABLE | 267 CF_PAGE_SHARED | 268 CF_PAGE_EXEC) 269 }; 270 DECLARE_VM_GET_PAGE_PROT 271