disk.c 8.9 KB

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