hibernate.c 28 KB

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