main.c 15 KB

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