hibernate.c 22 KB

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