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)
64 void kvmppc_xive_cpu_connect(XiveTCTX
*tctx
, Error
**errp
)
66 SpaprXive
*xive
= SPAPR_MACHINE(qdev_get_machine())->xive
;
67 unsigned long vcpu_id
;
70 /* Check if CPU was hot unplugged and replugged. */
71 if (kvm_cpu_is_enabled(tctx
->cs
)) {
75 vcpu_id
= kvm_arch_vcpu_id(tctx
->cs
);
77 ret
= kvm_vcpu_enable_cap(tctx
->cs
, KVM_CAP_PPC_IRQ_XIVE
, 0, xive
->fd
,
80 error_setg(errp
, "XIVE: unable to connect CPU%ld to KVM device: %s",
81 vcpu_id
, strerror(errno
));
85 kvm_cpu_enable(tctx
->cs
);
89 * XIVE Interrupt Source (KVM)
92 void kvmppc_xive_set_source_config(SpaprXive
*xive
, uint32_t lisn
, XiveEAS
*eas
,
102 Error
*local_err
= NULL
;
104 assert(xive_eas_is_valid(eas
));
106 end_idx
= xive_get_field64(EAS_END_INDEX
, eas
->w
);
107 end_blk
= xive_get_field64(EAS_END_BLOCK
, eas
->w
);
108 eisn
= xive_get_field64(EAS_END_DATA
, eas
->w
);
109 masked
= xive_eas_is_masked(eas
);
111 spapr_xive_end_to_target(end_blk
, end_idx
, &server
, &priority
);
113 kvm_src
= priority
<< KVM_XIVE_SOURCE_PRIORITY_SHIFT
&
114 KVM_XIVE_SOURCE_PRIORITY_MASK
;
115 kvm_src
|= server
<< KVM_XIVE_SOURCE_SERVER_SHIFT
&
116 KVM_XIVE_SOURCE_SERVER_MASK
;
117 kvm_src
|= ((uint64_t) masked
<< KVM_XIVE_SOURCE_MASKED_SHIFT
) &
118 KVM_XIVE_SOURCE_MASKED_MASK
;
119 kvm_src
|= ((uint64_t)eisn
<< KVM_XIVE_SOURCE_EISN_SHIFT
) &
120 KVM_XIVE_SOURCE_EISN_MASK
;
122 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_SOURCE_CONFIG
, lisn
,
123 &kvm_src
, true, &local_err
);
125 error_propagate(errp
, local_err
);
130 void kvmppc_xive_sync_source(SpaprXive
*xive
, uint32_t lisn
, Error
**errp
)
132 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_SOURCE_SYNC
, lisn
,
137 * At reset, the interrupt sources are simply created and MASKED. We
138 * only need to inform the KVM XIVE device about their type: LSI or
141 void kvmppc_xive_source_reset_one(XiveSource
*xsrc
, int srcno
, Error
**errp
)
143 SpaprXive
*xive
= SPAPR_XIVE(xsrc
->xive
);
146 if (xive_source_irq_is_lsi(xsrc
, srcno
)) {
147 state
|= KVM_XIVE_LEVEL_SENSITIVE
;
148 if (xsrc
->status
[srcno
] & XIVE_STATUS_ASSERTED
) {
149 state
|= KVM_XIVE_LEVEL_ASSERTED
;
153 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_SOURCE
, srcno
, &state
,
157 void kvmppc_xive_source_reset(XiveSource
*xsrc
, Error
**errp
)
161 for (i
= 0; i
< xsrc
->nr_irqs
; i
++) {
162 Error
*local_err
= NULL
;
164 kvmppc_xive_source_reset_one(xsrc
, i
, &local_err
);
166 error_propagate(errp
, local_err
);
173 * This is used to perform the magic loads on the ESB pages, described
176 * Memory barriers should not be needed for loads (no store for now).
178 static uint64_t xive_esb_rw(XiveSource
*xsrc
, int srcno
, uint32_t offset
,
179 uint64_t data
, bool write
)
181 uint64_t *addr
= xsrc
->esb_mmap
+ xive_source_esb_mgmt(xsrc
, srcno
) +
185 *addr
= cpu_to_be64(data
);
188 /* Prevent the compiler from optimizing away the load */
189 volatile uint64_t value
= be64_to_cpu(*addr
);
194 static uint8_t xive_esb_read(XiveSource
*xsrc
, int srcno
, uint32_t offset
)
196 return xive_esb_rw(xsrc
, srcno
, offset
, 0, 0) & 0x3;
199 static void xive_esb_trigger(XiveSource
*xsrc
, int srcno
)
201 uint64_t *addr
= xsrc
->esb_mmap
+ xive_source_esb_page(xsrc
, srcno
);
206 uint64_t kvmppc_xive_esb_rw(XiveSource
*xsrc
, int srcno
, uint32_t offset
,
207 uint64_t data
, bool write
)
210 return xive_esb_rw(xsrc
, srcno
, offset
, data
, 1);
214 * Special Load EOI handling for LSI sources. Q bit is never set
215 * and the interrupt should be re-triggered if the level is still
218 if (xive_source_irq_is_lsi(xsrc
, srcno
) &&
219 offset
== XIVE_ESB_LOAD_EOI
) {
220 xive_esb_read(xsrc
, srcno
, XIVE_ESB_SET_PQ_00
);
221 if (xsrc
->status
[srcno
] & XIVE_STATUS_ASSERTED
) {
222 xive_esb_trigger(xsrc
, srcno
);
226 return xive_esb_rw(xsrc
, srcno
, offset
, 0, 0);
230 void kvmppc_xive_source_set_irq(void *opaque
, int srcno
, int val
)
232 XiveSource
*xsrc
= opaque
;
233 struct kvm_irq_level args
;
237 if (!xive_source_irq_is_lsi(xsrc
, srcno
)) {
241 args
.level
= KVM_INTERRUPT_SET
;
244 xsrc
->status
[srcno
] |= XIVE_STATUS_ASSERTED
;
245 args
.level
= KVM_INTERRUPT_SET_LEVEL
;
247 xsrc
->status
[srcno
] &= ~XIVE_STATUS_ASSERTED
;
248 args
.level
= KVM_INTERRUPT_UNSET
;
251 rc
= kvm_vm_ioctl(kvm_state
, KVM_IRQ_LINE
, &args
);
253 error_report("XIVE: kvm_irq_line() failed : %s", strerror(errno
));
258 * sPAPR XIVE interrupt controller (KVM)
260 void kvmppc_xive_get_queue_config(SpaprXive
*xive
, uint8_t end_blk
,
261 uint32_t end_idx
, XiveEND
*end
,
264 struct kvm_ppc_xive_eq kvm_eq
= { 0 };
268 Error
*local_err
= NULL
;
270 assert(xive_end_is_valid(end
));
272 /* Encode the tuple (server, prio) as a KVM EQ index */
273 spapr_xive_end_to_target(end_blk
, end_idx
, &server
, &priority
);
275 kvm_eq_idx
= priority
<< KVM_XIVE_EQ_PRIORITY_SHIFT
&
276 KVM_XIVE_EQ_PRIORITY_MASK
;
277 kvm_eq_idx
|= server
<< KVM_XIVE_EQ_SERVER_SHIFT
&
278 KVM_XIVE_EQ_SERVER_MASK
;
280 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_EQ_CONFIG
, kvm_eq_idx
,
281 &kvm_eq
, false, &local_err
);
283 error_propagate(errp
, local_err
);
288 * The EQ index and toggle bit are updated by HW. These are the
289 * only fields from KVM we want to update QEMU with. The other END
290 * fields should already be in the QEMU END table.
292 end
->w1
= xive_set_field32(END_W1_GENERATION
, 0ul, kvm_eq
.qtoggle
) |
293 xive_set_field32(END_W1_PAGE_OFF
, 0ul, kvm_eq
.qindex
);
296 void kvmppc_xive_set_queue_config(SpaprXive
*xive
, uint8_t end_blk
,
297 uint32_t end_idx
, XiveEND
*end
,
300 struct kvm_ppc_xive_eq kvm_eq
= { 0 };
304 Error
*local_err
= NULL
;
307 * Build the KVM state from the local END structure.
311 if (xive_get_field32(END_W0_UCOND_NOTIFY
, end
->w0
)) {
312 kvm_eq
.flags
|= KVM_XIVE_EQ_ALWAYS_NOTIFY
;
316 * If the hcall is disabling the EQ, set the size and page address
317 * to zero. When migrating, only valid ENDs are taken into
320 if (xive_end_is_valid(end
)) {
321 kvm_eq
.qshift
= xive_get_field32(END_W0_QSIZE
, end
->w0
) + 12;
322 kvm_eq
.qaddr
= xive_end_qaddr(end
);
324 * The EQ toggle bit and index should only be relevant when
325 * restoring the EQ state
327 kvm_eq
.qtoggle
= xive_get_field32(END_W1_GENERATION
, end
->w1
);
328 kvm_eq
.qindex
= xive_get_field32(END_W1_PAGE_OFF
, end
->w1
);
334 /* Encode the tuple (server, prio) as a KVM EQ index */
335 spapr_xive_end_to_target(end_blk
, end_idx
, &server
, &priority
);
337 kvm_eq_idx
= priority
<< KVM_XIVE_EQ_PRIORITY_SHIFT
&
338 KVM_XIVE_EQ_PRIORITY_MASK
;
339 kvm_eq_idx
|= server
<< KVM_XIVE_EQ_SERVER_SHIFT
&
340 KVM_XIVE_EQ_SERVER_MASK
;
342 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_EQ_CONFIG
, kvm_eq_idx
,
343 &kvm_eq
, true, &local_err
);
345 error_propagate(errp
, local_err
);
350 void kvmppc_xive_reset(SpaprXive
*xive
, Error
**errp
)
352 kvm_device_access(xive
->fd
, KVM_DEV_XIVE_GRP_CTRL
, KVM_DEV_XIVE_RESET
,
356 static void *kvmppc_xive_mmap(SpaprXive
*xive
, int pgoff
, size_t len
,
360 uint32_t page_shift
= 16; /* TODO: fix page_shift */
362 addr
= mmap(NULL
, len
, PROT_WRITE
| PROT_READ
, MAP_SHARED
, xive
->fd
,
363 pgoff
<< page_shift
);
364 if (addr
== MAP_FAILED
) {
365 error_setg_errno(errp
, errno
, "XIVE: unable to set memory mapping");
373 * All the XIVE memory regions are now backed by mappings from the KVM
376 void kvmppc_xive_connect(SpaprXive
*xive
, Error
**errp
)
378 XiveSource
*xsrc
= &xive
->source
;
379 XiveENDSource
*end_xsrc
= &xive
->end_source
;
380 Error
*local_err
= NULL
;
381 size_t esb_len
= (1ull << xsrc
->esb_shift
) * xsrc
->nr_irqs
;
382 size_t tima_len
= 4ull << TM_SHIFT
;
384 if (!kvmppc_has_cap_xive()) {
385 error_setg(errp
, "IRQ_XIVE capability must be present for KVM");
389 /* First, create the KVM XIVE device */
390 xive
->fd
= kvm_create_device(kvm_state
, KVM_DEV_TYPE_XIVE
, false);
392 error_setg_errno(errp
, -xive
->fd
, "XIVE: error creating KVM device");
397 * 1. Source ESB pages - KVM mapping
399 xsrc
->esb_mmap
= kvmppc_xive_mmap(xive
, KVM_XIVE_ESB_PAGE_OFFSET
, esb_len
,
402 error_propagate(errp
, local_err
);
406 memory_region_init_ram_device_ptr(&xsrc
->esb_mmio
, OBJECT(xsrc
),
407 "xive.esb", esb_len
, xsrc
->esb_mmap
);
408 sysbus_init_mmio(SYS_BUS_DEVICE(xive
), &xsrc
->esb_mmio
);
411 * 2. END ESB pages (No KVM support yet)
413 sysbus_init_mmio(SYS_BUS_DEVICE(xive
), &end_xsrc
->esb_mmio
);
416 * 3. TIMA pages - KVM mapping
418 xive
->tm_mmap
= kvmppc_xive_mmap(xive
, KVM_XIVE_TIMA_PAGE_OFFSET
, tima_len
,
421 error_propagate(errp
, local_err
);
424 memory_region_init_ram_device_ptr(&xive
->tm_mmio
, OBJECT(xive
),
425 "xive.tima", tima_len
, xive
->tm_mmap
);
426 sysbus_init_mmio(SYS_BUS_DEVICE(xive
), &xive
->tm_mmio
);
428 kvm_kernel_irqchip
= true;
429 kvm_msi_via_irqfd_allowed
= true;
430 kvm_gsi_direct_mapping
= true;
432 /* Map all regions */
433 spapr_xive_map_mmio(xive
);