disk.c 8.3 KB

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