disk.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. down(&pm_sem);
  196. if (!swsusp_resume_device) {
  197. if (!strlen(resume_file)) {
  198. up(&pm_sem);
  199. return -ENOENT;
  200. }
  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. up(&pm_sem);
  213. return 0;
  214. }
  215. pr_debug("PM: Checking swsusp image.\n");
  216. if ((error = swsusp_check()))
  217. goto Done;
  218. pr_debug("PM: Preparing processes for restore.\n");
  219. if ((error = prepare_processes())) {
  220. swsusp_close();
  221. goto Done;
  222. }
  223. pr_debug("PM: Reading swsusp image.\n");
  224. if ((error = swsusp_read()))
  225. goto Cleanup;
  226. pr_debug("PM: Preparing devices for restore.\n");
  227. if ((error = prepare_devices()))
  228. goto Free;
  229. mb();
  230. pr_debug("PM: Restoring saved image.\n");
  231. swsusp_resume();
  232. pr_debug("PM: Restore failed, recovering.n");
  233. finish();
  234. Free:
  235. swsusp_free();
  236. Cleanup:
  237. unprepare_processes();
  238. Done:
  239. /* For success case, the suspend path will release the lock */
  240. up(&pm_sem);
  241. pr_debug("PM: Resume from disk failed.\n");
  242. return 0;
  243. }
  244. late_initcall(software_resume);
  245. static char * pm_disk_modes[] = {
  246. [PM_DISK_FIRMWARE] = "firmware",
  247. [PM_DISK_PLATFORM] = "platform",
  248. [PM_DISK_SHUTDOWN] = "shutdown",
  249. [PM_DISK_REBOOT] = "reboot",
  250. };
  251. /**
  252. * disk - Control suspend-to-disk mode
  253. *
  254. * Suspend-to-disk can be handled in several ways. The greatest
  255. * distinction is who writes memory to disk - the firmware or the OS.
  256. * If the firmware does it, we assume that it also handles suspending
  257. * the system.
  258. * If the OS does it, then we have three options for putting the system
  259. * to sleep - using the platform driver (e.g. ACPI or other PM registers),
  260. * powering off the system or rebooting the system (for testing).
  261. *
  262. * The system will support either 'firmware' or 'platform', and that is
  263. * known a priori (and encoded in pm_ops). But, the user may choose
  264. * 'shutdown' or 'reboot' as alternatives.
  265. *
  266. * show() will display what the mode is currently set to.
  267. * store() will accept one of
  268. *
  269. * 'firmware'
  270. * 'platform'
  271. * 'shutdown'
  272. * 'reboot'
  273. *
  274. * It will only change to 'firmware' or 'platform' if the system
  275. * supports it (as determined from pm_ops->pm_disk_mode).
  276. */
  277. static ssize_t disk_show(struct subsystem * subsys, char * buf)
  278. {
  279. return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
  280. }
  281. static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
  282. {
  283. int error = 0;
  284. int i;
  285. int len;
  286. char *p;
  287. suspend_disk_method_t mode = 0;
  288. p = memchr(buf, '\n', n);
  289. len = p ? p - buf : n;
  290. down(&pm_sem);
  291. for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
  292. if (!strncmp(buf, pm_disk_modes[i], len)) {
  293. mode = i;
  294. break;
  295. }
  296. }
  297. if (mode) {
  298. if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT)
  299. pm_disk_mode = mode;
  300. else {
  301. if (pm_ops && pm_ops->enter &&
  302. (mode == pm_ops->pm_disk_mode))
  303. pm_disk_mode = mode;
  304. else
  305. error = -EINVAL;
  306. }
  307. } else
  308. error = -EINVAL;
  309. pr_debug("PM: suspend-to-disk mode set to '%s'\n",
  310. pm_disk_modes[mode]);
  311. up(&pm_sem);
  312. return error ? error : n;
  313. }
  314. power_attr(disk);
  315. static ssize_t resume_show(struct subsystem * subsys, char *buf)
  316. {
  317. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  318. MINOR(swsusp_resume_device));
  319. }
  320. static ssize_t resume_store(struct subsystem * subsys, const char * buf, size_t n)
  321. {
  322. int len;
  323. char *p;
  324. unsigned int maj, min;
  325. int error = -EINVAL;
  326. dev_t res;
  327. p = memchr(buf, '\n', n);
  328. len = p ? p - buf : n;
  329. if (sscanf(buf, "%u:%u", &maj, &min) == 2) {
  330. res = MKDEV(maj,min);
  331. if (maj == MAJOR(res) && min == MINOR(res)) {
  332. down(&pm_sem);
  333. swsusp_resume_device = res;
  334. up(&pm_sem);
  335. printk("Attempting manual resume\n");
  336. noresume = 0;
  337. software_resume();
  338. }
  339. }
  340. return error >= 0 ? n : error;
  341. }
  342. power_attr(resume);
  343. static struct attribute * g[] = {
  344. &disk_attr.attr,
  345. &resume_attr.attr,
  346. NULL,
  347. };
  348. static struct attribute_group attr_group = {
  349. .attrs = g,
  350. };
  351. static int __init pm_disk_init(void)
  352. {
  353. return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
  354. }
  355. core_initcall(pm_disk_init);
  356. static int __init resume_setup(char *str)
  357. {
  358. if (noresume)
  359. return 1;
  360. strncpy( resume_file, str, 255 );
  361. return 1;
  362. }
  363. static int __init noresume_setup(char *str)
  364. {
  365. noresume = 1;
  366. return 1;
  367. }
  368. __setup("noresume", noresume_setup);
  369. __setup("resume=", resume_setup);