blob: a1403ba75bfc47bbf25479009bbd245cb11e6001 [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>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010019#include <asm/asm-offsets.h>
20#include <asm/uaccess.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010021#include "kvm-s390.h"
22#include "gaccess.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020023#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010024
Cornelia Huckd8346b72012-12-20 15:32:08 +010025#define IOINT_SCHID_MASK 0x0000ffff
26#define IOINT_SSID_MASK 0x00030000
27#define IOINT_CSSID_MASK 0x03fc0000
28#define IOINT_AI_MASK 0x04000000
29
30static int is_ioint(u64 type)
31{
32 return ((type & 0xfffe0000u) != 0xfffe0000u);
33}
34
Dominik Dingel3c038e62013-10-07 17:11:48 +020035int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010036{
37 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
38}
39
Cornelia Huckd8346b72012-12-20 15:32:08 +010040static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
41{
42 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
43}
44
Cornelia Huck48a3e952012-12-20 15:32:09 +010045static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
46{
47 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
48}
49
Carsten Otteba5c1e92008-03-25 18:47:26 +010050static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
51{
52 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
53 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
54 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
55 return 0;
56 return 1;
57}
58
Cornelia Huck79fd50c2013-02-07 13:20:52 +010059static u64 int_word_to_isc_bits(u32 int_word)
60{
61 u8 isc = (int_word & 0x38000000) >> 27;
62
63 return (0x80 >> isc) << 24;
64}
65
Carsten Otteba5c1e92008-03-25 18:47:26 +010066static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020067 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010068{
69 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020070 case KVM_S390_INT_EXTERNAL_CALL:
71 if (psw_extint_disabled(vcpu))
72 return 0;
73 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
74 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +010075 case KVM_S390_INT_EMERGENCY:
76 if (psw_extint_disabled(vcpu))
77 return 0;
78 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
79 return 1;
80 return 0;
81 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +020082 case KVM_S390_INT_PFAULT_INIT:
83 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +010084 case KVM_S390_INT_VIRTIO:
85 if (psw_extint_disabled(vcpu))
86 return 0;
87 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
88 return 1;
89 return 0;
90 case KVM_S390_PROGRAM_INT:
91 case KVM_S390_SIGP_STOP:
92 case KVM_S390_SIGP_SET_PREFIX:
93 case KVM_S390_RESTART:
94 return 1;
Cornelia Huck48a3e952012-12-20 15:32:09 +010095 case KVM_S390_MCHK:
96 if (psw_mchk_disabled(vcpu))
97 return 0;
98 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
99 return 1;
100 return 0;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100101 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
102 if (psw_ioint_disabled(vcpu))
103 return 0;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100104 if (vcpu->arch.sie_block->gcr[6] &
105 int_word_to_isc_bits(inti->io.io_int_word))
Cornelia Huckd8346b72012-12-20 15:32:08 +0100106 return 1;
107 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100108 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100109 printk(KERN_WARNING "illegal interrupt type %llx\n",
110 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100111 BUG();
112 }
113 return 0;
114}
115
116static void __set_cpu_idle(struct kvm_vcpu *vcpu)
117{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100118 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
119 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
120}
121
122static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
123{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100124 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
125 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
126}
127
128static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
129{
130 atomic_clear_mask(CPUSTAT_ECALL_PEND |
131 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
132 &vcpu->arch.sie_block->cpuflags);
133 vcpu->arch.sie_block->lctl = 0x0000;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100134 vcpu->arch.sie_block->ictl &= ~ICTL_LPSW;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100135}
136
137static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
138{
139 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
140}
141
142static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200143 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100144{
145 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200146 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100147 case KVM_S390_INT_EMERGENCY:
148 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200149 case KVM_S390_INT_PFAULT_INIT:
150 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100151 case KVM_S390_INT_VIRTIO:
152 if (psw_extint_disabled(vcpu))
153 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
154 else
155 vcpu->arch.sie_block->lctl |= LCTL_CR0;
156 break;
157 case KVM_S390_SIGP_STOP:
158 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
159 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100160 case KVM_S390_MCHK:
161 if (psw_mchk_disabled(vcpu))
162 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
163 else
164 vcpu->arch.sie_block->lctl |= LCTL_CR14;
165 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100166 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
167 if (psw_ioint_disabled(vcpu))
168 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
169 else
170 vcpu->arch.sie_block->lctl |= LCTL_CR6;
171 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100172 default:
173 BUG();
174 }
175}
176
177static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200178 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100179{
180 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100181 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100182
183 switch (inti->type) {
184 case KVM_S390_INT_EMERGENCY:
185 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
186 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200187 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
188 inti->emerg.code, 0);
Jens Freimann1a03b762014-02-12 14:05:38 +0100189 rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE);
190 rc |= put_guest_lc(vcpu, inti->emerg.code,
191 (u16 *)__LC_EXT_CPU_ADDR);
192 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
193 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
194 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100195 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100196 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200197 case KVM_S390_INT_EXTERNAL_CALL:
198 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
199 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200200 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
201 inti->extcall.code, 0);
Jens Freimann1a03b762014-02-12 14:05:38 +0100202 rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE);
203 rc |= put_guest_lc(vcpu, inti->extcall.code,
204 (u16 *)__LC_EXT_CPU_ADDR);
205 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
206 &vcpu->arch.sie_block->gpsw,
207 sizeof(psw_t));
208 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
209 &vcpu->arch.sie_block->gpsw,
210 sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200211 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100212 case KVM_S390_INT_SERVICE:
213 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
214 inti->ext.ext_params);
215 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200216 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
217 inti->ext.ext_params, 0);
Heiko Carstens79882762014-01-02 10:59:41 +0100218 rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE);
219 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
220 &vcpu->arch.sie_block->gpsw,
221 sizeof(psw_t));
222 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100223 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100224 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
225 (u32 *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100226 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200227 case KVM_S390_INT_PFAULT_INIT:
228 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
229 inti->ext.ext_params2);
Jens Freimann1a03b762014-02-12 14:05:38 +0100230 rc = put_guest_lc(vcpu, 0x2603, (u16 *) __LC_EXT_INT_CODE);
231 rc |= put_guest_lc(vcpu, 0x0600, (u16 *) __LC_EXT_CPU_ADDR);
232 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
233 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
234 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200235 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann1a03b762014-02-12 14:05:38 +0100236 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
237 (u64 *) __LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200238 break;
239 case KVM_S390_INT_PFAULT_DONE:
240 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
241 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100242 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
243 rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR);
244 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
245 &vcpu->arch.sie_block->gpsw,
246 sizeof(psw_t));
247 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200248 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100249 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
250 (u64 *)__LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200251 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100252 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100253 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100254 inti->ext.ext_params, inti->ext.ext_params2);
255 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200256 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
257 inti->ext.ext_params,
258 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100259 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
260 rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR);
261 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
262 &vcpu->arch.sie_block->gpsw,
263 sizeof(psw_t));
264 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100265 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100266 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
267 (u32 *)__LC_EXT_PARAMS);
268 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
269 (u64 *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100270 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100271 case KVM_S390_SIGP_STOP:
272 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
273 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200274 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
275 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100276 __set_intercept_indicator(vcpu, inti);
277 break;
278
279 case KVM_S390_SIGP_SET_PREFIX:
280 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
281 inti->prefix.address);
282 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200283 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
284 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100285 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100286 break;
287
288 case KVM_S390_RESTART:
289 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
290 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200291 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
292 0, 0);
Jens Freimann1a03b762014-02-12 14:05:38 +0100293 rc = write_guest_lc(vcpu,
294 offsetof(struct _lowcore, restart_old_psw),
295 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
296 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
297 &vcpu->arch.sie_block->gpsw,
298 sizeof(psw_t));
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100299 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100300 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100301 case KVM_S390_PROGRAM_INT:
302 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
303 inti->pgm.code,
304 table[vcpu->arch.sie_block->ipa >> 14]);
305 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200306 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
307 inti->pgm.code, 0);
Jens Freimann1a03b762014-02-12 14:05:38 +0100308 rc = put_guest_lc(vcpu, inti->pgm.code,
309 (u16 __user *)__LC_PGM_INT_CODE);
310 rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
311 (u16 __user *)__LC_PGM_ILC);
312 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
313 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
314 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
315 &vcpu->arch.sie_block->gpsw,
316 sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100317 break;
318
Cornelia Huck48a3e952012-12-20 15:32:09 +0100319 case KVM_S390_MCHK:
320 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
321 inti->mchk.mcic);
322 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
323 inti->mchk.cr14,
324 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100325 rc = kvm_s390_vcpu_store_status(vcpu,
326 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens79882762014-01-02 10:59:41 +0100327 rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE);
328 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
329 &vcpu->arch.sie_block->gpsw,
330 sizeof(psw_t));
331 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100332 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100333 break;
334
Cornelia Huckd8346b72012-12-20 15:32:08 +0100335 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
336 {
337 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
338 inti->io.subchannel_nr;
339 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
340 inti->io.io_int_word;
341 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
342 vcpu->stat.deliver_io_int++;
343 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
344 param0, param1);
Heiko Carstens79882762014-01-02 10:59:41 +0100345 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
346 (u16 *)__LC_SUBCHANNEL_ID);
347 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
348 (u16 *)__LC_SUBCHANNEL_NR);
349 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
350 (u32 *)__LC_IO_INT_PARM);
351 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
352 (u32 *)__LC_IO_INT_WORD);
353 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
354 &vcpu->arch.sie_block->gpsw,
355 sizeof(psw_t));
356 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
357 &vcpu->arch.sie_block->gpsw,
358 sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100359 break;
360 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100361 default:
362 BUG();
363 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100364 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200365 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100366 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200367 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100368 }
369}
370
371static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
372{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100373 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100374
375 if (psw_extint_disabled(vcpu))
376 return 0;
377 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
378 return 0;
Jens Freimann1a03b762014-02-12 14:05:38 +0100379 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
380 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
381 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
382 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
383 &vcpu->arch.sie_block->gpsw,
384 sizeof(psw_t));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100385 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200386 printk("kvm: The guest lowcore is not mapped during interrupt "
387 "delivery, killing userspace\n");
388 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100389 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100390 return 1;
391}
392
Dominik Dingel3c038e62013-10-07 17:11:48 +0200393int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100394{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200395 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
396 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
397 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100398 int rc = 0;
399
400 if (atomic_read(&li->active)) {
401 spin_lock_bh(&li->lock);
402 list_for_each_entry(inti, &li->list, list)
403 if (__interrupt_is_deliverable(vcpu, inti)) {
404 rc = 1;
405 break;
406 }
407 spin_unlock_bh(&li->lock);
408 }
409
410 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200411 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100412 list_for_each_entry(inti, &fi->list, list)
413 if (__interrupt_is_deliverable(vcpu, inti)) {
414 rc = 1;
415 break;
416 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200417 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100418 }
419
420 if ((!rc) && (vcpu->arch.sie_block->ckc <
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200421 get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100422 if ((!psw_extint_disabled(vcpu)) &&
423 (vcpu->arch.sie_block->gcr[0] & 0x800ul))
424 rc = 1;
425 }
426
427 return rc;
428}
429
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300430int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
431{
432 return 0;
433}
434
Carsten Otteba5c1e92008-03-25 18:47:26 +0100435int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
436{
437 u64 now, sltime;
438 DECLARE_WAITQUEUE(wait, current);
439
440 vcpu->stat.exit_wait_state++;
441 if (kvm_cpu_has_interrupt(vcpu))
442 return 0;
443
Carsten Ottee52b2af2008-05-21 13:37:44 +0200444 __set_cpu_idle(vcpu);
445 spin_lock_bh(&vcpu->arch.local_int.lock);
446 vcpu->arch.local_int.timer_due = 0;
447 spin_unlock_bh(&vcpu->arch.local_int.lock);
448
Carsten Otteba5c1e92008-03-25 18:47:26 +0100449 if (psw_interrupts_disabled(vcpu)) {
450 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
451 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100452 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100453 }
454
455 if (psw_extint_disabled(vcpu) ||
456 (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
457 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
458 goto no_timer;
459 }
460
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200461 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100462 if (vcpu->arch.sie_block->ckc < now) {
463 __unset_cpu_idle(vcpu);
464 return 0;
465 }
466
Heiko Carstensed4f2092013-01-14 16:55:55 +0100467 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100468
Christian Borntraegerca872302009-05-12 17:21:49 +0200469 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
470 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100471no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200472 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200473 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100474 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200475 add_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100476 while (list_empty(&vcpu->arch.local_int.list) &&
477 list_empty(&vcpu->arch.local_int.float_int->list) &&
478 (!vcpu->arch.local_int.timer_due) &&
479 !signal_pending(current)) {
480 set_current_state(TASK_INTERRUPTIBLE);
481 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200482 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100483 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200484 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100485 spin_lock_bh(&vcpu->arch.local_int.lock);
486 }
487 __unset_cpu_idle(vcpu);
488 __set_current_state(TASK_RUNNING);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200489 remove_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100490 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200491 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Thomas Huth800c1062013-09-12 10:33:45 +0200492 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
493
Christian Borntraegerca872302009-05-12 17:21:49 +0200494 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100495 return 0;
496}
497
Christian Borntraegerca872302009-05-12 17:21:49 +0200498void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100499{
Christian Borntraegerca872302009-05-12 17:21:49 +0200500 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100501
Christian Borntraegerca872302009-05-12 17:21:49 +0200502 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100503 vcpu->arch.local_int.timer_due = 1;
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200504 if (waitqueue_active(&vcpu->wq))
505 wake_up_interruptible(&vcpu->wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200506 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100507}
508
Christian Borntraegerca872302009-05-12 17:21:49 +0200509/*
510 * low level hrtimer wake routine. Because this runs in hardirq context
511 * we schedule a tasklet to do the real work.
512 */
513enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
514{
515 struct kvm_vcpu *vcpu;
516
517 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100518 vcpu->preempted = true;
Christian Borntraegerca872302009-05-12 17:21:49 +0200519 tasklet_schedule(&vcpu->arch.tasklet);
520
521 return HRTIMER_NORESTART;
522}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100523
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100524void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
525{
526 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
527 struct kvm_s390_interrupt_info *n, *inti = NULL;
528
529 spin_lock_bh(&li->lock);
530 list_for_each_entry_safe(inti, n, &li->list, list) {
531 list_del(&inti->list);
532 kfree(inti);
533 }
534 atomic_set(&li->active, 0);
535 spin_unlock_bh(&li->lock);
536}
537
Carsten Otteba5c1e92008-03-25 18:47:26 +0100538void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
539{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200540 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
541 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
542 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100543 int deliver;
544
545 __reset_intercept_indicators(vcpu);
546 if (atomic_read(&li->active)) {
547 do {
548 deliver = 0;
549 spin_lock_bh(&li->lock);
550 list_for_each_entry_safe(inti, n, &li->list, list) {
551 if (__interrupt_is_deliverable(vcpu, inti)) {
552 list_del(&inti->list);
553 deliver = 1;
554 break;
555 }
556 __set_intercept_indicator(vcpu, inti);
557 }
558 if (list_empty(&li->list))
559 atomic_set(&li->active, 0);
560 spin_unlock_bh(&li->lock);
561 if (deliver) {
562 __do_deliver_interrupt(vcpu, inti);
563 kfree(inti);
564 }
565 } while (deliver);
566 }
567
568 if ((vcpu->arch.sie_block->ckc <
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200569 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
Carsten Otteba5c1e92008-03-25 18:47:26 +0100570 __try_deliver_ckc_interrupt(vcpu);
571
572 if (atomic_read(&fi->active)) {
573 do {
574 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200575 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100576 list_for_each_entry_safe(inti, n, &fi->list, list) {
577 if (__interrupt_is_deliverable(vcpu, inti)) {
578 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100579 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100580 deliver = 1;
581 break;
582 }
583 __set_intercept_indicator(vcpu, inti);
584 }
585 if (list_empty(&fi->list))
586 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200587 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100588 if (deliver) {
589 __do_deliver_interrupt(vcpu, inti);
590 kfree(inti);
591 }
592 } while (deliver);
593 }
594}
595
Cornelia Huck48a3e952012-12-20 15:32:09 +0100596void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
597{
598 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
599 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
600 struct kvm_s390_interrupt_info *n, *inti = NULL;
601 int deliver;
602
603 __reset_intercept_indicators(vcpu);
604 if (atomic_read(&li->active)) {
605 do {
606 deliver = 0;
607 spin_lock_bh(&li->lock);
608 list_for_each_entry_safe(inti, n, &li->list, list) {
609 if ((inti->type == KVM_S390_MCHK) &&
610 __interrupt_is_deliverable(vcpu, inti)) {
611 list_del(&inti->list);
612 deliver = 1;
613 break;
614 }
615 __set_intercept_indicator(vcpu, inti);
616 }
617 if (list_empty(&li->list))
618 atomic_set(&li->active, 0);
619 spin_unlock_bh(&li->lock);
620 if (deliver) {
621 __do_deliver_interrupt(vcpu, inti);
622 kfree(inti);
623 }
624 } while (deliver);
625 }
626
627 if (atomic_read(&fi->active)) {
628 do {
629 deliver = 0;
630 spin_lock(&fi->lock);
631 list_for_each_entry_safe(inti, n, &fi->list, list) {
632 if ((inti->type == KVM_S390_MCHK) &&
633 __interrupt_is_deliverable(vcpu, inti)) {
634 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100635 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100636 deliver = 1;
637 break;
638 }
639 __set_intercept_indicator(vcpu, inti);
640 }
641 if (list_empty(&fi->list))
642 atomic_set(&fi->active, 0);
643 spin_unlock(&fi->lock);
644 if (deliver) {
645 __do_deliver_interrupt(vcpu, inti);
646 kfree(inti);
647 }
648 } while (deliver);
649 }
650}
651
Carsten Otteba5c1e92008-03-25 18:47:26 +0100652int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
653{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200654 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
655 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100656
657 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
658 if (!inti)
659 return -ENOMEM;
660
Joe Perchesa419aef2009-08-18 11:18:35 -0700661 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100662 inti->pgm.code = code;
663
664 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200665 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100666 spin_lock_bh(&li->lock);
667 list_add(&inti->list, &li->list);
668 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200669 BUG_ON(waitqueue_active(li->wq));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100670 spin_unlock_bh(&li->lock);
671 return 0;
672}
673
Jens Freimannbcd84682014-02-11 11:07:05 +0100674int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
675 struct kvm_s390_pgm_info *pgm_info)
676{
677 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
678 struct kvm_s390_interrupt_info *inti;
679
680 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
681 if (!inti)
682 return -ENOMEM;
683
684 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
685 pgm_info->code);
686 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
687 pgm_info->code, 0, 1);
688
689 inti->type = KVM_S390_PROGRAM_INT;
690 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
691 spin_lock_bh(&li->lock);
692 list_add(&inti->list, &li->list);
693 atomic_set(&li->active, 1);
694 BUG_ON(waitqueue_active(li->wq));
695 spin_unlock_bh(&li->lock);
696 return 0;
697}
698
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100699struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
700 u64 cr6, u64 schid)
701{
702 struct kvm_s390_float_interrupt *fi;
703 struct kvm_s390_interrupt_info *inti, *iter;
704
705 if ((!schid && !cr6) || (schid && cr6))
706 return NULL;
707 mutex_lock(&kvm->lock);
708 fi = &kvm->arch.float_int;
709 spin_lock(&fi->lock);
710 inti = NULL;
711 list_for_each_entry(iter, &fi->list, list) {
712 if (!is_ioint(iter->type))
713 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100714 if (cr6 &&
715 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100716 continue;
717 if (schid) {
718 if (((schid & 0x00000000ffff0000) >> 16) !=
719 iter->io.subchannel_id)
720 continue;
721 if ((schid & 0x000000000000ffff) !=
722 iter->io.subchannel_nr)
723 continue;
724 }
725 inti = iter;
726 break;
727 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100728 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100729 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100730 fi->irq_count--;
731 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100732 if (list_empty(&fi->list))
733 atomic_set(&fi->active, 0);
734 spin_unlock(&fi->lock);
735 mutex_unlock(&kvm->lock);
736 return inti;
737}
738
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100739static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100740{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200741 struct kvm_s390_local_interrupt *li;
742 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200743 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100744 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100745 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100746 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100747
Carsten Otteba5c1e92008-03-25 18:47:26 +0100748 mutex_lock(&kvm->lock);
749 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200750 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100751 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
752 rc = -EINVAL;
753 goto unlock_fi;
754 }
755 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200756 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100757 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200758 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100759 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
760
Cornelia Huckd8346b72012-12-20 15:32:08 +0100761 /* Keep I/O interrupts sorted in isc order. */
762 list_for_each_entry(iter, &fi->list, list) {
763 if (!is_ioint(iter->type))
764 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100765 if (int_word_to_isc_bits(iter->io.io_int_word)
766 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100767 continue;
768 break;
769 }
770 list_add_tail(&inti->list, &iter->list);
771 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100772 atomic_set(&fi->active, 1);
773 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
774 if (sigcpu == KVM_MAX_VCPUS) {
775 do {
776 sigcpu = fi->next_rr_cpu++;
777 if (sigcpu == KVM_MAX_VCPUS)
778 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100779 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100780 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100781 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
782 li = &dst_vcpu->arch.local_int;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100783 spin_lock_bh(&li->lock);
784 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200785 if (waitqueue_active(li->wq))
786 wake_up_interruptible(li->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100787 kvm_get_vcpu(kvm, sigcpu)->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100788 spin_unlock_bh(&li->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100789unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200790 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100791 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100792 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200793}
794
795int kvm_s390_inject_vm(struct kvm *kvm,
796 struct kvm_s390_interrupt *s390int)
797{
798 struct kvm_s390_interrupt_info *inti;
799
800 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
801 if (!inti)
802 return -ENOMEM;
803
804 inti->type = s390int->type;
805 switch (inti->type) {
806 case KVM_S390_INT_VIRTIO:
807 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
808 s390int->parm, s390int->parm64);
809 inti->ext.ext_params = s390int->parm;
810 inti->ext.ext_params2 = s390int->parm64;
811 break;
812 case KVM_S390_INT_SERVICE:
813 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
814 inti->ext.ext_params = s390int->parm;
815 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200816 case KVM_S390_INT_PFAULT_DONE:
817 inti->type = s390int->type;
818 inti->ext.ext_params2 = s390int->parm64;
819 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200820 case KVM_S390_MCHK:
821 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
822 s390int->parm64);
823 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
824 inti->mchk.mcic = s390int->parm64;
825 break;
826 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
827 if (inti->type & IOINT_AI_MASK)
828 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
829 else
830 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
831 s390int->type & IOINT_CSSID_MASK,
832 s390int->type & IOINT_SSID_MASK,
833 s390int->type & IOINT_SCHID_MASK);
834 inti->io.subchannel_id = s390int->parm >> 16;
835 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
836 inti->io.io_int_parm = s390int->parm64 >> 32;
837 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
838 break;
839 default:
840 kfree(inti);
841 return -EINVAL;
842 }
843 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
844 2);
845
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100846 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100847}
848
849int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
850 struct kvm_s390_interrupt *s390int)
851{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200852 struct kvm_s390_local_interrupt *li;
853 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100854
855 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
856 if (!inti)
857 return -ENOMEM;
858
859 switch (s390int->type) {
860 case KVM_S390_PROGRAM_INT:
861 if (s390int->parm & 0xffff0000) {
862 kfree(inti);
863 return -EINVAL;
864 }
865 inti->type = s390int->type;
866 inti->pgm.code = s390int->parm;
867 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
868 s390int->parm);
869 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100870 case KVM_S390_SIGP_SET_PREFIX:
871 inti->prefix.address = s390int->parm;
872 inti->type = s390int->type;
873 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
874 s390int->parm);
875 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100876 case KVM_S390_SIGP_STOP:
877 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100878 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
879 inti->type = s390int->type;
880 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200881 case KVM_S390_INT_EXTERNAL_CALL:
882 if (s390int->parm & 0xffff0000) {
883 kfree(inti);
884 return -EINVAL;
885 }
886 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
887 s390int->parm);
888 inti->type = s390int->type;
889 inti->extcall.code = s390int->parm;
890 break;
891 case KVM_S390_INT_EMERGENCY:
892 if (s390int->parm & 0xffff0000) {
893 kfree(inti);
894 return -EINVAL;
895 }
896 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
897 inti->type = s390int->type;
898 inti->emerg.code = s390int->parm;
899 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100900 case KVM_S390_MCHK:
901 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
902 s390int->parm64);
903 inti->type = s390int->type;
904 inti->mchk.mcic = s390int->parm64;
905 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200906 case KVM_S390_INT_PFAULT_INIT:
907 inti->type = s390int->type;
908 inti->ext.ext_params2 = s390int->parm64;
909 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100910 case KVM_S390_INT_VIRTIO:
911 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100912 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100913 default:
914 kfree(inti);
915 return -EINVAL;
916 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200917 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
918 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100919
920 mutex_lock(&vcpu->kvm->lock);
921 li = &vcpu->arch.local_int;
922 spin_lock_bh(&li->lock);
923 if (inti->type == KVM_S390_PROGRAM_INT)
924 list_add(&inti->list, &li->list);
925 else
926 list_add_tail(&inti->list, &li->list);
927 atomic_set(&li->active, 1);
928 if (inti->type == KVM_S390_SIGP_STOP)
929 li->action_bits |= ACTION_STOP_ON_STOP;
930 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200931 if (waitqueue_active(&vcpu->wq))
932 wake_up_interruptible(&vcpu->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100933 vcpu->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100934 spin_unlock_bh(&li->lock);
935 mutex_unlock(&vcpu->kvm->lock);
936 return 0;
937}
Jens Freimannc05c4182013-10-07 16:13:45 +0200938
939static void clear_floating_interrupts(struct kvm *kvm)
940{
941 struct kvm_s390_float_interrupt *fi;
942 struct kvm_s390_interrupt_info *n, *inti = NULL;
943
944 mutex_lock(&kvm->lock);
945 fi = &kvm->arch.float_int;
946 spin_lock(&fi->lock);
947 list_for_each_entry_safe(inti, n, &fi->list, list) {
948 list_del(&inti->list);
949 kfree(inti);
950 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100951 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +0200952 atomic_set(&fi->active, 0);
953 spin_unlock(&fi->lock);
954 mutex_unlock(&kvm->lock);
955}
956
957static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
958 u8 *addr)
959{
960 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
961 struct kvm_s390_irq irq = {0};
962
963 irq.type = inti->type;
964 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +0200965 case KVM_S390_INT_PFAULT_INIT:
966 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +0200967 case KVM_S390_INT_VIRTIO:
968 case KVM_S390_INT_SERVICE:
969 irq.u.ext = inti->ext;
970 break;
971 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
972 irq.u.io = inti->io;
973 break;
974 case KVM_S390_MCHK:
975 irq.u.mchk = inti->mchk;
976 break;
977 default:
978 return -EINVAL;
979 }
980
981 if (copy_to_user(uptr, &irq, sizeof(irq)))
982 return -EFAULT;
983
984 return 0;
985}
986
987static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
988{
989 struct kvm_s390_interrupt_info *inti;
990 struct kvm_s390_float_interrupt *fi;
991 int ret = 0;
992 int n = 0;
993
994 mutex_lock(&kvm->lock);
995 fi = &kvm->arch.float_int;
996 spin_lock(&fi->lock);
997
998 list_for_each_entry(inti, &fi->list, list) {
999 if (len < sizeof(struct kvm_s390_irq)) {
1000 /* signal userspace to try again */
1001 ret = -ENOMEM;
1002 break;
1003 }
1004 ret = copy_irq_to_user(inti, buf);
1005 if (ret)
1006 break;
1007 buf += sizeof(struct kvm_s390_irq);
1008 len -= sizeof(struct kvm_s390_irq);
1009 n++;
1010 }
1011
1012 spin_unlock(&fi->lock);
1013 mutex_unlock(&kvm->lock);
1014
1015 return ret < 0 ? ret : n;
1016}
1017
1018static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1019{
1020 int r;
1021
1022 switch (attr->group) {
1023 case KVM_DEV_FLIC_GET_ALL_IRQS:
1024 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1025 attr->attr);
1026 break;
1027 default:
1028 r = -EINVAL;
1029 }
1030
1031 return r;
1032}
1033
1034static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1035 u64 addr)
1036{
1037 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1038 void *target = NULL;
1039 void __user *source;
1040 u64 size;
1041
1042 if (get_user(inti->type, (u64 __user *)addr))
1043 return -EFAULT;
1044
1045 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001046 case KVM_S390_INT_PFAULT_INIT:
1047 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001048 case KVM_S390_INT_VIRTIO:
1049 case KVM_S390_INT_SERVICE:
1050 target = (void *) &inti->ext;
1051 source = &uptr->u.ext;
1052 size = sizeof(inti->ext);
1053 break;
1054 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1055 target = (void *) &inti->io;
1056 source = &uptr->u.io;
1057 size = sizeof(inti->io);
1058 break;
1059 case KVM_S390_MCHK:
1060 target = (void *) &inti->mchk;
1061 source = &uptr->u.mchk;
1062 size = sizeof(inti->mchk);
1063 break;
1064 default:
1065 return -EINVAL;
1066 }
1067
1068 if (copy_from_user(target, source, size))
1069 return -EFAULT;
1070
1071 return 0;
1072}
1073
1074static int enqueue_floating_irq(struct kvm_device *dev,
1075 struct kvm_device_attr *attr)
1076{
1077 struct kvm_s390_interrupt_info *inti = NULL;
1078 int r = 0;
1079 int len = attr->attr;
1080
1081 if (len % sizeof(struct kvm_s390_irq) != 0)
1082 return -EINVAL;
1083 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1084 return -EINVAL;
1085
1086 while (len >= sizeof(struct kvm_s390_irq)) {
1087 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1088 if (!inti)
1089 return -ENOMEM;
1090
1091 r = copy_irq_from_user(inti, attr->addr);
1092 if (r) {
1093 kfree(inti);
1094 return r;
1095 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001096 r = __inject_vm(dev->kvm, inti);
1097 if (r) {
1098 kfree(inti);
1099 return r;
1100 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001101 len -= sizeof(struct kvm_s390_irq);
1102 attr->addr += sizeof(struct kvm_s390_irq);
1103 }
1104
1105 return r;
1106}
1107
Cornelia Huck841b91c2013-07-15 13:36:01 +02001108static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1109{
1110 if (id >= MAX_S390_IO_ADAPTERS)
1111 return NULL;
1112 return kvm->arch.adapters[id];
1113}
1114
1115static int register_io_adapter(struct kvm_device *dev,
1116 struct kvm_device_attr *attr)
1117{
1118 struct s390_io_adapter *adapter;
1119 struct kvm_s390_io_adapter adapter_info;
1120
1121 if (copy_from_user(&adapter_info,
1122 (void __user *)attr->addr, sizeof(adapter_info)))
1123 return -EFAULT;
1124
1125 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1126 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1127 return -EINVAL;
1128
1129 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1130 if (!adapter)
1131 return -ENOMEM;
1132
1133 INIT_LIST_HEAD(&adapter->maps);
1134 init_rwsem(&adapter->maps_lock);
1135 atomic_set(&adapter->nr_maps, 0);
1136 adapter->id = adapter_info.id;
1137 adapter->isc = adapter_info.isc;
1138 adapter->maskable = adapter_info.maskable;
1139 adapter->masked = false;
1140 adapter->swap = adapter_info.swap;
1141 dev->kvm->arch.adapters[adapter->id] = adapter;
1142
1143 return 0;
1144}
1145
1146int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1147{
1148 int ret;
1149 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1150
1151 if (!adapter || !adapter->maskable)
1152 return -EINVAL;
1153 ret = adapter->masked;
1154 adapter->masked = masked;
1155 return ret;
1156}
1157
1158static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1159{
1160 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1161 struct s390_map_info *map;
1162 int ret;
1163
1164 if (!adapter || !addr)
1165 return -EINVAL;
1166
1167 map = kzalloc(sizeof(*map), GFP_KERNEL);
1168 if (!map) {
1169 ret = -ENOMEM;
1170 goto out;
1171 }
1172 INIT_LIST_HEAD(&map->list);
1173 map->guest_addr = addr;
1174 map->addr = gmap_translate(addr, kvm->arch.gmap);
1175 if (map->addr == -EFAULT) {
1176 ret = -EFAULT;
1177 goto out;
1178 }
1179 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1180 if (ret < 0)
1181 goto out;
1182 BUG_ON(ret != 1);
1183 down_write(&adapter->maps_lock);
1184 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1185 list_add_tail(&map->list, &adapter->maps);
1186 ret = 0;
1187 } else {
1188 put_page(map->page);
1189 ret = -EINVAL;
1190 }
1191 up_write(&adapter->maps_lock);
1192out:
1193 if (ret)
1194 kfree(map);
1195 return ret;
1196}
1197
1198static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1199{
1200 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1201 struct s390_map_info *map, *tmp;
1202 int found = 0;
1203
1204 if (!adapter || !addr)
1205 return -EINVAL;
1206
1207 down_write(&adapter->maps_lock);
1208 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1209 if (map->guest_addr == addr) {
1210 found = 1;
1211 atomic_dec(&adapter->nr_maps);
1212 list_del(&map->list);
1213 put_page(map->page);
1214 kfree(map);
1215 break;
1216 }
1217 }
1218 up_write(&adapter->maps_lock);
1219
1220 return found ? 0 : -EINVAL;
1221}
1222
1223void kvm_s390_destroy_adapters(struct kvm *kvm)
1224{
1225 int i;
1226 struct s390_map_info *map, *tmp;
1227
1228 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1229 if (!kvm->arch.adapters[i])
1230 continue;
1231 list_for_each_entry_safe(map, tmp,
1232 &kvm->arch.adapters[i]->maps, list) {
1233 list_del(&map->list);
1234 put_page(map->page);
1235 kfree(map);
1236 }
1237 kfree(kvm->arch.adapters[i]);
1238 }
1239}
1240
1241static int modify_io_adapter(struct kvm_device *dev,
1242 struct kvm_device_attr *attr)
1243{
1244 struct kvm_s390_io_adapter_req req;
1245 struct s390_io_adapter *adapter;
1246 int ret;
1247
1248 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1249 return -EFAULT;
1250
1251 adapter = get_io_adapter(dev->kvm, req.id);
1252 if (!adapter)
1253 return -EINVAL;
1254 switch (req.type) {
1255 case KVM_S390_IO_ADAPTER_MASK:
1256 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1257 if (ret > 0)
1258 ret = 0;
1259 break;
1260 case KVM_S390_IO_ADAPTER_MAP:
1261 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1262 break;
1263 case KVM_S390_IO_ADAPTER_UNMAP:
1264 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1265 break;
1266 default:
1267 ret = -EINVAL;
1268 }
1269
1270 return ret;
1271}
1272
Jens Freimannc05c4182013-10-07 16:13:45 +02001273static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1274{
1275 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001276 unsigned int i;
1277 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001278
1279 switch (attr->group) {
1280 case KVM_DEV_FLIC_ENQUEUE:
1281 r = enqueue_floating_irq(dev, attr);
1282 break;
1283 case KVM_DEV_FLIC_CLEAR_IRQS:
1284 r = 0;
1285 clear_floating_interrupts(dev->kvm);
1286 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001287 case KVM_DEV_FLIC_APF_ENABLE:
1288 dev->kvm->arch.gmap->pfault_enabled = 1;
1289 break;
1290 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1291 dev->kvm->arch.gmap->pfault_enabled = 0;
1292 /*
1293 * Make sure no async faults are in transition when
1294 * clearing the queues. So we don't need to worry
1295 * about late coming workers.
1296 */
1297 synchronize_srcu(&dev->kvm->srcu);
1298 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1299 kvm_clear_async_pf_completion_queue(vcpu);
1300 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001301 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1302 r = register_io_adapter(dev, attr);
1303 break;
1304 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1305 r = modify_io_adapter(dev, attr);
1306 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001307 default:
1308 r = -EINVAL;
1309 }
1310
1311 return r;
1312}
1313
1314static int flic_create(struct kvm_device *dev, u32 type)
1315{
1316 if (!dev)
1317 return -EINVAL;
1318 if (dev->kvm->arch.flic)
1319 return -EINVAL;
1320 dev->kvm->arch.flic = dev;
1321 return 0;
1322}
1323
1324static void flic_destroy(struct kvm_device *dev)
1325{
1326 dev->kvm->arch.flic = NULL;
1327 kfree(dev);
1328}
1329
1330/* s390 floating irq controller (flic) */
1331struct kvm_device_ops kvm_flic_ops = {
1332 .name = "kvm-flic",
1333 .get_attr = flic_get_attr,
1334 .set_attr = flic_set_attr,
1335 .create = flic_create,
1336 .destroy = flic_destroy,
1337};
Cornelia Huck84223592013-07-15 13:36:01 +02001338
1339static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1340{
1341 unsigned long bit;
1342
1343 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1344
1345 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1346}
1347
1348static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1349 u64 addr)
1350{
1351 struct s390_map_info *map;
1352
1353 if (!adapter)
1354 return NULL;
1355
1356 list_for_each_entry(map, &adapter->maps, list) {
1357 if (map->guest_addr == addr)
1358 return map;
1359 }
1360 return NULL;
1361}
1362
1363static int adapter_indicators_set(struct kvm *kvm,
1364 struct s390_io_adapter *adapter,
1365 struct kvm_s390_adapter_int *adapter_int)
1366{
1367 unsigned long bit;
1368 int summary_set, idx;
1369 struct s390_map_info *info;
1370 void *map;
1371
1372 info = get_map_info(adapter, adapter_int->ind_addr);
1373 if (!info)
1374 return -1;
1375 map = page_address(info->page);
1376 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1377 set_bit(bit, map);
1378 idx = srcu_read_lock(&kvm->srcu);
1379 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1380 set_page_dirty_lock(info->page);
1381 info = get_map_info(adapter, adapter_int->summary_addr);
1382 if (!info) {
1383 srcu_read_unlock(&kvm->srcu, idx);
1384 return -1;
1385 }
1386 map = page_address(info->page);
1387 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1388 adapter->swap);
1389 summary_set = test_and_set_bit(bit, map);
1390 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1391 set_page_dirty_lock(info->page);
1392 srcu_read_unlock(&kvm->srcu, idx);
1393 return summary_set ? 0 : 1;
1394}
1395
1396/*
1397 * < 0 - not injected due to error
1398 * = 0 - coalesced, summary indicator already active
1399 * > 0 - injected interrupt
1400 */
1401static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1402 struct kvm *kvm, int irq_source_id, int level,
1403 bool line_status)
1404{
1405 int ret;
1406 struct s390_io_adapter *adapter;
1407
1408 /* We're only interested in the 0->1 transition. */
1409 if (!level)
1410 return 0;
1411 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1412 if (!adapter)
1413 return -1;
1414 down_read(&adapter->maps_lock);
1415 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1416 up_read(&adapter->maps_lock);
1417 if ((ret > 0) && !adapter->masked) {
1418 struct kvm_s390_interrupt s390int = {
1419 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1420 .parm = 0,
1421 .parm64 = (adapter->isc << 27) | 0x80000000,
1422 };
1423 ret = kvm_s390_inject_vm(kvm, &s390int);
1424 if (ret == 0)
1425 ret = 1;
1426 }
1427 return ret;
1428}
1429
1430int kvm_set_routing_entry(struct kvm_irq_routing_table *rt,
1431 struct kvm_kernel_irq_routing_entry *e,
1432 const struct kvm_irq_routing_entry *ue)
1433{
1434 int ret;
1435
1436 switch (ue->type) {
1437 case KVM_IRQ_ROUTING_S390_ADAPTER:
1438 e->set = set_adapter_int;
1439 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1440 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1441 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1442 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1443 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1444 ret = 0;
1445 break;
1446 default:
1447 ret = -EINVAL;
1448 }
1449
1450 return ret;
1451}
1452
1453int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1454 int irq_source_id, int level, bool line_status)
1455{
1456 return -EINVAL;
1457}