sysfs.c 10 KB

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