mmu_context.h 8.3 KB

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