hibernate.c 25 KB

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