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, see <http://www.gnu.org/licenses/>.
19***/
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <stdint.h>
26#include <sys/types.h>
27#include <fcntl.h>
28
29#include <pulse/xmalloc.h>
30#include <pulsecore/core-util.h>
31#include <pulsecore/log.h>
32
33#include "cpu-arm.h"
34
35#if defined (__arm__) && defined (__linux__)
36
37#define MAX_BUFFER 4096
38static char *
39get_cpuinfo_line(char *cpuinfo, const char *tag) {
40    char *line, *end, *colon;
41
42    if (!(line = strstr(cpuinfo, tag)))
43        return NULL;
44
45    if (!(end = strchr(line, '\n')))
46        return NULL;
47
48    if (!(colon = strchr(line, ':')))
49        return NULL;
50
51    if (++colon >= end)
52        return NULL;
53
54    return pa_xstrndup(colon, end - colon);
55}
56
57static char *get_cpuinfo(void) {
58    char *cpuinfo;
59    int n, fd;
60
61    cpuinfo = pa_xmalloc(MAX_BUFFER);
62
63    if ((fd = pa_open_cloexec("/proc/cpuinfo", O_RDONLY, 0)) < 0) {
64        pa_xfree(cpuinfo);
65        return NULL;
66    }
67
68    if ((n = pa_read(fd, cpuinfo, MAX_BUFFER-1, NULL)) < 0) {
69        pa_xfree(cpuinfo);
70        pa_close(fd);
71        return NULL;
72    }
73    cpuinfo[n] = 0;
74    pa_close(fd);
75
76    return cpuinfo;
77}
78#endif /* defined (__arm__) && defined (__linux__) */
79
80void pa_cpu_get_arm_flags(pa_cpu_arm_flag_t *flags) {
81#if defined (__arm__) && defined (__linux__)
82    char *cpuinfo, *line;
83    int arch, part;
84
85    /* We need to read the CPU flags from /proc/cpuinfo because there is no user
86     * space support to get the CPU features. This only works on linux AFAIK. */
87    if (!(cpuinfo = get_cpuinfo())) {
88        pa_log("Can't read cpuinfo");
89        return;
90    }
91
92    *flags = 0;
93
94    /* get the CPU architecture */
95    if ((line = get_cpuinfo_line(cpuinfo, "CPU architecture"))) {
96        arch = strtoul(line, NULL, 0);
97        if (arch >= 6)
98            *flags |= PA_CPU_ARM_V6;
99        if (arch >= 7)
100            *flags |= PA_CPU_ARM_V7;
101
102        pa_xfree(line);
103    }
104
105    /* get the CPU features */
106    if ((line = get_cpuinfo_line(cpuinfo, "Features"))) {
107        const char *state = NULL;
108        char *current;
109
110        while ((current = pa_split_spaces(line, &state))) {
111            if (pa_streq(current, "vfp"))
112                *flags |= PA_CPU_ARM_VFP;
113            else if (pa_streq(current, "edsp"))
114                *flags |= PA_CPU_ARM_EDSP;
115            else if (pa_streq(current, "neon"))
116                *flags |= PA_CPU_ARM_NEON;
117            else if (pa_streq(current, "vfpv3"))
118                *flags |= PA_CPU_ARM_VFPV3;
119
120            pa_xfree(current);
121        }
122        pa_xfree(line);
123    }
124
125    /* get the CPU part number */
126    if ((line = get_cpuinfo_line(cpuinfo, "CPU part"))) {
127        part = strtoul(line, NULL, 0);
128        if (part == 0xc08)
129            *flags |= PA_CPU_ARM_CORTEX_A8;
130        pa_xfree(line);
131    }
132    pa_xfree(cpuinfo);
133
134    pa_log_info("CPU flags: %s%s%s%s%s%s%s",
135          (*flags & PA_CPU_ARM_V6) ? "V6 " : "",
136          (*flags & PA_CPU_ARM_V7) ? "V7 " : "",
137          (*flags & PA_CPU_ARM_VFP) ? "VFP " : "",
138          (*flags & PA_CPU_ARM_EDSP) ? "EDSP " : "",
139          (*flags & PA_CPU_ARM_NEON) ? "NEON " : "",
140          (*flags & PA_CPU_ARM_VFPV3) ? "VFPV3 " : "",
141          (*flags & PA_CPU_ARM_CORTEX_A8) ? "Cortex-A8 " : "");
142#endif
143}
144
145bool pa_cpu_init_arm(pa_cpu_arm_flag_t *flags) {
146#if defined (__arm__)
147#if defined (__linux__)
148    pa_cpu_get_arm_flags(flags);
149
150    if (*flags & PA_CPU_ARM_V6)
151        pa_volume_func_init_arm(*flags);
152
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        pa_remap_func_init_neon(*flags);
158    }
159#endif
160
161    return true;
162
163#else /* defined (__linux__) */
164    pa_log("Reading ARM CPU features not yet supported on this OS");
165#endif /* defined (__linux__) */
166
167#else /* defined (__arm__) */
168    return false;
169#endif /* defined (__arm__) */
170}
171