Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / cpuidle / sysfs.c
1 /*
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/slab.h>
13 #include <linux/cpu.h>
14 #include <linux/capability.h>
15
16 #include "cpuidle.h"
17
18 static unsigned int sysfs_switch;
19 static int __init cpuidle_sysfs_setup(char *unused)
20 {
21         sysfs_switch = 1;
22         return 1;
23 }
24 __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
25
26 static ssize_t show_available_governors(struct sysdev_class *class,
27                                         struct sysdev_class_attribute *attr,
28                                         char *buf)
29 {
30         ssize_t i = 0;
31         struct cpuidle_governor *tmp;
32
33         mutex_lock(&cpuidle_lock);
34         list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
35                 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
36                         goto out;
37                 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
38         }
39
40 out:
41         i+= sprintf(&buf[i], "\n");
42         mutex_unlock(&cpuidle_lock);
43         return i;
44 }
45
46 static ssize_t show_current_driver(struct sysdev_class *class,
47                                    struct sysdev_class_attribute *attr,
48                                    char *buf)
49 {
50         ssize_t ret;
51         struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
52
53         spin_lock(&cpuidle_driver_lock);
54         if (cpuidle_driver)
55                 ret = sprintf(buf, "%s\n", cpuidle_driver->name);
56         else
57                 ret = sprintf(buf, "none\n");
58         spin_unlock(&cpuidle_driver_lock);
59
60         return ret;
61 }
62
63 static ssize_t show_current_governor(struct sysdev_class *class,
64                                      struct sysdev_class_attribute *attr,
65                                      char *buf)
66 {
67         ssize_t ret;
68
69         mutex_lock(&cpuidle_lock);
70         if (cpuidle_curr_governor)
71                 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
72         else
73                 ret = sprintf(buf, "none\n");
74         mutex_unlock(&cpuidle_lock);
75
76         return ret;
77 }
78
79 static ssize_t store_current_governor(struct sysdev_class *class,
80                                       struct sysdev_class_attribute *attr,
81                                       const char *buf, size_t count)
82 {
83         char gov_name[CPUIDLE_NAME_LEN];
84         int ret = -EINVAL;
85         size_t len = count;
86         struct cpuidle_governor *gov;
87
88         if (!len || len >= sizeof(gov_name))
89                 return -EINVAL;
90
91         memcpy(gov_name, buf, len);
92         gov_name[len] = '\0';
93         if (gov_name[len - 1] == '\n')
94                 gov_name[--len] = '\0';
95
96         mutex_lock(&cpuidle_lock);
97
98         list_for_each_entry(gov, &cpuidle_governors, governor_list) {
99                 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
100                         ret = cpuidle_switch_governor(gov);
101                         break;
102                 }
103         }
104
105         mutex_unlock(&cpuidle_lock);
106
107         if (ret)
108                 return ret;
109         else
110                 return count;
111 }
112
113 static SYSDEV_CLASS_ATTR(current_driver, 0444, show_current_driver, NULL);
114 static SYSDEV_CLASS_ATTR(current_governor_ro, 0444, show_current_governor,
115                          NULL);
116
117 static struct attribute *cpuclass_default_attrs[] = {
118         &attr_current_driver.attr,
119         &attr_current_governor_ro.attr,
120         NULL
121 };
122
123 static SYSDEV_CLASS_ATTR(available_governors, 0444, show_available_governors,
124                          NULL);
125 static SYSDEV_CLASS_ATTR(current_governor, 0644, show_current_governor,
126                          store_current_governor);
127
128 static struct attribute *cpuclass_switch_attrs[] = {
129         &attr_available_governors.attr,
130         &attr_current_driver.attr,
131         &attr_current_governor.attr,
132         NULL
133 };
134
135 static struct attribute_group cpuclass_attr_group = {
136         .attrs = cpuclass_default_attrs,
137         .name = "cpuidle",
138 };
139
140 /**
141  * cpuidle_add_class_sysfs - add CPU global sysfs attributes
142  */
143 int cpuidle_add_class_sysfs(struct sysdev_class *cls)
144 {
145         if (sysfs_switch)
146                 cpuclass_attr_group.attrs = cpuclass_switch_attrs;
147
148         return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group);
149 }
150
151 /**
152  * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes
153  */
154 void cpuidle_remove_class_sysfs(struct sysdev_class *cls)
155 {
156         sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group);
157 }
158
159 struct cpuidle_attr {
160         struct attribute attr;
161         ssize_t (*show)(struct cpuidle_device *, char *);
162         ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
163 };
164
165 #define define_one_ro(_name, show) \
166         static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
167 #define define_one_rw(_name, show, store) \
168         static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
169
170 #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
171 #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
172 static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
173 {
174         int ret = -EIO;
175         struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
176         struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
177
178         if (cattr->show) {
179                 mutex_lock(&cpuidle_lock);
180                 ret = cattr->show(dev, buf);
181                 mutex_unlock(&cpuidle_lock);
182         }
183         return ret;
184 }
185
186 static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
187                      const char * buf, size_t count)
188 {
189         int ret = -EIO;
190         struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
191         struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
192
193         if (cattr->store) {
194                 mutex_lock(&cpuidle_lock);
195                 ret = cattr->store(dev, buf, count);
196                 mutex_unlock(&cpuidle_lock);
197         }
198         return ret;
199 }
200
201 static const struct sysfs_ops cpuidle_sysfs_ops = {
202         .show = cpuidle_show,
203         .store = cpuidle_store,
204 };
205
206 static void cpuidle_sysfs_release(struct kobject *kobj)
207 {
208         struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
209
210         complete(&dev->kobj_unregister);
211 }
212
213 static struct kobj_type ktype_cpuidle = {
214         .sysfs_ops = &cpuidle_sysfs_ops,
215         .release = cpuidle_sysfs_release,
216 };
217
218 struct cpuidle_state_attr {
219         struct attribute attr;
220         ssize_t (*show)(struct cpuidle_state *, \
221                                         struct cpuidle_state_usage *, char *);
222         ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
223 };
224
225 #define define_one_state_ro(_name, show) \
226 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
227
228 #define define_one_state_rw(_name, show, store) \
229 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
230
231 #define define_show_state_function(_name) \
232 static ssize_t show_state_##_name(struct cpuidle_state *state, \
233                          struct cpuidle_state_usage *state_usage, char *buf) \
234 { \
235         return sprintf(buf, "%u\n", state->_name);\
236 }
237
238 #define define_store_state_function(_name) \
239 static ssize_t store_state_##_name(struct cpuidle_state *state, \
240                 const char *buf, size_t size) \
241 { \
242         long value; \
243         int err; \
244         if (!capable(CAP_SYS_ADMIN)) \
245                 return -EPERM; \
246         err = kstrtol(buf, 0, &value); \
247         if (err) \
248                 return err; \
249         if (value) \
250                 state->disable = 1; \
251         else \
252                 state->disable = 0; \
253         return size; \
254 }
255
256 #define define_show_state_ull_function(_name) \
257 static ssize_t show_state_##_name(struct cpuidle_state *state, \
258                         struct cpuidle_state_usage *state_usage, char *buf) \
259 { \
260         return sprintf(buf, "%llu\n", state_usage->_name);\
261 }
262
263 #define define_show_state_str_function(_name) \
264 static ssize_t show_state_##_name(struct cpuidle_state *state, \
265                         struct cpuidle_state_usage *state_usage, char *buf) \
266 { \
267         if (state->_name[0] == '\0')\
268                 return sprintf(buf, "<null>\n");\
269         return sprintf(buf, "%s\n", state->_name);\
270 }
271
272 define_show_state_function(exit_latency)
273 define_show_state_function(power_usage)
274 define_show_state_ull_function(usage)
275 define_show_state_ull_function(time)
276 define_show_state_str_function(name)
277 define_show_state_str_function(desc)
278 define_show_state_function(disable)
279 define_store_state_function(disable)
280
281 define_one_state_ro(name, show_state_name);
282 define_one_state_ro(desc, show_state_desc);
283 define_one_state_ro(latency, show_state_exit_latency);
284 define_one_state_ro(power, show_state_power_usage);
285 define_one_state_ro(usage, show_state_usage);
286 define_one_state_ro(time, show_state_time);
287 define_one_state_rw(disable, show_state_disable, store_state_disable);
288
289 static struct attribute *cpuidle_state_default_attrs[] = {
290         &attr_name.attr,
291         &attr_desc.attr,
292         &attr_latency.attr,
293         &attr_power.attr,
294         &attr_usage.attr,
295         &attr_time.attr,
296         &attr_disable.attr,
297         NULL
298 };
299
300 #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
301 #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
302 #define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
303 #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
304 static ssize_t cpuidle_state_show(struct kobject * kobj,
305         struct attribute * attr ,char * buf)
306 {
307         int ret = -EIO;
308         struct cpuidle_state *state = kobj_to_state(kobj);
309         struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
310         struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
311
312         if (cattr->show)
313                 ret = cattr->show(state, state_usage, buf);
314
315         return ret;
316 }
317
318 static ssize_t cpuidle_state_store(struct kobject *kobj,
319         struct attribute *attr, const char *buf, size_t size)
320 {
321         int ret = -EIO;
322         struct cpuidle_state *state = kobj_to_state(kobj);
323         struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
324
325         if (cattr->store)
326                 ret = cattr->store(state, buf, size);
327
328         return ret;
329 }
330
331 static const struct sysfs_ops cpuidle_state_sysfs_ops = {
332         .show = cpuidle_state_show,
333         .store = cpuidle_state_store,
334 };
335
336 static void cpuidle_state_sysfs_release(struct kobject *kobj)
337 {
338         struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
339
340         complete(&state_obj->kobj_unregister);
341 }
342
343 static struct kobj_type ktype_state_cpuidle = {
344         .sysfs_ops = &cpuidle_state_sysfs_ops,
345         .default_attrs = cpuidle_state_default_attrs,
346         .release = cpuidle_state_sysfs_release,
347 };
348
349 static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
350 {
351         kobject_put(&device->kobjs[i]->kobj);
352         wait_for_completion(&device->kobjs[i]->kobj_unregister);
353         kfree(device->kobjs[i]);
354         device->kobjs[i] = NULL;
355 }
356
357 /**
358  * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
359  * @device: the target device
360  */
361 int cpuidle_add_state_sysfs(struct cpuidle_device *device)
362 {
363         int i, ret = -ENOMEM;
364         struct cpuidle_state_kobj *kobj;
365         struct cpuidle_driver *drv = cpuidle_get_driver();
366
367         /* state statistics */
368         for (i = 0; i < device->state_count; i++) {
369                 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
370                 if (!kobj)
371                         goto error_state;
372                 kobj->state = &drv->states[i];
373                 kobj->state_usage = &device->states_usage[i];
374                 init_completion(&kobj->kobj_unregister);
375
376                 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
377                                            "state%d", i);
378                 if (ret) {
379                         kfree(kobj);
380                         goto error_state;
381                 }
382                 kobject_uevent(&kobj->kobj, KOBJ_ADD);
383                 device->kobjs[i] = kobj;
384         }
385
386         return 0;
387
388 error_state:
389         for (i = i - 1; i >= 0; i--)
390                 cpuidle_free_state_kobj(device, i);
391         return ret;
392 }
393
394 /**
395  * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
396  * @device: the target device
397  */
398 void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
399 {
400         int i;
401
402         for (i = 0; i < device->state_count; i++)
403                 cpuidle_free_state_kobj(device, i);
404 }
405
406 /**
407  * cpuidle_add_sysfs - creates a sysfs instance for the target device
408  * @sysdev: the target device
409  */
410 int cpuidle_add_sysfs(struct sys_device *sysdev)
411 {
412         int cpu = sysdev->id;
413         struct cpuidle_device *dev;
414         int error;
415
416         dev = per_cpu(cpuidle_devices, cpu);
417         error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
418                                      "cpuidle");
419         if (!error)
420                 kobject_uevent(&dev->kobj, KOBJ_ADD);
421         return error;
422 }
423
424 /**
425  * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
426  * @sysdev: the target device
427  */
428 void cpuidle_remove_sysfs(struct sys_device *sysdev)
429 {
430         int cpu = sysdev->id;
431         struct cpuidle_device *dev;
432
433         dev = per_cpu(cpuidle_devices, cpu);
434         kobject_put(&dev->kobj);
435 }