mmu_context.h 8.1 KB

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