mmu_context.h 8.6 KB

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