therm_throt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Thermal throttle event support code (such as syslog messaging and rate
  3. * limiting) that was factored out from x86_64 (mce_intel.c) and i386 (p4.c).
  4. *
  5. * This allows consistent reporting of CPU thermal throttle events.
  6. *
  7. * Maintains a counter in /sys that keeps track of the number of thermal
  8. * events, such that the user knows how bad the thermal problem might be
  9. * (since the logging to syslog is rate limited).
  10. *
  11. * Author: Dmitriy Zavin (dmitriyz@google.com)
  12. *
  13. * Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.
  14. * Inspired by Ross Biro's and Al Borchers' counter code.
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/notifier.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/kernel.h>
  20. #include <linux/percpu.h>
  21. #include <linux/export.h>
  22. #include <linux/types.h>
  23. #include <linux/init.h>
  24. #include <linux/smp.h>
  25. #include <linux/cpu.h>
  26. #include <asm/processor.h>
  27. #include <asm/apic.h>
  28. #include <asm/mce.h>
  29. #include <asm/msr.h>
  30. #include <asm/trace/irq_vectors.h>
  31. /* How long to wait between reporting thermal events */
  32. #define CHECK_INTERVAL (300 * HZ)
  33. #define THERMAL_THROTTLING_EVENT 0
  34. #define POWER_LIMIT_EVENT 1
  35. /*
  36. * Current thermal event state:
  37. */
  38. struct _thermal_state {
  39. bool new_event;
  40. int event;
  41. u64 next_check;
  42. unsigned long count;
  43. unsigned long last_count;
  44. };
  45. struct thermal_state {
  46. struct _thermal_state core_throttle;
  47. struct _thermal_state core_power_limit;
  48. struct _thermal_state package_throttle;
  49. struct _thermal_state package_power_limit;
  50. struct _thermal_state core_thresh0;
  51. struct _thermal_state core_thresh1;
  52. struct _thermal_state pkg_thresh0;
  53. struct _thermal_state pkg_thresh1;
  54. };
  55. /* Callback to handle core threshold interrupts */
  56. int (*platform_thermal_notify)(__u64 msr_val);
  57. EXPORT_SYMBOL(platform_thermal_notify);
  58. /* Callback to handle core package threshold_interrupts */
  59. int (*platform_thermal_package_notify)(__u64 msr_val);
  60. EXPORT_SYMBOL_GPL(platform_thermal_package_notify);
  61. /* Callback support of rate control, return true, if
  62. * callback has rate control */
  63. bool (*platform_thermal_package_rate_control)(void);
  64. EXPORT_SYMBOL_GPL(platform_thermal_package_rate_control);
  65. static DEFINE_PER_CPU(struct thermal_state, thermal_state);
  66. static atomic_t therm_throt_en = ATOMIC_INIT(0);
  67. static u32 lvtthmr_init __read_mostly;
  68. #ifdef CONFIG_SYSFS
  69. #define define_therm_throt_device_one_ro(_name) \
  70. static DEVICE_ATTR(_name, 0444, \
  71. therm_throt_device_show_##_name, \
  72. NULL) \
  73. #define define_therm_throt_device_show_func(event, name) \
  74. \
  75. static ssize_t therm_throt_device_show_##event##_##name( \
  76. struct device *dev, \
  77. struct device_attribute *attr, \
  78. char *buf) \
  79. { \
  80. unsigned int cpu = dev->id; \
  81. ssize_t ret; \
  82. \
  83. preempt_disable(); /* CPU hotplug */ \
  84. if (cpu_online(cpu)) { \
  85. ret = sprintf(buf, "%lu\n", \
  86. per_cpu(thermal_state, cpu).event.name); \
  87. } else \
  88. ret = 0; \
  89. preempt_enable(); \
  90. \
  91. return ret; \
  92. }
  93. define_therm_throt_device_show_func(core_throttle, count);
  94. define_therm_throt_device_one_ro(core_throttle_count);
  95. define_therm_throt_device_show_func(core_power_limit, count);
  96. define_therm_throt_device_one_ro(core_power_limit_count);
  97. define_therm_throt_device_show_func(package_throttle, count);
  98. define_therm_throt_device_one_ro(package_throttle_count);
  99. define_therm_throt_device_show_func(package_power_limit, count);
  100. define_therm_throt_device_one_ro(package_power_limit_count);
  101. static struct attribute *thermal_throttle_attrs[] = {
  102. &dev_attr_core_throttle_count.attr,
  103. NULL
  104. };
  105. static const struct attribute_group thermal_attr_group = {
  106. .attrs = thermal_throttle_attrs,
  107. .name = "thermal_throttle"
  108. };
  109. #endif /* CONFIG_SYSFS */
  110. #define CORE_LEVEL 0
  111. #define PACKAGE_LEVEL 1
  112. /***
  113. * therm_throt_process - Process thermal throttling event from interrupt
  114. * @curr: Whether the condition is current or not (boolean), since the
  115. * thermal interrupt normally gets called both when the thermal
  116. * event begins and once the event has ended.
  117. *
  118. * This function is called by the thermal interrupt after the
  119. * IRQ has been acknowledged.
  120. *
  121. * It will take care of rate limiting and printing messages to the syslog.
  122. */
  123. static void therm_throt_process(bool new_event, int event, int level)
  124. {
  125. struct _thermal_state *state;
  126. unsigned int this_cpu = smp_processor_id();
  127. bool old_event;
  128. u64 now;
  129. struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
  130. now = get_jiffies_64();
  131. if (level == CORE_LEVEL) {
  132. if (event == THERMAL_THROTTLING_EVENT)
  133. state = &pstate->core_throttle;
  134. else if (event == POWER_LIMIT_EVENT)
  135. state = &pstate->core_power_limit;
  136. else
  137. return;
  138. } else if (level == PACKAGE_LEVEL) {
  139. if (event == THERMAL_THROTTLING_EVENT)
  140. state = &pstate->package_throttle;
  141. else if (event == POWER_LIMIT_EVENT)
  142. state = &pstate->package_power_limit;
  143. else
  144. return;
  145. } else
  146. return;
  147. old_event = state->new_event;
  148. state->new_event = new_event;
  149. if (new_event)
  150. state->count++;
  151. if (time_before64(now, state->next_check) &&
  152. state->count != state->last_count)
  153. return;
  154. state->next_check = now + CHECK_INTERVAL;
  155. state->last_count = state->count;
  156. /* if we just entered the thermal event */
  157. if (new_event) {
  158. if (event == THERMAL_THROTTLING_EVENT)
  159. pr_crit("CPU%d: %s temperature above threshold, cpu clock throttled (total events = %lu)\n",
  160. this_cpu,
  161. level == CORE_LEVEL ? "Core" : "Package",
  162. state->count);
  163. return;
  164. }
  165. if (old_event) {
  166. if (event == THERMAL_THROTTLING_EVENT)
  167. pr_info("CPU%d: %s temperature/speed normal\n", this_cpu,
  168. level == CORE_LEVEL ? "Core" : "Package");
  169. return;
  170. }
  171. }
  172. static int thresh_event_valid(int level, int event)
  173. {
  174. struct _thermal_state *state;
  175. unsigned int this_cpu = smp_processor_id();
  176. struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
  177. u64 now = get_jiffies_64();
  178. if (level == PACKAGE_LEVEL)
  179. state = (event == 0) ? &pstate->pkg_thresh0 :
  180. &pstate->pkg_thresh1;
  181. else
  182. state = (event == 0) ? &pstate->core_thresh0 :
  183. &pstate->core_thresh1;
  184. if (time_before64(now, state->next_check))
  185. return 0;
  186. state->next_check = now + CHECK_INTERVAL;
  187. return 1;
  188. }
  189. static bool int_pln_enable;
  190. static int __init int_pln_enable_setup(char *s)
  191. {
  192. int_pln_enable = true;
  193. return 1;
  194. }
  195. __setup("int_pln_enable", int_pln_enable_setup);
  196. #ifdef CONFIG_SYSFS
  197. /* Add/Remove thermal_throttle interface for CPU device: */
  198. static int thermal_throttle_add_dev(struct device *dev, unsigned int cpu)
  199. {
  200. int err;
  201. struct cpuinfo_x86 *c = &cpu_data(cpu);
  202. err = sysfs_create_group(&dev->kobj, &thermal_attr_group);
  203. if (err)
  204. return err;
  205. if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  206. err = sysfs_add_file_to_group(&dev->kobj,
  207. &dev_attr_core_power_limit_count.attr,
  208. thermal_attr_group.name);
  209. if (cpu_has(c, X86_FEATURE_PTS)) {
  210. err = sysfs_add_file_to_group(&dev->kobj,
  211. &dev_attr_package_throttle_count.attr,
  212. thermal_attr_group.name);
  213. if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  214. err = sysfs_add_file_to_group(&dev->kobj,
  215. &dev_attr_package_power_limit_count.attr,
  216. thermal_attr_group.name);
  217. }
  218. return err;
  219. }
  220. static void thermal_throttle_remove_dev(struct device *dev)
  221. {
  222. sysfs_remove_group(&dev->kobj, &thermal_attr_group);
  223. }
  224. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  225. static int thermal_throttle_online(unsigned int cpu)
  226. {
  227. struct device *dev = get_cpu_device(cpu);
  228. return thermal_throttle_add_dev(dev, cpu);
  229. }
  230. static int thermal_throttle_offline(unsigned int cpu)
  231. {
  232. struct device *dev = get_cpu_device(cpu);
  233. thermal_throttle_remove_dev(dev);
  234. return 0;
  235. }
  236. static __init int thermal_throttle_init_device(void)
  237. {
  238. int ret;
  239. if (!atomic_read(&therm_throt_en))
  240. return 0;
  241. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/therm:online",
  242. thermal_throttle_online,
  243. thermal_throttle_offline);
  244. return ret < 0 ? ret : 0;
  245. }
  246. device_initcall(thermal_throttle_init_device);
  247. #endif /* CONFIG_SYSFS */
  248. static void notify_package_thresholds(__u64 msr_val)
  249. {
  250. bool notify_thres_0 = false;
  251. bool notify_thres_1 = false;
  252. if (!platform_thermal_package_notify)
  253. return;
  254. /* lower threshold check */
  255. if (msr_val & THERM_LOG_THRESHOLD0)
  256. notify_thres_0 = true;
  257. /* higher threshold check */
  258. if (msr_val & THERM_LOG_THRESHOLD1)
  259. notify_thres_1 = true;
  260. if (!notify_thres_0 && !notify_thres_1)
  261. return;
  262. if (platform_thermal_package_rate_control &&
  263. platform_thermal_package_rate_control()) {
  264. /* Rate control is implemented in callback */
  265. platform_thermal_package_notify(msr_val);
  266. return;
  267. }
  268. /* lower threshold reached */
  269. if (notify_thres_0 && thresh_event_valid(PACKAGE_LEVEL, 0))
  270. platform_thermal_package_notify(msr_val);
  271. /* higher threshold reached */
  272. if (notify_thres_1 && thresh_event_valid(PACKAGE_LEVEL, 1))
  273. platform_thermal_package_notify(msr_val);
  274. }
  275. static void notify_thresholds(__u64 msr_val)
  276. {
  277. /* check whether the interrupt handler is defined;
  278. * otherwise simply return
  279. */
  280. if (!platform_thermal_notify)
  281. return;
  282. /* lower threshold reached */
  283. if ((msr_val & THERM_LOG_THRESHOLD0) &&
  284. thresh_event_valid(CORE_LEVEL, 0))
  285. platform_thermal_notify(msr_val);
  286. /* higher threshold reached */
  287. if ((msr_val & THERM_LOG_THRESHOLD1) &&
  288. thresh_event_valid(CORE_LEVEL, 1))
  289. platform_thermal_notify(msr_val);
  290. }
  291. /* Thermal transition interrupt handler */
  292. static void intel_thermal_interrupt(void)
  293. {
  294. __u64 msr_val;
  295. if (static_cpu_has(X86_FEATURE_HWP))
  296. wrmsrl_safe(MSR_HWP_STATUS, 0);
  297. rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
  298. /* Check for violation of core thermal thresholds*/
  299. notify_thresholds(msr_val);
  300. therm_throt_process(msr_val & THERM_STATUS_PROCHOT,
  301. THERMAL_THROTTLING_EVENT,
  302. CORE_LEVEL);
  303. if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
  304. therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT,
  305. POWER_LIMIT_EVENT,
  306. CORE_LEVEL);
  307. if (this_cpu_has(X86_FEATURE_PTS)) {
  308. rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
  309. /* check violations of package thermal thresholds */
  310. notify_package_thresholds(msr_val);
  311. therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT,
  312. THERMAL_THROTTLING_EVENT,
  313. PACKAGE_LEVEL);
  314. if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
  315. therm_throt_process(msr_val &
  316. PACKAGE_THERM_STATUS_POWER_LIMIT,
  317. POWER_LIMIT_EVENT,
  318. PACKAGE_LEVEL);
  319. }
  320. }
  321. static void unexpected_thermal_interrupt(void)
  322. {
  323. pr_err("CPU%d: Unexpected LVT thermal interrupt!\n",
  324. smp_processor_id());
  325. }
  326. static void (*smp_thermal_vector)(void) = unexpected_thermal_interrupt;
  327. asmlinkage __visible void __irq_entry smp_thermal_interrupt(struct pt_regs *r)
  328. {
  329. entering_irq();
  330. trace_thermal_apic_entry(THERMAL_APIC_VECTOR);
  331. inc_irq_stat(irq_thermal_count);
  332. smp_thermal_vector();
  333. trace_thermal_apic_exit(THERMAL_APIC_VECTOR);
  334. exiting_ack_irq();
  335. }
  336. /* Thermal monitoring depends on APIC, ACPI and clock modulation */
  337. static int intel_thermal_supported(struct cpuinfo_x86 *c)
  338. {
  339. if (!boot_cpu_has(X86_FEATURE_APIC))
  340. return 0;
  341. if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
  342. return 0;
  343. return 1;
  344. }
  345. void __init mcheck_intel_therm_init(void)
  346. {
  347. /*
  348. * This function is only called on boot CPU. Save the init thermal
  349. * LVT value on BSP and use that value to restore APs' thermal LVT
  350. * entry BIOS programmed later
  351. */
  352. if (intel_thermal_supported(&boot_cpu_data))
  353. lvtthmr_init = apic_read(APIC_LVTTHMR);
  354. }
  355. void intel_init_thermal(struct cpuinfo_x86 *c)
  356. {
  357. unsigned int cpu = smp_processor_id();
  358. int tm2 = 0;
  359. u32 l, h;
  360. if (!intel_thermal_supported(c))
  361. return;
  362. /*
  363. * First check if its enabled already, in which case there might
  364. * be some SMM goo which handles it, so we can't even put a handler
  365. * since it might be delivered via SMI already:
  366. */
  367. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  368. h = lvtthmr_init;
  369. /*
  370. * The initial value of thermal LVT entries on all APs always reads
  371. * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
  372. * sequence to them and LVT registers are reset to 0s except for
  373. * the mask bits which are set to 1s when APs receive INIT IPI.
  374. * If BIOS takes over the thermal interrupt and sets its interrupt
  375. * delivery mode to SMI (not fixed), it restores the value that the
  376. * BIOS has programmed on AP based on BSP's info we saved since BIOS
  377. * is always setting the same value for all threads/cores.
  378. */
  379. if ((h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED)
  380. apic_write(APIC_LVTTHMR, lvtthmr_init);
  381. if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {
  382. if (system_state == SYSTEM_BOOTING)
  383. pr_debug("CPU%d: Thermal monitoring handled by SMI\n", cpu);
  384. return;
  385. }
  386. /* early Pentium M models use different method for enabling TM2 */
  387. if (cpu_has(c, X86_FEATURE_TM2)) {
  388. if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) {
  389. rdmsr(MSR_THERM2_CTL, l, h);
  390. if (l & MSR_THERM2_CTL_TM_SELECT)
  391. tm2 = 1;
  392. } else if (l & MSR_IA32_MISC_ENABLE_TM2)
  393. tm2 = 1;
  394. }
  395. /* We'll mask the thermal vector in the lapic till we're ready: */
  396. h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;
  397. apic_write(APIC_LVTTHMR, h);
  398. rdmsr(MSR_IA32_THERM_INTERRUPT, l, h);
  399. if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable)
  400. wrmsr(MSR_IA32_THERM_INTERRUPT,
  401. (l | (THERM_INT_LOW_ENABLE
  402. | THERM_INT_HIGH_ENABLE)) & ~THERM_INT_PLN_ENABLE, h);
  403. else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  404. wrmsr(MSR_IA32_THERM_INTERRUPT,
  405. l | (THERM_INT_LOW_ENABLE
  406. | THERM_INT_HIGH_ENABLE | THERM_INT_PLN_ENABLE), h);
  407. else
  408. wrmsr(MSR_IA32_THERM_INTERRUPT,
  409. l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);
  410. if (cpu_has(c, X86_FEATURE_PTS)) {
  411. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  412. if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable)
  413. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  414. (l | (PACKAGE_THERM_INT_LOW_ENABLE
  415. | PACKAGE_THERM_INT_HIGH_ENABLE))
  416. & ~PACKAGE_THERM_INT_PLN_ENABLE, h);
  417. else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  418. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  419. l | (PACKAGE_THERM_INT_LOW_ENABLE
  420. | PACKAGE_THERM_INT_HIGH_ENABLE
  421. | PACKAGE_THERM_INT_PLN_ENABLE), h);
  422. else
  423. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  424. l | (PACKAGE_THERM_INT_LOW_ENABLE
  425. | PACKAGE_THERM_INT_HIGH_ENABLE), h);
  426. }
  427. smp_thermal_vector = intel_thermal_interrupt;
  428. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  429. wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);
  430. /* Unmask the thermal vector: */
  431. l = apic_read(APIC_LVTTHMR);
  432. apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
  433. pr_info_once("CPU0: Thermal monitoring enabled (%s)\n",
  434. tm2 ? "TM2" : "TM1");
  435. /* enable thermal throttle processing */
  436. atomic_set(&therm_throt_en, 1);
  437. }