]> git.sesse.net Git - ffmpeg/blob - libavutil/ppc/cpu.c
Merge commit '3058872c293e239e3b51e86fe18cfbe720aadff1'
[ffmpeg] / libavutil / ppc / cpu.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "config.h"
20
21 #ifdef __APPLE__
22 #include <sys/sysctl.h>
23 #elif defined(__linux__)
24 #include <asm/cputable.h>
25 #include <linux/auxvec.h>
26 #include <fcntl.h>
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #elif defined(__OpenBSD__)
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
33 #include <machine/cpu.h>
34 #elif defined(__AMIGAOS4__)
35 #include <exec/exec.h>
36 #include <interfaces/exec.h>
37 #include <proto/exec.h>
38 #endif /* __APPLE__ */
39
40 #include "libavutil/cpu.h"
41 #include "libavutil/cpu_internal.h"
42
43 /**
44  * This function MAY rely on signal() or fork() in order to make sure AltiVec
45  * is present.
46  */
47 int ff_get_cpu_flags_ppc(void)
48 {
49 #if HAVE_ALTIVEC
50 #ifdef __AMIGAOS4__
51     ULONG result = 0;
52     extern struct ExecIFace *IExec;
53
54     IExec->GetCPUInfoTags(GCIT_VectorUnit, &result, TAG_DONE);
55     if (result == VECTORTYPE_ALTIVEC)
56         return AV_CPU_FLAG_ALTIVEC;
57     return 0;
58 #elif defined(__APPLE__) || defined(__OpenBSD__)
59 #ifdef __OpenBSD__
60     int sels[2] = {CTL_MACHDEP, CPU_ALTIVEC};
61 #else
62     int sels[2] = {CTL_HW, HW_VECTORUNIT};
63 #endif
64     int has_vu = 0;
65     size_t len = sizeof(has_vu);
66     int err;
67
68     err = sysctl(sels, 2, &has_vu, &len, NULL, 0);
69
70     if (err == 0)
71         return has_vu ? AV_CPU_FLAG_ALTIVEC : 0;
72     return 0;
73 #elif defined(__linux__)
74     // The linux kernel could have the altivec support disabled
75     // even if the cpu has it.
76     int i, ret = 0;
77     int fd = open("/proc/self/auxv", O_RDONLY);
78     unsigned long buf[64] = { 0 };
79     ssize_t count;
80
81     if (fd < 0)
82         return 0;
83
84     while ((count = read(fd, buf, sizeof(buf))) > 0) {
85         for (i = 0; i < count / sizeof(*buf); i += 2) {
86             if (buf[i] == AT_NULL)
87                 goto out;
88             if (buf[i] == AT_HWCAP) {
89                 if (buf[i + 1] & PPC_FEATURE_HAS_ALTIVEC)
90                     ret = AV_CPU_FLAG_ALTIVEC;
91                 goto out;
92             }
93         }
94     }
95
96 out:
97     close(fd);
98     return ret;
99 #elif CONFIG_RUNTIME_CPUDETECT && defined(__linux__) && !ARCH_PPC64
100 #define PVR_G4_7400  0x000C
101 #define PVR_G5_970   0x0039
102 #define PVR_G5_970FX 0x003C
103 #define PVR_G5_970MP 0x0044
104 #define PVR_G5_970GX 0x0045
105 #define PVR_POWER6   0x003E
106 #define PVR_POWER7   0x003F
107 #define PVR_POWER8   0x004B
108 #define PVR_CELL_PPU 0x0070
109
110     int proc_ver;
111     // Support of mfspr PVR emulation added in Linux 2.6.17.
112     __asm__ volatile("mfspr %0, 287" : "=r" (proc_ver));
113     proc_ver >>= 16;
114     if (proc_ver  & 0x8000 ||
115         proc_ver == PVR_G4_7400  ||
116         proc_ver == PVR_G5_970   ||
117         proc_ver == PVR_G5_970FX ||
118         proc_ver == PVR_G5_970MP ||
119         proc_ver == PVR_G5_970GX ||
120         proc_ver == PVR_POWER6   ||
121         proc_ver == PVR_POWER7   ||
122         proc_ver == PVR_POWER8   ||
123         proc_ver == PVR_CELL_PPU)
124         return AV_CPU_FLAG_ALTIVEC;
125     return 0;
126 #else
127     // Since we were compiled for AltiVec, just assume we have it
128     // until someone comes up with a proper way (not involving signal hacks).
129     return AV_CPU_FLAG_ALTIVEC;
130 #endif /* __AMIGAOS4__ */
131 #endif /* HAVE_ALTIVEC */
132     return 0;
133 }