xref: /qemu/hw/intc/spapr_xive_kvm.c (revision 65cb7129f4160c7e07a0da107f888ec73ae96776)
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"
15*32cad1ffSPhilippe Mathieu-Daudé #include "system/cpus.h"
16*32cad1ffSPhilippe Mathieu-Daudé #include "system/kvm.h"
17*32cad1ffSPhilippe Mathieu-Daudé #include "system/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"
234e960974SCédric Le Goater #include "trace.h"
2438afd772SCédric Le Goater 
2538afd772SCédric Le Goater #include <sys/ioctl.h>
2638afd772SCédric Le Goater 
2738afd772SCédric Le Goater /*
2838afd772SCédric Le Goater  * Helpers for CPU hotplug
2938afd772SCédric Le Goater  *
3038afd772SCédric Le Goater  * TODO: make a common KVMEnabledCPU layer for XICS and XIVE
3138afd772SCédric Le Goater  */
3238afd772SCédric Le Goater typedef struct KVMEnabledCPU {
3338afd772SCédric Le Goater     unsigned long vcpu_id;
3438afd772SCédric Le Goater     QLIST_ENTRY(KVMEnabledCPU) node;
3538afd772SCédric Le Goater } KVMEnabledCPU;
3638afd772SCédric Le Goater 
3738afd772SCédric Le Goater static QLIST_HEAD(, KVMEnabledCPU)
3838afd772SCédric Le Goater     kvm_enabled_cpus = QLIST_HEAD_INITIALIZER(&kvm_enabled_cpus);
3938afd772SCédric Le Goater 
kvm_cpu_is_enabled(CPUState * cs)406d24795eSGreg Kurz static bool kvm_cpu_is_enabled(CPUState *cs)
4138afd772SCédric Le Goater {
4238afd772SCédric Le Goater     KVMEnabledCPU *enabled_cpu;
436d24795eSGreg Kurz     unsigned long vcpu_id = kvm_arch_vcpu_id(cs);
4438afd772SCédric Le Goater 
4538afd772SCédric Le Goater     QLIST_FOREACH(enabled_cpu, &kvm_enabled_cpus, node) {
4638afd772SCédric Le Goater         if (enabled_cpu->vcpu_id == vcpu_id) {
4738afd772SCédric Le Goater             return true;
4838afd772SCédric Le Goater         }
4938afd772SCédric Le Goater     }
5038afd772SCédric Le Goater     return false;
5138afd772SCédric Le Goater }
5238afd772SCédric Le Goater 
kvm_cpu_enable(CPUState * cs)5338afd772SCédric Le Goater static void kvm_cpu_enable(CPUState *cs)
5438afd772SCédric Le Goater {
5538afd772SCédric Le Goater     KVMEnabledCPU *enabled_cpu;
5638afd772SCédric Le Goater     unsigned long vcpu_id = kvm_arch_vcpu_id(cs);
5738afd772SCédric Le Goater 
5838afd772SCédric Le Goater     enabled_cpu = g_malloc(sizeof(*enabled_cpu));
5938afd772SCédric Le Goater     enabled_cpu->vcpu_id = vcpu_id;
6038afd772SCédric Le Goater     QLIST_INSERT_HEAD(&kvm_enabled_cpus, enabled_cpu, node);
6138afd772SCédric Le Goater }
6238afd772SCédric Le Goater 
kvm_cpu_disable_all(void)6356b11587SCédric Le Goater static void kvm_cpu_disable_all(void)
6456b11587SCédric Le Goater {
6556b11587SCédric Le Goater     KVMEnabledCPU *enabled_cpu, *next;
6656b11587SCédric Le Goater 
6756b11587SCédric Le Goater     QLIST_FOREACH_SAFE(enabled_cpu, &kvm_enabled_cpus, node, next) {
6856b11587SCédric Le Goater         QLIST_REMOVE(enabled_cpu, node);
6956b11587SCédric Le Goater         g_free(enabled_cpu);
7056b11587SCédric Le Goater     }
7156b11587SCédric Le Goater }
7256b11587SCédric Le Goater 
7338afd772SCédric Le Goater /*
7438afd772SCédric Le Goater  * XIVE Thread Interrupt Management context (KVM)
7538afd772SCédric Le Goater  */
76277dd3d7SCédric Le Goater 
kvmppc_xive_cpu_set_state(XiveTCTX * tctx,Error ** errp)775fa36b7fSGreg Kurz int kvmppc_xive_cpu_set_state(XiveTCTX *tctx, Error **errp)
78277dd3d7SCédric Le Goater {
7974e51a38SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(tctx->xptr);
80277dd3d7SCédric Le Goater     uint64_t state[2];
81277dd3d7SCédric Le Goater     int ret;
82277dd3d7SCédric Le Goater 
83a4907119SGreg Kurz     assert(xive->fd != -1);
84310cda5bSCédric Le Goater 
85277dd3d7SCédric Le Goater     /* word0 and word1 of the OS ring. */
86277dd3d7SCédric Le Goater     state[0] = *((uint64_t *) &tctx->regs[TM_QW1_OS]);
87277dd3d7SCédric Le Goater 
88277dd3d7SCédric Le Goater     ret = kvm_set_one_reg(tctx->cs, KVM_REG_PPC_VP_STATE, state);
89277dd3d7SCédric Le Goater     if (ret != 0) {
905fa36b7fSGreg Kurz         error_setg_errno(errp, -ret,
91277dd3d7SCédric Le Goater                          "XIVE: could not restore KVM state of CPU %ld",
92277dd3d7SCédric Le Goater                          kvm_arch_vcpu_id(tctx->cs));
935fa36b7fSGreg Kurz         return ret;
94277dd3d7SCédric Le Goater     }
95277dd3d7SCédric Le Goater 
965fa36b7fSGreg Kurz     return 0;
975fa36b7fSGreg Kurz }
985fa36b7fSGreg Kurz 
kvmppc_xive_cpu_get_state(XiveTCTX * tctx,Error ** errp)995fa36b7fSGreg Kurz int kvmppc_xive_cpu_get_state(XiveTCTX *tctx, Error **errp)
1007bfc759cSCédric Le Goater {
10174e51a38SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(tctx->xptr);
1027bfc759cSCédric Le Goater     uint64_t state[2] = { 0 };
1037bfc759cSCédric Le Goater     int ret;
1047bfc759cSCédric Le Goater 
105a4907119SGreg Kurz     assert(xive->fd != -1);
1063bf84e99SCédric Le Goater 
1077bfc759cSCédric Le Goater     ret = kvm_get_one_reg(tctx->cs, KVM_REG_PPC_VP_STATE, state);
1087bfc759cSCédric Le Goater     if (ret != 0) {
1095fa36b7fSGreg Kurz         error_setg_errno(errp, -ret,
1107bfc759cSCédric Le Goater                          "XIVE: could not capture KVM state of CPU %ld",
1117bfc759cSCédric Le Goater                          kvm_arch_vcpu_id(tctx->cs));
1125fa36b7fSGreg Kurz         return ret;
1137bfc759cSCédric Le Goater     }
1147bfc759cSCédric Le Goater 
1157bfc759cSCédric Le Goater     /* word0 and word1 of the OS ring. */
1167bfc759cSCédric Le Goater     *((uint64_t *) &tctx->regs[TM_QW1_OS]) = state[0];
1175fa36b7fSGreg Kurz 
1185fa36b7fSGreg Kurz     return 0;
1197bfc759cSCédric Le Goater }
1207bfc759cSCédric Le Goater 
1217bfc759cSCédric Le Goater typedef struct {
1227bfc759cSCédric Le Goater     XiveTCTX *tctx;
1231118b6b7SGreg Kurz     Error **errp;
1241118b6b7SGreg Kurz     int ret;
1257bfc759cSCédric Le Goater } XiveCpuGetState;
1267bfc759cSCédric Le Goater 
kvmppc_xive_cpu_do_synchronize_state(CPUState * cpu,run_on_cpu_data arg)1277bfc759cSCédric Le Goater static void kvmppc_xive_cpu_do_synchronize_state(CPUState *cpu,
1287bfc759cSCédric Le Goater                                                  run_on_cpu_data arg)
1297bfc759cSCédric Le Goater {
1307bfc759cSCédric Le Goater     XiveCpuGetState *s = arg.host_ptr;
1317bfc759cSCédric Le Goater 
1321118b6b7SGreg Kurz     s->ret = kvmppc_xive_cpu_get_state(s->tctx, s->errp);
1337bfc759cSCédric Le Goater }
1347bfc759cSCédric Le Goater 
kvmppc_xive_cpu_synchronize_state(XiveTCTX * tctx,Error ** errp)1351118b6b7SGreg Kurz int kvmppc_xive_cpu_synchronize_state(XiveTCTX *tctx, Error **errp)
1367bfc759cSCédric Le Goater {
1377bfc759cSCédric Le Goater     XiveCpuGetState s = {
1387bfc759cSCédric Le Goater         .tctx = tctx,
1391118b6b7SGreg Kurz         .errp = errp,
1407bfc759cSCédric Le Goater     };
1417bfc759cSCédric Le Goater 
1427bfc759cSCédric Le Goater     /*
1437bfc759cSCédric Le Goater      * Kick the vCPU to make sure they are available for the KVM ioctl.
1447bfc759cSCédric Le Goater      */
1457bfc759cSCédric Le Goater     run_on_cpu(tctx->cs, kvmppc_xive_cpu_do_synchronize_state,
1467bfc759cSCédric Le Goater                RUN_ON_CPU_HOST_PTR(&s));
1477bfc759cSCédric Le Goater 
1481118b6b7SGreg Kurz     return s.ret;
1497bfc759cSCédric Le Goater }
15038afd772SCédric Le Goater 
kvmppc_xive_cpu_connect(XiveTCTX * tctx,Error ** errp)1513885ca66SGreg Kurz int kvmppc_xive_cpu_connect(XiveTCTX *tctx, Error **errp)
15238afd772SCédric Le Goater {
1533885ca66SGreg Kurz     ERRP_GUARD();
15474e51a38SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(tctx->xptr);
15538afd772SCédric Le Goater     unsigned long vcpu_id;
15638afd772SCédric Le Goater     int ret;
15738afd772SCédric Le Goater 
158a4907119SGreg Kurz     assert(xive->fd != -1);
1593bf84e99SCédric Le Goater 
16038afd772SCédric Le Goater     /* Check if CPU was hot unplugged and replugged. */
1616d24795eSGreg Kurz     if (kvm_cpu_is_enabled(tctx->cs)) {
1623885ca66SGreg Kurz         return 0;
16338afd772SCédric Le Goater     }
16438afd772SCédric Le Goater 
16538afd772SCédric Le Goater     vcpu_id = kvm_arch_vcpu_id(tctx->cs);
16638afd772SCédric Le Goater 
1674e960974SCédric Le Goater     trace_kvm_xive_cpu_connect(vcpu_id);
1684e960974SCédric Le Goater 
16938afd772SCédric Le Goater     ret = kvm_vcpu_enable_cap(tctx->cs, KVM_CAP_PPC_IRQ_XIVE, 0, xive->fd,
17038afd772SCédric Le Goater                               vcpu_id, 0);
17138afd772SCédric Le Goater     if (ret < 0) {
1723885ca66SGreg Kurz         error_setg_errno(errp, -ret,
1733885ca66SGreg Kurz                          "XIVE: unable to connect CPU%ld to KVM device",
1743885ca66SGreg Kurz                          vcpu_id);
1753885ca66SGreg Kurz         if (ret == -ENOSPC) {
1763885ca66SGreg Kurz             error_append_hint(errp, "Try -smp maxcpus=N with N < %u\n",
17774e51a38SGreg Kurz                               MACHINE(qdev_get_machine())->smp.max_cpus);
17874f23d43SGreg Kurz         }
1793885ca66SGreg Kurz         return ret;
18038afd772SCédric Le Goater     }
18138afd772SCédric Le Goater 
18238afd772SCédric Le Goater     kvm_cpu_enable(tctx->cs);
1833885ca66SGreg Kurz     return 0;
18438afd772SCédric Le Goater }
18538afd772SCédric Le Goater 
18638afd772SCédric Le Goater /*
18738afd772SCédric Le Goater  * XIVE Interrupt Source (KVM)
18838afd772SCédric Le Goater  */
18938afd772SCédric Le Goater 
kvmppc_xive_set_source_config(SpaprXive * xive,uint32_t lisn,XiveEAS * eas,Error ** errp)190d55daadcSGreg Kurz int kvmppc_xive_set_source_config(SpaprXive *xive, uint32_t lisn, XiveEAS *eas,
1910c575703SCédric Le Goater                                   Error **errp)
1920c575703SCédric Le Goater {
1930c575703SCédric Le Goater     uint32_t end_idx;
1940c575703SCédric Le Goater     uint32_t end_blk;
1950c575703SCédric Le Goater     uint8_t priority;
1960c575703SCédric Le Goater     uint32_t server;
1970c575703SCédric Le Goater     bool masked;
1980c575703SCédric Le Goater     uint32_t eisn;
1990c575703SCédric Le Goater     uint64_t kvm_src;
2000c575703SCédric Le Goater 
2010c575703SCédric Le Goater     assert(xive_eas_is_valid(eas));
2020c575703SCédric Le Goater 
2030c575703SCédric Le Goater     end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
2040c575703SCédric Le Goater     end_blk = xive_get_field64(EAS_END_BLOCK, eas->w);
2050c575703SCédric Le Goater     eisn = xive_get_field64(EAS_END_DATA, eas->w);
2060c575703SCédric Le Goater     masked = xive_eas_is_masked(eas);
2070c575703SCédric Le Goater 
2080c575703SCédric Le Goater     spapr_xive_end_to_target(end_blk, end_idx, &server, &priority);
2090c575703SCédric Le Goater 
2100c575703SCédric Le Goater     kvm_src = priority << KVM_XIVE_SOURCE_PRIORITY_SHIFT &
2110c575703SCédric Le Goater         KVM_XIVE_SOURCE_PRIORITY_MASK;
2120c575703SCédric Le Goater     kvm_src |= server << KVM_XIVE_SOURCE_SERVER_SHIFT &
2130c575703SCédric Le Goater         KVM_XIVE_SOURCE_SERVER_MASK;
2140c575703SCédric Le Goater     kvm_src |= ((uint64_t) masked << KVM_XIVE_SOURCE_MASKED_SHIFT) &
2150c575703SCédric Le Goater         KVM_XIVE_SOURCE_MASKED_MASK;
2160c575703SCédric Le Goater     kvm_src |= ((uint64_t)eisn << KVM_XIVE_SOURCE_EISN_SHIFT) &
2170c575703SCédric Le Goater         KVM_XIVE_SOURCE_EISN_MASK;
2180c575703SCédric Le Goater 
219d55daadcSGreg Kurz     return kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_SOURCE_CONFIG, lisn,
220d55daadcSGreg Kurz                              &kvm_src, true, errp);
2210c575703SCédric Le Goater }
2220c575703SCédric Le Goater 
kvmppc_xive_sync_source(SpaprXive * xive,uint32_t lisn,Error ** errp)2230c575703SCédric Le Goater void kvmppc_xive_sync_source(SpaprXive *xive, uint32_t lisn, Error **errp)
2240c575703SCédric Le Goater {
2250c575703SCédric Le Goater     kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_SOURCE_SYNC, lisn,
2260c575703SCédric Le Goater                       NULL, true, errp);
2270c575703SCédric Le Goater }
2280c575703SCédric Le Goater 
22938afd772SCédric Le Goater /*
23038afd772SCédric Le Goater  * At reset, the interrupt sources are simply created and MASKED. We
23138afd772SCédric Le Goater  * only need to inform the KVM XIVE device about their type: LSI or
23238afd772SCédric Le Goater  * MSI.
23338afd772SCédric Le Goater  */
kvmppc_xive_source_reset_one(XiveSource * xsrc,int srcno,Error ** errp)234e594c2adSDavid Gibson int kvmppc_xive_source_reset_one(XiveSource *xsrc, int srcno, Error **errp)
23538afd772SCédric Le Goater {
23638afd772SCédric Le Goater     SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
23738afd772SCédric Le Goater     uint64_t state = 0;
23838afd772SCédric Le Goater 
239179abc1fSCédric Le Goater     trace_kvm_xive_source_reset(srcno);
240179abc1fSCédric Le Goater 
241a4907119SGreg Kurz     assert(xive->fd != -1);
2423bf84e99SCédric Le Goater 
24338afd772SCédric Le Goater     if (xive_source_irq_is_lsi(xsrc, srcno)) {
24438afd772SCédric Le Goater         state |= KVM_XIVE_LEVEL_SENSITIVE;
245621f70d2SCédric Le Goater         if (xive_source_is_asserted(xsrc, srcno)) {
24638afd772SCédric Le Goater             state |= KVM_XIVE_LEVEL_ASSERTED;
24738afd772SCédric Le Goater         }
24838afd772SCédric Le Goater     }
24938afd772SCédric Le Goater 
250e594c2adSDavid Gibson     return kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_SOURCE, srcno, &state,
25138afd772SCédric Le Goater                              true, errp);
25238afd772SCédric Le Goater }
25338afd772SCédric Le Goater 
kvmppc_xive_source_reset(XiveSource * xsrc,Error ** errp)25446407a25SGreg Kurz static int kvmppc_xive_source_reset(XiveSource *xsrc, Error **errp)
25538afd772SCédric Le Goater {
2564c3539d4SCédric Le Goater     SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
25738afd772SCédric Le Goater     int i;
25838afd772SCédric Le Goater 
2596d24795eSGreg Kurz     for (i = 0; i < xsrc->nr_irqs; i++) {
26046407a25SGreg Kurz         int ret;
26138afd772SCédric Le Goater 
2624c3539d4SCédric Le Goater         if (!xive_eas_is_valid(&xive->eat[i])) {
2634c3539d4SCédric Le Goater             continue;
2644c3539d4SCédric Le Goater         }
2654c3539d4SCédric Le Goater 
26646407a25SGreg Kurz         ret = kvmppc_xive_source_reset_one(xsrc, i, errp);
26746407a25SGreg Kurz         if (ret < 0) {
26846407a25SGreg Kurz             return ret;
26938afd772SCédric Le Goater         }
27038afd772SCédric Le Goater     }
27146407a25SGreg Kurz 
27246407a25SGreg Kurz     return 0;
27338afd772SCédric Le Goater }
27438afd772SCédric Le Goater 
2750c575703SCédric Le Goater /*
2760c575703SCédric Le Goater  * This is used to perform the magic loads on the ESB pages, described
2770c575703SCédric Le Goater  * in xive.h.
2780c575703SCédric Le Goater  *
2790c575703SCédric Le Goater  * Memory barriers should not be needed for loads (no store for now).
2800c575703SCédric Le Goater  */
xive_esb_rw(XiveSource * xsrc,int srcno,uint32_t offset,uint64_t data,bool write)2810c575703SCédric Le Goater static uint64_t xive_esb_rw(XiveSource *xsrc, int srcno, uint32_t offset,
2820c575703SCédric Le Goater                             uint64_t data, bool write)
2830c575703SCédric Le Goater {
2840c575703SCédric Le Goater     uint64_t *addr = xsrc->esb_mmap + xive_source_esb_mgmt(xsrc, srcno) +
2850c575703SCédric Le Goater         offset;
2860c575703SCédric Le Goater 
2870c575703SCédric Le Goater     if (write) {
2880c575703SCédric Le Goater         *addr = cpu_to_be64(data);
2890c575703SCédric Le Goater         return -1;
2900c575703SCédric Le Goater     } else {
2910c575703SCédric Le Goater         /* Prevent the compiler from optimizing away the load */
2920c575703SCédric Le Goater         volatile uint64_t value = be64_to_cpu(*addr);
2930c575703SCédric Le Goater         return value;
2940c575703SCédric Le Goater     }
2950c575703SCédric Le Goater }
2960c575703SCédric Le Goater 
xive_esb_read(XiveSource * xsrc,int srcno,uint32_t offset)2970c575703SCédric Le Goater static uint8_t xive_esb_read(XiveSource *xsrc, int srcno, uint32_t offset)
2980c575703SCédric Le Goater {
2990c575703SCédric Le Goater     return xive_esb_rw(xsrc, srcno, offset, 0, 0) & 0x3;
3000c575703SCédric Le Goater }
3010c575703SCédric Le Goater 
kvmppc_xive_esb_trigger(XiveSource * xsrc,int srcno)302fb8dc327SCédric Le Goater static void kvmppc_xive_esb_trigger(XiveSource *xsrc, int srcno)
3030c575703SCédric Le Goater {
304644c6869SCédric Le Goater     xive_esb_rw(xsrc, srcno, 0, 0, true);
3050c575703SCédric Le Goater }
3060c575703SCédric Le Goater 
kvmppc_xive_esb_rw(XiveSource * xsrc,int srcno,uint32_t offset,uint64_t data,bool write)3070c575703SCédric Le Goater uint64_t kvmppc_xive_esb_rw(XiveSource *xsrc, int srcno, uint32_t offset,
3080c575703SCédric Le Goater                             uint64_t data, bool write)
3090c575703SCédric Le Goater {
3100c575703SCédric Le Goater     if (write) {
3110c575703SCédric Le Goater         return xive_esb_rw(xsrc, srcno, offset, data, 1);
3120c575703SCédric Le Goater     }
3130c575703SCédric Le Goater 
3140c575703SCédric Le Goater     /*
3150c575703SCédric Le Goater      * Special Load EOI handling for LSI sources. Q bit is never set
3160c575703SCédric Le Goater      * and the interrupt should be re-triggered if the level is still
3170c575703SCédric Le Goater      * asserted.
3180c575703SCédric Le Goater      */
3190c575703SCédric Le Goater     if (xive_source_irq_is_lsi(xsrc, srcno) &&
3200c575703SCédric Le Goater         offset == XIVE_ESB_LOAD_EOI) {
3210c575703SCédric Le Goater         xive_esb_read(xsrc, srcno, XIVE_ESB_SET_PQ_00);
322621f70d2SCédric Le Goater         if (xive_source_is_asserted(xsrc, srcno)) {
323fb8dc327SCédric Le Goater             kvmppc_xive_esb_trigger(xsrc, srcno);
3240c575703SCédric Le Goater         }
3250c575703SCédric Le Goater         return 0;
3260c575703SCédric Le Goater     } else {
3270c575703SCédric Le Goater         return xive_esb_rw(xsrc, srcno, offset, 0, 0);
3280c575703SCédric Le Goater     }
3290c575703SCédric Le Goater }
3300c575703SCédric Le Goater 
kvmppc_xive_source_get_state(XiveSource * xsrc)3317bfc759cSCédric Le Goater static void kvmppc_xive_source_get_state(XiveSource *xsrc)
3327bfc759cSCédric Le Goater {
3334c3539d4SCédric Le Goater     SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
3347bfc759cSCédric Le Goater     int i;
3357bfc759cSCédric Le Goater 
3367bfc759cSCédric Le Goater     for (i = 0; i < xsrc->nr_irqs; i++) {
3374c3539d4SCédric Le Goater         uint8_t pq;
3384c3539d4SCédric Le Goater 
3396d24795eSGreg Kurz         if (!xive_eas_is_valid(&xive->eat[i])) {
3404c3539d4SCédric Le Goater             continue;
3414c3539d4SCédric Le Goater         }
3424c3539d4SCédric Le Goater 
3437bfc759cSCédric Le Goater         /* Perform a load without side effect to retrieve the PQ bits */
3444c3539d4SCédric Le Goater         pq = xive_esb_read(xsrc, i, XIVE_ESB_GET);
3457bfc759cSCédric Le Goater 
3467bfc759cSCédric Le Goater         /* and save PQ locally */
3477bfc759cSCédric Le Goater         xive_source_esb_set(xsrc, i, pq);
3487bfc759cSCédric Le Goater     }
3497bfc759cSCédric Le Goater }
3507bfc759cSCédric Le Goater 
kvmppc_xive_source_set_irq(void * opaque,int srcno,int val)35138afd772SCédric Le Goater void kvmppc_xive_source_set_irq(void *opaque, int srcno, int val)
35238afd772SCédric Le Goater {
35338afd772SCédric Le Goater     XiveSource *xsrc = opaque;
35438afd772SCédric Le Goater 
35538afd772SCédric Le Goater     if (!xive_source_irq_is_lsi(xsrc, srcno)) {
35638afd772SCédric Le Goater         if (!val) {
35738afd772SCédric Le Goater             return;
35838afd772SCédric Le Goater         }
35938afd772SCédric Le Goater     } else {
360621f70d2SCédric Le Goater         xive_source_set_asserted(xsrc, srcno, val);
36138afd772SCédric Le Goater     }
36258246041SGreg Kurz 
363fb8dc327SCédric Le Goater     kvmppc_xive_esb_trigger(xsrc, srcno);
36438afd772SCédric Le Goater }
36538afd772SCédric Le Goater 
36638afd772SCédric Le Goater /*
36738afd772SCédric Le Goater  * sPAPR XIVE interrupt controller (KVM)
36838afd772SCédric Le Goater  */
kvmppc_xive_get_queue_config(SpaprXive * xive,uint8_t end_blk,uint32_t end_idx,XiveEND * end,Error ** errp)369f9a548edSGreg Kurz int kvmppc_xive_get_queue_config(SpaprXive *xive, uint8_t end_blk,
3700c575703SCédric Le Goater                                  uint32_t end_idx, XiveEND *end,
3710c575703SCédric Le Goater                                  Error **errp)
3720c575703SCédric Le Goater {
3730c575703SCédric Le Goater     struct kvm_ppc_xive_eq kvm_eq = { 0 };
3740c575703SCédric Le Goater     uint64_t kvm_eq_idx;
3750c575703SCédric Le Goater     uint8_t priority;
3760c575703SCédric Le Goater     uint32_t server;
377f9a548edSGreg Kurz     int ret;
3780c575703SCédric Le Goater 
3790c575703SCédric Le Goater     assert(xive_end_is_valid(end));
3800c575703SCédric Le Goater 
3810c575703SCédric Le Goater     /* Encode the tuple (server, prio) as a KVM EQ index */
3820c575703SCédric Le Goater     spapr_xive_end_to_target(end_blk, end_idx, &server, &priority);
3830c575703SCédric Le Goater 
3840c575703SCédric Le Goater     kvm_eq_idx = priority << KVM_XIVE_EQ_PRIORITY_SHIFT &
3850c575703SCédric Le Goater             KVM_XIVE_EQ_PRIORITY_MASK;
3860c575703SCédric Le Goater     kvm_eq_idx |= server << KVM_XIVE_EQ_SERVER_SHIFT &
3870c575703SCédric Le Goater         KVM_XIVE_EQ_SERVER_MASK;
3880c575703SCédric Le Goater 
389f9a548edSGreg Kurz     ret = kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_EQ_CONFIG, kvm_eq_idx,
390f9a548edSGreg Kurz                             &kvm_eq, false, errp);
391f9a548edSGreg Kurz     if (ret < 0) {
392f9a548edSGreg Kurz         return ret;
3930c575703SCédric Le Goater     }
3940c575703SCédric Le Goater 
3950c575703SCédric Le Goater     /*
3960c575703SCédric Le Goater      * The EQ index and toggle bit are updated by HW. These are the
3970c575703SCédric Le Goater      * only fields from KVM we want to update QEMU with. The other END
3980c575703SCédric Le Goater      * fields should already be in the QEMU END table.
3990c575703SCédric Le Goater      */
4000c575703SCédric Le Goater     end->w1 = xive_set_field32(END_W1_GENERATION, 0ul, kvm_eq.qtoggle) |
4010c575703SCédric Le Goater         xive_set_field32(END_W1_PAGE_OFF, 0ul, kvm_eq.qindex);
402f9a548edSGreg Kurz 
403f9a548edSGreg Kurz     return 0;
4040c575703SCédric Le Goater }
4050c575703SCédric Le Goater 
kvmppc_xive_set_queue_config(SpaprXive * xive,uint8_t end_blk,uint32_t end_idx,XiveEND * end,Error ** errp)406f9a548edSGreg Kurz int kvmppc_xive_set_queue_config(SpaprXive *xive, uint8_t end_blk,
4070c575703SCédric Le Goater                                  uint32_t end_idx, XiveEND *end,
4080c575703SCédric Le Goater                                  Error **errp)
4090c575703SCédric Le Goater {
4100c575703SCédric Le Goater     struct kvm_ppc_xive_eq kvm_eq = { 0 };
4110c575703SCédric Le Goater     uint64_t kvm_eq_idx;
4120c575703SCédric Le Goater     uint8_t priority;
4130c575703SCédric Le Goater     uint32_t server;
4140c575703SCédric Le Goater 
4150c575703SCédric Le Goater     /*
4160c575703SCédric Le Goater      * Build the KVM state from the local END structure.
4170c575703SCédric Le Goater      */
4180c575703SCédric Le Goater 
4190c575703SCédric Le Goater     kvm_eq.flags = 0;
4200c575703SCédric Le Goater     if (xive_get_field32(END_W0_UCOND_NOTIFY, end->w0)) {
4210c575703SCédric Le Goater         kvm_eq.flags |= KVM_XIVE_EQ_ALWAYS_NOTIFY;
4220c575703SCédric Le Goater     }
4230c575703SCédric Le Goater 
4240c575703SCédric Le Goater     /*
4250c575703SCédric Le Goater      * If the hcall is disabling the EQ, set the size and page address
4260c575703SCédric Le Goater      * to zero. When migrating, only valid ENDs are taken into
4270c575703SCédric Le Goater      * account.
4280c575703SCédric Le Goater      */
4290c575703SCédric Le Goater     if (xive_end_is_valid(end)) {
4300c575703SCédric Le Goater         kvm_eq.qshift = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
4310c575703SCédric Le Goater         kvm_eq.qaddr  = xive_end_qaddr(end);
4320c575703SCédric Le Goater         /*
4330c575703SCédric Le Goater          * The EQ toggle bit and index should only be relevant when
4340c575703SCédric Le Goater          * restoring the EQ state
4350c575703SCédric Le Goater          */
4360c575703SCédric Le Goater         kvm_eq.qtoggle = xive_get_field32(END_W1_GENERATION, end->w1);
4370c575703SCédric Le Goater         kvm_eq.qindex  = xive_get_field32(END_W1_PAGE_OFF, end->w1);
4380c575703SCédric Le Goater     } else {
4390c575703SCédric Le Goater         kvm_eq.qshift = 0;
4400c575703SCédric Le Goater         kvm_eq.qaddr  = 0;
4410c575703SCédric Le Goater     }
4420c575703SCédric Le Goater 
4430c575703SCédric Le Goater     /* Encode the tuple (server, prio) as a KVM EQ index */
4440c575703SCédric Le Goater     spapr_xive_end_to_target(end_blk, end_idx, &server, &priority);
4450c575703SCédric Le Goater 
4460c575703SCédric Le Goater     kvm_eq_idx = priority << KVM_XIVE_EQ_PRIORITY_SHIFT &
4470c575703SCédric Le Goater             KVM_XIVE_EQ_PRIORITY_MASK;
4480c575703SCédric Le Goater     kvm_eq_idx |= server << KVM_XIVE_EQ_SERVER_SHIFT &
4490c575703SCédric Le Goater         KVM_XIVE_EQ_SERVER_MASK;
4500c575703SCédric Le Goater 
451f9a548edSGreg Kurz     return
4520c575703SCédric Le Goater         kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_EQ_CONFIG, kvm_eq_idx,
453f9a548edSGreg Kurz                           &kvm_eq, true, errp);
4540c575703SCédric Le Goater }
4550c575703SCédric Le Goater 
kvmppc_xive_reset(SpaprXive * xive,Error ** errp)4560c575703SCédric Le Goater void kvmppc_xive_reset(SpaprXive *xive, Error **errp)
4570c575703SCédric Le Goater {
4580c575703SCédric Le Goater     kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_CTRL, KVM_DEV_XIVE_RESET,
4590c575703SCédric Le Goater                       NULL, true, errp);
4600c575703SCédric Le Goater }
46138afd772SCédric Le Goater 
kvmppc_xive_get_queues(SpaprXive * xive,Error ** errp)462d53482a7SGreg Kurz static int kvmppc_xive_get_queues(SpaprXive *xive, Error **errp)
4637bfc759cSCédric Le Goater {
4647bfc759cSCédric Le Goater     int i;
465d53482a7SGreg Kurz     int ret;
4667bfc759cSCédric Le Goater 
4677bfc759cSCédric Le Goater     for (i = 0; i < xive->nr_ends; i++) {
4687bfc759cSCédric Le Goater         if (!xive_end_is_valid(&xive->endt[i])) {
4697bfc759cSCédric Le Goater             continue;
4707bfc759cSCédric Le Goater         }
4717bfc759cSCédric Le Goater 
472d53482a7SGreg Kurz         ret = kvmppc_xive_get_queue_config(xive, SPAPR_XIVE_BLOCK_ID, i,
473d53482a7SGreg Kurz                                            &xive->endt[i], errp);
474d53482a7SGreg Kurz         if (ret < 0) {
475d53482a7SGreg Kurz             return ret;
4767bfc759cSCédric Le Goater         }
4777bfc759cSCédric Le Goater     }
478d53482a7SGreg Kurz 
479d53482a7SGreg Kurz     return 0;
4807bfc759cSCédric Le Goater }
4817bfc759cSCédric Le Goater 
4829b88cd76SCédric Le Goater /*
4839b88cd76SCédric Le Goater  * The primary goal of the XIVE VM change handler is to mark the EQ
4849b88cd76SCédric Le Goater  * pages dirty when all XIVE event notifications have stopped.
4859b88cd76SCédric Le Goater  *
4869b88cd76SCédric Le Goater  * Whenever the VM is stopped, the VM change handler sets the source
4879b88cd76SCédric Le Goater  * PQs to PENDING to stop the flow of events and to possibly catch a
4889b4b4e51SMichael Tokarev  * triggered interrupt occurring while the VM is stopped. The previous
4899b88cd76SCédric Le Goater  * state is saved in anticipation of a migration. The XIVE controller
4909b88cd76SCédric Le Goater  * is then synced through KVM to flush any in-flight event
4919b88cd76SCédric Le Goater  * notification and stabilize the EQs.
4929b88cd76SCédric Le Goater  *
4939b88cd76SCédric Le Goater  * At this stage, we can mark the EQ page dirty and let a migration
4949b88cd76SCédric Le Goater  * sequence transfer the EQ pages to the destination, which is done
4959b88cd76SCédric Le Goater  * just after the stop state.
4969b88cd76SCédric Le Goater  *
4979b88cd76SCédric Le Goater  * The previous configuration of the sources is restored when the VM
4989b88cd76SCédric Le Goater  * runs again. If an interrupt was queued while the VM was stopped,
4999b88cd76SCédric Le Goater  * simply generate a trigger.
5009b88cd76SCédric Le Goater  */
kvmppc_xive_change_state_handler(void * opaque,bool running,RunState state)501538f0497SPhilippe Mathieu-Daudé static void kvmppc_xive_change_state_handler(void *opaque, bool running,
5029b88cd76SCédric Le Goater                                              RunState state)
5039b88cd76SCédric Le Goater {
5049b88cd76SCédric Le Goater     SpaprXive *xive = opaque;
5059b88cd76SCédric Le Goater     XiveSource *xsrc = &xive->source;
5069b88cd76SCédric Le Goater     Error *local_err = NULL;
5079b88cd76SCédric Le Goater     int i;
5089b88cd76SCédric Le Goater 
5099b88cd76SCédric Le Goater     /*
5109b88cd76SCédric Le Goater      * Restore the sources to their initial state. This is called when
5119b88cd76SCédric Le Goater      * the VM resumes after a stop or a migration.
5129b88cd76SCédric Le Goater      */
5139b88cd76SCédric Le Goater     if (running) {
5149b88cd76SCédric Le Goater         for (i = 0; i < xsrc->nr_irqs; i++) {
5154c3539d4SCédric Le Goater             uint8_t pq;
5169b88cd76SCédric Le Goater             uint8_t old_pq;
5179b88cd76SCédric Le Goater 
5186d24795eSGreg Kurz             if (!xive_eas_is_valid(&xive->eat[i])) {
5194c3539d4SCédric Le Goater                 continue;
5204c3539d4SCédric Le Goater             }
5214c3539d4SCédric Le Goater 
5224c3539d4SCédric Le Goater             pq = xive_source_esb_get(xsrc, i);
5239b88cd76SCédric Le Goater             old_pq = xive_esb_read(xsrc, i, XIVE_ESB_SET_PQ_00 + (pq << 8));
5249b88cd76SCédric Le Goater 
5259b88cd76SCédric Le Goater             /*
5269b88cd76SCédric Le Goater              * An interrupt was queued while the VM was stopped,
5279b88cd76SCédric Le Goater              * generate a trigger.
5289b88cd76SCédric Le Goater              */
5299b88cd76SCédric Le Goater             if (pq == XIVE_ESB_RESET && old_pq == XIVE_ESB_QUEUED) {
530fb8dc327SCédric Le Goater                 kvmppc_xive_esb_trigger(xsrc, i);
5319b88cd76SCédric Le Goater             }
5329b88cd76SCédric Le Goater         }
5339b88cd76SCédric Le Goater 
5349b88cd76SCédric Le Goater         return;
5359b88cd76SCédric Le Goater     }
5369b88cd76SCédric Le Goater 
5379b88cd76SCédric Le Goater     /*
5389b88cd76SCédric Le Goater      * Mask the sources, to stop the flow of event notifications, and
5399b88cd76SCédric Le Goater      * save the PQs locally in the XiveSource object. The XiveSource
5409b88cd76SCédric Le Goater      * state will be collected later on by its vmstate handler if a
5419b88cd76SCédric Le Goater      * migration is in progress.
5429b88cd76SCédric Le Goater      */
5439b88cd76SCédric Le Goater     for (i = 0; i < xsrc->nr_irqs; i++) {
5444c3539d4SCédric Le Goater         uint8_t pq;
5454c3539d4SCédric Le Goater 
5466d24795eSGreg Kurz         if (!xive_eas_is_valid(&xive->eat[i])) {
5474c3539d4SCédric Le Goater             continue;
5484c3539d4SCédric Le Goater         }
5494c3539d4SCédric Le Goater 
5504c3539d4SCédric Le Goater         pq = xive_esb_read(xsrc, i, XIVE_ESB_GET);
5519b88cd76SCédric Le Goater 
5529b88cd76SCédric Le Goater         /*
5539b88cd76SCédric Le Goater          * PQ is set to PENDING to possibly catch a triggered
5549b4b4e51SMichael Tokarev          * interrupt occurring while the VM is stopped (hotplug event
5559b88cd76SCédric Le Goater          * for instance) .
5569b88cd76SCédric Le Goater          */
5579b88cd76SCédric Le Goater         if (pq != XIVE_ESB_OFF) {
5589b88cd76SCédric Le Goater             pq = xive_esb_read(xsrc, i, XIVE_ESB_SET_PQ_10);
5599b88cd76SCédric Le Goater         }
5609b88cd76SCédric Le Goater         xive_source_esb_set(xsrc, i, pq);
5619b88cd76SCédric Le Goater     }
5629b88cd76SCédric Le Goater 
5639b88cd76SCédric Le Goater     /*
5649b88cd76SCédric Le Goater      * Sync the XIVE controller in KVM, to flush in-flight event
5659b88cd76SCédric Le Goater      * notification that should be enqueued in the EQs and mark the
5669b88cd76SCédric Le Goater      * XIVE EQ pages dirty to collect all updates.
5679b88cd76SCédric Le Goater      */
5689b88cd76SCédric Le Goater     kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_CTRL,
5699b88cd76SCédric Le Goater                       KVM_DEV_XIVE_EQ_SYNC, NULL, true, &local_err);
5709b88cd76SCédric Le Goater     if (local_err) {
5719b88cd76SCédric Le Goater         error_report_err(local_err);
5729b88cd76SCédric Le Goater         return;
5739b88cd76SCédric Le Goater     }
5749b88cd76SCédric Le Goater }
5759b88cd76SCédric Le Goater 
kvmppc_xive_synchronize_state(SpaprXive * xive,Error ** errp)5767bfc759cSCédric Le Goater void kvmppc_xive_synchronize_state(SpaprXive *xive, Error **errp)
5777bfc759cSCédric Le Goater {
578a4907119SGreg Kurz     assert(xive->fd != -1);
5793bf84e99SCédric Le Goater 
5809b88cd76SCédric Le Goater     /*
5819b88cd76SCédric Le Goater      * When the VM is stopped, the sources are masked and the previous
5829b88cd76SCédric Le Goater      * state is saved in anticipation of a migration. We should not
5839b88cd76SCédric Le Goater      * synchronize the source state in that case else we will override
5849b88cd76SCédric Le Goater      * the saved state.
5859b88cd76SCédric Le Goater      */
5869b88cd76SCédric Le Goater     if (runstate_is_running()) {
5877bfc759cSCédric Le Goater         kvmppc_xive_source_get_state(&xive->source);
5889b88cd76SCédric Le Goater     }
5897bfc759cSCédric Le Goater 
5907bfc759cSCédric Le Goater     /* EAT: there is no extra state to query from KVM */
5917bfc759cSCédric Le Goater 
5927bfc759cSCédric Le Goater     /* ENDT */
5937bfc759cSCédric Le Goater     kvmppc_xive_get_queues(xive, errp);
5947bfc759cSCédric Le Goater }
5957bfc759cSCédric Le Goater 
596277dd3d7SCédric Le Goater /*
597277dd3d7SCédric Le Goater  * The SpaprXive 'pre_save' method is called by the vmstate handler of
598277dd3d7SCédric Le Goater  * the SpaprXive model, after the XIVE controller is synced in the VM
599277dd3d7SCédric Le Goater  * change handler.
600277dd3d7SCédric Le Goater  */
kvmppc_xive_pre_save(SpaprXive * xive)601277dd3d7SCédric Le Goater int kvmppc_xive_pre_save(SpaprXive *xive)
602277dd3d7SCédric Le Goater {
603277dd3d7SCédric Le Goater     Error *local_err = NULL;
60442a92d92SGreg Kurz     int ret;
605277dd3d7SCédric Le Goater 
606a4907119SGreg Kurz     assert(xive->fd != -1);
6073bf84e99SCédric Le Goater 
608277dd3d7SCédric Le Goater     /* EAT: there is no extra state to query from KVM */
609277dd3d7SCédric Le Goater 
610277dd3d7SCédric Le Goater     /* ENDT */
61142a92d92SGreg Kurz     ret = kvmppc_xive_get_queues(xive, &local_err);
61242a92d92SGreg Kurz     if (ret < 0) {
613277dd3d7SCédric Le Goater         error_report_err(local_err);
61442a92d92SGreg Kurz         return ret;
615277dd3d7SCédric Le Goater     }
616277dd3d7SCédric Le Goater 
617277dd3d7SCédric Le Goater     return 0;
618277dd3d7SCédric Le Goater }
619277dd3d7SCédric Le Goater 
620277dd3d7SCédric Le Goater /*
621277dd3d7SCédric Le Goater  * The SpaprXive 'post_load' method is not called by a vmstate
622277dd3d7SCédric Le Goater  * handler. It is called at the sPAPR machine level at the end of the
623277dd3d7SCédric Le Goater  * migration sequence by the sPAPR IRQ backend 'post_load' method,
624277dd3d7SCédric Le Goater  * when all XIVE states have been transferred and loaded.
625277dd3d7SCédric Le Goater  */
kvmppc_xive_post_load(SpaprXive * xive,int version_id)626277dd3d7SCédric Le Goater int kvmppc_xive_post_load(SpaprXive *xive, int version_id)
627277dd3d7SCédric Le Goater {
628277dd3d7SCédric Le Goater     Error *local_err = NULL;
629277dd3d7SCédric Le Goater     CPUState *cs;
630277dd3d7SCédric Le Goater     int i;
631a845a54cSGreg Kurz     int ret;
632277dd3d7SCédric Le Goater 
6333bf84e99SCédric Le Goater     /* The KVM XIVE device should be in use */
6343bf84e99SCédric Le Goater     assert(xive->fd != -1);
6353bf84e99SCédric Le Goater 
6369b4b4e51SMichael Tokarev     /* Restore the ENDT first. The targeting depends on it. */
637277dd3d7SCédric Le Goater     for (i = 0; i < xive->nr_ends; i++) {
638277dd3d7SCédric Le Goater         if (!xive_end_is_valid(&xive->endt[i])) {
639277dd3d7SCédric Le Goater             continue;
640277dd3d7SCédric Le Goater         }
641277dd3d7SCédric Le Goater 
642a845a54cSGreg Kurz         ret = kvmppc_xive_set_queue_config(xive, SPAPR_XIVE_BLOCK_ID, i,
643277dd3d7SCédric Le Goater                                            &xive->endt[i], &local_err);
644a845a54cSGreg Kurz         if (ret < 0) {
645a845a54cSGreg Kurz             goto fail;
646277dd3d7SCédric Le Goater         }
647277dd3d7SCédric Le Goater     }
648277dd3d7SCédric Le Goater 
649277dd3d7SCédric Le Goater     /* Restore the EAT */
650277dd3d7SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
6516d24795eSGreg Kurz         if (!xive_eas_is_valid(&xive->eat[i])) {
652277dd3d7SCédric Le Goater             continue;
653277dd3d7SCédric Le Goater         }
654277dd3d7SCédric Le Goater 
6556d24795eSGreg Kurz         /*
6566d24795eSGreg Kurz          * We can only restore the source config if the source has been
6576d24795eSGreg Kurz          * previously set in KVM. Since we don't do that for all interrupts
6586d24795eSGreg Kurz          * at reset time anymore, let's do it now.
6596d24795eSGreg Kurz          */
6606d24795eSGreg Kurz         ret = kvmppc_xive_source_reset_one(&xive->source, i, &local_err);
6616d24795eSGreg Kurz         if (ret < 0) {
6626d24795eSGreg Kurz             goto fail;
6636d24795eSGreg Kurz         }
6646d24795eSGreg Kurz 
665a845a54cSGreg Kurz         ret = kvmppc_xive_set_source_config(xive, i, &xive->eat[i], &local_err);
666a845a54cSGreg Kurz         if (ret < 0) {
667a845a54cSGreg Kurz             goto fail;
668277dd3d7SCédric Le Goater         }
669277dd3d7SCédric Le Goater     }
670277dd3d7SCédric Le Goater 
671310cda5bSCédric Le Goater     /*
672310cda5bSCédric Le Goater      * Restore the thread interrupt contexts of initial CPUs.
673310cda5bSCédric Le Goater      *
674310cda5bSCédric Le Goater      * The context of hotplugged CPUs is restored later, by the
675310cda5bSCédric Le Goater      * 'post_load' handler of the XiveTCTX model because they are not
676310cda5bSCédric Le Goater      * available at the time the SpaprXive 'post_load' method is
677310cda5bSCédric Le Goater      * called. We can not restore the context of all CPUs in the
678310cda5bSCédric Le Goater      * 'post_load' handler of XiveTCTX because the machine is not
679310cda5bSCédric Le Goater      * necessarily connected to the KVM device at that time.
680310cda5bSCédric Le Goater      */
681277dd3d7SCédric Le Goater     CPU_FOREACH(cs) {
682277dd3d7SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
683277dd3d7SCédric Le Goater 
684a845a54cSGreg Kurz         ret = kvmppc_xive_cpu_set_state(spapr_cpu_state(cpu)->tctx, &local_err);
685a845a54cSGreg Kurz         if (ret < 0) {
686a845a54cSGreg Kurz             goto fail;
687277dd3d7SCédric Le Goater         }
688277dd3d7SCédric Le Goater     }
689277dd3d7SCédric Le Goater 
690277dd3d7SCédric Le Goater     /* The source states will be restored when the machine starts running */
691277dd3d7SCédric Le Goater     return 0;
692a845a54cSGreg Kurz 
693a845a54cSGreg Kurz fail:
694a845a54cSGreg Kurz     error_report_err(local_err);
695a845a54cSGreg Kurz     return ret;
696277dd3d7SCédric Le Goater }
697277dd3d7SCédric Le Goater 
698b14adb4aSGreg Kurz /* Returns MAP_FAILED on error and sets errno */
kvmppc_xive_mmap(SpaprXive * xive,int pgoff,size_t len,Error ** errp)69938afd772SCédric Le Goater static void *kvmppc_xive_mmap(SpaprXive *xive, int pgoff, size_t len,
70038afd772SCédric Le Goater                               Error **errp)
70138afd772SCédric Le Goater {
70238afd772SCédric Le Goater     void *addr;
70338afd772SCédric Le Goater     uint32_t page_shift = 16; /* TODO: fix page_shift */
70438afd772SCédric Le Goater 
70538afd772SCédric Le Goater     addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, xive->fd,
70638afd772SCédric Le Goater                 pgoff << page_shift);
70738afd772SCédric Le Goater     if (addr == MAP_FAILED) {
70838afd772SCédric Le Goater         error_setg_errno(errp, errno, "XIVE: unable to set memory mapping");
70938afd772SCédric Le Goater     }
71038afd772SCédric Le Goater 
71138afd772SCédric Le Goater     return addr;
71238afd772SCédric Le Goater }
71338afd772SCédric Le Goater 
71438afd772SCédric Le Goater /*
71538afd772SCédric Le Goater  * All the XIVE memory regions are now backed by mappings from the KVM
71638afd772SCédric Le Goater  * XIVE device.
71738afd772SCédric Le Goater  */
kvmppc_xive_connect(SpaprInterruptController * intc,uint32_t nr_servers,Error ** errp)7184ffb7496SGreg Kurz int kvmppc_xive_connect(SpaprInterruptController *intc, uint32_t nr_servers,
7194ffb7496SGreg Kurz                         Error **errp)
72038afd772SCédric Le Goater {
72198a39a79SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
72238afd772SCédric Le Goater     XiveSource *xsrc = &xive->source;
72307f27705SNicholas Piggin     uint64_t esb_len = xive_source_esb_len(xsrc);
72438afd772SCédric Le Goater     size_t tima_len = 4ull << TM_SHIFT;
7253f777abcSCédric Le Goater     CPUState *cs;
72682f086b5SGreg Kurz     int fd;
727b14adb4aSGreg Kurz     void *addr;
7286cdc0e20SGreg Kurz     int ret;
7293f777abcSCédric Le Goater 
7303f777abcSCédric Le Goater     /*
7313f777abcSCédric Le Goater      * The KVM XIVE device already in use. This is the case when
7323f777abcSCédric Le Goater      * rebooting under the XIVE-only interrupt mode.
7333f777abcSCédric Le Goater      */
7343f777abcSCédric Le Goater     if (xive->fd != -1) {
73598a39a79SDavid Gibson         return 0;
7363f777abcSCédric Le Goater     }
73738afd772SCédric Le Goater 
73838afd772SCédric Le Goater     if (!kvmppc_has_cap_xive()) {
73938afd772SCédric Le Goater         error_setg(errp, "IRQ_XIVE capability must be present for KVM");
74098a39a79SDavid Gibson         return -1;
74138afd772SCédric Le Goater     }
74238afd772SCédric Le Goater 
74338afd772SCédric Le Goater     /* First, create the KVM XIVE device */
74482f086b5SGreg Kurz     fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_XIVE, false);
74582f086b5SGreg Kurz     if (fd < 0) {
74682f086b5SGreg Kurz         error_setg_errno(errp, -fd, "XIVE: error creating KVM device");
74798a39a79SDavid Gibson         return -1;
74838afd772SCédric Le Goater     }
74982f086b5SGreg Kurz     xive->fd = fd;
75038afd772SCédric Le Goater 
75174f23d43SGreg Kurz     /* Tell KVM about the # of VCPUs we may have */
75274f23d43SGreg Kurz     if (kvm_device_check_attr(xive->fd, KVM_DEV_XIVE_GRP_CTRL,
75374f23d43SGreg Kurz                               KVM_DEV_XIVE_NR_SERVERS)) {
7546cdc0e20SGreg Kurz         ret = kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_CTRL,
75574f23d43SGreg Kurz                                 KVM_DEV_XIVE_NR_SERVERS, &nr_servers, true,
7566cdc0e20SGreg Kurz                                 errp);
7576cdc0e20SGreg Kurz         if (ret < 0) {
75874f23d43SGreg Kurz             goto fail;
75974f23d43SGreg Kurz         }
76074f23d43SGreg Kurz     }
76174f23d43SGreg Kurz 
76238afd772SCédric Le Goater     /*
76338afd772SCédric Le Goater      * 1. Source ESB pages - KVM mapping
76438afd772SCédric Le Goater      */
7656cdc0e20SGreg Kurz     addr = kvmppc_xive_mmap(xive, KVM_XIVE_ESB_PAGE_OFFSET, esb_len, errp);
766b14adb4aSGreg Kurz     if (addr == MAP_FAILED) {
7671c3d4a8fSGreg Kurz         goto fail;
76838afd772SCédric Le Goater     }
769b14adb4aSGreg Kurz     xsrc->esb_mmap = addr;
77038afd772SCédric Le Goater 
771981b1c62SCédric Le Goater     memory_region_init_ram_device_ptr(&xsrc->esb_mmio_kvm, OBJECT(xsrc),
772cf36e5b3SGreg Kurz                                       "xive.esb-kvm", esb_len, xsrc->esb_mmap);
773981b1c62SCédric Le Goater     memory_region_add_subregion_overlap(&xsrc->esb_mmio, 0,
774981b1c62SCédric Le Goater                                         &xsrc->esb_mmio_kvm, 1);
77538afd772SCédric Le Goater 
77638afd772SCédric Le Goater     /*
77738afd772SCédric Le Goater      * 2. END ESB pages (No KVM support yet)
77838afd772SCédric Le Goater      */
77938afd772SCédric Le Goater 
78038afd772SCédric Le Goater     /*
78138afd772SCédric Le Goater      * 3. TIMA pages - KVM mapping
78238afd772SCédric Le Goater      */
7836cdc0e20SGreg Kurz     addr = kvmppc_xive_mmap(xive, KVM_XIVE_TIMA_PAGE_OFFSET, tima_len, errp);
784b14adb4aSGreg Kurz     if (addr == MAP_FAILED) {
7851c3d4a8fSGreg Kurz         goto fail;
78638afd772SCédric Le Goater     }
787b14adb4aSGreg Kurz     xive->tm_mmap = addr;
788b14adb4aSGreg Kurz 
789981b1c62SCédric Le Goater     memory_region_init_ram_device_ptr(&xive->tm_mmio_kvm, OBJECT(xive),
79038afd772SCédric Le Goater                                       "xive.tima", tima_len, xive->tm_mmap);
791981b1c62SCédric Le Goater     memory_region_add_subregion_overlap(&xive->tm_mmio, 0,
792981b1c62SCédric Le Goater                                         &xive->tm_mmio_kvm, 1);
79338afd772SCédric Le Goater 
7949b88cd76SCédric Le Goater     xive->change = qemu_add_vm_change_state_handler(
7959b88cd76SCédric Le Goater         kvmppc_xive_change_state_handler, xive);
7969b88cd76SCédric Le Goater 
7973f777abcSCédric Le Goater     /* Connect the presenters to the initial VCPUs of the machine */
7983f777abcSCédric Le Goater     CPU_FOREACH(cs) {
7993f777abcSCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
8003f777abcSCédric Le Goater 
8016cdc0e20SGreg Kurz         ret = kvmppc_xive_cpu_connect(spapr_cpu_state(cpu)->tctx, errp);
8026cdc0e20SGreg Kurz         if (ret < 0) {
8031c3d4a8fSGreg Kurz             goto fail;
8043f777abcSCédric Le Goater         }
8053f777abcSCédric Le Goater     }
8063f777abcSCédric Le Goater 
8073f777abcSCédric Le Goater     /* Update the KVM sources */
8086cdc0e20SGreg Kurz     ret = kvmppc_xive_source_reset(xsrc, errp);
8096cdc0e20SGreg Kurz     if (ret < 0) {
8101c3d4a8fSGreg Kurz         goto fail;
8113f777abcSCédric Le Goater     }
8123f777abcSCédric Le Goater 
81338afd772SCédric Le Goater     kvm_kernel_irqchip = true;
81438afd772SCédric Le Goater     kvm_msi_via_irqfd_allowed = true;
81538afd772SCédric Le Goater     kvm_gsi_direct_mapping = true;
81698a39a79SDavid Gibson     return 0;
8171c3d4a8fSGreg Kurz 
8181c3d4a8fSGreg Kurz fail:
81998a39a79SDavid Gibson     kvmppc_xive_disconnect(intc);
82098a39a79SDavid Gibson     return -1;
82138afd772SCédric Le Goater }
82256b11587SCédric Le Goater 
kvmppc_xive_disconnect(SpaprInterruptController * intc)82398a39a79SDavid Gibson void kvmppc_xive_disconnect(SpaprInterruptController *intc)
82456b11587SCédric Le Goater {
82598a39a79SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
82656b11587SCédric Le Goater     XiveSource *xsrc;
82707f27705SNicholas Piggin     uint64_t esb_len;
82856b11587SCédric Le Goater 
829a4907119SGreg Kurz     assert(xive->fd != -1);
83056b11587SCédric Le Goater 
83156b11587SCédric Le Goater     /* Clear the KVM mapping */
83256b11587SCédric Le Goater     xsrc = &xive->source;
8333110f0eeSGreg Kurz     esb_len = xive_source_esb_len(xsrc);
83456b11587SCédric Le Goater 
8351c3d4a8fSGreg Kurz     if (xsrc->esb_mmap) {
836981b1c62SCédric Le Goater         memory_region_del_subregion(&xsrc->esb_mmio, &xsrc->esb_mmio_kvm);
837981b1c62SCédric Le Goater         object_unparent(OBJECT(&xsrc->esb_mmio_kvm));
83856b11587SCédric Le Goater         munmap(xsrc->esb_mmap, esb_len);
839981b1c62SCédric Le Goater         xsrc->esb_mmap = NULL;
8401c3d4a8fSGreg Kurz     }
84156b11587SCédric Le Goater 
8421c3d4a8fSGreg Kurz     if (xive->tm_mmap) {
843981b1c62SCédric Le Goater         memory_region_del_subregion(&xive->tm_mmio, &xive->tm_mmio_kvm);
844981b1c62SCédric Le Goater         object_unparent(OBJECT(&xive->tm_mmio_kvm));
84556b11587SCédric Le Goater         munmap(xive->tm_mmap, 4ull << TM_SHIFT);
846981b1c62SCédric Le Goater         xive->tm_mmap = NULL;
8471c3d4a8fSGreg Kurz     }
84856b11587SCédric Le Goater 
84956b11587SCédric Le Goater     /*
85056b11587SCédric Le Goater      * When the KVM device fd is closed, the KVM device is destroyed
85156b11587SCédric Le Goater      * and removed from the list of devices of the VM. The VCPU
85256b11587SCédric Le Goater      * presenters are also detached from the device.
85356b11587SCédric Le Goater      */
85456b11587SCédric Le Goater     close(xive->fd);
85556b11587SCédric Le Goater     xive->fd = -1;
85656b11587SCédric Le Goater 
85756b11587SCédric Le Goater     kvm_kernel_irqchip = false;
85856b11587SCédric Le Goater     kvm_msi_via_irqfd_allowed = false;
85956b11587SCédric Le Goater     kvm_gsi_direct_mapping = false;
86056b11587SCédric Le Goater 
86156b11587SCédric Le Goater     /* Clear the local list of presenter (hotplug) */
86256b11587SCédric Le Goater     kvm_cpu_disable_all();
86356b11587SCédric Le Goater 
86456b11587SCédric Le Goater     /* VM Change state handler is not needed anymore */
8651c3d4a8fSGreg Kurz     if (xive->change) {
86656b11587SCédric Le Goater         qemu_del_vm_change_state_handler(xive->change);
8671c3d4a8fSGreg Kurz         xive->change = NULL;
8681c3d4a8fSGreg Kurz     }
86956b11587SCédric Le Goater }
870