xref: /openbmc/linux/arch/m68k/include/asm/mmu_context.h (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __M68K_MMU_CONTEXT_H
3 #define __M68K_MMU_CONTEXT_H
4 
5 #include <asm-generic/mm_hooks.h>
6 #include <linux/mm_types.h>
7 
8 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
9 {
10 }
11 
12 #ifdef CONFIG_MMU
13 
14 #if defined(CONFIG_COLDFIRE)
15 
16 #include <asm/atomic.h>
17 #include <asm/bitops.h>
18 #include <asm/mcfmmu.h>
19 #include <asm/mmu.h>
20 
21 #define NO_CONTEXT		256
22 #define LAST_CONTEXT		255
23 #define FIRST_CONTEXT		1
24 
25 extern unsigned long context_map[];
26 extern mm_context_t next_mmu_context;
27 
28 extern atomic_t nr_free_contexts;
29 extern struct mm_struct *context_mm[LAST_CONTEXT+1];
30 extern void steal_context(void);
31 
32 static inline void get_mmu_context(struct mm_struct *mm)
33 {
34 	mm_context_t ctx;
35 
36 	if (mm->context != NO_CONTEXT)
37 		return;
38 	while (atomic_dec_and_test_lt(&nr_free_contexts)) {
39 		atomic_inc(&nr_free_contexts);
40 		steal_context();
41 	}
42 	ctx = next_mmu_context;
43 	while (test_and_set_bit(ctx, context_map)) {
44 		ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
45 		if (ctx > LAST_CONTEXT)
46 			ctx = 0;
47 	}
48 	next_mmu_context = (ctx + 1) & LAST_CONTEXT;
49 	mm->context = ctx;
50 	context_mm[ctx] = mm;
51 }
52 
53 /*
54  * Set up the context for a new address space.
55  */
56 #define init_new_context(tsk, mm)	(((mm)->context = NO_CONTEXT), 0)
57 
58 /*
59  * We're finished using the context for an address space.
60  */
61 static inline void destroy_context(struct mm_struct *mm)
62 {
63 	if (mm->context != NO_CONTEXT) {
64 		clear_bit(mm->context, context_map);
65 		mm->context = NO_CONTEXT;
66 		atomic_inc(&nr_free_contexts);
67 	}
68 }
69 
70 static inline void set_context(mm_context_t context, pgd_t *pgd)
71 {
72 	__asm__ __volatile__ ("movec %0,%%asid" : : "d" (context));
73 }
74 
75 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
76 	struct task_struct *tsk)
77 {
78 	get_mmu_context(tsk->mm);
79 	set_context(tsk->mm->context, next->pgd);
80 }
81 
82 /*
83  * After we have set current->mm to a new value, this activates
84  * the context for the new mm so we see the new mappings.
85  */
86 static inline void activate_mm(struct mm_struct *active_mm,
87 	struct mm_struct *mm)
88 {
89 	get_mmu_context(mm);
90 	set_context(mm->context, mm->pgd);
91 }
92 
93 #define deactivate_mm(tsk, mm) do { } while (0)
94 
95 extern void mmu_context_init(void);
96 #define prepare_arch_switch(next) load_ksp_mmu(next)
97 
98 static inline void load_ksp_mmu(struct task_struct *task)
99 {
100 	unsigned long flags;
101 	struct mm_struct *mm;
102 	int asid;
103 	pgd_t *pgd;
104 	pmd_t *pmd;
105 	pte_t *pte;
106 	unsigned long mmuar;
107 
108 	local_irq_save(flags);
109 	mmuar = task->thread.ksp;
110 
111 	/* Search for a valid TLB entry, if one is found, don't remap */
112 	mmu_write(MMUAR, mmuar);
113 	mmu_write(MMUOR, MMUOR_STLB | MMUOR_ADR);
114 	if (mmu_read(MMUSR) & MMUSR_HIT)
115 		goto end;
116 
117 	if (mmuar >= PAGE_OFFSET) {
118 		mm = &init_mm;
119 	} else {
120 		pr_info("load_ksp_mmu: non-kernel mm found: 0x%p\n", task->mm);
121 		mm = task->mm;
122 	}
123 
124 	if (!mm)
125 		goto bug;
126 
127 	pgd = pgd_offset(mm, mmuar);
128 	if (pgd_none(*pgd))
129 		goto bug;
130 
131 	pmd = pmd_offset(pgd, mmuar);
132 	if (pmd_none(*pmd))
133 		goto bug;
134 
135 	pte = (mmuar >= PAGE_OFFSET) ? pte_offset_kernel(pmd, mmuar)
136 				     : pte_offset_map(pmd, mmuar);
137 	if (pte_none(*pte) || !pte_present(*pte))
138 		goto bug;
139 
140 	set_pte(pte, pte_mkyoung(*pte));
141 	asid = mm->context & 0xff;
142 	if (!pte_dirty(*pte) && mmuar <= PAGE_OFFSET)
143 		set_pte(pte, pte_wrprotect(*pte));
144 
145 	mmu_write(MMUTR, (mmuar & PAGE_MASK) | (asid << MMUTR_IDN) |
146 		(((int)(pte->pte) & (int)CF_PAGE_MMUTR_MASK)
147 		>> CF_PAGE_MMUTR_SHIFT) | MMUTR_V);
148 
149 	mmu_write(MMUDR, (pte_val(*pte) & PAGE_MASK) |
150 		((pte->pte) & CF_PAGE_MMUDR_MASK) | MMUDR_SZ_8KB | MMUDR_X);
151 
152 	mmu_write(MMUOR, MMUOR_ACC | MMUOR_UAA);
153 
154 	goto end;
155 
156 bug:
157 	pr_info("ksp load failed: mm=0x%p ksp=0x08%lx\n", mm, mmuar);
158 end:
159 	local_irq_restore(flags);
160 }
161 
162 #elif defined(CONFIG_SUN3)
163 #include <asm/sun3mmu.h>
164 #include <linux/sched.h>
165 
166 extern unsigned long get_free_context(struct mm_struct *mm);
167 extern void clear_context(unsigned long context);
168 
169 /* set the context for a new task to unmapped */
170 static inline int init_new_context(struct task_struct *tsk,
171 				   struct mm_struct *mm)
172 {
173 	mm->context = SUN3_INVALID_CONTEXT;
174 	return 0;
175 }
176 
177 /* find the context given to this process, and if it hasn't already
178    got one, go get one for it. */
179 static inline void get_mmu_context(struct mm_struct *mm)
180 {
181 	if (mm->context == SUN3_INVALID_CONTEXT)
182 		mm->context = get_free_context(mm);
183 }
184 
185 /* flush context if allocated... */
186 static inline void destroy_context(struct mm_struct *mm)
187 {
188 	if (mm->context != SUN3_INVALID_CONTEXT)
189 		clear_context(mm->context);
190 }
191 
192 static inline void activate_context(struct mm_struct *mm)
193 {
194 	get_mmu_context(mm);
195 	sun3_put_context(mm->context);
196 }
197 
198 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
199 			     struct task_struct *tsk)
200 {
201 	activate_context(tsk->mm);
202 }
203 
204 #define deactivate_mm(tsk, mm)	do { } while (0)
205 
206 static inline void activate_mm(struct mm_struct *prev_mm,
207 			       struct mm_struct *next_mm)
208 {
209 	activate_context(next_mm);
210 }
211 
212 #else
213 
214 #include <asm/setup.h>
215 #include <asm/page.h>
216 #include <asm/pgalloc.h>
217 
218 static inline int init_new_context(struct task_struct *tsk,
219 				   struct mm_struct *mm)
220 {
221 	mm->context = virt_to_phys(mm->pgd);
222 	return 0;
223 }
224 
225 #define destroy_context(mm)		do { } while(0)
226 
227 static inline void switch_mm_0230(struct mm_struct *mm)
228 {
229 	unsigned long crp[2] = {
230 		0x80000000 | _PAGE_TABLE, mm->context
231 	};
232 	unsigned long tmp;
233 
234 	asm volatile (".chip 68030");
235 
236 	/* flush MC68030/MC68020 caches (they are virtually addressed) */
237 	asm volatile (
238 		"movec %%cacr,%0;"
239 		"orw %1,%0; "
240 		"movec %0,%%cacr"
241 		: "=d" (tmp) : "di" (FLUSH_I_AND_D));
242 
243 	/* Switch the root pointer. For a 030-only kernel,
244 	 * avoid flushing the whole ATC, we only need to
245 	 * flush the user entries. The 68851 does this by
246 	 * itself. Avoid a runtime check here.
247 	 */
248 	asm volatile (
249 #ifdef CPU_M68030_ONLY
250 		"pmovefd %0,%%crp; "
251 		"pflush #0,#4"
252 #else
253 		"pmove %0,%%crp"
254 #endif
255 		: : "m" (crp[0]));
256 
257 	asm volatile (".chip 68k");
258 }
259 
260 static inline void switch_mm_0460(struct mm_struct *mm)
261 {
262 	asm volatile (".chip 68040");
263 
264 	/* flush address translation cache (user entries) */
265 	asm volatile ("pflushan");
266 
267 	/* switch the root pointer */
268 	asm volatile ("movec %0,%%urp" : : "r" (mm->context));
269 
270 	if (CPU_IS_060) {
271 		unsigned long tmp;
272 
273 		/* clear user entries in the branch cache */
274 		asm volatile (
275 			"movec %%cacr,%0; "
276 		        "orl %1,%0; "
277 		        "movec %0,%%cacr"
278 			: "=d" (tmp): "di" (0x00200000));
279 	}
280 
281 	asm volatile (".chip 68k");
282 }
283 
284 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk)
285 {
286 	if (prev != next) {
287 		if (CPU_IS_020_OR_030)
288 			switch_mm_0230(next);
289 		else
290 			switch_mm_0460(next);
291 	}
292 }
293 
294 #define deactivate_mm(tsk,mm)	do { } while (0)
295 
296 static inline void activate_mm(struct mm_struct *prev_mm,
297 			       struct mm_struct *next_mm)
298 {
299 	next_mm->context = virt_to_phys(next_mm->pgd);
300 
301 	if (CPU_IS_020_OR_030)
302 		switch_mm_0230(next_mm);
303 	else
304 		switch_mm_0460(next_mm);
305 }
306 
307 #endif
308 
309 #else /* !CONFIG_MMU */
310 
311 static inline int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
312 {
313 	return 0;
314 }
315 
316 
317 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk)
318 {
319 }
320 
321 #define destroy_context(mm)	do { } while (0)
322 #define deactivate_mm(tsk,mm)	do { } while (0)
323 
324 static inline void activate_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm)
325 {
326 }
327 
328 #endif /* CONFIG_MMU */
329 #endif /* __M68K_MMU_CONTEXT_H */
330