disk.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. * kernel/power/disk.c - 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. *
  8. * This file is released under the GPLv2.
  9. *
  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. device_pm_lock();
  184. /* At this point, device_suspend() has been called, but *not*
  185. * device_power_down(). We *must* call device_power_down() now.
  186. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  187. * become desynchronized with the actual state of the hardware
  188. * at resume time, and evil weirdness ensues.
  189. */
  190. error = device_power_down(PMSG_FREEZE);
  191. if (error) {
  192. printk(KERN_ERR "PM: Some devices failed to power down, "
  193. "aborting hibernation\n");
  194. goto Unlock;
  195. }
  196. error = platform_pre_snapshot(platform_mode);
  197. if (error || hibernation_test(TEST_PLATFORM))
  198. goto Platform_finish;
  199. error = disable_nonboot_cpus();
  200. if (error || hibernation_test(TEST_CPUS)
  201. || hibernation_testmode(HIBERNATION_TEST))
  202. goto Enable_cpus;
  203. local_irq_disable();
  204. error = sysdev_suspend(PMSG_FREEZE);
  205. if (error) {
  206. printk(KERN_ERR "PM: Some system devices failed to power down, "
  207. "aborting hibernation\n");
  208. goto Enable_irqs;
  209. }
  210. if (hibernation_test(TEST_CORE))
  211. goto Power_up;
  212. in_suspend = 1;
  213. save_processor_state();
  214. error = swsusp_arch_suspend();
  215. if (error)
  216. printk(KERN_ERR "PM: Error %d creating hibernation image\n",
  217. error);
  218. /* Restore control flow magically appears here */
  219. restore_processor_state();
  220. if (!in_suspend)
  221. platform_leave(platform_mode);
  222. Power_up:
  223. sysdev_resume();
  224. /* NOTE: device_power_up() is just a resume() for devices
  225. * that suspended with irqs off ... no overall powerup.
  226. */
  227. Enable_irqs:
  228. local_irq_enable();
  229. Enable_cpus:
  230. enable_nonboot_cpus();
  231. Platform_finish:
  232. platform_finish(platform_mode);
  233. device_power_up(in_suspend ?
  234. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  235. Unlock:
  236. device_pm_unlock();
  237. return error;
  238. }
  239. /**
  240. * hibernation_snapshot - quiesce devices and create the hibernation
  241. * snapshot image.
  242. * @platform_mode - if set, use the platform driver, if available, to
  243. * prepare the platform firmware for the power transition.
  244. *
  245. * Must be called with pm_mutex held
  246. */
  247. int hibernation_snapshot(int platform_mode)
  248. {
  249. int error;
  250. error = platform_begin(platform_mode);
  251. if (error)
  252. return error;
  253. /* Free memory before shutting down devices. */
  254. error = swsusp_shrink_memory();
  255. if (error)
  256. goto Close;
  257. suspend_console();
  258. error = device_suspend(PMSG_FREEZE);
  259. if (error)
  260. goto Recover_platform;
  261. if (hibernation_test(TEST_DEVICES))
  262. goto Recover_platform;
  263. error = create_image(platform_mode);
  264. /* Control returns here after successful restore */
  265. Resume_devices:
  266. device_resume(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. device_pm_lock();
  287. error = device_power_down(PMSG_QUIESCE);
  288. if (error) {
  289. printk(KERN_ERR "PM: Some devices failed to power down, "
  290. "aborting resume\n");
  291. goto Unlock;
  292. }
  293. error = platform_pre_restore(platform_mode);
  294. if (error)
  295. goto Cleanup;
  296. error = disable_nonboot_cpus();
  297. if (error)
  298. goto Enable_cpus;
  299. local_irq_disable();
  300. error = sysdev_suspend(PMSG_QUIESCE);
  301. if (error)
  302. goto Enable_irqs;
  303. /* We'll ignore saved state, but this gets preempt count (etc) right */
  304. save_processor_state();
  305. error = restore_highmem();
  306. if (!error) {
  307. error = swsusp_arch_resume();
  308. /*
  309. * The code below is only ever reached in case of a failure.
  310. * Otherwise execution continues at place where
  311. * swsusp_arch_suspend() was called
  312. */
  313. BUG_ON(!error);
  314. /* This call to restore_highmem() undos the previous one */
  315. restore_highmem();
  316. }
  317. /*
  318. * The only reason why swsusp_arch_resume() can fail is memory being
  319. * very tight, so we have to free it as soon as we can to avoid
  320. * subsequent failures
  321. */
  322. swsusp_free();
  323. restore_processor_state();
  324. touch_softlockup_watchdog();
  325. sysdev_resume();
  326. Enable_irqs:
  327. local_irq_enable();
  328. Enable_cpus:
  329. enable_nonboot_cpus();
  330. Cleanup:
  331. platform_restore_cleanup(platform_mode);
  332. device_power_up(PMSG_RECOVER);
  333. Unlock:
  334. device_pm_unlock();
  335. return error;
  336. }
  337. /**
  338. * hibernation_restore - quiesce devices and restore the hibernation
  339. * snapshot image. If successful, control returns in hibernation_snaphot()
  340. * @platform_mode - if set, use the platform driver, if available, to
  341. * prepare the platform firmware for the transition.
  342. *
  343. * Must be called with pm_mutex held
  344. */
  345. int hibernation_restore(int platform_mode)
  346. {
  347. int error;
  348. pm_prepare_console();
  349. suspend_console();
  350. error = device_suspend(PMSG_QUIESCE);
  351. if (!error) {
  352. error = resume_target_kernel(platform_mode);
  353. device_resume(PMSG_RECOVER);
  354. }
  355. resume_console();
  356. pm_restore_console();
  357. return error;
  358. }
  359. /**
  360. * hibernation_platform_enter - enter the hibernation state using the
  361. * platform driver (if available)
  362. */
  363. int hibernation_platform_enter(void)
  364. {
  365. int error;
  366. if (!hibernation_ops)
  367. return -ENOSYS;
  368. /*
  369. * We have cancelled the power transition by running
  370. * hibernation_ops->finish() before saving the image, so we should let
  371. * the firmware know that we're going to enter the sleep state after all
  372. */
  373. error = hibernation_ops->begin();
  374. if (error)
  375. goto Close;
  376. entering_platform_hibernation = true;
  377. suspend_console();
  378. error = device_suspend(PMSG_HIBERNATE);
  379. if (error) {
  380. if (hibernation_ops->recover)
  381. hibernation_ops->recover();
  382. goto Resume_devices;
  383. }
  384. device_pm_lock();
  385. error = device_power_down(PMSG_HIBERNATE);
  386. if (error)
  387. goto Unlock;
  388. error = hibernation_ops->prepare();
  389. if (error)
  390. goto Platofrm_finish;
  391. error = disable_nonboot_cpus();
  392. if (error)
  393. goto Platofrm_finish;
  394. local_irq_disable();
  395. sysdev_suspend(PMSG_HIBERNATE);
  396. hibernation_ops->enter();
  397. /* We should never get here */
  398. while (1);
  399. /*
  400. * We don't need to reenable the nonboot CPUs or resume consoles, since
  401. * the system is going to be halted anyway.
  402. */
  403. Platofrm_finish:
  404. hibernation_ops->finish();
  405. device_power_up(PMSG_RESTORE);
  406. Unlock:
  407. device_pm_unlock();
  408. Resume_devices:
  409. entering_platform_hibernation = false;
  410. device_resume(PMSG_RESTORE);
  411. resume_console();
  412. Close:
  413. hibernation_ops->end();
  414. return error;
  415. }
  416. /**
  417. * power_down - Shut the machine down for hibernation.
  418. *
  419. * Use the platform driver, if configured so; otherwise try
  420. * to power off or reboot.
  421. */
  422. static void power_down(void)
  423. {
  424. switch (hibernation_mode) {
  425. case HIBERNATION_TEST:
  426. case HIBERNATION_TESTPROC:
  427. break;
  428. case HIBERNATION_REBOOT:
  429. kernel_restart(NULL);
  430. break;
  431. case HIBERNATION_PLATFORM:
  432. hibernation_platform_enter();
  433. case HIBERNATION_SHUTDOWN:
  434. kernel_power_off();
  435. break;
  436. }
  437. kernel_halt();
  438. /*
  439. * Valid image is on the disk, if we continue we risk serious data
  440. * corruption after resume.
  441. */
  442. printk(KERN_CRIT "PM: Please power down manually\n");
  443. while(1);
  444. }
  445. static int prepare_processes(void)
  446. {
  447. int error = 0;
  448. if (freeze_processes()) {
  449. error = -EBUSY;
  450. thaw_processes();
  451. }
  452. return error;
  453. }
  454. /**
  455. * hibernate - The granpappy of the built-in hibernation management
  456. */
  457. int hibernate(void)
  458. {
  459. int error;
  460. mutex_lock(&pm_mutex);
  461. /* The snapshot device should not be opened while we're running */
  462. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  463. error = -EBUSY;
  464. goto Unlock;
  465. }
  466. pm_prepare_console();
  467. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  468. if (error)
  469. goto Exit;
  470. error = usermodehelper_disable();
  471. if (error)
  472. goto Exit;
  473. /* Allocate memory management structures */
  474. error = create_basic_memory_bitmaps();
  475. if (error)
  476. goto Exit;
  477. printk(KERN_INFO "PM: Syncing filesystems ... ");
  478. sys_sync();
  479. printk("done.\n");
  480. error = prepare_processes();
  481. if (error)
  482. goto Finish;
  483. if (hibernation_test(TEST_FREEZER))
  484. goto Thaw;
  485. if (hibernation_testmode(HIBERNATION_TESTPROC))
  486. goto Thaw;
  487. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  488. if (in_suspend && !error) {
  489. unsigned int flags = 0;
  490. if (hibernation_mode == HIBERNATION_PLATFORM)
  491. flags |= SF_PLATFORM_MODE;
  492. pr_debug("PM: writing image.\n");
  493. error = swsusp_write(flags);
  494. swsusp_free();
  495. if (!error)
  496. power_down();
  497. } else {
  498. pr_debug("PM: Image restored successfully.\n");
  499. swsusp_free();
  500. }
  501. Thaw:
  502. thaw_processes();
  503. Finish:
  504. free_basic_memory_bitmaps();
  505. usermodehelper_enable();
  506. Exit:
  507. pm_notifier_call_chain(PM_POST_HIBERNATION);
  508. pm_restore_console();
  509. atomic_inc(&snapshot_device_available);
  510. Unlock:
  511. mutex_unlock(&pm_mutex);
  512. return error;
  513. }
  514. /**
  515. * software_resume - Resume from a saved image.
  516. *
  517. * Called as a late_initcall (so all devices are discovered and
  518. * initialized), we call swsusp to see if we have a saved image or not.
  519. * If so, we quiesce devices, the restore the saved image. We will
  520. * return above (in hibernate() ) if everything goes well.
  521. * Otherwise, we fail gracefully and return to the normally
  522. * scheduled program.
  523. *
  524. */
  525. static int software_resume(void)
  526. {
  527. int error;
  528. unsigned int flags;
  529. /*
  530. * If the user said "noresume".. bail out early.
  531. */
  532. if (noresume)
  533. return 0;
  534. /*
  535. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  536. * is configured into the kernel. Since the regular hibernate
  537. * trigger path is via sysfs which takes a buffer mutex before
  538. * calling hibernate functions (which take pm_mutex) this can
  539. * cause lockdep to complain about a possible ABBA deadlock
  540. * which cannot happen since we're in the boot code here and
  541. * sysfs can't be invoked yet. Therefore, we use a subclass
  542. * here to avoid lockdep complaining.
  543. */
  544. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  545. if (swsusp_resume_device)
  546. goto Check_image;
  547. if (!strlen(resume_file)) {
  548. error = -ENOENT;
  549. goto Unlock;
  550. }
  551. pr_debug("PM: Checking image partition %s\n", resume_file);
  552. /* Check if the device is there */
  553. swsusp_resume_device = name_to_dev_t(resume_file);
  554. if (!swsusp_resume_device) {
  555. /*
  556. * Some device discovery might still be in progress; we need
  557. * to wait for this to finish.
  558. */
  559. wait_for_device_probe();
  560. /*
  561. * We can't depend on SCSI devices being available after loading
  562. * one of their modules until scsi_complete_async_scans() is
  563. * called and the resume device usually is a SCSI one.
  564. */
  565. scsi_complete_async_scans();
  566. swsusp_resume_device = name_to_dev_t(resume_file);
  567. if (!swsusp_resume_device) {
  568. error = -ENODEV;
  569. goto Unlock;
  570. }
  571. }
  572. Check_image:
  573. pr_debug("PM: Resume from partition %d:%d\n",
  574. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  575. pr_debug("PM: Checking hibernation image.\n");
  576. error = swsusp_check();
  577. if (error)
  578. goto Unlock;
  579. /* The snapshot device should not be opened while we're running */
  580. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  581. error = -EBUSY;
  582. goto Unlock;
  583. }
  584. pm_prepare_console();
  585. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  586. if (error)
  587. goto Finish;
  588. error = usermodehelper_disable();
  589. if (error)
  590. goto Finish;
  591. error = create_basic_memory_bitmaps();
  592. if (error)
  593. goto Finish;
  594. pr_debug("PM: Preparing processes for restore.\n");
  595. error = prepare_processes();
  596. if (error) {
  597. swsusp_close(FMODE_READ);
  598. goto Done;
  599. }
  600. pr_debug("PM: Reading hibernation image.\n");
  601. error = swsusp_read(&flags);
  602. if (!error)
  603. hibernation_restore(flags & SF_PLATFORM_MODE);
  604. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  605. swsusp_free();
  606. thaw_processes();
  607. Done:
  608. free_basic_memory_bitmaps();
  609. usermodehelper_enable();
  610. Finish:
  611. pm_notifier_call_chain(PM_POST_RESTORE);
  612. pm_restore_console();
  613. atomic_inc(&snapshot_device_available);
  614. /* For success case, the suspend path will release the lock */
  615. Unlock:
  616. mutex_unlock(&pm_mutex);
  617. pr_debug("PM: Resume from disk failed.\n");
  618. return error;
  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);