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