sysfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 *, \
  183. struct cpuidle_state_usage *, const char *, size_t);
  184. };
  185. #define define_one_state_ro(_name, show) \
  186. static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
  187. #define define_one_state_rw(_name, show, store) \
  188. static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
  189. #define define_show_state_function(_name) \
  190. static ssize_t show_state_##_name(struct cpuidle_state *state, \
  191. struct cpuidle_state_usage *state_usage, char *buf) \
  192. { \
  193. return sprintf(buf, "%u\n", state->_name);\
  194. }
  195. #define define_store_state_ull_function(_name) \
  196. static ssize_t store_state_##_name(struct cpuidle_state *state, \
  197. struct cpuidle_state_usage *state_usage, \
  198. const char *buf, size_t size) \
  199. { \
  200. unsigned long long value; \
  201. int err; \
  202. if (!capable(CAP_SYS_ADMIN)) \
  203. return -EPERM; \
  204. err = kstrtoull(buf, 0, &value); \
  205. if (err) \
  206. return err; \
  207. if (value) \
  208. state_usage->_name = 1; \
  209. else \
  210. state_usage->_name = 0; \
  211. return size; \
  212. }
  213. #define define_show_state_ull_function(_name) \
  214. static ssize_t show_state_##_name(struct cpuidle_state *state, \
  215. struct cpuidle_state_usage *state_usage, char *buf) \
  216. { \
  217. return sprintf(buf, "%llu\n", state_usage->_name);\
  218. }
  219. #define define_show_state_str_function(_name) \
  220. static ssize_t show_state_##_name(struct cpuidle_state *state, \
  221. struct cpuidle_state_usage *state_usage, char *buf) \
  222. { \
  223. if (state->_name[0] == '\0')\
  224. return sprintf(buf, "<null>\n");\
  225. return sprintf(buf, "%s\n", state->_name);\
  226. }
  227. define_show_state_function(exit_latency)
  228. define_show_state_function(power_usage)
  229. define_show_state_ull_function(usage)
  230. define_show_state_ull_function(time)
  231. define_show_state_str_function(name)
  232. define_show_state_str_function(desc)
  233. define_show_state_ull_function(disable)
  234. define_store_state_ull_function(disable)
  235. define_one_state_ro(name, show_state_name);
  236. define_one_state_ro(desc, show_state_desc);
  237. define_one_state_ro(latency, show_state_exit_latency);
  238. define_one_state_ro(power, show_state_power_usage);
  239. define_one_state_ro(usage, show_state_usage);
  240. define_one_state_ro(time, show_state_time);
  241. define_one_state_rw(disable, show_state_disable, store_state_disable);
  242. static struct attribute *cpuidle_state_default_attrs[] = {
  243. &attr_name.attr,
  244. &attr_desc.attr,
  245. &attr_latency.attr,
  246. &attr_power.attr,
  247. &attr_usage.attr,
  248. &attr_time.attr,
  249. &attr_disable.attr,
  250. NULL
  251. };
  252. #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
  253. #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
  254. #define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
  255. #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
  256. static ssize_t cpuidle_state_show(struct kobject * kobj,
  257. struct attribute * attr ,char * buf)
  258. {
  259. int ret = -EIO;
  260. struct cpuidle_state *state = kobj_to_state(kobj);
  261. struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
  262. struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
  263. if (cattr->show)
  264. ret = cattr->show(state, state_usage, buf);
  265. return ret;
  266. }
  267. static ssize_t cpuidle_state_store(struct kobject *kobj,
  268. struct attribute *attr, const char *buf, size_t size)
  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->store)
  275. ret = cattr->store(state, state_usage, buf, size);
  276. return ret;
  277. }
  278. static const struct sysfs_ops cpuidle_state_sysfs_ops = {
  279. .show = cpuidle_state_show,
  280. .store = cpuidle_state_store,
  281. };
  282. static void cpuidle_state_sysfs_release(struct kobject *kobj)
  283. {
  284. struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
  285. complete(&state_obj->kobj_unregister);
  286. }
  287. static struct kobj_type ktype_state_cpuidle = {
  288. .sysfs_ops = &cpuidle_state_sysfs_ops,
  289. .default_attrs = cpuidle_state_default_attrs,
  290. .release = cpuidle_state_sysfs_release,
  291. };
  292. static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
  293. {
  294. kobject_put(&device->kobjs[i]->kobj);
  295. wait_for_completion(&device->kobjs[i]->kobj_unregister);
  296. kfree(device->kobjs[i]);
  297. device->kobjs[i] = NULL;
  298. }
  299. /**
  300. * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
  301. * @device: the target device
  302. */
  303. int cpuidle_add_state_sysfs(struct cpuidle_device *device)
  304. {
  305. int i, ret = -ENOMEM;
  306. struct cpuidle_state_kobj *kobj;
  307. struct cpuidle_driver *drv = cpuidle_get_driver();
  308. /* state statistics */
  309. for (i = 0; i < device->state_count; i++) {
  310. kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
  311. if (!kobj)
  312. goto error_state;
  313. kobj->state = &drv->states[i];
  314. kobj->state_usage = &device->states_usage[i];
  315. init_completion(&kobj->kobj_unregister);
  316. ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
  317. "state%d", i);
  318. if (ret) {
  319. kobject_put(&kobj->kobj);
  320. goto error_state;
  321. }
  322. kobject_uevent(&kobj->kobj, KOBJ_ADD);
  323. device->kobjs[i] = kobj;
  324. }
  325. return 0;
  326. error_state:
  327. for (i = i - 1; i >= 0; i--)
  328. cpuidle_free_state_kobj(device, i);
  329. return ret;
  330. }
  331. /**
  332. * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
  333. * @device: the target device
  334. */
  335. void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
  336. {
  337. int i;
  338. for (i = 0; i < device->state_count; i++)
  339. cpuidle_free_state_kobj(device, i);
  340. }
  341. /**
  342. * cpuidle_add_sysfs - creates a sysfs instance for the target device
  343. * @dev: the target device
  344. */
  345. int cpuidle_add_sysfs(struct device *cpu_dev)
  346. {
  347. int cpu = cpu_dev->id;
  348. struct cpuidle_device *dev;
  349. int error;
  350. dev = per_cpu(cpuidle_devices, cpu);
  351. error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
  352. "cpuidle");
  353. if (!error)
  354. kobject_uevent(&dev->kobj, KOBJ_ADD);
  355. return error;
  356. }
  357. /**
  358. * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
  359. * @dev: the target device
  360. */
  361. void cpuidle_remove_sysfs(struct device *cpu_dev)
  362. {
  363. int cpu = cpu_dev->id;
  364. struct cpuidle_device *dev;
  365. dev = per_cpu(cpuidle_devices, cpu);
  366. kobject_put(&dev->kobj);
  367. }