mmu_context.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifdef __KERNEL__
  2. #ifndef __PPC_MMU_CONTEXT_H
  3. #define __PPC_MMU_CONTEXT_H
  4. #include <linux/config.h>
  5. #include <asm/atomic.h>
  6. #include <asm/bitops.h>
  7. #include <asm/mmu.h>
  8. #include <asm/cputable.h>
  9. /*
  10. * On 32-bit PowerPC 6xx/7xx/7xxx CPUs, we use a set of 16 VSIDs
  11. * (virtual segment identifiers) for each context. Although the
  12. * hardware supports 24-bit VSIDs, and thus >1 million contexts,
  13. * we only use 32,768 of them. That is ample, since there can be
  14. * at most around 30,000 tasks in the system anyway, and it means
  15. * that we can use a bitmap to indicate which contexts are in use.
  16. * Using a bitmap means that we entirely avoid all of the problems
  17. * that we used to have when the context number overflowed,
  18. * particularly on SMP systems.
  19. * -- paulus.
  20. */
  21. /*
  22. * This function defines the mapping from contexts to VSIDs (virtual
  23. * segment IDs). We use a skew on both the context and the high 4 bits
  24. * of the 32-bit virtual address (the "effective segment ID") in order
  25. * to spread out the entries in the MMU hash table. Note, if this
  26. * function is changed then arch/ppc/mm/hashtable.S will have to be
  27. * changed to correspond.
  28. */
  29. #define CTX_TO_VSID(ctx, va) (((ctx) * (897 * 16) + ((va) >> 28) * 0x111) \
  30. & 0xffffff)
  31. /*
  32. The MPC8xx has only 16 contexts. We rotate through them on each
  33. task switch. A better way would be to keep track of tasks that
  34. own contexts, and implement an LRU usage. That way very active
  35. tasks don't always have to pay the TLB reload overhead. The
  36. kernel pages are mapped shared, so the kernel can run on behalf
  37. of any task that makes a kernel entry. Shared does not mean they
  38. are not protected, just that the ASID comparison is not performed.
  39. -- Dan
  40. The IBM4xx has 256 contexts, so we can just rotate through these
  41. as a way of "switching" contexts. If the TID of the TLB is zero,
  42. the PID/TID comparison is disabled, so we can use a TID of zero
  43. to represent all kernel pages as shared among all contexts.
  44. -- Dan
  45. */
  46. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  47. {
  48. }
  49. #ifdef CONFIG_8xx
  50. #define NO_CONTEXT 16
  51. #define LAST_CONTEXT 15
  52. #define FIRST_CONTEXT 0
  53. #elif defined(CONFIG_4xx)
  54. #define NO_CONTEXT 256
  55. #define LAST_CONTEXT 255
  56. #define FIRST_CONTEXT 1
  57. #elif defined(CONFIG_E200) || defined(CONFIG_E500)
  58. #define NO_CONTEXT 256
  59. #define LAST_CONTEXT 255
  60. #define FIRST_CONTEXT 1
  61. #else
  62. /* PPC 6xx, 7xx CPUs */
  63. #define NO_CONTEXT ((mm_context_t) -1)
  64. #define LAST_CONTEXT 32767
  65. #define FIRST_CONTEXT 1
  66. #endif
  67. /*
  68. * Set the current MMU context.
  69. * On 32-bit PowerPCs (other than the 8xx embedded chips), this is done by
  70. * loading up the segment registers for the user part of the address space.
  71. *
  72. * Since the PGD is immediately available, it is much faster to simply
  73. * pass this along as a second parameter, which is required for 8xx and
  74. * can be used for debugging on all processors (if you happen to have
  75. * an Abatron).
  76. */
  77. extern void set_context(mm_context_t context, pgd_t *pgd);
  78. /*
  79. * Bitmap of contexts in use.
  80. * The size of this bitmap is LAST_CONTEXT + 1 bits.
  81. */
  82. extern unsigned long context_map[];
  83. /*
  84. * This caches the next context number that we expect to be free.
  85. * Its use is an optimization only, we can't rely on this context
  86. * number to be free, but it usually will be.
  87. */
  88. extern mm_context_t next_mmu_context;
  89. /*
  90. * If we don't have sufficient contexts to give one to every task
  91. * that could be in the system, we need to be able to steal contexts.
  92. * These variables support that.
  93. */
  94. #if LAST_CONTEXT < 30000
  95. #define FEW_CONTEXTS 1
  96. extern atomic_t nr_free_contexts;
  97. extern struct mm_struct *context_mm[LAST_CONTEXT+1];
  98. extern void steal_context(void);
  99. #endif
  100. /*
  101. * Get a new mmu context for the address space described by `mm'.
  102. */
  103. static inline void get_mmu_context(struct mm_struct *mm)
  104. {
  105. mm_context_t ctx;
  106. if (mm->context != NO_CONTEXT)
  107. return;
  108. #ifdef FEW_CONTEXTS
  109. while (atomic_dec_if_positive(&nr_free_contexts) < 0)
  110. steal_context();
  111. #endif
  112. ctx = next_mmu_context;
  113. while (test_and_set_bit(ctx, context_map)) {
  114. ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
  115. if (ctx > LAST_CONTEXT)
  116. ctx = 0;
  117. }
  118. next_mmu_context = (ctx + 1) & LAST_CONTEXT;
  119. mm->context = ctx;
  120. #ifdef FEW_CONTEXTS
  121. context_mm[ctx] = mm;
  122. #endif
  123. }
  124. /*
  125. * Set up the context for a new address space.
  126. */
  127. #define init_new_context(tsk,mm) (((mm)->context = NO_CONTEXT), 0)
  128. /*
  129. * We're finished using the context for an address space.
  130. */
  131. static inline void destroy_context(struct mm_struct *mm)
  132. {
  133. preempt_disable();
  134. if (mm->context != NO_CONTEXT) {
  135. clear_bit(mm->context, context_map);
  136. mm->context = NO_CONTEXT;
  137. #ifdef FEW_CONTEXTS
  138. atomic_inc(&nr_free_contexts);
  139. #endif
  140. }
  141. preempt_enable();
  142. }
  143. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  144. struct task_struct *tsk)
  145. {
  146. #ifdef CONFIG_ALTIVEC
  147. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  148. asm volatile ("dssall;\n"
  149. #ifndef CONFIG_POWER4
  150. "sync;\n" /* G4 needs a sync here, G5 apparently not */
  151. #endif
  152. : : );
  153. #endif /* CONFIG_ALTIVEC */
  154. tsk->thread.pgdir = next->pgd;
  155. /* No need to flush userspace segments if the mm doesnt change */
  156. if (prev == next)
  157. return;
  158. /* Setup new userspace context */
  159. get_mmu_context(next);
  160. set_context(next->context, next->pgd);
  161. }
  162. #define deactivate_mm(tsk,mm) do { } while (0)
  163. /*
  164. * After we have set current->mm to a new value, this activates
  165. * the context for the new mm so we see the new mappings.
  166. */
  167. #define activate_mm(active_mm, mm) switch_mm(active_mm, mm, current)
  168. extern void mmu_context_init(void);
  169. #endif /* __PPC_MMU_CONTEXT_H */
  170. #endif /* __KERNEL__ */