mmu_context.h 8.9 KB

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