blob: c49b4d4d310a2fba246f78ab71a7811ae13dc0d5 [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
David Hildenbrand87128362014-03-03 10:55:13 +0100177static int __deliver_prog_irq(struct kvm_vcpu *vcpu,
178 struct kvm_s390_pgm_info *pgm_info)
179{
180 const unsigned short table[] = { 2, 4, 4, 6 };
181 int rc = 0;
182
183 switch (pgm_info->code & ~PGM_PER) {
184 case PGM_AFX_TRANSLATION:
185 case PGM_ASX_TRANSLATION:
186 case PGM_EX_TRANSLATION:
187 case PGM_LFX_TRANSLATION:
188 case PGM_LSTE_SEQUENCE:
189 case PGM_LSX_TRANSLATION:
190 case PGM_LX_TRANSLATION:
191 case PGM_PRIMARY_AUTHORITY:
192 case PGM_SECONDARY_AUTHORITY:
193 case PGM_SPACE_SWITCH:
194 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
195 (u64 *)__LC_TRANS_EXC_CODE);
196 break;
197 case PGM_ALEN_TRANSLATION:
198 case PGM_ALE_SEQUENCE:
199 case PGM_ASTE_INSTANCE:
200 case PGM_ASTE_SEQUENCE:
201 case PGM_ASTE_VALIDITY:
202 case PGM_EXTENDED_AUTHORITY:
203 rc = put_guest_lc(vcpu, pgm_info->exc_access_id,
204 (u8 *)__LC_EXC_ACCESS_ID);
205 break;
206 case PGM_ASCE_TYPE:
207 case PGM_PAGE_TRANSLATION:
208 case PGM_REGION_FIRST_TRANS:
209 case PGM_REGION_SECOND_TRANS:
210 case PGM_REGION_THIRD_TRANS:
211 case PGM_SEGMENT_TRANSLATION:
212 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
213 (u64 *)__LC_TRANS_EXC_CODE);
214 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
215 (u8 *)__LC_EXC_ACCESS_ID);
216 rc |= put_guest_lc(vcpu, pgm_info->op_access_id,
217 (u8 *)__LC_OP_ACCESS_ID);
218 break;
219 case PGM_MONITOR:
220 rc = put_guest_lc(vcpu, pgm_info->mon_class_nr,
221 (u64 *)__LC_MON_CLASS_NR);
222 rc |= put_guest_lc(vcpu, pgm_info->mon_code,
223 (u64 *)__LC_MON_CODE);
224 break;
225 case PGM_DATA:
226 rc = put_guest_lc(vcpu, pgm_info->data_exc_code,
227 (u32 *)__LC_DATA_EXC_CODE);
228 break;
229 case PGM_PROTECTION:
230 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
231 (u64 *)__LC_TRANS_EXC_CODE);
232 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
233 (u8 *)__LC_EXC_ACCESS_ID);
234 break;
235 }
236
237 if (pgm_info->code & PGM_PER) {
238 rc |= put_guest_lc(vcpu, pgm_info->per_code,
239 (u8 *) __LC_PER_CODE);
240 rc |= put_guest_lc(vcpu, pgm_info->per_atmid,
241 (u8 *)__LC_PER_ATMID);
242 rc |= put_guest_lc(vcpu, pgm_info->per_address,
243 (u64 *) __LC_PER_ADDRESS);
244 rc |= put_guest_lc(vcpu, pgm_info->per_access_id,
245 (u8 *) __LC_PER_ACCESS_ID);
246 }
247
248 switch (vcpu->arch.sie_block->icptcode) {
249 case ICPT_INST:
250 case ICPT_INSTPROGI:
251 case ICPT_OPEREXC:
252 case ICPT_PARTEXEC:
253 case ICPT_IOINST:
254 /* last instruction only stored for these icptcodes */
255 rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
256 (u16 *) __LC_PGM_ILC);
257 break;
258 case ICPT_PROGI:
259 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->pgmilc,
260 (u16 *) __LC_PGM_ILC);
261 break;
262 default:
263 rc |= put_guest_lc(vcpu, 0,
264 (u16 *) __LC_PGM_ILC);
265 }
266
267 rc |= put_guest_lc(vcpu, pgm_info->code,
268 (u16 *)__LC_PGM_INT_CODE);
269 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
270 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
271 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
272 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
273
274 return rc;
275}
276
Carsten Otteba5c1e92008-03-25 18:47:26 +0100277static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200278 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100279{
280 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100281 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100282
283 switch (inti->type) {
284 case KVM_S390_INT_EMERGENCY:
285 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
286 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200287 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
288 inti->emerg.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100289 rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE);
290 rc |= put_guest_lc(vcpu, inti->emerg.code,
291 (u16 *)__LC_EXT_CPU_ADDR);
292 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
293 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
294 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100295 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100296 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200297 case KVM_S390_INT_EXTERNAL_CALL:
298 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
299 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200300 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
301 inti->extcall.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100302 rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE);
303 rc |= put_guest_lc(vcpu, inti->extcall.code,
304 (u16 *)__LC_EXT_CPU_ADDR);
305 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
306 &vcpu->arch.sie_block->gpsw,
307 sizeof(psw_t));
308 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
309 &vcpu->arch.sie_block->gpsw,
310 sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200311 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100312 case KVM_S390_INT_SERVICE:
313 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
314 inti->ext.ext_params);
315 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200316 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
317 inti->ext.ext_params, 0);
Heiko Carstens79882762014-01-02 10:59:41 +0100318 rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE);
319 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
320 &vcpu->arch.sie_block->gpsw,
321 sizeof(psw_t));
322 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100323 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100324 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
325 (u32 *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100326 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200327 case KVM_S390_INT_PFAULT_INIT:
328 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
329 inti->ext.ext_params2);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100330 rc = put_guest_lc(vcpu, 0x2603, (u16 *) __LC_EXT_INT_CODE);
331 rc |= put_guest_lc(vcpu, 0x0600, (u16 *) __LC_EXT_CPU_ADDR);
332 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
333 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
334 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200335 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann1a03b7642014-02-12 14:05:38 +0100336 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
337 (u64 *) __LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200338 break;
339 case KVM_S390_INT_PFAULT_DONE:
340 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
341 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100342 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
343 rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR);
344 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
345 &vcpu->arch.sie_block->gpsw,
346 sizeof(psw_t));
347 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200348 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100349 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
350 (u64 *)__LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200351 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100352 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100353 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100354 inti->ext.ext_params, inti->ext.ext_params2);
355 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200356 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
357 inti->ext.ext_params,
358 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100359 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
360 rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR);
361 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
362 &vcpu->arch.sie_block->gpsw,
363 sizeof(psw_t));
364 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100365 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100366 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
367 (u32 *)__LC_EXT_PARAMS);
368 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
369 (u64 *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100370 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100371 case KVM_S390_SIGP_STOP:
372 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
373 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200374 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
375 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100376 __set_intercept_indicator(vcpu, inti);
377 break;
378
379 case KVM_S390_SIGP_SET_PREFIX:
380 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
381 inti->prefix.address);
382 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200383 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
384 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100385 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100386 break;
387
388 case KVM_S390_RESTART:
389 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
390 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200391 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
392 0, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100393 rc = write_guest_lc(vcpu,
394 offsetof(struct _lowcore, restart_old_psw),
395 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
396 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
397 &vcpu->arch.sie_block->gpsw,
398 sizeof(psw_t));
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100399 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100400 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100401 case KVM_S390_PROGRAM_INT:
402 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
403 inti->pgm.code,
404 table[vcpu->arch.sie_block->ipa >> 14]);
405 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200406 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
407 inti->pgm.code, 0);
David Hildenbrand87128362014-03-03 10:55:13 +0100408 rc = __deliver_prog_irq(vcpu, &inti->pgm);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100409 break;
410
Cornelia Huck48a3e952012-12-20 15:32:09 +0100411 case KVM_S390_MCHK:
412 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
413 inti->mchk.mcic);
414 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
415 inti->mchk.cr14,
416 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100417 rc = kvm_s390_vcpu_store_status(vcpu,
418 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens79882762014-01-02 10:59:41 +0100419 rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE);
420 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
421 &vcpu->arch.sie_block->gpsw,
422 sizeof(psw_t));
423 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100424 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100425 break;
426
Cornelia Huckd8346b72012-12-20 15:32:08 +0100427 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
428 {
429 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
430 inti->io.subchannel_nr;
431 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
432 inti->io.io_int_word;
433 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
434 vcpu->stat.deliver_io_int++;
435 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
436 param0, param1);
Heiko Carstens79882762014-01-02 10:59:41 +0100437 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
438 (u16 *)__LC_SUBCHANNEL_ID);
439 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
440 (u16 *)__LC_SUBCHANNEL_NR);
441 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
442 (u32 *)__LC_IO_INT_PARM);
443 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
444 (u32 *)__LC_IO_INT_WORD);
445 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
446 &vcpu->arch.sie_block->gpsw,
447 sizeof(psw_t));
448 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
449 &vcpu->arch.sie_block->gpsw,
450 sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100451 break;
452 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100453 default:
454 BUG();
455 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100456 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200457 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100458 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200459 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100460 }
461}
462
463static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
464{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100465 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100466
467 if (psw_extint_disabled(vcpu))
468 return 0;
469 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
470 return 0;
Jens Freimann1a03b7642014-02-12 14:05:38 +0100471 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
472 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
473 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
474 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
475 &vcpu->arch.sie_block->gpsw,
476 sizeof(psw_t));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100477 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200478 printk("kvm: The guest lowcore is not mapped during interrupt "
479 "delivery, killing userspace\n");
480 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100481 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100482 return 1;
483}
484
Dominik Dingel3c038e62013-10-07 17:11:48 +0200485int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100486{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200487 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
488 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
489 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100490 int rc = 0;
491
492 if (atomic_read(&li->active)) {
493 spin_lock_bh(&li->lock);
494 list_for_each_entry(inti, &li->list, list)
495 if (__interrupt_is_deliverable(vcpu, inti)) {
496 rc = 1;
497 break;
498 }
499 spin_unlock_bh(&li->lock);
500 }
501
502 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200503 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100504 list_for_each_entry(inti, &fi->list, list)
505 if (__interrupt_is_deliverable(vcpu, inti)) {
506 rc = 1;
507 break;
508 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200509 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100510 }
511
512 if ((!rc) && (vcpu->arch.sie_block->ckc <
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200513 get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100514 if ((!psw_extint_disabled(vcpu)) &&
515 (vcpu->arch.sie_block->gcr[0] & 0x800ul))
516 rc = 1;
517 }
518
519 return rc;
520}
521
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300522int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
523{
524 return 0;
525}
526
Carsten Otteba5c1e92008-03-25 18:47:26 +0100527int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
528{
529 u64 now, sltime;
530 DECLARE_WAITQUEUE(wait, current);
531
532 vcpu->stat.exit_wait_state++;
533 if (kvm_cpu_has_interrupt(vcpu))
534 return 0;
535
Carsten Ottee52b2af2008-05-21 13:37:44 +0200536 __set_cpu_idle(vcpu);
537 spin_lock_bh(&vcpu->arch.local_int.lock);
538 vcpu->arch.local_int.timer_due = 0;
539 spin_unlock_bh(&vcpu->arch.local_int.lock);
540
Carsten Otteba5c1e92008-03-25 18:47:26 +0100541 if (psw_interrupts_disabled(vcpu)) {
542 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
543 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100544 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100545 }
546
547 if (psw_extint_disabled(vcpu) ||
548 (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
549 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
550 goto no_timer;
551 }
552
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200553 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100554 if (vcpu->arch.sie_block->ckc < now) {
555 __unset_cpu_idle(vcpu);
556 return 0;
557 }
558
Heiko Carstensed4f2092013-01-14 16:55:55 +0100559 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100560
Christian Borntraegerca872302009-05-12 17:21:49 +0200561 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
562 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100563no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200564 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200565 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100566 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200567 add_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100568 while (list_empty(&vcpu->arch.local_int.list) &&
569 list_empty(&vcpu->arch.local_int.float_int->list) &&
570 (!vcpu->arch.local_int.timer_due) &&
571 !signal_pending(current)) {
572 set_current_state(TASK_INTERRUPTIBLE);
573 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200574 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100575 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200576 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100577 spin_lock_bh(&vcpu->arch.local_int.lock);
578 }
579 __unset_cpu_idle(vcpu);
580 __set_current_state(TASK_RUNNING);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200581 remove_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100582 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200583 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Thomas Huth800c1062013-09-12 10:33:45 +0200584 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
585
Christian Borntraegerca872302009-05-12 17:21:49 +0200586 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100587 return 0;
588}
589
Christian Borntraegerca872302009-05-12 17:21:49 +0200590void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100591{
Christian Borntraegerca872302009-05-12 17:21:49 +0200592 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100593
Christian Borntraegerca872302009-05-12 17:21:49 +0200594 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100595 vcpu->arch.local_int.timer_due = 1;
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200596 if (waitqueue_active(&vcpu->wq))
597 wake_up_interruptible(&vcpu->wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200598 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100599}
600
Christian Borntraegerca872302009-05-12 17:21:49 +0200601/*
602 * low level hrtimer wake routine. Because this runs in hardirq context
603 * we schedule a tasklet to do the real work.
604 */
605enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
606{
607 struct kvm_vcpu *vcpu;
608
609 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100610 vcpu->preempted = true;
Christian Borntraegerca872302009-05-12 17:21:49 +0200611 tasklet_schedule(&vcpu->arch.tasklet);
612
613 return HRTIMER_NORESTART;
614}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100615
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100616void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
617{
618 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
619 struct kvm_s390_interrupt_info *n, *inti = NULL;
620
621 spin_lock_bh(&li->lock);
622 list_for_each_entry_safe(inti, n, &li->list, list) {
623 list_del(&inti->list);
624 kfree(inti);
625 }
626 atomic_set(&li->active, 0);
627 spin_unlock_bh(&li->lock);
628}
629
Carsten Otteba5c1e92008-03-25 18:47:26 +0100630void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
631{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200632 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
633 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
634 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100635 int deliver;
636
637 __reset_intercept_indicators(vcpu);
638 if (atomic_read(&li->active)) {
639 do {
640 deliver = 0;
641 spin_lock_bh(&li->lock);
642 list_for_each_entry_safe(inti, n, &li->list, list) {
643 if (__interrupt_is_deliverable(vcpu, inti)) {
644 list_del(&inti->list);
645 deliver = 1;
646 break;
647 }
648 __set_intercept_indicator(vcpu, inti);
649 }
650 if (list_empty(&li->list))
651 atomic_set(&li->active, 0);
652 spin_unlock_bh(&li->lock);
653 if (deliver) {
654 __do_deliver_interrupt(vcpu, inti);
655 kfree(inti);
656 }
657 } while (deliver);
658 }
659
660 if ((vcpu->arch.sie_block->ckc <
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200661 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
Carsten Otteba5c1e92008-03-25 18:47:26 +0100662 __try_deliver_ckc_interrupt(vcpu);
663
664 if (atomic_read(&fi->active)) {
665 do {
666 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200667 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100668 list_for_each_entry_safe(inti, n, &fi->list, list) {
669 if (__interrupt_is_deliverable(vcpu, inti)) {
670 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100671 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100672 deliver = 1;
673 break;
674 }
675 __set_intercept_indicator(vcpu, inti);
676 }
677 if (list_empty(&fi->list))
678 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200679 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100680 if (deliver) {
681 __do_deliver_interrupt(vcpu, inti);
682 kfree(inti);
683 }
684 } while (deliver);
685 }
686}
687
Cornelia Huck48a3e952012-12-20 15:32:09 +0100688void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
689{
690 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
691 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
692 struct kvm_s390_interrupt_info *n, *inti = NULL;
693 int deliver;
694
695 __reset_intercept_indicators(vcpu);
696 if (atomic_read(&li->active)) {
697 do {
698 deliver = 0;
699 spin_lock_bh(&li->lock);
700 list_for_each_entry_safe(inti, n, &li->list, list) {
701 if ((inti->type == KVM_S390_MCHK) &&
702 __interrupt_is_deliverable(vcpu, inti)) {
703 list_del(&inti->list);
704 deliver = 1;
705 break;
706 }
707 __set_intercept_indicator(vcpu, inti);
708 }
709 if (list_empty(&li->list))
710 atomic_set(&li->active, 0);
711 spin_unlock_bh(&li->lock);
712 if (deliver) {
713 __do_deliver_interrupt(vcpu, inti);
714 kfree(inti);
715 }
716 } while (deliver);
717 }
718
719 if (atomic_read(&fi->active)) {
720 do {
721 deliver = 0;
722 spin_lock(&fi->lock);
723 list_for_each_entry_safe(inti, n, &fi->list, list) {
724 if ((inti->type == KVM_S390_MCHK) &&
725 __interrupt_is_deliverable(vcpu, inti)) {
726 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100727 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100728 deliver = 1;
729 break;
730 }
731 __set_intercept_indicator(vcpu, inti);
732 }
733 if (list_empty(&fi->list))
734 atomic_set(&fi->active, 0);
735 spin_unlock(&fi->lock);
736 if (deliver) {
737 __do_deliver_interrupt(vcpu, inti);
738 kfree(inti);
739 }
740 } while (deliver);
741 }
742}
743
Carsten Otteba5c1e92008-03-25 18:47:26 +0100744int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
745{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200746 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
747 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100748
749 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
750 if (!inti)
751 return -ENOMEM;
752
Joe Perchesa419aef2009-08-18 11:18:35 -0700753 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100754 inti->pgm.code = code;
755
756 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200757 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100758 spin_lock_bh(&li->lock);
759 list_add(&inti->list, &li->list);
760 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200761 BUG_ON(waitqueue_active(li->wq));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100762 spin_unlock_bh(&li->lock);
763 return 0;
764}
765
Jens Freimannbcd84682014-02-11 11:07:05 +0100766int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
767 struct kvm_s390_pgm_info *pgm_info)
768{
769 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
770 struct kvm_s390_interrupt_info *inti;
771
772 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
773 if (!inti)
774 return -ENOMEM;
775
776 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
777 pgm_info->code);
778 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
779 pgm_info->code, 0, 1);
780
781 inti->type = KVM_S390_PROGRAM_INT;
782 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
783 spin_lock_bh(&li->lock);
784 list_add(&inti->list, &li->list);
785 atomic_set(&li->active, 1);
786 BUG_ON(waitqueue_active(li->wq));
787 spin_unlock_bh(&li->lock);
788 return 0;
789}
790
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100791struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
792 u64 cr6, u64 schid)
793{
794 struct kvm_s390_float_interrupt *fi;
795 struct kvm_s390_interrupt_info *inti, *iter;
796
797 if ((!schid && !cr6) || (schid && cr6))
798 return NULL;
799 mutex_lock(&kvm->lock);
800 fi = &kvm->arch.float_int;
801 spin_lock(&fi->lock);
802 inti = NULL;
803 list_for_each_entry(iter, &fi->list, list) {
804 if (!is_ioint(iter->type))
805 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100806 if (cr6 &&
807 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100808 continue;
809 if (schid) {
810 if (((schid & 0x00000000ffff0000) >> 16) !=
811 iter->io.subchannel_id)
812 continue;
813 if ((schid & 0x000000000000ffff) !=
814 iter->io.subchannel_nr)
815 continue;
816 }
817 inti = iter;
818 break;
819 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100820 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100821 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100822 fi->irq_count--;
823 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100824 if (list_empty(&fi->list))
825 atomic_set(&fi->active, 0);
826 spin_unlock(&fi->lock);
827 mutex_unlock(&kvm->lock);
828 return inti;
829}
830
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100831static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100832{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200833 struct kvm_s390_local_interrupt *li;
834 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200835 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100836 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100837 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100838 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100839
Carsten Otteba5c1e92008-03-25 18:47:26 +0100840 mutex_lock(&kvm->lock);
841 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200842 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100843 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
844 rc = -EINVAL;
845 goto unlock_fi;
846 }
847 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200848 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100849 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200850 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100851 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
852
Cornelia Huckd8346b72012-12-20 15:32:08 +0100853 /* Keep I/O interrupts sorted in isc order. */
854 list_for_each_entry(iter, &fi->list, list) {
855 if (!is_ioint(iter->type))
856 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100857 if (int_word_to_isc_bits(iter->io.io_int_word)
858 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100859 continue;
860 break;
861 }
862 list_add_tail(&inti->list, &iter->list);
863 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100864 atomic_set(&fi->active, 1);
865 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
866 if (sigcpu == KVM_MAX_VCPUS) {
867 do {
868 sigcpu = fi->next_rr_cpu++;
869 if (sigcpu == KVM_MAX_VCPUS)
870 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100871 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100872 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100873 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
874 li = &dst_vcpu->arch.local_int;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100875 spin_lock_bh(&li->lock);
876 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200877 if (waitqueue_active(li->wq))
878 wake_up_interruptible(li->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100879 kvm_get_vcpu(kvm, sigcpu)->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100880 spin_unlock_bh(&li->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100881unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200882 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100883 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100884 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200885}
886
887int kvm_s390_inject_vm(struct kvm *kvm,
888 struct kvm_s390_interrupt *s390int)
889{
890 struct kvm_s390_interrupt_info *inti;
891
892 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
893 if (!inti)
894 return -ENOMEM;
895
896 inti->type = s390int->type;
897 switch (inti->type) {
898 case KVM_S390_INT_VIRTIO:
899 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
900 s390int->parm, s390int->parm64);
901 inti->ext.ext_params = s390int->parm;
902 inti->ext.ext_params2 = s390int->parm64;
903 break;
904 case KVM_S390_INT_SERVICE:
905 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
906 inti->ext.ext_params = s390int->parm;
907 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200908 case KVM_S390_INT_PFAULT_DONE:
909 inti->type = s390int->type;
910 inti->ext.ext_params2 = s390int->parm64;
911 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200912 case KVM_S390_MCHK:
913 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
914 s390int->parm64);
915 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
916 inti->mchk.mcic = s390int->parm64;
917 break;
918 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
919 if (inti->type & IOINT_AI_MASK)
920 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
921 else
922 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
923 s390int->type & IOINT_CSSID_MASK,
924 s390int->type & IOINT_SSID_MASK,
925 s390int->type & IOINT_SCHID_MASK);
926 inti->io.subchannel_id = s390int->parm >> 16;
927 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
928 inti->io.io_int_parm = s390int->parm64 >> 32;
929 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
930 break;
931 default:
932 kfree(inti);
933 return -EINVAL;
934 }
935 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
936 2);
937
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100938 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100939}
940
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100941void kvm_s390_reinject_io_int(struct kvm *kvm,
942 struct kvm_s390_interrupt_info *inti)
943{
944 __inject_vm(kvm, inti);
945}
946
Carsten Otteba5c1e92008-03-25 18:47:26 +0100947int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
948 struct kvm_s390_interrupt *s390int)
949{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200950 struct kvm_s390_local_interrupt *li;
951 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100952
953 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
954 if (!inti)
955 return -ENOMEM;
956
957 switch (s390int->type) {
958 case KVM_S390_PROGRAM_INT:
959 if (s390int->parm & 0xffff0000) {
960 kfree(inti);
961 return -EINVAL;
962 }
963 inti->type = s390int->type;
964 inti->pgm.code = s390int->parm;
965 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
966 s390int->parm);
967 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100968 case KVM_S390_SIGP_SET_PREFIX:
969 inti->prefix.address = s390int->parm;
970 inti->type = s390int->type;
971 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
972 s390int->parm);
973 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100974 case KVM_S390_SIGP_STOP:
975 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100976 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
977 inti->type = s390int->type;
978 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200979 case KVM_S390_INT_EXTERNAL_CALL:
980 if (s390int->parm & 0xffff0000) {
981 kfree(inti);
982 return -EINVAL;
983 }
984 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
985 s390int->parm);
986 inti->type = s390int->type;
987 inti->extcall.code = s390int->parm;
988 break;
989 case KVM_S390_INT_EMERGENCY:
990 if (s390int->parm & 0xffff0000) {
991 kfree(inti);
992 return -EINVAL;
993 }
994 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
995 inti->type = s390int->type;
996 inti->emerg.code = s390int->parm;
997 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100998 case KVM_S390_MCHK:
999 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1000 s390int->parm64);
1001 inti->type = s390int->type;
1002 inti->mchk.mcic = s390int->parm64;
1003 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001004 case KVM_S390_INT_PFAULT_INIT:
1005 inti->type = s390int->type;
1006 inti->ext.ext_params2 = s390int->parm64;
1007 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001008 case KVM_S390_INT_VIRTIO:
1009 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001010 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001011 default:
1012 kfree(inti);
1013 return -EINVAL;
1014 }
Cornelia Huckade38c32012-07-23 17:20:30 +02001015 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
1016 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001017
1018 mutex_lock(&vcpu->kvm->lock);
1019 li = &vcpu->arch.local_int;
1020 spin_lock_bh(&li->lock);
1021 if (inti->type == KVM_S390_PROGRAM_INT)
1022 list_add(&inti->list, &li->list);
1023 else
1024 list_add_tail(&inti->list, &li->list);
1025 atomic_set(&li->active, 1);
1026 if (inti->type == KVM_S390_SIGP_STOP)
1027 li->action_bits |= ACTION_STOP_ON_STOP;
1028 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001029 if (waitqueue_active(&vcpu->wq))
1030 wake_up_interruptible(&vcpu->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +01001031 vcpu->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001032 spin_unlock_bh(&li->lock);
1033 mutex_unlock(&vcpu->kvm->lock);
1034 return 0;
1035}
Jens Freimannc05c4182013-10-07 16:13:45 +02001036
1037static void clear_floating_interrupts(struct kvm *kvm)
1038{
1039 struct kvm_s390_float_interrupt *fi;
1040 struct kvm_s390_interrupt_info *n, *inti = NULL;
1041
1042 mutex_lock(&kvm->lock);
1043 fi = &kvm->arch.float_int;
1044 spin_lock(&fi->lock);
1045 list_for_each_entry_safe(inti, n, &fi->list, list) {
1046 list_del(&inti->list);
1047 kfree(inti);
1048 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001049 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001050 atomic_set(&fi->active, 0);
1051 spin_unlock(&fi->lock);
1052 mutex_unlock(&kvm->lock);
1053}
1054
1055static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1056 u8 *addr)
1057{
1058 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1059 struct kvm_s390_irq irq = {0};
1060
1061 irq.type = inti->type;
1062 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001063 case KVM_S390_INT_PFAULT_INIT:
1064 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001065 case KVM_S390_INT_VIRTIO:
1066 case KVM_S390_INT_SERVICE:
1067 irq.u.ext = inti->ext;
1068 break;
1069 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1070 irq.u.io = inti->io;
1071 break;
1072 case KVM_S390_MCHK:
1073 irq.u.mchk = inti->mchk;
1074 break;
1075 default:
1076 return -EINVAL;
1077 }
1078
1079 if (copy_to_user(uptr, &irq, sizeof(irq)))
1080 return -EFAULT;
1081
1082 return 0;
1083}
1084
1085static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1086{
1087 struct kvm_s390_interrupt_info *inti;
1088 struct kvm_s390_float_interrupt *fi;
1089 int ret = 0;
1090 int n = 0;
1091
1092 mutex_lock(&kvm->lock);
1093 fi = &kvm->arch.float_int;
1094 spin_lock(&fi->lock);
1095
1096 list_for_each_entry(inti, &fi->list, list) {
1097 if (len < sizeof(struct kvm_s390_irq)) {
1098 /* signal userspace to try again */
1099 ret = -ENOMEM;
1100 break;
1101 }
1102 ret = copy_irq_to_user(inti, buf);
1103 if (ret)
1104 break;
1105 buf += sizeof(struct kvm_s390_irq);
1106 len -= sizeof(struct kvm_s390_irq);
1107 n++;
1108 }
1109
1110 spin_unlock(&fi->lock);
1111 mutex_unlock(&kvm->lock);
1112
1113 return ret < 0 ? ret : n;
1114}
1115
1116static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1117{
1118 int r;
1119
1120 switch (attr->group) {
1121 case KVM_DEV_FLIC_GET_ALL_IRQS:
1122 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1123 attr->attr);
1124 break;
1125 default:
1126 r = -EINVAL;
1127 }
1128
1129 return r;
1130}
1131
1132static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1133 u64 addr)
1134{
1135 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1136 void *target = NULL;
1137 void __user *source;
1138 u64 size;
1139
1140 if (get_user(inti->type, (u64 __user *)addr))
1141 return -EFAULT;
1142
1143 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001144 case KVM_S390_INT_PFAULT_INIT:
1145 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001146 case KVM_S390_INT_VIRTIO:
1147 case KVM_S390_INT_SERVICE:
1148 target = (void *) &inti->ext;
1149 source = &uptr->u.ext;
1150 size = sizeof(inti->ext);
1151 break;
1152 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1153 target = (void *) &inti->io;
1154 source = &uptr->u.io;
1155 size = sizeof(inti->io);
1156 break;
1157 case KVM_S390_MCHK:
1158 target = (void *) &inti->mchk;
1159 source = &uptr->u.mchk;
1160 size = sizeof(inti->mchk);
1161 break;
1162 default:
1163 return -EINVAL;
1164 }
1165
1166 if (copy_from_user(target, source, size))
1167 return -EFAULT;
1168
1169 return 0;
1170}
1171
1172static int enqueue_floating_irq(struct kvm_device *dev,
1173 struct kvm_device_attr *attr)
1174{
1175 struct kvm_s390_interrupt_info *inti = NULL;
1176 int r = 0;
1177 int len = attr->attr;
1178
1179 if (len % sizeof(struct kvm_s390_irq) != 0)
1180 return -EINVAL;
1181 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1182 return -EINVAL;
1183
1184 while (len >= sizeof(struct kvm_s390_irq)) {
1185 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1186 if (!inti)
1187 return -ENOMEM;
1188
1189 r = copy_irq_from_user(inti, attr->addr);
1190 if (r) {
1191 kfree(inti);
1192 return r;
1193 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001194 r = __inject_vm(dev->kvm, inti);
1195 if (r) {
1196 kfree(inti);
1197 return r;
1198 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001199 len -= sizeof(struct kvm_s390_irq);
1200 attr->addr += sizeof(struct kvm_s390_irq);
1201 }
1202
1203 return r;
1204}
1205
Cornelia Huck841b91c2013-07-15 13:36:01 +02001206static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1207{
1208 if (id >= MAX_S390_IO_ADAPTERS)
1209 return NULL;
1210 return kvm->arch.adapters[id];
1211}
1212
1213static int register_io_adapter(struct kvm_device *dev,
1214 struct kvm_device_attr *attr)
1215{
1216 struct s390_io_adapter *adapter;
1217 struct kvm_s390_io_adapter adapter_info;
1218
1219 if (copy_from_user(&adapter_info,
1220 (void __user *)attr->addr, sizeof(adapter_info)))
1221 return -EFAULT;
1222
1223 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1224 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1225 return -EINVAL;
1226
1227 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1228 if (!adapter)
1229 return -ENOMEM;
1230
1231 INIT_LIST_HEAD(&adapter->maps);
1232 init_rwsem(&adapter->maps_lock);
1233 atomic_set(&adapter->nr_maps, 0);
1234 adapter->id = adapter_info.id;
1235 adapter->isc = adapter_info.isc;
1236 adapter->maskable = adapter_info.maskable;
1237 adapter->masked = false;
1238 adapter->swap = adapter_info.swap;
1239 dev->kvm->arch.adapters[adapter->id] = adapter;
1240
1241 return 0;
1242}
1243
1244int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1245{
1246 int ret;
1247 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1248
1249 if (!adapter || !adapter->maskable)
1250 return -EINVAL;
1251 ret = adapter->masked;
1252 adapter->masked = masked;
1253 return ret;
1254}
1255
1256static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1257{
1258 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1259 struct s390_map_info *map;
1260 int ret;
1261
1262 if (!adapter || !addr)
1263 return -EINVAL;
1264
1265 map = kzalloc(sizeof(*map), GFP_KERNEL);
1266 if (!map) {
1267 ret = -ENOMEM;
1268 goto out;
1269 }
1270 INIT_LIST_HEAD(&map->list);
1271 map->guest_addr = addr;
1272 map->addr = gmap_translate(addr, kvm->arch.gmap);
1273 if (map->addr == -EFAULT) {
1274 ret = -EFAULT;
1275 goto out;
1276 }
1277 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1278 if (ret < 0)
1279 goto out;
1280 BUG_ON(ret != 1);
1281 down_write(&adapter->maps_lock);
1282 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1283 list_add_tail(&map->list, &adapter->maps);
1284 ret = 0;
1285 } else {
1286 put_page(map->page);
1287 ret = -EINVAL;
1288 }
1289 up_write(&adapter->maps_lock);
1290out:
1291 if (ret)
1292 kfree(map);
1293 return ret;
1294}
1295
1296static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1297{
1298 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1299 struct s390_map_info *map, *tmp;
1300 int found = 0;
1301
1302 if (!adapter || !addr)
1303 return -EINVAL;
1304
1305 down_write(&adapter->maps_lock);
1306 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1307 if (map->guest_addr == addr) {
1308 found = 1;
1309 atomic_dec(&adapter->nr_maps);
1310 list_del(&map->list);
1311 put_page(map->page);
1312 kfree(map);
1313 break;
1314 }
1315 }
1316 up_write(&adapter->maps_lock);
1317
1318 return found ? 0 : -EINVAL;
1319}
1320
1321void kvm_s390_destroy_adapters(struct kvm *kvm)
1322{
1323 int i;
1324 struct s390_map_info *map, *tmp;
1325
1326 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1327 if (!kvm->arch.adapters[i])
1328 continue;
1329 list_for_each_entry_safe(map, tmp,
1330 &kvm->arch.adapters[i]->maps, list) {
1331 list_del(&map->list);
1332 put_page(map->page);
1333 kfree(map);
1334 }
1335 kfree(kvm->arch.adapters[i]);
1336 }
1337}
1338
1339static int modify_io_adapter(struct kvm_device *dev,
1340 struct kvm_device_attr *attr)
1341{
1342 struct kvm_s390_io_adapter_req req;
1343 struct s390_io_adapter *adapter;
1344 int ret;
1345
1346 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1347 return -EFAULT;
1348
1349 adapter = get_io_adapter(dev->kvm, req.id);
1350 if (!adapter)
1351 return -EINVAL;
1352 switch (req.type) {
1353 case KVM_S390_IO_ADAPTER_MASK:
1354 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1355 if (ret > 0)
1356 ret = 0;
1357 break;
1358 case KVM_S390_IO_ADAPTER_MAP:
1359 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1360 break;
1361 case KVM_S390_IO_ADAPTER_UNMAP:
1362 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1363 break;
1364 default:
1365 ret = -EINVAL;
1366 }
1367
1368 return ret;
1369}
1370
Jens Freimannc05c4182013-10-07 16:13:45 +02001371static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1372{
1373 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001374 unsigned int i;
1375 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001376
1377 switch (attr->group) {
1378 case KVM_DEV_FLIC_ENQUEUE:
1379 r = enqueue_floating_irq(dev, attr);
1380 break;
1381 case KVM_DEV_FLIC_CLEAR_IRQS:
1382 r = 0;
1383 clear_floating_interrupts(dev->kvm);
1384 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001385 case KVM_DEV_FLIC_APF_ENABLE:
1386 dev->kvm->arch.gmap->pfault_enabled = 1;
1387 break;
1388 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1389 dev->kvm->arch.gmap->pfault_enabled = 0;
1390 /*
1391 * Make sure no async faults are in transition when
1392 * clearing the queues. So we don't need to worry
1393 * about late coming workers.
1394 */
1395 synchronize_srcu(&dev->kvm->srcu);
1396 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1397 kvm_clear_async_pf_completion_queue(vcpu);
1398 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001399 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1400 r = register_io_adapter(dev, attr);
1401 break;
1402 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1403 r = modify_io_adapter(dev, attr);
1404 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001405 default:
1406 r = -EINVAL;
1407 }
1408
1409 return r;
1410}
1411
1412static int flic_create(struct kvm_device *dev, u32 type)
1413{
1414 if (!dev)
1415 return -EINVAL;
1416 if (dev->kvm->arch.flic)
1417 return -EINVAL;
1418 dev->kvm->arch.flic = dev;
1419 return 0;
1420}
1421
1422static void flic_destroy(struct kvm_device *dev)
1423{
1424 dev->kvm->arch.flic = NULL;
1425 kfree(dev);
1426}
1427
1428/* s390 floating irq controller (flic) */
1429struct kvm_device_ops kvm_flic_ops = {
1430 .name = "kvm-flic",
1431 .get_attr = flic_get_attr,
1432 .set_attr = flic_set_attr,
1433 .create = flic_create,
1434 .destroy = flic_destroy,
1435};
Cornelia Huck84223592013-07-15 13:36:01 +02001436
1437static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1438{
1439 unsigned long bit;
1440
1441 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1442
1443 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1444}
1445
1446static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1447 u64 addr)
1448{
1449 struct s390_map_info *map;
1450
1451 if (!adapter)
1452 return NULL;
1453
1454 list_for_each_entry(map, &adapter->maps, list) {
1455 if (map->guest_addr == addr)
1456 return map;
1457 }
1458 return NULL;
1459}
1460
1461static int adapter_indicators_set(struct kvm *kvm,
1462 struct s390_io_adapter *adapter,
1463 struct kvm_s390_adapter_int *adapter_int)
1464{
1465 unsigned long bit;
1466 int summary_set, idx;
1467 struct s390_map_info *info;
1468 void *map;
1469
1470 info = get_map_info(adapter, adapter_int->ind_addr);
1471 if (!info)
1472 return -1;
1473 map = page_address(info->page);
1474 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1475 set_bit(bit, map);
1476 idx = srcu_read_lock(&kvm->srcu);
1477 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1478 set_page_dirty_lock(info->page);
1479 info = get_map_info(adapter, adapter_int->summary_addr);
1480 if (!info) {
1481 srcu_read_unlock(&kvm->srcu, idx);
1482 return -1;
1483 }
1484 map = page_address(info->page);
1485 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1486 adapter->swap);
1487 summary_set = test_and_set_bit(bit, map);
1488 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1489 set_page_dirty_lock(info->page);
1490 srcu_read_unlock(&kvm->srcu, idx);
1491 return summary_set ? 0 : 1;
1492}
1493
1494/*
1495 * < 0 - not injected due to error
1496 * = 0 - coalesced, summary indicator already active
1497 * > 0 - injected interrupt
1498 */
1499static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1500 struct kvm *kvm, int irq_source_id, int level,
1501 bool line_status)
1502{
1503 int ret;
1504 struct s390_io_adapter *adapter;
1505
1506 /* We're only interested in the 0->1 transition. */
1507 if (!level)
1508 return 0;
1509 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1510 if (!adapter)
1511 return -1;
1512 down_read(&adapter->maps_lock);
1513 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1514 up_read(&adapter->maps_lock);
1515 if ((ret > 0) && !adapter->masked) {
1516 struct kvm_s390_interrupt s390int = {
1517 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1518 .parm = 0,
1519 .parm64 = (adapter->isc << 27) | 0x80000000,
1520 };
1521 ret = kvm_s390_inject_vm(kvm, &s390int);
1522 if (ret == 0)
1523 ret = 1;
1524 }
1525 return ret;
1526}
1527
1528int kvm_set_routing_entry(struct kvm_irq_routing_table *rt,
1529 struct kvm_kernel_irq_routing_entry *e,
1530 const struct kvm_irq_routing_entry *ue)
1531{
1532 int ret;
1533
1534 switch (ue->type) {
1535 case KVM_IRQ_ROUTING_S390_ADAPTER:
1536 e->set = set_adapter_int;
1537 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1538 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1539 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1540 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1541 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1542 ret = 0;
1543 break;
1544 default:
1545 ret = -EINVAL;
1546 }
1547
1548 return ret;
1549}
1550
1551int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1552 int irq_source_id, int level, bool line_status)
1553{
1554 return -EINVAL;
1555}