mmu_context.h 8.6 KB

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