hibernate.c 25 KB

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