mesa: Rely on USE_X86_64_ASM.
[mesa.git] / src / mesa / x86 / common_x86.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 Brian Paul 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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file common_x86.c
27 *
28 * Check CPU capabilities & initialize optimized funtions for this particular
29 * processor.
30 *
31 * Changed by Andre Werthmann for using the new SSE functions.
32 *
33 * \author Holger Waechtler <holger@akaflieg.extern.tu-berlin.de>
34 * \author Andre Werthmann <wertmann@cs.uni-potsdam.de>
35 */
36
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>
40 #endif
41 #if defined(USE_SSE_ASM) && defined(__FreeBSD__)
42 #include <sys/types.h>
43 #include <sys/sysctl.h>
44 #endif
45 #if defined(USE_SSE_ASM) && defined(__OpenBSD__)
46 #include <sys/param.h>
47 #include <sys/sysctl.h>
48 #include <machine/cpu.h>
49 #endif
50 #if defined(USE_X86_64_ASM)
51 #include <cpuid.h>
52 #endif
53
54 #include "main/imports.h"
55 #include "common_x86_asm.h"
56
57
58 /** Bitmask of X86_FEATURE_x bits */
59 int _mesa_x86_cpu_features = 0x0;
60
61 static int detection_debug = GL_FALSE;
62
63 /* No reason for this to be public.
64 */
65 extern GLuint _ASMAPI _mesa_x86_has_cpuid(void);
66 extern void _ASMAPI _mesa_x86_cpuid(GLuint op, GLuint *reg_eax, GLuint *reg_ebx, GLuint *reg_ecx, GLuint *reg_edx);
67 extern GLuint _ASMAPI _mesa_x86_cpuid_eax(GLuint op);
68 extern GLuint _ASMAPI _mesa_x86_cpuid_ebx(GLuint op);
69 extern GLuint _ASMAPI _mesa_x86_cpuid_ecx(GLuint op);
70 extern GLuint _ASMAPI _mesa_x86_cpuid_edx(GLuint op);
71
72
73 #if defined(USE_SSE_ASM)
74 /*
75 * We must verify that the Streaming SIMD Extensions are truly supported
76 * on this processor before we go ahead and hook out the optimized code.
77 *
78 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
79 * kernels provide full SSE support on all processors that expose SSE via
80 * the CPUID mechanism.
81 */
82
83 /* These are assembly functions: */
84 extern void _mesa_test_os_sse_support( void );
85 extern void _mesa_test_os_sse_exception_support( void );
86
87
88 #if defined(_WIN32)
89 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
90 # define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
91 #endif
92 static LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS exp)
93 {
94 PEXCEPTION_RECORD rec = exp->ExceptionRecord;
95 PCONTEXT ctx = exp->ContextRecord;
96
97 if ( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION ) {
98 _mesa_debug(NULL, "EXCEPTION_ILLEGAL_INSTRUCTION\n" );
99 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
100 } else if ( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS ) {
101 _mesa_debug(NULL, "STATUS_FLOAT_MULTIPLE_TRAPS\n");
102 /* Windows seems to clear the exception flag itself, we just have to increment Eip */
103 } else {
104 _mesa_debug(NULL, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
105 return EXCEPTION_EXECUTE_HANDLER;
106 }
107
108 if ( (ctx->ContextFlags & CONTEXT_CONTROL) != CONTEXT_CONTROL ) {
109 _mesa_debug(NULL, "Context does not contain control registers, terminating!\n");
110 return EXCEPTION_EXECUTE_HANDLER;
111 }
112 ctx->Eip += 3;
113
114 return EXCEPTION_CONTINUE_EXECUTION;
115 }
116 #endif /* _WIN32 */
117
118
119 /**
120 * Check if SSE is supported.
121 * If not, turn off the X86_FEATURE_XMM flag in _mesa_x86_cpu_features.
122 */
123 void _mesa_check_os_sse_support( void )
124 {
125 #if defined(__FreeBSD__)
126 {
127 int ret, enabled;
128 unsigned int len;
129 len = sizeof(enabled);
130 ret = sysctlbyname("hw.instruction_sse", &enabled, &len, NULL, 0);
131 if (ret || !enabled)
132 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
133 }
134 #elif defined (__NetBSD__)
135 {
136 int ret, enabled;
137 size_t len = sizeof(enabled);
138 ret = sysctlbyname("machdep.sse", &enabled, &len, (void *)NULL, 0);
139 if (ret || !enabled)
140 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
141 }
142 #elif defined(__OpenBSD__)
143 {
144 int mib[2];
145 int ret, enabled;
146 size_t len = sizeof(enabled);
147
148 mib[0] = CTL_MACHDEP;
149 mib[1] = CPU_SSE;
150
151 ret = sysctl(mib, 2, &enabled, &len, NULL, 0);
152 if (ret || !enabled)
153 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
154 }
155 #elif defined(_WIN32)
156 LPTOP_LEVEL_EXCEPTION_FILTER oldFilter;
157
158 /* Install our ExceptionFilter */
159 oldFilter = SetUnhandledExceptionFilter( ExceptionFilter );
160
161 if ( cpu_has_xmm ) {
162 _mesa_debug(NULL, "Testing OS support for SSE...\n");
163
164 _mesa_test_os_sse_support();
165
166 if ( cpu_has_xmm ) {
167 _mesa_debug(NULL, "Yes.\n");
168 } else {
169 _mesa_debug(NULL, "No!\n");
170 }
171 }
172
173 if ( cpu_has_xmm ) {
174 _mesa_debug(NULL, "Testing OS support for SSE unmasked exceptions...\n");
175
176 _mesa_test_os_sse_exception_support();
177
178 if ( cpu_has_xmm ) {
179 _mesa_debug(NULL, "Yes.\n");
180 } else {
181 _mesa_debug(NULL, "No!\n");
182 }
183 }
184
185 /* Restore previous exception filter */
186 SetUnhandledExceptionFilter( oldFilter );
187
188 if ( cpu_has_xmm ) {
189 _mesa_debug(NULL, "Tests of OS support for SSE passed.\n");
190 } else {
191 _mesa_debug(NULL, "Tests of OS support for SSE failed!\n");
192 }
193 #else
194 /* Do nothing on other platforms for now.
195 */
196 if (detection_debug)
197 _mesa_debug(NULL, "Not testing OS support for SSE, leaving enabled.\n");
198 #endif /* __FreeBSD__ */
199 }
200
201 #endif /* USE_SSE_ASM */
202
203
204 /**
205 * Initialize the _mesa_x86_cpu_features bitfield.
206 * This is a no-op if called more than once.
207 */
208 void
209 _mesa_get_x86_features(void)
210 {
211 static int called = 0;
212
213 if (called)
214 return;
215
216 called = 1;
217
218 #ifdef USE_X86_ASM
219 _mesa_x86_cpu_features = 0x0;
220
221 if (_mesa_getenv( "MESA_NO_ASM")) {
222 return;
223 }
224
225 if (!_mesa_x86_has_cpuid()) {
226 _mesa_debug(NULL, "CPUID not detected\n");
227 }
228 else {
229 GLuint cpu_features, cpu_features_ecx;
230 GLuint cpu_ext_features;
231 GLuint cpu_ext_info;
232 char cpu_vendor[13];
233 GLuint result;
234
235 /* get vendor name */
236 _mesa_x86_cpuid(0, &result, (GLuint *)(cpu_vendor + 0), (GLuint *)(cpu_vendor + 8), (GLuint *)(cpu_vendor + 4));
237 cpu_vendor[12] = '\0';
238
239 if (detection_debug)
240 _mesa_debug(NULL, "CPU vendor: %s\n", cpu_vendor);
241
242 /* get cpu features */
243 cpu_features = _mesa_x86_cpuid_edx(1);
244 cpu_features_ecx = _mesa_x86_cpuid_ecx(1);
245
246 if (cpu_features & X86_CPU_FPU)
247 _mesa_x86_cpu_features |= X86_FEATURE_FPU;
248 if (cpu_features & X86_CPU_CMOV)
249 _mesa_x86_cpu_features |= X86_FEATURE_CMOV;
250
251 #ifdef USE_MMX_ASM
252 if (cpu_features & X86_CPU_MMX)
253 _mesa_x86_cpu_features |= X86_FEATURE_MMX;
254 #endif
255
256 #ifdef USE_SSE_ASM
257 if (cpu_features & X86_CPU_XMM)
258 _mesa_x86_cpu_features |= X86_FEATURE_XMM;
259 if (cpu_features & X86_CPU_XMM2)
260 _mesa_x86_cpu_features |= X86_FEATURE_XMM2;
261 if (cpu_features & x86_CPU_SSE4_1)
262 _mesa_x86_features |= X86_FEATURE_SSE4_1;
263 #endif
264
265 /* query extended cpu features */
266 if ((cpu_ext_info = _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
267 if (cpu_ext_info >= 0x80000001) {
268
269 cpu_ext_features = _mesa_x86_cpuid_edx(0x80000001);
270
271 if (cpu_features & X86_CPU_MMX) {
272
273 #ifdef USE_3DNOW_ASM
274 if (cpu_ext_features & X86_CPUEXT_3DNOW)
275 _mesa_x86_cpu_features |= X86_FEATURE_3DNOW;
276 if (cpu_ext_features & X86_CPUEXT_3DNOW_EXT)
277 _mesa_x86_cpu_features |= X86_FEATURE_3DNOWEXT;
278 #endif
279
280 #ifdef USE_MMX_ASM
281 if (cpu_ext_features & X86_CPUEXT_MMX_EXT)
282 _mesa_x86_cpu_features |= X86_FEATURE_MMXEXT;
283 #endif
284 }
285 }
286
287 /* query cpu name */
288 if (cpu_ext_info >= 0x80000002) {
289 GLuint ofs;
290 char cpu_name[49];
291 for (ofs = 0; ofs < 3; ofs++)
292 _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));
293 cpu_name[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
294
295 if (detection_debug)
296 _mesa_debug(NULL, "CPU name: %s\n", cpu_name);
297 }
298 }
299
300 }
301
302 #ifdef USE_MMX_ASM
303 if ( cpu_has_mmx ) {
304 if ( _mesa_getenv( "MESA_NO_MMX" ) == 0 ) {
305 if (detection_debug)
306 _mesa_debug(NULL, "MMX cpu detected.\n");
307 } else {
308 _mesa_x86_cpu_features &= ~(X86_FEATURE_MMX);
309 }
310 }
311 #endif
312
313 #ifdef USE_3DNOW_ASM
314 if ( cpu_has_3dnow ) {
315 if ( _mesa_getenv( "MESA_NO_3DNOW" ) == 0 ) {
316 if (detection_debug)
317 _mesa_debug(NULL, "3DNow! cpu detected.\n");
318 } else {
319 _mesa_x86_cpu_features &= ~(X86_FEATURE_3DNOW);
320 }
321 }
322 #endif
323
324 #ifdef USE_SSE_ASM
325 if ( cpu_has_xmm ) {
326 if ( _mesa_getenv( "MESA_NO_SSE" ) == 0 ) {
327 if (detection_debug)
328 _mesa_debug(NULL, "SSE cpu detected.\n");
329 if ( _mesa_getenv( "MESA_FORCE_SSE" ) == 0 ) {
330 _mesa_check_os_sse_support();
331 }
332 } else {
333 _mesa_debug(NULL, "SSE cpu detected, but switched off by user.\n");
334 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
335 }
336 }
337 #endif
338
339 #elif defined(USE_X86_64_ASM)
340 unsigned int uninitialized_var(eax), uninitialized_var(ebx),
341 uninitialized_var(ecx), uninitialized_var(edx);
342
343 /* Always available on x86-64. */
344 _mesa_x86_cpu_features |= X86_FEATURE_XMM | X86_FEATURE_XMM2;
345
346 __get_cpuid(1, &eax, &ebx, &ecx, &edx);
347
348 if (ecx & bit_SSE4_1)
349 _mesa_x86_cpu_features |= X86_FEATURE_SSE4_1;
350 #endif /* USE_X86_64_ASM */
351
352 (void) detection_debug;
353 }