thermal.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "sched.h"
  18. #include "serial.h"
  19. #include "timer.h"
  20. #define MSR_IA32_THERM_STATUS 0x19C
  21. #define MSR_IA32_TEMPERATURE_TARGET 0x1A2
  22. static int thermal_state = -1; /* -1 unprobed, 0 unsupported, 1 DTS */
  23. static int thermal_tjmax_c = 100;
  24. static unsigned int thermal_tjmax_raw; /* last MSR 0x1A2 sample */
  25. static int thermal_tjmax_msr; /* TjMax came from the MSR */
  26. static void cpuid_leaf(unsigned int leaf, unsigned int sub,
  27. unsigned int *a, unsigned int *b, unsigned int *c,
  28. unsigned int *d)
  29. {
  30. __asm__ volatile ("cpuid"
  31. : "=a"(*a), "=b"(*b), "=c"(*c), "=d"(*d)
  32. : "a"(leaf), "c"(sub));
  33. }
  34. static int str_eq(const char *a, const char *b)
  35. {
  36. while (*a && *a == *b) {
  37. a++;
  38. b++;
  39. }
  40. return *a == *b;
  41. }
  42. static int str_has(const char *hay, const char *needle)
  43. {
  44. unsigned int i, j;
  45. for (i = 0; hay[i]; i++) {
  46. for (j = 0; needle[j] && hay[i + j] == needle[j]; j++)
  47. ;
  48. if (!needle[j])
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. /* Model allowlist from coretemp: only parts that really expose a DTS. This
  54. * keeps rdmsr(0x19C) away from CPUs (and emulators) that set CPUID.06H but
  55. * do not implement the MSR, where it would raise #GP and panic. */
  56. static int model_has_dts(unsigned int model)
  57. {
  58. switch (model) {
  59. case 0x0E: /* Core Solo/Duo (Yonah) */
  60. case 0x0F: /* Core 2 */
  61. case 0x16: /* Core 2 */
  62. case 0x17: /* Penryn */
  63. case 0x1A: /* Nehalem */
  64. case 0x1C: /* Atom 230/330 (Diamondville) */
  65. case 0x1D: /* Atom Pineview */
  66. case 0x1E: /* Lynnfield */
  67. case 0x1F: /* Nehalem */
  68. case 0x25: /* Arrandale/Clarkdale (Core i5/i7 M, X201) */
  69. case 0x2C: /* Westmere-EP */
  70. case 0x2E: /* Nehalem-EX */
  71. case 0x2F: /* Westmere-EX */
  72. case 0x4C: /* Airmont: Braswell / Cherry Trail */
  73. return 1;
  74. default:
  75. return 0;
  76. }
  77. }
  78. /* Models whose TjMax must be read from IA32_TEMPERATURE_TARGET (0x1A2)
  79. * instead of the Bonnell table. Nehalem and later implement the MSR; older
  80. * coretemp-listed parts do not, so a read there could raise #GP. Linux's
  81. * coretemp reads the same MSR and falls back to 100 C when it is missing or
  82. * implausible. */
  83. static int model_has_msr_tjmax(unsigned int model)
  84. {
  85. switch (model) {
  86. case 0x1A:
  87. case 0x1E:
  88. case 0x1F:
  89. case 0x25:
  90. case 0x2C:
  91. case 0x2E:
  92. case 0x2F:
  93. return 1;
  94. default:
  95. return 0;
  96. }
  97. }
  98. static unsigned int read_tjmax_msr(void)
  99. {
  100. unsigned int lo, hi;
  101. __asm__ volatile ("rdmsr" : "=a"(lo), "=d"(hi)
  102. : "c"(MSR_IA32_TEMPERATURE_TARGET));
  103. (void)hi;
  104. return (lo >> 16) & 0xFF;
  105. }
  106. static void read_brand(char out[49])
  107. {
  108. unsigned int max_ext, a, b, c, d, i;
  109. for (i = 0; i < 49; i++)
  110. out[i] = '\0';
  111. cpuid_leaf(0x80000000u, 0, &max_ext, &b, &c, &d);
  112. if (max_ext < 0x80000004u)
  113. return;
  114. for (i = 0; i < 3; i++) {
  115. unsigned int *dst = (unsigned int *)(out + i * 16);
  116. cpuid_leaf(0x80000002u + i, 0, &a, &b, &c, &d);
  117. dst[0] = a;
  118. dst[1] = b;
  119. dst[2] = c;
  120. dst[3] = d;
  121. }
  122. }
  123. static int pick_tjmax(unsigned int model, unsigned int stepping,
  124. const char *brand)
  125. {
  126. if (model == 0x1C || model == 0x1D) {
  127. /* The brand string often has double spaces ("CPU 330"), so
  128. * match the number alone. */
  129. if (str_has(brand, "330"))
  130. return 125;
  131. if (str_has(brand, "230"))
  132. return 100;
  133. if (stepping == 10) /* N4xx/N5xx/D4xx/D5xx */
  134. return 100;
  135. return 90;
  136. }
  137. if (model == 0x4C) /* Airmont (coretemp: 90 C) */
  138. return 90;
  139. return 100;
  140. }
  141. void thermal_init(void)
  142. {
  143. unsigned int max_leaf, a, b, c, d;
  144. unsigned int family, model = 0, stepping = 0;
  145. char vendor[13];
  146. char brand[49];
  147. int msr_probed = 0;
  148. thermal_state = 0;
  149. thermal_tjmax_c = 100;
  150. if (!cpu_has_cpuid())
  151. goto out;
  152. cpuid_leaf(0, 0, &max_leaf, &b, &c, &d);
  153. ((unsigned int *)vendor)[0] = b;
  154. ((unsigned int *)vendor)[1] = d;
  155. ((unsigned int *)vendor)[2] = c;
  156. vendor[12] = '\0';
  157. if (!str_eq(vendor, "GenuineIntel") || max_leaf < 1)
  158. goto out;
  159. cpuid_leaf(1, 0, &a, &b, &c, &d);
  160. if (!(d & (1u << 5))) /* MSR support */
  161. goto out;
  162. family = (a >> 8) & 0xF;
  163. model = (a >> 4) & 0xF;
  164. stepping = a & 0xF;
  165. if (family == 0xF)
  166. family += (a >> 20) & 0xFF;
  167. if (family == 0x6 || family == 0xF)
  168. model += ((a >> 16) & 0xF) << 4;
  169. if (family != 0x6 || !model_has_dts(model))
  170. goto out;
  171. if (max_leaf < 6)
  172. goto out;
  173. cpuid_leaf(6, 0, &a, &b, &c, &d);
  174. if (!(a & 1u)) /* DTS capability */
  175. goto out;
  176. read_brand(brand);
  177. thermal_tjmax_c = pick_tjmax(model, stepping, brand);
  178. thermal_tjmax_msr = 0;
  179. thermal_tjmax_raw = 0;
  180. if (model_has_msr_tjmax(model)) {
  181. unsigned int target = read_tjmax_msr();
  182. thermal_tjmax_raw = target;
  183. msr_probed = 1;
  184. if (target >= 80 && target <= 125) {
  185. thermal_tjmax_c = (int)target;
  186. thermal_tjmax_msr = 1;
  187. }
  188. }
  189. thermal_state = 1;
  190. out:
  191. if (thermal_state) {
  192. serial_puts(SERIAL_COM1, "thermal: Intel DTS, model ");
  193. serial_putdec(SERIAL_COM1, model);
  194. serial_puts(SERIAL_COM1, " stepping ");
  195. serial_putdec(SERIAL_COM1, stepping);
  196. serial_puts(SERIAL_COM1, ", TjMax ");
  197. serial_putdec(SERIAL_COM1, (unsigned int)thermal_tjmax_c);
  198. serial_puts(SERIAL_COM1, " C");
  199. if (msr_probed) {
  200. serial_puts(SERIAL_COM1, " (msr 0x1A2=");
  201. serial_puthex(SERIAL_COM1, thermal_tjmax_raw);
  202. serial_puts(SERIAL_COM1,
  203. thermal_tjmax_msr ? ")" : ", out of range)");
  204. } else {
  205. serial_puts(SERIAL_COM1, " (table)");
  206. }
  207. serial_puts(SERIAL_COM1, "\n");
  208. } else {
  209. serial_puts(SERIAL_COM1, "thermal: no Intel DTS, disabled\n");
  210. }
  211. }
  212. int thermal_read_this_cpu(void)
  213. {
  214. unsigned int raw;
  215. int t;
  216. if (thermal_status_this_cpu(&raw) != 0)
  217. return -1;
  218. t = thermal_tjmax_c - (int)((raw >> 16) & 0x7F);
  219. if (t < -20 || t > 150)
  220. return -1;
  221. return t;
  222. }
  223. int thermal_status_this_cpu(unsigned int *status)
  224. {
  225. unsigned int lo, hi;
  226. if (thermal_state < 0)
  227. thermal_init();
  228. if (!thermal_state)
  229. return -1;
  230. __asm__ volatile ("rdmsr" : "=a"(lo), "=d"(hi)
  231. : "c"(MSR_IA32_THERM_STATUS));
  232. (void)hi;
  233. if (status)
  234. *status = lo;
  235. return 0;
  236. }
  237. void thermal_tick(void)
  238. {
  239. struct cpu *c = this_cpu();
  240. unsigned int ms = (c->id == 0) ? timer_tick_ms() : c->tick_ms;
  241. /* The timer period drops to 10 ms while a CPU is idle, so count
  242. * milliseconds rather than ticks to keep the 1 Hz sample rate. */
  243. if ((c->temp_div += ms) < 1000)
  244. return;
  245. c->temp_div = 0;
  246. c->temp_c = thermal_read_this_cpu();
  247. }
  248. int thermal_max_c(void)
  249. {
  250. int max = -1;
  251. unsigned int i;
  252. if (thermal_state < 0)
  253. thermal_init();
  254. if (!thermal_state)
  255. return -1;
  256. for (i = 0; i < cpu_count && i < MAX_CPUS; i++)
  257. if (cpus[i].temp_c > max)
  258. max = cpus[i].temp_c;
  259. /* Before the first tick has published a value (or while a CPU is
  260. * still booting), read the CPU running the caller instead of showing
  261. * nothing for the first second. */
  262. if (max < 0)
  263. max = thermal_read_this_cpu();
  264. return max;
  265. }
  266. int thermal_tjmax(void)
  267. {
  268. if (thermal_state < 0)
  269. thermal_init();
  270. return thermal_state ? thermal_tjmax_c : 0;
  271. }