disk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. /*
  395. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  396. * is configured into the kernel. Since the regular hibernate
  397. * trigger path is via sysfs which takes a buffer mutex before
  398. * calling hibernate functions (which take pm_mutex) this can
  399. * cause lockdep to complain about a possible ABBA deadlock
  400. * which cannot happen since we're in the boot code here and
  401. * sysfs can't be invoked yet. Therefore, we use a subclass
  402. * here to avoid lockdep complaining.
  403. */
  404. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  405. if (!swsusp_resume_device) {
  406. if (!strlen(resume_file)) {
  407. mutex_unlock(&pm_mutex);
  408. return -ENOENT;
  409. }
  410. swsusp_resume_device = name_to_dev_t(resume_file);
  411. pr_debug("swsusp: Resume From Partition %s\n", resume_file);
  412. } else {
  413. pr_debug("swsusp: Resume From Partition %d:%d\n",
  414. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  415. }
  416. if (noresume) {
  417. /**
  418. * FIXME: If noresume is specified, we need to find the partition
  419. * and reset it back to normal swap space.
  420. */
  421. mutex_unlock(&pm_mutex);
  422. return 0;
  423. }
  424. pr_debug("PM: Checking swsusp image.\n");
  425. error = swsusp_check();
  426. if (error)
  427. goto Unlock;
  428. /* The snapshot device should not be opened while we're running */
  429. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  430. error = -EBUSY;
  431. goto Unlock;
  432. }
  433. error = create_basic_memory_bitmaps();
  434. if (error)
  435. goto Finish;
  436. pr_debug("PM: Preparing processes for restore.\n");
  437. error = prepare_processes();
  438. if (error) {
  439. swsusp_close();
  440. goto Done;
  441. }
  442. pr_debug("PM: Reading swsusp image.\n");
  443. error = swsusp_read(&flags);
  444. if (!error)
  445. hibernation_restore(flags & SF_PLATFORM_MODE);
  446. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  447. swsusp_free();
  448. unprepare_processes();
  449. Done:
  450. free_basic_memory_bitmaps();
  451. Finish:
  452. atomic_inc(&snapshot_device_available);
  453. /* For success case, the suspend path will release the lock */
  454. Unlock:
  455. mutex_unlock(&pm_mutex);
  456. pr_debug("PM: Resume from disk failed.\n");
  457. return error;
  458. }
  459. late_initcall(software_resume);
  460. static const char * const hibernation_modes[] = {
  461. [HIBERNATION_PLATFORM] = "platform",
  462. [HIBERNATION_SHUTDOWN] = "shutdown",
  463. [HIBERNATION_REBOOT] = "reboot",
  464. [HIBERNATION_TEST] = "test",
  465. [HIBERNATION_TESTPROC] = "testproc",
  466. };
  467. /**
  468. * disk - Control hibernation mode
  469. *
  470. * Suspend-to-disk can be handled in several ways. We have a few options
  471. * for putting the system to sleep - using the platform driver (e.g. ACPI
  472. * or other hibernation_ops), powering off the system or rebooting the
  473. * system (for testing) as well as the two test modes.
  474. *
  475. * The system can support 'platform', and that is known a priori (and
  476. * encoded by the presence of hibernation_ops). However, the user may
  477. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  478. * test modes, 'test' or 'testproc'.
  479. *
  480. * show() will display what the mode is currently set to.
  481. * store() will accept one of
  482. *
  483. * 'platform'
  484. * 'shutdown'
  485. * 'reboot'
  486. * 'test'
  487. * 'testproc'
  488. *
  489. * It will only change to 'platform' if the system
  490. * supports it (as determined by having hibernation_ops).
  491. */
  492. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  493. char *buf)
  494. {
  495. int i;
  496. char *start = buf;
  497. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  498. if (!hibernation_modes[i])
  499. continue;
  500. switch (i) {
  501. case HIBERNATION_SHUTDOWN:
  502. case HIBERNATION_REBOOT:
  503. case HIBERNATION_TEST:
  504. case HIBERNATION_TESTPROC:
  505. break;
  506. case HIBERNATION_PLATFORM:
  507. if (hibernation_ops)
  508. break;
  509. /* not a valid mode, continue with loop */
  510. continue;
  511. }
  512. if (i == hibernation_mode)
  513. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  514. else
  515. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  516. }
  517. buf += sprintf(buf, "\n");
  518. return buf-start;
  519. }
  520. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  521. const char *buf, size_t n)
  522. {
  523. int error = 0;
  524. int i;
  525. int len;
  526. char *p;
  527. int mode = HIBERNATION_INVALID;
  528. p = memchr(buf, '\n', n);
  529. len = p ? p - buf : n;
  530. mutex_lock(&pm_mutex);
  531. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  532. if (len == strlen(hibernation_modes[i])
  533. && !strncmp(buf, hibernation_modes[i], len)) {
  534. mode = i;
  535. break;
  536. }
  537. }
  538. if (mode != HIBERNATION_INVALID) {
  539. switch (mode) {
  540. case HIBERNATION_SHUTDOWN:
  541. case HIBERNATION_REBOOT:
  542. case HIBERNATION_TEST:
  543. case HIBERNATION_TESTPROC:
  544. hibernation_mode = mode;
  545. break;
  546. case HIBERNATION_PLATFORM:
  547. if (hibernation_ops)
  548. hibernation_mode = mode;
  549. else
  550. error = -EINVAL;
  551. }
  552. } else
  553. error = -EINVAL;
  554. if (!error)
  555. pr_debug("PM: suspend-to-disk mode set to '%s'\n",
  556. hibernation_modes[mode]);
  557. mutex_unlock(&pm_mutex);
  558. return error ? error : n;
  559. }
  560. power_attr(disk);
  561. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  562. char *buf)
  563. {
  564. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  565. MINOR(swsusp_resume_device));
  566. }
  567. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  568. const char *buf, size_t n)
  569. {
  570. unsigned int maj, min;
  571. dev_t res;
  572. int ret = -EINVAL;
  573. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  574. goto out;
  575. res = MKDEV(maj,min);
  576. if (maj != MAJOR(res) || min != MINOR(res))
  577. goto out;
  578. mutex_lock(&pm_mutex);
  579. swsusp_resume_device = res;
  580. mutex_unlock(&pm_mutex);
  581. printk("Attempting manual resume\n");
  582. noresume = 0;
  583. software_resume();
  584. ret = n;
  585. out:
  586. return ret;
  587. }
  588. power_attr(resume);
  589. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  590. char *buf)
  591. {
  592. return sprintf(buf, "%lu\n", image_size);
  593. }
  594. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  595. const char *buf, size_t n)
  596. {
  597. unsigned long size;
  598. if (sscanf(buf, "%lu", &size) == 1) {
  599. image_size = size;
  600. return n;
  601. }
  602. return -EINVAL;
  603. }
  604. power_attr(image_size);
  605. static struct attribute * g[] = {
  606. &disk_attr.attr,
  607. &resume_attr.attr,
  608. &image_size_attr.attr,
  609. NULL,
  610. };
  611. static struct attribute_group attr_group = {
  612. .attrs = g,
  613. };
  614. static int __init pm_disk_init(void)
  615. {
  616. return sysfs_create_group(power_kobj, &attr_group);
  617. }
  618. core_initcall(pm_disk_init);
  619. static int __init resume_setup(char *str)
  620. {
  621. if (noresume)
  622. return 1;
  623. strncpy( resume_file, str, 255 );
  624. return 1;
  625. }
  626. static int __init resume_offset_setup(char *str)
  627. {
  628. unsigned long long offset;
  629. if (noresume)
  630. return 1;
  631. if (sscanf(str, "%llu", &offset) == 1)
  632. swsusp_resume_block = offset;
  633. return 1;
  634. }
  635. static int __init noresume_setup(char *str)
  636. {
  637. noresume = 1;
  638. return 1;
  639. }
  640. __setup("noresume", noresume_setup);
  641. __setup("resume_offset=", resume_offset_setup);
  642. __setup("resume=", resume_setup);