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>
30 #include "vas.h" /* vas_migration_handler() */
31 #include "../../kernel/cacheinfo.h"
33 static struct kobject *mobility_kobj;
35 struct update_props_workarea {
42 #define NODE_ACTION_MASK 0xff000000
43 #define NODE_COUNT_MASK 0x00ffffff
45 #define DELETE_DT_NODE 0x01000000
46 #define UPDATE_DT_NODE 0x02000000
47 #define ADD_DT_NODE 0x03000000
49 #define MIGRATION_SCOPE (1)
52 #ifdef CONFIG_PPC_WATCHDOG
53 static unsigned int nmi_wd_lpm_factor = 200;
56 static const struct ctl_table nmi_wd_lpm_factor_ctl_table[] = {
58 .procname = "nmi_wd_lpm_factor",
59 .data = &nmi_wd_lpm_factor,
60 .maxlen = sizeof(int),
62 .proc_handler = proc_douintvec_minmax,
66 static int __init register_nmi_wd_lpm_factor_sysctl(void)
68 register_sysctl("kernel", nmi_wd_lpm_factor_ctl_table);
72 device_initcall(register_nmi_wd_lpm_factor_sysctl);
73 #endif /* CONFIG_SYSCTL */
74 #endif /* CONFIG_PPC_WATCHDOG */
76 static int mobility_rtas_call(int token, char *buf, s32 scope)
80 spin_lock(&rtas_data_buf_lock);
82 memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
83 rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
84 memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
86 spin_unlock(&rtas_data_buf_lock);
90 static int delete_dt_node(struct device_node *dn)
92 struct device_node *pdn;
95 pdn = of_get_parent(dn);
96 is_platfac = of_node_is_type(dn, "ibm,platform-facilities") ||
97 of_node_is_type(pdn, "ibm,platform-facilities");
101 * The drivers that bind to nodes in the platform-facilities
102 * hierarchy don't support node removal, and the removal directive
103 * from firmware is always followed by an add of an equivalent
104 * node. The capability (e.g. RNG, encryption, compression)
105 * represented by the node is never interrupted by the migration.
106 * So ignore changes to this part of the tree.
109 pr_notice("ignoring remove operation for %pOFfp\n", dn);
113 pr_debug("removing node %pOFfp\n", dn);
114 dlpar_detach_node(dn);
118 static int update_dt_property(struct device_node *dn, struct property **prop,
119 const char *name, u32 vd, char *value)
121 struct property *new_prop = *prop;
124 /* A negative 'vd' value indicates that only part of the new property
125 * value is contained in the buffer and we need to call
126 * ibm,update-properties again to get the rest of the value.
128 * A negative value is also the two's compliment of the actual value.
130 if (vd & 0x80000000) {
136 /* partial property fixup */
137 char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
141 memcpy(new_data, new_prop->value, new_prop->length);
142 memcpy(new_data + new_prop->length, value, vd);
144 kfree(new_prop->value);
145 new_prop->value = new_data;
146 new_prop->length += vd;
148 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
152 new_prop->name = kstrdup(name, GFP_KERNEL);
153 if (!new_prop->name) {
158 new_prop->length = vd;
159 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
160 if (!new_prop->value) {
161 kfree(new_prop->name);
166 memcpy(new_prop->value, value, vd);
171 pr_debug("updating node %pOF property %s\n", dn, name);
172 of_update_property(dn, new_prop);
179 static int update_dt_node(struct device_node *dn, s32 scope)
181 struct update_props_workarea *upwa;
182 struct property *prop = NULL;
186 int update_properties_token;
190 update_properties_token = rtas_function_token(RTAS_FN_IBM_UPDATE_PROPERTIES);
191 if (update_properties_token == RTAS_UNKNOWN_SERVICE)
194 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
198 upwa = (struct update_props_workarea *)&rtas_buf[0];
199 upwa->phandle = cpu_to_be32(dn->phandle);
202 rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
207 prop_data = rtas_buf + sizeof(*upwa);
208 nprops = be32_to_cpu(upwa->nprops);
210 /* On the first call to ibm,update-properties for a node the
211 * first property value descriptor contains an empty
212 * property name, the property value length encoded as u32,
213 * and the property value is the node path being updated.
215 if (*prop_data == 0) {
217 vd = be32_to_cpu(*(__be32 *)prop_data);
218 prop_data += vd + sizeof(vd);
222 for (i = 0; i < nprops; i++) {
225 prop_name = prop_data;
226 prop_data += strlen(prop_name) + 1;
227 vd = be32_to_cpu(*(__be32 *)prop_data);
228 prop_data += sizeof(vd);
232 /* name only property, nothing to do */
236 of_remove_property(dn, of_find_property(dn,
242 rc = update_dt_property(dn, &prop, prop_name,
245 pr_err("updating %s property failed: %d\n",
257 } while (rtas_rc == 1);
263 static int add_dt_node(struct device_node *parent_dn, __be32 drc_index)
265 struct device_node *dn;
268 dn = dlpar_configure_connector(drc_index, parent_dn);
273 * Since delete_dt_node() ignores this node type, this is the
274 * necessary counterpart. We also know that a platform-facilities
275 * node returned from dlpar_configure_connector() has children
276 * attached, and dlpar_attach_node() only adds the parent, leaking
277 * the children. So ignore these on the add side for now.
279 if (of_node_is_type(dn, "ibm,platform-facilities")) {
280 pr_notice("ignoring add operation for %pOF\n", dn);
281 dlpar_free_cc_nodes(dn);
285 rc = dlpar_attach_node(dn, parent_dn);
287 dlpar_free_cc_nodes(dn);
289 pr_debug("added node %pOFfp\n", dn);
294 static int pseries_devicetree_update(s32 scope)
298 int update_nodes_token;
301 update_nodes_token = rtas_function_token(RTAS_FN_IBM_UPDATE_NODES);
302 if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
305 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
310 rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
314 data = (__be32 *)rtas_buf + 4;
315 while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
317 u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
318 u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
322 for (i = 0; i < node_count; i++) {
323 struct device_node *np;
324 __be32 phandle = *data++;
327 np = of_find_node_by_phandle(be32_to_cpu(phandle));
329 pr_warn("Failed lookup: phandle 0x%x for action 0x%x\n",
330 be32_to_cpu(phandle), action);
339 update_dt_node(np, scope);
343 add_dt_node(np, drc_index);
359 void post_mobility_fixup(void)
363 rtas_activate_firmware();
366 * We don't want CPUs to go online/offline while the device
367 * tree is being updated.
372 * It's common for the destination firmware to replace cache
373 * nodes. Release all of the cacheinfo hierarchy's references
374 * before updating the device tree.
376 cacheinfo_teardown();
378 rc = pseries_devicetree_update(MIGRATION_SCOPE);
380 pr_err("device tree update failed: %d\n", rc);
386 /* Possibly switch to a new L1 flush type */
387 pseries_setup_security_mitigations();
389 /* Reinitialise system information for hv-24x7 */
390 read_24x7_sys_info();
395 static int poll_vasi_state(u64 handle, unsigned long *res)
397 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
401 hvrc = plpar_hcall(H_VASI_STATE, retbuf, handle);
415 pr_err("unexpected H_VASI_STATE result %ld\n", hvrc);
422 static int wait_for_vasi_session_suspending(u64 handle)
428 * Wait for transition from H_VASI_ENABLED to
429 * H_VASI_SUSPENDING. Treat anything else as an error.
432 ret = poll_vasi_state(handle, &state);
434 if (ret != 0 || state == H_VASI_SUSPENDING) {
436 } else if (state == H_VASI_ENABLED) {
439 pr_err("unexpected H_VASI_STATE result %lu\n", state);
446 * Proceed even if H_VASI_STATE is unavailable. If H_JOIN or
447 * ibm,suspend-me are also unimplemented, we'll recover then.
449 if (ret == -EOPNOTSUPP)
455 static void wait_for_vasi_session_completed(u64 handle)
457 unsigned long state = 0;
460 pr_info("waiting for memory transfer to complete...\n");
463 * Wait for transition from H_VASI_RESUMED to H_VASI_COMPLETED.
466 ret = poll_vasi_state(handle, &state);
469 * If the memory transfer is already complete and the migration
470 * has been cleaned up by the hypervisor, H_PARAMETER is return,
471 * which is translate in EINVAL by poll_vasi_state().
473 if (ret == -EINVAL || (!ret && state == H_VASI_COMPLETED)) {
474 pr_info("memory transfer completed.\n");
479 pr_err("H_VASI_STATE return error (%d)\n", ret);
483 if (state != H_VASI_RESUMED) {
484 pr_err("unexpected H_VASI_STATE result %lu\n", state);
492 static void prod_single(unsigned int target_cpu)
497 hwid = get_hard_smp_processor_id(target_cpu);
498 hvrc = plpar_hcall_norets(H_PROD, hwid);
499 if (hvrc == H_SUCCESS)
501 pr_err_ratelimited("H_PROD of CPU %u (hwid %d) error: %ld\n",
502 target_cpu, hwid, hvrc);
505 static void prod_others(void)
509 for_each_online_cpu(cpu) {
510 if (cpu != smp_processor_id())
515 static u16 clamp_slb_size(void)
517 #ifdef CONFIG_PPC_64S_HASH_MMU
518 u16 prev = mmu_slb_size;
520 slb_set_size(SLB_MIN_SIZE);
528 static int do_suspend(void)
534 pr_info("calling ibm,suspend-me on CPU %i\n", smp_processor_id());
537 * The destination processor model may have fewer SLB entries
538 * than the source. We reduce mmu_slb_size to a safe minimum
539 * before suspending in order to minimize the possibility of
540 * programming non-existent entries on the destination. If
541 * suspend fails, we restore it before returning. On success
542 * the OF reconfig path will update it from the new device
543 * tree after resuming on the destination.
545 saved_slb_size = clamp_slb_size();
547 ret = rtas_ibm_suspend_me(&status);
549 pr_err("ibm,suspend-me error: %d\n", status);
550 slb_set_size(saved_slb_size);
557 * struct pseries_suspend_info - State shared between CPUs for join/suspend.
558 * @counter: Threads are to increment this upon resuming from suspend
559 * or if an error is received from H_JOIN. The thread which performs
560 * the first increment (i.e. sets it to 1) is responsible for
561 * waking the other threads.
562 * @done: False if join/suspend is in progress. True if the operation is
563 * complete (successful or not).
565 struct pseries_suspend_info {
570 static int do_join(void *arg)
572 struct pseries_suspend_info *info = arg;
573 atomic_t *counter = &info->counter;
578 /* Must ensure MSR.EE off for H_JOIN. */
580 hvrc = plpar_hcall_norets(H_JOIN);
585 * All other CPUs are offline or in H_JOIN. This CPU
586 * attempts the suspend.
592 * The suspend is complete and this cpu has received a
593 * prod, or we've received a stray prod from unrelated
594 * code (e.g. paravirt spinlocks) and we need to join
597 * This barrier orders the return from H_JOIN above vs
598 * the load of info->done. It pairs with the barrier
599 * in the wakeup/prod path below.
602 if (READ_ONCE(info->done) == false) {
603 pr_info_ratelimited("premature return from H_JOIN on CPU %i, retrying",
613 pr_err_ratelimited("H_JOIN error %ld on CPU %i\n",
614 hvrc, smp_processor_id());
618 if (atomic_inc_return(counter) == 1) {
619 pr_info("CPU %u waking all threads\n", smp_processor_id());
620 WRITE_ONCE(info->done, true);
622 * This barrier orders the store to info->done vs subsequent
623 * H_PRODs to wake the other CPUs. It pairs with the barrier
624 * in the H_SUCCESS case above.
630 * Execution may have been suspended for several seconds, so reset
631 * the watchdogs. touch_nmi_watchdog() also touches the soft lockup
634 rcu_cpu_stall_reset();
635 touch_nmi_watchdog();
641 * Abort reason code byte 0. We use only the 'Migrating partition' value.
643 enum vasi_aborting_entity {
646 PARTITION_FIRMWARE = 3,
647 PLATFORM_FIRMWARE = 4,
649 MIGRATING_PARTITION = 6,
652 static void pseries_cancel_migration(u64 handle, int err)
659 entity = MIGRATING_PARTITION;
660 detail = abs(err) & 0xffffff;
661 reason_code = (entity << 24) | detail;
663 hvrc = plpar_hcall_norets(H_VASI_SIGNAL, handle,
664 H_VASI_SIGNAL_CANCEL, reason_code);
666 pr_err("H_VASI_SIGNAL error: %ld\n", hvrc);
669 static int pseries_suspend(u64 handle)
671 const unsigned int max_attempts = 5;
672 unsigned int retry_interval_ms = 1;
673 unsigned int attempt = 1;
677 struct pseries_suspend_info info;
678 unsigned long vasi_state;
681 info = (struct pseries_suspend_info) {
682 .counter = ATOMIC_INIT(0),
686 ret = stop_machine(do_join, &info, cpu_online_mask);
690 * Encountered an error. If the VASI stream is still
691 * in Suspending state, it's likely a transient
692 * condition related to some device in the partition
693 * and we can retry in the hope that the cause has
694 * cleared after some delay.
696 * A better design would allow drivers etc to prepare
697 * for the suspend and avoid conditions which prevent
698 * the suspend from succeeding. For now, we have this
701 pr_notice("Partition suspend attempt %u of %u error: %d\n",
702 attempt, max_attempts, ret);
704 if (attempt == max_attempts)
707 vasi_err = poll_vasi_state(handle, &vasi_state);
709 if (vasi_state != H_VASI_SUSPENDING) {
710 pr_notice("VASI state %lu after failed suspend\n",
714 } else if (vasi_err != -EOPNOTSUPP) {
715 pr_err("VASI state poll error: %d", vasi_err);
719 pr_notice("Will retry partition suspend after %u ms\n",
722 msleep(retry_interval_ms);
723 retry_interval_ms *= 10;
730 static int pseries_migrate_partition(u64 handle)
733 unsigned int factor = 0;
735 #ifdef CONFIG_PPC_WATCHDOG
736 factor = nmi_wd_lpm_factor;
739 * When the migration is initiated, the hypervisor changes VAS
740 * mappings to prepare before OS gets the notification and
741 * closes all VAS windows. NX generates continuous faults during
742 * this time and the user space can not differentiate these
743 * faults from the migration event. So reduce this time window
744 * by closing VAS windows at the beginning of this function.
746 vas_migration_handler(VAS_SUSPEND);
748 ret = wait_for_vasi_session_suspending(handle);
753 watchdog_hardlockup_set_timeout_pct(factor);
755 ret = pseries_suspend(handle);
757 post_mobility_fixup();
759 * Wait until the memory transfer is complete, so that the user
760 * space process returns from the syscall after the transfer is
761 * complete. This allows the user hooks to be executed at the
764 wait_for_vasi_session_completed(handle);
766 pseries_cancel_migration(handle, ret);
769 watchdog_hardlockup_set_timeout_pct(0);
772 vas_migration_handler(VAS_RESUME);
777 int rtas_syscall_dispatch_ibm_suspend_me(u64 handle)
779 return pseries_migrate_partition(handle);
782 static ssize_t migration_store(const struct class *class,
783 const struct class_attribute *attr, const char *buf,
789 rc = kstrtou64(buf, 0, &streamid);
793 rc = pseries_migrate_partition(streamid);
801 * Used by drmgr to determine the kernel behavior of the migration interface.
803 * Version 1: Performs all PAPR requirements for migration including
804 * firmware activation and device tree update.
806 #define MIGRATION_API_VERSION 1
808 static CLASS_ATTR_WO(migration);
809 static CLASS_ATTR_STRING(api_version, 0444, __stringify(MIGRATION_API_VERSION));
811 static int __init mobility_sysfs_init(void)
815 mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
819 rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
821 pr_err("unable to create migration sysfs file (%d)\n", rc);
823 rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
825 pr_err("unable to create api_version sysfs file (%d)\n", rc);
829 machine_device_initcall(pseries, mobility_sysfs_init);