disk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 <linux/console.h>
  21. #include <linux/cpu.h>
  22. #include <linux/freezer.h>
  23. #include "power.h"
  24. static int noresume = 0;
  25. char resume_file[256] = CONFIG_PM_STD_PARTITION;
  26. dev_t swsusp_resume_device;
  27. sector_t swsusp_resume_block;
  28. /**
  29. * platform_prepare - prepare the machine for hibernation using the
  30. * platform driver if so configured and return an error code if it fails
  31. */
  32. static inline int platform_prepare(void)
  33. {
  34. int error = 0;
  35. switch (pm_disk_mode) {
  36. case PM_DISK_TEST:
  37. case PM_DISK_TESTPROC:
  38. case PM_DISK_SHUTDOWN:
  39. case PM_DISK_REBOOT:
  40. break;
  41. default:
  42. if (pm_ops && pm_ops->prepare)
  43. error = pm_ops->prepare(PM_SUSPEND_DISK);
  44. }
  45. return error;
  46. }
  47. /**
  48. * power_down - Shut machine down for hibernate.
  49. *
  50. * Use the platform driver, if configured so; otherwise try
  51. * to power off or reboot.
  52. */
  53. static void power_down(void)
  54. {
  55. switch (pm_disk_mode) {
  56. case PM_DISK_TEST:
  57. case PM_DISK_TESTPROC:
  58. break;
  59. case PM_DISK_SHUTDOWN:
  60. kernel_power_off();
  61. break;
  62. case PM_DISK_REBOOT:
  63. kernel_restart(NULL);
  64. break;
  65. default:
  66. if (pm_ops && pm_ops->enter) {
  67. kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
  68. pm_ops->enter(PM_SUSPEND_DISK);
  69. break;
  70. }
  71. }
  72. kernel_halt();
  73. /*
  74. * Valid image is on the disk, if we continue we risk serious data
  75. * corruption after resume.
  76. */
  77. printk(KERN_CRIT "Please power me down manually\n");
  78. while(1);
  79. }
  80. static inline void platform_finish(void)
  81. {
  82. switch (pm_disk_mode) {
  83. case PM_DISK_TEST:
  84. case PM_DISK_TESTPROC:
  85. case PM_DISK_SHUTDOWN:
  86. case PM_DISK_REBOOT:
  87. break;
  88. default:
  89. if (pm_ops && pm_ops->finish)
  90. pm_ops->finish(PM_SUSPEND_DISK);
  91. }
  92. }
  93. static void unprepare_processes(void)
  94. {
  95. thaw_processes();
  96. pm_restore_console();
  97. }
  98. static int prepare_processes(void)
  99. {
  100. int error = 0;
  101. pm_prepare_console();
  102. if (freeze_processes()) {
  103. error = -EBUSY;
  104. unprepare_processes();
  105. }
  106. return error;
  107. }
  108. /**
  109. * pm_suspend_disk - The granpappy of hibernation power management.
  110. *
  111. * If not, then call swsusp to do its thing, then figure out how
  112. * to power down the system.
  113. */
  114. int pm_suspend_disk(void)
  115. {
  116. int error;
  117. /* The snapshot device should not be opened while we're running */
  118. if (!atomic_add_unless(&snapshot_device_available, -1, 0))
  119. return -EBUSY;
  120. /* Allocate memory management structures */
  121. error = create_basic_memory_bitmaps();
  122. if (error)
  123. goto Exit;
  124. error = prepare_processes();
  125. if (error)
  126. goto Finish;
  127. if (pm_disk_mode == PM_DISK_TESTPROC) {
  128. printk("swsusp debug: Waiting for 5 seconds.\n");
  129. mdelay(5000);
  130. goto Thaw;
  131. }
  132. /* Free memory before shutting down devices. */
  133. error = swsusp_shrink_memory();
  134. if (error)
  135. goto Thaw;
  136. error = platform_prepare();
  137. if (error)
  138. goto Thaw;
  139. suspend_console();
  140. error = device_suspend(PMSG_FREEZE);
  141. if (error) {
  142. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  143. goto Resume_devices;
  144. }
  145. error = disable_nonboot_cpus();
  146. if (error)
  147. goto Enable_cpus;
  148. if (pm_disk_mode == PM_DISK_TEST) {
  149. printk("swsusp debug: Waiting for 5 seconds.\n");
  150. mdelay(5000);
  151. goto Enable_cpus;
  152. }
  153. pr_debug("PM: snapshotting memory.\n");
  154. in_suspend = 1;
  155. error = swsusp_suspend();
  156. if (error)
  157. goto Enable_cpus;
  158. if (in_suspend) {
  159. enable_nonboot_cpus();
  160. platform_finish();
  161. device_resume();
  162. resume_console();
  163. pr_debug("PM: writing image.\n");
  164. error = swsusp_write();
  165. if (!error)
  166. power_down();
  167. else {
  168. swsusp_free();
  169. goto Thaw;
  170. }
  171. } else {
  172. pr_debug("PM: Image restored successfully.\n");
  173. }
  174. swsusp_free();
  175. Enable_cpus:
  176. enable_nonboot_cpus();
  177. Resume_devices:
  178. platform_finish();
  179. device_resume();
  180. resume_console();
  181. Thaw:
  182. unprepare_processes();
  183. Finish:
  184. free_basic_memory_bitmaps();
  185. Exit:
  186. atomic_inc(&snapshot_device_available);
  187. return error;
  188. }
  189. /**
  190. * software_resume - Resume from a saved image.
  191. *
  192. * Called as a late_initcall (so all devices are discovered and
  193. * initialized), we call swsusp to see if we have a saved image or not.
  194. * If so, we quiesce devices, the restore the saved image. We will
  195. * return above (in pm_suspend_disk() ) if everything goes well.
  196. * Otherwise, we fail gracefully and return to the normally
  197. * scheduled program.
  198. *
  199. */
  200. static int software_resume(void)
  201. {
  202. int error;
  203. mutex_lock(&pm_mutex);
  204. if (!swsusp_resume_device) {
  205. if (!strlen(resume_file)) {
  206. mutex_unlock(&pm_mutex);
  207. return -ENOENT;
  208. }
  209. swsusp_resume_device = name_to_dev_t(resume_file);
  210. pr_debug("swsusp: Resume From Partition %s\n", resume_file);
  211. } else {
  212. pr_debug("swsusp: Resume From Partition %d:%d\n",
  213. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  214. }
  215. if (noresume) {
  216. /**
  217. * FIXME: If noresume is specified, we need to find the partition
  218. * and reset it back to normal swap space.
  219. */
  220. mutex_unlock(&pm_mutex);
  221. return 0;
  222. }
  223. pr_debug("PM: Checking swsusp image.\n");
  224. error = swsusp_check();
  225. if (error)
  226. goto Unlock;
  227. /* The snapshot device should not be opened while we're running */
  228. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  229. error = -EBUSY;
  230. goto Unlock;
  231. }
  232. error = create_basic_memory_bitmaps();
  233. if (error)
  234. goto Finish;
  235. pr_debug("PM: Preparing processes for restore.\n");
  236. error = prepare_processes();
  237. if (error) {
  238. swsusp_close();
  239. goto Done;
  240. }
  241. pr_debug("PM: Reading swsusp image.\n");
  242. error = swsusp_read();
  243. if (error) {
  244. swsusp_free();
  245. goto Thaw;
  246. }
  247. pr_debug("PM: Preparing devices for restore.\n");
  248. suspend_console();
  249. error = device_suspend(PMSG_PRETHAW);
  250. if (error)
  251. goto Free;
  252. error = disable_nonboot_cpus();
  253. if (!error)
  254. swsusp_resume();
  255. enable_nonboot_cpus();
  256. Free:
  257. swsusp_free();
  258. device_resume();
  259. resume_console();
  260. Thaw:
  261. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  262. unprepare_processes();
  263. Done:
  264. free_basic_memory_bitmaps();
  265. Finish:
  266. atomic_inc(&snapshot_device_available);
  267. /* For success case, the suspend path will release the lock */
  268. Unlock:
  269. mutex_unlock(&pm_mutex);
  270. pr_debug("PM: Resume from disk failed.\n");
  271. return 0;
  272. }
  273. late_initcall(software_resume);
  274. static const char * const pm_disk_modes[] = {
  275. [PM_DISK_PLATFORM] = "platform",
  276. [PM_DISK_SHUTDOWN] = "shutdown",
  277. [PM_DISK_REBOOT] = "reboot",
  278. [PM_DISK_TEST] = "test",
  279. [PM_DISK_TESTPROC] = "testproc",
  280. };
  281. /**
  282. * disk - Control suspend-to-disk mode
  283. *
  284. * Suspend-to-disk can be handled in several ways. We have a few options
  285. * for putting the system to sleep - using the platform driver (e.g. ACPI
  286. * or other pm_ops), powering off the system or rebooting the system
  287. * (for testing) as well as the two test modes.
  288. *
  289. * The system can support 'platform', and that is known a priori (and
  290. * encoded in pm_ops). However, the user may choose 'shutdown' or 'reboot'
  291. * as alternatives, as well as the test modes 'test' and 'testproc'.
  292. *
  293. * show() will display what the mode is currently set to.
  294. * store() will accept one of
  295. *
  296. * 'platform'
  297. * 'shutdown'
  298. * 'reboot'
  299. * 'test'
  300. * 'testproc'
  301. *
  302. * It will only change to 'platform' if the system
  303. * supports it (as determined from pm_ops->pm_disk_mode).
  304. */
  305. static ssize_t disk_show(struct kset *kset, char *buf)
  306. {
  307. int i;
  308. char *start = buf;
  309. for (i = PM_DISK_PLATFORM; i < PM_DISK_MAX; i++) {
  310. if (!pm_disk_modes[i])
  311. continue;
  312. switch (i) {
  313. case PM_DISK_SHUTDOWN:
  314. case PM_DISK_REBOOT:
  315. case PM_DISK_TEST:
  316. case PM_DISK_TESTPROC:
  317. break;
  318. default:
  319. if (pm_ops && pm_ops->enter &&
  320. (i == pm_ops->pm_disk_mode))
  321. break;
  322. /* not a valid mode, continue with loop */
  323. continue;
  324. }
  325. if (i == pm_disk_mode)
  326. buf += sprintf(buf, "[%s]", pm_disk_modes[i]);
  327. else
  328. buf += sprintf(buf, "%s", pm_disk_modes[i]);
  329. if (i+1 != PM_DISK_MAX)
  330. buf += sprintf(buf, " ");
  331. }
  332. buf += sprintf(buf, "\n");
  333. return buf-start;
  334. }
  335. static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
  336. {
  337. int error = 0;
  338. int i;
  339. int len;
  340. char *p;
  341. suspend_disk_method_t mode = 0;
  342. p = memchr(buf, '\n', n);
  343. len = p ? p - buf : n;
  344. mutex_lock(&pm_mutex);
  345. for (i = PM_DISK_PLATFORM; i < PM_DISK_MAX; i++) {
  346. if (!strncmp(buf, pm_disk_modes[i], len)) {
  347. mode = i;
  348. break;
  349. }
  350. }
  351. if (mode) {
  352. switch (mode) {
  353. case PM_DISK_SHUTDOWN:
  354. case PM_DISK_REBOOT:
  355. case PM_DISK_TEST:
  356. case PM_DISK_TESTPROC:
  357. pm_disk_mode = mode;
  358. break;
  359. default:
  360. if (pm_ops && pm_ops->enter &&
  361. (mode == pm_ops->pm_disk_mode))
  362. pm_disk_mode = mode;
  363. else
  364. error = -EINVAL;
  365. }
  366. } else {
  367. error = -EINVAL;
  368. }
  369. pr_debug("PM: suspend-to-disk mode set to '%s'\n",
  370. pm_disk_modes[mode]);
  371. mutex_unlock(&pm_mutex);
  372. return error ? error : n;
  373. }
  374. power_attr(disk);
  375. static ssize_t resume_show(struct kset *kset, char *buf)
  376. {
  377. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  378. MINOR(swsusp_resume_device));
  379. }
  380. static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
  381. {
  382. unsigned int maj, min;
  383. dev_t res;
  384. int ret = -EINVAL;
  385. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  386. goto out;
  387. res = MKDEV(maj,min);
  388. if (maj != MAJOR(res) || min != MINOR(res))
  389. goto out;
  390. mutex_lock(&pm_mutex);
  391. swsusp_resume_device = res;
  392. mutex_unlock(&pm_mutex);
  393. printk("Attempting manual resume\n");
  394. noresume = 0;
  395. software_resume();
  396. ret = n;
  397. out:
  398. return ret;
  399. }
  400. power_attr(resume);
  401. static ssize_t image_size_show(struct kset *kset, char *buf)
  402. {
  403. return sprintf(buf, "%lu\n", image_size);
  404. }
  405. static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
  406. {
  407. unsigned long size;
  408. if (sscanf(buf, "%lu", &size) == 1) {
  409. image_size = size;
  410. return n;
  411. }
  412. return -EINVAL;
  413. }
  414. power_attr(image_size);
  415. static struct attribute * g[] = {
  416. &disk_attr.attr,
  417. &resume_attr.attr,
  418. &image_size_attr.attr,
  419. NULL,
  420. };
  421. static struct attribute_group attr_group = {
  422. .attrs = g,
  423. };
  424. static int __init pm_disk_init(void)
  425. {
  426. return sysfs_create_group(&power_subsys.kobj, &attr_group);
  427. }
  428. core_initcall(pm_disk_init);
  429. static int __init resume_setup(char *str)
  430. {
  431. if (noresume)
  432. return 1;
  433. strncpy( resume_file, str, 255 );
  434. return 1;
  435. }
  436. static int __init resume_offset_setup(char *str)
  437. {
  438. unsigned long long offset;
  439. if (noresume)
  440. return 1;
  441. if (sscanf(str, "%llu", &offset) == 1)
  442. swsusp_resume_block = offset;
  443. return 1;
  444. }
  445. static int __init noresume_setup(char *str)
  446. {
  447. noresume = 1;
  448. return 1;
  449. }
  450. __setup("noresume", noresume_setup);
  451. __setup("resume_offset=", resume_offset_setup);
  452. __setup("resume=", resume_setup);