blob: 6c9428e71fddefc72f52541023e90ac8f82972f0 [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
Thomas Huthe029ae52014-03-26 16:11:54 +010031static void deliver_ckc_interrupt(struct kvm_vcpu *vcpu);
32
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
David Hildenbrand87128362014-03-03 10:55:13 +0100209static int __deliver_prog_irq(struct kvm_vcpu *vcpu,
210 struct kvm_s390_pgm_info *pgm_info)
211{
212 const unsigned short table[] = { 2, 4, 4, 6 };
213 int rc = 0;
214
215 switch (pgm_info->code & ~PGM_PER) {
216 case PGM_AFX_TRANSLATION:
217 case PGM_ASX_TRANSLATION:
218 case PGM_EX_TRANSLATION:
219 case PGM_LFX_TRANSLATION:
220 case PGM_LSTE_SEQUENCE:
221 case PGM_LSX_TRANSLATION:
222 case PGM_LX_TRANSLATION:
223 case PGM_PRIMARY_AUTHORITY:
224 case PGM_SECONDARY_AUTHORITY:
225 case PGM_SPACE_SWITCH:
226 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
227 (u64 *)__LC_TRANS_EXC_CODE);
228 break;
229 case PGM_ALEN_TRANSLATION:
230 case PGM_ALE_SEQUENCE:
231 case PGM_ASTE_INSTANCE:
232 case PGM_ASTE_SEQUENCE:
233 case PGM_ASTE_VALIDITY:
234 case PGM_EXTENDED_AUTHORITY:
235 rc = put_guest_lc(vcpu, pgm_info->exc_access_id,
236 (u8 *)__LC_EXC_ACCESS_ID);
237 break;
238 case PGM_ASCE_TYPE:
239 case PGM_PAGE_TRANSLATION:
240 case PGM_REGION_FIRST_TRANS:
241 case PGM_REGION_SECOND_TRANS:
242 case PGM_REGION_THIRD_TRANS:
243 case PGM_SEGMENT_TRANSLATION:
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 rc |= put_guest_lc(vcpu, pgm_info->op_access_id,
249 (u8 *)__LC_OP_ACCESS_ID);
250 break;
251 case PGM_MONITOR:
252 rc = put_guest_lc(vcpu, pgm_info->mon_class_nr,
253 (u64 *)__LC_MON_CLASS_NR);
254 rc |= put_guest_lc(vcpu, pgm_info->mon_code,
255 (u64 *)__LC_MON_CODE);
256 break;
257 case PGM_DATA:
258 rc = put_guest_lc(vcpu, pgm_info->data_exc_code,
259 (u32 *)__LC_DATA_EXC_CODE);
260 break;
261 case PGM_PROTECTION:
262 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
263 (u64 *)__LC_TRANS_EXC_CODE);
264 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
265 (u8 *)__LC_EXC_ACCESS_ID);
266 break;
267 }
268
269 if (pgm_info->code & PGM_PER) {
270 rc |= put_guest_lc(vcpu, pgm_info->per_code,
271 (u8 *) __LC_PER_CODE);
272 rc |= put_guest_lc(vcpu, pgm_info->per_atmid,
273 (u8 *)__LC_PER_ATMID);
274 rc |= put_guest_lc(vcpu, pgm_info->per_address,
275 (u64 *) __LC_PER_ADDRESS);
276 rc |= put_guest_lc(vcpu, pgm_info->per_access_id,
277 (u8 *) __LC_PER_ACCESS_ID);
278 }
279
280 switch (vcpu->arch.sie_block->icptcode) {
281 case ICPT_INST:
282 case ICPT_INSTPROGI:
283 case ICPT_OPEREXC:
284 case ICPT_PARTEXEC:
285 case ICPT_IOINST:
286 /* last instruction only stored for these icptcodes */
287 rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
288 (u16 *) __LC_PGM_ILC);
289 break;
290 case ICPT_PROGI:
291 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->pgmilc,
292 (u16 *) __LC_PGM_ILC);
293 break;
294 default:
295 rc |= put_guest_lc(vcpu, 0,
296 (u16 *) __LC_PGM_ILC);
297 }
298
299 rc |= put_guest_lc(vcpu, pgm_info->code,
300 (u16 *)__LC_PGM_INT_CODE);
301 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
302 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
303 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
304 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
305
306 return rc;
307}
308
Carsten Otteba5c1e92008-03-25 18:47:26 +0100309static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200310 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100311{
312 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100313 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100314
315 switch (inti->type) {
316 case KVM_S390_INT_EMERGENCY:
317 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
318 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200319 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
320 inti->emerg.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100321 rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE);
322 rc |= put_guest_lc(vcpu, inti->emerg.code,
323 (u16 *)__LC_EXT_CPU_ADDR);
324 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
325 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
326 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100327 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100328 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200329 case KVM_S390_INT_EXTERNAL_CALL:
330 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
331 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200332 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
333 inti->extcall.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100334 rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE);
335 rc |= put_guest_lc(vcpu, inti->extcall.code,
336 (u16 *)__LC_EXT_CPU_ADDR);
337 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
338 &vcpu->arch.sie_block->gpsw,
339 sizeof(psw_t));
340 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
341 &vcpu->arch.sie_block->gpsw,
342 sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200343 break;
Thomas Huthe029ae52014-03-26 16:11:54 +0100344 case KVM_S390_INT_CLOCK_COMP:
345 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
346 inti->ext.ext_params, 0);
347 deliver_ckc_interrupt(vcpu);
348 break;
349 case KVM_S390_INT_CPU_TIMER:
350 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
351 inti->ext.ext_params, 0);
352 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
353 (u16 *)__LC_EXT_INT_CODE);
354 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
355 &vcpu->arch.sie_block->gpsw,
356 sizeof(psw_t));
357 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
358 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
359 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
360 (u32 *)__LC_EXT_PARAMS);
361 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100362 case KVM_S390_INT_SERVICE:
363 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
364 inti->ext.ext_params);
365 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200366 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
367 inti->ext.ext_params, 0);
Heiko Carstens79882762014-01-02 10:59:41 +0100368 rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE);
369 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
370 &vcpu->arch.sie_block->gpsw,
371 sizeof(psw_t));
372 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100373 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100374 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
375 (u32 *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100376 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200377 case KVM_S390_INT_PFAULT_INIT:
378 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
379 inti->ext.ext_params2);
Jens Freimann44c6ca32014-04-16 13:57:18 +0200380 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
381 (u16 *) __LC_EXT_INT_CODE);
382 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100383 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
384 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
385 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200386 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann1a03b7642014-02-12 14:05:38 +0100387 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
388 (u64 *) __LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200389 break;
390 case KVM_S390_INT_PFAULT_DONE:
391 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
392 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100393 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
394 rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR);
395 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
396 &vcpu->arch.sie_block->gpsw,
397 sizeof(psw_t));
398 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200399 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100400 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
401 (u64 *)__LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200402 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100403 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100404 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100405 inti->ext.ext_params, inti->ext.ext_params2);
406 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200407 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
408 inti->ext.ext_params,
409 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100410 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
411 rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR);
412 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
413 &vcpu->arch.sie_block->gpsw,
414 sizeof(psw_t));
415 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100416 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100417 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
418 (u32 *)__LC_EXT_PARAMS);
419 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
420 (u64 *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100421 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100422 case KVM_S390_SIGP_STOP:
423 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
424 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200425 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
426 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100427 __set_intercept_indicator(vcpu, inti);
428 break;
429
430 case KVM_S390_SIGP_SET_PREFIX:
431 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
432 inti->prefix.address);
433 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200434 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
435 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100436 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100437 break;
438
439 case KVM_S390_RESTART:
440 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
441 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200442 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
443 0, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100444 rc = write_guest_lc(vcpu,
445 offsetof(struct _lowcore, restart_old_psw),
446 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
447 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
448 &vcpu->arch.sie_block->gpsw,
449 sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100450 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100451 case KVM_S390_PROGRAM_INT:
452 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
453 inti->pgm.code,
454 table[vcpu->arch.sie_block->ipa >> 14]);
455 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200456 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
457 inti->pgm.code, 0);
David Hildenbrand87128362014-03-03 10:55:13 +0100458 rc = __deliver_prog_irq(vcpu, &inti->pgm);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100459 break;
460
Cornelia Huck48a3e952012-12-20 15:32:09 +0100461 case KVM_S390_MCHK:
462 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
463 inti->mchk.mcic);
464 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
465 inti->mchk.cr14,
466 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100467 rc = kvm_s390_vcpu_store_status(vcpu,
468 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens79882762014-01-02 10:59:41 +0100469 rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE);
470 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
471 &vcpu->arch.sie_block->gpsw,
472 sizeof(psw_t));
473 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100474 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100475 break;
476
Cornelia Huckd8346b72012-12-20 15:32:08 +0100477 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
478 {
479 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
480 inti->io.subchannel_nr;
481 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
482 inti->io.io_int_word;
483 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
484 vcpu->stat.deliver_io_int++;
485 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
486 param0, param1);
Heiko Carstens79882762014-01-02 10:59:41 +0100487 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
488 (u16 *)__LC_SUBCHANNEL_ID);
489 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
490 (u16 *)__LC_SUBCHANNEL_NR);
491 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
492 (u32 *)__LC_IO_INT_PARM);
493 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
494 (u32 *)__LC_IO_INT_WORD);
495 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
496 &vcpu->arch.sie_block->gpsw,
497 sizeof(psw_t));
498 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
499 &vcpu->arch.sie_block->gpsw,
500 sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100501 break;
502 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100503 default:
504 BUG();
505 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100506 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200507 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100508 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200509 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100510 }
511}
512
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100513static void deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100514{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100515 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100516
Jens Freimann1a03b7642014-02-12 14:05:38 +0100517 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
518 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
519 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
520 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
521 &vcpu->arch.sie_block->gpsw,
522 sizeof(psw_t));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100523 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200524 printk("kvm: The guest lowcore is not mapped during interrupt "
525 "delivery, killing userspace\n");
526 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100527 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100528}
529
David Hildenbrand49539192014-02-21 08:59:59 +0100530/* Check whether SIGP interpretation facility has an external call pending */
531int kvm_s390_si_ext_call_pending(struct kvm_vcpu *vcpu)
532{
533 atomic_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].ctrl;
534
535 if (!psw_extint_disabled(vcpu) &&
536 (vcpu->arch.sie_block->gcr[0] & 0x2000ul) &&
537 (atomic_read(sigp_ctrl) & SIGP_CTRL_C) &&
538 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND))
539 return 1;
540
541 return 0;
542}
543
Dominik Dingel3c038e62013-10-07 17:11:48 +0200544int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100545{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200546 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
547 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
548 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100549 int rc = 0;
550
551 if (atomic_read(&li->active)) {
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200552 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100553 list_for_each_entry(inti, &li->list, list)
554 if (__interrupt_is_deliverable(vcpu, inti)) {
555 rc = 1;
556 break;
557 }
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200558 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100559 }
560
561 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200562 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100563 list_for_each_entry(inti, &fi->list, list)
564 if (__interrupt_is_deliverable(vcpu, inti)) {
565 rc = 1;
566 break;
567 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200568 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100569 }
570
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100571 if (!rc && kvm_cpu_has_pending_timer(vcpu))
572 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100573
David Hildenbrand49539192014-02-21 08:59:59 +0100574 if (!rc && kvm_s390_si_ext_call_pending(vcpu))
575 rc = 1;
576
Carsten Otteba5c1e92008-03-25 18:47:26 +0100577 return rc;
578}
579
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300580int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
581{
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100582 if (!(vcpu->arch.sie_block->ckc <
583 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
584 return 0;
585 if (!ckc_interrupts_enabled(vcpu))
586 return 0;
587 return 1;
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300588}
589
Carsten Otteba5c1e92008-03-25 18:47:26 +0100590int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
591{
592 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100593
594 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100595
David Hildenbrand0759d062014-05-13 16:54:32 +0200596 /* fast path */
597 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
598 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200599
Carsten Otteba5c1e92008-03-25 18:47:26 +0100600 if (psw_interrupts_disabled(vcpu)) {
601 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100602 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100603 }
604
David Hildenbrand0759d062014-05-13 16:54:32 +0200605 __set_cpu_idle(vcpu);
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100606 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100607 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
608 goto no_timer;
609 }
610
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200611 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Heiko Carstensed4f2092013-01-14 16:55:55 +0100612 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200613 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
614 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100615no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200616 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200617 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100618 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200619 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
620
Christian Borntraegerca872302009-05-12 17:21:49 +0200621 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100622 return 0;
623}
624
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200625void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
626{
627 if (waitqueue_active(&vcpu->wq)) {
628 /*
629 * The vcpu gave up the cpu voluntarily, mark it as a good
630 * yield-candidate.
631 */
632 vcpu->preempted = true;
633 wake_up_interruptible(&vcpu->wq);
634 }
635}
636
Christian Borntraegerca872302009-05-12 17:21:49 +0200637enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
638{
639 struct kvm_vcpu *vcpu;
640
641 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandea74c0e2014-05-16 12:08:29 +0200642 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200643
644 return HRTIMER_NORESTART;
645}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100646
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100647void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
648{
649 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
650 struct kvm_s390_interrupt_info *n, *inti = NULL;
651
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200652 spin_lock(&li->lock);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100653 list_for_each_entry_safe(inti, n, &li->list, list) {
654 list_del(&inti->list);
655 kfree(inti);
656 }
657 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200658 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100659
660 /* clear pending external calls set by sigp interpretation facility */
661 atomic_clear_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
662 atomic_clear_mask(SIGP_CTRL_C,
663 &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].ctrl);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100664}
665
Carsten Otteba5c1e92008-03-25 18:47:26 +0100666void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
667{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200668 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
669 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
670 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100671 int deliver;
672
673 __reset_intercept_indicators(vcpu);
674 if (atomic_read(&li->active)) {
675 do {
676 deliver = 0;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200677 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100678 list_for_each_entry_safe(inti, n, &li->list, list) {
679 if (__interrupt_is_deliverable(vcpu, inti)) {
680 list_del(&inti->list);
681 deliver = 1;
682 break;
683 }
684 __set_intercept_indicator(vcpu, inti);
685 }
686 if (list_empty(&li->list))
687 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200688 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100689 if (deliver) {
690 __do_deliver_interrupt(vcpu, inti);
691 kfree(inti);
692 }
693 } while (deliver);
694 }
695
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100696 if (kvm_cpu_has_pending_timer(vcpu))
697 deliver_ckc_interrupt(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100698
699 if (atomic_read(&fi->active)) {
700 do {
701 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200702 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100703 list_for_each_entry_safe(inti, n, &fi->list, list) {
704 if (__interrupt_is_deliverable(vcpu, inti)) {
705 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100706 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100707 deliver = 1;
708 break;
709 }
710 __set_intercept_indicator(vcpu, inti);
711 }
712 if (list_empty(&fi->list))
713 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200714 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100715 if (deliver) {
716 __do_deliver_interrupt(vcpu, inti);
717 kfree(inti);
718 }
719 } while (deliver);
720 }
721}
722
Cornelia Huck48a3e952012-12-20 15:32:09 +0100723void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
724{
725 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
726 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
727 struct kvm_s390_interrupt_info *n, *inti = NULL;
728 int deliver;
729
730 __reset_intercept_indicators(vcpu);
731 if (atomic_read(&li->active)) {
732 do {
733 deliver = 0;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200734 spin_lock(&li->lock);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100735 list_for_each_entry_safe(inti, n, &li->list, list) {
736 if ((inti->type == KVM_S390_MCHK) &&
737 __interrupt_is_deliverable(vcpu, inti)) {
738 list_del(&inti->list);
739 deliver = 1;
740 break;
741 }
742 __set_intercept_indicator(vcpu, inti);
743 }
744 if (list_empty(&li->list))
745 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200746 spin_unlock(&li->lock);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100747 if (deliver) {
748 __do_deliver_interrupt(vcpu, inti);
749 kfree(inti);
750 }
751 } while (deliver);
752 }
753
754 if (atomic_read(&fi->active)) {
755 do {
756 deliver = 0;
757 spin_lock(&fi->lock);
758 list_for_each_entry_safe(inti, n, &fi->list, list) {
759 if ((inti->type == KVM_S390_MCHK) &&
760 __interrupt_is_deliverable(vcpu, inti)) {
761 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100762 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100763 deliver = 1;
764 break;
765 }
766 __set_intercept_indicator(vcpu, inti);
767 }
768 if (list_empty(&fi->list))
769 atomic_set(&fi->active, 0);
770 spin_unlock(&fi->lock);
771 if (deliver) {
772 __do_deliver_interrupt(vcpu, inti);
773 kfree(inti);
774 }
775 } while (deliver);
776 }
777}
778
Carsten Otteba5c1e92008-03-25 18:47:26 +0100779int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
780{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200781 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
782 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100783
784 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
785 if (!inti)
786 return -ENOMEM;
787
Joe Perchesa419aef2009-08-18 11:18:35 -0700788 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100789 inti->pgm.code = code;
790
791 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200792 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200793 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100794 list_add(&inti->list, &li->list);
795 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200796 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200797 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100798 return 0;
799}
800
Jens Freimannbcd84682014-02-11 11:07:05 +0100801int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
802 struct kvm_s390_pgm_info *pgm_info)
803{
804 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
805 struct kvm_s390_interrupt_info *inti;
806
807 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
808 if (!inti)
809 return -ENOMEM;
810
811 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
812 pgm_info->code);
813 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
814 pgm_info->code, 0, 1);
815
816 inti->type = KVM_S390_PROGRAM_INT;
817 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200818 spin_lock(&li->lock);
Jens Freimannbcd84682014-02-11 11:07:05 +0100819 list_add(&inti->list, &li->list);
820 atomic_set(&li->active, 1);
821 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200822 spin_unlock(&li->lock);
Jens Freimannbcd84682014-02-11 11:07:05 +0100823 return 0;
824}
825
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100826struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
827 u64 cr6, u64 schid)
828{
829 struct kvm_s390_float_interrupt *fi;
830 struct kvm_s390_interrupt_info *inti, *iter;
831
832 if ((!schid && !cr6) || (schid && cr6))
833 return NULL;
834 mutex_lock(&kvm->lock);
835 fi = &kvm->arch.float_int;
836 spin_lock(&fi->lock);
837 inti = NULL;
838 list_for_each_entry(iter, &fi->list, list) {
839 if (!is_ioint(iter->type))
840 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100841 if (cr6 &&
842 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100843 continue;
844 if (schid) {
845 if (((schid & 0x00000000ffff0000) >> 16) !=
846 iter->io.subchannel_id)
847 continue;
848 if ((schid & 0x000000000000ffff) !=
849 iter->io.subchannel_nr)
850 continue;
851 }
852 inti = iter;
853 break;
854 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100855 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100856 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100857 fi->irq_count--;
858 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100859 if (list_empty(&fi->list))
860 atomic_set(&fi->active, 0);
861 spin_unlock(&fi->lock);
862 mutex_unlock(&kvm->lock);
863 return inti;
864}
865
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100866static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100867{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200868 struct kvm_s390_local_interrupt *li;
869 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200870 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100871 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100872 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100873 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100874
Carsten Otteba5c1e92008-03-25 18:47:26 +0100875 mutex_lock(&kvm->lock);
876 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200877 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100878 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
879 rc = -EINVAL;
880 goto unlock_fi;
881 }
882 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200883 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100884 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200885 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100886 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
887
Cornelia Huckd8346b72012-12-20 15:32:08 +0100888 /* Keep I/O interrupts sorted in isc order. */
889 list_for_each_entry(iter, &fi->list, list) {
890 if (!is_ioint(iter->type))
891 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100892 if (int_word_to_isc_bits(iter->io.io_int_word)
893 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100894 continue;
895 break;
896 }
897 list_add_tail(&inti->list, &iter->list);
898 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100899 atomic_set(&fi->active, 1);
900 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
901 if (sigcpu == KVM_MAX_VCPUS) {
902 do {
903 sigcpu = fi->next_rr_cpu++;
904 if (sigcpu == KVM_MAX_VCPUS)
905 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100906 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100907 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100908 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
909 li = &dst_vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200910 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100911 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200912 spin_unlock(&li->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200913 kvm_s390_vcpu_wakeup(kvm_get_vcpu(kvm, sigcpu));
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100914unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200915 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100916 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100917 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200918}
919
920int kvm_s390_inject_vm(struct kvm *kvm,
921 struct kvm_s390_interrupt *s390int)
922{
923 struct kvm_s390_interrupt_info *inti;
924
925 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
926 if (!inti)
927 return -ENOMEM;
928
929 inti->type = s390int->type;
930 switch (inti->type) {
931 case KVM_S390_INT_VIRTIO:
932 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
933 s390int->parm, s390int->parm64);
934 inti->ext.ext_params = s390int->parm;
935 inti->ext.ext_params2 = s390int->parm64;
936 break;
937 case KVM_S390_INT_SERVICE:
938 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
939 inti->ext.ext_params = s390int->parm;
940 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200941 case KVM_S390_INT_PFAULT_DONE:
942 inti->type = s390int->type;
943 inti->ext.ext_params2 = s390int->parm64;
944 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200945 case KVM_S390_MCHK:
946 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
947 s390int->parm64);
948 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
949 inti->mchk.mcic = s390int->parm64;
950 break;
951 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
952 if (inti->type & IOINT_AI_MASK)
953 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
954 else
955 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
956 s390int->type & IOINT_CSSID_MASK,
957 s390int->type & IOINT_SSID_MASK,
958 s390int->type & IOINT_SCHID_MASK);
959 inti->io.subchannel_id = s390int->parm >> 16;
960 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
961 inti->io.io_int_parm = s390int->parm64 >> 32;
962 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
963 break;
964 default:
965 kfree(inti);
966 return -EINVAL;
967 }
968 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
969 2);
970
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100971 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100972}
973
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100974void kvm_s390_reinject_io_int(struct kvm *kvm,
975 struct kvm_s390_interrupt_info *inti)
976{
977 __inject_vm(kvm, inti);
978}
979
Carsten Otteba5c1e92008-03-25 18:47:26 +0100980int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
981 struct kvm_s390_interrupt *s390int)
982{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200983 struct kvm_s390_local_interrupt *li;
984 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100985
986 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
987 if (!inti)
988 return -ENOMEM;
989
990 switch (s390int->type) {
991 case KVM_S390_PROGRAM_INT:
992 if (s390int->parm & 0xffff0000) {
993 kfree(inti);
994 return -EINVAL;
995 }
996 inti->type = s390int->type;
997 inti->pgm.code = s390int->parm;
998 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
999 s390int->parm);
1000 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001001 case KVM_S390_SIGP_SET_PREFIX:
1002 inti->prefix.address = s390int->parm;
1003 inti->type = s390int->type;
1004 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
1005 s390int->parm);
1006 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001007 case KVM_S390_SIGP_STOP:
1008 case KVM_S390_RESTART:
Thomas Huthe029ae52014-03-26 16:11:54 +01001009 case KVM_S390_INT_CLOCK_COMP:
1010 case KVM_S390_INT_CPU_TIMER:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001011 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
1012 inti->type = s390int->type;
1013 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001014 case KVM_S390_INT_EXTERNAL_CALL:
1015 if (s390int->parm & 0xffff0000) {
1016 kfree(inti);
1017 return -EINVAL;
1018 }
1019 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
1020 s390int->parm);
1021 inti->type = s390int->type;
1022 inti->extcall.code = s390int->parm;
1023 break;
1024 case KVM_S390_INT_EMERGENCY:
1025 if (s390int->parm & 0xffff0000) {
1026 kfree(inti);
1027 return -EINVAL;
1028 }
1029 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
1030 inti->type = s390int->type;
1031 inti->emerg.code = s390int->parm;
1032 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001033 case KVM_S390_MCHK:
1034 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1035 s390int->parm64);
1036 inti->type = s390int->type;
1037 inti->mchk.mcic = s390int->parm64;
1038 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001039 case KVM_S390_INT_PFAULT_INIT:
1040 inti->type = s390int->type;
1041 inti->ext.ext_params2 = s390int->parm64;
1042 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001043 case KVM_S390_INT_VIRTIO:
1044 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001045 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001046 default:
1047 kfree(inti);
1048 return -EINVAL;
1049 }
Cornelia Huckade38c32012-07-23 17:20:30 +02001050 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
1051 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001052
1053 mutex_lock(&vcpu->kvm->lock);
1054 li = &vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001055 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001056 if (inti->type == KVM_S390_PROGRAM_INT)
1057 list_add(&inti->list, &li->list);
1058 else
1059 list_add_tail(&inti->list, &li->list);
1060 atomic_set(&li->active, 1);
1061 if (inti->type == KVM_S390_SIGP_STOP)
1062 li->action_bits |= ACTION_STOP_ON_STOP;
1063 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001064 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001065 mutex_unlock(&vcpu->kvm->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001066 kvm_s390_vcpu_wakeup(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001067 return 0;
1068}
Jens Freimannc05c4182013-10-07 16:13:45 +02001069
Christian Borntraeger67335e62014-03-25 17:09:08 +01001070void kvm_s390_clear_float_irqs(struct kvm *kvm)
Jens Freimannc05c4182013-10-07 16:13:45 +02001071{
1072 struct kvm_s390_float_interrupt *fi;
1073 struct kvm_s390_interrupt_info *n, *inti = NULL;
1074
1075 mutex_lock(&kvm->lock);
1076 fi = &kvm->arch.float_int;
1077 spin_lock(&fi->lock);
1078 list_for_each_entry_safe(inti, n, &fi->list, list) {
1079 list_del(&inti->list);
1080 kfree(inti);
1081 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001082 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001083 atomic_set(&fi->active, 0);
1084 spin_unlock(&fi->lock);
1085 mutex_unlock(&kvm->lock);
1086}
1087
1088static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1089 u8 *addr)
1090{
1091 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1092 struct kvm_s390_irq irq = {0};
1093
1094 irq.type = inti->type;
1095 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001096 case KVM_S390_INT_PFAULT_INIT:
1097 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001098 case KVM_S390_INT_VIRTIO:
1099 case KVM_S390_INT_SERVICE:
1100 irq.u.ext = inti->ext;
1101 break;
1102 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1103 irq.u.io = inti->io;
1104 break;
1105 case KVM_S390_MCHK:
1106 irq.u.mchk = inti->mchk;
1107 break;
1108 default:
1109 return -EINVAL;
1110 }
1111
1112 if (copy_to_user(uptr, &irq, sizeof(irq)))
1113 return -EFAULT;
1114
1115 return 0;
1116}
1117
1118static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1119{
1120 struct kvm_s390_interrupt_info *inti;
1121 struct kvm_s390_float_interrupt *fi;
1122 int ret = 0;
1123 int n = 0;
1124
1125 mutex_lock(&kvm->lock);
1126 fi = &kvm->arch.float_int;
1127 spin_lock(&fi->lock);
1128
1129 list_for_each_entry(inti, &fi->list, list) {
1130 if (len < sizeof(struct kvm_s390_irq)) {
1131 /* signal userspace to try again */
1132 ret = -ENOMEM;
1133 break;
1134 }
1135 ret = copy_irq_to_user(inti, buf);
1136 if (ret)
1137 break;
1138 buf += sizeof(struct kvm_s390_irq);
1139 len -= sizeof(struct kvm_s390_irq);
1140 n++;
1141 }
1142
1143 spin_unlock(&fi->lock);
1144 mutex_unlock(&kvm->lock);
1145
1146 return ret < 0 ? ret : n;
1147}
1148
1149static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1150{
1151 int r;
1152
1153 switch (attr->group) {
1154 case KVM_DEV_FLIC_GET_ALL_IRQS:
1155 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1156 attr->attr);
1157 break;
1158 default:
1159 r = -EINVAL;
1160 }
1161
1162 return r;
1163}
1164
1165static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1166 u64 addr)
1167{
1168 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1169 void *target = NULL;
1170 void __user *source;
1171 u64 size;
1172
1173 if (get_user(inti->type, (u64 __user *)addr))
1174 return -EFAULT;
1175
1176 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001177 case KVM_S390_INT_PFAULT_INIT:
1178 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001179 case KVM_S390_INT_VIRTIO:
1180 case KVM_S390_INT_SERVICE:
1181 target = (void *) &inti->ext;
1182 source = &uptr->u.ext;
1183 size = sizeof(inti->ext);
1184 break;
1185 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1186 target = (void *) &inti->io;
1187 source = &uptr->u.io;
1188 size = sizeof(inti->io);
1189 break;
1190 case KVM_S390_MCHK:
1191 target = (void *) &inti->mchk;
1192 source = &uptr->u.mchk;
1193 size = sizeof(inti->mchk);
1194 break;
1195 default:
1196 return -EINVAL;
1197 }
1198
1199 if (copy_from_user(target, source, size))
1200 return -EFAULT;
1201
1202 return 0;
1203}
1204
1205static int enqueue_floating_irq(struct kvm_device *dev,
1206 struct kvm_device_attr *attr)
1207{
1208 struct kvm_s390_interrupt_info *inti = NULL;
1209 int r = 0;
1210 int len = attr->attr;
1211
1212 if (len % sizeof(struct kvm_s390_irq) != 0)
1213 return -EINVAL;
1214 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1215 return -EINVAL;
1216
1217 while (len >= sizeof(struct kvm_s390_irq)) {
1218 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1219 if (!inti)
1220 return -ENOMEM;
1221
1222 r = copy_irq_from_user(inti, attr->addr);
1223 if (r) {
1224 kfree(inti);
1225 return r;
1226 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001227 r = __inject_vm(dev->kvm, inti);
1228 if (r) {
1229 kfree(inti);
1230 return r;
1231 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001232 len -= sizeof(struct kvm_s390_irq);
1233 attr->addr += sizeof(struct kvm_s390_irq);
1234 }
1235
1236 return r;
1237}
1238
Cornelia Huck841b91c2013-07-15 13:36:01 +02001239static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1240{
1241 if (id >= MAX_S390_IO_ADAPTERS)
1242 return NULL;
1243 return kvm->arch.adapters[id];
1244}
1245
1246static int register_io_adapter(struct kvm_device *dev,
1247 struct kvm_device_attr *attr)
1248{
1249 struct s390_io_adapter *adapter;
1250 struct kvm_s390_io_adapter adapter_info;
1251
1252 if (copy_from_user(&adapter_info,
1253 (void __user *)attr->addr, sizeof(adapter_info)))
1254 return -EFAULT;
1255
1256 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1257 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1258 return -EINVAL;
1259
1260 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1261 if (!adapter)
1262 return -ENOMEM;
1263
1264 INIT_LIST_HEAD(&adapter->maps);
1265 init_rwsem(&adapter->maps_lock);
1266 atomic_set(&adapter->nr_maps, 0);
1267 adapter->id = adapter_info.id;
1268 adapter->isc = adapter_info.isc;
1269 adapter->maskable = adapter_info.maskable;
1270 adapter->masked = false;
1271 adapter->swap = adapter_info.swap;
1272 dev->kvm->arch.adapters[adapter->id] = adapter;
1273
1274 return 0;
1275}
1276
1277int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1278{
1279 int ret;
1280 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1281
1282 if (!adapter || !adapter->maskable)
1283 return -EINVAL;
1284 ret = adapter->masked;
1285 adapter->masked = masked;
1286 return ret;
1287}
1288
1289static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1290{
1291 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1292 struct s390_map_info *map;
1293 int ret;
1294
1295 if (!adapter || !addr)
1296 return -EINVAL;
1297
1298 map = kzalloc(sizeof(*map), GFP_KERNEL);
1299 if (!map) {
1300 ret = -ENOMEM;
1301 goto out;
1302 }
1303 INIT_LIST_HEAD(&map->list);
1304 map->guest_addr = addr;
1305 map->addr = gmap_translate(addr, kvm->arch.gmap);
1306 if (map->addr == -EFAULT) {
1307 ret = -EFAULT;
1308 goto out;
1309 }
1310 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1311 if (ret < 0)
1312 goto out;
1313 BUG_ON(ret != 1);
1314 down_write(&adapter->maps_lock);
1315 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1316 list_add_tail(&map->list, &adapter->maps);
1317 ret = 0;
1318 } else {
1319 put_page(map->page);
1320 ret = -EINVAL;
1321 }
1322 up_write(&adapter->maps_lock);
1323out:
1324 if (ret)
1325 kfree(map);
1326 return ret;
1327}
1328
1329static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1330{
1331 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1332 struct s390_map_info *map, *tmp;
1333 int found = 0;
1334
1335 if (!adapter || !addr)
1336 return -EINVAL;
1337
1338 down_write(&adapter->maps_lock);
1339 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1340 if (map->guest_addr == addr) {
1341 found = 1;
1342 atomic_dec(&adapter->nr_maps);
1343 list_del(&map->list);
1344 put_page(map->page);
1345 kfree(map);
1346 break;
1347 }
1348 }
1349 up_write(&adapter->maps_lock);
1350
1351 return found ? 0 : -EINVAL;
1352}
1353
1354void kvm_s390_destroy_adapters(struct kvm *kvm)
1355{
1356 int i;
1357 struct s390_map_info *map, *tmp;
1358
1359 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1360 if (!kvm->arch.adapters[i])
1361 continue;
1362 list_for_each_entry_safe(map, tmp,
1363 &kvm->arch.adapters[i]->maps, list) {
1364 list_del(&map->list);
1365 put_page(map->page);
1366 kfree(map);
1367 }
1368 kfree(kvm->arch.adapters[i]);
1369 }
1370}
1371
1372static int modify_io_adapter(struct kvm_device *dev,
1373 struct kvm_device_attr *attr)
1374{
1375 struct kvm_s390_io_adapter_req req;
1376 struct s390_io_adapter *adapter;
1377 int ret;
1378
1379 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1380 return -EFAULT;
1381
1382 adapter = get_io_adapter(dev->kvm, req.id);
1383 if (!adapter)
1384 return -EINVAL;
1385 switch (req.type) {
1386 case KVM_S390_IO_ADAPTER_MASK:
1387 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1388 if (ret > 0)
1389 ret = 0;
1390 break;
1391 case KVM_S390_IO_ADAPTER_MAP:
1392 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1393 break;
1394 case KVM_S390_IO_ADAPTER_UNMAP:
1395 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1396 break;
1397 default:
1398 ret = -EINVAL;
1399 }
1400
1401 return ret;
1402}
1403
Jens Freimannc05c4182013-10-07 16:13:45 +02001404static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1405{
1406 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001407 unsigned int i;
1408 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001409
1410 switch (attr->group) {
1411 case KVM_DEV_FLIC_ENQUEUE:
1412 r = enqueue_floating_irq(dev, attr);
1413 break;
1414 case KVM_DEV_FLIC_CLEAR_IRQS:
1415 r = 0;
Christian Borntraeger67335e62014-03-25 17:09:08 +01001416 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001417 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001418 case KVM_DEV_FLIC_APF_ENABLE:
1419 dev->kvm->arch.gmap->pfault_enabled = 1;
1420 break;
1421 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1422 dev->kvm->arch.gmap->pfault_enabled = 0;
1423 /*
1424 * Make sure no async faults are in transition when
1425 * clearing the queues. So we don't need to worry
1426 * about late coming workers.
1427 */
1428 synchronize_srcu(&dev->kvm->srcu);
1429 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1430 kvm_clear_async_pf_completion_queue(vcpu);
1431 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001432 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1433 r = register_io_adapter(dev, attr);
1434 break;
1435 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1436 r = modify_io_adapter(dev, attr);
1437 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001438 default:
1439 r = -EINVAL;
1440 }
1441
1442 return r;
1443}
1444
1445static int flic_create(struct kvm_device *dev, u32 type)
1446{
1447 if (!dev)
1448 return -EINVAL;
1449 if (dev->kvm->arch.flic)
1450 return -EINVAL;
1451 dev->kvm->arch.flic = dev;
1452 return 0;
1453}
1454
1455static void flic_destroy(struct kvm_device *dev)
1456{
1457 dev->kvm->arch.flic = NULL;
1458 kfree(dev);
1459}
1460
1461/* s390 floating irq controller (flic) */
1462struct kvm_device_ops kvm_flic_ops = {
1463 .name = "kvm-flic",
1464 .get_attr = flic_get_attr,
1465 .set_attr = flic_set_attr,
1466 .create = flic_create,
1467 .destroy = flic_destroy,
1468};
Cornelia Huck84223592013-07-15 13:36:01 +02001469
1470static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1471{
1472 unsigned long bit;
1473
1474 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1475
1476 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1477}
1478
1479static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1480 u64 addr)
1481{
1482 struct s390_map_info *map;
1483
1484 if (!adapter)
1485 return NULL;
1486
1487 list_for_each_entry(map, &adapter->maps, list) {
1488 if (map->guest_addr == addr)
1489 return map;
1490 }
1491 return NULL;
1492}
1493
1494static int adapter_indicators_set(struct kvm *kvm,
1495 struct s390_io_adapter *adapter,
1496 struct kvm_s390_adapter_int *adapter_int)
1497{
1498 unsigned long bit;
1499 int summary_set, idx;
1500 struct s390_map_info *info;
1501 void *map;
1502
1503 info = get_map_info(adapter, adapter_int->ind_addr);
1504 if (!info)
1505 return -1;
1506 map = page_address(info->page);
1507 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1508 set_bit(bit, map);
1509 idx = srcu_read_lock(&kvm->srcu);
1510 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1511 set_page_dirty_lock(info->page);
1512 info = get_map_info(adapter, adapter_int->summary_addr);
1513 if (!info) {
1514 srcu_read_unlock(&kvm->srcu, idx);
1515 return -1;
1516 }
1517 map = page_address(info->page);
1518 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1519 adapter->swap);
1520 summary_set = test_and_set_bit(bit, map);
1521 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1522 set_page_dirty_lock(info->page);
1523 srcu_read_unlock(&kvm->srcu, idx);
1524 return summary_set ? 0 : 1;
1525}
1526
1527/*
1528 * < 0 - not injected due to error
1529 * = 0 - coalesced, summary indicator already active
1530 * > 0 - injected interrupt
1531 */
1532static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1533 struct kvm *kvm, int irq_source_id, int level,
1534 bool line_status)
1535{
1536 int ret;
1537 struct s390_io_adapter *adapter;
1538
1539 /* We're only interested in the 0->1 transition. */
1540 if (!level)
1541 return 0;
1542 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1543 if (!adapter)
1544 return -1;
1545 down_read(&adapter->maps_lock);
1546 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1547 up_read(&adapter->maps_lock);
1548 if ((ret > 0) && !adapter->masked) {
1549 struct kvm_s390_interrupt s390int = {
1550 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1551 .parm = 0,
1552 .parm64 = (adapter->isc << 27) | 0x80000000,
1553 };
1554 ret = kvm_s390_inject_vm(kvm, &s390int);
1555 if (ret == 0)
1556 ret = 1;
1557 }
1558 return ret;
1559}
1560
Paul Mackerras8ba918d2014-06-30 20:51:10 +10001561int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02001562 const struct kvm_irq_routing_entry *ue)
1563{
1564 int ret;
1565
1566 switch (ue->type) {
1567 case KVM_IRQ_ROUTING_S390_ADAPTER:
1568 e->set = set_adapter_int;
1569 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1570 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1571 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1572 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1573 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1574 ret = 0;
1575 break;
1576 default:
1577 ret = -EINVAL;
1578 }
1579
1580 return ret;
1581}
1582
1583int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1584 int irq_source_id, int level, bool line_status)
1585{
1586 return -EINVAL;
1587}