blob: 71bf7e749cf73e946a47acdf9a83c8becc170e41 [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
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
Carsten Otteba5c1e92008-03-25 18:47:26 +0100310static void __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);
348 deliver_ckc_interrupt(vcpu);
349 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 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100507 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200508 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100509 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200510 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100511 }
512}
513
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100514static void deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100515{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100516 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100517
Jens Freimann1a03b7642014-02-12 14:05:38 +0100518 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
519 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
520 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
521 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
522 &vcpu->arch.sie_block->gpsw,
523 sizeof(psw_t));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100524 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200525 printk("kvm: The guest lowcore is not mapped during interrupt "
526 "delivery, killing userspace\n");
527 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100528 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100529}
530
David Hildenbrand49539192014-02-21 08:59:59 +0100531/* Check whether SIGP interpretation facility has an external call pending */
532int kvm_s390_si_ext_call_pending(struct kvm_vcpu *vcpu)
533{
534 atomic_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].ctrl;
535
536 if (!psw_extint_disabled(vcpu) &&
537 (vcpu->arch.sie_block->gcr[0] & 0x2000ul) &&
538 (atomic_read(sigp_ctrl) & SIGP_CTRL_C) &&
539 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND))
540 return 1;
541
542 return 0;
543}
544
Dominik Dingel3c038e62013-10-07 17:11:48 +0200545int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100546{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200547 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
548 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
549 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100550 int rc = 0;
551
552 if (atomic_read(&li->active)) {
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200553 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100554 list_for_each_entry(inti, &li->list, list)
555 if (__interrupt_is_deliverable(vcpu, inti)) {
556 rc = 1;
557 break;
558 }
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200559 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100560 }
561
562 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200563 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100564 list_for_each_entry(inti, &fi->list, list)
565 if (__interrupt_is_deliverable(vcpu, inti)) {
566 rc = 1;
567 break;
568 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200569 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100570 }
571
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100572 if (!rc && kvm_cpu_has_pending_timer(vcpu))
573 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100574
David Hildenbrand49539192014-02-21 08:59:59 +0100575 if (!rc && kvm_s390_si_ext_call_pending(vcpu))
576 rc = 1;
577
Carsten Otteba5c1e92008-03-25 18:47:26 +0100578 return rc;
579}
580
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300581int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
582{
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100583 if (!(vcpu->arch.sie_block->ckc <
584 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
585 return 0;
586 if (!ckc_interrupts_enabled(vcpu))
587 return 0;
588 return 1;
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300589}
590
Carsten Otteba5c1e92008-03-25 18:47:26 +0100591int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
592{
593 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100594
595 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100596
David Hildenbrand0759d062014-05-13 16:54:32 +0200597 /* fast path */
598 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
599 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200600
Carsten Otteba5c1e92008-03-25 18:47:26 +0100601 if (psw_interrupts_disabled(vcpu)) {
602 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100603 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100604 }
605
David Hildenbrand0759d062014-05-13 16:54:32 +0200606 __set_cpu_idle(vcpu);
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100607 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100608 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
609 goto no_timer;
610 }
611
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200612 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Heiko Carstensed4f2092013-01-14 16:55:55 +0100613 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200614 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
615 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100616no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200617 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200618 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100619 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200620 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
621
Christian Borntraegerca872302009-05-12 17:21:49 +0200622 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100623 return 0;
624}
625
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200626void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
627{
628 if (waitqueue_active(&vcpu->wq)) {
629 /*
630 * The vcpu gave up the cpu voluntarily, mark it as a good
631 * yield-candidate.
632 */
633 vcpu->preempted = true;
634 wake_up_interruptible(&vcpu->wq);
635 }
636}
637
Christian Borntraegerca872302009-05-12 17:21:49 +0200638enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
639{
640 struct kvm_vcpu *vcpu;
641
642 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandea74c0e2014-05-16 12:08:29 +0200643 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200644
645 return HRTIMER_NORESTART;
646}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100647
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100648void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
649{
650 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
651 struct kvm_s390_interrupt_info *n, *inti = NULL;
652
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200653 spin_lock(&li->lock);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100654 list_for_each_entry_safe(inti, n, &li->list, list) {
655 list_del(&inti->list);
656 kfree(inti);
657 }
658 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200659 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100660
661 /* clear pending external calls set by sigp interpretation facility */
662 atomic_clear_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
663 atomic_clear_mask(SIGP_CTRL_C,
664 &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].ctrl);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100665}
666
Carsten Otteba5c1e92008-03-25 18:47:26 +0100667void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
668{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200669 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
670 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
671 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100672 int deliver;
673
674 __reset_intercept_indicators(vcpu);
675 if (atomic_read(&li->active)) {
676 do {
677 deliver = 0;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200678 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100679 list_for_each_entry_safe(inti, n, &li->list, list) {
680 if (__interrupt_is_deliverable(vcpu, inti)) {
681 list_del(&inti->list);
682 deliver = 1;
683 break;
684 }
685 __set_intercept_indicator(vcpu, inti);
686 }
687 if (list_empty(&li->list))
688 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200689 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100690 if (deliver) {
691 __do_deliver_interrupt(vcpu, inti);
692 kfree(inti);
693 }
694 } while (deliver);
695 }
696
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100697 if (kvm_cpu_has_pending_timer(vcpu))
698 deliver_ckc_interrupt(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100699
700 if (atomic_read(&fi->active)) {
701 do {
702 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200703 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100704 list_for_each_entry_safe(inti, n, &fi->list, list) {
705 if (__interrupt_is_deliverable(vcpu, inti)) {
706 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100707 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100708 deliver = 1;
709 break;
710 }
711 __set_intercept_indicator(vcpu, inti);
712 }
713 if (list_empty(&fi->list))
714 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200715 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100716 if (deliver) {
717 __do_deliver_interrupt(vcpu, inti);
718 kfree(inti);
719 }
720 } while (deliver);
721 }
722}
723
Cornelia Huck48a3e952012-12-20 15:32:09 +0100724void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
725{
726 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
727 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
728 struct kvm_s390_interrupt_info *n, *inti = NULL;
729 int deliver;
730
731 __reset_intercept_indicators(vcpu);
732 if (atomic_read(&li->active)) {
733 do {
734 deliver = 0;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200735 spin_lock(&li->lock);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100736 list_for_each_entry_safe(inti, n, &li->list, list) {
737 if ((inti->type == KVM_S390_MCHK) &&
738 __interrupt_is_deliverable(vcpu, inti)) {
739 list_del(&inti->list);
740 deliver = 1;
741 break;
742 }
743 __set_intercept_indicator(vcpu, inti);
744 }
745 if (list_empty(&li->list))
746 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200747 spin_unlock(&li->lock);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100748 if (deliver) {
749 __do_deliver_interrupt(vcpu, inti);
750 kfree(inti);
751 }
752 } while (deliver);
753 }
754
755 if (atomic_read(&fi->active)) {
756 do {
757 deliver = 0;
758 spin_lock(&fi->lock);
759 list_for_each_entry_safe(inti, n, &fi->list, list) {
760 if ((inti->type == KVM_S390_MCHK) &&
761 __interrupt_is_deliverable(vcpu, inti)) {
762 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100763 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100764 deliver = 1;
765 break;
766 }
767 __set_intercept_indicator(vcpu, inti);
768 }
769 if (list_empty(&fi->list))
770 atomic_set(&fi->active, 0);
771 spin_unlock(&fi->lock);
772 if (deliver) {
773 __do_deliver_interrupt(vcpu, inti);
774 kfree(inti);
775 }
776 } while (deliver);
777 }
778}
779
Carsten Otteba5c1e92008-03-25 18:47:26 +0100780int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
781{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200782 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
783 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100784
785 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
786 if (!inti)
787 return -ENOMEM;
788
Joe Perchesa419aef2009-08-18 11:18:35 -0700789 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100790 inti->pgm.code = code;
791
792 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200793 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200794 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100795 list_add(&inti->list, &li->list);
796 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200797 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200798 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100799 return 0;
800}
801
Jens Freimannbcd84682014-02-11 11:07:05 +0100802int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
803 struct kvm_s390_pgm_info *pgm_info)
804{
805 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
806 struct kvm_s390_interrupt_info *inti;
807
808 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
809 if (!inti)
810 return -ENOMEM;
811
812 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
813 pgm_info->code);
814 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
815 pgm_info->code, 0, 1);
816
817 inti->type = KVM_S390_PROGRAM_INT;
818 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200819 spin_lock(&li->lock);
Jens Freimannbcd84682014-02-11 11:07:05 +0100820 list_add(&inti->list, &li->list);
821 atomic_set(&li->active, 1);
822 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200823 spin_unlock(&li->lock);
Jens Freimannbcd84682014-02-11 11:07:05 +0100824 return 0;
825}
826
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100827struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
828 u64 cr6, u64 schid)
829{
830 struct kvm_s390_float_interrupt *fi;
831 struct kvm_s390_interrupt_info *inti, *iter;
832
833 if ((!schid && !cr6) || (schid && cr6))
834 return NULL;
835 mutex_lock(&kvm->lock);
836 fi = &kvm->arch.float_int;
837 spin_lock(&fi->lock);
838 inti = NULL;
839 list_for_each_entry(iter, &fi->list, list) {
840 if (!is_ioint(iter->type))
841 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100842 if (cr6 &&
843 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100844 continue;
845 if (schid) {
846 if (((schid & 0x00000000ffff0000) >> 16) !=
847 iter->io.subchannel_id)
848 continue;
849 if ((schid & 0x000000000000ffff) !=
850 iter->io.subchannel_nr)
851 continue;
852 }
853 inti = iter;
854 break;
855 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100856 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100857 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100858 fi->irq_count--;
859 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100860 if (list_empty(&fi->list))
861 atomic_set(&fi->active, 0);
862 spin_unlock(&fi->lock);
863 mutex_unlock(&kvm->lock);
864 return inti;
865}
866
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100867static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100868{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200869 struct kvm_s390_local_interrupt *li;
870 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200871 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100872 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100873 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100874 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100875
Carsten Otteba5c1e92008-03-25 18:47:26 +0100876 mutex_lock(&kvm->lock);
877 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200878 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100879 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
880 rc = -EINVAL;
881 goto unlock_fi;
882 }
883 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200884 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100885 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200886 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100887 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
888
Cornelia Huckd8346b72012-12-20 15:32:08 +0100889 /* Keep I/O interrupts sorted in isc order. */
890 list_for_each_entry(iter, &fi->list, list) {
891 if (!is_ioint(iter->type))
892 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100893 if (int_word_to_isc_bits(iter->io.io_int_word)
894 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100895 continue;
896 break;
897 }
898 list_add_tail(&inti->list, &iter->list);
899 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100900 atomic_set(&fi->active, 1);
901 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
902 if (sigcpu == KVM_MAX_VCPUS) {
903 do {
904 sigcpu = fi->next_rr_cpu++;
905 if (sigcpu == KVM_MAX_VCPUS)
906 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100907 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100908 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100909 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
910 li = &dst_vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200911 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100912 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200913 spin_unlock(&li->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200914 kvm_s390_vcpu_wakeup(kvm_get_vcpu(kvm, sigcpu));
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100915unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200916 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100917 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100918 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200919}
920
921int kvm_s390_inject_vm(struct kvm *kvm,
922 struct kvm_s390_interrupt *s390int)
923{
924 struct kvm_s390_interrupt_info *inti;
925
926 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
927 if (!inti)
928 return -ENOMEM;
929
930 inti->type = s390int->type;
931 switch (inti->type) {
932 case KVM_S390_INT_VIRTIO:
933 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
934 s390int->parm, s390int->parm64);
935 inti->ext.ext_params = s390int->parm;
936 inti->ext.ext_params2 = s390int->parm64;
937 break;
938 case KVM_S390_INT_SERVICE:
939 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
940 inti->ext.ext_params = s390int->parm;
941 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200942 case KVM_S390_INT_PFAULT_DONE:
943 inti->type = s390int->type;
944 inti->ext.ext_params2 = s390int->parm64;
945 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200946 case KVM_S390_MCHK:
947 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
948 s390int->parm64);
949 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
950 inti->mchk.mcic = s390int->parm64;
951 break;
952 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
953 if (inti->type & IOINT_AI_MASK)
954 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
955 else
956 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
957 s390int->type & IOINT_CSSID_MASK,
958 s390int->type & IOINT_SSID_MASK,
959 s390int->type & IOINT_SCHID_MASK);
960 inti->io.subchannel_id = s390int->parm >> 16;
961 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
962 inti->io.io_int_parm = s390int->parm64 >> 32;
963 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
964 break;
965 default:
966 kfree(inti);
967 return -EINVAL;
968 }
969 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
970 2);
971
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100972 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100973}
974
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100975void kvm_s390_reinject_io_int(struct kvm *kvm,
976 struct kvm_s390_interrupt_info *inti)
977{
978 __inject_vm(kvm, inti);
979}
980
Carsten Otteba5c1e92008-03-25 18:47:26 +0100981int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
982 struct kvm_s390_interrupt *s390int)
983{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200984 struct kvm_s390_local_interrupt *li;
985 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100986
987 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
988 if (!inti)
989 return -ENOMEM;
990
991 switch (s390int->type) {
992 case KVM_S390_PROGRAM_INT:
993 if (s390int->parm & 0xffff0000) {
994 kfree(inti);
995 return -EINVAL;
996 }
997 inti->type = s390int->type;
998 inti->pgm.code = s390int->parm;
999 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
1000 s390int->parm);
1001 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001002 case KVM_S390_SIGP_SET_PREFIX:
1003 inti->prefix.address = s390int->parm;
1004 inti->type = s390int->type;
1005 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
1006 s390int->parm);
1007 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001008 case KVM_S390_SIGP_STOP:
1009 case KVM_S390_RESTART:
Thomas Huthe029ae52014-03-26 16:11:54 +01001010 case KVM_S390_INT_CLOCK_COMP:
1011 case KVM_S390_INT_CPU_TIMER:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001012 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
1013 inti->type = s390int->type;
1014 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001015 case KVM_S390_INT_EXTERNAL_CALL:
1016 if (s390int->parm & 0xffff0000) {
1017 kfree(inti);
1018 return -EINVAL;
1019 }
1020 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
1021 s390int->parm);
1022 inti->type = s390int->type;
1023 inti->extcall.code = s390int->parm;
1024 break;
1025 case KVM_S390_INT_EMERGENCY:
1026 if (s390int->parm & 0xffff0000) {
1027 kfree(inti);
1028 return -EINVAL;
1029 }
1030 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
1031 inti->type = s390int->type;
1032 inti->emerg.code = s390int->parm;
1033 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001034 case KVM_S390_MCHK:
1035 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1036 s390int->parm64);
1037 inti->type = s390int->type;
1038 inti->mchk.mcic = s390int->parm64;
1039 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001040 case KVM_S390_INT_PFAULT_INIT:
1041 inti->type = s390int->type;
1042 inti->ext.ext_params2 = s390int->parm64;
1043 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001044 case KVM_S390_INT_VIRTIO:
1045 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001046 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001047 default:
1048 kfree(inti);
1049 return -EINVAL;
1050 }
Cornelia Huckade38c32012-07-23 17:20:30 +02001051 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
1052 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001053
1054 mutex_lock(&vcpu->kvm->lock);
1055 li = &vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001056 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001057 if (inti->type == KVM_S390_PROGRAM_INT)
1058 list_add(&inti->list, &li->list);
1059 else
1060 list_add_tail(&inti->list, &li->list);
1061 atomic_set(&li->active, 1);
1062 if (inti->type == KVM_S390_SIGP_STOP)
1063 li->action_bits |= ACTION_STOP_ON_STOP;
1064 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001065 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001066 mutex_unlock(&vcpu->kvm->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001067 kvm_s390_vcpu_wakeup(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001068 return 0;
1069}
Jens Freimannc05c4182013-10-07 16:13:45 +02001070
Christian Borntraeger67335e62014-03-25 17:09:08 +01001071void kvm_s390_clear_float_irqs(struct kvm *kvm)
Jens Freimannc05c4182013-10-07 16:13:45 +02001072{
1073 struct kvm_s390_float_interrupt *fi;
1074 struct kvm_s390_interrupt_info *n, *inti = NULL;
1075
1076 mutex_lock(&kvm->lock);
1077 fi = &kvm->arch.float_int;
1078 spin_lock(&fi->lock);
1079 list_for_each_entry_safe(inti, n, &fi->list, list) {
1080 list_del(&inti->list);
1081 kfree(inti);
1082 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001083 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001084 atomic_set(&fi->active, 0);
1085 spin_unlock(&fi->lock);
1086 mutex_unlock(&kvm->lock);
1087}
1088
1089static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1090 u8 *addr)
1091{
1092 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1093 struct kvm_s390_irq irq = {0};
1094
1095 irq.type = inti->type;
1096 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001097 case KVM_S390_INT_PFAULT_INIT:
1098 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001099 case KVM_S390_INT_VIRTIO:
1100 case KVM_S390_INT_SERVICE:
1101 irq.u.ext = inti->ext;
1102 break;
1103 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1104 irq.u.io = inti->io;
1105 break;
1106 case KVM_S390_MCHK:
1107 irq.u.mchk = inti->mchk;
1108 break;
1109 default:
1110 return -EINVAL;
1111 }
1112
1113 if (copy_to_user(uptr, &irq, sizeof(irq)))
1114 return -EFAULT;
1115
1116 return 0;
1117}
1118
1119static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1120{
1121 struct kvm_s390_interrupt_info *inti;
1122 struct kvm_s390_float_interrupt *fi;
1123 int ret = 0;
1124 int n = 0;
1125
1126 mutex_lock(&kvm->lock);
1127 fi = &kvm->arch.float_int;
1128 spin_lock(&fi->lock);
1129
1130 list_for_each_entry(inti, &fi->list, list) {
1131 if (len < sizeof(struct kvm_s390_irq)) {
1132 /* signal userspace to try again */
1133 ret = -ENOMEM;
1134 break;
1135 }
1136 ret = copy_irq_to_user(inti, buf);
1137 if (ret)
1138 break;
1139 buf += sizeof(struct kvm_s390_irq);
1140 len -= sizeof(struct kvm_s390_irq);
1141 n++;
1142 }
1143
1144 spin_unlock(&fi->lock);
1145 mutex_unlock(&kvm->lock);
1146
1147 return ret < 0 ? ret : n;
1148}
1149
1150static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1151{
1152 int r;
1153
1154 switch (attr->group) {
1155 case KVM_DEV_FLIC_GET_ALL_IRQS:
1156 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1157 attr->attr);
1158 break;
1159 default:
1160 r = -EINVAL;
1161 }
1162
1163 return r;
1164}
1165
1166static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1167 u64 addr)
1168{
1169 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1170 void *target = NULL;
1171 void __user *source;
1172 u64 size;
1173
1174 if (get_user(inti->type, (u64 __user *)addr))
1175 return -EFAULT;
1176
1177 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001178 case KVM_S390_INT_PFAULT_INIT:
1179 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001180 case KVM_S390_INT_VIRTIO:
1181 case KVM_S390_INT_SERVICE:
1182 target = (void *) &inti->ext;
1183 source = &uptr->u.ext;
1184 size = sizeof(inti->ext);
1185 break;
1186 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1187 target = (void *) &inti->io;
1188 source = &uptr->u.io;
1189 size = sizeof(inti->io);
1190 break;
1191 case KVM_S390_MCHK:
1192 target = (void *) &inti->mchk;
1193 source = &uptr->u.mchk;
1194 size = sizeof(inti->mchk);
1195 break;
1196 default:
1197 return -EINVAL;
1198 }
1199
1200 if (copy_from_user(target, source, size))
1201 return -EFAULT;
1202
1203 return 0;
1204}
1205
1206static int enqueue_floating_irq(struct kvm_device *dev,
1207 struct kvm_device_attr *attr)
1208{
1209 struct kvm_s390_interrupt_info *inti = NULL;
1210 int r = 0;
1211 int len = attr->attr;
1212
1213 if (len % sizeof(struct kvm_s390_irq) != 0)
1214 return -EINVAL;
1215 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1216 return -EINVAL;
1217
1218 while (len >= sizeof(struct kvm_s390_irq)) {
1219 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1220 if (!inti)
1221 return -ENOMEM;
1222
1223 r = copy_irq_from_user(inti, attr->addr);
1224 if (r) {
1225 kfree(inti);
1226 return r;
1227 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001228 r = __inject_vm(dev->kvm, inti);
1229 if (r) {
1230 kfree(inti);
1231 return r;
1232 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001233 len -= sizeof(struct kvm_s390_irq);
1234 attr->addr += sizeof(struct kvm_s390_irq);
1235 }
1236
1237 return r;
1238}
1239
Cornelia Huck841b91c2013-07-15 13:36:01 +02001240static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1241{
1242 if (id >= MAX_S390_IO_ADAPTERS)
1243 return NULL;
1244 return kvm->arch.adapters[id];
1245}
1246
1247static int register_io_adapter(struct kvm_device *dev,
1248 struct kvm_device_attr *attr)
1249{
1250 struct s390_io_adapter *adapter;
1251 struct kvm_s390_io_adapter adapter_info;
1252
1253 if (copy_from_user(&adapter_info,
1254 (void __user *)attr->addr, sizeof(adapter_info)))
1255 return -EFAULT;
1256
1257 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1258 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1259 return -EINVAL;
1260
1261 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1262 if (!adapter)
1263 return -ENOMEM;
1264
1265 INIT_LIST_HEAD(&adapter->maps);
1266 init_rwsem(&adapter->maps_lock);
1267 atomic_set(&adapter->nr_maps, 0);
1268 adapter->id = adapter_info.id;
1269 adapter->isc = adapter_info.isc;
1270 adapter->maskable = adapter_info.maskable;
1271 adapter->masked = false;
1272 adapter->swap = adapter_info.swap;
1273 dev->kvm->arch.adapters[adapter->id] = adapter;
1274
1275 return 0;
1276}
1277
1278int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1279{
1280 int ret;
1281 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1282
1283 if (!adapter || !adapter->maskable)
1284 return -EINVAL;
1285 ret = adapter->masked;
1286 adapter->masked = masked;
1287 return ret;
1288}
1289
1290static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1291{
1292 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1293 struct s390_map_info *map;
1294 int ret;
1295
1296 if (!adapter || !addr)
1297 return -EINVAL;
1298
1299 map = kzalloc(sizeof(*map), GFP_KERNEL);
1300 if (!map) {
1301 ret = -ENOMEM;
1302 goto out;
1303 }
1304 INIT_LIST_HEAD(&map->list);
1305 map->guest_addr = addr;
1306 map->addr = gmap_translate(addr, kvm->arch.gmap);
1307 if (map->addr == -EFAULT) {
1308 ret = -EFAULT;
1309 goto out;
1310 }
1311 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1312 if (ret < 0)
1313 goto out;
1314 BUG_ON(ret != 1);
1315 down_write(&adapter->maps_lock);
1316 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1317 list_add_tail(&map->list, &adapter->maps);
1318 ret = 0;
1319 } else {
1320 put_page(map->page);
1321 ret = -EINVAL;
1322 }
1323 up_write(&adapter->maps_lock);
1324out:
1325 if (ret)
1326 kfree(map);
1327 return ret;
1328}
1329
1330static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1331{
1332 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1333 struct s390_map_info *map, *tmp;
1334 int found = 0;
1335
1336 if (!adapter || !addr)
1337 return -EINVAL;
1338
1339 down_write(&adapter->maps_lock);
1340 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1341 if (map->guest_addr == addr) {
1342 found = 1;
1343 atomic_dec(&adapter->nr_maps);
1344 list_del(&map->list);
1345 put_page(map->page);
1346 kfree(map);
1347 break;
1348 }
1349 }
1350 up_write(&adapter->maps_lock);
1351
1352 return found ? 0 : -EINVAL;
1353}
1354
1355void kvm_s390_destroy_adapters(struct kvm *kvm)
1356{
1357 int i;
1358 struct s390_map_info *map, *tmp;
1359
1360 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1361 if (!kvm->arch.adapters[i])
1362 continue;
1363 list_for_each_entry_safe(map, tmp,
1364 &kvm->arch.adapters[i]->maps, list) {
1365 list_del(&map->list);
1366 put_page(map->page);
1367 kfree(map);
1368 }
1369 kfree(kvm->arch.adapters[i]);
1370 }
1371}
1372
1373static int modify_io_adapter(struct kvm_device *dev,
1374 struct kvm_device_attr *attr)
1375{
1376 struct kvm_s390_io_adapter_req req;
1377 struct s390_io_adapter *adapter;
1378 int ret;
1379
1380 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1381 return -EFAULT;
1382
1383 adapter = get_io_adapter(dev->kvm, req.id);
1384 if (!adapter)
1385 return -EINVAL;
1386 switch (req.type) {
1387 case KVM_S390_IO_ADAPTER_MASK:
1388 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1389 if (ret > 0)
1390 ret = 0;
1391 break;
1392 case KVM_S390_IO_ADAPTER_MAP:
1393 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1394 break;
1395 case KVM_S390_IO_ADAPTER_UNMAP:
1396 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1397 break;
1398 default:
1399 ret = -EINVAL;
1400 }
1401
1402 return ret;
1403}
1404
Jens Freimannc05c4182013-10-07 16:13:45 +02001405static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1406{
1407 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001408 unsigned int i;
1409 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001410
1411 switch (attr->group) {
1412 case KVM_DEV_FLIC_ENQUEUE:
1413 r = enqueue_floating_irq(dev, attr);
1414 break;
1415 case KVM_DEV_FLIC_CLEAR_IRQS:
1416 r = 0;
Christian Borntraeger67335e62014-03-25 17:09:08 +01001417 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001418 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001419 case KVM_DEV_FLIC_APF_ENABLE:
1420 dev->kvm->arch.gmap->pfault_enabled = 1;
1421 break;
1422 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1423 dev->kvm->arch.gmap->pfault_enabled = 0;
1424 /*
1425 * Make sure no async faults are in transition when
1426 * clearing the queues. So we don't need to worry
1427 * about late coming workers.
1428 */
1429 synchronize_srcu(&dev->kvm->srcu);
1430 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1431 kvm_clear_async_pf_completion_queue(vcpu);
1432 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001433 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1434 r = register_io_adapter(dev, attr);
1435 break;
1436 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1437 r = modify_io_adapter(dev, attr);
1438 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001439 default:
1440 r = -EINVAL;
1441 }
1442
1443 return r;
1444}
1445
1446static int flic_create(struct kvm_device *dev, u32 type)
1447{
1448 if (!dev)
1449 return -EINVAL;
1450 if (dev->kvm->arch.flic)
1451 return -EINVAL;
1452 dev->kvm->arch.flic = dev;
1453 return 0;
1454}
1455
1456static void flic_destroy(struct kvm_device *dev)
1457{
1458 dev->kvm->arch.flic = NULL;
1459 kfree(dev);
1460}
1461
1462/* s390 floating irq controller (flic) */
1463struct kvm_device_ops kvm_flic_ops = {
1464 .name = "kvm-flic",
1465 .get_attr = flic_get_attr,
1466 .set_attr = flic_set_attr,
1467 .create = flic_create,
1468 .destroy = flic_destroy,
1469};
Cornelia Huck84223592013-07-15 13:36:01 +02001470
1471static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1472{
1473 unsigned long bit;
1474
1475 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1476
1477 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1478}
1479
1480static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1481 u64 addr)
1482{
1483 struct s390_map_info *map;
1484
1485 if (!adapter)
1486 return NULL;
1487
1488 list_for_each_entry(map, &adapter->maps, list) {
1489 if (map->guest_addr == addr)
1490 return map;
1491 }
1492 return NULL;
1493}
1494
1495static int adapter_indicators_set(struct kvm *kvm,
1496 struct s390_io_adapter *adapter,
1497 struct kvm_s390_adapter_int *adapter_int)
1498{
1499 unsigned long bit;
1500 int summary_set, idx;
1501 struct s390_map_info *info;
1502 void *map;
1503
1504 info = get_map_info(adapter, adapter_int->ind_addr);
1505 if (!info)
1506 return -1;
1507 map = page_address(info->page);
1508 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1509 set_bit(bit, map);
1510 idx = srcu_read_lock(&kvm->srcu);
1511 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1512 set_page_dirty_lock(info->page);
1513 info = get_map_info(adapter, adapter_int->summary_addr);
1514 if (!info) {
1515 srcu_read_unlock(&kvm->srcu, idx);
1516 return -1;
1517 }
1518 map = page_address(info->page);
1519 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1520 adapter->swap);
1521 summary_set = test_and_set_bit(bit, map);
1522 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1523 set_page_dirty_lock(info->page);
1524 srcu_read_unlock(&kvm->srcu, idx);
1525 return summary_set ? 0 : 1;
1526}
1527
1528/*
1529 * < 0 - not injected due to error
1530 * = 0 - coalesced, summary indicator already active
1531 * > 0 - injected interrupt
1532 */
1533static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1534 struct kvm *kvm, int irq_source_id, int level,
1535 bool line_status)
1536{
1537 int ret;
1538 struct s390_io_adapter *adapter;
1539
1540 /* We're only interested in the 0->1 transition. */
1541 if (!level)
1542 return 0;
1543 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1544 if (!adapter)
1545 return -1;
1546 down_read(&adapter->maps_lock);
1547 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1548 up_read(&adapter->maps_lock);
1549 if ((ret > 0) && !adapter->masked) {
1550 struct kvm_s390_interrupt s390int = {
1551 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1552 .parm = 0,
1553 .parm64 = (adapter->isc << 27) | 0x80000000,
1554 };
1555 ret = kvm_s390_inject_vm(kvm, &s390int);
1556 if (ret == 0)
1557 ret = 1;
1558 }
1559 return ret;
1560}
1561
Paul Mackerras8ba918d2014-06-30 20:51:10 +10001562int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02001563 const struct kvm_irq_routing_entry *ue)
1564{
1565 int ret;
1566
1567 switch (ue->type) {
1568 case KVM_IRQ_ROUTING_S390_ADAPTER:
1569 e->set = set_adapter_int;
1570 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1571 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1572 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1573 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1574 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1575 ret = 0;
1576 break;
1577 default:
1578 ret = -EINVAL;
1579 }
1580
1581 return ret;
1582}
1583
1584int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1585 int irq_source_id, int level, bool line_status)
1586{
1587 return -EINVAL;
1588}