blob: 75cd3217cd5a9fdc20fb51acbc1043e57a3f4a4d [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{
151 atomic_clear_mask(CPUSTAT_ECALL_PEND |
152 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
153 &vcpu->arch.sie_block->cpuflags);
154 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 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100162}
163
164static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
165{
166 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
167}
168
169static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200170 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100171{
172 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200173 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100174 case KVM_S390_INT_EMERGENCY:
175 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200176 case KVM_S390_INT_PFAULT_INIT:
177 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100178 case KVM_S390_INT_VIRTIO:
Thomas Huthe029ae52014-03-26 16:11:54 +0100179 case KVM_S390_INT_CLOCK_COMP:
180 case KVM_S390_INT_CPU_TIMER:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100181 if (psw_extint_disabled(vcpu))
182 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
183 else
184 vcpu->arch.sie_block->lctl |= LCTL_CR0;
185 break;
186 case KVM_S390_SIGP_STOP:
187 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
188 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100189 case KVM_S390_MCHK:
190 if (psw_mchk_disabled(vcpu))
191 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
192 else
193 vcpu->arch.sie_block->lctl |= LCTL_CR14;
194 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100195 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
196 if (psw_ioint_disabled(vcpu))
197 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
198 else
199 vcpu->arch.sie_block->lctl |= LCTL_CR6;
200 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100201 default:
202 BUG();
203 }
204}
205
David Hildenbrand87128362014-03-03 10:55:13 +0100206static int __deliver_prog_irq(struct kvm_vcpu *vcpu,
207 struct kvm_s390_pgm_info *pgm_info)
208{
209 const unsigned short table[] = { 2, 4, 4, 6 };
210 int rc = 0;
211
212 switch (pgm_info->code & ~PGM_PER) {
213 case PGM_AFX_TRANSLATION:
214 case PGM_ASX_TRANSLATION:
215 case PGM_EX_TRANSLATION:
216 case PGM_LFX_TRANSLATION:
217 case PGM_LSTE_SEQUENCE:
218 case PGM_LSX_TRANSLATION:
219 case PGM_LX_TRANSLATION:
220 case PGM_PRIMARY_AUTHORITY:
221 case PGM_SECONDARY_AUTHORITY:
222 case PGM_SPACE_SWITCH:
223 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
224 (u64 *)__LC_TRANS_EXC_CODE);
225 break;
226 case PGM_ALEN_TRANSLATION:
227 case PGM_ALE_SEQUENCE:
228 case PGM_ASTE_INSTANCE:
229 case PGM_ASTE_SEQUENCE:
230 case PGM_ASTE_VALIDITY:
231 case PGM_EXTENDED_AUTHORITY:
232 rc = put_guest_lc(vcpu, pgm_info->exc_access_id,
233 (u8 *)__LC_EXC_ACCESS_ID);
234 break;
235 case PGM_ASCE_TYPE:
236 case PGM_PAGE_TRANSLATION:
237 case PGM_REGION_FIRST_TRANS:
238 case PGM_REGION_SECOND_TRANS:
239 case PGM_REGION_THIRD_TRANS:
240 case PGM_SEGMENT_TRANSLATION:
241 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
242 (u64 *)__LC_TRANS_EXC_CODE);
243 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
244 (u8 *)__LC_EXC_ACCESS_ID);
245 rc |= put_guest_lc(vcpu, pgm_info->op_access_id,
246 (u8 *)__LC_OP_ACCESS_ID);
247 break;
248 case PGM_MONITOR:
249 rc = put_guest_lc(vcpu, pgm_info->mon_class_nr,
250 (u64 *)__LC_MON_CLASS_NR);
251 rc |= put_guest_lc(vcpu, pgm_info->mon_code,
252 (u64 *)__LC_MON_CODE);
253 break;
254 case PGM_DATA:
255 rc = put_guest_lc(vcpu, pgm_info->data_exc_code,
256 (u32 *)__LC_DATA_EXC_CODE);
257 break;
258 case PGM_PROTECTION:
259 rc = put_guest_lc(vcpu, pgm_info->trans_exc_code,
260 (u64 *)__LC_TRANS_EXC_CODE);
261 rc |= put_guest_lc(vcpu, pgm_info->exc_access_id,
262 (u8 *)__LC_EXC_ACCESS_ID);
263 break;
264 }
265
266 if (pgm_info->code & PGM_PER) {
267 rc |= put_guest_lc(vcpu, pgm_info->per_code,
268 (u8 *) __LC_PER_CODE);
269 rc |= put_guest_lc(vcpu, pgm_info->per_atmid,
270 (u8 *)__LC_PER_ATMID);
271 rc |= put_guest_lc(vcpu, pgm_info->per_address,
272 (u64 *) __LC_PER_ADDRESS);
273 rc |= put_guest_lc(vcpu, pgm_info->per_access_id,
274 (u8 *) __LC_PER_ACCESS_ID);
275 }
276
277 switch (vcpu->arch.sie_block->icptcode) {
278 case ICPT_INST:
279 case ICPT_INSTPROGI:
280 case ICPT_OPEREXC:
281 case ICPT_PARTEXEC:
282 case ICPT_IOINST:
283 /* last instruction only stored for these icptcodes */
284 rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
285 (u16 *) __LC_PGM_ILC);
286 break;
287 case ICPT_PROGI:
288 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->pgmilc,
289 (u16 *) __LC_PGM_ILC);
290 break;
291 default:
292 rc |= put_guest_lc(vcpu, 0,
293 (u16 *) __LC_PGM_ILC);
294 }
295
296 rc |= put_guest_lc(vcpu, pgm_info->code,
297 (u16 *)__LC_PGM_INT_CODE);
298 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
299 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
300 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
301 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
302
303 return rc;
304}
305
Carsten Otteba5c1e92008-03-25 18:47:26 +0100306static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200307 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100308{
309 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100310 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100311
312 switch (inti->type) {
313 case KVM_S390_INT_EMERGENCY:
314 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
315 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200316 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
317 inti->emerg.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100318 rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE);
319 rc |= put_guest_lc(vcpu, inti->emerg.code,
320 (u16 *)__LC_EXT_CPU_ADDR);
321 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
322 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
323 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100324 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100325 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200326 case KVM_S390_INT_EXTERNAL_CALL:
327 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
328 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200329 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
330 inti->extcall.code, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100331 rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE);
332 rc |= put_guest_lc(vcpu, inti->extcall.code,
333 (u16 *)__LC_EXT_CPU_ADDR);
334 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
335 &vcpu->arch.sie_block->gpsw,
336 sizeof(psw_t));
337 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
338 &vcpu->arch.sie_block->gpsw,
339 sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200340 break;
Thomas Huthe029ae52014-03-26 16:11:54 +0100341 case KVM_S390_INT_CLOCK_COMP:
342 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
343 inti->ext.ext_params, 0);
344 deliver_ckc_interrupt(vcpu);
345 break;
346 case KVM_S390_INT_CPU_TIMER:
347 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
348 inti->ext.ext_params, 0);
349 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
350 (u16 *)__LC_EXT_INT_CODE);
351 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
352 &vcpu->arch.sie_block->gpsw,
353 sizeof(psw_t));
354 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
355 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
356 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
357 (u32 *)__LC_EXT_PARAMS);
358 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100359 case KVM_S390_INT_SERVICE:
360 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
361 inti->ext.ext_params);
362 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200363 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
364 inti->ext.ext_params, 0);
Heiko Carstens79882762014-01-02 10:59:41 +0100365 rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE);
366 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
367 &vcpu->arch.sie_block->gpsw,
368 sizeof(psw_t));
369 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100370 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100371 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
372 (u32 *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100373 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200374 case KVM_S390_INT_PFAULT_INIT:
375 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
376 inti->ext.ext_params2);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100377 rc = put_guest_lc(vcpu, 0x2603, (u16 *) __LC_EXT_INT_CODE);
378 rc |= put_guest_lc(vcpu, 0x0600, (u16 *) __LC_EXT_CPU_ADDR);
379 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
380 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
381 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200382 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann1a03b7642014-02-12 14:05:38 +0100383 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
384 (u64 *) __LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200385 break;
386 case KVM_S390_INT_PFAULT_DONE:
387 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
388 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100389 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
390 rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR);
391 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
392 &vcpu->arch.sie_block->gpsw,
393 sizeof(psw_t));
394 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Dominik Dingel3c038e62013-10-07 17:11:48 +0200395 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100396 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
397 (u64 *)__LC_EXT_PARAMS2);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200398 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100399 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100400 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100401 inti->ext.ext_params, inti->ext.ext_params2);
402 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200403 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
404 inti->ext.ext_params,
405 inti->ext.ext_params2);
Heiko Carstens79882762014-01-02 10:59:41 +0100406 rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE);
407 rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR);
408 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
409 &vcpu->arch.sie_block->gpsw,
410 sizeof(psw_t));
411 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100412 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstens79882762014-01-02 10:59:41 +0100413 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
414 (u32 *)__LC_EXT_PARAMS);
415 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
416 (u64 *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100417 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100418 case KVM_S390_SIGP_STOP:
419 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
420 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200421 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
422 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100423 __set_intercept_indicator(vcpu, inti);
424 break;
425
426 case KVM_S390_SIGP_SET_PREFIX:
427 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
428 inti->prefix.address);
429 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200430 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
431 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100432 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100433 break;
434
435 case KVM_S390_RESTART:
436 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
437 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200438 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
439 0, 0);
Jens Freimann1a03b7642014-02-12 14:05:38 +0100440 rc = write_guest_lc(vcpu,
441 offsetof(struct _lowcore, restart_old_psw),
442 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
443 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
444 &vcpu->arch.sie_block->gpsw,
445 sizeof(psw_t));
David Hildenbrand6852d7b2014-03-14 10:59:29 +0100446 kvm_s390_vcpu_start(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100447 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100448 case KVM_S390_PROGRAM_INT:
449 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
450 inti->pgm.code,
451 table[vcpu->arch.sie_block->ipa >> 14]);
452 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200453 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
454 inti->pgm.code, 0);
David Hildenbrand87128362014-03-03 10:55:13 +0100455 rc = __deliver_prog_irq(vcpu, &inti->pgm);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100456 break;
457
Cornelia Huck48a3e952012-12-20 15:32:09 +0100458 case KVM_S390_MCHK:
459 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
460 inti->mchk.mcic);
461 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
462 inti->mchk.cr14,
463 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100464 rc = kvm_s390_vcpu_store_status(vcpu,
465 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens79882762014-01-02 10:59:41 +0100466 rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE);
467 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
468 &vcpu->arch.sie_block->gpsw,
469 sizeof(psw_t));
470 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100471 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100472 break;
473
Cornelia Huckd8346b72012-12-20 15:32:08 +0100474 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
475 {
476 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
477 inti->io.subchannel_nr;
478 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
479 inti->io.io_int_word;
480 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
481 vcpu->stat.deliver_io_int++;
482 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
483 param0, param1);
Heiko Carstens79882762014-01-02 10:59:41 +0100484 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
485 (u16 *)__LC_SUBCHANNEL_ID);
486 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
487 (u16 *)__LC_SUBCHANNEL_NR);
488 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
489 (u32 *)__LC_IO_INT_PARM);
490 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
491 (u32 *)__LC_IO_INT_WORD);
492 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
493 &vcpu->arch.sie_block->gpsw,
494 sizeof(psw_t));
495 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
496 &vcpu->arch.sie_block->gpsw,
497 sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100498 break;
499 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100500 default:
501 BUG();
502 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100503 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200504 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100505 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200506 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100507 }
508}
509
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100510static void deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100511{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100512 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100513
Jens Freimann1a03b7642014-02-12 14:05:38 +0100514 rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
515 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
516 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
517 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
518 &vcpu->arch.sie_block->gpsw,
519 sizeof(psw_t));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100520 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200521 printk("kvm: The guest lowcore is not mapped during interrupt "
522 "delivery, killing userspace\n");
523 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100524 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100525}
526
Dominik Dingel3c038e62013-10-07 17:11:48 +0200527int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100528{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200529 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
530 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
531 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100532 int rc = 0;
533
534 if (atomic_read(&li->active)) {
535 spin_lock_bh(&li->lock);
536 list_for_each_entry(inti, &li->list, list)
537 if (__interrupt_is_deliverable(vcpu, inti)) {
538 rc = 1;
539 break;
540 }
541 spin_unlock_bh(&li->lock);
542 }
543
544 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200545 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100546 list_for_each_entry(inti, &fi->list, list)
547 if (__interrupt_is_deliverable(vcpu, inti)) {
548 rc = 1;
549 break;
550 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200551 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100552 }
553
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100554 if (!rc && kvm_cpu_has_pending_timer(vcpu))
555 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100556
557 return rc;
558}
559
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300560int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
561{
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100562 if (!(vcpu->arch.sie_block->ckc <
563 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
564 return 0;
565 if (!ckc_interrupts_enabled(vcpu))
566 return 0;
567 return 1;
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300568}
569
Carsten Otteba5c1e92008-03-25 18:47:26 +0100570int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
571{
572 u64 now, sltime;
573 DECLARE_WAITQUEUE(wait, current);
574
575 vcpu->stat.exit_wait_state++;
576 if (kvm_cpu_has_interrupt(vcpu))
577 return 0;
578
Carsten Ottee52b2af2008-05-21 13:37:44 +0200579 __set_cpu_idle(vcpu);
580 spin_lock_bh(&vcpu->arch.local_int.lock);
581 vcpu->arch.local_int.timer_due = 0;
582 spin_unlock_bh(&vcpu->arch.local_int.lock);
583
Carsten Otteba5c1e92008-03-25 18:47:26 +0100584 if (psw_interrupts_disabled(vcpu)) {
585 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
586 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100587 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100588 }
589
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100590 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100591 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
592 goto no_timer;
593 }
594
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200595 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100596 if (vcpu->arch.sie_block->ckc < now) {
597 __unset_cpu_idle(vcpu);
598 return 0;
599 }
600
Heiko Carstensed4f2092013-01-14 16:55:55 +0100601 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100602
Christian Borntraegerca872302009-05-12 17:21:49 +0200603 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
604 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100605no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200606 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200607 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100608 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200609 add_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100610 while (list_empty(&vcpu->arch.local_int.list) &&
611 list_empty(&vcpu->arch.local_int.float_int->list) &&
612 (!vcpu->arch.local_int.timer_due) &&
613 !signal_pending(current)) {
614 set_current_state(TASK_INTERRUPTIBLE);
615 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200616 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100617 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200618 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100619 spin_lock_bh(&vcpu->arch.local_int.lock);
620 }
621 __unset_cpu_idle(vcpu);
622 __set_current_state(TASK_RUNNING);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200623 remove_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100624 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200625 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Thomas Huth800c1062013-09-12 10:33:45 +0200626 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
627
Christian Borntraegerca872302009-05-12 17:21:49 +0200628 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100629 return 0;
630}
631
Christian Borntraegerca872302009-05-12 17:21:49 +0200632void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100633{
Christian Borntraegerca872302009-05-12 17:21:49 +0200634 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100635
Christian Borntraegerca872302009-05-12 17:21:49 +0200636 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100637 vcpu->arch.local_int.timer_due = 1;
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200638 if (waitqueue_active(&vcpu->wq))
639 wake_up_interruptible(&vcpu->wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200640 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100641}
642
Christian Borntraegerca872302009-05-12 17:21:49 +0200643/*
644 * low level hrtimer wake routine. Because this runs in hardirq context
645 * we schedule a tasklet to do the real work.
646 */
647enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
648{
649 struct kvm_vcpu *vcpu;
650
651 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100652 vcpu->preempted = true;
Christian Borntraegerca872302009-05-12 17:21:49 +0200653 tasklet_schedule(&vcpu->arch.tasklet);
654
655 return HRTIMER_NORESTART;
656}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100657
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100658void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
659{
660 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
661 struct kvm_s390_interrupt_info *n, *inti = NULL;
662
663 spin_lock_bh(&li->lock);
664 list_for_each_entry_safe(inti, n, &li->list, list) {
665 list_del(&inti->list);
666 kfree(inti);
667 }
668 atomic_set(&li->active, 0);
669 spin_unlock_bh(&li->lock);
670}
671
Carsten Otteba5c1e92008-03-25 18:47:26 +0100672void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
673{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200674 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
675 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
676 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100677 int deliver;
678
679 __reset_intercept_indicators(vcpu);
680 if (atomic_read(&li->active)) {
681 do {
682 deliver = 0;
683 spin_lock_bh(&li->lock);
684 list_for_each_entry_safe(inti, n, &li->list, list) {
685 if (__interrupt_is_deliverable(vcpu, inti)) {
686 list_del(&inti->list);
687 deliver = 1;
688 break;
689 }
690 __set_intercept_indicator(vcpu, inti);
691 }
692 if (list_empty(&li->list))
693 atomic_set(&li->active, 0);
694 spin_unlock_bh(&li->lock);
695 if (deliver) {
696 __do_deliver_interrupt(vcpu, inti);
697 kfree(inti);
698 }
699 } while (deliver);
700 }
701
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100702 if (kvm_cpu_has_pending_timer(vcpu))
703 deliver_ckc_interrupt(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100704
705 if (atomic_read(&fi->active)) {
706 do {
707 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200708 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100709 list_for_each_entry_safe(inti, n, &fi->list, list) {
710 if (__interrupt_is_deliverable(vcpu, inti)) {
711 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100712 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100713 deliver = 1;
714 break;
715 }
716 __set_intercept_indicator(vcpu, inti);
717 }
718 if (list_empty(&fi->list))
719 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200720 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100721 if (deliver) {
722 __do_deliver_interrupt(vcpu, inti);
723 kfree(inti);
724 }
725 } while (deliver);
726 }
727}
728
Cornelia Huck48a3e952012-12-20 15:32:09 +0100729void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
730{
731 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
732 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
733 struct kvm_s390_interrupt_info *n, *inti = NULL;
734 int deliver;
735
736 __reset_intercept_indicators(vcpu);
737 if (atomic_read(&li->active)) {
738 do {
739 deliver = 0;
740 spin_lock_bh(&li->lock);
741 list_for_each_entry_safe(inti, n, &li->list, list) {
742 if ((inti->type == KVM_S390_MCHK) &&
743 __interrupt_is_deliverable(vcpu, inti)) {
744 list_del(&inti->list);
745 deliver = 1;
746 break;
747 }
748 __set_intercept_indicator(vcpu, inti);
749 }
750 if (list_empty(&li->list))
751 atomic_set(&li->active, 0);
752 spin_unlock_bh(&li->lock);
753 if (deliver) {
754 __do_deliver_interrupt(vcpu, inti);
755 kfree(inti);
756 }
757 } while (deliver);
758 }
759
760 if (atomic_read(&fi->active)) {
761 do {
762 deliver = 0;
763 spin_lock(&fi->lock);
764 list_for_each_entry_safe(inti, n, &fi->list, list) {
765 if ((inti->type == KVM_S390_MCHK) &&
766 __interrupt_is_deliverable(vcpu, inti)) {
767 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100768 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100769 deliver = 1;
770 break;
771 }
772 __set_intercept_indicator(vcpu, inti);
773 }
774 if (list_empty(&fi->list))
775 atomic_set(&fi->active, 0);
776 spin_unlock(&fi->lock);
777 if (deliver) {
778 __do_deliver_interrupt(vcpu, inti);
779 kfree(inti);
780 }
781 } while (deliver);
782 }
783}
784
Carsten Otteba5c1e92008-03-25 18:47:26 +0100785int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
786{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200787 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
788 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100789
790 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
791 if (!inti)
792 return -ENOMEM;
793
Joe Perchesa419aef2009-08-18 11:18:35 -0700794 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100795 inti->pgm.code = code;
796
797 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200798 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100799 spin_lock_bh(&li->lock);
800 list_add(&inti->list, &li->list);
801 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200802 BUG_ON(waitqueue_active(li->wq));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100803 spin_unlock_bh(&li->lock);
804 return 0;
805}
806
Jens Freimannbcd84682014-02-11 11:07:05 +0100807int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
808 struct kvm_s390_pgm_info *pgm_info)
809{
810 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
811 struct kvm_s390_interrupt_info *inti;
812
813 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
814 if (!inti)
815 return -ENOMEM;
816
817 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
818 pgm_info->code);
819 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
820 pgm_info->code, 0, 1);
821
822 inti->type = KVM_S390_PROGRAM_INT;
823 memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm));
824 spin_lock_bh(&li->lock);
825 list_add(&inti->list, &li->list);
826 atomic_set(&li->active, 1);
827 BUG_ON(waitqueue_active(li->wq));
828 spin_unlock_bh(&li->lock);
829 return 0;
830}
831
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100832struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
833 u64 cr6, u64 schid)
834{
835 struct kvm_s390_float_interrupt *fi;
836 struct kvm_s390_interrupt_info *inti, *iter;
837
838 if ((!schid && !cr6) || (schid && cr6))
839 return NULL;
840 mutex_lock(&kvm->lock);
841 fi = &kvm->arch.float_int;
842 spin_lock(&fi->lock);
843 inti = NULL;
844 list_for_each_entry(iter, &fi->list, list) {
845 if (!is_ioint(iter->type))
846 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100847 if (cr6 &&
848 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100849 continue;
850 if (schid) {
851 if (((schid & 0x00000000ffff0000) >> 16) !=
852 iter->io.subchannel_id)
853 continue;
854 if ((schid & 0x000000000000ffff) !=
855 iter->io.subchannel_nr)
856 continue;
857 }
858 inti = iter;
859 break;
860 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100861 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100862 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100863 fi->irq_count--;
864 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100865 if (list_empty(&fi->list))
866 atomic_set(&fi->active, 0);
867 spin_unlock(&fi->lock);
868 mutex_unlock(&kvm->lock);
869 return inti;
870}
871
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100872static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100873{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200874 struct kvm_s390_local_interrupt *li;
875 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200876 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100877 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100878 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100879 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100880
Carsten Otteba5c1e92008-03-25 18:47:26 +0100881 mutex_lock(&kvm->lock);
882 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200883 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100884 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
885 rc = -EINVAL;
886 goto unlock_fi;
887 }
888 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200889 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100890 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200891 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100892 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
893
Cornelia Huckd8346b72012-12-20 15:32:08 +0100894 /* Keep I/O interrupts sorted in isc order. */
895 list_for_each_entry(iter, &fi->list, list) {
896 if (!is_ioint(iter->type))
897 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100898 if (int_word_to_isc_bits(iter->io.io_int_word)
899 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100900 continue;
901 break;
902 }
903 list_add_tail(&inti->list, &iter->list);
904 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100905 atomic_set(&fi->active, 1);
906 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
907 if (sigcpu == KVM_MAX_VCPUS) {
908 do {
909 sigcpu = fi->next_rr_cpu++;
910 if (sigcpu == KVM_MAX_VCPUS)
911 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100912 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100913 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100914 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
915 li = &dst_vcpu->arch.local_int;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100916 spin_lock_bh(&li->lock);
917 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200918 if (waitqueue_active(li->wq))
919 wake_up_interruptible(li->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100920 kvm_get_vcpu(kvm, sigcpu)->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100921 spin_unlock_bh(&li->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100922unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200923 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100924 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100925 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200926}
927
928int kvm_s390_inject_vm(struct kvm *kvm,
929 struct kvm_s390_interrupt *s390int)
930{
931 struct kvm_s390_interrupt_info *inti;
932
933 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
934 if (!inti)
935 return -ENOMEM;
936
937 inti->type = s390int->type;
938 switch (inti->type) {
939 case KVM_S390_INT_VIRTIO:
940 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
941 s390int->parm, s390int->parm64);
942 inti->ext.ext_params = s390int->parm;
943 inti->ext.ext_params2 = s390int->parm64;
944 break;
945 case KVM_S390_INT_SERVICE:
946 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
947 inti->ext.ext_params = s390int->parm;
948 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200949 case KVM_S390_INT_PFAULT_DONE:
950 inti->type = s390int->type;
951 inti->ext.ext_params2 = s390int->parm64;
952 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200953 case KVM_S390_MCHK:
954 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
955 s390int->parm64);
956 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
957 inti->mchk.mcic = s390int->parm64;
958 break;
959 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
960 if (inti->type & IOINT_AI_MASK)
961 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
962 else
963 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
964 s390int->type & IOINT_CSSID_MASK,
965 s390int->type & IOINT_SSID_MASK,
966 s390int->type & IOINT_SCHID_MASK);
967 inti->io.subchannel_id = s390int->parm >> 16;
968 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
969 inti->io.io_int_parm = s390int->parm64 >> 32;
970 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
971 break;
972 default:
973 kfree(inti);
974 return -EINVAL;
975 }
976 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
977 2);
978
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100979 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100980}
981
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100982void kvm_s390_reinject_io_int(struct kvm *kvm,
983 struct kvm_s390_interrupt_info *inti)
984{
985 __inject_vm(kvm, inti);
986}
987
Carsten Otteba5c1e92008-03-25 18:47:26 +0100988int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
989 struct kvm_s390_interrupt *s390int)
990{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200991 struct kvm_s390_local_interrupt *li;
992 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100993
994 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
995 if (!inti)
996 return -ENOMEM;
997
998 switch (s390int->type) {
999 case KVM_S390_PROGRAM_INT:
1000 if (s390int->parm & 0xffff0000) {
1001 kfree(inti);
1002 return -EINVAL;
1003 }
1004 inti->type = s390int->type;
1005 inti->pgm.code = s390int->parm;
1006 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
1007 s390int->parm);
1008 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001009 case KVM_S390_SIGP_SET_PREFIX:
1010 inti->prefix.address = s390int->parm;
1011 inti->type = s390int->type;
1012 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
1013 s390int->parm);
1014 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001015 case KVM_S390_SIGP_STOP:
1016 case KVM_S390_RESTART:
Thomas Huthe029ae52014-03-26 16:11:54 +01001017 case KVM_S390_INT_CLOCK_COMP:
1018 case KVM_S390_INT_CPU_TIMER:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001019 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
1020 inti->type = s390int->type;
1021 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001022 case KVM_S390_INT_EXTERNAL_CALL:
1023 if (s390int->parm & 0xffff0000) {
1024 kfree(inti);
1025 return -EINVAL;
1026 }
1027 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
1028 s390int->parm);
1029 inti->type = s390int->type;
1030 inti->extcall.code = s390int->parm;
1031 break;
1032 case KVM_S390_INT_EMERGENCY:
1033 if (s390int->parm & 0xffff0000) {
1034 kfree(inti);
1035 return -EINVAL;
1036 }
1037 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
1038 inti->type = s390int->type;
1039 inti->emerg.code = s390int->parm;
1040 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001041 case KVM_S390_MCHK:
1042 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1043 s390int->parm64);
1044 inti->type = s390int->type;
1045 inti->mchk.mcic = s390int->parm64;
1046 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001047 case KVM_S390_INT_PFAULT_INIT:
1048 inti->type = s390int->type;
1049 inti->ext.ext_params2 = s390int->parm64;
1050 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001051 case KVM_S390_INT_VIRTIO:
1052 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001053 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001054 default:
1055 kfree(inti);
1056 return -EINVAL;
1057 }
Cornelia Huckade38c32012-07-23 17:20:30 +02001058 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
1059 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001060
1061 mutex_lock(&vcpu->kvm->lock);
1062 li = &vcpu->arch.local_int;
1063 spin_lock_bh(&li->lock);
1064 if (inti->type == KVM_S390_PROGRAM_INT)
1065 list_add(&inti->list, &li->list);
1066 else
1067 list_add_tail(&inti->list, &li->list);
1068 atomic_set(&li->active, 1);
1069 if (inti->type == KVM_S390_SIGP_STOP)
1070 li->action_bits |= ACTION_STOP_ON_STOP;
1071 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001072 if (waitqueue_active(&vcpu->wq))
1073 wake_up_interruptible(&vcpu->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +01001074 vcpu->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001075 spin_unlock_bh(&li->lock);
1076 mutex_unlock(&vcpu->kvm->lock);
1077 return 0;
1078}
Jens Freimannc05c4182013-10-07 16:13:45 +02001079
Christian Borntraeger67335e62014-03-25 17:09:08 +01001080void kvm_s390_clear_float_irqs(struct kvm *kvm)
Jens Freimannc05c4182013-10-07 16:13:45 +02001081{
1082 struct kvm_s390_float_interrupt *fi;
1083 struct kvm_s390_interrupt_info *n, *inti = NULL;
1084
1085 mutex_lock(&kvm->lock);
1086 fi = &kvm->arch.float_int;
1087 spin_lock(&fi->lock);
1088 list_for_each_entry_safe(inti, n, &fi->list, list) {
1089 list_del(&inti->list);
1090 kfree(inti);
1091 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001092 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001093 atomic_set(&fi->active, 0);
1094 spin_unlock(&fi->lock);
1095 mutex_unlock(&kvm->lock);
1096}
1097
1098static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1099 u8 *addr)
1100{
1101 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1102 struct kvm_s390_irq irq = {0};
1103
1104 irq.type = inti->type;
1105 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001106 case KVM_S390_INT_PFAULT_INIT:
1107 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001108 case KVM_S390_INT_VIRTIO:
1109 case KVM_S390_INT_SERVICE:
1110 irq.u.ext = inti->ext;
1111 break;
1112 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1113 irq.u.io = inti->io;
1114 break;
1115 case KVM_S390_MCHK:
1116 irq.u.mchk = inti->mchk;
1117 break;
1118 default:
1119 return -EINVAL;
1120 }
1121
1122 if (copy_to_user(uptr, &irq, sizeof(irq)))
1123 return -EFAULT;
1124
1125 return 0;
1126}
1127
1128static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1129{
1130 struct kvm_s390_interrupt_info *inti;
1131 struct kvm_s390_float_interrupt *fi;
1132 int ret = 0;
1133 int n = 0;
1134
1135 mutex_lock(&kvm->lock);
1136 fi = &kvm->arch.float_int;
1137 spin_lock(&fi->lock);
1138
1139 list_for_each_entry(inti, &fi->list, list) {
1140 if (len < sizeof(struct kvm_s390_irq)) {
1141 /* signal userspace to try again */
1142 ret = -ENOMEM;
1143 break;
1144 }
1145 ret = copy_irq_to_user(inti, buf);
1146 if (ret)
1147 break;
1148 buf += sizeof(struct kvm_s390_irq);
1149 len -= sizeof(struct kvm_s390_irq);
1150 n++;
1151 }
1152
1153 spin_unlock(&fi->lock);
1154 mutex_unlock(&kvm->lock);
1155
1156 return ret < 0 ? ret : n;
1157}
1158
1159static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1160{
1161 int r;
1162
1163 switch (attr->group) {
1164 case KVM_DEV_FLIC_GET_ALL_IRQS:
1165 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1166 attr->attr);
1167 break;
1168 default:
1169 r = -EINVAL;
1170 }
1171
1172 return r;
1173}
1174
1175static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1176 u64 addr)
1177{
1178 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1179 void *target = NULL;
1180 void __user *source;
1181 u64 size;
1182
1183 if (get_user(inti->type, (u64 __user *)addr))
1184 return -EFAULT;
1185
1186 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001187 case KVM_S390_INT_PFAULT_INIT:
1188 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001189 case KVM_S390_INT_VIRTIO:
1190 case KVM_S390_INT_SERVICE:
1191 target = (void *) &inti->ext;
1192 source = &uptr->u.ext;
1193 size = sizeof(inti->ext);
1194 break;
1195 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1196 target = (void *) &inti->io;
1197 source = &uptr->u.io;
1198 size = sizeof(inti->io);
1199 break;
1200 case KVM_S390_MCHK:
1201 target = (void *) &inti->mchk;
1202 source = &uptr->u.mchk;
1203 size = sizeof(inti->mchk);
1204 break;
1205 default:
1206 return -EINVAL;
1207 }
1208
1209 if (copy_from_user(target, source, size))
1210 return -EFAULT;
1211
1212 return 0;
1213}
1214
1215static int enqueue_floating_irq(struct kvm_device *dev,
1216 struct kvm_device_attr *attr)
1217{
1218 struct kvm_s390_interrupt_info *inti = NULL;
1219 int r = 0;
1220 int len = attr->attr;
1221
1222 if (len % sizeof(struct kvm_s390_irq) != 0)
1223 return -EINVAL;
1224 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1225 return -EINVAL;
1226
1227 while (len >= sizeof(struct kvm_s390_irq)) {
1228 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1229 if (!inti)
1230 return -ENOMEM;
1231
1232 r = copy_irq_from_user(inti, attr->addr);
1233 if (r) {
1234 kfree(inti);
1235 return r;
1236 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001237 r = __inject_vm(dev->kvm, inti);
1238 if (r) {
1239 kfree(inti);
1240 return r;
1241 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001242 len -= sizeof(struct kvm_s390_irq);
1243 attr->addr += sizeof(struct kvm_s390_irq);
1244 }
1245
1246 return r;
1247}
1248
Cornelia Huck841b91c2013-07-15 13:36:01 +02001249static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1250{
1251 if (id >= MAX_S390_IO_ADAPTERS)
1252 return NULL;
1253 return kvm->arch.adapters[id];
1254}
1255
1256static int register_io_adapter(struct kvm_device *dev,
1257 struct kvm_device_attr *attr)
1258{
1259 struct s390_io_adapter *adapter;
1260 struct kvm_s390_io_adapter adapter_info;
1261
1262 if (copy_from_user(&adapter_info,
1263 (void __user *)attr->addr, sizeof(adapter_info)))
1264 return -EFAULT;
1265
1266 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1267 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1268 return -EINVAL;
1269
1270 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1271 if (!adapter)
1272 return -ENOMEM;
1273
1274 INIT_LIST_HEAD(&adapter->maps);
1275 init_rwsem(&adapter->maps_lock);
1276 atomic_set(&adapter->nr_maps, 0);
1277 adapter->id = adapter_info.id;
1278 adapter->isc = adapter_info.isc;
1279 adapter->maskable = adapter_info.maskable;
1280 adapter->masked = false;
1281 adapter->swap = adapter_info.swap;
1282 dev->kvm->arch.adapters[adapter->id] = adapter;
1283
1284 return 0;
1285}
1286
1287int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1288{
1289 int ret;
1290 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1291
1292 if (!adapter || !adapter->maskable)
1293 return -EINVAL;
1294 ret = adapter->masked;
1295 adapter->masked = masked;
1296 return ret;
1297}
1298
1299static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1300{
1301 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1302 struct s390_map_info *map;
1303 int ret;
1304
1305 if (!adapter || !addr)
1306 return -EINVAL;
1307
1308 map = kzalloc(sizeof(*map), GFP_KERNEL);
1309 if (!map) {
1310 ret = -ENOMEM;
1311 goto out;
1312 }
1313 INIT_LIST_HEAD(&map->list);
1314 map->guest_addr = addr;
1315 map->addr = gmap_translate(addr, kvm->arch.gmap);
1316 if (map->addr == -EFAULT) {
1317 ret = -EFAULT;
1318 goto out;
1319 }
1320 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1321 if (ret < 0)
1322 goto out;
1323 BUG_ON(ret != 1);
1324 down_write(&adapter->maps_lock);
1325 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1326 list_add_tail(&map->list, &adapter->maps);
1327 ret = 0;
1328 } else {
1329 put_page(map->page);
1330 ret = -EINVAL;
1331 }
1332 up_write(&adapter->maps_lock);
1333out:
1334 if (ret)
1335 kfree(map);
1336 return ret;
1337}
1338
1339static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1340{
1341 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1342 struct s390_map_info *map, *tmp;
1343 int found = 0;
1344
1345 if (!adapter || !addr)
1346 return -EINVAL;
1347
1348 down_write(&adapter->maps_lock);
1349 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1350 if (map->guest_addr == addr) {
1351 found = 1;
1352 atomic_dec(&adapter->nr_maps);
1353 list_del(&map->list);
1354 put_page(map->page);
1355 kfree(map);
1356 break;
1357 }
1358 }
1359 up_write(&adapter->maps_lock);
1360
1361 return found ? 0 : -EINVAL;
1362}
1363
1364void kvm_s390_destroy_adapters(struct kvm *kvm)
1365{
1366 int i;
1367 struct s390_map_info *map, *tmp;
1368
1369 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1370 if (!kvm->arch.adapters[i])
1371 continue;
1372 list_for_each_entry_safe(map, tmp,
1373 &kvm->arch.adapters[i]->maps, list) {
1374 list_del(&map->list);
1375 put_page(map->page);
1376 kfree(map);
1377 }
1378 kfree(kvm->arch.adapters[i]);
1379 }
1380}
1381
1382static int modify_io_adapter(struct kvm_device *dev,
1383 struct kvm_device_attr *attr)
1384{
1385 struct kvm_s390_io_adapter_req req;
1386 struct s390_io_adapter *adapter;
1387 int ret;
1388
1389 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1390 return -EFAULT;
1391
1392 adapter = get_io_adapter(dev->kvm, req.id);
1393 if (!adapter)
1394 return -EINVAL;
1395 switch (req.type) {
1396 case KVM_S390_IO_ADAPTER_MASK:
1397 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1398 if (ret > 0)
1399 ret = 0;
1400 break;
1401 case KVM_S390_IO_ADAPTER_MAP:
1402 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1403 break;
1404 case KVM_S390_IO_ADAPTER_UNMAP:
1405 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1406 break;
1407 default:
1408 ret = -EINVAL;
1409 }
1410
1411 return ret;
1412}
1413
Jens Freimannc05c4182013-10-07 16:13:45 +02001414static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1415{
1416 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001417 unsigned int i;
1418 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001419
1420 switch (attr->group) {
1421 case KVM_DEV_FLIC_ENQUEUE:
1422 r = enqueue_floating_irq(dev, attr);
1423 break;
1424 case KVM_DEV_FLIC_CLEAR_IRQS:
1425 r = 0;
Christian Borntraeger67335e62014-03-25 17:09:08 +01001426 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001427 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001428 case KVM_DEV_FLIC_APF_ENABLE:
1429 dev->kvm->arch.gmap->pfault_enabled = 1;
1430 break;
1431 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1432 dev->kvm->arch.gmap->pfault_enabled = 0;
1433 /*
1434 * Make sure no async faults are in transition when
1435 * clearing the queues. So we don't need to worry
1436 * about late coming workers.
1437 */
1438 synchronize_srcu(&dev->kvm->srcu);
1439 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1440 kvm_clear_async_pf_completion_queue(vcpu);
1441 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001442 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1443 r = register_io_adapter(dev, attr);
1444 break;
1445 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1446 r = modify_io_adapter(dev, attr);
1447 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001448 default:
1449 r = -EINVAL;
1450 }
1451
1452 return r;
1453}
1454
1455static int flic_create(struct kvm_device *dev, u32 type)
1456{
1457 if (!dev)
1458 return -EINVAL;
1459 if (dev->kvm->arch.flic)
1460 return -EINVAL;
1461 dev->kvm->arch.flic = dev;
1462 return 0;
1463}
1464
1465static void flic_destroy(struct kvm_device *dev)
1466{
1467 dev->kvm->arch.flic = NULL;
1468 kfree(dev);
1469}
1470
1471/* s390 floating irq controller (flic) */
1472struct kvm_device_ops kvm_flic_ops = {
1473 .name = "kvm-flic",
1474 .get_attr = flic_get_attr,
1475 .set_attr = flic_set_attr,
1476 .create = flic_create,
1477 .destroy = flic_destroy,
1478};
Cornelia Huck84223592013-07-15 13:36:01 +02001479
1480static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1481{
1482 unsigned long bit;
1483
1484 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1485
1486 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1487}
1488
1489static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1490 u64 addr)
1491{
1492 struct s390_map_info *map;
1493
1494 if (!adapter)
1495 return NULL;
1496
1497 list_for_each_entry(map, &adapter->maps, list) {
1498 if (map->guest_addr == addr)
1499 return map;
1500 }
1501 return NULL;
1502}
1503
1504static int adapter_indicators_set(struct kvm *kvm,
1505 struct s390_io_adapter *adapter,
1506 struct kvm_s390_adapter_int *adapter_int)
1507{
1508 unsigned long bit;
1509 int summary_set, idx;
1510 struct s390_map_info *info;
1511 void *map;
1512
1513 info = get_map_info(adapter, adapter_int->ind_addr);
1514 if (!info)
1515 return -1;
1516 map = page_address(info->page);
1517 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1518 set_bit(bit, map);
1519 idx = srcu_read_lock(&kvm->srcu);
1520 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1521 set_page_dirty_lock(info->page);
1522 info = get_map_info(adapter, adapter_int->summary_addr);
1523 if (!info) {
1524 srcu_read_unlock(&kvm->srcu, idx);
1525 return -1;
1526 }
1527 map = page_address(info->page);
1528 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1529 adapter->swap);
1530 summary_set = test_and_set_bit(bit, map);
1531 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1532 set_page_dirty_lock(info->page);
1533 srcu_read_unlock(&kvm->srcu, idx);
1534 return summary_set ? 0 : 1;
1535}
1536
1537/*
1538 * < 0 - not injected due to error
1539 * = 0 - coalesced, summary indicator already active
1540 * > 0 - injected interrupt
1541 */
1542static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1543 struct kvm *kvm, int irq_source_id, int level,
1544 bool line_status)
1545{
1546 int ret;
1547 struct s390_io_adapter *adapter;
1548
1549 /* We're only interested in the 0->1 transition. */
1550 if (!level)
1551 return 0;
1552 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1553 if (!adapter)
1554 return -1;
1555 down_read(&adapter->maps_lock);
1556 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1557 up_read(&adapter->maps_lock);
1558 if ((ret > 0) && !adapter->masked) {
1559 struct kvm_s390_interrupt s390int = {
1560 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1561 .parm = 0,
1562 .parm64 = (adapter->isc << 27) | 0x80000000,
1563 };
1564 ret = kvm_s390_inject_vm(kvm, &s390int);
1565 if (ret == 0)
1566 ret = 1;
1567 }
1568 return ret;
1569}
1570
1571int kvm_set_routing_entry(struct kvm_irq_routing_table *rt,
1572 struct kvm_kernel_irq_routing_entry *e,
1573 const struct kvm_irq_routing_entry *ue)
1574{
1575 int ret;
1576
1577 switch (ue->type) {
1578 case KVM_IRQ_ROUTING_S390_ADAPTER:
1579 e->set = set_adapter_int;
1580 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1581 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1582 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1583 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1584 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1585 ret = 0;
1586 break;
1587 default:
1588 ret = -EINVAL;
1589 }
1590
1591 return ret;
1592}
1593
1594int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1595 int irq_source_id, int level, bool line_status)
1596{
1597 return -EINVAL;
1598}