hibernate.c 26 KB

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