xref: /linux/arch/powerpc/platforms/pseries/mobility.c (revision 4c73cadcdc64b53248bca85baa8a19e7384701ec)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2410bccf9SNathan Fontenot /*
3410bccf9SNathan Fontenot  * Support for Partition Mobility/Migration
4410bccf9SNathan Fontenot  *
5410bccf9SNathan Fontenot  * Copyright (C) 2010 Nathan Fontenot
6410bccf9SNathan Fontenot  * Copyright (C) 2010 IBM Corporation
7410bccf9SNathan Fontenot  */
8410bccf9SNathan Fontenot 
9494a66f3SNathan Lynch 
10494a66f3SNathan Lynch #define pr_fmt(fmt) "mobility: " fmt
11494a66f3SNathan Lynch 
12e59a175fSNathan Lynch #include <linux/cpu.h>
13410bccf9SNathan Fontenot #include <linux/kernel.h>
14410bccf9SNathan Fontenot #include <linux/kobject.h>
159327dc0aSNathan Lynch #include <linux/nmi.h>
16ccfb5bd7SNathan Lynch #include <linux/sched.h>
17410bccf9SNathan Fontenot #include <linux/smp.h>
18b56eade5SPaul Gortmaker #include <linux/stat.h>
199327dc0aSNathan Lynch #include <linux/stop_machine.h>
20410bccf9SNathan Fontenot #include <linux/completion.h>
21410bccf9SNathan Fontenot #include <linux/device.h>
22410bccf9SNathan Fontenot #include <linux/delay.h>
23410bccf9SNathan Fontenot #include <linux/slab.h>
245c35a02cSChristophe Leroy #include <linux/stringify.h>
25410bccf9SNathan Fontenot 
268e83e905SMichael Ellerman #include <asm/machdep.h>
27410bccf9SNathan Fontenot #include <asm/rtas.h>
28410bccf9SNathan Fontenot #include "pseries.h"
2937e67648SHaren Myneni #include "vas.h"	/* vas_migration_handler() */
30e610a466SNathan Lynch #include "../../kernel/cacheinfo.h"
31410bccf9SNathan Fontenot 
32410bccf9SNathan Fontenot static struct kobject *mobility_kobj;
33410bccf9SNathan Fontenot 
34410bccf9SNathan Fontenot struct update_props_workarea {
35f6ff0414STyrel Datwyler 	__be32 phandle;
36f6ff0414STyrel Datwyler 	__be32 state;
37f6ff0414STyrel Datwyler 	__be64 reserved;
38f6ff0414STyrel Datwyler 	__be32 nprops;
39d0ef4403STyrel Datwyler } __packed;
40410bccf9SNathan Fontenot 
41410bccf9SNathan Fontenot #define NODE_ACTION_MASK	0xff000000
42410bccf9SNathan Fontenot #define NODE_COUNT_MASK		0x00ffffff
43410bccf9SNathan Fontenot 
44410bccf9SNathan Fontenot #define DELETE_DT_NODE	0x01000000
45410bccf9SNathan Fontenot #define UPDATE_DT_NODE	0x02000000
46410bccf9SNathan Fontenot #define ADD_DT_NODE	0x03000000
47410bccf9SNathan Fontenot 
48762ec157SNathan Fontenot #define MIGRATION_SCOPE	(1)
49675d8ee6SJohn Allen #define PRRN_SCOPE -2
50762ec157SNathan Fontenot 
51118b1366SLaurent Dufour #ifdef CONFIG_PPC_WATCHDOG
52118b1366SLaurent Dufour static unsigned int nmi_wd_lpm_factor = 200;
53118b1366SLaurent Dufour 
54118b1366SLaurent Dufour #ifdef CONFIG_SYSCTL
55118b1366SLaurent Dufour static struct ctl_table nmi_wd_lpm_factor_ctl_table[] = {
56118b1366SLaurent Dufour 	{
57118b1366SLaurent Dufour 		.procname	= "nmi_wd_lpm_factor",
58118b1366SLaurent Dufour 		.data		= &nmi_wd_lpm_factor,
59118b1366SLaurent Dufour 		.maxlen		= sizeof(int),
60118b1366SLaurent Dufour 		.mode		= 0644,
61118b1366SLaurent Dufour 		.proc_handler	= proc_douintvec_minmax,
62118b1366SLaurent Dufour 	},
63118b1366SLaurent Dufour 	{}
64118b1366SLaurent Dufour };
65118b1366SLaurent Dufour static struct ctl_table nmi_wd_lpm_factor_sysctl_root[] = {
66118b1366SLaurent Dufour 	{
67118b1366SLaurent Dufour 		.procname       = "kernel",
68118b1366SLaurent Dufour 		.mode           = 0555,
69118b1366SLaurent Dufour 		.child          = nmi_wd_lpm_factor_ctl_table,
70118b1366SLaurent Dufour 	},
71118b1366SLaurent Dufour 	{}
72118b1366SLaurent Dufour };
73118b1366SLaurent Dufour 
74118b1366SLaurent Dufour static int __init register_nmi_wd_lpm_factor_sysctl(void)
75118b1366SLaurent Dufour {
76118b1366SLaurent Dufour 	register_sysctl_table(nmi_wd_lpm_factor_sysctl_root);
77118b1366SLaurent Dufour 
78118b1366SLaurent Dufour 	return 0;
79118b1366SLaurent Dufour }
80118b1366SLaurent Dufour device_initcall(register_nmi_wd_lpm_factor_sysctl);
81118b1366SLaurent Dufour #endif /* CONFIG_SYSCTL */
82118b1366SLaurent Dufour #endif /* CONFIG_PPC_WATCHDOG */
83118b1366SLaurent Dufour 
84762ec157SNathan Fontenot static int mobility_rtas_call(int token, char *buf, s32 scope)
85410bccf9SNathan Fontenot {
86410bccf9SNathan Fontenot 	int rc;
87410bccf9SNathan Fontenot 
88410bccf9SNathan Fontenot 	spin_lock(&rtas_data_buf_lock);
89410bccf9SNathan Fontenot 
90410bccf9SNathan Fontenot 	memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
91762ec157SNathan Fontenot 	rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
92410bccf9SNathan Fontenot 	memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
93410bccf9SNathan Fontenot 
94410bccf9SNathan Fontenot 	spin_unlock(&rtas_data_buf_lock);
95410bccf9SNathan Fontenot 	return rc;
96410bccf9SNathan Fontenot }
97410bccf9SNathan Fontenot 
982efd7f6eSNathan Lynch static int delete_dt_node(struct device_node *dn)
99410bccf9SNathan Fontenot {
100319fa1a5SNathan Lynch 	struct device_node *pdn;
101319fa1a5SNathan Lynch 	bool is_platfac;
102319fa1a5SNathan Lynch 
103319fa1a5SNathan Lynch 	pdn = of_get_parent(dn);
104319fa1a5SNathan Lynch 	is_platfac = of_node_is_type(dn, "ibm,platform-facilities") ||
105319fa1a5SNathan Lynch 		     of_node_is_type(pdn, "ibm,platform-facilities");
106319fa1a5SNathan Lynch 	of_node_put(pdn);
107319fa1a5SNathan Lynch 
108319fa1a5SNathan Lynch 	/*
109319fa1a5SNathan Lynch 	 * The drivers that bind to nodes in the platform-facilities
110319fa1a5SNathan Lynch 	 * hierarchy don't support node removal, and the removal directive
111319fa1a5SNathan Lynch 	 * from firmware is always followed by an add of an equivalent
112319fa1a5SNathan Lynch 	 * node. The capability (e.g. RNG, encryption, compression)
113319fa1a5SNathan Lynch 	 * represented by the node is never interrupted by the migration.
114319fa1a5SNathan Lynch 	 * So ignore changes to this part of the tree.
115319fa1a5SNathan Lynch 	 */
116319fa1a5SNathan Lynch 	if (is_platfac) {
117319fa1a5SNathan Lynch 		pr_notice("ignoring remove operation for %pOFfp\n", dn);
118319fa1a5SNathan Lynch 		return 0;
119319fa1a5SNathan Lynch 	}
120319fa1a5SNathan Lynch 
1215d8b1f9dSNathan Lynch 	pr_debug("removing node %pOFfp\n", dn);
122410bccf9SNathan Fontenot 	dlpar_detach_node(dn);
123410bccf9SNathan Fontenot 	return 0;
124410bccf9SNathan Fontenot }
125410bccf9SNathan Fontenot 
126410bccf9SNathan Fontenot static int update_dt_property(struct device_node *dn, struct property **prop,
127410bccf9SNathan Fontenot 			      const char *name, u32 vd, char *value)
128410bccf9SNathan Fontenot {
129410bccf9SNathan Fontenot 	struct property *new_prop = *prop;
130410bccf9SNathan Fontenot 	int more = 0;
131410bccf9SNathan Fontenot 
132410bccf9SNathan Fontenot 	/* A negative 'vd' value indicates that only part of the new property
133410bccf9SNathan Fontenot 	 * value is contained in the buffer and we need to call
134410bccf9SNathan Fontenot 	 * ibm,update-properties again to get the rest of the value.
135410bccf9SNathan Fontenot 	 *
136410bccf9SNathan Fontenot 	 * A negative value is also the two's compliment of the actual value.
137410bccf9SNathan Fontenot 	 */
138410bccf9SNathan Fontenot 	if (vd & 0x80000000) {
139410bccf9SNathan Fontenot 		vd = ~vd + 1;
140410bccf9SNathan Fontenot 		more = 1;
141410bccf9SNathan Fontenot 	}
142410bccf9SNathan Fontenot 
143410bccf9SNathan Fontenot 	if (new_prop) {
144410bccf9SNathan Fontenot 		/* partial property fixup */
145410bccf9SNathan Fontenot 		char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
146410bccf9SNathan Fontenot 		if (!new_data)
147410bccf9SNathan Fontenot 			return -ENOMEM;
148410bccf9SNathan Fontenot 
149410bccf9SNathan Fontenot 		memcpy(new_data, new_prop->value, new_prop->length);
150410bccf9SNathan Fontenot 		memcpy(new_data + new_prop->length, value, vd);
151410bccf9SNathan Fontenot 
152410bccf9SNathan Fontenot 		kfree(new_prop->value);
153410bccf9SNathan Fontenot 		new_prop->value = new_data;
154410bccf9SNathan Fontenot 		new_prop->length += vd;
155410bccf9SNathan Fontenot 	} else {
156410bccf9SNathan Fontenot 		new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
157410bccf9SNathan Fontenot 		if (!new_prop)
158410bccf9SNathan Fontenot 			return -ENOMEM;
159410bccf9SNathan Fontenot 
160410bccf9SNathan Fontenot 		new_prop->name = kstrdup(name, GFP_KERNEL);
161410bccf9SNathan Fontenot 		if (!new_prop->name) {
162410bccf9SNathan Fontenot 			kfree(new_prop);
163410bccf9SNathan Fontenot 			return -ENOMEM;
164410bccf9SNathan Fontenot 		}
165410bccf9SNathan Fontenot 
166410bccf9SNathan Fontenot 		new_prop->length = vd;
167410bccf9SNathan Fontenot 		new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
168410bccf9SNathan Fontenot 		if (!new_prop->value) {
169410bccf9SNathan Fontenot 			kfree(new_prop->name);
170410bccf9SNathan Fontenot 			kfree(new_prop);
171410bccf9SNathan Fontenot 			return -ENOMEM;
172410bccf9SNathan Fontenot 		}
173410bccf9SNathan Fontenot 
174410bccf9SNathan Fontenot 		memcpy(new_prop->value, value, vd);
175410bccf9SNathan Fontenot 		*prop = new_prop;
176410bccf9SNathan Fontenot 	}
177410bccf9SNathan Fontenot 
178410bccf9SNathan Fontenot 	if (!more) {
1795d8b1f9dSNathan Lynch 		pr_debug("updating node %pOF property %s\n", dn, name);
18079d1c712SNathan Fontenot 		of_update_property(dn, new_prop);
181d8e533b4STyrel Datwyler 		*prop = NULL;
182410bccf9SNathan Fontenot 	}
183410bccf9SNathan Fontenot 
184410bccf9SNathan Fontenot 	return 0;
185410bccf9SNathan Fontenot }
186410bccf9SNathan Fontenot 
1872efd7f6eSNathan Lynch static int update_dt_node(struct device_node *dn, s32 scope)
188410bccf9SNathan Fontenot {
189410bccf9SNathan Fontenot 	struct update_props_workarea *upwa;
190410bccf9SNathan Fontenot 	struct property *prop = NULL;
191638a405fSTyrel Datwyler 	int i, rc, rtas_rc;
192410bccf9SNathan Fontenot 	char *prop_data;
193410bccf9SNathan Fontenot 	char *rtas_buf;
194410bccf9SNathan Fontenot 	int update_properties_token;
195f6ff0414STyrel Datwyler 	u32 nprops;
1962e9b7b02SNathan Fontenot 	u32 vd;
197410bccf9SNathan Fontenot 
198410bccf9SNathan Fontenot 	update_properties_token = rtas_token("ibm,update-properties");
199410bccf9SNathan Fontenot 	if (update_properties_token == RTAS_UNKNOWN_SERVICE)
200410bccf9SNathan Fontenot 		return -EINVAL;
201410bccf9SNathan Fontenot 
202410bccf9SNathan Fontenot 	rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
203410bccf9SNathan Fontenot 	if (!rtas_buf)
204410bccf9SNathan Fontenot 		return -ENOMEM;
205410bccf9SNathan Fontenot 
206410bccf9SNathan Fontenot 	upwa = (struct update_props_workarea *)&rtas_buf[0];
2072efd7f6eSNathan Lynch 	upwa->phandle = cpu_to_be32(dn->phandle);
208410bccf9SNathan Fontenot 
209410bccf9SNathan Fontenot 	do {
210638a405fSTyrel Datwyler 		rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
211762ec157SNathan Fontenot 					scope);
212638a405fSTyrel Datwyler 		if (rtas_rc < 0)
213410bccf9SNathan Fontenot 			break;
214410bccf9SNathan Fontenot 
215410bccf9SNathan Fontenot 		prop_data = rtas_buf + sizeof(*upwa);
216f6ff0414STyrel Datwyler 		nprops = be32_to_cpu(upwa->nprops);
217410bccf9SNathan Fontenot 
218c8f5a57cSTyrel Datwyler 		/* On the first call to ibm,update-properties for a node the
219*4c73cadcSJilin Yuan 		 * first property value descriptor contains an empty
220c8f5a57cSTyrel Datwyler 		 * property name, the property value length encoded as u32,
221c8f5a57cSTyrel Datwyler 		 * and the property value is the node path being updated.
2222e9b7b02SNathan Fontenot 		 */
223c8f5a57cSTyrel Datwyler 		if (*prop_data == 0) {
224c8f5a57cSTyrel Datwyler 			prop_data++;
225f6ff0414STyrel Datwyler 			vd = be32_to_cpu(*(__be32 *)prop_data);
226c8f5a57cSTyrel Datwyler 			prop_data += vd + sizeof(vd);
227f6ff0414STyrel Datwyler 			nprops--;
228c8f5a57cSTyrel Datwyler 		}
2292e9b7b02SNathan Fontenot 
230f6ff0414STyrel Datwyler 		for (i = 0; i < nprops; i++) {
2312e9b7b02SNathan Fontenot 			char *prop_name;
2322e9b7b02SNathan Fontenot 
2332e9b7b02SNathan Fontenot 			prop_name = prop_data;
2342e9b7b02SNathan Fontenot 			prop_data += strlen(prop_name) + 1;
235f6ff0414STyrel Datwyler 			vd = be32_to_cpu(*(__be32 *)prop_data);
2362e9b7b02SNathan Fontenot 			prop_data += sizeof(vd);
237410bccf9SNathan Fontenot 
238410bccf9SNathan Fontenot 			switch (vd) {
239410bccf9SNathan Fontenot 			case 0x00000000:
240410bccf9SNathan Fontenot 				/* name only property, nothing to do */
241410bccf9SNathan Fontenot 				break;
242410bccf9SNathan Fontenot 
243410bccf9SNathan Fontenot 			case 0x80000000:
244925e2d1dSSuraj Jitindar Singh 				of_remove_property(dn, of_find_property(dn,
245925e2d1dSSuraj Jitindar Singh 							prop_name, NULL));
246410bccf9SNathan Fontenot 				prop = NULL;
247410bccf9SNathan Fontenot 				break;
248410bccf9SNathan Fontenot 
249410bccf9SNathan Fontenot 			default:
250410bccf9SNathan Fontenot 				rc = update_dt_property(dn, &prop, prop_name,
251410bccf9SNathan Fontenot 							vd, prop_data);
252410bccf9SNathan Fontenot 				if (rc) {
2532d5be6f1SNathan Lynch 					pr_err("updating %s property failed: %d\n",
2542d5be6f1SNathan Lynch 					       prop_name, rc);
255410bccf9SNathan Fontenot 				}
256410bccf9SNathan Fontenot 
257410bccf9SNathan Fontenot 				prop_data += vd;
258aa5e5c9bSNathan Lynch 				break;
259410bccf9SNathan Fontenot 			}
260ccfb5bd7SNathan Lynch 
261ccfb5bd7SNathan Lynch 			cond_resched();
262410bccf9SNathan Fontenot 		}
263ccfb5bd7SNathan Lynch 
264ccfb5bd7SNathan Lynch 		cond_resched();
265638a405fSTyrel Datwyler 	} while (rtas_rc == 1);
266410bccf9SNathan Fontenot 
267410bccf9SNathan Fontenot 	kfree(rtas_buf);
268410bccf9SNathan Fontenot 	return 0;
269410bccf9SNathan Fontenot }
270410bccf9SNathan Fontenot 
2712efd7f6eSNathan Lynch static int add_dt_node(struct device_node *parent_dn, __be32 drc_index)
272410bccf9SNathan Fontenot {
273410bccf9SNathan Fontenot 	struct device_node *dn;
274410bccf9SNathan Fontenot 	int rc;
275410bccf9SNathan Fontenot 
2768d5ff320STyrel Datwyler 	dn = dlpar_configure_connector(drc_index, parent_dn);
2772efd7f6eSNathan Lynch 	if (!dn)
278410bccf9SNathan Fontenot 		return -ENOENT;
279410bccf9SNathan Fontenot 
280319fa1a5SNathan Lynch 	/*
281319fa1a5SNathan Lynch 	 * Since delete_dt_node() ignores this node type, this is the
282319fa1a5SNathan Lynch 	 * necessary counterpart. We also know that a platform-facilities
283319fa1a5SNathan Lynch 	 * node returned from dlpar_configure_connector() has children
284319fa1a5SNathan Lynch 	 * attached, and dlpar_attach_node() only adds the parent, leaking
285319fa1a5SNathan Lynch 	 * the children. So ignore these on the add side for now.
286319fa1a5SNathan Lynch 	 */
287319fa1a5SNathan Lynch 	if (of_node_is_type(dn, "ibm,platform-facilities")) {
288319fa1a5SNathan Lynch 		pr_notice("ignoring add operation for %pOF\n", dn);
289319fa1a5SNathan Lynch 		dlpar_free_cc_nodes(dn);
290319fa1a5SNathan Lynch 		return 0;
291319fa1a5SNathan Lynch 	}
292319fa1a5SNathan Lynch 
293215ee763SRob Herring 	rc = dlpar_attach_node(dn, parent_dn);
294410bccf9SNathan Fontenot 	if (rc)
295410bccf9SNathan Fontenot 		dlpar_free_cc_nodes(dn);
296410bccf9SNathan Fontenot 
2975d8b1f9dSNathan Lynch 	pr_debug("added node %pOFfp\n", dn);
2985d8b1f9dSNathan Lynch 
299410bccf9SNathan Fontenot 	return rc;
300410bccf9SNathan Fontenot }
301410bccf9SNathan Fontenot 
30292e6dc25SNathan Lynch static int pseries_devicetree_update(s32 scope)
303410bccf9SNathan Fontenot {
304410bccf9SNathan Fontenot 	char *rtas_buf;
305f6ff0414STyrel Datwyler 	__be32 *data;
306410bccf9SNathan Fontenot 	int update_nodes_token;
307410bccf9SNathan Fontenot 	int rc;
308410bccf9SNathan Fontenot 
309410bccf9SNathan Fontenot 	update_nodes_token = rtas_token("ibm,update-nodes");
310410bccf9SNathan Fontenot 	if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
311b06a6717SNathan Lynch 		return 0;
312410bccf9SNathan Fontenot 
313410bccf9SNathan Fontenot 	rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
314410bccf9SNathan Fontenot 	if (!rtas_buf)
315410bccf9SNathan Fontenot 		return -ENOMEM;
316410bccf9SNathan Fontenot 
317410bccf9SNathan Fontenot 	do {
318762ec157SNathan Fontenot 		rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
319410bccf9SNathan Fontenot 		if (rc && rc != 1)
320410bccf9SNathan Fontenot 			break;
321410bccf9SNathan Fontenot 
322f6ff0414STyrel Datwyler 		data = (__be32 *)rtas_buf + 4;
323f6ff0414STyrel Datwyler 		while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
324410bccf9SNathan Fontenot 			int i;
325f6ff0414STyrel Datwyler 			u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
326f6ff0414STyrel Datwyler 			u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
327410bccf9SNathan Fontenot 
328410bccf9SNathan Fontenot 			data++;
329410bccf9SNathan Fontenot 
330410bccf9SNathan Fontenot 			for (i = 0; i < node_count; i++) {
3312efd7f6eSNathan Lynch 				struct device_node *np;
332f6ff0414STyrel Datwyler 				__be32 phandle = *data++;
333f6ff0414STyrel Datwyler 				__be32 drc_index;
334410bccf9SNathan Fontenot 
3352efd7f6eSNathan Lynch 				np = of_find_node_by_phandle(be32_to_cpu(phandle));
3362efd7f6eSNathan Lynch 				if (!np) {
3372efd7f6eSNathan Lynch 					pr_warn("Failed lookup: phandle 0x%x for action 0x%x\n",
3382efd7f6eSNathan Lynch 						be32_to_cpu(phandle), action);
3392efd7f6eSNathan Lynch 					continue;
3402efd7f6eSNathan Lynch 				}
3412efd7f6eSNathan Lynch 
342410bccf9SNathan Fontenot 				switch (action) {
343410bccf9SNathan Fontenot 				case DELETE_DT_NODE:
3442efd7f6eSNathan Lynch 					delete_dt_node(np);
345410bccf9SNathan Fontenot 					break;
346410bccf9SNathan Fontenot 				case UPDATE_DT_NODE:
3472efd7f6eSNathan Lynch 					update_dt_node(np, scope);
348410bccf9SNathan Fontenot 					break;
349410bccf9SNathan Fontenot 				case ADD_DT_NODE:
350410bccf9SNathan Fontenot 					drc_index = *data++;
3512efd7f6eSNathan Lynch 					add_dt_node(np, drc_index);
352410bccf9SNathan Fontenot 					break;
353410bccf9SNathan Fontenot 				}
354ccfb5bd7SNathan Lynch 
3552efd7f6eSNathan Lynch 				of_node_put(np);
356ccfb5bd7SNathan Lynch 				cond_resched();
357410bccf9SNathan Fontenot 			}
358410bccf9SNathan Fontenot 		}
359ccfb5bd7SNathan Lynch 
360ccfb5bd7SNathan Lynch 		cond_resched();
361410bccf9SNathan Fontenot 	} while (rc == 1);
362410bccf9SNathan Fontenot 
363410bccf9SNathan Fontenot 	kfree(rtas_buf);
364410bccf9SNathan Fontenot 	return rc;
365410bccf9SNathan Fontenot }
366410bccf9SNathan Fontenot 
367410bccf9SNathan Fontenot void post_mobility_fixup(void)
368410bccf9SNathan Fontenot {
369410bccf9SNathan Fontenot 	int rc;
370410bccf9SNathan Fontenot 
371c3ae9781SNathan Lynch 	rtas_activate_firmware();
37239a33b59SHaren Myneni 
373e59a175fSNathan Lynch 	/*
374e59a175fSNathan Lynch 	 * We don't want CPUs to go online/offline while the device
375e59a175fSNathan Lynch 	 * tree is being updated.
376e59a175fSNathan Lynch 	 */
377e59a175fSNathan Lynch 	cpus_read_lock();
378e59a175fSNathan Lynch 
379e610a466SNathan Lynch 	/*
380e610a466SNathan Lynch 	 * It's common for the destination firmware to replace cache
381e610a466SNathan Lynch 	 * nodes.  Release all of the cacheinfo hierarchy's references
382e610a466SNathan Lynch 	 * before updating the device tree.
383e610a466SNathan Lynch 	 */
384e610a466SNathan Lynch 	cacheinfo_teardown();
385e610a466SNathan Lynch 
386762ec157SNathan Fontenot 	rc = pseries_devicetree_update(MIGRATION_SCOPE);
387410bccf9SNathan Fontenot 	if (rc)
3882d5be6f1SNathan Lynch 		pr_err("device tree update failed: %d\n", rc);
389410bccf9SNathan Fontenot 
390e610a466SNathan Lynch 	cacheinfo_rebuild();
391e610a466SNathan Lynch 
392e59a175fSNathan Lynch 	cpus_read_unlock();
393e59a175fSNathan Lynch 
394da631f7fSDaniel Axtens 	/* Possibly switch to a new L1 flush type */
395da631f7fSDaniel Axtens 	pseries_setup_security_mitigations();
396921bc6cfSMichael Ellerman 
397373b3730SKajol Jain 	/* Reinitialise system information for hv-24x7 */
398373b3730SKajol Jain 	read_24x7_sys_info();
399373b3730SKajol Jain 
400410bccf9SNathan Fontenot 	return;
401410bccf9SNathan Fontenot }
402410bccf9SNathan Fontenot 
403d9213319SNathan Lynch static int poll_vasi_state(u64 handle, unsigned long *res)
404d9213319SNathan Lynch {
405d9213319SNathan Lynch 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
406d9213319SNathan Lynch 	long hvrc;
407d9213319SNathan Lynch 	int ret;
408d9213319SNathan Lynch 
409d9213319SNathan Lynch 	hvrc = plpar_hcall(H_VASI_STATE, retbuf, handle);
410d9213319SNathan Lynch 	switch (hvrc) {
411d9213319SNathan Lynch 	case H_SUCCESS:
412d9213319SNathan Lynch 		ret = 0;
413d9213319SNathan Lynch 		*res = retbuf[0];
414d9213319SNathan Lynch 		break;
415d9213319SNathan Lynch 	case H_PARAMETER:
416d9213319SNathan Lynch 		ret = -EINVAL;
417d9213319SNathan Lynch 		break;
418d9213319SNathan Lynch 	case H_FUNCTION:
419d9213319SNathan Lynch 		ret = -EOPNOTSUPP;
420d9213319SNathan Lynch 		break;
421d9213319SNathan Lynch 	case H_HARDWARE:
422d9213319SNathan Lynch 	default:
423d9213319SNathan Lynch 		pr_err("unexpected H_VASI_STATE result %ld\n", hvrc);
424d9213319SNathan Lynch 		ret = -EIO;
425d9213319SNathan Lynch 		break;
426d9213319SNathan Lynch 	}
427d9213319SNathan Lynch 	return ret;
428d9213319SNathan Lynch }
429d9213319SNathan Lynch 
430d9213319SNathan Lynch static int wait_for_vasi_session_suspending(u64 handle)
431d9213319SNathan Lynch {
432d9213319SNathan Lynch 	unsigned long state;
433d9213319SNathan Lynch 	int ret;
434d9213319SNathan Lynch 
435d9213319SNathan Lynch 	/*
436d9213319SNathan Lynch 	 * Wait for transition from H_VASI_ENABLED to
437d9213319SNathan Lynch 	 * H_VASI_SUSPENDING. Treat anything else as an error.
438d9213319SNathan Lynch 	 */
439d9213319SNathan Lynch 	while (true) {
440d9213319SNathan Lynch 		ret = poll_vasi_state(handle, &state);
441d9213319SNathan Lynch 
442d9213319SNathan Lynch 		if (ret != 0 || state == H_VASI_SUSPENDING) {
443d9213319SNathan Lynch 			break;
444d9213319SNathan Lynch 		} else if (state == H_VASI_ENABLED) {
445d9213319SNathan Lynch 			ssleep(1);
446d9213319SNathan Lynch 		} else {
447d9213319SNathan Lynch 			pr_err("unexpected H_VASI_STATE result %lu\n", state);
448d9213319SNathan Lynch 			ret = -EIO;
449d9213319SNathan Lynch 			break;
450d9213319SNathan Lynch 		}
451d9213319SNathan Lynch 	}
452d9213319SNathan Lynch 
453d9213319SNathan Lynch 	/*
454d9213319SNathan Lynch 	 * Proceed even if H_VASI_STATE is unavailable. If H_JOIN or
455d9213319SNathan Lynch 	 * ibm,suspend-me are also unimplemented, we'll recover then.
456d9213319SNathan Lynch 	 */
457d9213319SNathan Lynch 	if (ret == -EOPNOTSUPP)
458d9213319SNathan Lynch 		ret = 0;
459d9213319SNathan Lynch 
460d9213319SNathan Lynch 	return ret;
461d9213319SNathan Lynch }
462d9213319SNathan Lynch 
463882c0d17SLaurent Dufour static void wait_for_vasi_session_completed(u64 handle)
464882c0d17SLaurent Dufour {
465882c0d17SLaurent Dufour 	unsigned long state = 0;
466882c0d17SLaurent Dufour 	int ret;
467882c0d17SLaurent Dufour 
468882c0d17SLaurent Dufour 	pr_info("waiting for memory transfer to complete...\n");
469882c0d17SLaurent Dufour 
470882c0d17SLaurent Dufour 	/*
471882c0d17SLaurent Dufour 	 * Wait for transition from H_VASI_RESUMED to H_VASI_COMPLETED.
472882c0d17SLaurent Dufour 	 */
473882c0d17SLaurent Dufour 	while (true) {
474882c0d17SLaurent Dufour 		ret = poll_vasi_state(handle, &state);
475882c0d17SLaurent Dufour 
476882c0d17SLaurent Dufour 		/*
477882c0d17SLaurent Dufour 		 * If the memory transfer is already complete and the migration
478882c0d17SLaurent Dufour 		 * has been cleaned up by the hypervisor, H_PARAMETER is return,
479882c0d17SLaurent Dufour 		 * which is translate in EINVAL by poll_vasi_state().
480882c0d17SLaurent Dufour 		 */
481882c0d17SLaurent Dufour 		if (ret == -EINVAL || (!ret && state == H_VASI_COMPLETED)) {
482882c0d17SLaurent Dufour 			pr_info("memory transfer completed.\n");
483882c0d17SLaurent Dufour 			break;
484882c0d17SLaurent Dufour 		}
485882c0d17SLaurent Dufour 
486882c0d17SLaurent Dufour 		if (ret) {
487882c0d17SLaurent Dufour 			pr_err("H_VASI_STATE return error (%d)\n", ret);
488882c0d17SLaurent Dufour 			break;
489882c0d17SLaurent Dufour 		}
490882c0d17SLaurent Dufour 
491882c0d17SLaurent Dufour 		if (state != H_VASI_RESUMED) {
492882c0d17SLaurent Dufour 			pr_err("unexpected H_VASI_STATE result %lu\n", state);
493882c0d17SLaurent Dufour 			break;
494882c0d17SLaurent Dufour 		}
495882c0d17SLaurent Dufour 
496882c0d17SLaurent Dufour 		msleep(500);
497882c0d17SLaurent Dufour 	}
498882c0d17SLaurent Dufour }
499882c0d17SLaurent Dufour 
5009327dc0aSNathan Lynch static void prod_single(unsigned int target_cpu)
5019327dc0aSNathan Lynch {
5029327dc0aSNathan Lynch 	long hvrc;
5039327dc0aSNathan Lynch 	int hwid;
5049327dc0aSNathan Lynch 
5059327dc0aSNathan Lynch 	hwid = get_hard_smp_processor_id(target_cpu);
5069327dc0aSNathan Lynch 	hvrc = plpar_hcall_norets(H_PROD, hwid);
5079327dc0aSNathan Lynch 	if (hvrc == H_SUCCESS)
5089327dc0aSNathan Lynch 		return;
5099327dc0aSNathan Lynch 	pr_err_ratelimited("H_PROD of CPU %u (hwid %d) error: %ld\n",
5109327dc0aSNathan Lynch 			   target_cpu, hwid, hvrc);
5119327dc0aSNathan Lynch }
5129327dc0aSNathan Lynch 
5139327dc0aSNathan Lynch static void prod_others(void)
5149327dc0aSNathan Lynch {
5159327dc0aSNathan Lynch 	unsigned int cpu;
5169327dc0aSNathan Lynch 
5179327dc0aSNathan Lynch 	for_each_online_cpu(cpu) {
5189327dc0aSNathan Lynch 		if (cpu != smp_processor_id())
5199327dc0aSNathan Lynch 			prod_single(cpu);
5209327dc0aSNathan Lynch 	}
5219327dc0aSNathan Lynch }
5229327dc0aSNathan Lynch 
5239327dc0aSNathan Lynch static u16 clamp_slb_size(void)
5249327dc0aSNathan Lynch {
525387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
5269327dc0aSNathan Lynch 	u16 prev = mmu_slb_size;
5279327dc0aSNathan Lynch 
5289327dc0aSNathan Lynch 	slb_set_size(SLB_MIN_SIZE);
5299327dc0aSNathan Lynch 
5309327dc0aSNathan Lynch 	return prev;
531387e220aSNicholas Piggin #else
532387e220aSNicholas Piggin 	return 0;
533387e220aSNicholas Piggin #endif
5349327dc0aSNathan Lynch }
5359327dc0aSNathan Lynch 
5369327dc0aSNathan Lynch static int do_suspend(void)
5379327dc0aSNathan Lynch {
5389327dc0aSNathan Lynch 	u16 saved_slb_size;
5399327dc0aSNathan Lynch 	int status;
5409327dc0aSNathan Lynch 	int ret;
5419327dc0aSNathan Lynch 
5429327dc0aSNathan Lynch 	pr_info("calling ibm,suspend-me on CPU %i\n", smp_processor_id());
5439327dc0aSNathan Lynch 
5449327dc0aSNathan Lynch 	/*
5459327dc0aSNathan Lynch 	 * The destination processor model may have fewer SLB entries
5469327dc0aSNathan Lynch 	 * than the source. We reduce mmu_slb_size to a safe minimum
5479327dc0aSNathan Lynch 	 * before suspending in order to minimize the possibility of
5489327dc0aSNathan Lynch 	 * programming non-existent entries on the destination. If
5499327dc0aSNathan Lynch 	 * suspend fails, we restore it before returning. On success
5509327dc0aSNathan Lynch 	 * the OF reconfig path will update it from the new device
5519327dc0aSNathan Lynch 	 * tree after resuming on the destination.
5529327dc0aSNathan Lynch 	 */
5539327dc0aSNathan Lynch 	saved_slb_size = clamp_slb_size();
5549327dc0aSNathan Lynch 
5559327dc0aSNathan Lynch 	ret = rtas_ibm_suspend_me(&status);
5569327dc0aSNathan Lynch 	if (ret != 0) {
5579327dc0aSNathan Lynch 		pr_err("ibm,suspend-me error: %d\n", status);
5589327dc0aSNathan Lynch 		slb_set_size(saved_slb_size);
5599327dc0aSNathan Lynch 	}
5609327dc0aSNathan Lynch 
5619327dc0aSNathan Lynch 	return ret;
5629327dc0aSNathan Lynch }
5639327dc0aSNathan Lynch 
564e834df6cSNathan Lynch /**
565e834df6cSNathan Lynch  * struct pseries_suspend_info - State shared between CPUs for join/suspend.
566e834df6cSNathan Lynch  * @counter: Threads are to increment this upon resuming from suspend
567e834df6cSNathan Lynch  *           or if an error is received from H_JOIN. The thread which performs
568e834df6cSNathan Lynch  *           the first increment (i.e. sets it to 1) is responsible for
569e834df6cSNathan Lynch  *           waking the other threads.
570274cb1caSNathan Lynch  * @done: False if join/suspend is in progress. True if the operation is
571274cb1caSNathan Lynch  *        complete (successful or not).
572e834df6cSNathan Lynch  */
573e834df6cSNathan Lynch struct pseries_suspend_info {
574e834df6cSNathan Lynch 	atomic_t counter;
575274cb1caSNathan Lynch 	bool done;
576e834df6cSNathan Lynch };
577e834df6cSNathan Lynch 
5789327dc0aSNathan Lynch static int do_join(void *arg)
5799327dc0aSNathan Lynch {
580e834df6cSNathan Lynch 	struct pseries_suspend_info *info = arg;
581e834df6cSNathan Lynch 	atomic_t *counter = &info->counter;
5829327dc0aSNathan Lynch 	long hvrc;
5839327dc0aSNathan Lynch 	int ret;
5849327dc0aSNathan Lynch 
585274cb1caSNathan Lynch retry:
5869327dc0aSNathan Lynch 	/* Must ensure MSR.EE off for H_JOIN. */
5879327dc0aSNathan Lynch 	hard_irq_disable();
5889327dc0aSNathan Lynch 	hvrc = plpar_hcall_norets(H_JOIN);
5899327dc0aSNathan Lynch 
5909327dc0aSNathan Lynch 	switch (hvrc) {
5919327dc0aSNathan Lynch 	case H_CONTINUE:
5929327dc0aSNathan Lynch 		/*
5939327dc0aSNathan Lynch 		 * All other CPUs are offline or in H_JOIN. This CPU
5949327dc0aSNathan Lynch 		 * attempts the suspend.
5959327dc0aSNathan Lynch 		 */
5969327dc0aSNathan Lynch 		ret = do_suspend();
5979327dc0aSNathan Lynch 		break;
5989327dc0aSNathan Lynch 	case H_SUCCESS:
5999327dc0aSNathan Lynch 		/*
6009327dc0aSNathan Lynch 		 * The suspend is complete and this cpu has received a
601274cb1caSNathan Lynch 		 * prod, or we've received a stray prod from unrelated
602274cb1caSNathan Lynch 		 * code (e.g. paravirt spinlocks) and we need to join
603274cb1caSNathan Lynch 		 * again.
604274cb1caSNathan Lynch 		 *
605274cb1caSNathan Lynch 		 * This barrier orders the return from H_JOIN above vs
606274cb1caSNathan Lynch 		 * the load of info->done. It pairs with the barrier
607274cb1caSNathan Lynch 		 * in the wakeup/prod path below.
6089327dc0aSNathan Lynch 		 */
609274cb1caSNathan Lynch 		smp_mb();
610274cb1caSNathan Lynch 		if (READ_ONCE(info->done) == false) {
611274cb1caSNathan Lynch 			pr_info_ratelimited("premature return from H_JOIN on CPU %i, retrying",
612274cb1caSNathan Lynch 					    smp_processor_id());
613274cb1caSNathan Lynch 			goto retry;
614274cb1caSNathan Lynch 		}
6159327dc0aSNathan Lynch 		ret = 0;
6169327dc0aSNathan Lynch 		break;
6179327dc0aSNathan Lynch 	case H_BAD_MODE:
6189327dc0aSNathan Lynch 	case H_HARDWARE:
6199327dc0aSNathan Lynch 	default:
6209327dc0aSNathan Lynch 		ret = -EIO;
6219327dc0aSNathan Lynch 		pr_err_ratelimited("H_JOIN error %ld on CPU %i\n",
6229327dc0aSNathan Lynch 				   hvrc, smp_processor_id());
6239327dc0aSNathan Lynch 		break;
6249327dc0aSNathan Lynch 	}
6259327dc0aSNathan Lynch 
6269327dc0aSNathan Lynch 	if (atomic_inc_return(counter) == 1) {
6279327dc0aSNathan Lynch 		pr_info("CPU %u waking all threads\n", smp_processor_id());
628274cb1caSNathan Lynch 		WRITE_ONCE(info->done, true);
629274cb1caSNathan Lynch 		/*
630274cb1caSNathan Lynch 		 * This barrier orders the store to info->done vs subsequent
631274cb1caSNathan Lynch 		 * H_PRODs to wake the other CPUs. It pairs with the barrier
632274cb1caSNathan Lynch 		 * in the H_SUCCESS case above.
633274cb1caSNathan Lynch 		 */
634274cb1caSNathan Lynch 		smp_mb();
6359327dc0aSNathan Lynch 		prod_others();
6369327dc0aSNathan Lynch 	}
6379327dc0aSNathan Lynch 	/*
6389327dc0aSNathan Lynch 	 * Execution may have been suspended for several seconds, so
6399327dc0aSNathan Lynch 	 * reset the watchdog.
6409327dc0aSNathan Lynch 	 */
6419327dc0aSNathan Lynch 	touch_nmi_watchdog();
6429327dc0aSNathan Lynch 	return ret;
6439327dc0aSNathan Lynch }
6449327dc0aSNathan Lynch 
64537cddc7dSNathan Lynch /*
64637cddc7dSNathan Lynch  * Abort reason code byte 0. We use only the 'Migrating partition' value.
64737cddc7dSNathan Lynch  */
64837cddc7dSNathan Lynch enum vasi_aborting_entity {
64937cddc7dSNathan Lynch 	ORCHESTRATOR        = 1,
65037cddc7dSNathan Lynch 	VSP_SOURCE          = 2,
65137cddc7dSNathan Lynch 	PARTITION_FIRMWARE  = 3,
65237cddc7dSNathan Lynch 	PLATFORM_FIRMWARE   = 4,
65337cddc7dSNathan Lynch 	VSP_TARGET          = 5,
65437cddc7dSNathan Lynch 	MIGRATING_PARTITION = 6,
65537cddc7dSNathan Lynch };
65637cddc7dSNathan Lynch 
65737cddc7dSNathan Lynch static void pseries_cancel_migration(u64 handle, int err)
65837cddc7dSNathan Lynch {
65937cddc7dSNathan Lynch 	u32 reason_code;
66037cddc7dSNathan Lynch 	u32 detail;
66137cddc7dSNathan Lynch 	u8 entity;
66237cddc7dSNathan Lynch 	long hvrc;
66337cddc7dSNathan Lynch 
66437cddc7dSNathan Lynch 	entity = MIGRATING_PARTITION;
66537cddc7dSNathan Lynch 	detail = abs(err) & 0xffffff;
66637cddc7dSNathan Lynch 	reason_code = (entity << 24) | detail;
66737cddc7dSNathan Lynch 
66837cddc7dSNathan Lynch 	hvrc = plpar_hcall_norets(H_VASI_SIGNAL, handle,
66937cddc7dSNathan Lynch 				  H_VASI_SIGNAL_CANCEL, reason_code);
67037cddc7dSNathan Lynch 	if (hvrc)
67137cddc7dSNathan Lynch 		pr_err("H_VASI_SIGNAL error: %ld\n", hvrc);
67237cddc7dSNathan Lynch }
67337cddc7dSNathan Lynch 
674aeca35b9SNathan Lynch static int pseries_suspend(u64 handle)
675aeca35b9SNathan Lynch {
676aeca35b9SNathan Lynch 	const unsigned int max_attempts = 5;
677aeca35b9SNathan Lynch 	unsigned int retry_interval_ms = 1;
678aeca35b9SNathan Lynch 	unsigned int attempt = 1;
679aeca35b9SNathan Lynch 	int ret;
680aeca35b9SNathan Lynch 
681aeca35b9SNathan Lynch 	while (true) {
682e834df6cSNathan Lynch 		struct pseries_suspend_info info;
683aeca35b9SNathan Lynch 		unsigned long vasi_state;
684aeca35b9SNathan Lynch 		int vasi_err;
685aeca35b9SNathan Lynch 
686e834df6cSNathan Lynch 		info = (struct pseries_suspend_info) {
687e834df6cSNathan Lynch 			.counter = ATOMIC_INIT(0),
688274cb1caSNathan Lynch 			.done = false,
689e834df6cSNathan Lynch 		};
690e834df6cSNathan Lynch 
691e834df6cSNathan Lynch 		ret = stop_machine(do_join, &info, cpu_online_mask);
692aeca35b9SNathan Lynch 		if (ret == 0)
693aeca35b9SNathan Lynch 			break;
694aeca35b9SNathan Lynch 		/*
695aeca35b9SNathan Lynch 		 * Encountered an error. If the VASI stream is still
696aeca35b9SNathan Lynch 		 * in Suspending state, it's likely a transient
697aeca35b9SNathan Lynch 		 * condition related to some device in the partition
698aeca35b9SNathan Lynch 		 * and we can retry in the hope that the cause has
699aeca35b9SNathan Lynch 		 * cleared after some delay.
700aeca35b9SNathan Lynch 		 *
701aeca35b9SNathan Lynch 		 * A better design would allow drivers etc to prepare
702aeca35b9SNathan Lynch 		 * for the suspend and avoid conditions which prevent
703aeca35b9SNathan Lynch 		 * the suspend from succeeding. For now, we have this
704aeca35b9SNathan Lynch 		 * mitigation.
705aeca35b9SNathan Lynch 		 */
706aeca35b9SNathan Lynch 		pr_notice("Partition suspend attempt %u of %u error: %d\n",
707aeca35b9SNathan Lynch 			  attempt, max_attempts, ret);
708aeca35b9SNathan Lynch 
709aeca35b9SNathan Lynch 		if (attempt == max_attempts)
710aeca35b9SNathan Lynch 			break;
711aeca35b9SNathan Lynch 
712aeca35b9SNathan Lynch 		vasi_err = poll_vasi_state(handle, &vasi_state);
713aeca35b9SNathan Lynch 		if (vasi_err == 0) {
714aeca35b9SNathan Lynch 			if (vasi_state != H_VASI_SUSPENDING) {
715aeca35b9SNathan Lynch 				pr_notice("VASI state %lu after failed suspend\n",
716aeca35b9SNathan Lynch 					  vasi_state);
717aeca35b9SNathan Lynch 				break;
718aeca35b9SNathan Lynch 			}
719aeca35b9SNathan Lynch 		} else if (vasi_err != -EOPNOTSUPP) {
720aeca35b9SNathan Lynch 			pr_err("VASI state poll error: %d", vasi_err);
721aeca35b9SNathan Lynch 			break;
722aeca35b9SNathan Lynch 		}
723aeca35b9SNathan Lynch 
724aeca35b9SNathan Lynch 		pr_notice("Will retry partition suspend after %u ms\n",
725aeca35b9SNathan Lynch 			  retry_interval_ms);
726aeca35b9SNathan Lynch 
727aeca35b9SNathan Lynch 		msleep(retry_interval_ms);
728aeca35b9SNathan Lynch 		retry_interval_ms *= 10;
729aeca35b9SNathan Lynch 		attempt++;
730aeca35b9SNathan Lynch 	}
731aeca35b9SNathan Lynch 
732aeca35b9SNathan Lynch 	return ret;
733aeca35b9SNathan Lynch }
734aeca35b9SNathan Lynch 
7359327dc0aSNathan Lynch static int pseries_migrate_partition(u64 handle)
7369327dc0aSNathan Lynch {
7379327dc0aSNathan Lynch 	int ret;
738118b1366SLaurent Dufour 	unsigned int factor = 0;
7399327dc0aSNathan Lynch 
740118b1366SLaurent Dufour #ifdef CONFIG_PPC_WATCHDOG
741118b1366SLaurent Dufour 	factor = nmi_wd_lpm_factor;
742118b1366SLaurent Dufour #endif
7439327dc0aSNathan Lynch 	ret = wait_for_vasi_session_suspending(handle);
7449327dc0aSNathan Lynch 	if (ret)
7459327dc0aSNathan Lynch 		return ret;
7469327dc0aSNathan Lynch 
74737e67648SHaren Myneni 	vas_migration_handler(VAS_SUSPEND);
74837e67648SHaren Myneni 
749118b1366SLaurent Dufour 	if (factor)
750118b1366SLaurent Dufour 		watchdog_nmi_set_timeout_pct(factor);
751118b1366SLaurent Dufour 
752aeca35b9SNathan Lynch 	ret = pseries_suspend(handle);
753882c0d17SLaurent Dufour 	if (ret == 0) {
7549327dc0aSNathan Lynch 		post_mobility_fixup();
755882c0d17SLaurent Dufour 		/*
756882c0d17SLaurent Dufour 		 * Wait until the memory transfer is complete, so that the user
757882c0d17SLaurent Dufour 		 * space process returns from the syscall after the transfer is
758882c0d17SLaurent Dufour 		 * complete. This allows the user hooks to be executed at the
759882c0d17SLaurent Dufour 		 * right time.
760882c0d17SLaurent Dufour 		 */
761882c0d17SLaurent Dufour 		wait_for_vasi_session_completed(handle);
762882c0d17SLaurent Dufour 	} else
76337cddc7dSNathan Lynch 		pseries_cancel_migration(handle, ret);
7649327dc0aSNathan Lynch 
765118b1366SLaurent Dufour 	if (factor)
766118b1366SLaurent Dufour 		watchdog_nmi_set_timeout_pct(0);
767118b1366SLaurent Dufour 
76837e67648SHaren Myneni 	vas_migration_handler(VAS_RESUME);
76937e67648SHaren Myneni 
7709327dc0aSNathan Lynch 	return ret;
7719327dc0aSNathan Lynch }
7729327dc0aSNathan Lynch 
7734d756894SNathan Lynch int rtas_syscall_dispatch_ibm_suspend_me(u64 handle)
7744d756894SNathan Lynch {
7754d756894SNathan Lynch 	return pseries_migrate_partition(handle);
7764d756894SNathan Lynch }
7774d756894SNathan Lynch 
7786f428096SGreg Kroah-Hartman static ssize_t migration_store(struct class *class,
7796f428096SGreg Kroah-Hartman 			       struct class_attribute *attr, const char *buf,
7806f428096SGreg Kroah-Hartman 			       size_t count)
781410bccf9SNathan Fontenot {
782410bccf9SNathan Fontenot 	u64 streamid;
783410bccf9SNathan Fontenot 	int rc;
784410bccf9SNathan Fontenot 
7851618bd53SDaniel Walter 	rc = kstrtou64(buf, 0, &streamid);
786410bccf9SNathan Fontenot 	if (rc)
787410bccf9SNathan Fontenot 		return rc;
788410bccf9SNathan Fontenot 
7899327dc0aSNathan Lynch 	rc = pseries_migrate_partition(streamid);
790d9213319SNathan Lynch 	if (rc)
791d9213319SNathan Lynch 		return rc;
792410bccf9SNathan Fontenot 
793410bccf9SNathan Fontenot 	return count;
794410bccf9SNathan Fontenot }
795410bccf9SNathan Fontenot 
796288a298cSTyrel Datwyler /*
797288a298cSTyrel Datwyler  * Used by drmgr to determine the kernel behavior of the migration interface.
798288a298cSTyrel Datwyler  *
799288a298cSTyrel Datwyler  * Version 1: Performs all PAPR requirements for migration including
800288a298cSTyrel Datwyler  *	firmware activation and device tree update.
801288a298cSTyrel Datwyler  */
802288a298cSTyrel Datwyler #define MIGRATION_API_VERSION	1
803288a298cSTyrel Datwyler 
8046f428096SGreg Kroah-Hartman static CLASS_ATTR_WO(migration);
80557ad583fSRussell Currey static CLASS_ATTR_STRING(api_version, 0444, __stringify(MIGRATION_API_VERSION));
806410bccf9SNathan Fontenot 
807410bccf9SNathan Fontenot static int __init mobility_sysfs_init(void)
808410bccf9SNathan Fontenot {
809410bccf9SNathan Fontenot 	int rc;
810410bccf9SNathan Fontenot 
811410bccf9SNathan Fontenot 	mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
812410bccf9SNathan Fontenot 	if (!mobility_kobj)
813410bccf9SNathan Fontenot 		return -ENOMEM;
814410bccf9SNathan Fontenot 
815410bccf9SNathan Fontenot 	rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
816288a298cSTyrel Datwyler 	if (rc)
817494a66f3SNathan Lynch 		pr_err("unable to create migration sysfs file (%d)\n", rc);
818410bccf9SNathan Fontenot 
819288a298cSTyrel Datwyler 	rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
820288a298cSTyrel Datwyler 	if (rc)
821494a66f3SNathan Lynch 		pr_err("unable to create api_version sysfs file (%d)\n", rc);
822288a298cSTyrel Datwyler 
823288a298cSTyrel Datwyler 	return 0;
824410bccf9SNathan Fontenot }
8258e83e905SMichael Ellerman machine_device_initcall(pseries, mobility_sysfs_init);
826