disk.c 9.5 KB

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