xref: /linux/arch/s390/kvm/s390/interrupt.c (revision c03717417232c690534315a428e019a006502031)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * handling kvm guest interrupts
4  *
5  * Copyright IBM Corp. 2008, 2020
6  *
7  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8  */
9 
10 #define pr_fmt(fmt) "kvm-s390: " fmt
11 
12 #include <linux/cpufeature.h>
13 #include <linux/interrupt.h>
14 #include <linux/kvm_host.h>
15 #include <linux/hrtimer.h>
16 #include <linux/export.h>
17 #include <linux/mmu_context.h>
18 #include <linux/nospec.h>
19 #include <linux/signal.h>
20 #include <linux/slab.h>
21 #include <linux/bitmap.h>
22 #include <linux/vmalloc.h>
23 #include <asm/access-regs.h>
24 #include <asm/asm-offsets.h>
25 #include <asm/dis.h>
26 #include <linux/uaccess.h>
27 #include <asm/sclp.h>
28 #include <asm/isc.h>
29 #include <asm/nmi.h>
30 #include <asm/airq.h>
31 #include <asm/tpi.h>
32 #include "s390.h"
33 #include "gaccess.h"
34 #include "trace-s390.h"
35 #include "pci.h"
36 #include "gmap.h"
37 
38 #define PFAULT_INIT 0x0600
39 #define PFAULT_DONE 0x0680
40 #define VIRTIO_PARAM 0x0d00
41 
42 static struct kvm_s390_gib *gib;
43 
44 /* handle external calls via sigp interpretation facility */
45 static int sca_ext_call_pending(struct kvm_vcpu *vcpu, int *src_id)
46 {
47 	struct esca_block *sca = vcpu->kvm->arch.sca;
48 	union esca_sigp_ctrl sigp_ctrl;
49 
50 	if (!kvm_s390_test_cpuflags(vcpu, CPUSTAT_ECALL_PEND))
51 		return 0;
52 	if (kvm_is_ucontrol(vcpu->kvm))
53 		return 0;
54 
55 	BUG_ON(!kvm_s390_use_sca_entries());
56 
57 	sigp_ctrl = sca->cpu[vcpu->vcpu_id].sigp_ctrl;
58 	if (src_id)
59 		*src_id = sigp_ctrl.scn;
60 
61 	return sigp_ctrl.c;
62 }
63 
64 static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
65 {
66 	union esca_sigp_ctrl old_val, new_val = {.scn = src_id, .c = 1};
67 	struct esca_block *sca = vcpu->kvm->arch.sca;
68 	union esca_sigp_ctrl *sigp_ctrl;
69 	int expect, rc;
70 
71 	BUG_ON(!kvm_s390_use_sca_entries());
72 	if (kvm_is_ucontrol(vcpu->kvm))
73 		return -EINVAL;
74 
75 	sigp_ctrl = &sca->cpu[vcpu->vcpu_id].sigp_ctrl;
76 	old_val = READ_ONCE(*sigp_ctrl);
77 	old_val.c = 0;
78 
79 	expect = old_val.value;
80 	rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
81 
82 	if (rc != expect) {
83 		/* another external call is pending */
84 		return -EBUSY;
85 	}
86 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_ECALL_PEND);
87 	return 0;
88 }
89 
90 static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
91 {
92 	struct esca_block *sca = vcpu->kvm->arch.sca;
93 	union esca_sigp_ctrl *sigp_ctrl;
94 
95 	if (!kvm_s390_use_sca_entries() || !vcpu->arch.initialized || kvm_is_ucontrol(vcpu->kvm))
96 		return;
97 
98 	/* Initialize after the above check, to prevent going out of bounds */
99 	sigp_ctrl = &sca->cpu[vcpu->vcpu_id].sigp_ctrl;
100 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_ECALL_PEND);
101 
102 	WRITE_ONCE(sigp_ctrl->value, 0);
103 }
104 
105 int psw_extint_disabled(struct kvm_vcpu *vcpu)
106 {
107 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
108 }
109 
110 static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
111 {
112 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
113 }
114 
115 static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
116 {
117 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
118 }
119 
120 static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
121 {
122 	return psw_extint_disabled(vcpu) &&
123 	       psw_ioint_disabled(vcpu) &&
124 	       psw_mchk_disabled(vcpu);
125 }
126 
127 static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
128 {
129 	if (psw_extint_disabled(vcpu) ||
130 	    !(vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SUBMASK))
131 		return 0;
132 	if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
133 		/* No timer interrupts when single stepping */
134 		return 0;
135 	return 1;
136 }
137 
138 static int ckc_irq_pending(struct kvm_vcpu *vcpu)
139 {
140 	const u64 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
141 	const u64 ckc = vcpu->arch.sie_block->ckc;
142 
143 	if (vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SIGN) {
144 		if ((s64)ckc >= (s64)now)
145 			return 0;
146 	} else if (ckc >= now) {
147 		return 0;
148 	}
149 	return ckc_interrupts_enabled(vcpu);
150 }
151 
152 static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
153 {
154 	return !psw_extint_disabled(vcpu) &&
155 	       (vcpu->arch.sie_block->gcr[0] & CR0_CPU_TIMER_SUBMASK);
156 }
157 
158 static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
159 {
160 	if (!cpu_timer_interrupts_enabled(vcpu))
161 		return 0;
162 	return kvm_s390_get_cpu_timer(vcpu) >> 63;
163 }
164 
165 static uint64_t isc_to_isc_bits(int isc)
166 {
167 	return (0x80 >> isc) << 24;
168 }
169 
170 static inline u32 isc_to_int_word(u8 isc)
171 {
172 	return ((u32)isc << 27) | 0x80000000;
173 }
174 
175 static inline u8 int_word_to_isc(u32 int_word)
176 {
177 	return (int_word & 0x38000000) >> 27;
178 }
179 
180 /*
181  * To use atomic bitmap functions, we have to provide a bitmap address
182  * that is u64 aligned. However, the ipm might be u32 aligned.
183  * Therefore, we logically start the bitmap at the very beginning of the
184  * struct and fixup the bit number.
185  */
186 #define IPM_BIT_OFFSET (offsetof(struct kvm_s390_gisa, ipm) * BITS_PER_BYTE)
187 
188 /**
189  * gisa_set_iam - change the GISA interruption alert mask
190  *
191  * @gisa: gisa to operate on
192  * @iam: new IAM value to use
193  *
194  * Change the IAM atomically with the next alert address and the IPM
195  * of the GISA if the GISA is not part of the GIB alert list. All three
196  * fields are located in the first long word of the GISA.
197  *
198  * Returns: 0 on success
199  *          -EBUSY in case the gisa is part of the alert list
200  */
201 static inline int gisa_set_iam(struct kvm_s390_gisa *gisa, u8 iam)
202 {
203 	u64 word, _word;
204 
205 	word = READ_ONCE(gisa->u64.word[0]);
206 	do {
207 		if ((u64)gisa != word >> 32)
208 			return -EBUSY;
209 		_word = (word & ~0xffUL) | iam;
210 	} while (!try_cmpxchg(&gisa->u64.word[0], &word, _word));
211 
212 	return 0;
213 }
214 
215 /**
216  * gisa_clear_ipm - clear the GISA interruption pending mask
217  *
218  * @gisa: gisa to operate on
219  *
220  * Clear the IPM atomically with the next alert address and the IAM
221  * of the GISA unconditionally. All three fields are located in the
222  * first long word of the GISA.
223  */
224 static inline void gisa_clear_ipm(struct kvm_s390_gisa *gisa)
225 {
226 	u64 word, _word;
227 
228 	word = READ_ONCE(gisa->u64.word[0]);
229 	do {
230 		_word = word & ~(0xffUL << 24);
231 	} while (!try_cmpxchg(&gisa->u64.word[0], &word, _word));
232 }
233 
234 /**
235  * gisa_get_ipm_or_restore_iam - return IPM or restore GISA IAM
236  *
237  * @gi: gisa interrupt struct to work on
238  *
239  * Atomically restores the interruption alert mask if none of the
240  * relevant ISCs are pending and return the IPM.
241  *
242  * Returns: the relevant pending ISCs
243  */
244 static inline u8 gisa_get_ipm_or_restore_iam(struct kvm_s390_gisa_interrupt *gi)
245 {
246 	u8 pending_mask, alert_mask;
247 	u64 word, _word;
248 
249 	word = READ_ONCE(gi->origin->u64.word[0]);
250 	do {
251 		alert_mask = READ_ONCE(gi->alert.mask);
252 		pending_mask = (u8)(word >> 24) & alert_mask;
253 		if (pending_mask)
254 			return pending_mask;
255 		_word = (word & ~0xffUL) | alert_mask;
256 	} while (!try_cmpxchg(&gi->origin->u64.word[0], &word, _word));
257 
258 	return 0;
259 }
260 
261 static inline void gisa_set_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
262 {
263 	set_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
264 }
265 
266 static inline u8 gisa_get_ipm(struct kvm_s390_gisa *gisa)
267 {
268 	return READ_ONCE(gisa->ipm);
269 }
270 
271 static inline int gisa_tac_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
272 {
273 	return test_and_clear_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
274 }
275 
276 static inline int gisa_test_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
277 {
278 	return test_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *)gisa);
279 }
280 
281 static inline unsigned long pending_irqs_no_gisa(struct kvm_vcpu *vcpu)
282 {
283 	unsigned long pending = vcpu->kvm->arch.float_int.pending_irqs |
284 				vcpu->arch.local_int.pending_irqs;
285 
286 	pending &= ~vcpu->kvm->arch.float_int.masked_irqs;
287 	return pending;
288 }
289 
290 static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
291 {
292 	struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int;
293 	unsigned long pending_mask;
294 
295 	pending_mask = pending_irqs_no_gisa(vcpu);
296 	if (gi->origin)
297 		pending_mask |= gisa_get_ipm(gi->origin) << IRQ_PEND_IO_ISC_7;
298 	return pending_mask;
299 }
300 
301 static inline int isc_to_irq_type(unsigned long isc)
302 {
303 	return IRQ_PEND_IO_ISC_0 - isc;
304 }
305 
306 static inline int irq_type_to_isc(unsigned long irq_type)
307 {
308 	return IRQ_PEND_IO_ISC_0 - irq_type;
309 }
310 
311 static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
312 				   unsigned long active_mask)
313 {
314 	int i;
315 
316 	for (i = 0; i <= MAX_ISC; i++)
317 		if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
318 			active_mask &= ~(1UL << (isc_to_irq_type(i)));
319 
320 	return active_mask;
321 }
322 
323 static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
324 {
325 	unsigned long active_mask;
326 
327 	active_mask = pending_irqs(vcpu);
328 	if (!active_mask)
329 		return 0;
330 
331 	if (psw_extint_disabled(vcpu))
332 		active_mask &= ~IRQ_PEND_EXT_MASK;
333 	if (psw_ioint_disabled(vcpu))
334 		active_mask &= ~IRQ_PEND_IO_MASK;
335 	else
336 		active_mask = disable_iscs(vcpu, active_mask);
337 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_EXTERNAL_CALL_SUBMASK))
338 		__clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
339 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_EMERGENCY_SIGNAL_SUBMASK))
340 		__clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
341 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SUBMASK))
342 		__clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
343 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_CPU_TIMER_SUBMASK))
344 		__clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
345 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) {
346 		__clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
347 		__clear_bit(IRQ_PEND_EXT_SERVICE_EV, &active_mask);
348 	}
349 	if (psw_mchk_disabled(vcpu))
350 		active_mask &= ~IRQ_PEND_MCHK_MASK;
351 	/* PV guest cpus can have a single interruption injected at a time. */
352 	if (kvm_s390_pv_cpu_get_handle(vcpu) &&
353 	    vcpu->arch.sie_block->iictl != IICTL_CODE_NONE)
354 		active_mask &= ~(IRQ_PEND_EXT_II_MASK |
355 				 IRQ_PEND_IO_MASK |
356 				 IRQ_PEND_MCHK_MASK);
357 	/*
358 	 * Check both floating and local interrupt's cr14 because
359 	 * bit IRQ_PEND_MCHK_REP could be set in both cases.
360 	 */
361 	if (!(vcpu->arch.sie_block->gcr[14] &
362 	   (vcpu->kvm->arch.float_int.mchk.cr14 |
363 	   vcpu->arch.local_int.irq.mchk.cr14)))
364 		__clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
365 
366 	/*
367 	 * STOP irqs will never be actively delivered. They are triggered via
368 	 * intercept requests and cleared when the stop intercept is performed.
369 	 */
370 	__clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
371 
372 	return active_mask;
373 }
374 
375 static void __set_cpu_idle(struct kvm_vcpu *vcpu)
376 {
377 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
378 	set_bit(vcpu->vcpu_idx, vcpu->kvm->arch.idle_mask);
379 }
380 
381 static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
382 {
383 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT);
384 	clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.idle_mask);
385 }
386 
387 static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
388 {
389 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IO_INT | CPUSTAT_EXT_INT |
390 				      CPUSTAT_STOP_INT);
391 	vcpu->arch.sie_block->lctl = 0x0000;
392 	vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
393 
394 	if (guestdbg_enabled(vcpu)) {
395 		vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
396 					       LCTL_CR10 | LCTL_CR11);
397 		vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
398 	}
399 }
400 
401 static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
402 {
403 	if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_IO_MASK))
404 		return;
405 	if (psw_ioint_disabled(vcpu))
406 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_IO_INT);
407 	else
408 		vcpu->arch.sie_block->lctl |= LCTL_CR6;
409 }
410 
411 static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
412 {
413 	if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_EXT_MASK))
414 		return;
415 	if (psw_extint_disabled(vcpu))
416 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
417 	else
418 		vcpu->arch.sie_block->lctl |= LCTL_CR0;
419 }
420 
421 static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
422 {
423 	if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_MCHK_MASK))
424 		return;
425 	if (psw_mchk_disabled(vcpu))
426 		vcpu->arch.sie_block->ictl |= ICTL_LPSW;
427 	else
428 		vcpu->arch.sie_block->lctl |= LCTL_CR14;
429 }
430 
431 static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
432 {
433 	if (kvm_s390_is_stop_irq_pending(vcpu))
434 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
435 }
436 
437 /* Set interception request for non-deliverable interrupts */
438 static void set_intercept_indicators(struct kvm_vcpu *vcpu)
439 {
440 	set_intercept_indicators_io(vcpu);
441 	set_intercept_indicators_ext(vcpu);
442 	set_intercept_indicators_mchk(vcpu);
443 	set_intercept_indicators_stop(vcpu);
444 }
445 
446 static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
447 {
448 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
449 	int rc = 0;
450 
451 	vcpu->stat.deliver_cputm++;
452 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
453 					 0, 0);
454 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
455 		vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
456 		vcpu->arch.sie_block->eic = EXT_IRQ_CPU_TIMER;
457 	} else {
458 		rc  = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
459 				   (u16 *)__LC_EXT_INT_CODE);
460 		rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
461 		rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
462 				     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
463 		rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
464 				    &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
465 	}
466 	clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
467 	return rc ? -EFAULT : 0;
468 }
469 
470 static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
471 {
472 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
473 	int rc = 0;
474 
475 	vcpu->stat.deliver_ckc++;
476 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
477 					 0, 0);
478 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
479 		vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
480 		vcpu->arch.sie_block->eic = EXT_IRQ_CLK_COMP;
481 	} else {
482 		rc  = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
483 				   (u16 __user *)__LC_EXT_INT_CODE);
484 		rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
485 		rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
486 				     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
487 		rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
488 				    &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
489 	}
490 	clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
491 	return rc ? -EFAULT : 0;
492 }
493 
494 static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
495 {
496 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
497 	struct kvm_s390_ext_info ext;
498 	int rc;
499 
500 	spin_lock(&li->lock);
501 	ext = li->irq.ext;
502 	clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
503 	li->irq.ext.ext_params2 = 0;
504 	spin_unlock(&li->lock);
505 
506 	VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
507 		   ext.ext_params2);
508 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
509 					 KVM_S390_INT_PFAULT_INIT,
510 					 0, ext.ext_params2);
511 
512 	rc  = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
513 	rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
514 	rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
515 			     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
516 	rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
517 			    &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
518 	rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
519 	return rc ? -EFAULT : 0;
520 }
521 
522 static int __write_machine_check(struct kvm_vcpu *vcpu,
523 				 struct kvm_s390_mchk_info *mchk)
524 {
525 	unsigned long ext_sa_addr;
526 	unsigned long lc;
527 	freg_t fprs[NUM_FPRS];
528 	union mci mci;
529 	int rc;
530 
531 	/*
532 	 * All other possible payload for a machine check (e.g. the register
533 	 * contents in the save area) will be handled by the ultravisor, as
534 	 * the hypervisor does not not have the needed information for
535 	 * protected guests.
536 	 */
537 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
538 		vcpu->arch.sie_block->iictl = IICTL_CODE_MCHK;
539 		vcpu->arch.sie_block->mcic = mchk->mcic;
540 		vcpu->arch.sie_block->faddr = mchk->failing_storage_address;
541 		vcpu->arch.sie_block->edc = mchk->ext_damage_code;
542 		return 0;
543 	}
544 
545 	mci.val = mchk->mcic;
546 	/* take care of lazy register loading */
547 	kvm_s390_fpu_store(vcpu->run);
548 	save_access_regs(vcpu->run->s.regs.acrs);
549 	if (cpu_has_gs() && vcpu->arch.gs_enabled)
550 		save_gs_cb(current->thread.gs_cb);
551 
552 	/* Extended save area */
553 	rc = read_guest_lc(vcpu, __LC_MCESAD, &ext_sa_addr,
554 			   sizeof(unsigned long));
555 	/* Only bits 0 through 63-LC are used for address formation */
556 	lc = ext_sa_addr & MCESA_LC_MASK;
557 	if (test_kvm_facility(vcpu->kvm, 133)) {
558 		switch (lc) {
559 		case 0:
560 		case 10:
561 			ext_sa_addr &= ~0x3ffUL;
562 			break;
563 		case 11:
564 			ext_sa_addr &= ~0x7ffUL;
565 			break;
566 		case 12:
567 			ext_sa_addr &= ~0xfffUL;
568 			break;
569 		default:
570 			ext_sa_addr = 0;
571 			break;
572 		}
573 	} else {
574 		ext_sa_addr &= ~0x3ffUL;
575 	}
576 
577 	if (!rc && mci.vr && ext_sa_addr && test_kvm_facility(vcpu->kvm, 129)) {
578 		if (write_guest_abs(vcpu, ext_sa_addr, vcpu->run->s.regs.vrs,
579 				    512))
580 			mci.vr = 0;
581 	} else {
582 		mci.vr = 0;
583 	}
584 	if (!rc && mci.gs && ext_sa_addr && test_kvm_facility(vcpu->kvm, 133)
585 	    && (lc == 11 || lc == 12)) {
586 		if (write_guest_abs(vcpu, ext_sa_addr + 1024,
587 				    &vcpu->run->s.regs.gscb, 32))
588 			mci.gs = 0;
589 	} else {
590 		mci.gs = 0;
591 	}
592 
593 	/* General interruption information */
594 	rc |= put_guest_lc(vcpu, 1, (u8 __user *) __LC_AR_MODE_ID);
595 	rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
596 			     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
597 	rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
598 			    &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
599 	rc |= put_guest_lc(vcpu, mci.val, (u64 __user *) __LC_MCCK_CODE);
600 
601 	/* Register-save areas */
602 	if (cpu_has_vx()) {
603 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
604 		rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA, fprs, 128);
605 	} else {
606 		rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA,
607 				     vcpu->run->s.regs.fprs, 128);
608 	}
609 	rc |= write_guest_lc(vcpu, __LC_GPREGS_SAVE_AREA,
610 			     vcpu->run->s.regs.gprs, 128);
611 	rc |= put_guest_lc(vcpu, vcpu->run->s.regs.fpc,
612 			   (u32 __user *) __LC_FP_CREG_SAVE_AREA);
613 	rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->todpr,
614 			   (u32 __user *) __LC_TOD_PROGREG_SAVE_AREA);
615 	rc |= put_guest_lc(vcpu, kvm_s390_get_cpu_timer(vcpu),
616 			   (u64 __user *) __LC_CPU_TIMER_SAVE_AREA);
617 	rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->ckc >> 8,
618 			   (u64 __user *) __LC_CLOCK_COMP_SAVE_AREA);
619 	rc |= write_guest_lc(vcpu, __LC_AREGS_SAVE_AREA,
620 			     &vcpu->run->s.regs.acrs, 64);
621 	rc |= write_guest_lc(vcpu, __LC_CREGS_SAVE_AREA,
622 			     &vcpu->arch.sie_block->gcr, 128);
623 
624 	/* Extended interruption information */
625 	rc |= put_guest_lc(vcpu, mchk->ext_damage_code,
626 			   (u32 __user *) __LC_EXT_DAMAGE_CODE);
627 	rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
628 			   (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
629 	rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, &mchk->fixed_logout,
630 			     sizeof(mchk->fixed_logout));
631 	return rc ? -EFAULT : 0;
632 }
633 
634 static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
635 {
636 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
637 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
638 	struct kvm_s390_mchk_info mchk = {};
639 	int deliver = 0;
640 	int rc = 0;
641 	unsigned long flags;
642 
643 	spin_lock_irqsave(&fi->lock, flags);
644 	spin_lock(&li->lock);
645 	if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
646 	    test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
647 		/*
648 		 * If there was an exigent machine check pending, then any
649 		 * repressible machine checks that might have been pending
650 		 * are indicated along with it, so always clear bits for
651 		 * repressible and exigent interrupts
652 		 */
653 		mchk = li->irq.mchk;
654 		clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
655 		clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
656 		memset(&li->irq.mchk, 0, sizeof(mchk));
657 		deliver = 1;
658 	}
659 	/*
660 	 * We indicate floating repressible conditions along with
661 	 * other pending conditions. Channel Report Pending and Channel
662 	 * Subsystem damage are the only two and are indicated by
663 	 * bits in mcic and masked in cr14.
664 	 */
665 	if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
666 		mchk.mcic |= fi->mchk.mcic;
667 		mchk.cr14 |= fi->mchk.cr14;
668 		memset(&fi->mchk, 0, sizeof(mchk));
669 		deliver = 1;
670 	}
671 	spin_unlock(&li->lock);
672 	spin_unlock_irqrestore(&fi->lock, flags);
673 
674 	if (deliver) {
675 		VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
676 			   mchk.mcic);
677 		trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
678 						 KVM_S390_MCHK,
679 						 mchk.cr14, mchk.mcic);
680 		vcpu->stat.deliver_machine_check++;
681 		rc = __write_machine_check(vcpu, &mchk);
682 	}
683 	return rc;
684 }
685 
686 static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
687 {
688 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
689 	int rc = 0;
690 
691 	VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
692 	vcpu->stat.deliver_restart_signal++;
693 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
694 
695 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
696 		vcpu->arch.sie_block->iictl = IICTL_CODE_RESTART;
697 	} else {
698 		rc  = write_guest_lc(vcpu,
699 				     offsetof(struct lowcore, restart_old_psw),
700 				     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
701 		rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
702 				    &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
703 	}
704 	clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
705 	return rc ? -EFAULT : 0;
706 }
707 
708 static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
709 {
710 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
711 	struct kvm_s390_prefix_info prefix;
712 
713 	spin_lock(&li->lock);
714 	prefix = li->irq.prefix;
715 	li->irq.prefix.address = 0;
716 	clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
717 	spin_unlock(&li->lock);
718 
719 	vcpu->stat.deliver_prefix_signal++;
720 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
721 					 KVM_S390_SIGP_SET_PREFIX,
722 					 prefix.address, 0);
723 
724 	kvm_s390_set_prefix(vcpu, prefix.address);
725 	return 0;
726 }
727 
728 static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
729 {
730 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
731 	int rc;
732 	int cpu_addr;
733 
734 	spin_lock(&li->lock);
735 	cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
736 	clear_bit(cpu_addr, li->sigp_emerg_pending);
737 	if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
738 		clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
739 	spin_unlock(&li->lock);
740 
741 	VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
742 	vcpu->stat.deliver_emergency_signal++;
743 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
744 					 cpu_addr, 0);
745 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
746 		vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
747 		vcpu->arch.sie_block->eic = EXT_IRQ_EMERGENCY_SIG;
748 		vcpu->arch.sie_block->extcpuaddr = cpu_addr;
749 		return 0;
750 	}
751 
752 	rc  = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
753 			   (u16 *)__LC_EXT_INT_CODE);
754 	rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
755 	rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
756 			     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
757 	rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
758 			    &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
759 	return rc ? -EFAULT : 0;
760 }
761 
762 static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
763 {
764 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
765 	struct kvm_s390_extcall_info extcall;
766 	int rc;
767 
768 	spin_lock(&li->lock);
769 	extcall = li->irq.extcall;
770 	li->irq.extcall.code = 0;
771 	clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
772 	spin_unlock(&li->lock);
773 
774 	VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
775 	vcpu->stat.deliver_external_call++;
776 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
777 					 KVM_S390_INT_EXTERNAL_CALL,
778 					 extcall.code, 0);
779 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
780 		vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
781 		vcpu->arch.sie_block->eic = EXT_IRQ_EXTERNAL_CALL;
782 		vcpu->arch.sie_block->extcpuaddr = extcall.code;
783 		return 0;
784 	}
785 
786 	rc  = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
787 			   (u16 *)__LC_EXT_INT_CODE);
788 	rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
789 	rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
790 			     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
791 	rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
792 			    sizeof(psw_t));
793 	return rc ? -EFAULT : 0;
794 }
795 
796 static int __deliver_prog_pv(struct kvm_vcpu *vcpu, u16 code)
797 {
798 	switch (code) {
799 	case PGM_SPECIFICATION:
800 		vcpu->arch.sie_block->iictl = IICTL_CODE_SPECIFICATION;
801 		break;
802 	case PGM_OPERAND:
803 		vcpu->arch.sie_block->iictl = IICTL_CODE_OPERAND;
804 		break;
805 	default:
806 		return -EINVAL;
807 	}
808 	return 0;
809 }
810 
811 static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
812 {
813 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
814 	struct kvm_s390_pgm_info pgm_info;
815 	int rc = 0, nullifying = false;
816 	u16 ilen;
817 
818 	spin_lock(&li->lock);
819 	pgm_info = li->irq.pgm;
820 	clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
821 	memset(&li->irq.pgm, 0, sizeof(pgm_info));
822 	spin_unlock(&li->lock);
823 
824 	ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK;
825 	VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d",
826 		   pgm_info.code, ilen);
827 	vcpu->stat.deliver_program++;
828 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
829 					 pgm_info.code, 0);
830 
831 	/* PER is handled by the ultravisor */
832 	if (kvm_s390_pv_cpu_is_protected(vcpu))
833 		return __deliver_prog_pv(vcpu, pgm_info.code & ~PGM_PER);
834 
835 	switch (pgm_info.code & ~PGM_PER) {
836 	case PGM_AFX_TRANSLATION:
837 	case PGM_ASX_TRANSLATION:
838 	case PGM_EX_TRANSLATION:
839 	case PGM_LFX_TRANSLATION:
840 	case PGM_LSTE_SEQUENCE:
841 	case PGM_LSX_TRANSLATION:
842 	case PGM_LX_TRANSLATION:
843 	case PGM_PRIMARY_AUTHORITY:
844 	case PGM_SECONDARY_AUTHORITY:
845 		nullifying = true;
846 		fallthrough;
847 	case PGM_SPACE_SWITCH:
848 		rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
849 				  (u64 *)__LC_TRANS_EXC_CODE);
850 		break;
851 	case PGM_ALEN_TRANSLATION:
852 	case PGM_ALE_SEQUENCE:
853 	case PGM_ASTE_INSTANCE:
854 	case PGM_ASTE_SEQUENCE:
855 	case PGM_ASTE_VALIDITY:
856 	case PGM_EXTENDED_AUTHORITY:
857 		rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
858 				  (u8 *)__LC_EXC_ACCESS_ID);
859 		nullifying = true;
860 		break;
861 	case PGM_ASCE_TYPE:
862 	case PGM_PAGE_TRANSLATION:
863 	case PGM_REGION_FIRST_TRANS:
864 	case PGM_REGION_SECOND_TRANS:
865 	case PGM_REGION_THIRD_TRANS:
866 	case PGM_SEGMENT_TRANSLATION:
867 		rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
868 				  (u64 *)__LC_TRANS_EXC_CODE);
869 		rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
870 				   (u8 *)__LC_EXC_ACCESS_ID);
871 		rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
872 				   (u8 *)__LC_OP_ACCESS_ID);
873 		nullifying = true;
874 		break;
875 	case PGM_MONITOR:
876 		rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
877 				  (u16 *)__LC_MON_CLASS_NR);
878 		rc |= put_guest_lc(vcpu, pgm_info.mon_code,
879 				   (u64 *)__LC_MON_CODE);
880 		break;
881 	case PGM_VECTOR_PROCESSING:
882 	case PGM_DATA:
883 		rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
884 				  (u32 *)__LC_DATA_EXC_CODE);
885 		break;
886 	case PGM_PROTECTION:
887 		rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
888 				  (u64 *)__LC_TRANS_EXC_CODE);
889 		rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
890 				   (u8 *)__LC_EXC_ACCESS_ID);
891 		break;
892 	case PGM_STACK_FULL:
893 	case PGM_STACK_EMPTY:
894 	case PGM_STACK_SPECIFICATION:
895 	case PGM_STACK_TYPE:
896 	case PGM_STACK_OPERATION:
897 	case PGM_TRACE_TABEL:
898 	case PGM_CRYPTO_OPERATION:
899 		nullifying = true;
900 		break;
901 	}
902 
903 	if (pgm_info.code & PGM_PER) {
904 		rc |= put_guest_lc(vcpu, pgm_info.per_code,
905 				   (u8 *) __LC_PER_CODE);
906 		rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
907 				   (u8 *)__LC_PER_ATMID);
908 		rc |= put_guest_lc(vcpu, pgm_info.per_address,
909 				   (u64 *) __LC_PER_ADDRESS);
910 		rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
911 				   (u8 *) __LC_PER_ACCESS_ID);
912 	}
913 
914 	if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND))
915 		kvm_s390_rewind_psw(vcpu, ilen);
916 
917 	/* bit 1+2 of the target are the ilc, so we can directly use ilen */
918 	rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC);
919 	rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
920 				 (u64 *) __LC_PGM_LAST_BREAK);
921 	rc |= put_guest_lc(vcpu, pgm_info.code, (u16 *)__LC_PGM_CODE);
922 	rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
923 			     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
924 	rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
925 			    &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
926 	return rc ? -EFAULT : 0;
927 }
928 
929 #define SCCB_MASK 0xFFFFFFF8
930 #define SCCB_EVENT_PENDING 0x3
931 
932 static int write_sclp(struct kvm_vcpu *vcpu, u32 parm)
933 {
934 	int rc;
935 
936 	if (kvm_s390_pv_cpu_get_handle(vcpu)) {
937 		vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
938 		vcpu->arch.sie_block->eic = EXT_IRQ_SERVICE_SIG;
939 		vcpu->arch.sie_block->eiparams = parm;
940 		return 0;
941 	}
942 
943 	rc  = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
944 	rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
945 	rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
946 			     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
947 	rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
948 			    &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
949 	rc |= put_guest_lc(vcpu, parm,
950 			   (u32 *)__LC_EXT_PARAMS);
951 
952 	return rc ? -EFAULT : 0;
953 }
954 
955 static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
956 {
957 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
958 	struct kvm_s390_ext_info ext;
959 	unsigned long flags;
960 
961 	spin_lock_irqsave(&fi->lock, flags);
962 	if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs) ||
963 	    !(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
964 		spin_unlock_irqrestore(&fi->lock, flags);
965 		return 0;
966 	}
967 	ext = fi->srv_signal;
968 	memset(&fi->srv_signal, 0, sizeof(ext));
969 	clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
970 	clear_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs);
971 	if (kvm_s390_pv_cpu_is_protected(vcpu))
972 		set_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs);
973 	spin_unlock_irqrestore(&fi->lock, flags);
974 
975 	if (!ext.ext_params)
976 		return 0;
977 
978 	VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
979 		   ext.ext_params);
980 	vcpu->stat.deliver_service_signal++;
981 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
982 					 ext.ext_params, 0);
983 
984 	return write_sclp(vcpu, ext.ext_params);
985 }
986 
987 static int __must_check __deliver_service_ev(struct kvm_vcpu *vcpu)
988 {
989 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
990 	struct kvm_s390_ext_info ext;
991 	unsigned long flags;
992 
993 	spin_lock_irqsave(&fi->lock, flags);
994 	if (!(test_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs))) {
995 		spin_unlock_irqrestore(&fi->lock, flags);
996 		return 0;
997 	}
998 	ext = fi->srv_signal;
999 	/* only clear the event bits */
1000 	fi->srv_signal.ext_params &= ~SCCB_EVENT_PENDING;
1001 	clear_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs);
1002 	spin_unlock_irqrestore(&fi->lock, flags);
1003 
1004 	VCPU_EVENT(vcpu, 4, "%s", "deliver: sclp parameter event");
1005 	vcpu->stat.deliver_service_signal++;
1006 	trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
1007 					 ext.ext_params, 0);
1008 
1009 	return write_sclp(vcpu, ext.ext_params & SCCB_EVENT_PENDING);
1010 }
1011 
1012 static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
1013 {
1014 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
1015 	struct kvm_s390_interrupt_info *inti;
1016 	int rc = 0;
1017 	unsigned long flags;
1018 
1019 	spin_lock_irqsave(&fi->lock, flags);
1020 	inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
1021 					struct kvm_s390_interrupt_info,
1022 					list);
1023 	if (inti) {
1024 		list_del(&inti->list);
1025 		fi->counters[FIRQ_CNTR_PFAULT] -= 1;
1026 	}
1027 	if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
1028 		clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1029 	spin_unlock_irqrestore(&fi->lock, flags);
1030 
1031 	if (inti) {
1032 		trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
1033 						 KVM_S390_INT_PFAULT_DONE, 0,
1034 						 inti->ext.ext_params2);
1035 		VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
1036 			   inti->ext.ext_params2);
1037 
1038 		rc  = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
1039 				(u16 *)__LC_EXT_INT_CODE);
1040 		rc |= put_guest_lc(vcpu, PFAULT_DONE,
1041 				(u16 *)__LC_EXT_CPU_ADDR);
1042 		rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
1043 				&vcpu->arch.sie_block->gpsw,
1044 				sizeof(psw_t));
1045 		rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
1046 				&vcpu->arch.sie_block->gpsw,
1047 				sizeof(psw_t));
1048 		rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
1049 				(u64 *)__LC_EXT_PARAMS2);
1050 		kfree(inti);
1051 	}
1052 	return rc ? -EFAULT : 0;
1053 }
1054 
1055 static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
1056 {
1057 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
1058 	struct kvm_s390_interrupt_info *inti;
1059 	int rc = 0;
1060 	unsigned long flags;
1061 
1062 	spin_lock_irqsave(&fi->lock, flags);
1063 	inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
1064 					struct kvm_s390_interrupt_info,
1065 					list);
1066 	if (inti) {
1067 		VCPU_EVENT(vcpu, 4,
1068 			   "deliver: virtio parm: 0x%x,parm64: 0x%llx",
1069 			   inti->ext.ext_params, inti->ext.ext_params2);
1070 		vcpu->stat.deliver_virtio++;
1071 		trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
1072 				inti->type,
1073 				inti->ext.ext_params,
1074 				inti->ext.ext_params2);
1075 		list_del(&inti->list);
1076 		fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
1077 	}
1078 	if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
1079 		clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1080 	spin_unlock_irqrestore(&fi->lock, flags);
1081 
1082 	if (inti) {
1083 		rc  = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
1084 				(u16 *)__LC_EXT_INT_CODE);
1085 		rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
1086 				(u16 *)__LC_EXT_CPU_ADDR);
1087 		rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
1088 				&vcpu->arch.sie_block->gpsw,
1089 				sizeof(psw_t));
1090 		rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
1091 				&vcpu->arch.sie_block->gpsw,
1092 				sizeof(psw_t));
1093 		rc |= put_guest_lc(vcpu, inti->ext.ext_params,
1094 				(u32 *)__LC_EXT_PARAMS);
1095 		rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
1096 				(u64 *)__LC_EXT_PARAMS2);
1097 		kfree(inti);
1098 	}
1099 	return rc ? -EFAULT : 0;
1100 }
1101 
1102 static int __do_deliver_io(struct kvm_vcpu *vcpu, struct kvm_s390_io_info *io)
1103 {
1104 	int rc;
1105 
1106 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
1107 		vcpu->arch.sie_block->iictl = IICTL_CODE_IO;
1108 		vcpu->arch.sie_block->subchannel_id = io->subchannel_id;
1109 		vcpu->arch.sie_block->subchannel_nr = io->subchannel_nr;
1110 		vcpu->arch.sie_block->io_int_parm = io->io_int_parm;
1111 		vcpu->arch.sie_block->io_int_word = io->io_int_word;
1112 		return 0;
1113 	}
1114 
1115 	rc  = put_guest_lc(vcpu, io->subchannel_id, (u16 *)__LC_SUBCHANNEL_ID);
1116 	rc |= put_guest_lc(vcpu, io->subchannel_nr, (u16 *)__LC_SUBCHANNEL_NR);
1117 	rc |= put_guest_lc(vcpu, io->io_int_parm, (u32 *)__LC_IO_INT_PARM);
1118 	rc |= put_guest_lc(vcpu, io->io_int_word, (u32 *)__LC_IO_INT_WORD);
1119 	rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
1120 			     &vcpu->arch.sie_block->gpsw,
1121 			     sizeof(psw_t));
1122 	rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
1123 			    &vcpu->arch.sie_block->gpsw,
1124 			    sizeof(psw_t));
1125 	return rc ? -EFAULT : 0;
1126 }
1127 
1128 static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
1129 				     unsigned long irq_type)
1130 {
1131 	struct list_head *isc_list;
1132 	struct kvm_s390_float_interrupt *fi;
1133 	struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int;
1134 	struct kvm_s390_interrupt_info *inti = NULL;
1135 	struct kvm_s390_io_info io;
1136 	u32 isc;
1137 	int rc = 0;
1138 	unsigned long flags;
1139 
1140 	fi = &vcpu->kvm->arch.float_int;
1141 
1142 	spin_lock_irqsave(&fi->lock, flags);
1143 	isc = irq_type_to_isc(irq_type);
1144 	isc_list = &fi->lists[isc];
1145 	inti = list_first_entry_or_null(isc_list,
1146 					struct kvm_s390_interrupt_info,
1147 					list);
1148 	if (inti) {
1149 		if (inti->type & KVM_S390_INT_IO_AI_MASK)
1150 			VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)");
1151 		else
1152 			VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x",
1153 			inti->io.subchannel_id >> 8,
1154 			inti->io.subchannel_id >> 1 & 0x3,
1155 			inti->io.subchannel_nr);
1156 
1157 		vcpu->stat.deliver_io++;
1158 		trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
1159 				inti->type,
1160 				((__u32)inti->io.subchannel_id << 16) |
1161 				inti->io.subchannel_nr,
1162 				((__u64)inti->io.io_int_parm << 32) |
1163 				inti->io.io_int_word);
1164 		list_del(&inti->list);
1165 		fi->counters[FIRQ_CNTR_IO] -= 1;
1166 	}
1167 	if (list_empty(isc_list))
1168 		clear_bit(irq_type, &fi->pending_irqs);
1169 	spin_unlock_irqrestore(&fi->lock, flags);
1170 
1171 	if (inti) {
1172 		rc = __do_deliver_io(vcpu, &(inti->io));
1173 		kfree(inti);
1174 		goto out;
1175 	}
1176 
1177 	if (gi->origin && gisa_tac_ipm_gisc(gi->origin, isc)) {
1178 		/*
1179 		 * in case an adapter interrupt was not delivered
1180 		 * in SIE context KVM will handle the delivery
1181 		 */
1182 		VCPU_EVENT(vcpu, 4, "%s isc %u", "deliver: I/O (AI/gisa)", isc);
1183 		memset(&io, 0, sizeof(io));
1184 		io.io_int_word = isc_to_int_word(isc);
1185 		vcpu->stat.deliver_io++;
1186 		trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
1187 			KVM_S390_INT_IO(1, 0, 0, 0),
1188 			((__u32)io.subchannel_id << 16) |
1189 			io.subchannel_nr,
1190 			((__u64)io.io_int_parm << 32) |
1191 			io.io_int_word);
1192 		rc = __do_deliver_io(vcpu, &io);
1193 	}
1194 out:
1195 	return rc;
1196 }
1197 
1198 /* Check whether an external call is pending (deliverable or not) */
1199 int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
1200 {
1201 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1202 
1203 	if (!kvm_s390_use_sca_entries())
1204 		return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
1205 
1206 	return sca_ext_call_pending(vcpu, NULL);
1207 }
1208 
1209 int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
1210 {
1211 	if (deliverable_irqs(vcpu))
1212 		return 1;
1213 
1214 	if (kvm_cpu_has_pending_timer(vcpu))
1215 		return 1;
1216 
1217 	/* external call pending and deliverable */
1218 	if (kvm_s390_ext_call_pending(vcpu) &&
1219 	    !psw_extint_disabled(vcpu) &&
1220 	    (vcpu->arch.sie_block->gcr[0] & CR0_EXTERNAL_CALL_SUBMASK))
1221 		return 1;
1222 
1223 	if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
1224 		return 1;
1225 	return 0;
1226 }
1227 
1228 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1229 {
1230 	return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
1231 }
1232 
1233 static u64 __calculate_sltime(struct kvm_vcpu *vcpu)
1234 {
1235 	const u64 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
1236 	const u64 ckc = vcpu->arch.sie_block->ckc;
1237 	u64 cputm, sltime = 0;
1238 
1239 	if (ckc_interrupts_enabled(vcpu)) {
1240 		if (vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SIGN) {
1241 			if ((s64)now < (s64)ckc)
1242 				sltime = tod_to_ns((s64)ckc - (s64)now);
1243 		} else if (now < ckc) {
1244 			sltime = tod_to_ns(ckc - now);
1245 		}
1246 		/* already expired */
1247 		if (!sltime)
1248 			return 0;
1249 		if (cpu_timer_interrupts_enabled(vcpu)) {
1250 			cputm = kvm_s390_get_cpu_timer(vcpu);
1251 			/* already expired? */
1252 			if (cputm >> 63)
1253 				return 0;
1254 			return min_t(u64, sltime, tod_to_ns(cputm));
1255 		}
1256 	} else if (cpu_timer_interrupts_enabled(vcpu)) {
1257 		sltime = kvm_s390_get_cpu_timer(vcpu);
1258 		/* already expired? */
1259 		if (sltime >> 63)
1260 			return 0;
1261 	}
1262 	return sltime;
1263 }
1264 
1265 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
1266 {
1267 	struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int;
1268 	u64 sltime;
1269 
1270 	vcpu->stat.exit_wait_state++;
1271 
1272 	/* fast path */
1273 	if (kvm_arch_vcpu_runnable(vcpu))
1274 		return 0;
1275 
1276 	if (psw_interrupts_disabled(vcpu)) {
1277 		VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
1278 		return -EOPNOTSUPP; /* disabled wait */
1279 	}
1280 
1281 	if (gi->origin &&
1282 	    (gisa_get_ipm_or_restore_iam(gi) &
1283 	     vcpu->arch.sie_block->gcr[6] >> 24))
1284 		return 0;
1285 
1286 	if (!ckc_interrupts_enabled(vcpu) &&
1287 	    !cpu_timer_interrupts_enabled(vcpu)) {
1288 		VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
1289 		__set_cpu_idle(vcpu);
1290 		goto no_timer;
1291 	}
1292 
1293 	sltime = __calculate_sltime(vcpu);
1294 	if (!sltime)
1295 		return 0;
1296 
1297 	__set_cpu_idle(vcpu);
1298 	hrtimer_start(&vcpu->arch.ckc_timer, sltime, HRTIMER_MODE_REL);
1299 	VCPU_EVENT(vcpu, 4, "enabled wait: %llu ns", sltime);
1300 no_timer:
1301 	kvm_vcpu_srcu_read_unlock(vcpu);
1302 	vcpu->kvm->arch.float_int.last_sleep_cpu = vcpu->vcpu_idx;
1303 	kvm_vcpu_halt(vcpu);
1304 	vcpu->valid_wakeup = false;
1305 	__unset_cpu_idle(vcpu);
1306 	kvm_vcpu_srcu_read_lock(vcpu);
1307 
1308 	hrtimer_cancel(&vcpu->arch.ckc_timer);
1309 	return 0;
1310 }
1311 
1312 void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
1313 {
1314 	vcpu->valid_wakeup = true;
1315 	kvm_vcpu_wake_up(vcpu);
1316 
1317 	/*
1318 	 * The VCPU might not be sleeping but rather executing VSIE. Let's
1319 	 * kick it, so it leaves the SIE to process the request.
1320 	 */
1321 	kvm_s390_vsie_kick(vcpu);
1322 }
1323 
1324 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
1325 {
1326 	struct kvm_vcpu *vcpu;
1327 	u64 sltime;
1328 
1329 	vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
1330 	sltime = __calculate_sltime(vcpu);
1331 
1332 	/*
1333 	 * If the monotonic clock runs faster than the tod clock we might be
1334 	 * woken up too early and have to go back to sleep to avoid deadlocks.
1335 	 */
1336 	if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime)))
1337 		return HRTIMER_RESTART;
1338 	kvm_s390_vcpu_wakeup(vcpu);
1339 	return HRTIMER_NORESTART;
1340 }
1341 
1342 void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
1343 {
1344 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1345 
1346 	spin_lock(&li->lock);
1347 	li->pending_irqs = 0;
1348 	bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
1349 	memset(&li->irq, 0, sizeof(li->irq));
1350 	spin_unlock(&li->lock);
1351 
1352 	sca_clear_ext_call(vcpu);
1353 }
1354 
1355 int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
1356 {
1357 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1358 	int rc = 0;
1359 	bool delivered = false;
1360 	unsigned long irq_type;
1361 	unsigned long irqs;
1362 
1363 	__reset_intercept_indicators(vcpu);
1364 
1365 	/* pending ckc conditions might have been invalidated */
1366 	clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1367 	if (ckc_irq_pending(vcpu))
1368 		set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1369 
1370 	/* pending cpu timer conditions might have been invalidated */
1371 	clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1372 	if (cpu_timer_irq_pending(vcpu))
1373 		set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1374 
1375 	while ((irqs = deliverable_irqs(vcpu)) && !rc) {
1376 		/* bits are in the reverse order of interrupt priority */
1377 		irq_type = find_last_bit(&irqs, IRQ_PEND_COUNT);
1378 		switch (irq_type) {
1379 		case IRQ_PEND_IO_ISC_0:
1380 		case IRQ_PEND_IO_ISC_1:
1381 		case IRQ_PEND_IO_ISC_2:
1382 		case IRQ_PEND_IO_ISC_3:
1383 		case IRQ_PEND_IO_ISC_4:
1384 		case IRQ_PEND_IO_ISC_5:
1385 		case IRQ_PEND_IO_ISC_6:
1386 		case IRQ_PEND_IO_ISC_7:
1387 			rc = __deliver_io(vcpu, irq_type);
1388 			break;
1389 		case IRQ_PEND_MCHK_EX:
1390 		case IRQ_PEND_MCHK_REP:
1391 			rc = __deliver_machine_check(vcpu);
1392 			break;
1393 		case IRQ_PEND_PROG:
1394 			rc = __deliver_prog(vcpu);
1395 			break;
1396 		case IRQ_PEND_EXT_EMERGENCY:
1397 			rc = __deliver_emergency_signal(vcpu);
1398 			break;
1399 		case IRQ_PEND_EXT_EXTERNAL:
1400 			rc = __deliver_external_call(vcpu);
1401 			break;
1402 		case IRQ_PEND_EXT_CLOCK_COMP:
1403 			rc = __deliver_ckc(vcpu);
1404 			break;
1405 		case IRQ_PEND_EXT_CPU_TIMER:
1406 			rc = __deliver_cpu_timer(vcpu);
1407 			break;
1408 		case IRQ_PEND_RESTART:
1409 			rc = __deliver_restart(vcpu);
1410 			break;
1411 		case IRQ_PEND_SET_PREFIX:
1412 			rc = __deliver_set_prefix(vcpu);
1413 			break;
1414 		case IRQ_PEND_PFAULT_INIT:
1415 			rc = __deliver_pfault_init(vcpu);
1416 			break;
1417 		case IRQ_PEND_EXT_SERVICE:
1418 			rc = __deliver_service(vcpu);
1419 			break;
1420 		case IRQ_PEND_EXT_SERVICE_EV:
1421 			rc = __deliver_service_ev(vcpu);
1422 			break;
1423 		case IRQ_PEND_PFAULT_DONE:
1424 			rc = __deliver_pfault_done(vcpu);
1425 			break;
1426 		case IRQ_PEND_VIRTIO:
1427 			rc = __deliver_virtio(vcpu);
1428 			break;
1429 		default:
1430 			WARN_ONCE(1, "Unknown pending irq type %ld", irq_type);
1431 			clear_bit(irq_type, &li->pending_irqs);
1432 		}
1433 		delivered |= !rc;
1434 	}
1435 
1436 	/*
1437 	 * We delivered at least one interrupt and modified the PC. Force a
1438 	 * singlestep event now.
1439 	 */
1440 	if (delivered && guestdbg_sstep_enabled(vcpu)) {
1441 		struct kvm_debug_exit_arch *debug_exit = &vcpu->run->debug.arch;
1442 
1443 		debug_exit->addr = vcpu->arch.sie_block->gpsw.addr;
1444 		debug_exit->type = KVM_SINGLESTEP;
1445 		vcpu->guest_debug |= KVM_GUESTDBG_EXIT_PENDING;
1446 	}
1447 
1448 	set_intercept_indicators(vcpu);
1449 
1450 	return rc;
1451 }
1452 
1453 static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1454 {
1455 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1456 
1457 	vcpu->stat.inject_program++;
1458 	VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1459 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1460 				   irq->u.pgm.code, 0);
1461 
1462 	if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1463 		/* auto detection if no valid ILC was given */
1464 		irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1465 		irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1466 		irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1467 	}
1468 
1469 	if (irq->u.pgm.code == PGM_PER) {
1470 		li->irq.pgm.code |= PGM_PER;
1471 		li->irq.pgm.flags = irq->u.pgm.flags;
1472 		/* only modify PER related information */
1473 		li->irq.pgm.per_address = irq->u.pgm.per_address;
1474 		li->irq.pgm.per_code = irq->u.pgm.per_code;
1475 		li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1476 		li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1477 	} else if (!(irq->u.pgm.code & PGM_PER)) {
1478 		li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1479 				   irq->u.pgm.code;
1480 		li->irq.pgm.flags = irq->u.pgm.flags;
1481 		/* only modify non-PER information */
1482 		li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1483 		li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1484 		li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1485 		li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1486 		li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1487 		li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1488 	} else {
1489 		li->irq.pgm = irq->u.pgm;
1490 	}
1491 	set_bit(IRQ_PEND_PROG, &li->pending_irqs);
1492 	return 0;
1493 }
1494 
1495 static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1496 {
1497 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1498 
1499 	vcpu->stat.inject_pfault_init++;
1500 	VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1501 		   irq->u.ext.ext_params2);
1502 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1503 				   irq->u.ext.ext_params,
1504 				   irq->u.ext.ext_params2);
1505 
1506 	li->irq.ext = irq->u.ext;
1507 	set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
1508 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1509 	return 0;
1510 }
1511 
1512 static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1513 {
1514 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1515 	struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
1516 	uint16_t src_id = irq->u.extcall.code;
1517 
1518 	vcpu->stat.inject_external_call++;
1519 	VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
1520 		   src_id);
1521 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
1522 				   src_id, 0);
1523 
1524 	/* sending vcpu invalid */
1525 	if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
1526 		return -EINVAL;
1527 
1528 	if (kvm_s390_use_sca_entries() && !kvm_s390_pv_cpu_get_handle(vcpu))
1529 		return sca_inject_ext_call(vcpu, src_id);
1530 
1531 	if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
1532 		return -EBUSY;
1533 	*extcall = irq->u.extcall;
1534 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1535 	return 0;
1536 }
1537 
1538 static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1539 {
1540 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1541 	struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
1542 
1543 	vcpu->stat.inject_set_prefix++;
1544 	VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
1545 		   irq->u.prefix.address);
1546 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
1547 				   irq->u.prefix.address, 0);
1548 
1549 	if (!is_vcpu_stopped(vcpu))
1550 		return -EBUSY;
1551 
1552 	*prefix = irq->u.prefix;
1553 	set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
1554 	return 0;
1555 }
1556 
1557 #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
1558 static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq, bool *storestatus)
1559 {
1560 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1561 	struct kvm_s390_stop_info *stop = &li->irq.stop;
1562 
1563 	vcpu->stat.inject_stop_signal++;
1564 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
1565 
1566 	if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1567 		return -EINVAL;
1568 	if (is_vcpu_stopped(vcpu)) {
1569 		if (!(irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS))
1570 			return 0;
1571 		*storestatus = true;
1572 		return -EWOULDBLOCK;
1573 	}
1574 
1575 	if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1576 		return -EBUSY;
1577 	stop->flags = irq->u.stop.flags;
1578 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
1579 	return 0;
1580 }
1581 
1582 static int __inject_sigp_restart(struct kvm_vcpu *vcpu)
1583 {
1584 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1585 
1586 	vcpu->stat.inject_restart++;
1587 	VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
1588 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
1589 
1590 	set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
1591 	return 0;
1592 }
1593 
1594 static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
1595 				   struct kvm_s390_irq *irq)
1596 {
1597 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1598 
1599 	vcpu->stat.inject_emergency_signal++;
1600 	VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
1601 		   irq->u.emerg.code);
1602 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
1603 				   irq->u.emerg.code, 0);
1604 
1605 	/* sending vcpu invalid */
1606 	if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1607 		return -EINVAL;
1608 
1609 	set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
1610 	set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
1611 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1612 	return 0;
1613 }
1614 
1615 static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1616 {
1617 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1618 	struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
1619 
1620 	vcpu->stat.inject_mchk++;
1621 	VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
1622 		   irq->u.mchk.mcic);
1623 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
1624 				   irq->u.mchk.mcic);
1625 
1626 	/*
1627 	 * Because repressible machine checks can be indicated along with
1628 	 * exigent machine checks (PoP, Chapter 11, Interruption action)
1629 	 * we need to combine cr14, mcic and external damage code.
1630 	 * Failing storage address and the logout area should not be or'ed
1631 	 * together, we just indicate the last occurrence of the corresponding
1632 	 * machine check
1633 	 */
1634 	mchk->cr14 |= irq->u.mchk.cr14;
1635 	mchk->mcic |= irq->u.mchk.mcic;
1636 	mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1637 	mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1638 	memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1639 	       sizeof(mchk->fixed_logout));
1640 	if (mchk->mcic & MCHK_EX_MASK)
1641 		set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1642 	else if (mchk->mcic & MCHK_REP_MASK)
1643 		set_bit(IRQ_PEND_MCHK_REP,  &li->pending_irqs);
1644 	return 0;
1645 }
1646 
1647 static int __inject_ckc(struct kvm_vcpu *vcpu)
1648 {
1649 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1650 
1651 	vcpu->stat.inject_ckc++;
1652 	VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
1653 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
1654 				   0, 0);
1655 
1656 	set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1657 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1658 	return 0;
1659 }
1660 
1661 static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
1662 {
1663 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1664 
1665 	vcpu->stat.inject_cputm++;
1666 	VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
1667 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
1668 				   0, 0);
1669 
1670 	set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1671 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1672 	return 0;
1673 }
1674 
1675 static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1676 						  int isc, u32 schid)
1677 {
1678 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1679 	struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1680 	struct kvm_s390_interrupt_info *iter;
1681 	u16 id = (schid & 0xffff0000U) >> 16;
1682 	u16 nr = schid & 0x0000ffffU;
1683 	unsigned long flags;
1684 
1685 	spin_lock_irqsave(&fi->lock, flags);
1686 	list_for_each_entry(iter, isc_list, list) {
1687 		if (schid && (id != iter->io.subchannel_id ||
1688 			      nr != iter->io.subchannel_nr))
1689 			continue;
1690 		/* found an appropriate entry */
1691 		list_del_init(&iter->list);
1692 		fi->counters[FIRQ_CNTR_IO] -= 1;
1693 		if (list_empty(isc_list))
1694 			clear_bit(isc_to_irq_type(isc), &fi->pending_irqs);
1695 		spin_unlock_irqrestore(&fi->lock, flags);
1696 		return iter;
1697 	}
1698 	spin_unlock_irqrestore(&fi->lock, flags);
1699 	return NULL;
1700 }
1701 
1702 static struct kvm_s390_interrupt_info *get_top_io_int(struct kvm *kvm,
1703 						      u64 isc_mask, u32 schid)
1704 {
1705 	struct kvm_s390_interrupt_info *inti = NULL;
1706 	int isc;
1707 
1708 	for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1709 		if (isc_mask & isc_to_isc_bits(isc))
1710 			inti = get_io_int(kvm, isc, schid);
1711 	}
1712 	return inti;
1713 }
1714 
1715 static int get_top_gisa_isc(struct kvm *kvm, u64 isc_mask, u32 schid)
1716 {
1717 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
1718 	unsigned long active_mask;
1719 	int isc;
1720 
1721 	if (schid)
1722 		goto out;
1723 	if (!gi->origin)
1724 		goto out;
1725 
1726 	active_mask = (isc_mask & gisa_get_ipm(gi->origin) << 24) << 32;
1727 	while (active_mask) {
1728 		isc = __fls(active_mask) ^ (BITS_PER_LONG - 1);
1729 		if (gisa_tac_ipm_gisc(gi->origin, isc))
1730 			return isc;
1731 		clear_bit_inv(isc, &active_mask);
1732 	}
1733 out:
1734 	return -EINVAL;
1735 }
1736 
1737 /*
1738  * Dequeue and return an I/O interrupt matching any of the interruption
1739  * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1740  * Take into account the interrupts pending in the interrupt list and in GISA.
1741  *
1742  * Note that for a guest that does not enable I/O interrupts
1743  * but relies on TPI, a flood of classic interrupts may starve
1744  * out adapter interrupts on the same isc. Linux does not do
1745  * that, and it is possible to work around the issue by configuring
1746  * different iscs for classic and adapter interrupts in the guest,
1747  * but we may want to revisit this in the future.
1748  */
1749 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
1750 						    u64 isc_mask, u32 schid)
1751 {
1752 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
1753 	struct kvm_s390_interrupt_info *inti, *tmp_inti;
1754 	int isc;
1755 
1756 	inti = get_top_io_int(kvm, isc_mask, schid);
1757 
1758 	isc = get_top_gisa_isc(kvm, isc_mask, schid);
1759 	if (isc < 0)
1760 		/* no AI in GISA */
1761 		goto out;
1762 
1763 	if (!inti)
1764 		/* AI in GISA but no classical IO int */
1765 		goto gisa_out;
1766 
1767 	/* both types of interrupts present */
1768 	if (int_word_to_isc(inti->io.io_int_word) <= isc) {
1769 		/* classical IO int with higher priority */
1770 		gisa_set_ipm_gisc(gi->origin, isc);
1771 		goto out;
1772 	}
1773 gisa_out:
1774 	tmp_inti = kzalloc_obj(*inti, GFP_KERNEL_ACCOUNT);
1775 	if (tmp_inti) {
1776 		tmp_inti->type = KVM_S390_INT_IO(1, 0, 0, 0);
1777 		tmp_inti->io.io_int_word = isc_to_int_word(isc);
1778 		if (inti)
1779 			kvm_s390_reinject_io_int(kvm, inti);
1780 		inti = tmp_inti;
1781 	} else
1782 		gisa_set_ipm_gisc(gi->origin, isc);
1783 out:
1784 	return inti;
1785 }
1786 
1787 static int __inject_service(struct kvm *kvm,
1788 			     struct kvm_s390_interrupt_info *inti)
1789 {
1790 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1791 	unsigned long flags;
1792 
1793 	kvm->stat.inject_service_signal++;
1794 	spin_lock_irqsave(&fi->lock, flags);
1795 	fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1796 
1797 	/* We always allow events, track them separately from the sccb ints */
1798 	if (fi->srv_signal.ext_params & SCCB_EVENT_PENDING)
1799 		set_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs);
1800 
1801 	/*
1802 	 * Early versions of the QEMU s390 bios will inject several
1803 	 * service interrupts after another without handling a
1804 	 * condition code indicating busy.
1805 	 * We will silently ignore those superfluous sccb values.
1806 	 * A future version of QEMU will take care of serialization
1807 	 * of servc requests
1808 	 */
1809 	if (fi->srv_signal.ext_params & SCCB_MASK)
1810 		goto out;
1811 	fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1812 	set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1813 out:
1814 	spin_unlock_irqrestore(&fi->lock, flags);
1815 	kfree(inti);
1816 	return 0;
1817 }
1818 
1819 static int __inject_virtio(struct kvm *kvm,
1820 			    struct kvm_s390_interrupt_info *inti)
1821 {
1822 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1823 	unsigned long flags;
1824 
1825 	kvm->stat.inject_virtio++;
1826 	spin_lock_irqsave(&fi->lock, flags);
1827 	if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1828 		spin_unlock_irqrestore(&fi->lock, flags);
1829 		return -EBUSY;
1830 	}
1831 	fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1832 	list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1833 	set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1834 	spin_unlock_irqrestore(&fi->lock, flags);
1835 	return 0;
1836 }
1837 
1838 static int __inject_pfault_done(struct kvm *kvm,
1839 				 struct kvm_s390_interrupt_info *inti)
1840 {
1841 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1842 	unsigned long flags;
1843 
1844 	kvm->stat.inject_pfault_done++;
1845 	spin_lock_irqsave(&fi->lock, flags);
1846 	if (fi->counters[FIRQ_CNTR_PFAULT] >=
1847 		(ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1848 		spin_unlock_irqrestore(&fi->lock, flags);
1849 		return -EBUSY;
1850 	}
1851 	fi->counters[FIRQ_CNTR_PFAULT] += 1;
1852 	list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1853 	set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1854 	spin_unlock_irqrestore(&fi->lock, flags);
1855 	return 0;
1856 }
1857 
1858 #define CR_PENDING_SUBCLASS 28
1859 static int __inject_float_mchk(struct kvm *kvm,
1860 				struct kvm_s390_interrupt_info *inti)
1861 {
1862 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1863 	unsigned long flags;
1864 
1865 	kvm->stat.inject_float_mchk++;
1866 	spin_lock_irqsave(&fi->lock, flags);
1867 	fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1868 	fi->mchk.mcic |= inti->mchk.mcic;
1869 	set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1870 	spin_unlock_irqrestore(&fi->lock, flags);
1871 	kfree(inti);
1872 	return 0;
1873 }
1874 
1875 static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1876 {
1877 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
1878 	struct kvm_s390_float_interrupt *fi;
1879 	struct list_head *list;
1880 	int isc;
1881 	unsigned long flags;
1882 
1883 	kvm->stat.inject_io++;
1884 	isc = int_word_to_isc(inti->io.io_int_word);
1885 
1886 	/*
1887 	 * We do not use the lock checking variant as this is just a
1888 	 * performance optimization and we do not hold the lock here.
1889 	 * This is ok as the code will pick interrupts from both "lists"
1890 	 * for delivery.
1891 	 */
1892 	if (gi->origin && inti->type & KVM_S390_INT_IO_AI_MASK) {
1893 		VM_EVENT(kvm, 4, "%s isc %1u", "inject: I/O (AI/gisa)", isc);
1894 		gisa_set_ipm_gisc(gi->origin, isc);
1895 		kfree(inti);
1896 		return 0;
1897 	}
1898 
1899 	fi = &kvm->arch.float_int;
1900 	spin_lock_irqsave(&fi->lock, flags);
1901 	if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1902 		spin_unlock_irqrestore(&fi->lock, flags);
1903 		return -EBUSY;
1904 	}
1905 	fi->counters[FIRQ_CNTR_IO] += 1;
1906 
1907 	if (inti->type & KVM_S390_INT_IO_AI_MASK)
1908 		VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1909 	else
1910 		VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1911 			inti->io.subchannel_id >> 8,
1912 			inti->io.subchannel_id >> 1 & 0x3,
1913 			inti->io.subchannel_nr);
1914 	list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1915 	list_add_tail(&inti->list, list);
1916 	set_bit(isc_to_irq_type(isc), &fi->pending_irqs);
1917 	spin_unlock_irqrestore(&fi->lock, flags);
1918 	return 0;
1919 }
1920 
1921 static u64 inti_to_irq_pend_mask(u64 type, int isc)
1922 {
1923 	switch (type) {
1924 	case KVM_S390_MCHK:
1925 		/* Only repressible machine checks are floating */
1926 		return BIT(IRQ_PEND_MCHK_REP);
1927 	case KVM_S390_INT_VIRTIO:
1928 		return BIT(IRQ_PEND_VIRTIO);
1929 	case KVM_S390_INT_SERVICE:
1930 		return BIT(IRQ_PEND_EXT_SERVICE) |
1931 		       BIT(IRQ_PEND_EXT_SERVICE_EV);
1932 	case KVM_S390_INT_PFAULT_DONE:
1933 		return BIT(IRQ_PEND_PFAULT_DONE);
1934 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1935 		return BIT(isc_to_irq_type(isc));
1936 	default:
1937 		return 0;
1938 	}
1939 }
1940 
1941 /*
1942  * Setup intervention masks to catch running vcpus that hopefully open
1943  * their masks soonish and kick sleeping vcpus to motivate them to
1944  * take IRQs.
1945  */
1946 static void vcpu_intervention_kick(struct kvm_vcpu *vcpu, u64 type)
1947 {
1948 	/* make the VCPU drop out of the SIE, or wake it up if sleeping */
1949 	switch (type) {
1950 	case KVM_S390_MCHK:
1951 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
1952 		break;
1953 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1954 		if (!(type & KVM_S390_INT_IO_AI_MASK &&
1955 		      vcpu->kvm->arch.gisa_int.origin) ||
1956 		      kvm_s390_pv_cpu_get_handle(vcpu))
1957 			kvm_s390_set_cpuflags(vcpu, CPUSTAT_IO_INT);
1958 		break;
1959 	default:
1960 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1961 		break;
1962 	}
1963 	kvm_s390_vcpu_wakeup(vcpu);
1964 }
1965 
1966 static void kick_cpu_irq(struct kvm *kvm, u64 type, u64 parm)
1967 {
1968 	struct kvm_vcpu *dst_vcpu;
1969 	int sigcpu, online_vcpus, nr_tries = 0;
1970 	u64 irq_pend_mask;
1971 	unsigned long i;
1972 
1973 	online_vcpus = atomic_read(&kvm->online_vcpus);
1974 	if (!online_vcpus)
1975 		return;
1976 
1977 	irq_pend_mask = inti_to_irq_pend_mask(type, parm);
1978 	for (sigcpu = kvm->arch.float_int.last_sleep_cpu; ; sigcpu++) {
1979 		sigcpu %= online_vcpus;
1980 		dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1981 		if (!is_vcpu_stopped(dst_vcpu) &&
1982 		    deliverable_irqs(dst_vcpu) & irq_pend_mask)
1983 			break;
1984 		/* avoid endless loops if all vcpus are stopped */
1985 		if (nr_tries++ >= online_vcpus) {
1986 			dst_vcpu = NULL;
1987 			break;
1988 		}
1989 	}
1990 
1991 	/* Nobody was enabled, time to wake all of them */
1992 	if (!dst_vcpu) {
1993 		kvm_for_each_vcpu(i, dst_vcpu, kvm)
1994 			vcpu_intervention_kick(dst_vcpu, type);
1995 		return;
1996 	}
1997 
1998 	vcpu_intervention_kick(dst_vcpu, type);
1999 }
2000 
2001 void kvm_s390_pv_sclp_kick(struct kvm_vcpu *vcpu)
2002 {
2003 	/*
2004 	 * The cpu that called sclp likely will also take the IRQ, no
2005 	 * need to kick anyone.
2006 	 */
2007 	if (deliverable_irqs(vcpu) & BIT(IRQ_PEND_EXT_SERVICE))
2008 		return;
2009 
2010 	/*
2011 	 * For the other cases we might have sleeping cpus with open
2012 	 * masks. Time to find and kick them.
2013 	 */
2014 	kick_cpu_irq(vcpu->kvm, KVM_S390_INT_SERVICE, -1);
2015 }
2016 
2017 /*
2018  * Find a destination VCPU for a floating irq and kick it.
2019  */
2020 static void __floating_irq_kick(struct kvm *kvm, u64 type, u64 parm)
2021 {
2022 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2023 
2024 	/*
2025 	 * No need to kick on non-ev service IRQs for PV VMs, we're
2026 	 * not allowed to inject anyway. We need to wait for the sclp
2027 	 * instruction notification AFTER re-entry of the vcpu that
2028 	 * handled the instruction intercept.
2029 	 */
2030 	if (type == KVM_S390_INT_SERVICE && !(parm & SCCB_EVENT_PENDING) &&
2031 	    test_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs))
2032 		return;
2033 
2034 	kick_cpu_irq(kvm, type, parm);
2035 }
2036 
2037 static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
2038 {
2039 	u64 type = READ_ONCE(inti->type);
2040 	u64 parm = -1;
2041 	int rc;
2042 
2043 	switch (type) {
2044 	case KVM_S390_MCHK:
2045 		rc = __inject_float_mchk(kvm, inti);
2046 		break;
2047 	case KVM_S390_INT_VIRTIO:
2048 		rc = __inject_virtio(kvm, inti);
2049 		break;
2050 	case KVM_S390_INT_SERVICE:
2051 		parm = inti->ext.ext_params & SCCB_EVENT_PENDING;
2052 		rc = __inject_service(kvm, inti);
2053 		break;
2054 	case KVM_S390_INT_PFAULT_DONE:
2055 		rc = __inject_pfault_done(kvm, inti);
2056 		break;
2057 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2058 		/* Grab isc here since __inject_io() might free inti */
2059 		parm = int_word_to_isc(inti->io.io_int_word);
2060 		rc = __inject_io(kvm, inti);
2061 		break;
2062 	default:
2063 		rc = -EINVAL;
2064 	}
2065 	if (rc)
2066 		return rc;
2067 
2068 	__floating_irq_kick(kvm, type, parm);
2069 	return 0;
2070 }
2071 
2072 int kvm_s390_inject_vm(struct kvm *kvm,
2073 		       struct kvm_s390_interrupt *s390int, struct kvm_s390_interrupt_info *inti)
2074 {
2075 	int rc;
2076 
2077 	inti->type = s390int->type;
2078 	switch (inti->type) {
2079 	case KVM_S390_INT_VIRTIO:
2080 		VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
2081 			 s390int->parm, s390int->parm64);
2082 		inti->ext.ext_params = s390int->parm;
2083 		inti->ext.ext_params2 = s390int->parm64;
2084 		break;
2085 	case KVM_S390_INT_SERVICE:
2086 		VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
2087 		inti->ext.ext_params = s390int->parm;
2088 		break;
2089 	case KVM_S390_INT_PFAULT_DONE:
2090 		inti->ext.ext_params2 = s390int->parm64;
2091 		break;
2092 	case KVM_S390_MCHK:
2093 		VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
2094 			 s390int->parm64);
2095 		inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
2096 		inti->mchk.mcic = s390int->parm64;
2097 		break;
2098 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2099 		inti->io.subchannel_id = s390int->parm >> 16;
2100 		inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
2101 		inti->io.io_int_parm = s390int->parm64 >> 32;
2102 		inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
2103 		break;
2104 	default:
2105 		return -EINVAL;
2106 	}
2107 	trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
2108 				 2);
2109 
2110 	rc = __inject_vm(kvm, inti);
2111 
2112 	return rc;
2113 }
2114 
2115 int kvm_s390_reinject_io_int(struct kvm *kvm,
2116 			      struct kvm_s390_interrupt_info *inti)
2117 {
2118 	return __inject_vm(kvm, inti);
2119 }
2120 
2121 int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
2122 		       struct kvm_s390_irq *irq)
2123 {
2124 	irq->type = s390int->type;
2125 	switch (irq->type) {
2126 	case KVM_S390_PROGRAM_INT:
2127 		if (s390int->parm & 0xffff0000)
2128 			return -EINVAL;
2129 		irq->u.pgm.code = s390int->parm;
2130 		break;
2131 	case KVM_S390_SIGP_SET_PREFIX:
2132 		irq->u.prefix.address = s390int->parm;
2133 		break;
2134 	case KVM_S390_SIGP_STOP:
2135 		irq->u.stop.flags = s390int->parm;
2136 		break;
2137 	case KVM_S390_INT_EXTERNAL_CALL:
2138 		if (s390int->parm & 0xffff0000)
2139 			return -EINVAL;
2140 		irq->u.extcall.code = s390int->parm;
2141 		break;
2142 	case KVM_S390_INT_EMERGENCY:
2143 		if (s390int->parm & 0xffff0000)
2144 			return -EINVAL;
2145 		irq->u.emerg.code = s390int->parm;
2146 		break;
2147 	case KVM_S390_MCHK:
2148 		irq->u.mchk.mcic = s390int->parm64;
2149 		break;
2150 	case KVM_S390_INT_PFAULT_INIT:
2151 		irq->u.ext.ext_params = s390int->parm;
2152 		irq->u.ext.ext_params2 = s390int->parm64;
2153 		break;
2154 	case KVM_S390_RESTART:
2155 	case KVM_S390_INT_CLOCK_COMP:
2156 	case KVM_S390_INT_CPU_TIMER:
2157 		break;
2158 	default:
2159 		return -EINVAL;
2160 	}
2161 	return 0;
2162 }
2163 
2164 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
2165 {
2166 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2167 
2168 	return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
2169 }
2170 
2171 int kvm_s390_is_restart_irq_pending(struct kvm_vcpu *vcpu)
2172 {
2173 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2174 
2175 	return test_bit(IRQ_PEND_RESTART, &li->pending_irqs);
2176 }
2177 
2178 void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
2179 {
2180 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2181 
2182 	spin_lock(&li->lock);
2183 	li->irq.stop.flags = 0;
2184 	clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
2185 	spin_unlock(&li->lock);
2186 }
2187 
2188 static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq, bool *storestatus)
2189 {
2190 	int rc;
2191 
2192 	switch (irq->type) {
2193 	case KVM_S390_PROGRAM_INT:
2194 		rc = __inject_prog(vcpu, irq);
2195 		break;
2196 	case KVM_S390_SIGP_SET_PREFIX:
2197 		rc = __inject_set_prefix(vcpu, irq);
2198 		break;
2199 	case KVM_S390_SIGP_STOP:
2200 		rc = __inject_sigp_stop(vcpu, irq, storestatus);
2201 		break;
2202 	case KVM_S390_RESTART:
2203 		rc = __inject_sigp_restart(vcpu);
2204 		break;
2205 	case KVM_S390_INT_CLOCK_COMP:
2206 		rc = __inject_ckc(vcpu);
2207 		break;
2208 	case KVM_S390_INT_CPU_TIMER:
2209 		rc = __inject_cpu_timer(vcpu);
2210 		break;
2211 	case KVM_S390_INT_EXTERNAL_CALL:
2212 		rc = __inject_extcall(vcpu, irq);
2213 		break;
2214 	case KVM_S390_INT_EMERGENCY:
2215 		rc = __inject_sigp_emergency(vcpu, irq);
2216 		break;
2217 	case KVM_S390_MCHK:
2218 		rc = __inject_mchk(vcpu, irq);
2219 		break;
2220 	case KVM_S390_INT_PFAULT_INIT:
2221 		rc = __inject_pfault_init(vcpu, irq);
2222 		break;
2223 	case KVM_S390_INT_VIRTIO:
2224 	case KVM_S390_INT_SERVICE:
2225 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2226 	default:
2227 		rc = -EINVAL;
2228 	}
2229 
2230 	return rc;
2231 }
2232 
2233 int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
2234 {
2235 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2236 	bool storestatus = false;
2237 	int rc;
2238 
2239 	spin_lock(&li->lock);
2240 	rc = do_inject_vcpu(vcpu, irq, &storestatus);
2241 	spin_unlock(&li->lock);
2242 
2243 	if (rc == -EWOULDBLOCK && storestatus)
2244 		rc = kvm_s390_store_status_unloaded(vcpu, KVM_S390_STORE_STATUS_NOADDR);
2245 
2246 	if (!rc)
2247 		kvm_s390_vcpu_wakeup(vcpu);
2248 	return rc;
2249 }
2250 
2251 static inline void clear_irq_list(struct list_head *_list)
2252 {
2253 	struct kvm_s390_interrupt_info *inti, *n;
2254 
2255 	list_for_each_entry_safe(inti, n, _list, list) {
2256 		list_del(&inti->list);
2257 		kfree(inti);
2258 	}
2259 }
2260 
2261 static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
2262 		       struct kvm_s390_irq *irq)
2263 {
2264 	irq->type = inti->type;
2265 	switch (inti->type) {
2266 	case KVM_S390_INT_PFAULT_INIT:
2267 	case KVM_S390_INT_PFAULT_DONE:
2268 	case KVM_S390_INT_VIRTIO:
2269 		irq->u.ext = inti->ext;
2270 		break;
2271 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2272 		irq->u.io = inti->io;
2273 		break;
2274 	}
2275 }
2276 
2277 void kvm_s390_clear_float_irqs(struct kvm *kvm)
2278 {
2279 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2280 	int i;
2281 	unsigned long flags;
2282 
2283 	mutex_lock(&kvm->lock);
2284 	if (!kvm_s390_pv_is_protected(kvm))
2285 		fi->masked_irqs = 0;
2286 	mutex_unlock(&kvm->lock);
2287 	spin_lock_irqsave(&fi->lock, flags);
2288 	fi->pending_irqs = 0;
2289 	memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
2290 	memset(&fi->mchk, 0, sizeof(fi->mchk));
2291 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
2292 		clear_irq_list(&fi->lists[i]);
2293 	for (i = 0; i < FIRQ_MAX_COUNT; i++)
2294 		fi->counters[i] = 0;
2295 	spin_unlock_irqrestore(&fi->lock, flags);
2296 	kvm_s390_gisa_clear(kvm);
2297 };
2298 
2299 static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
2300 {
2301 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
2302 	struct kvm_s390_interrupt_info *inti;
2303 	struct kvm_s390_float_interrupt *fi;
2304 	struct kvm_s390_irq *buf;
2305 	struct kvm_s390_irq *irq;
2306 	int max_irqs;
2307 	int ret = 0;
2308 	int n = 0;
2309 	int i;
2310 	unsigned long flags;
2311 
2312 	if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
2313 		return -EINVAL;
2314 
2315 	/*
2316 	 * We are already using -ENOMEM to signal
2317 	 * userspace it may retry with a bigger buffer,
2318 	 * so we need to use something else for this case
2319 	 */
2320 	buf = vzalloc(len);
2321 	if (!buf)
2322 		return -ENOBUFS;
2323 
2324 	max_irqs = len / sizeof(struct kvm_s390_irq);
2325 
2326 	if (gi->origin && gisa_get_ipm(gi->origin)) {
2327 		for (i = 0; i <= MAX_ISC; i++) {
2328 			if (n == max_irqs) {
2329 				/* signal userspace to try again */
2330 				ret = -ENOMEM;
2331 				goto out_nolock;
2332 			}
2333 			if (gisa_test_ipm_gisc(gi->origin, i)) {
2334 				irq = (struct kvm_s390_irq *) &buf[n];
2335 				irq->type = KVM_S390_INT_IO(1, 0, 0, 0);
2336 				irq->u.io.io_int_word = isc_to_int_word(i);
2337 				n++;
2338 			}
2339 		}
2340 	}
2341 	fi = &kvm->arch.float_int;
2342 	spin_lock_irqsave(&fi->lock, flags);
2343 	for (i = 0; i < FIRQ_LIST_COUNT; i++) {
2344 		list_for_each_entry(inti, &fi->lists[i], list) {
2345 			if (n == max_irqs) {
2346 				/* signal userspace to try again */
2347 				ret = -ENOMEM;
2348 				goto out;
2349 			}
2350 			inti_to_irq(inti, &buf[n]);
2351 			n++;
2352 		}
2353 	}
2354 	if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs) ||
2355 	    test_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs)) {
2356 		if (n == max_irqs) {
2357 			/* signal userspace to try again */
2358 			ret = -ENOMEM;
2359 			goto out;
2360 		}
2361 		irq = (struct kvm_s390_irq *) &buf[n];
2362 		irq->type = KVM_S390_INT_SERVICE;
2363 		irq->u.ext = fi->srv_signal;
2364 		n++;
2365 	}
2366 	if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
2367 		if (n == max_irqs) {
2368 				/* signal userspace to try again */
2369 				ret = -ENOMEM;
2370 				goto out;
2371 		}
2372 		irq = (struct kvm_s390_irq *) &buf[n];
2373 		irq->type = KVM_S390_MCHK;
2374 		irq->u.mchk = fi->mchk;
2375 		n++;
2376 }
2377 
2378 out:
2379 	spin_unlock_irqrestore(&fi->lock, flags);
2380 out_nolock:
2381 	if (!ret && n > 0) {
2382 		if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
2383 			ret = -EFAULT;
2384 	}
2385 	vfree(buf);
2386 
2387 	return ret < 0 ? ret : n;
2388 }
2389 
2390 static int flic_ais_mode_get_all(struct kvm *kvm, struct kvm_device_attr *attr)
2391 {
2392 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2393 	struct kvm_s390_ais_all ais;
2394 	unsigned long flags;
2395 
2396 	if (attr->attr < sizeof(ais))
2397 		return -EINVAL;
2398 
2399 	if (!test_kvm_facility(kvm, 72))
2400 		return -EOPNOTSUPP;
2401 
2402 	spin_lock_irqsave(&fi->ais_lock, flags);
2403 	ais.simm = fi->simm;
2404 	ais.nimm = fi->nimm;
2405 	spin_unlock_irqrestore(&fi->ais_lock, flags);
2406 
2407 	if (copy_to_user((void __user *)attr->addr, &ais, sizeof(ais)))
2408 		return -EFAULT;
2409 
2410 	return 0;
2411 }
2412 
2413 static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2414 {
2415 	int r;
2416 
2417 	switch (attr->group) {
2418 	case KVM_DEV_FLIC_GET_ALL_IRQS:
2419 		r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
2420 					  attr->attr);
2421 		break;
2422 	case KVM_DEV_FLIC_AISM_ALL:
2423 		r = flic_ais_mode_get_all(dev->kvm, attr);
2424 		break;
2425 	default:
2426 		r = -EINVAL;
2427 	}
2428 
2429 	return r;
2430 }
2431 
2432 static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
2433 				     u64 addr)
2434 {
2435 	struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
2436 	void *target = NULL;
2437 	void __user *source;
2438 	u64 size;
2439 
2440 	if (get_user(inti->type, (u64 __user *)addr))
2441 		return -EFAULT;
2442 
2443 	switch (inti->type) {
2444 	case KVM_S390_INT_PFAULT_INIT:
2445 	case KVM_S390_INT_PFAULT_DONE:
2446 	case KVM_S390_INT_VIRTIO:
2447 	case KVM_S390_INT_SERVICE:
2448 		target = (void *) &inti->ext;
2449 		source = &uptr->u.ext;
2450 		size = sizeof(inti->ext);
2451 		break;
2452 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2453 		target = (void *) &inti->io;
2454 		source = &uptr->u.io;
2455 		size = sizeof(inti->io);
2456 		break;
2457 	case KVM_S390_MCHK:
2458 		target = (void *) &inti->mchk;
2459 		source = &uptr->u.mchk;
2460 		size = sizeof(inti->mchk);
2461 		break;
2462 	default:
2463 		return -EINVAL;
2464 	}
2465 
2466 	if (copy_from_user(target, source, size))
2467 		return -EFAULT;
2468 
2469 	return 0;
2470 }
2471 
2472 static int enqueue_floating_irq(struct kvm_device *dev,
2473 				struct kvm_device_attr *attr)
2474 {
2475 	struct kvm_s390_interrupt_info *inti = NULL;
2476 	int r = 0;
2477 	int len = attr->attr;
2478 
2479 	if (len % sizeof(struct kvm_s390_irq) != 0)
2480 		return -EINVAL;
2481 	else if (len > KVM_S390_FLIC_MAX_BUFFER)
2482 		return -EINVAL;
2483 
2484 	while (len >= sizeof(struct kvm_s390_irq)) {
2485 		inti = kzalloc_obj(*inti, GFP_KERNEL_ACCOUNT);
2486 		if (!inti)
2487 			return -ENOMEM;
2488 
2489 		r = copy_irq_from_user(inti, attr->addr);
2490 		if (r) {
2491 			kfree(inti);
2492 			return r;
2493 		}
2494 		r = __inject_vm(dev->kvm, inti);
2495 		if (r) {
2496 			kfree(inti);
2497 			return r;
2498 		}
2499 		len -= sizeof(struct kvm_s390_irq);
2500 		attr->addr += sizeof(struct kvm_s390_irq);
2501 	}
2502 
2503 	return r;
2504 }
2505 
2506 static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
2507 {
2508 	if (id >= MAX_S390_IO_ADAPTERS)
2509 		return NULL;
2510 	id = array_index_nospec(id, MAX_S390_IO_ADAPTERS);
2511 	return kvm->arch.adapters[id];
2512 }
2513 
2514 static int register_io_adapter(struct kvm_device *dev,
2515 			       struct kvm_device_attr *attr)
2516 {
2517 	struct s390_io_adapter *adapter;
2518 	struct kvm_s390_io_adapter adapter_info;
2519 	int rc = 0;
2520 
2521 	mutex_lock(&dev->kvm->lock);
2522 	if (copy_from_user(&adapter_info,
2523 			   (void __user *)attr->addr, sizeof(adapter_info))) {
2524 		rc = -EFAULT;
2525 		goto out;
2526 	}
2527 	if (adapter_info.id >= MAX_S390_IO_ADAPTERS) {
2528 		rc = -EINVAL;
2529 		goto out;
2530 	}
2531 	adapter_info.id = array_index_nospec(adapter_info.id,
2532 					     MAX_S390_IO_ADAPTERS);
2533 
2534 	if (dev->kvm->arch.adapters[adapter_info.id] != NULL) {
2535 		rc = -EINVAL;
2536 		goto out;
2537 	}
2538 	adapter = kzalloc_obj(*adapter, GFP_KERNEL_ACCOUNT);
2539 	if (!adapter) {
2540 		rc = -ENOMEM;
2541 		goto out;
2542 	}
2543 
2544 	INIT_LIST_HEAD(&adapter->maps);
2545 	spin_lock_init(&adapter->maps_lock);
2546 	adapter->nr_maps = 0;
2547 	adapter->id = adapter_info.id;
2548 	adapter->isc = adapter_info.isc;
2549 	adapter->maskable = adapter_info.maskable;
2550 	adapter->masked = false;
2551 	adapter->swap = adapter_info.swap;
2552 	adapter->suppressible = adapter_info.flags &
2553 				KVM_S390_ADAPTER_SUPPRESSIBLE;
2554 	dev->kvm->arch.adapters[adapter->id] = adapter;
2555 
2556 out:
2557 	mutex_unlock(&dev->kvm->lock);
2558 	return rc;
2559 }
2560 
2561 int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
2562 {
2563 	int ret;
2564 	struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2565 
2566 	if (!adapter || !adapter->maskable)
2567 		return -EINVAL;
2568 	ret = adapter->masked;
2569 	adapter->masked = masked;
2570 	return ret;
2571 }
2572 
2573 static struct page *pin_map_page(struct kvm *kvm, u64 uaddr,
2574 				 unsigned int gup_flags)
2575 {
2576 	struct mm_struct *mm = kvm->mm;
2577 	struct page *page = NULL;
2578 	int locked = 1;
2579 
2580 	if (mmget_not_zero(mm)) {
2581 		mmap_read_lock(mm);
2582 		pin_user_pages_remote(mm, uaddr, 1, FOLL_WRITE | gup_flags,
2583 				      &page, &locked);
2584 		if (locked)
2585 			mmap_read_unlock(mm);
2586 		mmput(mm);
2587 	}
2588 
2589 	return page;
2590 }
2591 
2592 static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
2593 {
2594 	struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2595 	struct s390_map_info *map;
2596 	unsigned long flags;
2597 	__u64 host_addr;
2598 	int ret, idx;
2599 
2600 	if (!adapter || !addr)
2601 		return -EINVAL;
2602 
2603 	map = kzalloc_obj(*map, GFP_KERNEL_ACCOUNT);
2604 	if (!map)
2605 		return -ENOMEM;
2606 
2607 	INIT_LIST_HEAD(&map->list);
2608 	idx = srcu_read_lock(&kvm->srcu);
2609 	host_addr = gpa_to_hva(kvm, addr);
2610 	if (kvm_is_error_hva(host_addr)) {
2611 		srcu_read_unlock(&kvm->srcu, idx);
2612 		ret = -EFAULT;
2613 		goto out;
2614 	}
2615 	srcu_read_unlock(&kvm->srcu, idx);
2616 	map->guest_addr = addr;
2617 	map->addr = host_addr;
2618 	map->page = pin_map_page(kvm, host_addr, FOLL_LONGTERM);
2619 	if (!map->page) {
2620 		/*
2621 		 * Long-term pinning may fail for memory types such as file-backed
2622 		 * memory. Verify that short-term pinning succeeds so that the
2623 		 * non-atomic irqfd path can handle interrupt injection.
2624 		 */
2625 		map->page = pin_map_page(kvm, host_addr, 0);
2626 		if (!map->page) {
2627 			ret = -EINVAL;
2628 			goto out;
2629 		}
2630 		unpin_user_page(map->page);
2631 		map->page = NULL;
2632 		map->pinned = false;
2633 		/* Add an entry to preserve MAP/UNMAP symmetry. */
2634 	} else {
2635 		map->pinned = true;
2636 	}
2637 	spin_lock_irqsave(&adapter->maps_lock, flags);
2638 	if (adapter->nr_maps < MAX_S390_ADAPTER_MAPS) {
2639 		list_add_tail(&map->list, &adapter->maps);
2640 		adapter->nr_maps++;
2641 		ret = 0;
2642 	} else {
2643 		ret = -EINVAL;
2644 	}
2645 	spin_unlock_irqrestore(&adapter->maps_lock, flags);
2646 	if (ret && map->page)
2647 		unpin_user_page(map->page);
2648 out:
2649 	if (ret)
2650 		kfree(map);
2651 	return ret;
2652 }
2653 
2654 static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
2655 {
2656 	struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2657 	struct s390_map_info *map, *tmp, *map_to_free;
2658 	struct page *map_page_to_put = NULL;
2659 	u64 map_addr_to_mark = 0;
2660 	bool map_pinned = false;
2661 	unsigned long flags;
2662 	int found = 0, idx;
2663 
2664 	if (!adapter || !addr)
2665 		return -EINVAL;
2666 
2667 	spin_lock_irqsave(&adapter->maps_lock, flags);
2668 	list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
2669 		if (map->guest_addr == addr) {
2670 			found = 1;
2671 			adapter->nr_maps--;
2672 			list_del(&map->list);
2673 			map_page_to_put = map->page;
2674 			map_addr_to_mark = map->guest_addr;
2675 			map_pinned = map->pinned;
2676 			map_to_free = map;
2677 			break;
2678 		}
2679 	}
2680 	spin_unlock_irqrestore(&adapter->maps_lock, flags);
2681 
2682 	if (found) {
2683 		kfree(map_to_free);
2684 		if (map_pinned) {
2685 			/*
2686 			 * Only long-term pinned pages need to be marked dirty
2687 			 * and released. Fallback entries exist only for
2688 			 * MAP/UNMAP symmetry.
2689 			 */
2690 			idx = srcu_read_lock(&kvm->srcu);
2691 			mark_page_dirty(kvm, map_addr_to_mark >> PAGE_SHIFT);
2692 			set_page_dirty_lock(map_page_to_put);
2693 			srcu_read_unlock(&kvm->srcu, idx);
2694 			unpin_user_page(map_page_to_put);
2695 		}
2696 	}
2697 
2698 	return found ? 0 : -ENOENT;
2699 }
2700 
2701 void kvm_s390_unmap_all_adapters(struct kvm *kvm)
2702 {
2703 	struct s390_map_info *map, *tmp;
2704 	unsigned long flags;
2705 	int i, idx;
2706 
2707 	for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2708 		struct s390_io_adapter *adapter = kvm->arch.adapters[i];
2709 		LIST_HEAD(local_list);
2710 
2711 		if (!adapter)
2712 			continue;
2713 
2714 		spin_lock_irqsave(&adapter->maps_lock, flags);
2715 		list_splice_init(&adapter->maps, &local_list);
2716 		adapter->nr_maps = 0;
2717 		spin_unlock_irqrestore(&adapter->maps_lock, flags);
2718 
2719 		list_for_each_entry_safe(map, tmp, &local_list, list) {
2720 			list_del(&map->list);
2721 			if (map->pinned) {
2722 				idx = srcu_read_lock(&kvm->srcu);
2723 				mark_page_dirty(kvm, map->guest_addr >> PAGE_SHIFT);
2724 				set_page_dirty_lock(map->page);
2725 				srcu_read_unlock(&kvm->srcu, idx);
2726 				unpin_user_page(map->page);
2727 			}
2728 			kfree(map);
2729 		}
2730 	}
2731 }
2732 
2733 void kvm_s390_destroy_adapters(struct kvm *kvm)
2734 {
2735 	int i;
2736 
2737 	kvm_s390_unmap_all_adapters(kvm);
2738 
2739 	for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2740 		kfree(kvm->arch.adapters[i]);
2741 		kvm->arch.adapters[i] = NULL;
2742 	}
2743 }
2744 
2745 static int modify_io_adapter(struct kvm_device *dev,
2746 			     struct kvm_device_attr *attr)
2747 {
2748 	struct kvm_s390_io_adapter_req req;
2749 	struct s390_io_adapter *adapter;
2750 	int ret;
2751 
2752 	if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2753 		return -EFAULT;
2754 
2755 	adapter = get_io_adapter(dev->kvm, req.id);
2756 	if (!adapter)
2757 		return -EINVAL;
2758 	switch (req.type) {
2759 	case KVM_S390_IO_ADAPTER_MASK:
2760 		ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
2761 		if (ret > 0)
2762 			ret = 0;
2763 		break;
2764 	case KVM_S390_IO_ADAPTER_MAP:
2765 	case KVM_S390_IO_ADAPTER_UNMAP:
2766 		/* If in Secure Execution mode do not long term pin. */
2767 		mutex_lock(&dev->kvm->lock);
2768 		if (kvm_s390_pv_is_protected(dev->kvm)) {
2769 			mutex_unlock(&dev->kvm->lock);
2770 			return 0;
2771 		}
2772 		if (req.type == KVM_S390_IO_ADAPTER_MAP) {
2773 			dev->kvm->stat.io_390_adapter_map++;
2774 			ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2775 		} else {
2776 			dev->kvm->stat.io_390_adapter_unmap++;
2777 			ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2778 		}
2779 		mutex_unlock(&dev->kvm->lock);
2780 		break;
2781 	default:
2782 		ret = -EINVAL;
2783 	}
2784 
2785 	return ret;
2786 }
2787 
2788 static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr)
2789 
2790 {
2791 	const u64 isc_mask = 0xffUL << 24; /* all iscs set */
2792 	u32 schid;
2793 
2794 	if (attr->flags)
2795 		return -EINVAL;
2796 	if (attr->attr != sizeof(schid))
2797 		return -EINVAL;
2798 	if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid)))
2799 		return -EFAULT;
2800 	if (!schid)
2801 		return -EINVAL;
2802 	kfree(kvm_s390_get_io_int(kvm, isc_mask, schid));
2803 	/*
2804 	 * If userspace is conforming to the architecture, we can have at most
2805 	 * one pending I/O interrupt per subchannel, so this is effectively a
2806 	 * clear all.
2807 	 */
2808 	return 0;
2809 }
2810 
2811 static int modify_ais_mode(struct kvm *kvm, struct kvm_device_attr *attr)
2812 {
2813 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2814 	struct kvm_s390_ais_req req;
2815 	int ret = 0;
2816 	unsigned long flags;
2817 
2818 	if (!test_kvm_facility(kvm, 72))
2819 		return -EOPNOTSUPP;
2820 
2821 	if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2822 		return -EFAULT;
2823 
2824 	if (req.isc > MAX_ISC)
2825 		return -EINVAL;
2826 
2827 	trace_kvm_s390_modify_ais_mode(req.isc,
2828 				       (fi->simm & AIS_MODE_MASK(req.isc)) ?
2829 				       (fi->nimm & AIS_MODE_MASK(req.isc)) ?
2830 				       2 : KVM_S390_AIS_MODE_SINGLE :
2831 				       KVM_S390_AIS_MODE_ALL, req.mode);
2832 
2833 	spin_lock_irqsave(&fi->ais_lock, flags);
2834 	switch (req.mode) {
2835 	case KVM_S390_AIS_MODE_ALL:
2836 		fi->simm &= ~AIS_MODE_MASK(req.isc);
2837 		fi->nimm &= ~AIS_MODE_MASK(req.isc);
2838 		break;
2839 	case KVM_S390_AIS_MODE_SINGLE:
2840 		fi->simm |= AIS_MODE_MASK(req.isc);
2841 		fi->nimm &= ~AIS_MODE_MASK(req.isc);
2842 		break;
2843 	default:
2844 		ret = -EINVAL;
2845 	}
2846 	spin_unlock_irqrestore(&fi->ais_lock, flags);
2847 
2848 	return ret;
2849 }
2850 
2851 static int kvm_s390_inject_airq(struct kvm *kvm,
2852 				struct s390_io_adapter *adapter)
2853 {
2854 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2855 	struct kvm_s390_interrupt s390int = {
2856 		.type = KVM_S390_INT_IO(1, 0, 0, 0),
2857 		.parm = 0,
2858 		.parm64 = isc_to_int_word(adapter->isc),
2859 	};
2860 	struct kvm_s390_interrupt_info *inti;
2861 	unsigned long flags;
2862 
2863 	int ret = 0;
2864 
2865 	inti = kzalloc_obj(*inti, GFP_KERNEL_ACCOUNT);
2866 	if (!inti)
2867 		return -ENOMEM;
2868 
2869 	if (!test_kvm_facility(kvm, 72) || !adapter->suppressible) {
2870 		ret = kvm_s390_inject_vm(kvm, &s390int, inti);
2871 		if (ret)
2872 			kfree(inti);
2873 		return ret;
2874 	}
2875 
2876 	spin_lock_irqsave(&fi->ais_lock, flags);
2877 	if (fi->nimm & AIS_MODE_MASK(adapter->isc)) {
2878 		trace_kvm_s390_airq_suppressed(adapter->id, adapter->isc);
2879 		spin_unlock_irqrestore(&fi->ais_lock, flags);
2880 		kfree(inti);
2881 		return ret;
2882 	}
2883 
2884 	ret = kvm_s390_inject_vm(kvm, &s390int, inti);
2885 
2886 	if (!ret && (fi->simm & AIS_MODE_MASK(adapter->isc))) {
2887 		fi->nimm |= AIS_MODE_MASK(adapter->isc);
2888 		trace_kvm_s390_modify_ais_mode(adapter->isc,
2889 					       KVM_S390_AIS_MODE_SINGLE, 2);
2890 	}
2891 
2892 	spin_unlock_irqrestore(&fi->ais_lock, flags);
2893 	if (ret)
2894 		kfree(inti);
2895 	return ret;
2896 }
2897 
2898 static int flic_inject_airq(struct kvm *kvm, struct kvm_device_attr *attr)
2899 {
2900 	unsigned int id = attr->attr;
2901 	struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2902 
2903 	kvm->stat.io_flic_inject_airq++;
2904 
2905 	if (!adapter)
2906 		return -EINVAL;
2907 
2908 	return kvm_s390_inject_airq(kvm, adapter);
2909 }
2910 
2911 static int flic_ais_mode_set_all(struct kvm *kvm, struct kvm_device_attr *attr)
2912 {
2913 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2914 	struct kvm_s390_ais_all ais;
2915 	unsigned long flags;
2916 
2917 	if (!test_kvm_facility(kvm, 72))
2918 		return -EOPNOTSUPP;
2919 
2920 	if (copy_from_user(&ais, (void __user *)attr->addr, sizeof(ais)))
2921 		return -EFAULT;
2922 
2923 	spin_lock_irqsave(&fi->ais_lock, flags);
2924 	fi->simm = ais.simm;
2925 	fi->nimm = ais.nimm;
2926 	spin_unlock_irqrestore(&fi->ais_lock, flags);
2927 
2928 	return 0;
2929 }
2930 
2931 static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2932 {
2933 	int r = 0;
2934 	unsigned long i;
2935 	struct kvm_vcpu *vcpu;
2936 
2937 	switch (attr->group) {
2938 	case KVM_DEV_FLIC_ENQUEUE:
2939 		r = enqueue_floating_irq(dev, attr);
2940 		break;
2941 	case KVM_DEV_FLIC_CLEAR_IRQS:
2942 		kvm_s390_clear_float_irqs(dev->kvm);
2943 		break;
2944 	case KVM_DEV_FLIC_APF_ENABLE:
2945 		if (kvm_is_ucontrol(dev->kvm))
2946 			return -EINVAL;
2947 		set_bit(GMAP_FLAG_PFAULT_ENABLED, &dev->kvm->arch.gmap->flags);
2948 		break;
2949 	case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2950 		if (kvm_is_ucontrol(dev->kvm))
2951 			return -EINVAL;
2952 		clear_bit(GMAP_FLAG_PFAULT_ENABLED, &dev->kvm->arch.gmap->flags);
2953 		/*
2954 		 * Make sure no async faults are in transition when
2955 		 * clearing the queues. So we don't need to worry
2956 		 * about late coming workers.
2957 		 */
2958 		synchronize_srcu(&dev->kvm->srcu);
2959 		kvm_for_each_vcpu(i, vcpu, dev->kvm)
2960 			kvm_clear_async_pf_completion_queue(vcpu);
2961 		break;
2962 	case KVM_DEV_FLIC_ADAPTER_REGISTER:
2963 		r = register_io_adapter(dev, attr);
2964 		break;
2965 	case KVM_DEV_FLIC_ADAPTER_MODIFY:
2966 		r = modify_io_adapter(dev, attr);
2967 		break;
2968 	case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2969 		r = clear_io_irq(dev->kvm, attr);
2970 		break;
2971 	case KVM_DEV_FLIC_AISM:
2972 		r = modify_ais_mode(dev->kvm, attr);
2973 		break;
2974 	case KVM_DEV_FLIC_AIRQ_INJECT:
2975 		r = flic_inject_airq(dev->kvm, attr);
2976 		break;
2977 	case KVM_DEV_FLIC_AISM_ALL:
2978 		r = flic_ais_mode_set_all(dev->kvm, attr);
2979 		break;
2980 	default:
2981 		r = -EINVAL;
2982 	}
2983 
2984 	return r;
2985 }
2986 
2987 static int flic_has_attr(struct kvm_device *dev,
2988 			     struct kvm_device_attr *attr)
2989 {
2990 	switch (attr->group) {
2991 	case KVM_DEV_FLIC_GET_ALL_IRQS:
2992 	case KVM_DEV_FLIC_ENQUEUE:
2993 	case KVM_DEV_FLIC_CLEAR_IRQS:
2994 	case KVM_DEV_FLIC_APF_ENABLE:
2995 	case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2996 	case KVM_DEV_FLIC_ADAPTER_REGISTER:
2997 	case KVM_DEV_FLIC_ADAPTER_MODIFY:
2998 	case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2999 	case KVM_DEV_FLIC_AISM:
3000 	case KVM_DEV_FLIC_AIRQ_INJECT:
3001 	case KVM_DEV_FLIC_AISM_ALL:
3002 		return 0;
3003 	}
3004 	return -ENXIO;
3005 }
3006 
3007 static int flic_create(struct kvm_device *dev, u32 type)
3008 {
3009 	if (!dev)
3010 		return -EINVAL;
3011 	if (dev->kvm->arch.flic)
3012 		return -EINVAL;
3013 	dev->kvm->arch.flic = dev;
3014 	return 0;
3015 }
3016 
3017 static void flic_destroy(struct kvm_device *dev)
3018 {
3019 	dev->kvm->arch.flic = NULL;
3020 	kfree(dev);
3021 }
3022 
3023 /* s390 floating irq controller (flic) */
3024 struct kvm_device_ops kvm_flic_ops = {
3025 	.name = "kvm-flic",
3026 	.get_attr = flic_get_attr,
3027 	.set_attr = flic_set_attr,
3028 	.has_attr = flic_has_attr,
3029 	.create = flic_create,
3030 	.destroy = flic_destroy,
3031 };
3032 
3033 static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
3034 {
3035 	unsigned long bit;
3036 
3037 	bit = bit_nr + (addr % PAGE_SIZE) * 8;
3038 
3039 	/* kvm_set_routing_entry() should never allow this to happen */
3040 	WARN_ON_ONCE(bit > (PAGE_SIZE * BITS_PER_BYTE - 1));
3041 
3042 	return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
3043 }
3044 
3045 static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
3046 					  u64 addr)
3047 {
3048 	struct s390_map_info *map;
3049 
3050 	if (!adapter)
3051 		return NULL;
3052 
3053 	list_for_each_entry(map, &adapter->maps, list) {
3054 		if (map->addr == addr) {
3055 			if (!map->pinned)
3056 				return NULL;
3057 			return map;
3058 		}
3059 	}
3060 	return NULL;
3061 }
3062 
3063 static int adapter_indicators_set(struct kvm *kvm,
3064 				  struct s390_io_adapter *adapter,
3065 				  struct kvm_s390_adapter_int *adapter_int)
3066 {
3067 	struct s390_map_info *ind_info, *summary_info;
3068 	struct page *ind_page, *summary_page;
3069 	unsigned long bit;
3070 	int summary_set;
3071 	void *map;
3072 
3073 	ind_page = NULL;
3074 
3075 	scoped_guard(spinlock_irqsave, &adapter->maps_lock) {
3076 		ind_info = get_map_info(adapter, adapter_int->ind_addr);
3077 		if (ind_info) {
3078 			map = page_address(ind_info->page);
3079 			bit = get_ind_bit(ind_info->addr, adapter_int->ind_offset, adapter->swap);
3080 			set_bit(bit, map);
3081 		}
3082 	}
3083 	if (!ind_info) {
3084 		ind_page = pin_map_page(kvm, adapter_int->ind_addr, 0);
3085 		if (!ind_page)
3086 			return -1;
3087 		map = page_address(ind_page);
3088 		bit = get_ind_bit(adapter_int->ind_addr,
3089 				  adapter_int->ind_offset, adapter->swap);
3090 		set_bit(bit, map);
3091 		set_page_dirty_lock(ind_page);
3092 		unpin_user_page(ind_page);
3093 	}
3094 	scoped_guard(srcu, &kvm->srcu)
3095 		mark_page_dirty(kvm, gpa_to_gfn(adapter_int->ind_gaddr));
3096 
3097 	scoped_guard(spinlock_irqsave, &adapter->maps_lock) {
3098 		summary_info = get_map_info(adapter, adapter_int->summary_addr);
3099 		if (summary_info) {
3100 			map = page_address(summary_info->page);
3101 			bit = get_ind_bit(summary_info->addr, adapter_int->summary_offset,
3102 					  adapter->swap);
3103 			summary_set = test_and_set_bit(bit, map);
3104 		}
3105 	}
3106 	if (!summary_info) {
3107 		summary_page = pin_map_page(kvm, adapter_int->summary_addr, 0);
3108 		if (!summary_page)
3109 			return -1;
3110 		map = page_address(summary_page);
3111 		bit = get_ind_bit(adapter_int->summary_addr,
3112 				  adapter_int->summary_offset, adapter->swap);
3113 		summary_set = test_and_set_bit(bit, map);
3114 		set_page_dirty_lock(summary_page);
3115 		unpin_user_page(summary_page);
3116 	}
3117 	scoped_guard(srcu, &kvm->srcu)
3118 		mark_page_dirty(kvm, gpa_to_gfn(adapter_int->summary_gaddr));
3119 
3120 	return summary_set ? 0 : 1;
3121 }
3122 
3123 static int adapter_indicators_set_fast(struct kvm *kvm,
3124 				       struct s390_io_adapter *adapter,
3125 				       struct kvm_s390_adapter_int *adapter_int,
3126 				       int setbit)
3127 {
3128 	struct s390_map_info *ind_info, *summary_info;
3129 	unsigned long bit;
3130 	int summary_set;
3131 	void *map;
3132 
3133 	guard(srcu)(&kvm->srcu);
3134 	guard(spinlock)(&adapter->maps_lock);
3135 
3136 	ind_info = get_map_info(adapter, adapter_int->ind_addr);
3137 	if (!ind_info)
3138 		return -EWOULDBLOCK;
3139 
3140 	map = page_address(ind_info->page);
3141 	bit = get_ind_bit(ind_info->addr, adapter_int->ind_offset, adapter->swap);
3142 	if (setbit) {
3143 		set_bit(bit, map);
3144 		mark_page_dirty(kvm, gpa_to_gfn(adapter_int->ind_gaddr));
3145 	}
3146 
3147 	summary_info = get_map_info(adapter, adapter_int->summary_addr);
3148 	if (!summary_info)
3149 		return -EWOULDBLOCK;
3150 
3151 	map = page_address(summary_info->page);
3152 	bit = get_ind_bit(summary_info->addr, adapter_int->summary_offset,
3153 			  adapter->swap);
3154 	/* If setbit then set summary bit. Else if falling back to the slow path */
3155 	/* with setbit==0 then clear the summary bit so the slow path re-injects */
3156 	if (setbit)
3157 		summary_set = test_and_set_bit(bit, map);
3158 	else
3159 		summary_set = test_and_clear_bit(bit, map);
3160 	mark_page_dirty(kvm, gpa_to_gfn(adapter_int->summary_gaddr));
3161 
3162 	return summary_set ? 0 : 1;
3163 }
3164 
3165 /*
3166  * < 0 - not injected due to error
3167  * = 0 - coalesced, summary indicator already active
3168  * > 0 - injected interrupt
3169  */
3170 static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
3171 			   struct kvm *kvm, int irq_source_id, int level,
3172 			   bool line_status)
3173 {
3174 	int ret;
3175 	struct s390_io_adapter *adapter;
3176 
3177 	kvm->stat.io_set_adapter_int++;
3178 
3179 	/* We're only interested in the 0->1 transition. */
3180 	if (!level)
3181 		return 0;
3182 	adapter = get_io_adapter(kvm, e->adapter.adapter_id);
3183 	if (!adapter)
3184 		return -1;
3185 	ret = adapter_indicators_set(kvm, adapter, &e->adapter);
3186 	if ((ret > 0) && !adapter->masked) {
3187 		ret = kvm_s390_inject_airq(kvm, adapter);
3188 		if (ret == 0)
3189 			ret = 1;
3190 	}
3191 	return ret;
3192 }
3193 
3194 /*
3195  * Inject the machine check to the guest.
3196  */
3197 void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
3198 				     struct mcck_volatile_info *mcck_info)
3199 {
3200 	struct kvm_s390_irq irq = {};
3201 	union mci mci;
3202 	__u64 cr14 = 0;         /* upper bits are not used */
3203 	int rc;
3204 
3205 	mci.val = mcck_info->mcic;
3206 
3207 	/* log machine checks being reinjected on all debugs */
3208 	VCPU_EVENT(vcpu, 2, "guest machine check %lx", mci.val);
3209 	KVM_EVENT(2, "guest machine check %lx", mci.val);
3210 	pr_info("guest machine check pid %d: %lx", current->pid, mci.val);
3211 
3212 	if (mci.sr)
3213 		cr14 |= CR14_RECOVERY_SUBMASK;
3214 	if (mci.dg)
3215 		cr14 |= CR14_DEGRADATION_SUBMASK;
3216 	if (mci.w)
3217 		cr14 |= CR14_WARNING_SUBMASK;
3218 
3219 	irq.u.mchk.cr14 = cr14;
3220 	irq.u.mchk.mcic = mcck_info->mcic;
3221 	irq.u.mchk.ext_damage_code = mcck_info->ext_damage_code;
3222 	irq.u.mchk.failing_storage_address = mcck_info->failing_storage_address;
3223 
3224 	/* Inject the machine check to specified vcpu */
3225 	irq.type = KVM_S390_MCHK;
3226 	rc = kvm_s390_inject_vcpu(vcpu, &irq);
3227 	WARN_ON_ONCE(rc);
3228 }
3229 
3230 int kvm_set_routing_entry(struct kvm *kvm,
3231 			  struct kvm_kernel_irq_routing_entry *e,
3232 			  const struct kvm_irq_routing_entry *ue)
3233 {
3234 	const struct kvm_irq_routing_s390_adapter *adapter;
3235 	u64 uaddr_s, uaddr_i;
3236 	int idx;
3237 
3238 	switch (ue->type) {
3239 	case KVM_IRQ_ROUTING_S390_ADAPTER:
3240 		if (kvm_is_ucontrol(kvm))
3241 			return -EINVAL;
3242 		e->set = set_adapter_int;
3243 
3244 		adapter = &ue->u.adapter;
3245 		if (adapter->summary_addr + (adapter->summary_offset / 8) >=
3246 		    (adapter->summary_addr & PAGE_MASK) + PAGE_SIZE)
3247 			return -EINVAL;
3248 		if (adapter->ind_addr + (adapter->ind_offset / 8) >=
3249 		    (adapter->ind_addr & PAGE_MASK) + PAGE_SIZE)
3250 			return -EINVAL;
3251 
3252 		idx = srcu_read_lock(&kvm->srcu);
3253 		uaddr_s = gpa_to_hva(kvm, ue->u.adapter.summary_addr);
3254 		uaddr_i = gpa_to_hva(kvm, ue->u.adapter.ind_addr);
3255 		srcu_read_unlock(&kvm->srcu, idx);
3256 
3257 		if (kvm_is_error_hva(uaddr_s) || kvm_is_error_hva(uaddr_i))
3258 			return -EFAULT;
3259 		e->adapter.summary_addr = uaddr_s;
3260 		e->adapter.summary_gaddr = ue->u.adapter.summary_addr;
3261 		e->adapter.ind_addr = uaddr_i;
3262 		e->adapter.ind_gaddr = ue->u.adapter.ind_addr;
3263 		e->adapter.summary_offset = ue->u.adapter.summary_offset;
3264 		e->adapter.ind_offset = ue->u.adapter.ind_offset;
3265 		e->adapter.adapter_id = ue->u.adapter.adapter_id;
3266 		return 0;
3267 	default:
3268 		return -EINVAL;
3269 	}
3270 }
3271 
3272 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
3273 		int irq_source_id, int level, bool line_status)
3274 {
3275 	return -EINVAL;
3276 }
3277 
3278 int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
3279 {
3280 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
3281 	struct kvm_s390_irq *buf __free(kvfree) = NULL;
3282 	bool tmp, storestatus = false;
3283 	int r = 0;
3284 	int n;
3285 
3286 	buf = vmalloc(len);
3287 	if (!buf)
3288 		return -ENOMEM;
3289 
3290 	if (copy_from_user((void *)buf, irqstate, len))
3291 		return -EFAULT;
3292 
3293 	scoped_guard(spinlock, &li->lock) {
3294 		/*
3295 		 * Don't allow setting the interrupt state
3296 		 * when there are already interrupts pending
3297 		 */
3298 		if (li->pending_irqs)
3299 			return -EBUSY;
3300 
3301 		for (n = 0; n < len / sizeof(*buf); n++) {
3302 			tmp = false;
3303 			r = do_inject_vcpu(vcpu, &buf[n], &tmp);
3304 			if (r == -EWOULDBLOCK && tmp) {
3305 				storestatus = true;
3306 				r = 0;
3307 			}
3308 			if (r)
3309 				break;
3310 		}
3311 	}
3312 	if (storestatus) {
3313 		scoped_guard(srcu, &vcpu->kvm->srcu)
3314 			n = kvm_s390_store_status_unloaded(vcpu, KVM_S390_STORE_STATUS_NOADDR);
3315 		return r ? r : n;
3316 	}
3317 
3318 	return r;
3319 }
3320 
3321 static void store_local_irq(struct kvm_s390_local_interrupt *li,
3322 			    struct kvm_s390_irq *irq,
3323 			    unsigned long irq_type)
3324 {
3325 	switch (irq_type) {
3326 	case IRQ_PEND_MCHK_EX:
3327 	case IRQ_PEND_MCHK_REP:
3328 		irq->type = KVM_S390_MCHK;
3329 		irq->u.mchk = li->irq.mchk;
3330 		break;
3331 	case IRQ_PEND_PROG:
3332 		irq->type = KVM_S390_PROGRAM_INT;
3333 		irq->u.pgm = li->irq.pgm;
3334 		break;
3335 	case IRQ_PEND_PFAULT_INIT:
3336 		irq->type = KVM_S390_INT_PFAULT_INIT;
3337 		irq->u.ext = li->irq.ext;
3338 		break;
3339 	case IRQ_PEND_EXT_EXTERNAL:
3340 		irq->type = KVM_S390_INT_EXTERNAL_CALL;
3341 		irq->u.extcall = li->irq.extcall;
3342 		break;
3343 	case IRQ_PEND_EXT_CLOCK_COMP:
3344 		irq->type = KVM_S390_INT_CLOCK_COMP;
3345 		break;
3346 	case IRQ_PEND_EXT_CPU_TIMER:
3347 		irq->type = KVM_S390_INT_CPU_TIMER;
3348 		break;
3349 	case IRQ_PEND_SIGP_STOP:
3350 		irq->type = KVM_S390_SIGP_STOP;
3351 		irq->u.stop = li->irq.stop;
3352 		break;
3353 	case IRQ_PEND_RESTART:
3354 		irq->type = KVM_S390_RESTART;
3355 		break;
3356 	case IRQ_PEND_SET_PREFIX:
3357 		irq->type = KVM_S390_SIGP_SET_PREFIX;
3358 		irq->u.prefix = li->irq.prefix;
3359 		break;
3360 	}
3361 }
3362 
3363 int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
3364 {
3365 	int scn;
3366 	DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
3367 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
3368 	unsigned long pending_irqs;
3369 	struct kvm_s390_irq irq;
3370 	unsigned long irq_type;
3371 	int cpuaddr;
3372 	int n = 0;
3373 
3374 	spin_lock(&li->lock);
3375 	pending_irqs = li->pending_irqs;
3376 	memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
3377 	       sizeof(sigp_emerg_pending));
3378 	spin_unlock(&li->lock);
3379 
3380 	for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
3381 		memset(&irq, 0, sizeof(irq));
3382 		if (irq_type == IRQ_PEND_EXT_EMERGENCY)
3383 			continue;
3384 		if (n + sizeof(irq) > len)
3385 			return -ENOBUFS;
3386 		store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
3387 		if (copy_to_user(&buf[n], &irq, sizeof(irq)))
3388 			return -EFAULT;
3389 		n += sizeof(irq);
3390 	}
3391 
3392 	if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
3393 		for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
3394 			memset(&irq, 0, sizeof(irq));
3395 			if (n + sizeof(irq) > len)
3396 				return -ENOBUFS;
3397 			irq.type = KVM_S390_INT_EMERGENCY;
3398 			irq.u.emerg.code = cpuaddr;
3399 			if (copy_to_user(&buf[n], &irq, sizeof(irq)))
3400 				return -EFAULT;
3401 			n += sizeof(irq);
3402 		}
3403 	}
3404 
3405 	if (sca_ext_call_pending(vcpu, &scn)) {
3406 		if (n + sizeof(irq) > len)
3407 			return -ENOBUFS;
3408 		memset(&irq, 0, sizeof(irq));
3409 		irq.type = KVM_S390_INT_EXTERNAL_CALL;
3410 		irq.u.extcall.code = scn;
3411 		if (copy_to_user(&buf[n], &irq, sizeof(irq)))
3412 			return -EFAULT;
3413 		n += sizeof(irq);
3414 	}
3415 
3416 	return n;
3417 }
3418 
3419 static void __airqs_kick_single_vcpu(struct kvm *kvm, u8 deliverable_mask)
3420 {
3421 	int vcpu_idx, online_vcpus = atomic_read(&kvm->online_vcpus);
3422 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3423 	struct kvm_vcpu *vcpu;
3424 	u8 vcpu_isc_mask;
3425 
3426 	for_each_set_bit(vcpu_idx, kvm->arch.idle_mask, online_vcpus) {
3427 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
3428 		if (psw_ioint_disabled(vcpu))
3429 			continue;
3430 		vcpu_isc_mask = (u8)(vcpu->arch.sie_block->gcr[6] >> 24);
3431 		if (deliverable_mask & vcpu_isc_mask) {
3432 			/* lately kicked but not yet running */
3433 			if (test_and_set_bit(vcpu_idx, gi->kicked_mask))
3434 				return;
3435 			kvm_s390_vcpu_wakeup(vcpu);
3436 			return;
3437 		}
3438 	}
3439 }
3440 
3441 static enum hrtimer_restart gisa_vcpu_kicker(struct hrtimer *timer)
3442 {
3443 	struct kvm_s390_gisa_interrupt *gi =
3444 		container_of(timer, struct kvm_s390_gisa_interrupt, timer);
3445 	struct kvm *kvm =
3446 		container_of(gi->origin, struct sie_page2, gisa)->kvm;
3447 	u8 pending_mask;
3448 
3449 	pending_mask = gisa_get_ipm_or_restore_iam(gi);
3450 	if (pending_mask) {
3451 		__airqs_kick_single_vcpu(kvm, pending_mask);
3452 		hrtimer_forward_now(timer, ns_to_ktime(gi->expires));
3453 		return HRTIMER_RESTART;
3454 	}
3455 
3456 	return HRTIMER_NORESTART;
3457 }
3458 
3459 #define NULL_GISA_ADDR 0x00000000UL
3460 #define NONE_GISA_ADDR 0x00000001UL
3461 #define GISA_ADDR_MASK 0xfffff000UL
3462 
3463 static void process_gib_alert_list(void)
3464 {
3465 	struct kvm_s390_gisa_interrupt *gi;
3466 	u32 final, gisa_phys, origin = 0UL;
3467 	struct kvm_s390_gisa *gisa;
3468 	struct kvm *kvm;
3469 
3470 	do {
3471 		/*
3472 		 * If the NONE_GISA_ADDR is still stored in the alert list
3473 		 * origin, we will leave the outer loop. No further GISA has
3474 		 * been added to the alert list by millicode while processing
3475 		 * the current alert list.
3476 		 */
3477 		final = (origin & NONE_GISA_ADDR);
3478 		/*
3479 		 * Cut off the alert list and store the NONE_GISA_ADDR in the
3480 		 * alert list origin to avoid further GAL interruptions.
3481 		 * A new alert list can be build up by millicode in parallel
3482 		 * for guests not in the yet cut-off alert list. When in the
3483 		 * final loop, store the NULL_GISA_ADDR instead. This will re-
3484 		 * enable GAL interruptions on the host again.
3485 		 */
3486 		origin = xchg(&gib->alert_list_origin,
3487 			      (!final) ? NONE_GISA_ADDR : NULL_GISA_ADDR);
3488 		/*
3489 		 * Loop through the just cut-off alert list and start the
3490 		 * gisa timers to kick idle vcpus to consume the pending
3491 		 * interruptions asap.
3492 		 */
3493 		while (origin & GISA_ADDR_MASK) {
3494 			gisa_phys = origin;
3495 			gisa = phys_to_virt(gisa_phys);
3496 			origin = gisa->next_alert;
3497 			gisa->next_alert = gisa_phys;
3498 			kvm = container_of(gisa, struct sie_page2, gisa)->kvm;
3499 			gi = &kvm->arch.gisa_int;
3500 			if (hrtimer_active(&gi->timer))
3501 				hrtimer_cancel(&gi->timer);
3502 			hrtimer_start(&gi->timer, 0, HRTIMER_MODE_REL);
3503 		}
3504 	} while (!final);
3505 
3506 }
3507 
3508 void kvm_s390_gisa_clear(struct kvm *kvm)
3509 {
3510 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3511 
3512 	if (!gi->origin)
3513 		return;
3514 	gisa_clear_ipm(gi->origin);
3515 	VM_EVENT(kvm, 3, "gisa 0x%p cleared", gi->origin);
3516 }
3517 
3518 void kvm_s390_gisa_init(struct kvm *kvm)
3519 {
3520 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3521 
3522 	if (!css_general_characteristics.aiv)
3523 		return;
3524 	gi->origin = &kvm->arch.sie_page2->gisa;
3525 	gi->alert.mask = 0;
3526 	spin_lock_init(&gi->alert.ref_lock);
3527 	gi->expires = 50 * 1000; /* 50 usec */
3528 	hrtimer_setup(&gi->timer, gisa_vcpu_kicker, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3529 	memset(gi->origin, 0, sizeof(struct kvm_s390_gisa));
3530 	gi->origin->next_alert = (u32)virt_to_phys(gi->origin);
3531 	VM_EVENT(kvm, 3, "gisa 0x%p initialized", gi->origin);
3532 }
3533 
3534 void kvm_s390_gisa_enable(struct kvm *kvm)
3535 {
3536 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3537 	struct kvm_vcpu *vcpu;
3538 	unsigned long i;
3539 	u32 gisa_desc;
3540 
3541 	if (gi->origin)
3542 		return;
3543 	kvm_s390_gisa_init(kvm);
3544 	gisa_desc = kvm_s390_get_gisa_desc(kvm);
3545 	if (!gisa_desc)
3546 		return;
3547 	kvm_for_each_vcpu(i, vcpu, kvm) {
3548 		mutex_lock(&vcpu->mutex);
3549 		vcpu->arch.sie_block->gd = gisa_desc;
3550 		vcpu->arch.sie_block->eca |= ECA_AIV;
3551 		VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3552 			   vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3553 		mutex_unlock(&vcpu->mutex);
3554 	}
3555 }
3556 
3557 void kvm_s390_gisa_destroy(struct kvm *kvm)
3558 {
3559 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3560 	struct kvm_s390_gisa *gisa = gi->origin;
3561 
3562 	if (!gi->origin)
3563 		return;
3564 	WARN(gi->alert.mask != 0x00,
3565 	     "unexpected non zero alert.mask 0x%02x",
3566 	     gi->alert.mask);
3567 	gi->alert.mask = 0x00;
3568 	if (gisa_set_iam(gi->origin, gi->alert.mask))
3569 		process_gib_alert_list();
3570 	hrtimer_cancel(&gi->timer);
3571 	gi->origin = NULL;
3572 	VM_EVENT(kvm, 3, "gisa 0x%p destroyed", gisa);
3573 }
3574 
3575 void kvm_s390_gisa_disable(struct kvm *kvm)
3576 {
3577 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3578 	struct kvm_vcpu *vcpu;
3579 	unsigned long i;
3580 
3581 	if (!gi->origin)
3582 		return;
3583 	kvm_for_each_vcpu(i, vcpu, kvm) {
3584 		mutex_lock(&vcpu->mutex);
3585 		vcpu->arch.sie_block->eca &= ~ECA_AIV;
3586 		vcpu->arch.sie_block->gd = 0U;
3587 		mutex_unlock(&vcpu->mutex);
3588 		VCPU_EVENT(vcpu, 3, "AIV disabled for cpu %03u", vcpu->vcpu_id);
3589 	}
3590 	kvm_s390_gisa_destroy(kvm);
3591 }
3592 
3593 /**
3594  * kvm_s390_gisc_register - register a guest ISC
3595  *
3596  * @kvm:  the kernel vm to work with
3597  * @gisc: the guest interruption sub class to register
3598  *
3599  * The function extends the vm specific alert mask to use.
3600  * The effective IAM mask in the GISA is updated as well
3601  * in case the GISA is not part of the GIB alert list.
3602  * It will be updated latest when the IAM gets restored
3603  * by gisa_get_ipm_or_restore_iam().
3604  *
3605  * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3606  *          has registered with the channel subsystem.
3607  *          -ENODEV in case the vm uses no GISA
3608  *          -ERANGE in case the guest ISC is invalid
3609  */
3610 int kvm_s390_gisc_register(struct kvm *kvm, u32 gisc)
3611 {
3612 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3613 
3614 	if (!gi->origin)
3615 		return -ENODEV;
3616 	if (gisc > MAX_ISC)
3617 		return -ERANGE;
3618 
3619 	spin_lock(&gi->alert.ref_lock);
3620 	gi->alert.ref_count[gisc]++;
3621 	if (gi->alert.ref_count[gisc] == 1) {
3622 		gi->alert.mask |= 0x80 >> gisc;
3623 		gisa_set_iam(gi->origin, gi->alert.mask);
3624 	}
3625 	spin_unlock(&gi->alert.ref_lock);
3626 
3627 	return gib->nisc;
3628 }
3629 EXPORT_SYMBOL_GPL(kvm_s390_gisc_register);
3630 
3631 /**
3632  * kvm_s390_gisc_unregister - unregister a guest ISC
3633  *
3634  * @kvm:  the kernel vm to work with
3635  * @gisc: the guest interruption sub class to register
3636  *
3637  * The function reduces the vm specific alert mask to use.
3638  * The effective IAM mask in the GISA is updated as well
3639  * in case the GISA is not part of the GIB alert list.
3640  * It will be updated latest when the IAM gets restored
3641  * by gisa_get_ipm_or_restore_iam().
3642  *
3643  * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3644  *          has registered with the channel subsystem.
3645  *          -ENODEV in case the vm uses no GISA
3646  *          -ERANGE in case the guest ISC is invalid
3647  *          -EINVAL in case the guest ISC is not registered
3648  */
3649 int kvm_s390_gisc_unregister(struct kvm *kvm, u32 gisc)
3650 {
3651 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3652 	int rc = 0;
3653 
3654 	if (!gi->origin)
3655 		return -ENODEV;
3656 	if (gisc > MAX_ISC)
3657 		return -ERANGE;
3658 
3659 	spin_lock(&gi->alert.ref_lock);
3660 	if (gi->alert.ref_count[gisc] == 0) {
3661 		rc = -EINVAL;
3662 		goto out;
3663 	}
3664 	gi->alert.ref_count[gisc]--;
3665 	if (gi->alert.ref_count[gisc] == 0) {
3666 		gi->alert.mask &= ~(0x80 >> gisc);
3667 		gisa_set_iam(gi->origin, gi->alert.mask);
3668 	}
3669 out:
3670 	spin_unlock(&gi->alert.ref_lock);
3671 
3672 	return rc;
3673 }
3674 EXPORT_SYMBOL_GPL(kvm_s390_gisc_unregister);
3675 
3676 static void aen_host_forward(unsigned long si)
3677 {
3678 	struct kvm_s390_gisa_interrupt *gi;
3679 	struct zpci_gaite *gaite;
3680 	struct kvm *kvm;
3681 
3682 	gaite = aift->gait + si;
3683 	if (gaite->count == 0)
3684 		return;
3685 	if (gaite->aisb != 0)
3686 		set_bit_inv(gaite->aisbo, phys_to_virt(gaite->aisb));
3687 
3688 	kvm = kvm_s390_pci_si_to_kvm(aift, si);
3689 	if (!kvm)
3690 		return;
3691 	gi = &kvm->arch.gisa_int;
3692 
3693 	if (!(gi->origin->g1.simm & AIS_MODE_MASK(gaite->gisc)) ||
3694 	    !(gi->origin->g1.nimm & AIS_MODE_MASK(gaite->gisc))) {
3695 		gisa_set_ipm_gisc(gi->origin, gaite->gisc);
3696 		if (hrtimer_active(&gi->timer))
3697 			hrtimer_cancel(&gi->timer);
3698 		hrtimer_start(&gi->timer, 0, HRTIMER_MODE_REL);
3699 		kvm->stat.aen_forward++;
3700 	}
3701 }
3702 
3703 static void aen_process_gait(u8 isc)
3704 {
3705 	bool found = false, first = true;
3706 	union zpci_sic_iib iib = {{0}};
3707 	unsigned long si, flags;
3708 
3709 	spin_lock_irqsave(&aift->gait_lock, flags);
3710 
3711 	if (!aift->gait) {
3712 		spin_unlock_irqrestore(&aift->gait_lock, flags);
3713 		return;
3714 	}
3715 
3716 	for (si = 0;;) {
3717 		/* Scan adapter summary indicator bit vector */
3718 		si = airq_iv_scan(aift->sbv, si, airq_iv_end(aift->sbv));
3719 		if (si == -1UL) {
3720 			if (first || found) {
3721 				/* Re-enable interrupts. */
3722 				zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, isc,
3723 						  &iib);
3724 				first = found = false;
3725 			} else {
3726 				/* Interrupts on and all bits processed */
3727 				break;
3728 			}
3729 			found = false;
3730 			si = 0;
3731 			/* Scan again after re-enabling interrupts */
3732 			continue;
3733 		}
3734 		found = true;
3735 		aen_host_forward(si);
3736 	}
3737 
3738 	spin_unlock_irqrestore(&aift->gait_lock, flags);
3739 }
3740 
3741 static void gib_alert_irq_handler(struct airq_struct *airq,
3742 				  struct tpi_info *tpi_info)
3743 {
3744 	struct tpi_adapter_info *info = (struct tpi_adapter_info *)tpi_info;
3745 
3746 	inc_irq_stat(IRQIO_GAL);
3747 
3748 	if ((info->forward || info->error) &&
3749 	    IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
3750 		aen_process_gait(info->isc);
3751 		if (info->aism != 0)
3752 			process_gib_alert_list();
3753 	} else {
3754 		process_gib_alert_list();
3755 	}
3756 }
3757 
3758 static struct airq_struct gib_alert_irq = {
3759 	.handler = gib_alert_irq_handler,
3760 };
3761 
3762 void kvm_s390_gib_destroy(void)
3763 {
3764 	if (!gib)
3765 		return;
3766 	if (kvm_s390_pci_interp_allowed() && aift) {
3767 		mutex_lock(&aift->aift_lock);
3768 		kvm_s390_pci_aen_exit();
3769 		mutex_unlock(&aift->aift_lock);
3770 	}
3771 	chsc_sgib(0);
3772 	unregister_adapter_interrupt(&gib_alert_irq);
3773 	kfree(gib);
3774 	gib = NULL;
3775 }
3776 
3777 int __init kvm_s390_gib_init(u8 nisc)
3778 {
3779 	u32 gib_origin;
3780 	int rc = 0;
3781 
3782 	if (!css_general_characteristics.aiv) {
3783 		KVM_EVENT(3, "%s", "gib not initialized, no AIV facility");
3784 		goto out;
3785 	}
3786 
3787 	gib = kzalloc(PAGE_SIZE, GFP_KERNEL_ACCOUNT | GFP_DMA);
3788 	if (!gib) {
3789 		rc = -ENOMEM;
3790 		goto out;
3791 	}
3792 
3793 	gib_alert_irq.isc = nisc;
3794 	if (register_adapter_interrupt(&gib_alert_irq)) {
3795 		pr_err("Registering the GIB alert interruption handler failed\n");
3796 		rc = -EIO;
3797 		goto out_free_gib;
3798 	}
3799 	/* adapter interrupts used for AP (applicable here) don't use the LSI */
3800 	*gib_alert_irq.lsi_ptr = 0xff;
3801 
3802 	gib->nisc = nisc;
3803 	gib_origin = virt_to_phys(gib);
3804 	if (chsc_sgib(gib_origin)) {
3805 		pr_err("Associating the GIB with the AIV facility failed\n");
3806 		kfree(gib);
3807 		gib = NULL;
3808 		rc = -EIO;
3809 		goto out_unreg_gal;
3810 	}
3811 
3812 	if (kvm_s390_pci_interp_allowed()) {
3813 		if (kvm_s390_pci_aen_init(nisc)) {
3814 			pr_err("Initializing AEN for PCI failed\n");
3815 			rc = -EIO;
3816 			goto out_unreg_gal;
3817 		}
3818 	}
3819 
3820 	KVM_EVENT(3, "gib 0x%p (nisc=%d) initialized", gib, gib->nisc);
3821 	goto out;
3822 
3823 out_unreg_gal:
3824 	unregister_adapter_interrupt(&gib_alert_irq);
3825 out_free_gib:
3826 	kfree(gib);
3827 	gib = NULL;
3828 out:
3829 	return rc;
3830 }
3831 
3832 /*
3833  * kvm_arch_set_irq_inatomic: fast-path for irqfd injection
3834  */
3835 int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
3836 			      struct kvm *kvm, int irq_source_id, int level,
3837 			      bool line_status)
3838 {
3839 	int ret, setbit;
3840 	struct s390_io_adapter *adapter;
3841 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
3842 	struct kvm_s390_interrupt_info *inti;
3843 	struct kvm_s390_interrupt s390int = {
3844 			.type = KVM_S390_INT_IO(1, 0, 0, 0),
3845 			.parm = 0,
3846 	};
3847 
3848 	kvm->stat.io_390_inatomic++;
3849 
3850 	/* We're only interested in the 0->1 transition. */
3851 	if (!level)
3852 		return 0;
3853 	if (e->type != KVM_IRQ_ROUTING_S390_ADAPTER)
3854 		return -EWOULDBLOCK;
3855 
3856 	adapter = get_io_adapter(kvm, e->adapter.adapter_id);
3857 	if (!adapter)
3858 		return -EWOULDBLOCK;
3859 
3860 	s390int.parm64 = isc_to_int_word(adapter->isc);
3861 	setbit = 1;
3862 	ret = adapter_indicators_set_fast(kvm, adapter, &e->adapter, setbit);
3863 	if (ret < 0)
3864 		return -EWOULDBLOCK;
3865 	if (!ret || adapter->masked) {
3866 		kvm->stat.io_390_inatomic_no_inject++;
3867 		return 0;
3868 	}
3869 
3870 	inti = kzalloc_obj(*inti, GFP_ATOMIC);
3871 	if (!inti) {
3872 		setbit = 0;
3873 		adapter_indicators_set_fast(kvm, adapter, &e->adapter, setbit);
3874 		return -EWOULDBLOCK;
3875 	}
3876 
3877 	if (!test_kvm_facility(kvm, 72) || !adapter->suppressible) {
3878 		ret = kvm_s390_inject_vm(kvm, &s390int, inti);
3879 		if (ret == 0) {
3880 			return ret;
3881 		} else {
3882 			setbit = 0;
3883 			adapter_indicators_set_fast(kvm, adapter, &e->adapter, setbit);
3884 			kfree(inti);
3885 			return -EWOULDBLOCK;
3886 		}
3887 	}
3888 
3889 	spin_lock(&fi->ais_lock);
3890 	if (fi->nimm & AIS_MODE_MASK(adapter->isc)) {
3891 		trace_kvm_s390_airq_suppressed(adapter->id, adapter->isc);
3892 		spin_unlock(&fi->ais_lock);
3893 		kfree(inti);
3894 		kvm->stat.io_390_inatomic_no_inject++;
3895 		return 0;
3896 	}
3897 
3898 	ret = kvm_s390_inject_vm(kvm, &s390int, inti);
3899 	if (!ret && (fi->simm & AIS_MODE_MASK(adapter->isc))) {
3900 		fi->nimm |= AIS_MODE_MASK(adapter->isc);
3901 		trace_kvm_s390_modify_ais_mode(adapter->isc,
3902 					       KVM_S390_AIS_MODE_SINGLE, 2);
3903 	} else if (ret) {
3904 		spin_unlock(&fi->ais_lock);
3905 		setbit = 0;
3906 		adapter_indicators_set_fast(kvm, adapter, &e->adapter, setbit);
3907 		kfree(inti);
3908 		return -EWOULDBLOCK;
3909 	}
3910 
3911 	spin_unlock(&fi->ais_lock);
3912 	return 0;
3913 }
3914