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"
38 #include "qemu/config-file.h"
39 #include "qemu/error-report.h"
41 #include <sys/ioctl.h>
43 static int kernel_xics_fd
= -1;
45 typedef struct KVMEnabledICP
{
46 unsigned long vcpu_id
;
47 QLIST_ENTRY(KVMEnabledICP
) node
;
50 static QLIST_HEAD(, KVMEnabledICP
)
51 kvm_enabled_icps
= QLIST_HEAD_INITIALIZER(&kvm_enabled_icps
);
56 static void icp_get_kvm_state(ICPState
*icp
)
61 /* ICP for this CPU thread is not in use, exiting */
66 ret
= kvm_get_one_reg(icp
->cs
, KVM_REG_PPC_ICP_STATE
, &state
);
68 error_report("Unable to retrieve KVM interrupt controller state"
69 " for CPU %ld: %s", kvm_arch_vcpu_id(icp
->cs
), strerror(errno
));
73 icp
->xirr
= state
>> KVM_REG_PPC_ICP_XISR_SHIFT
;
74 icp
->mfrr
= (state
>> KVM_REG_PPC_ICP_MFRR_SHIFT
)
75 & KVM_REG_PPC_ICP_MFRR_MASK
;
76 icp
->pending_priority
= (state
>> KVM_REG_PPC_ICP_PPRI_SHIFT
)
77 & KVM_REG_PPC_ICP_PPRI_MASK
;
80 static void do_icp_synchronize_state(CPUState
*cpu
, run_on_cpu_data arg
)
82 icp_get_kvm_state(arg
.host_ptr
);
85 static void icp_synchronize_state(ICPState
*icp
)
88 run_on_cpu(icp
->cs
, do_icp_synchronize_state
, RUN_ON_CPU_HOST_PTR(icp
));
92 static int icp_set_kvm_state(ICPState
*icp
, int version_id
)
97 /* ICP for this CPU thread is not in use, exiting */
102 state
= ((uint64_t)icp
->xirr
<< KVM_REG_PPC_ICP_XISR_SHIFT
)
103 | ((uint64_t)icp
->mfrr
<< KVM_REG_PPC_ICP_MFRR_SHIFT
)
104 | ((uint64_t)icp
->pending_priority
<< KVM_REG_PPC_ICP_PPRI_SHIFT
);
106 ret
= kvm_set_one_reg(icp
->cs
, KVM_REG_PPC_ICP_STATE
, &state
);
108 error_report("Unable to restore KVM interrupt controller state (0x%"
109 PRIx64
") for CPU %ld: %s", state
, kvm_arch_vcpu_id(icp
->cs
),
117 static void icp_kvm_reset(DeviceState
*dev
)
119 ICPStateClass
*icpc
= ICP_GET_CLASS(dev
);
121 icpc
->parent_reset(dev
);
123 icp_set_kvm_state(ICP(dev
), 1);
126 static void icp_kvm_realize(DeviceState
*dev
, Error
**errp
)
128 ICPState
*icp
= ICP(dev
);
129 ICPStateClass
*icpc
= ICP_GET_CLASS(icp
);
130 Error
*local_err
= NULL
;
132 KVMEnabledICP
*enabled_icp
;
133 unsigned long vcpu_id
;
136 if (kernel_xics_fd
== -1) {
140 icpc
->parent_realize(dev
, &local_err
);
142 error_propagate(errp
, local_err
);
147 vcpu_id
= kvm_arch_vcpu_id(cs
);
150 * If we are reusing a parked vCPU fd corresponding to the CPU
151 * which was hot-removed earlier we don't have to renable
152 * KVM_CAP_IRQ_XICS capability again.
154 QLIST_FOREACH(enabled_icp
, &kvm_enabled_icps
, node
) {
155 if (enabled_icp
->vcpu_id
== vcpu_id
) {
160 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_IRQ_XICS
, 0, kernel_xics_fd
, vcpu_id
);
162 error_setg(errp
, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id
,
166 enabled_icp
= g_malloc(sizeof(*enabled_icp
));
167 enabled_icp
->vcpu_id
= vcpu_id
;
168 QLIST_INSERT_HEAD(&kvm_enabled_icps
, enabled_icp
, node
);
171 static void icp_kvm_class_init(ObjectClass
*klass
, void *data
)
173 DeviceClass
*dc
= DEVICE_CLASS(klass
);
174 ICPStateClass
*icpc
= ICP_CLASS(klass
);
176 device_class_set_parent_realize(dc
, icp_kvm_realize
,
177 &icpc
->parent_realize
);
178 device_class_set_parent_reset(dc
, icp_kvm_reset
,
179 &icpc
->parent_reset
);
181 icpc
->pre_save
= icp_get_kvm_state
;
182 icpc
->post_load
= icp_set_kvm_state
;
183 icpc
->synchronize_state
= icp_synchronize_state
;
186 static const TypeInfo icp_kvm_info
= {
187 .name
= TYPE_KVM_ICP
,
189 .instance_size
= sizeof(ICPState
),
190 .class_init
= icp_kvm_class_init
,
191 .class_size
= sizeof(ICPStateClass
),
197 static void ics_get_kvm_state(ICSState
*ics
)
201 Error
*local_err
= NULL
;
203 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
204 ICSIRQState
*irq
= &ics
->irqs
[i
];
206 kvm_device_access(kernel_xics_fd
, KVM_DEV_XICS_GRP_SOURCES
,
207 i
+ ics
->offset
, &state
, false, &local_err
);
209 error_report_err(local_err
);
213 irq
->server
= state
& KVM_XICS_DESTINATION_MASK
;
214 irq
->saved_priority
= (state
>> KVM_XICS_PRIORITY_SHIFT
)
215 & KVM_XICS_PRIORITY_MASK
;
217 * To be consistent with the software emulation in xics.c, we
218 * split out the masked state + priority that we get from the
219 * kernel into 'current priority' (0xff if masked) and
220 * 'saved priority' (if masked, this is the priority the
221 * interrupt had before it was masked). Masking and unmasking
222 * are done with the ibm,int-off and ibm,int-on RTAS calls.
224 if (state
& KVM_XICS_MASKED
) {
225 irq
->priority
= 0xff;
227 irq
->priority
= irq
->saved_priority
;
231 if (state
& KVM_XICS_PENDING
) {
232 if (state
& KVM_XICS_LEVEL_SENSITIVE
) {
233 irq
->status
|= XICS_STATUS_ASSERTED
;
236 * A pending edge-triggered interrupt (or MSI)
237 * must have been rejected previously when we
238 * first detected it and tried to deliver it,
239 * so mark it as pending and previously rejected
240 * for consistency with how xics.c works.
242 irq
->status
|= XICS_STATUS_MASKED_PENDING
243 | XICS_STATUS_REJECTED
;
246 if (state
& KVM_XICS_PRESENTED
) {
247 irq
->status
|= XICS_STATUS_PRESENTED
;
249 if (state
& KVM_XICS_QUEUED
) {
250 irq
->status
|= XICS_STATUS_QUEUED
;
255 static void ics_synchronize_state(ICSState
*ics
)
257 ics_get_kvm_state(ics
);
260 static int ics_set_kvm_state(ICSState
*ics
, int version_id
)
264 Error
*local_err
= NULL
;
266 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
267 ICSIRQState
*irq
= &ics
->irqs
[i
];
271 state
|= (uint64_t)(irq
->saved_priority
& KVM_XICS_PRIORITY_MASK
)
272 << KVM_XICS_PRIORITY_SHIFT
;
273 if (irq
->priority
!= irq
->saved_priority
) {
274 assert(irq
->priority
== 0xff);
275 state
|= KVM_XICS_MASKED
;
278 if (ics
->irqs
[i
].flags
& XICS_FLAGS_IRQ_LSI
) {
279 state
|= KVM_XICS_LEVEL_SENSITIVE
;
280 if (irq
->status
& XICS_STATUS_ASSERTED
) {
281 state
|= KVM_XICS_PENDING
;
284 if (irq
->status
& XICS_STATUS_MASKED_PENDING
) {
285 state
|= KVM_XICS_PENDING
;
288 if (irq
->status
& XICS_STATUS_PRESENTED
) {
289 state
|= KVM_XICS_PRESENTED
;
291 if (irq
->status
& XICS_STATUS_QUEUED
) {
292 state
|= KVM_XICS_QUEUED
;
295 ret
= kvm_device_access(kernel_xics_fd
, KVM_DEV_XICS_GRP_SOURCES
,
296 i
+ ics
->offset
, &state
, true, &local_err
);
298 error_report_err(local_err
);
306 static void ics_kvm_set_irq(void *opaque
, int srcno
, int val
)
308 ICSState
*ics
= opaque
;
309 struct kvm_irq_level args
;
312 args
.irq
= srcno
+ ics
->offset
;
313 if (ics
->irqs
[srcno
].flags
& XICS_FLAGS_IRQ_MSI
) {
317 args
.level
= KVM_INTERRUPT_SET
;
319 args
.level
= val
? KVM_INTERRUPT_SET_LEVEL
: KVM_INTERRUPT_UNSET
;
321 rc
= kvm_vm_ioctl(kvm_state
, KVM_IRQ_LINE
, &args
);
323 perror("kvm_irq_line");
327 static void ics_kvm_reset(DeviceState
*dev
)
329 ICSStateClass
*icsc
= ICS_BASE_GET_CLASS(dev
);
331 icsc
->parent_reset(dev
);
333 ics_set_kvm_state(ICS_KVM(dev
), 1);
336 static void ics_kvm_reset_handler(void *dev
)
341 static void ics_kvm_realize(DeviceState
*dev
, Error
**errp
)
343 ICSState
*ics
= ICS_KVM(dev
);
344 ICSStateClass
*icsc
= ICS_BASE_GET_CLASS(ics
);
345 Error
*local_err
= NULL
;
347 icsc
->parent_realize(dev
, &local_err
);
349 error_propagate(errp
, local_err
);
352 ics
->qirqs
= qemu_allocate_irqs(ics_kvm_set_irq
, ics
, ics
->nr_irqs
);
354 qemu_register_reset(ics_kvm_reset_handler
, ics
);
357 static void ics_kvm_class_init(ObjectClass
*klass
, void *data
)
359 ICSStateClass
*icsc
= ICS_BASE_CLASS(klass
);
360 DeviceClass
*dc
= DEVICE_CLASS(klass
);
362 device_class_set_parent_realize(dc
, ics_kvm_realize
,
363 &icsc
->parent_realize
);
364 device_class_set_parent_reset(dc
, ics_kvm_reset
,
365 &icsc
->parent_reset
);
367 icsc
->pre_save
= ics_get_kvm_state
;
368 icsc
->post_load
= ics_set_kvm_state
;
369 icsc
->synchronize_state
= ics_synchronize_state
;
372 static const TypeInfo ics_kvm_info
= {
373 .name
= TYPE_ICS_KVM
,
374 .parent
= TYPE_ICS_BASE
,
375 .instance_size
= sizeof(ICSState
),
376 .class_init
= ics_kvm_class_init
,
383 static void rtas_dummy(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
385 uint32_t nargs
, target_ulong args
,
386 uint32_t nret
, target_ulong rets
)
388 error_report("pseries: %s must never be called for in-kernel XICS",
392 int xics_kvm_init(sPAPRMachineState
*spapr
, Error
**errp
)
396 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_IRQ_XICS
)) {
398 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
402 spapr_rtas_register(RTAS_IBM_SET_XIVE
, "ibm,set-xive", rtas_dummy
);
403 spapr_rtas_register(RTAS_IBM_GET_XIVE
, "ibm,get-xive", rtas_dummy
);
404 spapr_rtas_register(RTAS_IBM_INT_OFF
, "ibm,int-off", rtas_dummy
);
405 spapr_rtas_register(RTAS_IBM_INT_ON
, "ibm,int-on", rtas_dummy
);
407 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE
, "ibm,set-xive");
409 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
413 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE
, "ibm,get-xive");
415 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
419 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON
, "ibm,int-on");
421 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-on");
425 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF
, "ibm,int-off");
427 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-off");
431 /* Create the KVM XICS device */
432 rc
= kvm_create_device(kvm_state
, KVM_DEV_TYPE_XICS
, false);
434 error_setg_errno(errp
, -rc
, "Error on KVM_CREATE_DEVICE for XICS");
439 kvm_kernel_irqchip
= true;
440 kvm_msi_via_irqfd_allowed
= true;
441 kvm_gsi_direct_mapping
= true;
446 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
447 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
448 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
449 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
453 static void xics_kvm_register_types(void)
455 type_register_static(&ics_kvm_info
);
456 type_register_static(&icp_kvm_info
);
459 type_init(xics_kvm_register_types
)