xref: /OK3568_Linux_fs/kernel/arch/powerpc/platforms/pseries/hotplug-cpu.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * pseries CPU Hotplug infrastructure.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Split out from arch/powerpc/platforms/pseries/setup.c
6*4882a593Smuzhiyun  *  arch/powerpc/kernel/rtas.c, and arch/powerpc/platforms/pseries/smp.c
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Peter Bergner, IBM	March 2001.
9*4882a593Smuzhiyun  * Copyright (C) 2001 IBM.
10*4882a593Smuzhiyun  * Dave Engebretsen, Peter Bergner, and
11*4882a593Smuzhiyun  * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
12*4882a593Smuzhiyun  * Plus various changes from other IBM teams...
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Copyright (C) 2006 Michael Ellerman, IBM Corporation
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #define pr_fmt(fmt)     "pseries-hotplug-cpu: " fmt
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <linux/kernel.h>
20*4882a593Smuzhiyun #include <linux/interrupt.h>
21*4882a593Smuzhiyun #include <linux/delay.h>
22*4882a593Smuzhiyun #include <linux/sched.h>	/* for idle_task_exit */
23*4882a593Smuzhiyun #include <linux/sched/hotplug.h>
24*4882a593Smuzhiyun #include <linux/cpu.h>
25*4882a593Smuzhiyun #include <linux/of.h>
26*4882a593Smuzhiyun #include <linux/slab.h>
27*4882a593Smuzhiyun #include <asm/prom.h>
28*4882a593Smuzhiyun #include <asm/rtas.h>
29*4882a593Smuzhiyun #include <asm/firmware.h>
30*4882a593Smuzhiyun #include <asm/machdep.h>
31*4882a593Smuzhiyun #include <asm/vdso_datapage.h>
32*4882a593Smuzhiyun #include <asm/xics.h>
33*4882a593Smuzhiyun #include <asm/xive.h>
34*4882a593Smuzhiyun #include <asm/plpar_wrappers.h>
35*4882a593Smuzhiyun #include <asm/topology.h>
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include "pseries.h"
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /* This version can't take the spinlock, because it never returns */
40*4882a593Smuzhiyun static int rtas_stop_self_token = RTAS_UNKNOWN_SERVICE;
41*4882a593Smuzhiyun 
rtas_stop_self(void)42*4882a593Smuzhiyun static void rtas_stop_self(void)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	static struct rtas_args args;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	local_irq_disable();
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	BUG_ON(rtas_stop_self_token == RTAS_UNKNOWN_SERVICE);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	rtas_call_unlocked(&args, rtas_stop_self_token, 0, 1, NULL);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	panic("Alas, I survived.\n");
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
pseries_cpu_offline_self(void)55*4882a593Smuzhiyun static void pseries_cpu_offline_self(void)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	unsigned int hwcpu = hard_smp_processor_id();
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	local_irq_disable();
60*4882a593Smuzhiyun 	idle_task_exit();
61*4882a593Smuzhiyun 	if (xive_enabled())
62*4882a593Smuzhiyun 		xive_teardown_cpu();
63*4882a593Smuzhiyun 	else
64*4882a593Smuzhiyun 		xics_teardown_cpu();
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	unregister_slb_shadow(hwcpu);
67*4882a593Smuzhiyun 	rtas_stop_self();
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	/* Should never get here... */
70*4882a593Smuzhiyun 	BUG();
71*4882a593Smuzhiyun 	for(;;);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
pseries_cpu_disable(void)74*4882a593Smuzhiyun static int pseries_cpu_disable(void)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	int cpu = smp_processor_id();
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	set_cpu_online(cpu, false);
79*4882a593Smuzhiyun 	vdso_data->processorCount--;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/*fix boot_cpuid here*/
82*4882a593Smuzhiyun 	if (cpu == boot_cpuid)
83*4882a593Smuzhiyun 		boot_cpuid = cpumask_any(cpu_online_mask);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	/* FIXME: abstract this to not be platform specific later on */
86*4882a593Smuzhiyun 	if (xive_enabled())
87*4882a593Smuzhiyun 		xive_smp_disable_cpu();
88*4882a593Smuzhiyun 	else
89*4882a593Smuzhiyun 		xics_migrate_irqs_away();
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	cleanup_cpu_mmu_context();
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	return 0;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun  * pseries_cpu_die: Wait for the cpu to die.
98*4882a593Smuzhiyun  * @cpu: logical processor id of the CPU whose death we're awaiting.
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  * This function is called from the context of the thread which is performing
101*4882a593Smuzhiyun  * the cpu-offline. Here we wait for long enough to allow the cpu in question
102*4882a593Smuzhiyun  * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
103*4882a593Smuzhiyun  * notifications.
104*4882a593Smuzhiyun  *
105*4882a593Smuzhiyun  * OTOH, pseries_cpu_offline_self() is called by the @cpu when it wants to
106*4882a593Smuzhiyun  * self-destruct.
107*4882a593Smuzhiyun  */
pseries_cpu_die(unsigned int cpu)108*4882a593Smuzhiyun static void pseries_cpu_die(unsigned int cpu)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	int cpu_status = 1;
111*4882a593Smuzhiyun 	unsigned int pcpu = get_hard_smp_processor_id(cpu);
112*4882a593Smuzhiyun 	unsigned long timeout = jiffies + msecs_to_jiffies(120000);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	while (true) {
115*4882a593Smuzhiyun 		cpu_status = smp_query_cpu_stopped(pcpu);
116*4882a593Smuzhiyun 		if (cpu_status == QCSS_STOPPED ||
117*4882a593Smuzhiyun 		    cpu_status == QCSS_HARDWARE_ERROR)
118*4882a593Smuzhiyun 			break;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 		if (time_after(jiffies, timeout)) {
121*4882a593Smuzhiyun 			pr_warn("CPU %i (hwid %i) didn't die after 120 seconds\n",
122*4882a593Smuzhiyun 				cpu, pcpu);
123*4882a593Smuzhiyun 			timeout = jiffies + msecs_to_jiffies(120000);
124*4882a593Smuzhiyun 		}
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 		cond_resched();
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	if (cpu_status == QCSS_HARDWARE_ERROR) {
130*4882a593Smuzhiyun 		pr_warn("CPU %i (hwid %i) reported error while dying\n",
131*4882a593Smuzhiyun 			cpu, pcpu);
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	/* Isolation and deallocation are definitely done by
135*4882a593Smuzhiyun 	 * drslot_chrp_cpu.  If they were not they would be
136*4882a593Smuzhiyun 	 * done here.  Change isolate state to Isolate and
137*4882a593Smuzhiyun 	 * change allocation-state to Unusable.
138*4882a593Smuzhiyun 	 */
139*4882a593Smuzhiyun 	paca_ptrs[cpu]->cpu_start = 0;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun  * Update cpu_present_mask and paca(s) for a new cpu node.  The wrinkle
144*4882a593Smuzhiyun  * here is that a cpu device node may represent up to two logical cpus
145*4882a593Smuzhiyun  * in the SMT case.  We must honor the assumption in other code that
146*4882a593Smuzhiyun  * the logical ids for sibling SMT threads x and y are adjacent, such
147*4882a593Smuzhiyun  * that x^1 == y and y^1 == x.
148*4882a593Smuzhiyun  */
pseries_add_processor(struct device_node * np)149*4882a593Smuzhiyun static int pseries_add_processor(struct device_node *np)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	unsigned int cpu;
152*4882a593Smuzhiyun 	cpumask_var_t candidate_mask, tmp;
153*4882a593Smuzhiyun 	int err = -ENOSPC, len, nthreads, i;
154*4882a593Smuzhiyun 	const __be32 *intserv;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
157*4882a593Smuzhiyun 	if (!intserv)
158*4882a593Smuzhiyun 		return 0;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	zalloc_cpumask_var(&candidate_mask, GFP_KERNEL);
161*4882a593Smuzhiyun 	zalloc_cpumask_var(&tmp, GFP_KERNEL);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	nthreads = len / sizeof(u32);
164*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++)
165*4882a593Smuzhiyun 		cpumask_set_cpu(i, tmp);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	cpu_maps_update_begin();
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	BUG_ON(!cpumask_subset(cpu_present_mask, cpu_possible_mask));
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/* Get a bitmap of unoccupied slots. */
172*4882a593Smuzhiyun 	cpumask_xor(candidate_mask, cpu_possible_mask, cpu_present_mask);
173*4882a593Smuzhiyun 	if (cpumask_empty(candidate_mask)) {
174*4882a593Smuzhiyun 		/* If we get here, it most likely means that NR_CPUS is
175*4882a593Smuzhiyun 		 * less than the partition's max processors setting.
176*4882a593Smuzhiyun 		 */
177*4882a593Smuzhiyun 		printk(KERN_ERR "Cannot add cpu %pOF; this system configuration"
178*4882a593Smuzhiyun 		       " supports %d logical cpus.\n", np,
179*4882a593Smuzhiyun 		       num_possible_cpus());
180*4882a593Smuzhiyun 		goto out_unlock;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	while (!cpumask_empty(tmp))
184*4882a593Smuzhiyun 		if (cpumask_subset(tmp, candidate_mask))
185*4882a593Smuzhiyun 			/* Found a range where we can insert the new cpu(s) */
186*4882a593Smuzhiyun 			break;
187*4882a593Smuzhiyun 		else
188*4882a593Smuzhiyun 			cpumask_shift_left(tmp, tmp, nthreads);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (cpumask_empty(tmp)) {
191*4882a593Smuzhiyun 		printk(KERN_ERR "Unable to find space in cpu_present_mask for"
192*4882a593Smuzhiyun 		       " processor %pOFn with %d thread(s)\n", np,
193*4882a593Smuzhiyun 		       nthreads);
194*4882a593Smuzhiyun 		goto out_unlock;
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	for_each_cpu(cpu, tmp) {
198*4882a593Smuzhiyun 		BUG_ON(cpu_present(cpu));
199*4882a593Smuzhiyun 		set_cpu_present(cpu, true);
200*4882a593Smuzhiyun 		set_hard_smp_processor_id(cpu, be32_to_cpu(*intserv++));
201*4882a593Smuzhiyun 	}
202*4882a593Smuzhiyun 	err = 0;
203*4882a593Smuzhiyun out_unlock:
204*4882a593Smuzhiyun 	cpu_maps_update_done();
205*4882a593Smuzhiyun 	free_cpumask_var(candidate_mask);
206*4882a593Smuzhiyun 	free_cpumask_var(tmp);
207*4882a593Smuzhiyun 	return err;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /*
211*4882a593Smuzhiyun  * Update the present map for a cpu node which is going away, and set
212*4882a593Smuzhiyun  * the hard id in the paca(s) to -1 to be consistent with boot time
213*4882a593Smuzhiyun  * convention for non-present cpus.
214*4882a593Smuzhiyun  */
pseries_remove_processor(struct device_node * np)215*4882a593Smuzhiyun static void pseries_remove_processor(struct device_node *np)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	unsigned int cpu;
218*4882a593Smuzhiyun 	int len, nthreads, i;
219*4882a593Smuzhiyun 	const __be32 *intserv;
220*4882a593Smuzhiyun 	u32 thread;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
223*4882a593Smuzhiyun 	if (!intserv)
224*4882a593Smuzhiyun 		return;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	nthreads = len / sizeof(u32);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	cpu_maps_update_begin();
229*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
230*4882a593Smuzhiyun 		thread = be32_to_cpu(intserv[i]);
231*4882a593Smuzhiyun 		for_each_present_cpu(cpu) {
232*4882a593Smuzhiyun 			if (get_hard_smp_processor_id(cpu) != thread)
233*4882a593Smuzhiyun 				continue;
234*4882a593Smuzhiyun 			BUG_ON(cpu_online(cpu));
235*4882a593Smuzhiyun 			set_cpu_present(cpu, false);
236*4882a593Smuzhiyun 			set_hard_smp_processor_id(cpu, -1);
237*4882a593Smuzhiyun 			update_numa_cpu_lookup_table(cpu, -1);
238*4882a593Smuzhiyun 			break;
239*4882a593Smuzhiyun 		}
240*4882a593Smuzhiyun 		if (cpu >= nr_cpu_ids)
241*4882a593Smuzhiyun 			printk(KERN_WARNING "Could not find cpu to remove "
242*4882a593Smuzhiyun 			       "with physical id 0x%x\n", thread);
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 	cpu_maps_update_done();
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun 
dlpar_offline_cpu(struct device_node * dn)247*4882a593Smuzhiyun static int dlpar_offline_cpu(struct device_node *dn)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun 	int rc = 0;
250*4882a593Smuzhiyun 	unsigned int cpu;
251*4882a593Smuzhiyun 	int len, nthreads, i;
252*4882a593Smuzhiyun 	const __be32 *intserv;
253*4882a593Smuzhiyun 	u32 thread;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
256*4882a593Smuzhiyun 	if (!intserv)
257*4882a593Smuzhiyun 		return -EINVAL;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	nthreads = len / sizeof(u32);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	cpu_maps_update_begin();
262*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
263*4882a593Smuzhiyun 		thread = be32_to_cpu(intserv[i]);
264*4882a593Smuzhiyun 		for_each_present_cpu(cpu) {
265*4882a593Smuzhiyun 			if (get_hard_smp_processor_id(cpu) != thread)
266*4882a593Smuzhiyun 				continue;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 			if (!cpu_online(cpu))
269*4882a593Smuzhiyun 				break;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 			cpu_maps_update_done();
272*4882a593Smuzhiyun 			rc = device_offline(get_cpu_device(cpu));
273*4882a593Smuzhiyun 			if (rc)
274*4882a593Smuzhiyun 				goto out;
275*4882a593Smuzhiyun 			cpu_maps_update_begin();
276*4882a593Smuzhiyun 			break;
277*4882a593Smuzhiyun 		}
278*4882a593Smuzhiyun 		if (cpu == num_possible_cpus()) {
279*4882a593Smuzhiyun 			pr_warn("Could not find cpu to offline with physical id 0x%x\n",
280*4882a593Smuzhiyun 				thread);
281*4882a593Smuzhiyun 		}
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 	cpu_maps_update_done();
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun out:
286*4882a593Smuzhiyun 	return rc;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
dlpar_online_cpu(struct device_node * dn)289*4882a593Smuzhiyun static int dlpar_online_cpu(struct device_node *dn)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	int rc = 0;
292*4882a593Smuzhiyun 	unsigned int cpu;
293*4882a593Smuzhiyun 	int len, nthreads, i;
294*4882a593Smuzhiyun 	const __be32 *intserv;
295*4882a593Smuzhiyun 	u32 thread;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
298*4882a593Smuzhiyun 	if (!intserv)
299*4882a593Smuzhiyun 		return -EINVAL;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	nthreads = len / sizeof(u32);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	cpu_maps_update_begin();
304*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
305*4882a593Smuzhiyun 		thread = be32_to_cpu(intserv[i]);
306*4882a593Smuzhiyun 		for_each_present_cpu(cpu) {
307*4882a593Smuzhiyun 			if (get_hard_smp_processor_id(cpu) != thread)
308*4882a593Smuzhiyun 				continue;
309*4882a593Smuzhiyun 			cpu_maps_update_done();
310*4882a593Smuzhiyun 			find_and_online_cpu_nid(cpu);
311*4882a593Smuzhiyun 			rc = device_online(get_cpu_device(cpu));
312*4882a593Smuzhiyun 			if (rc) {
313*4882a593Smuzhiyun 				dlpar_offline_cpu(dn);
314*4882a593Smuzhiyun 				goto out;
315*4882a593Smuzhiyun 			}
316*4882a593Smuzhiyun 			cpu_maps_update_begin();
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 			break;
319*4882a593Smuzhiyun 		}
320*4882a593Smuzhiyun 		if (cpu == num_possible_cpus())
321*4882a593Smuzhiyun 			printk(KERN_WARNING "Could not find cpu to online "
322*4882a593Smuzhiyun 			       "with physical id 0x%x\n", thread);
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 	cpu_maps_update_done();
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun out:
327*4882a593Smuzhiyun 	return rc;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
dlpar_cpu_exists(struct device_node * parent,u32 drc_index)331*4882a593Smuzhiyun static bool dlpar_cpu_exists(struct device_node *parent, u32 drc_index)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	struct device_node *child = NULL;
334*4882a593Smuzhiyun 	u32 my_drc_index;
335*4882a593Smuzhiyun 	bool found;
336*4882a593Smuzhiyun 	int rc;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	/* Assume cpu doesn't exist */
339*4882a593Smuzhiyun 	found = false;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	for_each_child_of_node(parent, child) {
342*4882a593Smuzhiyun 		rc = of_property_read_u32(child, "ibm,my-drc-index",
343*4882a593Smuzhiyun 					  &my_drc_index);
344*4882a593Smuzhiyun 		if (rc)
345*4882a593Smuzhiyun 			continue;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 		if (my_drc_index == drc_index) {
348*4882a593Smuzhiyun 			of_node_put(child);
349*4882a593Smuzhiyun 			found = true;
350*4882a593Smuzhiyun 			break;
351*4882a593Smuzhiyun 		}
352*4882a593Smuzhiyun 	}
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	return found;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
drc_info_valid_index(struct device_node * parent,u32 drc_index)357*4882a593Smuzhiyun static bool drc_info_valid_index(struct device_node *parent, u32 drc_index)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	struct property *info;
360*4882a593Smuzhiyun 	struct of_drc_info drc;
361*4882a593Smuzhiyun 	const __be32 *value;
362*4882a593Smuzhiyun 	u32 index;
363*4882a593Smuzhiyun 	int count, i, j;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	info = of_find_property(parent, "ibm,drc-info", NULL);
366*4882a593Smuzhiyun 	if (!info)
367*4882a593Smuzhiyun 		return false;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	value = of_prop_next_u32(info, NULL, &count);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	/* First value of ibm,drc-info is number of drc-info records */
372*4882a593Smuzhiyun 	if (value)
373*4882a593Smuzhiyun 		value++;
374*4882a593Smuzhiyun 	else
375*4882a593Smuzhiyun 		return false;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	for (i = 0; i < count; i++) {
378*4882a593Smuzhiyun 		if (of_read_drc_info_cell(&info, &value, &drc))
379*4882a593Smuzhiyun 			return false;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 		if (strncmp(drc.drc_type, "CPU", 3))
382*4882a593Smuzhiyun 			break;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 		if (drc_index > drc.last_drc_index)
385*4882a593Smuzhiyun 			continue;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 		index = drc.drc_index_start;
388*4882a593Smuzhiyun 		for (j = 0; j < drc.num_sequential_elems; j++) {
389*4882a593Smuzhiyun 			if (drc_index == index)
390*4882a593Smuzhiyun 				return true;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 			index += drc.sequential_inc;
393*4882a593Smuzhiyun 		}
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	return false;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
valid_cpu_drc_index(struct device_node * parent,u32 drc_index)399*4882a593Smuzhiyun static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	bool found = false;
402*4882a593Smuzhiyun 	int rc, index;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	if (of_find_property(parent, "ibm,drc-info", NULL))
405*4882a593Smuzhiyun 		return drc_info_valid_index(parent, drc_index);
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	/* Note that the format of the ibm,drc-indexes array is
408*4882a593Smuzhiyun 	 * the number of entries in the array followed by the array
409*4882a593Smuzhiyun 	 * of drc values so we start looking at index = 1.
410*4882a593Smuzhiyun 	 */
411*4882a593Smuzhiyun 	index = 1;
412*4882a593Smuzhiyun 	while (!found) {
413*4882a593Smuzhiyun 		u32 drc;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 		rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
416*4882a593Smuzhiyun 						index++, &drc);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 		if (rc)
419*4882a593Smuzhiyun 			break;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 		if (drc == drc_index)
422*4882a593Smuzhiyun 			found = true;
423*4882a593Smuzhiyun 	}
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	return found;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun 
dlpar_cpu_add(u32 drc_index)428*4882a593Smuzhiyun static ssize_t dlpar_cpu_add(u32 drc_index)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	struct device_node *dn, *parent;
431*4882a593Smuzhiyun 	int rc, saved_rc;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	pr_debug("Attempting to add CPU, drc index: %x\n", drc_index);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	parent = of_find_node_by_path("/cpus");
436*4882a593Smuzhiyun 	if (!parent) {
437*4882a593Smuzhiyun 		pr_warn("Failed to find CPU root node \"/cpus\"\n");
438*4882a593Smuzhiyun 		return -ENODEV;
439*4882a593Smuzhiyun 	}
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	if (dlpar_cpu_exists(parent, drc_index)) {
442*4882a593Smuzhiyun 		of_node_put(parent);
443*4882a593Smuzhiyun 		pr_warn("CPU with drc index %x already exists\n", drc_index);
444*4882a593Smuzhiyun 		return -EINVAL;
445*4882a593Smuzhiyun 	}
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	if (!valid_cpu_drc_index(parent, drc_index)) {
448*4882a593Smuzhiyun 		of_node_put(parent);
449*4882a593Smuzhiyun 		pr_warn("Cannot find CPU (drc index %x) to add.\n", drc_index);
450*4882a593Smuzhiyun 		return -EINVAL;
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	rc = dlpar_acquire_drc(drc_index);
454*4882a593Smuzhiyun 	if (rc) {
455*4882a593Smuzhiyun 		pr_warn("Failed to acquire DRC, rc: %d, drc index: %x\n",
456*4882a593Smuzhiyun 			rc, drc_index);
457*4882a593Smuzhiyun 		of_node_put(parent);
458*4882a593Smuzhiyun 		return -EINVAL;
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
462*4882a593Smuzhiyun 	if (!dn) {
463*4882a593Smuzhiyun 		pr_warn("Failed call to configure-connector, drc index: %x\n",
464*4882a593Smuzhiyun 			drc_index);
465*4882a593Smuzhiyun 		dlpar_release_drc(drc_index);
466*4882a593Smuzhiyun 		of_node_put(parent);
467*4882a593Smuzhiyun 		return -EINVAL;
468*4882a593Smuzhiyun 	}
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	rc = dlpar_attach_node(dn, parent);
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	/* Regardless we are done with parent now */
473*4882a593Smuzhiyun 	of_node_put(parent);
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	if (rc) {
476*4882a593Smuzhiyun 		saved_rc = rc;
477*4882a593Smuzhiyun 		pr_warn("Failed to attach node %pOFn, rc: %d, drc index: %x\n",
478*4882a593Smuzhiyun 			dn, rc, drc_index);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 		rc = dlpar_release_drc(drc_index);
481*4882a593Smuzhiyun 		if (!rc)
482*4882a593Smuzhiyun 			dlpar_free_cc_nodes(dn);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 		return saved_rc;
485*4882a593Smuzhiyun 	}
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	rc = dlpar_online_cpu(dn);
488*4882a593Smuzhiyun 	if (rc) {
489*4882a593Smuzhiyun 		saved_rc = rc;
490*4882a593Smuzhiyun 		pr_warn("Failed to online cpu %pOFn, rc: %d, drc index: %x\n",
491*4882a593Smuzhiyun 			dn, rc, drc_index);
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 		rc = dlpar_detach_node(dn);
494*4882a593Smuzhiyun 		if (!rc)
495*4882a593Smuzhiyun 			dlpar_release_drc(drc_index);
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 		return saved_rc;
498*4882a593Smuzhiyun 	}
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	pr_debug("Successfully added CPU %pOFn, drc index: %x\n", dn,
501*4882a593Smuzhiyun 		 drc_index);
502*4882a593Smuzhiyun 	return rc;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun 
dlpar_cpu_remove(struct device_node * dn,u32 drc_index)505*4882a593Smuzhiyun static ssize_t dlpar_cpu_remove(struct device_node *dn, u32 drc_index)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun 	int rc;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	pr_debug("Attempting to remove CPU %pOFn, drc index: %x\n",
510*4882a593Smuzhiyun 		 dn, drc_index);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	rc = dlpar_offline_cpu(dn);
513*4882a593Smuzhiyun 	if (rc) {
514*4882a593Smuzhiyun 		pr_warn("Failed to offline CPU %pOFn, rc: %d\n", dn, rc);
515*4882a593Smuzhiyun 		return -EINVAL;
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	rc = dlpar_release_drc(drc_index);
519*4882a593Smuzhiyun 	if (rc) {
520*4882a593Smuzhiyun 		pr_warn("Failed to release drc (%x) for CPU %pOFn, rc: %d\n",
521*4882a593Smuzhiyun 			drc_index, dn, rc);
522*4882a593Smuzhiyun 		dlpar_online_cpu(dn);
523*4882a593Smuzhiyun 		return rc;
524*4882a593Smuzhiyun 	}
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	rc = dlpar_detach_node(dn);
527*4882a593Smuzhiyun 	if (rc) {
528*4882a593Smuzhiyun 		int saved_rc = rc;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 		pr_warn("Failed to detach CPU %pOFn, rc: %d", dn, rc);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 		rc = dlpar_acquire_drc(drc_index);
533*4882a593Smuzhiyun 		if (!rc)
534*4882a593Smuzhiyun 			dlpar_online_cpu(dn);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 		return saved_rc;
537*4882a593Smuzhiyun 	}
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	pr_debug("Successfully removed CPU, drc index: %x\n", drc_index);
540*4882a593Smuzhiyun 	return 0;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
cpu_drc_index_to_dn(u32 drc_index)543*4882a593Smuzhiyun static struct device_node *cpu_drc_index_to_dn(u32 drc_index)
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun 	struct device_node *dn;
546*4882a593Smuzhiyun 	u32 my_index;
547*4882a593Smuzhiyun 	int rc;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	for_each_node_by_type(dn, "cpu") {
550*4882a593Smuzhiyun 		rc = of_property_read_u32(dn, "ibm,my-drc-index", &my_index);
551*4882a593Smuzhiyun 		if (rc)
552*4882a593Smuzhiyun 			continue;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 		if (my_index == drc_index)
555*4882a593Smuzhiyun 			break;
556*4882a593Smuzhiyun 	}
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	return dn;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun 
dlpar_cpu_remove_by_index(u32 drc_index)561*4882a593Smuzhiyun static int dlpar_cpu_remove_by_index(u32 drc_index)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun 	struct device_node *dn;
564*4882a593Smuzhiyun 	int rc;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	dn = cpu_drc_index_to_dn(drc_index);
567*4882a593Smuzhiyun 	if (!dn) {
568*4882a593Smuzhiyun 		pr_warn("Cannot find CPU (drc index %x) to remove\n",
569*4882a593Smuzhiyun 			drc_index);
570*4882a593Smuzhiyun 		return -ENODEV;
571*4882a593Smuzhiyun 	}
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	rc = dlpar_cpu_remove(dn, drc_index);
574*4882a593Smuzhiyun 	of_node_put(dn);
575*4882a593Smuzhiyun 	return rc;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun 
find_dlpar_cpus_to_remove(u32 * cpu_drcs,int cpus_to_remove)578*4882a593Smuzhiyun static int find_dlpar_cpus_to_remove(u32 *cpu_drcs, int cpus_to_remove)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun 	struct device_node *dn;
581*4882a593Smuzhiyun 	int cpus_found = 0;
582*4882a593Smuzhiyun 	int rc;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* We want to find cpus_to_remove + 1 CPUs to ensure we do not
585*4882a593Smuzhiyun 	 * remove the last CPU.
586*4882a593Smuzhiyun 	 */
587*4882a593Smuzhiyun 	for_each_node_by_type(dn, "cpu") {
588*4882a593Smuzhiyun 		cpus_found++;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 		if (cpus_found > cpus_to_remove) {
591*4882a593Smuzhiyun 			of_node_put(dn);
592*4882a593Smuzhiyun 			break;
593*4882a593Smuzhiyun 		}
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 		/* Note that cpus_found is always 1 ahead of the index
596*4882a593Smuzhiyun 		 * into the cpu_drcs array, so we use cpus_found - 1
597*4882a593Smuzhiyun 		 */
598*4882a593Smuzhiyun 		rc = of_property_read_u32(dn, "ibm,my-drc-index",
599*4882a593Smuzhiyun 					  &cpu_drcs[cpus_found - 1]);
600*4882a593Smuzhiyun 		if (rc) {
601*4882a593Smuzhiyun 			pr_warn("Error occurred getting drc-index for %pOFn\n",
602*4882a593Smuzhiyun 				dn);
603*4882a593Smuzhiyun 			of_node_put(dn);
604*4882a593Smuzhiyun 			return -1;
605*4882a593Smuzhiyun 		}
606*4882a593Smuzhiyun 	}
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	if (cpus_found < cpus_to_remove) {
609*4882a593Smuzhiyun 		pr_warn("Failed to find enough CPUs (%d of %d) to remove\n",
610*4882a593Smuzhiyun 			cpus_found, cpus_to_remove);
611*4882a593Smuzhiyun 	} else if (cpus_found == cpus_to_remove) {
612*4882a593Smuzhiyun 		pr_warn("Cannot remove all CPUs\n");
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	return cpus_found;
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun 
dlpar_cpu_remove_by_count(u32 cpus_to_remove)618*4882a593Smuzhiyun static int dlpar_cpu_remove_by_count(u32 cpus_to_remove)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun 	u32 *cpu_drcs;
621*4882a593Smuzhiyun 	int cpus_found;
622*4882a593Smuzhiyun 	int cpus_removed = 0;
623*4882a593Smuzhiyun 	int i, rc;
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	pr_debug("Attempting to hot-remove %d CPUs\n", cpus_to_remove);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	cpu_drcs = kcalloc(cpus_to_remove, sizeof(*cpu_drcs), GFP_KERNEL);
628*4882a593Smuzhiyun 	if (!cpu_drcs)
629*4882a593Smuzhiyun 		return -EINVAL;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	cpus_found = find_dlpar_cpus_to_remove(cpu_drcs, cpus_to_remove);
632*4882a593Smuzhiyun 	if (cpus_found <= cpus_to_remove) {
633*4882a593Smuzhiyun 		kfree(cpu_drcs);
634*4882a593Smuzhiyun 		return -EINVAL;
635*4882a593Smuzhiyun 	}
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	for (i = 0; i < cpus_to_remove; i++) {
638*4882a593Smuzhiyun 		rc = dlpar_cpu_remove_by_index(cpu_drcs[i]);
639*4882a593Smuzhiyun 		if (rc)
640*4882a593Smuzhiyun 			break;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 		cpus_removed++;
643*4882a593Smuzhiyun 	}
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	if (cpus_removed != cpus_to_remove) {
646*4882a593Smuzhiyun 		pr_warn("CPU hot-remove failed, adding back removed CPUs\n");
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 		for (i = 0; i < cpus_removed; i++)
649*4882a593Smuzhiyun 			dlpar_cpu_add(cpu_drcs[i]);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 		rc = -EINVAL;
652*4882a593Smuzhiyun 	} else {
653*4882a593Smuzhiyun 		rc = 0;
654*4882a593Smuzhiyun 	}
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	kfree(cpu_drcs);
657*4882a593Smuzhiyun 	return rc;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun 
find_drc_info_cpus_to_add(struct device_node * cpus,struct property * info,u32 * cpu_drcs,u32 cpus_to_add)660*4882a593Smuzhiyun static int find_drc_info_cpus_to_add(struct device_node *cpus,
661*4882a593Smuzhiyun 				     struct property *info,
662*4882a593Smuzhiyun 				     u32 *cpu_drcs, u32 cpus_to_add)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun 	struct of_drc_info drc;
665*4882a593Smuzhiyun 	const __be32 *value;
666*4882a593Smuzhiyun 	u32 count, drc_index;
667*4882a593Smuzhiyun 	int cpus_found = 0;
668*4882a593Smuzhiyun 	int i, j;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	if (!info)
671*4882a593Smuzhiyun 		return -1;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	value = of_prop_next_u32(info, NULL, &count);
674*4882a593Smuzhiyun 	if (value)
675*4882a593Smuzhiyun 		value++;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	for (i = 0; i < count; i++) {
678*4882a593Smuzhiyun 		of_read_drc_info_cell(&info, &value, &drc);
679*4882a593Smuzhiyun 		if (strncmp(drc.drc_type, "CPU", 3))
680*4882a593Smuzhiyun 			break;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 		drc_index = drc.drc_index_start;
683*4882a593Smuzhiyun 		for (j = 0; j < drc.num_sequential_elems; j++) {
684*4882a593Smuzhiyun 			if (dlpar_cpu_exists(cpus, drc_index))
685*4882a593Smuzhiyun 				continue;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 			cpu_drcs[cpus_found++] = drc_index;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 			if (cpus_found == cpus_to_add)
690*4882a593Smuzhiyun 				return cpus_found;
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 			drc_index += drc.sequential_inc;
693*4882a593Smuzhiyun 		}
694*4882a593Smuzhiyun 	}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	return cpus_found;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun 
find_drc_index_cpus_to_add(struct device_node * cpus,u32 * cpu_drcs,u32 cpus_to_add)699*4882a593Smuzhiyun static int find_drc_index_cpus_to_add(struct device_node *cpus,
700*4882a593Smuzhiyun 				      u32 *cpu_drcs, u32 cpus_to_add)
701*4882a593Smuzhiyun {
702*4882a593Smuzhiyun 	int cpus_found = 0;
703*4882a593Smuzhiyun 	int index, rc;
704*4882a593Smuzhiyun 	u32 drc_index;
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	/* Search the ibm,drc-indexes array for possible CPU drcs to
707*4882a593Smuzhiyun 	 * add. Note that the format of the ibm,drc-indexes array is
708*4882a593Smuzhiyun 	 * the number of entries in the array followed by the array
709*4882a593Smuzhiyun 	 * of drc values so we start looking at index = 1.
710*4882a593Smuzhiyun 	 */
711*4882a593Smuzhiyun 	index = 1;
712*4882a593Smuzhiyun 	while (cpus_found < cpus_to_add) {
713*4882a593Smuzhiyun 		rc = of_property_read_u32_index(cpus, "ibm,drc-indexes",
714*4882a593Smuzhiyun 						index++, &drc_index);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 		if (rc)
717*4882a593Smuzhiyun 			break;
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 		if (dlpar_cpu_exists(cpus, drc_index))
720*4882a593Smuzhiyun 			continue;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 		cpu_drcs[cpus_found++] = drc_index;
723*4882a593Smuzhiyun 	}
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	return cpus_found;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun 
dlpar_cpu_add_by_count(u32 cpus_to_add)728*4882a593Smuzhiyun static int dlpar_cpu_add_by_count(u32 cpus_to_add)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun 	struct device_node *parent;
731*4882a593Smuzhiyun 	struct property *info;
732*4882a593Smuzhiyun 	u32 *cpu_drcs;
733*4882a593Smuzhiyun 	int cpus_added = 0;
734*4882a593Smuzhiyun 	int cpus_found;
735*4882a593Smuzhiyun 	int i, rc;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	pr_debug("Attempting to hot-add %d CPUs\n", cpus_to_add);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	cpu_drcs = kcalloc(cpus_to_add, sizeof(*cpu_drcs), GFP_KERNEL);
740*4882a593Smuzhiyun 	if (!cpu_drcs)
741*4882a593Smuzhiyun 		return -EINVAL;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	parent = of_find_node_by_path("/cpus");
744*4882a593Smuzhiyun 	if (!parent) {
745*4882a593Smuzhiyun 		pr_warn("Could not find CPU root node in device tree\n");
746*4882a593Smuzhiyun 		kfree(cpu_drcs);
747*4882a593Smuzhiyun 		return -1;
748*4882a593Smuzhiyun 	}
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	info = of_find_property(parent, "ibm,drc-info", NULL);
751*4882a593Smuzhiyun 	if (info)
752*4882a593Smuzhiyun 		cpus_found = find_drc_info_cpus_to_add(parent, info, cpu_drcs, cpus_to_add);
753*4882a593Smuzhiyun 	else
754*4882a593Smuzhiyun 		cpus_found = find_drc_index_cpus_to_add(parent, cpu_drcs, cpus_to_add);
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	of_node_put(parent);
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	if (cpus_found < cpus_to_add) {
759*4882a593Smuzhiyun 		pr_warn("Failed to find enough CPUs (%d of %d) to add\n",
760*4882a593Smuzhiyun 			cpus_found, cpus_to_add);
761*4882a593Smuzhiyun 		kfree(cpu_drcs);
762*4882a593Smuzhiyun 		return -EINVAL;
763*4882a593Smuzhiyun 	}
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	for (i = 0; i < cpus_to_add; i++) {
766*4882a593Smuzhiyun 		rc = dlpar_cpu_add(cpu_drcs[i]);
767*4882a593Smuzhiyun 		if (rc)
768*4882a593Smuzhiyun 			break;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 		cpus_added++;
771*4882a593Smuzhiyun 	}
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	if (cpus_added < cpus_to_add) {
774*4882a593Smuzhiyun 		pr_warn("CPU hot-add failed, removing any added CPUs\n");
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 		for (i = 0; i < cpus_added; i++)
777*4882a593Smuzhiyun 			dlpar_cpu_remove_by_index(cpu_drcs[i]);
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 		rc = -EINVAL;
780*4882a593Smuzhiyun 	} else {
781*4882a593Smuzhiyun 		rc = 0;
782*4882a593Smuzhiyun 	}
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	kfree(cpu_drcs);
785*4882a593Smuzhiyun 	return rc;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun 
dlpar_cpu(struct pseries_hp_errorlog * hp_elog)788*4882a593Smuzhiyun int dlpar_cpu(struct pseries_hp_errorlog *hp_elog)
789*4882a593Smuzhiyun {
790*4882a593Smuzhiyun 	u32 count, drc_index;
791*4882a593Smuzhiyun 	int rc;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	count = hp_elog->_drc_u.drc_count;
794*4882a593Smuzhiyun 	drc_index = hp_elog->_drc_u.drc_index;
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	lock_device_hotplug();
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	switch (hp_elog->action) {
799*4882a593Smuzhiyun 	case PSERIES_HP_ELOG_ACTION_REMOVE:
800*4882a593Smuzhiyun 		if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
801*4882a593Smuzhiyun 			rc = dlpar_cpu_remove_by_count(count);
802*4882a593Smuzhiyun 		else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
803*4882a593Smuzhiyun 			rc = dlpar_cpu_remove_by_index(drc_index);
804*4882a593Smuzhiyun 		else
805*4882a593Smuzhiyun 			rc = -EINVAL;
806*4882a593Smuzhiyun 		break;
807*4882a593Smuzhiyun 	case PSERIES_HP_ELOG_ACTION_ADD:
808*4882a593Smuzhiyun 		if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
809*4882a593Smuzhiyun 			rc = dlpar_cpu_add_by_count(count);
810*4882a593Smuzhiyun 		else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
811*4882a593Smuzhiyun 			rc = dlpar_cpu_add(drc_index);
812*4882a593Smuzhiyun 		else
813*4882a593Smuzhiyun 			rc = -EINVAL;
814*4882a593Smuzhiyun 		break;
815*4882a593Smuzhiyun 	default:
816*4882a593Smuzhiyun 		pr_err("Invalid action (%d) specified\n", hp_elog->action);
817*4882a593Smuzhiyun 		rc = -EINVAL;
818*4882a593Smuzhiyun 		break;
819*4882a593Smuzhiyun 	}
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	unlock_device_hotplug();
822*4882a593Smuzhiyun 	return rc;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
826*4882a593Smuzhiyun 
dlpar_cpu_probe(const char * buf,size_t count)827*4882a593Smuzhiyun static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	u32 drc_index;
830*4882a593Smuzhiyun 	int rc;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	rc = kstrtou32(buf, 0, &drc_index);
833*4882a593Smuzhiyun 	if (rc)
834*4882a593Smuzhiyun 		return -EINVAL;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	rc = dlpar_cpu_add(drc_index);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	return rc ? rc : count;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun 
dlpar_cpu_release(const char * buf,size_t count)841*4882a593Smuzhiyun static ssize_t dlpar_cpu_release(const char *buf, size_t count)
842*4882a593Smuzhiyun {
843*4882a593Smuzhiyun 	struct device_node *dn;
844*4882a593Smuzhiyun 	u32 drc_index;
845*4882a593Smuzhiyun 	int rc;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	dn = of_find_node_by_path(buf);
848*4882a593Smuzhiyun 	if (!dn)
849*4882a593Smuzhiyun 		return -EINVAL;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
852*4882a593Smuzhiyun 	if (rc) {
853*4882a593Smuzhiyun 		of_node_put(dn);
854*4882a593Smuzhiyun 		return -EINVAL;
855*4882a593Smuzhiyun 	}
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	rc = dlpar_cpu_remove(dn, drc_index);
858*4882a593Smuzhiyun 	of_node_put(dn);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	return rc ? rc : count;
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
864*4882a593Smuzhiyun 
pseries_smp_notifier(struct notifier_block * nb,unsigned long action,void * data)865*4882a593Smuzhiyun static int pseries_smp_notifier(struct notifier_block *nb,
866*4882a593Smuzhiyun 				unsigned long action, void *data)
867*4882a593Smuzhiyun {
868*4882a593Smuzhiyun 	struct of_reconfig_data *rd = data;
869*4882a593Smuzhiyun 	int err = 0;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	switch (action) {
872*4882a593Smuzhiyun 	case OF_RECONFIG_ATTACH_NODE:
873*4882a593Smuzhiyun 		err = pseries_add_processor(rd->dn);
874*4882a593Smuzhiyun 		break;
875*4882a593Smuzhiyun 	case OF_RECONFIG_DETACH_NODE:
876*4882a593Smuzhiyun 		pseries_remove_processor(rd->dn);
877*4882a593Smuzhiyun 		break;
878*4882a593Smuzhiyun 	}
879*4882a593Smuzhiyun 	return notifier_from_errno(err);
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun static struct notifier_block pseries_smp_nb = {
883*4882a593Smuzhiyun 	.notifier_call = pseries_smp_notifier,
884*4882a593Smuzhiyun };
885*4882a593Smuzhiyun 
pseries_cpu_hotplug_init(void)886*4882a593Smuzhiyun static int __init pseries_cpu_hotplug_init(void)
887*4882a593Smuzhiyun {
888*4882a593Smuzhiyun 	int qcss_tok;
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
891*4882a593Smuzhiyun 	ppc_md.cpu_probe = dlpar_cpu_probe;
892*4882a593Smuzhiyun 	ppc_md.cpu_release = dlpar_cpu_release;
893*4882a593Smuzhiyun #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	rtas_stop_self_token = rtas_token("stop-self");
896*4882a593Smuzhiyun 	qcss_tok = rtas_token("query-cpu-stopped-state");
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	if (rtas_stop_self_token == RTAS_UNKNOWN_SERVICE ||
899*4882a593Smuzhiyun 			qcss_tok == RTAS_UNKNOWN_SERVICE) {
900*4882a593Smuzhiyun 		printk(KERN_INFO "CPU Hotplug not supported by firmware "
901*4882a593Smuzhiyun 				"- disabling.\n");
902*4882a593Smuzhiyun 		return 0;
903*4882a593Smuzhiyun 	}
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	smp_ops->cpu_offline_self = pseries_cpu_offline_self;
906*4882a593Smuzhiyun 	smp_ops->cpu_disable = pseries_cpu_disable;
907*4882a593Smuzhiyun 	smp_ops->cpu_die = pseries_cpu_die;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	/* Processors can be added/removed only on LPAR */
910*4882a593Smuzhiyun 	if (firmware_has_feature(FW_FEATURE_LPAR))
911*4882a593Smuzhiyun 		of_reconfig_notifier_register(&pseries_smp_nb);
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	return 0;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun machine_arch_initcall(pseries, pseries_cpu_hotplug_init);
916