disk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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_finish - switch the machine to the normal mode of operation
  82. * using the platform driver (must be called after platform_prepare())
  83. */
  84. static void platform_finish(int platform_mode)
  85. {
  86. if (platform_mode && hibernation_ops)
  87. hibernation_ops->finish();
  88. }
  89. /**
  90. * platform_pre_restore - prepare the platform for the restoration from a
  91. * hibernation image. If the restore fails after this function has been
  92. * called, platform_restore_cleanup() must be called.
  93. */
  94. static int platform_pre_restore(int platform_mode)
  95. {
  96. return (platform_mode && hibernation_ops) ?
  97. hibernation_ops->pre_restore() : 0;
  98. }
  99. /**
  100. * platform_restore_cleanup - switch the platform to the normal mode of
  101. * operation after a failing restore. If platform_pre_restore() has been
  102. * called before the failing restore, this function must be called too,
  103. * regardless of the result of platform_pre_restore().
  104. */
  105. static void platform_restore_cleanup(int platform_mode)
  106. {
  107. if (platform_mode && hibernation_ops)
  108. hibernation_ops->restore_cleanup();
  109. }
  110. /**
  111. * hibernation_snapshot - quiesce devices and create the hibernation
  112. * snapshot image.
  113. * @platform_mode - if set, use the platform driver, if available, to
  114. * prepare the platform frimware for the power transition.
  115. *
  116. * Must be called with pm_mutex held
  117. */
  118. int hibernation_snapshot(int platform_mode)
  119. {
  120. int error;
  121. /* Free memory before shutting down devices. */
  122. error = swsusp_shrink_memory();
  123. if (error)
  124. return error;
  125. error = platform_start(platform_mode);
  126. if (error)
  127. return error;
  128. suspend_console();
  129. error = device_suspend(PMSG_FREEZE);
  130. if (error)
  131. goto Resume_console;
  132. error = platform_pre_snapshot(platform_mode);
  133. if (error)
  134. goto Resume_devices;
  135. error = disable_nonboot_cpus();
  136. if (!error) {
  137. if (hibernation_mode != HIBERNATION_TEST) {
  138. in_suspend = 1;
  139. error = swsusp_suspend();
  140. /* Control returns here after successful restore */
  141. } else {
  142. printk("swsusp debug: Waiting for 5 seconds.\n");
  143. mdelay(5000);
  144. }
  145. }
  146. enable_nonboot_cpus();
  147. Resume_devices:
  148. platform_finish(platform_mode);
  149. device_resume();
  150. Resume_console:
  151. resume_console();
  152. return error;
  153. }
  154. /**
  155. * hibernation_restore - quiesce devices and restore the hibernation
  156. * snapshot image. If successful, control returns in hibernation_snaphot()
  157. * @platform_mode - if set, use the platform driver, if available, to
  158. * prepare the platform frimware for the transition.
  159. *
  160. * Must be called with pm_mutex held
  161. */
  162. int hibernation_restore(int platform_mode)
  163. {
  164. int error;
  165. pm_prepare_console();
  166. suspend_console();
  167. error = device_suspend(PMSG_PRETHAW);
  168. if (error)
  169. goto Finish;
  170. error = platform_pre_restore(platform_mode);
  171. if (!error) {
  172. error = disable_nonboot_cpus();
  173. if (!error)
  174. error = swsusp_resume();
  175. enable_nonboot_cpus();
  176. }
  177. platform_restore_cleanup(platform_mode);
  178. device_resume();
  179. Finish:
  180. resume_console();
  181. pm_restore_console();
  182. return error;
  183. }
  184. /**
  185. * hibernation_platform_enter - enter the hibernation state using the
  186. * platform driver (if available)
  187. */
  188. int hibernation_platform_enter(void)
  189. {
  190. int error;
  191. if (hibernation_ops) {
  192. kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
  193. /*
  194. * We have cancelled the power transition by running
  195. * hibernation_ops->finish() before saving the image, so we
  196. * should let the firmware know that we're going to enter the
  197. * sleep state after all
  198. */
  199. error = hibernation_ops->prepare();
  200. sysdev_shutdown();
  201. if (!error)
  202. error = hibernation_ops->enter();
  203. } else {
  204. error = -ENOSYS;
  205. }
  206. return error;
  207. }
  208. /**
  209. * power_down - Shut the machine down for hibernation.
  210. *
  211. * Use the platform driver, if configured so; otherwise try
  212. * to power off or reboot.
  213. */
  214. static void power_down(void)
  215. {
  216. switch (hibernation_mode) {
  217. case HIBERNATION_TEST:
  218. case HIBERNATION_TESTPROC:
  219. break;
  220. case HIBERNATION_SHUTDOWN:
  221. kernel_power_off();
  222. break;
  223. case HIBERNATION_REBOOT:
  224. kernel_restart(NULL);
  225. break;
  226. case HIBERNATION_PLATFORM:
  227. hibernation_platform_enter();
  228. }
  229. kernel_halt();
  230. /*
  231. * Valid image is on the disk, if we continue we risk serious data
  232. * corruption after resume.
  233. */
  234. printk(KERN_CRIT "Please power me down manually\n");
  235. while(1);
  236. }
  237. static void unprepare_processes(void)
  238. {
  239. thaw_processes();
  240. pm_restore_console();
  241. }
  242. static int prepare_processes(void)
  243. {
  244. int error = 0;
  245. pm_prepare_console();
  246. if (freeze_processes()) {
  247. error = -EBUSY;
  248. unprepare_processes();
  249. }
  250. return error;
  251. }
  252. /**
  253. * hibernate - The granpappy of the built-in hibernation management
  254. */
  255. int hibernate(void)
  256. {
  257. int error;
  258. mutex_lock(&pm_mutex);
  259. /* The snapshot device should not be opened while we're running */
  260. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  261. error = -EBUSY;
  262. goto Unlock;
  263. }
  264. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  265. if (error)
  266. goto Exit;
  267. /* Allocate memory management structures */
  268. error = create_basic_memory_bitmaps();
  269. if (error)
  270. goto Exit;
  271. printk("Syncing filesystems ... ");
  272. sys_sync();
  273. printk("done.\n");
  274. error = prepare_processes();
  275. if (error)
  276. goto Finish;
  277. if (hibernation_mode == HIBERNATION_TESTPROC) {
  278. printk("swsusp debug: Waiting for 5 seconds.\n");
  279. mdelay(5000);
  280. goto Thaw;
  281. }
  282. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  283. if (in_suspend && !error) {
  284. unsigned int flags = 0;
  285. if (hibernation_mode == HIBERNATION_PLATFORM)
  286. flags |= SF_PLATFORM_MODE;
  287. pr_debug("PM: writing image.\n");
  288. error = swsusp_write(flags);
  289. swsusp_free();
  290. if (!error)
  291. power_down();
  292. } else {
  293. pr_debug("PM: Image restored successfully.\n");
  294. swsusp_free();
  295. }
  296. Thaw:
  297. unprepare_processes();
  298. Finish:
  299. free_basic_memory_bitmaps();
  300. Exit:
  301. pm_notifier_call_chain(PM_POST_HIBERNATION);
  302. atomic_inc(&snapshot_device_available);
  303. Unlock:
  304. mutex_unlock(&pm_mutex);
  305. return error;
  306. }
  307. /**
  308. * software_resume - Resume from a saved image.
  309. *
  310. * Called as a late_initcall (so all devices are discovered and
  311. * initialized), we call swsusp to see if we have a saved image or not.
  312. * If so, we quiesce devices, the restore the saved image. We will
  313. * return above (in hibernate() ) if everything goes well.
  314. * Otherwise, we fail gracefully and return to the normally
  315. * scheduled program.
  316. *
  317. */
  318. static int software_resume(void)
  319. {
  320. int error;
  321. unsigned int flags;
  322. mutex_lock(&pm_mutex);
  323. if (!swsusp_resume_device) {
  324. if (!strlen(resume_file)) {
  325. mutex_unlock(&pm_mutex);
  326. return -ENOENT;
  327. }
  328. swsusp_resume_device = name_to_dev_t(resume_file);
  329. pr_debug("swsusp: Resume From Partition %s\n", resume_file);
  330. } else {
  331. pr_debug("swsusp: Resume From Partition %d:%d\n",
  332. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  333. }
  334. if (noresume) {
  335. /**
  336. * FIXME: If noresume is specified, we need to find the partition
  337. * and reset it back to normal swap space.
  338. */
  339. mutex_unlock(&pm_mutex);
  340. return 0;
  341. }
  342. pr_debug("PM: Checking swsusp image.\n");
  343. error = swsusp_check();
  344. if (error)
  345. goto Unlock;
  346. /* The snapshot device should not be opened while we're running */
  347. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  348. error = -EBUSY;
  349. goto Unlock;
  350. }
  351. error = create_basic_memory_bitmaps();
  352. if (error)
  353. goto Finish;
  354. pr_debug("PM: Preparing processes for restore.\n");
  355. error = prepare_processes();
  356. if (error) {
  357. swsusp_close();
  358. goto Done;
  359. }
  360. pr_debug("PM: Reading swsusp image.\n");
  361. error = swsusp_read(&flags);
  362. if (!error)
  363. hibernation_restore(flags & SF_PLATFORM_MODE);
  364. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  365. swsusp_free();
  366. unprepare_processes();
  367. Done:
  368. free_basic_memory_bitmaps();
  369. Finish:
  370. atomic_inc(&snapshot_device_available);
  371. /* For success case, the suspend path will release the lock */
  372. Unlock:
  373. mutex_unlock(&pm_mutex);
  374. pr_debug("PM: Resume from disk failed.\n");
  375. return error;
  376. }
  377. late_initcall(software_resume);
  378. static const char * const hibernation_modes[] = {
  379. [HIBERNATION_PLATFORM] = "platform",
  380. [HIBERNATION_SHUTDOWN] = "shutdown",
  381. [HIBERNATION_REBOOT] = "reboot",
  382. [HIBERNATION_TEST] = "test",
  383. [HIBERNATION_TESTPROC] = "testproc",
  384. };
  385. /**
  386. * disk - Control hibernation mode
  387. *
  388. * Suspend-to-disk can be handled in several ways. We have a few options
  389. * for putting the system to sleep - using the platform driver (e.g. ACPI
  390. * or other hibernation_ops), powering off the system or rebooting the
  391. * system (for testing) as well as the two test modes.
  392. *
  393. * The system can support 'platform', and that is known a priori (and
  394. * encoded by the presence of hibernation_ops). However, the user may
  395. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  396. * test modes, 'test' or 'testproc'.
  397. *
  398. * show() will display what the mode is currently set to.
  399. * store() will accept one of
  400. *
  401. * 'platform'
  402. * 'shutdown'
  403. * 'reboot'
  404. * 'test'
  405. * 'testproc'
  406. *
  407. * It will only change to 'platform' if the system
  408. * supports it (as determined by having hibernation_ops).
  409. */
  410. static ssize_t disk_show(struct kset *kset, char *buf)
  411. {
  412. int i;
  413. char *start = buf;
  414. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  415. if (!hibernation_modes[i])
  416. continue;
  417. switch (i) {
  418. case HIBERNATION_SHUTDOWN:
  419. case HIBERNATION_REBOOT:
  420. case HIBERNATION_TEST:
  421. case HIBERNATION_TESTPROC:
  422. break;
  423. case HIBERNATION_PLATFORM:
  424. if (hibernation_ops)
  425. break;
  426. /* not a valid mode, continue with loop */
  427. continue;
  428. }
  429. if (i == hibernation_mode)
  430. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  431. else
  432. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  433. }
  434. buf += sprintf(buf, "\n");
  435. return buf-start;
  436. }
  437. static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
  438. {
  439. int error = 0;
  440. int i;
  441. int len;
  442. char *p;
  443. int mode = HIBERNATION_INVALID;
  444. p = memchr(buf, '\n', n);
  445. len = p ? p - buf : n;
  446. mutex_lock(&pm_mutex);
  447. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  448. if (len == strlen(hibernation_modes[i])
  449. && !strncmp(buf, hibernation_modes[i], len)) {
  450. mode = i;
  451. break;
  452. }
  453. }
  454. if (mode != HIBERNATION_INVALID) {
  455. switch (mode) {
  456. case HIBERNATION_SHUTDOWN:
  457. case HIBERNATION_REBOOT:
  458. case HIBERNATION_TEST:
  459. case HIBERNATION_TESTPROC:
  460. hibernation_mode = mode;
  461. break;
  462. case HIBERNATION_PLATFORM:
  463. if (hibernation_ops)
  464. hibernation_mode = mode;
  465. else
  466. error = -EINVAL;
  467. }
  468. } else
  469. error = -EINVAL;
  470. if (!error)
  471. pr_debug("PM: suspend-to-disk mode set to '%s'\n",
  472. hibernation_modes[mode]);
  473. mutex_unlock(&pm_mutex);
  474. return error ? error : n;
  475. }
  476. power_attr(disk);
  477. static ssize_t resume_show(struct kset *kset, char *buf)
  478. {
  479. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  480. MINOR(swsusp_resume_device));
  481. }
  482. static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
  483. {
  484. unsigned int maj, min;
  485. dev_t res;
  486. int ret = -EINVAL;
  487. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  488. goto out;
  489. res = MKDEV(maj,min);
  490. if (maj != MAJOR(res) || min != MINOR(res))
  491. goto out;
  492. mutex_lock(&pm_mutex);
  493. swsusp_resume_device = res;
  494. mutex_unlock(&pm_mutex);
  495. printk("Attempting manual resume\n");
  496. noresume = 0;
  497. software_resume();
  498. ret = n;
  499. out:
  500. return ret;
  501. }
  502. power_attr(resume);
  503. static ssize_t image_size_show(struct kset *kset, char *buf)
  504. {
  505. return sprintf(buf, "%lu\n", image_size);
  506. }
  507. static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
  508. {
  509. unsigned long size;
  510. if (sscanf(buf, "%lu", &size) == 1) {
  511. image_size = size;
  512. return n;
  513. }
  514. return -EINVAL;
  515. }
  516. power_attr(image_size);
  517. static struct attribute * g[] = {
  518. &disk_attr.attr,
  519. &resume_attr.attr,
  520. &image_size_attr.attr,
  521. NULL,
  522. };
  523. static struct attribute_group attr_group = {
  524. .attrs = g,
  525. };
  526. static int __init pm_disk_init(void)
  527. {
  528. return sysfs_create_group(&power_subsys.kobj, &attr_group);
  529. }
  530. core_initcall(pm_disk_init);
  531. static int __init resume_setup(char *str)
  532. {
  533. if (noresume)
  534. return 1;
  535. strncpy( resume_file, str, 255 );
  536. return 1;
  537. }
  538. static int __init resume_offset_setup(char *str)
  539. {
  540. unsigned long long offset;
  541. if (noresume)
  542. return 1;
  543. if (sscanf(str, "%llu", &offset) == 1)
  544. swsusp_resume_block = offset;
  545. return 1;
  546. }
  547. static int __init noresume_setup(char *str)
  548. {
  549. noresume = 1;
  550. return 1;
  551. }
  552. __setup("noresume", noresume_setup);
  553. __setup("resume_offset=", resume_offset_setup);
  554. __setup("resume=", resume_setup);