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/syscore_ops.h>
18 #include <linux/interrupt.h>
19 #include <linux/notifier.h>
20 #include <linux/jiffies.h>
21 #include <linux/kernel.h>
22 #include <linux/percpu.h>
23 #include <linux/export.h>
24 #include <linux/delay.h>
25 #include <linux/types.h>
26 #include <linux/init.h>
27 #include <linux/smp.h>
28 #include <linux/sysfs.h>
29 #include <linux/cpu.h>
30
31 #include <asm/processor.h>
32 #include <asm/thermal.h>
33 #include <asm/traps.h>
34 #include <asm/apic.h>
35 #include <asm/irq.h>
36 #include <asm/msr.h>
37
38 #include "intel_hfi.h"
39 #include "thermal_interrupt.h"
40
41 /* How long to wait between reporting thermal events */
42 #define CHECK_INTERVAL (300 * HZ)
43
44 #define THERMAL_THROTTLING_EVENT 0
45 #define POWER_LIMIT_EVENT 1
46
47 /**
48 * struct _thermal_state - Represent the current thermal event state
49 * @next_check: Stores the next timestamp, when it is allowed
50 * to log the next warning message.
51 * @last_interrupt_time: Stores the timestamp for the last threshold
52 * high event.
53 * @therm_work: Delayed workqueue structure
54 * @count: Stores the current running count for thermal
55 * or power threshold interrupts.
56 * @last_count: Stores the previous running count for thermal
57 * or power threshold interrupts.
58 * @max_time_ms: This shows the maximum amount of time CPU was
59 * in throttled state for a single thermal
60 * threshold high to low state.
61 * @total_time_ms: This is a cumulative time during which CPU was
62 * in the throttled state.
63 * @rate_control_active: Set when a throttling message is logged.
64 * This is used for the purpose of rate-control.
65 * @new_event: Stores the last high/low status of the
66 * THERM_STATUS_PROCHOT or
67 * THERM_STATUS_POWER_LIMIT.
68 * @level: Stores whether this _thermal_state instance is
69 * for a CORE level or for PACKAGE level.
70 * @sample_index: Index for storing the next sample in the buffer
71 * temp_samples[].
72 * @sample_count: Total number of samples collected in the buffer
73 * temp_samples[].
74 * @average: The last moving average of temperature samples
75 * @baseline_temp: Temperature at which thermal threshold high
76 * interrupt was generated.
77 * @temp_samples: Storage for temperature samples to calculate
78 * moving average.
79 *
80 * This structure is used to represent data related to thermal state for a CPU.
81 * There is a separate storage for core and package level for each CPU.
82 */
83 struct _thermal_state {
84 u64 next_check;
85 u64 last_interrupt_time;
86 struct delayed_work therm_work;
87 unsigned long count;
88 unsigned long last_count;
89 unsigned long max_time_ms;
90 unsigned long total_time_ms;
91 bool rate_control_active;
92 bool new_event;
93 u8 level;
94 u8 sample_index;
95 u8 sample_count;
96 u8 average;
97 u8 baseline_temp;
98 u8 temp_samples[3];
99 };
100
101 struct thermal_state {
102 struct _thermal_state core_throttle;
103 struct _thermal_state core_power_limit;
104 struct _thermal_state package_throttle;
105 struct _thermal_state package_power_limit;
106 struct _thermal_state core_thresh0;
107 struct _thermal_state core_thresh1;
108 struct _thermal_state pkg_thresh0;
109 struct _thermal_state pkg_thresh1;
110 };
111
112 /* Callback to handle core threshold interrupts */
113 int (*platform_thermal_notify)(__u64 msr_val);
114 EXPORT_SYMBOL(platform_thermal_notify);
115
116 /* Callback to handle core package threshold_interrupts */
117 int (*platform_thermal_package_notify)(__u64 msr_val);
118 EXPORT_SYMBOL_GPL(platform_thermal_package_notify);
119
120 /* Callback support of rate control, return true, if
121 * callback has rate control */
122 bool (*platform_thermal_package_rate_control)(void);
123 EXPORT_SYMBOL_GPL(platform_thermal_package_rate_control);
124
125
126 static DEFINE_PER_CPU(struct thermal_state, thermal_state);
127
128 static atomic_t therm_throt_en = ATOMIC_INIT(0);
129
130 static u32 lvtthmr_init __read_mostly;
131
132 #ifdef CONFIG_SYSFS
133 #define define_therm_throt_device_one_ro(_name) \
134 static DEVICE_ATTR(_name, 0444, \
135 therm_throt_device_show_##_name, \
136 NULL) \
137
138 #define define_therm_throt_device_show_func(event, name) \
139 \
140 static ssize_t therm_throt_device_show_##event##_##name( \
141 struct device *dev, \
142 struct device_attribute *attr, \
143 char *buf) \
144 { \
145 unsigned int cpu = dev->id; \
146 ssize_t ret; \
147 \
148 preempt_disable(); /* CPU hotplug */ \
149 if (cpu_online(cpu)) { \
150 ret = sysfs_emit(buf, "%lu\n", \
151 per_cpu(thermal_state, cpu).event.name); \
152 } else \
153 ret = 0; \
154 preempt_enable(); \
155 \
156 return ret; \
157 }
158
159 define_therm_throt_device_show_func(core_throttle, count);
160 define_therm_throt_device_one_ro(core_throttle_count);
161
162 define_therm_throt_device_show_func(core_power_limit, count);
163 define_therm_throt_device_one_ro(core_power_limit_count);
164
165 define_therm_throt_device_show_func(package_throttle, count);
166 define_therm_throt_device_one_ro(package_throttle_count);
167
168 define_therm_throt_device_show_func(package_power_limit, count);
169 define_therm_throt_device_one_ro(package_power_limit_count);
170
171 define_therm_throt_device_show_func(core_throttle, max_time_ms);
172 define_therm_throt_device_one_ro(core_throttle_max_time_ms);
173
174 define_therm_throt_device_show_func(package_throttle, max_time_ms);
175 define_therm_throt_device_one_ro(package_throttle_max_time_ms);
176
177 define_therm_throt_device_show_func(core_throttle, total_time_ms);
178 define_therm_throt_device_one_ro(core_throttle_total_time_ms);
179
180 define_therm_throt_device_show_func(package_throttle, total_time_ms);
181 define_therm_throt_device_one_ro(package_throttle_total_time_ms);
182
183 static struct attribute *thermal_throttle_attrs[] = {
184 &dev_attr_core_throttle_count.attr,
185 &dev_attr_core_throttle_max_time_ms.attr,
186 &dev_attr_core_throttle_total_time_ms.attr,
187 NULL
188 };
189
190 static const struct attribute_group thermal_attr_group = {
191 .attrs = thermal_throttle_attrs,
192 .name = "thermal_throttle"
193 };
194 #endif /* CONFIG_SYSFS */
195
196 #define THERM_THROT_POLL_INTERVAL HZ
197 #define THERM_STATUS_PROCHOT_LOG BIT(1)
198
199 static u64 therm_intr_core_clear_mask;
200 static u64 therm_intr_pkg_clear_mask;
201
thermal_intr_init_core_clear_mask(void)202 static void thermal_intr_init_core_clear_mask(void)
203 {
204 if (therm_intr_core_clear_mask)
205 return;
206
207 /*
208 * Reference: Intel SDM Volume 4
209 * "Table 2-2. IA-32 Architectural MSRs", MSR 0x19C
210 * IA32_THERM_STATUS.
211 */
212
213 /*
214 * Bit 1, 3, 5: CPUID.01H:EDX[22] = 1. This driver will not
215 * enable interrupts, when 0 as it checks for X86_FEATURE_ACPI.
216 */
217 therm_intr_core_clear_mask = (BIT(1) | BIT(3) | BIT(5));
218
219 /*
220 * Bit 7 and 9: Thermal Threshold #1 and #2 log
221 * If CPUID.01H:ECX[8] = 1
222 */
223 if (boot_cpu_has(X86_FEATURE_TM2))
224 therm_intr_core_clear_mask |= (BIT(7) | BIT(9));
225
226 /* Bit 11: Power Limitation log (R/WC0) If CPUID.06H:EAX[4] = 1 */
227 if (boot_cpu_has(X86_FEATURE_PLN))
228 therm_intr_core_clear_mask |= BIT(11);
229
230 /*
231 * Bit 13: Current Limit log (R/WC0) If CPUID.06H:EAX[7] = 1
232 * Bit 15: Cross Domain Limit log (R/WC0) If CPUID.06H:EAX[7] = 1
233 */
234 if (boot_cpu_has(X86_FEATURE_HWP))
235 therm_intr_core_clear_mask |= (BIT(13) | BIT(15));
236 }
237
thermal_intr_init_pkg_clear_mask(void)238 static void thermal_intr_init_pkg_clear_mask(void)
239 {
240 if (therm_intr_pkg_clear_mask)
241 return;
242
243 /*
244 * Reference: Intel SDM Volume 4
245 * "Table 2-2. IA-32 Architectural MSRs", MSR 0x1B1
246 * IA32_PACKAGE_THERM_STATUS.
247 */
248
249 /* All bits except BITs 25 and 26 depend on CPUID.06H: EAX[6] = 1 */
250 if (boot_cpu_has(X86_FEATURE_PTS))
251 therm_intr_pkg_clear_mask = (BIT(1) | BIT(3) | BIT(5) | BIT(7) | BIT(9) | BIT(11));
252
253 /*
254 * Intel SDM Volume 1: Thermal and Power Management Leaf
255 * Bit 26: CPUID.06H: EAX[19] = 1
256 */
257 if (boot_cpu_has(X86_FEATURE_HFI))
258 therm_intr_pkg_clear_mask |= BIT(26);
259
260 /*
261 * Intel SDM Volume 1: Thermal and Power Management Leaf
262 * Bit 25: CPUID.06H: EAX[24] = 1
263 */
264 if (boot_cpu_has(X86_FEATURE_DPTI))
265 therm_intr_pkg_clear_mask |= BIT(25);
266 }
267
268 /*
269 * Clear the bits in package thermal status register for bit = 1
270 * in bitmask
271 */
thermal_clear_package_intr_status(int level,u64 bit_mask)272 void thermal_clear_package_intr_status(int level, u64 bit_mask)
273 {
274 u64 msr_val;
275 int msr;
276
277 if (level == CORE_LEVEL) {
278 msr = MSR_IA32_THERM_STATUS;
279 msr_val = therm_intr_core_clear_mask;
280 } else {
281 msr = MSR_IA32_PACKAGE_THERM_STATUS;
282 msr_val = therm_intr_pkg_clear_mask;
283 }
284
285 msr_val &= ~bit_mask;
286 wrmsrq(msr, msr_val);
287 }
288 EXPORT_SYMBOL_GPL(thermal_clear_package_intr_status);
289
get_therm_status(int level,bool * proc_hot,u8 * temp)290 static void get_therm_status(int level, bool *proc_hot, u8 *temp)
291 {
292 int msr;
293 u64 msr_val;
294
295 if (level == CORE_LEVEL)
296 msr = MSR_IA32_THERM_STATUS;
297 else
298 msr = MSR_IA32_PACKAGE_THERM_STATUS;
299
300 rdmsrq(msr, msr_val);
301 if (msr_val & THERM_STATUS_PROCHOT_LOG)
302 *proc_hot = true;
303 else
304 *proc_hot = false;
305
306 *temp = (msr_val >> 16) & 0x7F;
307 }
308
throttle_active_work(struct work_struct * work)309 static void __maybe_unused throttle_active_work(struct work_struct *work)
310 {
311 struct _thermal_state *state = container_of(to_delayed_work(work),
312 struct _thermal_state, therm_work);
313 unsigned int i, avg, this_cpu = smp_processor_id();
314 u64 now = get_jiffies_64();
315 bool hot;
316 u8 temp;
317
318 get_therm_status(state->level, &hot, &temp);
319 /* temperature value is offset from the max so lesser means hotter */
320 if (!hot && temp > state->baseline_temp) {
321 if (state->rate_control_active)
322 pr_info("CPU%d: %s temperature/speed normal (total events = %lu)\n",
323 this_cpu,
324 state->level == CORE_LEVEL ? "Core" : "Package",
325 state->count);
326
327 state->rate_control_active = false;
328 return;
329 }
330
331 if (time_before64(now, state->next_check) &&
332 state->rate_control_active)
333 goto re_arm;
334
335 state->next_check = now + CHECK_INTERVAL;
336
337 if (state->count != state->last_count) {
338 /* There was one new thermal interrupt */
339 state->last_count = state->count;
340 state->average = 0;
341 state->sample_count = 0;
342 state->sample_index = 0;
343 }
344
345 state->temp_samples[state->sample_index] = temp;
346 state->sample_count++;
347 state->sample_index = (state->sample_index + 1) % ARRAY_SIZE(state->temp_samples);
348 if (state->sample_count < ARRAY_SIZE(state->temp_samples))
349 goto re_arm;
350
351 avg = 0;
352 for (i = 0; i < ARRAY_SIZE(state->temp_samples); ++i)
353 avg += state->temp_samples[i];
354
355 avg /= ARRAY_SIZE(state->temp_samples);
356
357 if (state->average > avg) {
358 pr_warn("CPU%d: %s temperature is above threshold, cpu clock is throttled (total events = %lu)\n",
359 this_cpu,
360 state->level == CORE_LEVEL ? "Core" : "Package",
361 state->count);
362 state->rate_control_active = true;
363 }
364
365 state->average = avg;
366
367 re_arm:
368 thermal_clear_package_intr_status(state->level, THERM_STATUS_PROCHOT_LOG);
369 schedule_delayed_work_on(this_cpu, &state->therm_work, THERM_THROT_POLL_INTERVAL);
370 }
371
372 /***
373 * therm_throt_process - Process thermal throttling event from interrupt
374 * @curr: Whether the condition is current or not (boolean), since the
375 * thermal interrupt normally gets called both when the thermal
376 * event begins and once the event has ended.
377 *
378 * This function is called by the thermal interrupt after the
379 * IRQ has been acknowledged.
380 *
381 * It will take care of rate limiting and printing messages to the syslog.
382 */
therm_throt_process(bool new_event,int event,int level)383 static void therm_throt_process(bool new_event, int event, int level)
384 {
385 struct _thermal_state *state;
386 unsigned int this_cpu = smp_processor_id();
387 bool old_event;
388 u64 now;
389 struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
390
391 now = get_jiffies_64();
392 if (level == CORE_LEVEL) {
393 if (event == THERMAL_THROTTLING_EVENT)
394 state = &pstate->core_throttle;
395 else if (event == POWER_LIMIT_EVENT)
396 state = &pstate->core_power_limit;
397 else
398 return;
399 } else if (level == PACKAGE_LEVEL) {
400 if (event == THERMAL_THROTTLING_EVENT)
401 state = &pstate->package_throttle;
402 else if (event == POWER_LIMIT_EVENT)
403 state = &pstate->package_power_limit;
404 else
405 return;
406 } else
407 return;
408
409 old_event = state->new_event;
410 state->new_event = new_event;
411
412 if (new_event)
413 state->count++;
414
415 if (event != THERMAL_THROTTLING_EVENT)
416 return;
417
418 if (new_event && !state->last_interrupt_time) {
419 bool hot;
420 u8 temp;
421
422 get_therm_status(state->level, &hot, &temp);
423 /*
424 * Ignore short temperature spike as the system is not close
425 * to PROCHOT. 10C offset is large enough to ignore. It is
426 * already dropped from the high threshold temperature.
427 */
428 if (temp > 10)
429 return;
430
431 state->baseline_temp = temp;
432 state->last_interrupt_time = now;
433 schedule_delayed_work_on(this_cpu, &state->therm_work, THERM_THROT_POLL_INTERVAL);
434 } else if (old_event && state->last_interrupt_time) {
435 unsigned long throttle_time;
436
437 throttle_time = jiffies_delta_to_msecs(now - state->last_interrupt_time);
438 if (throttle_time > state->max_time_ms)
439 state->max_time_ms = throttle_time;
440 state->total_time_ms += throttle_time;
441 state->last_interrupt_time = 0;
442 }
443 }
444
thresh_event_valid(int level,int event)445 static int thresh_event_valid(int level, int event)
446 {
447 struct _thermal_state *state;
448 unsigned int this_cpu = smp_processor_id();
449 struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
450 u64 now = get_jiffies_64();
451
452 if (level == PACKAGE_LEVEL)
453 state = (event == 0) ? &pstate->pkg_thresh0 :
454 &pstate->pkg_thresh1;
455 else
456 state = (event == 0) ? &pstate->core_thresh0 :
457 &pstate->core_thresh1;
458
459 if (time_before64(now, state->next_check))
460 return 0;
461
462 state->next_check = now + CHECK_INTERVAL;
463
464 return 1;
465 }
466
467 static bool int_pln_enable;
int_pln_enable_setup(char * s)468 static int __init int_pln_enable_setup(char *s)
469 {
470 int_pln_enable = true;
471
472 return 1;
473 }
474 __setup("int_pln_enable", int_pln_enable_setup);
475
476 #ifdef CONFIG_SYSFS
477 /* Add/Remove thermal_throttle interface for CPU device: */
thermal_throttle_add_dev(struct device * dev,unsigned int cpu)478 static int thermal_throttle_add_dev(struct device *dev, unsigned int cpu)
479 {
480 int err;
481 struct cpuinfo_x86 *c = &cpu_data(cpu);
482
483 err = sysfs_create_group(&dev->kobj, &thermal_attr_group);
484 if (err)
485 return err;
486
487 if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) {
488 err = sysfs_add_file_to_group(&dev->kobj,
489 &dev_attr_core_power_limit_count.attr,
490 thermal_attr_group.name);
491 if (err)
492 goto del_group;
493 }
494
495 if (cpu_has(c, X86_FEATURE_PTS)) {
496 err = sysfs_add_file_to_group(&dev->kobj,
497 &dev_attr_package_throttle_count.attr,
498 thermal_attr_group.name);
499 if (err)
500 goto del_group;
501
502 err = sysfs_add_file_to_group(&dev->kobj,
503 &dev_attr_package_throttle_max_time_ms.attr,
504 thermal_attr_group.name);
505 if (err)
506 goto del_group;
507
508 err = sysfs_add_file_to_group(&dev->kobj,
509 &dev_attr_package_throttle_total_time_ms.attr,
510 thermal_attr_group.name);
511 if (err)
512 goto del_group;
513
514 if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) {
515 err = sysfs_add_file_to_group(&dev->kobj,
516 &dev_attr_package_power_limit_count.attr,
517 thermal_attr_group.name);
518 if (err)
519 goto del_group;
520 }
521 }
522
523 return 0;
524
525 del_group:
526 sysfs_remove_group(&dev->kobj, &thermal_attr_group);
527
528 return err;
529 }
530
thermal_throttle_remove_dev(struct device * dev)531 static void thermal_throttle_remove_dev(struct device *dev)
532 {
533 sysfs_remove_group(&dev->kobj, &thermal_attr_group);
534 }
535
check_directed_thermal_pkg_intr_ack(void)536 static int check_directed_thermal_pkg_intr_ack(void)
537 {
538 unsigned int count = 15000;
539 u64 msr_val;
540
541 /*
542 * Hardware acknowledges the directed interrupt setup in 10ms or less.
543 * Wait 15ms to be safe.
544 */
545 do {
546 rdmsrq(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
547 udelay(1);
548 } while (!(msr_val & PACKAGE_THERM_STATUS_DPTI_ACK) && --count);
549
550 if (!count)
551 return -ETIMEDOUT;
552
553 thermal_clear_package_intr_status(PACKAGE_LEVEL,
554 PACKAGE_THERM_STATUS_DPTI_ACK);
555
556 return 0;
557 }
558
config_directed_thermal_pkg_intr(void * info)559 static void config_directed_thermal_pkg_intr(void *info)
560 {
561 bool enable = *((bool *)info);
562 u64 msr_val;
563
564 rdmsrq(MSR_IA32_THERM_INTERRUPT, msr_val);
565
566 if (enable)
567 msr_val |= THERM_INT_DPTI_ENABLE;
568 else
569 msr_val &= ~THERM_INT_DPTI_ENABLE;
570
571 wrmsrq(MSR_IA32_THERM_INTERRUPT, msr_val);
572 }
573
574 /*
575 * Accessed from CPU hotplug callbacks and from code that runs while CPU
576 * hotplug is inactive: the init and cleanup paths as well as syscore callbacks.
577 * No extra locking needed.
578 */
579 static unsigned int *directed_intr_handler_cpus;
580
directed_thermal_pkg_intr_supported(void)581 static bool directed_thermal_pkg_intr_supported(void)
582 {
583 if (!boot_cpu_has(X86_FEATURE_DPTI))
584 return false;
585
586 if (!directed_intr_handler_cpus)
587 return false;
588
589 return true;
590 }
591
592 /*
593 * Must be called with cpu_hotplug_lock held to prevent CPUs from going offline
594 * while iterating through packages and interrupts must be enabled to avoid
595 * deadlocks in SMP function calls. The syscore shutdown callback also calls
596 * this function, but runs with CPU hotplug disabled (and interrupts enabled).
597 */
disable_directed_thermal_pkg_intr_all(void)598 static void disable_directed_thermal_pkg_intr_all(void)
599 {
600 bool enable = false;
601 int i;
602
603 if (!directed_thermal_pkg_intr_supported())
604 return;
605
606 for (i = 0; i < topology_max_packages(); i++) {
607 if (directed_intr_handler_cpus[i] == nr_cpu_ids)
608 continue;
609
610 smp_call_function_single(directed_intr_handler_cpus[i],
611 config_directed_thermal_pkg_intr,
612 &enable, true);
613 }
614 }
615
enable_directed_thermal_pkg_intr(unsigned int cpu)616 static int enable_directed_thermal_pkg_intr(unsigned int cpu)
617 {
618 bool enable = true;
619 u16 pkg_id;
620
621 if (!directed_thermal_pkg_intr_supported())
622 return 0;
623
624 pkg_id = topology_logical_package_id(cpu);
625 if (pkg_id >= topology_max_packages())
626 return -EINVAL;
627
628 /* Another CPU in this package already handles the directed interrupt. */
629 if (directed_intr_handler_cpus[pkg_id] != nr_cpu_ids)
630 return 0;
631
632 thermal_clear_package_intr_status(PACKAGE_LEVEL,
633 PACKAGE_THERM_STATUS_DPTI_ACK);
634
635 config_directed_thermal_pkg_intr(&enable);
636 if (!check_directed_thermal_pkg_intr_ack()) {
637 directed_intr_handler_cpus[pkg_id] = cpu;
638 return 0;
639 }
640
641 /*
642 * A failure indicates faulty hardware. Roll back completely so that
643 * no other CPU tries. This is especially important during boot as all
644 * CPUs may come online and would otherwise keep trying.
645 */
646 enable = false;
647 config_directed_thermal_pkg_intr(&enable);
648
649 return -ETIMEDOUT;
650 }
651
disable_directed_thermal_pkg_intr(unsigned int cpu)652 static void disable_directed_thermal_pkg_intr(unsigned int cpu)
653 {
654 unsigned int new_cpu;
655 bool enable;
656 u16 pkg_id;
657
658 if (!directed_thermal_pkg_intr_supported())
659 return;
660
661 pkg_id = topology_logical_package_id(cpu);
662 if (pkg_id >= topology_max_packages())
663 return;
664
665 /* Not the CPU handling the directed interrupt. */
666 if (directed_intr_handler_cpus[pkg_id] != cpu)
667 return;
668
669 /*
670 * The package-level interrupt must remain directed after this CPU goes
671 * offline.
672 */
673 new_cpu = cpumask_any_but(topology_core_cpumask(cpu), cpu);
674 if (new_cpu < nr_cpu_ids) {
675 enable = true;
676 thermal_clear_package_intr_status(PACKAGE_LEVEL,
677 PACKAGE_THERM_STATUS_DPTI_ACK);
678
679 /*
680 * We are here via CPU hotplug. Since we are holding the
681 * cpu_hotplug_lock, @new_cpu cannot go offline and interrupts
682 * are enabled, so the SMP function call is safe.
683 *
684 * The syscore suspend callback runs with interrupts disabled,
685 * but it does not reach this path because all the secondary
686 * CPUs are offline.
687 */
688 smp_call_function_single(new_cpu, config_directed_thermal_pkg_intr,
689 &enable, true);
690 }
691
692 /*
693 * If hardware does not acknowledge the directed interrupt setup on
694 * @new_cpu, disable the redirection. Since no other CPU is configured
695 * to receive the package-level interrupt, all CPUs in the package will
696 * receive it.
697 */
698 enable = false;
699 if (new_cpu < nr_cpu_ids && check_directed_thermal_pkg_intr_ack()) {
700 smp_call_function_single(new_cpu, config_directed_thermal_pkg_intr,
701 &enable, true);
702
703 pr_warn_once("Failed to redirect package thermal interrupt from CPU%u to CPU%u; reverting to broadcast.\n",
704 cpu, new_cpu);
705
706 new_cpu = nr_cpu_ids;
707 }
708
709 /*
710 * Clear the directed interrupt on @cpu. Hardware acknowledgment can be
711 * ignored since @cpu is going offline.
712 */
713 config_directed_thermal_pkg_intr(&enable);
714
715 directed_intr_handler_cpus[pkg_id] = (new_cpu < nr_cpu_ids) ? new_cpu : nr_cpu_ids;
716 }
717
718 /*
719 * CPU0 may be handling the directed interrupt, but the CPU hotplug callbacks
720 * are not called for CPU0 during suspend and resume.
721 */
directed_pkg_intr_syscore_resume(void * data)722 static void directed_pkg_intr_syscore_resume(void *data)
723 {
724 /*
725 * We can't do anything to handle errors. If direction fails for CPU0,
726 * another CPU will take over or disable direction entirely during CPU
727 * hotplug.
728 */
729 enable_directed_thermal_pkg_intr(0);
730 }
731
directed_pkg_intr_syscore_suspend(void * data)732 static int directed_pkg_intr_syscore_suspend(void *data)
733 {
734 disable_directed_thermal_pkg_intr(0);
735
736 return 0;
737 }
738
directed_pkg_intr_syscore_shutdown(void * data)739 static void directed_pkg_intr_syscore_shutdown(void *data)
740 {
741 disable_directed_thermal_pkg_intr_all();
742 }
743
744 static const struct syscore_ops directed_pkg_intr_pm_ops = {
745 .resume = directed_pkg_intr_syscore_resume,
746 .suspend = directed_pkg_intr_syscore_suspend,
747 .shutdown = directed_pkg_intr_syscore_shutdown,
748 };
749
750 static struct syscore directed_pkg_intr_pm = {
751 .ops = &directed_pkg_intr_pm_ops,
752 };
753
init_directed_pkg_intr(void)754 static __init void init_directed_pkg_intr(void)
755 {
756 int i;
757
758 if (!boot_cpu_has(X86_FEATURE_DPTI))
759 return;
760
761 directed_intr_handler_cpus = kmalloc_array(topology_max_packages(),
762 sizeof(*directed_intr_handler_cpus),
763 GFP_KERNEL);
764 if (!directed_intr_handler_cpus)
765 return;
766
767 for (i = 0; i < topology_max_packages(); i++)
768 directed_intr_handler_cpus[i] = nr_cpu_ids;
769
770 register_syscore(&directed_pkg_intr_pm);
771 }
772
cleanup_directed_pkg_thermal_intr(void)773 static void cleanup_directed_pkg_thermal_intr(void)
774 {
775 if (!directed_thermal_pkg_intr_supported())
776 return;
777
778 unregister_syscore(&directed_pkg_intr_pm);
779 disable_directed_thermal_pkg_intr_all();
780 kfree(directed_intr_handler_cpus);
781 directed_intr_handler_cpus = NULL;
782 }
783
784 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
thermal_throttle_online(unsigned int cpu)785 static int thermal_throttle_online(unsigned int cpu)
786 {
787 struct thermal_state *state = &per_cpu(thermal_state, cpu);
788 struct device *dev = get_cpu_device(cpu);
789 int err;
790 u32 l;
791
792 err = thermal_throttle_add_dev(dev, cpu);
793 if (err)
794 return err;
795
796 state->package_throttle.level = PACKAGE_LEVEL;
797 state->core_throttle.level = CORE_LEVEL;
798
799 INIT_DELAYED_WORK(&state->package_throttle.therm_work, throttle_active_work);
800 INIT_DELAYED_WORK(&state->core_throttle.therm_work, throttle_active_work);
801
802 /*
803 * The first CPU coming online will enable the HFI. Usually this causes
804 * hardware to issue an HFI thermal interrupt. Such interrupt will reach
805 * the CPU once we enable the thermal vector in the local APIC.
806 */
807 intel_hfi_online(cpu);
808
809 if (enable_directed_thermal_pkg_intr(cpu)) {
810 pr_info_once("Failed to direct package thermal interrupts. All CPUs will receive it.\n");
811 cleanup_directed_pkg_thermal_intr();
812 }
813
814 /* Unmask the thermal vector after the above workqueues are initialized. */
815 l = apic_read(APIC_LVTTHMR);
816 apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
817
818 return err;
819 }
820
thermal_throttle_offline(unsigned int cpu)821 static int thermal_throttle_offline(unsigned int cpu)
822 {
823 struct thermal_state *state = &per_cpu(thermal_state, cpu);
824 struct device *dev = get_cpu_device(cpu);
825 u32 l;
826
827 /* Mask the thermal vector before draining evtl. pending work */
828 l = apic_read(APIC_LVTTHMR);
829 apic_write(APIC_LVTTHMR, l | APIC_LVT_MASKED);
830
831 disable_directed_thermal_pkg_intr(cpu);
832
833 intel_hfi_offline(cpu);
834
835 cancel_delayed_work_sync(&state->package_throttle.therm_work);
836 cancel_delayed_work_sync(&state->core_throttle.therm_work);
837
838 state->package_throttle.rate_control_active = false;
839 state->core_throttle.rate_control_active = false;
840
841 thermal_throttle_remove_dev(dev);
842 return 0;
843 }
844
thermal_throttle_init_device(void)845 static __init int thermal_throttle_init_device(void)
846 {
847 int ret;
848
849 if (!atomic_read(&therm_throt_en))
850 return 0;
851
852 init_directed_pkg_intr();
853
854 intel_hfi_init();
855
856 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/therm:online",
857 thermal_throttle_online,
858 thermal_throttle_offline);
859 if (ret >= 0)
860 return 0;
861
862 cleanup_directed_pkg_thermal_intr();
863
864 return ret;
865 }
866 device_initcall(thermal_throttle_init_device);
867
868 #endif /* CONFIG_SYSFS */
869
notify_package_thresholds(__u64 msr_val)870 static void notify_package_thresholds(__u64 msr_val)
871 {
872 bool notify_thres_0 = false;
873 bool notify_thres_1 = false;
874
875 if (!platform_thermal_package_notify)
876 return;
877
878 /* lower threshold check */
879 if (msr_val & THERM_LOG_THRESHOLD0)
880 notify_thres_0 = true;
881 /* higher threshold check */
882 if (msr_val & THERM_LOG_THRESHOLD1)
883 notify_thres_1 = true;
884
885 if (!notify_thres_0 && !notify_thres_1)
886 return;
887
888 if (platform_thermal_package_rate_control &&
889 platform_thermal_package_rate_control()) {
890 /* Rate control is implemented in callback */
891 platform_thermal_package_notify(msr_val);
892 return;
893 }
894
895 /* lower threshold reached */
896 if (notify_thres_0 && thresh_event_valid(PACKAGE_LEVEL, 0))
897 platform_thermal_package_notify(msr_val);
898 /* higher threshold reached */
899 if (notify_thres_1 && thresh_event_valid(PACKAGE_LEVEL, 1))
900 platform_thermal_package_notify(msr_val);
901 }
902
notify_thresholds(__u64 msr_val)903 static void notify_thresholds(__u64 msr_val)
904 {
905 /* check whether the interrupt handler is defined;
906 * otherwise simply return
907 */
908 if (!platform_thermal_notify)
909 return;
910
911 /* lower threshold reached */
912 if ((msr_val & THERM_LOG_THRESHOLD0) &&
913 thresh_event_valid(CORE_LEVEL, 0))
914 platform_thermal_notify(msr_val);
915 /* higher threshold reached */
916 if ((msr_val & THERM_LOG_THRESHOLD1) &&
917 thresh_event_valid(CORE_LEVEL, 1))
918 platform_thermal_notify(msr_val);
919 }
920
notify_hwp_interrupt(void)921 void __weak notify_hwp_interrupt(void)
922 {
923 wrmsrq_safe(MSR_HWP_STATUS, 0);
924 }
925
926 /* Thermal transition interrupt handler */
intel_thermal_interrupt(void)927 void intel_thermal_interrupt(void)
928 {
929 __u64 msr_val;
930
931 if (cpu_feature_enabled(X86_FEATURE_HWP))
932 notify_hwp_interrupt();
933
934 rdmsrq(MSR_IA32_THERM_STATUS, msr_val);
935
936 /* Check for violation of core thermal thresholds*/
937 notify_thresholds(msr_val);
938
939 therm_throt_process(msr_val & THERM_STATUS_PROCHOT,
940 THERMAL_THROTTLING_EVENT,
941 CORE_LEVEL);
942
943 if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
944 therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT,
945 POWER_LIMIT_EVENT,
946 CORE_LEVEL);
947
948 if (this_cpu_has(X86_FEATURE_PTS)) {
949 rdmsrq(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
950 /* check violations of package thermal thresholds */
951 notify_package_thresholds(msr_val);
952 therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT,
953 THERMAL_THROTTLING_EVENT,
954 PACKAGE_LEVEL);
955 if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
956 therm_throt_process(msr_val &
957 PACKAGE_THERM_STATUS_POWER_LIMIT,
958 POWER_LIMIT_EVENT,
959 PACKAGE_LEVEL);
960
961 if (this_cpu_has(X86_FEATURE_HFI))
962 intel_hfi_process_event(msr_val &
963 PACKAGE_THERM_STATUS_HFI_UPDATED);
964 }
965 }
966
967 /* Thermal monitoring depends on APIC, ACPI and clock modulation */
intel_thermal_supported(struct cpuinfo_x86 * c)968 static int intel_thermal_supported(struct cpuinfo_x86 *c)
969 {
970 if (!boot_cpu_has(X86_FEATURE_APIC))
971 return 0;
972 if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
973 return 0;
974 return 1;
975 }
976
x86_thermal_enabled(void)977 bool x86_thermal_enabled(void)
978 {
979 return atomic_read(&therm_throt_en);
980 }
981
therm_lvt_init(void)982 void __init therm_lvt_init(void)
983 {
984 /*
985 * This function is only called on boot CPU. Save the init thermal
986 * LVT value on BSP and use that value to restore APs' thermal LVT
987 * entry BIOS programmed later
988 */
989 if (intel_thermal_supported(&boot_cpu_data))
990 lvtthmr_init = apic_read(APIC_LVTTHMR);
991 }
992
intel_init_thermal(struct cpuinfo_x86 * c)993 void intel_init_thermal(struct cpuinfo_x86 *c)
994 {
995 unsigned int cpu = smp_processor_id();
996 struct msr val;
997 int tm2 = 0;
998
999 if (!intel_thermal_supported(c))
1000 return;
1001
1002 /*
1003 * First check if its enabled already, in which case there might
1004 * be some SMM goo which handles it, so we can't even put a handler
1005 * since it might be delivered via SMI already:
1006 */
1007 rdmsrq(MSR_IA32_MISC_ENABLE, val.q);
1008
1009 val.h = lvtthmr_init;
1010 /*
1011 * The initial value of thermal LVT entries on all APs always reads
1012 * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
1013 * sequence to them and LVT registers are reset to 0s except for
1014 * the mask bits which are set to 1s when APs receive INIT IPI.
1015 * If BIOS takes over the thermal interrupt and sets its interrupt
1016 * delivery mode to SMI (not fixed), it restores the value that the
1017 * BIOS has programmed on AP based on BSP's info we saved since BIOS
1018 * is always setting the same value for all threads/cores.
1019 */
1020 if ((val.h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED)
1021 apic_write(APIC_LVTTHMR, lvtthmr_init);
1022
1023
1024 if ((val.l & MSR_IA32_MISC_ENABLE_TM1) && (val.h & APIC_DM_SMI)) {
1025 if (system_state == SYSTEM_BOOTING)
1026 pr_debug("CPU%d: Thermal monitoring handled by SMI\n", cpu);
1027 return;
1028 }
1029
1030 /* early Pentium M models use different method for enabling TM2 */
1031 if (cpu_has(c, X86_FEATURE_TM2)) {
1032 if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) {
1033 rdmsrq(MSR_THERM2_CTL, val.q);
1034 if (val.l & MSR_THERM2_CTL_TM_SELECT)
1035 tm2 = 1;
1036 } else if (val.l & MSR_IA32_MISC_ENABLE_TM2)
1037 tm2 = 1;
1038 }
1039
1040 /* We'll mask the thermal vector in the lapic till we're ready: */
1041 val.h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;
1042 apic_write(APIC_LVTTHMR, val.h);
1043
1044 thermal_intr_init_core_clear_mask();
1045 thermal_intr_init_pkg_clear_mask();
1046
1047 rdmsrq(MSR_IA32_THERM_INTERRUPT, val.q);
1048 if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable) {
1049 val.l |= THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE;
1050 val.l &= ~THERM_INT_PLN_ENABLE;
1051 } else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
1052 val.l |= THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE |
1053 THERM_INT_PLN_ENABLE;
1054 else
1055 val.l |= THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE;
1056 wrmsrq(MSR_IA32_THERM_INTERRUPT, val.q);
1057
1058 if (cpu_has(c, X86_FEATURE_PTS)) {
1059 rdmsrq(MSR_IA32_PACKAGE_THERM_INTERRUPT, val.q);
1060 if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable) {
1061 val.l |= PACKAGE_THERM_INT_LOW_ENABLE |
1062 PACKAGE_THERM_INT_HIGH_ENABLE;
1063 val.l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
1064 } else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
1065 val.l |= PACKAGE_THERM_INT_LOW_ENABLE |
1066 PACKAGE_THERM_INT_HIGH_ENABLE |
1067 PACKAGE_THERM_INT_PLN_ENABLE;
1068 else
1069 val.l |= PACKAGE_THERM_INT_LOW_ENABLE |
1070 PACKAGE_THERM_INT_HIGH_ENABLE;
1071 wrmsrq(MSR_IA32_PACKAGE_THERM_INTERRUPT, val.q);
1072
1073 if (cpu_has(c, X86_FEATURE_HFI)) {
1074 rdmsrq(MSR_IA32_PACKAGE_THERM_INTERRUPT, val.q);
1075 wrmsrq(MSR_IA32_PACKAGE_THERM_INTERRUPT,
1076 val.q | PACKAGE_THERM_INT_HFI_ENABLE);
1077 }
1078 }
1079
1080 rdmsrq(MSR_IA32_MISC_ENABLE, val.q);
1081 wrmsrq(MSR_IA32_MISC_ENABLE, val.q | MSR_IA32_MISC_ENABLE_TM1);
1082
1083 pr_info_once("CPU0: Thermal monitoring enabled (%s)\n",
1084 tm2 ? "TM2" : "TM1");
1085
1086 /* enable thermal throttle processing */
1087 atomic_set(&therm_throt_en, 1);
1088 }
1089