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