mmu_context.h 8.0 KB

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