2 * Copyright (C) 2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
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.
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>
18 #ifdef CONFIG_KERNEL_MODE_NEON
20 DECLARE_PER_CPU(bool, kernel_neon_busy
);
23 * may_use_simd - whether it is allowable at this time to issue SIMD
24 * instructions or access the SIMD register file
26 * Callers must not assume that the result remains true beyond the next
27 * preempt_enable() or return from softirq context.
29 static __must_check
inline bool may_use_simd(void)
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.
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
44 return !in_irq() && !irqs_disabled() && !in_nmi() &&
45 !raw_cpu_read(kernel_neon_busy
);
48 #else /* ! CONFIG_KERNEL_MODE_NEON */
50 static __must_check
inline bool may_use_simd(void) {
54 #endif /* ! CONFIG_KERNEL_MODE_NEON */