1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pseries CPU Hotplug infrastructure.
5 * Split out from arch/powerpc/platforms/pseries/setup.c
6 * arch/powerpc/kernel/rtas.c, and arch/powerpc/platforms/pseries/smp.c
8 * Peter Bergner, IBM March 2001.
9 * Copyright (C) 2001 IBM.
10 * Dave Engebretsen, Peter Bergner, and
11 * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
12 * Plus various changes from other IBM teams...
14 * Copyright (C) 2006 Michael Ellerman, IBM Corporation
17 #define pr_fmt(fmt) "pseries-hotplug-cpu: " fmt
19 #include <linux/kernel.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/sched.h> /* for idle_task_exit */
23 #include <linux/sched/hotplug.h>
24 #include <linux/cpu.h>
26 #include <linux/slab.h>
29 #include <asm/firmware.h>
30 #include <asm/machdep.h>
31 #include <asm/vdso_datapage.h>
34 #include <asm/plpar_wrappers.h>
35 #include <asm/topology.h>
39 /* This version can't take the spinlock, because it never returns */
40 static int rtas_stop_self_token = RTAS_UNKNOWN_SERVICE;
42 static void rtas_stop_self(void)
44 static struct rtas_args args;
48 BUG_ON(rtas_stop_self_token == RTAS_UNKNOWN_SERVICE);
50 rtas_call_unlocked(&args, rtas_stop_self_token, 0, 1, NULL);
52 panic("Alas, I survived.\n");
55 static void pseries_cpu_offline_self(void)
57 unsigned int hwcpu = hard_smp_processor_id();
66 unregister_slb_shadow(hwcpu);
69 /* Should never get here... */
74 static int pseries_cpu_disable(void)
76 int cpu = smp_processor_id();
78 set_cpu_online(cpu, false);
79 vdso_data->processorCount--;
81 /*fix boot_cpuid here*/
82 if (cpu == boot_cpuid)
83 boot_cpuid = cpumask_any(cpu_online_mask);
85 /* FIXME: abstract this to not be platform specific later on */
87 xive_smp_disable_cpu();
89 xics_migrate_irqs_away();
91 cleanup_cpu_mmu_context();
97 * pseries_cpu_die: Wait for the cpu to die.
98 * @cpu: logical processor id of the CPU whose death we're awaiting.
100 * This function is called from the context of the thread which is performing
101 * the cpu-offline. Here we wait for long enough to allow the cpu in question
102 * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
105 * OTOH, pseries_cpu_offline_self() is called by the @cpu when it wants to
108 static void pseries_cpu_die(unsigned int cpu)
111 unsigned int pcpu = get_hard_smp_processor_id(cpu);
112 unsigned long timeout = jiffies + msecs_to_jiffies(120000);
115 cpu_status = smp_query_cpu_stopped(pcpu);
116 if (cpu_status == QCSS_STOPPED ||
117 cpu_status == QCSS_HARDWARE_ERROR)
120 if (time_after(jiffies, timeout)) {
121 pr_warn("CPU %i (hwid %i) didn't die after 120 seconds\n",
123 timeout = jiffies + msecs_to_jiffies(120000);
129 if (cpu_status == QCSS_HARDWARE_ERROR) {
130 pr_warn("CPU %i (hwid %i) reported error while dying\n",
134 /* Isolation and deallocation are definitely done by
135 * drslot_chrp_cpu. If they were not they would be
136 * done here. Change isolate state to Isolate and
137 * change allocation-state to Unusable.
139 paca_ptrs[cpu]->cpu_start = 0;
143 * Update cpu_present_mask and paca(s) for a new cpu node. The wrinkle
144 * here is that a cpu device node may represent up to two logical cpus
145 * in the SMT case. We must honor the assumption in other code that
146 * the logical ids for sibling SMT threads x and y are adjacent, such
147 * that x^1 == y and y^1 == x.
149 static int pseries_add_processor(struct device_node *np)
152 cpumask_var_t candidate_mask, tmp;
153 int err = -ENOSPC, len, nthreads, i;
154 const __be32 *intserv;
156 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
160 zalloc_cpumask_var(&candidate_mask, GFP_KERNEL);
161 zalloc_cpumask_var(&tmp, GFP_KERNEL);
163 nthreads = len / sizeof(u32);
164 for (i = 0; i < nthreads; i++)
165 cpumask_set_cpu(i, tmp);
167 cpu_maps_update_begin();
169 BUG_ON(!cpumask_subset(cpu_present_mask, cpu_possible_mask));
171 /* Get a bitmap of unoccupied slots. */
172 cpumask_xor(candidate_mask, cpu_possible_mask, cpu_present_mask);
173 if (cpumask_empty(candidate_mask)) {
174 /* If we get here, it most likely means that NR_CPUS is
175 * less than the partition's max processors setting.
177 printk(KERN_ERR "Cannot add cpu %pOF; this system configuration"
178 " supports %d logical cpus.\n", np,
179 num_possible_cpus());
183 while (!cpumask_empty(tmp))
184 if (cpumask_subset(tmp, candidate_mask))
185 /* Found a range where we can insert the new cpu(s) */
188 cpumask_shift_left(tmp, tmp, nthreads);
190 if (cpumask_empty(tmp)) {
191 printk(KERN_ERR "Unable to find space in cpu_present_mask for"
192 " processor %pOFn with %d thread(s)\n", np,
197 for_each_cpu(cpu, tmp) {
198 BUG_ON(cpu_present(cpu));
199 set_cpu_present(cpu, true);
200 set_hard_smp_processor_id(cpu, be32_to_cpu(*intserv++));
204 cpu_maps_update_done();
205 free_cpumask_var(candidate_mask);
206 free_cpumask_var(tmp);
211 * Update the present map for a cpu node which is going away, and set
212 * the hard id in the paca(s) to -1 to be consistent with boot time
213 * convention for non-present cpus.
215 static void pseries_remove_processor(struct device_node *np)
218 int len, nthreads, i;
219 const __be32 *intserv;
222 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
226 nthreads = len / sizeof(u32);
228 cpu_maps_update_begin();
229 for (i = 0; i < nthreads; i++) {
230 thread = be32_to_cpu(intserv[i]);
231 for_each_present_cpu(cpu) {
232 if (get_hard_smp_processor_id(cpu) != thread)
234 BUG_ON(cpu_online(cpu));
235 set_cpu_present(cpu, false);
236 set_hard_smp_processor_id(cpu, -1);
237 update_numa_cpu_lookup_table(cpu, -1);
240 if (cpu >= nr_cpu_ids)
241 printk(KERN_WARNING "Could not find cpu to remove "
242 "with physical id 0x%x\n", thread);
244 cpu_maps_update_done();
247 static int dlpar_offline_cpu(struct device_node *dn)
251 int len, nthreads, i;
252 const __be32 *intserv;
255 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
259 nthreads = len / sizeof(u32);
261 cpu_maps_update_begin();
262 for (i = 0; i < nthreads; i++) {
263 thread = be32_to_cpu(intserv[i]);
264 for_each_present_cpu(cpu) {
265 if (get_hard_smp_processor_id(cpu) != thread)
268 if (!cpu_online(cpu))
272 * device_offline() will return -EBUSY (via cpu_down()) if there
273 * is only one CPU left. Check it here to fail earlier and with a
274 * more informative error message, while also retaining the
275 * cpu_add_remove_lock to be sure that no CPUs are being
276 * online/offlined during this check.
278 if (num_online_cpus() == 1) {
279 pr_warn("Unable to remove last online CPU %pOFn\n", dn);
284 cpu_maps_update_done();
285 rc = device_offline(get_cpu_device(cpu));
288 cpu_maps_update_begin();
291 if (cpu == num_possible_cpus()) {
292 pr_warn("Could not find cpu to offline with physical id 0x%x\n",
297 cpu_maps_update_done();
303 static int dlpar_online_cpu(struct device_node *dn)
307 int len, nthreads, i;
308 const __be32 *intserv;
311 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
315 nthreads = len / sizeof(u32);
317 cpu_maps_update_begin();
318 for (i = 0; i < nthreads; i++) {
319 thread = be32_to_cpu(intserv[i]);
320 for_each_present_cpu(cpu) {
321 if (get_hard_smp_processor_id(cpu) != thread)
323 cpu_maps_update_done();
324 find_and_online_cpu_nid(cpu);
325 rc = device_online(get_cpu_device(cpu));
327 dlpar_offline_cpu(dn);
330 cpu_maps_update_begin();
334 if (cpu == num_possible_cpus())
335 printk(KERN_WARNING "Could not find cpu to online "
336 "with physical id 0x%x\n", thread);
338 cpu_maps_update_done();
345 static bool dlpar_cpu_exists(struct device_node *parent, u32 drc_index)
347 struct device_node *child = NULL;
352 /* Assume cpu doesn't exist */
355 for_each_child_of_node(parent, child) {
356 rc = of_property_read_u32(child, "ibm,my-drc-index",
361 if (my_drc_index == drc_index) {
371 static bool drc_info_valid_index(struct device_node *parent, u32 drc_index)
373 struct property *info;
374 struct of_drc_info drc;
379 info = of_find_property(parent, "ibm,drc-info", NULL);
383 value = of_prop_next_u32(info, NULL, &count);
385 /* First value of ibm,drc-info is number of drc-info records */
391 for (i = 0; i < count; i++) {
392 if (of_read_drc_info_cell(&info, &value, &drc))
395 if (strncmp(drc.drc_type, "CPU", 3))
398 if (drc_index > drc.last_drc_index)
401 index = drc.drc_index_start;
402 for (j = 0; j < drc.num_sequential_elems; j++) {
403 if (drc_index == index)
406 index += drc.sequential_inc;
413 static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
418 if (of_find_property(parent, "ibm,drc-info", NULL))
419 return drc_info_valid_index(parent, drc_index);
421 /* Note that the format of the ibm,drc-indexes array is
422 * the number of entries in the array followed by the array
423 * of drc values so we start looking at index = 1.
429 rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
435 if (drc == drc_index)
442 static ssize_t dlpar_cpu_add(u32 drc_index)
444 struct device_node *dn, *parent;
447 pr_debug("Attempting to add CPU, drc index: %x\n", drc_index);
449 parent = of_find_node_by_path("/cpus");
451 pr_warn("Failed to find CPU root node \"/cpus\"\n");
455 if (dlpar_cpu_exists(parent, drc_index)) {
457 pr_warn("CPU with drc index %x already exists\n", drc_index);
461 if (!valid_cpu_drc_index(parent, drc_index)) {
463 pr_warn("Cannot find CPU (drc index %x) to add.\n", drc_index);
467 rc = dlpar_acquire_drc(drc_index);
469 pr_warn("Failed to acquire DRC, rc: %d, drc index: %x\n",
475 dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
477 pr_warn("Failed call to configure-connector, drc index: %x\n",
479 dlpar_release_drc(drc_index);
484 rc = dlpar_attach_node(dn, parent);
486 /* Regardless we are done with parent now */
491 pr_warn("Failed to attach node %pOFn, rc: %d, drc index: %x\n",
494 rc = dlpar_release_drc(drc_index);
496 dlpar_free_cc_nodes(dn);
501 rc = dlpar_online_cpu(dn);
504 pr_warn("Failed to online cpu %pOFn, rc: %d, drc index: %x\n",
507 rc = dlpar_detach_node(dn);
509 dlpar_release_drc(drc_index);
514 pr_debug("Successfully added CPU %pOFn, drc index: %x\n", dn,
519 static ssize_t dlpar_cpu_remove(struct device_node *dn, u32 drc_index)
523 pr_debug("Attempting to remove CPU %pOFn, drc index: %x\n",
526 rc = dlpar_offline_cpu(dn);
528 pr_warn("Failed to offline CPU %pOFn, rc: %d\n", dn, rc);
532 rc = dlpar_release_drc(drc_index);
534 pr_warn("Failed to release drc (%x) for CPU %pOFn, rc: %d\n",
536 dlpar_online_cpu(dn);
540 rc = dlpar_detach_node(dn);
544 pr_warn("Failed to detach CPU %pOFn, rc: %d", dn, rc);
546 rc = dlpar_acquire_drc(drc_index);
548 dlpar_online_cpu(dn);
553 pr_debug("Successfully removed CPU, drc index: %x\n", drc_index);
557 static struct device_node *cpu_drc_index_to_dn(u32 drc_index)
559 struct device_node *dn;
563 for_each_node_by_type(dn, "cpu") {
564 rc = of_property_read_u32(dn, "ibm,my-drc-index", &my_index);
568 if (my_index == drc_index)
575 static int dlpar_cpu_remove_by_index(u32 drc_index)
577 struct device_node *dn;
580 dn = cpu_drc_index_to_dn(drc_index);
582 pr_warn("Cannot find CPU (drc index %x) to remove\n",
587 rc = dlpar_cpu_remove(dn, drc_index);
592 static int find_dlpar_cpus_to_remove(u32 *cpu_drcs, int cpus_to_remove)
594 struct device_node *dn;
598 /* We want to find cpus_to_remove + 1 CPUs to ensure we do not
599 * remove the last CPU.
601 for_each_node_by_type(dn, "cpu") {
604 if (cpus_found > cpus_to_remove) {
609 /* Note that cpus_found is always 1 ahead of the index
610 * into the cpu_drcs array, so we use cpus_found - 1
612 rc = of_property_read_u32(dn, "ibm,my-drc-index",
613 &cpu_drcs[cpus_found - 1]);
615 pr_warn("Error occurred getting drc-index for %pOFn\n",
622 if (cpus_found < cpus_to_remove) {
623 pr_warn("Failed to find enough CPUs (%d of %d) to remove\n",
624 cpus_found, cpus_to_remove);
625 } else if (cpus_found == cpus_to_remove) {
626 pr_warn("Cannot remove all CPUs\n");
632 static int dlpar_cpu_remove_by_count(u32 cpus_to_remove)
636 int cpus_removed = 0;
639 pr_debug("Attempting to hot-remove %d CPUs\n", cpus_to_remove);
641 cpu_drcs = kcalloc(cpus_to_remove, sizeof(*cpu_drcs), GFP_KERNEL);
645 cpus_found = find_dlpar_cpus_to_remove(cpu_drcs, cpus_to_remove);
646 if (cpus_found <= cpus_to_remove) {
651 for (i = 0; i < cpus_to_remove; i++) {
652 rc = dlpar_cpu_remove_by_index(cpu_drcs[i]);
659 if (cpus_removed != cpus_to_remove) {
660 pr_warn("CPU hot-remove failed, adding back removed CPUs\n");
662 for (i = 0; i < cpus_removed; i++)
663 dlpar_cpu_add(cpu_drcs[i]);
674 static int find_drc_info_cpus_to_add(struct device_node *cpus,
675 struct property *info,
676 u32 *cpu_drcs, u32 cpus_to_add)
678 struct of_drc_info drc;
680 u32 count, drc_index;
687 value = of_prop_next_u32(info, NULL, &count);
691 for (i = 0; i < count; i++) {
692 of_read_drc_info_cell(&info, &value, &drc);
693 if (strncmp(drc.drc_type, "CPU", 3))
696 drc_index = drc.drc_index_start;
697 for (j = 0; j < drc.num_sequential_elems; j++) {
698 if (dlpar_cpu_exists(cpus, drc_index))
701 cpu_drcs[cpus_found++] = drc_index;
703 if (cpus_found == cpus_to_add)
706 drc_index += drc.sequential_inc;
713 static int find_drc_index_cpus_to_add(struct device_node *cpus,
714 u32 *cpu_drcs, u32 cpus_to_add)
720 /* Search the ibm,drc-indexes array for possible CPU drcs to
721 * add. Note that the format of the ibm,drc-indexes array is
722 * the number of entries in the array followed by the array
723 * of drc values so we start looking at index = 1.
726 while (cpus_found < cpus_to_add) {
727 rc = of_property_read_u32_index(cpus, "ibm,drc-indexes",
728 index++, &drc_index);
733 if (dlpar_cpu_exists(cpus, drc_index))
736 cpu_drcs[cpus_found++] = drc_index;
742 static int dlpar_cpu_add_by_count(u32 cpus_to_add)
744 struct device_node *parent;
745 struct property *info;
751 pr_debug("Attempting to hot-add %d CPUs\n", cpus_to_add);
753 cpu_drcs = kcalloc(cpus_to_add, sizeof(*cpu_drcs), GFP_KERNEL);
757 parent = of_find_node_by_path("/cpus");
759 pr_warn("Could not find CPU root node in device tree\n");
764 info = of_find_property(parent, "ibm,drc-info", NULL);
766 cpus_found = find_drc_info_cpus_to_add(parent, info, cpu_drcs, cpus_to_add);
768 cpus_found = find_drc_index_cpus_to_add(parent, cpu_drcs, cpus_to_add);
772 if (cpus_found < cpus_to_add) {
773 pr_warn("Failed to find enough CPUs (%d of %d) to add\n",
774 cpus_found, cpus_to_add);
779 for (i = 0; i < cpus_to_add; i++) {
780 rc = dlpar_cpu_add(cpu_drcs[i]);
787 if (cpus_added < cpus_to_add) {
788 pr_warn("CPU hot-add failed, removing any added CPUs\n");
790 for (i = 0; i < cpus_added; i++)
791 dlpar_cpu_remove_by_index(cpu_drcs[i]);
802 int dlpar_cpu(struct pseries_hp_errorlog *hp_elog)
804 u32 count, drc_index;
807 count = hp_elog->_drc_u.drc_count;
808 drc_index = hp_elog->_drc_u.drc_index;
810 lock_device_hotplug();
812 switch (hp_elog->action) {
813 case PSERIES_HP_ELOG_ACTION_REMOVE:
814 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
815 rc = dlpar_cpu_remove_by_count(count);
816 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
817 rc = dlpar_cpu_remove_by_index(drc_index);
819 * Setting the isolation state of an UNISOLATED/CONFIGURED
820 * device to UNISOLATE is a no-op, but the hypervisor can
821 * use it as a hint that the CPU removal failed.
824 dlpar_unisolate_drc(drc_index);
829 case PSERIES_HP_ELOG_ACTION_ADD:
830 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
831 rc = dlpar_cpu_add_by_count(count);
832 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
833 rc = dlpar_cpu_add(drc_index);
838 pr_err("Invalid action (%d) specified\n", hp_elog->action);
843 unlock_device_hotplug();
847 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
849 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
854 rc = kstrtou32(buf, 0, &drc_index);
858 rc = dlpar_cpu_add(drc_index);
860 return rc ? rc : count;
863 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
865 struct device_node *dn;
869 dn = of_find_node_by_path(buf);
873 rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
879 rc = dlpar_cpu_remove(dn, drc_index);
882 return rc ? rc : count;
885 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
887 static int pseries_smp_notifier(struct notifier_block *nb,
888 unsigned long action, void *data)
890 struct of_reconfig_data *rd = data;
894 case OF_RECONFIG_ATTACH_NODE:
895 err = pseries_add_processor(rd->dn);
897 case OF_RECONFIG_DETACH_NODE:
898 pseries_remove_processor(rd->dn);
901 return notifier_from_errno(err);
904 static struct notifier_block pseries_smp_nb = {
905 .notifier_call = pseries_smp_notifier,
908 static int __init pseries_cpu_hotplug_init(void)
912 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
913 ppc_md.cpu_probe = dlpar_cpu_probe;
914 ppc_md.cpu_release = dlpar_cpu_release;
915 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
917 rtas_stop_self_token = rtas_token("stop-self");
918 qcss_tok = rtas_token("query-cpu-stopped-state");
920 if (rtas_stop_self_token == RTAS_UNKNOWN_SERVICE ||
921 qcss_tok == RTAS_UNKNOWN_SERVICE) {
922 printk(KERN_INFO "CPU Hotplug not supported by firmware "
927 smp_ops->cpu_offline_self = pseries_cpu_offline_self;
928 smp_ops->cpu_disable = pseries_cpu_disable;
929 smp_ops->cpu_die = pseries_cpu_die;
931 /* Processors can be added/removed only on LPAR */
932 if (firmware_has_feature(FW_FEATURE_LPAR))
933 of_reconfig_notifier_register(&pseries_smp_nb);
937 machine_arch_initcall(pseries, pseries_cpu_hotplug_init);