disk.c 8.5 KB

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