hibernate.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. gfp_t saved_mask;
  275. error = platform_begin(platform_mode);
  276. if (error)
  277. return error;
  278. /* Preallocate image memory before shutting down devices. */
  279. error = hibernate_preallocate_memory();
  280. if (error)
  281. goto Close;
  282. suspend_console();
  283. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  284. error = dpm_suspend_start(PMSG_FREEZE);
  285. if (error)
  286. goto Recover_platform;
  287. if (hibernation_test(TEST_DEVICES))
  288. goto Recover_platform;
  289. error = create_image(platform_mode);
  290. /* Control returns here after successful restore */
  291. Resume_devices:
  292. /* We may need to release the preallocated image pages here. */
  293. if (error || !in_suspend)
  294. swsusp_free();
  295. dpm_resume_end(in_suspend ?
  296. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  297. set_gfp_allowed_mask(saved_mask);
  298. resume_console();
  299. Close:
  300. platform_end(platform_mode);
  301. return error;
  302. Recover_platform:
  303. platform_recover(platform_mode);
  304. goto Resume_devices;
  305. }
  306. /**
  307. * resume_target_kernel - prepare devices that need to be suspended with
  308. * interrupts off, restore the contents of highmem that have not been
  309. * restored yet from the image and run the low level code that will restore
  310. * the remaining contents of memory and switch to the just restored target
  311. * kernel.
  312. */
  313. static int resume_target_kernel(bool platform_mode)
  314. {
  315. int error;
  316. error = dpm_suspend_noirq(PMSG_QUIESCE);
  317. if (error) {
  318. printk(KERN_ERR "PM: Some devices failed to power down, "
  319. "aborting resume\n");
  320. return error;
  321. }
  322. error = platform_pre_restore(platform_mode);
  323. if (error)
  324. goto Cleanup;
  325. error = disable_nonboot_cpus();
  326. if (error)
  327. goto Enable_cpus;
  328. local_irq_disable();
  329. error = sysdev_suspend(PMSG_QUIESCE);
  330. if (error)
  331. goto Enable_irqs;
  332. /* We'll ignore saved state, but this gets preempt count (etc) right */
  333. save_processor_state();
  334. error = restore_highmem();
  335. if (!error) {
  336. error = swsusp_arch_resume();
  337. /*
  338. * The code below is only ever reached in case of a failure.
  339. * Otherwise execution continues at place where
  340. * swsusp_arch_suspend() was called
  341. */
  342. BUG_ON(!error);
  343. /* This call to restore_highmem() undos the previous one */
  344. restore_highmem();
  345. }
  346. /*
  347. * The only reason why swsusp_arch_resume() can fail is memory being
  348. * very tight, so we have to free it as soon as we can to avoid
  349. * subsequent failures
  350. */
  351. swsusp_free();
  352. restore_processor_state();
  353. touch_softlockup_watchdog();
  354. sysdev_resume();
  355. Enable_irqs:
  356. local_irq_enable();
  357. Enable_cpus:
  358. enable_nonboot_cpus();
  359. Cleanup:
  360. platform_restore_cleanup(platform_mode);
  361. dpm_resume_noirq(PMSG_RECOVER);
  362. return error;
  363. }
  364. /**
  365. * hibernation_restore - quiesce devices and restore the hibernation
  366. * snapshot image. If successful, control returns in hibernation_snaphot()
  367. * @platform_mode - if set, use the platform driver, if available, to
  368. * prepare the platform firmware for the transition.
  369. *
  370. * Must be called with pm_mutex held
  371. */
  372. int hibernation_restore(int platform_mode)
  373. {
  374. int error;
  375. gfp_t saved_mask;
  376. pm_prepare_console();
  377. suspend_console();
  378. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  379. error = dpm_suspend_start(PMSG_QUIESCE);
  380. if (!error) {
  381. error = resume_target_kernel(platform_mode);
  382. dpm_resume_end(PMSG_RECOVER);
  383. }
  384. set_gfp_allowed_mask(saved_mask);
  385. resume_console();
  386. pm_restore_console();
  387. return error;
  388. }
  389. /**
  390. * hibernation_platform_enter - enter the hibernation state using the
  391. * platform driver (if available)
  392. */
  393. int hibernation_platform_enter(void)
  394. {
  395. int error;
  396. gfp_t saved_mask;
  397. if (!hibernation_ops)
  398. return -ENOSYS;
  399. /*
  400. * We have cancelled the power transition by running
  401. * hibernation_ops->finish() before saving the image, so we should let
  402. * the firmware know that we're going to enter the sleep state after all
  403. */
  404. error = hibernation_ops->begin();
  405. if (error)
  406. goto Close;
  407. entering_platform_hibernation = true;
  408. suspend_console();
  409. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  410. error = dpm_suspend_start(PMSG_HIBERNATE);
  411. if (error) {
  412. if (hibernation_ops->recover)
  413. hibernation_ops->recover();
  414. goto Resume_devices;
  415. }
  416. error = dpm_suspend_noirq(PMSG_HIBERNATE);
  417. if (error)
  418. goto Resume_devices;
  419. error = hibernation_ops->prepare();
  420. if (error)
  421. goto Platform_finish;
  422. error = disable_nonboot_cpus();
  423. if (error)
  424. goto Platform_finish;
  425. local_irq_disable();
  426. sysdev_suspend(PMSG_HIBERNATE);
  427. hibernation_ops->enter();
  428. /* We should never get here */
  429. while (1);
  430. /*
  431. * We don't need to reenable the nonboot CPUs or resume consoles, since
  432. * the system is going to be halted anyway.
  433. */
  434. Platform_finish:
  435. hibernation_ops->finish();
  436. dpm_suspend_noirq(PMSG_RESTORE);
  437. Resume_devices:
  438. entering_platform_hibernation = false;
  439. dpm_resume_end(PMSG_RESTORE);
  440. set_gfp_allowed_mask(saved_mask);
  441. resume_console();
  442. Close:
  443. hibernation_ops->end();
  444. return error;
  445. }
  446. /**
  447. * power_down - Shut the machine down for hibernation.
  448. *
  449. * Use the platform driver, if configured so; otherwise try
  450. * to power off or reboot.
  451. */
  452. static void power_down(void)
  453. {
  454. switch (hibernation_mode) {
  455. case HIBERNATION_TEST:
  456. case HIBERNATION_TESTPROC:
  457. break;
  458. case HIBERNATION_REBOOT:
  459. kernel_restart(NULL);
  460. break;
  461. case HIBERNATION_PLATFORM:
  462. hibernation_platform_enter();
  463. case HIBERNATION_SHUTDOWN:
  464. kernel_power_off();
  465. break;
  466. }
  467. kernel_halt();
  468. /*
  469. * Valid image is on the disk, if we continue we risk serious data
  470. * corruption after resume.
  471. */
  472. printk(KERN_CRIT "PM: Please power down manually\n");
  473. while(1);
  474. }
  475. static int prepare_processes(void)
  476. {
  477. int error = 0;
  478. if (freeze_processes()) {
  479. error = -EBUSY;
  480. thaw_processes();
  481. }
  482. return error;
  483. }
  484. /**
  485. * hibernate - The granpappy of the built-in hibernation management
  486. */
  487. int hibernate(void)
  488. {
  489. int error;
  490. mutex_lock(&pm_mutex);
  491. /* The snapshot device should not be opened while we're running */
  492. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  493. error = -EBUSY;
  494. goto Unlock;
  495. }
  496. pm_prepare_console();
  497. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  498. if (error)
  499. goto Exit;
  500. error = usermodehelper_disable();
  501. if (error)
  502. goto Exit;
  503. /* Allocate memory management structures */
  504. error = create_basic_memory_bitmaps();
  505. if (error)
  506. goto Exit;
  507. printk(KERN_INFO "PM: Syncing filesystems ... ");
  508. sys_sync();
  509. printk("done.\n");
  510. error = prepare_processes();
  511. if (error)
  512. goto Finish;
  513. if (hibernation_test(TEST_FREEZER))
  514. goto Thaw;
  515. if (hibernation_testmode(HIBERNATION_TESTPROC))
  516. goto Thaw;
  517. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  518. if (error)
  519. goto Thaw;
  520. if (in_suspend) {
  521. unsigned int flags = 0;
  522. if (hibernation_mode == HIBERNATION_PLATFORM)
  523. flags |= SF_PLATFORM_MODE;
  524. pr_debug("PM: writing image.\n");
  525. error = swsusp_write(flags);
  526. swsusp_free();
  527. if (!error)
  528. power_down();
  529. } else {
  530. pr_debug("PM: Image restored successfully.\n");
  531. }
  532. Thaw:
  533. thaw_processes();
  534. Finish:
  535. free_basic_memory_bitmaps();
  536. usermodehelper_enable();
  537. Exit:
  538. pm_notifier_call_chain(PM_POST_HIBERNATION);
  539. pm_restore_console();
  540. atomic_inc(&snapshot_device_available);
  541. Unlock:
  542. mutex_unlock(&pm_mutex);
  543. return error;
  544. }
  545. /**
  546. * software_resume - Resume from a saved image.
  547. *
  548. * Called as a late_initcall (so all devices are discovered and
  549. * initialized), we call swsusp to see if we have a saved image or not.
  550. * If so, we quiesce devices, the restore the saved image. We will
  551. * return above (in hibernate() ) if everything goes well.
  552. * Otherwise, we fail gracefully and return to the normally
  553. * scheduled program.
  554. *
  555. */
  556. static int software_resume(void)
  557. {
  558. int error;
  559. unsigned int flags;
  560. /*
  561. * If the user said "noresume".. bail out early.
  562. */
  563. if (noresume)
  564. return 0;
  565. /*
  566. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  567. * is configured into the kernel. Since the regular hibernate
  568. * trigger path is via sysfs which takes a buffer mutex before
  569. * calling hibernate functions (which take pm_mutex) this can
  570. * cause lockdep to complain about a possible ABBA deadlock
  571. * which cannot happen since we're in the boot code here and
  572. * sysfs can't be invoked yet. Therefore, we use a subclass
  573. * here to avoid lockdep complaining.
  574. */
  575. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  576. if (swsusp_resume_device)
  577. goto Check_image;
  578. if (!strlen(resume_file)) {
  579. error = -ENOENT;
  580. goto Unlock;
  581. }
  582. pr_debug("PM: Checking image partition %s\n", resume_file);
  583. /* Check if the device is there */
  584. swsusp_resume_device = name_to_dev_t(resume_file);
  585. if (!swsusp_resume_device) {
  586. /*
  587. * Some device discovery might still be in progress; we need
  588. * to wait for this to finish.
  589. */
  590. wait_for_device_probe();
  591. /*
  592. * We can't depend on SCSI devices being available after loading
  593. * one of their modules until scsi_complete_async_scans() is
  594. * called and the resume device usually is a SCSI one.
  595. */
  596. scsi_complete_async_scans();
  597. swsusp_resume_device = name_to_dev_t(resume_file);
  598. if (!swsusp_resume_device) {
  599. error = -ENODEV;
  600. goto Unlock;
  601. }
  602. }
  603. Check_image:
  604. pr_debug("PM: Resume from partition %d:%d\n",
  605. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  606. pr_debug("PM: Checking hibernation image.\n");
  607. error = swsusp_check();
  608. if (error)
  609. goto Unlock;
  610. /* The snapshot device should not be opened while we're running */
  611. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  612. error = -EBUSY;
  613. swsusp_close(FMODE_READ);
  614. goto Unlock;
  615. }
  616. pm_prepare_console();
  617. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  618. if (error)
  619. goto close_finish;
  620. error = usermodehelper_disable();
  621. if (error)
  622. goto close_finish;
  623. error = create_basic_memory_bitmaps();
  624. if (error)
  625. goto close_finish;
  626. pr_debug("PM: Preparing processes for restore.\n");
  627. error = prepare_processes();
  628. if (error) {
  629. swsusp_close(FMODE_READ);
  630. goto Done;
  631. }
  632. pr_debug("PM: Reading hibernation image.\n");
  633. error = swsusp_read(&flags);
  634. swsusp_close(FMODE_READ);
  635. if (!error)
  636. hibernation_restore(flags & SF_PLATFORM_MODE);
  637. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  638. swsusp_free();
  639. thaw_processes();
  640. Done:
  641. free_basic_memory_bitmaps();
  642. usermodehelper_enable();
  643. Finish:
  644. pm_notifier_call_chain(PM_POST_RESTORE);
  645. pm_restore_console();
  646. atomic_inc(&snapshot_device_available);
  647. /* For success case, the suspend path will release the lock */
  648. Unlock:
  649. mutex_unlock(&pm_mutex);
  650. pr_debug("PM: Resume from disk failed.\n");
  651. return error;
  652. close_finish:
  653. swsusp_close(FMODE_READ);
  654. goto Finish;
  655. }
  656. late_initcall(software_resume);
  657. static const char * const hibernation_modes[] = {
  658. [HIBERNATION_PLATFORM] = "platform",
  659. [HIBERNATION_SHUTDOWN] = "shutdown",
  660. [HIBERNATION_REBOOT] = "reboot",
  661. [HIBERNATION_TEST] = "test",
  662. [HIBERNATION_TESTPROC] = "testproc",
  663. };
  664. /**
  665. * disk - Control hibernation mode
  666. *
  667. * Suspend-to-disk can be handled in several ways. We have a few options
  668. * for putting the system to sleep - using the platform driver (e.g. ACPI
  669. * or other hibernation_ops), powering off the system or rebooting the
  670. * system (for testing) as well as the two test modes.
  671. *
  672. * The system can support 'platform', and that is known a priori (and
  673. * encoded by the presence of hibernation_ops). However, the user may
  674. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  675. * test modes, 'test' or 'testproc'.
  676. *
  677. * show() will display what the mode is currently set to.
  678. * store() will accept one of
  679. *
  680. * 'platform'
  681. * 'shutdown'
  682. * 'reboot'
  683. * 'test'
  684. * 'testproc'
  685. *
  686. * It will only change to 'platform' if the system
  687. * supports it (as determined by having hibernation_ops).
  688. */
  689. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  690. char *buf)
  691. {
  692. int i;
  693. char *start = buf;
  694. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  695. if (!hibernation_modes[i])
  696. continue;
  697. switch (i) {
  698. case HIBERNATION_SHUTDOWN:
  699. case HIBERNATION_REBOOT:
  700. case HIBERNATION_TEST:
  701. case HIBERNATION_TESTPROC:
  702. break;
  703. case HIBERNATION_PLATFORM:
  704. if (hibernation_ops)
  705. break;
  706. /* not a valid mode, continue with loop */
  707. continue;
  708. }
  709. if (i == hibernation_mode)
  710. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  711. else
  712. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  713. }
  714. buf += sprintf(buf, "\n");
  715. return buf-start;
  716. }
  717. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  718. const char *buf, size_t n)
  719. {
  720. int error = 0;
  721. int i;
  722. int len;
  723. char *p;
  724. int mode = HIBERNATION_INVALID;
  725. p = memchr(buf, '\n', n);
  726. len = p ? p - buf : n;
  727. mutex_lock(&pm_mutex);
  728. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  729. if (len == strlen(hibernation_modes[i])
  730. && !strncmp(buf, hibernation_modes[i], len)) {
  731. mode = i;
  732. break;
  733. }
  734. }
  735. if (mode != HIBERNATION_INVALID) {
  736. switch (mode) {
  737. case HIBERNATION_SHUTDOWN:
  738. case HIBERNATION_REBOOT:
  739. case HIBERNATION_TEST:
  740. case HIBERNATION_TESTPROC:
  741. hibernation_mode = mode;
  742. break;
  743. case HIBERNATION_PLATFORM:
  744. if (hibernation_ops)
  745. hibernation_mode = mode;
  746. else
  747. error = -EINVAL;
  748. }
  749. } else
  750. error = -EINVAL;
  751. if (!error)
  752. pr_debug("PM: Hibernation mode set to '%s'\n",
  753. hibernation_modes[mode]);
  754. mutex_unlock(&pm_mutex);
  755. return error ? error : n;
  756. }
  757. power_attr(disk);
  758. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  759. char *buf)
  760. {
  761. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  762. MINOR(swsusp_resume_device));
  763. }
  764. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  765. const char *buf, size_t n)
  766. {
  767. unsigned int maj, min;
  768. dev_t res;
  769. int ret = -EINVAL;
  770. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  771. goto out;
  772. res = MKDEV(maj,min);
  773. if (maj != MAJOR(res) || min != MINOR(res))
  774. goto out;
  775. mutex_lock(&pm_mutex);
  776. swsusp_resume_device = res;
  777. mutex_unlock(&pm_mutex);
  778. printk(KERN_INFO "PM: Starting manual resume from disk\n");
  779. noresume = 0;
  780. software_resume();
  781. ret = n;
  782. out:
  783. return ret;
  784. }
  785. power_attr(resume);
  786. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  787. char *buf)
  788. {
  789. return sprintf(buf, "%lu\n", image_size);
  790. }
  791. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  792. const char *buf, size_t n)
  793. {
  794. unsigned long size;
  795. if (sscanf(buf, "%lu", &size) == 1) {
  796. image_size = size;
  797. return n;
  798. }
  799. return -EINVAL;
  800. }
  801. power_attr(image_size);
  802. static struct attribute * g[] = {
  803. &disk_attr.attr,
  804. &resume_attr.attr,
  805. &image_size_attr.attr,
  806. NULL,
  807. };
  808. static struct attribute_group attr_group = {
  809. .attrs = g,
  810. };
  811. static int __init pm_disk_init(void)
  812. {
  813. return sysfs_create_group(power_kobj, &attr_group);
  814. }
  815. core_initcall(pm_disk_init);
  816. static int __init resume_setup(char *str)
  817. {
  818. if (noresume)
  819. return 1;
  820. strncpy( resume_file, str, 255 );
  821. return 1;
  822. }
  823. static int __init resume_offset_setup(char *str)
  824. {
  825. unsigned long long offset;
  826. if (noresume)
  827. return 1;
  828. if (sscanf(str, "%llu", &offset) == 1)
  829. swsusp_resume_block = offset;
  830. return 1;
  831. }
  832. static int __init noresume_setup(char *str)
  833. {
  834. noresume = 1;
  835. return 1;
  836. }
  837. __setup("noresume", noresume_setup);
  838. __setup("resume_offset=", resume_offset_setup);
  839. __setup("resume=", resume_setup);