thermal.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* Intel digital thermal sensor (see thermal.h).
  2. *
  3. * IA32_THERM_STATUS (MSR 0x19C) reports the core temperature as a delta
  4. * below TjMax in bits 22:16. Bonnell (Atom 230/330) has no
  5. * IA32_TEMPERATURE_TARGET MSR, so TjMax has to come from a table; these are
  6. * the same values Linux's coretemp uses (the 330's DTS is calibrated against
  7. * 125 C, other Bonnell parts against 90 C). Nehalem and Westmere (including
  8. * the X201's Arrandale) publish TjMax in bits 23:16 of MSR 0x1A2, which is
  9. * read for those models; Airmont/Braswell uses coretemp's 90 C table instead
  10. * (an unsupported rdmsr before the IDT exists would triple-fault). The DTS
  11. * "valid" bit is ignored on purpose: coretemp does the same because the delta
  12. * stays meaningful (the bit can be clear at temperatures below the sensor's
  13. * range).
  14. */
  15. #include "thermal.h"
  16. #include "cpu.h"
  17. #include "pci.h"
  18. #include "sched.h"
  19. #include "serial.h"
  20. #include "timer.h"
  21. #define MSR_IA32_THERM_STATUS 0x19C
  22. #define MSR_IA32_TEMPERATURE_TARGET 0x1A2
  23. static int thermal_state = -1; /* -1 unprobed, 0 unsupported, 1 DTS */
  24. static int thermal_amd; /* 1 = AMD D18F3xA4 instead of Intel DTS */
  25. static int thermal_tjmax_c = 100;
  26. static unsigned int thermal_tjmax_raw; /* last MSR 0x1A2 sample */
  27. static int thermal_tjmax_msr; /* TjMax came from the MSR */
  28. /* AMD family 15h models below 60h (Bulldozer/Piledriver/Steamroller) report
  29. * Tctl in D18F3xA4: bits 31:21 are the raw temperature in 0.125 C units and
  30. * bits 17:16 select the -49 C range offset (k10temp's pre-Zen path). */
  31. #define AMD_NB_DEV 0x18
  32. #define AMD_NB_FUNC 3
  33. #define AMD_REPORTED_TEMP 0xA4
  34. #define AMD_TJMAX_C 70
  35. static void cpuid_leaf(unsigned int leaf, unsigned int sub,
  36. unsigned int *a, unsigned int *b, unsigned int *c,
  37. unsigned int *d)
  38. {
  39. __asm__ volatile ("cpuid"
  40. : "=a"(*a), "=b"(*b), "=c"(*c), "=d"(*d)
  41. : "a"(leaf), "c"(sub));
  42. }
  43. static int str_eq(const char *a, const char *b)
  44. {
  45. while (*a && *a == *b) {
  46. a++;
  47. b++;
  48. }
  49. return *a == *b;
  50. }
  51. static int str_has(const char *hay, const char *needle)
  52. {
  53. unsigned int i, j;
  54. for (i = 0; hay[i]; i++) {
  55. for (j = 0; needle[j] && hay[i + j] == needle[j]; j++)
  56. ;
  57. if (!needle[j])
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. /* Model allowlist from coretemp: only parts that really expose a DTS. This
  63. * keeps rdmsr(0x19C) away from CPUs (and emulators) that set CPUID.06H but
  64. * do not implement the MSR, where it would raise #GP and panic. */
  65. static int model_has_dts(unsigned int model)
  66. {
  67. switch (model) {
  68. case 0x0E: /* Core Solo/Duo (Yonah) */
  69. case 0x0F: /* Core 2 */
  70. case 0x16: /* Core 2 */
  71. case 0x17: /* Penryn */
  72. case 0x1A: /* Nehalem */
  73. case 0x1C: /* Atom 230/330 (Diamondville) */
  74. case 0x1D: /* Atom Pineview */
  75. case 0x1E: /* Lynnfield */
  76. case 0x1F: /* Nehalem */
  77. case 0x25: /* Arrandale/Clarkdale (Core i5/i7 M, X201) */
  78. case 0x2C: /* Westmere-EP */
  79. case 0x2E: /* Nehalem-EX */
  80. case 0x2F: /* Westmere-EX */
  81. case 0x4C: /* Airmont: Braswell / Cherry Trail */
  82. case 0x8E: /* Kaby Lake-U (Yoga 720) */
  83. return 1;
  84. default:
  85. return 0;
  86. }
  87. }
  88. /* Models whose TjMax must be read from IA32_TEMPERATURE_TARGET (0x1A2)
  89. * instead of the Bonnell table. Nehalem and later implement the MSR; older
  90. * coretemp-listed parts do not, so a read there could raise #GP. Linux's
  91. * coretemp reads the same MSR and falls back to 100 C when it is missing or
  92. * implausible. */
  93. static int model_has_msr_tjmax(unsigned int model)
  94. {
  95. switch (model) {
  96. case 0x1A:
  97. case 0x1E:
  98. case 0x1F:
  99. case 0x25:
  100. case 0x2C:
  101. case 0x2E:
  102. case 0x2F:
  103. case 0x8E:
  104. return 1;
  105. default:
  106. return 0;
  107. }
  108. }
  109. static unsigned int read_tjmax_msr(void)
  110. {
  111. unsigned int lo, hi;
  112. __asm__ volatile ("rdmsr" : "=a"(lo), "=d"(hi)
  113. : "c"(MSR_IA32_TEMPERATURE_TARGET));
  114. (void)hi;
  115. return (lo >> 16) & 0xFF;
  116. }
  117. static void read_brand(char out[49])
  118. {
  119. unsigned int max_ext, a, b, c, d, i;
  120. for (i = 0; i < 49; i++)
  121. out[i] = '\0';
  122. cpuid_leaf(0x80000000u, 0, &max_ext, &b, &c, &d);
  123. if (max_ext < 0x80000004u)
  124. return;
  125. for (i = 0; i < 3; i++) {
  126. unsigned int *dst = (unsigned int *)(out + i * 16);
  127. cpuid_leaf(0x80000002u + i, 0, &a, &b, &c, &d);
  128. dst[0] = a;
  129. dst[1] = b;
  130. dst[2] = c;
  131. dst[3] = d;
  132. }
  133. }
  134. static int amd_temp_c(void)
  135. {
  136. unsigned int id, val, raw, mc;
  137. id = pci_cfg_read32(0, AMD_NB_DEV, AMD_NB_FUNC, 0x00);
  138. if (id == 0xFFFFFFFFu || id == 0)
  139. return -1;
  140. val = pci_cfg_read32(0, AMD_NB_DEV, AMD_NB_FUNC, AMD_REPORTED_TEMP);
  141. if (val == 0xFFFFFFFFu || val == 0)
  142. return -1;
  143. raw = (val >> 21) & 0x7FFu;
  144. if (raw == 0 || raw == 0x7FFu)
  145. return -1;
  146. mc = raw * 125u;
  147. if ((val & 0x30000u) == 0x30000u) {
  148. if (mc < 49000u)
  149. return -1;
  150. mc -= 49000u;
  151. }
  152. if (mc > 125000u)
  153. return -1;
  154. return (int)((mc + 500u) / 1000u);
  155. }
  156. static int pick_tjmax(unsigned int model, unsigned int stepping,
  157. const char *brand)
  158. {
  159. if (model == 0x1C || model == 0x1D) {
  160. /* The brand string often has double spaces ("CPU 330"), so
  161. * match the number alone. */
  162. if (str_has(brand, "330"))
  163. return 125;
  164. if (str_has(brand, "230"))
  165. return 100;
  166. if (stepping == 10) /* N4xx/N5xx/D4xx/D5xx */
  167. return 100;
  168. return 90;
  169. }
  170. if (model == 0x4C) /* Airmont (coretemp: 90 C) */
  171. return 90;
  172. return 100;
  173. }
  174. void thermal_init(void)
  175. {
  176. unsigned int max_leaf, a, b, c, d;
  177. unsigned int family, model = 0, stepping = 0;
  178. char vendor[13];
  179. char brand[49];
  180. int msr_probed = 0;
  181. thermal_state = 0;
  182. thermal_amd = 0;
  183. thermal_tjmax_c = 100;
  184. if (!cpu_has_cpuid())
  185. goto out;
  186. cpuid_leaf(0, 0, &max_leaf, &b, &c, &d);
  187. ((unsigned int *)vendor)[0] = b;
  188. ((unsigned int *)vendor)[1] = d;
  189. ((unsigned int *)vendor)[2] = c;
  190. vendor[12] = '\0';
  191. if (max_leaf < 1)
  192. goto out;
  193. cpuid_leaf(1, 0, &a, &b, &c, &d);
  194. family = (a >> 8) & 0xF;
  195. model = (a >> 4) & 0xF;
  196. stepping = a & 0xF;
  197. if (family == 0xF)
  198. family += (a >> 20) & 0xFF;
  199. if (family == 0x6 || family == 0xF)
  200. model += ((a >> 16) & 0xF) << 4;
  201. if (str_eq(vendor, "AuthenticAMD")) {
  202. int t;
  203. if (family != 0x15 || model >= 0x60)
  204. goto out;
  205. t = amd_temp_c();
  206. if (t < -20 || t > 125)
  207. goto out;
  208. thermal_tjmax_c = AMD_TJMAX_C;
  209. thermal_amd = 1;
  210. thermal_state = 1;
  211. goto out;
  212. }
  213. if (!str_eq(vendor, "GenuineIntel"))
  214. goto out;
  215. if (!(d & (1u << 5))) /* MSR support */
  216. goto out;
  217. if (family != 0x6 || !model_has_dts(model))
  218. goto out;
  219. if (max_leaf < 6)
  220. goto out;
  221. cpuid_leaf(6, 0, &a, &b, &c, &d);
  222. if (!(a & 1u)) /* DTS capability */
  223. goto out;
  224. read_brand(brand);
  225. thermal_tjmax_c = pick_tjmax(model, stepping, brand);
  226. thermal_tjmax_msr = 0;
  227. thermal_tjmax_raw = 0;
  228. if (model_has_msr_tjmax(model)) {
  229. unsigned int target = read_tjmax_msr();
  230. thermal_tjmax_raw = target;
  231. msr_probed = 1;
  232. if (target >= 80 && target <= 125) {
  233. thermal_tjmax_c = (int)target;
  234. thermal_tjmax_msr = 1;
  235. }
  236. }
  237. thermal_state = 1;
  238. out:
  239. if (thermal_state && thermal_amd) {
  240. serial_puts(SERIAL_COM1,
  241. "thermal: AMD family 15h, D18F3xA4, TjMax ");
  242. serial_putdec(SERIAL_COM1, (unsigned int)thermal_tjmax_c);
  243. serial_puts(SERIAL_COM1, " C\n");
  244. } else if (thermal_state) {
  245. serial_puts(SERIAL_COM1, "thermal: Intel DTS, model ");
  246. serial_putdec(SERIAL_COM1, model);
  247. serial_puts(SERIAL_COM1, " stepping ");
  248. serial_putdec(SERIAL_COM1, stepping);
  249. serial_puts(SERIAL_COM1, ", TjMax ");
  250. serial_putdec(SERIAL_COM1, (unsigned int)thermal_tjmax_c);
  251. serial_puts(SERIAL_COM1, " C");
  252. if (msr_probed) {
  253. serial_puts(SERIAL_COM1, " (msr 0x1A2=");
  254. serial_puthex(SERIAL_COM1, thermal_tjmax_raw);
  255. serial_puts(SERIAL_COM1,
  256. thermal_tjmax_msr ? ")" : ", out of range)");
  257. } else {
  258. serial_puts(SERIAL_COM1, " (table)");
  259. }
  260. serial_puts(SERIAL_COM1, "\n");
  261. } else {
  262. serial_puts(SERIAL_COM1,
  263. "thermal: no supported temperature sensor, disabled\n");
  264. }
  265. }
  266. int thermal_read_this_cpu(void)
  267. {
  268. unsigned int raw;
  269. int t;
  270. if (thermal_state < 0)
  271. thermal_init();
  272. if (!thermal_state)
  273. return -1;
  274. if (thermal_amd)
  275. return amd_temp_c();
  276. if (thermal_status_this_cpu(&raw) != 0)
  277. return -1;
  278. t = thermal_tjmax_c - (int)((raw >> 16) & 0x7F);
  279. if (t < -20 || t > 150)
  280. return -1;
  281. return t;
  282. }
  283. int thermal_status_this_cpu(unsigned int *status)
  284. {
  285. unsigned int lo, hi;
  286. if (thermal_state < 0)
  287. thermal_init();
  288. if (!thermal_state || thermal_amd)
  289. return -1;
  290. __asm__ volatile ("rdmsr" : "=a"(lo), "=d"(hi)
  291. : "c"(MSR_IA32_THERM_STATUS));
  292. (void)hi;
  293. if (status)
  294. *status = lo;
  295. return 0;
  296. }
  297. void thermal_tick(void)
  298. {
  299. struct cpu *c = this_cpu();
  300. unsigned int ms = (c->id == 0) ? timer_tick_ms() : c->tick_ms;
  301. if (thermal_state < 0)
  302. thermal_init();
  303. if (!thermal_state || thermal_amd)
  304. return;
  305. /* The timer period drops to 10 ms while a CPU is idle, so count
  306. * milliseconds rather than ticks to keep the 1 Hz sample rate. */
  307. if ((c->temp_div += ms) < 1000)
  308. return;
  309. c->temp_div = 0;
  310. c->temp_c = thermal_read_this_cpu();
  311. }
  312. void thermal_housekeeping(void)
  313. {
  314. static unsigned int last_ms;
  315. unsigned int now, i;
  316. int t;
  317. if (thermal_state < 0)
  318. thermal_init();
  319. if (!thermal_state || !thermal_amd)
  320. return;
  321. now = get_ticks_ms();
  322. if (last_ms && now - last_ms < 1000)
  323. return;
  324. last_ms = now;
  325. /* D18F3xA4 is a package/northbridge value, not per core. Publish it
  326. * to every CPU so thermal_max_c() and the top bar see it. */
  327. t = amd_temp_c();
  328. for (i = 0; i < cpu_count && i < MAX_CPUS; i++)
  329. cpus[i].temp_c = t;
  330. }
  331. int thermal_max_c(void)
  332. {
  333. int max = -1;
  334. unsigned int i;
  335. if (thermal_state < 0)
  336. thermal_init();
  337. if (!thermal_state)
  338. return -1;
  339. for (i = 0; i < cpu_count && i < MAX_CPUS; i++)
  340. if (cpus[i].temp_c > max)
  341. max = cpus[i].temp_c;
  342. /* Before the first tick has published a value (or while a CPU is
  343. * still booting), read the CPU running the caller instead of showing
  344. * nothing for the first second. */
  345. if (max < 0)
  346. max = thermal_read_this_cpu();
  347. return max;
  348. }
  349. int thermal_tjmax(void)
  350. {
  351. if (thermal_state < 0)
  352. thermal_init();
  353. return thermal_state ? thermal_tjmax_c : 0;
  354. }