therm_throt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. printk(KERN_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. printk(KERN_INFO "CPU%d: %s temperature/speed normal\n",
  174. this_cpu,
  175. level == CORE_LEVEL ? "Core" : "Package");
  176. return 1;
  177. }
  178. return 0;
  179. }
  180. static int thresh_event_valid(int level, int event)
  181. {
  182. struct _thermal_state *state;
  183. unsigned int this_cpu = smp_processor_id();
  184. struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
  185. u64 now = get_jiffies_64();
  186. if (level == PACKAGE_LEVEL)
  187. state = (event == 0) ? &pstate->pkg_thresh0 :
  188. &pstate->pkg_thresh1;
  189. else
  190. state = (event == 0) ? &pstate->core_thresh0 :
  191. &pstate->core_thresh1;
  192. if (time_before64(now, state->next_check))
  193. return 0;
  194. state->next_check = now + CHECK_INTERVAL;
  195. return 1;
  196. }
  197. static bool int_pln_enable;
  198. static int __init int_pln_enable_setup(char *s)
  199. {
  200. int_pln_enable = true;
  201. return 1;
  202. }
  203. __setup("int_pln_enable", int_pln_enable_setup);
  204. #ifdef CONFIG_SYSFS
  205. /* Add/Remove thermal_throttle interface for CPU device: */
  206. static int thermal_throttle_add_dev(struct device *dev, unsigned int cpu)
  207. {
  208. int err;
  209. struct cpuinfo_x86 *c = &cpu_data(cpu);
  210. err = sysfs_create_group(&dev->kobj, &thermal_attr_group);
  211. if (err)
  212. return err;
  213. if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  214. err = sysfs_add_file_to_group(&dev->kobj,
  215. &dev_attr_core_power_limit_count.attr,
  216. thermal_attr_group.name);
  217. if (cpu_has(c, X86_FEATURE_PTS)) {
  218. err = sysfs_add_file_to_group(&dev->kobj,
  219. &dev_attr_package_throttle_count.attr,
  220. thermal_attr_group.name);
  221. if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  222. err = sysfs_add_file_to_group(&dev->kobj,
  223. &dev_attr_package_power_limit_count.attr,
  224. thermal_attr_group.name);
  225. }
  226. return err;
  227. }
  228. static void thermal_throttle_remove_dev(struct device *dev)
  229. {
  230. sysfs_remove_group(&dev->kobj, &thermal_attr_group);
  231. }
  232. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  233. static int
  234. thermal_throttle_cpu_callback(struct notifier_block *nfb,
  235. unsigned long action,
  236. void *hcpu)
  237. {
  238. unsigned int cpu = (unsigned long)hcpu;
  239. struct device *dev;
  240. int err = 0;
  241. dev = get_cpu_device(cpu);
  242. switch (action) {
  243. case CPU_UP_PREPARE:
  244. case CPU_UP_PREPARE_FROZEN:
  245. err = thermal_throttle_add_dev(dev, cpu);
  246. WARN_ON(err);
  247. break;
  248. case CPU_UP_CANCELED:
  249. case CPU_UP_CANCELED_FROZEN:
  250. case CPU_DEAD:
  251. case CPU_DEAD_FROZEN:
  252. thermal_throttle_remove_dev(dev);
  253. break;
  254. }
  255. return notifier_from_errno(err);
  256. }
  257. static struct notifier_block thermal_throttle_cpu_notifier =
  258. {
  259. .notifier_call = thermal_throttle_cpu_callback,
  260. };
  261. static __init int thermal_throttle_init_device(void)
  262. {
  263. unsigned int cpu = 0;
  264. int err;
  265. if (!atomic_read(&therm_throt_en))
  266. return 0;
  267. cpu_notifier_register_begin();
  268. /* connect live CPUs to sysfs */
  269. for_each_online_cpu(cpu) {
  270. err = thermal_throttle_add_dev(get_cpu_device(cpu), cpu);
  271. WARN_ON(err);
  272. }
  273. __register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
  274. cpu_notifier_register_done();
  275. return 0;
  276. }
  277. device_initcall(thermal_throttle_init_device);
  278. #endif /* CONFIG_SYSFS */
  279. static void notify_package_thresholds(__u64 msr_val)
  280. {
  281. bool notify_thres_0 = false;
  282. bool notify_thres_1 = false;
  283. if (!platform_thermal_package_notify)
  284. return;
  285. /* lower threshold check */
  286. if (msr_val & THERM_LOG_THRESHOLD0)
  287. notify_thres_0 = true;
  288. /* higher threshold check */
  289. if (msr_val & THERM_LOG_THRESHOLD1)
  290. notify_thres_1 = true;
  291. if (!notify_thres_0 && !notify_thres_1)
  292. return;
  293. if (platform_thermal_package_rate_control &&
  294. platform_thermal_package_rate_control()) {
  295. /* Rate control is implemented in callback */
  296. platform_thermal_package_notify(msr_val);
  297. return;
  298. }
  299. /* lower threshold reached */
  300. if (notify_thres_0 && thresh_event_valid(PACKAGE_LEVEL, 0))
  301. platform_thermal_package_notify(msr_val);
  302. /* higher threshold reached */
  303. if (notify_thres_1 && thresh_event_valid(PACKAGE_LEVEL, 1))
  304. platform_thermal_package_notify(msr_val);
  305. }
  306. static void notify_thresholds(__u64 msr_val)
  307. {
  308. /* check whether the interrupt handler is defined;
  309. * otherwise simply return
  310. */
  311. if (!platform_thermal_notify)
  312. return;
  313. /* lower threshold reached */
  314. if ((msr_val & THERM_LOG_THRESHOLD0) &&
  315. thresh_event_valid(CORE_LEVEL, 0))
  316. platform_thermal_notify(msr_val);
  317. /* higher threshold reached */
  318. if ((msr_val & THERM_LOG_THRESHOLD1) &&
  319. thresh_event_valid(CORE_LEVEL, 1))
  320. platform_thermal_notify(msr_val);
  321. }
  322. /* Thermal transition interrupt handler */
  323. static void intel_thermal_interrupt(void)
  324. {
  325. __u64 msr_val;
  326. rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
  327. /* Check for violation of core thermal thresholds*/
  328. notify_thresholds(msr_val);
  329. if (therm_throt_process(msr_val & THERM_STATUS_PROCHOT,
  330. THERMAL_THROTTLING_EVENT,
  331. CORE_LEVEL) != 0)
  332. mce_log_therm_throt_event(msr_val);
  333. if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
  334. therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT,
  335. POWER_LIMIT_EVENT,
  336. CORE_LEVEL);
  337. if (this_cpu_has(X86_FEATURE_PTS)) {
  338. rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
  339. /* check violations of package thermal thresholds */
  340. notify_package_thresholds(msr_val);
  341. therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT,
  342. THERMAL_THROTTLING_EVENT,
  343. PACKAGE_LEVEL);
  344. if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
  345. therm_throt_process(msr_val &
  346. PACKAGE_THERM_STATUS_POWER_LIMIT,
  347. POWER_LIMIT_EVENT,
  348. PACKAGE_LEVEL);
  349. }
  350. }
  351. static void unexpected_thermal_interrupt(void)
  352. {
  353. printk(KERN_ERR "CPU%d: Unexpected LVT thermal interrupt!\n",
  354. smp_processor_id());
  355. }
  356. static void (*smp_thermal_vector)(void) = unexpected_thermal_interrupt;
  357. static inline void __smp_thermal_interrupt(void)
  358. {
  359. inc_irq_stat(irq_thermal_count);
  360. smp_thermal_vector();
  361. }
  362. asmlinkage __visible void smp_thermal_interrupt(struct pt_regs *regs)
  363. {
  364. entering_irq();
  365. __smp_thermal_interrupt();
  366. exiting_ack_irq();
  367. }
  368. asmlinkage __visible void smp_trace_thermal_interrupt(struct pt_regs *regs)
  369. {
  370. entering_irq();
  371. trace_thermal_apic_entry(THERMAL_APIC_VECTOR);
  372. __smp_thermal_interrupt();
  373. trace_thermal_apic_exit(THERMAL_APIC_VECTOR);
  374. exiting_ack_irq();
  375. }
  376. /* Thermal monitoring depends on APIC, ACPI and clock modulation */
  377. static int intel_thermal_supported(struct cpuinfo_x86 *c)
  378. {
  379. if (!cpu_has_apic)
  380. return 0;
  381. if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
  382. return 0;
  383. return 1;
  384. }
  385. void __init mcheck_intel_therm_init(void)
  386. {
  387. /*
  388. * This function is only called on boot CPU. Save the init thermal
  389. * LVT value on BSP and use that value to restore APs' thermal LVT
  390. * entry BIOS programmed later
  391. */
  392. if (intel_thermal_supported(&boot_cpu_data))
  393. lvtthmr_init = apic_read(APIC_LVTTHMR);
  394. }
  395. void intel_init_thermal(struct cpuinfo_x86 *c)
  396. {
  397. unsigned int cpu = smp_processor_id();
  398. int tm2 = 0;
  399. u32 l, h;
  400. if (!intel_thermal_supported(c))
  401. return;
  402. /*
  403. * First check if its enabled already, in which case there might
  404. * be some SMM goo which handles it, so we can't even put a handler
  405. * since it might be delivered via SMI already:
  406. */
  407. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  408. h = lvtthmr_init;
  409. /*
  410. * The initial value of thermal LVT entries on all APs always reads
  411. * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
  412. * sequence to them and LVT registers are reset to 0s except for
  413. * the mask bits which are set to 1s when APs receive INIT IPI.
  414. * If BIOS takes over the thermal interrupt and sets its interrupt
  415. * delivery mode to SMI (not fixed), it restores the value that the
  416. * BIOS has programmed on AP based on BSP's info we saved since BIOS
  417. * is always setting the same value for all threads/cores.
  418. */
  419. if ((h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED)
  420. apic_write(APIC_LVTTHMR, lvtthmr_init);
  421. if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {
  422. printk(KERN_DEBUG
  423. "CPU%d: Thermal monitoring handled by SMI\n", cpu);
  424. return;
  425. }
  426. /* Check whether a vector already exists */
  427. if (h & APIC_VECTOR_MASK) {
  428. printk(KERN_DEBUG
  429. "CPU%d: Thermal LVT vector (%#x) already installed\n",
  430. cpu, (h & APIC_VECTOR_MASK));
  431. return;
  432. }
  433. /* early Pentium M models use different method for enabling TM2 */
  434. if (cpu_has(c, X86_FEATURE_TM2)) {
  435. if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) {
  436. rdmsr(MSR_THERM2_CTL, l, h);
  437. if (l & MSR_THERM2_CTL_TM_SELECT)
  438. tm2 = 1;
  439. } else if (l & MSR_IA32_MISC_ENABLE_TM2)
  440. tm2 = 1;
  441. }
  442. /* We'll mask the thermal vector in the lapic till we're ready: */
  443. h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;
  444. apic_write(APIC_LVTTHMR, h);
  445. rdmsr(MSR_IA32_THERM_INTERRUPT, l, h);
  446. 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 if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  451. wrmsr(MSR_IA32_THERM_INTERRUPT,
  452. l | (THERM_INT_LOW_ENABLE
  453. | THERM_INT_HIGH_ENABLE | THERM_INT_PLN_ENABLE), h);
  454. else
  455. wrmsr(MSR_IA32_THERM_INTERRUPT,
  456. l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);
  457. if (cpu_has(c, X86_FEATURE_PTS)) {
  458. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  459. if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable)
  460. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  461. (l | (PACKAGE_THERM_INT_LOW_ENABLE
  462. | PACKAGE_THERM_INT_HIGH_ENABLE))
  463. & ~PACKAGE_THERM_INT_PLN_ENABLE, h);
  464. else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  465. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  466. l | (PACKAGE_THERM_INT_LOW_ENABLE
  467. | PACKAGE_THERM_INT_HIGH_ENABLE
  468. | PACKAGE_THERM_INT_PLN_ENABLE), h);
  469. else
  470. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  471. l | (PACKAGE_THERM_INT_LOW_ENABLE
  472. | PACKAGE_THERM_INT_HIGH_ENABLE), h);
  473. }
  474. smp_thermal_vector = intel_thermal_interrupt;
  475. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  476. wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);
  477. /* Unmask the thermal vector: */
  478. l = apic_read(APIC_LVTTHMR);
  479. apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
  480. printk_once(KERN_INFO "CPU0: Thermal monitoring enabled (%s)\n",
  481. tm2 ? "TM2" : "TM1");
  482. /* enable thermal throttle processing */
  483. atomic_set(&therm_throt_en, 1);
  484. }