mmu_context.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/dsemul.h>
  19. #include <asm/hazards.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm-generic/mm_hooks.h>
  22. #define htw_set_pwbase(pgd) \
  23. do { \
  24. if (cpu_has_htw) { \
  25. write_c0_pwbase(pgd); \
  26. back_to_back_c0_hazard(); \
  27. } \
  28. } while (0)
  29. #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
  30. do { \
  31. extern void tlbmiss_handler_setup_pgd(unsigned long); \
  32. tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
  33. htw_set_pwbase((unsigned long)pgd); \
  34. } while (0)
  35. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  36. #define TLBMISS_HANDLER_RESTORE() \
  37. write_c0_xcontext((unsigned long) smp_processor_id() << \
  38. SMP_CPUID_REGSHIFT)
  39. #define TLBMISS_HANDLER_SETUP() \
  40. do { \
  41. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
  42. TLBMISS_HANDLER_RESTORE(); \
  43. } while (0)
  44. #else /* !CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
  45. /*
  46. * For the fast tlb miss handlers, we keep a per cpu array of pointers
  47. * to the current pgd for each processor. Also, the proc. id is stuffed
  48. * into the context register.
  49. */
  50. extern unsigned long pgd_current[];
  51. #define TLBMISS_HANDLER_RESTORE() \
  52. write_c0_context((unsigned long) smp_processor_id() << \
  53. SMP_CPUID_REGSHIFT)
  54. #define TLBMISS_HANDLER_SETUP() \
  55. TLBMISS_HANDLER_RESTORE(); \
  56. back_to_back_c0_hazard(); \
  57. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  58. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
  59. /*
  60. * All unused by hardware upper bits will be considered
  61. * as a software asid extension.
  62. */
  63. static unsigned long asid_version_mask(unsigned int cpu)
  64. {
  65. unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
  66. return ~(asid_mask | (asid_mask - 1));
  67. }
  68. static unsigned long asid_first_version(unsigned int cpu)
  69. {
  70. return ~asid_version_mask(cpu) + 1;
  71. }
  72. #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
  73. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  74. #define cpu_asid(cpu, mm) \
  75. (cpu_context((cpu), (mm)) & cpu_asid_mask(&cpu_data[cpu]))
  76. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  77. {
  78. }
  79. /* Normal, classic MIPS get_new_mmu_context */
  80. static inline void
  81. get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
  82. {
  83. extern void kvm_local_flush_tlb_all(void);
  84. unsigned long asid = asid_cache(cpu);
  85. if (!((asid += cpu_asid_inc()) & cpu_asid_mask(&cpu_data[cpu]))) {
  86. if (cpu_has_vtag_icache)
  87. flush_icache_all();
  88. #ifdef CONFIG_KVM
  89. kvm_local_flush_tlb_all(); /* start new asid cycle */
  90. #else
  91. local_flush_tlb_all(); /* start new asid cycle */
  92. #endif
  93. if (!asid) /* fix version if needed */
  94. asid = asid_first_version(cpu);
  95. }
  96. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  97. }
  98. /*
  99. * Initialize the context related info for a new mm_struct
  100. * instance.
  101. */
  102. static inline int
  103. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  104. {
  105. int i;
  106. for_each_possible_cpu(i)
  107. cpu_context(i, mm) = 0;
  108. atomic_set(&mm->context.fp_mode_switching, 0);
  109. mm->context.bd_emupage_allocmap = NULL;
  110. spin_lock_init(&mm->context.bd_emupage_lock);
  111. init_waitqueue_head(&mm->context.bd_emupage_queue);
  112. return 0;
  113. }
  114. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  115. struct task_struct *tsk)
  116. {
  117. unsigned int cpu = smp_processor_id();
  118. unsigned long flags;
  119. local_irq_save(flags);
  120. htw_stop();
  121. /* Check if our ASID is of an older version and thus invalid */
  122. if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & asid_version_mask(cpu))
  123. get_new_mmu_context(next, cpu);
  124. write_c0_entryhi(cpu_asid(cpu, next));
  125. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  126. /*
  127. * Mark current->active_mm as not "active" anymore.
  128. * We don't want to mislead possible IPI tlb flush routines.
  129. */
  130. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  131. cpumask_set_cpu(cpu, mm_cpumask(next));
  132. htw_start();
  133. local_irq_restore(flags);
  134. }
  135. /*
  136. * Destroy context related info for an mm_struct that is about
  137. * to be put to rest.
  138. */
  139. static inline void destroy_context(struct mm_struct *mm)
  140. {
  141. dsemul_mm_cleanup(mm);
  142. }
  143. #define deactivate_mm(tsk, mm) do { } while (0)
  144. /*
  145. * After we have set current->mm to a new value, this activates
  146. * the context for the new mm so we see the new mappings.
  147. */
  148. static inline void
  149. activate_mm(struct mm_struct *prev, struct mm_struct *next)
  150. {
  151. unsigned long flags;
  152. unsigned int cpu = smp_processor_id();
  153. local_irq_save(flags);
  154. htw_stop();
  155. /* Unconditionally get a new ASID. */
  156. get_new_mmu_context(next, cpu);
  157. write_c0_entryhi(cpu_asid(cpu, next));
  158. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  159. /* mark mmu ownership change */
  160. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  161. cpumask_set_cpu(cpu, mm_cpumask(next));
  162. htw_start();
  163. local_irq_restore(flags);
  164. }
  165. /*
  166. * If mm is currently active_mm, we can't really drop it. Instead,
  167. * we will get a new one for it.
  168. */
  169. static inline void
  170. drop_mmu_context(struct mm_struct *mm, unsigned cpu)
  171. {
  172. unsigned long flags;
  173. local_irq_save(flags);
  174. htw_stop();
  175. if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
  176. get_new_mmu_context(mm, cpu);
  177. write_c0_entryhi(cpu_asid(cpu, mm));
  178. } else {
  179. /* will get a new context next time */
  180. cpu_context(cpu, mm) = 0;
  181. }
  182. htw_start();
  183. local_irq_restore(flags);
  184. }
  185. #endif /* _ASM_MMU_CONTEXT_H */