mmu_context.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/config.h>
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/tlbflush.h>
  19. /*
  20. * For the fast tlb miss handlers, we keep a per cpu array of pointers
  21. * to the current pgd for each processor. Also, the proc. id is stuffed
  22. * into the context register.
  23. */
  24. extern unsigned long pgd_current[];
  25. #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
  26. pgd_current[smp_processor_id()] = (unsigned long)(pgd)
  27. #ifdef CONFIG_32BIT
  28. #define TLBMISS_HANDLER_SETUP() \
  29. write_c0_context((unsigned long) smp_processor_id() << 25); \
  30. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  31. #endif
  32. #if defined(CONFIG_64BIT) && !defined(CONFIG_BUILD_ELF64)
  33. #define TLBMISS_HANDLER_SETUP() \
  34. write_c0_context((unsigned long) &pgd_current[smp_processor_id()] << 23); \
  35. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  36. #endif
  37. #if defined(CONFIG_64BIT) && defined(CONFIG_BUILD_ELF64)
  38. #define TLBMISS_HANDLER_SETUP() \
  39. write_c0_context((unsigned long) smp_processor_id() << 26); \
  40. TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
  41. #endif
  42. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  43. #define ASID_INC 0x40
  44. #define ASID_MASK 0xfc0
  45. #elif defined(CONFIG_CPU_R8000)
  46. #define ASID_INC 0x10
  47. #define ASID_MASK 0xff0
  48. #elif defined(CONFIG_CPU_RM9000)
  49. #define ASID_INC 0x1
  50. #define ASID_MASK 0xfff
  51. #else /* FIXME: not correct for R6000 */
  52. #define ASID_INC 0x1
  53. #define ASID_MASK 0xff
  54. #endif
  55. #define cpu_context(cpu, mm) ((mm)->context[cpu])
  56. #define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & ASID_MASK)
  57. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  58. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  59. {
  60. }
  61. /*
  62. * All unused by hardware upper bits will be considered
  63. * as a software asid extension.
  64. */
  65. #define ASID_VERSION_MASK ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
  66. #define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
  67. static inline void
  68. get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
  69. {
  70. unsigned long asid = asid_cache(cpu);
  71. if (! ((asid += ASID_INC) & ASID_MASK) ) {
  72. if (cpu_has_vtag_icache)
  73. flush_icache_all();
  74. local_flush_tlb_all(); /* start new asid cycle */
  75. if (!asid) /* fix version if needed */
  76. asid = ASID_FIRST_VERSION;
  77. }
  78. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  79. }
  80. /*
  81. * Initialize the context related info for a new mm_struct
  82. * instance.
  83. */
  84. static inline int
  85. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  86. {
  87. int i;
  88. for (i = 0; i < num_online_cpus(); i++)
  89. cpu_context(i, mm) = 0;
  90. return 0;
  91. }
  92. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  93. struct task_struct *tsk)
  94. {
  95. unsigned int cpu = smp_processor_id();
  96. unsigned long flags;
  97. local_irq_save(flags);
  98. /* Check if our ASID is of an older version and thus invalid */
  99. if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
  100. get_new_mmu_context(next, cpu);
  101. write_c0_entryhi(cpu_context(cpu, next));
  102. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  103. /*
  104. * Mark current->active_mm as not "active" anymore.
  105. * We don't want to mislead possible IPI tlb flush routines.
  106. */
  107. cpu_clear(cpu, prev->cpu_vm_mask);
  108. cpu_set(cpu, next->cpu_vm_mask);
  109. local_irq_restore(flags);
  110. }
  111. /*
  112. * Destroy context related info for an mm_struct that is about
  113. * to be put to rest.
  114. */
  115. static inline void destroy_context(struct mm_struct *mm)
  116. {
  117. }
  118. #define deactivate_mm(tsk,mm) do { } while (0)
  119. /*
  120. * After we have set current->mm to a new value, this activates
  121. * the context for the new mm so we see the new mappings.
  122. */
  123. static inline void
  124. activate_mm(struct mm_struct *prev, struct mm_struct *next)
  125. {
  126. unsigned long flags;
  127. unsigned int cpu = smp_processor_id();
  128. local_irq_save(flags);
  129. /* Unconditionally get a new ASID. */
  130. get_new_mmu_context(next, cpu);
  131. write_c0_entryhi(cpu_context(cpu, next));
  132. TLBMISS_HANDLER_SETUP_PGD(next->pgd);
  133. /* mark mmu ownership change */
  134. cpu_clear(cpu, prev->cpu_vm_mask);
  135. cpu_set(cpu, next->cpu_vm_mask);
  136. local_irq_restore(flags);
  137. }
  138. /*
  139. * If mm is currently active_mm, we can't really drop it. Instead,
  140. * we will get a new one for it.
  141. */
  142. static inline void
  143. drop_mmu_context(struct mm_struct *mm, unsigned cpu)
  144. {
  145. unsigned long flags;
  146. local_irq_save(flags);
  147. if (cpu_isset(cpu, mm->cpu_vm_mask)) {
  148. get_new_mmu_context(mm, cpu);
  149. write_c0_entryhi(cpu_asid(cpu, mm));
  150. } else {
  151. /* will get a new context next time */
  152. cpu_context(cpu, mm) = 0;
  153. }
  154. local_irq_restore(flags);
  155. }
  156. #endif /* _ASM_MMU_CONTEXT_H */