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