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 typedef struct KVMXICSState
{
52 static void icp_get_kvm_state(ICPState
*ss
)
55 struct kvm_one_reg reg
= {
56 .id
= KVM_REG_PPC_ICP_STATE
,
57 .addr
= (uintptr_t)&state
,
61 /* ICP for this CPU thread is not in use, exiting */
66 ret
= kvm_vcpu_ioctl(ss
->cs
, KVM_GET_ONE_REG
, ®
);
68 error_report("Unable to retrieve KVM interrupt controller state"
69 " for CPU %ld: %s", kvm_arch_vcpu_id(ss
->cs
), strerror(errno
));
73 ss
->xirr
= state
>> KVM_REG_PPC_ICP_XISR_SHIFT
;
74 ss
->mfrr
= (state
>> KVM_REG_PPC_ICP_MFRR_SHIFT
)
75 & KVM_REG_PPC_ICP_MFRR_MASK
;
76 ss
->pending_priority
= (state
>> KVM_REG_PPC_ICP_PPRI_SHIFT
)
77 & KVM_REG_PPC_ICP_PPRI_MASK
;
80 static int icp_set_kvm_state(ICPState
*ss
, int version_id
)
83 struct kvm_one_reg reg
= {
84 .id
= KVM_REG_PPC_ICP_STATE
,
85 .addr
= (uintptr_t)&state
,
89 /* ICP for this CPU thread is not in use, exiting */
94 state
= ((uint64_t)ss
->xirr
<< KVM_REG_PPC_ICP_XISR_SHIFT
)
95 | ((uint64_t)ss
->mfrr
<< KVM_REG_PPC_ICP_MFRR_SHIFT
)
96 | ((uint64_t)ss
->pending_priority
<< KVM_REG_PPC_ICP_PPRI_SHIFT
);
98 ret
= kvm_vcpu_ioctl(ss
->cs
, KVM_SET_ONE_REG
, ®
);
100 error_report("Unable to restore KVM interrupt controller state (0x%"
101 PRIx64
") for CPU %ld: %s", state
, kvm_arch_vcpu_id(ss
->cs
),
109 static void icp_kvm_reset(DeviceState
*dev
)
111 ICPState
*icp
= ICP(dev
);
114 icp
->pending_priority
= 0xff;
117 /* Make all outputs as deasserted only if the CPU thread is in use */
119 qemu_set_irq(icp
->output
, 0);
122 icp_set_kvm_state(icp
, 1);
125 static void icp_kvm_class_init(ObjectClass
*klass
, void *data
)
127 DeviceClass
*dc
= DEVICE_CLASS(klass
);
128 ICPStateClass
*icpc
= ICP_CLASS(klass
);
130 dc
->reset
= icp_kvm_reset
;
131 icpc
->pre_save
= icp_get_kvm_state
;
132 icpc
->post_load
= icp_set_kvm_state
;
135 static const TypeInfo icp_kvm_info
= {
136 .name
= TYPE_KVM_ICP
,
138 .instance_size
= sizeof(ICPState
),
139 .class_init
= icp_kvm_class_init
,
140 .class_size
= sizeof(ICPStateClass
),
146 static void ics_get_kvm_state(ICSState
*ics
)
148 KVMXICSState
*xicskvm
= XICS_SPAPR_KVM(ics
->xics
);
150 struct kvm_device_attr attr
= {
152 .group
= KVM_DEV_XICS_GRP_SOURCES
,
153 .addr
= (uint64_t)(uintptr_t)&state
,
157 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
158 ICSIRQState
*irq
= &ics
->irqs
[i
];
161 attr
.attr
= i
+ ics
->offset
;
163 ret
= ioctl(xicskvm
->kernel_xics_fd
, KVM_GET_DEVICE_ATTR
, &attr
);
165 error_report("Unable to retrieve KVM interrupt controller state"
166 " for IRQ %d: %s", i
+ ics
->offset
, strerror(errno
));
170 irq
->server
= state
& KVM_XICS_DESTINATION_MASK
;
171 irq
->saved_priority
= (state
>> KVM_XICS_PRIORITY_SHIFT
)
172 & KVM_XICS_PRIORITY_MASK
;
174 * To be consistent with the software emulation in xics.c, we
175 * split out the masked state + priority that we get from the
176 * kernel into 'current priority' (0xff if masked) and
177 * 'saved priority' (if masked, this is the priority the
178 * interrupt had before it was masked). Masking and unmasking
179 * are done with the ibm,int-off and ibm,int-on RTAS calls.
181 if (state
& KVM_XICS_MASKED
) {
182 irq
->priority
= 0xff;
184 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
;
205 static int ics_set_kvm_state(ICSState
*ics
, int version_id
)
207 KVMXICSState
*xicskvm
= XICS_SPAPR_KVM(ics
->xics
);
209 struct kvm_device_attr attr
= {
211 .group
= KVM_DEV_XICS_GRP_SOURCES
,
212 .addr
= (uint64_t)(uintptr_t)&state
,
216 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
217 ICSIRQState
*irq
= &ics
->irqs
[i
];
220 attr
.attr
= i
+ ics
->offset
;
223 state
|= (uint64_t)(irq
->saved_priority
& KVM_XICS_PRIORITY_MASK
)
224 << KVM_XICS_PRIORITY_SHIFT
;
225 if (irq
->priority
!= irq
->saved_priority
) {
226 assert(irq
->priority
== 0xff);
227 state
|= KVM_XICS_MASKED
;
230 if (ics
->irqs
[i
].flags
& XICS_FLAGS_IRQ_LSI
) {
231 state
|= KVM_XICS_LEVEL_SENSITIVE
;
232 if (irq
->status
& XICS_STATUS_ASSERTED
) {
233 state
|= KVM_XICS_PENDING
;
236 if (irq
->status
& XICS_STATUS_MASKED_PENDING
) {
237 state
|= KVM_XICS_PENDING
;
241 ret
= ioctl(xicskvm
->kernel_xics_fd
, KVM_SET_DEVICE_ATTR
, &attr
);
243 error_report("Unable to restore KVM interrupt controller state"
244 " for IRQs %d: %s", i
+ ics
->offset
, strerror(errno
));
252 static void ics_kvm_set_irq(void *opaque
, int srcno
, int val
)
254 ICSState
*ics
= opaque
;
255 struct kvm_irq_level args
;
258 args
.irq
= srcno
+ ics
->offset
;
259 if (ics
->irqs
[srcno
].flags
& XICS_FLAGS_IRQ_MSI
) {
263 args
.level
= KVM_INTERRUPT_SET
;
265 args
.level
= val
? KVM_INTERRUPT_SET_LEVEL
: KVM_INTERRUPT_UNSET
;
267 rc
= kvm_vm_ioctl(kvm_state
, KVM_IRQ_LINE
, &args
);
269 perror("kvm_irq_line");
273 static void ics_kvm_reset(DeviceState
*dev
)
275 ICSState
*ics
= ICS_SIMPLE(dev
);
277 uint8_t flags
[ics
->nr_irqs
];
279 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
280 flags
[i
] = ics
->irqs
[i
].flags
;
283 memset(ics
->irqs
, 0, sizeof(ICSIRQState
) * ics
->nr_irqs
);
285 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
286 ics
->irqs
[i
].priority
= 0xff;
287 ics
->irqs
[i
].saved_priority
= 0xff;
288 ics
->irqs
[i
].flags
= flags
[i
];
291 ics_set_kvm_state(ics
, 1);
294 static void ics_kvm_realize(DeviceState
*dev
, Error
**errp
)
296 ICSState
*ics
= ICS_SIMPLE(dev
);
299 error_setg(errp
, "Number of interrupts needs to be greater 0");
302 ics
->irqs
= g_malloc0(ics
->nr_irqs
* sizeof(ICSIRQState
));
303 ics
->qirqs
= qemu_allocate_irqs(ics_kvm_set_irq
, ics
, ics
->nr_irqs
);
306 static void ics_kvm_class_init(ObjectClass
*klass
, void *data
)
308 DeviceClass
*dc
= DEVICE_CLASS(klass
);
309 ICSStateClass
*icsc
= ICS_BASE_CLASS(klass
);
311 dc
->realize
= ics_kvm_realize
;
312 dc
->reset
= ics_kvm_reset
;
313 icsc
->pre_save
= ics_get_kvm_state
;
314 icsc
->post_load
= ics_set_kvm_state
;
317 static const TypeInfo ics_kvm_info
= {
318 .name
= TYPE_ICS_KVM
,
319 .parent
= TYPE_ICS_SIMPLE
,
320 .instance_size
= sizeof(ICSState
),
321 .class_init
= ics_kvm_class_init
,
327 static void xics_kvm_cpu_setup(XICSState
*xics
, PowerPCCPU
*cpu
)
331 KVMXICSState
*xicskvm
= XICS_SPAPR_KVM(xics
);
335 ss
= &xics
->ss
[cs
->cpu_index
];
337 assert(cs
->cpu_index
< xics
->nr_servers
);
338 if (xicskvm
->kernel_xics_fd
== -1) {
343 * If we are reusing a parked vCPU fd corresponding to the CPU
344 * which was hot-removed earlier we don't have to renable
345 * KVM_CAP_IRQ_XICS capability again.
347 if (ss
->cap_irq_xics_enabled
) {
351 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_IRQ_XICS
, 0, xicskvm
->kernel_xics_fd
,
352 kvm_arch_vcpu_id(cs
));
354 error_report("Unable to connect CPU%ld to kernel XICS: %s",
355 kvm_arch_vcpu_id(cs
), strerror(errno
));
358 ss
->cap_irq_xics_enabled
= true;
361 static void xics_kvm_set_nr_irqs(XICSState
*xics
, uint32_t nr_irqs
,
364 ICSState
*ics
= QLIST_FIRST(&xics
->ics
);
366 /* This needs to be deprecated ... */
367 xics
->nr_irqs
= nr_irqs
;
369 ics
->nr_irqs
= nr_irqs
;
373 static void xics_kvm_set_nr_servers(XICSState
*xics
, uint32_t nr_servers
,
376 xics_set_nr_servers(xics
, nr_servers
, TYPE_KVM_ICP
, errp
);
379 static void rtas_dummy(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
381 uint32_t nargs
, target_ulong args
,
382 uint32_t nret
, target_ulong rets
)
384 error_report("pseries: %s must never be called for in-kernel XICS",
388 static void xics_kvm_realize(DeviceState
*dev
, Error
**errp
)
390 KVMXICSState
*xicskvm
= XICS_SPAPR_KVM(dev
);
391 XICSState
*xics
= XICS_COMMON(dev
);
395 struct kvm_create_device xics_create_device
= {
396 .type
= KVM_DEV_TYPE_XICS
,
400 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_IRQ_XICS
)) {
402 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
406 spapr_rtas_register(RTAS_IBM_SET_XIVE
, "ibm,set-xive", rtas_dummy
);
407 spapr_rtas_register(RTAS_IBM_GET_XIVE
, "ibm,get-xive", rtas_dummy
);
408 spapr_rtas_register(RTAS_IBM_INT_OFF
, "ibm,int-off", rtas_dummy
);
409 spapr_rtas_register(RTAS_IBM_INT_ON
, "ibm,int-on", rtas_dummy
);
411 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE
, "ibm,set-xive");
413 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
417 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE
, "ibm,get-xive");
419 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
423 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON
, "ibm,int-on");
425 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-on");
429 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF
, "ibm,int-off");
431 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-off");
435 /* Create the kernel ICP */
436 rc
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &xics_create_device
);
438 error_setg_errno(errp
, -rc
, "Error on KVM_CREATE_DEVICE for XICS");
442 xicskvm
->kernel_xics_fd
= xics_create_device
.fd
;
444 QLIST_FOREACH(ics
, &xics
->ics
, list
) {
445 object_property_set_bool(OBJECT(ics
), true, "realized", &error
);
447 error_propagate(errp
, error
);
452 assert(xics
->nr_servers
);
453 for (i
= 0; i
< xics
->nr_servers
; i
++) {
454 object_property_set_bool(OBJECT(&xics
->ss
[i
]), true, "realized",
457 error_propagate(errp
, error
);
462 kvm_kernel_irqchip
= true;
463 kvm_msi_via_irqfd_allowed
= true;
464 kvm_gsi_direct_mapping
= true;
469 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
470 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
471 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
472 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
475 static void xics_kvm_initfn(Object
*obj
)
477 XICSState
*xics
= XICS_COMMON(obj
);
480 ics
= ICS_SIMPLE(object_new(TYPE_ICS_KVM
));
481 object_property_add_child(obj
, "ics", OBJECT(ics
), NULL
);
483 QLIST_INSERT_HEAD(&xics
->ics
, ics
, list
);
486 static void xics_kvm_class_init(ObjectClass
*oc
, void *data
)
488 DeviceClass
*dc
= DEVICE_CLASS(oc
);
489 XICSStateClass
*xsc
= XICS_COMMON_CLASS(oc
);
491 dc
->realize
= xics_kvm_realize
;
492 xsc
->cpu_setup
= xics_kvm_cpu_setup
;
493 xsc
->set_nr_irqs
= xics_kvm_set_nr_irqs
;
494 xsc
->set_nr_servers
= xics_kvm_set_nr_servers
;
497 static const TypeInfo xics_spapr_kvm_info
= {
498 .name
= TYPE_XICS_SPAPR_KVM
,
499 .parent
= TYPE_XICS_COMMON
,
500 .instance_size
= sizeof(KVMXICSState
),
501 .class_init
= xics_kvm_class_init
,
502 .instance_init
= xics_kvm_initfn
,
505 static void xics_kvm_register_types(void)
507 type_register_static(&xics_spapr_kvm_info
);
508 type_register_static(&ics_kvm_info
);
509 type_register_static(&icp_kvm_info
);
512 type_init(xics_kvm_register_types
)