spapr: check for the activation of the KVM IRQ device
[qemu/ar7.git] / hw / intc / xics_kvm.c
blob12bd5190cfade7b77a065c3b8aeec5e667f73a5b
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * PAPR Virtualized Interrupt System, aka ICS/ICP aka xics, in-kernel emulation
6 * Copyright (c) 2013 David Gibson, IBM Corporation.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "qemu-common.h"
31 #include "cpu.h"
32 #include "hw/hw.h"
33 #include "trace.h"
34 #include "sysemu/kvm.h"
35 #include "hw/ppc/spapr.h"
36 #include "hw/ppc/xics.h"
37 #include "hw/ppc/xics_spapr.h"
38 #include "kvm_ppc.h"
39 #include "qemu/config-file.h"
40 #include "qemu/error-report.h"
42 #include <sys/ioctl.h>
44 static int kernel_xics_fd = -1;
46 typedef struct KVMEnabledICP {
47 unsigned long vcpu_id;
48 QLIST_ENTRY(KVMEnabledICP) node;
49 } KVMEnabledICP;
51 static QLIST_HEAD(, KVMEnabledICP)
52 kvm_enabled_icps = QLIST_HEAD_INITIALIZER(&kvm_enabled_icps);
54 static void kvm_disable_icps(void)
56 KVMEnabledICP *enabled_icp, *next;
58 QLIST_FOREACH_SAFE(enabled_icp, &kvm_enabled_icps, node, next) {
59 QLIST_REMOVE(enabled_icp, node);
60 g_free(enabled_icp);
65 * ICP-KVM
67 void icp_get_kvm_state(ICPState *icp)
69 uint64_t state;
70 int ret;
72 /* The KVM XICS device is not in use */
73 if (kernel_xics_fd == -1) {
74 return;
77 /* ICP for this CPU thread is not in use, exiting */
78 if (!icp->cs) {
79 return;
82 ret = kvm_get_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
83 if (ret != 0) {
84 error_report("Unable to retrieve KVM interrupt controller state"
85 " for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno));
86 exit(1);
89 icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
90 icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
91 & KVM_REG_PPC_ICP_MFRR_MASK;
92 icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
93 & KVM_REG_PPC_ICP_PPRI_MASK;
96 static void do_icp_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
98 icp_get_kvm_state(arg.host_ptr);
101 void icp_synchronize_state(ICPState *icp)
103 if (icp->cs) {
104 run_on_cpu(icp->cs, do_icp_synchronize_state, RUN_ON_CPU_HOST_PTR(icp));
108 int icp_set_kvm_state(ICPState *icp)
110 uint64_t state;
111 int ret;
113 /* The KVM XICS device is not in use */
114 if (kernel_xics_fd == -1) {
115 return 0;
118 /* ICP for this CPU thread is not in use, exiting */
119 if (!icp->cs) {
120 return 0;
123 state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
124 | ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
125 | ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
127 ret = kvm_set_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
128 if (ret != 0) {
129 error_report("Unable to restore KVM interrupt controller state (0x%"
130 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(icp->cs),
131 strerror(errno));
132 return ret;
135 return 0;
138 void icp_kvm_realize(DeviceState *dev, Error **errp)
140 ICPState *icp = ICP(dev);
141 CPUState *cs;
142 KVMEnabledICP *enabled_icp;
143 unsigned long vcpu_id;
144 int ret;
146 /* The KVM XICS device is not in use */
147 if (kernel_xics_fd == -1) {
148 return;
151 cs = icp->cs;
152 vcpu_id = kvm_arch_vcpu_id(cs);
155 * If we are reusing a parked vCPU fd corresponding to the CPU
156 * which was hot-removed earlier we don't have to renable
157 * KVM_CAP_IRQ_XICS capability again.
159 QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) {
160 if (enabled_icp->vcpu_id == vcpu_id) {
161 return;
165 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd, vcpu_id);
166 if (ret < 0) {
167 error_setg(errp, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id,
168 strerror(errno));
169 return;
171 enabled_icp = g_malloc(sizeof(*enabled_icp));
172 enabled_icp->vcpu_id = vcpu_id;
173 QLIST_INSERT_HEAD(&kvm_enabled_icps, enabled_icp, node);
177 * ICS-KVM
179 void ics_get_kvm_state(ICSState *ics)
181 uint64_t state;
182 int i;
184 /* The KVM XICS device is not in use */
185 if (kernel_xics_fd == -1) {
186 return;
189 for (i = 0; i < ics->nr_irqs; i++) {
190 ICSIRQState *irq = &ics->irqs[i];
192 kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
193 i + ics->offset, &state, false, &error_fatal);
195 irq->server = state & KVM_XICS_DESTINATION_MASK;
196 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
197 & KVM_XICS_PRIORITY_MASK;
199 * To be consistent with the software emulation in xics.c, we
200 * split out the masked state + priority that we get from the
201 * kernel into 'current priority' (0xff if masked) and
202 * 'saved priority' (if masked, this is the priority the
203 * interrupt had before it was masked). Masking and unmasking
204 * are done with the ibm,int-off and ibm,int-on RTAS calls.
206 if (state & KVM_XICS_MASKED) {
207 irq->priority = 0xff;
208 } else {
209 irq->priority = irq->saved_priority;
212 irq->status = 0;
213 if (state & KVM_XICS_PENDING) {
214 if (state & KVM_XICS_LEVEL_SENSITIVE) {
215 irq->status |= XICS_STATUS_ASSERTED;
216 } else {
218 * A pending edge-triggered interrupt (or MSI)
219 * must have been rejected previously when we
220 * first detected it and tried to deliver it,
221 * so mark it as pending and previously rejected
222 * for consistency with how xics.c works.
224 irq->status |= XICS_STATUS_MASKED_PENDING
225 | XICS_STATUS_REJECTED;
228 if (state & KVM_XICS_PRESENTED) {
229 irq->status |= XICS_STATUS_PRESENTED;
231 if (state & KVM_XICS_QUEUED) {
232 irq->status |= XICS_STATUS_QUEUED;
237 void ics_synchronize_state(ICSState *ics)
239 ics_get_kvm_state(ics);
242 int ics_set_kvm_state_one(ICSState *ics, int srcno)
244 uint64_t state;
245 Error *local_err = NULL;
246 ICSIRQState *irq = &ics->irqs[srcno];
247 int ret;
249 /* The KVM XICS device is not in use */
250 if (kernel_xics_fd == -1) {
251 return 0;
254 state = irq->server;
255 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
256 << KVM_XICS_PRIORITY_SHIFT;
257 if (irq->priority != irq->saved_priority) {
258 assert(irq->priority == 0xff);
259 state |= KVM_XICS_MASKED;
262 if (irq->flags & XICS_FLAGS_IRQ_LSI) {
263 state |= KVM_XICS_LEVEL_SENSITIVE;
264 if (irq->status & XICS_STATUS_ASSERTED) {
265 state |= KVM_XICS_PENDING;
267 } else {
268 if (irq->status & XICS_STATUS_MASKED_PENDING) {
269 state |= KVM_XICS_PENDING;
272 if (irq->status & XICS_STATUS_PRESENTED) {
273 state |= KVM_XICS_PRESENTED;
275 if (irq->status & XICS_STATUS_QUEUED) {
276 state |= KVM_XICS_QUEUED;
279 ret = kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
280 srcno + ics->offset, &state, true, &local_err);
281 if (local_err) {
282 error_report_err(local_err);
283 return ret;
286 return 0;
289 int ics_set_kvm_state(ICSState *ics)
291 int i;
293 /* The KVM XICS device is not in use */
294 if (kernel_xics_fd == -1) {
295 return 0;
298 for (i = 0; i < ics->nr_irqs; i++) {
299 int ret;
301 ret = ics_set_kvm_state_one(ics, i);
302 if (ret) {
303 return ret;
307 return 0;
310 void ics_kvm_set_irq(ICSState *ics, int srcno, int val)
312 struct kvm_irq_level args;
313 int rc;
315 /* The KVM XICS device should be in use */
316 assert(kernel_xics_fd != -1);
318 args.irq = srcno + ics->offset;
319 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
320 if (!val) {
321 return;
323 args.level = KVM_INTERRUPT_SET;
324 } else {
325 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
327 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
328 if (rc < 0) {
329 perror("kvm_irq_line");
333 static void rtas_dummy(PowerPCCPU *cpu, SpaprMachineState *spapr,
334 uint32_t token,
335 uint32_t nargs, target_ulong args,
336 uint32_t nret, target_ulong rets)
338 error_report("pseries: %s must never be called for in-kernel XICS",
339 __func__);
342 int xics_kvm_init(SpaprMachineState *spapr, Error **errp)
344 int rc;
346 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
347 error_setg(errp,
348 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
349 goto fail;
352 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy);
353 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy);
354 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy);
355 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy);
357 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
358 if (rc < 0) {
359 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
360 goto fail;
363 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
364 if (rc < 0) {
365 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
366 goto fail;
369 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
370 if (rc < 0) {
371 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on");
372 goto fail;
375 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
376 if (rc < 0) {
377 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off");
378 goto fail;
381 /* Create the KVM XICS device */
382 rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
383 if (rc < 0) {
384 error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS");
385 goto fail;
388 kernel_xics_fd = rc;
389 kvm_kernel_irqchip = true;
390 kvm_msi_via_irqfd_allowed = true;
391 kvm_gsi_direct_mapping = true;
393 return 0;
395 fail:
396 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
397 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
398 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
399 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
400 return -1;
403 void xics_kvm_disconnect(SpaprMachineState *spapr, Error **errp)
405 /* The KVM XICS device is not in use */
406 if (kernel_xics_fd == -1) {
407 return;
410 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
411 error_setg(errp,
412 "KVM and IRQ_XICS capability must be present for KVM XICS device");
413 return;
417 * Only on P9 using the XICS-on XIVE KVM device:
419 * When the KVM device fd is closed, the device is destroyed and
420 * removed from the list of devices of the VM. The VCPU presenters
421 * are also detached from the device.
423 close(kernel_xics_fd);
424 kernel_xics_fd = -1;
426 spapr_rtas_unregister(RTAS_IBM_SET_XIVE);
427 spapr_rtas_unregister(RTAS_IBM_GET_XIVE);
428 spapr_rtas_unregister(RTAS_IBM_INT_OFF);
429 spapr_rtas_unregister(RTAS_IBM_INT_ON);
431 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
432 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
433 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
434 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
436 kvm_kernel_irqchip = false;
437 kvm_msi_via_irqfd_allowed = false;
438 kvm_gsi_direct_mapping = false;
440 /* Clear the presenter from the VCPUs */
441 kvm_disable_icps();