xref: /linux/arch/riscv/kernel/kernel_mode_vector.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1ecd2ada8SGreentime Hu // SPDX-License-Identifier: GPL-2.0-or-later
2ecd2ada8SGreentime Hu /*
3ecd2ada8SGreentime Hu  * Copyright (C) 2012 ARM Ltd.
4ecd2ada8SGreentime Hu  * Author: Catalin Marinas <catalin.marinas@arm.com>
5ecd2ada8SGreentime Hu  * Copyright (C) 2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
6ecd2ada8SGreentime Hu  * Copyright (C) 2021 SiFive
7ecd2ada8SGreentime Hu  */
8ecd2ada8SGreentime Hu #include <linux/compiler.h>
9ecd2ada8SGreentime Hu #include <linux/irqflags.h>
10ecd2ada8SGreentime Hu #include <linux/percpu.h>
11ecd2ada8SGreentime Hu #include <linux/preempt.h>
12ecd2ada8SGreentime Hu #include <linux/types.h>
13ecd2ada8SGreentime Hu 
14ecd2ada8SGreentime Hu #include <asm/vector.h>
15ecd2ada8SGreentime Hu #include <asm/switch_to.h>
16ecd2ada8SGreentime Hu #include <asm/simd.h>
17*2080ff94SAndy Chiu #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE
18*2080ff94SAndy Chiu #include <asm/asm-prototypes.h>
19*2080ff94SAndy Chiu #endif
20ecd2ada8SGreentime Hu 
riscv_v_flags_set(u32 flags)21ecd2ada8SGreentime Hu static inline void riscv_v_flags_set(u32 flags)
22ecd2ada8SGreentime Hu {
23*2080ff94SAndy Chiu 	WRITE_ONCE(current->thread.riscv_v_flags, flags);
24ecd2ada8SGreentime Hu }
25ecd2ada8SGreentime Hu 
riscv_v_start(u32 flags)26ecd2ada8SGreentime Hu static inline void riscv_v_start(u32 flags)
27ecd2ada8SGreentime Hu {
28ecd2ada8SGreentime Hu 	int orig;
29ecd2ada8SGreentime Hu 
30ecd2ada8SGreentime Hu 	orig = riscv_v_flags();
31ecd2ada8SGreentime Hu 	BUG_ON((orig & flags) != 0);
32ecd2ada8SGreentime Hu 	riscv_v_flags_set(orig | flags);
33*2080ff94SAndy Chiu 	barrier();
34ecd2ada8SGreentime Hu }
35ecd2ada8SGreentime Hu 
riscv_v_stop(u32 flags)36ecd2ada8SGreentime Hu static inline void riscv_v_stop(u32 flags)
37ecd2ada8SGreentime Hu {
38ecd2ada8SGreentime Hu 	int orig;
39ecd2ada8SGreentime Hu 
40*2080ff94SAndy Chiu 	barrier();
41ecd2ada8SGreentime Hu 	orig = riscv_v_flags();
42ecd2ada8SGreentime Hu 	BUG_ON((orig & flags) == 0);
43ecd2ada8SGreentime Hu 	riscv_v_flags_set(orig & ~flags);
44ecd2ada8SGreentime Hu }
45ecd2ada8SGreentime Hu 
46ecd2ada8SGreentime Hu /*
47ecd2ada8SGreentime Hu  * Claim ownership of the CPU vector context for use by the calling context.
48ecd2ada8SGreentime Hu  *
49ecd2ada8SGreentime Hu  * The caller may freely manipulate the vector context metadata until
50ecd2ada8SGreentime Hu  * put_cpu_vector_context() is called.
51ecd2ada8SGreentime Hu  */
get_cpu_vector_context(void)52ecd2ada8SGreentime Hu void get_cpu_vector_context(void)
53ecd2ada8SGreentime Hu {
54956895b9SAndy Chiu 	/*
55956895b9SAndy Chiu 	 * disable softirqs so it is impossible for softirqs to nest
56956895b9SAndy Chiu 	 * get_cpu_vector_context() when kernel is actively using Vector.
57956895b9SAndy Chiu 	 */
58956895b9SAndy Chiu 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
59956895b9SAndy Chiu 		local_bh_disable();
60956895b9SAndy Chiu 	else
61ecd2ada8SGreentime Hu 		preempt_disable();
62ecd2ada8SGreentime Hu 
63ecd2ada8SGreentime Hu 	riscv_v_start(RISCV_KERNEL_MODE_V);
64ecd2ada8SGreentime Hu }
65ecd2ada8SGreentime Hu 
66ecd2ada8SGreentime Hu /*
67ecd2ada8SGreentime Hu  * Release the CPU vector context.
68ecd2ada8SGreentime Hu  *
69ecd2ada8SGreentime Hu  * Must be called from a context in which get_cpu_vector_context() was
70ecd2ada8SGreentime Hu  * previously called, with no call to put_cpu_vector_context() in the
71ecd2ada8SGreentime Hu  * meantime.
72ecd2ada8SGreentime Hu  */
put_cpu_vector_context(void)73ecd2ada8SGreentime Hu void put_cpu_vector_context(void)
74ecd2ada8SGreentime Hu {
75ecd2ada8SGreentime Hu 	riscv_v_stop(RISCV_KERNEL_MODE_V);
76ecd2ada8SGreentime Hu 
77956895b9SAndy Chiu 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
78956895b9SAndy Chiu 		local_bh_enable();
79956895b9SAndy Chiu 	else
80ecd2ada8SGreentime Hu 		preempt_enable();
81ecd2ada8SGreentime Hu }
82ecd2ada8SGreentime Hu 
83*2080ff94SAndy Chiu #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE
riscv_v_flags_ptr(void)84*2080ff94SAndy Chiu static __always_inline u32 *riscv_v_flags_ptr(void)
85*2080ff94SAndy Chiu {
86*2080ff94SAndy Chiu 	return &current->thread.riscv_v_flags;
87*2080ff94SAndy Chiu }
88*2080ff94SAndy Chiu 
riscv_preempt_v_set_dirty(void)89*2080ff94SAndy Chiu static inline void riscv_preempt_v_set_dirty(void)
90*2080ff94SAndy Chiu {
91*2080ff94SAndy Chiu 	*riscv_v_flags_ptr() |= RISCV_PREEMPT_V_DIRTY;
92*2080ff94SAndy Chiu }
93*2080ff94SAndy Chiu 
riscv_preempt_v_reset_flags(void)94*2080ff94SAndy Chiu static inline void riscv_preempt_v_reset_flags(void)
95*2080ff94SAndy Chiu {
96*2080ff94SAndy Chiu 	*riscv_v_flags_ptr() &= ~(RISCV_PREEMPT_V_DIRTY | RISCV_PREEMPT_V_NEED_RESTORE);
97*2080ff94SAndy Chiu }
98*2080ff94SAndy Chiu 
riscv_v_ctx_depth_inc(void)99*2080ff94SAndy Chiu static inline void riscv_v_ctx_depth_inc(void)
100*2080ff94SAndy Chiu {
101*2080ff94SAndy Chiu 	*riscv_v_flags_ptr() += RISCV_V_CTX_UNIT_DEPTH;
102*2080ff94SAndy Chiu }
103*2080ff94SAndy Chiu 
riscv_v_ctx_depth_dec(void)104*2080ff94SAndy Chiu static inline void riscv_v_ctx_depth_dec(void)
105*2080ff94SAndy Chiu {
106*2080ff94SAndy Chiu 	*riscv_v_flags_ptr() -= RISCV_V_CTX_UNIT_DEPTH;
107*2080ff94SAndy Chiu }
108*2080ff94SAndy Chiu 
riscv_v_ctx_get_depth(void)109*2080ff94SAndy Chiu static inline u32 riscv_v_ctx_get_depth(void)
110*2080ff94SAndy Chiu {
111*2080ff94SAndy Chiu 	return *riscv_v_flags_ptr() & RISCV_V_CTX_DEPTH_MASK;
112*2080ff94SAndy Chiu }
113*2080ff94SAndy Chiu 
riscv_v_stop_kernel_context(void)114*2080ff94SAndy Chiu static int riscv_v_stop_kernel_context(void)
115*2080ff94SAndy Chiu {
116*2080ff94SAndy Chiu 	if (riscv_v_ctx_get_depth() != 0 || !riscv_preempt_v_started(current))
117*2080ff94SAndy Chiu 		return 1;
118*2080ff94SAndy Chiu 
119*2080ff94SAndy Chiu 	riscv_preempt_v_clear_dirty(current);
120*2080ff94SAndy Chiu 	riscv_v_stop(RISCV_PREEMPT_V);
121*2080ff94SAndy Chiu 	return 0;
122*2080ff94SAndy Chiu }
123*2080ff94SAndy Chiu 
riscv_v_start_kernel_context(bool * is_nested)124*2080ff94SAndy Chiu static int riscv_v_start_kernel_context(bool *is_nested)
125*2080ff94SAndy Chiu {
126*2080ff94SAndy Chiu 	struct __riscv_v_ext_state *kvstate, *uvstate;
127*2080ff94SAndy Chiu 
128*2080ff94SAndy Chiu 	kvstate = &current->thread.kernel_vstate;
129*2080ff94SAndy Chiu 	if (!kvstate->datap)
130*2080ff94SAndy Chiu 		return -ENOENT;
131*2080ff94SAndy Chiu 
132*2080ff94SAndy Chiu 	if (riscv_preempt_v_started(current)) {
133*2080ff94SAndy Chiu 		WARN_ON(riscv_v_ctx_get_depth() == 0);
134*2080ff94SAndy Chiu 		*is_nested = true;
135*2080ff94SAndy Chiu 		get_cpu_vector_context();
136*2080ff94SAndy Chiu 		if (riscv_preempt_v_dirty(current)) {
137*2080ff94SAndy Chiu 			__riscv_v_vstate_save(kvstate, kvstate->datap);
138*2080ff94SAndy Chiu 			riscv_preempt_v_clear_dirty(current);
139*2080ff94SAndy Chiu 		}
140*2080ff94SAndy Chiu 		riscv_preempt_v_set_restore(current);
141*2080ff94SAndy Chiu 		return 0;
142*2080ff94SAndy Chiu 	}
143*2080ff94SAndy Chiu 
144*2080ff94SAndy Chiu 	/* Transfer the ownership of V from user to kernel, then save */
145*2080ff94SAndy Chiu 	riscv_v_start(RISCV_PREEMPT_V | RISCV_PREEMPT_V_DIRTY);
146*2080ff94SAndy Chiu 	if ((task_pt_regs(current)->status & SR_VS) == SR_VS_DIRTY) {
147*2080ff94SAndy Chiu 		uvstate = &current->thread.vstate;
148*2080ff94SAndy Chiu 		__riscv_v_vstate_save(uvstate, uvstate->datap);
149*2080ff94SAndy Chiu 	}
150*2080ff94SAndy Chiu 	riscv_preempt_v_clear_dirty(current);
151*2080ff94SAndy Chiu 	return 0;
152*2080ff94SAndy Chiu }
153*2080ff94SAndy Chiu 
154*2080ff94SAndy Chiu /* low-level V context handling code, called with irq disabled */
riscv_v_context_nesting_start(struct pt_regs * regs)155*2080ff94SAndy Chiu asmlinkage void riscv_v_context_nesting_start(struct pt_regs *regs)
156*2080ff94SAndy Chiu {
157*2080ff94SAndy Chiu 	int depth;
158*2080ff94SAndy Chiu 
159*2080ff94SAndy Chiu 	if (!riscv_preempt_v_started(current))
160*2080ff94SAndy Chiu 		return;
161*2080ff94SAndy Chiu 
162*2080ff94SAndy Chiu 	depth = riscv_v_ctx_get_depth();
163*2080ff94SAndy Chiu 	if (depth == 0 && (regs->status & SR_VS) == SR_VS_DIRTY)
164*2080ff94SAndy Chiu 		riscv_preempt_v_set_dirty();
165*2080ff94SAndy Chiu 
166*2080ff94SAndy Chiu 	riscv_v_ctx_depth_inc();
167*2080ff94SAndy Chiu }
168*2080ff94SAndy Chiu 
riscv_v_context_nesting_end(struct pt_regs * regs)169*2080ff94SAndy Chiu asmlinkage void riscv_v_context_nesting_end(struct pt_regs *regs)
170*2080ff94SAndy Chiu {
171*2080ff94SAndy Chiu 	struct __riscv_v_ext_state *vstate = &current->thread.kernel_vstate;
172*2080ff94SAndy Chiu 	u32 depth;
173*2080ff94SAndy Chiu 
174*2080ff94SAndy Chiu 	WARN_ON(!irqs_disabled());
175*2080ff94SAndy Chiu 
176*2080ff94SAndy Chiu 	if (!riscv_preempt_v_started(current))
177*2080ff94SAndy Chiu 		return;
178*2080ff94SAndy Chiu 
179*2080ff94SAndy Chiu 	riscv_v_ctx_depth_dec();
180*2080ff94SAndy Chiu 	depth = riscv_v_ctx_get_depth();
181*2080ff94SAndy Chiu 	if (depth == 0) {
182*2080ff94SAndy Chiu 		if (riscv_preempt_v_restore(current)) {
183*2080ff94SAndy Chiu 			__riscv_v_vstate_restore(vstate, vstate->datap);
184*2080ff94SAndy Chiu 			__riscv_v_vstate_clean(regs);
185*2080ff94SAndy Chiu 			riscv_preempt_v_reset_flags();
186*2080ff94SAndy Chiu 		}
187*2080ff94SAndy Chiu 	}
188*2080ff94SAndy Chiu }
189*2080ff94SAndy Chiu #else
190*2080ff94SAndy Chiu #define riscv_v_start_kernel_context(nested)	(-ENOENT)
191*2080ff94SAndy Chiu #define riscv_v_stop_kernel_context()		(-ENOENT)
192*2080ff94SAndy Chiu #endif /* CONFIG_RISCV_ISA_V_PREEMPTIVE */
193*2080ff94SAndy Chiu 
194ecd2ada8SGreentime Hu /*
195ecd2ada8SGreentime Hu  * kernel_vector_begin(): obtain the CPU vector registers for use by the calling
196ecd2ada8SGreentime Hu  * context
197ecd2ada8SGreentime Hu  *
198ecd2ada8SGreentime Hu  * Must not be called unless may_use_simd() returns true.
199ecd2ada8SGreentime Hu  * Task context in the vector registers is saved back to memory as necessary.
200ecd2ada8SGreentime Hu  *
201ecd2ada8SGreentime Hu  * A matching call to kernel_vector_end() must be made before returning from the
202ecd2ada8SGreentime Hu  * calling context.
203ecd2ada8SGreentime Hu  *
204ecd2ada8SGreentime Hu  * The caller may freely use the vector registers until kernel_vector_end() is
205ecd2ada8SGreentime Hu  * called.
206ecd2ada8SGreentime Hu  */
kernel_vector_begin(void)207ecd2ada8SGreentime Hu void kernel_vector_begin(void)
208ecd2ada8SGreentime Hu {
209*2080ff94SAndy Chiu 	bool nested = false;
210*2080ff94SAndy Chiu 
211ecd2ada8SGreentime Hu 	if (WARN_ON(!has_vector()))
212ecd2ada8SGreentime Hu 		return;
213ecd2ada8SGreentime Hu 
214ecd2ada8SGreentime Hu 	BUG_ON(!may_use_simd());
215ecd2ada8SGreentime Hu 
216*2080ff94SAndy Chiu 	if (riscv_v_start_kernel_context(&nested)) {
217ecd2ada8SGreentime Hu 		get_cpu_vector_context();
218d6c78f1cSAndy Chiu 		riscv_v_vstate_save(&current->thread.vstate, task_pt_regs(current));
219*2080ff94SAndy Chiu 	}
220*2080ff94SAndy Chiu 
221*2080ff94SAndy Chiu 	if (!nested)
222*2080ff94SAndy Chiu 		riscv_v_vstate_set_restore(current, task_pt_regs(current));
223ecd2ada8SGreentime Hu 
224ecd2ada8SGreentime Hu 	riscv_v_enable();
225ecd2ada8SGreentime Hu }
226ecd2ada8SGreentime Hu EXPORT_SYMBOL_GPL(kernel_vector_begin);
227ecd2ada8SGreentime Hu 
228ecd2ada8SGreentime Hu /*
229ecd2ada8SGreentime Hu  * kernel_vector_end(): give the CPU vector registers back to the current task
230ecd2ada8SGreentime Hu  *
231ecd2ada8SGreentime Hu  * Must be called from a context in which kernel_vector_begin() was previously
232ecd2ada8SGreentime Hu  * called, with no call to kernel_vector_end() in the meantime.
233ecd2ada8SGreentime Hu  *
234ecd2ada8SGreentime Hu  * The caller must not use the vector registers after this function is called,
235ecd2ada8SGreentime Hu  * unless kernel_vector_begin() is called again in the meantime.
236ecd2ada8SGreentime Hu  */
kernel_vector_end(void)237ecd2ada8SGreentime Hu void kernel_vector_end(void)
238ecd2ada8SGreentime Hu {
239ecd2ada8SGreentime Hu 	if (WARN_ON(!has_vector()))
240ecd2ada8SGreentime Hu 		return;
241ecd2ada8SGreentime Hu 
242ecd2ada8SGreentime Hu 	riscv_v_disable();
243ecd2ada8SGreentime Hu 
244*2080ff94SAndy Chiu 	if (riscv_v_stop_kernel_context())
245ecd2ada8SGreentime Hu 		put_cpu_vector_context();
246ecd2ada8SGreentime Hu }
247ecd2ada8SGreentime Hu EXPORT_SYMBOL_GPL(kernel_vector_end);
248