1 /* 2 * processor_thermal_device.c 3 * Copyright (c) 2014, Intel Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 */ 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/init.h> 18 #include <linux/pci.h> 19 #include <linux/interrupt.h> 20 #include <linux/platform_device.h> 21 #include <linux/acpi.h> 22 #include <linux/thermal.h> 23 #include "int340x_thermal_zone.h" 24 #include "../intel_soc_dts_iosf.h" 25 26 /* Broadwell-U/HSB thermal reporting device */ 27 #define PCI_DEVICE_ID_PROC_BDW_THERMAL 0x1603 28 #define PCI_DEVICE_ID_PROC_HSB_THERMAL 0x0A03 29 30 /* Skylake thermal reporting device */ 31 #define PCI_DEVICE_ID_PROC_SKL_THERMAL 0x1903 32 33 /* CannonLake thermal reporting device */ 34 #define PCI_DEVICE_ID_PROC_CNL_THERMAL 0x5a03 35 #define PCI_DEVICE_ID_PROC_CFL_THERMAL 0x3E83 36 37 /* Braswell thermal reporting device */ 38 #define PCI_DEVICE_ID_PROC_BSW_THERMAL 0x22DC 39 40 /* Broxton thermal reporting device */ 41 #define PCI_DEVICE_ID_PROC_BXT0_THERMAL 0x0A8C 42 #define PCI_DEVICE_ID_PROC_BXT1_THERMAL 0x1A8C 43 #define PCI_DEVICE_ID_PROC_BXTX_THERMAL 0x4A8C 44 #define PCI_DEVICE_ID_PROC_BXTP_THERMAL 0x5A8C 45 46 /* GeminiLake thermal reporting device */ 47 #define PCI_DEVICE_ID_PROC_GLK_THERMAL 0x318C 48 49 struct power_config { 50 u32 index; 51 u32 min_uw; 52 u32 max_uw; 53 u32 tmin_us; 54 u32 tmax_us; 55 u32 step_uw; 56 }; 57 58 struct proc_thermal_device { 59 struct device *dev; 60 struct acpi_device *adev; 61 struct power_config power_limits[2]; 62 struct int34x_thermal_zone *int340x_zone; 63 struct intel_soc_dts_sensors *soc_dts; 64 }; 65 66 enum proc_thermal_emum_mode_type { 67 PROC_THERMAL_NONE, 68 PROC_THERMAL_PCI, 69 PROC_THERMAL_PLATFORM_DEV 70 }; 71 72 /* 73 * We can have only one type of enumeration, PCI or Platform, 74 * not both. So we don't need instance specific data. 75 */ 76 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode = 77 PROC_THERMAL_NONE; 78 79 #define POWER_LIMIT_SHOW(index, suffix) \ 80 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \ 81 struct device_attribute *attr, \ 82 char *buf) \ 83 { \ 84 struct pci_dev *pci_dev; \ 85 struct platform_device *pdev; \ 86 struct proc_thermal_device *proc_dev; \ 87 \ 88 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { \ 89 pdev = to_platform_device(dev); \ 90 proc_dev = platform_get_drvdata(pdev); \ 91 } else { \ 92 pci_dev = to_pci_dev(dev); \ 93 proc_dev = pci_get_drvdata(pci_dev); \ 94 } \ 95 return sprintf(buf, "%lu\n",\ 96 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \ 97 } 98 99 POWER_LIMIT_SHOW(0, min_uw) 100 POWER_LIMIT_SHOW(0, max_uw) 101 POWER_LIMIT_SHOW(0, step_uw) 102 POWER_LIMIT_SHOW(0, tmin_us) 103 POWER_LIMIT_SHOW(0, tmax_us) 104 105 POWER_LIMIT_SHOW(1, min_uw) 106 POWER_LIMIT_SHOW(1, max_uw) 107 POWER_LIMIT_SHOW(1, step_uw) 108 POWER_LIMIT_SHOW(1, tmin_us) 109 POWER_LIMIT_SHOW(1, tmax_us) 110 111 static DEVICE_ATTR_RO(power_limit_0_min_uw); 112 static DEVICE_ATTR_RO(power_limit_0_max_uw); 113 static DEVICE_ATTR_RO(power_limit_0_step_uw); 114 static DEVICE_ATTR_RO(power_limit_0_tmin_us); 115 static DEVICE_ATTR_RO(power_limit_0_tmax_us); 116 117 static DEVICE_ATTR_RO(power_limit_1_min_uw); 118 static DEVICE_ATTR_RO(power_limit_1_max_uw); 119 static DEVICE_ATTR_RO(power_limit_1_step_uw); 120 static DEVICE_ATTR_RO(power_limit_1_tmin_us); 121 static DEVICE_ATTR_RO(power_limit_1_tmax_us); 122 123 static struct attribute *power_limit_attrs[] = { 124 &dev_attr_power_limit_0_min_uw.attr, 125 &dev_attr_power_limit_1_min_uw.attr, 126 &dev_attr_power_limit_0_max_uw.attr, 127 &dev_attr_power_limit_1_max_uw.attr, 128 &dev_attr_power_limit_0_step_uw.attr, 129 &dev_attr_power_limit_1_step_uw.attr, 130 &dev_attr_power_limit_0_tmin_us.attr, 131 &dev_attr_power_limit_1_tmin_us.attr, 132 &dev_attr_power_limit_0_tmax_us.attr, 133 &dev_attr_power_limit_1_tmax_us.attr, 134 NULL 135 }; 136 137 static const struct attribute_group power_limit_attribute_group = { 138 .attrs = power_limit_attrs, 139 .name = "power_limits" 140 }; 141 142 static int stored_tjmax; /* since it is fixed, we can have local storage */ 143 144 static int get_tjmax(void) 145 { 146 u32 eax, edx; 147 u32 val; 148 int err; 149 150 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx); 151 if (err) 152 return err; 153 154 val = (eax >> 16) & 0xff; 155 if (val) 156 return val; 157 158 return -EINVAL; 159 } 160 161 static int read_temp_msr(int *temp) 162 { 163 int cpu; 164 u32 eax, edx; 165 int err; 166 unsigned long curr_temp_off = 0; 167 168 *temp = 0; 169 170 for_each_online_cpu(cpu) { 171 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax, 172 &edx); 173 if (err) 174 goto err_ret; 175 else { 176 if (eax & 0x80000000) { 177 curr_temp_off = (eax >> 16) & 0x7f; 178 if (!*temp || curr_temp_off < *temp) 179 *temp = curr_temp_off; 180 } else { 181 err = -EINVAL; 182 goto err_ret; 183 } 184 } 185 } 186 187 return 0; 188 err_ret: 189 return err; 190 } 191 192 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone, 193 int *temp) 194 { 195 int ret; 196 197 ret = read_temp_msr(temp); 198 if (!ret) 199 *temp = (stored_tjmax - *temp) * 1000; 200 201 return ret; 202 } 203 204 static struct thermal_zone_device_ops proc_thermal_local_ops = { 205 .get_temp = proc_thermal_get_zone_temp, 206 }; 207 208 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv) 209 { 210 int i; 211 acpi_status status; 212 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; 213 union acpi_object *elements, *ppcc; 214 union acpi_object *p; 215 int ret = 0; 216 217 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC", 218 NULL, &buf); 219 if (ACPI_FAILURE(status)) 220 return -ENODEV; 221 222 p = buf.pointer; 223 if (!p || (p->type != ACPI_TYPE_PACKAGE)) { 224 dev_err(proc_priv->dev, "Invalid PPCC data\n"); 225 ret = -EFAULT; 226 goto free_buffer; 227 } 228 229 if (!p->package.count) { 230 dev_err(proc_priv->dev, "Invalid PPCC package size\n"); 231 ret = -EFAULT; 232 goto free_buffer; 233 } 234 235 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) { 236 elements = &(p->package.elements[i+1]); 237 if (elements->type != ACPI_TYPE_PACKAGE || 238 elements->package.count != 6) { 239 ret = -EFAULT; 240 goto free_buffer; 241 } 242 ppcc = elements->package.elements; 243 proc_priv->power_limits[i].index = ppcc[0].integer.value; 244 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value; 245 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value; 246 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value; 247 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value; 248 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value; 249 } 250 251 free_buffer: 252 kfree(buf.pointer); 253 254 return ret; 255 } 256 257 #define PROC_POWER_CAPABILITY_CHANGED 0x83 258 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data) 259 { 260 struct proc_thermal_device *proc_priv = data; 261 262 if (!proc_priv) 263 return; 264 265 switch (event) { 266 case PROC_POWER_CAPABILITY_CHANGED: 267 proc_thermal_read_ppcc(proc_priv); 268 int340x_thermal_zone_device_update(proc_priv->int340x_zone, 269 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED); 270 break; 271 default: 272 dev_err(proc_priv->dev, "Unsupported event [0x%x]\n", event); 273 break; 274 } 275 } 276 277 278 static int proc_thermal_add(struct device *dev, 279 struct proc_thermal_device **priv) 280 { 281 struct proc_thermal_device *proc_priv; 282 struct acpi_device *adev; 283 acpi_status status; 284 unsigned long long tmp; 285 struct thermal_zone_device_ops *ops = NULL; 286 int ret; 287 288 adev = ACPI_COMPANION(dev); 289 if (!adev) 290 return -ENODEV; 291 292 proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL); 293 if (!proc_priv) 294 return -ENOMEM; 295 296 proc_priv->dev = dev; 297 proc_priv->adev = adev; 298 *priv = proc_priv; 299 300 ret = proc_thermal_read_ppcc(proc_priv); 301 if (!ret) { 302 ret = sysfs_create_group(&dev->kobj, 303 &power_limit_attribute_group); 304 305 } 306 if (ret) 307 return ret; 308 309 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp); 310 if (ACPI_FAILURE(status)) { 311 /* there is no _TMP method, add local method */ 312 stored_tjmax = get_tjmax(); 313 if (stored_tjmax > 0) 314 ops = &proc_thermal_local_ops; 315 } 316 317 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops); 318 if (IS_ERR(proc_priv->int340x_zone)) { 319 ret = PTR_ERR(proc_priv->int340x_zone); 320 goto remove_group; 321 } else 322 ret = 0; 323 324 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, 325 proc_thermal_notify, 326 (void *)proc_priv); 327 if (ret) 328 goto remove_zone; 329 330 return 0; 331 332 remove_zone: 333 int340x_thermal_zone_remove(proc_priv->int340x_zone); 334 remove_group: 335 sysfs_remove_group(&proc_priv->dev->kobj, 336 &power_limit_attribute_group); 337 338 return ret; 339 } 340 341 static void proc_thermal_remove(struct proc_thermal_device *proc_priv) 342 { 343 acpi_remove_notify_handler(proc_priv->adev->handle, 344 ACPI_DEVICE_NOTIFY, proc_thermal_notify); 345 int340x_thermal_zone_remove(proc_priv->int340x_zone); 346 sysfs_remove_group(&proc_priv->dev->kobj, 347 &power_limit_attribute_group); 348 } 349 350 static int int3401_add(struct platform_device *pdev) 351 { 352 struct proc_thermal_device *proc_priv; 353 int ret; 354 355 if (proc_thermal_emum_mode == PROC_THERMAL_PCI) { 356 dev_err(&pdev->dev, "error: enumerated as PCI dev\n"); 357 return -ENODEV; 358 } 359 360 ret = proc_thermal_add(&pdev->dev, &proc_priv); 361 if (ret) 362 return ret; 363 364 platform_set_drvdata(pdev, proc_priv); 365 proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV; 366 367 return 0; 368 } 369 370 static int int3401_remove(struct platform_device *pdev) 371 { 372 proc_thermal_remove(platform_get_drvdata(pdev)); 373 374 return 0; 375 } 376 377 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid) 378 { 379 struct proc_thermal_device *proc_priv; 380 struct pci_dev *pdev = devid; 381 382 proc_priv = pci_get_drvdata(pdev); 383 384 intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts); 385 386 return IRQ_HANDLED; 387 } 388 389 static int proc_thermal_pci_probe(struct pci_dev *pdev, 390 const struct pci_device_id *unused) 391 { 392 struct proc_thermal_device *proc_priv; 393 int ret; 394 395 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { 396 dev_err(&pdev->dev, "error: enumerated as platform dev\n"); 397 return -ENODEV; 398 } 399 400 ret = pci_enable_device(pdev); 401 if (ret < 0) { 402 dev_err(&pdev->dev, "error: could not enable device\n"); 403 return ret; 404 } 405 406 ret = proc_thermal_add(&pdev->dev, &proc_priv); 407 if (ret) { 408 pci_disable_device(pdev); 409 return ret; 410 } 411 412 pci_set_drvdata(pdev, proc_priv); 413 proc_thermal_emum_mode = PROC_THERMAL_PCI; 414 415 if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) { 416 /* 417 * Enumerate additional DTS sensors available via IOSF. 418 * But we are not treating as a failure condition, if 419 * there are no aux DTSs enabled or fails. This driver 420 * already exposes sensors, which can be accessed via 421 * ACPI/MSR. So we don't want to fail for auxiliary DTSs. 422 */ 423 proc_priv->soc_dts = intel_soc_dts_iosf_init( 424 INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0); 425 426 if (proc_priv->soc_dts && pdev->irq) { 427 ret = pci_enable_msi(pdev); 428 if (!ret) { 429 ret = request_threaded_irq(pdev->irq, NULL, 430 proc_thermal_pci_msi_irq, 431 IRQF_ONESHOT, "proc_thermal", 432 pdev); 433 if (ret) { 434 intel_soc_dts_iosf_exit( 435 proc_priv->soc_dts); 436 pci_disable_msi(pdev); 437 proc_priv->soc_dts = NULL; 438 } 439 } 440 } else 441 dev_err(&pdev->dev, "No auxiliary DTSs enabled\n"); 442 } 443 444 return 0; 445 } 446 447 static void proc_thermal_pci_remove(struct pci_dev *pdev) 448 { 449 struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev); 450 451 if (proc_priv->soc_dts) { 452 intel_soc_dts_iosf_exit(proc_priv->soc_dts); 453 if (pdev->irq) { 454 free_irq(pdev->irq, pdev); 455 pci_disable_msi(pdev); 456 } 457 } 458 proc_thermal_remove(proc_priv); 459 pci_disable_device(pdev); 460 } 461 462 static const struct pci_device_id proc_thermal_pci_ids[] = { 463 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)}, 464 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)}, 465 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL)}, 466 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)}, 467 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT0_THERMAL)}, 468 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT1_THERMAL)}, 469 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTX_THERMAL)}, 470 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTP_THERMAL)}, 471 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CNL_THERMAL)}, 472 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CFL_THERMAL)}, 473 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_GLK_THERMAL)}, 474 { 0, }, 475 }; 476 477 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids); 478 479 static struct pci_driver proc_thermal_pci_driver = { 480 .name = "proc_thermal", 481 .probe = proc_thermal_pci_probe, 482 .remove = proc_thermal_pci_remove, 483 .id_table = proc_thermal_pci_ids, 484 }; 485 486 static const struct acpi_device_id int3401_device_ids[] = { 487 {"INT3401", 0}, 488 {"", 0}, 489 }; 490 MODULE_DEVICE_TABLE(acpi, int3401_device_ids); 491 492 static struct platform_driver int3401_driver = { 493 .probe = int3401_add, 494 .remove = int3401_remove, 495 .driver = { 496 .name = "int3401 thermal", 497 .acpi_match_table = int3401_device_ids, 498 }, 499 }; 500 501 static int __init proc_thermal_init(void) 502 { 503 int ret; 504 505 ret = platform_driver_register(&int3401_driver); 506 if (ret) 507 return ret; 508 509 ret = pci_register_driver(&proc_thermal_pci_driver); 510 511 return ret; 512 } 513 514 static void __exit proc_thermal_exit(void) 515 { 516 platform_driver_unregister(&int3401_driver); 517 pci_unregister_driver(&proc_thermal_pci_driver); 518 } 519 520 module_init(proc_thermal_init); 521 module_exit(proc_thermal_exit); 522 523 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 524 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver"); 525 MODULE_LICENSE("GPL v2"); 526