1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for Partition Mobility/Migration
5 * Copyright (C) 2010 Nathan Fontenot
6 * Copyright (C) 2010 IBM Corporation
10 #define pr_fmt(fmt) "mobility: " fmt
12 #include <linux/cpu.h>
13 #include <linux/kernel.h>
14 #include <linux/kobject.h>
15 #include <linux/nmi.h>
16 #include <linux/sched.h>
17 #include <linux/smp.h>
18 #include <linux/stat.h>
19 #include <linux/stop_machine.h>
20 #include <linux/completion.h>
21 #include <linux/device.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/stringify.h>
26 #include <asm/machdep.h>
29 #include "vas.h" /* vas_migration_handler() */
30 #include "../../kernel/cacheinfo.h"
32 static struct kobject *mobility_kobj;
34 struct update_props_workarea {
41 #define NODE_ACTION_MASK 0xff000000
42 #define NODE_COUNT_MASK 0x00ffffff
44 #define DELETE_DT_NODE 0x01000000
45 #define UPDATE_DT_NODE 0x02000000
46 #define ADD_DT_NODE 0x03000000
48 #define MIGRATION_SCOPE (1)
51 static int mobility_rtas_call(int token, char *buf, s32 scope)
55 spin_lock(&rtas_data_buf_lock);
57 memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
58 rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
59 memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
61 spin_unlock(&rtas_data_buf_lock);
65 static int delete_dt_node(struct device_node *dn)
67 struct device_node *pdn;
70 pdn = of_get_parent(dn);
71 is_platfac = of_node_is_type(dn, "ibm,platform-facilities") ||
72 of_node_is_type(pdn, "ibm,platform-facilities");
76 * The drivers that bind to nodes in the platform-facilities
77 * hierarchy don't support node removal, and the removal directive
78 * from firmware is always followed by an add of an equivalent
79 * node. The capability (e.g. RNG, encryption, compression)
80 * represented by the node is never interrupted by the migration.
81 * So ignore changes to this part of the tree.
84 pr_notice("ignoring remove operation for %pOFfp\n", dn);
88 pr_debug("removing node %pOFfp\n", dn);
89 dlpar_detach_node(dn);
93 static int update_dt_property(struct device_node *dn, struct property **prop,
94 const char *name, u32 vd, char *value)
96 struct property *new_prop = *prop;
99 /* A negative 'vd' value indicates that only part of the new property
100 * value is contained in the buffer and we need to call
101 * ibm,update-properties again to get the rest of the value.
103 * A negative value is also the two's compliment of the actual value.
105 if (vd & 0x80000000) {
111 /* partial property fixup */
112 char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
116 memcpy(new_data, new_prop->value, new_prop->length);
117 memcpy(new_data + new_prop->length, value, vd);
119 kfree(new_prop->value);
120 new_prop->value = new_data;
121 new_prop->length += vd;
123 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
127 new_prop->name = kstrdup(name, GFP_KERNEL);
128 if (!new_prop->name) {
133 new_prop->length = vd;
134 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
135 if (!new_prop->value) {
136 kfree(new_prop->name);
141 memcpy(new_prop->value, value, vd);
146 pr_debug("updating node %pOF property %s\n", dn, name);
147 of_update_property(dn, new_prop);
154 static int update_dt_node(struct device_node *dn, s32 scope)
156 struct update_props_workarea *upwa;
157 struct property *prop = NULL;
161 int update_properties_token;
165 update_properties_token = rtas_token("ibm,update-properties");
166 if (update_properties_token == RTAS_UNKNOWN_SERVICE)
169 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
173 upwa = (struct update_props_workarea *)&rtas_buf[0];
174 upwa->phandle = cpu_to_be32(dn->phandle);
177 rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
182 prop_data = rtas_buf + sizeof(*upwa);
183 nprops = be32_to_cpu(upwa->nprops);
185 /* On the first call to ibm,update-properties for a node the
186 * the first property value descriptor contains an empty
187 * property name, the property value length encoded as u32,
188 * and the property value is the node path being updated.
190 if (*prop_data == 0) {
192 vd = be32_to_cpu(*(__be32 *)prop_data);
193 prop_data += vd + sizeof(vd);
197 for (i = 0; i < nprops; i++) {
200 prop_name = prop_data;
201 prop_data += strlen(prop_name) + 1;
202 vd = be32_to_cpu(*(__be32 *)prop_data);
203 prop_data += sizeof(vd);
207 /* name only property, nothing to do */
211 of_remove_property(dn, of_find_property(dn,
217 rc = update_dt_property(dn, &prop, prop_name,
220 pr_err("updating %s property failed: %d\n",
232 } while (rtas_rc == 1);
238 static int add_dt_node(struct device_node *parent_dn, __be32 drc_index)
240 struct device_node *dn;
243 dn = dlpar_configure_connector(drc_index, parent_dn);
248 * Since delete_dt_node() ignores this node type, this is the
249 * necessary counterpart. We also know that a platform-facilities
250 * node returned from dlpar_configure_connector() has children
251 * attached, and dlpar_attach_node() only adds the parent, leaking
252 * the children. So ignore these on the add side for now.
254 if (of_node_is_type(dn, "ibm,platform-facilities")) {
255 pr_notice("ignoring add operation for %pOF\n", dn);
256 dlpar_free_cc_nodes(dn);
260 rc = dlpar_attach_node(dn, parent_dn);
262 dlpar_free_cc_nodes(dn);
264 pr_debug("added node %pOFfp\n", dn);
269 static int pseries_devicetree_update(s32 scope)
273 int update_nodes_token;
276 update_nodes_token = rtas_token("ibm,update-nodes");
277 if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
280 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
285 rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
289 data = (__be32 *)rtas_buf + 4;
290 while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
292 u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
293 u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
297 for (i = 0; i < node_count; i++) {
298 struct device_node *np;
299 __be32 phandle = *data++;
302 np = of_find_node_by_phandle(be32_to_cpu(phandle));
304 pr_warn("Failed lookup: phandle 0x%x for action 0x%x\n",
305 be32_to_cpu(phandle), action);
314 update_dt_node(np, scope);
318 add_dt_node(np, drc_index);
334 void post_mobility_fixup(void)
338 rtas_activate_firmware();
341 * We don't want CPUs to go online/offline while the device
342 * tree is being updated.
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.
351 cacheinfo_teardown();
353 rc = pseries_devicetree_update(MIGRATION_SCOPE);
355 pr_err("device tree update failed: %d\n", rc);
361 /* Possibly switch to a new L1 flush type */
362 pseries_setup_security_mitigations();
364 /* Reinitialise system information for hv-24x7 */
365 read_24x7_sys_info();
370 static int poll_vasi_state(u64 handle, unsigned long *res)
372 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
376 hvrc = plpar_hcall(H_VASI_STATE, retbuf, handle);
390 pr_err("unexpected H_VASI_STATE result %ld\n", hvrc);
397 static int wait_for_vasi_session_suspending(u64 handle)
403 * Wait for transition from H_VASI_ENABLED to
404 * H_VASI_SUSPENDING. Treat anything else as an error.
407 ret = poll_vasi_state(handle, &state);
409 if (ret != 0 || state == H_VASI_SUSPENDING) {
411 } else if (state == H_VASI_ENABLED) {
414 pr_err("unexpected H_VASI_STATE result %lu\n", state);
421 * Proceed even if H_VASI_STATE is unavailable. If H_JOIN or
422 * ibm,suspend-me are also unimplemented, we'll recover then.
424 if (ret == -EOPNOTSUPP)
430 static void prod_single(unsigned int target_cpu)
435 hwid = get_hard_smp_processor_id(target_cpu);
436 hvrc = plpar_hcall_norets(H_PROD, hwid);
437 if (hvrc == H_SUCCESS)
439 pr_err_ratelimited("H_PROD of CPU %u (hwid %d) error: %ld\n",
440 target_cpu, hwid, hvrc);
443 static void prod_others(void)
447 for_each_online_cpu(cpu) {
448 if (cpu != smp_processor_id())
453 static u16 clamp_slb_size(void)
455 #ifdef CONFIG_PPC_64S_HASH_MMU
456 u16 prev = mmu_slb_size;
458 slb_set_size(SLB_MIN_SIZE);
466 static int do_suspend(void)
472 pr_info("calling ibm,suspend-me on CPU %i\n", smp_processor_id());
475 * The destination processor model may have fewer SLB entries
476 * than the source. We reduce mmu_slb_size to a safe minimum
477 * before suspending in order to minimize the possibility of
478 * programming non-existent entries on the destination. If
479 * suspend fails, we restore it before returning. On success
480 * the OF reconfig path will update it from the new device
481 * tree after resuming on the destination.
483 saved_slb_size = clamp_slb_size();
485 ret = rtas_ibm_suspend_me(&status);
487 pr_err("ibm,suspend-me error: %d\n", status);
488 slb_set_size(saved_slb_size);
495 * struct pseries_suspend_info - State shared between CPUs for join/suspend.
496 * @counter: Threads are to increment this upon resuming from suspend
497 * or if an error is received from H_JOIN. The thread which performs
498 * the first increment (i.e. sets it to 1) is responsible for
499 * waking the other threads.
500 * @done: False if join/suspend is in progress. True if the operation is
501 * complete (successful or not).
503 struct pseries_suspend_info {
508 static int do_join(void *arg)
510 struct pseries_suspend_info *info = arg;
511 atomic_t *counter = &info->counter;
516 /* Must ensure MSR.EE off for H_JOIN. */
518 hvrc = plpar_hcall_norets(H_JOIN);
523 * All other CPUs are offline or in H_JOIN. This CPU
524 * attempts the suspend.
530 * The suspend is complete and this cpu has received a
531 * prod, or we've received a stray prod from unrelated
532 * code (e.g. paravirt spinlocks) and we need to join
535 * This barrier orders the return from H_JOIN above vs
536 * the load of info->done. It pairs with the barrier
537 * in the wakeup/prod path below.
540 if (READ_ONCE(info->done) == false) {
541 pr_info_ratelimited("premature return from H_JOIN on CPU %i, retrying",
551 pr_err_ratelimited("H_JOIN error %ld on CPU %i\n",
552 hvrc, smp_processor_id());
556 if (atomic_inc_return(counter) == 1) {
557 pr_info("CPU %u waking all threads\n", smp_processor_id());
558 WRITE_ONCE(info->done, true);
560 * This barrier orders the store to info->done vs subsequent
561 * H_PRODs to wake the other CPUs. It pairs with the barrier
562 * in the H_SUCCESS case above.
568 * Execution may have been suspended for several seconds, so
569 * reset the watchdog.
571 touch_nmi_watchdog();
576 * Abort reason code byte 0. We use only the 'Migrating partition' value.
578 enum vasi_aborting_entity {
581 PARTITION_FIRMWARE = 3,
582 PLATFORM_FIRMWARE = 4,
584 MIGRATING_PARTITION = 6,
587 static void pseries_cancel_migration(u64 handle, int err)
594 entity = MIGRATING_PARTITION;
595 detail = abs(err) & 0xffffff;
596 reason_code = (entity << 24) | detail;
598 hvrc = plpar_hcall_norets(H_VASI_SIGNAL, handle,
599 H_VASI_SIGNAL_CANCEL, reason_code);
601 pr_err("H_VASI_SIGNAL error: %ld\n", hvrc);
604 static int pseries_suspend(u64 handle)
606 const unsigned int max_attempts = 5;
607 unsigned int retry_interval_ms = 1;
608 unsigned int attempt = 1;
612 struct pseries_suspend_info info;
613 unsigned long vasi_state;
616 info = (struct pseries_suspend_info) {
617 .counter = ATOMIC_INIT(0),
621 ret = stop_machine(do_join, &info, cpu_online_mask);
625 * Encountered an error. If the VASI stream is still
626 * in Suspending state, it's likely a transient
627 * condition related to some device in the partition
628 * and we can retry in the hope that the cause has
629 * cleared after some delay.
631 * A better design would allow drivers etc to prepare
632 * for the suspend and avoid conditions which prevent
633 * the suspend from succeeding. For now, we have this
636 pr_notice("Partition suspend attempt %u of %u error: %d\n",
637 attempt, max_attempts, ret);
639 if (attempt == max_attempts)
642 vasi_err = poll_vasi_state(handle, &vasi_state);
644 if (vasi_state != H_VASI_SUSPENDING) {
645 pr_notice("VASI state %lu after failed suspend\n",
649 } else if (vasi_err != -EOPNOTSUPP) {
650 pr_err("VASI state poll error: %d", vasi_err);
654 pr_notice("Will retry partition suspend after %u ms\n",
657 msleep(retry_interval_ms);
658 retry_interval_ms *= 10;
665 static int pseries_migrate_partition(u64 handle)
669 ret = wait_for_vasi_session_suspending(handle);
673 vas_migration_handler(VAS_SUSPEND);
675 ret = pseries_suspend(handle);
677 post_mobility_fixup();
679 pseries_cancel_migration(handle, ret);
681 vas_migration_handler(VAS_RESUME);
686 int rtas_syscall_dispatch_ibm_suspend_me(u64 handle)
688 return pseries_migrate_partition(handle);
691 static ssize_t migration_store(struct class *class,
692 struct class_attribute *attr, const char *buf,
698 rc = kstrtou64(buf, 0, &streamid);
702 rc = pseries_migrate_partition(streamid);
710 * Used by drmgr to determine the kernel behavior of the migration interface.
712 * Version 1: Performs all PAPR requirements for migration including
713 * firmware activation and device tree update.
715 #define MIGRATION_API_VERSION 1
717 static CLASS_ATTR_WO(migration);
718 static CLASS_ATTR_STRING(api_version, 0444, __stringify(MIGRATION_API_VERSION));
720 static int __init mobility_sysfs_init(void)
724 mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
728 rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
730 pr_err("unable to create migration sysfs file (%d)\n", rc);
732 rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
734 pr_err("unable to create api_version sysfs file (%d)\n", rc);
738 machine_device_initcall(pseries, mobility_sysfs_init);