mmu_context.h 8.0 KB

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