disk.c 14 KB

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