suspend.c 5.6 KB

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