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(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(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_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_KVM_ICS
,
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 xics
->nr_irqs
= xics
->ics
->nr_irqs
= nr_irqs
;
367 static void xics_kvm_set_nr_servers(XICSState
*xics
, uint32_t nr_servers
,
372 xics
->nr_servers
= nr_servers
;
374 xics
->ss
= g_malloc0(xics
->nr_servers
* sizeof(ICPState
));
375 for (i
= 0; i
< xics
->nr_servers
; i
++) {
377 object_initialize(&xics
->ss
[i
], sizeof(xics
->ss
[i
]), TYPE_KVM_ICP
);
378 snprintf(buffer
, sizeof(buffer
), "icp[%d]", i
);
379 object_property_add_child(OBJECT(xics
), buffer
, OBJECT(&xics
->ss
[i
]),
384 static void rtas_dummy(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
386 uint32_t nargs
, target_ulong args
,
387 uint32_t nret
, target_ulong rets
)
389 error_report("pseries: %s must never be called for in-kernel XICS",
393 static void xics_kvm_realize(DeviceState
*dev
, Error
**errp
)
395 KVMXICSState
*xicskvm
= XICS_SPAPR_KVM(dev
);
396 XICSState
*xics
= XICS_COMMON(dev
);
399 struct kvm_create_device xics_create_device
= {
400 .type
= KVM_DEV_TYPE_XICS
,
404 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_IRQ_XICS
)) {
406 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
410 spapr_rtas_register(RTAS_IBM_SET_XIVE
, "ibm,set-xive", rtas_dummy
);
411 spapr_rtas_register(RTAS_IBM_GET_XIVE
, "ibm,get-xive", rtas_dummy
);
412 spapr_rtas_register(RTAS_IBM_INT_OFF
, "ibm,int-off", rtas_dummy
);
413 spapr_rtas_register(RTAS_IBM_INT_ON
, "ibm,int-on", rtas_dummy
);
415 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE
, "ibm,set-xive");
417 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
421 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE
, "ibm,get-xive");
423 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
427 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON
, "ibm,int-on");
429 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-on");
433 rc
= kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF
, "ibm,int-off");
435 error_setg(errp
, "kvmppc_define_rtas_kernel_token: ibm,int-off");
439 /* Create the kernel ICP */
440 rc
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &xics_create_device
);
442 error_setg_errno(errp
, -rc
, "Error on KVM_CREATE_DEVICE for XICS");
446 xicskvm
->kernel_xics_fd
= xics_create_device
.fd
;
448 object_property_set_bool(OBJECT(xics
->ics
), true, "realized", &error
);
450 error_propagate(errp
, error
);
454 assert(xics
->nr_servers
);
455 for (i
= 0; i
< xics
->nr_servers
; i
++) {
456 object_property_set_bool(OBJECT(&xics
->ss
[i
]), true, "realized",
459 error_propagate(errp
, error
);
464 kvm_kernel_irqchip
= true;
465 kvm_msi_via_irqfd_allowed
= true;
466 kvm_gsi_direct_mapping
= true;
471 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
472 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
473 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
474 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
477 static void xics_kvm_initfn(Object
*obj
)
479 XICSState
*xics
= XICS_COMMON(obj
);
481 xics
->ics
= ICS(object_new(TYPE_KVM_ICS
));
482 object_property_add_child(obj
, "ics", OBJECT(xics
->ics
), NULL
);
483 xics
->ics
->xics
= xics
;
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
)