disk.c 8.6 KB

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