1 /*
2  * SMP support for PowerNV machines.
3  *
4  * Copyright 2011 IBM Corp.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/cpu.h>
21 
22 #include <asm/irq.h>
23 #include <asm/smp.h>
24 #include <asm/paca.h>
25 #include <asm/machdep.h>
26 #include <asm/cputable.h>
27 #include <asm/firmware.h>
28 #include <asm/rtas.h>
29 #include <asm/vdso_datapage.h>
30 #include <asm/cputhreads.h>
31 #include <asm/xics.h>
32 #include <asm/opal.h>
33 #include <asm/code-patching.h>
34 
35 #include "powernv.h"
36 
37 #ifdef DEBUG
38 #include <asm/udbg.h>
39 #define DBG(fmt...) udbg_printf(fmt)
40 #else
41 #define DBG(fmt...)
42 #endif
43 
44 static void pnv_smp_setup_cpu(int cpu)
45 {
46 	if (cpu != boot_cpuid)
47 		xics_setup_cpu();
48 }
49 
50 int pnv_smp_kick_cpu(int nr)
51 {
52 	unsigned int pcpu = get_hard_smp_processor_id(nr);
53 	unsigned long start_here =
54 			__pa(ppc_function_entry(generic_secondary_smp_init));
55 	long rc;
56 
57 	BUG_ON(nr < 0 || nr >= NR_CPUS);
58 
59 	/*
60 	 * If we already started or OPALv2 is not supported, we just
61 	 * kick the CPU via the PACA
62 	 */
63 	if (paca[nr].cpu_start || !firmware_has_feature(FW_FEATURE_OPALv2))
64 		goto kick;
65 
66 	/*
67 	 * At this point, the CPU can either be spinning on the way in
68 	 * from kexec or be inside OPAL waiting to be started for the
69 	 * first time. OPAL v3 allows us to query OPAL to know if it
70 	 * has the CPUs, so we do that
71 	 */
72 	if (firmware_has_feature(FW_FEATURE_OPALv3)) {
73 		uint8_t status;
74 
75 		rc = opal_query_cpu_status(pcpu, &status);
76 		if (rc != OPAL_SUCCESS) {
77 			pr_warn("OPAL Error %ld querying CPU %d state\n",
78 				rc, nr);
79 			return -ENODEV;
80 		}
81 
82 		/*
83 		 * Already started, just kick it, probably coming from
84 		 * kexec and spinning
85 		 */
86 		if (status == OPAL_THREAD_STARTED)
87 			goto kick;
88 
89 		/*
90 		 * Available/inactive, let's kick it
91 		 */
92 		if (status == OPAL_THREAD_INACTIVE) {
93 			pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n",
94 				 nr, pcpu);
95 			rc = opal_start_cpu(pcpu, start_here);
96 			if (rc != OPAL_SUCCESS) {
97 				pr_warn("OPAL Error %ld starting CPU %d\n",
98 					rc, nr);
99 				return -ENODEV;
100 			}
101 		} else {
102 			/*
103 			 * An unavailable CPU (or any other unknown status)
104 			 * shouldn't be started. It should also
105 			 * not be in the possible map but currently it can
106 			 * happen
107 			 */
108 			pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
109 				 " (status %d)...\n", nr, pcpu, status);
110 			return -ENODEV;
111 		}
112 	} else {
113 		/*
114 		 * On OPAL v2, we just kick it and hope for the best,
115 		 * we must not test the error from opal_start_cpu() or
116 		 * we would fail to get CPUs from kexec.
117 		 */
118 		opal_start_cpu(pcpu, start_here);
119 	}
120  kick:
121 	return smp_generic_kick_cpu(nr);
122 }
123 
124 #ifdef CONFIG_HOTPLUG_CPU
125 
126 static int pnv_smp_cpu_disable(void)
127 {
128 	int cpu = smp_processor_id();
129 
130 	/* This is identical to pSeries... might consolidate by
131 	 * moving migrate_irqs_away to a ppc_md with default to
132 	 * the generic fixup_irqs. --BenH.
133 	 */
134 	set_cpu_online(cpu, false);
135 	vdso_data->processorCount--;
136 	if (cpu == boot_cpuid)
137 		boot_cpuid = cpumask_any(cpu_online_mask);
138 	xics_migrate_irqs_away();
139 	return 0;
140 }
141 
142 static void pnv_smp_cpu_kill_self(void)
143 {
144 	unsigned int cpu;
145 
146 	/* Standard hot unplug procedure */
147 	local_irq_disable();
148 	idle_task_exit();
149 	current->active_mm = NULL; /* for sanity */
150 	cpu = smp_processor_id();
151 	DBG("CPU%d offline\n", cpu);
152 	generic_set_cpu_dead(cpu);
153 	smp_wmb();
154 
155 	/* We don't want to take decrementer interrupts while we are offline,
156 	 * so clear LPCR:PECE1. We keep PECE2 enabled.
157 	 */
158 	mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1);
159 	while (!generic_check_cpu_restart(cpu)) {
160 		power7_nap();
161 		if (!generic_check_cpu_restart(cpu)) {
162 			DBG("CPU%d Unexpected exit while offline !\n", cpu);
163 			/* We may be getting an IPI, so we re-enable
164 			 * interrupts to process it, it will be ignored
165 			 * since we aren't online (hopefully)
166 			 */
167 			local_irq_enable();
168 			local_irq_disable();
169 		}
170 	}
171 	mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_PECE1);
172 	DBG("CPU%d coming online...\n", cpu);
173 }
174 
175 #endif /* CONFIG_HOTPLUG_CPU */
176 
177 static struct smp_ops_t pnv_smp_ops = {
178 	.message_pass	= smp_muxed_ipi_message_pass,
179 	.cause_ipi	= NULL,	/* Filled at runtime by xics_smp_probe() */
180 	.probe		= xics_smp_probe,
181 	.kick_cpu	= pnv_smp_kick_cpu,
182 	.setup_cpu	= pnv_smp_setup_cpu,
183 	.cpu_bootable	= smp_generic_cpu_bootable,
184 #ifdef CONFIG_HOTPLUG_CPU
185 	.cpu_disable	= pnv_smp_cpu_disable,
186 	.cpu_die	= generic_cpu_die,
187 #endif /* CONFIG_HOTPLUG_CPU */
188 };
189 
190 /* This is called very early during platform setup_arch */
191 void __init pnv_smp_init(void)
192 {
193 	smp_ops = &pnv_smp_ops;
194 
195 	/* XXX We don't yet have a proper entry point from HAL, for
196 	 * now we rely on kexec-style entry from BML
197 	 */
198 
199 #ifdef CONFIG_PPC_RTAS
200 	/* Non-lpar has additional take/give timebase */
201 	if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
202 		smp_ops->give_timebase = rtas_give_timebase;
203 		smp_ops->take_timebase = rtas_take_timebase;
204 	}
205 #endif /* CONFIG_PPC_RTAS */
206 
207 #ifdef CONFIG_HOTPLUG_CPU
208 	ppc_md.cpu_die	= pnv_smp_cpu_kill_self;
209 #endif
210 }
211