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>
28 #include <asm/topology.h>
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;
37 * pseries_suspend_begin - First phase of hibernation
39 * Check to ensure we are in a valid state to hibernate
42 * 0 on success / other on failure
44 static int pseries_suspend_begin(suspend_state_t state)
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];
55 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
57 } else if (vasi_state == H_VASI_ENABLED) {
59 } else if (vasi_state != H_VASI_SUSPENDING) {
60 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
69 * pseries_suspend_cpu - Suspend a single CPU
71 * Makes the H_JOIN call to suspend the CPU
74 static int pseries_suspend_cpu(void)
76 if (atomic_read(&suspending))
77 return rtas_suspend_cpu(&suspend_data);
82 * pseries_suspend_enable_irqs
84 * Post suspend configuration updates
87 static void pseries_suspend_enable_irqs(void)
90 * Update configuration which can be modified based on device tree
91 * changes during resume.
93 post_mobility_fixup();
97 * pseries_suspend_enter - Final phase of hibernation
100 * 0 on success / other on failure
102 static int pseries_suspend_enter(suspend_state_t state)
104 int rc = rtas_suspend_last_cpu(&suspend_data);
106 atomic_set(&suspending, 0);
107 atomic_set(&suspend_data.done, 1);
112 * pseries_prepare_late - Prepare to suspend all other CPUs
115 * 0 on success / other on failure
117 static int pseries_prepare_late(void)
119 atomic_set(&suspending, 1);
120 atomic_set(&suspend_data.working, 0);
121 atomic_set(&suspend_data.done, 0);
122 atomic_set(&suspend_data.error, 0);
123 suspend_data.complete = &suspend_work;
124 reinit_completion(&suspend_work);
129 * store_hibernate - Initiate partition hibernation
130 * @dev: subsys root device
131 * @attr: device attribute struct
133 * @count: buffer size
135 * Write the stream ID received from the HMC to this file
136 * to trigger hibernating the partition
139 * number of bytes printed to buffer / other on failure
141 static ssize_t store_hibernate(struct device *dev,
142 struct device_attribute *attr,
143 const char *buf, size_t count)
145 cpumask_var_t offline_mask;
148 if (!capable(CAP_SYS_ADMIN))
151 if (!alloc_cpumask_var(&offline_mask, GFP_KERNEL))
154 stream_id = simple_strtoul(buf, NULL, 16);
157 rc = pseries_suspend_begin(PM_SUSPEND_MEM);
160 } while (rc == -EAGAIN);
163 /* All present CPUs must be online */
164 cpumask_andnot(offline_mask, cpu_present_mask,
166 rc = rtas_online_cpus_mask(offline_mask);
168 pr_err("%s: Could not bring present CPUs online.\n",
173 stop_topology_update();
174 rc = pm_suspend(PM_SUSPEND_MEM);
175 start_topology_update();
177 /* Take down CPUs not online prior to suspend */
178 if (!rtas_offline_cpus_mask(offline_mask))
179 pr_warn("%s: Could not restore CPUs to offline "
180 "state.\n", __func__);
188 free_cpumask_var(offline_mask);
192 #define USER_DT_UPDATE 0
193 #define KERN_DT_UPDATE 1
196 * show_hibernate - Report device tree update responsibilty
197 * @dev: subsys root device
198 * @attr: device attribute struct
201 * Report whether a device tree update is performed by the kernel after a
202 * resume, or if drmgr must coordinate the update from user space.
205 * 0 if drmgr is to initiate update, and 1 otherwise
207 static ssize_t show_hibernate(struct device *dev,
208 struct device_attribute *attr,
211 return sprintf(buf, "%d\n", KERN_DT_UPDATE);
214 static DEVICE_ATTR(hibernate, 0644, show_hibernate, store_hibernate);
216 static struct bus_type suspend_subsys = {
221 static const struct platform_suspend_ops pseries_suspend_ops = {
222 .valid = suspend_valid_only_mem,
223 .prepare_late = pseries_prepare_late,
224 .enter = pseries_suspend_enter,
228 * pseries_suspend_sysfs_register - Register with sysfs
231 * 0 on success / other on failure
233 static int pseries_suspend_sysfs_register(struct device *dev)
237 if ((rc = subsys_system_register(&suspend_subsys, NULL)))
241 dev->bus = &suspend_subsys;
243 if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
244 goto subsys_unregister;
249 bus_unregister(&suspend_subsys);
254 * pseries_suspend_init - initcall for pSeries suspend
257 * 0 on success / other on failure
259 static int __init pseries_suspend_init(void)
263 if (!firmware_has_feature(FW_FEATURE_LPAR))
266 suspend_data.token = rtas_token("ibm,suspend-me");
267 if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
270 if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
273 ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
274 ppc_md.suspend_enable_irqs = pseries_suspend_enable_irqs;
275 suspend_set_ops(&pseries_suspend_ops);
278 machine_device_initcall(pseries, pseries_suspend_init);