blob: 1570d6f3e75d36a6373e7bcddc16365511f4dd23 [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 Kumar7854c752020-11-17 17:02:10 +053012#include <linux/sched/clock.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053013#include <linux/slab.h>
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 unsigned int *freq_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 unsigned int *trans_table;
Viresh Kumar40c3bd42020-10-05 13:26:01 +053024
25 /* Deferred reset */
26 unsigned int reset_pending;
27 unsigned long long reset_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028};
29
Viresh Kumar40c3bd42020-10-05 13:26:01 +053030static void cpufreq_stats_update(struct cpufreq_stats *stats,
31 unsigned long long time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Viresh Kumar7854c752020-11-17 17:02:10 +053033 unsigned long long cur_time = local_clock();
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070034
Viresh Kumar40c3bd42020-10-05 13:26:01 +053035 stats->time_in_state[stats->last_index] += cur_time - time;
Viresh Kumar50941602015-01-06 21:09:07 +053036 stats->last_time = cur_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Viresh Kumar40c3bd42020-10-05 13:26:01 +053039static void cpufreq_stats_reset_table(struct cpufreq_stats *stats)
Markus Mayeree7930e2016-11-07 10:02:23 -080040{
41 unsigned int count = stats->max_state;
42
43 memset(stats->time_in_state, 0, count * sizeof(u64));
Markus Mayeree7930e2016-11-07 10:02:23 -080044 memset(stats->trans_table, 0, count * count * sizeof(int));
Viresh Kumar7854c752020-11-17 17:02:10 +053045 stats->last_time = local_clock();
Markus Mayeree7930e2016-11-07 10:02:23 -080046 stats->total_trans = 0;
Viresh Kumar40c3bd42020-10-05 13:26:01 +053047
48 /* Adjust for the time elapsed since reset was requested */
49 WRITE_ONCE(stats->reset_pending, 0);
Rafael J. Wysockiefad4242020-10-06 21:43:43 +020050 /*
51 * Prevent the reset_time read from being reordered before the
52 * reset_pending accesses in cpufreq_stats_record_transition().
53 */
54 smp_rmb();
Viresh Kumar40c3bd42020-10-05 13:26:01 +053055 cpufreq_stats_update(stats, READ_ONCE(stats->reset_time));
Markus Mayeree7930e2016-11-07 10:02:23 -080056}
57
Dave Jones0a829c52009-01-18 01:49:04 -050058static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Viresh Kumar40c3bd42020-10-05 13:26:01 +053060 struct cpufreq_stats *stats = policy->stats;
61
62 if (READ_ONCE(stats->reset_pending))
63 return sprintf(buf, "%d\n", 0);
64 else
Viresh Kumarb7af6082020-10-12 10:20:07 +053065 return sprintf(buf, "%u\n", stats->total_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
Viresh Kumar10b81822019-02-01 11:45:44 +053067cpufreq_freq_attr_ro(total_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Dave Jones0a829c52009-01-18 01:49:04 -050069static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Viresh Kumar50941602015-01-06 21:09:07 +053071 struct cpufreq_stats *stats = policy->stats;
Viresh Kumar40c3bd42020-10-05 13:26:01 +053072 bool pending = READ_ONCE(stats->reset_pending);
73 unsigned long long time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 ssize_t len = 0;
75 int i;
Viresh Kumara9aaf292015-01-13 11:34:00 +053076
Viresh Kumar50941602015-01-06 21:09:07 +053077 for (i = 0; i < stats->state_num; i++) {
Viresh Kumar40c3bd42020-10-05 13:26:01 +053078 if (pending) {
Rafael J. Wysockiefad4242020-10-06 21:43:43 +020079 if (i == stats->last_index) {
80 /*
81 * Prevent the reset_time read from occurring
82 * before the reset_pending read above.
83 */
84 smp_rmb();
Viresh Kumar7854c752020-11-17 17:02:10 +053085 time = local_clock() - READ_ONCE(stats->reset_time);
Rafael J. Wysockiefad4242020-10-06 21:43:43 +020086 } else {
Viresh Kumar40c3bd42020-10-05 13:26:01 +053087 time = 0;
Rafael J. Wysockiefad4242020-10-06 21:43:43 +020088 }
Viresh Kumar40c3bd42020-10-05 13:26:01 +053089 } else {
90 time = stats->time_in_state[i];
91 if (i == stats->last_index)
Viresh Kumar7854c752020-11-17 17:02:10 +053092 time += local_clock() - stats->last_time;
Viresh Kumar40c3bd42020-10-05 13:26:01 +053093 }
94
Viresh Kumar50941602015-01-06 21:09:07 +053095 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
Viresh Kumar7854c752020-11-17 17:02:10 +053096 nsec_to_clock_t(time));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98 return len;
99}
Viresh Kumar10b81822019-02-01 11:45:44 +0530100cpufreq_freq_attr_ro(time_in_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Viresh Kumar40c3bd42020-10-05 13:26:01 +0530102/* We don't care what is written to the attribute */
Markus Mayeree7930e2016-11-07 10:02:23 -0800103static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
104 size_t count)
105{
Viresh Kumar40c3bd42020-10-05 13:26:01 +0530106 struct cpufreq_stats *stats = policy->stats;
107
108 /*
109 * Defer resetting of stats to cpufreq_stats_record_transition() to
110 * avoid races.
111 */
Viresh Kumar7854c752020-11-17 17:02:10 +0530112 WRITE_ONCE(stats->reset_time, local_clock());
Rafael J. Wysockiefad4242020-10-06 21:43:43 +0200113 /*
114 * The memory barrier below is to prevent the readers of reset_time from
115 * seeing a stale or partially updated value.
116 */
117 smp_wmb();
Viresh Kumar40c3bd42020-10-05 13:26:01 +0530118 WRITE_ONCE(stats->reset_pending, 1);
119
Markus Mayeree7930e2016-11-07 10:02:23 -0800120 return count;
121}
Viresh Kumar10b81822019-02-01 11:45:44 +0530122cpufreq_freq_attr_wo(reset);
Markus Mayeree7930e2016-11-07 10:02:23 -0800123
Dave Jones0a829c52009-01-18 01:49:04 -0500124static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Viresh Kumar50941602015-01-06 21:09:07 +0530126 struct cpufreq_stats *stats = policy->stats;
Viresh Kumar40c3bd42020-10-05 13:26:01 +0530127 bool pending = READ_ONCE(stats->reset_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 ssize_t len = 0;
Viresh Kumar40c3bd42020-10-05 13:26:01 +0530129 int i, j, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Takashi Iwai3c0897c2020-03-11 08:13:41 +0100131 len += scnprintf(buf + len, PAGE_SIZE - len, " From : To\n");
132 len += scnprintf(buf + len, PAGE_SIZE - len, " : ");
Viresh Kumar50941602015-01-06 21:09:07 +0530133 for (i = 0; i < stats->state_num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (len >= PAGE_SIZE)
135 break;
Takashi Iwai3c0897c2020-03-11 08:13:41 +0100136 len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +0530137 stats->freq_table[i]);
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700138 }
139 if (len >= PAGE_SIZE)
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200140 return PAGE_SIZE;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700141
Takashi Iwai3c0897c2020-03-11 08:13:41 +0100142 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700143
Viresh Kumar50941602015-01-06 21:09:07 +0530144 for (i = 0; i < stats->state_num; i++) {
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700145 if (len >= PAGE_SIZE)
146 break;
147
Takashi Iwai3c0897c2020-03-11 08:13:41 +0100148 len += scnprintf(buf + len, PAGE_SIZE - len, "%9u: ",
Viresh Kumar50941602015-01-06 21:09:07 +0530149 stats->freq_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Viresh Kumar50941602015-01-06 21:09:07 +0530151 for (j = 0; j < stats->state_num; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (len >= PAGE_SIZE)
153 break;
Viresh Kumar40c3bd42020-10-05 13:26:01 +0530154
155 if (pending)
156 count = 0;
157 else
158 count = stats->trans_table[i * stats->max_state + j];
159
160 len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200162 if (len >= PAGE_SIZE)
163 break;
Takashi Iwai3c0897c2020-03-11 08:13:41 +0100164 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Gautham R. Shenoyf7bc9b22017-11-07 13:39:29 +0530166
167 if (len >= PAGE_SIZE) {
168 pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
169 return -EFBIG;
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return len;
172}
Viresh Kumardf18e502013-02-04 11:38:52 +0000173cpufreq_freq_attr_ro(trans_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static struct attribute *default_attrs[] = {
Viresh Kumardf18e502013-02-04 11:38:52 +0000176 &total_trans.attr,
177 &time_in_state.attr,
Markus Mayeree7930e2016-11-07 10:02:23 -0800178 &reset.attr,
Viresh Kumardf18e502013-02-04 11:38:52 +0000179 &trans_table.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 NULL
181};
Arvind Yadav402202e2017-07-03 13:29:04 +0530182static const struct attribute_group stats_attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .attrs = default_attrs,
184 .name = "stats"
185};
186
Viresh Kumar50941602015-01-06 21:09:07 +0530187static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int index;
Viresh Kumar50941602015-01-06 21:09:07 +0530190 for (index = 0; index < stats->max_state; index++)
191 if (stats->freq_table[index] == freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return index;
193 return -1;
194}
195
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200196void cpufreq_stats_free_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Viresh Kumar50941602015-01-06 21:09:07 +0530198 struct cpufreq_stats *stats = policy->stats;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000199
Viresh Kumara9aaf292015-01-13 11:34:00 +0530200 /* Already freed */
Viresh Kumar50941602015-01-06 21:09:07 +0530201 if (!stats)
Viresh Kumar2d135942014-01-07 07:10:12 +0530202 return;
203
Viresh Kumar50941602015-01-06 21:09:07 +0530204 pr_debug("%s: Free stats table\n", __func__);
Viresh Kumar2d135942014-01-07 07:10:12 +0530205
206 sysfs_remove_group(&policy->kobj, &stats_attr_group);
Viresh Kumar50941602015-01-06 21:09:07 +0530207 kfree(stats->time_in_state);
208 kfree(stats);
Viresh Kumara9aaf292015-01-13 11:34:00 +0530209 policy->stats = NULL;
steven finney98586ed2011-05-02 11:29:17 -0700210}
211
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200212void cpufreq_stats_create_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Shaokun Zhang5de12622021-05-31 15:16:07 +0800214 unsigned int i = 0, count;
Viresh Kumar50941602015-01-06 21:09:07 +0530215 struct cpufreq_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 unsigned int alloc_size;
Viresh Kumar55d85292017-04-25 15:57:15 +0530217 struct cpufreq_frequency_table *pos;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800218
Viresh Kumar55d85292017-04-25 15:57:15 +0530219 count = cpufreq_table_count_valid_entries(policy);
220 if (!count)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200221 return;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800222
Viresh Kumarb8c67442015-01-06 21:09:01 +0530223 /* stats already initialized */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530224 if (policy->stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200225 return;
Viresh Kumarb8c67442015-01-06 21:09:01 +0530226
Viresh Kumar50941602015-01-06 21:09:07 +0530227 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530228 if (!stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200231 alloc_size = count * sizeof(int) + count * sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 alloc_size += count * count * sizeof(int);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530234
235 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
Viresh Kumar50941602015-01-06 21:09:07 +0530236 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530237 if (!stats->time_in_state)
238 goto free_stat;
239
Viresh Kumar50941602015-01-06 21:09:07 +0530240 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Viresh Kumar50941602015-01-06 21:09:07 +0530242 stats->trans_table = stats->freq_table + count;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530243
244 stats->max_state = count;
245
246 /* Find valid-unique entries */
Viresh Kumar55d85292017-04-25 15:57:15 +0530247 cpufreq_for_each_valid_entry(pos, policy->freq_table)
Viresh Kumar50941602015-01-06 21:09:07 +0530248 if (freq_table_get_index(stats, pos->frequency) == -1)
249 stats->freq_table[i++] = pos->frequency;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530250
Viresh Kumar490285c2015-01-06 21:09:15 +0530251 stats->state_num = i;
Viresh Kumar7854c752020-11-17 17:02:10 +0530252 stats->last_time = local_clock();
Viresh Kumar50941602015-01-06 21:09:07 +0530253 stats->last_index = freq_table_get_index(stats, policy->cur);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530254
255 policy->stats = stats;
Shaokun Zhang5de12622021-05-31 15:16:07 +0800256 if (!sysfs_create_group(&policy->kobj, &stats_attr_group))
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200257 return;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530258
259 /* We failed, release resources */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530260 policy->stats = NULL;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530261 kfree(stats->time_in_state);
262free_stat:
263 kfree(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200266void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
267 unsigned int new_freq)
Viresh Kumarb3f9ff82014-01-07 07:10:13 +0530268{
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200269 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int old_index, new_index;
271
Viresh Kumar4958b462020-10-05 13:26:03 +0530272 if (unlikely(!stats))
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200273 return;
Viresh Kumar40c3bd42020-10-05 13:26:01 +0530274
275 if (unlikely(READ_ONCE(stats->reset_pending)))
276 cpufreq_stats_reset_table(stats);
Viresh Kumara9aaf292015-01-13 11:34:00 +0530277
Viresh Kumar50941602015-01-06 21:09:07 +0530278 old_index = stats->last_index;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200279 new_index = freq_table_get_index(stats, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Viresh Kumar50941602015-01-06 21:09:07 +0530281 /* We can't do stats->time_in_state[-1]= .. */
Viresh Kumar4958b462020-10-05 13:26:03 +0530282 if (unlikely(old_index == -1 || new_index == -1 || old_index == new_index))
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200283 return;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800284
Viresh Kumar40c3bd42020-10-05 13:26:01 +0530285 cpufreq_stats_update(stats, stats->last_time);
Viresh Kumare7347692015-01-06 21:09:14 +0530286
Viresh Kumar50941602015-01-06 21:09:07 +0530287 stats->last_index = new_index;
Viresh Kumar50941602015-01-06 21:09:07 +0530288 stats->trans_table[old_index * stats->max_state + new_index]++;
Viresh Kumar50941602015-01-06 21:09:07 +0530289 stats->total_trans++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}