xref: /OK3568_Linux_fs/kernel/arch/powerpc/kvm/book3s_hv_ras.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright 2012 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/types.h>
8*4882a593Smuzhiyun #include <linux/string.h>
9*4882a593Smuzhiyun #include <linux/kvm.h>
10*4882a593Smuzhiyun #include <linux/kvm_host.h>
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <asm/opal.h>
13*4882a593Smuzhiyun #include <asm/mce.h>
14*4882a593Smuzhiyun #include <asm/machdep.h>
15*4882a593Smuzhiyun #include <asm/cputhreads.h>
16*4882a593Smuzhiyun #include <asm/hmi.h>
17*4882a593Smuzhiyun #include <asm/kvm_ppc.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /* SRR1 bits for machine check on POWER7 */
20*4882a593Smuzhiyun #define SRR1_MC_LDSTERR		(1ul << (63-42))
21*4882a593Smuzhiyun #define SRR1_MC_IFETCH_SH	(63-45)
22*4882a593Smuzhiyun #define SRR1_MC_IFETCH_MASK	0x7
23*4882a593Smuzhiyun #define SRR1_MC_IFETCH_SLBPAR		2	/* SLB parity error */
24*4882a593Smuzhiyun #define SRR1_MC_IFETCH_SLBMULTI		3	/* SLB multi-hit */
25*4882a593Smuzhiyun #define SRR1_MC_IFETCH_SLBPARMULTI	4	/* SLB parity + multi-hit */
26*4882a593Smuzhiyun #define SRR1_MC_IFETCH_TLBMULTI		5	/* I-TLB multi-hit */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* DSISR bits for machine check on POWER7 */
29*4882a593Smuzhiyun #define DSISR_MC_DERAT_MULTI	0x800		/* D-ERAT multi-hit */
30*4882a593Smuzhiyun #define DSISR_MC_TLB_MULTI	0x400		/* D-TLB multi-hit */
31*4882a593Smuzhiyun #define DSISR_MC_SLB_PARITY	0x100		/* SLB parity error */
32*4882a593Smuzhiyun #define DSISR_MC_SLB_MULTI	0x080		/* SLB multi-hit */
33*4882a593Smuzhiyun #define DSISR_MC_SLB_PARMULTI	0x040		/* SLB parity + multi-hit */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* POWER7 SLB flush and reload */
reload_slb(struct kvm_vcpu * vcpu)36*4882a593Smuzhiyun static void reload_slb(struct kvm_vcpu *vcpu)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	struct slb_shadow *slb;
39*4882a593Smuzhiyun 	unsigned long i, n;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	/* First clear out SLB */
42*4882a593Smuzhiyun 	asm volatile("slbmte %0,%0; slbia" : : "r" (0));
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	/* Do they have an SLB shadow buffer registered? */
45*4882a593Smuzhiyun 	slb = vcpu->arch.slb_shadow.pinned_addr;
46*4882a593Smuzhiyun 	if (!slb)
47*4882a593Smuzhiyun 		return;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	/* Sanity check */
50*4882a593Smuzhiyun 	n = min_t(u32, be32_to_cpu(slb->persistent), SLB_MIN_SIZE);
51*4882a593Smuzhiyun 	if ((void *) &slb->save_area[n] > vcpu->arch.slb_shadow.pinned_end)
52*4882a593Smuzhiyun 		return;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/* Load up the SLB from that */
55*4882a593Smuzhiyun 	for (i = 0; i < n; ++i) {
56*4882a593Smuzhiyun 		unsigned long rb = be64_to_cpu(slb->save_area[i].esid);
57*4882a593Smuzhiyun 		unsigned long rs = be64_to_cpu(slb->save_area[i].vsid);
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 		rb = (rb & ~0xFFFul) | i;	/* insert entry number */
60*4882a593Smuzhiyun 		asm volatile("slbmte %0,%1" : : "r" (rs), "r" (rb));
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun  * On POWER7, see if we can handle a machine check that occurred inside
66*4882a593Smuzhiyun  * the guest in real mode, without switching to the host partition.
67*4882a593Smuzhiyun  */
kvmppc_realmode_mc_power7(struct kvm_vcpu * vcpu)68*4882a593Smuzhiyun static void kvmppc_realmode_mc_power7(struct kvm_vcpu *vcpu)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	unsigned long srr1 = vcpu->arch.shregs.msr;
71*4882a593Smuzhiyun 	struct machine_check_event mce_evt;
72*4882a593Smuzhiyun 	long handled = 1;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (srr1 & SRR1_MC_LDSTERR) {
75*4882a593Smuzhiyun 		/* error on load/store */
76*4882a593Smuzhiyun 		unsigned long dsisr = vcpu->arch.shregs.dsisr;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 		if (dsisr & (DSISR_MC_SLB_PARMULTI | DSISR_MC_SLB_MULTI |
79*4882a593Smuzhiyun 			     DSISR_MC_SLB_PARITY | DSISR_MC_DERAT_MULTI)) {
80*4882a593Smuzhiyun 			/* flush and reload SLB; flushes D-ERAT too */
81*4882a593Smuzhiyun 			reload_slb(vcpu);
82*4882a593Smuzhiyun 			dsisr &= ~(DSISR_MC_SLB_PARMULTI | DSISR_MC_SLB_MULTI |
83*4882a593Smuzhiyun 				   DSISR_MC_SLB_PARITY | DSISR_MC_DERAT_MULTI);
84*4882a593Smuzhiyun 		}
85*4882a593Smuzhiyun 		if (dsisr & DSISR_MC_TLB_MULTI) {
86*4882a593Smuzhiyun 			tlbiel_all_lpid(vcpu->kvm->arch.radix);
87*4882a593Smuzhiyun 			dsisr &= ~DSISR_MC_TLB_MULTI;
88*4882a593Smuzhiyun 		}
89*4882a593Smuzhiyun 		/* Any other errors we don't understand? */
90*4882a593Smuzhiyun 		if (dsisr & 0xffffffffUL)
91*4882a593Smuzhiyun 			handled = 0;
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	switch ((srr1 >> SRR1_MC_IFETCH_SH) & SRR1_MC_IFETCH_MASK) {
95*4882a593Smuzhiyun 	case 0:
96*4882a593Smuzhiyun 		break;
97*4882a593Smuzhiyun 	case SRR1_MC_IFETCH_SLBPAR:
98*4882a593Smuzhiyun 	case SRR1_MC_IFETCH_SLBMULTI:
99*4882a593Smuzhiyun 	case SRR1_MC_IFETCH_SLBPARMULTI:
100*4882a593Smuzhiyun 		reload_slb(vcpu);
101*4882a593Smuzhiyun 		break;
102*4882a593Smuzhiyun 	case SRR1_MC_IFETCH_TLBMULTI:
103*4882a593Smuzhiyun 		tlbiel_all_lpid(vcpu->kvm->arch.radix);
104*4882a593Smuzhiyun 		break;
105*4882a593Smuzhiyun 	default:
106*4882a593Smuzhiyun 		handled = 0;
107*4882a593Smuzhiyun 	}
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	/*
110*4882a593Smuzhiyun 	 * Now get the event and stash it in the vcpu struct so it can
111*4882a593Smuzhiyun 	 * be handled by the primary thread in virtual mode.  We can't
112*4882a593Smuzhiyun 	 * call machine_check_queue_event() here if we are running on
113*4882a593Smuzhiyun 	 * an offline secondary thread.
114*4882a593Smuzhiyun 	 */
115*4882a593Smuzhiyun 	if (get_mce_event(&mce_evt, MCE_EVENT_RELEASE)) {
116*4882a593Smuzhiyun 		if (handled && mce_evt.version == MCE_V1)
117*4882a593Smuzhiyun 			mce_evt.disposition = MCE_DISPOSITION_RECOVERED;
118*4882a593Smuzhiyun 	} else {
119*4882a593Smuzhiyun 		memset(&mce_evt, 0, sizeof(mce_evt));
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	vcpu->arch.mce_evt = mce_evt;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
kvmppc_realmode_machine_check(struct kvm_vcpu * vcpu)125*4882a593Smuzhiyun void kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	kvmppc_realmode_mc_power7(vcpu);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /* Check if dynamic split is in force and return subcore size accordingly. */
kvmppc_cur_subcore_size(void)131*4882a593Smuzhiyun static inline int kvmppc_cur_subcore_size(void)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	if (local_paca->kvm_hstate.kvm_split_mode)
134*4882a593Smuzhiyun 		return local_paca->kvm_hstate.kvm_split_mode->subcore_size;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	return threads_per_subcore;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
kvmppc_subcore_enter_guest(void)139*4882a593Smuzhiyun void kvmppc_subcore_enter_guest(void)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	int thread_id, subcore_id;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	thread_id = cpu_thread_in_core(local_paca->paca_index);
144*4882a593Smuzhiyun 	subcore_id = thread_id / kvmppc_cur_subcore_size();
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	local_paca->sibling_subcore_state->in_guest[subcore_id] = 1;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(kvmppc_subcore_enter_guest);
149*4882a593Smuzhiyun 
kvmppc_subcore_exit_guest(void)150*4882a593Smuzhiyun void kvmppc_subcore_exit_guest(void)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	int thread_id, subcore_id;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	thread_id = cpu_thread_in_core(local_paca->paca_index);
155*4882a593Smuzhiyun 	subcore_id = thread_id / kvmppc_cur_subcore_size();
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	local_paca->sibling_subcore_state->in_guest[subcore_id] = 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(kvmppc_subcore_exit_guest);
160*4882a593Smuzhiyun 
kvmppc_tb_resync_required(void)161*4882a593Smuzhiyun static bool kvmppc_tb_resync_required(void)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	if (test_and_set_bit(CORE_TB_RESYNC_REQ_BIT,
164*4882a593Smuzhiyun 				&local_paca->sibling_subcore_state->flags))
165*4882a593Smuzhiyun 		return false;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	return true;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
kvmppc_tb_resync_done(void)170*4882a593Smuzhiyun static void kvmppc_tb_resync_done(void)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	clear_bit(CORE_TB_RESYNC_REQ_BIT,
173*4882a593Smuzhiyun 			&local_paca->sibling_subcore_state->flags);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun  * kvmppc_realmode_hmi_handler() is called only by primary thread during
178*4882a593Smuzhiyun  * guest exit path.
179*4882a593Smuzhiyun  *
180*4882a593Smuzhiyun  * There are multiple reasons why HMI could occur, one of them is
181*4882a593Smuzhiyun  * Timebase (TB) error. If this HMI is due to TB error, then TB would
182*4882a593Smuzhiyun  * have been in stopped state. The opal hmi handler Will fix it and
183*4882a593Smuzhiyun  * restore the TB value with host timebase value. For HMI caused due
184*4882a593Smuzhiyun  * to non-TB errors, opal hmi handler will not touch/restore TB register
185*4882a593Smuzhiyun  * and hence there won't be any change in TB value.
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  * Since we are not sure about the cause of this HMI, we can't be sure
188*4882a593Smuzhiyun  * about the content of TB register whether it holds guest or host timebase
189*4882a593Smuzhiyun  * value. Hence the idea is to resync the TB on every HMI, so that we
190*4882a593Smuzhiyun  * know about the exact state of the TB value. Resync TB call will
191*4882a593Smuzhiyun  * restore TB to host timebase.
192*4882a593Smuzhiyun  *
193*4882a593Smuzhiyun  * Things to consider:
194*4882a593Smuzhiyun  * - On TB error, HMI interrupt is reported on all the threads of the core
195*4882a593Smuzhiyun  *   that has encountered TB error irrespective of split-core mode.
196*4882a593Smuzhiyun  * - The very first thread on the core that get chance to fix TB error
197*4882a593Smuzhiyun  *   would rsync the TB with local chipTOD value.
198*4882a593Smuzhiyun  * - The resync TB is a core level action i.e. it will sync all the TBs
199*4882a593Smuzhiyun  *   in that core independent of split-core mode. This means if we trigger
200*4882a593Smuzhiyun  *   TB sync from a thread from one subcore, it would affect TB values of
201*4882a593Smuzhiyun  *   sibling subcores of the same core.
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * All threads need to co-ordinate before making opal hmi handler.
204*4882a593Smuzhiyun  * All threads will use sibling_subcore_state->in_guest[] (shared by all
205*4882a593Smuzhiyun  * threads in the core) in paca which holds information about whether
206*4882a593Smuzhiyun  * sibling subcores are in Guest mode or host mode. The in_guest[] array
207*4882a593Smuzhiyun  * is of size MAX_SUBCORE_PER_CORE=4, indexed using subcore id to set/unset
208*4882a593Smuzhiyun  * subcore status. Only primary threads from each subcore is responsible
209*4882a593Smuzhiyun  * to set/unset its designated array element while entering/exiting the
210*4882a593Smuzhiyun  * guset.
211*4882a593Smuzhiyun  *
212*4882a593Smuzhiyun  * After invoking opal hmi handler call, one of the thread (of entire core)
213*4882a593Smuzhiyun  * will need to resync the TB. Bit 63 from subcore state bitmap flags
214*4882a593Smuzhiyun  * (sibling_subcore_state->flags) will be used to co-ordinate between
215*4882a593Smuzhiyun  * primary threads to decide who takes up the responsibility.
216*4882a593Smuzhiyun  *
217*4882a593Smuzhiyun  * This is what we do:
218*4882a593Smuzhiyun  * - Primary thread from each subcore tries to set resync required bit[63]
219*4882a593Smuzhiyun  *   of paca->sibling_subcore_state->flags.
220*4882a593Smuzhiyun  * - The first primary thread that is able to set the flag takes the
221*4882a593Smuzhiyun  *   responsibility of TB resync. (Let us call it as thread leader)
222*4882a593Smuzhiyun  * - All other threads which are in host will call
223*4882a593Smuzhiyun  *   wait_for_subcore_guest_exit() and wait for in_guest[0-3] from
224*4882a593Smuzhiyun  *   paca->sibling_subcore_state to get cleared.
225*4882a593Smuzhiyun  * - All the primary thread will clear its subcore status from subcore
226*4882a593Smuzhiyun  *   state in_guest[] array respectively.
227*4882a593Smuzhiyun  * - Once all primary threads clear in_guest[0-3], all of them will invoke
228*4882a593Smuzhiyun  *   opal hmi handler.
229*4882a593Smuzhiyun  * - Now all threads will wait for TB resync to complete by invoking
230*4882a593Smuzhiyun  *   wait_for_tb_resync() except the thread leader.
231*4882a593Smuzhiyun  * - Thread leader will do a TB resync by invoking opal_resync_timebase()
232*4882a593Smuzhiyun  *   call and the it will clear the resync required bit.
233*4882a593Smuzhiyun  * - All other threads will now come out of resync wait loop and proceed
234*4882a593Smuzhiyun  *   with individual execution.
235*4882a593Smuzhiyun  * - On return of this function, primary thread will signal all
236*4882a593Smuzhiyun  *   secondary threads to proceed.
237*4882a593Smuzhiyun  * - All secondary threads will eventually call opal hmi handler on
238*4882a593Smuzhiyun  *   their exit path.
239*4882a593Smuzhiyun  *
240*4882a593Smuzhiyun  * Returns 1 if the timebase offset should be applied, 0 if not.
241*4882a593Smuzhiyun  */
242*4882a593Smuzhiyun 
kvmppc_realmode_hmi_handler(void)243*4882a593Smuzhiyun long kvmppc_realmode_hmi_handler(void)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	bool resync_req;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	local_paca->hmi_irqs++;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	if (hmi_handle_debugtrig(NULL) >= 0)
250*4882a593Smuzhiyun 		return 1;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	/*
253*4882a593Smuzhiyun 	 * By now primary thread has already completed guest->host
254*4882a593Smuzhiyun 	 * partition switch but haven't signaled secondaries yet.
255*4882a593Smuzhiyun 	 * All the secondary threads on this subcore is waiting
256*4882a593Smuzhiyun 	 * for primary thread to signal them to go ahead.
257*4882a593Smuzhiyun 	 *
258*4882a593Smuzhiyun 	 * For threads from subcore which isn't in guest, they all will
259*4882a593Smuzhiyun 	 * wait until all other subcores on this core exit the guest.
260*4882a593Smuzhiyun 	 *
261*4882a593Smuzhiyun 	 * Now set the resync required bit. If you are the first to
262*4882a593Smuzhiyun 	 * set this bit then kvmppc_tb_resync_required() function will
263*4882a593Smuzhiyun 	 * return true. For rest all other subcores
264*4882a593Smuzhiyun 	 * kvmppc_tb_resync_required() will return false.
265*4882a593Smuzhiyun 	 *
266*4882a593Smuzhiyun 	 * If resync_req == true, then this thread is responsible to
267*4882a593Smuzhiyun 	 * initiate TB resync after hmi handler has completed.
268*4882a593Smuzhiyun 	 * All other threads on this core will wait until this thread
269*4882a593Smuzhiyun 	 * clears the resync required bit flag.
270*4882a593Smuzhiyun 	 */
271*4882a593Smuzhiyun 	resync_req = kvmppc_tb_resync_required();
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	/* Reset the subcore status to indicate it has exited guest */
274*4882a593Smuzhiyun 	kvmppc_subcore_exit_guest();
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	/*
277*4882a593Smuzhiyun 	 * Wait for other subcores on this core to exit the guest.
278*4882a593Smuzhiyun 	 * All the primary threads and threads from subcore that are
279*4882a593Smuzhiyun 	 * not in guest will wait here until all subcores are out
280*4882a593Smuzhiyun 	 * of guest context.
281*4882a593Smuzhiyun 	 */
282*4882a593Smuzhiyun 	wait_for_subcore_guest_exit();
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	/*
285*4882a593Smuzhiyun 	 * At this point we are sure that primary threads from each
286*4882a593Smuzhiyun 	 * subcore on this core have completed guest->host partition
287*4882a593Smuzhiyun 	 * switch. Now it is safe to call HMI handler.
288*4882a593Smuzhiyun 	 */
289*4882a593Smuzhiyun 	if (ppc_md.hmi_exception_early)
290*4882a593Smuzhiyun 		ppc_md.hmi_exception_early(NULL);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	/*
293*4882a593Smuzhiyun 	 * Check if this thread is responsible to resync TB.
294*4882a593Smuzhiyun 	 * All other threads will wait until this thread completes the
295*4882a593Smuzhiyun 	 * TB resync.
296*4882a593Smuzhiyun 	 */
297*4882a593Smuzhiyun 	if (resync_req) {
298*4882a593Smuzhiyun 		opal_resync_timebase();
299*4882a593Smuzhiyun 		/* Reset TB resync req bit */
300*4882a593Smuzhiyun 		kvmppc_tb_resync_done();
301*4882a593Smuzhiyun 	} else {
302*4882a593Smuzhiyun 		wait_for_tb_resync();
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	/*
306*4882a593Smuzhiyun 	 * Reset tb_offset_applied so the guest exit code won't try
307*4882a593Smuzhiyun 	 * to subtract the previous timebase offset from the timebase.
308*4882a593Smuzhiyun 	 */
309*4882a593Smuzhiyun 	if (local_paca->kvm_hstate.kvm_vcore)
310*4882a593Smuzhiyun 		local_paca->kvm_hstate.kvm_vcore->tb_offset_applied = 0;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	return 0;
313*4882a593Smuzhiyun }
314