blob: 1572129844a5bf6e223ed92c55733f215feb312e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/cpufreq/cpufreq_stats.c
3 *
4 * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
Dave Jones0a829c52009-01-18 01:49:04 -05005 * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/cpufreq.h>
Paul Gortmaker5c720d372011-05-27 13:23:32 -040014#include <linux/module.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020017static DEFINE_SPINLOCK(cpufreq_stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019struct cpufreq_stats {
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 unsigned int total_trans;
Viresh Kumarbb176f72013-06-19 14:19:33 +053021 unsigned long long last_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 unsigned int max_state;
23 unsigned int state_num;
24 unsigned int last_index;
Viresh Kumar1e7586a2012-10-26 00:51:21 +020025 u64 *time_in_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 unsigned int *freq_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 unsigned int *trans_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028};
29
Viresh Kumard476ec42018-01-04 08:53:54 +053030static void cpufreq_stats_update(struct cpufreq_stats *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Viresh Kumar95313472015-01-06 21:09:03 +053032 unsigned long long cur_time = get_jiffies_64();
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 spin_lock(&cpufreq_stats_lock);
Viresh Kumarc960f9b2015-01-06 21:09:12 +053035 stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
Viresh Kumar50941602015-01-06 21:09:07 +053036 stats->last_time = cur_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 spin_unlock(&cpufreq_stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Markus Mayeree7930e2016-11-07 10:02:23 -080040static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
41{
42 unsigned int count = stats->max_state;
43
44 memset(stats->time_in_state, 0, count * sizeof(u64));
Markus Mayeree7930e2016-11-07 10:02:23 -080045 memset(stats->trans_table, 0, count * count * sizeof(int));
Markus Mayeree7930e2016-11-07 10:02:23 -080046 stats->last_time = get_jiffies_64();
47 stats->total_trans = 0;
48}
49
Dave Jones0a829c52009-01-18 01:49:04 -050050static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Viresh Kumara9aaf292015-01-13 11:34:00 +053052 return sprintf(buf, "%d\n", policy->stats->total_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Dave Jones0a829c52009-01-18 01:49:04 -050055static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Viresh Kumar50941602015-01-06 21:09:07 +053057 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 ssize_t len = 0;
59 int i;
Viresh Kumara9aaf292015-01-13 11:34:00 +053060
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020061 if (policy->fast_switch_enabled)
62 return 0;
63
Viresh Kumar50941602015-01-06 21:09:07 +053064 cpufreq_stats_update(stats);
65 for (i = 0; i < stats->state_num; i++) {
66 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
Dave Jones0a829c52009-01-18 01:49:04 -050067 (unsigned long long)
Viresh Kumar50941602015-01-06 21:09:07 +053068 jiffies_64_to_clock_t(stats->time_in_state[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70 return len;
71}
72
Markus Mayeree7930e2016-11-07 10:02:23 -080073static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
74 size_t count)
75{
76 /* We don't care what is written to the attribute. */
77 cpufreq_stats_clear_table(policy->stats);
78 return count;
79}
80
Dave Jones0a829c52009-01-18 01:49:04 -050081static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Viresh Kumar50941602015-01-06 21:09:07 +053083 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 ssize_t len = 0;
85 int i, j;
86
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020087 if (policy->fast_switch_enabled)
88 return 0;
89
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070090 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
91 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
Viresh Kumar50941602015-01-06 21:09:07 +053092 for (i = 0; i < stats->state_num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (len >= PAGE_SIZE)
94 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070095 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +053096 stats->freq_table[i]);
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070097 }
98 if (len >= PAGE_SIZE)
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -020099 return PAGE_SIZE;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700100
101 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
102
Viresh Kumar50941602015-01-06 21:09:07 +0530103 for (i = 0; i < stats->state_num; i++) {
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700104 if (len >= PAGE_SIZE)
105 break;
106
107 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
Viresh Kumar50941602015-01-06 21:09:07 +0530108 stats->freq_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Viresh Kumar50941602015-01-06 21:09:07 +0530110 for (j = 0; j < stats->state_num; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (len >= PAGE_SIZE)
112 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700113 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +0530114 stats->trans_table[i*stats->max_state+j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200116 if (len >= PAGE_SIZE)
117 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
119 }
Gautham R. Shenoyf7bc9b22017-11-07 13:39:29 +0530120
121 if (len >= PAGE_SIZE) {
122 pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
123 return -EFBIG;
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return len;
126}
Viresh Kumardf18e502013-02-04 11:38:52 +0000127cpufreq_freq_attr_ro(trans_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Viresh Kumardf18e502013-02-04 11:38:52 +0000129cpufreq_freq_attr_ro(total_trans);
130cpufreq_freq_attr_ro(time_in_state);
Markus Mayeree7930e2016-11-07 10:02:23 -0800131cpufreq_freq_attr_wo(reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static struct attribute *default_attrs[] = {
Viresh Kumardf18e502013-02-04 11:38:52 +0000134 &total_trans.attr,
135 &time_in_state.attr,
Markus Mayeree7930e2016-11-07 10:02:23 -0800136 &reset.attr,
Viresh Kumardf18e502013-02-04 11:38:52 +0000137 &trans_table.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 NULL
139};
Arvind Yadav402202e2017-07-03 13:29:04 +0530140static const struct attribute_group stats_attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .attrs = default_attrs,
142 .name = "stats"
143};
144
Viresh Kumar50941602015-01-06 21:09:07 +0530145static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 int index;
Viresh Kumar50941602015-01-06 21:09:07 +0530148 for (index = 0; index < stats->max_state; index++)
149 if (stats->freq_table[index] == freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return index;
151 return -1;
152}
153
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200154void cpufreq_stats_free_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Viresh Kumar50941602015-01-06 21:09:07 +0530156 struct cpufreq_stats *stats = policy->stats;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000157
Viresh Kumara9aaf292015-01-13 11:34:00 +0530158 /* Already freed */
Viresh Kumar50941602015-01-06 21:09:07 +0530159 if (!stats)
Viresh Kumar2d135942014-01-07 07:10:12 +0530160 return;
161
Viresh Kumar50941602015-01-06 21:09:07 +0530162 pr_debug("%s: Free stats table\n", __func__);
Viresh Kumar2d135942014-01-07 07:10:12 +0530163
164 sysfs_remove_group(&policy->kobj, &stats_attr_group);
Viresh Kumar50941602015-01-06 21:09:07 +0530165 kfree(stats->time_in_state);
166 kfree(stats);
Viresh Kumara9aaf292015-01-13 11:34:00 +0530167 policy->stats = NULL;
steven finney98586ed2011-05-02 11:29:17 -0700168}
169
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200170void cpufreq_stats_create_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Viresh Kumara685c6d2015-01-06 21:09:11 +0530172 unsigned int i = 0, count = 0, ret = -ENOMEM;
Viresh Kumar50941602015-01-06 21:09:07 +0530173 struct cpufreq_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned int alloc_size;
Viresh Kumar55d85292017-04-25 15:57:15 +0530175 struct cpufreq_frequency_table *pos;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800176
Viresh Kumar55d85292017-04-25 15:57:15 +0530177 count = cpufreq_table_count_valid_entries(policy);
178 if (!count)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200179 return;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800180
Viresh Kumarb8c67442015-01-06 21:09:01 +0530181 /* stats already initialized */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530182 if (policy->stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200183 return;
Viresh Kumarb8c67442015-01-06 21:09:01 +0530184
Viresh Kumar50941602015-01-06 21:09:07 +0530185 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530186 if (!stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200187 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200189 alloc_size = count * sizeof(int) + count * sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 alloc_size += count * count * sizeof(int);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530192
193 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
Viresh Kumar50941602015-01-06 21:09:07 +0530194 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530195 if (!stats->time_in_state)
196 goto free_stat;
197
Viresh Kumar50941602015-01-06 21:09:07 +0530198 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Viresh Kumar50941602015-01-06 21:09:07 +0530200 stats->trans_table = stats->freq_table + count;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530201
202 stats->max_state = count;
203
204 /* Find valid-unique entries */
Viresh Kumar55d85292017-04-25 15:57:15 +0530205 cpufreq_for_each_valid_entry(pos, policy->freq_table)
Viresh Kumar50941602015-01-06 21:09:07 +0530206 if (freq_table_get_index(stats, pos->frequency) == -1)
207 stats->freq_table[i++] = pos->frequency;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530208
Viresh Kumar490285c2015-01-06 21:09:15 +0530209 stats->state_num = i;
Viresh Kumar50941602015-01-06 21:09:07 +0530210 stats->last_time = get_jiffies_64();
211 stats->last_index = freq_table_get_index(stats, policy->cur);
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
Viresh Kumare7347692015-01-06 21:09:14 +0530243 cpufreq_stats_update(stats);
244
Viresh Kumar50941602015-01-06 21:09:07 +0530245 stats->last_index = new_index;
Viresh Kumar50941602015-01-06 21:09:07 +0530246 stats->trans_table[old_index * stats->max_state + new_index]++;
Viresh Kumar50941602015-01-06 21:09:07 +0530247 stats->total_trans++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}