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