mmu_context.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 1999 Niibe Yutaka
  4. * Copyright (C) 2003 - 2007 Paul Mundt
  5. *
  6. * ASID handling idea taken from MIPS implementation.
  7. */
  8. #ifndef __ASM_SH_MMU_CONTEXT_H
  9. #define __ASM_SH_MMU_CONTEXT_H
  10. #ifdef __KERNEL__
  11. #include <cpu/mmu_context.h>
  12. #include <asm/tlbflush.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/mm_types.h>
  15. #include <asm/io.h>
  16. #include <asm-generic/mm_hooks.h>
  17. /*
  18. * The MMU "context" consists of two things:
  19. * (a) TLB cache version (or round, cycle whatever expression you like)
  20. * (b) ASID (Address Space IDentifier)
  21. */
  22. #ifdef CONFIG_CPU_HAS_PTEAEX
  23. #define MMU_CONTEXT_ASID_MASK 0x0000ffff
  24. #else
  25. #define MMU_CONTEXT_ASID_MASK 0x000000ff
  26. #endif
  27. #define MMU_CONTEXT_VERSION_MASK (~0UL & ~MMU_CONTEXT_ASID_MASK)
  28. #define MMU_CONTEXT_FIRST_VERSION (MMU_CONTEXT_ASID_MASK + 1)
  29. /* Impossible ASID value, to differentiate from NO_CONTEXT. */
  30. #define MMU_NO_ASID MMU_CONTEXT_FIRST_VERSION
  31. #define NO_CONTEXT 0UL
  32. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  33. #ifdef CONFIG_MMU
  34. #define cpu_context(cpu, mm) ((mm)->context.id[cpu])
  35. #define cpu_asid(cpu, mm) \
  36. (cpu_context((cpu), (mm)) & MMU_CONTEXT_ASID_MASK)
  37. /*
  38. * Virtual Page Number mask
  39. */
  40. #define MMU_VPN_MASK 0xfffff000
  41. #if defined(CONFIG_SUPERH32)
  42. #include <asm/mmu_context_32.h>
  43. #else
  44. #include <asm/mmu_context_64.h>
  45. #endif
  46. /*
  47. * Get MMU context if needed.
  48. */
  49. static inline void get_mmu_context(struct mm_struct *mm, unsigned int cpu)
  50. {
  51. unsigned long asid = asid_cache(cpu);
  52. /* Check if we have old version of context. */
  53. if (((cpu_context(cpu, mm) ^ asid) & MMU_CONTEXT_VERSION_MASK) == 0)
  54. /* It's up to date, do nothing */
  55. return;
  56. /* It's old, we need to get new context with new version. */
  57. if (!(++asid & MMU_CONTEXT_ASID_MASK)) {
  58. /*
  59. * We exhaust ASID of this version.
  60. * Flush all TLB and start new cycle.
  61. */
  62. local_flush_tlb_all();
  63. #ifdef CONFIG_SUPERH64
  64. /*
  65. * The SH-5 cache uses the ASIDs, requiring both the I and D
  66. * cache to be flushed when the ASID is exhausted. Weak.
  67. */
  68. flush_cache_all();
  69. #endif
  70. /*
  71. * Fix version; Note that we avoid version #0
  72. * to distinguish NO_CONTEXT.
  73. */
  74. if (!asid)
  75. asid = MMU_CONTEXT_FIRST_VERSION;
  76. }
  77. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  78. }
  79. /*
  80. * Initialize the context related info for a new mm_struct
  81. * instance.
  82. */
  83. static inline int init_new_context(struct task_struct *tsk,
  84. struct mm_struct *mm)
  85. {
  86. int i;
  87. for_each_online_cpu(i)
  88. cpu_context(i, mm) = NO_CONTEXT;
  89. return 0;
  90. }
  91. /*
  92. * After we have set current->mm to a new value, this activates
  93. * the context for the new mm so we see the new mappings.
  94. */
  95. static inline void activate_context(struct mm_struct *mm, unsigned int cpu)
  96. {
  97. get_mmu_context(mm, cpu);
  98. set_asid(cpu_asid(cpu, mm));
  99. }
  100. static inline void switch_mm(struct mm_struct *prev,
  101. struct mm_struct *next,
  102. struct task_struct *tsk)
  103. {
  104. unsigned int cpu = smp_processor_id();
  105. if (likely(prev != next)) {
  106. cpumask_set_cpu(cpu, mm_cpumask(next));
  107. set_TTB(next->pgd);
  108. activate_context(next, cpu);
  109. } else
  110. if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next)))
  111. activate_context(next, cpu);
  112. }
  113. #define activate_mm(prev, next) switch_mm((prev),(next),NULL)
  114. #define deactivate_mm(tsk,mm) do { } while (0)
  115. #define enter_lazy_tlb(mm,tsk) do { } while (0)
  116. #else
  117. #define set_asid(asid) do { } while (0)
  118. #define get_asid() (0)
  119. #define cpu_asid(cpu, mm) ({ (void)cpu; NO_CONTEXT; })
  120. #define switch_and_save_asid(asid) (0)
  121. #define set_TTB(pgd) do { } while (0)
  122. #define get_TTB() (0)
  123. #include <asm-generic/mmu_context.h>
  124. #endif /* CONFIG_MMU */
  125. #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
  126. /*
  127. * If this processor has an MMU, we need methods to turn it off/on ..
  128. * paging_init() will also have to be updated for the processor in
  129. * question.
  130. */
  131. static inline void enable_mmu(void)
  132. {
  133. unsigned int cpu = smp_processor_id();
  134. /* Enable MMU */
  135. __raw_writel(MMU_CONTROL_INIT, MMUCR);
  136. ctrl_barrier();
  137. if (asid_cache(cpu) == NO_CONTEXT)
  138. asid_cache(cpu) = MMU_CONTEXT_FIRST_VERSION;
  139. set_asid(asid_cache(cpu) & MMU_CONTEXT_ASID_MASK);
  140. }
  141. static inline void disable_mmu(void)
  142. {
  143. unsigned long cr;
  144. cr = __raw_readl(MMUCR);
  145. cr &= ~MMU_CONTROL_INIT;
  146. __raw_writel(cr, MMUCR);
  147. ctrl_barrier();
  148. }
  149. #else
  150. /*
  151. * MMU control handlers for processors lacking memory
  152. * management hardware.
  153. */
  154. #define enable_mmu() do { } while (0)
  155. #define disable_mmu() do { } while (0)
  156. #endif
  157. #endif /* __KERNEL__ */
  158. #endif /* __ASM_SH_MMU_CONTEXT_H */