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