1 /**************************************************************************
3 * Copyright 2008 Dennis Smit
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 * CPU feature detection.
32 * @author Based on the work of Eric Anholt <anholt@FreeBSD.org>
35 #include "pipe/p_config.h"
38 #include "u_cpu_detect.h"
40 #if defined(PIPE_ARCH_PPC)
41 #if defined(PIPE_OS_APPLE)
42 #include <sys/sysctl.h>
49 #if defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD)
50 #include <sys/param.h>
51 #include <sys/sysctl.h>
52 #include <machine/cpu.h>
55 #if defined(PIPE_OS_FREEBSD)
56 #include <sys/types.h>
57 #include <sys/sysctl.h>
60 #if defined(PIPE_OS_LINUX)
68 #if defined(PIPE_OS_WINDOWS)
70 #if defined(PIPE_CC_MSVC)
77 DEBUG_GET_ONCE_BOOL_OPTION(dump_cpu
, "GALLIUM_DUMP_CPU", FALSE
)
81 struct util_cpu_caps util_cpu_caps
;
83 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
84 static int has_cpuid(void);
88 #if defined(PIPE_ARCH_PPC) && !defined(PIPE_OS_APPLE)
89 static jmp_buf __lv_powerpc_jmpbuf
;
90 static volatile sig_atomic_t __lv_powerpc_canjump
= 0;
93 sigill_handler(int sig
)
95 if (!__lv_powerpc_canjump
) {
96 signal (sig
, SIG_DFL
);
100 __lv_powerpc_canjump
= 0;
101 longjmp(__lv_powerpc_jmpbuf
, 1);
105 #if defined(PIPE_ARCH_PPC)
107 check_os_altivec_support(void)
109 #if defined(PIPE_OS_APPLE)
110 int sels
[2] = {CTL_HW
, HW_VECTORUNIT
};
112 int len
= sizeof (has_vu
);
115 err
= sysctl(sels
, 2, &has_vu
, &len
, NULL
, 0);
119 util_cpu_caps
.has_altivec
= 1;
122 #else /* !PIPE_OS_APPLE */
123 /* not on Apple/Darwin, do it the brute-force way */
124 /* this is borrowed from the libmpeg2 library */
125 signal(SIGILL
, sigill_handler
);
126 if (setjmp(__lv_powerpc_jmpbuf
)) {
127 signal(SIGILL
, SIG_DFL
);
129 __lv_powerpc_canjump
= 1;
133 "vand %%v0, %%v0, %%v0"
137 signal(SIGILL
, SIG_DFL
);
138 util_cpu_caps
.has_altivec
= 1;
140 #endif /* !PIPE_OS_APPLE */
142 #endif /* PIPE_ARCH_PPC */
145 #if defined(PIPE_ARCH_X86) || defined (PIPE_ARCH_X86_64)
146 static int has_cpuid(void)
148 #if defined(PIPE_ARCH_X86)
149 #if defined(PIPE_OS_GCC)
156 "xorl $0x200000, %0\n"
170 #elif defined(PIPE_ARCH_X86_64)
179 * @sa cpuid.h included in gcc-4.3 onwards.
180 * @sa http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
183 cpuid(uint32_t ax
, uint32_t *p
)
185 #if (defined(PIPE_CC_GCC) || defined(PIPE_CC_SUNPRO)) && defined(PIPE_ARCH_X86)
187 "xchgl %%ebx, %1\n\t"
196 #elif (defined(PIPE_CC_GCC) || defined(PIPE_CC_SUNPRO)) && defined(PIPE_ARCH_X86_64)
205 #elif defined(PIPE_CC_MSVC)
215 static INLINE
uint64_t xgetbv(void)
217 #if defined(PIPE_CC_GCC)
221 ".byte 0x0f, 0x01, 0xd0" // xgetbv isn't supported on gcc < 4.4
227 return ((uint64_t)edx
<< 32) | eax
;
228 #elif defined(PIPE_CC_MSVC) && defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
229 return _xgetbv(_XCR_XFEATURE_ENABLED_MASK
);
236 #if defined(PIPE_ARCH_X86)
237 static INLINE boolean
sse2_has_daz(void)
242 uint32_t pad2
[128-8];
243 } PIPE_ALIGN_VAR(16) fxarea
;
245 fxarea
.mxcsr_mask
= 0;
246 #if (defined(PIPE_CC_GCC) || defined(PIPE_CC_SUNPRO))
247 asm volatile ("fxsave %0" :: "m" (fxarea
));
248 #elif (defined(PIPE_CC_MSVC) || defined(PIPE_CC_ICL))
251 return !!(fxarea
.mxcsr_mask
& (1 << 6));
255 #endif /* X86 or X86_64 */
258 util_cpu_detect(void)
260 static boolean util_cpu_detect_initialized
= FALSE
;
262 if(util_cpu_detect_initialized
)
265 memset(&util_cpu_caps
, 0, sizeof util_cpu_caps
);
267 /* Count the number of CPUs in system */
268 #if defined(PIPE_OS_WINDOWS)
270 SYSTEM_INFO system_info
;
271 GetSystemInfo(&system_info
);
272 util_cpu_caps
.nr_cpus
= system_info
.dwNumberOfProcessors
;
274 #elif defined(PIPE_OS_UNIX) && defined(_SC_NPROCESSORS_ONLN)
275 util_cpu_caps
.nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
276 if (util_cpu_caps
.nr_cpus
== -1)
277 util_cpu_caps
.nr_cpus
= 1;
278 #elif defined(PIPE_OS_BSD)
287 sysctl(mib
, 2, &ncpu
, &len
, NULL
, 0);
288 util_cpu_caps
.nr_cpus
= ncpu
;
291 util_cpu_caps
.nr_cpus
= 1;
294 /* Make the fallback cacheline size nonzero so that it can be
295 * safely passed to align().
297 util_cpu_caps
.cacheline
= sizeof(void *);
299 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
304 util_cpu_caps
.cacheline
= 32;
306 /* Get max cpuid level */
307 cpuid(0x00000000, regs
);
309 if (regs
[0] >= 0x00000001) {
310 unsigned int cacheline
;
312 cpuid (0x00000001, regs2
);
314 util_cpu_caps
.x86_cpu_type
= (regs2
[0] >> 8) & 0xf;
315 if (util_cpu_caps
.x86_cpu_type
== 0xf)
316 util_cpu_caps
.x86_cpu_type
= 8 + ((regs2
[0] >> 20) & 255); /* use extended family (P4, IA64) */
318 /* general feature flags */
319 util_cpu_caps
.has_tsc
= (regs2
[3] >> 4) & 1; /* 0x0000010 */
320 util_cpu_caps
.has_mmx
= (regs2
[3] >> 23) & 1; /* 0x0800000 */
321 util_cpu_caps
.has_sse
= (regs2
[3] >> 25) & 1; /* 0x2000000 */
322 util_cpu_caps
.has_sse2
= (regs2
[3] >> 26) & 1; /* 0x4000000 */
323 util_cpu_caps
.has_sse3
= (regs2
[2] >> 0) & 1; /* 0x0000001 */
324 util_cpu_caps
.has_ssse3
= (regs2
[2] >> 9) & 1; /* 0x0000020 */
325 util_cpu_caps
.has_sse4_1
= (regs2
[2] >> 19) & 1;
326 util_cpu_caps
.has_sse4_2
= (regs2
[2] >> 20) & 1;
327 util_cpu_caps
.has_popcnt
= (regs2
[2] >> 23) & 1;
328 util_cpu_caps
.has_avx
= ((regs2
[2] >> 28) & 1) && // AVX
329 ((regs2
[2] >> 27) & 1) && // OSXSAVE
330 ((xgetbv() & 6) == 6); // XMM & YMM
331 util_cpu_caps
.has_f16c
= (regs2
[2] >> 29) & 1;
332 util_cpu_caps
.has_mmx2
= util_cpu_caps
.has_sse
; /* SSE cpus supports mmxext too */
333 #if defined(PIPE_ARCH_X86_64)
334 util_cpu_caps
.has_daz
= 1;
336 util_cpu_caps
.has_daz
= util_cpu_caps
.has_sse3
||
337 (util_cpu_caps
.has_sse2
&& sse2_has_daz());
340 cacheline
= ((regs2
[1] >> 8) & 0xFF) * 8;
342 util_cpu_caps
.cacheline
= cacheline
;
345 if (regs
[1] == 0x756e6547 && regs
[2] == 0x6c65746e && regs
[3] == 0x49656e69) {
347 util_cpu_caps
.has_intel
= 1;
350 cpuid(0x80000000, regs
);
352 if (regs
[0] >= 0x80000001) {
354 cpuid(0x80000001, regs2
);
356 util_cpu_caps
.has_mmx
|= (regs2
[3] >> 23) & 1;
357 util_cpu_caps
.has_mmx2
|= (regs2
[3] >> 22) & 1;
358 util_cpu_caps
.has_3dnow
= (regs2
[3] >> 31) & 1;
359 util_cpu_caps
.has_3dnow_ext
= (regs2
[3] >> 30) & 1;
362 if (regs
[0] >= 0x80000006) {
363 cpuid(0x80000006, regs2
);
364 util_cpu_caps
.cacheline
= regs2
[2] & 0xFF;
367 if (!util_cpu_caps
.has_sse
) {
368 util_cpu_caps
.has_sse2
= 0;
369 util_cpu_caps
.has_sse3
= 0;
370 util_cpu_caps
.has_ssse3
= 0;
371 util_cpu_caps
.has_sse4_1
= 0;
374 #endif /* PIPE_ARCH_X86 || PIPE_ARCH_X86_64 */
376 #if defined(PIPE_ARCH_PPC)
377 check_os_altivec_support();
378 #endif /* PIPE_ARCH_PPC */
381 if (debug_get_option_dump_cpu()) {
382 debug_printf("util_cpu_caps.nr_cpus = %u\n", util_cpu_caps
.nr_cpus
);
384 debug_printf("util_cpu_caps.x86_cpu_type = %u\n", util_cpu_caps
.x86_cpu_type
);
385 debug_printf("util_cpu_caps.cacheline = %u\n", util_cpu_caps
.cacheline
);
387 debug_printf("util_cpu_caps.has_tsc = %u\n", util_cpu_caps
.has_tsc
);
388 debug_printf("util_cpu_caps.has_mmx = %u\n", util_cpu_caps
.has_mmx
);
389 debug_printf("util_cpu_caps.has_mmx2 = %u\n", util_cpu_caps
.has_mmx2
);
390 debug_printf("util_cpu_caps.has_sse = %u\n", util_cpu_caps
.has_sse
);
391 debug_printf("util_cpu_caps.has_sse2 = %u\n", util_cpu_caps
.has_sse2
);
392 debug_printf("util_cpu_caps.has_sse3 = %u\n", util_cpu_caps
.has_sse3
);
393 debug_printf("util_cpu_caps.has_ssse3 = %u\n", util_cpu_caps
.has_ssse3
);
394 debug_printf("util_cpu_caps.has_sse4_1 = %u\n", util_cpu_caps
.has_sse4_1
);
395 debug_printf("util_cpu_caps.has_sse4_2 = %u\n", util_cpu_caps
.has_sse4_2
);
396 debug_printf("util_cpu_caps.has_avx = %u\n", util_cpu_caps
.has_avx
);
397 debug_printf("util_cpu_caps.has_f16c = %u\n", util_cpu_caps
.has_f16c
);
398 debug_printf("util_cpu_caps.has_popcnt = %u\n", util_cpu_caps
.has_popcnt
);
399 debug_printf("util_cpu_caps.has_3dnow = %u\n", util_cpu_caps
.has_3dnow
);
400 debug_printf("util_cpu_caps.has_3dnow_ext = %u\n", util_cpu_caps
.has_3dnow_ext
);
401 debug_printf("util_cpu_caps.has_altivec = %u\n", util_cpu_caps
.has_altivec
);
402 debug_printf("util_cpu_caps.has_daz = %u\n", util_cpu_caps
.has_daz
);
406 util_cpu_detect_initialized
= TRUE
;