spapr/xive: add state synchronization with KVM
[qemu.git] / hw / intc / spapr_xive_kvm.c
blob8dd4f96e0b97c7cfc7e7e8725e148b7e502b0e7f
1 /*
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.
8 */
10 #include "qemu/osdep.h"
11 #include "qemu/log.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"
20 #include "kvm_ppc.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;
32 } KVMEnabledCPU;
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) {
44 return true;
47 return false;
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 };
66 int ret;
68 ret = kvm_get_one_reg(tctx->cs, KVM_REG_PPC_VP_STATE, state);
69 if (ret != 0) {
70 error_setg_errno(errp, errno,
71 "XIVE: could not capture KVM state of CPU %ld",
72 kvm_arch_vcpu_id(tctx->cs));
73 return;
76 /* word0 and word1 of the OS ring. */
77 *((uint64_t *) &tctx->regs[TM_QW1_OS]) = state[0];
80 typedef struct {
81 XiveTCTX *tctx;
82 Error *err;
83 } XiveCpuGetState;
85 static void kvmppc_xive_cpu_do_synchronize_state(CPUState *cpu,
86 run_on_cpu_data arg)
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)
95 XiveCpuGetState s = {
96 .tctx = tctx,
97 .err = NULL,
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));
106 if (s.err) {
107 error_propagate(errp, s.err);
108 return;
112 void kvmppc_xive_cpu_connect(XiveTCTX *tctx, Error **errp)
114 SpaprXive *xive = SPAPR_MACHINE(qdev_get_machine())->xive;
115 unsigned long vcpu_id;
116 int ret;
118 /* Check if CPU was hot unplugged and replugged. */
119 if (kvm_cpu_is_enabled(tctx->cs)) {
120 return;
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,
126 vcpu_id, 0);
127 if (ret < 0) {
128 error_setg(errp, "XIVE: unable to connect CPU%ld to KVM device: %s",
129 vcpu_id, strerror(errno));
130 return;
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,
141 Error **errp)
143 uint32_t end_idx;
144 uint32_t end_blk;
145 uint8_t priority;
146 uint32_t server;
147 bool masked;
148 uint32_t eisn;
149 uint64_t kvm_src;
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);
172 if (local_err) {
173 error_propagate(errp, local_err);
174 return;
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,
181 NULL, true, errp);
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
187 * MSI.
189 void kvmppc_xive_source_reset_one(XiveSource *xsrc, int srcno, Error **errp)
191 SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
192 uint64_t state = 0;
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,
202 true, errp);
205 void kvmppc_xive_source_reset(XiveSource *xsrc, Error **errp)
207 int i;
209 for (i = 0; i < xsrc->nr_irqs; i++) {
210 Error *local_err = NULL;
212 kvmppc_xive_source_reset_one(xsrc, i, &local_err);
213 if (local_err) {
214 error_propagate(errp, local_err);
215 return;
221 * This is used to perform the magic loads on the ESB pages, described
222 * in xive.h.
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) +
230 offset;
232 if (write) {
233 *addr = cpu_to_be64(data);
234 return -1;
235 } else {
236 /* Prevent the compiler from optimizing away the load */
237 volatile uint64_t value = be64_to_cpu(*addr);
238 return value;
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);
251 *addr = 0x0;
254 uint64_t kvmppc_xive_esb_rw(XiveSource *xsrc, int srcno, uint32_t offset,
255 uint64_t data, bool write)
257 if (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
264 * asserted.
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);
272 return 0;
273 } else {
274 return xive_esb_rw(xsrc, srcno, offset, 0, 0);
278 static void kvmppc_xive_source_get_state(XiveSource *xsrc)
280 int i;
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;
295 int rc;
297 args.irq = srcno;
298 if (!xive_source_irq_is_lsi(xsrc, srcno)) {
299 if (!val) {
300 return;
302 args.level = KVM_INTERRUPT_SET;
303 } else {
304 if (val) {
305 xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
306 args.level = KVM_INTERRUPT_SET_LEVEL;
307 } else {
308 xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
309 args.level = KVM_INTERRUPT_UNSET;
312 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
313 if (rc < 0) {
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,
323 Error **errp)
325 struct kvm_ppc_xive_eq kvm_eq = { 0 };
326 uint64_t kvm_eq_idx;
327 uint8_t priority;
328 uint32_t server;
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);
343 if (local_err) {
344 error_propagate(errp, local_err);
345 return;
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,
359 Error **errp)
361 struct kvm_ppc_xive_eq kvm_eq = { 0 };
362 uint64_t kvm_eq_idx;
363 uint8_t priority;
364 uint32_t server;
365 Error *local_err = NULL;
368 * Build the KVM state from the local END structure.
371 kvm_eq.flags = 0;
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
379 * account.
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);
390 } else {
391 kvm_eq.qshift = 0;
392 kvm_eq.qaddr = 0;
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);
405 if (local_err) {
406 error_propagate(errp, local_err);
407 return;
411 void kvmppc_xive_reset(SpaprXive *xive, Error **errp)
413 kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_CTRL, KVM_DEV_XIVE_RESET,
414 NULL, true, errp);
417 static void kvmppc_xive_get_queues(SpaprXive *xive, Error **errp)
419 Error *local_err = NULL;
420 int i;
422 for (i = 0; i < xive->nr_ends; i++) {
423 if (!xive_end_is_valid(&xive->endt[i])) {
424 continue;
427 kvmppc_xive_get_queue_config(xive, SPAPR_XIVE_BLOCK_ID, i,
428 &xive->endt[i], &local_err);
429 if (local_err) {
430 error_propagate(errp, local_err);
431 return;
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 */
442 /* ENDT */
443 kvmppc_xive_get_queues(xive, errp);
446 static void *kvmppc_xive_mmap(SpaprXive *xive, int pgoff, size_t len,
447 Error **errp)
449 void *addr;
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");
456 return NULL;
459 return addr;
463 * All the XIVE memory regions are now backed by mappings from the KVM
464 * XIVE device.
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");
476 return;
479 /* First, create the KVM XIVE device */
480 xive->fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_XIVE, false);
481 if (xive->fd < 0) {
482 error_setg_errno(errp, -xive->fd, "XIVE: error creating KVM device");
483 return;
487 * 1. Source ESB pages - KVM mapping
489 xsrc->esb_mmap = kvmppc_xive_mmap(xive, KVM_XIVE_ESB_PAGE_OFFSET, esb_len,
490 &local_err);
491 if (local_err) {
492 error_propagate(errp, local_err);
493 return;
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,
509 &local_err);
510 if (local_err) {
511 error_propagate(errp, local_err);
512 return;
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);