suspend.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <linux/stat.h>
  21. #include <asm/firmware.h>
  22. #include <asm/hvcall.h>
  23. #include <asm/machdep.h>
  24. #include <asm/mmu.h>
  25. #include <asm/rtas.h>
  26. static u64 stream_id;
  27. static struct sys_device suspend_sysdev;
  28. static DECLARE_COMPLETION(suspend_work);
  29. static struct rtas_suspend_me_data suspend_data;
  30. static atomic_t suspending;
  31. /**
  32. * pseries_suspend_begin - First phase of hibernation
  33. *
  34. * Check to ensure we are in a valid state to hibernate
  35. *
  36. * Return value:
  37. * 0 on success / other on failure
  38. **/
  39. static int pseries_suspend_begin(suspend_state_t state)
  40. {
  41. long vasi_state, rc;
  42. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  43. /* Make sure the state is valid */
  44. rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
  45. vasi_state = retbuf[0];
  46. if (rc) {
  47. pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
  48. return rc;
  49. } else if (vasi_state == H_VASI_ENABLED) {
  50. return -EAGAIN;
  51. } else if (vasi_state != H_VASI_SUSPENDING) {
  52. pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
  53. vasi_state);
  54. return -EIO;
  55. }
  56. return 0;
  57. }
  58. /**
  59. * pseries_suspend_cpu - Suspend a single CPU
  60. *
  61. * Makes the H_JOIN call to suspend the CPU
  62. *
  63. **/
  64. static int pseries_suspend_cpu(void)
  65. {
  66. if (atomic_read(&suspending))
  67. return rtas_suspend_cpu(&suspend_data);
  68. return 0;
  69. }
  70. /**
  71. * pseries_suspend_enter - Final phase of hibernation
  72. *
  73. * Return value:
  74. * 0 on success / other on failure
  75. **/
  76. static int pseries_suspend_enter(suspend_state_t state)
  77. {
  78. int rc = rtas_suspend_last_cpu(&suspend_data);
  79. atomic_set(&suspending, 0);
  80. atomic_set(&suspend_data.done, 1);
  81. return rc;
  82. }
  83. /**
  84. * pseries_prepare_late - Prepare to suspend all other CPUs
  85. *
  86. * Return value:
  87. * 0 on success / other on failure
  88. **/
  89. static int pseries_prepare_late(void)
  90. {
  91. atomic_set(&suspending, 1);
  92. atomic_set(&suspend_data.working, 0);
  93. atomic_set(&suspend_data.done, 0);
  94. atomic_set(&suspend_data.error, 0);
  95. suspend_data.complete = &suspend_work;
  96. INIT_COMPLETION(suspend_work);
  97. return 0;
  98. }
  99. /**
  100. * store_hibernate - Initiate partition hibernation
  101. * @classdev: sysdev class struct
  102. * @attr: class device attribute struct
  103. * @buf: buffer
  104. * @count: buffer size
  105. *
  106. * Write the stream ID received from the HMC to this file
  107. * to trigger hibernating the partition
  108. *
  109. * Return value:
  110. * number of bytes printed to buffer / other on failure
  111. **/
  112. static ssize_t store_hibernate(struct sysdev_class *classdev,
  113. struct sysdev_class_attribute *attr,
  114. const char *buf, size_t count)
  115. {
  116. int rc;
  117. if (!capable(CAP_SYS_ADMIN))
  118. return -EPERM;
  119. stream_id = simple_strtoul(buf, NULL, 16);
  120. do {
  121. rc = pseries_suspend_begin(PM_SUSPEND_MEM);
  122. if (rc == -EAGAIN)
  123. ssleep(1);
  124. } while (rc == -EAGAIN);
  125. if (!rc)
  126. rc = pm_suspend(PM_SUSPEND_MEM);
  127. stream_id = 0;
  128. if (!rc)
  129. rc = count;
  130. return rc;
  131. }
  132. static SYSDEV_CLASS_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);
  133. static struct sysdev_class suspend_sysdev_class = {
  134. .name = "power",
  135. };
  136. static const struct platform_suspend_ops pseries_suspend_ops = {
  137. .valid = suspend_valid_only_mem,
  138. .begin = pseries_suspend_begin,
  139. .prepare_late = pseries_prepare_late,
  140. .enter = pseries_suspend_enter,
  141. };
  142. /**
  143. * pseries_suspend_sysfs_register - Register with sysfs
  144. *
  145. * Return value:
  146. * 0 on success / other on failure
  147. **/
  148. static int pseries_suspend_sysfs_register(struct sys_device *sysdev)
  149. {
  150. int rc;
  151. if ((rc = sysdev_class_register(&suspend_sysdev_class)))
  152. return rc;
  153. sysdev->id = 0;
  154. sysdev->cls = &suspend_sysdev_class;
  155. if ((rc = sysdev_class_create_file(&suspend_sysdev_class, &attr_hibernate)))
  156. goto class_unregister;
  157. return 0;
  158. class_unregister:
  159. sysdev_class_unregister(&suspend_sysdev_class);
  160. return rc;
  161. }
  162. /**
  163. * pseries_suspend_init - initcall for pSeries suspend
  164. *
  165. * Return value:
  166. * 0 on success / other on failure
  167. **/
  168. static int __init pseries_suspend_init(void)
  169. {
  170. int rc;
  171. if (!machine_is(pseries) || !firmware_has_feature(FW_FEATURE_LPAR))
  172. return 0;
  173. suspend_data.token = rtas_token("ibm,suspend-me");
  174. if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
  175. return 0;
  176. if ((rc = pseries_suspend_sysfs_register(&suspend_sysdev)))
  177. return rc;
  178. ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
  179. suspend_set_ops(&pseries_suspend_ops);
  180. return 0;
  181. }
  182. __initcall(pseries_suspend_init);