1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * processor_thermal_device.c 4 * Copyright (c) 2014, Intel Corporation. 5 */ 6 #include <linux/kernel.h> 7 #include <linux/module.h> 8 #include <linux/init.h> 9 #include <linux/pci.h> 10 #include <linux/interrupt.h> 11 #include <linux/platform_device.h> 12 #include <linux/acpi.h> 13 #include <linux/thermal.h> 14 #include <linux/cpuhotplug.h> 15 #include "int340x_thermal_zone.h" 16 #include "processor_thermal_device.h" 17 #include "../intel_soc_dts_iosf.h" 18 19 #define DRV_NAME "proc_thermal" 20 21 enum proc_thermal_emum_mode_type { 22 PROC_THERMAL_NONE, 23 PROC_THERMAL_PCI, 24 PROC_THERMAL_PLATFORM_DEV 25 }; 26 27 /* 28 * We can have only one type of enumeration, PCI or Platform, 29 * not both. So we don't need instance specific data. 30 */ 31 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode = 32 PROC_THERMAL_NONE; 33 34 #define POWER_LIMIT_SHOW(index, suffix) \ 35 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \ 36 struct device_attribute *attr, \ 37 char *buf) \ 38 { \ 39 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \ 40 \ 41 if (proc_thermal_emum_mode == PROC_THERMAL_NONE) { \ 42 dev_warn(dev, "Attempted to get power limit before device was initialized!\n"); \ 43 return 0; \ 44 } \ 45 \ 46 return sprintf(buf, "%lu\n",\ 47 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \ 48 } 49 50 POWER_LIMIT_SHOW(0, min_uw) 51 POWER_LIMIT_SHOW(0, max_uw) 52 POWER_LIMIT_SHOW(0, step_uw) 53 POWER_LIMIT_SHOW(0, tmin_us) 54 POWER_LIMIT_SHOW(0, tmax_us) 55 56 POWER_LIMIT_SHOW(1, min_uw) 57 POWER_LIMIT_SHOW(1, max_uw) 58 POWER_LIMIT_SHOW(1, step_uw) 59 POWER_LIMIT_SHOW(1, tmin_us) 60 POWER_LIMIT_SHOW(1, tmax_us) 61 62 static DEVICE_ATTR_RO(power_limit_0_min_uw); 63 static DEVICE_ATTR_RO(power_limit_0_max_uw); 64 static DEVICE_ATTR_RO(power_limit_0_step_uw); 65 static DEVICE_ATTR_RO(power_limit_0_tmin_us); 66 static DEVICE_ATTR_RO(power_limit_0_tmax_us); 67 68 static DEVICE_ATTR_RO(power_limit_1_min_uw); 69 static DEVICE_ATTR_RO(power_limit_1_max_uw); 70 static DEVICE_ATTR_RO(power_limit_1_step_uw); 71 static DEVICE_ATTR_RO(power_limit_1_tmin_us); 72 static DEVICE_ATTR_RO(power_limit_1_tmax_us); 73 74 static struct attribute *power_limit_attrs[] = { 75 &dev_attr_power_limit_0_min_uw.attr, 76 &dev_attr_power_limit_1_min_uw.attr, 77 &dev_attr_power_limit_0_max_uw.attr, 78 &dev_attr_power_limit_1_max_uw.attr, 79 &dev_attr_power_limit_0_step_uw.attr, 80 &dev_attr_power_limit_1_step_uw.attr, 81 &dev_attr_power_limit_0_tmin_us.attr, 82 &dev_attr_power_limit_1_tmin_us.attr, 83 &dev_attr_power_limit_0_tmax_us.attr, 84 &dev_attr_power_limit_1_tmax_us.attr, 85 NULL 86 }; 87 88 static const struct attribute_group power_limit_attribute_group = { 89 .attrs = power_limit_attrs, 90 .name = "power_limits" 91 }; 92 93 static ssize_t tcc_offset_degree_celsius_show(struct device *dev, 94 struct device_attribute *attr, char *buf) 95 { 96 u64 val; 97 int err; 98 99 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val); 100 if (err) 101 return err; 102 103 val = (val >> 24) & 0xff; 104 return sprintf(buf, "%d\n", (int)val); 105 } 106 107 static int tcc_offset_update(int tcc) 108 { 109 u64 val; 110 int err; 111 112 if (!tcc) 113 return -EINVAL; 114 115 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val); 116 if (err) 117 return err; 118 119 val &= ~GENMASK_ULL(31, 24); 120 val |= (tcc & 0xff) << 24; 121 122 err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val); 123 if (err) 124 return err; 125 126 return 0; 127 } 128 129 static int tcc_offset_save; 130 131 static ssize_t tcc_offset_degree_celsius_store(struct device *dev, 132 struct device_attribute *attr, const char *buf, 133 size_t count) 134 { 135 u64 val; 136 int tcc, err; 137 138 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val); 139 if (err) 140 return err; 141 142 if (!(val & BIT(30))) 143 return -EACCES; 144 145 if (kstrtoint(buf, 0, &tcc)) 146 return -EINVAL; 147 148 err = tcc_offset_update(tcc); 149 if (err) 150 return err; 151 152 tcc_offset_save = tcc; 153 154 return count; 155 } 156 157 static DEVICE_ATTR_RW(tcc_offset_degree_celsius); 158 159 static int stored_tjmax; /* since it is fixed, we can have local storage */ 160 161 static int get_tjmax(void) 162 { 163 u32 eax, edx; 164 u32 val; 165 int err; 166 167 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx); 168 if (err) 169 return err; 170 171 val = (eax >> 16) & 0xff; 172 if (val) 173 return val; 174 175 return -EINVAL; 176 } 177 178 static int read_temp_msr(int *temp) 179 { 180 int cpu; 181 u32 eax, edx; 182 int err; 183 unsigned long curr_temp_off = 0; 184 185 *temp = 0; 186 187 for_each_online_cpu(cpu) { 188 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax, 189 &edx); 190 if (err) 191 goto err_ret; 192 else { 193 if (eax & 0x80000000) { 194 curr_temp_off = (eax >> 16) & 0x7f; 195 if (!*temp || curr_temp_off < *temp) 196 *temp = curr_temp_off; 197 } else { 198 err = -EINVAL; 199 goto err_ret; 200 } 201 } 202 } 203 204 return 0; 205 err_ret: 206 return err; 207 } 208 209 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone, 210 int *temp) 211 { 212 int ret; 213 214 ret = read_temp_msr(temp); 215 if (!ret) 216 *temp = (stored_tjmax - *temp) * 1000; 217 218 return ret; 219 } 220 221 static struct thermal_zone_device_ops proc_thermal_local_ops = { 222 .get_temp = proc_thermal_get_zone_temp, 223 }; 224 225 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv) 226 { 227 int i; 228 acpi_status status; 229 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; 230 union acpi_object *elements, *ppcc; 231 union acpi_object *p; 232 int ret = 0; 233 234 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC", 235 NULL, &buf); 236 if (ACPI_FAILURE(status)) 237 return -ENODEV; 238 239 p = buf.pointer; 240 if (!p || (p->type != ACPI_TYPE_PACKAGE)) { 241 dev_err(proc_priv->dev, "Invalid PPCC data\n"); 242 ret = -EFAULT; 243 goto free_buffer; 244 } 245 246 if (!p->package.count) { 247 dev_err(proc_priv->dev, "Invalid PPCC package size\n"); 248 ret = -EFAULT; 249 goto free_buffer; 250 } 251 252 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) { 253 elements = &(p->package.elements[i+1]); 254 if (elements->type != ACPI_TYPE_PACKAGE || 255 elements->package.count != 6) { 256 ret = -EFAULT; 257 goto free_buffer; 258 } 259 ppcc = elements->package.elements; 260 proc_priv->power_limits[i].index = ppcc[0].integer.value; 261 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value; 262 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value; 263 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value; 264 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value; 265 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value; 266 } 267 268 free_buffer: 269 kfree(buf.pointer); 270 271 return ret; 272 } 273 274 #define PROC_POWER_CAPABILITY_CHANGED 0x83 275 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data) 276 { 277 struct proc_thermal_device *proc_priv = data; 278 279 if (!proc_priv) 280 return; 281 282 switch (event) { 283 case PROC_POWER_CAPABILITY_CHANGED: 284 proc_thermal_read_ppcc(proc_priv); 285 int340x_thermal_zone_device_update(proc_priv->int340x_zone, 286 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED); 287 break; 288 default: 289 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event); 290 break; 291 } 292 } 293 294 295 static int proc_thermal_add(struct device *dev, 296 struct proc_thermal_device **priv) 297 { 298 struct proc_thermal_device *proc_priv; 299 struct acpi_device *adev; 300 acpi_status status; 301 unsigned long long tmp; 302 struct thermal_zone_device_ops *ops = NULL; 303 int ret; 304 305 adev = ACPI_COMPANION(dev); 306 if (!adev) 307 return -ENODEV; 308 309 proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL); 310 if (!proc_priv) 311 return -ENOMEM; 312 313 proc_priv->dev = dev; 314 proc_priv->adev = adev; 315 *priv = proc_priv; 316 317 ret = proc_thermal_read_ppcc(proc_priv); 318 if (ret) 319 return ret; 320 321 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp); 322 if (ACPI_FAILURE(status)) { 323 /* there is no _TMP method, add local method */ 324 stored_tjmax = get_tjmax(); 325 if (stored_tjmax > 0) 326 ops = &proc_thermal_local_ops; 327 } 328 329 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops); 330 if (IS_ERR(proc_priv->int340x_zone)) { 331 return PTR_ERR(proc_priv->int340x_zone); 332 } else 333 ret = 0; 334 335 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, 336 proc_thermal_notify, 337 (void *)proc_priv); 338 if (ret) 339 goto remove_zone; 340 341 return 0; 342 343 remove_zone: 344 int340x_thermal_zone_remove(proc_priv->int340x_zone); 345 346 return ret; 347 } 348 349 static void proc_thermal_remove(struct proc_thermal_device *proc_priv) 350 { 351 acpi_remove_notify_handler(proc_priv->adev->handle, 352 ACPI_DEVICE_NOTIFY, proc_thermal_notify); 353 int340x_thermal_zone_remove(proc_priv->int340x_zone); 354 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr); 355 sysfs_remove_group(&proc_priv->dev->kobj, 356 &power_limit_attribute_group); 357 } 358 359 static int int3401_add(struct platform_device *pdev) 360 { 361 struct proc_thermal_device *proc_priv; 362 int ret; 363 364 if (proc_thermal_emum_mode == PROC_THERMAL_PCI) { 365 dev_err(&pdev->dev, "error: enumerated as PCI dev\n"); 366 return -ENODEV; 367 } 368 369 ret = proc_thermal_add(&pdev->dev, &proc_priv); 370 if (ret) 371 return ret; 372 373 platform_set_drvdata(pdev, proc_priv); 374 proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV; 375 376 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PLATFORM_DEV\n"); 377 378 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr); 379 if (ret) 380 return ret; 381 382 ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group); 383 if (ret) 384 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr); 385 386 return ret; 387 } 388 389 static int int3401_remove(struct platform_device *pdev) 390 { 391 proc_thermal_remove(platform_get_drvdata(pdev)); 392 393 return 0; 394 } 395 396 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid) 397 { 398 struct proc_thermal_device *proc_priv; 399 struct pci_dev *pdev = devid; 400 401 proc_priv = pci_get_drvdata(pdev); 402 403 intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts); 404 405 return IRQ_HANDLED; 406 } 407 408 #define MCHBAR 0 409 410 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, 411 struct proc_thermal_device *proc_priv) 412 { 413 int ret; 414 415 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME); 416 if (ret) { 417 dev_err(&pdev->dev, "cannot reserve PCI memory region\n"); 418 return -ENOMEM; 419 } 420 421 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR]; 422 423 return 0; 424 } 425 426 static int proc_thermal_mmio_add(struct pci_dev *pdev, 427 struct proc_thermal_device *proc_priv, 428 kernel_ulong_t feature_mask) 429 { 430 int ret; 431 432 proc_priv->mmio_feature_mask = feature_mask; 433 434 if (feature_mask) { 435 ret = proc_thermal_set_mmio_base(pdev, proc_priv); 436 if (ret) 437 return ret; 438 } 439 440 if (feature_mask & PROC_THERMAL_FEATURE_RAPL) { 441 ret = proc_thermal_rapl_add(pdev, proc_priv); 442 if (ret) { 443 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n"); 444 return ret; 445 } 446 } 447 448 if (feature_mask & PROC_THERMAL_FEATURE_FIVR || 449 feature_mask & PROC_THERMAL_FEATURE_DVFS) { 450 ret = proc_thermal_rfim_add(pdev, proc_priv); 451 if (ret) { 452 dev_err(&pdev->dev, "failed to add RFIM interface\n"); 453 goto err_rem_rapl; 454 } 455 } 456 457 if (feature_mask & PROC_THERMAL_FEATURE_MBOX) { 458 ret = proc_thermal_mbox_add(pdev, proc_priv); 459 if (ret) { 460 dev_err(&pdev->dev, "failed to add MBOX interface\n"); 461 goto err_rem_rfim; 462 } 463 } 464 465 return 0; 466 467 err_rem_rfim: 468 proc_thermal_rfim_remove(pdev); 469 err_rem_rapl: 470 proc_thermal_rapl_remove(); 471 472 return ret; 473 } 474 475 static void proc_thermal_mmio_remove(struct pci_dev *pdev) 476 { 477 struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev); 478 479 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL) 480 proc_thermal_rapl_remove(); 481 482 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR || 483 proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS) 484 proc_thermal_rfim_remove(pdev); 485 486 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_MBOX) 487 proc_thermal_mbox_remove(pdev); 488 } 489 490 static int proc_thermal_pci_probe(struct pci_dev *pdev, 491 const struct pci_device_id *id) 492 { 493 struct proc_thermal_device *proc_priv; 494 int ret; 495 496 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { 497 dev_err(&pdev->dev, "error: enumerated as platform dev\n"); 498 return -ENODEV; 499 } 500 501 ret = pcim_enable_device(pdev); 502 if (ret < 0) { 503 dev_err(&pdev->dev, "error: could not enable device\n"); 504 return ret; 505 } 506 507 ret = proc_thermal_add(&pdev->dev, &proc_priv); 508 if (ret) 509 return ret; 510 511 pci_set_drvdata(pdev, proc_priv); 512 proc_thermal_emum_mode = PROC_THERMAL_PCI; 513 514 if (pdev->device == PCI_DEVICE_ID_INTEL_BSW_THERMAL) { 515 /* 516 * Enumerate additional DTS sensors available via IOSF. 517 * But we are not treating as a failure condition, if 518 * there are no aux DTSs enabled or fails. This driver 519 * already exposes sensors, which can be accessed via 520 * ACPI/MSR. So we don't want to fail for auxiliary DTSs. 521 */ 522 proc_priv->soc_dts = intel_soc_dts_iosf_init( 523 INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0); 524 525 if (!IS_ERR(proc_priv->soc_dts) && pdev->irq) { 526 ret = pci_enable_msi(pdev); 527 if (!ret) { 528 ret = request_threaded_irq(pdev->irq, NULL, 529 proc_thermal_pci_msi_irq, 530 IRQF_ONESHOT, "proc_thermal", 531 pdev); 532 if (ret) { 533 intel_soc_dts_iosf_exit( 534 proc_priv->soc_dts); 535 pci_disable_msi(pdev); 536 proc_priv->soc_dts = NULL; 537 } 538 } 539 } else 540 dev_err(&pdev->dev, "No auxiliary DTSs enabled\n"); 541 } 542 543 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PCI\n"); 544 545 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr); 546 if (ret) 547 return ret; 548 549 ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group); 550 if (ret) { 551 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr); 552 return ret; 553 } 554 555 ret = proc_thermal_mmio_add(pdev, proc_priv, id->driver_data); 556 if (ret) { 557 proc_thermal_remove(proc_priv); 558 return ret; 559 } 560 561 return 0; 562 } 563 564 static void proc_thermal_pci_remove(struct pci_dev *pdev) 565 { 566 struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev); 567 568 if (proc_priv->soc_dts) { 569 intel_soc_dts_iosf_exit(proc_priv->soc_dts); 570 if (pdev->irq) { 571 free_irq(pdev->irq, pdev); 572 pci_disable_msi(pdev); 573 } 574 } 575 576 proc_thermal_mmio_remove(pdev); 577 proc_thermal_remove(proc_priv); 578 } 579 580 #ifdef CONFIG_PM_SLEEP 581 static int proc_thermal_resume(struct device *dev) 582 { 583 struct proc_thermal_device *proc_dev; 584 585 proc_dev = dev_get_drvdata(dev); 586 proc_thermal_read_ppcc(proc_dev); 587 588 tcc_offset_update(tcc_offset_save); 589 590 return 0; 591 } 592 #else 593 #define proc_thermal_resume NULL 594 #endif 595 596 static SIMPLE_DEV_PM_OPS(proc_thermal_pm, NULL, proc_thermal_resume); 597 598 static const struct pci_device_id proc_thermal_pci_ids[] = { 599 { PCI_DEVICE_DATA(INTEL, ADL_THERMAL, PROC_THERMAL_FEATURE_RAPL | PROC_THERMAL_FEATURE_FIVR | PROC_THERMAL_FEATURE_DVFS | PROC_THERMAL_FEATURE_MBOX) }, 600 { PCI_DEVICE_DATA(INTEL, BDW_THERMAL, 0) }, 601 { PCI_DEVICE_DATA(INTEL, BSW_THERMAL, 0) }, 602 { PCI_DEVICE_DATA(INTEL, BXT0_THERMAL, 0) }, 603 { PCI_DEVICE_DATA(INTEL, BXT1_THERMAL, 0) }, 604 { PCI_DEVICE_DATA(INTEL, BXTX_THERMAL, 0) }, 605 { PCI_DEVICE_DATA(INTEL, BXTP_THERMAL, 0) }, 606 { PCI_DEVICE_DATA(INTEL, CNL_THERMAL, 0) }, 607 { PCI_DEVICE_DATA(INTEL, CFL_THERMAL, 0) }, 608 { PCI_DEVICE_DATA(INTEL, GLK_THERMAL, 0) }, 609 { PCI_DEVICE_DATA(INTEL, HSB_THERMAL, 0) }, 610 { PCI_DEVICE_DATA(INTEL, ICL_THERMAL, PROC_THERMAL_FEATURE_RAPL) }, 611 { PCI_DEVICE_DATA(INTEL, JSL_THERMAL, 0) }, 612 { PCI_DEVICE_DATA(INTEL, SKL_THERMAL, PROC_THERMAL_FEATURE_RAPL) }, 613 { PCI_DEVICE_DATA(INTEL, TGL_THERMAL, PROC_THERMAL_FEATURE_RAPL | PROC_THERMAL_FEATURE_FIVR | PROC_THERMAL_FEATURE_MBOX) }, 614 { }, 615 }; 616 617 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids); 618 619 static struct pci_driver proc_thermal_pci_driver = { 620 .name = DRV_NAME, 621 .probe = proc_thermal_pci_probe, 622 .remove = proc_thermal_pci_remove, 623 .id_table = proc_thermal_pci_ids, 624 .driver.pm = &proc_thermal_pm, 625 }; 626 627 static const struct acpi_device_id int3401_device_ids[] = { 628 {"INT3401", 0}, 629 {"", 0}, 630 }; 631 MODULE_DEVICE_TABLE(acpi, int3401_device_ids); 632 633 static struct platform_driver int3401_driver = { 634 .probe = int3401_add, 635 .remove = int3401_remove, 636 .driver = { 637 .name = "int3401 thermal", 638 .acpi_match_table = int3401_device_ids, 639 .pm = &proc_thermal_pm, 640 }, 641 }; 642 643 static int __init proc_thermal_init(void) 644 { 645 int ret; 646 647 ret = platform_driver_register(&int3401_driver); 648 if (ret) 649 return ret; 650 651 ret = pci_register_driver(&proc_thermal_pci_driver); 652 653 return ret; 654 } 655 656 static void __exit proc_thermal_exit(void) 657 { 658 platform_driver_unregister(&int3401_driver); 659 pci_unregister_driver(&proc_thermal_pci_driver); 660 } 661 662 module_init(proc_thermal_init); 663 module_exit(proc_thermal_exit); 664 665 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 666 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver"); 667 MODULE_LICENSE("GPL v2"); 668