Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/penberg...
[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/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>
26
27 #include <asm/processor.h>
28 #include <asm/system.h>
29 #include <asm/apic.h>
30 #include <asm/idle.h>
31 #include <asm/mce.h>
32 #include <asm/msr.h>
33
34 /* How long to wait between reporting thermal events */
35 #define CHECK_INTERVAL          (300 * HZ)
36
37 #define THERMAL_THROTTLING_EVENT        0
38 #define POWER_LIMIT_EVENT               1
39
40 /*
41  * Current thermal event state:
42  */
43 struct _thermal_state {
44         bool                    new_event;
45         int                     event;
46         u64                     next_check;
47         unsigned long           count;
48         unsigned long           last_count;
49 };
50
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;
56         struct _thermal_state core_thresh0;
57         struct _thermal_state core_thresh1;
58 };
59
60 /* Callback to handle core threshold interrupts */
61 int (*platform_thermal_notify)(__u64 msr_val);
62
63 static DEFINE_PER_CPU(struct thermal_state, thermal_state);
64
65 static atomic_t therm_throt_en  = ATOMIC_INIT(0);
66
67 static u32 lvtthmr_init __read_mostly;
68
69 #ifdef CONFIG_SYSFS
70 #define define_therm_throt_sysdev_one_ro(_name)                         \
71         static SYSDEV_ATTR(_name, 0444,                                 \
72                            therm_throt_sysdev_show_##_name,             \
73                                    NULL)                                \
74
75 #define define_therm_throt_sysdev_show_func(event, name)                \
76                                                                         \
77 static ssize_t therm_throt_sysdev_show_##event##_##name(                \
78                         struct sys_device *dev,                         \
79                         struct sysdev_attribute *attr,                  \
80                         char *buf)                                      \
81 {                                                                       \
82         unsigned int cpu = dev->id;                                     \
83         ssize_t ret;                                                    \
84                                                                         \
85         preempt_disable();      /* CPU hotplug */                       \
86         if (cpu_online(cpu)) {                                          \
87                 ret = sprintf(buf, "%lu\n",                             \
88                               per_cpu(thermal_state, cpu).event.name);  \
89         } else                                                          \
90                 ret = 0;                                                \
91         preempt_enable();                                               \
92                                                                         \
93         return ret;                                                     \
94 }
95
96 define_therm_throt_sysdev_show_func(core_throttle, count);
97 define_therm_throt_sysdev_one_ro(core_throttle_count);
98
99 define_therm_throt_sysdev_show_func(core_power_limit, count);
100 define_therm_throt_sysdev_one_ro(core_power_limit_count);
101
102 define_therm_throt_sysdev_show_func(package_throttle, count);
103 define_therm_throt_sysdev_one_ro(package_throttle_count);
104
105 define_therm_throt_sysdev_show_func(package_power_limit, count);
106 define_therm_throt_sysdev_one_ro(package_power_limit_count);
107
108 static struct attribute *thermal_throttle_attrs[] = {
109         &attr_core_throttle_count.attr,
110         NULL
111 };
112
113 static struct attribute_group thermal_attr_group = {
114         .attrs  = thermal_throttle_attrs,
115         .name   = "thermal_throttle"
116 };
117 #endif /* CONFIG_SYSFS */
118
119 #define CORE_LEVEL      0
120 #define PACKAGE_LEVEL   1
121
122 /***
123  * therm_throt_process - Process thermal throttling event from interrupt
124  * @curr: Whether the condition is current or not (boolean), since the
125  *        thermal interrupt normally gets called both when the thermal
126  *        event begins and once the event has ended.
127  *
128  * This function is called by the thermal interrupt after the
129  * IRQ has been acknowledged.
130  *
131  * It will take care of rate limiting and printing messages to the syslog.
132  *
133  * Returns: 0 : Event should NOT be further logged, i.e. still in
134  *              "timeout" from previous log message.
135  *          1 : Event should be logged further, and a message has been
136  *              printed to the syslog.
137  */
138 static int therm_throt_process(bool new_event, int event, int level)
139 {
140         struct _thermal_state *state;
141         unsigned int this_cpu = smp_processor_id();
142         bool old_event;
143         u64 now;
144         struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
145
146         now = get_jiffies_64();
147         if (level == CORE_LEVEL) {
148                 if (event == THERMAL_THROTTLING_EVENT)
149                         state = &pstate->core_throttle;
150                 else if (event == POWER_LIMIT_EVENT)
151                         state = &pstate->core_power_limit;
152                 else
153                          return 0;
154         } else if (level == PACKAGE_LEVEL) {
155                 if (event == THERMAL_THROTTLING_EVENT)
156                         state = &pstate->package_throttle;
157                 else if (event == POWER_LIMIT_EVENT)
158                         state = &pstate->package_power_limit;
159                 else
160                         return 0;
161         } else
162                 return 0;
163
164         old_event = state->new_event;
165         state->new_event = new_event;
166
167         if (new_event)
168                 state->count++;
169
170         if (time_before64(now, state->next_check) &&
171                         state->count != state->last_count)
172                 return 0;
173
174         state->next_check = now + CHECK_INTERVAL;
175         state->last_count = state->count;
176
177         /* if we just entered the thermal event */
178         if (new_event) {
179                 if (event == THERMAL_THROTTLING_EVENT)
180                         printk(KERN_CRIT "CPU%d: %s temperature above threshold, cpu clock throttled (total events = %lu)\n",
181                                 this_cpu,
182                                 level == CORE_LEVEL ? "Core" : "Package",
183                                 state->count);
184                 else
185                         printk(KERN_CRIT "CPU%d: %s power limit notification (total events = %lu)\n",
186                                 this_cpu,
187                                 level == CORE_LEVEL ? "Core" : "Package",
188                                 state->count);
189
190                 add_taint(TAINT_MACHINE_CHECK);
191                 return 1;
192         }
193         if (old_event) {
194                 if (event == THERMAL_THROTTLING_EVENT)
195                         printk(KERN_INFO "CPU%d: %s temperature/speed normal\n",
196                                 this_cpu,
197                                 level == CORE_LEVEL ? "Core" : "Package");
198                 else
199                         printk(KERN_INFO "CPU%d: %s power limit normal\n",
200                                 this_cpu,
201                                 level == CORE_LEVEL ? "Core" : "Package");
202                 return 1;
203         }
204
205         return 0;
206 }
207
208 static int thresh_event_valid(int event)
209 {
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();
214
215         state = (event == 0) ? &pstate->core_thresh0 : &pstate->core_thresh1;
216
217         if (time_before64(now, state->next_check))
218                 return 0;
219
220         state->next_check = now + CHECK_INTERVAL;
221         return 1;
222 }
223
224 #ifdef CONFIG_SYSFS
225 /* Add/Remove thermal_throttle interface for CPU device: */
226 static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev,
227                                 unsigned int cpu)
228 {
229         int err;
230         struct cpuinfo_x86 *c = &cpu_data(cpu);
231
232         err = sysfs_create_group(&sys_dev->kobj, &thermal_attr_group);
233         if (err)
234                 return err;
235
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);
248         }
249
250         return err;
251 }
252
253 static __cpuinit void thermal_throttle_remove_dev(struct sys_device *sys_dev)
254 {
255         sysfs_remove_group(&sys_dev->kobj, &thermal_attr_group);
256 }
257
258 /* Mutex protecting device creation against CPU hotplug: */
259 static DEFINE_MUTEX(therm_cpu_lock);
260
261 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
262 static __cpuinit int
263 thermal_throttle_cpu_callback(struct notifier_block *nfb,
264                               unsigned long action,
265                               void *hcpu)
266 {
267         unsigned int cpu = (unsigned long)hcpu;
268         struct sys_device *sys_dev;
269         int err = 0;
270
271         sys_dev = get_cpu_sysdev(cpu);
272
273         switch (action) {
274         case CPU_UP_PREPARE:
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);
279                 WARN_ON(err);
280                 break;
281         case CPU_UP_CANCELED:
282         case CPU_UP_CANCELED_FROZEN:
283         case CPU_DEAD:
284         case CPU_DEAD_FROZEN:
285                 mutex_lock(&therm_cpu_lock);
286                 thermal_throttle_remove_dev(sys_dev);
287                 mutex_unlock(&therm_cpu_lock);
288                 break;
289         }
290         return notifier_from_errno(err);
291 }
292
293 static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata =
294 {
295         .notifier_call = thermal_throttle_cpu_callback,
296 };
297
298 static __init int thermal_throttle_init_device(void)
299 {
300         unsigned int cpu = 0;
301         int err;
302
303         if (!atomic_read(&therm_throt_en))
304                 return 0;
305
306         register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
307
308 #ifdef CONFIG_HOTPLUG_CPU
309         mutex_lock(&therm_cpu_lock);
310 #endif
311         /* connect live CPUs to sysfs */
312         for_each_online_cpu(cpu) {
313                 err = thermal_throttle_add_dev(get_cpu_sysdev(cpu), cpu);
314                 WARN_ON(err);
315         }
316 #ifdef CONFIG_HOTPLUG_CPU
317         mutex_unlock(&therm_cpu_lock);
318 #endif
319
320         return 0;
321 }
322 device_initcall(thermal_throttle_init_device);
323
324 #endif /* CONFIG_SYSFS */
325
326 /*
327  * Set up the most two significant bit to notify mce log that this thermal
328  * event type.
329  * This is a temp solution. May be changed in the future with mce log
330  * infrasture.
331  */
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)
336
337 static void notify_thresholds(__u64 msr_val)
338 {
339         /* check whether the interrupt handler is defined;
340          * otherwise simply return
341          */
342         if (!platform_thermal_notify)
343                 return;
344
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);
351 }
352
353 /* Thermal transition interrupt handler */
354 static void intel_thermal_interrupt(void)
355 {
356         __u64 msr_val;
357         struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
358
359         rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
360
361         /* Check for violation of core thermal thresholds*/
362         notify_thresholds(msr_val);
363
364         if (therm_throt_process(msr_val & THERM_STATUS_PROCHOT,
365                                 THERMAL_THROTTLING_EVENT,
366                                 CORE_LEVEL) != 0)
367                 mce_log_therm_throt_event(CORE_THROTTLED | msr_val);
368
369         if (cpu_has(c, X86_FEATURE_PLN))
370                 if (therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT,
371                                         POWER_LIMIT_EVENT,
372                                         CORE_LEVEL) != 0)
373                         mce_log_therm_throt_event(CORE_POWER_LIMIT | msr_val);
374
375         if (cpu_has(c, X86_FEATURE_PTS)) {
376                 rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
377                 if (therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT,
378                                         THERMAL_THROTTLING_EVENT,
379                                         PACKAGE_LEVEL) != 0)
380                         mce_log_therm_throt_event(PACKAGE_THROTTLED | msr_val);
381                 if (cpu_has(c, X86_FEATURE_PLN))
382                         if (therm_throt_process(msr_val &
383                                         PACKAGE_THERM_STATUS_POWER_LIMIT,
384                                         POWER_LIMIT_EVENT,
385                                         PACKAGE_LEVEL) != 0)
386                                 mce_log_therm_throt_event(PACKAGE_POWER_LIMIT
387                                                           | msr_val);
388         }
389 }
390
391 static void unexpected_thermal_interrupt(void)
392 {
393         printk(KERN_ERR "CPU%d: Unexpected LVT thermal interrupt!\n",
394                         smp_processor_id());
395         add_taint(TAINT_MACHINE_CHECK);
396 }
397
398 static void (*smp_thermal_vector)(void) = unexpected_thermal_interrupt;
399
400 asmlinkage void smp_thermal_interrupt(struct pt_regs *regs)
401 {
402         exit_idle();
403         irq_enter();
404         inc_irq_stat(irq_thermal_count);
405         smp_thermal_vector();
406         irq_exit();
407         /* Ack only at the end to avoid potential reentry */
408         ack_APIC_irq();
409 }
410
411 /* Thermal monitoring depends on APIC, ACPI and clock modulation */
412 static int intel_thermal_supported(struct cpuinfo_x86 *c)
413 {
414         if (!cpu_has_apic)
415                 return 0;
416         if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
417                 return 0;
418         return 1;
419 }
420
421 void __init mcheck_intel_therm_init(void)
422 {
423         /*
424          * This function is only called on boot CPU. Save the init thermal
425          * LVT value on BSP and use that value to restore APs' thermal LVT
426          * entry BIOS programmed later
427          */
428         if (intel_thermal_supported(&boot_cpu_data))
429                 lvtthmr_init = apic_read(APIC_LVTTHMR);
430 }
431
432 void intel_init_thermal(struct cpuinfo_x86 *c)
433 {
434         unsigned int cpu = smp_processor_id();
435         int tm2 = 0;
436         u32 l, h;
437
438         if (!intel_thermal_supported(c))
439                 return;
440
441         /*
442          * First check if its enabled already, in which case there might
443          * be some SMM goo which handles it, so we can't even put a handler
444          * since it might be delivered via SMI already:
445          */
446         rdmsr(MSR_IA32_MISC_ENABLE, l, h);
447
448         /*
449          * The initial value of thermal LVT entries on all APs always reads
450          * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
451          * sequence to them and LVT registers are reset to 0s except for
452          * the mask bits which are set to 1s when APs receive INIT IPI.
453          * Always restore the value that BIOS has programmed on AP based on
454          * BSP's info we saved since BIOS is always setting the same value
455          * for all threads/cores
456          */
457         apic_write(APIC_LVTTHMR, lvtthmr_init);
458
459         h = lvtthmr_init;
460
461         if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {
462                 printk(KERN_DEBUG
463                        "CPU%d: Thermal monitoring handled by SMI\n", cpu);
464                 return;
465         }
466
467         /* Check whether a vector already exists */
468         if (h & APIC_VECTOR_MASK) {
469                 printk(KERN_DEBUG
470                        "CPU%d: Thermal LVT vector (%#x) already installed\n",
471                        cpu, (h & APIC_VECTOR_MASK));
472                 return;
473         }
474
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)
480                                 tm2 = 1;
481                 } else if (l & MSR_IA32_MISC_ENABLE_TM2)
482                         tm2 = 1;
483         }
484
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);
488
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);
494         else
495                 wrmsr(MSR_IA32_THERM_INTERRUPT,
496                       l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);
497
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);
505                 else
506                         wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
507                               l | (PACKAGE_THERM_INT_LOW_ENABLE
508                                 | PACKAGE_THERM_INT_HIGH_ENABLE), h);
509         }
510
511         smp_thermal_vector = intel_thermal_interrupt;
512
513         rdmsr(MSR_IA32_MISC_ENABLE, l, h);
514         wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);
515
516         /* Unmask the thermal vector: */
517         l = apic_read(APIC_LVTTHMR);
518         apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
519
520         printk_once(KERN_INFO "CPU0: Thermal monitoring enabled (%s)\n",
521                        tm2 ? "TM2" : "TM1");
522
523         /* enable thermal throttle processing */
524         atomic_set(&therm_throt_en, 1);
525 }