x86, mce: clean up therm_throt.c
[linux-2.6] / arch / x86 / kernel / cpu / mcheck / therm_throt.c
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/notifier.h>
17 #include <linux/jiffies.h>
18 #include <linux/percpu.h>
19 #include <linux/sysdev.h>
20 #include <linux/cpu.h>
21
22 #include <asm/therm_throt.h>
23 #include <asm/cpu.h>
24
25 /* How long to wait between reporting thermal events */
26 #define CHECK_INTERVAL          (300 * HZ)
27
28 static DEFINE_PER_CPU(__u64, next_check) = INITIAL_JIFFIES;
29 static DEFINE_PER_CPU(unsigned long, thermal_throttle_count);
30
31 atomic_t therm_throt_en         = ATOMIC_INIT(0);
32
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)
36
37 #define define_therm_throt_sysdev_show_func(name)                       \
38 static ssize_t therm_throt_sysdev_show_##name(struct sys_device *dev,   \
39                                         struct sysdev_attribute *attr,  \
40                                               char *buf)                \
41 {                                                                       \
42         unsigned int cpu = dev->id;                                     \
43         ssize_t ret;                                                    \
44                                                                         \
45         preempt_disable();      /* CPU hotplug */                       \
46         if (cpu_online(cpu))                                            \
47                 ret = sprintf(buf, "%lu\n",                             \
48                               per_cpu(thermal_throttle_##name, cpu));   \
49         else                                                            \
50                 ret = 0;                                                \
51         preempt_enable();                                               \
52                                                                         \
53         return ret;                                                     \
54 }
55
56 define_therm_throt_sysdev_show_func(count);
57 define_therm_throt_sysdev_one_ro(count);
58
59 static struct attribute *thermal_throttle_attrs[] = {
60         &attr_count.attr,
61         NULL
62 };
63
64 static struct attribute_group thermal_throttle_attr_group = {
65         .attrs  = thermal_throttle_attrs,
66         .name   = "thermal_throttle"
67 };
68 #endif /* CONFIG_SYSFS */
69
70 /***
71  * therm_throt_process - Process thermal throttling event from interrupt
72  * @curr: Whether the condition is current or not (boolean), since the
73  *        thermal interrupt normally gets called both when the thermal
74  *        event begins and once the event has ended.
75  *
76  * This function is called by the thermal interrupt after the
77  * IRQ has been acknowledged.
78  *
79  * It will take care of rate limiting and printing messages to the syslog.
80  *
81  * Returns: 0 : Event should NOT be further logged, i.e. still in
82  *              "timeout" from previous log message.
83  *          1 : Event should be logged further, and a message has been
84  *              printed to the syslog.
85  */
86 int therm_throt_process(int curr)
87 {
88         unsigned int cpu = smp_processor_id();
89         __u64 tmp_jiffs = get_jiffies_64();
90
91         if (curr)
92                 __get_cpu_var(thermal_throttle_count)++;
93
94         if (time_before64(tmp_jiffs, __get_cpu_var(next_check)))
95                 return 0;
96
97         __get_cpu_var(next_check) = tmp_jiffs + CHECK_INTERVAL;
98
99         /* if we just entered the thermal event */
100         if (curr) {
101                 printk(KERN_CRIT "CPU%d: Temperature above threshold, "
102                        "cpu clock throttled (total events = %lu)\n", cpu,
103                        __get_cpu_var(thermal_throttle_count));
104
105                 add_taint(TAINT_MACHINE_CHECK);
106         } else {
107                 printk(KERN_CRIT "CPU%d: Temperature/speed normal\n", cpu);
108         }
109
110         return 1;
111 }
112
113 #ifdef CONFIG_SYSFS
114 /* Add/Remove thermal_throttle interface for CPU device: */
115 static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev)
116 {
117         return sysfs_create_group(&sys_dev->kobj,
118                                   &thermal_throttle_attr_group);
119 }
120
121 static __cpuinit void thermal_throttle_remove_dev(struct sys_device *sys_dev)
122 {
123         sysfs_remove_group(&sys_dev->kobj, &thermal_throttle_attr_group);
124 }
125
126 /* Mutex protecting device creation against CPU hotplug: */
127 static DEFINE_MUTEX(therm_cpu_lock);
128
129 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
130 static __cpuinit int
131 thermal_throttle_cpu_callback(struct notifier_block *nfb,
132                               unsigned long action,
133                               void *hcpu)
134 {
135         unsigned int cpu = (unsigned long)hcpu;
136         struct sys_device *sys_dev;
137         int err = 0;
138
139         sys_dev = get_cpu_sysdev(cpu);
140
141         switch (action) {
142         case CPU_UP_PREPARE:
143         case CPU_UP_PREPARE_FROZEN:
144                 mutex_lock(&therm_cpu_lock);
145                 err = thermal_throttle_add_dev(sys_dev);
146                 mutex_unlock(&therm_cpu_lock);
147                 WARN_ON(err);
148                 break;
149         case CPU_UP_CANCELED:
150         case CPU_UP_CANCELED_FROZEN:
151         case CPU_DEAD:
152         case CPU_DEAD_FROZEN:
153                 mutex_lock(&therm_cpu_lock);
154                 thermal_throttle_remove_dev(sys_dev);
155                 mutex_unlock(&therm_cpu_lock);
156                 break;
157         }
158         return err ? NOTIFY_BAD : NOTIFY_OK;
159 }
160
161 static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata =
162 {
163         .notifier_call = thermal_throttle_cpu_callback,
164 };
165
166 static __init int thermal_throttle_init_device(void)
167 {
168         unsigned int cpu = 0;
169         int err;
170
171         if (!atomic_read(&therm_throt_en))
172                 return 0;
173
174         register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
175
176 #ifdef CONFIG_HOTPLUG_CPU
177         mutex_lock(&therm_cpu_lock);
178 #endif
179         /* connect live CPUs to sysfs */
180         for_each_online_cpu(cpu) {
181                 err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
182                 WARN_ON(err);
183         }
184 #ifdef CONFIG_HOTPLUG_CPU
185         mutex_unlock(&therm_cpu_lock);
186 #endif
187
188         return 0;
189 }
190
191 device_initcall(thermal_throttle_init_device);
192 #endif /* CONFIG_SYSFS */