hibernate.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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@suse.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/kmod.h>
  17. #include <linux/delay.h>
  18. #include <linux/fs.h>
  19. #include <linux/mount.h>
  20. #include <linux/pm.h>
  21. #include <linux/console.h>
  22. #include <linux/cpu.h>
  23. #include <linux/freezer.h>
  24. #include <linux/gfp.h>
  25. #include <scsi/scsi_scan.h>
  26. #include <asm/suspend.h>
  27. #include "power.h"
  28. static int noresume = 0;
  29. static char resume_file[256] = CONFIG_PM_STD_PARTITION;
  30. dev_t swsusp_resume_device;
  31. sector_t swsusp_resume_block;
  32. int in_suspend __nosavedata = 0;
  33. enum {
  34. HIBERNATION_INVALID,
  35. HIBERNATION_PLATFORM,
  36. HIBERNATION_TEST,
  37. HIBERNATION_TESTPROC,
  38. HIBERNATION_SHUTDOWN,
  39. HIBERNATION_REBOOT,
  40. /* keep last */
  41. __HIBERNATION_AFTER_LAST
  42. };
  43. #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
  44. #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
  45. static int hibernation_mode = HIBERNATION_SHUTDOWN;
  46. static struct platform_hibernation_ops *hibernation_ops;
  47. /**
  48. * hibernation_set_ops - set the global hibernate operations
  49. * @ops: the hibernation operations to use in subsequent hibernation transitions
  50. */
  51. void hibernation_set_ops(struct platform_hibernation_ops *ops)
  52. {
  53. if (ops && !(ops->begin && ops->end && ops->pre_snapshot
  54. && ops->prepare && ops->finish && ops->enter && ops->pre_restore
  55. && ops->restore_cleanup)) {
  56. WARN_ON(1);
  57. return;
  58. }
  59. mutex_lock(&pm_mutex);
  60. hibernation_ops = ops;
  61. if (ops)
  62. hibernation_mode = HIBERNATION_PLATFORM;
  63. else if (hibernation_mode == HIBERNATION_PLATFORM)
  64. hibernation_mode = HIBERNATION_SHUTDOWN;
  65. mutex_unlock(&pm_mutex);
  66. }
  67. static bool entering_platform_hibernation;
  68. bool system_entering_hibernation(void)
  69. {
  70. return entering_platform_hibernation;
  71. }
  72. EXPORT_SYMBOL(system_entering_hibernation);
  73. #ifdef CONFIG_PM_DEBUG
  74. static void hibernation_debug_sleep(void)
  75. {
  76. printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
  77. mdelay(5000);
  78. }
  79. static int hibernation_testmode(int mode)
  80. {
  81. if (hibernation_mode == mode) {
  82. hibernation_debug_sleep();
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. static int hibernation_test(int level)
  88. {
  89. if (pm_test_level == level) {
  90. hibernation_debug_sleep();
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. #else /* !CONFIG_PM_DEBUG */
  96. static int hibernation_testmode(int mode) { return 0; }
  97. static int hibernation_test(int level) { return 0; }
  98. #endif /* !CONFIG_PM_DEBUG */
  99. /**
  100. * platform_begin - tell the platform driver that we're starting
  101. * hibernation
  102. */
  103. static int platform_begin(int platform_mode)
  104. {
  105. return (platform_mode && hibernation_ops) ?
  106. hibernation_ops->begin() : 0;
  107. }
  108. /**
  109. * platform_end - tell the platform driver that we've entered the
  110. * working state
  111. */
  112. static void platform_end(int platform_mode)
  113. {
  114. if (platform_mode && hibernation_ops)
  115. hibernation_ops->end();
  116. }
  117. /**
  118. * platform_pre_snapshot - prepare the machine for hibernation using the
  119. * platform driver if so configured and return an error code if it fails
  120. */
  121. static int platform_pre_snapshot(int platform_mode)
  122. {
  123. return (platform_mode && hibernation_ops) ?
  124. hibernation_ops->pre_snapshot() : 0;
  125. }
  126. /**
  127. * platform_leave - prepare the machine for switching to the normal mode
  128. * of operation using the platform driver (called with interrupts disabled)
  129. */
  130. static void platform_leave(int platform_mode)
  131. {
  132. if (platform_mode && hibernation_ops)
  133. hibernation_ops->leave();
  134. }
  135. /**
  136. * platform_finish - switch the machine to the normal mode of operation
  137. * using the platform driver (must be called after platform_prepare())
  138. */
  139. static void platform_finish(int platform_mode)
  140. {
  141. if (platform_mode && hibernation_ops)
  142. hibernation_ops->finish();
  143. }
  144. /**
  145. * platform_pre_restore - prepare the platform for the restoration from a
  146. * hibernation image. If the restore fails after this function has been
  147. * called, platform_restore_cleanup() must be called.
  148. */
  149. static int platform_pre_restore(int platform_mode)
  150. {
  151. return (platform_mode && hibernation_ops) ?
  152. hibernation_ops->pre_restore() : 0;
  153. }
  154. /**
  155. * platform_restore_cleanup - switch the platform to the normal mode of
  156. * operation after a failing restore. If platform_pre_restore() has been
  157. * called before the failing restore, this function must be called too,
  158. * regardless of the result of platform_pre_restore().
  159. */
  160. static void platform_restore_cleanup(int platform_mode)
  161. {
  162. if (platform_mode && hibernation_ops)
  163. hibernation_ops->restore_cleanup();
  164. }
  165. /**
  166. * platform_recover - recover the platform from a failure to suspend
  167. * devices.
  168. */
  169. static void platform_recover(int platform_mode)
  170. {
  171. if (platform_mode && hibernation_ops && hibernation_ops->recover)
  172. hibernation_ops->recover();
  173. }
  174. /**
  175. * swsusp_show_speed - print the time elapsed between two events.
  176. * @start: Starting event.
  177. * @stop: Final event.
  178. * @nr_pages - number of pages processed between @start and @stop
  179. * @msg - introductory message to print
  180. */
  181. void swsusp_show_speed(struct timeval *start, struct timeval *stop,
  182. unsigned nr_pages, char *msg)
  183. {
  184. s64 elapsed_centisecs64;
  185. int centisecs;
  186. int k;
  187. int kps;
  188. elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
  189. do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
  190. centisecs = elapsed_centisecs64;
  191. if (centisecs == 0)
  192. centisecs = 1; /* avoid div-by-zero */
  193. k = nr_pages * (PAGE_SIZE / 1024);
  194. kps = (k * 100) / centisecs;
  195. printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
  196. msg, k,
  197. centisecs / 100, centisecs % 100,
  198. kps / 1000, (kps % 1000) / 10);
  199. }
  200. /**
  201. * create_image - freeze devices that need to be frozen with interrupts
  202. * off, create the hibernation image and thaw those devices. Control
  203. * reappears in this routine after a restore.
  204. */
  205. static int create_image(int platform_mode)
  206. {
  207. int error;
  208. error = arch_prepare_suspend();
  209. if (error)
  210. return error;
  211. /* At this point, dpm_suspend_start() has been called, but *not*
  212. * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
  213. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  214. * become desynchronized with the actual state of the hardware
  215. * at resume time, and evil weirdness ensues.
  216. */
  217. error = dpm_suspend_noirq(PMSG_FREEZE);
  218. if (error) {
  219. printk(KERN_ERR "PM: Some devices failed to power down, "
  220. "aborting hibernation\n");
  221. return error;
  222. }
  223. error = platform_pre_snapshot(platform_mode);
  224. if (error || hibernation_test(TEST_PLATFORM))
  225. goto Platform_finish;
  226. error = disable_nonboot_cpus();
  227. if (error || hibernation_test(TEST_CPUS)
  228. || hibernation_testmode(HIBERNATION_TEST))
  229. goto Enable_cpus;
  230. local_irq_disable();
  231. error = sysdev_suspend(PMSG_FREEZE);
  232. if (error) {
  233. printk(KERN_ERR "PM: Some system devices failed to power down, "
  234. "aborting hibernation\n");
  235. goto Enable_irqs;
  236. }
  237. if (hibernation_test(TEST_CORE))
  238. goto Power_up;
  239. in_suspend = 1;
  240. save_processor_state();
  241. error = swsusp_arch_suspend();
  242. if (error)
  243. printk(KERN_ERR "PM: Error %d creating hibernation image\n",
  244. error);
  245. /* Restore control flow magically appears here */
  246. restore_processor_state();
  247. if (!in_suspend)
  248. platform_leave(platform_mode);
  249. Power_up:
  250. sysdev_resume();
  251. /* NOTE: dpm_resume_noirq() is just a resume() for devices
  252. * that suspended with irqs off ... no overall powerup.
  253. */
  254. Enable_irqs:
  255. local_irq_enable();
  256. Enable_cpus:
  257. enable_nonboot_cpus();
  258. Platform_finish:
  259. platform_finish(platform_mode);
  260. dpm_resume_noirq(in_suspend ?
  261. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  262. return error;
  263. }
  264. /**
  265. * hibernation_snapshot - quiesce devices and create the hibernation
  266. * snapshot image.
  267. * @platform_mode - if set, use the platform driver, if available, to
  268. * prepare the platform firmware for the power transition.
  269. *
  270. * Must be called with pm_mutex held
  271. */
  272. int hibernation_snapshot(int platform_mode)
  273. {
  274. int error;
  275. gfp_t saved_mask;
  276. error = platform_begin(platform_mode);
  277. if (error)
  278. return error;
  279. /* Preallocate image memory before shutting down devices. */
  280. error = hibernate_preallocate_memory();
  281. if (error)
  282. goto Close;
  283. suspend_console();
  284. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  285. error = dpm_suspend_start(PMSG_FREEZE);
  286. if (error)
  287. goto Recover_platform;
  288. if (hibernation_test(TEST_DEVICES))
  289. goto Recover_platform;
  290. error = create_image(platform_mode);
  291. /* Control returns here after successful restore */
  292. Resume_devices:
  293. /* We may need to release the preallocated image pages here. */
  294. if (error || !in_suspend)
  295. swsusp_free();
  296. dpm_resume_end(in_suspend ?
  297. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  298. set_gfp_allowed_mask(saved_mask);
  299. resume_console();
  300. Close:
  301. platform_end(platform_mode);
  302. return error;
  303. Recover_platform:
  304. platform_recover(platform_mode);
  305. goto Resume_devices;
  306. }
  307. /**
  308. * resume_target_kernel - prepare devices that need to be suspended with
  309. * interrupts off, restore the contents of highmem that have not been
  310. * restored yet from the image and run the low level code that will restore
  311. * the remaining contents of memory and switch to the just restored target
  312. * kernel.
  313. */
  314. static int resume_target_kernel(bool platform_mode)
  315. {
  316. int error;
  317. error = dpm_suspend_noirq(PMSG_QUIESCE);
  318. if (error) {
  319. printk(KERN_ERR "PM: Some devices failed to power down, "
  320. "aborting resume\n");
  321. return error;
  322. }
  323. error = platform_pre_restore(platform_mode);
  324. if (error)
  325. goto Cleanup;
  326. error = disable_nonboot_cpus();
  327. if (error)
  328. goto Enable_cpus;
  329. local_irq_disable();
  330. error = sysdev_suspend(PMSG_QUIESCE);
  331. if (error)
  332. goto Enable_irqs;
  333. /* We'll ignore saved state, but this gets preempt count (etc) right */
  334. save_processor_state();
  335. error = restore_highmem();
  336. if (!error) {
  337. error = swsusp_arch_resume();
  338. /*
  339. * The code below is only ever reached in case of a failure.
  340. * Otherwise execution continues at place where
  341. * swsusp_arch_suspend() was called
  342. */
  343. BUG_ON(!error);
  344. /* This call to restore_highmem() undos the previous one */
  345. restore_highmem();
  346. }
  347. /*
  348. * The only reason why swsusp_arch_resume() can fail is memory being
  349. * very tight, so we have to free it as soon as we can to avoid
  350. * subsequent failures
  351. */
  352. swsusp_free();
  353. restore_processor_state();
  354. touch_softlockup_watchdog();
  355. sysdev_resume();
  356. Enable_irqs:
  357. local_irq_enable();
  358. Enable_cpus:
  359. enable_nonboot_cpus();
  360. Cleanup:
  361. platform_restore_cleanup(platform_mode);
  362. dpm_resume_noirq(PMSG_RECOVER);
  363. return error;
  364. }
  365. /**
  366. * hibernation_restore - quiesce devices and restore the hibernation
  367. * snapshot image. If successful, control returns in hibernation_snaphot()
  368. * @platform_mode - if set, use the platform driver, if available, to
  369. * prepare the platform firmware for the transition.
  370. *
  371. * Must be called with pm_mutex held
  372. */
  373. int hibernation_restore(int platform_mode)
  374. {
  375. int error;
  376. gfp_t saved_mask;
  377. pm_prepare_console();
  378. suspend_console();
  379. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  380. error = dpm_suspend_start(PMSG_QUIESCE);
  381. if (!error) {
  382. error = resume_target_kernel(platform_mode);
  383. dpm_resume_end(PMSG_RECOVER);
  384. }
  385. set_gfp_allowed_mask(saved_mask);
  386. resume_console();
  387. pm_restore_console();
  388. return error;
  389. }
  390. /**
  391. * hibernation_platform_enter - enter the hibernation state using the
  392. * platform driver (if available)
  393. */
  394. int hibernation_platform_enter(void)
  395. {
  396. int error;
  397. gfp_t saved_mask;
  398. if (!hibernation_ops)
  399. return -ENOSYS;
  400. /*
  401. * We have cancelled the power transition by running
  402. * hibernation_ops->finish() before saving the image, so we should let
  403. * the firmware know that we're going to enter the sleep state after all
  404. */
  405. error = hibernation_ops->begin();
  406. if (error)
  407. goto Close;
  408. entering_platform_hibernation = true;
  409. suspend_console();
  410. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  411. error = dpm_suspend_start(PMSG_HIBERNATE);
  412. if (error) {
  413. if (hibernation_ops->recover)
  414. hibernation_ops->recover();
  415. goto Resume_devices;
  416. }
  417. error = dpm_suspend_noirq(PMSG_HIBERNATE);
  418. if (error)
  419. goto Resume_devices;
  420. error = hibernation_ops->prepare();
  421. if (error)
  422. goto Platform_finish;
  423. error = disable_nonboot_cpus();
  424. if (error)
  425. goto Platform_finish;
  426. local_irq_disable();
  427. sysdev_suspend(PMSG_HIBERNATE);
  428. hibernation_ops->enter();
  429. /* We should never get here */
  430. while (1);
  431. /*
  432. * We don't need to reenable the nonboot CPUs or resume consoles, since
  433. * the system is going to be halted anyway.
  434. */
  435. Platform_finish:
  436. hibernation_ops->finish();
  437. dpm_suspend_noirq(PMSG_RESTORE);
  438. Resume_devices:
  439. entering_platform_hibernation = false;
  440. dpm_resume_end(PMSG_RESTORE);
  441. set_gfp_allowed_mask(saved_mask);
  442. resume_console();
  443. Close:
  444. hibernation_ops->end();
  445. return error;
  446. }
  447. /**
  448. * power_down - Shut the machine down for hibernation.
  449. *
  450. * Use the platform driver, if configured so; otherwise try
  451. * to power off or reboot.
  452. */
  453. static void power_down(void)
  454. {
  455. switch (hibernation_mode) {
  456. case HIBERNATION_TEST:
  457. case HIBERNATION_TESTPROC:
  458. break;
  459. case HIBERNATION_REBOOT:
  460. kernel_restart(NULL);
  461. break;
  462. case HIBERNATION_PLATFORM:
  463. hibernation_platform_enter();
  464. case HIBERNATION_SHUTDOWN:
  465. kernel_power_off();
  466. break;
  467. }
  468. kernel_halt();
  469. /*
  470. * Valid image is on the disk, if we continue we risk serious data
  471. * corruption after resume.
  472. */
  473. printk(KERN_CRIT "PM: Please power down manually\n");
  474. while(1);
  475. }
  476. static int prepare_processes(void)
  477. {
  478. int error = 0;
  479. if (freeze_processes()) {
  480. error = -EBUSY;
  481. thaw_processes();
  482. }
  483. return error;
  484. }
  485. /**
  486. * hibernate - The granpappy of the built-in hibernation management
  487. */
  488. int hibernate(void)
  489. {
  490. int error;
  491. mutex_lock(&pm_mutex);
  492. /* The snapshot device should not be opened while we're running */
  493. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  494. error = -EBUSY;
  495. goto Unlock;
  496. }
  497. pm_prepare_console();
  498. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  499. if (error)
  500. goto Exit;
  501. error = usermodehelper_disable();
  502. if (error)
  503. goto Exit;
  504. /* Allocate memory management structures */
  505. error = create_basic_memory_bitmaps();
  506. if (error)
  507. goto Exit;
  508. printk(KERN_INFO "PM: Syncing filesystems ... ");
  509. sys_sync();
  510. printk("done.\n");
  511. error = prepare_processes();
  512. if (error)
  513. goto Finish;
  514. if (hibernation_test(TEST_FREEZER))
  515. goto Thaw;
  516. if (hibernation_testmode(HIBERNATION_TESTPROC))
  517. goto Thaw;
  518. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  519. if (error)
  520. goto Thaw;
  521. if (in_suspend) {
  522. unsigned int flags = 0;
  523. if (hibernation_mode == HIBERNATION_PLATFORM)
  524. flags |= SF_PLATFORM_MODE;
  525. pr_debug("PM: writing image.\n");
  526. error = swsusp_write(flags);
  527. swsusp_free();
  528. if (!error)
  529. power_down();
  530. } else {
  531. pr_debug("PM: Image restored successfully.\n");
  532. }
  533. Thaw:
  534. thaw_processes();
  535. Finish:
  536. free_basic_memory_bitmaps();
  537. usermodehelper_enable();
  538. Exit:
  539. pm_notifier_call_chain(PM_POST_HIBERNATION);
  540. pm_restore_console();
  541. atomic_inc(&snapshot_device_available);
  542. Unlock:
  543. mutex_unlock(&pm_mutex);
  544. return error;
  545. }
  546. /**
  547. * software_resume - Resume from a saved image.
  548. *
  549. * Called as a late_initcall (so all devices are discovered and
  550. * initialized), we call swsusp to see if we have a saved image or not.
  551. * If so, we quiesce devices, the restore the saved image. We will
  552. * return above (in hibernate() ) if everything goes well.
  553. * Otherwise, we fail gracefully and return to the normally
  554. * scheduled program.
  555. *
  556. */
  557. static int software_resume(void)
  558. {
  559. int error;
  560. unsigned int flags;
  561. /*
  562. * If the user said "noresume".. bail out early.
  563. */
  564. if (noresume)
  565. return 0;
  566. /*
  567. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  568. * is configured into the kernel. Since the regular hibernate
  569. * trigger path is via sysfs which takes a buffer mutex before
  570. * calling hibernate functions (which take pm_mutex) this can
  571. * cause lockdep to complain about a possible ABBA deadlock
  572. * which cannot happen since we're in the boot code here and
  573. * sysfs can't be invoked yet. Therefore, we use a subclass
  574. * here to avoid lockdep complaining.
  575. */
  576. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  577. if (swsusp_resume_device)
  578. goto Check_image;
  579. if (!strlen(resume_file)) {
  580. error = -ENOENT;
  581. goto Unlock;
  582. }
  583. pr_debug("PM: Checking image partition %s\n", resume_file);
  584. /* Check if the device is there */
  585. swsusp_resume_device = name_to_dev_t(resume_file);
  586. if (!swsusp_resume_device) {
  587. /*
  588. * Some device discovery might still be in progress; we need
  589. * to wait for this to finish.
  590. */
  591. wait_for_device_probe();
  592. /*
  593. * We can't depend on SCSI devices being available after loading
  594. * one of their modules until scsi_complete_async_scans() is
  595. * called and the resume device usually is a SCSI one.
  596. */
  597. scsi_complete_async_scans();
  598. swsusp_resume_device = name_to_dev_t(resume_file);
  599. if (!swsusp_resume_device) {
  600. error = -ENODEV;
  601. goto Unlock;
  602. }
  603. }
  604. Check_image:
  605. pr_debug("PM: Resume from partition %d:%d\n",
  606. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  607. pr_debug("PM: Checking hibernation image.\n");
  608. error = swsusp_check();
  609. if (error)
  610. goto Unlock;
  611. /* The snapshot device should not be opened while we're running */
  612. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  613. error = -EBUSY;
  614. swsusp_close(FMODE_READ);
  615. goto Unlock;
  616. }
  617. pm_prepare_console();
  618. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  619. if (error)
  620. goto close_finish;
  621. error = usermodehelper_disable();
  622. if (error)
  623. goto close_finish;
  624. error = create_basic_memory_bitmaps();
  625. if (error)
  626. goto close_finish;
  627. pr_debug("PM: Preparing processes for restore.\n");
  628. error = prepare_processes();
  629. if (error) {
  630. swsusp_close(FMODE_READ);
  631. goto Done;
  632. }
  633. pr_debug("PM: Reading hibernation image.\n");
  634. error = swsusp_read(&flags);
  635. swsusp_close(FMODE_READ);
  636. if (!error)
  637. hibernation_restore(flags & SF_PLATFORM_MODE);
  638. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  639. swsusp_free();
  640. thaw_processes();
  641. Done:
  642. free_basic_memory_bitmaps();
  643. usermodehelper_enable();
  644. Finish:
  645. pm_notifier_call_chain(PM_POST_RESTORE);
  646. pm_restore_console();
  647. atomic_inc(&snapshot_device_available);
  648. /* For success case, the suspend path will release the lock */
  649. Unlock:
  650. mutex_unlock(&pm_mutex);
  651. pr_debug("PM: Resume from disk failed.\n");
  652. return error;
  653. close_finish:
  654. swsusp_close(FMODE_READ);
  655. goto Finish;
  656. }
  657. late_initcall(software_resume);
  658. static const char * const hibernation_modes[] = {
  659. [HIBERNATION_PLATFORM] = "platform",
  660. [HIBERNATION_SHUTDOWN] = "shutdown",
  661. [HIBERNATION_REBOOT] = "reboot",
  662. [HIBERNATION_TEST] = "test",
  663. [HIBERNATION_TESTPROC] = "testproc",
  664. };
  665. /**
  666. * disk - Control hibernation mode
  667. *
  668. * Suspend-to-disk can be handled in several ways. We have a few options
  669. * for putting the system to sleep - using the platform driver (e.g. ACPI
  670. * or other hibernation_ops), powering off the system or rebooting the
  671. * system (for testing) as well as the two test modes.
  672. *
  673. * The system can support 'platform', and that is known a priori (and
  674. * encoded by the presence of hibernation_ops). However, the user may
  675. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  676. * test modes, 'test' or 'testproc'.
  677. *
  678. * show() will display what the mode is currently set to.
  679. * store() will accept one of
  680. *
  681. * 'platform'
  682. * 'shutdown'
  683. * 'reboot'
  684. * 'test'
  685. * 'testproc'
  686. *
  687. * It will only change to 'platform' if the system
  688. * supports it (as determined by having hibernation_ops).
  689. */
  690. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  691. char *buf)
  692. {
  693. int i;
  694. char *start = buf;
  695. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  696. if (!hibernation_modes[i])
  697. continue;
  698. switch (i) {
  699. case HIBERNATION_SHUTDOWN:
  700. case HIBERNATION_REBOOT:
  701. case HIBERNATION_TEST:
  702. case HIBERNATION_TESTPROC:
  703. break;
  704. case HIBERNATION_PLATFORM:
  705. if (hibernation_ops)
  706. break;
  707. /* not a valid mode, continue with loop */
  708. continue;
  709. }
  710. if (i == hibernation_mode)
  711. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  712. else
  713. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  714. }
  715. buf += sprintf(buf, "\n");
  716. return buf-start;
  717. }
  718. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  719. const char *buf, size_t n)
  720. {
  721. int error = 0;
  722. int i;
  723. int len;
  724. char *p;
  725. int mode = HIBERNATION_INVALID;
  726. p = memchr(buf, '\n', n);
  727. len = p ? p - buf : n;
  728. mutex_lock(&pm_mutex);
  729. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  730. if (len == strlen(hibernation_modes[i])
  731. && !strncmp(buf, hibernation_modes[i], len)) {
  732. mode = i;
  733. break;
  734. }
  735. }
  736. if (mode != HIBERNATION_INVALID) {
  737. switch (mode) {
  738. case HIBERNATION_SHUTDOWN:
  739. case HIBERNATION_REBOOT:
  740. case HIBERNATION_TEST:
  741. case HIBERNATION_TESTPROC:
  742. hibernation_mode = mode;
  743. break;
  744. case HIBERNATION_PLATFORM:
  745. if (hibernation_ops)
  746. hibernation_mode = mode;
  747. else
  748. error = -EINVAL;
  749. }
  750. } else
  751. error = -EINVAL;
  752. if (!error)
  753. pr_debug("PM: Hibernation mode set to '%s'\n",
  754. hibernation_modes[mode]);
  755. mutex_unlock(&pm_mutex);
  756. return error ? error : n;
  757. }
  758. power_attr(disk);
  759. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  760. char *buf)
  761. {
  762. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  763. MINOR(swsusp_resume_device));
  764. }
  765. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  766. const char *buf, size_t n)
  767. {
  768. unsigned int maj, min;
  769. dev_t res;
  770. int ret = -EINVAL;
  771. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  772. goto out;
  773. res = MKDEV(maj,min);
  774. if (maj != MAJOR(res) || min != MINOR(res))
  775. goto out;
  776. mutex_lock(&pm_mutex);
  777. swsusp_resume_device = res;
  778. mutex_unlock(&pm_mutex);
  779. printk(KERN_INFO "PM: Starting manual resume from disk\n");
  780. noresume = 0;
  781. software_resume();
  782. ret = n;
  783. out:
  784. return ret;
  785. }
  786. power_attr(resume);
  787. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  788. char *buf)
  789. {
  790. return sprintf(buf, "%lu\n", image_size);
  791. }
  792. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  793. const char *buf, size_t n)
  794. {
  795. unsigned long size;
  796. if (sscanf(buf, "%lu", &size) == 1) {
  797. image_size = size;
  798. return n;
  799. }
  800. return -EINVAL;
  801. }
  802. power_attr(image_size);
  803. static struct attribute * g[] = {
  804. &disk_attr.attr,
  805. &resume_attr.attr,
  806. &image_size_attr.attr,
  807. NULL,
  808. };
  809. static struct attribute_group attr_group = {
  810. .attrs = g,
  811. };
  812. static int __init pm_disk_init(void)
  813. {
  814. return sysfs_create_group(power_kobj, &attr_group);
  815. }
  816. core_initcall(pm_disk_init);
  817. static int __init resume_setup(char *str)
  818. {
  819. if (noresume)
  820. return 1;
  821. strncpy( resume_file, str, 255 );
  822. return 1;
  823. }
  824. static int __init resume_offset_setup(char *str)
  825. {
  826. unsigned long long offset;
  827. if (noresume)
  828. return 1;
  829. if (sscanf(str, "%llu", &offset) == 1)
  830. swsusp_resume_block = offset;
  831. return 1;
  832. }
  833. static int __init noresume_setup(char *str)
  834. {
  835. noresume = 1;
  836. return 1;
  837. }
  838. __setup("noresume", noresume_setup);
  839. __setup("resume_offset=", resume_offset_setup);
  840. __setup("resume=", resume_setup);