xref: /freebsd/sys/contrib/openzfs/include/os/linux/kernel/linux/simd_aarch64.h (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (C) 2016 Romain Dolbeau <romain@dolbeau.org>.
24  * Copyright (C) 2022 Tino Reichardt <milky-zfs@mcmilk.de>
25  * Copyright (C) 2022 Sebastian Gottschall <s.gottschall@dd-wrt.com>
26  */
27 
28 /*
29  * USER API:
30  *
31  * Kernel fpu methods:
32  *	kfpu_allowed()
33  *	kfpu_begin()
34  *	kfpu_end()
35  *	kfpu_init()
36  *	kfpu_fini()
37  *
38  * SIMD support:
39  *
40  * Following functions should be called to determine whether CPU feature
41  * is supported. All functions are usable in kernel and user space.
42  * If a SIMD algorithm is using more than one instruction set
43  * all relevant feature test functions should be called.
44  *
45  * Supported features:
46  *   zfs_neon_available()
47  *   zfs_sha256_available()
48  *   zfs_sha512_available()
49  */
50 
51 #ifndef _LINUX_SIMD_AARCH64_H
52 #define	_LINUX_SIMD_AARCH64_H
53 
54 #include <sys/types.h>
55 #include <asm/neon.h>
56 #include <asm/elf.h>
57 #include <asm/hwcap.h>
58 #include <linux/version.h>
59 #include <asm/sysreg.h>
60 
61 #define	ID_AA64PFR0_EL1		sys_reg(3, 0, 0, 1, 0)
62 #define	ID_AA64ISAR0_EL1	sys_reg(3, 0, 0, 6, 0)
63 
64 #if (defined(HAVE_KERNEL_NEON) && defined(CONFIG_KERNEL_MODE_NEON))
65 #define	kfpu_allowed()		1
66 #define	kfpu_begin()		kernel_neon_begin()
67 #define	kfpu_end()		kernel_neon_end()
68 #else
69 #define	kfpu_allowed()		0
70 #define	kfpu_begin()		do {} while (0)
71 #define	kfpu_end()		do {} while (0)
72 #endif
73 #define	kfpu_init()		(0)
74 #define	kfpu_fini()		do {} while (0)
75 
76 #define	get_ftr(id) {				\
77 	unsigned long __val;			\
78 	asm("mrs %0, "#id : "=r" (__val));	\
79 	__val;					\
80 }
81 
82 /*
83  * Check if NEON is available
84  */
85 static inline boolean_t
zfs_neon_available(void)86 zfs_neon_available(void)
87 {
88 	unsigned long ftr = ((get_ftr(ID_AA64PFR0_EL1)) >> 16) & 0xf;
89 	return (ftr == 0 || ftr == 1);
90 }
91 
92 /*
93  * Check if SHA256 is available
94  */
95 static inline boolean_t
zfs_sha256_available(void)96 zfs_sha256_available(void)
97 {
98 	unsigned long ftr = ((get_ftr(ID_AA64ISAR0_EL1)) >> 12) & 0x3;
99 	return (ftr & 0x1);
100 }
101 
102 /*
103  * Check if SHA512 is available
104  */
105 static inline boolean_t
zfs_sha512_available(void)106 zfs_sha512_available(void)
107 {
108 	unsigned long ftr = ((get_ftr(ID_AA64ISAR0_EL1)) >> 12) & 0x3;
109 	return (ftr & 0x2);
110 }
111 
112 #endif /* _LINUX_SIMD_AARCH64_H */
113