thermal.c 7.4 KB

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