disk.c 9.6 KB

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