main.c 16 KB

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