main.c 16 KB

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