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/sysdev.h>
22 #include <linux/types.h>
23 #include <linux/init.h>
24 #include <linux/smp.h>
25 #include <linux/cpu.h>
27 #include <asm/processor.h>
28 #include <asm/system.h>
34 /* How long to wait between reporting thermal events */
35 #define CHECK_INTERVAL (300 * HZ)
37 #define THERMAL_THROTTLING_EVENT 0
38 #define POWER_LIMIT_EVENT 1
41 * Current thermal event state:
43 struct _thermal_state
{
48 unsigned long last_count
;
51 struct thermal_state
{
52 struct _thermal_state core_throttle
;
53 struct _thermal_state core_power_limit
;
54 struct _thermal_state package_throttle
;
55 struct _thermal_state package_power_limit
;
58 static DEFINE_PER_CPU(struct thermal_state
, thermal_state
);
60 static atomic_t therm_throt_en
= ATOMIC_INIT(0);
62 static u32 lvtthmr_init __read_mostly
;
65 #define define_therm_throt_sysdev_one_ro(_name) \
66 static SYSDEV_ATTR(_name, 0444, \
67 therm_throt_sysdev_show_##_name, \
70 #define define_therm_throt_sysdev_show_func(event, name) \
72 static ssize_t therm_throt_sysdev_show_##event##_##name( \
73 struct sys_device *dev, \
74 struct sysdev_attribute *attr, \
77 unsigned int cpu = dev->id; \
80 preempt_disable(); /* CPU hotplug */ \
81 if (cpu_online(cpu)) { \
82 ret = sprintf(buf, "%lu\n", \
83 per_cpu(thermal_state, cpu).event.name); \
91 define_therm_throt_sysdev_show_func(core_throttle
, count
);
92 define_therm_throt_sysdev_one_ro(core_throttle_count
);
94 define_therm_throt_sysdev_show_func(core_power_limit
, count
);
95 define_therm_throt_sysdev_one_ro(core_power_limit_count
);
97 define_therm_throt_sysdev_show_func(package_throttle
, count
);
98 define_therm_throt_sysdev_one_ro(package_throttle_count
);
100 define_therm_throt_sysdev_show_func(package_power_limit
, count
);
101 define_therm_throt_sysdev_one_ro(package_power_limit_count
);
103 static struct attribute
*thermal_throttle_attrs
[] = {
104 &attr_core_throttle_count
.attr
,
108 static struct attribute_group thermal_attr_group
= {
109 .attrs
= thermal_throttle_attrs
,
110 .name
= "thermal_throttle"
112 #endif /* CONFIG_SYSFS */
115 #define PACKAGE_LEVEL 1
118 * therm_throt_process - Process thermal throttling event from interrupt
119 * @curr: Whether the condition is current or not (boolean), since the
120 * thermal interrupt normally gets called both when the thermal
121 * event begins and once the event has ended.
123 * This function is called by the thermal interrupt after the
124 * IRQ has been acknowledged.
126 * It will take care of rate limiting and printing messages to the syslog.
128 * Returns: 0 : Event should NOT be further logged, i.e. still in
129 * "timeout" from previous log message.
130 * 1 : Event should be logged further, and a message has been
131 * printed to the syslog.
133 static int therm_throt_process(bool new_event
, int event
, int level
)
135 struct _thermal_state
*state
;
136 unsigned int this_cpu
= smp_processor_id();
139 struct thermal_state
*pstate
= &per_cpu(thermal_state
, this_cpu
);
141 now
= get_jiffies_64();
142 if (level
== CORE_LEVEL
) {
143 if (event
== THERMAL_THROTTLING_EVENT
)
144 state
= &pstate
->core_throttle
;
145 else if (event
== POWER_LIMIT_EVENT
)
146 state
= &pstate
->core_power_limit
;
149 } else if (level
== PACKAGE_LEVEL
) {
150 if (event
== THERMAL_THROTTLING_EVENT
)
151 state
= &pstate
->package_throttle
;
152 else if (event
== POWER_LIMIT_EVENT
)
153 state
= &pstate
->package_power_limit
;
159 old_event
= state
->new_event
;
160 state
->new_event
= new_event
;
165 if (time_before64(now
, state
->next_check
) &&
166 state
->count
!= state
->last_count
)
169 state
->next_check
= now
+ CHECK_INTERVAL
;
170 state
->last_count
= state
->count
;
172 /* if we just entered the thermal event */
174 if (event
== THERMAL_THROTTLING_EVENT
)
175 printk(KERN_CRIT
"CPU%d: %s temperature above threshold, cpu clock throttled (total events = %lu)\n",
177 level
== CORE_LEVEL
? "Core" : "Package",
180 printk(KERN_CRIT
"CPU%d: %s power limit notification (total events = %lu)\n",
182 level
== CORE_LEVEL
? "Core" : "Package",
185 add_taint(TAINT_MACHINE_CHECK
);
189 if (event
== THERMAL_THROTTLING_EVENT
)
190 printk(KERN_INFO
"CPU%d: %s temperature/speed normal\n",
192 level
== CORE_LEVEL
? "Core" : "Package");
194 printk(KERN_INFO
"CPU%d: %s power limit normal\n",
196 level
== CORE_LEVEL
? "Core" : "Package");
204 /* Add/Remove thermal_throttle interface for CPU device: */
205 static __cpuinit
int thermal_throttle_add_dev(struct sys_device
*sys_dev
)
208 struct cpuinfo_x86
*c
= &cpu_data(smp_processor_id());
210 err
= sysfs_create_group(&sys_dev
->kobj
, &thermal_attr_group
);
214 if (cpu_has(c
, X86_FEATURE_PLN
))
215 err
= sysfs_add_file_to_group(&sys_dev
->kobj
,
216 &attr_core_power_limit_count
.attr
,
217 thermal_attr_group
.name
);
218 if (cpu_has(c
, X86_FEATURE_PTS
))
219 err
= sysfs_add_file_to_group(&sys_dev
->kobj
,
220 &attr_package_throttle_count
.attr
,
221 thermal_attr_group
.name
);
222 if (cpu_has(c
, X86_FEATURE_PLN
))
223 err
= sysfs_add_file_to_group(&sys_dev
->kobj
,
224 &attr_package_power_limit_count
.attr
,
225 thermal_attr_group
.name
);
230 static __cpuinit
void thermal_throttle_remove_dev(struct sys_device
*sys_dev
)
232 sysfs_remove_group(&sys_dev
->kobj
, &thermal_attr_group
);
235 /* Mutex protecting device creation against CPU hotplug: */
236 static DEFINE_MUTEX(therm_cpu_lock
);
238 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
240 thermal_throttle_cpu_callback(struct notifier_block
*nfb
,
241 unsigned long action
,
244 unsigned int cpu
= (unsigned long)hcpu
;
245 struct sys_device
*sys_dev
;
248 sys_dev
= get_cpu_sysdev(cpu
);
252 case CPU_UP_PREPARE_FROZEN
:
253 mutex_lock(&therm_cpu_lock
);
254 err
= thermal_throttle_add_dev(sys_dev
);
255 mutex_unlock(&therm_cpu_lock
);
258 case CPU_UP_CANCELED
:
259 case CPU_UP_CANCELED_FROZEN
:
261 case CPU_DEAD_FROZEN
:
262 mutex_lock(&therm_cpu_lock
);
263 thermal_throttle_remove_dev(sys_dev
);
264 mutex_unlock(&therm_cpu_lock
);
267 return notifier_from_errno(err
);
270 static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata
=
272 .notifier_call
= thermal_throttle_cpu_callback
,
275 static __init
int thermal_throttle_init_device(void)
277 unsigned int cpu
= 0;
280 if (!atomic_read(&therm_throt_en
))
283 register_hotcpu_notifier(&thermal_throttle_cpu_notifier
);
285 #ifdef CONFIG_HOTPLUG_CPU
286 mutex_lock(&therm_cpu_lock
);
288 /* connect live CPUs to sysfs */
289 for_each_online_cpu(cpu
) {
290 err
= thermal_throttle_add_dev(get_cpu_sysdev(cpu
));
293 #ifdef CONFIG_HOTPLUG_CPU
294 mutex_unlock(&therm_cpu_lock
);
299 device_initcall(thermal_throttle_init_device
);
301 #endif /* CONFIG_SYSFS */
304 * Set up the most two significant bit to notify mce log that this thermal
306 * This is a temp solution. May be changed in the future with mce log
309 #define CORE_THROTTLED (0)
310 #define CORE_POWER_LIMIT ((__u64)1 << 62)
311 #define PACKAGE_THROTTLED ((__u64)2 << 62)
312 #define PACKAGE_POWER_LIMIT ((__u64)3 << 62)
314 /* Thermal transition interrupt handler */
315 static void intel_thermal_interrupt(void)
318 struct cpuinfo_x86
*c
= &cpu_data(smp_processor_id());
320 rdmsrl(MSR_IA32_THERM_STATUS
, msr_val
);
322 if (therm_throt_process(msr_val
& THERM_STATUS_PROCHOT
,
323 THERMAL_THROTTLING_EVENT
,
325 mce_log_therm_throt_event(CORE_THROTTLED
| msr_val
);
327 if (cpu_has(c
, X86_FEATURE_PLN
))
328 if (therm_throt_process(msr_val
& THERM_STATUS_POWER_LIMIT
,
331 mce_log_therm_throt_event(CORE_POWER_LIMIT
| msr_val
);
333 if (cpu_has(c
, X86_FEATURE_PTS
)) {
334 rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS
, msr_val
);
335 if (therm_throt_process(msr_val
& PACKAGE_THERM_STATUS_PROCHOT
,
336 THERMAL_THROTTLING_EVENT
,
338 mce_log_therm_throt_event(PACKAGE_THROTTLED
| msr_val
);
339 if (cpu_has(c
, X86_FEATURE_PLN
))
340 if (therm_throt_process(msr_val
&
341 PACKAGE_THERM_STATUS_POWER_LIMIT
,
344 mce_log_therm_throt_event(PACKAGE_POWER_LIMIT
349 static void unexpected_thermal_interrupt(void)
351 printk(KERN_ERR
"CPU%d: Unexpected LVT TMR interrupt!\n",
353 add_taint(TAINT_MACHINE_CHECK
);
356 static void (*smp_thermal_vector
)(void) = unexpected_thermal_interrupt
;
358 asmlinkage
void smp_thermal_interrupt(struct pt_regs
*regs
)
362 inc_irq_stat(irq_thermal_count
);
363 smp_thermal_vector();
365 /* Ack only at the end to avoid potential reentry */
369 /* Thermal monitoring depends on APIC, ACPI and clock modulation */
370 static int intel_thermal_supported(struct cpuinfo_x86
*c
)
374 if (!cpu_has(c
, X86_FEATURE_ACPI
) || !cpu_has(c
, X86_FEATURE_ACC
))
379 void __init
mcheck_intel_therm_init(void)
382 * This function is only called on boot CPU. Save the init thermal
383 * LVT value on BSP and use that value to restore APs' thermal LVT
384 * entry BIOS programmed later
386 if (intel_thermal_supported(&boot_cpu_data
))
387 lvtthmr_init
= apic_read(APIC_LVTTHMR
);
390 void intel_init_thermal(struct cpuinfo_x86
*c
)
392 unsigned int cpu
= smp_processor_id();
396 if (!intel_thermal_supported(c
))
400 * First check if its enabled already, in which case there might
401 * be some SMM goo which handles it, so we can't even put a handler
402 * since it might be delivered via SMI already:
404 rdmsr(MSR_IA32_MISC_ENABLE
, l
, h
);
407 * The initial value of thermal LVT entries on all APs always reads
408 * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
409 * sequence to them and LVT registers are reset to 0s except for
410 * the mask bits which are set to 1s when APs receive INIT IPI.
411 * Always restore the value that BIOS has programmed on AP based on
412 * BSP's info we saved since BIOS is always setting the same value
413 * for all threads/cores
415 apic_write(APIC_LVTTHMR
, lvtthmr_init
);
419 if ((l
& MSR_IA32_MISC_ENABLE_TM1
) && (h
& APIC_DM_SMI
)) {
421 "CPU%d: Thermal monitoring handled by SMI\n", cpu
);
425 /* Check whether a vector already exists */
426 if (h
& APIC_VECTOR_MASK
) {
428 "CPU%d: Thermal LVT vector (%#x) already installed\n",
429 cpu
, (h
& APIC_VECTOR_MASK
));
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
)
439 } else if (l
& MSR_IA32_MISC_ENABLE_TM2
)
443 /* We'll mask the thermal vector in the lapic till we're ready: */
444 h
= THERMAL_APIC_VECTOR
| APIC_DM_FIXED
| APIC_LVT_MASKED
;
445 apic_write(APIC_LVTTHMR
, h
);
447 rdmsr(MSR_IA32_THERM_INTERRUPT
, l
, h
);
448 if (cpu_has(c
, X86_FEATURE_PLN
))
449 wrmsr(MSR_IA32_THERM_INTERRUPT
,
450 l
| (THERM_INT_LOW_ENABLE
451 | THERM_INT_HIGH_ENABLE
| THERM_INT_PLN_ENABLE
), h
);
453 wrmsr(MSR_IA32_THERM_INTERRUPT
,
454 l
| (THERM_INT_LOW_ENABLE
| THERM_INT_HIGH_ENABLE
), h
);
456 if (cpu_has(c
, X86_FEATURE_PTS
)) {
457 rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
, l
, h
);
458 if (cpu_has(c
, X86_FEATURE_PLN
))
459 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
,
460 l
| (PACKAGE_THERM_INT_LOW_ENABLE
461 | PACKAGE_THERM_INT_HIGH_ENABLE
462 | PACKAGE_THERM_INT_PLN_ENABLE
), h
);
464 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
,
465 l
| (PACKAGE_THERM_INT_LOW_ENABLE
466 | PACKAGE_THERM_INT_HIGH_ENABLE
), h
);
469 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
);
474 /* Unmask the thermal vector: */
475 l
= apic_read(APIC_LVTTHMR
);
476 apic_write(APIC_LVTTHMR
, l
& ~APIC_LVT_MASKED
);
478 printk_once(KERN_INFO
"CPU0: Thermal monitoring enabled (%s)\n",
479 tm2
? "TM2" : "TM1");
481 /* enable thermal throttle processing */
482 atomic_set(&therm_throt_en
, 1);