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.
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.
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]
22 * Copyright (C) 2016 Gvozden Neskovic <neskovic@compeng.uni-frankfurt.de>.
34 * Following functions should be called to determine whether CPU feature
35 * is supported. All functions are usable in kernel and user space.
36 * If a SIMD algorithm is using more than one instruction set
37 * all relevant feature test functions should be called.
41 * zfs_sse2_available()
42 * zfs_sse3_available()
43 * zfs_ssse3_available()
44 * zfs_sse4_1_available()
45 * zfs_sse4_2_available()
48 * zfs_avx2_available()
50 * zfs_bmi1_available()
51 * zfs_bmi2_available()
53 * zfs_avx512f_available()
54 * zfs_avx512cd_available()
55 * zfs_avx512er_available()
56 * zfs_avx512pf_available()
57 * zfs_avx512bw_available()
58 * zfs_avx512dq_available()
59 * zfs_avx512vl_available()
60 * zfs_avx512ifma_available()
61 * zfs_avx512vbmi_available()
63 * NOTE(AVX-512VL): If using AVX-512 instructions with 128Bit registers
64 * also add zfs_avx512vl_available() to feature check.
70 #include <sys/isa_defs.h>
75 #include <sys/types.h>
78 #include <asm/cpufeature.h>
84 #if defined(HAVE_FPU_API_H)
85 #include <asm/fpu/api.h>
86 #include <asm/fpu/internal.h>
87 #define kfpu_begin() \
90 __kernel_fpu_begin(); \
100 #define kfpu_begin() kernel_fpu_begin()
101 #define kfpu_end() kernel_fpu_end()
102 #endif /* defined(HAVE_FPU_API_H) */
105 * fpu dummy methods for userspace
107 #define kfpu_begin() do {} while (0)
108 #define kfpu_end() do {} while (0)
109 #endif /* defined(_KERNEL) */
112 * CPUID feature tests for user-space. Linux kernel provides an interface for
113 * CPU feature testing.
115 #if !defined(_KERNEL)
118 * x86 registers used implicitly by CPUID
120 typedef enum cpuid_regs
{
129 * List of instruction sets identified by CPUID
131 typedef enum cpuid_inst_sets
{
155 * Instruction set descriptor.
157 typedef struct cpuid_feature_desc
{
158 uint32_t leaf
; /* CPUID leaf */
159 uint32_t subleaf
; /* CPUID sub-leaf */
160 uint32_t flag
; /* bit mask of the feature */
161 cpuid_regs_t reg
; /* which CPUID return register to test */
162 } cpuid_feature_desc_t
;
164 #define _AVX512F_BIT (1U << 16)
165 #define _AVX512CD_BIT (_AVX512F_BIT | (1U << 28))
166 #define _AVX512DQ_BIT (_AVX512F_BIT | (1U << 17))
167 #define _AVX512BW_BIT (_AVX512F_BIT | (1U << 30))
168 #define _AVX512IFMA_BIT (_AVX512F_BIT | (1U << 21))
169 #define _AVX512VBMI_BIT (1U << 1) /* AVX512F_BIT is on another leaf */
170 #define _AVX512PF_BIT (_AVX512F_BIT | (1U << 26))
171 #define _AVX512ER_BIT (_AVX512F_BIT | (1U << 27))
172 #define _AVX512VL_BIT (1U << 31) /* if used also check other levels */
175 * Descriptions of supported instruction sets
177 static const cpuid_feature_desc_t cpuid_features
[] = {
178 [SSE
] = {1U, 0U, 1U << 25, EDX
},
179 [SSE2
] = {1U, 0U, 1U << 26, EDX
},
180 [SSE3
] = {1U, 0U, 1U << 0, ECX
},
181 [SSSE3
] = {1U, 0U, 1U << 9, ECX
},
182 [SSE4_1
] = {1U, 0U, 1U << 19, ECX
},
183 [SSE4_2
] = {1U, 0U, 1U << 20, ECX
},
184 [OSXSAVE
] = {1U, 0U, 1U << 27, ECX
},
185 [AVX
] = {1U, 0U, 1U << 28, ECX
},
186 [AVX2
] = {7U, 0U, 1U << 5, EBX
},
187 [BMI1
] = {7U, 0U, 1U << 3, EBX
},
188 [BMI2
] = {7U, 0U, 1U << 8, EBX
},
189 [AVX512F
] = {7U, 0U, _AVX512F_BIT
, EBX
},
190 [AVX512CD
] = {7U, 0U, _AVX512CD_BIT
, EBX
},
191 [AVX512DQ
] = {7U, 0U, _AVX512DQ_BIT
, EBX
},
192 [AVX512BW
] = {7U, 0U, _AVX512BW_BIT
, EBX
},
193 [AVX512IFMA
] = {7U, 0U, _AVX512IFMA_BIT
, EBX
},
194 [AVX512VBMI
] = {7U, 0U, _AVX512VBMI_BIT
, ECX
},
195 [AVX512PF
] = {7U, 0U, _AVX512PF_BIT
, EBX
},
196 [AVX512ER
] = {7U, 0U, _AVX512ER_BIT
, EBX
},
197 [AVX512VL
] = {7U, 0U, _AVX512ER_BIT
, EBX
}
201 * Check if OS supports AVX and AVX2 by checking XCR0
202 * Only call this function if CPUID indicates that AVX feature is
203 * supported by the CPU, otherwise it might be an illegal instruction.
205 static inline uint64_t
206 xgetbv(uint32_t index
)
209 /* xgetbv - instruction byte code */
210 __asm__
__volatile__(".byte 0x0f; .byte 0x01; .byte 0xd0"
211 : "=a" (eax
), "=d" (edx
)
214 return ((((uint64_t)edx
)<<32) | (uint64_t)eax
);
218 * Check if CPU supports a feature
220 static inline boolean_t
221 __cpuid_check_feature(const cpuid_feature_desc_t
*desc
)
223 uint32_t r
[CPUID_REG_CNT
];
225 if (__get_cpuid_max(0, NULL
) >= desc
->leaf
) {
227 * __cpuid_count is needed to properly check
228 * for AVX2. It is a macro, so return parameters
229 * are passed by value.
231 __cpuid_count(desc
->leaf
, desc
->subleaf
,
232 r
[EAX
], r
[EBX
], r
[ECX
], r
[EDX
]);
233 return ((r
[desc
->reg
] & desc
->flag
) == desc
->flag
);
238 #define CPUID_FEATURE_CHECK(name, id) \
239 static inline boolean_t \
240 __cpuid_has_ ## name(void) \
242 return (__cpuid_check_feature(&cpuid_features[id])); \
246 * Define functions for user-space CPUID features testing
248 CPUID_FEATURE_CHECK(sse
, SSE
);
249 CPUID_FEATURE_CHECK(sse2
, SSE2
);
250 CPUID_FEATURE_CHECK(sse3
, SSE3
);
251 CPUID_FEATURE_CHECK(ssse3
, SSSE3
);
252 CPUID_FEATURE_CHECK(sse4_1
, SSE4_1
);
253 CPUID_FEATURE_CHECK(sse4_2
, SSE4_2
);
254 CPUID_FEATURE_CHECK(avx
, AVX
);
255 CPUID_FEATURE_CHECK(avx2
, AVX2
);
256 CPUID_FEATURE_CHECK(osxsave
, OSXSAVE
);
257 CPUID_FEATURE_CHECK(bmi1
, BMI1
);
258 CPUID_FEATURE_CHECK(bmi2
, BMI2
);
259 CPUID_FEATURE_CHECK(avx512f
, AVX512F
);
260 CPUID_FEATURE_CHECK(avx512cd
, AVX512CD
);
261 CPUID_FEATURE_CHECK(avx512dq
, AVX512DQ
);
262 CPUID_FEATURE_CHECK(avx512bw
, AVX512BW
);
263 CPUID_FEATURE_CHECK(avx512ifma
, AVX512IFMA
);
264 CPUID_FEATURE_CHECK(avx512vbmi
, AVX512VBMI
);
265 CPUID_FEATURE_CHECK(avx512pf
, AVX512PF
);
266 CPUID_FEATURE_CHECK(avx512er
, AVX512ER
);
267 CPUID_FEATURE_CHECK(avx512vl
, AVX512VL
);
269 #endif /* !defined(_KERNEL) */
273 * Detect register set support
275 static inline boolean_t
276 __simd_state_enabled(const uint64_t state
)
278 boolean_t has_osxsave
;
281 #if defined(_KERNEL) && defined(X86_FEATURE_OSXSAVE)
282 has_osxsave
= !!boot_cpu_has(X86_FEATURE_OSXSAVE
);
283 #elif defined(_KERNEL) && !defined(X86_FEATURE_OSXSAVE)
284 has_osxsave
= B_FALSE
;
286 has_osxsave
= __cpuid_has_osxsave();
293 return ((xcr0
& state
) == state
);
296 #define _XSTATE_SSE_AVX (0x2 | 0x4)
297 #define _XSTATE_AVX512 (0xE0 | _XSTATE_SSE_AVX)
299 #define __ymm_enabled() __simd_state_enabled(_XSTATE_SSE_AVX)
300 #define __zmm_enabled() __simd_state_enabled(_XSTATE_AVX512)
304 * Check if SSE instruction set is available
306 static inline boolean_t
307 zfs_sse_available(void)
310 return (!!boot_cpu_has(X86_FEATURE_XMM
));
312 return (__cpuid_has_sse());
317 * Check if SSE2 instruction set is available
319 static inline boolean_t
320 zfs_sse2_available(void)
323 return (!!boot_cpu_has(X86_FEATURE_XMM2
));
325 return (__cpuid_has_sse2());
330 * Check if SSE3 instruction set is available
332 static inline boolean_t
333 zfs_sse3_available(void)
336 return (!!boot_cpu_has(X86_FEATURE_XMM3
));
338 return (__cpuid_has_sse3());
343 * Check if SSSE3 instruction set is available
345 static inline boolean_t
346 zfs_ssse3_available(void)
349 return (!!boot_cpu_has(X86_FEATURE_SSSE3
));
351 return (__cpuid_has_ssse3());
356 * Check if SSE4.1 instruction set is available
358 static inline boolean_t
359 zfs_sse4_1_available(void)
362 return (!!boot_cpu_has(X86_FEATURE_XMM4_1
));
364 return (__cpuid_has_sse4_1());
369 * Check if SSE4.2 instruction set is available
371 static inline boolean_t
372 zfs_sse4_2_available(void)
375 return (!!boot_cpu_has(X86_FEATURE_XMM4_2
));
377 return (__cpuid_has_sse4_2());
382 * Check if AVX instruction set is available
384 static inline boolean_t
385 zfs_avx_available(void)
389 has_avx
= !!boot_cpu_has(X86_FEATURE_AVX
);
391 has_avx
= __cpuid_has_avx();
394 return (has_avx
&& __ymm_enabled());
398 * Check if AVX2 instruction set is available
400 static inline boolean_t
401 zfs_avx2_available(void)
404 #if defined(_KERNEL) && defined(X86_FEATURE_AVX2)
405 has_avx2
= !!boot_cpu_has(X86_FEATURE_AVX2
);
406 #elif defined(_KERNEL) && !defined(X86_FEATURE_AVX2)
409 has_avx2
= __cpuid_has_avx2();
412 return (has_avx2
&& __ymm_enabled());
416 * Check if BMI1 instruction set is available
418 static inline boolean_t
419 zfs_bmi1_available(void)
421 #if defined(_KERNEL) && defined(X86_FEATURE_BMI1)
422 return (!!boot_cpu_has(X86_FEATURE_BMI1
));
423 #elif defined(_KERNEL) && !defined(X86_FEATURE_BMI1)
426 return (__cpuid_has_bmi1());
431 * Check if BMI2 instruction set is available
433 static inline boolean_t
434 zfs_bmi2_available(void)
436 #if defined(_KERNEL) && defined(X86_FEATURE_BMI2)
437 return (!!boot_cpu_has(X86_FEATURE_BMI2
));
438 #elif defined(_KERNEL) && !defined(X86_FEATURE_BMI2)
441 return (__cpuid_has_bmi2());
447 * AVX-512 family of instruction sets:
450 * AVX512CD Conflict Detection Instructions
451 * AVX512ER Exponential and Reciprocal Instructions
452 * AVX512PF Prefetch Instructions
454 * AVX512BW Byte and Word Instructions
455 * AVX512DQ Double-word and Quadword Instructions
456 * AVX512VL Vector Length Extensions
458 * AVX512IFMA Integer Fused Multiply Add (Not supported by kernel 4.4)
459 * AVX512VBMI Vector Byte Manipulation Instructions
463 /* Check if AVX512F instruction set is available */
464 static inline boolean_t
465 zfs_avx512f_available(void)
467 boolean_t has_avx512
= B_FALSE
;
469 #if defined(_KERNEL) && defined(X86_FEATURE_AVX512F)
470 has_avx512
= !!boot_cpu_has(X86_FEATURE_AVX512F
);
471 #elif !defined(_KERNEL)
472 has_avx512
= __cpuid_has_avx512f();
475 return (has_avx512
&& __zmm_enabled());
478 /* Check if AVX512CD instruction set is available */
479 static inline boolean_t
480 zfs_avx512cd_available(void)
482 boolean_t has_avx512
= B_FALSE
;
484 #if defined(_KERNEL) && defined(X86_FEATURE_AVX512CD)
485 has_avx512
= boot_cpu_has(X86_FEATURE_AVX512F
) &&
486 boot_cpu_has(X86_FEATURE_AVX512CD
);
487 #elif !defined(_KERNEL)
488 has_avx512
= __cpuid_has_avx512cd();
491 return (has_avx512
&& __zmm_enabled());
494 /* Check if AVX512ER instruction set is available */
495 static inline boolean_t
496 zfs_avx512er_available(void)
498 boolean_t has_avx512
= B_FALSE
;
500 #if defined(_KERNEL) && defined(X86_FEATURE_AVX512ER)
501 has_avx512
= boot_cpu_has(X86_FEATURE_AVX512F
) &&
502 boot_cpu_has(X86_FEATURE_AVX512ER
);
503 #elif !defined(_KERNEL)
504 has_avx512
= __cpuid_has_avx512er();
507 return (has_avx512
&& __zmm_enabled());
510 /* Check if AVX512PF instruction set is available */
511 static inline boolean_t
512 zfs_avx512pf_available(void)
514 boolean_t has_avx512
= B_FALSE
;
516 #if defined(_KERNEL) && defined(X86_FEATURE_AVX512PF)
517 has_avx512
= boot_cpu_has(X86_FEATURE_AVX512F
) &&
518 boot_cpu_has(X86_FEATURE_AVX512PF
);
519 #elif !defined(_KERNEL)
520 has_avx512
= __cpuid_has_avx512pf();
523 return (has_avx512
&& __zmm_enabled());
526 /* Check if AVX512BW instruction set is available */
527 static inline boolean_t
528 zfs_avx512bw_available(void)
530 boolean_t has_avx512
= B_FALSE
;
532 #if defined(_KERNEL) && defined(X86_FEATURE_AVX512BW)
533 has_avx512
= boot_cpu_has(X86_FEATURE_AVX512F
) &&
534 boot_cpu_has(X86_FEATURE_AVX512BW
);
535 #elif !defined(_KERNEL)
536 has_avx512
= __cpuid_has_avx512bw();
539 return (has_avx512
&& __zmm_enabled());
542 /* Check if AVX512DQ instruction set is available */
543 static inline boolean_t
544 zfs_avx512dq_available(void)
546 boolean_t has_avx512
= B_FALSE
;
548 #if defined(_KERNEL) && defined(X86_FEATURE_AVX512DQ)
549 has_avx512
= boot_cpu_has(X86_FEATURE_AVX512F
) &&
550 boot_cpu_has(X86_FEATURE_AVX512DQ
);
551 #elif !defined(_KERNEL)
552 has_avx512
= __cpuid_has_avx512dq();
555 return (has_avx512
&& __zmm_enabled());
558 /* Check if AVX512VL instruction set is available */
559 static inline boolean_t
560 zfs_avx512vl_available(void)
562 boolean_t has_avx512
= B_FALSE
;
564 #if defined(_KERNEL) && defined(X86_FEATURE_AVX512VL)
565 has_avx512
= boot_cpu_has(X86_FEATURE_AVX512F
) &&
566 boot_cpu_has(X86_FEATURE_AVX512VL
);
567 #elif !defined(_KERNEL)
568 has_avx512
= __cpuid_has_avx512vl();
571 return (has_avx512
&& __zmm_enabled());
574 /* Check if AVX512IFMA instruction set is available */
575 static inline boolean_t
576 zfs_avx512ifma_available(void)
578 boolean_t has_avx512
= B_FALSE
;
580 #if defined(_KERNEL) && defined(X86_FEATURE_AVX512IFMA)
581 has_avx512
= boot_cpu_has(X86_FEATURE_AVX512F
) &&
582 boot_cpu_has(X86_FEATURE_AVX512IFMA
);
583 #elif !defined(_KERNEL)
584 has_avx512
= __cpuid_has_avx512ifma();
587 return (has_avx512
&& __zmm_enabled());
590 /* Check if AVX512VBMI instruction set is available */
591 static inline boolean_t
592 zfs_avx512vbmi_available(void)
594 boolean_t has_avx512
= B_FALSE
;
596 #if defined(_KERNEL) && defined(X86_FEATURE_AVX512VBMI)
597 has_avx512
= boot_cpu_has(X86_FEATURE_AVX512F
) &&
598 boot_cpu_has(X86_FEATURE_AVX512VBMI
);
599 #elif !defined(_KERNEL)
600 has_avx512
= __cpuid_has_avx512f() &&
601 __cpuid_has_avx512vbmi();
604 return (has_avx512
&& __zmm_enabled());
607 #endif /* defined(__x86) */
609 #endif /* _SIMD_X86_H */