|
1 /* |
|
2 * LIBOIL - Library of Optimized Inner Loops |
|
3 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org> |
|
4 * All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions |
|
8 * are met: |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * 2. Redistributions in binary form must reproduce the above copyright |
|
12 * notice, this list of conditions and the following disclaimer in the |
|
13 * documentation and/or other materials provided with the distribution. |
|
14 * |
|
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
|
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
25 * POSSIBILITY OF SUCH DAMAGE. |
|
26 */ |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include "config.h" |
|
30 #endif |
|
31 #include <liboil/liboilfunction.h> |
|
32 #include <liboil/liboildebug.h> |
|
33 #include <liboil/liboilcpu.h> |
|
34 #include <liboil/liboilfault.h> |
|
35 #include <liboil/liboilutils.h> |
|
36 |
|
37 #if defined(__linux__) |
|
38 #include <linux/auxvec.h> |
|
39 #include <sys/types.h> |
|
40 #include <sys/stat.h> |
|
41 #include <fcntl.h> |
|
42 #include <unistd.h> |
|
43 #include <stdio.h> |
|
44 |
|
45 #ifndef PPC_FEATURE_HAS_ALTIVEC |
|
46 /* From linux-2.6/include/asm-powerpc/cputable.h */ |
|
47 #define PPC_FEATURE_HAS_ALTIVEC 0x10000000 |
|
48 #endif |
|
49 |
|
50 #endif |
|
51 |
|
52 #if defined(__APPLE__) |
|
53 #include <sys/types.h> |
|
54 #include <sys/sysctl.h> |
|
55 #endif |
|
56 |
|
57 #if defined(__FreeBSD__) |
|
58 #include <sys/types.h> |
|
59 #include <sys/sysctl.h> |
|
60 #endif |
|
61 |
|
62 |
|
63 /***** powerpc *****/ |
|
64 |
|
65 static unsigned long |
|
66 oil_profile_stamp_tb(void) |
|
67 { |
|
68 unsigned long ts; |
|
69 __asm__ __volatile__("mftb %0\n" : "=r" (ts)); |
|
70 return ts; |
|
71 } |
|
72 |
|
73 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__) |
|
74 static void |
|
75 test_altivec (void * ignored) |
|
76 { |
|
77 asm volatile ("vor v0, v0, v0\n"); |
|
78 } |
|
79 #endif |
|
80 |
|
81 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
|
82 static void |
|
83 oil_check_altivec_sysctl_freebsd (void) |
|
84 { |
|
85 int ret, av; |
|
86 size_t len; |
|
87 |
|
88 len = sizeof(av); |
|
89 ret = sysctlbyname("hw.altivec", &av, &len, NULL, 0); |
|
90 if (!ret && av) { |
|
91 oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC; |
|
92 } |
|
93 } |
|
94 #endif |
|
95 |
|
96 #if defined(__APPLE__) |
|
97 static void |
|
98 oil_check_altivec_sysctl_darwin (void) |
|
99 { |
|
100 int ret, vu; |
|
101 size_t len; |
|
102 |
|
103 len = sizeof(vu); |
|
104 ret = sysctlbyname("hw.vectorunit", &vu, &len, NULL, 0); |
|
105 if (!ret && vu) { |
|
106 oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC; |
|
107 } |
|
108 } |
|
109 #endif |
|
110 |
|
111 #if defined(__linux__) |
|
112 static void |
|
113 oil_check_altivec_proc_auxv (void) |
|
114 { |
|
115 static int available = -1; |
|
116 int new_avail = 0; |
|
117 unsigned long buf[64]; |
|
118 ssize_t count; |
|
119 int fd, i; |
|
120 |
|
121 /* Flags already set */ |
|
122 if (available != -1) { |
|
123 return; |
|
124 } |
|
125 |
|
126 fd = open("/proc/self/auxv", O_RDONLY); |
|
127 if (fd < 0) { |
|
128 goto out; |
|
129 } |
|
130 |
|
131 more: |
|
132 count = read(fd, buf, sizeof(buf)); |
|
133 if (count < 0) { |
|
134 goto out_close; |
|
135 } |
|
136 |
|
137 for (i=0; i < (count / sizeof(unsigned long)); i += 2) { |
|
138 if (buf[i] == AT_HWCAP) { |
|
139 new_avail = !!(buf[i+1] & PPC_FEATURE_HAS_ALTIVEC); |
|
140 goto out_close; |
|
141 } else if (buf[i] == AT_NULL) { |
|
142 goto out_close; |
|
143 } |
|
144 } |
|
145 |
|
146 if (count == sizeof(buf)) { |
|
147 goto more; |
|
148 } |
|
149 |
|
150 out_close: |
|
151 close(fd); |
|
152 |
|
153 out: |
|
154 available = new_avail; |
|
155 if (available) { |
|
156 oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC; |
|
157 } |
|
158 } |
|
159 #endif |
|
160 |
|
161 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__) |
|
162 static void |
|
163 oil_check_altivec_fault (void) |
|
164 { |
|
165 oil_fault_check_enable (); |
|
166 if (oil_fault_check_try(test_altivec, NULL)) { |
|
167 OIL_DEBUG ("cpu flag altivec"); |
|
168 oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC; |
|
169 } |
|
170 oil_fault_check_disable (); |
|
171 } |
|
172 #endif |
|
173 |
|
174 void |
|
175 oil_cpu_detect_arch(void) |
|
176 { |
|
177 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
|
178 oil_check_altivec_sysctl_freebsd(); |
|
179 #elif defined(__APPLE__) |
|
180 oil_check_altivec_sysctl_darwin(); |
|
181 #elif defined(__linux__) |
|
182 oil_check_altivec_proc_auxv(); |
|
183 #else |
|
184 oil_check_altivec_fault(); |
|
185 #endif |
|
186 |
|
187 _oil_profile_stamp = oil_profile_stamp_tb; |
|
188 } |
|
189 |
|
190 |
|
191 |