mmu_context.h 4.1 KB

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