sysfs.c 15.2 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/capability.h>
15
#include <linux/device.h>
16 17 18 19 20 21 22 23 24 25 26

#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);

27 28
static ssize_t show_available_governors(struct device *dev,
					struct device_attribute *attr,
29
					char *buf)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
{
	ssize_t i = 0;
	struct cpuidle_governor *tmp;

	mutex_lock(&cpuidle_lock);
	list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
		if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
			goto out;
		i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
	}

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

47 48
static ssize_t show_current_driver(struct device *dev,
				   struct device_attribute *attr,
49
				   char *buf)
50 51
{
	ssize_t ret;
52
	struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
53 54

	spin_lock(&cpuidle_driver_lock);
55 56
	if (cpuidle_driver)
		ret = sprintf(buf, "%s\n", cpuidle_driver->name);
57 58 59 60 61 62 63
	else
		ret = sprintf(buf, "none\n");
	spin_unlock(&cpuidle_driver_lock);

	return ret;
}

64 65
static ssize_t show_current_governor(struct device *dev,
				     struct device_attribute *attr,
66
				     char *buf)
67 68 69 70 71 72 73 74 75 76 77 78 79
{
	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;
}

80 81
static ssize_t store_current_governor(struct device *dev,
				      struct device_attribute *attr,
82
				      const char *buf, size_t count)
83 84 85 86 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
{
	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;
}

114 115
static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);
static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
116

117 118 119
static struct attribute *cpuidle_default_attrs[] = {
	&dev_attr_current_driver.attr,
	&dev_attr_current_governor_ro.attr,
120 121 122
	NULL
};

123 124 125
static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL);
static DEVICE_ATTR(current_governor, 0644, show_current_governor,
		   store_current_governor);
126

127 128 129 130
static struct attribute *cpuidle_switch_attrs[] = {
	&dev_attr_available_governors.attr,
	&dev_attr_current_driver.attr,
	&dev_attr_current_governor.attr,
131 132 133
	NULL
};

134 135
static struct attribute_group cpuidle_attr_group = {
	.attrs = cpuidle_default_attrs,
136 137 138 139
	.name = "cpuidle",
};

/**
140
 * cpuidle_add_interface - add CPU global sysfs attributes
141
 */
142
int cpuidle_add_interface(struct device *dev)
143 144
{
	if (sysfs_switch)
145
		cpuidle_attr_group.attrs = cpuidle_switch_attrs;
146

147
	return sysfs_create_group(&dev->kobj, &cpuidle_attr_group);
148 149 150
}

/**
151
 * cpuidle_remove_interface - remove CPU global sysfs attributes
152
 */
153
void cpuidle_remove_interface(struct device *dev)
154
{
155
	sysfs_remove_group(&dev->kobj, &cpuidle_attr_group);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
}

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 kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
#define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
{
	int ret = -EIO;
	struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
	struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);

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

static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
		     const char * buf, size_t count)
{
	int ret = -EIO;
	struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
	struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);

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

200
static const struct sysfs_ops cpuidle_sysfs_ops = {
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	.show = cpuidle_show,
	.store = cpuidle_store,
};

static void cpuidle_sysfs_release(struct kobject *kobj)
{
	struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);

	complete(&dev->kobj_unregister);
}

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

struct cpuidle_state_attr {
	struct attribute attr;
219 220
	ssize_t (*show)(struct cpuidle_state *, \
					struct cpuidle_state_usage *, char *);
221 222
	ssize_t (*store)(struct cpuidle_state *, \
			struct cpuidle_state_usage *, const char *, size_t);
223 224 225 226 227
};

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

228 229 230
#define define_one_state_rw(_name, show, store) \
static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)

231
#define define_show_state_function(_name) \
232 233
static ssize_t show_state_##_name(struct cpuidle_state *state, \
			 struct cpuidle_state_usage *state_usage, char *buf) \
234 235 236 237
{ \
	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 242
		const char *buf, size_t size) \
{ \
243
	unsigned long long value; \
244 245 246
	int err; \
	if (!capable(CAP_SYS_ADMIN)) \
		return -EPERM; \
247
	err = kstrtoull(buf, 0, &value); \
248 249 250
	if (err) \
		return err; \
	if (value) \
251
		state_usage->_name = 1; \
252
	else \
253
		state_usage->_name = 0; \
254 255 256
	return size; \
}

257
#define define_show_state_ull_function(_name) \
258 259
static ssize_t show_state_##_name(struct cpuidle_state *state, \
			struct cpuidle_state_usage *state_usage, char *buf) \
260
{ \
261
	return sprintf(buf, "%llu\n", state_usage->_name);\
262 263
}

264
#define define_show_state_str_function(_name) \
265 266
static ssize_t show_state_##_name(struct cpuidle_state *state, \
			struct cpuidle_state_usage *state_usage, char *buf) \
267 268 269 270
{ \
	if (state->_name[0] == '\0')\
		return sprintf(buf, "<null>\n");\
	return sprintf(buf, "%s\n", state->_name);\
271 272 273 274
}

define_show_state_function(exit_latency)
define_show_state_function(power_usage)
275 276
define_show_state_ull_function(usage)
define_show_state_ull_function(time)
277 278
define_show_state_str_function(name)
define_show_state_str_function(desc)
279 280
define_show_state_ull_function(disable)
define_store_state_ull_function(disable)
281

282
define_one_state_ro(name, show_state_name);
283
define_one_state_ro(desc, show_state_desc);
284 285 286 287
define_one_state_ro(latency, show_state_exit_latency);
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);
288
define_one_state_rw(disable, show_state_disable, store_state_disable);
289 290 291

static struct attribute *cpuidle_state_default_attrs[] = {
	&attr_name.attr,
292
	&attr_desc.attr,
293 294 295 296
	&attr_latency.attr,
	&attr_power.attr,
	&attr_usage.attr,
	&attr_time.attr,
297
	&attr_disable.attr,
298 299 300
	NULL
};

301 302 303 304 305 306 307
struct cpuidle_state_kobj {
	struct cpuidle_state *state;
	struct cpuidle_state_usage *state_usage;
	struct completion kobj_unregister;
	struct kobject kobj;
};

308 309
#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)
310
#define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
311 312 313 314 315 316
#define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
static ssize_t cpuidle_state_show(struct kobject * kobj,
	struct attribute * attr ,char * buf)
{
	int ret = -EIO;
	struct cpuidle_state *state = kobj_to_state(kobj);
317
	struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
318 319 320
	struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);

	if (cattr->show)
321
		ret = cattr->show(state, state_usage, buf);
322 323 324 325

	return ret;
}

326 327 328 329 330
static ssize_t cpuidle_state_store(struct kobject *kobj,
	struct attribute *attr, const char *buf, size_t size)
{
	int ret = -EIO;
	struct cpuidle_state *state = kobj_to_state(kobj);
331
	struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
332 333 334
	struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);

	if (cattr->store)
335
		ret = cattr->store(state, state_usage, buf, size);
336 337 338 339

	return ret;
}

340
static const struct sysfs_ops cpuidle_state_sysfs_ops = {
341
	.show = cpuidle_state_show,
342
	.store = cpuidle_state_store,
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
};

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,
};

358
static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
359
{
360
	kobject_put(&device->kobjs[i]->kobj);
361 362 363 364 365 366
	wait_for_completion(&device->kobjs[i]->kobj_unregister);
	kfree(device->kobjs[i]);
	device->kobjs[i] = NULL;
}

/**
D
Daniel Lezcano 已提交
367
 * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes
368 369
 * @device: the target device
 */
D
Daniel Lezcano 已提交
370
static int cpuidle_add_state_sysfs(struct cpuidle_device *device)
371 372 373
{
	int i, ret = -ENOMEM;
	struct cpuidle_state_kobj *kobj;
D
Daniel Lezcano 已提交
374
	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
375 376

	/* state statistics */
377
	for (i = 0; i < device->state_count; i++) {
378 379 380
		kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
		if (!kobj)
			goto error_state;
381
		kobj->state = &drv->states[i];
382
		kobj->state_usage = &device->states_usage[i];
383 384
		init_completion(&kobj->kobj_unregister);

385 386
		ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle,
					   &device->kobj, "state%d", i);
387 388 389 390
		if (ret) {
			kfree(kobj);
			goto error_state;
		}
391
		kobject_uevent(&kobj->kobj, KOBJ_ADD);
392 393 394 395 396 397 398 399 400 401 402 403
		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 已提交
404
 * cpuidle_remove_driver_sysfs - removes the cpuidle states sysfs attributes
405 406
 * @device: the target device
 */
D
Daniel Lezcano 已提交
407
static void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
408 409 410 411 412 413 414
{
	int i;

	for (i = 0; i < device->state_count; i++)
		cpuidle_free_state_kobj(device, i);
}

D
Daniel Lezcano 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 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 543 544 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
#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 = \
		__ATTR(_name, 0644, show, NULL)

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);
}

static ssize_t cpuidle_driver_show(struct kobject *kobj, struct attribute * attr,
				   char * buf)
{
	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;
	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,
				   &dev->kobj, "driver");
	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);
}

577 578
/**
 * cpuidle_add_sysfs - creates a sysfs instance for the target device
579
 * @dev: the target device
580
 */
581
int cpuidle_add_sysfs(struct cpuidle_device *dev)
582
{
583
	struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
584
	int error;
585

586 587
	init_completion(&dev->kobj_unregister);

588
	error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
589 590 591 592
				     "cpuidle");
	if (!error)
		kobject_uevent(&dev->kobj, KOBJ_ADD);
	return error;
593 594 595 596
}

/**
 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
597
 * @dev: the target device
598
 */
599
void cpuidle_remove_sysfs(struct cpuidle_device *dev)
600
{
601
	kobject_put(&dev->kobj);
602
	wait_for_completion(&dev->kobj_unregister);
603
}