hibernate.c 26 KB

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