disk.c 9.7 KB

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