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 #define	kfpu_allowed()		1
75 #define	kfpu_begin()		kernel_neon_begin()
76 #define	kfpu_end()		kernel_neon_end()
77 #define	kfpu_init()		(0)
78 #define	kfpu_fini()		do {} while (0)
79 
80 #define	get_ftr(id) {				\
81 	unsigned long __val;			\
82 	asm("mrs %0, "#id : "=r" (__val));	\
83 	__val;					\
84 }
85 
86 /*
87  * Check if NEON is available
88  */
89 static inline boolean_t
90 zfs_neon_available(void)
91 {
92 	unsigned long ftr = ((get_ftr(ID_AA64PFR0_EL1)) >> 16) & 0xf;
93 	return (ftr == 0 || ftr == 1);
94 }
95 
96 /*
97  * Check if SHA256 is available
98  */
99 static inline boolean_t
100 zfs_sha256_available(void)
101 {
102 	unsigned long ftr = ((get_ftr(ID_AA64ISAR0_EL1)) >> 12) & 0x3;
103 	return (ftr & 0x1);
104 }
105 
106 /*
107  * Check if SHA512 is available
108  */
109 static inline boolean_t
110 zfs_sha512_available(void)
111 {
112 	unsigned long ftr = ((get_ftr(ID_AA64ISAR0_EL1)) >> 12) & 0x3;
113 	return (ftr & 0x2);
114 }
115 
116 #endif /* _LINUX_SIMD_AARCH64_H */
117