mmu_context.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (C) 1999 Niibe Yutaka
  3. * Copyright (C) 2003 - 2006 Paul Mundt
  4. *
  5. * ASID handling idea taken from MIPS implementation.
  6. */
  7. #ifndef __ASM_SH_MMU_CONTEXT_H
  8. #define __ASM_SH_MMU_CONTEXT_H
  9. #ifdef __KERNEL__
  10. #include <asm/cpu/mmu_context.h>
  11. #include <asm/tlbflush.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/io.h>
  14. #include <asm-generic/mm_hooks.h>
  15. /*
  16. * The MMU "context" consists of two things:
  17. * (a) TLB cache version (or round, cycle whatever expression you like)
  18. * (b) ASID (Address Space IDentifier)
  19. */
  20. #define MMU_CONTEXT_ASID_MASK 0x000000ff
  21. #define MMU_CONTEXT_VERSION_MASK 0xffffff00
  22. #define MMU_CONTEXT_FIRST_VERSION 0x00000100
  23. #define NO_CONTEXT 0
  24. /* ASID is 8-bit value, so it can't be 0x100 */
  25. #define MMU_NO_ASID 0x100
  26. #define cpu_context(cpu, mm) ((mm)->context.id[cpu])
  27. #define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & \
  28. MMU_CONTEXT_ASID_MASK)
  29. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  30. /*
  31. * Virtual Page Number mask
  32. */
  33. #define MMU_VPN_MASK 0xfffff000
  34. #ifdef CONFIG_MMU
  35. /*
  36. * Get MMU context if needed.
  37. */
  38. static inline void get_mmu_context(struct mm_struct *mm, unsigned int cpu)
  39. {
  40. unsigned long asid = asid_cache(cpu);
  41. /* Check if we have old version of context. */
  42. if (((cpu_context(cpu, mm) ^ asid) & MMU_CONTEXT_VERSION_MASK) == 0)
  43. /* It's up to date, do nothing */
  44. return;
  45. /* It's old, we need to get new context with new version. */
  46. if (!(++asid & MMU_CONTEXT_ASID_MASK)) {
  47. /*
  48. * We exhaust ASID of this version.
  49. * Flush all TLB and start new cycle.
  50. */
  51. flush_tlb_all();
  52. /*
  53. * Fix version; Note that we avoid version #0
  54. * to distingush NO_CONTEXT.
  55. */
  56. if (!asid)
  57. asid = MMU_CONTEXT_FIRST_VERSION;
  58. }
  59. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  60. }
  61. /*
  62. * Initialize the context related info for a new mm_struct
  63. * instance.
  64. */
  65. static inline int init_new_context(struct task_struct *tsk,
  66. struct mm_struct *mm)
  67. {
  68. int i;
  69. for (i = 0; i < num_online_cpus(); i++)
  70. cpu_context(i, mm) = NO_CONTEXT;
  71. return 0;
  72. }
  73. /*
  74. * Destroy context related info for an mm_struct that is about
  75. * to be put to rest.
  76. */
  77. static inline void destroy_context(struct mm_struct *mm)
  78. {
  79. /* Do nothing */
  80. }
  81. static inline void set_asid(unsigned long asid)
  82. {
  83. unsigned long __dummy;
  84. __asm__ __volatile__ ("mov.l %2, %0\n\t"
  85. "and %3, %0\n\t"
  86. "or %1, %0\n\t"
  87. "mov.l %0, %2"
  88. : "=&r" (__dummy)
  89. : "r" (asid), "m" (__m(MMU_PTEH)),
  90. "r" (0xffffff00));
  91. }
  92. static inline unsigned long get_asid(void)
  93. {
  94. unsigned long asid;
  95. __asm__ __volatile__ ("mov.l %1, %0"
  96. : "=r" (asid)
  97. : "m" (__m(MMU_PTEH)));
  98. asid &= MMU_CONTEXT_ASID_MASK;
  99. return asid;
  100. }
  101. /*
  102. * After we have set current->mm to a new value, this activates
  103. * the context for the new mm so we see the new mappings.
  104. */
  105. static inline void activate_context(struct mm_struct *mm, unsigned int cpu)
  106. {
  107. get_mmu_context(mm, cpu);
  108. set_asid(cpu_asid(cpu, mm));
  109. }
  110. /* MMU_TTB is used for optimizing the fault handling. */
  111. static inline void set_TTB(pgd_t *pgd)
  112. {
  113. ctrl_outl((unsigned long)pgd, MMU_TTB);
  114. }
  115. static inline pgd_t *get_TTB(void)
  116. {
  117. return (pgd_t *)ctrl_inl(MMU_TTB);
  118. }
  119. static inline void switch_mm(struct mm_struct *prev,
  120. struct mm_struct *next,
  121. struct task_struct *tsk)
  122. {
  123. unsigned int cpu = smp_processor_id();
  124. if (likely(prev != next)) {
  125. cpu_set(cpu, next->cpu_vm_mask);
  126. set_TTB(next->pgd);
  127. activate_context(next, cpu);
  128. } else
  129. if (!cpu_test_and_set(cpu, next->cpu_vm_mask))
  130. activate_context(next, cpu);
  131. }
  132. #define deactivate_mm(tsk,mm) do { } while (0)
  133. #define activate_mm(prev, next) \
  134. switch_mm((prev),(next),NULL)
  135. static inline void
  136. enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  137. {
  138. }
  139. #else /* !CONFIG_MMU */
  140. #define get_mmu_context(mm) do { } while (0)
  141. #define init_new_context(tsk,mm) (0)
  142. #define destroy_context(mm) do { } while (0)
  143. #define set_asid(asid) do { } while (0)
  144. #define get_asid() (0)
  145. #define set_TTB(pgd) do { } while (0)
  146. #define get_TTB() (0)
  147. #define activate_context(mm,cpu) do { } while (0)
  148. #define switch_mm(prev,next,tsk) do { } while (0)
  149. #define deactivate_mm(tsk,mm) do { } while (0)
  150. #define activate_mm(prev,next) do { } while (0)
  151. #define enter_lazy_tlb(mm,tsk) do { } while (0)
  152. #endif /* CONFIG_MMU */
  153. #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
  154. /*
  155. * If this processor has an MMU, we need methods to turn it off/on ..
  156. * paging_init() will also have to be updated for the processor in
  157. * question.
  158. */
  159. static inline void enable_mmu(void)
  160. {
  161. unsigned int cpu = smp_processor_id();
  162. /* Enable MMU */
  163. ctrl_outl(MMU_CONTROL_INIT, MMUCR);
  164. ctrl_barrier();
  165. if (asid_cache(cpu) == NO_CONTEXT)
  166. asid_cache(cpu) = MMU_CONTEXT_FIRST_VERSION;
  167. set_asid(asid_cache(cpu) & MMU_CONTEXT_ASID_MASK);
  168. }
  169. static inline void disable_mmu(void)
  170. {
  171. unsigned long cr;
  172. cr = ctrl_inl(MMUCR);
  173. cr &= ~MMU_CONTROL_INIT;
  174. ctrl_outl(cr, MMUCR);
  175. ctrl_barrier();
  176. }
  177. #else
  178. /*
  179. * MMU control handlers for processors lacking memory
  180. * management hardware.
  181. */
  182. #define enable_mmu() do { } while (0)
  183. #define disable_mmu() do { } while (0)
  184. #endif
  185. #endif /* __KERNEL__ */
  186. #endif /* __ASM_SH_MMU_CONTEXT_H */