xref: /openbmc/linux/arch/arm64/include/asm/simd.h (revision cb84d11e)
1 /*
2  * Copyright (C) 2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  */
8 
9 #ifndef __ASM_SIMD_H
10 #define __ASM_SIMD_H
11 
12 #include <linux/compiler.h>
13 #include <linux/percpu.h>
14 #include <linux/preempt.h>
15 #include <linux/types.h>
16 
17 #ifdef CONFIG_KERNEL_MODE_NEON
18 
19 DECLARE_PER_CPU(bool, kernel_neon_busy);
20 
21 /*
22  * may_use_simd - whether it is allowable at this time to issue SIMD
23  *                instructions or access the SIMD register file
24  *
25  * Callers must not assume that the result remains true beyond the next
26  * preempt_enable() or return from softirq context.
27  */
28 static __must_check inline bool may_use_simd(void)
29 {
30 	/*
31 	 * The raw_cpu_read() is racy if called with preemption enabled.
32 	 * This is not a bug: kernel_neon_busy is only set when
33 	 * preemption is disabled, so we cannot migrate to another CPU
34 	 * while it is set, nor can we migrate to a CPU where it is set.
35 	 * So, if we find it clear on some CPU then we're guaranteed to
36 	 * find it clear on any CPU we could migrate to.
37 	 *
38 	 * If we are in between kernel_neon_begin()...kernel_neon_end(),
39 	 * the flag will be set, but preemption is also disabled, so we
40 	 * can't migrate to another CPU and spuriously see it become
41 	 * false.
42 	 */
43 	return !in_irq() && !in_nmi() && !raw_cpu_read(kernel_neon_busy);
44 }
45 
46 #else /* ! CONFIG_KERNEL_MODE_NEON */
47 
48 static __must_check inline bool may_use_simd(void) {
49 	return false;
50 }
51 
52 #endif /* ! CONFIG_KERNEL_MODE_NEON */
53 
54 #endif
55