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