mmu_context.h 9.1 KB

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