1 // SPDX-License-Identifier: GPL-2.0
3 * handling kvm guest interrupts
5 * Copyright IBM Corp. 2008, 2015
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
10 #define KMSG_COMPONENT "kvm-s390"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
24 #include <linux/uaccess.h>
28 #include <asm/switch_to.h>
33 #include "trace-s390.h"
35 #define PFAULT_INIT 0x0600
36 #define PFAULT_DONE 0x0680
37 #define VIRTIO_PARAM 0x0d00
39 static struct kvm_s390_gib
*gib
;
41 /* handle external calls via sigp interpretation facility */
42 static int sca_ext_call_pending(struct kvm_vcpu
*vcpu
, int *src_id
)
46 if (!kvm_s390_test_cpuflags(vcpu
, CPUSTAT_ECALL_PEND
))
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
;
59 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
60 union bsca_sigp_ctrl sigp_ctrl
=
61 sca
->cpu
[vcpu
->vcpu_id
].sigp_ctrl
;
66 read_unlock(&vcpu
->kvm
->arch
.sca_lock
);
74 static int sca_inject_ext_call(struct kvm_vcpu
*vcpu
, int src_id
)
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
;
90 expect
= old_val
.value
;
91 rc
= cmpxchg(&sigp_ctrl
->value
, old_val
.value
, new_val
.value
);
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
;
102 expect
= old_val
.value
;
103 rc
= cmpxchg(&sigp_ctrl
->value
, old_val
.value
, new_val
.value
);
105 read_unlock(&vcpu
->kvm
->arch
.sca_lock
);
108 /* another external call is pending */
111 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_ECALL_PEND
);
115 static void sca_clear_ext_call(struct kvm_vcpu
*vcpu
)
119 if (!kvm_s390_use_sca_entries())
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
;
130 rc
= cmpxchg(&sigp_ctrl
->value
, old
.value
, 0);
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
;
138 rc
= cmpxchg(&sigp_ctrl
->value
, old
.value
, 0);
140 read_unlock(&vcpu
->kvm
->arch
.sca_lock
);
141 WARN_ON(rc
!= expect
); /* cannot clear? */
144 int psw_extint_disabled(struct kvm_vcpu
*vcpu
)
146 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_EXT
);
149 static int psw_ioint_disabled(struct kvm_vcpu
*vcpu
)
151 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_IO
);
154 static int psw_mchk_disabled(struct kvm_vcpu
*vcpu
)
156 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_MCHECK
);
159 static int psw_interrupts_disabled(struct kvm_vcpu
*vcpu
)
161 return psw_extint_disabled(vcpu
) &&
162 psw_ioint_disabled(vcpu
) &&
163 psw_mchk_disabled(vcpu
);
166 static int ckc_interrupts_enabled(struct kvm_vcpu
*vcpu
)
168 if (psw_extint_disabled(vcpu
) ||
169 !(vcpu
->arch
.sie_block
->gcr
[0] & CR0_CLOCK_COMPARATOR_SUBMASK
))
171 if (guestdbg_enabled(vcpu
) && guestdbg_sstep_enabled(vcpu
))
172 /* No timer interrupts when single stepping */
177 static int ckc_irq_pending(struct kvm_vcpu
*vcpu
)
179 const u64 now
= kvm_s390_get_tod_clock_fast(vcpu
->kvm
);
180 const u64 ckc
= vcpu
->arch
.sie_block
->ckc
;
182 if (vcpu
->arch
.sie_block
->gcr
[0] & CR0_CLOCK_COMPARATOR_SIGN
) {
183 if ((s64
)ckc
>= (s64
)now
)
185 } else if (ckc
>= now
) {
188 return ckc_interrupts_enabled(vcpu
);
191 static int cpu_timer_interrupts_enabled(struct kvm_vcpu
*vcpu
)
193 return !psw_extint_disabled(vcpu
) &&
194 (vcpu
->arch
.sie_block
->gcr
[0] & CR0_CPU_TIMER_SUBMASK
);
197 static int cpu_timer_irq_pending(struct kvm_vcpu
*vcpu
)
199 if (!cpu_timer_interrupts_enabled(vcpu
))
201 return kvm_s390_get_cpu_timer(vcpu
) >> 63;
204 static uint64_t isc_to_isc_bits(int isc
)
206 return (0x80 >> isc
) << 24;
209 static inline u32
isc_to_int_word(u8 isc
)
211 return ((u32
)isc
<< 27) | 0x80000000;
214 static inline u8
int_word_to_isc(u32 int_word
)
216 return (int_word
& 0x38000000) >> 27;
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.
225 #define IPM_BIT_OFFSET (offsetof(struct kvm_s390_gisa, ipm) * BITS_PER_BYTE)
228 * gisa_set_iam - change the GISA interruption alert mask
230 * @gisa: gisa to operate on
231 * @iam: new IAM value to use
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.
237 * Returns: 0 on success
238 * -EBUSY in case the gisa is part of the alert list
240 static inline int gisa_set_iam(struct kvm_s390_gisa
*gisa
, u8 iam
)
245 word
= READ_ONCE(gisa
->u64
.word
[0]);
246 if ((u64
)gisa
!= word
>> 32)
248 _word
= (word
& ~0xffUL
) | iam
;
249 } while (cmpxchg(&gisa
->u64
.word
[0], word
, _word
) != word
);
255 * gisa_clear_ipm - clear the GISA interruption pending mask
257 * @gisa: gisa to operate on
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.
263 static inline void gisa_clear_ipm(struct kvm_s390_gisa
*gisa
)
268 word
= READ_ONCE(gisa
->u64
.word
[0]);
269 _word
= word
& ~(0xffUL
<< 24);
270 } while (cmpxchg(&gisa
->u64
.word
[0], word
, _word
) != word
);
274 * gisa_get_ipm_or_restore_iam - return IPM or restore GISA IAM
276 * @gi: gisa interrupt struct to work on
278 * Atomically restores the interruption alert mask if none of the
279 * relevant ISCs are pending and return the IPM.
281 * Returns: the relevant pending ISCs
283 static inline u8
gisa_get_ipm_or_restore_iam(struct kvm_s390_gisa_interrupt
*gi
)
285 u8 pending_mask
, alert_mask
;
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
;
294 _word
= (word
& ~0xffUL
) | alert_mask
;
295 } while (cmpxchg(&gi
->origin
->u64
.word
[0], word
, _word
) != word
);
300 static inline int gisa_in_alert_list(struct kvm_s390_gisa
*gisa
)
302 return READ_ONCE(gisa
->next_alert
) != (u32
)(u64
)gisa
;
305 static inline void gisa_set_ipm_gisc(struct kvm_s390_gisa
*gisa
, u32 gisc
)
307 set_bit_inv(IPM_BIT_OFFSET
+ gisc
, (unsigned long *) gisa
);
310 static inline u8
gisa_get_ipm(struct kvm_s390_gisa
*gisa
)
312 return READ_ONCE(gisa
->ipm
);
315 static inline void gisa_clear_ipm_gisc(struct kvm_s390_gisa
*gisa
, u32 gisc
)
317 clear_bit_inv(IPM_BIT_OFFSET
+ gisc
, (unsigned long *) gisa
);
320 static inline int gisa_tac_ipm_gisc(struct kvm_s390_gisa
*gisa
, u32 gisc
)
322 return test_and_clear_bit_inv(IPM_BIT_OFFSET
+ gisc
, (unsigned long *) gisa
);
325 static inline unsigned long pending_irqs_no_gisa(struct kvm_vcpu
*vcpu
)
327 return vcpu
->kvm
->arch
.float_int
.pending_irqs
|
328 vcpu
->arch
.local_int
.pending_irqs
;
331 static inline unsigned long pending_irqs(struct kvm_vcpu
*vcpu
)
333 struct kvm_s390_gisa_interrupt
*gi
= &vcpu
->kvm
->arch
.gisa_int
;
334 unsigned long pending_mask
;
336 pending_mask
= pending_irqs_no_gisa(vcpu
);
338 pending_mask
|= gisa_get_ipm(gi
->origin
) << IRQ_PEND_IO_ISC_7
;
342 static inline int isc_to_irq_type(unsigned long isc
)
344 return IRQ_PEND_IO_ISC_0
- isc
;
347 static inline int irq_type_to_isc(unsigned long irq_type
)
349 return IRQ_PEND_IO_ISC_0
- irq_type
;
352 static unsigned long disable_iscs(struct kvm_vcpu
*vcpu
,
353 unsigned long active_mask
)
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
)));
364 static unsigned long deliverable_irqs(struct kvm_vcpu
*vcpu
)
366 unsigned long active_mask
;
368 active_mask
= pending_irqs(vcpu
);
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
;
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
;
391 * Check both floating and local interrupt's cr14 because
392 * bit IRQ_PEND_MCHK_REP could be set in both cases.
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
);
400 * STOP irqs will never be actively delivered. They are triggered via
401 * intercept requests and cleared when the stop intercept is performed.
403 __clear_bit(IRQ_PEND_SIGP_STOP
, &active_mask
);
408 static void __set_cpu_idle(struct kvm_vcpu
*vcpu
)
410 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_WAIT
);
411 set_bit(vcpu
->vcpu_id
, vcpu
->kvm
->arch
.idle_mask
);
414 static void __unset_cpu_idle(struct kvm_vcpu
*vcpu
)
416 kvm_s390_clear_cpuflags(vcpu
, CPUSTAT_WAIT
);
417 clear_bit(vcpu
->vcpu_id
, vcpu
->kvm
->arch
.idle_mask
);
420 static void __reset_intercept_indicators(struct kvm_vcpu
*vcpu
)
422 kvm_s390_clear_cpuflags(vcpu
, CPUSTAT_IO_INT
| CPUSTAT_EXT_INT
|
424 vcpu
->arch
.sie_block
->lctl
= 0x0000;
425 vcpu
->arch
.sie_block
->ictl
&= ~(ICTL_LPSW
| ICTL_STCTL
| ICTL_PINT
);
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
);
434 static void set_intercept_indicators_io(struct kvm_vcpu
*vcpu
)
436 if (!(pending_irqs_no_gisa(vcpu
) & IRQ_PEND_IO_MASK
))
438 if (psw_ioint_disabled(vcpu
))
439 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_IO_INT
);
441 vcpu
->arch
.sie_block
->lctl
|= LCTL_CR6
;
444 static void set_intercept_indicators_ext(struct kvm_vcpu
*vcpu
)
446 if (!(pending_irqs_no_gisa(vcpu
) & IRQ_PEND_EXT_MASK
))
448 if (psw_extint_disabled(vcpu
))
449 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_EXT_INT
);
451 vcpu
->arch
.sie_block
->lctl
|= LCTL_CR0
;
454 static void set_intercept_indicators_mchk(struct kvm_vcpu
*vcpu
)
456 if (!(pending_irqs_no_gisa(vcpu
) & IRQ_PEND_MCHK_MASK
))
458 if (psw_mchk_disabled(vcpu
))
459 vcpu
->arch
.sie_block
->ictl
|= ICTL_LPSW
;
461 vcpu
->arch
.sie_block
->lctl
|= LCTL_CR14
;
464 static void set_intercept_indicators_stop(struct kvm_vcpu
*vcpu
)
466 if (kvm_s390_is_stop_irq_pending(vcpu
))
467 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_STOP_INT
);
470 /* Set interception request for non-deliverable interrupts */
471 static void set_intercept_indicators(struct kvm_vcpu
*vcpu
)
473 set_intercept_indicators_io(vcpu
);
474 set_intercept_indicators_ext(vcpu
);
475 set_intercept_indicators_mchk(vcpu
);
476 set_intercept_indicators_stop(vcpu
);
479 static int __must_check
__deliver_cpu_timer(struct kvm_vcpu
*vcpu
)
481 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
484 vcpu
->stat
.deliver_cputm
++;
485 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, KVM_S390_INT_CPU_TIMER
,
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;
499 static int __must_check
__deliver_ckc(struct kvm_vcpu
*vcpu
)
501 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
504 vcpu
->stat
.deliver_ckc
++;
505 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, KVM_S390_INT_CLOCK_COMP
,
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;
519 static int __must_check
__deliver_pfault_init(struct kvm_vcpu
*vcpu
)
521 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
522 struct kvm_s390_ext_info ext
;
525 spin_lock(&li
->lock
);
527 clear_bit(IRQ_PEND_PFAULT_INIT
, &li
->pending_irqs
);
528 li
->irq
.ext
.ext_params2
= 0;
529 spin_unlock(&li
->lock
);
531 VCPU_EVENT(vcpu
, 4, "deliver: pfault init token 0x%llx",
533 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
534 KVM_S390_INT_PFAULT_INIT
,
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;
547 static int __write_machine_check(struct kvm_vcpu
*vcpu
,
548 struct kvm_s390_mchk_info
*mchk
)
550 unsigned long ext_sa_addr
;
552 freg_t fprs
[NUM_FPRS
];
556 mci
.val
= mchk
->mcic
;
557 /* take care of lazy register loading */
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
);
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)) {
572 ext_sa_addr
&= ~0x3ffUL
;
575 ext_sa_addr
&= ~0x7ffUL
;
578 ext_sa_addr
&= ~0xfffUL
;
585 ext_sa_addr
&= ~0x3ffUL
;
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
,
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))
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
);
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);
617 rc
|= write_guest_lc(vcpu
, __LC_FPREGS_SAVE_AREA
,
618 vcpu
->run
->s
.regs
.fprs
, 128);
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);
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;
645 static int __must_check
__deliver_machine_check(struct kvm_vcpu
*vcpu
)
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
= {};
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
)) {
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
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
));
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.
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
));
681 spin_unlock(&li
->lock
);
682 spin_unlock(&fi
->lock
);
685 VCPU_EVENT(vcpu
, 3, "deliver: machine check mcic 0x%llx",
687 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
689 mchk
.cr14
, mchk
.mcic
);
690 vcpu
->stat
.deliver_machine_check
++;
691 rc
= __write_machine_check(vcpu
, &mchk
);
696 static int __must_check
__deliver_restart(struct kvm_vcpu
*vcpu
)
698 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
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);
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;
714 static int __must_check
__deliver_set_prefix(struct kvm_vcpu
*vcpu
)
716 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
717 struct kvm_s390_prefix_info prefix
;
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
);
725 vcpu
->stat
.deliver_prefix_signal
++;
726 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
727 KVM_S390_SIGP_SET_PREFIX
,
730 kvm_s390_set_prefix(vcpu
, prefix
.address
);
734 static int __must_check
__deliver_emergency_signal(struct kvm_vcpu
*vcpu
)
736 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
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
);
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
,
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;
762 static int __must_check
__deliver_external_call(struct kvm_vcpu
*vcpu
)
764 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
765 struct kvm_s390_extcall_info extcall
;
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
);
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
,
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
,
787 return rc
? -EFAULT
: 0;
790 static int __must_check
__deliver_prog(struct kvm_vcpu
*vcpu
)
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;
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
);
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
,
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
:
822 case PGM_SPACE_SWITCH
:
823 rc
= put_guest_lc(vcpu
, pgm_info
.trans_exc_code
,
824 (u64
*)__LC_TRANS_EXC_CODE
);
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
);
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
);
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
);
856 case PGM_VECTOR_PROCESSING
:
858 rc
= put_guest_lc(vcpu
, pgm_info
.data_exc_code
,
859 (u32
*)__LC_DATA_EXC_CODE
);
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
);
868 case PGM_STACK_EMPTY
:
869 case PGM_STACK_SPECIFICATION
:
871 case PGM_STACK_OPERATION
:
872 case PGM_TRACE_TABEL
:
873 case PGM_CRYPTO_OPERATION
:
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
);
889 if (nullifying
&& !(pgm_info
.flags
& KVM_S390_PGM_FLAGS_NO_REWIND
))
890 kvm_s390_rewind_psw(vcpu
, ilen
);
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;
905 static int __must_check
__deliver_service(struct kvm_vcpu
*vcpu
)
907 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
908 struct kvm_s390_ext_info ext
;
911 spin_lock(&fi
->lock
);
912 if (!(test_bit(IRQ_PEND_EXT_SERVICE
, &fi
->pending_irqs
))) {
913 spin_unlock(&fi
->lock
);
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
);
921 VCPU_EVENT(vcpu
, 4, "deliver: sclp parameter 0x%x",
923 vcpu
->stat
.deliver_service_signal
++;
924 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, KVM_S390_INT_SERVICE
,
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
);
936 return rc
? -EFAULT
: 0;
939 static int __must_check
__deliver_pfault_done(struct kvm_vcpu
*vcpu
)
941 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
942 struct kvm_s390_interrupt_info
*inti
;
945 spin_lock(&fi
->lock
);
946 inti
= list_first_entry_or_null(&fi
->lists
[FIRQ_LIST_PFAULT
],
947 struct kvm_s390_interrupt_info
,
950 list_del(&inti
->list
);
951 fi
->counters
[FIRQ_CNTR_PFAULT
] -= 1;
953 if (list_empty(&fi
->lists
[FIRQ_LIST_PFAULT
]))
954 clear_bit(IRQ_PEND_PFAULT_DONE
, &fi
->pending_irqs
);
955 spin_unlock(&fi
->lock
);
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
);
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
,
971 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
,
972 &vcpu
->arch
.sie_block
->gpsw
,
974 rc
|= put_guest_lc(vcpu
, inti
->ext
.ext_params2
,
975 (u64
*)__LC_EXT_PARAMS2
);
978 return rc
? -EFAULT
: 0;
981 static int __must_check
__deliver_virtio(struct kvm_vcpu
*vcpu
)
983 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
984 struct kvm_s390_interrupt_info
*inti
;
987 spin_lock(&fi
->lock
);
988 inti
= list_first_entry_or_null(&fi
->lists
[FIRQ_LIST_VIRTIO
],
989 struct kvm_s390_interrupt_info
,
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
,
998 inti
->ext
.ext_params
,
999 inti
->ext
.ext_params2
);
1000 list_del(&inti
->list
);
1001 fi
->counters
[FIRQ_CNTR_VIRTIO
] -= 1;
1003 if (list_empty(&fi
->lists
[FIRQ_LIST_VIRTIO
]))
1004 clear_bit(IRQ_PEND_VIRTIO
, &fi
->pending_irqs
);
1005 spin_unlock(&fi
->lock
);
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
,
1015 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
,
1016 &vcpu
->arch
.sie_block
->gpsw
,
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
);
1024 return rc
? -EFAULT
: 0;
1027 static int __do_deliver_io(struct kvm_vcpu
*vcpu
, struct kvm_s390_io_info
*io
)
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
,
1038 rc
|= read_guest_lc(vcpu
, __LC_IO_NEW_PSW
,
1039 &vcpu
->arch
.sie_block
->gpsw
,
1041 return rc
? -EFAULT
: 0;
1044 static int __must_check
__deliver_io(struct kvm_vcpu
*vcpu
,
1045 unsigned long irq_type
)
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
;
1055 fi
= &vcpu
->kvm
->arch
.float_int
;
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
,
1064 if (inti
->type
& KVM_S390_INT_IO_AI_MASK
)
1065 VCPU_EVENT(vcpu
, 4, "%s", "deliver: I/O (AI)");
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
);
1072 vcpu
->stat
.deliver_io
++;
1073 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
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;
1082 if (list_empty(isc_list
))
1083 clear_bit(irq_type
, &fi
->pending_irqs
);
1084 spin_unlock(&fi
->lock
);
1087 rc
= __do_deliver_io(vcpu
, &(inti
->io
));
1092 if (gi
->origin
&& gisa_tac_ipm_gisc(gi
->origin
, isc
)) {
1094 * in case an adapter interrupt was not delivered
1095 * in SIE context KVM will handle the delivery
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) |
1105 ((__u64
)io
.io_int_parm
<< 32) |
1107 rc
= __do_deliver_io(vcpu
, &io
);
1113 /* Check whether an external call is pending (deliverable or not) */
1114 int kvm_s390_ext_call_pending(struct kvm_vcpu
*vcpu
)
1116 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1118 if (!sclp
.has_sigpif
)
1119 return test_bit(IRQ_PEND_EXT_EXTERNAL
, &li
->pending_irqs
);
1121 return sca_ext_call_pending(vcpu
, NULL
);
1124 int kvm_s390_vcpu_has_irq(struct kvm_vcpu
*vcpu
, int exclude_stop
)
1126 if (deliverable_irqs(vcpu
))
1129 if (kvm_cpu_has_pending_timer(vcpu
))
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
))
1138 if (!exclude_stop
&& kvm_s390_is_stop_irq_pending(vcpu
))
1143 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
1145 return ckc_irq_pending(vcpu
) || cpu_timer_irq_pending(vcpu
);
1148 static u64
__calculate_sltime(struct kvm_vcpu
*vcpu
)
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;
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
);
1161 /* already expired */
1164 if (cpu_timer_interrupts_enabled(vcpu
)) {
1165 cputm
= kvm_s390_get_cpu_timer(vcpu
);
1166 /* already expired? */
1169 return min(sltime
, tod_to_ns(cputm
));
1171 } else if (cpu_timer_interrupts_enabled(vcpu
)) {
1172 sltime
= kvm_s390_get_cpu_timer(vcpu
);
1173 /* already expired? */
1180 int kvm_s390_handle_wait(struct kvm_vcpu
*vcpu
)
1182 struct kvm_s390_gisa_interrupt
*gi
= &vcpu
->kvm
->arch
.gisa_int
;
1185 vcpu
->stat
.exit_wait_state
++;
1188 if (kvm_arch_vcpu_runnable(vcpu
))
1191 if (psw_interrupts_disabled(vcpu
)) {
1192 VCPU_EVENT(vcpu
, 3, "%s", "disabled wait");
1193 return -EOPNOTSUPP
; /* disabled wait */
1197 (gisa_get_ipm_or_restore_iam(gi
) &
1198 vcpu
->arch
.sie_block
->gcr
[6] >> 24))
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
);
1208 sltime
= __calculate_sltime(vcpu
);
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
);
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
);
1221 hrtimer_cancel(&vcpu
->arch
.ckc_timer
);
1225 void kvm_s390_vcpu_wakeup(struct kvm_vcpu
*vcpu
)
1227 vcpu
->valid_wakeup
= true;
1228 kvm_vcpu_wake_up(vcpu
);
1231 * The VCPU might not be sleeping but rather executing VSIE. Let's
1232 * kick it, so it leaves the SIE to process the request.
1234 kvm_s390_vsie_kick(vcpu
);
1237 enum hrtimer_restart
kvm_s390_idle_wakeup(struct hrtimer
*timer
)
1239 struct kvm_vcpu
*vcpu
;
1242 vcpu
= container_of(timer
, struct kvm_vcpu
, arch
.ckc_timer
);
1243 sltime
= __calculate_sltime(vcpu
);
1246 * If the monotonic clock runs faster than the tod clock we might be
1247 * woken up too early and have to go back to sleep to avoid deadlocks.
1249 if (sltime
&& hrtimer_forward_now(timer
, ns_to_ktime(sltime
)))
1250 return HRTIMER_RESTART
;
1251 kvm_s390_vcpu_wakeup(vcpu
);
1252 return HRTIMER_NORESTART
;
1255 void kvm_s390_clear_local_irqs(struct kvm_vcpu
*vcpu
)
1257 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1259 spin_lock(&li
->lock
);
1260 li
->pending_irqs
= 0;
1261 bitmap_zero(li
->sigp_emerg_pending
, KVM_MAX_VCPUS
);
1262 memset(&li
->irq
, 0, sizeof(li
->irq
));
1263 spin_unlock(&li
->lock
);
1265 sca_clear_ext_call(vcpu
);
1268 int __must_check
kvm_s390_deliver_pending_interrupts(struct kvm_vcpu
*vcpu
)
1270 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1272 unsigned long irq_type
;
1275 __reset_intercept_indicators(vcpu
);
1277 /* pending ckc conditions might have been invalidated */
1278 clear_bit(IRQ_PEND_EXT_CLOCK_COMP
, &li
->pending_irqs
);
1279 if (ckc_irq_pending(vcpu
))
1280 set_bit(IRQ_PEND_EXT_CLOCK_COMP
, &li
->pending_irqs
);
1282 /* pending cpu timer conditions might have been invalidated */
1283 clear_bit(IRQ_PEND_EXT_CPU_TIMER
, &li
->pending_irqs
);
1284 if (cpu_timer_irq_pending(vcpu
))
1285 set_bit(IRQ_PEND_EXT_CPU_TIMER
, &li
->pending_irqs
);
1287 while ((irqs
= deliverable_irqs(vcpu
)) && !rc
) {
1288 /* bits are in the reverse order of interrupt priority */
1289 irq_type
= find_last_bit(&irqs
, IRQ_PEND_COUNT
);
1291 case IRQ_PEND_IO_ISC_0
:
1292 case IRQ_PEND_IO_ISC_1
:
1293 case IRQ_PEND_IO_ISC_2
:
1294 case IRQ_PEND_IO_ISC_3
:
1295 case IRQ_PEND_IO_ISC_4
:
1296 case IRQ_PEND_IO_ISC_5
:
1297 case IRQ_PEND_IO_ISC_6
:
1298 case IRQ_PEND_IO_ISC_7
:
1299 rc
= __deliver_io(vcpu
, irq_type
);
1301 case IRQ_PEND_MCHK_EX
:
1302 case IRQ_PEND_MCHK_REP
:
1303 rc
= __deliver_machine_check(vcpu
);
1306 rc
= __deliver_prog(vcpu
);
1308 case IRQ_PEND_EXT_EMERGENCY
:
1309 rc
= __deliver_emergency_signal(vcpu
);
1311 case IRQ_PEND_EXT_EXTERNAL
:
1312 rc
= __deliver_external_call(vcpu
);
1314 case IRQ_PEND_EXT_CLOCK_COMP
:
1315 rc
= __deliver_ckc(vcpu
);
1317 case IRQ_PEND_EXT_CPU_TIMER
:
1318 rc
= __deliver_cpu_timer(vcpu
);
1320 case IRQ_PEND_RESTART
:
1321 rc
= __deliver_restart(vcpu
);
1323 case IRQ_PEND_SET_PREFIX
:
1324 rc
= __deliver_set_prefix(vcpu
);
1326 case IRQ_PEND_PFAULT_INIT
:
1327 rc
= __deliver_pfault_init(vcpu
);
1329 case IRQ_PEND_EXT_SERVICE
:
1330 rc
= __deliver_service(vcpu
);
1332 case IRQ_PEND_PFAULT_DONE
:
1333 rc
= __deliver_pfault_done(vcpu
);
1335 case IRQ_PEND_VIRTIO
:
1336 rc
= __deliver_virtio(vcpu
);
1339 WARN_ONCE(1, "Unknown pending irq type %ld", irq_type
);
1340 clear_bit(irq_type
, &li
->pending_irqs
);
1344 set_intercept_indicators(vcpu
);
1349 static int __inject_prog(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1351 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1353 vcpu
->stat
.inject_program
++;
1354 VCPU_EVENT(vcpu
, 3, "inject: program irq code 0x%x", irq
->u
.pgm
.code
);
1355 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_PROGRAM_INT
,
1356 irq
->u
.pgm
.code
, 0);
1358 if (!(irq
->u
.pgm
.flags
& KVM_S390_PGM_FLAGS_ILC_VALID
)) {
1359 /* auto detection if no valid ILC was given */
1360 irq
->u
.pgm
.flags
&= ~KVM_S390_PGM_FLAGS_ILC_MASK
;
1361 irq
->u
.pgm
.flags
|= kvm_s390_get_ilen(vcpu
);
1362 irq
->u
.pgm
.flags
|= KVM_S390_PGM_FLAGS_ILC_VALID
;
1365 if (irq
->u
.pgm
.code
== PGM_PER
) {
1366 li
->irq
.pgm
.code
|= PGM_PER
;
1367 li
->irq
.pgm
.flags
= irq
->u
.pgm
.flags
;
1368 /* only modify PER related information */
1369 li
->irq
.pgm
.per_address
= irq
->u
.pgm
.per_address
;
1370 li
->irq
.pgm
.per_code
= irq
->u
.pgm
.per_code
;
1371 li
->irq
.pgm
.per_atmid
= irq
->u
.pgm
.per_atmid
;
1372 li
->irq
.pgm
.per_access_id
= irq
->u
.pgm
.per_access_id
;
1373 } else if (!(irq
->u
.pgm
.code
& PGM_PER
)) {
1374 li
->irq
.pgm
.code
= (li
->irq
.pgm
.code
& PGM_PER
) |
1376 li
->irq
.pgm
.flags
= irq
->u
.pgm
.flags
;
1377 /* only modify non-PER information */
1378 li
->irq
.pgm
.trans_exc_code
= irq
->u
.pgm
.trans_exc_code
;
1379 li
->irq
.pgm
.mon_code
= irq
->u
.pgm
.mon_code
;
1380 li
->irq
.pgm
.data_exc_code
= irq
->u
.pgm
.data_exc_code
;
1381 li
->irq
.pgm
.mon_class_nr
= irq
->u
.pgm
.mon_class_nr
;
1382 li
->irq
.pgm
.exc_access_id
= irq
->u
.pgm
.exc_access_id
;
1383 li
->irq
.pgm
.op_access_id
= irq
->u
.pgm
.op_access_id
;
1385 li
->irq
.pgm
= irq
->u
.pgm
;
1387 set_bit(IRQ_PEND_PROG
, &li
->pending_irqs
);
1391 static int __inject_pfault_init(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1393 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1395 vcpu
->stat
.inject_pfault_init
++;
1396 VCPU_EVENT(vcpu
, 4, "inject: pfault init parameter block at 0x%llx",
1397 irq
->u
.ext
.ext_params2
);
1398 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_INT_PFAULT_INIT
,
1399 irq
->u
.ext
.ext_params
,
1400 irq
->u
.ext
.ext_params2
);
1402 li
->irq
.ext
= irq
->u
.ext
;
1403 set_bit(IRQ_PEND_PFAULT_INIT
, &li
->pending_irqs
);
1404 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_EXT_INT
);
1408 static int __inject_extcall(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1410 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1411 struct kvm_s390_extcall_info
*extcall
= &li
->irq
.extcall
;
1412 uint16_t src_id
= irq
->u
.extcall
.code
;
1414 vcpu
->stat
.inject_external_call
++;
1415 VCPU_EVENT(vcpu
, 4, "inject: external call source-cpu:%u",
1417 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_INT_EXTERNAL_CALL
,
1420 /* sending vcpu invalid */
1421 if (kvm_get_vcpu_by_id(vcpu
->kvm
, src_id
) == NULL
)
1424 if (sclp
.has_sigpif
)
1425 return sca_inject_ext_call(vcpu
, src_id
);
1427 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL
, &li
->pending_irqs
))
1429 *extcall
= irq
->u
.extcall
;
1430 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_EXT_INT
);
1434 static int __inject_set_prefix(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1436 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1437 struct kvm_s390_prefix_info
*prefix
= &li
->irq
.prefix
;
1439 vcpu
->stat
.inject_set_prefix
++;
1440 VCPU_EVENT(vcpu
, 3, "inject: set prefix to %x",
1441 irq
->u
.prefix
.address
);
1442 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_SIGP_SET_PREFIX
,
1443 irq
->u
.prefix
.address
, 0);
1445 if (!is_vcpu_stopped(vcpu
))
1448 *prefix
= irq
->u
.prefix
;
1449 set_bit(IRQ_PEND_SET_PREFIX
, &li
->pending_irqs
);
1453 #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
1454 static int __inject_sigp_stop(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1456 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1457 struct kvm_s390_stop_info
*stop
= &li
->irq
.stop
;
1460 vcpu
->stat
.inject_stop_signal
++;
1461 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_SIGP_STOP
, 0, 0);
1463 if (irq
->u
.stop
.flags
& ~KVM_S390_STOP_SUPP_FLAGS
)
1466 if (is_vcpu_stopped(vcpu
)) {
1467 if (irq
->u
.stop
.flags
& KVM_S390_STOP_FLAG_STORE_STATUS
)
1468 rc
= kvm_s390_store_status_unloaded(vcpu
,
1469 KVM_S390_STORE_STATUS_NOADDR
);
1473 if (test_and_set_bit(IRQ_PEND_SIGP_STOP
, &li
->pending_irqs
))
1475 stop
->flags
= irq
->u
.stop
.flags
;
1476 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_STOP_INT
);
1480 static int __inject_sigp_restart(struct kvm_vcpu
*vcpu
)
1482 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1484 vcpu
->stat
.inject_restart
++;
1485 VCPU_EVENT(vcpu
, 3, "%s", "inject: restart int");
1486 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_RESTART
, 0, 0);
1488 set_bit(IRQ_PEND_RESTART
, &li
->pending_irqs
);
1492 static int __inject_sigp_emergency(struct kvm_vcpu
*vcpu
,
1493 struct kvm_s390_irq
*irq
)
1495 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1497 vcpu
->stat
.inject_emergency_signal
++;
1498 VCPU_EVENT(vcpu
, 4, "inject: emergency from cpu %u",
1500 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_INT_EMERGENCY
,
1501 irq
->u
.emerg
.code
, 0);
1503 /* sending vcpu invalid */
1504 if (kvm_get_vcpu_by_id(vcpu
->kvm
, irq
->u
.emerg
.code
) == NULL
)
1507 set_bit(irq
->u
.emerg
.code
, li
->sigp_emerg_pending
);
1508 set_bit(IRQ_PEND_EXT_EMERGENCY
, &li
->pending_irqs
);
1509 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_EXT_INT
);
1513 static int __inject_mchk(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1515 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1516 struct kvm_s390_mchk_info
*mchk
= &li
->irq
.mchk
;
1518 vcpu
->stat
.inject_mchk
++;
1519 VCPU_EVENT(vcpu
, 3, "inject: machine check mcic 0x%llx",
1521 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_MCHK
, 0,
1525 * Because repressible machine checks can be indicated along with
1526 * exigent machine checks (PoP, Chapter 11, Interruption action)
1527 * we need to combine cr14, mcic and external damage code.
1528 * Failing storage address and the logout area should not be or'ed
1529 * together, we just indicate the last occurrence of the corresponding
1532 mchk
->cr14
|= irq
->u
.mchk
.cr14
;
1533 mchk
->mcic
|= irq
->u
.mchk
.mcic
;
1534 mchk
->ext_damage_code
|= irq
->u
.mchk
.ext_damage_code
;
1535 mchk
->failing_storage_address
= irq
->u
.mchk
.failing_storage_address
;
1536 memcpy(&mchk
->fixed_logout
, &irq
->u
.mchk
.fixed_logout
,
1537 sizeof(mchk
->fixed_logout
));
1538 if (mchk
->mcic
& MCHK_EX_MASK
)
1539 set_bit(IRQ_PEND_MCHK_EX
, &li
->pending_irqs
);
1540 else if (mchk
->mcic
& MCHK_REP_MASK
)
1541 set_bit(IRQ_PEND_MCHK_REP
, &li
->pending_irqs
);
1545 static int __inject_ckc(struct kvm_vcpu
*vcpu
)
1547 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1549 vcpu
->stat
.inject_ckc
++;
1550 VCPU_EVENT(vcpu
, 3, "%s", "inject: clock comparator external");
1551 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_INT_CLOCK_COMP
,
1554 set_bit(IRQ_PEND_EXT_CLOCK_COMP
, &li
->pending_irqs
);
1555 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_EXT_INT
);
1559 static int __inject_cpu_timer(struct kvm_vcpu
*vcpu
)
1561 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1563 vcpu
->stat
.inject_cputm
++;
1564 VCPU_EVENT(vcpu
, 3, "%s", "inject: cpu timer external");
1565 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_INT_CPU_TIMER
,
1568 set_bit(IRQ_PEND_EXT_CPU_TIMER
, &li
->pending_irqs
);
1569 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_EXT_INT
);
1573 static struct kvm_s390_interrupt_info
*get_io_int(struct kvm
*kvm
,
1576 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
1577 struct list_head
*isc_list
= &fi
->lists
[FIRQ_LIST_IO_ISC_0
+ isc
];
1578 struct kvm_s390_interrupt_info
*iter
;
1579 u16 id
= (schid
& 0xffff0000U
) >> 16;
1580 u16 nr
= schid
& 0x0000ffffU
;
1582 spin_lock(&fi
->lock
);
1583 list_for_each_entry(iter
, isc_list
, list
) {
1584 if (schid
&& (id
!= iter
->io
.subchannel_id
||
1585 nr
!= iter
->io
.subchannel_nr
))
1587 /* found an appropriate entry */
1588 list_del_init(&iter
->list
);
1589 fi
->counters
[FIRQ_CNTR_IO
] -= 1;
1590 if (list_empty(isc_list
))
1591 clear_bit(isc_to_irq_type(isc
), &fi
->pending_irqs
);
1592 spin_unlock(&fi
->lock
);
1595 spin_unlock(&fi
->lock
);
1599 static struct kvm_s390_interrupt_info
*get_top_io_int(struct kvm
*kvm
,
1600 u64 isc_mask
, u32 schid
)
1602 struct kvm_s390_interrupt_info
*inti
= NULL
;
1605 for (isc
= 0; isc
<= MAX_ISC
&& !inti
; isc
++) {
1606 if (isc_mask
& isc_to_isc_bits(isc
))
1607 inti
= get_io_int(kvm
, isc
, schid
);
1612 static int get_top_gisa_isc(struct kvm
*kvm
, u64 isc_mask
, u32 schid
)
1614 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
1615 unsigned long active_mask
;
1623 active_mask
= (isc_mask
& gisa_get_ipm(gi
->origin
) << 24) << 32;
1624 while (active_mask
) {
1625 isc
= __fls(active_mask
) ^ (BITS_PER_LONG
- 1);
1626 if (gisa_tac_ipm_gisc(gi
->origin
, isc
))
1628 clear_bit_inv(isc
, &active_mask
);
1635 * Dequeue and return an I/O interrupt matching any of the interruption
1636 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1637 * Take into account the interrupts pending in the interrupt list and in GISA.
1639 * Note that for a guest that does not enable I/O interrupts
1640 * but relies on TPI, a flood of classic interrupts may starve
1641 * out adapter interrupts on the same isc. Linux does not do
1642 * that, and it is possible to work around the issue by configuring
1643 * different iscs for classic and adapter interrupts in the guest,
1644 * but we may want to revisit this in the future.
1646 struct kvm_s390_interrupt_info
*kvm_s390_get_io_int(struct kvm
*kvm
,
1647 u64 isc_mask
, u32 schid
)
1649 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
1650 struct kvm_s390_interrupt_info
*inti
, *tmp_inti
;
1653 inti
= get_top_io_int(kvm
, isc_mask
, schid
);
1655 isc
= get_top_gisa_isc(kvm
, isc_mask
, schid
);
1661 /* AI in GISA but no classical IO int */
1664 /* both types of interrupts present */
1665 if (int_word_to_isc(inti
->io
.io_int_word
) <= isc
) {
1666 /* classical IO int with higher priority */
1667 gisa_set_ipm_gisc(gi
->origin
, isc
);
1671 tmp_inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
1673 tmp_inti
->type
= KVM_S390_INT_IO(1, 0, 0, 0);
1674 tmp_inti
->io
.io_int_word
= isc_to_int_word(isc
);
1676 kvm_s390_reinject_io_int(kvm
, inti
);
1679 gisa_set_ipm_gisc(gi
->origin
, isc
);
1684 #define SCCB_MASK 0xFFFFFFF8
1685 #define SCCB_EVENT_PENDING 0x3
1687 static int __inject_service(struct kvm
*kvm
,
1688 struct kvm_s390_interrupt_info
*inti
)
1690 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
1692 kvm
->stat
.inject_service_signal
++;
1693 spin_lock(&fi
->lock
);
1694 fi
->srv_signal
.ext_params
|= inti
->ext
.ext_params
& SCCB_EVENT_PENDING
;
1696 * Early versions of the QEMU s390 bios will inject several
1697 * service interrupts after another without handling a
1698 * condition code indicating busy.
1699 * We will silently ignore those superfluous sccb values.
1700 * A future version of QEMU will take care of serialization
1703 if (fi
->srv_signal
.ext_params
& SCCB_MASK
)
1705 fi
->srv_signal
.ext_params
|= inti
->ext
.ext_params
& SCCB_MASK
;
1706 set_bit(IRQ_PEND_EXT_SERVICE
, &fi
->pending_irqs
);
1708 spin_unlock(&fi
->lock
);
1713 static int __inject_virtio(struct kvm
*kvm
,
1714 struct kvm_s390_interrupt_info
*inti
)
1716 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
1718 kvm
->stat
.inject_virtio
++;
1719 spin_lock(&fi
->lock
);
1720 if (fi
->counters
[FIRQ_CNTR_VIRTIO
] >= KVM_S390_MAX_VIRTIO_IRQS
) {
1721 spin_unlock(&fi
->lock
);
1724 fi
->counters
[FIRQ_CNTR_VIRTIO
] += 1;
1725 list_add_tail(&inti
->list
, &fi
->lists
[FIRQ_LIST_VIRTIO
]);
1726 set_bit(IRQ_PEND_VIRTIO
, &fi
->pending_irqs
);
1727 spin_unlock(&fi
->lock
);
1731 static int __inject_pfault_done(struct kvm
*kvm
,
1732 struct kvm_s390_interrupt_info
*inti
)
1734 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
1736 kvm
->stat
.inject_pfault_done
++;
1737 spin_lock(&fi
->lock
);
1738 if (fi
->counters
[FIRQ_CNTR_PFAULT
] >=
1739 (ASYNC_PF_PER_VCPU
* KVM_MAX_VCPUS
)) {
1740 spin_unlock(&fi
->lock
);
1743 fi
->counters
[FIRQ_CNTR_PFAULT
] += 1;
1744 list_add_tail(&inti
->list
, &fi
->lists
[FIRQ_LIST_PFAULT
]);
1745 set_bit(IRQ_PEND_PFAULT_DONE
, &fi
->pending_irqs
);
1746 spin_unlock(&fi
->lock
);
1750 #define CR_PENDING_SUBCLASS 28
1751 static int __inject_float_mchk(struct kvm
*kvm
,
1752 struct kvm_s390_interrupt_info
*inti
)
1754 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
1756 kvm
->stat
.inject_float_mchk
++;
1757 spin_lock(&fi
->lock
);
1758 fi
->mchk
.cr14
|= inti
->mchk
.cr14
& (1UL << CR_PENDING_SUBCLASS
);
1759 fi
->mchk
.mcic
|= inti
->mchk
.mcic
;
1760 set_bit(IRQ_PEND_MCHK_REP
, &fi
->pending_irqs
);
1761 spin_unlock(&fi
->lock
);
1766 static int __inject_io(struct kvm
*kvm
, struct kvm_s390_interrupt_info
*inti
)
1768 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
1769 struct kvm_s390_float_interrupt
*fi
;
1770 struct list_head
*list
;
1773 kvm
->stat
.inject_io
++;
1774 isc
= int_word_to_isc(inti
->io
.io_int_word
);
1776 if (gi
->origin
&& inti
->type
& KVM_S390_INT_IO_AI_MASK
) {
1777 VM_EVENT(kvm
, 4, "%s isc %1u", "inject: I/O (AI/gisa)", isc
);
1778 gisa_set_ipm_gisc(gi
->origin
, isc
);
1783 fi
= &kvm
->arch
.float_int
;
1784 spin_lock(&fi
->lock
);
1785 if (fi
->counters
[FIRQ_CNTR_IO
] >= KVM_S390_MAX_FLOAT_IRQS
) {
1786 spin_unlock(&fi
->lock
);
1789 fi
->counters
[FIRQ_CNTR_IO
] += 1;
1791 if (inti
->type
& KVM_S390_INT_IO_AI_MASK
)
1792 VM_EVENT(kvm
, 4, "%s", "inject: I/O (AI)");
1794 VM_EVENT(kvm
, 4, "inject: I/O %x ss %x schid %04x",
1795 inti
->io
.subchannel_id
>> 8,
1796 inti
->io
.subchannel_id
>> 1 & 0x3,
1797 inti
->io
.subchannel_nr
);
1798 list
= &fi
->lists
[FIRQ_LIST_IO_ISC_0
+ isc
];
1799 list_add_tail(&inti
->list
, list
);
1800 set_bit(isc_to_irq_type(isc
), &fi
->pending_irqs
);
1801 spin_unlock(&fi
->lock
);
1806 * Find a destination VCPU for a floating irq and kick it.
1808 static void __floating_irq_kick(struct kvm
*kvm
, u64 type
)
1810 struct kvm_vcpu
*dst_vcpu
;
1811 int sigcpu
, online_vcpus
, nr_tries
= 0;
1813 online_vcpus
= atomic_read(&kvm
->online_vcpus
);
1817 /* find idle VCPUs first, then round robin */
1818 sigcpu
= find_first_bit(kvm
->arch
.idle_mask
, online_vcpus
);
1819 if (sigcpu
== online_vcpus
) {
1821 sigcpu
= kvm
->arch
.float_int
.next_rr_cpu
++;
1822 kvm
->arch
.float_int
.next_rr_cpu
%= online_vcpus
;
1823 /* avoid endless loops if all vcpus are stopped */
1824 if (nr_tries
++ >= online_vcpus
)
1826 } while (is_vcpu_stopped(kvm_get_vcpu(kvm
, sigcpu
)));
1828 dst_vcpu
= kvm_get_vcpu(kvm
, sigcpu
);
1830 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1833 kvm_s390_set_cpuflags(dst_vcpu
, CPUSTAT_STOP_INT
);
1835 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1836 if (!(type
& KVM_S390_INT_IO_AI_MASK
&&
1837 kvm
->arch
.gisa_int
.origin
))
1838 kvm_s390_set_cpuflags(dst_vcpu
, CPUSTAT_IO_INT
);
1841 kvm_s390_set_cpuflags(dst_vcpu
, CPUSTAT_EXT_INT
);
1844 kvm_s390_vcpu_wakeup(dst_vcpu
);
1847 static int __inject_vm(struct kvm
*kvm
, struct kvm_s390_interrupt_info
*inti
)
1849 u64 type
= READ_ONCE(inti
->type
);
1854 rc
= __inject_float_mchk(kvm
, inti
);
1856 case KVM_S390_INT_VIRTIO
:
1857 rc
= __inject_virtio(kvm
, inti
);
1859 case KVM_S390_INT_SERVICE
:
1860 rc
= __inject_service(kvm
, inti
);
1862 case KVM_S390_INT_PFAULT_DONE
:
1863 rc
= __inject_pfault_done(kvm
, inti
);
1865 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1866 rc
= __inject_io(kvm
, inti
);
1874 __floating_irq_kick(kvm
, type
);
1878 int kvm_s390_inject_vm(struct kvm
*kvm
,
1879 struct kvm_s390_interrupt
*s390int
)
1881 struct kvm_s390_interrupt_info
*inti
;
1884 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
1888 inti
->type
= s390int
->type
;
1889 switch (inti
->type
) {
1890 case KVM_S390_INT_VIRTIO
:
1891 VM_EVENT(kvm
, 5, "inject: virtio parm:%x,parm64:%llx",
1892 s390int
->parm
, s390int
->parm64
);
1893 inti
->ext
.ext_params
= s390int
->parm
;
1894 inti
->ext
.ext_params2
= s390int
->parm64
;
1896 case KVM_S390_INT_SERVICE
:
1897 VM_EVENT(kvm
, 4, "inject: sclp parm:%x", s390int
->parm
);
1898 inti
->ext
.ext_params
= s390int
->parm
;
1900 case KVM_S390_INT_PFAULT_DONE
:
1901 inti
->ext
.ext_params2
= s390int
->parm64
;
1904 VM_EVENT(kvm
, 3, "inject: machine check mcic 0x%llx",
1906 inti
->mchk
.cr14
= s390int
->parm
; /* upper bits are not used */
1907 inti
->mchk
.mcic
= s390int
->parm64
;
1909 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1910 inti
->io
.subchannel_id
= s390int
->parm
>> 16;
1911 inti
->io
.subchannel_nr
= s390int
->parm
& 0x0000ffffu
;
1912 inti
->io
.io_int_parm
= s390int
->parm64
>> 32;
1913 inti
->io
.io_int_word
= s390int
->parm64
& 0x00000000ffffffffull
;
1919 trace_kvm_s390_inject_vm(s390int
->type
, s390int
->parm
, s390int
->parm64
,
1922 rc
= __inject_vm(kvm
, inti
);
1928 int kvm_s390_reinject_io_int(struct kvm
*kvm
,
1929 struct kvm_s390_interrupt_info
*inti
)
1931 return __inject_vm(kvm
, inti
);
1934 int s390int_to_s390irq(struct kvm_s390_interrupt
*s390int
,
1935 struct kvm_s390_irq
*irq
)
1937 irq
->type
= s390int
->type
;
1938 switch (irq
->type
) {
1939 case KVM_S390_PROGRAM_INT
:
1940 if (s390int
->parm
& 0xffff0000)
1942 irq
->u
.pgm
.code
= s390int
->parm
;
1944 case KVM_S390_SIGP_SET_PREFIX
:
1945 irq
->u
.prefix
.address
= s390int
->parm
;
1947 case KVM_S390_SIGP_STOP
:
1948 irq
->u
.stop
.flags
= s390int
->parm
;
1950 case KVM_S390_INT_EXTERNAL_CALL
:
1951 if (s390int
->parm
& 0xffff0000)
1953 irq
->u
.extcall
.code
= s390int
->parm
;
1955 case KVM_S390_INT_EMERGENCY
:
1956 if (s390int
->parm
& 0xffff0000)
1958 irq
->u
.emerg
.code
= s390int
->parm
;
1961 irq
->u
.mchk
.mcic
= s390int
->parm64
;
1963 case KVM_S390_INT_PFAULT_INIT
:
1964 irq
->u
.ext
.ext_params
= s390int
->parm
;
1965 irq
->u
.ext
.ext_params2
= s390int
->parm64
;
1967 case KVM_S390_RESTART
:
1968 case KVM_S390_INT_CLOCK_COMP
:
1969 case KVM_S390_INT_CPU_TIMER
:
1977 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu
*vcpu
)
1979 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1981 return test_bit(IRQ_PEND_SIGP_STOP
, &li
->pending_irqs
);
1984 void kvm_s390_clear_stop_irq(struct kvm_vcpu
*vcpu
)
1986 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1988 spin_lock(&li
->lock
);
1989 li
->irq
.stop
.flags
= 0;
1990 clear_bit(IRQ_PEND_SIGP_STOP
, &li
->pending_irqs
);
1991 spin_unlock(&li
->lock
);
1994 static int do_inject_vcpu(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1998 switch (irq
->type
) {
1999 case KVM_S390_PROGRAM_INT
:
2000 rc
= __inject_prog(vcpu
, irq
);
2002 case KVM_S390_SIGP_SET_PREFIX
:
2003 rc
= __inject_set_prefix(vcpu
, irq
);
2005 case KVM_S390_SIGP_STOP
:
2006 rc
= __inject_sigp_stop(vcpu
, irq
);
2008 case KVM_S390_RESTART
:
2009 rc
= __inject_sigp_restart(vcpu
);
2011 case KVM_S390_INT_CLOCK_COMP
:
2012 rc
= __inject_ckc(vcpu
);
2014 case KVM_S390_INT_CPU_TIMER
:
2015 rc
= __inject_cpu_timer(vcpu
);
2017 case KVM_S390_INT_EXTERNAL_CALL
:
2018 rc
= __inject_extcall(vcpu
, irq
);
2020 case KVM_S390_INT_EMERGENCY
:
2021 rc
= __inject_sigp_emergency(vcpu
, irq
);
2024 rc
= __inject_mchk(vcpu
, irq
);
2026 case KVM_S390_INT_PFAULT_INIT
:
2027 rc
= __inject_pfault_init(vcpu
, irq
);
2029 case KVM_S390_INT_VIRTIO
:
2030 case KVM_S390_INT_SERVICE
:
2031 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
2039 int kvm_s390_inject_vcpu(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
2041 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
2044 spin_lock(&li
->lock
);
2045 rc
= do_inject_vcpu(vcpu
, irq
);
2046 spin_unlock(&li
->lock
);
2048 kvm_s390_vcpu_wakeup(vcpu
);
2052 static inline void clear_irq_list(struct list_head
*_list
)
2054 struct kvm_s390_interrupt_info
*inti
, *n
;
2056 list_for_each_entry_safe(inti
, n
, _list
, list
) {
2057 list_del(&inti
->list
);
2062 static void inti_to_irq(struct kvm_s390_interrupt_info
*inti
,
2063 struct kvm_s390_irq
*irq
)
2065 irq
->type
= inti
->type
;
2066 switch (inti
->type
) {
2067 case KVM_S390_INT_PFAULT_INIT
:
2068 case KVM_S390_INT_PFAULT_DONE
:
2069 case KVM_S390_INT_VIRTIO
:
2070 irq
->u
.ext
= inti
->ext
;
2072 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
2073 irq
->u
.io
= inti
->io
;
2078 void kvm_s390_clear_float_irqs(struct kvm
*kvm
)
2080 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
2083 spin_lock(&fi
->lock
);
2084 fi
->pending_irqs
= 0;
2085 memset(&fi
->srv_signal
, 0, sizeof(fi
->srv_signal
));
2086 memset(&fi
->mchk
, 0, sizeof(fi
->mchk
));
2087 for (i
= 0; i
< FIRQ_LIST_COUNT
; i
++)
2088 clear_irq_list(&fi
->lists
[i
]);
2089 for (i
= 0; i
< FIRQ_MAX_COUNT
; i
++)
2090 fi
->counters
[i
] = 0;
2091 spin_unlock(&fi
->lock
);
2092 kvm_s390_gisa_clear(kvm
);
2095 static int get_all_floating_irqs(struct kvm
*kvm
, u8 __user
*usrbuf
, u64 len
)
2097 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
2098 struct kvm_s390_interrupt_info
*inti
;
2099 struct kvm_s390_float_interrupt
*fi
;
2100 struct kvm_s390_irq
*buf
;
2101 struct kvm_s390_irq
*irq
;
2107 if (len
> KVM_S390_FLIC_MAX_BUFFER
|| len
== 0)
2111 * We are already using -ENOMEM to signal
2112 * userspace it may retry with a bigger buffer,
2113 * so we need to use something else for this case
2119 max_irqs
= len
/ sizeof(struct kvm_s390_irq
);
2121 if (gi
->origin
&& gisa_get_ipm(gi
->origin
)) {
2122 for (i
= 0; i
<= MAX_ISC
; i
++) {
2123 if (n
== max_irqs
) {
2124 /* signal userspace to try again */
2128 if (gisa_tac_ipm_gisc(gi
->origin
, i
)) {
2129 irq
= (struct kvm_s390_irq
*) &buf
[n
];
2130 irq
->type
= KVM_S390_INT_IO(1, 0, 0, 0);
2131 irq
->u
.io
.io_int_word
= isc_to_int_word(i
);
2136 fi
= &kvm
->arch
.float_int
;
2137 spin_lock(&fi
->lock
);
2138 for (i
= 0; i
< FIRQ_LIST_COUNT
; i
++) {
2139 list_for_each_entry(inti
, &fi
->lists
[i
], list
) {
2140 if (n
== max_irqs
) {
2141 /* signal userspace to try again */
2145 inti_to_irq(inti
, &buf
[n
]);
2149 if (test_bit(IRQ_PEND_EXT_SERVICE
, &fi
->pending_irqs
)) {
2150 if (n
== max_irqs
) {
2151 /* signal userspace to try again */
2155 irq
= (struct kvm_s390_irq
*) &buf
[n
];
2156 irq
->type
= KVM_S390_INT_SERVICE
;
2157 irq
->u
.ext
= fi
->srv_signal
;
2160 if (test_bit(IRQ_PEND_MCHK_REP
, &fi
->pending_irqs
)) {
2161 if (n
== max_irqs
) {
2162 /* signal userspace to try again */
2166 irq
= (struct kvm_s390_irq
*) &buf
[n
];
2167 irq
->type
= KVM_S390_MCHK
;
2168 irq
->u
.mchk
= fi
->mchk
;
2173 spin_unlock(&fi
->lock
);
2175 if (!ret
&& n
> 0) {
2176 if (copy_to_user(usrbuf
, buf
, sizeof(struct kvm_s390_irq
) * n
))
2181 return ret
< 0 ? ret
: n
;
2184 static int flic_ais_mode_get_all(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
2186 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
2187 struct kvm_s390_ais_all ais
;
2189 if (attr
->attr
< sizeof(ais
))
2192 if (!test_kvm_facility(kvm
, 72))
2195 mutex_lock(&fi
->ais_lock
);
2196 ais
.simm
= fi
->simm
;
2197 ais
.nimm
= fi
->nimm
;
2198 mutex_unlock(&fi
->ais_lock
);
2200 if (copy_to_user((void __user
*)attr
->addr
, &ais
, sizeof(ais
)))
2206 static int flic_get_attr(struct kvm_device
*dev
, struct kvm_device_attr
*attr
)
2210 switch (attr
->group
) {
2211 case KVM_DEV_FLIC_GET_ALL_IRQS
:
2212 r
= get_all_floating_irqs(dev
->kvm
, (u8 __user
*) attr
->addr
,
2215 case KVM_DEV_FLIC_AISM_ALL
:
2216 r
= flic_ais_mode_get_all(dev
->kvm
, attr
);
2225 static inline int copy_irq_from_user(struct kvm_s390_interrupt_info
*inti
,
2228 struct kvm_s390_irq __user
*uptr
= (struct kvm_s390_irq __user
*) addr
;
2229 void *target
= NULL
;
2230 void __user
*source
;
2233 if (get_user(inti
->type
, (u64 __user
*)addr
))
2236 switch (inti
->type
) {
2237 case KVM_S390_INT_PFAULT_INIT
:
2238 case KVM_S390_INT_PFAULT_DONE
:
2239 case KVM_S390_INT_VIRTIO
:
2240 case KVM_S390_INT_SERVICE
:
2241 target
= (void *) &inti
->ext
;
2242 source
= &uptr
->u
.ext
;
2243 size
= sizeof(inti
->ext
);
2245 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
2246 target
= (void *) &inti
->io
;
2247 source
= &uptr
->u
.io
;
2248 size
= sizeof(inti
->io
);
2251 target
= (void *) &inti
->mchk
;
2252 source
= &uptr
->u
.mchk
;
2253 size
= sizeof(inti
->mchk
);
2259 if (copy_from_user(target
, source
, size
))
2265 static int enqueue_floating_irq(struct kvm_device
*dev
,
2266 struct kvm_device_attr
*attr
)
2268 struct kvm_s390_interrupt_info
*inti
= NULL
;
2270 int len
= attr
->attr
;
2272 if (len
% sizeof(struct kvm_s390_irq
) != 0)
2274 else if (len
> KVM_S390_FLIC_MAX_BUFFER
)
2277 while (len
>= sizeof(struct kvm_s390_irq
)) {
2278 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
2282 r
= copy_irq_from_user(inti
, attr
->addr
);
2287 r
= __inject_vm(dev
->kvm
, inti
);
2292 len
-= sizeof(struct kvm_s390_irq
);
2293 attr
->addr
+= sizeof(struct kvm_s390_irq
);
2299 static struct s390_io_adapter
*get_io_adapter(struct kvm
*kvm
, unsigned int id
)
2301 if (id
>= MAX_S390_IO_ADAPTERS
)
2303 id
= array_index_nospec(id
, MAX_S390_IO_ADAPTERS
);
2304 return kvm
->arch
.adapters
[id
];
2307 static int register_io_adapter(struct kvm_device
*dev
,
2308 struct kvm_device_attr
*attr
)
2310 struct s390_io_adapter
*adapter
;
2311 struct kvm_s390_io_adapter adapter_info
;
2313 if (copy_from_user(&adapter_info
,
2314 (void __user
*)attr
->addr
, sizeof(adapter_info
)))
2317 if (adapter_info
.id
>= MAX_S390_IO_ADAPTERS
)
2320 adapter_info
.id
= array_index_nospec(adapter_info
.id
,
2321 MAX_S390_IO_ADAPTERS
);
2323 if (dev
->kvm
->arch
.adapters
[adapter_info
.id
] != NULL
)
2326 adapter
= kzalloc(sizeof(*adapter
), GFP_KERNEL
);
2330 INIT_LIST_HEAD(&adapter
->maps
);
2331 init_rwsem(&adapter
->maps_lock
);
2332 atomic_set(&adapter
->nr_maps
, 0);
2333 adapter
->id
= adapter_info
.id
;
2334 adapter
->isc
= adapter_info
.isc
;
2335 adapter
->maskable
= adapter_info
.maskable
;
2336 adapter
->masked
= false;
2337 adapter
->swap
= adapter_info
.swap
;
2338 adapter
->suppressible
= (adapter_info
.flags
) &
2339 KVM_S390_ADAPTER_SUPPRESSIBLE
;
2340 dev
->kvm
->arch
.adapters
[adapter
->id
] = adapter
;
2345 int kvm_s390_mask_adapter(struct kvm
*kvm
, unsigned int id
, bool masked
)
2348 struct s390_io_adapter
*adapter
= get_io_adapter(kvm
, id
);
2350 if (!adapter
|| !adapter
->maskable
)
2352 ret
= adapter
->masked
;
2353 adapter
->masked
= masked
;
2357 static int kvm_s390_adapter_map(struct kvm
*kvm
, unsigned int id
, __u64 addr
)
2359 struct s390_io_adapter
*adapter
= get_io_adapter(kvm
, id
);
2360 struct s390_map_info
*map
;
2363 if (!adapter
|| !addr
)
2366 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
2371 INIT_LIST_HEAD(&map
->list
);
2372 map
->guest_addr
= addr
;
2373 map
->addr
= gmap_translate(kvm
->arch
.gmap
, addr
);
2374 if (map
->addr
== -EFAULT
) {
2378 ret
= get_user_pages_fast(map
->addr
, 1, FOLL_WRITE
, &map
->page
);
2382 down_write(&adapter
->maps_lock
);
2383 if (atomic_inc_return(&adapter
->nr_maps
) < MAX_S390_ADAPTER_MAPS
) {
2384 list_add_tail(&map
->list
, &adapter
->maps
);
2387 put_page(map
->page
);
2390 up_write(&adapter
->maps_lock
);
2397 static int kvm_s390_adapter_unmap(struct kvm
*kvm
, unsigned int id
, __u64 addr
)
2399 struct s390_io_adapter
*adapter
= get_io_adapter(kvm
, id
);
2400 struct s390_map_info
*map
, *tmp
;
2403 if (!adapter
|| !addr
)
2406 down_write(&adapter
->maps_lock
);
2407 list_for_each_entry_safe(map
, tmp
, &adapter
->maps
, list
) {
2408 if (map
->guest_addr
== addr
) {
2410 atomic_dec(&adapter
->nr_maps
);
2411 list_del(&map
->list
);
2412 put_page(map
->page
);
2417 up_write(&adapter
->maps_lock
);
2419 return found
? 0 : -EINVAL
;
2422 void kvm_s390_destroy_adapters(struct kvm
*kvm
)
2425 struct s390_map_info
*map
, *tmp
;
2427 for (i
= 0; i
< MAX_S390_IO_ADAPTERS
; i
++) {
2428 if (!kvm
->arch
.adapters
[i
])
2430 list_for_each_entry_safe(map
, tmp
,
2431 &kvm
->arch
.adapters
[i
]->maps
, list
) {
2432 list_del(&map
->list
);
2433 put_page(map
->page
);
2436 kfree(kvm
->arch
.adapters
[i
]);
2440 static int modify_io_adapter(struct kvm_device
*dev
,
2441 struct kvm_device_attr
*attr
)
2443 struct kvm_s390_io_adapter_req req
;
2444 struct s390_io_adapter
*adapter
;
2447 if (copy_from_user(&req
, (void __user
*)attr
->addr
, sizeof(req
)))
2450 adapter
= get_io_adapter(dev
->kvm
, req
.id
);
2454 case KVM_S390_IO_ADAPTER_MASK
:
2455 ret
= kvm_s390_mask_adapter(dev
->kvm
, req
.id
, req
.mask
);
2459 case KVM_S390_IO_ADAPTER_MAP
:
2460 ret
= kvm_s390_adapter_map(dev
->kvm
, req
.id
, req
.addr
);
2462 case KVM_S390_IO_ADAPTER_UNMAP
:
2463 ret
= kvm_s390_adapter_unmap(dev
->kvm
, req
.id
, req
.addr
);
2472 static int clear_io_irq(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
2475 const u64 isc_mask
= 0xffUL
<< 24; /* all iscs set */
2480 if (attr
->attr
!= sizeof(schid
))
2482 if (copy_from_user(&schid
, (void __user
*) attr
->addr
, sizeof(schid
)))
2486 kfree(kvm_s390_get_io_int(kvm
, isc_mask
, schid
));
2488 * If userspace is conforming to the architecture, we can have at most
2489 * one pending I/O interrupt per subchannel, so this is effectively a
2495 static int modify_ais_mode(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
2497 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
2498 struct kvm_s390_ais_req req
;
2501 if (!test_kvm_facility(kvm
, 72))
2504 if (copy_from_user(&req
, (void __user
*)attr
->addr
, sizeof(req
)))
2507 if (req
.isc
> MAX_ISC
)
2510 trace_kvm_s390_modify_ais_mode(req
.isc
,
2511 (fi
->simm
& AIS_MODE_MASK(req
.isc
)) ?
2512 (fi
->nimm
& AIS_MODE_MASK(req
.isc
)) ?
2513 2 : KVM_S390_AIS_MODE_SINGLE
:
2514 KVM_S390_AIS_MODE_ALL
, req
.mode
);
2516 mutex_lock(&fi
->ais_lock
);
2518 case KVM_S390_AIS_MODE_ALL
:
2519 fi
->simm
&= ~AIS_MODE_MASK(req
.isc
);
2520 fi
->nimm
&= ~AIS_MODE_MASK(req
.isc
);
2522 case KVM_S390_AIS_MODE_SINGLE
:
2523 fi
->simm
|= AIS_MODE_MASK(req
.isc
);
2524 fi
->nimm
&= ~AIS_MODE_MASK(req
.isc
);
2529 mutex_unlock(&fi
->ais_lock
);
2534 static int kvm_s390_inject_airq(struct kvm
*kvm
,
2535 struct s390_io_adapter
*adapter
)
2537 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
2538 struct kvm_s390_interrupt s390int
= {
2539 .type
= KVM_S390_INT_IO(1, 0, 0, 0),
2541 .parm64
= isc_to_int_word(adapter
->isc
),
2545 if (!test_kvm_facility(kvm
, 72) || !adapter
->suppressible
)
2546 return kvm_s390_inject_vm(kvm
, &s390int
);
2548 mutex_lock(&fi
->ais_lock
);
2549 if (fi
->nimm
& AIS_MODE_MASK(adapter
->isc
)) {
2550 trace_kvm_s390_airq_suppressed(adapter
->id
, adapter
->isc
);
2554 ret
= kvm_s390_inject_vm(kvm
, &s390int
);
2555 if (!ret
&& (fi
->simm
& AIS_MODE_MASK(adapter
->isc
))) {
2556 fi
->nimm
|= AIS_MODE_MASK(adapter
->isc
);
2557 trace_kvm_s390_modify_ais_mode(adapter
->isc
,
2558 KVM_S390_AIS_MODE_SINGLE
, 2);
2561 mutex_unlock(&fi
->ais_lock
);
2565 static int flic_inject_airq(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
2567 unsigned int id
= attr
->attr
;
2568 struct s390_io_adapter
*adapter
= get_io_adapter(kvm
, id
);
2573 return kvm_s390_inject_airq(kvm
, adapter
);
2576 static int flic_ais_mode_set_all(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
2578 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
2579 struct kvm_s390_ais_all ais
;
2581 if (!test_kvm_facility(kvm
, 72))
2584 if (copy_from_user(&ais
, (void __user
*)attr
->addr
, sizeof(ais
)))
2587 mutex_lock(&fi
->ais_lock
);
2588 fi
->simm
= ais
.simm
;
2589 fi
->nimm
= ais
.nimm
;
2590 mutex_unlock(&fi
->ais_lock
);
2595 static int flic_set_attr(struct kvm_device
*dev
, struct kvm_device_attr
*attr
)
2599 struct kvm_vcpu
*vcpu
;
2601 switch (attr
->group
) {
2602 case KVM_DEV_FLIC_ENQUEUE
:
2603 r
= enqueue_floating_irq(dev
, attr
);
2605 case KVM_DEV_FLIC_CLEAR_IRQS
:
2606 kvm_s390_clear_float_irqs(dev
->kvm
);
2608 case KVM_DEV_FLIC_APF_ENABLE
:
2609 dev
->kvm
->arch
.gmap
->pfault_enabled
= 1;
2611 case KVM_DEV_FLIC_APF_DISABLE_WAIT
:
2612 dev
->kvm
->arch
.gmap
->pfault_enabled
= 0;
2614 * Make sure no async faults are in transition when
2615 * clearing the queues. So we don't need to worry
2616 * about late coming workers.
2618 synchronize_srcu(&dev
->kvm
->srcu
);
2619 kvm_for_each_vcpu(i
, vcpu
, dev
->kvm
)
2620 kvm_clear_async_pf_completion_queue(vcpu
);
2622 case KVM_DEV_FLIC_ADAPTER_REGISTER
:
2623 r
= register_io_adapter(dev
, attr
);
2625 case KVM_DEV_FLIC_ADAPTER_MODIFY
:
2626 r
= modify_io_adapter(dev
, attr
);
2628 case KVM_DEV_FLIC_CLEAR_IO_IRQ
:
2629 r
= clear_io_irq(dev
->kvm
, attr
);
2631 case KVM_DEV_FLIC_AISM
:
2632 r
= modify_ais_mode(dev
->kvm
, attr
);
2634 case KVM_DEV_FLIC_AIRQ_INJECT
:
2635 r
= flic_inject_airq(dev
->kvm
, attr
);
2637 case KVM_DEV_FLIC_AISM_ALL
:
2638 r
= flic_ais_mode_set_all(dev
->kvm
, attr
);
2647 static int flic_has_attr(struct kvm_device
*dev
,
2648 struct kvm_device_attr
*attr
)
2650 switch (attr
->group
) {
2651 case KVM_DEV_FLIC_GET_ALL_IRQS
:
2652 case KVM_DEV_FLIC_ENQUEUE
:
2653 case KVM_DEV_FLIC_CLEAR_IRQS
:
2654 case KVM_DEV_FLIC_APF_ENABLE
:
2655 case KVM_DEV_FLIC_APF_DISABLE_WAIT
:
2656 case KVM_DEV_FLIC_ADAPTER_REGISTER
:
2657 case KVM_DEV_FLIC_ADAPTER_MODIFY
:
2658 case KVM_DEV_FLIC_CLEAR_IO_IRQ
:
2659 case KVM_DEV_FLIC_AISM
:
2660 case KVM_DEV_FLIC_AIRQ_INJECT
:
2661 case KVM_DEV_FLIC_AISM_ALL
:
2667 static int flic_create(struct kvm_device
*dev
, u32 type
)
2671 if (dev
->kvm
->arch
.flic
)
2673 dev
->kvm
->arch
.flic
= dev
;
2677 static void flic_destroy(struct kvm_device
*dev
)
2679 dev
->kvm
->arch
.flic
= NULL
;
2683 /* s390 floating irq controller (flic) */
2684 struct kvm_device_ops kvm_flic_ops
= {
2686 .get_attr
= flic_get_attr
,
2687 .set_attr
= flic_set_attr
,
2688 .has_attr
= flic_has_attr
,
2689 .create
= flic_create
,
2690 .destroy
= flic_destroy
,
2693 static unsigned long get_ind_bit(__u64 addr
, unsigned long bit_nr
, bool swap
)
2697 bit
= bit_nr
+ (addr
% PAGE_SIZE
) * 8;
2699 return swap
? (bit
^ (BITS_PER_LONG
- 1)) : bit
;
2702 static struct s390_map_info
*get_map_info(struct s390_io_adapter
*adapter
,
2705 struct s390_map_info
*map
;
2710 list_for_each_entry(map
, &adapter
->maps
, list
) {
2711 if (map
->guest_addr
== addr
)
2717 static int adapter_indicators_set(struct kvm
*kvm
,
2718 struct s390_io_adapter
*adapter
,
2719 struct kvm_s390_adapter_int
*adapter_int
)
2722 int summary_set
, idx
;
2723 struct s390_map_info
*info
;
2726 info
= get_map_info(adapter
, adapter_int
->ind_addr
);
2729 map
= page_address(info
->page
);
2730 bit
= get_ind_bit(info
->addr
, adapter_int
->ind_offset
, adapter
->swap
);
2732 idx
= srcu_read_lock(&kvm
->srcu
);
2733 mark_page_dirty(kvm
, info
->guest_addr
>> PAGE_SHIFT
);
2734 set_page_dirty_lock(info
->page
);
2735 info
= get_map_info(adapter
, adapter_int
->summary_addr
);
2737 srcu_read_unlock(&kvm
->srcu
, idx
);
2740 map
= page_address(info
->page
);
2741 bit
= get_ind_bit(info
->addr
, adapter_int
->summary_offset
,
2743 summary_set
= test_and_set_bit(bit
, map
);
2744 mark_page_dirty(kvm
, info
->guest_addr
>> PAGE_SHIFT
);
2745 set_page_dirty_lock(info
->page
);
2746 srcu_read_unlock(&kvm
->srcu
, idx
);
2747 return summary_set
? 0 : 1;
2751 * < 0 - not injected due to error
2752 * = 0 - coalesced, summary indicator already active
2753 * > 0 - injected interrupt
2755 static int set_adapter_int(struct kvm_kernel_irq_routing_entry
*e
,
2756 struct kvm
*kvm
, int irq_source_id
, int level
,
2760 struct s390_io_adapter
*adapter
;
2762 /* We're only interested in the 0->1 transition. */
2765 adapter
= get_io_adapter(kvm
, e
->adapter
.adapter_id
);
2768 down_read(&adapter
->maps_lock
);
2769 ret
= adapter_indicators_set(kvm
, adapter
, &e
->adapter
);
2770 up_read(&adapter
->maps_lock
);
2771 if ((ret
> 0) && !adapter
->masked
) {
2772 ret
= kvm_s390_inject_airq(kvm
, adapter
);
2780 * Inject the machine check to the guest.
2782 void kvm_s390_reinject_machine_check(struct kvm_vcpu
*vcpu
,
2783 struct mcck_volatile_info
*mcck_info
)
2785 struct kvm_s390_interrupt_info inti
;
2786 struct kvm_s390_irq irq
;
2787 struct kvm_s390_mchk_info
*mchk
;
2789 __u64 cr14
= 0; /* upper bits are not used */
2792 mci
.val
= mcck_info
->mcic
;
2794 cr14
|= CR14_RECOVERY_SUBMASK
;
2796 cr14
|= CR14_DEGRADATION_SUBMASK
;
2798 cr14
|= CR14_WARNING_SUBMASK
;
2800 mchk
= mci
.ck
? &inti
.mchk
: &irq
.u
.mchk
;
2802 mchk
->mcic
= mcck_info
->mcic
;
2803 mchk
->ext_damage_code
= mcck_info
->ext_damage_code
;
2804 mchk
->failing_storage_address
= mcck_info
->failing_storage_address
;
2806 /* Inject the floating machine check */
2807 inti
.type
= KVM_S390_MCHK
;
2808 rc
= __inject_vm(vcpu
->kvm
, &inti
);
2810 /* Inject the machine check to specified vcpu */
2811 irq
.type
= KVM_S390_MCHK
;
2812 rc
= kvm_s390_inject_vcpu(vcpu
, &irq
);
2817 int kvm_set_routing_entry(struct kvm
*kvm
,
2818 struct kvm_kernel_irq_routing_entry
*e
,
2819 const struct kvm_irq_routing_entry
*ue
)
2824 case KVM_IRQ_ROUTING_S390_ADAPTER
:
2825 e
->set
= set_adapter_int
;
2826 e
->adapter
.summary_addr
= ue
->u
.adapter
.summary_addr
;
2827 e
->adapter
.ind_addr
= ue
->u
.adapter
.ind_addr
;
2828 e
->adapter
.summary_offset
= ue
->u
.adapter
.summary_offset
;
2829 e
->adapter
.ind_offset
= ue
->u
.adapter
.ind_offset
;
2830 e
->adapter
.adapter_id
= ue
->u
.adapter
.adapter_id
;
2840 int kvm_set_msi(struct kvm_kernel_irq_routing_entry
*e
, struct kvm
*kvm
,
2841 int irq_source_id
, int level
, bool line_status
)
2846 int kvm_s390_set_irq_state(struct kvm_vcpu
*vcpu
, void __user
*irqstate
, int len
)
2848 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
2849 struct kvm_s390_irq
*buf
;
2857 if (copy_from_user((void *) buf
, irqstate
, len
)) {
2863 * Don't allow setting the interrupt state
2864 * when there are already interrupts pending
2866 spin_lock(&li
->lock
);
2867 if (li
->pending_irqs
) {
2872 for (n
= 0; n
< len
/ sizeof(*buf
); n
++) {
2873 r
= do_inject_vcpu(vcpu
, &buf
[n
]);
2879 spin_unlock(&li
->lock
);
2886 static void store_local_irq(struct kvm_s390_local_interrupt
*li
,
2887 struct kvm_s390_irq
*irq
,
2888 unsigned long irq_type
)
2891 case IRQ_PEND_MCHK_EX
:
2892 case IRQ_PEND_MCHK_REP
:
2893 irq
->type
= KVM_S390_MCHK
;
2894 irq
->u
.mchk
= li
->irq
.mchk
;
2897 irq
->type
= KVM_S390_PROGRAM_INT
;
2898 irq
->u
.pgm
= li
->irq
.pgm
;
2900 case IRQ_PEND_PFAULT_INIT
:
2901 irq
->type
= KVM_S390_INT_PFAULT_INIT
;
2902 irq
->u
.ext
= li
->irq
.ext
;
2904 case IRQ_PEND_EXT_EXTERNAL
:
2905 irq
->type
= KVM_S390_INT_EXTERNAL_CALL
;
2906 irq
->u
.extcall
= li
->irq
.extcall
;
2908 case IRQ_PEND_EXT_CLOCK_COMP
:
2909 irq
->type
= KVM_S390_INT_CLOCK_COMP
;
2911 case IRQ_PEND_EXT_CPU_TIMER
:
2912 irq
->type
= KVM_S390_INT_CPU_TIMER
;
2914 case IRQ_PEND_SIGP_STOP
:
2915 irq
->type
= KVM_S390_SIGP_STOP
;
2916 irq
->u
.stop
= li
->irq
.stop
;
2918 case IRQ_PEND_RESTART
:
2919 irq
->type
= KVM_S390_RESTART
;
2921 case IRQ_PEND_SET_PREFIX
:
2922 irq
->type
= KVM_S390_SIGP_SET_PREFIX
;
2923 irq
->u
.prefix
= li
->irq
.prefix
;
2928 int kvm_s390_get_irq_state(struct kvm_vcpu
*vcpu
, __u8 __user
*buf
, int len
)
2931 DECLARE_BITMAP(sigp_emerg_pending
, KVM_MAX_VCPUS
);
2932 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
2933 unsigned long pending_irqs
;
2934 struct kvm_s390_irq irq
;
2935 unsigned long irq_type
;
2939 spin_lock(&li
->lock
);
2940 pending_irqs
= li
->pending_irqs
;
2941 memcpy(&sigp_emerg_pending
, &li
->sigp_emerg_pending
,
2942 sizeof(sigp_emerg_pending
));
2943 spin_unlock(&li
->lock
);
2945 for_each_set_bit(irq_type
, &pending_irqs
, IRQ_PEND_COUNT
) {
2946 memset(&irq
, 0, sizeof(irq
));
2947 if (irq_type
== IRQ_PEND_EXT_EMERGENCY
)
2949 if (n
+ sizeof(irq
) > len
)
2951 store_local_irq(&vcpu
->arch
.local_int
, &irq
, irq_type
);
2952 if (copy_to_user(&buf
[n
], &irq
, sizeof(irq
)))
2957 if (test_bit(IRQ_PEND_EXT_EMERGENCY
, &pending_irqs
)) {
2958 for_each_set_bit(cpuaddr
, sigp_emerg_pending
, KVM_MAX_VCPUS
) {
2959 memset(&irq
, 0, sizeof(irq
));
2960 if (n
+ sizeof(irq
) > len
)
2962 irq
.type
= KVM_S390_INT_EMERGENCY
;
2963 irq
.u
.emerg
.code
= cpuaddr
;
2964 if (copy_to_user(&buf
[n
], &irq
, sizeof(irq
)))
2970 if (sca_ext_call_pending(vcpu
, &scn
)) {
2971 if (n
+ sizeof(irq
) > len
)
2973 memset(&irq
, 0, sizeof(irq
));
2974 irq
.type
= KVM_S390_INT_EXTERNAL_CALL
;
2975 irq
.u
.extcall
.code
= scn
;
2976 if (copy_to_user(&buf
[n
], &irq
, sizeof(irq
)))
2984 static void __airqs_kick_single_vcpu(struct kvm
*kvm
, u8 deliverable_mask
)
2986 int vcpu_id
, online_vcpus
= atomic_read(&kvm
->online_vcpus
);
2987 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
2988 struct kvm_vcpu
*vcpu
;
2990 for_each_set_bit(vcpu_id
, kvm
->arch
.idle_mask
, online_vcpus
) {
2991 vcpu
= kvm_get_vcpu(kvm
, vcpu_id
);
2992 if (psw_ioint_disabled(vcpu
))
2994 deliverable_mask
&= (u8
)(vcpu
->arch
.sie_block
->gcr
[6] >> 24);
2995 if (deliverable_mask
) {
2996 /* lately kicked but not yet running */
2997 if (test_and_set_bit(vcpu_id
, gi
->kicked_mask
))
2999 kvm_s390_vcpu_wakeup(vcpu
);
3005 static enum hrtimer_restart
gisa_vcpu_kicker(struct hrtimer
*timer
)
3007 struct kvm_s390_gisa_interrupt
*gi
=
3008 container_of(timer
, struct kvm_s390_gisa_interrupt
, timer
);
3010 container_of(gi
->origin
, struct sie_page2
, gisa
)->kvm
;
3013 pending_mask
= gisa_get_ipm_or_restore_iam(gi
);
3015 __airqs_kick_single_vcpu(kvm
, pending_mask
);
3016 hrtimer_forward_now(timer
, ns_to_ktime(gi
->expires
));
3017 return HRTIMER_RESTART
;
3020 return HRTIMER_NORESTART
;
3023 #define NULL_GISA_ADDR 0x00000000UL
3024 #define NONE_GISA_ADDR 0x00000001UL
3025 #define GISA_ADDR_MASK 0xfffff000UL
3027 static void process_gib_alert_list(void)
3029 struct kvm_s390_gisa_interrupt
*gi
;
3030 struct kvm_s390_gisa
*gisa
;
3032 u32 final
, origin
= 0UL;
3036 * If the NONE_GISA_ADDR is still stored in the alert list
3037 * origin, we will leave the outer loop. No further GISA has
3038 * been added to the alert list by millicode while processing
3039 * the current alert list.
3041 final
= (origin
& NONE_GISA_ADDR
);
3043 * Cut off the alert list and store the NONE_GISA_ADDR in the
3044 * alert list origin to avoid further GAL interruptions.
3045 * A new alert list can be build up by millicode in parallel
3046 * for guests not in the yet cut-off alert list. When in the
3047 * final loop, store the NULL_GISA_ADDR instead. This will re-
3048 * enable GAL interruptions on the host again.
3050 origin
= xchg(&gib
->alert_list_origin
,
3051 (!final
) ? NONE_GISA_ADDR
: NULL_GISA_ADDR
);
3053 * Loop through the just cut-off alert list and start the
3054 * gisa timers to kick idle vcpus to consume the pending
3055 * interruptions asap.
3057 while (origin
& GISA_ADDR_MASK
) {
3058 gisa
= (struct kvm_s390_gisa
*)(u64
)origin
;
3059 origin
= gisa
->next_alert
;
3060 gisa
->next_alert
= (u32
)(u64
)gisa
;
3061 kvm
= container_of(gisa
, struct sie_page2
, gisa
)->kvm
;
3062 gi
= &kvm
->arch
.gisa_int
;
3063 if (hrtimer_active(&gi
->timer
))
3064 hrtimer_cancel(&gi
->timer
);
3065 hrtimer_start(&gi
->timer
, 0, HRTIMER_MODE_REL
);
3071 void kvm_s390_gisa_clear(struct kvm
*kvm
)
3073 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
3077 gisa_clear_ipm(gi
->origin
);
3078 VM_EVENT(kvm
, 3, "gisa 0x%pK cleared", gi
->origin
);
3081 void kvm_s390_gisa_init(struct kvm
*kvm
)
3083 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
3085 if (!css_general_characteristics
.aiv
)
3087 gi
->origin
= &kvm
->arch
.sie_page2
->gisa
;
3089 spin_lock_init(&gi
->alert
.ref_lock
);
3090 gi
->expires
= 50 * 1000; /* 50 usec */
3091 hrtimer_init(&gi
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3092 gi
->timer
.function
= gisa_vcpu_kicker
;
3093 memset(gi
->origin
, 0, sizeof(struct kvm_s390_gisa
));
3094 gi
->origin
->next_alert
= (u32
)(u64
)gi
->origin
;
3095 VM_EVENT(kvm
, 3, "gisa 0x%pK initialized", gi
->origin
);
3098 void kvm_s390_gisa_destroy(struct kvm
*kvm
)
3100 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
3105 KVM_EVENT(3, "vm 0x%pK has unexpected iam 0x%02x",
3106 kvm
, gi
->alert
.mask
);
3107 while (gisa_in_alert_list(gi
->origin
))
3109 hrtimer_cancel(&gi
->timer
);
3114 * kvm_s390_gisc_register - register a guest ISC
3116 * @kvm: the kernel vm to work with
3117 * @gisc: the guest interruption sub class to register
3119 * The function extends the vm specific alert mask to use.
3120 * The effective IAM mask in the GISA is updated as well
3121 * in case the GISA is not part of the GIB alert list.
3122 * It will be updated latest when the IAM gets restored
3123 * by gisa_get_ipm_or_restore_iam().
3125 * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3126 * has registered with the channel subsystem.
3127 * -ENODEV in case the vm uses no GISA
3128 * -ERANGE in case the guest ISC is invalid
3130 int kvm_s390_gisc_register(struct kvm
*kvm
, u32 gisc
)
3132 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
3139 spin_lock(&gi
->alert
.ref_lock
);
3140 gi
->alert
.ref_count
[gisc
]++;
3141 if (gi
->alert
.ref_count
[gisc
] == 1) {
3142 gi
->alert
.mask
|= 0x80 >> gisc
;
3143 gisa_set_iam(gi
->origin
, gi
->alert
.mask
);
3145 spin_unlock(&gi
->alert
.ref_lock
);
3149 EXPORT_SYMBOL_GPL(kvm_s390_gisc_register
);
3152 * kvm_s390_gisc_unregister - unregister a guest ISC
3154 * @kvm: the kernel vm to work with
3155 * @gisc: the guest interruption sub class to register
3157 * The function reduces the vm specific alert mask to use.
3158 * The effective IAM mask in the GISA is updated as well
3159 * in case the GISA is not part of the GIB alert list.
3160 * It will be updated latest when the IAM gets restored
3161 * by gisa_get_ipm_or_restore_iam().
3163 * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3164 * has registered with the channel subsystem.
3165 * -ENODEV in case the vm uses no GISA
3166 * -ERANGE in case the guest ISC is invalid
3167 * -EINVAL in case the guest ISC is not registered
3169 int kvm_s390_gisc_unregister(struct kvm
*kvm
, u32 gisc
)
3171 struct kvm_s390_gisa_interrupt
*gi
= &kvm
->arch
.gisa_int
;
3179 spin_lock(&gi
->alert
.ref_lock
);
3180 if (gi
->alert
.ref_count
[gisc
] == 0) {
3184 gi
->alert
.ref_count
[gisc
]--;
3185 if (gi
->alert
.ref_count
[gisc
] == 0) {
3186 gi
->alert
.mask
&= ~(0x80 >> gisc
);
3187 gisa_set_iam(gi
->origin
, gi
->alert
.mask
);
3190 spin_unlock(&gi
->alert
.ref_lock
);
3194 EXPORT_SYMBOL_GPL(kvm_s390_gisc_unregister
);
3196 static void gib_alert_irq_handler(struct airq_struct
*airq
, bool floating
)
3198 inc_irq_stat(IRQIO_GAL
);
3199 process_gib_alert_list();
3202 static struct airq_struct gib_alert_irq
= {
3203 .handler
= gib_alert_irq_handler
,
3204 .lsi_ptr
= &gib_alert_irq
.lsi_mask
,
3207 void kvm_s390_gib_destroy(void)
3212 unregister_adapter_interrupt(&gib_alert_irq
);
3213 free_page((unsigned long)gib
);
3217 int kvm_s390_gib_init(u8 nisc
)
3221 if (!css_general_characteristics
.aiv
) {
3222 KVM_EVENT(3, "%s", "gib not initialized, no AIV facility");
3226 gib
= (struct kvm_s390_gib
*)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
3232 gib_alert_irq
.isc
= nisc
;
3233 if (register_adapter_interrupt(&gib_alert_irq
)) {
3234 pr_err("Registering the GIB alert interruption handler failed\n");
3240 if (chsc_sgib((u32
)(u64
)gib
)) {
3241 pr_err("Associating the GIB with the AIV facility failed\n");
3242 free_page((unsigned long)gib
);
3248 KVM_EVENT(3, "gib 0x%pK (nisc=%d) initialized", gib
, gib
->nisc
);
3252 unregister_adapter_interrupt(&gib_alert_irq
);
3254 free_page((unsigned long)gib
);