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/mm_types.h>
  16. #include <linux/smp.h>
  17. #include <linux/slab.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/dsemul.h>
  20. #include <asm/hazards.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm-generic/mm_hooks.h>
  23. #define htw_set_pwbase(pgd) \
  24. do { \
  25. if (cpu_has_htw) { \
  26. write_c0_pwbase(pgd); \
  27. back_to_back_c0_hazard(); \
  28. } \
  29. } while (0)
  30. extern void tlbmiss_handler_setup_pgd(unsigned long);
  31. /* Note: This is also implemented with uasm in arch/mips/kvm/entry.c */
  32. #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
  33. do { \
  34. tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
  35. htw_set_pwbase((unsigned long)pgd); \
  36. } while (0)
  37. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  38. #define TLBMISS_HANDLER_RESTORE() \
  39. write_c0_xcontext((unsigned long) smp_processor_id() << \
  40. SMP_CPUID_REGSHIFT)
  41. #define TLBMISS_HANDLER_SETUP() \
  42. do { \
  43. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
  44. TLBMISS_HANDLER_RESTORE(); \
  45. } while (0)
  46. #else /* !CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
  47. /*
  48. * For the fast tlb miss handlers, we keep a per cpu array of pointers
  49. * to the current pgd for each processor. Also, the proc. id is stuffed
  50. * into the context register.
  51. */
  52. extern unsigned long pgd_current[];
  53. #define TLBMISS_HANDLER_RESTORE() \
  54. write_c0_context((unsigned long) smp_processor_id() << \
  55. SMP_CPUID_REGSHIFT)
  56. #define TLBMISS_HANDLER_SETUP() \
  57. TLBMISS_HANDLER_RESTORE(); \
  58. back_to_back_c0_hazard(); \
  59. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  60. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
  61. /*
  62. * All unused by hardware upper bits will be considered
  63. * as a software asid extension.
  64. */
  65. static unsigned long asid_version_mask(unsigned int cpu)
  66. {
  67. unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
  68. return ~(asid_mask | (asid_mask - 1));
  69. }
  70. static unsigned long asid_first_version(unsigned int cpu)
  71. {
  72. return ~asid_version_mask(cpu) + 1;
  73. }
  74. #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
  75. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  76. #define cpu_asid(cpu, mm) \
  77. (cpu_context((cpu), (mm)) & cpu_asid_mask(&cpu_data[cpu]))
  78. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  79. {
  80. }
  81. /* Normal, classic MIPS get_new_mmu_context */
  82. static inline void
  83. get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
  84. {
  85. unsigned long asid = asid_cache(cpu);
  86. if (!((asid += cpu_asid_inc()) & cpu_asid_mask(&cpu_data[cpu]))) {
  87. if (cpu_has_vtag_icache)
  88. flush_icache_all();
  89. local_flush_tlb_all(); /* start new asid cycle */
  90. if (!asid) /* fix version if needed */
  91. asid = asid_first_version(cpu);
  92. }
  93. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  94. }
  95. /*
  96. * Initialize the context related info for a new mm_struct
  97. * instance.
  98. */
  99. static inline int
  100. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  101. {
  102. int i;
  103. for_each_possible_cpu(i)
  104. cpu_context(i, mm) = 0;
  105. mm->context.bd_emupage_allocmap = NULL;
  106. spin_lock_init(&mm->context.bd_emupage_lock);
  107. init_waitqueue_head(&mm->context.bd_emupage_queue);
  108. return 0;
  109. }
  110. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  111. struct task_struct *tsk)
  112. {
  113. unsigned int cpu = smp_processor_id();
  114. unsigned long flags;
  115. local_irq_save(flags);
  116. htw_stop();
  117. /* Check if our ASID is of an older version and thus invalid */
  118. if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & asid_version_mask(cpu))
  119. get_new_mmu_context(next, cpu);
  120. write_c0_entryhi(cpu_asid(cpu, next));
  121. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  122. /*
  123. * Mark current->active_mm as not "active" anymore.
  124. * We don't want to mislead possible IPI tlb flush routines.
  125. */
  126. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  127. cpumask_set_cpu(cpu, mm_cpumask(next));
  128. htw_start();
  129. local_irq_restore(flags);
  130. }
  131. /*
  132. * Destroy context related info for an mm_struct that is about
  133. * to be put to rest.
  134. */
  135. static inline void destroy_context(struct mm_struct *mm)
  136. {
  137. dsemul_mm_cleanup(mm);
  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 */