thermal.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. return 1;
  83. default:
  84. return 0;
  85. }
  86. }
  87. /* Models whose TjMax must be read from IA32_TEMPERATURE_TARGET (0x1A2)
  88. * instead of the Bonnell table. Nehalem and later implement the MSR; older
  89. * coretemp-listed parts do not, so a read there could raise #GP. Linux's
  90. * coretemp reads the same MSR and falls back to 100 C when it is missing or
  91. * implausible. */
  92. static int model_has_msr_tjmax(unsigned int model)
  93. {
  94. switch (model) {
  95. case 0x1A:
  96. case 0x1E:
  97. case 0x1F:
  98. case 0x25:
  99. case 0x2C:
  100. case 0x2E:
  101. case 0x2F:
  102. return 1;
  103. default:
  104. return 0;
  105. }
  106. }
  107. static unsigned int read_tjmax_msr(void)
  108. {
  109. unsigned int lo, hi;
  110. __asm__ volatile ("rdmsr" : "=a"(lo), "=d"(hi)
  111. : "c"(MSR_IA32_TEMPERATURE_TARGET));
  112. (void)hi;
  113. return (lo >> 16) & 0xFF;
  114. }
  115. static void read_brand(char out[49])
  116. {
  117. unsigned int max_ext, a, b, c, d, i;
  118. for (i = 0; i < 49; i++)
  119. out[i] = '\0';
  120. cpuid_leaf(0x80000000u, 0, &max_ext, &b, &c, &d);
  121. if (max_ext < 0x80000004u)
  122. return;
  123. for (i = 0; i < 3; i++) {
  124. unsigned int *dst = (unsigned int *)(out + i * 16);
  125. cpuid_leaf(0x80000002u + i, 0, &a, &b, &c, &d);
  126. dst[0] = a;
  127. dst[1] = b;
  128. dst[2] = c;
  129. dst[3] = d;
  130. }
  131. }
  132. static int amd_temp_c(void)
  133. {
  134. unsigned int id, val, raw, mc;
  135. id = pci_cfg_read32(0, AMD_NB_DEV, AMD_NB_FUNC, 0x00);
  136. if (id == 0xFFFFFFFFu || id == 0)
  137. return -1;
  138. val = pci_cfg_read32(0, AMD_NB_DEV, AMD_NB_FUNC, AMD_REPORTED_TEMP);
  139. if (val == 0xFFFFFFFFu || val == 0)
  140. return -1;
  141. raw = (val >> 21) & 0x7FFu;
  142. if (raw == 0 || raw == 0x7FFu)
  143. return -1;
  144. mc = raw * 125u;
  145. if ((val & 0x30000u) == 0x30000u) {
  146. if (mc < 49000u)
  147. return -1;
  148. mc -= 49000u;
  149. }
  150. if (mc > 125000u)
  151. return -1;
  152. return (int)((mc + 500u) / 1000u);
  153. }
  154. static int pick_tjmax(unsigned int model, unsigned int stepping,
  155. const char *brand)
  156. {
  157. if (model == 0x1C || model == 0x1D) {
  158. /* The brand string often has double spaces ("CPU 330"), so
  159. * match the number alone. */
  160. if (str_has(brand, "330"))
  161. return 125;
  162. if (str_has(brand, "230"))
  163. return 100;
  164. if (stepping == 10) /* N4xx/N5xx/D4xx/D5xx */
  165. return 100;
  166. return 90;
  167. }
  168. if (model == 0x4C) /* Airmont (coretemp: 90 C) */
  169. return 90;
  170. return 100;
  171. }
  172. void thermal_init(void)
  173. {
  174. unsigned int max_leaf, a, b, c, d;
  175. unsigned int family, model = 0, stepping = 0;
  176. char vendor[13];
  177. char brand[49];
  178. int msr_probed = 0;
  179. thermal_state = 0;
  180. thermal_amd = 0;
  181. thermal_tjmax_c = 100;
  182. if (!cpu_has_cpuid())
  183. goto out;
  184. cpuid_leaf(0, 0, &max_leaf, &b, &c, &d);
  185. ((unsigned int *)vendor)[0] = b;
  186. ((unsigned int *)vendor)[1] = d;
  187. ((unsigned int *)vendor)[2] = c;
  188. vendor[12] = '\0';
  189. if (max_leaf < 1)
  190. goto out;
  191. cpuid_leaf(1, 0, &a, &b, &c, &d);
  192. family = (a >> 8) & 0xF;
  193. model = (a >> 4) & 0xF;
  194. stepping = a & 0xF;
  195. if (family == 0xF)
  196. family += (a >> 20) & 0xFF;
  197. if (family == 0x6 || family == 0xF)
  198. model += ((a >> 16) & 0xF) << 4;
  199. if (str_eq(vendor, "AuthenticAMD")) {
  200. int t;
  201. if (family != 0x15 || model >= 0x60)
  202. goto out;
  203. t = amd_temp_c();
  204. if (t < -20 || t > 125)
  205. goto out;
  206. thermal_tjmax_c = AMD_TJMAX_C;
  207. thermal_amd = 1;
  208. thermal_state = 1;
  209. goto out;
  210. }
  211. if (!str_eq(vendor, "GenuineIntel"))
  212. goto out;
  213. if (!(d & (1u << 5))) /* MSR support */
  214. goto out;
  215. if (family != 0x6 || !model_has_dts(model))
  216. goto out;
  217. if (max_leaf < 6)
  218. goto out;
  219. cpuid_leaf(6, 0, &a, &b, &c, &d);
  220. if (!(a & 1u)) /* DTS capability */
  221. goto out;
  222. read_brand(brand);
  223. thermal_tjmax_c = pick_tjmax(model, stepping, brand);
  224. thermal_tjmax_msr = 0;
  225. thermal_tjmax_raw = 0;
  226. if (model_has_msr_tjmax(model)) {
  227. unsigned int target = read_tjmax_msr();
  228. thermal_tjmax_raw = target;
  229. msr_probed = 1;
  230. if (target >= 80 && target <= 125) {
  231. thermal_tjmax_c = (int)target;
  232. thermal_tjmax_msr = 1;
  233. }
  234. }
  235. thermal_state = 1;
  236. out:
  237. if (thermal_state && thermal_amd) {
  238. serial_puts(SERIAL_COM1,
  239. "thermal: AMD family 15h, D18F3xA4, TjMax ");
  240. serial_putdec(SERIAL_COM1, (unsigned int)thermal_tjmax_c);
  241. serial_puts(SERIAL_COM1, " C\n");
  242. } else if (thermal_state) {
  243. serial_puts(SERIAL_COM1, "thermal: Intel DTS, model ");
  244. serial_putdec(SERIAL_COM1, model);
  245. serial_puts(SERIAL_COM1, " stepping ");
  246. serial_putdec(SERIAL_COM1, stepping);
  247. serial_puts(SERIAL_COM1, ", TjMax ");
  248. serial_putdec(SERIAL_COM1, (unsigned int)thermal_tjmax_c);
  249. serial_puts(SERIAL_COM1, " C");
  250. if (msr_probed) {
  251. serial_puts(SERIAL_COM1, " (msr 0x1A2=");
  252. serial_puthex(SERIAL_COM1, thermal_tjmax_raw);
  253. serial_puts(SERIAL_COM1,
  254. thermal_tjmax_msr ? ")" : ", out of range)");
  255. } else {
  256. serial_puts(SERIAL_COM1, " (table)");
  257. }
  258. serial_puts(SERIAL_COM1, "\n");
  259. } else {
  260. serial_puts(SERIAL_COM1,
  261. "thermal: no supported temperature sensor, disabled\n");
  262. }
  263. }
  264. int thermal_read_this_cpu(void)
  265. {
  266. unsigned int raw;
  267. int t;
  268. if (thermal_state < 0)
  269. thermal_init();
  270. if (!thermal_state)
  271. return -1;
  272. if (thermal_amd)
  273. return amd_temp_c();
  274. if (thermal_status_this_cpu(&raw) != 0)
  275. return -1;
  276. t = thermal_tjmax_c - (int)((raw >> 16) & 0x7F);
  277. if (t < -20 || t > 150)
  278. return -1;
  279. return t;
  280. }
  281. int thermal_status_this_cpu(unsigned int *status)
  282. {
  283. unsigned int lo, hi;
  284. if (thermal_state < 0)
  285. thermal_init();
  286. if (!thermal_state || thermal_amd)
  287. return -1;
  288. __asm__ volatile ("rdmsr" : "=a"(lo), "=d"(hi)
  289. : "c"(MSR_IA32_THERM_STATUS));
  290. (void)hi;
  291. if (status)
  292. *status = lo;
  293. return 0;
  294. }
  295. void thermal_tick(void)
  296. {
  297. struct cpu *c = this_cpu();
  298. unsigned int ms = (c->id == 0) ? timer_tick_ms() : c->tick_ms;
  299. if (thermal_state < 0)
  300. thermal_init();
  301. if (!thermal_state || thermal_amd)
  302. return;
  303. /* The timer period drops to 10 ms while a CPU is idle, so count
  304. * milliseconds rather than ticks to keep the 1 Hz sample rate. */
  305. if ((c->temp_div += ms) < 1000)
  306. return;
  307. c->temp_div = 0;
  308. c->temp_c = thermal_read_this_cpu();
  309. }
  310. void thermal_housekeeping(void)
  311. {
  312. static unsigned int last_ms;
  313. unsigned int now, i;
  314. int t;
  315. if (thermal_state < 0)
  316. thermal_init();
  317. if (!thermal_state || !thermal_amd)
  318. return;
  319. now = get_ticks_ms();
  320. if (last_ms && now - last_ms < 1000)
  321. return;
  322. last_ms = now;
  323. /* D18F3xA4 is a package/northbridge value, not per core. Publish it
  324. * to every CPU so thermal_max_c() and the top bar see it. */
  325. t = amd_temp_c();
  326. for (i = 0; i < cpu_count && i < MAX_CPUS; i++)
  327. cpus[i].temp_c = t;
  328. }
  329. int thermal_max_c(void)
  330. {
  331. int max = -1;
  332. unsigned int i;
  333. if (thermal_state < 0)
  334. thermal_init();
  335. if (!thermal_state)
  336. return -1;
  337. for (i = 0; i < cpu_count && i < MAX_CPUS; i++)
  338. if (cpus[i].temp_c > max)
  339. max = cpus[i].temp_c;
  340. /* Before the first tick has published a value (or while a CPU is
  341. * still booting), read the CPU running the caller instead of showing
  342. * nothing for the first second. */
  343. if (max < 0)
  344. max = thermal_read_this_cpu();
  345. return max;
  346. }
  347. int thermal_tjmax(void)
  348. {
  349. if (thermal_state < 0)
  350. thermal_init();
  351. return thermal_state ? thermal_tjmax_c : 0;
  352. }