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/completion.h>
15 #include <linux/capability.h>
16 #include <linux/device.h>
17 #include <linux/kobject.h>
21 static ssize_t
show_available_governors(struct device
*dev
,
22 struct device_attribute
*attr
,
26 struct cpuidle_governor
*tmp
;
28 mutex_lock(&cpuidle_lock
);
29 list_for_each_entry(tmp
, &cpuidle_governors
, governor_list
) {
30 if (i
>= (ssize_t
) (PAGE_SIZE
- (CPUIDLE_NAME_LEN
+ 2)))
33 i
+= scnprintf(&buf
[i
], CPUIDLE_NAME_LEN
+ 1, "%s ", tmp
->name
);
37 i
+= sprintf(&buf
[i
], "\n");
38 mutex_unlock(&cpuidle_lock
);
42 static ssize_t
show_current_driver(struct device
*dev
,
43 struct device_attribute
*attr
,
47 struct cpuidle_driver
*drv
;
49 spin_lock(&cpuidle_driver_lock
);
50 drv
= cpuidle_get_driver();
52 ret
= sprintf(buf
, "%s\n", drv
->name
);
54 ret
= sprintf(buf
, "none\n");
55 spin_unlock(&cpuidle_driver_lock
);
60 static ssize_t
show_current_governor(struct device
*dev
,
61 struct device_attribute
*attr
,
66 mutex_lock(&cpuidle_lock
);
67 if (cpuidle_curr_governor
)
68 ret
= sprintf(buf
, "%s\n", cpuidle_curr_governor
->name
);
70 ret
= sprintf(buf
, "none\n");
71 mutex_unlock(&cpuidle_lock
);
76 static ssize_t
store_current_governor(struct device
*dev
,
77 struct device_attribute
*attr
,
78 const char *buf
, size_t count
)
80 char gov_name
[CPUIDLE_NAME_LEN
+ 1];
82 struct cpuidle_governor
*gov
;
84 ret
= sscanf(buf
, "%" __stringify(CPUIDLE_NAME_LEN
) "s", gov_name
);
88 mutex_lock(&cpuidle_lock
);
90 list_for_each_entry(gov
, &cpuidle_governors
, governor_list
) {
91 if (!strncmp(gov
->name
, gov_name
, CPUIDLE_NAME_LEN
)) {
92 ret
= cpuidle_switch_governor(gov
);
96 mutex_unlock(&cpuidle_lock
);
98 return ret
? ret
: count
;
101 static DEVICE_ATTR(available_governors
, 0444, show_available_governors
, NULL
);
102 static DEVICE_ATTR(current_driver
, 0444, show_current_driver
, NULL
);
103 static DEVICE_ATTR(current_governor
, 0644, show_current_governor
,
104 store_current_governor
);
105 static DEVICE_ATTR(current_governor_ro
, 0444, show_current_governor
, NULL
);
107 static struct attribute
*cpuidle_attrs
[] = {
108 &dev_attr_available_governors
.attr
,
109 &dev_attr_current_driver
.attr
,
110 &dev_attr_current_governor
.attr
,
111 &dev_attr_current_governor_ro
.attr
,
115 static struct attribute_group cpuidle_attr_group
= {
116 .attrs
= cpuidle_attrs
,
121 * cpuidle_add_interface - add CPU global sysfs attributes
123 int cpuidle_add_interface(void)
125 struct device
*dev_root
= bus_get_dev_root(&cpu_subsys
);
131 retval
= sysfs_create_group(&dev_root
->kobj
, &cpuidle_attr_group
);
132 put_device(dev_root
);
137 * cpuidle_remove_interface - remove CPU global sysfs attributes
138 * @dev: the target device
140 void cpuidle_remove_interface(struct device
*dev
)
142 sysfs_remove_group(&dev
->kobj
, &cpuidle_attr_group
);
145 struct cpuidle_attr
{
146 struct attribute attr
;
147 ssize_t (*show
)(struct cpuidle_device
*, char *);
148 ssize_t (*store
)(struct cpuidle_device
*, const char *, size_t count
);
151 #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
153 struct cpuidle_device_kobj
{
154 struct cpuidle_device
*dev
;
155 struct completion kobj_unregister
;
159 static inline struct cpuidle_device
*to_cpuidle_device(struct kobject
*kobj
)
161 struct cpuidle_device_kobj
*kdev
=
162 container_of(kobj
, struct cpuidle_device_kobj
, kobj
);
167 static ssize_t
cpuidle_show(struct kobject
*kobj
, struct attribute
*attr
,
171 struct cpuidle_device
*dev
= to_cpuidle_device(kobj
);
172 struct cpuidle_attr
*cattr
= attr_to_cpuidleattr(attr
);
175 mutex_lock(&cpuidle_lock
);
176 ret
= cattr
->show(dev
, buf
);
177 mutex_unlock(&cpuidle_lock
);
182 static ssize_t
cpuidle_store(struct kobject
*kobj
, struct attribute
*attr
,
183 const char *buf
, size_t count
)
186 struct cpuidle_device
*dev
= to_cpuidle_device(kobj
);
187 struct cpuidle_attr
*cattr
= attr_to_cpuidleattr(attr
);
190 mutex_lock(&cpuidle_lock
);
191 ret
= cattr
->store(dev
, buf
, count
);
192 mutex_unlock(&cpuidle_lock
);
197 static const struct sysfs_ops cpuidle_sysfs_ops
= {
198 .show
= cpuidle_show
,
199 .store
= cpuidle_store
,
202 static void cpuidle_sysfs_release(struct kobject
*kobj
)
204 struct cpuidle_device_kobj
*kdev
=
205 container_of(kobj
, struct cpuidle_device_kobj
, kobj
);
207 complete(&kdev
->kobj_unregister
);
210 static const struct kobj_type ktype_cpuidle
= {
211 .sysfs_ops
= &cpuidle_sysfs_ops
,
212 .release
= cpuidle_sysfs_release
,
215 struct cpuidle_state_attr
{
216 struct attribute attr
;
217 ssize_t (*show
)(struct cpuidle_state
*, \
218 struct cpuidle_state_usage
*, char *);
219 ssize_t (*store
)(struct cpuidle_state
*, \
220 struct cpuidle_state_usage
*, const char *, size_t);
223 #define define_one_state_ro(_name, show) \
224 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
226 #define define_one_state_rw(_name, show, store) \
227 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
229 #define define_show_state_function(_name) \
230 static ssize_t show_state_##_name(struct cpuidle_state *state, \
231 struct cpuidle_state_usage *state_usage, char *buf) \
233 return sprintf(buf, "%u\n", state->_name);\
236 #define define_show_state_ull_function(_name) \
237 static ssize_t show_state_##_name(struct cpuidle_state *state, \
238 struct cpuidle_state_usage *state_usage, \
241 return sprintf(buf, "%llu\n", state_usage->_name);\
244 #define define_show_state_str_function(_name) \
245 static ssize_t show_state_##_name(struct cpuidle_state *state, \
246 struct cpuidle_state_usage *state_usage, \
249 if (state->_name[0] == '\0')\
250 return sprintf(buf, "<null>\n");\
251 return sprintf(buf, "%s\n", state->_name);\
254 #define define_show_state_time_function(_name) \
255 static ssize_t show_state_##_name(struct cpuidle_state *state, \
256 struct cpuidle_state_usage *state_usage, \
259 return sprintf(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
262 define_show_state_time_function(exit_latency
)
263 define_show_state_time_function(target_residency
)
264 define_show_state_function(power_usage
)
265 define_show_state_ull_function(usage
)
266 define_show_state_ull_function(rejected
)
267 define_show_state_str_function(name
)
268 define_show_state_str_function(desc
)
269 define_show_state_ull_function(above
)
270 define_show_state_ull_function(below
)
272 static ssize_t
show_state_time(struct cpuidle_state
*state
,
273 struct cpuidle_state_usage
*state_usage
,
276 return sprintf(buf
, "%llu\n", ktime_to_us(state_usage
->time_ns
));
279 static ssize_t
show_state_disable(struct cpuidle_state
*state
,
280 struct cpuidle_state_usage
*state_usage
,
283 return sprintf(buf
, "%llu\n",
284 state_usage
->disable
& CPUIDLE_STATE_DISABLED_BY_USER
);
287 static ssize_t
store_state_disable(struct cpuidle_state
*state
,
288 struct cpuidle_state_usage
*state_usage
,
289 const char *buf
, size_t size
)
294 if (!capable(CAP_SYS_ADMIN
))
297 err
= kstrtouint(buf
, 0, &value
);
302 state_usage
->disable
|= CPUIDLE_STATE_DISABLED_BY_USER
;
304 state_usage
->disable
&= ~CPUIDLE_STATE_DISABLED_BY_USER
;
309 static ssize_t
show_state_default_status(struct cpuidle_state
*state
,
310 struct cpuidle_state_usage
*state_usage
,
313 return sprintf(buf
, "%s\n",
314 state
->flags
& CPUIDLE_FLAG_OFF
? "disabled" : "enabled");
317 define_one_state_ro(name
, show_state_name
);
318 define_one_state_ro(desc
, show_state_desc
);
319 define_one_state_ro(latency
, show_state_exit_latency
);
320 define_one_state_ro(residency
, show_state_target_residency
);
321 define_one_state_ro(power
, show_state_power_usage
);
322 define_one_state_ro(usage
, show_state_usage
);
323 define_one_state_ro(rejected
, show_state_rejected
);
324 define_one_state_ro(time
, show_state_time
);
325 define_one_state_rw(disable
, show_state_disable
, store_state_disable
);
326 define_one_state_ro(above
, show_state_above
);
327 define_one_state_ro(below
, show_state_below
);
328 define_one_state_ro(default_status
, show_state_default_status
);
330 static struct attribute
*cpuidle_state_default_attrs
[] = {
334 &attr_residency
.attr
,
342 &attr_default_status
.attr
,
345 ATTRIBUTE_GROUPS(cpuidle_state_default
);
347 struct cpuidle_state_kobj
{
348 struct cpuidle_state
*state
;
349 struct cpuidle_state_usage
*state_usage
;
350 struct completion kobj_unregister
;
352 struct cpuidle_device
*device
;
355 #ifdef CONFIG_SUSPEND
356 #define define_show_state_s2idle_ull_function(_name) \
357 static ssize_t show_state_s2idle_##_name(struct cpuidle_state *state, \
358 struct cpuidle_state_usage *state_usage, \
361 return sprintf(buf, "%llu\n", state_usage->s2idle_##_name);\
364 define_show_state_s2idle_ull_function(usage
);
365 define_show_state_s2idle_ull_function(time
);
367 #define define_one_state_s2idle_ro(_name, show) \
368 static struct cpuidle_state_attr attr_s2idle_##_name = \
369 __ATTR(_name, 0444, show, NULL)
371 define_one_state_s2idle_ro(usage
, show_state_s2idle_usage
);
372 define_one_state_s2idle_ro(time
, show_state_s2idle_time
);
374 static struct attribute
*cpuidle_state_s2idle_attrs
[] = {
375 &attr_s2idle_usage
.attr
,
376 &attr_s2idle_time
.attr
,
380 static const struct attribute_group cpuidle_state_s2idle_group
= {
382 .attrs
= cpuidle_state_s2idle_attrs
,
385 static void cpuidle_add_s2idle_attr_group(struct cpuidle_state_kobj
*kobj
)
389 if (!kobj
->state
->enter_s2idle
)
392 ret
= sysfs_create_group(&kobj
->kobj
, &cpuidle_state_s2idle_group
);
394 pr_debug("%s: sysfs attribute group not created\n", __func__
);
397 static void cpuidle_remove_s2idle_attr_group(struct cpuidle_state_kobj
*kobj
)
399 if (kobj
->state
->enter_s2idle
)
400 sysfs_remove_group(&kobj
->kobj
, &cpuidle_state_s2idle_group
);
403 static inline void cpuidle_add_s2idle_attr_group(struct cpuidle_state_kobj
*kobj
) { }
404 static inline void cpuidle_remove_s2idle_attr_group(struct cpuidle_state_kobj
*kobj
) { }
405 #endif /* CONFIG_SUSPEND */
407 #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
408 #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
409 #define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
410 #define kobj_to_device(k) (kobj_to_state_obj(k)->device)
411 #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
413 static ssize_t
cpuidle_state_show(struct kobject
*kobj
, struct attribute
*attr
,
417 struct cpuidle_state
*state
= kobj_to_state(kobj
);
418 struct cpuidle_state_usage
*state_usage
= kobj_to_state_usage(kobj
);
419 struct cpuidle_state_attr
*cattr
= attr_to_stateattr(attr
);
422 ret
= cattr
->show(state
, state_usage
, buf
);
427 static ssize_t
cpuidle_state_store(struct kobject
*kobj
, struct attribute
*attr
,
428 const char *buf
, size_t size
)
431 struct cpuidle_state
*state
= kobj_to_state(kobj
);
432 struct cpuidle_state_usage
*state_usage
= kobj_to_state_usage(kobj
);
433 struct cpuidle_state_attr
*cattr
= attr_to_stateattr(attr
);
434 struct cpuidle_device
*dev
= kobj_to_device(kobj
);
437 ret
= cattr
->store(state
, state_usage
, buf
, size
);
439 /* reset poll time cache */
440 dev
->poll_limit_ns
= 0;
445 static const struct sysfs_ops cpuidle_state_sysfs_ops
= {
446 .show
= cpuidle_state_show
,
447 .store
= cpuidle_state_store
,
450 static void cpuidle_state_sysfs_release(struct kobject
*kobj
)
452 struct cpuidle_state_kobj
*state_obj
= kobj_to_state_obj(kobj
);
454 complete(&state_obj
->kobj_unregister
);
457 static const struct kobj_type ktype_state_cpuidle
= {
458 .sysfs_ops
= &cpuidle_state_sysfs_ops
,
459 .default_groups
= cpuidle_state_default_groups
,
460 .release
= cpuidle_state_sysfs_release
,
463 static inline void cpuidle_free_state_kobj(struct cpuidle_device
*device
, int i
)
465 cpuidle_remove_s2idle_attr_group(device
->kobjs
[i
]);
466 kobject_put(&device
->kobjs
[i
]->kobj
);
467 wait_for_completion(&device
->kobjs
[i
]->kobj_unregister
);
468 kfree(device
->kobjs
[i
]);
469 device
->kobjs
[i
] = NULL
;
473 * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes
474 * @device: the target device
476 static int cpuidle_add_state_sysfs(struct cpuidle_device
*device
)
478 int i
, ret
= -ENOMEM
;
479 struct cpuidle_state_kobj
*kobj
;
480 struct cpuidle_device_kobj
*kdev
= device
->kobj_dev
;
481 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(device
);
483 /* state statistics */
484 for (i
= 0; i
< drv
->state_count
; i
++) {
485 kobj
= kzalloc(sizeof(struct cpuidle_state_kobj
), GFP_KERNEL
);
490 kobj
->state
= &drv
->states
[i
];
491 kobj
->state_usage
= &device
->states_usage
[i
];
492 kobj
->device
= device
;
493 init_completion(&kobj
->kobj_unregister
);
495 ret
= kobject_init_and_add(&kobj
->kobj
, &ktype_state_cpuidle
,
496 &kdev
->kobj
, "state%d", i
);
498 kobject_put(&kobj
->kobj
);
502 cpuidle_add_s2idle_attr_group(kobj
);
503 kobject_uevent(&kobj
->kobj
, KOBJ_ADD
);
504 device
->kobjs
[i
] = kobj
;
510 for (i
= i
- 1; i
>= 0; i
--)
511 cpuidle_free_state_kobj(device
, i
);
516 * cpuidle_remove_state_sysfs - removes the cpuidle states sysfs attributes
517 * @device: the target device
519 static void cpuidle_remove_state_sysfs(struct cpuidle_device
*device
)
521 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(device
);
524 for (i
= 0; i
< drv
->state_count
; i
++)
525 cpuidle_free_state_kobj(device
, i
);
528 #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
529 #define kobj_to_driver_kobj(k) container_of(k, struct cpuidle_driver_kobj, kobj)
530 #define attr_to_driver_attr(a) container_of(a, struct cpuidle_driver_attr, attr)
532 #define define_one_driver_ro(_name, show) \
533 static struct cpuidle_driver_attr attr_driver_##_name = \
534 __ATTR(_name, 0444, show, NULL)
536 struct cpuidle_driver_kobj
{
537 struct cpuidle_driver
*drv
;
538 struct completion kobj_unregister
;
542 struct cpuidle_driver_attr
{
543 struct attribute attr
;
544 ssize_t (*show
)(struct cpuidle_driver
*, char *);
545 ssize_t (*store
)(struct cpuidle_driver
*, const char *, size_t);
548 static ssize_t
show_driver_name(struct cpuidle_driver
*drv
, char *buf
)
552 spin_lock(&cpuidle_driver_lock
);
553 ret
= sprintf(buf
, "%s\n", drv
? drv
->name
: "none");
554 spin_unlock(&cpuidle_driver_lock
);
559 static void cpuidle_driver_sysfs_release(struct kobject
*kobj
)
561 struct cpuidle_driver_kobj
*driver_kobj
= kobj_to_driver_kobj(kobj
);
562 complete(&driver_kobj
->kobj_unregister
);
565 static ssize_t
cpuidle_driver_show(struct kobject
*kobj
, struct attribute
*attr
,
569 struct cpuidle_driver_kobj
*driver_kobj
= kobj_to_driver_kobj(kobj
);
570 struct cpuidle_driver_attr
*dattr
= attr_to_driver_attr(attr
);
573 ret
= dattr
->show(driver_kobj
->drv
, buf
);
578 static ssize_t
cpuidle_driver_store(struct kobject
*kobj
, struct attribute
*attr
,
579 const char *buf
, size_t size
)
582 struct cpuidle_driver_kobj
*driver_kobj
= kobj_to_driver_kobj(kobj
);
583 struct cpuidle_driver_attr
*dattr
= attr_to_driver_attr(attr
);
586 ret
= dattr
->store(driver_kobj
->drv
, buf
, size
);
591 define_one_driver_ro(name
, show_driver_name
);
593 static const struct sysfs_ops cpuidle_driver_sysfs_ops
= {
594 .show
= cpuidle_driver_show
,
595 .store
= cpuidle_driver_store
,
598 static struct attribute
*cpuidle_driver_default_attrs
[] = {
599 &attr_driver_name
.attr
,
602 ATTRIBUTE_GROUPS(cpuidle_driver_default
);
604 static const struct kobj_type ktype_driver_cpuidle
= {
605 .sysfs_ops
= &cpuidle_driver_sysfs_ops
,
606 .default_groups
= cpuidle_driver_default_groups
,
607 .release
= cpuidle_driver_sysfs_release
,
611 * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute
612 * @dev: the target device
614 static int cpuidle_add_driver_sysfs(struct cpuidle_device
*dev
)
616 struct cpuidle_driver_kobj
*kdrv
;
617 struct cpuidle_device_kobj
*kdev
= dev
->kobj_dev
;
618 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
621 kdrv
= kzalloc(sizeof(*kdrv
), GFP_KERNEL
);
626 init_completion(&kdrv
->kobj_unregister
);
628 ret
= kobject_init_and_add(&kdrv
->kobj
, &ktype_driver_cpuidle
,
629 &kdev
->kobj
, "driver");
631 kobject_put(&kdrv
->kobj
);
636 kobject_uevent(&kdrv
->kobj
, KOBJ_ADD
);
637 dev
->kobj_driver
= kdrv
;
643 * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute
644 * @dev: the target device
646 static void cpuidle_remove_driver_sysfs(struct cpuidle_device
*dev
)
648 struct cpuidle_driver_kobj
*kdrv
= dev
->kobj_driver
;
649 kobject_put(&kdrv
->kobj
);
650 wait_for_completion(&kdrv
->kobj_unregister
);
654 static inline int cpuidle_add_driver_sysfs(struct cpuidle_device
*dev
)
659 static inline void cpuidle_remove_driver_sysfs(struct cpuidle_device
*dev
)
666 * cpuidle_add_device_sysfs - adds device specific sysfs attributes
667 * @device: the target device
669 int cpuidle_add_device_sysfs(struct cpuidle_device
*device
)
673 ret
= cpuidle_add_state_sysfs(device
);
677 ret
= cpuidle_add_driver_sysfs(device
);
679 cpuidle_remove_state_sysfs(device
);
684 * cpuidle_remove_device_sysfs : removes device specific sysfs attributes
685 * @device : the target device
687 void cpuidle_remove_device_sysfs(struct cpuidle_device
*device
)
689 cpuidle_remove_driver_sysfs(device
);
690 cpuidle_remove_state_sysfs(device
);
694 * cpuidle_add_sysfs - creates a sysfs instance for the target device
695 * @dev: the target device
697 int cpuidle_add_sysfs(struct cpuidle_device
*dev
)
699 struct cpuidle_device_kobj
*kdev
;
700 struct device
*cpu_dev
= get_cpu_device((unsigned long)dev
->cpu
);
704 * Return if cpu_device is not setup for this CPU.
706 * This could happen if the arch did not set up cpu_device
707 * since this CPU is not in cpu_present mask and the
708 * driver did not send a correct CPU mask during registration.
709 * Without this check we would end up passing bogus
710 * value for &cpu_dev->kobj in kobject_init_and_add()
715 kdev
= kzalloc(sizeof(*kdev
), GFP_KERNEL
);
720 init_completion(&kdev
->kobj_unregister
);
722 error
= kobject_init_and_add(&kdev
->kobj
, &ktype_cpuidle
, &cpu_dev
->kobj
,
725 kobject_put(&kdev
->kobj
);
730 dev
->kobj_dev
= kdev
;
731 kobject_uevent(&kdev
->kobj
, KOBJ_ADD
);
737 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
738 * @dev: the target device
740 void cpuidle_remove_sysfs(struct cpuidle_device
*dev
)
742 struct cpuidle_device_kobj
*kdev
= dev
->kobj_dev
;
744 kobject_put(&kdev
->kobj
);
745 wait_for_completion(&kdev
->kobj_unregister
);