cpu.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/avassert.h"
  38. #include "libavutil/cpu.h"
  39. #include "libavutil/cpu_internal.h"
  40. /**
  41. * This function MAY rely on signal() or fork() in order to make sure AltiVec
  42. * is present.
  43. */
  44. int ff_get_cpu_flags_ppc(void)
  45. {
  46. #if HAVE_ALTIVEC
  47. #ifdef __AMIGAOS4__
  48. ULONG result = 0;
  49. extern struct ExecIFace *IExec;
  50. IExec->GetCPUInfoTags(GCIT_VectorUnit, &result, TAG_DONE);
  51. if (result == VECTORTYPE_ALTIVEC)
  52. return AV_CPU_FLAG_ALTIVEC;
  53. return 0;
  54. #elif defined(__APPLE__) || defined(__OpenBSD__)
  55. #ifdef __OpenBSD__
  56. int sels[2] = {CTL_MACHDEP, CPU_ALTIVEC};
  57. #else
  58. int sels[2] = {CTL_HW, HW_VECTORUNIT};
  59. #endif
  60. int has_vu = 0;
  61. size_t len = sizeof(has_vu);
  62. int err;
  63. err = sysctl(sels, 2, &has_vu, &len, NULL, 0);
  64. if (err == 0)
  65. return has_vu ? AV_CPU_FLAG_ALTIVEC : 0;
  66. return 0;
  67. #elif defined(__linux__)
  68. // The linux kernel could have the altivec support disabled
  69. // even if the cpu has it.
  70. int i, ret = 0;
  71. int fd = open("/proc/self/auxv", O_RDONLY);
  72. unsigned long buf[64] = { 0 };
  73. ssize_t count;
  74. if (fd < 0)
  75. return 0;
  76. while ((count = read(fd, buf, sizeof(buf))) > 0) {
  77. for (i = 0; i < count / sizeof(*buf); i += 2) {
  78. if (buf[i] == AT_NULL)
  79. goto out;
  80. if (buf[i] == AT_HWCAP) {
  81. if (buf[i + 1] & PPC_FEATURE_HAS_ALTIVEC)
  82. ret = AV_CPU_FLAG_ALTIVEC;
  83. #ifdef PPC_FEATURE_HAS_VSX
  84. if (buf[i + 1] & PPC_FEATURE_HAS_VSX)
  85. ret |= AV_CPU_FLAG_VSX;
  86. #endif
  87. if (ret & AV_CPU_FLAG_VSX)
  88. av_assert0(ret & AV_CPU_FLAG_ALTIVEC);
  89. } else if (buf[i] == AT_HWCAP2) {
  90. #ifdef PPC_FEATURE2_ARCH_2_07
  91. if (buf[i + 1] & PPC_FEATURE2_ARCH_2_07)
  92. ret |= AV_CPU_FLAG_POWER8;
  93. #endif
  94. }
  95. }
  96. }
  97. out:
  98. close(fd);
  99. return ret;
  100. #elif CONFIG_RUNTIME_CPUDETECT && defined(__linux__)
  101. #define PVR_G4_7400 0x000C
  102. #define PVR_G5_970 0x0039
  103. #define PVR_G5_970FX 0x003C
  104. #define PVR_G5_970MP 0x0044
  105. #define PVR_G5_970GX 0x0045
  106. #define PVR_POWER6 0x003E
  107. #define PVR_POWER7 0x003F
  108. #define PVR_POWER8 0x004B
  109. #define PVR_CELL_PPU 0x0070
  110. int ret = 0;
  111. int proc_ver;
  112. // Support of mfspr PVR emulation added in Linux 2.6.17.
  113. __asm__ volatile("mfspr %0, 287" : "=r" (proc_ver));
  114. proc_ver >>= 16;
  115. if (proc_ver & 0x8000 ||
  116. proc_ver == PVR_G4_7400 ||
  117. proc_ver == PVR_G5_970 ||
  118. proc_ver == PVR_G5_970FX ||
  119. proc_ver == PVR_G5_970MP ||
  120. proc_ver == PVR_G5_970GX ||
  121. proc_ver == PVR_POWER6 ||
  122. proc_ver == PVR_POWER7 ||
  123. proc_ver == PVR_POWER8 ||
  124. proc_ver == PVR_CELL_PPU)
  125. ret = AV_CPU_FLAG_ALTIVEC;
  126. if (proc_ver == PVR_POWER7 ||
  127. proc_ver == PVR_POWER8)
  128. ret |= AV_CPU_FLAG_VSX;
  129. if (proc_ver == PVR_POWER8)
  130. ret |= AV_CPU_FLAG_POWER8;
  131. return ret;
  132. #else
  133. // Since we were compiled for AltiVec, just assume we have it
  134. // until someone comes up with a proper way (not involving signal hacks).
  135. return AV_CPU_FLAG_ALTIVEC;
  136. #endif /* __AMIGAOS4__ */
  137. #endif /* HAVE_ALTIVEC */
  138. return 0;
  139. }
  140. size_t ff_get_cpu_max_align_ppc(void)
  141. {
  142. int flags = av_get_cpu_flags();
  143. if (flags & (AV_CPU_FLAG_ALTIVEC |
  144. AV_CPU_FLAG_VSX |
  145. AV_CPU_FLAG_POWER8))
  146. return 16;
  147. return 8;
  148. }