mmu_context.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifdef __KERNEL__
  2. #ifndef __PPC_MMU_CONTEXT_H
  3. #define __PPC_MMU_CONTEXT_H
  4. #include <linux/bitops.h>
  5. #include <asm/atomic.h>
  6. #include <asm/mmu.h>
  7. #include <asm/cputable.h>
  8. #include <asm-generic/mm_hooks.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. #else
  58. /* PPC 6xx, 7xx CPUs */
  59. #define NO_CONTEXT ((unsigned long) -1)
  60. #define LAST_CONTEXT 32767
  61. #define FIRST_CONTEXT 1
  62. #endif
  63. /*
  64. * Set the current MMU context.
  65. * On 32-bit PowerPCs (other than the 8xx embedded chips), this is done by
  66. * loading up the segment registers for the user part of the address space.
  67. *
  68. * Since the PGD is immediately available, it is much faster to simply
  69. * pass this along as a second parameter, which is required for 8xx and
  70. * can be used for debugging on all processors (if you happen to have
  71. * an Abatron).
  72. */
  73. extern void set_context(unsigned long contextid, pgd_t *pgd);
  74. /*
  75. * Bitmap of contexts in use.
  76. * The size of this bitmap is LAST_CONTEXT + 1 bits.
  77. */
  78. extern unsigned long context_map[];
  79. /*
  80. * This caches the next context number that we expect to be free.
  81. * Its use is an optimization only, we can't rely on this context
  82. * number to be free, but it usually will be.
  83. */
  84. extern unsigned long next_mmu_context;
  85. /*
  86. * If we don't have sufficient contexts to give one to every task
  87. * that could be in the system, we need to be able to steal contexts.
  88. * These variables support that.
  89. */
  90. #if LAST_CONTEXT < 30000
  91. #define FEW_CONTEXTS 1
  92. extern atomic_t nr_free_contexts;
  93. extern struct mm_struct *context_mm[LAST_CONTEXT+1];
  94. extern void steal_context(void);
  95. #endif
  96. /*
  97. * Get a new mmu context for the address space described by `mm'.
  98. */
  99. static inline void get_mmu_context(struct mm_struct *mm)
  100. {
  101. unsigned long ctx;
  102. if (mm->context.id != NO_CONTEXT)
  103. return;
  104. #ifdef FEW_CONTEXTS
  105. while (atomic_dec_if_positive(&nr_free_contexts) < 0)
  106. steal_context();
  107. #endif
  108. ctx = next_mmu_context;
  109. while (test_and_set_bit(ctx, context_map)) {
  110. ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
  111. if (ctx > LAST_CONTEXT)
  112. ctx = 0;
  113. }
  114. next_mmu_context = (ctx + 1) & LAST_CONTEXT;
  115. mm->context.id = ctx;
  116. #ifdef FEW_CONTEXTS
  117. context_mm[ctx] = mm;
  118. #endif
  119. }
  120. /*
  121. * Set up the context for a new address space.
  122. */
  123. static inline int init_new_context(struct task_struct *t, struct mm_struct *mm)
  124. {
  125. mm->context.id = NO_CONTEXT;
  126. mm->context.vdso_base = 0;
  127. return 0;
  128. }
  129. /*
  130. * We're finished using the context for an address space.
  131. */
  132. static inline void destroy_context(struct mm_struct *mm)
  133. {
  134. preempt_disable();
  135. if (mm->context.id != NO_CONTEXT) {
  136. clear_bit(mm->context.id, context_map);
  137. mm->context.id = NO_CONTEXT;
  138. #ifdef FEW_CONTEXTS
  139. atomic_inc(&nr_free_contexts);
  140. #endif
  141. }
  142. preempt_enable();
  143. }
  144. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  145. struct task_struct *tsk)
  146. {
  147. #ifdef CONFIG_ALTIVEC
  148. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  149. asm volatile ("dssall;\n"
  150. #ifndef CONFIG_POWER4
  151. "sync;\n" /* G4 needs a sync here, G5 apparently not */
  152. #endif
  153. : : );
  154. #endif /* CONFIG_ALTIVEC */
  155. tsk->thread.pgdir = next->pgd;
  156. /* No need to flush userspace segments if the mm doesnt change */
  157. if (prev == next)
  158. return;
  159. /* Setup new userspace context */
  160. get_mmu_context(next);
  161. set_context(next->context.id, next->pgd);
  162. }
  163. #define deactivate_mm(tsk,mm) do { } while (0)
  164. /*
  165. * After we have set current->mm to a new value, this activates
  166. * the context for the new mm so we see the new mappings.
  167. */
  168. #define activate_mm(active_mm, mm) switch_mm(active_mm, mm, current)
  169. extern void mmu_context_init(void);
  170. #endif /* __PPC_MMU_CONTEXT_H */
  171. #endif /* __KERNEL__ */