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