hibernate.c 23 KB

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