disk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. mutex_lock(&pm_mutex);
  245. /* The snapshot device should not be opened while we're running */
  246. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  247. error = -EBUSY;
  248. goto Unlock;
  249. }
  250. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  251. if (error)
  252. goto Exit;
  253. /* Allocate memory management structures */
  254. error = create_basic_memory_bitmaps();
  255. if (error)
  256. goto Exit;
  257. error = prepare_processes();
  258. if (error)
  259. goto Finish;
  260. if (hibernation_mode == HIBERNATION_TESTPROC) {
  261. printk("swsusp debug: Waiting for 5 seconds.\n");
  262. mdelay(5000);
  263. goto Thaw;
  264. }
  265. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  266. if (in_suspend && !error) {
  267. unsigned int flags = 0;
  268. if (hibernation_mode == HIBERNATION_PLATFORM)
  269. flags |= SF_PLATFORM_MODE;
  270. pr_debug("PM: writing image.\n");
  271. error = swsusp_write(flags);
  272. swsusp_free();
  273. if (!error)
  274. power_down();
  275. } else {
  276. pr_debug("PM: Image restored successfully.\n");
  277. swsusp_free();
  278. }
  279. Thaw:
  280. unprepare_processes();
  281. Finish:
  282. free_basic_memory_bitmaps();
  283. Exit:
  284. pm_notifier_call_chain(PM_POST_HIBERNATION);
  285. atomic_inc(&snapshot_device_available);
  286. Unlock:
  287. mutex_unlock(&pm_mutex);
  288. return error;
  289. }
  290. /**
  291. * software_resume - Resume from a saved image.
  292. *
  293. * Called as a late_initcall (so all devices are discovered and
  294. * initialized), we call swsusp to see if we have a saved image or not.
  295. * If so, we quiesce devices, the restore the saved image. We will
  296. * return above (in hibernate() ) if everything goes well.
  297. * Otherwise, we fail gracefully and return to the normally
  298. * scheduled program.
  299. *
  300. */
  301. static int software_resume(void)
  302. {
  303. int error;
  304. unsigned int flags;
  305. mutex_lock(&pm_mutex);
  306. if (!swsusp_resume_device) {
  307. if (!strlen(resume_file)) {
  308. mutex_unlock(&pm_mutex);
  309. return -ENOENT;
  310. }
  311. swsusp_resume_device = name_to_dev_t(resume_file);
  312. pr_debug("swsusp: Resume From Partition %s\n", resume_file);
  313. } else {
  314. pr_debug("swsusp: Resume From Partition %d:%d\n",
  315. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  316. }
  317. if (noresume) {
  318. /**
  319. * FIXME: If noresume is specified, we need to find the partition
  320. * and reset it back to normal swap space.
  321. */
  322. mutex_unlock(&pm_mutex);
  323. return 0;
  324. }
  325. pr_debug("PM: Checking swsusp image.\n");
  326. error = swsusp_check();
  327. if (error)
  328. goto Unlock;
  329. /* The snapshot device should not be opened while we're running */
  330. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  331. error = -EBUSY;
  332. goto Unlock;
  333. }
  334. error = create_basic_memory_bitmaps();
  335. if (error)
  336. goto Finish;
  337. pr_debug("PM: Preparing processes for restore.\n");
  338. error = prepare_processes();
  339. if (error) {
  340. swsusp_close();
  341. goto Done;
  342. }
  343. pr_debug("PM: Reading swsusp image.\n");
  344. error = swsusp_read(&flags);
  345. if (!error)
  346. hibernation_restore(flags & SF_PLATFORM_MODE);
  347. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  348. swsusp_free();
  349. unprepare_processes();
  350. Done:
  351. free_basic_memory_bitmaps();
  352. Finish:
  353. atomic_inc(&snapshot_device_available);
  354. /* For success case, the suspend path will release the lock */
  355. Unlock:
  356. mutex_unlock(&pm_mutex);
  357. pr_debug("PM: Resume from disk failed.\n");
  358. return error;
  359. }
  360. late_initcall(software_resume);
  361. static const char * const hibernation_modes[] = {
  362. [HIBERNATION_PLATFORM] = "platform",
  363. [HIBERNATION_SHUTDOWN] = "shutdown",
  364. [HIBERNATION_REBOOT] = "reboot",
  365. [HIBERNATION_TEST] = "test",
  366. [HIBERNATION_TESTPROC] = "testproc",
  367. };
  368. /**
  369. * disk - Control hibernation mode
  370. *
  371. * Suspend-to-disk can be handled in several ways. We have a few options
  372. * for putting the system to sleep - using the platform driver (e.g. ACPI
  373. * or other hibernation_ops), powering off the system or rebooting the
  374. * system (for testing) as well as the two test modes.
  375. *
  376. * The system can support 'platform', and that is known a priori (and
  377. * encoded by the presence of hibernation_ops). However, the user may
  378. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  379. * test modes, 'test' or 'testproc'.
  380. *
  381. * show() will display what the mode is currently set to.
  382. * store() will accept one of
  383. *
  384. * 'platform'
  385. * 'shutdown'
  386. * 'reboot'
  387. * 'test'
  388. * 'testproc'
  389. *
  390. * It will only change to 'platform' if the system
  391. * supports it (as determined by having hibernation_ops).
  392. */
  393. static ssize_t disk_show(struct kset *kset, char *buf)
  394. {
  395. int i;
  396. char *start = buf;
  397. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  398. if (!hibernation_modes[i])
  399. continue;
  400. switch (i) {
  401. case HIBERNATION_SHUTDOWN:
  402. case HIBERNATION_REBOOT:
  403. case HIBERNATION_TEST:
  404. case HIBERNATION_TESTPROC:
  405. break;
  406. case HIBERNATION_PLATFORM:
  407. if (hibernation_ops)
  408. break;
  409. /* not a valid mode, continue with loop */
  410. continue;
  411. }
  412. if (i == hibernation_mode)
  413. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  414. else
  415. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  416. }
  417. buf += sprintf(buf, "\n");
  418. return buf-start;
  419. }
  420. static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
  421. {
  422. int error = 0;
  423. int i;
  424. int len;
  425. char *p;
  426. int mode = HIBERNATION_INVALID;
  427. p = memchr(buf, '\n', n);
  428. len = p ? p - buf : n;
  429. mutex_lock(&pm_mutex);
  430. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  431. if (len == strlen(hibernation_modes[i])
  432. && !strncmp(buf, hibernation_modes[i], len)) {
  433. mode = i;
  434. break;
  435. }
  436. }
  437. if (mode != HIBERNATION_INVALID) {
  438. switch (mode) {
  439. case HIBERNATION_SHUTDOWN:
  440. case HIBERNATION_REBOOT:
  441. case HIBERNATION_TEST:
  442. case HIBERNATION_TESTPROC:
  443. hibernation_mode = mode;
  444. break;
  445. case HIBERNATION_PLATFORM:
  446. if (hibernation_ops)
  447. hibernation_mode = mode;
  448. else
  449. error = -EINVAL;
  450. }
  451. } else
  452. error = -EINVAL;
  453. if (!error)
  454. pr_debug("PM: suspend-to-disk mode set to '%s'\n",
  455. hibernation_modes[mode]);
  456. mutex_unlock(&pm_mutex);
  457. return error ? error : n;
  458. }
  459. power_attr(disk);
  460. static ssize_t resume_show(struct kset *kset, char *buf)
  461. {
  462. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  463. MINOR(swsusp_resume_device));
  464. }
  465. static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
  466. {
  467. unsigned int maj, min;
  468. dev_t res;
  469. int ret = -EINVAL;
  470. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  471. goto out;
  472. res = MKDEV(maj,min);
  473. if (maj != MAJOR(res) || min != MINOR(res))
  474. goto out;
  475. mutex_lock(&pm_mutex);
  476. swsusp_resume_device = res;
  477. mutex_unlock(&pm_mutex);
  478. printk("Attempting manual resume\n");
  479. noresume = 0;
  480. software_resume();
  481. ret = n;
  482. out:
  483. return ret;
  484. }
  485. power_attr(resume);
  486. static ssize_t image_size_show(struct kset *kset, char *buf)
  487. {
  488. return sprintf(buf, "%lu\n", image_size);
  489. }
  490. static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
  491. {
  492. unsigned long size;
  493. if (sscanf(buf, "%lu", &size) == 1) {
  494. image_size = size;
  495. return n;
  496. }
  497. return -EINVAL;
  498. }
  499. power_attr(image_size);
  500. static struct attribute * g[] = {
  501. &disk_attr.attr,
  502. &resume_attr.attr,
  503. &image_size_attr.attr,
  504. NULL,
  505. };
  506. static struct attribute_group attr_group = {
  507. .attrs = g,
  508. };
  509. static int __init pm_disk_init(void)
  510. {
  511. return sysfs_create_group(&power_subsys.kobj, &attr_group);
  512. }
  513. core_initcall(pm_disk_init);
  514. static int __init resume_setup(char *str)
  515. {
  516. if (noresume)
  517. return 1;
  518. strncpy( resume_file, str, 255 );
  519. return 1;
  520. }
  521. static int __init resume_offset_setup(char *str)
  522. {
  523. unsigned long long offset;
  524. if (noresume)
  525. return 1;
  526. if (sscanf(str, "%llu", &offset) == 1)
  527. swsusp_resume_block = offset;
  528. return 1;
  529. }
  530. static int __init noresume_setup(char *str)
  531. {
  532. noresume = 1;
  533. return 1;
  534. }
  535. __setup("noresume", noresume_setup);
  536. __setup("resume_offset=", resume_offset_setup);
  537. __setup("resume=", resume_setup);