disk.c 8.8 KB

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