disk.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. sysdev_suspend(PMSG_FREEZE);
  205. if (error) {
  206. printk(KERN_ERR "PM: Some 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. * We can't depend on SCSI devices being available after loading one of
  536. * their modules if scsi_complete_async_scans() is not called and the
  537. * resume device usually is a SCSI one.
  538. */
  539. scsi_complete_async_scans();
  540. /*
  541. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  542. * is configured into the kernel. Since the regular hibernate
  543. * trigger path is via sysfs which takes a buffer mutex before
  544. * calling hibernate functions (which take pm_mutex) this can
  545. * cause lockdep to complain about a possible ABBA deadlock
  546. * which cannot happen since we're in the boot code here and
  547. * sysfs can't be invoked yet. Therefore, we use a subclass
  548. * here to avoid lockdep complaining.
  549. */
  550. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  551. if (!swsusp_resume_device) {
  552. if (!strlen(resume_file)) {
  553. mutex_unlock(&pm_mutex);
  554. return -ENOENT;
  555. }
  556. /*
  557. * Some device discovery might still be in progress; we need
  558. * to wait for this to finish.
  559. */
  560. wait_for_device_probe();
  561. swsusp_resume_device = name_to_dev_t(resume_file);
  562. pr_debug("PM: Resume from partition %s\n", resume_file);
  563. } else {
  564. pr_debug("PM: Resume from partition %d:%d\n",
  565. MAJOR(swsusp_resume_device),
  566. MINOR(swsusp_resume_device));
  567. }
  568. if (noresume) {
  569. /**
  570. * FIXME: If noresume is specified, we need to find the
  571. * partition and reset it back to normal swap space.
  572. */
  573. mutex_unlock(&pm_mutex);
  574. return 0;
  575. }
  576. pr_debug("PM: Checking hibernation image.\n");
  577. error = swsusp_check();
  578. if (error)
  579. goto Unlock;
  580. /* The snapshot device should not be opened while we're running */
  581. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  582. error = -EBUSY;
  583. goto Unlock;
  584. }
  585. pm_prepare_console();
  586. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  587. if (error)
  588. goto Finish;
  589. error = usermodehelper_disable();
  590. if (error)
  591. goto Finish;
  592. error = create_basic_memory_bitmaps();
  593. if (error)
  594. goto Finish;
  595. pr_debug("PM: Preparing processes for restore.\n");
  596. error = prepare_processes();
  597. if (error) {
  598. swsusp_close(FMODE_READ);
  599. goto Done;
  600. }
  601. pr_debug("PM: Reading hibernation image.\n");
  602. error = swsusp_read(&flags);
  603. if (!error)
  604. hibernation_restore(flags & SF_PLATFORM_MODE);
  605. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  606. swsusp_free();
  607. thaw_processes();
  608. Done:
  609. free_basic_memory_bitmaps();
  610. usermodehelper_enable();
  611. Finish:
  612. pm_notifier_call_chain(PM_POST_RESTORE);
  613. pm_restore_console();
  614. atomic_inc(&snapshot_device_available);
  615. /* For success case, the suspend path will release the lock */
  616. Unlock:
  617. mutex_unlock(&pm_mutex);
  618. pr_debug("PM: Resume from disk failed.\n");
  619. return error;
  620. }
  621. late_initcall(software_resume);
  622. static const char * const hibernation_modes[] = {
  623. [HIBERNATION_PLATFORM] = "platform",
  624. [HIBERNATION_SHUTDOWN] = "shutdown",
  625. [HIBERNATION_REBOOT] = "reboot",
  626. [HIBERNATION_TEST] = "test",
  627. [HIBERNATION_TESTPROC] = "testproc",
  628. };
  629. /**
  630. * disk - Control hibernation mode
  631. *
  632. * Suspend-to-disk can be handled in several ways. We have a few options
  633. * for putting the system to sleep - using the platform driver (e.g. ACPI
  634. * or other hibernation_ops), powering off the system or rebooting the
  635. * system (for testing) as well as the two test modes.
  636. *
  637. * The system can support 'platform', and that is known a priori (and
  638. * encoded by the presence of hibernation_ops). However, the user may
  639. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  640. * test modes, 'test' or 'testproc'.
  641. *
  642. * show() will display what the mode is currently set to.
  643. * store() will accept one of
  644. *
  645. * 'platform'
  646. * 'shutdown'
  647. * 'reboot'
  648. * 'test'
  649. * 'testproc'
  650. *
  651. * It will only change to 'platform' if the system
  652. * supports it (as determined by having hibernation_ops).
  653. */
  654. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  655. char *buf)
  656. {
  657. int i;
  658. char *start = buf;
  659. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  660. if (!hibernation_modes[i])
  661. continue;
  662. switch (i) {
  663. case HIBERNATION_SHUTDOWN:
  664. case HIBERNATION_REBOOT:
  665. case HIBERNATION_TEST:
  666. case HIBERNATION_TESTPROC:
  667. break;
  668. case HIBERNATION_PLATFORM:
  669. if (hibernation_ops)
  670. break;
  671. /* not a valid mode, continue with loop */
  672. continue;
  673. }
  674. if (i == hibernation_mode)
  675. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  676. else
  677. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  678. }
  679. buf += sprintf(buf, "\n");
  680. return buf-start;
  681. }
  682. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  683. const char *buf, size_t n)
  684. {
  685. int error = 0;
  686. int i;
  687. int len;
  688. char *p;
  689. int mode = HIBERNATION_INVALID;
  690. p = memchr(buf, '\n', n);
  691. len = p ? p - buf : n;
  692. mutex_lock(&pm_mutex);
  693. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  694. if (len == strlen(hibernation_modes[i])
  695. && !strncmp(buf, hibernation_modes[i], len)) {
  696. mode = i;
  697. break;
  698. }
  699. }
  700. if (mode != HIBERNATION_INVALID) {
  701. switch (mode) {
  702. case HIBERNATION_SHUTDOWN:
  703. case HIBERNATION_REBOOT:
  704. case HIBERNATION_TEST:
  705. case HIBERNATION_TESTPROC:
  706. hibernation_mode = mode;
  707. break;
  708. case HIBERNATION_PLATFORM:
  709. if (hibernation_ops)
  710. hibernation_mode = mode;
  711. else
  712. error = -EINVAL;
  713. }
  714. } else
  715. error = -EINVAL;
  716. if (!error)
  717. pr_debug("PM: Hibernation mode set to '%s'\n",
  718. hibernation_modes[mode]);
  719. mutex_unlock(&pm_mutex);
  720. return error ? error : n;
  721. }
  722. power_attr(disk);
  723. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  724. char *buf)
  725. {
  726. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  727. MINOR(swsusp_resume_device));
  728. }
  729. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  730. const char *buf, size_t n)
  731. {
  732. unsigned int maj, min;
  733. dev_t res;
  734. int ret = -EINVAL;
  735. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  736. goto out;
  737. res = MKDEV(maj,min);
  738. if (maj != MAJOR(res) || min != MINOR(res))
  739. goto out;
  740. mutex_lock(&pm_mutex);
  741. swsusp_resume_device = res;
  742. mutex_unlock(&pm_mutex);
  743. printk(KERN_INFO "PM: Starting manual resume from disk\n");
  744. noresume = 0;
  745. software_resume();
  746. ret = n;
  747. out:
  748. return ret;
  749. }
  750. power_attr(resume);
  751. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  752. char *buf)
  753. {
  754. return sprintf(buf, "%lu\n", image_size);
  755. }
  756. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  757. const char *buf, size_t n)
  758. {
  759. unsigned long size;
  760. if (sscanf(buf, "%lu", &size) == 1) {
  761. image_size = size;
  762. return n;
  763. }
  764. return -EINVAL;
  765. }
  766. power_attr(image_size);
  767. static struct attribute * g[] = {
  768. &disk_attr.attr,
  769. &resume_attr.attr,
  770. &image_size_attr.attr,
  771. NULL,
  772. };
  773. static struct attribute_group attr_group = {
  774. .attrs = g,
  775. };
  776. static int __init pm_disk_init(void)
  777. {
  778. return sysfs_create_group(power_kobj, &attr_group);
  779. }
  780. core_initcall(pm_disk_init);
  781. static int __init resume_setup(char *str)
  782. {
  783. if (noresume)
  784. return 1;
  785. strncpy( resume_file, str, 255 );
  786. return 1;
  787. }
  788. static int __init resume_offset_setup(char *str)
  789. {
  790. unsigned long long offset;
  791. if (noresume)
  792. return 1;
  793. if (sscanf(str, "%llu", &offset) == 1)
  794. swsusp_resume_block = offset;
  795. return 1;
  796. }
  797. static int __init noresume_setup(char *str)
  798. {
  799. noresume = 1;
  800. return 1;
  801. }
  802. __setup("noresume", noresume_setup);
  803. __setup("resume_offset=", resume_offset_setup);
  804. __setup("resume=", resume_setup);