2 * sysfs.c - sysfs support
4 * (C) 2006-2007 Shaohua Li <shaohua.li@intel.com>
6 * This code is licenced under the GPL.
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 #include <linux/device.h>
19 static unsigned int sysfs_switch;
20 static int __init cpuidle_sysfs_setup(char *unused)
25 __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
27 static ssize_t show_available_governors(struct device *dev,
28 struct device_attribute *attr,
32 struct cpuidle_governor *tmp;
34 mutex_lock(&cpuidle_lock);
35 list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
36 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
38 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
42 i+= sprintf(&buf[i], "\n");
43 mutex_unlock(&cpuidle_lock);
47 static ssize_t show_current_driver(struct device *dev,
48 struct device_attribute *attr,
52 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
54 spin_lock(&cpuidle_driver_lock);
56 ret = sprintf(buf, "%s\n", cpuidle_driver->name);
58 ret = sprintf(buf, "none\n");
59 spin_unlock(&cpuidle_driver_lock);
64 static ssize_t show_current_governor(struct device *dev,
65 struct device_attribute *attr,
70 mutex_lock(&cpuidle_lock);
71 if (cpuidle_curr_governor)
72 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
74 ret = sprintf(buf, "none\n");
75 mutex_unlock(&cpuidle_lock);
80 static ssize_t store_current_governor(struct device *dev,
81 struct device_attribute *attr,
82 const char *buf, size_t count)
84 char gov_name[CPUIDLE_NAME_LEN];
87 struct cpuidle_governor *gov;
89 if (!len || len >= sizeof(gov_name))
92 memcpy(gov_name, buf, len);
94 if (gov_name[len - 1] == '\n')
95 gov_name[--len] = '\0';
97 mutex_lock(&cpuidle_lock);
99 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
100 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
101 ret = cpuidle_switch_governor(gov);
106 mutex_unlock(&cpuidle_lock);
114 static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);
115 static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
117 static struct attribute *cpuidle_default_attrs[] = {
118 &dev_attr_current_driver.attr,
119 &dev_attr_current_governor_ro.attr,
123 static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL);
124 static DEVICE_ATTR(current_governor, 0644, show_current_governor,
125 store_current_governor);
127 static struct attribute *cpuidle_switch_attrs[] = {
128 &dev_attr_available_governors.attr,
129 &dev_attr_current_driver.attr,
130 &dev_attr_current_governor.attr,
134 static struct attribute_group cpuidle_attr_group = {
135 .attrs = cpuidle_default_attrs,
140 * cpuidle_add_interface - add CPU global sysfs attributes
142 int cpuidle_add_interface(struct device *dev)
145 cpuidle_attr_group.attrs = cpuidle_switch_attrs;
147 return sysfs_create_group(&dev->kobj, &cpuidle_attr_group);
151 * cpuidle_remove_interface - remove CPU global sysfs attributes
153 void cpuidle_remove_interface(struct device *dev)
155 sysfs_remove_group(&dev->kobj, &cpuidle_attr_group);
158 struct cpuidle_attr {
159 struct attribute attr;
160 ssize_t (*show)(struct cpuidle_device *, char *);
161 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
164 #define define_one_ro(_name, show) \
165 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
166 #define define_one_rw(_name, show, store) \
167 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
169 #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
170 #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
171 static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
174 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
175 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
178 mutex_lock(&cpuidle_lock);
179 ret = cattr->show(dev, buf);
180 mutex_unlock(&cpuidle_lock);
185 static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
186 const char * buf, size_t count)
189 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
190 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
193 mutex_lock(&cpuidle_lock);
194 ret = cattr->store(dev, buf, count);
195 mutex_unlock(&cpuidle_lock);
200 static const struct sysfs_ops cpuidle_sysfs_ops = {
201 .show = cpuidle_show,
202 .store = cpuidle_store,
205 static void cpuidle_sysfs_release(struct kobject *kobj)
207 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
209 complete(&dev->kobj_unregister);
212 static struct kobj_type ktype_cpuidle = {
213 .sysfs_ops = &cpuidle_sysfs_ops,
214 .release = cpuidle_sysfs_release,
217 struct cpuidle_state_attr {
218 struct attribute attr;
219 ssize_t (*show)(struct cpuidle_state *, \
220 struct cpuidle_state_usage *, char *);
221 ssize_t (*store)(struct cpuidle_state *, \
222 struct cpuidle_state_usage *, const char *, size_t);
225 #define define_one_state_ro(_name, show) \
226 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
228 #define define_one_state_rw(_name, show, store) \
229 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
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) \
235 return sprintf(buf, "%u\n", state->_name);\
238 #define define_store_state_ull_function(_name) \
239 static ssize_t store_state_##_name(struct cpuidle_state *state, \
240 struct cpuidle_state_usage *state_usage, \
241 const char *buf, size_t size) \
243 unsigned long long value; \
245 if (!capable(CAP_SYS_ADMIN)) \
247 err = kstrtoull(buf, 0, &value); \
251 state_usage->_name = 1; \
253 state_usage->_name = 0; \
257 #define define_show_state_ull_function(_name) \
258 static ssize_t show_state_##_name(struct cpuidle_state *state, \
259 struct cpuidle_state_usage *state_usage, char *buf) \
261 return sprintf(buf, "%llu\n", state_usage->_name);\
264 #define define_show_state_str_function(_name) \
265 static ssize_t show_state_##_name(struct cpuidle_state *state, \
266 struct cpuidle_state_usage *state_usage, char *buf) \
268 if (state->_name[0] == '\0')\
269 return sprintf(buf, "<null>\n");\
270 return sprintf(buf, "%s\n", state->_name);\
273 define_show_state_function(exit_latency)
274 define_show_state_function(power_usage)
275 define_show_state_ull_function(usage)
276 define_show_state_ull_function(time)
277 define_show_state_str_function(name)
278 define_show_state_str_function(desc)
279 define_show_state_ull_function(disable)
280 define_store_state_ull_function(disable)
282 define_one_state_ro(name, show_state_name);
283 define_one_state_ro(desc, show_state_desc);
284 define_one_state_ro(latency, show_state_exit_latency);
285 define_one_state_ro(power, show_state_power_usage);
286 define_one_state_ro(usage, show_state_usage);
287 define_one_state_ro(time, show_state_time);
288 define_one_state_rw(disable, show_state_disable, store_state_disable);
290 static struct attribute *cpuidle_state_default_attrs[] = {
301 struct cpuidle_state_kobj {
302 struct cpuidle_state *state;
303 struct cpuidle_state_usage *state_usage;
304 struct completion kobj_unregister;
308 #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
309 #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
310 #define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
311 #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
312 static ssize_t cpuidle_state_show(struct kobject * kobj,
313 struct attribute * attr ,char * buf)
316 struct cpuidle_state *state = kobj_to_state(kobj);
317 struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
318 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
321 ret = cattr->show(state, state_usage, buf);
326 static ssize_t cpuidle_state_store(struct kobject *kobj,
327 struct attribute *attr, const char *buf, size_t size)
330 struct cpuidle_state *state = kobj_to_state(kobj);
331 struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
332 struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
335 ret = cattr->store(state, state_usage, buf, size);
340 static const struct sysfs_ops cpuidle_state_sysfs_ops = {
341 .show = cpuidle_state_show,
342 .store = cpuidle_state_store,
345 static void cpuidle_state_sysfs_release(struct kobject *kobj)
347 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
349 complete(&state_obj->kobj_unregister);
352 static struct kobj_type ktype_state_cpuidle = {
353 .sysfs_ops = &cpuidle_state_sysfs_ops,
354 .default_attrs = cpuidle_state_default_attrs,
355 .release = cpuidle_state_sysfs_release,
358 static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
360 kobject_put(&device->kobjs[i]->kobj);
361 wait_for_completion(&device->kobjs[i]->kobj_unregister);
362 kfree(device->kobjs[i]);
363 device->kobjs[i] = NULL;
367 * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes
368 * @device: the target device
370 static int cpuidle_add_state_sysfs(struct cpuidle_device *device)
372 int i, ret = -ENOMEM;
373 struct cpuidle_state_kobj *kobj;
374 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
376 /* state statistics */
377 for (i = 0; i < drv->state_count; i++) {
378 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
381 kobj->state = &drv->states[i];
382 kobj->state_usage = &device->states_usage[i];
383 init_completion(&kobj->kobj_unregister);
385 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle,
386 &device->kobj, "state%d", i);
391 kobject_uevent(&kobj->kobj, KOBJ_ADD);
392 device->kobjs[i] = kobj;
398 for (i = i - 1; i >= 0; i--)
399 cpuidle_free_state_kobj(device, i);
404 * cpuidle_remove_driver_sysfs - removes the cpuidle states sysfs attributes
405 * @device: the target device
407 static void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
411 for (i = 0; i < device->state_count; i++)
412 cpuidle_free_state_kobj(device, i);
415 #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
416 #define kobj_to_driver_kobj(k) container_of(k, struct cpuidle_driver_kobj, kobj)
417 #define attr_to_driver_attr(a) container_of(a, struct cpuidle_driver_attr, attr)
419 #define define_one_driver_ro(_name, show) \
420 static struct cpuidle_driver_attr attr_driver_##_name = \
421 __ATTR(_name, 0644, show, NULL)
423 struct cpuidle_driver_kobj {
424 struct cpuidle_driver *drv;
425 struct completion kobj_unregister;
429 struct cpuidle_driver_attr {
430 struct attribute attr;
431 ssize_t (*show)(struct cpuidle_driver *, char *);
432 ssize_t (*store)(struct cpuidle_driver *, const char *, size_t);
435 static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
439 spin_lock(&cpuidle_driver_lock);
440 ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
441 spin_unlock(&cpuidle_driver_lock);
446 static void cpuidle_driver_sysfs_release(struct kobject *kobj)
448 struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
449 complete(&driver_kobj->kobj_unregister);
452 static ssize_t cpuidle_driver_show(struct kobject *kobj, struct attribute * attr,
456 struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
457 struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
460 ret = dattr->show(driver_kobj->drv, buf);
465 static ssize_t cpuidle_driver_store(struct kobject *kobj, struct attribute *attr,
466 const char *buf, size_t size)
469 struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
470 struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
473 ret = dattr->store(driver_kobj->drv, buf, size);
478 define_one_driver_ro(name, show_driver_name);
480 static const struct sysfs_ops cpuidle_driver_sysfs_ops = {
481 .show = cpuidle_driver_show,
482 .store = cpuidle_driver_store,
485 static struct attribute *cpuidle_driver_default_attrs[] = {
486 &attr_driver_name.attr,
490 static struct kobj_type ktype_driver_cpuidle = {
491 .sysfs_ops = &cpuidle_driver_sysfs_ops,
492 .default_attrs = cpuidle_driver_default_attrs,
493 .release = cpuidle_driver_sysfs_release,
497 * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute
498 * @device: the target device
500 static int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
502 struct cpuidle_driver_kobj *kdrv;
503 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
506 kdrv = kzalloc(sizeof(*kdrv), GFP_KERNEL);
511 init_completion(&kdrv->kobj_unregister);
513 ret = kobject_init_and_add(&kdrv->kobj, &ktype_driver_cpuidle,
514 &dev->kobj, "driver");
520 kobject_uevent(&kdrv->kobj, KOBJ_ADD);
521 dev->kobj_driver = kdrv;
527 * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute
528 * @device: the target device
530 static void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
532 struct cpuidle_driver_kobj *kdrv = dev->kobj_driver;
533 kobject_put(&kdrv->kobj);
534 wait_for_completion(&kdrv->kobj_unregister);
538 static inline int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
543 static inline void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
550 * cpuidle_add_device_sysfs - adds device specific sysfs attributes
551 * @device: the target device
553 int cpuidle_add_device_sysfs(struct cpuidle_device *device)
557 ret = cpuidle_add_state_sysfs(device);
561 ret = cpuidle_add_driver_sysfs(device);
563 cpuidle_remove_state_sysfs(device);
568 * cpuidle_remove_device_sysfs : removes device specific sysfs attributes
569 * @device : the target device
571 void cpuidle_remove_device_sysfs(struct cpuidle_device *device)
573 cpuidle_remove_driver_sysfs(device);
574 cpuidle_remove_state_sysfs(device);
578 * cpuidle_add_sysfs - creates a sysfs instance for the target device
579 * @dev: the target device
581 int cpuidle_add_sysfs(struct cpuidle_device *dev)
583 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
586 init_completion(&dev->kobj_unregister);
588 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
591 kobject_uevent(&dev->kobj, KOBJ_ADD);
596 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
597 * @dev: the target device
599 void cpuidle_remove_sysfs(struct cpuidle_device *dev)
601 kobject_put(&dev->kobj);
602 wait_for_completion(&dev->kobj_unregister);