mmu_context.h 4.4 KB

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