blob: 0165f1b089ac92c456cd2d2977849168ce17fe4f [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;
64 return 1;
65}
66
Cornelia Huck79fd50c2013-02-07 13:20:52 +010067static u64 int_word_to_isc_bits(u32 int_word)
68{
69 u8 isc = (int_word & 0x38000000) >> 27;
70
71 return (0x80 >> isc) << 24;
72}
73
Carsten Otteba5c1e92008-03-25 18:47:26 +010074static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020075 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010076{
77 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020078 case KVM_S390_INT_EXTERNAL_CALL:
79 if (psw_extint_disabled(vcpu))
80 return 0;
81 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
82 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +010083 case KVM_S390_INT_EMERGENCY:
84 if (psw_extint_disabled(vcpu))
85 return 0;
86 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
87 return 1;
88 return 0;
89 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +020090 case KVM_S390_INT_PFAULT_INIT:
91 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +010092 case KVM_S390_INT_VIRTIO:
93 if (psw_extint_disabled(vcpu))
94 return 0;
95 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
96 return 1;
97 return 0;
98 case KVM_S390_PROGRAM_INT:
99 case KVM_S390_SIGP_STOP:
100 case KVM_S390_SIGP_SET_PREFIX:
101 case KVM_S390_RESTART:
102 return 1;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100103 case KVM_S390_MCHK:
104 if (psw_mchk_disabled(vcpu))
105 return 0;
106 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
107 return 1;
108 return 0;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100109 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
110 if (psw_ioint_disabled(vcpu))
111 return 0;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100112 if (vcpu->arch.sie_block->gcr[6] &
113 int_word_to_isc_bits(inti->io.io_int_word))
Cornelia Huckd8346b72012-12-20 15:32:08 +0100114 return 1;
115 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100116 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100117 printk(KERN_WARNING "illegal interrupt type %llx\n",
118 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100119 BUG();
120 }
121 return 0;
122}
123
124static void __set_cpu_idle(struct kvm_vcpu *vcpu)
125{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100126 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
127 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
128}
129
130static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
131{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100132 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
133 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
134}
135
136static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
137{
138 atomic_clear_mask(CPUSTAT_ECALL_PEND |
139 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
140 &vcpu->arch.sie_block->cpuflags);
141 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100142 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
143
144 if (guestdbg_enabled(vcpu)) {
145 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
146 LCTL_CR10 | LCTL_CR11);
147 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
148 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100149}
150
151static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
152{
153 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
154}
155
156static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200157 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100158{
159 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200160 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100161 case KVM_S390_INT_EMERGENCY:
162 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200163 case KVM_S390_INT_PFAULT_INIT:
164 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100165 case KVM_S390_INT_VIRTIO:
166 if (psw_extint_disabled(vcpu))
167 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
168 else
169 vcpu->arch.sie_block->lctl |= LCTL_CR0;
170 break;
171 case KVM_S390_SIGP_STOP:
172 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
173 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100174 case KVM_S390_MCHK:
175 if (psw_mchk_disabled(vcpu))
176 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
177 else
178 vcpu->arch.sie_block->lctl |= LCTL_CR14;
179 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100180 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
181 if (psw_ioint_disabled(vcpu))
182 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
183 else
184 vcpu->arch.sie_block->lctl |= LCTL_CR6;
185 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100186 default:
187 BUG();
188 }
189}
190
David Hildenbrand87128362014-03-03 10:55:13 +0100191static int __deliver_prog_irq(struct kvm_vcpu *vcpu,
192 struct kvm_s390_pgm_info *pgm_info)
193{
194 const unsigned short table[] = { 2, 4, 4, 6 };
195 int rc = 0;
196
197 switch (pgm_info->code & ~PGM_PER) {
198 case PGM_AFX_TRANSLATION:
199 case PGM_ASX_TRANSLATION:
200 case PGM_EX_TRANSLATION:
201 case PGM_LFX_TRANSLATION:
202 case PGM_LSTE_SEQUENCE:
203 case PGM_LSX_TRANSLATION:
204 case PGM_LX_TRANSLATION:
205 case PGM_PRIMARY_AUTHORITY:
206 case PGM_SECONDARY_AUTHORITY:
207 case PGM_SPACE_SWITCH:
208 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
209 (u64 *)__LC_TRANS_EXC_CODE);
210 break;
211 case PGM_ALEN_TRANSLATION:
212 case PGM_ALE_SEQUENCE:
213 case PGM_ASTE_INSTANCE:
214 case PGM_ASTE_SEQUENCE:
215 case PGM_ASTE_VALIDITY:
216 case PGM_EXTENDED_AUTHORITY:
217 rc = put_guest_lc(vcpu, pgm_info->exc_access_id,
218 (u8 *)__LC_EXC_ACCESS_ID);
219 break;
220 case PGM_ASCE_TYPE:
221 case PGM_PAGE_TRANSLATION:
222 case PGM_REGION_FIRST_TRANS:
223 case PGM_REGION_SECOND_TRANS:
224 case PGM_REGION_THIRD_TRANS:
225 case PGM_SEGMENT_TRANSLATION:
226 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
227 (u64 *)__LC_TRANS_EXC_CODE);
228 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
229 (u8 *)__LC_EXC_ACCESS_ID);
230 rc |= put_guest_lc(vcpu, pgm_info->op_access_id,
231 (u8 *)__LC_OP_ACCESS_ID);
232 break;
233 case PGM_MONITOR:
234 rc = put_guest_lc(vcpu, pgm_info->mon_class_nr,
235 (u64 *)__LC_MON_CLASS_NR);
236 rc |= put_guest_lc(vcpu, pgm_info->mon_code,
237 (u64 *)__LC_MON_CODE);
238 break;
239 case PGM_DATA:
240 rc = put_guest_lc(vcpu, pgm_info->data_exc_code,
241 (u32 *)__LC_DATA_EXC_CODE);
242 break;
243 case PGM_PROTECTION:
244 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
245 (u64 *)__LC_TRANS_EXC_CODE);
246 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
247 (u8 *)__LC_EXC_ACCESS_ID);
248 break;
249 }
250
251 if (pgm_info->code & PGM_PER) {
252 rc |= put_guest_lc(vcpu, pgm_info->per_code,
253 (u8 *) __LC_PER_CODE);
254 rc |= put_guest_lc(vcpu, pgm_info->per_atmid,
255 (u8 *)__LC_PER_ATMID);
256 rc |= put_guest_lc(vcpu, pgm_info->per_address,
257 (u64 *) __LC_PER_ADDRESS);
258 rc |= put_guest_lc(vcpu, pgm_info->per_access_id,
259 (u8 *) __LC_PER_ACCESS_ID);
260 }
261
262 switch (vcpu->arch.sie_block->icptcode) {
263 case ICPT_INST:
264 case ICPT_INSTPROGI:
265 case ICPT_OPEREXC:
266 case ICPT_PARTEXEC:
267 case ICPT_IOINST:
268 /* last instruction only stored for these icptcodes */
269 rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
270 (u16 *) __LC_PGM_ILC);
271 break;
272 case ICPT_PROGI:
273 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->pgmilc,
274 (u16 *) __LC_PGM_ILC);
275 break;
276 default:
277 rc |= put_guest_lc(vcpu, 0,
278 (u16 *) __LC_PGM_ILC);
279 }
280
281 rc |= put_guest_lc(vcpu, pgm_info->code,
282 (u16 *)__LC_PGM_INT_CODE);
283 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
284 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
285 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
286 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
287
288 return rc;
289}
290
Carsten Otteba5c1e92008-03-25 18:47:26 +0100291static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200292 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100293{
294 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100295 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100296
297 switch (inti->type) {
298 case KVM_S390_INT_EMERGENCY:
299 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
300 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200301 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
302 inti->emerg.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100303 rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE);
304 rc |= put_guest_lc(vcpu, inti->emerg.code,
305 (u16 *)__LC_EXT_CPU_ADDR);
306 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
307 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
308 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100309 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100310 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200311 case KVM_S390_INT_EXTERNAL_CALL:
312 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
313 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200314 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
315 inti->extcall.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100316 rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE);
317 rc |= put_guest_lc(vcpu, inti->extcall.code,
318 (u16 *)__LC_EXT_CPU_ADDR);
319 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
320 &vcpu->arch.sie_block->gpsw,
321 sizeof(psw_t));
322 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
323 &vcpu->arch.sie_block->gpsw,
324 sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200325 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100326 case KVM_S390_INT_SERVICE:
327 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
328 inti->ext.ext_params);
329 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200330 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
331 inti->ext.ext_params, 0);
Heiko Carstens79882762014-01-02 10:59:41 +0100332 rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE);
333 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
334 &vcpu->arch.sie_block->gpsw,
335 sizeof(psw_t));
336 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100337 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100338 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
339 (u32 *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100340 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200341 case KVM_S390_INT_PFAULT_INIT:
342 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
343 inti->ext.ext_params2);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100344 rc = put_guest_lc(vcpu, 0x2603, (u16 *) __LC_EXT_INT_CODE);
345 rc |= put_guest_lc(vcpu, 0x0600, (u16 *) __LC_EXT_CPU_ADDR);
346 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
347 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
348 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200349 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann1a03b7642014-02-12 14:05:38 +0100350 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
351 (u64 *) __LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200352 break;
353 case KVM_S390_INT_PFAULT_DONE:
354 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
355 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100356 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
357 rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR);
358 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
359 &vcpu->arch.sie_block->gpsw,
360 sizeof(psw_t));
361 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200362 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100363 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
364 (u64 *)__LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200365 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100366 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100367 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100368 inti->ext.ext_params, inti->ext.ext_params2);
369 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200370 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
371 inti->ext.ext_params,
372 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100373 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
374 rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR);
375 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
376 &vcpu->arch.sie_block->gpsw,
377 sizeof(psw_t));
378 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100379 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100380 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
381 (u32 *)__LC_EXT_PARAMS);
382 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
383 (u64 *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100384 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100385 case KVM_S390_SIGP_STOP:
386 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
387 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200388 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
389 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100390 __set_intercept_indicator(vcpu, inti);
391 break;
392
393 case KVM_S390_SIGP_SET_PREFIX:
394 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
395 inti->prefix.address);
396 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200397 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
398 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100399 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100400 break;
401
402 case KVM_S390_RESTART:
403 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
404 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200405 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
406 0, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100407 rc = write_guest_lc(vcpu,
408 offsetof(struct _lowcore, restart_old_psw),
409 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
410 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
411 &vcpu->arch.sie_block->gpsw,
412 sizeof(psw_t));
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100413 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100414 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100415 case KVM_S390_PROGRAM_INT:
416 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
417 inti->pgm.code,
418 table[vcpu->arch.sie_block->ipa >> 14]);
419 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200420 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
421 inti->pgm.code, 0);
David Hildenbrand87128362014-03-03 10:55:13 +0100422 rc = __deliver_prog_irq(vcpu, &inti->pgm);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100423 break;
424
Cornelia Huck48a3e952012-12-20 15:32:09 +0100425 case KVM_S390_MCHK:
426 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
427 inti->mchk.mcic);
428 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
429 inti->mchk.cr14,
430 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100431 rc = kvm_s390_vcpu_store_status(vcpu,
432 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens79882762014-01-02 10:59:41 +0100433 rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE);
434 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
435 &vcpu->arch.sie_block->gpsw,
436 sizeof(psw_t));
437 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100438 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100439 break;
440
Cornelia Huckd8346b72012-12-20 15:32:08 +0100441 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
442 {
443 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
444 inti->io.subchannel_nr;
445 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
446 inti->io.io_int_word;
447 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
448 vcpu->stat.deliver_io_int++;
449 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
450 param0, param1);
Heiko Carstens79882762014-01-02 10:59:41 +0100451 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
452 (u16 *)__LC_SUBCHANNEL_ID);
453 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
454 (u16 *)__LC_SUBCHANNEL_NR);
455 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
456 (u32 *)__LC_IO_INT_PARM);
457 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
458 (u32 *)__LC_IO_INT_WORD);
459 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
460 &vcpu->arch.sie_block->gpsw,
461 sizeof(psw_t));
462 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
463 &vcpu->arch.sie_block->gpsw,
464 sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100465 break;
466 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100467 default:
468 BUG();
469 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100470 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200471 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100472 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200473 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100474 }
475}
476
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100477static void deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100478{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100479 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100480
Jens Freimann1a03b7642014-02-12 14:05:38 +0100481 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
482 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
483 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
484 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
485 &vcpu->arch.sie_block->gpsw,
486 sizeof(psw_t));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100487 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200488 printk("kvm: The guest lowcore is not mapped during interrupt "
489 "delivery, killing userspace\n");
490 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100491 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100492}
493
Dominik Dingel3c038e62013-10-07 17:11:48 +0200494int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100495{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200496 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
497 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
498 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100499 int rc = 0;
500
501 if (atomic_read(&li->active)) {
502 spin_lock_bh(&li->lock);
503 list_for_each_entry(inti, &li->list, list)
504 if (__interrupt_is_deliverable(vcpu, inti)) {
505 rc = 1;
506 break;
507 }
508 spin_unlock_bh(&li->lock);
509 }
510
511 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200512 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100513 list_for_each_entry(inti, &fi->list, list)
514 if (__interrupt_is_deliverable(vcpu, inti)) {
515 rc = 1;
516 break;
517 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200518 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100519 }
520
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100521 if (!rc && kvm_cpu_has_pending_timer(vcpu))
522 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100523
524 return rc;
525}
526
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300527int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
528{
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100529 if (!(vcpu->arch.sie_block->ckc <
530 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
531 return 0;
532 if (!ckc_interrupts_enabled(vcpu))
533 return 0;
534 return 1;
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300535}
536
Carsten Otteba5c1e92008-03-25 18:47:26 +0100537int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
538{
539 u64 now, sltime;
540 DECLARE_WAITQUEUE(wait, current);
541
542 vcpu->stat.exit_wait_state++;
543 if (kvm_cpu_has_interrupt(vcpu))
544 return 0;
545
Carsten Ottee52b2af2008-05-21 13:37:44 +0200546 __set_cpu_idle(vcpu);
547 spin_lock_bh(&vcpu->arch.local_int.lock);
548 vcpu->arch.local_int.timer_due = 0;
549 spin_unlock_bh(&vcpu->arch.local_int.lock);
550
Carsten Otteba5c1e92008-03-25 18:47:26 +0100551 if (psw_interrupts_disabled(vcpu)) {
552 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
553 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100554 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100555 }
556
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100557 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100558 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
559 goto no_timer;
560 }
561
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200562 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100563 if (vcpu->arch.sie_block->ckc < now) {
564 __unset_cpu_idle(vcpu);
565 return 0;
566 }
567
Heiko Carstensed4f2092013-01-14 16:55:55 +0100568 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100569
Christian Borntraegerca872302009-05-12 17:21:49 +0200570 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
571 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100572no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200573 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200574 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100575 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200576 add_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100577 while (list_empty(&vcpu->arch.local_int.list) &&
578 list_empty(&vcpu->arch.local_int.float_int->list) &&
579 (!vcpu->arch.local_int.timer_due) &&
580 !signal_pending(current)) {
581 set_current_state(TASK_INTERRUPTIBLE);
582 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200583 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100584 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200585 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100586 spin_lock_bh(&vcpu->arch.local_int.lock);
587 }
588 __unset_cpu_idle(vcpu);
589 __set_current_state(TASK_RUNNING);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200590 remove_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100591 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200592 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Thomas Huth800c1062013-09-12 10:33:45 +0200593 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
594
Christian Borntraegerca872302009-05-12 17:21:49 +0200595 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100596 return 0;
597}
598
Christian Borntraegerca872302009-05-12 17:21:49 +0200599void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100600{
Christian Borntraegerca872302009-05-12 17:21:49 +0200601 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100602
Christian Borntraegerca872302009-05-12 17:21:49 +0200603 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100604 vcpu->arch.local_int.timer_due = 1;
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200605 if (waitqueue_active(&vcpu->wq))
606 wake_up_interruptible(&vcpu->wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200607 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100608}
609
Christian Borntraegerca872302009-05-12 17:21:49 +0200610/*
611 * low level hrtimer wake routine. Because this runs in hardirq context
612 * we schedule a tasklet to do the real work.
613 */
614enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
615{
616 struct kvm_vcpu *vcpu;
617
618 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100619 vcpu->preempted = true;
Christian Borntraegerca872302009-05-12 17:21:49 +0200620 tasklet_schedule(&vcpu->arch.tasklet);
621
622 return HRTIMER_NORESTART;
623}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100624
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100625void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
626{
627 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
628 struct kvm_s390_interrupt_info *n, *inti = NULL;
629
630 spin_lock_bh(&li->lock);
631 list_for_each_entry_safe(inti, n, &li->list, list) {
632 list_del(&inti->list);
633 kfree(inti);
634 }
635 atomic_set(&li->active, 0);
636 spin_unlock_bh(&li->lock);
637}
638
Carsten Otteba5c1e92008-03-25 18:47:26 +0100639void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
640{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200641 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
642 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
643 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100644 int deliver;
645
646 __reset_intercept_indicators(vcpu);
647 if (atomic_read(&li->active)) {
648 do {
649 deliver = 0;
650 spin_lock_bh(&li->lock);
651 list_for_each_entry_safe(inti, n, &li->list, list) {
652 if (__interrupt_is_deliverable(vcpu, inti)) {
653 list_del(&inti->list);
654 deliver = 1;
655 break;
656 }
657 __set_intercept_indicator(vcpu, inti);
658 }
659 if (list_empty(&li->list))
660 atomic_set(&li->active, 0);
661 spin_unlock_bh(&li->lock);
662 if (deliver) {
663 __do_deliver_interrupt(vcpu, inti);
664 kfree(inti);
665 }
666 } while (deliver);
667 }
668
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100669 if (kvm_cpu_has_pending_timer(vcpu))
670 deliver_ckc_interrupt(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100671
672 if (atomic_read(&fi->active)) {
673 do {
674 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200675 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100676 list_for_each_entry_safe(inti, n, &fi->list, list) {
677 if (__interrupt_is_deliverable(vcpu, inti)) {
678 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100679 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100680 deliver = 1;
681 break;
682 }
683 __set_intercept_indicator(vcpu, inti);
684 }
685 if (list_empty(&fi->list))
686 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200687 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100688 if (deliver) {
689 __do_deliver_interrupt(vcpu, inti);
690 kfree(inti);
691 }
692 } while (deliver);
693 }
694}
695
Cornelia Huck48a3e952012-12-20 15:32:09 +0100696void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
697{
698 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
699 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
700 struct kvm_s390_interrupt_info *n, *inti = NULL;
701 int deliver;
702
703 __reset_intercept_indicators(vcpu);
704 if (atomic_read(&li->active)) {
705 do {
706 deliver = 0;
707 spin_lock_bh(&li->lock);
708 list_for_each_entry_safe(inti, n, &li->list, list) {
709 if ((inti->type == KVM_S390_MCHK) &&
710 __interrupt_is_deliverable(vcpu, inti)) {
711 list_del(&inti->list);
712 deliver = 1;
713 break;
714 }
715 __set_intercept_indicator(vcpu, inti);
716 }
717 if (list_empty(&li->list))
718 atomic_set(&li->active, 0);
719 spin_unlock_bh(&li->lock);
720 if (deliver) {
721 __do_deliver_interrupt(vcpu, inti);
722 kfree(inti);
723 }
724 } while (deliver);
725 }
726
727 if (atomic_read(&fi->active)) {
728 do {
729 deliver = 0;
730 spin_lock(&fi->lock);
731 list_for_each_entry_safe(inti, n, &fi->list, list) {
732 if ((inti->type == KVM_S390_MCHK) &&
733 __interrupt_is_deliverable(vcpu, inti)) {
734 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100735 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100736 deliver = 1;
737 break;
738 }
739 __set_intercept_indicator(vcpu, inti);
740 }
741 if (list_empty(&fi->list))
742 atomic_set(&fi->active, 0);
743 spin_unlock(&fi->lock);
744 if (deliver) {
745 __do_deliver_interrupt(vcpu, inti);
746 kfree(inti);
747 }
748 } while (deliver);
749 }
750}
751
Carsten Otteba5c1e92008-03-25 18:47:26 +0100752int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
753{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200754 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
755 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100756
757 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
758 if (!inti)
759 return -ENOMEM;
760
Joe Perchesa419aef2009-08-18 11:18:35 -0700761 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100762 inti->pgm.code = code;
763
764 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200765 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100766 spin_lock_bh(&li->lock);
767 list_add(&inti->list, &li->list);
768 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200769 BUG_ON(waitqueue_active(li->wq));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100770 spin_unlock_bh(&li->lock);
771 return 0;
772}
773
Jens Freimannbcd84682014-02-11 11:07:05 +0100774int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
775 struct kvm_s390_pgm_info *pgm_info)
776{
777 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
778 struct kvm_s390_interrupt_info *inti;
779
780 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
781 if (!inti)
782 return -ENOMEM;
783
784 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
785 pgm_info->code);
786 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
787 pgm_info->code, 0, 1);
788
789 inti->type = KVM_S390_PROGRAM_INT;
790 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
791 spin_lock_bh(&li->lock);
792 list_add(&inti->list, &li->list);
793 atomic_set(&li->active, 1);
794 BUG_ON(waitqueue_active(li->wq));
795 spin_unlock_bh(&li->lock);
796 return 0;
797}
798
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100799struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
800 u64 cr6, u64 schid)
801{
802 struct kvm_s390_float_interrupt *fi;
803 struct kvm_s390_interrupt_info *inti, *iter;
804
805 if ((!schid && !cr6) || (schid && cr6))
806 return NULL;
807 mutex_lock(&kvm->lock);
808 fi = &kvm->arch.float_int;
809 spin_lock(&fi->lock);
810 inti = NULL;
811 list_for_each_entry(iter, &fi->list, list) {
812 if (!is_ioint(iter->type))
813 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100814 if (cr6 &&
815 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100816 continue;
817 if (schid) {
818 if (((schid & 0x00000000ffff0000) >> 16) !=
819 iter->io.subchannel_id)
820 continue;
821 if ((schid & 0x000000000000ffff) !=
822 iter->io.subchannel_nr)
823 continue;
824 }
825 inti = iter;
826 break;
827 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100828 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100829 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100830 fi->irq_count--;
831 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100832 if (list_empty(&fi->list))
833 atomic_set(&fi->active, 0);
834 spin_unlock(&fi->lock);
835 mutex_unlock(&kvm->lock);
836 return inti;
837}
838
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100839static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100840{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200841 struct kvm_s390_local_interrupt *li;
842 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200843 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100844 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100845 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100846 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100847
Carsten Otteba5c1e92008-03-25 18:47:26 +0100848 mutex_lock(&kvm->lock);
849 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200850 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100851 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
852 rc = -EINVAL;
853 goto unlock_fi;
854 }
855 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200856 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100857 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200858 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100859 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
860
Cornelia Huckd8346b72012-12-20 15:32:08 +0100861 /* Keep I/O interrupts sorted in isc order. */
862 list_for_each_entry(iter, &fi->list, list) {
863 if (!is_ioint(iter->type))
864 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100865 if (int_word_to_isc_bits(iter->io.io_int_word)
866 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100867 continue;
868 break;
869 }
870 list_add_tail(&inti->list, &iter->list);
871 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100872 atomic_set(&fi->active, 1);
873 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
874 if (sigcpu == KVM_MAX_VCPUS) {
875 do {
876 sigcpu = fi->next_rr_cpu++;
877 if (sigcpu == KVM_MAX_VCPUS)
878 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100879 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100880 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100881 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
882 li = &dst_vcpu->arch.local_int;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100883 spin_lock_bh(&li->lock);
884 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200885 if (waitqueue_active(li->wq))
886 wake_up_interruptible(li->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100887 kvm_get_vcpu(kvm, sigcpu)->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100888 spin_unlock_bh(&li->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100889unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200890 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100891 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100892 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200893}
894
895int kvm_s390_inject_vm(struct kvm *kvm,
896 struct kvm_s390_interrupt *s390int)
897{
898 struct kvm_s390_interrupt_info *inti;
899
900 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
901 if (!inti)
902 return -ENOMEM;
903
904 inti->type = s390int->type;
905 switch (inti->type) {
906 case KVM_S390_INT_VIRTIO:
907 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
908 s390int->parm, s390int->parm64);
909 inti->ext.ext_params = s390int->parm;
910 inti->ext.ext_params2 = s390int->parm64;
911 break;
912 case KVM_S390_INT_SERVICE:
913 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
914 inti->ext.ext_params = s390int->parm;
915 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200916 case KVM_S390_INT_PFAULT_DONE:
917 inti->type = s390int->type;
918 inti->ext.ext_params2 = s390int->parm64;
919 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200920 case KVM_S390_MCHK:
921 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
922 s390int->parm64);
923 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
924 inti->mchk.mcic = s390int->parm64;
925 break;
926 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
927 if (inti->type & IOINT_AI_MASK)
928 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
929 else
930 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
931 s390int->type & IOINT_CSSID_MASK,
932 s390int->type & IOINT_SSID_MASK,
933 s390int->type & IOINT_SCHID_MASK);
934 inti->io.subchannel_id = s390int->parm >> 16;
935 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
936 inti->io.io_int_parm = s390int->parm64 >> 32;
937 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
938 break;
939 default:
940 kfree(inti);
941 return -EINVAL;
942 }
943 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
944 2);
945
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100946 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100947}
948
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100949void kvm_s390_reinject_io_int(struct kvm *kvm,
950 struct kvm_s390_interrupt_info *inti)
951{
952 __inject_vm(kvm, inti);
953}
954
Carsten Otteba5c1e92008-03-25 18:47:26 +0100955int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
956 struct kvm_s390_interrupt *s390int)
957{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200958 struct kvm_s390_local_interrupt *li;
959 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100960
961 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
962 if (!inti)
963 return -ENOMEM;
964
965 switch (s390int->type) {
966 case KVM_S390_PROGRAM_INT:
967 if (s390int->parm & 0xffff0000) {
968 kfree(inti);
969 return -EINVAL;
970 }
971 inti->type = s390int->type;
972 inti->pgm.code = s390int->parm;
973 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
974 s390int->parm);
975 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100976 case KVM_S390_SIGP_SET_PREFIX:
977 inti->prefix.address = s390int->parm;
978 inti->type = s390int->type;
979 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
980 s390int->parm);
981 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100982 case KVM_S390_SIGP_STOP:
983 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100984 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
985 inti->type = s390int->type;
986 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200987 case KVM_S390_INT_EXTERNAL_CALL:
988 if (s390int->parm & 0xffff0000) {
989 kfree(inti);
990 return -EINVAL;
991 }
992 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
993 s390int->parm);
994 inti->type = s390int->type;
995 inti->extcall.code = s390int->parm;
996 break;
997 case KVM_S390_INT_EMERGENCY:
998 if (s390int->parm & 0xffff0000) {
999 kfree(inti);
1000 return -EINVAL;
1001 }
1002 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
1003 inti->type = s390int->type;
1004 inti->emerg.code = s390int->parm;
1005 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001006 case KVM_S390_MCHK:
1007 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1008 s390int->parm64);
1009 inti->type = s390int->type;
1010 inti->mchk.mcic = s390int->parm64;
1011 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001012 case KVM_S390_INT_PFAULT_INIT:
1013 inti->type = s390int->type;
1014 inti->ext.ext_params2 = s390int->parm64;
1015 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001016 case KVM_S390_INT_VIRTIO:
1017 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001018 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001019 default:
1020 kfree(inti);
1021 return -EINVAL;
1022 }
Cornelia Huckade38c32012-07-23 17:20:30 +02001023 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
1024 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001025
1026 mutex_lock(&vcpu->kvm->lock);
1027 li = &vcpu->arch.local_int;
1028 spin_lock_bh(&li->lock);
1029 if (inti->type == KVM_S390_PROGRAM_INT)
1030 list_add(&inti->list, &li->list);
1031 else
1032 list_add_tail(&inti->list, &li->list);
1033 atomic_set(&li->active, 1);
1034 if (inti->type == KVM_S390_SIGP_STOP)
1035 li->action_bits |= ACTION_STOP_ON_STOP;
1036 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001037 if (waitqueue_active(&vcpu->wq))
1038 wake_up_interruptible(&vcpu->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +01001039 vcpu->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001040 spin_unlock_bh(&li->lock);
1041 mutex_unlock(&vcpu->kvm->lock);
1042 return 0;
1043}
Jens Freimannc05c4182013-10-07 16:13:45 +02001044
1045static void clear_floating_interrupts(struct kvm *kvm)
1046{
1047 struct kvm_s390_float_interrupt *fi;
1048 struct kvm_s390_interrupt_info *n, *inti = NULL;
1049
1050 mutex_lock(&kvm->lock);
1051 fi = &kvm->arch.float_int;
1052 spin_lock(&fi->lock);
1053 list_for_each_entry_safe(inti, n, &fi->list, list) {
1054 list_del(&inti->list);
1055 kfree(inti);
1056 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001057 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001058 atomic_set(&fi->active, 0);
1059 spin_unlock(&fi->lock);
1060 mutex_unlock(&kvm->lock);
1061}
1062
1063static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1064 u8 *addr)
1065{
1066 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1067 struct kvm_s390_irq irq = {0};
1068
1069 irq.type = inti->type;
1070 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001071 case KVM_S390_INT_PFAULT_INIT:
1072 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001073 case KVM_S390_INT_VIRTIO:
1074 case KVM_S390_INT_SERVICE:
1075 irq.u.ext = inti->ext;
1076 break;
1077 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1078 irq.u.io = inti->io;
1079 break;
1080 case KVM_S390_MCHK:
1081 irq.u.mchk = inti->mchk;
1082 break;
1083 default:
1084 return -EINVAL;
1085 }
1086
1087 if (copy_to_user(uptr, &irq, sizeof(irq)))
1088 return -EFAULT;
1089
1090 return 0;
1091}
1092
1093static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1094{
1095 struct kvm_s390_interrupt_info *inti;
1096 struct kvm_s390_float_interrupt *fi;
1097 int ret = 0;
1098 int n = 0;
1099
1100 mutex_lock(&kvm->lock);
1101 fi = &kvm->arch.float_int;
1102 spin_lock(&fi->lock);
1103
1104 list_for_each_entry(inti, &fi->list, list) {
1105 if (len < sizeof(struct kvm_s390_irq)) {
1106 /* signal userspace to try again */
1107 ret = -ENOMEM;
1108 break;
1109 }
1110 ret = copy_irq_to_user(inti, buf);
1111 if (ret)
1112 break;
1113 buf += sizeof(struct kvm_s390_irq);
1114 len -= sizeof(struct kvm_s390_irq);
1115 n++;
1116 }
1117
1118 spin_unlock(&fi->lock);
1119 mutex_unlock(&kvm->lock);
1120
1121 return ret < 0 ? ret : n;
1122}
1123
1124static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1125{
1126 int r;
1127
1128 switch (attr->group) {
1129 case KVM_DEV_FLIC_GET_ALL_IRQS:
1130 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1131 attr->attr);
1132 break;
1133 default:
1134 r = -EINVAL;
1135 }
1136
1137 return r;
1138}
1139
1140static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1141 u64 addr)
1142{
1143 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1144 void *target = NULL;
1145 void __user *source;
1146 u64 size;
1147
1148 if (get_user(inti->type, (u64 __user *)addr))
1149 return -EFAULT;
1150
1151 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001152 case KVM_S390_INT_PFAULT_INIT:
1153 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001154 case KVM_S390_INT_VIRTIO:
1155 case KVM_S390_INT_SERVICE:
1156 target = (void *) &inti->ext;
1157 source = &uptr->u.ext;
1158 size = sizeof(inti->ext);
1159 break;
1160 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1161 target = (void *) &inti->io;
1162 source = &uptr->u.io;
1163 size = sizeof(inti->io);
1164 break;
1165 case KVM_S390_MCHK:
1166 target = (void *) &inti->mchk;
1167 source = &uptr->u.mchk;
1168 size = sizeof(inti->mchk);
1169 break;
1170 default:
1171 return -EINVAL;
1172 }
1173
1174 if (copy_from_user(target, source, size))
1175 return -EFAULT;
1176
1177 return 0;
1178}
1179
1180static int enqueue_floating_irq(struct kvm_device *dev,
1181 struct kvm_device_attr *attr)
1182{
1183 struct kvm_s390_interrupt_info *inti = NULL;
1184 int r = 0;
1185 int len = attr->attr;
1186
1187 if (len % sizeof(struct kvm_s390_irq) != 0)
1188 return -EINVAL;
1189 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1190 return -EINVAL;
1191
1192 while (len >= sizeof(struct kvm_s390_irq)) {
1193 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1194 if (!inti)
1195 return -ENOMEM;
1196
1197 r = copy_irq_from_user(inti, attr->addr);
1198 if (r) {
1199 kfree(inti);
1200 return r;
1201 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001202 r = __inject_vm(dev->kvm, inti);
1203 if (r) {
1204 kfree(inti);
1205 return r;
1206 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001207 len -= sizeof(struct kvm_s390_irq);
1208 attr->addr += sizeof(struct kvm_s390_irq);
1209 }
1210
1211 return r;
1212}
1213
Cornelia Huck841b91c2013-07-15 13:36:01 +02001214static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1215{
1216 if (id >= MAX_S390_IO_ADAPTERS)
1217 return NULL;
1218 return kvm->arch.adapters[id];
1219}
1220
1221static int register_io_adapter(struct kvm_device *dev,
1222 struct kvm_device_attr *attr)
1223{
1224 struct s390_io_adapter *adapter;
1225 struct kvm_s390_io_adapter adapter_info;
1226
1227 if (copy_from_user(&adapter_info,
1228 (void __user *)attr->addr, sizeof(adapter_info)))
1229 return -EFAULT;
1230
1231 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1232 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1233 return -EINVAL;
1234
1235 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1236 if (!adapter)
1237 return -ENOMEM;
1238
1239 INIT_LIST_HEAD(&adapter->maps);
1240 init_rwsem(&adapter->maps_lock);
1241 atomic_set(&adapter->nr_maps, 0);
1242 adapter->id = adapter_info.id;
1243 adapter->isc = adapter_info.isc;
1244 adapter->maskable = adapter_info.maskable;
1245 adapter->masked = false;
1246 adapter->swap = adapter_info.swap;
1247 dev->kvm->arch.adapters[adapter->id] = adapter;
1248
1249 return 0;
1250}
1251
1252int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1253{
1254 int ret;
1255 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1256
1257 if (!adapter || !adapter->maskable)
1258 return -EINVAL;
1259 ret = adapter->masked;
1260 adapter->masked = masked;
1261 return ret;
1262}
1263
1264static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1265{
1266 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1267 struct s390_map_info *map;
1268 int ret;
1269
1270 if (!adapter || !addr)
1271 return -EINVAL;
1272
1273 map = kzalloc(sizeof(*map), GFP_KERNEL);
1274 if (!map) {
1275 ret = -ENOMEM;
1276 goto out;
1277 }
1278 INIT_LIST_HEAD(&map->list);
1279 map->guest_addr = addr;
1280 map->addr = gmap_translate(addr, kvm->arch.gmap);
1281 if (map->addr == -EFAULT) {
1282 ret = -EFAULT;
1283 goto out;
1284 }
1285 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1286 if (ret < 0)
1287 goto out;
1288 BUG_ON(ret != 1);
1289 down_write(&adapter->maps_lock);
1290 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1291 list_add_tail(&map->list, &adapter->maps);
1292 ret = 0;
1293 } else {
1294 put_page(map->page);
1295 ret = -EINVAL;
1296 }
1297 up_write(&adapter->maps_lock);
1298out:
1299 if (ret)
1300 kfree(map);
1301 return ret;
1302}
1303
1304static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1305{
1306 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1307 struct s390_map_info *map, *tmp;
1308 int found = 0;
1309
1310 if (!adapter || !addr)
1311 return -EINVAL;
1312
1313 down_write(&adapter->maps_lock);
1314 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1315 if (map->guest_addr == addr) {
1316 found = 1;
1317 atomic_dec(&adapter->nr_maps);
1318 list_del(&map->list);
1319 put_page(map->page);
1320 kfree(map);
1321 break;
1322 }
1323 }
1324 up_write(&adapter->maps_lock);
1325
1326 return found ? 0 : -EINVAL;
1327}
1328
1329void kvm_s390_destroy_adapters(struct kvm *kvm)
1330{
1331 int i;
1332 struct s390_map_info *map, *tmp;
1333
1334 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1335 if (!kvm->arch.adapters[i])
1336 continue;
1337 list_for_each_entry_safe(map, tmp,
1338 &kvm->arch.adapters[i]->maps, list) {
1339 list_del(&map->list);
1340 put_page(map->page);
1341 kfree(map);
1342 }
1343 kfree(kvm->arch.adapters[i]);
1344 }
1345}
1346
1347static int modify_io_adapter(struct kvm_device *dev,
1348 struct kvm_device_attr *attr)
1349{
1350 struct kvm_s390_io_adapter_req req;
1351 struct s390_io_adapter *adapter;
1352 int ret;
1353
1354 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1355 return -EFAULT;
1356
1357 adapter = get_io_adapter(dev->kvm, req.id);
1358 if (!adapter)
1359 return -EINVAL;
1360 switch (req.type) {
1361 case KVM_S390_IO_ADAPTER_MASK:
1362 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1363 if (ret > 0)
1364 ret = 0;
1365 break;
1366 case KVM_S390_IO_ADAPTER_MAP:
1367 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1368 break;
1369 case KVM_S390_IO_ADAPTER_UNMAP:
1370 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1371 break;
1372 default:
1373 ret = -EINVAL;
1374 }
1375
1376 return ret;
1377}
1378
Jens Freimannc05c4182013-10-07 16:13:45 +02001379static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1380{
1381 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001382 unsigned int i;
1383 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001384
1385 switch (attr->group) {
1386 case KVM_DEV_FLIC_ENQUEUE:
1387 r = enqueue_floating_irq(dev, attr);
1388 break;
1389 case KVM_DEV_FLIC_CLEAR_IRQS:
1390 r = 0;
1391 clear_floating_interrupts(dev->kvm);
1392 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001393 case KVM_DEV_FLIC_APF_ENABLE:
1394 dev->kvm->arch.gmap->pfault_enabled = 1;
1395 break;
1396 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1397 dev->kvm->arch.gmap->pfault_enabled = 0;
1398 /*
1399 * Make sure no async faults are in transition when
1400 * clearing the queues. So we don't need to worry
1401 * about late coming workers.
1402 */
1403 synchronize_srcu(&dev->kvm->srcu);
1404 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1405 kvm_clear_async_pf_completion_queue(vcpu);
1406 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001407 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1408 r = register_io_adapter(dev, attr);
1409 break;
1410 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1411 r = modify_io_adapter(dev, attr);
1412 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001413 default:
1414 r = -EINVAL;
1415 }
1416
1417 return r;
1418}
1419
1420static int flic_create(struct kvm_device *dev, u32 type)
1421{
1422 if (!dev)
1423 return -EINVAL;
1424 if (dev->kvm->arch.flic)
1425 return -EINVAL;
1426 dev->kvm->arch.flic = dev;
1427 return 0;
1428}
1429
1430static void flic_destroy(struct kvm_device *dev)
1431{
1432 dev->kvm->arch.flic = NULL;
1433 kfree(dev);
1434}
1435
1436/* s390 floating irq controller (flic) */
1437struct kvm_device_ops kvm_flic_ops = {
1438 .name = "kvm-flic",
1439 .get_attr = flic_get_attr,
1440 .set_attr = flic_set_attr,
1441 .create = flic_create,
1442 .destroy = flic_destroy,
1443};
Cornelia Huck84223592013-07-15 13:36:01 +02001444
1445static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1446{
1447 unsigned long bit;
1448
1449 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1450
1451 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1452}
1453
1454static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1455 u64 addr)
1456{
1457 struct s390_map_info *map;
1458
1459 if (!adapter)
1460 return NULL;
1461
1462 list_for_each_entry(map, &adapter->maps, list) {
1463 if (map->guest_addr == addr)
1464 return map;
1465 }
1466 return NULL;
1467}
1468
1469static int adapter_indicators_set(struct kvm *kvm,
1470 struct s390_io_adapter *adapter,
1471 struct kvm_s390_adapter_int *adapter_int)
1472{
1473 unsigned long bit;
1474 int summary_set, idx;
1475 struct s390_map_info *info;
1476 void *map;
1477
1478 info = get_map_info(adapter, adapter_int->ind_addr);
1479 if (!info)
1480 return -1;
1481 map = page_address(info->page);
1482 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1483 set_bit(bit, map);
1484 idx = srcu_read_lock(&kvm->srcu);
1485 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1486 set_page_dirty_lock(info->page);
1487 info = get_map_info(adapter, adapter_int->summary_addr);
1488 if (!info) {
1489 srcu_read_unlock(&kvm->srcu, idx);
1490 return -1;
1491 }
1492 map = page_address(info->page);
1493 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1494 adapter->swap);
1495 summary_set = test_and_set_bit(bit, map);
1496 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1497 set_page_dirty_lock(info->page);
1498 srcu_read_unlock(&kvm->srcu, idx);
1499 return summary_set ? 0 : 1;
1500}
1501
1502/*
1503 * < 0 - not injected due to error
1504 * = 0 - coalesced, summary indicator already active
1505 * > 0 - injected interrupt
1506 */
1507static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1508 struct kvm *kvm, int irq_source_id, int level,
1509 bool line_status)
1510{
1511 int ret;
1512 struct s390_io_adapter *adapter;
1513
1514 /* We're only interested in the 0->1 transition. */
1515 if (!level)
1516 return 0;
1517 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1518 if (!adapter)
1519 return -1;
1520 down_read(&adapter->maps_lock);
1521 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1522 up_read(&adapter->maps_lock);
1523 if ((ret > 0) && !adapter->masked) {
1524 struct kvm_s390_interrupt s390int = {
1525 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1526 .parm = 0,
1527 .parm64 = (adapter->isc << 27) | 0x80000000,
1528 };
1529 ret = kvm_s390_inject_vm(kvm, &s390int);
1530 if (ret == 0)
1531 ret = 1;
1532 }
1533 return ret;
1534}
1535
1536int kvm_set_routing_entry(struct kvm_irq_routing_table *rt,
1537 struct kvm_kernel_irq_routing_entry *e,
1538 const struct kvm_irq_routing_entry *ue)
1539{
1540 int ret;
1541
1542 switch (ue->type) {
1543 case KVM_IRQ_ROUTING_S390_ADAPTER:
1544 e->set = set_adapter_int;
1545 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1546 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1547 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1548 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1549 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1550 ret = 0;
1551 break;
1552 default:
1553 ret = -EINVAL;
1554 }
1555
1556 return ret;
1557}
1558
1559int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1560 int irq_source_id, int level, bool line_status)
1561{
1562 return -EINVAL;
1563}