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