1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _ASM_IA64_MMU_CONTEXT_H
3*4882a593Smuzhiyun #define _ASM_IA64_MMU_CONTEXT_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * Copyright (C) 1998-2002 Hewlett-Packard Co
7*4882a593Smuzhiyun * David Mosberger-Tang <davidm@hpl.hp.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun * Routines to manage the allocation of task context numbers. Task context
12*4882a593Smuzhiyun * numbers are used to reduce or eliminate the need to perform TLB flushes
13*4882a593Smuzhiyun * due to context switches. Context numbers are implemented using ia-64
14*4882a593Smuzhiyun * region ids. Since the IA-64 TLB does not consider the region number when
15*4882a593Smuzhiyun * performing a TLB lookup, we need to assign a unique region id to each
16*4882a593Smuzhiyun * region in a process. We use the least significant three bits in aregion
17*4882a593Smuzhiyun * id for this purpose.
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define IA64_REGION_ID_KERNEL 0 /* the kernel's region id (tlb.c depends on this being 0) */
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #define ia64_rid(ctx,addr) (((ctx) << 3) | (addr >> 61))
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun # include <asm/page.h>
25*4882a593Smuzhiyun # ifndef __ASSEMBLY__
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <linux/compiler.h>
28*4882a593Smuzhiyun #include <linux/percpu.h>
29*4882a593Smuzhiyun #include <linux/sched.h>
30*4882a593Smuzhiyun #include <linux/mm_types.h>
31*4882a593Smuzhiyun #include <linux/spinlock.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #include <asm/processor.h>
34*4882a593Smuzhiyun #include <asm-generic/mm_hooks.h>
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun struct ia64_ctx {
37*4882a593Smuzhiyun spinlock_t lock;
38*4882a593Smuzhiyun unsigned int next; /* next context number to use */
39*4882a593Smuzhiyun unsigned int limit; /* available free range */
40*4882a593Smuzhiyun unsigned int max_ctx; /* max. context value supported by all CPUs */
41*4882a593Smuzhiyun /* call wrap_mmu_context when next >= max */
42*4882a593Smuzhiyun unsigned long *bitmap; /* bitmap size is max_ctx+1 */
43*4882a593Smuzhiyun unsigned long *flushmap;/* pending rid to be flushed */
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun extern struct ia64_ctx ia64_ctx;
47*4882a593Smuzhiyun DECLARE_PER_CPU(u8, ia64_need_tlb_flush);
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun extern void mmu_context_init (void);
50*4882a593Smuzhiyun extern void wrap_mmu_context (struct mm_struct *mm);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun static inline void
enter_lazy_tlb(struct mm_struct * mm,struct task_struct * tsk)53*4882a593Smuzhiyun enter_lazy_tlb (struct mm_struct *mm, struct task_struct *tsk)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * When the context counter wraps around all TLBs need to be flushed because
59*4882a593Smuzhiyun * an old context number might have been reused. This is signalled by the
60*4882a593Smuzhiyun * ia64_need_tlb_flush per-CPU variable, which is checked in the routine
61*4882a593Smuzhiyun * below. Called by activate_mm(). <efocht@ess.nec.de>
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun static inline void
delayed_tlb_flush(void)64*4882a593Smuzhiyun delayed_tlb_flush (void)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun extern void local_flush_tlb_all (void);
67*4882a593Smuzhiyun unsigned long flags;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun if (unlikely(__ia64_per_cpu_var(ia64_need_tlb_flush))) {
70*4882a593Smuzhiyun spin_lock_irqsave(&ia64_ctx.lock, flags);
71*4882a593Smuzhiyun if (__ia64_per_cpu_var(ia64_need_tlb_flush)) {
72*4882a593Smuzhiyun local_flush_tlb_all();
73*4882a593Smuzhiyun __ia64_per_cpu_var(ia64_need_tlb_flush) = 0;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun spin_unlock_irqrestore(&ia64_ctx.lock, flags);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun static inline nv_mm_context_t
get_mmu_context(struct mm_struct * mm)80*4882a593Smuzhiyun get_mmu_context (struct mm_struct *mm)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun unsigned long flags;
83*4882a593Smuzhiyun nv_mm_context_t context = mm->context;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (likely(context))
86*4882a593Smuzhiyun goto out;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun spin_lock_irqsave(&ia64_ctx.lock, flags);
89*4882a593Smuzhiyun /* re-check, now that we've got the lock: */
90*4882a593Smuzhiyun context = mm->context;
91*4882a593Smuzhiyun if (context == 0) {
92*4882a593Smuzhiyun cpumask_clear(mm_cpumask(mm));
93*4882a593Smuzhiyun if (ia64_ctx.next >= ia64_ctx.limit) {
94*4882a593Smuzhiyun ia64_ctx.next = find_next_zero_bit(ia64_ctx.bitmap,
95*4882a593Smuzhiyun ia64_ctx.max_ctx, ia64_ctx.next);
96*4882a593Smuzhiyun ia64_ctx.limit = find_next_bit(ia64_ctx.bitmap,
97*4882a593Smuzhiyun ia64_ctx.max_ctx, ia64_ctx.next);
98*4882a593Smuzhiyun if (ia64_ctx.next >= ia64_ctx.max_ctx)
99*4882a593Smuzhiyun wrap_mmu_context(mm);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun mm->context = context = ia64_ctx.next++;
102*4882a593Smuzhiyun __set_bit(context, ia64_ctx.bitmap);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun spin_unlock_irqrestore(&ia64_ctx.lock, flags);
105*4882a593Smuzhiyun out:
106*4882a593Smuzhiyun /*
107*4882a593Smuzhiyun * Ensure we're not starting to use "context" before any old
108*4882a593Smuzhiyun * uses of it are gone from our TLB.
109*4882a593Smuzhiyun */
110*4882a593Smuzhiyun delayed_tlb_flush();
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return context;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun * Initialize context number to some sane value. MM is guaranteed to be a
117*4882a593Smuzhiyun * brand-new address-space, so no TLB flushing is needed, ever.
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun static inline int
init_new_context(struct task_struct * p,struct mm_struct * mm)120*4882a593Smuzhiyun init_new_context (struct task_struct *p, struct mm_struct *mm)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun mm->context = 0;
123*4882a593Smuzhiyun return 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun static inline void
destroy_context(struct mm_struct * mm)127*4882a593Smuzhiyun destroy_context (struct mm_struct *mm)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun /* Nothing to do. */
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun static inline void
reload_context(nv_mm_context_t context)133*4882a593Smuzhiyun reload_context (nv_mm_context_t context)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun unsigned long rid;
136*4882a593Smuzhiyun unsigned long rid_incr = 0;
137*4882a593Smuzhiyun unsigned long rr0, rr1, rr2, rr3, rr4, old_rr4;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun old_rr4 = ia64_get_rr(RGN_BASE(RGN_HPAGE));
140*4882a593Smuzhiyun rid = context << 3; /* make space for encoding the region number */
141*4882a593Smuzhiyun rid_incr = 1 << 8;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* encode the region id, preferred page size, and VHPT enable bit: */
144*4882a593Smuzhiyun rr0 = (rid << 8) | (PAGE_SHIFT << 2) | 1;
145*4882a593Smuzhiyun rr1 = rr0 + 1*rid_incr;
146*4882a593Smuzhiyun rr2 = rr0 + 2*rid_incr;
147*4882a593Smuzhiyun rr3 = rr0 + 3*rid_incr;
148*4882a593Smuzhiyun rr4 = rr0 + 4*rid_incr;
149*4882a593Smuzhiyun #ifdef CONFIG_HUGETLB_PAGE
150*4882a593Smuzhiyun rr4 = (rr4 & (~(0xfcUL))) | (old_rr4 & 0xfc);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun # if RGN_HPAGE != 4
153*4882a593Smuzhiyun # error "reload_context assumes RGN_HPAGE is 4"
154*4882a593Smuzhiyun # endif
155*4882a593Smuzhiyun #endif
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun ia64_set_rr0_to_rr4(rr0, rr1, rr2, rr3, rr4);
158*4882a593Smuzhiyun ia64_srlz_i(); /* srlz.i implies srlz.d */
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun * Must be called with preemption off
163*4882a593Smuzhiyun */
164*4882a593Smuzhiyun static inline void
activate_context(struct mm_struct * mm)165*4882a593Smuzhiyun activate_context (struct mm_struct *mm)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun nv_mm_context_t context;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun do {
170*4882a593Smuzhiyun context = get_mmu_context(mm);
171*4882a593Smuzhiyun if (!cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
172*4882a593Smuzhiyun cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
173*4882a593Smuzhiyun reload_context(context);
174*4882a593Smuzhiyun /*
175*4882a593Smuzhiyun * in the unlikely event of a TLB-flush by another thread,
176*4882a593Smuzhiyun * redo the load.
177*4882a593Smuzhiyun */
178*4882a593Smuzhiyun } while (unlikely(context != mm->context));
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun #define deactivate_mm(tsk,mm) do { } while (0)
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * Switch from address space PREV to address space NEXT.
185*4882a593Smuzhiyun */
186*4882a593Smuzhiyun static inline void
activate_mm(struct mm_struct * prev,struct mm_struct * next)187*4882a593Smuzhiyun activate_mm (struct mm_struct *prev, struct mm_struct *next)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun * We may get interrupts here, but that's OK because interrupt
191*4882a593Smuzhiyun * handlers cannot touch user-space.
192*4882a593Smuzhiyun */
193*4882a593Smuzhiyun ia64_set_kr(IA64_KR_PT_BASE, __pa(next->pgd));
194*4882a593Smuzhiyun activate_context(next);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun #define switch_mm(prev_mm,next_mm,next_task) activate_mm(prev_mm, next_mm)
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun # endif /* ! __ASSEMBLY__ */
200*4882a593Smuzhiyun #endif /* _ASM_IA64_MMU_CONTEXT_H */
201