blob: f9bcf0f3ea30442d3d2aa69d903b66815ec4d8bf [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/cpufreq/cpufreq_stats.c
4 *
5 * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
Dave Jones0a829c52009-01-18 01:49:04 -05006 * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/cpufreq.h>
Paul Gortmaker5c720d372011-05-27 13:23:32 -040011#include <linux/module.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015struct cpufreq_stats {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 unsigned int total_trans;
Viresh Kumarbb176f72013-06-19 14:19:33 +053017 unsigned long long last_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 unsigned int max_state;
19 unsigned int state_num;
20 unsigned int last_index;
Viresh Kumar1e7586a2012-10-26 00:51:21 +020021 u64 *time_in_state;
Kyle Linfcccc5c2019-04-09 16:43:04 +080022 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 unsigned int *freq_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 unsigned int *trans_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
Viresh Kumard476ec42018-01-04 08:53:54 +053027static void cpufreq_stats_update(struct cpufreq_stats *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Viresh Kumar95313472015-01-06 21:09:03 +053029 unsigned long long cur_time = get_jiffies_64();
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070030
Viresh Kumarc960f9b2015-01-06 21:09:12 +053031 stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
Viresh Kumar50941602015-01-06 21:09:07 +053032 stats->last_time = cur_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Markus Mayeree7930e2016-11-07 10:02:23 -080035static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
36{
37 unsigned int count = stats->max_state;
38
Kyle Linfcccc5c2019-04-09 16:43:04 +080039 spin_lock(&stats->lock);
Markus Mayeree7930e2016-11-07 10:02:23 -080040 memset(stats->time_in_state, 0, count * sizeof(u64));
Markus Mayeree7930e2016-11-07 10:02:23 -080041 memset(stats->trans_table, 0, count * count * sizeof(int));
Markus Mayeree7930e2016-11-07 10:02:23 -080042 stats->last_time = get_jiffies_64();
43 stats->total_trans = 0;
Kyle Linfcccc5c2019-04-09 16:43:04 +080044 spin_unlock(&stats->lock);
Markus Mayeree7930e2016-11-07 10:02:23 -080045}
46
Dave Jones0a829c52009-01-18 01:49:04 -050047static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Viresh Kumara9aaf292015-01-13 11:34:00 +053049 return sprintf(buf, "%d\n", policy->stats->total_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
Viresh Kumar10b81822019-02-01 11:45:44 +053051cpufreq_freq_attr_ro(total_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Dave Jones0a829c52009-01-18 01:49:04 -050053static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Viresh Kumar50941602015-01-06 21:09:07 +053055 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 ssize_t len = 0;
57 int i;
Viresh Kumara9aaf292015-01-13 11:34:00 +053058
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020059 if (policy->fast_switch_enabled)
60 return 0;
61
Kyle Linfcccc5c2019-04-09 16:43:04 +080062 spin_lock(&stats->lock);
Viresh Kumar50941602015-01-06 21:09:07 +053063 cpufreq_stats_update(stats);
Kyle Linfcccc5c2019-04-09 16:43:04 +080064 spin_unlock(&stats->lock);
Viresh Kumar97956072019-02-01 11:45:45 +053065
Viresh Kumar50941602015-01-06 21:09:07 +053066 for (i = 0; i < stats->state_num; i++) {
67 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
Dave Jones0a829c52009-01-18 01:49:04 -050068 (unsigned long long)
Viresh Kumar50941602015-01-06 21:09:07 +053069 jiffies_64_to_clock_t(stats->time_in_state[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71 return len;
72}
Viresh Kumar10b81822019-02-01 11:45:44 +053073cpufreq_freq_attr_ro(time_in_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Markus Mayeree7930e2016-11-07 10:02:23 -080075static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
76 size_t count)
77{
78 /* We don't care what is written to the attribute. */
79 cpufreq_stats_clear_table(policy->stats);
80 return count;
81}
Viresh Kumar10b81822019-02-01 11:45:44 +053082cpufreq_freq_attr_wo(reset);
Markus Mayeree7930e2016-11-07 10:02:23 -080083
Dave Jones0a829c52009-01-18 01:49:04 -050084static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Viresh Kumar50941602015-01-06 21:09:07 +053086 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 ssize_t len = 0;
88 int i, j;
89
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020090 if (policy->fast_switch_enabled)
91 return 0;
92
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070093 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
94 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
Viresh Kumar50941602015-01-06 21:09:07 +053095 for (i = 0; i < stats->state_num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (len >= PAGE_SIZE)
97 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070098 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +053099 stats->freq_table[i]);
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700100 }
101 if (len >= PAGE_SIZE)
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200102 return PAGE_SIZE;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700103
104 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
105
Viresh Kumar50941602015-01-06 21:09:07 +0530106 for (i = 0; i < stats->state_num; i++) {
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700107 if (len >= PAGE_SIZE)
108 break;
109
110 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
Viresh Kumar50941602015-01-06 21:09:07 +0530111 stats->freq_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Viresh Kumar50941602015-01-06 21:09:07 +0530113 for (j = 0; j < stats->state_num; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (len >= PAGE_SIZE)
115 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700116 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +0530117 stats->trans_table[i*stats->max_state+j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200119 if (len >= PAGE_SIZE)
120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
122 }
Gautham R. Shenoyf7bc9b22017-11-07 13:39:29 +0530123
124 if (len >= PAGE_SIZE) {
125 pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
126 return -EFBIG;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return len;
129}
Viresh Kumardf18e502013-02-04 11:38:52 +0000130cpufreq_freq_attr_ro(trans_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static struct attribute *default_attrs[] = {
Viresh Kumardf18e502013-02-04 11:38:52 +0000133 &total_trans.attr,
134 &time_in_state.attr,
Markus Mayeree7930e2016-11-07 10:02:23 -0800135 &reset.attr,
Viresh Kumardf18e502013-02-04 11:38:52 +0000136 &trans_table.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 NULL
138};
Arvind Yadav402202e2017-07-03 13:29:04 +0530139static const struct attribute_group stats_attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .attrs = default_attrs,
141 .name = "stats"
142};
143
Viresh Kumar50941602015-01-06 21:09:07 +0530144static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int index;
Viresh Kumar50941602015-01-06 21:09:07 +0530147 for (index = 0; index < stats->max_state; index++)
148 if (stats->freq_table[index] == freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return index;
150 return -1;
151}
152
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200153void cpufreq_stats_free_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Viresh Kumar50941602015-01-06 21:09:07 +0530155 struct cpufreq_stats *stats = policy->stats;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000156
Viresh Kumara9aaf292015-01-13 11:34:00 +0530157 /* Already freed */
Viresh Kumar50941602015-01-06 21:09:07 +0530158 if (!stats)
Viresh Kumar2d135942014-01-07 07:10:12 +0530159 return;
160
Viresh Kumar50941602015-01-06 21:09:07 +0530161 pr_debug("%s: Free stats table\n", __func__);
Viresh Kumar2d135942014-01-07 07:10:12 +0530162
163 sysfs_remove_group(&policy->kobj, &stats_attr_group);
Viresh Kumar50941602015-01-06 21:09:07 +0530164 kfree(stats->time_in_state);
165 kfree(stats);
Viresh Kumara9aaf292015-01-13 11:34:00 +0530166 policy->stats = NULL;
steven finney98586ed2011-05-02 11:29:17 -0700167}
168
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200169void cpufreq_stats_create_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Viresh Kumara685c6d2015-01-06 21:09:11 +0530171 unsigned int i = 0, count = 0, ret = -ENOMEM;
Viresh Kumar50941602015-01-06 21:09:07 +0530172 struct cpufreq_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned int alloc_size;
Viresh Kumar55d85292017-04-25 15:57:15 +0530174 struct cpufreq_frequency_table *pos;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800175
Viresh Kumar55d85292017-04-25 15:57:15 +0530176 count = cpufreq_table_count_valid_entries(policy);
177 if (!count)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200178 return;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800179
Viresh Kumarb8c67442015-01-06 21:09:01 +0530180 /* stats already initialized */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530181 if (policy->stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200182 return;
Viresh Kumarb8c67442015-01-06 21:09:01 +0530183
Viresh Kumar50941602015-01-06 21:09:07 +0530184 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530185 if (!stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200186 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200188 alloc_size = count * sizeof(int) + count * sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 alloc_size += count * count * sizeof(int);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530191
192 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
Viresh Kumar50941602015-01-06 21:09:07 +0530193 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530194 if (!stats->time_in_state)
195 goto free_stat;
196
Viresh Kumar50941602015-01-06 21:09:07 +0530197 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Viresh Kumar50941602015-01-06 21:09:07 +0530199 stats->trans_table = stats->freq_table + count;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530200
201 stats->max_state = count;
202
203 /* Find valid-unique entries */
Viresh Kumar55d85292017-04-25 15:57:15 +0530204 cpufreq_for_each_valid_entry(pos, policy->freq_table)
Viresh Kumar50941602015-01-06 21:09:07 +0530205 if (freq_table_get_index(stats, pos->frequency) == -1)
206 stats->freq_table[i++] = pos->frequency;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530207
Viresh Kumar490285c2015-01-06 21:09:15 +0530208 stats->state_num = i;
Viresh Kumar50941602015-01-06 21:09:07 +0530209 stats->last_time = get_jiffies_64();
210 stats->last_index = freq_table_get_index(stats, policy->cur);
Kyle Linfcccc5c2019-04-09 16:43:04 +0800211 spin_lock_init(&stats->lock);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530212
213 policy->stats = stats;
214 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
215 if (!ret)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200216 return;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530217
218 /* We failed, release resources */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530219 policy->stats = NULL;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530220 kfree(stats->time_in_state);
221free_stat:
222 kfree(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200225void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
226 unsigned int new_freq)
Viresh Kumarb3f9ff82014-01-07 07:10:13 +0530227{
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200228 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int old_index, new_index;
230
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200231 if (!stats) {
Viresh Kumara9aaf292015-01-13 11:34:00 +0530232 pr_debug("%s: No stats found\n", __func__);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200233 return;
Viresh Kumara9aaf292015-01-13 11:34:00 +0530234 }
235
Viresh Kumar50941602015-01-06 21:09:07 +0530236 old_index = stats->last_index;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200237 new_index = freq_table_get_index(stats, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Viresh Kumar50941602015-01-06 21:09:07 +0530239 /* We can't do stats->time_in_state[-1]= .. */
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200240 if (old_index == -1 || new_index == -1 || old_index == new_index)
241 return;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800242
Kyle Linfcccc5c2019-04-09 16:43:04 +0800243 spin_lock(&stats->lock);
Viresh Kumare7347692015-01-06 21:09:14 +0530244 cpufreq_stats_update(stats);
245
Viresh Kumar50941602015-01-06 21:09:07 +0530246 stats->last_index = new_index;
Viresh Kumar50941602015-01-06 21:09:07 +0530247 stats->trans_table[old_index * stats->max_state + new_index]++;
Viresh Kumar50941602015-01-06 21:09:07 +0530248 stats->total_trans++;
Kyle Linfcccc5c2019-04-09 16:43:04 +0800249 spin_unlock(&stats->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}