ppc_cpudetect.c (1991B)
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 16 #include "config/aom_config.h" 17 18 #include "aom_ports/ppc.h" 19 20 #if CONFIG_RUNTIME_CPU_DETECT 21 #include <asm/cputable.h> 22 #include <linux/auxvec.h> 23 24 static int cpu_env_flags(int *flags) { 25 char *env; 26 env = getenv("AOM_SIMD_CAPS"); 27 if (env && *env) { 28 *flags = (int)strtol(env, NULL, 0); 29 return 0; 30 } 31 *flags = 0; 32 return -1; 33 } 34 35 static int cpu_env_mask(void) { 36 char *env; 37 env = getenv("AOM_SIMD_CAPS_MASK"); 38 return env && *env ? (int)strtol(env, NULL, 0) : ~0; 39 } 40 41 int ppc_simd_caps(void) { 42 int flags; 43 int mask; 44 int fd; 45 ssize_t count; 46 unsigned int i; 47 uint64_t buf[64]; 48 49 // If AOM_SIMD_CAPS_MASK is set then allow only those capabilities. 50 if (!cpu_env_flags(&flags)) { 51 return flags; 52 } 53 54 mask = cpu_env_mask(); 55 56 fd = open("/proc/self/auxv", O_RDONLY); 57 if (fd < 0) { 58 return 0; 59 } 60 61 while ((count = read(fd, buf, sizeof(buf))) > 0) { 62 for (i = 0; i < (count / sizeof(*buf)); i += 2) { 63 if (buf[i] == AT_HWCAP) { 64 #if HAVE_VSX 65 if (buf[i + 1] & PPC_FEATURE_HAS_VSX) { 66 flags |= HAS_VSX; 67 } 68 #endif // HAVE_VSX 69 goto out_close; 70 } else if (buf[i] == AT_NULL) { 71 goto out_close; 72 } 73 } 74 } 75 out_close: 76 close(fd); 77 return flags & mask; 78 } 79 #else 80 // If there is no RTCD the function pointers are not used and can not be 81 // changed. 82 int ppc_simd_caps(void) { return 0; } 83 #endif // CONFIG_RUNTIME_CPU_DETECT