mmu_context.h 5.3 KB

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