blob: 1e55b5790853517dbd6605ebb4e1e328adff75ba [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 Kumar50941602015-01-06 21:09:07 +053030static int 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);
38 return 0;
39}
40
Markus Mayeree7930e2016-11-07 10:02:23 -080041static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
42{
43 unsigned int count = stats->max_state;
44
45 memset(stats->time_in_state, 0, count * sizeof(u64));
Markus Mayeree7930e2016-11-07 10:02:23 -080046 memset(stats->trans_table, 0, count * count * sizeof(int));
Markus Mayeree7930e2016-11-07 10:02:23 -080047 stats->last_time = get_jiffies_64();
48 stats->total_trans = 0;
49}
50
Dave Jones0a829c52009-01-18 01:49:04 -050051static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Viresh Kumara9aaf292015-01-13 11:34:00 +053053 return sprintf(buf, "%d\n", policy->stats->total_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Dave Jones0a829c52009-01-18 01:49:04 -050056static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Viresh Kumar50941602015-01-06 21:09:07 +053058 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 ssize_t len = 0;
60 int i;
Viresh Kumara9aaf292015-01-13 11:34:00 +053061
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020062 if (policy->fast_switch_enabled)
63 return 0;
64
Viresh Kumar50941602015-01-06 21:09:07 +053065 cpufreq_stats_update(stats);
66 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}
73
Markus Mayeree7930e2016-11-07 10:02:23 -080074static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
75 size_t count)
76{
77 /* We don't care what is written to the attribute. */
78 cpufreq_stats_clear_table(policy->stats);
79 return count;
80}
81
Dave Jones0a829c52009-01-18 01:49:04 -050082static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Viresh Kumar50941602015-01-06 21:09:07 +053084 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 ssize_t len = 0;
86 int i, j;
87
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020088 if (policy->fast_switch_enabled)
89 return 0;
90
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070091 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
92 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
Viresh Kumar50941602015-01-06 21:09:07 +053093 for (i = 0; i < stats->state_num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (len >= PAGE_SIZE)
95 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070096 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +053097 stats->freq_table[i]);
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070098 }
99 if (len >= PAGE_SIZE)
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200100 return PAGE_SIZE;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700101
102 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
103
Viresh Kumar50941602015-01-06 21:09:07 +0530104 for (i = 0; i < stats->state_num; i++) {
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700105 if (len >= PAGE_SIZE)
106 break;
107
108 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
Viresh Kumar50941602015-01-06 21:09:07 +0530109 stats->freq_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Viresh Kumar50941602015-01-06 21:09:07 +0530111 for (j = 0; j < stats->state_num; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (len >= PAGE_SIZE)
113 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700114 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +0530115 stats->trans_table[i*stats->max_state+j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200117 if (len >= PAGE_SIZE)
118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
120 }
Gautham R. Shenoyf7bc9b22017-11-07 13:39:29 +0530121
122 if (len >= PAGE_SIZE) {
123 pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
124 return -EFBIG;
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return len;
127}
Viresh Kumardf18e502013-02-04 11:38:52 +0000128cpufreq_freq_attr_ro(trans_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Viresh Kumardf18e502013-02-04 11:38:52 +0000130cpufreq_freq_attr_ro(total_trans);
131cpufreq_freq_attr_ro(time_in_state);
Markus Mayeree7930e2016-11-07 10:02:23 -0800132cpufreq_freq_attr_wo(reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134static struct attribute *default_attrs[] = {
Viresh Kumardf18e502013-02-04 11:38:52 +0000135 &total_trans.attr,
136 &time_in_state.attr,
Markus Mayeree7930e2016-11-07 10:02:23 -0800137 &reset.attr,
Viresh Kumardf18e502013-02-04 11:38:52 +0000138 &trans_table.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 NULL
140};
Arvind Yadav402202e2017-07-03 13:29:04 +0530141static const struct attribute_group stats_attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .attrs = default_attrs,
143 .name = "stats"
144};
145
Viresh Kumar50941602015-01-06 21:09:07 +0530146static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 int index;
Viresh Kumar50941602015-01-06 21:09:07 +0530149 for (index = 0; index < stats->max_state; index++)
150 if (stats->freq_table[index] == freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return index;
152 return -1;
153}
154
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200155void cpufreq_stats_free_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Viresh Kumar50941602015-01-06 21:09:07 +0530157 struct cpufreq_stats *stats = policy->stats;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000158
Viresh Kumara9aaf292015-01-13 11:34:00 +0530159 /* Already freed */
Viresh Kumar50941602015-01-06 21:09:07 +0530160 if (!stats)
Viresh Kumar2d135942014-01-07 07:10:12 +0530161 return;
162
Viresh Kumar50941602015-01-06 21:09:07 +0530163 pr_debug("%s: Free stats table\n", __func__);
Viresh Kumar2d135942014-01-07 07:10:12 +0530164
165 sysfs_remove_group(&policy->kobj, &stats_attr_group);
Viresh Kumar50941602015-01-06 21:09:07 +0530166 kfree(stats->time_in_state);
167 kfree(stats);
Viresh Kumara9aaf292015-01-13 11:34:00 +0530168 policy->stats = NULL;
steven finney98586ed2011-05-02 11:29:17 -0700169}
170
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200171void cpufreq_stats_create_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Viresh Kumara685c6d2015-01-06 21:09:11 +0530173 unsigned int i = 0, count = 0, ret = -ENOMEM;
Viresh Kumar50941602015-01-06 21:09:07 +0530174 struct cpufreq_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 unsigned int alloc_size;
Viresh Kumar55d85292017-04-25 15:57:15 +0530176 struct cpufreq_frequency_table *pos;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800177
Viresh Kumar55d85292017-04-25 15:57:15 +0530178 count = cpufreq_table_count_valid_entries(policy);
179 if (!count)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200180 return;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800181
Viresh Kumarb8c67442015-01-06 21:09:01 +0530182 /* stats already initialized */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530183 if (policy->stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200184 return;
Viresh Kumarb8c67442015-01-06 21:09:01 +0530185
Viresh Kumar50941602015-01-06 21:09:07 +0530186 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530187 if (!stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200188 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200190 alloc_size = count * sizeof(int) + count * sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 alloc_size += count * count * sizeof(int);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530193
194 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
Viresh Kumar50941602015-01-06 21:09:07 +0530195 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530196 if (!stats->time_in_state)
197 goto free_stat;
198
Viresh Kumar50941602015-01-06 21:09:07 +0530199 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Viresh Kumar50941602015-01-06 21:09:07 +0530201 stats->trans_table = stats->freq_table + count;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530202
203 stats->max_state = count;
204
205 /* Find valid-unique entries */
Viresh Kumar55d85292017-04-25 15:57:15 +0530206 cpufreq_for_each_valid_entry(pos, policy->freq_table)
Viresh Kumar50941602015-01-06 21:09:07 +0530207 if (freq_table_get_index(stats, pos->frequency) == -1)
208 stats->freq_table[i++] = pos->frequency;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530209
Viresh Kumar490285c2015-01-06 21:09:15 +0530210 stats->state_num = i;
Viresh Kumar50941602015-01-06 21:09:07 +0530211 stats->last_time = get_jiffies_64();
212 stats->last_index = freq_table_get_index(stats, policy->cur);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530213
214 policy->stats = stats;
215 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
216 if (!ret)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200217 return;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530218
219 /* We failed, release resources */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530220 policy->stats = NULL;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530221 kfree(stats->time_in_state);
222free_stat:
223 kfree(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200226void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
227 unsigned int new_freq)
Viresh Kumarb3f9ff82014-01-07 07:10:13 +0530228{
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200229 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int old_index, new_index;
231
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200232 if (!stats) {
Viresh Kumara9aaf292015-01-13 11:34:00 +0530233 pr_debug("%s: No stats found\n", __func__);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200234 return;
Viresh Kumara9aaf292015-01-13 11:34:00 +0530235 }
236
Viresh Kumar50941602015-01-06 21:09:07 +0530237 old_index = stats->last_index;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200238 new_index = freq_table_get_index(stats, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Viresh Kumar50941602015-01-06 21:09:07 +0530240 /* We can't do stats->time_in_state[-1]= .. */
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200241 if (old_index == -1 || new_index == -1 || old_index == new_index)
242 return;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800243
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++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}