mmu_context.h 5.7 KB

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