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;
21 * pseries_suspend_begin - First phase of hibernation
23 * Check to ensure we are in a valid state to hibernate
26 * 0 on success / other on failure
28 static int pseries_suspend_begin(u64 stream_id)
31 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
33 /* Make sure the state is valid */
34 rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
36 vasi_state = retbuf[0];
39 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
41 } else if (vasi_state == H_VASI_ENABLED) {
43 } else if (vasi_state != H_VASI_SUSPENDING) {
44 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
52 * pseries_suspend_enter - Final phase of hibernation
55 * 0 on success / other on failure
57 static int pseries_suspend_enter(suspend_state_t state)
59 return rtas_ibm_suspend_me(NULL);
63 * store_hibernate - Initiate partition hibernation
64 * @dev: subsys root device
65 * @attr: device attribute struct
69 * Write the stream ID received from the HMC to this file
70 * to trigger hibernating the partition
73 * number of bytes printed to buffer / other on failure
75 static ssize_t store_hibernate(struct device *dev,
76 struct device_attribute *attr,
77 const char *buf, size_t count)
82 if (!capable(CAP_SYS_ADMIN))
85 stream_id = simple_strtoul(buf, NULL, 16);
88 rc = pseries_suspend_begin(stream_id);
91 } while (rc == -EAGAIN);
94 rc = pm_suspend(PM_SUSPEND_MEM);
98 post_mobility_fixup();
105 #define USER_DT_UPDATE 0
106 #define KERN_DT_UPDATE 1
109 * show_hibernate - Report device tree update responsibilty
110 * @dev: subsys root device
111 * @attr: device attribute struct
114 * Report whether a device tree update is performed by the kernel after a
115 * resume, or if drmgr must coordinate the update from user space.
118 * 0 if drmgr is to initiate update, and 1 otherwise
120 static ssize_t show_hibernate(struct device *dev,
121 struct device_attribute *attr,
124 return sprintf(buf, "%d\n", KERN_DT_UPDATE);
127 static DEVICE_ATTR(hibernate, 0644, show_hibernate, store_hibernate);
129 static struct bus_type suspend_subsys = {
134 static const struct platform_suspend_ops pseries_suspend_ops = {
135 .valid = suspend_valid_only_mem,
136 .enter = pseries_suspend_enter,
140 * pseries_suspend_sysfs_register - Register with sysfs
143 * 0 on success / other on failure
145 static int pseries_suspend_sysfs_register(struct device *dev)
147 struct device *dev_root;
150 if ((rc = subsys_system_register(&suspend_subsys, NULL)))
154 dev->bus = &suspend_subsys;
156 dev_root = bus_get_dev_root(&suspend_subsys);
158 rc = device_create_file(dev_root, &dev_attr_hibernate);
159 put_device(dev_root);
161 goto subsys_unregister;
167 bus_unregister(&suspend_subsys);
172 * pseries_suspend_init - initcall for pSeries suspend
175 * 0 on success / other on failure
177 static int __init pseries_suspend_init(void)
181 if (!firmware_has_feature(FW_FEATURE_LPAR))
184 if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
187 suspend_set_ops(&pseries_suspend_ops);
190 machine_device_initcall(pseries, pseries_suspend_init);