blob: 1be3d8da49e91d6fb16807e459a8867a9d7be879 [file] [log] [blame]
Carsten Otteba5c1e92008-03-25 18:47:26 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling kvm guest interrupts
Carsten Otteba5c1e92008-03-25 18:47:26 +01003 *
Cornelia Huck841b91c2013-07-15 13:36:01 +02004 * Copyright IBM Corp. 2008,2014
Carsten Otteba5c1e92008-03-25 18:47:26 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 */
12
Christian Borntraegerca872302009-05-12 17:21:49 +020013#include <linux/interrupt.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010014#include <linux/kvm_host.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010015#include <linux/hrtimer.h>
Cornelia Huck84223592013-07-15 13:36:01 +020016#include <linux/mmu_context.h>
Christian Borntraeger3cd61292008-07-25 15:51:54 +020017#include <linux/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010019#include <asm/asm-offsets.h>
20#include <asm/uaccess.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010021#include "kvm-s390.h"
22#include "gaccess.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020023#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010024
Cornelia Huckd8346b72012-12-20 15:32:08 +010025#define IOINT_SCHID_MASK 0x0000ffff
26#define IOINT_SSID_MASK 0x00030000
27#define IOINT_CSSID_MASK 0x03fc0000
28#define IOINT_AI_MASK 0x04000000
29
Thomas Huthe029ae52014-03-26 16:11:54 +010030static void deliver_ckc_interrupt(struct kvm_vcpu *vcpu);
31
Cornelia Huckd8346b72012-12-20 15:32:08 +010032static int is_ioint(u64 type)
33{
34 return ((type & 0xfffe0000u) != 0xfffe0000u);
35}
36
Dominik Dingel3c038e62013-10-07 17:11:48 +020037int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010038{
39 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
40}
41
Cornelia Huckd8346b72012-12-20 15:32:08 +010042static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
43{
44 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
45}
46
Cornelia Huck48a3e952012-12-20 15:32:09 +010047static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
48{
49 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
50}
51
Carsten Otteba5c1e92008-03-25 18:47:26 +010052static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
53{
54 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
55 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
56 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
57 return 0;
58 return 1;
59}
60
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010061static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
62{
63 if (psw_extint_disabled(vcpu) ||
64 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
65 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +010066 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
67 /* No timer interrupts when single stepping */
68 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010069 return 1;
70}
71
Cornelia Huck79fd50c2013-02-07 13:20:52 +010072static u64 int_word_to_isc_bits(u32 int_word)
73{
74 u8 isc = (int_word & 0x38000000) >> 27;
75
76 return (0x80 >> isc) << 24;
77}
78
Carsten Otteba5c1e92008-03-25 18:47:26 +010079static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020080 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010081{
82 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020083 case KVM_S390_INT_EXTERNAL_CALL:
84 if (psw_extint_disabled(vcpu))
85 return 0;
86 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
87 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +010088 case KVM_S390_INT_EMERGENCY:
89 if (psw_extint_disabled(vcpu))
90 return 0;
91 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
92 return 1;
93 return 0;
Thomas Huthe029ae52014-03-26 16:11:54 +010094 case KVM_S390_INT_CLOCK_COMP:
95 return ckc_interrupts_enabled(vcpu);
96 case KVM_S390_INT_CPU_TIMER:
97 if (psw_extint_disabled(vcpu))
98 return 0;
99 if (vcpu->arch.sie_block->gcr[0] & 0x400ul)
100 return 1;
101 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100102 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200103 case KVM_S390_INT_PFAULT_INIT:
104 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100105 case KVM_S390_INT_VIRTIO:
106 if (psw_extint_disabled(vcpu))
107 return 0;
108 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
109 return 1;
110 return 0;
111 case KVM_S390_PROGRAM_INT:
112 case KVM_S390_SIGP_STOP:
113 case KVM_S390_SIGP_SET_PREFIX:
114 case KVM_S390_RESTART:
115 return 1;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100116 case KVM_S390_MCHK:
117 if (psw_mchk_disabled(vcpu))
118 return 0;
119 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
120 return 1;
121 return 0;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100122 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
123 if (psw_ioint_disabled(vcpu))
124 return 0;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100125 if (vcpu->arch.sie_block->gcr[6] &
126 int_word_to_isc_bits(inti->io.io_int_word))
Cornelia Huckd8346b72012-12-20 15:32:08 +0100127 return 1;
128 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100129 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100130 printk(KERN_WARNING "illegal interrupt type %llx\n",
131 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100132 BUG();
133 }
134 return 0;
135}
136
137static void __set_cpu_idle(struct kvm_vcpu *vcpu)
138{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100139 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
140 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
141}
142
143static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
144{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100145 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
146 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
147}
148
149static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
150{
David Hildenbrand49539192014-02-21 08:59:59 +0100151 atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
152 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100153 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100154 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
155
156 if (guestdbg_enabled(vcpu)) {
157 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
158 LCTL_CR10 | LCTL_CR11);
159 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
160 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100161}
162
163static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
164{
165 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
166}
167
168static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200169 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100170{
171 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200172 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100173 case KVM_S390_INT_EMERGENCY:
174 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200175 case KVM_S390_INT_PFAULT_INIT:
176 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100177 case KVM_S390_INT_VIRTIO:
Thomas Huthe029ae52014-03-26 16:11:54 +0100178 case KVM_S390_INT_CLOCK_COMP:
179 case KVM_S390_INT_CPU_TIMER:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100180 if (psw_extint_disabled(vcpu))
181 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
182 else
183 vcpu->arch.sie_block->lctl |= LCTL_CR0;
184 break;
185 case KVM_S390_SIGP_STOP:
186 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
187 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100188 case KVM_S390_MCHK:
189 if (psw_mchk_disabled(vcpu))
190 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
191 else
192 vcpu->arch.sie_block->lctl |= LCTL_CR14;
193 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100194 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
195 if (psw_ioint_disabled(vcpu))
196 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
197 else
198 vcpu->arch.sie_block->lctl |= LCTL_CR6;
199 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100200 default:
201 BUG();
202 }
203}
204
David Hildenbrand87128362014-03-03 10:55:13 +0100205static int __deliver_prog_irq(struct kvm_vcpu *vcpu,
206 struct kvm_s390_pgm_info *pgm_info)
207{
208 const unsigned short table[] = { 2, 4, 4, 6 };
209 int rc = 0;
210
211 switch (pgm_info->code & ~PGM_PER) {
212 case PGM_AFX_TRANSLATION:
213 case PGM_ASX_TRANSLATION:
214 case PGM_EX_TRANSLATION:
215 case PGM_LFX_TRANSLATION:
216 case PGM_LSTE_SEQUENCE:
217 case PGM_LSX_TRANSLATION:
218 case PGM_LX_TRANSLATION:
219 case PGM_PRIMARY_AUTHORITY:
220 case PGM_SECONDARY_AUTHORITY:
221 case PGM_SPACE_SWITCH:
222 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
223 (u64 *)__LC_TRANS_EXC_CODE);
224 break;
225 case PGM_ALEN_TRANSLATION:
226 case PGM_ALE_SEQUENCE:
227 case PGM_ASTE_INSTANCE:
228 case PGM_ASTE_SEQUENCE:
229 case PGM_ASTE_VALIDITY:
230 case PGM_EXTENDED_AUTHORITY:
231 rc = put_guest_lc(vcpu, pgm_info->exc_access_id,
232 (u8 *)__LC_EXC_ACCESS_ID);
233 break;
234 case PGM_ASCE_TYPE:
235 case PGM_PAGE_TRANSLATION:
236 case PGM_REGION_FIRST_TRANS:
237 case PGM_REGION_SECOND_TRANS:
238 case PGM_REGION_THIRD_TRANS:
239 case PGM_SEGMENT_TRANSLATION:
240 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
241 (u64 *)__LC_TRANS_EXC_CODE);
242 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
243 (u8 *)__LC_EXC_ACCESS_ID);
244 rc |= put_guest_lc(vcpu, pgm_info->op_access_id,
245 (u8 *)__LC_OP_ACCESS_ID);
246 break;
247 case PGM_MONITOR:
248 rc = put_guest_lc(vcpu, pgm_info->mon_class_nr,
249 (u64 *)__LC_MON_CLASS_NR);
250 rc |= put_guest_lc(vcpu, pgm_info->mon_code,
251 (u64 *)__LC_MON_CODE);
252 break;
253 case PGM_DATA:
254 rc = put_guest_lc(vcpu, pgm_info->data_exc_code,
255 (u32 *)__LC_DATA_EXC_CODE);
256 break;
257 case PGM_PROTECTION:
258 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
259 (u64 *)__LC_TRANS_EXC_CODE);
260 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
261 (u8 *)__LC_EXC_ACCESS_ID);
262 break;
263 }
264
265 if (pgm_info->code & PGM_PER) {
266 rc |= put_guest_lc(vcpu, pgm_info->per_code,
267 (u8 *) __LC_PER_CODE);
268 rc |= put_guest_lc(vcpu, pgm_info->per_atmid,
269 (u8 *)__LC_PER_ATMID);
270 rc |= put_guest_lc(vcpu, pgm_info->per_address,
271 (u64 *) __LC_PER_ADDRESS);
272 rc |= put_guest_lc(vcpu, pgm_info->per_access_id,
273 (u8 *) __LC_PER_ACCESS_ID);
274 }
275
276 switch (vcpu->arch.sie_block->icptcode) {
277 case ICPT_INST:
278 case ICPT_INSTPROGI:
279 case ICPT_OPEREXC:
280 case ICPT_PARTEXEC:
281 case ICPT_IOINST:
282 /* last instruction only stored for these icptcodes */
283 rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
284 (u16 *) __LC_PGM_ILC);
285 break;
286 case ICPT_PROGI:
287 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->pgmilc,
288 (u16 *) __LC_PGM_ILC);
289 break;
290 default:
291 rc |= put_guest_lc(vcpu, 0,
292 (u16 *) __LC_PGM_ILC);
293 }
294
295 rc |= put_guest_lc(vcpu, pgm_info->code,
296 (u16 *)__LC_PGM_INT_CODE);
297 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
298 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
299 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
300 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
301
302 return rc;
303}
304
Carsten Otteba5c1e92008-03-25 18:47:26 +0100305static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200306 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100307{
308 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100309 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100310
311 switch (inti->type) {
312 case KVM_S390_INT_EMERGENCY:
313 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
314 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200315 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
316 inti->emerg.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100317 rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE);
318 rc |= put_guest_lc(vcpu, inti->emerg.code,
319 (u16 *)__LC_EXT_CPU_ADDR);
320 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
321 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
322 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100323 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100324 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200325 case KVM_S390_INT_EXTERNAL_CALL:
326 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
327 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200328 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
329 inti->extcall.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100330 rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE);
331 rc |= put_guest_lc(vcpu, inti->extcall.code,
332 (u16 *)__LC_EXT_CPU_ADDR);
333 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
334 &vcpu->arch.sie_block->gpsw,
335 sizeof(psw_t));
336 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
337 &vcpu->arch.sie_block->gpsw,
338 sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200339 break;
Thomas Huthe029ae52014-03-26 16:11:54 +0100340 case KVM_S390_INT_CLOCK_COMP:
341 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
342 inti->ext.ext_params, 0);
343 deliver_ckc_interrupt(vcpu);
344 break;
345 case KVM_S390_INT_CPU_TIMER:
346 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
347 inti->ext.ext_params, 0);
348 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
349 (u16 *)__LC_EXT_INT_CODE);
350 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
351 &vcpu->arch.sie_block->gpsw,
352 sizeof(psw_t));
353 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
354 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
355 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
356 (u32 *)__LC_EXT_PARAMS);
357 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100358 case KVM_S390_INT_SERVICE:
359 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
360 inti->ext.ext_params);
361 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200362 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
363 inti->ext.ext_params, 0);
Heiko Carstens79882762014-01-02 10:59:41 +0100364 rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE);
365 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
366 &vcpu->arch.sie_block->gpsw,
367 sizeof(psw_t));
368 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100369 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100370 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
371 (u32 *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100372 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200373 case KVM_S390_INT_PFAULT_INIT:
374 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
375 inti->ext.ext_params2);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100376 rc = put_guest_lc(vcpu, 0x2603, (u16 *) __LC_EXT_INT_CODE);
377 rc |= put_guest_lc(vcpu, 0x0600, (u16 *) __LC_EXT_CPU_ADDR);
378 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
379 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
380 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200381 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann1a03b7642014-02-12 14:05:38 +0100382 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
383 (u64 *) __LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200384 break;
385 case KVM_S390_INT_PFAULT_DONE:
386 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
387 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100388 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
389 rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR);
390 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
391 &vcpu->arch.sie_block->gpsw,
392 sizeof(psw_t));
393 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200394 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100395 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
396 (u64 *)__LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200397 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100398 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100399 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100400 inti->ext.ext_params, inti->ext.ext_params2);
401 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200402 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
403 inti->ext.ext_params,
404 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100405 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
406 rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR);
407 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
408 &vcpu->arch.sie_block->gpsw,
409 sizeof(psw_t));
410 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100411 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100412 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
413 (u32 *)__LC_EXT_PARAMS);
414 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
415 (u64 *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100416 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100417 case KVM_S390_SIGP_STOP:
418 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
419 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200420 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
421 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100422 __set_intercept_indicator(vcpu, inti);
423 break;
424
425 case KVM_S390_SIGP_SET_PREFIX:
426 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
427 inti->prefix.address);
428 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200429 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
430 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100431 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100432 break;
433
434 case KVM_S390_RESTART:
435 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
436 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200437 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
438 0, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100439 rc = write_guest_lc(vcpu,
440 offsetof(struct _lowcore, restart_old_psw),
441 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
442 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
443 &vcpu->arch.sie_block->gpsw,
444 sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100445 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100446 case KVM_S390_PROGRAM_INT:
447 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
448 inti->pgm.code,
449 table[vcpu->arch.sie_block->ipa >> 14]);
450 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200451 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
452 inti->pgm.code, 0);
David Hildenbrand87128362014-03-03 10:55:13 +0100453 rc = __deliver_prog_irq(vcpu, &inti->pgm);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100454 break;
455
Cornelia Huck48a3e952012-12-20 15:32:09 +0100456 case KVM_S390_MCHK:
457 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
458 inti->mchk.mcic);
459 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
460 inti->mchk.cr14,
461 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100462 rc = kvm_s390_vcpu_store_status(vcpu,
463 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens79882762014-01-02 10:59:41 +0100464 rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE);
465 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
466 &vcpu->arch.sie_block->gpsw,
467 sizeof(psw_t));
468 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100469 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100470 break;
471
Cornelia Huckd8346b72012-12-20 15:32:08 +0100472 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
473 {
474 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
475 inti->io.subchannel_nr;
476 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
477 inti->io.io_int_word;
478 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
479 vcpu->stat.deliver_io_int++;
480 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
481 param0, param1);
Heiko Carstens79882762014-01-02 10:59:41 +0100482 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
483 (u16 *)__LC_SUBCHANNEL_ID);
484 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
485 (u16 *)__LC_SUBCHANNEL_NR);
486 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
487 (u32 *)__LC_IO_INT_PARM);
488 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
489 (u32 *)__LC_IO_INT_WORD);
490 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
491 &vcpu->arch.sie_block->gpsw,
492 sizeof(psw_t));
493 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
494 &vcpu->arch.sie_block->gpsw,
495 sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100496 break;
497 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100498 default:
499 BUG();
500 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100501 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200502 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100503 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200504 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100505 }
506}
507
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100508static void deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100509{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100510 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100511
Jens Freimann1a03b7642014-02-12 14:05:38 +0100512 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
513 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
514 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
515 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
516 &vcpu->arch.sie_block->gpsw,
517 sizeof(psw_t));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100518 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200519 printk("kvm: The guest lowcore is not mapped during interrupt "
520 "delivery, killing userspace\n");
521 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100522 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100523}
524
David Hildenbrand49539192014-02-21 08:59:59 +0100525/* Check whether SIGP interpretation facility has an external call pending */
526int kvm_s390_si_ext_call_pending(struct kvm_vcpu *vcpu)
527{
528 atomic_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].ctrl;
529
530 if (!psw_extint_disabled(vcpu) &&
531 (vcpu->arch.sie_block->gcr[0] & 0x2000ul) &&
532 (atomic_read(sigp_ctrl) & SIGP_CTRL_C) &&
533 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND))
534 return 1;
535
536 return 0;
537}
538
Dominik Dingel3c038e62013-10-07 17:11:48 +0200539int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100540{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200541 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
542 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
543 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100544 int rc = 0;
545
546 if (atomic_read(&li->active)) {
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200547 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100548 list_for_each_entry(inti, &li->list, list)
549 if (__interrupt_is_deliverable(vcpu, inti)) {
550 rc = 1;
551 break;
552 }
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200553 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100554 }
555
556 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200557 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100558 list_for_each_entry(inti, &fi->list, list)
559 if (__interrupt_is_deliverable(vcpu, inti)) {
560 rc = 1;
561 break;
562 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200563 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100564 }
565
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100566 if (!rc && kvm_cpu_has_pending_timer(vcpu))
567 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100568
David Hildenbrand49539192014-02-21 08:59:59 +0100569 if (!rc && kvm_s390_si_ext_call_pending(vcpu))
570 rc = 1;
571
Carsten Otteba5c1e92008-03-25 18:47:26 +0100572 return rc;
573}
574
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300575int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
576{
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100577 if (!(vcpu->arch.sie_block->ckc <
578 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
579 return 0;
580 if (!ckc_interrupts_enabled(vcpu))
581 return 0;
582 return 1;
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300583}
584
Carsten Otteba5c1e92008-03-25 18:47:26 +0100585int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
586{
587 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100588
589 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100590
David Hildenbrand0759d062014-05-13 16:54:32 +0200591 /* fast path */
592 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
593 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200594
Carsten Otteba5c1e92008-03-25 18:47:26 +0100595 if (psw_interrupts_disabled(vcpu)) {
596 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100597 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100598 }
599
David Hildenbrand0759d062014-05-13 16:54:32 +0200600 __set_cpu_idle(vcpu);
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100601 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100602 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
603 goto no_timer;
604 }
605
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200606 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Heiko Carstensed4f2092013-01-14 16:55:55 +0100607 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200608 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
609 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100610no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200611 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200612 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100613 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200614 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
615
Christian Borntraegerca872302009-05-12 17:21:49 +0200616 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100617 return 0;
618}
619
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200620void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
621{
622 if (waitqueue_active(&vcpu->wq)) {
623 /*
624 * The vcpu gave up the cpu voluntarily, mark it as a good
625 * yield-candidate.
626 */
627 vcpu->preempted = true;
628 wake_up_interruptible(&vcpu->wq);
629 }
630}
631
Christian Borntraegerca872302009-05-12 17:21:49 +0200632enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
633{
634 struct kvm_vcpu *vcpu;
635
636 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandea74c0e2014-05-16 12:08:29 +0200637 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200638
639 return HRTIMER_NORESTART;
640}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100641
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100642void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
643{
644 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
645 struct kvm_s390_interrupt_info *n, *inti = NULL;
646
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200647 spin_lock(&li->lock);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100648 list_for_each_entry_safe(inti, n, &li->list, list) {
649 list_del(&inti->list);
650 kfree(inti);
651 }
652 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200653 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100654
655 /* clear pending external calls set by sigp interpretation facility */
656 atomic_clear_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
657 atomic_clear_mask(SIGP_CTRL_C,
658 &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].ctrl);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100659}
660
Carsten Otteba5c1e92008-03-25 18:47:26 +0100661void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
662{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200663 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
664 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
665 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100666 int deliver;
667
668 __reset_intercept_indicators(vcpu);
669 if (atomic_read(&li->active)) {
670 do {
671 deliver = 0;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200672 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100673 list_for_each_entry_safe(inti, n, &li->list, list) {
674 if (__interrupt_is_deliverable(vcpu, inti)) {
675 list_del(&inti->list);
676 deliver = 1;
677 break;
678 }
679 __set_intercept_indicator(vcpu, inti);
680 }
681 if (list_empty(&li->list))
682 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200683 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100684 if (deliver) {
685 __do_deliver_interrupt(vcpu, inti);
686 kfree(inti);
687 }
688 } while (deliver);
689 }
690
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100691 if (kvm_cpu_has_pending_timer(vcpu))
692 deliver_ckc_interrupt(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100693
694 if (atomic_read(&fi->active)) {
695 do {
696 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200697 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100698 list_for_each_entry_safe(inti, n, &fi->list, list) {
699 if (__interrupt_is_deliverable(vcpu, inti)) {
700 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100701 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100702 deliver = 1;
703 break;
704 }
705 __set_intercept_indicator(vcpu, inti);
706 }
707 if (list_empty(&fi->list))
708 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200709 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100710 if (deliver) {
711 __do_deliver_interrupt(vcpu, inti);
712 kfree(inti);
713 }
714 } while (deliver);
715 }
716}
717
Cornelia Huck48a3e952012-12-20 15:32:09 +0100718void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
719{
720 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
721 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
722 struct kvm_s390_interrupt_info *n, *inti = NULL;
723 int deliver;
724
725 __reset_intercept_indicators(vcpu);
726 if (atomic_read(&li->active)) {
727 do {
728 deliver = 0;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200729 spin_lock(&li->lock);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100730 list_for_each_entry_safe(inti, n, &li->list, list) {
731 if ((inti->type == KVM_S390_MCHK) &&
732 __interrupt_is_deliverable(vcpu, inti)) {
733 list_del(&inti->list);
734 deliver = 1;
735 break;
736 }
737 __set_intercept_indicator(vcpu, inti);
738 }
739 if (list_empty(&li->list))
740 atomic_set(&li->active, 0);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200741 spin_unlock(&li->lock);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100742 if (deliver) {
743 __do_deliver_interrupt(vcpu, inti);
744 kfree(inti);
745 }
746 } while (deliver);
747 }
748
749 if (atomic_read(&fi->active)) {
750 do {
751 deliver = 0;
752 spin_lock(&fi->lock);
753 list_for_each_entry_safe(inti, n, &fi->list, list) {
754 if ((inti->type == KVM_S390_MCHK) &&
755 __interrupt_is_deliverable(vcpu, inti)) {
756 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100757 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100758 deliver = 1;
759 break;
760 }
761 __set_intercept_indicator(vcpu, inti);
762 }
763 if (list_empty(&fi->list))
764 atomic_set(&fi->active, 0);
765 spin_unlock(&fi->lock);
766 if (deliver) {
767 __do_deliver_interrupt(vcpu, inti);
768 kfree(inti);
769 }
770 } while (deliver);
771 }
772}
773
Carsten Otteba5c1e92008-03-25 18:47:26 +0100774int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
775{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200776 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
777 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100778
779 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
780 if (!inti)
781 return -ENOMEM;
782
Joe Perchesa419aef2009-08-18 11:18:35 -0700783 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100784 inti->pgm.code = code;
785
786 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200787 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200788 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100789 list_add(&inti->list, &li->list);
790 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200791 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200792 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100793 return 0;
794}
795
Jens Freimannbcd84682014-02-11 11:07:05 +0100796int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
797 struct kvm_s390_pgm_info *pgm_info)
798{
799 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
800 struct kvm_s390_interrupt_info *inti;
801
802 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
803 if (!inti)
804 return -ENOMEM;
805
806 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
807 pgm_info->code);
808 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
809 pgm_info->code, 0, 1);
810
811 inti->type = KVM_S390_PROGRAM_INT;
812 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200813 spin_lock(&li->lock);
Jens Freimannbcd84682014-02-11 11:07:05 +0100814 list_add(&inti->list, &li->list);
815 atomic_set(&li->active, 1);
816 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200817 spin_unlock(&li->lock);
Jens Freimannbcd84682014-02-11 11:07:05 +0100818 return 0;
819}
820
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100821struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
822 u64 cr6, u64 schid)
823{
824 struct kvm_s390_float_interrupt *fi;
825 struct kvm_s390_interrupt_info *inti, *iter;
826
827 if ((!schid && !cr6) || (schid && cr6))
828 return NULL;
829 mutex_lock(&kvm->lock);
830 fi = &kvm->arch.float_int;
831 spin_lock(&fi->lock);
832 inti = NULL;
833 list_for_each_entry(iter, &fi->list, list) {
834 if (!is_ioint(iter->type))
835 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100836 if (cr6 &&
837 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100838 continue;
839 if (schid) {
840 if (((schid & 0x00000000ffff0000) >> 16) !=
841 iter->io.subchannel_id)
842 continue;
843 if ((schid & 0x000000000000ffff) !=
844 iter->io.subchannel_nr)
845 continue;
846 }
847 inti = iter;
848 break;
849 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100850 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100851 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100852 fi->irq_count--;
853 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100854 if (list_empty(&fi->list))
855 atomic_set(&fi->active, 0);
856 spin_unlock(&fi->lock);
857 mutex_unlock(&kvm->lock);
858 return inti;
859}
860
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100861static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100862{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200863 struct kvm_s390_local_interrupt *li;
864 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200865 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100866 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100867 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100868 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100869
Carsten Otteba5c1e92008-03-25 18:47:26 +0100870 mutex_lock(&kvm->lock);
871 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200872 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100873 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
874 rc = -EINVAL;
875 goto unlock_fi;
876 }
877 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200878 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100879 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200880 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100881 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
882
Cornelia Huckd8346b72012-12-20 15:32:08 +0100883 /* Keep I/O interrupts sorted in isc order. */
884 list_for_each_entry(iter, &fi->list, list) {
885 if (!is_ioint(iter->type))
886 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100887 if (int_word_to_isc_bits(iter->io.io_int_word)
888 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100889 continue;
890 break;
891 }
892 list_add_tail(&inti->list, &iter->list);
893 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100894 atomic_set(&fi->active, 1);
895 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
896 if (sigcpu == KVM_MAX_VCPUS) {
897 do {
898 sigcpu = fi->next_rr_cpu++;
899 if (sigcpu == KVM_MAX_VCPUS)
900 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100901 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100902 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100903 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
904 li = &dst_vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200905 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100906 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200907 spin_unlock(&li->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200908 kvm_s390_vcpu_wakeup(kvm_get_vcpu(kvm, sigcpu));
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100909unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200910 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100911 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100912 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200913}
914
915int kvm_s390_inject_vm(struct kvm *kvm,
916 struct kvm_s390_interrupt *s390int)
917{
918 struct kvm_s390_interrupt_info *inti;
919
920 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
921 if (!inti)
922 return -ENOMEM;
923
924 inti->type = s390int->type;
925 switch (inti->type) {
926 case KVM_S390_INT_VIRTIO:
927 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
928 s390int->parm, s390int->parm64);
929 inti->ext.ext_params = s390int->parm;
930 inti->ext.ext_params2 = s390int->parm64;
931 break;
932 case KVM_S390_INT_SERVICE:
933 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
934 inti->ext.ext_params = s390int->parm;
935 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200936 case KVM_S390_INT_PFAULT_DONE:
937 inti->type = s390int->type;
938 inti->ext.ext_params2 = s390int->parm64;
939 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200940 case KVM_S390_MCHK:
941 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
942 s390int->parm64);
943 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
944 inti->mchk.mcic = s390int->parm64;
945 break;
946 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
947 if (inti->type & IOINT_AI_MASK)
948 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
949 else
950 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
951 s390int->type & IOINT_CSSID_MASK,
952 s390int->type & IOINT_SSID_MASK,
953 s390int->type & IOINT_SCHID_MASK);
954 inti->io.subchannel_id = s390int->parm >> 16;
955 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
956 inti->io.io_int_parm = s390int->parm64 >> 32;
957 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
958 break;
959 default:
960 kfree(inti);
961 return -EINVAL;
962 }
963 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
964 2);
965
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100966 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100967}
968
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100969void kvm_s390_reinject_io_int(struct kvm *kvm,
970 struct kvm_s390_interrupt_info *inti)
971{
972 __inject_vm(kvm, inti);
973}
974
Carsten Otteba5c1e92008-03-25 18:47:26 +0100975int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
976 struct kvm_s390_interrupt *s390int)
977{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200978 struct kvm_s390_local_interrupt *li;
979 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100980
981 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
982 if (!inti)
983 return -ENOMEM;
984
985 switch (s390int->type) {
986 case KVM_S390_PROGRAM_INT:
987 if (s390int->parm & 0xffff0000) {
988 kfree(inti);
989 return -EINVAL;
990 }
991 inti->type = s390int->type;
992 inti->pgm.code = s390int->parm;
993 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
994 s390int->parm);
995 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100996 case KVM_S390_SIGP_SET_PREFIX:
997 inti->prefix.address = s390int->parm;
998 inti->type = s390int->type;
999 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
1000 s390int->parm);
1001 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001002 case KVM_S390_SIGP_STOP:
1003 case KVM_S390_RESTART:
Thomas Huthe029ae52014-03-26 16:11:54 +01001004 case KVM_S390_INT_CLOCK_COMP:
1005 case KVM_S390_INT_CPU_TIMER:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001006 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
1007 inti->type = s390int->type;
1008 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001009 case KVM_S390_INT_EXTERNAL_CALL:
1010 if (s390int->parm & 0xffff0000) {
1011 kfree(inti);
1012 return -EINVAL;
1013 }
1014 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
1015 s390int->parm);
1016 inti->type = s390int->type;
1017 inti->extcall.code = s390int->parm;
1018 break;
1019 case KVM_S390_INT_EMERGENCY:
1020 if (s390int->parm & 0xffff0000) {
1021 kfree(inti);
1022 return -EINVAL;
1023 }
1024 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
1025 inti->type = s390int->type;
1026 inti->emerg.code = s390int->parm;
1027 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001028 case KVM_S390_MCHK:
1029 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1030 s390int->parm64);
1031 inti->type = s390int->type;
1032 inti->mchk.mcic = s390int->parm64;
1033 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001034 case KVM_S390_INT_PFAULT_INIT:
1035 inti->type = s390int->type;
1036 inti->ext.ext_params2 = s390int->parm64;
1037 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001038 case KVM_S390_INT_VIRTIO:
1039 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001040 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001041 default:
1042 kfree(inti);
1043 return -EINVAL;
1044 }
Cornelia Huckade38c32012-07-23 17:20:30 +02001045 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
1046 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001047
1048 mutex_lock(&vcpu->kvm->lock);
1049 li = &vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001050 spin_lock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001051 if (inti->type == KVM_S390_PROGRAM_INT)
1052 list_add(&inti->list, &li->list);
1053 else
1054 list_add_tail(&inti->list, &li->list);
1055 atomic_set(&li->active, 1);
1056 if (inti->type == KVM_S390_SIGP_STOP)
1057 li->action_bits |= ACTION_STOP_ON_STOP;
1058 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001059 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001060 mutex_unlock(&vcpu->kvm->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001061 kvm_s390_vcpu_wakeup(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001062 return 0;
1063}
Jens Freimannc05c4182013-10-07 16:13:45 +02001064
Christian Borntraeger67335e62014-03-25 17:09:08 +01001065void kvm_s390_clear_float_irqs(struct kvm *kvm)
Jens Freimannc05c4182013-10-07 16:13:45 +02001066{
1067 struct kvm_s390_float_interrupt *fi;
1068 struct kvm_s390_interrupt_info *n, *inti = NULL;
1069
1070 mutex_lock(&kvm->lock);
1071 fi = &kvm->arch.float_int;
1072 spin_lock(&fi->lock);
1073 list_for_each_entry_safe(inti, n, &fi->list, list) {
1074 list_del(&inti->list);
1075 kfree(inti);
1076 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001077 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001078 atomic_set(&fi->active, 0);
1079 spin_unlock(&fi->lock);
1080 mutex_unlock(&kvm->lock);
1081}
1082
1083static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1084 u8 *addr)
1085{
1086 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1087 struct kvm_s390_irq irq = {0};
1088
1089 irq.type = inti->type;
1090 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001091 case KVM_S390_INT_PFAULT_INIT:
1092 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001093 case KVM_S390_INT_VIRTIO:
1094 case KVM_S390_INT_SERVICE:
1095 irq.u.ext = inti->ext;
1096 break;
1097 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1098 irq.u.io = inti->io;
1099 break;
1100 case KVM_S390_MCHK:
1101 irq.u.mchk = inti->mchk;
1102 break;
1103 default:
1104 return -EINVAL;
1105 }
1106
1107 if (copy_to_user(uptr, &irq, sizeof(irq)))
1108 return -EFAULT;
1109
1110 return 0;
1111}
1112
1113static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1114{
1115 struct kvm_s390_interrupt_info *inti;
1116 struct kvm_s390_float_interrupt *fi;
1117 int ret = 0;
1118 int n = 0;
1119
1120 mutex_lock(&kvm->lock);
1121 fi = &kvm->arch.float_int;
1122 spin_lock(&fi->lock);
1123
1124 list_for_each_entry(inti, &fi->list, list) {
1125 if (len < sizeof(struct kvm_s390_irq)) {
1126 /* signal userspace to try again */
1127 ret = -ENOMEM;
1128 break;
1129 }
1130 ret = copy_irq_to_user(inti, buf);
1131 if (ret)
1132 break;
1133 buf += sizeof(struct kvm_s390_irq);
1134 len -= sizeof(struct kvm_s390_irq);
1135 n++;
1136 }
1137
1138 spin_unlock(&fi->lock);
1139 mutex_unlock(&kvm->lock);
1140
1141 return ret < 0 ? ret : n;
1142}
1143
1144static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1145{
1146 int r;
1147
1148 switch (attr->group) {
1149 case KVM_DEV_FLIC_GET_ALL_IRQS:
1150 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1151 attr->attr);
1152 break;
1153 default:
1154 r = -EINVAL;
1155 }
1156
1157 return r;
1158}
1159
1160static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1161 u64 addr)
1162{
1163 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1164 void *target = NULL;
1165 void __user *source;
1166 u64 size;
1167
1168 if (get_user(inti->type, (u64 __user *)addr))
1169 return -EFAULT;
1170
1171 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001172 case KVM_S390_INT_PFAULT_INIT:
1173 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001174 case KVM_S390_INT_VIRTIO:
1175 case KVM_S390_INT_SERVICE:
1176 target = (void *) &inti->ext;
1177 source = &uptr->u.ext;
1178 size = sizeof(inti->ext);
1179 break;
1180 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1181 target = (void *) &inti->io;
1182 source = &uptr->u.io;
1183 size = sizeof(inti->io);
1184 break;
1185 case KVM_S390_MCHK:
1186 target = (void *) &inti->mchk;
1187 source = &uptr->u.mchk;
1188 size = sizeof(inti->mchk);
1189 break;
1190 default:
1191 return -EINVAL;
1192 }
1193
1194 if (copy_from_user(target, source, size))
1195 return -EFAULT;
1196
1197 return 0;
1198}
1199
1200static int enqueue_floating_irq(struct kvm_device *dev,
1201 struct kvm_device_attr *attr)
1202{
1203 struct kvm_s390_interrupt_info *inti = NULL;
1204 int r = 0;
1205 int len = attr->attr;
1206
1207 if (len % sizeof(struct kvm_s390_irq) != 0)
1208 return -EINVAL;
1209 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1210 return -EINVAL;
1211
1212 while (len >= sizeof(struct kvm_s390_irq)) {
1213 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1214 if (!inti)
1215 return -ENOMEM;
1216
1217 r = copy_irq_from_user(inti, attr->addr);
1218 if (r) {
1219 kfree(inti);
1220 return r;
1221 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001222 r = __inject_vm(dev->kvm, inti);
1223 if (r) {
1224 kfree(inti);
1225 return r;
1226 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001227 len -= sizeof(struct kvm_s390_irq);
1228 attr->addr += sizeof(struct kvm_s390_irq);
1229 }
1230
1231 return r;
1232}
1233
Cornelia Huck841b91c2013-07-15 13:36:01 +02001234static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1235{
1236 if (id >= MAX_S390_IO_ADAPTERS)
1237 return NULL;
1238 return kvm->arch.adapters[id];
1239}
1240
1241static int register_io_adapter(struct kvm_device *dev,
1242 struct kvm_device_attr *attr)
1243{
1244 struct s390_io_adapter *adapter;
1245 struct kvm_s390_io_adapter adapter_info;
1246
1247 if (copy_from_user(&adapter_info,
1248 (void __user *)attr->addr, sizeof(adapter_info)))
1249 return -EFAULT;
1250
1251 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1252 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1253 return -EINVAL;
1254
1255 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1256 if (!adapter)
1257 return -ENOMEM;
1258
1259 INIT_LIST_HEAD(&adapter->maps);
1260 init_rwsem(&adapter->maps_lock);
1261 atomic_set(&adapter->nr_maps, 0);
1262 adapter->id = adapter_info.id;
1263 adapter->isc = adapter_info.isc;
1264 adapter->maskable = adapter_info.maskable;
1265 adapter->masked = false;
1266 adapter->swap = adapter_info.swap;
1267 dev->kvm->arch.adapters[adapter->id] = adapter;
1268
1269 return 0;
1270}
1271
1272int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1273{
1274 int ret;
1275 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1276
1277 if (!adapter || !adapter->maskable)
1278 return -EINVAL;
1279 ret = adapter->masked;
1280 adapter->masked = masked;
1281 return ret;
1282}
1283
1284static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1285{
1286 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1287 struct s390_map_info *map;
1288 int ret;
1289
1290 if (!adapter || !addr)
1291 return -EINVAL;
1292
1293 map = kzalloc(sizeof(*map), GFP_KERNEL);
1294 if (!map) {
1295 ret = -ENOMEM;
1296 goto out;
1297 }
1298 INIT_LIST_HEAD(&map->list);
1299 map->guest_addr = addr;
1300 map->addr = gmap_translate(addr, kvm->arch.gmap);
1301 if (map->addr == -EFAULT) {
1302 ret = -EFAULT;
1303 goto out;
1304 }
1305 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1306 if (ret < 0)
1307 goto out;
1308 BUG_ON(ret != 1);
1309 down_write(&adapter->maps_lock);
1310 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1311 list_add_tail(&map->list, &adapter->maps);
1312 ret = 0;
1313 } else {
1314 put_page(map->page);
1315 ret = -EINVAL;
1316 }
1317 up_write(&adapter->maps_lock);
1318out:
1319 if (ret)
1320 kfree(map);
1321 return ret;
1322}
1323
1324static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1325{
1326 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1327 struct s390_map_info *map, *tmp;
1328 int found = 0;
1329
1330 if (!adapter || !addr)
1331 return -EINVAL;
1332
1333 down_write(&adapter->maps_lock);
1334 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1335 if (map->guest_addr == addr) {
1336 found = 1;
1337 atomic_dec(&adapter->nr_maps);
1338 list_del(&map->list);
1339 put_page(map->page);
1340 kfree(map);
1341 break;
1342 }
1343 }
1344 up_write(&adapter->maps_lock);
1345
1346 return found ? 0 : -EINVAL;
1347}
1348
1349void kvm_s390_destroy_adapters(struct kvm *kvm)
1350{
1351 int i;
1352 struct s390_map_info *map, *tmp;
1353
1354 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1355 if (!kvm->arch.adapters[i])
1356 continue;
1357 list_for_each_entry_safe(map, tmp,
1358 &kvm->arch.adapters[i]->maps, list) {
1359 list_del(&map->list);
1360 put_page(map->page);
1361 kfree(map);
1362 }
1363 kfree(kvm->arch.adapters[i]);
1364 }
1365}
1366
1367static int modify_io_adapter(struct kvm_device *dev,
1368 struct kvm_device_attr *attr)
1369{
1370 struct kvm_s390_io_adapter_req req;
1371 struct s390_io_adapter *adapter;
1372 int ret;
1373
1374 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1375 return -EFAULT;
1376
1377 adapter = get_io_adapter(dev->kvm, req.id);
1378 if (!adapter)
1379 return -EINVAL;
1380 switch (req.type) {
1381 case KVM_S390_IO_ADAPTER_MASK:
1382 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1383 if (ret > 0)
1384 ret = 0;
1385 break;
1386 case KVM_S390_IO_ADAPTER_MAP:
1387 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1388 break;
1389 case KVM_S390_IO_ADAPTER_UNMAP:
1390 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1391 break;
1392 default:
1393 ret = -EINVAL;
1394 }
1395
1396 return ret;
1397}
1398
Jens Freimannc05c4182013-10-07 16:13:45 +02001399static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1400{
1401 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001402 unsigned int i;
1403 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001404
1405 switch (attr->group) {
1406 case KVM_DEV_FLIC_ENQUEUE:
1407 r = enqueue_floating_irq(dev, attr);
1408 break;
1409 case KVM_DEV_FLIC_CLEAR_IRQS:
1410 r = 0;
Christian Borntraeger67335e62014-03-25 17:09:08 +01001411 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001412 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001413 case KVM_DEV_FLIC_APF_ENABLE:
1414 dev->kvm->arch.gmap->pfault_enabled = 1;
1415 break;
1416 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1417 dev->kvm->arch.gmap->pfault_enabled = 0;
1418 /*
1419 * Make sure no async faults are in transition when
1420 * clearing the queues. So we don't need to worry
1421 * about late coming workers.
1422 */
1423 synchronize_srcu(&dev->kvm->srcu);
1424 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1425 kvm_clear_async_pf_completion_queue(vcpu);
1426 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001427 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1428 r = register_io_adapter(dev, attr);
1429 break;
1430 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1431 r = modify_io_adapter(dev, attr);
1432 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001433 default:
1434 r = -EINVAL;
1435 }
1436
1437 return r;
1438}
1439
1440static int flic_create(struct kvm_device *dev, u32 type)
1441{
1442 if (!dev)
1443 return -EINVAL;
1444 if (dev->kvm->arch.flic)
1445 return -EINVAL;
1446 dev->kvm->arch.flic = dev;
1447 return 0;
1448}
1449
1450static void flic_destroy(struct kvm_device *dev)
1451{
1452 dev->kvm->arch.flic = NULL;
1453 kfree(dev);
1454}
1455
1456/* s390 floating irq controller (flic) */
1457struct kvm_device_ops kvm_flic_ops = {
1458 .name = "kvm-flic",
1459 .get_attr = flic_get_attr,
1460 .set_attr = flic_set_attr,
1461 .create = flic_create,
1462 .destroy = flic_destroy,
1463};
Cornelia Huck84223592013-07-15 13:36:01 +02001464
1465static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1466{
1467 unsigned long bit;
1468
1469 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1470
1471 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1472}
1473
1474static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1475 u64 addr)
1476{
1477 struct s390_map_info *map;
1478
1479 if (!adapter)
1480 return NULL;
1481
1482 list_for_each_entry(map, &adapter->maps, list) {
1483 if (map->guest_addr == addr)
1484 return map;
1485 }
1486 return NULL;
1487}
1488
1489static int adapter_indicators_set(struct kvm *kvm,
1490 struct s390_io_adapter *adapter,
1491 struct kvm_s390_adapter_int *adapter_int)
1492{
1493 unsigned long bit;
1494 int summary_set, idx;
1495 struct s390_map_info *info;
1496 void *map;
1497
1498 info = get_map_info(adapter, adapter_int->ind_addr);
1499 if (!info)
1500 return -1;
1501 map = page_address(info->page);
1502 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1503 set_bit(bit, map);
1504 idx = srcu_read_lock(&kvm->srcu);
1505 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1506 set_page_dirty_lock(info->page);
1507 info = get_map_info(adapter, adapter_int->summary_addr);
1508 if (!info) {
1509 srcu_read_unlock(&kvm->srcu, idx);
1510 return -1;
1511 }
1512 map = page_address(info->page);
1513 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1514 adapter->swap);
1515 summary_set = test_and_set_bit(bit, map);
1516 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1517 set_page_dirty_lock(info->page);
1518 srcu_read_unlock(&kvm->srcu, idx);
1519 return summary_set ? 0 : 1;
1520}
1521
1522/*
1523 * < 0 - not injected due to error
1524 * = 0 - coalesced, summary indicator already active
1525 * > 0 - injected interrupt
1526 */
1527static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1528 struct kvm *kvm, int irq_source_id, int level,
1529 bool line_status)
1530{
1531 int ret;
1532 struct s390_io_adapter *adapter;
1533
1534 /* We're only interested in the 0->1 transition. */
1535 if (!level)
1536 return 0;
1537 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1538 if (!adapter)
1539 return -1;
1540 down_read(&adapter->maps_lock);
1541 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1542 up_read(&adapter->maps_lock);
1543 if ((ret > 0) && !adapter->masked) {
1544 struct kvm_s390_interrupt s390int = {
1545 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1546 .parm = 0,
1547 .parm64 = (adapter->isc << 27) | 0x80000000,
1548 };
1549 ret = kvm_s390_inject_vm(kvm, &s390int);
1550 if (ret == 0)
1551 ret = 1;
1552 }
1553 return ret;
1554}
1555
1556int kvm_set_routing_entry(struct kvm_irq_routing_table *rt,
1557 struct kvm_kernel_irq_routing_entry *e,
1558 const struct kvm_irq_routing_entry *ue)
1559{
1560 int ret;
1561
1562 switch (ue->type) {
1563 case KVM_IRQ_ROUTING_S390_ADAPTER:
1564 e->set = set_adapter_int;
1565 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1566 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1567 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1568 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1569 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1570 ret = 0;
1571 break;
1572 default:
1573 ret = -EINVAL;
1574 }
1575
1576 return ret;
1577}
1578
1579int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1580 int irq_source_id, int level, bool line_status)
1581{
1582 return -EINVAL;
1583}