Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (C) 2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
0004  */
0005 
0006 #ifndef __ASM_SIMD_H
0007 #define __ASM_SIMD_H
0008 
0009 #include <linux/compiler.h>
0010 #include <linux/irqflags.h>
0011 #include <linux/percpu.h>
0012 #include <linux/preempt.h>
0013 #include <linux/types.h>
0014 
0015 DECLARE_PER_CPU(bool, fpsimd_context_busy);
0016 
0017 #ifdef CONFIG_KERNEL_MODE_NEON
0018 
0019 /*
0020  * may_use_simd - whether it is allowable at this time to issue SIMD
0021  *                instructions or access the SIMD register file
0022  *
0023  * Callers must not assume that the result remains true beyond the next
0024  * preempt_enable() or return from softirq context.
0025  */
0026 static __must_check inline bool may_use_simd(void)
0027 {
0028     /*
0029      * We must make sure that the SVE has been initialized properly
0030      * before using the SIMD in kernel.
0031      * fpsimd_context_busy is only set while preemption is disabled,
0032      * and is clear whenever preemption is enabled. Since
0033      * this_cpu_read() is atomic w.r.t. preemption, fpsimd_context_busy
0034      * cannot change under our feet -- if it's set we cannot be
0035      * migrated, and if it's clear we cannot be migrated to a CPU
0036      * where it is set.
0037      */
0038     return !WARN_ON(!system_capabilities_finalized()) &&
0039            system_supports_fpsimd() &&
0040            !in_hardirq() && !irqs_disabled() && !in_nmi() &&
0041            !this_cpu_read(fpsimd_context_busy);
0042 }
0043 
0044 #else /* ! CONFIG_KERNEL_MODE_NEON */
0045 
0046 static __must_check inline bool may_use_simd(void) {
0047     return false;
0048 }
0049 
0050 #endif /* ! CONFIG_KERNEL_MODE_NEON */
0051 
0052 #endif