suspend.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_enable_irqs
  74. *
  75. * Post suspend configuration updates
  76. *
  77. **/
  78. static void pseries_suspend_enable_irqs(void)
  79. {
  80. /*
  81. * Update configuration which can be modified based on device tree
  82. * changes during resume.
  83. */
  84. post_mobility_fixup();
  85. }
  86. /**
  87. * pseries_suspend_enter - Final phase of hibernation
  88. *
  89. * Return value:
  90. * 0 on success / other on failure
  91. **/
  92. static int pseries_suspend_enter(suspend_state_t state)
  93. {
  94. int rc = rtas_suspend_last_cpu(&suspend_data);
  95. atomic_set(&suspending, 0);
  96. atomic_set(&suspend_data.done, 1);
  97. return rc;
  98. }
  99. /**
  100. * pseries_prepare_late - Prepare to suspend all other CPUs
  101. *
  102. * Return value:
  103. * 0 on success / other on failure
  104. **/
  105. static int pseries_prepare_late(void)
  106. {
  107. atomic_set(&suspending, 1);
  108. atomic_set(&suspend_data.working, 0);
  109. atomic_set(&suspend_data.done, 0);
  110. atomic_set(&suspend_data.error, 0);
  111. suspend_data.complete = &suspend_work;
  112. reinit_completion(&suspend_work);
  113. return 0;
  114. }
  115. /**
  116. * store_hibernate - Initiate partition hibernation
  117. * @dev: subsys root device
  118. * @attr: device attribute struct
  119. * @buf: buffer
  120. * @count: buffer size
  121. *
  122. * Write the stream ID received from the HMC to this file
  123. * to trigger hibernating the partition
  124. *
  125. * Return value:
  126. * number of bytes printed to buffer / other on failure
  127. **/
  128. static ssize_t store_hibernate(struct device *dev,
  129. struct device_attribute *attr,
  130. const char *buf, size_t count)
  131. {
  132. cpumask_var_t offline_mask;
  133. int rc;
  134. if (!capable(CAP_SYS_ADMIN))
  135. return -EPERM;
  136. if (!alloc_cpumask_var(&offline_mask, GFP_KERNEL))
  137. return -ENOMEM;
  138. stream_id = simple_strtoul(buf, NULL, 16);
  139. do {
  140. rc = pseries_suspend_begin(PM_SUSPEND_MEM);
  141. if (rc == -EAGAIN)
  142. ssleep(1);
  143. } while (rc == -EAGAIN);
  144. if (!rc) {
  145. /* All present CPUs must be online */
  146. cpumask_andnot(offline_mask, cpu_present_mask,
  147. cpu_online_mask);
  148. rc = rtas_online_cpus_mask(offline_mask);
  149. if (rc) {
  150. pr_err("%s: Could not bring present CPUs online.\n",
  151. __func__);
  152. goto out;
  153. }
  154. stop_topology_update();
  155. rc = pm_suspend(PM_SUSPEND_MEM);
  156. start_topology_update();
  157. /* Take down CPUs not online prior to suspend */
  158. if (!rtas_offline_cpus_mask(offline_mask))
  159. pr_warn("%s: Could not restore CPUs to offline "
  160. "state.\n", __func__);
  161. }
  162. stream_id = 0;
  163. if (!rc)
  164. rc = count;
  165. out:
  166. free_cpumask_var(offline_mask);
  167. return rc;
  168. }
  169. #define USER_DT_UPDATE 0
  170. #define KERN_DT_UPDATE 1
  171. /**
  172. * show_hibernate - Report device tree update responsibilty
  173. * @dev: subsys root device
  174. * @attr: device attribute struct
  175. * @buf: buffer
  176. *
  177. * Report whether a device tree update is performed by the kernel after a
  178. * resume, or if drmgr must coordinate the update from user space.
  179. *
  180. * Return value:
  181. * 0 if drmgr is to initiate update, and 1 otherwise
  182. **/
  183. static ssize_t show_hibernate(struct device *dev,
  184. struct device_attribute *attr,
  185. char *buf)
  186. {
  187. return sprintf(buf, "%d\n", KERN_DT_UPDATE);
  188. }
  189. static DEVICE_ATTR(hibernate, 0644, show_hibernate, store_hibernate);
  190. static struct bus_type suspend_subsys = {
  191. .name = "power",
  192. .dev_name = "power",
  193. };
  194. static const struct platform_suspend_ops pseries_suspend_ops = {
  195. .valid = suspend_valid_only_mem,
  196. .prepare_late = pseries_prepare_late,
  197. .enter = pseries_suspend_enter,
  198. };
  199. /**
  200. * pseries_suspend_sysfs_register - Register with sysfs
  201. *
  202. * Return value:
  203. * 0 on success / other on failure
  204. **/
  205. static int pseries_suspend_sysfs_register(struct device *dev)
  206. {
  207. int rc;
  208. if ((rc = subsys_system_register(&suspend_subsys, NULL)))
  209. return rc;
  210. dev->id = 0;
  211. dev->bus = &suspend_subsys;
  212. if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
  213. goto subsys_unregister;
  214. return 0;
  215. subsys_unregister:
  216. bus_unregister(&suspend_subsys);
  217. return rc;
  218. }
  219. /**
  220. * pseries_suspend_init - initcall for pSeries suspend
  221. *
  222. * Return value:
  223. * 0 on success / other on failure
  224. **/
  225. static int __init pseries_suspend_init(void)
  226. {
  227. int rc;
  228. if (!firmware_has_feature(FW_FEATURE_LPAR))
  229. return 0;
  230. suspend_data.token = rtas_token("ibm,suspend-me");
  231. if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
  232. return 0;
  233. if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
  234. return rc;
  235. ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
  236. ppc_md.suspend_enable_irqs = pseries_suspend_enable_irqs;
  237. suspend_set_ops(&pseries_suspend_ops);
  238. return 0;
  239. }
  240. machine_device_initcall(pseries, pseries_suspend_init);