hibernate.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
  7. * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
  8. * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
  9. *
  10. * This file is released under the GPLv2.
  11. */
  12. #include <linux/export.h>
  13. #include <linux/suspend.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/reboot.h>
  16. #include <linux/string.h>
  17. #include <linux/device.h>
  18. #include <linux/async.h>
  19. #include <linux/delay.h>
  20. #include <linux/fs.h>
  21. #include <linux/mount.h>
  22. #include <linux/pm.h>
  23. #include <linux/console.h>
  24. #include <linux/cpu.h>
  25. #include <linux/freezer.h>
  26. #include <linux/gfp.h>
  27. #include <linux/syscore_ops.h>
  28. #include <linux/ctype.h>
  29. #include <linux/genhd.h>
  30. #include "power.h"
  31. static int nocompress;
  32. static int noresume;
  33. static int resume_wait;
  34. static int resume_delay;
  35. static char resume_file[256] = CONFIG_PM_STD_PARTITION;
  36. dev_t swsusp_resume_device;
  37. sector_t swsusp_resume_block;
  38. int in_suspend __nosavedata;
  39. enum {
  40. HIBERNATION_INVALID,
  41. HIBERNATION_PLATFORM,
  42. HIBERNATION_SHUTDOWN,
  43. HIBERNATION_REBOOT,
  44. #ifdef CONFIG_SUSPEND
  45. HIBERNATION_SUSPEND,
  46. #endif
  47. /* keep last */
  48. __HIBERNATION_AFTER_LAST
  49. };
  50. #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
  51. #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
  52. static int hibernation_mode = HIBERNATION_SHUTDOWN;
  53. bool freezer_test_done;
  54. static const struct platform_hibernation_ops *hibernation_ops;
  55. /**
  56. * hibernation_set_ops - Set the global hibernate operations.
  57. * @ops: Hibernation operations to use in subsequent hibernation transitions.
  58. */
  59. void hibernation_set_ops(const struct platform_hibernation_ops *ops)
  60. {
  61. if (ops && !(ops->begin && ops->end && ops->pre_snapshot
  62. && ops->prepare && ops->finish && ops->enter && ops->pre_restore
  63. && ops->restore_cleanup && ops->leave)) {
  64. WARN_ON(1);
  65. return;
  66. }
  67. lock_system_sleep();
  68. hibernation_ops = ops;
  69. if (ops)
  70. hibernation_mode = HIBERNATION_PLATFORM;
  71. else if (hibernation_mode == HIBERNATION_PLATFORM)
  72. hibernation_mode = HIBERNATION_SHUTDOWN;
  73. unlock_system_sleep();
  74. }
  75. static bool entering_platform_hibernation;
  76. bool system_entering_hibernation(void)
  77. {
  78. return entering_platform_hibernation;
  79. }
  80. EXPORT_SYMBOL(system_entering_hibernation);
  81. #ifdef CONFIG_PM_DEBUG
  82. static void hibernation_debug_sleep(void)
  83. {
  84. printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
  85. mdelay(5000);
  86. }
  87. static int hibernation_test(int level)
  88. {
  89. if (pm_test_level == level) {
  90. hibernation_debug_sleep();
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. #else /* !CONFIG_PM_DEBUG */
  96. static int hibernation_test(int level) { return 0; }
  97. #endif /* !CONFIG_PM_DEBUG */
  98. /**
  99. * platform_begin - Call platform to start hibernation.
  100. * @platform_mode: Whether or not to use the platform driver.
  101. */
  102. static int platform_begin(int platform_mode)
  103. {
  104. return (platform_mode && hibernation_ops) ?
  105. hibernation_ops->begin() : 0;
  106. }
  107. /**
  108. * platform_end - Call platform to finish transition to the working state.
  109. * @platform_mode: Whether or not to use the platform driver.
  110. */
  111. static void platform_end(int platform_mode)
  112. {
  113. if (platform_mode && hibernation_ops)
  114. hibernation_ops->end();
  115. }
  116. /**
  117. * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
  118. * @platform_mode: Whether or not to use the platform driver.
  119. *
  120. * Use the platform driver to prepare the system for creating a hibernate image,
  121. * if so configured, and return an error code if that fails.
  122. */
  123. static int platform_pre_snapshot(int platform_mode)
  124. {
  125. return (platform_mode && hibernation_ops) ?
  126. hibernation_ops->pre_snapshot() : 0;
  127. }
  128. /**
  129. * platform_leave - Call platform to prepare a transition to the working state.
  130. * @platform_mode: Whether or not to use the platform driver.
  131. *
  132. * Use the platform driver prepare to prepare the machine for switching to the
  133. * normal mode of operation.
  134. *
  135. * This routine is called on one CPU with interrupts disabled.
  136. */
  137. static void platform_leave(int platform_mode)
  138. {
  139. if (platform_mode && hibernation_ops)
  140. hibernation_ops->leave();
  141. }
  142. /**
  143. * platform_finish - Call platform to switch the system to the working state.
  144. * @platform_mode: Whether or not to use the platform driver.
  145. *
  146. * Use the platform driver to switch the machine to the normal mode of
  147. * operation.
  148. *
  149. * This routine must be called after platform_prepare().
  150. */
  151. static void platform_finish(int platform_mode)
  152. {
  153. if (platform_mode && hibernation_ops)
  154. hibernation_ops->finish();
  155. }
  156. /**
  157. * platform_pre_restore - Prepare for hibernate image restoration.
  158. * @platform_mode: Whether or not to use the platform driver.
  159. *
  160. * Use the platform driver to prepare the system for resume from a hibernation
  161. * image.
  162. *
  163. * If the restore fails after this function has been called,
  164. * platform_restore_cleanup() must be called.
  165. */
  166. static int platform_pre_restore(int platform_mode)
  167. {
  168. return (platform_mode && hibernation_ops) ?
  169. hibernation_ops->pre_restore() : 0;
  170. }
  171. /**
  172. * platform_restore_cleanup - Switch to the working state after failing restore.
  173. * @platform_mode: Whether or not to use the platform driver.
  174. *
  175. * Use the platform driver to switch the system to the normal mode of operation
  176. * after a failing restore.
  177. *
  178. * If platform_pre_restore() has been called before the failing restore, this
  179. * function must be called too, regardless of the result of
  180. * platform_pre_restore().
  181. */
  182. static void platform_restore_cleanup(int platform_mode)
  183. {
  184. if (platform_mode && hibernation_ops)
  185. hibernation_ops->restore_cleanup();
  186. }
  187. /**
  188. * platform_recover - Recover from a failure to suspend devices.
  189. * @platform_mode: Whether or not to use the platform driver.
  190. */
  191. static void platform_recover(int platform_mode)
  192. {
  193. if (platform_mode && hibernation_ops && hibernation_ops->recover)
  194. hibernation_ops->recover();
  195. }
  196. /**
  197. * swsusp_show_speed - Print time elapsed between two events during hibernation.
  198. * @start: Starting event.
  199. * @stop: Final event.
  200. * @nr_pages: Number of memory pages processed between @start and @stop.
  201. * @msg: Additional diagnostic message to print.
  202. */
  203. void swsusp_show_speed(struct timeval *start, struct timeval *stop,
  204. unsigned nr_pages, char *msg)
  205. {
  206. s64 elapsed_centisecs64;
  207. int centisecs;
  208. int k;
  209. int kps;
  210. elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
  211. do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
  212. centisecs = elapsed_centisecs64;
  213. if (centisecs == 0)
  214. centisecs = 1; /* avoid div-by-zero */
  215. k = nr_pages * (PAGE_SIZE / 1024);
  216. kps = (k * 100) / centisecs;
  217. printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
  218. msg, k,
  219. centisecs / 100, centisecs % 100,
  220. kps / 1000, (kps % 1000) / 10);
  221. }
  222. /**
  223. * create_image - Create a hibernation image.
  224. * @platform_mode: Whether or not to use the platform driver.
  225. *
  226. * Execute device drivers' "late" and "noirq" freeze callbacks, create a
  227. * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
  228. *
  229. * Control reappears in this routine after the subsequent restore.
  230. */
  231. static int create_image(int platform_mode)
  232. {
  233. int error;
  234. error = dpm_suspend_end(PMSG_FREEZE);
  235. if (error) {
  236. printk(KERN_ERR "PM: Some devices failed to power down, "
  237. "aborting hibernation\n");
  238. return error;
  239. }
  240. error = platform_pre_snapshot(platform_mode);
  241. if (error || hibernation_test(TEST_PLATFORM))
  242. goto Platform_finish;
  243. error = disable_nonboot_cpus();
  244. if (error || hibernation_test(TEST_CPUS))
  245. goto Enable_cpus;
  246. local_irq_disable();
  247. error = syscore_suspend();
  248. if (error) {
  249. printk(KERN_ERR "PM: Some system devices failed to power down, "
  250. "aborting hibernation\n");
  251. goto Enable_irqs;
  252. }
  253. if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
  254. goto Power_up;
  255. in_suspend = 1;
  256. save_processor_state();
  257. error = swsusp_arch_suspend();
  258. if (error)
  259. printk(KERN_ERR "PM: Error %d creating hibernation image\n",
  260. error);
  261. /* Restore control flow magically appears here */
  262. restore_processor_state();
  263. if (!in_suspend) {
  264. events_check_enabled = false;
  265. platform_leave(platform_mode);
  266. }
  267. Power_up:
  268. syscore_resume();
  269. Enable_irqs:
  270. local_irq_enable();
  271. Enable_cpus:
  272. enable_nonboot_cpus();
  273. Platform_finish:
  274. platform_finish(platform_mode);
  275. dpm_resume_start(in_suspend ?
  276. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  277. return error;
  278. }
  279. /**
  280. * hibernation_snapshot - Quiesce devices and create a hibernation image.
  281. * @platform_mode: If set, use platform driver to prepare for the transition.
  282. *
  283. * This routine must be called with pm_mutex held.
  284. */
  285. int hibernation_snapshot(int platform_mode)
  286. {
  287. pm_message_t msg;
  288. int error;
  289. error = platform_begin(platform_mode);
  290. if (error)
  291. goto Close;
  292. /* Preallocate image memory before shutting down devices. */
  293. error = hibernate_preallocate_memory();
  294. if (error)
  295. goto Close;
  296. error = freeze_kernel_threads();
  297. if (error)
  298. goto Cleanup;
  299. if (hibernation_test(TEST_FREEZER)) {
  300. /*
  301. * Indicate to the caller that we are returning due to a
  302. * successful freezer test.
  303. */
  304. freezer_test_done = true;
  305. goto Thaw;
  306. }
  307. error = dpm_prepare(PMSG_FREEZE);
  308. if (error) {
  309. dpm_complete(PMSG_RECOVER);
  310. goto Thaw;
  311. }
  312. suspend_console();
  313. ftrace_stop();
  314. pm_restrict_gfp_mask();
  315. error = dpm_suspend(PMSG_FREEZE);
  316. if (error || hibernation_test(TEST_DEVICES))
  317. platform_recover(platform_mode);
  318. else
  319. error = create_image(platform_mode);
  320. /*
  321. * In the case that we call create_image() above, the control
  322. * returns here (1) after the image has been created or the
  323. * image creation has failed and (2) after a successful restore.
  324. */
  325. /* We may need to release the preallocated image pages here. */
  326. if (error || !in_suspend)
  327. swsusp_free();
  328. msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
  329. dpm_resume(msg);
  330. if (error || !in_suspend)
  331. pm_restore_gfp_mask();
  332. ftrace_start();
  333. resume_console();
  334. dpm_complete(msg);
  335. Close:
  336. platform_end(platform_mode);
  337. return error;
  338. Thaw:
  339. thaw_kernel_threads();
  340. Cleanup:
  341. swsusp_free();
  342. goto Close;
  343. }
  344. /**
  345. * resume_target_kernel - Restore system state from a hibernation image.
  346. * @platform_mode: Whether or not to use the platform driver.
  347. *
  348. * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
  349. * contents of highmem that have not been restored yet from the image and run
  350. * the low-level code that will restore the remaining contents of memory and
  351. * switch to the just restored target kernel.
  352. */
  353. static int resume_target_kernel(bool platform_mode)
  354. {
  355. int error;
  356. error = dpm_suspend_end(PMSG_QUIESCE);
  357. if (error) {
  358. printk(KERN_ERR "PM: Some devices failed to power down, "
  359. "aborting resume\n");
  360. return error;
  361. }
  362. error = platform_pre_restore(platform_mode);
  363. if (error)
  364. goto Cleanup;
  365. error = disable_nonboot_cpus();
  366. if (error)
  367. goto Enable_cpus;
  368. local_irq_disable();
  369. error = syscore_suspend();
  370. if (error)
  371. goto Enable_irqs;
  372. save_processor_state();
  373. error = restore_highmem();
  374. if (!error) {
  375. error = swsusp_arch_resume();
  376. /*
  377. * The code below is only ever reached in case of a failure.
  378. * Otherwise, execution continues at the place where
  379. * swsusp_arch_suspend() was called.
  380. */
  381. BUG_ON(!error);
  382. /*
  383. * This call to restore_highmem() reverts the changes made by
  384. * the previous one.
  385. */
  386. restore_highmem();
  387. }
  388. /*
  389. * The only reason why swsusp_arch_resume() can fail is memory being
  390. * very tight, so we have to free it as soon as we can to avoid
  391. * subsequent failures.
  392. */
  393. swsusp_free();
  394. restore_processor_state();
  395. touch_softlockup_watchdog();
  396. syscore_resume();
  397. Enable_irqs:
  398. local_irq_enable();
  399. Enable_cpus:
  400. enable_nonboot_cpus();
  401. Cleanup:
  402. platform_restore_cleanup(platform_mode);
  403. dpm_resume_start(PMSG_RECOVER);
  404. return error;
  405. }
  406. /**
  407. * hibernation_restore - Quiesce devices and restore from a hibernation image.
  408. * @platform_mode: If set, use platform driver to prepare for the transition.
  409. *
  410. * This routine must be called with pm_mutex held. If it is successful, control
  411. * reappears in the restored target kernel in hibernation_snapshot().
  412. */
  413. int hibernation_restore(int platform_mode)
  414. {
  415. int error;
  416. pm_prepare_console();
  417. suspend_console();
  418. ftrace_stop();
  419. pm_restrict_gfp_mask();
  420. error = dpm_suspend_start(PMSG_QUIESCE);
  421. if (!error) {
  422. error = resume_target_kernel(platform_mode);
  423. dpm_resume_end(PMSG_RECOVER);
  424. }
  425. pm_restore_gfp_mask();
  426. ftrace_start();
  427. resume_console();
  428. pm_restore_console();
  429. return error;
  430. }
  431. /**
  432. * hibernation_platform_enter - Power off the system using the platform driver.
  433. */
  434. int hibernation_platform_enter(void)
  435. {
  436. int error;
  437. if (!hibernation_ops)
  438. return -ENOSYS;
  439. /*
  440. * We have cancelled the power transition by running
  441. * hibernation_ops->finish() before saving the image, so we should let
  442. * the firmware know that we're going to enter the sleep state after all
  443. */
  444. error = hibernation_ops->begin();
  445. if (error)
  446. goto Close;
  447. entering_platform_hibernation = true;
  448. suspend_console();
  449. ftrace_stop();
  450. error = dpm_suspend_start(PMSG_HIBERNATE);
  451. if (error) {
  452. if (hibernation_ops->recover)
  453. hibernation_ops->recover();
  454. goto Resume_devices;
  455. }
  456. error = dpm_suspend_end(PMSG_HIBERNATE);
  457. if (error)
  458. goto Resume_devices;
  459. error = hibernation_ops->prepare();
  460. if (error)
  461. goto Platform_finish;
  462. error = disable_nonboot_cpus();
  463. if (error)
  464. goto Platform_finish;
  465. local_irq_disable();
  466. syscore_suspend();
  467. if (pm_wakeup_pending()) {
  468. error = -EAGAIN;
  469. goto Power_up;
  470. }
  471. hibernation_ops->enter();
  472. /* We should never get here */
  473. while (1);
  474. Power_up:
  475. syscore_resume();
  476. local_irq_enable();
  477. enable_nonboot_cpus();
  478. Platform_finish:
  479. hibernation_ops->finish();
  480. dpm_resume_start(PMSG_RESTORE);
  481. Resume_devices:
  482. entering_platform_hibernation = false;
  483. dpm_resume_end(PMSG_RESTORE);
  484. ftrace_start();
  485. resume_console();
  486. Close:
  487. hibernation_ops->end();
  488. return error;
  489. }
  490. /**
  491. * power_down - Shut the machine down for hibernation.
  492. *
  493. * Use the platform driver, if configured, to put the system into the sleep
  494. * state corresponding to hibernation, or try to power it off or reboot,
  495. * depending on the value of hibernation_mode.
  496. */
  497. static void power_down(void)
  498. {
  499. #ifdef CONFIG_SUSPEND
  500. int error;
  501. #endif
  502. switch (hibernation_mode) {
  503. case HIBERNATION_REBOOT:
  504. kernel_restart(NULL);
  505. break;
  506. case HIBERNATION_PLATFORM:
  507. hibernation_platform_enter();
  508. case HIBERNATION_SHUTDOWN:
  509. kernel_power_off();
  510. break;
  511. #ifdef CONFIG_SUSPEND
  512. case HIBERNATION_SUSPEND:
  513. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  514. if (error) {
  515. if (hibernation_ops)
  516. hibernation_mode = HIBERNATION_PLATFORM;
  517. else
  518. hibernation_mode = HIBERNATION_SHUTDOWN;
  519. power_down();
  520. }
  521. /*
  522. * Restore swap signature.
  523. */
  524. error = swsusp_unmark();
  525. if (error)
  526. printk(KERN_ERR "PM: Swap will be unusable! "
  527. "Try swapon -a.\n");
  528. return;
  529. #endif
  530. }
  531. kernel_halt();
  532. /*
  533. * Valid image is on the disk, if we continue we risk serious data
  534. * corruption after resume.
  535. */
  536. printk(KERN_CRIT "PM: Please power down manually\n");
  537. while(1);
  538. }
  539. /**
  540. * hibernate - Carry out system hibernation, including saving the image.
  541. */
  542. int hibernate(void)
  543. {
  544. int error;
  545. lock_system_sleep();
  546. /* The snapshot device should not be opened while we're running */
  547. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  548. error = -EBUSY;
  549. goto Unlock;
  550. }
  551. pm_prepare_console();
  552. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  553. if (error)
  554. goto Exit;
  555. /* Allocate memory management structures */
  556. error = create_basic_memory_bitmaps();
  557. if (error)
  558. goto Exit;
  559. printk(KERN_INFO "PM: Syncing filesystems ... ");
  560. sys_sync();
  561. printk("done.\n");
  562. error = freeze_processes();
  563. if (error)
  564. goto Free_bitmaps;
  565. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  566. if (error || freezer_test_done)
  567. goto Thaw;
  568. if (in_suspend) {
  569. unsigned int flags = 0;
  570. if (hibernation_mode == HIBERNATION_PLATFORM)
  571. flags |= SF_PLATFORM_MODE;
  572. if (nocompress)
  573. flags |= SF_NOCOMPRESS_MODE;
  574. else
  575. flags |= SF_CRC32_MODE;
  576. pr_debug("PM: writing image.\n");
  577. error = swsusp_write(flags);
  578. swsusp_free();
  579. if (!error)
  580. power_down();
  581. in_suspend = 0;
  582. pm_restore_gfp_mask();
  583. } else {
  584. pr_debug("PM: Image restored successfully.\n");
  585. }
  586. Thaw:
  587. thaw_processes();
  588. /* Don't bother checking whether freezer_test_done is true */
  589. freezer_test_done = false;
  590. Free_bitmaps:
  591. free_basic_memory_bitmaps();
  592. Exit:
  593. pm_notifier_call_chain(PM_POST_HIBERNATION);
  594. pm_restore_console();
  595. atomic_inc(&snapshot_device_available);
  596. Unlock:
  597. unlock_system_sleep();
  598. return error;
  599. }
  600. /**
  601. * software_resume - Resume from a saved hibernation image.
  602. *
  603. * This routine is called as a late initcall, when all devices have been
  604. * discovered and initialized already.
  605. *
  606. * The image reading code is called to see if there is a hibernation image
  607. * available for reading. If that is the case, devices are quiesced and the
  608. * contents of memory is restored from the saved image.
  609. *
  610. * If this is successful, control reappears in the restored target kernel in
  611. * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
  612. * attempts to recover gracefully and make the kernel return to the normal mode
  613. * of operation.
  614. */
  615. static int software_resume(void)
  616. {
  617. int error;
  618. unsigned int flags;
  619. /*
  620. * If the user said "noresume".. bail out early.
  621. */
  622. if (noresume)
  623. return 0;
  624. /*
  625. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  626. * is configured into the kernel. Since the regular hibernate
  627. * trigger path is via sysfs which takes a buffer mutex before
  628. * calling hibernate functions (which take pm_mutex) this can
  629. * cause lockdep to complain about a possible ABBA deadlock
  630. * which cannot happen since we're in the boot code here and
  631. * sysfs can't be invoked yet. Therefore, we use a subclass
  632. * here to avoid lockdep complaining.
  633. */
  634. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  635. if (swsusp_resume_device)
  636. goto Check_image;
  637. if (!strlen(resume_file)) {
  638. error = -ENOENT;
  639. goto Unlock;
  640. }
  641. pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
  642. if (resume_delay) {
  643. printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
  644. resume_delay);
  645. ssleep(resume_delay);
  646. }
  647. /* Check if the device is there */
  648. swsusp_resume_device = name_to_dev_t(resume_file);
  649. /*
  650. * name_to_dev_t is ineffective to verify parition if resume_file is in
  651. * integer format. (e.g. major:minor)
  652. */
  653. if (isdigit(resume_file[0]) && resume_wait) {
  654. int partno;
  655. while (!get_gendisk(swsusp_resume_device, &partno))
  656. msleep(10);
  657. }
  658. if (!swsusp_resume_device) {
  659. /*
  660. * Some device discovery might still be in progress; we need
  661. * to wait for this to finish.
  662. */
  663. wait_for_device_probe();
  664. if (resume_wait) {
  665. while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
  666. msleep(10);
  667. async_synchronize_full();
  668. }
  669. swsusp_resume_device = name_to_dev_t(resume_file);
  670. if (!swsusp_resume_device) {
  671. error = -ENODEV;
  672. goto Unlock;
  673. }
  674. }
  675. Check_image:
  676. pr_debug("PM: Hibernation image partition %d:%d present\n",
  677. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  678. pr_debug("PM: Looking for hibernation image.\n");
  679. error = swsusp_check();
  680. if (error)
  681. goto Unlock;
  682. /* The snapshot device should not be opened while we're running */
  683. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  684. error = -EBUSY;
  685. swsusp_close(FMODE_READ);
  686. goto Unlock;
  687. }
  688. pm_prepare_console();
  689. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  690. if (error)
  691. goto close_finish;
  692. error = create_basic_memory_bitmaps();
  693. if (error)
  694. goto close_finish;
  695. pr_debug("PM: Preparing processes for restore.\n");
  696. error = freeze_processes();
  697. if (error) {
  698. swsusp_close(FMODE_READ);
  699. goto Done;
  700. }
  701. pr_debug("PM: Loading hibernation image.\n");
  702. error = swsusp_read(&flags);
  703. swsusp_close(FMODE_READ);
  704. if (!error)
  705. hibernation_restore(flags & SF_PLATFORM_MODE);
  706. printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
  707. swsusp_free();
  708. thaw_processes();
  709. Done:
  710. free_basic_memory_bitmaps();
  711. Finish:
  712. pm_notifier_call_chain(PM_POST_RESTORE);
  713. pm_restore_console();
  714. atomic_inc(&snapshot_device_available);
  715. /* For success case, the suspend path will release the lock */
  716. Unlock:
  717. mutex_unlock(&pm_mutex);
  718. pr_debug("PM: Hibernation image not present or could not be loaded.\n");
  719. return error;
  720. close_finish:
  721. swsusp_close(FMODE_READ);
  722. goto Finish;
  723. }
  724. late_initcall(software_resume);
  725. static const char * const hibernation_modes[] = {
  726. [HIBERNATION_PLATFORM] = "platform",
  727. [HIBERNATION_SHUTDOWN] = "shutdown",
  728. [HIBERNATION_REBOOT] = "reboot",
  729. #ifdef CONFIG_SUSPEND
  730. [HIBERNATION_SUSPEND] = "suspend",
  731. #endif
  732. };
  733. /*
  734. * /sys/power/disk - Control hibernation mode.
  735. *
  736. * Hibernation can be handled in several ways. There are a few different ways
  737. * to put the system into the sleep state: using the platform driver (e.g. ACPI
  738. * or other hibernation_ops), powering it off or rebooting it (for testing
  739. * mostly).
  740. *
  741. * The sysfs file /sys/power/disk provides an interface for selecting the
  742. * hibernation mode to use. Reading from this file causes the available modes
  743. * to be printed. There are 3 modes that can be supported:
  744. *
  745. * 'platform'
  746. * 'shutdown'
  747. * 'reboot'
  748. *
  749. * If a platform hibernation driver is in use, 'platform' will be supported
  750. * and will be used by default. Otherwise, 'shutdown' will be used by default.
  751. * The selected option (i.e. the one corresponding to the current value of
  752. * hibernation_mode) is enclosed by a square bracket.
  753. *
  754. * To select a given hibernation mode it is necessary to write the mode's
  755. * string representation (as returned by reading from /sys/power/disk) back
  756. * into /sys/power/disk.
  757. */
  758. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  759. char *buf)
  760. {
  761. int i;
  762. char *start = buf;
  763. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  764. if (!hibernation_modes[i])
  765. continue;
  766. switch (i) {
  767. case HIBERNATION_SHUTDOWN:
  768. case HIBERNATION_REBOOT:
  769. #ifdef CONFIG_SUSPEND
  770. case HIBERNATION_SUSPEND:
  771. #endif
  772. break;
  773. case HIBERNATION_PLATFORM:
  774. if (hibernation_ops)
  775. break;
  776. /* not a valid mode, continue with loop */
  777. continue;
  778. }
  779. if (i == hibernation_mode)
  780. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  781. else
  782. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  783. }
  784. buf += sprintf(buf, "\n");
  785. return buf-start;
  786. }
  787. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  788. const char *buf, size_t n)
  789. {
  790. int error = 0;
  791. int i;
  792. int len;
  793. char *p;
  794. int mode = HIBERNATION_INVALID;
  795. p = memchr(buf, '\n', n);
  796. len = p ? p - buf : n;
  797. lock_system_sleep();
  798. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  799. if (len == strlen(hibernation_modes[i])
  800. && !strncmp(buf, hibernation_modes[i], len)) {
  801. mode = i;
  802. break;
  803. }
  804. }
  805. if (mode != HIBERNATION_INVALID) {
  806. switch (mode) {
  807. case HIBERNATION_SHUTDOWN:
  808. case HIBERNATION_REBOOT:
  809. #ifdef CONFIG_SUSPEND
  810. case HIBERNATION_SUSPEND:
  811. #endif
  812. hibernation_mode = mode;
  813. break;
  814. case HIBERNATION_PLATFORM:
  815. if (hibernation_ops)
  816. hibernation_mode = mode;
  817. else
  818. error = -EINVAL;
  819. }
  820. } else
  821. error = -EINVAL;
  822. if (!error)
  823. pr_debug("PM: Hibernation mode set to '%s'\n",
  824. hibernation_modes[mode]);
  825. unlock_system_sleep();
  826. return error ? error : n;
  827. }
  828. power_attr(disk);
  829. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  830. char *buf)
  831. {
  832. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  833. MINOR(swsusp_resume_device));
  834. }
  835. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  836. const char *buf, size_t n)
  837. {
  838. unsigned int maj, min;
  839. dev_t res;
  840. int ret = -EINVAL;
  841. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  842. goto out;
  843. res = MKDEV(maj,min);
  844. if (maj != MAJOR(res) || min != MINOR(res))
  845. goto out;
  846. lock_system_sleep();
  847. swsusp_resume_device = res;
  848. unlock_system_sleep();
  849. printk(KERN_INFO "PM: Starting manual resume from disk\n");
  850. noresume = 0;
  851. software_resume();
  852. ret = n;
  853. out:
  854. return ret;
  855. }
  856. power_attr(resume);
  857. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  858. char *buf)
  859. {
  860. return sprintf(buf, "%lu\n", image_size);
  861. }
  862. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  863. const char *buf, size_t n)
  864. {
  865. unsigned long size;
  866. if (sscanf(buf, "%lu", &size) == 1) {
  867. image_size = size;
  868. return n;
  869. }
  870. return -EINVAL;
  871. }
  872. power_attr(image_size);
  873. static ssize_t reserved_size_show(struct kobject *kobj,
  874. struct kobj_attribute *attr, char *buf)
  875. {
  876. return sprintf(buf, "%lu\n", reserved_size);
  877. }
  878. static ssize_t reserved_size_store(struct kobject *kobj,
  879. struct kobj_attribute *attr,
  880. const char *buf, size_t n)
  881. {
  882. unsigned long size;
  883. if (sscanf(buf, "%lu", &size) == 1) {
  884. reserved_size = size;
  885. return n;
  886. }
  887. return -EINVAL;
  888. }
  889. power_attr(reserved_size);
  890. static struct attribute * g[] = {
  891. &disk_attr.attr,
  892. &resume_attr.attr,
  893. &image_size_attr.attr,
  894. &reserved_size_attr.attr,
  895. NULL,
  896. };
  897. static struct attribute_group attr_group = {
  898. .attrs = g,
  899. };
  900. static int __init pm_disk_init(void)
  901. {
  902. return sysfs_create_group(power_kobj, &attr_group);
  903. }
  904. core_initcall(pm_disk_init);
  905. static int __init resume_setup(char *str)
  906. {
  907. if (noresume)
  908. return 1;
  909. strncpy( resume_file, str, 255 );
  910. return 1;
  911. }
  912. static int __init resume_offset_setup(char *str)
  913. {
  914. unsigned long long offset;
  915. if (noresume)
  916. return 1;
  917. if (sscanf(str, "%llu", &offset) == 1)
  918. swsusp_resume_block = offset;
  919. return 1;
  920. }
  921. static int __init hibernate_setup(char *str)
  922. {
  923. if (!strncmp(str, "noresume", 8))
  924. noresume = 1;
  925. else if (!strncmp(str, "nocompress", 10))
  926. nocompress = 1;
  927. return 1;
  928. }
  929. static int __init noresume_setup(char *str)
  930. {
  931. noresume = 1;
  932. return 1;
  933. }
  934. static int __init resumewait_setup(char *str)
  935. {
  936. resume_wait = 1;
  937. return 1;
  938. }
  939. static int __init resumedelay_setup(char *str)
  940. {
  941. resume_delay = simple_strtoul(str, NULL, 0);
  942. return 1;
  943. }
  944. __setup("noresume", noresume_setup);
  945. __setup("resume_offset=", resume_offset_setup);
  946. __setup("resume=", resume_setup);
  947. __setup("hibernate=", hibernate_setup);
  948. __setup("resumewait", resumewait_setup);
  949. __setup("resumedelay=", resumedelay_setup);