2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Check CPU capabilities & initialize optimized funtions for this particular
31 * Changed by Andre Werthmann for using the new SSE functions.
33 * \author Holger Waechtler <holger@akaflieg.extern.tu-berlin.de>
34 * \author Andre Werthmann <wertmann@cs.uni-potsdam.de>
37 /* XXX these includes should probably go into imports.h or glheader.h */
38 #if defined(USE_SSE_ASM) && defined(__linux__)
39 #include <linux/version.h>
41 #if defined(USE_SSE_ASM) && defined(__FreeBSD__)
42 #include <sys/types.h>
43 #include <sys/sysctl.h>
45 #if defined(USE_SSE_ASM) && defined(__OpenBSD__)
46 #include <sys/param.h>
47 #include <sys/sysctl.h>
48 #include <machine/cpu.h>
51 #include "main/imports.h"
52 #include "common_x86_asm.h"
55 /** Bitmask of X86_FEATURE_x bits */
56 int _mesa_x86_cpu_features
= 0x0;
60 /* No reason for this to be public.
62 extern GLuint _ASMAPI
_mesa_x86_has_cpuid(void);
63 extern void _ASMAPI
_mesa_x86_cpuid(GLuint op
, GLuint
*reg_eax
, GLuint
*reg_ebx
, GLuint
*reg_ecx
, GLuint
*reg_edx
);
64 extern GLuint _ASMAPI
_mesa_x86_cpuid_eax(GLuint op
);
65 extern GLuint _ASMAPI
_mesa_x86_cpuid_ebx(GLuint op
);
66 extern GLuint _ASMAPI
_mesa_x86_cpuid_ecx(GLuint op
);
67 extern GLuint _ASMAPI
_mesa_x86_cpuid_edx(GLuint op
);
70 #if defined(USE_SSE_ASM)
72 * We must verify that the Streaming SIMD Extensions are truly supported
73 * on this processor before we go ahead and hook out the optimized code.
75 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
76 * kernels provide full SSE support on all processors that expose SSE via
77 * the CPUID mechanism.
80 /* These are assembly functions: */
81 extern void _mesa_test_os_sse_support( void );
82 extern void _mesa_test_os_sse_exception_support( void );
86 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
87 # define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
89 static LONG WINAPI
ExceptionFilter(LPEXCEPTION_POINTERS exp
)
91 PEXCEPTION_RECORD rec
= exp
->ExceptionRecord
;
92 PCONTEXT ctx
= exp
->ContextRecord
;
94 if ( rec
->ExceptionCode
== EXCEPTION_ILLEGAL_INSTRUCTION
) {
95 _mesa_debug(NULL
, "EXCEPTION_ILLEGAL_INSTRUCTION\n" );
96 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
97 } else if ( rec
->ExceptionCode
== STATUS_FLOAT_MULTIPLE_TRAPS
) {
98 _mesa_debug(NULL
, "STATUS_FLOAT_MULTIPLE_TRAPS\n");
99 /* Windows seems to clear the exception flag itself, we just have to increment Eip */
101 _mesa_debug(NULL
, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
102 return EXCEPTION_EXECUTE_HANDLER
;
105 if ( (ctx
->ContextFlags
& CONTEXT_CONTROL
) != CONTEXT_CONTROL
) {
106 _mesa_debug(NULL
, "Context does not contain control registers, terminating!\n");
107 return EXCEPTION_EXECUTE_HANDLER
;
111 return EXCEPTION_CONTINUE_EXECUTION
;
117 * Check if SSE is supported.
118 * If not, turn off the X86_FEATURE_XMM flag in _mesa_x86_cpu_features.
120 void _mesa_check_os_sse_support( void )
122 #if defined(__FreeBSD__)
126 len
= sizeof(enabled
);
127 ret
= sysctlbyname("hw.instruction_sse", &enabled
, &len
, NULL
, 0);
129 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
131 #elif defined (__NetBSD__)
134 size_t len
= sizeof(enabled
);
135 ret
= sysctlbyname("machdep.sse", &enabled
, &len
, (void *)NULL
, 0);
137 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
139 #elif defined(__OpenBSD__)
143 size_t len
= sizeof(enabled
);
145 mib
[0] = CTL_MACHDEP
;
148 ret
= sysctl(mib
, 2, &enabled
, &len
, NULL
, 0);
150 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
153 LPTOP_LEVEL_EXCEPTION_FILTER oldFilter
;
155 /* Install our ExceptionFilter */
156 oldFilter
= SetUnhandledExceptionFilter( ExceptionFilter
);
159 _mesa_debug(NULL
, "Testing OS support for SSE...\n");
161 _mesa_test_os_sse_support();
164 _mesa_debug(NULL
, "Yes.\n");
166 _mesa_debug(NULL
, "No!\n");
171 _mesa_debug(NULL
, "Testing OS support for SSE unmasked exceptions...\n");
173 _mesa_test_os_sse_exception_support();
176 _mesa_debug(NULL
, "Yes.\n");
178 _mesa_debug(NULL
, "No!\n");
182 /* Restore previous exception filter */
183 SetUnhandledExceptionFilter( oldFilter
);
186 _mesa_debug(NULL
, "Tests of OS support for SSE passed.\n");
188 _mesa_debug(NULL
, "Tests of OS support for SSE failed!\n");
191 /* Do nothing on other platforms for now.
193 _mesa_debug(NULL
, "Not testing OS support for SSE, leaving enabled.\n");
194 #endif /* __FreeBSD__ */
197 #endif /* USE_SSE_ASM */
201 * Initialize the _mesa_x86_cpu_features bitfield.
202 * This is a no-op if called more than once.
205 _mesa_get_x86_features(void)
207 static int called
= 0;
215 _mesa_x86_cpu_features
= 0x0;
217 if (_mesa_getenv( "MESA_NO_ASM")) {
221 if (!_mesa_x86_has_cpuid()) {
222 _mesa_debug(NULL
, "CPUID not detected\n");
226 GLuint cpu_ext_features
;
231 /* get vendor name */
232 _mesa_x86_cpuid(0, &result
, (GLuint
*)(cpu_vendor
+ 0), (GLuint
*)(cpu_vendor
+ 8), (GLuint
*)(cpu_vendor
+ 4));
233 cpu_vendor
[12] = '\0';
235 _mesa_debug(NULL
, "CPU vendor: %s\n", cpu_vendor
);
237 /* get cpu features */
238 cpu_features
= _mesa_x86_cpuid_edx(1);
240 if (cpu_features
& X86_CPU_FPU
)
241 _mesa_x86_cpu_features
|= X86_FEATURE_FPU
;
242 if (cpu_features
& X86_CPU_CMOV
)
243 _mesa_x86_cpu_features
|= X86_FEATURE_CMOV
;
246 if (cpu_features
& X86_CPU_MMX
)
247 _mesa_x86_cpu_features
|= X86_FEATURE_MMX
;
251 if (cpu_features
& X86_CPU_XMM
)
252 _mesa_x86_cpu_features
|= X86_FEATURE_XMM
;
253 if (cpu_features
& X86_CPU_XMM2
)
254 _mesa_x86_cpu_features
|= X86_FEATURE_XMM2
;
257 /* query extended cpu features */
258 if ((cpu_ext_info
= _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
259 if (cpu_ext_info
>= 0x80000001) {
261 cpu_ext_features
= _mesa_x86_cpuid_edx(0x80000001);
263 if (cpu_features
& X86_CPU_MMX
) {
266 if (cpu_ext_features
& X86_CPUEXT_3DNOW
)
267 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOW
;
268 if (cpu_ext_features
& X86_CPUEXT_3DNOW_EXT
)
269 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOWEXT
;
273 if (cpu_ext_features
& X86_CPUEXT_MMX_EXT
)
274 _mesa_x86_cpu_features
|= X86_FEATURE_MMXEXT
;
280 if (cpu_ext_info
>= 0x80000002) {
283 for (ofs
= 0; ofs
< 3; ofs
++)
284 _mesa_x86_cpuid(0x80000002+ofs
, (GLuint
*)(cpu_name
+ (16*ofs
)+0), (GLuint
*)(cpu_name
+ (16*ofs
)+4), (GLuint
*)(cpu_name
+ (16*ofs
)+8), (GLuint
*)(cpu_name
+ (16*ofs
)+12));
285 cpu_name
[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
287 _mesa_debug(NULL
, "CPU name: %s\n", cpu_name
);
292 #endif /* USE_X86_ASM */