mmu_context.h 8.1 KB

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