main.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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 && (hibernation_in_progress() ||
  505. pm_suspend_target_state != PM_SUSPEND_ON);
  506. }
  507. EXPORT_SYMBOL_GPL(pm_debug_messages_should_print);
  508. static ssize_t pm_debug_messages_show(struct kobject *kobj,
  509. struct kobj_attribute *attr, char *buf)
  510. {
  511. return sysfs_emit(buf, "%d\n", pm_debug_messages_on);
  512. }
  513. static ssize_t pm_debug_messages_store(struct kobject *kobj,
  514. struct kobj_attribute *attr,
  515. const char *buf, size_t n)
  516. {
  517. unsigned long val;
  518. if (kstrtoul(buf, 10, &val))
  519. return -EINVAL;
  520. if (val > 1)
  521. return -EINVAL;
  522. pm_debug_messages_on = !!val;
  523. return n;
  524. }
  525. power_attr(pm_debug_messages);
  526. static int __init pm_debug_messages_setup(char *str)
  527. {
  528. pm_debug_messages_on = true;
  529. return 1;
  530. }
  531. __setup("pm_debug_messages", pm_debug_messages_setup);
  532. #else /* !CONFIG_PM_SLEEP_DEBUG */
  533. static inline void pm_print_times_init(void) {}
  534. #endif /* CONFIG_PM_SLEEP_DEBUG */
  535. struct kobject *power_kobj;
  536. /*
  537. * state - control system sleep states.
  538. *
  539. * show() returns available sleep state labels, which may be "mem", "standby",
  540. * "freeze" and "disk" (hibernation).
  541. * See Documentation/admin-guide/pm/sleep-states.rst for a description of
  542. * what they mean.
  543. *
  544. * store() accepts one of those strings, translates it into the proper
  545. * enumerated value, and initiates a suspend transition.
  546. */
  547. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  548. char *buf)
  549. {
  550. ssize_t count = 0;
  551. #ifdef CONFIG_SUSPEND
  552. suspend_state_t i;
  553. for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
  554. if (pm_states[i])
  555. count += sysfs_emit_at(buf, count, "%s ", pm_states[i]);
  556. #endif
  557. if (hibernation_available())
  558. count += sysfs_emit_at(buf, count, "disk ");
  559. /* Convert the last space to a newline if needed. */
  560. if (count > 0)
  561. buf[count - 1] = '\n';
  562. return count;
  563. }
  564. static suspend_state_t decode_state(const char *buf, size_t n)
  565. {
  566. #ifdef CONFIG_SUSPEND
  567. suspend_state_t state;
  568. #endif
  569. char *p;
  570. int len;
  571. p = memchr(buf, '\n', n);
  572. len = p ? p - buf : n;
  573. /* Check hibernation first. */
  574. if (len == 4 && str_has_prefix(buf, "disk"))
  575. return PM_SUSPEND_MAX;
  576. #ifdef CONFIG_SUSPEND
  577. for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
  578. const char *label = pm_states[state];
  579. if (label && len == strlen(label) && !strncmp(buf, label, len))
  580. return state;
  581. }
  582. #endif
  583. return PM_SUSPEND_ON;
  584. }
  585. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  586. const char *buf, size_t n)
  587. {
  588. suspend_state_t state;
  589. int error;
  590. error = pm_autosleep_lock();
  591. if (error)
  592. return error;
  593. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  594. error = -EBUSY;
  595. goto out;
  596. }
  597. state = decode_state(buf, n);
  598. if (state < PM_SUSPEND_MAX) {
  599. if (state == PM_SUSPEND_MEM)
  600. state = mem_sleep_current;
  601. error = pm_suspend(state);
  602. } else if (state == PM_SUSPEND_MAX) {
  603. error = hibernate();
  604. } else {
  605. error = -EINVAL;
  606. }
  607. out:
  608. pm_autosleep_unlock();
  609. return error ? error : n;
  610. }
  611. power_attr(state);
  612. #ifdef CONFIG_PM_SLEEP
  613. /*
  614. * The 'wakeup_count' attribute, along with the functions defined in
  615. * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
  616. * handled in a non-racy way.
  617. *
  618. * If a wakeup event occurs when the system is in a sleep state, it simply is
  619. * woken up. In turn, if an event that would wake the system up from a sleep
  620. * state occurs when it is undergoing a transition to that sleep state, the
  621. * transition should be aborted. Moreover, if such an event occurs when the
  622. * system is in the working state, an attempt to start a transition to the
  623. * given sleep state should fail during certain period after the detection of
  624. * the event. Using the 'state' attribute alone is not sufficient to satisfy
  625. * these requirements, because a wakeup event may occur exactly when 'state'
  626. * is being written to and may be delivered to user space right before it is
  627. * frozen, so the event will remain only partially processed until the system is
  628. * woken up by another event. In particular, it won't cause the transition to
  629. * a sleep state to be aborted.
  630. *
  631. * This difficulty may be overcome if user space uses 'wakeup_count' before
  632. * writing to 'state'. It first should read from 'wakeup_count' and store
  633. * the read value. Then, after carrying out its own preparations for the system
  634. * transition to a sleep state, it should write the stored value to
  635. * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
  636. * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
  637. * is allowed to write to 'state', but the transition will be aborted if there
  638. * are any wakeup events detected after 'wakeup_count' was written to.
  639. */
  640. static ssize_t wakeup_count_show(struct kobject *kobj,
  641. struct kobj_attribute *attr,
  642. char *buf)
  643. {
  644. unsigned int val;
  645. return pm_get_wakeup_count(&val, true) ?
  646. sysfs_emit(buf, "%u\n", val) : -EINTR;
  647. }
  648. static ssize_t wakeup_count_store(struct kobject *kobj,
  649. struct kobj_attribute *attr,
  650. const char *buf, size_t n)
  651. {
  652. unsigned int val;
  653. int error;
  654. error = pm_autosleep_lock();
  655. if (error)
  656. return error;
  657. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  658. error = -EBUSY;
  659. goto out;
  660. }
  661. error = -EINVAL;
  662. if (sscanf(buf, "%u", &val) == 1) {
  663. if (pm_save_wakeup_count(val))
  664. error = n;
  665. else
  666. pm_print_active_wakeup_sources();
  667. }
  668. out:
  669. pm_autosleep_unlock();
  670. return error;
  671. }
  672. power_attr(wakeup_count);
  673. #ifdef CONFIG_PM_AUTOSLEEP
  674. static ssize_t autosleep_show(struct kobject *kobj,
  675. struct kobj_attribute *attr,
  676. char *buf)
  677. {
  678. suspend_state_t state = pm_autosleep_state();
  679. if (state == PM_SUSPEND_ON)
  680. return sysfs_emit(buf, "off\n");
  681. #ifdef CONFIG_SUSPEND
  682. if (state < PM_SUSPEND_MAX)
  683. return sysfs_emit(buf, "%s\n", pm_states[state] ?
  684. pm_states[state] : "error");
  685. #endif
  686. #ifdef CONFIG_HIBERNATION
  687. return sysfs_emit(buf, "disk\n");
  688. #else
  689. return sysfs_emit(buf, "error\n");
  690. #endif
  691. }
  692. static ssize_t autosleep_store(struct kobject *kobj,
  693. struct kobj_attribute *attr,
  694. const char *buf, size_t n)
  695. {
  696. suspend_state_t state = decode_state(buf, n);
  697. int error;
  698. if (state == PM_SUSPEND_ON
  699. && strcmp(buf, "off") && strcmp(buf, "off\n"))
  700. return -EINVAL;
  701. if (state == PM_SUSPEND_MEM)
  702. state = mem_sleep_current;
  703. error = pm_autosleep_set_state(state);
  704. return error ? error : n;
  705. }
  706. power_attr(autosleep);
  707. #endif /* CONFIG_PM_AUTOSLEEP */
  708. #ifdef CONFIG_PM_WAKELOCKS
  709. static ssize_t wake_lock_show(struct kobject *kobj,
  710. struct kobj_attribute *attr,
  711. char *buf)
  712. {
  713. return pm_show_wakelocks(buf, true);
  714. }
  715. static ssize_t wake_lock_store(struct kobject *kobj,
  716. struct kobj_attribute *attr,
  717. const char *buf, size_t n)
  718. {
  719. int error = pm_wake_lock(buf);
  720. return error ? error : n;
  721. }
  722. power_attr(wake_lock);
  723. static ssize_t wake_unlock_show(struct kobject *kobj,
  724. struct kobj_attribute *attr,
  725. char *buf)
  726. {
  727. return pm_show_wakelocks(buf, false);
  728. }
  729. static ssize_t wake_unlock_store(struct kobject *kobj,
  730. struct kobj_attribute *attr,
  731. const char *buf, size_t n)
  732. {
  733. int error = pm_wake_unlock(buf);
  734. return error ? error : n;
  735. }
  736. power_attr(wake_unlock);
  737. #endif /* CONFIG_PM_WAKELOCKS */
  738. #endif /* CONFIG_PM_SLEEP */
  739. #ifdef CONFIG_PM_TRACE
  740. int pm_trace_enabled;
  741. static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
  742. char *buf)
  743. {
  744. return sysfs_emit(buf, "%d\n", pm_trace_enabled);
  745. }
  746. static ssize_t
  747. pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
  748. const char *buf, size_t n)
  749. {
  750. int val;
  751. if (sscanf(buf, "%d", &val) == 1) {
  752. pm_trace_enabled = !!val;
  753. if (pm_trace_enabled) {
  754. pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
  755. "PM: Correct system time has to be restored manually after resume.\n");
  756. }
  757. return n;
  758. }
  759. return -EINVAL;
  760. }
  761. power_attr(pm_trace);
  762. static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
  763. struct kobj_attribute *attr,
  764. char *buf)
  765. {
  766. return show_trace_dev_match(buf, PAGE_SIZE);
  767. }
  768. power_attr_ro(pm_trace_dev_match);
  769. #endif /* CONFIG_PM_TRACE */
  770. #ifdef CONFIG_FREEZER
  771. static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
  772. struct kobj_attribute *attr, char *buf)
  773. {
  774. return sysfs_emit(buf, "%u\n", freeze_timeout_msecs);
  775. }
  776. static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
  777. struct kobj_attribute *attr,
  778. const char *buf, size_t n)
  779. {
  780. unsigned long val;
  781. if (kstrtoul(buf, 10, &val))
  782. return -EINVAL;
  783. freeze_timeout_msecs = val;
  784. return n;
  785. }
  786. power_attr(pm_freeze_timeout);
  787. #endif /* CONFIG_FREEZER*/
  788. static struct attribute * g[] = {
  789. &state_attr.attr,
  790. #ifdef CONFIG_PM_TRACE
  791. &pm_trace_attr.attr,
  792. &pm_trace_dev_match_attr.attr,
  793. #endif
  794. #ifdef CONFIG_PM_SLEEP
  795. &pm_async_attr.attr,
  796. &wakeup_count_attr.attr,
  797. #ifdef CONFIG_SUSPEND
  798. &mem_sleep_attr.attr,
  799. &sync_on_suspend_attr.attr,
  800. #endif
  801. #ifdef CONFIG_PM_AUTOSLEEP
  802. &autosleep_attr.attr,
  803. #endif
  804. #ifdef CONFIG_PM_WAKELOCKS
  805. &wake_lock_attr.attr,
  806. &wake_unlock_attr.attr,
  807. #endif
  808. #ifdef CONFIG_PM_SLEEP_DEBUG
  809. &pm_test_attr.attr,
  810. &pm_print_times_attr.attr,
  811. &pm_wakeup_irq_attr.attr,
  812. &pm_debug_messages_attr.attr,
  813. #endif
  814. #endif
  815. #ifdef CONFIG_FREEZER
  816. &pm_freeze_timeout_attr.attr,
  817. #endif
  818. NULL,
  819. };
  820. static const struct attribute_group attr_group = {
  821. .attrs = g,
  822. };
  823. static const struct attribute_group *attr_groups[] = {
  824. &attr_group,
  825. #ifdef CONFIG_PM_SLEEP
  826. &suspend_attr_group,
  827. #endif
  828. NULL,
  829. };
  830. struct workqueue_struct *pm_wq;
  831. EXPORT_SYMBOL_GPL(pm_wq);
  832. static int __init pm_start_workqueue(void)
  833. {
  834. pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
  835. return pm_wq ? 0 : -ENOMEM;
  836. }
  837. static int __init pm_init(void)
  838. {
  839. int error = pm_start_workqueue();
  840. if (error)
  841. return error;
  842. hibernate_image_size_init();
  843. hibernate_reserved_size_init();
  844. pm_states_init();
  845. power_kobj = kobject_create_and_add("power", NULL);
  846. if (!power_kobj)
  847. return -ENOMEM;
  848. error = sysfs_create_groups(power_kobj, attr_groups);
  849. if (error)
  850. return error;
  851. pm_print_times_init();
  852. return pm_autosleep_init();
  853. }
  854. core_initcall(pm_init);