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 void thermal_zone_device_update(struct thermal_zone_device *tz, 699 enum thermal_notify_event event) 700 { 701 guard(thermal_zone)(tz); 702 703 __thermal_zone_device_update(tz, event); 704 } 705 EXPORT_SYMBOL_GPL(thermal_zone_device_update); 706 707 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), 708 void *data) 709 { 710 struct thermal_governor *gov; 711 712 guard(mutex)(&thermal_governor_lock); 713 714 list_for_each_entry(gov, &thermal_governor_list, governor_list) { 715 int ret; 716 717 ret = cb(gov, data); 718 if (ret) 719 return ret; 720 } 721 722 return 0; 723 } 724 725 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *, 726 void *), void *data) 727 { 728 struct thermal_cooling_device *cdev; 729 730 guard(mutex)(&thermal_list_lock); 731 732 list_for_each_entry(cdev, &thermal_cdev_list, node) { 733 int ret; 734 735 ret = cb(cdev, data); 736 if (ret) 737 return ret; 738 } 739 740 return 0; 741 } 742 743 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *), 744 void *data) 745 { 746 struct thermal_zone_device *tz; 747 748 guard(mutex)(&thermal_list_lock); 749 750 list_for_each_entry(tz, &thermal_tz_list, node) { 751 int ret; 752 753 ret = cb(tz, data); 754 if (ret) 755 return ret; 756 } 757 758 return 0; 759 } 760 761 struct thermal_zone_device *thermal_zone_get_by_id(int id) 762 { 763 struct thermal_zone_device *tz; 764 765 guard(mutex)(&thermal_list_lock); 766 767 list_for_each_entry(tz, &thermal_tz_list, node) { 768 if (tz->id == id) { 769 get_device(&tz->device); 770 return tz; 771 } 772 } 773 774 return NULL; 775 } 776 777 /* 778 * Device management section: cooling devices, zones devices, and binding 779 * 780 * Set of functions provided by the thermal core for: 781 * - cooling devices lifecycle: registration, unregistration, 782 * binding, and unbinding. 783 * - thermal zone devices lifecycle: registration, unregistration, 784 * binding, and unbinding. 785 */ 786 787 static int thermal_instance_add(struct thermal_instance *new_instance, 788 struct thermal_cooling_device *cdev, 789 struct thermal_trip_desc *td) 790 { 791 struct thermal_instance *instance; 792 793 list_for_each_entry(instance, &td->thermal_instances, trip_node) { 794 if (instance->cdev == cdev) 795 return -EEXIST; 796 } 797 798 list_add_tail(&new_instance->trip_node, &td->thermal_instances); 799 800 guard(cooling_dev)(cdev); 801 802 list_add_tail(&new_instance->cdev_node, &cdev->thermal_instances); 803 804 return 0; 805 } 806 807 /** 808 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone 809 * @tz: pointer to struct thermal_zone_device 810 * @td: descriptor of the trip point to bind @cdev to 811 * @cdev: pointer to struct thermal_cooling_device 812 * @cool_spec: cooling specification for the trip point and @cdev 813 * 814 * This interface function bind a thermal cooling device to the certain trip 815 * point of a thermal zone device. 816 * This function is usually called in the thermal zone device .bind callback. 817 * 818 * Return: 0 on success, the proper error value otherwise. 819 */ 820 static int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz, 821 struct thermal_trip_desc *td, 822 struct thermal_cooling_device *cdev, 823 struct cooling_spec *cool_spec) 824 { 825 struct thermal_instance *dev; 826 bool upper_no_limit; 827 int result; 828 829 /* lower default 0, upper default max_state */ 830 if (cool_spec->lower == THERMAL_NO_LIMIT) 831 cool_spec->lower = 0; 832 833 if (cool_spec->upper == THERMAL_NO_LIMIT) { 834 cool_spec->upper = cdev->max_state; 835 upper_no_limit = true; 836 } else { 837 upper_no_limit = false; 838 } 839 840 if (cool_spec->lower > cool_spec->upper || cool_spec->upper > cdev->max_state) 841 return -EINVAL; 842 843 dev = kzalloc_obj(*dev); 844 if (!dev) 845 return -ENOMEM; 846 847 dev->cdev = cdev; 848 dev->trip = &td->trip; 849 dev->upper = cool_spec->upper; 850 dev->upper_no_limit = upper_no_limit; 851 dev->lower = cool_spec->lower; 852 dev->target = THERMAL_NO_TARGET; 853 dev->weight = cool_spec->weight; 854 855 result = ida_alloc(&tz->ida, GFP_KERNEL); 856 if (result < 0) 857 goto free_mem; 858 859 dev->id = result; 860 snprintf(dev->name, sizeof(dev->name), "cdev%d", dev->id); 861 result = 862 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); 863 if (result) 864 goto release_ida; 865 866 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point", 867 dev->id); 868 sysfs_attr_init(&dev->attr.attr); 869 dev->attr.attr.name = dev->attr_name; 870 dev->attr.attr.mode = 0444; 871 dev->attr.show = trip_point_show; 872 result = device_create_file(&tz->device, &dev->attr); 873 if (result) 874 goto remove_symbol_link; 875 876 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name), 877 "cdev%d_weight", dev->id); 878 sysfs_attr_init(&dev->weight_attr.attr); 879 dev->weight_attr.attr.name = dev->weight_attr_name; 880 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO; 881 dev->weight_attr.show = weight_show; 882 dev->weight_attr.store = weight_store; 883 result = device_create_file(&tz->device, &dev->weight_attr); 884 if (result) 885 goto remove_trip_file; 886 887 result = thermal_instance_add(dev, cdev, td); 888 if (result) 889 goto remove_weight_file; 890 891 thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV); 892 893 return 0; 894 895 remove_weight_file: 896 device_remove_file(&tz->device, &dev->weight_attr); 897 remove_trip_file: 898 device_remove_file(&tz->device, &dev->attr); 899 remove_symbol_link: 900 sysfs_remove_link(&tz->device.kobj, dev->name); 901 release_ida: 902 ida_free(&tz->ida, dev->id); 903 free_mem: 904 kfree(dev); 905 return result; 906 } 907 908 static void thermal_instance_delete(struct thermal_instance *instance) 909 { 910 list_del(&instance->trip_node); 911 912 guard(cooling_dev)(instance->cdev); 913 914 list_del(&instance->cdev_node); 915 } 916 917 /** 918 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone. 919 * @tz: pointer to a struct thermal_zone_device. 920 * @td: descriptor of the trip point to unbind @cdev from 921 * @cdev: pointer to a struct thermal_cooling_device. 922 * 923 * This interface function unbind a thermal cooling device from the certain 924 * trip point of a thermal zone device. 925 * This function is usually called in the thermal zone device .unbind callback. 926 */ 927 static void thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz, 928 struct thermal_trip_desc *td, 929 struct thermal_cooling_device *cdev) 930 { 931 struct thermal_instance *pos, *next; 932 933 list_for_each_entry_safe(pos, next, &td->thermal_instances, trip_node) { 934 if (pos->cdev == cdev) { 935 thermal_instance_delete(pos); 936 goto unbind; 937 } 938 } 939 940 return; 941 942 unbind: 943 thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV); 944 945 device_remove_file(&tz->device, &pos->weight_attr); 946 device_remove_file(&tz->device, &pos->attr); 947 sysfs_remove_link(&tz->device.kobj, pos->name); 948 ida_free(&tz->ida, pos->id); 949 kfree(pos); 950 } 951 952 static void thermal_release(struct device *dev) 953 { 954 struct thermal_zone_device *tz; 955 struct thermal_cooling_device *cdev; 956 957 if (!strncmp(dev_name(dev), "thermal_zone", 958 sizeof("thermal_zone") - 1)) { 959 tz = to_thermal_zone(dev); 960 thermal_zone_destroy_device_groups(tz); 961 thermal_set_governor(tz, NULL); 962 ida_destroy(&tz->ida); 963 mutex_destroy(&tz->lock); 964 complete(&tz->removal); 965 } else if (!strncmp(dev_name(dev), "cooling_device", 966 sizeof("cooling_device") - 1)) { 967 cdev = to_cooling_device(dev); 968 thermal_cooling_device_destroy_sysfs(cdev); 969 kfree_const(cdev->type); 970 ida_free(&thermal_cdev_ida, cdev->id); 971 kfree(cdev); 972 } 973 } 974 975 static const struct class thermal_class = { 976 .name = "thermal", 977 .dev_release = thermal_release, 978 }; 979 static bool thermal_class_unavailable __ro_after_init = true; 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 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_unavailable) 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 1084 cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL); 1085 if (!cdev->type) { 1086 ret = -ENOMEM; 1087 goto out_ida_remove; 1088 } 1089 1090 mutex_init(&cdev->lock); 1091 INIT_LIST_HEAD(&cdev->thermal_instances); 1092 cdev->np = np; 1093 cdev->ops = ops; 1094 cdev->updated = false; 1095 cdev->device.class = &thermal_class; 1096 cdev->devdata = devdata; 1097 1098 ret = cdev->ops->get_max_state(cdev, &cdev->max_state); 1099 if (ret) 1100 goto out_cdev_type; 1101 1102 /* 1103 * The cooling device's current state is only needed for debug 1104 * initialization below, so a failure to get it does not cause 1105 * the entire cooling device initialization to fail. However, 1106 * the debug will not work for the device if its initial state 1107 * cannot be determined and drivers are responsible for ensuring 1108 * that this will not happen. 1109 */ 1110 ret = cdev->ops->get_cur_state(cdev, ¤t_state); 1111 if (ret) 1112 current_state = ULONG_MAX; 1113 1114 thermal_cooling_device_setup_sysfs(cdev); 1115 1116 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id); 1117 if (ret) 1118 goto out_cooling_dev; 1119 1120 ret = device_register(&cdev->device); 1121 if (ret) { 1122 /* thermal_release() handles rest of the cleanup */ 1123 put_device(&cdev->device); 1124 return ERR_PTR(ret); 1125 } 1126 1127 if (current_state <= cdev->max_state) 1128 thermal_debug_cdev_add(cdev, current_state); 1129 1130 thermal_cooling_device_init_complete(cdev); 1131 1132 return cdev; 1133 1134 out_cooling_dev: 1135 thermal_cooling_device_destroy_sysfs(cdev); 1136 out_cdev_type: 1137 kfree_const(cdev->type); 1138 out_ida_remove: 1139 ida_free(&thermal_cdev_ida, cdev->id); 1140 out_kfree_cdev: 1141 kfree(cdev); 1142 return ERR_PTR(ret); 1143 } 1144 1145 /** 1146 * thermal_cooling_device_register() - register a new thermal cooling device 1147 * @type: the thermal cooling device type. 1148 * @devdata: device private data. 1149 * @ops: standard thermal cooling devices callbacks. 1150 * 1151 * This interface function adds a new thermal cooling device (fan/processor/...) 1152 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1153 * to all the thermal zone devices registered at the same time. 1154 * 1155 * Return: a pointer to the created struct thermal_cooling_device or an 1156 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1157 */ 1158 struct thermal_cooling_device * 1159 thermal_cooling_device_register(const char *type, void *devdata, 1160 const struct thermal_cooling_device_ops *ops) 1161 { 1162 return __thermal_cooling_device_register(NULL, type, devdata, ops); 1163 } 1164 EXPORT_SYMBOL_GPL(thermal_cooling_device_register); 1165 1166 /** 1167 * thermal_of_cooling_device_register() - register an OF thermal cooling device 1168 * @np: a pointer to a device tree node. 1169 * @type: the thermal cooling device type. 1170 * @devdata: device private data. 1171 * @ops: standard thermal cooling devices callbacks. 1172 * 1173 * This function will register a cooling device with device tree node reference. 1174 * This interface function adds a new thermal cooling device (fan/processor/...) 1175 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1176 * to all the thermal zone devices registered at the same time. 1177 * 1178 * Return: a pointer to the created struct thermal_cooling_device or an 1179 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1180 */ 1181 struct thermal_cooling_device * 1182 thermal_of_cooling_device_register(struct device_node *np, 1183 const char *type, void *devdata, 1184 const struct thermal_cooling_device_ops *ops) 1185 { 1186 return __thermal_cooling_device_register(np, type, devdata, ops); 1187 } 1188 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register); 1189 1190 static void thermal_cooling_device_release(struct device *dev, void *res) 1191 { 1192 thermal_cooling_device_unregister( 1193 *(struct thermal_cooling_device **)res); 1194 } 1195 1196 /** 1197 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling 1198 * device 1199 * @dev: a valid struct device pointer of a sensor device. 1200 * @np: a pointer to a device tree node. 1201 * @type: the thermal cooling device type. 1202 * @devdata: device private data. 1203 * @ops: standard thermal cooling devices callbacks. 1204 * 1205 * This function will register a cooling device with device tree node reference. 1206 * This interface function adds a new thermal cooling device (fan/processor/...) 1207 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself 1208 * to all the thermal zone devices registered at the same time. 1209 * 1210 * Return: a pointer to the created struct thermal_cooling_device or an 1211 * ERR_PTR. Caller must check return value with IS_ERR*() helpers. 1212 */ 1213 struct thermal_cooling_device * 1214 devm_thermal_of_cooling_device_register(struct device *dev, 1215 struct device_node *np, 1216 const char *type, void *devdata, 1217 const struct thermal_cooling_device_ops *ops) 1218 { 1219 struct thermal_cooling_device **ptr, *tcd; 1220 1221 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr), 1222 GFP_KERNEL); 1223 if (!ptr) 1224 return ERR_PTR(-ENOMEM); 1225 1226 tcd = __thermal_cooling_device_register(np, type, devdata, ops); 1227 if (IS_ERR(tcd)) { 1228 devres_free(ptr); 1229 return tcd; 1230 } 1231 1232 *ptr = tcd; 1233 devres_add(dev, ptr); 1234 1235 return tcd; 1236 } 1237 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register); 1238 1239 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev) 1240 { 1241 struct thermal_cooling_device *pos = NULL; 1242 1243 list_for_each_entry(pos, &thermal_cdev_list, node) { 1244 if (pos == cdev) 1245 return true; 1246 } 1247 1248 return false; 1249 } 1250 1251 /** 1252 * thermal_cooling_device_update - Update a cooling device object 1253 * @cdev: Target cooling device. 1254 * 1255 * Update @cdev to reflect a change of the underlying hardware or platform. 1256 * 1257 * Must be called when the maximum cooling state of @cdev becomes invalid and so 1258 * its .get_max_state() callback needs to be run to produce the new maximum 1259 * cooling state value. 1260 */ 1261 void thermal_cooling_device_update(struct thermal_cooling_device *cdev) 1262 { 1263 struct thermal_instance *ti; 1264 unsigned long state; 1265 1266 if (IS_ERR_OR_NULL(cdev)) 1267 return; 1268 1269 /* 1270 * Hold thermal_list_lock throughout the update to prevent the device 1271 * from going away while being updated. 1272 */ 1273 guard(mutex)(&thermal_list_lock); 1274 1275 if (!thermal_cooling_device_present(cdev)) 1276 return; 1277 1278 /* 1279 * Update under the cdev lock to prevent the state from being set beyond 1280 * the new limit concurrently. 1281 */ 1282 guard(cooling_dev)(cdev); 1283 1284 if (cdev->ops->get_max_state(cdev, &cdev->max_state)) 1285 return; 1286 1287 thermal_cooling_device_stats_reinit(cdev); 1288 1289 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) { 1290 if (ti->upper == cdev->max_state) 1291 continue; 1292 1293 if (ti->upper < cdev->max_state) { 1294 if (ti->upper_no_limit) 1295 ti->upper = cdev->max_state; 1296 1297 continue; 1298 } 1299 1300 ti->upper = cdev->max_state; 1301 if (ti->lower > ti->upper) 1302 ti->lower = ti->upper; 1303 1304 if (ti->target == THERMAL_NO_TARGET) 1305 continue; 1306 1307 if (ti->target > ti->upper) 1308 ti->target = ti->upper; 1309 } 1310 1311 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state) 1312 return; 1313 1314 thermal_cooling_device_stats_update(cdev, state); 1315 } 1316 EXPORT_SYMBOL_GPL(thermal_cooling_device_update); 1317 1318 static void __thermal_zone_cdev_unbind(struct thermal_zone_device *tz, 1319 struct thermal_cooling_device *cdev) 1320 { 1321 struct thermal_trip_desc *td; 1322 1323 for_each_trip_desc(tz, td) 1324 thermal_unbind_cdev_from_trip(tz, td, cdev); 1325 } 1326 1327 static void thermal_zone_cdev_unbind(struct thermal_zone_device *tz, 1328 struct thermal_cooling_device *cdev) 1329 { 1330 guard(thermal_zone)(tz); 1331 1332 __thermal_zone_cdev_unbind(tz, cdev); 1333 } 1334 1335 static bool thermal_cooling_device_exit(struct thermal_cooling_device *cdev) 1336 { 1337 struct thermal_zone_device *tz; 1338 1339 guard(mutex)(&thermal_list_lock); 1340 1341 if (!thermal_cooling_device_present(cdev)) 1342 return false; 1343 1344 list_del(&cdev->node); 1345 1346 list_for_each_entry(tz, &thermal_tz_list, node) 1347 thermal_zone_cdev_unbind(tz, cdev); 1348 1349 return true; 1350 } 1351 1352 /** 1353 * thermal_cooling_device_unregister() - removes a thermal cooling device 1354 * @cdev: Thermal cooling device to remove. 1355 */ 1356 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) 1357 { 1358 if (!cdev) 1359 return; 1360 1361 thermal_debug_cdev_remove(cdev); 1362 1363 if (thermal_cooling_device_exit(cdev)) 1364 device_unregister(&cdev->device); 1365 } 1366 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); 1367 1368 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) 1369 { 1370 const struct thermal_trip_desc *td; 1371 int ret = -EINVAL; 1372 1373 if (tz->ops.get_crit_temp) 1374 return tz->ops.get_crit_temp(tz, temp); 1375 1376 guard(thermal_zone)(tz); 1377 1378 for_each_trip_desc(tz, td) { 1379 const struct thermal_trip *trip = &td->trip; 1380 1381 if (trip->type == THERMAL_TRIP_CRITICAL) { 1382 *temp = trip->temperature; 1383 ret = 0; 1384 break; 1385 } 1386 } 1387 1388 return ret; 1389 } 1390 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp); 1391 1392 static void thermal_zone_device_check(struct work_struct *work) 1393 { 1394 struct thermal_zone_device *tz = container_of(work, struct 1395 thermal_zone_device, 1396 poll_queue.work); 1397 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 1398 } 1399 1400 static void thermal_zone_device_init(struct thermal_zone_device *tz) 1401 { 1402 struct thermal_trip_desc *td, *next; 1403 1404 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check); 1405 1406 tz->temperature = THERMAL_TEMP_INIT; 1407 tz->passive = 0; 1408 tz->prev_low_trip = -INT_MAX; 1409 tz->prev_high_trip = INT_MAX; 1410 for_each_trip_desc(tz, td) { 1411 struct thermal_instance *instance; 1412 1413 list_for_each_entry(instance, &td->thermal_instances, trip_node) 1414 instance->initialized = false; 1415 } 1416 /* 1417 * At this point, all valid trips need to be moved to trips_high so that 1418 * mitigation can be started if the zone temperature is above them. 1419 */ 1420 list_for_each_entry_safe(td, next, &tz->trips_invalid, list_node) { 1421 if (td->trip.temperature != THERMAL_TEMP_INVALID) 1422 move_to_trips_high(tz, td); 1423 } 1424 /* The trips_reached list may not be empty during system resume. */ 1425 list_for_each_entry_safe(td, next, &tz->trips_reached, list_node) { 1426 if (td->trip.temperature == THERMAL_TEMP_INVALID) 1427 move_to_trips_invalid(tz, td); 1428 else 1429 move_to_trips_high(tz, td); 1430 } 1431 } 1432 1433 static int thermal_zone_init_governor(struct thermal_zone_device *tz) 1434 { 1435 struct thermal_governor *governor; 1436 1437 guard(mutex)(&thermal_governor_lock); 1438 1439 if (tz->tzp) 1440 governor = __find_governor(tz->tzp->governor_name); 1441 else 1442 governor = def_governor; 1443 1444 return thermal_set_governor(tz, governor); 1445 } 1446 1447 static void thermal_zone_init_complete(struct thermal_zone_device *tz) 1448 { 1449 struct thermal_cooling_device *cdev; 1450 1451 guard(mutex)(&thermal_list_lock); 1452 1453 list_add_tail(&tz->node, &thermal_tz_list); 1454 1455 guard(thermal_zone)(tz); 1456 1457 /* Bind cooling devices for this zone. */ 1458 list_for_each_entry(cdev, &thermal_cdev_list, node) 1459 __thermal_zone_cdev_bind(tz, cdev); 1460 1461 tz->state &= ~TZ_STATE_FLAG_INIT; 1462 /* 1463 * If system suspend or resume is in progress at this point, the 1464 * new thermal zone needs to be marked as suspended because 1465 * thermal_pm_notify() has run already. 1466 */ 1467 if (thermal_pm_suspended) 1468 tz->state |= TZ_STATE_FLAG_SUSPENDED; 1469 1470 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 1471 } 1472 1473 /** 1474 * thermal_zone_device_register_with_trips() - register a new thermal zone device 1475 * @type: the thermal zone device type 1476 * @trips: a pointer to an array of thermal trips 1477 * @num_trips: the number of trip points the thermal zone support 1478 * @devdata: private device data 1479 * @ops: standard thermal zone device callbacks 1480 * @tzp: thermal zone platform parameters 1481 * @passive_delay: number of milliseconds to wait between polls when 1482 * performing passive cooling 1483 * @polling_delay: number of milliseconds to wait between polls when checking 1484 * whether trip points have been crossed (0 for interrupt 1485 * driven systems) 1486 * 1487 * This interface function adds a new thermal zone device (sensor) to 1488 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the 1489 * thermal cooling devices registered at the same time. 1490 * thermal_zone_device_unregister() must be called when the device is no 1491 * longer needed. The passive cooling depends on the .get_trend() return value. 1492 * 1493 * Return: a pointer to the created struct thermal_zone_device or an 1494 * in case of error, an ERR_PTR. Caller must check return value with 1495 * IS_ERR*() helpers. 1496 */ 1497 struct thermal_zone_device * 1498 thermal_zone_device_register_with_trips(const char *type, 1499 const struct thermal_trip *trips, 1500 int num_trips, void *devdata, 1501 const struct thermal_zone_device_ops *ops, 1502 const struct thermal_zone_params *tzp, 1503 unsigned int passive_delay, 1504 unsigned int polling_delay) 1505 { 1506 const struct thermal_trip *trip = trips; 1507 struct thermal_zone_device *tz; 1508 struct thermal_trip_desc *td; 1509 size_t type_len = 0; 1510 int id; 1511 int result; 1512 1513 if (type) 1514 type_len = strnlen(type, THERMAL_NAME_LENGTH); 1515 1516 if (type_len == 0) { 1517 pr_err("No thermal zone type defined\n"); 1518 return ERR_PTR(-EINVAL); 1519 } 1520 1521 if (type_len == THERMAL_NAME_LENGTH) { 1522 pr_err("Thermal zone name (%s) too long, should be under %d chars\n", 1523 type, THERMAL_NAME_LENGTH); 1524 return ERR_PTR(-EINVAL); 1525 } 1526 1527 if (num_trips < 0) { 1528 pr_err("Incorrect number of thermal trips\n"); 1529 return ERR_PTR(-EINVAL); 1530 } 1531 1532 if (!ops || !ops->get_temp) { 1533 pr_err("Thermal zone device ops not defined or invalid\n"); 1534 return ERR_PTR(-EINVAL); 1535 } 1536 1537 if (num_trips > 0 && !trips) 1538 return ERR_PTR(-EINVAL); 1539 1540 if (polling_delay && passive_delay > polling_delay) 1541 return ERR_PTR(-EINVAL); 1542 1543 if (thermal_class_unavailable) 1544 return ERR_PTR(-ENODEV); 1545 1546 tz = kzalloc_flex(*tz, trips, num_trips); 1547 if (!tz) 1548 return ERR_PTR(-ENOMEM); 1549 1550 if (tzp) { 1551 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL); 1552 if (!tz->tzp) { 1553 result = -ENOMEM; 1554 goto free_tz; 1555 } 1556 } 1557 1558 INIT_LIST_HEAD(&tz->node); 1559 INIT_LIST_HEAD(&tz->trips_high); 1560 INIT_LIST_HEAD(&tz->trips_reached); 1561 INIT_LIST_HEAD(&tz->trips_invalid); 1562 ida_init(&tz->ida); 1563 mutex_init(&tz->lock); 1564 init_completion(&tz->removal); 1565 init_completion(&tz->resume); 1566 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL); 1567 if (id < 0) { 1568 result = id; 1569 goto free_tzp; 1570 } 1571 1572 tz->id = id; 1573 strscpy(tz->type, type, sizeof(tz->type)); 1574 1575 tz->ops = *ops; 1576 if (!tz->ops.critical) 1577 tz->ops.critical = thermal_zone_device_critical; 1578 1579 tz->device.class = &thermal_class; 1580 tz->devdata = devdata; 1581 tz->num_trips = num_trips; 1582 for_each_trip_desc(tz, td) { 1583 td->trip = *trip++; 1584 INIT_LIST_HEAD(&td->thermal_instances); 1585 INIT_LIST_HEAD(&td->list_node); 1586 /* 1587 * Mark all thresholds as invalid to start with even though 1588 * this only matters for the trips that start as invalid and 1589 * become valid later. 1590 */ 1591 move_to_trips_invalid(tz, td); 1592 } 1593 1594 tz->polling_delay_jiffies = msecs_to_jiffies(polling_delay); 1595 tz->passive_delay_jiffies = msecs_to_jiffies(passive_delay); 1596 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY; 1597 1598 tz->state = TZ_STATE_FLAG_INIT; 1599 1600 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id); 1601 if (result) 1602 goto remove_id; 1603 1604 thermal_zone_device_init(tz); 1605 1606 result = thermal_zone_init_governor(tz); 1607 if (result) 1608 goto remove_id; 1609 1610 /* sys I/F */ 1611 /* Add nodes that are always present via .groups */ 1612 result = thermal_zone_create_device_groups(tz); 1613 if (result) { 1614 thermal_set_governor(tz, NULL); 1615 goto remove_id; 1616 } 1617 1618 result = device_register(&tz->device); 1619 if (result) 1620 goto release_device; 1621 1622 if (!tz->tzp || !tz->tzp->no_hwmon) { 1623 result = thermal_add_hwmon_sysfs(tz); 1624 if (result) 1625 goto unregister; 1626 } 1627 1628 result = thermal_thresholds_init(tz); 1629 if (result) 1630 goto remove_hwmon; 1631 1632 thermal_zone_init_complete(tz); 1633 1634 thermal_notify_tz_create(tz); 1635 1636 thermal_debug_tz_add(tz); 1637 1638 return tz; 1639 1640 remove_hwmon: 1641 thermal_remove_hwmon_sysfs(tz); 1642 unregister: 1643 device_del(&tz->device); 1644 release_device: 1645 put_device(&tz->device); 1646 wait_for_completion(&tz->removal); 1647 remove_id: 1648 ida_free(&thermal_tz_ida, id); 1649 free_tzp: 1650 kfree(tz->tzp); 1651 free_tz: 1652 kfree(tz); 1653 return ERR_PTR(result); 1654 } 1655 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips); 1656 1657 struct thermal_zone_device *thermal_tripless_zone_device_register( 1658 const char *type, 1659 void *devdata, 1660 const struct thermal_zone_device_ops *ops, 1661 const struct thermal_zone_params *tzp) 1662 { 1663 return thermal_zone_device_register_with_trips(type, NULL, 0, devdata, 1664 ops, tzp, 0, 0); 1665 } 1666 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register); 1667 1668 void *thermal_zone_device_priv(struct thermal_zone_device *tzd) 1669 { 1670 return tzd->devdata; 1671 } 1672 EXPORT_SYMBOL_GPL(thermal_zone_device_priv); 1673 1674 const char *thermal_zone_device_type(struct thermal_zone_device *tzd) 1675 { 1676 return tzd->type; 1677 } 1678 EXPORT_SYMBOL_GPL(thermal_zone_device_type); 1679 1680 int thermal_zone_device_id(struct thermal_zone_device *tzd) 1681 { 1682 return tzd->id; 1683 } 1684 EXPORT_SYMBOL_GPL(thermal_zone_device_id); 1685 1686 struct device *thermal_zone_device(struct thermal_zone_device *tzd) 1687 { 1688 return &tzd->device; 1689 } 1690 EXPORT_SYMBOL_GPL(thermal_zone_device); 1691 1692 static bool thermal_zone_exit(struct thermal_zone_device *tz) 1693 { 1694 struct thermal_cooling_device *cdev; 1695 1696 guard(mutex)(&thermal_list_lock); 1697 1698 if (list_empty(&tz->node)) 1699 return false; 1700 1701 guard(thermal_zone)(tz); 1702 1703 tz->state |= TZ_STATE_FLAG_EXIT; 1704 list_del_init(&tz->node); 1705 1706 /* Unbind all cdevs associated with this thermal zone. */ 1707 list_for_each_entry(cdev, &thermal_cdev_list, node) 1708 __thermal_zone_cdev_unbind(tz, cdev); 1709 1710 return true; 1711 } 1712 1713 /** 1714 * thermal_zone_device_unregister - removes the registered thermal zone device 1715 * @tz: the thermal zone device to remove 1716 */ 1717 void thermal_zone_device_unregister(struct thermal_zone_device *tz) 1718 { 1719 if (!tz) 1720 return; 1721 1722 thermal_debug_tz_remove(tz); 1723 1724 if (!thermal_zone_exit(tz)) 1725 return; 1726 1727 cancel_delayed_work_sync(&tz->poll_queue); 1728 1729 thermal_thresholds_exit(tz); 1730 thermal_remove_hwmon_sysfs(tz); 1731 1732 device_del(&tz->device); 1733 put_device(&tz->device); 1734 1735 thermal_notify_tz_delete(tz); 1736 1737 wait_for_completion(&tz->removal); 1738 1739 ida_free(&thermal_tz_ida, tz->id); 1740 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_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 void thermal_pm_prepare(void) 1839 { 1840 if (thermal_class_unavailable) 1841 return; 1842 1843 __thermal_pm_prepare(); 1844 /* 1845 * Allow any leftover thermal work items already on the worqueue to 1846 * complete so they don't get in the way later. 1847 */ 1848 flush_workqueue(thermal_wq); 1849 } 1850 1851 static void thermal_zone_pm_complete(struct thermal_zone_device *tz) 1852 { 1853 guard(thermal_zone)(tz); 1854 1855 reinit_completion(&tz->resume); 1856 tz->state |= TZ_STATE_FLAG_RESUMING; 1857 1858 /* 1859 * Replace the work function with the resume one, which will restore the 1860 * original work function and schedule the polling work if needed. 1861 */ 1862 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_resume); 1863 /* Queue up the work without a delay. */ 1864 mod_delayed_work(thermal_wq, &tz->poll_queue, 0); 1865 } 1866 1867 void thermal_pm_complete(void) 1868 { 1869 struct thermal_zone_device *tz; 1870 1871 if (thermal_class_unavailable) 1872 return; 1873 1874 guard(mutex)(&thermal_list_lock); 1875 1876 thermal_pm_suspended = false; 1877 1878 list_for_each_entry(tz, &thermal_tz_list, node) 1879 thermal_zone_pm_complete(tz); 1880 } 1881 1882 static int __init thermal_init(void) 1883 { 1884 int result; 1885 1886 thermal_debug_init(); 1887 1888 result = thermal_netlink_init(); 1889 if (result) 1890 goto error; 1891 1892 thermal_wq = alloc_workqueue("thermal_events", WQ_POWER_EFFICIENT, 0); 1893 if (!thermal_wq) { 1894 result = -ENOMEM; 1895 goto unregister_netlink; 1896 } 1897 1898 result = thermal_register_governors(); 1899 if (result) 1900 goto destroy_workqueue; 1901 1902 result = class_register(&thermal_class); 1903 if (result) 1904 goto unregister_governors; 1905 1906 thermal_class_unavailable = false; 1907 1908 return 0; 1909 1910 unregister_governors: 1911 thermal_unregister_governors(); 1912 destroy_workqueue: 1913 destroy_workqueue(thermal_wq); 1914 unregister_netlink: 1915 thermal_netlink_exit(); 1916 error: 1917 mutex_destroy(&thermal_list_lock); 1918 mutex_destroy(&thermal_governor_lock); 1919 return result; 1920 } 1921 postcore_initcall(thermal_init); 1922