disk.c 8.7 KB

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