hibernate.c 21 KB

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