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 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 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 */ 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 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 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 */ 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 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; 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: */ 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 531 static void thermal_throttle_remove_dev(struct device *dev) 532 { 533 sysfs_remove_group(&dev->kobj, &thermal_attr_group); 534 } 535 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 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 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 */ 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 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 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 */ 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 732 static int directed_pkg_intr_syscore_suspend(void *data) 733 { 734 disable_directed_thermal_pkg_intr(0); 735 736 return 0; 737 } 738 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 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_objs(*directed_intr_handler_cpus, 762 topology_max_packages()); 763 if (!directed_intr_handler_cpus) 764 return; 765 766 for (i = 0; i < topology_max_packages(); i++) 767 directed_intr_handler_cpus[i] = nr_cpu_ids; 768 769 register_syscore(&directed_pkg_intr_pm); 770 } 771 772 static void cleanup_directed_pkg_thermal_intr(void) 773 { 774 if (!directed_thermal_pkg_intr_supported()) 775 return; 776 777 unregister_syscore(&directed_pkg_intr_pm); 778 disable_directed_thermal_pkg_intr_all(); 779 kfree(directed_intr_handler_cpus); 780 directed_intr_handler_cpus = NULL; 781 } 782 783 /* Get notified when a cpu comes on/off. Be hotplug friendly. */ 784 static int thermal_throttle_online(unsigned int cpu) 785 { 786 struct thermal_state *state = &per_cpu(thermal_state, cpu); 787 struct device *dev = get_cpu_device(cpu); 788 int err; 789 u32 l; 790 791 err = thermal_throttle_add_dev(dev, cpu); 792 if (err) 793 return err; 794 795 state->package_throttle.level = PACKAGE_LEVEL; 796 state->core_throttle.level = CORE_LEVEL; 797 798 INIT_DELAYED_WORK(&state->package_throttle.therm_work, throttle_active_work); 799 INIT_DELAYED_WORK(&state->core_throttle.therm_work, throttle_active_work); 800 801 /* 802 * The first CPU coming online will enable the HFI. Usually this causes 803 * hardware to issue an HFI thermal interrupt. Such interrupt will reach 804 * the CPU once we enable the thermal vector in the local APIC. 805 */ 806 intel_hfi_online(cpu); 807 808 if (enable_directed_thermal_pkg_intr(cpu)) { 809 pr_info_once("Failed to direct package thermal interrupts. All CPUs will receive it.\n"); 810 cleanup_directed_pkg_thermal_intr(); 811 } 812 813 /* Unmask the thermal vector after the above workqueues are initialized. */ 814 l = apic_read(APIC_LVTTHMR); 815 apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED); 816 817 return err; 818 } 819 820 static int thermal_throttle_offline(unsigned int cpu) 821 { 822 struct thermal_state *state = &per_cpu(thermal_state, cpu); 823 struct device *dev = get_cpu_device(cpu); 824 u32 l; 825 826 /* Mask the thermal vector before draining evtl. pending work */ 827 l = apic_read(APIC_LVTTHMR); 828 apic_write(APIC_LVTTHMR, l | APIC_LVT_MASKED); 829 830 disable_directed_thermal_pkg_intr(cpu); 831 832 intel_hfi_offline(cpu); 833 834 cancel_delayed_work_sync(&state->package_throttle.therm_work); 835 cancel_delayed_work_sync(&state->core_throttle.therm_work); 836 837 state->package_throttle.rate_control_active = false; 838 state->core_throttle.rate_control_active = false; 839 840 thermal_throttle_remove_dev(dev); 841 return 0; 842 } 843 844 static __init int thermal_throttle_init_device(void) 845 { 846 int ret; 847 848 if (!atomic_read(&therm_throt_en)) 849 return 0; 850 851 init_directed_pkg_intr(); 852 853 intel_hfi_init(); 854 855 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/therm:online", 856 thermal_throttle_online, 857 thermal_throttle_offline); 858 if (ret >= 0) 859 return 0; 860 861 cleanup_directed_pkg_thermal_intr(); 862 863 return ret; 864 } 865 device_initcall(thermal_throttle_init_device); 866 867 #endif /* CONFIG_SYSFS */ 868 869 static void notify_package_thresholds(__u64 msr_val) 870 { 871 bool notify_thres_0 = false; 872 bool notify_thres_1 = false; 873 874 if (!platform_thermal_package_notify) 875 return; 876 877 /* lower threshold check */ 878 if (msr_val & THERM_LOG_THRESHOLD0) 879 notify_thres_0 = true; 880 /* higher threshold check */ 881 if (msr_val & THERM_LOG_THRESHOLD1) 882 notify_thres_1 = true; 883 884 if (!notify_thres_0 && !notify_thres_1) 885 return; 886 887 if (platform_thermal_package_rate_control && 888 platform_thermal_package_rate_control()) { 889 /* Rate control is implemented in callback */ 890 platform_thermal_package_notify(msr_val); 891 return; 892 } 893 894 /* lower threshold reached */ 895 if (notify_thres_0 && thresh_event_valid(PACKAGE_LEVEL, 0)) 896 platform_thermal_package_notify(msr_val); 897 /* higher threshold reached */ 898 if (notify_thres_1 && thresh_event_valid(PACKAGE_LEVEL, 1)) 899 platform_thermal_package_notify(msr_val); 900 } 901 902 static void notify_thresholds(__u64 msr_val) 903 { 904 /* check whether the interrupt handler is defined; 905 * otherwise simply return 906 */ 907 if (!platform_thermal_notify) 908 return; 909 910 /* lower threshold reached */ 911 if ((msr_val & THERM_LOG_THRESHOLD0) && 912 thresh_event_valid(CORE_LEVEL, 0)) 913 platform_thermal_notify(msr_val); 914 /* higher threshold reached */ 915 if ((msr_val & THERM_LOG_THRESHOLD1) && 916 thresh_event_valid(CORE_LEVEL, 1)) 917 platform_thermal_notify(msr_val); 918 } 919 920 void __weak notify_hwp_interrupt(void) 921 { 922 wrmsrq_safe(MSR_HWP_STATUS, 0); 923 } 924 925 /* Thermal transition interrupt handler */ 926 void intel_thermal_interrupt(void) 927 { 928 __u64 msr_val; 929 930 if (cpu_feature_enabled(X86_FEATURE_HWP)) 931 notify_hwp_interrupt(); 932 933 rdmsrq(MSR_IA32_THERM_STATUS, msr_val); 934 935 /* Check for violation of core thermal thresholds*/ 936 notify_thresholds(msr_val); 937 938 therm_throt_process(msr_val & THERM_STATUS_PROCHOT, 939 THERMAL_THROTTLING_EVENT, 940 CORE_LEVEL); 941 942 if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable) 943 therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT, 944 POWER_LIMIT_EVENT, 945 CORE_LEVEL); 946 947 if (this_cpu_has(X86_FEATURE_PTS)) { 948 rdmsrq(MSR_IA32_PACKAGE_THERM_STATUS, msr_val); 949 /* check violations of package thermal thresholds */ 950 notify_package_thresholds(msr_val); 951 therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT, 952 THERMAL_THROTTLING_EVENT, 953 PACKAGE_LEVEL); 954 if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable) 955 therm_throt_process(msr_val & 956 PACKAGE_THERM_STATUS_POWER_LIMIT, 957 POWER_LIMIT_EVENT, 958 PACKAGE_LEVEL); 959 960 if (this_cpu_has(X86_FEATURE_HFI)) 961 intel_hfi_process_event(msr_val & 962 PACKAGE_THERM_STATUS_HFI_UPDATED); 963 } 964 } 965 966 /* Thermal monitoring depends on APIC, ACPI and clock modulation */ 967 static int intel_thermal_supported(struct cpuinfo_x86 *c) 968 { 969 if (!boot_cpu_has(X86_FEATURE_APIC)) 970 return 0; 971 if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC)) 972 return 0; 973 return 1; 974 } 975 976 bool x86_thermal_enabled(void) 977 { 978 return atomic_read(&therm_throt_en); 979 } 980 981 void __init therm_lvt_init(void) 982 { 983 /* 984 * This function is only called on boot CPU. Save the init thermal 985 * LVT value on BSP and use that value to restore APs' thermal LVT 986 * entry BIOS programmed later 987 */ 988 if (intel_thermal_supported(&boot_cpu_data)) 989 lvtthmr_init = apic_read(APIC_LVTTHMR); 990 } 991 992 void intel_init_thermal(struct cpuinfo_x86 *c) 993 { 994 unsigned int cpu = smp_processor_id(); 995 struct msr val; 996 int tm2 = 0; 997 998 if (!intel_thermal_supported(c)) 999 return; 1000 1001 /* 1002 * First check if its enabled already, in which case there might 1003 * be some SMM goo which handles it, so we can't even put a handler 1004 * since it might be delivered via SMI already: 1005 */ 1006 rdmsrq(MSR_IA32_MISC_ENABLE, val.q); 1007 1008 val.h = lvtthmr_init; 1009 /* 1010 * The initial value of thermal LVT entries on all APs always reads 1011 * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI 1012 * sequence to them and LVT registers are reset to 0s except for 1013 * the mask bits which are set to 1s when APs receive INIT IPI. 1014 * If BIOS takes over the thermal interrupt and sets its interrupt 1015 * delivery mode to SMI (not fixed), it restores the value that the 1016 * BIOS has programmed on AP based on BSP's info we saved since BIOS 1017 * is always setting the same value for all threads/cores. 1018 */ 1019 if ((val.h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED) 1020 apic_write(APIC_LVTTHMR, lvtthmr_init); 1021 1022 1023 if ((val.l & MSR_IA32_MISC_ENABLE_TM1) && (val.h & APIC_DM_SMI)) { 1024 if (system_state == SYSTEM_BOOTING) 1025 pr_debug("CPU%d: Thermal monitoring handled by SMI\n", cpu); 1026 return; 1027 } 1028 1029 /* early Pentium M models use different method for enabling TM2 */ 1030 if (cpu_has(c, X86_FEATURE_TM2)) { 1031 if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) { 1032 rdmsrq(MSR_THERM2_CTL, val.q); 1033 if (val.l & MSR_THERM2_CTL_TM_SELECT) 1034 tm2 = 1; 1035 } else if (val.l & MSR_IA32_MISC_ENABLE_TM2) 1036 tm2 = 1; 1037 } 1038 1039 /* We'll mask the thermal vector in the lapic till we're ready: */ 1040 val.h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED; 1041 apic_write(APIC_LVTTHMR, val.h); 1042 1043 thermal_intr_init_core_clear_mask(); 1044 thermal_intr_init_pkg_clear_mask(); 1045 1046 rdmsrq(MSR_IA32_THERM_INTERRUPT, val.q); 1047 if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable) { 1048 val.l |= THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE; 1049 val.l &= ~THERM_INT_PLN_ENABLE; 1050 } else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) 1051 val.l |= THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE | 1052 THERM_INT_PLN_ENABLE; 1053 else 1054 val.l |= THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE; 1055 wrmsrq(MSR_IA32_THERM_INTERRUPT, val.q); 1056 1057 if (cpu_has(c, X86_FEATURE_PTS)) { 1058 rdmsrq(MSR_IA32_PACKAGE_THERM_INTERRUPT, val.q); 1059 if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable) { 1060 val.l |= PACKAGE_THERM_INT_LOW_ENABLE | 1061 PACKAGE_THERM_INT_HIGH_ENABLE; 1062 val.l &= ~PACKAGE_THERM_INT_PLN_ENABLE; 1063 } else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) 1064 val.l |= PACKAGE_THERM_INT_LOW_ENABLE | 1065 PACKAGE_THERM_INT_HIGH_ENABLE | 1066 PACKAGE_THERM_INT_PLN_ENABLE; 1067 else 1068 val.l |= PACKAGE_THERM_INT_LOW_ENABLE | 1069 PACKAGE_THERM_INT_HIGH_ENABLE; 1070 wrmsrq(MSR_IA32_PACKAGE_THERM_INTERRUPT, val.q); 1071 1072 if (cpu_has(c, X86_FEATURE_HFI)) { 1073 rdmsrq(MSR_IA32_PACKAGE_THERM_INTERRUPT, val.q); 1074 wrmsrq(MSR_IA32_PACKAGE_THERM_INTERRUPT, 1075 val.q | PACKAGE_THERM_INT_HFI_ENABLE); 1076 } 1077 } 1078 1079 rdmsrq(MSR_IA32_MISC_ENABLE, val.q); 1080 wrmsrq(MSR_IA32_MISC_ENABLE, val.q | MSR_IA32_MISC_ENABLE_TM1); 1081 1082 pr_info_once("CPU0: Thermal monitoring enabled (%s)\n", 1083 tm2 ? "TM2" : "TM1"); 1084 1085 /* enable thermal throttle processing */ 1086 atomic_set(&therm_throt_en, 1); 1087 } 1088