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);
73 unregister_vpa(hwcpu);
76 /* Should never get here... */
81 static int pseries_cpu_disable(void)
83 int cpu = smp_processor_id();
85 set_cpu_online(cpu, false);
86 vdso_data->processorCount--;
88 /*fix boot_cpuid here*/
89 if (cpu == boot_cpuid)
90 boot_cpuid = cpumask_any(cpu_online_mask);
92 /* FIXME: abstract this to not be platform specific later on */
94 xive_smp_disable_cpu();
96 xics_migrate_irqs_away();
98 cleanup_cpu_mmu_context();
104 * pseries_cpu_die: Wait for the cpu to die.
105 * @cpu: logical processor id of the CPU whose death we're awaiting.
107 * This function is called from the context of the thread which is performing
108 * the cpu-offline. Here we wait for long enough to allow the cpu in question
109 * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
112 * OTOH, pseries_cpu_offline_self() is called by the @cpu when it wants to
115 static void pseries_cpu_die(unsigned int cpu)
118 unsigned int pcpu = get_hard_smp_processor_id(cpu);
119 unsigned long timeout = jiffies + msecs_to_jiffies(120000);
122 cpu_status = smp_query_cpu_stopped(pcpu);
123 if (cpu_status == QCSS_STOPPED ||
124 cpu_status == QCSS_HARDWARE_ERROR)
127 if (time_after(jiffies, timeout)) {
128 pr_warn("CPU %i (hwid %i) didn't die after 120 seconds\n",
130 timeout = jiffies + msecs_to_jiffies(120000);
136 if (cpu_status == QCSS_HARDWARE_ERROR) {
137 pr_warn("CPU %i (hwid %i) reported error while dying\n",
141 paca_ptrs[cpu]->cpu_start = 0;
145 * find_cpu_id_range - found a linear ranger of @nthreads free CPU ids.
146 * @nthreads : the number of threads (cpu ids)
147 * @assigned_node : the node it belongs to or NUMA_NO_NODE if free ids from any
149 * @cpu_mask: the returned CPU mask.
151 * Returns 0 on success.
153 static int find_cpu_id_range(unsigned int nthreads, int assigned_node,
154 cpumask_var_t *cpu_mask)
156 cpumask_var_t candidate_mask;
157 unsigned int cpu, node;
160 if (!zalloc_cpumask_var(&candidate_mask, GFP_KERNEL))
163 cpumask_clear(*cpu_mask);
164 for (cpu = 0; cpu < nthreads; cpu++)
165 cpumask_set_cpu(cpu, *cpu_mask);
167 BUG_ON(!cpumask_subset(cpu_present_mask, cpu_possible_mask));
169 /* Get a bitmap of unoccupied slots. */
170 cpumask_xor(candidate_mask, cpu_possible_mask, cpu_present_mask);
172 if (assigned_node != NUMA_NO_NODE) {
174 * Remove free ids previously assigned on the other nodes. We
175 * can walk only online nodes because once a node became online
176 * it is not turned offlined back.
178 for_each_online_node(node) {
179 if (node == assigned_node)
181 cpumask_andnot(candidate_mask, candidate_mask,
182 node_recorded_ids_map[node]);
186 if (cpumask_empty(candidate_mask))
189 while (!cpumask_empty(*cpu_mask)) {
190 if (cpumask_subset(*cpu_mask, candidate_mask))
191 /* Found a range where we can insert the new cpu(s) */
193 cpumask_shift_left(*cpu_mask, *cpu_mask, nthreads);
196 if (!cpumask_empty(*cpu_mask))
200 free_cpumask_var(candidate_mask);
205 * Update cpu_present_mask and paca(s) for a new cpu node. The wrinkle
206 * here is that a cpu device node may represent multiple logical cpus
207 * in the SMT case. We must honor the assumption in other code that
208 * the logical ids for sibling SMT threads x and y are adjacent, such
209 * that x^1 == y and y^1 == x.
211 static int pseries_add_processor(struct device_node *np)
213 int len, nthreads, node, cpu, assigned_node;
215 cpumask_var_t cpu_mask;
216 const __be32 *intserv;
218 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
222 nthreads = len / sizeof(u32);
224 if (!alloc_cpumask_var(&cpu_mask, GFP_KERNEL))
228 * Fetch from the DT nodes read by dlpar_configure_connector() the NUMA
229 * node id the added CPU belongs to.
231 node = of_node_to_nid(np);
232 if (node < 0 || !node_possible(node))
233 node = first_online_node;
235 BUG_ON(node == NUMA_NO_NODE);
236 assigned_node = node;
238 cpu_maps_update_begin();
240 rc = find_cpu_id_range(nthreads, node, &cpu_mask);
241 if (rc && nr_node_ids > 1) {
243 * Try again, considering the free CPU ids from the other node.
246 rc = find_cpu_id_range(nthreads, NUMA_NO_NODE, &cpu_mask);
250 pr_err("Cannot add cpu %pOF; this system configuration"
251 " supports %d logical cpus.\n", np, num_possible_cpus());
255 for_each_cpu(cpu, cpu_mask) {
256 BUG_ON(cpu_present(cpu));
257 set_cpu_present(cpu, true);
258 set_hard_smp_processor_id(cpu, be32_to_cpu(*intserv++));
261 /* Record the newly used CPU ids for the associate node. */
262 cpumask_or(node_recorded_ids_map[assigned_node],
263 node_recorded_ids_map[assigned_node], cpu_mask);
266 * If node is set to NUMA_NO_NODE, CPU ids have be reused from
267 * another node, remove them from its mask.
269 if (node == NUMA_NO_NODE) {
270 cpu = cpumask_first(cpu_mask);
271 pr_warn("Reusing free CPU ids %d-%d from another node\n",
272 cpu, cpu + nthreads - 1);
273 for_each_online_node(node) {
274 if (node == assigned_node)
276 cpumask_andnot(node_recorded_ids_map[node],
277 node_recorded_ids_map[node],
283 cpu_maps_update_done();
284 free_cpumask_var(cpu_mask);
289 * Update the present map for a cpu node which is going away, and set
290 * the hard id in the paca(s) to -1 to be consistent with boot time
291 * convention for non-present cpus.
293 static void pseries_remove_processor(struct device_node *np)
296 int len, nthreads, i;
297 const __be32 *intserv;
300 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
304 nthreads = len / sizeof(u32);
306 cpu_maps_update_begin();
307 for (i = 0; i < nthreads; i++) {
308 thread = be32_to_cpu(intserv[i]);
309 for_each_present_cpu(cpu) {
310 if (get_hard_smp_processor_id(cpu) != thread)
312 BUG_ON(cpu_online(cpu));
313 set_cpu_present(cpu, false);
314 set_hard_smp_processor_id(cpu, -1);
315 update_numa_cpu_lookup_table(cpu, -1);
318 if (cpu >= nr_cpu_ids)
319 printk(KERN_WARNING "Could not find cpu to remove "
320 "with physical id 0x%x\n", thread);
322 cpu_maps_update_done();
325 static int dlpar_offline_cpu(struct device_node *dn)
329 int len, nthreads, i;
330 const __be32 *intserv;
333 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
337 nthreads = len / sizeof(u32);
339 cpu_maps_update_begin();
340 for (i = 0; i < nthreads; i++) {
341 thread = be32_to_cpu(intserv[i]);
342 for_each_present_cpu(cpu) {
343 if (get_hard_smp_processor_id(cpu) != thread)
346 if (!cpu_online(cpu))
350 * device_offline() will return -EBUSY (via cpu_down()) if there
351 * is only one CPU left. Check it here to fail earlier and with a
352 * more informative error message, while also retaining the
353 * cpu_add_remove_lock to be sure that no CPUs are being
354 * online/offlined during this check.
356 if (num_online_cpus() == 1) {
357 pr_warn("Unable to remove last online CPU %pOFn\n", dn);
362 cpu_maps_update_done();
363 rc = device_offline(get_cpu_device(cpu));
366 cpu_maps_update_begin();
369 if (cpu == num_possible_cpus()) {
370 pr_warn("Could not find cpu to offline with physical id 0x%x\n",
375 cpu_maps_update_done();
381 static int dlpar_online_cpu(struct device_node *dn)
385 int len, nthreads, i;
386 const __be32 *intserv;
389 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
393 nthreads = len / sizeof(u32);
395 cpu_maps_update_begin();
396 for (i = 0; i < nthreads; i++) {
397 thread = be32_to_cpu(intserv[i]);
398 for_each_present_cpu(cpu) {
399 if (get_hard_smp_processor_id(cpu) != thread)
402 if (!topology_is_primary_thread(cpu)) {
403 if (cpu_smt_control != CPU_SMT_ENABLED)
405 if (!topology_smt_thread_allowed(cpu))
409 cpu_maps_update_done();
410 find_and_update_cpu_nid(cpu);
411 rc = device_online(get_cpu_device(cpu));
413 dlpar_offline_cpu(dn);
416 cpu_maps_update_begin();
420 if (cpu == num_possible_cpus())
421 printk(KERN_WARNING "Could not find cpu to online "
422 "with physical id 0x%x\n", thread);
424 cpu_maps_update_done();
431 static bool dlpar_cpu_exists(struct device_node *parent, u32 drc_index)
433 struct device_node *child = NULL;
438 /* Assume cpu doesn't exist */
441 for_each_child_of_node(parent, child) {
442 rc = of_property_read_u32(child, "ibm,my-drc-index",
447 if (my_drc_index == drc_index) {
457 static bool drc_info_valid_index(struct device_node *parent, u32 drc_index)
459 struct property *info;
460 struct of_drc_info drc;
465 info = of_find_property(parent, "ibm,drc-info", NULL);
469 value = of_prop_next_u32(info, NULL, &count);
471 /* First value of ibm,drc-info is number of drc-info records */
477 for (i = 0; i < count; i++) {
478 if (of_read_drc_info_cell(&info, &value, &drc))
481 if (strncmp(drc.drc_type, "CPU", 3))
484 if (drc_index > drc.last_drc_index)
487 index = drc.drc_index_start;
488 for (j = 0; j < drc.num_sequential_elems; j++) {
489 if (drc_index == index)
492 index += drc.sequential_inc;
499 static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
504 if (of_property_present(parent, "ibm,drc-info"))
505 return drc_info_valid_index(parent, drc_index);
507 /* Note that the format of the ibm,drc-indexes array is
508 * the number of entries in the array followed by the array
509 * of drc values so we start looking at index = 1.
515 rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
521 if (drc == drc_index)
528 static int pseries_cpuhp_attach_nodes(struct device_node *dn)
530 struct of_changeset cs;
534 * This device node is unattached but may have siblings; open-code the
537 for (of_changeset_init(&cs); dn != NULL; dn = dn->sibling) {
538 ret = of_changeset_attach_node(&cs, dn);
543 ret = of_changeset_apply(&cs);
545 of_changeset_destroy(&cs);
549 static ssize_t dlpar_cpu_add(u32 drc_index)
551 struct device_node *dn, *parent;
554 pr_debug("Attempting to add CPU, drc index: %x\n", drc_index);
556 parent = of_find_node_by_path("/cpus");
558 pr_warn("Failed to find CPU root node \"/cpus\"\n");
562 if (dlpar_cpu_exists(parent, drc_index)) {
564 pr_warn("CPU with drc index %x already exists\n", drc_index);
568 if (!valid_cpu_drc_index(parent, drc_index)) {
570 pr_warn("Cannot find CPU (drc index %x) to add.\n", drc_index);
574 rc = dlpar_acquire_drc(drc_index);
576 pr_warn("Failed to acquire DRC, rc: %d, drc index: %x\n",
582 dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
584 pr_warn("Failed call to configure-connector, drc index: %x\n",
586 dlpar_release_drc(drc_index);
591 rc = pseries_cpuhp_attach_nodes(dn);
593 /* Regardless we are done with parent now */
598 pr_warn("Failed to attach node %pOFn, rc: %d, drc index: %x\n",
601 rc = dlpar_release_drc(drc_index);
603 dlpar_free_cc_nodes(dn);
608 update_numa_distance(dn);
610 rc = dlpar_online_cpu(dn);
613 pr_warn("Failed to online cpu %pOFn, rc: %d, drc index: %x\n",
616 rc = dlpar_detach_node(dn);
618 dlpar_release_drc(drc_index);
623 pr_debug("Successfully added CPU %pOFn, drc index: %x\n", dn,
628 static unsigned int pseries_cpuhp_cache_use_count(const struct device_node *cachedn)
630 unsigned int use_count = 0;
631 struct device_node *dn, *tn;
633 WARN_ON(!of_node_is_type(cachedn, "cache"));
635 for_each_of_cpu_node(dn) {
636 tn = of_find_next_cache_node(dn);
642 for_each_node_by_type(dn, "cache") {
643 tn = of_find_next_cache_node(dn);
652 static int pseries_cpuhp_detach_nodes(struct device_node *cpudn)
654 struct device_node *dn;
655 struct of_changeset cs;
658 of_changeset_init(&cs);
659 ret = of_changeset_detach_node(&cs, cpudn);
664 while ((dn = of_find_next_cache_node(dn))) {
665 if (pseries_cpuhp_cache_use_count(dn) > 1) {
670 ret = of_changeset_detach_node(&cs, dn);
676 ret = of_changeset_apply(&cs);
678 of_changeset_destroy(&cs);
682 static ssize_t dlpar_cpu_remove(struct device_node *dn, u32 drc_index)
686 pr_debug("Attempting to remove CPU %pOFn, drc index: %x\n",
689 rc = dlpar_offline_cpu(dn);
691 pr_warn("Failed to offline CPU %pOFn, rc: %d\n", dn, rc);
695 rc = dlpar_release_drc(drc_index);
697 pr_warn("Failed to release drc (%x) for CPU %pOFn, rc: %d\n",
699 dlpar_online_cpu(dn);
703 rc = pseries_cpuhp_detach_nodes(dn);
707 pr_warn("Failed to detach CPU %pOFn, rc: %d", dn, rc);
709 rc = dlpar_acquire_drc(drc_index);
711 dlpar_online_cpu(dn);
716 pr_debug("Successfully removed CPU, drc index: %x\n", drc_index);
720 static struct device_node *cpu_drc_index_to_dn(u32 drc_index)
722 struct device_node *dn;
726 for_each_node_by_type(dn, "cpu") {
727 rc = of_property_read_u32(dn, "ibm,my-drc-index", &my_index);
731 if (my_index == drc_index)
738 static int dlpar_cpu_remove_by_index(u32 drc_index)
740 struct device_node *dn;
743 dn = cpu_drc_index_to_dn(drc_index);
745 pr_warn("Cannot find CPU (drc index %x) to remove\n",
750 rc = dlpar_cpu_remove(dn, drc_index);
755 int dlpar_cpu(struct pseries_hp_errorlog *hp_elog)
760 drc_index = hp_elog->_drc_u.drc_index;
762 lock_device_hotplug();
764 switch (hp_elog->action) {
765 case PSERIES_HP_ELOG_ACTION_REMOVE:
766 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
767 rc = dlpar_cpu_remove_by_index(drc_index);
769 * Setting the isolation state of an UNISOLATED/CONFIGURED
770 * device to UNISOLATE is a no-op, but the hypervisor can
771 * use it as a hint that the CPU removal failed.
774 dlpar_unisolate_drc(drc_index);
779 case PSERIES_HP_ELOG_ACTION_ADD:
780 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
781 rc = dlpar_cpu_add(drc_index);
786 pr_err("Invalid action (%d) specified\n", hp_elog->action);
791 unlock_device_hotplug();
795 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
797 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
802 rc = kstrtou32(buf, 0, &drc_index);
806 rc = dlpar_cpu_add(drc_index);
808 return rc ? rc : count;
811 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
813 struct device_node *dn;
817 dn = of_find_node_by_path(buf);
821 rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
827 rc = dlpar_cpu_remove(dn, drc_index);
830 return rc ? rc : count;
833 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
835 static int pseries_smp_notifier(struct notifier_block *nb,
836 unsigned long action, void *data)
838 struct of_reconfig_data *rd = data;
842 case OF_RECONFIG_ATTACH_NODE:
843 err = pseries_add_processor(rd->dn);
845 case OF_RECONFIG_DETACH_NODE:
846 pseries_remove_processor(rd->dn);
849 return notifier_from_errno(err);
852 static struct notifier_block pseries_smp_nb = {
853 .notifier_call = pseries_smp_notifier,
856 void __init pseries_cpu_hotplug_init(void)
860 rtas_stop_self_token = rtas_function_token(RTAS_FN_STOP_SELF);
861 qcss_tok = rtas_function_token(RTAS_FN_QUERY_CPU_STOPPED_STATE);
863 if (rtas_stop_self_token == RTAS_UNKNOWN_SERVICE ||
864 qcss_tok == RTAS_UNKNOWN_SERVICE) {
865 printk(KERN_INFO "CPU Hotplug not supported by firmware "
870 smp_ops->cpu_offline_self = pseries_cpu_offline_self;
871 smp_ops->cpu_disable = pseries_cpu_disable;
872 smp_ops->cpu_die = pseries_cpu_die;
875 static int __init pseries_dlpar_init(void)
879 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
880 ppc_md.cpu_probe = dlpar_cpu_probe;
881 ppc_md.cpu_release = dlpar_cpu_release;
882 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
884 /* Processors can be added/removed only on LPAR */
885 if (firmware_has_feature(FW_FEATURE_LPAR)) {
886 for_each_node(node) {
887 if (!alloc_cpumask_var_node(&node_recorded_ids_map[node],
891 /* Record ids of CPU added at boot time */
892 cpumask_copy(node_recorded_ids_map[node],
893 cpumask_of_node(node));
896 of_reconfig_notifier_register(&pseries_smp_nb);
901 machine_arch_initcall(pseries, pseries_dlpar_init);