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>
23 #include <asm/machdep.h>
26 #include "../../kernel/cacheinfo.h"
28 static struct kobject *mobility_kobj;
30 struct update_props_workarea {
37 #define NODE_ACTION_MASK 0xff000000
38 #define NODE_COUNT_MASK 0x00ffffff
40 #define DELETE_DT_NODE 0x01000000
41 #define UPDATE_DT_NODE 0x02000000
42 #define ADD_DT_NODE 0x03000000
44 #define MIGRATION_SCOPE (1)
47 static int mobility_rtas_call(int token, char *buf, s32 scope)
51 spin_lock(&rtas_data_buf_lock);
53 memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
54 rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
55 memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
57 spin_unlock(&rtas_data_buf_lock);
61 static int delete_dt_node(__be32 phandle)
63 struct device_node *dn;
65 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
69 dlpar_detach_node(dn);
74 static int update_dt_property(struct device_node *dn, struct property **prop,
75 const char *name, u32 vd, char *value)
77 struct property *new_prop = *prop;
80 /* A negative 'vd' value indicates that only part of the new property
81 * value is contained in the buffer and we need to call
82 * ibm,update-properties again to get the rest of the value.
84 * A negative value is also the two's compliment of the actual value.
86 if (vd & 0x80000000) {
92 /* partial property fixup */
93 char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
97 memcpy(new_data, new_prop->value, new_prop->length);
98 memcpy(new_data + new_prop->length, value, vd);
100 kfree(new_prop->value);
101 new_prop->value = new_data;
102 new_prop->length += vd;
104 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
108 new_prop->name = kstrdup(name, GFP_KERNEL);
109 if (!new_prop->name) {
114 new_prop->length = vd;
115 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
116 if (!new_prop->value) {
117 kfree(new_prop->name);
122 memcpy(new_prop->value, value, vd);
127 of_update_property(dn, new_prop);
134 static int update_dt_node(__be32 phandle, s32 scope)
136 struct update_props_workarea *upwa;
137 struct device_node *dn;
138 struct property *prop = NULL;
142 int update_properties_token;
146 update_properties_token = rtas_token("ibm,update-properties");
147 if (update_properties_token == RTAS_UNKNOWN_SERVICE)
150 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
154 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
160 upwa = (struct update_props_workarea *)&rtas_buf[0];
161 upwa->phandle = phandle;
164 rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
169 prop_data = rtas_buf + sizeof(*upwa);
170 nprops = be32_to_cpu(upwa->nprops);
172 /* On the first call to ibm,update-properties for a node the
173 * the first property value descriptor contains an empty
174 * property name, the property value length encoded as u32,
175 * and the property value is the node path being updated.
177 if (*prop_data == 0) {
179 vd = be32_to_cpu(*(__be32 *)prop_data);
180 prop_data += vd + sizeof(vd);
184 for (i = 0; i < nprops; i++) {
187 prop_name = prop_data;
188 prop_data += strlen(prop_name) + 1;
189 vd = be32_to_cpu(*(__be32 *)prop_data);
190 prop_data += sizeof(vd);
194 /* name only property, nothing to do */
198 of_remove_property(dn, of_find_property(dn,
204 rc = update_dt_property(dn, &prop, prop_name,
207 printk(KERN_ERR "Could not update %s"
208 " property\n", prop_name);
218 } while (rtas_rc == 1);
225 static int add_dt_node(__be32 parent_phandle, __be32 drc_index)
227 struct device_node *dn;
228 struct device_node *parent_dn;
231 parent_dn = of_find_node_by_phandle(be32_to_cpu(parent_phandle));
235 dn = dlpar_configure_connector(drc_index, parent_dn);
237 of_node_put(parent_dn);
241 rc = dlpar_attach_node(dn, parent_dn);
243 dlpar_free_cc_nodes(dn);
245 of_node_put(parent_dn);
249 static void prrn_update_node(__be32 phandle)
251 struct pseries_hp_errorlog *hp_elog;
252 struct device_node *dn;
255 * If a node is found from a the given phandle, the phandle does not
256 * represent the drc index of an LMB and we can ignore.
258 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
264 hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
268 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
269 hp_elog->action = PSERIES_HP_ELOG_ACTION_READD;
270 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
271 hp_elog->_drc_u.drc_index = phandle;
273 queue_hotplug_event(hp_elog, NULL, NULL);
278 int pseries_devicetree_update(s32 scope)
282 int update_nodes_token;
285 update_nodes_token = rtas_token("ibm,update-nodes");
286 if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
289 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
294 rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
298 data = (__be32 *)rtas_buf + 4;
299 while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
301 u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
302 u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
306 for (i = 0; i < node_count; i++) {
307 __be32 phandle = *data++;
312 delete_dt_node(phandle);
315 update_dt_node(phandle, scope);
317 if (scope == PRRN_SCOPE)
318 prrn_update_node(phandle);
323 add_dt_node(phandle, drc_index);
338 void post_mobility_fixup(void)
341 int activate_fw_token;
343 activate_fw_token = rtas_token("ibm,activate-firmware");
344 if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
345 printk(KERN_ERR "Could not make post-mobility "
346 "activate-fw call.\n");
351 rc = rtas_call(activate_fw_token, 0, 1, NULL);
352 } while (rtas_busy_delay(rc));
355 printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
358 * We don't want CPUs to go online/offline while the device
359 * tree is being updated.
364 * It's common for the destination firmware to replace cache
365 * nodes. Release all of the cacheinfo hierarchy's references
366 * before updating the device tree.
368 cacheinfo_teardown();
370 rc = pseries_devicetree_update(MIGRATION_SCOPE);
372 printk(KERN_ERR "Post-mobility device tree update "
379 /* Possibly switch to a new RFI flush type */
380 pseries_setup_rfi_flush();
385 static ssize_t migration_store(struct class *class,
386 struct class_attribute *attr, const char *buf,
392 rc = kstrtou64(buf, 0, &streamid);
397 rc = rtas_ibm_suspend_me(streamid);
400 } while (rc == -EAGAIN);
405 post_mobility_fixup();
410 * Used by drmgr to determine the kernel behavior of the migration interface.
412 * Version 1: Performs all PAPR requirements for migration including
413 * firmware activation and device tree update.
415 #define MIGRATION_API_VERSION 1
417 static CLASS_ATTR_WO(migration);
418 static CLASS_ATTR_STRING(api_version, S_IRUGO, __stringify(MIGRATION_API_VERSION));
420 static int __init mobility_sysfs_init(void)
424 mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
428 rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
430 pr_err("mobility: unable to create migration sysfs file (%d)\n", rc);
432 rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
434 pr_err("mobility: unable to create api_version sysfs file (%d)\n", rc);
438 machine_device_initcall(pseries, mobility_sysfs_init);