1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2010 Brian King IBM Corporation
4 */
5
6 #include <linux/cpu.h>
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>
13 #include <asm/mmu.h>
14 #include <asm/rtas.h>
15 #include <asm/topology.h>
16
17 static u64 stream_id;
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;
22
23 /**
24 * pseries_suspend_begin - First phase of hibernation
25 *
26 * Check to ensure we are in a valid state to hibernate
27 *
28 * Return value:
29 * 0 on success / other on failure
30 **/
pseries_suspend_begin(suspend_state_t state)31 static int pseries_suspend_begin(suspend_state_t state)
32 {
33 long vasi_state, rc;
34 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
35
36 /* Make sure the state is valid */
37 rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
38
39 vasi_state = retbuf[0];
40
41 if (rc) {
42 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
43 return rc;
44 } else if (vasi_state == H_VASI_ENABLED) {
45 return -EAGAIN;
46 } else if (vasi_state != H_VASI_SUSPENDING) {
47 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
48 vasi_state);
49 return -EIO;
50 }
51
52 return 0;
53 }
54
55 /**
56 * pseries_suspend_cpu - Suspend a single CPU
57 *
58 * Makes the H_JOIN call to suspend the CPU
59 *
60 **/
pseries_suspend_cpu(void)61 static int pseries_suspend_cpu(void)
62 {
63 if (atomic_read(&suspending))
64 return rtas_suspend_cpu(&suspend_data);
65 return 0;
66 }
67
68 /**
69 * pseries_suspend_enable_irqs
70 *
71 * Post suspend configuration updates
72 *
73 **/
pseries_suspend_enable_irqs(void)74 static void pseries_suspend_enable_irqs(void)
75 {
76 /*
77 * Update configuration which can be modified based on device tree
78 * changes during resume.
79 */
80 post_mobility_fixup();
81 }
82
83 /**
84 * pseries_suspend_enter - Final phase of hibernation
85 *
86 * Return value:
87 * 0 on success / other on failure
88 **/
pseries_suspend_enter(suspend_state_t state)89 static int pseries_suspend_enter(suspend_state_t state)
90 {
91 int rc = rtas_suspend_last_cpu(&suspend_data);
92
93 atomic_set(&suspending, 0);
94 atomic_set(&suspend_data.done, 1);
95 return rc;
96 }
97
98 /**
99 * pseries_prepare_late - Prepare to suspend all other CPUs
100 *
101 * Return value:
102 * 0 on success / other on failure
103 **/
pseries_prepare_late(void)104 static int pseries_prepare_late(void)
105 {
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);
112 return 0;
113 }
114
115 /**
116 * store_hibernate - Initiate partition hibernation
117 * @dev: subsys root device
118 * @attr: device attribute struct
119 * @buf: buffer
120 * @count: buffer size
121 *
122 * Write the stream ID received from the HMC to this file
123 * to trigger hibernating the partition
124 *
125 * Return value:
126 * number of bytes printed to buffer / other on failure
127 **/
store_hibernate(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)128 static ssize_t store_hibernate(struct device *dev,
129 struct device_attribute *attr,
130 const char *buf, size_t count)
131 {
132 int rc;
133
134 if (!capable(CAP_SYS_ADMIN))
135 return -EPERM;
136
137 stream_id = simple_strtoul(buf, NULL, 16);
138
139 do {
140 rc = pseries_suspend_begin(PM_SUSPEND_MEM);
141 if (rc == -EAGAIN)
142 ssleep(1);
143 } while (rc == -EAGAIN);
144
145 if (!rc)
146 rc = pm_suspend(PM_SUSPEND_MEM);
147
148 stream_id = 0;
149
150 if (!rc)
151 rc = count;
152
153 return rc;
154 }
155
156 #define USER_DT_UPDATE 0
157 #define KERN_DT_UPDATE 1
158
159 /**
160 * show_hibernate - Report device tree update responsibilty
161 * @dev: subsys root device
162 * @attr: device attribute struct
163 * @buf: buffer
164 *
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.
167 *
168 * Return value:
169 * 0 if drmgr is to initiate update, and 1 otherwise
170 **/
show_hibernate(struct device * dev,struct device_attribute * attr,char * buf)171 static ssize_t show_hibernate(struct device *dev,
172 struct device_attribute *attr,
173 char *buf)
174 {
175 return sprintf(buf, "%d\n", KERN_DT_UPDATE);
176 }
177
178 static DEVICE_ATTR(hibernate, 0644, show_hibernate, store_hibernate);
179
180 static struct bus_type suspend_subsys = {
181 .name = "power",
182 .dev_name = "power",
183 };
184
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,
189 };
190
191 /**
192 * pseries_suspend_sysfs_register - Register with sysfs
193 *
194 * Return value:
195 * 0 on success / other on failure
196 **/
pseries_suspend_sysfs_register(struct device * dev)197 static int pseries_suspend_sysfs_register(struct device *dev)
198 {
199 int rc;
200
201 if ((rc = subsys_system_register(&suspend_subsys, NULL)))
202 return rc;
203
204 dev->id = 0;
205 dev->bus = &suspend_subsys;
206
207 if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
208 goto subsys_unregister;
209
210 return 0;
211
212 subsys_unregister:
213 bus_unregister(&suspend_subsys);
214 return rc;
215 }
216
217 /**
218 * pseries_suspend_init - initcall for pSeries suspend
219 *
220 * Return value:
221 * 0 on success / other on failure
222 **/
pseries_suspend_init(void)223 static int __init pseries_suspend_init(void)
224 {
225 int rc;
226
227 if (!firmware_has_feature(FW_FEATURE_LPAR))
228 return 0;
229
230 suspend_data.token = rtas_token("ibm,suspend-me");
231 if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
232 return 0;
233
234 if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
235 return rc;
236
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);
240 return 0;
241 }
242 machine_device_initcall(pseries, pseries_suspend_init);
243