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