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 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 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 */ 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 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 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 */ 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 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; 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: */ 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 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. */ 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 int err; 533 u32 l; 534 535 err = thermal_throttle_add_dev(dev, cpu); 536 if (err) 537 return err; 538 539 state->package_throttle.level = PACKAGE_LEVEL; 540 state->core_throttle.level = CORE_LEVEL; 541 542 INIT_DELAYED_WORK(&state->package_throttle.therm_work, throttle_active_work); 543 INIT_DELAYED_WORK(&state->core_throttle.therm_work, throttle_active_work); 544 545 /* 546 * The first CPU coming online will enable the HFI. Usually this causes 547 * hardware to issue an HFI thermal interrupt. Such interrupt will reach 548 * the CPU once we enable the thermal vector in the local APIC. 549 */ 550 intel_hfi_online(cpu); 551 552 /* Unmask the thermal vector after the above workqueues are initialized. */ 553 l = apic_read(APIC_LVTTHMR); 554 apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED); 555 556 return err; 557 } 558 559 static int thermal_throttle_offline(unsigned int cpu) 560 { 561 struct thermal_state *state = &per_cpu(thermal_state, cpu); 562 struct device *dev = get_cpu_device(cpu); 563 u32 l; 564 565 /* Mask the thermal vector before draining evtl. pending work */ 566 l = apic_read(APIC_LVTTHMR); 567 apic_write(APIC_LVTTHMR, l | APIC_LVT_MASKED); 568 569 intel_hfi_offline(cpu); 570 571 cancel_delayed_work_sync(&state->package_throttle.therm_work); 572 cancel_delayed_work_sync(&state->core_throttle.therm_work); 573 574 state->package_throttle.rate_control_active = false; 575 state->core_throttle.rate_control_active = false; 576 577 thermal_throttle_remove_dev(dev); 578 return 0; 579 } 580 581 static __init int thermal_throttle_init_device(void) 582 { 583 int ret; 584 585 if (!atomic_read(&therm_throt_en)) 586 return 0; 587 588 intel_hfi_init(); 589 590 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/therm:online", 591 thermal_throttle_online, 592 thermal_throttle_offline); 593 return ret < 0 ? ret : 0; 594 } 595 device_initcall(thermal_throttle_init_device); 596 597 #endif /* CONFIG_SYSFS */ 598 599 static void notify_package_thresholds(__u64 msr_val) 600 { 601 bool notify_thres_0 = false; 602 bool notify_thres_1 = false; 603 604 if (!platform_thermal_package_notify) 605 return; 606 607 /* lower threshold check */ 608 if (msr_val & THERM_LOG_THRESHOLD0) 609 notify_thres_0 = true; 610 /* higher threshold check */ 611 if (msr_val & THERM_LOG_THRESHOLD1) 612 notify_thres_1 = true; 613 614 if (!notify_thres_0 && !notify_thres_1) 615 return; 616 617 if (platform_thermal_package_rate_control && 618 platform_thermal_package_rate_control()) { 619 /* Rate control is implemented in callback */ 620 platform_thermal_package_notify(msr_val); 621 return; 622 } 623 624 /* lower threshold reached */ 625 if (notify_thres_0 && thresh_event_valid(PACKAGE_LEVEL, 0)) 626 platform_thermal_package_notify(msr_val); 627 /* higher threshold reached */ 628 if (notify_thres_1 && thresh_event_valid(PACKAGE_LEVEL, 1)) 629 platform_thermal_package_notify(msr_val); 630 } 631 632 static void notify_thresholds(__u64 msr_val) 633 { 634 /* check whether the interrupt handler is defined; 635 * otherwise simply return 636 */ 637 if (!platform_thermal_notify) 638 return; 639 640 /* lower threshold reached */ 641 if ((msr_val & THERM_LOG_THRESHOLD0) && 642 thresh_event_valid(CORE_LEVEL, 0)) 643 platform_thermal_notify(msr_val); 644 /* higher threshold reached */ 645 if ((msr_val & THERM_LOG_THRESHOLD1) && 646 thresh_event_valid(CORE_LEVEL, 1)) 647 platform_thermal_notify(msr_val); 648 } 649 650 void __weak notify_hwp_interrupt(void) 651 { 652 wrmsrq_safe(MSR_HWP_STATUS, 0); 653 } 654 655 /* Thermal transition interrupt handler */ 656 void intel_thermal_interrupt(void) 657 { 658 __u64 msr_val; 659 660 if (static_cpu_has(X86_FEATURE_HWP)) 661 notify_hwp_interrupt(); 662 663 rdmsrq(MSR_IA32_THERM_STATUS, msr_val); 664 665 /* Check for violation of core thermal thresholds*/ 666 notify_thresholds(msr_val); 667 668 therm_throt_process(msr_val & THERM_STATUS_PROCHOT, 669 THERMAL_THROTTLING_EVENT, 670 CORE_LEVEL); 671 672 if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable) 673 therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT, 674 POWER_LIMIT_EVENT, 675 CORE_LEVEL); 676 677 if (this_cpu_has(X86_FEATURE_PTS)) { 678 rdmsrq(MSR_IA32_PACKAGE_THERM_STATUS, msr_val); 679 /* check violations of package thermal thresholds */ 680 notify_package_thresholds(msr_val); 681 therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT, 682 THERMAL_THROTTLING_EVENT, 683 PACKAGE_LEVEL); 684 if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable) 685 therm_throt_process(msr_val & 686 PACKAGE_THERM_STATUS_POWER_LIMIT, 687 POWER_LIMIT_EVENT, 688 PACKAGE_LEVEL); 689 690 if (this_cpu_has(X86_FEATURE_HFI)) 691 intel_hfi_process_event(msr_val & 692 PACKAGE_THERM_STATUS_HFI_UPDATED); 693 } 694 } 695 696 /* Thermal monitoring depends on APIC, ACPI and clock modulation */ 697 static int intel_thermal_supported(struct cpuinfo_x86 *c) 698 { 699 if (!boot_cpu_has(X86_FEATURE_APIC)) 700 return 0; 701 if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC)) 702 return 0; 703 return 1; 704 } 705 706 bool x86_thermal_enabled(void) 707 { 708 return atomic_read(&therm_throt_en); 709 } 710 711 void __init therm_lvt_init(void) 712 { 713 /* 714 * This function is only called on boot CPU. Save the init thermal 715 * LVT value on BSP and use that value to restore APs' thermal LVT 716 * entry BIOS programmed later 717 */ 718 if (intel_thermal_supported(&boot_cpu_data)) 719 lvtthmr_init = apic_read(APIC_LVTTHMR); 720 } 721 722 void intel_init_thermal(struct cpuinfo_x86 *c) 723 { 724 unsigned int cpu = smp_processor_id(); 725 int tm2 = 0; 726 u32 l, h; 727 728 if (!intel_thermal_supported(c)) 729 return; 730 731 /* 732 * First check if its enabled already, in which case there might 733 * be some SMM goo which handles it, so we can't even put a handler 734 * since it might be delivered via SMI already: 735 */ 736 rdmsr(MSR_IA32_MISC_ENABLE, l, h); 737 738 h = lvtthmr_init; 739 /* 740 * The initial value of thermal LVT entries on all APs always reads 741 * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI 742 * sequence to them and LVT registers are reset to 0s except for 743 * the mask bits which are set to 1s when APs receive INIT IPI. 744 * If BIOS takes over the thermal interrupt and sets its interrupt 745 * delivery mode to SMI (not fixed), it restores the value that the 746 * BIOS has programmed on AP based on BSP's info we saved since BIOS 747 * is always setting the same value for all threads/cores. 748 */ 749 if ((h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED) 750 apic_write(APIC_LVTTHMR, lvtthmr_init); 751 752 753 if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) { 754 if (system_state == SYSTEM_BOOTING) 755 pr_debug("CPU%d: Thermal monitoring handled by SMI\n", cpu); 756 return; 757 } 758 759 /* early Pentium M models use different method for enabling TM2 */ 760 if (cpu_has(c, X86_FEATURE_TM2)) { 761 if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) { 762 rdmsr(MSR_THERM2_CTL, l, h); 763 if (l & MSR_THERM2_CTL_TM_SELECT) 764 tm2 = 1; 765 } else if (l & MSR_IA32_MISC_ENABLE_TM2) 766 tm2 = 1; 767 } 768 769 /* We'll mask the thermal vector in the lapic till we're ready: */ 770 h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED; 771 apic_write(APIC_LVTTHMR, h); 772 773 thermal_intr_init_core_clear_mask(); 774 thermal_intr_init_pkg_clear_mask(); 775 776 rdmsr(MSR_IA32_THERM_INTERRUPT, l, h); 777 if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable) 778 wrmsr(MSR_IA32_THERM_INTERRUPT, 779 (l | (THERM_INT_LOW_ENABLE 780 | THERM_INT_HIGH_ENABLE)) & ~THERM_INT_PLN_ENABLE, h); 781 else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) 782 wrmsr(MSR_IA32_THERM_INTERRUPT, 783 l | (THERM_INT_LOW_ENABLE 784 | THERM_INT_HIGH_ENABLE | THERM_INT_PLN_ENABLE), h); 785 else 786 wrmsr(MSR_IA32_THERM_INTERRUPT, 787 l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h); 788 789 if (cpu_has(c, X86_FEATURE_PTS)) { 790 rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); 791 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 if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) 797 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, 798 l | (PACKAGE_THERM_INT_LOW_ENABLE 799 | PACKAGE_THERM_INT_HIGH_ENABLE 800 | PACKAGE_THERM_INT_PLN_ENABLE), h); 801 else 802 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, 803 l | (PACKAGE_THERM_INT_LOW_ENABLE 804 | PACKAGE_THERM_INT_HIGH_ENABLE), h); 805 806 if (cpu_has(c, X86_FEATURE_HFI)) { 807 rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); 808 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, 809 l | PACKAGE_THERM_INT_HFI_ENABLE, h); 810 } 811 } 812 813 rdmsr(MSR_IA32_MISC_ENABLE, l, h); 814 wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h); 815 816 pr_info_once("CPU0: Thermal monitoring enabled (%s)\n", 817 tm2 ? "TM2" : "TM1"); 818 819 /* enable thermal throttle processing */ 820 atomic_set(&therm_throt_en, 1); 821 } 822