Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 1 | /* |
| 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 Baechle | 631330f | 2009-06-19 14:05:26 +0100 | [diff] [blame] | 16 | #include <linux/smp.h> |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <asm/cacheflush.h> |
Ralf Baechle | c2ea1d5 | 2009-10-13 23:23:28 +0200 | [diff] [blame] | 19 | #include <asm/hazards.h> |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 20 | #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 Daney | 8262228 | 2009-10-14 12:16:56 -0700 | [diff] [blame] | 27 | #ifdef CONFIG_MIPS_PGD_C0_CONTEXT |
| 28 | |
Ralf Baechle | 0bfbf6a | 2013-03-21 11:28:10 +0100 | [diff] [blame] | 29 | #define TLBMISS_HANDLER_SETUP_PGD(pgd) \ |
| 30 | do { \ |
| 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 Daney | 8262228 | 2009-10-14 12:16:56 -0700 | [diff] [blame] | 38 | |
| 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 Daney | 8262228 | 2009-10-14 12:16:56 -0700 | [diff] [blame] | 45 | #else /* CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/ |
| 46 | |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 47 | /* |
| 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 | */ |
| 52 | extern 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 Baechle | c2ea1d5 | 2009-10-13 23:23:28 +0200 | [diff] [blame] | 60 | back_to_back_c0_hazard(); \ |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 61 | 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 Baechle | c2ea1d5 | 2009-10-13 23:23:28 +0200 | [diff] [blame] | 66 | back_to_back_c0_hazard(); \ |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 67 | TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir) |
| 68 | #endif |
David Daney | 8262228 | 2009-10-14 12:16:56 -0700 | [diff] [blame] | 69 | #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/ |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 70 | |
Steven J. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 71 | #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 Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 113 | |
Steven J. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 114 | #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 Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 118 | |
Steven J. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 119 | #ifdef CONFIG_MIPS_MT_SMTC |
| 120 | #define SMTC_HW_ASID_MASK 0xff |
| 121 | extern unsigned int smtc_asid_mask; |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 122 | #endif |
| 123 | |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 124 | #define cpu_context(cpu, mm) ((mm)->context.asid[cpu]) |
Steven J. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 125 | #define cpu_asid(cpu, mm) ASID_MASK(cpu_context((cpu), (mm))) |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 126 | #define asid_cache(cpu) (cpu_data[cpu].asid_cache) |
| 127 | |
| 128 | static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) |
| 129 | { |
| 130 | } |
| 131 | |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 132 | #ifndef CONFIG_MIPS_MT_SMTC |
| 133 | /* Normal, classic MIPS get_new_mmu_context */ |
| 134 | static inline void |
| 135 | get_new_mmu_context(struct mm_struct *mm, unsigned long cpu) |
| 136 | { |
Sanjay Lal | f9afbd4 | 2012-11-21 18:34:11 -0800 | [diff] [blame] | 137 | extern void kvm_local_flush_tlb_all(void); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 138 | unsigned long asid = asid_cache(cpu); |
| 139 | |
Steven J. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 140 | if (!ASID_MASK((asid = ASID_INC(asid)))) { |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 141 | if (cpu_has_vtag_icache) |
| 142 | flush_icache_all(); |
Sanjay Lal | f9afbd4 | 2012-11-21 18:34:11 -0800 | [diff] [blame] | 143 | #ifdef CONFIG_VIRTUALIZATION |
| 144 | kvm_local_flush_tlb_all(); /* start new asid cycle */ |
| 145 | #else |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 146 | local_flush_tlb_all(); /* start new asid cycle */ |
Sanjay Lal | f9afbd4 | 2012-11-21 18:34:11 -0800 | [diff] [blame] | 147 | #endif |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 148 | if (!asid) /* fix version if needed */ |
| 149 | asid = ASID_FIRST_VERSION; |
| 150 | } |
Sanjay Lal | f9afbd4 | 2012-11-21 18:34:11 -0800 | [diff] [blame] | 151 | |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 152 | 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 | */ |
| 165 | static inline int |
| 166 | init_new_context(struct task_struct *tsk, struct mm_struct *mm) |
| 167 | { |
| 168 | int i; |
| 169 | |
Huacai Chen | 2247867 | 2013-03-17 11:50:14 +0000 | [diff] [blame] | 170 | for_each_possible_cpu(i) |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 171 | cpu_context(i, mm) = 0; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 177 | struct task_struct *tsk) |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 178 | { |
| 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. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 203 | oldasid = ASID_MASK(read_c0_entryhi()); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 204 | 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. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 214 | write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK) | |
Ralf Baechle | d30cecb | 2009-05-27 17:29:37 +0100 | [diff] [blame] | 215 | cpu_asid(cpu, next)); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 216 | ehb(); /* Make sure it propagates to TCStatus */ |
| 217 | evpe(mtflags); |
| 218 | #else |
Ralf Baechle | d30cecb | 2009-05-27 17:29:37 +0100 | [diff] [blame] | 219 | write_c0_entryhi(cpu_asid(cpu, next)); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 220 | #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 Russell | 55b8cab | 2009-09-24 09:34:50 -0600 | [diff] [blame] | 227 | cpumask_clear_cpu(cpu, mm_cpumask(prev)); |
| 228 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 229 | |
| 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 | */ |
| 237 | static 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 | */ |
| 247 | static inline void |
| 248 | activate_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. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 267 | oldasid = ASID_MASK(read_c0_entryhi()); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 268 | 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. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 274 | write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK) | |
| 275 | cpu_asid(cpu, next)); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 276 | ehb(); /* Make sure it propagates to TCStatus */ |
| 277 | evpe(mtflags); |
| 278 | #else |
Ralf Baechle | d30cecb | 2009-05-27 17:29:37 +0100 | [diff] [blame] | 279 | write_c0_entryhi(cpu_asid(cpu, next)); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 280 | #endif /* CONFIG_MIPS_MT_SMTC */ |
| 281 | TLBMISS_HANDLER_SETUP_PGD(next->pgd); |
| 282 | |
| 283 | /* mark mmu ownership change */ |
Rusty Russell | 55b8cab | 2009-09-24 09:34:50 -0600 | [diff] [blame] | 284 | cpumask_clear_cpu(cpu, mm_cpumask(prev)); |
| 285 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 286 | |
| 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 | */ |
| 294 | static inline void |
| 295 | drop_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 Russell | 55b8cab | 2009-09-24 09:34:50 -0600 | [diff] [blame] | 307 | if (cpumask_test_cpu(cpu, mm_cpumask(mm))) { |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 308 | get_new_mmu_context(mm, cpu); |
| 309 | #ifdef CONFIG_MIPS_MT_SMTC |
| 310 | /* See comments for similar code above */ |
| 311 | prevvpe = dvpe(); |
Steven J. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 312 | oldasid = ASID_MASK(read_c0_entryhi()); |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 313 | 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. Hill | d532f3d | 2013-03-25 11:58:57 -0500 | [diff] [blame] | 319 | write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK) |
Ralf Baechle | 384740d | 2008-09-16 19:48:51 +0200 | [diff] [blame] | 320 | | 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 */ |