2 * QEMU PowerPC sPAPR XIVE interrupt controller model
4 * Copyright (c) 2017-2019, IBM Corporation.
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
12 #include "qemu/error-report.h"
13 #include "qapi/error.h"
14 #include "target/ppc/cpu.h"
15 #include "sysemu/cpus.h"
16 #include "sysemu/kvm.h"
17 #include "hw/ppc/spapr.h"
18 #include "hw/ppc/spapr_xive.h"
19 #include "hw/ppc/xive.h"
22 #include <sys/ioctl.h>
25 * Helpers for CPU hotplug
27 * TODO: make a common KVMEnabledCPU layer for XICS and XIVE
29 typedef struct KVMEnabledCPU
{
30 unsigned long vcpu_id
;
31 QLIST_ENTRY(KVMEnabledCPU
) node
;
34 static QLIST_HEAD(, KVMEnabledCPU
)
35 kvm_enabled_cpus
= QLIST_HEAD_INITIALIZER(&kvm_enabled_cpus
);
37 static bool kvm_cpu_is_enabled(CPUState
*cs
)
39 KVMEnabledCPU
*enabled_cpu
;
40 unsigned long vcpu_id
= kvm_arch_vcpu_id(cs
);
42 QLIST_FOREACH(enabled_cpu
, &kvm_enabled_cpus
, node
) {
43 if (enabled_cpu
->vcpu_id
== vcpu_id
) {
50 static void kvm_cpu_enable(CPUState
*cs
)
52 KVMEnabledCPU
*enabled_cpu
;
53 unsigned long vcpu_id
= kvm_arch_vcpu_id(cs
);
55 enabled_cpu
= g_malloc(sizeof(*enabled_cpu
));
56 enabled_cpu
->vcpu_id
= vcpu_id
;
57 QLIST_INSERT_HEAD(&kvm_enabled_cpus
, enabled_cpu
, node
);
61 * XIVE Thread Interrupt Management context (KVM)
63 static void kvmppc_xive_cpu_get_state(XiveTCTX
*tctx
, Error
**errp
)
65 uint64_t state
[2] = { 0 };
68 ret
= kvm_get_one_reg(tctx
->cs
, KVM_REG_PPC_VP_STATE
, state
);
70 error_setg_errno(errp
, errno
,
71 "XIVE: could not capture KVM state of CPU %ld",
72 kvm_arch_vcpu_id(tctx
->cs
));
76 /* word0 and word1 of the OS ring. */
77 *((uint64_t *) &tctx
->regs
[TM_QW1_OS
]) = state
[0];
85 static void kvmppc_xive_cpu_do_synchronize_state(CPUState
*cpu
,
88 XiveCpuGetState
*s
= arg
.host_ptr
;
90 kvmppc_xive_cpu_get_state(s
->tctx
, &s
->err
);
93 void kvmppc_xive_cpu_synchronize_state(XiveTCTX
*tctx
, Error
**errp
)
101 * Kick the vCPU to make sure they are available for the KVM ioctl.
103 run_on_cpu(tctx
->cs
, kvmppc_xive_cpu_do_synchronize_state
,
104 RUN_ON_CPU_HOST_PTR(&s
));
107 error_propagate(errp
, s
.err
);
112 void kvmppc_xive_cpu_connect(XiveTCTX
*tctx
, Error
**errp
)
114 SpaprXive
*xive
= SPAPR_MACHINE(qdev_get_machine())->xive
;
115 unsigned long vcpu_id
;
118 /* Check if CPU was hot unplugged and replugged. */
119 if (kvm_cpu_is_enabled(tctx
->cs
)) {
123 vcpu_id
= kvm_arch_vcpu_id(tctx
->cs
);
125 ret
= kvm_vcpu_enable_cap(tctx
->cs
, KVM_CAP_PPC_IRQ_XIVE
, 0, xive
->fd
,
128 error_setg(errp
, "XIVE: unable to connect CPU%ld to KVM device: %s",
129 vcpu_id
, strerror(errno
));
133 kvm_cpu_enable(tctx
->cs
);
137 * XIVE Interrupt Source (KVM)
140 void kvmppc_xive_set_source_config(SpaprXive
*xive
, uint32_t lisn
, XiveEAS
*eas
,
150 Error
*local_err
= NULL
;
152 assert(xive_eas_is_valid(eas
));
154 end_idx
= xive_get_field64(EAS_END_INDEX
, eas
->w
);
155 end_blk
= xive_get_field64(EAS_END_BLOCK
, eas
->w
);
156 eisn
= xive_get_field64(EAS_END_DATA
, eas
->w
);
157 masked
= xive_eas_is_masked(eas
);
159 spapr_xive_end_to_target(end_blk
, end_idx
, &server
, &priority
);
161 kvm_src
= priority
<< KVM_XIVE_SOURCE_PRIORITY_SHIFT
&
162 KVM_XIVE_SOURCE_PRIORITY_MASK
;
163 kvm_src
|= server
<< KVM_XIVE_SOURCE_SERVER_SHIFT
&
164 KVM_XIVE_SOURCE_SERVER_MASK
;
165 kvm_src
|= ((uint64_t) masked
<< KVM_XIVE_SOURCE_MASKED_SHIFT
) &
166 KVM_XIVE_SOURCE_MASKED_MASK
;
167 kvm_src
|= ((uint64_t)eisn
<< KVM_XIVE_SOURCE_EISN_SHIFT
) &
168 KVM_XIVE_SOURCE_EISN_MASK
;
170 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_SOURCE_CONFIG
, lisn
,
171 &kvm_src
, true, &local_err
);
173 error_propagate(errp
, local_err
);
178 void kvmppc_xive_sync_source(SpaprXive
*xive
, uint32_t lisn
, Error
**errp
)
180 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_SOURCE_SYNC
, lisn
,
185 * At reset, the interrupt sources are simply created and MASKED. We
186 * only need to inform the KVM XIVE device about their type: LSI or
189 void kvmppc_xive_source_reset_one(XiveSource
*xsrc
, int srcno
, Error
**errp
)
191 SpaprXive
*xive
= SPAPR_XIVE(xsrc
->xive
);
194 if (xive_source_irq_is_lsi(xsrc
, srcno
)) {
195 state
|= KVM_XIVE_LEVEL_SENSITIVE
;
196 if (xsrc
->status
[srcno
] & XIVE_STATUS_ASSERTED
) {
197 state
|= KVM_XIVE_LEVEL_ASSERTED
;
201 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_SOURCE
, srcno
, &state
,
205 void kvmppc_xive_source_reset(XiveSource
*xsrc
, Error
**errp
)
209 for (i
= 0; i
< xsrc
->nr_irqs
; i
++) {
210 Error
*local_err
= NULL
;
212 kvmppc_xive_source_reset_one(xsrc
, i
, &local_err
);
214 error_propagate(errp
, local_err
);
221 * This is used to perform the magic loads on the ESB pages, described
224 * Memory barriers should not be needed for loads (no store for now).
226 static uint64_t xive_esb_rw(XiveSource
*xsrc
, int srcno
, uint32_t offset
,
227 uint64_t data
, bool write
)
229 uint64_t *addr
= xsrc
->esb_mmap
+ xive_source_esb_mgmt(xsrc
, srcno
) +
233 *addr
= cpu_to_be64(data
);
236 /* Prevent the compiler from optimizing away the load */
237 volatile uint64_t value
= be64_to_cpu(*addr
);
242 static uint8_t xive_esb_read(XiveSource
*xsrc
, int srcno
, uint32_t offset
)
244 return xive_esb_rw(xsrc
, srcno
, offset
, 0, 0) & 0x3;
247 static void xive_esb_trigger(XiveSource
*xsrc
, int srcno
)
249 uint64_t *addr
= xsrc
->esb_mmap
+ xive_source_esb_page(xsrc
, srcno
);
254 uint64_t kvmppc_xive_esb_rw(XiveSource
*xsrc
, int srcno
, uint32_t offset
,
255 uint64_t data
, bool write
)
258 return xive_esb_rw(xsrc
, srcno
, offset
, data
, 1);
262 * Special Load EOI handling for LSI sources. Q bit is never set
263 * and the interrupt should be re-triggered if the level is still
266 if (xive_source_irq_is_lsi(xsrc
, srcno
) &&
267 offset
== XIVE_ESB_LOAD_EOI
) {
268 xive_esb_read(xsrc
, srcno
, XIVE_ESB_SET_PQ_00
);
269 if (xsrc
->status
[srcno
] & XIVE_STATUS_ASSERTED
) {
270 xive_esb_trigger(xsrc
, srcno
);
274 return xive_esb_rw(xsrc
, srcno
, offset
, 0, 0);
278 static void kvmppc_xive_source_get_state(XiveSource
*xsrc
)
282 for (i
= 0; i
< xsrc
->nr_irqs
; i
++) {
283 /* Perform a load without side effect to retrieve the PQ bits */
284 uint8_t pq
= xive_esb_read(xsrc
, i
, XIVE_ESB_GET
);
286 /* and save PQ locally */
287 xive_source_esb_set(xsrc
, i
, pq
);
291 void kvmppc_xive_source_set_irq(void *opaque
, int srcno
, int val
)
293 XiveSource
*xsrc
= opaque
;
294 struct kvm_irq_level args
;
298 if (!xive_source_irq_is_lsi(xsrc
, srcno
)) {
302 args
.level
= KVM_INTERRUPT_SET
;
305 xsrc
->status
[srcno
] |= XIVE_STATUS_ASSERTED
;
306 args
.level
= KVM_INTERRUPT_SET_LEVEL
;
308 xsrc
->status
[srcno
] &= ~XIVE_STATUS_ASSERTED
;
309 args
.level
= KVM_INTERRUPT_UNSET
;
312 rc
= kvm_vm_ioctl(kvm_state
, KVM_IRQ_LINE
, &args
);
314 error_report("XIVE: kvm_irq_line() failed : %s", strerror(errno
));
319 * sPAPR XIVE interrupt controller (KVM)
321 void kvmppc_xive_get_queue_config(SpaprXive
*xive
, uint8_t end_blk
,
322 uint32_t end_idx
, XiveEND
*end
,
325 struct kvm_ppc_xive_eq kvm_eq
= { 0 };
329 Error
*local_err
= NULL
;
331 assert(xive_end_is_valid(end
));
333 /* Encode the tuple (server, prio) as a KVM EQ index */
334 spapr_xive_end_to_target(end_blk
, end_idx
, &server
, &priority
);
336 kvm_eq_idx
= priority
<< KVM_XIVE_EQ_PRIORITY_SHIFT
&
337 KVM_XIVE_EQ_PRIORITY_MASK
;
338 kvm_eq_idx
|= server
<< KVM_XIVE_EQ_SERVER_SHIFT
&
339 KVM_XIVE_EQ_SERVER_MASK
;
341 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_EQ_CONFIG
, kvm_eq_idx
,
342 &kvm_eq
, false, &local_err
);
344 error_propagate(errp
, local_err
);
349 * The EQ index and toggle bit are updated by HW. These are the
350 * only fields from KVM we want to update QEMU with. The other END
351 * fields should already be in the QEMU END table.
353 end
->w1
= xive_set_field32(END_W1_GENERATION
, 0ul, kvm_eq
.qtoggle
) |
354 xive_set_field32(END_W1_PAGE_OFF
, 0ul, kvm_eq
.qindex
);
357 void kvmppc_xive_set_queue_config(SpaprXive
*xive
, uint8_t end_blk
,
358 uint32_t end_idx
, XiveEND
*end
,
361 struct kvm_ppc_xive_eq kvm_eq
= { 0 };
365 Error
*local_err
= NULL
;
368 * Build the KVM state from the local END structure.
372 if (xive_get_field32(END_W0_UCOND_NOTIFY
, end
->w0
)) {
373 kvm_eq
.flags
|= KVM_XIVE_EQ_ALWAYS_NOTIFY
;
377 * If the hcall is disabling the EQ, set the size and page address
378 * to zero. When migrating, only valid ENDs are taken into
381 if (xive_end_is_valid(end
)) {
382 kvm_eq
.qshift
= xive_get_field32(END_W0_QSIZE
, end
->w0
) + 12;
383 kvm_eq
.qaddr
= xive_end_qaddr(end
);
385 * The EQ toggle bit and index should only be relevant when
386 * restoring the EQ state
388 kvm_eq
.qtoggle
= xive_get_field32(END_W1_GENERATION
, end
->w1
);
389 kvm_eq
.qindex
= xive_get_field32(END_W1_PAGE_OFF
, end
->w1
);
395 /* Encode the tuple (server, prio) as a KVM EQ index */
396 spapr_xive_end_to_target(end_blk
, end_idx
, &server
, &priority
);
398 kvm_eq_idx
= priority
<< KVM_XIVE_EQ_PRIORITY_SHIFT
&
399 KVM_XIVE_EQ_PRIORITY_MASK
;
400 kvm_eq_idx
|= server
<< KVM_XIVE_EQ_SERVER_SHIFT
&
401 KVM_XIVE_EQ_SERVER_MASK
;
403 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_EQ_CONFIG
, kvm_eq_idx
,
404 &kvm_eq
, true, &local_err
);
406 error_propagate(errp
, local_err
);
411 void kvmppc_xive_reset(SpaprXive
*xive
, Error
**errp
)
413 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_CTRL
, KVM_DEV_XIVE_RESET
,
417 static void kvmppc_xive_get_queues(SpaprXive
*xive
, Error
**errp
)
419 Error
*local_err
= NULL
;
422 for (i
= 0; i
< xive
->nr_ends
; i
++) {
423 if (!xive_end_is_valid(&xive
->endt
[i
])) {
427 kvmppc_xive_get_queue_config(xive
, SPAPR_XIVE_BLOCK_ID
, i
,
428 &xive
->endt
[i
], &local_err
);
430 error_propagate(errp
, local_err
);
436 void kvmppc_xive_synchronize_state(SpaprXive
*xive
, Error
**errp
)
438 kvmppc_xive_source_get_state(&xive
->source
);
440 /* EAT: there is no extra state to query from KVM */
443 kvmppc_xive_get_queues(xive
, errp
);
446 static void *kvmppc_xive_mmap(SpaprXive
*xive
, int pgoff
, size_t len
,
450 uint32_t page_shift
= 16; /* TODO: fix page_shift */
452 addr
= mmap(NULL
, len
, PROT_WRITE
| PROT_READ
, MAP_SHARED
, xive
->fd
,
453 pgoff
<< page_shift
);
454 if (addr
== MAP_FAILED
) {
455 error_setg_errno(errp
, errno
, "XIVE: unable to set memory mapping");
463 * All the XIVE memory regions are now backed by mappings from the KVM
466 void kvmppc_xive_connect(SpaprXive
*xive
, Error
**errp
)
468 XiveSource
*xsrc
= &xive
->source
;
469 XiveENDSource
*end_xsrc
= &xive
->end_source
;
470 Error
*local_err
= NULL
;
471 size_t esb_len
= (1ull << xsrc
->esb_shift
) * xsrc
->nr_irqs
;
472 size_t tima_len
= 4ull << TM_SHIFT
;
474 if (!kvmppc_has_cap_xive()) {
475 error_setg(errp
, "IRQ_XIVE capability must be present for KVM");
479 /* First, create the KVM XIVE device */
480 xive
->fd
= kvm_create_device(kvm_state
, KVM_DEV_TYPE_XIVE
, false);
482 error_setg_errno(errp
, -xive
->fd
, "XIVE: error creating KVM device");
487 * 1. Source ESB pages - KVM mapping
489 xsrc
->esb_mmap
= kvmppc_xive_mmap(xive
, KVM_XIVE_ESB_PAGE_OFFSET
, esb_len
,
492 error_propagate(errp
, local_err
);
496 memory_region_init_ram_device_ptr(&xsrc
->esb_mmio
, OBJECT(xsrc
),
497 "xive.esb", esb_len
, xsrc
->esb_mmap
);
498 sysbus_init_mmio(SYS_BUS_DEVICE(xive
), &xsrc
->esb_mmio
);
501 * 2. END ESB pages (No KVM support yet)
503 sysbus_init_mmio(SYS_BUS_DEVICE(xive
), &end_xsrc
->esb_mmio
);
506 * 3. TIMA pages - KVM mapping
508 xive
->tm_mmap
= kvmppc_xive_mmap(xive
, KVM_XIVE_TIMA_PAGE_OFFSET
, tima_len
,
511 error_propagate(errp
, local_err
);
514 memory_region_init_ram_device_ptr(&xive
->tm_mmio
, OBJECT(xive
),
515 "xive.tima", tima_len
, xive
->tm_mmap
);
516 sysbus_init_mmio(SYS_BUS_DEVICE(xive
), &xive
->tm_mmio
);
518 kvm_kernel_irqchip
= true;
519 kvm_msi_via_irqfd_allowed
= true;
520 kvm_gsi_direct_mapping
= true;
522 /* Map all regions */
523 spapr_xive_map_mmio(xive
);