main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kernel/power/main.c - PM subsystem core functionality.
  4. *
  5. * Copyright (c) 2003 Patrick Mochel
  6. * Copyright (c) 2003 Open Source Development Lab
  7. */
  8. #include <linux/export.h>
  9. #include <linux/kobject.h>
  10. #include <linux/string.h>
  11. #include <linux/pm-trace.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/suspend.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/pm_runtime.h>
  18. #include "power.h"
  19. #ifdef CONFIG_PM_SLEEP
  20. void lock_system_sleep(void)
  21. {
  22. current->flags |= PF_FREEZER_SKIP;
  23. mutex_lock(&system_transition_mutex);
  24. }
  25. EXPORT_SYMBOL_GPL(lock_system_sleep);
  26. void unlock_system_sleep(void)
  27. {
  28. /*
  29. * Don't use freezer_count() because we don't want the call to
  30. * try_to_freeze() here.
  31. *
  32. * Reason:
  33. * Fundamentally, we just don't need it, because freezing condition
  34. * doesn't come into effect until we release the
  35. * system_transition_mutex lock, since the freezer always works with
  36. * system_transition_mutex held.
  37. *
  38. * More importantly, in the case of hibernation,
  39. * unlock_system_sleep() gets called in snapshot_read() and
  40. * snapshot_write() when the freezing condition is still in effect.
  41. * Which means, if we use try_to_freeze() here, it would make them
  42. * enter the refrigerator, thus causing hibernation to lockup.
  43. */
  44. current->flags &= ~PF_FREEZER_SKIP;
  45. mutex_unlock(&system_transition_mutex);
  46. }
  47. EXPORT_SYMBOL_GPL(unlock_system_sleep);
  48. void ksys_sync_helper(void)
  49. {
  50. ktime_t start;
  51. long elapsed_msecs;
  52. start = ktime_get();
  53. ksys_sync();
  54. elapsed_msecs = ktime_to_ms(ktime_sub(ktime_get(), start));
  55. pr_info("Filesystems sync: %ld.%03ld seconds\n",
  56. elapsed_msecs / MSEC_PER_SEC, elapsed_msecs % MSEC_PER_SEC);
  57. }
  58. EXPORT_SYMBOL_GPL(ksys_sync_helper);
  59. /* Routines for PM-transition notifications */
  60. static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
  61. int register_pm_notifier(struct notifier_block *nb)
  62. {
  63. return blocking_notifier_chain_register(&pm_chain_head, nb);
  64. }
  65. EXPORT_SYMBOL_GPL(register_pm_notifier);
  66. int unregister_pm_notifier(struct notifier_block *nb)
  67. {
  68. return blocking_notifier_chain_unregister(&pm_chain_head, nb);
  69. }
  70. EXPORT_SYMBOL_GPL(unregister_pm_notifier);
  71. int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down)
  72. {
  73. int ret;
  74. ret = blocking_notifier_call_chain_robust(&pm_chain_head, val_up, val_down, NULL);
  75. return notifier_to_errno(ret);
  76. }
  77. int pm_notifier_call_chain(unsigned long val)
  78. {
  79. return blocking_notifier_call_chain(&pm_chain_head, val, NULL);
  80. }
  81. /* If set, devices may be suspended and resumed asynchronously. */
  82. int pm_async_enabled = 1;
  83. static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
  84. char *buf)
  85. {
  86. return sprintf(buf, "%d\n", pm_async_enabled);
  87. }
  88. static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
  89. const char *buf, size_t n)
  90. {
  91. unsigned long val;
  92. if (kstrtoul(buf, 10, &val))
  93. return -EINVAL;
  94. if (val > 1)
  95. return -EINVAL;
  96. pm_async_enabled = val;
  97. return n;
  98. }
  99. power_attr(pm_async);
  100. #ifdef CONFIG_SUSPEND
  101. static ssize_t mem_sleep_show(struct kobject *kobj, struct kobj_attribute *attr,
  102. char *buf)
  103. {
  104. char *s = buf;
  105. suspend_state_t i;
  106. for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
  107. if (mem_sleep_states[i]) {
  108. const char *label = mem_sleep_states[i];
  109. if (mem_sleep_current == i)
  110. s += sprintf(s, "[%s] ", label);
  111. else
  112. s += sprintf(s, "%s ", label);
  113. }
  114. /* Convert the last space to a newline if needed. */
  115. if (s != buf)
  116. *(s-1) = '\n';
  117. return (s - buf);
  118. }
  119. static suspend_state_t decode_suspend_state(const char *buf, size_t n)
  120. {
  121. suspend_state_t state;
  122. char *p;
  123. int len;
  124. p = memchr(buf, '\n', n);
  125. len = p ? p - buf : n;
  126. for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
  127. const char *label = mem_sleep_states[state];
  128. if (label && len == strlen(label) && !strncmp(buf, label, len))
  129. return state;
  130. }
  131. return PM_SUSPEND_ON;
  132. }
  133. static ssize_t mem_sleep_store(struct kobject *kobj, struct kobj_attribute *attr,
  134. const char *buf, size_t n)
  135. {
  136. suspend_state_t state;
  137. int error;
  138. error = pm_autosleep_lock();
  139. if (error)
  140. return error;
  141. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  142. error = -EBUSY;
  143. goto out;
  144. }
  145. state = decode_suspend_state(buf, n);
  146. if (state < PM_SUSPEND_MAX && state > PM_SUSPEND_ON)
  147. mem_sleep_current = state;
  148. else
  149. error = -EINVAL;
  150. out:
  151. pm_autosleep_unlock();
  152. return error ? error : n;
  153. }
  154. power_attr(mem_sleep);
  155. /*
  156. * sync_on_suspend: invoke ksys_sync_helper() before suspend.
  157. *
  158. * show() returns whether ksys_sync_helper() is invoked before suspend.
  159. * store() accepts 0 or 1. 0 disables ksys_sync_helper() and 1 enables it.
  160. */
  161. bool sync_on_suspend_enabled = !IS_ENABLED(CONFIG_SUSPEND_SKIP_SYNC);
  162. static ssize_t sync_on_suspend_show(struct kobject *kobj,
  163. struct kobj_attribute *attr, char *buf)
  164. {
  165. return sprintf(buf, "%d\n", sync_on_suspend_enabled);
  166. }
  167. static ssize_t sync_on_suspend_store(struct kobject *kobj,
  168. struct kobj_attribute *attr,
  169. const char *buf, size_t n)
  170. {
  171. unsigned long val;
  172. if (kstrtoul(buf, 10, &val))
  173. return -EINVAL;
  174. if (val > 1)
  175. return -EINVAL;
  176. sync_on_suspend_enabled = !!val;
  177. return n;
  178. }
  179. power_attr(sync_on_suspend);
  180. #endif /* CONFIG_SUSPEND */
  181. #ifdef CONFIG_PM_SLEEP_DEBUG
  182. int pm_test_level = TEST_NONE;
  183. static const char * const pm_tests[__TEST_AFTER_LAST] = {
  184. [TEST_NONE] = "none",
  185. [TEST_CORE] = "core",
  186. [TEST_CPUS] = "processors",
  187. [TEST_PLATFORM] = "platform",
  188. [TEST_DEVICES] = "devices",
  189. [TEST_FREEZER] = "freezer",
  190. };
  191. static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
  192. char *buf)
  193. {
  194. char *s = buf;
  195. int level;
  196. for (level = TEST_FIRST; level <= TEST_MAX; level++)
  197. if (pm_tests[level]) {
  198. if (level == pm_test_level)
  199. s += sprintf(s, "[%s] ", pm_tests[level]);
  200. else
  201. s += sprintf(s, "%s ", pm_tests[level]);
  202. }
  203. if (s != buf)
  204. /* convert the last space to a newline */
  205. *(s-1) = '\n';
  206. return (s - buf);
  207. }
  208. static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
  209. const char *buf, size_t n)
  210. {
  211. const char * const *s;
  212. int level;
  213. char *p;
  214. int len;
  215. int error = -EINVAL;
  216. p = memchr(buf, '\n', n);
  217. len = p ? p - buf : n;
  218. lock_system_sleep();
  219. level = TEST_FIRST;
  220. for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
  221. if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
  222. pm_test_level = level;
  223. error = 0;
  224. break;
  225. }
  226. unlock_system_sleep();
  227. return error ? error : n;
  228. }
  229. power_attr(pm_test);
  230. #endif /* CONFIG_PM_SLEEP_DEBUG */
  231. static char *suspend_step_name(enum suspend_stat_step step)
  232. {
  233. switch (step) {
  234. case SUSPEND_FREEZE:
  235. return "freeze";
  236. case SUSPEND_PREPARE:
  237. return "prepare";
  238. case SUSPEND_SUSPEND:
  239. return "suspend";
  240. case SUSPEND_SUSPEND_NOIRQ:
  241. return "suspend_noirq";
  242. case SUSPEND_RESUME_NOIRQ:
  243. return "resume_noirq";
  244. case SUSPEND_RESUME:
  245. return "resume";
  246. default:
  247. return "";
  248. }
  249. }
  250. #define suspend_attr(_name) \
  251. static ssize_t _name##_show(struct kobject *kobj, \
  252. struct kobj_attribute *attr, char *buf) \
  253. { \
  254. return sprintf(buf, "%d\n", suspend_stats._name); \
  255. } \
  256. static struct kobj_attribute _name = __ATTR_RO(_name)
  257. suspend_attr(success);
  258. suspend_attr(fail);
  259. suspend_attr(failed_freeze);
  260. suspend_attr(failed_prepare);
  261. suspend_attr(failed_suspend);
  262. suspend_attr(failed_suspend_late);
  263. suspend_attr(failed_suspend_noirq);
  264. suspend_attr(failed_resume);
  265. suspend_attr(failed_resume_early);
  266. suspend_attr(failed_resume_noirq);
  267. static ssize_t last_failed_dev_show(struct kobject *kobj,
  268. struct kobj_attribute *attr, char *buf)
  269. {
  270. int index;
  271. char *last_failed_dev = NULL;
  272. index = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
  273. index %= REC_FAILED_NUM;
  274. last_failed_dev = suspend_stats.failed_devs[index];
  275. return sprintf(buf, "%s\n", last_failed_dev);
  276. }
  277. static struct kobj_attribute last_failed_dev = __ATTR_RO(last_failed_dev);
  278. static ssize_t last_failed_errno_show(struct kobject *kobj,
  279. struct kobj_attribute *attr, char *buf)
  280. {
  281. int index;
  282. int last_failed_errno;
  283. index = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
  284. index %= REC_FAILED_NUM;
  285. last_failed_errno = suspend_stats.errno[index];
  286. return sprintf(buf, "%d\n", last_failed_errno);
  287. }
  288. static struct kobj_attribute last_failed_errno = __ATTR_RO(last_failed_errno);
  289. static ssize_t last_failed_step_show(struct kobject *kobj,
  290. struct kobj_attribute *attr, char *buf)
  291. {
  292. int index;
  293. enum suspend_stat_step step;
  294. char *last_failed_step = NULL;
  295. index = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
  296. index %= REC_FAILED_NUM;
  297. step = suspend_stats.failed_steps[index];
  298. last_failed_step = suspend_step_name(step);
  299. return sprintf(buf, "%s\n", last_failed_step);
  300. }
  301. static struct kobj_attribute last_failed_step = __ATTR_RO(last_failed_step);
  302. static struct attribute *suspend_attrs[] = {
  303. &success.attr,
  304. &fail.attr,
  305. &failed_freeze.attr,
  306. &failed_prepare.attr,
  307. &failed_suspend.attr,
  308. &failed_suspend_late.attr,
  309. &failed_suspend_noirq.attr,
  310. &failed_resume.attr,
  311. &failed_resume_early.attr,
  312. &failed_resume_noirq.attr,
  313. &last_failed_dev.attr,
  314. &last_failed_errno.attr,
  315. &last_failed_step.attr,
  316. NULL,
  317. };
  318. static struct attribute_group suspend_attr_group = {
  319. .name = "suspend_stats",
  320. .attrs = suspend_attrs,
  321. };
  322. #ifdef CONFIG_DEBUG_FS
  323. static int suspend_stats_show(struct seq_file *s, void *unused)
  324. {
  325. int i, index, last_dev, last_errno, last_step;
  326. last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
  327. last_dev %= REC_FAILED_NUM;
  328. last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
  329. last_errno %= REC_FAILED_NUM;
  330. last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
  331. last_step %= REC_FAILED_NUM;
  332. seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
  333. "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
  334. "success", suspend_stats.success,
  335. "fail", suspend_stats.fail,
  336. "failed_freeze", suspend_stats.failed_freeze,
  337. "failed_prepare", suspend_stats.failed_prepare,
  338. "failed_suspend", suspend_stats.failed_suspend,
  339. "failed_suspend_late",
  340. suspend_stats.failed_suspend_late,
  341. "failed_suspend_noirq",
  342. suspend_stats.failed_suspend_noirq,
  343. "failed_resume", suspend_stats.failed_resume,
  344. "failed_resume_early",
  345. suspend_stats.failed_resume_early,
  346. "failed_resume_noirq",
  347. suspend_stats.failed_resume_noirq);
  348. seq_printf(s, "failures:\n last_failed_dev:\t%-s\n",
  349. suspend_stats.failed_devs[last_dev]);
  350. for (i = 1; i < REC_FAILED_NUM; i++) {
  351. index = last_dev + REC_FAILED_NUM - i;
  352. index %= REC_FAILED_NUM;
  353. seq_printf(s, "\t\t\t%-s\n",
  354. suspend_stats.failed_devs[index]);
  355. }
  356. seq_printf(s, " last_failed_errno:\t%-d\n",
  357. suspend_stats.errno[last_errno]);
  358. for (i = 1; i < REC_FAILED_NUM; i++) {
  359. index = last_errno + REC_FAILED_NUM - i;
  360. index %= REC_FAILED_NUM;
  361. seq_printf(s, "\t\t\t%-d\n",
  362. suspend_stats.errno[index]);
  363. }
  364. seq_printf(s, " last_failed_step:\t%-s\n",
  365. suspend_step_name(
  366. suspend_stats.failed_steps[last_step]));
  367. for (i = 1; i < REC_FAILED_NUM; i++) {
  368. index = last_step + REC_FAILED_NUM - i;
  369. index %= REC_FAILED_NUM;
  370. seq_printf(s, "\t\t\t%-s\n",
  371. suspend_step_name(
  372. suspend_stats.failed_steps[index]));
  373. }
  374. return 0;
  375. }
  376. DEFINE_SHOW_ATTRIBUTE(suspend_stats);
  377. static int __init pm_debugfs_init(void)
  378. {
  379. debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
  380. NULL, NULL, &suspend_stats_fops);
  381. return 0;
  382. }
  383. late_initcall(pm_debugfs_init);
  384. #endif /* CONFIG_DEBUG_FS */
  385. #endif /* CONFIG_PM_SLEEP */
  386. #ifdef CONFIG_PM_SLEEP_DEBUG
  387. /*
  388. * pm_print_times: print time taken by devices to suspend and resume.
  389. *
  390. * show() returns whether printing of suspend and resume times is enabled.
  391. * store() accepts 0 or 1. 0 disables printing and 1 enables it.
  392. */
  393. bool pm_print_times_enabled;
  394. static ssize_t pm_print_times_show(struct kobject *kobj,
  395. struct kobj_attribute *attr, char *buf)
  396. {
  397. return sprintf(buf, "%d\n", pm_print_times_enabled);
  398. }
  399. static ssize_t pm_print_times_store(struct kobject *kobj,
  400. struct kobj_attribute *attr,
  401. const char *buf, size_t n)
  402. {
  403. unsigned long val;
  404. if (kstrtoul(buf, 10, &val))
  405. return -EINVAL;
  406. if (val > 1)
  407. return -EINVAL;
  408. pm_print_times_enabled = !!val;
  409. return n;
  410. }
  411. power_attr(pm_print_times);
  412. static inline void pm_print_times_init(void)
  413. {
  414. pm_print_times_enabled = !!initcall_debug;
  415. }
  416. static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
  417. struct kobj_attribute *attr,
  418. char *buf)
  419. {
  420. if (!pm_wakeup_irq())
  421. return -ENODATA;
  422. return sprintf(buf, "%u\n", pm_wakeup_irq());
  423. }
  424. power_attr_ro(pm_wakeup_irq);
  425. bool pm_debug_messages_on __read_mostly;
  426. static ssize_t pm_debug_messages_show(struct kobject *kobj,
  427. struct kobj_attribute *attr, char *buf)
  428. {
  429. return sprintf(buf, "%d\n", pm_debug_messages_on);
  430. }
  431. static ssize_t pm_debug_messages_store(struct kobject *kobj,
  432. struct kobj_attribute *attr,
  433. const char *buf, size_t n)
  434. {
  435. unsigned long val;
  436. if (kstrtoul(buf, 10, &val))
  437. return -EINVAL;
  438. if (val > 1)
  439. return -EINVAL;
  440. pm_debug_messages_on = !!val;
  441. return n;
  442. }
  443. power_attr(pm_debug_messages);
  444. static int __init pm_debug_messages_setup(char *str)
  445. {
  446. pm_debug_messages_on = true;
  447. return 1;
  448. }
  449. __setup("pm_debug_messages", pm_debug_messages_setup);
  450. /**
  451. * __pm_pr_dbg - Print a suspend debug message to the kernel log.
  452. * @defer: Whether or not to use printk_deferred() to print the message.
  453. * @fmt: Message format.
  454. *
  455. * The message will be emitted if enabled through the pm_debug_messages
  456. * sysfs attribute.
  457. */
  458. void __pm_pr_dbg(bool defer, const char *fmt, ...)
  459. {
  460. struct va_format vaf;
  461. va_list args;
  462. if (!pm_debug_messages_on)
  463. return;
  464. va_start(args, fmt);
  465. vaf.fmt = fmt;
  466. vaf.va = &args;
  467. if (defer)
  468. printk_deferred(KERN_DEBUG "PM: %pV", &vaf);
  469. else
  470. printk(KERN_DEBUG "PM: %pV", &vaf);
  471. va_end(args);
  472. }
  473. #else /* !CONFIG_PM_SLEEP_DEBUG */
  474. static inline void pm_print_times_init(void) {}
  475. #endif /* CONFIG_PM_SLEEP_DEBUG */
  476. struct kobject *power_kobj;
  477. /**
  478. * state - control system sleep states.
  479. *
  480. * show() returns available sleep state labels, which may be "mem", "standby",
  481. * "freeze" and "disk" (hibernation).
  482. * See Documentation/admin-guide/pm/sleep-states.rst for a description of
  483. * what they mean.
  484. *
  485. * store() accepts one of those strings, translates it into the proper
  486. * enumerated value, and initiates a suspend transition.
  487. */
  488. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  489. char *buf)
  490. {
  491. char *s = buf;
  492. #ifdef CONFIG_SUSPEND
  493. suspend_state_t i;
  494. for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
  495. if (pm_states[i])
  496. s += sprintf(s,"%s ", pm_states[i]);
  497. #endif
  498. if (hibernation_available())
  499. s += sprintf(s, "disk ");
  500. if (s != buf)
  501. /* convert the last space to a newline */
  502. *(s-1) = '\n';
  503. return (s - buf);
  504. }
  505. static suspend_state_t decode_state(const char *buf, size_t n)
  506. {
  507. #ifdef CONFIG_SUSPEND
  508. suspend_state_t state;
  509. #endif
  510. char *p;
  511. int len;
  512. p = memchr(buf, '\n', n);
  513. len = p ? p - buf : n;
  514. /* Check hibernation first. */
  515. if (len == 4 && str_has_prefix(buf, "disk"))
  516. return PM_SUSPEND_MAX;
  517. #ifdef CONFIG_SUSPEND
  518. for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
  519. const char *label = pm_states[state];
  520. if (label && len == strlen(label) && !strncmp(buf, label, len))
  521. return state;
  522. }
  523. #endif
  524. return PM_SUSPEND_ON;
  525. }
  526. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  527. const char *buf, size_t n)
  528. {
  529. suspend_state_t state;
  530. int error;
  531. error = pm_autosleep_lock();
  532. if (error)
  533. return error;
  534. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  535. error = -EBUSY;
  536. goto out;
  537. }
  538. state = decode_state(buf, n);
  539. if (state < PM_SUSPEND_MAX) {
  540. if (state == PM_SUSPEND_MEM)
  541. state = mem_sleep_current;
  542. error = pm_suspend(state);
  543. } else if (state == PM_SUSPEND_MAX) {
  544. error = hibernate();
  545. } else {
  546. error = -EINVAL;
  547. }
  548. out:
  549. pm_autosleep_unlock();
  550. return error ? error : n;
  551. }
  552. power_attr(state);
  553. #ifdef CONFIG_PM_SLEEP
  554. /*
  555. * The 'wakeup_count' attribute, along with the functions defined in
  556. * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
  557. * handled in a non-racy way.
  558. *
  559. * If a wakeup event occurs when the system is in a sleep state, it simply is
  560. * woken up. In turn, if an event that would wake the system up from a sleep
  561. * state occurs when it is undergoing a transition to that sleep state, the
  562. * transition should be aborted. Moreover, if such an event occurs when the
  563. * system is in the working state, an attempt to start a transition to the
  564. * given sleep state should fail during certain period after the detection of
  565. * the event. Using the 'state' attribute alone is not sufficient to satisfy
  566. * these requirements, because a wakeup event may occur exactly when 'state'
  567. * is being written to and may be delivered to user space right before it is
  568. * frozen, so the event will remain only partially processed until the system is
  569. * woken up by another event. In particular, it won't cause the transition to
  570. * a sleep state to be aborted.
  571. *
  572. * This difficulty may be overcome if user space uses 'wakeup_count' before
  573. * writing to 'state'. It first should read from 'wakeup_count' and store
  574. * the read value. Then, after carrying out its own preparations for the system
  575. * transition to a sleep state, it should write the stored value to
  576. * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
  577. * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
  578. * is allowed to write to 'state', but the transition will be aborted if there
  579. * are any wakeup events detected after 'wakeup_count' was written to.
  580. */
  581. static ssize_t wakeup_count_show(struct kobject *kobj,
  582. struct kobj_attribute *attr,
  583. char *buf)
  584. {
  585. unsigned int val;
  586. return pm_get_wakeup_count(&val, true) ?
  587. sprintf(buf, "%u\n", val) : -EINTR;
  588. }
  589. static ssize_t wakeup_count_store(struct kobject *kobj,
  590. struct kobj_attribute *attr,
  591. const char *buf, size_t n)
  592. {
  593. unsigned int val;
  594. int error;
  595. error = pm_autosleep_lock();
  596. if (error)
  597. return error;
  598. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  599. error = -EBUSY;
  600. goto out;
  601. }
  602. error = -EINVAL;
  603. if (sscanf(buf, "%u", &val) == 1) {
  604. if (pm_save_wakeup_count(val))
  605. error = n;
  606. else
  607. pm_print_active_wakeup_sources();
  608. }
  609. out:
  610. pm_autosleep_unlock();
  611. return error;
  612. }
  613. power_attr(wakeup_count);
  614. #ifdef CONFIG_PM_AUTOSLEEP
  615. static ssize_t autosleep_show(struct kobject *kobj,
  616. struct kobj_attribute *attr,
  617. char *buf)
  618. {
  619. suspend_state_t state = pm_autosleep_state();
  620. if (state == PM_SUSPEND_ON)
  621. return sprintf(buf, "off\n");
  622. #ifdef CONFIG_SUSPEND
  623. if (state < PM_SUSPEND_MAX)
  624. return sprintf(buf, "%s\n", pm_states[state] ?
  625. pm_states[state] : "error");
  626. #endif
  627. #ifdef CONFIG_HIBERNATION
  628. return sprintf(buf, "disk\n");
  629. #else
  630. return sprintf(buf, "error");
  631. #endif
  632. }
  633. static ssize_t autosleep_store(struct kobject *kobj,
  634. struct kobj_attribute *attr,
  635. const char *buf, size_t n)
  636. {
  637. suspend_state_t state = decode_state(buf, n);
  638. int error;
  639. if (state == PM_SUSPEND_ON
  640. && strcmp(buf, "off") && strcmp(buf, "off\n"))
  641. return -EINVAL;
  642. if (state == PM_SUSPEND_MEM)
  643. state = mem_sleep_current;
  644. error = pm_autosleep_set_state(state);
  645. return error ? error : n;
  646. }
  647. power_attr(autosleep);
  648. #endif /* CONFIG_PM_AUTOSLEEP */
  649. #ifdef CONFIG_PM_WAKELOCKS
  650. static ssize_t wake_lock_show(struct kobject *kobj,
  651. struct kobj_attribute *attr,
  652. char *buf)
  653. {
  654. return pm_show_wakelocks(buf, true);
  655. }
  656. static ssize_t wake_lock_store(struct kobject *kobj,
  657. struct kobj_attribute *attr,
  658. const char *buf, size_t n)
  659. {
  660. int error = pm_wake_lock(buf);
  661. return error ? error : n;
  662. }
  663. power_attr(wake_lock);
  664. static ssize_t wake_unlock_show(struct kobject *kobj,
  665. struct kobj_attribute *attr,
  666. char *buf)
  667. {
  668. return pm_show_wakelocks(buf, false);
  669. }
  670. static ssize_t wake_unlock_store(struct kobject *kobj,
  671. struct kobj_attribute *attr,
  672. const char *buf, size_t n)
  673. {
  674. int error = pm_wake_unlock(buf);
  675. return error ? error : n;
  676. }
  677. power_attr(wake_unlock);
  678. #endif /* CONFIG_PM_WAKELOCKS */
  679. #endif /* CONFIG_PM_SLEEP */
  680. #ifdef CONFIG_PM_TRACE
  681. int pm_trace_enabled;
  682. static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
  683. char *buf)
  684. {
  685. return sprintf(buf, "%d\n", pm_trace_enabled);
  686. }
  687. static ssize_t
  688. pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
  689. const char *buf, size_t n)
  690. {
  691. int val;
  692. if (sscanf(buf, "%d", &val) == 1) {
  693. pm_trace_enabled = !!val;
  694. if (pm_trace_enabled) {
  695. pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
  696. "PM: Correct system time has to be restored manually after resume.\n");
  697. }
  698. return n;
  699. }
  700. return -EINVAL;
  701. }
  702. power_attr(pm_trace);
  703. static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
  704. struct kobj_attribute *attr,
  705. char *buf)
  706. {
  707. return show_trace_dev_match(buf, PAGE_SIZE);
  708. }
  709. power_attr_ro(pm_trace_dev_match);
  710. #endif /* CONFIG_PM_TRACE */
  711. #ifdef CONFIG_FREEZER
  712. static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
  713. struct kobj_attribute *attr, char *buf)
  714. {
  715. return sprintf(buf, "%u\n", freeze_timeout_msecs);
  716. }
  717. static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
  718. struct kobj_attribute *attr,
  719. const char *buf, size_t n)
  720. {
  721. unsigned long val;
  722. if (kstrtoul(buf, 10, &val))
  723. return -EINVAL;
  724. freeze_timeout_msecs = val;
  725. return n;
  726. }
  727. power_attr(pm_freeze_timeout);
  728. #endif /* CONFIG_FREEZER*/
  729. static struct attribute * g[] = {
  730. &state_attr.attr,
  731. #ifdef CONFIG_PM_TRACE
  732. &pm_trace_attr.attr,
  733. &pm_trace_dev_match_attr.attr,
  734. #endif
  735. #ifdef CONFIG_PM_SLEEP
  736. &pm_async_attr.attr,
  737. &wakeup_count_attr.attr,
  738. #ifdef CONFIG_SUSPEND
  739. &mem_sleep_attr.attr,
  740. &sync_on_suspend_attr.attr,
  741. #endif
  742. #ifdef CONFIG_PM_AUTOSLEEP
  743. &autosleep_attr.attr,
  744. #endif
  745. #ifdef CONFIG_PM_WAKELOCKS
  746. &wake_lock_attr.attr,
  747. &wake_unlock_attr.attr,
  748. #endif
  749. #ifdef CONFIG_PM_SLEEP_DEBUG
  750. &pm_test_attr.attr,
  751. &pm_print_times_attr.attr,
  752. &pm_wakeup_irq_attr.attr,
  753. &pm_debug_messages_attr.attr,
  754. #endif
  755. #endif
  756. #ifdef CONFIG_FREEZER
  757. &pm_freeze_timeout_attr.attr,
  758. #endif
  759. NULL,
  760. };
  761. static const struct attribute_group attr_group = {
  762. .attrs = g,
  763. };
  764. static const struct attribute_group *attr_groups[] = {
  765. &attr_group,
  766. #ifdef CONFIG_PM_SLEEP
  767. &suspend_attr_group,
  768. #endif
  769. NULL,
  770. };
  771. struct workqueue_struct *pm_wq;
  772. EXPORT_SYMBOL_GPL(pm_wq);
  773. static int __init pm_start_workqueue(void)
  774. {
  775. pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
  776. return pm_wq ? 0 : -ENOMEM;
  777. }
  778. static int __init pm_init(void)
  779. {
  780. int error = pm_start_workqueue();
  781. if (error)
  782. return error;
  783. hibernate_image_size_init();
  784. hibernate_reserved_size_init();
  785. pm_states_init();
  786. power_kobj = kobject_create_and_add("power", NULL);
  787. if (!power_kobj)
  788. return -ENOMEM;
  789. error = sysfs_create_groups(power_kobj, attr_groups);
  790. if (error)
  791. return error;
  792. pm_print_times_init();
  793. return pm_autosleep_init();
  794. }
  795. core_initcall(pm_init);