blob: d9526bb291945c8c1275faa198eff1144d1b50b0 [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
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010059static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
60{
61 if (psw_extint_disabled(vcpu) ||
62 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
63 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +010064 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
65 /* No timer interrupts when single stepping */
66 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010067 return 1;
68}
69
Cornelia Huck79fd50c2013-02-07 13:20:52 +010070static u64 int_word_to_isc_bits(u32 int_word)
71{
72 u8 isc = (int_word & 0x38000000) >> 27;
73
74 return (0x80 >> isc) << 24;
75}
76
Carsten Otteba5c1e92008-03-25 18:47:26 +010077static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020078 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010079{
80 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020081 case KVM_S390_INT_EXTERNAL_CALL:
82 if (psw_extint_disabled(vcpu))
83 return 0;
84 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
85 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +010086 case KVM_S390_INT_EMERGENCY:
87 if (psw_extint_disabled(vcpu))
88 return 0;
89 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
90 return 1;
91 return 0;
92 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +020093 case KVM_S390_INT_PFAULT_INIT:
94 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +010095 case KVM_S390_INT_VIRTIO:
96 if (psw_extint_disabled(vcpu))
97 return 0;
98 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
99 return 1;
100 return 0;
101 case KVM_S390_PROGRAM_INT:
102 case KVM_S390_SIGP_STOP:
103 case KVM_S390_SIGP_SET_PREFIX:
104 case KVM_S390_RESTART:
105 return 1;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100106 case KVM_S390_MCHK:
107 if (psw_mchk_disabled(vcpu))
108 return 0;
109 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
110 return 1;
111 return 0;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100112 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
113 if (psw_ioint_disabled(vcpu))
114 return 0;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100115 if (vcpu->arch.sie_block->gcr[6] &
116 int_word_to_isc_bits(inti->io.io_int_word))
Cornelia Huckd8346b72012-12-20 15:32:08 +0100117 return 1;
118 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100119 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100120 printk(KERN_WARNING "illegal interrupt type %llx\n",
121 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100122 BUG();
123 }
124 return 0;
125}
126
127static void __set_cpu_idle(struct kvm_vcpu *vcpu)
128{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100129 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
130 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
131}
132
133static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
134{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100135 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
136 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
137}
138
139static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
140{
141 atomic_clear_mask(CPUSTAT_ECALL_PEND |
142 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
143 &vcpu->arch.sie_block->cpuflags);
144 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100145 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
146
147 if (guestdbg_enabled(vcpu)) {
148 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
149 LCTL_CR10 | LCTL_CR11);
150 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
151 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100152}
153
154static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
155{
156 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
157}
158
159static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200160 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100161{
162 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200163 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100164 case KVM_S390_INT_EMERGENCY:
165 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200166 case KVM_S390_INT_PFAULT_INIT:
167 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100168 case KVM_S390_INT_VIRTIO:
169 if (psw_extint_disabled(vcpu))
170 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
171 else
172 vcpu->arch.sie_block->lctl |= LCTL_CR0;
173 break;
174 case KVM_S390_SIGP_STOP:
175 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
176 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100177 case KVM_S390_MCHK:
178 if (psw_mchk_disabled(vcpu))
179 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
180 else
181 vcpu->arch.sie_block->lctl |= LCTL_CR14;
182 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100183 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
184 if (psw_ioint_disabled(vcpu))
185 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
186 else
187 vcpu->arch.sie_block->lctl |= LCTL_CR6;
188 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100189 default:
190 BUG();
191 }
192}
193
David Hildenbrand87128362014-03-03 10:55:13 +0100194static int __deliver_prog_irq(struct kvm_vcpu *vcpu,
195 struct kvm_s390_pgm_info *pgm_info)
196{
197 const unsigned short table[] = { 2, 4, 4, 6 };
198 int rc = 0;
199
200 switch (pgm_info->code & ~PGM_PER) {
201 case PGM_AFX_TRANSLATION:
202 case PGM_ASX_TRANSLATION:
203 case PGM_EX_TRANSLATION:
204 case PGM_LFX_TRANSLATION:
205 case PGM_LSTE_SEQUENCE:
206 case PGM_LSX_TRANSLATION:
207 case PGM_LX_TRANSLATION:
208 case PGM_PRIMARY_AUTHORITY:
209 case PGM_SECONDARY_AUTHORITY:
210 case PGM_SPACE_SWITCH:
211 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
212 (u64 *)__LC_TRANS_EXC_CODE);
213 break;
214 case PGM_ALEN_TRANSLATION:
215 case PGM_ALE_SEQUENCE:
216 case PGM_ASTE_INSTANCE:
217 case PGM_ASTE_SEQUENCE:
218 case PGM_ASTE_VALIDITY:
219 case PGM_EXTENDED_AUTHORITY:
220 rc = put_guest_lc(vcpu, pgm_info->exc_access_id,
221 (u8 *)__LC_EXC_ACCESS_ID);
222 break;
223 case PGM_ASCE_TYPE:
224 case PGM_PAGE_TRANSLATION:
225 case PGM_REGION_FIRST_TRANS:
226 case PGM_REGION_SECOND_TRANS:
227 case PGM_REGION_THIRD_TRANS:
228 case PGM_SEGMENT_TRANSLATION:
229 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
230 (u64 *)__LC_TRANS_EXC_CODE);
231 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
232 (u8 *)__LC_EXC_ACCESS_ID);
233 rc |= put_guest_lc(vcpu, pgm_info->op_access_id,
234 (u8 *)__LC_OP_ACCESS_ID);
235 break;
236 case PGM_MONITOR:
237 rc = put_guest_lc(vcpu, pgm_info->mon_class_nr,
238 (u64 *)__LC_MON_CLASS_NR);
239 rc |= put_guest_lc(vcpu, pgm_info->mon_code,
240 (u64 *)__LC_MON_CODE);
241 break;
242 case PGM_DATA:
243 rc = put_guest_lc(vcpu, pgm_info->data_exc_code,
244 (u32 *)__LC_DATA_EXC_CODE);
245 break;
246 case PGM_PROTECTION:
247 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
248 (u64 *)__LC_TRANS_EXC_CODE);
249 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
250 (u8 *)__LC_EXC_ACCESS_ID);
251 break;
252 }
253
254 if (pgm_info->code & PGM_PER) {
255 rc |= put_guest_lc(vcpu, pgm_info->per_code,
256 (u8 *) __LC_PER_CODE);
257 rc |= put_guest_lc(vcpu, pgm_info->per_atmid,
258 (u8 *)__LC_PER_ATMID);
259 rc |= put_guest_lc(vcpu, pgm_info->per_address,
260 (u64 *) __LC_PER_ADDRESS);
261 rc |= put_guest_lc(vcpu, pgm_info->per_access_id,
262 (u8 *) __LC_PER_ACCESS_ID);
263 }
264
265 switch (vcpu->arch.sie_block->icptcode) {
266 case ICPT_INST:
267 case ICPT_INSTPROGI:
268 case ICPT_OPEREXC:
269 case ICPT_PARTEXEC:
270 case ICPT_IOINST:
271 /* last instruction only stored for these icptcodes */
272 rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
273 (u16 *) __LC_PGM_ILC);
274 break;
275 case ICPT_PROGI:
276 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->pgmilc,
277 (u16 *) __LC_PGM_ILC);
278 break;
279 default:
280 rc |= put_guest_lc(vcpu, 0,
281 (u16 *) __LC_PGM_ILC);
282 }
283
284 rc |= put_guest_lc(vcpu, pgm_info->code,
285 (u16 *)__LC_PGM_INT_CODE);
286 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
287 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
288 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
289 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
290
291 return rc;
292}
293
Carsten Otteba5c1e92008-03-25 18:47:26 +0100294static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200295 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100296{
297 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100298 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100299
300 switch (inti->type) {
301 case KVM_S390_INT_EMERGENCY:
302 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
303 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200304 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
305 inti->emerg.code, 0);
Jens Freimann1a03b762014-02-12 14:05:38 +0100306 rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE);
307 rc |= put_guest_lc(vcpu, inti->emerg.code,
308 (u16 *)__LC_EXT_CPU_ADDR);
309 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
310 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
311 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100312 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100313 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200314 case KVM_S390_INT_EXTERNAL_CALL:
315 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
316 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200317 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
318 inti->extcall.code, 0);
Jens Freimann1a03b762014-02-12 14:05:38 +0100319 rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE);
320 rc |= put_guest_lc(vcpu, inti->extcall.code,
321 (u16 *)__LC_EXT_CPU_ADDR);
322 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
323 &vcpu->arch.sie_block->gpsw,
324 sizeof(psw_t));
325 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
326 &vcpu->arch.sie_block->gpsw,
327 sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200328 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100329 case KVM_S390_INT_SERVICE:
330 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
331 inti->ext.ext_params);
332 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200333 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
334 inti->ext.ext_params, 0);
Heiko Carstens79882762014-01-02 10:59:41 +0100335 rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE);
336 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
337 &vcpu->arch.sie_block->gpsw,
338 sizeof(psw_t));
339 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100340 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100341 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
342 (u32 *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100343 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200344 case KVM_S390_INT_PFAULT_INIT:
345 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
346 inti->ext.ext_params2);
Jens Freimann1a03b762014-02-12 14:05:38 +0100347 rc = put_guest_lc(vcpu, 0x2603, (u16 *) __LC_EXT_INT_CODE);
348 rc |= put_guest_lc(vcpu, 0x0600, (u16 *) __LC_EXT_CPU_ADDR);
349 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
350 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
351 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200352 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann1a03b762014-02-12 14:05:38 +0100353 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
354 (u64 *) __LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200355 break;
356 case KVM_S390_INT_PFAULT_DONE:
357 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
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, 0x0680, (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,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200365 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100366 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
367 (u64 *)__LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200368 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100369 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100370 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100371 inti->ext.ext_params, inti->ext.ext_params2);
372 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200373 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
374 inti->ext.ext_params,
375 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100376 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
377 rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR);
378 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
379 &vcpu->arch.sie_block->gpsw,
380 sizeof(psw_t));
381 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100382 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100383 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
384 (u32 *)__LC_EXT_PARAMS);
385 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
386 (u64 *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100387 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100388 case KVM_S390_SIGP_STOP:
389 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
390 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200391 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
392 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100393 __set_intercept_indicator(vcpu, inti);
394 break;
395
396 case KVM_S390_SIGP_SET_PREFIX:
397 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
398 inti->prefix.address);
399 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200400 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
401 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100402 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100403 break;
404
405 case KVM_S390_RESTART:
406 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
407 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200408 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
409 0, 0);
Jens Freimann1a03b762014-02-12 14:05:38 +0100410 rc = write_guest_lc(vcpu,
411 offsetof(struct _lowcore, restart_old_psw),
412 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
413 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
414 &vcpu->arch.sie_block->gpsw,
415 sizeof(psw_t));
David Hildenbrand6852d7b2014-03-14 10:59:29 +0100416 kvm_s390_vcpu_start(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100417 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100418 case KVM_S390_PROGRAM_INT:
419 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
420 inti->pgm.code,
421 table[vcpu->arch.sie_block->ipa >> 14]);
422 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200423 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
424 inti->pgm.code, 0);
David Hildenbrand87128362014-03-03 10:55:13 +0100425 rc = __deliver_prog_irq(vcpu, &inti->pgm);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100426 break;
427
Cornelia Huck48a3e952012-12-20 15:32:09 +0100428 case KVM_S390_MCHK:
429 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
430 inti->mchk.mcic);
431 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
432 inti->mchk.cr14,
433 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100434 rc = kvm_s390_vcpu_store_status(vcpu,
435 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens79882762014-01-02 10:59:41 +0100436 rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE);
437 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
438 &vcpu->arch.sie_block->gpsw,
439 sizeof(psw_t));
440 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100441 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100442 break;
443
Cornelia Huckd8346b72012-12-20 15:32:08 +0100444 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
445 {
446 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
447 inti->io.subchannel_nr;
448 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
449 inti->io.io_int_word;
450 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
451 vcpu->stat.deliver_io_int++;
452 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
453 param0, param1);
Heiko Carstens79882762014-01-02 10:59:41 +0100454 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
455 (u16 *)__LC_SUBCHANNEL_ID);
456 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
457 (u16 *)__LC_SUBCHANNEL_NR);
458 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
459 (u32 *)__LC_IO_INT_PARM);
460 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
461 (u32 *)__LC_IO_INT_WORD);
462 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
463 &vcpu->arch.sie_block->gpsw,
464 sizeof(psw_t));
465 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
466 &vcpu->arch.sie_block->gpsw,
467 sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100468 break;
469 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100470 default:
471 BUG();
472 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100473 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200474 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100475 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200476 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100477 }
478}
479
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100480static void deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100481{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100482 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100483
Jens Freimann1a03b762014-02-12 14:05:38 +0100484 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
485 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
486 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
487 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
488 &vcpu->arch.sie_block->gpsw,
489 sizeof(psw_t));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100490 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200491 printk("kvm: The guest lowcore is not mapped during interrupt "
492 "delivery, killing userspace\n");
493 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100494 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100495}
496
Dominik Dingel3c038e62013-10-07 17:11:48 +0200497int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100498{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200499 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
500 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
501 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100502 int rc = 0;
503
504 if (atomic_read(&li->active)) {
505 spin_lock_bh(&li->lock);
506 list_for_each_entry(inti, &li->list, list)
507 if (__interrupt_is_deliverable(vcpu, inti)) {
508 rc = 1;
509 break;
510 }
511 spin_unlock_bh(&li->lock);
512 }
513
514 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200515 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100516 list_for_each_entry(inti, &fi->list, list)
517 if (__interrupt_is_deliverable(vcpu, inti)) {
518 rc = 1;
519 break;
520 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200521 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100522 }
523
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100524 if (!rc && kvm_cpu_has_pending_timer(vcpu))
525 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100526
527 return rc;
528}
529
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300530int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
531{
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100532 if (!(vcpu->arch.sie_block->ckc <
533 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
534 return 0;
535 if (!ckc_interrupts_enabled(vcpu))
536 return 0;
537 return 1;
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300538}
539
Carsten Otteba5c1e92008-03-25 18:47:26 +0100540int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
541{
542 u64 now, sltime;
543 DECLARE_WAITQUEUE(wait, current);
544
545 vcpu->stat.exit_wait_state++;
546 if (kvm_cpu_has_interrupt(vcpu))
547 return 0;
548
Carsten Ottee52b2af2008-05-21 13:37:44 +0200549 __set_cpu_idle(vcpu);
550 spin_lock_bh(&vcpu->arch.local_int.lock);
551 vcpu->arch.local_int.timer_due = 0;
552 spin_unlock_bh(&vcpu->arch.local_int.lock);
553
Carsten Otteba5c1e92008-03-25 18:47:26 +0100554 if (psw_interrupts_disabled(vcpu)) {
555 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
556 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100557 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100558 }
559
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100560 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100561 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
562 goto no_timer;
563 }
564
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200565 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100566 if (vcpu->arch.sie_block->ckc < now) {
567 __unset_cpu_idle(vcpu);
568 return 0;
569 }
570
Heiko Carstensed4f2092013-01-14 16:55:55 +0100571 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100572
Christian Borntraegerca872302009-05-12 17:21:49 +0200573 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
574 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100575no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200576 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200577 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100578 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200579 add_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100580 while (list_empty(&vcpu->arch.local_int.list) &&
581 list_empty(&vcpu->arch.local_int.float_int->list) &&
582 (!vcpu->arch.local_int.timer_due) &&
583 !signal_pending(current)) {
584 set_current_state(TASK_INTERRUPTIBLE);
585 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200586 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100587 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200588 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100589 spin_lock_bh(&vcpu->arch.local_int.lock);
590 }
591 __unset_cpu_idle(vcpu);
592 __set_current_state(TASK_RUNNING);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200593 remove_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100594 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200595 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Thomas Huth800c1062013-09-12 10:33:45 +0200596 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
597
Christian Borntraegerca872302009-05-12 17:21:49 +0200598 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100599 return 0;
600}
601
Christian Borntraegerca872302009-05-12 17:21:49 +0200602void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100603{
Christian Borntraegerca872302009-05-12 17:21:49 +0200604 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100605
Christian Borntraegerca872302009-05-12 17:21:49 +0200606 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100607 vcpu->arch.local_int.timer_due = 1;
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200608 if (waitqueue_active(&vcpu->wq))
609 wake_up_interruptible(&vcpu->wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200610 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100611}
612
Christian Borntraegerca872302009-05-12 17:21:49 +0200613/*
614 * low level hrtimer wake routine. Because this runs in hardirq context
615 * we schedule a tasklet to do the real work.
616 */
617enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
618{
619 struct kvm_vcpu *vcpu;
620
621 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100622 vcpu->preempted = true;
Christian Borntraegerca872302009-05-12 17:21:49 +0200623 tasklet_schedule(&vcpu->arch.tasklet);
624
625 return HRTIMER_NORESTART;
626}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100627
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100628void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
629{
630 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
631 struct kvm_s390_interrupt_info *n, *inti = NULL;
632
633 spin_lock_bh(&li->lock);
634 list_for_each_entry_safe(inti, n, &li->list, list) {
635 list_del(&inti->list);
636 kfree(inti);
637 }
638 atomic_set(&li->active, 0);
639 spin_unlock_bh(&li->lock);
640}
641
Carsten Otteba5c1e92008-03-25 18:47:26 +0100642void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
643{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200644 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
645 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
646 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100647 int deliver;
648
649 __reset_intercept_indicators(vcpu);
650 if (atomic_read(&li->active)) {
651 do {
652 deliver = 0;
653 spin_lock_bh(&li->lock);
654 list_for_each_entry_safe(inti, n, &li->list, list) {
655 if (__interrupt_is_deliverable(vcpu, inti)) {
656 list_del(&inti->list);
657 deliver = 1;
658 break;
659 }
660 __set_intercept_indicator(vcpu, inti);
661 }
662 if (list_empty(&li->list))
663 atomic_set(&li->active, 0);
664 spin_unlock_bh(&li->lock);
665 if (deliver) {
666 __do_deliver_interrupt(vcpu, inti);
667 kfree(inti);
668 }
669 } while (deliver);
670 }
671
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100672 if (kvm_cpu_has_pending_timer(vcpu))
673 deliver_ckc_interrupt(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100674
675 if (atomic_read(&fi->active)) {
676 do {
677 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200678 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100679 list_for_each_entry_safe(inti, n, &fi->list, list) {
680 if (__interrupt_is_deliverable(vcpu, inti)) {
681 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100682 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100683 deliver = 1;
684 break;
685 }
686 __set_intercept_indicator(vcpu, inti);
687 }
688 if (list_empty(&fi->list))
689 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200690 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100691 if (deliver) {
692 __do_deliver_interrupt(vcpu, inti);
693 kfree(inti);
694 }
695 } while (deliver);
696 }
697}
698
Cornelia Huck48a3e952012-12-20 15:32:09 +0100699void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
700{
701 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
702 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
703 struct kvm_s390_interrupt_info *n, *inti = NULL;
704 int deliver;
705
706 __reset_intercept_indicators(vcpu);
707 if (atomic_read(&li->active)) {
708 do {
709 deliver = 0;
710 spin_lock_bh(&li->lock);
711 list_for_each_entry_safe(inti, n, &li->list, list) {
712 if ((inti->type == KVM_S390_MCHK) &&
713 __interrupt_is_deliverable(vcpu, inti)) {
714 list_del(&inti->list);
715 deliver = 1;
716 break;
717 }
718 __set_intercept_indicator(vcpu, inti);
719 }
720 if (list_empty(&li->list))
721 atomic_set(&li->active, 0);
722 spin_unlock_bh(&li->lock);
723 if (deliver) {
724 __do_deliver_interrupt(vcpu, inti);
725 kfree(inti);
726 }
727 } while (deliver);
728 }
729
730 if (atomic_read(&fi->active)) {
731 do {
732 deliver = 0;
733 spin_lock(&fi->lock);
734 list_for_each_entry_safe(inti, n, &fi->list, list) {
735 if ((inti->type == KVM_S390_MCHK) &&
736 __interrupt_is_deliverable(vcpu, inti)) {
737 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100738 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100739 deliver = 1;
740 break;
741 }
742 __set_intercept_indicator(vcpu, inti);
743 }
744 if (list_empty(&fi->list))
745 atomic_set(&fi->active, 0);
746 spin_unlock(&fi->lock);
747 if (deliver) {
748 __do_deliver_interrupt(vcpu, inti);
749 kfree(inti);
750 }
751 } while (deliver);
752 }
753}
754
Carsten Otteba5c1e92008-03-25 18:47:26 +0100755int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
756{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200757 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
758 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100759
760 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
761 if (!inti)
762 return -ENOMEM;
763
Joe Perchesa419aef2009-08-18 11:18:35 -0700764 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100765 inti->pgm.code = code;
766
767 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200768 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100769 spin_lock_bh(&li->lock);
770 list_add(&inti->list, &li->list);
771 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200772 BUG_ON(waitqueue_active(li->wq));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100773 spin_unlock_bh(&li->lock);
774 return 0;
775}
776
Jens Freimannbcd84682014-02-11 11:07:05 +0100777int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
778 struct kvm_s390_pgm_info *pgm_info)
779{
780 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
781 struct kvm_s390_interrupt_info *inti;
782
783 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
784 if (!inti)
785 return -ENOMEM;
786
787 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
788 pgm_info->code);
789 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
790 pgm_info->code, 0, 1);
791
792 inti->type = KVM_S390_PROGRAM_INT;
793 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
794 spin_lock_bh(&li->lock);
795 list_add(&inti->list, &li->list);
796 atomic_set(&li->active, 1);
797 BUG_ON(waitqueue_active(li->wq));
798 spin_unlock_bh(&li->lock);
799 return 0;
800}
801
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100802struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
803 u64 cr6, u64 schid)
804{
805 struct kvm_s390_float_interrupt *fi;
806 struct kvm_s390_interrupt_info *inti, *iter;
807
808 if ((!schid && !cr6) || (schid && cr6))
809 return NULL;
810 mutex_lock(&kvm->lock);
811 fi = &kvm->arch.float_int;
812 spin_lock(&fi->lock);
813 inti = NULL;
814 list_for_each_entry(iter, &fi->list, list) {
815 if (!is_ioint(iter->type))
816 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100817 if (cr6 &&
818 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100819 continue;
820 if (schid) {
821 if (((schid & 0x00000000ffff0000) >> 16) !=
822 iter->io.subchannel_id)
823 continue;
824 if ((schid & 0x000000000000ffff) !=
825 iter->io.subchannel_nr)
826 continue;
827 }
828 inti = iter;
829 break;
830 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100831 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100832 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100833 fi->irq_count--;
834 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100835 if (list_empty(&fi->list))
836 atomic_set(&fi->active, 0);
837 spin_unlock(&fi->lock);
838 mutex_unlock(&kvm->lock);
839 return inti;
840}
841
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100842static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100843{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200844 struct kvm_s390_local_interrupt *li;
845 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200846 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100847 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100848 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100849 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100850
Carsten Otteba5c1e92008-03-25 18:47:26 +0100851 mutex_lock(&kvm->lock);
852 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200853 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100854 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
855 rc = -EINVAL;
856 goto unlock_fi;
857 }
858 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200859 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100860 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200861 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100862 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
863
Cornelia Huckd8346b72012-12-20 15:32:08 +0100864 /* Keep I/O interrupts sorted in isc order. */
865 list_for_each_entry(iter, &fi->list, list) {
866 if (!is_ioint(iter->type))
867 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100868 if (int_word_to_isc_bits(iter->io.io_int_word)
869 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100870 continue;
871 break;
872 }
873 list_add_tail(&inti->list, &iter->list);
874 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100875 atomic_set(&fi->active, 1);
876 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
877 if (sigcpu == KVM_MAX_VCPUS) {
878 do {
879 sigcpu = fi->next_rr_cpu++;
880 if (sigcpu == KVM_MAX_VCPUS)
881 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100882 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100883 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100884 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
885 li = &dst_vcpu->arch.local_int;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100886 spin_lock_bh(&li->lock);
887 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200888 if (waitqueue_active(li->wq))
889 wake_up_interruptible(li->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100890 kvm_get_vcpu(kvm, sigcpu)->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100891 spin_unlock_bh(&li->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100892unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200893 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100894 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100895 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200896}
897
898int kvm_s390_inject_vm(struct kvm *kvm,
899 struct kvm_s390_interrupt *s390int)
900{
901 struct kvm_s390_interrupt_info *inti;
902
903 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
904 if (!inti)
905 return -ENOMEM;
906
907 inti->type = s390int->type;
908 switch (inti->type) {
909 case KVM_S390_INT_VIRTIO:
910 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
911 s390int->parm, s390int->parm64);
912 inti->ext.ext_params = s390int->parm;
913 inti->ext.ext_params2 = s390int->parm64;
914 break;
915 case KVM_S390_INT_SERVICE:
916 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
917 inti->ext.ext_params = s390int->parm;
918 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200919 case KVM_S390_INT_PFAULT_DONE:
920 inti->type = s390int->type;
921 inti->ext.ext_params2 = s390int->parm64;
922 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200923 case KVM_S390_MCHK:
924 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
925 s390int->parm64);
926 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
927 inti->mchk.mcic = s390int->parm64;
928 break;
929 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
930 if (inti->type & IOINT_AI_MASK)
931 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
932 else
933 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
934 s390int->type & IOINT_CSSID_MASK,
935 s390int->type & IOINT_SSID_MASK,
936 s390int->type & IOINT_SCHID_MASK);
937 inti->io.subchannel_id = s390int->parm >> 16;
938 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
939 inti->io.io_int_parm = s390int->parm64 >> 32;
940 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
941 break;
942 default:
943 kfree(inti);
944 return -EINVAL;
945 }
946 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
947 2);
948
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100949 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100950}
951
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100952void kvm_s390_reinject_io_int(struct kvm *kvm,
953 struct kvm_s390_interrupt_info *inti)
954{
955 __inject_vm(kvm, inti);
956}
957
Carsten Otteba5c1e92008-03-25 18:47:26 +0100958int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
959 struct kvm_s390_interrupt *s390int)
960{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200961 struct kvm_s390_local_interrupt *li;
962 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100963
964 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
965 if (!inti)
966 return -ENOMEM;
967
968 switch (s390int->type) {
969 case KVM_S390_PROGRAM_INT:
970 if (s390int->parm & 0xffff0000) {
971 kfree(inti);
972 return -EINVAL;
973 }
974 inti->type = s390int->type;
975 inti->pgm.code = s390int->parm;
976 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
977 s390int->parm);
978 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100979 case KVM_S390_SIGP_SET_PREFIX:
980 inti->prefix.address = s390int->parm;
981 inti->type = s390int->type;
982 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
983 s390int->parm);
984 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100985 case KVM_S390_SIGP_STOP:
986 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100987 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
988 inti->type = s390int->type;
989 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200990 case KVM_S390_INT_EXTERNAL_CALL:
991 if (s390int->parm & 0xffff0000) {
992 kfree(inti);
993 return -EINVAL;
994 }
995 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
996 s390int->parm);
997 inti->type = s390int->type;
998 inti->extcall.code = s390int->parm;
999 break;
1000 case KVM_S390_INT_EMERGENCY:
1001 if (s390int->parm & 0xffff0000) {
1002 kfree(inti);
1003 return -EINVAL;
1004 }
1005 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
1006 inti->type = s390int->type;
1007 inti->emerg.code = s390int->parm;
1008 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001009 case KVM_S390_MCHK:
1010 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1011 s390int->parm64);
1012 inti->type = s390int->type;
1013 inti->mchk.mcic = s390int->parm64;
1014 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001015 case KVM_S390_INT_PFAULT_INIT:
1016 inti->type = s390int->type;
1017 inti->ext.ext_params2 = s390int->parm64;
1018 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001019 case KVM_S390_INT_VIRTIO:
1020 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001021 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001022 default:
1023 kfree(inti);
1024 return -EINVAL;
1025 }
Cornelia Huckade38c32012-07-23 17:20:30 +02001026 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
1027 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001028
1029 mutex_lock(&vcpu->kvm->lock);
1030 li = &vcpu->arch.local_int;
1031 spin_lock_bh(&li->lock);
1032 if (inti->type == KVM_S390_PROGRAM_INT)
1033 list_add(&inti->list, &li->list);
1034 else
1035 list_add_tail(&inti->list, &li->list);
1036 atomic_set(&li->active, 1);
1037 if (inti->type == KVM_S390_SIGP_STOP)
1038 li->action_bits |= ACTION_STOP_ON_STOP;
1039 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001040 if (waitqueue_active(&vcpu->wq))
1041 wake_up_interruptible(&vcpu->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +01001042 vcpu->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001043 spin_unlock_bh(&li->lock);
1044 mutex_unlock(&vcpu->kvm->lock);
1045 return 0;
1046}
Jens Freimannc05c4182013-10-07 16:13:45 +02001047
Christian Borntraeger67335e62014-03-25 17:09:08 +01001048void kvm_s390_clear_float_irqs(struct kvm *kvm)
Jens Freimannc05c4182013-10-07 16:13:45 +02001049{
1050 struct kvm_s390_float_interrupt *fi;
1051 struct kvm_s390_interrupt_info *n, *inti = NULL;
1052
1053 mutex_lock(&kvm->lock);
1054 fi = &kvm->arch.float_int;
1055 spin_lock(&fi->lock);
1056 list_for_each_entry_safe(inti, n, &fi->list, list) {
1057 list_del(&inti->list);
1058 kfree(inti);
1059 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001060 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001061 atomic_set(&fi->active, 0);
1062 spin_unlock(&fi->lock);
1063 mutex_unlock(&kvm->lock);
1064}
1065
1066static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1067 u8 *addr)
1068{
1069 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1070 struct kvm_s390_irq irq = {0};
1071
1072 irq.type = inti->type;
1073 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001074 case KVM_S390_INT_PFAULT_INIT:
1075 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001076 case KVM_S390_INT_VIRTIO:
1077 case KVM_S390_INT_SERVICE:
1078 irq.u.ext = inti->ext;
1079 break;
1080 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1081 irq.u.io = inti->io;
1082 break;
1083 case KVM_S390_MCHK:
1084 irq.u.mchk = inti->mchk;
1085 break;
1086 default:
1087 return -EINVAL;
1088 }
1089
1090 if (copy_to_user(uptr, &irq, sizeof(irq)))
1091 return -EFAULT;
1092
1093 return 0;
1094}
1095
1096static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1097{
1098 struct kvm_s390_interrupt_info *inti;
1099 struct kvm_s390_float_interrupt *fi;
1100 int ret = 0;
1101 int n = 0;
1102
1103 mutex_lock(&kvm->lock);
1104 fi = &kvm->arch.float_int;
1105 spin_lock(&fi->lock);
1106
1107 list_for_each_entry(inti, &fi->list, list) {
1108 if (len < sizeof(struct kvm_s390_irq)) {
1109 /* signal userspace to try again */
1110 ret = -ENOMEM;
1111 break;
1112 }
1113 ret = copy_irq_to_user(inti, buf);
1114 if (ret)
1115 break;
1116 buf += sizeof(struct kvm_s390_irq);
1117 len -= sizeof(struct kvm_s390_irq);
1118 n++;
1119 }
1120
1121 spin_unlock(&fi->lock);
1122 mutex_unlock(&kvm->lock);
1123
1124 return ret < 0 ? ret : n;
1125}
1126
1127static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1128{
1129 int r;
1130
1131 switch (attr->group) {
1132 case KVM_DEV_FLIC_GET_ALL_IRQS:
1133 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1134 attr->attr);
1135 break;
1136 default:
1137 r = -EINVAL;
1138 }
1139
1140 return r;
1141}
1142
1143static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1144 u64 addr)
1145{
1146 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1147 void *target = NULL;
1148 void __user *source;
1149 u64 size;
1150
1151 if (get_user(inti->type, (u64 __user *)addr))
1152 return -EFAULT;
1153
1154 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001155 case KVM_S390_INT_PFAULT_INIT:
1156 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001157 case KVM_S390_INT_VIRTIO:
1158 case KVM_S390_INT_SERVICE:
1159 target = (void *) &inti->ext;
1160 source = &uptr->u.ext;
1161 size = sizeof(inti->ext);
1162 break;
1163 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1164 target = (void *) &inti->io;
1165 source = &uptr->u.io;
1166 size = sizeof(inti->io);
1167 break;
1168 case KVM_S390_MCHK:
1169 target = (void *) &inti->mchk;
1170 source = &uptr->u.mchk;
1171 size = sizeof(inti->mchk);
1172 break;
1173 default:
1174 return -EINVAL;
1175 }
1176
1177 if (copy_from_user(target, source, size))
1178 return -EFAULT;
1179
1180 return 0;
1181}
1182
1183static int enqueue_floating_irq(struct kvm_device *dev,
1184 struct kvm_device_attr *attr)
1185{
1186 struct kvm_s390_interrupt_info *inti = NULL;
1187 int r = 0;
1188 int len = attr->attr;
1189
1190 if (len % sizeof(struct kvm_s390_irq) != 0)
1191 return -EINVAL;
1192 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1193 return -EINVAL;
1194
1195 while (len >= sizeof(struct kvm_s390_irq)) {
1196 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1197 if (!inti)
1198 return -ENOMEM;
1199
1200 r = copy_irq_from_user(inti, attr->addr);
1201 if (r) {
1202 kfree(inti);
1203 return r;
1204 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001205 r = __inject_vm(dev->kvm, inti);
1206 if (r) {
1207 kfree(inti);
1208 return r;
1209 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001210 len -= sizeof(struct kvm_s390_irq);
1211 attr->addr += sizeof(struct kvm_s390_irq);
1212 }
1213
1214 return r;
1215}
1216
Cornelia Huck841b91c2013-07-15 13:36:01 +02001217static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1218{
1219 if (id >= MAX_S390_IO_ADAPTERS)
1220 return NULL;
1221 return kvm->arch.adapters[id];
1222}
1223
1224static int register_io_adapter(struct kvm_device *dev,
1225 struct kvm_device_attr *attr)
1226{
1227 struct s390_io_adapter *adapter;
1228 struct kvm_s390_io_adapter adapter_info;
1229
1230 if (copy_from_user(&adapter_info,
1231 (void __user *)attr->addr, sizeof(adapter_info)))
1232 return -EFAULT;
1233
1234 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1235 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1236 return -EINVAL;
1237
1238 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1239 if (!adapter)
1240 return -ENOMEM;
1241
1242 INIT_LIST_HEAD(&adapter->maps);
1243 init_rwsem(&adapter->maps_lock);
1244 atomic_set(&adapter->nr_maps, 0);
1245 adapter->id = adapter_info.id;
1246 adapter->isc = adapter_info.isc;
1247 adapter->maskable = adapter_info.maskable;
1248 adapter->masked = false;
1249 adapter->swap = adapter_info.swap;
1250 dev->kvm->arch.adapters[adapter->id] = adapter;
1251
1252 return 0;
1253}
1254
1255int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1256{
1257 int ret;
1258 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1259
1260 if (!adapter || !adapter->maskable)
1261 return -EINVAL;
1262 ret = adapter->masked;
1263 adapter->masked = masked;
1264 return ret;
1265}
1266
1267static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1268{
1269 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1270 struct s390_map_info *map;
1271 int ret;
1272
1273 if (!adapter || !addr)
1274 return -EINVAL;
1275
1276 map = kzalloc(sizeof(*map), GFP_KERNEL);
1277 if (!map) {
1278 ret = -ENOMEM;
1279 goto out;
1280 }
1281 INIT_LIST_HEAD(&map->list);
1282 map->guest_addr = addr;
1283 map->addr = gmap_translate(addr, kvm->arch.gmap);
1284 if (map->addr == -EFAULT) {
1285 ret = -EFAULT;
1286 goto out;
1287 }
1288 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1289 if (ret < 0)
1290 goto out;
1291 BUG_ON(ret != 1);
1292 down_write(&adapter->maps_lock);
1293 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1294 list_add_tail(&map->list, &adapter->maps);
1295 ret = 0;
1296 } else {
1297 put_page(map->page);
1298 ret = -EINVAL;
1299 }
1300 up_write(&adapter->maps_lock);
1301out:
1302 if (ret)
1303 kfree(map);
1304 return ret;
1305}
1306
1307static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1308{
1309 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1310 struct s390_map_info *map, *tmp;
1311 int found = 0;
1312
1313 if (!adapter || !addr)
1314 return -EINVAL;
1315
1316 down_write(&adapter->maps_lock);
1317 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1318 if (map->guest_addr == addr) {
1319 found = 1;
1320 atomic_dec(&adapter->nr_maps);
1321 list_del(&map->list);
1322 put_page(map->page);
1323 kfree(map);
1324 break;
1325 }
1326 }
1327 up_write(&adapter->maps_lock);
1328
1329 return found ? 0 : -EINVAL;
1330}
1331
1332void kvm_s390_destroy_adapters(struct kvm *kvm)
1333{
1334 int i;
1335 struct s390_map_info *map, *tmp;
1336
1337 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1338 if (!kvm->arch.adapters[i])
1339 continue;
1340 list_for_each_entry_safe(map, tmp,
1341 &kvm->arch.adapters[i]->maps, list) {
1342 list_del(&map->list);
1343 put_page(map->page);
1344 kfree(map);
1345 }
1346 kfree(kvm->arch.adapters[i]);
1347 }
1348}
1349
1350static int modify_io_adapter(struct kvm_device *dev,
1351 struct kvm_device_attr *attr)
1352{
1353 struct kvm_s390_io_adapter_req req;
1354 struct s390_io_adapter *adapter;
1355 int ret;
1356
1357 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1358 return -EFAULT;
1359
1360 adapter = get_io_adapter(dev->kvm, req.id);
1361 if (!adapter)
1362 return -EINVAL;
1363 switch (req.type) {
1364 case KVM_S390_IO_ADAPTER_MASK:
1365 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1366 if (ret > 0)
1367 ret = 0;
1368 break;
1369 case KVM_S390_IO_ADAPTER_MAP:
1370 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1371 break;
1372 case KVM_S390_IO_ADAPTER_UNMAP:
1373 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1374 break;
1375 default:
1376 ret = -EINVAL;
1377 }
1378
1379 return ret;
1380}
1381
Jens Freimannc05c4182013-10-07 16:13:45 +02001382static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1383{
1384 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001385 unsigned int i;
1386 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001387
1388 switch (attr->group) {
1389 case KVM_DEV_FLIC_ENQUEUE:
1390 r = enqueue_floating_irq(dev, attr);
1391 break;
1392 case KVM_DEV_FLIC_CLEAR_IRQS:
1393 r = 0;
Christian Borntraeger67335e62014-03-25 17:09:08 +01001394 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001395 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001396 case KVM_DEV_FLIC_APF_ENABLE:
1397 dev->kvm->arch.gmap->pfault_enabled = 1;
1398 break;
1399 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1400 dev->kvm->arch.gmap->pfault_enabled = 0;
1401 /*
1402 * Make sure no async faults are in transition when
1403 * clearing the queues. So we don't need to worry
1404 * about late coming workers.
1405 */
1406 synchronize_srcu(&dev->kvm->srcu);
1407 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1408 kvm_clear_async_pf_completion_queue(vcpu);
1409 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001410 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1411 r = register_io_adapter(dev, attr);
1412 break;
1413 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1414 r = modify_io_adapter(dev, attr);
1415 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001416 default:
1417 r = -EINVAL;
1418 }
1419
1420 return r;
1421}
1422
1423static int flic_create(struct kvm_device *dev, u32 type)
1424{
1425 if (!dev)
1426 return -EINVAL;
1427 if (dev->kvm->arch.flic)
1428 return -EINVAL;
1429 dev->kvm->arch.flic = dev;
1430 return 0;
1431}
1432
1433static void flic_destroy(struct kvm_device *dev)
1434{
1435 dev->kvm->arch.flic = NULL;
1436 kfree(dev);
1437}
1438
1439/* s390 floating irq controller (flic) */
1440struct kvm_device_ops kvm_flic_ops = {
1441 .name = "kvm-flic",
1442 .get_attr = flic_get_attr,
1443 .set_attr = flic_set_attr,
1444 .create = flic_create,
1445 .destroy = flic_destroy,
1446};
Cornelia Huck84223592013-07-15 13:36:01 +02001447
1448static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1449{
1450 unsigned long bit;
1451
1452 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1453
1454 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1455}
1456
1457static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1458 u64 addr)
1459{
1460 struct s390_map_info *map;
1461
1462 if (!adapter)
1463 return NULL;
1464
1465 list_for_each_entry(map, &adapter->maps, list) {
1466 if (map->guest_addr == addr)
1467 return map;
1468 }
1469 return NULL;
1470}
1471
1472static int adapter_indicators_set(struct kvm *kvm,
1473 struct s390_io_adapter *adapter,
1474 struct kvm_s390_adapter_int *adapter_int)
1475{
1476 unsigned long bit;
1477 int summary_set, idx;
1478 struct s390_map_info *info;
1479 void *map;
1480
1481 info = get_map_info(adapter, adapter_int->ind_addr);
1482 if (!info)
1483 return -1;
1484 map = page_address(info->page);
1485 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1486 set_bit(bit, map);
1487 idx = srcu_read_lock(&kvm->srcu);
1488 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1489 set_page_dirty_lock(info->page);
1490 info = get_map_info(adapter, adapter_int->summary_addr);
1491 if (!info) {
1492 srcu_read_unlock(&kvm->srcu, idx);
1493 return -1;
1494 }
1495 map = page_address(info->page);
1496 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1497 adapter->swap);
1498 summary_set = test_and_set_bit(bit, map);
1499 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1500 set_page_dirty_lock(info->page);
1501 srcu_read_unlock(&kvm->srcu, idx);
1502 return summary_set ? 0 : 1;
1503}
1504
1505/*
1506 * < 0 - not injected due to error
1507 * = 0 - coalesced, summary indicator already active
1508 * > 0 - injected interrupt
1509 */
1510static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1511 struct kvm *kvm, int irq_source_id, int level,
1512 bool line_status)
1513{
1514 int ret;
1515 struct s390_io_adapter *adapter;
1516
1517 /* We're only interested in the 0->1 transition. */
1518 if (!level)
1519 return 0;
1520 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1521 if (!adapter)
1522 return -1;
1523 down_read(&adapter->maps_lock);
1524 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1525 up_read(&adapter->maps_lock);
1526 if ((ret > 0) && !adapter->masked) {
1527 struct kvm_s390_interrupt s390int = {
1528 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1529 .parm = 0,
1530 .parm64 = (adapter->isc << 27) | 0x80000000,
1531 };
1532 ret = kvm_s390_inject_vm(kvm, &s390int);
1533 if (ret == 0)
1534 ret = 1;
1535 }
1536 return ret;
1537}
1538
1539int kvm_set_routing_entry(struct kvm_irq_routing_table *rt,
1540 struct kvm_kernel_irq_routing_entry *e,
1541 const struct kvm_irq_routing_entry *ue)
1542{
1543 int ret;
1544
1545 switch (ue->type) {
1546 case KVM_IRQ_ROUTING_S390_ADAPTER:
1547 e->set = set_adapter_int;
1548 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1549 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1550 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1551 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1552 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1553 ret = 0;
1554 break;
1555 default:
1556 ret = -EINVAL;
1557 }
1558
1559 return ret;
1560}
1561
1562int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1563 int irq_source_id, int level, bool line_status)
1564{
1565 return -EINVAL;
1566}