1 /*
2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <stdint.h>
15 #include <asm/cputable.h>
16 #include <linux/auxvec.h>
17
18 #include "config/aom_config.h"
19
20 #include "aom_ports/ppc.h"
21
22 #if CONFIG_RUNTIME_CPU_DETECT
cpu_env_flags(int * flags)23 static int cpu_env_flags(int *flags) {
24 char *env;
25 env = getenv("AOM_SIMD_CAPS");
26 if (env && *env) {
27 *flags = (int)strtol(env, NULL, 0);
28 return 0;
29 }
30 *flags = 0;
31 return -1;
32 }
33
cpu_env_mask(void)34 static int cpu_env_mask(void) {
35 char *env;
36 env = getenv("AOM_SIMD_CAPS_MASK");
37 return env && *env ? (int)strtol(env, NULL, 0) : ~0;
38 }
39
ppc_simd_caps(void)40 int ppc_simd_caps(void) {
41 int flags;
42 int mask;
43 int fd;
44 ssize_t count;
45 unsigned int i;
46 uint64_t buf[64];
47
48 // If AOM_SIMD_CAPS_MASK is set then allow only those capabilities.
49 if (!cpu_env_flags(&flags)) {
50 return flags;
51 }
52
53 mask = cpu_env_mask();
54
55 fd = open("/proc/self/auxv", O_RDONLY);
56 if (fd < 0) {
57 return 0;
58 }
59
60 while ((count = read(fd, buf, sizeof(buf))) > 0) {
61 for (i = 0; i < (count / sizeof(*buf)); i += 2) {
62 if (buf[i] == AT_HWCAP) {
63 #if HAVE_VSX
64 if (buf[i + 1] & PPC_FEATURE_HAS_VSX) {
65 flags |= HAS_VSX;
66 }
67 #endif // HAVE_VSX
68 goto out_close;
69 } else if (buf[i] == AT_NULL) {
70 goto out_close;
71 }
72 }
73 }
74 out_close:
75 close(fd);
76 return flags & mask;
77 }
78 #else
79 // If there is no RTCD the function pointers are not used and can not be
80 // changed.
ppc_simd_caps(void)81 int ppc_simd_caps(void) { return 0; }
82 #endif // CONFIG_RUNTIME_CPU_DETECT
83