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