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