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