You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.3KB

  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. #include "config.h"
  19. #ifdef __APPLE__
  20. #include <sys/sysctl.h>
  21. #elif defined(__linux__)
  22. #include <asm/cputable.h>
  23. #include <linux/auxvec.h>
  24. #include <fcntl.h>
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #elif defined(__OpenBSD__)
  29. #include <sys/param.h>
  30. #include <sys/sysctl.h>
  31. #include <machine/cpu.h>
  32. #elif defined(__AMIGAOS4__)
  33. #include <exec/exec.h>
  34. #include <interfaces/exec.h>
  35. #include <proto/exec.h>
  36. #endif /* __APPLE__ */
  37. #include "libavutil/cpu.h"
  38. #include "libavutil/cpu_internal.h"
  39. /**
  40. * This function MAY rely on signal() or fork() in order to make sure AltiVec
  41. * is present.
  42. */
  43. int ff_get_cpu_flags_ppc(void)
  44. {
  45. #if HAVE_ALTIVEC
  46. #ifdef __AMIGAOS4__
  47. ULONG result = 0;
  48. extern struct ExecIFace *IExec;
  49. IExec->GetCPUInfoTags(GCIT_VectorUnit, &result, TAG_DONE);
  50. if (result == VECTORTYPE_ALTIVEC)
  51. return AV_CPU_FLAG_ALTIVEC;
  52. return 0;
  53. #elif defined(__APPLE__) || defined(__OpenBSD__)
  54. #ifdef __OpenBSD__
  55. int sels[2] = {CTL_MACHDEP, CPU_ALTIVEC};
  56. #else
  57. int sels[2] = {CTL_HW, HW_VECTORUNIT};
  58. #endif
  59. int has_vu = 0;
  60. size_t len = sizeof(has_vu);
  61. int err;
  62. err = sysctl(sels, 2, &has_vu, &len, NULL, 0);
  63. if (err == 0)
  64. return has_vu ? AV_CPU_FLAG_ALTIVEC : 0;
  65. return 0;
  66. #elif defined(__linux__)
  67. // The linux kernel could have the altivec support disabled
  68. // even if the cpu has it.
  69. int i, ret = 0;
  70. int fd = open("/proc/self/auxv", O_RDONLY);
  71. unsigned long buf[64] = { 0 };
  72. ssize_t count;
  73. if (fd < 0)
  74. return 0;
  75. while ((count = read(fd, buf, sizeof(buf))) > 0) {
  76. for (i = 0; i < count / sizeof(*buf); i += 2) {
  77. if (buf[i] == AT_NULL)
  78. goto out;
  79. if (buf[i] == AT_HWCAP) {
  80. if (buf[i + 1] & PPC_FEATURE_HAS_ALTIVEC)
  81. ret = AV_CPU_FLAG_ALTIVEC;
  82. #ifdef PPC_FEATURE_HAS_VSX
  83. if (buf[i + 1] & PPC_FEATURE_HAS_VSX)
  84. ret |= AV_CPU_FLAG_VSX;
  85. #endif
  86. #ifdef PPC_FEATURE_ARCH_2_07
  87. if (buf[i + 1] & PPC_FEATURE_HAS_POWER8)
  88. ret |= AV_CPU_FLAG_POWER8;
  89. #endif
  90. if (ret & AV_CPU_FLAG_VSX)
  91. av_assert0(ret & AV_CPU_FLAG_ALTIVEC);
  92. goto out;
  93. }
  94. }
  95. }
  96. out:
  97. close(fd);
  98. return ret;
  99. #elif CONFIG_RUNTIME_CPUDETECT && defined(__linux__)
  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. int ret = 0;
  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. ret = AV_CPU_FLAG_ALTIVEC;
  125. if (proc_ver == PVR_POWER7 ||
  126. proc_ver == PVR_POWER8)
  127. ret |= AV_CPU_FLAG_VSX;
  128. if (proc_ver == PVR_POWER8)
  129. ret |= AV_CPU_FLAG_POWER8;
  130. return ret;
  131. #else
  132. // Since we were compiled for AltiVec, just assume we have it
  133. // until someone comes up with a proper way (not involving signal hacks).
  134. return AV_CPU_FLAG_ALTIVEC;
  135. #endif /* __AMIGAOS4__ */
  136. #endif /* HAVE_ALTIVEC */
  137. return 0;
  138. }