blob: 38a45088f633bb190ff53b5f7ac8306fd24acb4e [file] [log] [blame]
Benjamin Herrenschmidt55190f82011-09-19 17:44:52 +00001/*
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>
Benjamin Herrenschmidt55190f82011-09-19 17:44:52 +000028#include <asm/rtas.h>
29#include <asm/vdso_datapage.h>
30#include <asm/cputhreads.h>
31#include <asm/xics.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000032#include <asm/opal.h>
Preeti U Murthyf2038912014-04-11 16:01:48 +053033#include <asm/runlatch.h>
Anton Blanchard2751b622014-03-11 11:54:06 +110034#include <asm/code-patching.h>
Michael Neulingd4e58e52014-06-11 15:59:28 +100035#include <asm/dbell.h>
Paul Mackerras755563b2015-03-19 19:29:01 +110036#include <asm/kvm_ppc.h>
37#include <asm/ppc-opcode.h>
Benjamin Herrenschmidt55190f82011-09-19 17:44:52 +000038
39#include "powernv.h"
40
Benjamin Herrenschmidt344eb012011-09-19 17:44:54 +000041#ifdef DEBUG
42#include <asm/udbg.h>
43#define DBG(fmt...) udbg_printf(fmt)
44#else
45#define DBG(fmt...)
46#endif
47
Paul Gortmaker061d19f2013-06-24 15:30:09 -040048static void pnv_smp_setup_cpu(int cpu)
Benjamin Herrenschmidt55190f82011-09-19 17:44:52 +000049{
50 if (cpu != boot_cpuid)
51 xics_setup_cpu();
Michael Neulingd4e58e52014-06-11 15:59:28 +100052
53#ifdef CONFIG_PPC_DOORBELL
54 if (cpu_has_feature(CPU_FTR_DBELL))
55 doorbell_setup_this_cpu();
56#endif
Benjamin Herrenschmidt55190f82011-09-19 17:44:52 +000057}
58
Anton Blancharde51df2c2014-08-20 08:55:18 +100059static int pnv_smp_kick_cpu(int nr)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000060{
61 unsigned int pcpu = get_hard_smp_processor_id(nr);
Anton Blanchard2751b622014-03-11 11:54:06 +110062 unsigned long start_here =
63 __pa(ppc_function_entry(generic_secondary_smp_init));
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000064 long rc;
65
66 BUG_ON(nr < 0 || nr >= NR_CPUS);
67
Benjamin Herrenschmidtb2b48582013-05-14 15:12:31 +100068 /*
69 * If we already started or OPALv2 is not supported, we just
70 * kick the CPU via the PACA
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000071 */
Benjamin Herrenschmidtb2b48582013-05-14 15:12:31 +100072 if (paca[nr].cpu_start || !firmware_has_feature(FW_FEATURE_OPALv2))
73 goto kick;
74
75 /*
76 * At this point, the CPU can either be spinning on the way in
77 * from kexec or be inside OPAL waiting to be started for the
78 * first time. OPAL v3 allows us to query OPAL to know if it
79 * has the CPUs, so we do that
80 */
81 if (firmware_has_feature(FW_FEATURE_OPALv3)) {
82 uint8_t status;
83
84 rc = opal_query_cpu_status(pcpu, &status);
Benjamin Herrenschmidt4ea90082013-05-03 17:21:00 +000085 if (rc != OPAL_SUCCESS) {
Benjamin Herrenschmidtb2b48582013-05-14 15:12:31 +100086 pr_warn("OPAL Error %ld querying CPU %d state\n",
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000087 rc, nr);
Benjamin Herrenschmidt4ea90082013-05-03 17:21:00 +000088 return -ENODEV;
89 }
Benjamin Herrenschmidtb2b48582013-05-14 15:12:31 +100090
91 /*
92 * Already started, just kick it, probably coming from
93 * kexec and spinning
94 */
95 if (status == OPAL_THREAD_STARTED)
96 goto kick;
97
98 /*
99 * Available/inactive, let's kick it
100 */
101 if (status == OPAL_THREAD_INACTIVE) {
102 pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n",
103 nr, pcpu);
104 rc = opal_start_cpu(pcpu, start_here);
105 if (rc != OPAL_SUCCESS) {
106 pr_warn("OPAL Error %ld starting CPU %d\n",
107 rc, nr);
108 return -ENODEV;
109 }
110 } else {
111 /*
112 * An unavailable CPU (or any other unknown status)
113 * shouldn't be started. It should also
114 * not be in the possible map but currently it can
115 * happen
116 */
117 pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
118 " (status %d)...\n", nr, pcpu, status);
119 return -ENODEV;
120 }
121 } else {
122 /*
123 * On OPAL v2, we just kick it and hope for the best,
124 * we must not test the error from opal_start_cpu() or
125 * we would fail to get CPUs from kexec.
126 */
127 opal_start_cpu(pcpu, start_here);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000128 }
Benjamin Herrenschmidtb2b48582013-05-14 15:12:31 +1000129 kick:
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000130 return smp_generic_kick_cpu(nr);
131}
132
Benjamin Herrenschmidt344eb012011-09-19 17:44:54 +0000133#ifdef CONFIG_HOTPLUG_CPU
134
135static int pnv_smp_cpu_disable(void)
136{
137 int cpu = smp_processor_id();
138
139 /* This is identical to pSeries... might consolidate by
140 * moving migrate_irqs_away to a ppc_md with default to
141 * the generic fixup_irqs. --BenH.
142 */
143 set_cpu_online(cpu, false);
144 vdso_data->processorCount--;
145 if (cpu == boot_cpuid)
146 boot_cpuid = cpumask_any(cpu_online_mask);
147 xics_migrate_irqs_away();
148 return 0;
149}
150
151static void pnv_smp_cpu_kill_self(void)
152{
153 unsigned int cpu;
Paul Mackerras755563b2015-03-19 19:29:01 +1100154 unsigned long srr1, wmask;
Shreyas B. Prabhu8eb8ac82014-12-10 00:26:51 +0530155 u32 idle_states;
Benjamin Herrenschmidt344eb012011-09-19 17:44:54 +0000156
Benjamin Herrenschmidt344eb012011-09-19 17:44:54 +0000157 /* Standard hot unplug procedure */
158 local_irq_disable();
159 idle_task_exit();
160 current->active_mm = NULL; /* for sanity */
161 cpu = smp_processor_id();
162 DBG("CPU%d offline\n", cpu);
163 generic_set_cpu_dead(cpu);
164 smp_wmb();
165
Paul Mackerras755563b2015-03-19 19:29:01 +1100166 wmask = SRR1_WAKEMASK;
167 if (cpu_has_feature(CPU_FTR_ARCH_207S))
168 wmask = SRR1_WAKEMASK_P8;
169
Shreyas B. Prabhu8eb8ac82014-12-10 00:26:51 +0530170 idle_states = pnv_get_supported_cpuidle_states();
Benjamin Herrenschmidt344eb012011-09-19 17:44:54 +0000171 /* We don't want to take decrementer interrupts while we are offline,
172 * so clear LPCR:PECE1. We keep PECE2 enabled.
173 */
174 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1);
175 while (!generic_check_cpu_restart(cpu)) {
Shreyas B. Prabhu77b54e92014-12-10 00:26:53 +0530176
Preeti U Murthyf2038912014-04-11 16:01:48 +0530177 ppc64_runlatch_off();
Shreyas B. Prabhu77b54e92014-12-10 00:26:53 +0530178
179 if (idle_states & OPAL_PM_WINKLE_ENABLED)
180 srr1 = power7_winkle();
181 else if ((idle_states & OPAL_PM_SLEEP_ENABLED) ||
Shreyas B. Prabhu7cba1602014-12-10 00:26:52 +0530182 (idle_states & OPAL_PM_SLEEP_ENABLED_ER1))
Shreyas B. Prabhu8eb8ac82014-12-10 00:26:51 +0530183 srr1 = power7_sleep();
184 else
185 srr1 = power7_nap(1);
Shreyas B. Prabhu77b54e92014-12-10 00:26:53 +0530186
Preeti U Murthyf2038912014-04-11 16:01:48 +0530187 ppc64_runlatch_on();
Michael Ellermane2186022014-05-23 18:15:30 +1000188
Paul Mackerras56548fc2014-12-03 14:48:40 +1100189 /*
190 * If the SRR1 value indicates that we woke up due to
191 * an external interrupt, then clear the interrupt.
192 * We clear the interrupt before checking for the
193 * reason, so as to avoid a race where we wake up for
194 * some other reason, find nothing and clear the interrupt
195 * just as some other cpu is sending us an interrupt.
196 * If we returned from power7_nap as a result of
197 * having finished executing in a KVM guest, then srr1
198 * contains 0.
199 */
Paul Mackerras755563b2015-03-19 19:29:01 +1100200 if ((srr1 & wmask) == SRR1_WAKEEE) {
Paul Mackerras56548fc2014-12-03 14:48:40 +1100201 icp_native_flush_interrupt();
202 local_paca->irq_happened &= PACA_IRQ_HARD_DIS;
203 smp_mb();
Paul Mackerras755563b2015-03-19 19:29:01 +1100204 } else if ((srr1 & wmask) == SRR1_WAKEHDBELL) {
205 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
206 asm volatile(PPC_MSGCLR(%0) : : "r" (msg));
207 kvmppc_set_host_ipi(cpu, 0);
Paul Mackerras56548fc2014-12-03 14:48:40 +1100208 }
Michael Ellermane2186022014-05-23 18:15:30 +1000209
210 if (cpu_core_split_required())
211 continue;
212
213 if (!generic_check_cpu_restart(cpu))
Benjamin Herrenschmidt344eb012011-09-19 17:44:54 +0000214 DBG("CPU%d Unexpected exit while offline !\n", cpu);
Benjamin Herrenschmidt344eb012011-09-19 17:44:54 +0000215 }
216 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_PECE1);
217 DBG("CPU%d coming online...\n", cpu);
218}
219
220#endif /* CONFIG_HOTPLUG_CPU */
221
Greg Kurzd70a54e2014-12-12 12:37:40 +0100222static int pnv_cpu_bootable(unsigned int nr)
223{
224 /*
225 * Starting with POWER8, the subcore logic relies on all threads of a
226 * core being booted so that they can participate in split mode
227 * switches. So on those machines we ignore the smt_enabled_at_boot
228 * setting (smt-enabled on the kernel command line).
229 */
230 if (cpu_has_feature(CPU_FTR_ARCH_207S))
231 return 1;
232
233 return smp_generic_cpu_bootable(nr);
234}
235
Benjamin Herrenschmidt55190f82011-09-19 17:44:52 +0000236static struct smp_ops_t pnv_smp_ops = {
237 .message_pass = smp_muxed_ipi_message_pass,
238 .cause_ipi = NULL, /* Filled at runtime by xics_smp_probe() */
239 .probe = xics_smp_probe,
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000240 .kick_cpu = pnv_smp_kick_cpu,
Benjamin Herrenschmidt55190f82011-09-19 17:44:52 +0000241 .setup_cpu = pnv_smp_setup_cpu,
Greg Kurzd70a54e2014-12-12 12:37:40 +0100242 .cpu_bootable = pnv_cpu_bootable,
Benjamin Herrenschmidt344eb012011-09-19 17:44:54 +0000243#ifdef CONFIG_HOTPLUG_CPU
244 .cpu_disable = pnv_smp_cpu_disable,
245 .cpu_die = generic_cpu_die,
246#endif /* CONFIG_HOTPLUG_CPU */
Benjamin Herrenschmidt55190f82011-09-19 17:44:52 +0000247};
248
249/* This is called very early during platform setup_arch */
250void __init pnv_smp_init(void)
251{
252 smp_ops = &pnv_smp_ops;
253
254 /* XXX We don't yet have a proper entry point from HAL, for
255 * now we rely on kexec-style entry from BML
256 */
257
258#ifdef CONFIG_PPC_RTAS
259 /* Non-lpar has additional take/give timebase */
260 if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
261 smp_ops->give_timebase = rtas_give_timebase;
262 smp_ops->take_timebase = rtas_take_timebase;
263 }
264#endif /* CONFIG_PPC_RTAS */
Benjamin Herrenschmidt344eb012011-09-19 17:44:54 +0000265
266#ifdef CONFIG_HOTPLUG_CPU
267 ppc_md.cpu_die = pnv_smp_cpu_kill_self;
268#endif
Benjamin Herrenschmidt55190f82011-09-19 17:44:52 +0000269}