xref: /linux/arch/powerpc/platforms/pseries/mobility.c (revision 192f0f8e9db7efe4ac98d47f5fa4334e43c1204d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Support for Partition Mobility/Migration
4  *
5  * Copyright (C) 2010 Nathan Fontenot
6  * Copyright (C) 2010 IBM Corporation
7  */
8 
9 #include <linux/cpu.h>
10 #include <linux/kernel.h>
11 #include <linux/kobject.h>
12 #include <linux/smp.h>
13 #include <linux/stat.h>
14 #include <linux/completion.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/stringify.h>
19 
20 #include <asm/machdep.h>
21 #include <asm/rtas.h>
22 #include "pseries.h"
23 #include "../../kernel/cacheinfo.h"
24 
25 static struct kobject *mobility_kobj;
26 
27 struct update_props_workarea {
28 	__be32 phandle;
29 	__be32 state;
30 	__be64 reserved;
31 	__be32 nprops;
32 } __packed;
33 
34 #define NODE_ACTION_MASK	0xff000000
35 #define NODE_COUNT_MASK		0x00ffffff
36 
37 #define DELETE_DT_NODE	0x01000000
38 #define UPDATE_DT_NODE	0x02000000
39 #define ADD_DT_NODE	0x03000000
40 
41 #define MIGRATION_SCOPE	(1)
42 #define PRRN_SCOPE -2
43 
44 static int mobility_rtas_call(int token, char *buf, s32 scope)
45 {
46 	int rc;
47 
48 	spin_lock(&rtas_data_buf_lock);
49 
50 	memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
51 	rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
52 	memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
53 
54 	spin_unlock(&rtas_data_buf_lock);
55 	return rc;
56 }
57 
58 static int delete_dt_node(__be32 phandle)
59 {
60 	struct device_node *dn;
61 
62 	dn = of_find_node_by_phandle(be32_to_cpu(phandle));
63 	if (!dn)
64 		return -ENOENT;
65 
66 	dlpar_detach_node(dn);
67 	of_node_put(dn);
68 	return 0;
69 }
70 
71 static int update_dt_property(struct device_node *dn, struct property **prop,
72 			      const char *name, u32 vd, char *value)
73 {
74 	struct property *new_prop = *prop;
75 	int more = 0;
76 
77 	/* A negative 'vd' value indicates that only part of the new property
78 	 * value is contained in the buffer and we need to call
79 	 * ibm,update-properties again to get the rest of the value.
80 	 *
81 	 * A negative value is also the two's compliment of the actual value.
82 	 */
83 	if (vd & 0x80000000) {
84 		vd = ~vd + 1;
85 		more = 1;
86 	}
87 
88 	if (new_prop) {
89 		/* partial property fixup */
90 		char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
91 		if (!new_data)
92 			return -ENOMEM;
93 
94 		memcpy(new_data, new_prop->value, new_prop->length);
95 		memcpy(new_data + new_prop->length, value, vd);
96 
97 		kfree(new_prop->value);
98 		new_prop->value = new_data;
99 		new_prop->length += vd;
100 	} else {
101 		new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
102 		if (!new_prop)
103 			return -ENOMEM;
104 
105 		new_prop->name = kstrdup(name, GFP_KERNEL);
106 		if (!new_prop->name) {
107 			kfree(new_prop);
108 			return -ENOMEM;
109 		}
110 
111 		new_prop->length = vd;
112 		new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
113 		if (!new_prop->value) {
114 			kfree(new_prop->name);
115 			kfree(new_prop);
116 			return -ENOMEM;
117 		}
118 
119 		memcpy(new_prop->value, value, vd);
120 		*prop = new_prop;
121 	}
122 
123 	if (!more) {
124 		of_update_property(dn, new_prop);
125 		*prop = NULL;
126 	}
127 
128 	return 0;
129 }
130 
131 static int update_dt_node(__be32 phandle, s32 scope)
132 {
133 	struct update_props_workarea *upwa;
134 	struct device_node *dn;
135 	struct property *prop = NULL;
136 	int i, rc, rtas_rc;
137 	char *prop_data;
138 	char *rtas_buf;
139 	int update_properties_token;
140 	u32 nprops;
141 	u32 vd;
142 
143 	update_properties_token = rtas_token("ibm,update-properties");
144 	if (update_properties_token == RTAS_UNKNOWN_SERVICE)
145 		return -EINVAL;
146 
147 	rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
148 	if (!rtas_buf)
149 		return -ENOMEM;
150 
151 	dn = of_find_node_by_phandle(be32_to_cpu(phandle));
152 	if (!dn) {
153 		kfree(rtas_buf);
154 		return -ENOENT;
155 	}
156 
157 	upwa = (struct update_props_workarea *)&rtas_buf[0];
158 	upwa->phandle = phandle;
159 
160 	do {
161 		rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
162 					scope);
163 		if (rtas_rc < 0)
164 			break;
165 
166 		prop_data = rtas_buf + sizeof(*upwa);
167 		nprops = be32_to_cpu(upwa->nprops);
168 
169 		/* On the first call to ibm,update-properties for a node the
170 		 * the first property value descriptor contains an empty
171 		 * property name, the property value length encoded as u32,
172 		 * and the property value is the node path being updated.
173 		 */
174 		if (*prop_data == 0) {
175 			prop_data++;
176 			vd = be32_to_cpu(*(__be32 *)prop_data);
177 			prop_data += vd + sizeof(vd);
178 			nprops--;
179 		}
180 
181 		for (i = 0; i < nprops; i++) {
182 			char *prop_name;
183 
184 			prop_name = prop_data;
185 			prop_data += strlen(prop_name) + 1;
186 			vd = be32_to_cpu(*(__be32 *)prop_data);
187 			prop_data += sizeof(vd);
188 
189 			switch (vd) {
190 			case 0x00000000:
191 				/* name only property, nothing to do */
192 				break;
193 
194 			case 0x80000000:
195 				of_remove_property(dn, of_find_property(dn,
196 							prop_name, NULL));
197 				prop = NULL;
198 				break;
199 
200 			default:
201 				rc = update_dt_property(dn, &prop, prop_name,
202 							vd, prop_data);
203 				if (rc) {
204 					printk(KERN_ERR "Could not update %s"
205 					       " property\n", prop_name);
206 				}
207 
208 				prop_data += vd;
209 			}
210 		}
211 	} while (rtas_rc == 1);
212 
213 	of_node_put(dn);
214 	kfree(rtas_buf);
215 	return 0;
216 }
217 
218 static int add_dt_node(__be32 parent_phandle, __be32 drc_index)
219 {
220 	struct device_node *dn;
221 	struct device_node *parent_dn;
222 	int rc;
223 
224 	parent_dn = of_find_node_by_phandle(be32_to_cpu(parent_phandle));
225 	if (!parent_dn)
226 		return -ENOENT;
227 
228 	dn = dlpar_configure_connector(drc_index, parent_dn);
229 	if (!dn) {
230 		of_node_put(parent_dn);
231 		return -ENOENT;
232 	}
233 
234 	rc = dlpar_attach_node(dn, parent_dn);
235 	if (rc)
236 		dlpar_free_cc_nodes(dn);
237 
238 	of_node_put(parent_dn);
239 	return rc;
240 }
241 
242 static void prrn_update_node(__be32 phandle)
243 {
244 	struct pseries_hp_errorlog hp_elog;
245 	struct device_node *dn;
246 
247 	/*
248 	 * If a node is found from a the given phandle, the phandle does not
249 	 * represent the drc index of an LMB and we can ignore.
250 	 */
251 	dn = of_find_node_by_phandle(be32_to_cpu(phandle));
252 	if (dn) {
253 		of_node_put(dn);
254 		return;
255 	}
256 
257 	hp_elog.resource = PSERIES_HP_ELOG_RESOURCE_MEM;
258 	hp_elog.action = PSERIES_HP_ELOG_ACTION_READD;
259 	hp_elog.id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
260 	hp_elog._drc_u.drc_index = phandle;
261 
262 	handle_dlpar_errorlog(&hp_elog);
263 }
264 
265 int pseries_devicetree_update(s32 scope)
266 {
267 	char *rtas_buf;
268 	__be32 *data;
269 	int update_nodes_token;
270 	int rc;
271 
272 	update_nodes_token = rtas_token("ibm,update-nodes");
273 	if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
274 		return -EINVAL;
275 
276 	rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
277 	if (!rtas_buf)
278 		return -ENOMEM;
279 
280 	do {
281 		rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
282 		if (rc && rc != 1)
283 			break;
284 
285 		data = (__be32 *)rtas_buf + 4;
286 		while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
287 			int i;
288 			u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
289 			u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
290 
291 			data++;
292 
293 			for (i = 0; i < node_count; i++) {
294 				__be32 phandle = *data++;
295 				__be32 drc_index;
296 
297 				switch (action) {
298 				case DELETE_DT_NODE:
299 					delete_dt_node(phandle);
300 					break;
301 				case UPDATE_DT_NODE:
302 					update_dt_node(phandle, scope);
303 
304 					if (scope == PRRN_SCOPE)
305 						prrn_update_node(phandle);
306 
307 					break;
308 				case ADD_DT_NODE:
309 					drc_index = *data++;
310 					add_dt_node(phandle, drc_index);
311 					break;
312 				}
313 			}
314 		}
315 	} while (rc == 1);
316 
317 	kfree(rtas_buf);
318 	return rc;
319 }
320 
321 void post_mobility_fixup(void)
322 {
323 	int rc;
324 	int activate_fw_token;
325 
326 	activate_fw_token = rtas_token("ibm,activate-firmware");
327 	if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
328 		printk(KERN_ERR "Could not make post-mobility "
329 		       "activate-fw call.\n");
330 		return;
331 	}
332 
333 	do {
334 		rc = rtas_call(activate_fw_token, 0, 1, NULL);
335 	} while (rtas_busy_delay(rc));
336 
337 	if (rc)
338 		printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
339 
340 	/*
341 	 * We don't want CPUs to go online/offline while the device
342 	 * tree is being updated.
343 	 */
344 	cpus_read_lock();
345 
346 	/*
347 	 * It's common for the destination firmware to replace cache
348 	 * nodes.  Release all of the cacheinfo hierarchy's references
349 	 * before updating the device tree.
350 	 */
351 	cacheinfo_teardown();
352 
353 	rc = pseries_devicetree_update(MIGRATION_SCOPE);
354 	if (rc)
355 		printk(KERN_ERR "Post-mobility device tree update "
356 			"failed: %d\n", rc);
357 
358 	cacheinfo_rebuild();
359 
360 	cpus_read_unlock();
361 
362 	/* Possibly switch to a new RFI flush type */
363 	pseries_setup_rfi_flush();
364 
365 	return;
366 }
367 
368 static ssize_t migration_store(struct class *class,
369 			       struct class_attribute *attr, const char *buf,
370 			       size_t count)
371 {
372 	u64 streamid;
373 	int rc;
374 
375 	rc = kstrtou64(buf, 0, &streamid);
376 	if (rc)
377 		return rc;
378 
379 	stop_topology_update();
380 
381 	do {
382 		rc = rtas_ibm_suspend_me(streamid);
383 		if (rc == -EAGAIN)
384 			ssleep(1);
385 	} while (rc == -EAGAIN);
386 
387 	if (rc)
388 		return rc;
389 
390 	post_mobility_fixup();
391 
392 	start_topology_update();
393 
394 	return count;
395 }
396 
397 /*
398  * Used by drmgr to determine the kernel behavior of the migration interface.
399  *
400  * Version 1: Performs all PAPR requirements for migration including
401  *	firmware activation and device tree update.
402  */
403 #define MIGRATION_API_VERSION	1
404 
405 static CLASS_ATTR_WO(migration);
406 static CLASS_ATTR_STRING(api_version, 0444, __stringify(MIGRATION_API_VERSION));
407 
408 static int __init mobility_sysfs_init(void)
409 {
410 	int rc;
411 
412 	mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
413 	if (!mobility_kobj)
414 		return -ENOMEM;
415 
416 	rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
417 	if (rc)
418 		pr_err("mobility: unable to create migration sysfs file (%d)\n", rc);
419 
420 	rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
421 	if (rc)
422 		pr_err("mobility: unable to create api_version sysfs file (%d)\n", rc);
423 
424 	return 0;
425 }
426 machine_device_initcall(pseries, mobility_sysfs_init);
427