disk.c 8.9 KB

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