mmu_context_64.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __SPARC64_MMU_CONTEXT_H
  3. #define __SPARC64_MMU_CONTEXT_H
  4. /* Derived heavily from Linus's Alpha/AXP ASN code... */
  5. #ifndef __ASSEMBLY__
  6. #include <linux/spinlock.h>
  7. #include <linux/mm_types.h>
  8. #include <linux/smp.h>
  9. #include <linux/sched.h>
  10. #include <asm/spitfire.h>
  11. #include <asm/adi_64.h>
  12. #include <asm-generic/mm_hooks.h>
  13. #include <asm/percpu.h>
  14. extern spinlock_t ctx_alloc_lock;
  15. extern unsigned long tlb_context_cache;
  16. extern unsigned long mmu_context_bmap[];
  17. DECLARE_PER_CPU(struct mm_struct *, per_cpu_secondary_mm);
  18. void get_new_mmu_context(struct mm_struct *mm);
  19. #define init_new_context init_new_context
  20. int init_new_context(struct task_struct *tsk, struct mm_struct *mm);
  21. #define destroy_context destroy_context
  22. void destroy_context(struct mm_struct *mm);
  23. void __tsb_context_switch(unsigned long pgd_pa,
  24. struct tsb_config *tsb_base,
  25. struct tsb_config *tsb_huge,
  26. unsigned long tsb_descr_pa,
  27. unsigned long secondary_ctx);
  28. static inline void tsb_context_switch_ctx(struct mm_struct *mm,
  29. unsigned long ctx)
  30. {
  31. __tsb_context_switch(__pa(mm->pgd),
  32. &mm->context.tsb_block[MM_TSB_BASE],
  33. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  34. (mm->context.tsb_block[MM_TSB_HUGE].tsb ?
  35. &mm->context.tsb_block[MM_TSB_HUGE] :
  36. NULL)
  37. #else
  38. NULL
  39. #endif
  40. , __pa(&mm->context.tsb_descr[MM_TSB_BASE]),
  41. ctx);
  42. }
  43. #define tsb_context_switch(X) tsb_context_switch_ctx(X, 0)
  44. void tsb_grow(struct mm_struct *mm,
  45. unsigned long tsb_index,
  46. unsigned long mm_rss);
  47. #ifdef CONFIG_SMP
  48. void smp_tsb_sync(struct mm_struct *mm);
  49. #else
  50. #define smp_tsb_sync(__mm) do { } while (0)
  51. #endif
  52. /* Set MMU context in the actual hardware. */
  53. #define load_secondary_context(__mm) \
  54. __asm__ __volatile__( \
  55. "\n661: stxa %0, [%1] %2\n" \
  56. " .section .sun4v_1insn_patch, \"ax\"\n" \
  57. " .word 661b\n" \
  58. " stxa %0, [%1] %3\n" \
  59. " .previous\n" \
  60. " flush %%g6\n" \
  61. : /* No outputs */ \
  62. : "r" (CTX_HWBITS((__mm)->context)), \
  63. "r" (SECONDARY_CONTEXT), "i" (ASI_DMMU), "i" (ASI_MMU))
  64. void __flush_tlb_mm(unsigned long, unsigned long);
  65. /* Switch the current MM context. */
  66. static inline void switch_mm(struct mm_struct *old_mm, struct mm_struct *mm, struct task_struct *tsk)
  67. {
  68. unsigned long ctx_valid, flags;
  69. int cpu = smp_processor_id();
  70. per_cpu(per_cpu_secondary_mm, cpu) = mm;
  71. if (unlikely(mm == &init_mm))
  72. return;
  73. spin_lock_irqsave(&mm->context.lock, flags);
  74. ctx_valid = CTX_VALID(mm->context);
  75. if (!ctx_valid)
  76. get_new_mmu_context(mm);
  77. /* We have to be extremely careful here or else we will miss
  78. * a TSB grow if we switch back and forth between a kernel
  79. * thread and an address space which has its TSB size increased
  80. * on another processor.
  81. *
  82. * It is possible to play some games in order to optimize the
  83. * switch, but the safest thing to do is to unconditionally
  84. * perform the secondary context load and the TSB context switch.
  85. *
  86. * For reference the bad case is, for address space "A":
  87. *
  88. * CPU 0 CPU 1
  89. * run address space A
  90. * set cpu0's bits in cpu_vm_mask
  91. * switch to kernel thread, borrow
  92. * address space A via entry_lazy_tlb
  93. * run address space A
  94. * set cpu1's bit in cpu_vm_mask
  95. * flush_tlb_pending()
  96. * reset cpu_vm_mask to just cpu1
  97. * TSB grow
  98. * run address space A
  99. * context was valid, so skip
  100. * TSB context switch
  101. *
  102. * At that point cpu0 continues to use a stale TSB, the one from
  103. * before the TSB grow performed on cpu1. cpu1 did not cross-call
  104. * cpu0 to update its TSB because at that point the cpu_vm_mask
  105. * only had cpu1 set in it.
  106. */
  107. tsb_context_switch_ctx(mm, CTX_HWBITS(mm->context));
  108. /* Any time a processor runs a context on an address space
  109. * for the first time, we must flush that context out of the
  110. * local TLB.
  111. */
  112. if (!ctx_valid || !cpumask_test_cpu(cpu, mm_cpumask(mm))) {
  113. cpumask_set_cpu(cpu, mm_cpumask(mm));
  114. __flush_tlb_mm(CTX_HWBITS(mm->context),
  115. SECONDARY_CONTEXT);
  116. }
  117. spin_unlock_irqrestore(&mm->context.lock, flags);
  118. }
  119. #define activate_mm(active_mm, mm) switch_mm(active_mm, mm, NULL)
  120. #define __HAVE_ARCH_START_CONTEXT_SWITCH
  121. static inline void arch_start_context_switch(struct task_struct *prev)
  122. {
  123. /* Save the current state of MCDPER register for the process
  124. * we are switching from
  125. */
  126. if (adi_capable()) {
  127. register unsigned long tmp_mcdper;
  128. __asm__ __volatile__(
  129. ".word 0x83438000\n\t" /* rd %mcdper, %g1 */
  130. "mov %%g1, %0\n\t"
  131. : "=r" (tmp_mcdper)
  132. :
  133. : "g1");
  134. if (tmp_mcdper)
  135. set_tsk_thread_flag(prev, TIF_MCDPER);
  136. else
  137. clear_tsk_thread_flag(prev, TIF_MCDPER);
  138. }
  139. }
  140. #define finish_arch_post_lock_switch finish_arch_post_lock_switch
  141. static inline void finish_arch_post_lock_switch(void)
  142. {
  143. /* Restore the state of MCDPER register for the new process
  144. * just switched to.
  145. */
  146. if (adi_capable()) {
  147. register unsigned long tmp_mcdper;
  148. tmp_mcdper = test_thread_flag(TIF_MCDPER);
  149. __asm__ __volatile__(
  150. "mov %0, %%g1\n\t"
  151. ".word 0x9d800001\n\t" /* wr %g0, %g1, %mcdper" */
  152. ".word 0xaf902001\n\t" /* wrpr %g0, 1, %pmcdper */
  153. :
  154. : "ir" (tmp_mcdper)
  155. : "g1");
  156. if (current && current->mm && current->mm->context.adi) {
  157. struct pt_regs *regs;
  158. regs = task_pt_regs(current);
  159. regs->tstate |= TSTATE_MCDE;
  160. }
  161. }
  162. }
  163. #define mm_untag_mask mm_untag_mask
  164. static inline unsigned long mm_untag_mask(struct mm_struct *mm)
  165. {
  166. return -1UL >> adi_nbits();
  167. }
  168. #include <asm-generic/mmu_context.h>
  169. #endif /* !(__ASSEMBLY__) */
  170. #endif /* !(__SPARC64_MMU_CONTEXT_H) */