therm_throt.c 14 KB

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