summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/cpu-arm.c
blob: 1d0d76516798ad565824c15c738ac9ab2bd3380a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/***
  This file is part of PulseAudio.

  Copyright 2004-2006 Lennart Poettering
  Copyright 2009 Wim Taymans <wim.taymans@collabora.co.uk>

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <pulse/xmalloc.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>

#include "cpu-arm.h"

#if defined (__arm__) && defined (__linux__)

#define MAX_BUFFER  4096
static char *
get_cpuinfo_line (char *cpuinfo, const char *tag) {
    char *line, *end, *colon;

    if (!(line = strstr (cpuinfo, tag)))
        return NULL;

    if (!(end = strchr (line, '\n')))
        return NULL;

    if (!(colon = strchr (line, ':')))
        return NULL;

    if (++colon >= end)
        return NULL;

    return pa_xstrndup (colon, end - colon);
}

static char *get_cpuinfo(void) {
    char *cpuinfo;
    int n, fd;

    cpuinfo = pa_xmalloc(MAX_BUFFER);

    if ((fd = pa_open_cloexec("/proc/cpuinfo", O_RDONLY, 0)) < 0) {
        pa_xfree(cpuinfo);
        return NULL;
    }

    if ((n = pa_read(fd, cpuinfo, MAX_BUFFER-1, NULL)) < 0) {
        pa_xfree(cpuinfo);
        pa_close(fd);
        return NULL;
    }
    cpuinfo[n] = 0;
    pa_close(fd);

    return cpuinfo;
}
#endif /* defined (__arm__) && defined (__linux__) */

pa_bool_t pa_cpu_init_arm (pa_cpu_arm_flag_t *flags) {
#if defined (__arm__)
#if defined (__linux__)
    char *cpuinfo, *line;
    int arch;

    /* We need to read the CPU flags from /proc/cpuinfo because there is no user
     * space support to get the CPU features. This only works on linux AFAIK. */
    if (!(cpuinfo = get_cpuinfo ())) {
        pa_log ("Can't read cpuinfo");
        return;
    }

    *flags = 0;

    /* get the CPU architecture */
    if ((line = get_cpuinfo_line (cpuinfo, "CPU architecture"))) {
        arch = strtoul (line, NULL, 0);
        if (arch >= 6)
            *flags |= PA_CPU_ARM_V6;
        if (arch >= 7)
            *flags |= PA_CPU_ARM_V7;

        pa_xfree(line);
    }
    /* get the CPU features */
    if ((line = get_cpuinfo_line (cpuinfo, "Features"))) {
        const char *state = NULL;
        char *current;

        while ((current = pa_split_spaces (line, &state))) {
            if (!strcmp (current, "vfp"))
                *flags |= PA_CPU_ARM_VFP;
            else if (!strcmp (current, "edsp"))
                *flags |= PA_CPU_ARM_EDSP;
            else if (!strcmp (current, "neon"))
                *flags |= PA_CPU_ARM_NEON;
            else if (!strcmp (current, "vfpv3"))
                *flags |= PA_CPU_ARM_VFPV3;

            pa_xfree(current);
        }
    }
    pa_xfree(cpuinfo);

    pa_log_info ("CPU flags: %s%s%s%s%s%s",
          (*flags & PA_CPU_ARM_V6) ? "V6 " : "",
          (*flags & PA_CPU_ARM_V7) ? "V7 " : "",
          (*flags & PA_CPU_ARM_VFP) ? "VFP " : "",
          (*flags & PA_CPU_ARM_EDSP) ? "EDSP " : "",
          (*flags & PA_CPU_ARM_NEON) ? "NEON " : "",
          (*flags & PA_CPU_ARM_VFPV3) ? "VFPV3 " : "");

    if (*flags & PA_CPU_ARM_V6)
        pa_volume_func_init_arm (*flags);

    return TRUE;

#else /* defined (__linux__) */
    pa_log ("ARM cpu features not yet supported on this OS");
#endif /* defined (__linux__) */

#else /* defined (__arm__) */
    return FALSE;
#endif /* defined (__arm__) */
}