Merge tag 'mediatek-drm-fixes-5.3' of https://github.com/ckhu-mediatek/linux.git...
[linux-2.6-block.git] / drivers / cpufreq / cpufreq_stats.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * drivers/cpufreq/cpufreq_stats.c
4 *
5 * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
0a829c5a 6 * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
1da177e4
LT
7 */
8
1da177e4 9#include <linux/cpu.h>
1da177e4 10#include <linux/cpufreq.h>
5c720d37 11#include <linux/module.h>
5ff0a268 12#include <linux/slab.h>
1da177e4 13
1da177e4 14
1da177e4 15struct cpufreq_stats {
1da177e4 16 unsigned int total_trans;
bb176f7d 17 unsigned long long last_time;
1da177e4
LT
18 unsigned int max_state;
19 unsigned int state_num;
20 unsigned int last_index;
1e7586a1 21 u64 *time_in_state;
fcccc5c8 22 spinlock_t lock;
1da177e4 23 unsigned int *freq_table;
1da177e4 24 unsigned int *trans_table;
1da177e4
LT
25};
26
d476ec4f 27static void cpufreq_stats_update(struct cpufreq_stats *stats)
1da177e4 28{
9531347c 29 unsigned long long cur_time = get_jiffies_64();
58f1df25 30
c960f9b2 31 stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
50941607 32 stats->last_time = cur_time;
1da177e4
LT
33}
34
ee7930ee
MM
35static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
36{
37 unsigned int count = stats->max_state;
38
fcccc5c8 39 spin_lock(&stats->lock);
ee7930ee 40 memset(stats->time_in_state, 0, count * sizeof(u64));
ee7930ee 41 memset(stats->trans_table, 0, count * count * sizeof(int));
ee7930ee
MM
42 stats->last_time = get_jiffies_64();
43 stats->total_trans = 0;
fcccc5c8 44 spin_unlock(&stats->lock);
ee7930ee
MM
45}
46
0a829c5a 47static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
1da177e4 48{
a9aaf291 49 return sprintf(buf, "%d\n", policy->stats->total_trans);
1da177e4 50}
10b81821 51cpufreq_freq_attr_ro(total_trans);
1da177e4 52
0a829c5a 53static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
1da177e4 54{
50941607 55 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
56 ssize_t len = 0;
57 int i;
a9aaf291 58
1aefc75b
RW
59 if (policy->fast_switch_enabled)
60 return 0;
61
fcccc5c8 62 spin_lock(&stats->lock);
50941607 63 cpufreq_stats_update(stats);
fcccc5c8 64 spin_unlock(&stats->lock);
9795607d 65
50941607
VK
66 for (i = 0; i < stats->state_num; i++) {
67 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
0a829c5a 68 (unsigned long long)
50941607 69 jiffies_64_to_clock_t(stats->time_in_state[i]));
1da177e4
LT
70 }
71 return len;
72}
10b81821 73cpufreq_freq_attr_ro(time_in_state);
1da177e4 74
ee7930ee
MM
75static 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}
10b81821 82cpufreq_freq_attr_wo(reset);
ee7930ee 83
0a829c5a 84static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
1da177e4 85{
50941607 86 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
87 ssize_t len = 0;
88 int i, j;
89
1aefc75b
RW
90 if (policy->fast_switch_enabled)
91 return 0;
92
58f1df25
VP
93 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
94 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
50941607 95 for (i = 0; i < stats->state_num; i++) {
58f1df25
VP
96 if (len >= PAGE_SIZE)
97 break;
98 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
50941607 99 stats->freq_table[i]);
58f1df25
VP
100 }
101 if (len >= PAGE_SIZE)
25aca347 102 return PAGE_SIZE;
58f1df25
VP
103
104 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
105
50941607 106 for (i = 0; i < stats->state_num; i++) {
1da177e4
LT
107 if (len >= PAGE_SIZE)
108 break;
58f1df25
VP
109
110 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
50941607 111 stats->freq_table[i]);
1da177e4 112
50941607 113 for (j = 0; j < stats->state_num; j++) {
1da177e4
LT
114 if (len >= PAGE_SIZE)
115 break;
58f1df25 116 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
50941607 117 stats->trans_table[i*stats->max_state+j]);
1da177e4 118 }
25aca347
CEB
119 if (len >= PAGE_SIZE)
120 break;
1da177e4
LT
121 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
122 }
f7bc9b20
GS
123
124 if (len >= PAGE_SIZE) {
125 pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
126 return -EFBIG;
127 }
1da177e4
LT
128 return len;
129}
df18e504 130cpufreq_freq_attr_ro(trans_table);
1da177e4 131
1da177e4 132static struct attribute *default_attrs[] = {
df18e504
VK
133 &total_trans.attr,
134 &time_in_state.attr,
ee7930ee 135 &reset.attr,
df18e504 136 &trans_table.attr,
1da177e4
LT
137 NULL
138};
402202e8 139static const struct attribute_group stats_attr_group = {
1da177e4
LT
140 .attrs = default_attrs,
141 .name = "stats"
142};
143
50941607 144static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
1da177e4
LT
145{
146 int index;
50941607
VK
147 for (index = 0; index < stats->max_state; index++)
148 if (stats->freq_table[index] == freq)
1da177e4
LT
149 return index;
150 return -1;
151}
152
1aefc75b 153void cpufreq_stats_free_table(struct cpufreq_policy *policy)
1da177e4 154{
50941607 155 struct cpufreq_stats *stats = policy->stats;
b8eed8af 156
a9aaf291 157 /* Already freed */
50941607 158 if (!stats)
2d13594d
VK
159 return;
160
50941607 161 pr_debug("%s: Free stats table\n", __func__);
2d13594d
VK
162
163 sysfs_remove_group(&policy->kobj, &stats_attr_group);
50941607
VK
164 kfree(stats->time_in_state);
165 kfree(stats);
a9aaf291 166 policy->stats = NULL;
98586ed8 167}
168
1aefc75b 169void cpufreq_stats_create_table(struct cpufreq_policy *policy)
1da177e4 170{
a685c6d0 171 unsigned int i = 0, count = 0, ret = -ENOMEM;
50941607 172 struct cpufreq_stats *stats;
1da177e4 173 unsigned int alloc_size;
55d85293 174 struct cpufreq_frequency_table *pos;
ad4c2302 175
55d85293
VK
176 count = cpufreq_table_count_valid_entries(policy);
177 if (!count)
1aefc75b 178 return;
ad4c2302 179
b8c67448 180 /* stats already initialized */
a9aaf291 181 if (policy->stats)
1aefc75b 182 return;
b8c67448 183
50941607 184 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
a685c6d0 185 if (!stats)
1aefc75b 186 return;
1da177e4 187
1e7586a1 188 alloc_size = count * sizeof(int) + count * sizeof(u64);
1da177e4 189
1da177e4 190 alloc_size += count * count * sizeof(int);
a685c6d0
VK
191
192 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
50941607 193 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
a685c6d0
VK
194 if (!stats->time_in_state)
195 goto free_stat;
196
50941607 197 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
1da177e4 198
50941607 199 stats->trans_table = stats->freq_table + count;
a685c6d0
VK
200
201 stats->max_state = count;
202
203 /* Find valid-unique entries */
55d85293 204 cpufreq_for_each_valid_entry(pos, policy->freq_table)
50941607
VK
205 if (freq_table_get_index(stats, pos->frequency) == -1)
206 stats->freq_table[i++] = pos->frequency;
a685c6d0 207
490285c6 208 stats->state_num = i;
50941607
VK
209 stats->last_time = get_jiffies_64();
210 stats->last_index = freq_table_get_index(stats, policy->cur);
fcccc5c8 211 spin_lock_init(&stats->lock);
a685c6d0
VK
212
213 policy->stats = stats;
214 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
215 if (!ret)
1aefc75b 216 return;
a685c6d0
VK
217
218 /* We failed, release resources */
a9aaf291 219 policy->stats = NULL;
a685c6d0
VK
220 kfree(stats->time_in_state);
221free_stat:
222 kfree(stats);
b3f9ff88
VK
223}
224
1aefc75b
RW
225void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
226 unsigned int new_freq)
1da177e4 227{
1aefc75b 228 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
229 int old_index, new_index;
230
1aefc75b 231 if (!stats) {
a9aaf291 232 pr_debug("%s: No stats found\n", __func__);
1aefc75b 233 return;
a9aaf291
VK
234 }
235
50941607 236 old_index = stats->last_index;
1aefc75b 237 new_index = freq_table_get_index(stats, new_freq);
1da177e4 238
50941607 239 /* We can't do stats->time_in_state[-1]= .. */
1aefc75b
RW
240 if (old_index == -1 || new_index == -1 || old_index == new_index)
241 return;
8edc59d9 242
fcccc5c8 243 spin_lock(&stats->lock);
e7347694
VK
244 cpufreq_stats_update(stats);
245
50941607 246 stats->last_index = new_index;
50941607 247 stats->trans_table[old_index * stats->max_state + new_index]++;
50941607 248 stats->total_trans++;
fcccc5c8 249 spin_unlock(&stats->lock);
1da177e4 250}