]> code.delx.au - pulseaudio/blob - src/pulsecore/cpu-arm.c
32f0e536abbc2e55ae25bd53dc47f3a9dec06de7
[pulseaudio] / src / pulsecore / cpu-arm.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2009 Wim Taymans <wim.taymans@collabora.co.uk>
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdint.h>
28 #include <sys/types.h>
29 #include <fcntl.h>
30
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/log.h>
34
35 #include "cpu-arm.h"
36
37 #if defined (__arm__) && defined (__linux__)
38
39 #define MAX_BUFFER 4096
40 static char *
41 get_cpuinfo_line(char *cpuinfo, const char *tag) {
42 char *line, *end, *colon;
43
44 if (!(line = strstr(cpuinfo, tag)))
45 return NULL;
46
47 if (!(end = strchr(line, '\n')))
48 return NULL;
49
50 if (!(colon = strchr(line, ':')))
51 return NULL;
52
53 if (++colon >= end)
54 return NULL;
55
56 return pa_xstrndup(colon, end - colon);
57 }
58
59 static char *get_cpuinfo(void) {
60 char *cpuinfo;
61 int n, fd;
62
63 cpuinfo = pa_xmalloc(MAX_BUFFER);
64
65 if ((fd = pa_open_cloexec("/proc/cpuinfo", O_RDONLY, 0)) < 0) {
66 pa_xfree(cpuinfo);
67 return NULL;
68 }
69
70 if ((n = pa_read(fd, cpuinfo, MAX_BUFFER-1, NULL)) < 0) {
71 pa_xfree(cpuinfo);
72 pa_close(fd);
73 return NULL;
74 }
75 cpuinfo[n] = 0;
76 pa_close(fd);
77
78 return cpuinfo;
79 }
80 #endif /* defined (__arm__) && defined (__linux__) */
81
82 void pa_cpu_get_arm_flags(pa_cpu_arm_flag_t *flags) {
83 #if defined (__arm__) && defined (__linux__)
84 char *cpuinfo, *line;
85 int arch, part;
86
87 /* We need to read the CPU flags from /proc/cpuinfo because there is no user
88 * space support to get the CPU features. This only works on linux AFAIK. */
89 if (!(cpuinfo = get_cpuinfo())) {
90 pa_log("Can't read cpuinfo");
91 return;
92 }
93
94 *flags = 0;
95
96 /* get the CPU architecture */
97 if ((line = get_cpuinfo_line(cpuinfo, "CPU architecture"))) {
98 arch = strtoul(line, NULL, 0);
99 if (arch >= 6)
100 *flags |= PA_CPU_ARM_V6;
101 if (arch >= 7)
102 *flags |= PA_CPU_ARM_V7;
103
104 pa_xfree(line);
105 }
106
107 /* get the CPU features */
108 if ((line = get_cpuinfo_line(cpuinfo, "Features"))) {
109 const char *state = NULL;
110 char *current;
111
112 while ((current = pa_split_spaces(line, &state))) {
113 if (pa_streq(current, "vfp"))
114 *flags |= PA_CPU_ARM_VFP;
115 else if (pa_streq(current, "edsp"))
116 *flags |= PA_CPU_ARM_EDSP;
117 else if (pa_streq(current, "neon"))
118 *flags |= PA_CPU_ARM_NEON;
119 else if (pa_streq(current, "vfpv3"))
120 *flags |= PA_CPU_ARM_VFPV3;
121
122 pa_xfree(current);
123 }
124 }
125
126 /* get the CPU part number */
127 if ((line = get_cpuinfo_line(cpuinfo, "CPU part"))) {
128 part = strtoul(line, NULL, 0);
129 if (part == 0xc08)
130 *flags |= PA_CPU_ARM_CORTEX_A8;
131 pa_xfree(line);
132 }
133 pa_xfree(cpuinfo);
134
135 pa_log_info("CPU flags: %s%s%s%s%s%s%s",
136 (*flags & PA_CPU_ARM_V6) ? "V6 " : "",
137 (*flags & PA_CPU_ARM_V7) ? "V7 " : "",
138 (*flags & PA_CPU_ARM_VFP) ? "VFP " : "",
139 (*flags & PA_CPU_ARM_EDSP) ? "EDSP " : "",
140 (*flags & PA_CPU_ARM_NEON) ? "NEON " : "",
141 (*flags & PA_CPU_ARM_VFPV3) ? "VFPV3 " : "",
142 (*flags & PA_CPU_ARM_CORTEX_A8) ? "Cortex-A8 " : "");
143 #endif
144 }
145
146 bool pa_cpu_init_arm(pa_cpu_arm_flag_t *flags) {
147 #if defined (__arm__)
148 #if defined (__linux__)
149 pa_cpu_get_arm_flags(flags);
150
151 if (*flags & PA_CPU_ARM_V6)
152 pa_volume_func_init_arm(*flags);
153 #ifdef HAVE_NEON
154 if (*flags & PA_CPU_ARM_NEON) {
155 pa_convert_func_init_neon(*flags);
156 pa_mix_func_init_neon(*flags);
157 }
158 #endif
159
160 return true;
161
162 #else /* defined (__linux__) */
163 pa_log("Reading ARM CPU features not yet supported on this OS");
164 #endif /* defined (__linux__) */
165
166 #else /* defined (__arm__) */
167 return false;
168 #endif /* defined (__arm__) */
169 }