hibernate.c 22 KB

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