main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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/export.h>
  11. #include <linux/kobject.h>
  12. #include <linux/string.h>
  13. #include <linux/resume-trace.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/seq_file.h>
  17. #include "power.h"
  18. DEFINE_MUTEX(pm_mutex);
  19. #ifdef CONFIG_PM_SLEEP
  20. /* Routines for PM-transition notifications */
  21. static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
  22. int register_pm_notifier(struct notifier_block *nb)
  23. {
  24. return blocking_notifier_chain_register(&pm_chain_head, nb);
  25. }
  26. EXPORT_SYMBOL_GPL(register_pm_notifier);
  27. int unregister_pm_notifier(struct notifier_block *nb)
  28. {
  29. return blocking_notifier_chain_unregister(&pm_chain_head, nb);
  30. }
  31. EXPORT_SYMBOL_GPL(unregister_pm_notifier);
  32. int pm_notifier_call_chain(unsigned long val)
  33. {
  34. int ret = blocking_notifier_call_chain(&pm_chain_head, val, NULL);
  35. return notifier_to_errno(ret);
  36. }
  37. /* If set, devices may be suspended and resumed asynchronously. */
  38. int pm_async_enabled = 1;
  39. static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
  40. char *buf)
  41. {
  42. return sprintf(buf, "%d\n", pm_async_enabled);
  43. }
  44. static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
  45. const char *buf, size_t n)
  46. {
  47. unsigned long val;
  48. if (strict_strtoul(buf, 10, &val))
  49. return -EINVAL;
  50. if (val > 1)
  51. return -EINVAL;
  52. pm_async_enabled = val;
  53. return n;
  54. }
  55. power_attr(pm_async);
  56. #ifdef CONFIG_PM_DEBUG
  57. int pm_test_level = TEST_NONE;
  58. static const char * const pm_tests[__TEST_AFTER_LAST] = {
  59. [TEST_NONE] = "none",
  60. [TEST_CORE] = "core",
  61. [TEST_CPUS] = "processors",
  62. [TEST_PLATFORM] = "platform",
  63. [TEST_DEVICES] = "devices",
  64. [TEST_FREEZER] = "freezer",
  65. };
  66. static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
  67. char *buf)
  68. {
  69. char *s = buf;
  70. int level;
  71. for (level = TEST_FIRST; level <= TEST_MAX; level++)
  72. if (pm_tests[level]) {
  73. if (level == pm_test_level)
  74. s += sprintf(s, "[%s] ", pm_tests[level]);
  75. else
  76. s += sprintf(s, "%s ", pm_tests[level]);
  77. }
  78. if (s != buf)
  79. /* convert the last space to a newline */
  80. *(s-1) = '\n';
  81. return (s - buf);
  82. }
  83. static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
  84. const char *buf, size_t n)
  85. {
  86. const char * const *s;
  87. int level;
  88. char *p;
  89. int len;
  90. int error = -EINVAL;
  91. p = memchr(buf, '\n', n);
  92. len = p ? p - buf : n;
  93. lock_system_sleep();
  94. level = TEST_FIRST;
  95. for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
  96. if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
  97. pm_test_level = level;
  98. error = 0;
  99. break;
  100. }
  101. unlock_system_sleep();
  102. return error ? error : n;
  103. }
  104. power_attr(pm_test);
  105. /*
  106. * pm_print_times: print time taken by devices to suspend and resume.
  107. *
  108. * show() returns whether printing of suspend and resume times is enabled.
  109. * store() accepts 0 or 1. 0 disables printing and 1 enables it.
  110. */
  111. int pm_print_times_enabled;
  112. static ssize_t pm_print_times_show(struct kobject *kobj,
  113. struct kobj_attribute *attr, char *buf)
  114. {
  115. return sprintf(buf, "%d\n", pm_print_times_enabled);
  116. }
  117. static ssize_t pm_print_times_store(struct kobject *kobj,
  118. struct kobj_attribute *attr,
  119. const char *buf, size_t n)
  120. {
  121. unsigned long val;
  122. if (kstrtoul(buf, 10, &val))
  123. return -EINVAL;
  124. if (val > 1)
  125. return -EINVAL;
  126. pm_print_times_enabled = val;
  127. return n;
  128. }
  129. power_attr(pm_print_times);
  130. #endif /* CONFIG_PM_DEBUG */
  131. #ifdef CONFIG_DEBUG_FS
  132. static char *suspend_step_name(enum suspend_stat_step step)
  133. {
  134. switch (step) {
  135. case SUSPEND_FREEZE:
  136. return "freeze";
  137. case SUSPEND_PREPARE:
  138. return "prepare";
  139. case SUSPEND_SUSPEND:
  140. return "suspend";
  141. case SUSPEND_SUSPEND_NOIRQ:
  142. return "suspend_noirq";
  143. case SUSPEND_RESUME_NOIRQ:
  144. return "resume_noirq";
  145. case SUSPEND_RESUME:
  146. return "resume";
  147. default:
  148. return "";
  149. }
  150. }
  151. static int suspend_stats_show(struct seq_file *s, void *unused)
  152. {
  153. int i, index, last_dev, last_errno, last_step;
  154. last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
  155. last_dev %= REC_FAILED_NUM;
  156. last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
  157. last_errno %= REC_FAILED_NUM;
  158. last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
  159. last_step %= REC_FAILED_NUM;
  160. seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
  161. "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
  162. "success", suspend_stats.success,
  163. "fail", suspend_stats.fail,
  164. "failed_freeze", suspend_stats.failed_freeze,
  165. "failed_prepare", suspend_stats.failed_prepare,
  166. "failed_suspend", suspend_stats.failed_suspend,
  167. "failed_suspend_late",
  168. suspend_stats.failed_suspend_late,
  169. "failed_suspend_noirq",
  170. suspend_stats.failed_suspend_noirq,
  171. "failed_resume", suspend_stats.failed_resume,
  172. "failed_resume_early",
  173. suspend_stats.failed_resume_early,
  174. "failed_resume_noirq",
  175. suspend_stats.failed_resume_noirq);
  176. seq_printf(s, "failures:\n last_failed_dev:\t%-s\n",
  177. suspend_stats.failed_devs[last_dev]);
  178. for (i = 1; i < REC_FAILED_NUM; i++) {
  179. index = last_dev + REC_FAILED_NUM - i;
  180. index %= REC_FAILED_NUM;
  181. seq_printf(s, "\t\t\t%-s\n",
  182. suspend_stats.failed_devs[index]);
  183. }
  184. seq_printf(s, " last_failed_errno:\t%-d\n",
  185. suspend_stats.errno[last_errno]);
  186. for (i = 1; i < REC_FAILED_NUM; i++) {
  187. index = last_errno + REC_FAILED_NUM - i;
  188. index %= REC_FAILED_NUM;
  189. seq_printf(s, "\t\t\t%-d\n",
  190. suspend_stats.errno[index]);
  191. }
  192. seq_printf(s, " last_failed_step:\t%-s\n",
  193. suspend_step_name(
  194. suspend_stats.failed_steps[last_step]));
  195. for (i = 1; i < REC_FAILED_NUM; i++) {
  196. index = last_step + REC_FAILED_NUM - i;
  197. index %= REC_FAILED_NUM;
  198. seq_printf(s, "\t\t\t%-s\n",
  199. suspend_step_name(
  200. suspend_stats.failed_steps[index]));
  201. }
  202. return 0;
  203. }
  204. static int suspend_stats_open(struct inode *inode, struct file *file)
  205. {
  206. return single_open(file, suspend_stats_show, NULL);
  207. }
  208. static const struct file_operations suspend_stats_operations = {
  209. .open = suspend_stats_open,
  210. .read = seq_read,
  211. .llseek = seq_lseek,
  212. .release = single_release,
  213. };
  214. static int __init pm_debugfs_init(void)
  215. {
  216. debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
  217. NULL, NULL, &suspend_stats_operations);
  218. return 0;
  219. }
  220. late_initcall(pm_debugfs_init);
  221. #endif /* CONFIG_DEBUG_FS */
  222. #endif /* CONFIG_PM_SLEEP */
  223. struct kobject *power_kobj;
  224. /**
  225. * state - control system power state.
  226. *
  227. * show() returns what states are supported, which is hard-coded to
  228. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  229. * 'disk' (Suspend-to-Disk).
  230. *
  231. * store() accepts one of those strings, translates it into the
  232. * proper enumerated value, and initiates a suspend transition.
  233. */
  234. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  235. char *buf)
  236. {
  237. char *s = buf;
  238. #ifdef CONFIG_SUSPEND
  239. int i;
  240. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  241. if (pm_states[i] && valid_state(i))
  242. s += sprintf(s,"%s ", pm_states[i]);
  243. }
  244. #endif
  245. #ifdef CONFIG_HIBERNATION
  246. s += sprintf(s, "%s\n", "disk");
  247. #else
  248. if (s != buf)
  249. /* convert the last space to a newline */
  250. *(s-1) = '\n';
  251. #endif
  252. return (s - buf);
  253. }
  254. static suspend_state_t decode_state(const char *buf, size_t n)
  255. {
  256. #ifdef CONFIG_SUSPEND
  257. suspend_state_t state = PM_SUSPEND_STANDBY;
  258. const char * const *s;
  259. #endif
  260. char *p;
  261. int len;
  262. p = memchr(buf, '\n', n);
  263. len = p ? p - buf : n;
  264. /* Check hibernation first. */
  265. if (len == 4 && !strncmp(buf, "disk", len))
  266. return PM_SUSPEND_MAX;
  267. #ifdef CONFIG_SUSPEND
  268. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++)
  269. if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
  270. return state;
  271. #endif
  272. return PM_SUSPEND_ON;
  273. }
  274. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  275. const char *buf, size_t n)
  276. {
  277. suspend_state_t state;
  278. int error;
  279. error = pm_autosleep_lock();
  280. if (error)
  281. return error;
  282. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  283. error = -EBUSY;
  284. goto out;
  285. }
  286. state = decode_state(buf, n);
  287. if (state < PM_SUSPEND_MAX)
  288. error = pm_suspend(state);
  289. else if (state == PM_SUSPEND_MAX)
  290. error = hibernate();
  291. else
  292. error = -EINVAL;
  293. out:
  294. pm_autosleep_unlock();
  295. return error ? error : n;
  296. }
  297. power_attr(state);
  298. #ifdef CONFIG_PM_SLEEP
  299. /*
  300. * The 'wakeup_count' attribute, along with the functions defined in
  301. * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
  302. * handled in a non-racy way.
  303. *
  304. * If a wakeup event occurs when the system is in a sleep state, it simply is
  305. * woken up. In turn, if an event that would wake the system up from a sleep
  306. * state occurs when it is undergoing a transition to that sleep state, the
  307. * transition should be aborted. Moreover, if such an event occurs when the
  308. * system is in the working state, an attempt to start a transition to the
  309. * given sleep state should fail during certain period after the detection of
  310. * the event. Using the 'state' attribute alone is not sufficient to satisfy
  311. * these requirements, because a wakeup event may occur exactly when 'state'
  312. * is being written to and may be delivered to user space right before it is
  313. * frozen, so the event will remain only partially processed until the system is
  314. * woken up by another event. In particular, it won't cause the transition to
  315. * a sleep state to be aborted.
  316. *
  317. * This difficulty may be overcome if user space uses 'wakeup_count' before
  318. * writing to 'state'. It first should read from 'wakeup_count' and store
  319. * the read value. Then, after carrying out its own preparations for the system
  320. * transition to a sleep state, it should write the stored value to
  321. * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
  322. * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
  323. * is allowed to write to 'state', but the transition will be aborted if there
  324. * are any wakeup events detected after 'wakeup_count' was written to.
  325. */
  326. static ssize_t wakeup_count_show(struct kobject *kobj,
  327. struct kobj_attribute *attr,
  328. char *buf)
  329. {
  330. unsigned int val;
  331. return pm_get_wakeup_count(&val, true) ?
  332. sprintf(buf, "%u\n", val) : -EINTR;
  333. }
  334. static ssize_t wakeup_count_store(struct kobject *kobj,
  335. struct kobj_attribute *attr,
  336. const char *buf, size_t n)
  337. {
  338. unsigned int val;
  339. int error;
  340. error = pm_autosleep_lock();
  341. if (error)
  342. return error;
  343. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  344. error = -EBUSY;
  345. goto out;
  346. }
  347. error = -EINVAL;
  348. if (sscanf(buf, "%u", &val) == 1) {
  349. if (pm_save_wakeup_count(val))
  350. error = n;
  351. }
  352. out:
  353. pm_autosleep_unlock();
  354. return error;
  355. }
  356. power_attr(wakeup_count);
  357. #ifdef CONFIG_PM_AUTOSLEEP
  358. static ssize_t autosleep_show(struct kobject *kobj,
  359. struct kobj_attribute *attr,
  360. char *buf)
  361. {
  362. suspend_state_t state = pm_autosleep_state();
  363. if (state == PM_SUSPEND_ON)
  364. return sprintf(buf, "off\n");
  365. #ifdef CONFIG_SUSPEND
  366. if (state < PM_SUSPEND_MAX)
  367. return sprintf(buf, "%s\n", valid_state(state) ?
  368. pm_states[state] : "error");
  369. #endif
  370. #ifdef CONFIG_HIBERNATION
  371. return sprintf(buf, "disk\n");
  372. #else
  373. return sprintf(buf, "error");
  374. #endif
  375. }
  376. static ssize_t autosleep_store(struct kobject *kobj,
  377. struct kobj_attribute *attr,
  378. const char *buf, size_t n)
  379. {
  380. suspend_state_t state = decode_state(buf, n);
  381. int error;
  382. if (state == PM_SUSPEND_ON
  383. && strcmp(buf, "off") && strcmp(buf, "off\n"))
  384. return -EINVAL;
  385. error = pm_autosleep_set_state(state);
  386. return error ? error : n;
  387. }
  388. power_attr(autosleep);
  389. #endif /* CONFIG_PM_AUTOSLEEP */
  390. #ifdef CONFIG_PM_WAKELOCKS
  391. static ssize_t wake_lock_show(struct kobject *kobj,
  392. struct kobj_attribute *attr,
  393. char *buf)
  394. {
  395. return pm_show_wakelocks(buf, true);
  396. }
  397. static ssize_t wake_lock_store(struct kobject *kobj,
  398. struct kobj_attribute *attr,
  399. const char *buf, size_t n)
  400. {
  401. int error = pm_wake_lock(buf);
  402. return error ? error : n;
  403. }
  404. power_attr(wake_lock);
  405. static ssize_t wake_unlock_show(struct kobject *kobj,
  406. struct kobj_attribute *attr,
  407. char *buf)
  408. {
  409. return pm_show_wakelocks(buf, false);
  410. }
  411. static ssize_t wake_unlock_store(struct kobject *kobj,
  412. struct kobj_attribute *attr,
  413. const char *buf, size_t n)
  414. {
  415. int error = pm_wake_unlock(buf);
  416. return error ? error : n;
  417. }
  418. power_attr(wake_unlock);
  419. #endif /* CONFIG_PM_WAKELOCKS */
  420. #endif /* CONFIG_PM_SLEEP */
  421. #ifdef CONFIG_PM_TRACE
  422. int pm_trace_enabled;
  423. static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
  424. char *buf)
  425. {
  426. return sprintf(buf, "%d\n", pm_trace_enabled);
  427. }
  428. static ssize_t
  429. pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
  430. const char *buf, size_t n)
  431. {
  432. int val;
  433. if (sscanf(buf, "%d", &val) == 1) {
  434. pm_trace_enabled = !!val;
  435. return n;
  436. }
  437. return -EINVAL;
  438. }
  439. power_attr(pm_trace);
  440. static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
  441. struct kobj_attribute *attr,
  442. char *buf)
  443. {
  444. return show_trace_dev_match(buf, PAGE_SIZE);
  445. }
  446. static ssize_t
  447. pm_trace_dev_match_store(struct kobject *kobj, struct kobj_attribute *attr,
  448. const char *buf, size_t n)
  449. {
  450. return -EINVAL;
  451. }
  452. power_attr(pm_trace_dev_match);
  453. #endif /* CONFIG_PM_TRACE */
  454. static struct attribute * g[] = {
  455. &state_attr.attr,
  456. #ifdef CONFIG_PM_TRACE
  457. &pm_trace_attr.attr,
  458. &pm_trace_dev_match_attr.attr,
  459. #endif
  460. #ifdef CONFIG_PM_SLEEP
  461. &pm_async_attr.attr,
  462. &wakeup_count_attr.attr,
  463. #ifdef CONFIG_PM_AUTOSLEEP
  464. &autosleep_attr.attr,
  465. #endif
  466. #ifdef CONFIG_PM_WAKELOCKS
  467. &wake_lock_attr.attr,
  468. &wake_unlock_attr.attr,
  469. #endif
  470. #ifdef CONFIG_PM_DEBUG
  471. &pm_test_attr.attr,
  472. &pm_print_times_attr.attr,
  473. #endif
  474. #endif
  475. NULL,
  476. };
  477. static struct attribute_group attr_group = {
  478. .attrs = g,
  479. };
  480. #ifdef CONFIG_PM_RUNTIME
  481. struct workqueue_struct *pm_wq;
  482. EXPORT_SYMBOL_GPL(pm_wq);
  483. static int __init pm_start_workqueue(void)
  484. {
  485. pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
  486. return pm_wq ? 0 : -ENOMEM;
  487. }
  488. #else
  489. static inline int pm_start_workqueue(void) { return 0; }
  490. #endif
  491. static int __init pm_init(void)
  492. {
  493. int error = pm_start_workqueue();
  494. if (error)
  495. return error;
  496. hibernate_image_size_init();
  497. hibernate_reserved_size_init();
  498. power_kobj = kobject_create_and_add("power", NULL);
  499. if (!power_kobj)
  500. return -ENOMEM;
  501. error = sysfs_create_group(power_kobj, &attr_group);
  502. if (error)
  503. return error;
  504. return pm_autosleep_init();
  505. }
  506. core_initcall(pm_init);