cpuid.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include <stdbool.h>
  25. #include <stdint.h>
  26. /**
  27. * enum cpuid - stuff to get information on from the CPU.
  28. *
  29. * This is used as a parameter in cpuid().
  30. *
  31. * CPU_VENDORID:
  32. * The CPU's Vendor ID.
  33. *
  34. * CPU_PROCINFO_AND_FEATUREBITS:
  35. * Processor information and feature bits (SSE, etc.).
  36. *
  37. * CPU_CACHE_AND_TLBD_INFO
  38. * Cache and TLBD Information.
  39. *
  40. * CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
  41. * Highest extended function supported address.
  42. * Can be like 0x80000008.
  43. *
  44. * CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
  45. * Extended processor information and feature bits (64bit etc.)
  46. *
  47. * CPU_PROC_BRAND_STRING:
  48. * The Processor's brand string.
  49. *
  50. * CPU_L1_CACHE_AND_TLB_IDS:
  51. * L1 Cache and TLB Identifications.
  52. *
  53. * CPU_EXTENDED_L2_CACHE_FEATURES:
  54. * Extended L2 Cache features.
  55. *
  56. * CPU_ADV_POWER_MGT_INFO:
  57. * Advaned power management information.
  58. *
  59. * CPU_VIRT_PHYS_ADDR_SIZES:
  60. * Virtual and physical address sizes.
  61. */
  62. typedef enum cpuid {
  63. CPU_VENDORID = 0,
  64. CPU_PROCINFO_AND_FEATUREBITS = 1,
  65. CPU_CACHE_AND_TLBD_INFO = 2,
  66. CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED = 0x80000000,
  67. CPU_EXTENDED_PROC_INFO_FEATURE_BITS = 0x80000001,
  68. CPU_PROC_BRAND_STRING = 0x80000002,
  69. CPU_L1_CACHE_AND_TLB_IDS = 0x80000005,
  70. CPU_EXTENDED_L2_CACHE_FEATURES = 0x80000006,
  71. CPU_ADV_POWER_MGT_INFO = 0x80000007,
  72. CPU_VIRT_PHYS_ADDR_SIZES = 0x80000008
  73. } cpuid_t;
  74. #define CF_MMX 0
  75. #define CF_SSE 1
  76. #define CF_SSE2 2
  77. #define CF_SSE3 3
  78. #define CF_FPU 4
  79. #define CF_TSC 5
  80. #define CF_MSR 6
  81. #define CF_SSSE3 7
  82. #define CF_AVX 8
  83. #define CF_FMA 9
  84. #define CEF_x64 10
  85. #define CEF_FPU 11
  86. #define CEF_DE 12
  87. #define CEF_SYSCALLRET 13
  88. #define CEF_CMOV 14
  89. #define CEF_SSE4a 15
  90. #define CEF_FMA4 16
  91. #define CEF_XOP 17
  92. typedef enum cputype {
  93. CT_NONE,
  94. CT_AMDK5,
  95. CT_AMD,
  96. CT_CENTAUR,
  97. CT_CYRIX,
  98. CT_INTEL,
  99. CT_TRANSMETA,
  100. CT_NATIONAL_SEMICONDUCTOR,
  101. CT_NEXGEN,
  102. CT_RISE,
  103. CT_SIS,
  104. CT_UMC,
  105. CT_VIA,
  106. CT_VORTEX,
  107. CT_KVM
  108. } cputype_t;
  109. #if defined(__i386__) || defined(__i386) || defined(__x86_64) \
  110. || defined(_M_AMD64) || defined(__M_X64)
  111. /**
  112. * cpuid_get_cpu_type - Get CPU Type
  113. *
  114. * Returns the CPU Type as cputype_t.
  115. *
  116. * See also: cpuid_get_cpu_type_string()
  117. */
  118. cputype_t cpuid_get_cpu_type(void);
  119. /**
  120. * cpuid_get_cpu_type_string - Get CPU Type string
  121. *
  122. * Returns the CPU type string based off cputype_t.
  123. */
  124. const char *cpuid_get_cpu_type_string(const cputype_t cputype);
  125. /**
  126. * cpuid_is_supported - test if the CPUID instruction is supported
  127. *
  128. * CPUID is not supported by old CPUS.
  129. *
  130. * Returns true if the cpuid instruction is supported, false otherwise.
  131. *
  132. * See also: cpuid()
  133. */
  134. bool cpuid_is_supported(void);
  135. /**
  136. * cpuid_highest_ext_func_supported - Get the highest extended function supported
  137. *
  138. *
  139. * Returns the highest extended function supported.
  140. *
  141. * This is the same as calling:
  142. * cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
  143. *
  144. * This is made visible to the linker because it's easier to call it
  145. * instead of calling cpuid with less type-checking. cpuid calls this.
  146. *
  147. * See also: cpuid()
  148. */
  149. uint32_t cpuid_highest_ext_func_supported(void);
  150. /**
  151. * cpuid - Get Some information from the CPU.
  152. *
  153. * This function expects buf to be a valid pointer to a string/int/...
  154. * depending on the requested information.
  155. *
  156. * For CPU_VENDOR_ID:
  157. * Returns a string into buf.
  158. *
  159. * For CPU_PROCINFO_AND_FEATUREBITS:
  160. * buf[0]:
  161. * - 3:0 - Stepping
  162. * - 7:4 - Model
  163. * - 11:8 - Family
  164. * - 13:12 - Processor Type
  165. * - 19:16 - Extended Model
  166. * - 27:20 - Extended family
  167. * buf[1] and buf[2]:
  168. * Feature flags
  169. * buf[3]:
  170. * Additional feature information.
  171. *
  172. * For CPU_L1_CACHE_AND_TLB_IDS:
  173. * buf[0]: (eax):
  174. * - 7..0 Number of times to exec cpuid to get all descriptors.
  175. * - 15..8 Instruction TLB: 4K Pages, 4-way set associtive, 128 entries.
  176. * - 23..16 Data TLB: 4k Pages, 4-way set associtive, 128 entries.
  177. * - 24..31 Instruction TLB: 4K Pages, 4-way set associtive, 2 entries.
  178. * buf[1]: (ebx):
  179. * - 7..0 64-byte prefetching
  180. * - 8..31 Null descriptor
  181. * buf[2]: (ecx):
  182. * - 0..31 Null descriptor
  183. * buf[3]: (edx):
  184. * - 7..0 2nd-level cache, 2M, 8-way set associtive, 64-byte line size
  185. * - 15..8 1st-level instruction cache: 32K, 8-way set associtive, 64 byte line size
  186. * - 16..23 Data TLB: 4M Pages, 4-way set associtive, 8 entires.
  187. * - 24..31 1st-level data cache: 32K, 8-way set associtive, 64 byte line size
  188. *
  189. * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
  190. * Returns the highest supported function in *buf (expects an integer ofc)
  191. *
  192. * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
  193. * Returns them in buf[0] and buf[1].
  194. *
  195. * For CPU_VIRT_PHYS_ADDR_SIZES:
  196. * Returns it as an integer in *buf.
  197. *
  198. * For CPU_PROC_BRAND_STRING:
  199. * Have a char array with at least 48 bytes assigned to it.
  200. *
  201. * Here's a page which will help you parse the data provided by this function.
  202. * http://www.flounder.com/cpuid_explorer2.htm
  203. *
  204. * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
  205. */
  206. void cpuid(cpuid_t info, uint32_t *buf);
  207. /**
  208. * cpuid_test_feature - Test if @feature is available
  209. *
  210. * Returns true if feature is supported, false otherwise.
  211. *
  212. * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
  213. * and <= CPU_VIRT_PHYS_ADDR_SIZES.
  214. */
  215. bool cpuid_test_feature(cpuid_t feature);
  216. /**
  217. * cpuid_has_feature - Test if @feature is supported
  218. *
  219. * Test if the CPU supports MMX/SSE* etc.
  220. * For the extended parameter, usually you want to pass it as
  221. * false if you're not passing CEF_*.
  222. *
  223. * For more information about the CPU extended features, have a look
  224. * at:
  225. * http://en.wikipedia.org/wiki/CPUID
  226. *
  227. * Returns true if the feature is available, false otherwise.
  228. */
  229. #define cpuid_has_mmx() cpuid_has_feature(CF_MMX, false)
  230. #define cpuid_has_sse() cpuid_has_feature(CF_SSE, false)
  231. #define cpuid_has_sse2() cpuid_has_feature(CF_SSE2, false)
  232. #define cpuid_has_sse3() cpuid_has_feature(CF_SSE3, false)
  233. #define cpuid_has_ssse3() cpuid_has_feature(CF_SSSE3, false)
  234. #define cpuid_has_avx() cpuid_has_feature(CF_AVX, false)
  235. #define cpuid_has_fma() cpuid_has_feature(CF_FMA, false)
  236. #define cpuid_has_x64() cpuid_has_feature(CEF_x64, true)
  237. #define cpuid_has_sse4a() cpuid_has_feature(CEF_SSE4a, true)
  238. #define cpuid_has_fma4() cpuid_has_feature(CEF_FMA4, true)
  239. #define cpuid_has_xop() cpuid_has_feature(CEF_XOP, true)
  240. bool cpuid_has_feature(int feature, bool extended);
  241. #else
  242. #include <ccan/build_assert/build_assert.h>
  243. #define cpuid_get_cpu_type() BUILD_ASSERT_OR_ZERO(0)
  244. #define cpuid_get_cpu_type_string() BUILD_ASSERT_OR_ZERO(0)
  245. #define cpuid_is_supported() BUILD_ASSERT_OR_ZERO(0)
  246. #define cpuid(info, buf) BUILD_ASSERT_OR_ZERO(0)
  247. #define cpuid_highest_ext_func_supported() BUILD_ASSERT_OR_ZERO(0)
  248. #define cpuid_test_feature(feature) BUILD_ASSERT_OR_ZERO(0)
  249. #define cpuid_has_feature(feature, ext) BUILD_ASSERT_OR_ZERO(0)
  250. #endif
  251. #endif