1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2023 Linaro Limited
5 * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
7 * Thermal subsystem debug support
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>
16 #include "thermal_core.h"
18 static struct dentry *d_root;
19 static struct dentry *d_cdev;
20 static struct dentry *d_tz;
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.
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.
37 #define CDEVSTATS_HASH_SIZE 16
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
47 * Every transition updates the current_state and the timestamp. The
48 * transitions and the durations are stored in lists.
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
60 struct list_head transitions[CDEVSTATS_HASH_SIZE];
61 struct list_head durations[CDEVSTATS_HASH_SIZE];
65 * struct cdev_record - Common structure for cooling device entry
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.
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
78 struct list_head node;
87 * struct trip_stats - Thermal trip statistics
89 * The trip_stats structure has the relevant information to show the
90 * statistics related to temperature going above a trip point.
92 * @timestamp: the trip crossing timestamp
93 * @duration: total time when the zone temperature was above the trip point
94 * @count: the number of times the zone temperature was above the trip point
95 * @max: maximum recorded temperature above the trip point
96 * @min: minimum recorded temperature above the trip point
97 * @avg: average temperature above the trip point
109 * struct tz_episode - A mitigation episode information
111 * The tz_episode structure describes a mitigation episode. A
112 * mitigation episode begins the trip point with the lower temperature
113 * is crossed the way up and ends when it is crossed the way
114 * down. During this episode we can have multiple trip points crossed
115 * the way up and down if there are multiple trip described in the
116 * firmware after the lowest temperature trip point.
118 * @timestamp: first trip point crossed the way up
119 * @duration: total duration of the mitigation episode
120 * @node: a list element to be added to the list of tz events
121 * @trip_stats: per trip point statistics, flexible array
126 struct list_head node;
127 struct trip_stats trip_stats[];
131 * struct tz_debugfs - Store all mitigation episodes for a thermal zone
133 * The tz_debugfs structure contains the list of the mitigation
134 * episodes and has to track which trip point has been crossed in
135 * order to handle correctly nested trip point mitigation episodes.
137 * We keep the history of the trip point crossed in an array and as we
138 * can go back and forth inside this history, eg. trip 0,1,2,1,2,1,0,
139 * we keep track of the current position in the history array.
141 * @tz_episodes: a list of thermal mitigation episodes
142 * @trips_crossed: an array of trip points crossed by id
143 * @nr_trips: the number of trip points currently being crossed
146 struct list_head tz_episodes;
152 * struct thermal_debugfs - High level structure for a thermal object in debugfs
154 * The thermal_debugfs structure is the common structure used by the
155 * cooling device or the thermal zone to store the statistics.
157 * @d_top: top directory of the thermal object directory
158 * @lock: per object lock to protect the internals
160 * @cdev_dbg: a cooling device debug structure
161 * @tz_dbg: a thermal zone debug structure
163 struct thermal_debugfs {
164 struct dentry *d_top;
167 struct cdev_debugfs cdev_dbg;
168 struct tz_debugfs tz_dbg;
172 void thermal_debug_init(void)
174 d_root = debugfs_create_dir("thermal", NULL);
178 d_cdev = debugfs_create_dir("cooling_devices", d_root);
182 d_tz = debugfs_create_dir("thermal_zones", d_root);
185 static struct thermal_debugfs *thermal_debugfs_add_id(struct dentry *d, int id)
187 struct thermal_debugfs *thermal_dbg;
190 thermal_dbg = kzalloc(sizeof(*thermal_dbg), GFP_KERNEL);
194 mutex_init(&thermal_dbg->lock);
196 snprintf(ids, IDSLENGTH, "%d", id);
198 thermal_dbg->d_top = debugfs_create_dir(ids, d);
199 if (!thermal_dbg->d_top) {
207 static void thermal_debugfs_remove_id(struct thermal_debugfs *thermal_dbg)
212 debugfs_remove(thermal_dbg->d_top);
217 static struct cdev_record *
218 thermal_debugfs_cdev_record_alloc(struct thermal_debugfs *thermal_dbg,
219 struct list_head *lists, int id)
221 struct cdev_record *cdev_record;
223 cdev_record = kzalloc(sizeof(*cdev_record), GFP_KERNEL);
227 cdev_record->id = id;
228 INIT_LIST_HEAD(&cdev_record->node);
229 list_add_tail(&cdev_record->node,
230 &lists[cdev_record->id % CDEVSTATS_HASH_SIZE]);
235 static struct cdev_record *
236 thermal_debugfs_cdev_record_find(struct thermal_debugfs *thermal_dbg,
237 struct list_head *lists, int id)
239 struct cdev_record *entry;
241 list_for_each_entry(entry, &lists[id % CDEVSTATS_HASH_SIZE], node)
248 static struct cdev_record *
249 thermal_debugfs_cdev_record_get(struct thermal_debugfs *thermal_dbg,
250 struct list_head *lists, int id)
252 struct cdev_record *cdev_record;
254 cdev_record = thermal_debugfs_cdev_record_find(thermal_dbg, lists, id);
258 return thermal_debugfs_cdev_record_alloc(thermal_dbg, lists, id);
261 static void thermal_debugfs_cdev_clear(struct cdev_debugfs *cdev_dbg)
264 struct cdev_record *entry, *tmp;
266 for (i = 0; i < CDEVSTATS_HASH_SIZE; i++) {
268 list_for_each_entry_safe(entry, tmp,
269 &cdev_dbg->transitions[i], node) {
270 list_del(&entry->node);
274 list_for_each_entry_safe(entry, tmp,
275 &cdev_dbg->durations[i], node) {
276 list_del(&entry->node);
284 static void *cdev_seq_start(struct seq_file *s, loff_t *pos)
286 struct thermal_debugfs *thermal_dbg = s->private;
288 mutex_lock(&thermal_dbg->lock);
290 return (*pos < CDEVSTATS_HASH_SIZE) ? pos : NULL;
293 static void *cdev_seq_next(struct seq_file *s, void *v, loff_t *pos)
297 return (*pos < CDEVSTATS_HASH_SIZE) ? pos : NULL;
300 static void cdev_seq_stop(struct seq_file *s, void *v)
302 struct thermal_debugfs *thermal_dbg = s->private;
304 mutex_unlock(&thermal_dbg->lock);
307 static int cdev_tt_seq_show(struct seq_file *s, void *v)
309 struct thermal_debugfs *thermal_dbg = s->private;
310 struct cdev_debugfs *cdev_dbg = &thermal_dbg->cdev_dbg;
311 struct list_head *transitions = cdev_dbg->transitions;
312 struct cdev_record *entry;
313 int i = *(loff_t *)v;
316 seq_puts(s, "Transition\tOccurences\n");
318 list_for_each_entry(entry, &transitions[i], node) {
320 * Assuming maximum cdev states is 1024, the longer
321 * string for a transition would be "1024->1024\0"
325 snprintf(buffer, ARRAY_SIZE(buffer), "%d->%d",
326 entry->id >> 16, entry->id & 0xFFFF);
328 seq_printf(s, "%-10s\t%-10llu\n", buffer, entry->count);
334 static const struct seq_operations tt_sops = {
335 .start = cdev_seq_start,
336 .next = cdev_seq_next,
337 .stop = cdev_seq_stop,
338 .show = cdev_tt_seq_show,
341 DEFINE_SEQ_ATTRIBUTE(tt);
343 static int cdev_dt_seq_show(struct seq_file *s, void *v)
345 struct thermal_debugfs *thermal_dbg = s->private;
346 struct cdev_debugfs *cdev_dbg = &thermal_dbg->cdev_dbg;
347 struct list_head *durations = cdev_dbg->durations;
348 struct cdev_record *entry;
349 int i = *(loff_t *)v;
352 seq_puts(s, "State\tResidency\n");
354 list_for_each_entry(entry, &durations[i], node) {
355 s64 duration = ktime_to_ms(entry->residency);
357 if (entry->id == cdev_dbg->current_state)
358 duration += ktime_ms_delta(ktime_get(),
359 cdev_dbg->timestamp);
361 seq_printf(s, "%-5d\t%-10llu\n", entry->id, duration);
367 static const struct seq_operations dt_sops = {
368 .start = cdev_seq_start,
369 .next = cdev_seq_next,
370 .stop = cdev_seq_stop,
371 .show = cdev_dt_seq_show,
374 DEFINE_SEQ_ATTRIBUTE(dt);
376 static int cdev_clear_set(void *data, u64 val)
378 struct thermal_debugfs *thermal_dbg = data;
383 mutex_lock(&thermal_dbg->lock);
385 thermal_debugfs_cdev_clear(&thermal_dbg->cdev_dbg);
387 mutex_unlock(&thermal_dbg->lock);
392 DEFINE_DEBUGFS_ATTRIBUTE(cdev_clear_fops, NULL, cdev_clear_set, "%llu\n");
395 * thermal_debug_cdev_state_update - Update a cooling device state change
397 * Computes a transition and the duration of the previous state residency.
399 * @cdev : a pointer to a cooling device
400 * @new_state: an integer corresponding to the new cooling device state
402 void thermal_debug_cdev_state_update(const struct thermal_cooling_device *cdev,
405 struct thermal_debugfs *thermal_dbg = cdev->debugfs;
406 struct cdev_debugfs *cdev_dbg;
407 struct cdev_record *cdev_record;
408 int transition, old_state;
410 if (!thermal_dbg || (thermal_dbg->cdev_dbg.current_state == new_state))
413 mutex_lock(&thermal_dbg->lock);
415 cdev_dbg = &thermal_dbg->cdev_dbg;
417 old_state = cdev_dbg->current_state;
420 * Get the old state information in the durations list. If
421 * this one does not exist, a new allocated one will be
422 * returned. Recompute the total duration in the old state and
423 * get a new timestamp for the new state.
425 cdev_record = thermal_debugfs_cdev_record_get(thermal_dbg,
429 ktime_t now = ktime_get();
430 ktime_t delta = ktime_sub(now, cdev_dbg->timestamp);
431 cdev_record->residency = ktime_add(cdev_record->residency, delta);
432 cdev_dbg->timestamp = now;
435 cdev_dbg->current_state = new_state;
436 transition = (old_state << 16) | new_state;
439 * Get the transition in the transitions list. If this one
440 * does not exist, a new allocated one will be returned.
441 * Increment the occurrence of this transition which is stored
442 * in the value field.
444 cdev_record = thermal_debugfs_cdev_record_get(thermal_dbg,
445 cdev_dbg->transitions,
448 cdev_record->count++;
452 mutex_unlock(&thermal_dbg->lock);
456 * thermal_debug_cdev_add - Add a cooling device debugfs entry
458 * Allocates a cooling device object for debug, initializes the
459 * statistics and create the entries in sysfs.
460 * @cdev: a pointer to a cooling device
462 void thermal_debug_cdev_add(struct thermal_cooling_device *cdev)
464 struct thermal_debugfs *thermal_dbg;
465 struct cdev_debugfs *cdev_dbg;
468 thermal_dbg = thermal_debugfs_add_id(d_cdev, cdev->id);
472 cdev_dbg = &thermal_dbg->cdev_dbg;
474 for (i = 0; i < CDEVSTATS_HASH_SIZE; i++) {
475 INIT_LIST_HEAD(&cdev_dbg->transitions[i]);
476 INIT_LIST_HEAD(&cdev_dbg->durations[i]);
479 cdev_dbg->current_state = 0;
480 cdev_dbg->timestamp = ktime_get();
482 debugfs_create_file("trans_table", 0400, thermal_dbg->d_top,
483 thermal_dbg, &tt_fops);
485 debugfs_create_file("time_in_state_ms", 0400, thermal_dbg->d_top,
486 thermal_dbg, &dt_fops);
488 debugfs_create_file("clear", 0200, thermal_dbg->d_top,
489 thermal_dbg, &cdev_clear_fops);
491 debugfs_create_u32("total_trans", 0400, thermal_dbg->d_top,
494 cdev->debugfs = thermal_dbg;
498 * thermal_debug_cdev_remove - Remove a cooling device debugfs entry
500 * Frees the statistics memory data and remove the debugfs entry
502 * @cdev: a pointer to a cooling device
504 void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev)
506 struct thermal_debugfs *thermal_dbg = cdev->debugfs;
511 mutex_lock(&thermal_dbg->lock);
513 thermal_debugfs_cdev_clear(&thermal_dbg->cdev_dbg);
514 cdev->debugfs = NULL;
516 mutex_unlock(&thermal_dbg->lock);
518 thermal_debugfs_remove_id(thermal_dbg);
521 static struct tz_episode *thermal_debugfs_tz_event_alloc(struct thermal_zone_device *tz,
524 struct tz_episode *tze;
527 tze = kzalloc(struct_size(tze, trip_stats, tz->num_trips), GFP_KERNEL);
531 INIT_LIST_HEAD(&tze->node);
532 tze->timestamp = now;
534 for (i = 0; i < tz->num_trips; i++) {
535 tze->trip_stats[i].min = INT_MAX;
536 tze->trip_stats[i].max = INT_MIN;
542 void thermal_debug_tz_trip_up(struct thermal_zone_device *tz,
543 const struct thermal_trip *trip)
545 struct tz_episode *tze;
546 struct tz_debugfs *tz_dbg;
547 struct thermal_debugfs *thermal_dbg = tz->debugfs;
548 int temperature = tz->temperature;
549 int trip_id = thermal_zone_trip_id(tz, trip);
550 ktime_t now = ktime_get();
555 mutex_lock(&thermal_dbg->lock);
557 tz_dbg = &thermal_dbg->tz_dbg;
560 * The mitigation is starting. A mitigation can contain
561 * several episodes where each of them is related to a
562 * temperature crossing a trip point. The episodes are
563 * nested. That means when the temperature is crossing the
564 * first trip point, the duration begins to be measured. If
565 * the temperature continues to increase and reaches the
566 * second trip point, the duration of the first trip must be
576 * trip 1 / | | `---- | | \
578 * trip 0 / | | | | | | \
579 * | /| | | | | | | |\
580 * | / | | | | | | | | `--
581 * | / | | | | | | | |
582 * |----- | | | | | | | |
584 * --------|-|-|--------|--------|------|-|-|------------------> time
585 * | | |<--t2-->| |<-t2'>| | |
587 * | |<------------t1------------>| |
589 * |<-------------t0--------------->|
592 if (!tz_dbg->nr_trips) {
593 tze = thermal_debugfs_tz_event_alloc(tz, now);
597 list_add(&tze->node, &tz_dbg->tz_episodes);
601 * Each time a trip point is crossed the way up, the trip_id
602 * is stored in the trip_crossed array and the nr_trips is
603 * incremented. A nr_trips equal to zero means we are entering
604 * a mitigation episode.
606 * The trip ids may not be in the ascending order but the
607 * result in the array trips_crossed will be in the ascending
608 * temperature order. The function detecting when a trip point
609 * is crossed the way down will handle the very rare case when
610 * the trip points may have been reordered during this
611 * mitigation episode.
613 tz_dbg->trips_crossed[tz_dbg->nr_trips++] = trip_id;
615 tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
616 tze->trip_stats[trip_id].timestamp = now;
617 tze->trip_stats[trip_id].max = max(tze->trip_stats[trip_id].max, temperature);
618 tze->trip_stats[trip_id].min = min(tze->trip_stats[trip_id].min, temperature);
619 tze->trip_stats[trip_id].count++;
620 tze->trip_stats[trip_id].avg = tze->trip_stats[trip_id].avg +
621 (temperature - tze->trip_stats[trip_id].avg) /
622 tze->trip_stats[trip_id].count;
625 mutex_unlock(&thermal_dbg->lock);
628 void thermal_debug_tz_trip_down(struct thermal_zone_device *tz,
629 const struct thermal_trip *trip)
631 struct thermal_debugfs *thermal_dbg = tz->debugfs;
632 struct tz_episode *tze;
633 struct tz_debugfs *tz_dbg;
634 ktime_t delta, now = ktime_get();
635 int trip_id = thermal_zone_trip_id(tz, trip);
641 mutex_lock(&thermal_dbg->lock);
643 tz_dbg = &thermal_dbg->tz_dbg;
646 * The temperature crosses the way down but there was not
647 * mitigation detected before. That may happen when the
648 * temperature is greater than a trip point when registering a
649 * thermal zone, which is a common use case as the kernel has
650 * no mitigation mechanism yet at boot time.
652 if (!tz_dbg->nr_trips)
655 for (i = tz_dbg->nr_trips - 1; i >= 0; i--) {
656 if (tz_dbg->trips_crossed[i] == trip_id)
665 if (i < tz_dbg->nr_trips)
666 tz_dbg->trips_crossed[i] = tz_dbg->trips_crossed[tz_dbg->nr_trips];
668 tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
670 delta = ktime_sub(now, tze->trip_stats[trip_id].timestamp);
672 tze->trip_stats[trip_id].duration =
673 ktime_add(delta, tze->trip_stats[trip_id].duration);
676 * This event closes the mitigation as we are crossing the
677 * last trip point the way down.
679 if (!tz_dbg->nr_trips)
680 tze->duration = ktime_sub(now, tze->timestamp);
683 mutex_unlock(&thermal_dbg->lock);
686 void thermal_debug_update_temp(struct thermal_zone_device *tz)
688 struct thermal_debugfs *thermal_dbg = tz->debugfs;
689 struct tz_episode *tze;
690 struct tz_debugfs *tz_dbg;
696 mutex_lock(&thermal_dbg->lock);
698 tz_dbg = &thermal_dbg->tz_dbg;
700 if (!tz_dbg->nr_trips)
703 for (i = 0; i < tz_dbg->nr_trips; i++) {
704 trip_id = tz_dbg->trips_crossed[i];
705 tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
706 tze->trip_stats[trip_id].count++;
707 tze->trip_stats[trip_id].max = max(tze->trip_stats[trip_id].max, tz->temperature);
708 tze->trip_stats[trip_id].min = min(tze->trip_stats[trip_id].min, tz->temperature);
709 tze->trip_stats[trip_id].avg = tze->trip_stats[trip_id].avg +
710 (tz->temperature - tze->trip_stats[trip_id].avg) /
711 tze->trip_stats[trip_id].count;
714 mutex_unlock(&thermal_dbg->lock);
717 static void *tze_seq_start(struct seq_file *s, loff_t *pos)
719 struct thermal_zone_device *tz = s->private;
720 struct thermal_debugfs *thermal_dbg = tz->debugfs;
721 struct tz_debugfs *tz_dbg = &thermal_dbg->tz_dbg;
723 mutex_lock(&thermal_dbg->lock);
725 return seq_list_start(&tz_dbg->tz_episodes, *pos);
728 static void *tze_seq_next(struct seq_file *s, void *v, loff_t *pos)
730 struct thermal_zone_device *tz = s->private;
731 struct thermal_debugfs *thermal_dbg = tz->debugfs;
732 struct tz_debugfs *tz_dbg = &thermal_dbg->tz_dbg;
734 return seq_list_next(v, &tz_dbg->tz_episodes, pos);
737 static void tze_seq_stop(struct seq_file *s, void *v)
739 struct thermal_zone_device *tz = s->private;
740 struct thermal_debugfs *thermal_dbg = tz->debugfs;
742 mutex_unlock(&thermal_dbg->lock);
745 static int tze_seq_show(struct seq_file *s, void *v)
747 struct thermal_zone_device *tz = s->private;
748 struct thermal_trip_desc *td;
749 struct tz_episode *tze;
753 tze = list_entry((struct list_head *)v, struct tz_episode, node);
755 seq_printf(s, ",-Mitigation at %lluus, duration=%llums\n",
756 ktime_to_us(tze->timestamp),
757 ktime_to_ms(tze->duration));
759 seq_printf(s, "| trip | type | temp(°mC) | hyst(°mC) | duration | avg(°mC) | min(°mC) | max(°mC) |\n");
761 for_each_trip_desc(tz, td) {
762 const struct thermal_trip *trip = &td->trip;
765 * There is no possible mitigation happening at the
766 * critical trip point, so the stats will be always
767 * zero, skip this trip point
769 if (trip->type == THERMAL_TRIP_CRITICAL)
772 if (trip->type == THERMAL_TRIP_PASSIVE)
774 else if (trip->type == THERMAL_TRIP_ACTIVE)
779 trip_id = thermal_zone_trip_id(tz, trip);
781 seq_printf(s, "| %*d | %*s | %*d | %*d | %*lld | %*d | %*d | %*d |\n",
784 9, trip->temperature,
786 10, ktime_to_ms(tze->trip_stats[trip_id].duration),
787 9, tze->trip_stats[trip_id].avg,
788 9, tze->trip_stats[trip_id].min,
789 9, tze->trip_stats[trip_id].max);
795 static const struct seq_operations tze_sops = {
796 .start = tze_seq_start,
797 .next = tze_seq_next,
798 .stop = tze_seq_stop,
799 .show = tze_seq_show,
802 DEFINE_SEQ_ATTRIBUTE(tze);
804 void thermal_debug_tz_add(struct thermal_zone_device *tz)
806 struct thermal_debugfs *thermal_dbg;
807 struct tz_debugfs *tz_dbg;
809 thermal_dbg = thermal_debugfs_add_id(d_tz, tz->id);
813 tz_dbg = &thermal_dbg->tz_dbg;
815 tz_dbg->trips_crossed = kzalloc(sizeof(int) * tz->num_trips, GFP_KERNEL);
816 if (!tz_dbg->trips_crossed) {
817 thermal_debugfs_remove_id(thermal_dbg);
821 INIT_LIST_HEAD(&tz_dbg->tz_episodes);
823 debugfs_create_file("mitigations", 0400, thermal_dbg->d_top, tz, &tze_fops);
825 tz->debugfs = thermal_dbg;
828 void thermal_debug_tz_remove(struct thermal_zone_device *tz)
830 struct thermal_debugfs *thermal_dbg = tz->debugfs;
835 mutex_lock(&thermal_dbg->lock);
839 mutex_unlock(&thermal_dbg->lock);
841 thermal_debugfs_remove_id(thermal_dbg);