hibernate.c 23 KB

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