mmu_context.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Switch a MMU context.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 1996, 1997, 1998, 1999 by Ralf Baechle
  9. * Copyright (C) 1999 Silicon Graphics, Inc.
  10. */
  11. #ifndef _ASM_MMU_CONTEXT_H
  12. #define _ASM_MMU_CONTEXT_H
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/smp.h>
  16. #include <linux/slab.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/hazards.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm-generic/mm_hooks.h>
  21. #define htw_set_pwbase(pgd) \
  22. do { \
  23. if (cpu_has_htw) { \
  24. write_c0_pwbase(pgd); \
  25. back_to_back_c0_hazard(); \
  26. } \
  27. } while (0)
  28. #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
  29. do { \
  30. extern void tlbmiss_handler_setup_pgd(unsigned long); \
  31. tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
  32. htw_set_pwbase((unsigned long)pgd); \
  33. } while (0)
  34. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  35. #define TLBMISS_HANDLER_RESTORE() \
  36. write_c0_xcontext((unsigned long) smp_processor_id() << \
  37. SMP_CPUID_REGSHIFT)
  38. #define TLBMISS_HANDLER_SETUP() \
  39. do { \
  40. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
  41. TLBMISS_HANDLER_RESTORE(); \
  42. } while (0)
  43. #else /* !CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
  44. /*
  45. * For the fast tlb miss handlers, we keep a per cpu array of pointers
  46. * to the current pgd for each processor. Also, the proc. id is stuffed
  47. * into the context register.
  48. */
  49. extern unsigned long pgd_current[];
  50. #define TLBMISS_HANDLER_RESTORE() \
  51. write_c0_context((unsigned long) smp_processor_id() << \
  52. SMP_CPUID_REGSHIFT)
  53. #define TLBMISS_HANDLER_SETUP() \
  54. TLBMISS_HANDLER_RESTORE(); \
  55. back_to_back_c0_hazard(); \
  56. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  57. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
  58. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  59. #define ASID_INC 0x40
  60. #define ASID_MASK 0xfc0
  61. #elif defined(CONFIG_CPU_R8000)
  62. #define ASID_INC 0x10
  63. #define ASID_MASK 0xff0
  64. #else /* FIXME: not correct for R6000 */
  65. #define ASID_INC 0x1
  66. #define ASID_MASK 0xff
  67. #endif
  68. #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
  69. #define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & ASID_MASK)
  70. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  71. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  72. {
  73. }
  74. /*
  75. * All unused by hardware upper bits will be considered
  76. * as a software asid extension.
  77. */
  78. #define ASID_VERSION_MASK ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
  79. #define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
  80. /* Normal, classic MIPS get_new_mmu_context */
  81. static inline void
  82. get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
  83. {
  84. extern void kvm_local_flush_tlb_all(void);
  85. unsigned long asid = asid_cache(cpu);
  86. if (! ((asid += ASID_INC) & ASID_MASK) ) {
  87. if (cpu_has_vtag_icache)
  88. flush_icache_all();
  89. #ifdef CONFIG_KVM
  90. kvm_local_flush_tlb_all(); /* start new asid cycle */
  91. #else
  92. local_flush_tlb_all(); /* start new asid cycle */
  93. #endif
  94. if (!asid) /* fix version if needed */
  95. asid = ASID_FIRST_VERSION;
  96. }
  97. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  98. }
  99. /*
  100. * Initialize the context related info for a new mm_struct
  101. * instance.
  102. */
  103. static inline int
  104. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  105. {
  106. int i;
  107. for_each_possible_cpu(i)
  108. cpu_context(i, mm) = 0;
  109. return 0;
  110. }
  111. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  112. struct task_struct *tsk)
  113. {
  114. unsigned int cpu = smp_processor_id();
  115. unsigned long flags;
  116. local_irq_save(flags);
  117. htw_stop();
  118. /* Check if our ASID is of an older version and thus invalid */
  119. if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
  120. get_new_mmu_context(next, cpu);
  121. write_c0_entryhi(cpu_asid(cpu, next));
  122. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  123. /*
  124. * Mark current->active_mm as not "active" anymore.
  125. * We don't want to mislead possible IPI tlb flush routines.
  126. */
  127. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  128. cpumask_set_cpu(cpu, mm_cpumask(next));
  129. htw_start();
  130. local_irq_restore(flags);
  131. }
  132. /*
  133. * Destroy context related info for an mm_struct that is about
  134. * to be put to rest.
  135. */
  136. static inline void destroy_context(struct mm_struct *mm)
  137. {
  138. }
  139. #define deactivate_mm(tsk, mm) do { } while (0)
  140. /*
  141. * After we have set current->mm to a new value, this activates
  142. * the context for the new mm so we see the new mappings.
  143. */
  144. static inline void
  145. activate_mm(struct mm_struct *prev, struct mm_struct *next)
  146. {
  147. unsigned long flags;
  148. unsigned int cpu = smp_processor_id();
  149. local_irq_save(flags);
  150. htw_stop();
  151. /* Unconditionally get a new ASID. */
  152. get_new_mmu_context(next, cpu);
  153. write_c0_entryhi(cpu_asid(cpu, next));
  154. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  155. /* mark mmu ownership change */
  156. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  157. cpumask_set_cpu(cpu, mm_cpumask(next));
  158. htw_start();
  159. local_irq_restore(flags);
  160. }
  161. /*
  162. * If mm is currently active_mm, we can't really drop it. Instead,
  163. * we will get a new one for it.
  164. */
  165. static inline void
  166. drop_mmu_context(struct mm_struct *mm, unsigned cpu)
  167. {
  168. unsigned long flags;
  169. local_irq_save(flags);
  170. htw_stop();
  171. if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
  172. get_new_mmu_context(mm, cpu);
  173. write_c0_entryhi(cpu_asid(cpu, mm));
  174. } else {
  175. /* will get a new context next time */
  176. cpu_context(cpu, mm) = 0;
  177. }
  178. htw_start();
  179. local_irq_restore(flags);
  180. }
  181. #endif /* _ASM_MMU_CONTEXT_H */