suspend.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2010 Brian King IBM Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/suspend.h>
  20. #include <asm/firmware.h>
  21. #include <asm/hvcall.h>
  22. #include <asm/machdep.h>
  23. #include <asm/mmu.h>
  24. #include <asm/rtas.h>
  25. static u64 stream_id;
  26. static struct sys_device suspend_sysdev;
  27. static DECLARE_COMPLETION(suspend_work);
  28. static struct rtas_suspend_me_data suspend_data;
  29. static atomic_t suspending;
  30. /**
  31. * pseries_suspend_begin - First phase of hibernation
  32. *
  33. * Check to ensure we are in a valid state to hibernate
  34. *
  35. * Return value:
  36. * 0 on success / other on failure
  37. **/
  38. static int pseries_suspend_begin(suspend_state_t state)
  39. {
  40. long vasi_state, rc;
  41. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  42. /* Make sure the state is valid */
  43. rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
  44. vasi_state = retbuf[0];
  45. if (rc) {
  46. pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
  47. return rc;
  48. } else if (vasi_state == H_VASI_ENABLED) {
  49. return -EAGAIN;
  50. } else if (vasi_state != H_VASI_SUSPENDING) {
  51. pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
  52. vasi_state);
  53. return -EIO;
  54. }
  55. return 0;
  56. }
  57. /**
  58. * pseries_suspend_cpu - Suspend a single CPU
  59. *
  60. * Makes the H_JOIN call to suspend the CPU
  61. *
  62. **/
  63. static int pseries_suspend_cpu(void)
  64. {
  65. if (atomic_read(&suspending))
  66. return rtas_suspend_cpu(&suspend_data);
  67. return 0;
  68. }
  69. /**
  70. * pseries_suspend_enter - Final phase of hibernation
  71. *
  72. * Return value:
  73. * 0 on success / other on failure
  74. **/
  75. static int pseries_suspend_enter(suspend_state_t state)
  76. {
  77. int rc = rtas_suspend_last_cpu(&suspend_data);
  78. atomic_set(&suspending, 0);
  79. atomic_set(&suspend_data.done, 1);
  80. return rc;
  81. }
  82. /**
  83. * pseries_prepare_late - Prepare to suspend all other CPUs
  84. *
  85. * Return value:
  86. * 0 on success / other on failure
  87. **/
  88. static int pseries_prepare_late(void)
  89. {
  90. atomic_set(&suspending, 1);
  91. atomic_set(&suspend_data.working, 0);
  92. atomic_set(&suspend_data.done, 0);
  93. atomic_set(&suspend_data.error, 0);
  94. suspend_data.complete = &suspend_work;
  95. INIT_COMPLETION(suspend_work);
  96. return 0;
  97. }
  98. /**
  99. * store_hibernate - Initiate partition hibernation
  100. * @classdev: sysdev class struct
  101. * @attr: class device attribute struct
  102. * @buf: buffer
  103. * @count: buffer size
  104. *
  105. * Write the stream ID received from the HMC to this file
  106. * to trigger hibernating the partition
  107. *
  108. * Return value:
  109. * number of bytes printed to buffer / other on failure
  110. **/
  111. static ssize_t store_hibernate(struct sysdev_class *classdev,
  112. struct sysdev_class_attribute *attr,
  113. const char *buf, size_t count)
  114. {
  115. int rc;
  116. if (!capable(CAP_SYS_ADMIN))
  117. return -EPERM;
  118. stream_id = simple_strtoul(buf, NULL, 16);
  119. do {
  120. rc = pseries_suspend_begin(PM_SUSPEND_MEM);
  121. if (rc == -EAGAIN)
  122. ssleep(1);
  123. } while (rc == -EAGAIN);
  124. if (!rc)
  125. rc = pm_suspend(PM_SUSPEND_MEM);
  126. stream_id = 0;
  127. if (!rc)
  128. rc = count;
  129. return rc;
  130. }
  131. static SYSDEV_CLASS_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);
  132. static struct sysdev_class suspend_sysdev_class = {
  133. .name = "power",
  134. };
  135. static const struct platform_suspend_ops pseries_suspend_ops = {
  136. .valid = suspend_valid_only_mem,
  137. .begin = pseries_suspend_begin,
  138. .prepare_late = pseries_prepare_late,
  139. .enter = pseries_suspend_enter,
  140. };
  141. /**
  142. * pseries_suspend_sysfs_register - Register with sysfs
  143. *
  144. * Return value:
  145. * 0 on success / other on failure
  146. **/
  147. static int pseries_suspend_sysfs_register(struct sys_device *sysdev)
  148. {
  149. int rc;
  150. if ((rc = sysdev_class_register(&suspend_sysdev_class)))
  151. return rc;
  152. sysdev->id = 0;
  153. sysdev->cls = &suspend_sysdev_class;
  154. if ((rc = sysdev_class_create_file(&suspend_sysdev_class, &attr_hibernate)))
  155. goto class_unregister;
  156. return 0;
  157. class_unregister:
  158. sysdev_class_unregister(&suspend_sysdev_class);
  159. return rc;
  160. }
  161. /**
  162. * pseries_suspend_init - initcall for pSeries suspend
  163. *
  164. * Return value:
  165. * 0 on success / other on failure
  166. **/
  167. static int __init pseries_suspend_init(void)
  168. {
  169. int rc;
  170. if (!machine_is(pseries) || !firmware_has_feature(FW_FEATURE_LPAR))
  171. return 0;
  172. suspend_data.token = rtas_token("ibm,suspend-me");
  173. if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
  174. return 0;
  175. if ((rc = pseries_suspend_sysfs_register(&suspend_sysdev)))
  176. return rc;
  177. ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
  178. suspend_set_ops(&pseries_suspend_ops);
  179. return 0;
  180. }
  181. __initcall(pseries_suspend_init);