xref: /openbmc/linux/drivers/cpuidle/sysfs.c (revision a36954f5)
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/completion.h>
15 #include <linux/capability.h>
16 #include <linux/device.h>
17 #include <linux/kobject.h>
18 
19 #include "cpuidle.h"
20 
21 static unsigned int sysfs_switch;
22 static int __init cpuidle_sysfs_setup(char *unused)
23 {
24 	sysfs_switch = 1;
25 	return 1;
26 }
27 __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
28 
29 static ssize_t show_available_governors(struct device *dev,
30 					struct device_attribute *attr,
31 					char *buf)
32 {
33 	ssize_t i = 0;
34 	struct cpuidle_governor *tmp;
35 
36 	mutex_lock(&cpuidle_lock);
37 	list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
38 		if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) -
39 				    CPUIDLE_NAME_LEN - 2))
40 			goto out;
41 		i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
42 	}
43 
44 out:
45 	i+= sprintf(&buf[i], "\n");
46 	mutex_unlock(&cpuidle_lock);
47 	return i;
48 }
49 
50 static ssize_t show_current_driver(struct device *dev,
51 				   struct device_attribute *attr,
52 				   char *buf)
53 {
54 	ssize_t ret;
55 	struct cpuidle_driver *drv;
56 
57 	spin_lock(&cpuidle_driver_lock);
58 	drv = cpuidle_get_driver();
59 	if (drv)
60 		ret = sprintf(buf, "%s\n", drv->name);
61 	else
62 		ret = sprintf(buf, "none\n");
63 	spin_unlock(&cpuidle_driver_lock);
64 
65 	return ret;
66 }
67 
68 static ssize_t show_current_governor(struct device *dev,
69 				     struct device_attribute *attr,
70 				     char *buf)
71 {
72 	ssize_t ret;
73 
74 	mutex_lock(&cpuidle_lock);
75 	if (cpuidle_curr_governor)
76 		ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
77 	else
78 		ret = sprintf(buf, "none\n");
79 	mutex_unlock(&cpuidle_lock);
80 
81 	return ret;
82 }
83 
84 static ssize_t store_current_governor(struct device *dev,
85 				      struct device_attribute *attr,
86 				      const char *buf, size_t count)
87 {
88 	char gov_name[CPUIDLE_NAME_LEN];
89 	int ret = -EINVAL;
90 	size_t len = count;
91 	struct cpuidle_governor *gov;
92 
93 	if (!len || len >= sizeof(gov_name))
94 		return -EINVAL;
95 
96 	memcpy(gov_name, buf, len);
97 	gov_name[len] = '\0';
98 	if (gov_name[len - 1] == '\n')
99 		gov_name[--len] = '\0';
100 
101 	mutex_lock(&cpuidle_lock);
102 
103 	list_for_each_entry(gov, &cpuidle_governors, governor_list) {
104 		if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
105 			ret = cpuidle_switch_governor(gov);
106 			break;
107 		}
108 	}
109 
110 	mutex_unlock(&cpuidle_lock);
111 
112 	if (ret)
113 		return ret;
114 	else
115 		return count;
116 }
117 
118 static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);
119 static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
120 
121 static struct attribute *cpuidle_default_attrs[] = {
122 	&dev_attr_current_driver.attr,
123 	&dev_attr_current_governor_ro.attr,
124 	NULL
125 };
126 
127 static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL);
128 static DEVICE_ATTR(current_governor, 0644, show_current_governor,
129 		   store_current_governor);
130 
131 static struct attribute *cpuidle_switch_attrs[] = {
132 	&dev_attr_available_governors.attr,
133 	&dev_attr_current_driver.attr,
134 	&dev_attr_current_governor.attr,
135 	NULL
136 };
137 
138 static struct attribute_group cpuidle_attr_group = {
139 	.attrs = cpuidle_default_attrs,
140 	.name = "cpuidle",
141 };
142 
143 /**
144  * cpuidle_add_interface - add CPU global sysfs attributes
145  */
146 int cpuidle_add_interface(struct device *dev)
147 {
148 	if (sysfs_switch)
149 		cpuidle_attr_group.attrs = cpuidle_switch_attrs;
150 
151 	return sysfs_create_group(&dev->kobj, &cpuidle_attr_group);
152 }
153 
154 /**
155  * cpuidle_remove_interface - remove CPU global sysfs attributes
156  */
157 void cpuidle_remove_interface(struct device *dev)
158 {
159 	sysfs_remove_group(&dev->kobj, &cpuidle_attr_group);
160 }
161 
162 struct cpuidle_attr {
163 	struct attribute attr;
164 	ssize_t (*show)(struct cpuidle_device *, char *);
165 	ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
166 };
167 
168 #define define_one_ro(_name, show) \
169 	static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
170 #define define_one_rw(_name, show, store) \
171 	static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
172 
173 #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
174 
175 struct cpuidle_device_kobj {
176 	struct cpuidle_device *dev;
177 	struct completion kobj_unregister;
178 	struct kobject kobj;
179 };
180 
181 static inline struct cpuidle_device *to_cpuidle_device(struct kobject *kobj)
182 {
183 	struct cpuidle_device_kobj *kdev =
184 		container_of(kobj, struct cpuidle_device_kobj, kobj);
185 
186 	return kdev->dev;
187 }
188 
189 static ssize_t cpuidle_show(struct kobject *kobj, struct attribute *attr,
190 			    char *buf)
191 {
192 	int ret = -EIO;
193 	struct cpuidle_device *dev = to_cpuidle_device(kobj);
194 	struct cpuidle_attr *cattr = attr_to_cpuidleattr(attr);
195 
196 	if (cattr->show) {
197 		mutex_lock(&cpuidle_lock);
198 		ret = cattr->show(dev, buf);
199 		mutex_unlock(&cpuidle_lock);
200 	}
201 	return ret;
202 }
203 
204 static ssize_t cpuidle_store(struct kobject *kobj, struct attribute *attr,
205 			     const char *buf, size_t count)
206 {
207 	int ret = -EIO;
208 	struct cpuidle_device *dev = to_cpuidle_device(kobj);
209 	struct cpuidle_attr *cattr = attr_to_cpuidleattr(attr);
210 
211 	if (cattr->store) {
212 		mutex_lock(&cpuidle_lock);
213 		ret = cattr->store(dev, buf, count);
214 		mutex_unlock(&cpuidle_lock);
215 	}
216 	return ret;
217 }
218 
219 static const struct sysfs_ops cpuidle_sysfs_ops = {
220 	.show = cpuidle_show,
221 	.store = cpuidle_store,
222 };
223 
224 static void cpuidle_sysfs_release(struct kobject *kobj)
225 {
226 	struct cpuidle_device_kobj *kdev =
227 		container_of(kobj, struct cpuidle_device_kobj, kobj);
228 
229 	complete(&kdev->kobj_unregister);
230 }
231 
232 static struct kobj_type ktype_cpuidle = {
233 	.sysfs_ops = &cpuidle_sysfs_ops,
234 	.release = cpuidle_sysfs_release,
235 };
236 
237 struct cpuidle_state_attr {
238 	struct attribute attr;
239 	ssize_t (*show)(struct cpuidle_state *, \
240 					struct cpuidle_state_usage *, char *);
241 	ssize_t (*store)(struct cpuidle_state *, \
242 			struct cpuidle_state_usage *, const char *, size_t);
243 };
244 
245 #define define_one_state_ro(_name, show) \
246 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
247 
248 #define define_one_state_rw(_name, show, store) \
249 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
250 
251 #define define_show_state_function(_name) \
252 static ssize_t show_state_##_name(struct cpuidle_state *state, \
253 			 struct cpuidle_state_usage *state_usage, char *buf) \
254 { \
255 	return sprintf(buf, "%u\n", state->_name);\
256 }
257 
258 #define define_store_state_ull_function(_name) \
259 static ssize_t store_state_##_name(struct cpuidle_state *state, \
260 				   struct cpuidle_state_usage *state_usage, \
261 				   const char *buf, size_t size)	\
262 { \
263 	unsigned long long value; \
264 	int err; \
265 	if (!capable(CAP_SYS_ADMIN)) \
266 		return -EPERM; \
267 	err = kstrtoull(buf, 0, &value); \
268 	if (err) \
269 		return err; \
270 	if (value) \
271 		state_usage->_name = 1; \
272 	else \
273 		state_usage->_name = 0; \
274 	return size; \
275 }
276 
277 #define define_show_state_ull_function(_name) \
278 static ssize_t show_state_##_name(struct cpuidle_state *state, \
279 				  struct cpuidle_state_usage *state_usage, \
280 				  char *buf)				\
281 { \
282 	return sprintf(buf, "%llu\n", state_usage->_name);\
283 }
284 
285 #define define_show_state_str_function(_name) \
286 static ssize_t show_state_##_name(struct cpuidle_state *state, \
287 				  struct cpuidle_state_usage *state_usage, \
288 				  char *buf)				\
289 { \
290 	if (state->_name[0] == '\0')\
291 		return sprintf(buf, "<null>\n");\
292 	return sprintf(buf, "%s\n", state->_name);\
293 }
294 
295 define_show_state_function(exit_latency)
296 define_show_state_function(target_residency)
297 define_show_state_function(power_usage)
298 define_show_state_ull_function(usage)
299 define_show_state_ull_function(time)
300 define_show_state_str_function(name)
301 define_show_state_str_function(desc)
302 define_show_state_ull_function(disable)
303 define_store_state_ull_function(disable)
304 
305 define_one_state_ro(name, show_state_name);
306 define_one_state_ro(desc, show_state_desc);
307 define_one_state_ro(latency, show_state_exit_latency);
308 define_one_state_ro(residency, show_state_target_residency);
309 define_one_state_ro(power, show_state_power_usage);
310 define_one_state_ro(usage, show_state_usage);
311 define_one_state_ro(time, show_state_time);
312 define_one_state_rw(disable, show_state_disable, store_state_disable);
313 
314 static struct attribute *cpuidle_state_default_attrs[] = {
315 	&attr_name.attr,
316 	&attr_desc.attr,
317 	&attr_latency.attr,
318 	&attr_residency.attr,
319 	&attr_power.attr,
320 	&attr_usage.attr,
321 	&attr_time.attr,
322 	&attr_disable.attr,
323 	NULL
324 };
325 
326 struct cpuidle_state_kobj {
327 	struct cpuidle_state *state;
328 	struct cpuidle_state_usage *state_usage;
329 	struct completion kobj_unregister;
330 	struct kobject kobj;
331 };
332 
333 #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
334 #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
335 #define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
336 #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
337 
338 static ssize_t cpuidle_state_show(struct kobject *kobj, struct attribute *attr,
339 				  char * buf)
340 {
341 	int ret = -EIO;
342 	struct cpuidle_state *state = kobj_to_state(kobj);
343 	struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
344 	struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
345 
346 	if (cattr->show)
347 		ret = cattr->show(state, state_usage, buf);
348 
349 	return ret;
350 }
351 
352 static ssize_t cpuidle_state_store(struct kobject *kobj, struct attribute *attr,
353 				   const char *buf, size_t size)
354 {
355 	int ret = -EIO;
356 	struct cpuidle_state *state = kobj_to_state(kobj);
357 	struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
358 	struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
359 
360 	if (cattr->store)
361 		ret = cattr->store(state, state_usage, buf, size);
362 
363 	return ret;
364 }
365 
366 static const struct sysfs_ops cpuidle_state_sysfs_ops = {
367 	.show = cpuidle_state_show,
368 	.store = cpuidle_state_store,
369 };
370 
371 static void cpuidle_state_sysfs_release(struct kobject *kobj)
372 {
373 	struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
374 
375 	complete(&state_obj->kobj_unregister);
376 }
377 
378 static struct kobj_type ktype_state_cpuidle = {
379 	.sysfs_ops = &cpuidle_state_sysfs_ops,
380 	.default_attrs = cpuidle_state_default_attrs,
381 	.release = cpuidle_state_sysfs_release,
382 };
383 
384 static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
385 {
386 	kobject_put(&device->kobjs[i]->kobj);
387 	wait_for_completion(&device->kobjs[i]->kobj_unregister);
388 	kfree(device->kobjs[i]);
389 	device->kobjs[i] = NULL;
390 }
391 
392 /**
393  * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes
394  * @device: the target device
395  */
396 static int cpuidle_add_state_sysfs(struct cpuidle_device *device)
397 {
398 	int i, ret = -ENOMEM;
399 	struct cpuidle_state_kobj *kobj;
400 	struct cpuidle_device_kobj *kdev = device->kobj_dev;
401 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
402 
403 	/* state statistics */
404 	for (i = 0; i < drv->state_count; i++) {
405 		kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
406 		if (!kobj) {
407 			ret = -ENOMEM;
408 			goto error_state;
409 		}
410 		kobj->state = &drv->states[i];
411 		kobj->state_usage = &device->states_usage[i];
412 		init_completion(&kobj->kobj_unregister);
413 
414 		ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle,
415 					   &kdev->kobj, "state%d", i);
416 		if (ret) {
417 			kfree(kobj);
418 			goto error_state;
419 		}
420 		kobject_uevent(&kobj->kobj, KOBJ_ADD);
421 		device->kobjs[i] = kobj;
422 	}
423 
424 	return 0;
425 
426 error_state:
427 	for (i = i - 1; i >= 0; i--)
428 		cpuidle_free_state_kobj(device, i);
429 	return ret;
430 }
431 
432 /**
433  * cpuidle_remove_driver_sysfs - removes the cpuidle states sysfs attributes
434  * @device: the target device
435  */
436 static void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
437 {
438 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
439 	int i;
440 
441 	for (i = 0; i < drv->state_count; i++)
442 		cpuidle_free_state_kobj(device, i);
443 }
444 
445 #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
446 #define kobj_to_driver_kobj(k) container_of(k, struct cpuidle_driver_kobj, kobj)
447 #define attr_to_driver_attr(a) container_of(a, struct cpuidle_driver_attr, attr)
448 
449 #define define_one_driver_ro(_name, show)                       \
450 	static struct cpuidle_driver_attr attr_driver_##_name = \
451 		__ATTR(_name, 0444, show, NULL)
452 
453 struct cpuidle_driver_kobj {
454 	struct cpuidle_driver *drv;
455 	struct completion kobj_unregister;
456 	struct kobject kobj;
457 };
458 
459 struct cpuidle_driver_attr {
460 	struct attribute attr;
461 	ssize_t (*show)(struct cpuidle_driver *, char *);
462 	ssize_t (*store)(struct cpuidle_driver *, const char *, size_t);
463 };
464 
465 static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
466 {
467 	ssize_t ret;
468 
469 	spin_lock(&cpuidle_driver_lock);
470 	ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
471 	spin_unlock(&cpuidle_driver_lock);
472 
473 	return ret;
474 }
475 
476 static void cpuidle_driver_sysfs_release(struct kobject *kobj)
477 {
478 	struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
479 	complete(&driver_kobj->kobj_unregister);
480 }
481 
482 static ssize_t cpuidle_driver_show(struct kobject *kobj, struct attribute *attr,
483 				   char *buf)
484 {
485 	int ret = -EIO;
486 	struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
487 	struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
488 
489 	if (dattr->show)
490 		ret = dattr->show(driver_kobj->drv, buf);
491 
492 	return ret;
493 }
494 
495 static ssize_t cpuidle_driver_store(struct kobject *kobj, struct attribute *attr,
496 				    const char *buf, size_t size)
497 {
498 	int ret = -EIO;
499 	struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
500 	struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
501 
502 	if (dattr->store)
503 		ret = dattr->store(driver_kobj->drv, buf, size);
504 
505 	return ret;
506 }
507 
508 define_one_driver_ro(name, show_driver_name);
509 
510 static const struct sysfs_ops cpuidle_driver_sysfs_ops = {
511 	.show = cpuidle_driver_show,
512 	.store = cpuidle_driver_store,
513 };
514 
515 static struct attribute *cpuidle_driver_default_attrs[] = {
516 	&attr_driver_name.attr,
517 	NULL
518 };
519 
520 static struct kobj_type ktype_driver_cpuidle = {
521 	.sysfs_ops = &cpuidle_driver_sysfs_ops,
522 	.default_attrs = cpuidle_driver_default_attrs,
523 	.release = cpuidle_driver_sysfs_release,
524 };
525 
526 /**
527  * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute
528  * @device: the target device
529  */
530 static int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
531 {
532 	struct cpuidle_driver_kobj *kdrv;
533 	struct cpuidle_device_kobj *kdev = dev->kobj_dev;
534 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
535 	int ret;
536 
537 	kdrv = kzalloc(sizeof(*kdrv), GFP_KERNEL);
538 	if (!kdrv)
539 		return -ENOMEM;
540 
541 	kdrv->drv = drv;
542 	init_completion(&kdrv->kobj_unregister);
543 
544 	ret = kobject_init_and_add(&kdrv->kobj, &ktype_driver_cpuidle,
545 				   &kdev->kobj, "driver");
546 	if (ret) {
547 		kfree(kdrv);
548 		return ret;
549 	}
550 
551 	kobject_uevent(&kdrv->kobj, KOBJ_ADD);
552 	dev->kobj_driver = kdrv;
553 
554 	return ret;
555 }
556 
557 /**
558  * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute
559  * @device: the target device
560  */
561 static void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
562 {
563 	struct cpuidle_driver_kobj *kdrv = dev->kobj_driver;
564 	kobject_put(&kdrv->kobj);
565 	wait_for_completion(&kdrv->kobj_unregister);
566 	kfree(kdrv);
567 }
568 #else
569 static inline int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
570 {
571 	return 0;
572 }
573 
574 static inline void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
575 {
576 	;
577 }
578 #endif
579 
580 /**
581  * cpuidle_add_device_sysfs - adds device specific sysfs attributes
582  * @device: the target device
583  */
584 int cpuidle_add_device_sysfs(struct cpuidle_device *device)
585 {
586 	int ret;
587 
588 	ret = cpuidle_add_state_sysfs(device);
589 	if (ret)
590 		return ret;
591 
592 	ret = cpuidle_add_driver_sysfs(device);
593 	if (ret)
594 		cpuidle_remove_state_sysfs(device);
595 	return ret;
596 }
597 
598 /**
599  * cpuidle_remove_device_sysfs : removes device specific sysfs attributes
600  * @device : the target device
601  */
602 void cpuidle_remove_device_sysfs(struct cpuidle_device *device)
603 {
604 	cpuidle_remove_driver_sysfs(device);
605 	cpuidle_remove_state_sysfs(device);
606 }
607 
608 /**
609  * cpuidle_add_sysfs - creates a sysfs instance for the target device
610  * @dev: the target device
611  */
612 int cpuidle_add_sysfs(struct cpuidle_device *dev)
613 {
614 	struct cpuidle_device_kobj *kdev;
615 	struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
616 	int error;
617 
618 	/*
619 	 * Return if cpu_device is not setup for this CPU.
620 	 *
621 	 * This could happen if the arch did not set up cpu_device
622 	 * since this CPU is not in cpu_present mask and the
623 	 * driver did not send a correct CPU mask during registration.
624 	 * Without this check we would end up passing bogus
625 	 * value for &cpu_dev->kobj in kobject_init_and_add()
626 	 */
627 	if (!cpu_dev)
628 		return -ENODEV;
629 
630 	kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
631 	if (!kdev)
632 		return -ENOMEM;
633 	kdev->dev = dev;
634 	dev->kobj_dev = kdev;
635 
636 	init_completion(&kdev->kobj_unregister);
637 
638 	error = kobject_init_and_add(&kdev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
639 				   "cpuidle");
640 	if (error) {
641 		kfree(kdev);
642 		return error;
643 	}
644 
645 	kobject_uevent(&kdev->kobj, KOBJ_ADD);
646 
647 	return 0;
648 }
649 
650 /**
651  * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
652  * @dev: the target device
653  */
654 void cpuidle_remove_sysfs(struct cpuidle_device *dev)
655 {
656 	struct cpuidle_device_kobj *kdev = dev->kobj_dev;
657 
658 	kobject_put(&kdev->kobj);
659 	wait_for_completion(&kdev->kobj_unregister);
660 	kfree(kdev);
661 }
662