mmu_context.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #ifndef __ALPHA_MMU_CONTEXT_H
  2. #define __ALPHA_MMU_CONTEXT_H
  3. /*
  4. * get a new mmu context..
  5. *
  6. * Copyright (C) 1996, Linus Torvalds
  7. */
  8. #include <linux/sched.h>
  9. #include <asm/machvec.h>
  10. #include <asm/compiler.h>
  11. #include <asm-generic/mm_hooks.h>
  12. /*
  13. * Force a context reload. This is needed when we change the page
  14. * table pointer or when we update the ASN of the current process.
  15. */
  16. /* Don't get into trouble with dueling __EXTERN_INLINEs. */
  17. #ifndef __EXTERN_INLINE
  18. #include <asm/io.h>
  19. #endif
  20. static inline unsigned long
  21. __reload_thread(struct pcb_struct *pcb)
  22. {
  23. register unsigned long a0 __asm__("$16");
  24. register unsigned long v0 __asm__("$0");
  25. a0 = virt_to_phys(pcb);
  26. __asm__ __volatile__(
  27. "call_pal %2 #__reload_thread"
  28. : "=r"(v0), "=r"(a0)
  29. : "i"(PAL_swpctx), "r"(a0)
  30. : "$1", "$22", "$23", "$24", "$25");
  31. return v0;
  32. }
  33. /*
  34. * The maximum ASN's the processor supports. On the EV4 this is 63
  35. * but the PAL-code doesn't actually use this information. On the
  36. * EV5 this is 127, and EV6 has 255.
  37. *
  38. * On the EV4, the ASNs are more-or-less useless anyway, as they are
  39. * only used as an icache tag, not for TB entries. On the EV5 and EV6,
  40. * ASN's also validate the TB entries, and thus make a lot more sense.
  41. *
  42. * The EV4 ASN's don't even match the architecture manual, ugh. And
  43. * I quote: "If a processor implements address space numbers (ASNs),
  44. * and the old PTE has the Address Space Match (ASM) bit clear (ASNs
  45. * in use) and the Valid bit set, then entries can also effectively be
  46. * made coherent by assigning a new, unused ASN to the currently
  47. * running process and not reusing the previous ASN before calling the
  48. * appropriate PALcode routine to invalidate the translation buffer (TB)".
  49. *
  50. * In short, the EV4 has a "kind of" ASN capability, but it doesn't actually
  51. * work correctly and can thus not be used (explaining the lack of PAL-code
  52. * support).
  53. */
  54. #define EV4_MAX_ASN 63
  55. #define EV5_MAX_ASN 127
  56. #define EV6_MAX_ASN 255
  57. #ifdef CONFIG_ALPHA_GENERIC
  58. # define MAX_ASN (alpha_mv.max_asn)
  59. #else
  60. # ifdef CONFIG_ALPHA_EV4
  61. # define MAX_ASN EV4_MAX_ASN
  62. # elif defined(CONFIG_ALPHA_EV5)
  63. # define MAX_ASN EV5_MAX_ASN
  64. # else
  65. # define MAX_ASN EV6_MAX_ASN
  66. # endif
  67. #endif
  68. /*
  69. * cpu_last_asn(processor):
  70. * 63 0
  71. * +-------------+----------------+--------------+
  72. * | asn version | this processor | hardware asn |
  73. * +-------------+----------------+--------------+
  74. */
  75. #include <asm/smp.h>
  76. #ifdef CONFIG_SMP
  77. #define cpu_last_asn(cpuid) (cpu_data[cpuid].last_asn)
  78. #else
  79. extern unsigned long last_asn;
  80. #define cpu_last_asn(cpuid) last_asn
  81. #endif /* CONFIG_SMP */
  82. #define WIDTH_HARDWARE_ASN 8
  83. #define ASN_FIRST_VERSION (1UL << WIDTH_HARDWARE_ASN)
  84. #define HARDWARE_ASN_MASK ((1UL << WIDTH_HARDWARE_ASN) - 1)
  85. /*
  86. * NOTE! The way this is set up, the high bits of the "asn_cache" (and
  87. * the "mm->context") are the ASN _version_ code. A version of 0 is
  88. * always considered invalid, so to invalidate another process you only
  89. * need to do "p->mm->context = 0".
  90. *
  91. * If we need more ASN's than the processor has, we invalidate the old
  92. * user TLB's (tbiap()) and start a new ASN version. That will automatically
  93. * force a new asn for any other processes the next time they want to
  94. * run.
  95. */
  96. #ifndef __EXTERN_INLINE
  97. #define __EXTERN_INLINE extern inline
  98. #define __MMU_EXTERN_INLINE
  99. #endif
  100. extern inline unsigned long
  101. __get_new_mm_context(struct mm_struct *mm, long cpu)
  102. {
  103. unsigned long asn = cpu_last_asn(cpu);
  104. unsigned long next = asn + 1;
  105. if ((asn & HARDWARE_ASN_MASK) >= MAX_ASN) {
  106. tbiap();
  107. imb();
  108. next = (asn & ~HARDWARE_ASN_MASK) + ASN_FIRST_VERSION;
  109. }
  110. cpu_last_asn(cpu) = next;
  111. return next;
  112. }
  113. __EXTERN_INLINE void
  114. ev5_switch_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm,
  115. struct task_struct *next)
  116. {
  117. /* Check if our ASN is of an older version, and thus invalid. */
  118. unsigned long asn;
  119. unsigned long mmc;
  120. long cpu = smp_processor_id();
  121. #ifdef CONFIG_SMP
  122. cpu_data[cpu].asn_lock = 1;
  123. barrier();
  124. #endif
  125. asn = cpu_last_asn(cpu);
  126. mmc = next_mm->context[cpu];
  127. if ((mmc ^ asn) & ~HARDWARE_ASN_MASK) {
  128. mmc = __get_new_mm_context(next_mm, cpu);
  129. next_mm->context[cpu] = mmc;
  130. }
  131. #ifdef CONFIG_SMP
  132. else
  133. cpu_data[cpu].need_new_asn = 1;
  134. #endif
  135. /* Always update the PCB ASN. Another thread may have allocated
  136. a new mm->context (via flush_tlb_mm) without the ASN serial
  137. number wrapping. We have no way to detect when this is needed. */
  138. task_thread_info(next)->pcb.asn = mmc & HARDWARE_ASN_MASK;
  139. }
  140. __EXTERN_INLINE void
  141. ev4_switch_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm,
  142. struct task_struct *next)
  143. {
  144. /* As described, ASN's are broken for TLB usage. But we can
  145. optimize for switching between threads -- if the mm is
  146. unchanged from current we needn't flush. */
  147. /* ??? May not be needed because EV4 PALcode recognizes that
  148. ASN's are broken and does a tbiap itself on swpctx, under
  149. the "Must set ASN or flush" rule. At least this is true
  150. for a 1992 SRM, reports Joseph Martin (jmartin@hlo.dec.com).
  151. I'm going to leave this here anyway, just to Be Sure. -- r~ */
  152. if (prev_mm != next_mm)
  153. tbiap();
  154. /* Do continue to allocate ASNs, because we can still use them
  155. to avoid flushing the icache. */
  156. ev5_switch_mm(prev_mm, next_mm, next);
  157. }
  158. extern void __load_new_mm_context(struct mm_struct *);
  159. #ifdef CONFIG_SMP
  160. #define check_mmu_context() \
  161. do { \
  162. int cpu = smp_processor_id(); \
  163. cpu_data[cpu].asn_lock = 0; \
  164. barrier(); \
  165. if (cpu_data[cpu].need_new_asn) { \
  166. struct mm_struct * mm = current->active_mm; \
  167. cpu_data[cpu].need_new_asn = 0; \
  168. if (!mm->context[cpu]) \
  169. __load_new_mm_context(mm); \
  170. } \
  171. } while(0)
  172. #else
  173. #define check_mmu_context() do { } while(0)
  174. #endif
  175. __EXTERN_INLINE void
  176. ev5_activate_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm)
  177. {
  178. __load_new_mm_context(next_mm);
  179. }
  180. __EXTERN_INLINE void
  181. ev4_activate_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm)
  182. {
  183. __load_new_mm_context(next_mm);
  184. tbiap();
  185. }
  186. #define deactivate_mm(tsk,mm) do { } while (0)
  187. #ifdef CONFIG_ALPHA_GENERIC
  188. # define switch_mm(a,b,c) alpha_mv.mv_switch_mm((a),(b),(c))
  189. # define activate_mm(x,y) alpha_mv.mv_activate_mm((x),(y))
  190. #else
  191. # ifdef CONFIG_ALPHA_EV4
  192. # define switch_mm(a,b,c) ev4_switch_mm((a),(b),(c))
  193. # define activate_mm(x,y) ev4_activate_mm((x),(y))
  194. # else
  195. # define switch_mm(a,b,c) ev5_switch_mm((a),(b),(c))
  196. # define activate_mm(x,y) ev5_activate_mm((x),(y))
  197. # endif
  198. #endif
  199. static inline int
  200. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  201. {
  202. int i;
  203. for_each_online_cpu(i)
  204. mm->context[i] = 0;
  205. if (tsk != current)
  206. task_thread_info(tsk)->pcb.ptbr
  207. = ((unsigned long)mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
  208. return 0;
  209. }
  210. extern inline void
  211. destroy_context(struct mm_struct *mm)
  212. {
  213. /* Nothing to do. */
  214. }
  215. static inline void
  216. enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  217. {
  218. task_thread_info(tsk)->pcb.ptbr
  219. = ((unsigned long)mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
  220. }
  221. #ifdef __MMU_EXTERN_INLINE
  222. #undef __EXTERN_INLINE
  223. #undef __MMU_EXTERN_INLINE
  224. #endif
  225. #endif /* __ALPHA_MMU_CONTEXT_H */