mmu_context.h 8.7 KB

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