xref: /freebsd-src/sys/contrib/openzfs/include/os/linux/kernel/linux/simd_powerpc.h (revision e67e85659c0de33e617e5fbf1028c6e8b49eee53)
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 http://www.opensolaris.org/os/licensing.
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  * Copyright (C) 2019 Romain Dolbeau
23  *           <romain.dolbeau@european-processor-initiative.eu>
24  */
25 
26 /*
27  * USER API:
28  *
29  * Kernel fpu methods:
30  *	kfpu_allowed()
31  *	kfpu_begin()
32  *	kfpu_end()
33  *	kfpu_init()
34  *	kfpu_fini()
35  *
36  * SIMD support:
37  *
38  * Following functions should be called to determine whether CPU feature
39  * is supported. All functions are usable in kernel and user space.
40  * If a SIMD algorithm is using more than one instruction set
41  * all relevant feature test functions should be called.
42  *
43  * Supported features:
44  *	zfs_altivec_available()
45  */
46 
47 #ifndef _LINUX_SIMD_POWERPC_H
48 #define	_LINUX_SIMD_POWERPC_H
49 
50 /* only for __powerpc__ */
51 #if defined(__powerpc__)
52 
53 #include <linux/preempt.h>
54 #include <linux/export.h>
55 #include <linux/sched.h>
56 #include <asm/switch_to.h>
57 #include <sys/types.h>
58 #include <linux/version.h>
59 
60 #define	kfpu_allowed()			1
61 
62 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
63 #define	kfpu_end()				\
64 	{					\
65 		disable_kernel_vsx();		\
66 		disable_kernel_altivec();	\
67 		preempt_enable();		\
68 	}
69 #define	kfpu_begin()				\
70 	{					\
71 		preempt_disable();		\
72 		enable_kernel_altivec();	\
73 		enable_kernel_vsx();		\
74 	}
75 #else
76 /* seems that before 4.5 no-one bothered */
77 #define	kfpu_begin()
78 #define	kfpu_end()		preempt_enable()
79 #endif
80 #define	kfpu_init()		0
81 #define	kfpu_fini()		((void) 0)
82 
83 static inline boolean_t
84 zfs_vsx_available(void)
85 {
86 	boolean_t res;
87 #if defined(__powerpc64__)
88 	u64 msr;
89 #else
90 	u32 msr;
91 #endif
92 	kfpu_begin();
93 	__asm volatile("mfmsr %0" : "=r"(msr));
94 	res = (msr & 0x800000) != 0;
95 	kfpu_end();
96 	return (res);
97 }
98 
99 /*
100  * Check if AltiVec instruction set is available
101  */
102 static inline boolean_t
103 zfs_altivec_available(void)
104 {
105 	boolean_t res;
106 	/* suggested by macallan at netbsd dot org */
107 #if defined(__powerpc64__)
108 	u64 msr;
109 #else
110 	u32 msr;
111 #endif
112 	kfpu_begin();
113 	__asm volatile("mfmsr %0" : "=r"(msr));
114 	/*
115 	 * 64 bits -> need to check bit 38
116 	 * Power ISA Version 3.0B
117 	 * p944
118 	 * 32 bits -> Need to check bit 6
119 	 * AltiVec Technology Programming Environments Manual
120 	 * p49 (2-9)
121 	 * They are the same, as ppc counts 'backward' ...
122 	 */
123 	res = (msr & 0x2000000) != 0;
124 	kfpu_end();
125 	return (res);
126 }
127 #endif /* defined(__powerpc) */
128 
129 #endif /* _LINUX_SIMD_POWERPC_H */
130