therm_throt.c 16 KB

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