xref: /qemu/hw/intc/spapr_xive_kvm.c (revision 74e51a38f74537d7260f0d45fb01e675f1558163)
138afd772SCédric Le Goater /*
238afd772SCédric Le Goater  * QEMU PowerPC sPAPR XIVE interrupt controller model
338afd772SCédric Le Goater  *
438afd772SCédric Le Goater  * Copyright (c) 2017-2019, IBM Corporation.
538afd772SCédric Le Goater  *
638afd772SCédric Le Goater  * This code is licensed under the GPL version 2 or later. See the
738afd772SCédric Le Goater  * COPYING file in the top-level directory.
838afd772SCédric Le Goater  */
938afd772SCédric Le Goater 
1038afd772SCédric Le Goater #include "qemu/osdep.h"
1138afd772SCédric Le Goater #include "qemu/log.h"
1238afd772SCédric Le Goater #include "qemu/error-report.h"
1338afd772SCédric Le Goater #include "qapi/error.h"
1438afd772SCédric Le Goater #include "target/ppc/cpu.h"
1538afd772SCédric Le Goater #include "sysemu/cpus.h"
1638afd772SCédric Le Goater #include "sysemu/kvm.h"
1754d31236SMarkus Armbruster #include "sysemu/runstate.h"
1838afd772SCédric Le Goater #include "hw/ppc/spapr.h"
19277dd3d7SCédric Le Goater #include "hw/ppc/spapr_cpu_core.h"
2038afd772SCédric Le Goater #include "hw/ppc/spapr_xive.h"
2138afd772SCédric Le Goater #include "hw/ppc/xive.h"
2238afd772SCédric Le Goater #include "kvm_ppc.h"
2338afd772SCédric Le Goater 
2438afd772SCédric Le Goater #include <sys/ioctl.h>
2538afd772SCédric Le Goater 
2638afd772SCédric Le Goater /*
2738afd772SCédric Le Goater  * Helpers for CPU hotplug
2838afd772SCédric Le Goater  *
2938afd772SCédric Le Goater  * TODO: make a common KVMEnabledCPU layer for XICS and XIVE
3038afd772SCédric Le Goater  */
3138afd772SCédric Le Goater typedef struct KVMEnabledCPU {
3238afd772SCédric Le Goater     unsigned long vcpu_id;
3338afd772SCédric Le Goater     QLIST_ENTRY(KVMEnabledCPU) node;
3438afd772SCédric Le Goater } KVMEnabledCPU;
3538afd772SCédric Le Goater 
3638afd772SCédric Le Goater static QLIST_HEAD(, KVMEnabledCPU)
3738afd772SCédric Le Goater     kvm_enabled_cpus = QLIST_HEAD_INITIALIZER(&kvm_enabled_cpus);
3838afd772SCédric Le Goater 
3938afd772SCédric Le Goater static bool kvm_cpu_is_enabled(CPUState *cs)
4038afd772SCédric Le Goater {
4138afd772SCédric Le Goater     KVMEnabledCPU *enabled_cpu;
4238afd772SCédric Le Goater     unsigned long vcpu_id = kvm_arch_vcpu_id(cs);
4338afd772SCédric Le Goater 
4438afd772SCédric Le Goater     QLIST_FOREACH(enabled_cpu, &kvm_enabled_cpus, node) {
4538afd772SCédric Le Goater         if (enabled_cpu->vcpu_id == vcpu_id) {
4638afd772SCédric Le Goater             return true;
4738afd772SCédric Le Goater         }
4838afd772SCédric Le Goater     }
4938afd772SCédric Le Goater     return false;
5038afd772SCédric Le Goater }
5138afd772SCédric Le Goater 
5238afd772SCédric Le Goater static void kvm_cpu_enable(CPUState *cs)
5338afd772SCédric Le Goater {
5438afd772SCédric Le Goater     KVMEnabledCPU *enabled_cpu;
5538afd772SCédric Le Goater     unsigned long vcpu_id = kvm_arch_vcpu_id(cs);
5638afd772SCédric Le Goater 
5738afd772SCédric Le Goater     enabled_cpu = g_malloc(sizeof(*enabled_cpu));
5838afd772SCédric Le Goater     enabled_cpu->vcpu_id = vcpu_id;
5938afd772SCédric Le Goater     QLIST_INSERT_HEAD(&kvm_enabled_cpus, enabled_cpu, node);
6038afd772SCédric Le Goater }
6138afd772SCédric Le Goater 
6256b11587SCédric Le Goater static void kvm_cpu_disable_all(void)
6356b11587SCédric Le Goater {
6456b11587SCédric Le Goater     KVMEnabledCPU *enabled_cpu, *next;
6556b11587SCédric Le Goater 
6656b11587SCédric Le Goater     QLIST_FOREACH_SAFE(enabled_cpu, &kvm_enabled_cpus, node, next) {
6756b11587SCédric Le Goater         QLIST_REMOVE(enabled_cpu, node);
6856b11587SCédric Le Goater         g_free(enabled_cpu);
6956b11587SCédric Le Goater     }
7056b11587SCédric Le Goater }
7156b11587SCédric Le Goater 
7238afd772SCédric Le Goater /*
7338afd772SCédric Le Goater  * XIVE Thread Interrupt Management context (KVM)
7438afd772SCédric Le Goater  */
75277dd3d7SCédric Le Goater 
76310cda5bSCédric Le Goater void kvmppc_xive_cpu_set_state(XiveTCTX *tctx, Error **errp)
77277dd3d7SCédric Le Goater {
78*74e51a38SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(tctx->xptr);
79277dd3d7SCédric Le Goater     uint64_t state[2];
80277dd3d7SCédric Le Goater     int ret;
81277dd3d7SCédric Le Goater 
82310cda5bSCédric Le Goater     /* The KVM XIVE device is not in use yet */
83310cda5bSCédric Le Goater     if (xive->fd == -1) {
84310cda5bSCédric Le Goater         return;
85310cda5bSCédric Le Goater     }
86310cda5bSCédric Le Goater 
87277dd3d7SCédric Le Goater     /* word0 and word1 of the OS ring. */
88277dd3d7SCédric Le Goater     state[0] = *((uint64_t *) &tctx->regs[TM_QW1_OS]);
89277dd3d7SCédric Le Goater 
90277dd3d7SCédric Le Goater     ret = kvm_set_one_reg(tctx->cs, KVM_REG_PPC_VP_STATE, state);
91277dd3d7SCédric Le Goater     if (ret != 0) {
92277dd3d7SCédric Le Goater         error_setg_errno(errp, errno,
93277dd3d7SCédric Le Goater                          "XIVE: could not restore KVM state of CPU %ld",
94277dd3d7SCédric Le Goater                          kvm_arch_vcpu_id(tctx->cs));
95277dd3d7SCédric Le Goater     }
96277dd3d7SCédric Le Goater }
97277dd3d7SCédric Le Goater 
98277dd3d7SCédric Le Goater void kvmppc_xive_cpu_get_state(XiveTCTX *tctx, Error **errp)
997bfc759cSCédric Le Goater {
100*74e51a38SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(tctx->xptr);
1017bfc759cSCédric Le Goater     uint64_t state[2] = { 0 };
1027bfc759cSCédric Le Goater     int ret;
1037bfc759cSCédric Le Goater 
1043bf84e99SCédric Le Goater     /* The KVM XIVE device is not in use */
1053bf84e99SCédric Le Goater     if (xive->fd == -1) {
1063bf84e99SCédric Le Goater         return;
1073bf84e99SCédric Le Goater     }
1083bf84e99SCédric Le Goater 
1097bfc759cSCédric Le Goater     ret = kvm_get_one_reg(tctx->cs, KVM_REG_PPC_VP_STATE, state);
1107bfc759cSCédric Le Goater     if (ret != 0) {
1117bfc759cSCédric Le Goater         error_setg_errno(errp, errno,
1127bfc759cSCédric Le Goater                          "XIVE: could not capture KVM state of CPU %ld",
1137bfc759cSCédric Le Goater                          kvm_arch_vcpu_id(tctx->cs));
1147bfc759cSCédric Le Goater         return;
1157bfc759cSCédric Le Goater     }
1167bfc759cSCédric Le Goater 
1177bfc759cSCédric Le Goater     /* word0 and word1 of the OS ring. */
1187bfc759cSCédric Le Goater     *((uint64_t *) &tctx->regs[TM_QW1_OS]) = state[0];
1197bfc759cSCédric Le Goater }
1207bfc759cSCédric Le Goater 
1217bfc759cSCédric Le Goater typedef struct {
1227bfc759cSCédric Le Goater     XiveTCTX *tctx;
1237bfc759cSCédric Le Goater     Error *err;
1247bfc759cSCédric Le Goater } XiveCpuGetState;
1257bfc759cSCédric Le Goater 
1267bfc759cSCédric Le Goater static void kvmppc_xive_cpu_do_synchronize_state(CPUState *cpu,
1277bfc759cSCédric Le Goater                                                  run_on_cpu_data arg)
1287bfc759cSCédric Le Goater {
1297bfc759cSCédric Le Goater     XiveCpuGetState *s = arg.host_ptr;
1307bfc759cSCédric Le Goater 
1317bfc759cSCédric Le Goater     kvmppc_xive_cpu_get_state(s->tctx, &s->err);
1327bfc759cSCédric Le Goater }
1337bfc759cSCédric Le Goater 
1347bfc759cSCédric Le Goater void kvmppc_xive_cpu_synchronize_state(XiveTCTX *tctx, Error **errp)
1357bfc759cSCédric Le Goater {
1367bfc759cSCédric Le Goater     XiveCpuGetState s = {
1377bfc759cSCédric Le Goater         .tctx = tctx,
1387bfc759cSCédric Le Goater         .err = NULL,
1397bfc759cSCédric Le Goater     };
1407bfc759cSCédric Le Goater 
1417bfc759cSCédric Le Goater     /*
1427bfc759cSCédric Le Goater      * Kick the vCPU to make sure they are available for the KVM ioctl.
1437bfc759cSCédric Le Goater      */
1447bfc759cSCédric Le Goater     run_on_cpu(tctx->cs, kvmppc_xive_cpu_do_synchronize_state,
1457bfc759cSCédric Le Goater                RUN_ON_CPU_HOST_PTR(&s));
1467bfc759cSCédric Le Goater 
1477bfc759cSCédric Le Goater     if (s.err) {
1487bfc759cSCédric Le Goater         error_propagate(errp, s.err);
1497bfc759cSCédric Le Goater         return;
1507bfc759cSCédric Le Goater     }
1517bfc759cSCédric Le Goater }
15238afd772SCédric Le Goater 
15338afd772SCédric Le Goater void kvmppc_xive_cpu_connect(XiveTCTX *tctx, Error **errp)
15438afd772SCédric Le Goater {
155*74e51a38SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(tctx->xptr);
15638afd772SCédric Le Goater     unsigned long vcpu_id;
15738afd772SCédric Le Goater     int ret;
15838afd772SCédric Le Goater 
1593bf84e99SCédric Le Goater     /* The KVM XIVE device is not in use */
1603bf84e99SCédric Le Goater     if (xive->fd == -1) {
1613bf84e99SCédric Le Goater         return;
1623bf84e99SCédric Le Goater     }
1633bf84e99SCédric Le Goater 
16438afd772SCédric Le Goater     /* Check if CPU was hot unplugged and replugged. */
16538afd772SCédric Le Goater     if (kvm_cpu_is_enabled(tctx->cs)) {
16638afd772SCédric Le Goater         return;
16738afd772SCédric Le Goater     }
16838afd772SCédric Le Goater 
16938afd772SCédric Le Goater     vcpu_id = kvm_arch_vcpu_id(tctx->cs);
17038afd772SCédric Le Goater 
17138afd772SCédric Le Goater     ret = kvm_vcpu_enable_cap(tctx->cs, KVM_CAP_PPC_IRQ_XIVE, 0, xive->fd,
17238afd772SCédric Le Goater                               vcpu_id, 0);
17338afd772SCédric Le Goater     if (ret < 0) {
17474f23d43SGreg Kurz         Error *local_err = NULL;
17574f23d43SGreg Kurz 
17674f23d43SGreg Kurz         error_setg(&local_err,
17774f23d43SGreg Kurz                    "XIVE: unable to connect CPU%ld to KVM device: %s",
17838afd772SCédric Le Goater                    vcpu_id, strerror(errno));
17974f23d43SGreg Kurz         if (errno == ENOSPC) {
18074f23d43SGreg Kurz             error_append_hint(&local_err, "Try -smp maxcpus=N with N < %u\n",
181*74e51a38SGreg Kurz                               MACHINE(qdev_get_machine())->smp.max_cpus);
18274f23d43SGreg Kurz         }
18374f23d43SGreg Kurz         error_propagate(errp, local_err);
18438afd772SCédric Le Goater         return;
18538afd772SCédric Le Goater     }
18638afd772SCédric Le Goater 
18738afd772SCédric Le Goater     kvm_cpu_enable(tctx->cs);
18838afd772SCédric Le Goater }
18938afd772SCédric Le Goater 
19038afd772SCédric Le Goater /*
19138afd772SCédric Le Goater  * XIVE Interrupt Source (KVM)
19238afd772SCédric Le Goater  */
19338afd772SCédric Le Goater 
1940c575703SCédric Le Goater void kvmppc_xive_set_source_config(SpaprXive *xive, uint32_t lisn, XiveEAS *eas,
1950c575703SCédric Le Goater                                    Error **errp)
1960c575703SCédric Le Goater {
1970c575703SCédric Le Goater     uint32_t end_idx;
1980c575703SCédric Le Goater     uint32_t end_blk;
1990c575703SCédric Le Goater     uint8_t priority;
2000c575703SCédric Le Goater     uint32_t server;
2010c575703SCédric Le Goater     bool masked;
2020c575703SCédric Le Goater     uint32_t eisn;
2030c575703SCédric Le Goater     uint64_t kvm_src;
2040c575703SCédric Le Goater     Error *local_err = NULL;
2050c575703SCédric Le Goater 
2060c575703SCédric Le Goater     assert(xive_eas_is_valid(eas));
2070c575703SCédric Le Goater 
2080c575703SCédric Le Goater     end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
2090c575703SCédric Le Goater     end_blk = xive_get_field64(EAS_END_BLOCK, eas->w);
2100c575703SCédric Le Goater     eisn = xive_get_field64(EAS_END_DATA, eas->w);
2110c575703SCédric Le Goater     masked = xive_eas_is_masked(eas);
2120c575703SCédric Le Goater 
2130c575703SCédric Le Goater     spapr_xive_end_to_target(end_blk, end_idx, &server, &priority);
2140c575703SCédric Le Goater 
2150c575703SCédric Le Goater     kvm_src = priority << KVM_XIVE_SOURCE_PRIORITY_SHIFT &
2160c575703SCédric Le Goater         KVM_XIVE_SOURCE_PRIORITY_MASK;
2170c575703SCédric Le Goater     kvm_src |= server << KVM_XIVE_SOURCE_SERVER_SHIFT &
2180c575703SCédric Le Goater         KVM_XIVE_SOURCE_SERVER_MASK;
2190c575703SCédric Le Goater     kvm_src |= ((uint64_t) masked << KVM_XIVE_SOURCE_MASKED_SHIFT) &
2200c575703SCédric Le Goater         KVM_XIVE_SOURCE_MASKED_MASK;
2210c575703SCédric Le Goater     kvm_src |= ((uint64_t)eisn << KVM_XIVE_SOURCE_EISN_SHIFT) &
2220c575703SCédric Le Goater         KVM_XIVE_SOURCE_EISN_MASK;
2230c575703SCédric Le Goater 
2240c575703SCédric Le Goater     kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_SOURCE_CONFIG, lisn,
2250c575703SCédric Le Goater                       &kvm_src, true, &local_err);
2260c575703SCédric Le Goater     if (local_err) {
2270c575703SCédric Le Goater         error_propagate(errp, local_err);
2280c575703SCédric Le Goater         return;
2290c575703SCédric Le Goater     }
2300c575703SCédric Le Goater }
2310c575703SCédric Le Goater 
2320c575703SCédric Le Goater void kvmppc_xive_sync_source(SpaprXive *xive, uint32_t lisn, Error **errp)
2330c575703SCédric Le Goater {
2340c575703SCédric Le Goater     kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_SOURCE_SYNC, lisn,
2350c575703SCédric Le Goater                       NULL, true, errp);
2360c575703SCédric Le Goater }
2370c575703SCédric Le Goater 
23838afd772SCédric Le Goater /*
23938afd772SCédric Le Goater  * At reset, the interrupt sources are simply created and MASKED. We
24038afd772SCédric Le Goater  * only need to inform the KVM XIVE device about their type: LSI or
24138afd772SCédric Le Goater  * MSI.
24238afd772SCédric Le Goater  */
243e594c2adSDavid Gibson int kvmppc_xive_source_reset_one(XiveSource *xsrc, int srcno, Error **errp)
24438afd772SCédric Le Goater {
24538afd772SCédric Le Goater     SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
24638afd772SCédric Le Goater     uint64_t state = 0;
24738afd772SCédric Le Goater 
2483bf84e99SCédric Le Goater     /* The KVM XIVE device is not in use */
2493bf84e99SCédric Le Goater     if (xive->fd == -1) {
250e594c2adSDavid Gibson         return -ENODEV;
2513bf84e99SCédric Le Goater     }
2523bf84e99SCédric Le Goater 
25338afd772SCédric Le Goater     if (xive_source_irq_is_lsi(xsrc, srcno)) {
25438afd772SCédric Le Goater         state |= KVM_XIVE_LEVEL_SENSITIVE;
25538afd772SCédric Le Goater         if (xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
25638afd772SCédric Le Goater             state |= KVM_XIVE_LEVEL_ASSERTED;
25738afd772SCédric Le Goater         }
25838afd772SCédric Le Goater     }
25938afd772SCédric Le Goater 
260e594c2adSDavid Gibson     return kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_SOURCE, srcno, &state,
26138afd772SCédric Le Goater                              true, errp);
26238afd772SCédric Le Goater }
26338afd772SCédric Le Goater 
2643f777abcSCédric Le Goater static void kvmppc_xive_source_reset(XiveSource *xsrc, Error **errp)
26538afd772SCédric Le Goater {
2664c3539d4SCédric Le Goater     SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
26738afd772SCédric Le Goater     int i;
26838afd772SCédric Le Goater 
26938afd772SCédric Le Goater     for (i = 0; i < xsrc->nr_irqs; i++) {
27038afd772SCédric Le Goater         Error *local_err = NULL;
27138afd772SCédric Le Goater 
2724c3539d4SCédric Le Goater         if (!xive_eas_is_valid(&xive->eat[i])) {
2734c3539d4SCédric Le Goater             continue;
2744c3539d4SCédric Le Goater         }
2754c3539d4SCédric Le Goater 
27638afd772SCédric Le Goater         kvmppc_xive_source_reset_one(xsrc, i, &local_err);
27738afd772SCédric Le Goater         if (local_err) {
27838afd772SCédric Le Goater             error_propagate(errp, local_err);
27938afd772SCédric Le Goater             return;
28038afd772SCédric Le Goater         }
28138afd772SCédric Le Goater     }
28238afd772SCédric Le Goater }
28338afd772SCédric Le Goater 
2840c575703SCédric Le Goater /*
2850c575703SCédric Le Goater  * This is used to perform the magic loads on the ESB pages, described
2860c575703SCédric Le Goater  * in xive.h.
2870c575703SCédric Le Goater  *
2880c575703SCédric Le Goater  * Memory barriers should not be needed for loads (no store for now).
2890c575703SCédric Le Goater  */
2900c575703SCédric Le Goater static uint64_t xive_esb_rw(XiveSource *xsrc, int srcno, uint32_t offset,
2910c575703SCédric Le Goater                             uint64_t data, bool write)
2920c575703SCédric Le Goater {
2930c575703SCédric Le Goater     uint64_t *addr = xsrc->esb_mmap + xive_source_esb_mgmt(xsrc, srcno) +
2940c575703SCédric Le Goater         offset;
2950c575703SCédric Le Goater 
2960c575703SCédric Le Goater     if (write) {
2970c575703SCédric Le Goater         *addr = cpu_to_be64(data);
2980c575703SCédric Le Goater         return -1;
2990c575703SCédric Le Goater     } else {
3000c575703SCédric Le Goater         /* Prevent the compiler from optimizing away the load */
3010c575703SCédric Le Goater         volatile uint64_t value = be64_to_cpu(*addr);
3020c575703SCédric Le Goater         return value;
3030c575703SCédric Le Goater     }
3040c575703SCédric Le Goater }
3050c575703SCédric Le Goater 
3060c575703SCédric Le Goater static uint8_t xive_esb_read(XiveSource *xsrc, int srcno, uint32_t offset)
3070c575703SCédric Le Goater {
3080c575703SCédric Le Goater     return xive_esb_rw(xsrc, srcno, offset, 0, 0) & 0x3;
3090c575703SCédric Le Goater }
3100c575703SCédric Le Goater 
3110c575703SCédric Le Goater static void xive_esb_trigger(XiveSource *xsrc, int srcno)
3120c575703SCédric Le Goater {
3130c575703SCédric Le Goater     uint64_t *addr = xsrc->esb_mmap + xive_source_esb_page(xsrc, srcno);
3140c575703SCédric Le Goater 
3150c575703SCédric Le Goater     *addr = 0x0;
3160c575703SCédric Le Goater }
3170c575703SCédric Le Goater 
3180c575703SCédric Le Goater uint64_t kvmppc_xive_esb_rw(XiveSource *xsrc, int srcno, uint32_t offset,
3190c575703SCédric Le Goater                             uint64_t data, bool write)
3200c575703SCédric Le Goater {
3210c575703SCédric Le Goater     if (write) {
3220c575703SCédric Le Goater         return xive_esb_rw(xsrc, srcno, offset, data, 1);
3230c575703SCédric Le Goater     }
3240c575703SCédric Le Goater 
3250c575703SCédric Le Goater     /*
3260c575703SCédric Le Goater      * Special Load EOI handling for LSI sources. Q bit is never set
3270c575703SCédric Le Goater      * and the interrupt should be re-triggered if the level is still
3280c575703SCédric Le Goater      * asserted.
3290c575703SCédric Le Goater      */
3300c575703SCédric Le Goater     if (xive_source_irq_is_lsi(xsrc, srcno) &&
3310c575703SCédric Le Goater         offset == XIVE_ESB_LOAD_EOI) {
3320c575703SCédric Le Goater         xive_esb_read(xsrc, srcno, XIVE_ESB_SET_PQ_00);
3330c575703SCédric Le Goater         if (xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
3340c575703SCédric Le Goater             xive_esb_trigger(xsrc, srcno);
3350c575703SCédric Le Goater         }
3360c575703SCédric Le Goater         return 0;
3370c575703SCédric Le Goater     } else {
3380c575703SCédric Le Goater         return xive_esb_rw(xsrc, srcno, offset, 0, 0);
3390c575703SCédric Le Goater     }
3400c575703SCédric Le Goater }
3410c575703SCédric Le Goater 
3427bfc759cSCédric Le Goater static void kvmppc_xive_source_get_state(XiveSource *xsrc)
3437bfc759cSCédric Le Goater {
3444c3539d4SCédric Le Goater     SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
3457bfc759cSCédric Le Goater     int i;
3467bfc759cSCédric Le Goater 
3477bfc759cSCédric Le Goater     for (i = 0; i < xsrc->nr_irqs; i++) {
3484c3539d4SCédric Le Goater         uint8_t pq;
3494c3539d4SCédric Le Goater 
3504c3539d4SCédric Le Goater         if (!xive_eas_is_valid(&xive->eat[i])) {
3514c3539d4SCédric Le Goater             continue;
3524c3539d4SCédric Le Goater         }
3534c3539d4SCédric Le Goater 
3547bfc759cSCédric Le Goater         /* Perform a load without side effect to retrieve the PQ bits */
3554c3539d4SCédric Le Goater         pq = xive_esb_read(xsrc, i, XIVE_ESB_GET);
3567bfc759cSCédric Le Goater 
3577bfc759cSCédric Le Goater         /* and save PQ locally */
3587bfc759cSCédric Le Goater         xive_source_esb_set(xsrc, i, pq);
3597bfc759cSCédric Le Goater     }
3607bfc759cSCédric Le Goater }
3617bfc759cSCédric Le Goater 
36238afd772SCédric Le Goater void kvmppc_xive_source_set_irq(void *opaque, int srcno, int val)
36338afd772SCédric Le Goater {
36438afd772SCédric Le Goater     XiveSource *xsrc = opaque;
36538afd772SCédric Le Goater 
36638afd772SCédric Le Goater     if (!xive_source_irq_is_lsi(xsrc, srcno)) {
36738afd772SCédric Le Goater         if (!val) {
36838afd772SCédric Le Goater             return;
36938afd772SCédric Le Goater         }
37038afd772SCédric Le Goater     } else {
37138afd772SCédric Le Goater         if (val) {
37238afd772SCédric Le Goater             xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
37338afd772SCédric Le Goater         } else {
37438afd772SCédric Le Goater             xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
37538afd772SCédric Le Goater         }
37638afd772SCédric Le Goater     }
37758246041SGreg Kurz 
37858246041SGreg Kurz     xive_esb_trigger(xsrc, srcno);
37938afd772SCédric Le Goater }
38038afd772SCédric Le Goater 
38138afd772SCédric Le Goater /*
38238afd772SCédric Le Goater  * sPAPR XIVE interrupt controller (KVM)
38338afd772SCédric Le Goater  */
3840c575703SCédric Le Goater void kvmppc_xive_get_queue_config(SpaprXive *xive, uint8_t end_blk,
3850c575703SCédric Le Goater                                   uint32_t end_idx, XiveEND *end,
3860c575703SCédric Le Goater                                   Error **errp)
3870c575703SCédric Le Goater {
3880c575703SCédric Le Goater     struct kvm_ppc_xive_eq kvm_eq = { 0 };
3890c575703SCédric Le Goater     uint64_t kvm_eq_idx;
3900c575703SCédric Le Goater     uint8_t priority;
3910c575703SCédric Le Goater     uint32_t server;
3920c575703SCédric Le Goater     Error *local_err = NULL;
3930c575703SCédric Le Goater 
3940c575703SCédric Le Goater     assert(xive_end_is_valid(end));
3950c575703SCédric Le Goater 
3960c575703SCédric Le Goater     /* Encode the tuple (server, prio) as a KVM EQ index */
3970c575703SCédric Le Goater     spapr_xive_end_to_target(end_blk, end_idx, &server, &priority);
3980c575703SCédric Le Goater 
3990c575703SCédric Le Goater     kvm_eq_idx = priority << KVM_XIVE_EQ_PRIORITY_SHIFT &
4000c575703SCédric Le Goater             KVM_XIVE_EQ_PRIORITY_MASK;
4010c575703SCédric Le Goater     kvm_eq_idx |= server << KVM_XIVE_EQ_SERVER_SHIFT &
4020c575703SCédric Le Goater         KVM_XIVE_EQ_SERVER_MASK;
4030c575703SCédric Le Goater 
4040c575703SCédric Le Goater     kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_EQ_CONFIG, kvm_eq_idx,
4050c575703SCédric Le Goater                       &kvm_eq, false, &local_err);
4060c575703SCédric Le Goater     if (local_err) {
4070c575703SCédric Le Goater         error_propagate(errp, local_err);
4080c575703SCédric Le Goater         return;
4090c575703SCédric Le Goater     }
4100c575703SCédric Le Goater 
4110c575703SCédric Le Goater     /*
4120c575703SCédric Le Goater      * The EQ index and toggle bit are updated by HW. These are the
4130c575703SCédric Le Goater      * only fields from KVM we want to update QEMU with. The other END
4140c575703SCédric Le Goater      * fields should already be in the QEMU END table.
4150c575703SCédric Le Goater      */
4160c575703SCédric Le Goater     end->w1 = xive_set_field32(END_W1_GENERATION, 0ul, kvm_eq.qtoggle) |
4170c575703SCédric Le Goater         xive_set_field32(END_W1_PAGE_OFF, 0ul, kvm_eq.qindex);
4180c575703SCédric Le Goater }
4190c575703SCédric Le Goater 
4200c575703SCédric Le Goater void kvmppc_xive_set_queue_config(SpaprXive *xive, uint8_t end_blk,
4210c575703SCédric Le Goater                                   uint32_t end_idx, XiveEND *end,
4220c575703SCédric Le Goater                                   Error **errp)
4230c575703SCédric Le Goater {
4240c575703SCédric Le Goater     struct kvm_ppc_xive_eq kvm_eq = { 0 };
4250c575703SCédric Le Goater     uint64_t kvm_eq_idx;
4260c575703SCédric Le Goater     uint8_t priority;
4270c575703SCédric Le Goater     uint32_t server;
4280c575703SCédric Le Goater     Error *local_err = NULL;
4290c575703SCédric Le Goater 
4300c575703SCédric Le Goater     /*
4310c575703SCédric Le Goater      * Build the KVM state from the local END structure.
4320c575703SCédric Le Goater      */
4330c575703SCédric Le Goater 
4340c575703SCédric Le Goater     kvm_eq.flags = 0;
4350c575703SCédric Le Goater     if (xive_get_field32(END_W0_UCOND_NOTIFY, end->w0)) {
4360c575703SCédric Le Goater         kvm_eq.flags |= KVM_XIVE_EQ_ALWAYS_NOTIFY;
4370c575703SCédric Le Goater     }
4380c575703SCédric Le Goater 
4390c575703SCédric Le Goater     /*
4400c575703SCédric Le Goater      * If the hcall is disabling the EQ, set the size and page address
4410c575703SCédric Le Goater      * to zero. When migrating, only valid ENDs are taken into
4420c575703SCédric Le Goater      * account.
4430c575703SCédric Le Goater      */
4440c575703SCédric Le Goater     if (xive_end_is_valid(end)) {
4450c575703SCédric Le Goater         kvm_eq.qshift = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
4460c575703SCédric Le Goater         kvm_eq.qaddr  = xive_end_qaddr(end);
4470c575703SCédric Le Goater         /*
4480c575703SCédric Le Goater          * The EQ toggle bit and index should only be relevant when
4490c575703SCédric Le Goater          * restoring the EQ state
4500c575703SCédric Le Goater          */
4510c575703SCédric Le Goater         kvm_eq.qtoggle = xive_get_field32(END_W1_GENERATION, end->w1);
4520c575703SCédric Le Goater         kvm_eq.qindex  = xive_get_field32(END_W1_PAGE_OFF, end->w1);
4530c575703SCédric Le Goater     } else {
4540c575703SCédric Le Goater         kvm_eq.qshift = 0;
4550c575703SCédric Le Goater         kvm_eq.qaddr  = 0;
4560c575703SCédric Le Goater     }
4570c575703SCédric Le Goater 
4580c575703SCédric Le Goater     /* Encode the tuple (server, prio) as a KVM EQ index */
4590c575703SCédric Le Goater     spapr_xive_end_to_target(end_blk, end_idx, &server, &priority);
4600c575703SCédric Le Goater 
4610c575703SCédric Le Goater     kvm_eq_idx = priority << KVM_XIVE_EQ_PRIORITY_SHIFT &
4620c575703SCédric Le Goater             KVM_XIVE_EQ_PRIORITY_MASK;
4630c575703SCédric Le Goater     kvm_eq_idx |= server << KVM_XIVE_EQ_SERVER_SHIFT &
4640c575703SCédric Le Goater         KVM_XIVE_EQ_SERVER_MASK;
4650c575703SCédric Le Goater 
4660c575703SCédric Le Goater     kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_EQ_CONFIG, kvm_eq_idx,
4670c575703SCédric Le Goater                       &kvm_eq, true, &local_err);
4680c575703SCédric Le Goater     if (local_err) {
4690c575703SCédric Le Goater         error_propagate(errp, local_err);
4700c575703SCédric Le Goater         return;
4710c575703SCédric Le Goater     }
4720c575703SCédric Le Goater }
4730c575703SCédric Le Goater 
4740c575703SCédric Le Goater void kvmppc_xive_reset(SpaprXive *xive, Error **errp)
4750c575703SCédric Le Goater {
4760c575703SCédric Le Goater     kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_CTRL, KVM_DEV_XIVE_RESET,
4770c575703SCédric Le Goater                       NULL, true, errp);
4780c575703SCédric Le Goater }
47938afd772SCédric Le Goater 
4807bfc759cSCédric Le Goater static void kvmppc_xive_get_queues(SpaprXive *xive, Error **errp)
4817bfc759cSCédric Le Goater {
4827bfc759cSCédric Le Goater     Error *local_err = NULL;
4837bfc759cSCédric Le Goater     int i;
4847bfc759cSCédric Le Goater 
4857bfc759cSCédric Le Goater     for (i = 0; i < xive->nr_ends; i++) {
4867bfc759cSCédric Le Goater         if (!xive_end_is_valid(&xive->endt[i])) {
4877bfc759cSCédric Le Goater             continue;
4887bfc759cSCédric Le Goater         }
4897bfc759cSCédric Le Goater 
4907bfc759cSCédric Le Goater         kvmppc_xive_get_queue_config(xive, SPAPR_XIVE_BLOCK_ID, i,
4917bfc759cSCédric Le Goater                                      &xive->endt[i], &local_err);
4927bfc759cSCédric Le Goater         if (local_err) {
4937bfc759cSCédric Le Goater             error_propagate(errp, local_err);
4947bfc759cSCédric Le Goater             return;
4957bfc759cSCédric Le Goater         }
4967bfc759cSCédric Le Goater     }
4977bfc759cSCédric Le Goater }
4987bfc759cSCédric Le Goater 
4999b88cd76SCédric Le Goater /*
5009b88cd76SCédric Le Goater  * The primary goal of the XIVE VM change handler is to mark the EQ
5019b88cd76SCédric Le Goater  * pages dirty when all XIVE event notifications have stopped.
5029b88cd76SCédric Le Goater  *
5039b88cd76SCédric Le Goater  * Whenever the VM is stopped, the VM change handler sets the source
5049b88cd76SCédric Le Goater  * PQs to PENDING to stop the flow of events and to possibly catch a
5059b88cd76SCédric Le Goater  * triggered interrupt occuring while the VM is stopped. The previous
5069b88cd76SCédric Le Goater  * state is saved in anticipation of a migration. The XIVE controller
5079b88cd76SCédric Le Goater  * is then synced through KVM to flush any in-flight event
5089b88cd76SCédric Le Goater  * notification and stabilize the EQs.
5099b88cd76SCédric Le Goater  *
5109b88cd76SCédric Le Goater  * At this stage, we can mark the EQ page dirty and let a migration
5119b88cd76SCédric Le Goater  * sequence transfer the EQ pages to the destination, which is done
5129b88cd76SCédric Le Goater  * just after the stop state.
5139b88cd76SCédric Le Goater  *
5149b88cd76SCédric Le Goater  * The previous configuration of the sources is restored when the VM
5159b88cd76SCédric Le Goater  * runs again. If an interrupt was queued while the VM was stopped,
5169b88cd76SCédric Le Goater  * simply generate a trigger.
5179b88cd76SCédric Le Goater  */
5189b88cd76SCédric Le Goater static void kvmppc_xive_change_state_handler(void *opaque, int running,
5199b88cd76SCédric Le Goater                                              RunState state)
5209b88cd76SCédric Le Goater {
5219b88cd76SCédric Le Goater     SpaprXive *xive = opaque;
5229b88cd76SCédric Le Goater     XiveSource *xsrc = &xive->source;
5239b88cd76SCédric Le Goater     Error *local_err = NULL;
5249b88cd76SCédric Le Goater     int i;
5259b88cd76SCédric Le Goater 
5269b88cd76SCédric Le Goater     /*
5279b88cd76SCédric Le Goater      * Restore the sources to their initial state. This is called when
5289b88cd76SCédric Le Goater      * the VM resumes after a stop or a migration.
5299b88cd76SCédric Le Goater      */
5309b88cd76SCédric Le Goater     if (running) {
5319b88cd76SCédric Le Goater         for (i = 0; i < xsrc->nr_irqs; i++) {
5324c3539d4SCédric Le Goater             uint8_t pq;
5339b88cd76SCédric Le Goater             uint8_t old_pq;
5349b88cd76SCédric Le Goater 
5354c3539d4SCédric Le Goater             if (!xive_eas_is_valid(&xive->eat[i])) {
5364c3539d4SCédric Le Goater                 continue;
5374c3539d4SCédric Le Goater             }
5384c3539d4SCédric Le Goater 
5394c3539d4SCédric Le Goater             pq = xive_source_esb_get(xsrc, i);
5409b88cd76SCédric Le Goater             old_pq = xive_esb_read(xsrc, i, XIVE_ESB_SET_PQ_00 + (pq << 8));
5419b88cd76SCédric Le Goater 
5429b88cd76SCédric Le Goater             /*
5439b88cd76SCédric Le Goater              * An interrupt was queued while the VM was stopped,
5449b88cd76SCédric Le Goater              * generate a trigger.
5459b88cd76SCédric Le Goater              */
5469b88cd76SCédric Le Goater             if (pq == XIVE_ESB_RESET && old_pq == XIVE_ESB_QUEUED) {
5479b88cd76SCédric Le Goater                 xive_esb_trigger(xsrc, i);
5489b88cd76SCédric Le Goater             }
5499b88cd76SCédric Le Goater         }
5509b88cd76SCédric Le Goater 
5519b88cd76SCédric Le Goater         return;
5529b88cd76SCédric Le Goater     }
5539b88cd76SCédric Le Goater 
5549b88cd76SCédric Le Goater     /*
5559b88cd76SCédric Le Goater      * Mask the sources, to stop the flow of event notifications, and
5569b88cd76SCédric Le Goater      * save the PQs locally in the XiveSource object. The XiveSource
5579b88cd76SCédric Le Goater      * state will be collected later on by its vmstate handler if a
5589b88cd76SCédric Le Goater      * migration is in progress.
5599b88cd76SCédric Le Goater      */
5609b88cd76SCédric Le Goater     for (i = 0; i < xsrc->nr_irqs; i++) {
5614c3539d4SCédric Le Goater         uint8_t pq;
5624c3539d4SCédric Le Goater 
5634c3539d4SCédric Le Goater         if (!xive_eas_is_valid(&xive->eat[i])) {
5644c3539d4SCédric Le Goater             continue;
5654c3539d4SCédric Le Goater         }
5664c3539d4SCédric Le Goater 
5674c3539d4SCédric Le Goater         pq = xive_esb_read(xsrc, i, XIVE_ESB_GET);
5689b88cd76SCédric Le Goater 
5699b88cd76SCédric Le Goater         /*
5709b88cd76SCédric Le Goater          * PQ is set to PENDING to possibly catch a triggered
5719b88cd76SCédric Le Goater          * interrupt occuring while the VM is stopped (hotplug event
5729b88cd76SCédric Le Goater          * for instance) .
5739b88cd76SCédric Le Goater          */
5749b88cd76SCédric Le Goater         if (pq != XIVE_ESB_OFF) {
5759b88cd76SCédric Le Goater             pq = xive_esb_read(xsrc, i, XIVE_ESB_SET_PQ_10);
5769b88cd76SCédric Le Goater         }
5779b88cd76SCédric Le Goater         xive_source_esb_set(xsrc, i, pq);
5789b88cd76SCédric Le Goater     }
5799b88cd76SCédric Le Goater 
5809b88cd76SCédric Le Goater     /*
5819b88cd76SCédric Le Goater      * Sync the XIVE controller in KVM, to flush in-flight event
5829b88cd76SCédric Le Goater      * notification that should be enqueued in the EQs and mark the
5839b88cd76SCédric Le Goater      * XIVE EQ pages dirty to collect all updates.
5849b88cd76SCédric Le Goater      */
5859b88cd76SCédric Le Goater     kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_CTRL,
5869b88cd76SCédric Le Goater                       KVM_DEV_XIVE_EQ_SYNC, NULL, true, &local_err);
5879b88cd76SCédric Le Goater     if (local_err) {
5889b88cd76SCédric Le Goater         error_report_err(local_err);
5899b88cd76SCédric Le Goater         return;
5909b88cd76SCédric Le Goater     }
5919b88cd76SCédric Le Goater }
5929b88cd76SCédric Le Goater 
5937bfc759cSCédric Le Goater void kvmppc_xive_synchronize_state(SpaprXive *xive, Error **errp)
5947bfc759cSCédric Le Goater {
5953bf84e99SCédric Le Goater     /* The KVM XIVE device is not in use */
5963bf84e99SCédric Le Goater     if (xive->fd == -1) {
5973bf84e99SCédric Le Goater         return;
5983bf84e99SCédric Le Goater     }
5993bf84e99SCédric Le Goater 
6009b88cd76SCédric Le Goater     /*
6019b88cd76SCédric Le Goater      * When the VM is stopped, the sources are masked and the previous
6029b88cd76SCédric Le Goater      * state is saved in anticipation of a migration. We should not
6039b88cd76SCédric Le Goater      * synchronize the source state in that case else we will override
6049b88cd76SCédric Le Goater      * the saved state.
6059b88cd76SCédric Le Goater      */
6069b88cd76SCédric Le Goater     if (runstate_is_running()) {
6077bfc759cSCédric Le Goater         kvmppc_xive_source_get_state(&xive->source);
6089b88cd76SCédric Le Goater     }
6097bfc759cSCédric Le Goater 
6107bfc759cSCédric Le Goater     /* EAT: there is no extra state to query from KVM */
6117bfc759cSCédric Le Goater 
6127bfc759cSCédric Le Goater     /* ENDT */
6137bfc759cSCédric Le Goater     kvmppc_xive_get_queues(xive, errp);
6147bfc759cSCédric Le Goater }
6157bfc759cSCédric Le Goater 
616277dd3d7SCédric Le Goater /*
617277dd3d7SCédric Le Goater  * The SpaprXive 'pre_save' method is called by the vmstate handler of
618277dd3d7SCédric Le Goater  * the SpaprXive model, after the XIVE controller is synced in the VM
619277dd3d7SCédric Le Goater  * change handler.
620277dd3d7SCédric Le Goater  */
621277dd3d7SCédric Le Goater int kvmppc_xive_pre_save(SpaprXive *xive)
622277dd3d7SCédric Le Goater {
623277dd3d7SCédric Le Goater     Error *local_err = NULL;
624277dd3d7SCédric Le Goater 
6253bf84e99SCédric Le Goater     /* The KVM XIVE device is not in use */
6263bf84e99SCédric Le Goater     if (xive->fd == -1) {
6273bf84e99SCédric Le Goater         return 0;
6283bf84e99SCédric Le Goater     }
6293bf84e99SCédric Le Goater 
630277dd3d7SCédric Le Goater     /* EAT: there is no extra state to query from KVM */
631277dd3d7SCédric Le Goater 
632277dd3d7SCédric Le Goater     /* ENDT */
633277dd3d7SCédric Le Goater     kvmppc_xive_get_queues(xive, &local_err);
634277dd3d7SCédric Le Goater     if (local_err) {
635277dd3d7SCédric Le Goater         error_report_err(local_err);
636277dd3d7SCédric Le Goater         return -1;
637277dd3d7SCédric Le Goater     }
638277dd3d7SCédric Le Goater 
639277dd3d7SCédric Le Goater     return 0;
640277dd3d7SCédric Le Goater }
641277dd3d7SCédric Le Goater 
642277dd3d7SCédric Le Goater /*
643277dd3d7SCédric Le Goater  * The SpaprXive 'post_load' method is not called by a vmstate
644277dd3d7SCédric Le Goater  * handler. It is called at the sPAPR machine level at the end of the
645277dd3d7SCédric Le Goater  * migration sequence by the sPAPR IRQ backend 'post_load' method,
646277dd3d7SCédric Le Goater  * when all XIVE states have been transferred and loaded.
647277dd3d7SCédric Le Goater  */
648277dd3d7SCédric Le Goater int kvmppc_xive_post_load(SpaprXive *xive, int version_id)
649277dd3d7SCédric Le Goater {
650277dd3d7SCédric Le Goater     Error *local_err = NULL;
651277dd3d7SCédric Le Goater     CPUState *cs;
652277dd3d7SCédric Le Goater     int i;
653277dd3d7SCédric Le Goater 
6543bf84e99SCédric Le Goater     /* The KVM XIVE device should be in use */
6553bf84e99SCédric Le Goater     assert(xive->fd != -1);
6563bf84e99SCédric Le Goater 
657277dd3d7SCédric Le Goater     /* Restore the ENDT first. The targetting depends on it. */
658277dd3d7SCédric Le Goater     for (i = 0; i < xive->nr_ends; i++) {
659277dd3d7SCédric Le Goater         if (!xive_end_is_valid(&xive->endt[i])) {
660277dd3d7SCédric Le Goater             continue;
661277dd3d7SCédric Le Goater         }
662277dd3d7SCédric Le Goater 
663277dd3d7SCédric Le Goater         kvmppc_xive_set_queue_config(xive, SPAPR_XIVE_BLOCK_ID, i,
664277dd3d7SCédric Le Goater                                      &xive->endt[i], &local_err);
665277dd3d7SCédric Le Goater         if (local_err) {
666277dd3d7SCédric Le Goater             error_report_err(local_err);
667277dd3d7SCédric Le Goater             return -1;
668277dd3d7SCédric Le Goater         }
669277dd3d7SCédric Le Goater     }
670277dd3d7SCédric Le Goater 
671277dd3d7SCédric Le Goater     /* Restore the EAT */
672277dd3d7SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
673277dd3d7SCédric Le Goater         if (!xive_eas_is_valid(&xive->eat[i])) {
674277dd3d7SCédric Le Goater             continue;
675277dd3d7SCédric Le Goater         }
676277dd3d7SCédric Le Goater 
6774c3539d4SCédric Le Goater         /*
6784c3539d4SCédric Le Goater          * We can only restore the source config if the source has been
6794c3539d4SCédric Le Goater          * previously set in KVM. Since we don't do that for all interrupts
6804c3539d4SCédric Le Goater          * at reset time anymore, let's do it now.
6814c3539d4SCédric Le Goater          */
6824c3539d4SCédric Le Goater         kvmppc_xive_source_reset_one(&xive->source, i, &local_err);
6834c3539d4SCédric Le Goater         if (local_err) {
6844c3539d4SCédric Le Goater             error_report_err(local_err);
6854c3539d4SCédric Le Goater             return -1;
6864c3539d4SCédric Le Goater         }
6874c3539d4SCédric Le Goater 
688277dd3d7SCédric Le Goater         kvmppc_xive_set_source_config(xive, i, &xive->eat[i], &local_err);
689277dd3d7SCédric Le Goater         if (local_err) {
690277dd3d7SCédric Le Goater             error_report_err(local_err);
691277dd3d7SCédric Le Goater             return -1;
692277dd3d7SCédric Le Goater         }
693277dd3d7SCédric Le Goater     }
694277dd3d7SCédric Le Goater 
695310cda5bSCédric Le Goater     /*
696310cda5bSCédric Le Goater      * Restore the thread interrupt contexts of initial CPUs.
697310cda5bSCédric Le Goater      *
698310cda5bSCédric Le Goater      * The context of hotplugged CPUs is restored later, by the
699310cda5bSCédric Le Goater      * 'post_load' handler of the XiveTCTX model because they are not
700310cda5bSCédric Le Goater      * available at the time the SpaprXive 'post_load' method is
701310cda5bSCédric Le Goater      * called. We can not restore the context of all CPUs in the
702310cda5bSCédric Le Goater      * 'post_load' handler of XiveTCTX because the machine is not
703310cda5bSCédric Le Goater      * necessarily connected to the KVM device at that time.
704310cda5bSCédric Le Goater      */
705277dd3d7SCédric Le Goater     CPU_FOREACH(cs) {
706277dd3d7SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
707277dd3d7SCédric Le Goater 
708277dd3d7SCédric Le Goater         kvmppc_xive_cpu_set_state(spapr_cpu_state(cpu)->tctx, &local_err);
709277dd3d7SCédric Le Goater         if (local_err) {
710277dd3d7SCédric Le Goater             error_report_err(local_err);
711277dd3d7SCédric Le Goater             return -1;
712277dd3d7SCédric Le Goater         }
713277dd3d7SCédric Le Goater     }
714277dd3d7SCédric Le Goater 
715277dd3d7SCédric Le Goater     /* The source states will be restored when the machine starts running */
716277dd3d7SCédric Le Goater     return 0;
717277dd3d7SCédric Le Goater }
718277dd3d7SCédric Le Goater 
71938afd772SCédric Le Goater static void *kvmppc_xive_mmap(SpaprXive *xive, int pgoff, size_t len,
72038afd772SCédric Le Goater                               Error **errp)
72138afd772SCédric Le Goater {
72238afd772SCédric Le Goater     void *addr;
72338afd772SCédric Le Goater     uint32_t page_shift = 16; /* TODO: fix page_shift */
72438afd772SCédric Le Goater 
72538afd772SCédric Le Goater     addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, xive->fd,
72638afd772SCédric Le Goater                 pgoff << page_shift);
72738afd772SCédric Le Goater     if (addr == MAP_FAILED) {
72838afd772SCédric Le Goater         error_setg_errno(errp, errno, "XIVE: unable to set memory mapping");
72938afd772SCédric Le Goater         return NULL;
73038afd772SCédric Le Goater     }
73138afd772SCédric Le Goater 
73238afd772SCédric Le Goater     return addr;
73338afd772SCédric Le Goater }
73438afd772SCédric Le Goater 
73538afd772SCédric Le Goater /*
73638afd772SCédric Le Goater  * All the XIVE memory regions are now backed by mappings from the KVM
73738afd772SCédric Le Goater  * XIVE device.
73838afd772SCédric Le Goater  */
7394ffb7496SGreg Kurz int kvmppc_xive_connect(SpaprInterruptController *intc, uint32_t nr_servers,
7404ffb7496SGreg Kurz                         Error **errp)
74138afd772SCédric Le Goater {
74298a39a79SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
74338afd772SCédric Le Goater     XiveSource *xsrc = &xive->source;
74438afd772SCédric Le Goater     Error *local_err = NULL;
74538afd772SCédric Le Goater     size_t esb_len = (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
74638afd772SCédric Le Goater     size_t tima_len = 4ull << TM_SHIFT;
7473f777abcSCédric Le Goater     CPUState *cs;
7483f777abcSCédric Le Goater 
7493f777abcSCédric Le Goater     /*
7503f777abcSCédric Le Goater      * The KVM XIVE device already in use. This is the case when
7513f777abcSCédric Le Goater      * rebooting under the XIVE-only interrupt mode.
7523f777abcSCédric Le Goater      */
7533f777abcSCédric Le Goater     if (xive->fd != -1) {
75498a39a79SDavid Gibson         return 0;
7553f777abcSCédric Le Goater     }
75638afd772SCédric Le Goater 
75738afd772SCédric Le Goater     if (!kvmppc_has_cap_xive()) {
75838afd772SCédric Le Goater         error_setg(errp, "IRQ_XIVE capability must be present for KVM");
75998a39a79SDavid Gibson         return -1;
76038afd772SCédric Le Goater     }
76138afd772SCédric Le Goater 
76238afd772SCédric Le Goater     /* First, create the KVM XIVE device */
76338afd772SCédric Le Goater     xive->fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_XIVE, false);
76438afd772SCédric Le Goater     if (xive->fd < 0) {
76538afd772SCédric Le Goater         error_setg_errno(errp, -xive->fd, "XIVE: error creating KVM device");
76698a39a79SDavid Gibson         return -1;
76738afd772SCédric Le Goater     }
76838afd772SCédric Le Goater 
76974f23d43SGreg Kurz     /* Tell KVM about the # of VCPUs we may have */
77074f23d43SGreg Kurz     if (kvm_device_check_attr(xive->fd, KVM_DEV_XIVE_GRP_CTRL,
77174f23d43SGreg Kurz                               KVM_DEV_XIVE_NR_SERVERS)) {
77274f23d43SGreg Kurz         if (kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_CTRL,
77374f23d43SGreg Kurz                               KVM_DEV_XIVE_NR_SERVERS, &nr_servers, true,
77474f23d43SGreg Kurz                               &local_err)) {
77574f23d43SGreg Kurz             goto fail;
77674f23d43SGreg Kurz         }
77774f23d43SGreg Kurz     }
77874f23d43SGreg Kurz 
77938afd772SCédric Le Goater     /*
78038afd772SCédric Le Goater      * 1. Source ESB pages - KVM mapping
78138afd772SCédric Le Goater      */
78238afd772SCédric Le Goater     xsrc->esb_mmap = kvmppc_xive_mmap(xive, KVM_XIVE_ESB_PAGE_OFFSET, esb_len,
78338afd772SCédric Le Goater                                       &local_err);
78438afd772SCédric Le Goater     if (local_err) {
7851c3d4a8fSGreg Kurz         goto fail;
78638afd772SCédric Le Goater     }
78738afd772SCédric Le Goater 
788981b1c62SCédric Le Goater     memory_region_init_ram_device_ptr(&xsrc->esb_mmio_kvm, OBJECT(xsrc),
78938afd772SCédric Le Goater                                       "xive.esb", esb_len, xsrc->esb_mmap);
790981b1c62SCédric Le Goater     memory_region_add_subregion_overlap(&xsrc->esb_mmio, 0,
791981b1c62SCédric Le Goater                                         &xsrc->esb_mmio_kvm, 1);
79238afd772SCédric Le Goater 
79338afd772SCédric Le Goater     /*
79438afd772SCédric Le Goater      * 2. END ESB pages (No KVM support yet)
79538afd772SCédric Le Goater      */
79638afd772SCédric Le Goater 
79738afd772SCédric Le Goater     /*
79838afd772SCédric Le Goater      * 3. TIMA pages - KVM mapping
79938afd772SCédric Le Goater      */
80038afd772SCédric Le Goater     xive->tm_mmap = kvmppc_xive_mmap(xive, KVM_XIVE_TIMA_PAGE_OFFSET, tima_len,
80138afd772SCédric Le Goater                                      &local_err);
80238afd772SCédric Le Goater     if (local_err) {
8031c3d4a8fSGreg Kurz         goto fail;
80438afd772SCédric Le Goater     }
805981b1c62SCédric Le Goater     memory_region_init_ram_device_ptr(&xive->tm_mmio_kvm, OBJECT(xive),
80638afd772SCédric Le Goater                                       "xive.tima", tima_len, xive->tm_mmap);
807981b1c62SCédric Le Goater     memory_region_add_subregion_overlap(&xive->tm_mmio, 0,
808981b1c62SCédric Le Goater                                         &xive->tm_mmio_kvm, 1);
80938afd772SCédric Le Goater 
8109b88cd76SCédric Le Goater     xive->change = qemu_add_vm_change_state_handler(
8119b88cd76SCédric Le Goater         kvmppc_xive_change_state_handler, xive);
8129b88cd76SCédric Le Goater 
8133f777abcSCédric Le Goater     /* Connect the presenters to the initial VCPUs of the machine */
8143f777abcSCédric Le Goater     CPU_FOREACH(cs) {
8153f777abcSCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
8163f777abcSCédric Le Goater 
8173f777abcSCédric Le Goater         kvmppc_xive_cpu_connect(spapr_cpu_state(cpu)->tctx, &local_err);
8183f777abcSCédric Le Goater         if (local_err) {
8191c3d4a8fSGreg Kurz             goto fail;
8203f777abcSCédric Le Goater         }
8213f777abcSCédric Le Goater     }
8223f777abcSCédric Le Goater 
8233f777abcSCédric Le Goater     /* Update the KVM sources */
8243f777abcSCédric Le Goater     kvmppc_xive_source_reset(xsrc, &local_err);
8253f777abcSCédric Le Goater     if (local_err) {
8261c3d4a8fSGreg Kurz         goto fail;
8273f777abcSCédric Le Goater     }
8283f777abcSCédric Le Goater 
82938afd772SCédric Le Goater     kvm_kernel_irqchip = true;
83038afd772SCédric Le Goater     kvm_msi_via_irqfd_allowed = true;
83138afd772SCédric Le Goater     kvm_gsi_direct_mapping = true;
83298a39a79SDavid Gibson     return 0;
8331c3d4a8fSGreg Kurz 
8341c3d4a8fSGreg Kurz fail:
8351c3d4a8fSGreg Kurz     error_propagate(errp, local_err);
83698a39a79SDavid Gibson     kvmppc_xive_disconnect(intc);
83798a39a79SDavid Gibson     return -1;
83838afd772SCédric Le Goater }
83956b11587SCédric Le Goater 
84098a39a79SDavid Gibson void kvmppc_xive_disconnect(SpaprInterruptController *intc)
84156b11587SCédric Le Goater {
84298a39a79SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
84356b11587SCédric Le Goater     XiveSource *xsrc;
84456b11587SCédric Le Goater     size_t esb_len;
84556b11587SCédric Le Goater 
84656b11587SCédric Le Goater     /* The KVM XIVE device is not in use */
84756b11587SCédric Le Goater     if (!xive || xive->fd == -1) {
84856b11587SCédric Le Goater         return;
84956b11587SCédric Le Goater     }
85056b11587SCédric Le Goater 
85156b11587SCédric Le Goater     /* Clear the KVM mapping */
85256b11587SCédric Le Goater     xsrc = &xive->source;
85356b11587SCédric Le Goater     esb_len = (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
85456b11587SCédric Le Goater 
8551c3d4a8fSGreg Kurz     if (xsrc->esb_mmap) {
856981b1c62SCédric Le Goater         memory_region_del_subregion(&xsrc->esb_mmio, &xsrc->esb_mmio_kvm);
857981b1c62SCédric Le Goater         object_unparent(OBJECT(&xsrc->esb_mmio_kvm));
85856b11587SCédric Le Goater         munmap(xsrc->esb_mmap, esb_len);
859981b1c62SCédric Le Goater         xsrc->esb_mmap = NULL;
8601c3d4a8fSGreg Kurz     }
86156b11587SCédric Le Goater 
8621c3d4a8fSGreg Kurz     if (xive->tm_mmap) {
863981b1c62SCédric Le Goater         memory_region_del_subregion(&xive->tm_mmio, &xive->tm_mmio_kvm);
864981b1c62SCédric Le Goater         object_unparent(OBJECT(&xive->tm_mmio_kvm));
86556b11587SCédric Le Goater         munmap(xive->tm_mmap, 4ull << TM_SHIFT);
866981b1c62SCédric Le Goater         xive->tm_mmap = NULL;
8671c3d4a8fSGreg Kurz     }
86856b11587SCédric Le Goater 
86956b11587SCédric Le Goater     /*
87056b11587SCédric Le Goater      * When the KVM device fd is closed, the KVM device is destroyed
87156b11587SCédric Le Goater      * and removed from the list of devices of the VM. The VCPU
87256b11587SCédric Le Goater      * presenters are also detached from the device.
87356b11587SCédric Le Goater      */
8741c3d4a8fSGreg Kurz     if (xive->fd != -1) {
87556b11587SCédric Le Goater         close(xive->fd);
87656b11587SCédric Le Goater         xive->fd = -1;
8771c3d4a8fSGreg Kurz     }
87856b11587SCédric Le Goater 
87956b11587SCédric Le Goater     kvm_kernel_irqchip = false;
88056b11587SCédric Le Goater     kvm_msi_via_irqfd_allowed = false;
88156b11587SCédric Le Goater     kvm_gsi_direct_mapping = false;
88256b11587SCédric Le Goater 
88356b11587SCédric Le Goater     /* Clear the local list of presenter (hotplug) */
88456b11587SCédric Le Goater     kvm_cpu_disable_all();
88556b11587SCédric Le Goater 
88656b11587SCédric Le Goater     /* VM Change state handler is not needed anymore */
8871c3d4a8fSGreg Kurz     if (xive->change) {
88856b11587SCédric Le Goater         qemu_del_vm_change_state_handler(xive->change);
8891c3d4a8fSGreg Kurz         xive->change = NULL;
8901c3d4a8fSGreg Kurz     }
89156b11587SCédric Le Goater }
892