1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * thermal.c - Generic Thermal Management Sysfs support. 4 * 5 * Copyright (C) 2008 Intel Corp 6 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> 7 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/device.h> 13 #include <linux/err.h> 14 #include <linux/export.h> 15 #include <linux/slab.h> 16 #include <linux/kdev_t.h> 17 #include <linux/idr.h> 18 #include <linux/thermal.h> 19 #include <linux/reboot.h> 20 #include <linux/string.h> 21 #include <linux/of.h> 22 #include <linux/suspend.h> 23 24 #define CREATE_TRACE_POINTS 25 #include "thermal_trace.h" 26 27 #include "thermal_core.h" 28 #include "thermal_hwmon.h" 29 30 static DEFINE_IDA(thermal_tz_ida); 31 static DEFINE_IDA(thermal_cdev_ida); 32 33 static LIST_HEAD(thermal_tz_list); 34 static LIST_HEAD(thermal_cdev_list); 35 static LIST_HEAD(thermal_governor_list); 36 37 static DEFINE_MUTEX(thermal_list_lock); 38 static DEFINE_MUTEX(thermal_governor_lock); 39 40 static struct thermal_governor *def_governor; 41 42 static bool thermal_pm_suspended; 43 44 static struct workqueue_struct *thermal_wq __ro_after_init; 45 46 /* 47 * Governor section: set of functions to handle thermal governors 48 * 49 * Functions to help in the life cycle of thermal governors within 50 * the thermal core and by the thermal governor code. 51 */ 52 53 static struct thermal_governor *__find_governor(const char *name) 54 { 55 struct thermal_governor *pos; 56 57 if (!name || !name[0]) 58 return def_governor; 59 60 list_for_each_entry(pos, &thermal_governor_list, governor_list) 61 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH)) 62 return pos; 63 64 return NULL; 65 } 66 67 /** 68 * bind_previous_governor() - bind the previous governor of the thermal zone 69 * @tz: a valid pointer to a struct thermal_zone_device 70 * @failed_gov_name: the name of the governor that failed to register 71 * 72 * Register the previous governor of the thermal zone after a new 73 * governor has failed to be bound. 74 */ 75 static void bind_previous_governor(struct thermal_zone_device *tz, 76 const char *failed_gov_name) 77 { 78 if (tz->governor && tz->governor->bind_to_tz) { 79 if (tz->governor->bind_to_tz(tz)) { 80 dev_err(&tz->device, 81 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n", 82 failed_gov_name, tz->governor->name, tz->type); 83 tz->governor = NULL; 84 } 85 } 86 } 87 88 /** 89 * thermal_set_governor() - Switch to another governor 90 * @tz: a valid pointer to a struct thermal_zone_device 91 * @new_gov: pointer to the new governor 92 * 93 * Change the governor of thermal zone @tz. 94 * 95 * Return: 0 on success, an error if the new governor's bind_to_tz() failed. 96 */ 97 static int thermal_set_governor(struct thermal_zone_device *tz, 98 struct thermal_governor *new_gov) 99 { 100 int ret = 0; 101 102 if (tz->governor && tz->governor->unbind_from_tz) 103 tz->governor->unbind_from_tz(tz); 104 105 if (new_gov && new_gov->bind_to_tz) { 106 ret = new_gov->bind_to_tz(tz); 107 if (ret) { 108 bind_previous_governor(tz, new_gov->name); 109 110 return ret; 111 } 112 } 113 114 tz->governor = new_gov; 115 116 return ret; 117 } 118 119 int thermal_register_governor(struct thermal_governor *governor) 120 { 121 int err; 122 const char *name; 123 struct thermal_zone_device *pos; 124 125 if (!governor) 126 return -EINVAL; 127 128 guard(mutex)(&thermal_governor_lock); 129 130 err = -EBUSY; 131 if (!__find_governor(governor->name)) { 132 bool match_default; 133 134 err = 0; 135 list_add(&governor->governor_list, &thermal_governor_list); 136 match_default = !strncmp(governor->name, 137 DEFAULT_THERMAL_GOVERNOR, 138 THERMAL_NAME_LENGTH); 139 140 if (!def_governor && match_default) 141 def_governor = governor; 142 } 143 144 guard(mutex)(&thermal_list_lock); 145 146 list_for_each_entry(pos, &thermal_tz_list, node) { 147 /* 148 * only thermal zones with specified tz->tzp->governor_name 149 * may run with tz->govenor unset 150 */ 151 if (pos->governor) 152 continue; 153 154 name = pos->tzp->governor_name; 155 156 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) { 157 int ret; 158 159 ret = thermal_set_governor(pos, governor); 160 if (ret) 161 dev_err(&pos->device, 162 "Failed to set governor %s for thermal zone %s: %d\n", 163 governor->name, pos->type, ret); 164 } 165 } 166 167 return err; 168 } 169 170 void thermal_unregister_governor(struct thermal_governor *governor) 171 { 172 struct thermal_zone_device *pos; 173 174 if (!governor) 175 return; 176 177 guard(mutex)(&thermal_governor_lock); 178 179 if (!__find_governor(governor->name)) 180 return; 181 182 list_del(&governor->governor_list); 183 184 guard(mutex)(&thermal_list_lock); 185 186 list_for_each_entry(pos, &thermal_tz_list, node) { 187 if (!strncasecmp(pos->governor->name, governor->name, 188 THERMAL_NAME_LENGTH)) 189 thermal_set_governor(pos, NULL); 190 } 191 } 192 193 int thermal_zone_device_set_policy(struct thermal_zone_device *tz, 194 char *policy) 195 { 196 struct thermal_governor *gov; 197 int ret = -EINVAL; 198 199 guard(mutex)(&thermal_governor_lock); 200 guard(thermal_zone)(tz); 201 202 gov = __find_governor(strim(policy)); 203 if (gov) 204 ret = thermal_set_governor(tz, gov); 205 206 thermal_notify_tz_gov_change(tz, policy); 207 208 return ret; 209 } 210 211 int thermal_build_list_of_policies(char *buf) 212 { 213 struct thermal_governor *pos; 214 ssize_t count = 0; 215 216 guard(mutex)(&thermal_governor_lock); 217 218 list_for_each_entry(pos, &thermal_governor_list, governor_list) { 219 count += sysfs_emit_at(buf, count, "%s ", pos->name); 220 } 221 count += sysfs_emit_at(buf, count, "\n"); 222 223 return count; 224 } 225 226 static void __init thermal_unregister_governors(void) 227 { 228 struct thermal_governor **governor; 229 230 for_each_governor_table(governor) 231 thermal_unregister_governor(*governor); 232 } 233 234 static int __init thermal_register_governors(void) 235 { 236 int ret = 0; 237 struct thermal_governor **governor; 238 239 for_each_governor_table(governor) { 240 ret = thermal_register_governor(*governor); 241 if (ret) { 242 pr_err("Failed to register governor: '%s'", 243 (*governor)->name); 244 break; 245 } 246 247 pr_info("Registered thermal governor '%s'", 248 (*governor)->name); 249 } 250 251 if (ret) { 252 struct thermal_governor **gov; 253 254 for_each_governor_table(gov) { 255 if (gov == governor) 256 break; 257 thermal_unregister_governor(*gov); 258 } 259 } 260 261 return ret; 262 } 263 264 static int __thermal_zone_device_set_mode(struct thermal_zone_device *tz, 265 enum thermal_device_mode mode) 266 { 267 if (tz->ops.change_mode) { 268 int ret; 269 270 ret = tz->ops.change_mode(tz, mode); 271 if (ret) 272 return ret; 273 } 274 275 tz->mode = mode; 276 277 return 0; 278 } 279 280 static void thermal_zone_broken_disable(struct thermal_zone_device *tz) 281 { 282 struct thermal_trip_desc *td; 283 284 dev_err(&tz->device, "Unable to get temperature, disabling!\n"); 285 /* 286 * This function only runs for enabled thermal zones, so no need to 287 * check for the current mode. 288 */ 289 __thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED); 290 thermal_notify_tz_disable(tz); 291 292 for_each_trip_desc(tz, td) { 293 if (td->trip.type == THERMAL_TRIP_CRITICAL && 294 td->trip.temperature > THERMAL_TEMP_INVALID) { 295 dev_crit(&tz->device, 296 "Disabled thermal zone with critical trip point\n"); 297 return; 298 } 299 } 300 } 301 302 /* 303 * Zone update section: main control loop applied to each zone while monitoring 304 * in polling mode. The monitoring is done using a workqueue. 305 * Same update may be done on a zone by calling thermal_zone_device_update(). 306 * 307 * An update means: 308 * - Non-critical trips will invoke the governor responsible for that zone; 309 * - Hot trips will produce a notification to userspace; 310 * - Critical trip point will cause a system shutdown. 311 */ 312 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, 313 unsigned long delay) 314 { 315 if (delay > HZ) 316 delay = round_jiffies_relative(delay); 317 318 mod_delayed_work(thermal_wq, &tz->poll_queue, delay); 319 } 320 321 static void thermal_zone_recheck(struct thermal_zone_device *tz, int error) 322 { 323 if (error == -EAGAIN) { 324 thermal_zone_device_set_polling(tz, THERMAL_RECHECK_DELAY); 325 return; 326 } 327 328 /* 329 * Print the message once to reduce log noise. It will be followed by 330 * another one if the temperature cannot be determined after multiple 331 * attempts. 332 */ 333 if (tz->recheck_delay_jiffies == THERMAL_RECHECK_DELAY) 334 dev_info(&tz->device, "Temperature check failed (%d)\n", error); 335 336 thermal_zone_device_set_polling(tz, tz->recheck_delay_jiffies); 337 338 tz->recheck_delay_jiffies += max(tz->recheck_delay_jiffies >> 1, 1ULL); 339 if (tz->recheck_delay_jiffies > THERMAL_MAX_RECHECK_DELAY) { 340 thermal_zone_broken_disable(tz); 341 /* 342 * Restore the original recheck delay value to allow the thermal 343 * zone to try to recover when it is reenabled by user space. 344 */ 345 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY; 346 } 347 } 348 349 static void monitor_thermal_zone(struct thermal_zone_device *tz) 350 { 351 if (tz->passive > 0 && tz->passive_delay_jiffies) 352 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies); 353 else if (tz->polling_delay_jiffies) 354 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies); 355 } 356 357 static struct thermal_governor *thermal_get_tz_governor(struct thermal_zone_device *tz) 358 { 359 if (tz->governor) 360 return tz->governor; 361 362 return def_governor; 363 } 364 365 void thermal_governor_update_tz(struct thermal_zone_device *tz, 366 enum thermal_notify_event reason) 367 { 368 if (!tz->governor || !tz->governor->update_tz) 369 return; 370 371 tz->governor->update_tz(tz, reason); 372 } 373 374 static void thermal_zone_device_halt(struct thermal_zone_device *tz, 375 enum hw_protection_action action) 376 { 377 /* 378 * poweroff_delay_ms must be a carefully profiled positive value. 379 * Its a must for forced_emergency_poweroff_work to be scheduled. 380 */ 381 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS; 382 const char *msg = "Temperature too high"; 383 384 dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type); 385 386 __hw_protection_trigger(msg, poweroff_delay_ms, action); 387 } 388 389 void thermal_zone_device_critical(struct thermal_zone_device *tz) 390 { 391 thermal_zone_device_halt(tz, HWPROT_ACT_DEFAULT); 392 } 393 EXPORT_SYMBOL(thermal_zone_device_critical); 394 395 void thermal_zone_device_critical_shutdown(struct thermal_zone_device *tz) 396 { 397 thermal_zone_device_halt(tz, HWPROT_ACT_SHUTDOWN); 398 } 399 400 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz) 401 { 402 thermal_zone_device_halt(tz, HWPROT_ACT_REBOOT); 403 } 404 405 static void handle_critical_trips(struct thermal_zone_device *tz, 406 const struct thermal_trip *trip) 407 { 408 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type); 409 410 if (trip->type == THERMAL_TRIP_CRITICAL) 411 tz->ops.critical(tz); 412 else if (tz->ops.hot) 413 tz->ops.hot(tz); 414 } 415 416 static void move_trip_to_sorted_list(struct thermal_trip_desc *td, 417 struct list_head *list) 418 { 419 struct thermal_trip_desc *entry; 420 421 /* 422 * Delete upfront and then add to make relocation within the same list 423 * work. 424 */ 425 list_del(&td->list_node); 426 427 /* Assume that the new entry is likely to be the last one. */ 428 list_for_each_entry_reverse(entry, list, list_node) { 429 if (entry->threshold <= td->threshold) { 430 list_add(&td->list_node, &entry->list_node); 431 return; 432 } 433 } 434 list_add(&td->list_node, list); 435 } 436 437 static void move_to_trips_high(struct thermal_zone_device *tz, 438 struct thermal_trip_desc *td) 439 { 440 td->threshold = td->trip.temperature; 441 move_trip_to_sorted_list(td, &tz->trips_high); 442 } 443 444 static void move_to_trips_reached(struct thermal_zone_device *tz, 445 struct thermal_trip_desc *td) 446 { 447 td->threshold = td->trip.temperature - td->trip.hysteresis; 448 move_trip_to_sorted_list(td, &tz->trips_reached); 449 } 450 451 static void move_to_trips_invalid(struct thermal_zone_device *tz, 452 struct thermal_trip_desc *td) 453 { 454 td->threshold = INT_MAX; 455 list_move(&td->list_node, &tz->trips_invalid); 456 } 457 458 static void thermal_governor_trip_crossed(struct thermal_governor *governor, 459 struct thermal_zone_device *tz, 460 const struct thermal_trip *trip, 461 bool upward) 462 { 463 if (trip->type == THERMAL_TRIP_HOT || trip->type == THERMAL_TRIP_CRITICAL) 464 return; 465 466 if (governor->trip_crossed) 467 governor->trip_crossed(tz, trip, upward); 468 } 469 470 static void thermal_trip_crossed(struct thermal_zone_device *tz, 471 struct thermal_trip_desc *td, 472 struct thermal_governor *governor, 473 bool upward) 474 { 475 const struct thermal_trip *trip = &td->trip; 476 477 if (upward) { 478 if (trip->type == THERMAL_TRIP_PASSIVE) 479 tz->passive++; 480 else if (trip->type == THERMAL_TRIP_CRITICAL || 481 trip->type == THERMAL_TRIP_HOT) 482 handle_critical_trips(tz, trip); 483 484 thermal_notify_tz_trip_up(tz, trip); 485 thermal_debug_tz_trip_up(tz, trip); 486 } else { 487 if (trip->type == THERMAL_TRIP_PASSIVE) { 488 tz->passive--; 489 WARN_ON(tz->passive < 0); 490 } 491 thermal_notify_tz_trip_down(tz, trip); 492 thermal_debug_tz_trip_down(tz, trip); 493 } 494 thermal_governor_trip_crossed(governor, tz, trip, upward); 495 } 496 497 void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz, 498 struct thermal_trip *trip, int hyst) 499 { 500 struct thermal_trip_desc *td = trip_to_trip_desc(trip); 501 502 WRITE_ONCE(trip->hysteresis, hyst); 503 thermal_notify_tz_trip_change(tz, trip); 504 /* 505 * If the zone temperature is above or at the trip temperature, the trip 506 * is in the trips_reached list and its threshold is equal to its low 507 * temperature. It needs to stay in that list, but its threshold needs 508 * to be updated and the list ordering may need to be restored. 509 */ 510 if (tz->temperature >= td->threshold) 511 move_to_trips_reached(tz, td); 512 } 513 514 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz, 515 struct thermal_trip *trip, int temp) 516 { 517 struct thermal_trip_desc *td = trip_to_trip_desc(trip); 518 int old_temp = trip->temperature; 519 520 if (old_temp == temp) 521 return; 522 523 WRITE_ONCE(trip->temperature, temp); 524 thermal_notify_tz_trip_change(tz, trip); 525 526 if (old_temp == THERMAL_TEMP_INVALID) { 527 /* 528 * The trip was invalid before the change, so move it to the 529 * trips_high list regardless of the new temperature value 530 * because there is no mitigation under way for it. If a 531 * mitigation needs to be started, the trip will be moved to the 532 * trips_reached list later. 533 */ 534 move_to_trips_high(tz, td); 535 return; 536 } 537 538 if (temp == THERMAL_TEMP_INVALID) { 539 /* 540 * If the trip is in the trips_reached list, mitigation is under 541 * way for it and it needs to be stopped because the trip is 542 * effectively going away. 543 */ 544 if (tz->temperature >= td->threshold) 545 thermal_trip_crossed(tz, td, thermal_get_tz_governor(tz), false); 546 547 move_to_trips_invalid(tz, td); 548 return; 549 } 550 551 /* 552 * The trip stays on its current list, but its threshold needs to be 553 * updated due to the temperature change and the list ordering may need 554 * to be restored. 555 */ 556 if (tz->temperature >= td->threshold) 557 move_to_trips_reached(tz, td); 558 else 559 move_to_trips_high(tz, td); 560 } 561 EXPORT_SYMBOL_GPL(thermal_zone_set_trip_temp); 562 563 static void thermal_zone_handle_trips(struct thermal_zone_device *tz, 564 struct thermal_governor *governor, 565 int *low, int *high) 566 { 567 struct thermal_trip_desc *td, *next; 568 LIST_HEAD(way_down_list); 569 570 /* Check the trips that were below or at the zone temperature. */ 571 list_for_each_entry_safe_reverse(td, next, &tz->trips_reached, list_node) { 572 if (td->threshold <= tz->temperature) 573 break; 574 575 thermal_trip_crossed(tz, td, governor, false); 576 /* 577 * The current trips_high list needs to be processed before 578 * adding new entries to it, so put them on a temporary list. 579 */ 580 list_move(&td->list_node, &way_down_list); 581 } 582 /* Check the trips that were previously above the zone temperature. */ 583 list_for_each_entry_safe(td, next, &tz->trips_high, list_node) { 584 if (td->threshold > tz->temperature) 585 break; 586 587 thermal_trip_crossed(tz, td, governor, true); 588 move_to_trips_reached(tz, td); 589 } 590 /* Move all of the trips from the temporary list to trips_high. */ 591 list_for_each_entry_safe(td, next, &way_down_list, list_node) 592 move_to_trips_high(tz, td); 593 594 if (!list_empty(&tz->trips_reached)) { 595 td = list_last_entry(&tz->trips_reached, 596 struct thermal_trip_desc, list_node); 597 /* 598 * Set the "low" value below the current trip threshold in case 599 * the zone temperature is at that threshold and stays there, 600 * which would trigger a new interrupt immediately in vain. 601 */ 602 *low = td->threshold - 1; 603 } 604 if (!list_empty(&tz->trips_high)) { 605 td = list_first_entry(&tz->trips_high, 606 struct thermal_trip_desc, list_node); 607 *high = td->threshold; 608 } 609 } 610 611 void __thermal_zone_device_update(struct thermal_zone_device *tz, 612 enum thermal_notify_event event) 613 { 614 struct thermal_governor *governor = thermal_get_tz_governor(tz); 615 int low = -INT_MAX, high = INT_MAX; 616 int temp, ret; 617 618 if (tz->state != TZ_STATE_READY || tz->mode != THERMAL_DEVICE_ENABLED) 619 return; 620 621 ret = __thermal_zone_get_temp(tz, &temp); 622 if (ret) { 623 thermal_zone_recheck(tz, ret); 624 return; 625 } else if (temp <= THERMAL_TEMP_INVALID) { 626 /* 627 * Special case: No valid temperature value is available, but 628 * the zone owner does not want the core to do anything about 629 * it. Continue regular zone polling if needed, so that this 630 * function can be called again, but skip everything else. 631 */ 632 goto monitor; 633 } 634 635 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY; 636 637 tz->last_temperature = tz->temperature; 638 tz->temperature = temp; 639 640 trace_thermal_temperature(tz); 641 642 thermal_genl_sampling_temp(tz->id, temp); 643 644 tz->notify_event = event; 645 646 thermal_zone_handle_trips(tz, governor, &low, &high); 647 648 thermal_thresholds_handle(tz, &low, &high); 649 650 thermal_zone_set_trips(tz, low, high); 651 652 if (governor->manage) 653 governor->manage(tz); 654 655 thermal_debug_update_trip_stats(tz); 656 657 monitor: 658 monitor_thermal_zone(tz); 659 } 660 661 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz, 662 enum thermal_device_mode mode) 663 { 664 int ret; 665 666 guard(thermal_zone)(tz); 667 668 /* do nothing if mode isn't changing */ 669 if (mode == tz->mode) 670 return 0; 671 672 ret = __thermal_zone_device_set_mode(tz, mode); 673 if (ret) 674 return ret; 675 676 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 677 678 if (mode == THERMAL_DEVICE_ENABLED) 679 thermal_notify_tz_enable(tz); 680 else 681 thermal_notify_tz_disable(tz); 682 683 return 0; 684 } 685 686 int thermal_zone_device_enable(struct thermal_zone_device *tz) 687 { 688 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED); 689 } 690 EXPORT_SYMBOL_GPL(thermal_zone_device_enable); 691 692 int thermal_zone_device_disable(struct thermal_zone_device *tz) 693 { 694 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED); 695 } 696 EXPORT_SYMBOL_GPL(thermal_zone_device_disable); 697 698 static bool thermal_zone_is_present(struct thermal_zone_device *tz) 699 { 700 return !list_empty(&tz->node); 701 } 702 703 void thermal_zone_device_update(struct thermal_zone_device *tz, 704 enum thermal_notify_event event) 705 { 706 guard(thermal_zone)(tz); 707 708 if (thermal_zone_is_present(tz)) 709 __thermal_zone_device_update(tz, event); 710 } 711 EXPORT_SYMBOL_GPL(thermal_zone_device_update); 712 713 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), 714 void *data) 715 { 716 struct thermal_governor *gov; 717 718 guard(mutex)(&thermal_governor_lock); 719 720 list_for_each_entry(gov, &thermal_governor_list, governor_list) { 721 int ret; 722 723 ret = cb(gov, data); 724 if (ret) 725 return ret; 726 } 727 728 return 0; 729 } 730 731 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *, 732 void *), void *data) 733 { 734 struct thermal_cooling_device *cdev; 735 736 guard(mutex)(&thermal_list_lock); 737 738 list_for_each_entry(cdev, &thermal_cdev_list, node) { 739 int ret; 740 741 ret = cb(cdev, data); 742 if (ret) 743 return ret; 744 } 745 746 return 0; 747 } 748 749 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *), 750 void *data) 751 { 752 struct thermal_zone_device *tz; 753 754 guard(mutex)(&thermal_list_lock); 755 756 list_for_each_entry(tz, &thermal_tz_list, node) { 757 int ret; 758 759 ret = cb(tz, data); 760 if (ret) 761 return ret; 762 } 763 764 return 0; 765 } 766 767 struct thermal_zone_device *thermal_zone_get_by_id(int id) 768 { 769 struct thermal_zone_device *tz; 770 771 guard(mutex)(&thermal_list_lock); 772 773 list_for_each_entry(tz, &thermal_tz_list, node) { 774 if (tz->id == id) { 775 get_device(&tz->device); 776 return tz; 777 } 778 } 779 780 return NULL; 781 } 782 783 /* 784 * Device management section: cooling devices, zones devices, and binding 785 * 786 * Set of functions provided by the thermal core for: 787 * - cooling devices lifecycle: registration, unregistration, 788 * binding, and unbinding. 789 * - thermal zone devices lifecycle: registration, unregistration, 790 * binding, and unbinding. 791 */ 792 793 static int thermal_instance_add(struct thermal_instance *new_instance, 794 struct thermal_cooling_device *cdev, 795 struct thermal_trip_desc *td) 796 { 797 struct thermal_instance *instance; 798 799 list_for_each_entry(instance, &td->thermal_instances, trip_node) { 800 if (instance->cdev == cdev) 801 return -EEXIST; 802 } 803 804 list_add_tail(&new_instance->trip_node, &td->thermal_instances); 805 806 guard(cooling_dev)(cdev); 807 808 list_add_tail(&new_instance->cdev_node, &cdev->thermal_instances); 809 810 return 0; 811 } 812 813 /** 814 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone 815 * @tz: pointer to struct thermal_zone_device 816 * @td: descriptor of the trip point to bind @cdev to 817 * @cdev: pointer to struct thermal_cooling_device 818 * @cool_spec: cooling specification for the trip point and @cdev 819 * 820 * This interface function bind a thermal cooling device to the certain trip 821 * point of a thermal zone device. 822 * This function is usually called in the thermal zone device .bind callback. 823 * 824 * Return: 0 on success, the proper error value otherwise. 825 */ 826 static int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz, 827 struct thermal_trip_desc *td, 828 struct thermal_cooling_device *cdev, 829 struct cooling_spec *cool_spec) 830 { 831 struct thermal_instance *dev; 832 bool upper_no_limit; 833 int result; 834 835 /* lower default 0, upper default max_state */ 836 if (cool_spec->lower == THERMAL_NO_LIMIT) 837 cool_spec->lower = 0; 838 839 if (cool_spec->upper == THERMAL_NO_LIMIT) { 840 cool_spec->upper = cdev->max_state; 841 upper_no_limit = true; 842 } else { 843 upper_no_limit = false; 844 } 845 846 if (cool_spec->lower > cool_spec->upper || cool_spec->upper > cdev->max_state) 847 return -EINVAL; 848 849 dev = kzalloc_obj(*dev); 850 if (!dev) 851 return -ENOMEM; 852 853 dev->cdev = cdev; 854 dev->trip = &td->trip; 855 dev->upper = cool_spec->upper; 856 dev->upper_no_limit = upper_no_limit; 857 dev->lower = cool_spec->lower; 858 dev->target = THERMAL_NO_TARGET; 859 dev->weight = cool_spec->weight; 860 861 result = ida_alloc(&tz->ida, GFP_KERNEL); 862 if (result < 0) 863 goto free_mem; 864 865 dev->id = result; 866 sprintf(dev->name, "cdev%d", dev->id); 867 result = 868 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); 869 if (result) 870 goto release_ida; 871 872 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point", 873 dev->id); 874 sysfs_attr_init(&dev->attr.attr); 875 dev->attr.attr.name = dev->attr_name; 876 dev->attr.attr.mode = 0444; 877 dev->attr.show = trip_point_show; 878 result = device_create_file(&tz->device, &dev->attr); 879 if (result) 880 goto remove_symbol_link; 881 882 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name), 883 "cdev%d_weight", dev->id); 884 sysfs_attr_init(&dev->weight_attr.attr); 885 dev->weight_attr.attr.name = dev->weight_attr_name; 886 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO; 887 dev->weight_attr.show = weight_show; 888 dev->weight_attr.store = weight_store; 889 result = device_create_file(&tz->device, &dev->weight_attr); 890 if (result) 891 goto remove_trip_file; 892 893 result = thermal_instance_add(dev, cdev, td); 894 if (result) 895 goto remove_weight_file; 896 897 thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV); 898 899 return 0; 900 901 remove_weight_file: 902 device_remove_file(&tz->device, &dev->weight_attr); 903 remove_trip_file: 904 device_remove_file(&tz->device, &dev->attr); 905 remove_symbol_link: 906 sysfs_remove_link(&tz->device.kobj, dev->name); 907 release_ida: 908 ida_free(&tz->ida, dev->id); 909 free_mem: 910 kfree(dev); 911 return result; 912 } 913 914 static void thermal_instance_delete(struct thermal_instance *instance) 915 { 916 list_del(&instance->trip_node); 917 918 guard(cooling_dev)(instance->cdev); 919 920 list_del(&instance->cdev_node); 921 } 922 923 /** 924 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone. 925 * @tz: pointer to a struct thermal_zone_device. 926 * @td: descriptor of the trip point to unbind @cdev from 927 * @cdev: pointer to a struct thermal_cooling_device. 928 * 929 * This interface function unbind a thermal cooling device from the certain 930 * trip point of a thermal zone device. 931 * This function is usually called in the thermal zone device .unbind callback. 932 */ 933 static void thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz, 934 struct thermal_trip_desc *td, 935 struct thermal_cooling_device *cdev) 936 { 937 struct thermal_instance *pos, *next; 938 939 list_for_each_entry_safe(pos, next, &td->thermal_instances, trip_node) { 940 if (pos->cdev == cdev) { 941 thermal_instance_delete(pos); 942 goto unbind; 943 } 944 } 945 946 return; 947 948 unbind: 949 thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV); 950 951 device_remove_file(&tz->device, &pos->weight_attr); 952 device_remove_file(&tz->device, &pos->attr); 953 sysfs_remove_link(&tz->device.kobj, pos->name); 954 ida_free(&tz->ida, pos->id); 955 kfree(pos); 956 } 957 958 static void thermal_release(struct device *dev) 959 { 960 struct thermal_zone_device *tz; 961 struct thermal_cooling_device *cdev; 962 963 if (!strncmp(dev_name(dev), "thermal_zone", 964 sizeof("thermal_zone") - 1)) { 965 tz = to_thermal_zone(dev); 966 thermal_zone_destroy_device_groups(tz); 967 mutex_destroy(&tz->lock); 968 complete(&tz->removal); 969 } else if (!strncmp(dev_name(dev), "cooling_device", 970 sizeof("cooling_device") - 1)) { 971 cdev = to_cooling_device(dev); 972 thermal_cooling_device_destroy_sysfs(cdev); 973 kfree_const(cdev->type); 974 ida_free(&thermal_cdev_ida, cdev->id); 975 kfree(cdev); 976 } 977 } 978 979 static struct class *thermal_class; 980 981 static inline 982 void print_bind_err_msg(struct thermal_zone_device *tz, 983 const struct thermal_trip_desc *td, 984 struct thermal_cooling_device *cdev, int ret) 985 { 986 dev_err(&tz->device, "binding cdev %s to trip %d failed: %d\n", 987 cdev->type, thermal_zone_trip_id(tz, &td->trip), ret); 988 } 989 990 static bool __thermal_zone_cdev_bind(struct thermal_zone_device *tz, 991 struct thermal_cooling_device *cdev) 992 { 993 struct thermal_trip_desc *td; 994 bool update_tz = false; 995 996 if (!tz->ops.should_bind) 997 return false; 998 999 for_each_trip_desc(tz, td) { 1000 struct cooling_spec c = { 1001 .upper = THERMAL_NO_LIMIT, 1002 .lower = THERMAL_NO_LIMIT, 1003 .weight = THERMAL_WEIGHT_DEFAULT 1004 }; 1005 int ret; 1006 1007 if (!tz->ops.should_bind(tz, &td->trip, cdev, &c)) 1008 continue; 1009 1010 ret = thermal_bind_cdev_to_trip(tz, td, cdev, &c); 1011 if (ret) { 1012 print_bind_err_msg(tz, td, cdev, ret); 1013 continue; 1014 } 1015 1016 update_tz = true; 1017 } 1018 1019 return update_tz; 1020 } 1021 1022 static void thermal_zone_cdev_bind(struct thermal_zone_device *tz, 1023 struct thermal_cooling_device *cdev) 1024 { 1025 guard(thermal_zone)(tz); 1026 1027 if (__thermal_zone_cdev_bind(tz, cdev)) 1028 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 1029 } 1030 1031 static void thermal_cooling_device_init_complete(struct thermal_cooling_device *cdev) 1032 { 1033 struct thermal_zone_device *tz; 1034 1035 guard(mutex)(&thermal_list_lock); 1036 1037 list_add(&cdev->node, &thermal_cdev_list); 1038 1039 list_for_each_entry(tz, &thermal_tz_list, node) 1040 thermal_zone_cdev_bind(tz, cdev); 1041 } 1042 1043 /** 1044 * __thermal_cooling_device_register() - register a new thermal cooling device 1045 * @np: a pointer to a device tree node. 1046 * @type: the thermal cooling device type. 1047 * @devdata: device private data. 1048 * @ops: standard thermal cooling devices callbacks. 1049 * 1050 * This interface function adds a new thermal cooling device (fan/processor/...) 1051 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1052 * to all the thermal zone devices registered at the same time. 1053 * It also gives the opportunity to link the cooling device to a device tree 1054 * node, so that it can be bound to a thermal zone created out of device tree. 1055 * 1056 * Return: a pointer to the created struct thermal_cooling_device or an 1057 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1058 */ 1059 static struct thermal_cooling_device * 1060 __thermal_cooling_device_register(struct device_node *np, 1061 const char *type, void *devdata, 1062 const struct thermal_cooling_device_ops *ops) 1063 { 1064 struct thermal_cooling_device *cdev; 1065 unsigned long current_state; 1066 int id, ret; 1067 1068 if (!ops || !ops->get_max_state || !ops->get_cur_state || 1069 !ops->set_cur_state) 1070 return ERR_PTR(-EINVAL); 1071 1072 if (!thermal_class) 1073 return ERR_PTR(-ENODEV); 1074 1075 cdev = kzalloc_obj(*cdev); 1076 if (!cdev) 1077 return ERR_PTR(-ENOMEM); 1078 1079 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL); 1080 if (ret < 0) 1081 goto out_kfree_cdev; 1082 cdev->id = ret; 1083 id = ret; 1084 1085 cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL); 1086 if (!cdev->type) { 1087 ret = -ENOMEM; 1088 goto out_ida_remove; 1089 } 1090 1091 mutex_init(&cdev->lock); 1092 INIT_LIST_HEAD(&cdev->thermal_instances); 1093 cdev->np = np; 1094 cdev->ops = ops; 1095 cdev->updated = false; 1096 cdev->device.class = thermal_class; 1097 cdev->devdata = devdata; 1098 1099 ret = cdev->ops->get_max_state(cdev, &cdev->max_state); 1100 if (ret) 1101 goto out_cdev_type; 1102 1103 /* 1104 * The cooling device's current state is only needed for debug 1105 * initialization below, so a failure to get it does not cause 1106 * the entire cooling device initialization to fail. However, 1107 * the debug will not work for the device if its initial state 1108 * cannot be determined and drivers are responsible for ensuring 1109 * that this will not happen. 1110 */ 1111 ret = cdev->ops->get_cur_state(cdev, ¤t_state); 1112 if (ret) 1113 current_state = ULONG_MAX; 1114 1115 thermal_cooling_device_setup_sysfs(cdev); 1116 1117 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id); 1118 if (ret) 1119 goto out_cooling_dev; 1120 1121 ret = device_register(&cdev->device); 1122 if (ret) { 1123 /* thermal_release() handles rest of the cleanup */ 1124 put_device(&cdev->device); 1125 return ERR_PTR(ret); 1126 } 1127 1128 if (current_state <= cdev->max_state) 1129 thermal_debug_cdev_add(cdev, current_state); 1130 1131 thermal_cooling_device_init_complete(cdev); 1132 1133 return cdev; 1134 1135 out_cooling_dev: 1136 thermal_cooling_device_destroy_sysfs(cdev); 1137 out_cdev_type: 1138 kfree_const(cdev->type); 1139 out_ida_remove: 1140 ida_free(&thermal_cdev_ida, id); 1141 out_kfree_cdev: 1142 kfree(cdev); 1143 return ERR_PTR(ret); 1144 } 1145 1146 /** 1147 * thermal_cooling_device_register() - register a new thermal cooling device 1148 * @type: the thermal cooling device type. 1149 * @devdata: device private data. 1150 * @ops: standard thermal cooling devices callbacks. 1151 * 1152 * This interface function adds a new thermal cooling device (fan/processor/...) 1153 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1154 * to all the thermal zone devices registered at the same time. 1155 * 1156 * Return: a pointer to the created struct thermal_cooling_device or an 1157 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1158 */ 1159 struct thermal_cooling_device * 1160 thermal_cooling_device_register(const char *type, void *devdata, 1161 const struct thermal_cooling_device_ops *ops) 1162 { 1163 return __thermal_cooling_device_register(NULL, type, devdata, ops); 1164 } 1165 EXPORT_SYMBOL_GPL(thermal_cooling_device_register); 1166 1167 /** 1168 * thermal_of_cooling_device_register() - register an OF thermal cooling device 1169 * @np: a pointer to a device tree node. 1170 * @type: the thermal cooling device type. 1171 * @devdata: device private data. 1172 * @ops: standard thermal cooling devices callbacks. 1173 * 1174 * This function will register a cooling device with device tree node reference. 1175 * This interface function adds a new thermal cooling device (fan/processor/...) 1176 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1177 * to all the thermal zone devices registered at the same time. 1178 * 1179 * Return: a pointer to the created struct thermal_cooling_device or an 1180 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1181 */ 1182 struct thermal_cooling_device * 1183 thermal_of_cooling_device_register(struct device_node *np, 1184 const char *type, void *devdata, 1185 const struct thermal_cooling_device_ops *ops) 1186 { 1187 return __thermal_cooling_device_register(np, type, devdata, ops); 1188 } 1189 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register); 1190 1191 static void thermal_cooling_device_release(struct device *dev, void *res) 1192 { 1193 thermal_cooling_device_unregister( 1194 *(struct thermal_cooling_device **)res); 1195 } 1196 1197 /** 1198 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling 1199 * device 1200 * @dev: a valid struct device pointer of a sensor device. 1201 * @np: a pointer to a device tree node. 1202 * @type: the thermal cooling device type. 1203 * @devdata: device private data. 1204 * @ops: standard thermal cooling devices callbacks. 1205 * 1206 * This function will register a cooling device with device tree node reference. 1207 * This interface function adds a new thermal cooling device (fan/processor/...) 1208 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1209 * to all the thermal zone devices registered at the same time. 1210 * 1211 * Return: a pointer to the created struct thermal_cooling_device or an 1212 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1213 */ 1214 struct thermal_cooling_device * 1215 devm_thermal_of_cooling_device_register(struct device *dev, 1216 struct device_node *np, 1217 const char *type, void *devdata, 1218 const struct thermal_cooling_device_ops *ops) 1219 { 1220 struct thermal_cooling_device **ptr, *tcd; 1221 1222 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr), 1223 GFP_KERNEL); 1224 if (!ptr) 1225 return ERR_PTR(-ENOMEM); 1226 1227 tcd = __thermal_cooling_device_register(np, type, devdata, ops); 1228 if (IS_ERR(tcd)) { 1229 devres_free(ptr); 1230 return tcd; 1231 } 1232 1233 *ptr = tcd; 1234 devres_add(dev, ptr); 1235 1236 return tcd; 1237 } 1238 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register); 1239 1240 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev) 1241 { 1242 struct thermal_cooling_device *pos = NULL; 1243 1244 list_for_each_entry(pos, &thermal_cdev_list, node) { 1245 if (pos == cdev) 1246 return true; 1247 } 1248 1249 return false; 1250 } 1251 1252 /** 1253 * thermal_cooling_device_update - Update a cooling device object 1254 * @cdev: Target cooling device. 1255 * 1256 * Update @cdev to reflect a change of the underlying hardware or platform. 1257 * 1258 * Must be called when the maximum cooling state of @cdev becomes invalid and so 1259 * its .get_max_state() callback needs to be run to produce the new maximum 1260 * cooling state value. 1261 */ 1262 void thermal_cooling_device_update(struct thermal_cooling_device *cdev) 1263 { 1264 struct thermal_instance *ti; 1265 unsigned long state; 1266 1267 if (IS_ERR_OR_NULL(cdev)) 1268 return; 1269 1270 /* 1271 * Hold thermal_list_lock throughout the update to prevent the device 1272 * from going away while being updated. 1273 */ 1274 guard(mutex)(&thermal_list_lock); 1275 1276 if (!thermal_cooling_device_present(cdev)) 1277 return; 1278 1279 /* 1280 * Update under the cdev lock to prevent the state from being set beyond 1281 * the new limit concurrently. 1282 */ 1283 guard(cooling_dev)(cdev); 1284 1285 if (cdev->ops->get_max_state(cdev, &cdev->max_state)) 1286 return; 1287 1288 thermal_cooling_device_stats_reinit(cdev); 1289 1290 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) { 1291 if (ti->upper == cdev->max_state) 1292 continue; 1293 1294 if (ti->upper < cdev->max_state) { 1295 if (ti->upper_no_limit) 1296 ti->upper = cdev->max_state; 1297 1298 continue; 1299 } 1300 1301 ti->upper = cdev->max_state; 1302 if (ti->lower > ti->upper) 1303 ti->lower = ti->upper; 1304 1305 if (ti->target == THERMAL_NO_TARGET) 1306 continue; 1307 1308 if (ti->target > ti->upper) 1309 ti->target = ti->upper; 1310 } 1311 1312 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state) 1313 return; 1314 1315 thermal_cooling_device_stats_update(cdev, state); 1316 } 1317 EXPORT_SYMBOL_GPL(thermal_cooling_device_update); 1318 1319 static void __thermal_zone_cdev_unbind(struct thermal_zone_device *tz, 1320 struct thermal_cooling_device *cdev) 1321 { 1322 struct thermal_trip_desc *td; 1323 1324 for_each_trip_desc(tz, td) 1325 thermal_unbind_cdev_from_trip(tz, td, cdev); 1326 } 1327 1328 static void thermal_zone_cdev_unbind(struct thermal_zone_device *tz, 1329 struct thermal_cooling_device *cdev) 1330 { 1331 guard(thermal_zone)(tz); 1332 1333 __thermal_zone_cdev_unbind(tz, cdev); 1334 } 1335 1336 static bool thermal_cooling_device_exit(struct thermal_cooling_device *cdev) 1337 { 1338 struct thermal_zone_device *tz; 1339 1340 guard(mutex)(&thermal_list_lock); 1341 1342 if (!thermal_cooling_device_present(cdev)) 1343 return false; 1344 1345 list_del(&cdev->node); 1346 1347 list_for_each_entry(tz, &thermal_tz_list, node) 1348 thermal_zone_cdev_unbind(tz, cdev); 1349 1350 return true; 1351 } 1352 1353 /** 1354 * thermal_cooling_device_unregister() - removes a thermal cooling device 1355 * @cdev: Thermal cooling device to remove. 1356 */ 1357 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) 1358 { 1359 if (!cdev) 1360 return; 1361 1362 thermal_debug_cdev_remove(cdev); 1363 1364 if (thermal_cooling_device_exit(cdev)) 1365 device_unregister(&cdev->device); 1366 } 1367 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); 1368 1369 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) 1370 { 1371 const struct thermal_trip_desc *td; 1372 int ret = -EINVAL; 1373 1374 if (tz->ops.get_crit_temp) 1375 return tz->ops.get_crit_temp(tz, temp); 1376 1377 guard(thermal_zone)(tz); 1378 1379 for_each_trip_desc(tz, td) { 1380 const struct thermal_trip *trip = &td->trip; 1381 1382 if (trip->type == THERMAL_TRIP_CRITICAL) { 1383 *temp = trip->temperature; 1384 ret = 0; 1385 break; 1386 } 1387 } 1388 1389 return ret; 1390 } 1391 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp); 1392 1393 static void thermal_zone_device_check(struct work_struct *work) 1394 { 1395 struct thermal_zone_device *tz = container_of(work, struct 1396 thermal_zone_device, 1397 poll_queue.work); 1398 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 1399 } 1400 1401 static void thermal_zone_device_init(struct thermal_zone_device *tz) 1402 { 1403 struct thermal_trip_desc *td, *next; 1404 1405 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check); 1406 1407 tz->temperature = THERMAL_TEMP_INIT; 1408 tz->passive = 0; 1409 tz->prev_low_trip = -INT_MAX; 1410 tz->prev_high_trip = INT_MAX; 1411 for_each_trip_desc(tz, td) { 1412 struct thermal_instance *instance; 1413 1414 list_for_each_entry(instance, &td->thermal_instances, trip_node) 1415 instance->initialized = false; 1416 } 1417 /* 1418 * At this point, all valid trips need to be moved to trips_high so that 1419 * mitigation can be started if the zone temperature is above them. 1420 */ 1421 list_for_each_entry_safe(td, next, &tz->trips_invalid, list_node) { 1422 if (td->trip.temperature != THERMAL_TEMP_INVALID) 1423 move_to_trips_high(tz, td); 1424 } 1425 /* The trips_reached list may not be empty during system resume. */ 1426 list_for_each_entry_safe(td, next, &tz->trips_reached, list_node) { 1427 if (td->trip.temperature == THERMAL_TEMP_INVALID) 1428 move_to_trips_invalid(tz, td); 1429 else 1430 move_to_trips_high(tz, td); 1431 } 1432 } 1433 1434 static int thermal_zone_init_governor(struct thermal_zone_device *tz) 1435 { 1436 struct thermal_governor *governor; 1437 1438 guard(mutex)(&thermal_governor_lock); 1439 1440 if (tz->tzp) 1441 governor = __find_governor(tz->tzp->governor_name); 1442 else 1443 governor = def_governor; 1444 1445 return thermal_set_governor(tz, governor); 1446 } 1447 1448 static void thermal_zone_init_complete(struct thermal_zone_device *tz) 1449 { 1450 struct thermal_cooling_device *cdev; 1451 1452 guard(mutex)(&thermal_list_lock); 1453 1454 list_add_tail(&tz->node, &thermal_tz_list); 1455 1456 guard(thermal_zone)(tz); 1457 1458 /* Bind cooling devices for this zone. */ 1459 list_for_each_entry(cdev, &thermal_cdev_list, node) 1460 __thermal_zone_cdev_bind(tz, cdev); 1461 1462 tz->state &= ~TZ_STATE_FLAG_INIT; 1463 /* 1464 * If system suspend or resume is in progress at this point, the 1465 * new thermal zone needs to be marked as suspended because 1466 * thermal_pm_notify() has run already. 1467 */ 1468 if (thermal_pm_suspended) 1469 tz->state |= TZ_STATE_FLAG_SUSPENDED; 1470 1471 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 1472 } 1473 1474 /** 1475 * thermal_zone_device_register_with_trips() - register a new thermal zone device 1476 * @type: the thermal zone device type 1477 * @trips: a pointer to an array of thermal trips 1478 * @num_trips: the number of trip points the thermal zone support 1479 * @devdata: private device data 1480 * @ops: standard thermal zone device callbacks 1481 * @tzp: thermal zone platform parameters 1482 * @passive_delay: number of milliseconds to wait between polls when 1483 * performing passive cooling 1484 * @polling_delay: number of milliseconds to wait between polls when checking 1485 * whether trip points have been crossed (0 for interrupt 1486 * driven systems) 1487 * 1488 * This interface function adds a new thermal zone device (sensor) to 1489 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the 1490 * thermal cooling devices registered at the same time. 1491 * thermal_zone_device_unregister() must be called when the device is no 1492 * longer needed. The passive cooling depends on the .get_trend() return value. 1493 * 1494 * Return: a pointer to the created struct thermal_zone_device or an 1495 * in case of error, an ERR_PTR. Caller must check return value with 1496 * IS_ERR*() helpers. 1497 */ 1498 struct thermal_zone_device * 1499 thermal_zone_device_register_with_trips(const char *type, 1500 const struct thermal_trip *trips, 1501 int num_trips, void *devdata, 1502 const struct thermal_zone_device_ops *ops, 1503 const struct thermal_zone_params *tzp, 1504 unsigned int passive_delay, 1505 unsigned int polling_delay) 1506 { 1507 const struct thermal_trip *trip = trips; 1508 struct thermal_zone_device *tz; 1509 struct thermal_trip_desc *td; 1510 size_t type_len = 0; 1511 int id; 1512 int result; 1513 1514 if (type) 1515 type_len = strnlen(type, THERMAL_NAME_LENGTH); 1516 1517 if (type_len == 0) { 1518 pr_err("No thermal zone type defined\n"); 1519 return ERR_PTR(-EINVAL); 1520 } 1521 1522 if (type_len == THERMAL_NAME_LENGTH) { 1523 pr_err("Thermal zone name (%s) too long, should be under %d chars\n", 1524 type, THERMAL_NAME_LENGTH); 1525 return ERR_PTR(-EINVAL); 1526 } 1527 1528 if (num_trips < 0) { 1529 pr_err("Incorrect number of thermal trips\n"); 1530 return ERR_PTR(-EINVAL); 1531 } 1532 1533 if (!ops || !ops->get_temp) { 1534 pr_err("Thermal zone device ops not defined or invalid\n"); 1535 return ERR_PTR(-EINVAL); 1536 } 1537 1538 if (num_trips > 0 && !trips) 1539 return ERR_PTR(-EINVAL); 1540 1541 if (polling_delay && passive_delay > polling_delay) 1542 return ERR_PTR(-EINVAL); 1543 1544 if (!thermal_class) 1545 return ERR_PTR(-ENODEV); 1546 1547 tz = kzalloc_flex(*tz, trips, num_trips); 1548 if (!tz) 1549 return ERR_PTR(-ENOMEM); 1550 1551 if (tzp) { 1552 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL); 1553 if (!tz->tzp) { 1554 result = -ENOMEM; 1555 goto free_tz; 1556 } 1557 } 1558 1559 INIT_LIST_HEAD(&tz->node); 1560 INIT_LIST_HEAD(&tz->trips_high); 1561 INIT_LIST_HEAD(&tz->trips_reached); 1562 INIT_LIST_HEAD(&tz->trips_invalid); 1563 ida_init(&tz->ida); 1564 mutex_init(&tz->lock); 1565 init_completion(&tz->removal); 1566 init_completion(&tz->resume); 1567 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL); 1568 if (id < 0) { 1569 result = id; 1570 goto free_tzp; 1571 } 1572 1573 tz->id = id; 1574 strscpy(tz->type, type, sizeof(tz->type)); 1575 1576 tz->ops = *ops; 1577 if (!tz->ops.critical) 1578 tz->ops.critical = thermal_zone_device_critical; 1579 1580 tz->device.class = thermal_class; 1581 tz->devdata = devdata; 1582 tz->num_trips = num_trips; 1583 for_each_trip_desc(tz, td) { 1584 td->trip = *trip++; 1585 INIT_LIST_HEAD(&td->thermal_instances); 1586 INIT_LIST_HEAD(&td->list_node); 1587 /* 1588 * Mark all thresholds as invalid to start with even though 1589 * this only matters for the trips that start as invalid and 1590 * become valid later. 1591 */ 1592 move_to_trips_invalid(tz, td); 1593 } 1594 1595 tz->polling_delay_jiffies = msecs_to_jiffies(polling_delay); 1596 tz->passive_delay_jiffies = msecs_to_jiffies(passive_delay); 1597 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY; 1598 1599 tz->state = TZ_STATE_FLAG_INIT; 1600 1601 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id); 1602 if (result) 1603 goto remove_id; 1604 1605 thermal_zone_device_init(tz); 1606 1607 result = thermal_zone_init_governor(tz); 1608 if (result) 1609 goto remove_id; 1610 1611 /* sys I/F */ 1612 /* Add nodes that are always present via .groups */ 1613 result = thermal_zone_create_device_groups(tz); 1614 if (result) 1615 goto remove_id; 1616 1617 result = device_register(&tz->device); 1618 if (result) 1619 goto release_device; 1620 1621 if (!tz->tzp || !tz->tzp->no_hwmon) { 1622 result = thermal_add_hwmon_sysfs(tz); 1623 if (result) 1624 goto unregister; 1625 } 1626 1627 result = thermal_thresholds_init(tz); 1628 if (result) 1629 goto remove_hwmon; 1630 1631 thermal_zone_init_complete(tz); 1632 1633 thermal_notify_tz_create(tz); 1634 1635 thermal_debug_tz_add(tz); 1636 1637 return tz; 1638 1639 remove_hwmon: 1640 thermal_remove_hwmon_sysfs(tz); 1641 unregister: 1642 device_del(&tz->device); 1643 release_device: 1644 put_device(&tz->device); 1645 wait_for_completion(&tz->removal); 1646 remove_id: 1647 ida_free(&thermal_tz_ida, id); 1648 free_tzp: 1649 kfree(tz->tzp); 1650 free_tz: 1651 kfree(tz); 1652 return ERR_PTR(result); 1653 } 1654 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips); 1655 1656 struct thermal_zone_device *thermal_tripless_zone_device_register( 1657 const char *type, 1658 void *devdata, 1659 const struct thermal_zone_device_ops *ops, 1660 const struct thermal_zone_params *tzp) 1661 { 1662 return thermal_zone_device_register_with_trips(type, NULL, 0, devdata, 1663 ops, tzp, 0, 0); 1664 } 1665 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register); 1666 1667 void *thermal_zone_device_priv(struct thermal_zone_device *tzd) 1668 { 1669 return tzd->devdata; 1670 } 1671 EXPORT_SYMBOL_GPL(thermal_zone_device_priv); 1672 1673 const char *thermal_zone_device_type(struct thermal_zone_device *tzd) 1674 { 1675 return tzd->type; 1676 } 1677 EXPORT_SYMBOL_GPL(thermal_zone_device_type); 1678 1679 int thermal_zone_device_id(struct thermal_zone_device *tzd) 1680 { 1681 return tzd->id; 1682 } 1683 EXPORT_SYMBOL_GPL(thermal_zone_device_id); 1684 1685 struct device *thermal_zone_device(struct thermal_zone_device *tzd) 1686 { 1687 return &tzd->device; 1688 } 1689 EXPORT_SYMBOL_GPL(thermal_zone_device); 1690 1691 static bool thermal_zone_exit(struct thermal_zone_device *tz) 1692 { 1693 struct thermal_cooling_device *cdev; 1694 1695 guard(mutex)(&thermal_list_lock); 1696 1697 if (list_empty(&tz->node)) 1698 return false; 1699 1700 guard(thermal_zone)(tz); 1701 1702 tz->state |= TZ_STATE_FLAG_EXIT; 1703 list_del_init(&tz->node); 1704 1705 /* Unbind all cdevs associated with this thermal zone. */ 1706 list_for_each_entry(cdev, &thermal_cdev_list, node) 1707 __thermal_zone_cdev_unbind(tz, cdev); 1708 1709 return true; 1710 } 1711 1712 /** 1713 * thermal_zone_device_unregister - removes the registered thermal zone device 1714 * @tz: the thermal zone device to remove 1715 */ 1716 void thermal_zone_device_unregister(struct thermal_zone_device *tz) 1717 { 1718 if (!tz) 1719 return; 1720 1721 thermal_debug_tz_remove(tz); 1722 1723 if (!thermal_zone_exit(tz)) 1724 return; 1725 1726 cancel_delayed_work_sync(&tz->poll_queue); 1727 1728 thermal_set_governor(tz, NULL); 1729 1730 thermal_thresholds_exit(tz); 1731 thermal_remove_hwmon_sysfs(tz); 1732 ida_free(&thermal_tz_ida, tz->id); 1733 ida_destroy(&tz->ida); 1734 1735 device_del(&tz->device); 1736 put_device(&tz->device); 1737 1738 thermal_notify_tz_delete(tz); 1739 1740 wait_for_completion(&tz->removal); 1741 kfree(tz->tzp); 1742 kfree(tz); 1743 } 1744 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); 1745 1746 /** 1747 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref 1748 * @name: thermal zone name to fetch the temperature 1749 * 1750 * When only one zone is found with the passed name, returns a reference to it. 1751 * 1752 * Return: On success returns a reference to an unique thermal zone with 1753 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid 1754 * paramenters, -ENODEV for not found and -EEXIST for multiple matches). 1755 */ 1756 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) 1757 { 1758 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL); 1759 unsigned int found = 0; 1760 1761 if (!name) 1762 return ERR_PTR(-EINVAL); 1763 1764 guard(mutex)(&thermal_list_lock); 1765 1766 list_for_each_entry(pos, &thermal_tz_list, node) 1767 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) { 1768 found++; 1769 ref = pos; 1770 } 1771 1772 if (!found) 1773 return ERR_PTR(-ENODEV); 1774 1775 /* Success only when one zone is found. */ 1776 if (found > 1) 1777 return ERR_PTR(-EEXIST); 1778 1779 return ref; 1780 } 1781 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); 1782 1783 static void thermal_zone_device_resume(struct work_struct *work) 1784 { 1785 struct thermal_zone_device *tz; 1786 1787 tz = container_of(work, struct thermal_zone_device, poll_queue.work); 1788 1789 guard(thermal_zone)(tz); 1790 1791 /* If the thermal zone is going away, there's nothing to do. */ 1792 if (tz->state & TZ_STATE_FLAG_EXIT) 1793 return; 1794 1795 tz->state &= ~(TZ_STATE_FLAG_SUSPENDED | TZ_STATE_FLAG_RESUMING); 1796 1797 thermal_debug_tz_resume(tz); 1798 thermal_zone_device_init(tz); 1799 thermal_governor_update_tz(tz, THERMAL_TZ_RESUME); 1800 __thermal_zone_device_update(tz, THERMAL_TZ_RESUME); 1801 1802 complete(&tz->resume); 1803 } 1804 1805 static void thermal_zone_pm_prepare(struct thermal_zone_device *tz) 1806 { 1807 guard(thermal_zone)(tz); 1808 1809 if (tz->state & TZ_STATE_FLAG_RESUMING) { 1810 /* 1811 * thermal_zone_device_resume() queued up for this zone has not 1812 * acquired the lock yet, so release it to let the function run 1813 * and wait util it has done the work. 1814 */ 1815 scoped_guard(thermal_zone_reverse, tz) { 1816 wait_for_completion(&tz->resume); 1817 } 1818 } 1819 1820 tz->state |= TZ_STATE_FLAG_SUSPENDED; 1821 1822 /* Prevent new work from getting to the workqueue subsequently. */ 1823 cancel_delayed_work(&tz->poll_queue); 1824 } 1825 1826 static void thermal_pm_notify_prepare(void) 1827 { 1828 struct thermal_zone_device *tz; 1829 1830 guard(mutex)(&thermal_list_lock); 1831 1832 thermal_pm_suspended = true; 1833 1834 list_for_each_entry(tz, &thermal_tz_list, node) 1835 thermal_zone_pm_prepare(tz); 1836 } 1837 1838 static void thermal_zone_pm_complete(struct thermal_zone_device *tz) 1839 { 1840 guard(thermal_zone)(tz); 1841 1842 reinit_completion(&tz->resume); 1843 tz->state |= TZ_STATE_FLAG_RESUMING; 1844 1845 /* 1846 * Replace the work function with the resume one, which will restore the 1847 * original work function and schedule the polling work if needed. 1848 */ 1849 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_resume); 1850 /* Queue up the work without a delay. */ 1851 mod_delayed_work(thermal_wq, &tz->poll_queue, 0); 1852 } 1853 1854 static void thermal_pm_notify_complete(void) 1855 { 1856 struct thermal_zone_device *tz; 1857 1858 guard(mutex)(&thermal_list_lock); 1859 1860 thermal_pm_suspended = false; 1861 1862 list_for_each_entry(tz, &thermal_tz_list, node) 1863 thermal_zone_pm_complete(tz); 1864 } 1865 1866 static int thermal_pm_notify(struct notifier_block *nb, 1867 unsigned long mode, void *_unused) 1868 { 1869 switch (mode) { 1870 case PM_HIBERNATION_PREPARE: 1871 case PM_RESTORE_PREPARE: 1872 case PM_SUSPEND_PREPARE: 1873 thermal_pm_notify_prepare(); 1874 /* 1875 * Allow any leftover thermal work items already on the 1876 * worqueue to complete so they don't get in the way later. 1877 */ 1878 flush_workqueue(thermal_wq); 1879 break; 1880 case PM_POST_HIBERNATION: 1881 case PM_POST_RESTORE: 1882 case PM_POST_SUSPEND: 1883 thermal_pm_notify_complete(); 1884 break; 1885 default: 1886 break; 1887 } 1888 return 0; 1889 } 1890 1891 static struct notifier_block thermal_pm_nb = { 1892 .notifier_call = thermal_pm_notify, 1893 /* 1894 * Run at the lowest priority to avoid interference between the thermal 1895 * zone resume work items spawned by thermal_pm_notify() and the other 1896 * PM notifiers. 1897 */ 1898 .priority = INT_MIN, 1899 }; 1900 1901 static int __init thermal_init(void) 1902 { 1903 int result; 1904 1905 thermal_debug_init(); 1906 1907 result = thermal_netlink_init(); 1908 if (result) 1909 goto error; 1910 1911 thermal_wq = alloc_workqueue("thermal_events", 1912 WQ_FREEZABLE | WQ_POWER_EFFICIENT | WQ_PERCPU, 0); 1913 if (!thermal_wq) { 1914 result = -ENOMEM; 1915 goto unregister_netlink; 1916 } 1917 1918 result = thermal_register_governors(); 1919 if (result) 1920 goto destroy_workqueue; 1921 1922 thermal_class = kzalloc_obj(*thermal_class); 1923 if (!thermal_class) { 1924 result = -ENOMEM; 1925 goto unregister_governors; 1926 } 1927 1928 thermal_class->name = "thermal"; 1929 thermal_class->dev_release = thermal_release; 1930 1931 result = class_register(thermal_class); 1932 if (result) { 1933 kfree(thermal_class); 1934 thermal_class = NULL; 1935 goto unregister_governors; 1936 } 1937 1938 result = register_pm_notifier(&thermal_pm_nb); 1939 if (result) 1940 pr_warn("Thermal: Can not register suspend notifier, return %d\n", 1941 result); 1942 1943 return 0; 1944 1945 unregister_governors: 1946 thermal_unregister_governors(); 1947 destroy_workqueue: 1948 destroy_workqueue(thermal_wq); 1949 unregister_netlink: 1950 thermal_netlink_exit(); 1951 error: 1952 mutex_destroy(&thermal_list_lock); 1953 mutex_destroy(&thermal_governor_lock); 1954 return result; 1955 } 1956 postcore_initcall(thermal_init); 1957