disk.c 9.6 KB

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