Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / arch / powerpc / platforms / pseries / suspend.c
blob47226e04126d10e930a7a4a1bf42aa1c9abedb7a
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/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>
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;
35 /**
36 * pseries_suspend_begin - First phase of hibernation
38 * Check to ensure we are in a valid state to hibernate
40 * Return value:
41 * 0 on success / other on failure
42 **/
43 static int pseries_suspend_begin(suspend_state_t state)
45 long vasi_state, rc;
46 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
48 /* Make sure the state is valid */
49 rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
51 vasi_state = retbuf[0];
53 if (rc) {
54 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
55 return rc;
56 } else if (vasi_state == H_VASI_ENABLED) {
57 return -EAGAIN;
58 } else if (vasi_state != H_VASI_SUSPENDING) {
59 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
60 vasi_state);
61 return -EIO;
64 return 0;
67 /**
68 * pseries_suspend_cpu - Suspend a single CPU
70 * Makes the H_JOIN call to suspend the CPU
72 **/
73 static int pseries_suspend_cpu(void)
75 if (atomic_read(&suspending))
76 return rtas_suspend_cpu(&suspend_data);
77 return 0;
80 /**
81 * pseries_suspend_enter - Final phase of hibernation
83 * Return value:
84 * 0 on success / other on failure
85 **/
86 static int pseries_suspend_enter(suspend_state_t state)
88 int rc = rtas_suspend_last_cpu(&suspend_data);
90 atomic_set(&suspending, 0);
91 atomic_set(&suspend_data.done, 1);
92 return rc;
95 /**
96 * pseries_prepare_late - Prepare to suspend all other CPUs
98 * Return value:
99 * 0 on success / other on failure
101 static int pseries_prepare_late(void)
103 atomic_set(&suspending, 1);
104 atomic_set(&suspend_data.working, 0);
105 atomic_set(&suspend_data.done, 0);
106 atomic_set(&suspend_data.error, 0);
107 suspend_data.complete = &suspend_work;
108 INIT_COMPLETION(suspend_work);
109 return 0;
113 * store_hibernate - Initiate partition hibernation
114 * @dev: subsys root device
115 * @attr: device attribute struct
116 * @buf: buffer
117 * @count: buffer size
119 * Write the stream ID received from the HMC to this file
120 * to trigger hibernating the partition
122 * Return value:
123 * number of bytes printed to buffer / other on failure
125 static ssize_t store_hibernate(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t count)
129 int rc;
131 if (!capable(CAP_SYS_ADMIN))
132 return -EPERM;
134 stream_id = simple_strtoul(buf, NULL, 16);
136 do {
137 rc = pseries_suspend_begin(PM_SUSPEND_MEM);
138 if (rc == -EAGAIN)
139 ssleep(1);
140 } while (rc == -EAGAIN);
142 if (!rc) {
143 stop_topology_update();
144 rc = pm_suspend(PM_SUSPEND_MEM);
145 start_topology_update();
148 stream_id = 0;
150 if (!rc)
151 rc = count;
152 return rc;
155 static DEVICE_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);
157 static struct bus_type suspend_subsys = {
158 .name = "power",
159 .dev_name = "power",
162 static const struct platform_suspend_ops pseries_suspend_ops = {
163 .valid = suspend_valid_only_mem,
164 .begin = pseries_suspend_begin,
165 .prepare_late = pseries_prepare_late,
166 .enter = pseries_suspend_enter,
170 * pseries_suspend_sysfs_register - Register with sysfs
172 * Return value:
173 * 0 on success / other on failure
175 static int pseries_suspend_sysfs_register(struct device *dev)
177 int rc;
179 if ((rc = subsys_system_register(&suspend_subsys, NULL)))
180 return rc;
182 dev->id = 0;
183 dev->bus = &suspend_subsys;
185 if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
186 goto subsys_unregister;
188 return 0;
190 subsys_unregister:
191 bus_unregister(&suspend_subsys);
192 return rc;
196 * pseries_suspend_init - initcall for pSeries suspend
198 * Return value:
199 * 0 on success / other on failure
201 static int __init pseries_suspend_init(void)
203 int rc;
205 if (!machine_is(pseries) || !firmware_has_feature(FW_FEATURE_LPAR))
206 return 0;
208 suspend_data.token = rtas_token("ibm,suspend-me");
209 if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
210 return 0;
212 if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
213 return rc;
215 ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
216 suspend_set_ops(&pseries_suspend_ops);
217 return 0;
220 __initcall(pseries_suspend_init);