2008-03-10 16:45:19 +00:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright 2008 Dennis Smit
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* on the rights to use, copy, modify, merge, publish, distribute, sub
|
|
|
|
* license, and/or sell copies of the Software, and to permit persons to whom
|
|
|
|
* the Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* CPU feature detection.
|
|
|
|
*
|
|
|
|
* @author Dennis Smit
|
|
|
|
* @author Based on the work of Eric Anholt <anholt@FreeBSD.org>
|
2008-03-10 16:45:19 +00:00
|
|
|
*/
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
#include "pipe/p_config.h"
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
#include "u_debug.h"
|
2008-03-10 16:45:19 +00:00
|
|
|
#include "u_cpu_detect.h"
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
#if defined(PIPE_ARCH_PPC)
|
2010-07-24 13:59:40 +01:00
|
|
|
#if defined(PIPE_OS_APPLE)
|
2008-03-10 16:45:19 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#else
|
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
#if defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD)
|
2008-03-10 16:45:19 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
#endif
|
|
|
|
|
2016-01-17 09:10:21 +00:00
|
|
|
#if defined(PIPE_OS_FREEBSD) || defined(PIPE_OS_DRAGONFLY)
|
2008-03-10 16:45:19 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
#if defined(PIPE_OS_LINUX)
|
2008-03-10 16:45:19 +00:00
|
|
|
#include <signal.h>
|
2017-04-13 19:43:30 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <elf.h>
|
2008-03-10 16:45:19 +00:00
|
|
|
#endif
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
#ifdef PIPE_OS_UNIX
|
|
|
|
#include <unistd.h>
|
2008-03-10 16:45:19 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-13 19:43:30 +01:00
|
|
|
#if defined(PIPE_OS_ANDROID)
|
|
|
|
#include <cpu-features.h>
|
|
|
|
#endif
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
#if defined(PIPE_OS_WINDOWS)
|
|
|
|
#include <windows.h>
|
2013-07-23 18:08:45 +01:00
|
|
|
#if defined(PIPE_CC_MSVC)
|
2009-10-27 20:29:19 +00:00
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
2009-09-29 13:25:08 +01:00
|
|
|
#endif
|
2008-03-10 16:45:19 +00:00
|
|
|
|
|
|
|
|
2010-08-22 07:36:30 +01:00
|
|
|
#ifdef DEBUG
|
2010-08-15 01:00:42 +01:00
|
|
|
DEBUG_GET_ONCE_BOOL_OPTION(dump_cpu, "GALLIUM_DUMP_CPU", FALSE)
|
2010-08-22 07:28:52 +01:00
|
|
|
#endif
|
2010-08-06 01:21:49 +01:00
|
|
|
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
struct util_cpu_caps util_cpu_caps;
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2010-07-27 03:48:29 +01:00
|
|
|
#if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
|
2008-03-10 16:45:19 +00:00
|
|
|
static int has_cpuid(void);
|
2010-07-27 03:48:29 +01:00
|
|
|
#endif
|
|
|
|
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2010-07-24 13:59:40 +01:00
|
|
|
#if defined(PIPE_ARCH_PPC) && !defined(PIPE_OS_APPLE)
|
2009-10-18 16:28:33 +01:00
|
|
|
static jmp_buf __lv_powerpc_jmpbuf;
|
2008-03-10 16:45:19 +00:00
|
|
|
static volatile sig_atomic_t __lv_powerpc_canjump = 0;
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
static void
|
|
|
|
sigill_handler(int sig)
|
2008-03-10 16:45:19 +00:00
|
|
|
{
|
2009-09-29 13:25:08 +01:00
|
|
|
if (!__lv_powerpc_canjump) {
|
|
|
|
signal (sig, SIG_DFL);
|
|
|
|
raise (sig);
|
|
|
|
}
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
__lv_powerpc_canjump = 0;
|
2009-10-18 16:28:33 +01:00
|
|
|
longjmp(__lv_powerpc_jmpbuf, 1);
|
2008-03-10 16:45:19 +00:00
|
|
|
}
|
2009-10-18 16:28:33 +01:00
|
|
|
#endif
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-10-18 16:28:33 +01:00
|
|
|
#if defined(PIPE_ARCH_PPC)
|
2009-09-29 13:25:08 +01:00
|
|
|
static void
|
|
|
|
check_os_altivec_support(void)
|
2008-03-10 16:45:19 +00:00
|
|
|
{
|
2010-07-24 13:59:40 +01:00
|
|
|
#if defined(PIPE_OS_APPLE)
|
2009-09-29 13:25:08 +01:00
|
|
|
int sels[2] = {CTL_HW, HW_VECTORUNIT};
|
|
|
|
int has_vu = 0;
|
|
|
|
int len = sizeof (has_vu);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = sysctl(sels, 2, &has_vu, &len, NULL, 0);
|
|
|
|
|
|
|
|
if (err == 0) {
|
|
|
|
if (has_vu != 0) {
|
|
|
|
util_cpu_caps.has_altivec = 1;
|
|
|
|
}
|
|
|
|
}
|
2010-07-24 13:59:40 +01:00
|
|
|
#else /* !PIPE_OS_APPLE */
|
|
|
|
/* not on Apple/Darwin, do it the brute-force way */
|
2009-09-29 13:25:08 +01:00
|
|
|
/* this is borrowed from the libmpeg2 library */
|
|
|
|
signal(SIGILL, sigill_handler);
|
2009-10-18 16:28:33 +01:00
|
|
|
if (setjmp(__lv_powerpc_jmpbuf)) {
|
2009-09-29 13:25:08 +01:00
|
|
|
signal(SIGILL, SIG_DFL);
|
|
|
|
} else {
|
|
|
|
__lv_powerpc_canjump = 1;
|
|
|
|
|
|
|
|
__asm __volatile
|
|
|
|
("mtspr 256, %0\n\t"
|
|
|
|
"vand %%v0, %%v0, %%v0"
|
|
|
|
:
|
|
|
|
: "r" (-1));
|
|
|
|
|
|
|
|
signal(SIGILL, SIG_DFL);
|
|
|
|
util_cpu_caps.has_altivec = 1;
|
|
|
|
}
|
2010-07-24 13:59:40 +01:00
|
|
|
#endif /* !PIPE_OS_APPLE */
|
2008-03-10 16:45:19 +00:00
|
|
|
}
|
2009-10-18 16:28:33 +01:00
|
|
|
#endif /* PIPE_ARCH_PPC */
|
2008-03-10 16:45:19 +00:00
|
|
|
|
|
|
|
|
2010-08-17 02:52:37 +01:00
|
|
|
#if defined(PIPE_ARCH_X86) || defined (PIPE_ARCH_X86_64)
|
2008-03-10 16:45:19 +00:00
|
|
|
static int has_cpuid(void)
|
|
|
|
{
|
2009-09-29 13:25:08 +01:00
|
|
|
#if defined(PIPE_ARCH_X86)
|
|
|
|
#if defined(PIPE_OS_GCC)
|
|
|
|
int a, c;
|
|
|
|
|
|
|
|
__asm __volatile
|
|
|
|
("pushf\n"
|
|
|
|
"popl %0\n"
|
|
|
|
"movl %0, %1\n"
|
|
|
|
"xorl $0x200000, %0\n"
|
|
|
|
"push %0\n"
|
|
|
|
"popf\n"
|
|
|
|
"pushf\n"
|
|
|
|
"popl %0\n"
|
|
|
|
: "=a" (a), "=c" (c)
|
|
|
|
:
|
|
|
|
: "cc");
|
|
|
|
|
|
|
|
return a != c;
|
|
|
|
#else
|
|
|
|
/* FIXME */
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
#elif defined(PIPE_ARCH_X86_64)
|
|
|
|
return 1;
|
2008-03-10 16:45:19 +00:00
|
|
|
#else
|
2009-09-29 13:25:08 +01:00
|
|
|
return 0;
|
2008-03-10 16:45:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-05 16:45:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @sa cpuid.h included in gcc-4.3 onwards.
|
2009-10-27 20:29:19 +00:00
|
|
|
* @sa http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
|
2009-10-05 16:45:38 +01:00
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline void
|
2009-10-05 16:45:38 +01:00
|
|
|
cpuid(uint32_t ax, uint32_t *p)
|
2008-03-10 16:45:19 +00:00
|
|
|
{
|
2015-12-01 23:07:08 +00:00
|
|
|
#if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
|
2009-10-04 12:49:31 +01:00
|
|
|
__asm __volatile (
|
2009-10-05 16:45:38 +01:00
|
|
|
"xchgl %%ebx, %1\n\t"
|
2009-10-04 12:49:31 +01:00
|
|
|
"cpuid\n\t"
|
2009-10-05 16:45:38 +01:00
|
|
|
"xchgl %%ebx, %1"
|
2009-10-04 12:49:31 +01:00
|
|
|
: "=a" (p[0]),
|
2009-10-09 13:22:42 +01:00
|
|
|
"=S" (p[1]),
|
2009-10-04 12:49:31 +01:00
|
|
|
"=c" (p[2]),
|
|
|
|
"=d" (p[3])
|
|
|
|
: "0" (ax)
|
|
|
|
);
|
2015-12-01 23:07:08 +00:00
|
|
|
#elif defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86_64)
|
2009-10-04 12:49:31 +01:00
|
|
|
__asm __volatile (
|
|
|
|
"cpuid\n\t"
|
|
|
|
: "=a" (p[0]),
|
2009-10-05 16:45:38 +01:00
|
|
|
"=b" (p[1]),
|
2009-10-04 12:49:31 +01:00
|
|
|
"=c" (p[2]),
|
|
|
|
"=d" (p[3])
|
|
|
|
: "0" (ax)
|
|
|
|
);
|
2009-09-29 13:25:08 +01:00
|
|
|
#elif defined(PIPE_CC_MSVC)
|
2009-10-27 20:29:19 +00:00
|
|
|
__cpuid(p, ax);
|
2009-10-27 20:45:53 +00:00
|
|
|
#else
|
|
|
|
p[0] = 0;
|
|
|
|
p[1] = 0;
|
|
|
|
p[2] = 0;
|
|
|
|
p[3] = 0;
|
2008-03-10 16:45:19 +00:00
|
|
|
#endif
|
2013-07-23 18:08:45 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 03:20:33 +01:00
|
|
|
/**
|
|
|
|
* @sa cpuid.h included in gcc-4.4 onwards.
|
|
|
|
* @sa http://msdn.microsoft.com/en-us/library/hskdteyh%28v=vs.90%29.aspx
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline void
|
2013-08-20 03:20:33 +01:00
|
|
|
cpuid_count(uint32_t ax, uint32_t cx, uint32_t *p)
|
|
|
|
{
|
2015-12-01 23:07:08 +00:00
|
|
|
#if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
|
2013-08-20 03:20:33 +01:00
|
|
|
__asm __volatile (
|
|
|
|
"xchgl %%ebx, %1\n\t"
|
|
|
|
"cpuid\n\t"
|
|
|
|
"xchgl %%ebx, %1"
|
|
|
|
: "=a" (p[0]),
|
|
|
|
"=S" (p[1]),
|
|
|
|
"=c" (p[2]),
|
|
|
|
"=d" (p[3])
|
|
|
|
: "0" (ax), "2" (cx)
|
|
|
|
);
|
2015-12-01 23:07:08 +00:00
|
|
|
#elif defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86_64)
|
2013-08-20 03:20:33 +01:00
|
|
|
__asm __volatile (
|
|
|
|
"cpuid\n\t"
|
|
|
|
: "=a" (p[0]),
|
|
|
|
"=b" (p[1]),
|
|
|
|
"=c" (p[2]),
|
|
|
|
"=d" (p[3])
|
|
|
|
: "0" (ax), "2" (cx)
|
|
|
|
);
|
|
|
|
#elif defined(PIPE_CC_MSVC)
|
|
|
|
__cpuidex(p, ax, cx);
|
|
|
|
#else
|
|
|
|
p[0] = 0;
|
|
|
|
p[1] = 0;
|
|
|
|
p[2] = 0;
|
|
|
|
p[3] = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline uint64_t xgetbv(void)
|
2013-07-23 18:08:45 +01:00
|
|
|
{
|
|
|
|
#if defined(PIPE_CC_GCC)
|
|
|
|
uint32_t eax, edx;
|
|
|
|
|
|
|
|
__asm __volatile (
|
|
|
|
".byte 0x0f, 0x01, 0xd0" // xgetbv isn't supported on gcc < 4.4
|
|
|
|
: "=a"(eax),
|
|
|
|
"=d"(edx)
|
|
|
|
: "c"(0)
|
|
|
|
);
|
|
|
|
|
|
|
|
return ((uint64_t)edx << 32) | eax;
|
|
|
|
#elif defined(PIPE_CC_MSVC) && defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
|
|
|
|
return _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2013-08-06 02:30:37 +01:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:08:45 +01:00
|
|
|
|
2013-08-06 02:30:37 +01:00
|
|
|
#if defined(PIPE_ARCH_X86)
|
2015-07-21 00:58:43 +01:00
|
|
|
PIPE_ALIGN_STACK static inline boolean sse2_has_daz(void)
|
2013-08-06 02:30:37 +01:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
uint32_t pad1[7];
|
|
|
|
uint32_t mxcsr_mask;
|
|
|
|
uint32_t pad2[128-8];
|
|
|
|
} PIPE_ALIGN_VAR(16) fxarea;
|
|
|
|
|
|
|
|
fxarea.mxcsr_mask = 0;
|
2015-12-01 23:07:08 +00:00
|
|
|
#if defined(PIPE_CC_GCC)
|
2013-08-09 16:29:52 +01:00
|
|
|
__asm __volatile ("fxsave %0" : "+m" (fxarea));
|
2016-02-11 11:36:17 +00:00
|
|
|
#elif defined(PIPE_CC_MSVC) || defined(PIPE_CC_ICL)
|
2013-08-08 18:08:57 +01:00
|
|
|
_fxsave(&fxarea);
|
2013-09-10 16:20:34 +01:00
|
|
|
#else
|
|
|
|
fxarea.mxcsr_mask = 0;
|
2013-08-06 02:30:37 +01:00
|
|
|
#endif
|
|
|
|
return !!(fxarea.mxcsr_mask & (1 << 6));
|
2008-03-10 16:45:19 +00:00
|
|
|
}
|
2013-08-06 02:30:37 +01:00
|
|
|
#endif
|
|
|
|
|
2009-10-18 16:28:33 +01:00
|
|
|
#endif /* X86 or X86_64 */
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2017-04-13 19:43:30 +01:00
|
|
|
#if defined(PIPE_ARCH_ARM)
|
|
|
|
static void
|
|
|
|
check_os_arm_support(void)
|
|
|
|
{
|
|
|
|
#if defined(PIPE_OS_ANDROID)
|
|
|
|
AndroidCpuFamily cpu_family = android_getCpuFamily();
|
|
|
|
uint64_t cpu_features = android_getCpuFeatures();
|
|
|
|
|
|
|
|
if (cpu_family == ANDROID_CPU_FAMILY_ARM) {
|
|
|
|
if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON)
|
|
|
|
util_cpu_caps.has_neon = 1;
|
|
|
|
}
|
|
|
|
#elif defined(PIPE_OS_LINUX)
|
|
|
|
Elf32_auxv_t aux;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open("/proc/self/auxv", O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd >= 0) {
|
|
|
|
while (read(fd, &aux, sizeof(Elf32_auxv_t)) == sizeof(Elf32_auxv_t)) {
|
|
|
|
if (aux.a_type == AT_HWCAP) {
|
|
|
|
uint32_t hwcap = aux.a_un.a_val;
|
|
|
|
|
|
|
|
util_cpu_caps.has_neon = (hwcap >> 12) & 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
#endif /* PIPE_OS_LINUX */
|
|
|
|
}
|
|
|
|
#endif /* PIPE_ARCH_ARM */
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
void
|
|
|
|
util_cpu_detect(void)
|
2008-03-10 16:45:19 +00:00
|
|
|
{
|
2009-09-29 13:25:08 +01:00
|
|
|
static boolean util_cpu_detect_initialized = FALSE;
|
|
|
|
|
|
|
|
if(util_cpu_detect_initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memset(&util_cpu_caps, 0, sizeof util_cpu_caps);
|
|
|
|
|
|
|
|
/* Count the number of CPUs in system */
|
2009-10-14 17:27:06 +01:00
|
|
|
#if defined(PIPE_OS_WINDOWS)
|
|
|
|
{
|
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
GetSystemInfo(&system_info);
|
|
|
|
util_cpu_caps.nr_cpus = system_info.dwNumberOfProcessors;
|
|
|
|
}
|
|
|
|
#elif defined(PIPE_OS_UNIX) && defined(_SC_NPROCESSORS_ONLN)
|
2009-09-29 13:25:08 +01:00
|
|
|
util_cpu_caps.nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
2016-04-29 20:46:06 +01:00
|
|
|
if (util_cpu_caps.nr_cpus == ~0u)
|
2009-09-29 13:25:08 +01:00
|
|
|
util_cpu_caps.nr_cpus = 1;
|
2009-10-14 17:27:06 +01:00
|
|
|
#elif defined(PIPE_OS_BSD)
|
2009-09-29 13:25:08 +01:00
|
|
|
{
|
|
|
|
int mib[2], ncpu;
|
|
|
|
int len;
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_NCPU;
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
len = sizeof (ncpu);
|
|
|
|
sysctl(mib, 2, &ncpu, &len, NULL, 0);
|
|
|
|
util_cpu_caps.nr_cpus = ncpu;
|
|
|
|
}
|
2008-03-10 16:45:19 +00:00
|
|
|
#else
|
2009-09-29 13:25:08 +01:00
|
|
|
util_cpu_caps.nr_cpus = 1;
|
2008-03-10 16:45:19 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-05 15:38:27 +01:00
|
|
|
/* Make the fallback cacheline size nonzero so that it can be
|
|
|
|
* safely passed to align().
|
|
|
|
*/
|
|
|
|
util_cpu_caps.cacheline = sizeof(void *);
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
#if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
|
|
|
|
if (has_cpuid()) {
|
2009-10-05 16:45:38 +01:00
|
|
|
uint32_t regs[4];
|
|
|
|
uint32_t regs2[4];
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
util_cpu_caps.cacheline = 32;
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
/* Get max cpuid level */
|
|
|
|
cpuid(0x00000000, regs);
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
if (regs[0] >= 0x00000001) {
|
|
|
|
unsigned int cacheline;
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
cpuid (0x00000001, regs2);
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
util_cpu_caps.x86_cpu_type = (regs2[0] >> 8) & 0xf;
|
|
|
|
if (util_cpu_caps.x86_cpu_type == 0xf)
|
|
|
|
util_cpu_caps.x86_cpu_type = 8 + ((regs2[0] >> 20) & 255); /* use extended family (P4, IA64) */
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
/* general feature flags */
|
2013-03-07 01:30:03 +00:00
|
|
|
util_cpu_caps.has_tsc = (regs2[3] >> 4) & 1; /* 0x0000010 */
|
2011-04-07 20:41:52 +01:00
|
|
|
util_cpu_caps.has_mmx = (regs2[3] >> 23) & 1; /* 0x0800000 */
|
|
|
|
util_cpu_caps.has_sse = (regs2[3] >> 25) & 1; /* 0x2000000 */
|
|
|
|
util_cpu_caps.has_sse2 = (regs2[3] >> 26) & 1; /* 0x4000000 */
|
|
|
|
util_cpu_caps.has_sse3 = (regs2[2] >> 0) & 1; /* 0x0000001 */
|
|
|
|
util_cpu_caps.has_ssse3 = (regs2[2] >> 9) & 1; /* 0x0000020 */
|
|
|
|
util_cpu_caps.has_sse4_1 = (regs2[2] >> 19) & 1;
|
2011-04-07 20:41:52 +01:00
|
|
|
util_cpu_caps.has_sse4_2 = (regs2[2] >> 20) & 1;
|
2013-06-19 22:46:15 +01:00
|
|
|
util_cpu_caps.has_popcnt = (regs2[2] >> 23) & 1;
|
2013-07-23 18:08:45 +01:00
|
|
|
util_cpu_caps.has_avx = ((regs2[2] >> 28) & 1) && // AVX
|
|
|
|
((regs2[2] >> 27) & 1) && // OSXSAVE
|
|
|
|
((xgetbv() & 6) == 6); // XMM & YMM
|
2014-09-19 15:56:04 +01:00
|
|
|
util_cpu_caps.has_f16c = ((regs2[2] >> 29) & 1) && util_cpu_caps.has_avx;
|
2016-05-24 14:36:28 +01:00
|
|
|
util_cpu_caps.has_fma = ((regs2[2] >> 12) & 1) && util_cpu_caps.has_avx;
|
2009-09-29 13:25:08 +01:00
|
|
|
util_cpu_caps.has_mmx2 = util_cpu_caps.has_sse; /* SSE cpus supports mmxext too */
|
2013-08-06 02:30:37 +01:00
|
|
|
#if defined(PIPE_ARCH_X86_64)
|
|
|
|
util_cpu_caps.has_daz = 1;
|
|
|
|
#else
|
|
|
|
util_cpu_caps.has_daz = util_cpu_caps.has_sse3 ||
|
|
|
|
(util_cpu_caps.has_sse2 && sse2_has_daz());
|
|
|
|
#endif
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
cacheline = ((regs2[1] >> 8) & 0xFF) * 8;
|
|
|
|
if (cacheline > 0)
|
|
|
|
util_cpu_caps.cacheline = cacheline;
|
|
|
|
}
|
2013-08-20 03:20:33 +01:00
|
|
|
if (util_cpu_caps.has_avx && regs[0] >= 0x00000007) {
|
|
|
|
uint32_t regs7[4];
|
|
|
|
cpuid_count(0x00000007, 0x00000000, regs7);
|
|
|
|
util_cpu_caps.has_avx2 = (regs7[1] >> 5) & 1;
|
|
|
|
}
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2016-05-26 21:06:07 +01:00
|
|
|
// check for avx512
|
|
|
|
if (((regs2[2] >> 27) & 1) && // OSXSAVE
|
|
|
|
(xgetbv() & (0x7 << 5)) && // OPMASK: upper-256 enabled by OS
|
|
|
|
((xgetbv() & 6) == 6)) { // XMM/YMM enabled by OS
|
|
|
|
uint32_t regs3[4];
|
|
|
|
cpuid(0x00000007, regs3);
|
|
|
|
util_cpu_caps.has_avx512f = (regs3[1] >> 16) & 1;
|
|
|
|
util_cpu_caps.has_avx512dq = (regs3[1] >> 17) & 1;
|
|
|
|
util_cpu_caps.has_avx512ifma = (regs3[1] >> 21) & 1;
|
|
|
|
util_cpu_caps.has_avx512pf = (regs3[1] >> 26) & 1;
|
|
|
|
util_cpu_caps.has_avx512er = (regs3[1] >> 27) & 1;
|
|
|
|
util_cpu_caps.has_avx512cd = (regs3[1] >> 28) & 1;
|
|
|
|
util_cpu_caps.has_avx512bw = (regs3[1] >> 30) & 1;
|
|
|
|
util_cpu_caps.has_avx512vl = (regs3[1] >> 31) & 1;
|
|
|
|
util_cpu_caps.has_avx512vbmi = (regs3[2] >> 1) & 1;
|
|
|
|
}
|
|
|
|
|
2012-08-31 17:01:50 +01:00
|
|
|
if (regs[1] == 0x756e6547 && regs[2] == 0x6c65746e && regs[3] == 0x49656e69) {
|
|
|
|
/* GenuineIntel */
|
|
|
|
util_cpu_caps.has_intel = 1;
|
|
|
|
}
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
cpuid(0x80000000, regs);
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
if (regs[0] >= 0x80000001) {
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
cpuid(0x80000001, regs2);
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2011-04-07 20:41:52 +01:00
|
|
|
util_cpu_caps.has_mmx |= (regs2[3] >> 23) & 1;
|
|
|
|
util_cpu_caps.has_mmx2 |= (regs2[3] >> 22) & 1;
|
|
|
|
util_cpu_caps.has_3dnow = (regs2[3] >> 31) & 1;
|
|
|
|
util_cpu_caps.has_3dnow_ext = (regs2[3] >> 30) & 1;
|
2013-08-20 03:20:33 +01:00
|
|
|
|
|
|
|
util_cpu_caps.has_xop = util_cpu_caps.has_avx &&
|
|
|
|
((regs2[2] >> 11) & 1);
|
2009-09-29 13:25:08 +01:00
|
|
|
}
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
if (regs[0] >= 0x80000006) {
|
2014-12-31 18:07:44 +00:00
|
|
|
/* should we really do this if the clflush size above worked? */
|
|
|
|
unsigned int cacheline;
|
2009-09-29 13:25:08 +01:00
|
|
|
cpuid(0x80000006, regs2);
|
2014-12-31 18:07:44 +00:00
|
|
|
cacheline = regs2[2] & 0xFF;
|
|
|
|
if (cacheline > 0)
|
|
|
|
util_cpu_caps.cacheline = cacheline;
|
2009-09-29 13:25:08 +01:00
|
|
|
}
|
2008-03-10 16:45:19 +00:00
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
if (!util_cpu_caps.has_sse) {
|
|
|
|
util_cpu_caps.has_sse2 = 0;
|
|
|
|
util_cpu_caps.has_sse3 = 0;
|
|
|
|
util_cpu_caps.has_ssse3 = 0;
|
2009-10-14 17:27:06 +01:00
|
|
|
util_cpu_caps.has_sse4_1 = 0;
|
2009-09-29 13:25:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* PIPE_ARCH_X86 || PIPE_ARCH_X86_64 */
|
|
|
|
|
2017-04-13 19:43:30 +01:00
|
|
|
#if defined(PIPE_ARCH_ARM)
|
|
|
|
check_os_arm_support();
|
|
|
|
#endif
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
#if defined(PIPE_ARCH_PPC)
|
|
|
|
check_os_altivec_support();
|
|
|
|
#endif /* PIPE_ARCH_PPC */
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2010-08-06 01:21:49 +01:00
|
|
|
if (debug_get_option_dump_cpu()) {
|
|
|
|
debug_printf("util_cpu_caps.nr_cpus = %u\n", util_cpu_caps.nr_cpus);
|
|
|
|
|
|
|
|
debug_printf("util_cpu_caps.x86_cpu_type = %u\n", util_cpu_caps.x86_cpu_type);
|
|
|
|
debug_printf("util_cpu_caps.cacheline = %u\n", util_cpu_caps.cacheline);
|
|
|
|
|
|
|
|
debug_printf("util_cpu_caps.has_tsc = %u\n", util_cpu_caps.has_tsc);
|
|
|
|
debug_printf("util_cpu_caps.has_mmx = %u\n", util_cpu_caps.has_mmx);
|
|
|
|
debug_printf("util_cpu_caps.has_mmx2 = %u\n", util_cpu_caps.has_mmx2);
|
|
|
|
debug_printf("util_cpu_caps.has_sse = %u\n", util_cpu_caps.has_sse);
|
|
|
|
debug_printf("util_cpu_caps.has_sse2 = %u\n", util_cpu_caps.has_sse2);
|
|
|
|
debug_printf("util_cpu_caps.has_sse3 = %u\n", util_cpu_caps.has_sse3);
|
|
|
|
debug_printf("util_cpu_caps.has_ssse3 = %u\n", util_cpu_caps.has_ssse3);
|
|
|
|
debug_printf("util_cpu_caps.has_sse4_1 = %u\n", util_cpu_caps.has_sse4_1);
|
2011-04-07 20:41:52 +01:00
|
|
|
debug_printf("util_cpu_caps.has_sse4_2 = %u\n", util_cpu_caps.has_sse4_2);
|
|
|
|
debug_printf("util_cpu_caps.has_avx = %u\n", util_cpu_caps.has_avx);
|
2013-08-20 03:20:33 +01:00
|
|
|
debug_printf("util_cpu_caps.has_avx2 = %u\n", util_cpu_caps.has_avx2);
|
2013-08-06 02:30:37 +01:00
|
|
|
debug_printf("util_cpu_caps.has_f16c = %u\n", util_cpu_caps.has_f16c);
|
|
|
|
debug_printf("util_cpu_caps.has_popcnt = %u\n", util_cpu_caps.has_popcnt);
|
2010-08-06 01:21:49 +01:00
|
|
|
debug_printf("util_cpu_caps.has_3dnow = %u\n", util_cpu_caps.has_3dnow);
|
|
|
|
debug_printf("util_cpu_caps.has_3dnow_ext = %u\n", util_cpu_caps.has_3dnow_ext);
|
2013-08-20 03:20:33 +01:00
|
|
|
debug_printf("util_cpu_caps.has_xop = %u\n", util_cpu_caps.has_xop);
|
2010-08-06 01:21:49 +01:00
|
|
|
debug_printf("util_cpu_caps.has_altivec = %u\n", util_cpu_caps.has_altivec);
|
2017-04-13 19:43:30 +01:00
|
|
|
debug_printf("util_cpu_caps.has_neon = %u\n", util_cpu_caps.has_neon);
|
2013-08-06 02:30:37 +01:00
|
|
|
debug_printf("util_cpu_caps.has_daz = %u\n", util_cpu_caps.has_daz);
|
2016-05-26 21:06:07 +01:00
|
|
|
debug_printf("util_cpu_caps.has_avx512f = %u\n", util_cpu_caps.has_avx512f);
|
|
|
|
debug_printf("util_cpu_caps.has_avx512dq = %u\n", util_cpu_caps.has_avx512dq);
|
|
|
|
debug_printf("util_cpu_caps.has_avx512ifma = %u\n", util_cpu_caps.has_avx512ifma);
|
|
|
|
debug_printf("util_cpu_caps.has_avx512pf = %u\n", util_cpu_caps.has_avx512pf);
|
|
|
|
debug_printf("util_cpu_caps.has_avx512er = %u\n", util_cpu_caps.has_avx512er);
|
|
|
|
debug_printf("util_cpu_caps.has_avx512cd = %u\n", util_cpu_caps.has_avx512cd);
|
|
|
|
debug_printf("util_cpu_caps.has_avx512bw = %u\n", util_cpu_caps.has_avx512bw);
|
|
|
|
debug_printf("util_cpu_caps.has_avx512vl = %u\n", util_cpu_caps.has_avx512vl);
|
|
|
|
debug_printf("util_cpu_caps.has_avx512vbmi = %u\n", util_cpu_caps.has_avx512vbmi);
|
2010-08-06 01:21:49 +01:00
|
|
|
}
|
2008-03-10 16:45:19 +00:00
|
|
|
#endif
|
|
|
|
|
2009-09-29 13:25:08 +01:00
|
|
|
util_cpu_detect_initialized = TRUE;
|
2008-03-10 16:45:19 +00:00
|
|
|
}
|