disk.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. /**
  29. * platform_prepare - prepare the machine for hibernation using the
  30. * platform driver if so configured and return an error code if it fails
  31. */
  32. static inline int platform_prepare(void)
  33. {
  34. int error = 0;
  35. switch (pm_disk_mode) {
  36. case PM_DISK_TEST:
  37. case PM_DISK_TESTPROC:
  38. case PM_DISK_SHUTDOWN:
  39. case PM_DISK_REBOOT:
  40. break;
  41. default:
  42. if (pm_ops && pm_ops->prepare)
  43. error = pm_ops->prepare(PM_SUSPEND_DISK);
  44. }
  45. return error;
  46. }
  47. /**
  48. * power_down - Shut machine down for hibernate.
  49. *
  50. * Use the platform driver, if configured so; otherwise try
  51. * to power off or reboot.
  52. */
  53. static void power_down(void)
  54. {
  55. switch (pm_disk_mode) {
  56. case PM_DISK_TEST:
  57. case PM_DISK_TESTPROC:
  58. break;
  59. case PM_DISK_SHUTDOWN:
  60. kernel_power_off();
  61. break;
  62. case PM_DISK_REBOOT:
  63. kernel_restart(NULL);
  64. break;
  65. default:
  66. if (pm_ops && pm_ops->enter) {
  67. kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
  68. pm_ops->enter(PM_SUSPEND_DISK);
  69. break;
  70. }
  71. }
  72. kernel_halt();
  73. /*
  74. * Valid image is on the disk, if we continue we risk serious data
  75. * corruption after resume.
  76. */
  77. printk(KERN_CRIT "Please power me down manually\n");
  78. while(1);
  79. }
  80. static inline void platform_finish(void)
  81. {
  82. switch (pm_disk_mode) {
  83. case PM_DISK_TEST:
  84. case PM_DISK_TESTPROC:
  85. case PM_DISK_SHUTDOWN:
  86. case PM_DISK_REBOOT:
  87. break;
  88. default:
  89. if (pm_ops && pm_ops->finish)
  90. pm_ops->finish(PM_SUSPEND_DISK);
  91. }
  92. }
  93. static void unprepare_processes(void)
  94. {
  95. thaw_processes();
  96. pm_restore_console();
  97. }
  98. static int prepare_processes(void)
  99. {
  100. int error = 0;
  101. pm_prepare_console();
  102. if (freeze_processes()) {
  103. error = -EBUSY;
  104. unprepare_processes();
  105. }
  106. return error;
  107. }
  108. /**
  109. * pm_suspend_disk - The granpappy of hibernation power management.
  110. *
  111. * If not, then call swsusp to do its thing, then figure out how
  112. * to power down the system.
  113. */
  114. int pm_suspend_disk(void)
  115. {
  116. int error;
  117. error = prepare_processes();
  118. if (error)
  119. return error;
  120. if (pm_disk_mode == PM_DISK_TESTPROC) {
  121. printk("swsusp debug: Waiting for 5 seconds.\n");
  122. mdelay(5000);
  123. goto Thaw;
  124. }
  125. /* Free memory before shutting down devices. */
  126. error = swsusp_shrink_memory();
  127. if (error)
  128. goto Thaw;
  129. error = platform_prepare();
  130. if (error)
  131. goto Thaw;
  132. suspend_console();
  133. error = device_suspend(PMSG_FREEZE);
  134. if (error) {
  135. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  136. goto Resume_devices;
  137. }
  138. error = disable_nonboot_cpus();
  139. if (error)
  140. goto Enable_cpus;
  141. if (pm_disk_mode == PM_DISK_TEST) {
  142. printk("swsusp debug: Waiting for 5 seconds.\n");
  143. mdelay(5000);
  144. goto Enable_cpus;
  145. }
  146. pr_debug("PM: snapshotting memory.\n");
  147. in_suspend = 1;
  148. error = swsusp_suspend();
  149. if (error)
  150. goto Enable_cpus;
  151. if (in_suspend) {
  152. enable_nonboot_cpus();
  153. platform_finish();
  154. device_resume();
  155. resume_console();
  156. pr_debug("PM: writing image.\n");
  157. error = swsusp_write();
  158. if (!error)
  159. power_down();
  160. else {
  161. swsusp_free();
  162. goto Thaw;
  163. }
  164. } else {
  165. pr_debug("PM: Image restored successfully.\n");
  166. }
  167. swsusp_free();
  168. Enable_cpus:
  169. enable_nonboot_cpus();
  170. Resume_devices:
  171. platform_finish();
  172. device_resume();
  173. resume_console();
  174. Thaw:
  175. unprepare_processes();
  176. return error;
  177. }
  178. /**
  179. * software_resume - Resume from a saved image.
  180. *
  181. * Called as a late_initcall (so all devices are discovered and
  182. * initialized), we call swsusp to see if we have a saved image or not.
  183. * If so, we quiesce devices, the restore the saved image. We will
  184. * return above (in pm_suspend_disk() ) if everything goes well.
  185. * Otherwise, we fail gracefully and return to the normally
  186. * scheduled program.
  187. *
  188. */
  189. static int software_resume(void)
  190. {
  191. int error;
  192. mutex_lock(&pm_mutex);
  193. if (!swsusp_resume_device) {
  194. if (!strlen(resume_file)) {
  195. mutex_unlock(&pm_mutex);
  196. return -ENOENT;
  197. }
  198. swsusp_resume_device = name_to_dev_t(resume_file);
  199. pr_debug("swsusp: Resume From Partition %s\n", resume_file);
  200. } else {
  201. pr_debug("swsusp: Resume From Partition %d:%d\n",
  202. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  203. }
  204. if (noresume) {
  205. /**
  206. * FIXME: If noresume is specified, we need to find the partition
  207. * and reset it back to normal swap space.
  208. */
  209. mutex_unlock(&pm_mutex);
  210. return 0;
  211. }
  212. pr_debug("PM: Checking swsusp image.\n");
  213. error = swsusp_check();
  214. if (error)
  215. goto Done;
  216. pr_debug("PM: Preparing processes for restore.\n");
  217. error = prepare_processes();
  218. if (error) {
  219. swsusp_close();
  220. goto Done;
  221. }
  222. pr_debug("PM: Reading swsusp image.\n");
  223. error = swsusp_read();
  224. if (error) {
  225. swsusp_free();
  226. goto Thaw;
  227. }
  228. pr_debug("PM: Preparing devices for restore.\n");
  229. suspend_console();
  230. error = device_suspend(PMSG_PRETHAW);
  231. if (error)
  232. goto Free;
  233. error = disable_nonboot_cpus();
  234. if (!error)
  235. swsusp_resume();
  236. enable_nonboot_cpus();
  237. Free:
  238. swsusp_free();
  239. device_resume();
  240. resume_console();
  241. Thaw:
  242. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  243. unprepare_processes();
  244. Done:
  245. /* For success case, the suspend path will release the lock */
  246. mutex_unlock(&pm_mutex);
  247. pr_debug("PM: Resume from disk failed.\n");
  248. return 0;
  249. }
  250. late_initcall(software_resume);
  251. static const char * const pm_disk_modes[] = {
  252. [PM_DISK_PLATFORM] = "platform",
  253. [PM_DISK_SHUTDOWN] = "shutdown",
  254. [PM_DISK_REBOOT] = "reboot",
  255. [PM_DISK_TEST] = "test",
  256. [PM_DISK_TESTPROC] = "testproc",
  257. };
  258. /**
  259. * disk - Control suspend-to-disk mode
  260. *
  261. * Suspend-to-disk can be handled in several ways. We have a few options
  262. * for putting the system to sleep - using the platform driver (e.g. ACPI
  263. * or other pm_ops), powering off the system or rebooting the system
  264. * (for testing) as well as the two test modes.
  265. *
  266. * The system can support 'platform', and that is known a priori (and
  267. * encoded in pm_ops). However, the user may choose 'shutdown' or 'reboot'
  268. * as alternatives, as well as the test modes 'test' and 'testproc'.
  269. *
  270. * show() will display what the mode is currently set to.
  271. * store() will accept one of
  272. *
  273. * 'platform'
  274. * 'shutdown'
  275. * 'reboot'
  276. * 'test'
  277. * 'testproc'
  278. *
  279. * It will only change to 'platform' if the system
  280. * supports it (as determined from pm_ops->pm_disk_mode).
  281. */
  282. static ssize_t disk_show(struct kset *kset, char *buf)
  283. {
  284. return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
  285. }
  286. static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
  287. {
  288. int error = 0;
  289. int i;
  290. int len;
  291. char *p;
  292. suspend_disk_method_t mode = 0;
  293. p = memchr(buf, '\n', n);
  294. len = p ? p - buf : n;
  295. mutex_lock(&pm_mutex);
  296. for (i = PM_DISK_PLATFORM; i < PM_DISK_MAX; i++) {
  297. if (!strncmp(buf, pm_disk_modes[i], len)) {
  298. mode = i;
  299. break;
  300. }
  301. }
  302. if (mode) {
  303. switch (mode) {
  304. case PM_DISK_SHUTDOWN:
  305. case PM_DISK_REBOOT:
  306. case PM_DISK_TEST:
  307. case PM_DISK_TESTPROC:
  308. pm_disk_mode = mode;
  309. break;
  310. default:
  311. if (pm_ops && pm_ops->enter &&
  312. (mode == pm_ops->pm_disk_mode))
  313. pm_disk_mode = mode;
  314. else
  315. error = -EINVAL;
  316. }
  317. } else {
  318. error = -EINVAL;
  319. }
  320. pr_debug("PM: suspend-to-disk mode set to '%s'\n",
  321. pm_disk_modes[mode]);
  322. mutex_unlock(&pm_mutex);
  323. return error ? error : n;
  324. }
  325. power_attr(disk);
  326. static ssize_t resume_show(struct kset *kset, char *buf)
  327. {
  328. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  329. MINOR(swsusp_resume_device));
  330. }
  331. static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
  332. {
  333. unsigned int maj, min;
  334. dev_t res;
  335. int ret = -EINVAL;
  336. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  337. goto out;
  338. res = MKDEV(maj,min);
  339. if (maj != MAJOR(res) || min != MINOR(res))
  340. goto out;
  341. mutex_lock(&pm_mutex);
  342. swsusp_resume_device = res;
  343. mutex_unlock(&pm_mutex);
  344. printk("Attempting manual resume\n");
  345. noresume = 0;
  346. software_resume();
  347. ret = n;
  348. out:
  349. return ret;
  350. }
  351. power_attr(resume);
  352. static ssize_t image_size_show(struct kset *kset, char *buf)
  353. {
  354. return sprintf(buf, "%lu\n", image_size);
  355. }
  356. static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
  357. {
  358. unsigned long size;
  359. if (sscanf(buf, "%lu", &size) == 1) {
  360. image_size = size;
  361. return n;
  362. }
  363. return -EINVAL;
  364. }
  365. power_attr(image_size);
  366. static struct attribute * g[] = {
  367. &disk_attr.attr,
  368. &resume_attr.attr,
  369. &image_size_attr.attr,
  370. NULL,
  371. };
  372. static struct attribute_group attr_group = {
  373. .attrs = g,
  374. };
  375. static int __init pm_disk_init(void)
  376. {
  377. return sysfs_create_group(&power_subsys.kobj, &attr_group);
  378. }
  379. core_initcall(pm_disk_init);
  380. static int __init resume_setup(char *str)
  381. {
  382. if (noresume)
  383. return 1;
  384. strncpy( resume_file, str, 255 );
  385. return 1;
  386. }
  387. static int __init resume_offset_setup(char *str)
  388. {
  389. unsigned long long offset;
  390. if (noresume)
  391. return 1;
  392. if (sscanf(str, "%llu", &offset) == 1)
  393. swsusp_resume_block = offset;
  394. return 1;
  395. }
  396. static int __init noresume_setup(char *str)
  397. {
  398. noresume = 1;
  399. return 1;
  400. }
  401. __setup("noresume", noresume_setup);
  402. __setup("resume_offset=", resume_offset_setup);
  403. __setup("resume=", resume_setup);