suspend.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 device suspend_dev;
  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. * @dev: subsys root device
  102. * @attr: 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 device *dev,
  113. struct device_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 DEVICE_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);
  133. static struct bus_type suspend_subsys = {
  134. .name = "power",
  135. .dev_name = "power",
  136. };
  137. static const struct platform_suspend_ops pseries_suspend_ops = {
  138. .valid = suspend_valid_only_mem,
  139. .begin = pseries_suspend_begin,
  140. .prepare_late = pseries_prepare_late,
  141. .enter = pseries_suspend_enter,
  142. };
  143. /**
  144. * pseries_suspend_sysfs_register - Register with sysfs
  145. *
  146. * Return value:
  147. * 0 on success / other on failure
  148. **/
  149. static int pseries_suspend_sysfs_register(struct device *dev)
  150. {
  151. int rc;
  152. if ((rc = subsys_system_register(&suspend_subsys, NULL)))
  153. return rc;
  154. dev->id = 0;
  155. dev->bus = &suspend_subsys;
  156. if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
  157. goto subsys_unregister;
  158. return 0;
  159. subsys_unregister:
  160. bus_unregister(&suspend_subsys);
  161. return rc;
  162. }
  163. /**
  164. * pseries_suspend_init - initcall for pSeries suspend
  165. *
  166. * Return value:
  167. * 0 on success / other on failure
  168. **/
  169. static int __init pseries_suspend_init(void)
  170. {
  171. int rc;
  172. if (!machine_is(pseries) || !firmware_has_feature(FW_FEATURE_LPAR))
  173. return 0;
  174. suspend_data.token = rtas_token("ibm,suspend-me");
  175. if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
  176. return 0;
  177. if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
  178. return rc;
  179. ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
  180. suspend_set_ops(&pseries_suspend_ops);
  181. return 0;
  182. }
  183. __initcall(pseries_suspend_init);