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 
60 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
61 #include <asm/sysreg.h>
62 #else
63 #define	sys_reg(op0, op1, crn, crm, op2) ( \
64 	((op0) << Op0_shift) | \
65 	((op1) << Op1_shift) | \
66 	((crn) << CRn_shift) | \
67 	((crm) << CRm_shift) | \
68 	((op2) << Op2_shift))
69 #endif
70 
71 #define	ID_AA64PFR0_EL1		sys_reg(3, 0, 0, 1, 0)
72 #define	ID_AA64ISAR0_EL1	sys_reg(3, 0, 0, 6, 0)
73 
74 #if (defined(HAVE_KERNEL_NEON) && defined(CONFIG_KERNEL_MODE_NEON))
75 #define	kfpu_allowed()		1
76 #define	kfpu_begin()		kernel_neon_begin()
77 #define	kfpu_end()		kernel_neon_end()
78 #else
79 #define	kfpu_allowed()		0
80 #define	kfpu_begin()		do {} while (0)
81 #define	kfpu_end()		do {} while (0)
82 #endif
83 #define	kfpu_init()		(0)
84 #define	kfpu_fini()		do {} while (0)
85 
86 #define	get_ftr(id) {				\
87 	unsigned long __val;			\
88 	asm("mrs %0, "#id : "=r" (__val));	\
89 	__val;					\
90 }
91 
92 /*
93  * Check if NEON is available
94  */
95 static inline boolean_t
zfs_neon_available(void)96 zfs_neon_available(void)
97 {
98 	unsigned long ftr = ((get_ftr(ID_AA64PFR0_EL1)) >> 16) & 0xf;
99 	return (ftr == 0 || ftr == 1);
100 }
101 
102 /*
103  * Check if SHA256 is available
104  */
105 static inline boolean_t
zfs_sha256_available(void)106 zfs_sha256_available(void)
107 {
108 	unsigned long ftr = ((get_ftr(ID_AA64ISAR0_EL1)) >> 12) & 0x3;
109 	return (ftr & 0x1);
110 }
111 
112 /*
113  * Check if SHA512 is available
114  */
115 static inline boolean_t
zfs_sha512_available(void)116 zfs_sha512_available(void)
117 {
118 	unsigned long ftr = ((get_ftr(ID_AA64ISAR0_EL1)) >> 12) & 0x3;
119 	return (ftr & 0x2);
120 }
121 
122 #endif /* _LINUX_SIMD_AARCH64_H */
123