blob: 31a0e0b455b6e3c61ffef1ef004351fac0085dcd [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
2 * sysfs.c - sysfs support
3 *
4 * (C) 2006-2007 Shaohua Li <shaohua.li@intel.com>
5 *
6 * This code is licenced under the GPL.
7 */
8
9#include <linux/kernel.h>
10#include <linux/cpuidle.h>
11#include <linux/sysfs.h>
12#include <linux/cpu.h>
13
14#include "cpuidle.h"
15
16static unsigned int sysfs_switch;
17static int __init cpuidle_sysfs_setup(char *unused)
18{
19 sysfs_switch = 1;
20 return 1;
21}
22__setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
23
Andi Kleen4a0b2b42008-07-01 18:48:41 +020024static ssize_t show_available_governors(struct sys_device *dev,
25 struct sysdev_attribute *attr, char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040026{
27 ssize_t i = 0;
28 struct cpuidle_governor *tmp;
29
30 mutex_lock(&cpuidle_lock);
31 list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
32 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
33 goto out;
34 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
35 }
36
37out:
38 i+= sprintf(&buf[i], "\n");
39 mutex_unlock(&cpuidle_lock);
40 return i;
41}
42
Andi Kleen4a0b2b42008-07-01 18:48:41 +020043static ssize_t show_current_driver(struct sys_device *dev,
44 struct sysdev_attribute *attr, char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040045{
46 ssize_t ret;
47
48 spin_lock(&cpuidle_driver_lock);
49 if (cpuidle_curr_driver)
50 ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name);
51 else
52 ret = sprintf(buf, "none\n");
53 spin_unlock(&cpuidle_driver_lock);
54
55 return ret;
56}
57
Andi Kleen4a0b2b42008-07-01 18:48:41 +020058static ssize_t show_current_governor(struct sys_device *dev,
59 struct sysdev_attribute *attr, char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040060{
61 ssize_t ret;
62
63 mutex_lock(&cpuidle_lock);
64 if (cpuidle_curr_governor)
65 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
66 else
67 ret = sprintf(buf, "none\n");
68 mutex_unlock(&cpuidle_lock);
69
70 return ret;
71}
72
73static ssize_t store_current_governor(struct sys_device *dev,
Andi Kleen4a0b2b42008-07-01 18:48:41 +020074 struct sysdev_attribute *attr,
Len Brown4f86d3a2007-10-03 18:58:00 -040075 const char *buf, size_t count)
76{
77 char gov_name[CPUIDLE_NAME_LEN];
78 int ret = -EINVAL;
79 size_t len = count;
80 struct cpuidle_governor *gov;
81
82 if (!len || len >= sizeof(gov_name))
83 return -EINVAL;
84
85 memcpy(gov_name, buf, len);
86 gov_name[len] = '\0';
87 if (gov_name[len - 1] == '\n')
88 gov_name[--len] = '\0';
89
90 mutex_lock(&cpuidle_lock);
91
92 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
93 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
94 ret = cpuidle_switch_governor(gov);
95 break;
96 }
97 }
98
99 mutex_unlock(&cpuidle_lock);
100
101 if (ret)
102 return ret;
103 else
104 return count;
105}
106
107static SYSDEV_ATTR(current_driver, 0444, show_current_driver, NULL);
108static SYSDEV_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
109
110static struct attribute *cpuclass_default_attrs[] = {
111 &attr_current_driver.attr,
112 &attr_current_governor_ro.attr,
113 NULL
114};
115
116static SYSDEV_ATTR(available_governors, 0444, show_available_governors, NULL);
117static SYSDEV_ATTR(current_governor, 0644, show_current_governor,
118 store_current_governor);
119
120static struct attribute *cpuclass_switch_attrs[] = {
121 &attr_available_governors.attr,
122 &attr_current_driver.attr,
123 &attr_current_governor.attr,
124 NULL
125};
126
127static struct attribute_group cpuclass_attr_group = {
128 .attrs = cpuclass_default_attrs,
129 .name = "cpuidle",
130};
131
132/**
133 * cpuidle_add_class_sysfs - add CPU global sysfs attributes
134 */
135int cpuidle_add_class_sysfs(struct sysdev_class *cls)
136{
137 if (sysfs_switch)
138 cpuclass_attr_group.attrs = cpuclass_switch_attrs;
139
140 return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group);
141}
142
143/**
144 * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes
145 */
146void cpuidle_remove_class_sysfs(struct sysdev_class *cls)
147{
148 sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group);
149}
150
151struct cpuidle_attr {
152 struct attribute attr;
153 ssize_t (*show)(struct cpuidle_device *, char *);
154 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
155};
156
157#define define_one_ro(_name, show) \
158 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
159#define define_one_rw(_name, show, store) \
160 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
161
162#define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
163#define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
164static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
165{
166 int ret = -EIO;
167 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
168 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
169
170 if (cattr->show) {
171 mutex_lock(&cpuidle_lock);
172 ret = cattr->show(dev, buf);
173 mutex_unlock(&cpuidle_lock);
174 }
175 return ret;
176}
177
178static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
179 const char * buf, size_t count)
180{
181 int ret = -EIO;
182 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
183 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
184
185 if (cattr->store) {
186 mutex_lock(&cpuidle_lock);
187 ret = cattr->store(dev, buf, count);
188 mutex_unlock(&cpuidle_lock);
189 }
190 return ret;
191}
192
193static struct sysfs_ops cpuidle_sysfs_ops = {
194 .show = cpuidle_show,
195 .store = cpuidle_store,
196};
197
198static void cpuidle_sysfs_release(struct kobject *kobj)
199{
200 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
201
202 complete(&dev->kobj_unregister);
203}
204
205static struct kobj_type ktype_cpuidle = {
206 .sysfs_ops = &cpuidle_sysfs_ops,
207 .release = cpuidle_sysfs_release,
208};
209
210struct cpuidle_state_attr {
211 struct attribute attr;
212 ssize_t (*show)(struct cpuidle_state *, char *);
213 ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
214};
215
216#define define_one_state_ro(_name, show) \
217static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
218
219#define define_show_state_function(_name) \
220static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
221{ \
222 return sprintf(buf, "%u\n", state->_name);\
223}
224
Yi Yang8b78cf62008-02-25 08:46:12 +0800225#define define_show_state_ull_function(_name) \
226static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
227{ \
228 return sprintf(buf, "%llu\n", state->_name);\
229}
230
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800231#define define_show_state_str_function(_name) \
232static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
233{ \
234 if (state->_name[0] == '\0')\
235 return sprintf(buf, "<null>\n");\
236 return sprintf(buf, "%s\n", state->_name);\
Len Brown4f86d3a2007-10-03 18:58:00 -0400237}
238
239define_show_state_function(exit_latency)
240define_show_state_function(power_usage)
Yi Yang8b78cf62008-02-25 08:46:12 +0800241define_show_state_ull_function(usage)
242define_show_state_ull_function(time)
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800243define_show_state_str_function(name)
244define_show_state_str_function(desc)
245
Len Brown4f86d3a2007-10-03 18:58:00 -0400246define_one_state_ro(name, show_state_name);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800247define_one_state_ro(desc, show_state_desc);
Len Brown4f86d3a2007-10-03 18:58:00 -0400248define_one_state_ro(latency, show_state_exit_latency);
249define_one_state_ro(power, show_state_power_usage);
250define_one_state_ro(usage, show_state_usage);
251define_one_state_ro(time, show_state_time);
252
253static struct attribute *cpuidle_state_default_attrs[] = {
254 &attr_name.attr,
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800255 &attr_desc.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400256 &attr_latency.attr,
257 &attr_power.attr,
258 &attr_usage.attr,
259 &attr_time.attr,
260 NULL
261};
262
263#define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
264#define kobj_to_state(k) (kobj_to_state_obj(k)->state)
265#define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
266static ssize_t cpuidle_state_show(struct kobject * kobj,
267 struct attribute * attr ,char * buf)
268{
269 int ret = -EIO;
270 struct cpuidle_state *state = kobj_to_state(kobj);
271 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
272
273 if (cattr->show)
274 ret = cattr->show(state, buf);
275
276 return ret;
277}
278
279static struct sysfs_ops cpuidle_state_sysfs_ops = {
280 .show = cpuidle_state_show,
281};
282
283static void cpuidle_state_sysfs_release(struct kobject *kobj)
284{
285 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
286
287 complete(&state_obj->kobj_unregister);
288}
289
290static struct kobj_type ktype_state_cpuidle = {
291 .sysfs_ops = &cpuidle_state_sysfs_ops,
292 .default_attrs = cpuidle_state_default_attrs,
293 .release = cpuidle_state_sysfs_release,
294};
295
296static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
297{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800298 kobject_put(&device->kobjs[i]->kobj);
Len Brown4f86d3a2007-10-03 18:58:00 -0400299 wait_for_completion(&device->kobjs[i]->kobj_unregister);
300 kfree(device->kobjs[i]);
301 device->kobjs[i] = NULL;
302}
303
304/**
305 * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
306 * @device: the target device
307 */
308int cpuidle_add_state_sysfs(struct cpuidle_device *device)
309{
310 int i, ret = -ENOMEM;
311 struct cpuidle_state_kobj *kobj;
312
313 /* state statistics */
314 for (i = 0; i < device->state_count; i++) {
315 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
316 if (!kobj)
317 goto error_state;
318 kobj->state = &device->states[i];
319 init_completion(&kobj->kobj_unregister);
320
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400321 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
322 "state%d", i);
Len Brown4f86d3a2007-10-03 18:58:00 -0400323 if (ret) {
324 kfree(kobj);
325 goto error_state;
326 }
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400327 kobject_uevent(&kobj->kobj, KOBJ_ADD);
Len Brown4f86d3a2007-10-03 18:58:00 -0400328 device->kobjs[i] = kobj;
329 }
330
331 return 0;
332
333error_state:
334 for (i = i - 1; i >= 0; i--)
335 cpuidle_free_state_kobj(device, i);
336 return ret;
337}
338
339/**
340 * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
341 * @device: the target device
342 */
343void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
344{
345 int i;
346
347 for (i = 0; i < device->state_count; i++)
348 cpuidle_free_state_kobj(device, i);
349}
350
351/**
352 * cpuidle_add_sysfs - creates a sysfs instance for the target device
353 * @sysdev: the target device
354 */
355int cpuidle_add_sysfs(struct sys_device *sysdev)
356{
357 int cpu = sysdev->id;
358 struct cpuidle_device *dev;
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400359 int error;
Len Brown4f86d3a2007-10-03 18:58:00 -0400360
361 dev = per_cpu(cpuidle_devices, cpu);
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400362 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
363 "cpuidle");
364 if (!error)
365 kobject_uevent(&dev->kobj, KOBJ_ADD);
366 return error;
Len Brown4f86d3a2007-10-03 18:58:00 -0400367}
368
369/**
370 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
371 * @sysdev: the target device
372 */
373void cpuidle_remove_sysfs(struct sys_device *sysdev)
374{
375 int cpu = sysdev->id;
376 struct cpuidle_device *dev;
377
378 dev = per_cpu(cpuidle_devices, cpu);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800379 kobject_put(&dev->kobj);
Len Brown4f86d3a2007-10-03 18:58:00 -0400380}