Merge branch 'linux-linaro-lsk' into linux-linaro-lsk-android
[firefly-linux-kernel-4.4.55.git] / drivers / cpufreq / cpufreq_stats.c
1 /*
2  *  drivers/cpufreq/cpufreq_stats.c
3  *
4  *  Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
5  *  (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
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
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/cpu.h>
15 #include <linux/sysfs.h>
16 #include <linux/cpufreq.h>
17 #include <linux/module.h>
18 #include <linux/jiffies.h>
19 #include <linux/percpu.h>
20 #include <linux/kobject.h>
21 #include <linux/spinlock.h>
22 #include <linux/notifier.h>
23 #include <asm/cputime.h>
24 #include <asm/bL_switcher.h>
25
26 static spinlock_t cpufreq_stats_lock;
27
28 struct cpufreq_stats {
29         unsigned int cpu;
30         unsigned int total_trans;
31         unsigned long long  last_time;
32         unsigned int max_state;
33         unsigned int state_num;
34         unsigned int last_index;
35         u64 *time_in_state;
36         unsigned int *freq_table;
37 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
38         unsigned int *trans_table;
39 #endif
40 };
41
42 static DEFINE_PER_CPU(struct cpufreq_stats *, cpufreq_stats_table);
43
44 struct cpufreq_stats_attribute {
45         struct attribute attr;
46         ssize_t(*show) (struct cpufreq_stats *, char *);
47 };
48
49 static int cpufreq_stats_update(unsigned int cpu)
50 {
51         struct cpufreq_stats *stat;
52         unsigned long long cur_time;
53
54         cur_time = get_jiffies_64();
55         spin_lock(&cpufreq_stats_lock);
56         stat = per_cpu(cpufreq_stats_table, cpu);
57         if (stat->time_in_state)
58                 stat->time_in_state[stat->last_index] +=
59                         cur_time - stat->last_time;
60         stat->last_time = cur_time;
61         spin_unlock(&cpufreq_stats_lock);
62         return 0;
63 }
64
65 static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
66 {
67         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
68         if (!stat)
69                 return 0;
70         return sprintf(buf, "%d\n",
71                         per_cpu(cpufreq_stats_table, stat->cpu)->total_trans);
72 }
73
74 static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
75 {
76         ssize_t len = 0;
77         int i;
78         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
79         if (!stat)
80                 return 0;
81         cpufreq_stats_update(stat->cpu);
82         for (i = 0; i < stat->state_num; i++) {
83                 len += sprintf(buf + len, "%u %llu\n", stat->freq_table[i],
84                         (unsigned long long)
85                         cputime64_to_clock_t(stat->time_in_state[i]));
86         }
87         return len;
88 }
89
90 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
91 static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
92 {
93         ssize_t len = 0;
94         int i, j;
95
96         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
97         if (!stat)
98                 return 0;
99         cpufreq_stats_update(stat->cpu);
100         len += snprintf(buf + len, PAGE_SIZE - len, "   From  :    To\n");
101         len += snprintf(buf + len, PAGE_SIZE - len, "         : ");
102         for (i = 0; i < stat->state_num; i++) {
103                 if (len >= PAGE_SIZE)
104                         break;
105                 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
106                                 stat->freq_table[i]);
107         }
108         if (len >= PAGE_SIZE)
109                 return PAGE_SIZE;
110
111         len += snprintf(buf + len, PAGE_SIZE - len, "\n");
112
113         for (i = 0; i < stat->state_num; i++) {
114                 if (len >= PAGE_SIZE)
115                         break;
116
117                 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
118                                 stat->freq_table[i]);
119
120                 for (j = 0; j < stat->state_num; j++)   {
121                         if (len >= PAGE_SIZE)
122                                 break;
123                         len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
124                                         stat->trans_table[i*stat->max_state+j]);
125                 }
126                 if (len >= PAGE_SIZE)
127                         break;
128                 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
129         }
130         if (len >= PAGE_SIZE)
131                 return PAGE_SIZE;
132         return len;
133 }
134 cpufreq_freq_attr_ro(trans_table);
135 #endif
136
137 cpufreq_freq_attr_ro(total_trans);
138 cpufreq_freq_attr_ro(time_in_state);
139
140 static struct attribute *default_attrs[] = {
141         &total_trans.attr,
142         &time_in_state.attr,
143 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
144         &trans_table.attr,
145 #endif
146         NULL
147 };
148 static struct attribute_group stats_attr_group = {
149         .attrs = default_attrs,
150         .name = "stats"
151 };
152
153 static int freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
154 {
155         int index;
156         for (index = 0; index < stat->max_state; index++)
157                 if (stat->freq_table[index] == freq)
158                         return index;
159         return -1;
160 }
161
162 /* should be called late in the CPU removal sequence so that the stats
163  * memory is still available in case someone tries to use it.
164  */
165 static void cpufreq_stats_free_table(unsigned int cpu)
166 {
167         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, cpu);
168
169         if (stat) {
170                 pr_debug("%s: Free stat table\n", __func__);
171                 kfree(stat->time_in_state);
172                 kfree(stat);
173                 per_cpu(cpufreq_stats_table, cpu) = NULL;
174         }
175 }
176
177 /* must be called early in the CPU removal sequence (before
178  * cpufreq_remove_dev) so that policy is still valid.
179  */
180 static void cpufreq_stats_free_sysfs(unsigned int cpu)
181 {
182         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
183
184         if (!policy)
185                 return;
186
187         if (!cpufreq_frequency_get_table(cpu))
188                 goto put_ref;
189
190         if (!policy_is_shared(policy)) {
191                 pr_debug("%s: Free sysfs stat\n", __func__);
192                 sysfs_remove_group(&policy->kobj, &stats_attr_group);
193         }
194
195 put_ref:
196         cpufreq_cpu_put(policy);
197 }
198
199 static int cpufreq_stats_create_table(struct cpufreq_policy *policy,
200                 struct cpufreq_frequency_table *table)
201 {
202         unsigned int i, j, count = 0, ret = 0;
203         struct cpufreq_stats *stat;
204         struct cpufreq_policy *data;
205         unsigned int alloc_size;
206         unsigned int cpu = policy->cpu;
207         if (per_cpu(cpufreq_stats_table, cpu))
208                 return -EBUSY;
209         stat = kzalloc(sizeof(struct cpufreq_stats), GFP_KERNEL);
210         if ((stat) == NULL)
211                 return -ENOMEM;
212
213         data = cpufreq_cpu_get(cpu);
214         if (data == NULL) {
215                 ret = -EINVAL;
216                 goto error_get_fail;
217         }
218
219         ret = sysfs_create_group(&data->kobj, &stats_attr_group);
220         if (ret)
221                 goto error_out;
222
223         stat->cpu = cpu;
224         per_cpu(cpufreq_stats_table, cpu) = stat;
225
226         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
227                 unsigned int freq = table[i].frequency;
228                 if (freq == CPUFREQ_ENTRY_INVALID)
229                         continue;
230                 count++;
231         }
232
233         alloc_size = count * sizeof(int) + count * sizeof(u64);
234
235 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
236         alloc_size += count * count * sizeof(int);
237 #endif
238         stat->max_state = count;
239         stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
240         if (!stat->time_in_state) {
241                 ret = -ENOMEM;
242                 goto error_out;
243         }
244         stat->freq_table = (unsigned int *)(stat->time_in_state + count);
245
246 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
247         stat->trans_table = stat->freq_table + count;
248 #endif
249         j = 0;
250         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
251                 unsigned int freq = table[i].frequency;
252                 if (freq == CPUFREQ_ENTRY_INVALID)
253                         continue;
254                 if (freq_table_get_index(stat, freq) == -1)
255                         stat->freq_table[j++] = freq;
256         }
257         stat->state_num = j;
258         spin_lock(&cpufreq_stats_lock);
259         stat->last_time = get_jiffies_64();
260         stat->last_index = freq_table_get_index(stat, policy->cur);
261         spin_unlock(&cpufreq_stats_lock);
262         cpufreq_cpu_put(data);
263         return 0;
264 error_out:
265         cpufreq_cpu_put(data);
266 error_get_fail:
267         kfree(stat);
268         per_cpu(cpufreq_stats_table, cpu) = NULL;
269         return ret;
270 }
271
272 static void cpufreq_stats_update_policy_cpu(struct cpufreq_policy *policy)
273 {
274         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table,
275                         policy->last_cpu);
276
277         pr_debug("Updating stats_table for new_cpu %u from last_cpu %u\n",
278                         policy->cpu, policy->last_cpu);
279         per_cpu(cpufreq_stats_table, policy->cpu) = per_cpu(cpufreq_stats_table,
280                         policy->last_cpu);
281         per_cpu(cpufreq_stats_table, policy->last_cpu) = NULL;
282         stat->cpu = policy->cpu;
283 }
284
285 static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
286                 unsigned long val, void *data)
287 {
288         int ret;
289         struct cpufreq_policy *policy = data;
290         struct cpufreq_frequency_table *table;
291         unsigned int cpu = policy->cpu;
292
293         if (val == CPUFREQ_UPDATE_POLICY_CPU) {
294                 cpufreq_stats_update_policy_cpu(policy);
295                 return 0;
296         }
297
298         if (val != CPUFREQ_NOTIFY)
299                 return 0;
300         table = cpufreq_frequency_get_table(cpu);
301         if (!table)
302                 return 0;
303         ret = cpufreq_stats_create_table(policy, table);
304         if (ret)
305                 return ret;
306         return 0;
307 }
308
309 static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
310                 unsigned long val, void *data)
311 {
312         struct cpufreq_freqs *freq = data;
313         struct cpufreq_stats *stat;
314         int old_index, new_index;
315
316         if (val != CPUFREQ_POSTCHANGE)
317                 return 0;
318
319         stat = per_cpu(cpufreq_stats_table, freq->cpu);
320         if (!stat)
321                 return 0;
322
323         old_index = stat->last_index;
324         new_index = freq_table_get_index(stat, freq->new);
325
326         /* We can't do stat->time_in_state[-1]= .. */
327         if (old_index == -1 || new_index == -1)
328                 return 0;
329
330         cpufreq_stats_update(freq->cpu);
331
332         if (old_index == new_index)
333                 return 0;
334
335         spin_lock(&cpufreq_stats_lock);
336         stat->last_index = new_index;
337 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
338         stat->trans_table[old_index * stat->max_state + new_index]++;
339 #endif
340         stat->total_trans++;
341         spin_unlock(&cpufreq_stats_lock);
342         return 0;
343 }
344
345 static int cpufreq_stats_create_table_cpu(unsigned int cpu)
346 {
347         struct cpufreq_policy *policy;
348         struct cpufreq_frequency_table *table;
349         int ret = -ENODEV;
350
351         policy = cpufreq_cpu_get(cpu);
352         if (!policy)
353                 return -ENODEV;
354
355         table = cpufreq_frequency_get_table(cpu);
356         if (!table)
357                 goto out;
358
359         ret = cpufreq_stats_create_table(policy, table);
360
361 out:
362         cpufreq_cpu_put(policy);
363         return ret;
364 }
365
366 static int __cpuinit cpufreq_stat_cpu_callback(struct notifier_block *nfb,
367                                                unsigned long action,
368                                                void *hcpu)
369 {
370         unsigned int cpu = (unsigned long)hcpu;
371
372         switch (action) {
373         case CPU_ONLINE:
374         case CPU_ONLINE_FROZEN:
375                 cpufreq_update_policy(cpu);
376                 break;
377         case CPU_DOWN_PREPARE:
378         case CPU_DOWN_PREPARE_FROZEN:
379                 cpufreq_stats_free_sysfs(cpu);
380                 break;
381         case CPU_DEAD:
382         case CPU_DEAD_FROZEN:
383                 cpufreq_stats_free_table(cpu);
384                 break;
385         case CPU_DOWN_FAILED:
386         case CPU_DOWN_FAILED_FROZEN:
387                 cpufreq_stats_create_table_cpu(cpu);
388                 break;
389         }
390         return NOTIFY_OK;
391 }
392
393 /* priority=1 so this will get called before cpufreq_remove_dev */
394 static struct notifier_block cpufreq_stat_cpu_notifier __refdata = {
395         .notifier_call = cpufreq_stat_cpu_callback,
396         .priority = 1,
397 };
398
399 static struct notifier_block notifier_policy_block = {
400         .notifier_call = cpufreq_stat_notifier_policy
401 };
402
403 static struct notifier_block notifier_trans_block = {
404         .notifier_call = cpufreq_stat_notifier_trans
405 };
406
407 static int cpufreq_stats_setup(void)
408 {
409         int ret;
410         unsigned int cpu;
411
412         spin_lock_init(&cpufreq_stats_lock);
413         ret = cpufreq_register_notifier(&notifier_policy_block,
414                                 CPUFREQ_POLICY_NOTIFIER);
415         if (ret)
416                 return ret;
417
418         register_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
419         for_each_online_cpu(cpu)
420                 cpufreq_update_policy(cpu);
421
422         ret = cpufreq_register_notifier(&notifier_trans_block,
423                                 CPUFREQ_TRANSITION_NOTIFIER);
424         if (ret) {
425                 cpufreq_unregister_notifier(&notifier_policy_block,
426                                 CPUFREQ_POLICY_NOTIFIER);
427                 unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
428                 for_each_online_cpu(cpu)
429                         cpufreq_stats_free_table(cpu);
430                 return ret;
431         }
432
433         return 0;
434 }
435
436 static void cpufreq_stats_cleanup(void)
437 {
438         unsigned int cpu;
439
440         cpufreq_unregister_notifier(&notifier_policy_block,
441                         CPUFREQ_POLICY_NOTIFIER);
442         cpufreq_unregister_notifier(&notifier_trans_block,
443                         CPUFREQ_TRANSITION_NOTIFIER);
444         unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
445         for_each_online_cpu(cpu) {
446                 cpufreq_stats_free_table(cpu);
447                 cpufreq_stats_free_sysfs(cpu);
448         }
449 }
450
451 static int cpufreq_stats_switcher_notifier(struct notifier_block *nfb,
452                                         unsigned long action, void *_arg)
453 {
454         switch (action) {
455         case BL_NOTIFY_PRE_ENABLE:
456         case BL_NOTIFY_PRE_DISABLE:
457                 cpufreq_stats_cleanup();
458                 break;
459
460         case BL_NOTIFY_POST_ENABLE:
461         case BL_NOTIFY_POST_DISABLE:
462                 cpufreq_stats_setup();
463                 break;
464
465         default:
466                 return NOTIFY_DONE;
467         }
468
469         return NOTIFY_OK;
470 }
471
472 static struct notifier_block switcher_notifier = {
473         .notifier_call = cpufreq_stats_switcher_notifier,
474 };
475
476 static int __init cpufreq_stats_init(void)
477 {
478         int ret;
479         spin_lock_init(&cpufreq_stats_lock);
480
481         ret = cpufreq_stats_setup();
482         if (!ret)
483                 bL_switcher_register_notifier(&switcher_notifier);
484
485         return ret;
486 }
487
488 static void __exit cpufreq_stats_exit(void)
489 {
490         bL_switcher_unregister_notifier(&switcher_notifier);
491         cpufreq_stats_cleanup();
492 }
493
494 MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
495 MODULE_DESCRIPTION("'cpufreq_stats' - A driver to export cpufreq stats "
496                                 "through sysfs filesystem");
497 MODULE_LICENSE("GPL");
498
499 module_init(cpufreq_stats_init);
500 module_exit(cpufreq_stats_exit);