1 /*
2 * Copyright © 2000 SuSE, Inc.
3 * Copyright © 2007 Red Hat, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of SuSE not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. SuSE makes no representations about the
12 * suitability of this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
14 *
15 * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "pixman-private.h"
27
28 typedef enum
29 {
30 ARM_V7 = (1 << 0),
31 ARM_V6 = (1 << 1),
32 ARM_VFP = (1 << 2),
33 ARM_NEON = (1 << 3),
34 ARM_IWMMXT = (1 << 4)
35 } arm_cpu_features_t;
36
37 #if defined(USE_ARM_SIMD) || defined(USE_ARM_NEON) || defined(USE_ARM_IWMMXT)
38
39 #if defined(_MSC_VER)
40
41 /* Needed for EXCEPTION_ILLEGAL_INSTRUCTION */
42 #include <windows.h>
43
44 extern int pixman_msvc_try_arm_neon_op ();
45 extern int pixman_msvc_try_arm_simd_op ();
46
47 static arm_cpu_features_t
detect_cpu_features(void)48 detect_cpu_features (void)
49 {
50 arm_cpu_features_t features = 0;
51
52 __try
53 {
54 pixman_msvc_try_arm_simd_op ();
55 features |= ARM_V6;
56 }
57 __except (GetExceptionCode () == EXCEPTION_ILLEGAL_INSTRUCTION)
58 {
59 }
60
61 __try
62 {
63 pixman_msvc_try_arm_neon_op ();
64 features |= ARM_NEON;
65 }
66 __except (GetExceptionCode () == EXCEPTION_ILLEGAL_INSTRUCTION)
67 {
68 }
69
70 return features;
71 }
72
73 #elif defined(__APPLE__) && defined(TARGET_OS_IPHONE) /* iOS */
74
75 #include "TargetConditionals.h"
76
77 static arm_cpu_features_t
detect_cpu_features(void)78 detect_cpu_features (void)
79 {
80 arm_cpu_features_t features = 0;
81
82 features |= ARM_V6;
83
84 /* Detection of ARM NEON on iOS is fairly simple because iOS binaries
85 * contain separate executable images for each processor architecture.
86 * So all we have to do is detect the armv7 architecture build. The
87 * operating system automatically runs the armv7 binary for armv7 devices
88 * and the armv6 binary for armv6 devices.
89 */
90 #if defined(__ARM_NEON__)
91 features |= ARM_NEON;
92 #endif
93
94 return features;
95 }
96
97 #elif defined(__ANDROID__) || defined(ANDROID) /* Android */
98
99 #include <cpu-features.h>
100
101 static arm_cpu_features_t
detect_cpu_features(void)102 detect_cpu_features (void)
103 {
104 arm_cpu_features_t features = 0;
105 AndroidCpuFamily cpu_family;
106 uint64_t cpu_features;
107
108 cpu_family = android_getCpuFamily();
109 cpu_features = android_getCpuFeatures();
110
111 if (cpu_family == ANDROID_CPU_FAMILY_ARM)
112 {
113 if (cpu_features & ANDROID_CPU_ARM_FEATURE_ARMv7)
114 features |= ARM_V7;
115
116 if (cpu_features & ANDROID_CPU_ARM_FEATURE_VFPv3)
117 features |= ARM_VFP;
118
119 if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON)
120 features |= ARM_NEON;
121 }
122
123 return features;
124 }
125
126 #elif defined (__linux__) /* linux ELF */
127
128 #include <unistd.h>
129 #include <sys/types.h>
130 #include <sys/stat.h>
131 #include <sys/mman.h>
132 #include <fcntl.h>
133 #include <string.h>
134 #include <elf.h>
135
136 static arm_cpu_features_t
detect_cpu_features(void)137 detect_cpu_features (void)
138 {
139 arm_cpu_features_t features = 0;
140 Elf32_auxv_t aux;
141 int fd;
142
143 fd = open ("/proc/self/auxv", O_RDONLY);
144 if (fd >= 0)
145 {
146 while (read (fd, &aux, sizeof(Elf32_auxv_t)) == sizeof(Elf32_auxv_t))
147 {
148 if (aux.a_type == AT_HWCAP)
149 {
150 uint32_t hwcap = aux.a_un.a_val;
151
152 /* hardcode these values to avoid depending on specific
153 * versions of the hwcap header, e.g. HWCAP_NEON
154 */
155 if ((hwcap & 64) != 0)
156 features |= ARM_VFP;
157 if ((hwcap & 512) != 0)
158 features |= ARM_IWMMXT;
159 /* this flag is only present on kernel 2.6.29 */
160 if ((hwcap & 4096) != 0)
161 features |= ARM_NEON;
162 }
163 else if (aux.a_type == AT_PLATFORM)
164 {
165 const char *plat = (const char*) aux.a_un.a_val;
166
167 if (strncmp (plat, "v7l", 3) == 0)
168 features |= (ARM_V7 | ARM_V6);
169 else if (strncmp (plat, "v6l", 3) == 0)
170 features |= ARM_V6;
171 }
172 }
173 close (fd);
174 }
175
176 return features;
177 }
178
179 #else /* Unknown */
180
181 static arm_cpu_features_t
detect_cpu_features(void)182 detect_cpu_features (void)
183 {
184 return 0;
185 }
186
187 #endif /* Linux elf */
188
189 static pixman_bool_t
have_feature(arm_cpu_features_t feature)190 have_feature (arm_cpu_features_t feature)
191 {
192 static pixman_bool_t initialized;
193 static arm_cpu_features_t features;
194
195 if (!initialized)
196 {
197 features = detect_cpu_features();
198 initialized = TRUE;
199 }
200
201 return (features & feature) == feature;
202 }
203
204 #endif /* USE_ARM_SIMD || USE_ARM_NEON || USE_ARM_IWMMXT */
205
206 pixman_implementation_t *
_pixman_arm_get_implementations(pixman_implementation_t * imp)207 _pixman_arm_get_implementations (pixman_implementation_t *imp)
208 {
209 #ifdef USE_ARM_SIMD
210 if (!_pixman_disabled ("arm-simd") && have_feature (ARM_V6))
211 imp = _pixman_implementation_create_arm_simd (imp);
212 #endif
213
214 #ifdef USE_ARM_IWMMXT
215 if (!_pixman_disabled ("arm-iwmmxt") && have_feature (ARM_IWMMXT))
216 imp = _pixman_implementation_create_mmx (imp);
217 #endif
218
219 #ifdef USE_ARM_NEON
220 if (!_pixman_disabled ("arm-neon") && have_feature (ARM_NEON))
221 imp = _pixman_implementation_create_arm_neon (imp);
222 #endif
223
224 return imp;
225 }
226