sysfs.c 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * sysfs.c - sysfs support
 *
 * (C) 2006-2007 Shaohua Li <shaohua.li@intel.com>
 *
 * This code is licenced under the GPL.
 */

#include <linux/kernel.h>
#include <linux/cpuidle.h>
#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 20 21 22 23 24 25 26 27 28

#include "cpuidle.h"

static unsigned int sysfs_switch;
static int __init cpuidle_sysfs_setup(char *unused)
{
	sysfs_switch = 1;
	return 1;
}
__setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);

29 30
static ssize_t show_available_governors(struct device *dev,
					struct device_attribute *attr,
31
					char *buf)
32 33 34 35 36 37
{
	ssize_t i = 0;
	struct cpuidle_governor *tmp;

	mutex_lock(&cpuidle_lock);
	list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
38 39
		if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) -
				    CPUIDLE_NAME_LEN - 2))
40 41 42 43 44 45 46 47 48 49
			goto out;
		i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
	}

out:
	i+= sprintf(&buf[i], "\n");
	mutex_unlock(&cpuidle_lock);
	return i;
}

50 51
static ssize_t show_current_driver(struct device *dev,
				   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 59 60
	drv = cpuidle_get_driver();
	if (drv)
		ret = sprintf(buf, "%s\n", drv->name);
61 62 63 64 65 66 67
	else
		ret = sprintf(buf, "none\n");
	spin_unlock(&cpuidle_driver_lock);

	return ret;
}

68 69
static ssize_t show_current_governor(struct device *dev,
				     struct device_attribute *attr,
70
				     char *buf)
71 72 73 74 75 76 77 78 79 80 81 82 83
{
	ssize_t ret;

	mutex_lock(&cpuidle_lock);
	if (cpuidle_curr_governor)
		ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
	else
		ret = sprintf(buf, "none\n");
	mutex_unlock(&cpuidle_lock);

	return ret;
}

84 85
static ssize_t store_current_governor(struct device *dev,
				      struct device_attribute *attr,
86
				      const char *buf, size_t count)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
{
	char gov_name[CPUIDLE_NAME_LEN];
	int ret = -EINVAL;
	size_t len = count;
	struct cpuidle_governor *gov;

	if (!len || len >= sizeof(gov_name))
		return -EINVAL;

	memcpy(gov_name, buf, len);
	gov_name[len] = '\0';
	if (gov_name[len - 1] == '\n')
		gov_name[--len] = '\0';

	mutex_lock(&cpuidle_lock);

	list_for_each_entry(gov, &cpuidle_governors, governor_list) {
		if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
			ret = cpuidle_switch_governor(gov);
			break;
		}
	}

	mutex_unlock(&cpuidle_lock);

	if (ret)
		return ret;
	else
		return count;
}

118 119
static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);
static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
120

121 122 123
static struct attribute *cpuidle_default_attrs[] = {
	&dev_attr_current_driver.attr,
	&dev_attr_current_governor_ro.attr,
124 125 126
	NULL
};

127 128 129
static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL);
static DEVICE_ATTR(current_governor, 0644, show_current_governor,
		   store_current_governor);
130

131 132 133 134
static struct attribute *cpuidle_switch_attrs[] = {
	&dev_attr_available_governors.attr,
	&dev_attr_current_driver.attr,
	&dev_attr_current_governor.attr,
135 136 137
	NULL
};

138 139
static struct attribute_group cpuidle_attr_group = {
	.attrs = cpuidle_default_attrs,
140 141 142 143
	.name = "cpuidle",
};

/**
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 163 164 165 166 167 168 169 170 171 172 173
}

struct cpuidle_attr {
	struct attribute attr;
	ssize_t (*show)(struct cpuidle_device *, char *);
	ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
};

#define define_one_ro(_name, show) \
	static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
#define define_one_rw(_name, show, store) \
	static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)

#define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188
struct cpuidle_device_kobj {
	struct cpuidle_device *dev;
	struct completion kobj_unregister;
	struct kobject kobj;
};

static inline struct cpuidle_device *to_cpuidle_device(struct kobject *kobj)
{
	struct cpuidle_device_kobj *kdev =
		container_of(kobj, struct cpuidle_device_kobj, kobj);

	return kdev->dev;
}

189 190
static ssize_t cpuidle_show(struct kobject *kobj, struct attribute *attr,
			    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 197 198 199 200 201 202 203

	if (cattr->show) {
		mutex_lock(&cpuidle_lock);
		ret = cattr->show(dev, buf);
		mutex_unlock(&cpuidle_lock);
	}
	return ret;
}

204 205
static ssize_t cpuidle_store(struct kobject *kobj, struct attribute *attr,
			     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 212 213 214 215 216 217 218

	if (cattr->store) {
		mutex_lock(&cpuidle_lock);
		ret = cattr->store(dev, buf, count);
		mutex_unlock(&cpuidle_lock);
	}
	return ret;
}

219
static const struct sysfs_ops cpuidle_sysfs_ops = {
220 221 222 223 224 225
	.show = cpuidle_show,
	.store = cpuidle_store,
};

static void cpuidle_sysfs_release(struct kobject *kobj)
{
226 227
	struct cpuidle_device_kobj *kdev =
		container_of(kobj, struct cpuidle_device_kobj, kobj);
228

229
	complete(&kdev->kobj_unregister);
230 231 232 233 234 235 236 237 238
}

static struct kobj_type ktype_cpuidle = {
	.sysfs_ops = &cpuidle_sysfs_ops,
	.release = cpuidle_sysfs_release,
};

struct cpuidle_state_attr {
	struct attribute attr;
239 240
	ssize_t (*show)(struct cpuidle_state *, \
					struct cpuidle_state_usage *, char *);
241 242
	ssize_t (*store)(struct cpuidle_state *, \
			struct cpuidle_state_usage *, const char *, size_t);
243 244 245 246 247
};

#define define_one_state_ro(_name, show) \
static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)

248 249 250
#define define_one_state_rw(_name, show, store) \
static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)

251
#define define_show_state_function(_name) \
252 253
static ssize_t show_state_##_name(struct cpuidle_state *state, \
			 struct cpuidle_state_usage *state_usage, char *buf) \
254 255 256 257
{ \
	return sprintf(buf, "%u\n", state->_name);\
}

258
#define define_store_state_ull_function(_name) \
259
static ssize_t store_state_##_name(struct cpuidle_state *state, \
260 261
				   struct cpuidle_state_usage *state_usage, \
				   const char *buf, size_t size)	\
262
{ \
263
	unsigned long long value; \
264 265 266
	int err; \
	if (!capable(CAP_SYS_ADMIN)) \
		return -EPERM; \
267
	err = kstrtoull(buf, 0, &value); \
268 269 270
	if (err) \
		return err; \
	if (value) \
271
		state_usage->_name = 1; \
272
	else \
273
		state_usage->_name = 0; \
274 275 276
	return size; \
}

277
#define define_show_state_ull_function(_name) \
278
static ssize_t show_state_##_name(struct cpuidle_state *state, \
279 280
				  struct cpuidle_state_usage *state_usage, \
				  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 288
				  struct cpuidle_state_usage *state_usage, \
				  char *buf)				\
289 290 291 292
{ \
	if (state->_name[0] == '\0')\
		return sprintf(buf, "<null>\n");\
	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 299
define_show_state_ull_function(usage)
define_show_state_ull_function(time)
300 301
define_show_state_str_function(name)
define_show_state_str_function(desc)
302 303
define_show_state_ull_function(disable)
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 310 311
define_one_state_ro(power, show_state_power_usage);
define_one_state_ro(usage, show_state_usage);
define_one_state_ro(time, show_state_time);
312
define_one_state_rw(disable, show_state_disable, store_state_disable);
313 314 315

static struct attribute *cpuidle_state_default_attrs[] = {
	&attr_name.attr,
316
	&attr_desc.attr,
317
	&attr_latency.attr,
318
	&attr_residency.attr,
319 320 321
	&attr_power.attr,
	&attr_usage.attr,
	&attr_time.attr,
322
	&attr_disable.attr,
323 324 325
	NULL
};

326 327 328 329 330
struct cpuidle_state_kobj {
	struct cpuidle_state *state;
	struct cpuidle_state_usage *state_usage;
	struct completion kobj_unregister;
	struct kobject kobj;
331
	struct cpuidle_device *device;
332 333
};

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
#ifdef CONFIG_SUSPEND
#define define_show_state_s2idle_ull_function(_name) \
static ssize_t show_state_s2idle_##_name(struct cpuidle_state *state, \
					 struct cpuidle_state_usage *state_usage, \
					 char *buf)				\
{ \
	return sprintf(buf, "%llu\n", state_usage->s2idle_##_name);\
}

define_show_state_s2idle_ull_function(usage);
define_show_state_s2idle_ull_function(time);

#define define_one_state_s2idle_ro(_name, show) \
static struct cpuidle_state_attr attr_s2idle_##_name = \
	__ATTR(_name, 0444, show, NULL)

define_one_state_s2idle_ro(usage, show_state_s2idle_usage);
define_one_state_s2idle_ro(time, show_state_s2idle_time);

static struct attribute *cpuidle_state_s2idle_attrs[] = {
	&attr_s2idle_usage.attr,
	&attr_s2idle_time.attr,
	NULL
};

static const struct attribute_group cpuidle_state_s2idle_group = {
	.name	= "s2idle",
	.attrs	= cpuidle_state_s2idle_attrs,
};

static void cpuidle_add_s2idle_attr_group(struct cpuidle_state_kobj *kobj)
{
	int ret;

	if (!kobj->state->enter_s2idle)
		return;

	ret = sysfs_create_group(&kobj->kobj, &cpuidle_state_s2idle_group);
	if (ret)
		pr_debug("%s: sysfs attribute group not created\n", __func__);
}

static void cpuidle_remove_s2idle_attr_group(struct cpuidle_state_kobj *kobj)
{
	if (kobj->state->enter_s2idle)
		sysfs_remove_group(&kobj->kobj, &cpuidle_state_s2idle_group);
}
#else
static inline void cpuidle_add_s2idle_attr_group(struct cpuidle_state_kobj *kobj) { }
static inline void cpuidle_remove_s2idle_attr_group(struct cpuidle_state_kobj *kobj) { }
#endif /* CONFIG_SUSPEND */

386 387
#define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
#define kobj_to_state(k) (kobj_to_state_obj(k)->state)
388
#define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
389
#define kobj_to_device(k) (kobj_to_state_obj(k)->device)
390
#define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
391 392 393

static ssize_t cpuidle_state_show(struct kobject *kobj, struct attribute *attr,
				  char * buf)
394 395 396
{
	int ret = -EIO;
	struct cpuidle_state *state = kobj_to_state(kobj);
397
	struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
398 399 400
	struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);

	if (cattr->show)
401
		ret = cattr->show(state, state_usage, buf);
402 403 404 405

	return ret;
}

406 407
static ssize_t cpuidle_state_store(struct kobject *kobj, struct attribute *attr,
				   const char *buf, size_t size)
408 409 410
{
	int ret = -EIO;
	struct cpuidle_state *state = kobj_to_state(kobj);
411
	struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
412
	struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
413
	struct cpuidle_device *dev = kobj_to_device(kobj);
414 415

	if (cattr->store)
416
		ret = cattr->store(state, state_usage, buf, size);
417

418 419 420
	/* reset poll time cache */
	dev->poll_limit_ns = 0;

421 422 423
	return ret;
}

424
static const struct sysfs_ops cpuidle_state_sysfs_ops = {
425
	.show = cpuidle_state_show,
426
	.store = cpuidle_state_store,
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
};

static void cpuidle_state_sysfs_release(struct kobject *kobj)
{
	struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);

	complete(&state_obj->kobj_unregister);
}

static struct kobj_type ktype_state_cpuidle = {
	.sysfs_ops = &cpuidle_state_sysfs_ops,
	.default_attrs = cpuidle_state_default_attrs,
	.release = cpuidle_state_sysfs_release,
};

442
static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
443
{
444
	cpuidle_remove_s2idle_attr_group(device->kobjs[i]);
445
	kobject_put(&device->kobjs[i]->kobj);
446 447 448 449 450 451
	wait_for_completion(&device->kobjs[i]->kobj_unregister);
	kfree(device->kobjs[i]);
	device->kobjs[i] = NULL;
}

/**
D
Daniel Lezcano 已提交
452
 * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes
453 454
 * @device: the target device
 */
D
Daniel Lezcano 已提交
455
static int cpuidle_add_state_sysfs(struct cpuidle_device *device)
456 457 458
{
	int i, ret = -ENOMEM;
	struct cpuidle_state_kobj *kobj;
459
	struct cpuidle_device_kobj *kdev = device->kobj_dev;
D
Daniel Lezcano 已提交
460
	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
461 462

	/* state statistics */
463
	for (i = 0; i < drv->state_count; i++) {
464
		kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
465 466
		if (!kobj) {
			ret = -ENOMEM;
467
			goto error_state;
468
		}
469
		kobj->state = &drv->states[i];
470
		kobj->state_usage = &device->states_usage[i];
471
		kobj->device = device;
472 473
		init_completion(&kobj->kobj_unregister);

474
		ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle,
475
					   &kdev->kobj, "state%d", i);
476 477 478 479
		if (ret) {
			kfree(kobj);
			goto error_state;
		}
480
		cpuidle_add_s2idle_attr_group(kobj);
481
		kobject_uevent(&kobj->kobj, KOBJ_ADD);
482 483 484 485 486 487 488 489 490 491 492 493
		device->kobjs[i] = kobj;
	}

	return 0;

error_state:
	for (i = i - 1; i >= 0; i--)
		cpuidle_free_state_kobj(device, i);
	return ret;
}

/**
D
Daniel Lezcano 已提交
494
 * cpuidle_remove_driver_sysfs - removes the cpuidle states sysfs attributes
495 496
 * @device: the target device
 */
D
Daniel Lezcano 已提交
497
static void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
498
{
499
	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
500 501
	int i;

502
	for (i = 0; i < drv->state_count; i++)
503 504 505
		cpuidle_free_state_kobj(device, i);
}

D
Daniel Lezcano 已提交
506 507 508 509 510 511
#ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
#define kobj_to_driver_kobj(k) container_of(k, struct cpuidle_driver_kobj, kobj)
#define attr_to_driver_attr(a) container_of(a, struct cpuidle_driver_attr, attr)

#define define_one_driver_ro(_name, show)                       \
	static struct cpuidle_driver_attr attr_driver_##_name = \
512
		__ATTR(_name, 0444, show, NULL)
D
Daniel Lezcano 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

struct cpuidle_driver_kobj {
	struct cpuidle_driver *drv;
	struct completion kobj_unregister;
	struct kobject kobj;
};

struct cpuidle_driver_attr {
	struct attribute attr;
	ssize_t (*show)(struct cpuidle_driver *, char *);
	ssize_t (*store)(struct cpuidle_driver *, const char *, size_t);
};

static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
{
	ssize_t ret;

	spin_lock(&cpuidle_driver_lock);
	ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
	spin_unlock(&cpuidle_driver_lock);

	return ret;
}

static void cpuidle_driver_sysfs_release(struct kobject *kobj)
{
	struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
	complete(&driver_kobj->kobj_unregister);
}

543 544
static ssize_t cpuidle_driver_show(struct kobject *kobj, struct attribute *attr,
				   char *buf)
D
Daniel Lezcano 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
{
	int ret = -EIO;
	struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
	struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);

	if (dattr->show)
		ret = dattr->show(driver_kobj->drv, buf);

	return ret;
}

static ssize_t cpuidle_driver_store(struct kobject *kobj, struct attribute *attr,
				    const char *buf, size_t size)
{
	int ret = -EIO;
	struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
	struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);

	if (dattr->store)
		ret = dattr->store(driver_kobj->drv, buf, size);

	return ret;
}

define_one_driver_ro(name, show_driver_name);

static const struct sysfs_ops cpuidle_driver_sysfs_ops = {
	.show = cpuidle_driver_show,
	.store = cpuidle_driver_store,
};

static struct attribute *cpuidle_driver_default_attrs[] = {
	&attr_driver_name.attr,
	NULL
};

static struct kobj_type ktype_driver_cpuidle = {
	.sysfs_ops = &cpuidle_driver_sysfs_ops,
	.default_attrs = cpuidle_driver_default_attrs,
	.release = cpuidle_driver_sysfs_release,
};

/**
 * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute
 * @device: the target device
 */
static int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
{
	struct cpuidle_driver_kobj *kdrv;
594
	struct cpuidle_device_kobj *kdev = dev->kobj_dev;
D
Daniel Lezcano 已提交
595 596 597 598 599 600 601 602 603 604 605
	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
	int ret;

	kdrv = kzalloc(sizeof(*kdrv), GFP_KERNEL);
	if (!kdrv)
		return -ENOMEM;

	kdrv->drv = drv;
	init_completion(&kdrv->kobj_unregister);

	ret = kobject_init_and_add(&kdrv->kobj, &ktype_driver_cpuidle,
606
				   &kdev->kobj, "driver");
D
Daniel Lezcano 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	if (ret) {
		kfree(kdrv);
		return ret;
	}

	kobject_uevent(&kdrv->kobj, KOBJ_ADD);
	dev->kobj_driver = kdrv;

	return ret;
}

/**
 * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute
 * @device: the target device
 */
static void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
{
	struct cpuidle_driver_kobj *kdrv = dev->kobj_driver;
	kobject_put(&kdrv->kobj);
	wait_for_completion(&kdrv->kobj_unregister);
	kfree(kdrv);
}
#else
static inline int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
{
	return 0;
}

static inline void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
{
	;
}
#endif

/**
 * cpuidle_add_device_sysfs - adds device specific sysfs attributes
 * @device: the target device
 */
int cpuidle_add_device_sysfs(struct cpuidle_device *device)
{
	int ret;

	ret = cpuidle_add_state_sysfs(device);
	if (ret)
		return ret;

	ret = cpuidle_add_driver_sysfs(device);
	if (ret)
		cpuidle_remove_state_sysfs(device);
	return ret;
}

/**
 * cpuidle_remove_device_sysfs : removes device specific sysfs attributes
 * @device : the target device
 */
void cpuidle_remove_device_sysfs(struct cpuidle_device *device)
{
	cpuidle_remove_driver_sysfs(device);
	cpuidle_remove_state_sysfs(device);
}

669 670
/**
 * cpuidle_add_sysfs - creates a sysfs instance for the target device
671
 * @dev: the target device
672
 */
673
int cpuidle_add_sysfs(struct cpuidle_device *dev)
674
{
675
	struct cpuidle_device_kobj *kdev;
676
	struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
677
	int error;
678

679 680 681 682 683 684 685 686 687 688 689 690
	/*
	 * Return if cpu_device is not setup for this CPU.
	 *
	 * This could happen if the arch did not set up cpu_device
	 * since this CPU is not in cpu_present mask and the
	 * driver did not send a correct CPU mask during registration.
	 * Without this check we would end up passing bogus
	 * value for &cpu_dev->kobj in kobject_init_and_add()
	 */
	if (!cpu_dev)
		return -ENODEV;

691 692 693 694 695 696 697 698 699 700 701 702 703 704
	kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
	if (!kdev)
		return -ENOMEM;
	kdev->dev = dev;
	dev->kobj_dev = kdev;

	init_completion(&kdev->kobj_unregister);

	error = kobject_init_and_add(&kdev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
				   "cpuidle");
	if (error) {
		kfree(kdev);
		return error;
	}
705

706 707 708
	kobject_uevent(&kdev->kobj, KOBJ_ADD);

	return 0;
709 710 711 712
}

/**
 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
713
 * @dev: the target device
714
 */
715
void cpuidle_remove_sysfs(struct cpuidle_device *dev)
716
{
717 718 719 720 721
	struct cpuidle_device_kobj *kdev = dev->kobj_dev;

	kobject_put(&kdev->kobj);
	wait_for_completion(&kdev->kobj_unregister);
	kfree(kdev);
722
}