Merge branch 'pm-cpufreq-ondemand' into pm-cpufreq
[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/kernel.h>
5a0e3ad6 13#include <linux/slab.h>
1da177e4
LT
14#include <linux/cpu.h>
15#include <linux/sysfs.h>
16#include <linux/cpufreq.h>
5c720d37 17#include <linux/module.h>
1da177e4
LT
18#include <linux/jiffies.h>
19#include <linux/percpu.h>
20#include <linux/kobject.h>
21#include <linux/spinlock.h>
c32b6b8e 22#include <linux/notifier.h>
58f1df25 23#include <asm/cputime.h>
1da177e4
LT
24
25static spinlock_t cpufreq_stats_lock;
26
1da177e4
LT
27struct cpufreq_stats {
28 unsigned int cpu;
29 unsigned int total_trans;
bb176f7d 30 unsigned long long last_time;
1da177e4
LT
31 unsigned int max_state;
32 unsigned int state_num;
33 unsigned int last_index;
1e7586a1 34 u64 *time_in_state;
1da177e4
LT
35 unsigned int *freq_table;
36#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
37 unsigned int *trans_table;
38#endif
39};
40
7a6aedfa 41static DEFINE_PER_CPU(struct cpufreq_stats *, cpufreq_stats_table);
1da177e4
LT
42
43struct cpufreq_stats_attribute {
44 struct attribute attr;
45 ssize_t(*show) (struct cpufreq_stats *, char *);
46};
47
0a829c5a 48static int cpufreq_stats_update(unsigned int cpu)
1da177e4
LT
49{
50 struct cpufreq_stats *stat;
58f1df25
VP
51 unsigned long long cur_time;
52
53 cur_time = get_jiffies_64();
1da177e4 54 spin_lock(&cpufreq_stats_lock);
7a6aedfa 55 stat = per_cpu(cpufreq_stats_table, cpu);
1da177e4 56 if (stat->time_in_state)
64861634
MS
57 stat->time_in_state[stat->last_index] +=
58 cur_time - stat->last_time;
58f1df25 59 stat->last_time = cur_time;
1da177e4
LT
60 spin_unlock(&cpufreq_stats_lock);
61 return 0;
62}
63
0a829c5a 64static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
1da177e4 65{
7a6aedfa 66 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
511e9ee1 67 if (!stat)
1da177e4
LT
68 return 0;
69 return sprintf(buf, "%d\n",
7a6aedfa 70 per_cpu(cpufreq_stats_table, stat->cpu)->total_trans);
1da177e4
LT
71}
72
0a829c5a 73static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
1da177e4
LT
74{
75 ssize_t len = 0;
76 int i;
7a6aedfa 77 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
511e9ee1 78 if (!stat)
1da177e4
LT
79 return 0;
80 cpufreq_stats_update(stat->cpu);
81 for (i = 0; i < stat->state_num; i++) {
32ee8c3e 82 len += sprintf(buf + len, "%u %llu\n", stat->freq_table[i],
0a829c5a
DJ
83 (unsigned long long)
84 cputime64_to_clock_t(stat->time_in_state[i]));
1da177e4
LT
85 }
86 return len;
87}
88
89#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
0a829c5a 90static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
1da177e4
LT
91{
92 ssize_t len = 0;
93 int i, j;
94
7a6aedfa 95 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
511e9ee1 96 if (!stat)
1da177e4
LT
97 return 0;
98 cpufreq_stats_update(stat->cpu);
58f1df25
VP
99 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
100 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
101 for (i = 0; i < stat->state_num; i++) {
102 if (len >= PAGE_SIZE)
103 break;
104 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
105 stat->freq_table[i]);
106 }
107 if (len >= PAGE_SIZE)
25aca347 108 return PAGE_SIZE;
58f1df25
VP
109
110 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
111
1da177e4
LT
112 for (i = 0; i < stat->state_num; i++) {
113 if (len >= PAGE_SIZE)
114 break;
58f1df25
VP
115
116 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
1da177e4
LT
117 stat->freq_table[i]);
118
bb176f7d 119 for (j = 0; j < stat->state_num; j++) {
1da177e4
LT
120 if (len >= PAGE_SIZE)
121 break;
58f1df25 122 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
1da177e4
LT
123 stat->trans_table[i*stat->max_state+j]);
124 }
25aca347
CEB
125 if (len >= PAGE_SIZE)
126 break;
1da177e4
LT
127 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
128 }
25aca347
CEB
129 if (len >= PAGE_SIZE)
130 return PAGE_SIZE;
1da177e4
LT
131 return len;
132}
df18e504 133cpufreq_freq_attr_ro(trans_table);
1da177e4
LT
134#endif
135
df18e504
VK
136cpufreq_freq_attr_ro(total_trans);
137cpufreq_freq_attr_ro(time_in_state);
1da177e4
LT
138
139static struct attribute *default_attrs[] = {
df18e504
VK
140 &total_trans.attr,
141 &time_in_state.attr,
1da177e4 142#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
df18e504 143 &trans_table.attr,
1da177e4
LT
144#endif
145 NULL
146};
147static struct attribute_group stats_attr_group = {
148 .attrs = default_attrs,
149 .name = "stats"
150};
151
0a829c5a 152static int freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
1da177e4
LT
153{
154 int index;
155 for (index = 0; index < stat->max_state; index++)
156 if (stat->freq_table[index] == freq)
157 return index;
158 return -1;
159}
160
98586ed8 161/* should be called late in the CPU removal sequence so that the stats
162 * memory is still available in case someone tries to use it.
163 */
a3323473 164static void cpufreq_stats_free_table(unsigned int cpu)
1da177e4 165{
7a6aedfa 166 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, cpu);
b8eed8af 167
1da177e4 168 if (stat) {
b8eed8af 169 pr_debug("%s: Free stat table\n", __func__);
1da177e4
LT
170 kfree(stat->time_in_state);
171 kfree(stat);
b8eed8af 172 per_cpu(cpufreq_stats_table, cpu) = NULL;
1da177e4 173 }
98586ed8 174}
175
176/* must be called early in the CPU removal sequence (before
177 * cpufreq_remove_dev) so that policy is still valid.
178 */
179static void cpufreq_stats_free_sysfs(unsigned int cpu)
180{
181 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
633d47d6 182
187da1d9 183 if (!policy)
633d47d6
DB
184 return;
185
187da1d9 186 if (!cpufreq_frequency_get_table(cpu))
187 goto put_ref;
188
189 if (!policy_is_shared(policy)) {
b8eed8af 190 pr_debug("%s: Free sysfs stat\n", __func__);
98586ed8 191 sysfs_remove_group(&policy->kobj, &stats_attr_group);
b8eed8af 192 }
187da1d9 193
194put_ref:
195 cpufreq_cpu_put(policy);
1da177e4
LT
196}
197
0a829c5a 198static int cpufreq_stats_create_table(struct cpufreq_policy *policy,
1da177e4
LT
199 struct cpufreq_frequency_table *table)
200{
201 unsigned int i, j, count = 0, ret = 0;
202 struct cpufreq_stats *stat;
203 struct cpufreq_policy *data;
204 unsigned int alloc_size;
205 unsigned int cpu = policy->cpu;
7a6aedfa 206 if (per_cpu(cpufreq_stats_table, cpu))
1da177e4 207 return -EBUSY;
0a829c5a
DJ
208 stat = kzalloc(sizeof(struct cpufreq_stats), GFP_KERNEL);
209 if ((stat) == NULL)
1da177e4 210 return -ENOMEM;
1da177e4
LT
211
212 data = cpufreq_cpu_get(cpu);
bc7b26fd
DJ
213 if (data == NULL) {
214 ret = -EINVAL;
215 goto error_get_fail;
216 }
217
0a829c5a
DJ
218 ret = sysfs_create_group(&data->kobj, &stats_attr_group);
219 if (ret)
1da177e4
LT
220 goto error_out;
221
222 stat->cpu = cpu;
7a6aedfa 223 per_cpu(cpufreq_stats_table, cpu) = stat;
1da177e4 224
0a829c5a 225 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
1da177e4
LT
226 unsigned int freq = table[i].frequency;
227 if (freq == CPUFREQ_ENTRY_INVALID)
228 continue;
229 count++;
230 }
231
1e7586a1 232 alloc_size = count * sizeof(int) + count * sizeof(u64);
1da177e4
LT
233
234#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
235 alloc_size += count * count * sizeof(int);
236#endif
237 stat->max_state = count;
e98df50c 238 stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
1da177e4
LT
239 if (!stat->time_in_state) {
240 ret = -ENOMEM;
241 goto error_out;
242 }
1da177e4
LT
243 stat->freq_table = (unsigned int *)(stat->time_in_state + count);
244
245#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
246 stat->trans_table = stat->freq_table + count;
247#endif
248 j = 0;
249 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
250 unsigned int freq = table[i].frequency;
251 if (freq == CPUFREQ_ENTRY_INVALID)
252 continue;
253 if (freq_table_get_index(stat, freq) == -1)
254 stat->freq_table[j++] = freq;
255 }
256 stat->state_num = j;
257 spin_lock(&cpufreq_stats_lock);
58f1df25 258 stat->last_time = get_jiffies_64();
1da177e4
LT
259 stat->last_index = freq_table_get_index(stat, policy->cur);
260 spin_unlock(&cpufreq_stats_lock);
261 cpufreq_cpu_put(data);
262 return 0;
263error_out:
264 cpufreq_cpu_put(data);
b7fb358c 265error_get_fail:
1da177e4 266 kfree(stat);
7a6aedfa 267 per_cpu(cpufreq_stats_table, cpu) = NULL;
1da177e4
LT
268 return ret;
269}
270
b8eed8af
VK
271static void cpufreq_stats_update_policy_cpu(struct cpufreq_policy *policy)
272{
273 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table,
274 policy->last_cpu);
275
276 pr_debug("Updating stats_table for new_cpu %u from last_cpu %u\n",
277 policy->cpu, policy->last_cpu);
278 per_cpu(cpufreq_stats_table, policy->cpu) = per_cpu(cpufreq_stats_table,
279 policy->last_cpu);
280 per_cpu(cpufreq_stats_table, policy->last_cpu) = NULL;
281 stat->cpu = policy->cpu;
282}
283
0a829c5a
DJ
284static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
285 unsigned long val, void *data)
1da177e4
LT
286{
287 int ret;
288 struct cpufreq_policy *policy = data;
289 struct cpufreq_frequency_table *table;
290 unsigned int cpu = policy->cpu;
b8eed8af
VK
291
292 if (val == CPUFREQ_UPDATE_POLICY_CPU) {
293 cpufreq_stats_update_policy_cpu(policy);
294 return 0;
295 }
296
1da177e4
LT
297 if (val != CPUFREQ_NOTIFY)
298 return 0;
299 table = cpufreq_frequency_get_table(cpu);
300 if (!table)
301 return 0;
0a829c5a
DJ
302 ret = cpufreq_stats_create_table(policy, table);
303 if (ret)
1da177e4
LT
304 return ret;
305 return 0;
306}
307
0a829c5a
DJ
308static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
309 unsigned long val, void *data)
1da177e4
LT
310{
311 struct cpufreq_freqs *freq = data;
312 struct cpufreq_stats *stat;
313 int old_index, new_index;
314
315 if (val != CPUFREQ_POSTCHANGE)
316 return 0;
317
7a6aedfa 318 stat = per_cpu(cpufreq_stats_table, freq->cpu);
1da177e4
LT
319 if (!stat)
320 return 0;
8edc59d9 321
6501faf8 322 old_index = stat->last_index;
1da177e4
LT
323 new_index = freq_table_get_index(stat, freq->new);
324
46a310b8
KRW
325 /* We can't do stat->time_in_state[-1]= .. */
326 if (old_index == -1 || new_index == -1)
1da177e4
LT
327 return 0;
328
46a310b8
KRW
329 cpufreq_stats_update(freq->cpu);
330
331 if (old_index == new_index)
8edc59d9
VP
332 return 0;
333
1da177e4
LT
334 spin_lock(&cpufreq_stats_lock);
335 stat->last_index = new_index;
336#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
337 stat->trans_table[old_index * stat->max_state + new_index]++;
338#endif
339 stat->total_trans++;
340 spin_unlock(&cpufreq_stats_lock);
341 return 0;
342}
343
2760984f 344static int cpufreq_stat_cpu_callback(struct notifier_block *nfb,
55395ae7
SS
345 unsigned long action,
346 void *hcpu)
c32b6b8e
AR
347{
348 unsigned int cpu = (unsigned long)hcpu;
349
350 switch (action) {
98586ed8 351 case CPU_DOWN_PREPARE:
352 cpufreq_stats_free_sysfs(cpu);
353 break;
c32b6b8e
AR
354 case CPU_DEAD:
355 cpufreq_stats_free_table(cpu);
356 break;
357 }
358 return NOTIFY_OK;
359}
360
98586ed8 361/* priority=1 so this will get called before cpufreq_remove_dev */
469057d5 362static struct notifier_block cpufreq_stat_cpu_notifier __refdata = {
c32b6b8e 363 .notifier_call = cpufreq_stat_cpu_callback,
98586ed8 364 .priority = 1,
c32b6b8e
AR
365};
366
1da177e4
LT
367static struct notifier_block notifier_policy_block = {
368 .notifier_call = cpufreq_stat_notifier_policy
369};
370
371static struct notifier_block notifier_trans_block = {
372 .notifier_call = cpufreq_stat_notifier_trans
373};
374
0a829c5a 375static int __init cpufreq_stats_init(void)
1da177e4
LT
376{
377 int ret;
378 unsigned int cpu;
c32b6b8e 379
1da177e4 380 spin_lock_init(&cpufreq_stats_lock);
0a829c5a
DJ
381 ret = cpufreq_register_notifier(&notifier_policy_block,
382 CPUFREQ_POLICY_NOTIFIER);
383 if (ret)
1da177e4
LT
384 return ret;
385
56836fb4 386 register_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
56836fb4 387
0a829c5a
DJ
388 ret = cpufreq_register_notifier(&notifier_trans_block,
389 CPUFREQ_TRANSITION_NOTIFIER);
390 if (ret) {
1da177e4
LT
391 cpufreq_unregister_notifier(&notifier_policy_block,
392 CPUFREQ_POLICY_NOTIFIER);
56836fb4
KK
393 unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
394 for_each_online_cpu(cpu)
395 cpufreq_stats_free_table(cpu);
1da177e4
LT
396 return ret;
397 }
398
1da177e4
LT
399 return 0;
400}
0a829c5a 401static void __exit cpufreq_stats_exit(void)
1da177e4
LT
402{
403 unsigned int cpu;
c32b6b8e 404
1da177e4
LT
405 cpufreq_unregister_notifier(&notifier_policy_block,
406 CPUFREQ_POLICY_NOTIFIER);
407 cpufreq_unregister_notifier(&notifier_trans_block,
408 CPUFREQ_TRANSITION_NOTIFIER);
65edc68c 409 unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
c32b6b8e 410 for_each_online_cpu(cpu) {
55395ae7 411 cpufreq_stats_free_table(cpu);
13f06753 412 cpufreq_stats_free_sysfs(cpu);
c32b6b8e 413 }
1da177e4
LT
414}
415
0a829c5a
DJ
416MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
417MODULE_DESCRIPTION("'cpufreq_stats' - A driver to export cpufreq stats "
e08f5f5b 418 "through sysfs filesystem");
0a829c5a 419MODULE_LICENSE("GPL");
1da177e4
LT
420
421module_init(cpufreq_stats_init);
422module_exit(cpufreq_stats_exit);