blob: ab8e26051ed7f376513678de8c18aa7119f9cd90 [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 { \
Jayachandran C6ba045f2013-06-23 17:16:19 +000031 extern void tlbmiss_handler_setup_pgd(unsigned long); \
Ralf Baechle0bfbf6a2013-03-21 11:28:10 +010032 tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
33} while (0)
David Daney82622282009-10-14 12:16:56 -070034
35#define TLBMISS_HANDLER_SETUP() \
36 do { \
37 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
Jayachandran Cc2377a42013-08-11 17:10:16 +053038 write_c0_xcontext((unsigned long) smp_processor_id() << \
39 SMP_CPUID_REGSHIFT); \
David Daney82622282009-10-14 12:16:56 -070040 } while (0)
41
Jayachandran Cc2377a42013-08-11 17:10:16 +053042#else /* !CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
David Daney82622282009-10-14 12:16:56 -070043
Ralf Baechle384740d2008-09-16 19:48:51 +020044/*
45 * For the fast tlb miss handlers, we keep a per cpu array of pointers
46 * to the current pgd for each processor. Also, the proc. id is stuffed
47 * into the context register.
48 */
49extern unsigned long pgd_current[];
50
51#define TLBMISS_HANDLER_SETUP_PGD(pgd) \
52 pgd_current[smp_processor_id()] = (unsigned long)(pgd)
53
Ralf Baechle384740d2008-09-16 19:48:51 +020054#define TLBMISS_HANDLER_SETUP() \
Jayachandran Cc2377a42013-08-11 17:10:16 +053055 write_c0_context((unsigned long) smp_processor_id() << \
56 SMP_CPUID_REGSHIFT); \
Ralf Baechlec2ea1d52009-10-13 23:23:28 +020057 back_to_back_c0_hazard(); \
Ralf Baechle384740d2008-09-16 19:48:51 +020058 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
David Daney82622282009-10-14 12:16:56 -070059#endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
David Daney48c4ac92013-05-13 13:56:44 -070060#if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
Ralf Baechle384740d2008-09-16 19:48:51 +020061
David Daney48c4ac92013-05-13 13:56:44 -070062#define ASID_INC 0x40
63#define ASID_MASK 0xfc0
Ralf Baechle384740d2008-09-16 19:48:51 +020064
David Daney48c4ac92013-05-13 13:56:44 -070065#elif defined(CONFIG_CPU_R8000)
Ralf Baechle384740d2008-09-16 19:48:51 +020066
David Daney48c4ac92013-05-13 13:56:44 -070067#define ASID_INC 0x10
68#define ASID_MASK 0xff0
69
70#elif defined(CONFIG_MIPS_MT_SMTC)
71
72#define ASID_INC 0x1
73extern unsigned long smtc_asid_mask;
74#define ASID_MASK (smtc_asid_mask)
75#define HW_ASID_MASK 0xff
76/* End SMTC/34K debug hack */
77#else /* FIXME: not correct for R6000 */
78
79#define ASID_INC 0x1
80#define ASID_MASK 0xff
81
Ralf Baechle384740d2008-09-16 19:48:51 +020082#endif
83
David Daneyc52d0d32010-02-18 16:13:04 -080084#define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
David Daney48c4ac92013-05-13 13:56:44 -070085#define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & ASID_MASK)
Ralf Baechle384740d2008-09-16 19:48:51 +020086#define asid_cache(cpu) (cpu_data[cpu].asid_cache)
87
88static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
89{
90}
91
David Daney48c4ac92013-05-13 13:56:44 -070092/*
93 * All unused by hardware upper bits will be considered
94 * as a software asid extension.
95 */
96#define ASID_VERSION_MASK ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
97#define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
98
Ralf Baechle384740d2008-09-16 19:48:51 +020099#ifndef CONFIG_MIPS_MT_SMTC
100/* Normal, classic MIPS get_new_mmu_context */
101static inline void
102get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
103{
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800104 extern void kvm_local_flush_tlb_all(void);
Ralf Baechle384740d2008-09-16 19:48:51 +0200105 unsigned long asid = asid_cache(cpu);
106
David Daney48c4ac92013-05-13 13:56:44 -0700107 if (! ((asid += ASID_INC) & ASID_MASK) ) {
Ralf Baechle384740d2008-09-16 19:48:51 +0200108 if (cpu_has_vtag_icache)
109 flush_icache_all();
Markos Chandrasd4149762013-06-10 12:16:16 +0000110#ifdef CONFIG_KVM
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800111 kvm_local_flush_tlb_all(); /* start new asid cycle */
112#else
Ralf Baechle384740d2008-09-16 19:48:51 +0200113 local_flush_tlb_all(); /* start new asid cycle */
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800114#endif
Ralf Baechle384740d2008-09-16 19:48:51 +0200115 if (!asid) /* fix version if needed */
116 asid = ASID_FIRST_VERSION;
117 }
Sanjay Lalf9afbd42012-11-21 18:34:11 -0800118
Ralf Baechle384740d2008-09-16 19:48:51 +0200119 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
120}
121
122#else /* CONFIG_MIPS_MT_SMTC */
123
124#define get_new_mmu_context(mm, cpu) smtc_get_new_mmu_context((mm), (cpu))
125
126#endif /* CONFIG_MIPS_MT_SMTC */
127
128/*
129 * Initialize the context related info for a new mm_struct
130 * instance.
131 */
132static inline int
133init_new_context(struct task_struct *tsk, struct mm_struct *mm)
134{
135 int i;
136
Huacai Chen22478672013-03-17 11:50:14 +0000137 for_each_possible_cpu(i)
Ralf Baechle384740d2008-09-16 19:48:51 +0200138 cpu_context(i, mm) = 0;
139
140 return 0;
141}
142
143static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
Ralf Baechle70342282013-01-22 12:59:30 +0100144 struct task_struct *tsk)
Ralf Baechle384740d2008-09-16 19:48:51 +0200145{
146 unsigned int cpu = smp_processor_id();
147 unsigned long flags;
148#ifdef CONFIG_MIPS_MT_SMTC
149 unsigned long oldasid;
150 unsigned long mtflags;
151 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
152 local_irq_save(flags);
153 mtflags = dvpe();
154#else /* Not SMTC */
155 local_irq_save(flags);
156#endif /* CONFIG_MIPS_MT_SMTC */
157
158 /* Check if our ASID is of an older version and thus invalid */
159 if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
160 get_new_mmu_context(next, cpu);
161#ifdef CONFIG_MIPS_MT_SMTC
162 /*
163 * If the EntryHi ASID being replaced happens to be
164 * the value flagged at ASID recycling time as having
165 * an extended life, clear the bit showing it being
166 * in use by this "CPU", and if that's the last bit,
167 * free up the ASID value for use and flush any old
168 * instances of it from the TLB.
169 */
David Daney48c4ac92013-05-13 13:56:44 -0700170 oldasid = (read_c0_entryhi() & ASID_MASK);
Ralf Baechle384740d2008-09-16 19:48:51 +0200171 if(smtc_live_asid[mytlb][oldasid]) {
172 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
173 if(smtc_live_asid[mytlb][oldasid] == 0)
174 smtc_flush_tlb_asid(oldasid);
175 }
176 /*
177 * Tread softly on EntryHi, and so long as we support
178 * having ASID_MASK smaller than the hardware maximum,
179 * make sure no "soft" bits become "hard"...
180 */
David Daney48c4ac92013-05-13 13:56:44 -0700181 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
Ralf Baechled30cecb2009-05-27 17:29:37 +0100182 cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200183 ehb(); /* Make sure it propagates to TCStatus */
184 evpe(mtflags);
185#else
Ralf Baechled30cecb2009-05-27 17:29:37 +0100186 write_c0_entryhi(cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200187#endif /* CONFIG_MIPS_MT_SMTC */
188 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
189
190 /*
191 * Mark current->active_mm as not "active" anymore.
192 * We don't want to mislead possible IPI tlb flush routines.
193 */
Rusty Russell55b8cab2009-09-24 09:34:50 -0600194 cpumask_clear_cpu(cpu, mm_cpumask(prev));
195 cpumask_set_cpu(cpu, mm_cpumask(next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200196
197 local_irq_restore(flags);
198}
199
200/*
201 * Destroy context related info for an mm_struct that is about
202 * to be put to rest.
203 */
204static inline void destroy_context(struct mm_struct *mm)
205{
206}
207
208#define deactivate_mm(tsk, mm) do { } while (0)
209
210/*
211 * After we have set current->mm to a new value, this activates
212 * the context for the new mm so we see the new mappings.
213 */
214static inline void
215activate_mm(struct mm_struct *prev, struct mm_struct *next)
216{
217 unsigned long flags;
218 unsigned int cpu = smp_processor_id();
219
220#ifdef CONFIG_MIPS_MT_SMTC
221 unsigned long oldasid;
222 unsigned long mtflags;
223 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
224#endif /* CONFIG_MIPS_MT_SMTC */
225
226 local_irq_save(flags);
227
228 /* Unconditionally get a new ASID. */
229 get_new_mmu_context(next, cpu);
230
231#ifdef CONFIG_MIPS_MT_SMTC
232 /* See comments for similar code above */
233 mtflags = dvpe();
David Daney48c4ac92013-05-13 13:56:44 -0700234 oldasid = read_c0_entryhi() & ASID_MASK;
Ralf Baechle384740d2008-09-16 19:48:51 +0200235 if(smtc_live_asid[mytlb][oldasid]) {
236 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
237 if(smtc_live_asid[mytlb][oldasid] == 0)
238 smtc_flush_tlb_asid(oldasid);
239 }
240 /* See comments for similar code above */
David Daney48c4ac92013-05-13 13:56:44 -0700241 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
242 cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200243 ehb(); /* Make sure it propagates to TCStatus */
244 evpe(mtflags);
245#else
Ralf Baechled30cecb2009-05-27 17:29:37 +0100246 write_c0_entryhi(cpu_asid(cpu, next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200247#endif /* CONFIG_MIPS_MT_SMTC */
248 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
249
250 /* mark mmu ownership change */
Rusty Russell55b8cab2009-09-24 09:34:50 -0600251 cpumask_clear_cpu(cpu, mm_cpumask(prev));
252 cpumask_set_cpu(cpu, mm_cpumask(next));
Ralf Baechle384740d2008-09-16 19:48:51 +0200253
254 local_irq_restore(flags);
255}
256
257/*
258 * If mm is currently active_mm, we can't really drop it. Instead,
259 * we will get a new one for it.
260 */
261static inline void
262drop_mmu_context(struct mm_struct *mm, unsigned cpu)
263{
264 unsigned long flags;
265#ifdef CONFIG_MIPS_MT_SMTC
266 unsigned long oldasid;
267 /* Can't use spinlock because called from TLB flush within DVPE */
268 unsigned int prevvpe;
269 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
270#endif /* CONFIG_MIPS_MT_SMTC */
271
272 local_irq_save(flags);
273
Rusty Russell55b8cab2009-09-24 09:34:50 -0600274 if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
Ralf Baechle384740d2008-09-16 19:48:51 +0200275 get_new_mmu_context(mm, cpu);
276#ifdef CONFIG_MIPS_MT_SMTC
277 /* See comments for similar code above */
278 prevvpe = dvpe();
David Daney48c4ac92013-05-13 13:56:44 -0700279 oldasid = (read_c0_entryhi() & ASID_MASK);
Ralf Baechle384740d2008-09-16 19:48:51 +0200280 if (smtc_live_asid[mytlb][oldasid]) {
281 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
282 if(smtc_live_asid[mytlb][oldasid] == 0)
283 smtc_flush_tlb_asid(oldasid);
284 }
285 /* See comments for similar code above */
David Daney48c4ac92013-05-13 13:56:44 -0700286 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK)
Ralf Baechle384740d2008-09-16 19:48:51 +0200287 | cpu_asid(cpu, mm));
288 ehb(); /* Make sure it propagates to TCStatus */
289 evpe(prevvpe);
290#else /* not CONFIG_MIPS_MT_SMTC */
291 write_c0_entryhi(cpu_asid(cpu, mm));
292#endif /* CONFIG_MIPS_MT_SMTC */
293 } else {
294 /* will get a new context next time */
295#ifndef CONFIG_MIPS_MT_SMTC
296 cpu_context(cpu, mm) = 0;
297#else /* SMTC */
298 int i;
299
300 /* SMTC shares the TLB (and ASIDs) across VPEs */
301 for_each_online_cpu(i) {
302 if((smtc_status & SMTC_TLB_SHARED)
303 || (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
304 cpu_context(i, mm) = 0;
305 }
306#endif /* CONFIG_MIPS_MT_SMTC */
307 }
308 local_irq_restore(flags);
309}
310
311#endif /* _ASM_MMU_CONTEXT_H */