xref: /openbmc/linux/arch/arm64/include/asm/simd.h (revision ba61bb17)
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/irqflags.h>
14 #include <linux/percpu.h>
15 #include <linux/preempt.h>
16 #include <linux/types.h>
17 
18 #ifdef CONFIG_KERNEL_MODE_NEON
19 
20 DECLARE_PER_CPU(bool, kernel_neon_busy);
21 
22 /*
23  * may_use_simd - whether it is allowable at this time to issue SIMD
24  *                instructions or access the SIMD register file
25  *
26  * Callers must not assume that the result remains true beyond the next
27  * preempt_enable() or return from softirq context.
28  */
29 static __must_check inline bool may_use_simd(void)
30 {
31 	/*
32 	 * The raw_cpu_read() is racy if called with preemption enabled.
33 	 * This is not a bug: kernel_neon_busy is only set when
34 	 * preemption is disabled, so we cannot migrate to another CPU
35 	 * while it is set, nor can we migrate to a CPU where it is set.
36 	 * So, if we find it clear on some CPU then we're guaranteed to
37 	 * find it clear on any CPU we could migrate to.
38 	 *
39 	 * If we are in between kernel_neon_begin()...kernel_neon_end(),
40 	 * the flag will be set, but preemption is also disabled, so we
41 	 * can't migrate to another CPU and spuriously see it become
42 	 * false.
43 	 */
44 	return !in_irq() && !irqs_disabled() && !in_nmi() &&
45 		!raw_cpu_read(kernel_neon_busy);
46 }
47 
48 #else /* ! CONFIG_KERNEL_MODE_NEON */
49 
50 static __must_check inline bool may_use_simd(void) {
51 	return false;
52 }
53 
54 #endif /* ! CONFIG_KERNEL_MODE_NEON */
55 
56 #endif
57