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