blob: 1554721e4808e7ffc67d61bc87646cbdd5a325be [file] [log] [blame]
Ralf Baechle384740d2008-09-16 19:48:51 +02001/*
2 * Switch a MMU context.
3 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details.
7 *
8 * Copyright (C) 1996, 1997, 1998, 1999 by Ralf Baechle
9 * Copyright (C) 1999 Silicon Graphics, Inc.
10 */
11#ifndef _ASM_MMU_CONTEXT_H
12#define _ASM_MMU_CONTEXT_H
13
14#include <linux/errno.h>
15#include <linux/sched.h>
Ralf Baechle631330f2009-06-19 14:05:26 +010016#include <linux/smp.h>
Ralf Baechle384740d2008-09-16 19:48:51 +020017#include <linux/slab.h>
18#include <asm/cacheflush.h>
Ralf Baechlec2ea1d52009-10-13 23:23:28 +020019#include <asm/hazards.h>
Ralf Baechle384740d2008-09-16 19:48:51 +020020#include <asm/tlbflush.h>
21#ifdef CONFIG_MIPS_MT_SMTC
22#include <asm/mipsmtregs.h>
23#include <asm/smtc.h>
24#endif /* SMTC */
25#include <asm-generic/mm_hooks.h>
26
David Daney82622282009-10-14 12:16:56 -070027#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
28
Ralf Baechle0bfbf6a2013-03-21 11:28:10 +010029#define TLBMISS_HANDLER_SETUP_PGD(pgd) \
30do { \
31 void (*tlbmiss_handler_setup_pgd)(unsigned long); \
32 extern u32 tlbmiss_handler_setup_pgd_array[16]; \
33 \
34 tlbmiss_handler_setup_pgd = \
35 (__typeof__(tlbmiss_handler_setup_pgd)) tlbmiss_handler_setup_pgd_array; \
36 tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
37} while (0)
David Daney82622282009-10-14 12:16:56 -070038
39#define TLBMISS_HANDLER_SETUP() \
40 do { \
41 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
42 write_c0_xcontext((unsigned long) smp_processor_id() << 51); \
43 } while (0)
44
David Daney82622282009-10-14 12:16:56 -070045#else /* CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
46
Ralf Baechle384740d2008-09-16 19:48:51 +020047/*
48 * For the fast tlb miss handlers, we keep a per cpu array of pointers
49 * to the current pgd for each processor. Also, the proc. id is stuffed
50 * into the context register.
51 */
52extern unsigned long pgd_current[];
53
54#define TLBMISS_HANDLER_SETUP_PGD(pgd) \
55 pgd_current[smp_processor_id()] = (unsigned long)(pgd)
56
57#ifdef CONFIG_32BIT
58#define TLBMISS_HANDLER_SETUP() \
59 write_c0_context((unsigned long) smp_processor_id() << 25); \
Ralf Baechlec2ea1d52009-10-13 23:23:28 +020060 back_to_back_c0_hazard(); \
Ralf Baechle384740d2008-09-16 19:48:51 +020061 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
62#endif
63#ifdef CONFIG_64BIT
64#define TLBMISS_HANDLER_SETUP() \
65 write_c0_context((unsigned long) smp_processor_id() << 26); \
Ralf Baechlec2ea1d52009-10-13 23:23:28 +020066 back_to_back_c0_hazard(); \
Ralf Baechle384740d2008-09-16 19:48:51 +020067 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
68#endif
David Daney82622282009-10-14 12:16:56 -070069#endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
Ralf Baechle384740d2008-09-16 19:48:51 +020070
Steven J. Hilld532f3d2013-03-25 11:58:57 -050071#define ASID_INC(asid) \
72({ \
73 unsigned long __asid = asid; \
74 __asm__("1:\taddiu\t%0,1\t\t\t\t# patched\n\t" \
75 ".section\t__asid_inc,\"a\"\n\t" \
76 ".word\t1b\n\t" \
77 ".previous" \
78 :"=r" (__asid) \
79 :"0" (__asid)); \
80 __asid; \
81})
82#define ASID_MASK(asid) \
83({ \
84 unsigned long __asid = asid; \
85 __asm__("1:\tandi\t%0,%1,0xfc0\t\t\t# patched\n\t" \
86 ".section\t__asid_mask,\"a\"\n\t" \
87 ".word\t1b\n\t" \
88 ".previous" \
89 :"=r" (__asid) \
90 :"r" (__asid)); \
91 __asid; \
92})
93#define ASID_VERSION_MASK \
94({ \
95 unsigned long __asid; \
96 __asm__("1:\taddiu\t%0,$0,0xff00\t\t\t\t# patched\n\t" \
97 ".section\t__asid_version_mask,\"a\"\n\t" \
98 ".word\t1b\n\t" \
99 ".previous" \
100 :"=r" (__asid)); \
101 __asid; \
102})
103#define ASID_FIRST_VERSION \
104({ \
105 unsigned long __asid = asid; \
106 __asm__("1:\tli\t%0,0x100\t\t\t\t# patched\n\t" \
107 ".section\t__asid_first_version,\"a\"\n\t" \
108 ".word\t1b\n\t" \
109 ".previous" \
110 :"=r" (__asid)); \
111 __asid; \
112})
Ralf Baechle384740d2008-09-16 19:48:51 +0200113
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500114#define ASID_FIRST_VERSION_R3000 0x1000
115#define ASID_FIRST_VERSION_R4000 0x100
116#define ASID_FIRST_VERSION_R8000 0x1000
117#define ASID_FIRST_VERSION_RM9000 0x1000
Ralf Baechle384740d2008-09-16 19:48:51 +0200118
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500119#ifdef CONFIG_MIPS_MT_SMTC
120#define SMTC_HW_ASID_MASK 0xff
121extern unsigned int smtc_asid_mask;
Ralf Baechle384740d2008-09-16 19:48:51 +0200122#endif
123
David Daneyc52d0d32010-02-18 16:13:04 -0800124#define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500125#define cpu_asid(cpu, mm) ASID_MASK(cpu_context((cpu), (mm)))
Ralf Baechle384740d2008-09-16 19:48:51 +0200126#define asid_cache(cpu) (cpu_data[cpu].asid_cache)
127
128static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
129{
130}
131
Ralf Baechle384740d2008-09-16 19:48:51 +0200132#ifndef CONFIG_MIPS_MT_SMTC
133/* Normal, classic MIPS get_new_mmu_context */
134static inline void
135get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
136{
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800137 extern void kvm_local_flush_tlb_all(void);
Ralf Baechle384740d2008-09-16 19:48:51 +0200138 unsigned long asid = asid_cache(cpu);
139
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500140 if (!ASID_MASK((asid = ASID_INC(asid)))) {
Ralf Baechle384740d2008-09-16 19:48:51 +0200141 if (cpu_has_vtag_icache)
142 flush_icache_all();
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800143#ifdef CONFIG_VIRTUALIZATION
144 kvm_local_flush_tlb_all(); /* start new asid cycle */
145#else
Ralf Baechle384740d2008-09-16 19:48:51 +0200146 local_flush_tlb_all(); /* start new asid cycle */
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800147#endif
Ralf Baechle384740d2008-09-16 19:48:51 +0200148 if (!asid) /* fix version if needed */
149 asid = ASID_FIRST_VERSION;
150 }
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800151
Ralf Baechle384740d2008-09-16 19:48:51 +0200152 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
153}
154
155#else /* CONFIG_MIPS_MT_SMTC */
156
157#define get_new_mmu_context(mm, cpu) smtc_get_new_mmu_context((mm), (cpu))
158
159#endif /* CONFIG_MIPS_MT_SMTC */
160
161/*
162 * Initialize the context related info for a new mm_struct
163 * instance.
164 */
165static inline int
166init_new_context(struct task_struct *tsk, struct mm_struct *mm)
167{
168 int i;
169
Huacai Chen22478672013-03-17 11:50:14 +0000170 for_each_possible_cpu(i)
Ralf Baechle384740d2008-09-16 19:48:51 +0200171 cpu_context(i, mm) = 0;
172
173 return 0;
174}
175
176static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
Ralf Baechle70342282013-01-22 12:59:30 +0100177 struct task_struct *tsk)
Ralf Baechle384740d2008-09-16 19:48:51 +0200178{
179 unsigned int cpu = smp_processor_id();
180 unsigned long flags;
181#ifdef CONFIG_MIPS_MT_SMTC
182 unsigned long oldasid;
183 unsigned long mtflags;
184 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
185 local_irq_save(flags);
186 mtflags = dvpe();
187#else /* Not SMTC */
188 local_irq_save(flags);
189#endif /* CONFIG_MIPS_MT_SMTC */
190
191 /* Check if our ASID is of an older version and thus invalid */
192 if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
193 get_new_mmu_context(next, cpu);
194#ifdef CONFIG_MIPS_MT_SMTC
195 /*
196 * If the EntryHi ASID being replaced happens to be
197 * the value flagged at ASID recycling time as having
198 * an extended life, clear the bit showing it being
199 * in use by this "CPU", and if that's the last bit,
200 * free up the ASID value for use and flush any old
201 * instances of it from the TLB.
202 */
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500203 oldasid = ASID_MASK(read_c0_entryhi());
Ralf Baechle384740d2008-09-16 19:48:51 +0200204 if(smtc_live_asid[mytlb][oldasid]) {
205 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
206 if(smtc_live_asid[mytlb][oldasid] == 0)
207 smtc_flush_tlb_asid(oldasid);
208 }
209 /*
210 * Tread softly on EntryHi, and so long as we support
211 * having ASID_MASK smaller than the hardware maximum,
212 * make sure no "soft" bits become "hard"...
213 */
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500214 write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK) |
Ralf Baechled30cecb2009-05-27 17:29:37 +0100215 cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200216 ehb(); /* Make sure it propagates to TCStatus */
217 evpe(mtflags);
218#else
Ralf Baechled30cecb2009-05-27 17:29:37 +0100219 write_c0_entryhi(cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200220#endif /* CONFIG_MIPS_MT_SMTC */
221 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
222
223 /*
224 * Mark current->active_mm as not "active" anymore.
225 * We don't want to mislead possible IPI tlb flush routines.
226 */
Rusty Russell55b8cab2009-09-24 09:34:50 -0600227 cpumask_clear_cpu(cpu, mm_cpumask(prev));
228 cpumask_set_cpu(cpu, mm_cpumask(next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200229
230 local_irq_restore(flags);
231}
232
233/*
234 * Destroy context related info for an mm_struct that is about
235 * to be put to rest.
236 */
237static inline void destroy_context(struct mm_struct *mm)
238{
239}
240
241#define deactivate_mm(tsk, mm) do { } while (0)
242
243/*
244 * After we have set current->mm to a new value, this activates
245 * the context for the new mm so we see the new mappings.
246 */
247static inline void
248activate_mm(struct mm_struct *prev, struct mm_struct *next)
249{
250 unsigned long flags;
251 unsigned int cpu = smp_processor_id();
252
253#ifdef CONFIG_MIPS_MT_SMTC
254 unsigned long oldasid;
255 unsigned long mtflags;
256 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
257#endif /* CONFIG_MIPS_MT_SMTC */
258
259 local_irq_save(flags);
260
261 /* Unconditionally get a new ASID. */
262 get_new_mmu_context(next, cpu);
263
264#ifdef CONFIG_MIPS_MT_SMTC
265 /* See comments for similar code above */
266 mtflags = dvpe();
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500267 oldasid = ASID_MASK(read_c0_entryhi());
Ralf Baechle384740d2008-09-16 19:48:51 +0200268 if(smtc_live_asid[mytlb][oldasid]) {
269 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
270 if(smtc_live_asid[mytlb][oldasid] == 0)
271 smtc_flush_tlb_asid(oldasid);
272 }
273 /* See comments for similar code above */
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500274 write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK) |
275 cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200276 ehb(); /* Make sure it propagates to TCStatus */
277 evpe(mtflags);
278#else
Ralf Baechled30cecb2009-05-27 17:29:37 +0100279 write_c0_entryhi(cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200280#endif /* CONFIG_MIPS_MT_SMTC */
281 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
282
283 /* mark mmu ownership change */
Rusty Russell55b8cab2009-09-24 09:34:50 -0600284 cpumask_clear_cpu(cpu, mm_cpumask(prev));
285 cpumask_set_cpu(cpu, mm_cpumask(next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200286
287 local_irq_restore(flags);
288}
289
290/*
291 * If mm is currently active_mm, we can't really drop it. Instead,
292 * we will get a new one for it.
293 */
294static inline void
295drop_mmu_context(struct mm_struct *mm, unsigned cpu)
296{
297 unsigned long flags;
298#ifdef CONFIG_MIPS_MT_SMTC
299 unsigned long oldasid;
300 /* Can't use spinlock because called from TLB flush within DVPE */
301 unsigned int prevvpe;
302 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
303#endif /* CONFIG_MIPS_MT_SMTC */
304
305 local_irq_save(flags);
306
Rusty Russell55b8cab2009-09-24 09:34:50 -0600307 if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
Ralf Baechle384740d2008-09-16 19:48:51 +0200308 get_new_mmu_context(mm, cpu);
309#ifdef CONFIG_MIPS_MT_SMTC
310 /* See comments for similar code above */
311 prevvpe = dvpe();
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500312 oldasid = ASID_MASK(read_c0_entryhi());
Ralf Baechle384740d2008-09-16 19:48:51 +0200313 if (smtc_live_asid[mytlb][oldasid]) {
314 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
315 if(smtc_live_asid[mytlb][oldasid] == 0)
316 smtc_flush_tlb_asid(oldasid);
317 }
318 /* See comments for similar code above */
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500319 write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK)
Ralf Baechle384740d2008-09-16 19:48:51 +0200320 | cpu_asid(cpu, mm));
321 ehb(); /* Make sure it propagates to TCStatus */
322 evpe(prevvpe);
323#else /* not CONFIG_MIPS_MT_SMTC */
324 write_c0_entryhi(cpu_asid(cpu, mm));
325#endif /* CONFIG_MIPS_MT_SMTC */
326 } else {
327 /* will get a new context next time */
328#ifndef CONFIG_MIPS_MT_SMTC
329 cpu_context(cpu, mm) = 0;
330#else /* SMTC */
331 int i;
332
333 /* SMTC shares the TLB (and ASIDs) across VPEs */
334 for_each_online_cpu(i) {
335 if((smtc_status & SMTC_TLB_SHARED)
336 || (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
337 cpu_context(i, mm) = 0;
338 }
339#endif /* CONFIG_MIPS_MT_SMTC */
340 }
341 local_irq_restore(flags);
342}
343
344#endif /* _ASM_MMU_CONTEXT_H */