disk.c 8.9 KB

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