Merge tag 'driver-core-5.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / arch / x86 / kernel / cpu / mce / therm_throt.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Thermal throttle event support code (such as syslog messaging and rate
4  * limiting) that was factored out from x86_64 (mce_intel.c) and i386 (p4.c).
5  *
6  * This allows consistent reporting of CPU thermal throttle events.
7  *
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 is rate limited).
11  *
12  * Author: Dmitriy Zavin (dmitriyz@google.com)
13  *
14  * Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.
15  *          Inspired by Ross Biro's and Al Borchers' counter code.
16  */
17 #include <linux/interrupt.h>
18 #include <linux/notifier.h>
19 #include <linux/jiffies.h>
20 #include <linux/kernel.h>
21 #include <linux/percpu.h>
22 #include <linux/export.h>
23 #include <linux/types.h>
24 #include <linux/init.h>
25 #include <linux/smp.h>
26 #include <linux/cpu.h>
27
28 #include <asm/processor.h>
29 #include <asm/traps.h>
30 #include <asm/apic.h>
31 #include <asm/mce.h>
32 #include <asm/msr.h>
33 #include <asm/trace/irq_vectors.h>
34
35 #include "internal.h"
36
37 /* How long to wait between reporting thermal events */
38 #define CHECK_INTERVAL          (300 * HZ)
39
40 #define THERMAL_THROTTLING_EVENT        0
41 #define POWER_LIMIT_EVENT               1
42
43 /**
44  * struct _thermal_state - Represent the current thermal event state
45  * @next_check:                 Stores the next timestamp, when it is allowed
46  *                              to log the next warning message.
47  * @last_interrupt_time:        Stores the timestamp for the last threshold
48  *                              high event.
49  * @therm_work:                 Delayed workqueue structure
50  * @count:                      Stores the current running count for thermal
51  *                              or power threshold interrupts.
52  * @last_count:                 Stores the previous running count for thermal
53  *                              or power threshold interrupts.
54  * @max_time_ms:                This shows the maximum amount of time CPU was
55  *                              in throttled state for a single thermal
56  *                              threshold high to low state.
57  * @total_time_ms:              This is a cumulative time during which CPU was
58  *                              in the throttled state.
59  * @rate_control_active:        Set when a throttling message is logged.
60  *                              This is used for the purpose of rate-control.
61  * @new_event:                  Stores the last high/low status of the
62  *                              THERM_STATUS_PROCHOT or
63  *                              THERM_STATUS_POWER_LIMIT.
64  * @level:                      Stores whether this _thermal_state instance is
65  *                              for a CORE level or for PACKAGE level.
66  * @sample_index:               Index for storing the next sample in the buffer
67  *                              temp_samples[].
68  * @sample_count:               Total number of samples collected in the buffer
69  *                              temp_samples[].
70  * @average:                    The last moving average of temperature samples
71  * @baseline_temp:              Temperature at which thermal threshold high
72  *                              interrupt was generated.
73  * @temp_samples:               Storage for temperature samples to calculate
74  *                              moving average.
75  *
76  * This structure is used to represent data related to thermal state for a CPU.
77  * There is a separate storage for core and package level for each CPU.
78  */
79 struct _thermal_state {
80         u64                     next_check;
81         u64                     last_interrupt_time;
82         struct delayed_work     therm_work;
83         unsigned long           count;
84         unsigned long           last_count;
85         unsigned long           max_time_ms;
86         unsigned long           total_time_ms;
87         bool                    rate_control_active;
88         bool                    new_event;
89         u8                      level;
90         u8                      sample_index;
91         u8                      sample_count;
92         u8                      average;
93         u8                      baseline_temp;
94         u8                      temp_samples[3];
95 };
96
97 struct thermal_state {
98         struct _thermal_state core_throttle;
99         struct _thermal_state core_power_limit;
100         struct _thermal_state package_throttle;
101         struct _thermal_state package_power_limit;
102         struct _thermal_state core_thresh0;
103         struct _thermal_state core_thresh1;
104         struct _thermal_state pkg_thresh0;
105         struct _thermal_state pkg_thresh1;
106 };
107
108 /* Callback to handle core threshold interrupts */
109 int (*platform_thermal_notify)(__u64 msr_val);
110 EXPORT_SYMBOL(platform_thermal_notify);
111
112 /* Callback to handle core package threshold_interrupts */
113 int (*platform_thermal_package_notify)(__u64 msr_val);
114 EXPORT_SYMBOL_GPL(platform_thermal_package_notify);
115
116 /* Callback support of rate control, return true, if
117  * callback has rate control */
118 bool (*platform_thermal_package_rate_control)(void);
119 EXPORT_SYMBOL_GPL(platform_thermal_package_rate_control);
120
121
122 static DEFINE_PER_CPU(struct thermal_state, thermal_state);
123
124 static atomic_t therm_throt_en  = ATOMIC_INIT(0);
125
126 static u32 lvtthmr_init __read_mostly;
127
128 #ifdef CONFIG_SYSFS
129 #define define_therm_throt_device_one_ro(_name)                         \
130         static DEVICE_ATTR(_name, 0444,                                 \
131                            therm_throt_device_show_##_name,             \
132                                    NULL)                                \
133
134 #define define_therm_throt_device_show_func(event, name)                \
135                                                                         \
136 static ssize_t therm_throt_device_show_##event##_##name(                \
137                         struct device *dev,                             \
138                         struct device_attribute *attr,                  \
139                         char *buf)                                      \
140 {                                                                       \
141         unsigned int cpu = dev->id;                                     \
142         ssize_t ret;                                                    \
143                                                                         \
144         preempt_disable();      /* CPU hotplug */                       \
145         if (cpu_online(cpu)) {                                          \
146                 ret = sprintf(buf, "%lu\n",                             \
147                               per_cpu(thermal_state, cpu).event.name);  \
148         } else                                                          \
149                 ret = 0;                                                \
150         preempt_enable();                                               \
151                                                                         \
152         return ret;                                                     \
153 }
154
155 define_therm_throt_device_show_func(core_throttle, count);
156 define_therm_throt_device_one_ro(core_throttle_count);
157
158 define_therm_throt_device_show_func(core_power_limit, count);
159 define_therm_throt_device_one_ro(core_power_limit_count);
160
161 define_therm_throt_device_show_func(package_throttle, count);
162 define_therm_throt_device_one_ro(package_throttle_count);
163
164 define_therm_throt_device_show_func(package_power_limit, count);
165 define_therm_throt_device_one_ro(package_power_limit_count);
166
167 define_therm_throt_device_show_func(core_throttle, max_time_ms);
168 define_therm_throt_device_one_ro(core_throttle_max_time_ms);
169
170 define_therm_throt_device_show_func(package_throttle, max_time_ms);
171 define_therm_throt_device_one_ro(package_throttle_max_time_ms);
172
173 define_therm_throt_device_show_func(core_throttle, total_time_ms);
174 define_therm_throt_device_one_ro(core_throttle_total_time_ms);
175
176 define_therm_throt_device_show_func(package_throttle, total_time_ms);
177 define_therm_throt_device_one_ro(package_throttle_total_time_ms);
178
179 static struct attribute *thermal_throttle_attrs[] = {
180         &dev_attr_core_throttle_count.attr,
181         &dev_attr_core_throttle_max_time_ms.attr,
182         &dev_attr_core_throttle_total_time_ms.attr,
183         NULL
184 };
185
186 static const struct attribute_group thermal_attr_group = {
187         .attrs  = thermal_throttle_attrs,
188         .name   = "thermal_throttle"
189 };
190 #endif /* CONFIG_SYSFS */
191
192 #define CORE_LEVEL      0
193 #define PACKAGE_LEVEL   1
194
195 #define THERM_THROT_POLL_INTERVAL       HZ
196 #define THERM_STATUS_PROCHOT_LOG        BIT(1)
197
198 static void clear_therm_status_log(int level)
199 {
200         int msr;
201         u64 msr_val;
202
203         if (level == CORE_LEVEL)
204                 msr = MSR_IA32_THERM_STATUS;
205         else
206                 msr = MSR_IA32_PACKAGE_THERM_STATUS;
207
208         rdmsrl(msr, msr_val);
209         wrmsrl(msr, msr_val & ~THERM_STATUS_PROCHOT_LOG);
210 }
211
212 static void get_therm_status(int level, bool *proc_hot, u8 *temp)
213 {
214         int msr;
215         u64 msr_val;
216
217         if (level == CORE_LEVEL)
218                 msr = MSR_IA32_THERM_STATUS;
219         else
220                 msr = MSR_IA32_PACKAGE_THERM_STATUS;
221
222         rdmsrl(msr, msr_val);
223         if (msr_val & THERM_STATUS_PROCHOT_LOG)
224                 *proc_hot = true;
225         else
226                 *proc_hot = false;
227
228         *temp = (msr_val >> 16) & 0x7F;
229 }
230
231 static void throttle_active_work(struct work_struct *work)
232 {
233         struct _thermal_state *state = container_of(to_delayed_work(work),
234                                                 struct _thermal_state, therm_work);
235         unsigned int i, avg, this_cpu = smp_processor_id();
236         u64 now = get_jiffies_64();
237         bool hot;
238         u8 temp;
239
240         get_therm_status(state->level, &hot, &temp);
241         /* temperature value is offset from the max so lesser means hotter */
242         if (!hot && temp > state->baseline_temp) {
243                 if (state->rate_control_active)
244                         pr_info("CPU%d: %s temperature/speed normal (total events = %lu)\n",
245                                 this_cpu,
246                                 state->level == CORE_LEVEL ? "Core" : "Package",
247                                 state->count);
248
249                 state->rate_control_active = false;
250                 return;
251         }
252
253         if (time_before64(now, state->next_check) &&
254                           state->rate_control_active)
255                 goto re_arm;
256
257         state->next_check = now + CHECK_INTERVAL;
258
259         if (state->count != state->last_count) {
260                 /* There was one new thermal interrupt */
261                 state->last_count = state->count;
262                 state->average = 0;
263                 state->sample_count = 0;
264                 state->sample_index = 0;
265         }
266
267         state->temp_samples[state->sample_index] = temp;
268         state->sample_count++;
269         state->sample_index = (state->sample_index + 1) % ARRAY_SIZE(state->temp_samples);
270         if (state->sample_count < ARRAY_SIZE(state->temp_samples))
271                 goto re_arm;
272
273         avg = 0;
274         for (i = 0; i < ARRAY_SIZE(state->temp_samples); ++i)
275                 avg += state->temp_samples[i];
276
277         avg /= ARRAY_SIZE(state->temp_samples);
278
279         if (state->average > avg) {
280                 pr_warn("CPU%d: %s temperature is above threshold, cpu clock is throttled (total events = %lu)\n",
281                         this_cpu,
282                         state->level == CORE_LEVEL ? "Core" : "Package",
283                         state->count);
284                 state->rate_control_active = true;
285         }
286
287         state->average = avg;
288
289 re_arm:
290         clear_therm_status_log(state->level);
291         schedule_delayed_work_on(this_cpu, &state->therm_work, THERM_THROT_POLL_INTERVAL);
292 }
293
294 /***
295  * therm_throt_process - Process thermal throttling event from interrupt
296  * @curr: Whether the condition is current or not (boolean), since the
297  *        thermal interrupt normally gets called both when the thermal
298  *        event begins and once the event has ended.
299  *
300  * This function is called by the thermal interrupt after the
301  * IRQ has been acknowledged.
302  *
303  * It will take care of rate limiting and printing messages to the syslog.
304  */
305 static void therm_throt_process(bool new_event, int event, int level)
306 {
307         struct _thermal_state *state;
308         unsigned int this_cpu = smp_processor_id();
309         bool old_event;
310         u64 now;
311         struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
312
313         now = get_jiffies_64();
314         if (level == CORE_LEVEL) {
315                 if (event == THERMAL_THROTTLING_EVENT)
316                         state = &pstate->core_throttle;
317                 else if (event == POWER_LIMIT_EVENT)
318                         state = &pstate->core_power_limit;
319                 else
320                         return;
321         } else if (level == PACKAGE_LEVEL) {
322                 if (event == THERMAL_THROTTLING_EVENT)
323                         state = &pstate->package_throttle;
324                 else if (event == POWER_LIMIT_EVENT)
325                         state = &pstate->package_power_limit;
326                 else
327                         return;
328         } else
329                 return;
330
331         old_event = state->new_event;
332         state->new_event = new_event;
333
334         if (new_event)
335                 state->count++;
336
337         if (event != THERMAL_THROTTLING_EVENT)
338                 return;
339
340         if (new_event && !state->last_interrupt_time) {
341                 bool hot;
342                 u8 temp;
343
344                 get_therm_status(state->level, &hot, &temp);
345                 /*
346                  * Ignore short temperature spike as the system is not close
347                  * to PROCHOT. 10C offset is large enough to ignore. It is
348                  * already dropped from the high threshold temperature.
349                  */
350                 if (temp > 10)
351                         return;
352
353                 state->baseline_temp = temp;
354                 state->last_interrupt_time = now;
355                 schedule_delayed_work_on(this_cpu, &state->therm_work, THERM_THROT_POLL_INTERVAL);
356         } else if (old_event && state->last_interrupt_time) {
357                 unsigned long throttle_time;
358
359                 throttle_time = jiffies_delta_to_msecs(now - state->last_interrupt_time);
360                 if (throttle_time > state->max_time_ms)
361                         state->max_time_ms = throttle_time;
362                 state->total_time_ms += throttle_time;
363                 state->last_interrupt_time = 0;
364         }
365 }
366
367 static int thresh_event_valid(int level, int event)
368 {
369         struct _thermal_state *state;
370         unsigned int this_cpu = smp_processor_id();
371         struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
372         u64 now = get_jiffies_64();
373
374         if (level == PACKAGE_LEVEL)
375                 state = (event == 0) ? &pstate->pkg_thresh0 :
376                                                 &pstate->pkg_thresh1;
377         else
378                 state = (event == 0) ? &pstate->core_thresh0 :
379                                                 &pstate->core_thresh1;
380
381         if (time_before64(now, state->next_check))
382                 return 0;
383
384         state->next_check = now + CHECK_INTERVAL;
385
386         return 1;
387 }
388
389 static bool int_pln_enable;
390 static int __init int_pln_enable_setup(char *s)
391 {
392         int_pln_enable = true;
393
394         return 1;
395 }
396 __setup("int_pln_enable", int_pln_enable_setup);
397
398 #ifdef CONFIG_SYSFS
399 /* Add/Remove thermal_throttle interface for CPU device: */
400 static int thermal_throttle_add_dev(struct device *dev, unsigned int cpu)
401 {
402         int err;
403         struct cpuinfo_x86 *c = &cpu_data(cpu);
404
405         err = sysfs_create_group(&dev->kobj, &thermal_attr_group);
406         if (err)
407                 return err;
408
409         if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) {
410                 err = sysfs_add_file_to_group(&dev->kobj,
411                                               &dev_attr_core_power_limit_count.attr,
412                                               thermal_attr_group.name);
413                 if (err)
414                         goto del_group;
415         }
416
417         if (cpu_has(c, X86_FEATURE_PTS)) {
418                 err = sysfs_add_file_to_group(&dev->kobj,
419                                               &dev_attr_package_throttle_count.attr,
420                                               thermal_attr_group.name);
421                 if (err)
422                         goto del_group;
423
424                 err = sysfs_add_file_to_group(&dev->kobj,
425                                               &dev_attr_package_throttle_max_time_ms.attr,
426                                               thermal_attr_group.name);
427                 if (err)
428                         goto del_group;
429
430                 err = sysfs_add_file_to_group(&dev->kobj,
431                                               &dev_attr_package_throttle_total_time_ms.attr,
432                                               thermal_attr_group.name);
433                 if (err)
434                         goto del_group;
435
436                 if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) {
437                         err = sysfs_add_file_to_group(&dev->kobj,
438                                         &dev_attr_package_power_limit_count.attr,
439                                         thermal_attr_group.name);
440                         if (err)
441                                 goto del_group;
442                 }
443         }
444
445         return 0;
446
447 del_group:
448         sysfs_remove_group(&dev->kobj, &thermal_attr_group);
449
450         return err;
451 }
452
453 static void thermal_throttle_remove_dev(struct device *dev)
454 {
455         sysfs_remove_group(&dev->kobj, &thermal_attr_group);
456 }
457
458 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
459 static int thermal_throttle_online(unsigned int cpu)
460 {
461         struct thermal_state *state = &per_cpu(thermal_state, cpu);
462         struct device *dev = get_cpu_device(cpu);
463
464         state->package_throttle.level = PACKAGE_LEVEL;
465         state->core_throttle.level = CORE_LEVEL;
466
467         INIT_DELAYED_WORK(&state->package_throttle.therm_work, throttle_active_work);
468         INIT_DELAYED_WORK(&state->core_throttle.therm_work, throttle_active_work);
469
470         return thermal_throttle_add_dev(dev, cpu);
471 }
472
473 static int thermal_throttle_offline(unsigned int cpu)
474 {
475         struct thermal_state *state = &per_cpu(thermal_state, cpu);
476         struct device *dev = get_cpu_device(cpu);
477
478         cancel_delayed_work(&state->package_throttle.therm_work);
479         cancel_delayed_work(&state->core_throttle.therm_work);
480
481         state->package_throttle.rate_control_active = false;
482         state->core_throttle.rate_control_active = false;
483
484         thermal_throttle_remove_dev(dev);
485         return 0;
486 }
487
488 static __init int thermal_throttle_init_device(void)
489 {
490         int ret;
491
492         if (!atomic_read(&therm_throt_en))
493                 return 0;
494
495         ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/therm:online",
496                                 thermal_throttle_online,
497                                 thermal_throttle_offline);
498         return ret < 0 ? ret : 0;
499 }
500 device_initcall(thermal_throttle_init_device);
501
502 #endif /* CONFIG_SYSFS */
503
504 static void notify_package_thresholds(__u64 msr_val)
505 {
506         bool notify_thres_0 = false;
507         bool notify_thres_1 = false;
508
509         if (!platform_thermal_package_notify)
510                 return;
511
512         /* lower threshold check */
513         if (msr_val & THERM_LOG_THRESHOLD0)
514                 notify_thres_0 = true;
515         /* higher threshold check */
516         if (msr_val & THERM_LOG_THRESHOLD1)
517                 notify_thres_1 = true;
518
519         if (!notify_thres_0 && !notify_thres_1)
520                 return;
521
522         if (platform_thermal_package_rate_control &&
523                 platform_thermal_package_rate_control()) {
524                 /* Rate control is implemented in callback */
525                 platform_thermal_package_notify(msr_val);
526                 return;
527         }
528
529         /* lower threshold reached */
530         if (notify_thres_0 && thresh_event_valid(PACKAGE_LEVEL, 0))
531                 platform_thermal_package_notify(msr_val);
532         /* higher threshold reached */
533         if (notify_thres_1 && thresh_event_valid(PACKAGE_LEVEL, 1))
534                 platform_thermal_package_notify(msr_val);
535 }
536
537 static void notify_thresholds(__u64 msr_val)
538 {
539         /* check whether the interrupt handler is defined;
540          * otherwise simply return
541          */
542         if (!platform_thermal_notify)
543                 return;
544
545         /* lower threshold reached */
546         if ((msr_val & THERM_LOG_THRESHOLD0) &&
547                         thresh_event_valid(CORE_LEVEL, 0))
548                 platform_thermal_notify(msr_val);
549         /* higher threshold reached */
550         if ((msr_val & THERM_LOG_THRESHOLD1) &&
551                         thresh_event_valid(CORE_LEVEL, 1))
552                 platform_thermal_notify(msr_val);
553 }
554
555 /* Thermal transition interrupt handler */
556 static void intel_thermal_interrupt(void)
557 {
558         __u64 msr_val;
559
560         if (static_cpu_has(X86_FEATURE_HWP))
561                 wrmsrl_safe(MSR_HWP_STATUS, 0);
562
563         rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
564
565         /* Check for violation of core thermal thresholds*/
566         notify_thresholds(msr_val);
567
568         therm_throt_process(msr_val & THERM_STATUS_PROCHOT,
569                             THERMAL_THROTTLING_EVENT,
570                             CORE_LEVEL);
571
572         if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
573                 therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT,
574                                         POWER_LIMIT_EVENT,
575                                         CORE_LEVEL);
576
577         if (this_cpu_has(X86_FEATURE_PTS)) {
578                 rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
579                 /* check violations of package thermal thresholds */
580                 notify_package_thresholds(msr_val);
581                 therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT,
582                                         THERMAL_THROTTLING_EVENT,
583                                         PACKAGE_LEVEL);
584                 if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
585                         therm_throt_process(msr_val &
586                                         PACKAGE_THERM_STATUS_POWER_LIMIT,
587                                         POWER_LIMIT_EVENT,
588                                         PACKAGE_LEVEL);
589         }
590 }
591
592 static void unexpected_thermal_interrupt(void)
593 {
594         pr_err("CPU%d: Unexpected LVT thermal interrupt!\n",
595                 smp_processor_id());
596 }
597
598 static void (*smp_thermal_vector)(void) = unexpected_thermal_interrupt;
599
600 asmlinkage __visible void __irq_entry smp_thermal_interrupt(struct pt_regs *regs)
601 {
602         entering_irq();
603         trace_thermal_apic_entry(THERMAL_APIC_VECTOR);
604         inc_irq_stat(irq_thermal_count);
605         smp_thermal_vector();
606         trace_thermal_apic_exit(THERMAL_APIC_VECTOR);
607         exiting_ack_irq();
608 }
609
610 /* Thermal monitoring depends on APIC, ACPI and clock modulation */
611 static int intel_thermal_supported(struct cpuinfo_x86 *c)
612 {
613         if (!boot_cpu_has(X86_FEATURE_APIC))
614                 return 0;
615         if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
616                 return 0;
617         return 1;
618 }
619
620 void __init mcheck_intel_therm_init(void)
621 {
622         /*
623          * This function is only called on boot CPU. Save the init thermal
624          * LVT value on BSP and use that value to restore APs' thermal LVT
625          * entry BIOS programmed later
626          */
627         if (intel_thermal_supported(&boot_cpu_data))
628                 lvtthmr_init = apic_read(APIC_LVTTHMR);
629 }
630
631 void intel_init_thermal(struct cpuinfo_x86 *c)
632 {
633         unsigned int cpu = smp_processor_id();
634         int tm2 = 0;
635         u32 l, h;
636
637         if (!intel_thermal_supported(c))
638                 return;
639
640         /*
641          * First check if its enabled already, in which case there might
642          * be some SMM goo which handles it, so we can't even put a handler
643          * since it might be delivered via SMI already:
644          */
645         rdmsr(MSR_IA32_MISC_ENABLE, l, h);
646
647         h = lvtthmr_init;
648         /*
649          * The initial value of thermal LVT entries on all APs always reads
650          * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
651          * sequence to them and LVT registers are reset to 0s except for
652          * the mask bits which are set to 1s when APs receive INIT IPI.
653          * If BIOS takes over the thermal interrupt and sets its interrupt
654          * delivery mode to SMI (not fixed), it restores the value that the
655          * BIOS has programmed on AP based on BSP's info we saved since BIOS
656          * is always setting the same value for all threads/cores.
657          */
658         if ((h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED)
659                 apic_write(APIC_LVTTHMR, lvtthmr_init);
660
661
662         if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {
663                 if (system_state == SYSTEM_BOOTING)
664                         pr_debug("CPU%d: Thermal monitoring handled by SMI\n", cpu);
665                 return;
666         }
667
668         /* early Pentium M models use different method for enabling TM2 */
669         if (cpu_has(c, X86_FEATURE_TM2)) {
670                 if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) {
671                         rdmsr(MSR_THERM2_CTL, l, h);
672                         if (l & MSR_THERM2_CTL_TM_SELECT)
673                                 tm2 = 1;
674                 } else if (l & MSR_IA32_MISC_ENABLE_TM2)
675                         tm2 = 1;
676         }
677
678         /* We'll mask the thermal vector in the lapic till we're ready: */
679         h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;
680         apic_write(APIC_LVTTHMR, h);
681
682         rdmsr(MSR_IA32_THERM_INTERRUPT, l, h);
683         if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable)
684                 wrmsr(MSR_IA32_THERM_INTERRUPT,
685                         (l | (THERM_INT_LOW_ENABLE
686                         | THERM_INT_HIGH_ENABLE)) & ~THERM_INT_PLN_ENABLE, h);
687         else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
688                 wrmsr(MSR_IA32_THERM_INTERRUPT,
689                         l | (THERM_INT_LOW_ENABLE
690                         | THERM_INT_HIGH_ENABLE | THERM_INT_PLN_ENABLE), h);
691         else
692                 wrmsr(MSR_IA32_THERM_INTERRUPT,
693                       l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);
694
695         if (cpu_has(c, X86_FEATURE_PTS)) {
696                 rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
697                 if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable)
698                         wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
699                                 (l | (PACKAGE_THERM_INT_LOW_ENABLE
700                                 | PACKAGE_THERM_INT_HIGH_ENABLE))
701                                 & ~PACKAGE_THERM_INT_PLN_ENABLE, h);
702                 else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
703                         wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
704                                 l | (PACKAGE_THERM_INT_LOW_ENABLE
705                                 | PACKAGE_THERM_INT_HIGH_ENABLE
706                                 | PACKAGE_THERM_INT_PLN_ENABLE), h);
707                 else
708                         wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
709                               l | (PACKAGE_THERM_INT_LOW_ENABLE
710                                 | PACKAGE_THERM_INT_HIGH_ENABLE), h);
711         }
712
713         smp_thermal_vector = intel_thermal_interrupt;
714
715         rdmsr(MSR_IA32_MISC_ENABLE, l, h);
716         wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);
717
718         /* Unmask the thermal vector: */
719         l = apic_read(APIC_LVTTHMR);
720         apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
721
722         pr_info_once("CPU0: Thermal monitoring enabled (%s)\n",
723                       tm2 ? "TM2" : "TM1");
724
725         /* enable thermal throttle processing */
726         atomic_set(&therm_throt_en, 1);
727 }