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