sysfs.c 16 KB

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