hibernate.c 23 KB

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