blob: e2f6240584574e7a668d8ac97ae16f21e3758afd [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
Jens Freimann44c6ca32014-04-16 13:57:18 +020029#define PFAULT_INIT 0x0600
Cornelia Huckd8346b72012-12-20 15:32:08 +010030
Jens Freimann79395032014-04-17 10:10:30 +020031static int deliver_ckc_interrupt(struct kvm_vcpu *vcpu);
Thomas Huthe029ae52014-03-26 16:11:54 +010032
Cornelia Huckd8346b72012-12-20 15:32:08 +010033static int is_ioint(u64 type)
34{
35 return ((type & 0xfffe0000u) != 0xfffe0000u);
36}
37
Dominik Dingel3c038e62013-10-07 17:11:48 +020038int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010039{
40 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
41}
42
Cornelia Huckd8346b72012-12-20 15:32:08 +010043static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
44{
45 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
46}
47
Cornelia Huck48a3e952012-12-20 15:32:09 +010048static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
49{
50 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
51}
52
Carsten Otteba5c1e92008-03-25 18:47:26 +010053static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
54{
55 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
56 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
57 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
58 return 0;
59 return 1;
60}
61
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010062static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
63{
64 if (psw_extint_disabled(vcpu) ||
65 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
66 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +010067 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
68 /* No timer interrupts when single stepping */
69 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010070 return 1;
71}
72
Cornelia Huck79fd50c2013-02-07 13:20:52 +010073static u64 int_word_to_isc_bits(u32 int_word)
74{
75 u8 isc = (int_word & 0x38000000) >> 27;
76
77 return (0x80 >> isc) << 24;
78}
79
Carsten Otteba5c1e92008-03-25 18:47:26 +010080static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020081 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010082{
83 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020084 case KVM_S390_INT_EXTERNAL_CALL:
85 if (psw_extint_disabled(vcpu))
86 return 0;
87 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
88 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +010089 case KVM_S390_INT_EMERGENCY:
90 if (psw_extint_disabled(vcpu))
91 return 0;
92 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
93 return 1;
94 return 0;
Thomas Huthe029ae52014-03-26 16:11:54 +010095 case KVM_S390_INT_CLOCK_COMP:
96 return ckc_interrupts_enabled(vcpu);
97 case KVM_S390_INT_CPU_TIMER:
98 if (psw_extint_disabled(vcpu))
99 return 0;
100 if (vcpu->arch.sie_block->gcr[0] & 0x400ul)
101 return 1;
102 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100103 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200104 case KVM_S390_INT_PFAULT_INIT:
105 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100106 case KVM_S390_INT_VIRTIO:
107 if (psw_extint_disabled(vcpu))
108 return 0;
109 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
110 return 1;
111 return 0;
112 case KVM_S390_PROGRAM_INT:
113 case KVM_S390_SIGP_STOP:
114 case KVM_S390_SIGP_SET_PREFIX:
115 case KVM_S390_RESTART:
116 return 1;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100117 case KVM_S390_MCHK:
118 if (psw_mchk_disabled(vcpu))
119 return 0;
120 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
121 return 1;
122 return 0;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100123 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
124 if (psw_ioint_disabled(vcpu))
125 return 0;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100126 if (vcpu->arch.sie_block->gcr[6] &
127 int_word_to_isc_bits(inti->io.io_int_word))
Cornelia Huckd8346b72012-12-20 15:32:08 +0100128 return 1;
129 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100130 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100131 printk(KERN_WARNING "illegal interrupt type %llx\n",
132 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100133 BUG();
134 }
135 return 0;
136}
137
138static void __set_cpu_idle(struct kvm_vcpu *vcpu)
139{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100140 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
141 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
142}
143
144static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
145{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100146 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
147 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
148}
149
150static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
151{
David Hildenbrand49539192014-02-21 08:59:59 +0100152 atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
153 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100154 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100155 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
156
157 if (guestdbg_enabled(vcpu)) {
158 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
159 LCTL_CR10 | LCTL_CR11);
160 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
161 }
David Hildenbranddb373862014-07-28 14:05:41 +0200162
163 if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP)
164 atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100165}
166
167static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
168{
169 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
170}
171
172static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200173 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100174{
175 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200176 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100177 case KVM_S390_INT_EMERGENCY:
178 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200179 case KVM_S390_INT_PFAULT_INIT:
180 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100181 case KVM_S390_INT_VIRTIO:
Thomas Huthe029ae52014-03-26 16:11:54 +0100182 case KVM_S390_INT_CLOCK_COMP:
183 case KVM_S390_INT_CPU_TIMER:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100184 if (psw_extint_disabled(vcpu))
185 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
186 else
187 vcpu->arch.sie_block->lctl |= LCTL_CR0;
188 break;
189 case KVM_S390_SIGP_STOP:
190 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
191 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100192 case KVM_S390_MCHK:
193 if (psw_mchk_disabled(vcpu))
194 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
195 else
196 vcpu->arch.sie_block->lctl |= LCTL_CR14;
197 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100198 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
199 if (psw_ioint_disabled(vcpu))
200 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
201 else
202 vcpu->arch.sie_block->lctl |= LCTL_CR6;
203 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100204 default:
205 BUG();
206 }
207}
208
Jens Freimann8a2ef712014-07-23 16:36:06 +0200209static u16 get_ilc(struct kvm_vcpu *vcpu)
210{
211 const unsigned short table[] = { 2, 4, 4, 6 };
212
213 switch (vcpu->arch.sie_block->icptcode) {
214 case ICPT_INST:
215 case ICPT_INSTPROGI:
216 case ICPT_OPEREXC:
217 case ICPT_PARTEXEC:
218 case ICPT_IOINST:
219 /* last instruction only stored for these icptcodes */
220 return table[vcpu->arch.sie_block->ipa >> 14];
221 case ICPT_PROGI:
222 return vcpu->arch.sie_block->pgmilc;
223 default:
224 return 0;
225 }
226}
227
David Hildenbrand87128362014-03-03 10:55:13 +0100228static int __deliver_prog_irq(struct kvm_vcpu *vcpu,
229 struct kvm_s390_pgm_info *pgm_info)
230{
David Hildenbrand87128362014-03-03 10:55:13 +0100231 int rc = 0;
Jens Freimann8a2ef712014-07-23 16:36:06 +0200232 u16 ilc = get_ilc(vcpu);
David Hildenbrand87128362014-03-03 10:55:13 +0100233
234 switch (pgm_info->code & ~PGM_PER) {
235 case PGM_AFX_TRANSLATION:
236 case PGM_ASX_TRANSLATION:
237 case PGM_EX_TRANSLATION:
238 case PGM_LFX_TRANSLATION:
239 case PGM_LSTE_SEQUENCE:
240 case PGM_LSX_TRANSLATION:
241 case PGM_LX_TRANSLATION:
242 case PGM_PRIMARY_AUTHORITY:
243 case PGM_SECONDARY_AUTHORITY:
244 case PGM_SPACE_SWITCH:
245 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
246 (u64 *)__LC_TRANS_EXC_CODE);
247 break;
248 case PGM_ALEN_TRANSLATION:
249 case PGM_ALE_SEQUENCE:
250 case PGM_ASTE_INSTANCE:
251 case PGM_ASTE_SEQUENCE:
252 case PGM_ASTE_VALIDITY:
253 case PGM_EXTENDED_AUTHORITY:
254 rc = put_guest_lc(vcpu, pgm_info->exc_access_id,
255 (u8 *)__LC_EXC_ACCESS_ID);
256 break;
257 case PGM_ASCE_TYPE:
258 case PGM_PAGE_TRANSLATION:
259 case PGM_REGION_FIRST_TRANS:
260 case PGM_REGION_SECOND_TRANS:
261 case PGM_REGION_THIRD_TRANS:
262 case PGM_SEGMENT_TRANSLATION:
263 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
264 (u64 *)__LC_TRANS_EXC_CODE);
265 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
266 (u8 *)__LC_EXC_ACCESS_ID);
267 rc |= put_guest_lc(vcpu, pgm_info->op_access_id,
268 (u8 *)__LC_OP_ACCESS_ID);
269 break;
270 case PGM_MONITOR:
271 rc = put_guest_lc(vcpu, pgm_info->mon_class_nr,
272 (u64 *)__LC_MON_CLASS_NR);
273 rc |= put_guest_lc(vcpu, pgm_info->mon_code,
274 (u64 *)__LC_MON_CODE);
275 break;
276 case PGM_DATA:
277 rc = put_guest_lc(vcpu, pgm_info->data_exc_code,
278 (u32 *)__LC_DATA_EXC_CODE);
279 break;
280 case PGM_PROTECTION:
281 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
282 (u64 *)__LC_TRANS_EXC_CODE);
283 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
284 (u8 *)__LC_EXC_ACCESS_ID);
285 break;
286 }
287
288 if (pgm_info->code & PGM_PER) {
289 rc |= put_guest_lc(vcpu, pgm_info->per_code,
290 (u8 *) __LC_PER_CODE);
291 rc |= put_guest_lc(vcpu, pgm_info->per_atmid,
292 (u8 *)__LC_PER_ATMID);
293 rc |= put_guest_lc(vcpu, pgm_info->per_address,
294 (u64 *) __LC_PER_ADDRESS);
295 rc |= put_guest_lc(vcpu, pgm_info->per_access_id,
296 (u8 *) __LC_PER_ACCESS_ID);
297 }
298
Jens Freimann8a2ef712014-07-23 16:36:06 +0200299 rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
David Hildenbrand87128362014-03-03 10:55:13 +0100300 rc |= put_guest_lc(vcpu, pgm_info->code,
301 (u16 *)__LC_PGM_INT_CODE);
302 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
303 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
304 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
305 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
306
307 return rc;
308}
309
Jens Freimann79395032014-04-17 10:10:30 +0200310static int __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200311 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100312{
313 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100314 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100315
316 switch (inti->type) {
317 case KVM_S390_INT_EMERGENCY:
318 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
319 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200320 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
321 inti->emerg.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100322 rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE);
323 rc |= put_guest_lc(vcpu, inti->emerg.code,
324 (u16 *)__LC_EXT_CPU_ADDR);
325 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
326 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
327 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100328 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100329 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200330 case KVM_S390_INT_EXTERNAL_CALL:
331 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
332 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200333 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
334 inti->extcall.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100335 rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE);
336 rc |= put_guest_lc(vcpu, inti->extcall.code,
337 (u16 *)__LC_EXT_CPU_ADDR);
338 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
339 &vcpu->arch.sie_block->gpsw,
340 sizeof(psw_t));
341 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
342 &vcpu->arch.sie_block->gpsw,
343 sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200344 break;
Thomas Huthe029ae52014-03-26 16:11:54 +0100345 case KVM_S390_INT_CLOCK_COMP:
346 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
347 inti->ext.ext_params, 0);
Jens Freimann79395032014-04-17 10:10:30 +0200348 rc = deliver_ckc_interrupt(vcpu);
Thomas Huthe029ae52014-03-26 16:11:54 +0100349 break;
350 case KVM_S390_INT_CPU_TIMER:
351 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
352 inti->ext.ext_params, 0);
353 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
354 (u16 *)__LC_EXT_INT_CODE);
355 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
356 &vcpu->arch.sie_block->gpsw,
357 sizeof(psw_t));
358 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
359 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
360 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
361 (u32 *)__LC_EXT_PARAMS);
362 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100363 case KVM_S390_INT_SERVICE:
364 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
365 inti->ext.ext_params);
366 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200367 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
368 inti->ext.ext_params, 0);
Heiko Carstens79882762014-01-02 10:59:41 +0100369 rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE);
370 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
371 &vcpu->arch.sie_block->gpsw,
372 sizeof(psw_t));
373 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100374 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100375 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
376 (u32 *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100377 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200378 case KVM_S390_INT_PFAULT_INIT:
379 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
380 inti->ext.ext_params2);
Jens Freimann44c6ca32014-04-16 13:57:18 +0200381 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
382 (u16 *) __LC_EXT_INT_CODE);
383 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100384 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
385 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
386 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200387 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann1a03b7642014-02-12 14:05:38 +0100388 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
389 (u64 *) __LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200390 break;
391 case KVM_S390_INT_PFAULT_DONE:
392 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
393 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100394 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
395 rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR);
396 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
397 &vcpu->arch.sie_block->gpsw,
398 sizeof(psw_t));
399 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200400 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100401 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
402 (u64 *)__LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200403 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100404 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100405 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100406 inti->ext.ext_params, inti->ext.ext_params2);
407 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200408 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
409 inti->ext.ext_params,
410 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100411 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
412 rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR);
413 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
414 &vcpu->arch.sie_block->gpsw,
415 sizeof(psw_t));
416 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100417 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100418 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
419 (u32 *)__LC_EXT_PARAMS);
420 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
421 (u64 *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100422 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100423 case KVM_S390_SIGP_STOP:
424 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
425 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200426 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
427 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100428 __set_intercept_indicator(vcpu, inti);
429 break;
430
431 case KVM_S390_SIGP_SET_PREFIX:
432 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
433 inti->prefix.address);
434 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200435 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
436 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100437 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100438 break;
439
440 case KVM_S390_RESTART:
441 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
442 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200443 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
444 0, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100445 rc = write_guest_lc(vcpu,
446 offsetof(struct _lowcore, restart_old_psw),
447 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
448 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
449 &vcpu->arch.sie_block->gpsw,
450 sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100451 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100452 case KVM_S390_PROGRAM_INT:
453 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
454 inti->pgm.code,
455 table[vcpu->arch.sie_block->ipa >> 14]);
456 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200457 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
458 inti->pgm.code, 0);
David Hildenbrand87128362014-03-03 10:55:13 +0100459 rc = __deliver_prog_irq(vcpu, &inti->pgm);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100460 break;
461
Cornelia Huck48a3e952012-12-20 15:32:09 +0100462 case KVM_S390_MCHK:
463 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
464 inti->mchk.mcic);
465 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
466 inti->mchk.cr14,
467 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100468 rc = kvm_s390_vcpu_store_status(vcpu,
469 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens79882762014-01-02 10:59:41 +0100470 rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE);
471 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
472 &vcpu->arch.sie_block->gpsw,
473 sizeof(psw_t));
474 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100475 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100476 break;
477
Cornelia Huckd8346b72012-12-20 15:32:08 +0100478 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
479 {
480 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
481 inti->io.subchannel_nr;
482 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
483 inti->io.io_int_word;
484 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
485 vcpu->stat.deliver_io_int++;
486 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
487 param0, param1);
Heiko Carstens79882762014-01-02 10:59:41 +0100488 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
489 (u16 *)__LC_SUBCHANNEL_ID);
490 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
491 (u16 *)__LC_SUBCHANNEL_NR);
492 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
493 (u32 *)__LC_IO_INT_PARM);
494 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
495 (u32 *)__LC_IO_INT_WORD);
496 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
497 &vcpu->arch.sie_block->gpsw,
498 sizeof(psw_t));
499 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
500 &vcpu->arch.sie_block->gpsw,
501 sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100502 break;
503 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100504 default:
505 BUG();
506 }
Jens Freimann79395032014-04-17 10:10:30 +0200507
508 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100509}
510
Jens Freimann79395032014-04-17 10:10:30 +0200511static int deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100512{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100513 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100514
Jens Freimann1a03b7642014-02-12 14:05:38 +0100515 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
516 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
517 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
518 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
519 &vcpu->arch.sie_block->gpsw,
520 sizeof(psw_t));
Jens Freimann79395032014-04-17 10:10:30 +0200521 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100522}
523
David Hildenbrand49539192014-02-21 08:59:59 +0100524/* Check whether SIGP interpretation facility has an external call pending */
525int kvm_s390_si_ext_call_pending(struct kvm_vcpu *vcpu)
526{
527 atomic_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].ctrl;
528
529 if (!psw_extint_disabled(vcpu) &&
530 (vcpu->arch.sie_block->gcr[0] & 0x2000ul) &&
531 (atomic_read(sigp_ctrl) & SIGP_CTRL_C) &&
532 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND))
533 return 1;
534
535 return 0;
536}
537
Dominik Dingel3c038e62013-10-07 17:11:48 +0200538int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100539{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200540 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
541 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
542 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100543 int rc = 0;
544
545 if (atomic_read(&li->active)) {
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200546 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100547 list_for_each_entry(inti, &li->list, list)
548 if (__interrupt_is_deliverable(vcpu, inti)) {
549 rc = 1;
550 break;
551 }
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200552 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100553 }
554
555 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200556 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100557 list_for_each_entry(inti, &fi->list, list)
558 if (__interrupt_is_deliverable(vcpu, inti)) {
559 rc = 1;
560 break;
561 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200562 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100563 }
564
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100565 if (!rc && kvm_cpu_has_pending_timer(vcpu))
566 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100567
David Hildenbrand49539192014-02-21 08:59:59 +0100568 if (!rc && kvm_s390_si_ext_call_pending(vcpu))
569 rc = 1;
570
Carsten Otteba5c1e92008-03-25 18:47:26 +0100571 return rc;
572}
573
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300574int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
575{
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100576 if (!(vcpu->arch.sie_block->ckc <
577 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
578 return 0;
579 if (!ckc_interrupts_enabled(vcpu))
580 return 0;
581 return 1;
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300582}
583
Carsten Otteba5c1e92008-03-25 18:47:26 +0100584int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
585{
586 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100587
588 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100589
David Hildenbrand0759d062014-05-13 16:54:32 +0200590 /* fast path */
591 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
592 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200593
Carsten Otteba5c1e92008-03-25 18:47:26 +0100594 if (psw_interrupts_disabled(vcpu)) {
595 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100596 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100597 }
598
David Hildenbrand0759d062014-05-13 16:54:32 +0200599 __set_cpu_idle(vcpu);
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100600 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100601 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
602 goto no_timer;
603 }
604
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200605 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Heiko Carstensed4f2092013-01-14 16:55:55 +0100606 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200607 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
608 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100609no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200610 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200611 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100612 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200613 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
614
Christian Borntraegerca872302009-05-12 17:21:49 +0200615 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100616 return 0;
617}
618
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200619void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
620{
621 if (waitqueue_active(&vcpu->wq)) {
622 /*
623 * The vcpu gave up the cpu voluntarily, mark it as a good
624 * yield-candidate.
625 */
626 vcpu->preempted = true;
627 wake_up_interruptible(&vcpu->wq);
628 }
629}
630
Christian Borntraegerca872302009-05-12 17:21:49 +0200631enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
632{
633 struct kvm_vcpu *vcpu;
634
635 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandea74c0e2014-05-16 12:08:29 +0200636 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200637
638 return HRTIMER_NORESTART;
639}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100640
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100641void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
642{
643 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
644 struct kvm_s390_interrupt_info *n, *inti = NULL;
645
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200646 spin_lock(&li->lock);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100647 list_for_each_entry_safe(inti, n, &li->list, list) {
648 list_del(&inti->list);
649 kfree(inti);
650 }
651 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200652 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100653
654 /* clear pending external calls set by sigp interpretation facility */
655 atomic_clear_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
656 atomic_clear_mask(SIGP_CTRL_C,
657 &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].ctrl);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100658}
659
Jens Freimann79395032014-04-17 10:10:30 +0200660int kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100661{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200662 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
663 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
664 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100665 int deliver;
Jens Freimann79395032014-04-17 10:10:30 +0200666 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100667
668 __reset_intercept_indicators(vcpu);
669 if (atomic_read(&li->active)) {
670 do {
671 deliver = 0;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200672 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100673 list_for_each_entry_safe(inti, n, &li->list, list) {
674 if (__interrupt_is_deliverable(vcpu, inti)) {
675 list_del(&inti->list);
676 deliver = 1;
677 break;
678 }
679 __set_intercept_indicator(vcpu, inti);
680 }
681 if (list_empty(&li->list))
682 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200683 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100684 if (deliver) {
Jens Freimann79395032014-04-17 10:10:30 +0200685 rc = __do_deliver_interrupt(vcpu, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100686 kfree(inti);
687 }
Jens Freimann79395032014-04-17 10:10:30 +0200688 } while (!rc && deliver);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100689 }
690
Jens Freimann79395032014-04-17 10:10:30 +0200691 if (!rc && kvm_cpu_has_pending_timer(vcpu))
692 rc = deliver_ckc_interrupt(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100693
Jens Freimann79395032014-04-17 10:10:30 +0200694 if (!rc && atomic_read(&fi->active)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100695 do {
696 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200697 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100698 list_for_each_entry_safe(inti, n, &fi->list, list) {
699 if (__interrupt_is_deliverable(vcpu, inti)) {
700 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100701 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100702 deliver = 1;
703 break;
704 }
705 __set_intercept_indicator(vcpu, inti);
706 }
707 if (list_empty(&fi->list))
708 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200709 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100710 if (deliver) {
Jens Freimann79395032014-04-17 10:10:30 +0200711 rc = __do_deliver_interrupt(vcpu, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100712 kfree(inti);
713 }
Jens Freimann79395032014-04-17 10:10:30 +0200714 } while (!rc && deliver);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100715 }
Jens Freimann79395032014-04-17 10:10:30 +0200716
717 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100718}
719
720int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
721{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200722 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
723 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100724
725 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
726 if (!inti)
727 return -ENOMEM;
728
Joe Perchesa419aef2009-08-18 11:18:35 -0700729 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100730 inti->pgm.code = code;
731
732 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200733 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200734 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100735 list_add(&inti->list, &li->list);
736 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200737 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200738 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100739 return 0;
740}
741
Jens Freimannbcd84682014-02-11 11:07:05 +0100742int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
743 struct kvm_s390_pgm_info *pgm_info)
744{
745 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
746 struct kvm_s390_interrupt_info *inti;
747
748 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
749 if (!inti)
750 return -ENOMEM;
751
752 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
753 pgm_info->code);
754 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
755 pgm_info->code, 0, 1);
756
757 inti->type = KVM_S390_PROGRAM_INT;
758 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200759 spin_lock(&li->lock);
Jens Freimannbcd84682014-02-11 11:07:05 +0100760 list_add(&inti->list, &li->list);
761 atomic_set(&li->active, 1);
762 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200763 spin_unlock(&li->lock);
Jens Freimannbcd84682014-02-11 11:07:05 +0100764 return 0;
765}
766
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100767struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
768 u64 cr6, u64 schid)
769{
770 struct kvm_s390_float_interrupt *fi;
771 struct kvm_s390_interrupt_info *inti, *iter;
772
773 if ((!schid && !cr6) || (schid && cr6))
774 return NULL;
775 mutex_lock(&kvm->lock);
776 fi = &kvm->arch.float_int;
777 spin_lock(&fi->lock);
778 inti = NULL;
779 list_for_each_entry(iter, &fi->list, list) {
780 if (!is_ioint(iter->type))
781 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100782 if (cr6 &&
783 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100784 continue;
785 if (schid) {
786 if (((schid & 0x00000000ffff0000) >> 16) !=
787 iter->io.subchannel_id)
788 continue;
789 if ((schid & 0x000000000000ffff) !=
790 iter->io.subchannel_nr)
791 continue;
792 }
793 inti = iter;
794 break;
795 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100796 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100797 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100798 fi->irq_count--;
799 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100800 if (list_empty(&fi->list))
801 atomic_set(&fi->active, 0);
802 spin_unlock(&fi->lock);
803 mutex_unlock(&kvm->lock);
804 return inti;
805}
806
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100807static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100808{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200809 struct kvm_s390_local_interrupt *li;
810 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200811 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100812 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100813 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100814 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100815
Carsten Otteba5c1e92008-03-25 18:47:26 +0100816 mutex_lock(&kvm->lock);
817 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200818 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100819 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
820 rc = -EINVAL;
821 goto unlock_fi;
822 }
823 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200824 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100825 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200826 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100827 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
828
Cornelia Huckd8346b72012-12-20 15:32:08 +0100829 /* Keep I/O interrupts sorted in isc order. */
830 list_for_each_entry(iter, &fi->list, list) {
831 if (!is_ioint(iter->type))
832 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100833 if (int_word_to_isc_bits(iter->io.io_int_word)
834 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100835 continue;
836 break;
837 }
838 list_add_tail(&inti->list, &iter->list);
839 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100840 atomic_set(&fi->active, 1);
841 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
842 if (sigcpu == KVM_MAX_VCPUS) {
843 do {
844 sigcpu = fi->next_rr_cpu++;
845 if (sigcpu == KVM_MAX_VCPUS)
846 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100847 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100848 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100849 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
850 li = &dst_vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200851 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100852 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200853 spin_unlock(&li->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200854 kvm_s390_vcpu_wakeup(kvm_get_vcpu(kvm, sigcpu));
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100855unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200856 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100857 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100858 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200859}
860
861int kvm_s390_inject_vm(struct kvm *kvm,
862 struct kvm_s390_interrupt *s390int)
863{
864 struct kvm_s390_interrupt_info *inti;
865
866 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
867 if (!inti)
868 return -ENOMEM;
869
870 inti->type = s390int->type;
871 switch (inti->type) {
872 case KVM_S390_INT_VIRTIO:
873 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
874 s390int->parm, s390int->parm64);
875 inti->ext.ext_params = s390int->parm;
876 inti->ext.ext_params2 = s390int->parm64;
877 break;
878 case KVM_S390_INT_SERVICE:
879 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
880 inti->ext.ext_params = s390int->parm;
881 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200882 case KVM_S390_INT_PFAULT_DONE:
883 inti->type = s390int->type;
884 inti->ext.ext_params2 = s390int->parm64;
885 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200886 case KVM_S390_MCHK:
887 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
888 s390int->parm64);
889 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
890 inti->mchk.mcic = s390int->parm64;
891 break;
892 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
893 if (inti->type & IOINT_AI_MASK)
894 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
895 else
896 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
897 s390int->type & IOINT_CSSID_MASK,
898 s390int->type & IOINT_SSID_MASK,
899 s390int->type & IOINT_SCHID_MASK);
900 inti->io.subchannel_id = s390int->parm >> 16;
901 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
902 inti->io.io_int_parm = s390int->parm64 >> 32;
903 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
904 break;
905 default:
906 kfree(inti);
907 return -EINVAL;
908 }
909 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
910 2);
911
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100912 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100913}
914
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100915void kvm_s390_reinject_io_int(struct kvm *kvm,
916 struct kvm_s390_interrupt_info *inti)
917{
918 __inject_vm(kvm, inti);
919}
920
Carsten Otteba5c1e92008-03-25 18:47:26 +0100921int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
922 struct kvm_s390_interrupt *s390int)
923{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200924 struct kvm_s390_local_interrupt *li;
925 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100926
927 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
928 if (!inti)
929 return -ENOMEM;
930
931 switch (s390int->type) {
932 case KVM_S390_PROGRAM_INT:
933 if (s390int->parm & 0xffff0000) {
934 kfree(inti);
935 return -EINVAL;
936 }
937 inti->type = s390int->type;
938 inti->pgm.code = s390int->parm;
939 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
940 s390int->parm);
941 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100942 case KVM_S390_SIGP_SET_PREFIX:
943 inti->prefix.address = s390int->parm;
944 inti->type = s390int->type;
945 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
946 s390int->parm);
947 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100948 case KVM_S390_SIGP_STOP:
949 case KVM_S390_RESTART:
Thomas Huthe029ae52014-03-26 16:11:54 +0100950 case KVM_S390_INT_CLOCK_COMP:
951 case KVM_S390_INT_CPU_TIMER:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100952 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
953 inti->type = s390int->type;
954 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200955 case KVM_S390_INT_EXTERNAL_CALL:
956 if (s390int->parm & 0xffff0000) {
957 kfree(inti);
958 return -EINVAL;
959 }
960 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
961 s390int->parm);
962 inti->type = s390int->type;
963 inti->extcall.code = s390int->parm;
964 break;
965 case KVM_S390_INT_EMERGENCY:
966 if (s390int->parm & 0xffff0000) {
967 kfree(inti);
968 return -EINVAL;
969 }
970 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
971 inti->type = s390int->type;
972 inti->emerg.code = s390int->parm;
973 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100974 case KVM_S390_MCHK:
975 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
976 s390int->parm64);
977 inti->type = s390int->type;
978 inti->mchk.mcic = s390int->parm64;
979 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200980 case KVM_S390_INT_PFAULT_INIT:
981 inti->type = s390int->type;
982 inti->ext.ext_params2 = s390int->parm64;
983 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100984 case KVM_S390_INT_VIRTIO:
985 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100986 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100987 default:
988 kfree(inti);
989 return -EINVAL;
990 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200991 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
992 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100993
994 mutex_lock(&vcpu->kvm->lock);
995 li = &vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200996 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100997 if (inti->type == KVM_S390_PROGRAM_INT)
998 list_add(&inti->list, &li->list);
999 else
1000 list_add_tail(&inti->list, &li->list);
1001 atomic_set(&li->active, 1);
1002 if (inti->type == KVM_S390_SIGP_STOP)
1003 li->action_bits |= ACTION_STOP_ON_STOP;
1004 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001005 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001006 mutex_unlock(&vcpu->kvm->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001007 kvm_s390_vcpu_wakeup(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001008 return 0;
1009}
Jens Freimannc05c4182013-10-07 16:13:45 +02001010
Christian Borntraeger67335e62014-03-25 17:09:08 +01001011void kvm_s390_clear_float_irqs(struct kvm *kvm)
Jens Freimannc05c4182013-10-07 16:13:45 +02001012{
1013 struct kvm_s390_float_interrupt *fi;
1014 struct kvm_s390_interrupt_info *n, *inti = NULL;
1015
1016 mutex_lock(&kvm->lock);
1017 fi = &kvm->arch.float_int;
1018 spin_lock(&fi->lock);
1019 list_for_each_entry_safe(inti, n, &fi->list, list) {
1020 list_del(&inti->list);
1021 kfree(inti);
1022 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001023 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001024 atomic_set(&fi->active, 0);
1025 spin_unlock(&fi->lock);
1026 mutex_unlock(&kvm->lock);
1027}
1028
1029static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1030 u8 *addr)
1031{
1032 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1033 struct kvm_s390_irq irq = {0};
1034
1035 irq.type = inti->type;
1036 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001037 case KVM_S390_INT_PFAULT_INIT:
1038 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001039 case KVM_S390_INT_VIRTIO:
1040 case KVM_S390_INT_SERVICE:
1041 irq.u.ext = inti->ext;
1042 break;
1043 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1044 irq.u.io = inti->io;
1045 break;
1046 case KVM_S390_MCHK:
1047 irq.u.mchk = inti->mchk;
1048 break;
1049 default:
1050 return -EINVAL;
1051 }
1052
1053 if (copy_to_user(uptr, &irq, sizeof(irq)))
1054 return -EFAULT;
1055
1056 return 0;
1057}
1058
1059static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1060{
1061 struct kvm_s390_interrupt_info *inti;
1062 struct kvm_s390_float_interrupt *fi;
1063 int ret = 0;
1064 int n = 0;
1065
1066 mutex_lock(&kvm->lock);
1067 fi = &kvm->arch.float_int;
1068 spin_lock(&fi->lock);
1069
1070 list_for_each_entry(inti, &fi->list, list) {
1071 if (len < sizeof(struct kvm_s390_irq)) {
1072 /* signal userspace to try again */
1073 ret = -ENOMEM;
1074 break;
1075 }
1076 ret = copy_irq_to_user(inti, buf);
1077 if (ret)
1078 break;
1079 buf += sizeof(struct kvm_s390_irq);
1080 len -= sizeof(struct kvm_s390_irq);
1081 n++;
1082 }
1083
1084 spin_unlock(&fi->lock);
1085 mutex_unlock(&kvm->lock);
1086
1087 return ret < 0 ? ret : n;
1088}
1089
1090static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1091{
1092 int r;
1093
1094 switch (attr->group) {
1095 case KVM_DEV_FLIC_GET_ALL_IRQS:
1096 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1097 attr->attr);
1098 break;
1099 default:
1100 r = -EINVAL;
1101 }
1102
1103 return r;
1104}
1105
1106static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1107 u64 addr)
1108{
1109 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1110 void *target = NULL;
1111 void __user *source;
1112 u64 size;
1113
1114 if (get_user(inti->type, (u64 __user *)addr))
1115 return -EFAULT;
1116
1117 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001118 case KVM_S390_INT_PFAULT_INIT:
1119 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001120 case KVM_S390_INT_VIRTIO:
1121 case KVM_S390_INT_SERVICE:
1122 target = (void *) &inti->ext;
1123 source = &uptr->u.ext;
1124 size = sizeof(inti->ext);
1125 break;
1126 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1127 target = (void *) &inti->io;
1128 source = &uptr->u.io;
1129 size = sizeof(inti->io);
1130 break;
1131 case KVM_S390_MCHK:
1132 target = (void *) &inti->mchk;
1133 source = &uptr->u.mchk;
1134 size = sizeof(inti->mchk);
1135 break;
1136 default:
1137 return -EINVAL;
1138 }
1139
1140 if (copy_from_user(target, source, size))
1141 return -EFAULT;
1142
1143 return 0;
1144}
1145
1146static int enqueue_floating_irq(struct kvm_device *dev,
1147 struct kvm_device_attr *attr)
1148{
1149 struct kvm_s390_interrupt_info *inti = NULL;
1150 int r = 0;
1151 int len = attr->attr;
1152
1153 if (len % sizeof(struct kvm_s390_irq) != 0)
1154 return -EINVAL;
1155 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1156 return -EINVAL;
1157
1158 while (len >= sizeof(struct kvm_s390_irq)) {
1159 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1160 if (!inti)
1161 return -ENOMEM;
1162
1163 r = copy_irq_from_user(inti, attr->addr);
1164 if (r) {
1165 kfree(inti);
1166 return r;
1167 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001168 r = __inject_vm(dev->kvm, inti);
1169 if (r) {
1170 kfree(inti);
1171 return r;
1172 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001173 len -= sizeof(struct kvm_s390_irq);
1174 attr->addr += sizeof(struct kvm_s390_irq);
1175 }
1176
1177 return r;
1178}
1179
Cornelia Huck841b91c2013-07-15 13:36:01 +02001180static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1181{
1182 if (id >= MAX_S390_IO_ADAPTERS)
1183 return NULL;
1184 return kvm->arch.adapters[id];
1185}
1186
1187static int register_io_adapter(struct kvm_device *dev,
1188 struct kvm_device_attr *attr)
1189{
1190 struct s390_io_adapter *adapter;
1191 struct kvm_s390_io_adapter adapter_info;
1192
1193 if (copy_from_user(&adapter_info,
1194 (void __user *)attr->addr, sizeof(adapter_info)))
1195 return -EFAULT;
1196
1197 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1198 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1199 return -EINVAL;
1200
1201 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1202 if (!adapter)
1203 return -ENOMEM;
1204
1205 INIT_LIST_HEAD(&adapter->maps);
1206 init_rwsem(&adapter->maps_lock);
1207 atomic_set(&adapter->nr_maps, 0);
1208 adapter->id = adapter_info.id;
1209 adapter->isc = adapter_info.isc;
1210 adapter->maskable = adapter_info.maskable;
1211 adapter->masked = false;
1212 adapter->swap = adapter_info.swap;
1213 dev->kvm->arch.adapters[adapter->id] = adapter;
1214
1215 return 0;
1216}
1217
1218int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1219{
1220 int ret;
1221 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1222
1223 if (!adapter || !adapter->maskable)
1224 return -EINVAL;
1225 ret = adapter->masked;
1226 adapter->masked = masked;
1227 return ret;
1228}
1229
1230static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1231{
1232 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1233 struct s390_map_info *map;
1234 int ret;
1235
1236 if (!adapter || !addr)
1237 return -EINVAL;
1238
1239 map = kzalloc(sizeof(*map), GFP_KERNEL);
1240 if (!map) {
1241 ret = -ENOMEM;
1242 goto out;
1243 }
1244 INIT_LIST_HEAD(&map->list);
1245 map->guest_addr = addr;
1246 map->addr = gmap_translate(addr, kvm->arch.gmap);
1247 if (map->addr == -EFAULT) {
1248 ret = -EFAULT;
1249 goto out;
1250 }
1251 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1252 if (ret < 0)
1253 goto out;
1254 BUG_ON(ret != 1);
1255 down_write(&adapter->maps_lock);
1256 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1257 list_add_tail(&map->list, &adapter->maps);
1258 ret = 0;
1259 } else {
1260 put_page(map->page);
1261 ret = -EINVAL;
1262 }
1263 up_write(&adapter->maps_lock);
1264out:
1265 if (ret)
1266 kfree(map);
1267 return ret;
1268}
1269
1270static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1271{
1272 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1273 struct s390_map_info *map, *tmp;
1274 int found = 0;
1275
1276 if (!adapter || !addr)
1277 return -EINVAL;
1278
1279 down_write(&adapter->maps_lock);
1280 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1281 if (map->guest_addr == addr) {
1282 found = 1;
1283 atomic_dec(&adapter->nr_maps);
1284 list_del(&map->list);
1285 put_page(map->page);
1286 kfree(map);
1287 break;
1288 }
1289 }
1290 up_write(&adapter->maps_lock);
1291
1292 return found ? 0 : -EINVAL;
1293}
1294
1295void kvm_s390_destroy_adapters(struct kvm *kvm)
1296{
1297 int i;
1298 struct s390_map_info *map, *tmp;
1299
1300 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1301 if (!kvm->arch.adapters[i])
1302 continue;
1303 list_for_each_entry_safe(map, tmp,
1304 &kvm->arch.adapters[i]->maps, list) {
1305 list_del(&map->list);
1306 put_page(map->page);
1307 kfree(map);
1308 }
1309 kfree(kvm->arch.adapters[i]);
1310 }
1311}
1312
1313static int modify_io_adapter(struct kvm_device *dev,
1314 struct kvm_device_attr *attr)
1315{
1316 struct kvm_s390_io_adapter_req req;
1317 struct s390_io_adapter *adapter;
1318 int ret;
1319
1320 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1321 return -EFAULT;
1322
1323 adapter = get_io_adapter(dev->kvm, req.id);
1324 if (!adapter)
1325 return -EINVAL;
1326 switch (req.type) {
1327 case KVM_S390_IO_ADAPTER_MASK:
1328 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1329 if (ret > 0)
1330 ret = 0;
1331 break;
1332 case KVM_S390_IO_ADAPTER_MAP:
1333 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1334 break;
1335 case KVM_S390_IO_ADAPTER_UNMAP:
1336 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1337 break;
1338 default:
1339 ret = -EINVAL;
1340 }
1341
1342 return ret;
1343}
1344
Jens Freimannc05c4182013-10-07 16:13:45 +02001345static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1346{
1347 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001348 unsigned int i;
1349 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001350
1351 switch (attr->group) {
1352 case KVM_DEV_FLIC_ENQUEUE:
1353 r = enqueue_floating_irq(dev, attr);
1354 break;
1355 case KVM_DEV_FLIC_CLEAR_IRQS:
1356 r = 0;
Christian Borntraeger67335e62014-03-25 17:09:08 +01001357 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001358 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001359 case KVM_DEV_FLIC_APF_ENABLE:
1360 dev->kvm->arch.gmap->pfault_enabled = 1;
1361 break;
1362 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1363 dev->kvm->arch.gmap->pfault_enabled = 0;
1364 /*
1365 * Make sure no async faults are in transition when
1366 * clearing the queues. So we don't need to worry
1367 * about late coming workers.
1368 */
1369 synchronize_srcu(&dev->kvm->srcu);
1370 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1371 kvm_clear_async_pf_completion_queue(vcpu);
1372 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001373 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1374 r = register_io_adapter(dev, attr);
1375 break;
1376 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1377 r = modify_io_adapter(dev, attr);
1378 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001379 default:
1380 r = -EINVAL;
1381 }
1382
1383 return r;
1384}
1385
1386static int flic_create(struct kvm_device *dev, u32 type)
1387{
1388 if (!dev)
1389 return -EINVAL;
1390 if (dev->kvm->arch.flic)
1391 return -EINVAL;
1392 dev->kvm->arch.flic = dev;
1393 return 0;
1394}
1395
1396static void flic_destroy(struct kvm_device *dev)
1397{
1398 dev->kvm->arch.flic = NULL;
1399 kfree(dev);
1400}
1401
1402/* s390 floating irq controller (flic) */
1403struct kvm_device_ops kvm_flic_ops = {
1404 .name = "kvm-flic",
1405 .get_attr = flic_get_attr,
1406 .set_attr = flic_set_attr,
1407 .create = flic_create,
1408 .destroy = flic_destroy,
1409};
Cornelia Huck84223592013-07-15 13:36:01 +02001410
1411static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1412{
1413 unsigned long bit;
1414
1415 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1416
1417 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1418}
1419
1420static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1421 u64 addr)
1422{
1423 struct s390_map_info *map;
1424
1425 if (!adapter)
1426 return NULL;
1427
1428 list_for_each_entry(map, &adapter->maps, list) {
1429 if (map->guest_addr == addr)
1430 return map;
1431 }
1432 return NULL;
1433}
1434
1435static int adapter_indicators_set(struct kvm *kvm,
1436 struct s390_io_adapter *adapter,
1437 struct kvm_s390_adapter_int *adapter_int)
1438{
1439 unsigned long bit;
1440 int summary_set, idx;
1441 struct s390_map_info *info;
1442 void *map;
1443
1444 info = get_map_info(adapter, adapter_int->ind_addr);
1445 if (!info)
1446 return -1;
1447 map = page_address(info->page);
1448 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1449 set_bit(bit, map);
1450 idx = srcu_read_lock(&kvm->srcu);
1451 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1452 set_page_dirty_lock(info->page);
1453 info = get_map_info(adapter, adapter_int->summary_addr);
1454 if (!info) {
1455 srcu_read_unlock(&kvm->srcu, idx);
1456 return -1;
1457 }
1458 map = page_address(info->page);
1459 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1460 adapter->swap);
1461 summary_set = test_and_set_bit(bit, map);
1462 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1463 set_page_dirty_lock(info->page);
1464 srcu_read_unlock(&kvm->srcu, idx);
1465 return summary_set ? 0 : 1;
1466}
1467
1468/*
1469 * < 0 - not injected due to error
1470 * = 0 - coalesced, summary indicator already active
1471 * > 0 - injected interrupt
1472 */
1473static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1474 struct kvm *kvm, int irq_source_id, int level,
1475 bool line_status)
1476{
1477 int ret;
1478 struct s390_io_adapter *adapter;
1479
1480 /* We're only interested in the 0->1 transition. */
1481 if (!level)
1482 return 0;
1483 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1484 if (!adapter)
1485 return -1;
1486 down_read(&adapter->maps_lock);
1487 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1488 up_read(&adapter->maps_lock);
1489 if ((ret > 0) && !adapter->masked) {
1490 struct kvm_s390_interrupt s390int = {
1491 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1492 .parm = 0,
1493 .parm64 = (adapter->isc << 27) | 0x80000000,
1494 };
1495 ret = kvm_s390_inject_vm(kvm, &s390int);
1496 if (ret == 0)
1497 ret = 1;
1498 }
1499 return ret;
1500}
1501
Paul Mackerras8ba918d2014-06-30 20:51:10 +10001502int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02001503 const struct kvm_irq_routing_entry *ue)
1504{
1505 int ret;
1506
1507 switch (ue->type) {
1508 case KVM_IRQ_ROUTING_S390_ADAPTER:
1509 e->set = set_adapter_int;
1510 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1511 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1512 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1513 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1514 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1515 ret = 0;
1516 break;
1517 default:
1518 ret = -EINVAL;
1519 }
1520
1521 return ret;
1522}
1523
1524int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1525 int irq_source_id, int level, bool line_status)
1526{
1527 return -EINVAL;
1528}