disk.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 "power.h"
  19. extern suspend_disk_method_t pm_disk_mode;
  20. extern struct pm_ops * pm_ops;
  21. extern int swsusp_suspend(void);
  22. extern int swsusp_write(void);
  23. extern int swsusp_check(void);
  24. extern int swsusp_read(void);
  25. extern void swsusp_close(void);
  26. extern int swsusp_resume(void);
  27. extern int swsusp_free(void);
  28. static int noresume = 0;
  29. char resume_file[256] = CONFIG_PM_STD_PARTITION;
  30. dev_t swsusp_resume_device;
  31. /**
  32. * power_down - Shut machine down for hibernate.
  33. * @mode: Suspend-to-disk mode
  34. *
  35. * Use the platform driver, if configured so, and return gracefully if it
  36. * fails.
  37. * Otherwise, try to power off and reboot. If they fail, halt the machine,
  38. * there ain't no turning back.
  39. */
  40. static void power_down(suspend_disk_method_t mode)
  41. {
  42. unsigned long flags;
  43. int error = 0;
  44. local_irq_save(flags);
  45. switch(mode) {
  46. case PM_DISK_PLATFORM:
  47. device_shutdown();
  48. error = pm_ops->enter(PM_SUSPEND_DISK);
  49. break;
  50. case PM_DISK_SHUTDOWN:
  51. printk("Powering off system\n");
  52. device_shutdown();
  53. machine_power_off();
  54. break;
  55. case PM_DISK_REBOOT:
  56. device_shutdown();
  57. machine_restart(NULL);
  58. break;
  59. }
  60. machine_halt();
  61. /* Valid image is on the disk, if we continue we risk serious data corruption
  62. after resume. */
  63. printk(KERN_CRIT "Please power me down manually\n");
  64. while(1);
  65. }
  66. static int in_suspend __nosavedata = 0;
  67. /**
  68. * free_some_memory - Try to free as much memory as possible
  69. *
  70. * ... but do not OOM-kill anyone
  71. *
  72. * Notice: all userland should be stopped at this point, or
  73. * livelock is possible.
  74. */
  75. static void free_some_memory(void)
  76. {
  77. unsigned int i = 0;
  78. unsigned int tmp;
  79. unsigned long pages = 0;
  80. char *p = "-\\|/";
  81. printk("Freeing memory... ");
  82. while ((tmp = shrink_all_memory(10000))) {
  83. pages += tmp;
  84. printk("\b%c", p[i]);
  85. i++;
  86. if (i > 3)
  87. i = 0;
  88. }
  89. printk("\bdone (%li pages freed)\n", pages);
  90. }
  91. static inline void platform_finish(void)
  92. {
  93. if (pm_disk_mode == PM_DISK_PLATFORM) {
  94. if (pm_ops && pm_ops->finish)
  95. pm_ops->finish(PM_SUSPEND_DISK);
  96. }
  97. }
  98. static void finish(void)
  99. {
  100. device_resume();
  101. platform_finish();
  102. thaw_processes();
  103. enable_nonboot_cpus();
  104. pm_restore_console();
  105. }
  106. static int prepare_processes(void)
  107. {
  108. int error;
  109. pm_prepare_console();
  110. sys_sync();
  111. disable_nonboot_cpus();
  112. if (freeze_processes()) {
  113. error = -EBUSY;
  114. goto thaw;
  115. }
  116. if (pm_disk_mode == PM_DISK_PLATFORM) {
  117. if (pm_ops && pm_ops->prepare) {
  118. if ((error = pm_ops->prepare(PM_SUSPEND_DISK)))
  119. goto thaw;
  120. }
  121. }
  122. /* Free memory before shutting down devices. */
  123. free_some_memory();
  124. return 0;
  125. thaw:
  126. thaw_processes();
  127. enable_nonboot_cpus();
  128. pm_restore_console();
  129. return error;
  130. }
  131. static void unprepare_processes(void)
  132. {
  133. platform_finish();
  134. thaw_processes();
  135. enable_nonboot_cpus();
  136. pm_restore_console();
  137. }
  138. static int prepare_devices(void)
  139. {
  140. int error;
  141. if ((error = device_suspend(PMSG_FREEZE)))
  142. printk("Some devices failed to suspend\n");
  143. return error;
  144. }
  145. /**
  146. * pm_suspend_disk - The granpappy of power management.
  147. *
  148. * If we're going through the firmware, then get it over with quickly.
  149. *
  150. * If not, then call swsusp to do its thing, then figure out how
  151. * to power down the system.
  152. */
  153. int pm_suspend_disk(void)
  154. {
  155. int error;
  156. error = prepare_processes();
  157. if (error)
  158. return error;
  159. error = prepare_devices();
  160. if (error) {
  161. unprepare_processes();
  162. return error;
  163. }
  164. pr_debug("PM: Attempting to suspend to disk.\n");
  165. if (pm_disk_mode == PM_DISK_FIRMWARE)
  166. return pm_ops->enter(PM_SUSPEND_DISK);
  167. pr_debug("PM: snapshotting memory.\n");
  168. in_suspend = 1;
  169. if ((error = swsusp_suspend()))
  170. goto Done;
  171. if (in_suspend) {
  172. pr_debug("PM: writing image.\n");
  173. error = swsusp_write();
  174. if (!error)
  175. power_down(pm_disk_mode);
  176. } else
  177. pr_debug("PM: Image restored successfully.\n");
  178. swsusp_free();
  179. Done:
  180. finish();
  181. return error;
  182. }
  183. /**
  184. * software_resume - Resume from a saved image.
  185. *
  186. * Called as a late_initcall (so all devices are discovered and
  187. * initialized), we call swsusp to see if we have a saved image or not.
  188. * If so, we quiesce devices, the restore the saved image. We will
  189. * return above (in pm_suspend_disk() ) if everything goes well.
  190. * Otherwise, we fail gracefully and return to the normally
  191. * scheduled program.
  192. *
  193. */
  194. static int software_resume(void)
  195. {
  196. int error;
  197. if (!swsusp_resume_device) {
  198. if (!strlen(resume_file))
  199. return -ENOENT;
  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. return 0;
  212. }
  213. pr_debug("PM: Checking swsusp image.\n");
  214. if ((error = swsusp_check()))
  215. goto Done;
  216. pr_debug("PM: Preparing processes for restore.\n");
  217. if ((error = prepare_processes())) {
  218. swsusp_close();
  219. goto Done;
  220. }
  221. pr_debug("PM: Reading swsusp image.\n");
  222. if ((error = swsusp_read()))
  223. goto Cleanup;
  224. pr_debug("PM: Preparing devices for restore.\n");
  225. if ((error = prepare_devices()))
  226. goto Free;
  227. mb();
  228. pr_debug("PM: Restoring saved image.\n");
  229. swsusp_resume();
  230. pr_debug("PM: Restore failed, recovering.n");
  231. finish();
  232. Free:
  233. swsusp_free();
  234. Cleanup:
  235. unprepare_processes();
  236. Done:
  237. pr_debug("PM: Resume from disk failed.\n");
  238. return 0;
  239. }
  240. late_initcall(software_resume);
  241. static char * pm_disk_modes[] = {
  242. [PM_DISK_FIRMWARE] = "firmware",
  243. [PM_DISK_PLATFORM] = "platform",
  244. [PM_DISK_SHUTDOWN] = "shutdown",
  245. [PM_DISK_REBOOT] = "reboot",
  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. 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. pr_debug("PM: suspend-to-disk mode set to '%s'\n",
  306. pm_disk_modes[mode]);
  307. up(&pm_sem);
  308. return error ? error : n;
  309. }
  310. power_attr(disk);
  311. static ssize_t resume_show(struct subsystem * subsys, char *buf)
  312. {
  313. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  314. MINOR(swsusp_resume_device));
  315. }
  316. static ssize_t resume_store(struct subsystem * subsys, const char * buf, size_t n)
  317. {
  318. int len;
  319. char *p;
  320. unsigned int maj, min;
  321. int error = -EINVAL;
  322. dev_t res;
  323. p = memchr(buf, '\n', n);
  324. len = p ? p - buf : n;
  325. if (sscanf(buf, "%u:%u", &maj, &min) == 2) {
  326. res = MKDEV(maj,min);
  327. if (maj == MAJOR(res) && min == MINOR(res)) {
  328. swsusp_resume_device = res;
  329. printk("Attempting manual resume\n");
  330. noresume = 0;
  331. software_resume();
  332. }
  333. }
  334. return error >= 0 ? n : error;
  335. }
  336. power_attr(resume);
  337. static struct attribute * g[] = {
  338. &disk_attr.attr,
  339. &resume_attr.attr,
  340. NULL,
  341. };
  342. static struct attribute_group attr_group = {
  343. .attrs = g,
  344. };
  345. static int __init pm_disk_init(void)
  346. {
  347. return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
  348. }
  349. core_initcall(pm_disk_init);
  350. static int __init resume_setup(char *str)
  351. {
  352. if (noresume)
  353. return 1;
  354. strncpy( resume_file, str, 255 );
  355. return 1;
  356. }
  357. static int __init noresume_setup(char *str)
  358. {
  359. noresume = 1;
  360. return 1;
  361. }
  362. __setup("noresume", noresume_setup);
  363. __setup("resume=", resume_setup);