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
);
57 void icp_get_kvm_state(ICPState
*icp
)
62 /* ICP for this CPU thread is not in use, exiting */
67 ret
= kvm_get_one_reg(icp
->cs
, KVM_REG_PPC_ICP_STATE
, &state
);
69 error_report("Unable to retrieve KVM interrupt controller state"
70 " for CPU %ld: %s", kvm_arch_vcpu_id(icp
->cs
), strerror(errno
));
74 icp
->xirr
= state
>> KVM_REG_PPC_ICP_XISR_SHIFT
;
75 icp
->mfrr
= (state
>> KVM_REG_PPC_ICP_MFRR_SHIFT
)
76 & KVM_REG_PPC_ICP_MFRR_MASK
;
77 icp
->pending_priority
= (state
>> KVM_REG_PPC_ICP_PPRI_SHIFT
)
78 & KVM_REG_PPC_ICP_PPRI_MASK
;
81 static void do_icp_synchronize_state(CPUState
*cpu
, run_on_cpu_data arg
)
83 icp_get_kvm_state(arg
.host_ptr
);
86 void icp_synchronize_state(ICPState
*icp
)
89 run_on_cpu(icp
->cs
, do_icp_synchronize_state
, RUN_ON_CPU_HOST_PTR(icp
));
93 int icp_set_kvm_state(ICPState
*icp
)
98 /* ICP for this CPU thread is not in use, exiting */
103 state
= ((uint64_t)icp
->xirr
<< KVM_REG_PPC_ICP_XISR_SHIFT
)
104 | ((uint64_t)icp
->mfrr
<< KVM_REG_PPC_ICP_MFRR_SHIFT
)
105 | ((uint64_t)icp
->pending_priority
<< KVM_REG_PPC_ICP_PPRI_SHIFT
);
107 ret
= kvm_set_one_reg(icp
->cs
, KVM_REG_PPC_ICP_STATE
, &state
);
109 error_report("Unable to restore KVM interrupt controller state (0x%"
110 PRIx64
") for CPU %ld: %s", state
, kvm_arch_vcpu_id(icp
->cs
),
118 void icp_kvm_realize(DeviceState
*dev
, Error
**errp
)
120 ICPState
*icp
= ICP(dev
);
122 KVMEnabledICP
*enabled_icp
;
123 unsigned long vcpu_id
;
126 if (kernel_xics_fd
== -1) {
131 vcpu_id
= kvm_arch_vcpu_id(cs
);
134 * If we are reusing a parked vCPU fd corresponding to the CPU
135 * which was hot-removed earlier we don't have to renable
136 * KVM_CAP_IRQ_XICS capability again.
138 QLIST_FOREACH(enabled_icp
, &kvm_enabled_icps
, node
) {
139 if (enabled_icp
->vcpu_id
== vcpu_id
) {
144 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_IRQ_XICS
, 0, kernel_xics_fd
, vcpu_id
);
146 error_setg(errp
, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id
,
150 enabled_icp
= g_malloc(sizeof(*enabled_icp
));
151 enabled_icp
->vcpu_id
= vcpu_id
;
152 QLIST_INSERT_HEAD(&kvm_enabled_icps
, enabled_icp
, node
);
158 void ics_get_kvm_state(ICSState
*ics
)
163 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
164 ICSIRQState
*irq
= &ics
->irqs
[i
];
166 kvm_device_access(kernel_xics_fd
, KVM_DEV_XICS_GRP_SOURCES
,
167 i
+ ics
->offset
, &state
, false, &error_fatal
);
169 irq
->server
= state
& KVM_XICS_DESTINATION_MASK
;
170 irq
->saved_priority
= (state
>> KVM_XICS_PRIORITY_SHIFT
)
171 & KVM_XICS_PRIORITY_MASK
;
173 * To be consistent with the software emulation in xics.c, we
174 * split out the masked state + priority that we get from the
175 * kernel into 'current priority' (0xff if masked) and
176 * 'saved priority' (if masked, this is the priority the
177 * interrupt had before it was masked). Masking and unmasking
178 * are done with the ibm,int-off and ibm,int-on RTAS calls.
180 if (state
& KVM_XICS_MASKED
) {
181 irq
->priority
= 0xff;
183 irq
->priority
= irq
->saved_priority
;
187 if (state
& KVM_XICS_PENDING
) {
188 if (state
& KVM_XICS_LEVEL_SENSITIVE
) {
189 irq
->status
|= XICS_STATUS_ASSERTED
;
192 * A pending edge-triggered interrupt (or MSI)
193 * must have been rejected previously when we
194 * first detected it and tried to deliver it,
195 * so mark it as pending and previously rejected
196 * for consistency with how xics.c works.
198 irq
->status
|= XICS_STATUS_MASKED_PENDING
199 | XICS_STATUS_REJECTED
;
202 if (state
& KVM_XICS_PRESENTED
) {
203 irq
->status
|= XICS_STATUS_PRESENTED
;
205 if (state
& KVM_XICS_QUEUED
) {
206 irq
->status
|= XICS_STATUS_QUEUED
;
211 void ics_synchronize_state(ICSState
*ics
)
213 ics_get_kvm_state(ics
);
216 int ics_set_kvm_state_one(ICSState
*ics
, int srcno
)
219 Error
*local_err
= NULL
;
220 ICSIRQState
*irq
= &ics
->irqs
[srcno
];
224 state
|= (uint64_t)(irq
->saved_priority
& KVM_XICS_PRIORITY_MASK
)
225 << KVM_XICS_PRIORITY_SHIFT
;
226 if (irq
->priority
!= irq
->saved_priority
) {
227 assert(irq
->priority
== 0xff);
228 state
|= KVM_XICS_MASKED
;
231 if (irq
->flags
& XICS_FLAGS_IRQ_LSI
) {
232 state
|= KVM_XICS_LEVEL_SENSITIVE
;
233 if (irq
->status
& XICS_STATUS_ASSERTED
) {
234 state
|= KVM_XICS_PENDING
;
237 if (irq
->status
& XICS_STATUS_MASKED_PENDING
) {
238 state
|= KVM_XICS_PENDING
;
241 if (irq
->status
& XICS_STATUS_PRESENTED
) {
242 state
|= KVM_XICS_PRESENTED
;
244 if (irq
->status
& XICS_STATUS_QUEUED
) {
245 state
|= KVM_XICS_QUEUED
;
248 ret
= kvm_device_access(kernel_xics_fd
, KVM_DEV_XICS_GRP_SOURCES
,
249 srcno
+ ics
->offset
, &state
, true, &local_err
);
251 error_report_err(local_err
);
258 int ics_set_kvm_state(ICSState
*ics
)
262 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
265 ret
= ics_set_kvm_state_one(ics
, i
);
274 void ics_kvm_set_irq(ICSState
*ics
, int srcno
, int val
)
276 struct kvm_irq_level args
;
279 args
.irq
= srcno
+ ics
->offset
;
280 if (ics
->irqs
[srcno
].flags
& XICS_FLAGS_IRQ_MSI
) {
284 args
.level
= KVM_INTERRUPT_SET
;
286 args
.level
= val
? KVM_INTERRUPT_SET_LEVEL
: KVM_INTERRUPT_UNSET
;
288 rc
= kvm_vm_ioctl(kvm_state
, KVM_IRQ_LINE
, &args
);
290 perror("kvm_irq_line");
294 static void rtas_dummy(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
296 uint32_t nargs
, target_ulong args
,
297 uint32_t nret
, target_ulong rets
)
299 error_report("pseries: %s must never be called for in-kernel XICS",
303 int xics_kvm_init(SpaprMachineState
*spapr
, Error
**errp
)
307 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_IRQ_XICS
)) {
309 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
313 spapr_rtas_register(RTAS_IBM_SET_XIVE
, "ibm,set-xive", rtas_dummy
);
314 spapr_rtas_register(RTAS_IBM_GET_XIVE
, "ibm,get-xive", rtas_dummy
);
315 spapr_rtas_register(RTAS_IBM_INT_OFF
, "ibm,int-off", rtas_dummy
);
316 spapr_rtas_register(RTAS_IBM_INT_ON
, "ibm,int-on", rtas_dummy
);
318 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE
, "ibm,set-xive");
320 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
324 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE
, "ibm,get-xive");
326 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
330 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON
, "ibm,int-on");
332 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-on");
336 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF
, "ibm,int-off");
338 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-off");
342 /* Create the KVM XICS device */
343 rc
= kvm_create_device(kvm_state
, KVM_DEV_TYPE_XICS
, false);
345 error_setg_errno(errp
, -rc
, "Error on KVM_CREATE_DEVICE for XICS");
350 kvm_kernel_irqchip
= true;
351 kvm_msi_via_irqfd_allowed
= true;
352 kvm_gsi_direct_mapping
= true;
357 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
358 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
359 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
360 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");