mmu_context.h 4.5 KB

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