main.c 25 KB

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