Merge commit 'origin/gallium-0.1' into gallium-0.2
[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 * 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.
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
51 #include "main/imports.h"
52 #include "common_x86_asm.h"
53
54
55 int _mesa_x86_cpu_features = 0;
56
57 /* No reason for this to be public.
58 */
59 extern GLuint _ASMAPI _mesa_x86_has_cpuid(void);
60 extern void _ASMAPI _mesa_x86_cpuid(GLuint op, GLuint *reg_eax, GLuint *reg_ebx, GLuint *reg_ecx, GLuint *reg_edx);
61 extern GLuint _ASMAPI _mesa_x86_cpuid_eax(GLuint op);
62 extern GLuint _ASMAPI _mesa_x86_cpuid_ebx(GLuint op);
63 extern GLuint _ASMAPI _mesa_x86_cpuid_ecx(GLuint op);
64 extern GLuint _ASMAPI _mesa_x86_cpuid_edx(GLuint op);
65
66
67 #if defined(USE_SSE_ASM)
68 /*
69 * We must verify that the Streaming SIMD Extensions are truly supported
70 * on this processor before we go ahead and hook out the optimized code.
71 *
72 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
73 * kernels provide full SSE support on all processors that expose SSE via
74 * the CPUID mechanism.
75 */
76 extern void _mesa_test_os_sse_support( void );
77 extern void _mesa_test_os_sse_exception_support( void );
78
79 #if defined(WIN32)
80 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
81 # define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
82 #endif
83 static LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS exp)
84 {
85 PEXCEPTION_RECORD rec = exp->ExceptionRecord;
86 PCONTEXT ctx = exp->ContextRecord;
87
88 if ( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION ) {
89 _mesa_debug(NULL, "EXCEPTION_ILLEGAL_INSTRUCTION\n" );
90 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
91 } else if ( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS ) {
92 _mesa_debug(NULL, "STATUS_FLOAT_MULTIPLE_TRAPS\n");
93 /* Windows seems to clear the exception flag itself, we just have to increment Eip */
94 } else {
95 _mesa_debug(NULL, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
96 return EXCEPTION_EXECUTE_HANDLER;
97 }
98
99 if ( (ctx->ContextFlags & CONTEXT_CONTROL) != CONTEXT_CONTROL ) {
100 _mesa_debug(NULL, "Context does not contain control registers, terminating!\n");
101 return EXCEPTION_EXECUTE_HANDLER;
102 }
103 ctx->Eip += 3;
104
105 return EXCEPTION_CONTINUE_EXECUTION;
106 }
107 #endif /* WIN32 */
108
109
110 static void check_os_sse_support( void )
111 {
112 #if defined(__FreeBSD__)
113 {
114 int ret, enabled;
115 unsigned int len;
116 len = sizeof(enabled);
117 ret = sysctlbyname("hw.instruction_sse", &enabled, &len, NULL, 0);
118 if (ret || !enabled)
119 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
120 }
121 #elif defined (__NetBSD__)
122 {
123 int ret, enabled;
124 size_t len = sizeof(enabled);
125 ret = sysctlbyname("machdep.sse", &enabled, &len, (void *)NULL, 0);
126 if (ret || !enabled)
127 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
128 }
129 #elif defined(__OpenBSD__)
130 {
131 int mib[2];
132 int ret, enabled;
133 size_t len = sizeof(enabled);
134
135 mib[0] = CTL_MACHDEP;
136 mib[1] = CPU_SSE;
137
138 ret = sysctl(mib, 2, &enabled, &len, NULL, 0);
139 if (ret || !enabled)
140 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
141 }
142 #elif defined(WIN32)
143 LPTOP_LEVEL_EXCEPTION_FILTER oldFilter;
144
145 /* Install our ExceptionFilter */
146 oldFilter = SetUnhandledExceptionFilter( ExceptionFilter );
147
148 if ( cpu_has_xmm ) {
149 _mesa_debug(NULL, "Testing OS support for SSE...\n");
150
151 _mesa_test_os_sse_support();
152
153 if ( cpu_has_xmm ) {
154 _mesa_debug(NULL, "Yes.\n");
155 } else {
156 _mesa_debug(NULL, "No!\n");
157 }
158 }
159
160 if ( cpu_has_xmm ) {
161 _mesa_debug(NULL, "Testing OS support for SSE unmasked exceptions...\n");
162
163 _mesa_test_os_sse_exception_support();
164
165 if ( cpu_has_xmm ) {
166 _mesa_debug(NULL, "Yes.\n");
167 } else {
168 _mesa_debug(NULL, "No!\n");
169 }
170 }
171
172 /* Restore previous exception filter */
173 SetUnhandledExceptionFilter( oldFilter );
174
175 if ( cpu_has_xmm ) {
176 _mesa_debug(NULL, "Tests of OS support for SSE passed.\n");
177 } else {
178 _mesa_debug(NULL, "Tests of OS support for SSE failed!\n");
179 }
180 #else
181 /* Do nothing on other platforms for now.
182 */
183 _mesa_debug(NULL, "Not testing OS support for SSE, leaving enabled.\n");
184 #endif /* __FreeBSD__ */
185 }
186
187 #endif /* USE_SSE_ASM */
188
189
190 void _mesa_init_all_x86_transform_asm( void )
191 {
192 #ifdef USE_X86_ASM
193 _mesa_x86_cpu_features = 0;
194
195 if (!_mesa_x86_has_cpuid()) {
196 _mesa_debug(NULL, "CPUID not detected\n");
197 }
198 else {
199 GLuint cpu_features;
200 GLuint cpu_ext_features;
201 GLuint cpu_ext_info;
202 char cpu_vendor[13];
203 GLuint result;
204
205 /* get vendor name */
206 _mesa_x86_cpuid(0, &result, (GLuint *)(cpu_vendor + 0), (GLuint *)(cpu_vendor + 8), (GLuint *)(cpu_vendor + 4));
207 cpu_vendor[12] = '\0';
208
209 _mesa_debug(NULL, "CPU vendor: %s\n", cpu_vendor);
210
211 /* get cpu features */
212 cpu_features = _mesa_x86_cpuid_edx(1);
213
214 if (cpu_features & X86_CPU_FPU)
215 _mesa_x86_cpu_features |= X86_FEATURE_FPU;
216 if (cpu_features & X86_CPU_CMOV)
217 _mesa_x86_cpu_features |= X86_FEATURE_CMOV;
218
219 #ifdef USE_MMX_ASM
220 if (cpu_features & X86_CPU_MMX)
221 _mesa_x86_cpu_features |= X86_FEATURE_MMX;
222 #endif
223
224 #ifdef USE_SSE_ASM
225 if (cpu_features & X86_CPU_XMM)
226 _mesa_x86_cpu_features |= X86_FEATURE_XMM;
227 if (cpu_features & X86_CPU_XMM2)
228 _mesa_x86_cpu_features |= X86_FEATURE_XMM2;
229 #endif
230
231 /* query extended cpu features */
232 if ((cpu_ext_info = _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
233 if (cpu_ext_info >= 0x80000001) {
234
235 cpu_ext_features = _mesa_x86_cpuid_edx(0x80000001);
236
237 if (cpu_features & X86_CPU_MMX) {
238
239 #ifdef USE_3DNOW_ASM
240 if (cpu_ext_features & X86_CPUEXT_3DNOW)
241 _mesa_x86_cpu_features |= X86_FEATURE_3DNOW;
242 if (cpu_ext_features & X86_CPUEXT_3DNOW_EXT)
243 _mesa_x86_cpu_features |= X86_FEATURE_3DNOWEXT;
244 #endif
245
246 #ifdef USE_MMX_ASM
247 if (cpu_ext_features & X86_CPUEXT_MMX_EXT)
248 _mesa_x86_cpu_features |= X86_FEATURE_MMXEXT;
249 #endif
250 }
251 }
252
253 /* query cpu name */
254 if (cpu_ext_info >= 0x80000002) {
255 GLuint ofs;
256 char cpu_name[49];
257 for (ofs = 0; ofs < 3; ofs++)
258 _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));
259 cpu_name[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
260
261 _mesa_debug(NULL, "CPU name: %s\n", cpu_name);
262 }
263 }
264
265 }
266
267 if ( _mesa_getenv( "MESA_NO_ASM" ) ) {
268 _mesa_x86_cpu_features = 0;
269 }
270
271 if ( _mesa_x86_cpu_features ) {
272 _mesa_init_x86_transform_asm();
273 }
274
275 #ifdef USE_MMX_ASM
276 if ( cpu_has_mmx ) {
277 if ( _mesa_getenv( "MESA_NO_MMX" ) == 0 ) {
278 _mesa_debug(NULL, "MMX cpu detected.\n");
279 } else {
280 _mesa_x86_cpu_features &= ~(X86_FEATURE_MMX);
281 }
282 }
283 #endif
284
285 #ifdef USE_3DNOW_ASM
286 if ( cpu_has_3dnow ) {
287 if ( _mesa_getenv( "MESA_NO_3DNOW" ) == 0 ) {
288 _mesa_debug(NULL, "3DNow! cpu detected.\n");
289 _mesa_init_3dnow_transform_asm();
290 } else {
291 _mesa_x86_cpu_features &= ~(X86_FEATURE_3DNOW);
292 }
293 }
294 #endif
295
296 #ifdef USE_SSE_ASM
297 if ( cpu_has_xmm ) {
298 if ( _mesa_getenv( "MESA_NO_SSE" ) == 0 ) {
299 _mesa_debug(NULL, "SSE cpu detected.\n");
300 if ( _mesa_getenv( "MESA_FORCE_SSE" ) == 0 ) {
301 check_os_sse_support();
302 }
303 if ( cpu_has_xmm ) {
304 _mesa_init_sse_transform_asm();
305 }
306 } else {
307 _mesa_debug(NULL, "SSE cpu detected, but switched off by user.\n");
308 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
309 }
310 }
311 #endif
312 #endif
313 }
314