169a80d3fSPaul Mackerras /*
269a80d3fSPaul Mackerras  * SMP support for pSeries machines.
369a80d3fSPaul Mackerras  *
469a80d3fSPaul Mackerras  * Dave Engebretsen, Peter Bergner, and
569a80d3fSPaul Mackerras  * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
669a80d3fSPaul Mackerras  *
769a80d3fSPaul Mackerras  * Plus various changes from other IBM teams...
869a80d3fSPaul Mackerras  *
969a80d3fSPaul Mackerras  *      This program is free software; you can redistribute it and/or
1069a80d3fSPaul Mackerras  *      modify it under the terms of the GNU General Public License
1169a80d3fSPaul Mackerras  *      as published by the Free Software Foundation; either version
1269a80d3fSPaul Mackerras  *      2 of the License, or (at your option) any later version.
1369a80d3fSPaul Mackerras  */
1469a80d3fSPaul Mackerras 
1569a80d3fSPaul Mackerras 
1669a80d3fSPaul Mackerras #include <linux/kernel.h>
1769a80d3fSPaul Mackerras #include <linux/sched.h>
1869a80d3fSPaul Mackerras #include <linux/smp.h>
1969a80d3fSPaul Mackerras #include <linux/interrupt.h>
2069a80d3fSPaul Mackerras #include <linux/delay.h>
2169a80d3fSPaul Mackerras #include <linux/init.h>
2269a80d3fSPaul Mackerras #include <linux/spinlock.h>
2369a80d3fSPaul Mackerras #include <linux/cache.h>
2469a80d3fSPaul Mackerras #include <linux/err.h>
25edbaa603SKay Sievers #include <linux/device.h>
2669a80d3fSPaul Mackerras #include <linux/cpu.h>
2769a80d3fSPaul Mackerras 
2869a80d3fSPaul Mackerras #include <asm/ptrace.h>
2960063497SArun Sharma #include <linux/atomic.h>
3069a80d3fSPaul Mackerras #include <asm/irq.h>
3169a80d3fSPaul Mackerras #include <asm/page.h>
3269a80d3fSPaul Mackerras #include <asm/pgtable.h>
3369a80d3fSPaul Mackerras #include <asm/io.h>
3469a80d3fSPaul Mackerras #include <asm/prom.h>
3569a80d3fSPaul Mackerras #include <asm/smp.h>
3669a80d3fSPaul Mackerras #include <asm/paca.h>
3769a80d3fSPaul Mackerras #include <asm/machdep.h>
3869a80d3fSPaul Mackerras #include <asm/cputable.h>
3969a80d3fSPaul Mackerras #include <asm/firmware.h>
4069a80d3fSPaul Mackerras #include <asm/rtas.h>
41271c3f35SPaul Mackerras #include <asm/vdso_datapage.h>
428d089085SBenjamin Herrenschmidt #include <asm/cputhreads.h>
430b05ac6eSBenjamin Herrenschmidt #include <asm/xics.h>
44eac1e731SCédric Le Goater #include <asm/xive.h>
45e5e84f0aSIan Munsie #include <asm/dbell.h>
46212bebb4SDeepthi Dharwar #include <asm/plpar_wrappers.h>
472751b628SAnton Blanchard #include <asm/code-patching.h>
4869a80d3fSPaul Mackerras 
498feaeca2SMichael Ellerman #include "pseries.h"
503aa565f5SGautham R Shenoy #include "offline_states.h"
51a1218720SMichael Ellerman 
5269a80d3fSPaul Mackerras 
5369a80d3fSPaul Mackerras /*
546a75a6b8SMilton Miller  * The Primary thread of each non-boot processor was started from the OF client
556a75a6b8SMilton Miller  * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
5669a80d3fSPaul Mackerras  */
57af831e1eSAnton Blanchard static cpumask_var_t of_spin_mask;
5869a80d3fSPaul Mackerras 
59f8b67691SMichael Neuling /* Query where a cpu is now.  Return codes #defined in plpar_wrappers.h */
60f8b67691SMichael Neuling int smp_query_cpu_stopped(unsigned int pcpu)
61f8b67691SMichael Neuling {
62f8b67691SMichael Neuling 	int cpu_status, status;
63f8b67691SMichael Neuling 	int qcss_tok = rtas_token("query-cpu-stopped-state");
64f8b67691SMichael Neuling 
65f8b67691SMichael Neuling 	if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
662d86938aSMilton Miller 		printk_once(KERN_INFO
672d86938aSMilton Miller 			"Firmware doesn't support query-cpu-stopped-state\n");
68f8b67691SMichael Neuling 		return QCSS_HARDWARE_ERROR;
69f8b67691SMichael Neuling 	}
70f8b67691SMichael Neuling 
71f8b67691SMichael Neuling 	status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
72f8b67691SMichael Neuling 	if (status != 0) {
73f8b67691SMichael Neuling 		printk(KERN_ERR
74f8b67691SMichael Neuling 		       "RTAS query-cpu-stopped-state failed: %i\n", status);
75f8b67691SMichael Neuling 		return status;
76f8b67691SMichael Neuling 	}
77f8b67691SMichael Neuling 
78f8b67691SMichael Neuling 	return cpu_status;
79f8b67691SMichael Neuling }
80f8b67691SMichael Neuling 
8169a80d3fSPaul Mackerras /**
8269a80d3fSPaul Mackerras  * smp_startup_cpu() - start the given cpu
8369a80d3fSPaul Mackerras  *
8469a80d3fSPaul Mackerras  * At boot time, there is nothing to do for primary threads which were
8569a80d3fSPaul Mackerras  * started from Open Firmware.  For anything else, call RTAS with the
8669a80d3fSPaul Mackerras  * appropriate start location.
8769a80d3fSPaul Mackerras  *
8869a80d3fSPaul Mackerras  * Returns:
8969a80d3fSPaul Mackerras  *	0	- failure
9069a80d3fSPaul Mackerras  *	1	- success
9169a80d3fSPaul Mackerras  */
92cad5cef6SGreg Kroah-Hartman static inline int smp_startup_cpu(unsigned int lcpu)
9369a80d3fSPaul Mackerras {
9469a80d3fSPaul Mackerras 	int status;
952751b628SAnton Blanchard 	unsigned long start_here =
962751b628SAnton Blanchard 			__pa(ppc_function_entry(generic_secondary_smp_init));
9769a80d3fSPaul Mackerras 	unsigned int pcpu;
9869a80d3fSPaul Mackerras 	int start_cpu;
9969a80d3fSPaul Mackerras 
100af831e1eSAnton Blanchard 	if (cpumask_test_cpu(lcpu, of_spin_mask))
10169a80d3fSPaul Mackerras 		/* Already started by OF and sitting in spin loop */
10269a80d3fSPaul Mackerras 		return 1;
10369a80d3fSPaul Mackerras 
10469a80d3fSPaul Mackerras 	pcpu = get_hard_smp_processor_id(lcpu);
10569a80d3fSPaul Mackerras 
106aef40e87SMichael Neuling 	/* Check to see if the CPU out of FW already for kexec */
107aef40e87SMichael Neuling 	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
108af831e1eSAnton Blanchard 		cpumask_set_cpu(lcpu, of_spin_mask);
109aef40e87SMichael Neuling 		return 1;
110aef40e87SMichael Neuling 	}
111aef40e87SMichael Neuling 
11269a80d3fSPaul Mackerras 	/* Fixup atomic count: it exited inside IRQ handler. */
113b5e2fc1cSAl Viro 	task_thread_info(paca[lcpu].__current)->preempt_count	= 0;
114c60e65d7SMatt Evans #ifdef CONFIG_HOTPLUG_CPU
1153aa565f5SGautham R Shenoy 	if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
1163aa565f5SGautham R Shenoy 		goto out;
117c60e65d7SMatt Evans #endif
11869a80d3fSPaul Mackerras 	/*
11969a80d3fSPaul Mackerras 	 * If the RTAS start-cpu token does not exist then presume the
12069a80d3fSPaul Mackerras 	 * cpu is already spinning.
12169a80d3fSPaul Mackerras 	 */
12269a80d3fSPaul Mackerras 	start_cpu = rtas_token("start-cpu");
12369a80d3fSPaul Mackerras 	if (start_cpu == RTAS_UNKNOWN_SERVICE)
12469a80d3fSPaul Mackerras 		return 1;
12569a80d3fSPaul Mackerras 
126496b7a51SMichael Ellerman 	status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
12769a80d3fSPaul Mackerras 	if (status != 0) {
12869a80d3fSPaul Mackerras 		printk(KERN_ERR "start-cpu failed: %i\n", status);
12969a80d3fSPaul Mackerras 		return 0;
13069a80d3fSPaul Mackerras 	}
13169a80d3fSPaul Mackerras 
132c60e65d7SMatt Evans #ifdef CONFIG_HOTPLUG_CPU
1333aa565f5SGautham R Shenoy out:
134c60e65d7SMatt Evans #endif
13569a80d3fSPaul Mackerras 	return 1;
13669a80d3fSPaul Mackerras }
13769a80d3fSPaul Mackerras 
13886425bedSRashmica Gupta static void smp_setup_cpu(int cpu)
13969a80d3fSPaul Mackerras {
140eac1e731SCédric Le Goater 	if (xive_enabled())
141eac1e731SCédric Le Goater 		xive_smp_setup_cpu();
142eac1e731SCédric Le Goater 	else if (cpu != boot_cpuid)
14369a80d3fSPaul Mackerras 		xics_setup_cpu();
14469a80d3fSPaul Mackerras 
14569a80d3fSPaul Mackerras 	if (firmware_has_feature(FW_FEATURE_SPLPAR))
14669a80d3fSPaul Mackerras 		vpa_init(cpu);
14769a80d3fSPaul Mackerras 
148af831e1eSAnton Blanchard 	cpumask_clear_cpu(cpu, of_spin_mask);
149c60e65d7SMatt Evans #ifdef CONFIG_HOTPLUG_CPU
1503aa565f5SGautham R Shenoy 	set_cpu_current_state(cpu, CPU_STATE_ONLINE);
1513aa565f5SGautham R Shenoy 	set_default_offline_state(cpu);
152c60e65d7SMatt Evans #endif
15369a80d3fSPaul Mackerras }
15469a80d3fSPaul Mackerras 
155cad5cef6SGreg Kroah-Hartman static int smp_pSeries_kick_cpu(int nr)
15669a80d3fSPaul Mackerras {
157c642af9cSSantosh Sivaraj 	if (nr < 0 || nr >= nr_cpu_ids)
158f8d0d5dcSSantosh Sivaraj 		return -EINVAL;
15969a80d3fSPaul Mackerras 
16069a80d3fSPaul Mackerras 	if (!smp_startup_cpu(nr))
161de300974SMichael Ellerman 		return -ENOENT;
16269a80d3fSPaul Mackerras 
16369a80d3fSPaul Mackerras 	/*
16469a80d3fSPaul Mackerras 	 * The processor is currently spinning, waiting for the
16569a80d3fSPaul Mackerras 	 * cpu_start field to become non-zero After we set cpu_start,
16669a80d3fSPaul Mackerras 	 * the processor will continue on to secondary_start
16769a80d3fSPaul Mackerras 	 */
16869a80d3fSPaul Mackerras 	paca[nr].cpu_start = 1;
169c60e65d7SMatt Evans #ifdef CONFIG_HOTPLUG_CPU
1703aa565f5SGautham R Shenoy 	set_preferred_offline_state(nr, CPU_STATE_ONLINE);
1713aa565f5SGautham R Shenoy 
1723aa565f5SGautham R Shenoy 	if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
173c60e65d7SMatt Evans 		long rc;
174c60e65d7SMatt Evans 		unsigned long hcpuid;
175c60e65d7SMatt Evans 
1763aa565f5SGautham R Shenoy 		hcpuid = get_hard_smp_processor_id(nr);
1773aa565f5SGautham R Shenoy 		rc = plpar_hcall_norets(H_PROD, hcpuid);
1783aa565f5SGautham R Shenoy 		if (rc != H_SUCCESS)
1795a2ad98eSJoe Perches 			printk(KERN_ERR "Error: Prod to wake up processor %d "
1805a2ad98eSJoe Perches 						"Ret= %ld\n", nr, rc);
1813aa565f5SGautham R Shenoy 	}
182c60e65d7SMatt Evans #endif
183de300974SMichael Ellerman 
184de300974SMichael Ellerman 	return 0;
18569a80d3fSPaul Mackerras }
18669a80d3fSPaul Mackerras 
187eac1e731SCédric Le Goater static int pseries_smp_prepare_cpu(int cpu)
188eac1e731SCédric Le Goater {
189eac1e731SCédric Le Goater 	if (xive_enabled())
190eac1e731SCédric Le Goater 		return xive_smp_prepare_cpu(cpu);
191eac1e731SCédric Le Goater 	return 0;
192eac1e731SCédric Le Goater }
193eac1e731SCédric Le Goater 
194b866cc21SNicholas Piggin static void smp_pseries_cause_ipi(int cpu)
195e5e84f0aSIan Munsie {
196b866cc21SNicholas Piggin 	/* POWER9 should not use this handler */
197b866cc21SNicholas Piggin 	if (doorbell_try_core_ipi(cpu))
198b866cc21SNicholas Piggin 		return;
199b866cc21SNicholas Piggin 
200b866cc21SNicholas Piggin 	icp_ops->cause_ipi(cpu);
201e5e84f0aSIan Munsie }
202e5e84f0aSIan Munsie 
203102c05e8SNicholas Piggin static int pseries_cause_nmi_ipi(int cpu)
204102c05e8SNicholas Piggin {
205102c05e8SNicholas Piggin 	int hwcpu;
206102c05e8SNicholas Piggin 
207102c05e8SNicholas Piggin 	if (cpu == NMI_IPI_ALL_OTHERS) {
208102c05e8SNicholas Piggin 		hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
209102c05e8SNicholas Piggin 	} else {
210102c05e8SNicholas Piggin 		if (cpu < 0) {
211102c05e8SNicholas Piggin 			WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
212102c05e8SNicholas Piggin 			return 0;
213102c05e8SNicholas Piggin 		}
214102c05e8SNicholas Piggin 
215102c05e8SNicholas Piggin 		hwcpu = get_hard_smp_processor_id(cpu);
216102c05e8SNicholas Piggin 	}
217102c05e8SNicholas Piggin 
2187c09c186SMichael Ellerman 	if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
219102c05e8SNicholas Piggin 		return 1;
220102c05e8SNicholas Piggin 
221102c05e8SNicholas Piggin 	return 0;
222102c05e8SNicholas Piggin }
223102c05e8SNicholas Piggin 
224eac1e731SCédric Le Goater static __init void pSeries_smp_probe_xics(void)
225e5e84f0aSIan Munsie {
226a7f4ee1fSMichael Ellerman 	xics_smp_probe();
227e5e84f0aSIan Munsie 
228b866cc21SNicholas Piggin 	if (cpu_has_feature(CPU_FTR_DBELL))
229b866cc21SNicholas Piggin 		smp_ops->cause_ipi = smp_pseries_cause_ipi;
230b866cc21SNicholas Piggin 	else
231b866cc21SNicholas Piggin 		smp_ops->cause_ipi = icp_ops->cause_ipi;
232e5e84f0aSIan Munsie }
233e5e84f0aSIan Munsie 
234eac1e731SCédric Le Goater static __init void pSeries_smp_probe(void)
235eac1e731SCédric Le Goater {
236eac1e731SCédric Le Goater 	if (xive_enabled())
237eac1e731SCédric Le Goater 		/*
238eac1e731SCédric Le Goater 		 * Don't use P9 doorbells when XIVE is enabled. IPIs
239eac1e731SCédric Le Goater 		 * using MMIOs should be faster
240eac1e731SCédric Le Goater 		 */
241eac1e731SCédric Le Goater 		xive_smp_probe();
242eac1e731SCédric Le Goater 	else
243eac1e731SCédric Le Goater 		pSeries_smp_probe_xics();
244eac1e731SCédric Le Goater }
245eac1e731SCédric Le Goater 
24686425bedSRashmica Gupta static struct smp_ops_t pseries_smp_ops = {
2479ca980dcSPaul Mackerras 	.message_pass	= NULL,	/* Use smp_muxed_ipi_message_pass */
248e5e84f0aSIan Munsie 	.cause_ipi	= NULL,	/* Filled at runtime by pSeries_smp_probe() */
249102c05e8SNicholas Piggin 	.cause_nmi_ipi	= pseries_cause_nmi_ipi,
250e5e84f0aSIan Munsie 	.probe		= pSeries_smp_probe,
251eac1e731SCédric Le Goater 	.prepare_cpu	= pseries_smp_prepare_cpu,
25269a80d3fSPaul Mackerras 	.kick_cpu	= smp_pSeries_kick_cpu,
25386425bedSRashmica Gupta 	.setup_cpu	= smp_setup_cpu,
25439fd4027SAndy Fleming 	.cpu_bootable	= smp_generic_cpu_bootable,
25569a80d3fSPaul Mackerras };
25669a80d3fSPaul Mackerras 
25769a80d3fSPaul Mackerras /* This is called very early */
25886425bedSRashmica Gupta void __init smp_init_pseries(void)
25969a80d3fSPaul Mackerras {
26069a80d3fSPaul Mackerras 	int i;
26169a80d3fSPaul Mackerras 
262f7ebf352SMichael Ellerman 	pr_debug(" -> smp_init_pSeries()\n");
26386425bedSRashmica Gupta 	smp_ops = &pseries_smp_ops;
26469a80d3fSPaul Mackerras 
265af831e1eSAnton Blanchard 	alloc_bootmem_cpumask_var(&of_spin_mask);
266af831e1eSAnton Blanchard 
267dbe78b40SBenjamin Herrenschmidt 	/*
268dbe78b40SBenjamin Herrenschmidt 	 * Mark threads which are still spinning in hold loops
269dbe78b40SBenjamin Herrenschmidt 	 *
270dbe78b40SBenjamin Herrenschmidt 	 * We know prom_init will not have started them if RTAS supports
271dbe78b40SBenjamin Herrenschmidt 	 * query-cpu-stopped-state.
272dbe78b40SBenjamin Herrenschmidt 	 */
273dbe78b40SBenjamin Herrenschmidt 	if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
27469a80d3fSPaul Mackerras 		if (cpu_has_feature(CPU_FTR_SMT)) {
27569a80d3fSPaul Mackerras 			for_each_present_cpu(i) {
2766a75a6b8SMilton Miller 				if (cpu_thread_in_core(i) == 0)
277af831e1eSAnton Blanchard 					cpumask_set_cpu(i, of_spin_mask);
27869a80d3fSPaul Mackerras 			}
279dbe78b40SBenjamin Herrenschmidt 		} else
280af831e1eSAnton Blanchard 			cpumask_copy(of_spin_mask, cpu_present_mask);
28169a80d3fSPaul Mackerras 
282af831e1eSAnton Blanchard 		cpumask_clear_cpu(boot_cpuid, of_spin_mask);
283dbe78b40SBenjamin Herrenschmidt 	}
28469a80d3fSPaul Mackerras 
28569a80d3fSPaul Mackerras 	/* Non-lpar has additional take/give timebase */
28669a80d3fSPaul Mackerras 	if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
287c4007a2fSBenjamin Herrenschmidt 		smp_ops->give_timebase = rtas_give_timebase;
288c4007a2fSBenjamin Herrenschmidt 		smp_ops->take_timebase = rtas_take_timebase;
28969a80d3fSPaul Mackerras 	}
29069a80d3fSPaul Mackerras 
291f7ebf352SMichael Ellerman 	pr_debug(" <- smp_init_pSeries()\n");
29269a80d3fSPaul Mackerras }
293