disk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
  242. /*
  243. * We have cancelled the power transition by running
  244. * hibernation_ops->finish() before saving the image, so we
  245. * should let the firmware know that we're going to enter the
  246. * sleep state after all
  247. */
  248. error = hibernation_ops->prepare();
  249. sysdev_shutdown();
  250. if (!error)
  251. error = hibernation_ops->enter();
  252. } else {
  253. error = -ENOSYS;
  254. }
  255. return error;
  256. }
  257. /**
  258. * power_down - Shut the machine down for hibernation.
  259. *
  260. * Use the platform driver, if configured so; otherwise try
  261. * to power off or reboot.
  262. */
  263. static void power_down(void)
  264. {
  265. switch (hibernation_mode) {
  266. case HIBERNATION_TEST:
  267. case HIBERNATION_TESTPROC:
  268. break;
  269. case HIBERNATION_SHUTDOWN:
  270. kernel_power_off();
  271. break;
  272. case HIBERNATION_REBOOT:
  273. kernel_restart(NULL);
  274. break;
  275. case HIBERNATION_PLATFORM:
  276. hibernation_platform_enter();
  277. }
  278. kernel_halt();
  279. /*
  280. * Valid image is on the disk, if we continue we risk serious data
  281. * corruption after resume.
  282. */
  283. printk(KERN_CRIT "Please power me down manually\n");
  284. while(1);
  285. }
  286. static void unprepare_processes(void)
  287. {
  288. thaw_processes();
  289. pm_restore_console();
  290. }
  291. static int prepare_processes(void)
  292. {
  293. int error = 0;
  294. pm_prepare_console();
  295. if (freeze_processes()) {
  296. error = -EBUSY;
  297. unprepare_processes();
  298. }
  299. return error;
  300. }
  301. /**
  302. * hibernate - The granpappy of the built-in hibernation management
  303. */
  304. int hibernate(void)
  305. {
  306. int error;
  307. mutex_lock(&pm_mutex);
  308. /* The snapshot device should not be opened while we're running */
  309. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  310. error = -EBUSY;
  311. goto Unlock;
  312. }
  313. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  314. if (error)
  315. goto Exit;
  316. /* Allocate memory management structures */
  317. error = create_basic_memory_bitmaps();
  318. if (error)
  319. goto Exit;
  320. printk("Syncing filesystems ... ");
  321. sys_sync();
  322. printk("done.\n");
  323. error = prepare_processes();
  324. if (error)
  325. goto Finish;
  326. if (hibernation_mode == HIBERNATION_TESTPROC) {
  327. printk("swsusp debug: Waiting for 5 seconds.\n");
  328. mdelay(5000);
  329. goto Thaw;
  330. }
  331. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  332. if (in_suspend && !error) {
  333. unsigned int flags = 0;
  334. if (hibernation_mode == HIBERNATION_PLATFORM)
  335. flags |= SF_PLATFORM_MODE;
  336. pr_debug("PM: writing image.\n");
  337. error = swsusp_write(flags);
  338. swsusp_free();
  339. if (!error)
  340. power_down();
  341. } else {
  342. pr_debug("PM: Image restored successfully.\n");
  343. swsusp_free();
  344. }
  345. Thaw:
  346. unprepare_processes();
  347. Finish:
  348. free_basic_memory_bitmaps();
  349. Exit:
  350. pm_notifier_call_chain(PM_POST_HIBERNATION);
  351. atomic_inc(&snapshot_device_available);
  352. Unlock:
  353. mutex_unlock(&pm_mutex);
  354. return error;
  355. }
  356. /**
  357. * software_resume - Resume from a saved image.
  358. *
  359. * Called as a late_initcall (so all devices are discovered and
  360. * initialized), we call swsusp to see if we have a saved image or not.
  361. * If so, we quiesce devices, the restore the saved image. We will
  362. * return above (in hibernate() ) if everything goes well.
  363. * Otherwise, we fail gracefully and return to the normally
  364. * scheduled program.
  365. *
  366. */
  367. static int software_resume(void)
  368. {
  369. int error;
  370. unsigned int flags;
  371. mutex_lock(&pm_mutex);
  372. if (!swsusp_resume_device) {
  373. if (!strlen(resume_file)) {
  374. mutex_unlock(&pm_mutex);
  375. return -ENOENT;
  376. }
  377. swsusp_resume_device = name_to_dev_t(resume_file);
  378. pr_debug("swsusp: Resume From Partition %s\n", resume_file);
  379. } else {
  380. pr_debug("swsusp: Resume From Partition %d:%d\n",
  381. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  382. }
  383. if (noresume) {
  384. /**
  385. * FIXME: If noresume is specified, we need to find the partition
  386. * and reset it back to normal swap space.
  387. */
  388. mutex_unlock(&pm_mutex);
  389. return 0;
  390. }
  391. pr_debug("PM: Checking swsusp image.\n");
  392. error = swsusp_check();
  393. if (error)
  394. goto Unlock;
  395. /* The snapshot device should not be opened while we're running */
  396. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  397. error = -EBUSY;
  398. goto Unlock;
  399. }
  400. error = create_basic_memory_bitmaps();
  401. if (error)
  402. goto Finish;
  403. pr_debug("PM: Preparing processes for restore.\n");
  404. error = prepare_processes();
  405. if (error) {
  406. swsusp_close();
  407. goto Done;
  408. }
  409. pr_debug("PM: Reading swsusp image.\n");
  410. error = swsusp_read(&flags);
  411. if (!error)
  412. hibernation_restore(flags & SF_PLATFORM_MODE);
  413. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  414. swsusp_free();
  415. unprepare_processes();
  416. Done:
  417. free_basic_memory_bitmaps();
  418. Finish:
  419. atomic_inc(&snapshot_device_available);
  420. /* For success case, the suspend path will release the lock */
  421. Unlock:
  422. mutex_unlock(&pm_mutex);
  423. pr_debug("PM: Resume from disk failed.\n");
  424. return error;
  425. }
  426. late_initcall(software_resume);
  427. static const char * const hibernation_modes[] = {
  428. [HIBERNATION_PLATFORM] = "platform",
  429. [HIBERNATION_SHUTDOWN] = "shutdown",
  430. [HIBERNATION_REBOOT] = "reboot",
  431. [HIBERNATION_TEST] = "test",
  432. [HIBERNATION_TESTPROC] = "testproc",
  433. };
  434. /**
  435. * disk - Control hibernation mode
  436. *
  437. * Suspend-to-disk can be handled in several ways. We have a few options
  438. * for putting the system to sleep - using the platform driver (e.g. ACPI
  439. * or other hibernation_ops), powering off the system or rebooting the
  440. * system (for testing) as well as the two test modes.
  441. *
  442. * The system can support 'platform', and that is known a priori (and
  443. * encoded by the presence of hibernation_ops). However, the user may
  444. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  445. * test modes, 'test' or 'testproc'.
  446. *
  447. * show() will display what the mode is currently set to.
  448. * store() will accept one of
  449. *
  450. * 'platform'
  451. * 'shutdown'
  452. * 'reboot'
  453. * 'test'
  454. * 'testproc'
  455. *
  456. * It will only change to 'platform' if the system
  457. * supports it (as determined by having hibernation_ops).
  458. */
  459. static ssize_t disk_show(struct kset *kset, char *buf)
  460. {
  461. int i;
  462. char *start = buf;
  463. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  464. if (!hibernation_modes[i])
  465. continue;
  466. switch (i) {
  467. case HIBERNATION_SHUTDOWN:
  468. case HIBERNATION_REBOOT:
  469. case HIBERNATION_TEST:
  470. case HIBERNATION_TESTPROC:
  471. break;
  472. case HIBERNATION_PLATFORM:
  473. if (hibernation_ops)
  474. break;
  475. /* not a valid mode, continue with loop */
  476. continue;
  477. }
  478. if (i == hibernation_mode)
  479. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  480. else
  481. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  482. }
  483. buf += sprintf(buf, "\n");
  484. return buf-start;
  485. }
  486. static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
  487. {
  488. int error = 0;
  489. int i;
  490. int len;
  491. char *p;
  492. int mode = HIBERNATION_INVALID;
  493. p = memchr(buf, '\n', n);
  494. len = p ? p - buf : n;
  495. mutex_lock(&pm_mutex);
  496. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  497. if (len == strlen(hibernation_modes[i])
  498. && !strncmp(buf, hibernation_modes[i], len)) {
  499. mode = i;
  500. break;
  501. }
  502. }
  503. if (mode != HIBERNATION_INVALID) {
  504. switch (mode) {
  505. case HIBERNATION_SHUTDOWN:
  506. case HIBERNATION_REBOOT:
  507. case HIBERNATION_TEST:
  508. case HIBERNATION_TESTPROC:
  509. hibernation_mode = mode;
  510. break;
  511. case HIBERNATION_PLATFORM:
  512. if (hibernation_ops)
  513. hibernation_mode = mode;
  514. else
  515. error = -EINVAL;
  516. }
  517. } else
  518. error = -EINVAL;
  519. if (!error)
  520. pr_debug("PM: suspend-to-disk mode set to '%s'\n",
  521. hibernation_modes[mode]);
  522. mutex_unlock(&pm_mutex);
  523. return error ? error : n;
  524. }
  525. power_attr(disk);
  526. static ssize_t resume_show(struct kset *kset, char *buf)
  527. {
  528. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  529. MINOR(swsusp_resume_device));
  530. }
  531. static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
  532. {
  533. unsigned int maj, min;
  534. dev_t res;
  535. int ret = -EINVAL;
  536. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  537. goto out;
  538. res = MKDEV(maj,min);
  539. if (maj != MAJOR(res) || min != MINOR(res))
  540. goto out;
  541. mutex_lock(&pm_mutex);
  542. swsusp_resume_device = res;
  543. mutex_unlock(&pm_mutex);
  544. printk("Attempting manual resume\n");
  545. noresume = 0;
  546. software_resume();
  547. ret = n;
  548. out:
  549. return ret;
  550. }
  551. power_attr(resume);
  552. static ssize_t image_size_show(struct kset *kset, char *buf)
  553. {
  554. return sprintf(buf, "%lu\n", image_size);
  555. }
  556. static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
  557. {
  558. unsigned long size;
  559. if (sscanf(buf, "%lu", &size) == 1) {
  560. image_size = size;
  561. return n;
  562. }
  563. return -EINVAL;
  564. }
  565. power_attr(image_size);
  566. static struct attribute * g[] = {
  567. &disk_attr.attr,
  568. &resume_attr.attr,
  569. &image_size_attr.attr,
  570. NULL,
  571. };
  572. static struct attribute_group attr_group = {
  573. .attrs = g,
  574. };
  575. static int __init pm_disk_init(void)
  576. {
  577. return sysfs_create_group(&power_subsys.kobj, &attr_group);
  578. }
  579. core_initcall(pm_disk_init);
  580. static int __init resume_setup(char *str)
  581. {
  582. if (noresume)
  583. return 1;
  584. strncpy( resume_file, str, 255 );
  585. return 1;
  586. }
  587. static int __init resume_offset_setup(char *str)
  588. {
  589. unsigned long long offset;
  590. if (noresume)
  591. return 1;
  592. if (sscanf(str, "%llu", &offset) == 1)
  593. swsusp_resume_block = offset;
  594. return 1;
  595. }
  596. static int __init noresume_setup(char *str)
  597. {
  598. noresume = 1;
  599. return 1;
  600. }
  601. __setup("noresume", noresume_setup);
  602. __setup("resume_offset=", resume_offset_setup);
  603. __setup("resume=", resume_setup);