hwmon-vid.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * hwmon-vid.c - VID/VRM/VRD voltage conversions
  4. *
  5. * Copyright (c) 2004 Rudolf Marek <r.marek@assembler.cz>
  6. *
  7. * Partly imported from i2c-vid.h of the lm_sensors project
  8. * Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
  9. * With assistance from Trent Piepho <xyzzy@speakeasy.org>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/hwmon-vid.h>
  15. #ifdef CONFIG_X86
  16. #include <asm/msr.h>
  17. #endif
  18. /*
  19. * Common code for decoding VID pins.
  20. *
  21. * References:
  22. *
  23. * For VRM 8.4 to 9.1, "VRM x.y DC-DC Converter Design Guidelines",
  24. * available at http://developer.intel.com/.
  25. *
  26. * For VRD 10.0 and up, "VRD x.y Design Guide",
  27. * available at http://developer.intel.com/.
  28. *
  29. * AMD Athlon 64 and AMD Opteron Processors, AMD Publication 26094,
  30. * http://support.amd.com/us/Processor_TechDocs/26094.PDF
  31. * Table 74. VID Code Voltages
  32. * This corresponds to an arbitrary VRM code of 24 in the functions below.
  33. * These CPU models (K8 revision <= E) have 5 VID pins. See also:
  34. * Revision Guide for AMD Athlon 64 and AMD Opteron Processors, AMD Publication 25759,
  35. * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25759.pdf
  36. *
  37. * AMD NPT Family 0Fh Processors, AMD Publication 32559,
  38. * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/32559.pdf
  39. * Table 71. VID Code Voltages
  40. * This corresponds to an arbitrary VRM code of 25 in the functions below.
  41. * These CPU models (K8 revision >= F) have 6 VID pins. See also:
  42. * Revision Guide for AMD NPT Family 0Fh Processors, AMD Publication 33610,
  43. * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/33610.pdf
  44. *
  45. * The 17 specification is in fact Intel Mobile Voltage Positioning -
  46. * (IMVP-II). You can find more information in the datasheet of Max1718
  47. * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2452
  48. *
  49. * The 13 specification corresponds to the Intel Pentium M series. There
  50. * doesn't seem to be any named specification for these. The conversion
  51. * tables are detailed directly in the various Pentium M datasheets:
  52. * https://www.intel.com/design/intarch/pentiumm/docs_pentiumm.htm
  53. *
  54. * The 14 specification corresponds to Intel Core series. There
  55. * doesn't seem to be any named specification for these. The conversion
  56. * tables are detailed directly in the various Pentium Core datasheets:
  57. * https://www.intel.com/design/mobile/datashts/309221.htm
  58. *
  59. * The 110 (VRM 11) specification corresponds to Intel Conroe based series.
  60. * https://www.intel.com/design/processor/applnots/313214.htm
  61. */
  62. /*
  63. * vrm is the VRM/VRD document version multiplied by 10.
  64. * val is the 4-bit or more VID code.
  65. * Returned value is in mV to avoid floating point in the kernel.
  66. * Some VID have some bits in uV scale, this is rounded to mV.
  67. */
  68. int vid_from_reg(int val, u8 vrm)
  69. {
  70. int vid;
  71. switch (vrm) {
  72. case 100: /* VRD 10.0 */
  73. /* compute in uV, round to mV */
  74. val &= 0x3f;
  75. if ((val & 0x1f) == 0x1f)
  76. return 0;
  77. if ((val & 0x1f) <= 0x09 || val == 0x0a)
  78. vid = 1087500 - (val & 0x1f) * 25000;
  79. else
  80. vid = 1862500 - (val & 0x1f) * 25000;
  81. if (val & 0x20)
  82. vid -= 12500;
  83. return (vid + 500) / 1000;
  84. case 110: /* Intel Conroe */
  85. /* compute in uV, round to mV */
  86. val &= 0xff;
  87. if (val < 0x02 || val > 0xb2)
  88. return 0;
  89. return (1600000 - (val - 2) * 6250 + 500) / 1000;
  90. case 24: /* Athlon64 & Opteron */
  91. val &= 0x1f;
  92. if (val == 0x1f)
  93. return 0;
  94. fallthrough;
  95. case 25: /* AMD NPT 0Fh */
  96. val &= 0x3f;
  97. return (val < 32) ? 1550 - 25 * val
  98. : 775 - (25 * (val - 31)) / 2;
  99. case 26: /* AMD family 10h to 15h, serial VID */
  100. val &= 0x7f;
  101. if (val >= 0x7c)
  102. return 0;
  103. return DIV_ROUND_CLOSEST(15500 - 125 * val, 10);
  104. case 91: /* VRM 9.1 */
  105. case 90: /* VRM 9.0 */
  106. val &= 0x1f;
  107. return val == 0x1f ? 0 :
  108. 1850 - val * 25;
  109. case 85: /* VRM 8.5 */
  110. val &= 0x1f;
  111. return (val & 0x10 ? 25 : 0) +
  112. ((val & 0x0f) > 0x04 ? 2050 : 1250) -
  113. ((val & 0x0f) * 50);
  114. case 84: /* VRM 8.4 */
  115. val &= 0x0f;
  116. fallthrough;
  117. case 82: /* VRM 8.2 */
  118. val &= 0x1f;
  119. return val == 0x1f ? 0 :
  120. val & 0x10 ? 5100 - (val) * 100 :
  121. 2050 - (val) * 50;
  122. case 17: /* Intel IMVP-II */
  123. val &= 0x1f;
  124. return val & 0x10 ? 975 - (val & 0xF) * 25 :
  125. 1750 - val * 50;
  126. case 13:
  127. case 131:
  128. val &= 0x3f;
  129. /* Exception for Eden ULV 500 MHz */
  130. if (vrm == 131 && val == 0x3f)
  131. val++;
  132. return 1708 - val * 16;
  133. case 14: /* Intel Core */
  134. /* compute in uV, round to mV */
  135. val &= 0x7f;
  136. return val > 0x77 ? 0 : (1500000 - (val * 12500) + 500) / 1000;
  137. default: /* report 0 for unknown */
  138. if (vrm)
  139. pr_warn("Requested unsupported VRM version (%u)\n",
  140. (unsigned int)vrm);
  141. return 0;
  142. }
  143. }
  144. EXPORT_SYMBOL(vid_from_reg);
  145. /*
  146. * After this point is the code to automatically determine which
  147. * VRM/VRD specification should be used depending on the CPU.
  148. */
  149. struct vrm_model {
  150. u8 vendor;
  151. u8 family;
  152. u8 model_from;
  153. u8 model_to;
  154. u8 stepping_to;
  155. u8 vrm_type;
  156. };
  157. #define ANY 0xFF
  158. #ifdef CONFIG_X86
  159. /*
  160. * The stepping_to parameter is highest acceptable stepping for current line.
  161. * The model match must be exact for 4-bit values. For model values 0x10
  162. * and above (extended model), all models below the parameter will match.
  163. */
  164. static struct vrm_model vrm_models[] = {
  165. {X86_VENDOR_AMD, 0x6, 0x0, ANY, ANY, 90}, /* Athlon Duron etc */
  166. {X86_VENDOR_AMD, 0xF, 0x0, 0x3F, ANY, 24}, /* Athlon 64, Opteron */
  167. /*
  168. * In theory, all NPT family 0Fh processors have 6 VID pins and should
  169. * thus use vrm 25, however in practice not all mainboards route the
  170. * 6th VID pin because it is never needed. So we use the 5 VID pin
  171. * variant (vrm 24) for the models which exist today.
  172. */
  173. {X86_VENDOR_AMD, 0xF, 0x40, 0x7F, ANY, 24}, /* NPT family 0Fh */
  174. {X86_VENDOR_AMD, 0xF, 0x80, ANY, ANY, 25}, /* future fam. 0Fh */
  175. {X86_VENDOR_AMD, 0x10, 0x0, ANY, ANY, 25}, /* NPT family 10h */
  176. {X86_VENDOR_AMD, 0x11, 0x0, ANY, ANY, 26}, /* family 11h */
  177. {X86_VENDOR_AMD, 0x12, 0x0, ANY, ANY, 26}, /* family 12h */
  178. {X86_VENDOR_AMD, 0x14, 0x0, ANY, ANY, 26}, /* family 14h */
  179. {X86_VENDOR_AMD, 0x15, 0x0, ANY, ANY, 26}, /* family 15h */
  180. {X86_VENDOR_INTEL, 0x6, 0x0, 0x6, ANY, 82}, /* Pentium Pro,
  181. * Pentium II, Xeon,
  182. * Mobile Pentium,
  183. * Celeron */
  184. {X86_VENDOR_INTEL, 0x6, 0x7, 0x7, ANY, 84}, /* Pentium III, Xeon */
  185. {X86_VENDOR_INTEL, 0x6, 0x8, 0x8, ANY, 82}, /* Pentium III, Xeon */
  186. {X86_VENDOR_INTEL, 0x6, 0x9, 0x9, ANY, 13}, /* Pentium M (130 nm) */
  187. {X86_VENDOR_INTEL, 0x6, 0xA, 0xA, ANY, 82}, /* Pentium III Xeon */
  188. {X86_VENDOR_INTEL, 0x6, 0xB, 0xB, ANY, 85}, /* Tualatin */
  189. {X86_VENDOR_INTEL, 0x6, 0xD, 0xD, ANY, 13}, /* Pentium M (90 nm) */
  190. {X86_VENDOR_INTEL, 0x6, 0xE, 0xE, ANY, 14}, /* Intel Core (65 nm) */
  191. {X86_VENDOR_INTEL, 0x6, 0xF, ANY, ANY, 110}, /* Intel Conroe and
  192. * later */
  193. {X86_VENDOR_INTEL, 0xF, 0x0, 0x0, ANY, 90}, /* P4 */
  194. {X86_VENDOR_INTEL, 0xF, 0x1, 0x1, ANY, 90}, /* P4 Willamette */
  195. {X86_VENDOR_INTEL, 0xF, 0x2, 0x2, ANY, 90}, /* P4 Northwood */
  196. {X86_VENDOR_INTEL, 0xF, 0x3, ANY, ANY, 100}, /* Prescott and above
  197. * assume VRD 10 */
  198. {X86_VENDOR_CENTAUR, 0x6, 0x7, 0x7, ANY, 85}, /* Eden ESP/Ezra */
  199. {X86_VENDOR_CENTAUR, 0x6, 0x8, 0x8, 0x7, 85}, /* Ezra T */
  200. {X86_VENDOR_CENTAUR, 0x6, 0x9, 0x9, 0x7, 85}, /* Nehemiah */
  201. {X86_VENDOR_CENTAUR, 0x6, 0x9, 0x9, ANY, 17}, /* C3-M, Eden-N */
  202. {X86_VENDOR_CENTAUR, 0x6, 0xA, 0xA, 0x7, 0}, /* No information */
  203. {X86_VENDOR_CENTAUR, 0x6, 0xA, 0xA, ANY, 13}, /* C7-M, C7,
  204. * Eden (Esther) */
  205. {X86_VENDOR_CENTAUR, 0x6, 0xD, 0xD, ANY, 134}, /* C7-D, C7-M, C7,
  206. * Eden (Esther) */
  207. };
  208. /*
  209. * Special case for VIA model D: there are two different possible
  210. * VID tables, so we have to figure out first, which one must be
  211. * used. This resolves temporary drm value 134 to 14 (Intel Core
  212. * 7-bit VID), 13 (Pentium M 6-bit VID) or 131 (Pentium M 6-bit VID
  213. * + quirk for Eden ULV 500 MHz).
  214. * Note: something similar might be needed for model A, I'm not sure.
  215. */
  216. static u8 get_via_model_d_vrm(void)
  217. {
  218. unsigned int vid, brand, __maybe_unused dummy;
  219. static const char *brands[4] = {
  220. "C7-M", "C7", "Eden", "C7-D"
  221. };
  222. rdmsr(0x198, dummy, vid);
  223. vid &= 0xff;
  224. rdmsr(0x1154, brand, dummy);
  225. brand = ((brand >> 4) ^ (brand >> 2)) & 0x03;
  226. if (vid > 0x3f) {
  227. pr_info("Using %d-bit VID table for VIA %s CPU\n",
  228. 7, brands[brand]);
  229. return 14;
  230. } else {
  231. pr_info("Using %d-bit VID table for VIA %s CPU\n",
  232. 6, brands[brand]);
  233. /* Enable quirk for Eden */
  234. return brand == 2 ? 131 : 13;
  235. }
  236. }
  237. static u8 find_vrm(u8 family, u8 model, u8 stepping, u8 vendor)
  238. {
  239. int i;
  240. for (i = 0; i < ARRAY_SIZE(vrm_models); i++) {
  241. if (vendor == vrm_models[i].vendor &&
  242. family == vrm_models[i].family &&
  243. model >= vrm_models[i].model_from &&
  244. model <= vrm_models[i].model_to &&
  245. stepping <= vrm_models[i].stepping_to)
  246. return vrm_models[i].vrm_type;
  247. }
  248. return 0;
  249. }
  250. u8 vid_which_vrm(void)
  251. {
  252. struct cpuinfo_x86 *c = &cpu_data(0);
  253. u8 vrm_ret;
  254. if (c->x86 < 6) /* Any CPU with family lower than 6 */
  255. return 0; /* doesn't have VID */
  256. vrm_ret = find_vrm(c->x86, c->x86_model, c->x86_stepping, c->x86_vendor);
  257. if (vrm_ret == 134)
  258. vrm_ret = get_via_model_d_vrm();
  259. if (vrm_ret == 0)
  260. pr_info("Unknown VRM version of your x86 CPU\n");
  261. return vrm_ret;
  262. }
  263. /* and now for something completely different for the non-x86 world */
  264. #else
  265. u8 vid_which_vrm(void)
  266. {
  267. pr_info("Unknown VRM version of your CPU\n");
  268. return 0;
  269. }
  270. #endif
  271. EXPORT_SYMBOL(vid_which_vrm);
  272. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  273. MODULE_DESCRIPTION("hwmon-vid driver");
  274. MODULE_LICENSE("GPL");