Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / arch / powerpc / platforms / pseries / suspend.c
blob16a255255d3003c64ce8d517fe90bf8b9109552b
1 /*
2 * Copyright (C) 2010 Brian King IBM Corporation
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.
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.
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
19 #include <linux/cpu.h>
20 #include <linux/delay.h>
21 #include <linux/suspend.h>
22 #include <linux/stat.h>
23 #include <asm/firmware.h>
24 #include <asm/hvcall.h>
25 #include <asm/machdep.h>
26 #include <asm/mmu.h>
27 #include <asm/rtas.h>
28 #include <asm/topology.h>
30 static u64 stream_id;
31 static struct device suspend_dev;
32 static DECLARE_COMPLETION(suspend_work);
33 static struct rtas_suspend_me_data suspend_data;
34 static atomic_t suspending;
36 /**
37 * pseries_suspend_begin - First phase of hibernation
39 * Check to ensure we are in a valid state to hibernate
41 * Return value:
42 * 0 on success / other on failure
43 **/
44 static int pseries_suspend_begin(suspend_state_t state)
46 long vasi_state, rc;
47 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
49 /* Make sure the state is valid */
50 rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
52 vasi_state = retbuf[0];
54 if (rc) {
55 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
56 return rc;
57 } else if (vasi_state == H_VASI_ENABLED) {
58 return -EAGAIN;
59 } else if (vasi_state != H_VASI_SUSPENDING) {
60 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
61 vasi_state);
62 return -EIO;
65 return 0;
68 /**
69 * pseries_suspend_cpu - Suspend a single CPU
71 * Makes the H_JOIN call to suspend the CPU
73 **/
74 static int pseries_suspend_cpu(void)
76 if (atomic_read(&suspending))
77 return rtas_suspend_cpu(&suspend_data);
78 return 0;
81 /**
82 * pseries_suspend_enter - Final phase of hibernation
84 * Return value:
85 * 0 on success / other on failure
86 **/
87 static int pseries_suspend_enter(suspend_state_t state)
89 int rc = rtas_suspend_last_cpu(&suspend_data);
91 atomic_set(&suspending, 0);
92 atomic_set(&suspend_data.done, 1);
93 return rc;
96 /**
97 * pseries_prepare_late - Prepare to suspend all other CPUs
99 * Return value:
100 * 0 on success / other on failure
102 static int pseries_prepare_late(void)
104 atomic_set(&suspending, 1);
105 atomic_set(&suspend_data.working, 0);
106 atomic_set(&suspend_data.done, 0);
107 atomic_set(&suspend_data.error, 0);
108 suspend_data.complete = &suspend_work;
109 reinit_completion(&suspend_work);
110 return 0;
114 * store_hibernate - Initiate partition hibernation
115 * @dev: subsys root device
116 * @attr: device attribute struct
117 * @buf: buffer
118 * @count: buffer size
120 * Write the stream ID received from the HMC to this file
121 * to trigger hibernating the partition
123 * Return value:
124 * number of bytes printed to buffer / other on failure
126 static ssize_t store_hibernate(struct device *dev,
127 struct device_attribute *attr,
128 const char *buf, size_t count)
130 cpumask_var_t offline_mask;
131 int rc;
133 if (!capable(CAP_SYS_ADMIN))
134 return -EPERM;
136 if (!alloc_cpumask_var(&offline_mask, GFP_TEMPORARY))
137 return -ENOMEM;
139 stream_id = simple_strtoul(buf, NULL, 16);
141 do {
142 rc = pseries_suspend_begin(PM_SUSPEND_MEM);
143 if (rc == -EAGAIN)
144 ssleep(1);
145 } 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;
158 stop_topology_update();
159 rc = pm_suspend(PM_SUSPEND_MEM);
160 start_topology_update();
162 /* Take down CPUs not online prior to suspend */
163 if (!rtas_offline_cpus_mask(offline_mask))
164 pr_warn("%s: Could not restore CPUs to offline "
165 "state.\n", __func__);
168 stream_id = 0;
170 if (!rc)
171 rc = count;
172 out:
173 free_cpumask_var(offline_mask);
174 return rc;
177 static DEVICE_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);
179 static struct bus_type suspend_subsys = {
180 .name = "power",
181 .dev_name = "power",
184 static const struct platform_suspend_ops pseries_suspend_ops = {
185 .valid = suspend_valid_only_mem,
186 .begin = pseries_suspend_begin,
187 .prepare_late = pseries_prepare_late,
188 .enter = pseries_suspend_enter,
192 * pseries_suspend_sysfs_register - Register with sysfs
194 * Return value:
195 * 0 on success / other on failure
197 static int pseries_suspend_sysfs_register(struct device *dev)
199 int rc;
201 if ((rc = subsys_system_register(&suspend_subsys, NULL)))
202 return rc;
204 dev->id = 0;
205 dev->bus = &suspend_subsys;
207 if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
208 goto subsys_unregister;
210 return 0;
212 subsys_unregister:
213 bus_unregister(&suspend_subsys);
214 return rc;
218 * pseries_suspend_init - initcall for pSeries suspend
220 * Return value:
221 * 0 on success / other on failure
223 static int __init pseries_suspend_init(void)
225 int rc;
227 if (!machine_is(pseries) || !firmware_has_feature(FW_FEATURE_LPAR))
228 return 0;
230 suspend_data.token = rtas_token("ibm,suspend-me");
231 if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
232 return 0;
234 if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
235 return rc;
237 ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
238 suspend_set_ops(&pseries_suspend_ops);
239 return 0;
242 __initcall(pseries_suspend_init);