disk.c 8.9 KB

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