cpuquiet: Adding runnable thread knobs
[firefly-linux-kernel-4.4.55.git] / drivers / cpuquiet / driver.c
1 /*
2  * Copyright (c) 2012 NVIDIA CORPORATION.  All rights reserved.
3  *
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.
7  *
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
11  * more details.
12  *
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.
16  *
17  */
18
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>
26
27 #include "cpuquiet.h"
28
29 struct cpuquiet_cpu_stat {
30         cputime64_t time_up_total;
31         u64 last_update;
32         unsigned int up_down_count;
33         struct kobject cpu_kobject;
34 };
35
36 struct cpu_attribute {
37         struct attribute attr;
38         enum { up_down_count, time_up_total } type;
39 };
40
41 static struct cpuquiet_driver *cpuquiet_curr_driver;
42 struct cpuquiet_cpu_stat *stats;
43
44 #define CPU_ATTRIBUTE(_name) \
45         static struct cpu_attribute _name ## _attr = {                  \
46                 .attr =  {.name = __stringify(_name), .mode = 0444 },   \
47                 .type   = _name,                                        \
48 }
49
50 CPU_ATTRIBUTE(up_down_count);
51 CPU_ATTRIBUTE(time_up_total);
52
53 static struct attribute *cpu_attributes[] = {
54         &up_down_count_attr.attr,
55         &time_up_total_attr.attr,
56         NULL,
57 };
58
59 static void stats_update(struct cpuquiet_cpu_stat *stat, bool up)
60 {
61         u64 cur_jiffies = get_jiffies_64();
62         bool was_up = stat->up_down_count & 0x1;
63
64         if (was_up)
65                 stat->time_up_total += cur_jiffies - stat->last_update;
66
67         if (was_up != up)
68                 stat->up_down_count++;
69
70         stat->last_update = cur_jiffies;
71 }
72
73 int cpuquiet_quiesence_cpu(unsigned int cpunumber)
74 {
75         int err = -EPERM;
76
77         if (cpuquiet_curr_driver && cpuquiet_curr_driver->quiesence_cpu)
78                 err = cpuquiet_curr_driver->quiesence_cpu(cpunumber);
79
80         if (!err)
81                 stats_update(stats + cpunumber, 0);
82
83         return err;
84 }
85 EXPORT_SYMBOL(cpuquiet_quiesence_cpu);
86
87 int cpuquiet_wake_cpu(unsigned int cpunumber)
88 {
89         int err = -EPERM;
90
91         if (cpuquiet_curr_driver && cpuquiet_curr_driver->wake_cpu)
92                 err = cpuquiet_curr_driver->wake_cpu(cpunumber);
93
94         if (!err)
95                 stats_update(stats + cpunumber, 1);
96
97         return err;
98 }
99 EXPORT_SYMBOL(cpuquiet_wake_cpu);
100
101 static ssize_t stats_sysfs_show(struct kobject *kobj,
102                         struct attribute *attr, char *buf)
103 {
104         struct cpu_attribute *cattr =
105                 container_of(attr, struct cpu_attribute, attr);
106         struct cpuquiet_cpu_stat *stat  =
107                 container_of(kobj, struct cpuquiet_cpu_stat, cpu_kobject);
108         ssize_t len = 0;
109         bool was_up = stat->up_down_count & 0x1;
110
111         stats_update(stat, was_up);
112
113         switch (cattr->type) {
114         case up_down_count:
115                 len = sprintf(buf, "%u\n", stat->up_down_count);
116                 break;
117         case time_up_total:
118                 len =  sprintf(buf, "%llu\n", stat->time_up_total);
119                 break;
120         }
121
122         return len;
123 }
124
125 static const struct sysfs_ops stats_sysfs_ops = {
126         .show = stats_sysfs_show,
127 };
128
129 static struct kobj_type ktype_cpu_stats = {
130         .sysfs_ops = &stats_sysfs_ops,
131         .default_attrs = cpu_attributes,
132 };
133
134 int cpuquiet_register_driver(struct cpuquiet_driver *drv)
135 {
136         int err = -EBUSY;
137         unsigned int cpu;
138         struct device *dev;
139         u64 cur_jiffies;
140
141         if (!drv)
142                 return -EINVAL;
143
144         stats = kzalloc(nr_cpu_ids * sizeof(*stats), GFP_KERNEL);
145         if (!stats)
146                 return -ENOMEM;
147
148         for_each_possible_cpu(cpu) {
149                 cur_jiffies = get_jiffies_64();
150                 stats[cpu].last_update = cur_jiffies;
151                 if (cpu_online(cpu))
152                         stats[cpu].up_down_count = 1;
153                 dev = get_cpu_device(cpu);
154                 if (dev) {
155                         cpuquiet_add_dev(dev, cpu);
156                         cpuquiet_cpu_kobject_init(&stats[cpu].cpu_kobject,
157                                         &ktype_cpu_stats, "stats", cpu);
158                 }
159         }
160
161         mutex_lock(&cpuquiet_lock);
162         if (!cpuquiet_curr_driver) {
163                 err = 0;
164                 cpuquiet_curr_driver = drv;
165                 cpuquiet_switch_governor(cpuquiet_get_first_governor());
166         }
167         mutex_unlock(&cpuquiet_lock);
168
169         return err;
170 }
171 EXPORT_SYMBOL(cpuquiet_register_driver);
172
173 struct cpuquiet_driver *cpuquiet_get_driver(void)
174 {
175         return cpuquiet_curr_driver;
176 }
177
178 void cpuquiet_unregister_driver(struct cpuquiet_driver *drv)
179 {
180         unsigned int cpu;
181
182         if (drv != cpuquiet_curr_driver) {
183                 WARN(1, "invalid cpuquiet_unregister_driver(%s)\n",
184                         drv->name);
185                 return;
186         }
187
188         /* stop current governor first */
189         cpuquiet_switch_governor(NULL);
190
191         mutex_lock(&cpuquiet_lock);
192         cpuquiet_curr_driver = NULL;
193
194         for_each_possible_cpu(cpu) {
195                 kobject_put(&stats[cpu].cpu_kobject);
196                 cpuquiet_remove_dev(cpu);
197         }
198
199         mutex_unlock(&cpuquiet_lock);
200 }
201 EXPORT_SYMBOL(cpuquiet_unregister_driver);