2 * Copyright (C) 1999 Niibe Yutaka
3 * Copyright (C) 2003 - 2007 Paul Mundt
5 * ASID handling idea taken from MIPS implementation.
7 #ifndef __ASM_SH_MMU_CONTEXT_H
8 #define __ASM_SH_MMU_CONTEXT_H
11 #include <cpu/mmu_context.h>
12 #include <asm/tlbflush.h>
13 #include <asm/uaccess.h>
15 #include <asm-generic/mm_hooks.h>
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)
22 #ifdef CONFIG_CPU_HAS_PTEAEX
23 #define MMU_CONTEXT_ASID_MASK 0x0000ffff
25 #define MMU_CONTEXT_ASID_MASK 0x000000ff
28 #define MMU_CONTEXT_VERSION_MASK (~0UL & ~MMU_CONTEXT_ASID_MASK)
29 #define MMU_CONTEXT_FIRST_VERSION (MMU_CONTEXT_ASID_MASK + 1)
31 /* Impossible ASID value, to differentiate from NO_CONTEXT. */
32 #define MMU_NO_ASID MMU_CONTEXT_FIRST_VERSION
33 #define NO_CONTEXT 0UL
35 #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
38 #define cpu_context(cpu, mm) ((mm)->context.id[cpu])
40 #define cpu_asid(cpu, mm) \
41 (cpu_context((cpu), (mm)) & MMU_CONTEXT_ASID_MASK)
44 * Virtual Page Number mask
46 #define MMU_VPN_MASK 0xfffff000
48 #if defined(CONFIG_SUPERH32)
49 #include "mmu_context_32.h"
51 #include "mmu_context_64.h"
55 * Get MMU context if needed.
57 static inline void get_mmu_context(struct mm_struct *mm, unsigned int cpu)
59 unsigned long asid = asid_cache(cpu);
61 /* Check if we have old version of context. */
62 if (((cpu_context(cpu, mm) ^ asid) & MMU_CONTEXT_VERSION_MASK) == 0)
63 /* It's up to date, do nothing */
66 /* It's old, we need to get new context with new version. */
67 if (!(++asid & MMU_CONTEXT_ASID_MASK)) {
69 * We exhaust ASID of this version.
70 * Flush all TLB and start new cycle.
74 #ifdef CONFIG_SUPERH64
76 * The SH-5 cache uses the ASIDs, requiring both the I and D
77 * cache to be flushed when the ASID is exhausted. Weak.
83 * Fix version; Note that we avoid version #0
84 * to distingush NO_CONTEXT.
87 asid = MMU_CONTEXT_FIRST_VERSION;
90 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
94 * Initialize the context related info for a new mm_struct
97 static inline int init_new_context(struct task_struct *tsk,
102 for (i = 0; i < num_online_cpus(); i++)
103 cpu_context(i, mm) = NO_CONTEXT;
109 * After we have set current->mm to a new value, this activates
110 * the context for the new mm so we see the new mappings.
112 static inline void activate_context(struct mm_struct *mm, unsigned int cpu)
114 get_mmu_context(mm, cpu);
115 set_asid(cpu_asid(cpu, mm));
118 static inline void switch_mm(struct mm_struct *prev,
119 struct mm_struct *next,
120 struct task_struct *tsk)
122 unsigned int cpu = smp_processor_id();
124 if (likely(prev != next)) {
125 cpu_set(cpu, next->cpu_vm_mask);
127 activate_context(next, cpu);
129 if (!cpu_test_and_set(cpu, next->cpu_vm_mask))
130 activate_context(next, cpu);
133 #define get_mmu_context(mm) do { } while (0)
134 #define init_new_context(tsk,mm) (0)
135 #define destroy_context(mm) do { } while (0)
136 #define set_asid(asid) do { } while (0)
137 #define get_asid() (0)
138 #define cpu_asid(cpu, mm) ({ (void)cpu; NO_CONTEXT; })
139 #define switch_and_save_asid(asid) (0)
140 #define set_TTB(pgd) do { } while (0)
141 #define get_TTB() (0)
142 #define activate_context(mm,cpu) do { } while (0)
143 #define switch_mm(prev,next,tsk) do { } while (0)
144 #endif /* CONFIG_MMU */
146 #define activate_mm(prev, next) switch_mm((prev),(next),NULL)
147 #define deactivate_mm(tsk,mm) do { } while (0)
148 #define enter_lazy_tlb(mm,tsk) do { } while (0)
150 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
152 * If this processor has an MMU, we need methods to turn it off/on ..
153 * paging_init() will also have to be updated for the processor in
156 static inline void enable_mmu(void)
158 unsigned int cpu = smp_processor_id();
161 ctrl_outl(MMU_CONTROL_INIT, MMUCR);
164 if (asid_cache(cpu) == NO_CONTEXT)
165 asid_cache(cpu) = MMU_CONTEXT_FIRST_VERSION;
167 set_asid(asid_cache(cpu) & MMU_CONTEXT_ASID_MASK);
170 static inline void disable_mmu(void)
174 cr = ctrl_inl(MMUCR);
175 cr &= ~MMU_CONTROL_INIT;
176 ctrl_outl(cr, MMUCR);
182 * MMU control handlers for processors lacking memory
183 * management hardware.
185 #define enable_mmu() do { } while (0)
186 #define disable_mmu() do { } while (0)
189 #endif /* __KERNEL__ */
190 #endif /* __ASM_SH_MMU_CONTEXT_H */