mmu_context.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/barrier.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/dsemul.h>
  21. #include <asm/ginvt.h>
  22. #include <asm/hazards.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm-generic/mm_hooks.h>
  25. #define htw_set_pwbase(pgd) \
  26. do { \
  27. if (cpu_has_htw) { \
  28. write_c0_pwbase(pgd); \
  29. back_to_back_c0_hazard(); \
  30. } \
  31. } while (0)
  32. extern void tlbmiss_handler_setup_pgd(unsigned long);
  33. extern char tlbmiss_handler_setup_pgd_end[];
  34. /* Note: This is also implemented with uasm in arch/mips/kvm/entry.c */
  35. #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
  36. do { \
  37. tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
  38. htw_set_pwbase((unsigned long)pgd); \
  39. } while (0)
  40. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  41. #define TLBMISS_HANDLER_RESTORE() \
  42. write_c0_xcontext((unsigned long) smp_processor_id() << \
  43. SMP_CPUID_REGSHIFT)
  44. #define TLBMISS_HANDLER_SETUP() \
  45. do { \
  46. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
  47. TLBMISS_HANDLER_RESTORE(); \
  48. } while (0)
  49. #else /* !CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
  50. /*
  51. * For the fast tlb miss handlers, we keep a per cpu array of pointers
  52. * to the current pgd for each processor. Also, the proc. id is stuffed
  53. * into the context register.
  54. */
  55. extern unsigned long pgd_current[];
  56. #define TLBMISS_HANDLER_RESTORE() \
  57. write_c0_context((unsigned long) smp_processor_id() << \
  58. SMP_CPUID_REGSHIFT)
  59. #define TLBMISS_HANDLER_SETUP() \
  60. TLBMISS_HANDLER_RESTORE(); \
  61. back_to_back_c0_hazard(); \
  62. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  63. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
  64. /*
  65. * The ginvt instruction will invalidate wired entries when its type field
  66. * targets anything other than the entire TLB. That means that if we were to
  67. * allow the kernel to create wired entries with the MMID of current->active_mm
  68. * then those wired entries could be invalidated when we later use ginvt to
  69. * invalidate TLB entries with that MMID.
  70. *
  71. * In order to prevent ginvt from trashing wired entries, we reserve one MMID
  72. * for use by the kernel when creating wired entries. This MMID will never be
  73. * assigned to a struct mm, and we'll never target it with a ginvt instruction.
  74. */
  75. #define MMID_KERNEL_WIRED 0
  76. /*
  77. * All unused by hardware upper bits will be considered
  78. * as a software asid extension.
  79. */
  80. static inline u64 asid_version_mask(unsigned int cpu)
  81. {
  82. unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
  83. return ~(u64)(asid_mask | (asid_mask - 1));
  84. }
  85. static inline u64 asid_first_version(unsigned int cpu)
  86. {
  87. return ~asid_version_mask(cpu) + 1;
  88. }
  89. static inline u64 cpu_context(unsigned int cpu, const struct mm_struct *mm)
  90. {
  91. if (cpu_has_mmid)
  92. return atomic64_read(&mm->context.mmid);
  93. return mm->context.asid[cpu];
  94. }
  95. static inline void set_cpu_context(unsigned int cpu,
  96. struct mm_struct *mm, u64 ctx)
  97. {
  98. if (cpu_has_mmid)
  99. atomic64_set(&mm->context.mmid, ctx);
  100. else
  101. mm->context.asid[cpu] = ctx;
  102. }
  103. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  104. #define cpu_asid(cpu, mm) \
  105. (cpu_context((cpu), (mm)) & cpu_asid_mask(&cpu_data[cpu]))
  106. extern void get_new_mmu_context(struct mm_struct *mm);
  107. extern void check_mmu_context(struct mm_struct *mm);
  108. extern void check_switch_mmu_context(struct mm_struct *mm);
  109. /*
  110. * Initialize the context related info for a new mm_struct
  111. * instance.
  112. */
  113. #define init_new_context init_new_context
  114. static inline int
  115. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  116. {
  117. int i;
  118. if (cpu_has_mmid) {
  119. set_cpu_context(0, mm, 0);
  120. } else {
  121. for_each_possible_cpu(i)
  122. set_cpu_context(i, mm, 0);
  123. }
  124. mm->context.bd_emupage_allocmap = NULL;
  125. spin_lock_init(&mm->context.bd_emupage_lock);
  126. init_waitqueue_head(&mm->context.bd_emupage_queue);
  127. return 0;
  128. }
  129. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  130. struct task_struct *tsk)
  131. {
  132. unsigned int cpu = smp_processor_id();
  133. unsigned long flags;
  134. local_irq_save(flags);
  135. htw_stop();
  136. check_switch_mmu_context(next);
  137. /*
  138. * Mark current->active_mm as not "active" anymore.
  139. * We don't want to mislead possible IPI tlb flush routines.
  140. */
  141. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  142. cpumask_set_cpu(cpu, mm_cpumask(next));
  143. htw_start();
  144. local_irq_restore(flags);
  145. }
  146. /*
  147. * Destroy context related info for an mm_struct that is about
  148. * to be put to rest.
  149. */
  150. #define destroy_context destroy_context
  151. static inline void destroy_context(struct mm_struct *mm)
  152. {
  153. dsemul_mm_cleanup(mm);
  154. }
  155. static inline void
  156. drop_mmu_context(struct mm_struct *mm)
  157. {
  158. unsigned long flags;
  159. unsigned int cpu;
  160. u32 old_mmid;
  161. u64 ctx;
  162. local_irq_save(flags);
  163. cpu = smp_processor_id();
  164. ctx = cpu_context(cpu, mm);
  165. if (!ctx) {
  166. /* no-op */
  167. } else if (cpu_has_mmid) {
  168. /*
  169. * Globally invalidating TLB entries associated with the MMID
  170. * is pretty cheap using the GINVT instruction, so we'll do
  171. * that rather than incur the overhead of allocating a new
  172. * MMID. The latter would be especially difficult since MMIDs
  173. * are global & other CPUs may be actively using ctx.
  174. */
  175. htw_stop();
  176. old_mmid = read_c0_memorymapid();
  177. write_c0_memorymapid(ctx & cpu_asid_mask(&cpu_data[cpu]));
  178. mtc0_tlbw_hazard();
  179. ginvt_mmid();
  180. sync_ginv();
  181. write_c0_memorymapid(old_mmid);
  182. instruction_hazard();
  183. htw_start();
  184. } else if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
  185. /*
  186. * mm is currently active, so we can't really drop it.
  187. * Instead we bump the ASID.
  188. */
  189. htw_stop();
  190. get_new_mmu_context(mm);
  191. write_c0_entryhi(cpu_asid(cpu, mm));
  192. htw_start();
  193. } else {
  194. /* will get a new context next time */
  195. set_cpu_context(cpu, mm, 0);
  196. }
  197. local_irq_restore(flags);
  198. }
  199. #include <asm-generic/mmu_context.h>
  200. #endif /* _ASM_MMU_CONTEXT_H */