disk.c 11 KB

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