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
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "qemu-common.h"
34 #include "sysemu/kvm.h"
35 #include "hw/ppc/spapr.h"
36 #include "hw/ppc/spapr_cpu_core.h"
37 #include "hw/ppc/xics.h"
38 #include "hw/ppc/xics_spapr.h"
40 #include "qemu/config-file.h"
41 #include "qemu/error-report.h"
43 #include <sys/ioctl.h>
45 static int kernel_xics_fd
= -1;
47 typedef struct KVMEnabledICP
{
48 unsigned long vcpu_id
;
49 QLIST_ENTRY(KVMEnabledICP
) node
;
52 static QLIST_HEAD(, KVMEnabledICP
)
53 kvm_enabled_icps
= QLIST_HEAD_INITIALIZER(&kvm_enabled_icps
);
55 static void kvm_disable_icps(void)
57 KVMEnabledICP
*enabled_icp
, *next
;
59 QLIST_FOREACH_SAFE(enabled_icp
, &kvm_enabled_icps
, node
, next
) {
60 QLIST_REMOVE(enabled_icp
, node
);
68 void icp_get_kvm_state(ICPState
*icp
)
73 /* The KVM XICS device is not in use */
74 if (kernel_xics_fd
== -1) {
78 /* ICP for this CPU thread is not in use, exiting */
83 ret
= kvm_get_one_reg(icp
->cs
, KVM_REG_PPC_ICP_STATE
, &state
);
85 error_report("Unable to retrieve KVM interrupt controller state"
86 " for CPU %ld: %s", kvm_arch_vcpu_id(icp
->cs
), strerror(errno
));
90 icp
->xirr
= state
>> KVM_REG_PPC_ICP_XISR_SHIFT
;
91 icp
->mfrr
= (state
>> KVM_REG_PPC_ICP_MFRR_SHIFT
)
92 & KVM_REG_PPC_ICP_MFRR_MASK
;
93 icp
->pending_priority
= (state
>> KVM_REG_PPC_ICP_PPRI_SHIFT
)
94 & KVM_REG_PPC_ICP_PPRI_MASK
;
97 static void do_icp_synchronize_state(CPUState
*cpu
, run_on_cpu_data arg
)
99 icp_get_kvm_state(arg
.host_ptr
);
102 void icp_synchronize_state(ICPState
*icp
)
105 run_on_cpu(icp
->cs
, do_icp_synchronize_state
, RUN_ON_CPU_HOST_PTR(icp
));
109 int icp_set_kvm_state(ICPState
*icp
)
114 /* The KVM XICS device is not in use */
115 if (kernel_xics_fd
== -1) {
119 /* ICP for this CPU thread is not in use, exiting */
124 state
= ((uint64_t)icp
->xirr
<< KVM_REG_PPC_ICP_XISR_SHIFT
)
125 | ((uint64_t)icp
->mfrr
<< KVM_REG_PPC_ICP_MFRR_SHIFT
)
126 | ((uint64_t)icp
->pending_priority
<< KVM_REG_PPC_ICP_PPRI_SHIFT
);
128 ret
= kvm_set_one_reg(icp
->cs
, KVM_REG_PPC_ICP_STATE
, &state
);
130 error_report("Unable to restore KVM interrupt controller state (0x%"
131 PRIx64
") for CPU %ld: %s", state
, kvm_arch_vcpu_id(icp
->cs
),
139 void icp_kvm_realize(DeviceState
*dev
, Error
**errp
)
141 ICPState
*icp
= ICP(dev
);
143 KVMEnabledICP
*enabled_icp
;
144 unsigned long vcpu_id
;
147 /* The KVM XICS device is not in use */
148 if (kernel_xics_fd
== -1) {
153 vcpu_id
= kvm_arch_vcpu_id(cs
);
156 * If we are reusing a parked vCPU fd corresponding to the CPU
157 * which was hot-removed earlier we don't have to renable
158 * KVM_CAP_IRQ_XICS capability again.
160 QLIST_FOREACH(enabled_icp
, &kvm_enabled_icps
, node
) {
161 if (enabled_icp
->vcpu_id
== vcpu_id
) {
166 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_IRQ_XICS
, 0, kernel_xics_fd
, vcpu_id
);
168 error_setg(errp
, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id
,
172 enabled_icp
= g_malloc(sizeof(*enabled_icp
));
173 enabled_icp
->vcpu_id
= vcpu_id
;
174 QLIST_INSERT_HEAD(&kvm_enabled_icps
, enabled_icp
, node
);
180 void ics_get_kvm_state(ICSState
*ics
)
185 /* The KVM XICS device is not in use */
186 if (kernel_xics_fd
== -1) {
190 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
191 ICSIRQState
*irq
= &ics
->irqs
[i
];
193 kvm_device_access(kernel_xics_fd
, KVM_DEV_XICS_GRP_SOURCES
,
194 i
+ ics
->offset
, &state
, false, &error_fatal
);
196 irq
->server
= state
& KVM_XICS_DESTINATION_MASK
;
197 irq
->saved_priority
= (state
>> KVM_XICS_PRIORITY_SHIFT
)
198 & KVM_XICS_PRIORITY_MASK
;
200 * To be consistent with the software emulation in xics.c, we
201 * split out the masked state + priority that we get from the
202 * kernel into 'current priority' (0xff if masked) and
203 * 'saved priority' (if masked, this is the priority the
204 * interrupt had before it was masked). Masking and unmasking
205 * are done with the ibm,int-off and ibm,int-on RTAS calls.
207 if (state
& KVM_XICS_MASKED
) {
208 irq
->priority
= 0xff;
210 irq
->priority
= irq
->saved_priority
;
214 if (state
& KVM_XICS_PENDING
) {
215 if (state
& KVM_XICS_LEVEL_SENSITIVE
) {
216 irq
->status
|= XICS_STATUS_ASSERTED
;
219 * A pending edge-triggered interrupt (or MSI)
220 * must have been rejected previously when we
221 * first detected it and tried to deliver it,
222 * so mark it as pending and previously rejected
223 * for consistency with how xics.c works.
225 irq
->status
|= XICS_STATUS_MASKED_PENDING
226 | XICS_STATUS_REJECTED
;
229 if (state
& KVM_XICS_PRESENTED
) {
230 irq
->status
|= XICS_STATUS_PRESENTED
;
232 if (state
& KVM_XICS_QUEUED
) {
233 irq
->status
|= XICS_STATUS_QUEUED
;
238 void ics_synchronize_state(ICSState
*ics
)
240 ics_get_kvm_state(ics
);
243 int ics_set_kvm_state_one(ICSState
*ics
, int srcno
)
246 Error
*local_err
= NULL
;
247 ICSIRQState
*irq
= &ics
->irqs
[srcno
];
250 /* The KVM XICS device is not in use */
251 if (kernel_xics_fd
== -1) {
256 state
|= (uint64_t)(irq
->saved_priority
& KVM_XICS_PRIORITY_MASK
)
257 << KVM_XICS_PRIORITY_SHIFT
;
258 if (irq
->priority
!= irq
->saved_priority
) {
259 assert(irq
->priority
== 0xff);
260 state
|= KVM_XICS_MASKED
;
263 if (irq
->flags
& XICS_FLAGS_IRQ_LSI
) {
264 state
|= KVM_XICS_LEVEL_SENSITIVE
;
265 if (irq
->status
& XICS_STATUS_ASSERTED
) {
266 state
|= KVM_XICS_PENDING
;
269 if (irq
->status
& XICS_STATUS_MASKED_PENDING
) {
270 state
|= KVM_XICS_PENDING
;
273 if (irq
->status
& XICS_STATUS_PRESENTED
) {
274 state
|= KVM_XICS_PRESENTED
;
276 if (irq
->status
& XICS_STATUS_QUEUED
) {
277 state
|= KVM_XICS_QUEUED
;
280 ret
= kvm_device_access(kernel_xics_fd
, KVM_DEV_XICS_GRP_SOURCES
,
281 srcno
+ ics
->offset
, &state
, true, &local_err
);
283 error_report_err(local_err
);
290 int ics_set_kvm_state(ICSState
*ics
)
294 /* The KVM XICS device is not in use */
295 if (kernel_xics_fd
== -1) {
299 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
302 ret
= ics_set_kvm_state_one(ics
, i
);
311 void ics_kvm_set_irq(ICSState
*ics
, int srcno
, int val
)
313 struct kvm_irq_level args
;
316 /* The KVM XICS device should be in use */
317 assert(kernel_xics_fd
!= -1);
319 args
.irq
= srcno
+ ics
->offset
;
320 if (ics
->irqs
[srcno
].flags
& XICS_FLAGS_IRQ_MSI
) {
324 args
.level
= KVM_INTERRUPT_SET
;
326 args
.level
= val
? KVM_INTERRUPT_SET_LEVEL
: KVM_INTERRUPT_UNSET
;
328 rc
= kvm_vm_ioctl(kvm_state
, KVM_IRQ_LINE
, &args
);
330 perror("kvm_irq_line");
334 static void rtas_dummy(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
336 uint32_t nargs
, target_ulong args
,
337 uint32_t nret
, target_ulong rets
)
339 error_report("pseries: %s must never be called for in-kernel XICS",
343 int xics_kvm_init(SpaprMachineState
*spapr
, Error
**errp
)
347 Error
*local_err
= NULL
;
350 * The KVM XICS device already in use. This is the case when
351 * rebooting under the XICS-only interrupt mode.
353 if (kernel_xics_fd
!= -1) {
357 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_IRQ_XICS
)) {
359 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
363 spapr_rtas_register(RTAS_IBM_SET_XIVE
, "ibm,set-xive", rtas_dummy
);
364 spapr_rtas_register(RTAS_IBM_GET_XIVE
, "ibm,get-xive", rtas_dummy
);
365 spapr_rtas_register(RTAS_IBM_INT_OFF
, "ibm,int-off", rtas_dummy
);
366 spapr_rtas_register(RTAS_IBM_INT_ON
, "ibm,int-on", rtas_dummy
);
368 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE
, "ibm,set-xive");
370 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
374 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE
, "ibm,get-xive");
376 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
380 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON
, "ibm,int-on");
382 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-on");
386 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF
, "ibm,int-off");
388 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-off");
392 /* Create the KVM XICS device */
393 rc
= kvm_create_device(kvm_state
, KVM_DEV_TYPE_XICS
, false);
395 error_setg_errno(errp
, -rc
, "Error on KVM_CREATE_DEVICE for XICS");
400 kvm_kernel_irqchip
= true;
401 kvm_msi_via_irqfd_allowed
= true;
402 kvm_gsi_direct_mapping
= true;
404 /* Create the presenters */
406 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
408 icp_kvm_realize(DEVICE(spapr_cpu_state(cpu
)->icp
), &local_err
);
410 error_propagate(errp
, local_err
);
415 /* Update the KVM sources */
416 ics_set_kvm_state(spapr
->ics
);
418 /* Connect the presenters to the initial VCPUs of the machine */
420 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
421 icp_set_kvm_state(spapr_cpu_state(cpu
)->icp
);
427 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
428 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
429 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
430 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
434 void xics_kvm_disconnect(SpaprMachineState
*spapr
, Error
**errp
)
436 /* The KVM XICS device is not in use */
437 if (kernel_xics_fd
== -1) {
441 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_IRQ_XICS
)) {
443 "KVM and IRQ_XICS capability must be present for KVM XICS device");
448 * Only on P9 using the XICS-on XIVE KVM device:
450 * When the KVM device fd is closed, the device is destroyed and
451 * removed from the list of devices of the VM. The VCPU presenters
452 * are also detached from the device.
454 close(kernel_xics_fd
);
457 spapr_rtas_unregister(RTAS_IBM_SET_XIVE
);
458 spapr_rtas_unregister(RTAS_IBM_GET_XIVE
);
459 spapr_rtas_unregister(RTAS_IBM_INT_OFF
);
460 spapr_rtas_unregister(RTAS_IBM_INT_ON
);
462 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
463 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
464 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
465 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
467 kvm_kernel_irqchip
= false;
468 kvm_msi_via_irqfd_allowed
= false;
469 kvm_gsi_direct_mapping
= false;
471 /* Clear the presenter from the VCPUs */