mmu_context_64.h 5.4 KB

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