blob: f570ead624547e111d74911e722b31aabcfce117 [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 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200121 if (len >= PAGE_SIZE)
122 return PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return len;
124}
Viresh Kumardf18e502013-02-04 11:38:52 +0000125cpufreq_freq_attr_ro(trans_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Viresh Kumardf18e502013-02-04 11:38:52 +0000127cpufreq_freq_attr_ro(total_trans);
128cpufreq_freq_attr_ro(time_in_state);
Markus Mayeree7930e2016-11-07 10:02:23 -0800129cpufreq_freq_attr_wo(reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131static struct attribute *default_attrs[] = {
Viresh Kumardf18e502013-02-04 11:38:52 +0000132 &total_trans.attr,
133 &time_in_state.attr,
Markus Mayeree7930e2016-11-07 10:02:23 -0800134 &reset.attr,
Viresh Kumardf18e502013-02-04 11:38:52 +0000135 &trans_table.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 NULL
137};
138static struct attribute_group stats_attr_group = {
139 .attrs = default_attrs,
140 .name = "stats"
141};
142
Viresh Kumar50941602015-01-06 21:09:07 +0530143static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int index;
Viresh Kumar50941602015-01-06 21:09:07 +0530146 for (index = 0; index < stats->max_state; index++)
147 if (stats->freq_table[index] == freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return index;
149 return -1;
150}
151
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200152void cpufreq_stats_free_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Viresh Kumar50941602015-01-06 21:09:07 +0530154 struct cpufreq_stats *stats = policy->stats;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000155
Viresh Kumara9aaf292015-01-13 11:34:00 +0530156 /* Already freed */
Viresh Kumar50941602015-01-06 21:09:07 +0530157 if (!stats)
Viresh Kumar2d135942014-01-07 07:10:12 +0530158 return;
159
Viresh Kumar50941602015-01-06 21:09:07 +0530160 pr_debug("%s: Free stats table\n", __func__);
Viresh Kumar2d135942014-01-07 07:10:12 +0530161
162 sysfs_remove_group(&policy->kobj, &stats_attr_group);
Viresh Kumar50941602015-01-06 21:09:07 +0530163 kfree(stats->time_in_state);
164 kfree(stats);
Viresh Kumara9aaf292015-01-13 11:34:00 +0530165 policy->stats = NULL;
steven finney98586ed2011-05-02 11:29:17 -0700166}
167
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200168void cpufreq_stats_create_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Viresh Kumara685c6d2015-01-06 21:09:11 +0530170 unsigned int i = 0, count = 0, ret = -ENOMEM;
Viresh Kumar50941602015-01-06 21:09:07 +0530171 struct cpufreq_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int alloc_size;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300173 struct cpufreq_frequency_table *pos, *table;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800174
Viresh Kumara685c6d2015-01-06 21:09:11 +0530175 /* We need cpufreq table for creating stats table */
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530176 table = policy->freq_table;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800177 if (unlikely(!table))
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 Kumara685c6d2015-01-06 21:09:11 +0530188 /* Find total allocation size */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300189 cpufreq_for_each_valid_entry(pos, table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200192 alloc_size = count * sizeof(int) + count * sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 alloc_size += count * count * sizeof(int);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530195
196 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
Viresh Kumar50941602015-01-06 21:09:07 +0530197 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530198 if (!stats->time_in_state)
199 goto free_stat;
200
Viresh Kumar50941602015-01-06 21:09:07 +0530201 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Viresh Kumar50941602015-01-06 21:09:07 +0530203 stats->trans_table = stats->freq_table + count;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530204
205 stats->max_state = count;
206
207 /* Find valid-unique entries */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300208 cpufreq_for_each_valid_entry(pos, table)
Viresh Kumar50941602015-01-06 21:09:07 +0530209 if (freq_table_get_index(stats, pos->frequency) == -1)
210 stats->freq_table[i++] = pos->frequency;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530211
Viresh Kumar490285c2015-01-06 21:09:15 +0530212 stats->state_num = i;
Viresh Kumar50941602015-01-06 21:09:07 +0530213 stats->last_time = get_jiffies_64();
214 stats->last_index = freq_table_get_index(stats, policy->cur);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530215
216 policy->stats = stats;
217 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
218 if (!ret)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200219 return;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530220
221 /* We failed, release resources */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530222 policy->stats = NULL;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530223 kfree(stats->time_in_state);
224free_stat:
225 kfree(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200228void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
229 unsigned int new_freq)
Viresh Kumarb3f9ff82014-01-07 07:10:13 +0530230{
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200231 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int old_index, new_index;
233
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200234 if (!stats) {
Viresh Kumara9aaf292015-01-13 11:34:00 +0530235 pr_debug("%s: No stats found\n", __func__);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200236 return;
Viresh Kumara9aaf292015-01-13 11:34:00 +0530237 }
238
Viresh Kumar50941602015-01-06 21:09:07 +0530239 old_index = stats->last_index;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200240 new_index = freq_table_get_index(stats, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Viresh Kumar50941602015-01-06 21:09:07 +0530242 /* We can't do stats->time_in_state[-1]= .. */
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200243 if (old_index == -1 || new_index == -1 || old_index == new_index)
244 return;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800245
Viresh Kumare7347692015-01-06 21:09:14 +0530246 cpufreq_stats_update(stats);
247
Viresh Kumar50941602015-01-06 21:09:07 +0530248 stats->last_index = new_index;
Viresh Kumar50941602015-01-06 21:09:07 +0530249 stats->trans_table[old_index * stats->max_state + new_index]++;
Viresh Kumar50941602015-01-06 21:09:07 +0530250 stats->total_trans++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}