Merge tag 'drm-vc4-fixes-2016-09-14' of https://github.com/anholt/linux into drm...
[linux-2.6-block.git] / arch / powerpc / platforms / powernv / smp.c
CommitLineData
55190f88
BH
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>
55190f88
BH
28#include <asm/vdso_datapage.h>
29#include <asm/cputhreads.h>
30#include <asm/xics.h>
14a43e69 31#include <asm/opal.h>
f2038911 32#include <asm/runlatch.h>
2751b628 33#include <asm/code-patching.h>
d4e58e59 34#include <asm/dbell.h>
755563bc
PM
35#include <asm/kvm_ppc.h>
36#include <asm/ppc-opcode.h>
55190f88
BH
37
38#include "powernv.h"
39
344eb010
BH
40#ifdef DEBUG
41#include <asm/udbg.h>
42#define DBG(fmt...) udbg_printf(fmt)
43#else
44#define DBG(fmt...)
45#endif
46
061d19f2 47static void pnv_smp_setup_cpu(int cpu)
55190f88
BH
48{
49 if (cpu != boot_cpuid)
50 xics_setup_cpu();
d4e58e59
MN
51
52#ifdef CONFIG_PPC_DOORBELL
53 if (cpu_has_feature(CPU_FTR_DBELL))
54 doorbell_setup_this_cpu();
55#endif
55190f88
BH
56}
57
e51df2c1 58static int pnv_smp_kick_cpu(int nr)
14a43e69
BH
59{
60 unsigned int pcpu = get_hard_smp_processor_id(nr);
2751b628
AB
61 unsigned long start_here =
62 __pa(ppc_function_entry(generic_secondary_smp_init));
14a43e69 63 long rc;
e4d54f71 64 uint8_t status;
14a43e69
BH
65
66 BUG_ON(nr < 0 || nr >= NR_CPUS);
67
b2b48584 68 /*
e4d54f71 69 * If we already started or OPAL is not supported, we just
b2b48584 70 * kick the CPU via the PACA
14a43e69 71 */
e4d54f71 72 if (paca[nr].cpu_start || !firmware_has_feature(FW_FEATURE_OPAL))
b2b48584
BH
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 */
e4d54f71
SS
81 rc = opal_query_cpu_status(pcpu, &status);
82 if (rc != OPAL_SUCCESS) {
83 pr_warn("OPAL Error %ld querying CPU %d state\n", rc, nr);
84 return -ENODEV;
85 }
b2b48584 86
e4d54f71
SS
87 /*
88 * Already started, just kick it, probably coming from
89 * kexec and spinning
90 */
91 if (status == OPAL_THREAD_STARTED)
92 goto kick;
b2b48584 93
e4d54f71
SS
94 /*
95 * Available/inactive, let's kick it
96 */
97 if (status == OPAL_THREAD_INACTIVE) {
98 pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n", nr, pcpu);
99 rc = opal_start_cpu(pcpu, start_here);
100 if (rc != OPAL_SUCCESS) {
101 pr_warn("OPAL Error %ld starting CPU %d\n", rc, nr);
b2b48584
BH
102 return -ENODEV;
103 }
104 } else {
105 /*
e4d54f71
SS
106 * An unavailable CPU (or any other unknown status)
107 * shouldn't be started. It should also
108 * not be in the possible map but currently it can
109 * happen
b2b48584 110 */
e4d54f71
SS
111 pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
112 " (status %d)...\n", nr, pcpu, status);
113 return -ENODEV;
14a43e69 114 }
e4d54f71
SS
115
116kick:
14a43e69
BH
117 return smp_generic_kick_cpu(nr);
118}
119
344eb010
BH
120#ifdef CONFIG_HOTPLUG_CPU
121
122static int pnv_smp_cpu_disable(void)
123{
124 int cpu = smp_processor_id();
125
126 /* This is identical to pSeries... might consolidate by
127 * moving migrate_irqs_away to a ppc_md with default to
128 * the generic fixup_irqs. --BenH.
129 */
130 set_cpu_online(cpu, false);
131 vdso_data->processorCount--;
132 if (cpu == boot_cpuid)
133 boot_cpuid = cpumask_any(cpu_online_mask);
134 xics_migrate_irqs_away();
135 return 0;
136}
137
138static void pnv_smp_cpu_kill_self(void)
139{
140 unsigned int cpu;
755563bc 141 unsigned long srr1, wmask;
8eb8ac89 142 u32 idle_states;
344eb010 143
344eb010
BH
144 /* Standard hot unplug procedure */
145 local_irq_disable();
146 idle_task_exit();
147 current->active_mm = NULL; /* for sanity */
148 cpu = smp_processor_id();
149 DBG("CPU%d offline\n", cpu);
150 generic_set_cpu_dead(cpu);
151 smp_wmb();
152
755563bc
PM
153 wmask = SRR1_WAKEMASK;
154 if (cpu_has_feature(CPU_FTR_ARCH_207S))
155 wmask = SRR1_WAKEMASK_P8;
156
8eb8ac89 157 idle_states = pnv_get_supported_cpuidle_states();
344eb010
BH
158 /* We don't want to take decrementer interrupts while we are offline,
159 * so clear LPCR:PECE1. We keep PECE2 enabled.
160 */
161 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1);
53c656c4
PM
162
163 /*
164 * Hard-disable interrupts, and then clear irq_happened flags
165 * that we can safely ignore while off-line, since they
166 * are for things for which we do no processing when off-line
167 * (or in the case of HMI, all the processing we need to do
168 * is done in lower-level real-mode code).
169 */
170 hard_irq_disable();
171 local_paca->irq_happened &= ~(PACA_IRQ_DEC | PACA_IRQ_HMI);
172
344eb010 173 while (!generic_check_cpu_restart(cpu)) {
53c656c4
PM
174 /*
175 * Clear IPI flag, since we don't handle IPIs while
176 * offline, except for those when changing micro-threading
177 * mode, which are handled explicitly below, and those
178 * for coming online, which are handled via
179 * generic_check_cpu_restart() calls.
180 */
181 kvmppc_set_host_ipi(cpu, 0);
77b54e9f 182
f2038911 183 ppc64_runlatch_off();
77b54e9f 184
c0691f9d
SP
185 if (cpu_has_feature(CPU_FTR_ARCH_300))
186 srr1 = power9_idle_stop(pnv_deepest_stop_state);
187 else if (idle_states & OPAL_PM_WINKLE_ENABLED)
77b54e9f
SP
188 srr1 = power7_winkle();
189 else if ((idle_states & OPAL_PM_SLEEP_ENABLED) ||
7cba160a 190 (idle_states & OPAL_PM_SLEEP_ENABLED_ER1))
8eb8ac89
SP
191 srr1 = power7_sleep();
192 else
193 srr1 = power7_nap(1);
77b54e9f 194
f2038911 195 ppc64_runlatch_on();
e2186023 196
56548fc0
PM
197 /*
198 * If the SRR1 value indicates that we woke up due to
199 * an external interrupt, then clear the interrupt.
200 * We clear the interrupt before checking for the
201 * reason, so as to avoid a race where we wake up for
202 * some other reason, find nothing and clear the interrupt
203 * just as some other cpu is sending us an interrupt.
204 * If we returned from power7_nap as a result of
205 * having finished executing in a KVM guest, then srr1
206 * contains 0.
207 */
53c656c4
PM
208 if (((srr1 & wmask) == SRR1_WAKEEE) ||
209 (local_paca->irq_happened & PACA_IRQ_EE)) {
56548fc0 210 icp_native_flush_interrupt();
755563bc
PM
211 } else if ((srr1 & wmask) == SRR1_WAKEHDBELL) {
212 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
213 asm volatile(PPC_MSGCLR(%0) : : "r" (msg));
56548fc0 214 }
53c656c4
PM
215 local_paca->irq_happened &= ~(PACA_IRQ_EE | PACA_IRQ_DBELL);
216 smp_mb();
e2186023
ME
217
218 if (cpu_core_split_required())
219 continue;
220
53c656c4 221 if (srr1 && !generic_check_cpu_restart(cpu))
344eb010 222 DBG("CPU%d Unexpected exit while offline !\n", cpu);
344eb010
BH
223 }
224 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_PECE1);
225 DBG("CPU%d coming online...\n", cpu);
226}
227
228#endif /* CONFIG_HOTPLUG_CPU */
229
d70a54e2
GK
230static int pnv_cpu_bootable(unsigned int nr)
231{
232 /*
233 * Starting with POWER8, the subcore logic relies on all threads of a
234 * core being booted so that they can participate in split mode
235 * switches. So on those machines we ignore the smt_enabled_at_boot
236 * setting (smt-enabled on the kernel command line).
237 */
238 if (cpu_has_feature(CPU_FTR_ARCH_207S))
239 return 1;
240
241 return smp_generic_cpu_bootable(nr);
242}
243
55190f88
BH
244static struct smp_ops_t pnv_smp_ops = {
245 .message_pass = smp_muxed_ipi_message_pass,
246 .cause_ipi = NULL, /* Filled at runtime by xics_smp_probe() */
247 .probe = xics_smp_probe,
14a43e69 248 .kick_cpu = pnv_smp_kick_cpu,
55190f88 249 .setup_cpu = pnv_smp_setup_cpu,
d70a54e2 250 .cpu_bootable = pnv_cpu_bootable,
344eb010
BH
251#ifdef CONFIG_HOTPLUG_CPU
252 .cpu_disable = pnv_smp_cpu_disable,
253 .cpu_die = generic_cpu_die,
254#endif /* CONFIG_HOTPLUG_CPU */
55190f88
BH
255};
256
257/* This is called very early during platform setup_arch */
258void __init pnv_smp_init(void)
259{
260 smp_ops = &pnv_smp_ops;
261
344eb010
BH
262#ifdef CONFIG_HOTPLUG_CPU
263 ppc_md.cpu_die = pnv_smp_cpu_kill_self;
264#endif
55190f88 265}