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/cpu.h>
13 #include <linux/kernel.h>
14 #include <linux/kobject.h>
15 #include <linux/sched.h>
16 #include <linux/smp.h>
17 #include <linux/stat.h>
18 #include <linux/completion.h>
19 #include <linux/device.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/stringify.h>
24 #include <asm/machdep.h>
27 #include "../../kernel/cacheinfo.h"
29 static struct kobject *mobility_kobj;
31 struct update_props_workarea {
38 #define NODE_ACTION_MASK 0xff000000
39 #define NODE_COUNT_MASK 0x00ffffff
41 #define DELETE_DT_NODE 0x01000000
42 #define UPDATE_DT_NODE 0x02000000
43 #define ADD_DT_NODE 0x03000000
45 #define MIGRATION_SCOPE (1)
48 static int mobility_rtas_call(int token, char *buf, s32 scope)
52 spin_lock(&rtas_data_buf_lock);
54 memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
55 rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
56 memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
58 spin_unlock(&rtas_data_buf_lock);
62 static int delete_dt_node(__be32 phandle)
64 struct device_node *dn;
66 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
70 dlpar_detach_node(dn);
75 static int update_dt_property(struct device_node *dn, struct property **prop,
76 const char *name, u32 vd, char *value)
78 struct property *new_prop = *prop;
81 /* A negative 'vd' value indicates that only part of the new property
82 * value is contained in the buffer and we need to call
83 * ibm,update-properties again to get the rest of the value.
85 * A negative value is also the two's compliment of the actual value.
87 if (vd & 0x80000000) {
93 /* partial property fixup */
94 char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
98 memcpy(new_data, new_prop->value, new_prop->length);
99 memcpy(new_data + new_prop->length, value, vd);
101 kfree(new_prop->value);
102 new_prop->value = new_data;
103 new_prop->length += vd;
105 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
109 new_prop->name = kstrdup(name, GFP_KERNEL);
110 if (!new_prop->name) {
115 new_prop->length = vd;
116 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
117 if (!new_prop->value) {
118 kfree(new_prop->name);
123 memcpy(new_prop->value, value, vd);
128 of_update_property(dn, new_prop);
135 static int update_dt_node(__be32 phandle, s32 scope)
137 struct update_props_workarea *upwa;
138 struct device_node *dn;
139 struct property *prop = NULL;
143 int update_properties_token;
147 update_properties_token = rtas_token("ibm,update-properties");
148 if (update_properties_token == RTAS_UNKNOWN_SERVICE)
151 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
155 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
161 upwa = (struct update_props_workarea *)&rtas_buf[0];
162 upwa->phandle = phandle;
165 rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
170 prop_data = rtas_buf + sizeof(*upwa);
171 nprops = be32_to_cpu(upwa->nprops);
173 /* On the first call to ibm,update-properties for a node the
174 * the first property value descriptor contains an empty
175 * property name, the property value length encoded as u32,
176 * and the property value is the node path being updated.
178 if (*prop_data == 0) {
180 vd = be32_to_cpu(*(__be32 *)prop_data);
181 prop_data += vd + sizeof(vd);
185 for (i = 0; i < nprops; i++) {
188 prop_name = prop_data;
189 prop_data += strlen(prop_name) + 1;
190 vd = be32_to_cpu(*(__be32 *)prop_data);
191 prop_data += sizeof(vd);
195 /* name only property, nothing to do */
199 of_remove_property(dn, of_find_property(dn,
205 rc = update_dt_property(dn, &prop, prop_name,
208 printk(KERN_ERR "Could not update %s"
209 " property\n", prop_name);
219 } while (rtas_rc == 1);
226 static int add_dt_node(__be32 parent_phandle, __be32 drc_index)
228 struct device_node *dn;
229 struct device_node *parent_dn;
232 parent_dn = of_find_node_by_phandle(be32_to_cpu(parent_phandle));
236 dn = dlpar_configure_connector(drc_index, parent_dn);
238 of_node_put(parent_dn);
242 rc = dlpar_attach_node(dn, parent_dn);
244 dlpar_free_cc_nodes(dn);
246 of_node_put(parent_dn);
250 static void prrn_update_node(__be32 phandle)
252 struct pseries_hp_errorlog *hp_elog;
253 struct device_node *dn;
256 * If a node is found from a the given phandle, the phandle does not
257 * represent the drc index of an LMB and we can ignore.
259 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
265 hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
269 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
270 hp_elog->action = PSERIES_HP_ELOG_ACTION_READD;
271 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
272 hp_elog->_drc_u.drc_index = phandle;
274 queue_hotplug_event(hp_elog, NULL, NULL);
279 int pseries_devicetree_update(s32 scope)
283 int update_nodes_token;
286 update_nodes_token = rtas_token("ibm,update-nodes");
287 if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
290 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
295 rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
299 data = (__be32 *)rtas_buf + 4;
300 while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
302 u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
303 u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
307 for (i = 0; i < node_count; i++) {
308 __be32 phandle = *data++;
313 delete_dt_node(phandle);
316 update_dt_node(phandle, scope);
318 if (scope == PRRN_SCOPE)
319 prrn_update_node(phandle);
324 add_dt_node(phandle, drc_index);
339 void post_mobility_fixup(void)
342 int activate_fw_token;
344 activate_fw_token = rtas_token("ibm,activate-firmware");
345 if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
346 printk(KERN_ERR "Could not make post-mobility "
347 "activate-fw call.\n");
352 rc = rtas_call(activate_fw_token, 0, 1, NULL);
353 } while (rtas_busy_delay(rc));
356 printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
359 * We don't want CPUs to go online/offline while the device
360 * tree is being updated.
365 * It's common for the destination firmware to replace cache
366 * nodes. Release all of the cacheinfo hierarchy's references
367 * before updating the device tree.
369 cacheinfo_teardown();
371 rc = pseries_devicetree_update(MIGRATION_SCOPE);
373 printk(KERN_ERR "Post-mobility device tree update "
380 /* Possibly switch to a new RFI flush type */
381 pseries_setup_rfi_flush();
386 static ssize_t migration_store(struct class *class,
387 struct class_attribute *attr, const char *buf,
393 rc = kstrtou64(buf, 0, &streamid);
398 rc = rtas_ibm_suspend_me(streamid);
401 } while (rc == -EAGAIN);
406 post_mobility_fixup();
411 * Used by drmgr to determine the kernel behavior of the migration interface.
413 * Version 1: Performs all PAPR requirements for migration including
414 * firmware activation and device tree update.
416 #define MIGRATION_API_VERSION 1
418 static CLASS_ATTR_WO(migration);
419 static CLASS_ATTR_STRING(api_version, 0444, __stringify(MIGRATION_API_VERSION));
421 static int __init mobility_sysfs_init(void)
425 mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
429 rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
431 pr_err("mobility: unable to create migration sysfs file (%d)\n", rc);
433 rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
435 pr_err("mobility: unable to create api_version sysfs file (%d)\n", rc);
439 machine_device_initcall(pseries, mobility_sysfs_init);