hibernate.c 26 KB

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