suspend.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #include "../../kernel/cacheinfo.h"
  29. static u64 stream_id;
  30. static struct device suspend_dev;
  31. static DECLARE_COMPLETION(suspend_work);
  32. static struct rtas_suspend_me_data suspend_data;
  33. static atomic_t suspending;
  34. /**
  35. * pseries_suspend_begin - First phase of hibernation
  36. *
  37. * Check to ensure we are in a valid state to hibernate
  38. *
  39. * Return value:
  40. * 0 on success / other on failure
  41. **/
  42. static int pseries_suspend_begin(suspend_state_t state)
  43. {
  44. long vasi_state, rc;
  45. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  46. /* Make sure the state is valid */
  47. rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
  48. vasi_state = retbuf[0];
  49. if (rc) {
  50. pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
  51. return rc;
  52. } else if (vasi_state == H_VASI_ENABLED) {
  53. return -EAGAIN;
  54. } else if (vasi_state != H_VASI_SUSPENDING) {
  55. pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
  56. vasi_state);
  57. return -EIO;
  58. }
  59. return 0;
  60. }
  61. /**
  62. * pseries_suspend_cpu - Suspend a single CPU
  63. *
  64. * Makes the H_JOIN call to suspend the CPU
  65. *
  66. **/
  67. static int pseries_suspend_cpu(void)
  68. {
  69. if (atomic_read(&suspending))
  70. return rtas_suspend_cpu(&suspend_data);
  71. return 0;
  72. }
  73. /**
  74. * pseries_suspend_enable_irqs
  75. *
  76. * Post suspend configuration updates
  77. *
  78. **/
  79. static void pseries_suspend_enable_irqs(void)
  80. {
  81. /*
  82. * Update configuration which can be modified based on device tree
  83. * changes during resume.
  84. */
  85. cacheinfo_cpu_offline(smp_processor_id());
  86. post_mobility_fixup();
  87. cacheinfo_cpu_online(smp_processor_id());
  88. }
  89. /**
  90. * pseries_suspend_enter - Final phase of hibernation
  91. *
  92. * Return value:
  93. * 0 on success / other on failure
  94. **/
  95. static int pseries_suspend_enter(suspend_state_t state)
  96. {
  97. int rc = rtas_suspend_last_cpu(&suspend_data);
  98. atomic_set(&suspending, 0);
  99. atomic_set(&suspend_data.done, 1);
  100. return rc;
  101. }
  102. /**
  103. * pseries_prepare_late - Prepare to suspend all other CPUs
  104. *
  105. * Return value:
  106. * 0 on success / other on failure
  107. **/
  108. static int pseries_prepare_late(void)
  109. {
  110. atomic_set(&suspending, 1);
  111. atomic_set(&suspend_data.working, 0);
  112. atomic_set(&suspend_data.done, 0);
  113. atomic_set(&suspend_data.error, 0);
  114. suspend_data.complete = &suspend_work;
  115. reinit_completion(&suspend_work);
  116. return 0;
  117. }
  118. /**
  119. * store_hibernate - Initiate partition hibernation
  120. * @dev: subsys root device
  121. * @attr: device attribute struct
  122. * @buf: buffer
  123. * @count: buffer size
  124. *
  125. * Write the stream ID received from the HMC to this file
  126. * to trigger hibernating the partition
  127. *
  128. * Return value:
  129. * number of bytes printed to buffer / other on failure
  130. **/
  131. static ssize_t store_hibernate(struct device *dev,
  132. struct device_attribute *attr,
  133. const char *buf, size_t count)
  134. {
  135. cpumask_var_t offline_mask;
  136. int rc;
  137. if (!capable(CAP_SYS_ADMIN))
  138. return -EPERM;
  139. if (!alloc_cpumask_var(&offline_mask, GFP_TEMPORARY))
  140. return -ENOMEM;
  141. stream_id = simple_strtoul(buf, NULL, 16);
  142. do {
  143. rc = pseries_suspend_begin(PM_SUSPEND_MEM);
  144. if (rc == -EAGAIN)
  145. ssleep(1);
  146. } while (rc == -EAGAIN);
  147. if (!rc) {
  148. /* All present CPUs must be online */
  149. cpumask_andnot(offline_mask, cpu_present_mask,
  150. cpu_online_mask);
  151. rc = rtas_online_cpus_mask(offline_mask);
  152. if (rc) {
  153. pr_err("%s: Could not bring present CPUs online.\n",
  154. __func__);
  155. goto out;
  156. }
  157. stop_topology_update();
  158. rc = pm_suspend(PM_SUSPEND_MEM);
  159. start_topology_update();
  160. /* Take down CPUs not online prior to suspend */
  161. if (!rtas_offline_cpus_mask(offline_mask))
  162. pr_warn("%s: Could not restore CPUs to offline "
  163. "state.\n", __func__);
  164. }
  165. stream_id = 0;
  166. if (!rc)
  167. rc = count;
  168. out:
  169. free_cpumask_var(offline_mask);
  170. return rc;
  171. }
  172. #define USER_DT_UPDATE 0
  173. #define KERN_DT_UPDATE 1
  174. /**
  175. * show_hibernate - Report device tree update responsibilty
  176. * @dev: subsys root device
  177. * @attr: device attribute struct
  178. * @buf: buffer
  179. *
  180. * Report whether a device tree update is performed by the kernel after a
  181. * resume, or if drmgr must coordinate the update from user space.
  182. *
  183. * Return value:
  184. * 0 if drmgr is to initiate update, and 1 otherwise
  185. **/
  186. static ssize_t show_hibernate(struct device *dev,
  187. struct device_attribute *attr,
  188. char *buf)
  189. {
  190. return sprintf(buf, "%d\n", KERN_DT_UPDATE);
  191. }
  192. static DEVICE_ATTR(hibernate, S_IWUSR | S_IRUGO,
  193. show_hibernate, store_hibernate);
  194. static struct bus_type suspend_subsys = {
  195. .name = "power",
  196. .dev_name = "power",
  197. };
  198. static const struct platform_suspend_ops pseries_suspend_ops = {
  199. .valid = suspend_valid_only_mem,
  200. .begin = pseries_suspend_begin,
  201. .prepare_late = pseries_prepare_late,
  202. .enter = pseries_suspend_enter,
  203. };
  204. /**
  205. * pseries_suspend_sysfs_register - Register with sysfs
  206. *
  207. * Return value:
  208. * 0 on success / other on failure
  209. **/
  210. static int pseries_suspend_sysfs_register(struct device *dev)
  211. {
  212. int rc;
  213. if ((rc = subsys_system_register(&suspend_subsys, NULL)))
  214. return rc;
  215. dev->id = 0;
  216. dev->bus = &suspend_subsys;
  217. if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
  218. goto subsys_unregister;
  219. return 0;
  220. subsys_unregister:
  221. bus_unregister(&suspend_subsys);
  222. return rc;
  223. }
  224. /**
  225. * pseries_suspend_init - initcall for pSeries suspend
  226. *
  227. * Return value:
  228. * 0 on success / other on failure
  229. **/
  230. static int __init pseries_suspend_init(void)
  231. {
  232. int rc;
  233. if (!firmware_has_feature(FW_FEATURE_LPAR))
  234. return 0;
  235. suspend_data.token = rtas_token("ibm,suspend-me");
  236. if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
  237. return 0;
  238. if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
  239. return rc;
  240. ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
  241. ppc_md.suspend_enable_irqs = pseries_suspend_enable_irqs;
  242. suspend_set_ops(&pseries_suspend_ops);
  243. return 0;
  244. }
  245. machine_device_initcall(pseries, pseries_suspend_init);