disk.c 9.1 KB

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