mmu_context.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. extern void tlbmiss_handler_setup_pgd(unsigned long); \
  29. tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
  30. } while (0)
  31. #define TLBMISS_HANDLER_SETUP() \
  32. do { \
  33. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
  34. write_c0_xcontext((unsigned long) smp_processor_id() << \
  35. SMP_CPUID_REGSHIFT); \
  36. } while (0)
  37. #else /* !CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
  38. /*
  39. * For the fast tlb miss handlers, we keep a per cpu array of pointers
  40. * to the current pgd for each processor. Also, the proc. id is stuffed
  41. * into the context register.
  42. */
  43. extern unsigned long pgd_current[];
  44. #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
  45. pgd_current[smp_processor_id()] = (unsigned long)(pgd)
  46. #define TLBMISS_HANDLER_SETUP() \
  47. write_c0_context((unsigned long) smp_processor_id() << \
  48. SMP_CPUID_REGSHIFT); \
  49. back_to_back_c0_hazard(); \
  50. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  51. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
  52. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  53. #define ASID_INC 0x40
  54. #define ASID_MASK 0xfc0
  55. #elif defined(CONFIG_CPU_R8000)
  56. #define ASID_INC 0x10
  57. #define ASID_MASK 0xff0
  58. #elif defined(CONFIG_MIPS_MT_SMTC)
  59. #define ASID_INC 0x1
  60. extern unsigned long smtc_asid_mask;
  61. #define ASID_MASK (smtc_asid_mask)
  62. #define HW_ASID_MASK 0xff
  63. /* End SMTC/34K debug hack */
  64. #else /* FIXME: not correct for R6000 */
  65. #define ASID_INC 0x1
  66. #define ASID_MASK 0xff
  67. #endif
  68. #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
  69. #define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & ASID_MASK)
  70. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  71. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  72. {
  73. }
  74. /*
  75. * All unused by hardware upper bits will be considered
  76. * as a software asid extension.
  77. */
  78. #define ASID_VERSION_MASK ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
  79. #define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
  80. #ifndef CONFIG_MIPS_MT_SMTC
  81. /* Normal, classic MIPS get_new_mmu_context */
  82. static inline void
  83. get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
  84. {
  85. extern void kvm_local_flush_tlb_all(void);
  86. unsigned long asid = asid_cache(cpu);
  87. if (! ((asid += ASID_INC) & ASID_MASK) ) {
  88. if (cpu_has_vtag_icache)
  89. flush_icache_all();
  90. #ifdef CONFIG_KVM
  91. kvm_local_flush_tlb_all(); /* start new asid cycle */
  92. #else
  93. local_flush_tlb_all(); /* start new asid cycle */
  94. #endif
  95. if (!asid) /* fix version if needed */
  96. asid = ASID_FIRST_VERSION;
  97. }
  98. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  99. }
  100. #else /* CONFIG_MIPS_MT_SMTC */
  101. #define get_new_mmu_context(mm, cpu) smtc_get_new_mmu_context((mm), (cpu))
  102. #endif /* CONFIG_MIPS_MT_SMTC */
  103. /*
  104. * Initialize the context related info for a new mm_struct
  105. * instance.
  106. */
  107. static inline int
  108. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  109. {
  110. int i;
  111. for_each_possible_cpu(i)
  112. cpu_context(i, mm) = 0;
  113. return 0;
  114. }
  115. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  116. struct task_struct *tsk)
  117. {
  118. unsigned int cpu = smp_processor_id();
  119. unsigned long flags;
  120. #ifdef CONFIG_MIPS_MT_SMTC
  121. unsigned long oldasid;
  122. unsigned long mtflags;
  123. int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
  124. local_irq_save(flags);
  125. mtflags = dvpe();
  126. #else /* Not SMTC */
  127. local_irq_save(flags);
  128. #endif /* CONFIG_MIPS_MT_SMTC */
  129. /* Check if our ASID is of an older version and thus invalid */
  130. if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
  131. get_new_mmu_context(next, cpu);
  132. #ifdef CONFIG_MIPS_MT_SMTC
  133. /*
  134. * If the EntryHi ASID being replaced happens to be
  135. * the value flagged at ASID recycling time as having
  136. * an extended life, clear the bit showing it being
  137. * in use by this "CPU", and if that's the last bit,
  138. * free up the ASID value for use and flush any old
  139. * instances of it from the TLB.
  140. */
  141. oldasid = (read_c0_entryhi() & ASID_MASK);
  142. if(smtc_live_asid[mytlb][oldasid]) {
  143. smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
  144. if(smtc_live_asid[mytlb][oldasid] == 0)
  145. smtc_flush_tlb_asid(oldasid);
  146. }
  147. /*
  148. * Tread softly on EntryHi, and so long as we support
  149. * having ASID_MASK smaller than the hardware maximum,
  150. * make sure no "soft" bits become "hard"...
  151. */
  152. write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
  153. cpu_asid(cpu, next));
  154. ehb(); /* Make sure it propagates to TCStatus */
  155. evpe(mtflags);
  156. #else
  157. write_c0_entryhi(cpu_asid(cpu, next));
  158. #endif /* CONFIG_MIPS_MT_SMTC */
  159. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  160. /*
  161. * Mark current->active_mm as not "active" anymore.
  162. * We don't want to mislead possible IPI tlb flush routines.
  163. */
  164. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  165. cpumask_set_cpu(cpu, mm_cpumask(next));
  166. local_irq_restore(flags);
  167. }
  168. /*
  169. * Destroy context related info for an mm_struct that is about
  170. * to be put to rest.
  171. */
  172. static inline void destroy_context(struct mm_struct *mm)
  173. {
  174. }
  175. #define deactivate_mm(tsk, mm) do { } while (0)
  176. /*
  177. * After we have set current->mm to a new value, this activates
  178. * the context for the new mm so we see the new mappings.
  179. */
  180. static inline void
  181. activate_mm(struct mm_struct *prev, struct mm_struct *next)
  182. {
  183. unsigned long flags;
  184. unsigned int cpu = smp_processor_id();
  185. #ifdef CONFIG_MIPS_MT_SMTC
  186. unsigned long oldasid;
  187. unsigned long mtflags;
  188. int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
  189. #endif /* CONFIG_MIPS_MT_SMTC */
  190. local_irq_save(flags);
  191. /* Unconditionally get a new ASID. */
  192. get_new_mmu_context(next, cpu);
  193. #ifdef CONFIG_MIPS_MT_SMTC
  194. /* See comments for similar code above */
  195. mtflags = dvpe();
  196. oldasid = read_c0_entryhi() & ASID_MASK;
  197. if(smtc_live_asid[mytlb][oldasid]) {
  198. smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
  199. if(smtc_live_asid[mytlb][oldasid] == 0)
  200. smtc_flush_tlb_asid(oldasid);
  201. }
  202. /* See comments for similar code above */
  203. write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
  204. cpu_asid(cpu, next));
  205. ehb(); /* Make sure it propagates to TCStatus */
  206. evpe(mtflags);
  207. #else
  208. write_c0_entryhi(cpu_asid(cpu, next));
  209. #endif /* CONFIG_MIPS_MT_SMTC */
  210. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  211. /* mark mmu ownership change */
  212. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  213. cpumask_set_cpu(cpu, mm_cpumask(next));
  214. local_irq_restore(flags);
  215. }
  216. /*
  217. * If mm is currently active_mm, we can't really drop it. Instead,
  218. * we will get a new one for it.
  219. */
  220. static inline void
  221. drop_mmu_context(struct mm_struct *mm, unsigned cpu)
  222. {
  223. unsigned long flags;
  224. #ifdef CONFIG_MIPS_MT_SMTC
  225. unsigned long oldasid;
  226. /* Can't use spinlock because called from TLB flush within DVPE */
  227. unsigned int prevvpe;
  228. int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
  229. #endif /* CONFIG_MIPS_MT_SMTC */
  230. local_irq_save(flags);
  231. if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
  232. get_new_mmu_context(mm, cpu);
  233. #ifdef CONFIG_MIPS_MT_SMTC
  234. /* See comments for similar code above */
  235. prevvpe = dvpe();
  236. oldasid = (read_c0_entryhi() & ASID_MASK);
  237. if (smtc_live_asid[mytlb][oldasid]) {
  238. smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
  239. if(smtc_live_asid[mytlb][oldasid] == 0)
  240. smtc_flush_tlb_asid(oldasid);
  241. }
  242. /* See comments for similar code above */
  243. write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK)
  244. | cpu_asid(cpu, mm));
  245. ehb(); /* Make sure it propagates to TCStatus */
  246. evpe(prevvpe);
  247. #else /* not CONFIG_MIPS_MT_SMTC */
  248. write_c0_entryhi(cpu_asid(cpu, mm));
  249. #endif /* CONFIG_MIPS_MT_SMTC */
  250. } else {
  251. /* will get a new context next time */
  252. #ifndef CONFIG_MIPS_MT_SMTC
  253. cpu_context(cpu, mm) = 0;
  254. #else /* SMTC */
  255. int i;
  256. /* SMTC shares the TLB (and ASIDs) across VPEs */
  257. for_each_online_cpu(i) {
  258. if((smtc_status & SMTC_TLB_SHARED)
  259. || (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
  260. cpu_context(i, mm) = 0;
  261. }
  262. #endif /* CONFIG_MIPS_MT_SMTC */
  263. }
  264. local_irq_restore(flags);
  265. }
  266. #endif /* _ASM_MMU_CONTEXT_H */