hibernate.c 25 KB

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