disk.c 8.2 KB

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