mmu_context.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ALPHA_MMU_CONTEXT_H
  3. #define __ALPHA_MMU_CONTEXT_H
  4. /*
  5. * get a new mmu context..
  6. *
  7. * Copyright (C) 1996, Linus Torvalds
  8. */
  9. #include <linux/mm_types.h>
  10. #include <linux/sched.h>
  11. #include <asm/machvec.h>
  12. #include <asm/compiler.h>
  13. #include <asm-generic/mm_hooks.h>
  14. /*
  15. * Force a context reload. This is needed when we change the page
  16. * table pointer or when we update the ASN of the current process.
  17. */
  18. /* Don't get into trouble with dueling __EXTERN_INLINEs. */
  19. #ifndef __EXTERN_INLINE
  20. #include <asm/io.h>
  21. #endif
  22. static inline unsigned long
  23. __reload_thread(struct pcb_struct *pcb)
  24. {
  25. register unsigned long a0 __asm__("$16");
  26. register unsigned long v0 __asm__("$0");
  27. a0 = virt_to_phys(pcb);
  28. __asm__ __volatile__(
  29. "call_pal %2 #__reload_thread"
  30. : "=r"(v0), "=r"(a0)
  31. : "i"(PAL_swpctx), "r"(a0)
  32. : "$1", "$22", "$23", "$24", "$25");
  33. return v0;
  34. }
  35. /*
  36. * The maximum ASN's the processor supports. On the EV4 this is 63
  37. * but the PAL-code doesn't actually use this information. On the
  38. * EV5 this is 127, and EV6 has 255.
  39. *
  40. * On the EV4, the ASNs are more-or-less useless anyway, as they are
  41. * only used as an icache tag, not for TB entries. On the EV5 and EV6,
  42. * ASN's also validate the TB entries, and thus make a lot more sense.
  43. *
  44. * The EV4 ASN's don't even match the architecture manual, ugh. And
  45. * I quote: "If a processor implements address space numbers (ASNs),
  46. * and the old PTE has the Address Space Match (ASM) bit clear (ASNs
  47. * in use) and the Valid bit set, then entries can also effectively be
  48. * made coherent by assigning a new, unused ASN to the currently
  49. * running process and not reusing the previous ASN before calling the
  50. * appropriate PALcode routine to invalidate the translation buffer (TB)".
  51. *
  52. * In short, the EV4 has a "kind of" ASN capability, but it doesn't actually
  53. * work correctly and can thus not be used (explaining the lack of PAL-code
  54. * support).
  55. */
  56. #define EV4_MAX_ASN 63
  57. #define EV5_MAX_ASN 127
  58. #define EV6_MAX_ASN 255
  59. #ifdef CONFIG_ALPHA_GENERIC
  60. # define MAX_ASN (alpha_mv.max_asn)
  61. #else
  62. # if defined(CONFIG_ALPHA_EV56)
  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 void __load_new_mm_context(struct mm_struct *);
  141. asmlinkage void do_page_fault(unsigned long address, unsigned long mmcsr,
  142. long cause, struct pt_regs *regs);
  143. #ifdef CONFIG_SMP
  144. #define check_mmu_context() \
  145. do { \
  146. int cpu = smp_processor_id(); \
  147. cpu_data[cpu].asn_lock = 0; \
  148. barrier(); \
  149. if (cpu_data[cpu].need_new_asn) { \
  150. struct mm_struct * mm = current->active_mm; \
  151. cpu_data[cpu].need_new_asn = 0; \
  152. if (!mm->context[cpu]) \
  153. __load_new_mm_context(mm); \
  154. } \
  155. } while(0)
  156. #else
  157. #define check_mmu_context() do { } while(0)
  158. #endif
  159. __EXTERN_INLINE void
  160. ev5_activate_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm)
  161. {
  162. __load_new_mm_context(next_mm);
  163. }
  164. #define switch_mm(a,b,c) ev5_switch_mm((a),(b),(c))
  165. #define activate_mm(x,y) ev5_activate_mm((x),(y))
  166. #define init_new_context init_new_context
  167. static inline int
  168. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  169. {
  170. int i;
  171. for_each_online_cpu(i)
  172. mm->context[i] = 0;
  173. if (tsk != current)
  174. task_thread_info(tsk)->pcb.ptbr
  175. = ((unsigned long)mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
  176. return 0;
  177. }
  178. #define enter_lazy_tlb enter_lazy_tlb
  179. static inline void
  180. enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  181. {
  182. task_thread_info(tsk)->pcb.ptbr
  183. = ((unsigned long)mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
  184. }
  185. #include <asm-generic/mmu_context.h>
  186. #ifdef __MMU_EXTERN_INLINE
  187. #undef __EXTERN_INLINE
  188. #undef __MMU_EXTERN_INLINE
  189. #endif
  190. #endif /* __ALPHA_MMU_CONTEXT_H */