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;
43 * Record the CPU ids used on each nodes.
44 * Protected by cpu_add_remove_lock.
46 static cpumask_var_t node_recorded_ids_map[MAX_NUMNODES];
48 static void rtas_stop_self(void)
50 static struct rtas_args args;
54 BUG_ON(rtas_stop_self_token == RTAS_UNKNOWN_SERVICE);
56 rtas_call_unlocked(&args, rtas_stop_self_token, 0, 1, NULL);
58 panic("Alas, I survived.\n");
61 static void pseries_cpu_offline_self(void)
63 unsigned int hwcpu = hard_smp_processor_id();
72 unregister_slb_shadow(hwcpu);
75 /* Should never get here... */
80 static int pseries_cpu_disable(void)
82 int cpu = smp_processor_id();
84 set_cpu_online(cpu, false);
85 vdso_data->processorCount--;
87 /*fix boot_cpuid here*/
88 if (cpu == boot_cpuid)
89 boot_cpuid = cpumask_any(cpu_online_mask);
91 /* FIXME: abstract this to not be platform specific later on */
93 xive_smp_disable_cpu();
95 xics_migrate_irqs_away();
97 cleanup_cpu_mmu_context();
103 * pseries_cpu_die: Wait for the cpu to die.
104 * @cpu: logical processor id of the CPU whose death we're awaiting.
106 * This function is called from the context of the thread which is performing
107 * the cpu-offline. Here we wait for long enough to allow the cpu in question
108 * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
111 * OTOH, pseries_cpu_offline_self() is called by the @cpu when it wants to
114 static void pseries_cpu_die(unsigned int cpu)
117 unsigned int pcpu = get_hard_smp_processor_id(cpu);
118 unsigned long timeout = jiffies + msecs_to_jiffies(120000);
121 cpu_status = smp_query_cpu_stopped(pcpu);
122 if (cpu_status == QCSS_STOPPED ||
123 cpu_status == QCSS_HARDWARE_ERROR)
126 if (time_after(jiffies, timeout)) {
127 pr_warn("CPU %i (hwid %i) didn't die after 120 seconds\n",
129 timeout = jiffies + msecs_to_jiffies(120000);
135 if (cpu_status == QCSS_HARDWARE_ERROR) {
136 pr_warn("CPU %i (hwid %i) reported error while dying\n",
140 paca_ptrs[cpu]->cpu_start = 0;
144 * find_cpu_id_range - found a linear ranger of @nthreads free CPU ids.
145 * @nthreads : the number of threads (cpu ids)
146 * @assigned_node : the node it belongs to or NUMA_NO_NODE if free ids from any
148 * @cpu_mask: the returned CPU mask.
150 * Returns 0 on success.
152 static int find_cpu_id_range(unsigned int nthreads, int assigned_node,
153 cpumask_var_t *cpu_mask)
155 cpumask_var_t candidate_mask;
156 unsigned int cpu, node;
159 if (!zalloc_cpumask_var(&candidate_mask, GFP_KERNEL))
162 cpumask_clear(*cpu_mask);
163 for (cpu = 0; cpu < nthreads; cpu++)
164 cpumask_set_cpu(cpu, *cpu_mask);
166 BUG_ON(!cpumask_subset(cpu_present_mask, cpu_possible_mask));
168 /* Get a bitmap of unoccupied slots. */
169 cpumask_xor(candidate_mask, cpu_possible_mask, cpu_present_mask);
171 if (assigned_node != NUMA_NO_NODE) {
173 * Remove free ids previously assigned on the other nodes. We
174 * can walk only online nodes because once a node became online
175 * it is not turned offlined back.
177 for_each_online_node(node) {
178 if (node == assigned_node)
180 cpumask_andnot(candidate_mask, candidate_mask,
181 node_recorded_ids_map[node]);
185 if (cpumask_empty(candidate_mask))
188 while (!cpumask_empty(*cpu_mask)) {
189 if (cpumask_subset(*cpu_mask, candidate_mask))
190 /* Found a range where we can insert the new cpu(s) */
192 cpumask_shift_left(*cpu_mask, *cpu_mask, nthreads);
195 if (!cpumask_empty(*cpu_mask))
199 free_cpumask_var(candidate_mask);
204 * Update cpu_present_mask and paca(s) for a new cpu node. The wrinkle
205 * here is that a cpu device node may represent multiple logical cpus
206 * in the SMT case. We must honor the assumption in other code that
207 * the logical ids for sibling SMT threads x and y are adjacent, such
208 * that x^1 == y and y^1 == x.
210 static int pseries_add_processor(struct device_node *np)
212 int len, nthreads, node, cpu, assigned_node;
214 cpumask_var_t cpu_mask;
215 const __be32 *intserv;
217 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
221 nthreads = len / sizeof(u32);
223 if (!alloc_cpumask_var(&cpu_mask, GFP_KERNEL))
227 * Fetch from the DT nodes read by dlpar_configure_connector() the NUMA
228 * node id the added CPU belongs to.
230 node = of_node_to_nid(np);
231 if (node < 0 || !node_possible(node))
232 node = first_online_node;
234 BUG_ON(node == NUMA_NO_NODE);
235 assigned_node = node;
237 cpu_maps_update_begin();
239 rc = find_cpu_id_range(nthreads, node, &cpu_mask);
240 if (rc && nr_node_ids > 1) {
242 * Try again, considering the free CPU ids from the other node.
245 rc = find_cpu_id_range(nthreads, NUMA_NO_NODE, &cpu_mask);
249 pr_err("Cannot add cpu %pOF; this system configuration"
250 " supports %d logical cpus.\n", np, num_possible_cpus());
254 for_each_cpu(cpu, cpu_mask) {
255 BUG_ON(cpu_present(cpu));
256 set_cpu_present(cpu, true);
257 set_hard_smp_processor_id(cpu, be32_to_cpu(*intserv++));
260 /* Record the newly used CPU ids for the associate node. */
261 cpumask_or(node_recorded_ids_map[assigned_node],
262 node_recorded_ids_map[assigned_node], cpu_mask);
265 * If node is set to NUMA_NO_NODE, CPU ids have be reused from
266 * another node, remove them from its mask.
268 if (node == NUMA_NO_NODE) {
269 cpu = cpumask_first(cpu_mask);
270 pr_warn("Reusing free CPU ids %d-%d from another node\n",
271 cpu, cpu + nthreads - 1);
272 for_each_online_node(node) {
273 if (node == assigned_node)
275 cpumask_andnot(node_recorded_ids_map[node],
276 node_recorded_ids_map[node],
282 cpu_maps_update_done();
283 free_cpumask_var(cpu_mask);
288 * Update the present map for a cpu node which is going away, and set
289 * the hard id in the paca(s) to -1 to be consistent with boot time
290 * convention for non-present cpus.
292 static void pseries_remove_processor(struct device_node *np)
295 int len, nthreads, i;
296 const __be32 *intserv;
299 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
303 nthreads = len / sizeof(u32);
305 cpu_maps_update_begin();
306 for (i = 0; i < nthreads; i++) {
307 thread = be32_to_cpu(intserv[i]);
308 for_each_present_cpu(cpu) {
309 if (get_hard_smp_processor_id(cpu) != thread)
311 BUG_ON(cpu_online(cpu));
312 set_cpu_present(cpu, false);
313 set_hard_smp_processor_id(cpu, -1);
314 update_numa_cpu_lookup_table(cpu, -1);
317 if (cpu >= nr_cpu_ids)
318 printk(KERN_WARNING "Could not find cpu to remove "
319 "with physical id 0x%x\n", thread);
321 cpu_maps_update_done();
324 static int dlpar_offline_cpu(struct device_node *dn)
328 int len, nthreads, i;
329 const __be32 *intserv;
332 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
336 nthreads = len / sizeof(u32);
338 cpu_maps_update_begin();
339 for (i = 0; i < nthreads; i++) {
340 thread = be32_to_cpu(intserv[i]);
341 for_each_present_cpu(cpu) {
342 if (get_hard_smp_processor_id(cpu) != thread)
345 if (!cpu_online(cpu))
349 * device_offline() will return -EBUSY (via cpu_down()) if there
350 * is only one CPU left. Check it here to fail earlier and with a
351 * more informative error message, while also retaining the
352 * cpu_add_remove_lock to be sure that no CPUs are being
353 * online/offlined during this check.
355 if (num_online_cpus() == 1) {
356 pr_warn("Unable to remove last online CPU %pOFn\n", dn);
361 cpu_maps_update_done();
362 rc = device_offline(get_cpu_device(cpu));
365 cpu_maps_update_begin();
368 if (cpu == num_possible_cpus()) {
369 pr_warn("Could not find cpu to offline with physical id 0x%x\n",
374 cpu_maps_update_done();
380 static int dlpar_online_cpu(struct device_node *dn)
384 int len, nthreads, i;
385 const __be32 *intserv;
388 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
392 nthreads = len / sizeof(u32);
394 cpu_maps_update_begin();
395 for (i = 0; i < nthreads; i++) {
396 thread = be32_to_cpu(intserv[i]);
397 for_each_present_cpu(cpu) {
398 if (get_hard_smp_processor_id(cpu) != thread)
400 cpu_maps_update_done();
401 find_and_update_cpu_nid(cpu);
402 rc = device_online(get_cpu_device(cpu));
404 dlpar_offline_cpu(dn);
407 cpu_maps_update_begin();
411 if (cpu == num_possible_cpus())
412 printk(KERN_WARNING "Could not find cpu to online "
413 "with physical id 0x%x\n", thread);
415 cpu_maps_update_done();
422 static bool dlpar_cpu_exists(struct device_node *parent, u32 drc_index)
424 struct device_node *child = NULL;
429 /* Assume cpu doesn't exist */
432 for_each_child_of_node(parent, child) {
433 rc = of_property_read_u32(child, "ibm,my-drc-index",
438 if (my_drc_index == drc_index) {
448 static bool drc_info_valid_index(struct device_node *parent, u32 drc_index)
450 struct property *info;
451 struct of_drc_info drc;
456 info = of_find_property(parent, "ibm,drc-info", NULL);
460 value = of_prop_next_u32(info, NULL, &count);
462 /* First value of ibm,drc-info is number of drc-info records */
468 for (i = 0; i < count; i++) {
469 if (of_read_drc_info_cell(&info, &value, &drc))
472 if (strncmp(drc.drc_type, "CPU", 3))
475 if (drc_index > drc.last_drc_index)
478 index = drc.drc_index_start;
479 for (j = 0; j < drc.num_sequential_elems; j++) {
480 if (drc_index == index)
483 index += drc.sequential_inc;
490 static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
495 if (of_find_property(parent, "ibm,drc-info", NULL))
496 return drc_info_valid_index(parent, drc_index);
498 /* Note that the format of the ibm,drc-indexes array is
499 * the number of entries in the array followed by the array
500 * of drc values so we start looking at index = 1.
506 rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
512 if (drc == drc_index)
519 static int pseries_cpuhp_attach_nodes(struct device_node *dn)
521 struct of_changeset cs;
525 * This device node is unattached but may have siblings; open-code the
528 for (of_changeset_init(&cs); dn != NULL; dn = dn->sibling) {
529 ret = of_changeset_attach_node(&cs, dn);
534 ret = of_changeset_apply(&cs);
536 of_changeset_destroy(&cs);
540 static ssize_t dlpar_cpu_add(u32 drc_index)
542 struct device_node *dn, *parent;
545 pr_debug("Attempting to add CPU, drc index: %x\n", drc_index);
547 parent = of_find_node_by_path("/cpus");
549 pr_warn("Failed to find CPU root node \"/cpus\"\n");
553 if (dlpar_cpu_exists(parent, drc_index)) {
555 pr_warn("CPU with drc index %x already exists\n", drc_index);
559 if (!valid_cpu_drc_index(parent, drc_index)) {
561 pr_warn("Cannot find CPU (drc index %x) to add.\n", drc_index);
565 rc = dlpar_acquire_drc(drc_index);
567 pr_warn("Failed to acquire DRC, rc: %d, drc index: %x\n",
573 dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
575 pr_warn("Failed call to configure-connector, drc index: %x\n",
577 dlpar_release_drc(drc_index);
582 rc = pseries_cpuhp_attach_nodes(dn);
584 /* Regardless we are done with parent now */
589 pr_warn("Failed to attach node %pOFn, rc: %d, drc index: %x\n",
592 rc = dlpar_release_drc(drc_index);
594 dlpar_free_cc_nodes(dn);
599 update_numa_distance(dn);
601 rc = dlpar_online_cpu(dn);
604 pr_warn("Failed to online cpu %pOFn, rc: %d, drc index: %x\n",
607 rc = dlpar_detach_node(dn);
609 dlpar_release_drc(drc_index);
614 pr_debug("Successfully added CPU %pOFn, drc index: %x\n", dn,
619 static unsigned int pseries_cpuhp_cache_use_count(const struct device_node *cachedn)
621 unsigned int use_count = 0;
622 struct device_node *dn;
624 WARN_ON(!of_node_is_type(cachedn, "cache"));
626 for_each_of_cpu_node(dn) {
627 if (of_find_next_cache_node(dn) == cachedn)
631 for_each_node_by_type(dn, "cache") {
632 if (of_find_next_cache_node(dn) == cachedn)
639 static int pseries_cpuhp_detach_nodes(struct device_node *cpudn)
641 struct device_node *dn;
642 struct of_changeset cs;
645 of_changeset_init(&cs);
646 ret = of_changeset_detach_node(&cs, cpudn);
651 while ((dn = of_find_next_cache_node(dn))) {
652 if (pseries_cpuhp_cache_use_count(dn) > 1)
655 ret = of_changeset_detach_node(&cs, dn);
660 ret = of_changeset_apply(&cs);
662 of_changeset_destroy(&cs);
666 static ssize_t dlpar_cpu_remove(struct device_node *dn, u32 drc_index)
670 pr_debug("Attempting to remove CPU %pOFn, drc index: %x\n",
673 rc = dlpar_offline_cpu(dn);
675 pr_warn("Failed to offline CPU %pOFn, rc: %d\n", dn, rc);
679 rc = dlpar_release_drc(drc_index);
681 pr_warn("Failed to release drc (%x) for CPU %pOFn, rc: %d\n",
683 dlpar_online_cpu(dn);
687 rc = pseries_cpuhp_detach_nodes(dn);
691 pr_warn("Failed to detach CPU %pOFn, rc: %d", dn, rc);
693 rc = dlpar_acquire_drc(drc_index);
695 dlpar_online_cpu(dn);
700 pr_debug("Successfully removed CPU, drc index: %x\n", drc_index);
704 static struct device_node *cpu_drc_index_to_dn(u32 drc_index)
706 struct device_node *dn;
710 for_each_node_by_type(dn, "cpu") {
711 rc = of_property_read_u32(dn, "ibm,my-drc-index", &my_index);
715 if (my_index == drc_index)
722 static int dlpar_cpu_remove_by_index(u32 drc_index)
724 struct device_node *dn;
727 dn = cpu_drc_index_to_dn(drc_index);
729 pr_warn("Cannot find CPU (drc index %x) to remove\n",
734 rc = dlpar_cpu_remove(dn, drc_index);
739 int dlpar_cpu(struct pseries_hp_errorlog *hp_elog)
744 drc_index = hp_elog->_drc_u.drc_index;
746 lock_device_hotplug();
748 switch (hp_elog->action) {
749 case PSERIES_HP_ELOG_ACTION_REMOVE:
750 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
751 rc = dlpar_cpu_remove_by_index(drc_index);
753 * Setting the isolation state of an UNISOLATED/CONFIGURED
754 * device to UNISOLATE is a no-op, but the hypervisor can
755 * use it as a hint that the CPU removal failed.
758 dlpar_unisolate_drc(drc_index);
763 case PSERIES_HP_ELOG_ACTION_ADD:
764 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
765 rc = dlpar_cpu_add(drc_index);
770 pr_err("Invalid action (%d) specified\n", hp_elog->action);
775 unlock_device_hotplug();
779 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
781 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
786 rc = kstrtou32(buf, 0, &drc_index);
790 rc = dlpar_cpu_add(drc_index);
792 return rc ? rc : count;
795 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
797 struct device_node *dn;
801 dn = of_find_node_by_path(buf);
805 rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
811 rc = dlpar_cpu_remove(dn, drc_index);
814 return rc ? rc : count;
817 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
819 static int pseries_smp_notifier(struct notifier_block *nb,
820 unsigned long action, void *data)
822 struct of_reconfig_data *rd = data;
826 case OF_RECONFIG_ATTACH_NODE:
827 err = pseries_add_processor(rd->dn);
829 case OF_RECONFIG_DETACH_NODE:
830 pseries_remove_processor(rd->dn);
833 return notifier_from_errno(err);
836 static struct notifier_block pseries_smp_nb = {
837 .notifier_call = pseries_smp_notifier,
840 static int __init pseries_cpu_hotplug_init(void)
845 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
846 ppc_md.cpu_probe = dlpar_cpu_probe;
847 ppc_md.cpu_release = dlpar_cpu_release;
848 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
850 rtas_stop_self_token = rtas_token("stop-self");
851 qcss_tok = rtas_token("query-cpu-stopped-state");
853 if (rtas_stop_self_token == RTAS_UNKNOWN_SERVICE ||
854 qcss_tok == RTAS_UNKNOWN_SERVICE) {
855 printk(KERN_INFO "CPU Hotplug not supported by firmware "
860 smp_ops->cpu_offline_self = pseries_cpu_offline_self;
861 smp_ops->cpu_disable = pseries_cpu_disable;
862 smp_ops->cpu_die = pseries_cpu_die;
864 /* Processors can be added/removed only on LPAR */
865 if (firmware_has_feature(FW_FEATURE_LPAR)) {
866 for_each_node(node) {
867 if (!alloc_cpumask_var_node(&node_recorded_ids_map[node],
871 /* Record ids of CPU added at boot time */
872 cpumask_copy(node_recorded_ids_map[node],
873 cpumask_of_node(node));
876 of_reconfig_notifier_register(&pseries_smp_nb);
881 machine_arch_initcall(pseries, pseries_cpu_hotplug_init);