GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / m32r / include / asm / mmu_context.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_M32R_MMU_CONTEXT_H
3 #define _ASM_M32R_MMU_CONTEXT_H
4 #ifdef __KERNEL__
5
6 #include <asm/m32r.h>
7
8 #define MMU_CONTEXT_ASID_MASK      (0x000000FF)
9 #define MMU_CONTEXT_VERSION_MASK   (0xFFFFFF00)
10 #define MMU_CONTEXT_FIRST_VERSION  (0x00000100)
11 #define NO_CONTEXT                 (0x00000000)
12
13 #ifndef __ASSEMBLY__
14
15 #include <linux/atomic.h>
16 #include <linux/mm_types.h>
17
18 #include <asm/pgalloc.h>
19 #include <asm/mmu.h>
20 #include <asm/tlbflush.h>
21 #include <asm-generic/mm_hooks.h>
22
23 /*
24  * Cache of MMU context last used.
25  */
26 #ifndef CONFIG_SMP
27 extern unsigned long mmu_context_cache_dat;
28 #define mmu_context_cache       mmu_context_cache_dat
29 #define mm_context(mm)          mm->context
30 #else /* not CONFIG_SMP */
31 extern unsigned long mmu_context_cache_dat[];
32 #define mmu_context_cache       mmu_context_cache_dat[smp_processor_id()]
33 #define mm_context(mm)          mm->context[smp_processor_id()]
34 #endif /* not CONFIG_SMP */
35
36 #define set_tlb_tag(entry, tag)         (*entry = (tag & PAGE_MASK)|get_asid())
37 #define set_tlb_data(entry, data)       (*entry = (data | _PAGE_PRESENT))
38
39 #ifdef CONFIG_MMU
40 #define enter_lazy_tlb(mm, tsk) do { } while (0)
41
42 static inline void get_new_mmu_context(struct mm_struct *mm)
43 {
44         unsigned long mc = ++mmu_context_cache;
45
46         if (!(mc & MMU_CONTEXT_ASID_MASK)) {
47                 /* We exhaust ASID of this version.
48                    Flush all TLB and start new cycle. */
49                 local_flush_tlb_all();
50                 /* Fix version if needed.
51                    Note that we avoid version #0 to distinguish NO_CONTEXT. */
52                 if (!mc)
53                         mmu_context_cache = mc = MMU_CONTEXT_FIRST_VERSION;
54         }
55         mm_context(mm) = mc;
56 }
57
58 /*
59  * Get MMU context if needed.
60  */
61 static inline void get_mmu_context(struct mm_struct *mm)
62 {
63         if (mm) {
64                 unsigned long mc = mmu_context_cache;
65
66                 /* Check if we have old version of context.
67                    If it's old, we need to get new context with new version. */
68                 if ((mm_context(mm) ^ mc) & MMU_CONTEXT_VERSION_MASK)
69                         get_new_mmu_context(mm);
70         }
71 }
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 #ifndef CONFIG_SMP
81         mm->context = NO_CONTEXT;
82 #else /* CONFIG_SMP */
83         int num_cpus = num_online_cpus();
84         int i;
85
86         for (i = 0 ; i < num_cpus ; i++)
87                 mm->context[i] = NO_CONTEXT;
88 #endif /* CONFIG_SMP */
89
90         return 0;
91 }
92
93 /*
94  * Destroy context related info for an mm_struct that is about
95  * to be put to rest.
96  */
97 #define destroy_context(mm)     do { } while (0)
98
99 static inline void set_asid(unsigned long asid)
100 {
101         *(volatile unsigned long *)MASID = (asid & MMU_CONTEXT_ASID_MASK);
102 }
103
104 static inline unsigned long get_asid(void)
105 {
106         unsigned long asid;
107
108         asid = *(volatile long *)MASID;
109         asid &= MMU_CONTEXT_ASID_MASK;
110
111         return asid;
112 }
113
114 /*
115  * After we have set current->mm to a new value, this activates
116  * the context for the new mm so we see the new mappings.
117  */
118 static inline void activate_context(struct mm_struct *mm)
119 {
120         get_mmu_context(mm);
121         set_asid(mm_context(mm) & MMU_CONTEXT_ASID_MASK);
122 }
123
124 static inline void switch_mm(struct mm_struct *prev,
125         struct mm_struct *next, struct task_struct *tsk)
126 {
127 #ifdef CONFIG_SMP
128         int cpu = smp_processor_id();
129 #endif  /* CONFIG_SMP */
130
131         if (prev != next) {
132 #ifdef CONFIG_SMP
133                 cpumask_set_cpu(cpu, mm_cpumask(next));
134 #endif /* CONFIG_SMP */
135                 /* Set MPTB = next->pgd */
136                 *(volatile unsigned long *)MPTB = (unsigned long)next->pgd;
137                 activate_context(next);
138         }
139 #ifdef CONFIG_SMP
140         else
141                 if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next)))
142                         activate_context(next);
143 #endif /* CONFIG_SMP */
144 }
145
146 #define deactivate_mm(tsk, mm)  do { } while (0)
147
148 #define activate_mm(prev, next) \
149         switch_mm((prev), (next), NULL)
150
151 #else /* not CONFIG_MMU */
152 #define get_mmu_context(mm)             do { } while (0)
153 #define init_new_context(tsk,mm)        (0)
154 #define destroy_context(mm)             do { } while (0)
155 #define set_asid(asid)                  do { } while (0)
156 #define get_asid()                      (0)
157 #define activate_context(mm)            do { } while (0)
158 #define switch_mm(prev,next,tsk)        do { } while (0)
159 #define deactivate_mm(mm,tsk)           do { } while (0)
160 #define activate_mm(prev,next)          do { } while (0)
161 #define enter_lazy_tlb(mm,tsk)          do { } while (0)
162 #endif /* not CONFIG_MMU */
163
164 #endif /* not __ASSEMBLY__ */
165
166 #endif /* __KERNEL__ */
167 #endif /* _ASM_M32R_MMU_CONTEXT_H */