1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __ALPHA_MMU_CONTEXT_H
3*4882a593Smuzhiyun #define __ALPHA_MMU_CONTEXT_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * get a new mmu context..
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Copyright (C) 1996, Linus Torvalds
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/mm_types.h>
12*4882a593Smuzhiyun #include <linux/sched.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <asm/machvec.h>
15*4882a593Smuzhiyun #include <asm/compiler.h>
16*4882a593Smuzhiyun #include <asm-generic/mm_hooks.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun * Force a context reload. This is needed when we change the page
20*4882a593Smuzhiyun * table pointer or when we update the ASN of the current process.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /* Don't get into trouble with dueling __EXTERN_INLINEs. */
24*4882a593Smuzhiyun #ifndef __EXTERN_INLINE
25*4882a593Smuzhiyun #include <asm/io.h>
26*4882a593Smuzhiyun #endif
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun static inline unsigned long
__reload_thread(struct pcb_struct * pcb)30*4882a593Smuzhiyun __reload_thread(struct pcb_struct *pcb)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun register unsigned long a0 __asm__("$16");
33*4882a593Smuzhiyun register unsigned long v0 __asm__("$0");
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun a0 = virt_to_phys(pcb);
36*4882a593Smuzhiyun __asm__ __volatile__(
37*4882a593Smuzhiyun "call_pal %2 #__reload_thread"
38*4882a593Smuzhiyun : "=r"(v0), "=r"(a0)
39*4882a593Smuzhiyun : "i"(PAL_swpctx), "r"(a0)
40*4882a593Smuzhiyun : "$1", "$22", "$23", "$24", "$25");
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun return v0;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * The maximum ASN's the processor supports. On the EV4 this is 63
48*4882a593Smuzhiyun * but the PAL-code doesn't actually use this information. On the
49*4882a593Smuzhiyun * EV5 this is 127, and EV6 has 255.
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * On the EV4, the ASNs are more-or-less useless anyway, as they are
52*4882a593Smuzhiyun * only used as an icache tag, not for TB entries. On the EV5 and EV6,
53*4882a593Smuzhiyun * ASN's also validate the TB entries, and thus make a lot more sense.
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * The EV4 ASN's don't even match the architecture manual, ugh. And
56*4882a593Smuzhiyun * I quote: "If a processor implements address space numbers (ASNs),
57*4882a593Smuzhiyun * and the old PTE has the Address Space Match (ASM) bit clear (ASNs
58*4882a593Smuzhiyun * in use) and the Valid bit set, then entries can also effectively be
59*4882a593Smuzhiyun * made coherent by assigning a new, unused ASN to the currently
60*4882a593Smuzhiyun * running process and not reusing the previous ASN before calling the
61*4882a593Smuzhiyun * appropriate PALcode routine to invalidate the translation buffer (TB)".
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * In short, the EV4 has a "kind of" ASN capability, but it doesn't actually
64*4882a593Smuzhiyun * work correctly and can thus not be used (explaining the lack of PAL-code
65*4882a593Smuzhiyun * support).
66*4882a593Smuzhiyun */
67*4882a593Smuzhiyun #define EV4_MAX_ASN 63
68*4882a593Smuzhiyun #define EV5_MAX_ASN 127
69*4882a593Smuzhiyun #define EV6_MAX_ASN 255
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #ifdef CONFIG_ALPHA_GENERIC
72*4882a593Smuzhiyun # define MAX_ASN (alpha_mv.max_asn)
73*4882a593Smuzhiyun #else
74*4882a593Smuzhiyun # ifdef CONFIG_ALPHA_EV4
75*4882a593Smuzhiyun # define MAX_ASN EV4_MAX_ASN
76*4882a593Smuzhiyun # elif defined(CONFIG_ALPHA_EV5)
77*4882a593Smuzhiyun # define MAX_ASN EV5_MAX_ASN
78*4882a593Smuzhiyun # else
79*4882a593Smuzhiyun # define MAX_ASN EV6_MAX_ASN
80*4882a593Smuzhiyun # endif
81*4882a593Smuzhiyun #endif
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /*
84*4882a593Smuzhiyun * cpu_last_asn(processor):
85*4882a593Smuzhiyun * 63 0
86*4882a593Smuzhiyun * +-------------+----------------+--------------+
87*4882a593Smuzhiyun * | asn version | this processor | hardware asn |
88*4882a593Smuzhiyun * +-------------+----------------+--------------+
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun #include <asm/smp.h>
92*4882a593Smuzhiyun #ifdef CONFIG_SMP
93*4882a593Smuzhiyun #define cpu_last_asn(cpuid) (cpu_data[cpuid].last_asn)
94*4882a593Smuzhiyun #else
95*4882a593Smuzhiyun extern unsigned long last_asn;
96*4882a593Smuzhiyun #define cpu_last_asn(cpuid) last_asn
97*4882a593Smuzhiyun #endif /* CONFIG_SMP */
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun #define WIDTH_HARDWARE_ASN 8
100*4882a593Smuzhiyun #define ASN_FIRST_VERSION (1UL << WIDTH_HARDWARE_ASN)
101*4882a593Smuzhiyun #define HARDWARE_ASN_MASK ((1UL << WIDTH_HARDWARE_ASN) - 1)
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * NOTE! The way this is set up, the high bits of the "asn_cache" (and
105*4882a593Smuzhiyun * the "mm->context") are the ASN _version_ code. A version of 0 is
106*4882a593Smuzhiyun * always considered invalid, so to invalidate another process you only
107*4882a593Smuzhiyun * need to do "p->mm->context = 0".
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * If we need more ASN's than the processor has, we invalidate the old
110*4882a593Smuzhiyun * user TLB's (tbiap()) and start a new ASN version. That will automatically
111*4882a593Smuzhiyun * force a new asn for any other processes the next time they want to
112*4882a593Smuzhiyun * run.
113*4882a593Smuzhiyun */
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun #ifndef __EXTERN_INLINE
116*4882a593Smuzhiyun #define __EXTERN_INLINE extern inline
117*4882a593Smuzhiyun #define __MMU_EXTERN_INLINE
118*4882a593Smuzhiyun #endif
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun extern inline unsigned long
__get_new_mm_context(struct mm_struct * mm,long cpu)121*4882a593Smuzhiyun __get_new_mm_context(struct mm_struct *mm, long cpu)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun unsigned long asn = cpu_last_asn(cpu);
124*4882a593Smuzhiyun unsigned long next = asn + 1;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun if ((asn & HARDWARE_ASN_MASK) >= MAX_ASN) {
127*4882a593Smuzhiyun tbiap();
128*4882a593Smuzhiyun imb();
129*4882a593Smuzhiyun next = (asn & ~HARDWARE_ASN_MASK) + ASN_FIRST_VERSION;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun cpu_last_asn(cpu) = next;
132*4882a593Smuzhiyun return next;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun __EXTERN_INLINE void
ev5_switch_mm(struct mm_struct * prev_mm,struct mm_struct * next_mm,struct task_struct * next)136*4882a593Smuzhiyun ev5_switch_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm,
137*4882a593Smuzhiyun struct task_struct *next)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun /* Check if our ASN is of an older version, and thus invalid. */
140*4882a593Smuzhiyun unsigned long asn;
141*4882a593Smuzhiyun unsigned long mmc;
142*4882a593Smuzhiyun long cpu = smp_processor_id();
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun #ifdef CONFIG_SMP
145*4882a593Smuzhiyun cpu_data[cpu].asn_lock = 1;
146*4882a593Smuzhiyun barrier();
147*4882a593Smuzhiyun #endif
148*4882a593Smuzhiyun asn = cpu_last_asn(cpu);
149*4882a593Smuzhiyun mmc = next_mm->context[cpu];
150*4882a593Smuzhiyun if ((mmc ^ asn) & ~HARDWARE_ASN_MASK) {
151*4882a593Smuzhiyun mmc = __get_new_mm_context(next_mm, cpu);
152*4882a593Smuzhiyun next_mm->context[cpu] = mmc;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun #ifdef CONFIG_SMP
155*4882a593Smuzhiyun else
156*4882a593Smuzhiyun cpu_data[cpu].need_new_asn = 1;
157*4882a593Smuzhiyun #endif
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* Always update the PCB ASN. Another thread may have allocated
160*4882a593Smuzhiyun a new mm->context (via flush_tlb_mm) without the ASN serial
161*4882a593Smuzhiyun number wrapping. We have no way to detect when this is needed. */
162*4882a593Smuzhiyun task_thread_info(next)->pcb.asn = mmc & HARDWARE_ASN_MASK;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun __EXTERN_INLINE void
ev4_switch_mm(struct mm_struct * prev_mm,struct mm_struct * next_mm,struct task_struct * next)166*4882a593Smuzhiyun ev4_switch_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm,
167*4882a593Smuzhiyun struct task_struct *next)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun /* As described, ASN's are broken for TLB usage. But we can
170*4882a593Smuzhiyun optimize for switching between threads -- if the mm is
171*4882a593Smuzhiyun unchanged from current we needn't flush. */
172*4882a593Smuzhiyun /* ??? May not be needed because EV4 PALcode recognizes that
173*4882a593Smuzhiyun ASN's are broken and does a tbiap itself on swpctx, under
174*4882a593Smuzhiyun the "Must set ASN or flush" rule. At least this is true
175*4882a593Smuzhiyun for a 1992 SRM, reports Joseph Martin (jmartin@hlo.dec.com).
176*4882a593Smuzhiyun I'm going to leave this here anyway, just to Be Sure. -- r~ */
177*4882a593Smuzhiyun if (prev_mm != next_mm)
178*4882a593Smuzhiyun tbiap();
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* Do continue to allocate ASNs, because we can still use them
181*4882a593Smuzhiyun to avoid flushing the icache. */
182*4882a593Smuzhiyun ev5_switch_mm(prev_mm, next_mm, next);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun extern void __load_new_mm_context(struct mm_struct *);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun #ifdef CONFIG_SMP
188*4882a593Smuzhiyun #define check_mmu_context() \
189*4882a593Smuzhiyun do { \
190*4882a593Smuzhiyun int cpu = smp_processor_id(); \
191*4882a593Smuzhiyun cpu_data[cpu].asn_lock = 0; \
192*4882a593Smuzhiyun barrier(); \
193*4882a593Smuzhiyun if (cpu_data[cpu].need_new_asn) { \
194*4882a593Smuzhiyun struct mm_struct * mm = current->active_mm; \
195*4882a593Smuzhiyun cpu_data[cpu].need_new_asn = 0; \
196*4882a593Smuzhiyun if (!mm->context[cpu]) \
197*4882a593Smuzhiyun __load_new_mm_context(mm); \
198*4882a593Smuzhiyun } \
199*4882a593Smuzhiyun } while(0)
200*4882a593Smuzhiyun #else
201*4882a593Smuzhiyun #define check_mmu_context() do { } while(0)
202*4882a593Smuzhiyun #endif
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun __EXTERN_INLINE void
ev5_activate_mm(struct mm_struct * prev_mm,struct mm_struct * next_mm)205*4882a593Smuzhiyun ev5_activate_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun __load_new_mm_context(next_mm);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun __EXTERN_INLINE void
ev4_activate_mm(struct mm_struct * prev_mm,struct mm_struct * next_mm)211*4882a593Smuzhiyun ev4_activate_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun __load_new_mm_context(next_mm);
214*4882a593Smuzhiyun tbiap();
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun #define deactivate_mm(tsk,mm) do { } while (0)
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun #ifdef CONFIG_ALPHA_GENERIC
220*4882a593Smuzhiyun # define switch_mm(a,b,c) alpha_mv.mv_switch_mm((a),(b),(c))
221*4882a593Smuzhiyun # define activate_mm(x,y) alpha_mv.mv_activate_mm((x),(y))
222*4882a593Smuzhiyun #else
223*4882a593Smuzhiyun # ifdef CONFIG_ALPHA_EV4
224*4882a593Smuzhiyun # define switch_mm(a,b,c) ev4_switch_mm((a),(b),(c))
225*4882a593Smuzhiyun # define activate_mm(x,y) ev4_activate_mm((x),(y))
226*4882a593Smuzhiyun # else
227*4882a593Smuzhiyun # define switch_mm(a,b,c) ev5_switch_mm((a),(b),(c))
228*4882a593Smuzhiyun # define activate_mm(x,y) ev5_activate_mm((x),(y))
229*4882a593Smuzhiyun # endif
230*4882a593Smuzhiyun #endif
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun static inline int
init_new_context(struct task_struct * tsk,struct mm_struct * mm)233*4882a593Smuzhiyun init_new_context(struct task_struct *tsk, struct mm_struct *mm)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun int i;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun for_each_online_cpu(i)
238*4882a593Smuzhiyun mm->context[i] = 0;
239*4882a593Smuzhiyun if (tsk != current)
240*4882a593Smuzhiyun task_thread_info(tsk)->pcb.ptbr
241*4882a593Smuzhiyun = ((unsigned long)mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
242*4882a593Smuzhiyun return 0;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun extern inline void
destroy_context(struct mm_struct * mm)246*4882a593Smuzhiyun destroy_context(struct mm_struct *mm)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun /* Nothing to do. */
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun static inline void
enter_lazy_tlb(struct mm_struct * mm,struct task_struct * tsk)252*4882a593Smuzhiyun enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun task_thread_info(tsk)->pcb.ptbr
255*4882a593Smuzhiyun = ((unsigned long)mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun #ifdef __MMU_EXTERN_INLINE
259*4882a593Smuzhiyun #undef __EXTERN_INLINE
260*4882a593Smuzhiyun #undef __MMU_EXTERN_INLINE
261*4882a593Smuzhiyun #endif
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun #endif /* __ALPHA_MMU_CONTEXT_H */
264