mmu_context.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_IA64_MMU_CONTEXT_H
  3. #define _ASM_IA64_MMU_CONTEXT_H
  4. /*
  5. * Copyright (C) 1998-2002 Hewlett-Packard Co
  6. * David Mosberger-Tang <[email protected]>
  7. */
  8. /*
  9. * Routines to manage the allocation of task context numbers. Task context
  10. * numbers are used to reduce or eliminate the need to perform TLB flushes
  11. * due to context switches. Context numbers are implemented using ia-64
  12. * region ids. Since the IA-64 TLB does not consider the region number when
  13. * performing a TLB lookup, we need to assign a unique region id to each
  14. * region in a process. We use the least significant three bits in aregion
  15. * id for this purpose.
  16. */
  17. #define IA64_REGION_ID_KERNEL 0 /* the kernel's region id (tlb.c depends on this being 0) */
  18. #define ia64_rid(ctx,addr) (((ctx) << 3) | (addr >> 61))
  19. # include <asm/page.h>
  20. # ifndef __ASSEMBLY__
  21. #include <linux/compiler.h>
  22. #include <linux/percpu.h>
  23. #include <linux/sched.h>
  24. #include <linux/mm_types.h>
  25. #include <linux/spinlock.h>
  26. #include <asm/processor.h>
  27. #include <asm-generic/mm_hooks.h>
  28. struct ia64_ctx {
  29. spinlock_t lock;
  30. unsigned int next; /* next context number to use */
  31. unsigned int limit; /* available free range */
  32. unsigned int max_ctx; /* max. context value supported by all CPUs */
  33. /* call wrap_mmu_context when next >= max */
  34. unsigned long *bitmap; /* bitmap size is max_ctx+1 */
  35. unsigned long *flushmap;/* pending rid to be flushed */
  36. };
  37. extern struct ia64_ctx ia64_ctx;
  38. DECLARE_PER_CPU(u8, ia64_need_tlb_flush);
  39. extern void mmu_context_init (void);
  40. extern void wrap_mmu_context (struct mm_struct *mm);
  41. /*
  42. * When the context counter wraps around all TLBs need to be flushed because
  43. * an old context number might have been reused. This is signalled by the
  44. * ia64_need_tlb_flush per-CPU variable, which is checked in the routine
  45. * below. Called by activate_mm(). <[email protected]>
  46. */
  47. static inline void
  48. delayed_tlb_flush (void)
  49. {
  50. extern void local_flush_tlb_all (void);
  51. unsigned long flags;
  52. if (unlikely(__ia64_per_cpu_var(ia64_need_tlb_flush))) {
  53. spin_lock_irqsave(&ia64_ctx.lock, flags);
  54. if (__ia64_per_cpu_var(ia64_need_tlb_flush)) {
  55. local_flush_tlb_all();
  56. __ia64_per_cpu_var(ia64_need_tlb_flush) = 0;
  57. }
  58. spin_unlock_irqrestore(&ia64_ctx.lock, flags);
  59. }
  60. }
  61. static inline nv_mm_context_t
  62. get_mmu_context (struct mm_struct *mm)
  63. {
  64. unsigned long flags;
  65. nv_mm_context_t context = mm->context;
  66. if (likely(context))
  67. goto out;
  68. spin_lock_irqsave(&ia64_ctx.lock, flags);
  69. /* re-check, now that we've got the lock: */
  70. context = mm->context;
  71. if (context == 0) {
  72. cpumask_clear(mm_cpumask(mm));
  73. if (ia64_ctx.next >= ia64_ctx.limit) {
  74. ia64_ctx.next = find_next_zero_bit(ia64_ctx.bitmap,
  75. ia64_ctx.max_ctx, ia64_ctx.next);
  76. ia64_ctx.limit = find_next_bit(ia64_ctx.bitmap,
  77. ia64_ctx.max_ctx, ia64_ctx.next);
  78. if (ia64_ctx.next >= ia64_ctx.max_ctx)
  79. wrap_mmu_context(mm);
  80. }
  81. mm->context = context = ia64_ctx.next++;
  82. __set_bit(context, ia64_ctx.bitmap);
  83. }
  84. spin_unlock_irqrestore(&ia64_ctx.lock, flags);
  85. out:
  86. /*
  87. * Ensure we're not starting to use "context" before any old
  88. * uses of it are gone from our TLB.
  89. */
  90. delayed_tlb_flush();
  91. return context;
  92. }
  93. /*
  94. * Initialize context number to some sane value. MM is guaranteed to be a
  95. * brand-new address-space, so no TLB flushing is needed, ever.
  96. */
  97. #define init_new_context init_new_context
  98. static inline int
  99. init_new_context (struct task_struct *p, struct mm_struct *mm)
  100. {
  101. mm->context = 0;
  102. return 0;
  103. }
  104. static inline void
  105. reload_context (nv_mm_context_t context)
  106. {
  107. unsigned long rid;
  108. unsigned long rid_incr = 0;
  109. unsigned long rr0, rr1, rr2, rr3, rr4;
  110. #ifdef CONFIG_HUGETLB_PAGE
  111. unsigned long old_rr4;
  112. old_rr4 = ia64_get_rr(RGN_BASE(RGN_HPAGE));
  113. #endif
  114. rid = context << 3; /* make space for encoding the region number */
  115. rid_incr = 1 << 8;
  116. /* encode the region id, preferred page size, and VHPT enable bit: */
  117. rr0 = (rid << 8) | (PAGE_SHIFT << 2) | 1;
  118. rr1 = rr0 + 1*rid_incr;
  119. rr2 = rr0 + 2*rid_incr;
  120. rr3 = rr0 + 3*rid_incr;
  121. rr4 = rr0 + 4*rid_incr;
  122. #ifdef CONFIG_HUGETLB_PAGE
  123. rr4 = (rr4 & (~(0xfcUL))) | (old_rr4 & 0xfc);
  124. # if RGN_HPAGE != 4
  125. # error "reload_context assumes RGN_HPAGE is 4"
  126. # endif
  127. #endif
  128. ia64_set_rr0_to_rr4(rr0, rr1, rr2, rr3, rr4);
  129. ia64_srlz_i(); /* srlz.i implies srlz.d */
  130. }
  131. /*
  132. * Must be called with preemption off
  133. */
  134. static inline void
  135. activate_context (struct mm_struct *mm)
  136. {
  137. nv_mm_context_t context;
  138. do {
  139. context = get_mmu_context(mm);
  140. if (!cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
  141. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  142. reload_context(context);
  143. /*
  144. * in the unlikely event of a TLB-flush by another thread,
  145. * redo the load.
  146. */
  147. } while (unlikely(context != mm->context));
  148. }
  149. /*
  150. * Switch from address space PREV to address space NEXT.
  151. */
  152. #define activate_mm activate_mm
  153. static inline void
  154. activate_mm (struct mm_struct *prev, struct mm_struct *next)
  155. {
  156. /*
  157. * We may get interrupts here, but that's OK because interrupt
  158. * handlers cannot touch user-space.
  159. */
  160. ia64_set_kr(IA64_KR_PT_BASE, __pa(next->pgd));
  161. activate_context(next);
  162. }
  163. #define switch_mm(prev_mm,next_mm,next_task) activate_mm(prev_mm, next_mm)
  164. #include <asm-generic/mmu_context.h>
  165. # endif /* ! __ASSEMBLY__ */
  166. #endif /* _ASM_IA64_MMU_CONTEXT_H */