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>
17 static struct device suspend_dev
;
20 * pseries_suspend_begin - First phase of hibernation
22 * Check to ensure we are in a valid state to hibernate
25 * 0 on success / other on failure
27 static int pseries_suspend_begin(u64 stream_id
)
30 unsigned long retbuf
[PLPAR_HCALL_BUFSIZE
];
32 /* Make sure the state is valid */
33 rc
= plpar_hcall(H_VASI_STATE
, retbuf
, stream_id
);
35 vasi_state
= retbuf
[0];
38 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc
);
40 } else if (vasi_state
== H_VASI_ENABLED
) {
42 } else if (vasi_state
!= H_VASI_SUSPENDING
) {
43 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
51 * pseries_suspend_enter - Final phase of hibernation
54 * 0 on success / other on failure
56 static int pseries_suspend_enter(suspend_state_t state
)
58 return rtas_ibm_suspend_me(NULL
);
62 * store_hibernate - Initiate partition hibernation
63 * @dev: subsys root device
64 * @attr: device attribute struct
68 * Write the stream ID received from the HMC to this file
69 * to trigger hibernating the partition
72 * number of bytes printed to buffer / other on failure
74 static ssize_t
store_hibernate(struct device
*dev
,
75 struct device_attribute
*attr
,
76 const char *buf
, size_t count
)
81 if (!capable(CAP_SYS_ADMIN
))
84 stream_id
= simple_strtoul(buf
, NULL
, 16);
87 rc
= pseries_suspend_begin(stream_id
);
90 } while (rc
== -EAGAIN
);
93 rc
= pm_suspend(PM_SUSPEND_MEM
);
97 post_mobility_fixup();
104 #define USER_DT_UPDATE 0
105 #define KERN_DT_UPDATE 1
108 * show_hibernate - Report device tree update responsibilty
109 * @dev: subsys root device
110 * @attr: device attribute struct
113 * Report whether a device tree update is performed by the kernel after a
114 * resume, or if drmgr must coordinate the update from user space.
117 * 0 if drmgr is to initiate update, and 1 otherwise
119 static ssize_t
show_hibernate(struct device
*dev
,
120 struct device_attribute
*attr
,
123 return sprintf(buf
, "%d\n", KERN_DT_UPDATE
);
126 static DEVICE_ATTR(hibernate
, 0644, show_hibernate
, store_hibernate
);
128 static struct bus_type suspend_subsys
= {
133 static const struct platform_suspend_ops pseries_suspend_ops
= {
134 .valid
= suspend_valid_only_mem
,
135 .enter
= pseries_suspend_enter
,
139 * pseries_suspend_sysfs_register - Register with sysfs
142 * 0 on success / other on failure
144 static int pseries_suspend_sysfs_register(struct device
*dev
)
148 if ((rc
= subsys_system_register(&suspend_subsys
, NULL
)))
152 dev
->bus
= &suspend_subsys
;
154 if ((rc
= device_create_file(suspend_subsys
.dev_root
, &dev_attr_hibernate
)))
155 goto subsys_unregister
;
160 bus_unregister(&suspend_subsys
);
165 * pseries_suspend_init - initcall for pSeries suspend
168 * 0 on success / other on failure
170 static int __init
pseries_suspend_init(void)
174 if (!firmware_has_feature(FW_FEATURE_LPAR
))
177 if ((rc
= pseries_suspend_sysfs_register(&suspend_dev
)))
180 suspend_set_ops(&pseries_suspend_ops
);
183 machine_device_initcall(pseries
, pseries_suspend_init
);