#include <linux/debugfs.h>
#include <linux/ktime.h>
#include <linux/list.h>
#include <linux/minmax.h>
#include <linux/mutex.h>
#include <linux/thermal.h>
#include "thermal_core.h"
static struct dentry *d_root;
static struct dentry *d_cdev;
static struct dentry *d_tz;
#define IDSLENGTH 4
#define CDEVSTATS_HASH_SIZE 16
struct cdev_debugfs {
u32 total;
int current_state;
ktime_t timestamp;
struct list_head transitions[CDEVSTATS_HASH_SIZE];
struct list_head durations[CDEVSTATS_HASH_SIZE];
};
struct cdev_record {
struct list_head node;
int id;
union {
ktime_t residency;
u64 count;
};
};
struct trip_stats {
ktime_t timestamp;
ktime_t duration;
int trip_temp;
int trip_hyst;
int count;
int min;
int avg;
};
struct tz_episode {
ktime_t timestamp;
ktime_t duration;
struct list_head node;
int max_temp;
struct trip_stats trip_stats[];
};
struct tz_debugfs {
struct list_head tz_episodes;
struct thermal_zone_device *tz;
int *trips_crossed;
int nr_trips;
};
struct thermal_debugfs {
struct dentry *d_top;
struct mutex lock;
union {
struct cdev_debugfs cdev_dbg;
struct tz_debugfs tz_dbg;
};
};
void thermal_debug_init(void)
{
d_root = debugfs_create_dir("thermal", NULL);
if (IS_ERR(d_root))
return;
d_cdev = debugfs_create_dir("cooling_devices", d_root);
if (IS_ERR(d_cdev))
return;
d_tz = debugfs_create_dir("thermal_zones", d_root);
}
static struct thermal_debugfs *thermal_debugfs_add_id(struct dentry *d, int id)
{
struct thermal_debugfs *thermal_dbg;
char ids[IDSLENGTH];
thermal_dbg = kzalloc_obj(*thermal_dbg);
if (!thermal_dbg)
return NULL;
mutex_init(&thermal_dbg->lock);
snprintf(ids, IDSLENGTH, "%d", id);
thermal_dbg->d_top = debugfs_create_dir(ids, d);
if (IS_ERR(thermal_dbg->d_top)) {
kfree(thermal_dbg);
return NULL;
}
return thermal_dbg;
}
static void thermal_debugfs_remove_id(struct thermal_debugfs *thermal_dbg)
{
if (!thermal_dbg)
return;
debugfs_remove(thermal_dbg->d_top);
kfree(thermal_dbg);
}
static struct cdev_record *
thermal_debugfs_cdev_record_alloc(struct thermal_debugfs *thermal_dbg,
struct list_head *lists, int id)
{
struct cdev_record *cdev_record;
cdev_record = kzalloc_obj(*cdev_record);
if (!cdev_record)
return NULL;
cdev_record->id = id;
INIT_LIST_HEAD(&cdev_record->node);
list_add_tail(&cdev_record->node,
&lists[cdev_record->id % CDEVSTATS_HASH_SIZE]);
return cdev_record;
}
static struct cdev_record *
thermal_debugfs_cdev_record_find(struct thermal_debugfs *thermal_dbg,
struct list_head *lists, int id)
{
struct cdev_record *entry;
list_for_each_entry(entry, &lists[id % CDEVSTATS_HASH_SIZE], node)
if (entry->id == id)
return entry;
return NULL;
}
static struct cdev_record *
thermal_debugfs_cdev_record_get(struct thermal_debugfs *thermal_dbg,
struct list_head *lists, int id)
{
struct cdev_record *cdev_record;
cdev_record = thermal_debugfs_cdev_record_find(thermal_dbg, lists, id);
if (cdev_record)
return cdev_record;
return thermal_debugfs_cdev_record_alloc(thermal_dbg, lists, id);
}
static void thermal_debugfs_cdev_clear(struct cdev_debugfs *cdev_dbg)
{
int i;
struct cdev_record *entry, *tmp;
for (i = 0; i < CDEVSTATS_HASH_SIZE; i++) {
list_for_each_entry_safe(entry, tmp,
&cdev_dbg->transitions[i], node) {
list_del(&entry->node);
kfree(entry);
}
list_for_each_entry_safe(entry, tmp,
&cdev_dbg->durations[i], node) {
list_del(&entry->node);
kfree(entry);
}
}
cdev_dbg->total = 0;
}
static void *cdev_seq_start(struct seq_file *s, loff_t *pos)
{
struct thermal_debugfs *thermal_dbg = s->private;
mutex_lock(&thermal_dbg->lock);
return (*pos < CDEVSTATS_HASH_SIZE) ? pos : NULL;
}
static void *cdev_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
(*pos)++;
return (*pos < CDEVSTATS_HASH_SIZE) ? pos : NULL;
}
static void cdev_seq_stop(struct seq_file *s, void *v)
{
struct thermal_debugfs *thermal_dbg = s->private;
mutex_unlock(&thermal_dbg->lock);
}
static int cdev_tt_seq_show(struct seq_file *s, void *v)
{
struct thermal_debugfs *thermal_dbg = s->private;
struct cdev_debugfs *cdev_dbg = &thermal_dbg->cdev_dbg;
struct list_head *transitions = cdev_dbg->transitions;
struct cdev_record *entry;
int i = *(loff_t *)v;
if (!i)
seq_puts(s, "Transition\tOccurrences\n");
list_for_each_entry(entry, &transitions[i], node) {
char buffer[11];
snprintf(buffer, ARRAY_SIZE(buffer), "%d->%d",
entry->id >> 16, entry->id & 0xFFFF);
seq_printf(s, "%-10s\t%-10llu\n", buffer, entry->count);
}
return 0;
}
static const struct seq_operations tt_sops = {
.start = cdev_seq_start,
.next = cdev_seq_next,
.stop = cdev_seq_stop,
.show = cdev_tt_seq_show,
};
DEFINE_SEQ_ATTRIBUTE(tt);
static int cdev_dt_seq_show(struct seq_file *s, void *v)
{
struct thermal_debugfs *thermal_dbg = s->private;
struct cdev_debugfs *cdev_dbg = &thermal_dbg->cdev_dbg;
struct list_head *durations = cdev_dbg->durations;
struct cdev_record *entry;
int i = *(loff_t *)v;
if (!i)
seq_puts(s, "State\tResidency\n");
list_for_each_entry(entry, &durations[i], node) {
s64 duration = ktime_to_ms(entry->residency);
if (entry->id == cdev_dbg->current_state)
duration += ktime_ms_delta(ktime_get(),
cdev_dbg->timestamp);
seq_printf(s, "%-5d\t%-10llu\n", entry->id, duration);
}
return 0;
}
static const struct seq_operations dt_sops = {
.start = cdev_seq_start,
.next = cdev_seq_next,
.stop = cdev_seq_stop,
.show = cdev_dt_seq_show,
};
DEFINE_SEQ_ATTRIBUTE(dt);
static int cdev_clear_set(void *data, u64 val)
{
struct thermal_debugfs *thermal_dbg = data;
if (!val)
return -EINVAL;
mutex_lock(&thermal_dbg->lock);
thermal_debugfs_cdev_clear(&thermal_dbg->cdev_dbg);
mutex_unlock(&thermal_dbg->lock);
return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(cdev_clear_fops, NULL, cdev_clear_set, "%llu\n");
void thermal_debug_cdev_state_update(const struct thermal_cooling_device *cdev,
int new_state)
{
struct thermal_debugfs *thermal_dbg = cdev->debugfs;
struct cdev_debugfs *cdev_dbg;
struct cdev_record *cdev_record;
int transition, old_state;
if (!thermal_dbg || (thermal_dbg->cdev_dbg.current_state == new_state))
return;
mutex_lock(&thermal_dbg->lock);
cdev_dbg = &thermal_dbg->cdev_dbg;
old_state = cdev_dbg->current_state;
cdev_record = thermal_debugfs_cdev_record_get(thermal_dbg,
cdev_dbg->durations,
old_state);
if (cdev_record) {
ktime_t now = ktime_get();
ktime_t delta = ktime_sub(now, cdev_dbg->timestamp);
cdev_record->residency = ktime_add(cdev_record->residency, delta);
cdev_dbg->timestamp = now;
}
cdev_dbg->current_state = new_state;
thermal_debugfs_cdev_record_get(thermal_dbg, cdev_dbg->durations, new_state);
transition = (old_state << 16) | new_state;
cdev_record = thermal_debugfs_cdev_record_get(thermal_dbg,
cdev_dbg->transitions,
transition);
if (cdev_record)
cdev_record->count++;
cdev_dbg->total++;
mutex_unlock(&thermal_dbg->lock);
}
void thermal_debug_cdev_add(struct thermal_cooling_device *cdev, int state)
{
struct thermal_debugfs *thermal_dbg;
struct cdev_debugfs *cdev_dbg;
int i;
thermal_dbg = thermal_debugfs_add_id(d_cdev, cdev->id);
if (!thermal_dbg)
return;
cdev_dbg = &thermal_dbg->cdev_dbg;
for (i = 0; i < CDEVSTATS_HASH_SIZE; i++) {
INIT_LIST_HEAD(&cdev_dbg->transitions[i]);
INIT_LIST_HEAD(&cdev_dbg->durations[i]);
}
cdev_dbg->current_state = state;
cdev_dbg->timestamp = ktime_get();
thermal_debugfs_cdev_record_get(thermal_dbg, cdev_dbg->durations, state);
debugfs_create_file("trans_table", 0400, thermal_dbg->d_top,
thermal_dbg, &tt_fops);
debugfs_create_file("time_in_state_ms", 0400, thermal_dbg->d_top,
thermal_dbg, &dt_fops);
debugfs_create_file("clear", 0200, thermal_dbg->d_top,
thermal_dbg, &cdev_clear_fops);
debugfs_create_u32("total_trans", 0400, thermal_dbg->d_top,
&cdev_dbg->total);
cdev->debugfs = thermal_dbg;
}
static struct thermal_debugfs *thermal_debug_cdev_clear(struct thermal_cooling_device *cdev)
{
struct thermal_debugfs *thermal_dbg;
guard(cooling_dev)(cdev);
thermal_dbg = cdev->debugfs;
if (thermal_dbg)
cdev->debugfs = NULL;
return thermal_dbg;
}
void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev)
{
struct thermal_debugfs *thermal_dbg;
thermal_dbg = thermal_debug_cdev_clear(cdev);
if (!thermal_dbg)
return;
mutex_lock(&thermal_dbg->lock);
thermal_debugfs_cdev_clear(&thermal_dbg->cdev_dbg);
mutex_unlock(&thermal_dbg->lock);
thermal_debugfs_remove_id(thermal_dbg);
}
static struct tz_episode *thermal_debugfs_tz_event_alloc(struct thermal_zone_device *tz,
ktime_t now)
{
struct tz_episode *tze;
int i;
tze = kzalloc_flex(*tze, trip_stats, tz->num_trips);
if (!tze)
return NULL;
INIT_LIST_HEAD(&tze->node);
tze->timestamp = now;
tze->duration = KTIME_MIN;
tze->max_temp = INT_MIN;
for (i = 0; i < tz->num_trips; i++) {
tze->trip_stats[i].trip_temp = THERMAL_TEMP_INVALID;
tze->trip_stats[i].min = INT_MAX;
}
return tze;
}
void thermal_debug_tz_trip_up(struct thermal_zone_device *tz,
const struct thermal_trip *trip)
{
struct thermal_debugfs *thermal_dbg = tz->debugfs;
int trip_id = thermal_zone_trip_id(tz, trip);
ktime_t now = ktime_get();
struct trip_stats *trip_stats;
struct tz_debugfs *tz_dbg;
struct tz_episode *tze;
if (!thermal_dbg)
return;
tz_dbg = &thermal_dbg->tz_dbg;
mutex_lock(&thermal_dbg->lock);
if (!tz_dbg->nr_trips) {
tze = thermal_debugfs_tz_event_alloc(tz, now);
if (!tze)
goto unlock;
list_add(&tze->node, &tz_dbg->tz_episodes);
}
tz_dbg->trips_crossed[tz_dbg->nr_trips++] = trip_id;
tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
trip_stats = &tze->trip_stats[trip_id];
trip_stats->trip_temp = trip->temperature;
trip_stats->trip_hyst = trip->hysteresis;
trip_stats->timestamp = now;
unlock:
mutex_unlock(&thermal_dbg->lock);
}
static void tz_episode_close_trip(struct tz_episode *tze, int trip_id, ktime_t now)
{
struct trip_stats *trip_stats = &tze->trip_stats[trip_id];
ktime_t delta = ktime_sub(now, trip_stats->timestamp);
trip_stats->duration = ktime_add(delta, trip_stats->duration);
trip_stats->timestamp = KTIME_MAX;
}
void thermal_debug_tz_trip_down(struct thermal_zone_device *tz,
const struct thermal_trip *trip)
{
struct thermal_debugfs *thermal_dbg = tz->debugfs;
int trip_id = thermal_zone_trip_id(tz, trip);
ktime_t now = ktime_get();
struct tz_episode *tze;
struct tz_debugfs *tz_dbg;
int i;
if (!thermal_dbg)
return;
tz_dbg = &thermal_dbg->tz_dbg;
mutex_lock(&thermal_dbg->lock);
if (!tz_dbg->nr_trips)
goto out;
for (i = tz_dbg->nr_trips - 1; i >= 0; i--) {
if (tz_dbg->trips_crossed[i] == trip_id)
break;
}
if (i < 0)
goto out;
tz_dbg->nr_trips--;
if (i < tz_dbg->nr_trips)
tz_dbg->trips_crossed[i] = tz_dbg->trips_crossed[tz_dbg->nr_trips];
tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
tz_episode_close_trip(tze, trip_id, now);
if (!tz_dbg->nr_trips)
tze->duration = ktime_sub(now, tze->timestamp);
out:
mutex_unlock(&thermal_dbg->lock);
}
void thermal_debug_update_trip_stats(struct thermal_zone_device *tz)
{
struct thermal_debugfs *thermal_dbg = tz->debugfs;
struct tz_debugfs *tz_dbg;
struct tz_episode *tze;
int i;
if (!thermal_dbg)
return;
tz_dbg = &thermal_dbg->tz_dbg;
mutex_lock(&thermal_dbg->lock);
if (!tz_dbg->nr_trips)
goto out;
tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
if (tz->temperature > tze->max_temp)
tze->max_temp = tz->temperature;
for (i = 0; i < tz_dbg->nr_trips; i++) {
int trip_id = tz_dbg->trips_crossed[i];
struct trip_stats *trip_stats = &tze->trip_stats[trip_id];
trip_stats->min = min(trip_stats->min, tz->temperature);
trip_stats->avg += (tz->temperature - trip_stats->avg) /
++trip_stats->count;
}
out:
mutex_unlock(&thermal_dbg->lock);
}
static void *tze_seq_start(struct seq_file *s, loff_t *pos)
{
struct thermal_debugfs *thermal_dbg = s->private;
struct tz_debugfs *tz_dbg = &thermal_dbg->tz_dbg;
mutex_lock(&thermal_dbg->lock);
return seq_list_start(&tz_dbg->tz_episodes, *pos);
}
static void *tze_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
struct thermal_debugfs *thermal_dbg = s->private;
struct tz_debugfs *tz_dbg = &thermal_dbg->tz_dbg;
return seq_list_next(v, &tz_dbg->tz_episodes, pos);
}
static void tze_seq_stop(struct seq_file *s, void *v)
{
struct thermal_debugfs *thermal_dbg = s->private;
mutex_unlock(&thermal_dbg->lock);
}
static int tze_seq_show(struct seq_file *s, void *v)
{
struct thermal_debugfs *thermal_dbg = s->private;
struct thermal_zone_device *tz = thermal_dbg->tz_dbg.tz;
struct thermal_trip_desc *td;
struct tz_episode *tze;
u64 duration_ms;
int trip_id;
char c;
tze = list_entry((struct list_head *)v, struct tz_episode, node);
if (tze->duration == KTIME_MIN) {
duration_ms = ktime_to_ms(ktime_sub(ktime_get(), tze->timestamp));
c = '>';
} else {
duration_ms = ktime_to_ms(tze->duration);
c = '=';
}
seq_printf(s, ",-Mitigation at %llums, duration%c%llums, max. temp=%dm°C\n",
ktime_to_ms(tze->timestamp), c, duration_ms, tze->max_temp);
seq_puts(s, "| trip | type | temp(m°C) | hyst(m°C) | duration(ms) | avg(m°C) | min(m°C) |\n");
for_each_trip_desc(tz, td) {
const struct thermal_trip *trip = &td->trip;
struct trip_stats *trip_stats;
if (trip->type == THERMAL_TRIP_CRITICAL)
continue;
trip_id = thermal_zone_trip_id(tz, trip);
trip_stats = &tze->trip_stats[trip_id];
if (trip_stats->trip_temp == THERMAL_TEMP_INVALID)
continue;
if (trip_stats->timestamp != KTIME_MAX) {
ktime_t delta = ktime_sub(ktime_get(),
trip_stats->timestamp);
delta = ktime_add(delta, trip_stats->duration);
duration_ms = ktime_to_ms(delta);
c = '>';
} else {
duration_ms = ktime_to_ms(trip_stats->duration);
c = ' ';
}
seq_printf(s, "| %*d | %*s | %*d | %*d | %c%*lld | %*d | %*d |\n",
4 , trip_id,
8, thermal_trip_type_name(trip->type),
9, trip_stats->trip_temp,
9, trip_stats->trip_hyst,
c, 11, duration_ms,
9, trip_stats->avg,
9, trip_stats->min);
}
return 0;
}
static const struct seq_operations tze_sops = {
.start = tze_seq_start,
.next = tze_seq_next,
.stop = tze_seq_stop,
.show = tze_seq_show,
};
DEFINE_SEQ_ATTRIBUTE(tze);
void thermal_debug_tz_add(struct thermal_zone_device *tz)
{
struct thermal_debugfs *thermal_dbg;
struct tz_debugfs *tz_dbg;
thermal_dbg = thermal_debugfs_add_id(d_tz, tz->id);
if (!thermal_dbg)
return;
tz_dbg = &thermal_dbg->tz_dbg;
tz_dbg->tz = tz;
tz_dbg->trips_crossed = kzalloc_objs(int, tz->num_trips);
if (!tz_dbg->trips_crossed) {
thermal_debugfs_remove_id(thermal_dbg);
return;
}
INIT_LIST_HEAD(&tz_dbg->tz_episodes);
debugfs_create_file("mitigations", 0400, thermal_dbg->d_top,
thermal_dbg, &tze_fops);
tz->debugfs = thermal_dbg;
}
static struct thermal_debugfs *thermal_debug_tz_clear(struct thermal_zone_device *tz)
{
struct thermal_debugfs *thermal_dbg;
guard(thermal_zone)(tz);
thermal_dbg = tz->debugfs;
if (thermal_dbg)
tz->debugfs = NULL;
return thermal_dbg;
}
void thermal_debug_tz_remove(struct thermal_zone_device *tz)
{
struct thermal_debugfs *thermal_dbg;
struct tz_episode *tze, *tmp;
struct tz_debugfs *tz_dbg;
int *trips_crossed;
thermal_dbg = thermal_debug_tz_clear(tz);
if (!thermal_dbg)
return;
tz_dbg = &thermal_dbg->tz_dbg;
mutex_lock(&thermal_dbg->lock);
trips_crossed = tz_dbg->trips_crossed;
list_for_each_entry_safe(tze, tmp, &tz_dbg->tz_episodes, node) {
list_del(&tze->node);
kfree(tze);
}
mutex_unlock(&thermal_dbg->lock);
thermal_debugfs_remove_id(thermal_dbg);
kfree(trips_crossed);
}
void thermal_debug_tz_resume(struct thermal_zone_device *tz)
{
struct thermal_debugfs *thermal_dbg = tz->debugfs;
ktime_t now = ktime_get();
struct tz_debugfs *tz_dbg;
struct tz_episode *tze;
int i;
if (!thermal_dbg)
return;
mutex_lock(&thermal_dbg->lock);
tz_dbg = &thermal_dbg->tz_dbg;
if (!tz_dbg->nr_trips)
goto out;
tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
for (i = 0; i < tz_dbg->nr_trips; i++)
tz_episode_close_trip(tze, tz_dbg->trips_crossed[i], now);
tze->duration = ktime_sub(now, tze->timestamp);
tz_dbg->nr_trips = 0;
out:
mutex_unlock(&thermal_dbg->lock);
}