xref: /openbmc/linux/arch/s390/kvm/interrupt.c (revision 3fc41476)
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 	/*
1228 	 * We cannot move this into the if, as the CPU might be already
1229 	 * in kvm_vcpu_block without having the waitqueue set (polling)
1230 	 */
1231 	vcpu->valid_wakeup = true;
1232 	/*
1233 	 * This is mostly to document, that the read in swait_active could
1234 	 * be moved before other stores, leading to subtle races.
1235 	 * All current users do not store or use an atomic like update
1236 	 */
1237 	smp_mb__after_atomic();
1238 	if (swait_active(&vcpu->wq)) {
1239 		/*
1240 		 * The vcpu gave up the cpu voluntarily, mark it as a good
1241 		 * yield-candidate.
1242 		 */
1243 		vcpu->preempted = true;
1244 		swake_up_one(&vcpu->wq);
1245 		vcpu->stat.halt_wakeup++;
1246 	}
1247 	/*
1248 	 * The VCPU might not be sleeping but is executing the VSIE. Let's
1249 	 * kick it, so it leaves the SIE to process the request.
1250 	 */
1251 	kvm_s390_vsie_kick(vcpu);
1252 }
1253 
1254 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
1255 {
1256 	struct kvm_vcpu *vcpu;
1257 	u64 sltime;
1258 
1259 	vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
1260 	sltime = __calculate_sltime(vcpu);
1261 
1262 	/*
1263 	 * If the monotonic clock runs faster than the tod clock we might be
1264 	 * woken up too early and have to go back to sleep to avoid deadlocks.
1265 	 */
1266 	if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime)))
1267 		return HRTIMER_RESTART;
1268 	kvm_s390_vcpu_wakeup(vcpu);
1269 	return HRTIMER_NORESTART;
1270 }
1271 
1272 void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
1273 {
1274 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1275 
1276 	spin_lock(&li->lock);
1277 	li->pending_irqs = 0;
1278 	bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
1279 	memset(&li->irq, 0, sizeof(li->irq));
1280 	spin_unlock(&li->lock);
1281 
1282 	sca_clear_ext_call(vcpu);
1283 }
1284 
1285 int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
1286 {
1287 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1288 	int rc = 0;
1289 	unsigned long irq_type;
1290 	unsigned long irqs;
1291 
1292 	__reset_intercept_indicators(vcpu);
1293 
1294 	/* pending ckc conditions might have been invalidated */
1295 	clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1296 	if (ckc_irq_pending(vcpu))
1297 		set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1298 
1299 	/* pending cpu timer conditions might have been invalidated */
1300 	clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1301 	if (cpu_timer_irq_pending(vcpu))
1302 		set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1303 
1304 	while ((irqs = deliverable_irqs(vcpu)) && !rc) {
1305 		/* bits are in the reverse order of interrupt priority */
1306 		irq_type = find_last_bit(&irqs, IRQ_PEND_COUNT);
1307 		switch (irq_type) {
1308 		case IRQ_PEND_IO_ISC_0:
1309 		case IRQ_PEND_IO_ISC_1:
1310 		case IRQ_PEND_IO_ISC_2:
1311 		case IRQ_PEND_IO_ISC_3:
1312 		case IRQ_PEND_IO_ISC_4:
1313 		case IRQ_PEND_IO_ISC_5:
1314 		case IRQ_PEND_IO_ISC_6:
1315 		case IRQ_PEND_IO_ISC_7:
1316 			rc = __deliver_io(vcpu, irq_type);
1317 			break;
1318 		case IRQ_PEND_MCHK_EX:
1319 		case IRQ_PEND_MCHK_REP:
1320 			rc = __deliver_machine_check(vcpu);
1321 			break;
1322 		case IRQ_PEND_PROG:
1323 			rc = __deliver_prog(vcpu);
1324 			break;
1325 		case IRQ_PEND_EXT_EMERGENCY:
1326 			rc = __deliver_emergency_signal(vcpu);
1327 			break;
1328 		case IRQ_PEND_EXT_EXTERNAL:
1329 			rc = __deliver_external_call(vcpu);
1330 			break;
1331 		case IRQ_PEND_EXT_CLOCK_COMP:
1332 			rc = __deliver_ckc(vcpu);
1333 			break;
1334 		case IRQ_PEND_EXT_CPU_TIMER:
1335 			rc = __deliver_cpu_timer(vcpu);
1336 			break;
1337 		case IRQ_PEND_RESTART:
1338 			rc = __deliver_restart(vcpu);
1339 			break;
1340 		case IRQ_PEND_SET_PREFIX:
1341 			rc = __deliver_set_prefix(vcpu);
1342 			break;
1343 		case IRQ_PEND_PFAULT_INIT:
1344 			rc = __deliver_pfault_init(vcpu);
1345 			break;
1346 		case IRQ_PEND_EXT_SERVICE:
1347 			rc = __deliver_service(vcpu);
1348 			break;
1349 		case IRQ_PEND_PFAULT_DONE:
1350 			rc = __deliver_pfault_done(vcpu);
1351 			break;
1352 		case IRQ_PEND_VIRTIO:
1353 			rc = __deliver_virtio(vcpu);
1354 			break;
1355 		default:
1356 			WARN_ONCE(1, "Unknown pending irq type %ld", irq_type);
1357 			clear_bit(irq_type, &li->pending_irqs);
1358 		}
1359 	}
1360 
1361 	set_intercept_indicators(vcpu);
1362 
1363 	return rc;
1364 }
1365 
1366 static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1367 {
1368 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1369 
1370 	vcpu->stat.inject_program++;
1371 	VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1372 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1373 				   irq->u.pgm.code, 0);
1374 
1375 	if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1376 		/* auto detection if no valid ILC was given */
1377 		irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1378 		irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1379 		irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1380 	}
1381 
1382 	if (irq->u.pgm.code == PGM_PER) {
1383 		li->irq.pgm.code |= PGM_PER;
1384 		li->irq.pgm.flags = irq->u.pgm.flags;
1385 		/* only modify PER related information */
1386 		li->irq.pgm.per_address = irq->u.pgm.per_address;
1387 		li->irq.pgm.per_code = irq->u.pgm.per_code;
1388 		li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1389 		li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1390 	} else if (!(irq->u.pgm.code & PGM_PER)) {
1391 		li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1392 				   irq->u.pgm.code;
1393 		li->irq.pgm.flags = irq->u.pgm.flags;
1394 		/* only modify non-PER information */
1395 		li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1396 		li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1397 		li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1398 		li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1399 		li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1400 		li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1401 	} else {
1402 		li->irq.pgm = irq->u.pgm;
1403 	}
1404 	set_bit(IRQ_PEND_PROG, &li->pending_irqs);
1405 	return 0;
1406 }
1407 
1408 static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1409 {
1410 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1411 
1412 	vcpu->stat.inject_pfault_init++;
1413 	VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1414 		   irq->u.ext.ext_params2);
1415 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1416 				   irq->u.ext.ext_params,
1417 				   irq->u.ext.ext_params2);
1418 
1419 	li->irq.ext = irq->u.ext;
1420 	set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
1421 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1422 	return 0;
1423 }
1424 
1425 static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1426 {
1427 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1428 	struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
1429 	uint16_t src_id = irq->u.extcall.code;
1430 
1431 	vcpu->stat.inject_external_call++;
1432 	VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
1433 		   src_id);
1434 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
1435 				   src_id, 0);
1436 
1437 	/* sending vcpu invalid */
1438 	if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
1439 		return -EINVAL;
1440 
1441 	if (sclp.has_sigpif)
1442 		return sca_inject_ext_call(vcpu, src_id);
1443 
1444 	if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
1445 		return -EBUSY;
1446 	*extcall = irq->u.extcall;
1447 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1448 	return 0;
1449 }
1450 
1451 static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1452 {
1453 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1454 	struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
1455 
1456 	vcpu->stat.inject_set_prefix++;
1457 	VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
1458 		   irq->u.prefix.address);
1459 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
1460 				   irq->u.prefix.address, 0);
1461 
1462 	if (!is_vcpu_stopped(vcpu))
1463 		return -EBUSY;
1464 
1465 	*prefix = irq->u.prefix;
1466 	set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
1467 	return 0;
1468 }
1469 
1470 #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
1471 static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1472 {
1473 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1474 	struct kvm_s390_stop_info *stop = &li->irq.stop;
1475 	int rc = 0;
1476 
1477 	vcpu->stat.inject_stop_signal++;
1478 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
1479 
1480 	if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1481 		return -EINVAL;
1482 
1483 	if (is_vcpu_stopped(vcpu)) {
1484 		if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1485 			rc = kvm_s390_store_status_unloaded(vcpu,
1486 						KVM_S390_STORE_STATUS_NOADDR);
1487 		return rc;
1488 	}
1489 
1490 	if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1491 		return -EBUSY;
1492 	stop->flags = irq->u.stop.flags;
1493 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
1494 	return 0;
1495 }
1496 
1497 static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
1498 				 struct kvm_s390_irq *irq)
1499 {
1500 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1501 
1502 	vcpu->stat.inject_restart++;
1503 	VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
1504 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
1505 
1506 	set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
1507 	return 0;
1508 }
1509 
1510 static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
1511 				   struct kvm_s390_irq *irq)
1512 {
1513 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1514 
1515 	vcpu->stat.inject_emergency_signal++;
1516 	VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
1517 		   irq->u.emerg.code);
1518 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
1519 				   irq->u.emerg.code, 0);
1520 
1521 	/* sending vcpu invalid */
1522 	if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1523 		return -EINVAL;
1524 
1525 	set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
1526 	set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
1527 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1528 	return 0;
1529 }
1530 
1531 static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1532 {
1533 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1534 	struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
1535 
1536 	vcpu->stat.inject_mchk++;
1537 	VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
1538 		   irq->u.mchk.mcic);
1539 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
1540 				   irq->u.mchk.mcic);
1541 
1542 	/*
1543 	 * Because repressible machine checks can be indicated along with
1544 	 * exigent machine checks (PoP, Chapter 11, Interruption action)
1545 	 * we need to combine cr14, mcic and external damage code.
1546 	 * Failing storage address and the logout area should not be or'ed
1547 	 * together, we just indicate the last occurrence of the corresponding
1548 	 * machine check
1549 	 */
1550 	mchk->cr14 |= irq->u.mchk.cr14;
1551 	mchk->mcic |= irq->u.mchk.mcic;
1552 	mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1553 	mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1554 	memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1555 	       sizeof(mchk->fixed_logout));
1556 	if (mchk->mcic & MCHK_EX_MASK)
1557 		set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1558 	else if (mchk->mcic & MCHK_REP_MASK)
1559 		set_bit(IRQ_PEND_MCHK_REP,  &li->pending_irqs);
1560 	return 0;
1561 }
1562 
1563 static int __inject_ckc(struct kvm_vcpu *vcpu)
1564 {
1565 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1566 
1567 	vcpu->stat.inject_ckc++;
1568 	VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
1569 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
1570 				   0, 0);
1571 
1572 	set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1573 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1574 	return 0;
1575 }
1576 
1577 static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
1578 {
1579 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1580 
1581 	vcpu->stat.inject_cputm++;
1582 	VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
1583 	trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
1584 				   0, 0);
1585 
1586 	set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1587 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1588 	return 0;
1589 }
1590 
1591 static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1592 						  int isc, u32 schid)
1593 {
1594 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1595 	struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1596 	struct kvm_s390_interrupt_info *iter;
1597 	u16 id = (schid & 0xffff0000U) >> 16;
1598 	u16 nr = schid & 0x0000ffffU;
1599 
1600 	spin_lock(&fi->lock);
1601 	list_for_each_entry(iter, isc_list, list) {
1602 		if (schid && (id != iter->io.subchannel_id ||
1603 			      nr != iter->io.subchannel_nr))
1604 			continue;
1605 		/* found an appropriate entry */
1606 		list_del_init(&iter->list);
1607 		fi->counters[FIRQ_CNTR_IO] -= 1;
1608 		if (list_empty(isc_list))
1609 			clear_bit(isc_to_irq_type(isc), &fi->pending_irqs);
1610 		spin_unlock(&fi->lock);
1611 		return iter;
1612 	}
1613 	spin_unlock(&fi->lock);
1614 	return NULL;
1615 }
1616 
1617 static struct kvm_s390_interrupt_info *get_top_io_int(struct kvm *kvm,
1618 						      u64 isc_mask, u32 schid)
1619 {
1620 	struct kvm_s390_interrupt_info *inti = NULL;
1621 	int isc;
1622 
1623 	for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1624 		if (isc_mask & isc_to_isc_bits(isc))
1625 			inti = get_io_int(kvm, isc, schid);
1626 	}
1627 	return inti;
1628 }
1629 
1630 static int get_top_gisa_isc(struct kvm *kvm, u64 isc_mask, u32 schid)
1631 {
1632 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
1633 	unsigned long active_mask;
1634 	int isc;
1635 
1636 	if (schid)
1637 		goto out;
1638 	if (!gi->origin)
1639 		goto out;
1640 
1641 	active_mask = (isc_mask & gisa_get_ipm(gi->origin) << 24) << 32;
1642 	while (active_mask) {
1643 		isc = __fls(active_mask) ^ (BITS_PER_LONG - 1);
1644 		if (gisa_tac_ipm_gisc(gi->origin, isc))
1645 			return isc;
1646 		clear_bit_inv(isc, &active_mask);
1647 	}
1648 out:
1649 	return -EINVAL;
1650 }
1651 
1652 /*
1653  * Dequeue and return an I/O interrupt matching any of the interruption
1654  * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1655  * Take into account the interrupts pending in the interrupt list and in GISA.
1656  *
1657  * Note that for a guest that does not enable I/O interrupts
1658  * but relies on TPI, a flood of classic interrupts may starve
1659  * out adapter interrupts on the same isc. Linux does not do
1660  * that, and it is possible to work around the issue by configuring
1661  * different iscs for classic and adapter interrupts in the guest,
1662  * but we may want to revisit this in the future.
1663  */
1664 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
1665 						    u64 isc_mask, u32 schid)
1666 {
1667 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
1668 	struct kvm_s390_interrupt_info *inti, *tmp_inti;
1669 	int isc;
1670 
1671 	inti = get_top_io_int(kvm, isc_mask, schid);
1672 
1673 	isc = get_top_gisa_isc(kvm, isc_mask, schid);
1674 	if (isc < 0)
1675 		/* no AI in GISA */
1676 		goto out;
1677 
1678 	if (!inti)
1679 		/* AI in GISA but no classical IO int */
1680 		goto gisa_out;
1681 
1682 	/* both types of interrupts present */
1683 	if (int_word_to_isc(inti->io.io_int_word) <= isc) {
1684 		/* classical IO int with higher priority */
1685 		gisa_set_ipm_gisc(gi->origin, isc);
1686 		goto out;
1687 	}
1688 gisa_out:
1689 	tmp_inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1690 	if (tmp_inti) {
1691 		tmp_inti->type = KVM_S390_INT_IO(1, 0, 0, 0);
1692 		tmp_inti->io.io_int_word = isc_to_int_word(isc);
1693 		if (inti)
1694 			kvm_s390_reinject_io_int(kvm, inti);
1695 		inti = tmp_inti;
1696 	} else
1697 		gisa_set_ipm_gisc(gi->origin, isc);
1698 out:
1699 	return inti;
1700 }
1701 
1702 #define SCCB_MASK 0xFFFFFFF8
1703 #define SCCB_EVENT_PENDING 0x3
1704 
1705 static int __inject_service(struct kvm *kvm,
1706 			     struct kvm_s390_interrupt_info *inti)
1707 {
1708 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1709 
1710 	kvm->stat.inject_service_signal++;
1711 	spin_lock(&fi->lock);
1712 	fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1713 	/*
1714 	 * Early versions of the QEMU s390 bios will inject several
1715 	 * service interrupts after another without handling a
1716 	 * condition code indicating busy.
1717 	 * We will silently ignore those superfluous sccb values.
1718 	 * A future version of QEMU will take care of serialization
1719 	 * of servc requests
1720 	 */
1721 	if (fi->srv_signal.ext_params & SCCB_MASK)
1722 		goto out;
1723 	fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1724 	set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1725 out:
1726 	spin_unlock(&fi->lock);
1727 	kfree(inti);
1728 	return 0;
1729 }
1730 
1731 static int __inject_virtio(struct kvm *kvm,
1732 			    struct kvm_s390_interrupt_info *inti)
1733 {
1734 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1735 
1736 	kvm->stat.inject_virtio++;
1737 	spin_lock(&fi->lock);
1738 	if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1739 		spin_unlock(&fi->lock);
1740 		return -EBUSY;
1741 	}
1742 	fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1743 	list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1744 	set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1745 	spin_unlock(&fi->lock);
1746 	return 0;
1747 }
1748 
1749 static int __inject_pfault_done(struct kvm *kvm,
1750 				 struct kvm_s390_interrupt_info *inti)
1751 {
1752 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1753 
1754 	kvm->stat.inject_pfault_done++;
1755 	spin_lock(&fi->lock);
1756 	if (fi->counters[FIRQ_CNTR_PFAULT] >=
1757 		(ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1758 		spin_unlock(&fi->lock);
1759 		return -EBUSY;
1760 	}
1761 	fi->counters[FIRQ_CNTR_PFAULT] += 1;
1762 	list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1763 	set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1764 	spin_unlock(&fi->lock);
1765 	return 0;
1766 }
1767 
1768 #define CR_PENDING_SUBCLASS 28
1769 static int __inject_float_mchk(struct kvm *kvm,
1770 				struct kvm_s390_interrupt_info *inti)
1771 {
1772 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1773 
1774 	kvm->stat.inject_float_mchk++;
1775 	spin_lock(&fi->lock);
1776 	fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1777 	fi->mchk.mcic |= inti->mchk.mcic;
1778 	set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1779 	spin_unlock(&fi->lock);
1780 	kfree(inti);
1781 	return 0;
1782 }
1783 
1784 static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1785 {
1786 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
1787 	struct kvm_s390_float_interrupt *fi;
1788 	struct list_head *list;
1789 	int isc;
1790 
1791 	kvm->stat.inject_io++;
1792 	isc = int_word_to_isc(inti->io.io_int_word);
1793 
1794 	if (gi->origin && inti->type & KVM_S390_INT_IO_AI_MASK) {
1795 		VM_EVENT(kvm, 4, "%s isc %1u", "inject: I/O (AI/gisa)", isc);
1796 		gisa_set_ipm_gisc(gi->origin, isc);
1797 		kfree(inti);
1798 		return 0;
1799 	}
1800 
1801 	fi = &kvm->arch.float_int;
1802 	spin_lock(&fi->lock);
1803 	if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1804 		spin_unlock(&fi->lock);
1805 		return -EBUSY;
1806 	}
1807 	fi->counters[FIRQ_CNTR_IO] += 1;
1808 
1809 	if (inti->type & KVM_S390_INT_IO_AI_MASK)
1810 		VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1811 	else
1812 		VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1813 			inti->io.subchannel_id >> 8,
1814 			inti->io.subchannel_id >> 1 & 0x3,
1815 			inti->io.subchannel_nr);
1816 	list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1817 	list_add_tail(&inti->list, list);
1818 	set_bit(isc_to_irq_type(isc), &fi->pending_irqs);
1819 	spin_unlock(&fi->lock);
1820 	return 0;
1821 }
1822 
1823 /*
1824  * Find a destination VCPU for a floating irq and kick it.
1825  */
1826 static void __floating_irq_kick(struct kvm *kvm, u64 type)
1827 {
1828 	struct kvm_vcpu *dst_vcpu;
1829 	int sigcpu, online_vcpus, nr_tries = 0;
1830 
1831 	online_vcpus = atomic_read(&kvm->online_vcpus);
1832 	if (!online_vcpus)
1833 		return;
1834 
1835 	/* find idle VCPUs first, then round robin */
1836 	sigcpu = find_first_bit(kvm->arch.idle_mask, online_vcpus);
1837 	if (sigcpu == online_vcpus) {
1838 		do {
1839 			sigcpu = kvm->arch.float_int.next_rr_cpu++;
1840 			kvm->arch.float_int.next_rr_cpu %= online_vcpus;
1841 			/* avoid endless loops if all vcpus are stopped */
1842 			if (nr_tries++ >= online_vcpus)
1843 				return;
1844 		} while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1845 	}
1846 	dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1847 
1848 	/* make the VCPU drop out of the SIE, or wake it up if sleeping */
1849 	switch (type) {
1850 	case KVM_S390_MCHK:
1851 		kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_STOP_INT);
1852 		break;
1853 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1854 		if (!(type & KVM_S390_INT_IO_AI_MASK &&
1855 		      kvm->arch.gisa_int.origin))
1856 			kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_IO_INT);
1857 		break;
1858 	default:
1859 		kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_EXT_INT);
1860 		break;
1861 	}
1862 	kvm_s390_vcpu_wakeup(dst_vcpu);
1863 }
1864 
1865 static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1866 {
1867 	u64 type = READ_ONCE(inti->type);
1868 	int rc;
1869 
1870 	switch (type) {
1871 	case KVM_S390_MCHK:
1872 		rc = __inject_float_mchk(kvm, inti);
1873 		break;
1874 	case KVM_S390_INT_VIRTIO:
1875 		rc = __inject_virtio(kvm, inti);
1876 		break;
1877 	case KVM_S390_INT_SERVICE:
1878 		rc = __inject_service(kvm, inti);
1879 		break;
1880 	case KVM_S390_INT_PFAULT_DONE:
1881 		rc = __inject_pfault_done(kvm, inti);
1882 		break;
1883 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1884 		rc = __inject_io(kvm, inti);
1885 		break;
1886 	default:
1887 		rc = -EINVAL;
1888 	}
1889 	if (rc)
1890 		return rc;
1891 
1892 	__floating_irq_kick(kvm, type);
1893 	return 0;
1894 }
1895 
1896 int kvm_s390_inject_vm(struct kvm *kvm,
1897 		       struct kvm_s390_interrupt *s390int)
1898 {
1899 	struct kvm_s390_interrupt_info *inti;
1900 	int rc;
1901 
1902 	inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1903 	if (!inti)
1904 		return -ENOMEM;
1905 
1906 	inti->type = s390int->type;
1907 	switch (inti->type) {
1908 	case KVM_S390_INT_VIRTIO:
1909 		VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1910 			 s390int->parm, s390int->parm64);
1911 		inti->ext.ext_params = s390int->parm;
1912 		inti->ext.ext_params2 = s390int->parm64;
1913 		break;
1914 	case KVM_S390_INT_SERVICE:
1915 		VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
1916 		inti->ext.ext_params = s390int->parm;
1917 		break;
1918 	case KVM_S390_INT_PFAULT_DONE:
1919 		inti->ext.ext_params2 = s390int->parm64;
1920 		break;
1921 	case KVM_S390_MCHK:
1922 		VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
1923 			 s390int->parm64);
1924 		inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1925 		inti->mchk.mcic = s390int->parm64;
1926 		break;
1927 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1928 		inti->io.subchannel_id = s390int->parm >> 16;
1929 		inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1930 		inti->io.io_int_parm = s390int->parm64 >> 32;
1931 		inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1932 		break;
1933 	default:
1934 		kfree(inti);
1935 		return -EINVAL;
1936 	}
1937 	trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1938 				 2);
1939 
1940 	rc = __inject_vm(kvm, inti);
1941 	if (rc)
1942 		kfree(inti);
1943 	return rc;
1944 }
1945 
1946 int kvm_s390_reinject_io_int(struct kvm *kvm,
1947 			      struct kvm_s390_interrupt_info *inti)
1948 {
1949 	return __inject_vm(kvm, inti);
1950 }
1951 
1952 int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1953 		       struct kvm_s390_irq *irq)
1954 {
1955 	irq->type = s390int->type;
1956 	switch (irq->type) {
1957 	case KVM_S390_PROGRAM_INT:
1958 		if (s390int->parm & 0xffff0000)
1959 			return -EINVAL;
1960 		irq->u.pgm.code = s390int->parm;
1961 		break;
1962 	case KVM_S390_SIGP_SET_PREFIX:
1963 		irq->u.prefix.address = s390int->parm;
1964 		break;
1965 	case KVM_S390_SIGP_STOP:
1966 		irq->u.stop.flags = s390int->parm;
1967 		break;
1968 	case KVM_S390_INT_EXTERNAL_CALL:
1969 		if (s390int->parm & 0xffff0000)
1970 			return -EINVAL;
1971 		irq->u.extcall.code = s390int->parm;
1972 		break;
1973 	case KVM_S390_INT_EMERGENCY:
1974 		if (s390int->parm & 0xffff0000)
1975 			return -EINVAL;
1976 		irq->u.emerg.code = s390int->parm;
1977 		break;
1978 	case KVM_S390_MCHK:
1979 		irq->u.mchk.mcic = s390int->parm64;
1980 		break;
1981 	}
1982 	return 0;
1983 }
1984 
1985 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1986 {
1987 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1988 
1989 	return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1990 }
1991 
1992 void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1993 {
1994 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1995 
1996 	spin_lock(&li->lock);
1997 	li->irq.stop.flags = 0;
1998 	clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1999 	spin_unlock(&li->lock);
2000 }
2001 
2002 static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
2003 {
2004 	int rc;
2005 
2006 	switch (irq->type) {
2007 	case KVM_S390_PROGRAM_INT:
2008 		rc = __inject_prog(vcpu, irq);
2009 		break;
2010 	case KVM_S390_SIGP_SET_PREFIX:
2011 		rc = __inject_set_prefix(vcpu, irq);
2012 		break;
2013 	case KVM_S390_SIGP_STOP:
2014 		rc = __inject_sigp_stop(vcpu, irq);
2015 		break;
2016 	case KVM_S390_RESTART:
2017 		rc = __inject_sigp_restart(vcpu, irq);
2018 		break;
2019 	case KVM_S390_INT_CLOCK_COMP:
2020 		rc = __inject_ckc(vcpu);
2021 		break;
2022 	case KVM_S390_INT_CPU_TIMER:
2023 		rc = __inject_cpu_timer(vcpu);
2024 		break;
2025 	case KVM_S390_INT_EXTERNAL_CALL:
2026 		rc = __inject_extcall(vcpu, irq);
2027 		break;
2028 	case KVM_S390_INT_EMERGENCY:
2029 		rc = __inject_sigp_emergency(vcpu, irq);
2030 		break;
2031 	case KVM_S390_MCHK:
2032 		rc = __inject_mchk(vcpu, irq);
2033 		break;
2034 	case KVM_S390_INT_PFAULT_INIT:
2035 		rc = __inject_pfault_init(vcpu, irq);
2036 		break;
2037 	case KVM_S390_INT_VIRTIO:
2038 	case KVM_S390_INT_SERVICE:
2039 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2040 	default:
2041 		rc = -EINVAL;
2042 	}
2043 
2044 	return rc;
2045 }
2046 
2047 int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
2048 {
2049 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2050 	int rc;
2051 
2052 	spin_lock(&li->lock);
2053 	rc = do_inject_vcpu(vcpu, irq);
2054 	spin_unlock(&li->lock);
2055 	if (!rc)
2056 		kvm_s390_vcpu_wakeup(vcpu);
2057 	return rc;
2058 }
2059 
2060 static inline void clear_irq_list(struct list_head *_list)
2061 {
2062 	struct kvm_s390_interrupt_info *inti, *n;
2063 
2064 	list_for_each_entry_safe(inti, n, _list, list) {
2065 		list_del(&inti->list);
2066 		kfree(inti);
2067 	}
2068 }
2069 
2070 static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
2071 		       struct kvm_s390_irq *irq)
2072 {
2073 	irq->type = inti->type;
2074 	switch (inti->type) {
2075 	case KVM_S390_INT_PFAULT_INIT:
2076 	case KVM_S390_INT_PFAULT_DONE:
2077 	case KVM_S390_INT_VIRTIO:
2078 		irq->u.ext = inti->ext;
2079 		break;
2080 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2081 		irq->u.io = inti->io;
2082 		break;
2083 	}
2084 }
2085 
2086 void kvm_s390_clear_float_irqs(struct kvm *kvm)
2087 {
2088 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2089 	int i;
2090 
2091 	spin_lock(&fi->lock);
2092 	fi->pending_irqs = 0;
2093 	memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
2094 	memset(&fi->mchk, 0, sizeof(fi->mchk));
2095 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
2096 		clear_irq_list(&fi->lists[i]);
2097 	for (i = 0; i < FIRQ_MAX_COUNT; i++)
2098 		fi->counters[i] = 0;
2099 	spin_unlock(&fi->lock);
2100 	kvm_s390_gisa_clear(kvm);
2101 };
2102 
2103 static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
2104 {
2105 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
2106 	struct kvm_s390_interrupt_info *inti;
2107 	struct kvm_s390_float_interrupt *fi;
2108 	struct kvm_s390_irq *buf;
2109 	struct kvm_s390_irq *irq;
2110 	int max_irqs;
2111 	int ret = 0;
2112 	int n = 0;
2113 	int i;
2114 
2115 	if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
2116 		return -EINVAL;
2117 
2118 	/*
2119 	 * We are already using -ENOMEM to signal
2120 	 * userspace it may retry with a bigger buffer,
2121 	 * so we need to use something else for this case
2122 	 */
2123 	buf = vzalloc(len);
2124 	if (!buf)
2125 		return -ENOBUFS;
2126 
2127 	max_irqs = len / sizeof(struct kvm_s390_irq);
2128 
2129 	if (gi->origin && gisa_get_ipm(gi->origin)) {
2130 		for (i = 0; i <= MAX_ISC; i++) {
2131 			if (n == max_irqs) {
2132 				/* signal userspace to try again */
2133 				ret = -ENOMEM;
2134 				goto out_nolock;
2135 			}
2136 			if (gisa_tac_ipm_gisc(gi->origin, i)) {
2137 				irq = (struct kvm_s390_irq *) &buf[n];
2138 				irq->type = KVM_S390_INT_IO(1, 0, 0, 0);
2139 				irq->u.io.io_int_word = isc_to_int_word(i);
2140 				n++;
2141 			}
2142 		}
2143 	}
2144 	fi = &kvm->arch.float_int;
2145 	spin_lock(&fi->lock);
2146 	for (i = 0; i < FIRQ_LIST_COUNT; i++) {
2147 		list_for_each_entry(inti, &fi->lists[i], list) {
2148 			if (n == max_irqs) {
2149 				/* signal userspace to try again */
2150 				ret = -ENOMEM;
2151 				goto out;
2152 			}
2153 			inti_to_irq(inti, &buf[n]);
2154 			n++;
2155 		}
2156 	}
2157 	if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
2158 		if (n == max_irqs) {
2159 			/* signal userspace to try again */
2160 			ret = -ENOMEM;
2161 			goto out;
2162 		}
2163 		irq = (struct kvm_s390_irq *) &buf[n];
2164 		irq->type = KVM_S390_INT_SERVICE;
2165 		irq->u.ext = fi->srv_signal;
2166 		n++;
2167 	}
2168 	if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
2169 		if (n == max_irqs) {
2170 				/* signal userspace to try again */
2171 				ret = -ENOMEM;
2172 				goto out;
2173 		}
2174 		irq = (struct kvm_s390_irq *) &buf[n];
2175 		irq->type = KVM_S390_MCHK;
2176 		irq->u.mchk = fi->mchk;
2177 		n++;
2178 }
2179 
2180 out:
2181 	spin_unlock(&fi->lock);
2182 out_nolock:
2183 	if (!ret && n > 0) {
2184 		if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
2185 			ret = -EFAULT;
2186 	}
2187 	vfree(buf);
2188 
2189 	return ret < 0 ? ret : n;
2190 }
2191 
2192 static int flic_ais_mode_get_all(struct kvm *kvm, struct kvm_device_attr *attr)
2193 {
2194 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2195 	struct kvm_s390_ais_all ais;
2196 
2197 	if (attr->attr < sizeof(ais))
2198 		return -EINVAL;
2199 
2200 	if (!test_kvm_facility(kvm, 72))
2201 		return -ENOTSUPP;
2202 
2203 	mutex_lock(&fi->ais_lock);
2204 	ais.simm = fi->simm;
2205 	ais.nimm = fi->nimm;
2206 	mutex_unlock(&fi->ais_lock);
2207 
2208 	if (copy_to_user((void __user *)attr->addr, &ais, sizeof(ais)))
2209 		return -EFAULT;
2210 
2211 	return 0;
2212 }
2213 
2214 static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2215 {
2216 	int r;
2217 
2218 	switch (attr->group) {
2219 	case KVM_DEV_FLIC_GET_ALL_IRQS:
2220 		r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
2221 					  attr->attr);
2222 		break;
2223 	case KVM_DEV_FLIC_AISM_ALL:
2224 		r = flic_ais_mode_get_all(dev->kvm, attr);
2225 		break;
2226 	default:
2227 		r = -EINVAL;
2228 	}
2229 
2230 	return r;
2231 }
2232 
2233 static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
2234 				     u64 addr)
2235 {
2236 	struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
2237 	void *target = NULL;
2238 	void __user *source;
2239 	u64 size;
2240 
2241 	if (get_user(inti->type, (u64 __user *)addr))
2242 		return -EFAULT;
2243 
2244 	switch (inti->type) {
2245 	case KVM_S390_INT_PFAULT_INIT:
2246 	case KVM_S390_INT_PFAULT_DONE:
2247 	case KVM_S390_INT_VIRTIO:
2248 	case KVM_S390_INT_SERVICE:
2249 		target = (void *) &inti->ext;
2250 		source = &uptr->u.ext;
2251 		size = sizeof(inti->ext);
2252 		break;
2253 	case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2254 		target = (void *) &inti->io;
2255 		source = &uptr->u.io;
2256 		size = sizeof(inti->io);
2257 		break;
2258 	case KVM_S390_MCHK:
2259 		target = (void *) &inti->mchk;
2260 		source = &uptr->u.mchk;
2261 		size = sizeof(inti->mchk);
2262 		break;
2263 	default:
2264 		return -EINVAL;
2265 	}
2266 
2267 	if (copy_from_user(target, source, size))
2268 		return -EFAULT;
2269 
2270 	return 0;
2271 }
2272 
2273 static int enqueue_floating_irq(struct kvm_device *dev,
2274 				struct kvm_device_attr *attr)
2275 {
2276 	struct kvm_s390_interrupt_info *inti = NULL;
2277 	int r = 0;
2278 	int len = attr->attr;
2279 
2280 	if (len % sizeof(struct kvm_s390_irq) != 0)
2281 		return -EINVAL;
2282 	else if (len > KVM_S390_FLIC_MAX_BUFFER)
2283 		return -EINVAL;
2284 
2285 	while (len >= sizeof(struct kvm_s390_irq)) {
2286 		inti = kzalloc(sizeof(*inti), GFP_KERNEL);
2287 		if (!inti)
2288 			return -ENOMEM;
2289 
2290 		r = copy_irq_from_user(inti, attr->addr);
2291 		if (r) {
2292 			kfree(inti);
2293 			return r;
2294 		}
2295 		r = __inject_vm(dev->kvm, inti);
2296 		if (r) {
2297 			kfree(inti);
2298 			return r;
2299 		}
2300 		len -= sizeof(struct kvm_s390_irq);
2301 		attr->addr += sizeof(struct kvm_s390_irq);
2302 	}
2303 
2304 	return r;
2305 }
2306 
2307 static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
2308 {
2309 	if (id >= MAX_S390_IO_ADAPTERS)
2310 		return NULL;
2311 	id = array_index_nospec(id, MAX_S390_IO_ADAPTERS);
2312 	return kvm->arch.adapters[id];
2313 }
2314 
2315 static int register_io_adapter(struct kvm_device *dev,
2316 			       struct kvm_device_attr *attr)
2317 {
2318 	struct s390_io_adapter *adapter;
2319 	struct kvm_s390_io_adapter adapter_info;
2320 
2321 	if (copy_from_user(&adapter_info,
2322 			   (void __user *)attr->addr, sizeof(adapter_info)))
2323 		return -EFAULT;
2324 
2325 	if (adapter_info.id >= MAX_S390_IO_ADAPTERS)
2326 		return -EINVAL;
2327 
2328 	adapter_info.id = array_index_nospec(adapter_info.id,
2329 					     MAX_S390_IO_ADAPTERS);
2330 
2331 	if (dev->kvm->arch.adapters[adapter_info.id] != NULL)
2332 		return -EINVAL;
2333 
2334 	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
2335 	if (!adapter)
2336 		return -ENOMEM;
2337 
2338 	INIT_LIST_HEAD(&adapter->maps);
2339 	init_rwsem(&adapter->maps_lock);
2340 	atomic_set(&adapter->nr_maps, 0);
2341 	adapter->id = adapter_info.id;
2342 	adapter->isc = adapter_info.isc;
2343 	adapter->maskable = adapter_info.maskable;
2344 	adapter->masked = false;
2345 	adapter->swap = adapter_info.swap;
2346 	adapter->suppressible = (adapter_info.flags) &
2347 				KVM_S390_ADAPTER_SUPPRESSIBLE;
2348 	dev->kvm->arch.adapters[adapter->id] = adapter;
2349 
2350 	return 0;
2351 }
2352 
2353 int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
2354 {
2355 	int ret;
2356 	struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2357 
2358 	if (!adapter || !adapter->maskable)
2359 		return -EINVAL;
2360 	ret = adapter->masked;
2361 	adapter->masked = masked;
2362 	return ret;
2363 }
2364 
2365 static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
2366 {
2367 	struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2368 	struct s390_map_info *map;
2369 	int ret;
2370 
2371 	if (!adapter || !addr)
2372 		return -EINVAL;
2373 
2374 	map = kzalloc(sizeof(*map), GFP_KERNEL);
2375 	if (!map) {
2376 		ret = -ENOMEM;
2377 		goto out;
2378 	}
2379 	INIT_LIST_HEAD(&map->list);
2380 	map->guest_addr = addr;
2381 	map->addr = gmap_translate(kvm->arch.gmap, addr);
2382 	if (map->addr == -EFAULT) {
2383 		ret = -EFAULT;
2384 		goto out;
2385 	}
2386 	ret = get_user_pages_fast(map->addr, 1, FOLL_WRITE, &map->page);
2387 	if (ret < 0)
2388 		goto out;
2389 	BUG_ON(ret != 1);
2390 	down_write(&adapter->maps_lock);
2391 	if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
2392 		list_add_tail(&map->list, &adapter->maps);
2393 		ret = 0;
2394 	} else {
2395 		put_page(map->page);
2396 		ret = -EINVAL;
2397 	}
2398 	up_write(&adapter->maps_lock);
2399 out:
2400 	if (ret)
2401 		kfree(map);
2402 	return ret;
2403 }
2404 
2405 static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
2406 {
2407 	struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2408 	struct s390_map_info *map, *tmp;
2409 	int found = 0;
2410 
2411 	if (!adapter || !addr)
2412 		return -EINVAL;
2413 
2414 	down_write(&adapter->maps_lock);
2415 	list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
2416 		if (map->guest_addr == addr) {
2417 			found = 1;
2418 			atomic_dec(&adapter->nr_maps);
2419 			list_del(&map->list);
2420 			put_page(map->page);
2421 			kfree(map);
2422 			break;
2423 		}
2424 	}
2425 	up_write(&adapter->maps_lock);
2426 
2427 	return found ? 0 : -EINVAL;
2428 }
2429 
2430 void kvm_s390_destroy_adapters(struct kvm *kvm)
2431 {
2432 	int i;
2433 	struct s390_map_info *map, *tmp;
2434 
2435 	for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2436 		if (!kvm->arch.adapters[i])
2437 			continue;
2438 		list_for_each_entry_safe(map, tmp,
2439 					 &kvm->arch.adapters[i]->maps, list) {
2440 			list_del(&map->list);
2441 			put_page(map->page);
2442 			kfree(map);
2443 		}
2444 		kfree(kvm->arch.adapters[i]);
2445 	}
2446 }
2447 
2448 static int modify_io_adapter(struct kvm_device *dev,
2449 			     struct kvm_device_attr *attr)
2450 {
2451 	struct kvm_s390_io_adapter_req req;
2452 	struct s390_io_adapter *adapter;
2453 	int ret;
2454 
2455 	if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2456 		return -EFAULT;
2457 
2458 	adapter = get_io_adapter(dev->kvm, req.id);
2459 	if (!adapter)
2460 		return -EINVAL;
2461 	switch (req.type) {
2462 	case KVM_S390_IO_ADAPTER_MASK:
2463 		ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
2464 		if (ret > 0)
2465 			ret = 0;
2466 		break;
2467 	case KVM_S390_IO_ADAPTER_MAP:
2468 		ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2469 		break;
2470 	case KVM_S390_IO_ADAPTER_UNMAP:
2471 		ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2472 		break;
2473 	default:
2474 		ret = -EINVAL;
2475 	}
2476 
2477 	return ret;
2478 }
2479 
2480 static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr)
2481 
2482 {
2483 	const u64 isc_mask = 0xffUL << 24; /* all iscs set */
2484 	u32 schid;
2485 
2486 	if (attr->flags)
2487 		return -EINVAL;
2488 	if (attr->attr != sizeof(schid))
2489 		return -EINVAL;
2490 	if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid)))
2491 		return -EFAULT;
2492 	if (!schid)
2493 		return -EINVAL;
2494 	kfree(kvm_s390_get_io_int(kvm, isc_mask, schid));
2495 	/*
2496 	 * If userspace is conforming to the architecture, we can have at most
2497 	 * one pending I/O interrupt per subchannel, so this is effectively a
2498 	 * clear all.
2499 	 */
2500 	return 0;
2501 }
2502 
2503 static int modify_ais_mode(struct kvm *kvm, struct kvm_device_attr *attr)
2504 {
2505 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2506 	struct kvm_s390_ais_req req;
2507 	int ret = 0;
2508 
2509 	if (!test_kvm_facility(kvm, 72))
2510 		return -ENOTSUPP;
2511 
2512 	if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2513 		return -EFAULT;
2514 
2515 	if (req.isc > MAX_ISC)
2516 		return -EINVAL;
2517 
2518 	trace_kvm_s390_modify_ais_mode(req.isc,
2519 				       (fi->simm & AIS_MODE_MASK(req.isc)) ?
2520 				       (fi->nimm & AIS_MODE_MASK(req.isc)) ?
2521 				       2 : KVM_S390_AIS_MODE_SINGLE :
2522 				       KVM_S390_AIS_MODE_ALL, req.mode);
2523 
2524 	mutex_lock(&fi->ais_lock);
2525 	switch (req.mode) {
2526 	case KVM_S390_AIS_MODE_ALL:
2527 		fi->simm &= ~AIS_MODE_MASK(req.isc);
2528 		fi->nimm &= ~AIS_MODE_MASK(req.isc);
2529 		break;
2530 	case KVM_S390_AIS_MODE_SINGLE:
2531 		fi->simm |= AIS_MODE_MASK(req.isc);
2532 		fi->nimm &= ~AIS_MODE_MASK(req.isc);
2533 		break;
2534 	default:
2535 		ret = -EINVAL;
2536 	}
2537 	mutex_unlock(&fi->ais_lock);
2538 
2539 	return ret;
2540 }
2541 
2542 static int kvm_s390_inject_airq(struct kvm *kvm,
2543 				struct s390_io_adapter *adapter)
2544 {
2545 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2546 	struct kvm_s390_interrupt s390int = {
2547 		.type = KVM_S390_INT_IO(1, 0, 0, 0),
2548 		.parm = 0,
2549 		.parm64 = isc_to_int_word(adapter->isc),
2550 	};
2551 	int ret = 0;
2552 
2553 	if (!test_kvm_facility(kvm, 72) || !adapter->suppressible)
2554 		return kvm_s390_inject_vm(kvm, &s390int);
2555 
2556 	mutex_lock(&fi->ais_lock);
2557 	if (fi->nimm & AIS_MODE_MASK(adapter->isc)) {
2558 		trace_kvm_s390_airq_suppressed(adapter->id, adapter->isc);
2559 		goto out;
2560 	}
2561 
2562 	ret = kvm_s390_inject_vm(kvm, &s390int);
2563 	if (!ret && (fi->simm & AIS_MODE_MASK(adapter->isc))) {
2564 		fi->nimm |= AIS_MODE_MASK(adapter->isc);
2565 		trace_kvm_s390_modify_ais_mode(adapter->isc,
2566 					       KVM_S390_AIS_MODE_SINGLE, 2);
2567 	}
2568 out:
2569 	mutex_unlock(&fi->ais_lock);
2570 	return ret;
2571 }
2572 
2573 static int flic_inject_airq(struct kvm *kvm, struct kvm_device_attr *attr)
2574 {
2575 	unsigned int id = attr->attr;
2576 	struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2577 
2578 	if (!adapter)
2579 		return -EINVAL;
2580 
2581 	return kvm_s390_inject_airq(kvm, adapter);
2582 }
2583 
2584 static int flic_ais_mode_set_all(struct kvm *kvm, struct kvm_device_attr *attr)
2585 {
2586 	struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2587 	struct kvm_s390_ais_all ais;
2588 
2589 	if (!test_kvm_facility(kvm, 72))
2590 		return -ENOTSUPP;
2591 
2592 	if (copy_from_user(&ais, (void __user *)attr->addr, sizeof(ais)))
2593 		return -EFAULT;
2594 
2595 	mutex_lock(&fi->ais_lock);
2596 	fi->simm = ais.simm;
2597 	fi->nimm = ais.nimm;
2598 	mutex_unlock(&fi->ais_lock);
2599 
2600 	return 0;
2601 }
2602 
2603 static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2604 {
2605 	int r = 0;
2606 	unsigned int i;
2607 	struct kvm_vcpu *vcpu;
2608 
2609 	switch (attr->group) {
2610 	case KVM_DEV_FLIC_ENQUEUE:
2611 		r = enqueue_floating_irq(dev, attr);
2612 		break;
2613 	case KVM_DEV_FLIC_CLEAR_IRQS:
2614 		kvm_s390_clear_float_irqs(dev->kvm);
2615 		break;
2616 	case KVM_DEV_FLIC_APF_ENABLE:
2617 		dev->kvm->arch.gmap->pfault_enabled = 1;
2618 		break;
2619 	case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2620 		dev->kvm->arch.gmap->pfault_enabled = 0;
2621 		/*
2622 		 * Make sure no async faults are in transition when
2623 		 * clearing the queues. So we don't need to worry
2624 		 * about late coming workers.
2625 		 */
2626 		synchronize_srcu(&dev->kvm->srcu);
2627 		kvm_for_each_vcpu(i, vcpu, dev->kvm)
2628 			kvm_clear_async_pf_completion_queue(vcpu);
2629 		break;
2630 	case KVM_DEV_FLIC_ADAPTER_REGISTER:
2631 		r = register_io_adapter(dev, attr);
2632 		break;
2633 	case KVM_DEV_FLIC_ADAPTER_MODIFY:
2634 		r = modify_io_adapter(dev, attr);
2635 		break;
2636 	case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2637 		r = clear_io_irq(dev->kvm, attr);
2638 		break;
2639 	case KVM_DEV_FLIC_AISM:
2640 		r = modify_ais_mode(dev->kvm, attr);
2641 		break;
2642 	case KVM_DEV_FLIC_AIRQ_INJECT:
2643 		r = flic_inject_airq(dev->kvm, attr);
2644 		break;
2645 	case KVM_DEV_FLIC_AISM_ALL:
2646 		r = flic_ais_mode_set_all(dev->kvm, attr);
2647 		break;
2648 	default:
2649 		r = -EINVAL;
2650 	}
2651 
2652 	return r;
2653 }
2654 
2655 static int flic_has_attr(struct kvm_device *dev,
2656 			     struct kvm_device_attr *attr)
2657 {
2658 	switch (attr->group) {
2659 	case KVM_DEV_FLIC_GET_ALL_IRQS:
2660 	case KVM_DEV_FLIC_ENQUEUE:
2661 	case KVM_DEV_FLIC_CLEAR_IRQS:
2662 	case KVM_DEV_FLIC_APF_ENABLE:
2663 	case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2664 	case KVM_DEV_FLIC_ADAPTER_REGISTER:
2665 	case KVM_DEV_FLIC_ADAPTER_MODIFY:
2666 	case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2667 	case KVM_DEV_FLIC_AISM:
2668 	case KVM_DEV_FLIC_AIRQ_INJECT:
2669 	case KVM_DEV_FLIC_AISM_ALL:
2670 		return 0;
2671 	}
2672 	return -ENXIO;
2673 }
2674 
2675 static int flic_create(struct kvm_device *dev, u32 type)
2676 {
2677 	if (!dev)
2678 		return -EINVAL;
2679 	if (dev->kvm->arch.flic)
2680 		return -EINVAL;
2681 	dev->kvm->arch.flic = dev;
2682 	return 0;
2683 }
2684 
2685 static void flic_destroy(struct kvm_device *dev)
2686 {
2687 	dev->kvm->arch.flic = NULL;
2688 	kfree(dev);
2689 }
2690 
2691 /* s390 floating irq controller (flic) */
2692 struct kvm_device_ops kvm_flic_ops = {
2693 	.name = "kvm-flic",
2694 	.get_attr = flic_get_attr,
2695 	.set_attr = flic_set_attr,
2696 	.has_attr = flic_has_attr,
2697 	.create = flic_create,
2698 	.destroy = flic_destroy,
2699 };
2700 
2701 static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2702 {
2703 	unsigned long bit;
2704 
2705 	bit = bit_nr + (addr % PAGE_SIZE) * 8;
2706 
2707 	return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2708 }
2709 
2710 static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2711 					  u64 addr)
2712 {
2713 	struct s390_map_info *map;
2714 
2715 	if (!adapter)
2716 		return NULL;
2717 
2718 	list_for_each_entry(map, &adapter->maps, list) {
2719 		if (map->guest_addr == addr)
2720 			return map;
2721 	}
2722 	return NULL;
2723 }
2724 
2725 static int adapter_indicators_set(struct kvm *kvm,
2726 				  struct s390_io_adapter *adapter,
2727 				  struct kvm_s390_adapter_int *adapter_int)
2728 {
2729 	unsigned long bit;
2730 	int summary_set, idx;
2731 	struct s390_map_info *info;
2732 	void *map;
2733 
2734 	info = get_map_info(adapter, adapter_int->ind_addr);
2735 	if (!info)
2736 		return -1;
2737 	map = page_address(info->page);
2738 	bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2739 	set_bit(bit, map);
2740 	idx = srcu_read_lock(&kvm->srcu);
2741 	mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2742 	set_page_dirty_lock(info->page);
2743 	info = get_map_info(adapter, adapter_int->summary_addr);
2744 	if (!info) {
2745 		srcu_read_unlock(&kvm->srcu, idx);
2746 		return -1;
2747 	}
2748 	map = page_address(info->page);
2749 	bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2750 			  adapter->swap);
2751 	summary_set = test_and_set_bit(bit, map);
2752 	mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2753 	set_page_dirty_lock(info->page);
2754 	srcu_read_unlock(&kvm->srcu, idx);
2755 	return summary_set ? 0 : 1;
2756 }
2757 
2758 /*
2759  * < 0 - not injected due to error
2760  * = 0 - coalesced, summary indicator already active
2761  * > 0 - injected interrupt
2762  */
2763 static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2764 			   struct kvm *kvm, int irq_source_id, int level,
2765 			   bool line_status)
2766 {
2767 	int ret;
2768 	struct s390_io_adapter *adapter;
2769 
2770 	/* We're only interested in the 0->1 transition. */
2771 	if (!level)
2772 		return 0;
2773 	adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2774 	if (!adapter)
2775 		return -1;
2776 	down_read(&adapter->maps_lock);
2777 	ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2778 	up_read(&adapter->maps_lock);
2779 	if ((ret > 0) && !adapter->masked) {
2780 		ret = kvm_s390_inject_airq(kvm, adapter);
2781 		if (ret == 0)
2782 			ret = 1;
2783 	}
2784 	return ret;
2785 }
2786 
2787 /*
2788  * Inject the machine check to the guest.
2789  */
2790 void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
2791 				     struct mcck_volatile_info *mcck_info)
2792 {
2793 	struct kvm_s390_interrupt_info inti;
2794 	struct kvm_s390_irq irq;
2795 	struct kvm_s390_mchk_info *mchk;
2796 	union mci mci;
2797 	__u64 cr14 = 0;         /* upper bits are not used */
2798 	int rc;
2799 
2800 	mci.val = mcck_info->mcic;
2801 	if (mci.sr)
2802 		cr14 |= CR14_RECOVERY_SUBMASK;
2803 	if (mci.dg)
2804 		cr14 |= CR14_DEGRADATION_SUBMASK;
2805 	if (mci.w)
2806 		cr14 |= CR14_WARNING_SUBMASK;
2807 
2808 	mchk = mci.ck ? &inti.mchk : &irq.u.mchk;
2809 	mchk->cr14 = cr14;
2810 	mchk->mcic = mcck_info->mcic;
2811 	mchk->ext_damage_code = mcck_info->ext_damage_code;
2812 	mchk->failing_storage_address = mcck_info->failing_storage_address;
2813 	if (mci.ck) {
2814 		/* Inject the floating machine check */
2815 		inti.type = KVM_S390_MCHK;
2816 		rc = __inject_vm(vcpu->kvm, &inti);
2817 	} else {
2818 		/* Inject the machine check to specified vcpu */
2819 		irq.type = KVM_S390_MCHK;
2820 		rc = kvm_s390_inject_vcpu(vcpu, &irq);
2821 	}
2822 	WARN_ON_ONCE(rc);
2823 }
2824 
2825 int kvm_set_routing_entry(struct kvm *kvm,
2826 			  struct kvm_kernel_irq_routing_entry *e,
2827 			  const struct kvm_irq_routing_entry *ue)
2828 {
2829 	int ret;
2830 
2831 	switch (ue->type) {
2832 	case KVM_IRQ_ROUTING_S390_ADAPTER:
2833 		e->set = set_adapter_int;
2834 		e->adapter.summary_addr = ue->u.adapter.summary_addr;
2835 		e->adapter.ind_addr = ue->u.adapter.ind_addr;
2836 		e->adapter.summary_offset = ue->u.adapter.summary_offset;
2837 		e->adapter.ind_offset = ue->u.adapter.ind_offset;
2838 		e->adapter.adapter_id = ue->u.adapter.adapter_id;
2839 		ret = 0;
2840 		break;
2841 	default:
2842 		ret = -EINVAL;
2843 	}
2844 
2845 	return ret;
2846 }
2847 
2848 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2849 		int irq_source_id, int level, bool line_status)
2850 {
2851 	return -EINVAL;
2852 }
2853 
2854 int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2855 {
2856 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2857 	struct kvm_s390_irq *buf;
2858 	int r = 0;
2859 	int n;
2860 
2861 	buf = vmalloc(len);
2862 	if (!buf)
2863 		return -ENOMEM;
2864 
2865 	if (copy_from_user((void *) buf, irqstate, len)) {
2866 		r = -EFAULT;
2867 		goto out_free;
2868 	}
2869 
2870 	/*
2871 	 * Don't allow setting the interrupt state
2872 	 * when there are already interrupts pending
2873 	 */
2874 	spin_lock(&li->lock);
2875 	if (li->pending_irqs) {
2876 		r = -EBUSY;
2877 		goto out_unlock;
2878 	}
2879 
2880 	for (n = 0; n < len / sizeof(*buf); n++) {
2881 		r = do_inject_vcpu(vcpu, &buf[n]);
2882 		if (r)
2883 			break;
2884 	}
2885 
2886 out_unlock:
2887 	spin_unlock(&li->lock);
2888 out_free:
2889 	vfree(buf);
2890 
2891 	return r;
2892 }
2893 
2894 static void store_local_irq(struct kvm_s390_local_interrupt *li,
2895 			    struct kvm_s390_irq *irq,
2896 			    unsigned long irq_type)
2897 {
2898 	switch (irq_type) {
2899 	case IRQ_PEND_MCHK_EX:
2900 	case IRQ_PEND_MCHK_REP:
2901 		irq->type = KVM_S390_MCHK;
2902 		irq->u.mchk = li->irq.mchk;
2903 		break;
2904 	case IRQ_PEND_PROG:
2905 		irq->type = KVM_S390_PROGRAM_INT;
2906 		irq->u.pgm = li->irq.pgm;
2907 		break;
2908 	case IRQ_PEND_PFAULT_INIT:
2909 		irq->type = KVM_S390_INT_PFAULT_INIT;
2910 		irq->u.ext = li->irq.ext;
2911 		break;
2912 	case IRQ_PEND_EXT_EXTERNAL:
2913 		irq->type = KVM_S390_INT_EXTERNAL_CALL;
2914 		irq->u.extcall = li->irq.extcall;
2915 		break;
2916 	case IRQ_PEND_EXT_CLOCK_COMP:
2917 		irq->type = KVM_S390_INT_CLOCK_COMP;
2918 		break;
2919 	case IRQ_PEND_EXT_CPU_TIMER:
2920 		irq->type = KVM_S390_INT_CPU_TIMER;
2921 		break;
2922 	case IRQ_PEND_SIGP_STOP:
2923 		irq->type = KVM_S390_SIGP_STOP;
2924 		irq->u.stop = li->irq.stop;
2925 		break;
2926 	case IRQ_PEND_RESTART:
2927 		irq->type = KVM_S390_RESTART;
2928 		break;
2929 	case IRQ_PEND_SET_PREFIX:
2930 		irq->type = KVM_S390_SIGP_SET_PREFIX;
2931 		irq->u.prefix = li->irq.prefix;
2932 		break;
2933 	}
2934 }
2935 
2936 int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2937 {
2938 	int scn;
2939 	DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
2940 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2941 	unsigned long pending_irqs;
2942 	struct kvm_s390_irq irq;
2943 	unsigned long irq_type;
2944 	int cpuaddr;
2945 	int n = 0;
2946 
2947 	spin_lock(&li->lock);
2948 	pending_irqs = li->pending_irqs;
2949 	memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2950 	       sizeof(sigp_emerg_pending));
2951 	spin_unlock(&li->lock);
2952 
2953 	for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2954 		memset(&irq, 0, sizeof(irq));
2955 		if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2956 			continue;
2957 		if (n + sizeof(irq) > len)
2958 			return -ENOBUFS;
2959 		store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2960 		if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2961 			return -EFAULT;
2962 		n += sizeof(irq);
2963 	}
2964 
2965 	if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2966 		for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2967 			memset(&irq, 0, sizeof(irq));
2968 			if (n + sizeof(irq) > len)
2969 				return -ENOBUFS;
2970 			irq.type = KVM_S390_INT_EMERGENCY;
2971 			irq.u.emerg.code = cpuaddr;
2972 			if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2973 				return -EFAULT;
2974 			n += sizeof(irq);
2975 		}
2976 	}
2977 
2978 	if (sca_ext_call_pending(vcpu, &scn)) {
2979 		if (n + sizeof(irq) > len)
2980 			return -ENOBUFS;
2981 		memset(&irq, 0, sizeof(irq));
2982 		irq.type = KVM_S390_INT_EXTERNAL_CALL;
2983 		irq.u.extcall.code = scn;
2984 		if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2985 			return -EFAULT;
2986 		n += sizeof(irq);
2987 	}
2988 
2989 	return n;
2990 }
2991 
2992 static void __airqs_kick_single_vcpu(struct kvm *kvm, u8 deliverable_mask)
2993 {
2994 	int vcpu_id, online_vcpus = atomic_read(&kvm->online_vcpus);
2995 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
2996 	struct kvm_vcpu *vcpu;
2997 
2998 	for_each_set_bit(vcpu_id, kvm->arch.idle_mask, online_vcpus) {
2999 		vcpu = kvm_get_vcpu(kvm, vcpu_id);
3000 		if (psw_ioint_disabled(vcpu))
3001 			continue;
3002 		deliverable_mask &= (u8)(vcpu->arch.sie_block->gcr[6] >> 24);
3003 		if (deliverable_mask) {
3004 			/* lately kicked but not yet running */
3005 			if (test_and_set_bit(vcpu_id, gi->kicked_mask))
3006 				return;
3007 			kvm_s390_vcpu_wakeup(vcpu);
3008 			return;
3009 		}
3010 	}
3011 }
3012 
3013 static enum hrtimer_restart gisa_vcpu_kicker(struct hrtimer *timer)
3014 {
3015 	struct kvm_s390_gisa_interrupt *gi =
3016 		container_of(timer, struct kvm_s390_gisa_interrupt, timer);
3017 	struct kvm *kvm =
3018 		container_of(gi->origin, struct sie_page2, gisa)->kvm;
3019 	u8 pending_mask;
3020 
3021 	pending_mask = gisa_get_ipm_or_restore_iam(gi);
3022 	if (pending_mask) {
3023 		__airqs_kick_single_vcpu(kvm, pending_mask);
3024 		hrtimer_forward_now(timer, ns_to_ktime(gi->expires));
3025 		return HRTIMER_RESTART;
3026 	};
3027 
3028 	return HRTIMER_NORESTART;
3029 }
3030 
3031 #define NULL_GISA_ADDR 0x00000000UL
3032 #define NONE_GISA_ADDR 0x00000001UL
3033 #define GISA_ADDR_MASK 0xfffff000UL
3034 
3035 static void process_gib_alert_list(void)
3036 {
3037 	struct kvm_s390_gisa_interrupt *gi;
3038 	struct kvm_s390_gisa *gisa;
3039 	struct kvm *kvm;
3040 	u32 final, origin = 0UL;
3041 
3042 	do {
3043 		/*
3044 		 * If the NONE_GISA_ADDR is still stored in the alert list
3045 		 * origin, we will leave the outer loop. No further GISA has
3046 		 * been added to the alert list by millicode while processing
3047 		 * the current alert list.
3048 		 */
3049 		final = (origin & NONE_GISA_ADDR);
3050 		/*
3051 		 * Cut off the alert list and store the NONE_GISA_ADDR in the
3052 		 * alert list origin to avoid further GAL interruptions.
3053 		 * A new alert list can be build up by millicode in parallel
3054 		 * for guests not in the yet cut-off alert list. When in the
3055 		 * final loop, store the NULL_GISA_ADDR instead. This will re-
3056 		 * enable GAL interruptions on the host again.
3057 		 */
3058 		origin = xchg(&gib->alert_list_origin,
3059 			      (!final) ? NONE_GISA_ADDR : NULL_GISA_ADDR);
3060 		/*
3061 		 * Loop through the just cut-off alert list and start the
3062 		 * gisa timers to kick idle vcpus to consume the pending
3063 		 * interruptions asap.
3064 		 */
3065 		while (origin & GISA_ADDR_MASK) {
3066 			gisa = (struct kvm_s390_gisa *)(u64)origin;
3067 			origin = gisa->next_alert;
3068 			gisa->next_alert = (u32)(u64)gisa;
3069 			kvm = container_of(gisa, struct sie_page2, gisa)->kvm;
3070 			gi = &kvm->arch.gisa_int;
3071 			if (hrtimer_active(&gi->timer))
3072 				hrtimer_cancel(&gi->timer);
3073 			hrtimer_start(&gi->timer, 0, HRTIMER_MODE_REL);
3074 		}
3075 	} while (!final);
3076 
3077 }
3078 
3079 void kvm_s390_gisa_clear(struct kvm *kvm)
3080 {
3081 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3082 
3083 	if (!gi->origin)
3084 		return;
3085 	gisa_clear_ipm(gi->origin);
3086 	VM_EVENT(kvm, 3, "gisa 0x%pK cleared", gi->origin);
3087 }
3088 
3089 void kvm_s390_gisa_init(struct kvm *kvm)
3090 {
3091 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3092 
3093 	if (!css_general_characteristics.aiv)
3094 		return;
3095 	gi->origin = &kvm->arch.sie_page2->gisa;
3096 	gi->alert.mask = 0;
3097 	spin_lock_init(&gi->alert.ref_lock);
3098 	gi->expires = 50 * 1000; /* 50 usec */
3099 	hrtimer_init(&gi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3100 	gi->timer.function = gisa_vcpu_kicker;
3101 	memset(gi->origin, 0, sizeof(struct kvm_s390_gisa));
3102 	gi->origin->next_alert = (u32)(u64)gi->origin;
3103 	VM_EVENT(kvm, 3, "gisa 0x%pK initialized", gi->origin);
3104 }
3105 
3106 void kvm_s390_gisa_destroy(struct kvm *kvm)
3107 {
3108 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3109 
3110 	if (!gi->origin)
3111 		return;
3112 	if (gi->alert.mask)
3113 		KVM_EVENT(3, "vm 0x%pK has unexpected iam 0x%02x",
3114 			  kvm, gi->alert.mask);
3115 	while (gisa_in_alert_list(gi->origin))
3116 		cpu_relax();
3117 	hrtimer_cancel(&gi->timer);
3118 	gi->origin = NULL;
3119 }
3120 
3121 /**
3122  * kvm_s390_gisc_register - register a guest ISC
3123  *
3124  * @kvm:  the kernel vm to work with
3125  * @gisc: the guest interruption sub class to register
3126  *
3127  * The function extends the vm specific alert mask to use.
3128  * The effective IAM mask in the GISA is updated as well
3129  * in case the GISA is not part of the GIB alert list.
3130  * It will be updated latest when the IAM gets restored
3131  * by gisa_get_ipm_or_restore_iam().
3132  *
3133  * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3134  *          has registered with the channel subsystem.
3135  *          -ENODEV in case the vm uses no GISA
3136  *          -ERANGE in case the guest ISC is invalid
3137  */
3138 int kvm_s390_gisc_register(struct kvm *kvm, u32 gisc)
3139 {
3140 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3141 
3142 	if (!gi->origin)
3143 		return -ENODEV;
3144 	if (gisc > MAX_ISC)
3145 		return -ERANGE;
3146 
3147 	spin_lock(&gi->alert.ref_lock);
3148 	gi->alert.ref_count[gisc]++;
3149 	if (gi->alert.ref_count[gisc] == 1) {
3150 		gi->alert.mask |= 0x80 >> gisc;
3151 		gisa_set_iam(gi->origin, gi->alert.mask);
3152 	}
3153 	spin_unlock(&gi->alert.ref_lock);
3154 
3155 	return gib->nisc;
3156 }
3157 EXPORT_SYMBOL_GPL(kvm_s390_gisc_register);
3158 
3159 /**
3160  * kvm_s390_gisc_unregister - unregister a guest ISC
3161  *
3162  * @kvm:  the kernel vm to work with
3163  * @gisc: the guest interruption sub class to register
3164  *
3165  * The function reduces the vm specific alert mask to use.
3166  * The effective IAM mask in the GISA is updated as well
3167  * in case the GISA is not part of the GIB alert list.
3168  * It will be updated latest when the IAM gets restored
3169  * by gisa_get_ipm_or_restore_iam().
3170  *
3171  * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3172  *          has registered with the channel subsystem.
3173  *          -ENODEV in case the vm uses no GISA
3174  *          -ERANGE in case the guest ISC is invalid
3175  *          -EINVAL in case the guest ISC is not registered
3176  */
3177 int kvm_s390_gisc_unregister(struct kvm *kvm, u32 gisc)
3178 {
3179 	struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3180 	int rc = 0;
3181 
3182 	if (!gi->origin)
3183 		return -ENODEV;
3184 	if (gisc > MAX_ISC)
3185 		return -ERANGE;
3186 
3187 	spin_lock(&gi->alert.ref_lock);
3188 	if (gi->alert.ref_count[gisc] == 0) {
3189 		rc = -EINVAL;
3190 		goto out;
3191 	}
3192 	gi->alert.ref_count[gisc]--;
3193 	if (gi->alert.ref_count[gisc] == 0) {
3194 		gi->alert.mask &= ~(0x80 >> gisc);
3195 		gisa_set_iam(gi->origin, gi->alert.mask);
3196 	}
3197 out:
3198 	spin_unlock(&gi->alert.ref_lock);
3199 
3200 	return rc;
3201 }
3202 EXPORT_SYMBOL_GPL(kvm_s390_gisc_unregister);
3203 
3204 static void gib_alert_irq_handler(struct airq_struct *airq, bool floating)
3205 {
3206 	inc_irq_stat(IRQIO_GAL);
3207 	process_gib_alert_list();
3208 }
3209 
3210 static struct airq_struct gib_alert_irq = {
3211 	.handler = gib_alert_irq_handler,
3212 	.lsi_ptr = &gib_alert_irq.lsi_mask,
3213 };
3214 
3215 void kvm_s390_gib_destroy(void)
3216 {
3217 	if (!gib)
3218 		return;
3219 	chsc_sgib(0);
3220 	unregister_adapter_interrupt(&gib_alert_irq);
3221 	free_page((unsigned long)gib);
3222 	gib = NULL;
3223 }
3224 
3225 int kvm_s390_gib_init(u8 nisc)
3226 {
3227 	int rc = 0;
3228 
3229 	if (!css_general_characteristics.aiv) {
3230 		KVM_EVENT(3, "%s", "gib not initialized, no AIV facility");
3231 		goto out;
3232 	}
3233 
3234 	gib = (struct kvm_s390_gib *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
3235 	if (!gib) {
3236 		rc = -ENOMEM;
3237 		goto out;
3238 	}
3239 
3240 	gib_alert_irq.isc = nisc;
3241 	if (register_adapter_interrupt(&gib_alert_irq)) {
3242 		pr_err("Registering the GIB alert interruption handler failed\n");
3243 		rc = -EIO;
3244 		goto out_free_gib;
3245 	}
3246 
3247 	gib->nisc = nisc;
3248 	if (chsc_sgib((u32)(u64)gib)) {
3249 		pr_err("Associating the GIB with the AIV facility failed\n");
3250 		free_page((unsigned long)gib);
3251 		gib = NULL;
3252 		rc = -EIO;
3253 		goto out_unreg_gal;
3254 	}
3255 
3256 	KVM_EVENT(3, "gib 0x%pK (nisc=%d) initialized", gib, gib->nisc);
3257 	goto out;
3258 
3259 out_unreg_gal:
3260 	unregister_adapter_interrupt(&gib_alert_irq);
3261 out_free_gib:
3262 	free_page((unsigned long)gib);
3263 	gib = NULL;
3264 out:
3265 	return rc;
3266 }
3267