mmu_context.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/smp.h>
  16. #include <linux/slab.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/hazards.h>
  19. #include <asm/tlbflush.h>
  20. #ifdef CONFIG_MIPS_MT_SMTC
  21. #include <asm/mipsmtregs.h>
  22. #include <asm/smtc.h>
  23. #endif /* SMTC */
  24. #include <asm-generic/mm_hooks.h>
  25. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  26. #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
  27. do { \
  28. void (*tlbmiss_handler_setup_pgd)(unsigned long); \
  29. extern u32 tlbmiss_handler_setup_pgd_array[16]; \
  30. \
  31. tlbmiss_handler_setup_pgd = \
  32. (__typeof__(tlbmiss_handler_setup_pgd)) tlbmiss_handler_setup_pgd_array; \
  33. tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
  34. } while (0)
  35. #define TLBMISS_HANDLER_SETUP() \
  36. do { \
  37. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
  38. write_c0_xcontext((unsigned long) smp_processor_id() << 51); \
  39. } while (0)
  40. #else /* CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
  41. /*
  42. * For the fast tlb miss handlers, we keep a per cpu array of pointers
  43. * to the current pgd for each processor. Also, the proc. id is stuffed
  44. * into the context register.
  45. */
  46. extern unsigned long pgd_current[];
  47. #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
  48. pgd_current[smp_processor_id()] = (unsigned long)(pgd)
  49. #ifdef CONFIG_32BIT
  50. #define TLBMISS_HANDLER_SETUP() \
  51. write_c0_context((unsigned long) smp_processor_id() << 25); \
  52. back_to_back_c0_hazard(); \
  53. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  54. #endif
  55. #ifdef CONFIG_64BIT
  56. #define TLBMISS_HANDLER_SETUP() \
  57. write_c0_context((unsigned long) smp_processor_id() << 26); \
  58. back_to_back_c0_hazard(); \
  59. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  60. #endif
  61. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
  62. #define ASID_INC(asid) \
  63. ({ \
  64. unsigned long __asid = asid; \
  65. __asm__("1:\taddiu\t%0,1\t\t\t\t# patched\n\t" \
  66. ".section\t__asid_inc,\"a\"\n\t" \
  67. ".word\t1b\n\t" \
  68. ".previous" \
  69. :"=r" (__asid) \
  70. :"0" (__asid)); \
  71. __asid; \
  72. })
  73. #define ASID_MASK(asid) \
  74. ({ \
  75. unsigned long __asid = asid; \
  76. __asm__("1:\tandi\t%0,%1,0xfc0\t\t\t# patched\n\t" \
  77. ".section\t__asid_mask,\"a\"\n\t" \
  78. ".word\t1b\n\t" \
  79. ".previous" \
  80. :"=r" (__asid) \
  81. :"r" (__asid)); \
  82. __asid; \
  83. })
  84. #define ASID_VERSION_MASK \
  85. ({ \
  86. unsigned long __asid; \
  87. __asm__("1:\taddiu\t%0,$0,0xff00\t\t\t\t# patched\n\t" \
  88. ".section\t__asid_version_mask,\"a\"\n\t" \
  89. ".word\t1b\n\t" \
  90. ".previous" \
  91. :"=r" (__asid)); \
  92. __asid; \
  93. })
  94. #define ASID_FIRST_VERSION \
  95. ({ \
  96. unsigned long __asid = asid; \
  97. __asm__("1:\tli\t%0,0x100\t\t\t\t# patched\n\t" \
  98. ".section\t__asid_first_version,\"a\"\n\t" \
  99. ".word\t1b\n\t" \
  100. ".previous" \
  101. :"=r" (__asid)); \
  102. __asid; \
  103. })
  104. #define ASID_FIRST_VERSION_R3000 0x1000
  105. #define ASID_FIRST_VERSION_R4000 0x100
  106. #define ASID_FIRST_VERSION_R8000 0x1000
  107. #define ASID_FIRST_VERSION_RM9000 0x1000
  108. #ifdef CONFIG_MIPS_MT_SMTC
  109. #define SMTC_HW_ASID_MASK 0xff
  110. extern unsigned int smtc_asid_mask;
  111. #endif
  112. #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
  113. #define cpu_asid(cpu, mm) ASID_MASK(cpu_context((cpu), (mm)))
  114. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  115. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  116. {
  117. }
  118. #ifndef CONFIG_MIPS_MT_SMTC
  119. /* Normal, classic MIPS get_new_mmu_context */
  120. static inline void
  121. get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
  122. {
  123. extern void kvm_local_flush_tlb_all(void);
  124. unsigned long asid = asid_cache(cpu);
  125. if (!ASID_MASK((asid = ASID_INC(asid)))) {
  126. if (cpu_has_vtag_icache)
  127. flush_icache_all();
  128. #ifdef CONFIG_VIRTUALIZATION
  129. kvm_local_flush_tlb_all(); /* start new asid cycle */
  130. #else
  131. local_flush_tlb_all(); /* start new asid cycle */
  132. #endif
  133. if (!asid) /* fix version if needed */
  134. asid = ASID_FIRST_VERSION;
  135. }
  136. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  137. }
  138. #else /* CONFIG_MIPS_MT_SMTC */
  139. #define get_new_mmu_context(mm, cpu) smtc_get_new_mmu_context((mm), (cpu))
  140. #endif /* CONFIG_MIPS_MT_SMTC */
  141. /*
  142. * Initialize the context related info for a new mm_struct
  143. * instance.
  144. */
  145. static inline int
  146. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  147. {
  148. int i;
  149. for_each_possible_cpu(i)
  150. cpu_context(i, mm) = 0;
  151. return 0;
  152. }
  153. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  154. struct task_struct *tsk)
  155. {
  156. unsigned int cpu = smp_processor_id();
  157. unsigned long flags;
  158. #ifdef CONFIG_MIPS_MT_SMTC
  159. unsigned long oldasid;
  160. unsigned long mtflags;
  161. int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
  162. local_irq_save(flags);
  163. mtflags = dvpe();
  164. #else /* Not SMTC */
  165. local_irq_save(flags);
  166. #endif /* CONFIG_MIPS_MT_SMTC */
  167. /* Check if our ASID is of an older version and thus invalid */
  168. if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
  169. get_new_mmu_context(next, cpu);
  170. #ifdef CONFIG_MIPS_MT_SMTC
  171. /*
  172. * If the EntryHi ASID being replaced happens to be
  173. * the value flagged at ASID recycling time as having
  174. * an extended life, clear the bit showing it being
  175. * in use by this "CPU", and if that's the last bit,
  176. * free up the ASID value for use and flush any old
  177. * instances of it from the TLB.
  178. */
  179. oldasid = ASID_MASK(read_c0_entryhi());
  180. if(smtc_live_asid[mytlb][oldasid]) {
  181. smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
  182. if(smtc_live_asid[mytlb][oldasid] == 0)
  183. smtc_flush_tlb_asid(oldasid);
  184. }
  185. /*
  186. * Tread softly on EntryHi, and so long as we support
  187. * having ASID_MASK smaller than the hardware maximum,
  188. * make sure no "soft" bits become "hard"...
  189. */
  190. write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK) |
  191. cpu_asid(cpu, next));
  192. ehb(); /* Make sure it propagates to TCStatus */
  193. evpe(mtflags);
  194. #else
  195. write_c0_entryhi(cpu_asid(cpu, next));
  196. #endif /* CONFIG_MIPS_MT_SMTC */
  197. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  198. /*
  199. * Mark current->active_mm as not "active" anymore.
  200. * We don't want to mislead possible IPI tlb flush routines.
  201. */
  202. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  203. cpumask_set_cpu(cpu, mm_cpumask(next));
  204. local_irq_restore(flags);
  205. }
  206. /*
  207. * Destroy context related info for an mm_struct that is about
  208. * to be put to rest.
  209. */
  210. static inline void destroy_context(struct mm_struct *mm)
  211. {
  212. }
  213. #define deactivate_mm(tsk, mm) do { } while (0)
  214. /*
  215. * After we have set current->mm to a new value, this activates
  216. * the context for the new mm so we see the new mappings.
  217. */
  218. static inline void
  219. activate_mm(struct mm_struct *prev, struct mm_struct *next)
  220. {
  221. unsigned long flags;
  222. unsigned int cpu = smp_processor_id();
  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. local_irq_save(flags);
  229. /* Unconditionally get a new ASID. */
  230. get_new_mmu_context(next, cpu);
  231. #ifdef CONFIG_MIPS_MT_SMTC
  232. /* See comments for similar code above */
  233. mtflags = dvpe();
  234. oldasid = ASID_MASK(read_c0_entryhi());
  235. 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 */
  241. write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK) |
  242. cpu_asid(cpu, next));
  243. ehb(); /* Make sure it propagates to TCStatus */
  244. evpe(mtflags);
  245. #else
  246. write_c0_entryhi(cpu_asid(cpu, next));
  247. #endif /* CONFIG_MIPS_MT_SMTC */
  248. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  249. /* mark mmu ownership change */
  250. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  251. cpumask_set_cpu(cpu, mm_cpumask(next));
  252. local_irq_restore(flags);
  253. }
  254. /*
  255. * If mm is currently active_mm, we can't really drop it. Instead,
  256. * we will get a new one for it.
  257. */
  258. static inline void
  259. drop_mmu_context(struct mm_struct *mm, unsigned cpu)
  260. {
  261. unsigned long flags;
  262. #ifdef CONFIG_MIPS_MT_SMTC
  263. unsigned long oldasid;
  264. /* Can't use spinlock because called from TLB flush within DVPE */
  265. unsigned int prevvpe;
  266. int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
  267. #endif /* CONFIG_MIPS_MT_SMTC */
  268. local_irq_save(flags);
  269. if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
  270. get_new_mmu_context(mm, cpu);
  271. #ifdef CONFIG_MIPS_MT_SMTC
  272. /* See comments for similar code above */
  273. prevvpe = dvpe();
  274. oldasid = ASID_MASK(read_c0_entryhi());
  275. if (smtc_live_asid[mytlb][oldasid]) {
  276. smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
  277. if(smtc_live_asid[mytlb][oldasid] == 0)
  278. smtc_flush_tlb_asid(oldasid);
  279. }
  280. /* See comments for similar code above */
  281. write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK)
  282. | cpu_asid(cpu, mm));
  283. ehb(); /* Make sure it propagates to TCStatus */
  284. evpe(prevvpe);
  285. #else /* not CONFIG_MIPS_MT_SMTC */
  286. write_c0_entryhi(cpu_asid(cpu, mm));
  287. #endif /* CONFIG_MIPS_MT_SMTC */
  288. } else {
  289. /* will get a new context next time */
  290. #ifndef CONFIG_MIPS_MT_SMTC
  291. cpu_context(cpu, mm) = 0;
  292. #else /* SMTC */
  293. int i;
  294. /* SMTC shares the TLB (and ASIDs) across VPEs */
  295. for_each_online_cpu(i) {
  296. if((smtc_status & SMTC_TLB_SHARED)
  297. || (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
  298. cpu_context(i, mm) = 0;
  299. }
  300. #endif /* CONFIG_MIPS_MT_SMTC */
  301. }
  302. local_irq_restore(flags);
  303. }
  304. #endif /* _ASM_MMU_CONTEXT_H */