sysfs.c 15 KB

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