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/xics.h"
37 #include "hw/ppc/xics_spapr.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
;
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
);
67 void icp_get_kvm_state(ICPState
*icp
)
72 /* The KVM XICS device is not in use */
73 if (kernel_xics_fd
== -1) {
77 /* ICP for this CPU thread is not in use, exiting */
82 ret
= kvm_get_one_reg(icp
->cs
, KVM_REG_PPC_ICP_STATE
, &state
);
84 error_report("Unable to retrieve KVM interrupt controller state"
85 " for CPU %ld: %s", kvm_arch_vcpu_id(icp
->cs
), strerror(errno
));
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
)
104 run_on_cpu(icp
->cs
, do_icp_synchronize_state
, RUN_ON_CPU_HOST_PTR(icp
));
108 int icp_set_kvm_state(ICPState
*icp
)
113 /* The KVM XICS device is not in use */
114 if (kernel_xics_fd
== -1) {
118 /* ICP for this CPU thread is not in use, exiting */
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
);
129 error_report("Unable to restore KVM interrupt controller state (0x%"
130 PRIx64
") for CPU %ld: %s", state
, kvm_arch_vcpu_id(icp
->cs
),
138 void icp_kvm_realize(DeviceState
*dev
, Error
**errp
)
140 ICPState
*icp
= ICP(dev
);
142 KVMEnabledICP
*enabled_icp
;
143 unsigned long vcpu_id
;
146 /* The KVM XICS device is not in use */
147 if (kernel_xics_fd
== -1) {
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
) {
165 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_IRQ_XICS
, 0, kernel_xics_fd
, vcpu_id
);
167 error_setg(errp
, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id
,
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
);
179 void ics_get_kvm_state(ICSState
*ics
)
184 /* The KVM XICS device is not in use */
185 if (kernel_xics_fd
== -1) {
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;
209 irq
->priority
= irq
->saved_priority
;
213 if (state
& KVM_XICS_PENDING
) {
214 if (state
& KVM_XICS_LEVEL_SENSITIVE
) {
215 irq
->status
|= XICS_STATUS_ASSERTED
;
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
)
245 Error
*local_err
= NULL
;
246 ICSIRQState
*irq
= &ics
->irqs
[srcno
];
249 /* The KVM XICS device is not in use */
250 if (kernel_xics_fd
== -1) {
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
;
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
);
282 error_report_err(local_err
);
289 int ics_set_kvm_state(ICSState
*ics
)
293 /* The KVM XICS device is not in use */
294 if (kernel_xics_fd
== -1) {
298 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
301 ret
= ics_set_kvm_state_one(ics
, i
);
310 void ics_kvm_set_irq(ICSState
*ics
, int srcno
, int val
)
312 struct kvm_irq_level args
;
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
) {
323 args
.level
= KVM_INTERRUPT_SET
;
325 args
.level
= val
? KVM_INTERRUPT_SET_LEVEL
: KVM_INTERRUPT_UNSET
;
327 rc
= kvm_vm_ioctl(kvm_state
, KVM_IRQ_LINE
, &args
);
329 perror("kvm_irq_line");
333 static void rtas_dummy(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
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",
342 int xics_kvm_init(SpaprMachineState
*spapr
, Error
**errp
)
346 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_IRQ_XICS
)) {
348 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
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");
359 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
363 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE
, "ibm,get-xive");
365 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
369 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON
, "ibm,int-on");
371 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-on");
375 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF
, "ibm,int-off");
377 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-off");
381 /* Create the KVM XICS device */
382 rc
= kvm_create_device(kvm_state
, KVM_DEV_TYPE_XICS
, false);
384 error_setg_errno(errp
, -rc
, "Error on KVM_CREATE_DEVICE for XICS");
389 kvm_kernel_irqchip
= true;
390 kvm_msi_via_irqfd_allowed
= true;
391 kvm_gsi_direct_mapping
= true;
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");
403 void xics_kvm_disconnect(SpaprMachineState
*spapr
, Error
**errp
)
405 /* The KVM XICS device is not in use */
406 if (kernel_xics_fd
== -1) {
410 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_IRQ_XICS
)) {
412 "KVM and IRQ_XICS capability must be present for KVM XICS device");
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
);
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 */