drm/amd/powerplay: fix bug that get wrong polaris evv voltage.
[linux-2.6-block.git] / drivers / cpufreq / cpufreq_stats.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/cpufreq/cpufreq_stats.c
3 *
4 * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
0a829c5a 5 * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
1da177e4
LT
6 *
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
1da177e4 12#include <linux/cpu.h>
1da177e4 13#include <linux/cpufreq.h>
5c720d37 14#include <linux/module.h>
5ff0a268 15#include <linux/slab.h>
bfc3f028 16#include <linux/cputime.h>
1da177e4
LT
17
18static spinlock_t cpufreq_stats_lock;
19
1da177e4 20struct cpufreq_stats {
1da177e4 21 unsigned int total_trans;
bb176f7d 22 unsigned long long last_time;
1da177e4
LT
23 unsigned int max_state;
24 unsigned int state_num;
25 unsigned int last_index;
1e7586a1 26 u64 *time_in_state;
1da177e4
LT
27 unsigned int *freq_table;
28#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
29 unsigned int *trans_table;
30#endif
31};
32
50941607 33static int cpufreq_stats_update(struct cpufreq_stats *stats)
1da177e4 34{
9531347c 35 unsigned long long cur_time = get_jiffies_64();
58f1df25 36
1da177e4 37 spin_lock(&cpufreq_stats_lock);
c960f9b2 38 stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
50941607 39 stats->last_time = cur_time;
1da177e4
LT
40 spin_unlock(&cpufreq_stats_lock);
41 return 0;
42}
43
0a829c5a 44static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
1da177e4 45{
a9aaf291 46 return sprintf(buf, "%d\n", policy->stats->total_trans);
1da177e4
LT
47}
48
0a829c5a 49static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
1da177e4 50{
50941607 51 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
52 ssize_t len = 0;
53 int i;
a9aaf291 54
50941607
VK
55 cpufreq_stats_update(stats);
56 for (i = 0; i < stats->state_num; i++) {
57 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
0a829c5a 58 (unsigned long long)
50941607 59 jiffies_64_to_clock_t(stats->time_in_state[i]));
1da177e4
LT
60 }
61 return len;
62}
63
64#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
0a829c5a 65static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
1da177e4 66{
50941607 67 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
68 ssize_t len = 0;
69 int i, j;
70
58f1df25
VP
71 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
72 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
50941607 73 for (i = 0; i < stats->state_num; i++) {
58f1df25
VP
74 if (len >= PAGE_SIZE)
75 break;
76 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
50941607 77 stats->freq_table[i]);
58f1df25
VP
78 }
79 if (len >= PAGE_SIZE)
25aca347 80 return PAGE_SIZE;
58f1df25
VP
81
82 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
83
50941607 84 for (i = 0; i < stats->state_num; i++) {
1da177e4
LT
85 if (len >= PAGE_SIZE)
86 break;
58f1df25
VP
87
88 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
50941607 89 stats->freq_table[i]);
1da177e4 90
50941607 91 for (j = 0; j < stats->state_num; j++) {
1da177e4
LT
92 if (len >= PAGE_SIZE)
93 break;
58f1df25 94 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
50941607 95 stats->trans_table[i*stats->max_state+j]);
1da177e4 96 }
25aca347
CEB
97 if (len >= PAGE_SIZE)
98 break;
1da177e4
LT
99 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
100 }
25aca347
CEB
101 if (len >= PAGE_SIZE)
102 return PAGE_SIZE;
1da177e4
LT
103 return len;
104}
df18e504 105cpufreq_freq_attr_ro(trans_table);
1da177e4
LT
106#endif
107
df18e504
VK
108cpufreq_freq_attr_ro(total_trans);
109cpufreq_freq_attr_ro(time_in_state);
1da177e4
LT
110
111static struct attribute *default_attrs[] = {
df18e504
VK
112 &total_trans.attr,
113 &time_in_state.attr,
1da177e4 114#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
df18e504 115 &trans_table.attr,
1da177e4
LT
116#endif
117 NULL
118};
119static struct attribute_group stats_attr_group = {
120 .attrs = default_attrs,
121 .name = "stats"
122};
123
50941607 124static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
1da177e4
LT
125{
126 int index;
50941607
VK
127 for (index = 0; index < stats->max_state; index++)
128 if (stats->freq_table[index] == freq)
1da177e4
LT
129 return index;
130 return -1;
131}
132
2d13594d 133static void __cpufreq_stats_free_table(struct cpufreq_policy *policy)
1da177e4 134{
50941607 135 struct cpufreq_stats *stats = policy->stats;
b8eed8af 136
a9aaf291 137 /* Already freed */
50941607 138 if (!stats)
2d13594d
VK
139 return;
140
50941607 141 pr_debug("%s: Free stats table\n", __func__);
2d13594d
VK
142
143 sysfs_remove_group(&policy->kobj, &stats_attr_group);
50941607
VK
144 kfree(stats->time_in_state);
145 kfree(stats);
a9aaf291 146 policy->stats = NULL;
98586ed8 147}
148
2d13594d 149static void cpufreq_stats_free_table(unsigned int cpu)
98586ed8 150{
2d13594d 151 struct cpufreq_policy *policy;
633d47d6 152
2d13594d 153 policy = cpufreq_cpu_get(cpu);
187da1d9 154 if (!policy)
633d47d6
DB
155 return;
156
f93dbbbd 157 __cpufreq_stats_free_table(policy);
187da1d9 158
187da1d9 159 cpufreq_cpu_put(policy);
1da177e4
LT
160}
161
ad4c2302 162static int __cpufreq_stats_create_table(struct cpufreq_policy *policy)
1da177e4 163{
a685c6d0 164 unsigned int i = 0, count = 0, ret = -ENOMEM;
50941607 165 struct cpufreq_stats *stats;
1da177e4
LT
166 unsigned int alloc_size;
167 unsigned int cpu = policy->cpu;
041526f9 168 struct cpufreq_frequency_table *pos, *table;
ad4c2302 169
a685c6d0 170 /* We need cpufreq table for creating stats table */
ad4c2302
SK
171 table = cpufreq_frequency_get_table(cpu);
172 if (unlikely(!table))
173 return 0;
174
b8c67448 175 /* stats already initialized */
a9aaf291 176 if (policy->stats)
b8c67448
VK
177 return -EEXIST;
178
50941607 179 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
a685c6d0 180 if (!stats)
1da177e4 181 return -ENOMEM;
1da177e4 182
a685c6d0 183 /* Find total allocation size */
041526f9 184 cpufreq_for_each_valid_entry(pos, table)
1da177e4 185 count++;
1da177e4 186
1e7586a1 187 alloc_size = count * sizeof(int) + count * sizeof(u64);
1da177e4
LT
188
189#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
190 alloc_size += count * count * sizeof(int);
191#endif
a685c6d0
VK
192
193 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
50941607 194 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
a685c6d0
VK
195 if (!stats->time_in_state)
196 goto free_stat;
197
50941607 198 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
1da177e4
LT
199
200#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
50941607 201 stats->trans_table = stats->freq_table + count;
1da177e4 202#endif
a685c6d0
VK
203
204 stats->max_state = count;
205
206 /* Find valid-unique entries */
041526f9 207 cpufreq_for_each_valid_entry(pos, table)
50941607
VK
208 if (freq_table_get_index(stats, pos->frequency) == -1)
209 stats->freq_table[i++] = pos->frequency;
a685c6d0 210
490285c6 211 stats->state_num = i;
50941607
VK
212 stats->last_time = get_jiffies_64();
213 stats->last_index = freq_table_get_index(stats, policy->cur);
a685c6d0
VK
214
215 policy->stats = stats;
216 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
217 if (!ret)
218 return 0;
219
220 /* We failed, release resources */
a9aaf291 221 policy->stats = NULL;
a685c6d0
VK
222 kfree(stats->time_in_state);
223free_stat:
224 kfree(stats);
225
1da177e4
LT
226 return ret;
227}
228
b3f9ff88
VK
229static void cpufreq_stats_create_table(unsigned int cpu)
230{
231 struct cpufreq_policy *policy;
b3f9ff88
VK
232
233 /*
234 * "likely(!policy)" because normally cpufreq_stats will be registered
235 * before cpufreq driver
236 */
237 policy = cpufreq_cpu_get(cpu);
238 if (likely(!policy))
239 return;
240
ad4c2302 241 __cpufreq_stats_create_table(policy);
b3f9ff88
VK
242
243 cpufreq_cpu_put(policy);
244}
245
0a829c5a
DJ
246static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
247 unsigned long val, void *data)
1da177e4 248{
fcd7af91 249 int ret = 0;
1da177e4 250 struct cpufreq_policy *policy = data;
b8eed8af 251
fcd7af91 252 if (val == CPUFREQ_CREATE_POLICY)
ad4c2302 253 ret = __cpufreq_stats_create_table(policy);
2d13594d
VK
254 else if (val == CPUFREQ_REMOVE_POLICY)
255 __cpufreq_stats_free_table(policy);
fcd7af91
VK
256
257 return ret;
1da177e4
LT
258}
259
0a829c5a
DJ
260static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
261 unsigned long val, void *data)
1da177e4
LT
262{
263 struct cpufreq_freqs *freq = data;
a9aaf291 264 struct cpufreq_policy *policy = cpufreq_cpu_get(freq->cpu);
50941607 265 struct cpufreq_stats *stats;
1da177e4
LT
266 int old_index, new_index;
267
a9aaf291
VK
268 if (!policy) {
269 pr_err("%s: No policy found\n", __func__);
1da177e4 270 return 0;
a9aaf291 271 }
1da177e4 272
a9aaf291
VK
273 if (val != CPUFREQ_POSTCHANGE)
274 goto put_policy;
275
276 if (!policy->stats) {
277 pr_debug("%s: No stats found\n", __func__);
278 goto put_policy;
279 }
280
50941607 281 stats = policy->stats;
8edc59d9 282
50941607
VK
283 old_index = stats->last_index;
284 new_index = freq_table_get_index(stats, freq->new);
1da177e4 285
50941607 286 /* We can't do stats->time_in_state[-1]= .. */
46a310b8 287 if (old_index == -1 || new_index == -1)
a9aaf291 288 goto put_policy;
1da177e4 289
46a310b8 290 if (old_index == new_index)
a9aaf291 291 goto put_policy;
8edc59d9 292
e7347694
VK
293 cpufreq_stats_update(stats);
294
50941607 295 stats->last_index = new_index;
1da177e4 296#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
50941607 297 stats->trans_table[old_index * stats->max_state + new_index]++;
1da177e4 298#endif
50941607 299 stats->total_trans++;
a9aaf291
VK
300
301put_policy:
302 cpufreq_cpu_put(policy);
1da177e4
LT
303 return 0;
304}
305
306static struct notifier_block notifier_policy_block = {
307 .notifier_call = cpufreq_stat_notifier_policy
308};
309
310static struct notifier_block notifier_trans_block = {
311 .notifier_call = cpufreq_stat_notifier_trans
312};
313
0a829c5a 314static int __init cpufreq_stats_init(void)
1da177e4
LT
315{
316 int ret;
317 unsigned int cpu;
c32b6b8e 318
1da177e4 319 spin_lock_init(&cpufreq_stats_lock);
0a829c5a
DJ
320 ret = cpufreq_register_notifier(&notifier_policy_block,
321 CPUFREQ_POLICY_NOTIFIER);
322 if (ret)
1da177e4
LT
323 return ret;
324
b3f9ff88
VK
325 for_each_online_cpu(cpu)
326 cpufreq_stats_create_table(cpu);
327
0a829c5a
DJ
328 ret = cpufreq_register_notifier(&notifier_trans_block,
329 CPUFREQ_TRANSITION_NOTIFIER);
330 if (ret) {
1da177e4
LT
331 cpufreq_unregister_notifier(&notifier_policy_block,
332 CPUFREQ_POLICY_NOTIFIER);
56836fb4
KK
333 for_each_online_cpu(cpu)
334 cpufreq_stats_free_table(cpu);
1da177e4
LT
335 return ret;
336 }
337
1da177e4
LT
338 return 0;
339}
0a829c5a 340static void __exit cpufreq_stats_exit(void)
1da177e4
LT
341{
342 unsigned int cpu;
c32b6b8e 343
1da177e4
LT
344 cpufreq_unregister_notifier(&notifier_policy_block,
345 CPUFREQ_POLICY_NOTIFIER);
346 cpufreq_unregister_notifier(&notifier_trans_block,
347 CPUFREQ_TRANSITION_NOTIFIER);
2d13594d 348 for_each_online_cpu(cpu)
55395ae7 349 cpufreq_stats_free_table(cpu);
1da177e4
LT
350}
351
0a829c5a 352MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
00d0b294 353MODULE_DESCRIPTION("Export cpufreq stats via sysfs");
0a829c5a 354MODULE_LICENSE("GPL");
1da177e4
LT
355
356module_init(cpufreq_stats_init);
357module_exit(cpufreq_stats_exit);