main.c 15 KB

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