mmu_context.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #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 asid_cache(cpu) (cpu_data[cpu].asid_cache)
  27. #ifdef CONFIG_MMU
  28. #define cpu_context(cpu, mm) ((mm)->context.id[cpu])
  29. #define cpu_asid(cpu, mm) \
  30. (cpu_context((cpu), (mm)) & MMU_CONTEXT_ASID_MASK)
  31. /*
  32. * Virtual Page Number mask
  33. */
  34. #define MMU_VPN_MASK 0xfffff000
  35. #if defined(CONFIG_SUPERH32)
  36. #include "mmu_context_32.h"
  37. #else
  38. #include "mmu_context_64.h"
  39. #endif
  40. /*
  41. * Get MMU context if needed.
  42. */
  43. static inline void get_mmu_context(struct mm_struct *mm, unsigned int cpu)
  44. {
  45. unsigned long asid = asid_cache(cpu);
  46. /* Check if we have old version of context. */
  47. if (((cpu_context(cpu, mm) ^ asid) & MMU_CONTEXT_VERSION_MASK) == 0)
  48. /* It's up to date, do nothing */
  49. return;
  50. /* It's old, we need to get new context with new version. */
  51. if (!(++asid & MMU_CONTEXT_ASID_MASK)) {
  52. /*
  53. * We exhaust ASID of this version.
  54. * Flush all TLB and start new cycle.
  55. */
  56. flush_tlb_all();
  57. #ifdef CONFIG_SUPERH64
  58. /*
  59. * The SH-5 cache uses the ASIDs, requiring both the I and D
  60. * cache to be flushed when the ASID is exhausted. Weak.
  61. */
  62. flush_cache_all();
  63. #endif
  64. /*
  65. * Fix version; Note that we avoid version #0
  66. * to distingush NO_CONTEXT.
  67. */
  68. if (!asid)
  69. asid = MMU_CONTEXT_FIRST_VERSION;
  70. }
  71. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  72. }
  73. /*
  74. * Initialize the context related info for a new mm_struct
  75. * instance.
  76. */
  77. static inline int init_new_context(struct task_struct *tsk,
  78. struct mm_struct *mm)
  79. {
  80. int i;
  81. for (i = 0; i < num_online_cpus(); i++)
  82. cpu_context(i, mm) = NO_CONTEXT;
  83. return 0;
  84. }
  85. /*
  86. * After we have set current->mm to a new value, this activates
  87. * the context for the new mm so we see the new mappings.
  88. */
  89. static inline void activate_context(struct mm_struct *mm, unsigned int cpu)
  90. {
  91. get_mmu_context(mm, cpu);
  92. set_asid(cpu_asid(cpu, mm));
  93. }
  94. static inline void switch_mm(struct mm_struct *prev,
  95. struct mm_struct *next,
  96. struct task_struct *tsk)
  97. {
  98. unsigned int cpu = smp_processor_id();
  99. if (likely(prev != next)) {
  100. cpu_set(cpu, next->cpu_vm_mask);
  101. set_TTB(next->pgd);
  102. activate_context(next, cpu);
  103. } else
  104. if (!cpu_test_and_set(cpu, next->cpu_vm_mask))
  105. activate_context(next, cpu);
  106. }
  107. #else
  108. #define get_mmu_context(mm) do { } while (0)
  109. #define init_new_context(tsk,mm) (0)
  110. #define destroy_context(mm) do { } while (0)
  111. #define set_asid(asid) do { } while (0)
  112. #define get_asid() (0)
  113. #define cpu_asid(cpu, mm) ({ (void)cpu; 0; })
  114. #define switch_and_save_asid(asid) (0)
  115. #define set_TTB(pgd) do { } while (0)
  116. #define get_TTB() (0)
  117. #define activate_context(mm,cpu) do { } while (0)
  118. #define switch_mm(prev,next,tsk) do { } while (0)
  119. #endif /* CONFIG_MMU */
  120. #define activate_mm(prev, next) switch_mm((prev),(next),NULL)
  121. #define deactivate_mm(tsk,mm) do { } while (0)
  122. #define enter_lazy_tlb(mm,tsk) do { } while (0)
  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. ctrl_outl(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 = ctrl_inl(MMUCR);
  143. cr &= ~MMU_CONTROL_INIT;
  144. ctrl_outl(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 */