suspend.c 5.0 KB

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