sysfs.c 16 KB

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