main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * kernel/power/main.c - PM subsystem core functionality.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. */
  10. #include <linux/kobject.h>
  11. #include <linux/string.h>
  12. #include <linux/resume-trace.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/seq_file.h>
  16. #include "power.h"
  17. DEFINE_MUTEX(pm_mutex);
  18. #ifdef CONFIG_PM_SLEEP
  19. /* Routines for PM-transition notifications */
  20. static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
  21. int register_pm_notifier(struct notifier_block *nb)
  22. {
  23. return blocking_notifier_chain_register(&pm_chain_head, nb);
  24. }
  25. EXPORT_SYMBOL_GPL(register_pm_notifier);
  26. int unregister_pm_notifier(struct notifier_block *nb)
  27. {
  28. return blocking_notifier_chain_unregister(&pm_chain_head, nb);
  29. }
  30. EXPORT_SYMBOL_GPL(unregister_pm_notifier);
  31. int pm_notifier_call_chain(unsigned long val)
  32. {
  33. int ret = blocking_notifier_call_chain(&pm_chain_head, val, NULL);
  34. return notifier_to_errno(ret);
  35. }
  36. /* If set, devices may be suspended and resumed asynchronously. */
  37. int pm_async_enabled = 1;
  38. static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
  39. char *buf)
  40. {
  41. return sprintf(buf, "%d\n", pm_async_enabled);
  42. }
  43. static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
  44. const char *buf, size_t n)
  45. {
  46. unsigned long val;
  47. if (strict_strtoul(buf, 10, &val))
  48. return -EINVAL;
  49. if (val > 1)
  50. return -EINVAL;
  51. pm_async_enabled = val;
  52. return n;
  53. }
  54. power_attr(pm_async);
  55. #ifdef CONFIG_PM_DEBUG
  56. int pm_test_level = TEST_NONE;
  57. static const char * const pm_tests[__TEST_AFTER_LAST] = {
  58. [TEST_NONE] = "none",
  59. [TEST_CORE] = "core",
  60. [TEST_CPUS] = "processors",
  61. [TEST_PLATFORM] = "platform",
  62. [TEST_DEVICES] = "devices",
  63. [TEST_FREEZER] = "freezer",
  64. };
  65. static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
  66. char *buf)
  67. {
  68. char *s = buf;
  69. int level;
  70. for (level = TEST_FIRST; level <= TEST_MAX; level++)
  71. if (pm_tests[level]) {
  72. if (level == pm_test_level)
  73. s += sprintf(s, "[%s] ", pm_tests[level]);
  74. else
  75. s += sprintf(s, "%s ", pm_tests[level]);
  76. }
  77. if (s != buf)
  78. /* convert the last space to a newline */
  79. *(s-1) = '\n';
  80. return (s - buf);
  81. }
  82. static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
  83. const char *buf, size_t n)
  84. {
  85. const char * const *s;
  86. int level;
  87. char *p;
  88. int len;
  89. int error = -EINVAL;
  90. p = memchr(buf, '\n', n);
  91. len = p ? p - buf : n;
  92. mutex_lock(&pm_mutex);
  93. level = TEST_FIRST;
  94. for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
  95. if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
  96. pm_test_level = level;
  97. error = 0;
  98. break;
  99. }
  100. mutex_unlock(&pm_mutex);
  101. return error ? error : n;
  102. }
  103. power_attr(pm_test);
  104. #endif /* CONFIG_PM_DEBUG */
  105. #ifdef CONFIG_DEBUG_FS
  106. static char *suspend_step_name(enum suspend_stat_step step)
  107. {
  108. switch (step) {
  109. case SUSPEND_FREEZE:
  110. return "freeze";
  111. case SUSPEND_PREPARE:
  112. return "prepare";
  113. case SUSPEND_SUSPEND:
  114. return "suspend";
  115. case SUSPEND_SUSPEND_NOIRQ:
  116. return "suspend_noirq";
  117. case SUSPEND_RESUME_NOIRQ:
  118. return "resume_noirq";
  119. case SUSPEND_RESUME:
  120. return "resume";
  121. default:
  122. return "";
  123. }
  124. }
  125. static int suspend_stats_show(struct seq_file *s, void *unused)
  126. {
  127. int i, index, last_dev, last_errno, last_step;
  128. last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
  129. last_dev %= REC_FAILED_NUM;
  130. last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
  131. last_errno %= REC_FAILED_NUM;
  132. last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
  133. last_step %= REC_FAILED_NUM;
  134. seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
  135. "%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
  136. "success", suspend_stats.success,
  137. "fail", suspend_stats.fail,
  138. "failed_freeze", suspend_stats.failed_freeze,
  139. "failed_prepare", suspend_stats.failed_prepare,
  140. "failed_suspend", suspend_stats.failed_suspend,
  141. "failed_suspend_noirq",
  142. suspend_stats.failed_suspend_noirq,
  143. "failed_resume", suspend_stats.failed_resume,
  144. "failed_resume_noirq",
  145. suspend_stats.failed_resume_noirq);
  146. seq_printf(s, "failures:\n last_failed_dev:\t%-s\n",
  147. suspend_stats.failed_devs[last_dev]);
  148. for (i = 1; i < REC_FAILED_NUM; i++) {
  149. index = last_dev + REC_FAILED_NUM - i;
  150. index %= REC_FAILED_NUM;
  151. seq_printf(s, "\t\t\t%-s\n",
  152. suspend_stats.failed_devs[index]);
  153. }
  154. seq_printf(s, " last_failed_errno:\t%-d\n",
  155. suspend_stats.errno[last_errno]);
  156. for (i = 1; i < REC_FAILED_NUM; i++) {
  157. index = last_errno + REC_FAILED_NUM - i;
  158. index %= REC_FAILED_NUM;
  159. seq_printf(s, "\t\t\t%-d\n",
  160. suspend_stats.errno[index]);
  161. }
  162. seq_printf(s, " last_failed_step:\t%-s\n",
  163. suspend_step_name(
  164. suspend_stats.failed_steps[last_step]));
  165. for (i = 1; i < REC_FAILED_NUM; i++) {
  166. index = last_step + REC_FAILED_NUM - i;
  167. index %= REC_FAILED_NUM;
  168. seq_printf(s, "\t\t\t%-s\n",
  169. suspend_step_name(
  170. suspend_stats.failed_steps[index]));
  171. }
  172. return 0;
  173. }
  174. static int suspend_stats_open(struct inode *inode, struct file *file)
  175. {
  176. return single_open(file, suspend_stats_show, NULL);
  177. }
  178. static const struct file_operations suspend_stats_operations = {
  179. .open = suspend_stats_open,
  180. .read = seq_read,
  181. .llseek = seq_lseek,
  182. .release = single_release,
  183. };
  184. static int __init pm_debugfs_init(void)
  185. {
  186. debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
  187. NULL, NULL, &suspend_stats_operations);
  188. return 0;
  189. }
  190. late_initcall(pm_debugfs_init);
  191. #endif /* CONFIG_DEBUG_FS */
  192. #endif /* CONFIG_PM_SLEEP */
  193. struct kobject *power_kobj;
  194. /**
  195. * state - control system power state.
  196. *
  197. * show() returns what states are supported, which is hard-coded to
  198. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  199. * 'disk' (Suspend-to-Disk).
  200. *
  201. * store() accepts one of those strings, translates it into the
  202. * proper enumerated value, and initiates a suspend transition.
  203. */
  204. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  205. char *buf)
  206. {
  207. char *s = buf;
  208. #ifdef CONFIG_SUSPEND
  209. int i;
  210. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  211. if (pm_states[i] && valid_state(i))
  212. s += sprintf(s,"%s ", pm_states[i]);
  213. }
  214. #endif
  215. #ifdef CONFIG_HIBERNATION
  216. s += sprintf(s, "%s\n", "disk");
  217. #else
  218. if (s != buf)
  219. /* convert the last space to a newline */
  220. *(s-1) = '\n';
  221. #endif
  222. return (s - buf);
  223. }
  224. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  225. const char *buf, size_t n)
  226. {
  227. #ifdef CONFIG_SUSPEND
  228. suspend_state_t state = PM_SUSPEND_STANDBY;
  229. const char * const *s;
  230. #endif
  231. char *p;
  232. int len;
  233. int error = -EINVAL;
  234. p = memchr(buf, '\n', n);
  235. len = p ? p - buf : n;
  236. /* First, check if we are requested to hibernate */
  237. if (len == 4 && !strncmp(buf, "disk", len)) {
  238. error = hibernate();
  239. goto Exit;
  240. }
  241. #ifdef CONFIG_SUSPEND
  242. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  243. if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
  244. break;
  245. }
  246. if (state < PM_SUSPEND_MAX && *s)
  247. error = enter_state(state);
  248. if (error) {
  249. suspend_stats.fail++;
  250. dpm_save_failed_errno(error);
  251. } else
  252. suspend_stats.success++;
  253. #endif
  254. Exit:
  255. return error ? error : n;
  256. }
  257. power_attr(state);
  258. #ifdef CONFIG_PM_SLEEP
  259. /*
  260. * The 'wakeup_count' attribute, along with the functions defined in
  261. * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
  262. * handled in a non-racy way.
  263. *
  264. * If a wakeup event occurs when the system is in a sleep state, it simply is
  265. * woken up. In turn, if an event that would wake the system up from a sleep
  266. * state occurs when it is undergoing a transition to that sleep state, the
  267. * transition should be aborted. Moreover, if such an event occurs when the
  268. * system is in the working state, an attempt to start a transition to the
  269. * given sleep state should fail during certain period after the detection of
  270. * the event. Using the 'state' attribute alone is not sufficient to satisfy
  271. * these requirements, because a wakeup event may occur exactly when 'state'
  272. * is being written to and may be delivered to user space right before it is
  273. * frozen, so the event will remain only partially processed until the system is
  274. * woken up by another event. In particular, it won't cause the transition to
  275. * a sleep state to be aborted.
  276. *
  277. * This difficulty may be overcome if user space uses 'wakeup_count' before
  278. * writing to 'state'. It first should read from 'wakeup_count' and store
  279. * the read value. Then, after carrying out its own preparations for the system
  280. * transition to a sleep state, it should write the stored value to
  281. * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
  282. * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
  283. * is allowed to write to 'state', but the transition will be aborted if there
  284. * are any wakeup events detected after 'wakeup_count' was written to.
  285. */
  286. static ssize_t wakeup_count_show(struct kobject *kobj,
  287. struct kobj_attribute *attr,
  288. char *buf)
  289. {
  290. unsigned int val;
  291. return pm_get_wakeup_count(&val) ? sprintf(buf, "%u\n", val) : -EINTR;
  292. }
  293. static ssize_t wakeup_count_store(struct kobject *kobj,
  294. struct kobj_attribute *attr,
  295. const char *buf, size_t n)
  296. {
  297. unsigned int val;
  298. if (sscanf(buf, "%u", &val) == 1) {
  299. if (pm_save_wakeup_count(val))
  300. return n;
  301. }
  302. return -EINVAL;
  303. }
  304. power_attr(wakeup_count);
  305. #endif /* CONFIG_PM_SLEEP */
  306. #ifdef CONFIG_PM_TRACE
  307. int pm_trace_enabled;
  308. static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
  309. char *buf)
  310. {
  311. return sprintf(buf, "%d\n", pm_trace_enabled);
  312. }
  313. static ssize_t
  314. pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
  315. const char *buf, size_t n)
  316. {
  317. int val;
  318. if (sscanf(buf, "%d", &val) == 1) {
  319. pm_trace_enabled = !!val;
  320. return n;
  321. }
  322. return -EINVAL;
  323. }
  324. power_attr(pm_trace);
  325. static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
  326. struct kobj_attribute *attr,
  327. char *buf)
  328. {
  329. return show_trace_dev_match(buf, PAGE_SIZE);
  330. }
  331. static ssize_t
  332. pm_trace_dev_match_store(struct kobject *kobj, struct kobj_attribute *attr,
  333. const char *buf, size_t n)
  334. {
  335. return -EINVAL;
  336. }
  337. power_attr(pm_trace_dev_match);
  338. #endif /* CONFIG_PM_TRACE */
  339. static struct attribute * g[] = {
  340. &state_attr.attr,
  341. #ifdef CONFIG_PM_TRACE
  342. &pm_trace_attr.attr,
  343. &pm_trace_dev_match_attr.attr,
  344. #endif
  345. #ifdef CONFIG_PM_SLEEP
  346. &pm_async_attr.attr,
  347. &wakeup_count_attr.attr,
  348. #ifdef CONFIG_PM_DEBUG
  349. &pm_test_attr.attr,
  350. #endif
  351. #endif
  352. NULL,
  353. };
  354. static struct attribute_group attr_group = {
  355. .attrs = g,
  356. };
  357. #ifdef CONFIG_PM_RUNTIME
  358. struct workqueue_struct *pm_wq;
  359. EXPORT_SYMBOL_GPL(pm_wq);
  360. static int __init pm_start_workqueue(void)
  361. {
  362. pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
  363. return pm_wq ? 0 : -ENOMEM;
  364. }
  365. #else
  366. static inline int pm_start_workqueue(void) { return 0; }
  367. #endif
  368. static int __init pm_init(void)
  369. {
  370. int error = pm_start_workqueue();
  371. if (error)
  372. return error;
  373. hibernate_image_size_init();
  374. hibernate_reserved_size_init();
  375. power_kobj = kobject_create_and_add("power", NULL);
  376. if (!power_kobj)
  377. return -ENOMEM;
  378. return sysfs_create_group(power_kobj, &attr_group);
  379. }
  380. core_initcall(pm_init);