main.c 22 KB

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