1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2010 Brian King IBM Corporation
7 #include <linux/delay.h>
8 #include <linux/suspend.h>
9 #include <linux/stat.h>
10 #include <asm/firmware.h>
11 #include <asm/hvcall.h>
12 #include <asm/machdep.h>
15 #include <asm/topology.h>
18 static struct device suspend_dev;
19 static DECLARE_COMPLETION(suspend_work);
20 static struct rtas_suspend_me_data suspend_data;
21 static atomic_t suspending;
24 * pseries_suspend_begin - First phase of hibernation
26 * Check to ensure we are in a valid state to hibernate
29 * 0 on success / other on failure
31 static int pseries_suspend_begin(suspend_state_t state)
34 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
36 /* Make sure the state is valid */
37 rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
39 vasi_state = retbuf[0];
42 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
44 } else if (vasi_state == H_VASI_ENABLED) {
46 } else if (vasi_state != H_VASI_SUSPENDING) {
47 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
56 * pseries_suspend_cpu - Suspend a single CPU
58 * Makes the H_JOIN call to suspend the CPU
61 static int pseries_suspend_cpu(void)
63 if (atomic_read(&suspending))
64 return rtas_suspend_cpu(&suspend_data);
69 * pseries_suspend_enable_irqs
71 * Post suspend configuration updates
74 static void pseries_suspend_enable_irqs(void)
77 * Update configuration which can be modified based on device tree
78 * changes during resume.
80 post_mobility_fixup();
84 * pseries_suspend_enter - Final phase of hibernation
87 * 0 on success / other on failure
89 static int pseries_suspend_enter(suspend_state_t state)
91 int rc = rtas_suspend_last_cpu(&suspend_data);
93 atomic_set(&suspending, 0);
94 atomic_set(&suspend_data.done, 1);
99 * pseries_prepare_late - Prepare to suspend all other CPUs
102 * 0 on success / other on failure
104 static int pseries_prepare_late(void)
106 atomic_set(&suspending, 1);
107 atomic_set(&suspend_data.working, 0);
108 atomic_set(&suspend_data.done, 0);
109 atomic_set(&suspend_data.error, 0);
110 suspend_data.complete = &suspend_work;
111 reinit_completion(&suspend_work);
116 * store_hibernate - Initiate partition hibernation
117 * @dev: subsys root device
118 * @attr: device attribute struct
120 * @count: buffer size
122 * Write the stream ID received from the HMC to this file
123 * to trigger hibernating the partition
126 * number of bytes printed to buffer / other on failure
128 static ssize_t store_hibernate(struct device *dev,
129 struct device_attribute *attr,
130 const char *buf, size_t count)
134 if (!capable(CAP_SYS_ADMIN))
137 stream_id = simple_strtoul(buf, NULL, 16);
140 rc = pseries_suspend_begin(PM_SUSPEND_MEM);
143 } while (rc == -EAGAIN);
146 rc = pm_suspend(PM_SUSPEND_MEM);
156 #define USER_DT_UPDATE 0
157 #define KERN_DT_UPDATE 1
160 * show_hibernate - Report device tree update responsibilty
161 * @dev: subsys root device
162 * @attr: device attribute struct
165 * Report whether a device tree update is performed by the kernel after a
166 * resume, or if drmgr must coordinate the update from user space.
169 * 0 if drmgr is to initiate update, and 1 otherwise
171 static ssize_t show_hibernate(struct device *dev,
172 struct device_attribute *attr,
175 return sprintf(buf, "%d\n", KERN_DT_UPDATE);
178 static DEVICE_ATTR(hibernate, 0644, show_hibernate, store_hibernate);
180 static struct bus_type suspend_subsys = {
185 static const struct platform_suspend_ops pseries_suspend_ops = {
186 .valid = suspend_valid_only_mem,
187 .prepare_late = pseries_prepare_late,
188 .enter = pseries_suspend_enter,
192 * pseries_suspend_sysfs_register - Register with sysfs
195 * 0 on success / other on failure
197 static int pseries_suspend_sysfs_register(struct device *dev)
201 if ((rc = subsys_system_register(&suspend_subsys, NULL)))
205 dev->bus = &suspend_subsys;
207 if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
208 goto subsys_unregister;
213 bus_unregister(&suspend_subsys);
218 * pseries_suspend_init - initcall for pSeries suspend
221 * 0 on success / other on failure
223 static int __init pseries_suspend_init(void)
227 if (!firmware_has_feature(FW_FEATURE_LPAR))
230 suspend_data.token = rtas_token("ibm,suspend-me");
231 if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
234 if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
237 ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
238 ppc_md.suspend_enable_irqs = pseries_suspend_enable_irqs;
239 suspend_set_ops(&pseries_suspend_ops);
242 machine_device_initcall(pseries, pseries_suspend_init);