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  */
may_use_simd(void)29 static __must_check inline bool may_use_simd(void)
30 {
31 	/*
32 	 * kernel_neon_busy is only set while preemption is disabled,
33 	 * and is clear whenever preemption is enabled. Since
34 	 * this_cpu_read() is atomic w.r.t. preemption, kernel_neon_busy
35 	 * cannot change under our feet -- if it's set we cannot be
36 	 * migrated, and if it's clear we cannot be migrated to a CPU
37 	 * where it is set.
38 	 */
39 	return !in_irq() && !irqs_disabled() && !in_nmi() &&
40 		!this_cpu_read(kernel_neon_busy);
41 }
42 
43 #else /* ! CONFIG_KERNEL_MODE_NEON */
44 
may_use_simd(void)45 static __must_check inline bool may_use_simd(void) {
46 	return false;
47 }
48 
49 #endif /* ! CONFIG_KERNEL_MODE_NEON */
50 
51 #endif
52