2 * Copyright (c) 2012 NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <linux/mutex.h>
20 #include <linux/module.h>
21 #include <linux/cpuquiet.h>
22 #include <linux/cpu.h>
23 #include <linux/jiffies.h>
24 #include <linux/slab.h>
25 #include <asm/cputime.h>
29 struct cpuquiet_cpu_stat {
30 cputime64_t time_up_total;
32 unsigned int up_down_count;
33 struct kobject cpu_kobject;
36 struct cpu_attribute {
37 struct attribute attr;
38 enum { up_down_count, time_up_total } type;
41 static struct cpuquiet_driver *cpuquiet_curr_driver;
42 struct cpuquiet_cpu_stat *stats;
44 #define CPU_ATTRIBUTE(_name) \
45 static struct cpu_attribute _name ## _attr = { \
46 .attr = {.name = __stringify(_name), .mode = 0444 }, \
50 CPU_ATTRIBUTE(up_down_count);
51 CPU_ATTRIBUTE(time_up_total);
53 static struct attribute *cpu_attributes[] = {
54 &up_down_count_attr.attr,
55 &time_up_total_attr.attr,
59 static void stats_update(struct cpuquiet_cpu_stat *stat, bool up)
61 u64 cur_jiffies = get_jiffies_64();
62 bool was_up = stat->up_down_count & 0x1;
65 stat->time_up_total = cputime64_add(stat->time_up_total,
66 cputime64_sub(cur_jiffies, stat->last_update));
69 stat->up_down_count++;
71 stat->last_update = cur_jiffies;
74 int cpuquiet_quiesence_cpu(unsigned int cpunumber)
78 if (cpuquiet_curr_driver && cpuquiet_curr_driver->quiesence_cpu)
79 err = cpuquiet_curr_driver->quiesence_cpu(cpunumber);
81 stats_update(stats + cpunumber, 0);
85 EXPORT_SYMBOL(cpuquiet_quiesence_cpu);
87 int cpuquiet_wake_cpu(unsigned int cpunumber)
91 if (cpuquiet_curr_driver && cpuquiet_curr_driver->wake_cpu)
92 err = cpuquiet_curr_driver->wake_cpu(cpunumber);
94 stats_update(stats + cpunumber, 1);
98 EXPORT_SYMBOL(cpuquiet_wake_cpu);
100 static ssize_t stats_sysfs_show(struct kobject *kobj,
101 struct attribute *attr, char *buf)
103 struct cpu_attribute *cattr =
104 container_of(attr, struct cpu_attribute, attr);
105 struct cpuquiet_cpu_stat *stat =
106 container_of(kobj, struct cpuquiet_cpu_stat, cpu_kobject);
108 bool was_up = stat->up_down_count & 0x1;
110 stats_update(stat, was_up);
112 switch (cattr->type) {
114 len = sprintf(buf, "%u\n", stat->up_down_count);
117 len = sprintf(buf, "%llu\n", stat->time_up_total);
124 static const struct sysfs_ops stats_sysfs_ops = {
125 .show = stats_sysfs_show,
128 static struct kobj_type ktype_cpu_stats = {
129 .sysfs_ops = &stats_sysfs_ops,
130 .default_attrs = cpu_attributes,
133 int cpuquiet_register_driver(struct cpuquiet_driver *drv)
137 struct sys_device *sys_dev;
143 stats = kzalloc(nr_cpu_ids * sizeof(*stats), GFP_KERNEL);
147 for_each_possible_cpu(cpu) {
148 cur_jiffies = get_jiffies_64();
149 stats[cpu].last_update = cur_jiffies;
151 stats[cpu].up_down_count = 1;
152 sys_dev = get_cpu_sysdev(cpu);
154 cpuquiet_add_dev(sys_dev, cpu);
155 cpuquiet_cpu_kobject_init(&stats[cpu].cpu_kobject,
156 &ktype_cpu_stats, "stats", cpu);
160 mutex_lock(&cpuquiet_lock);
161 if (!cpuquiet_curr_driver) {
163 cpuquiet_curr_driver = drv;
164 cpuquiet_switch_governor(cpuquiet_get_first_governor());
166 mutex_unlock(&cpuquiet_lock);
170 EXPORT_SYMBOL(cpuquiet_register_driver);
172 struct cpuquiet_driver *cpuquiet_get_driver(void)
174 return cpuquiet_curr_driver;
177 void cpuquiet_unregister_driver(struct cpuquiet_driver *drv)
181 if (drv != cpuquiet_curr_driver) {
182 WARN(1, "invalid cpuquiet_unregister_driver(%s)\n",
187 /* stop current governor first */
188 cpuquiet_switch_governor(NULL);
190 mutex_lock(&cpuquiet_lock);
191 cpuquiet_curr_driver = NULL;
193 for_each_possible_cpu(cpu) {
194 kobject_put(&stats[cpu].cpu_kobject);
195 cpuquiet_remove_dev(cpu);
198 mutex_unlock(&cpuquiet_lock);
200 EXPORT_SYMBOL(cpuquiet_unregister_driver);