1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $) 4 * 5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> 6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 7 * 8 * This driver fully implements the ACPI thermal policy as described in the 9 * ACPI 2.0 Specification. 10 * 11 * TBD: 1. Implement passive cooling hysteresis. 12 * 2. Enhance passive cooling (CPU) states/limit interface to support 13 * concepts of 'multiple limiters', upper/lower limits, etc. 14 */ 15 16 #define pr_fmt(fmt) "ACPI: thermal: " fmt 17 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/dmi.h> 21 #include <linux/init.h> 22 #include <linux/slab.h> 23 #include <linux/types.h> 24 #include <linux/jiffies.h> 25 #include <linux/kmod.h> 26 #include <linux/reboot.h> 27 #include <linux/device.h> 28 #include <linux/thermal.h> 29 #include <linux/acpi.h> 30 #include <linux/workqueue.h> 31 #include <linux/uaccess.h> 32 #include <linux/units.h> 33 34 #define ACPI_THERMAL_CLASS "thermal_zone" 35 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone" 36 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80 37 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81 38 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82 39 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0 40 #define ACPI_THERMAL_NOTIFY_HOT 0xF1 41 #define ACPI_THERMAL_MODE_ACTIVE 0x00 42 43 #define ACPI_THERMAL_MAX_ACTIVE 10 44 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65 45 46 #define ACPI_TRIPS_CRITICAL BIT(0) 47 #define ACPI_TRIPS_HOT BIT(1) 48 #define ACPI_TRIPS_PASSIVE BIT(2) 49 #define ACPI_TRIPS_ACTIVE BIT(3) 50 #define ACPI_TRIPS_DEVICES BIT(4) 51 52 #define ACPI_TRIPS_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE) 53 54 #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \ 55 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \ 56 ACPI_TRIPS_DEVICES) 57 58 /* 59 * This exception is thrown out in two cases: 60 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid 61 * when re-evaluating the AML code. 62 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change. 63 * We need to re-bind the cooling devices of a thermal zone when this occurs. 64 */ 65 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, tz, str) \ 66 do { \ 67 if (flags != ACPI_TRIPS_INIT) \ 68 acpi_handle_info(tz->device->handle, \ 69 "ACPI thermal trip point %s changed\n" \ 70 "Please report to linux-acpi@vger.kernel.org\n", str); \ 71 } while (0) 72 73 static int act; 74 module_param(act, int, 0644); 75 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points."); 76 77 static int crt; 78 module_param(crt, int, 0644); 79 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points."); 80 81 static int tzp; 82 module_param(tzp, int, 0444); 83 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds."); 84 85 static int nocrt; 86 module_param(nocrt, int, 0); 87 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points."); 88 89 static int off; 90 module_param(off, int, 0); 91 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support."); 92 93 static int psv; 94 module_param(psv, int, 0644); 95 MODULE_PARM_DESC(psv, "Disable or override all passive trip points."); 96 97 static struct workqueue_struct *acpi_thermal_pm_queue; 98 99 struct acpi_thermal_critical { 100 unsigned long temperature; 101 bool valid; 102 }; 103 104 struct acpi_thermal_hot { 105 unsigned long temperature; 106 bool valid; 107 }; 108 109 struct acpi_thermal_passive { 110 struct acpi_handle_list devices; 111 unsigned long temperature; 112 unsigned long tc1; 113 unsigned long tc2; 114 unsigned long tsp; 115 bool valid; 116 }; 117 118 struct acpi_thermal_active { 119 struct acpi_handle_list devices; 120 unsigned long temperature; 121 bool valid; 122 bool enabled; 123 }; 124 125 struct acpi_thermal_trips { 126 struct acpi_thermal_critical critical; 127 struct acpi_thermal_hot hot; 128 struct acpi_thermal_passive passive; 129 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE]; 130 }; 131 132 struct acpi_thermal { 133 struct acpi_device *device; 134 acpi_bus_id name; 135 unsigned long temperature; 136 unsigned long last_temperature; 137 unsigned long polling_frequency; 138 volatile u8 zombie; 139 struct acpi_thermal_trips trips; 140 struct acpi_handle_list devices; 141 struct thermal_zone_device *thermal_zone; 142 int kelvin_offset; /* in millidegrees */ 143 struct work_struct thermal_check_work; 144 struct mutex thermal_check_lock; 145 refcount_t thermal_check_count; 146 }; 147 148 /* -------------------------------------------------------------------------- 149 Thermal Zone Management 150 -------------------------------------------------------------------------- */ 151 152 static int acpi_thermal_get_temperature(struct acpi_thermal *tz) 153 { 154 acpi_status status = AE_OK; 155 unsigned long long tmp; 156 157 if (!tz) 158 return -EINVAL; 159 160 tz->last_temperature = tz->temperature; 161 162 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp); 163 if (ACPI_FAILURE(status)) 164 return -ENODEV; 165 166 tz->temperature = tmp; 167 168 acpi_handle_debug(tz->device->handle, "Temperature is %lu dK\n", 169 tz->temperature); 170 171 return 0; 172 } 173 174 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz) 175 { 176 acpi_status status = AE_OK; 177 unsigned long long tmp; 178 179 if (!tz) 180 return -EINVAL; 181 182 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp); 183 if (ACPI_FAILURE(status)) 184 return -ENODEV; 185 186 tz->polling_frequency = tmp; 187 acpi_handle_debug(tz->device->handle, "Polling frequency is %lu dS\n", 188 tz->polling_frequency); 189 190 return 0; 191 } 192 193 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag) 194 { 195 acpi_status status; 196 unsigned long long tmp; 197 struct acpi_handle_list devices; 198 bool valid = false; 199 int i; 200 201 /* Critical Shutdown */ 202 if (flag & ACPI_TRIPS_CRITICAL) { 203 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL, &tmp); 204 tz->trips.critical.temperature = tmp; 205 /* 206 * Treat freezing temperatures as invalid as well; some 207 * BIOSes return really low values and cause reboots at startup. 208 * Below zero (Celsius) values clearly aren't right for sure.. 209 * ... so lets discard those as invalid. 210 */ 211 if (ACPI_FAILURE(status)) { 212 tz->trips.critical.valid = false; 213 acpi_handle_debug(tz->device->handle, 214 "No critical threshold\n"); 215 } else if (tmp <= 2732) { 216 pr_info(FW_BUG "Invalid critical threshold (%llu)\n", tmp); 217 tz->trips.critical.valid = false; 218 } else { 219 tz->trips.critical.valid = true; 220 acpi_handle_debug(tz->device->handle, 221 "Found critical threshold [%lu]\n", 222 tz->trips.critical.temperature); 223 } 224 if (tz->trips.critical.valid) { 225 if (crt == -1) { 226 tz->trips.critical.valid = false; 227 } else if (crt > 0) { 228 unsigned long crt_k = celsius_to_deci_kelvin(crt); 229 230 /* 231 * Allow override critical threshold 232 */ 233 if (crt_k > tz->trips.critical.temperature) 234 pr_info("Critical threshold %d C\n", crt); 235 236 tz->trips.critical.temperature = crt_k; 237 } 238 } 239 } 240 241 /* Critical Sleep (optional) */ 242 if (flag & ACPI_TRIPS_HOT) { 243 status = acpi_evaluate_integer(tz->device->handle, "_HOT", NULL, &tmp); 244 if (ACPI_FAILURE(status)) { 245 tz->trips.hot.valid = false; 246 acpi_handle_debug(tz->device->handle, 247 "No hot threshold\n"); 248 } else { 249 tz->trips.hot.temperature = tmp; 250 tz->trips.hot.valid = true; 251 acpi_handle_debug(tz->device->handle, 252 "Found hot threshold [%lu]\n", 253 tz->trips.hot.temperature); 254 } 255 } 256 257 /* Passive (optional) */ 258 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.valid) || 259 flag == ACPI_TRIPS_INIT) { 260 valid = tz->trips.passive.valid; 261 if (psv == -1) { 262 status = AE_SUPPORT; 263 } else if (psv > 0) { 264 tmp = celsius_to_deci_kelvin(psv); 265 status = AE_OK; 266 } else { 267 status = acpi_evaluate_integer(tz->device->handle, 268 "_PSV", NULL, &tmp); 269 } 270 271 if (ACPI_FAILURE(status)) { 272 tz->trips.passive.valid = false; 273 } else { 274 tz->trips.passive.temperature = tmp; 275 tz->trips.passive.valid = true; 276 if (flag == ACPI_TRIPS_INIT) { 277 status = acpi_evaluate_integer(tz->device->handle, 278 "_TC1", NULL, &tmp); 279 if (ACPI_FAILURE(status)) 280 tz->trips.passive.valid = false; 281 else 282 tz->trips.passive.tc1 = tmp; 283 284 status = acpi_evaluate_integer(tz->device->handle, 285 "_TC2", NULL, &tmp); 286 if (ACPI_FAILURE(status)) 287 tz->trips.passive.valid = false; 288 else 289 tz->trips.passive.tc2 = tmp; 290 291 status = acpi_evaluate_integer(tz->device->handle, 292 "_TSP", NULL, &tmp); 293 if (ACPI_FAILURE(status)) 294 tz->trips.passive.valid = false; 295 else 296 tz->trips.passive.tsp = tmp; 297 } 298 } 299 } 300 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.valid) { 301 memset(&devices, 0, sizeof(struct acpi_handle_list)); 302 status = acpi_evaluate_reference(tz->device->handle, "_PSL", 303 NULL, &devices); 304 if (ACPI_FAILURE(status)) { 305 acpi_handle_info(tz->device->handle, 306 "Invalid passive threshold\n"); 307 tz->trips.passive.valid = false; 308 } else { 309 tz->trips.passive.valid = true; 310 } 311 312 if (memcmp(&tz->trips.passive.devices, &devices, 313 sizeof(struct acpi_handle_list))) { 314 memcpy(&tz->trips.passive.devices, &devices, 315 sizeof(struct acpi_handle_list)); 316 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device"); 317 } 318 } 319 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) { 320 if (valid != tz->trips.passive.valid) 321 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state"); 322 } 323 324 /* Active (optional) */ 325 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { 326 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' }; 327 valid = tz->trips.active[i].valid; 328 329 if (act == -1) 330 break; /* disable all active trip points */ 331 332 if (flag == ACPI_TRIPS_INIT || ((flag & ACPI_TRIPS_ACTIVE) && 333 tz->trips.active[i].valid)) { 334 status = acpi_evaluate_integer(tz->device->handle, 335 name, NULL, &tmp); 336 if (ACPI_FAILURE(status)) { 337 tz->trips.active[i].valid = false; 338 if (i == 0) 339 break; 340 341 if (act <= 0) 342 break; 343 344 if (i == 1) 345 tz->trips.active[0].temperature = celsius_to_deci_kelvin(act); 346 else 347 /* 348 * Don't allow override higher than 349 * the next higher trip point 350 */ 351 tz->trips.active[i-1].temperature = 352 min_t(unsigned long, 353 tz->trips.active[i-2].temperature, 354 celsius_to_deci_kelvin(act)); 355 356 break; 357 } else { 358 tz->trips.active[i].temperature = tmp; 359 tz->trips.active[i].valid = true; 360 } 361 } 362 363 name[2] = 'L'; 364 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].valid) { 365 memset(&devices, 0, sizeof(struct acpi_handle_list)); 366 status = acpi_evaluate_reference(tz->device->handle, 367 name, NULL, &devices); 368 if (ACPI_FAILURE(status)) { 369 acpi_handle_info(tz->device->handle, 370 "Invalid active%d threshold\n", i); 371 tz->trips.active[i].valid = false; 372 } else { 373 tz->trips.active[i].valid = true; 374 } 375 376 if (memcmp(&tz->trips.active[i].devices, &devices, 377 sizeof(struct acpi_handle_list))) { 378 memcpy(&tz->trips.active[i].devices, &devices, 379 sizeof(struct acpi_handle_list)); 380 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device"); 381 } 382 } 383 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES)) 384 if (valid != tz->trips.active[i].valid) 385 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state"); 386 387 if (!tz->trips.active[i].valid) 388 break; 389 } 390 391 if (flag & ACPI_TRIPS_DEVICES) { 392 memset(&devices, 0, sizeof(devices)); 393 status = acpi_evaluate_reference(tz->device->handle, "_TZD", 394 NULL, &devices); 395 if (ACPI_SUCCESS(status) && 396 memcmp(&tz->devices, &devices, sizeof(devices))) { 397 tz->devices = devices; 398 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device"); 399 } 400 } 401 402 return 0; 403 } 404 405 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz) 406 { 407 int i, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT); 408 bool valid; 409 410 if (ret) 411 return ret; 412 413 valid = tz->trips.critical.valid | 414 tz->trips.hot.valid | 415 tz->trips.passive.valid; 416 417 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) 418 valid = valid || tz->trips.active[i].valid; 419 420 if (!valid) { 421 pr_warn(FW_BUG "No valid trip found\n"); 422 return -ENODEV; 423 } 424 return 0; 425 } 426 427 /* sys I/F for generic thermal sysfs support */ 428 429 static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp) 430 { 431 struct acpi_thermal *tz = thermal_zone_device_priv(thermal); 432 int result; 433 434 if (!tz) 435 return -EINVAL; 436 437 result = acpi_thermal_get_temperature(tz); 438 if (result) 439 return result; 440 441 *temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature, 442 tz->kelvin_offset); 443 return 0; 444 } 445 446 static int thermal_get_trip_type(struct thermal_zone_device *thermal, 447 int trip, enum thermal_trip_type *type) 448 { 449 struct acpi_thermal *tz = thermal_zone_device_priv(thermal); 450 int i; 451 452 if (!tz || trip < 0) 453 return -EINVAL; 454 455 if (tz->trips.critical.valid) { 456 if (!trip) { 457 *type = THERMAL_TRIP_CRITICAL; 458 return 0; 459 } 460 trip--; 461 } 462 463 if (tz->trips.hot.valid) { 464 if (!trip) { 465 *type = THERMAL_TRIP_HOT; 466 return 0; 467 } 468 trip--; 469 } 470 471 if (tz->trips.passive.valid) { 472 if (!trip) { 473 *type = THERMAL_TRIP_PASSIVE; 474 return 0; 475 } 476 trip--; 477 } 478 479 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && tz->trips.active[i].valid; i++) { 480 if (!trip) { 481 *type = THERMAL_TRIP_ACTIVE; 482 return 0; 483 } 484 trip--; 485 } 486 487 return -EINVAL; 488 } 489 490 static int thermal_get_trip_temp(struct thermal_zone_device *thermal, 491 int trip, int *temp) 492 { 493 struct acpi_thermal *tz = thermal_zone_device_priv(thermal); 494 int i; 495 496 if (!tz || trip < 0) 497 return -EINVAL; 498 499 if (tz->trips.critical.valid) { 500 if (!trip) { 501 *temp = deci_kelvin_to_millicelsius_with_offset( 502 tz->trips.critical.temperature, 503 tz->kelvin_offset); 504 return 0; 505 } 506 trip--; 507 } 508 509 if (tz->trips.hot.valid) { 510 if (!trip) { 511 *temp = deci_kelvin_to_millicelsius_with_offset( 512 tz->trips.hot.temperature, 513 tz->kelvin_offset); 514 return 0; 515 } 516 trip--; 517 } 518 519 if (tz->trips.passive.valid) { 520 if (!trip) { 521 *temp = deci_kelvin_to_millicelsius_with_offset( 522 tz->trips.passive.temperature, 523 tz->kelvin_offset); 524 return 0; 525 } 526 trip--; 527 } 528 529 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && 530 tz->trips.active[i].valid; i++) { 531 if (!trip) { 532 *temp = deci_kelvin_to_millicelsius_with_offset( 533 tz->trips.active[i].temperature, 534 tz->kelvin_offset); 535 return 0; 536 } 537 trip--; 538 } 539 540 return -EINVAL; 541 } 542 543 static int thermal_get_crit_temp(struct thermal_zone_device *thermal, 544 int *temperature) 545 { 546 struct acpi_thermal *tz = thermal_zone_device_priv(thermal); 547 548 if (tz->trips.critical.valid) { 549 *temperature = deci_kelvin_to_millicelsius_with_offset( 550 tz->trips.critical.temperature, 551 tz->kelvin_offset); 552 return 0; 553 } 554 555 return -EINVAL; 556 } 557 558 static int thermal_get_trend(struct thermal_zone_device *thermal, 559 int trip, enum thermal_trend *trend) 560 { 561 struct acpi_thermal *tz = thermal_zone_device_priv(thermal); 562 enum thermal_trip_type type; 563 int i; 564 565 if (thermal_get_trip_type(thermal, trip, &type)) 566 return -EINVAL; 567 568 if (type == THERMAL_TRIP_ACTIVE) { 569 int trip_temp; 570 int temp = deci_kelvin_to_millicelsius_with_offset( 571 tz->temperature, tz->kelvin_offset); 572 if (thermal_get_trip_temp(thermal, trip, &trip_temp)) 573 return -EINVAL; 574 575 if (temp > trip_temp) { 576 *trend = THERMAL_TREND_RAISING; 577 return 0; 578 } else { 579 /* Fall back on default trend */ 580 return -EINVAL; 581 } 582 } 583 584 /* 585 * tz->temperature has already been updated by generic thermal layer, 586 * before this callback being invoked 587 */ 588 i = tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature) + 589 tz->trips.passive.tc2 * (tz->temperature - tz->trips.passive.temperature); 590 591 if (i > 0) 592 *trend = THERMAL_TREND_RAISING; 593 else if (i < 0) 594 *trend = THERMAL_TREND_DROPPING; 595 else 596 *trend = THERMAL_TREND_STABLE; 597 598 return 0; 599 } 600 601 static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal) 602 { 603 struct acpi_thermal *tz = thermal_zone_device_priv(thermal); 604 605 acpi_bus_generate_netlink_event(tz->device->pnp.device_class, 606 dev_name(&tz->device->dev), 607 ACPI_THERMAL_NOTIFY_HOT, 1); 608 } 609 610 static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal) 611 { 612 struct acpi_thermal *tz = thermal_zone_device_priv(thermal); 613 614 acpi_bus_generate_netlink_event(tz->device->pnp.device_class, 615 dev_name(&tz->device->dev), 616 ACPI_THERMAL_NOTIFY_CRITICAL, 1); 617 618 thermal_zone_device_critical(thermal); 619 } 620 621 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal, 622 struct thermal_cooling_device *cdev, 623 bool bind) 624 { 625 struct acpi_device *device = cdev->devdata; 626 struct acpi_thermal *tz = thermal_zone_device_priv(thermal); 627 struct acpi_device *dev; 628 acpi_handle handle; 629 int i; 630 int j; 631 int trip = -1; 632 int result = 0; 633 634 if (tz->trips.critical.valid) 635 trip++; 636 637 if (tz->trips.hot.valid) 638 trip++; 639 640 if (tz->trips.passive.valid) { 641 trip++; 642 for (i = 0; i < tz->trips.passive.devices.count; i++) { 643 handle = tz->trips.passive.devices.handles[i]; 644 dev = acpi_fetch_acpi_dev(handle); 645 if (dev != device) 646 continue; 647 648 if (bind) 649 result = thermal_zone_bind_cooling_device( 650 thermal, trip, cdev, 651 THERMAL_NO_LIMIT, 652 THERMAL_NO_LIMIT, 653 THERMAL_WEIGHT_DEFAULT); 654 else 655 result = 656 thermal_zone_unbind_cooling_device( 657 thermal, trip, cdev); 658 659 if (result) 660 goto failed; 661 } 662 } 663 664 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { 665 if (!tz->trips.active[i].valid) 666 break; 667 668 trip++; 669 for (j = 0; j < tz->trips.active[i].devices.count; j++) { 670 handle = tz->trips.active[i].devices.handles[j]; 671 dev = acpi_fetch_acpi_dev(handle); 672 if (dev != device) 673 continue; 674 675 if (bind) 676 result = thermal_zone_bind_cooling_device( 677 thermal, trip, cdev, 678 THERMAL_NO_LIMIT, 679 THERMAL_NO_LIMIT, 680 THERMAL_WEIGHT_DEFAULT); 681 else 682 result = thermal_zone_unbind_cooling_device( 683 thermal, trip, cdev); 684 685 if (result) 686 goto failed; 687 } 688 } 689 690 failed: 691 return result; 692 } 693 694 static int 695 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal, 696 struct thermal_cooling_device *cdev) 697 { 698 return acpi_thermal_cooling_device_cb(thermal, cdev, true); 699 } 700 701 static int 702 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal, 703 struct thermal_cooling_device *cdev) 704 { 705 return acpi_thermal_cooling_device_cb(thermal, cdev, false); 706 } 707 708 static struct thermal_zone_device_ops acpi_thermal_zone_ops = { 709 .bind = acpi_thermal_bind_cooling_device, 710 .unbind = acpi_thermal_unbind_cooling_device, 711 .get_temp = thermal_get_temp, 712 .get_trip_type = thermal_get_trip_type, 713 .get_trip_temp = thermal_get_trip_temp, 714 .get_crit_temp = thermal_get_crit_temp, 715 .get_trend = thermal_get_trend, 716 .hot = acpi_thermal_zone_device_hot, 717 .critical = acpi_thermal_zone_device_critical, 718 }; 719 720 static int acpi_thermal_zone_sysfs_add(struct acpi_thermal *tz) 721 { 722 struct device *tzdev = thermal_zone_device(tz->thermal_zone); 723 int ret; 724 725 ret = sysfs_create_link(&tz->device->dev.kobj, 726 &tzdev->kobj, "thermal_zone"); 727 if (ret) 728 return ret; 729 730 ret = sysfs_create_link(&tzdev->kobj, 731 &tz->device->dev.kobj, "device"); 732 if (ret) 733 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone"); 734 735 return ret; 736 } 737 738 static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz) 739 { 740 struct device *tzdev = thermal_zone_device(tz->thermal_zone); 741 742 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone"); 743 sysfs_remove_link(&tzdev->kobj, "device"); 744 } 745 746 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz) 747 { 748 int trips = 0; 749 int result; 750 acpi_status status; 751 int i; 752 753 if (tz->trips.critical.valid) 754 trips++; 755 756 if (tz->trips.hot.valid) 757 trips++; 758 759 if (tz->trips.passive.valid) 760 trips++; 761 762 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && tz->trips.active[i].valid; 763 i++, trips++); 764 765 if (tz->trips.passive.valid) 766 tz->thermal_zone = thermal_zone_device_register("acpitz", trips, 0, tz, 767 &acpi_thermal_zone_ops, NULL, 768 tz->trips.passive.tsp * 100, 769 tz->polling_frequency * 100); 770 else 771 tz->thermal_zone = 772 thermal_zone_device_register("acpitz", trips, 0, tz, 773 &acpi_thermal_zone_ops, NULL, 774 0, tz->polling_frequency * 100); 775 776 if (IS_ERR(tz->thermal_zone)) 777 return -ENODEV; 778 779 result = acpi_thermal_zone_sysfs_add(tz); 780 if (result) 781 goto unregister_tzd; 782 783 status = acpi_bus_attach_private_data(tz->device->handle, 784 tz->thermal_zone); 785 if (ACPI_FAILURE(status)) { 786 result = -ENODEV; 787 goto remove_links; 788 } 789 790 result = thermal_zone_device_enable(tz->thermal_zone); 791 if (result) 792 goto acpi_bus_detach; 793 794 dev_info(&tz->device->dev, "registered as thermal_zone%d\n", 795 thermal_zone_device_id(tz->thermal_zone)); 796 797 return 0; 798 799 acpi_bus_detach: 800 acpi_bus_detach_private_data(tz->device->handle); 801 remove_links: 802 acpi_thermal_zone_sysfs_remove(tz); 803 unregister_tzd: 804 thermal_zone_device_unregister(tz->thermal_zone); 805 806 return result; 807 } 808 809 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz) 810 { 811 acpi_thermal_zone_sysfs_remove(tz); 812 thermal_zone_device_unregister(tz->thermal_zone); 813 tz->thermal_zone = NULL; 814 acpi_bus_detach_private_data(tz->device->handle); 815 } 816 817 818 /* -------------------------------------------------------------------------- 819 Driver Interface 820 -------------------------------------------------------------------------- */ 821 822 static void acpi_queue_thermal_check(struct acpi_thermal *tz) 823 { 824 if (!work_pending(&tz->thermal_check_work)) 825 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work); 826 } 827 828 static void acpi_thermal_notify(struct acpi_device *device, u32 event) 829 { 830 struct acpi_thermal *tz = acpi_driver_data(device); 831 832 if (!tz) 833 return; 834 835 switch (event) { 836 case ACPI_THERMAL_NOTIFY_TEMPERATURE: 837 acpi_queue_thermal_check(tz); 838 break; 839 case ACPI_THERMAL_NOTIFY_THRESHOLDS: 840 acpi_thermal_trips_update(tz, ACPI_TRIPS_THRESHOLDS); 841 acpi_queue_thermal_check(tz); 842 acpi_bus_generate_netlink_event(device->pnp.device_class, 843 dev_name(&device->dev), event, 0); 844 break; 845 case ACPI_THERMAL_NOTIFY_DEVICES: 846 acpi_thermal_trips_update(tz, ACPI_TRIPS_DEVICES); 847 acpi_queue_thermal_check(tz); 848 acpi_bus_generate_netlink_event(device->pnp.device_class, 849 dev_name(&device->dev), event, 0); 850 break; 851 default: 852 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n", 853 event); 854 break; 855 } 856 } 857 858 /* 859 * On some platforms, the AML code has dependency about 860 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx. 861 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after 862 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off. 863 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0 864 * if _TMP has never been evaluated. 865 * 866 * As this dependency is totally transparent to OS, evaluate 867 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx, 868 * _TMP, before they are actually used. 869 */ 870 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz) 871 { 872 acpi_handle handle = tz->device->handle; 873 unsigned long long value; 874 int i; 875 876 acpi_evaluate_integer(handle, "_CRT", NULL, &value); 877 acpi_evaluate_integer(handle, "_HOT", NULL, &value); 878 acpi_evaluate_integer(handle, "_PSV", NULL, &value); 879 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { 880 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' }; 881 acpi_status status; 882 883 status = acpi_evaluate_integer(handle, name, NULL, &value); 884 if (status == AE_NOT_FOUND) 885 break; 886 } 887 acpi_evaluate_integer(handle, "_TMP", NULL, &value); 888 } 889 890 static int acpi_thermal_get_info(struct acpi_thermal *tz) 891 { 892 int result; 893 894 if (!tz) 895 return -EINVAL; 896 897 acpi_thermal_aml_dependency_fix(tz); 898 899 /* Get trip points [_CRT, _PSV, etc.] (required) */ 900 result = acpi_thermal_get_trip_points(tz); 901 if (result) 902 return result; 903 904 /* Get temperature [_TMP] (required) */ 905 result = acpi_thermal_get_temperature(tz); 906 if (result) 907 return result; 908 909 /* Set the cooling mode [_SCP] to active cooling (default) */ 910 acpi_execute_simple_method(tz->device->handle, "_SCP", 911 ACPI_THERMAL_MODE_ACTIVE); 912 913 /* Get default polling frequency [_TZP] (optional) */ 914 if (tzp) 915 tz->polling_frequency = tzp; 916 else 917 acpi_thermal_get_polling_frequency(tz); 918 919 return 0; 920 } 921 922 /* 923 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI 924 * handles temperature values with a single decimal place. As a consequence, 925 * some implementations use an offset of 273.1 and others use an offset of 926 * 273.2. Try to find out which one is being used, to present the most 927 * accurate and visually appealing number. 928 * 929 * The heuristic below should work for all ACPI thermal zones which have a 930 * critical trip point with a value being a multiple of 0.5 degree Celsius. 931 */ 932 static void acpi_thermal_guess_offset(struct acpi_thermal *tz) 933 { 934 if (tz->trips.critical.valid && 935 (tz->trips.critical.temperature % 5) == 1) 936 tz->kelvin_offset = 273100; 937 else 938 tz->kelvin_offset = 273200; 939 } 940 941 static void acpi_thermal_check_fn(struct work_struct *work) 942 { 943 struct acpi_thermal *tz = container_of(work, struct acpi_thermal, 944 thermal_check_work); 945 946 /* 947 * In general, it is not sufficient to check the pending bit, because 948 * subsequent instances of this function may be queued after one of them 949 * has started running (e.g. if _TMP sleeps). Avoid bailing out if just 950 * one of them is running, though, because it may have done the actual 951 * check some time ago, so allow at least one of them to block on the 952 * mutex while another one is running the update. 953 */ 954 if (!refcount_dec_not_one(&tz->thermal_check_count)) 955 return; 956 957 mutex_lock(&tz->thermal_check_lock); 958 959 thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED); 960 961 refcount_inc(&tz->thermal_check_count); 962 963 mutex_unlock(&tz->thermal_check_lock); 964 } 965 966 static int acpi_thermal_add(struct acpi_device *device) 967 { 968 struct acpi_thermal *tz; 969 int result; 970 971 if (!device) 972 return -EINVAL; 973 974 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL); 975 if (!tz) 976 return -ENOMEM; 977 978 tz->device = device; 979 strcpy(tz->name, device->pnp.bus_id); 980 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME); 981 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS); 982 device->driver_data = tz; 983 984 result = acpi_thermal_get_info(tz); 985 if (result) 986 goto free_memory; 987 988 acpi_thermal_guess_offset(tz); 989 990 result = acpi_thermal_register_thermal_zone(tz); 991 if (result) 992 goto free_memory; 993 994 refcount_set(&tz->thermal_check_count, 3); 995 mutex_init(&tz->thermal_check_lock); 996 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn); 997 998 pr_info("%s [%s] (%ld C)\n", acpi_device_name(device), 999 acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature)); 1000 goto end; 1001 1002 free_memory: 1003 kfree(tz); 1004 end: 1005 return result; 1006 } 1007 1008 static void acpi_thermal_remove(struct acpi_device *device) 1009 { 1010 struct acpi_thermal *tz; 1011 1012 if (!device || !acpi_driver_data(device)) 1013 return; 1014 1015 flush_workqueue(acpi_thermal_pm_queue); 1016 tz = acpi_driver_data(device); 1017 1018 acpi_thermal_unregister_thermal_zone(tz); 1019 kfree(tz); 1020 } 1021 1022 #ifdef CONFIG_PM_SLEEP 1023 static int acpi_thermal_suspend(struct device *dev) 1024 { 1025 /* Make sure the previously queued thermal check work has been done */ 1026 flush_workqueue(acpi_thermal_pm_queue); 1027 return 0; 1028 } 1029 1030 static int acpi_thermal_resume(struct device *dev) 1031 { 1032 struct acpi_thermal *tz; 1033 int i, j, power_state, result; 1034 1035 if (!dev) 1036 return -EINVAL; 1037 1038 tz = acpi_driver_data(to_acpi_device(dev)); 1039 if (!tz) 1040 return -EINVAL; 1041 1042 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { 1043 if (!tz->trips.active[i].valid) 1044 break; 1045 1046 tz->trips.active[i].enabled = true; 1047 for (j = 0; j < tz->trips.active[i].devices.count; j++) { 1048 result = acpi_bus_update_power( 1049 tz->trips.active[i].devices.handles[j], 1050 &power_state); 1051 if (result || (power_state != ACPI_STATE_D0)) { 1052 tz->trips.active[i].enabled = false; 1053 break; 1054 } 1055 } 1056 } 1057 1058 acpi_queue_thermal_check(tz); 1059 1060 return AE_OK; 1061 } 1062 #else 1063 #define acpi_thermal_suspend NULL 1064 #define acpi_thermal_resume NULL 1065 #endif 1066 static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume); 1067 1068 static const struct acpi_device_id thermal_device_ids[] = { 1069 {ACPI_THERMAL_HID, 0}, 1070 {"", 0}, 1071 }; 1072 MODULE_DEVICE_TABLE(acpi, thermal_device_ids); 1073 1074 static struct acpi_driver acpi_thermal_driver = { 1075 .name = "thermal", 1076 .class = ACPI_THERMAL_CLASS, 1077 .ids = thermal_device_ids, 1078 .ops = { 1079 .add = acpi_thermal_add, 1080 .remove = acpi_thermal_remove, 1081 .notify = acpi_thermal_notify, 1082 }, 1083 .drv.pm = &acpi_thermal_pm, 1084 }; 1085 1086 static int thermal_act(const struct dmi_system_id *d) { 1087 if (act == 0) { 1088 pr_notice("%s detected: disabling all active thermal trip points\n", 1089 d->ident); 1090 act = -1; 1091 } 1092 return 0; 1093 } 1094 static int thermal_nocrt(const struct dmi_system_id *d) { 1095 pr_notice("%s detected: disabling all critical thermal trip point actions.\n", 1096 d->ident); 1097 nocrt = 1; 1098 return 0; 1099 } 1100 static int thermal_tzp(const struct dmi_system_id *d) { 1101 if (tzp == 0) { 1102 pr_notice("%s detected: enabling thermal zone polling\n", 1103 d->ident); 1104 tzp = 300; /* 300 dS = 30 Seconds */ 1105 } 1106 return 0; 1107 } 1108 static int thermal_psv(const struct dmi_system_id *d) { 1109 if (psv == 0) { 1110 pr_notice("%s detected: disabling all passive thermal trip points\n", 1111 d->ident); 1112 psv = -1; 1113 } 1114 return 0; 1115 } 1116 1117 static const struct dmi_system_id thermal_dmi_table[] __initconst = { 1118 /* 1119 * Award BIOS on this AOpen makes thermal control almost worthless. 1120 * http://bugzilla.kernel.org/show_bug.cgi?id=8842 1121 */ 1122 { 1123 .callback = thermal_act, 1124 .ident = "AOpen i915GMm-HFS", 1125 .matches = { 1126 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"), 1127 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"), 1128 }, 1129 }, 1130 { 1131 .callback = thermal_psv, 1132 .ident = "AOpen i915GMm-HFS", 1133 .matches = { 1134 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"), 1135 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"), 1136 }, 1137 }, 1138 { 1139 .callback = thermal_tzp, 1140 .ident = "AOpen i915GMm-HFS", 1141 .matches = { 1142 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"), 1143 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"), 1144 }, 1145 }, 1146 { 1147 .callback = thermal_nocrt, 1148 .ident = "Gigabyte GA-7ZX", 1149 .matches = { 1150 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."), 1151 DMI_MATCH(DMI_BOARD_NAME, "7ZX"), 1152 }, 1153 }, 1154 {} 1155 }; 1156 1157 static int __init acpi_thermal_init(void) 1158 { 1159 int result; 1160 1161 dmi_check_system(thermal_dmi_table); 1162 1163 if (off) { 1164 pr_notice("thermal control disabled\n"); 1165 return -ENODEV; 1166 } 1167 1168 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm", 1169 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0); 1170 if (!acpi_thermal_pm_queue) 1171 return -ENODEV; 1172 1173 result = acpi_bus_register_driver(&acpi_thermal_driver); 1174 if (result < 0) { 1175 destroy_workqueue(acpi_thermal_pm_queue); 1176 return -ENODEV; 1177 } 1178 1179 return 0; 1180 } 1181 1182 static void __exit acpi_thermal_exit(void) 1183 { 1184 acpi_bus_unregister_driver(&acpi_thermal_driver); 1185 destroy_workqueue(acpi_thermal_pm_queue); 1186 } 1187 1188 module_init(acpi_thermal_init); 1189 module_exit(acpi_thermal_exit); 1190 1191 MODULE_AUTHOR("Paul Diefenbaugh"); 1192 MODULE_DESCRIPTION("ACPI Thermal Zone Driver"); 1193 MODULE_LICENSE("GPL"); 1194