disk.c 8.8 KB

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