1ecd2ada8SGreentime Hu // SPDX-License-Identifier: GPL-2.0-or-later
2ecd2ada8SGreentime Hu /*
3ecd2ada8SGreentime Hu  * Copyright (C) 2012 ARM Ltd.
4ecd2ada8SGreentime Hu  * Author: Catalin Marinas <[email protected]>
5ecd2ada8SGreentime Hu  * Copyright (C) 2017 Linaro Ltd. <[email protected]>
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>
172080ff94SAndy Chiu #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE
182080ff94SAndy Chiu #include <asm/asm-prototypes.h>
192080ff94SAndy Chiu #endif
20ecd2ada8SGreentime Hu 
riscv_v_flags_set(u32 flags)21ecd2ada8SGreentime Hu static inline void riscv_v_flags_set(u32 flags)
22ecd2ada8SGreentime Hu {
232080ff94SAndy 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);
332080ff94SAndy 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 
402080ff94SAndy 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 
832080ff94SAndy Chiu #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE
riscv_v_flags_ptr(void)842080ff94SAndy Chiu static __always_inline u32 *riscv_v_flags_ptr(void)
852080ff94SAndy Chiu {
862080ff94SAndy Chiu 	return &current->thread.riscv_v_flags;
872080ff94SAndy Chiu }
882080ff94SAndy Chiu 
riscv_preempt_v_set_dirty(void)892080ff94SAndy Chiu static inline void riscv_preempt_v_set_dirty(void)
902080ff94SAndy Chiu {
912080ff94SAndy Chiu 	*riscv_v_flags_ptr() |= RISCV_PREEMPT_V_DIRTY;
922080ff94SAndy Chiu }
932080ff94SAndy Chiu 
riscv_preempt_v_reset_flags(void)942080ff94SAndy Chiu static inline void riscv_preempt_v_reset_flags(void)
952080ff94SAndy Chiu {
962080ff94SAndy Chiu 	*riscv_v_flags_ptr() &= ~(RISCV_PREEMPT_V_DIRTY | RISCV_PREEMPT_V_NEED_RESTORE);
972080ff94SAndy Chiu }
982080ff94SAndy Chiu 
riscv_v_ctx_depth_inc(void)992080ff94SAndy Chiu static inline void riscv_v_ctx_depth_inc(void)
1002080ff94SAndy Chiu {
1012080ff94SAndy Chiu 	*riscv_v_flags_ptr() += RISCV_V_CTX_UNIT_DEPTH;
1022080ff94SAndy Chiu }
1032080ff94SAndy Chiu 
riscv_v_ctx_depth_dec(void)1042080ff94SAndy Chiu static inline void riscv_v_ctx_depth_dec(void)
1052080ff94SAndy Chiu {
1062080ff94SAndy Chiu 	*riscv_v_flags_ptr() -= RISCV_V_CTX_UNIT_DEPTH;
1072080ff94SAndy Chiu }
1082080ff94SAndy Chiu 
riscv_v_ctx_get_depth(void)1092080ff94SAndy Chiu static inline u32 riscv_v_ctx_get_depth(void)
1102080ff94SAndy Chiu {
1112080ff94SAndy Chiu 	return *riscv_v_flags_ptr() & RISCV_V_CTX_DEPTH_MASK;
1122080ff94SAndy Chiu }
1132080ff94SAndy Chiu 
riscv_v_stop_kernel_context(void)1142080ff94SAndy Chiu static int riscv_v_stop_kernel_context(void)
1152080ff94SAndy Chiu {
1162080ff94SAndy Chiu 	if (riscv_v_ctx_get_depth() != 0 || !riscv_preempt_v_started(current))
1172080ff94SAndy Chiu 		return 1;
1182080ff94SAndy Chiu 
1192080ff94SAndy Chiu 	riscv_preempt_v_clear_dirty(current);
1202080ff94SAndy Chiu 	riscv_v_stop(RISCV_PREEMPT_V);
1212080ff94SAndy Chiu 	return 0;
1222080ff94SAndy Chiu }
1232080ff94SAndy Chiu 
riscv_v_start_kernel_context(bool * is_nested)1242080ff94SAndy Chiu static int riscv_v_start_kernel_context(bool *is_nested)
1252080ff94SAndy Chiu {
1262080ff94SAndy Chiu 	struct __riscv_v_ext_state *kvstate, *uvstate;
1272080ff94SAndy Chiu 
1282080ff94SAndy Chiu 	kvstate = &current->thread.kernel_vstate;
1292080ff94SAndy Chiu 	if (!kvstate->datap)
1302080ff94SAndy Chiu 		return -ENOENT;
1312080ff94SAndy Chiu 
1322080ff94SAndy Chiu 	if (riscv_preempt_v_started(current)) {
1332080ff94SAndy Chiu 		WARN_ON(riscv_v_ctx_get_depth() == 0);
1342080ff94SAndy Chiu 		*is_nested = true;
1352080ff94SAndy Chiu 		get_cpu_vector_context();
1362080ff94SAndy Chiu 		if (riscv_preempt_v_dirty(current)) {
1372080ff94SAndy Chiu 			__riscv_v_vstate_save(kvstate, kvstate->datap);
1382080ff94SAndy Chiu 			riscv_preempt_v_clear_dirty(current);
1392080ff94SAndy Chiu 		}
1402080ff94SAndy Chiu 		riscv_preempt_v_set_restore(current);
1412080ff94SAndy Chiu 		return 0;
1422080ff94SAndy Chiu 	}
1432080ff94SAndy Chiu 
1442080ff94SAndy Chiu 	/* Transfer the ownership of V from user to kernel, then save */
1452080ff94SAndy Chiu 	riscv_v_start(RISCV_PREEMPT_V | RISCV_PREEMPT_V_DIRTY);
146*d863910eSCharlie Jenkins 	if (__riscv_v_vstate_check(task_pt_regs(current)->status, DIRTY)) {
1472080ff94SAndy Chiu 		uvstate = &current->thread.vstate;
1482080ff94SAndy Chiu 		__riscv_v_vstate_save(uvstate, uvstate->datap);
1492080ff94SAndy Chiu 	}
1502080ff94SAndy Chiu 	riscv_preempt_v_clear_dirty(current);
1512080ff94SAndy Chiu 	return 0;
1522080ff94SAndy Chiu }
1532080ff94SAndy Chiu 
1542080ff94SAndy Chiu /* low-level V context handling code, called with irq disabled */
riscv_v_context_nesting_start(struct pt_regs * regs)1552080ff94SAndy Chiu asmlinkage void riscv_v_context_nesting_start(struct pt_regs *regs)
1562080ff94SAndy Chiu {
1572080ff94SAndy Chiu 	int depth;
1582080ff94SAndy Chiu 
1592080ff94SAndy Chiu 	if (!riscv_preempt_v_started(current))
1602080ff94SAndy Chiu 		return;
1612080ff94SAndy Chiu 
1622080ff94SAndy Chiu 	depth = riscv_v_ctx_get_depth();
163*d863910eSCharlie Jenkins 	if (depth == 0 && __riscv_v_vstate_check(regs->status, DIRTY))
1642080ff94SAndy Chiu 		riscv_preempt_v_set_dirty();
1652080ff94SAndy Chiu 
1662080ff94SAndy Chiu 	riscv_v_ctx_depth_inc();
1672080ff94SAndy Chiu }
1682080ff94SAndy Chiu 
riscv_v_context_nesting_end(struct pt_regs * regs)1692080ff94SAndy Chiu asmlinkage void riscv_v_context_nesting_end(struct pt_regs *regs)
1702080ff94SAndy Chiu {
1712080ff94SAndy Chiu 	struct __riscv_v_ext_state *vstate = &current->thread.kernel_vstate;
1722080ff94SAndy Chiu 	u32 depth;
1732080ff94SAndy Chiu 
1742080ff94SAndy Chiu 	WARN_ON(!irqs_disabled());
1752080ff94SAndy Chiu 
1762080ff94SAndy Chiu 	if (!riscv_preempt_v_started(current))
1772080ff94SAndy Chiu 		return;
1782080ff94SAndy Chiu 
1792080ff94SAndy Chiu 	riscv_v_ctx_depth_dec();
1802080ff94SAndy Chiu 	depth = riscv_v_ctx_get_depth();
1812080ff94SAndy Chiu 	if (depth == 0) {
1822080ff94SAndy Chiu 		if (riscv_preempt_v_restore(current)) {
1832080ff94SAndy Chiu 			__riscv_v_vstate_restore(vstate, vstate->datap);
1842080ff94SAndy Chiu 			__riscv_v_vstate_clean(regs);
1852080ff94SAndy Chiu 			riscv_preempt_v_reset_flags();
1862080ff94SAndy Chiu 		}
1872080ff94SAndy Chiu 	}
1882080ff94SAndy Chiu }
1892080ff94SAndy Chiu #else
1902080ff94SAndy Chiu #define riscv_v_start_kernel_context(nested)	(-ENOENT)
1912080ff94SAndy Chiu #define riscv_v_stop_kernel_context()		(-ENOENT)
1922080ff94SAndy Chiu #endif /* CONFIG_RISCV_ISA_V_PREEMPTIVE */
1932080ff94SAndy 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 {
2092080ff94SAndy Chiu 	bool nested = false;
2102080ff94SAndy Chiu 
211*d863910eSCharlie Jenkins 	if (WARN_ON(!(has_vector() || has_xtheadvector())))
212ecd2ada8SGreentime Hu 		return;
213ecd2ada8SGreentime Hu 
214ecd2ada8SGreentime Hu 	BUG_ON(!may_use_simd());
215ecd2ada8SGreentime Hu 
2162080ff94SAndy 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));
2192080ff94SAndy Chiu 	}
2202080ff94SAndy Chiu 
2212080ff94SAndy Chiu 	if (!nested)
2222080ff94SAndy 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 {
239*d863910eSCharlie Jenkins 	if (WARN_ON(!(has_vector() || has_xtheadvector())))
240ecd2ada8SGreentime Hu 		return;
241ecd2ada8SGreentime Hu 
242ecd2ada8SGreentime Hu 	riscv_v_disable();
243ecd2ada8SGreentime Hu 
2442080ff94SAndy Chiu 	if (riscv_v_stop_kernel_context())
245ecd2ada8SGreentime Hu 		put_cpu_vector_context();
246ecd2ada8SGreentime Hu }
247ecd2ada8SGreentime Hu EXPORT_SYMBOL_GPL(kernel_vector_end);
248