GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / powerpc / platforms / pseries / suspend.c
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  **/
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  **/
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  **/
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  **/
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  **/
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  **/
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                 stop_topology_update();
147                 rc = pm_suspend(PM_SUSPEND_MEM);
148                 start_topology_update();
149         }
150
151         stream_id = 0;
152
153         if (!rc)
154                 rc = count;
155
156         return rc;
157 }
158
159 #define USER_DT_UPDATE  0
160 #define KERN_DT_UPDATE  1
161
162 /**
163  * show_hibernate - Report device tree update responsibilty
164  * @dev:                subsys root device
165  * @attr:               device attribute struct
166  * @buf:                buffer
167  *
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.
170  *
171  * Return value:
172  *      0 if drmgr is to initiate update, and 1 otherwise
173  **/
174 static ssize_t show_hibernate(struct device *dev,
175                               struct device_attribute *attr,
176                               char *buf)
177 {
178         return sprintf(buf, "%d\n", KERN_DT_UPDATE);
179 }
180
181 static DEVICE_ATTR(hibernate, 0644, show_hibernate, store_hibernate);
182
183 static struct bus_type suspend_subsys = {
184         .name = "power",
185         .dev_name = "power",
186 };
187
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,
192 };
193
194 /**
195  * pseries_suspend_sysfs_register - Register with sysfs
196  *
197  * Return value:
198  *      0 on success / other on failure
199  **/
200 static int pseries_suspend_sysfs_register(struct device *dev)
201 {
202         int rc;
203
204         if ((rc = subsys_system_register(&suspend_subsys, NULL)))
205                 return rc;
206
207         dev->id = 0;
208         dev->bus = &suspend_subsys;
209
210         if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
211                 goto subsys_unregister;
212
213         return 0;
214
215 subsys_unregister:
216         bus_unregister(&suspend_subsys);
217         return rc;
218 }
219
220 /**
221  * pseries_suspend_init - initcall for pSeries suspend
222  *
223  * Return value:
224  *      0 on success / other on failure
225  **/
226 static int __init pseries_suspend_init(void)
227 {
228         int rc;
229
230         if (!firmware_has_feature(FW_FEATURE_LPAR))
231                 return 0;
232
233         suspend_data.token = rtas_token("ibm,suspend-me");
234         if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
235                 return 0;
236
237         if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
238                 return rc;
239
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);
243         return 0;
244 }
245 machine_device_initcall(pseries, pseries_suspend_init);