Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[pandora-kernel.git] / 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
24 /* How long to wait between reporting thermal events */
25 #define CHECK_INTERVAL          (300 * HZ)
26
27 static DEFINE_PER_CPU(__u64, next_check) = INITIAL_JIFFIES;
28 static DEFINE_PER_CPU(unsigned long, thermal_throttle_count);
29
30 atomic_t therm_throt_en         = ATOMIC_INIT(0);
31
32 #ifdef CONFIG_SYSFS
33 #define define_therm_throt_sysdev_one_ro(_name)                         \
34         static SYSDEV_ATTR(_name, 0444, therm_throt_sysdev_show_##_name, NULL)
35
36 #define define_therm_throt_sysdev_show_func(name)                       \
37 static ssize_t therm_throt_sysdev_show_##name(struct sys_device *dev,   \
38                                         struct sysdev_attribute *attr,  \
39                                               char *buf)                \
40 {                                                                       \
41         unsigned int cpu = dev->id;                                     \
42         ssize_t ret;                                                    \
43                                                                         \
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();                                               \
51                                                                         \
52         return ret;                                                     \
53 }
54
55 define_therm_throt_sysdev_show_func(count);
56 define_therm_throt_sysdev_one_ro(count);
57
58 static struct attribute *thermal_throttle_attrs[] = {
59         &attr_count.attr,
60         NULL
61 };
62
63 static struct attribute_group thermal_throttle_attr_group = {
64         .attrs  = thermal_throttle_attrs,
65         .name   = "thermal_throttle"
66 };
67 #endif /* CONFIG_SYSFS */
68
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.
74  *
75  * This function is called by the thermal interrupt after the
76  * IRQ has been acknowledged.
77  *
78  * It will take care of rate limiting and printing messages to the syslog.
79  *
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.
84  */
85 int therm_throt_process(int curr)
86 {
87         unsigned int cpu = smp_processor_id();
88         __u64 tmp_jiffs = get_jiffies_64();
89
90         if (curr)
91                 __get_cpu_var(thermal_throttle_count)++;
92
93         if (time_before64(tmp_jiffs, __get_cpu_var(next_check)))
94                 return 0;
95
96         __get_cpu_var(next_check) = tmp_jiffs + CHECK_INTERVAL;
97
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));
103
104                 add_taint(TAINT_MACHINE_CHECK);
105         } else {
106                 printk(KERN_CRIT "CPU%d: Temperature/speed normal\n", cpu);
107         }
108
109         return 1;
110 }
111
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)
115 {
116         return sysfs_create_group(&sys_dev->kobj,
117                                   &thermal_throttle_attr_group);
118 }
119
120 static __cpuinit void thermal_throttle_remove_dev(struct sys_device *sys_dev)
121 {
122         sysfs_remove_group(&sys_dev->kobj, &thermal_throttle_attr_group);
123 }
124
125 /* Mutex protecting device creation against CPU hotplug: */
126 static DEFINE_MUTEX(therm_cpu_lock);
127
128 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
129 static __cpuinit int
130 thermal_throttle_cpu_callback(struct notifier_block *nfb,
131                               unsigned long action,
132                               void *hcpu)
133 {
134         unsigned int cpu = (unsigned long)hcpu;
135         struct sys_device *sys_dev;
136         int err = 0;
137
138         sys_dev = get_cpu_sysdev(cpu);
139
140         switch (action) {
141         case CPU_UP_PREPARE:
142         case CPU_UP_PREPARE_FROZEN:
143                 mutex_lock(&therm_cpu_lock);
144                 err = thermal_throttle_add_dev(sys_dev);
145                 mutex_unlock(&therm_cpu_lock);
146                 WARN_ON(err);
147                 break;
148         case CPU_UP_CANCELED:
149         case CPU_UP_CANCELED_FROZEN:
150         case CPU_DEAD:
151         case CPU_DEAD_FROZEN:
152                 mutex_lock(&therm_cpu_lock);
153                 thermal_throttle_remove_dev(sys_dev);
154                 mutex_unlock(&therm_cpu_lock);
155                 break;
156         }
157         return err ? NOTIFY_BAD : NOTIFY_OK;
158 }
159
160 static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata =
161 {
162         .notifier_call = thermal_throttle_cpu_callback,
163 };
164
165 static __init int thermal_throttle_init_device(void)
166 {
167         unsigned int cpu = 0;
168         int err;
169
170         if (!atomic_read(&therm_throt_en))
171                 return 0;
172
173         register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
174
175 #ifdef CONFIG_HOTPLUG_CPU
176         mutex_lock(&therm_cpu_lock);
177 #endif
178         /* connect live CPUs to sysfs */
179         for_each_online_cpu(cpu) {
180                 err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
181                 WARN_ON(err);
182         }
183 #ifdef CONFIG_HOTPLUG_CPU
184         mutex_unlock(&therm_cpu_lock);
185 #endif
186
187         return 0;
188 }
189
190 device_initcall(thermal_throttle_init_device);
191 #endif /* CONFIG_SYSFS */