hibernate.c 25 KB

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