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/sched.h>
15 #include <linux/smp.h>
16 #include <linux/stat.h>
17 #include <linux/completion.h>
18 #include <linux/device.h>
19 #include <linux/delay.h>
20 #include <linux/slab.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)
44 static int mobility_rtas_call(int token, char *buf, s32 scope)
48 spin_lock(&rtas_data_buf_lock);
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);
54 spin_unlock(&rtas_data_buf_lock);
58 static int delete_dt_node(__be32 phandle)
60 struct device_node *dn;
62 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
66 dlpar_detach_node(dn);
71 static int update_dt_property(struct device_node *dn, struct property **prop,
72 const char *name, u32 vd, char *value)
74 struct property *new_prop = *prop;
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.
81 * A negative value is also the two's compliment of the actual value.
83 if (vd & 0x80000000) {
89 /* partial property fixup */
90 char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
94 memcpy(new_data, new_prop->value, new_prop->length);
95 memcpy(new_data + new_prop->length, value, vd);
97 kfree(new_prop->value);
98 new_prop->value = new_data;
99 new_prop->length += vd;
101 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
105 new_prop->name = kstrdup(name, GFP_KERNEL);
106 if (!new_prop->name) {
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);
119 memcpy(new_prop->value, value, vd);
124 of_update_property(dn, new_prop);
131 static int update_dt_node(__be32 phandle, s32 scope)
133 struct update_props_workarea *upwa;
134 struct device_node *dn;
135 struct property *prop = NULL;
139 int update_properties_token;
143 update_properties_token = rtas_token("ibm,update-properties");
144 if (update_properties_token == RTAS_UNKNOWN_SERVICE)
147 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
151 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
157 upwa = (struct update_props_workarea *)&rtas_buf[0];
158 upwa->phandle = phandle;
161 rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
166 prop_data = rtas_buf + sizeof(*upwa);
167 nprops = be32_to_cpu(upwa->nprops);
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.
174 if (*prop_data == 0) {
176 vd = be32_to_cpu(*(__be32 *)prop_data);
177 prop_data += vd + sizeof(vd);
181 for (i = 0; i < nprops; i++) {
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);
191 /* name only property, nothing to do */
195 prop = of_find_property(dn, prop_name, NULL);
196 of_remove_property(dn, prop);
201 rc = update_dt_property(dn, &prop, prop_name,
204 printk(KERN_ERR "Could not update %s"
205 " property\n", prop_name);
215 } while (rtas_rc == 1);
222 static int add_dt_node(__be32 parent_phandle, __be32 drc_index)
224 struct device_node *dn;
225 struct device_node *parent_dn;
228 parent_dn = of_find_node_by_phandle(be32_to_cpu(parent_phandle));
232 dn = dlpar_configure_connector(drc_index, parent_dn);
234 of_node_put(parent_dn);
238 rc = dlpar_attach_node(dn);
240 dlpar_free_cc_nodes(dn);
242 of_node_put(parent_dn);
246 int pseries_devicetree_update(s32 scope)
250 int update_nodes_token;
253 update_nodes_token = rtas_token("ibm,update-nodes");
254 if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
257 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
262 rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
266 data = (__be32 *)rtas_buf + 4;
267 while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
269 u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
270 u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
274 for (i = 0; i < node_count; i++) {
275 __be32 phandle = *data++;
280 delete_dt_node(phandle);
283 update_dt_node(phandle, scope);
287 add_dt_node(phandle, drc_index);
302 void post_mobility_fixup(void)
305 int activate_fw_token;
307 activate_fw_token = rtas_token("ibm,activate-firmware");
308 if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
309 printk(KERN_ERR "Could not make post-mobility "
310 "activate-fw call.\n");
315 rc = rtas_call(activate_fw_token, 0, 1, NULL);
316 } while (rtas_busy_delay(rc));
319 printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
321 rc = pseries_devicetree_update(MIGRATION_SCOPE);
323 printk(KERN_ERR "Post-mobility device tree update "
326 /* Possibly switch to a new RFI flush type */
327 pseries_setup_rfi_flush();
332 static ssize_t migrate_store(struct class *class, struct class_attribute *attr,
333 const char *buf, size_t count)
338 rc = kstrtou64(buf, 0, &streamid);
343 rc = rtas_ibm_suspend_me(streamid);
346 } while (rc == -EAGAIN);
351 post_mobility_fixup();
356 * Used by drmgr to determine the kernel behavior of the migration interface.
358 * Version 1: Performs all PAPR requirements for migration including
359 * firmware activation and device tree update.
361 #define MIGRATION_API_VERSION 1
363 static CLASS_ATTR(migration, S_IWUSR, NULL, migrate_store);
364 static CLASS_ATTR_STRING(api_version, S_IRUGO, __stringify(MIGRATION_API_VERSION));
366 static int __init mobility_sysfs_init(void)
370 mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
374 rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
376 pr_err("mobility: unable to create migration sysfs file (%d)\n", rc);
378 rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
380 pr_err("mobility: unable to create api_version sysfs file (%d)\n", rc);
384 machine_device_initcall(pseries, mobility_sysfs_init);