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>
38 #include "offline_states.h"
40 /* This version can't take the spinlock, because it never returns */
41 static int rtas_stop_self_token = RTAS_UNKNOWN_SERVICE;
43 static DEFINE_PER_CPU(enum cpu_state_vals, preferred_offline_state) =
45 static DEFINE_PER_CPU(enum cpu_state_vals, current_state) = CPU_STATE_OFFLINE;
47 static enum cpu_state_vals default_offline_state = CPU_STATE_OFFLINE;
49 static bool cede_offline_enabled __read_mostly = true;
52 * Enable/disable cede_offline when available.
54 static int __init setup_cede_offline(char *str)
56 return (kstrtobool(str, &cede_offline_enabled) == 0);
59 __setup("cede_offline=", setup_cede_offline);
61 enum cpu_state_vals get_cpu_current_state(int cpu)
63 return per_cpu(current_state, cpu);
66 void set_cpu_current_state(int cpu, enum cpu_state_vals state)
68 per_cpu(current_state, cpu) = state;
71 enum cpu_state_vals get_preferred_offline_state(int cpu)
73 return per_cpu(preferred_offline_state, cpu);
76 void set_preferred_offline_state(int cpu, enum cpu_state_vals state)
78 per_cpu(preferred_offline_state, cpu) = state;
81 void set_default_offline_state(int cpu)
83 per_cpu(preferred_offline_state, cpu) = default_offline_state;
86 static void rtas_stop_self(void)
88 static struct rtas_args args;
92 BUG_ON(rtas_stop_self_token == RTAS_UNKNOWN_SERVICE);
94 printk("cpu %u (hwid %u) Ready to die...\n",
95 smp_processor_id(), hard_smp_processor_id());
97 rtas_call_unlocked(&args, rtas_stop_self_token, 0, 1, NULL);
99 panic("Alas, I survived.\n");
102 static void pseries_mach_cpu_die(void)
104 unsigned int cpu = smp_processor_id();
105 unsigned int hwcpu = hard_smp_processor_id();
106 u8 cede_latency_hint = 0;
115 if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
116 set_cpu_current_state(cpu, CPU_STATE_INACTIVE);
117 if (ppc_md.suspend_disable_cpu)
118 ppc_md.suspend_disable_cpu();
120 cede_latency_hint = 2;
122 get_lppaca()->idle = 1;
123 if (!lppaca_shared_proc(get_lppaca()))
124 get_lppaca()->donate_dedicated_cpu = 1;
126 while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
127 while (!prep_irq_for_idle()) {
132 extended_cede_processor(cede_latency_hint);
137 if (!lppaca_shared_proc(get_lppaca()))
138 get_lppaca()->donate_dedicated_cpu = 0;
139 get_lppaca()->idle = 0;
141 if (get_preferred_offline_state(cpu) == CPU_STATE_ONLINE) {
142 unregister_slb_shadow(hwcpu);
146 * Call to start_secondary_resume() will not return.
147 * Kernel stack will be reset and start_secondary()
148 * will be called to continue the online operation.
150 start_secondary_resume();
154 /* Requested state is CPU_STATE_OFFLINE at this point */
155 WARN_ON(get_preferred_offline_state(cpu) != CPU_STATE_OFFLINE);
157 set_cpu_current_state(cpu, CPU_STATE_OFFLINE);
158 unregister_slb_shadow(hwcpu);
161 /* Should never get here... */
166 static int pseries_cpu_disable(void)
168 int cpu = smp_processor_id();
170 set_cpu_online(cpu, false);
171 vdso_data->processorCount--;
173 /*fix boot_cpuid here*/
174 if (cpu == boot_cpuid)
175 boot_cpuid = cpumask_any(cpu_online_mask);
177 /* FIXME: abstract this to not be platform specific later on */
179 xive_smp_disable_cpu();
181 xics_migrate_irqs_away();
186 * pseries_cpu_die: Wait for the cpu to die.
187 * @cpu: logical processor id of the CPU whose death we're awaiting.
189 * This function is called from the context of the thread which is performing
190 * the cpu-offline. Here we wait for long enough to allow the cpu in question
191 * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
194 * OTOH, pseries_mach_cpu_die() is called by the @cpu when it wants to
197 static void pseries_cpu_die(unsigned int cpu)
201 unsigned int pcpu = get_hard_smp_processor_id(cpu);
203 if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
205 for (tries = 0; tries < 5000; tries++) {
206 if (get_cpu_current_state(cpu) == CPU_STATE_INACTIVE) {
212 } else if (get_preferred_offline_state(cpu) == CPU_STATE_OFFLINE) {
214 for (tries = 0; tries < 25; tries++) {
215 cpu_status = smp_query_cpu_stopped(pcpu);
216 if (cpu_status == QCSS_STOPPED ||
217 cpu_status == QCSS_HARDWARE_ERROR)
223 if (cpu_status != 0) {
224 printk("Querying DEAD? cpu %i (%i) shows %i\n",
225 cpu, pcpu, cpu_status);
228 /* Isolation and deallocation are definitely done by
229 * drslot_chrp_cpu. If they were not they would be
230 * done here. Change isolate state to Isolate and
231 * change allocation-state to Unusable.
233 paca_ptrs[cpu]->cpu_start = 0;
237 * Update cpu_present_mask and paca(s) for a new cpu node. The wrinkle
238 * here is that a cpu device node may represent up to two logical cpus
239 * in the SMT case. We must honor the assumption in other code that
240 * the logical ids for sibling SMT threads x and y are adjacent, such
241 * that x^1 == y and y^1 == x.
243 static int pseries_add_processor(struct device_node *np)
246 cpumask_var_t candidate_mask, tmp;
247 int err = -ENOSPC, len, nthreads, i;
248 const __be32 *intserv;
250 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
254 zalloc_cpumask_var(&candidate_mask, GFP_KERNEL);
255 zalloc_cpumask_var(&tmp, GFP_KERNEL);
257 nthreads = len / sizeof(u32);
258 for (i = 0; i < nthreads; i++)
259 cpumask_set_cpu(i, tmp);
261 cpu_maps_update_begin();
263 BUG_ON(!cpumask_subset(cpu_present_mask, cpu_possible_mask));
265 /* Get a bitmap of unoccupied slots. */
266 cpumask_xor(candidate_mask, cpu_possible_mask, cpu_present_mask);
267 if (cpumask_empty(candidate_mask)) {
268 /* If we get here, it most likely means that NR_CPUS is
269 * less than the partition's max processors setting.
271 printk(KERN_ERR "Cannot add cpu %pOF; this system configuration"
272 " supports %d logical cpus.\n", np,
273 num_possible_cpus());
277 while (!cpumask_empty(tmp))
278 if (cpumask_subset(tmp, candidate_mask))
279 /* Found a range where we can insert the new cpu(s) */
282 cpumask_shift_left(tmp, tmp, nthreads);
284 if (cpumask_empty(tmp)) {
285 printk(KERN_ERR "Unable to find space in cpu_present_mask for"
286 " processor %pOFn with %d thread(s)\n", np,
291 for_each_cpu(cpu, tmp) {
292 BUG_ON(cpu_present(cpu));
293 set_cpu_present(cpu, true);
294 set_hard_smp_processor_id(cpu, be32_to_cpu(*intserv++));
298 cpu_maps_update_done();
299 free_cpumask_var(candidate_mask);
300 free_cpumask_var(tmp);
305 * Update the present map for a cpu node which is going away, and set
306 * the hard id in the paca(s) to -1 to be consistent with boot time
307 * convention for non-present cpus.
309 static void pseries_remove_processor(struct device_node *np)
312 int len, nthreads, i;
313 const __be32 *intserv;
316 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
320 nthreads = len / sizeof(u32);
322 cpu_maps_update_begin();
323 for (i = 0; i < nthreads; i++) {
324 thread = be32_to_cpu(intserv[i]);
325 for_each_present_cpu(cpu) {
326 if (get_hard_smp_processor_id(cpu) != thread)
328 BUG_ON(cpu_online(cpu));
329 set_cpu_present(cpu, false);
330 set_hard_smp_processor_id(cpu, -1);
331 update_numa_cpu_lookup_table(cpu, -1);
334 if (cpu >= nr_cpu_ids)
335 printk(KERN_WARNING "Could not find cpu to remove "
336 "with physical id 0x%x\n", thread);
338 cpu_maps_update_done();
341 static int dlpar_online_cpu(struct device_node *dn)
345 int len, nthreads, i;
346 const __be32 *intserv;
349 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
353 nthreads = len / sizeof(u32);
355 cpu_maps_update_begin();
356 for (i = 0; i < nthreads; i++) {
357 thread = be32_to_cpu(intserv[i]);
358 for_each_present_cpu(cpu) {
359 if (get_hard_smp_processor_id(cpu) != thread)
361 BUG_ON(get_cpu_current_state(cpu)
362 != CPU_STATE_OFFLINE);
363 cpu_maps_update_done();
364 timed_topology_update(1);
365 find_and_online_cpu_nid(cpu);
366 rc = device_online(get_cpu_device(cpu));
369 cpu_maps_update_begin();
373 if (cpu == num_possible_cpus())
374 printk(KERN_WARNING "Could not find cpu to online "
375 "with physical id 0x%x\n", thread);
377 cpu_maps_update_done();
384 static bool dlpar_cpu_exists(struct device_node *parent, u32 drc_index)
386 struct device_node *child = NULL;
391 /* Assume cpu doesn't exist */
394 for_each_child_of_node(parent, child) {
395 rc = of_property_read_u32(child, "ibm,my-drc-index",
400 if (my_drc_index == drc_index) {
410 static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
419 rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
424 if (drc == drc_index)
431 static ssize_t dlpar_cpu_add(u32 drc_index)
433 struct device_node *dn, *parent;
436 pr_debug("Attempting to add CPU, drc index: %x\n", drc_index);
438 parent = of_find_node_by_path("/cpus");
440 pr_warn("Failed to find CPU root node \"/cpus\"\n");
444 if (dlpar_cpu_exists(parent, drc_index)) {
446 pr_warn("CPU with drc index %x already exists\n", drc_index);
450 if (!valid_cpu_drc_index(parent, drc_index)) {
452 pr_warn("Cannot find CPU (drc index %x) to add.\n", drc_index);
456 rc = dlpar_acquire_drc(drc_index);
458 pr_warn("Failed to acquire DRC, rc: %d, drc index: %x\n",
464 dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
466 pr_warn("Failed call to configure-connector, drc index: %x\n",
468 dlpar_release_drc(drc_index);
473 rc = dlpar_attach_node(dn, parent);
475 /* Regardless we are done with parent now */
480 pr_warn("Failed to attach node %pOFn, rc: %d, drc index: %x\n",
483 rc = dlpar_release_drc(drc_index);
485 dlpar_free_cc_nodes(dn);
490 rc = dlpar_online_cpu(dn);
493 pr_warn("Failed to online cpu %pOFn, rc: %d, drc index: %x\n",
496 rc = dlpar_detach_node(dn);
498 dlpar_release_drc(drc_index);
503 pr_debug("Successfully added CPU %pOFn, drc index: %x\n", dn,
508 static int dlpar_offline_cpu(struct device_node *dn)
512 int len, nthreads, i;
513 const __be32 *intserv;
516 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
520 nthreads = len / sizeof(u32);
522 cpu_maps_update_begin();
523 for (i = 0; i < nthreads; i++) {
524 thread = be32_to_cpu(intserv[i]);
525 for_each_present_cpu(cpu) {
526 if (get_hard_smp_processor_id(cpu) != thread)
529 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
532 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
533 set_preferred_offline_state(cpu,
535 cpu_maps_update_done();
536 timed_topology_update(1);
537 rc = device_offline(get_cpu_device(cpu));
540 cpu_maps_update_begin();
546 * The cpu is in CPU_STATE_INACTIVE.
547 * Upgrade it's state to CPU_STATE_OFFLINE.
549 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
550 BUG_ON(plpar_hcall_norets(H_PROD, thread)
555 if (cpu == num_possible_cpus())
556 printk(KERN_WARNING "Could not find cpu to offline with physical id 0x%x\n", thread);
558 cpu_maps_update_done();
565 static ssize_t dlpar_cpu_remove(struct device_node *dn, u32 drc_index)
569 pr_debug("Attempting to remove CPU %pOFn, drc index: %x\n",
572 rc = dlpar_offline_cpu(dn);
574 pr_warn("Failed to offline CPU %pOFn, rc: %d\n", dn, rc);
578 rc = dlpar_release_drc(drc_index);
580 pr_warn("Failed to release drc (%x) for CPU %pOFn, rc: %d\n",
582 dlpar_online_cpu(dn);
586 rc = dlpar_detach_node(dn);
590 pr_warn("Failed to detach CPU %pOFn, rc: %d", dn, rc);
592 rc = dlpar_acquire_drc(drc_index);
594 dlpar_online_cpu(dn);
599 pr_debug("Successfully removed CPU, drc index: %x\n", drc_index);
603 static struct device_node *cpu_drc_index_to_dn(u32 drc_index)
605 struct device_node *dn;
609 for_each_node_by_type(dn, "cpu") {
610 rc = of_property_read_u32(dn, "ibm,my-drc-index", &my_index);
614 if (my_index == drc_index)
621 static int dlpar_cpu_remove_by_index(u32 drc_index)
623 struct device_node *dn;
626 dn = cpu_drc_index_to_dn(drc_index);
628 pr_warn("Cannot find CPU (drc index %x) to remove\n",
633 rc = dlpar_cpu_remove(dn, drc_index);
638 static int find_dlpar_cpus_to_remove(u32 *cpu_drcs, int cpus_to_remove)
640 struct device_node *dn;
644 /* We want to find cpus_to_remove + 1 CPUs to ensure we do not
645 * remove the last CPU.
647 for_each_node_by_type(dn, "cpu") {
650 if (cpus_found > cpus_to_remove) {
655 /* Note that cpus_found is always 1 ahead of the index
656 * into the cpu_drcs array, so we use cpus_found - 1
658 rc = of_property_read_u32(dn, "ibm,my-drc-index",
659 &cpu_drcs[cpus_found - 1]);
661 pr_warn("Error occurred getting drc-index for %pOFn\n",
668 if (cpus_found < cpus_to_remove) {
669 pr_warn("Failed to find enough CPUs (%d of %d) to remove\n",
670 cpus_found, cpus_to_remove);
671 } else if (cpus_found == cpus_to_remove) {
672 pr_warn("Cannot remove all CPUs\n");
678 static int dlpar_cpu_remove_by_count(u32 cpus_to_remove)
682 int cpus_removed = 0;
685 pr_debug("Attempting to hot-remove %d CPUs\n", cpus_to_remove);
687 cpu_drcs = kcalloc(cpus_to_remove, sizeof(*cpu_drcs), GFP_KERNEL);
691 cpus_found = find_dlpar_cpus_to_remove(cpu_drcs, cpus_to_remove);
692 if (cpus_found <= cpus_to_remove) {
697 for (i = 0; i < cpus_to_remove; i++) {
698 rc = dlpar_cpu_remove_by_index(cpu_drcs[i]);
705 if (cpus_removed != cpus_to_remove) {
706 pr_warn("CPU hot-remove failed, adding back removed CPUs\n");
708 for (i = 0; i < cpus_removed; i++)
709 dlpar_cpu_add(cpu_drcs[i]);
720 static int find_dlpar_cpus_to_add(u32 *cpu_drcs, u32 cpus_to_add)
722 struct device_node *parent;
726 parent = of_find_node_by_path("/cpus");
728 pr_warn("Could not find CPU root node in device tree\n");
733 /* Search the ibm,drc-indexes array for possible CPU drcs to
734 * add. Note that the format of the ibm,drc-indexes array is
735 * the number of entries in the array followed by the array
736 * of drc values so we start looking at index = 1.
739 while (cpus_found < cpus_to_add) {
742 rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
747 if (dlpar_cpu_exists(parent, drc))
750 cpu_drcs[cpus_found++] = drc;
757 static int dlpar_cpu_add_by_count(u32 cpus_to_add)
764 pr_debug("Attempting to hot-add %d CPUs\n", cpus_to_add);
766 cpu_drcs = kcalloc(cpus_to_add, sizeof(*cpu_drcs), GFP_KERNEL);
770 cpus_found = find_dlpar_cpus_to_add(cpu_drcs, cpus_to_add);
771 if (cpus_found < cpus_to_add) {
772 pr_warn("Failed to find enough CPUs (%d of %d) to add\n",
773 cpus_found, cpus_to_add);
778 for (i = 0; i < cpus_to_add; i++) {
779 rc = dlpar_cpu_add(cpu_drcs[i]);
786 if (cpus_added < cpus_to_add) {
787 pr_warn("CPU hot-add failed, removing any added CPUs\n");
789 for (i = 0; i < cpus_added; i++)
790 dlpar_cpu_remove_by_index(cpu_drcs[i]);
801 int dlpar_cpu_readd(int cpu)
803 struct device_node *dn;
808 dev = get_cpu_device(cpu);
811 rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
813 rc = dlpar_cpu_remove_by_index(drc_index);
815 rc = dlpar_cpu_add(drc_index);
820 int dlpar_cpu(struct pseries_hp_errorlog *hp_elog)
822 u32 count, drc_index;
825 count = hp_elog->_drc_u.drc_count;
826 drc_index = hp_elog->_drc_u.drc_index;
828 lock_device_hotplug();
830 switch (hp_elog->action) {
831 case PSERIES_HP_ELOG_ACTION_REMOVE:
832 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
833 rc = dlpar_cpu_remove_by_count(count);
834 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
835 rc = dlpar_cpu_remove_by_index(drc_index);
839 case PSERIES_HP_ELOG_ACTION_ADD:
840 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
841 rc = dlpar_cpu_add_by_count(count);
842 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
843 rc = dlpar_cpu_add(drc_index);
848 pr_err("Invalid action (%d) specified\n", hp_elog->action);
853 unlock_device_hotplug();
857 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
859 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
864 rc = kstrtou32(buf, 0, &drc_index);
868 rc = dlpar_cpu_add(drc_index);
870 return rc ? rc : count;
873 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
875 struct device_node *dn;
879 dn = of_find_node_by_path(buf);
883 rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
889 rc = dlpar_cpu_remove(dn, drc_index);
892 return rc ? rc : count;
895 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
897 static int pseries_smp_notifier(struct notifier_block *nb,
898 unsigned long action, void *data)
900 struct of_reconfig_data *rd = data;
904 case OF_RECONFIG_ATTACH_NODE:
905 err = pseries_add_processor(rd->dn);
907 case OF_RECONFIG_DETACH_NODE:
908 pseries_remove_processor(rd->dn);
911 return notifier_from_errno(err);
914 static struct notifier_block pseries_smp_nb = {
915 .notifier_call = pseries_smp_notifier,
918 #define MAX_CEDE_LATENCY_LEVELS 4
919 #define CEDE_LATENCY_PARAM_LENGTH 10
920 #define CEDE_LATENCY_PARAM_MAX_LENGTH \
921 (MAX_CEDE_LATENCY_LEVELS * CEDE_LATENCY_PARAM_LENGTH * sizeof(char))
922 #define CEDE_LATENCY_TOKEN 45
924 static char cede_parameters[CEDE_LATENCY_PARAM_MAX_LENGTH];
926 static int parse_cede_parameters(void)
928 memset(cede_parameters, 0, CEDE_LATENCY_PARAM_MAX_LENGTH);
929 return rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
932 __pa(cede_parameters),
933 CEDE_LATENCY_PARAM_MAX_LENGTH);
936 static int __init pseries_cpu_hotplug_init(void)
941 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
942 ppc_md.cpu_probe = dlpar_cpu_probe;
943 ppc_md.cpu_release = dlpar_cpu_release;
944 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
946 rtas_stop_self_token = rtas_token("stop-self");
947 qcss_tok = rtas_token("query-cpu-stopped-state");
949 if (rtas_stop_self_token == RTAS_UNKNOWN_SERVICE ||
950 qcss_tok == RTAS_UNKNOWN_SERVICE) {
951 printk(KERN_INFO "CPU Hotplug not supported by firmware "
956 ppc_md.cpu_die = pseries_mach_cpu_die;
957 smp_ops->cpu_disable = pseries_cpu_disable;
958 smp_ops->cpu_die = pseries_cpu_die;
960 /* Processors can be added/removed only on LPAR */
961 if (firmware_has_feature(FW_FEATURE_LPAR)) {
962 of_reconfig_notifier_register(&pseries_smp_nb);
963 cpu_maps_update_begin();
964 if (cede_offline_enabled && parse_cede_parameters() == 0) {
965 default_offline_state = CPU_STATE_INACTIVE;
966 for_each_online_cpu(cpu)
967 set_default_offline_state(cpu);
969 cpu_maps_update_done();
974 machine_arch_initcall(pseries, pseries_cpu_hotplug_init);