| 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 <[email protected]> |
| 7 |
* Copyright (C) 2008 Sujith Thomas <[email protected]> |
| 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 |
thermal_set_governor(tz, NULL); |
| 968 |
ida_destroy(&tz->ida); |
| 969 |
mutex_destroy(&tz->lock); |
| 970 |
complete(&tz->removal); |
| 971 |
} else if (!strncmp(dev_name(dev), "cooling_device", |
| 972 |
sizeof("cooling_device") - 1)) { |
| 973 |
cdev = to_cooling_device(dev); |
| 974 |
thermal_cooling_device_destroy_sysfs(cdev); |
| 975 |
kfree_const(cdev->type); |
| 976 |
ida_free(&thermal_cdev_ida, cdev->id); |
| 977 |
kfree(cdev); |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
static struct class *thermal_class; |
| 982 |
|
| 983 |
static inline |
| 984 |
void print_bind_err_msg(struct thermal_zone_device *tz, |
| 985 |
const struct thermal_trip_desc *td, |
| 986 |
struct thermal_cooling_device *cdev, int ret) |
| 987 |
{ |
| 988 |
dev_err(&tz->device, "binding cdev %s to trip %d failed: %d\n", |
| 989 |
cdev->type, thermal_zone_trip_id(tz, &td->trip), ret); |
| 990 |
} |
| 991 |
|
| 992 |
static bool __thermal_zone_cdev_bind(struct thermal_zone_device *tz, |
| 993 |
struct thermal_cooling_device *cdev) |
| 994 |
{ |
| 995 |
struct thermal_trip_desc *td; |
| 996 |
bool update_tz = false; |
| 997 |
|
| 998 |
if (!tz->ops.should_bind) |
| 999 |
return false; |
| 1000 |
|
| 1001 |
for_each_trip_desc(tz, td) { |
| 1002 |
struct cooling_spec c = { |
| 1003 |
.upper = THERMAL_NO_LIMIT, |
| 1004 |
.lower = THERMAL_NO_LIMIT, |
| 1005 |
.weight = THERMAL_WEIGHT_DEFAULT |
| 1006 |
}; |
| 1007 |
int ret; |
| 1008 |
|
| 1009 |
if (!tz->ops.should_bind(tz, &td->trip, cdev, &c)) |
| 1010 |
continue; |
| 1011 |
|
| 1012 |
ret = thermal_bind_cdev_to_trip(tz, td, cdev, &c); |
| 1013 |
if (ret) { |
| 1014 |
print_bind_err_msg(tz, td, cdev, ret); |
| 1015 |
continue; |
| 1016 |
} |
| 1017 |
|
| 1018 |
update_tz = true; |
| 1019 |
} |
| 1020 |
|
| 1021 |
return update_tz; |
| 1022 |
} |
| 1023 |
|
| 1024 |
static void thermal_zone_cdev_bind(struct thermal_zone_device *tz, |
| 1025 |
struct thermal_cooling_device *cdev) |
| 1026 |
{ |
| 1027 |
guard(thermal_zone)(tz); |
| 1028 |
|
| 1029 |
if (__thermal_zone_cdev_bind(tz, cdev)) |
| 1030 |
__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
| 1031 |
} |
| 1032 |
|
| 1033 |
static void thermal_cooling_device_init_complete(struct thermal_cooling_device *cdev) |
| 1034 |
{ |
| 1035 |
struct thermal_zone_device *tz; |
| 1036 |
|
| 1037 |
guard(mutex)(&thermal_list_lock); |
| 1038 |
|
| 1039 |
list_add(&cdev->node, &thermal_cdev_list); |
| 1040 |
|
| 1041 |
list_for_each_entry(tz, &thermal_tz_list, node) |
| 1042 |
thermal_zone_cdev_bind(tz, cdev); |
| 1043 |
} |
| 1044 |
|
| 1045 |
/** |
| 1046 |
* __thermal_cooling_device_register() - register a new thermal cooling device |
| 1047 |
* @np: a pointer to a device tree node. |
| 1048 |
* @type: the thermal cooling device type. |
| 1049 |
* @devdata: device private data. |
| 1050 |
* @ops: standard thermal cooling devices callbacks. |
| 1051 |
* |
| 1052 |
* This interface function adds a new thermal cooling device (fan/processor/...) |
| 1053 |
* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 1054 |
* to all the thermal zone devices registered at the same time. |
| 1055 |
* It also gives the opportunity to link the cooling device to a device tree |
| 1056 |
* node, so that it can be bound to a thermal zone created out of device tree. |
| 1057 |
* |
| 1058 |
* Return: a pointer to the created struct thermal_cooling_device or an |
| 1059 |
* ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
| 1060 |
*/ |
| 1061 |
static struct thermal_cooling_device * |
| 1062 |
__thermal_cooling_device_register(struct device_node *np, |
| 1063 |
const char *type, void *devdata, |
| 1064 |
const struct thermal_cooling_device_ops *ops) |
| 1065 |
{ |
| 1066 |
struct thermal_cooling_device *cdev; |
| 1067 |
unsigned long current_state; |
| 1068 |
int id, ret; |
| 1069 |
|
| 1070 |
if (!ops || !ops->get_max_state || !ops->get_cur_state || |
| 1071 |
!ops->set_cur_state) |
| 1072 |
return ERR_PTR(-EINVAL); |
| 1073 |
|
| 1074 |
if (!thermal_class) |
| 1075 |
return ERR_PTR(-ENODEV); |
| 1076 |
|
| 1077 |
cdev = kzalloc_obj(*cdev); |
| 1078 |
if (!cdev) |
| 1079 |
return ERR_PTR(-ENOMEM); |
| 1080 |
|
| 1081 |
ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL); |
| 1082 |
if (ret < 0) |
| 1083 |
goto out_kfree_cdev; |
| 1084 |
cdev->id = ret; |
| 1085 |
id = ret; |
| 1086 |
|
| 1087 |
cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL); |
| 1088 |
if (!cdev->type) { |
| 1089 |
ret = -ENOMEM; |
| 1090 |
goto out_ida_remove; |
| 1091 |
} |
| 1092 |
|
| 1093 |
mutex_init(&cdev->lock); |
| 1094 |
INIT_LIST_HEAD(&cdev->thermal_instances); |
| 1095 |
cdev->np = np; |
| 1096 |
cdev->ops = ops; |
| 1097 |
cdev->updated = false; |
| 1098 |
cdev->device.class = thermal_class; |
| 1099 |
cdev->devdata = devdata; |
| 1100 |
|
| 1101 |
ret = cdev->ops->get_max_state(cdev, &cdev->max_state); |
| 1102 |
if (ret) |
| 1103 |
goto out_cdev_type; |
| 1104 |
|
| 1105 |
/* |
| 1106 |
* The cooling device's current state is only needed for debug |
| 1107 |
* initialization below, so a failure to get it does not cause |
| 1108 |
* the entire cooling device initialization to fail. However, |
| 1109 |
* the debug will not work for the device if its initial state |
| 1110 |
* cannot be determined and drivers are responsible for ensuring |
| 1111 |
* that this will not happen. |
| 1112 |
*/ |
| 1113 |
ret = cdev->ops->get_cur_state(cdev, ¤t_state); |
| 1114 |
if (ret) |
| 1115 |
current_state = ULONG_MAX; |
| 1116 |
|
| 1117 |
thermal_cooling_device_setup_sysfs(cdev); |
| 1118 |
|
| 1119 |
ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
| 1120 |
if (ret) |
| 1121 |
goto out_cooling_dev; |
| 1122 |
|
| 1123 |
ret = device_register(&cdev->device); |
| 1124 |
if (ret) { |
| 1125 |
/* thermal_release() handles rest of the cleanup */ |
| 1126 |
put_device(&cdev->device); |
| 1127 |
return ERR_PTR(ret); |
| 1128 |
} |
| 1129 |
|
| 1130 |
if (current_state <= cdev->max_state) |
| 1131 |
thermal_debug_cdev_add(cdev, current_state); |
| 1132 |
|
| 1133 |
thermal_cooling_device_init_complete(cdev); |
| 1134 |
|
| 1135 |
return cdev; |
| 1136 |
|
| 1137 |
out_cooling_dev: |
| 1138 |
thermal_cooling_device_destroy_sysfs(cdev); |
| 1139 |
out_cdev_type: |
| 1140 |
kfree_const(cdev->type); |
| 1141 |
out_ida_remove: |
| 1142 |
ida_free(&thermal_cdev_ida, id); |
| 1143 |
out_kfree_cdev: |
| 1144 |
kfree(cdev); |
| 1145 |
return ERR_PTR(ret); |
| 1146 |
} |
| 1147 |
|
| 1148 |
/** |
| 1149 |
* thermal_cooling_device_register() - register a new thermal cooling device |
| 1150 |
* @type: the thermal cooling device type. |
| 1151 |
* @devdata: device private data. |
| 1152 |
* @ops: standard thermal cooling devices callbacks. |
| 1153 |
* |
| 1154 |
* This interface function adds a new thermal cooling device (fan/processor/...) |
| 1155 |
* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 1156 |
* to all the thermal zone devices registered at the same time. |
| 1157 |
* |
| 1158 |
* Return: a pointer to the created struct thermal_cooling_device or an |
| 1159 |
* ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
| 1160 |
*/ |
| 1161 |
struct thermal_cooling_device * |
| 1162 |
thermal_cooling_device_register(const char *type, void *devdata, |
| 1163 |
const struct thermal_cooling_device_ops *ops) |
| 1164 |
{ |
| 1165 |
return __thermal_cooling_device_register(NULL, type, devdata, ops); |
| 1166 |
} |
| 1167 |
EXPORT_SYMBOL_GPL(thermal_cooling_device_register); |
| 1168 |
|
| 1169 |
/** |
| 1170 |
* thermal_of_cooling_device_register() - register an OF thermal cooling device |
| 1171 |
* @np: a pointer to a device tree node. |
| 1172 |
* @type: the thermal cooling device type. |
| 1173 |
* @devdata: device private data. |
| 1174 |
* @ops: standard thermal cooling devices callbacks. |
| 1175 |
* |
| 1176 |
* This function will register a cooling device with device tree node reference. |
| 1177 |
* This interface function adds a new thermal cooling device (fan/processor/...) |
| 1178 |
* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 1179 |
* to all the thermal zone devices registered at the same time. |
| 1180 |
* |
| 1181 |
* Return: a pointer to the created struct thermal_cooling_device or an |
| 1182 |
* ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
| 1183 |
*/ |
| 1184 |
struct thermal_cooling_device * |
| 1185 |
thermal_of_cooling_device_register(struct device_node *np, |
| 1186 |
const char *type, void *devdata, |
| 1187 |
const struct thermal_cooling_device_ops *ops) |
| 1188 |
{ |
| 1189 |
return __thermal_cooling_device_register(np, type, devdata, ops); |
| 1190 |
} |
| 1191 |
EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register); |
| 1192 |
|
| 1193 |
static void thermal_cooling_device_release(struct device *dev, void *res) |
| 1194 |
{ |
| 1195 |
thermal_cooling_device_unregister( |
| 1196 |
*(struct thermal_cooling_device **)res); |
| 1197 |
} |
| 1198 |
|
| 1199 |
/** |
| 1200 |
* devm_thermal_of_cooling_device_register() - register an OF thermal cooling |
| 1201 |
* device |
| 1202 |
* @dev: a valid struct device pointer of a sensor device. |
| 1203 |
* @np: a pointer to a device tree node. |
| 1204 |
* @type: the thermal cooling device type. |
| 1205 |
* @devdata: device private data. |
| 1206 |
* @ops: standard thermal cooling devices callbacks. |
| 1207 |
* |
| 1208 |
* This function will register a cooling device with device tree node reference. |
| 1209 |
* This interface function adds a new thermal cooling device (fan/processor/...) |
| 1210 |
* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 1211 |
* to all the thermal zone devices registered at the same time. |
| 1212 |
* |
| 1213 |
* Return: a pointer to the created struct thermal_cooling_device or an |
| 1214 |
* ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
| 1215 |
*/ |
| 1216 |
struct thermal_cooling_device * |
| 1217 |
devm_thermal_of_cooling_device_register(struct device *dev, |
| 1218 |
struct device_node *np, |
| 1219 |
const char *type, void *devdata, |
| 1220 |
const struct thermal_cooling_device_ops *ops) |
| 1221 |
{ |
| 1222 |
struct thermal_cooling_device **ptr, *tcd; |
| 1223 |
|
| 1224 |
ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr), |
| 1225 |
GFP_KERNEL); |
| 1226 |
if (!ptr) |
| 1227 |
return ERR_PTR(-ENOMEM); |
| 1228 |
|
| 1229 |
tcd = __thermal_cooling_device_register(np, type, devdata, ops); |
| 1230 |
if (IS_ERR(tcd)) { |
| 1231 |
devres_free(ptr); |
| 1232 |
return tcd; |
| 1233 |
} |
| 1234 |
|
| 1235 |
*ptr = tcd; |
| 1236 |
devres_add(dev, ptr); |
| 1237 |
|
| 1238 |
return tcd; |
| 1239 |
} |
| 1240 |
EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register); |
| 1241 |
|
| 1242 |
static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev) |
| 1243 |
{ |
| 1244 |
struct thermal_cooling_device *pos = NULL; |
| 1245 |
|
| 1246 |
list_for_each_entry(pos, &thermal_cdev_list, node) { |
| 1247 |
if (pos == cdev) |
| 1248 |
return true; |
| 1249 |
} |
| 1250 |
|
| 1251 |
return false; |
| 1252 |
} |
| 1253 |
|
| 1254 |
/** |
| 1255 |
* thermal_cooling_device_update - Update a cooling device object |
| 1256 |
* @cdev: Target cooling device. |
| 1257 |
* |
| 1258 |
* Update @cdev to reflect a change of the underlying hardware or platform. |
| 1259 |
* |
| 1260 |
* Must be called when the maximum cooling state of @cdev becomes invalid and so |
| 1261 |
* its .get_max_state() callback needs to be run to produce the new maximum |
| 1262 |
* cooling state value. |
| 1263 |
*/ |
| 1264 |
void thermal_cooling_device_update(struct thermal_cooling_device *cdev) |
| 1265 |
{ |
| 1266 |
struct thermal_instance *ti; |
| 1267 |
unsigned long state; |
| 1268 |
|
| 1269 |
if (IS_ERR_OR_NULL(cdev)) |
| 1270 |
return; |
| 1271 |
|
| 1272 |
/* |
| 1273 |
* Hold thermal_list_lock throughout the update to prevent the device |
| 1274 |
* from going away while being updated. |
| 1275 |
*/ |
| 1276 |
guard(mutex)(&thermal_list_lock); |
| 1277 |
|
| 1278 |
if (!thermal_cooling_device_present(cdev)) |
| 1279 |
return; |
| 1280 |
|
| 1281 |
/* |
| 1282 |
* Update under the cdev lock to prevent the state from being set beyond |
| 1283 |
* the new limit concurrently. |
| 1284 |
*/ |
| 1285 |
guard(cooling_dev)(cdev); |
| 1286 |
|
| 1287 |
if (cdev->ops->get_max_state(cdev, &cdev->max_state)) |
| 1288 |
return; |
| 1289 |
|
| 1290 |
thermal_cooling_device_stats_reinit(cdev); |
| 1291 |
|
| 1292 |
list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) { |
| 1293 |
if (ti->upper == cdev->max_state) |
| 1294 |
continue; |
| 1295 |
|
| 1296 |
if (ti->upper < cdev->max_state) { |
| 1297 |
if (ti->upper_no_limit) |
| 1298 |
ti->upper = cdev->max_state; |
| 1299 |
|
| 1300 |
continue; |
| 1301 |
} |
| 1302 |
|
| 1303 |
ti->upper = cdev->max_state; |
| 1304 |
if (ti->lower > ti->upper) |
| 1305 |
ti->lower = ti->upper; |
| 1306 |
|
| 1307 |
if (ti->target == THERMAL_NO_TARGET) |
| 1308 |
continue; |
| 1309 |
|
| 1310 |
if (ti->target > ti->upper) |
| 1311 |
ti->target = ti->upper; |
| 1312 |
} |
| 1313 |
|
| 1314 |
if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state) |
| 1315 |
return; |
| 1316 |
|
| 1317 |
thermal_cooling_device_stats_update(cdev, state); |
| 1318 |
} |
| 1319 |
EXPORT_SYMBOL_GPL(thermal_cooling_device_update); |
| 1320 |
|
| 1321 |
static void __thermal_zone_cdev_unbind(struct thermal_zone_device *tz, |
| 1322 |
struct thermal_cooling_device *cdev) |
| 1323 |
{ |
| 1324 |
struct thermal_trip_desc *td; |
| 1325 |
|
| 1326 |
for_each_trip_desc(tz, td) |
| 1327 |
thermal_unbind_cdev_from_trip(tz, td, cdev); |
| 1328 |
} |
| 1329 |
|
| 1330 |
static void thermal_zone_cdev_unbind(struct thermal_zone_device *tz, |
| 1331 |
struct thermal_cooling_device *cdev) |
| 1332 |
{ |
| 1333 |
guard(thermal_zone)(tz); |
| 1334 |
|
| 1335 |
__thermal_zone_cdev_unbind(tz, cdev); |
| 1336 |
} |
| 1337 |
|
| 1338 |
static bool thermal_cooling_device_exit(struct thermal_cooling_device *cdev) |
| 1339 |
{ |
| 1340 |
struct thermal_zone_device *tz; |
| 1341 |
|
| 1342 |
guard(mutex)(&thermal_list_lock); |
| 1343 |
|
| 1344 |
if (!thermal_cooling_device_present(cdev)) |
| 1345 |
return false; |
| 1346 |
|
| 1347 |
list_del(&cdev->node); |
| 1348 |
|
| 1349 |
list_for_each_entry(tz, &thermal_tz_list, node) |
| 1350 |
thermal_zone_cdev_unbind(tz, cdev); |
| 1351 |
|
| 1352 |
return true; |
| 1353 |
} |
| 1354 |
|
| 1355 |
/** |
| 1356 |
* thermal_cooling_device_unregister() - removes a thermal cooling device |
| 1357 |
* @cdev: Thermal cooling device to remove. |
| 1358 |
*/ |
| 1359 |
void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) |
| 1360 |
{ |
| 1361 |
if (!cdev) |
| 1362 |
return; |
| 1363 |
|
| 1364 |
thermal_debug_cdev_remove(cdev); |
| 1365 |
|
| 1366 |
if (thermal_cooling_device_exit(cdev)) |
| 1367 |
device_unregister(&cdev->device); |
| 1368 |
} |
| 1369 |
EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); |
| 1370 |
|
| 1371 |
int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) |
| 1372 |
{ |
| 1373 |
const struct thermal_trip_desc *td; |
| 1374 |
int ret = -EINVAL; |
| 1375 |
|
| 1376 |
if (tz->ops.get_crit_temp) |
| 1377 |
return tz->ops.get_crit_temp(tz, temp); |
| 1378 |
|
| 1379 |
guard(thermal_zone)(tz); |
| 1380 |
|
| 1381 |
for_each_trip_desc(tz, td) { |
| 1382 |
const struct thermal_trip *trip = &td->trip; |
| 1383 |
|
| 1384 |
if (trip->type == THERMAL_TRIP_CRITICAL) { |
| 1385 |
*temp = trip->temperature; |
| 1386 |
ret = 0; |
| 1387 |
break; |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
return ret; |
| 1392 |
} |
| 1393 |
EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp); |
| 1394 |
|
| 1395 |
static void thermal_zone_device_check(struct work_struct *work) |
| 1396 |
{ |
| 1397 |
struct thermal_zone_device *tz = container_of(work, struct |
| 1398 |
thermal_zone_device, |
| 1399 |
poll_queue.work); |
| 1400 |
thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
| 1401 |
} |
| 1402 |
|
| 1403 |
static void thermal_zone_device_init(struct thermal_zone_device *tz) |
| 1404 |
{ |
| 1405 |
struct thermal_trip_desc *td, *next; |
| 1406 |
|
| 1407 |
INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check); |
| 1408 |
|
| 1409 |
tz->temperature = THERMAL_TEMP_INIT; |
| 1410 |
tz->passive = 0; |
| 1411 |
tz->prev_low_trip = -INT_MAX; |
| 1412 |
tz->prev_high_trip = INT_MAX; |
| 1413 |
for_each_trip_desc(tz, td) { |
| 1414 |
struct thermal_instance *instance; |
| 1415 |
|
| 1416 |
list_for_each_entry(instance, &td->thermal_instances, trip_node) |
| 1417 |
instance->initialized = false; |
| 1418 |
} |
| 1419 |
/* |
| 1420 |
* At this point, all valid trips need to be moved to trips_high so that |
| 1421 |
* mitigation can be started if the zone temperature is above them. |
| 1422 |
*/ |
| 1423 |
list_for_each_entry_safe(td, next, &tz->trips_invalid, list_node) { |
| 1424 |
if (td->trip.temperature != THERMAL_TEMP_INVALID) |
| 1425 |
move_to_trips_high(tz, td); |
| 1426 |
} |
| 1427 |
/* The trips_reached list may not be empty during system resume. */ |
| 1428 |
list_for_each_entry_safe(td, next, &tz->trips_reached, list_node) { |
| 1429 |
if (td->trip.temperature == THERMAL_TEMP_INVALID) |
| 1430 |
move_to_trips_invalid(tz, td); |
| 1431 |
else |
| 1432 |
move_to_trips_high(tz, td); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
static int thermal_zone_init_governor(struct thermal_zone_device *tz) |
| 1437 |
{ |
| 1438 |
struct thermal_governor *governor; |
| 1439 |
|
| 1440 |
guard(mutex)(&thermal_governor_lock); |
| 1441 |
|
| 1442 |
if (tz->tzp) |
| 1443 |
governor = __find_governor(tz->tzp->governor_name); |
| 1444 |
else |
| 1445 |
governor = def_governor; |
| 1446 |
|
| 1447 |
return thermal_set_governor(tz, governor); |
| 1448 |
} |
| 1449 |
|
| 1450 |
static void thermal_zone_init_complete(struct thermal_zone_device *tz) |
| 1451 |
{ |
| 1452 |
struct thermal_cooling_device *cdev; |
| 1453 |
|
| 1454 |
guard(mutex)(&thermal_list_lock); |
| 1455 |
|
| 1456 |
list_add_tail(&tz->node, &thermal_tz_list); |
| 1457 |
|
| 1458 |
guard(thermal_zone)(tz); |
| 1459 |
|
| 1460 |
/* Bind cooling devices for this zone. */ |
| 1461 |
list_for_each_entry(cdev, &thermal_cdev_list, node) |
| 1462 |
__thermal_zone_cdev_bind(tz, cdev); |
| 1463 |
|
| 1464 |
tz->state &= ~TZ_STATE_FLAG_INIT; |
| 1465 |
/* |
| 1466 |
* If system suspend or resume is in progress at this point, the |
| 1467 |
* new thermal zone needs to be marked as suspended because |
| 1468 |
* thermal_pm_notify() has run already. |
| 1469 |
*/ |
| 1470 |
if (thermal_pm_suspended) |
| 1471 |
tz->state |= TZ_STATE_FLAG_SUSPENDED; |
| 1472 |
|
| 1473 |
__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
| 1474 |
} |
| 1475 |
|
| 1476 |
/** |
| 1477 |
* thermal_zone_device_register_with_trips() - register a new thermal zone device |
| 1478 |
* @type: the thermal zone device type |
| 1479 |
* @trips: a pointer to an array of thermal trips |
| 1480 |
* @num_trips: the number of trip points the thermal zone support |
| 1481 |
* @devdata: private device data |
| 1482 |
* @ops: standard thermal zone device callbacks |
| 1483 |
* @tzp: thermal zone platform parameters |
| 1484 |
* @passive_delay: number of milliseconds to wait between polls when |
| 1485 |
* performing passive cooling |
| 1486 |
* @polling_delay: number of milliseconds to wait between polls when checking |
| 1487 |
* whether trip points have been crossed (0 for interrupt |
| 1488 |
* driven systems) |
| 1489 |
* |
| 1490 |
* This interface function adds a new thermal zone device (sensor) to |
| 1491 |
* /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the |
| 1492 |
* thermal cooling devices registered at the same time. |
| 1493 |
* thermal_zone_device_unregister() must be called when the device is no |
| 1494 |
* longer needed. The passive cooling depends on the .get_trend() return value. |
| 1495 |
* |
| 1496 |
* Return: a pointer to the created struct thermal_zone_device or an |
| 1497 |
* in case of error, an ERR_PTR. Caller must check return value with |
| 1498 |
* IS_ERR*() helpers. |
| 1499 |
*/ |
| 1500 |
struct thermal_zone_device * |
| 1501 |
thermal_zone_device_register_with_trips(const char *type, |
| 1502 |
const struct thermal_trip *trips, |
| 1503 |
int num_trips, void *devdata, |
| 1504 |
const struct thermal_zone_device_ops *ops, |
| 1505 |
const struct thermal_zone_params *tzp, |
| 1506 |
unsigned int passive_delay, |
| 1507 |
unsigned int polling_delay) |
| 1508 |
{ |
| 1509 |
const struct thermal_trip *trip = trips; |
| 1510 |
struct thermal_zone_device *tz; |
| 1511 |
struct thermal_trip_desc *td; |
| 1512 |
size_t type_len = 0; |
| 1513 |
int id; |
| 1514 |
int result; |
| 1515 |
|
| 1516 |
if (type) |
| 1517 |
type_len = strnlen(type, THERMAL_NAME_LENGTH); |
| 1518 |
|
| 1519 |
if (type_len == 0) { |
| 1520 |
pr_err("No thermal zone type defined\n"); |
| 1521 |
return ERR_PTR(-EINVAL); |
| 1522 |
} |
| 1523 |
|
| 1524 |
if (type_len == THERMAL_NAME_LENGTH) { |
| 1525 |
pr_err("Thermal zone name (%s) too long, should be under %d chars\n", |
| 1526 |
type, THERMAL_NAME_LENGTH); |
| 1527 |
return ERR_PTR(-EINVAL); |
| 1528 |
} |
| 1529 |
|
| 1530 |
if (num_trips < 0) { |
| 1531 |
pr_err("Incorrect number of thermal trips\n"); |
| 1532 |
return ERR_PTR(-EINVAL); |
| 1533 |
} |
| 1534 |
|
| 1535 |
if (!ops || !ops->get_temp) { |
| 1536 |
pr_err("Thermal zone device ops not defined or invalid\n"); |
| 1537 |
return ERR_PTR(-EINVAL); |
| 1538 |
} |
| 1539 |
|
| 1540 |
if (num_trips > 0 && !trips) |
| 1541 |
return ERR_PTR(-EINVAL); |
| 1542 |
|
| 1543 |
if (polling_delay && passive_delay > polling_delay) |
| 1544 |
return ERR_PTR(-EINVAL); |
| 1545 |
|
| 1546 |
if (!thermal_class) |
| 1547 |
return ERR_PTR(-ENODEV); |
| 1548 |
|
| 1549 |
tz = kzalloc_flex(*tz, trips, num_trips); |
| 1550 |
if (!tz) |
| 1551 |
return ERR_PTR(-ENOMEM); |
| 1552 |
|
| 1553 |
if (tzp) { |
| 1554 |
tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL); |
| 1555 |
if (!tz->tzp) { |
| 1556 |
result = -ENOMEM; |
| 1557 |
goto free_tz; |
| 1558 |
} |
| 1559 |
} |
| 1560 |
|
| 1561 |
INIT_LIST_HEAD(&tz->node); |
| 1562 |
INIT_LIST_HEAD(&tz->trips_high); |
| 1563 |
INIT_LIST_HEAD(&tz->trips_reached); |
| 1564 |
INIT_LIST_HEAD(&tz->trips_invalid); |
| 1565 |
ida_init(&tz->ida); |
| 1566 |
mutex_init(&tz->lock); |
| 1567 |
init_completion(&tz->removal); |
| 1568 |
init_completion(&tz->resume); |
| 1569 |
id = ida_alloc(&thermal_tz_ida, GFP_KERNEL); |
| 1570 |
if (id < 0) { |
| 1571 |
result = id; |
| 1572 |
goto free_tzp; |
| 1573 |
} |
| 1574 |
|
| 1575 |
tz->id = id; |
| 1576 |
strscpy(tz->type, type, sizeof(tz->type)); |
| 1577 |
|
| 1578 |
tz->ops = *ops; |
| 1579 |
if (!tz->ops.critical) |
| 1580 |
tz->ops.critical = thermal_zone_device_critical; |
| 1581 |
|
| 1582 |
tz->device.class = thermal_class; |
| 1583 |
tz->devdata = devdata; |
| 1584 |
tz->num_trips = num_trips; |
| 1585 |
for_each_trip_desc(tz, td) { |
| 1586 |
td->trip = *trip++; |
| 1587 |
INIT_LIST_HEAD(&td->thermal_instances); |
| 1588 |
INIT_LIST_HEAD(&td->list_node); |
| 1589 |
/* |
| 1590 |
* Mark all thresholds as invalid to start with even though |
| 1591 |
* this only matters for the trips that start as invalid and |
| 1592 |
* become valid later. |
| 1593 |
*/ |
| 1594 |
move_to_trips_invalid(tz, td); |
| 1595 |
} |
| 1596 |
|
| 1597 |
tz->polling_delay_jiffies = msecs_to_jiffies(polling_delay); |
| 1598 |
tz->passive_delay_jiffies = msecs_to_jiffies(passive_delay); |
| 1599 |
tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY; |
| 1600 |
|
| 1601 |
tz->state = TZ_STATE_FLAG_INIT; |
| 1602 |
|
| 1603 |
result = dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
| 1604 |
if (result) |
| 1605 |
goto remove_id; |
| 1606 |
|
| 1607 |
thermal_zone_device_init(tz); |
| 1608 |
|
| 1609 |
result = thermal_zone_init_governor(tz); |
| 1610 |
if (result) |
| 1611 |
goto remove_id; |
| 1612 |
|
| 1613 |
/* sys I/F */ |
| 1614 |
/* Add nodes that are always present via .groups */ |
| 1615 |
result = thermal_zone_create_device_groups(tz); |
| 1616 |
if (result) { |
| 1617 |
thermal_set_governor(tz, NULL); |
| 1618 |
goto remove_id; |
| 1619 |
} |
| 1620 |
|
| 1621 |
result = device_register(&tz->device); |
| 1622 |
if (result) |
| 1623 |
goto release_device; |
| 1624 |
|
| 1625 |
if (!tz->tzp || !tz->tzp->no_hwmon) { |
| 1626 |
result = thermal_add_hwmon_sysfs(tz); |
| 1627 |
if (result) |
| 1628 |
goto unregister; |
| 1629 |
} |
| 1630 |
|
| 1631 |
result = thermal_thresholds_init(tz); |
| 1632 |
if (result) |
| 1633 |
goto remove_hwmon; |
| 1634 |
|
| 1635 |
thermal_zone_init_complete(tz); |
| 1636 |
|
| 1637 |
thermal_notify_tz_create(tz); |
| 1638 |
|
| 1639 |
thermal_debug_tz_add(tz); |
| 1640 |
|
| 1641 |
return tz; |
| 1642 |
|
| 1643 |
remove_hwmon: |
| 1644 |
thermal_remove_hwmon_sysfs(tz); |
| 1645 |
unregister: |
| 1646 |
device_del(&tz->device); |
| 1647 |
release_device: |
| 1648 |
put_device(&tz->device); |
| 1649 |
wait_for_completion(&tz->removal); |
| 1650 |
remove_id: |
| 1651 |
ida_free(&thermal_tz_ida, id); |
| 1652 |
free_tzp: |
| 1653 |
kfree(tz->tzp); |
| 1654 |
free_tz: |
| 1655 |
kfree(tz); |
| 1656 |
return ERR_PTR(result); |
| 1657 |
} |
| 1658 |
EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips); |
| 1659 |
|
| 1660 |
struct thermal_zone_device *thermal_tripless_zone_device_register( |
| 1661 |
const char *type, |
| 1662 |
void *devdata, |
| 1663 |
const struct thermal_zone_device_ops *ops, |
| 1664 |
const struct thermal_zone_params *tzp) |
| 1665 |
{ |
| 1666 |
return thermal_zone_device_register_with_trips(type, NULL, 0, devdata, |
| 1667 |
ops, tzp, 0, 0); |
| 1668 |
} |
| 1669 |
EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register); |
| 1670 |
|
| 1671 |
void *thermal_zone_device_priv(struct thermal_zone_device *tzd) |
| 1672 |
{ |
| 1673 |
return tzd->devdata; |
| 1674 |
} |
| 1675 |
EXPORT_SYMBOL_GPL(thermal_zone_device_priv); |
| 1676 |
|
| 1677 |
const char *thermal_zone_device_type(struct thermal_zone_device *tzd) |
| 1678 |
{ |
| 1679 |
return tzd->type; |
| 1680 |
} |
| 1681 |
EXPORT_SYMBOL_GPL(thermal_zone_device_type); |
| 1682 |
|
| 1683 |
int thermal_zone_device_id(struct thermal_zone_device *tzd) |
| 1684 |
{ |
| 1685 |
return tzd->id; |
| 1686 |
} |
| 1687 |
EXPORT_SYMBOL_GPL(thermal_zone_device_id); |
| 1688 |
|
| 1689 |
struct device *thermal_zone_device(struct thermal_zone_device *tzd) |
| 1690 |
{ |
| 1691 |
return &tzd->device; |
| 1692 |
} |
| 1693 |
EXPORT_SYMBOL_GPL(thermal_zone_device); |
| 1694 |
|
| 1695 |
static bool thermal_zone_exit(struct thermal_zone_device *tz) |
| 1696 |
{ |
| 1697 |
struct thermal_cooling_device *cdev; |
| 1698 |
|
| 1699 |
guard(mutex)(&thermal_list_lock); |
| 1700 |
|
| 1701 |
if (list_empty(&tz->node)) |
| 1702 |
return false; |
| 1703 |
|
| 1704 |
guard(thermal_zone)(tz); |
| 1705 |
|
| 1706 |
tz->state |= TZ_STATE_FLAG_EXIT; |
| 1707 |
list_del_init(&tz->node); |
| 1708 |
|
| 1709 |
/* Unbind all cdevs associated with this thermal zone. */ |
| 1710 |
list_for_each_entry(cdev, &thermal_cdev_list, node) |
| 1711 |
__thermal_zone_cdev_unbind(tz, cdev); |
| 1712 |
|
| 1713 |
return true; |
| 1714 |
} |
| 1715 |
|
| 1716 |
/** |
| 1717 |
* thermal_zone_device_unregister - removes the registered thermal zone device |
| 1718 |
* @tz: the thermal zone device to remove |
| 1719 |
*/ |
| 1720 |
void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 1721 |
{ |
| 1722 |
if (!tz) |
| 1723 |
return; |
| 1724 |
|
| 1725 |
thermal_debug_tz_remove(tz); |
| 1726 |
|
| 1727 |
if (!thermal_zone_exit(tz)) |
| 1728 |
return; |
| 1729 |
|
| 1730 |
cancel_delayed_work_sync(&tz->poll_queue); |
| 1731 |
|
| 1732 |
thermal_thresholds_exit(tz); |
| 1733 |
thermal_remove_hwmon_sysfs(tz); |
| 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 |
|
| 1742 |
ida_free(&thermal_tz_ida, tz->id); |
| 1743 |
|
| 1744 |
kfree(tz->tzp); |
| 1745 |
kfree(tz); |
| 1746 |
} |
| 1747 |
EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); |
| 1748 |
|
| 1749 |
/** |
| 1750 |
* thermal_zone_get_zone_by_name() - search for a zone and returns its ref |
| 1751 |
* @name: thermal zone name to fetch the temperature |
| 1752 |
* |
| 1753 |
* When only one zone is found with the passed name, returns a reference to it. |
| 1754 |
* |
| 1755 |
* Return: On success returns a reference to an unique thermal zone with |
| 1756 |
* matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid |
| 1757 |
* paramenters, -ENODEV for not found and -EEXIST for multiple matches). |
| 1758 |
*/ |
| 1759 |
struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) |
| 1760 |
{ |
| 1761 |
struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL); |
| 1762 |
unsigned int found = 0; |
| 1763 |
|
| 1764 |
if (!name) |
| 1765 |
return ERR_PTR(-EINVAL); |
| 1766 |
|
| 1767 |
guard(mutex)(&thermal_list_lock); |
| 1768 |
|
| 1769 |
list_for_each_entry(pos, &thermal_tz_list, node) |
| 1770 |
if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) { |
| 1771 |
found++; |
| 1772 |
ref = pos; |
| 1773 |
} |
| 1774 |
|
| 1775 |
if (!found) |
| 1776 |
return ERR_PTR(-ENODEV); |
| 1777 |
|
| 1778 |
/* Success only when one zone is found. */ |
| 1779 |
if (found > 1) |
| 1780 |
return ERR_PTR(-EEXIST); |
| 1781 |
|
| 1782 |
return ref; |
| 1783 |
} |
| 1784 |
EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); |
| 1785 |
|
| 1786 |
static void thermal_zone_device_resume(struct work_struct *work) |
| 1787 |
{ |
| 1788 |
struct thermal_zone_device *tz; |
| 1789 |
|
| 1790 |
tz = container_of(work, struct thermal_zone_device, poll_queue.work); |
| 1791 |
|
| 1792 |
guard(thermal_zone)(tz); |
| 1793 |
|
| 1794 |
/* If the thermal zone is going away, there's nothing to do. */ |
| 1795 |
if (tz->state & TZ_STATE_FLAG_EXIT) |
| 1796 |
return; |
| 1797 |
|
| 1798 |
tz->state &= ~(TZ_STATE_FLAG_SUSPENDED | TZ_STATE_FLAG_RESUMING); |
| 1799 |
|
| 1800 |
thermal_debug_tz_resume(tz); |
| 1801 |
thermal_zone_device_init(tz); |
| 1802 |
thermal_governor_update_tz(tz, THERMAL_TZ_RESUME); |
| 1803 |
__thermal_zone_device_update(tz, THERMAL_TZ_RESUME); |
| 1804 |
|
| 1805 |
complete(&tz->resume); |
| 1806 |
} |
| 1807 |
|
| 1808 |
static void thermal_zone_pm_prepare(struct thermal_zone_device *tz) |
| 1809 |
{ |
| 1810 |
guard(thermal_zone)(tz); |
| 1811 |
|
| 1812 |
if (tz->state & TZ_STATE_FLAG_RESUMING) { |
| 1813 |
/* |
| 1814 |
* thermal_zone_device_resume() queued up for this zone has not |
| 1815 |
* acquired the lock yet, so release it to let the function run |
| 1816 |
* and wait util it has done the work. |
| 1817 |
*/ |
| 1818 |
scoped_guard(thermal_zone_reverse, tz) { |
| 1819 |
wait_for_completion(&tz->resume); |
| 1820 |
} |
| 1821 |
} |
| 1822 |
|
| 1823 |
tz->state |= TZ_STATE_FLAG_SUSPENDED; |
| 1824 |
|
| 1825 |
/* Prevent new work from getting to the workqueue subsequently. */ |
| 1826 |
cancel_delayed_work(&tz->poll_queue); |
| 1827 |
} |
| 1828 |
|
| 1829 |
static void thermal_pm_notify_prepare(void) |
| 1830 |
{ |
| 1831 |
struct thermal_zone_device *tz; |
| 1832 |
|
| 1833 |
guard(mutex)(&thermal_list_lock); |
| 1834 |
|
| 1835 |
thermal_pm_suspended = true; |
| 1836 |
|
| 1837 |
list_for_each_entry(tz, &thermal_tz_list, node) |
| 1838 |
thermal_zone_pm_prepare(tz); |
| 1839 |
} |
| 1840 |
|
| 1841 |
static void thermal_zone_pm_complete(struct thermal_zone_device *tz) |
| 1842 |
{ |
| 1843 |
guard(thermal_zone)(tz); |
| 1844 |
|
| 1845 |
reinit_completion(&tz->resume); |
| 1846 |
tz->state |= TZ_STATE_FLAG_RESUMING; |
| 1847 |
|
| 1848 |
/* |
| 1849 |
* Replace the work function with the resume one, which will restore the |
| 1850 |
* original work function and schedule the polling work if needed. |
| 1851 |
*/ |
| 1852 |
INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_resume); |
| 1853 |
/* Queue up the work without a delay. */ |
| 1854 |
mod_delayed_work(thermal_wq, &tz->poll_queue, 0); |
| 1855 |
} |
| 1856 |
|
| 1857 |
static void thermal_pm_notify_complete(void) |
| 1858 |
{ |
| 1859 |
struct thermal_zone_device *tz; |
| 1860 |
|
| 1861 |
guard(mutex)(&thermal_list_lock); |
| 1862 |
|
| 1863 |
thermal_pm_suspended = false; |
| 1864 |
|
| 1865 |
list_for_each_entry(tz, &thermal_tz_list, node) |
| 1866 |
thermal_zone_pm_complete(tz); |
| 1867 |
} |
| 1868 |
|
| 1869 |
static int thermal_pm_notify(struct notifier_block *nb, |
| 1870 |
unsigned long mode, void *_unused) |
| 1871 |
{ |
| 1872 |
switch (mode) { |
| 1873 |
case PM_HIBERNATION_PREPARE: |
| 1874 |
case PM_RESTORE_PREPARE: |
| 1875 |
case PM_SUSPEND_PREPARE: |
| 1876 |
thermal_pm_notify_prepare(); |
| 1877 |
/* |
| 1878 |
* Allow any leftover thermal work items already on the |
| 1879 |
* worqueue to complete so they don't get in the way later. |
| 1880 |
*/ |
| 1881 |
flush_workqueue(thermal_wq); |
| 1882 |
break; |
| 1883 |
case PM_POST_HIBERNATION: |
| 1884 |
case PM_POST_RESTORE: |
| 1885 |
case PM_POST_SUSPEND: |
| 1886 |
thermal_pm_notify_complete(); |
| 1887 |
break; |
| 1888 |
default: |
| 1889 |
break; |
| 1890 |
} |
| 1891 |
return 0; |
| 1892 |
} |
| 1893 |
|
| 1894 |
static struct notifier_block thermal_pm_nb = { |
| 1895 |
.notifier_call = thermal_pm_notify, |
| 1896 |
/* |
| 1897 |
* Run at the lowest priority to avoid interference between the thermal |
| 1898 |
* zone resume work items spawned by thermal_pm_notify() and the other |
| 1899 |
* PM notifiers. |
| 1900 |
*/ |
| 1901 |
.priority = INT_MIN, |
| 1902 |
}; |
| 1903 |
|
| 1904 |
static int __init thermal_init(void) |
| 1905 |
{ |
| 1906 |
int result; |
| 1907 |
|
| 1908 |
thermal_debug_init(); |
| 1909 |
|
| 1910 |
result = thermal_netlink_init(); |
| 1911 |
if (result) |
| 1912 |
goto error; |
| 1913 |
|
| 1914 |
thermal_wq = alloc_workqueue("thermal_events", |
| 1915 |
WQ_FREEZABLE | WQ_POWER_EFFICIENT | WQ_PERCPU, 0); |
| 1916 |
if (!thermal_wq) { |
| 1917 |
result = -ENOMEM; |
| 1918 |
goto unregister_netlink; |
| 1919 |
} |
| 1920 |
|
| 1921 |
result = thermal_register_governors(); |
| 1922 |
if (result) |
| 1923 |
goto destroy_workqueue; |
| 1924 |
|
| 1925 |
thermal_class = kzalloc_obj(*thermal_class); |
| 1926 |
if (!thermal_class) { |
| 1927 |
result = -ENOMEM; |
| 1928 |
goto unregister_governors; |
| 1929 |
} |
| 1930 |
|
| 1931 |
thermal_class->name = "thermal"; |
| 1932 |
thermal_class->dev_release = thermal_release; |
| 1933 |
|
| 1934 |
result = class_register(thermal_class); |
| 1935 |
if (result) { |
| 1936 |
kfree(thermal_class); |
| 1937 |
thermal_class = NULL; |
| 1938 |
goto unregister_governors; |
| 1939 |
} |
| 1940 |
|
| 1941 |
result = register_pm_notifier(&thermal_pm_nb); |
| 1942 |
if (result) |
| 1943 |
pr_warn("Thermal: Can not register suspend notifier, return %d\n", |
| 1944 |
result); |
| 1945 |
|
| 1946 |
return 0; |
| 1947 |
|
| 1948 |
unregister_governors: |
| 1949 |
thermal_unregister_governors(); |
| 1950 |
destroy_workqueue: |
| 1951 |
destroy_workqueue(thermal_wq); |
| 1952 |
unregister_netlink: |
| 1953 |
thermal_netlink_exit(); |
| 1954 |
error: |
| 1955 |
mutex_destroy(&thermal_list_lock); |
| 1956 |
mutex_destroy(&thermal_governor_lock); |
| 1957 |
return result; |
| 1958 |
} |
| 1959 |
postcore_initcall(thermal_init); |