gallium: fix refcount bug introduced in eb20e2984
[mesa.git] / src / mesa / ppc / common_ppc.c
1 /*
2 * (C) Copyright IBM Corporation 2004
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file common_ppc.c
27 * Check CPU capabilities & initialize optimized funtions for this particular
28 * processor.
29 *
30 * \author Ian Romanick <idr@us.ibm.com>
31 */
32
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36
37 #ifdef USE_PPC_ASM
38 #include <elf.h>
39 #endif
40
41 unsigned long _mesa_ppc_cpu_features = 0;
42
43 /**
44 * Detect CPU features and install optimized transform and lighting routines.
45 * Currently, CPU features are only detected. The optimized routines have
46 * yet to be written.
47 *
48 * \bug
49 * This routine is highly specific to Linux kernel 2.6. I'm still waiting
50 * to hear back from the glibc folk on how to do this "right".
51 */
52
53 void _mesa_init_all_ppc_transform_asm( void )
54 {
55 #ifdef USE_PPC_ASM
56 const pid_t my_pid = getpid();
57 char file_name[32];
58 FILE * f;
59 #ifdef __powerpc64__
60 Elf64_auxv_t v;
61 #else
62 Elf32_auxv_t v;
63 #endif
64
65 sprintf( file_name, "/proc/%u/auxv", (unsigned) my_pid );
66 f = fopen( file_name, "rb" );
67 if ( f != NULL ) {
68 while( 1 ) {
69 ssize_t elem = fread( & v, sizeof( v ), 1, f );
70
71 if ( elem < 1 ) {
72 break;
73 }
74
75 if ( v.a_type == AT_HWCAP ) {
76 _mesa_ppc_cpu_features = v.a_un.a_val;
77 break;
78 }
79 }
80
81 fclose( f );
82 }
83
84 # ifndef USE_VMX_ASM
85 _mesa_ppc_cpu_features &= ~PPC_FEATURES_HAS_ALTIVEC;
86 # endif
87 #endif
88 }