HOTPLUG: Adapt thermal throttle to CPU_DYING
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / i386 / kernel / cpu / mcheck / therm_throt.c
blob1203dc5ab87aa36ec2042ed8249c76892be1b20c
1 /*
2 * linux/arch/i386/kernel/cpu/mcheck/therm_throt.c
4 * Thermal throttle event support code (such as syslog messaging and rate
5 * limiting) that was factored out from x86_64 (mce_intel.c) and i386 (p4.c).
6 * This allows consistent reporting of CPU thermal throttle events.
8 * Maintains a counter in /sys that keeps track of the number of thermal
9 * events, such that the user knows how bad the thermal problem might be
10 * (since the logging to syslog and mcelog is rate limited).
12 * Author: Dmitriy Zavin (dmitriyz@google.com)
14 * Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.
15 * Inspired by Ross Biro's and Al Borchers' counter code.
18 #include <linux/percpu.h>
19 #include <linux/sysdev.h>
20 #include <linux/cpu.h>
21 #include <asm/cpu.h>
22 #include <linux/notifier.h>
23 #include <linux/jiffies.h>
24 #include <asm/therm_throt.h>
26 /* How long to wait between reporting thermal events */
27 #define CHECK_INTERVAL (300 * HZ)
29 static DEFINE_PER_CPU(__u64, next_check) = INITIAL_JIFFIES;
30 static DEFINE_PER_CPU(unsigned long, thermal_throttle_count);
31 atomic_t therm_throt_en = ATOMIC_INIT(0);
33 #ifdef CONFIG_SYSFS
34 #define define_therm_throt_sysdev_one_ro(_name) \
35 static SYSDEV_ATTR(_name, 0444, therm_throt_sysdev_show_##_name, NULL)
37 #define define_therm_throt_sysdev_show_func(name) \
38 static ssize_t therm_throt_sysdev_show_##name(struct sys_device *dev, \
39 char *buf) \
40 { \
41 unsigned int cpu = dev->id; \
42 ssize_t ret; \
44 preempt_disable(); /* CPU hotplug */ \
45 if (cpu_online(cpu)) \
46 ret = sprintf(buf, "%lu\n", \
47 per_cpu(thermal_throttle_##name, cpu)); \
48 else \
49 ret = 0; \
50 preempt_enable(); \
52 return ret; \
55 define_therm_throt_sysdev_show_func(count);
56 define_therm_throt_sysdev_one_ro(count);
58 static struct attribute *thermal_throttle_attrs[] = {
59 &attr_count.attr,
60 NULL
63 static struct attribute_group thermal_throttle_attr_group = {
64 .attrs = thermal_throttle_attrs,
65 .name = "thermal_throttle"
67 #endif /* CONFIG_SYSFS */
69 /***
70 * therm_throt_process - Process thermal throttling event from interrupt
71 * @curr: Whether the condition is current or not (boolean), since the
72 * thermal interrupt normally gets called both when the thermal
73 * event begins and once the event has ended.
75 * This function is called by the thermal interrupt after the
76 * IRQ has been acknowledged.
78 * It will take care of rate limiting and printing messages to the syslog.
80 * Returns: 0 : Event should NOT be further logged, i.e. still in
81 * "timeout" from previous log message.
82 * 1 : Event should be logged further, and a message has been
83 * printed to the syslog.
85 int therm_throt_process(int curr)
87 unsigned int cpu = smp_processor_id();
88 __u64 tmp_jiffs = get_jiffies_64();
90 if (curr)
91 __get_cpu_var(thermal_throttle_count)++;
93 if (time_before64(tmp_jiffs, __get_cpu_var(next_check)))
94 return 0;
96 __get_cpu_var(next_check) = tmp_jiffs + CHECK_INTERVAL;
98 /* if we just entered the thermal event */
99 if (curr) {
100 printk(KERN_CRIT "CPU%d: Temperature above threshold, "
101 "cpu clock throttled (total events = %lu)\n", cpu,
102 __get_cpu_var(thermal_throttle_count));
104 add_taint(TAINT_MACHINE_CHECK);
105 } else {
106 printk(KERN_CRIT "CPU%d: Temperature/speed normal\n", cpu);
109 return 1;
112 #ifdef CONFIG_SYSFS
113 /* Add/Remove thermal_throttle interface for CPU device */
114 static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev)
116 return sysfs_create_group(&sys_dev->kobj, &thermal_throttle_attr_group);
119 static __cpuinit void thermal_throttle_remove_dev(struct sys_device *sys_dev)
121 return sysfs_remove_group(&sys_dev->kobj, &thermal_throttle_attr_group);
124 /* Mutex protecting device creation against CPU hotplug */
125 static DEFINE_MUTEX(therm_cpu_lock);
127 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
128 static __cpuinit int thermal_throttle_cpu_callback(struct notifier_block *nfb,
129 unsigned long action,
130 void *hcpu)
132 unsigned int cpu = (unsigned long)hcpu;
133 struct sys_device *sys_dev;
134 int err;
136 sys_dev = get_cpu_sysdev(cpu);
137 switch (action) {
138 case CPU_ONLINE:
139 case CPU_ONLINE_FROZEN:
140 mutex_lock(&therm_cpu_lock);
141 err = thermal_throttle_add_dev(sys_dev);
142 mutex_unlock(&therm_cpu_lock);
143 WARN_ON(err);
144 break;
145 case CPU_DEAD:
146 case CPU_DEAD_FROZEN:
147 mutex_lock(&therm_cpu_lock);
148 thermal_throttle_remove_dev(sys_dev);
149 mutex_unlock(&therm_cpu_lock);
150 break;
152 return NOTIFY_OK;
155 static struct notifier_block thermal_throttle_cpu_notifier =
157 .notifier_call = thermal_throttle_cpu_callback,
160 static __init int thermal_throttle_init_device(void)
162 unsigned int cpu = 0;
163 int err;
165 if (!atomic_read(&therm_throt_en))
166 return 0;
168 register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
170 #ifdef CONFIG_HOTPLUG_CPU
171 mutex_lock(&therm_cpu_lock);
172 #endif
173 /* connect live CPUs to sysfs */
174 for_each_online_cpu(cpu) {
175 err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
176 WARN_ON(err);
178 #ifdef CONFIG_HOTPLUG_CPU
179 mutex_unlock(&therm_cpu_lock);
180 #endif
182 return 0;
185 device_initcall(thermal_throttle_init_device);
186 #endif /* CONFIG_SYSFS */