blob: 89cba2622b79bab10c03737b414a522de6a29292 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Ard Biesheuvelf9601812017-08-03 17:23:19 +01002/*
3 * Copyright (C) 2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
Ard Biesheuvelf9601812017-08-03 17:23:19 +01004 */
5
6#ifndef __ASM_SIMD_H
7#define __ASM_SIMD_H
8
Dave Martincb84d112017-08-03 17:23:23 +01009#include <linux/compiler.h>
Dave Martin66c3ec52017-08-09 11:43:28 +010010#include <linux/irqflags.h>
Dave Martincb84d112017-08-03 17:23:23 +010011#include <linux/percpu.h>
12#include <linux/preempt.h>
Ard Biesheuvelf9601812017-08-03 17:23:19 +010013#include <linux/types.h>
14
Julien Grall6dcdefc2019-05-21 18:21:39 +010015DECLARE_PER_CPU(bool, fpsimd_context_busy);
Dave Martincb84d112017-08-03 17:23:23 +010016
Julien Grall6dcdefc2019-05-21 18:21:39 +010017#ifdef CONFIG_KERNEL_MODE_NEON
Dave Martincb84d112017-08-03 17:23:23 +010018
Ard Biesheuvelf9601812017-08-03 17:23:19 +010019/*
20 * may_use_simd - whether it is allowable at this time to issue SIMD
21 * instructions or access the SIMD register file
Dave Martincb84d112017-08-03 17:23:23 +010022 *
23 * Callers must not assume that the result remains true beyond the next
24 * preempt_enable() or return from softirq context.
Ard Biesheuvelf9601812017-08-03 17:23:19 +010025 */
26static __must_check inline bool may_use_simd(void)
27{
Dave Martincb84d112017-08-03 17:23:23 +010028 /*
Suzuki K Poulose0cd82fe2020-01-13 23:30:18 +000029 * We must make sure that the SVE has been initialized properly
30 * before using the SIMD in kernel.
Julien Grall6dcdefc2019-05-21 18:21:39 +010031 * fpsimd_context_busy is only set while preemption is disabled,
Yandong Zhao2fd8eb42018-07-11 19:06:28 +080032 * and is clear whenever preemption is enabled. Since
Julien Grall6dcdefc2019-05-21 18:21:39 +010033 * this_cpu_read() is atomic w.r.t. preemption, fpsimd_context_busy
Yandong Zhao2fd8eb42018-07-11 19:06:28 +080034 * 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 Martincb84d112017-08-03 17:23:23 +010037 */
Suzuki K Poulose0cd82fe2020-01-13 23:30:18 +000038 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 Biesheuvelf9601812017-08-03 17:23:19 +010042}
43
Dave Martincb84d112017-08-03 17:23:23 +010044#else /* ! CONFIG_KERNEL_MODE_NEON */
45
46static __must_check inline bool may_use_simd(void) {
47 return false;
48}
49
50#endif /* ! CONFIG_KERNEL_MODE_NEON */
51
Ard Biesheuvelf9601812017-08-03 17:23:19 +010052#endif