1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __M68K_MMU_CONTEXT_H
3 #define __M68K_MMU_CONTEXT_H
5 #include <asm-generic/mm_hooks.h>
6 #include <linux/mm_types.h>
10 #if defined(CONFIG_COLDFIRE)
12 #include <asm/atomic.h>
13 #include <asm/bitops.h>
14 #include <asm/mcfmmu.h>
17 #define NO_CONTEXT 256
18 #define LAST_CONTEXT 255
19 #define FIRST_CONTEXT 1
21 extern unsigned long context_map[];
22 extern mm_context_t next_mmu_context;
24 extern atomic_t nr_free_contexts;
25 extern struct mm_struct *context_mm[LAST_CONTEXT+1];
26 extern void steal_context(void);
28 static inline void get_mmu_context(struct mm_struct *mm)
32 if (mm->context != NO_CONTEXT)
34 while (arch_atomic_dec_and_test_lt(&nr_free_contexts)) {
35 atomic_inc(&nr_free_contexts);
38 ctx = next_mmu_context;
39 while (test_and_set_bit(ctx, context_map)) {
40 ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
41 if (ctx > LAST_CONTEXT)
44 next_mmu_context = (ctx + 1) & LAST_CONTEXT;
50 * Set up the context for a new address space.
52 #define init_new_context(tsk, mm) (((mm)->context = NO_CONTEXT), 0)
55 * We're finished using the context for an address space.
57 #define destroy_context destroy_context
58 static inline void destroy_context(struct mm_struct *mm)
60 if (mm->context != NO_CONTEXT) {
61 clear_bit(mm->context, context_map);
62 mm->context = NO_CONTEXT;
63 atomic_inc(&nr_free_contexts);
67 static inline void set_context(mm_context_t context, pgd_t *pgd)
69 __asm__ __volatile__ ("movec %0,%%asid" : : "d" (context));
72 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
73 struct task_struct *tsk)
75 get_mmu_context(tsk->mm);
76 set_context(tsk->mm->context, next->pgd);
80 * After we have set current->mm to a new value, this activates
81 * the context for the new mm so we see the new mappings.
83 #define activate_mm activate_mm
84 static inline void activate_mm(struct mm_struct *active_mm,
88 set_context(mm->context, mm->pgd);
91 #define prepare_arch_switch(next) load_ksp_mmu(next)
93 static inline void load_ksp_mmu(struct task_struct *task)
105 local_irq_save(flags);
106 mmuar = task->thread.ksp;
108 /* Search for a valid TLB entry, if one is found, don't remap */
109 mmu_write(MMUAR, mmuar);
110 mmu_write(MMUOR, MMUOR_STLB | MMUOR_ADR);
111 if (mmu_read(MMUSR) & MMUSR_HIT)
114 if (mmuar >= PAGE_OFFSET) {
117 pr_info("load_ksp_mmu: non-kernel mm found: 0x%p\n", task->mm);
124 pgd = pgd_offset(mm, mmuar);
128 p4d = p4d_offset(pgd, mmuar);
132 pud = pud_offset(p4d, mmuar);
136 pmd = pmd_offset(pud, mmuar);
140 pte = (mmuar >= PAGE_OFFSET) ? pte_offset_kernel(pmd, mmuar)
141 : pte_offset_map(pmd, mmuar);
142 if (!pte || pte_none(*pte) || !pte_present(*pte))
145 set_pte(pte, pte_mkyoung(*pte));
146 asid = mm->context & 0xff;
147 if (!pte_dirty(*pte) && mmuar <= PAGE_OFFSET)
148 set_pte(pte, pte_wrprotect(*pte));
150 mmu_write(MMUTR, (mmuar & PAGE_MASK) | (asid << MMUTR_IDN) |
151 (((int)(pte->pte) & (int)CF_PAGE_MMUTR_MASK)
152 >> CF_PAGE_MMUTR_SHIFT) | MMUTR_V);
154 mmu_write(MMUDR, (pte_val(*pte) & PAGE_MASK) |
155 ((pte->pte) & CF_PAGE_MMUDR_MASK) | MMUDR_SZ_8KB | MMUDR_X);
157 mmu_write(MMUOR, MMUOR_ACC | MMUOR_UAA);
162 pr_info("ksp load failed: mm=0x%p ksp=0x08%lx\n", mm, mmuar);
164 if (pte && mmuar < PAGE_OFFSET)
166 local_irq_restore(flags);
169 #elif defined(CONFIG_SUN3)
170 #include <asm/sun3mmu.h>
171 #include <linux/sched.h>
173 extern unsigned long get_free_context(struct mm_struct *mm);
174 extern void clear_context(unsigned long context);
176 /* set the context for a new task to unmapped */
177 #define init_new_context init_new_context
178 static inline int init_new_context(struct task_struct *tsk,
179 struct mm_struct *mm)
181 mm->context = SUN3_INVALID_CONTEXT;
185 /* find the context given to this process, and if it hasn't already
186 got one, go get one for it. */
187 static inline void get_mmu_context(struct mm_struct *mm)
189 if (mm->context == SUN3_INVALID_CONTEXT)
190 mm->context = get_free_context(mm);
193 /* flush context if allocated... */
194 #define destroy_context destroy_context
195 static inline void destroy_context(struct mm_struct *mm)
197 if (mm->context != SUN3_INVALID_CONTEXT)
198 clear_context(mm->context);
201 static inline void activate_context(struct mm_struct *mm)
204 sun3_put_context(mm->context);
207 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
208 struct task_struct *tsk)
210 activate_context(tsk->mm);
213 #define activate_mm activate_mm
214 static inline void activate_mm(struct mm_struct *prev_mm,
215 struct mm_struct *next_mm)
217 activate_context(next_mm);
222 #include <asm/setup.h>
223 #include <asm/page.h>
224 #include <asm/cacheflush.h>
226 #define init_new_context init_new_context
227 static inline int init_new_context(struct task_struct *tsk,
228 struct mm_struct *mm)
230 mm->context = virt_to_phys(mm->pgd);
234 static inline void switch_mm_0230(struct mm_struct *mm)
236 unsigned long crp[2] = {
237 0x80000000 | _PAGE_TABLE, mm->context
241 asm volatile (".chip 68030");
243 /* flush MC68030/MC68020 caches (they are virtually addressed) */
248 : "=d" (tmp) : "di" (FLUSH_I_AND_D));
250 /* Switch the root pointer. For a 030-only kernel,
251 * avoid flushing the whole ATC, we only need to
252 * flush the user entries. The 68851 does this by
253 * itself. Avoid a runtime check here.
256 #ifdef CPU_M68030_ONLY
264 asm volatile (".chip 68k");
267 static inline void switch_mm_0460(struct mm_struct *mm)
269 asm volatile (".chip 68040");
271 /* flush address translation cache (user entries) */
272 asm volatile ("pflushan");
274 /* switch the root pointer */
275 asm volatile ("movec %0,%%urp" : : "r" (mm->context));
280 /* clear user entries in the branch cache */
285 : "=d" (tmp): "di" (0x00200000));
288 asm volatile (".chip 68k");
291 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk)
294 if (CPU_IS_020_OR_030)
295 switch_mm_0230(next);
297 switch_mm_0460(next);
301 #define activate_mm activate_mm
302 static inline void activate_mm(struct mm_struct *prev_mm,
303 struct mm_struct *next_mm)
305 next_mm->context = virt_to_phys(next_mm->pgd);
307 if (CPU_IS_020_OR_030)
308 switch_mm_0230(next_mm);
310 switch_mm_0460(next_mm);
315 #include <asm-generic/mmu_context.h>
317 #else /* !CONFIG_MMU */
319 #include <asm-generic/nommu_context.h>
321 #endif /* CONFIG_MMU */
322 #endif /* __M68K_MMU_CONTEXT_H */