disk.c 12 KB

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