xref: /linux/drivers/thermal/thermal_debugfs.c (revision 1c75adb22d49ca9389333ca5e6939052a7203111)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2023 Linaro Limited
4  *
5  * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
6  *
7  * Thermal subsystem debug support
8  */
9 #include <linux/debugfs.h>
10 #include <linux/ktime.h>
11 #include <linux/list.h>
12 #include <linux/minmax.h>
13 #include <linux/mutex.h>
14 #include <linux/thermal.h>
15 
16 #include "thermal_core.h"
17 
18 static struct dentry *d_root;
19 static struct dentry *d_cdev;
20 static struct dentry *d_tz;
21 
22 /*
23  * Length of the string containing the thermal zone id or the cooling
24  * device id, including the ending nul character. We can reasonably
25  * assume there won't be more than 256 thermal zones as the maximum
26  * observed today is around 32.
27  */
28 #define IDSLENGTH 4
29 
30 /*
31  * The cooling device transition list is stored in a hash table where
32  * the size is CDEVSTATS_HASH_SIZE. The majority of cooling devices
33  * have dozen of states but some can have much more, so a hash table
34  * is more adequate in this case, because the cost of browsing the entire
35  * list when storing the transitions may not be negligible.
36  */
37 #define CDEVSTATS_HASH_SIZE 16
38 
39 /**
40  * struct cdev_debugfs - per cooling device statistics structure
41  * A cooling device can have a high number of states. Showing the
42  * transitions on a matrix based representation can be overkill given
43  * most of the transitions won't happen and we end up with a matrix
44  * filled with zero. Instead, we show the transitions which actually
45  * happened.
46  *
47  * Every transition updates the current_state and the timestamp. The
48  * transitions and the durations are stored in lists.
49  *
50  * @total: the number of transitions for this cooling device
51  * @current_state: the current cooling device state
52  * @timestamp: the state change timestamp
53  * @transitions: an array of lists containing the state transitions
54  * @durations: an array of lists containing the residencies of each state
55  */
56 struct cdev_debugfs {
57 	u32 total;
58 	int current_state;
59 	ktime_t timestamp;
60 	struct list_head transitions[CDEVSTATS_HASH_SIZE];
61 	struct list_head durations[CDEVSTATS_HASH_SIZE];
62 };
63 
64 /**
65  * struct cdev_record - Common structure for cooling device entry
66  *
67  * The following common structure allows to store the information
68  * related to the transitions and to the state residencies. They are
69  * identified with a id which is associated to a value. It is used as
70  * nodes for the "transitions" and "durations" above.
71  *
72  * @node: node to insert the structure in a list
73  * @id: identifier of the value which can be a state or a transition
74  * @residency: a ktime_t representing a state residency duration
75  * @count: a number of occurrences
76  */
77 struct cdev_record {
78 	struct list_head node;
79 	int id;
80 	union {
81                 ktime_t residency;
82                 u64 count;
83         };
84 };
85 
86 /**
87  * struct trip_stats - Thermal trip statistics
88  *
89  * The trip_stats structure has the relevant information to show the
90  * statistics related to temperature going above a trip point.
91  *
92  * @timestamp: the trip crossing timestamp
93  * @duration: total time when the zone temperature was above the trip point
94  * @trip_temp: trip temperature at mitigation start
95  * @trip_hyst: trip hysteresis at mitigation start
96  * @count: the number of times the zone temperature was above the trip point
97  * @max: maximum recorded temperature above the trip point
98  * @min: minimum recorded temperature above the trip point
99  * @avg: average temperature above the trip point
100  */
101 struct trip_stats {
102 	ktime_t timestamp;
103 	ktime_t duration;
104 	int trip_temp;
105 	int trip_hyst;
106 	int count;
107 	int max;
108 	int min;
109 	int avg;
110 };
111 
112 /**
113  * struct tz_episode - A mitigation episode information
114  *
115  * The tz_episode structure describes a mitigation episode. A
116  * mitigation episode begins the trip point with the lower temperature
117  * is crossed the way up and ends when it is crossed the way
118  * down. During this episode we can have multiple trip points crossed
119  * the way up and down if there are multiple trip described in the
120  * firmware after the lowest temperature trip point.
121  *
122  * @timestamp: first trip point crossed the way up
123  * @duration: total duration of the mitigation episode
124  * @node: a list element to be added to the list of tz events
125  * @trip_stats: per trip point statistics, flexible array
126  */
127 struct tz_episode {
128 	ktime_t timestamp;
129 	ktime_t duration;
130 	struct list_head node;
131 	struct trip_stats trip_stats[];
132 };
133 
134 /**
135  * struct tz_debugfs - Store all mitigation episodes for a thermal zone
136  *
137  * The tz_debugfs structure contains the list of the mitigation
138  * episodes and has to track which trip point has been crossed in
139  * order to handle correctly nested trip point mitigation episodes.
140  *
141  * We keep the history of the trip point crossed in an array and as we
142  * can go back and forth inside this history, eg. trip 0,1,2,1,2,1,0,
143  * we keep track of the current position in the history array.
144  *
145  * @tz_episodes: a list of thermal mitigation episodes
146  * @tz: thermal zone this object belongs to
147  * @trips_crossed: an array of trip points crossed by id
148  * @nr_trips: the number of trip points currently being crossed
149  */
150 struct tz_debugfs {
151 	struct list_head tz_episodes;
152 	struct thermal_zone_device *tz;
153 	int *trips_crossed;
154 	int nr_trips;
155 };
156 
157 /**
158  * struct thermal_debugfs - High level structure for a thermal object in debugfs
159  *
160  * The thermal_debugfs structure is the common structure used by the
161  * cooling device or the thermal zone to store the statistics.
162  *
163  * @d_top: top directory of the thermal object directory
164  * @lock: per object lock to protect the internals
165  *
166  * @cdev_dbg: a cooling device debug structure
167  * @tz_dbg: a thermal zone debug structure
168  */
169 struct thermal_debugfs {
170 	struct dentry *d_top;
171 	struct mutex lock;
172 	union {
173 		struct cdev_debugfs cdev_dbg;
174 		struct tz_debugfs tz_dbg;
175 	};
176 };
177 
178 void thermal_debug_init(void)
179 {
180 	d_root = debugfs_create_dir("thermal", NULL);
181 	if (!d_root)
182 		return;
183 
184 	d_cdev = debugfs_create_dir("cooling_devices", d_root);
185 	if (!d_cdev)
186 		return;
187 
188 	d_tz = debugfs_create_dir("thermal_zones", d_root);
189 }
190 
191 static struct thermal_debugfs *thermal_debugfs_add_id(struct dentry *d, int id)
192 {
193 	struct thermal_debugfs *thermal_dbg;
194 	char ids[IDSLENGTH];
195 
196 	thermal_dbg = kzalloc(sizeof(*thermal_dbg), GFP_KERNEL);
197 	if (!thermal_dbg)
198 		return NULL;
199 
200 	mutex_init(&thermal_dbg->lock);
201 
202 	snprintf(ids, IDSLENGTH, "%d", id);
203 
204 	thermal_dbg->d_top = debugfs_create_dir(ids, d);
205 	if (!thermal_dbg->d_top) {
206 		kfree(thermal_dbg);
207 		return NULL;
208 	}
209 
210 	return thermal_dbg;
211 }
212 
213 static void thermal_debugfs_remove_id(struct thermal_debugfs *thermal_dbg)
214 {
215 	if (!thermal_dbg)
216 		return;
217 
218 	debugfs_remove(thermal_dbg->d_top);
219 
220 	kfree(thermal_dbg);
221 }
222 
223 static struct cdev_record *
224 thermal_debugfs_cdev_record_alloc(struct thermal_debugfs *thermal_dbg,
225 				  struct list_head *lists, int id)
226 {
227 	struct cdev_record *cdev_record;
228 
229 	cdev_record = kzalloc(sizeof(*cdev_record), GFP_KERNEL);
230 	if (!cdev_record)
231 		return NULL;
232 
233 	cdev_record->id = id;
234 	INIT_LIST_HEAD(&cdev_record->node);
235 	list_add_tail(&cdev_record->node,
236 		      &lists[cdev_record->id % CDEVSTATS_HASH_SIZE]);
237 
238 	return cdev_record;
239 }
240 
241 static struct cdev_record *
242 thermal_debugfs_cdev_record_find(struct thermal_debugfs *thermal_dbg,
243 				 struct list_head *lists, int id)
244 {
245 	struct cdev_record *entry;
246 
247 	list_for_each_entry(entry, &lists[id % CDEVSTATS_HASH_SIZE], node)
248 		if (entry->id == id)
249 			return entry;
250 
251 	return NULL;
252 }
253 
254 static struct cdev_record *
255 thermal_debugfs_cdev_record_get(struct thermal_debugfs *thermal_dbg,
256 				struct list_head *lists, int id)
257 {
258 	struct cdev_record *cdev_record;
259 
260 	cdev_record = thermal_debugfs_cdev_record_find(thermal_dbg, lists, id);
261 	if (cdev_record)
262 		return cdev_record;
263 
264 	return thermal_debugfs_cdev_record_alloc(thermal_dbg, lists, id);
265 }
266 
267 static void thermal_debugfs_cdev_clear(struct cdev_debugfs *cdev_dbg)
268 {
269 	int i;
270 	struct cdev_record *entry, *tmp;
271 
272 	for (i = 0; i < CDEVSTATS_HASH_SIZE; i++) {
273 
274 		list_for_each_entry_safe(entry, tmp,
275 					 &cdev_dbg->transitions[i], node) {
276 			list_del(&entry->node);
277 			kfree(entry);
278 		}
279 
280 		list_for_each_entry_safe(entry, tmp,
281 					 &cdev_dbg->durations[i], node) {
282 			list_del(&entry->node);
283 			kfree(entry);
284 		}
285 	}
286 
287 	cdev_dbg->total = 0;
288 }
289 
290 static void *cdev_seq_start(struct seq_file *s, loff_t *pos)
291 {
292 	struct thermal_debugfs *thermal_dbg = s->private;
293 
294 	mutex_lock(&thermal_dbg->lock);
295 
296 	return (*pos < CDEVSTATS_HASH_SIZE) ? pos : NULL;
297 }
298 
299 static void *cdev_seq_next(struct seq_file *s, void *v, loff_t *pos)
300 {
301 	(*pos)++;
302 
303 	return (*pos < CDEVSTATS_HASH_SIZE) ? pos : NULL;
304 }
305 
306 static void cdev_seq_stop(struct seq_file *s, void *v)
307 {
308 	struct thermal_debugfs *thermal_dbg = s->private;
309 
310 	mutex_unlock(&thermal_dbg->lock);
311 }
312 
313 static int cdev_tt_seq_show(struct seq_file *s, void *v)
314 {
315 	struct thermal_debugfs *thermal_dbg = s->private;
316 	struct cdev_debugfs *cdev_dbg = &thermal_dbg->cdev_dbg;
317 	struct list_head *transitions = cdev_dbg->transitions;
318 	struct cdev_record *entry;
319 	int i = *(loff_t *)v;
320 
321 	if (!i)
322 		seq_puts(s, "Transition\tOccurences\n");
323 
324 	list_for_each_entry(entry, &transitions[i], node) {
325 		/*
326 		 * Assuming maximum cdev states is 1024, the longer
327 		 * string for a transition would be "1024->1024\0"
328 		 */
329 		char buffer[11];
330 
331 		snprintf(buffer, ARRAY_SIZE(buffer), "%d->%d",
332 			 entry->id >> 16, entry->id & 0xFFFF);
333 
334 		seq_printf(s, "%-10s\t%-10llu\n", buffer, entry->count);
335 	}
336 
337 	return 0;
338 }
339 
340 static const struct seq_operations tt_sops = {
341 	.start = cdev_seq_start,
342 	.next = cdev_seq_next,
343 	.stop = cdev_seq_stop,
344 	.show = cdev_tt_seq_show,
345 };
346 
347 DEFINE_SEQ_ATTRIBUTE(tt);
348 
349 static int cdev_dt_seq_show(struct seq_file *s, void *v)
350 {
351 	struct thermal_debugfs *thermal_dbg = s->private;
352 	struct cdev_debugfs *cdev_dbg = &thermal_dbg->cdev_dbg;
353 	struct list_head *durations = cdev_dbg->durations;
354 	struct cdev_record *entry;
355 	int i = *(loff_t *)v;
356 
357 	if (!i)
358 		seq_puts(s, "State\tResidency\n");
359 
360 	list_for_each_entry(entry, &durations[i], node) {
361 		s64 duration = ktime_to_ms(entry->residency);
362 
363 		if (entry->id == cdev_dbg->current_state)
364 			duration += ktime_ms_delta(ktime_get(),
365 						   cdev_dbg->timestamp);
366 
367 		seq_printf(s, "%-5d\t%-10llu\n", entry->id, duration);
368 	}
369 
370 	return 0;
371 }
372 
373 static const struct seq_operations dt_sops = {
374 	.start = cdev_seq_start,
375 	.next = cdev_seq_next,
376 	.stop = cdev_seq_stop,
377 	.show = cdev_dt_seq_show,
378 };
379 
380 DEFINE_SEQ_ATTRIBUTE(dt);
381 
382 static int cdev_clear_set(void *data, u64 val)
383 {
384 	struct thermal_debugfs *thermal_dbg = data;
385 
386 	if (!val)
387 		return -EINVAL;
388 
389 	mutex_lock(&thermal_dbg->lock);
390 
391 	thermal_debugfs_cdev_clear(&thermal_dbg->cdev_dbg);
392 
393 	mutex_unlock(&thermal_dbg->lock);
394 
395 	return 0;
396 }
397 
398 DEFINE_DEBUGFS_ATTRIBUTE(cdev_clear_fops, NULL, cdev_clear_set, "%llu\n");
399 
400 /**
401  * thermal_debug_cdev_state_update - Update a cooling device state change
402  *
403  * Computes a transition and the duration of the previous state residency.
404  *
405  * @cdev : a pointer to a cooling device
406  * @new_state: an integer corresponding to the new cooling device state
407  */
408 void thermal_debug_cdev_state_update(const struct thermal_cooling_device *cdev,
409 				     int new_state)
410 {
411 	struct thermal_debugfs *thermal_dbg = cdev->debugfs;
412 	struct cdev_debugfs *cdev_dbg;
413 	struct cdev_record *cdev_record;
414 	int transition, old_state;
415 
416 	if (!thermal_dbg || (thermal_dbg->cdev_dbg.current_state == new_state))
417 		return;
418 
419 	mutex_lock(&thermal_dbg->lock);
420 
421 	cdev_dbg = &thermal_dbg->cdev_dbg;
422 
423 	old_state = cdev_dbg->current_state;
424 
425 	/*
426 	 * Get the old state information in the durations list. If
427 	 * this one does not exist, a new allocated one will be
428 	 * returned. Recompute the total duration in the old state and
429 	 * get a new timestamp for the new state.
430 	 */
431 	cdev_record = thermal_debugfs_cdev_record_get(thermal_dbg,
432 						      cdev_dbg->durations,
433 						      old_state);
434 	if (cdev_record) {
435 		ktime_t now = ktime_get();
436 		ktime_t delta = ktime_sub(now, cdev_dbg->timestamp);
437 		cdev_record->residency = ktime_add(cdev_record->residency, delta);
438 		cdev_dbg->timestamp = now;
439 	}
440 
441 	cdev_dbg->current_state = new_state;
442 
443 	/*
444 	 * Create a record for the new state if it is not there, so its
445 	 * duration will be printed by cdev_dt_seq_show() as expected if it
446 	 * runs before the next state transition.
447 	 */
448 	thermal_debugfs_cdev_record_get(thermal_dbg, cdev_dbg->durations, new_state);
449 
450 	transition = (old_state << 16) | new_state;
451 
452 	/*
453 	 * Get the transition in the transitions list. If this one
454 	 * does not exist, a new allocated one will be returned.
455 	 * Increment the occurrence of this transition which is stored
456 	 * in the value field.
457 	 */
458 	cdev_record = thermal_debugfs_cdev_record_get(thermal_dbg,
459 						      cdev_dbg->transitions,
460 						      transition);
461 	if (cdev_record)
462 		cdev_record->count++;
463 
464 	cdev_dbg->total++;
465 
466 	mutex_unlock(&thermal_dbg->lock);
467 }
468 
469 /**
470  * thermal_debug_cdev_add - Add a cooling device debugfs entry
471  *
472  * Allocates a cooling device object for debug, initializes the
473  * statistics and create the entries in sysfs.
474  * @cdev: a pointer to a cooling device
475  * @state: current state of the cooling device
476  */
477 void thermal_debug_cdev_add(struct thermal_cooling_device *cdev, int state)
478 {
479 	struct thermal_debugfs *thermal_dbg;
480 	struct cdev_debugfs *cdev_dbg;
481 	int i;
482 
483 	thermal_dbg = thermal_debugfs_add_id(d_cdev, cdev->id);
484 	if (!thermal_dbg)
485 		return;
486 
487 	cdev_dbg = &thermal_dbg->cdev_dbg;
488 
489 	for (i = 0; i < CDEVSTATS_HASH_SIZE; i++) {
490 		INIT_LIST_HEAD(&cdev_dbg->transitions[i]);
491 		INIT_LIST_HEAD(&cdev_dbg->durations[i]);
492 	}
493 
494 	cdev_dbg->current_state = state;
495 	cdev_dbg->timestamp = ktime_get();
496 
497 	/*
498 	 * Create a record for the initial cooling device state, so its
499 	 * duration will be printed by cdev_dt_seq_show() as expected if it
500 	 * runs before the first state transition.
501 	 */
502 	thermal_debugfs_cdev_record_get(thermal_dbg, cdev_dbg->durations, state);
503 
504 	debugfs_create_file("trans_table", 0400, thermal_dbg->d_top,
505 			    thermal_dbg, &tt_fops);
506 
507 	debugfs_create_file("time_in_state_ms", 0400, thermal_dbg->d_top,
508 			    thermal_dbg, &dt_fops);
509 
510 	debugfs_create_file("clear", 0200, thermal_dbg->d_top,
511 			    thermal_dbg, &cdev_clear_fops);
512 
513 	debugfs_create_u32("total_trans", 0400, thermal_dbg->d_top,
514 			   &cdev_dbg->total);
515 
516 	cdev->debugfs = thermal_dbg;
517 }
518 
519 /**
520  * thermal_debug_cdev_remove - Remove a cooling device debugfs entry
521  *
522  * Frees the statistics memory data and remove the debugfs entry
523  *
524  * @cdev: a pointer to a cooling device
525  */
526 void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev)
527 {
528 	struct thermal_debugfs *thermal_dbg;
529 
530 	mutex_lock(&cdev->lock);
531 
532 	thermal_dbg = cdev->debugfs;
533 	if (!thermal_dbg) {
534 		mutex_unlock(&cdev->lock);
535 		return;
536 	}
537 
538 	cdev->debugfs = NULL;
539 
540 	mutex_unlock(&cdev->lock);
541 
542 	mutex_lock(&thermal_dbg->lock);
543 
544 	thermal_debugfs_cdev_clear(&thermal_dbg->cdev_dbg);
545 
546 	mutex_unlock(&thermal_dbg->lock);
547 
548 	thermal_debugfs_remove_id(thermal_dbg);
549 }
550 
551 static struct tz_episode *thermal_debugfs_tz_event_alloc(struct thermal_zone_device *tz,
552 							ktime_t now)
553 {
554 	struct tz_episode *tze;
555 	int i;
556 
557 	tze = kzalloc(struct_size(tze, trip_stats, tz->num_trips), GFP_KERNEL);
558 	if (!tze)
559 		return NULL;
560 
561 	INIT_LIST_HEAD(&tze->node);
562 	tze->timestamp = now;
563 	tze->duration = KTIME_MIN;
564 
565 	for (i = 0; i < tz->num_trips; i++) {
566 		tze->trip_stats[i].min = INT_MAX;
567 		tze->trip_stats[i].max = INT_MIN;
568 	}
569 
570 	return tze;
571 }
572 
573 void thermal_debug_tz_trip_up(struct thermal_zone_device *tz,
574 			      const struct thermal_trip *trip)
575 {
576 	struct tz_episode *tze;
577 	struct tz_debugfs *tz_dbg;
578 	struct thermal_debugfs *thermal_dbg = tz->debugfs;
579 	int trip_id = thermal_zone_trip_id(tz, trip);
580 	ktime_t now = ktime_get();
581 	struct trip_stats *trip_stats;
582 
583 	if (!thermal_dbg)
584 		return;
585 
586 	mutex_lock(&thermal_dbg->lock);
587 
588 	tz_dbg = &thermal_dbg->tz_dbg;
589 
590 	/*
591 	 * The mitigation is starting. A mitigation can contain
592 	 * several episodes where each of them is related to a
593 	 * temperature crossing a trip point. The episodes are
594 	 * nested. That means when the temperature is crossing the
595 	 * first trip point, the duration begins to be measured. If
596 	 * the temperature continues to increase and reaches the
597 	 * second trip point, the duration of the first trip must be
598 	 * also accumulated.
599 	 *
600 	 * eg.
601 	 *
602 	 * temp
603 	 *   ^
604 	 *   |             --------
605 	 * trip 2         /        \         ------
606 	 *   |           /|        |\      /|      |\
607 	 * trip 1       / |        | `----  |      | \
608 	 *   |         /| |        |        |      | |\
609 	 * trip 0     / | |        |        |      | | \
610 	 *   |       /| | |        |        |      | | |\
611 	 *   |      / | | |        |        |      | | | `--
612 	 *   |     /  | | |        |        |      | | |
613 	 *   |-----   | | |        |        |      | | |
614 	 *   |        | | |        |        |      | | |
615 	 *    --------|-|-|--------|--------|------|-|-|------------------> time
616 	 *            | | |<--t2-->|        |<-t2'>| | |
617 	 *            | |                            | |
618 	 *            | |<------------t1------------>| |
619 	 *            |                                |
620 	 *            |<-------------t0--------------->|
621 	 *
622 	 */
623 	if (!tz_dbg->nr_trips) {
624 		tze = thermal_debugfs_tz_event_alloc(tz, now);
625 		if (!tze)
626 			goto unlock;
627 
628 		list_add(&tze->node, &tz_dbg->tz_episodes);
629 	}
630 
631 	/*
632 	 * Each time a trip point is crossed the way up, the trip_id
633 	 * is stored in the trip_crossed array and the nr_trips is
634 	 * incremented. A nr_trips equal to zero means we are entering
635 	 * a mitigation episode.
636 	 *
637 	 * The trip ids may not be in the ascending order but the
638 	 * result in the array trips_crossed will be in the ascending
639 	 * temperature order. The function detecting when a trip point
640 	 * is crossed the way down will handle the very rare case when
641 	 * the trip points may have been reordered during this
642 	 * mitigation episode.
643 	 */
644 	tz_dbg->trips_crossed[tz_dbg->nr_trips++] = trip_id;
645 
646 	tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
647 	trip_stats = &tze->trip_stats[trip_id];
648 	trip_stats->trip_temp = trip->temperature;
649 	trip_stats->trip_hyst = trip->hysteresis;
650 	trip_stats->timestamp = now;
651 
652 unlock:
653 	mutex_unlock(&thermal_dbg->lock);
654 }
655 
656 void thermal_debug_tz_trip_down(struct thermal_zone_device *tz,
657 				const struct thermal_trip *trip)
658 {
659 	struct thermal_debugfs *thermal_dbg = tz->debugfs;
660 	struct tz_episode *tze;
661 	struct tz_debugfs *tz_dbg;
662 	ktime_t delta, now = ktime_get();
663 	int trip_id = thermal_zone_trip_id(tz, trip);
664 	int i;
665 
666 	if (!thermal_dbg)
667 		return;
668 
669 	mutex_lock(&thermal_dbg->lock);
670 
671 	tz_dbg = &thermal_dbg->tz_dbg;
672 
673 	/*
674 	 * The temperature crosses the way down but there was not
675 	 * mitigation detected before. That may happen when the
676 	 * temperature is greater than a trip point when registering a
677 	 * thermal zone, which is a common use case as the kernel has
678 	 * no mitigation mechanism yet at boot time.
679 	 */
680 	if (!tz_dbg->nr_trips)
681 		goto out;
682 
683 	for (i = tz_dbg->nr_trips - 1; i >= 0; i--) {
684 		if (tz_dbg->trips_crossed[i] == trip_id)
685 			break;
686 	}
687 
688 	if (i < 0)
689 		goto out;
690 
691 	tz_dbg->nr_trips--;
692 
693 	if (i < tz_dbg->nr_trips)
694 		tz_dbg->trips_crossed[i] = tz_dbg->trips_crossed[tz_dbg->nr_trips];
695 
696 	tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
697 
698 	delta = ktime_sub(now, tze->trip_stats[trip_id].timestamp);
699 
700 	tze->trip_stats[trip_id].duration =
701 		ktime_add(delta, tze->trip_stats[trip_id].duration);
702 
703 	/* Mark the end of mitigation for this trip point. */
704 	tze->trip_stats[trip_id].timestamp = KTIME_MAX;
705 
706 	/*
707 	 * This event closes the mitigation as we are crossing the
708 	 * last trip point the way down.
709 	 */
710 	if (!tz_dbg->nr_trips)
711 		tze->duration = ktime_sub(now, tze->timestamp);
712 
713 out:
714 	mutex_unlock(&thermal_dbg->lock);
715 }
716 
717 void thermal_debug_update_trip_stats(struct thermal_zone_device *tz)
718 {
719 	struct thermal_debugfs *thermal_dbg = tz->debugfs;
720 	struct tz_debugfs *tz_dbg;
721 	struct tz_episode *tze;
722 	int i;
723 
724 	if (!thermal_dbg)
725 		return;
726 
727 	mutex_lock(&thermal_dbg->lock);
728 
729 	tz_dbg = &thermal_dbg->tz_dbg;
730 
731 	if (!tz_dbg->nr_trips)
732 		goto out;
733 
734 	tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
735 
736 	for (i = 0; i < tz_dbg->nr_trips; i++) {
737 		int trip_id = tz_dbg->trips_crossed[i];
738 		struct trip_stats *trip_stats = &tze->trip_stats[trip_id];
739 
740 		trip_stats->max = max(trip_stats->max, tz->temperature);
741 		trip_stats->min = min(trip_stats->min, tz->temperature);
742 		trip_stats->avg += (tz->temperature - trip_stats->avg) /
743 					++trip_stats->count;
744 	}
745 out:
746 	mutex_unlock(&thermal_dbg->lock);
747 }
748 
749 static void *tze_seq_start(struct seq_file *s, loff_t *pos)
750 {
751 	struct thermal_debugfs *thermal_dbg = s->private;
752 	struct tz_debugfs *tz_dbg = &thermal_dbg->tz_dbg;
753 
754 	mutex_lock(&thermal_dbg->lock);
755 
756 	return seq_list_start(&tz_dbg->tz_episodes, *pos);
757 }
758 
759 static void *tze_seq_next(struct seq_file *s, void *v, loff_t *pos)
760 {
761 	struct thermal_debugfs *thermal_dbg = s->private;
762 	struct tz_debugfs *tz_dbg = &thermal_dbg->tz_dbg;
763 
764 	return seq_list_next(v, &tz_dbg->tz_episodes, pos);
765 }
766 
767 static void tze_seq_stop(struct seq_file *s, void *v)
768 {
769 	struct thermal_debugfs *thermal_dbg = s->private;
770 
771 	mutex_unlock(&thermal_dbg->lock);
772 }
773 
774 static int tze_seq_show(struct seq_file *s, void *v)
775 {
776 	struct thermal_debugfs *thermal_dbg = s->private;
777 	struct thermal_zone_device *tz = thermal_dbg->tz_dbg.tz;
778 	struct thermal_trip_desc *td;
779 	struct tz_episode *tze;
780 	const char *type;
781 	u64 duration_ms;
782 	int trip_id;
783 	char c;
784 
785 	tze = list_entry((struct list_head *)v, struct tz_episode, node);
786 
787 	if (tze->duration == KTIME_MIN) {
788 		/* Mitigation in progress. */
789 		duration_ms = ktime_to_ms(ktime_sub(ktime_get(), tze->timestamp));
790 		c = '>';
791 	} else {
792 		duration_ms = ktime_to_ms(tze->duration);
793 		c = '=';
794 	}
795 
796 	seq_printf(s, ",-Mitigation at %lluus, duration%c%llums\n",
797 		   ktime_to_us(tze->timestamp), c, duration_ms);
798 
799 	seq_printf(s, "| trip |     type | temp(°mC) | hyst(°mC) |  duration   |  avg(°mC) |  min(°mC) |  max(°mC) |\n");
800 
801 	for_each_trip_desc(tz, td) {
802 		const struct thermal_trip *trip = &td->trip;
803 		struct trip_stats *trip_stats;
804 
805 		/*
806 		 * There is no possible mitigation happening at the
807 		 * critical trip point, so the stats will be always
808 		 * zero, skip this trip point
809 		 */
810 		if (trip->type == THERMAL_TRIP_CRITICAL)
811 			continue;
812 
813 		trip_id = thermal_zone_trip_id(tz, trip);
814 		trip_stats = &tze->trip_stats[trip_id];
815 
816 		/* Skip trips without any stats. */
817 		if (trip_stats->min > trip_stats->max)
818 			continue;
819 
820 		if (trip->type == THERMAL_TRIP_PASSIVE)
821 			type = "passive";
822 		else if (trip->type == THERMAL_TRIP_ACTIVE)
823 			type = "active";
824 		else
825 			type = "hot";
826 
827 		if (trip_stats->timestamp != KTIME_MAX) {
828 			/* Mitigation in progress. */
829 			ktime_t delta = ktime_sub(ktime_get(),
830 						  trip_stats->timestamp);
831 
832 			delta = ktime_add(delta, trip_stats->duration);
833 			duration_ms = ktime_to_ms(delta);
834 			c = '>';
835 		} else {
836 			duration_ms = ktime_to_ms(trip_stats->duration);
837 			c = ' ';
838 		}
839 
840 		seq_printf(s, "| %*d | %*s | %*d | %*d | %c%*lld | %*d | %*d | %*d |\n",
841 			   4 , trip_id,
842 			   8, type,
843 			   9, trip_stats->trip_temp,
844 			   9, trip_stats->trip_hyst,
845 			   c, 10, duration_ms,
846 			   9, trip_stats->avg,
847 			   9, trip_stats->min,
848 			   9, trip_stats->max);
849 	}
850 
851 	return 0;
852 }
853 
854 static const struct seq_operations tze_sops = {
855 	.start = tze_seq_start,
856 	.next = tze_seq_next,
857 	.stop = tze_seq_stop,
858 	.show = tze_seq_show,
859 };
860 
861 DEFINE_SEQ_ATTRIBUTE(tze);
862 
863 void thermal_debug_tz_add(struct thermal_zone_device *tz)
864 {
865 	struct thermal_debugfs *thermal_dbg;
866 	struct tz_debugfs *tz_dbg;
867 
868 	thermal_dbg = thermal_debugfs_add_id(d_tz, tz->id);
869 	if (!thermal_dbg)
870 		return;
871 
872 	tz_dbg = &thermal_dbg->tz_dbg;
873 
874 	tz_dbg->tz = tz;
875 
876 	tz_dbg->trips_crossed = kzalloc(sizeof(int) * tz->num_trips, GFP_KERNEL);
877 	if (!tz_dbg->trips_crossed) {
878 		thermal_debugfs_remove_id(thermal_dbg);
879 		return;
880 	}
881 
882 	INIT_LIST_HEAD(&tz_dbg->tz_episodes);
883 
884 	debugfs_create_file("mitigations", 0400, thermal_dbg->d_top,
885 			    thermal_dbg, &tze_fops);
886 
887 	tz->debugfs = thermal_dbg;
888 }
889 
890 void thermal_debug_tz_remove(struct thermal_zone_device *tz)
891 {
892 	struct thermal_debugfs *thermal_dbg;
893 	struct tz_episode *tze, *tmp;
894 	struct tz_debugfs *tz_dbg;
895 	int *trips_crossed;
896 
897 	mutex_lock(&tz->lock);
898 
899 	thermal_dbg = tz->debugfs;
900 	if (!thermal_dbg) {
901 		mutex_unlock(&tz->lock);
902 		return;
903 	}
904 
905 	tz->debugfs = NULL;
906 
907 	mutex_unlock(&tz->lock);
908 
909 	tz_dbg = &thermal_dbg->tz_dbg;
910 
911 	mutex_lock(&thermal_dbg->lock);
912 
913 	trips_crossed = tz_dbg->trips_crossed;
914 
915 	list_for_each_entry_safe(tze, tmp, &tz_dbg->tz_episodes, node) {
916 		list_del(&tze->node);
917 		kfree(tze);
918 	}
919 
920 	mutex_unlock(&thermal_dbg->lock);
921 
922 	thermal_debugfs_remove_id(thermal_dbg);
923 	kfree(trips_crossed);
924 }
925