cpuid.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2013 Ahmed Samy <f.fallen45@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef CCAN_CPUID_H
  23. #define CCAN_CPUID_H
  24. /**
  25. * enum cpuid - stuff to get information on from the CPU.
  26. *
  27. * This is used as a parameter in cpuid().
  28. *
  29. * CPU_VENDORID:
  30. * The CPU's Vendor ID.
  31. *
  32. * CPU_PROCINFO_AND_FEATUREBITS:
  33. * Processor information and feature bits (SSE, etc.).
  34. *
  35. * CPU_CACHE_AND_TLBD_INFO
  36. * Cache and TLBD Information.
  37. *
  38. * CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
  39. * Highest extended function supported address.
  40. * Can be like 0x80000008.
  41. *
  42. * CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
  43. * Extended processor information and feature bits (64bit etc.)
  44. *
  45. * CPU_PROC_BRAND_STRING:
  46. * The Processor's brand string.
  47. *
  48. * CPU_L1_CACHE_AND_TLB_IDS:
  49. * L1 Cache and TLB Identifications.
  50. *
  51. * CPU_EXTENDED_L2_CACHE_FEATURES:
  52. * Extended L2 Cache features.
  53. *
  54. * CPU_ADV_POWER_MGT_INFO:
  55. * Advaned power management information.
  56. *
  57. * CPU_VIRT_PHYS_ADDR_SIZES:
  58. * Virtual and physical address sizes.
  59. */
  60. typedef enum cpuid {
  61. CPU_VENDORID = 0,
  62. CPU_PROCINFO_AND_FEATUREBITS = 1,
  63. CPU_CACHE_AND_TLBD_INFO = 2,
  64. CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED = 0x80000000,
  65. CPU_EXTENDED_PROC_INFO_FEATURE_BITS = 0x80000001,
  66. CPU_PROC_BRAND_STRING = 0x80000002,
  67. CPU_L1_CACHE_AND_TLB_IDS = 0x80000005,
  68. CPU_EXTENDED_L2_CACHE_FEATURES = 0x80000006,
  69. CPU_ADV_POWER_MGT_INFO = 0x80000007,
  70. CPU_VIRT_PHYS_ADDR_SIZES = 0x80000008
  71. } cpuid_t;
  72. /**
  73. * enum cpufeature
  74. *
  75. * This is used by cpuid_has_feature().
  76. *
  77. * CF_MMX:
  78. * Test for MMX support.
  79. *
  80. * CF_SSE*:
  81. * Test for SSE* support.
  82. *
  83. * CF_AVX:
  84. * Test for AVX support.
  85. *
  86. * CF_FMA:
  87. * Test for FMA support.
  88. */
  89. typedef enum cpufeature {
  90. CF_MMX = 1 << 23,
  91. CF_SSE = 1 << 25,
  92. CF_SSE2 = 1 << 26,
  93. CF_SSE3 = 1 << 0,
  94. CF_SSSE3 = 1 << 9,
  95. CF_SSE41 = 1 << 19,
  96. CF_SSE42 = 1 << 20,
  97. CF_AVX = 1 << 28,
  98. CF_FMA = 1 << 12
  99. } cpufeature_t;
  100. /**
  101. * enum cpuextfeature - Test for an extended feature provided by the CPU
  102. *
  103. * This is used by cpuid_has_ext_feature()
  104. *
  105. * CEF_x64:
  106. * Test for 64-bits.
  107. *
  108. * CEF_SSE4a:
  109. * CEF_FMA4:
  110. * CEF_XOP:
  111. * Test for SSE4a/FMA4/XOP
  112. */
  113. typedef enum cpuextfeature {
  114. CEF_x64 = 1 << 29,
  115. CEF_SSE4a = 1 << 6,
  116. CEF_FMA4 = 1 << 16,
  117. CEF_XOP = 1 << 11
  118. } cpuextfeature_t;
  119. /**
  120. * cpuid_is_supported - test if the CPUID instruction is supported
  121. *
  122. * CPUID is not supported by old CPUS.
  123. *
  124. * Returns 1 if the cpuid instruction is supported, 0 otherwise.
  125. *
  126. * See also: cpuid()
  127. */
  128. int cpuid_is_supported(void);
  129. /**
  130. * highest_ext_func_supported - Get the highest extended function supported
  131. *
  132. *
  133. * Returns the highest extended function supported.
  134. *
  135. * This is the same as calling:
  136. * cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
  137. *
  138. * This is made visible to the linker because it's easier to call it
  139. * instead of calling cpuid with less type-checking. cpuid calls this.
  140. *
  141. * See also: cpuid()
  142. */
  143. int highest_ext_func_supported(void);
  144. /**
  145. * cpuid - Get Some information from the CPU.
  146. *
  147. * This function expects buf to be a valid pointer to a string/int/...
  148. * depending on the requested information.
  149. *
  150. * For CPU_VENDOR_ID:
  151. * Returns a string into buf.
  152. *
  153. * For CPU_PROCINFO_AND_FEATUREBITS:
  154. * buf[0]:
  155. * - 3:0 - Stepping
  156. * - 7:4 - Model
  157. * - 11:8 - Family
  158. * - 13:12 - Processor Type
  159. * - 19:16 - Extended Model
  160. * - 27:20 - Extended family
  161. * buf[1] and buf[2]:
  162. * Feature flags
  163. * buf[3]:
  164. * Additional feature information.
  165. *
  166. * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
  167. * Returns the highest supported function in *buf (expects an integer ofc)
  168. *
  169. * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
  170. * Returns them in buf[0] and buf[1].
  171. *
  172. * For CPU_VIRT_PHYS_ADDR_SIZES:
  173. * Returns it as an integer in *buf.
  174. *
  175. * For CPU_PROC_BRAND_STRING:
  176. * Have a char array with at least 48 bytes assigned to it.
  177. *
  178. * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
  179. */
  180. void cpuid(cpuid_t info, void *buf);
  181. /**
  182. * cpuid_test_feature - Test if @feature is available
  183. *
  184. * Returns 1 if feature is supported, 0 otherwise.
  185. *
  186. * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
  187. * and <= CPU_VIRT_PHYS_ADDR_SIZES.
  188. */
  189. int cpuid_test_feature(cpuid_t feature);
  190. /**
  191. * cpuid_has_feature - Test if @feature is supported
  192. *
  193. * Test if the CPU supports MMX/SSE* etc
  194. *
  195. * Returns 1 if the feature is available, 0 otherwise.
  196. */
  197. #define cpuid_has_mmx() cpuid_has_feature(CF_MMX)
  198. #define cpuid_has_sse() cpuid_has_feature(CF_SSE)
  199. #define cpuid_has_sse2() cpuid_has_feature(CF_SSE2)
  200. #define cpuid_has_sse3() cpuid_has_feature(CF_SSE3)
  201. #define cpuid_has_ssse3() cpuid_has_feature(CF_SSSE3)
  202. #define cpuid_has_sse41() cpuid_has_feature(CF_SSE41)
  203. #define cpuid_has_sse42() cpuid_has_feature(CF_SSE42)
  204. #define cpuid_has_avx() cpuid_has_feature(CF_AVX)
  205. #define cpuid_has_fma() cpuid_has_feature(CF_FMA)
  206. int cpuid_has_feature(cpufeature_t feature);
  207. /**
  208. * cpuid_has_ext_feature - Test if @extfeature is an extended feature
  209. * that's supported by the CPU.
  210. *
  211. * Test if the CPU supports this extended feature.
  212. *
  213. * Returns 1 if available, 0 otherwise.
  214. */
  215. #define cpuid_has_x64() cpuid_has_ext_feature(CEF_x64)
  216. #define cpuid_has_sse4a() cpuid_has_ext_feature(CEF_SSE4a)
  217. #define cpuid_has_fma4() cpuid_has_ext_feature(CEF_FMA4)
  218. #define cpuid_has_xop() cpuid_has_ext_feature(CEF_XOP)
  219. int cpuid_has_ext_feature(cpuextfeature_t extfeature);
  220. #endif