sysfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 "cpuidle.h"
  15. static unsigned int sysfs_switch;
  16. static int __init cpuidle_sysfs_setup(char *unused)
  17. {
  18. sysfs_switch = 1;
  19. return 1;
  20. }
  21. __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
  22. static ssize_t show_available_governors(struct device *dev,
  23. struct device_attribute *attr,
  24. char *buf)
  25. {
  26. ssize_t i = 0;
  27. struct cpuidle_governor *tmp;
  28. mutex_lock(&cpuidle_lock);
  29. list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
  30. if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
  31. goto out;
  32. i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
  33. }
  34. out:
  35. i+= sprintf(&buf[i], "\n");
  36. mutex_unlock(&cpuidle_lock);
  37. return i;
  38. }
  39. static ssize_t show_current_driver(struct device *dev,
  40. struct device_attribute *attr,
  41. char *buf)
  42. {
  43. ssize_t ret;
  44. struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
  45. spin_lock(&cpuidle_driver_lock);
  46. if (cpuidle_driver)
  47. ret = sprintf(buf, "%s\n", cpuidle_driver->name);
  48. else
  49. ret = sprintf(buf, "none\n");
  50. spin_unlock(&cpuidle_driver_lock);
  51. return ret;
  52. }
  53. static ssize_t show_current_governor(struct device *dev,
  54. struct device_attribute *attr,
  55. char *buf)
  56. {
  57. ssize_t ret;
  58. mutex_lock(&cpuidle_lock);
  59. if (cpuidle_curr_governor)
  60. ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
  61. else
  62. ret = sprintf(buf, "none\n");
  63. mutex_unlock(&cpuidle_lock);
  64. return ret;
  65. }
  66. static ssize_t store_current_governor(struct device *dev,
  67. struct device_attribute *attr,
  68. const char *buf, size_t count)
  69. {
  70. char gov_name[CPUIDLE_NAME_LEN];
  71. int ret = -EINVAL;
  72. size_t len = count;
  73. struct cpuidle_governor *gov;
  74. if (!len || len >= sizeof(gov_name))
  75. return -EINVAL;
  76. memcpy(gov_name, buf, len);
  77. gov_name[len] = '\0';
  78. if (gov_name[len - 1] == '\n')
  79. gov_name[--len] = '\0';
  80. mutex_lock(&cpuidle_lock);
  81. list_for_each_entry(gov, &cpuidle_governors, governor_list) {
  82. if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
  83. ret = cpuidle_switch_governor(gov);
  84. break;
  85. }
  86. }
  87. mutex_unlock(&cpuidle_lock);
  88. if (ret)
  89. return ret;
  90. else
  91. return count;
  92. }
  93. static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);
  94. static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
  95. static struct attribute *cpuidle_default_attrs[] = {
  96. &dev_attr_current_driver.attr,
  97. &dev_attr_current_governor_ro.attr,
  98. NULL
  99. };
  100. static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL);
  101. static DEVICE_ATTR(current_governor, 0644, show_current_governor,
  102. store_current_governor);
  103. static struct attribute *cpuidle_switch_attrs[] = {
  104. &dev_attr_available_governors.attr,
  105. &dev_attr_current_driver.attr,
  106. &dev_attr_current_governor.attr,
  107. NULL
  108. };
  109. static struct attribute_group cpuidle_attr_group = {
  110. .attrs = cpuidle_default_attrs,
  111. .name = "cpuidle",
  112. };
  113. /**
  114. * cpuidle_add_interface - add CPU global sysfs attributes
  115. */
  116. int cpuidle_add_interface(struct device *dev)
  117. {
  118. if (sysfs_switch)
  119. cpuidle_attr_group.attrs = cpuidle_switch_attrs;
  120. return sysfs_create_group(&dev->kobj, &cpuidle_attr_group);
  121. }
  122. /**
  123. * cpuidle_remove_interface - remove CPU global sysfs attributes
  124. */
  125. void cpuidle_remove_interface(struct device *dev)
  126. {
  127. sysfs_remove_group(&dev->kobj, &cpuidle_attr_group);
  128. }
  129. struct cpuidle_attr {
  130. struct attribute attr;
  131. ssize_t (*show)(struct cpuidle_device *, char *);
  132. ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
  133. };
  134. #define define_one_ro(_name, show) \
  135. static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
  136. #define define_one_rw(_name, show, store) \
  137. static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
  138. #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
  139. #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
  140. static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
  141. {
  142. int ret = -EIO;
  143. struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
  144. struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
  145. if (cattr->show) {
  146. mutex_lock(&cpuidle_lock);
  147. ret = cattr->show(dev, buf);
  148. mutex_unlock(&cpuidle_lock);
  149. }
  150. return ret;
  151. }
  152. static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
  153. const char * buf, size_t count)
  154. {
  155. int ret = -EIO;
  156. struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
  157. struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
  158. if (cattr->store) {
  159. mutex_lock(&cpuidle_lock);
  160. ret = cattr->store(dev, buf, count);
  161. mutex_unlock(&cpuidle_lock);
  162. }
  163. return ret;
  164. }
  165. static const struct sysfs_ops cpuidle_sysfs_ops = {
  166. .show = cpuidle_show,
  167. .store = cpuidle_store,
  168. };
  169. static void cpuidle_sysfs_release(struct kobject *kobj)
  170. {
  171. struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
  172. complete(&dev->kobj_unregister);
  173. }
  174. static struct kobj_type ktype_cpuidle = {
  175. .sysfs_ops = &cpuidle_sysfs_ops,
  176. .release = cpuidle_sysfs_release,
  177. };
  178. struct cpuidle_state_attr {
  179. struct attribute attr;
  180. ssize_t (*show)(struct cpuidle_state *, \
  181. struct cpuidle_state_usage *, char *);
  182. ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
  183. };
  184. #define define_one_state_ro(_name, show) \
  185. static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
  186. #define define_one_state_rw(_name, show, store) \
  187. static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
  188. #define define_show_state_function(_name) \
  189. static ssize_t show_state_##_name(struct cpuidle_state *state, \
  190. struct cpuidle_state_usage *state_usage, char *buf) \
  191. { \
  192. return sprintf(buf, "%u\n", state->_name);\
  193. }
  194. #define define_store_state_function(_name) \
  195. static ssize_t store_state_##_name(struct cpuidle_state *state, \
  196. const char *buf, size_t size) \
  197. { \
  198. long value; \
  199. int err; \
  200. if (!capable(CAP_SYS_ADMIN)) \
  201. return -EPERM; \
  202. err = kstrtol(buf, 0, &value); \
  203. if (err) \
  204. return err; \
  205. if (value) \
  206. state->disable = 1; \
  207. else \
  208. state->disable = 0; \
  209. return size; \
  210. }
  211. #define define_show_state_ull_function(_name) \
  212. static ssize_t show_state_##_name(struct cpuidle_state *state, \
  213. struct cpuidle_state_usage *state_usage, char *buf) \
  214. { \
  215. return sprintf(buf, "%llu\n", state_usage->_name);\
  216. }
  217. #define define_show_state_str_function(_name) \
  218. static ssize_t show_state_##_name(struct cpuidle_state *state, \
  219. struct cpuidle_state_usage *state_usage, char *buf) \
  220. { \
  221. if (state->_name[0] == '\0')\
  222. return sprintf(buf, "<null>\n");\
  223. return sprintf(buf, "%s\n", state->_name);\
  224. }
  225. define_show_state_function(exit_latency)
  226. define_show_state_function(power_usage)
  227. define_show_state_ull_function(usage)
  228. define_show_state_ull_function(time)
  229. define_show_state_str_function(name)
  230. define_show_state_str_function(desc)
  231. define_show_state_function(disable)
  232. define_store_state_function(disable)
  233. define_one_state_ro(name, show_state_name);
  234. define_one_state_ro(desc, show_state_desc);
  235. define_one_state_ro(latency, show_state_exit_latency);
  236. define_one_state_ro(power, show_state_power_usage);
  237. define_one_state_ro(usage, show_state_usage);
  238. define_one_state_ro(time, show_state_time);
  239. define_one_state_rw(disable, show_state_disable, store_state_disable);
  240. static struct attribute *cpuidle_state_default_attrs[] = {
  241. &attr_name.attr,
  242. &attr_desc.attr,
  243. &attr_latency.attr,
  244. &attr_power.attr,
  245. &attr_usage.attr,
  246. &attr_time.attr,
  247. &attr_disable.attr,
  248. NULL
  249. };
  250. #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
  251. #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
  252. #define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
  253. #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
  254. static ssize_t cpuidle_state_show(struct kobject * kobj,
  255. struct attribute * attr ,char * buf)
  256. {
  257. int ret = -EIO;
  258. struct cpuidle_state *state = kobj_to_state(kobj);
  259. struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
  260. struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
  261. if (cattr->show)
  262. ret = cattr->show(state, state_usage, buf);
  263. return ret;
  264. }
  265. static ssize_t cpuidle_state_store(struct kobject *kobj,
  266. struct attribute *attr, const char *buf, size_t size)
  267. {
  268. int ret = -EIO;
  269. struct cpuidle_state *state = kobj_to_state(kobj);
  270. struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
  271. if (cattr->store)
  272. ret = cattr->store(state, buf, size);
  273. return ret;
  274. }
  275. static const struct sysfs_ops cpuidle_state_sysfs_ops = {
  276. .show = cpuidle_state_show,
  277. .store = cpuidle_state_store,
  278. };
  279. static void cpuidle_state_sysfs_release(struct kobject *kobj)
  280. {
  281. struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
  282. complete(&state_obj->kobj_unregister);
  283. }
  284. static struct kobj_type ktype_state_cpuidle = {
  285. .sysfs_ops = &cpuidle_state_sysfs_ops,
  286. .default_attrs = cpuidle_state_default_attrs,
  287. .release = cpuidle_state_sysfs_release,
  288. };
  289. static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
  290. {
  291. kobject_put(&device->kobjs[i]->kobj);
  292. wait_for_completion(&device->kobjs[i]->kobj_unregister);
  293. kfree(device->kobjs[i]);
  294. device->kobjs[i] = NULL;
  295. }
  296. /**
  297. * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
  298. * @device: the target device
  299. */
  300. int cpuidle_add_state_sysfs(struct cpuidle_device *device)
  301. {
  302. int i, ret = -ENOMEM;
  303. struct cpuidle_state_kobj *kobj;
  304. struct cpuidle_driver *drv = cpuidle_get_driver();
  305. /* state statistics */
  306. for (i = 0; i < device->state_count; i++) {
  307. kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
  308. if (!kobj)
  309. goto error_state;
  310. kobj->state = &drv->states[i];
  311. kobj->state_usage = &device->states_usage[i];
  312. init_completion(&kobj->kobj_unregister);
  313. ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
  314. "state%d", i);
  315. if (ret) {
  316. kfree(kobj);
  317. goto error_state;
  318. }
  319. kobject_uevent(&kobj->kobj, KOBJ_ADD);
  320. device->kobjs[i] = kobj;
  321. }
  322. return 0;
  323. error_state:
  324. for (i = i - 1; i >= 0; i--)
  325. cpuidle_free_state_kobj(device, i);
  326. return ret;
  327. }
  328. /**
  329. * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
  330. * @device: the target device
  331. */
  332. void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
  333. {
  334. int i;
  335. for (i = 0; i < device->state_count; i++)
  336. cpuidle_free_state_kobj(device, i);
  337. }
  338. /**
  339. * cpuidle_add_sysfs - creates a sysfs instance for the target device
  340. * @dev: the target device
  341. */
  342. int cpuidle_add_sysfs(struct device *cpu_dev)
  343. {
  344. int cpu = cpu_dev->id;
  345. struct cpuidle_device *dev;
  346. int error;
  347. dev = per_cpu(cpuidle_devices, cpu);
  348. error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
  349. "cpuidle");
  350. if (!error)
  351. kobject_uevent(&dev->kobj, KOBJ_ADD);
  352. return error;
  353. }
  354. /**
  355. * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
  356. * @dev: the target device
  357. */
  358. void cpuidle_remove_sysfs(struct device *cpu_dev)
  359. {
  360. int cpu = cpu_dev->id;
  361. struct cpuidle_device *dev;
  362. dev = per_cpu(cpuidle_devices, cpu);
  363. kobject_put(&dev->kobj);
  364. }