sysfs.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "cpuidle.h"
  14. static unsigned int sysfs_switch;
  15. static int __init cpuidle_sysfs_setup(char *unused)
  16. {
  17. sysfs_switch = 1;
  18. return 1;
  19. }
  20. __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
  21. static ssize_t show_available_governors(struct sysdev_class *class,
  22. struct sysdev_class_attribute *attr,
  23. char *buf)
  24. {
  25. ssize_t i = 0;
  26. struct cpuidle_governor *tmp;
  27. mutex_lock(&cpuidle_lock);
  28. list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
  29. if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
  30. goto out;
  31. i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
  32. }
  33. out:
  34. i+= sprintf(&buf[i], "\n");
  35. mutex_unlock(&cpuidle_lock);
  36. return i;
  37. }
  38. static ssize_t show_current_driver(struct sysdev_class *class,
  39. struct sysdev_class_attribute *attr,
  40. char *buf)
  41. {
  42. ssize_t ret;
  43. spin_lock(&cpuidle_driver_lock);
  44. if (cpuidle_curr_driver)
  45. ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name);
  46. else
  47. ret = sprintf(buf, "none\n");
  48. spin_unlock(&cpuidle_driver_lock);
  49. return ret;
  50. }
  51. static ssize_t show_current_governor(struct sysdev_class *class,
  52. struct sysdev_class_attribute *attr,
  53. char *buf)
  54. {
  55. ssize_t ret;
  56. mutex_lock(&cpuidle_lock);
  57. if (cpuidle_curr_governor)
  58. ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
  59. else
  60. ret = sprintf(buf, "none\n");
  61. mutex_unlock(&cpuidle_lock);
  62. return ret;
  63. }
  64. static ssize_t store_current_governor(struct sysdev_class *class,
  65. struct sysdev_class_attribute *attr,
  66. const char *buf, size_t count)
  67. {
  68. char gov_name[CPUIDLE_NAME_LEN];
  69. int ret = -EINVAL;
  70. size_t len = count;
  71. struct cpuidle_governor *gov;
  72. if (!len || len >= sizeof(gov_name))
  73. return -EINVAL;
  74. memcpy(gov_name, buf, len);
  75. gov_name[len] = '\0';
  76. if (gov_name[len - 1] == '\n')
  77. gov_name[--len] = '\0';
  78. mutex_lock(&cpuidle_lock);
  79. list_for_each_entry(gov, &cpuidle_governors, governor_list) {
  80. if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
  81. ret = cpuidle_switch_governor(gov);
  82. break;
  83. }
  84. }
  85. mutex_unlock(&cpuidle_lock);
  86. if (ret)
  87. return ret;
  88. else
  89. return count;
  90. }
  91. static SYSDEV_CLASS_ATTR(current_driver, 0444, show_current_driver, NULL);
  92. static SYSDEV_CLASS_ATTR(current_governor_ro, 0444, show_current_governor,
  93. NULL);
  94. static struct attribute *cpuclass_default_attrs[] = {
  95. &attr_current_driver.attr,
  96. &attr_current_governor_ro.attr,
  97. NULL
  98. };
  99. static SYSDEV_CLASS_ATTR(available_governors, 0444, show_available_governors,
  100. NULL);
  101. static SYSDEV_CLASS_ATTR(current_governor, 0644, show_current_governor,
  102. store_current_governor);
  103. static struct attribute *cpuclass_switch_attrs[] = {
  104. &attr_available_governors.attr,
  105. &attr_current_driver.attr,
  106. &attr_current_governor.attr,
  107. NULL
  108. };
  109. static struct attribute_group cpuclass_attr_group = {
  110. .attrs = cpuclass_default_attrs,
  111. .name = "cpuidle",
  112. };
  113. /**
  114. * cpuidle_add_class_sysfs - add CPU global sysfs attributes
  115. */
  116. int cpuidle_add_class_sysfs(struct sysdev_class *cls)
  117. {
  118. if (sysfs_switch)
  119. cpuclass_attr_group.attrs = cpuclass_switch_attrs;
  120. return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group);
  121. }
  122. /**
  123. * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes
  124. */
  125. void cpuidle_remove_class_sysfs(struct sysdev_class *cls)
  126. {
  127. sysfs_remove_group(&cls->kset.kobj, &cpuclass_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 *, char *);
  181. ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
  182. };
  183. #define define_one_state_ro(_name, show) \
  184. static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
  185. #define define_show_state_function(_name) \
  186. static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
  187. { \
  188. return sprintf(buf, "%u\n", state->_name);\
  189. }
  190. #define define_show_state_ull_function(_name) \
  191. static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
  192. { \
  193. return sprintf(buf, "%llu\n", state->_name);\
  194. }
  195. #define define_show_state_str_function(_name) \
  196. static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
  197. { \
  198. if (state->_name[0] == '\0')\
  199. return sprintf(buf, "<null>\n");\
  200. return sprintf(buf, "%s\n", state->_name);\
  201. }
  202. define_show_state_function(exit_latency)
  203. define_show_state_function(power_usage)
  204. define_show_state_ull_function(usage)
  205. define_show_state_ull_function(time)
  206. define_show_state_str_function(name)
  207. define_show_state_str_function(desc)
  208. define_one_state_ro(name, show_state_name);
  209. define_one_state_ro(desc, show_state_desc);
  210. define_one_state_ro(latency, show_state_exit_latency);
  211. define_one_state_ro(power, show_state_power_usage);
  212. define_one_state_ro(usage, show_state_usage);
  213. define_one_state_ro(time, show_state_time);
  214. static struct attribute *cpuidle_state_default_attrs[] = {
  215. &attr_name.attr,
  216. &attr_desc.attr,
  217. &attr_latency.attr,
  218. &attr_power.attr,
  219. &attr_usage.attr,
  220. &attr_time.attr,
  221. NULL
  222. };
  223. #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
  224. #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
  225. #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
  226. static ssize_t cpuidle_state_show(struct kobject * kobj,
  227. struct attribute * attr ,char * buf)
  228. {
  229. int ret = -EIO;
  230. struct cpuidle_state *state = kobj_to_state(kobj);
  231. struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
  232. if (cattr->show)
  233. ret = cattr->show(state, buf);
  234. return ret;
  235. }
  236. static const struct sysfs_ops cpuidle_state_sysfs_ops = {
  237. .show = cpuidle_state_show,
  238. };
  239. static void cpuidle_state_sysfs_release(struct kobject *kobj)
  240. {
  241. struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
  242. complete(&state_obj->kobj_unregister);
  243. }
  244. static struct kobj_type ktype_state_cpuidle = {
  245. .sysfs_ops = &cpuidle_state_sysfs_ops,
  246. .default_attrs = cpuidle_state_default_attrs,
  247. .release = cpuidle_state_sysfs_release,
  248. };
  249. static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
  250. {
  251. kobject_put(&device->kobjs[i]->kobj);
  252. wait_for_completion(&device->kobjs[i]->kobj_unregister);
  253. kfree(device->kobjs[i]);
  254. device->kobjs[i] = NULL;
  255. }
  256. /**
  257. * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
  258. * @device: the target device
  259. */
  260. int cpuidle_add_state_sysfs(struct cpuidle_device *device)
  261. {
  262. int i, ret = -ENOMEM;
  263. struct cpuidle_state_kobj *kobj;
  264. /* state statistics */
  265. for (i = 0; i < device->state_count; i++) {
  266. kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
  267. if (!kobj)
  268. goto error_state;
  269. kobj->state = &device->states[i];
  270. init_completion(&kobj->kobj_unregister);
  271. ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
  272. "state%d", i);
  273. if (ret) {
  274. kfree(kobj);
  275. goto error_state;
  276. }
  277. kobject_uevent(&kobj->kobj, KOBJ_ADD);
  278. device->kobjs[i] = kobj;
  279. }
  280. return 0;
  281. error_state:
  282. for (i = i - 1; i >= 0; i--)
  283. cpuidle_free_state_kobj(device, i);
  284. return ret;
  285. }
  286. /**
  287. * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
  288. * @device: the target device
  289. */
  290. void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
  291. {
  292. int i;
  293. for (i = 0; i < device->state_count; i++)
  294. cpuidle_free_state_kobj(device, i);
  295. }
  296. /**
  297. * cpuidle_add_sysfs - creates a sysfs instance for the target device
  298. * @sysdev: the target device
  299. */
  300. int cpuidle_add_sysfs(struct sys_device *sysdev)
  301. {
  302. int cpu = sysdev->id;
  303. struct cpuidle_device *dev;
  304. int error;
  305. dev = per_cpu(cpuidle_devices, cpu);
  306. error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
  307. "cpuidle");
  308. if (!error)
  309. kobject_uevent(&dev->kobj, KOBJ_ADD);
  310. return error;
  311. }
  312. /**
  313. * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
  314. * @sysdev: the target device
  315. */
  316. void cpuidle_remove_sysfs(struct sys_device *sysdev)
  317. {
  318. int cpu = sysdev->id;
  319. struct cpuidle_device *dev;
  320. dev = per_cpu(cpuidle_devices, cpu);
  321. kobject_put(&dev->kobj);
  322. }