disk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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/delay.h>
  17. #include <linux/fs.h>
  18. #include <linux/mount.h>
  19. #include <linux/pm.h>
  20. #include <linux/console.h>
  21. #include <linux/cpu.h>
  22. #include <linux/freezer.h>
  23. #include "power.h"
  24. static int noresume = 0;
  25. char resume_file[256] = CONFIG_PM_STD_PARTITION;
  26. dev_t swsusp_resume_device;
  27. sector_t swsusp_resume_block;
  28. enum {
  29. HIBERNATION_INVALID,
  30. HIBERNATION_PLATFORM,
  31. HIBERNATION_TEST,
  32. HIBERNATION_TESTPROC,
  33. HIBERNATION_SHUTDOWN,
  34. HIBERNATION_REBOOT,
  35. /* keep last */
  36. __HIBERNATION_AFTER_LAST
  37. };
  38. #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
  39. #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
  40. static int hibernation_mode = HIBERNATION_SHUTDOWN;
  41. static struct platform_hibernation_ops *hibernation_ops;
  42. /**
  43. * hibernation_set_ops - set the global hibernate operations
  44. * @ops: the hibernation operations to use in subsequent hibernation transitions
  45. */
  46. void hibernation_set_ops(struct platform_hibernation_ops *ops)
  47. {
  48. if (ops && !(ops->start && ops->pre_snapshot && ops->finish
  49. && ops->prepare && ops->enter && ops->pre_restore
  50. && ops->restore_cleanup)) {
  51. WARN_ON(1);
  52. return;
  53. }
  54. mutex_lock(&pm_mutex);
  55. hibernation_ops = ops;
  56. if (ops)
  57. hibernation_mode = HIBERNATION_PLATFORM;
  58. else if (hibernation_mode == HIBERNATION_PLATFORM)
  59. hibernation_mode = HIBERNATION_SHUTDOWN;
  60. mutex_unlock(&pm_mutex);
  61. }
  62. /**
  63. * platform_start - tell the platform driver that we're starting
  64. * hibernation
  65. */
  66. static int platform_start(int platform_mode)
  67. {
  68. return (platform_mode && hibernation_ops) ?
  69. hibernation_ops->start() : 0;
  70. }
  71. /**
  72. * platform_pre_snapshot - prepare the machine for hibernation using the
  73. * platform driver if so configured and return an error code if it fails
  74. */
  75. static int platform_pre_snapshot(int platform_mode)
  76. {
  77. return (platform_mode && hibernation_ops) ?
  78. hibernation_ops->pre_snapshot() : 0;
  79. }
  80. /**
  81. * platform_leave - prepare the machine for switching to the normal mode
  82. * of operation using the platform driver (called with interrupts disabled)
  83. */
  84. static void platform_leave(int platform_mode)
  85. {
  86. if (platform_mode && hibernation_ops)
  87. hibernation_ops->leave();
  88. }
  89. /**
  90. * platform_finish - switch the machine to the normal mode of operation
  91. * using the platform driver (must be called after platform_prepare())
  92. */
  93. static void platform_finish(int platform_mode)
  94. {
  95. if (platform_mode && hibernation_ops)
  96. hibernation_ops->finish();
  97. }
  98. /**
  99. * platform_pre_restore - prepare the platform for the restoration from a
  100. * hibernation image. If the restore fails after this function has been
  101. * called, platform_restore_cleanup() must be called.
  102. */
  103. static int platform_pre_restore(int platform_mode)
  104. {
  105. return (platform_mode && hibernation_ops) ?
  106. hibernation_ops->pre_restore() : 0;
  107. }
  108. /**
  109. * platform_restore_cleanup - switch the platform to the normal mode of
  110. * operation after a failing restore. If platform_pre_restore() has been
  111. * called before the failing restore, this function must be called too,
  112. * regardless of the result of platform_pre_restore().
  113. */
  114. static void platform_restore_cleanup(int platform_mode)
  115. {
  116. if (platform_mode && hibernation_ops)
  117. hibernation_ops->restore_cleanup();
  118. }
  119. /**
  120. * create_image - freeze devices that need to be frozen with interrupts
  121. * off, create the hibernation image and thaw those devices. Control
  122. * reappears in this routine after a restore.
  123. */
  124. int create_image(int platform_mode)
  125. {
  126. int error;
  127. error = arch_prepare_suspend();
  128. if (error)
  129. return error;
  130. local_irq_disable();
  131. /* At this point, device_suspend() has been called, but *not*
  132. * device_power_down(). We *must* call device_power_down() now.
  133. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  134. * become desynchronized with the actual state of the hardware
  135. * at resume time, and evil weirdness ensues.
  136. */
  137. error = device_power_down(PMSG_FREEZE);
  138. if (error) {
  139. printk(KERN_ERR "Some devices failed to power down, "
  140. KERN_ERR "aborting suspend\n");
  141. goto Enable_irqs;
  142. }
  143. save_processor_state();
  144. error = swsusp_arch_suspend();
  145. if (error)
  146. printk(KERN_ERR "Error %d while creating the image\n", error);
  147. /* Restore control flow magically appears here */
  148. restore_processor_state();
  149. if (!in_suspend)
  150. platform_leave(platform_mode);
  151. /* NOTE: device_power_up() is just a resume() for devices
  152. * that suspended with irqs off ... no overall powerup.
  153. */
  154. device_power_up();
  155. Enable_irqs:
  156. local_irq_enable();
  157. return error;
  158. }
  159. /**
  160. * hibernation_snapshot - quiesce devices and create the hibernation
  161. * snapshot image.
  162. * @platform_mode - if set, use the platform driver, if available, to
  163. * prepare the platform frimware for the power transition.
  164. *
  165. * Must be called with pm_mutex held
  166. */
  167. int hibernation_snapshot(int platform_mode)
  168. {
  169. int error;
  170. /* Free memory before shutting down devices. */
  171. error = swsusp_shrink_memory();
  172. if (error)
  173. return error;
  174. error = platform_start(platform_mode);
  175. if (error)
  176. return error;
  177. suspend_console();
  178. error = device_suspend(PMSG_FREEZE);
  179. if (error)
  180. goto Resume_console;
  181. error = platform_pre_snapshot(platform_mode);
  182. if (error)
  183. goto Resume_devices;
  184. error = disable_nonboot_cpus();
  185. if (!error) {
  186. if (hibernation_mode != HIBERNATION_TEST) {
  187. in_suspend = 1;
  188. error = create_image(platform_mode);
  189. /* Control returns here after successful restore */
  190. } else {
  191. printk("swsusp debug: Waiting for 5 seconds.\n");
  192. mdelay(5000);
  193. }
  194. }
  195. enable_nonboot_cpus();
  196. Resume_devices:
  197. platform_finish(platform_mode);
  198. device_resume();
  199. Resume_console:
  200. resume_console();
  201. return error;
  202. }
  203. /**
  204. * hibernation_restore - quiesce devices and restore the hibernation
  205. * snapshot image. If successful, control returns in hibernation_snaphot()
  206. * @platform_mode - if set, use the platform driver, if available, to
  207. * prepare the platform frimware for the transition.
  208. *
  209. * Must be called with pm_mutex held
  210. */
  211. int hibernation_restore(int platform_mode)
  212. {
  213. int error;
  214. pm_prepare_console();
  215. suspend_console();
  216. error = device_suspend(PMSG_PRETHAW);
  217. if (error)
  218. goto Finish;
  219. error = platform_pre_restore(platform_mode);
  220. if (!error) {
  221. error = disable_nonboot_cpus();
  222. if (!error)
  223. error = swsusp_resume();
  224. enable_nonboot_cpus();
  225. }
  226. platform_restore_cleanup(platform_mode);
  227. device_resume();
  228. Finish:
  229. resume_console();
  230. pm_restore_console();
  231. return error;
  232. }
  233. /**
  234. * hibernation_platform_enter - enter the hibernation state using the
  235. * platform driver (if available)
  236. */
  237. int hibernation_platform_enter(void)
  238. {
  239. int error;
  240. if (!hibernation_ops)
  241. return -ENOSYS;
  242. /*
  243. * We have cancelled the power transition by running
  244. * hibernation_ops->finish() before saving the image, so we should let
  245. * the firmware know that we're going to enter the sleep state after all
  246. */
  247. error = hibernation_ops->start();
  248. if (error)
  249. return error;
  250. suspend_console();
  251. error = device_suspend(PMSG_SUSPEND);
  252. if (error)
  253. goto Resume_console;
  254. error = hibernation_ops->prepare();
  255. if (error)
  256. goto Resume_devices;
  257. error = disable_nonboot_cpus();
  258. if (error)
  259. goto Finish;
  260. local_irq_disable();
  261. error = device_power_down(PMSG_SUSPEND);
  262. if (!error) {
  263. hibernation_ops->enter();
  264. /* We should never get here */
  265. while (1);
  266. }
  267. local_irq_enable();
  268. /*
  269. * We don't need to reenable the nonboot CPUs or resume consoles, since
  270. * the system is going to be halted anyway.
  271. */
  272. Finish:
  273. hibernation_ops->finish();
  274. Resume_devices:
  275. device_resume();
  276. Resume_console:
  277. resume_console();
  278. return error;
  279. }
  280. /**
  281. * power_down - Shut the machine down for hibernation.
  282. *
  283. * Use the platform driver, if configured so; otherwise try
  284. * to power off or reboot.
  285. */
  286. static void power_down(void)
  287. {
  288. switch (hibernation_mode) {
  289. case HIBERNATION_TEST:
  290. case HIBERNATION_TESTPROC:
  291. break;
  292. case HIBERNATION_REBOOT:
  293. kernel_restart(NULL);
  294. break;
  295. case HIBERNATION_PLATFORM:
  296. hibernation_platform_enter();
  297. case HIBERNATION_SHUTDOWN:
  298. kernel_power_off();
  299. break;
  300. }
  301. kernel_halt();
  302. /*
  303. * Valid image is on the disk, if we continue we risk serious data
  304. * corruption after resume.
  305. */
  306. printk(KERN_CRIT "Please power me down manually\n");
  307. while(1);
  308. }
  309. static void unprepare_processes(void)
  310. {
  311. thaw_processes();
  312. pm_restore_console();
  313. }
  314. static int prepare_processes(void)
  315. {
  316. int error = 0;
  317. pm_prepare_console();
  318. if (freeze_processes()) {
  319. error = -EBUSY;
  320. unprepare_processes();
  321. }
  322. return error;
  323. }
  324. /**
  325. * hibernate - The granpappy of the built-in hibernation management
  326. */
  327. int hibernate(void)
  328. {
  329. int error;
  330. mutex_lock(&pm_mutex);
  331. /* The snapshot device should not be opened while we're running */
  332. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  333. error = -EBUSY;
  334. goto Unlock;
  335. }
  336. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  337. if (error)
  338. goto Exit;
  339. /* Allocate memory management structures */
  340. error = create_basic_memory_bitmaps();
  341. if (error)
  342. goto Exit;
  343. printk("Syncing filesystems ... ");
  344. sys_sync();
  345. printk("done.\n");
  346. error = prepare_processes();
  347. if (error)
  348. goto Finish;
  349. if (hibernation_mode == HIBERNATION_TESTPROC) {
  350. printk("swsusp debug: Waiting for 5 seconds.\n");
  351. mdelay(5000);
  352. goto Thaw;
  353. }
  354. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  355. if (in_suspend && !error) {
  356. unsigned int flags = 0;
  357. if (hibernation_mode == HIBERNATION_PLATFORM)
  358. flags |= SF_PLATFORM_MODE;
  359. pr_debug("PM: writing image.\n");
  360. error = swsusp_write(flags);
  361. swsusp_free();
  362. if (!error)
  363. power_down();
  364. } else {
  365. pr_debug("PM: Image restored successfully.\n");
  366. swsusp_free();
  367. }
  368. Thaw:
  369. unprepare_processes();
  370. Finish:
  371. free_basic_memory_bitmaps();
  372. Exit:
  373. pm_notifier_call_chain(PM_POST_HIBERNATION);
  374. atomic_inc(&snapshot_device_available);
  375. Unlock:
  376. mutex_unlock(&pm_mutex);
  377. return error;
  378. }
  379. /**
  380. * software_resume - Resume from a saved image.
  381. *
  382. * Called as a late_initcall (so all devices are discovered and
  383. * initialized), we call swsusp to see if we have a saved image or not.
  384. * If so, we quiesce devices, the restore the saved image. We will
  385. * return above (in hibernate() ) if everything goes well.
  386. * Otherwise, we fail gracefully and return to the normally
  387. * scheduled program.
  388. *
  389. */
  390. static int software_resume(void)
  391. {
  392. int error;
  393. unsigned int flags;
  394. mutex_lock(&pm_mutex);
  395. if (!swsusp_resume_device) {
  396. if (!strlen(resume_file)) {
  397. mutex_unlock(&pm_mutex);
  398. return -ENOENT;
  399. }
  400. swsusp_resume_device = name_to_dev_t(resume_file);
  401. pr_debug("swsusp: Resume From Partition %s\n", resume_file);
  402. } else {
  403. pr_debug("swsusp: Resume From Partition %d:%d\n",
  404. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  405. }
  406. if (noresume) {
  407. /**
  408. * FIXME: If noresume is specified, we need to find the partition
  409. * and reset it back to normal swap space.
  410. */
  411. mutex_unlock(&pm_mutex);
  412. return 0;
  413. }
  414. pr_debug("PM: Checking swsusp image.\n");
  415. error = swsusp_check();
  416. if (error)
  417. goto Unlock;
  418. /* The snapshot device should not be opened while we're running */
  419. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  420. error = -EBUSY;
  421. goto Unlock;
  422. }
  423. error = create_basic_memory_bitmaps();
  424. if (error)
  425. goto Finish;
  426. pr_debug("PM: Preparing processes for restore.\n");
  427. error = prepare_processes();
  428. if (error) {
  429. swsusp_close();
  430. goto Done;
  431. }
  432. pr_debug("PM: Reading swsusp image.\n");
  433. error = swsusp_read(&flags);
  434. if (!error)
  435. hibernation_restore(flags & SF_PLATFORM_MODE);
  436. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  437. swsusp_free();
  438. unprepare_processes();
  439. Done:
  440. free_basic_memory_bitmaps();
  441. Finish:
  442. atomic_inc(&snapshot_device_available);
  443. /* For success case, the suspend path will release the lock */
  444. Unlock:
  445. mutex_unlock(&pm_mutex);
  446. pr_debug("PM: Resume from disk failed.\n");
  447. return error;
  448. }
  449. late_initcall(software_resume);
  450. static const char * const hibernation_modes[] = {
  451. [HIBERNATION_PLATFORM] = "platform",
  452. [HIBERNATION_SHUTDOWN] = "shutdown",
  453. [HIBERNATION_REBOOT] = "reboot",
  454. [HIBERNATION_TEST] = "test",
  455. [HIBERNATION_TESTPROC] = "testproc",
  456. };
  457. /**
  458. * disk - Control hibernation mode
  459. *
  460. * Suspend-to-disk can be handled in several ways. We have a few options
  461. * for putting the system to sleep - using the platform driver (e.g. ACPI
  462. * or other hibernation_ops), powering off the system or rebooting the
  463. * system (for testing) as well as the two test modes.
  464. *
  465. * The system can support 'platform', and that is known a priori (and
  466. * encoded by the presence of hibernation_ops). However, the user may
  467. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  468. * test modes, 'test' or 'testproc'.
  469. *
  470. * show() will display what the mode is currently set to.
  471. * store() will accept one of
  472. *
  473. * 'platform'
  474. * 'shutdown'
  475. * 'reboot'
  476. * 'test'
  477. * 'testproc'
  478. *
  479. * It will only change to 'platform' if the system
  480. * supports it (as determined by having hibernation_ops).
  481. */
  482. static ssize_t disk_show(struct kset *kset, char *buf)
  483. {
  484. int i;
  485. char *start = buf;
  486. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  487. if (!hibernation_modes[i])
  488. continue;
  489. switch (i) {
  490. case HIBERNATION_SHUTDOWN:
  491. case HIBERNATION_REBOOT:
  492. case HIBERNATION_TEST:
  493. case HIBERNATION_TESTPROC:
  494. break;
  495. case HIBERNATION_PLATFORM:
  496. if (hibernation_ops)
  497. break;
  498. /* not a valid mode, continue with loop */
  499. continue;
  500. }
  501. if (i == hibernation_mode)
  502. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  503. else
  504. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  505. }
  506. buf += sprintf(buf, "\n");
  507. return buf-start;
  508. }
  509. static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
  510. {
  511. int error = 0;
  512. int i;
  513. int len;
  514. char *p;
  515. int mode = HIBERNATION_INVALID;
  516. p = memchr(buf, '\n', n);
  517. len = p ? p - buf : n;
  518. mutex_lock(&pm_mutex);
  519. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  520. if (len == strlen(hibernation_modes[i])
  521. && !strncmp(buf, hibernation_modes[i], len)) {
  522. mode = i;
  523. break;
  524. }
  525. }
  526. if (mode != HIBERNATION_INVALID) {
  527. switch (mode) {
  528. case HIBERNATION_SHUTDOWN:
  529. case HIBERNATION_REBOOT:
  530. case HIBERNATION_TEST:
  531. case HIBERNATION_TESTPROC:
  532. hibernation_mode = mode;
  533. break;
  534. case HIBERNATION_PLATFORM:
  535. if (hibernation_ops)
  536. hibernation_mode = mode;
  537. else
  538. error = -EINVAL;
  539. }
  540. } else
  541. error = -EINVAL;
  542. if (!error)
  543. pr_debug("PM: suspend-to-disk mode set to '%s'\n",
  544. hibernation_modes[mode]);
  545. mutex_unlock(&pm_mutex);
  546. return error ? error : n;
  547. }
  548. power_attr(disk);
  549. static ssize_t resume_show(struct kset *kset, char *buf)
  550. {
  551. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  552. MINOR(swsusp_resume_device));
  553. }
  554. static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
  555. {
  556. unsigned int maj, min;
  557. dev_t res;
  558. int ret = -EINVAL;
  559. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  560. goto out;
  561. res = MKDEV(maj,min);
  562. if (maj != MAJOR(res) || min != MINOR(res))
  563. goto out;
  564. mutex_lock(&pm_mutex);
  565. swsusp_resume_device = res;
  566. mutex_unlock(&pm_mutex);
  567. printk("Attempting manual resume\n");
  568. noresume = 0;
  569. software_resume();
  570. ret = n;
  571. out:
  572. return ret;
  573. }
  574. power_attr(resume);
  575. static ssize_t image_size_show(struct kset *kset, char *buf)
  576. {
  577. return sprintf(buf, "%lu\n", image_size);
  578. }
  579. static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
  580. {
  581. unsigned long size;
  582. if (sscanf(buf, "%lu", &size) == 1) {
  583. image_size = size;
  584. return n;
  585. }
  586. return -EINVAL;
  587. }
  588. power_attr(image_size);
  589. static struct attribute * g[] = {
  590. &disk_attr.attr,
  591. &resume_attr.attr,
  592. &image_size_attr.attr,
  593. NULL,
  594. };
  595. static struct attribute_group attr_group = {
  596. .attrs = g,
  597. };
  598. static int __init pm_disk_init(void)
  599. {
  600. return sysfs_create_group(&power_subsys.kobj, &attr_group);
  601. }
  602. core_initcall(pm_disk_init);
  603. static int __init resume_setup(char *str)
  604. {
  605. if (noresume)
  606. return 1;
  607. strncpy( resume_file, str, 255 );
  608. return 1;
  609. }
  610. static int __init resume_offset_setup(char *str)
  611. {
  612. unsigned long long offset;
  613. if (noresume)
  614. return 1;
  615. if (sscanf(str, "%llu", &offset) == 1)
  616. swsusp_resume_block = offset;
  617. return 1;
  618. }
  619. static int __init noresume_setup(char *str)
  620. {
  621. noresume = 1;
  622. return 1;
  623. }
  624. __setup("noresume", noresume_setup);
  625. __setup("resume_offset=", resume_offset_setup);
  626. __setup("resume=", resume_setup);