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).
5 * This allows consistent reporting of CPU thermal throttle events.
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).
11 * Author: Dmitriy Zavin (dmitriyz@google.com)
13 * Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.
14 * Inspired by Ross Biro's and Al Borchers' counter code.
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>
28 #include <asm/processor.h>
29 #include <asm/system.h>
35 /* How long to wait between reporting thermal events */
36 #define CHECK_INTERVAL (300 * HZ)
38 #define THERMAL_THROTTLING_EVENT 0
39 #define POWER_LIMIT_EVENT 1
42 * Current thermal event state:
44 struct _thermal_state
{
49 unsigned long last_count
;
52 struct thermal_state
{
53 struct _thermal_state core_throttle
;
54 struct _thermal_state core_power_limit
;
55 struct _thermal_state package_throttle
;
56 struct _thermal_state package_power_limit
;
57 struct _thermal_state core_thresh0
;
58 struct _thermal_state core_thresh1
;
61 /* Callback to handle core threshold interrupts */
62 int (*platform_thermal_notify
)(__u64 msr_val
);
63 EXPORT_SYMBOL(platform_thermal_notify
);
65 static DEFINE_PER_CPU(struct thermal_state
, thermal_state
);
67 static atomic_t therm_throt_en
= ATOMIC_INIT(0);
69 static u32 lvtthmr_init __read_mostly
;
72 #define define_therm_throt_sysdev_one_ro(_name) \
73 static SYSDEV_ATTR(_name, 0444, \
74 therm_throt_sysdev_show_##_name, \
77 #define define_therm_throt_sysdev_show_func(event, name) \
79 static ssize_t therm_throt_sysdev_show_##event##_##name( \
80 struct sys_device *dev, \
81 struct sysdev_attribute *attr, \
84 unsigned int cpu = dev->id; \
87 preempt_disable(); /* CPU hotplug */ \
88 if (cpu_online(cpu)) { \
89 ret = sprintf(buf, "%lu\n", \
90 per_cpu(thermal_state, cpu).event.name); \
98 define_therm_throt_sysdev_show_func(core_throttle
, count
);
99 define_therm_throt_sysdev_one_ro(core_throttle_count
);
101 define_therm_throt_sysdev_show_func(core_power_limit
, count
);
102 define_therm_throt_sysdev_one_ro(core_power_limit_count
);
104 define_therm_throt_sysdev_show_func(package_throttle
, count
);
105 define_therm_throt_sysdev_one_ro(package_throttle_count
);
107 define_therm_throt_sysdev_show_func(package_power_limit
, count
);
108 define_therm_throt_sysdev_one_ro(package_power_limit_count
);
110 static struct attribute
*thermal_throttle_attrs
[] = {
111 &attr_core_throttle_count
.attr
,
115 static struct attribute_group thermal_attr_group
= {
116 .attrs
= thermal_throttle_attrs
,
117 .name
= "thermal_throttle"
119 #endif /* CONFIG_SYSFS */
122 #define PACKAGE_LEVEL 1
125 * therm_throt_process - Process thermal throttling event from interrupt
126 * @curr: Whether the condition is current or not (boolean), since the
127 * thermal interrupt normally gets called both when the thermal
128 * event begins and once the event has ended.
130 * This function is called by the thermal interrupt after the
131 * IRQ has been acknowledged.
133 * It will take care of rate limiting and printing messages to the syslog.
135 * Returns: 0 : Event should NOT be further logged, i.e. still in
136 * "timeout" from previous log message.
137 * 1 : Event should be logged further, and a message has been
138 * printed to the syslog.
140 static int therm_throt_process(bool new_event
, int event
, int level
)
142 struct _thermal_state
*state
;
143 unsigned int this_cpu
= smp_processor_id();
146 struct thermal_state
*pstate
= &per_cpu(thermal_state
, this_cpu
);
148 now
= get_jiffies_64();
149 if (level
== CORE_LEVEL
) {
150 if (event
== THERMAL_THROTTLING_EVENT
)
151 state
= &pstate
->core_throttle
;
152 else if (event
== POWER_LIMIT_EVENT
)
153 state
= &pstate
->core_power_limit
;
156 } else if (level
== PACKAGE_LEVEL
) {
157 if (event
== THERMAL_THROTTLING_EVENT
)
158 state
= &pstate
->package_throttle
;
159 else if (event
== POWER_LIMIT_EVENT
)
160 state
= &pstate
->package_power_limit
;
166 old_event
= state
->new_event
;
167 state
->new_event
= new_event
;
172 if (time_before64(now
, state
->next_check
) &&
173 state
->count
!= state
->last_count
)
176 state
->next_check
= now
+ CHECK_INTERVAL
;
177 state
->last_count
= state
->count
;
179 /* if we just entered the thermal event */
181 if (event
== THERMAL_THROTTLING_EVENT
)
182 printk(KERN_CRIT
"CPU%d: %s temperature above threshold, cpu clock throttled (total events = %lu)\n",
184 level
== CORE_LEVEL
? "Core" : "Package",
187 printk(KERN_CRIT
"CPU%d: %s power limit notification (total events = %lu)\n",
189 level
== CORE_LEVEL
? "Core" : "Package",
194 if (event
== THERMAL_THROTTLING_EVENT
)
195 printk(KERN_INFO
"CPU%d: %s temperature/speed normal\n",
197 level
== CORE_LEVEL
? "Core" : "Package");
199 printk(KERN_INFO
"CPU%d: %s power limit normal\n",
201 level
== CORE_LEVEL
? "Core" : "Package");
208 static int thresh_event_valid(int event
)
210 struct _thermal_state
*state
;
211 unsigned int this_cpu
= smp_processor_id();
212 struct thermal_state
*pstate
= &per_cpu(thermal_state
, this_cpu
);
213 u64 now
= get_jiffies_64();
215 state
= (event
== 0) ? &pstate
->core_thresh0
: &pstate
->core_thresh1
;
217 if (time_before64(now
, state
->next_check
))
220 state
->next_check
= now
+ CHECK_INTERVAL
;
225 /* Add/Remove thermal_throttle interface for CPU device: */
226 static __cpuinit
int thermal_throttle_add_dev(struct sys_device
*sys_dev
,
230 struct cpuinfo_x86
*c
= &cpu_data(cpu
);
232 err
= sysfs_create_group(&sys_dev
->kobj
, &thermal_attr_group
);
236 if (cpu_has(c
, X86_FEATURE_PLN
))
237 err
= sysfs_add_file_to_group(&sys_dev
->kobj
,
238 &attr_core_power_limit_count
.attr
,
239 thermal_attr_group
.name
);
240 if (cpu_has(c
, X86_FEATURE_PTS
)) {
241 err
= sysfs_add_file_to_group(&sys_dev
->kobj
,
242 &attr_package_throttle_count
.attr
,
243 thermal_attr_group
.name
);
244 if (cpu_has(c
, X86_FEATURE_PLN
))
245 err
= sysfs_add_file_to_group(&sys_dev
->kobj
,
246 &attr_package_power_limit_count
.attr
,
247 thermal_attr_group
.name
);
253 static __cpuinit
void thermal_throttle_remove_dev(struct sys_device
*sys_dev
)
255 sysfs_remove_group(&sys_dev
->kobj
, &thermal_attr_group
);
258 /* Mutex protecting device creation against CPU hotplug: */
259 static DEFINE_MUTEX(therm_cpu_lock
);
261 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
263 thermal_throttle_cpu_callback(struct notifier_block
*nfb
,
264 unsigned long action
,
267 unsigned int cpu
= (unsigned long)hcpu
;
268 struct sys_device
*sys_dev
;
271 sys_dev
= get_cpu_sysdev(cpu
);
275 case CPU_UP_PREPARE_FROZEN
:
276 mutex_lock(&therm_cpu_lock
);
277 err
= thermal_throttle_add_dev(sys_dev
, cpu
);
278 mutex_unlock(&therm_cpu_lock
);
281 case CPU_UP_CANCELED
:
282 case CPU_UP_CANCELED_FROZEN
:
284 case CPU_DEAD_FROZEN
:
285 mutex_lock(&therm_cpu_lock
);
286 thermal_throttle_remove_dev(sys_dev
);
287 mutex_unlock(&therm_cpu_lock
);
290 return notifier_from_errno(err
);
293 static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata
=
295 .notifier_call
= thermal_throttle_cpu_callback
,
298 static __init
int thermal_throttle_init_device(void)
300 unsigned int cpu
= 0;
303 if (!atomic_read(&therm_throt_en
))
306 register_hotcpu_notifier(&thermal_throttle_cpu_notifier
);
308 #ifdef CONFIG_HOTPLUG_CPU
309 mutex_lock(&therm_cpu_lock
);
311 /* connect live CPUs to sysfs */
312 for_each_online_cpu(cpu
) {
313 err
= thermal_throttle_add_dev(get_cpu_sysdev(cpu
), cpu
);
316 #ifdef CONFIG_HOTPLUG_CPU
317 mutex_unlock(&therm_cpu_lock
);
322 device_initcall(thermal_throttle_init_device
);
324 #endif /* CONFIG_SYSFS */
327 * Set up the most two significant bit to notify mce log that this thermal
329 * This is a temp solution. May be changed in the future with mce log
332 #define CORE_THROTTLED (0)
333 #define CORE_POWER_LIMIT ((__u64)1 << 62)
334 #define PACKAGE_THROTTLED ((__u64)2 << 62)
335 #define PACKAGE_POWER_LIMIT ((__u64)3 << 62)
337 static void notify_thresholds(__u64 msr_val
)
339 /* check whether the interrupt handler is defined;
340 * otherwise simply return
342 if (!platform_thermal_notify
)
345 /* lower threshold reached */
346 if ((msr_val
& THERM_LOG_THRESHOLD0
) && thresh_event_valid(0))
347 platform_thermal_notify(msr_val
);
348 /* higher threshold reached */
349 if ((msr_val
& THERM_LOG_THRESHOLD1
) && thresh_event_valid(1))
350 platform_thermal_notify(msr_val
);
353 /* Thermal transition interrupt handler */
354 static void intel_thermal_interrupt(void)
358 rdmsrl(MSR_IA32_THERM_STATUS
, msr_val
);
360 /* Check for violation of core thermal thresholds*/
361 notify_thresholds(msr_val
);
363 if (therm_throt_process(msr_val
& THERM_STATUS_PROCHOT
,
364 THERMAL_THROTTLING_EVENT
,
366 mce_log_therm_throt_event(CORE_THROTTLED
| msr_val
);
368 if (this_cpu_has(X86_FEATURE_PLN
))
369 if (therm_throt_process(msr_val
& THERM_STATUS_POWER_LIMIT
,
372 mce_log_therm_throt_event(CORE_POWER_LIMIT
| msr_val
);
374 if (this_cpu_has(X86_FEATURE_PTS
)) {
375 rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS
, msr_val
);
376 if (therm_throt_process(msr_val
& PACKAGE_THERM_STATUS_PROCHOT
,
377 THERMAL_THROTTLING_EVENT
,
379 mce_log_therm_throt_event(PACKAGE_THROTTLED
| msr_val
);
380 if (this_cpu_has(X86_FEATURE_PLN
))
381 if (therm_throt_process(msr_val
&
382 PACKAGE_THERM_STATUS_POWER_LIMIT
,
385 mce_log_therm_throt_event(PACKAGE_POWER_LIMIT
390 static void unexpected_thermal_interrupt(void)
392 printk(KERN_ERR
"CPU%d: Unexpected LVT thermal interrupt!\n",
396 static void (*smp_thermal_vector
)(void) = unexpected_thermal_interrupt
;
398 asmlinkage
void smp_thermal_interrupt(struct pt_regs
*regs
)
402 inc_irq_stat(irq_thermal_count
);
403 smp_thermal_vector();
405 /* Ack only at the end to avoid potential reentry */
409 /* Thermal monitoring depends on APIC, ACPI and clock modulation */
410 static int intel_thermal_supported(struct cpuinfo_x86
*c
)
414 if (!cpu_has(c
, X86_FEATURE_ACPI
) || !cpu_has(c
, X86_FEATURE_ACC
))
419 void __init
mcheck_intel_therm_init(void)
422 * This function is only called on boot CPU. Save the init thermal
423 * LVT value on BSP and use that value to restore APs' thermal LVT
424 * entry BIOS programmed later
426 if (intel_thermal_supported(&boot_cpu_data
))
427 lvtthmr_init
= apic_read(APIC_LVTTHMR
);
430 void intel_init_thermal(struct cpuinfo_x86
*c
)
432 unsigned int cpu
= smp_processor_id();
436 if (!intel_thermal_supported(c
))
440 * First check if its enabled already, in which case there might
441 * be some SMM goo which handles it, so we can't even put a handler
442 * since it might be delivered via SMI already:
444 rdmsr(MSR_IA32_MISC_ENABLE
, l
, h
);
448 * The initial value of thermal LVT entries on all APs always reads
449 * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
450 * sequence to them and LVT registers are reset to 0s except for
451 * the mask bits which are set to 1s when APs receive INIT IPI.
452 * If BIOS takes over the thermal interrupt and sets its interrupt
453 * delivery mode to SMI (not fixed), it restores the value that the
454 * BIOS has programmed on AP based on BSP's info we saved since BIOS
455 * is always setting the same value for all threads/cores.
457 if ((h
& APIC_DM_FIXED_MASK
) != APIC_DM_FIXED
)
458 apic_write(APIC_LVTTHMR
, lvtthmr_init
);
461 if ((l
& MSR_IA32_MISC_ENABLE_TM1
) && (h
& APIC_DM_SMI
)) {
463 "CPU%d: Thermal monitoring handled by SMI\n", cpu
);
467 /* Check whether a vector already exists */
468 if (h
& APIC_VECTOR_MASK
) {
470 "CPU%d: Thermal LVT vector (%#x) already installed\n",
471 cpu
, (h
& APIC_VECTOR_MASK
));
475 /* early Pentium M models use different method for enabling TM2 */
476 if (cpu_has(c
, X86_FEATURE_TM2
)) {
477 if (c
->x86
== 6 && (c
->x86_model
== 9 || c
->x86_model
== 13)) {
478 rdmsr(MSR_THERM2_CTL
, l
, h
);
479 if (l
& MSR_THERM2_CTL_TM_SELECT
)
481 } else if (l
& MSR_IA32_MISC_ENABLE_TM2
)
485 /* We'll mask the thermal vector in the lapic till we're ready: */
486 h
= THERMAL_APIC_VECTOR
| APIC_DM_FIXED
| APIC_LVT_MASKED
;
487 apic_write(APIC_LVTTHMR
, h
);
489 rdmsr(MSR_IA32_THERM_INTERRUPT
, l
, h
);
490 if (cpu_has(c
, X86_FEATURE_PLN
))
491 wrmsr(MSR_IA32_THERM_INTERRUPT
,
492 l
| (THERM_INT_LOW_ENABLE
493 | THERM_INT_HIGH_ENABLE
| THERM_INT_PLN_ENABLE
), h
);
495 wrmsr(MSR_IA32_THERM_INTERRUPT
,
496 l
| (THERM_INT_LOW_ENABLE
| THERM_INT_HIGH_ENABLE
), h
);
498 if (cpu_has(c
, X86_FEATURE_PTS
)) {
499 rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
, l
, h
);
500 if (cpu_has(c
, X86_FEATURE_PLN
))
501 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
,
502 l
| (PACKAGE_THERM_INT_LOW_ENABLE
503 | PACKAGE_THERM_INT_HIGH_ENABLE
504 | PACKAGE_THERM_INT_PLN_ENABLE
), h
);
506 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
,
507 l
| (PACKAGE_THERM_INT_LOW_ENABLE
508 | PACKAGE_THERM_INT_HIGH_ENABLE
), h
);
511 smp_thermal_vector
= intel_thermal_interrupt
;
513 rdmsr(MSR_IA32_MISC_ENABLE
, l
, h
);
514 wrmsr(MSR_IA32_MISC_ENABLE
, l
| MSR_IA32_MISC_ENABLE_TM1
, h
);
516 /* Unmask the thermal vector: */
517 l
= apic_read(APIC_LVTTHMR
);
518 apic_write(APIC_LVTTHMR
, l
& ~APIC_LVT_MASKED
);
520 printk_once(KERN_INFO
"CPU0: Thermal monitoring enabled (%s)\n",
521 tm2
? "TM2" : "TM1");
523 /* enable thermal throttle processing */
524 atomic_set(&therm_throt_en
, 1);