cpufreq_stats: Adds the fucntionality to load current values for each frequency
[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 <linux/sort.h>
24 #include <linux/err.h>
25 #include <linux/of.h>
26 #include <asm/cputime.h>
27
28 static spinlock_t cpufreq_stats_lock;
29
30 struct cpufreq_stats {
31         unsigned int cpu;
32         unsigned int total_trans;
33         unsigned long long  last_time;
34         unsigned int max_state;
35         unsigned int state_num;
36         unsigned int last_index;
37         u64 *time_in_state;
38         unsigned int *freq_table;
39 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
40         unsigned int *trans_table;
41 #endif
42 };
43
44 struct all_cpufreq_stats {
45         unsigned int state_num;
46         cputime64_t *time_in_state;
47         unsigned int *freq_table;
48 };
49
50 struct cpufreq_power_stats {
51         unsigned int state_num;
52         unsigned int *curr;
53         unsigned int *freq_table;
54 };
55
56 struct all_freq_table {
57         unsigned int *freq_table;
58         unsigned int table_size;
59 };
60
61 static struct all_freq_table *all_freq_table;
62
63 static DEFINE_PER_CPU(struct all_cpufreq_stats *, all_cpufreq_stats);
64 static DEFINE_PER_CPU(struct cpufreq_stats *, cpufreq_stats_table);
65 static DEFINE_PER_CPU(struct cpufreq_power_stats *, cpufreq_power_stats);
66
67 struct cpufreq_stats_attribute {
68         struct attribute attr;
69         ssize_t(*show) (struct cpufreq_stats *, char *);
70 };
71
72 static int cpufreq_stats_update(unsigned int cpu)
73 {
74         struct cpufreq_stats *stat;
75         struct all_cpufreq_stats *all_stat;
76         unsigned long long cur_time;
77
78         cur_time = get_jiffies_64();
79         spin_lock(&cpufreq_stats_lock);
80         stat = per_cpu(cpufreq_stats_table, cpu);
81         all_stat = per_cpu(all_cpufreq_stats, cpu);
82         if (!stat) {
83                 spin_unlock(&cpufreq_stats_lock);
84                 return 0;
85         }
86         if (stat->time_in_state) {
87                 stat->time_in_state[stat->last_index] +=
88                         cur_time - stat->last_time;
89                 if (all_stat)
90                         all_stat->time_in_state[stat->last_index] +=
91                                         cur_time - stat->last_time;
92         }
93         stat->last_time = cur_time;
94         spin_unlock(&cpufreq_stats_lock);
95         return 0;
96 }
97
98 static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
99 {
100         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
101         if (!stat)
102                 return 0;
103         return sprintf(buf, "%d\n",
104                         per_cpu(cpufreq_stats_table, stat->cpu)->total_trans);
105 }
106
107 static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
108 {
109         ssize_t len = 0;
110         int i;
111         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
112         if (!stat)
113                 return 0;
114         cpufreq_stats_update(stat->cpu);
115         for (i = 0; i < stat->state_num; i++) {
116                 len += sprintf(buf + len, "%u %llu\n", stat->freq_table[i],
117                         (unsigned long long)
118                         cputime64_to_clock_t(stat->time_in_state[i]));
119         }
120         return len;
121 }
122
123 static int get_index_all_cpufreq_stat(struct all_cpufreq_stats *all_stat,
124                 unsigned int freq)
125 {
126         int i;
127         if (!all_stat)
128                 return -1;
129         for (i = 0; i < all_stat->state_num; i++) {
130                 if (all_stat->freq_table[i] == freq)
131                         return i;
132         }
133         return -1;
134 }
135
136 static ssize_t show_current_in_state(struct kobject *kobj,
137                 struct kobj_attribute *attr, char *buf)
138 {
139         ssize_t len = 0;
140         unsigned int i, cpu;
141         struct cpufreq_power_stats *powerstats;
142
143         spin_lock(&cpufreq_stats_lock);
144         for_each_possible_cpu(cpu) {
145                 powerstats = per_cpu(cpufreq_power_stats, cpu);
146                 if (!powerstats)
147                         continue;
148                 len += scnprintf(buf + len, PAGE_SIZE - len, "CPU%d:", cpu);
149                 for (i = 0; i < powerstats->state_num; i++)
150                         len += scnprintf(buf + len, PAGE_SIZE - len,
151                                         "%d=%d ", powerstats->freq_table[i],
152                                         powerstats->curr[i]);
153                 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
154         }
155         spin_unlock(&cpufreq_stats_lock);
156         return len;
157 }
158
159 static ssize_t show_all_time_in_state(struct kobject *kobj,
160                 struct kobj_attribute *attr, char *buf)
161 {
162         ssize_t len = 0;
163         unsigned int i, cpu, freq, index;
164         struct all_cpufreq_stats *all_stat;
165         struct cpufreq_policy *policy;
166
167         len += scnprintf(buf + len, PAGE_SIZE - len, "freq\t\t");
168         for_each_possible_cpu(cpu) {
169                 len += scnprintf(buf + len, PAGE_SIZE - len, "cpu%d\t\t", cpu);
170                 if (cpu_online(cpu))
171                         cpufreq_stats_update(cpu);
172         }
173
174         if (!all_freq_table)
175                 goto out;
176         for (i = 0; i < all_freq_table->table_size; i++) {
177                 freq = all_freq_table->freq_table[i];
178                 len += scnprintf(buf + len, PAGE_SIZE - len, "\n%u\t\t", freq);
179                 for_each_possible_cpu(cpu) {
180                         policy = cpufreq_cpu_get(cpu);
181                         if (policy == NULL)
182                                 continue;
183                         all_stat = per_cpu(all_cpufreq_stats, policy->cpu);
184                         index = get_index_all_cpufreq_stat(all_stat, freq);
185                         if (index != -1) {
186                                 len += scnprintf(buf + len, PAGE_SIZE - len,
187                                         "%llu\t\t", (unsigned long long)
188                                         cputime64_to_clock_t(all_stat->time_in_state[index]));
189                         } else {
190                                 len += scnprintf(buf + len, PAGE_SIZE - len,
191                                                 "N/A\t\t");
192                         }
193                         cpufreq_cpu_put(policy);
194                 }
195         }
196
197 out:
198         len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
199         return len;
200 }
201
202 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
203 static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
204 {
205         ssize_t len = 0;
206         int i, j;
207
208         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
209         if (!stat)
210                 return 0;
211         cpufreq_stats_update(stat->cpu);
212         len += snprintf(buf + len, PAGE_SIZE - len, "   From  :    To\n");
213         len += snprintf(buf + len, PAGE_SIZE - len, "         : ");
214         for (i = 0; i < stat->state_num; i++) {
215                 if (len >= PAGE_SIZE)
216                         break;
217                 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
218                                 stat->freq_table[i]);
219         }
220         if (len >= PAGE_SIZE)
221                 return PAGE_SIZE;
222
223         len += snprintf(buf + len, PAGE_SIZE - len, "\n");
224
225         for (i = 0; i < stat->state_num; i++) {
226                 if (len >= PAGE_SIZE)
227                         break;
228
229                 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
230                                 stat->freq_table[i]);
231
232                 for (j = 0; j < stat->state_num; j++)   {
233                         if (len >= PAGE_SIZE)
234                                 break;
235                         len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
236                                         stat->trans_table[i*stat->max_state+j]);
237                 }
238                 if (len >= PAGE_SIZE)
239                         break;
240                 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
241         }
242         if (len >= PAGE_SIZE)
243                 return PAGE_SIZE;
244         return len;
245 }
246 cpufreq_freq_attr_ro(trans_table);
247 #endif
248
249 cpufreq_freq_attr_ro(total_trans);
250 cpufreq_freq_attr_ro(time_in_state);
251
252 static struct attribute *default_attrs[] = {
253         &total_trans.attr,
254         &time_in_state.attr,
255 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
256         &trans_table.attr,
257 #endif
258         NULL
259 };
260 static struct attribute_group stats_attr_group = {
261         .attrs = default_attrs,
262         .name = "stats"
263 };
264
265 static struct kobj_attribute _attr_all_time_in_state = __ATTR(all_time_in_state,
266                 0444, show_all_time_in_state, NULL);
267
268 static struct kobj_attribute _attr_current_in_state = __ATTR(current_in_state,
269                 0444, show_current_in_state, NULL);
270
271 static int freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
272 {
273         int index;
274         for (index = 0; index < stat->max_state; index++)
275                 if (stat->freq_table[index] == freq)
276                         return index;
277         return -1;
278 }
279
280 /* should be called late in the CPU removal sequence so that the stats
281  * memory is still available in case someone tries to use it.
282  */
283 static void cpufreq_stats_free_table(unsigned int cpu)
284 {
285         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, cpu);
286
287         if (stat) {
288                 pr_debug("%s: Free stat table\n", __func__);
289                 kfree(stat->time_in_state);
290                 kfree(stat);
291                 per_cpu(cpufreq_stats_table, cpu) = NULL;
292         }
293 }
294
295 /* must be called early in the CPU removal sequence (before
296  * cpufreq_remove_dev) so that policy is still valid.
297  */
298 static void cpufreq_stats_free_sysfs(unsigned int cpu)
299 {
300         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
301
302         if (!policy)
303                 return;
304
305         if (!cpufreq_frequency_get_table(cpu))
306                 goto put_ref;
307
308         if (!policy_is_shared(policy)) {
309                 pr_debug("%s: Free sysfs stat\n", __func__);
310                 sysfs_remove_group(&policy->kobj, &stats_attr_group);
311         }
312
313 put_ref:
314         cpufreq_cpu_put(policy);
315 }
316
317 static void cpufreq_allstats_free(void)
318 {
319         int cpu;
320         struct all_cpufreq_stats *all_stat;
321
322         sysfs_remove_file(cpufreq_global_kobject,
323                                                 &_attr_all_time_in_state.attr);
324
325         for_each_possible_cpu(cpu) {
326                 all_stat = per_cpu(all_cpufreq_stats, cpu);
327                 if (!all_stat)
328                         continue;
329                 kfree(all_stat->time_in_state);
330                 kfree(all_stat);
331                 per_cpu(all_cpufreq_stats, cpu) = NULL;
332         }
333         if (all_freq_table) {
334                 kfree(all_freq_table->freq_table);
335                 kfree(all_freq_table);
336                 all_freq_table = NULL;
337         }
338 }
339
340 static void cpufreq_powerstats_free(void)
341 {
342         int cpu;
343         struct cpufreq_power_stats *powerstats;
344
345         sysfs_remove_file(cpufreq_global_kobject, &_attr_current_in_state.attr);
346
347         for_each_possible_cpu(cpu) {
348                 powerstats = per_cpu(cpufreq_power_stats, cpu);
349                 if (!powerstats)
350                         continue;
351                 kfree(powerstats->curr);
352                 kfree(powerstats);
353                 per_cpu(cpufreq_power_stats, cpu) = NULL;
354         }
355 }
356
357 static int cpufreq_stats_create_table(struct cpufreq_policy *policy,
358                 struct cpufreq_frequency_table *table, int count)
359 {
360         unsigned int i, j, ret = 0;
361         struct cpufreq_stats *stat;
362         struct cpufreq_policy *data;
363         unsigned int alloc_size;
364         unsigned int cpu = policy->cpu;
365         if (per_cpu(cpufreq_stats_table, cpu))
366                 return -EBUSY;
367         stat = kzalloc(sizeof(struct cpufreq_stats), GFP_KERNEL);
368         if ((stat) == NULL)
369                 return -ENOMEM;
370
371         data = cpufreq_cpu_get(cpu);
372         if (data == NULL) {
373                 ret = -EINVAL;
374                 goto error_get_fail;
375         }
376
377         ret = sysfs_create_group(&data->kobj, &stats_attr_group);
378         if (ret)
379                 goto error_out;
380
381         stat->cpu = cpu;
382         per_cpu(cpufreq_stats_table, cpu) = stat;
383
384
385         alloc_size = count * sizeof(int) + count * sizeof(u64);
386
387 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
388         alloc_size += count * count * sizeof(int);
389 #endif
390         stat->max_state = count;
391         stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
392         if (!stat->time_in_state) {
393                 ret = -ENOMEM;
394                 goto error_out;
395         }
396         stat->freq_table = (unsigned int *)(stat->time_in_state + count);
397
398 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
399         stat->trans_table = stat->freq_table + count;
400 #endif
401         j = 0;
402         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
403                 unsigned int freq = table[i].frequency;
404                 if (freq == CPUFREQ_ENTRY_INVALID)
405                         continue;
406                 if (freq_table_get_index(stat, freq) == -1)
407                         stat->freq_table[j++] = freq;
408         }
409         stat->state_num = j;
410         spin_lock(&cpufreq_stats_lock);
411         stat->last_time = get_jiffies_64();
412         stat->last_index = freq_table_get_index(stat, policy->cur);
413         spin_unlock(&cpufreq_stats_lock);
414         cpufreq_cpu_put(data);
415         return 0;
416 error_out:
417         cpufreq_cpu_put(data);
418 error_get_fail:
419         kfree(stat);
420         per_cpu(cpufreq_stats_table, cpu) = NULL;
421         return ret;
422 }
423
424 static void cpufreq_stats_update_policy_cpu(struct cpufreq_policy *policy)
425 {
426         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table,
427                         policy->last_cpu);
428
429         pr_debug("Updating stats_table for new_cpu %u from last_cpu %u\n",
430                         policy->cpu, policy->last_cpu);
431         per_cpu(cpufreq_stats_table, policy->cpu) = per_cpu(cpufreq_stats_table,
432                         policy->last_cpu);
433         per_cpu(cpufreq_stats_table, policy->last_cpu) = NULL;
434         stat->cpu = policy->cpu;
435 }
436
437 static void cpufreq_powerstats_create(unsigned int cpu,
438                 struct cpufreq_frequency_table *table, int count) {
439         unsigned int alloc_size, i = 0, j = 0, ret = 0;
440         struct cpufreq_power_stats *powerstats;
441         struct device_node *cpu_node;
442         char device_path[16];
443
444         powerstats = kzalloc(sizeof(struct cpufreq_power_stats),
445                         GFP_KERNEL);
446         if (!powerstats)
447                 return;
448
449         /* Allocate memory for freq table per cpu as well as clockticks per
450          * freq*/
451         alloc_size = count * sizeof(unsigned int) +
452                 count * sizeof(unsigned int);
453         powerstats->curr = kzalloc(alloc_size, GFP_KERNEL);
454         if (!powerstats->curr) {
455                 kfree(powerstats);
456                 return;
457         }
458         powerstats->freq_table = powerstats->curr + count;
459
460         spin_lock(&cpufreq_stats_lock);
461         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END && j < count; i++) {
462                 unsigned int freq = table[i].frequency;
463
464                 if (freq == CPUFREQ_ENTRY_INVALID)
465                         continue;
466                 powerstats->freq_table[j++] = freq;
467         }
468         powerstats->state_num = j;
469
470         snprintf(device_path, sizeof(device_path), "/cpus/cpu@%d", cpu);
471         cpu_node = of_find_node_by_path(device_path);
472         if (cpu_node) {
473                 ret = of_property_read_u32_array(cpu_node, "current",
474                                 powerstats->curr, count);
475                 if (ret) {
476                         kfree(powerstats->curr);
477                         kfree(powerstats);
478                         powerstats = NULL;
479                 }
480         }
481         per_cpu(cpufreq_power_stats, cpu) = powerstats;
482         spin_unlock(&cpufreq_stats_lock);
483 }
484
485 static int compare_for_sort(const void *lhs_ptr, const void *rhs_ptr)
486 {
487         unsigned int lhs = *(const unsigned int *)(lhs_ptr);
488         unsigned int rhs = *(const unsigned int *)(rhs_ptr);
489         if (lhs < rhs)
490                 return -1;
491         if (lhs > rhs)
492                 return 1;
493         return 0;
494 }
495
496 static bool check_all_freq_table(unsigned int freq)
497 {
498         int i;
499         for (i = 0; i < all_freq_table->table_size; i++) {
500                 if (freq == all_freq_table->freq_table[i])
501                         return true;
502         }
503         return false;
504 }
505
506 static void create_all_freq_table(void)
507 {
508         all_freq_table = kzalloc(sizeof(struct all_freq_table),
509                         GFP_KERNEL);
510         if (!all_freq_table)
511                 pr_warn("could not allocate memory for all_freq_table\n");
512         return;
513 }
514
515 static void add_all_freq_table(unsigned int freq)
516 {
517         unsigned int size;
518         size = sizeof(unsigned int) * (all_freq_table->table_size + 1);
519         all_freq_table->freq_table = krealloc(all_freq_table->freq_table,
520                         size, GFP_ATOMIC);
521         if (IS_ERR(all_freq_table->freq_table)) {
522                 pr_warn("Could not reallocate memory for freq_table\n");
523                 all_freq_table->freq_table = NULL;
524                 return;
525         }
526         all_freq_table->freq_table[all_freq_table->table_size++] = freq;
527 }
528
529 static void cpufreq_allstats_create(unsigned int cpu,
530                 struct cpufreq_frequency_table *table, int count)
531 {
532         int i , j = 0;
533         unsigned int alloc_size;
534         struct all_cpufreq_stats *all_stat;
535         bool sort_needed = false;
536
537         all_stat = kzalloc(sizeof(struct all_cpufreq_stats),
538                         GFP_KERNEL);
539         if (!all_stat) {
540                 pr_warn("Cannot allocate memory for cpufreq stats\n");
541                 return;
542         }
543
544         /*Allocate memory for freq table per cpu as well as clockticks per freq*/
545         alloc_size = count * sizeof(int) + count * sizeof(cputime64_t);
546         all_stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
547         if (!all_stat->time_in_state) {
548                 pr_warn("Cannot allocate memory for cpufreq time_in_state\n");
549                 kfree(all_stat);
550                 all_stat = NULL;
551                 return;
552         }
553         all_stat->freq_table = (unsigned int *)
554                 (all_stat->time_in_state + count);
555
556         spin_lock(&cpufreq_stats_lock);
557         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
558                 unsigned int freq = table[i].frequency;
559                 if (freq == CPUFREQ_ENTRY_INVALID)
560                         continue;
561                 all_stat->freq_table[j++] = freq;
562                 if (all_freq_table && !check_all_freq_table(freq)) {
563                         add_all_freq_table(freq);
564                         sort_needed = true;
565                 }
566         }
567         if (sort_needed)
568                 sort(all_freq_table->freq_table, all_freq_table->table_size,
569                                 sizeof(unsigned int), &compare_for_sort, NULL);
570         all_stat->state_num = j;
571         per_cpu(all_cpufreq_stats, cpu) = all_stat;
572         spin_unlock(&cpufreq_stats_lock);
573 }
574
575 static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
576                 unsigned long val, void *data)
577 {
578         int ret, count = 0, i;
579         struct cpufreq_policy *policy = data;
580         struct cpufreq_frequency_table *table;
581         unsigned int cpu = policy->cpu;
582
583         if (val == CPUFREQ_UPDATE_POLICY_CPU) {
584                 cpufreq_stats_update_policy_cpu(policy);
585                 return 0;
586         }
587
588         if (val != CPUFREQ_NOTIFY)
589                 return 0;
590         table = cpufreq_frequency_get_table(cpu);
591         if (!table)
592                 return 0;
593
594         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
595                 unsigned int freq = table[i].frequency;
596
597                 if (freq == CPUFREQ_ENTRY_INVALID)
598                         continue;
599                 count++;
600         }
601
602         if (!per_cpu(all_cpufreq_stats, cpu))
603                 cpufreq_allstats_create(cpu, table, count);
604
605         if (!per_cpu(cpufreq_power_stats, cpu))
606                 cpufreq_powerstats_create(cpu, table, count);
607
608         ret = cpufreq_stats_create_table(policy, table, count);
609         if (ret)
610                 return ret;
611         return 0;
612 }
613
614 static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
615                 unsigned long val, void *data)
616 {
617         struct cpufreq_freqs *freq = data;
618         struct cpufreq_stats *stat;
619         int old_index, new_index;
620
621         if (val != CPUFREQ_POSTCHANGE)
622                 return 0;
623
624         stat = per_cpu(cpufreq_stats_table, freq->cpu);
625         if (!stat)
626                 return 0;
627
628         old_index = stat->last_index;
629         new_index = freq_table_get_index(stat, freq->new);
630
631         /* We can't do stat->time_in_state[-1]= .. */
632         if (old_index == -1 || new_index == -1)
633                 return 0;
634
635         cpufreq_stats_update(freq->cpu);
636
637         if (old_index == new_index)
638                 return 0;
639
640         spin_lock(&cpufreq_stats_lock);
641         stat->last_index = new_index;
642 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
643         stat->trans_table[old_index * stat->max_state + new_index]++;
644 #endif
645         stat->total_trans++;
646         spin_unlock(&cpufreq_stats_lock);
647         return 0;
648 }
649
650 static int cpufreq_stats_create_table_cpu(unsigned int cpu)
651 {
652         struct cpufreq_policy *policy;
653         struct cpufreq_frequency_table *table;
654         int ret = -ENODEV, i, count = 0;
655
656         policy = cpufreq_cpu_get(cpu);
657         if (!policy)
658                 return -ENODEV;
659
660         table = cpufreq_frequency_get_table(cpu);
661         if (!table)
662                 goto out;
663
664         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
665                 unsigned int freq = table[i].frequency;
666
667                 if (freq == CPUFREQ_ENTRY_INVALID)
668                         continue;
669                 count++;
670         }
671
672         if (!per_cpu(all_cpufreq_stats, cpu))
673                 cpufreq_allstats_create(cpu, table, count);
674
675         if (!per_cpu(cpufreq_power_stats, cpu))
676                 cpufreq_powerstats_create(cpu, table, count);
677
678         ret = cpufreq_stats_create_table(policy, table, count);
679
680 out:
681         cpufreq_cpu_put(policy);
682         return ret;
683 }
684
685 static int __cpuinit cpufreq_stat_cpu_callback(struct notifier_block *nfb,
686                                                unsigned long action,
687                                                void *hcpu)
688 {
689         unsigned int cpu = (unsigned long)hcpu;
690
691         switch (action) {
692         case CPU_ONLINE:
693                 cpufreq_update_policy(cpu);
694                 break;
695         case CPU_DOWN_PREPARE:
696                 cpufreq_stats_free_sysfs(cpu);
697                 break;
698         case CPU_DEAD:
699                 cpufreq_stats_free_table(cpu);
700                 break;
701         case CPU_UP_CANCELED_FROZEN:
702                 cpufreq_stats_free_sysfs(cpu);
703                 cpufreq_stats_free_table(cpu);
704                 break;
705         case CPU_DOWN_FAILED:
706         case CPU_DOWN_FAILED_FROZEN:
707                 cpufreq_stats_create_table_cpu(cpu);
708                 break;
709         }
710         return NOTIFY_OK;
711 }
712
713 /* priority=1 so this will get called before cpufreq_remove_dev */
714 static struct notifier_block cpufreq_stat_cpu_notifier __refdata = {
715         .notifier_call = cpufreq_stat_cpu_callback,
716         .priority = 1,
717 };
718
719 static struct notifier_block notifier_policy_block = {
720         .notifier_call = cpufreq_stat_notifier_policy
721 };
722
723 static struct notifier_block notifier_trans_block = {
724         .notifier_call = cpufreq_stat_notifier_trans
725 };
726
727 static int __init cpufreq_stats_init(void)
728 {
729         int ret;
730         unsigned int cpu;
731
732         spin_lock_init(&cpufreq_stats_lock);
733         ret = cpufreq_register_notifier(&notifier_policy_block,
734                                 CPUFREQ_POLICY_NOTIFIER);
735         if (ret)
736                 return ret;
737
738         register_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
739         for_each_online_cpu(cpu)
740                 cpufreq_update_policy(cpu);
741
742         ret = cpufreq_register_notifier(&notifier_trans_block,
743                                 CPUFREQ_TRANSITION_NOTIFIER);
744         if (ret) {
745                 cpufreq_unregister_notifier(&notifier_policy_block,
746                                 CPUFREQ_POLICY_NOTIFIER);
747                 unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
748                 for_each_online_cpu(cpu)
749                         cpufreq_stats_free_table(cpu);
750                 return ret;
751         }
752
753         create_all_freq_table();
754         ret = sysfs_create_file(cpufreq_global_kobject,
755                         &_attr_all_time_in_state.attr);
756         if (ret)
757                 pr_warn("Cannot create sysfs file for cpufreq stats\n");
758
759         ret = sysfs_create_file(cpufreq_global_kobject,
760                         &_attr_current_in_state.attr);
761         if (ret)
762                 pr_warn("Cannot create sysfs file for cpufreq current stats\n");
763
764         return 0;
765 }
766 static void __exit cpufreq_stats_exit(void)
767 {
768         unsigned int cpu;
769
770         cpufreq_unregister_notifier(&notifier_policy_block,
771                         CPUFREQ_POLICY_NOTIFIER);
772         cpufreq_unregister_notifier(&notifier_trans_block,
773                         CPUFREQ_TRANSITION_NOTIFIER);
774         unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
775         for_each_online_cpu(cpu) {
776                 cpufreq_stats_free_table(cpu);
777                 cpufreq_stats_free_sysfs(cpu);
778         }
779         cpufreq_allstats_free();
780         cpufreq_powerstats_free();
781 }
782
783 MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
784 MODULE_DESCRIPTION("'cpufreq_stats' - A driver to export cpufreq stats "
785                                 "through sysfs filesystem");
786 MODULE_LICENSE("GPL");
787
788 module_init(cpufreq_stats_init);
789 module_exit(cpufreq_stats_exit);