1 /*
2  * SMP support for pSeries machines.
3  *
4  * Dave Engebretsen, Peter Bergner, and
5  * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
6  *
7  * Plus various changes from other IBM teams...
8  *
9  *      This program is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU General Public License
11  *      as published by the Free Software Foundation; either version
12  *      2 of the License, or (at your option) any later version.
13  */
14 
15 
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/spinlock.h>
23 #include <linux/cache.h>
24 #include <linux/err.h>
25 #include <linux/device.h>
26 #include <linux/cpu.h>
27 
28 #include <asm/ptrace.h>
29 #include <linux/atomic.h>
30 #include <asm/irq.h>
31 #include <asm/page.h>
32 #include <asm/pgtable.h>
33 #include <asm/io.h>
34 #include <asm/prom.h>
35 #include <asm/smp.h>
36 #include <asm/paca.h>
37 #include <asm/machdep.h>
38 #include <asm/cputable.h>
39 #include <asm/firmware.h>
40 #include <asm/rtas.h>
41 #include <asm/vdso_datapage.h>
42 #include <asm/cputhreads.h>
43 #include <asm/xics.h>
44 #include <asm/dbell.h>
45 #include <asm/plpar_wrappers.h>
46 #include <asm/code-patching.h>
47 
48 #include "pseries.h"
49 #include "offline_states.h"
50 
51 
52 /*
53  * The Primary thread of each non-boot processor was started from the OF client
54  * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
55  */
56 static cpumask_var_t of_spin_mask;
57 
58 /* Query where a cpu is now.  Return codes #defined in plpar_wrappers.h */
59 int smp_query_cpu_stopped(unsigned int pcpu)
60 {
61 	int cpu_status, status;
62 	int qcss_tok = rtas_token("query-cpu-stopped-state");
63 
64 	if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
65 		printk_once(KERN_INFO
66 			"Firmware doesn't support query-cpu-stopped-state\n");
67 		return QCSS_HARDWARE_ERROR;
68 	}
69 
70 	status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
71 	if (status != 0) {
72 		printk(KERN_ERR
73 		       "RTAS query-cpu-stopped-state failed: %i\n", status);
74 		return status;
75 	}
76 
77 	return cpu_status;
78 }
79 
80 /**
81  * smp_startup_cpu() - start the given cpu
82  *
83  * At boot time, there is nothing to do for primary threads which were
84  * started from Open Firmware.  For anything else, call RTAS with the
85  * appropriate start location.
86  *
87  * Returns:
88  *	0	- failure
89  *	1	- success
90  */
91 static inline int smp_startup_cpu(unsigned int lcpu)
92 {
93 	int status;
94 	unsigned long start_here =
95 			__pa(ppc_function_entry(generic_secondary_smp_init));
96 	unsigned int pcpu;
97 	int start_cpu;
98 
99 	if (cpumask_test_cpu(lcpu, of_spin_mask))
100 		/* Already started by OF and sitting in spin loop */
101 		return 1;
102 
103 	pcpu = get_hard_smp_processor_id(lcpu);
104 
105 	/* Check to see if the CPU out of FW already for kexec */
106 	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
107 		cpumask_set_cpu(lcpu, of_spin_mask);
108 		return 1;
109 	}
110 
111 	/* Fixup atomic count: it exited inside IRQ handler. */
112 	task_thread_info(paca[lcpu].__current)->preempt_count	= 0;
113 #ifdef CONFIG_HOTPLUG_CPU
114 	if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
115 		goto out;
116 #endif
117 	/*
118 	 * If the RTAS start-cpu token does not exist then presume the
119 	 * cpu is already spinning.
120 	 */
121 	start_cpu = rtas_token("start-cpu");
122 	if (start_cpu == RTAS_UNKNOWN_SERVICE)
123 		return 1;
124 
125 	status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
126 	if (status != 0) {
127 		printk(KERN_ERR "start-cpu failed: %i\n", status);
128 		return 0;
129 	}
130 
131 #ifdef CONFIG_HOTPLUG_CPU
132 out:
133 #endif
134 	return 1;
135 }
136 
137 static void smp_setup_cpu(int cpu)
138 {
139 	if (cpu != boot_cpuid)
140 		xics_setup_cpu();
141 
142 	if (firmware_has_feature(FW_FEATURE_SPLPAR))
143 		vpa_init(cpu);
144 
145 	cpumask_clear_cpu(cpu, of_spin_mask);
146 #ifdef CONFIG_HOTPLUG_CPU
147 	set_cpu_current_state(cpu, CPU_STATE_ONLINE);
148 	set_default_offline_state(cpu);
149 #endif
150 }
151 
152 static int smp_pSeries_kick_cpu(int nr)
153 {
154 	if (nr < 0 || nr >= nr_cpu_ids)
155 		return -EINVAL;
156 
157 	if (!smp_startup_cpu(nr))
158 		return -ENOENT;
159 
160 	/*
161 	 * The processor is currently spinning, waiting for the
162 	 * cpu_start field to become non-zero After we set cpu_start,
163 	 * the processor will continue on to secondary_start
164 	 */
165 	paca[nr].cpu_start = 1;
166 #ifdef CONFIG_HOTPLUG_CPU
167 	set_preferred_offline_state(nr, CPU_STATE_ONLINE);
168 
169 	if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
170 		long rc;
171 		unsigned long hcpuid;
172 
173 		hcpuid = get_hard_smp_processor_id(nr);
174 		rc = plpar_hcall_norets(H_PROD, hcpuid);
175 		if (rc != H_SUCCESS)
176 			printk(KERN_ERR "Error: Prod to wake up processor %d "
177 						"Ret= %ld\n", nr, rc);
178 	}
179 #endif
180 
181 	return 0;
182 }
183 
184 static void smp_pseries_cause_ipi(int cpu)
185 {
186 	/* POWER9 should not use this handler */
187 	if (doorbell_try_core_ipi(cpu))
188 		return;
189 
190 	icp_ops->cause_ipi(cpu);
191 }
192 
193 static int pseries_cause_nmi_ipi(int cpu)
194 {
195 	int hwcpu;
196 
197 	if (cpu == NMI_IPI_ALL_OTHERS) {
198 		hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
199 	} else {
200 		if (cpu < 0) {
201 			WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
202 			return 0;
203 		}
204 
205 		hwcpu = get_hard_smp_processor_id(cpu);
206 	}
207 
208 	if (plapr_signal_sys_reset(hwcpu) == H_SUCCESS)
209 		return 1;
210 
211 	return 0;
212 }
213 
214 static __init void pSeries_smp_probe(void)
215 {
216 	xics_smp_probe();
217 
218 	if (cpu_has_feature(CPU_FTR_DBELL))
219 		smp_ops->cause_ipi = smp_pseries_cause_ipi;
220 	else
221 		smp_ops->cause_ipi = icp_ops->cause_ipi;
222 }
223 
224 static struct smp_ops_t pseries_smp_ops = {
225 	.message_pass	= NULL,	/* Use smp_muxed_ipi_message_pass */
226 	.cause_ipi	= NULL,	/* Filled at runtime by pSeries_smp_probe() */
227 	.cause_nmi_ipi	= pseries_cause_nmi_ipi,
228 	.probe		= pSeries_smp_probe,
229 	.kick_cpu	= smp_pSeries_kick_cpu,
230 	.setup_cpu	= smp_setup_cpu,
231 	.cpu_bootable	= smp_generic_cpu_bootable,
232 };
233 
234 /* This is called very early */
235 void __init smp_init_pseries(void)
236 {
237 	int i;
238 
239 	pr_debug(" -> smp_init_pSeries()\n");
240 	smp_ops = &pseries_smp_ops;
241 
242 	alloc_bootmem_cpumask_var(&of_spin_mask);
243 
244 	/*
245 	 * Mark threads which are still spinning in hold loops
246 	 *
247 	 * We know prom_init will not have started them if RTAS supports
248 	 * query-cpu-stopped-state.
249 	 */
250 	if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
251 		if (cpu_has_feature(CPU_FTR_SMT)) {
252 			for_each_present_cpu(i) {
253 				if (cpu_thread_in_core(i) == 0)
254 					cpumask_set_cpu(i, of_spin_mask);
255 			}
256 		} else
257 			cpumask_copy(of_spin_mask, cpu_present_mask);
258 
259 		cpumask_clear_cpu(boot_cpuid, of_spin_mask);
260 	}
261 
262 	/* Non-lpar has additional take/give timebase */
263 	if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
264 		smp_ops->give_timebase = rtas_give_timebase;
265 		smp_ops->take_timebase = rtas_take_timebase;
266 	}
267 
268 	pr_debug(" <- smp_init_pSeries()\n");
269 }
270