thermal.c 5.6 KB

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