Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Ard Biesheuvel | f960181 | 2017-08-03 17:23:19 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Linaro Ltd. <ard.biesheuvel@linaro.org> |
Ard Biesheuvel | f960181 | 2017-08-03 17:23:19 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef __ASM_SIMD_H |
| 7 | #define __ASM_SIMD_H |
| 8 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 9 | #include <linux/compiler.h> |
Dave Martin | 66c3ec5 | 2017-08-09 11:43:28 +0100 | [diff] [blame] | 10 | #include <linux/irqflags.h> |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 11 | #include <linux/percpu.h> |
| 12 | #include <linux/preempt.h> |
Ard Biesheuvel | f960181 | 2017-08-03 17:23:19 +0100 | [diff] [blame] | 13 | #include <linux/types.h> |
| 14 | |
Julien Grall | 6dcdefc | 2019-05-21 18:21:39 +0100 | [diff] [blame] | 15 | DECLARE_PER_CPU(bool, fpsimd_context_busy); |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 16 | |
Julien Grall | 6dcdefc | 2019-05-21 18:21:39 +0100 | [diff] [blame] | 17 | #ifdef CONFIG_KERNEL_MODE_NEON |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 18 | |
Ard Biesheuvel | f960181 | 2017-08-03 17:23:19 +0100 | [diff] [blame] | 19 | /* |
| 20 | * may_use_simd - whether it is allowable at this time to issue SIMD |
| 21 | * instructions or access the SIMD register file |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 22 | * |
| 23 | * Callers must not assume that the result remains true beyond the next |
| 24 | * preempt_enable() or return from softirq context. |
Ard Biesheuvel | f960181 | 2017-08-03 17:23:19 +0100 | [diff] [blame] | 25 | */ |
| 26 | static __must_check inline bool may_use_simd(void) |
| 27 | { |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 28 | /* |
Suzuki K Poulose | 0cd82fe | 2020-01-13 23:30:18 +0000 | [diff] [blame] | 29 | * We must make sure that the SVE has been initialized properly |
| 30 | * before using the SIMD in kernel. |
Julien Grall | 6dcdefc | 2019-05-21 18:21:39 +0100 | [diff] [blame] | 31 | * fpsimd_context_busy is only set while preemption is disabled, |
Yandong Zhao | 2fd8eb4 | 2018-07-11 19:06:28 +0800 | [diff] [blame] | 32 | * and is clear whenever preemption is enabled. Since |
Julien Grall | 6dcdefc | 2019-05-21 18:21:39 +0100 | [diff] [blame] | 33 | * this_cpu_read() is atomic w.r.t. preemption, fpsimd_context_busy |
Yandong Zhao | 2fd8eb4 | 2018-07-11 19:06:28 +0800 | [diff] [blame] | 34 | * cannot change under our feet -- if it's set we cannot be |
| 35 | * migrated, and if it's clear we cannot be migrated to a CPU |
| 36 | * where it is set. |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 37 | */ |
Suzuki K Poulose | 0cd82fe | 2020-01-13 23:30:18 +0000 | [diff] [blame] | 38 | return !WARN_ON(!system_capabilities_finalized()) && |
| 39 | system_supports_fpsimd() && |
| 40 | !in_irq() && !irqs_disabled() && !in_nmi() && |
| 41 | !this_cpu_read(fpsimd_context_busy); |
Ard Biesheuvel | f960181 | 2017-08-03 17:23:19 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 44 | #else /* ! CONFIG_KERNEL_MODE_NEON */ |
| 45 | |
| 46 | static __must_check inline bool may_use_simd(void) { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | #endif /* ! CONFIG_KERNEL_MODE_NEON */ |
| 51 | |
Ard Biesheuvel | f960181 | 2017-08-03 17:23:19 +0100 | [diff] [blame] | 52 | #endif |