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"
31 #include "hw/ppc/spapr.h"
32 #include "hw/ppc/xics.h"
34 #include "qemu/config-file.h"
35 #include "qemu/error-report.h"
37 #include <sys/ioctl.h>
39 typedef struct KVMXICSState {
48 static void icp_get_kvm_state(ICPState *ss)
51 struct kvm_one_reg reg = {
52 .id = KVM_REG_PPC_ICP_STATE,
53 .addr = (uintptr_t)&state,
57 /* ICP for this CPU thread is not in use, exiting */
62 ret = kvm_vcpu_ioctl(ss->cs, KVM_GET_ONE_REG, ®);
64 error_report("Unable to retrieve KVM interrupt controller state"
65 " for CPU %ld: %s", kvm_arch_vcpu_id(ss->cs), strerror(errno));
69 ss->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
70 ss->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
71 & KVM_REG_PPC_ICP_MFRR_MASK;
72 ss->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
73 & KVM_REG_PPC_ICP_PPRI_MASK;
76 static int icp_set_kvm_state(ICPState *ss, int version_id)
79 struct kvm_one_reg reg = {
80 .id = KVM_REG_PPC_ICP_STATE,
81 .addr = (uintptr_t)&state,
85 /* ICP for this CPU thread is not in use, exiting */
90 state = ((uint64_t)ss->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
91 | ((uint64_t)ss->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
92 | ((uint64_t)ss->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
94 ret = kvm_vcpu_ioctl(ss->cs, KVM_SET_ONE_REG, ®);
96 error_report("Unable to restore KVM interrupt controller state (0x%"
97 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(ss->cs),
105 static void icp_kvm_reset(DeviceState *dev)
107 ICPState *icp = ICP(dev);
110 icp->pending_priority = 0xff;
113 /* Make all outputs are deasserted */
114 qemu_set_irq(icp->output, 0);
116 icp_set_kvm_state(icp, 1);
119 static void icp_kvm_class_init(ObjectClass *klass, void *data)
121 DeviceClass *dc = DEVICE_CLASS(klass);
122 ICPStateClass *icpc = ICP_CLASS(klass);
124 dc->reset = icp_kvm_reset;
125 icpc->pre_save = icp_get_kvm_state;
126 icpc->post_load = icp_set_kvm_state;
129 static const TypeInfo icp_kvm_info = {
130 .name = TYPE_KVM_ICP,
132 .instance_size = sizeof(ICPState),
133 .class_init = icp_kvm_class_init,
134 .class_size = sizeof(ICPStateClass),
140 static void ics_get_kvm_state(ICSState *ics)
142 KVMXICSState *icpkvm = KVM_XICS(ics->icp);
144 struct kvm_device_attr attr = {
146 .group = KVM_DEV_XICS_GRP_SOURCES,
147 .addr = (uint64_t)(uintptr_t)&state,
151 for (i = 0; i < ics->nr_irqs; i++) {
152 ICSIRQState *irq = &ics->irqs[i];
155 attr.attr = i + ics->offset;
157 ret = ioctl(icpkvm->kernel_xics_fd, KVM_GET_DEVICE_ATTR, &attr);
159 error_report("Unable to retrieve KVM interrupt controller state"
160 " for IRQ %d: %s", i + ics->offset, strerror(errno));
164 irq->server = state & KVM_XICS_DESTINATION_MASK;
165 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
166 & KVM_XICS_PRIORITY_MASK;
168 * To be consistent with the software emulation in xics.c, we
169 * split out the masked state + priority that we get from the
170 * kernel into 'current priority' (0xff if masked) and
171 * 'saved priority' (if masked, this is the priority the
172 * interrupt had before it was masked). Masking and unmasking
173 * are done with the ibm,int-off and ibm,int-on RTAS calls.
175 if (state & KVM_XICS_MASKED) {
176 irq->priority = 0xff;
178 irq->priority = irq->saved_priority;
181 if (state & KVM_XICS_PENDING) {
182 if (state & KVM_XICS_LEVEL_SENSITIVE) {
183 irq->status |= XICS_STATUS_ASSERTED;
186 * A pending edge-triggered interrupt (or MSI)
187 * must have been rejected previously when we
188 * first detected it and tried to deliver it,
189 * so mark it as pending and previously rejected
190 * for consistency with how xics.c works.
192 irq->status |= XICS_STATUS_MASKED_PENDING
193 | XICS_STATUS_REJECTED;
199 static int ics_set_kvm_state(ICSState *ics, int version_id)
201 KVMXICSState *icpkvm = KVM_XICS(ics->icp);
203 struct kvm_device_attr attr = {
205 .group = KVM_DEV_XICS_GRP_SOURCES,
206 .addr = (uint64_t)(uintptr_t)&state,
210 for (i = 0; i < ics->nr_irqs; i++) {
211 ICSIRQState *irq = &ics->irqs[i];
214 attr.attr = i + ics->offset;
217 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
218 << KVM_XICS_PRIORITY_SHIFT;
219 if (irq->priority != irq->saved_priority) {
220 assert(irq->priority == 0xff);
221 state |= KVM_XICS_MASKED;
224 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
225 state |= KVM_XICS_LEVEL_SENSITIVE;
226 if (irq->status & XICS_STATUS_ASSERTED) {
227 state |= KVM_XICS_PENDING;
230 if (irq->status & XICS_STATUS_MASKED_PENDING) {
231 state |= KVM_XICS_PENDING;
235 ret = ioctl(icpkvm->kernel_xics_fd, KVM_SET_DEVICE_ATTR, &attr);
237 error_report("Unable to restore KVM interrupt controller state"
238 " for IRQs %d: %s", i + ics->offset, strerror(errno));
246 static void ics_kvm_set_irq(void *opaque, int srcno, int val)
248 ICSState *ics = opaque;
249 struct kvm_irq_level args;
252 args.irq = srcno + ics->offset;
253 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
257 args.level = KVM_INTERRUPT_SET;
259 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
261 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
263 perror("kvm_irq_line");
267 static void ics_kvm_reset(DeviceState *dev)
269 ICSState *ics = ICS(dev);
271 uint8_t flags[ics->nr_irqs];
273 for (i = 0; i < ics->nr_irqs; i++) {
274 flags[i] = ics->irqs[i].flags;
277 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
279 for (i = 0; i < ics->nr_irqs; i++) {
280 ics->irqs[i].priority = 0xff;
281 ics->irqs[i].saved_priority = 0xff;
282 ics->irqs[i].flags = flags[i];
285 ics_set_kvm_state(ics, 1);
288 static void ics_kvm_realize(DeviceState *dev, Error **errp)
290 ICSState *ics = ICS(dev);
293 error_setg(errp, "Number of interrupts needs to be greater 0");
296 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
297 ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs);
300 static void ics_kvm_class_init(ObjectClass *klass, void *data)
302 DeviceClass *dc = DEVICE_CLASS(klass);
303 ICSStateClass *icsc = ICS_CLASS(klass);
305 dc->realize = ics_kvm_realize;
306 dc->reset = ics_kvm_reset;
307 icsc->pre_save = ics_get_kvm_state;
308 icsc->post_load = ics_set_kvm_state;
311 static const TypeInfo ics_kvm_info = {
312 .name = TYPE_KVM_ICS,
314 .instance_size = sizeof(ICSState),
315 .class_init = ics_kvm_class_init,
321 static void xics_kvm_cpu_setup(XICSState *icp, PowerPCCPU *cpu)
325 KVMXICSState *icpkvm = KVM_XICS(icp);
328 ss = &icp->ss[cs->cpu_index];
330 assert(cs->cpu_index < icp->nr_servers);
331 if (icpkvm->kernel_xics_fd == -1) {
336 * If we are reusing a parked vCPU fd corresponding to the CPU
337 * which was hot-removed earlier we don't have to renable
338 * KVM_CAP_IRQ_XICS capability again.
340 if (ss->cap_irq_xics_enabled) {
344 if (icpkvm->kernel_xics_fd != -1) {
349 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0,
350 icpkvm->kernel_xics_fd, kvm_arch_vcpu_id(cs));
352 error_report("Unable to connect CPU%ld to kernel XICS: %s",
353 kvm_arch_vcpu_id(cs), strerror(errno));
356 ss->cap_irq_xics_enabled = true;
360 static void xics_kvm_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp)
362 icp->nr_irqs = icp->ics->nr_irqs = nr_irqs;
365 static void xics_kvm_set_nr_servers(XICSState *icp, uint32_t nr_servers,
370 icp->nr_servers = nr_servers;
372 icp->ss = g_malloc0(icp->nr_servers*sizeof(ICPState));
373 for (i = 0; i < icp->nr_servers; i++) {
375 object_initialize(&icp->ss[i], sizeof(icp->ss[i]), TYPE_KVM_ICP);
376 snprintf(buffer, sizeof(buffer), "icp[%d]", i);
377 object_property_add_child(OBJECT(icp), buffer, OBJECT(&icp->ss[i]),
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 static void xics_kvm_realize(DeviceState *dev, Error **errp)
393 KVMXICSState *icpkvm = KVM_XICS(dev);
394 XICSState *icp = XICS_COMMON(dev);
397 struct kvm_create_device xics_create_device = {
398 .type = KVM_DEV_TYPE_XICS,
402 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
404 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
408 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy);
409 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy);
410 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy);
411 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy);
413 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
415 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
419 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
421 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
425 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
427 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on");
431 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
433 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off");
437 /* Create the kernel ICP */
438 rc = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &xics_create_device);
440 error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS");
444 icpkvm->kernel_xics_fd = xics_create_device.fd;
446 object_property_set_bool(OBJECT(icp->ics), true, "realized", &error);
448 error_propagate(errp, error);
452 assert(icp->nr_servers);
453 for (i = 0; i < icp->nr_servers; i++) {
454 object_property_set_bool(OBJECT(&icp->ss[i]), true, "realized", &error);
456 error_propagate(errp, error);
461 kvm_kernel_irqchip = true;
462 kvm_msi_via_irqfd_allowed = true;
463 kvm_gsi_direct_mapping = true;
468 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
469 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
470 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
471 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
474 static void xics_kvm_initfn(Object *obj)
476 XICSState *xics = XICS_COMMON(obj);
478 xics->ics = ICS(object_new(TYPE_KVM_ICS));
479 object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
480 xics->ics->icp = xics;
483 static void xics_kvm_class_init(ObjectClass *oc, void *data)
485 DeviceClass *dc = DEVICE_CLASS(oc);
486 XICSStateClass *xsc = XICS_COMMON_CLASS(oc);
488 dc->realize = xics_kvm_realize;
489 xsc->cpu_setup = xics_kvm_cpu_setup;
490 xsc->set_nr_irqs = xics_kvm_set_nr_irqs;
491 xsc->set_nr_servers = xics_kvm_set_nr_servers;
494 static const TypeInfo xics_kvm_info = {
495 .name = TYPE_KVM_XICS,
496 .parent = TYPE_XICS_COMMON,
497 .instance_size = sizeof(KVMXICSState),
498 .class_init = xics_kvm_class_init,
499 .instance_init = xics_kvm_initfn,
502 static void xics_kvm_register_types(void)
504 type_register_static(&xics_kvm_info);
505 type_register_static(&ics_kvm_info);
506 type_register_static(&icp_kvm_info);
509 type_init(xics_kvm_register_types)