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 stop_topology_update();
147 rc = pm_suspend(PM_SUSPEND_MEM);
148 start_topology_update();
159 #define USER_DT_UPDATE 0
160 #define KERN_DT_UPDATE 1
163 * show_hibernate - Report device tree update responsibilty
164 * @dev: subsys root device
165 * @attr: device attribute struct
168 * Report whether a device tree update is performed by the kernel after a
169 * resume, or if drmgr must coordinate the update from user space.
172 * 0 if drmgr is to initiate update, and 1 otherwise
174 static ssize_t show_hibernate(struct device *dev,
175 struct device_attribute *attr,
178 return sprintf(buf, "%d\n", KERN_DT_UPDATE);
181 static DEVICE_ATTR(hibernate, 0644, show_hibernate, store_hibernate);
183 static struct bus_type suspend_subsys = {
188 static const struct platform_suspend_ops pseries_suspend_ops = {
189 .valid = suspend_valid_only_mem,
190 .prepare_late = pseries_prepare_late,
191 .enter = pseries_suspend_enter,
195 * pseries_suspend_sysfs_register - Register with sysfs
198 * 0 on success / other on failure
200 static int pseries_suspend_sysfs_register(struct device *dev)
204 if ((rc = subsys_system_register(&suspend_subsys, NULL)))
208 dev->bus = &suspend_subsys;
210 if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
211 goto subsys_unregister;
216 bus_unregister(&suspend_subsys);
221 * pseries_suspend_init - initcall for pSeries suspend
224 * 0 on success / other on failure
226 static int __init pseries_suspend_init(void)
230 if (!firmware_has_feature(FW_FEATURE_LPAR))
233 suspend_data.token = rtas_token("ibm,suspend-me");
234 if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
237 if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
240 ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
241 ppc_md.suspend_enable_irqs = pseries_suspend_enable_irqs;
242 suspend_set_ops(&pseries_suspend_ops);
245 machine_device_initcall(pseries, pseries_suspend_init);