sysfs.c 9.3 KB

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