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