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
)
59 struct kvm_one_reg reg
= {
60 .id
= KVM_REG_PPC_ICP_STATE
,
61 .addr
= (uintptr_t)&state
,
65 /* ICP for this CPU thread is not in use, exiting */
70 ret
= kvm_vcpu_ioctl(icp
->cs
, KVM_GET_ONE_REG
, ®
);
72 error_report("Unable to retrieve KVM interrupt controller state"
73 " for CPU %ld: %s", kvm_arch_vcpu_id(icp
->cs
), strerror(errno
));
77 icp
->xirr
= state
>> KVM_REG_PPC_ICP_XISR_SHIFT
;
78 icp
->mfrr
= (state
>> KVM_REG_PPC_ICP_MFRR_SHIFT
)
79 & KVM_REG_PPC_ICP_MFRR_MASK
;
80 icp
->pending_priority
= (state
>> KVM_REG_PPC_ICP_PPRI_SHIFT
)
81 & KVM_REG_PPC_ICP_PPRI_MASK
;
84 static void do_icp_synchronize_state(CPUState
*cpu
, run_on_cpu_data arg
)
86 icp_get_kvm_state(arg
.host_ptr
);
89 static void icp_synchronize_state(ICPState
*icp
)
92 run_on_cpu(icp
->cs
, do_icp_synchronize_state
, RUN_ON_CPU_HOST_PTR(icp
));
96 static int icp_set_kvm_state(ICPState
*icp
, int version_id
)
99 struct kvm_one_reg reg
= {
100 .id
= KVM_REG_PPC_ICP_STATE
,
101 .addr
= (uintptr_t)&state
,
105 /* ICP for this CPU thread is not in use, exiting */
110 state
= ((uint64_t)icp
->xirr
<< KVM_REG_PPC_ICP_XISR_SHIFT
)
111 | ((uint64_t)icp
->mfrr
<< KVM_REG_PPC_ICP_MFRR_SHIFT
)
112 | ((uint64_t)icp
->pending_priority
<< KVM_REG_PPC_ICP_PPRI_SHIFT
);
114 ret
= kvm_vcpu_ioctl(icp
->cs
, KVM_SET_ONE_REG
, ®
);
116 error_report("Unable to restore KVM interrupt controller state (0x%"
117 PRIx64
") for CPU %ld: %s", state
, kvm_arch_vcpu_id(icp
->cs
),
125 static void icp_kvm_reset(ICPState
*icp
)
127 icp_set_kvm_state(icp
, 1);
130 static void icp_kvm_realize(ICPState
*icp
, Error
**errp
)
132 CPUState
*cs
= icp
->cs
;
133 KVMEnabledICP
*enabled_icp
;
134 unsigned long vcpu_id
= kvm_arch_vcpu_id(cs
);
137 if (kernel_xics_fd
== -1) {
142 * If we are reusing a parked vCPU fd corresponding to the CPU
143 * which was hot-removed earlier we don't have to renable
144 * KVM_CAP_IRQ_XICS capability again.
146 QLIST_FOREACH(enabled_icp
, &kvm_enabled_icps
, node
) {
147 if (enabled_icp
->vcpu_id
== vcpu_id
) {
152 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_IRQ_XICS
, 0, kernel_xics_fd
, vcpu_id
);
154 error_setg(errp
, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id
,
158 enabled_icp
= g_malloc(sizeof(*enabled_icp
));
159 enabled_icp
->vcpu_id
= vcpu_id
;
160 QLIST_INSERT_HEAD(&kvm_enabled_icps
, enabled_icp
, node
);
163 static void icp_kvm_class_init(ObjectClass
*klass
, void *data
)
165 ICPStateClass
*icpc
= ICP_CLASS(klass
);
167 icpc
->pre_save
= icp_get_kvm_state
;
168 icpc
->post_load
= icp_set_kvm_state
;
169 icpc
->realize
= icp_kvm_realize
;
170 icpc
->reset
= icp_kvm_reset
;
171 icpc
->synchronize_state
= icp_synchronize_state
;
174 static const TypeInfo icp_kvm_info
= {
175 .name
= TYPE_KVM_ICP
,
177 .instance_size
= sizeof(ICPState
),
178 .class_init
= icp_kvm_class_init
,
179 .class_size
= sizeof(ICPStateClass
),
185 static void ics_get_kvm_state(ICSState
*ics
)
188 struct kvm_device_attr attr
= {
190 .group
= KVM_DEV_XICS_GRP_SOURCES
,
191 .addr
= (uint64_t)(uintptr_t)&state
,
195 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
196 ICSIRQState
*irq
= &ics
->irqs
[i
];
199 attr
.attr
= i
+ ics
->offset
;
201 ret
= ioctl(kernel_xics_fd
, KVM_GET_DEVICE_ATTR
, &attr
);
203 error_report("Unable to retrieve KVM interrupt controller state"
204 " for IRQ %d: %s", i
+ ics
->offset
, strerror(errno
));
208 irq
->server
= state
& KVM_XICS_DESTINATION_MASK
;
209 irq
->saved_priority
= (state
>> KVM_XICS_PRIORITY_SHIFT
)
210 & KVM_XICS_PRIORITY_MASK
;
212 * To be consistent with the software emulation in xics.c, we
213 * split out the masked state + priority that we get from the
214 * kernel into 'current priority' (0xff if masked) and
215 * 'saved priority' (if masked, this is the priority the
216 * interrupt had before it was masked). Masking and unmasking
217 * are done with the ibm,int-off and ibm,int-on RTAS calls.
219 if (state
& KVM_XICS_MASKED
) {
220 irq
->priority
= 0xff;
222 irq
->priority
= irq
->saved_priority
;
226 if (state
& KVM_XICS_PENDING
) {
227 if (state
& KVM_XICS_LEVEL_SENSITIVE
) {
228 irq
->status
|= XICS_STATUS_ASSERTED
;
231 * A pending edge-triggered interrupt (or MSI)
232 * must have been rejected previously when we
233 * first detected it and tried to deliver it,
234 * so mark it as pending and previously rejected
235 * for consistency with how xics.c works.
237 irq
->status
|= XICS_STATUS_MASKED_PENDING
238 | XICS_STATUS_REJECTED
;
241 if (state
& KVM_XICS_PRESENTED
) {
242 irq
->status
|= XICS_STATUS_PRESENTED
;
244 if (state
& KVM_XICS_QUEUED
) {
245 irq
->status
|= XICS_STATUS_QUEUED
;
250 static void ics_synchronize_state(ICSState
*ics
)
252 ics_get_kvm_state(ics
);
255 static int ics_set_kvm_state(ICSState
*ics
, int version_id
)
258 struct kvm_device_attr attr
= {
260 .group
= KVM_DEV_XICS_GRP_SOURCES
,
261 .addr
= (uint64_t)(uintptr_t)&state
,
265 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
266 ICSIRQState
*irq
= &ics
->irqs
[i
];
269 attr
.attr
= i
+ ics
->offset
;
272 state
|= (uint64_t)(irq
->saved_priority
& KVM_XICS_PRIORITY_MASK
)
273 << KVM_XICS_PRIORITY_SHIFT
;
274 if (irq
->priority
!= irq
->saved_priority
) {
275 assert(irq
->priority
== 0xff);
276 state
|= KVM_XICS_MASKED
;
279 if (ics
->irqs
[i
].flags
& XICS_FLAGS_IRQ_LSI
) {
280 state
|= KVM_XICS_LEVEL_SENSITIVE
;
281 if (irq
->status
& XICS_STATUS_ASSERTED
) {
282 state
|= KVM_XICS_PENDING
;
285 if (irq
->status
& XICS_STATUS_MASKED_PENDING
) {
286 state
|= KVM_XICS_PENDING
;
289 if (irq
->status
& XICS_STATUS_PRESENTED
) {
290 state
|= KVM_XICS_PRESENTED
;
292 if (irq
->status
& XICS_STATUS_QUEUED
) {
293 state
|= KVM_XICS_QUEUED
;
296 ret
= ioctl(kernel_xics_fd
, KVM_SET_DEVICE_ATTR
, &attr
);
298 error_report("Unable to restore KVM interrupt controller state"
299 " for IRQs %d: %s", i
+ ics
->offset
, strerror(errno
));
307 static void ics_kvm_set_irq(void *opaque
, int srcno
, int val
)
309 ICSState
*ics
= opaque
;
310 struct kvm_irq_level args
;
313 args
.irq
= srcno
+ ics
->offset
;
314 if (ics
->irqs
[srcno
].flags
& XICS_FLAGS_IRQ_MSI
) {
318 args
.level
= KVM_INTERRUPT_SET
;
320 args
.level
= val
? KVM_INTERRUPT_SET_LEVEL
: KVM_INTERRUPT_UNSET
;
322 rc
= kvm_vm_ioctl(kvm_state
, KVM_IRQ_LINE
, &args
);
324 perror("kvm_irq_line");
328 static void ics_kvm_reset(void *dev
)
330 ICSState
*ics
= ICS_SIMPLE(dev
);
332 uint8_t flags
[ics
->nr_irqs
];
334 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
335 flags
[i
] = ics
->irqs
[i
].flags
;
338 memset(ics
->irqs
, 0, sizeof(ICSIRQState
) * ics
->nr_irqs
);
340 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
341 ics
->irqs
[i
].priority
= 0xff;
342 ics
->irqs
[i
].saved_priority
= 0xff;
343 ics
->irqs
[i
].flags
= flags
[i
];
346 ics_set_kvm_state(ics
, 1);
349 static void ics_kvm_realize(ICSState
*ics
, Error
**errp
)
352 error_setg(errp
, "Number of interrupts needs to be greater 0");
355 ics
->irqs
= g_malloc0(ics
->nr_irqs
* sizeof(ICSIRQState
));
356 ics
->qirqs
= qemu_allocate_irqs(ics_kvm_set_irq
, ics
, ics
->nr_irqs
);
358 qemu_register_reset(ics_kvm_reset
, ics
);
361 static void ics_kvm_class_init(ObjectClass
*klass
, void *data
)
363 ICSStateClass
*icsc
= ICS_BASE_CLASS(klass
);
365 icsc
->realize
= ics_kvm_realize
;
366 icsc
->pre_save
= ics_get_kvm_state
;
367 icsc
->post_load
= ics_set_kvm_state
;
368 icsc
->synchronize_state
= ics_synchronize_state
;
371 static const TypeInfo ics_kvm_info
= {
372 .name
= TYPE_ICS_KVM
,
373 .parent
= TYPE_ICS_SIMPLE
,
374 .instance_size
= sizeof(ICSState
),
375 .class_init
= ics_kvm_class_init
,
382 static void rtas_dummy(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
384 uint32_t nargs
, target_ulong args
,
385 uint32_t nret
, target_ulong rets
)
387 error_report("pseries: %s must never be called for in-kernel XICS",
391 int xics_kvm_init(sPAPRMachineState
*spapr
, Error
**errp
)
394 struct kvm_create_device xics_create_device
= {
395 .type
= KVM_DEV_TYPE_XICS
,
399 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_IRQ_XICS
)) {
401 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
405 spapr_rtas_register(RTAS_IBM_SET_XIVE
, "ibm,set-xive", rtas_dummy
);
406 spapr_rtas_register(RTAS_IBM_GET_XIVE
, "ibm,get-xive", rtas_dummy
);
407 spapr_rtas_register(RTAS_IBM_INT_OFF
, "ibm,int-off", rtas_dummy
);
408 spapr_rtas_register(RTAS_IBM_INT_ON
, "ibm,int-on", rtas_dummy
);
410 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE
, "ibm,set-xive");
412 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
416 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE
, "ibm,get-xive");
418 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
422 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON
, "ibm,int-on");
424 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-on");
428 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF
, "ibm,int-off");
430 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-off");
434 /* Create the kernel ICP */
435 rc
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &xics_create_device
);
437 error_setg_errno(errp
, -rc
, "Error on KVM_CREATE_DEVICE for XICS");
441 kernel_xics_fd
= xics_create_device
.fd
;
443 kvm_kernel_irqchip
= true;
444 kvm_msi_via_irqfd_allowed
= true;
445 kvm_gsi_direct_mapping
= true;
450 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
451 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
452 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
453 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
457 static void xics_kvm_register_types(void)
459 type_register_static(&ics_kvm_info
);
460 type_register_static(&icp_kvm_info
);
463 type_init(xics_kvm_register_types
)