sysfs.c 9.6 KB

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