blob: 6d7513e1556082f5f860ef9ebe8cdbe875d5db17 [file] [log] [blame]
Carsten Otteba5c1e92008-03-25 18:47:26 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling kvm guest interrupts
Carsten Otteba5c1e92008-03-25 18:47:26 +01003 *
Cornelia Huck841b91c2013-07-15 13:36:01 +02004 * Copyright IBM Corp. 2008,2014
Carsten Otteba5c1e92008-03-25 18:47:26 +01005 *
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.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 */
12
Christian Borntraegerca872302009-05-12 17:21:49 +020013#include <linux/interrupt.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010014#include <linux/kvm_host.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010015#include <linux/hrtimer.h>
Cornelia Huck84223592013-07-15 13:36:01 +020016#include <linux/mmu_context.h>
Christian Borntraeger3cd61292008-07-25 15:51:54 +020017#include <linux/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Jens Freimann383d0b02014-07-29 15:11:49 +020019#include <linux/bitmap.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010020#include <asm/asm-offsets.h>
21#include <asm/uaccess.h>
David Hildenbrandea5f4962014-10-14 15:29:30 +020022#include <asm/sclp.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010023#include "kvm-s390.h"
24#include "gaccess.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020025#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010026
Cornelia Huckd8346b72012-12-20 15:32:08 +010027#define IOINT_SCHID_MASK 0x0000ffff
28#define IOINT_SSID_MASK 0x00030000
29#define IOINT_CSSID_MASK 0x03fc0000
30#define IOINT_AI_MASK 0x04000000
Jens Freimann44c6ca32014-04-16 13:57:18 +020031#define PFAULT_INIT 0x0600
Jens Freimann60f90a12014-11-10 17:20:07 +010032#define PFAULT_DONE 0x0680
33#define VIRTIO_PARAM 0x0d00
Cornelia Huckd8346b72012-12-20 15:32:08 +010034
35static int is_ioint(u64 type)
36{
37 return ((type & 0xfffe0000u) != 0xfffe0000u);
38}
39
Dominik Dingel3c038e62013-10-07 17:11:48 +020040int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010041{
42 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
43}
44
Cornelia Huckd8346b72012-12-20 15:32:08 +010045static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
46{
47 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
48}
49
Cornelia Huck48a3e952012-12-20 15:32:09 +010050static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
51{
52 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
53}
54
Carsten Otteba5c1e92008-03-25 18:47:26 +010055static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
56{
57 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
58 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
59 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
60 return 0;
61 return 1;
62}
63
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010064static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
65{
66 if (psw_extint_disabled(vcpu) ||
67 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
68 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +010069 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
70 /* No timer interrupts when single stepping */
71 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010072 return 1;
73}
74
Cornelia Huck79fd50c2013-02-07 13:20:52 +010075static u64 int_word_to_isc_bits(u32 int_word)
76{
77 u8 isc = (int_word & 0x38000000) >> 27;
78
79 return (0x80 >> isc) << 24;
80}
81
Christian Borntraeger614aeab2014-08-25 12:27:29 +020082static int __must_check __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020083 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010084{
85 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020086 case KVM_S390_INT_EXTERNAL_CALL:
87 if (psw_extint_disabled(vcpu))
88 return 0;
89 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
90 return 1;
Christian Borntraegerf3460262014-09-03 16:21:32 +020091 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +010092 case KVM_S390_INT_EMERGENCY:
93 if (psw_extint_disabled(vcpu))
94 return 0;
95 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
96 return 1;
97 return 0;
Thomas Huthe029ae52014-03-26 16:11:54 +010098 case KVM_S390_INT_CLOCK_COMP:
99 return ckc_interrupts_enabled(vcpu);
100 case KVM_S390_INT_CPU_TIMER:
101 if (psw_extint_disabled(vcpu))
102 return 0;
103 if (vcpu->arch.sie_block->gcr[0] & 0x400ul)
104 return 1;
105 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100106 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200107 case KVM_S390_INT_PFAULT_INIT:
108 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100109 case KVM_S390_INT_VIRTIO:
110 if (psw_extint_disabled(vcpu))
111 return 0;
112 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
113 return 1;
114 return 0;
115 case KVM_S390_PROGRAM_INT:
116 case KVM_S390_SIGP_STOP:
117 case KVM_S390_SIGP_SET_PREFIX:
118 case KVM_S390_RESTART:
119 return 1;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100120 case KVM_S390_MCHK:
121 if (psw_mchk_disabled(vcpu))
122 return 0;
123 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
124 return 1;
125 return 0;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100126 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
127 if (psw_ioint_disabled(vcpu))
128 return 0;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100129 if (vcpu->arch.sie_block->gcr[6] &
130 int_word_to_isc_bits(inti->io.io_int_word))
Cornelia Huckd8346b72012-12-20 15:32:08 +0100131 return 1;
132 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100133 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100134 printk(KERN_WARNING "illegal interrupt type %llx\n",
135 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100136 BUG();
137 }
138 return 0;
139}
140
Jens Freimann383d0b02014-07-29 15:11:49 +0200141static inline unsigned long pending_local_irqs(struct kvm_vcpu *vcpu)
142{
143 return vcpu->arch.local_int.pending_irqs;
144}
145
146static unsigned long deliverable_local_irqs(struct kvm_vcpu *vcpu)
147{
148 unsigned long active_mask = pending_local_irqs(vcpu);
149
150 if (psw_extint_disabled(vcpu))
151 active_mask &= ~IRQ_PEND_EXT_MASK;
152 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
153 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
154 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
155 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
156 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
157 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
158 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
159 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
160 if (psw_mchk_disabled(vcpu))
161 active_mask &= ~IRQ_PEND_MCHK_MASK;
162
David Hildenbrand6cddd432014-10-15 16:48:53 +0200163 /*
164 * STOP irqs will never be actively delivered. They are triggered via
165 * intercept requests and cleared when the stop intercept is performed.
166 */
167 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
168
Jens Freimann383d0b02014-07-29 15:11:49 +0200169 return active_mask;
170}
171
Carsten Otteba5c1e92008-03-25 18:47:26 +0100172static void __set_cpu_idle(struct kvm_vcpu *vcpu)
173{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100174 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
175 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
176}
177
178static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
179{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100180 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
181 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
182}
183
184static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
185{
David Hildenbrand49539192014-02-21 08:59:59 +0100186 atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
187 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100188 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100189 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
190
191 if (guestdbg_enabled(vcpu)) {
192 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
193 LCTL_CR10 | LCTL_CR11);
194 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
195 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100196}
197
198static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
199{
200 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
201}
202
Jens Freimann383d0b02014-07-29 15:11:49 +0200203static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
204{
205 if (!(pending_local_irqs(vcpu) & IRQ_PEND_EXT_MASK))
206 return;
207 if (psw_extint_disabled(vcpu))
208 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
209 else
210 vcpu->arch.sie_block->lctl |= LCTL_CR0;
211}
212
213static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
214{
215 if (!(pending_local_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
216 return;
217 if (psw_mchk_disabled(vcpu))
218 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
219 else
220 vcpu->arch.sie_block->lctl |= LCTL_CR14;
221}
222
David Hildenbrand6cddd432014-10-15 16:48:53 +0200223static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
224{
225 if (kvm_s390_is_stop_irq_pending(vcpu))
226 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
227}
228
Jens Freimann383d0b02014-07-29 15:11:49 +0200229/* Set interception request for non-deliverable local interrupts */
230static void set_intercept_indicators_local(struct kvm_vcpu *vcpu)
231{
232 set_intercept_indicators_ext(vcpu);
233 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200234 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200235}
236
Carsten Otteba5c1e92008-03-25 18:47:26 +0100237static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200238 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100239{
240 switch (inti->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100241 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200242 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100243 case KVM_S390_INT_VIRTIO:
244 if (psw_extint_disabled(vcpu))
245 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
246 else
247 vcpu->arch.sie_block->lctl |= LCTL_CR0;
248 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100249 case KVM_S390_MCHK:
250 if (psw_mchk_disabled(vcpu))
251 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
252 else
253 vcpu->arch.sie_block->lctl |= LCTL_CR14;
254 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100255 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
256 if (psw_ioint_disabled(vcpu))
257 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
258 else
259 vcpu->arch.sie_block->lctl |= LCTL_CR6;
260 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100261 default:
262 BUG();
263 }
264}
265
Jens Freimann8a2ef712014-07-23 16:36:06 +0200266static u16 get_ilc(struct kvm_vcpu *vcpu)
267{
268 const unsigned short table[] = { 2, 4, 4, 6 };
269
270 switch (vcpu->arch.sie_block->icptcode) {
271 case ICPT_INST:
272 case ICPT_INSTPROGI:
273 case ICPT_OPEREXC:
274 case ICPT_PARTEXEC:
275 case ICPT_IOINST:
276 /* last instruction only stored for these icptcodes */
277 return table[vcpu->arch.sie_block->ipa >> 14];
278 case ICPT_PROGI:
279 return vcpu->arch.sie_block->pgmilc;
280 default:
281 return 0;
282 }
283}
284
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200285static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100286{
Jens Freimann383d0b02014-07-29 15:11:49 +0200287 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200288 int rc;
289
290 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
291 0, 0);
292
293 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
294 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100295 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200296 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
297 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
298 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
299 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200300 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100301 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200302}
303
304static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
305{
Jens Freimann383d0b02014-07-29 15:11:49 +0200306 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200307 int rc;
308
309 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
310 0, 0);
311
312 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
313 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100314 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200315 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
316 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
317 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
318 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200319 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100320 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200321}
322
Jens Freimann383d0b02014-07-29 15:11:49 +0200323static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200324{
Jens Freimann383d0b02014-07-29 15:11:49 +0200325 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
326 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200327 int rc;
328
Jens Freimann383d0b02014-07-29 15:11:49 +0200329 spin_lock(&li->lock);
330 ext = li->irq.ext;
331 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
332 li->irq.ext.ext_params2 = 0;
333 spin_unlock(&li->lock);
334
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200335 VCPU_EVENT(vcpu, 4, "interrupt: pfault init parm:%x,parm64:%llx",
Jens Freimann383d0b02014-07-29 15:11:49 +0200336 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200337 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
338 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200339 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200340
341 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
342 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
343 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
344 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
345 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
346 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200347 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100348 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200349}
350
Jens Freimann383d0b02014-07-29 15:11:49 +0200351static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200352{
Jens Freimann383d0b02014-07-29 15:11:49 +0200353 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
354 struct kvm_s390_mchk_info mchk;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200355 int rc;
356
Jens Freimann383d0b02014-07-29 15:11:49 +0200357 spin_lock(&li->lock);
358 mchk = li->irq.mchk;
359 /*
360 * If there was an exigent machine check pending, then any repressible
361 * machine checks that might have been pending are indicated along
362 * with it, so always clear both bits
363 */
364 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
365 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
366 memset(&li->irq.mchk, 0, sizeof(mchk));
367 spin_unlock(&li->lock);
368
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200369 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
Jens Freimann383d0b02014-07-29 15:11:49 +0200370 mchk.mcic);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200371 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_MCHK,
Jens Freimann383d0b02014-07-29 15:11:49 +0200372 mchk.cr14, mchk.mcic);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200373
374 rc = kvm_s390_vcpu_store_status(vcpu, KVM_S390_STORE_STATUS_PREFIXED);
Jens Freimann383d0b02014-07-29 15:11:49 +0200375 rc |= put_guest_lc(vcpu, mchk.mcic,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200376 (u64 __user *) __LC_MCCK_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200377 rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200378 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
379 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
Jens Freimann383d0b02014-07-29 15:11:49 +0200380 &mchk.fixed_logout, sizeof(mchk.fixed_logout));
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200381 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
382 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
383 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
384 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100385 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200386}
387
388static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
389{
Jens Freimann383d0b02014-07-29 15:11:49 +0200390 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200391 int rc;
392
393 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
394 vcpu->stat.deliver_restart_signal++;
395 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
396
397 rc = write_guest_lc(vcpu,
398 offsetof(struct _lowcore, restart_old_psw),
399 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
400 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
401 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200402 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100403 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200404}
405
Jens Freimann383d0b02014-07-29 15:11:49 +0200406static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200407{
Jens Freimann383d0b02014-07-29 15:11:49 +0200408 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
409 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200410
Jens Freimann383d0b02014-07-29 15:11:49 +0200411 spin_lock(&li->lock);
412 prefix = li->irq.prefix;
413 li->irq.prefix.address = 0;
414 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
415 spin_unlock(&li->lock);
416
417 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200418 vcpu->stat.deliver_prefix_signal++;
419 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
420 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200421 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200422
Jens Freimann383d0b02014-07-29 15:11:49 +0200423 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200424 return 0;
425}
426
Jens Freimann383d0b02014-07-29 15:11:49 +0200427static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200428{
Jens Freimann383d0b02014-07-29 15:11:49 +0200429 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200430 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200431 int cpu_addr;
432
433 spin_lock(&li->lock);
434 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
435 clear_bit(cpu_addr, li->sigp_emerg_pending);
436 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
437 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
438 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200439
440 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
441 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200442 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
443 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200444
445 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
446 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200447 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200448 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
449 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
450 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
451 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100452 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200453}
454
Jens Freimann383d0b02014-07-29 15:11:49 +0200455static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200456{
Jens Freimann383d0b02014-07-29 15:11:49 +0200457 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
458 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200459 int rc;
460
Jens Freimann383d0b02014-07-29 15:11:49 +0200461 spin_lock(&li->lock);
462 extcall = li->irq.extcall;
463 li->irq.extcall.code = 0;
464 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
465 spin_unlock(&li->lock);
466
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200467 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
468 vcpu->stat.deliver_external_call++;
469 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
470 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200471 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200472
473 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
474 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200475 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200476 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
477 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
478 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
479 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100480 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200481}
482
Jens Freimann383d0b02014-07-29 15:11:49 +0200483static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200484{
Jens Freimann383d0b02014-07-29 15:11:49 +0200485 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
486 struct kvm_s390_pgm_info pgm_info;
David Hildenbrand87128362014-03-03 10:55:13 +0100487 int rc = 0;
Jens Freimann8a2ef712014-07-23 16:36:06 +0200488 u16 ilc = get_ilc(vcpu);
David Hildenbrand87128362014-03-03 10:55:13 +0100489
Jens Freimann383d0b02014-07-29 15:11:49 +0200490 spin_lock(&li->lock);
491 pgm_info = li->irq.pgm;
492 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
493 memset(&li->irq.pgm, 0, sizeof(pgm_info));
494 spin_unlock(&li->lock);
495
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200496 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
Jens Freimann383d0b02014-07-29 15:11:49 +0200497 pgm_info.code, ilc);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200498 vcpu->stat.deliver_program_int++;
499 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200500 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200501
Jens Freimann383d0b02014-07-29 15:11:49 +0200502 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100503 case PGM_AFX_TRANSLATION:
504 case PGM_ASX_TRANSLATION:
505 case PGM_EX_TRANSLATION:
506 case PGM_LFX_TRANSLATION:
507 case PGM_LSTE_SEQUENCE:
508 case PGM_LSX_TRANSLATION:
509 case PGM_LX_TRANSLATION:
510 case PGM_PRIMARY_AUTHORITY:
511 case PGM_SECONDARY_AUTHORITY:
512 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200513 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100514 (u64 *)__LC_TRANS_EXC_CODE);
515 break;
516 case PGM_ALEN_TRANSLATION:
517 case PGM_ALE_SEQUENCE:
518 case PGM_ASTE_INSTANCE:
519 case PGM_ASTE_SEQUENCE:
520 case PGM_ASTE_VALIDITY:
521 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200522 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100523 (u8 *)__LC_EXC_ACCESS_ID);
524 break;
525 case PGM_ASCE_TYPE:
526 case PGM_PAGE_TRANSLATION:
527 case PGM_REGION_FIRST_TRANS:
528 case PGM_REGION_SECOND_TRANS:
529 case PGM_REGION_THIRD_TRANS:
530 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200531 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100532 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200533 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100534 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200535 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100536 (u8 *)__LC_OP_ACCESS_ID);
537 break;
538 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200539 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200540 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200541 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100542 (u64 *)__LC_MON_CODE);
543 break;
544 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200545 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100546 (u32 *)__LC_DATA_EXC_CODE);
547 break;
548 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200549 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100550 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200551 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100552 (u8 *)__LC_EXC_ACCESS_ID);
553 break;
554 }
555
Jens Freimann383d0b02014-07-29 15:11:49 +0200556 if (pgm_info.code & PGM_PER) {
557 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100558 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200559 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100560 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200561 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100562 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200563 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100564 (u8 *) __LC_PER_ACCESS_ID);
565 }
566
Jens Freimann8a2ef712014-07-23 16:36:06 +0200567 rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
Jens Freimann383d0b02014-07-29 15:11:49 +0200568 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100569 (u16 *)__LC_PGM_INT_CODE);
570 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
571 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
572 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
573 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100574 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200575}
David Hildenbrand87128362014-03-03 10:55:13 +0100576
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200577static int __must_check __deliver_service(struct kvm_vcpu *vcpu,
578 struct kvm_s390_interrupt_info *inti)
579{
580 int rc;
581
582 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
583 inti->ext.ext_params);
584 vcpu->stat.deliver_service_signal++;
585 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
586 inti->ext.ext_params, 0);
587
588 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100589 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200590 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
591 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
592 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
593 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
594 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
595 (u32 *)__LC_EXT_PARAMS);
Jens Freimann99e20002014-12-01 17:05:39 +0100596 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200597}
598
599static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu,
600 struct kvm_s390_interrupt_info *inti)
601{
602 int rc;
603
604 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
605 KVM_S390_INT_PFAULT_DONE, 0,
606 inti->ext.ext_params2);
607
608 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *)__LC_EXT_INT_CODE);
609 rc |= put_guest_lc(vcpu, PFAULT_DONE, (u16 *)__LC_EXT_CPU_ADDR);
610 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
611 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
612 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
613 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
614 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
615 (u64 *)__LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100616 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200617}
618
619static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu,
620 struct kvm_s390_interrupt_info *inti)
621{
622 int rc;
623
624 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
625 inti->ext.ext_params, inti->ext.ext_params2);
626 vcpu->stat.deliver_virtio_interrupt++;
627 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
628 inti->ext.ext_params,
629 inti->ext.ext_params2);
630
631 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *)__LC_EXT_INT_CODE);
632 rc |= put_guest_lc(vcpu, VIRTIO_PARAM, (u16 *)__LC_EXT_CPU_ADDR);
633 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
634 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
635 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
636 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
637 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
638 (u32 *)__LC_EXT_PARAMS);
639 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
640 (u64 *)__LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100641 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200642}
643
644static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
645 struct kvm_s390_interrupt_info *inti)
646{
647 int rc;
648
649 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
650 vcpu->stat.deliver_io_int++;
651 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
652 ((__u32)inti->io.subchannel_id << 16) |
653 inti->io.subchannel_nr,
654 ((__u64)inti->io.io_int_parm << 32) |
655 inti->io.io_int_word);
656
657 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
658 (u16 *)__LC_SUBCHANNEL_ID);
659 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
660 (u16 *)__LC_SUBCHANNEL_NR);
661 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
662 (u32 *)__LC_IO_INT_PARM);
663 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
664 (u32 *)__LC_IO_INT_WORD);
665 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
666 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
667 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
668 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100669 return rc ? -EFAULT : 0;
David Hildenbrand87128362014-03-03 10:55:13 +0100670}
671
Jens Freimann383d0b02014-07-29 15:11:49 +0200672static int __must_check __deliver_mchk_floating(struct kvm_vcpu *vcpu,
673 struct kvm_s390_interrupt_info *inti)
674{
675 struct kvm_s390_mchk_info *mchk = &inti->mchk;
676 int rc;
677
678 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
679 mchk->mcic);
680 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_MCHK,
681 mchk->cr14, mchk->mcic);
682
683 rc = kvm_s390_vcpu_store_status(vcpu, KVM_S390_STORE_STATUS_PREFIXED);
684 rc |= put_guest_lc(vcpu, mchk->mcic,
685 (u64 __user *) __LC_MCCK_CODE);
686 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
687 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
688 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
689 &mchk->fixed_logout, sizeof(mchk->fixed_logout));
690 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
691 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
692 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
693 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100694 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200695}
696
697typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
698
699static const deliver_irq_t deliver_irq_funcs[] = {
700 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
701 [IRQ_PEND_PROG] = __deliver_prog,
702 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
703 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
704 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
705 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
706 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200707 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
708 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
709};
710
711static int __must_check __deliver_floating_interrupt(struct kvm_vcpu *vcpu,
712 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100713{
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200714 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100715
716 switch (inti->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100717 case KVM_S390_INT_SERVICE:
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200718 rc = __deliver_service(vcpu, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100719 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200720 case KVM_S390_INT_PFAULT_DONE:
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200721 rc = __deliver_pfault_done(vcpu, inti);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200722 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100723 case KVM_S390_INT_VIRTIO:
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200724 rc = __deliver_virtio(vcpu, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100725 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100726 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +0200727 rc = __deliver_mchk_floating(vcpu, inti);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100728 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100729 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200730 rc = __deliver_io(vcpu, inti);
Cornelia Huckd8346b72012-12-20 15:32:08 +0100731 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100732 default:
733 BUG();
734 }
Jens Freimann79395032014-04-17 10:10:30 +0200735
736 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100737}
738
David Hildenbrandea5f4962014-10-14 15:29:30 +0200739/* Check whether an external call is pending (deliverable or not) */
740int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100741{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200742 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
743 uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
David Hildenbrand49539192014-02-21 08:59:59 +0100744
David Hildenbrandea5f4962014-10-14 15:29:30 +0200745 if (!sclp_has_sigpif())
746 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100747
David Hildenbrandea5f4962014-10-14 15:29:30 +0200748 return (sigp_ctrl & SIGP_CTRL_C) &&
749 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND);
David Hildenbrand49539192014-02-21 08:59:59 +0100750}
751
David Hildenbrand9a022062014-08-05 17:40:47 +0200752int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100753{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200754 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
755 struct kvm_s390_interrupt_info *inti;
Jens Freimann383d0b02014-07-29 15:11:49 +0200756 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100757
Jens Freimann383d0b02014-07-29 15:11:49 +0200758 rc = !!deliverable_local_irqs(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100759
760 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200761 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100762 list_for_each_entry(inti, &fi->list, list)
763 if (__interrupt_is_deliverable(vcpu, inti)) {
764 rc = 1;
765 break;
766 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200767 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100768 }
769
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100770 if (!rc && kvm_cpu_has_pending_timer(vcpu))
771 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100772
David Hildenbrandea5f4962014-10-14 15:29:30 +0200773 /* external call pending and deliverable */
774 if (!rc && kvm_s390_ext_call_pending(vcpu) &&
775 !psw_extint_disabled(vcpu) &&
776 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand49539192014-02-21 08:59:59 +0100777 rc = 1;
778
David Hildenbrand9a022062014-08-05 17:40:47 +0200779 if (!rc && !exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
David Hildenbrand6cddd432014-10-15 16:48:53 +0200780 rc = 1;
781
Carsten Otteba5c1e92008-03-25 18:47:26 +0100782 return rc;
783}
784
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300785int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
786{
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100787 if (!(vcpu->arch.sie_block->ckc <
788 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
789 return 0;
790 if (!ckc_interrupts_enabled(vcpu))
791 return 0;
792 return 1;
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300793}
794
Carsten Otteba5c1e92008-03-25 18:47:26 +0100795int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
796{
797 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100798
799 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100800
David Hildenbrand0759d062014-05-13 16:54:32 +0200801 /* fast path */
802 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
803 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200804
Carsten Otteba5c1e92008-03-25 18:47:26 +0100805 if (psw_interrupts_disabled(vcpu)) {
806 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100807 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100808 }
809
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100810 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100811 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +0100812 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100813 goto no_timer;
814 }
815
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200816 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Heiko Carstensed4f2092013-01-14 16:55:55 +0100817 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
David Hildenbrandbda343e2014-12-12 12:26:40 +0100818
819 /* underflow */
820 if (vcpu->arch.sie_block->ckc < now)
821 return 0;
822
823 __set_cpu_idle(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200824 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
825 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100826no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200827 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200828 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100829 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200830 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
831
David Hildenbrand2d00f752014-12-11 10:18:01 +0100832 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100833 return 0;
834}
835
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200836void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
837{
838 if (waitqueue_active(&vcpu->wq)) {
839 /*
840 * The vcpu gave up the cpu voluntarily, mark it as a good
841 * yield-candidate.
842 */
843 vcpu->preempted = true;
844 wake_up_interruptible(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +0200845 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200846 }
847}
848
Christian Borntraegerca872302009-05-12 17:21:49 +0200849enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
850{
851 struct kvm_vcpu *vcpu;
David Hildenbrand2d00f752014-12-11 10:18:01 +0100852 u64 now, sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +0200853
854 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrand2d00f752014-12-11 10:18:01 +0100855 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
856 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200857
David Hildenbrand2d00f752014-12-11 10:18:01 +0100858 /*
859 * If the monotonic clock runs faster than the tod clock we might be
860 * woken up too early and have to go back to sleep to avoid deadlocks.
861 */
862 if (vcpu->arch.sie_block->ckc > now &&
863 hrtimer_forward_now(timer, ns_to_ktime(sltime)))
864 return HRTIMER_RESTART;
865 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200866 return HRTIMER_NORESTART;
867}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100868
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100869void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
870{
871 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100872
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200873 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200874 li->pending_irqs = 0;
875 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
876 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200877 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100878
879 /* clear pending external calls set by sigp interpretation facility */
Jens Freimann383d0b02014-07-29 15:11:49 +0200880 atomic_clear_mask(CPUSTAT_ECALL_PEND, li->cpuflags);
David Hildenbrandea5f4962014-10-14 15:29:30 +0200881 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl = 0;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100882}
883
Christian Borntraeger614aeab2014-08-25 12:27:29 +0200884int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100885{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200886 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
887 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
888 struct kvm_s390_interrupt_info *n, *inti = NULL;
Jens Freimann383d0b02014-07-29 15:11:49 +0200889 deliver_irq_t func;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100890 int deliver;
Jens Freimann79395032014-04-17 10:10:30 +0200891 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200892 unsigned long irq_type;
893 unsigned long deliverable_irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100894
895 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100896
Jens Freimann383d0b02014-07-29 15:11:49 +0200897 /* pending ckc conditions might have been invalidated */
898 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
899 if (kvm_cpu_has_pending_timer(vcpu))
900 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
901
902 do {
903 deliverable_irqs = deliverable_local_irqs(vcpu);
904 /* bits are in the order of interrupt priority */
905 irq_type = find_first_bit(&deliverable_irqs, IRQ_PEND_COUNT);
906 if (irq_type == IRQ_PEND_COUNT)
907 break;
908 func = deliver_irq_funcs[irq_type];
909 if (!func) {
910 WARN_ON_ONCE(func == NULL);
911 clear_bit(irq_type, &li->pending_irqs);
912 continue;
913 }
914 rc = func(vcpu);
915 } while (!rc && irq_type != IRQ_PEND_COUNT);
916
917 set_intercept_indicators_local(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100918
Jens Freimann79395032014-04-17 10:10:30 +0200919 if (!rc && atomic_read(&fi->active)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100920 do {
921 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200922 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100923 list_for_each_entry_safe(inti, n, &fi->list, list) {
924 if (__interrupt_is_deliverable(vcpu, inti)) {
925 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100926 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100927 deliver = 1;
928 break;
929 }
930 __set_intercept_indicator(vcpu, inti);
931 }
932 if (list_empty(&fi->list))
933 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200934 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100935 if (deliver) {
Jens Freimann383d0b02014-07-29 15:11:49 +0200936 rc = __deliver_floating_interrupt(vcpu, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100937 kfree(inti);
938 }
Jens Freimann79395032014-04-17 10:10:30 +0200939 } while (!rc && deliver);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100940 }
Jens Freimann79395032014-04-17 10:10:30 +0200941
942 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100943}
944
Jens Freimann383d0b02014-07-29 15:11:49 +0200945static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +0200946{
947 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
948
Jens Freimann383d0b02014-07-29 15:11:49 +0200949 li->irq.pgm = irq->u.pgm;
Jens Freimann91851242014-12-01 16:43:40 +0100950 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +0200951 return 0;
952}
953
Carsten Otteba5c1e92008-03-25 18:47:26 +0100954int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
955{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200956 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200957 struct kvm_s390_irq irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100958
959 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Jens Freimann383d0b02014-07-29 15:11:49 +0200960 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, code,
961 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200962 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200963 irq.u.pgm.code = code;
964 __inject_prog(vcpu, &irq);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200965 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200966 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100967 return 0;
968}
969
Jens Freimannbcd84682014-02-11 11:07:05 +0100970int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
971 struct kvm_s390_pgm_info *pgm_info)
972{
973 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200974 struct kvm_s390_irq irq;
Jens Freimann0146a7b2014-07-28 15:37:58 +0200975 int rc;
Jens Freimannbcd84682014-02-11 11:07:05 +0100976
Jens Freimannbcd84682014-02-11 11:07:05 +0100977 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
978 pgm_info->code);
979 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
980 pgm_info->code, 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200981 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200982 irq.u.pgm = *pgm_info;
983 rc = __inject_prog(vcpu, &irq);
Jens Freimannbcd84682014-02-11 11:07:05 +0100984 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200985 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +0200986 return rc;
987}
988
Jens Freimann383d0b02014-07-29 15:11:49 +0200989static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +0200990{
991 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
992
Jens Freimann383d0b02014-07-29 15:11:49 +0200993 VCPU_EVENT(vcpu, 3, "inject: external irq params:%x, params2:%llx",
994 irq->u.ext.ext_params, irq->u.ext.ext_params2);
995 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
996 irq->u.ext.ext_params,
997 irq->u.ext.ext_params2, 2);
998
999 li->irq.ext = irq->u.ext;
1000 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001001 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1002 return 0;
1003}
1004
David Hildenbrandea5f4962014-10-14 15:29:30 +02001005static int __inject_extcall_sigpif(struct kvm_vcpu *vcpu, uint16_t src_id)
1006{
1007 unsigned char new_val, old_val;
1008 uint8_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
1009
1010 new_val = SIGP_CTRL_C | (src_id & SIGP_CTRL_SCN_MASK);
1011 old_val = *sigp_ctrl & ~SIGP_CTRL_C;
1012 if (cmpxchg(sigp_ctrl, old_val, new_val) != old_val) {
1013 /* another external call is pending */
1014 return -EBUSY;
1015 }
1016 atomic_set_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
1017 return 0;
1018}
1019
Christian Borntraeger0675d922015-01-15 12:40:42 +01001020static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001021{
1022 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001023 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001024 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001025
1026 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001027 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001028 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbrandea5f4962014-10-14 15:29:30 +02001029 src_id, 0, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001030
David Hildenbrandea5f4962014-10-14 15:29:30 +02001031 /* sending vcpu invalid */
1032 if (src_id >= KVM_MAX_VCPUS ||
1033 kvm_get_vcpu(vcpu->kvm, src_id) == NULL)
1034 return -EINVAL;
1035
1036 if (sclp_has_sigpif())
1037 return __inject_extcall_sigpif(vcpu, src_id);
1038
1039 if (!test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
1040 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001041 *extcall = irq->u.extcall;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001042 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1043 return 0;
1044}
1045
Jens Freimann383d0b02014-07-29 15:11:49 +02001046static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001047{
1048 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001049 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001050
1051 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001052 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001053 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
Jens Freimann556cc0d2014-12-18 15:52:21 +01001054 irq->u.prefix.address, 0, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001055
David Hildenbranda3a9c592014-10-14 09:44:55 +02001056 if (!is_vcpu_stopped(vcpu))
1057 return -EBUSY;
1058
Jens Freimann383d0b02014-07-29 15:11:49 +02001059 *prefix = irq->u.prefix;
1060 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001061 return 0;
1062}
1063
David Hildenbrand6cddd432014-10-15 16:48:53 +02001064#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001065static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001066{
1067 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001068 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001069 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001070
Jens Freimann383d0b02014-07-29 15:11:49 +02001071 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0, 2);
1072
David Hildenbrand28225452014-10-15 16:48:16 +02001073 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1074 return -EINVAL;
1075
David Hildenbrand6cddd432014-10-15 16:48:53 +02001076 if (is_vcpu_stopped(vcpu)) {
1077 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1078 rc = kvm_s390_store_status_unloaded(vcpu,
1079 KVM_S390_STORE_STATUS_NOADDR);
1080 return rc;
1081 }
1082
1083 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1084 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001085 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001086 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001087 return 0;
1088}
1089
1090static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001091 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001092{
1093 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1094
Jens Freimann383d0b02014-07-29 15:11:49 +02001095 VCPU_EVENT(vcpu, 3, "inject: restart type %llx", irq->type);
1096 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0, 2);
1097
1098 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001099 return 0;
1100}
1101
1102static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001103 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001104{
1105 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1106
Jens Freimann383d0b02014-07-29 15:11:49 +02001107 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n",
1108 irq->u.emerg.code);
1109 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
Jens Freimann49538d12014-12-18 15:48:14 +01001110 irq->u.emerg.code, 0, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001111
Jens Freimann49538d12014-12-18 15:48:14 +01001112 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001113 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001114 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1115 return 0;
1116}
1117
Jens Freimann383d0b02014-07-29 15:11:49 +02001118static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001119{
1120 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001121 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001122
1123 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001124 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001125 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
Jens Freimann556cc0d2014-12-18 15:52:21 +01001126 irq->u.mchk.mcic, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001127
1128 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001129 * Because repressible machine checks can be indicated along with
1130 * exigent machine checks (PoP, Chapter 11, Interruption action)
1131 * we need to combine cr14, mcic and external damage code.
1132 * Failing storage address and the logout area should not be or'ed
1133 * together, we just indicate the last occurrence of the corresponding
1134 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001135 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001136 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001137 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001138 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1139 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1140 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1141 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001142 if (mchk->mcic & MCHK_EX_MASK)
1143 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1144 else if (mchk->mcic & MCHK_REP_MASK)
1145 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001146 return 0;
1147}
1148
Jens Freimann383d0b02014-07-29 15:11:49 +02001149static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001150{
1151 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1152
Jens Freimann383d0b02014-07-29 15:11:49 +02001153 VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CLOCK_COMP);
1154 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
1155 0, 0, 2);
1156
1157 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001158 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1159 return 0;
1160}
1161
Jens Freimann383d0b02014-07-29 15:11:49 +02001162static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001163{
1164 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1165
Jens Freimann383d0b02014-07-29 15:11:49 +02001166 VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CPU_TIMER);
1167 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
1168 0, 0, 2);
1169
1170 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001171 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001172 return 0;
1173}
1174
Jens Freimann383d0b02014-07-29 15:11:49 +02001175
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001176struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
1177 u64 cr6, u64 schid)
1178{
1179 struct kvm_s390_float_interrupt *fi;
1180 struct kvm_s390_interrupt_info *inti, *iter;
1181
1182 if ((!schid && !cr6) || (schid && cr6))
1183 return NULL;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001184 fi = &kvm->arch.float_int;
1185 spin_lock(&fi->lock);
1186 inti = NULL;
1187 list_for_each_entry(iter, &fi->list, list) {
1188 if (!is_ioint(iter->type))
1189 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +01001190 if (cr6 &&
1191 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001192 continue;
1193 if (schid) {
1194 if (((schid & 0x00000000ffff0000) >> 16) !=
1195 iter->io.subchannel_id)
1196 continue;
1197 if ((schid & 0x000000000000ffff) !=
1198 iter->io.subchannel_nr)
1199 continue;
1200 }
1201 inti = iter;
1202 break;
1203 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001204 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001205 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001206 fi->irq_count--;
1207 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001208 if (list_empty(&fi->list))
1209 atomic_set(&fi->active, 0);
1210 spin_unlock(&fi->lock);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001211 return inti;
1212}
1213
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001214static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001215{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001216 struct kvm_s390_local_interrupt *li;
1217 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +02001218 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +01001219 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001220 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001221 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001222
Carsten Otteba5c1e92008-03-25 18:47:26 +01001223 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +02001224 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001225 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
1226 rc = -EINVAL;
1227 goto unlock_fi;
1228 }
1229 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +02001230 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +01001231 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +02001232 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +01001233 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
1234
Cornelia Huckd8346b72012-12-20 15:32:08 +01001235 /* Keep I/O interrupts sorted in isc order. */
1236 list_for_each_entry(iter, &fi->list, list) {
1237 if (!is_ioint(iter->type))
1238 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +01001239 if (int_word_to_isc_bits(iter->io.io_int_word)
1240 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +01001241 continue;
1242 break;
1243 }
1244 list_add_tail(&inti->list, &iter->list);
1245 }
Carsten Otteba5c1e92008-03-25 18:47:26 +01001246 atomic_set(&fi->active, 1);
1247 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
1248 if (sigcpu == KVM_MAX_VCPUS) {
1249 do {
1250 sigcpu = fi->next_rr_cpu++;
1251 if (sigcpu == KVM_MAX_VCPUS)
1252 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +01001253 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001254 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +01001255 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1256 li = &dst_vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001257 spin_lock(&li->lock);
Christian Borntraegerda00fcb2014-11-21 09:38:12 +01001258 switch (inti->type) {
1259 case KVM_S390_MCHK:
1260 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
1261 break;
1262 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1263 atomic_set_mask(CPUSTAT_IO_INT, li->cpuflags);
1264 break;
1265 default:
1266 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1267 break;
1268 }
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001269 spin_unlock(&li->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001270 kvm_s390_vcpu_wakeup(kvm_get_vcpu(kvm, sigcpu));
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001271unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +02001272 spin_unlock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001273 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001274}
1275
1276int kvm_s390_inject_vm(struct kvm *kvm,
1277 struct kvm_s390_interrupt *s390int)
1278{
1279 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001280 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001281
1282 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1283 if (!inti)
1284 return -ENOMEM;
1285
1286 inti->type = s390int->type;
1287 switch (inti->type) {
1288 case KVM_S390_INT_VIRTIO:
1289 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1290 s390int->parm, s390int->parm64);
1291 inti->ext.ext_params = s390int->parm;
1292 inti->ext.ext_params2 = s390int->parm64;
1293 break;
1294 case KVM_S390_INT_SERVICE:
1295 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
1296 inti->ext.ext_params = s390int->parm;
1297 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001298 case KVM_S390_INT_PFAULT_DONE:
1299 inti->type = s390int->type;
1300 inti->ext.ext_params2 = s390int->parm64;
1301 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001302 case KVM_S390_MCHK:
1303 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
1304 s390int->parm64);
1305 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1306 inti->mchk.mcic = s390int->parm64;
1307 break;
1308 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1309 if (inti->type & IOINT_AI_MASK)
1310 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1311 else
1312 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1313 s390int->type & IOINT_CSSID_MASK,
1314 s390int->type & IOINT_SSID_MASK,
1315 s390int->type & IOINT_SCHID_MASK);
1316 inti->io.subchannel_id = s390int->parm >> 16;
1317 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1318 inti->io.io_int_parm = s390int->parm64 >> 32;
1319 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1320 break;
1321 default:
1322 kfree(inti);
1323 return -EINVAL;
1324 }
1325 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1326 2);
1327
David Hildenbrand428d53b2015-01-16 12:58:09 +01001328 rc = __inject_vm(kvm, inti);
1329 if (rc)
1330 kfree(inti);
1331 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001332}
1333
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001334void kvm_s390_reinject_io_int(struct kvm *kvm,
1335 struct kvm_s390_interrupt_info *inti)
1336{
1337 __inject_vm(kvm, inti);
1338}
1339
Jens Freimann383d0b02014-07-29 15:11:49 +02001340int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1341 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001342{
Jens Freimann383d0b02014-07-29 15:11:49 +02001343 irq->type = s390int->type;
1344 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001345 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001346 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001347 return -EINVAL;
1348 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001349 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001350 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001351 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001352 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001353 case KVM_S390_SIGP_STOP:
1354 irq->u.stop.flags = s390int->parm;
1355 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001356 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann383d0b02014-07-29 15:11:49 +02001357 if (irq->u.extcall.code & 0xffff0000)
1358 return -EINVAL;
1359 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001360 break;
1361 case KVM_S390_INT_EMERGENCY:
Jens Freimann383d0b02014-07-29 15:11:49 +02001362 if (irq->u.emerg.code & 0xffff0000)
1363 return -EINVAL;
1364 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001365 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001366 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001367 irq->u.mchk.mcic = s390int->parm64;
1368 break;
1369 }
1370 return 0;
1371}
1372
David Hildenbrand6cddd432014-10-15 16:48:53 +02001373int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1374{
1375 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1376
1377 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1378}
1379
1380void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1381{
1382 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1383
1384 spin_lock(&li->lock);
1385 li->irq.stop.flags = 0;
1386 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1387 spin_unlock(&li->lock);
1388}
1389
Jens Freimann383d0b02014-07-29 15:11:49 +02001390int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1391{
1392 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1393 int rc;
1394
1395 spin_lock(&li->lock);
1396 switch (irq->type) {
1397 case KVM_S390_PROGRAM_INT:
1398 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
1399 irq->u.pgm.code);
1400 rc = __inject_prog(vcpu, irq);
1401 break;
1402 case KVM_S390_SIGP_SET_PREFIX:
1403 rc = __inject_set_prefix(vcpu, irq);
1404 break;
1405 case KVM_S390_SIGP_STOP:
1406 rc = __inject_sigp_stop(vcpu, irq);
1407 break;
1408 case KVM_S390_RESTART:
1409 rc = __inject_sigp_restart(vcpu, irq);
1410 break;
1411 case KVM_S390_INT_CLOCK_COMP:
1412 rc = __inject_ckc(vcpu);
1413 break;
1414 case KVM_S390_INT_CPU_TIMER:
1415 rc = __inject_cpu_timer(vcpu);
1416 break;
1417 case KVM_S390_INT_EXTERNAL_CALL:
1418 rc = __inject_extcall(vcpu, irq);
1419 break;
1420 case KVM_S390_INT_EMERGENCY:
1421 rc = __inject_sigp_emergency(vcpu, irq);
1422 break;
1423 case KVM_S390_MCHK:
1424 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001425 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001426 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001427 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001428 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001429 case KVM_S390_INT_VIRTIO:
1430 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001431 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001432 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001433 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001434 }
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001435 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001436 if (!rc)
1437 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001438 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001439}
Jens Freimannc05c4182013-10-07 16:13:45 +02001440
Christian Borntraeger67335e62014-03-25 17:09:08 +01001441void kvm_s390_clear_float_irqs(struct kvm *kvm)
Jens Freimannc05c4182013-10-07 16:13:45 +02001442{
1443 struct kvm_s390_float_interrupt *fi;
1444 struct kvm_s390_interrupt_info *n, *inti = NULL;
1445
Jens Freimannc05c4182013-10-07 16:13:45 +02001446 fi = &kvm->arch.float_int;
1447 spin_lock(&fi->lock);
1448 list_for_each_entry_safe(inti, n, &fi->list, list) {
1449 list_del(&inti->list);
1450 kfree(inti);
1451 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001452 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001453 atomic_set(&fi->active, 0);
1454 spin_unlock(&fi->lock);
Jens Freimannc05c4182013-10-07 16:13:45 +02001455}
1456
1457static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1458 u8 *addr)
1459{
1460 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1461 struct kvm_s390_irq irq = {0};
1462
1463 irq.type = inti->type;
1464 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001465 case KVM_S390_INT_PFAULT_INIT:
1466 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001467 case KVM_S390_INT_VIRTIO:
1468 case KVM_S390_INT_SERVICE:
1469 irq.u.ext = inti->ext;
1470 break;
1471 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1472 irq.u.io = inti->io;
1473 break;
1474 case KVM_S390_MCHK:
1475 irq.u.mchk = inti->mchk;
1476 break;
1477 default:
1478 return -EINVAL;
1479 }
1480
1481 if (copy_to_user(uptr, &irq, sizeof(irq)))
1482 return -EFAULT;
1483
1484 return 0;
1485}
1486
1487static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1488{
1489 struct kvm_s390_interrupt_info *inti;
1490 struct kvm_s390_float_interrupt *fi;
1491 int ret = 0;
1492 int n = 0;
1493
Jens Freimannc05c4182013-10-07 16:13:45 +02001494 fi = &kvm->arch.float_int;
1495 spin_lock(&fi->lock);
1496
1497 list_for_each_entry(inti, &fi->list, list) {
1498 if (len < sizeof(struct kvm_s390_irq)) {
1499 /* signal userspace to try again */
1500 ret = -ENOMEM;
1501 break;
1502 }
1503 ret = copy_irq_to_user(inti, buf);
1504 if (ret)
1505 break;
1506 buf += sizeof(struct kvm_s390_irq);
1507 len -= sizeof(struct kvm_s390_irq);
1508 n++;
1509 }
1510
1511 spin_unlock(&fi->lock);
Jens Freimannc05c4182013-10-07 16:13:45 +02001512
1513 return ret < 0 ? ret : n;
1514}
1515
1516static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1517{
1518 int r;
1519
1520 switch (attr->group) {
1521 case KVM_DEV_FLIC_GET_ALL_IRQS:
1522 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1523 attr->attr);
1524 break;
1525 default:
1526 r = -EINVAL;
1527 }
1528
1529 return r;
1530}
1531
1532static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1533 u64 addr)
1534{
1535 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1536 void *target = NULL;
1537 void __user *source;
1538 u64 size;
1539
1540 if (get_user(inti->type, (u64 __user *)addr))
1541 return -EFAULT;
1542
1543 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001544 case KVM_S390_INT_PFAULT_INIT:
1545 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001546 case KVM_S390_INT_VIRTIO:
1547 case KVM_S390_INT_SERVICE:
1548 target = (void *) &inti->ext;
1549 source = &uptr->u.ext;
1550 size = sizeof(inti->ext);
1551 break;
1552 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1553 target = (void *) &inti->io;
1554 source = &uptr->u.io;
1555 size = sizeof(inti->io);
1556 break;
1557 case KVM_S390_MCHK:
1558 target = (void *) &inti->mchk;
1559 source = &uptr->u.mchk;
1560 size = sizeof(inti->mchk);
1561 break;
1562 default:
1563 return -EINVAL;
1564 }
1565
1566 if (copy_from_user(target, source, size))
1567 return -EFAULT;
1568
1569 return 0;
1570}
1571
1572static int enqueue_floating_irq(struct kvm_device *dev,
1573 struct kvm_device_attr *attr)
1574{
1575 struct kvm_s390_interrupt_info *inti = NULL;
1576 int r = 0;
1577 int len = attr->attr;
1578
1579 if (len % sizeof(struct kvm_s390_irq) != 0)
1580 return -EINVAL;
1581 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1582 return -EINVAL;
1583
1584 while (len >= sizeof(struct kvm_s390_irq)) {
1585 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1586 if (!inti)
1587 return -ENOMEM;
1588
1589 r = copy_irq_from_user(inti, attr->addr);
1590 if (r) {
1591 kfree(inti);
1592 return r;
1593 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001594 r = __inject_vm(dev->kvm, inti);
1595 if (r) {
1596 kfree(inti);
1597 return r;
1598 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001599 len -= sizeof(struct kvm_s390_irq);
1600 attr->addr += sizeof(struct kvm_s390_irq);
1601 }
1602
1603 return r;
1604}
1605
Cornelia Huck841b91c2013-07-15 13:36:01 +02001606static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1607{
1608 if (id >= MAX_S390_IO_ADAPTERS)
1609 return NULL;
1610 return kvm->arch.adapters[id];
1611}
1612
1613static int register_io_adapter(struct kvm_device *dev,
1614 struct kvm_device_attr *attr)
1615{
1616 struct s390_io_adapter *adapter;
1617 struct kvm_s390_io_adapter adapter_info;
1618
1619 if (copy_from_user(&adapter_info,
1620 (void __user *)attr->addr, sizeof(adapter_info)))
1621 return -EFAULT;
1622
1623 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1624 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1625 return -EINVAL;
1626
1627 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1628 if (!adapter)
1629 return -ENOMEM;
1630
1631 INIT_LIST_HEAD(&adapter->maps);
1632 init_rwsem(&adapter->maps_lock);
1633 atomic_set(&adapter->nr_maps, 0);
1634 adapter->id = adapter_info.id;
1635 adapter->isc = adapter_info.isc;
1636 adapter->maskable = adapter_info.maskable;
1637 adapter->masked = false;
1638 adapter->swap = adapter_info.swap;
1639 dev->kvm->arch.adapters[adapter->id] = adapter;
1640
1641 return 0;
1642}
1643
1644int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1645{
1646 int ret;
1647 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1648
1649 if (!adapter || !adapter->maskable)
1650 return -EINVAL;
1651 ret = adapter->masked;
1652 adapter->masked = masked;
1653 return ret;
1654}
1655
1656static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1657{
1658 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1659 struct s390_map_info *map;
1660 int ret;
1661
1662 if (!adapter || !addr)
1663 return -EINVAL;
1664
1665 map = kzalloc(sizeof(*map), GFP_KERNEL);
1666 if (!map) {
1667 ret = -ENOMEM;
1668 goto out;
1669 }
1670 INIT_LIST_HEAD(&map->list);
1671 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02001672 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001673 if (map->addr == -EFAULT) {
1674 ret = -EFAULT;
1675 goto out;
1676 }
1677 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1678 if (ret < 0)
1679 goto out;
1680 BUG_ON(ret != 1);
1681 down_write(&adapter->maps_lock);
1682 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1683 list_add_tail(&map->list, &adapter->maps);
1684 ret = 0;
1685 } else {
1686 put_page(map->page);
1687 ret = -EINVAL;
1688 }
1689 up_write(&adapter->maps_lock);
1690out:
1691 if (ret)
1692 kfree(map);
1693 return ret;
1694}
1695
1696static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1697{
1698 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1699 struct s390_map_info *map, *tmp;
1700 int found = 0;
1701
1702 if (!adapter || !addr)
1703 return -EINVAL;
1704
1705 down_write(&adapter->maps_lock);
1706 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1707 if (map->guest_addr == addr) {
1708 found = 1;
1709 atomic_dec(&adapter->nr_maps);
1710 list_del(&map->list);
1711 put_page(map->page);
1712 kfree(map);
1713 break;
1714 }
1715 }
1716 up_write(&adapter->maps_lock);
1717
1718 return found ? 0 : -EINVAL;
1719}
1720
1721void kvm_s390_destroy_adapters(struct kvm *kvm)
1722{
1723 int i;
1724 struct s390_map_info *map, *tmp;
1725
1726 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1727 if (!kvm->arch.adapters[i])
1728 continue;
1729 list_for_each_entry_safe(map, tmp,
1730 &kvm->arch.adapters[i]->maps, list) {
1731 list_del(&map->list);
1732 put_page(map->page);
1733 kfree(map);
1734 }
1735 kfree(kvm->arch.adapters[i]);
1736 }
1737}
1738
1739static int modify_io_adapter(struct kvm_device *dev,
1740 struct kvm_device_attr *attr)
1741{
1742 struct kvm_s390_io_adapter_req req;
1743 struct s390_io_adapter *adapter;
1744 int ret;
1745
1746 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1747 return -EFAULT;
1748
1749 adapter = get_io_adapter(dev->kvm, req.id);
1750 if (!adapter)
1751 return -EINVAL;
1752 switch (req.type) {
1753 case KVM_S390_IO_ADAPTER_MASK:
1754 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1755 if (ret > 0)
1756 ret = 0;
1757 break;
1758 case KVM_S390_IO_ADAPTER_MAP:
1759 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1760 break;
1761 case KVM_S390_IO_ADAPTER_UNMAP:
1762 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1763 break;
1764 default:
1765 ret = -EINVAL;
1766 }
1767
1768 return ret;
1769}
1770
Jens Freimannc05c4182013-10-07 16:13:45 +02001771static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1772{
1773 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001774 unsigned int i;
1775 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001776
1777 switch (attr->group) {
1778 case KVM_DEV_FLIC_ENQUEUE:
1779 r = enqueue_floating_irq(dev, attr);
1780 break;
1781 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01001782 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001783 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001784 case KVM_DEV_FLIC_APF_ENABLE:
1785 dev->kvm->arch.gmap->pfault_enabled = 1;
1786 break;
1787 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1788 dev->kvm->arch.gmap->pfault_enabled = 0;
1789 /*
1790 * Make sure no async faults are in transition when
1791 * clearing the queues. So we don't need to worry
1792 * about late coming workers.
1793 */
1794 synchronize_srcu(&dev->kvm->srcu);
1795 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1796 kvm_clear_async_pf_completion_queue(vcpu);
1797 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001798 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1799 r = register_io_adapter(dev, attr);
1800 break;
1801 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1802 r = modify_io_adapter(dev, attr);
1803 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001804 default:
1805 r = -EINVAL;
1806 }
1807
1808 return r;
1809}
1810
1811static int flic_create(struct kvm_device *dev, u32 type)
1812{
1813 if (!dev)
1814 return -EINVAL;
1815 if (dev->kvm->arch.flic)
1816 return -EINVAL;
1817 dev->kvm->arch.flic = dev;
1818 return 0;
1819}
1820
1821static void flic_destroy(struct kvm_device *dev)
1822{
1823 dev->kvm->arch.flic = NULL;
1824 kfree(dev);
1825}
1826
1827/* s390 floating irq controller (flic) */
1828struct kvm_device_ops kvm_flic_ops = {
1829 .name = "kvm-flic",
1830 .get_attr = flic_get_attr,
1831 .set_attr = flic_set_attr,
1832 .create = flic_create,
1833 .destroy = flic_destroy,
1834};
Cornelia Huck84223592013-07-15 13:36:01 +02001835
1836static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1837{
1838 unsigned long bit;
1839
1840 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1841
1842 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1843}
1844
1845static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1846 u64 addr)
1847{
1848 struct s390_map_info *map;
1849
1850 if (!adapter)
1851 return NULL;
1852
1853 list_for_each_entry(map, &adapter->maps, list) {
1854 if (map->guest_addr == addr)
1855 return map;
1856 }
1857 return NULL;
1858}
1859
1860static int adapter_indicators_set(struct kvm *kvm,
1861 struct s390_io_adapter *adapter,
1862 struct kvm_s390_adapter_int *adapter_int)
1863{
1864 unsigned long bit;
1865 int summary_set, idx;
1866 struct s390_map_info *info;
1867 void *map;
1868
1869 info = get_map_info(adapter, adapter_int->ind_addr);
1870 if (!info)
1871 return -1;
1872 map = page_address(info->page);
1873 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1874 set_bit(bit, map);
1875 idx = srcu_read_lock(&kvm->srcu);
1876 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1877 set_page_dirty_lock(info->page);
1878 info = get_map_info(adapter, adapter_int->summary_addr);
1879 if (!info) {
1880 srcu_read_unlock(&kvm->srcu, idx);
1881 return -1;
1882 }
1883 map = page_address(info->page);
1884 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1885 adapter->swap);
1886 summary_set = test_and_set_bit(bit, map);
1887 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1888 set_page_dirty_lock(info->page);
1889 srcu_read_unlock(&kvm->srcu, idx);
1890 return summary_set ? 0 : 1;
1891}
1892
1893/*
1894 * < 0 - not injected due to error
1895 * = 0 - coalesced, summary indicator already active
1896 * > 0 - injected interrupt
1897 */
1898static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1899 struct kvm *kvm, int irq_source_id, int level,
1900 bool line_status)
1901{
1902 int ret;
1903 struct s390_io_adapter *adapter;
1904
1905 /* We're only interested in the 0->1 transition. */
1906 if (!level)
1907 return 0;
1908 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1909 if (!adapter)
1910 return -1;
1911 down_read(&adapter->maps_lock);
1912 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1913 up_read(&adapter->maps_lock);
1914 if ((ret > 0) && !adapter->masked) {
1915 struct kvm_s390_interrupt s390int = {
1916 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1917 .parm = 0,
1918 .parm64 = (adapter->isc << 27) | 0x80000000,
1919 };
1920 ret = kvm_s390_inject_vm(kvm, &s390int);
1921 if (ret == 0)
1922 ret = 1;
1923 }
1924 return ret;
1925}
1926
Paul Mackerras8ba918d2014-06-30 20:51:10 +10001927int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02001928 const struct kvm_irq_routing_entry *ue)
1929{
1930 int ret;
1931
1932 switch (ue->type) {
1933 case KVM_IRQ_ROUTING_S390_ADAPTER:
1934 e->set = set_adapter_int;
1935 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1936 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1937 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1938 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1939 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1940 ret = 0;
1941 break;
1942 default:
1943 ret = -EINVAL;
1944 }
1945
1946 return ret;
1947}
1948
1949int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1950 int irq_source_id, int level, bool line_status)
1951{
1952 return -EINVAL;
1953}