hibernate.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 <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) || !pm_check_wakeup_events())
  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. events_check_enabled = false;
  249. platform_leave(platform_mode);
  250. }
  251. Power_up:
  252. sysdev_resume();
  253. /* NOTE: dpm_resume_noirq() is just a resume() for devices
  254. * that suspended with irqs off ... no overall powerup.
  255. */
  256. Enable_irqs:
  257. local_irq_enable();
  258. Enable_cpus:
  259. enable_nonboot_cpus();
  260. Platform_finish:
  261. platform_finish(platform_mode);
  262. dpm_resume_noirq(in_suspend ?
  263. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  264. return error;
  265. }
  266. /**
  267. * hibernation_snapshot - quiesce devices and create the hibernation
  268. * snapshot image.
  269. * @platform_mode - if set, use the platform driver, if available, to
  270. * prepare the platform firmware for the power transition.
  271. *
  272. * Must be called with pm_mutex held
  273. */
  274. int hibernation_snapshot(int platform_mode)
  275. {
  276. int error;
  277. gfp_t saved_mask;
  278. error = platform_begin(platform_mode);
  279. if (error)
  280. goto Close;
  281. /* Preallocate image memory before shutting down devices. */
  282. error = hibernate_preallocate_memory();
  283. if (error)
  284. goto Close;
  285. suspend_console();
  286. hibernation_freeze_swap();
  287. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  288. error = dpm_suspend_start(PMSG_FREEZE);
  289. if (error)
  290. goto Recover_platform;
  291. if (hibernation_test(TEST_DEVICES))
  292. goto Recover_platform;
  293. error = create_image(platform_mode);
  294. /* Control returns here after successful restore */
  295. Resume_devices:
  296. /* We may need to release the preallocated image pages here. */
  297. if (error || !in_suspend)
  298. swsusp_free();
  299. dpm_resume_end(in_suspend ?
  300. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  301. set_gfp_allowed_mask(saved_mask);
  302. resume_console();
  303. Close:
  304. platform_end(platform_mode);
  305. return error;
  306. Recover_platform:
  307. platform_recover(platform_mode);
  308. goto Resume_devices;
  309. }
  310. /**
  311. * resume_target_kernel - prepare devices that need to be suspended with
  312. * interrupts off, restore the contents of highmem that have not been
  313. * restored yet from the image and run the low level code that will restore
  314. * the remaining contents of memory and switch to the just restored target
  315. * kernel.
  316. */
  317. static int resume_target_kernel(bool platform_mode)
  318. {
  319. int error;
  320. error = dpm_suspend_noirq(PMSG_QUIESCE);
  321. if (error) {
  322. printk(KERN_ERR "PM: Some devices failed to power down, "
  323. "aborting resume\n");
  324. return error;
  325. }
  326. error = platform_pre_restore(platform_mode);
  327. if (error)
  328. goto Cleanup;
  329. error = disable_nonboot_cpus();
  330. if (error)
  331. goto Enable_cpus;
  332. local_irq_disable();
  333. error = sysdev_suspend(PMSG_QUIESCE);
  334. if (error)
  335. goto Enable_irqs;
  336. /* We'll ignore saved state, but this gets preempt count (etc) right */
  337. save_processor_state();
  338. error = restore_highmem();
  339. if (!error) {
  340. error = swsusp_arch_resume();
  341. /*
  342. * The code below is only ever reached in case of a failure.
  343. * Otherwise execution continues at place where
  344. * swsusp_arch_suspend() was called
  345. */
  346. BUG_ON(!error);
  347. /* This call to restore_highmem() undos the previous one */
  348. restore_highmem();
  349. }
  350. /*
  351. * The only reason why swsusp_arch_resume() can fail is memory being
  352. * very tight, so we have to free it as soon as we can to avoid
  353. * subsequent failures
  354. */
  355. swsusp_free();
  356. restore_processor_state();
  357. touch_softlockup_watchdog();
  358. sysdev_resume();
  359. Enable_irqs:
  360. local_irq_enable();
  361. Enable_cpus:
  362. enable_nonboot_cpus();
  363. Cleanup:
  364. platform_restore_cleanup(platform_mode);
  365. dpm_resume_noirq(PMSG_RECOVER);
  366. return error;
  367. }
  368. /**
  369. * hibernation_restore - quiesce devices and restore the hibernation
  370. * snapshot image. If successful, control returns in hibernation_snaphot()
  371. * @platform_mode - if set, use the platform driver, if available, to
  372. * prepare the platform firmware for the transition.
  373. *
  374. * Must be called with pm_mutex held
  375. */
  376. int hibernation_restore(int platform_mode)
  377. {
  378. int error;
  379. gfp_t saved_mask;
  380. pm_prepare_console();
  381. suspend_console();
  382. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  383. error = dpm_suspend_start(PMSG_QUIESCE);
  384. if (!error) {
  385. error = resume_target_kernel(platform_mode);
  386. dpm_resume_end(PMSG_RECOVER);
  387. }
  388. set_gfp_allowed_mask(saved_mask);
  389. resume_console();
  390. pm_restore_console();
  391. return error;
  392. }
  393. /**
  394. * hibernation_platform_enter - enter the hibernation state using the
  395. * platform driver (if available)
  396. */
  397. int hibernation_platform_enter(void)
  398. {
  399. int error;
  400. gfp_t saved_mask;
  401. if (!hibernation_ops)
  402. return -ENOSYS;
  403. /*
  404. * We have cancelled the power transition by running
  405. * hibernation_ops->finish() before saving the image, so we should let
  406. * the firmware know that we're going to enter the sleep state after all
  407. */
  408. error = hibernation_ops->begin();
  409. if (error)
  410. goto Close;
  411. entering_platform_hibernation = true;
  412. suspend_console();
  413. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  414. error = dpm_suspend_start(PMSG_HIBERNATE);
  415. if (error) {
  416. if (hibernation_ops->recover)
  417. hibernation_ops->recover();
  418. goto Resume_devices;
  419. }
  420. error = dpm_suspend_noirq(PMSG_HIBERNATE);
  421. if (error)
  422. goto Resume_devices;
  423. error = hibernation_ops->prepare();
  424. if (error)
  425. goto Platform_finish;
  426. error = disable_nonboot_cpus();
  427. if (error)
  428. goto Platform_finish;
  429. local_irq_disable();
  430. sysdev_suspend(PMSG_HIBERNATE);
  431. if (!pm_check_wakeup_events()) {
  432. error = -EAGAIN;
  433. goto Power_up;
  434. }
  435. hibernation_ops->enter();
  436. /* We should never get here */
  437. while (1);
  438. Power_up:
  439. sysdev_resume();
  440. local_irq_enable();
  441. enable_nonboot_cpus();
  442. Platform_finish:
  443. hibernation_ops->finish();
  444. dpm_resume_noirq(PMSG_RESTORE);
  445. Resume_devices:
  446. entering_platform_hibernation = false;
  447. dpm_resume_end(PMSG_RESTORE);
  448. set_gfp_allowed_mask(saved_mask);
  449. resume_console();
  450. Close:
  451. hibernation_ops->end();
  452. return error;
  453. }
  454. /**
  455. * power_down - Shut the machine down for hibernation.
  456. *
  457. * Use the platform driver, if configured so; otherwise try
  458. * to power off or reboot.
  459. */
  460. static void power_down(void)
  461. {
  462. switch (hibernation_mode) {
  463. case HIBERNATION_TEST:
  464. case HIBERNATION_TESTPROC:
  465. break;
  466. case HIBERNATION_REBOOT:
  467. kernel_restart(NULL);
  468. break;
  469. case HIBERNATION_PLATFORM:
  470. hibernation_platform_enter();
  471. case HIBERNATION_SHUTDOWN:
  472. kernel_power_off();
  473. break;
  474. }
  475. kernel_halt();
  476. /*
  477. * Valid image is on the disk, if we continue we risk serious data
  478. * corruption after resume.
  479. */
  480. printk(KERN_CRIT "PM: Please power down manually\n");
  481. while(1);
  482. }
  483. static int prepare_processes(void)
  484. {
  485. int error = 0;
  486. if (freeze_processes()) {
  487. error = -EBUSY;
  488. thaw_processes();
  489. }
  490. return error;
  491. }
  492. /**
  493. * hibernate - The granpappy of the built-in hibernation management
  494. */
  495. int hibernate(void)
  496. {
  497. int error;
  498. mutex_lock(&pm_mutex);
  499. /* The snapshot device should not be opened while we're running */
  500. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  501. error = -EBUSY;
  502. goto Unlock;
  503. }
  504. pm_prepare_console();
  505. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  506. if (error)
  507. goto Exit;
  508. error = usermodehelper_disable();
  509. if (error)
  510. goto Exit;
  511. /* Allocate memory management structures */
  512. error = create_basic_memory_bitmaps();
  513. if (error)
  514. goto Exit;
  515. printk(KERN_INFO "PM: Syncing filesystems ... ");
  516. sys_sync();
  517. printk("done.\n");
  518. error = prepare_processes();
  519. if (error)
  520. goto Finish;
  521. if (hibernation_test(TEST_FREEZER))
  522. goto Thaw;
  523. if (hibernation_testmode(HIBERNATION_TESTPROC))
  524. goto Thaw;
  525. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  526. if (error)
  527. goto Thaw;
  528. if (in_suspend) {
  529. unsigned int flags = 0;
  530. if (hibernation_mode == HIBERNATION_PLATFORM)
  531. flags |= SF_PLATFORM_MODE;
  532. pr_debug("PM: writing image.\n");
  533. error = swsusp_write(flags);
  534. swsusp_free();
  535. if (!error)
  536. power_down();
  537. } else {
  538. pr_debug("PM: Image restored successfully.\n");
  539. }
  540. Thaw:
  541. thaw_processes();
  542. Finish:
  543. free_basic_memory_bitmaps();
  544. usermodehelper_enable();
  545. Exit:
  546. pm_notifier_call_chain(PM_POST_HIBERNATION);
  547. pm_restore_console();
  548. atomic_inc(&snapshot_device_available);
  549. Unlock:
  550. mutex_unlock(&pm_mutex);
  551. return error;
  552. }
  553. /**
  554. * software_resume - Resume from a saved image.
  555. *
  556. * Called as a late_initcall (so all devices are discovered and
  557. * initialized), we call swsusp to see if we have a saved image or not.
  558. * If so, we quiesce devices, the restore the saved image. We will
  559. * return above (in hibernate() ) if everything goes well.
  560. * Otherwise, we fail gracefully and return to the normally
  561. * scheduled program.
  562. *
  563. */
  564. static int software_resume(void)
  565. {
  566. int error;
  567. unsigned int flags;
  568. /*
  569. * If the user said "noresume".. bail out early.
  570. */
  571. if (noresume)
  572. return 0;
  573. /*
  574. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  575. * is configured into the kernel. Since the regular hibernate
  576. * trigger path is via sysfs which takes a buffer mutex before
  577. * calling hibernate functions (which take pm_mutex) this can
  578. * cause lockdep to complain about a possible ABBA deadlock
  579. * which cannot happen since we're in the boot code here and
  580. * sysfs can't be invoked yet. Therefore, we use a subclass
  581. * here to avoid lockdep complaining.
  582. */
  583. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  584. if (swsusp_resume_device)
  585. goto Check_image;
  586. if (!strlen(resume_file)) {
  587. error = -ENOENT;
  588. goto Unlock;
  589. }
  590. pr_debug("PM: Checking image partition %s\n", resume_file);
  591. /* Check if the device is there */
  592. swsusp_resume_device = name_to_dev_t(resume_file);
  593. if (!swsusp_resume_device) {
  594. /*
  595. * Some device discovery might still be in progress; we need
  596. * to wait for this to finish.
  597. */
  598. wait_for_device_probe();
  599. /*
  600. * We can't depend on SCSI devices being available after loading
  601. * one of their modules until scsi_complete_async_scans() is
  602. * called and the resume device usually is a SCSI one.
  603. */
  604. scsi_complete_async_scans();
  605. swsusp_resume_device = name_to_dev_t(resume_file);
  606. if (!swsusp_resume_device) {
  607. error = -ENODEV;
  608. goto Unlock;
  609. }
  610. }
  611. Check_image:
  612. pr_debug("PM: Resume from partition %d:%d\n",
  613. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  614. pr_debug("PM: Checking hibernation image.\n");
  615. error = swsusp_check();
  616. if (error)
  617. goto Unlock;
  618. /* The snapshot device should not be opened while we're running */
  619. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  620. error = -EBUSY;
  621. swsusp_close(FMODE_READ);
  622. goto Unlock;
  623. }
  624. pm_prepare_console();
  625. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  626. if (error)
  627. goto close_finish;
  628. error = usermodehelper_disable();
  629. if (error)
  630. goto close_finish;
  631. error = create_basic_memory_bitmaps();
  632. if (error)
  633. goto close_finish;
  634. pr_debug("PM: Preparing processes for restore.\n");
  635. error = prepare_processes();
  636. if (error) {
  637. swsusp_close(FMODE_READ);
  638. goto Done;
  639. }
  640. pr_debug("PM: Reading hibernation image.\n");
  641. error = swsusp_read(&flags);
  642. swsusp_close(FMODE_READ);
  643. if (!error)
  644. hibernation_restore(flags & SF_PLATFORM_MODE);
  645. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  646. swsusp_free();
  647. thaw_processes();
  648. Done:
  649. free_basic_memory_bitmaps();
  650. usermodehelper_enable();
  651. Finish:
  652. pm_notifier_call_chain(PM_POST_RESTORE);
  653. pm_restore_console();
  654. atomic_inc(&snapshot_device_available);
  655. /* For success case, the suspend path will release the lock */
  656. Unlock:
  657. mutex_unlock(&pm_mutex);
  658. pr_debug("PM: Resume from disk failed.\n");
  659. return error;
  660. close_finish:
  661. swsusp_close(FMODE_READ);
  662. goto Finish;
  663. }
  664. late_initcall(software_resume);
  665. static const char * const hibernation_modes[] = {
  666. [HIBERNATION_PLATFORM] = "platform",
  667. [HIBERNATION_SHUTDOWN] = "shutdown",
  668. [HIBERNATION_REBOOT] = "reboot",
  669. [HIBERNATION_TEST] = "test",
  670. [HIBERNATION_TESTPROC] = "testproc",
  671. };
  672. /**
  673. * disk - Control hibernation mode
  674. *
  675. * Suspend-to-disk can be handled in several ways. We have a few options
  676. * for putting the system to sleep - using the platform driver (e.g. ACPI
  677. * or other hibernation_ops), powering off the system or rebooting the
  678. * system (for testing) as well as the two test modes.
  679. *
  680. * The system can support 'platform', and that is known a priori (and
  681. * encoded by the presence of hibernation_ops). However, the user may
  682. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  683. * test modes, 'test' or 'testproc'.
  684. *
  685. * show() will display what the mode is currently set to.
  686. * store() will accept one of
  687. *
  688. * 'platform'
  689. * 'shutdown'
  690. * 'reboot'
  691. * 'test'
  692. * 'testproc'
  693. *
  694. * It will only change to 'platform' if the system
  695. * supports it (as determined by having hibernation_ops).
  696. */
  697. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  698. char *buf)
  699. {
  700. int i;
  701. char *start = buf;
  702. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  703. if (!hibernation_modes[i])
  704. continue;
  705. switch (i) {
  706. case HIBERNATION_SHUTDOWN:
  707. case HIBERNATION_REBOOT:
  708. case HIBERNATION_TEST:
  709. case HIBERNATION_TESTPROC:
  710. break;
  711. case HIBERNATION_PLATFORM:
  712. if (hibernation_ops)
  713. break;
  714. /* not a valid mode, continue with loop */
  715. continue;
  716. }
  717. if (i == hibernation_mode)
  718. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  719. else
  720. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  721. }
  722. buf += sprintf(buf, "\n");
  723. return buf-start;
  724. }
  725. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  726. const char *buf, size_t n)
  727. {
  728. int error = 0;
  729. int i;
  730. int len;
  731. char *p;
  732. int mode = HIBERNATION_INVALID;
  733. p = memchr(buf, '\n', n);
  734. len = p ? p - buf : n;
  735. mutex_lock(&pm_mutex);
  736. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  737. if (len == strlen(hibernation_modes[i])
  738. && !strncmp(buf, hibernation_modes[i], len)) {
  739. mode = i;
  740. break;
  741. }
  742. }
  743. if (mode != HIBERNATION_INVALID) {
  744. switch (mode) {
  745. case HIBERNATION_SHUTDOWN:
  746. case HIBERNATION_REBOOT:
  747. case HIBERNATION_TEST:
  748. case HIBERNATION_TESTPROC:
  749. hibernation_mode = mode;
  750. break;
  751. case HIBERNATION_PLATFORM:
  752. if (hibernation_ops)
  753. hibernation_mode = mode;
  754. else
  755. error = -EINVAL;
  756. }
  757. } else
  758. error = -EINVAL;
  759. if (!error)
  760. pr_debug("PM: Hibernation mode set to '%s'\n",
  761. hibernation_modes[mode]);
  762. mutex_unlock(&pm_mutex);
  763. return error ? error : n;
  764. }
  765. power_attr(disk);
  766. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  767. char *buf)
  768. {
  769. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  770. MINOR(swsusp_resume_device));
  771. }
  772. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  773. const char *buf, size_t n)
  774. {
  775. unsigned int maj, min;
  776. dev_t res;
  777. int ret = -EINVAL;
  778. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  779. goto out;
  780. res = MKDEV(maj,min);
  781. if (maj != MAJOR(res) || min != MINOR(res))
  782. goto out;
  783. mutex_lock(&pm_mutex);
  784. swsusp_resume_device = res;
  785. mutex_unlock(&pm_mutex);
  786. printk(KERN_INFO "PM: Starting manual resume from disk\n");
  787. noresume = 0;
  788. software_resume();
  789. ret = n;
  790. out:
  791. return ret;
  792. }
  793. power_attr(resume);
  794. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  795. char *buf)
  796. {
  797. return sprintf(buf, "%lu\n", image_size);
  798. }
  799. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  800. const char *buf, size_t n)
  801. {
  802. unsigned long size;
  803. if (sscanf(buf, "%lu", &size) == 1) {
  804. image_size = size;
  805. return n;
  806. }
  807. return -EINVAL;
  808. }
  809. power_attr(image_size);
  810. static struct attribute * g[] = {
  811. &disk_attr.attr,
  812. &resume_attr.attr,
  813. &image_size_attr.attr,
  814. NULL,
  815. };
  816. static struct attribute_group attr_group = {
  817. .attrs = g,
  818. };
  819. static int __init pm_disk_init(void)
  820. {
  821. return sysfs_create_group(power_kobj, &attr_group);
  822. }
  823. core_initcall(pm_disk_init);
  824. static int __init resume_setup(char *str)
  825. {
  826. if (noresume)
  827. return 1;
  828. strncpy( resume_file, str, 255 );
  829. return 1;
  830. }
  831. static int __init resume_offset_setup(char *str)
  832. {
  833. unsigned long long offset;
  834. if (noresume)
  835. return 1;
  836. if (sscanf(str, "%llu", &offset) == 1)
  837. swsusp_resume_block = offset;
  838. return 1;
  839. }
  840. static int __init noresume_setup(char *str)
  841. {
  842. noresume = 1;
  843. return 1;
  844. }
  845. __setup("noresume", noresume_setup);
  846. __setup("resume_offset=", resume_offset_setup);
  847. __setup("resume=", resume_setup);