blob: ecae1dc260fdfac745b9ad8ecb1c33df7de79322 [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
Ralf Baechle0bfbf6a2013-03-21 11:28:10 +010027#define TLBMISS_HANDLER_SETUP_PGD(pgd) \
28do { \
Jayachandran C6ba045f2013-06-23 17:16:19 +000029 extern void tlbmiss_handler_setup_pgd(unsigned long); \
Ralf Baechle0bfbf6a2013-03-21 11:28:10 +010030 tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
31} while (0)
David Daney82622282009-10-14 12:16:56 -070032
Jayachandran Cf4ae17a2013-09-25 16:28:04 +053033#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
James Hoganae4ce452014-03-04 10:20:43 +000034
35#define TLBMISS_HANDLER_RESTORE() \
36 write_c0_xcontext((unsigned long) smp_processor_id() << \
37 SMP_CPUID_REGSHIFT)
38
David Daney82622282009-10-14 12:16:56 -070039#define TLBMISS_HANDLER_SETUP() \
40 do { \
41 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
James Hoganae4ce452014-03-04 10:20:43 +000042 TLBMISS_HANDLER_RESTORE(); \
David Daney82622282009-10-14 12:16:56 -070043 } while (0)
44
Jayachandran Cc2377a42013-08-11 17:10:16 +053045#else /* !CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
David Daney82622282009-10-14 12:16:56 -070046
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
James Hoganae4ce452014-03-04 10:20:43 +000054#define TLBMISS_HANDLER_RESTORE() \
Jayachandran Cc2377a42013-08-11 17:10:16 +053055 write_c0_context((unsigned long) smp_processor_id() << \
James Hoganae4ce452014-03-04 10:20:43 +000056 SMP_CPUID_REGSHIFT)
57
58#define TLBMISS_HANDLER_SETUP() \
59 TLBMISS_HANDLER_RESTORE(); \
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)
David Daney82622282009-10-14 12:16:56 -070062#endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
David Daney48c4ac92013-05-13 13:56:44 -070063#if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
Ralf Baechle384740d2008-09-16 19:48:51 +020064
David Daney48c4ac92013-05-13 13:56:44 -070065#define ASID_INC 0x40
66#define ASID_MASK 0xfc0
Ralf Baechle384740d2008-09-16 19:48:51 +020067
David Daney48c4ac92013-05-13 13:56:44 -070068#elif defined(CONFIG_CPU_R8000)
Ralf Baechle384740d2008-09-16 19:48:51 +020069
David Daney48c4ac92013-05-13 13:56:44 -070070#define ASID_INC 0x10
71#define ASID_MASK 0xff0
72
73#elif defined(CONFIG_MIPS_MT_SMTC)
74
75#define ASID_INC 0x1
76extern unsigned long smtc_asid_mask;
77#define ASID_MASK (smtc_asid_mask)
78#define HW_ASID_MASK 0xff
79/* End SMTC/34K debug hack */
80#else /* FIXME: not correct for R6000 */
81
82#define ASID_INC 0x1
83#define ASID_MASK 0xff
84
Ralf Baechle384740d2008-09-16 19:48:51 +020085#endif
86
David Daneyc52d0d32010-02-18 16:13:04 -080087#define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
David Daney48c4ac92013-05-13 13:56:44 -070088#define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & ASID_MASK)
Ralf Baechle384740d2008-09-16 19:48:51 +020089#define asid_cache(cpu) (cpu_data[cpu].asid_cache)
90
91static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
92{
93}
94
David Daney48c4ac92013-05-13 13:56:44 -070095/*
96 * All unused by hardware upper bits will be considered
97 * as a software asid extension.
98 */
99#define ASID_VERSION_MASK ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
100#define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
101
Ralf Baechle384740d2008-09-16 19:48:51 +0200102#ifndef CONFIG_MIPS_MT_SMTC
103/* Normal, classic MIPS get_new_mmu_context */
104static inline void
105get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
106{
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800107 extern void kvm_local_flush_tlb_all(void);
Ralf Baechle384740d2008-09-16 19:48:51 +0200108 unsigned long asid = asid_cache(cpu);
109
David Daney48c4ac92013-05-13 13:56:44 -0700110 if (! ((asid += ASID_INC) & ASID_MASK) ) {
Ralf Baechle384740d2008-09-16 19:48:51 +0200111 if (cpu_has_vtag_icache)
112 flush_icache_all();
Markos Chandrasd4149762013-06-10 12:16:16 +0000113#ifdef CONFIG_KVM
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800114 kvm_local_flush_tlb_all(); /* start new asid cycle */
115#else
Ralf Baechle384740d2008-09-16 19:48:51 +0200116 local_flush_tlb_all(); /* start new asid cycle */
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800117#endif
Ralf Baechle384740d2008-09-16 19:48:51 +0200118 if (!asid) /* fix version if needed */
119 asid = ASID_FIRST_VERSION;
120 }
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800121
Ralf Baechle384740d2008-09-16 19:48:51 +0200122 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
123}
124
125#else /* CONFIG_MIPS_MT_SMTC */
126
127#define get_new_mmu_context(mm, cpu) smtc_get_new_mmu_context((mm), (cpu))
128
129#endif /* CONFIG_MIPS_MT_SMTC */
130
131/*
132 * Initialize the context related info for a new mm_struct
133 * instance.
134 */
135static inline int
136init_new_context(struct task_struct *tsk, struct mm_struct *mm)
137{
138 int i;
139
Huacai Chen22478672013-03-17 11:50:14 +0000140 for_each_possible_cpu(i)
Ralf Baechle384740d2008-09-16 19:48:51 +0200141 cpu_context(i, mm) = 0;
142
143 return 0;
144}
145
146static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
Ralf Baechle70342282013-01-22 12:59:30 +0100147 struct task_struct *tsk)
Ralf Baechle384740d2008-09-16 19:48:51 +0200148{
149 unsigned int cpu = smp_processor_id();
150 unsigned long flags;
151#ifdef CONFIG_MIPS_MT_SMTC
152 unsigned long oldasid;
153 unsigned long mtflags;
154 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
155 local_irq_save(flags);
156 mtflags = dvpe();
157#else /* Not SMTC */
158 local_irq_save(flags);
159#endif /* CONFIG_MIPS_MT_SMTC */
160
161 /* Check if our ASID is of an older version and thus invalid */
162 if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
163 get_new_mmu_context(next, cpu);
164#ifdef CONFIG_MIPS_MT_SMTC
165 /*
166 * If the EntryHi ASID being replaced happens to be
167 * the value flagged at ASID recycling time as having
168 * an extended life, clear the bit showing it being
169 * in use by this "CPU", and if that's the last bit,
170 * free up the ASID value for use and flush any old
171 * instances of it from the TLB.
172 */
David Daney48c4ac92013-05-13 13:56:44 -0700173 oldasid = (read_c0_entryhi() & ASID_MASK);
Ralf Baechle384740d2008-09-16 19:48:51 +0200174 if(smtc_live_asid[mytlb][oldasid]) {
175 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
176 if(smtc_live_asid[mytlb][oldasid] == 0)
177 smtc_flush_tlb_asid(oldasid);
178 }
179 /*
180 * Tread softly on EntryHi, and so long as we support
181 * having ASID_MASK smaller than the hardware maximum,
182 * make sure no "soft" bits become "hard"...
183 */
David Daney48c4ac92013-05-13 13:56:44 -0700184 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
Ralf Baechled30cecb2009-05-27 17:29:37 +0100185 cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200186 ehb(); /* Make sure it propagates to TCStatus */
187 evpe(mtflags);
188#else
Ralf Baechled30cecb2009-05-27 17:29:37 +0100189 write_c0_entryhi(cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200190#endif /* CONFIG_MIPS_MT_SMTC */
191 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
192
193 /*
194 * Mark current->active_mm as not "active" anymore.
195 * We don't want to mislead possible IPI tlb flush routines.
196 */
Rusty Russell55b8cab2009-09-24 09:34:50 -0600197 cpumask_clear_cpu(cpu, mm_cpumask(prev));
198 cpumask_set_cpu(cpu, mm_cpumask(next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200199
200 local_irq_restore(flags);
201}
202
203/*
204 * Destroy context related info for an mm_struct that is about
205 * to be put to rest.
206 */
207static inline void destroy_context(struct mm_struct *mm)
208{
209}
210
211#define deactivate_mm(tsk, mm) do { } while (0)
212
213/*
214 * After we have set current->mm to a new value, this activates
215 * the context for the new mm so we see the new mappings.
216 */
217static inline void
218activate_mm(struct mm_struct *prev, struct mm_struct *next)
219{
220 unsigned long flags;
221 unsigned int cpu = smp_processor_id();
222
223#ifdef CONFIG_MIPS_MT_SMTC
224 unsigned long oldasid;
225 unsigned long mtflags;
226 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
227#endif /* CONFIG_MIPS_MT_SMTC */
228
229 local_irq_save(flags);
230
231 /* Unconditionally get a new ASID. */
232 get_new_mmu_context(next, cpu);
233
234#ifdef CONFIG_MIPS_MT_SMTC
235 /* See comments for similar code above */
236 mtflags = dvpe();
David Daney48c4ac92013-05-13 13:56:44 -0700237 oldasid = read_c0_entryhi() & ASID_MASK;
Ralf Baechle384740d2008-09-16 19:48:51 +0200238 if(smtc_live_asid[mytlb][oldasid]) {
239 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
240 if(smtc_live_asid[mytlb][oldasid] == 0)
241 smtc_flush_tlb_asid(oldasid);
242 }
243 /* See comments for similar code above */
David Daney48c4ac92013-05-13 13:56:44 -0700244 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
245 cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200246 ehb(); /* Make sure it propagates to TCStatus */
247 evpe(mtflags);
248#else
Ralf Baechled30cecb2009-05-27 17:29:37 +0100249 write_c0_entryhi(cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200250#endif /* CONFIG_MIPS_MT_SMTC */
251 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
252
253 /* mark mmu ownership change */
Rusty Russell55b8cab2009-09-24 09:34:50 -0600254 cpumask_clear_cpu(cpu, mm_cpumask(prev));
255 cpumask_set_cpu(cpu, mm_cpumask(next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200256
257 local_irq_restore(flags);
258}
259
260/*
261 * If mm is currently active_mm, we can't really drop it. Instead,
262 * we will get a new one for it.
263 */
264static inline void
265drop_mmu_context(struct mm_struct *mm, unsigned cpu)
266{
267 unsigned long flags;
268#ifdef CONFIG_MIPS_MT_SMTC
269 unsigned long oldasid;
270 /* Can't use spinlock because called from TLB flush within DVPE */
271 unsigned int prevvpe;
272 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
273#endif /* CONFIG_MIPS_MT_SMTC */
274
275 local_irq_save(flags);
276
Rusty Russell55b8cab2009-09-24 09:34:50 -0600277 if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
Ralf Baechle384740d2008-09-16 19:48:51 +0200278 get_new_mmu_context(mm, cpu);
279#ifdef CONFIG_MIPS_MT_SMTC
280 /* See comments for similar code above */
281 prevvpe = dvpe();
David Daney48c4ac92013-05-13 13:56:44 -0700282 oldasid = (read_c0_entryhi() & ASID_MASK);
Ralf Baechle384740d2008-09-16 19:48:51 +0200283 if (smtc_live_asid[mytlb][oldasid]) {
284 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
285 if(smtc_live_asid[mytlb][oldasid] == 0)
286 smtc_flush_tlb_asid(oldasid);
287 }
288 /* See comments for similar code above */
David Daney48c4ac92013-05-13 13:56:44 -0700289 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK)
Ralf Baechle384740d2008-09-16 19:48:51 +0200290 | cpu_asid(cpu, mm));
291 ehb(); /* Make sure it propagates to TCStatus */
292 evpe(prevvpe);
293#else /* not CONFIG_MIPS_MT_SMTC */
294 write_c0_entryhi(cpu_asid(cpu, mm));
295#endif /* CONFIG_MIPS_MT_SMTC */
296 } else {
297 /* will get a new context next time */
298#ifndef CONFIG_MIPS_MT_SMTC
299 cpu_context(cpu, mm) = 0;
300#else /* SMTC */
301 int i;
302
303 /* SMTC shares the TLB (and ASIDs) across VPEs */
304 for_each_online_cpu(i) {
305 if((smtc_status & SMTC_TLB_SHARED)
306 || (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
307 cpu_context(i, mm) = 0;
308 }
309#endif /* CONFIG_MIPS_MT_SMTC */
310 }
311 local_irq_restore(flags);
312}
313
314#endif /* _ASM_MMU_CONTEXT_H */