hibernate.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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 = PMSG_RECOVER;
  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 Close;
  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 Close;
  315. }
  316. error = dpm_prepare(PMSG_FREEZE);
  317. if (error)
  318. goto Complete_devices;
  319. suspend_console();
  320. pm_restrict_gfp_mask();
  321. error = dpm_suspend(PMSG_FREEZE);
  322. if (error)
  323. goto Recover_platform;
  324. if (hibernation_test(TEST_DEVICES))
  325. goto Recover_platform;
  326. error = create_image(platform_mode);
  327. /*
  328. * Control returns here (1) after the image has been created or the
  329. * image creation has failed and (2) after a successful restore.
  330. */
  331. Resume_devices:
  332. /* We may need to release the preallocated image pages here. */
  333. if (error || !in_suspend)
  334. swsusp_free();
  335. msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
  336. dpm_resume(msg);
  337. if (error || !in_suspend)
  338. pm_restore_gfp_mask();
  339. resume_console();
  340. Complete_devices:
  341. dpm_complete(msg);
  342. Close:
  343. platform_end(platform_mode);
  344. return error;
  345. Recover_platform:
  346. platform_recover(platform_mode);
  347. goto Resume_devices;
  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. /**
  522. * hibernate - Carry out system hibernation, including saving the image.
  523. */
  524. int hibernate(void)
  525. {
  526. int error;
  527. mutex_lock(&pm_mutex);
  528. /* The snapshot device should not be opened while we're running */
  529. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  530. error = -EBUSY;
  531. goto Unlock;
  532. }
  533. pm_prepare_console();
  534. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  535. if (error)
  536. goto Exit;
  537. error = usermodehelper_disable();
  538. if (error)
  539. goto Exit;
  540. /* Allocate memory management structures */
  541. error = create_basic_memory_bitmaps();
  542. if (error)
  543. goto Exit;
  544. printk(KERN_INFO "PM: Syncing filesystems ... ");
  545. sys_sync();
  546. printk("done.\n");
  547. error = freeze_processes();
  548. if (error)
  549. goto Finish;
  550. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  551. if (error)
  552. goto Thaw;
  553. if (freezer_test_done) {
  554. freezer_test_done = false;
  555. goto Thaw;
  556. }
  557. if (in_suspend) {
  558. unsigned int flags = 0;
  559. if (hibernation_mode == HIBERNATION_PLATFORM)
  560. flags |= SF_PLATFORM_MODE;
  561. if (nocompress)
  562. flags |= SF_NOCOMPRESS_MODE;
  563. else
  564. flags |= SF_CRC32_MODE;
  565. pr_debug("PM: writing image.\n");
  566. error = swsusp_write(flags);
  567. swsusp_free();
  568. if (!error)
  569. power_down();
  570. in_suspend = 0;
  571. pm_restore_gfp_mask();
  572. } else {
  573. pr_debug("PM: Image restored successfully.\n");
  574. }
  575. Thaw:
  576. thaw_processes();
  577. Finish:
  578. free_basic_memory_bitmaps();
  579. usermodehelper_enable();
  580. Exit:
  581. pm_notifier_call_chain(PM_POST_HIBERNATION);
  582. pm_restore_console();
  583. atomic_inc(&snapshot_device_available);
  584. Unlock:
  585. mutex_unlock(&pm_mutex);
  586. return error;
  587. }
  588. /**
  589. * software_resume - Resume from a saved hibernation image.
  590. *
  591. * This routine is called as a late initcall, when all devices have been
  592. * discovered and initialized already.
  593. *
  594. * The image reading code is called to see if there is a hibernation image
  595. * available for reading. If that is the case, devices are quiesced and the
  596. * contents of memory is restored from the saved image.
  597. *
  598. * If this is successful, control reappears in the restored target kernel in
  599. * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
  600. * attempts to recover gracefully and make the kernel return to the normal mode
  601. * of operation.
  602. */
  603. static int software_resume(void)
  604. {
  605. int error;
  606. unsigned int flags;
  607. /*
  608. * If the user said "noresume".. bail out early.
  609. */
  610. if (noresume)
  611. return 0;
  612. /*
  613. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  614. * is configured into the kernel. Since the regular hibernate
  615. * trigger path is via sysfs which takes a buffer mutex before
  616. * calling hibernate functions (which take pm_mutex) this can
  617. * cause lockdep to complain about a possible ABBA deadlock
  618. * which cannot happen since we're in the boot code here and
  619. * sysfs can't be invoked yet. Therefore, we use a subclass
  620. * here to avoid lockdep complaining.
  621. */
  622. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  623. if (swsusp_resume_device)
  624. goto Check_image;
  625. if (!strlen(resume_file)) {
  626. error = -ENOENT;
  627. goto Unlock;
  628. }
  629. pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
  630. if (resume_delay) {
  631. printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
  632. resume_delay);
  633. ssleep(resume_delay);
  634. }
  635. /* Check if the device is there */
  636. swsusp_resume_device = name_to_dev_t(resume_file);
  637. if (!swsusp_resume_device) {
  638. /*
  639. * Some device discovery might still be in progress; we need
  640. * to wait for this to finish.
  641. */
  642. wait_for_device_probe();
  643. if (resume_wait) {
  644. while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
  645. msleep(10);
  646. async_synchronize_full();
  647. }
  648. /*
  649. * We can't depend on SCSI devices being available after loading
  650. * one of their modules until scsi_complete_async_scans() is
  651. * called and the resume device usually is a SCSI one.
  652. */
  653. scsi_complete_async_scans();
  654. swsusp_resume_device = name_to_dev_t(resume_file);
  655. if (!swsusp_resume_device) {
  656. error = -ENODEV;
  657. goto Unlock;
  658. }
  659. }
  660. Check_image:
  661. pr_debug("PM: Hibernation image partition %d:%d present\n",
  662. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  663. pr_debug("PM: Looking for hibernation image.\n");
  664. error = swsusp_check();
  665. if (error)
  666. goto Unlock;
  667. /* The snapshot device should not be opened while we're running */
  668. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  669. error = -EBUSY;
  670. swsusp_close(FMODE_READ);
  671. goto Unlock;
  672. }
  673. pm_prepare_console();
  674. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  675. if (error)
  676. goto close_finish;
  677. error = usermodehelper_disable();
  678. if (error)
  679. goto close_finish;
  680. error = create_basic_memory_bitmaps();
  681. if (error)
  682. goto close_finish;
  683. pr_debug("PM: Preparing processes for restore.\n");
  684. error = freeze_processes();
  685. if (error) {
  686. swsusp_close(FMODE_READ);
  687. goto Done;
  688. }
  689. pr_debug("PM: Loading hibernation image.\n");
  690. error = swsusp_read(&flags);
  691. swsusp_close(FMODE_READ);
  692. if (!error)
  693. hibernation_restore(flags & SF_PLATFORM_MODE);
  694. printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
  695. swsusp_free();
  696. thaw_processes();
  697. Done:
  698. free_basic_memory_bitmaps();
  699. usermodehelper_enable();
  700. Finish:
  701. pm_notifier_call_chain(PM_POST_RESTORE);
  702. pm_restore_console();
  703. atomic_inc(&snapshot_device_available);
  704. /* For success case, the suspend path will release the lock */
  705. Unlock:
  706. mutex_unlock(&pm_mutex);
  707. pr_debug("PM: Hibernation image not present or could not be loaded.\n");
  708. return error;
  709. close_finish:
  710. swsusp_close(FMODE_READ);
  711. goto Finish;
  712. }
  713. late_initcall(software_resume);
  714. static const char * const hibernation_modes[] = {
  715. [HIBERNATION_PLATFORM] = "platform",
  716. [HIBERNATION_SHUTDOWN] = "shutdown",
  717. [HIBERNATION_REBOOT] = "reboot",
  718. [HIBERNATION_TEST] = "test",
  719. [HIBERNATION_TESTPROC] = "testproc",
  720. };
  721. /*
  722. * /sys/power/disk - Control hibernation mode.
  723. *
  724. * Hibernation can be handled in several ways. There are a few different ways
  725. * to put the system into the sleep state: using the platform driver (e.g. ACPI
  726. * or other hibernation_ops), powering it off or rebooting it (for testing
  727. * mostly), or using one of the two available test modes.
  728. *
  729. * The sysfs file /sys/power/disk provides an interface for selecting the
  730. * hibernation mode to use. Reading from this file causes the available modes
  731. * to be printed. There are 5 modes that can be supported:
  732. *
  733. * 'platform'
  734. * 'shutdown'
  735. * 'reboot'
  736. * 'test'
  737. * 'testproc'
  738. *
  739. * If a platform hibernation driver is in use, 'platform' will be supported
  740. * and will be used by default. Otherwise, 'shutdown' will be used by default.
  741. * The selected option (i.e. the one corresponding to the current value of
  742. * hibernation_mode) is enclosed by a square bracket.
  743. *
  744. * To select a given hibernation mode it is necessary to write the mode's
  745. * string representation (as returned by reading from /sys/power/disk) back
  746. * into /sys/power/disk.
  747. */
  748. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  749. char *buf)
  750. {
  751. int i;
  752. char *start = buf;
  753. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  754. if (!hibernation_modes[i])
  755. continue;
  756. switch (i) {
  757. case HIBERNATION_SHUTDOWN:
  758. case HIBERNATION_REBOOT:
  759. case HIBERNATION_TEST:
  760. case HIBERNATION_TESTPROC:
  761. break;
  762. case HIBERNATION_PLATFORM:
  763. if (hibernation_ops)
  764. break;
  765. /* not a valid mode, continue with loop */
  766. continue;
  767. }
  768. if (i == hibernation_mode)
  769. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  770. else
  771. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  772. }
  773. buf += sprintf(buf, "\n");
  774. return buf-start;
  775. }
  776. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  777. const char *buf, size_t n)
  778. {
  779. int error = 0;
  780. int i;
  781. int len;
  782. char *p;
  783. int mode = HIBERNATION_INVALID;
  784. p = memchr(buf, '\n', n);
  785. len = p ? p - buf : n;
  786. mutex_lock(&pm_mutex);
  787. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  788. if (len == strlen(hibernation_modes[i])
  789. && !strncmp(buf, hibernation_modes[i], len)) {
  790. mode = i;
  791. break;
  792. }
  793. }
  794. if (mode != HIBERNATION_INVALID) {
  795. switch (mode) {
  796. case HIBERNATION_SHUTDOWN:
  797. case HIBERNATION_REBOOT:
  798. case HIBERNATION_TEST:
  799. case HIBERNATION_TESTPROC:
  800. hibernation_mode = mode;
  801. break;
  802. case HIBERNATION_PLATFORM:
  803. if (hibernation_ops)
  804. hibernation_mode = mode;
  805. else
  806. error = -EINVAL;
  807. }
  808. } else
  809. error = -EINVAL;
  810. if (!error)
  811. pr_debug("PM: Hibernation mode set to '%s'\n",
  812. hibernation_modes[mode]);
  813. mutex_unlock(&pm_mutex);
  814. return error ? error : n;
  815. }
  816. power_attr(disk);
  817. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  818. char *buf)
  819. {
  820. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  821. MINOR(swsusp_resume_device));
  822. }
  823. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  824. const char *buf, size_t n)
  825. {
  826. unsigned int maj, min;
  827. dev_t res;
  828. int ret = -EINVAL;
  829. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  830. goto out;
  831. res = MKDEV(maj,min);
  832. if (maj != MAJOR(res) || min != MINOR(res))
  833. goto out;
  834. mutex_lock(&pm_mutex);
  835. swsusp_resume_device = res;
  836. mutex_unlock(&pm_mutex);
  837. printk(KERN_INFO "PM: Starting manual resume from disk\n");
  838. noresume = 0;
  839. software_resume();
  840. ret = n;
  841. out:
  842. return ret;
  843. }
  844. power_attr(resume);
  845. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  846. char *buf)
  847. {
  848. return sprintf(buf, "%lu\n", image_size);
  849. }
  850. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  851. const char *buf, size_t n)
  852. {
  853. unsigned long size;
  854. if (sscanf(buf, "%lu", &size) == 1) {
  855. image_size = size;
  856. return n;
  857. }
  858. return -EINVAL;
  859. }
  860. power_attr(image_size);
  861. static ssize_t reserved_size_show(struct kobject *kobj,
  862. struct kobj_attribute *attr, char *buf)
  863. {
  864. return sprintf(buf, "%lu\n", reserved_size);
  865. }
  866. static ssize_t reserved_size_store(struct kobject *kobj,
  867. struct kobj_attribute *attr,
  868. const char *buf, size_t n)
  869. {
  870. unsigned long size;
  871. if (sscanf(buf, "%lu", &size) == 1) {
  872. reserved_size = size;
  873. return n;
  874. }
  875. return -EINVAL;
  876. }
  877. power_attr(reserved_size);
  878. static struct attribute * g[] = {
  879. &disk_attr.attr,
  880. &resume_attr.attr,
  881. &image_size_attr.attr,
  882. &reserved_size_attr.attr,
  883. NULL,
  884. };
  885. static struct attribute_group attr_group = {
  886. .attrs = g,
  887. };
  888. static int __init pm_disk_init(void)
  889. {
  890. return sysfs_create_group(power_kobj, &attr_group);
  891. }
  892. core_initcall(pm_disk_init);
  893. static int __init resume_setup(char *str)
  894. {
  895. if (noresume)
  896. return 1;
  897. strncpy( resume_file, str, 255 );
  898. return 1;
  899. }
  900. static int __init resume_offset_setup(char *str)
  901. {
  902. unsigned long long offset;
  903. if (noresume)
  904. return 1;
  905. if (sscanf(str, "%llu", &offset) == 1)
  906. swsusp_resume_block = offset;
  907. return 1;
  908. }
  909. static int __init hibernate_setup(char *str)
  910. {
  911. if (!strncmp(str, "noresume", 8))
  912. noresume = 1;
  913. else if (!strncmp(str, "nocompress", 10))
  914. nocompress = 1;
  915. return 1;
  916. }
  917. static int __init noresume_setup(char *str)
  918. {
  919. noresume = 1;
  920. return 1;
  921. }
  922. static int __init resumewait_setup(char *str)
  923. {
  924. resume_wait = 1;
  925. return 1;
  926. }
  927. static int __init resumedelay_setup(char *str)
  928. {
  929. resume_delay = simple_strtoul(str, NULL, 0);
  930. return 1;
  931. }
  932. __setup("noresume", noresume_setup);
  933. __setup("resume_offset=", resume_offset_setup);
  934. __setup("resume=", resume_setup);
  935. __setup("hibernate=", hibernate_setup);
  936. __setup("resumewait", resumewait_setup);
  937. __setup("resumedelay=", resumedelay_setup);