blob: f331014dd7668b0a757c1dd04cd0fa3f6ec05201 [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;
David Hildenbrand27291e22014-01-23 12:26:52 +0100134 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
135
136 if (guestdbg_enabled(vcpu)) {
137 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
138 LCTL_CR10 | LCTL_CR11);
139 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
140 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100141}
142
143static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
144{
145 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
146}
147
148static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200149 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100150{
151 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200152 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100153 case KVM_S390_INT_EMERGENCY:
154 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200155 case KVM_S390_INT_PFAULT_INIT:
156 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100157 case KVM_S390_INT_VIRTIO:
158 if (psw_extint_disabled(vcpu))
159 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
160 else
161 vcpu->arch.sie_block->lctl |= LCTL_CR0;
162 break;
163 case KVM_S390_SIGP_STOP:
164 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
165 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100166 case KVM_S390_MCHK:
167 if (psw_mchk_disabled(vcpu))
168 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
169 else
170 vcpu->arch.sie_block->lctl |= LCTL_CR14;
171 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100172 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
173 if (psw_ioint_disabled(vcpu))
174 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
175 else
176 vcpu->arch.sie_block->lctl |= LCTL_CR6;
177 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100178 default:
179 BUG();
180 }
181}
182
David Hildenbrand87128362014-03-03 10:55:13 +0100183static int __deliver_prog_irq(struct kvm_vcpu *vcpu,
184 struct kvm_s390_pgm_info *pgm_info)
185{
186 const unsigned short table[] = { 2, 4, 4, 6 };
187 int rc = 0;
188
189 switch (pgm_info->code & ~PGM_PER) {
190 case PGM_AFX_TRANSLATION:
191 case PGM_ASX_TRANSLATION:
192 case PGM_EX_TRANSLATION:
193 case PGM_LFX_TRANSLATION:
194 case PGM_LSTE_SEQUENCE:
195 case PGM_LSX_TRANSLATION:
196 case PGM_LX_TRANSLATION:
197 case PGM_PRIMARY_AUTHORITY:
198 case PGM_SECONDARY_AUTHORITY:
199 case PGM_SPACE_SWITCH:
200 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
201 (u64 *)__LC_TRANS_EXC_CODE);
202 break;
203 case PGM_ALEN_TRANSLATION:
204 case PGM_ALE_SEQUENCE:
205 case PGM_ASTE_INSTANCE:
206 case PGM_ASTE_SEQUENCE:
207 case PGM_ASTE_VALIDITY:
208 case PGM_EXTENDED_AUTHORITY:
209 rc = put_guest_lc(vcpu, pgm_info->exc_access_id,
210 (u8 *)__LC_EXC_ACCESS_ID);
211 break;
212 case PGM_ASCE_TYPE:
213 case PGM_PAGE_TRANSLATION:
214 case PGM_REGION_FIRST_TRANS:
215 case PGM_REGION_SECOND_TRANS:
216 case PGM_REGION_THIRD_TRANS:
217 case PGM_SEGMENT_TRANSLATION:
218 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
219 (u64 *)__LC_TRANS_EXC_CODE);
220 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
221 (u8 *)__LC_EXC_ACCESS_ID);
222 rc |= put_guest_lc(vcpu, pgm_info->op_access_id,
223 (u8 *)__LC_OP_ACCESS_ID);
224 break;
225 case PGM_MONITOR:
226 rc = put_guest_lc(vcpu, pgm_info->mon_class_nr,
227 (u64 *)__LC_MON_CLASS_NR);
228 rc |= put_guest_lc(vcpu, pgm_info->mon_code,
229 (u64 *)__LC_MON_CODE);
230 break;
231 case PGM_DATA:
232 rc = put_guest_lc(vcpu, pgm_info->data_exc_code,
233 (u32 *)__LC_DATA_EXC_CODE);
234 break;
235 case PGM_PROTECTION:
236 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
237 (u64 *)__LC_TRANS_EXC_CODE);
238 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
239 (u8 *)__LC_EXC_ACCESS_ID);
240 break;
241 }
242
243 if (pgm_info->code & PGM_PER) {
244 rc |= put_guest_lc(vcpu, pgm_info->per_code,
245 (u8 *) __LC_PER_CODE);
246 rc |= put_guest_lc(vcpu, pgm_info->per_atmid,
247 (u8 *)__LC_PER_ATMID);
248 rc |= put_guest_lc(vcpu, pgm_info->per_address,
249 (u64 *) __LC_PER_ADDRESS);
250 rc |= put_guest_lc(vcpu, pgm_info->per_access_id,
251 (u8 *) __LC_PER_ACCESS_ID);
252 }
253
254 switch (vcpu->arch.sie_block->icptcode) {
255 case ICPT_INST:
256 case ICPT_INSTPROGI:
257 case ICPT_OPEREXC:
258 case ICPT_PARTEXEC:
259 case ICPT_IOINST:
260 /* last instruction only stored for these icptcodes */
261 rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
262 (u16 *) __LC_PGM_ILC);
263 break;
264 case ICPT_PROGI:
265 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->pgmilc,
266 (u16 *) __LC_PGM_ILC);
267 break;
268 default:
269 rc |= put_guest_lc(vcpu, 0,
270 (u16 *) __LC_PGM_ILC);
271 }
272
273 rc |= put_guest_lc(vcpu, pgm_info->code,
274 (u16 *)__LC_PGM_INT_CODE);
275 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
276 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
277 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
278 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
279
280 return rc;
281}
282
Carsten Otteba5c1e92008-03-25 18:47:26 +0100283static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200284 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100285{
286 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100287 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100288
289 switch (inti->type) {
290 case KVM_S390_INT_EMERGENCY:
291 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
292 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200293 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
294 inti->emerg.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100295 rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE);
296 rc |= put_guest_lc(vcpu, inti->emerg.code,
297 (u16 *)__LC_EXT_CPU_ADDR);
298 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
299 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
300 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100301 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100302 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200303 case KVM_S390_INT_EXTERNAL_CALL:
304 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
305 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200306 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
307 inti->extcall.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100308 rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE);
309 rc |= put_guest_lc(vcpu, inti->extcall.code,
310 (u16 *)__LC_EXT_CPU_ADDR);
311 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
312 &vcpu->arch.sie_block->gpsw,
313 sizeof(psw_t));
314 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
315 &vcpu->arch.sie_block->gpsw,
316 sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200317 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100318 case KVM_S390_INT_SERVICE:
319 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
320 inti->ext.ext_params);
321 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200322 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
323 inti->ext.ext_params, 0);
Heiko Carstens79882762014-01-02 10:59:41 +0100324 rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE);
325 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
326 &vcpu->arch.sie_block->gpsw,
327 sizeof(psw_t));
328 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100329 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100330 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
331 (u32 *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100332 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200333 case KVM_S390_INT_PFAULT_INIT:
334 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
335 inti->ext.ext_params2);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100336 rc = put_guest_lc(vcpu, 0x2603, (u16 *) __LC_EXT_INT_CODE);
337 rc |= put_guest_lc(vcpu, 0x0600, (u16 *) __LC_EXT_CPU_ADDR);
338 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
339 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
340 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200341 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann1a03b7642014-02-12 14:05:38 +0100342 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
343 (u64 *) __LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200344 break;
345 case KVM_S390_INT_PFAULT_DONE:
346 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
347 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100348 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
349 rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR);
350 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
351 &vcpu->arch.sie_block->gpsw,
352 sizeof(psw_t));
353 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200354 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100355 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
356 (u64 *)__LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200357 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100358 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100359 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100360 inti->ext.ext_params, inti->ext.ext_params2);
361 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200362 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
363 inti->ext.ext_params,
364 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100365 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
366 rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR);
367 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
368 &vcpu->arch.sie_block->gpsw,
369 sizeof(psw_t));
370 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100371 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100372 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
373 (u32 *)__LC_EXT_PARAMS);
374 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
375 (u64 *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100376 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100377 case KVM_S390_SIGP_STOP:
378 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
379 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200380 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
381 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100382 __set_intercept_indicator(vcpu, inti);
383 break;
384
385 case KVM_S390_SIGP_SET_PREFIX:
386 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
387 inti->prefix.address);
388 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200389 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
390 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100391 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100392 break;
393
394 case KVM_S390_RESTART:
395 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
396 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200397 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
398 0, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100399 rc = write_guest_lc(vcpu,
400 offsetof(struct _lowcore, restart_old_psw),
401 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
402 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
403 &vcpu->arch.sie_block->gpsw,
404 sizeof(psw_t));
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100405 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100406 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100407 case KVM_S390_PROGRAM_INT:
408 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
409 inti->pgm.code,
410 table[vcpu->arch.sie_block->ipa >> 14]);
411 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200412 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
413 inti->pgm.code, 0);
David Hildenbrand87128362014-03-03 10:55:13 +0100414 rc = __deliver_prog_irq(vcpu, &inti->pgm);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100415 break;
416
Cornelia Huck48a3e952012-12-20 15:32:09 +0100417 case KVM_S390_MCHK:
418 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
419 inti->mchk.mcic);
420 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
421 inti->mchk.cr14,
422 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100423 rc = kvm_s390_vcpu_store_status(vcpu,
424 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens79882762014-01-02 10:59:41 +0100425 rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE);
426 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
427 &vcpu->arch.sie_block->gpsw,
428 sizeof(psw_t));
429 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100430 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100431 break;
432
Cornelia Huckd8346b72012-12-20 15:32:08 +0100433 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
434 {
435 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
436 inti->io.subchannel_nr;
437 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
438 inti->io.io_int_word;
439 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
440 vcpu->stat.deliver_io_int++;
441 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
442 param0, param1);
Heiko Carstens79882762014-01-02 10:59:41 +0100443 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
444 (u16 *)__LC_SUBCHANNEL_ID);
445 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
446 (u16 *)__LC_SUBCHANNEL_NR);
447 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
448 (u32 *)__LC_IO_INT_PARM);
449 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
450 (u32 *)__LC_IO_INT_WORD);
451 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
452 &vcpu->arch.sie_block->gpsw,
453 sizeof(psw_t));
454 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
455 &vcpu->arch.sie_block->gpsw,
456 sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100457 break;
458 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100459 default:
460 BUG();
461 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100462 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200463 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100464 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200465 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100466 }
467}
468
469static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
470{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100471 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100472
473 if (psw_extint_disabled(vcpu))
474 return 0;
475 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
476 return 0;
Jens Freimann1a03b7642014-02-12 14:05:38 +0100477 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
478 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
479 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
480 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
481 &vcpu->arch.sie_block->gpsw,
482 sizeof(psw_t));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100483 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200484 printk("kvm: The guest lowcore is not mapped during interrupt "
485 "delivery, killing userspace\n");
486 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100487 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100488 return 1;
489}
490
Dominik Dingel3c038e62013-10-07 17:11:48 +0200491int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100492{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200493 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
494 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
495 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100496 int rc = 0;
497
498 if (atomic_read(&li->active)) {
499 spin_lock_bh(&li->lock);
500 list_for_each_entry(inti, &li->list, list)
501 if (__interrupt_is_deliverable(vcpu, inti)) {
502 rc = 1;
503 break;
504 }
505 spin_unlock_bh(&li->lock);
506 }
507
508 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200509 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100510 list_for_each_entry(inti, &fi->list, list)
511 if (__interrupt_is_deliverable(vcpu, inti)) {
512 rc = 1;
513 break;
514 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200515 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100516 }
517
518 if ((!rc) && (vcpu->arch.sie_block->ckc <
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200519 get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100520 if ((!psw_extint_disabled(vcpu)) &&
521 (vcpu->arch.sie_block->gcr[0] & 0x800ul))
522 rc = 1;
523 }
524
525 return rc;
526}
527
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300528int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
529{
530 return 0;
531}
532
Carsten Otteba5c1e92008-03-25 18:47:26 +0100533int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
534{
535 u64 now, sltime;
536 DECLARE_WAITQUEUE(wait, current);
537
538 vcpu->stat.exit_wait_state++;
539 if (kvm_cpu_has_interrupt(vcpu))
540 return 0;
541
Carsten Ottee52b2af2008-05-21 13:37:44 +0200542 __set_cpu_idle(vcpu);
543 spin_lock_bh(&vcpu->arch.local_int.lock);
544 vcpu->arch.local_int.timer_due = 0;
545 spin_unlock_bh(&vcpu->arch.local_int.lock);
546
Carsten Otteba5c1e92008-03-25 18:47:26 +0100547 if (psw_interrupts_disabled(vcpu)) {
548 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
549 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100550 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100551 }
552
553 if (psw_extint_disabled(vcpu) ||
554 (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
555 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
556 goto no_timer;
557 }
558
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200559 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100560 if (vcpu->arch.sie_block->ckc < now) {
561 __unset_cpu_idle(vcpu);
562 return 0;
563 }
564
Heiko Carstensed4f2092013-01-14 16:55:55 +0100565 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100566
Christian Borntraegerca872302009-05-12 17:21:49 +0200567 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
568 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100569no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200570 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200571 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100572 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200573 add_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100574 while (list_empty(&vcpu->arch.local_int.list) &&
575 list_empty(&vcpu->arch.local_int.float_int->list) &&
576 (!vcpu->arch.local_int.timer_due) &&
577 !signal_pending(current)) {
578 set_current_state(TASK_INTERRUPTIBLE);
579 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200580 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100581 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200582 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100583 spin_lock_bh(&vcpu->arch.local_int.lock);
584 }
585 __unset_cpu_idle(vcpu);
586 __set_current_state(TASK_RUNNING);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200587 remove_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100588 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200589 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Thomas Huth800c1062013-09-12 10:33:45 +0200590 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
591
Christian Borntraegerca872302009-05-12 17:21:49 +0200592 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100593 return 0;
594}
595
Christian Borntraegerca872302009-05-12 17:21:49 +0200596void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100597{
Christian Borntraegerca872302009-05-12 17:21:49 +0200598 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100599
Christian Borntraegerca872302009-05-12 17:21:49 +0200600 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100601 vcpu->arch.local_int.timer_due = 1;
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200602 if (waitqueue_active(&vcpu->wq))
603 wake_up_interruptible(&vcpu->wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200604 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100605}
606
Christian Borntraegerca872302009-05-12 17:21:49 +0200607/*
608 * low level hrtimer wake routine. Because this runs in hardirq context
609 * we schedule a tasklet to do the real work.
610 */
611enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
612{
613 struct kvm_vcpu *vcpu;
614
615 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100616 vcpu->preempted = true;
Christian Borntraegerca872302009-05-12 17:21:49 +0200617 tasklet_schedule(&vcpu->arch.tasklet);
618
619 return HRTIMER_NORESTART;
620}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100621
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100622void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
623{
624 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
625 struct kvm_s390_interrupt_info *n, *inti = NULL;
626
627 spin_lock_bh(&li->lock);
628 list_for_each_entry_safe(inti, n, &li->list, list) {
629 list_del(&inti->list);
630 kfree(inti);
631 }
632 atomic_set(&li->active, 0);
633 spin_unlock_bh(&li->lock);
634}
635
Carsten Otteba5c1e92008-03-25 18:47:26 +0100636void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
637{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200638 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
639 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
640 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100641 int deliver;
642
643 __reset_intercept_indicators(vcpu);
644 if (atomic_read(&li->active)) {
645 do {
646 deliver = 0;
647 spin_lock_bh(&li->lock);
648 list_for_each_entry_safe(inti, n, &li->list, list) {
649 if (__interrupt_is_deliverable(vcpu, inti)) {
650 list_del(&inti->list);
651 deliver = 1;
652 break;
653 }
654 __set_intercept_indicator(vcpu, inti);
655 }
656 if (list_empty(&li->list))
657 atomic_set(&li->active, 0);
658 spin_unlock_bh(&li->lock);
659 if (deliver) {
660 __do_deliver_interrupt(vcpu, inti);
661 kfree(inti);
662 }
663 } while (deliver);
664 }
665
666 if ((vcpu->arch.sie_block->ckc <
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200667 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
Carsten Otteba5c1e92008-03-25 18:47:26 +0100668 __try_deliver_ckc_interrupt(vcpu);
669
670 if (atomic_read(&fi->active)) {
671 do {
672 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200673 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100674 list_for_each_entry_safe(inti, n, &fi->list, list) {
675 if (__interrupt_is_deliverable(vcpu, inti)) {
676 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100677 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100678 deliver = 1;
679 break;
680 }
681 __set_intercept_indicator(vcpu, inti);
682 }
683 if (list_empty(&fi->list))
684 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200685 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100686 if (deliver) {
687 __do_deliver_interrupt(vcpu, inti);
688 kfree(inti);
689 }
690 } while (deliver);
691 }
692}
693
Cornelia Huck48a3e952012-12-20 15:32:09 +0100694void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
695{
696 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
697 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
698 struct kvm_s390_interrupt_info *n, *inti = NULL;
699 int deliver;
700
701 __reset_intercept_indicators(vcpu);
702 if (atomic_read(&li->active)) {
703 do {
704 deliver = 0;
705 spin_lock_bh(&li->lock);
706 list_for_each_entry_safe(inti, n, &li->list, list) {
707 if ((inti->type == KVM_S390_MCHK) &&
708 __interrupt_is_deliverable(vcpu, inti)) {
709 list_del(&inti->list);
710 deliver = 1;
711 break;
712 }
713 __set_intercept_indicator(vcpu, inti);
714 }
715 if (list_empty(&li->list))
716 atomic_set(&li->active, 0);
717 spin_unlock_bh(&li->lock);
718 if (deliver) {
719 __do_deliver_interrupt(vcpu, inti);
720 kfree(inti);
721 }
722 } while (deliver);
723 }
724
725 if (atomic_read(&fi->active)) {
726 do {
727 deliver = 0;
728 spin_lock(&fi->lock);
729 list_for_each_entry_safe(inti, n, &fi->list, list) {
730 if ((inti->type == KVM_S390_MCHK) &&
731 __interrupt_is_deliverable(vcpu, inti)) {
732 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100733 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100734 deliver = 1;
735 break;
736 }
737 __set_intercept_indicator(vcpu, inti);
738 }
739 if (list_empty(&fi->list))
740 atomic_set(&fi->active, 0);
741 spin_unlock(&fi->lock);
742 if (deliver) {
743 __do_deliver_interrupt(vcpu, inti);
744 kfree(inti);
745 }
746 } while (deliver);
747 }
748}
749
Carsten Otteba5c1e92008-03-25 18:47:26 +0100750int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
751{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200752 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
753 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100754
755 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
756 if (!inti)
757 return -ENOMEM;
758
Joe Perchesa419aef2009-08-18 11:18:35 -0700759 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100760 inti->pgm.code = code;
761
762 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200763 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100764 spin_lock_bh(&li->lock);
765 list_add(&inti->list, &li->list);
766 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200767 BUG_ON(waitqueue_active(li->wq));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100768 spin_unlock_bh(&li->lock);
769 return 0;
770}
771
Jens Freimannbcd84682014-02-11 11:07:05 +0100772int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
773 struct kvm_s390_pgm_info *pgm_info)
774{
775 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
776 struct kvm_s390_interrupt_info *inti;
777
778 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
779 if (!inti)
780 return -ENOMEM;
781
782 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
783 pgm_info->code);
784 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
785 pgm_info->code, 0, 1);
786
787 inti->type = KVM_S390_PROGRAM_INT;
788 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
789 spin_lock_bh(&li->lock);
790 list_add(&inti->list, &li->list);
791 atomic_set(&li->active, 1);
792 BUG_ON(waitqueue_active(li->wq));
793 spin_unlock_bh(&li->lock);
794 return 0;
795}
796
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100797struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
798 u64 cr6, u64 schid)
799{
800 struct kvm_s390_float_interrupt *fi;
801 struct kvm_s390_interrupt_info *inti, *iter;
802
803 if ((!schid && !cr6) || (schid && cr6))
804 return NULL;
805 mutex_lock(&kvm->lock);
806 fi = &kvm->arch.float_int;
807 spin_lock(&fi->lock);
808 inti = NULL;
809 list_for_each_entry(iter, &fi->list, list) {
810 if (!is_ioint(iter->type))
811 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100812 if (cr6 &&
813 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100814 continue;
815 if (schid) {
816 if (((schid & 0x00000000ffff0000) >> 16) !=
817 iter->io.subchannel_id)
818 continue;
819 if ((schid & 0x000000000000ffff) !=
820 iter->io.subchannel_nr)
821 continue;
822 }
823 inti = iter;
824 break;
825 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100826 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100827 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100828 fi->irq_count--;
829 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100830 if (list_empty(&fi->list))
831 atomic_set(&fi->active, 0);
832 spin_unlock(&fi->lock);
833 mutex_unlock(&kvm->lock);
834 return inti;
835}
836
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100837static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100838{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200839 struct kvm_s390_local_interrupt *li;
840 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200841 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100842 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100843 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100844 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100845
Carsten Otteba5c1e92008-03-25 18:47:26 +0100846 mutex_lock(&kvm->lock);
847 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200848 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100849 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
850 rc = -EINVAL;
851 goto unlock_fi;
852 }
853 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200854 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100855 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200856 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100857 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
858
Cornelia Huckd8346b72012-12-20 15:32:08 +0100859 /* Keep I/O interrupts sorted in isc order. */
860 list_for_each_entry(iter, &fi->list, list) {
861 if (!is_ioint(iter->type))
862 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100863 if (int_word_to_isc_bits(iter->io.io_int_word)
864 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100865 continue;
866 break;
867 }
868 list_add_tail(&inti->list, &iter->list);
869 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100870 atomic_set(&fi->active, 1);
871 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
872 if (sigcpu == KVM_MAX_VCPUS) {
873 do {
874 sigcpu = fi->next_rr_cpu++;
875 if (sigcpu == KVM_MAX_VCPUS)
876 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100877 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100878 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100879 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
880 li = &dst_vcpu->arch.local_int;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100881 spin_lock_bh(&li->lock);
882 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200883 if (waitqueue_active(li->wq))
884 wake_up_interruptible(li->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100885 kvm_get_vcpu(kvm, sigcpu)->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100886 spin_unlock_bh(&li->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100887unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200888 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100889 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100890 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200891}
892
893int kvm_s390_inject_vm(struct kvm *kvm,
894 struct kvm_s390_interrupt *s390int)
895{
896 struct kvm_s390_interrupt_info *inti;
897
898 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
899 if (!inti)
900 return -ENOMEM;
901
902 inti->type = s390int->type;
903 switch (inti->type) {
904 case KVM_S390_INT_VIRTIO:
905 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
906 s390int->parm, s390int->parm64);
907 inti->ext.ext_params = s390int->parm;
908 inti->ext.ext_params2 = s390int->parm64;
909 break;
910 case KVM_S390_INT_SERVICE:
911 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
912 inti->ext.ext_params = s390int->parm;
913 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200914 case KVM_S390_INT_PFAULT_DONE:
915 inti->type = s390int->type;
916 inti->ext.ext_params2 = s390int->parm64;
917 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200918 case KVM_S390_MCHK:
919 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
920 s390int->parm64);
921 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
922 inti->mchk.mcic = s390int->parm64;
923 break;
924 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
925 if (inti->type & IOINT_AI_MASK)
926 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
927 else
928 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
929 s390int->type & IOINT_CSSID_MASK,
930 s390int->type & IOINT_SSID_MASK,
931 s390int->type & IOINT_SCHID_MASK);
932 inti->io.subchannel_id = s390int->parm >> 16;
933 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
934 inti->io.io_int_parm = s390int->parm64 >> 32;
935 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
936 break;
937 default:
938 kfree(inti);
939 return -EINVAL;
940 }
941 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
942 2);
943
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100944 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100945}
946
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100947void kvm_s390_reinject_io_int(struct kvm *kvm,
948 struct kvm_s390_interrupt_info *inti)
949{
950 __inject_vm(kvm, inti);
951}
952
Carsten Otteba5c1e92008-03-25 18:47:26 +0100953int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
954 struct kvm_s390_interrupt *s390int)
955{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200956 struct kvm_s390_local_interrupt *li;
957 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100958
959 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
960 if (!inti)
961 return -ENOMEM;
962
963 switch (s390int->type) {
964 case KVM_S390_PROGRAM_INT:
965 if (s390int->parm & 0xffff0000) {
966 kfree(inti);
967 return -EINVAL;
968 }
969 inti->type = s390int->type;
970 inti->pgm.code = s390int->parm;
971 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
972 s390int->parm);
973 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100974 case KVM_S390_SIGP_SET_PREFIX:
975 inti->prefix.address = s390int->parm;
976 inti->type = s390int->type;
977 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
978 s390int->parm);
979 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100980 case KVM_S390_SIGP_STOP:
981 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100982 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
983 inti->type = s390int->type;
984 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200985 case KVM_S390_INT_EXTERNAL_CALL:
986 if (s390int->parm & 0xffff0000) {
987 kfree(inti);
988 return -EINVAL;
989 }
990 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
991 s390int->parm);
992 inti->type = s390int->type;
993 inti->extcall.code = s390int->parm;
994 break;
995 case KVM_S390_INT_EMERGENCY:
996 if (s390int->parm & 0xffff0000) {
997 kfree(inti);
998 return -EINVAL;
999 }
1000 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
1001 inti->type = s390int->type;
1002 inti->emerg.code = s390int->parm;
1003 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001004 case KVM_S390_MCHK:
1005 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1006 s390int->parm64);
1007 inti->type = s390int->type;
1008 inti->mchk.mcic = s390int->parm64;
1009 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001010 case KVM_S390_INT_PFAULT_INIT:
1011 inti->type = s390int->type;
1012 inti->ext.ext_params2 = s390int->parm64;
1013 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001014 case KVM_S390_INT_VIRTIO:
1015 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001016 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001017 default:
1018 kfree(inti);
1019 return -EINVAL;
1020 }
Cornelia Huckade38c32012-07-23 17:20:30 +02001021 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
1022 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001023
1024 mutex_lock(&vcpu->kvm->lock);
1025 li = &vcpu->arch.local_int;
1026 spin_lock_bh(&li->lock);
1027 if (inti->type == KVM_S390_PROGRAM_INT)
1028 list_add(&inti->list, &li->list);
1029 else
1030 list_add_tail(&inti->list, &li->list);
1031 atomic_set(&li->active, 1);
1032 if (inti->type == KVM_S390_SIGP_STOP)
1033 li->action_bits |= ACTION_STOP_ON_STOP;
1034 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001035 if (waitqueue_active(&vcpu->wq))
1036 wake_up_interruptible(&vcpu->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +01001037 vcpu->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001038 spin_unlock_bh(&li->lock);
1039 mutex_unlock(&vcpu->kvm->lock);
1040 return 0;
1041}
Jens Freimannc05c4182013-10-07 16:13:45 +02001042
1043static void clear_floating_interrupts(struct kvm *kvm)
1044{
1045 struct kvm_s390_float_interrupt *fi;
1046 struct kvm_s390_interrupt_info *n, *inti = NULL;
1047
1048 mutex_lock(&kvm->lock);
1049 fi = &kvm->arch.float_int;
1050 spin_lock(&fi->lock);
1051 list_for_each_entry_safe(inti, n, &fi->list, list) {
1052 list_del(&inti->list);
1053 kfree(inti);
1054 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001055 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001056 atomic_set(&fi->active, 0);
1057 spin_unlock(&fi->lock);
1058 mutex_unlock(&kvm->lock);
1059}
1060
1061static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1062 u8 *addr)
1063{
1064 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1065 struct kvm_s390_irq irq = {0};
1066
1067 irq.type = inti->type;
1068 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001069 case KVM_S390_INT_PFAULT_INIT:
1070 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001071 case KVM_S390_INT_VIRTIO:
1072 case KVM_S390_INT_SERVICE:
1073 irq.u.ext = inti->ext;
1074 break;
1075 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1076 irq.u.io = inti->io;
1077 break;
1078 case KVM_S390_MCHK:
1079 irq.u.mchk = inti->mchk;
1080 break;
1081 default:
1082 return -EINVAL;
1083 }
1084
1085 if (copy_to_user(uptr, &irq, sizeof(irq)))
1086 return -EFAULT;
1087
1088 return 0;
1089}
1090
1091static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1092{
1093 struct kvm_s390_interrupt_info *inti;
1094 struct kvm_s390_float_interrupt *fi;
1095 int ret = 0;
1096 int n = 0;
1097
1098 mutex_lock(&kvm->lock);
1099 fi = &kvm->arch.float_int;
1100 spin_lock(&fi->lock);
1101
1102 list_for_each_entry(inti, &fi->list, list) {
1103 if (len < sizeof(struct kvm_s390_irq)) {
1104 /* signal userspace to try again */
1105 ret = -ENOMEM;
1106 break;
1107 }
1108 ret = copy_irq_to_user(inti, buf);
1109 if (ret)
1110 break;
1111 buf += sizeof(struct kvm_s390_irq);
1112 len -= sizeof(struct kvm_s390_irq);
1113 n++;
1114 }
1115
1116 spin_unlock(&fi->lock);
1117 mutex_unlock(&kvm->lock);
1118
1119 return ret < 0 ? ret : n;
1120}
1121
1122static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1123{
1124 int r;
1125
1126 switch (attr->group) {
1127 case KVM_DEV_FLIC_GET_ALL_IRQS:
1128 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1129 attr->attr);
1130 break;
1131 default:
1132 r = -EINVAL;
1133 }
1134
1135 return r;
1136}
1137
1138static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1139 u64 addr)
1140{
1141 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1142 void *target = NULL;
1143 void __user *source;
1144 u64 size;
1145
1146 if (get_user(inti->type, (u64 __user *)addr))
1147 return -EFAULT;
1148
1149 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001150 case KVM_S390_INT_PFAULT_INIT:
1151 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001152 case KVM_S390_INT_VIRTIO:
1153 case KVM_S390_INT_SERVICE:
1154 target = (void *) &inti->ext;
1155 source = &uptr->u.ext;
1156 size = sizeof(inti->ext);
1157 break;
1158 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1159 target = (void *) &inti->io;
1160 source = &uptr->u.io;
1161 size = sizeof(inti->io);
1162 break;
1163 case KVM_S390_MCHK:
1164 target = (void *) &inti->mchk;
1165 source = &uptr->u.mchk;
1166 size = sizeof(inti->mchk);
1167 break;
1168 default:
1169 return -EINVAL;
1170 }
1171
1172 if (copy_from_user(target, source, size))
1173 return -EFAULT;
1174
1175 return 0;
1176}
1177
1178static int enqueue_floating_irq(struct kvm_device *dev,
1179 struct kvm_device_attr *attr)
1180{
1181 struct kvm_s390_interrupt_info *inti = NULL;
1182 int r = 0;
1183 int len = attr->attr;
1184
1185 if (len % sizeof(struct kvm_s390_irq) != 0)
1186 return -EINVAL;
1187 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1188 return -EINVAL;
1189
1190 while (len >= sizeof(struct kvm_s390_irq)) {
1191 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1192 if (!inti)
1193 return -ENOMEM;
1194
1195 r = copy_irq_from_user(inti, attr->addr);
1196 if (r) {
1197 kfree(inti);
1198 return r;
1199 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001200 r = __inject_vm(dev->kvm, inti);
1201 if (r) {
1202 kfree(inti);
1203 return r;
1204 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001205 len -= sizeof(struct kvm_s390_irq);
1206 attr->addr += sizeof(struct kvm_s390_irq);
1207 }
1208
1209 return r;
1210}
1211
Cornelia Huck841b91c2013-07-15 13:36:01 +02001212static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1213{
1214 if (id >= MAX_S390_IO_ADAPTERS)
1215 return NULL;
1216 return kvm->arch.adapters[id];
1217}
1218
1219static int register_io_adapter(struct kvm_device *dev,
1220 struct kvm_device_attr *attr)
1221{
1222 struct s390_io_adapter *adapter;
1223 struct kvm_s390_io_adapter adapter_info;
1224
1225 if (copy_from_user(&adapter_info,
1226 (void __user *)attr->addr, sizeof(adapter_info)))
1227 return -EFAULT;
1228
1229 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1230 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1231 return -EINVAL;
1232
1233 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1234 if (!adapter)
1235 return -ENOMEM;
1236
1237 INIT_LIST_HEAD(&adapter->maps);
1238 init_rwsem(&adapter->maps_lock);
1239 atomic_set(&adapter->nr_maps, 0);
1240 adapter->id = adapter_info.id;
1241 adapter->isc = adapter_info.isc;
1242 adapter->maskable = adapter_info.maskable;
1243 adapter->masked = false;
1244 adapter->swap = adapter_info.swap;
1245 dev->kvm->arch.adapters[adapter->id] = adapter;
1246
1247 return 0;
1248}
1249
1250int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1251{
1252 int ret;
1253 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1254
1255 if (!adapter || !adapter->maskable)
1256 return -EINVAL;
1257 ret = adapter->masked;
1258 adapter->masked = masked;
1259 return ret;
1260}
1261
1262static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1263{
1264 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1265 struct s390_map_info *map;
1266 int ret;
1267
1268 if (!adapter || !addr)
1269 return -EINVAL;
1270
1271 map = kzalloc(sizeof(*map), GFP_KERNEL);
1272 if (!map) {
1273 ret = -ENOMEM;
1274 goto out;
1275 }
1276 INIT_LIST_HEAD(&map->list);
1277 map->guest_addr = addr;
1278 map->addr = gmap_translate(addr, kvm->arch.gmap);
1279 if (map->addr == -EFAULT) {
1280 ret = -EFAULT;
1281 goto out;
1282 }
1283 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1284 if (ret < 0)
1285 goto out;
1286 BUG_ON(ret != 1);
1287 down_write(&adapter->maps_lock);
1288 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1289 list_add_tail(&map->list, &adapter->maps);
1290 ret = 0;
1291 } else {
1292 put_page(map->page);
1293 ret = -EINVAL;
1294 }
1295 up_write(&adapter->maps_lock);
1296out:
1297 if (ret)
1298 kfree(map);
1299 return ret;
1300}
1301
1302static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1303{
1304 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1305 struct s390_map_info *map, *tmp;
1306 int found = 0;
1307
1308 if (!adapter || !addr)
1309 return -EINVAL;
1310
1311 down_write(&adapter->maps_lock);
1312 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1313 if (map->guest_addr == addr) {
1314 found = 1;
1315 atomic_dec(&adapter->nr_maps);
1316 list_del(&map->list);
1317 put_page(map->page);
1318 kfree(map);
1319 break;
1320 }
1321 }
1322 up_write(&adapter->maps_lock);
1323
1324 return found ? 0 : -EINVAL;
1325}
1326
1327void kvm_s390_destroy_adapters(struct kvm *kvm)
1328{
1329 int i;
1330 struct s390_map_info *map, *tmp;
1331
1332 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1333 if (!kvm->arch.adapters[i])
1334 continue;
1335 list_for_each_entry_safe(map, tmp,
1336 &kvm->arch.adapters[i]->maps, list) {
1337 list_del(&map->list);
1338 put_page(map->page);
1339 kfree(map);
1340 }
1341 kfree(kvm->arch.adapters[i]);
1342 }
1343}
1344
1345static int modify_io_adapter(struct kvm_device *dev,
1346 struct kvm_device_attr *attr)
1347{
1348 struct kvm_s390_io_adapter_req req;
1349 struct s390_io_adapter *adapter;
1350 int ret;
1351
1352 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1353 return -EFAULT;
1354
1355 adapter = get_io_adapter(dev->kvm, req.id);
1356 if (!adapter)
1357 return -EINVAL;
1358 switch (req.type) {
1359 case KVM_S390_IO_ADAPTER_MASK:
1360 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1361 if (ret > 0)
1362 ret = 0;
1363 break;
1364 case KVM_S390_IO_ADAPTER_MAP:
1365 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1366 break;
1367 case KVM_S390_IO_ADAPTER_UNMAP:
1368 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1369 break;
1370 default:
1371 ret = -EINVAL;
1372 }
1373
1374 return ret;
1375}
1376
Jens Freimannc05c4182013-10-07 16:13:45 +02001377static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1378{
1379 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001380 unsigned int i;
1381 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001382
1383 switch (attr->group) {
1384 case KVM_DEV_FLIC_ENQUEUE:
1385 r = enqueue_floating_irq(dev, attr);
1386 break;
1387 case KVM_DEV_FLIC_CLEAR_IRQS:
1388 r = 0;
1389 clear_floating_interrupts(dev->kvm);
1390 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001391 case KVM_DEV_FLIC_APF_ENABLE:
1392 dev->kvm->arch.gmap->pfault_enabled = 1;
1393 break;
1394 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1395 dev->kvm->arch.gmap->pfault_enabled = 0;
1396 /*
1397 * Make sure no async faults are in transition when
1398 * clearing the queues. So we don't need to worry
1399 * about late coming workers.
1400 */
1401 synchronize_srcu(&dev->kvm->srcu);
1402 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1403 kvm_clear_async_pf_completion_queue(vcpu);
1404 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001405 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1406 r = register_io_adapter(dev, attr);
1407 break;
1408 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1409 r = modify_io_adapter(dev, attr);
1410 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001411 default:
1412 r = -EINVAL;
1413 }
1414
1415 return r;
1416}
1417
1418static int flic_create(struct kvm_device *dev, u32 type)
1419{
1420 if (!dev)
1421 return -EINVAL;
1422 if (dev->kvm->arch.flic)
1423 return -EINVAL;
1424 dev->kvm->arch.flic = dev;
1425 return 0;
1426}
1427
1428static void flic_destroy(struct kvm_device *dev)
1429{
1430 dev->kvm->arch.flic = NULL;
1431 kfree(dev);
1432}
1433
1434/* s390 floating irq controller (flic) */
1435struct kvm_device_ops kvm_flic_ops = {
1436 .name = "kvm-flic",
1437 .get_attr = flic_get_attr,
1438 .set_attr = flic_set_attr,
1439 .create = flic_create,
1440 .destroy = flic_destroy,
1441};
Cornelia Huck84223592013-07-15 13:36:01 +02001442
1443static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1444{
1445 unsigned long bit;
1446
1447 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1448
1449 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1450}
1451
1452static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1453 u64 addr)
1454{
1455 struct s390_map_info *map;
1456
1457 if (!adapter)
1458 return NULL;
1459
1460 list_for_each_entry(map, &adapter->maps, list) {
1461 if (map->guest_addr == addr)
1462 return map;
1463 }
1464 return NULL;
1465}
1466
1467static int adapter_indicators_set(struct kvm *kvm,
1468 struct s390_io_adapter *adapter,
1469 struct kvm_s390_adapter_int *adapter_int)
1470{
1471 unsigned long bit;
1472 int summary_set, idx;
1473 struct s390_map_info *info;
1474 void *map;
1475
1476 info = get_map_info(adapter, adapter_int->ind_addr);
1477 if (!info)
1478 return -1;
1479 map = page_address(info->page);
1480 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1481 set_bit(bit, map);
1482 idx = srcu_read_lock(&kvm->srcu);
1483 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1484 set_page_dirty_lock(info->page);
1485 info = get_map_info(adapter, adapter_int->summary_addr);
1486 if (!info) {
1487 srcu_read_unlock(&kvm->srcu, idx);
1488 return -1;
1489 }
1490 map = page_address(info->page);
1491 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1492 adapter->swap);
1493 summary_set = test_and_set_bit(bit, map);
1494 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1495 set_page_dirty_lock(info->page);
1496 srcu_read_unlock(&kvm->srcu, idx);
1497 return summary_set ? 0 : 1;
1498}
1499
1500/*
1501 * < 0 - not injected due to error
1502 * = 0 - coalesced, summary indicator already active
1503 * > 0 - injected interrupt
1504 */
1505static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1506 struct kvm *kvm, int irq_source_id, int level,
1507 bool line_status)
1508{
1509 int ret;
1510 struct s390_io_adapter *adapter;
1511
1512 /* We're only interested in the 0->1 transition. */
1513 if (!level)
1514 return 0;
1515 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1516 if (!adapter)
1517 return -1;
1518 down_read(&adapter->maps_lock);
1519 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1520 up_read(&adapter->maps_lock);
1521 if ((ret > 0) && !adapter->masked) {
1522 struct kvm_s390_interrupt s390int = {
1523 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1524 .parm = 0,
1525 .parm64 = (adapter->isc << 27) | 0x80000000,
1526 };
1527 ret = kvm_s390_inject_vm(kvm, &s390int);
1528 if (ret == 0)
1529 ret = 1;
1530 }
1531 return ret;
1532}
1533
1534int kvm_set_routing_entry(struct kvm_irq_routing_table *rt,
1535 struct kvm_kernel_irq_routing_entry *e,
1536 const struct kvm_irq_routing_entry *ue)
1537{
1538 int ret;
1539
1540 switch (ue->type) {
1541 case KVM_IRQ_ROUTING_S390_ADAPTER:
1542 e->set = set_adapter_int;
1543 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1544 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1545 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1546 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1547 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1548 ret = 0;
1549 break;
1550 default:
1551 ret = -EINVAL;
1552 }
1553
1554 return ret;
1555}
1556
1557int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1558 int irq_source_id, int level, bool line_status)
1559{
1560 return -EINVAL;
1561}