Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/cpufreq/cpufreq_stats.c |
| 3 | * |
| 4 | * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 5 | * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/cpu.h> |
| 15 | #include <linux/sysfs.h> |
| 16 | #include <linux/cpufreq.h> |
Paul Gortmaker | 5c720d37 | 2011-05-27 13:23:32 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/jiffies.h> |
| 19 | #include <linux/percpu.h> |
| 20 | #include <linux/kobject.h> |
| 21 | #include <linux/spinlock.h> |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 22 | #include <linux/notifier.h> |
Venkatesh Pallipadi | 58f1df2 | 2005-05-25 14:46:50 -0700 | [diff] [blame] | 23 | #include <asm/cputime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | static spinlock_t cpufreq_stats_lock; |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | struct cpufreq_stats { |
| 28 | unsigned int cpu; |
| 29 | unsigned int total_trans; |
Venkatesh Pallipadi | 58f1df2 | 2005-05-25 14:46:50 -0700 | [diff] [blame] | 30 | unsigned long long last_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | unsigned int max_state; |
| 32 | unsigned int state_num; |
| 33 | unsigned int last_index; |
Viresh Kumar | 1e7586a | 2012-10-26 00:51:21 +0200 | [diff] [blame] | 34 | u64 *time_in_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | unsigned int *freq_table; |
| 36 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS |
| 37 | unsigned int *trans_table; |
| 38 | #endif |
| 39 | }; |
| 40 | |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 41 | static DEFINE_PER_CPU(struct cpufreq_stats *, cpufreq_stats_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | struct cpufreq_stats_attribute { |
| 44 | struct attribute attr; |
| 45 | ssize_t(*show) (struct cpufreq_stats *, char *); |
| 46 | }; |
| 47 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 48 | static int cpufreq_stats_update(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
| 50 | struct cpufreq_stats *stat; |
Venkatesh Pallipadi | 58f1df2 | 2005-05-25 14:46:50 -0700 | [diff] [blame] | 51 | unsigned long long cur_time; |
| 52 | |
| 53 | cur_time = get_jiffies_64(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | spin_lock(&cpufreq_stats_lock); |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 55 | stat = per_cpu(cpufreq_stats_table, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | if (stat->time_in_state) |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 57 | stat->time_in_state[stat->last_index] += |
| 58 | cur_time - stat->last_time; |
Venkatesh Pallipadi | 58f1df2 | 2005-05-25 14:46:50 -0700 | [diff] [blame] | 59 | stat->last_time = cur_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | spin_unlock(&cpufreq_stats_lock); |
| 61 | return 0; |
| 62 | } |
| 63 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 64 | static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 66 | struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu); |
Dave Jones | 511e9ee | 2006-05-30 17:57:14 -0400 | [diff] [blame] | 67 | if (!stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return 0; |
| 69 | return sprintf(buf, "%d\n", |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 70 | per_cpu(cpufreq_stats_table, stat->cpu)->total_trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 73 | static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
| 75 | ssize_t len = 0; |
| 76 | int i; |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 77 | struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu); |
Dave Jones | 511e9ee | 2006-05-30 17:57:14 -0400 | [diff] [blame] | 78 | if (!stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return 0; |
| 80 | cpufreq_stats_update(stat->cpu); |
| 81 | for (i = 0; i < stat->state_num; i++) { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 82 | len += sprintf(buf + len, "%u %llu\n", stat->freq_table[i], |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 83 | (unsigned long long) |
| 84 | cputime64_to_clock_t(stat->time_in_state[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | return len; |
| 87 | } |
| 88 | |
| 89 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 90 | static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
| 92 | ssize_t len = 0; |
| 93 | int i, j; |
| 94 | |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 95 | struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu); |
Dave Jones | 511e9ee | 2006-05-30 17:57:14 -0400 | [diff] [blame] | 96 | if (!stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return 0; |
| 98 | cpufreq_stats_update(stat->cpu); |
Venkatesh Pallipadi | 58f1df2 | 2005-05-25 14:46:50 -0700 | [diff] [blame] | 99 | len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n"); |
| 100 | len += snprintf(buf + len, PAGE_SIZE - len, " : "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | for (i = 0; i < stat->state_num; i++) { |
| 102 | if (len >= PAGE_SIZE) |
| 103 | break; |
Venkatesh Pallipadi | 58f1df2 | 2005-05-25 14:46:50 -0700 | [diff] [blame] | 104 | len += snprintf(buf + len, PAGE_SIZE - len, "%9u ", |
| 105 | stat->freq_table[i]); |
| 106 | } |
| 107 | if (len >= PAGE_SIZE) |
Cesar Eduardo Barros | 25aca34 | 2008-02-16 08:41:25 -0200 | [diff] [blame] | 108 | return PAGE_SIZE; |
Venkatesh Pallipadi | 58f1df2 | 2005-05-25 14:46:50 -0700 | [diff] [blame] | 109 | |
| 110 | len += snprintf(buf + len, PAGE_SIZE - len, "\n"); |
| 111 | |
| 112 | for (i = 0; i < stat->state_num; i++) { |
| 113 | if (len >= PAGE_SIZE) |
| 114 | break; |
| 115 | |
| 116 | len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | stat->freq_table[i]); |
| 118 | |
| 119 | for (j = 0; j < stat->state_num; j++) { |
| 120 | if (len >= PAGE_SIZE) |
| 121 | break; |
Venkatesh Pallipadi | 58f1df2 | 2005-05-25 14:46:50 -0700 | [diff] [blame] | 122 | len += snprintf(buf + len, PAGE_SIZE - len, "%9u ", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | stat->trans_table[i*stat->max_state+j]); |
| 124 | } |
Cesar Eduardo Barros | 25aca34 | 2008-02-16 08:41:25 -0200 | [diff] [blame] | 125 | if (len >= PAGE_SIZE) |
| 126 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | len += snprintf(buf + len, PAGE_SIZE - len, "\n"); |
| 128 | } |
Cesar Eduardo Barros | 25aca34 | 2008-02-16 08:41:25 -0200 | [diff] [blame] | 129 | if (len >= PAGE_SIZE) |
| 130 | return PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | return len; |
| 132 | } |
Viresh Kumar | df18e50 | 2013-02-04 11:38:52 +0000 | [diff] [blame] | 133 | cpufreq_freq_attr_ro(trans_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | #endif |
| 135 | |
Viresh Kumar | df18e50 | 2013-02-04 11:38:52 +0000 | [diff] [blame] | 136 | cpufreq_freq_attr_ro(total_trans); |
| 137 | cpufreq_freq_attr_ro(time_in_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | static struct attribute *default_attrs[] = { |
Viresh Kumar | df18e50 | 2013-02-04 11:38:52 +0000 | [diff] [blame] | 140 | &total_trans.attr, |
| 141 | &time_in_state.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS |
Viresh Kumar | df18e50 | 2013-02-04 11:38:52 +0000 | [diff] [blame] | 143 | &trans_table.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | #endif |
| 145 | NULL |
| 146 | }; |
| 147 | static struct attribute_group stats_attr_group = { |
| 148 | .attrs = default_attrs, |
| 149 | .name = "stats" |
| 150 | }; |
| 151 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 152 | static int freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
steven finney | 98586ed | 2011-05-02 11:29:17 -0700 | [diff] [blame] | 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 | */ |
Adrian Bunk | a332347 | 2007-12-17 16:20:03 -0800 | [diff] [blame] | 164 | static void cpufreq_stats_free_table(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 166 | struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, cpu); |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (stat) { |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 169 | pr_debug("%s: Free stat table\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | kfree(stat->time_in_state); |
| 171 | kfree(stat); |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 172 | per_cpu(cpufreq_stats_table, cpu) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
steven finney | 98586ed | 2011-05-02 11:29:17 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* must be called early in the CPU removal sequence (before |
| 177 | * cpufreq_remove_dev) so that policy is still valid. |
| 178 | */ |
| 179 | static void cpufreq_stats_free_sysfs(unsigned int cpu) |
| 180 | { |
| 181 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
Dirk Brandewie | 633d47d | 2013-02-06 09:02:12 -0800 | [diff] [blame] | 182 | |
viresh kumar | 187da1d | 2013-03-22 10:13:52 +0000 | [diff] [blame] | 183 | if (!policy) |
Dirk Brandewie | 633d47d | 2013-02-06 09:02:12 -0800 | [diff] [blame] | 184 | return; |
| 185 | |
viresh kumar | 187da1d | 2013-03-22 10:13:52 +0000 | [diff] [blame] | 186 | if (!cpufreq_frequency_get_table(cpu)) |
| 187 | goto put_ref; |
| 188 | |
| 189 | if (!policy_is_shared(policy)) { |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 190 | pr_debug("%s: Free sysfs stat\n", __func__); |
steven finney | 98586ed | 2011-05-02 11:29:17 -0700 | [diff] [blame] | 191 | sysfs_remove_group(&policy->kobj, &stats_attr_group); |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 192 | } |
viresh kumar | 187da1d | 2013-03-22 10:13:52 +0000 | [diff] [blame] | 193 | |
| 194 | put_ref: |
| 195 | cpufreq_cpu_put(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 198 | static int cpufreq_stats_create_table(struct cpufreq_policy *policy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 206 | if (per_cpu(cpufreq_stats_table, cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return -EBUSY; |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 208 | stat = kzalloc(sizeof(struct cpufreq_stats), GFP_KERNEL); |
| 209 | if ((stat) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | data = cpufreq_cpu_get(cpu); |
Dave Jones | bc7b26f | 2005-10-27 16:02:06 -0700 | [diff] [blame] | 213 | if (data == NULL) { |
| 214 | ret = -EINVAL; |
| 215 | goto error_get_fail; |
| 216 | } |
| 217 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 218 | ret = sysfs_create_group(&data->kobj, &stats_attr_group); |
| 219 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | goto error_out; |
| 221 | |
| 222 | stat->cpu = cpu; |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 223 | per_cpu(cpufreq_stats_table, cpu) = stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 225 | for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | unsigned int freq = table[i].frequency; |
| 227 | if (freq == CPUFREQ_ENTRY_INVALID) |
| 228 | continue; |
| 229 | count++; |
| 230 | } |
| 231 | |
Viresh Kumar | 1e7586a | 2012-10-26 00:51:21 +0200 | [diff] [blame] | 232 | alloc_size = count * sizeof(int) + count * sizeof(u64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS |
| 235 | alloc_size += count * count * sizeof(int); |
| 236 | #endif |
| 237 | stat->max_state = count; |
Dave Jones | e98df50 | 2005-10-20 15:17:43 -0700 | [diff] [blame] | 238 | stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | if (!stat->time_in_state) { |
| 240 | ret = -ENOMEM; |
| 241 | goto error_out; |
| 242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Venkatesh Pallipadi | 58f1df2 | 2005-05-25 14:46:50 -0700 | [diff] [blame] | 258 | stat->last_time = get_jiffies_64(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 263 | error_out: |
| 264 | cpufreq_cpu_put(data); |
Dave Jones | b7fb358 | 2005-11-01 23:13:45 -0800 | [diff] [blame] | 265 | error_get_fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | kfree(stat); |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 267 | per_cpu(cpufreq_stats_table, cpu) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return ret; |
| 269 | } |
| 270 | |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 271 | static 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 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 284 | static int cpufreq_stat_notifier_policy(struct notifier_block *nb, |
| 285 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
| 287 | int ret; |
| 288 | struct cpufreq_policy *policy = data; |
| 289 | struct cpufreq_frequency_table *table; |
| 290 | unsigned int cpu = policy->cpu; |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 291 | |
| 292 | if (val == CPUFREQ_UPDATE_POLICY_CPU) { |
| 293 | cpufreq_stats_update_policy_cpu(policy); |
| 294 | return 0; |
| 295 | } |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | if (val != CPUFREQ_NOTIFY) |
| 298 | return 0; |
| 299 | table = cpufreq_frequency_get_table(cpu); |
| 300 | if (!table) |
| 301 | return 0; |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 302 | ret = cpufreq_stats_create_table(policy, table); |
| 303 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | return ret; |
| 305 | return 0; |
| 306 | } |
| 307 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 308 | static int cpufreq_stat_notifier_trans(struct notifier_block *nb, |
| 309 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 318 | stat = per_cpu(cpufreq_stats_table, freq->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (!stat) |
| 320 | return 0; |
Venkatesh Pallipadi | 8edc59d9 | 2006-12-19 12:58:55 -0800 | [diff] [blame] | 321 | |
Shaohua Li | 6501faf | 2008-04-27 13:46:56 -0700 | [diff] [blame] | 322 | old_index = stat->last_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | new_index = freq_table_get_index(stat, freq->new); |
| 324 | |
Konrad Rzeszutek Wilk | 46a310b | 2011-06-16 15:36:38 -0400 | [diff] [blame] | 325 | /* We can't do stat->time_in_state[-1]= .. */ |
| 326 | if (old_index == -1 || new_index == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return 0; |
| 328 | |
Konrad Rzeszutek Wilk | 46a310b | 2011-06-16 15:36:38 -0400 | [diff] [blame] | 329 | cpufreq_stats_update(freq->cpu); |
| 330 | |
| 331 | if (old_index == new_index) |
Venkatesh Pallipadi | 8edc59d9 | 2006-12-19 12:58:55 -0800 | [diff] [blame] | 332 | return 0; |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Satyam Sharma | 55395ae | 2007-10-02 13:28:15 -0700 | [diff] [blame] | 344 | static int __cpuinit cpufreq_stat_cpu_callback(struct notifier_block *nfb, |
| 345 | unsigned long action, |
| 346 | void *hcpu) |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 347 | { |
| 348 | unsigned int cpu = (unsigned long)hcpu; |
| 349 | |
| 350 | switch (action) { |
| 351 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 352 | case CPU_ONLINE_FROZEN: |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 353 | cpufreq_update_policy(cpu); |
| 354 | break; |
steven finney | 98586ed | 2011-05-02 11:29:17 -0700 | [diff] [blame] | 355 | case CPU_DOWN_PREPARE: |
Tu, Xiaobing | e377367 | 2012-10-23 01:03:00 +0200 | [diff] [blame] | 356 | case CPU_DOWN_PREPARE_FROZEN: |
steven finney | 98586ed | 2011-05-02 11:29:17 -0700 | [diff] [blame] | 357 | cpufreq_stats_free_sysfs(cpu); |
| 358 | break; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 359 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 360 | case CPU_DEAD_FROZEN: |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 361 | cpufreq_stats_free_table(cpu); |
| 362 | break; |
| 363 | } |
| 364 | return NOTIFY_OK; |
| 365 | } |
| 366 | |
steven finney | 98586ed | 2011-05-02 11:29:17 -0700 | [diff] [blame] | 367 | /* priority=1 so this will get called before cpufreq_remove_dev */ |
Karthigan Srinivasan | 469057d | 2011-04-01 17:34:47 -0500 | [diff] [blame] | 368 | static struct notifier_block cpufreq_stat_cpu_notifier __refdata = { |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 369 | .notifier_call = cpufreq_stat_cpu_callback, |
steven finney | 98586ed | 2011-05-02 11:29:17 -0700 | [diff] [blame] | 370 | .priority = 1, |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 371 | }; |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | static struct notifier_block notifier_policy_block = { |
| 374 | .notifier_call = cpufreq_stat_notifier_policy |
| 375 | }; |
| 376 | |
| 377 | static struct notifier_block notifier_trans_block = { |
| 378 | .notifier_call = cpufreq_stat_notifier_trans |
| 379 | }; |
| 380 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 381 | static int __init cpufreq_stats_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
| 383 | int ret; |
| 384 | unsigned int cpu; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | spin_lock_init(&cpufreq_stats_lock); |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 387 | ret = cpufreq_register_notifier(¬ifier_policy_block, |
| 388 | CPUFREQ_POLICY_NOTIFIER); |
| 389 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return ret; |
| 391 | |
Konstantin Khlebnikov | 56836fb | 2012-12-15 00:22:02 +0100 | [diff] [blame] | 392 | register_hotcpu_notifier(&cpufreq_stat_cpu_notifier); |
| 393 | for_each_online_cpu(cpu) |
| 394 | cpufreq_update_policy(cpu); |
| 395 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 396 | ret = cpufreq_register_notifier(¬ifier_trans_block, |
| 397 | CPUFREQ_TRANSITION_NOTIFIER); |
| 398 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | cpufreq_unregister_notifier(¬ifier_policy_block, |
| 400 | CPUFREQ_POLICY_NOTIFIER); |
Konstantin Khlebnikov | 56836fb | 2012-12-15 00:22:02 +0100 | [diff] [blame] | 401 | unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier); |
| 402 | for_each_online_cpu(cpu) |
| 403 | cpufreq_stats_free_table(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return ret; |
| 405 | } |
| 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | return 0; |
| 408 | } |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 409 | static void __exit cpufreq_stats_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
| 411 | unsigned int cpu; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | cpufreq_unregister_notifier(¬ifier_policy_block, |
| 414 | CPUFREQ_POLICY_NOTIFIER); |
| 415 | cpufreq_unregister_notifier(¬ifier_trans_block, |
| 416 | CPUFREQ_TRANSITION_NOTIFIER); |
Chandra Seetharaman | 65edc68 | 2006-06-27 02:54:08 -0700 | [diff] [blame] | 417 | unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier); |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 418 | for_each_online_cpu(cpu) { |
Satyam Sharma | 55395ae | 2007-10-02 13:28:15 -0700 | [diff] [blame] | 419 | cpufreq_stats_free_table(cpu); |
Dave Jones | 13f0675 | 2011-06-12 16:35:28 -0400 | [diff] [blame] | 420 | cpufreq_stats_free_sysfs(cpu); |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 424 | MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>"); |
| 425 | MODULE_DESCRIPTION("'cpufreq_stats' - A driver to export cpufreq stats " |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 426 | "through sysfs filesystem"); |
Dave Jones | 0a829c5 | 2009-01-18 01:49:04 -0500 | [diff] [blame] | 427 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | module_init(cpufreq_stats_init); |
| 430 | module_exit(cpufreq_stats_exit); |