2 * handling kvm guest interrupts
4 * Copyright IBM Corp. 2008, 2015
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
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/signal.h>
18 #include <linux/slab.h>
19 #include <linux/bitmap.h>
20 #include <linux/vmalloc.h>
21 #include <asm/asm-offsets.h>
23 #include <asm/uaccess.h>
28 #include "trace-s390.h"
30 #define IOINT_SCHID_MASK 0x0000ffff
31 #define IOINT_SSID_MASK 0x00030000
32 #define IOINT_CSSID_MASK 0x03fc0000
33 #define PFAULT_INIT 0x0600
34 #define PFAULT_DONE 0x0680
35 #define VIRTIO_PARAM 0x0d00
37 int psw_extint_disabled(struct kvm_vcpu *vcpu)
39 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
42 static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
44 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
47 static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
49 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
52 static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
54 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
55 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
56 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
61 static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
63 if (psw_extint_disabled(vcpu) ||
64 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
66 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
67 /* No timer interrupts when single stepping */
72 static int ckc_irq_pending(struct kvm_vcpu *vcpu)
74 if (!(vcpu->arch.sie_block->ckc <
75 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
77 return ckc_interrupts_enabled(vcpu);
80 static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
82 return !psw_extint_disabled(vcpu) &&
83 (vcpu->arch.sie_block->gcr[0] & 0x400ul);
86 static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
88 return (vcpu->arch.sie_block->cputm >> 63) &&
89 cpu_timer_interrupts_enabled(vcpu);
92 static inline int is_ioirq(unsigned long irq_type)
94 return ((irq_type >= IRQ_PEND_IO_ISC_0) &&
95 (irq_type <= IRQ_PEND_IO_ISC_7));
98 static uint64_t isc_to_isc_bits(int isc)
100 return (0x80 >> isc) << 24;
103 static inline u8 int_word_to_isc(u32 int_word)
105 return (int_word & 0x38000000) >> 27;
108 static inline unsigned long pending_floating_irqs(struct kvm_vcpu *vcpu)
110 return vcpu->kvm->arch.float_int.pending_irqs;
113 static inline unsigned long pending_local_irqs(struct kvm_vcpu *vcpu)
115 return vcpu->arch.local_int.pending_irqs;
118 static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
119 unsigned long active_mask)
123 for (i = 0; i <= MAX_ISC; i++)
124 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
125 active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i));
130 static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
132 unsigned long active_mask;
134 active_mask = pending_local_irqs(vcpu);
135 active_mask |= pending_floating_irqs(vcpu);
139 if (psw_extint_disabled(vcpu))
140 active_mask &= ~IRQ_PEND_EXT_MASK;
141 if (psw_ioint_disabled(vcpu))
142 active_mask &= ~IRQ_PEND_IO_MASK;
144 active_mask = disable_iscs(vcpu, active_mask);
145 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
146 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
147 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
148 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
149 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
150 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
151 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
152 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
153 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
154 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
155 if (psw_mchk_disabled(vcpu))
156 active_mask &= ~IRQ_PEND_MCHK_MASK;
157 if (!(vcpu->arch.sie_block->gcr[14] &
158 vcpu->kvm->arch.float_int.mchk.cr14))
159 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
162 * STOP irqs will never be actively delivered. They are triggered via
163 * intercept requests and cleared when the stop intercept is performed.
165 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
170 static void __set_cpu_idle(struct kvm_vcpu *vcpu)
172 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
173 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
176 static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
178 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
179 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
182 static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
184 atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
185 &vcpu->arch.sie_block->cpuflags);
186 vcpu->arch.sie_block->lctl = 0x0000;
187 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
189 if (guestdbg_enabled(vcpu)) {
190 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
191 LCTL_CR10 | LCTL_CR11);
192 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
196 static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
198 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
201 static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
203 if (!(pending_floating_irqs(vcpu) & IRQ_PEND_IO_MASK))
205 else if (psw_ioint_disabled(vcpu))
206 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
208 vcpu->arch.sie_block->lctl |= LCTL_CR6;
211 static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
213 if (!(pending_local_irqs(vcpu) & IRQ_PEND_EXT_MASK))
215 if (psw_extint_disabled(vcpu))
216 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
218 vcpu->arch.sie_block->lctl |= LCTL_CR0;
221 static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
223 if (!(pending_local_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
225 if (psw_mchk_disabled(vcpu))
226 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
228 vcpu->arch.sie_block->lctl |= LCTL_CR14;
231 static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
233 if (kvm_s390_is_stop_irq_pending(vcpu))
234 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
237 /* Set interception request for non-deliverable interrupts */
238 static void set_intercept_indicators(struct kvm_vcpu *vcpu)
240 set_intercept_indicators_io(vcpu);
241 set_intercept_indicators_ext(vcpu);
242 set_intercept_indicators_mchk(vcpu);
243 set_intercept_indicators_stop(vcpu);
246 static u16 get_ilc(struct kvm_vcpu *vcpu)
248 switch (vcpu->arch.sie_block->icptcode) {
254 /* last instruction only stored for these icptcodes */
255 return insn_length(vcpu->arch.sie_block->ipa >> 8);
257 return vcpu->arch.sie_block->pgmilc;
263 static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
265 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
268 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
271 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
272 (u16 *)__LC_EXT_INT_CODE);
273 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
274 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
275 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
276 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
277 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
278 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
279 return rc ? -EFAULT : 0;
282 static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
284 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
287 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
290 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
291 (u16 __user *)__LC_EXT_INT_CODE);
292 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
293 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
294 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
295 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
296 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
297 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
298 return rc ? -EFAULT : 0;
301 static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
303 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
304 struct kvm_s390_ext_info ext;
307 spin_lock(&li->lock);
309 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
310 li->irq.ext.ext_params2 = 0;
311 spin_unlock(&li->lock);
313 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
315 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
316 KVM_S390_INT_PFAULT_INIT,
319 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
320 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
321 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
322 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
323 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
324 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
325 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
326 return rc ? -EFAULT : 0;
329 static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
331 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
332 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
333 struct kvm_s390_mchk_info mchk = {};
334 unsigned long adtl_status_addr;
338 spin_lock(&fi->lock);
339 spin_lock(&li->lock);
340 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
341 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
343 * If there was an exigent machine check pending, then any
344 * repressible machine checks that might have been pending
345 * are indicated along with it, so always clear bits for
346 * repressible and exigent interrupts
349 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
350 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
351 memset(&li->irq.mchk, 0, sizeof(mchk));
355 * We indicate floating repressible conditions along with
356 * other pending conditions. Channel Report Pending and Channel
357 * Subsystem damage are the only two and and are indicated by
358 * bits in mcic and masked in cr14.
360 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
361 mchk.mcic |= fi->mchk.mcic;
362 mchk.cr14 |= fi->mchk.cr14;
363 memset(&fi->mchk, 0, sizeof(mchk));
366 spin_unlock(&li->lock);
367 spin_unlock(&fi->lock);
370 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
372 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
374 mchk.cr14, mchk.mcic);
376 rc = kvm_s390_vcpu_store_status(vcpu,
377 KVM_S390_STORE_STATUS_PREFIXED);
378 rc |= read_guest_lc(vcpu, __LC_VX_SAVE_AREA_ADDR,
380 sizeof(unsigned long));
381 rc |= kvm_s390_vcpu_store_adtl_status(vcpu,
383 rc |= put_guest_lc(vcpu, mchk.mcic,
384 (u64 __user *) __LC_MCCK_CODE);
385 rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
386 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
387 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
389 sizeof(mchk.fixed_logout));
390 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
391 &vcpu->arch.sie_block->gpsw,
393 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
394 &vcpu->arch.sie_block->gpsw,
397 return rc ? -EFAULT : 0;
400 static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
402 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
405 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
406 vcpu->stat.deliver_restart_signal++;
407 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
409 rc = write_guest_lc(vcpu,
410 offsetof(struct _lowcore, restart_old_psw),
411 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
412 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
413 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
414 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
415 return rc ? -EFAULT : 0;
418 static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
420 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
421 struct kvm_s390_prefix_info prefix;
423 spin_lock(&li->lock);
424 prefix = li->irq.prefix;
425 li->irq.prefix.address = 0;
426 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
427 spin_unlock(&li->lock);
429 vcpu->stat.deliver_prefix_signal++;
430 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
431 KVM_S390_SIGP_SET_PREFIX,
434 kvm_s390_set_prefix(vcpu, prefix.address);
438 static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
440 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
444 spin_lock(&li->lock);
445 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
446 clear_bit(cpu_addr, li->sigp_emerg_pending);
447 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
448 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
449 spin_unlock(&li->lock);
451 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
452 vcpu->stat.deliver_emergency_signal++;
453 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
456 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
457 (u16 *)__LC_EXT_INT_CODE);
458 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
459 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
460 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
461 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
462 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
463 return rc ? -EFAULT : 0;
466 static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
468 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
469 struct kvm_s390_extcall_info extcall;
472 spin_lock(&li->lock);
473 extcall = li->irq.extcall;
474 li->irq.extcall.code = 0;
475 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
476 spin_unlock(&li->lock);
478 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
479 vcpu->stat.deliver_external_call++;
480 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
481 KVM_S390_INT_EXTERNAL_CALL,
484 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
485 (u16 *)__LC_EXT_INT_CODE);
486 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
487 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
488 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
489 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
491 return rc ? -EFAULT : 0;
494 static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
496 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
497 struct kvm_s390_pgm_info pgm_info;
498 int rc = 0, nullifying = false;
499 u16 ilc = get_ilc(vcpu);
501 spin_lock(&li->lock);
502 pgm_info = li->irq.pgm;
503 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
504 memset(&li->irq.pgm, 0, sizeof(pgm_info));
505 spin_unlock(&li->lock);
507 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilc:%d",
509 vcpu->stat.deliver_program_int++;
510 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
513 switch (pgm_info.code & ~PGM_PER) {
514 case PGM_AFX_TRANSLATION:
515 case PGM_ASX_TRANSLATION:
516 case PGM_EX_TRANSLATION:
517 case PGM_LFX_TRANSLATION:
518 case PGM_LSTE_SEQUENCE:
519 case PGM_LSX_TRANSLATION:
520 case PGM_LX_TRANSLATION:
521 case PGM_PRIMARY_AUTHORITY:
522 case PGM_SECONDARY_AUTHORITY:
525 case PGM_SPACE_SWITCH:
526 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
527 (u64 *)__LC_TRANS_EXC_CODE);
529 case PGM_ALEN_TRANSLATION:
530 case PGM_ALE_SEQUENCE:
531 case PGM_ASTE_INSTANCE:
532 case PGM_ASTE_SEQUENCE:
533 case PGM_ASTE_VALIDITY:
534 case PGM_EXTENDED_AUTHORITY:
535 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
536 (u8 *)__LC_EXC_ACCESS_ID);
540 case PGM_PAGE_TRANSLATION:
541 case PGM_REGION_FIRST_TRANS:
542 case PGM_REGION_SECOND_TRANS:
543 case PGM_REGION_THIRD_TRANS:
544 case PGM_SEGMENT_TRANSLATION:
545 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
546 (u64 *)__LC_TRANS_EXC_CODE);
547 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
548 (u8 *)__LC_EXC_ACCESS_ID);
549 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
550 (u8 *)__LC_OP_ACCESS_ID);
554 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
555 (u16 *)__LC_MON_CLASS_NR);
556 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
557 (u64 *)__LC_MON_CODE);
559 case PGM_VECTOR_PROCESSING:
561 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
562 (u32 *)__LC_DATA_EXC_CODE);
565 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
566 (u64 *)__LC_TRANS_EXC_CODE);
567 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
568 (u8 *)__LC_EXC_ACCESS_ID);
571 case PGM_STACK_EMPTY:
572 case PGM_STACK_SPECIFICATION:
574 case PGM_STACK_OPERATION:
575 case PGM_TRACE_TABEL:
576 case PGM_CRYPTO_OPERATION:
581 if (pgm_info.code & PGM_PER) {
582 rc |= put_guest_lc(vcpu, pgm_info.per_code,
583 (u8 *) __LC_PER_CODE);
584 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
585 (u8 *)__LC_PER_ATMID);
586 rc |= put_guest_lc(vcpu, pgm_info.per_address,
587 (u64 *) __LC_PER_ADDRESS);
588 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
589 (u8 *) __LC_PER_ACCESS_ID);
592 if (nullifying && vcpu->arch.sie_block->icptcode == ICPT_INST)
593 kvm_s390_rewind_psw(vcpu, ilc);
595 rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
596 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
597 (u64 *) __LC_LAST_BREAK);
598 rc |= put_guest_lc(vcpu, pgm_info.code,
599 (u16 *)__LC_PGM_INT_CODE);
600 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
601 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
602 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
603 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
604 return rc ? -EFAULT : 0;
607 static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
609 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
610 struct kvm_s390_ext_info ext;
613 spin_lock(&fi->lock);
614 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
615 spin_unlock(&fi->lock);
618 ext = fi->srv_signal;
619 memset(&fi->srv_signal, 0, sizeof(ext));
620 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
621 spin_unlock(&fi->lock);
623 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
625 vcpu->stat.deliver_service_signal++;
626 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
629 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
630 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
631 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
632 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
633 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
634 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
635 rc |= put_guest_lc(vcpu, ext.ext_params,
636 (u32 *)__LC_EXT_PARAMS);
638 return rc ? -EFAULT : 0;
641 static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
643 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
644 struct kvm_s390_interrupt_info *inti;
647 spin_lock(&fi->lock);
648 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
649 struct kvm_s390_interrupt_info,
652 list_del(&inti->list);
653 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
655 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
656 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
657 spin_unlock(&fi->lock);
660 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
661 KVM_S390_INT_PFAULT_DONE, 0,
662 inti->ext.ext_params2);
663 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
664 inti->ext.ext_params2);
666 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
667 (u16 *)__LC_EXT_INT_CODE);
668 rc |= put_guest_lc(vcpu, PFAULT_DONE,
669 (u16 *)__LC_EXT_CPU_ADDR);
670 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
671 &vcpu->arch.sie_block->gpsw,
673 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
674 &vcpu->arch.sie_block->gpsw,
676 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
677 (u64 *)__LC_EXT_PARAMS2);
680 return rc ? -EFAULT : 0;
683 static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
685 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
686 struct kvm_s390_interrupt_info *inti;
689 spin_lock(&fi->lock);
690 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
691 struct kvm_s390_interrupt_info,
695 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
696 inti->ext.ext_params, inti->ext.ext_params2);
697 vcpu->stat.deliver_virtio_interrupt++;
698 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
700 inti->ext.ext_params,
701 inti->ext.ext_params2);
702 list_del(&inti->list);
703 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
705 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
706 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
707 spin_unlock(&fi->lock);
710 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
711 (u16 *)__LC_EXT_INT_CODE);
712 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
713 (u16 *)__LC_EXT_CPU_ADDR);
714 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
715 &vcpu->arch.sie_block->gpsw,
717 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
718 &vcpu->arch.sie_block->gpsw,
720 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
721 (u32 *)__LC_EXT_PARAMS);
722 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
723 (u64 *)__LC_EXT_PARAMS2);
726 return rc ? -EFAULT : 0;
729 static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
730 unsigned long irq_type)
732 struct list_head *isc_list;
733 struct kvm_s390_float_interrupt *fi;
734 struct kvm_s390_interrupt_info *inti = NULL;
737 fi = &vcpu->kvm->arch.float_int;
739 spin_lock(&fi->lock);
740 isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
741 inti = list_first_entry_or_null(isc_list,
742 struct kvm_s390_interrupt_info,
745 VCPU_EVENT(vcpu, 4, "deliver: I/O 0x%llx", inti->type);
746 vcpu->stat.deliver_io_int++;
747 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
749 ((__u32)inti->io.subchannel_id << 16) |
750 inti->io.subchannel_nr,
751 ((__u64)inti->io.io_int_parm << 32) |
752 inti->io.io_int_word);
753 list_del(&inti->list);
754 fi->counters[FIRQ_CNTR_IO] -= 1;
756 if (list_empty(isc_list))
757 clear_bit(irq_type, &fi->pending_irqs);
758 spin_unlock(&fi->lock);
761 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
762 (u16 *)__LC_SUBCHANNEL_ID);
763 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
764 (u16 *)__LC_SUBCHANNEL_NR);
765 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
766 (u32 *)__LC_IO_INT_PARM);
767 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
768 (u32 *)__LC_IO_INT_WORD);
769 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
770 &vcpu->arch.sie_block->gpsw,
772 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
773 &vcpu->arch.sie_block->gpsw,
778 return rc ? -EFAULT : 0;
781 typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
783 static const deliver_irq_t deliver_irq_funcs[] = {
784 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
785 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
786 [IRQ_PEND_PROG] = __deliver_prog,
787 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
788 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
789 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
790 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
791 [IRQ_PEND_RESTART] = __deliver_restart,
792 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
793 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
794 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
795 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
796 [IRQ_PEND_VIRTIO] = __deliver_virtio,
799 /* Check whether an external call is pending (deliverable or not) */
800 int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
802 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
803 uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
805 if (!sclp.has_sigpif)
806 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
808 return (sigp_ctrl & SIGP_CTRL_C) &&
809 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND);
812 int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
816 rc = !!deliverable_irqs(vcpu);
818 if (!rc && kvm_cpu_has_pending_timer(vcpu))
821 /* external call pending and deliverable */
822 if (!rc && kvm_s390_ext_call_pending(vcpu) &&
823 !psw_extint_disabled(vcpu) &&
824 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
827 if (!rc && !exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
833 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
835 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
838 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
842 vcpu->stat.exit_wait_state++;
845 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
848 if (psw_interrupts_disabled(vcpu)) {
849 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
850 return -EOPNOTSUPP; /* disabled wait */
853 if (!ckc_interrupts_enabled(vcpu)) {
854 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
855 __set_cpu_idle(vcpu);
859 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
860 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
863 if (vcpu->arch.sie_block->ckc < now)
866 __set_cpu_idle(vcpu);
867 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
868 VCPU_EVENT(vcpu, 4, "enabled wait via clock comparator: %llu ns", sltime);
870 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
871 kvm_vcpu_block(vcpu);
872 __unset_cpu_idle(vcpu);
873 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
875 hrtimer_cancel(&vcpu->arch.ckc_timer);
879 void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
881 if (waitqueue_active(&vcpu->wq)) {
883 * The vcpu gave up the cpu voluntarily, mark it as a good
886 vcpu->preempted = true;
887 wake_up_interruptible(&vcpu->wq);
888 vcpu->stat.halt_wakeup++;
892 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
894 struct kvm_vcpu *vcpu;
897 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
898 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
899 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
902 * If the monotonic clock runs faster than the tod clock we might be
903 * woken up too early and have to go back to sleep to avoid deadlocks.
905 if (vcpu->arch.sie_block->ckc > now &&
906 hrtimer_forward_now(timer, ns_to_ktime(sltime)))
907 return HRTIMER_RESTART;
908 kvm_s390_vcpu_wakeup(vcpu);
909 return HRTIMER_NORESTART;
912 void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
914 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
916 spin_lock(&li->lock);
917 li->pending_irqs = 0;
918 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
919 memset(&li->irq, 0, sizeof(li->irq));
920 spin_unlock(&li->lock);
922 /* clear pending external calls set by sigp interpretation facility */
923 atomic_clear_mask(CPUSTAT_ECALL_PEND, li->cpuflags);
924 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl = 0;
927 int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
929 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
932 unsigned long irq_type;
935 __reset_intercept_indicators(vcpu);
937 /* pending ckc conditions might have been invalidated */
938 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
939 if (ckc_irq_pending(vcpu))
940 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
942 /* pending cpu timer conditions might have been invalidated */
943 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
944 if (cpu_timer_irq_pending(vcpu))
945 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
947 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
948 /* bits are in the order of interrupt priority */
949 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
950 if (is_ioirq(irq_type)) {
951 rc = __deliver_io(vcpu, irq_type);
953 func = deliver_irq_funcs[irq_type];
955 WARN_ON_ONCE(func == NULL);
956 clear_bit(irq_type, &li->pending_irqs);
963 set_intercept_indicators(vcpu);
968 static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
970 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
972 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
973 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
976 li->irq.pgm = irq->u.pgm;
977 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
981 int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
983 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
984 struct kvm_s390_irq irq;
986 spin_lock(&li->lock);
987 irq.u.pgm.code = code;
988 __inject_prog(vcpu, &irq);
989 BUG_ON(waitqueue_active(li->wq));
990 spin_unlock(&li->lock);
994 int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
995 struct kvm_s390_pgm_info *pgm_info)
997 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
998 struct kvm_s390_irq irq;
1001 spin_lock(&li->lock);
1002 irq.u.pgm = *pgm_info;
1003 rc = __inject_prog(vcpu, &irq);
1004 BUG_ON(waitqueue_active(li->wq));
1005 spin_unlock(&li->lock);
1009 static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1011 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1013 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1014 irq->u.ext.ext_params2);
1015 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1016 irq->u.ext.ext_params,
1017 irq->u.ext.ext_params2);
1019 li->irq.ext = irq->u.ext;
1020 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
1021 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1025 static int __inject_extcall_sigpif(struct kvm_vcpu *vcpu, uint16_t src_id)
1027 unsigned char new_val, old_val;
1028 uint8_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
1030 new_val = SIGP_CTRL_C | (src_id & SIGP_CTRL_SCN_MASK);
1031 old_val = *sigp_ctrl & ~SIGP_CTRL_C;
1032 if (cmpxchg(sigp_ctrl, old_val, new_val) != old_val) {
1033 /* another external call is pending */
1036 atomic_set_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
1040 static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1042 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1043 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
1044 uint16_t src_id = irq->u.extcall.code;
1046 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
1048 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
1051 /* sending vcpu invalid */
1052 if (src_id >= KVM_MAX_VCPUS ||
1053 kvm_get_vcpu(vcpu->kvm, src_id) == NULL)
1056 if (sclp.has_sigpif)
1057 return __inject_extcall_sigpif(vcpu, src_id);
1059 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
1061 *extcall = irq->u.extcall;
1062 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1066 static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1068 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1069 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
1071 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
1072 irq->u.prefix.address);
1073 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
1074 irq->u.prefix.address, 0);
1076 if (!is_vcpu_stopped(vcpu))
1079 *prefix = irq->u.prefix;
1080 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
1084 #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
1085 static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1087 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1088 struct kvm_s390_stop_info *stop = &li->irq.stop;
1091 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
1093 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1096 if (is_vcpu_stopped(vcpu)) {
1097 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1098 rc = kvm_s390_store_status_unloaded(vcpu,
1099 KVM_S390_STORE_STATUS_NOADDR);
1103 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1105 stop->flags = irq->u.stop.flags;
1106 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
1110 static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
1111 struct kvm_s390_irq *irq)
1113 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1115 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
1116 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
1118 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
1122 static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
1123 struct kvm_s390_irq *irq)
1125 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1127 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
1129 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
1130 irq->u.emerg.code, 0);
1132 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
1133 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
1134 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1138 static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1140 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1141 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
1143 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
1145 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
1149 * Because repressible machine checks can be indicated along with
1150 * exigent machine checks (PoP, Chapter 11, Interruption action)
1151 * we need to combine cr14, mcic and external damage code.
1152 * Failing storage address and the logout area should not be or'ed
1153 * together, we just indicate the last occurrence of the corresponding
1156 mchk->cr14 |= irq->u.mchk.cr14;
1157 mchk->mcic |= irq->u.mchk.mcic;
1158 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1159 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1160 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1161 sizeof(mchk->fixed_logout));
1162 if (mchk->mcic & MCHK_EX_MASK)
1163 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1164 else if (mchk->mcic & MCHK_REP_MASK)
1165 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
1169 static int __inject_ckc(struct kvm_vcpu *vcpu)
1171 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1173 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
1174 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
1177 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1178 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1182 static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
1184 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1186 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
1187 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
1190 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1191 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1195 static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1198 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1199 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1200 struct kvm_s390_interrupt_info *iter;
1201 u16 id = (schid & 0xffff0000U) >> 16;
1202 u16 nr = schid & 0x0000ffffU;
1204 spin_lock(&fi->lock);
1205 list_for_each_entry(iter, isc_list, list) {
1206 if (schid && (id != iter->io.subchannel_id ||
1207 nr != iter->io.subchannel_nr))
1209 /* found an appropriate entry */
1210 list_del_init(&iter->list);
1211 fi->counters[FIRQ_CNTR_IO] -= 1;
1212 if (list_empty(isc_list))
1213 clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1214 spin_unlock(&fi->lock);
1217 spin_unlock(&fi->lock);
1222 * Dequeue and return an I/O interrupt matching any of the interruption
1223 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1225 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
1226 u64 isc_mask, u32 schid)
1228 struct kvm_s390_interrupt_info *inti = NULL;
1231 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1232 if (isc_mask & isc_to_isc_bits(isc))
1233 inti = get_io_int(kvm, isc, schid);
1238 #define SCCB_MASK 0xFFFFFFF8
1239 #define SCCB_EVENT_PENDING 0x3
1241 static int __inject_service(struct kvm *kvm,
1242 struct kvm_s390_interrupt_info *inti)
1244 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1246 spin_lock(&fi->lock);
1247 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1249 * Early versions of the QEMU s390 bios will inject several
1250 * service interrupts after another without handling a
1251 * condition code indicating busy.
1252 * We will silently ignore those superfluous sccb values.
1253 * A future version of QEMU will take care of serialization
1256 if (fi->srv_signal.ext_params & SCCB_MASK)
1258 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1259 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1261 spin_unlock(&fi->lock);
1266 static int __inject_virtio(struct kvm *kvm,
1267 struct kvm_s390_interrupt_info *inti)
1269 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1271 spin_lock(&fi->lock);
1272 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1273 spin_unlock(&fi->lock);
1276 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1277 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1278 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1279 spin_unlock(&fi->lock);
1283 static int __inject_pfault_done(struct kvm *kvm,
1284 struct kvm_s390_interrupt_info *inti)
1286 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1288 spin_lock(&fi->lock);
1289 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1290 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1291 spin_unlock(&fi->lock);
1294 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1295 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1296 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1297 spin_unlock(&fi->lock);
1301 #define CR_PENDING_SUBCLASS 28
1302 static int __inject_float_mchk(struct kvm *kvm,
1303 struct kvm_s390_interrupt_info *inti)
1305 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1307 spin_lock(&fi->lock);
1308 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1309 fi->mchk.mcic |= inti->mchk.mcic;
1310 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1311 spin_unlock(&fi->lock);
1316 static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1318 struct kvm_s390_float_interrupt *fi;
1319 struct list_head *list;
1322 fi = &kvm->arch.float_int;
1323 spin_lock(&fi->lock);
1324 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1325 spin_unlock(&fi->lock);
1328 fi->counters[FIRQ_CNTR_IO] += 1;
1330 isc = int_word_to_isc(inti->io.io_int_word);
1331 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1332 list_add_tail(&inti->list, list);
1333 set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1334 spin_unlock(&fi->lock);
1339 * Find a destination VCPU for a floating irq and kick it.
1341 static void __floating_irq_kick(struct kvm *kvm, u64 type)
1343 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1344 struct kvm_s390_local_interrupt *li;
1345 struct kvm_vcpu *dst_vcpu;
1346 int sigcpu, online_vcpus, nr_tries = 0;
1348 online_vcpus = atomic_read(&kvm->online_vcpus);
1352 /* find idle VCPUs first, then round robin */
1353 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1354 if (sigcpu == online_vcpus) {
1356 sigcpu = fi->next_rr_cpu;
1357 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1358 /* avoid endless loops if all vcpus are stopped */
1359 if (nr_tries++ >= online_vcpus)
1361 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1363 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1365 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1366 li = &dst_vcpu->arch.local_int;
1367 spin_lock(&li->lock);
1370 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
1372 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1373 atomic_set_mask(CPUSTAT_IO_INT, li->cpuflags);
1376 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1379 spin_unlock(&li->lock);
1380 kvm_s390_vcpu_wakeup(dst_vcpu);
1383 static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1385 struct kvm_s390_float_interrupt *fi;
1386 u64 type = READ_ONCE(inti->type);
1389 fi = &kvm->arch.float_int;
1393 rc = __inject_float_mchk(kvm, inti);
1395 case KVM_S390_INT_VIRTIO:
1396 rc = __inject_virtio(kvm, inti);
1398 case KVM_S390_INT_SERVICE:
1399 rc = __inject_service(kvm, inti);
1401 case KVM_S390_INT_PFAULT_DONE:
1402 rc = __inject_pfault_done(kvm, inti);
1404 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1405 rc = __inject_io(kvm, inti);
1413 __floating_irq_kick(kvm, type);
1417 int kvm_s390_inject_vm(struct kvm *kvm,
1418 struct kvm_s390_interrupt *s390int)
1420 struct kvm_s390_interrupt_info *inti;
1423 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1427 inti->type = s390int->type;
1428 switch (inti->type) {
1429 case KVM_S390_INT_VIRTIO:
1430 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1431 s390int->parm, s390int->parm64);
1432 inti->ext.ext_params = s390int->parm;
1433 inti->ext.ext_params2 = s390int->parm64;
1435 case KVM_S390_INT_SERVICE:
1436 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
1437 inti->ext.ext_params = s390int->parm;
1439 case KVM_S390_INT_PFAULT_DONE:
1440 inti->ext.ext_params2 = s390int->parm64;
1443 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
1445 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1446 inti->mchk.mcic = s390int->parm64;
1448 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1449 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1450 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1452 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1453 s390int->type & IOINT_CSSID_MASK,
1454 s390int->type & IOINT_SSID_MASK,
1455 s390int->type & IOINT_SCHID_MASK);
1456 inti->io.subchannel_id = s390int->parm >> 16;
1457 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1458 inti->io.io_int_parm = s390int->parm64 >> 32;
1459 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1465 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1468 rc = __inject_vm(kvm, inti);
1474 int kvm_s390_reinject_io_int(struct kvm *kvm,
1475 struct kvm_s390_interrupt_info *inti)
1477 return __inject_vm(kvm, inti);
1480 int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1481 struct kvm_s390_irq *irq)
1483 irq->type = s390int->type;
1484 switch (irq->type) {
1485 case KVM_S390_PROGRAM_INT:
1486 if (s390int->parm & 0xffff0000)
1488 irq->u.pgm.code = s390int->parm;
1490 case KVM_S390_SIGP_SET_PREFIX:
1491 irq->u.prefix.address = s390int->parm;
1493 case KVM_S390_SIGP_STOP:
1494 irq->u.stop.flags = s390int->parm;
1496 case KVM_S390_INT_EXTERNAL_CALL:
1497 if (s390int->parm & 0xffff0000)
1499 irq->u.extcall.code = s390int->parm;
1501 case KVM_S390_INT_EMERGENCY:
1502 if (s390int->parm & 0xffff0000)
1504 irq->u.emerg.code = s390int->parm;
1507 irq->u.mchk.mcic = s390int->parm64;
1513 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1515 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1517 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1520 void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1522 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1524 spin_lock(&li->lock);
1525 li->irq.stop.flags = 0;
1526 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1527 spin_unlock(&li->lock);
1530 static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1534 switch (irq->type) {
1535 case KVM_S390_PROGRAM_INT:
1536 rc = __inject_prog(vcpu, irq);
1538 case KVM_S390_SIGP_SET_PREFIX:
1539 rc = __inject_set_prefix(vcpu, irq);
1541 case KVM_S390_SIGP_STOP:
1542 rc = __inject_sigp_stop(vcpu, irq);
1544 case KVM_S390_RESTART:
1545 rc = __inject_sigp_restart(vcpu, irq);
1547 case KVM_S390_INT_CLOCK_COMP:
1548 rc = __inject_ckc(vcpu);
1550 case KVM_S390_INT_CPU_TIMER:
1551 rc = __inject_cpu_timer(vcpu);
1553 case KVM_S390_INT_EXTERNAL_CALL:
1554 rc = __inject_extcall(vcpu, irq);
1556 case KVM_S390_INT_EMERGENCY:
1557 rc = __inject_sigp_emergency(vcpu, irq);
1560 rc = __inject_mchk(vcpu, irq);
1562 case KVM_S390_INT_PFAULT_INIT:
1563 rc = __inject_pfault_init(vcpu, irq);
1565 case KVM_S390_INT_VIRTIO:
1566 case KVM_S390_INT_SERVICE:
1567 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1575 int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1577 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1580 spin_lock(&li->lock);
1581 rc = do_inject_vcpu(vcpu, irq);
1582 spin_unlock(&li->lock);
1584 kvm_s390_vcpu_wakeup(vcpu);
1588 static inline void clear_irq_list(struct list_head *_list)
1590 struct kvm_s390_interrupt_info *inti, *n;
1592 list_for_each_entry_safe(inti, n, _list, list) {
1593 list_del(&inti->list);
1598 static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1599 struct kvm_s390_irq *irq)
1601 irq->type = inti->type;
1602 switch (inti->type) {
1603 case KVM_S390_INT_PFAULT_INIT:
1604 case KVM_S390_INT_PFAULT_DONE:
1605 case KVM_S390_INT_VIRTIO:
1606 irq->u.ext = inti->ext;
1608 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1609 irq->u.io = inti->io;
1614 void kvm_s390_clear_float_irqs(struct kvm *kvm)
1616 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1619 spin_lock(&fi->lock);
1620 fi->pending_irqs = 0;
1621 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1622 memset(&fi->mchk, 0, sizeof(fi->mchk));
1623 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1624 clear_irq_list(&fi->lists[i]);
1625 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1626 fi->counters[i] = 0;
1627 spin_unlock(&fi->lock);
1630 static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
1632 struct kvm_s390_interrupt_info *inti;
1633 struct kvm_s390_float_interrupt *fi;
1634 struct kvm_s390_irq *buf;
1635 struct kvm_s390_irq *irq;
1641 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1645 * We are already using -ENOMEM to signal
1646 * userspace it may retry with a bigger buffer,
1647 * so we need to use something else for this case
1653 max_irqs = len / sizeof(struct kvm_s390_irq);
1655 fi = &kvm->arch.float_int;
1656 spin_lock(&fi->lock);
1657 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1658 list_for_each_entry(inti, &fi->lists[i], list) {
1659 if (n == max_irqs) {
1660 /* signal userspace to try again */
1664 inti_to_irq(inti, &buf[n]);
1668 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
1669 if (n == max_irqs) {
1670 /* signal userspace to try again */
1674 irq = (struct kvm_s390_irq *) &buf[n];
1675 irq->type = KVM_S390_INT_SERVICE;
1676 irq->u.ext = fi->srv_signal;
1679 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1680 if (n == max_irqs) {
1681 /* signal userspace to try again */
1685 irq = (struct kvm_s390_irq *) &buf[n];
1686 irq->type = KVM_S390_MCHK;
1687 irq->u.mchk = fi->mchk;
1692 spin_unlock(&fi->lock);
1693 if (!ret && n > 0) {
1694 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1699 return ret < 0 ? ret : n;
1702 static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1706 switch (attr->group) {
1707 case KVM_DEV_FLIC_GET_ALL_IRQS:
1708 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
1718 static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1721 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1722 void *target = NULL;
1723 void __user *source;
1726 if (get_user(inti->type, (u64 __user *)addr))
1729 switch (inti->type) {
1730 case KVM_S390_INT_PFAULT_INIT:
1731 case KVM_S390_INT_PFAULT_DONE:
1732 case KVM_S390_INT_VIRTIO:
1733 case KVM_S390_INT_SERVICE:
1734 target = (void *) &inti->ext;
1735 source = &uptr->u.ext;
1736 size = sizeof(inti->ext);
1738 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1739 target = (void *) &inti->io;
1740 source = &uptr->u.io;
1741 size = sizeof(inti->io);
1744 target = (void *) &inti->mchk;
1745 source = &uptr->u.mchk;
1746 size = sizeof(inti->mchk);
1752 if (copy_from_user(target, source, size))
1758 static int enqueue_floating_irq(struct kvm_device *dev,
1759 struct kvm_device_attr *attr)
1761 struct kvm_s390_interrupt_info *inti = NULL;
1763 int len = attr->attr;
1765 if (len % sizeof(struct kvm_s390_irq) != 0)
1767 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1770 while (len >= sizeof(struct kvm_s390_irq)) {
1771 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1775 r = copy_irq_from_user(inti, attr->addr);
1780 r = __inject_vm(dev->kvm, inti);
1785 len -= sizeof(struct kvm_s390_irq);
1786 attr->addr += sizeof(struct kvm_s390_irq);
1792 static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1794 if (id >= MAX_S390_IO_ADAPTERS)
1796 return kvm->arch.adapters[id];
1799 static int register_io_adapter(struct kvm_device *dev,
1800 struct kvm_device_attr *attr)
1802 struct s390_io_adapter *adapter;
1803 struct kvm_s390_io_adapter adapter_info;
1805 if (copy_from_user(&adapter_info,
1806 (void __user *)attr->addr, sizeof(adapter_info)))
1809 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1810 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1813 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1817 INIT_LIST_HEAD(&adapter->maps);
1818 init_rwsem(&adapter->maps_lock);
1819 atomic_set(&adapter->nr_maps, 0);
1820 adapter->id = adapter_info.id;
1821 adapter->isc = adapter_info.isc;
1822 adapter->maskable = adapter_info.maskable;
1823 adapter->masked = false;
1824 adapter->swap = adapter_info.swap;
1825 dev->kvm->arch.adapters[adapter->id] = adapter;
1830 int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1833 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1835 if (!adapter || !adapter->maskable)
1837 ret = adapter->masked;
1838 adapter->masked = masked;
1842 static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1844 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1845 struct s390_map_info *map;
1848 if (!adapter || !addr)
1851 map = kzalloc(sizeof(*map), GFP_KERNEL);
1856 INIT_LIST_HEAD(&map->list);
1857 map->guest_addr = addr;
1858 map->addr = gmap_translate(kvm->arch.gmap, addr);
1859 if (map->addr == -EFAULT) {
1863 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1867 down_write(&adapter->maps_lock);
1868 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1869 list_add_tail(&map->list, &adapter->maps);
1872 put_page(map->page);
1875 up_write(&adapter->maps_lock);
1882 static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1884 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1885 struct s390_map_info *map, *tmp;
1888 if (!adapter || !addr)
1891 down_write(&adapter->maps_lock);
1892 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1893 if (map->guest_addr == addr) {
1895 atomic_dec(&adapter->nr_maps);
1896 list_del(&map->list);
1897 put_page(map->page);
1902 up_write(&adapter->maps_lock);
1904 return found ? 0 : -EINVAL;
1907 void kvm_s390_destroy_adapters(struct kvm *kvm)
1910 struct s390_map_info *map, *tmp;
1912 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1913 if (!kvm->arch.adapters[i])
1915 list_for_each_entry_safe(map, tmp,
1916 &kvm->arch.adapters[i]->maps, list) {
1917 list_del(&map->list);
1918 put_page(map->page);
1921 kfree(kvm->arch.adapters[i]);
1925 static int modify_io_adapter(struct kvm_device *dev,
1926 struct kvm_device_attr *attr)
1928 struct kvm_s390_io_adapter_req req;
1929 struct s390_io_adapter *adapter;
1932 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1935 adapter = get_io_adapter(dev->kvm, req.id);
1939 case KVM_S390_IO_ADAPTER_MASK:
1940 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1944 case KVM_S390_IO_ADAPTER_MAP:
1945 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1947 case KVM_S390_IO_ADAPTER_UNMAP:
1948 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1957 static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1961 struct kvm_vcpu *vcpu;
1963 switch (attr->group) {
1964 case KVM_DEV_FLIC_ENQUEUE:
1965 r = enqueue_floating_irq(dev, attr);
1967 case KVM_DEV_FLIC_CLEAR_IRQS:
1968 kvm_s390_clear_float_irqs(dev->kvm);
1970 case KVM_DEV_FLIC_APF_ENABLE:
1971 dev->kvm->arch.gmap->pfault_enabled = 1;
1973 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1974 dev->kvm->arch.gmap->pfault_enabled = 0;
1976 * Make sure no async faults are in transition when
1977 * clearing the queues. So we don't need to worry
1978 * about late coming workers.
1980 synchronize_srcu(&dev->kvm->srcu);
1981 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1982 kvm_clear_async_pf_completion_queue(vcpu);
1984 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1985 r = register_io_adapter(dev, attr);
1987 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1988 r = modify_io_adapter(dev, attr);
1997 static int flic_create(struct kvm_device *dev, u32 type)
2001 if (dev->kvm->arch.flic)
2003 dev->kvm->arch.flic = dev;
2007 static void flic_destroy(struct kvm_device *dev)
2009 dev->kvm->arch.flic = NULL;
2013 /* s390 floating irq controller (flic) */
2014 struct kvm_device_ops kvm_flic_ops = {
2016 .get_attr = flic_get_attr,
2017 .set_attr = flic_set_attr,
2018 .create = flic_create,
2019 .destroy = flic_destroy,
2022 static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2026 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2028 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2031 static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2034 struct s390_map_info *map;
2039 list_for_each_entry(map, &adapter->maps, list) {
2040 if (map->guest_addr == addr)
2046 static int adapter_indicators_set(struct kvm *kvm,
2047 struct s390_io_adapter *adapter,
2048 struct kvm_s390_adapter_int *adapter_int)
2051 int summary_set, idx;
2052 struct s390_map_info *info;
2055 info = get_map_info(adapter, adapter_int->ind_addr);
2058 map = page_address(info->page);
2059 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2061 idx = srcu_read_lock(&kvm->srcu);
2062 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2063 set_page_dirty_lock(info->page);
2064 info = get_map_info(adapter, adapter_int->summary_addr);
2066 srcu_read_unlock(&kvm->srcu, idx);
2069 map = page_address(info->page);
2070 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2072 summary_set = test_and_set_bit(bit, map);
2073 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2074 set_page_dirty_lock(info->page);
2075 srcu_read_unlock(&kvm->srcu, idx);
2076 return summary_set ? 0 : 1;
2080 * < 0 - not injected due to error
2081 * = 0 - coalesced, summary indicator already active
2082 * > 0 - injected interrupt
2084 static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2085 struct kvm *kvm, int irq_source_id, int level,
2089 struct s390_io_adapter *adapter;
2091 /* We're only interested in the 0->1 transition. */
2094 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2097 down_read(&adapter->maps_lock);
2098 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2099 up_read(&adapter->maps_lock);
2100 if ((ret > 0) && !adapter->masked) {
2101 struct kvm_s390_interrupt s390int = {
2102 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2104 .parm64 = (adapter->isc << 27) | 0x80000000,
2106 ret = kvm_s390_inject_vm(kvm, &s390int);
2113 int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
2114 const struct kvm_irq_routing_entry *ue)
2119 case KVM_IRQ_ROUTING_S390_ADAPTER:
2120 e->set = set_adapter_int;
2121 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2122 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2123 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2124 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2125 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2135 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2136 int irq_source_id, int level, bool line_status)
2141 int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2143 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2144 struct kvm_s390_irq *buf;
2152 if (copy_from_user((void *) buf, irqstate, len)) {
2158 * Don't allow setting the interrupt state
2159 * when there are already interrupts pending
2161 spin_lock(&li->lock);
2162 if (li->pending_irqs) {
2167 for (n = 0; n < len / sizeof(*buf); n++) {
2168 r = do_inject_vcpu(vcpu, &buf[n]);
2174 spin_unlock(&li->lock);
2181 static void store_local_irq(struct kvm_s390_local_interrupt *li,
2182 struct kvm_s390_irq *irq,
2183 unsigned long irq_type)
2186 case IRQ_PEND_MCHK_EX:
2187 case IRQ_PEND_MCHK_REP:
2188 irq->type = KVM_S390_MCHK;
2189 irq->u.mchk = li->irq.mchk;
2192 irq->type = KVM_S390_PROGRAM_INT;
2193 irq->u.pgm = li->irq.pgm;
2195 case IRQ_PEND_PFAULT_INIT:
2196 irq->type = KVM_S390_INT_PFAULT_INIT;
2197 irq->u.ext = li->irq.ext;
2199 case IRQ_PEND_EXT_EXTERNAL:
2200 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2201 irq->u.extcall = li->irq.extcall;
2203 case IRQ_PEND_EXT_CLOCK_COMP:
2204 irq->type = KVM_S390_INT_CLOCK_COMP;
2206 case IRQ_PEND_EXT_CPU_TIMER:
2207 irq->type = KVM_S390_INT_CPU_TIMER;
2209 case IRQ_PEND_SIGP_STOP:
2210 irq->type = KVM_S390_SIGP_STOP;
2211 irq->u.stop = li->irq.stop;
2213 case IRQ_PEND_RESTART:
2214 irq->type = KVM_S390_RESTART;
2216 case IRQ_PEND_SET_PREFIX:
2217 irq->type = KVM_S390_SIGP_SET_PREFIX;
2218 irq->u.prefix = li->irq.prefix;
2223 int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2225 uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
2226 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2227 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2228 unsigned long pending_irqs;
2229 struct kvm_s390_irq irq;
2230 unsigned long irq_type;
2234 spin_lock(&li->lock);
2235 pending_irqs = li->pending_irqs;
2236 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2237 sizeof(sigp_emerg_pending));
2238 spin_unlock(&li->lock);
2240 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2241 memset(&irq, 0, sizeof(irq));
2242 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2244 if (n + sizeof(irq) > len)
2246 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2247 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2252 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2253 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2254 memset(&irq, 0, sizeof(irq));
2255 if (n + sizeof(irq) > len)
2257 irq.type = KVM_S390_INT_EMERGENCY;
2258 irq.u.emerg.code = cpuaddr;
2259 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2265 if ((sigp_ctrl & SIGP_CTRL_C) &&
2266 (atomic_read(&vcpu->arch.sie_block->cpuflags) &
2267 CPUSTAT_ECALL_PEND)) {
2268 if (n + sizeof(irq) > len)
2270 memset(&irq, 0, sizeof(irq));
2271 irq.type = KVM_S390_INT_EXTERNAL_CALL;
2272 irq.u.extcall.code = sigp_ctrl & SIGP_CTRL_SCN_MASK;
2273 if (copy_to_user(&buf[n], &irq, sizeof(irq)))