sysfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. #include <linux/kernel.h>
  9. #include <linux/cpuidle.h>
  10. #include <linux/sysfs.h>
  11. #include <linux/slab.h>
  12. #include <linux/cpu.h>
  13. #include <linux/capability.h>
  14. #include <linux/device.h>
  15. #include "cpuidle.h"
  16. static unsigned int sysfs_switch;
  17. static int __init cpuidle_sysfs_setup(char *unused)
  18. {
  19. sysfs_switch = 1;
  20. return 1;
  21. }
  22. __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
  23. static ssize_t show_available_governors(struct device *dev,
  24. struct device_attribute *attr,
  25. char *buf)
  26. {
  27. ssize_t i = 0;
  28. struct cpuidle_governor *tmp;
  29. mutex_lock(&cpuidle_lock);
  30. list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
  31. if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) -
  32. CPUIDLE_NAME_LEN - 2))
  33. goto out;
  34. i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
  35. }
  36. out:
  37. i+= sprintf(&buf[i], "\n");
  38. mutex_unlock(&cpuidle_lock);
  39. return i;
  40. }
  41. static ssize_t show_current_driver(struct device *dev,
  42. struct device_attribute *attr,
  43. char *buf)
  44. {
  45. ssize_t ret;
  46. struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
  47. spin_lock(&cpuidle_driver_lock);
  48. if (cpuidle_driver)
  49. ret = sprintf(buf, "%s\n", cpuidle_driver->name);
  50. else
  51. ret = sprintf(buf, "none\n");
  52. spin_unlock(&cpuidle_driver_lock);
  53. return ret;
  54. }
  55. static ssize_t show_current_governor(struct device *dev,
  56. struct device_attribute *attr,
  57. char *buf)
  58. {
  59. ssize_t ret;
  60. mutex_lock(&cpuidle_lock);
  61. if (cpuidle_curr_governor)
  62. ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
  63. else
  64. ret = sprintf(buf, "none\n");
  65. mutex_unlock(&cpuidle_lock);
  66. return ret;
  67. }
  68. static ssize_t store_current_governor(struct device *dev,
  69. struct device_attribute *attr,
  70. const char *buf, size_t count)
  71. {
  72. char gov_name[CPUIDLE_NAME_LEN];
  73. int ret = -EINVAL;
  74. size_t len = count;
  75. struct cpuidle_governor *gov;
  76. if (!len || len >= sizeof(gov_name))
  77. return -EINVAL;
  78. memcpy(gov_name, buf, len);
  79. gov_name[len] = '\0';
  80. if (gov_name[len - 1] == '\n')
  81. gov_name[--len] = '\0';
  82. mutex_lock(&cpuidle_lock);
  83. list_for_each_entry(gov, &cpuidle_governors, governor_list) {
  84. if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
  85. ret = cpuidle_switch_governor(gov);
  86. break;
  87. }
  88. }
  89. mutex_unlock(&cpuidle_lock);
  90. if (ret)
  91. return ret;
  92. else
  93. return count;
  94. }
  95. static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);
  96. static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
  97. static struct attribute *cpuidle_default_attrs[] = {
  98. &dev_attr_current_driver.attr,
  99. &dev_attr_current_governor_ro.attr,
  100. NULL
  101. };
  102. static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL);
  103. static DEVICE_ATTR(current_governor, 0644, show_current_governor,
  104. store_current_governor);
  105. static struct attribute *cpuidle_switch_attrs[] = {
  106. &dev_attr_available_governors.attr,
  107. &dev_attr_current_driver.attr,
  108. &dev_attr_current_governor.attr,
  109. NULL
  110. };
  111. static struct attribute_group cpuidle_attr_group = {
  112. .attrs = cpuidle_default_attrs,
  113. .name = "cpuidle",
  114. };
  115. /**
  116. * cpuidle_add_interface - add CPU global sysfs attributes
  117. */
  118. int cpuidle_add_interface(struct device *dev)
  119. {
  120. if (sysfs_switch)
  121. cpuidle_attr_group.attrs = cpuidle_switch_attrs;
  122. return sysfs_create_group(&dev->kobj, &cpuidle_attr_group);
  123. }
  124. /**
  125. * cpuidle_remove_interface - remove CPU global sysfs attributes
  126. */
  127. void cpuidle_remove_interface(struct device *dev)
  128. {
  129. sysfs_remove_group(&dev->kobj, &cpuidle_attr_group);
  130. }
  131. struct cpuidle_attr {
  132. struct attribute attr;
  133. ssize_t (*show)(struct cpuidle_device *, char *);
  134. ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
  135. };
  136. #define define_one_ro(_name, show) \
  137. static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
  138. #define define_one_rw(_name, show, store) \
  139. static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
  140. #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
  141. #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
  142. static ssize_t cpuidle_show(struct kobject *kobj, struct attribute *attr,
  143. char *buf)
  144. {
  145. int ret = -EIO;
  146. struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
  147. struct cpuidle_attr *cattr = attr_to_cpuidleattr(attr);
  148. if (cattr->show) {
  149. mutex_lock(&cpuidle_lock);
  150. ret = cattr->show(dev, buf);
  151. mutex_unlock(&cpuidle_lock);
  152. }
  153. return ret;
  154. }
  155. static ssize_t cpuidle_store(struct kobject *kobj, struct attribute *attr,
  156. const char *buf, size_t count)
  157. {
  158. int ret = -EIO;
  159. struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
  160. struct cpuidle_attr *cattr = attr_to_cpuidleattr(attr);
  161. if (cattr->store) {
  162. mutex_lock(&cpuidle_lock);
  163. ret = cattr->store(dev, buf, count);
  164. mutex_unlock(&cpuidle_lock);
  165. }
  166. return ret;
  167. }
  168. static const struct sysfs_ops cpuidle_sysfs_ops = {
  169. .show = cpuidle_show,
  170. .store = cpuidle_store,
  171. };
  172. static void cpuidle_sysfs_release(struct kobject *kobj)
  173. {
  174. struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
  175. complete(&dev->kobj_unregister);
  176. }
  177. static struct kobj_type ktype_cpuidle = {
  178. .sysfs_ops = &cpuidle_sysfs_ops,
  179. .release = cpuidle_sysfs_release,
  180. };
  181. struct cpuidle_state_attr {
  182. struct attribute attr;
  183. ssize_t (*show)(struct cpuidle_state *, \
  184. struct cpuidle_state_usage *, char *);
  185. ssize_t (*store)(struct cpuidle_state *, \
  186. struct cpuidle_state_usage *, const char *, size_t);
  187. };
  188. #define define_one_state_ro(_name, show) \
  189. static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
  190. #define define_one_state_rw(_name, show, store) \
  191. static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
  192. #define define_show_state_function(_name) \
  193. static ssize_t show_state_##_name(struct cpuidle_state *state, \
  194. struct cpuidle_state_usage *state_usage, char *buf) \
  195. { \
  196. return sprintf(buf, "%u\n", state->_name);\
  197. }
  198. #define define_store_state_ull_function(_name) \
  199. static ssize_t store_state_##_name(struct cpuidle_state *state, \
  200. struct cpuidle_state_usage *state_usage, \
  201. const char *buf, size_t size) \
  202. { \
  203. unsigned long long value; \
  204. int err; \
  205. if (!capable(CAP_SYS_ADMIN)) \
  206. return -EPERM; \
  207. err = kstrtoull(buf, 0, &value); \
  208. if (err) \
  209. return err; \
  210. if (value) \
  211. state_usage->_name = 1; \
  212. else \
  213. state_usage->_name = 0; \
  214. return size; \
  215. }
  216. #define define_show_state_ull_function(_name) \
  217. static ssize_t show_state_##_name(struct cpuidle_state *state, \
  218. struct cpuidle_state_usage *state_usage, \
  219. char *buf) \
  220. { \
  221. return sprintf(buf, "%llu\n", state_usage->_name);\
  222. }
  223. #define define_show_state_str_function(_name) \
  224. static ssize_t show_state_##_name(struct cpuidle_state *state, \
  225. struct cpuidle_state_usage *state_usage, \
  226. char *buf) \
  227. { \
  228. if (state->_name[0] == '\0')\
  229. return sprintf(buf, "<null>\n");\
  230. return sprintf(buf, "%s\n", state->_name);\
  231. }
  232. define_show_state_function(exit_latency)
  233. define_show_state_function(power_usage)
  234. define_show_state_ull_function(usage)
  235. define_show_state_ull_function(time)
  236. define_show_state_str_function(name)
  237. define_show_state_str_function(desc)
  238. define_show_state_ull_function(disable)
  239. define_store_state_ull_function(disable)
  240. define_one_state_ro(name, show_state_name);
  241. define_one_state_ro(desc, show_state_desc);
  242. define_one_state_ro(latency, show_state_exit_latency);
  243. define_one_state_ro(power, show_state_power_usage);
  244. define_one_state_ro(usage, show_state_usage);
  245. define_one_state_ro(time, show_state_time);
  246. define_one_state_rw(disable, show_state_disable, store_state_disable);
  247. static struct attribute *cpuidle_state_default_attrs[] = {
  248. &attr_name.attr,
  249. &attr_desc.attr,
  250. &attr_latency.attr,
  251. &attr_power.attr,
  252. &attr_usage.attr,
  253. &attr_time.attr,
  254. &attr_disable.attr,
  255. NULL
  256. };
  257. struct cpuidle_state_kobj {
  258. struct cpuidle_state *state;
  259. struct cpuidle_state_usage *state_usage;
  260. struct completion kobj_unregister;
  261. struct kobject kobj;
  262. };
  263. #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
  264. #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
  265. #define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
  266. #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
  267. static ssize_t cpuidle_state_show(struct kobject *kobj, struct attribute *attr,
  268. char * buf)
  269. {
  270. int ret = -EIO;
  271. struct cpuidle_state *state = kobj_to_state(kobj);
  272. struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
  273. struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
  274. if (cattr->show)
  275. ret = cattr->show(state, state_usage, buf);
  276. return ret;
  277. }
  278. static ssize_t cpuidle_state_store(struct kobject *kobj, struct attribute *attr,
  279. const char *buf, size_t size)
  280. {
  281. int ret = -EIO;
  282. struct cpuidle_state *state = kobj_to_state(kobj);
  283. struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
  284. struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
  285. if (cattr->store)
  286. ret = cattr->store(state, state_usage, buf, size);
  287. return ret;
  288. }
  289. static const struct sysfs_ops cpuidle_state_sysfs_ops = {
  290. .show = cpuidle_state_show,
  291. .store = cpuidle_state_store,
  292. };
  293. static void cpuidle_state_sysfs_release(struct kobject *kobj)
  294. {
  295. struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
  296. complete(&state_obj->kobj_unregister);
  297. }
  298. static struct kobj_type ktype_state_cpuidle = {
  299. .sysfs_ops = &cpuidle_state_sysfs_ops,
  300. .default_attrs = cpuidle_state_default_attrs,
  301. .release = cpuidle_state_sysfs_release,
  302. };
  303. static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
  304. {
  305. kobject_put(&device->kobjs[i]->kobj);
  306. wait_for_completion(&device->kobjs[i]->kobj_unregister);
  307. kfree(device->kobjs[i]);
  308. device->kobjs[i] = NULL;
  309. }
  310. /**
  311. * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes
  312. * @device: the target device
  313. */
  314. static int cpuidle_add_state_sysfs(struct cpuidle_device *device)
  315. {
  316. int i, ret = -ENOMEM;
  317. struct cpuidle_state_kobj *kobj;
  318. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
  319. /* state statistics */
  320. for (i = 0; i < device->state_count; i++) {
  321. kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
  322. if (!kobj)
  323. goto error_state;
  324. kobj->state = &drv->states[i];
  325. kobj->state_usage = &device->states_usage[i];
  326. init_completion(&kobj->kobj_unregister);
  327. ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle,
  328. &device->kobj, "state%d", i);
  329. if (ret) {
  330. kfree(kobj);
  331. goto error_state;
  332. }
  333. kobject_uevent(&kobj->kobj, KOBJ_ADD);
  334. device->kobjs[i] = kobj;
  335. }
  336. return 0;
  337. error_state:
  338. for (i = i - 1; i >= 0; i--)
  339. cpuidle_free_state_kobj(device, i);
  340. return ret;
  341. }
  342. /**
  343. * cpuidle_remove_driver_sysfs - removes the cpuidle states sysfs attributes
  344. * @device: the target device
  345. */
  346. static void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
  347. {
  348. int i;
  349. for (i = 0; i < device->state_count; i++)
  350. cpuidle_free_state_kobj(device, i);
  351. }
  352. #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
  353. #define kobj_to_driver_kobj(k) container_of(k, struct cpuidle_driver_kobj, kobj)
  354. #define attr_to_driver_attr(a) container_of(a, struct cpuidle_driver_attr, attr)
  355. #define define_one_driver_ro(_name, show) \
  356. static struct cpuidle_driver_attr attr_driver_##_name = \
  357. __ATTR(_name, 0644, show, NULL)
  358. struct cpuidle_driver_kobj {
  359. struct cpuidle_driver *drv;
  360. struct completion kobj_unregister;
  361. struct kobject kobj;
  362. };
  363. struct cpuidle_driver_attr {
  364. struct attribute attr;
  365. ssize_t (*show)(struct cpuidle_driver *, char *);
  366. ssize_t (*store)(struct cpuidle_driver *, const char *, size_t);
  367. };
  368. static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
  369. {
  370. ssize_t ret;
  371. spin_lock(&cpuidle_driver_lock);
  372. ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
  373. spin_unlock(&cpuidle_driver_lock);
  374. return ret;
  375. }
  376. static void cpuidle_driver_sysfs_release(struct kobject *kobj)
  377. {
  378. struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
  379. complete(&driver_kobj->kobj_unregister);
  380. }
  381. static ssize_t cpuidle_driver_show(struct kobject *kobj, struct attribute *attr,
  382. char *buf)
  383. {
  384. int ret = -EIO;
  385. struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
  386. struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
  387. if (dattr->show)
  388. ret = dattr->show(driver_kobj->drv, buf);
  389. return ret;
  390. }
  391. static ssize_t cpuidle_driver_store(struct kobject *kobj, struct attribute *attr,
  392. const char *buf, size_t size)
  393. {
  394. int ret = -EIO;
  395. struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
  396. struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
  397. if (dattr->store)
  398. ret = dattr->store(driver_kobj->drv, buf, size);
  399. return ret;
  400. }
  401. define_one_driver_ro(name, show_driver_name);
  402. static const struct sysfs_ops cpuidle_driver_sysfs_ops = {
  403. .show = cpuidle_driver_show,
  404. .store = cpuidle_driver_store,
  405. };
  406. static struct attribute *cpuidle_driver_default_attrs[] = {
  407. &attr_driver_name.attr,
  408. NULL
  409. };
  410. static struct kobj_type ktype_driver_cpuidle = {
  411. .sysfs_ops = &cpuidle_driver_sysfs_ops,
  412. .default_attrs = cpuidle_driver_default_attrs,
  413. .release = cpuidle_driver_sysfs_release,
  414. };
  415. /**
  416. * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute
  417. * @device: the target device
  418. */
  419. static int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
  420. {
  421. struct cpuidle_driver_kobj *kdrv;
  422. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  423. int ret;
  424. kdrv = kzalloc(sizeof(*kdrv), GFP_KERNEL);
  425. if (!kdrv)
  426. return -ENOMEM;
  427. kdrv->drv = drv;
  428. init_completion(&kdrv->kobj_unregister);
  429. ret = kobject_init_and_add(&kdrv->kobj, &ktype_driver_cpuidle,
  430. &dev->kobj, "driver");
  431. if (ret) {
  432. kfree(kdrv);
  433. return ret;
  434. }
  435. kobject_uevent(&kdrv->kobj, KOBJ_ADD);
  436. dev->kobj_driver = kdrv;
  437. return ret;
  438. }
  439. /**
  440. * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute
  441. * @device: the target device
  442. */
  443. static void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
  444. {
  445. struct cpuidle_driver_kobj *kdrv = dev->kobj_driver;
  446. kobject_put(&kdrv->kobj);
  447. wait_for_completion(&kdrv->kobj_unregister);
  448. kfree(kdrv);
  449. }
  450. #else
  451. static inline int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
  452. {
  453. return 0;
  454. }
  455. static inline void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
  456. {
  457. ;
  458. }
  459. #endif
  460. /**
  461. * cpuidle_add_device_sysfs - adds device specific sysfs attributes
  462. * @device: the target device
  463. */
  464. int cpuidle_add_device_sysfs(struct cpuidle_device *device)
  465. {
  466. int ret;
  467. ret = cpuidle_add_state_sysfs(device);
  468. if (ret)
  469. return ret;
  470. ret = cpuidle_add_driver_sysfs(device);
  471. if (ret)
  472. cpuidle_remove_state_sysfs(device);
  473. return ret;
  474. }
  475. /**
  476. * cpuidle_remove_device_sysfs : removes device specific sysfs attributes
  477. * @device : the target device
  478. */
  479. void cpuidle_remove_device_sysfs(struct cpuidle_device *device)
  480. {
  481. cpuidle_remove_driver_sysfs(device);
  482. cpuidle_remove_state_sysfs(device);
  483. }
  484. /**
  485. * cpuidle_add_sysfs - creates a sysfs instance for the target device
  486. * @dev: the target device
  487. */
  488. int cpuidle_add_sysfs(struct cpuidle_device *dev)
  489. {
  490. struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
  491. int error;
  492. init_completion(&dev->kobj_unregister);
  493. error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
  494. "cpuidle");
  495. if (!error)
  496. kobject_uevent(&dev->kobj, KOBJ_ADD);
  497. return error;
  498. }
  499. /**
  500. * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
  501. * @dev: the target device
  502. */
  503. void cpuidle_remove_sysfs(struct cpuidle_device *dev)
  504. {
  505. kobject_put(&dev->kobj);
  506. wait_for_completion(&dev->kobj_unregister);
  507. }