2 * Support for Partition Mobility/Migration
4 * Copyright (C) 2010 Nathan Fontenot
5 * Copyright (C) 2010 IBM Corporation
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/kobject.h>
14 #include <linux/smp.h>
15 #include <linux/stat.h>
16 #include <linux/completion.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/stringify.h>
22 #include <asm/machdep.h>
26 static struct kobject *mobility_kobj;
28 struct update_props_workarea {
35 #define NODE_ACTION_MASK 0xff000000
36 #define NODE_COUNT_MASK 0x00ffffff
38 #define DELETE_DT_NODE 0x01000000
39 #define UPDATE_DT_NODE 0x02000000
40 #define ADD_DT_NODE 0x03000000
42 #define MIGRATION_SCOPE (1)
45 static int mobility_rtas_call(int token, char *buf, s32 scope)
49 spin_lock(&rtas_data_buf_lock);
51 memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
52 rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
53 memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
55 spin_unlock(&rtas_data_buf_lock);
59 static int delete_dt_node(__be32 phandle)
61 struct device_node *dn;
63 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
67 dlpar_detach_node(dn);
72 static int update_dt_property(struct device_node *dn, struct property **prop,
73 const char *name, u32 vd, char *value)
75 struct property *new_prop = *prop;
78 /* A negative 'vd' value indicates that only part of the new property
79 * value is contained in the buffer and we need to call
80 * ibm,update-properties again to get the rest of the value.
82 * A negative value is also the two's compliment of the actual value.
84 if (vd & 0x80000000) {
90 /* partial property fixup */
91 char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
95 memcpy(new_data, new_prop->value, new_prop->length);
96 memcpy(new_data + new_prop->length, value, vd);
98 kfree(new_prop->value);
99 new_prop->value = new_data;
100 new_prop->length += vd;
102 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
106 new_prop->name = kstrdup(name, GFP_KERNEL);
107 if (!new_prop->name) {
112 new_prop->length = vd;
113 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
114 if (!new_prop->value) {
115 kfree(new_prop->name);
120 memcpy(new_prop->value, value, vd);
125 of_update_property(dn, new_prop);
132 static int update_dt_node(__be32 phandle, s32 scope)
134 struct update_props_workarea *upwa;
135 struct device_node *dn;
136 struct property *prop = NULL;
140 int update_properties_token;
144 update_properties_token = rtas_token("ibm,update-properties");
145 if (update_properties_token == RTAS_UNKNOWN_SERVICE)
148 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
152 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
158 upwa = (struct update_props_workarea *)&rtas_buf[0];
159 upwa->phandle = phandle;
162 rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
167 prop_data = rtas_buf + sizeof(*upwa);
168 nprops = be32_to_cpu(upwa->nprops);
170 /* On the first call to ibm,update-properties for a node the
171 * the first property value descriptor contains an empty
172 * property name, the property value length encoded as u32,
173 * and the property value is the node path being updated.
175 if (*prop_data == 0) {
177 vd = be32_to_cpu(*(__be32 *)prop_data);
178 prop_data += vd + sizeof(vd);
182 for (i = 0; i < nprops; i++) {
185 prop_name = prop_data;
186 prop_data += strlen(prop_name) + 1;
187 vd = be32_to_cpu(*(__be32 *)prop_data);
188 prop_data += sizeof(vd);
192 /* name only property, nothing to do */
196 of_remove_property(dn, of_find_property(dn,
202 rc = update_dt_property(dn, &prop, prop_name,
205 printk(KERN_ERR "Could not update %s"
206 " property\n", prop_name);
212 } while (rtas_rc == 1);
219 static int add_dt_node(__be32 parent_phandle, __be32 drc_index)
221 struct device_node *dn;
222 struct device_node *parent_dn;
225 parent_dn = of_find_node_by_phandle(be32_to_cpu(parent_phandle));
229 dn = dlpar_configure_connector(drc_index, parent_dn);
231 of_node_put(parent_dn);
235 rc = dlpar_attach_node(dn, parent_dn);
237 dlpar_free_cc_nodes(dn);
239 of_node_put(parent_dn);
243 static void prrn_update_node(__be32 phandle)
245 struct pseries_hp_errorlog hp_elog;
246 struct device_node *dn;
249 * If a node is found from a the given phandle, the phandle does not
250 * represent the drc index of an LMB and we can ignore.
252 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
258 hp_elog.resource = PSERIES_HP_ELOG_RESOURCE_MEM;
259 hp_elog.action = PSERIES_HP_ELOG_ACTION_READD;
260 hp_elog.id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
261 hp_elog._drc_u.drc_index = phandle;
263 handle_dlpar_errorlog(&hp_elog);
266 int pseries_devicetree_update(s32 scope)
270 int update_nodes_token;
273 update_nodes_token = rtas_token("ibm,update-nodes");
274 if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
277 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
282 rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
286 data = (__be32 *)rtas_buf + 4;
287 while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
289 u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
290 u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
294 for (i = 0; i < node_count; i++) {
295 __be32 phandle = *data++;
300 delete_dt_node(phandle);
303 update_dt_node(phandle, scope);
305 if (scope == PRRN_SCOPE)
306 prrn_update_node(phandle);
311 add_dt_node(phandle, drc_index);
322 void post_mobility_fixup(void)
325 int activate_fw_token;
327 activate_fw_token = rtas_token("ibm,activate-firmware");
328 if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
329 printk(KERN_ERR "Could not make post-mobility "
330 "activate-fw call.\n");
335 rc = rtas_call(activate_fw_token, 0, 1, NULL);
336 } while (rtas_busy_delay(rc));
339 printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
341 rc = pseries_devicetree_update(MIGRATION_SCOPE);
343 printk(KERN_ERR "Post-mobility device tree update "
346 /* Possibly switch to a new RFI flush type */
347 pseries_setup_rfi_flush();
352 static ssize_t migration_store(struct class *class,
353 struct class_attribute *attr, const char *buf,
359 rc = kstrtou64(buf, 0, &streamid);
363 stop_topology_update();
366 rc = rtas_ibm_suspend_me(streamid);
369 } while (rc == -EAGAIN);
374 post_mobility_fixup();
376 start_topology_update();
382 * Used by drmgr to determine the kernel behavior of the migration interface.
384 * Version 1: Performs all PAPR requirements for migration including
385 * firmware activation and device tree update.
387 #define MIGRATION_API_VERSION 1
389 static CLASS_ATTR_WO(migration);
390 static CLASS_ATTR_STRING(api_version, 0444, __stringify(MIGRATION_API_VERSION));
392 static int __init mobility_sysfs_init(void)
396 mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
400 rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
402 pr_err("mobility: unable to create migration sysfs file (%d)\n", rc);
404 rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
406 pr_err("mobility: unable to create api_version sysfs file (%d)\n", rc);
410 machine_device_initcall(pseries, mobility_sysfs_init);