disk.c 8.5 KB

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