disk.c 14 KB

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