main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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/module.h>
  11. #include <linux/suspend.h>
  12. #include <linux/kobject.h>
  13. #include <linux/string.h>
  14. #include <linux/delay.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/console.h>
  18. #include <linux/cpu.h>
  19. #include <linux/resume-trace.h>
  20. #include <linux/freezer.h>
  21. #include <linux/vmstat.h>
  22. #include <linux/syscalls.h>
  23. #include "power.h"
  24. DEFINE_MUTEX(pm_mutex);
  25. unsigned int pm_flags;
  26. EXPORT_SYMBOL(pm_flags);
  27. #ifdef CONFIG_PM_SLEEP
  28. /* Routines for PM-transition notifications */
  29. static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
  30. int register_pm_notifier(struct notifier_block *nb)
  31. {
  32. return blocking_notifier_chain_register(&pm_chain_head, nb);
  33. }
  34. EXPORT_SYMBOL_GPL(register_pm_notifier);
  35. int unregister_pm_notifier(struct notifier_block *nb)
  36. {
  37. return blocking_notifier_chain_unregister(&pm_chain_head, nb);
  38. }
  39. EXPORT_SYMBOL_GPL(unregister_pm_notifier);
  40. int pm_notifier_call_chain(unsigned long val)
  41. {
  42. return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
  43. == NOTIFY_BAD) ? -EINVAL : 0;
  44. }
  45. #ifdef CONFIG_PM_DEBUG
  46. int pm_test_level = TEST_NONE;
  47. static int suspend_test(int level)
  48. {
  49. if (pm_test_level == level) {
  50. printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
  51. mdelay(5000);
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static const char * const pm_tests[__TEST_AFTER_LAST] = {
  57. [TEST_NONE] = "none",
  58. [TEST_CORE] = "core",
  59. [TEST_CPUS] = "processors",
  60. [TEST_PLATFORM] = "platform",
  61. [TEST_DEVICES] = "devices",
  62. [TEST_FREEZER] = "freezer",
  63. };
  64. static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
  65. char *buf)
  66. {
  67. char *s = buf;
  68. int level;
  69. for (level = TEST_FIRST; level <= TEST_MAX; level++)
  70. if (pm_tests[level]) {
  71. if (level == pm_test_level)
  72. s += sprintf(s, "[%s] ", pm_tests[level]);
  73. else
  74. s += sprintf(s, "%s ", pm_tests[level]);
  75. }
  76. if (s != buf)
  77. /* convert the last space to a newline */
  78. *(s-1) = '\n';
  79. return (s - buf);
  80. }
  81. static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
  82. const char *buf, size_t n)
  83. {
  84. const char * const *s;
  85. int level;
  86. char *p;
  87. int len;
  88. int error = -EINVAL;
  89. p = memchr(buf, '\n', n);
  90. len = p ? p - buf : n;
  91. mutex_lock(&pm_mutex);
  92. level = TEST_FIRST;
  93. for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
  94. if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
  95. pm_test_level = level;
  96. error = 0;
  97. break;
  98. }
  99. mutex_unlock(&pm_mutex);
  100. return error ? error : n;
  101. }
  102. power_attr(pm_test);
  103. #else /* !CONFIG_PM_DEBUG */
  104. static inline int suspend_test(int level) { return 0; }
  105. #endif /* !CONFIG_PM_DEBUG */
  106. #endif /* CONFIG_PM_SLEEP */
  107. #ifdef CONFIG_SUSPEND
  108. #ifdef CONFIG_PM_TEST_SUSPEND
  109. /*
  110. * We test the system suspend code by setting an RTC wakealarm a short
  111. * time in the future, then suspending. Suspending the devices won't
  112. * normally take long ... some systems only need a few milliseconds.
  113. *
  114. * The time it takes is system-specific though, so when we test this
  115. * during system bootup we allow a LOT of time.
  116. */
  117. #define TEST_SUSPEND_SECONDS 5
  118. static unsigned long suspend_test_start_time;
  119. static void suspend_test_start(void)
  120. {
  121. /* FIXME Use better timebase than "jiffies", ideally a clocksource.
  122. * What we want is a hardware counter that will work correctly even
  123. * during the irqs-are-off stages of the suspend/resume cycle...
  124. */
  125. suspend_test_start_time = jiffies;
  126. }
  127. static void suspend_test_finish(const char *label)
  128. {
  129. long nj = jiffies - suspend_test_start_time;
  130. unsigned msec;
  131. msec = jiffies_to_msecs(abs(nj));
  132. pr_info("PM: %s took %d.%03d seconds\n", label,
  133. msec / 1000, msec % 1000);
  134. /* Warning on suspend means the RTC alarm period needs to be
  135. * larger -- the system was sooo slooowwww to suspend that the
  136. * alarm (should have) fired before the system went to sleep!
  137. *
  138. * Warning on either suspend or resume also means the system
  139. * has some performance issues. The stack dump of a WARN_ON
  140. * is more likely to get the right attention than a printk...
  141. */
  142. WARN_ON(msec > (TEST_SUSPEND_SECONDS * 1000));
  143. }
  144. #else
  145. static void suspend_test_start(void)
  146. {
  147. }
  148. static void suspend_test_finish(const char *label)
  149. {
  150. }
  151. #endif
  152. /* This is just an arbitrary number */
  153. #define FREE_PAGE_NUMBER (100)
  154. static struct platform_suspend_ops *suspend_ops;
  155. /**
  156. * suspend_set_ops - Set the global suspend method table.
  157. * @ops: Pointer to ops structure.
  158. */
  159. void suspend_set_ops(struct platform_suspend_ops *ops)
  160. {
  161. mutex_lock(&pm_mutex);
  162. suspend_ops = ops;
  163. mutex_unlock(&pm_mutex);
  164. }
  165. /**
  166. * suspend_valid_only_mem - generic memory-only valid callback
  167. *
  168. * Platform drivers that implement mem suspend only and only need
  169. * to check for that in their .valid callback can use this instead
  170. * of rolling their own .valid callback.
  171. */
  172. int suspend_valid_only_mem(suspend_state_t state)
  173. {
  174. return state == PM_SUSPEND_MEM;
  175. }
  176. /**
  177. * suspend_prepare - Do prep work before entering low-power state.
  178. *
  179. * This is common code that is called for each state that we're entering.
  180. * Run suspend notifiers, allocate a console and stop all processes.
  181. */
  182. static int suspend_prepare(void)
  183. {
  184. int error;
  185. unsigned int free_pages;
  186. if (!suspend_ops || !suspend_ops->enter)
  187. return -EPERM;
  188. pm_prepare_console();
  189. error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
  190. if (error)
  191. goto Finish;
  192. if (suspend_freeze_processes()) {
  193. error = -EAGAIN;
  194. goto Thaw;
  195. }
  196. free_pages = global_page_state(NR_FREE_PAGES);
  197. if (free_pages < FREE_PAGE_NUMBER) {
  198. pr_debug("PM: free some memory\n");
  199. shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
  200. if (nr_free_pages() < FREE_PAGE_NUMBER) {
  201. error = -ENOMEM;
  202. printk(KERN_ERR "PM: No enough memory\n");
  203. }
  204. }
  205. if (!error)
  206. return 0;
  207. Thaw:
  208. suspend_thaw_processes();
  209. Finish:
  210. pm_notifier_call_chain(PM_POST_SUSPEND);
  211. pm_restore_console();
  212. return error;
  213. }
  214. /* default implementation */
  215. void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
  216. {
  217. local_irq_disable();
  218. }
  219. /* default implementation */
  220. void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
  221. {
  222. local_irq_enable();
  223. }
  224. /**
  225. * suspend_enter - enter the desired system sleep state.
  226. * @state: state to enter
  227. *
  228. * This function should be called after devices have been suspended.
  229. */
  230. static int suspend_enter(suspend_state_t state)
  231. {
  232. int error = 0;
  233. device_pm_lock();
  234. arch_suspend_disable_irqs();
  235. BUG_ON(!irqs_disabled());
  236. if ((error = device_power_down(PMSG_SUSPEND))) {
  237. printk(KERN_ERR "PM: Some devices failed to power down\n");
  238. goto Done;
  239. }
  240. if (!suspend_test(TEST_CORE))
  241. error = suspend_ops->enter(state);
  242. device_power_up(PMSG_RESUME);
  243. Done:
  244. arch_suspend_enable_irqs();
  245. BUG_ON(irqs_disabled());
  246. device_pm_unlock();
  247. return error;
  248. }
  249. /**
  250. * suspend_devices_and_enter - suspend devices and enter the desired system
  251. * sleep state.
  252. * @state: state to enter
  253. */
  254. int suspend_devices_and_enter(suspend_state_t state)
  255. {
  256. int error;
  257. if (!suspend_ops)
  258. return -ENOSYS;
  259. if (suspend_ops->begin) {
  260. error = suspend_ops->begin(state);
  261. if (error)
  262. goto Close;
  263. }
  264. suspend_console();
  265. suspend_test_start();
  266. error = device_suspend(PMSG_SUSPEND);
  267. if (error) {
  268. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  269. goto Recover_platform;
  270. }
  271. suspend_test_finish("suspend devices");
  272. if (suspend_test(TEST_DEVICES))
  273. goto Recover_platform;
  274. if (suspend_ops->prepare) {
  275. error = suspend_ops->prepare();
  276. if (error)
  277. goto Resume_devices;
  278. }
  279. if (suspend_test(TEST_PLATFORM))
  280. goto Finish;
  281. error = disable_nonboot_cpus();
  282. if (!error && !suspend_test(TEST_CPUS))
  283. suspend_enter(state);
  284. enable_nonboot_cpus();
  285. Finish:
  286. if (suspend_ops->finish)
  287. suspend_ops->finish();
  288. Resume_devices:
  289. suspend_test_start();
  290. device_resume(PMSG_RESUME);
  291. suspend_test_finish("resume devices");
  292. resume_console();
  293. Close:
  294. if (suspend_ops->end)
  295. suspend_ops->end();
  296. return error;
  297. Recover_platform:
  298. if (suspend_ops->recover)
  299. suspend_ops->recover();
  300. goto Resume_devices;
  301. }
  302. /**
  303. * suspend_finish - Do final work before exiting suspend sequence.
  304. *
  305. * Call platform code to clean up, restart processes, and free the
  306. * console that we've allocated. This is not called for suspend-to-disk.
  307. */
  308. static void suspend_finish(void)
  309. {
  310. suspend_thaw_processes();
  311. pm_notifier_call_chain(PM_POST_SUSPEND);
  312. pm_restore_console();
  313. }
  314. static const char * const pm_states[PM_SUSPEND_MAX] = {
  315. [PM_SUSPEND_STANDBY] = "standby",
  316. [PM_SUSPEND_MEM] = "mem",
  317. };
  318. static inline int valid_state(suspend_state_t state)
  319. {
  320. /* All states need lowlevel support and need to be valid
  321. * to the lowlevel implementation, no valid callback
  322. * implies that none are valid. */
  323. if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
  324. return 0;
  325. return 1;
  326. }
  327. /**
  328. * enter_state - Do common work of entering low-power state.
  329. * @state: pm_state structure for state we're entering.
  330. *
  331. * Make sure we're the only ones trying to enter a sleep state. Fail
  332. * if someone has beat us to it, since we don't want anything weird to
  333. * happen when we wake up.
  334. * Then, do the setup for suspend, enter the state, and cleaup (after
  335. * we've woken up).
  336. */
  337. static int enter_state(suspend_state_t state)
  338. {
  339. int error;
  340. if (!valid_state(state))
  341. return -ENODEV;
  342. if (!mutex_trylock(&pm_mutex))
  343. return -EBUSY;
  344. printk(KERN_INFO "PM: Syncing filesystems ... ");
  345. sys_sync();
  346. printk("done.\n");
  347. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  348. error = suspend_prepare();
  349. if (error)
  350. goto Unlock;
  351. if (suspend_test(TEST_FREEZER))
  352. goto Finish;
  353. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  354. error = suspend_devices_and_enter(state);
  355. Finish:
  356. pr_debug("PM: Finishing wakeup.\n");
  357. suspend_finish();
  358. Unlock:
  359. mutex_unlock(&pm_mutex);
  360. return error;
  361. }
  362. /**
  363. * pm_suspend - Externally visible function for suspending system.
  364. * @state: Enumerated value of state to enter.
  365. *
  366. * Determine whether or not value is within range, get state
  367. * structure, and enter (above).
  368. */
  369. int pm_suspend(suspend_state_t state)
  370. {
  371. if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
  372. return enter_state(state);
  373. return -EINVAL;
  374. }
  375. EXPORT_SYMBOL(pm_suspend);
  376. #endif /* CONFIG_SUSPEND */
  377. struct kobject *power_kobj;
  378. /**
  379. * state - control system power state.
  380. *
  381. * show() returns what states are supported, which is hard-coded to
  382. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  383. * 'disk' (Suspend-to-Disk).
  384. *
  385. * store() accepts one of those strings, translates it into the
  386. * proper enumerated value, and initiates a suspend transition.
  387. */
  388. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  389. char *buf)
  390. {
  391. char *s = buf;
  392. #ifdef CONFIG_SUSPEND
  393. int i;
  394. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  395. if (pm_states[i] && valid_state(i))
  396. s += sprintf(s,"%s ", pm_states[i]);
  397. }
  398. #endif
  399. #ifdef CONFIG_HIBERNATION
  400. s += sprintf(s, "%s\n", "disk");
  401. #else
  402. if (s != buf)
  403. /* convert the last space to a newline */
  404. *(s-1) = '\n';
  405. #endif
  406. return (s - buf);
  407. }
  408. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  409. const char *buf, size_t n)
  410. {
  411. #ifdef CONFIG_SUSPEND
  412. suspend_state_t state = PM_SUSPEND_STANDBY;
  413. const char * const *s;
  414. #endif
  415. char *p;
  416. int len;
  417. int error = -EINVAL;
  418. p = memchr(buf, '\n', n);
  419. len = p ? p - buf : n;
  420. /* First, check if we are requested to hibernate */
  421. if (len == 4 && !strncmp(buf, "disk", len)) {
  422. error = hibernate();
  423. goto Exit;
  424. }
  425. #ifdef CONFIG_SUSPEND
  426. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  427. if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
  428. break;
  429. }
  430. if (state < PM_SUSPEND_MAX && *s)
  431. error = enter_state(state);
  432. #endif
  433. Exit:
  434. return error ? error : n;
  435. }
  436. power_attr(state);
  437. #ifdef CONFIG_PM_TRACE
  438. int pm_trace_enabled;
  439. static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
  440. char *buf)
  441. {
  442. return sprintf(buf, "%d\n", pm_trace_enabled);
  443. }
  444. static ssize_t
  445. pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
  446. const char *buf, size_t n)
  447. {
  448. int val;
  449. if (sscanf(buf, "%d", &val) == 1) {
  450. pm_trace_enabled = !!val;
  451. return n;
  452. }
  453. return -EINVAL;
  454. }
  455. power_attr(pm_trace);
  456. #endif /* CONFIG_PM_TRACE */
  457. static struct attribute * g[] = {
  458. &state_attr.attr,
  459. #ifdef CONFIG_PM_TRACE
  460. &pm_trace_attr.attr,
  461. #endif
  462. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
  463. &pm_test_attr.attr,
  464. #endif
  465. NULL,
  466. };
  467. static struct attribute_group attr_group = {
  468. .attrs = g,
  469. };
  470. static int __init pm_init(void)
  471. {
  472. power_kobj = kobject_create_and_add("power", NULL);
  473. if (!power_kobj)
  474. return -ENOMEM;
  475. return sysfs_create_group(power_kobj, &attr_group);
  476. }
  477. core_initcall(pm_init);
  478. #ifdef CONFIG_PM_TEST_SUSPEND
  479. #include <linux/rtc.h>
  480. /*
  481. * To test system suspend, we need a hands-off mechanism to resume the
  482. * system. RTCs wake alarms are a common self-contained mechanism.
  483. */
  484. static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
  485. {
  486. static char err_readtime[] __initdata =
  487. KERN_ERR "PM: can't read %s time, err %d\n";
  488. static char err_wakealarm [] __initdata =
  489. KERN_ERR "PM: can't set %s wakealarm, err %d\n";
  490. static char err_suspend[] __initdata =
  491. KERN_ERR "PM: suspend test failed, error %d\n";
  492. static char info_test[] __initdata =
  493. KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";
  494. unsigned long now;
  495. struct rtc_wkalrm alm;
  496. int status;
  497. /* this may fail if the RTC hasn't been initialized */
  498. status = rtc_read_time(rtc, &alm.time);
  499. if (status < 0) {
  500. printk(err_readtime, rtc->dev.bus_id, status);
  501. return;
  502. }
  503. rtc_tm_to_time(&alm.time, &now);
  504. memset(&alm, 0, sizeof alm);
  505. rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
  506. alm.enabled = true;
  507. status = rtc_set_alarm(rtc, &alm);
  508. if (status < 0) {
  509. printk(err_wakealarm, rtc->dev.bus_id, status);
  510. return;
  511. }
  512. if (state == PM_SUSPEND_MEM) {
  513. printk(info_test, pm_states[state]);
  514. status = pm_suspend(state);
  515. if (status == -ENODEV)
  516. state = PM_SUSPEND_STANDBY;
  517. }
  518. if (state == PM_SUSPEND_STANDBY) {
  519. printk(info_test, pm_states[state]);
  520. status = pm_suspend(state);
  521. }
  522. if (status < 0)
  523. printk(err_suspend, status);
  524. }
  525. static int __init has_wakealarm(struct device *dev, void *name_ptr)
  526. {
  527. struct rtc_device *candidate = to_rtc_device(dev);
  528. if (!candidate->ops->set_alarm)
  529. return 0;
  530. if (!device_may_wakeup(candidate->dev.parent))
  531. return 0;
  532. *(char **)name_ptr = dev->bus_id;
  533. return 1;
  534. }
  535. /*
  536. * Kernel options like "test_suspend=mem" force suspend/resume sanity tests
  537. * at startup time. They're normally disabled, for faster boot and because
  538. * we can't know which states really work on this particular system.
  539. */
  540. static suspend_state_t test_state __initdata = PM_SUSPEND_ON;
  541. static char warn_bad_state[] __initdata =
  542. KERN_WARNING "PM: can't test '%s' suspend state\n";
  543. static int __init setup_test_suspend(char *value)
  544. {
  545. unsigned i;
  546. /* "=mem" ==> "mem" */
  547. value++;
  548. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  549. if (!pm_states[i])
  550. continue;
  551. if (strcmp(pm_states[i], value) != 0)
  552. continue;
  553. test_state = (__force suspend_state_t) i;
  554. return 0;
  555. }
  556. printk(warn_bad_state, value);
  557. return 0;
  558. }
  559. __setup("test_suspend", setup_test_suspend);
  560. static int __init test_suspend(void)
  561. {
  562. static char warn_no_rtc[] __initdata =
  563. KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
  564. char *pony = NULL;
  565. struct rtc_device *rtc = NULL;
  566. /* PM is initialized by now; is that state testable? */
  567. if (test_state == PM_SUSPEND_ON)
  568. goto done;
  569. if (!valid_state(test_state)) {
  570. printk(warn_bad_state, pm_states[test_state]);
  571. goto done;
  572. }
  573. /* RTCs have initialized by now too ... can we use one? */
  574. class_find_device(rtc_class, NULL, &pony, has_wakealarm);
  575. if (pony)
  576. rtc = rtc_class_open(pony);
  577. if (!rtc) {
  578. printk(warn_no_rtc);
  579. goto done;
  580. }
  581. /* go for it */
  582. test_wakealarm(rtc, test_state);
  583. rtc_class_close(rtc);
  584. done:
  585. return 0;
  586. }
  587. late_initcall(test_suspend);
  588. #endif /* CONFIG_PM_TEST_SUSPEND */