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>
27 #include <asm/topology.h>
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;
36 * pseries_suspend_begin - First phase of hibernation
38 * Check to ensure we are in a valid state to hibernate
41 * 0 on success / other on failure
43 static int pseries_suspend_begin(suspend_state_t state)
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];
54 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
56 } else if (vasi_state == H_VASI_ENABLED) {
58 } else if (vasi_state != H_VASI_SUSPENDING) {
59 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
68 * pseries_suspend_cpu - Suspend a single CPU
70 * Makes the H_JOIN call to suspend the CPU
73 static int pseries_suspend_cpu(void)
75 if (atomic_read(&suspending))
76 return rtas_suspend_cpu(&suspend_data);
81 * pseries_suspend_enter - Final phase of hibernation
84 * 0 on success / other on failure
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);
96 * pseries_prepare_late - Prepare to suspend all other CPUs
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);
113 * store_hibernate - Initiate partition hibernation
114 * @dev: subsys root device
115 * @attr: device attribute struct
117 * @count: buffer size
119 * Write the stream ID received from the HMC to this file
120 * to trigger hibernating the partition
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)
131 if (!capable(CAP_SYS_ADMIN))
134 stream_id = simple_strtoul(buf, NULL, 16);
137 rc = pseries_suspend_begin(PM_SUSPEND_MEM);
140 } while (rc == -EAGAIN);
143 stop_topology_update();
144 rc = pm_suspend(PM_SUSPEND_MEM);
145 start_topology_update();
155 static DEVICE_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);
157 static struct bus_type suspend_subsys = {
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
173 * 0 on success / other on failure
175 static int pseries_suspend_sysfs_register(struct device *dev)
179 if ((rc = subsys_system_register(&suspend_subsys, NULL)))
183 dev->bus = &suspend_subsys;
185 if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
186 goto subsys_unregister;
191 bus_unregister(&suspend_subsys);
196 * pseries_suspend_init - initcall for pSeries suspend
199 * 0 on success / other on failure
201 static int __init pseries_suspend_init(void)
205 if (!machine_is(pseries) || !firmware_has_feature(FW_FEATURE_LPAR))
208 suspend_data.token = rtas_token("ibm,suspend-me");
209 if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
212 if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
215 ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
216 suspend_set_ops(&pseries_suspend_ops);
220 __initcall(pseries_suspend_init);