Merge branch 'glsl2'
[mesa.git] / src / gallium / auxiliary / util / u_cpu_detect.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Dennis Smit
4 * All Rights Reserved.
5 *
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:
12 *
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
15 * Software.
16 *
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.
24 *
25 **************************************************************************/
26
27 /**
28 * @file
29 * CPU feature detection.
30 *
31 * @author Dennis Smit
32 * @author Based on the work of Eric Anholt <anholt@FreeBSD.org>
33 */
34
35 #include "pipe/p_config.h"
36
37 #include "u_debug.h"
38 #include "u_cpu_detect.h"
39
40 #if defined(PIPE_ARCH_PPC)
41 #if defined(PIPE_OS_APPLE)
42 #include <sys/sysctl.h>
43 #else
44 #include <signal.h>
45 #include <setjmp.h>
46 #endif
47 #endif
48
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>
53 #endif
54
55 #if defined(PIPE_OS_FREEBSD)
56 #include <sys/types.h>
57 #include <sys/sysctl.h>
58 #endif
59
60 #if defined(PIPE_OS_LINUX)
61 #include <signal.h>
62 #endif
63
64 #ifdef PIPE_OS_UNIX
65 #include <unistd.h>
66 #endif
67
68 #if defined(PIPE_OS_WINDOWS)
69 #include <windows.h>
70 #if defined(MSVC)
71 #include <intrin.h>
72 #endif
73 #endif
74
75
76 DEBUG_GET_ONCE_BOOL_OPTION(dump_cpu, "GALLIUM_DUMP_CPU", FALSE)
77
78
79 struct util_cpu_caps util_cpu_caps;
80
81 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
82 static int has_cpuid(void);
83 #endif
84
85
86 #if defined(PIPE_ARCH_X86)
87
88 /* The sigill handlers */
89 #if defined(PIPE_OS_LINUX) /*&& defined(_POSIX_SOURCE) && defined(X86_FXSR_MAGIC)*/
90 static void
91 sigill_handler_sse(int signal, struct sigcontext sc)
92 {
93 /* Both the "xorps %%xmm0,%%xmm0" and "divps %xmm0,%%xmm1"
94 * instructions are 3 bytes long. We must increment the instruction
95 * pointer manually to avoid repeated execution of the offending
96 * instruction.
97 *
98 * If the SIGILL is caused by a divide-by-zero when unmasked
99 * exceptions aren't supported, the SIMD FPU status and control
100 * word will be restored at the end of the test, so we don't need
101 * to worry about doing it here. Besides, we may not be able to...
102 */
103 sc.eip += 3;
104
105 util_cpu_caps.has_sse=0;
106 }
107
108 static void
109 sigfpe_handler_sse(int signal, struct sigcontext sc)
110 {
111 if (sc.fpstate->magic != 0xffff) {
112 /* Our signal context has the extended FPU state, so reset the
113 * divide-by-zero exception mask and clear the divide-by-zero
114 * exception bit.
115 */
116 sc.fpstate->mxcsr |= 0x00000200;
117 sc.fpstate->mxcsr &= 0xfffffffb;
118 } else {
119 /* If we ever get here, we're completely hosed.
120 */
121 }
122 }
123 #endif /* PIPE_OS_LINUX && _POSIX_SOURCE && X86_FXSR_MAGIC */
124
125 #if defined(PIPE_OS_WINDOWS)
126 static LONG CALLBACK
127 win32_sig_handler_sse(EXCEPTION_POINTERS* ep)
128 {
129 if(ep->ExceptionRecord->ExceptionCode==EXCEPTION_ILLEGAL_INSTRUCTION){
130 ep->ContextRecord->Eip +=3;
131 util_cpu_caps.has_sse=0;
132 return EXCEPTION_CONTINUE_EXECUTION;
133 }
134 return EXCEPTION_CONTINUE_SEARCH;
135 }
136 #endif /* PIPE_OS_WINDOWS */
137
138 #endif /* PIPE_ARCH_X86 */
139
140
141 #if defined(PIPE_ARCH_PPC) && !defined(PIPE_OS_APPLE)
142 static jmp_buf __lv_powerpc_jmpbuf;
143 static volatile sig_atomic_t __lv_powerpc_canjump = 0;
144
145 static void
146 sigill_handler(int sig)
147 {
148 if (!__lv_powerpc_canjump) {
149 signal (sig, SIG_DFL);
150 raise (sig);
151 }
152
153 __lv_powerpc_canjump = 0;
154 longjmp(__lv_powerpc_jmpbuf, 1);
155 }
156 #endif
157
158 #if defined(PIPE_ARCH_PPC)
159 static void
160 check_os_altivec_support(void)
161 {
162 #if defined(PIPE_OS_APPLE)
163 int sels[2] = {CTL_HW, HW_VECTORUNIT};
164 int has_vu = 0;
165 int len = sizeof (has_vu);
166 int err;
167
168 err = sysctl(sels, 2, &has_vu, &len, NULL, 0);
169
170 if (err == 0) {
171 if (has_vu != 0) {
172 util_cpu_caps.has_altivec = 1;
173 }
174 }
175 #else /* !PIPE_OS_APPLE */
176 /* not on Apple/Darwin, do it the brute-force way */
177 /* this is borrowed from the libmpeg2 library */
178 signal(SIGILL, sigill_handler);
179 if (setjmp(__lv_powerpc_jmpbuf)) {
180 signal(SIGILL, SIG_DFL);
181 } else {
182 __lv_powerpc_canjump = 1;
183
184 __asm __volatile
185 ("mtspr 256, %0\n\t"
186 "vand %%v0, %%v0, %%v0"
187 :
188 : "r" (-1));
189
190 signal(SIGILL, SIG_DFL);
191 util_cpu_caps.has_altivec = 1;
192 }
193 #endif /* !PIPE_OS_APPLE */
194 }
195 #endif /* PIPE_ARCH_PPC */
196
197
198 #if defined(PIPE_ARCH_X86) || defined (PIPE_ARCH_X86_64)
199 static int has_cpuid(void)
200 {
201 #if defined(PIPE_ARCH_X86)
202 #if defined(PIPE_OS_GCC)
203 int a, c;
204
205 __asm __volatile
206 ("pushf\n"
207 "popl %0\n"
208 "movl %0, %1\n"
209 "xorl $0x200000, %0\n"
210 "push %0\n"
211 "popf\n"
212 "pushf\n"
213 "popl %0\n"
214 : "=a" (a), "=c" (c)
215 :
216 : "cc");
217
218 return a != c;
219 #else
220 /* FIXME */
221 return 1;
222 #endif
223 #elif defined(PIPE_ARCH_X86_64)
224 return 1;
225 #else
226 return 0;
227 #endif
228 }
229
230
231 /**
232 * @sa cpuid.h included in gcc-4.3 onwards.
233 * @sa http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
234 */
235 static INLINE void
236 cpuid(uint32_t ax, uint32_t *p)
237 {
238 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
239 __asm __volatile (
240 "xchgl %%ebx, %1\n\t"
241 "cpuid\n\t"
242 "xchgl %%ebx, %1"
243 : "=a" (p[0]),
244 "=S" (p[1]),
245 "=c" (p[2]),
246 "=d" (p[3])
247 : "0" (ax)
248 );
249 #elif defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86_64)
250 __asm __volatile (
251 "cpuid\n\t"
252 : "=a" (p[0]),
253 "=b" (p[1]),
254 "=c" (p[2]),
255 "=d" (p[3])
256 : "0" (ax)
257 );
258 #elif defined(PIPE_CC_MSVC)
259 __cpuid(p, ax);
260 #else
261 p[0] = 0;
262 p[1] = 0;
263 p[2] = 0;
264 p[3] = 0;
265 #endif
266 }
267 #endif /* X86 or X86_64 */
268
269 void
270 util_cpu_detect(void)
271 {
272 static boolean util_cpu_detect_initialized = FALSE;
273
274 if(util_cpu_detect_initialized)
275 return;
276
277 memset(&util_cpu_caps, 0, sizeof util_cpu_caps);
278
279 /* Count the number of CPUs in system */
280 #if defined(PIPE_OS_WINDOWS)
281 {
282 SYSTEM_INFO system_info;
283 GetSystemInfo(&system_info);
284 util_cpu_caps.nr_cpus = system_info.dwNumberOfProcessors;
285 }
286 #elif defined(PIPE_OS_UNIX) && defined(_SC_NPROCESSORS_ONLN)
287 util_cpu_caps.nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
288 if (util_cpu_caps.nr_cpus == -1)
289 util_cpu_caps.nr_cpus = 1;
290 #elif defined(PIPE_OS_BSD)
291 {
292 int mib[2], ncpu;
293 int len;
294
295 mib[0] = CTL_HW;
296 mib[1] = HW_NCPU;
297
298 len = sizeof (ncpu);
299 sysctl(mib, 2, &ncpu, &len, NULL, 0);
300 util_cpu_caps.nr_cpus = ncpu;
301 }
302 #else
303 util_cpu_caps.nr_cpus = 1;
304 #endif
305
306 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
307 if (has_cpuid()) {
308 uint32_t regs[4];
309 uint32_t regs2[4];
310
311 util_cpu_caps.cacheline = 32;
312
313 /* Get max cpuid level */
314 cpuid(0x00000000, regs);
315
316 if (regs[0] >= 0x00000001) {
317 unsigned int cacheline;
318
319 cpuid (0x00000001, regs2);
320
321 util_cpu_caps.x86_cpu_type = (regs2[0] >> 8) & 0xf;
322 if (util_cpu_caps.x86_cpu_type == 0xf)
323 util_cpu_caps.x86_cpu_type = 8 + ((regs2[0] >> 20) & 255); /* use extended family (P4, IA64) */
324
325 /* general feature flags */
326 util_cpu_caps.has_tsc = (regs2[3] & (1 << 8 )) >> 8; /* 0x0000010 */
327 util_cpu_caps.has_mmx = (regs2[3] & (1 << 23 )) >> 23; /* 0x0800000 */
328 util_cpu_caps.has_sse = (regs2[3] & (1 << 25 )) >> 25; /* 0x2000000 */
329 util_cpu_caps.has_sse2 = (regs2[3] & (1 << 26 )) >> 26; /* 0x4000000 */
330 util_cpu_caps.has_sse3 = (regs2[2] & (1)); /* 0x0000001 */
331 util_cpu_caps.has_ssse3 = (regs2[2] & (1 << 9 )) >> 9; /* 0x0000020 */
332 util_cpu_caps.has_sse4_1 = (regs2[2] & (1 << 19)) >> 19;
333 util_cpu_caps.has_mmx2 = util_cpu_caps.has_sse; /* SSE cpus supports mmxext too */
334
335 cacheline = ((regs2[1] >> 8) & 0xFF) * 8;
336 if (cacheline > 0)
337 util_cpu_caps.cacheline = cacheline;
338 }
339
340 cpuid(0x80000000, regs);
341
342 if (regs[0] >= 0x80000001) {
343
344 cpuid(0x80000001, regs2);
345
346 util_cpu_caps.has_mmx |= (regs2[3] & (1 << 23 )) >> 23; /* 0x0800000 */
347 util_cpu_caps.has_mmx2 |= (regs2[3] & (1 << 22 )) >> 22; /* 0x400000 */
348 util_cpu_caps.has_3dnow = (regs2[3] & (1 << 31 )) >> 31; /* 0x80000000 */
349 util_cpu_caps.has_3dnow_ext = (regs2[3] & (1 << 30 )) >> 30;
350 }
351
352 if (regs[0] >= 0x80000006) {
353 cpuid(0x80000006, regs2);
354 util_cpu_caps.cacheline = regs2[2] & 0xFF;
355 }
356
357 if (!util_cpu_caps.has_sse) {
358 util_cpu_caps.has_sse2 = 0;
359 util_cpu_caps.has_sse3 = 0;
360 util_cpu_caps.has_ssse3 = 0;
361 util_cpu_caps.has_sse4_1 = 0;
362 }
363 }
364 #endif /* PIPE_ARCH_X86 || PIPE_ARCH_X86_64 */
365
366 #if defined(PIPE_ARCH_PPC)
367 check_os_altivec_support();
368 #endif /* PIPE_ARCH_PPC */
369
370 #ifdef DEBUG
371 if (debug_get_option_dump_cpu()) {
372 debug_printf("util_cpu_caps.nr_cpus = %u\n", util_cpu_caps.nr_cpus);
373
374 debug_printf("util_cpu_caps.x86_cpu_type = %u\n", util_cpu_caps.x86_cpu_type);
375 debug_printf("util_cpu_caps.cacheline = %u\n", util_cpu_caps.cacheline);
376
377 debug_printf("util_cpu_caps.has_tsc = %u\n", util_cpu_caps.has_tsc);
378 debug_printf("util_cpu_caps.has_mmx = %u\n", util_cpu_caps.has_mmx);
379 debug_printf("util_cpu_caps.has_mmx2 = %u\n", util_cpu_caps.has_mmx2);
380 debug_printf("util_cpu_caps.has_sse = %u\n", util_cpu_caps.has_sse);
381 debug_printf("util_cpu_caps.has_sse2 = %u\n", util_cpu_caps.has_sse2);
382 debug_printf("util_cpu_caps.has_sse3 = %u\n", util_cpu_caps.has_sse3);
383 debug_printf("util_cpu_caps.has_ssse3 = %u\n", util_cpu_caps.has_ssse3);
384 debug_printf("util_cpu_caps.has_sse4_1 = %u\n", util_cpu_caps.has_sse4_1);
385 debug_printf("util_cpu_caps.has_3dnow = %u\n", util_cpu_caps.has_3dnow);
386 debug_printf("util_cpu_caps.has_3dnow_ext = %u\n", util_cpu_caps.has_3dnow_ext);
387 debug_printf("util_cpu_caps.has_altivec = %u\n", util_cpu_caps.has_altivec);
388 }
389 #endif
390
391 util_cpu_detect_initialized = TRUE;
392 }