xref: /qemu/hw/intc/spapr_xive.c (revision 97c00c54449b4ff349f85c6ce409dadd1b935a7d)
13aa597f6SCédric Le Goater /*
23aa597f6SCédric Le Goater  * QEMU PowerPC sPAPR XIVE interrupt controller model
33aa597f6SCédric Le Goater  *
43aa597f6SCédric Le Goater  * Copyright (c) 2017-2018, IBM Corporation.
53aa597f6SCédric Le Goater  *
63aa597f6SCédric Le Goater  * This code is licensed under the GPL version 2 or later. See the
73aa597f6SCédric Le Goater  * COPYING file in the top-level directory.
83aa597f6SCédric Le Goater  */
93aa597f6SCédric Le Goater 
103aa597f6SCédric Le Goater #include "qemu/osdep.h"
113aa597f6SCédric Le Goater #include "qemu/log.h"
120b8fa32fSMarkus Armbruster #include "qemu/module.h"
133aa597f6SCédric Le Goater #include "qapi/error.h"
143aa597f6SCédric Le Goater #include "qemu/error-report.h"
153aa597f6SCédric Le Goater #include "target/ppc/cpu.h"
163aa597f6SCédric Le Goater #include "sysemu/cpus.h"
1771e8a915SMarkus Armbruster #include "sysemu/reset.h"
18d6454270SMarkus Armbruster #include "migration/vmstate.h"
193aa597f6SCédric Le Goater #include "monitor/monitor.h"
206e21de4aSCédric Le Goater #include "hw/ppc/fdt.h"
213aa597f6SCédric Le Goater #include "hw/ppc/spapr.h"
22a28b9a5aSCédric Le Goater #include "hw/ppc/spapr_cpu_core.h"
233aa597f6SCédric Le Goater #include "hw/ppc/spapr_xive.h"
243aa597f6SCédric Le Goater #include "hw/ppc/xive.h"
253aa597f6SCédric Le Goater #include "hw/ppc/xive_regs.h"
26a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
273aa597f6SCédric Le Goater 
283aa597f6SCédric Le Goater /*
293aa597f6SCédric Le Goater  * XIVE Virtualization Controller BAR and Thread Managment BAR that we
303aa597f6SCédric Le Goater  * use for the ESB pages and the TIMA pages
313aa597f6SCédric Le Goater  */
323aa597f6SCédric Le Goater #define SPAPR_XIVE_VC_BASE   0x0006010000000000ull
333aa597f6SCédric Le Goater #define SPAPR_XIVE_TM_BASE   0x0006030203180000ull
343aa597f6SCédric Le Goater 
353aa597f6SCédric Le Goater /*
360cddee8dSCédric Le Goater  * The allocation of VP blocks is a complex operation in OPAL and the
370cddee8dSCédric Le Goater  * VP identifiers have a relation with the number of HW chips, the
380cddee8dSCédric Le Goater  * size of the VP blocks, VP grouping, etc. The QEMU sPAPR XIVE
390cddee8dSCédric Le Goater  * controller model does not have the same constraints and can use a
400cddee8dSCédric Le Goater  * simple mapping scheme of the CPU vcpu_id
410cddee8dSCédric Le Goater  *
420cddee8dSCédric Le Goater  * These identifiers are never returned to the OS.
430cddee8dSCédric Le Goater  */
440cddee8dSCédric Le Goater 
450cddee8dSCédric Le Goater #define SPAPR_XIVE_NVT_BASE 0x400
460cddee8dSCédric Le Goater 
470cddee8dSCédric Le Goater /*
480cddee8dSCédric Le Goater  * sPAPR NVT and END indexing helpers
490cddee8dSCédric Le Goater  */
500cddee8dSCédric Le Goater static uint32_t spapr_xive_nvt_to_target(uint8_t nvt_blk, uint32_t nvt_idx)
510cddee8dSCédric Le Goater {
520cddee8dSCédric Le Goater     return nvt_idx - SPAPR_XIVE_NVT_BASE;
530cddee8dSCédric Le Goater }
540cddee8dSCédric Le Goater 
5523bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_nvt(PowerPCCPU *cpu,
5623bcd5ebSCédric Le Goater                                   uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
5723bcd5ebSCédric Le Goater {
5823bcd5ebSCédric Le Goater     assert(cpu);
5923bcd5ebSCédric Le Goater 
6023bcd5ebSCédric Le Goater     if (out_nvt_blk) {
6123bcd5ebSCédric Le Goater         *out_nvt_blk = SPAPR_XIVE_BLOCK_ID;
6223bcd5ebSCédric Le Goater     }
6323bcd5ebSCédric Le Goater 
6423bcd5ebSCédric Le Goater     if (out_nvt_blk) {
6523bcd5ebSCédric Le Goater         *out_nvt_idx = SPAPR_XIVE_NVT_BASE + cpu->vcpu_id;
6623bcd5ebSCédric Le Goater     }
6723bcd5ebSCédric Le Goater }
6823bcd5ebSCédric Le Goater 
6923bcd5ebSCédric Le Goater static int spapr_xive_target_to_nvt(uint32_t target,
7023bcd5ebSCédric Le Goater                                     uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
7123bcd5ebSCédric Le Goater {
7223bcd5ebSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(target);
7323bcd5ebSCédric Le Goater 
7423bcd5ebSCédric Le Goater     if (!cpu) {
7523bcd5ebSCédric Le Goater         return -1;
7623bcd5ebSCédric Le Goater     }
7723bcd5ebSCédric Le Goater 
7823bcd5ebSCédric Le Goater     spapr_xive_cpu_to_nvt(cpu, out_nvt_blk, out_nvt_idx);
7923bcd5ebSCédric Le Goater     return 0;
8023bcd5ebSCédric Le Goater }
8123bcd5ebSCédric Le Goater 
8223bcd5ebSCédric Le Goater /*
8323bcd5ebSCédric Le Goater  * sPAPR END indexing uses a simple mapping of the CPU vcpu_id, 8
8423bcd5ebSCédric Le Goater  * priorities per CPU
8523bcd5ebSCédric Le Goater  */
860c575703SCédric Le Goater int spapr_xive_end_to_target(uint8_t end_blk, uint32_t end_idx,
870c575703SCédric Le Goater                              uint32_t *out_server, uint8_t *out_prio)
880c575703SCédric Le Goater {
890c575703SCédric Le Goater 
900c575703SCédric Le Goater     assert(end_blk == SPAPR_XIVE_BLOCK_ID);
910c575703SCédric Le Goater 
920c575703SCédric Le Goater     if (out_server) {
930c575703SCédric Le Goater         *out_server = end_idx >> 3;
940c575703SCédric Le Goater     }
950c575703SCédric Le Goater 
960c575703SCédric Le Goater     if (out_prio) {
970c575703SCédric Le Goater         *out_prio = end_idx & 0x7;
980c575703SCédric Le Goater     }
990c575703SCédric Le Goater     return 0;
1000c575703SCédric Le Goater }
1010c575703SCédric Le Goater 
10223bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_end(PowerPCCPU *cpu, uint8_t prio,
10323bcd5ebSCédric Le Goater                                   uint8_t *out_end_blk, uint32_t *out_end_idx)
10423bcd5ebSCédric Le Goater {
10523bcd5ebSCédric Le Goater     assert(cpu);
10623bcd5ebSCédric Le Goater 
10723bcd5ebSCédric Le Goater     if (out_end_blk) {
10823bcd5ebSCédric Le Goater         *out_end_blk = SPAPR_XIVE_BLOCK_ID;
10923bcd5ebSCédric Le Goater     }
11023bcd5ebSCédric Le Goater 
11123bcd5ebSCédric Le Goater     if (out_end_idx) {
11223bcd5ebSCédric Le Goater         *out_end_idx = (cpu->vcpu_id << 3) + prio;
11323bcd5ebSCédric Le Goater     }
11423bcd5ebSCédric Le Goater }
11523bcd5ebSCédric Le Goater 
11623bcd5ebSCédric Le Goater static int spapr_xive_target_to_end(uint32_t target, uint8_t prio,
11723bcd5ebSCédric Le Goater                                     uint8_t *out_end_blk, uint32_t *out_end_idx)
11823bcd5ebSCédric Le Goater {
11923bcd5ebSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(target);
12023bcd5ebSCédric Le Goater 
12123bcd5ebSCédric Le Goater     if (!cpu) {
12223bcd5ebSCédric Le Goater         return -1;
12323bcd5ebSCédric Le Goater     }
12423bcd5ebSCédric Le Goater 
12523bcd5ebSCédric Le Goater     spapr_xive_cpu_to_end(cpu, prio, out_end_blk, out_end_idx);
12623bcd5ebSCédric Le Goater     return 0;
12723bcd5ebSCédric Le Goater }
12823bcd5ebSCédric Le Goater 
1290cddee8dSCédric Le Goater /*
1303aa597f6SCédric Le Goater  * On sPAPR machines, use a simplified output for the XIVE END
1313aa597f6SCédric Le Goater  * structure dumping only the information related to the OS EQ.
1323aa597f6SCédric Le Goater  */
133ce2918cbSDavid Gibson static void spapr_xive_end_pic_print_info(SpaprXive *xive, XiveEND *end,
1343aa597f6SCédric Le Goater                                           Monitor *mon)
1353aa597f6SCédric Le Goater {
136fb2e8b51SCédric Le Goater     uint64_t qaddr_base = xive_end_qaddr(end);
1373aa597f6SCédric Le Goater     uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
1383aa597f6SCédric Le Goater     uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
1393aa597f6SCédric Le Goater     uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
1403aa597f6SCédric Le Goater     uint32_t qentries = 1 << (qsize + 10);
1413aa597f6SCédric Le Goater     uint32_t nvt = xive_get_field32(END_W6_NVT_INDEX, end->w6);
1423aa597f6SCédric Le Goater     uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
1433aa597f6SCédric Le Goater 
144fb2e8b51SCédric Le Goater     monitor_printf(mon, "%3d/%d % 6d/%5d @%"PRIx64" ^%d",
1450cddee8dSCédric Le Goater                    spapr_xive_nvt_to_target(0, nvt),
146fb2e8b51SCédric Le Goater                    priority, qindex, qentries, qaddr_base, qgen);
1473aa597f6SCédric Le Goater 
1483aa597f6SCédric Le Goater     xive_end_queue_pic_print_info(end, 6, mon);
1493aa597f6SCédric Le Goater }
1503aa597f6SCédric Le Goater 
151ce2918cbSDavid Gibson void spapr_xive_pic_print_info(SpaprXive *xive, Monitor *mon)
1523aa597f6SCédric Le Goater {
1533aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
1543aa597f6SCédric Le Goater     int i;
1553aa597f6SCédric Le Goater 
1567bfc759cSCédric Le Goater     if (kvm_irqchip_in_kernel()) {
1577bfc759cSCédric Le Goater         Error *local_err = NULL;
1587bfc759cSCédric Le Goater 
1597bfc759cSCédric Le Goater         kvmppc_xive_synchronize_state(xive, &local_err);
1607bfc759cSCédric Le Goater         if (local_err) {
1617bfc759cSCédric Le Goater             error_report_err(local_err);
1627bfc759cSCédric Le Goater             return;
1637bfc759cSCédric Le Goater         }
1647bfc759cSCédric Le Goater     }
1657bfc759cSCédric Le Goater 
166f81d69fcSSatheesh Rajendran     monitor_printf(mon, "  LISN         PQ    EISN     CPU/PRIO EQ\n");
1673aa597f6SCédric Le Goater 
1683aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
1693aa597f6SCédric Le Goater         uint8_t pq = xive_source_esb_get(xsrc, i);
1703aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
1713aa597f6SCédric Le Goater 
1723aa597f6SCédric Le Goater         if (!xive_eas_is_valid(eas)) {
1733aa597f6SCédric Le Goater             continue;
1743aa597f6SCédric Le Goater         }
1753aa597f6SCédric Le Goater 
1763aa597f6SCédric Le Goater         monitor_printf(mon, "  %08x %s %c%c%c %s %08x ", i,
1773aa597f6SCédric Le Goater                        xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
1783aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
1793aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
1803aa597f6SCédric Le Goater                        xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ',
1813aa597f6SCédric Le Goater                        xive_eas_is_masked(eas) ? "M" : " ",
1823aa597f6SCédric Le Goater                        (int) xive_get_field64(EAS_END_DATA, eas->w));
1833aa597f6SCédric Le Goater 
1843aa597f6SCédric Le Goater         if (!xive_eas_is_masked(eas)) {
1853aa597f6SCédric Le Goater             uint32_t end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
1863aa597f6SCédric Le Goater             XiveEND *end;
1873aa597f6SCédric Le Goater 
1883aa597f6SCédric Le Goater             assert(end_idx < xive->nr_ends);
1893aa597f6SCédric Le Goater             end = &xive->endt[end_idx];
1903aa597f6SCédric Le Goater 
1913aa597f6SCédric Le Goater             if (xive_end_is_valid(end)) {
1923aa597f6SCédric Le Goater                 spapr_xive_end_pic_print_info(xive, end, mon);
1933aa597f6SCédric Le Goater             }
1943aa597f6SCédric Le Goater         }
1953aa597f6SCédric Le Goater         monitor_printf(mon, "\n");
1963aa597f6SCédric Le Goater     }
1973aa597f6SCédric Le Goater }
1983aa597f6SCédric Le Goater 
199ce2918cbSDavid Gibson void spapr_xive_mmio_set_enabled(SpaprXive *xive, bool enable)
2003a8eb78eSCédric Le Goater {
2013a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->source.esb_mmio, enable);
2023a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->tm_mmio, enable);
2033a8eb78eSCédric Le Goater 
2043a8eb78eSCédric Le Goater     /* Disable the END ESBs until a guest OS makes use of them */
2053a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->end_source.esb_mmio, false);
2063a8eb78eSCédric Le Goater }
2073a8eb78eSCédric Le Goater 
2083aa597f6SCédric Le Goater static void spapr_xive_end_reset(XiveEND *end)
2093aa597f6SCédric Le Goater {
2103aa597f6SCédric Le Goater     memset(end, 0, sizeof(*end));
2113aa597f6SCédric Le Goater 
2123aa597f6SCédric Le Goater     /* switch off the escalation and notification ESBs */
2133aa597f6SCédric Le Goater     end->w1 = cpu_to_be32(END_W1_ESe_Q | END_W1_ESn_Q);
2143aa597f6SCédric Le Goater }
2153aa597f6SCédric Le Goater 
2163aa597f6SCédric Le Goater static void spapr_xive_reset(void *dev)
2173aa597f6SCédric Le Goater {
218ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(dev);
2193aa597f6SCédric Le Goater     int i;
2203aa597f6SCédric Le Goater 
2213aa597f6SCédric Le Goater     /*
2223aa597f6SCédric Le Goater      * The XiveSource has its own reset handler, which mask off all
2233aa597f6SCédric Le Goater      * IRQs (!P|Q)
2243aa597f6SCédric Le Goater      */
2253aa597f6SCédric Le Goater 
2263aa597f6SCédric Le Goater     /* Mask all valid EASs in the IRQ number space. */
2273aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
2283aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
2293aa597f6SCédric Le Goater         if (xive_eas_is_valid(eas)) {
2303aa597f6SCédric Le Goater             eas->w = cpu_to_be64(EAS_VALID | EAS_MASKED);
2313aa597f6SCédric Le Goater         } else {
2323aa597f6SCédric Le Goater             eas->w = 0;
2333aa597f6SCédric Le Goater         }
2343aa597f6SCédric Le Goater     }
2353aa597f6SCédric Le Goater 
2363aa597f6SCédric Le Goater     /* Clear all ENDs */
2373aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_ends; i++) {
2383aa597f6SCédric Le Goater         spapr_xive_end_reset(&xive->endt[i]);
2393aa597f6SCédric Le Goater     }
2403aa597f6SCédric Le Goater }
2413aa597f6SCédric Le Goater 
2423aa597f6SCédric Le Goater static void spapr_xive_instance_init(Object *obj)
2433aa597f6SCédric Le Goater {
244ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(obj);
2453aa597f6SCédric Le Goater 
246f6d4dca8SThomas Huth     object_initialize_child(obj, "source", &xive->source, sizeof(xive->source),
247f6d4dca8SThomas Huth                             TYPE_XIVE_SOURCE, &error_abort, NULL);
2483aa597f6SCédric Le Goater 
249f6d4dca8SThomas Huth     object_initialize_child(obj, "end_source", &xive->end_source,
250f6d4dca8SThomas Huth                             sizeof(xive->end_source), TYPE_XIVE_END_SOURCE,
251f6d4dca8SThomas Huth                             &error_abort, NULL);
25238afd772SCédric Le Goater 
25338afd772SCédric Le Goater     /* Not connected to the KVM XIVE device */
25438afd772SCédric Le Goater     xive->fd = -1;
2553aa597f6SCédric Le Goater }
2563aa597f6SCédric Le Goater 
2573aa597f6SCédric Le Goater static void spapr_xive_realize(DeviceState *dev, Error **errp)
2583aa597f6SCédric Le Goater {
259ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(dev);
2603aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
2613aa597f6SCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
2623aa597f6SCédric Le Goater     Error *local_err = NULL;
2633aa597f6SCédric Le Goater 
2643aa597f6SCédric Le Goater     if (!xive->nr_irqs) {
2653aa597f6SCédric Le Goater         error_setg(errp, "Number of interrupt needs to be greater 0");
2663aa597f6SCédric Le Goater         return;
2673aa597f6SCédric Le Goater     }
2683aa597f6SCédric Le Goater 
2693aa597f6SCédric Le Goater     if (!xive->nr_ends) {
2703aa597f6SCédric Le Goater         error_setg(errp, "Number of interrupt needs to be greater 0");
2713aa597f6SCédric Le Goater         return;
2723aa597f6SCédric Le Goater     }
2733aa597f6SCédric Le Goater 
2743aa597f6SCédric Le Goater     /*
2753aa597f6SCédric Le Goater      * Initialize the internal sources, for IPIs and virtual devices.
2763aa597f6SCédric Le Goater      */
2773aa597f6SCédric Le Goater     object_property_set_int(OBJECT(xsrc), xive->nr_irqs, "nr-irqs",
2783aa597f6SCédric Le Goater                             &error_fatal);
2793aa597f6SCédric Le Goater     object_property_add_const_link(OBJECT(xsrc), "xive", OBJECT(xive),
2803aa597f6SCédric Le Goater                                    &error_fatal);
2813aa597f6SCédric Le Goater     object_property_set_bool(OBJECT(xsrc), true, "realized", &local_err);
2823aa597f6SCédric Le Goater     if (local_err) {
2833aa597f6SCédric Le Goater         error_propagate(errp, local_err);
2843aa597f6SCédric Le Goater         return;
2853aa597f6SCédric Le Goater     }
286981b1c62SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xsrc->esb_mmio);
2873aa597f6SCédric Le Goater 
2883aa597f6SCédric Le Goater     /*
2893aa597f6SCédric Le Goater      * Initialize the END ESB source
2903aa597f6SCédric Le Goater      */
2913aa597f6SCédric Le Goater     object_property_set_int(OBJECT(end_xsrc), xive->nr_irqs, "nr-ends",
2923aa597f6SCédric Le Goater                             &error_fatal);
2933aa597f6SCédric Le Goater     object_property_add_const_link(OBJECT(end_xsrc), "xive", OBJECT(xive),
2943aa597f6SCédric Le Goater                                    &error_fatal);
2953aa597f6SCédric Le Goater     object_property_set_bool(OBJECT(end_xsrc), true, "realized", &local_err);
2963aa597f6SCédric Le Goater     if (local_err) {
2973aa597f6SCédric Le Goater         error_propagate(errp, local_err);
2983aa597f6SCédric Le Goater         return;
2993aa597f6SCédric Le Goater     }
300981b1c62SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &end_xsrc->esb_mmio);
3013aa597f6SCédric Le Goater 
3023aa597f6SCédric Le Goater     /* Set the mapping address of the END ESB pages after the source ESBs */
3033aa597f6SCédric Le Goater     xive->end_base = xive->vc_base + (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
3043aa597f6SCédric Le Goater 
3053aa597f6SCédric Le Goater     /*
3063aa597f6SCédric Le Goater      * Allocate the routing tables
3073aa597f6SCédric Le Goater      */
3083aa597f6SCédric Le Goater     xive->eat = g_new0(XiveEAS, xive->nr_irqs);
3093aa597f6SCédric Le Goater     xive->endt = g_new0(XiveEND, xive->nr_ends);
3103aa597f6SCédric Le Goater 
31138afd772SCédric Le Goater     xive->nodename = g_strdup_printf("interrupt-controller@%" PRIx64,
31238afd772SCédric Le Goater                            xive->tm_base + XIVE_TM_USER_PAGE * (1 << TM_SHIFT));
31338afd772SCédric Le Goater 
31438afd772SCédric Le Goater     qemu_register_reset(spapr_xive_reset, dev);
315cdd71c8eSCédric Le Goater 
3163aa597f6SCédric Le Goater     /* TIMA initialization */
3173aa597f6SCédric Le Goater     memory_region_init_io(&xive->tm_mmio, OBJECT(xive), &xive_tm_ops, xive,
3183aa597f6SCédric Le Goater                           "xive.tima", 4ull << TM_SHIFT);
319981b1c62SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xive->tm_mmio);
3203aa597f6SCédric Le Goater 
321981b1c62SCédric Le Goater     /*
322981b1c62SCédric Le Goater      * Map all regions. These will be enabled or disabled at reset and
323981b1c62SCédric Le Goater      * can also be overridden by KVM memory regions if active
324981b1c62SCédric Le Goater      */
325981b1c62SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->vc_base);
326981b1c62SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1, xive->end_base);
327981b1c62SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 2, xive->tm_base);
3283aa597f6SCédric Le Goater }
3293aa597f6SCédric Le Goater 
3303aa597f6SCédric Le Goater static int spapr_xive_get_eas(XiveRouter *xrtr, uint8_t eas_blk,
3313aa597f6SCédric Le Goater                               uint32_t eas_idx, XiveEAS *eas)
3323aa597f6SCédric Le Goater {
333ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3343aa597f6SCédric Le Goater 
3353aa597f6SCédric Le Goater     if (eas_idx >= xive->nr_irqs) {
3363aa597f6SCédric Le Goater         return -1;
3373aa597f6SCédric Le Goater     }
3383aa597f6SCédric Le Goater 
3393aa597f6SCédric Le Goater     *eas = xive->eat[eas_idx];
3403aa597f6SCédric Le Goater     return 0;
3413aa597f6SCédric Le Goater }
3423aa597f6SCédric Le Goater 
3433aa597f6SCédric Le Goater static int spapr_xive_get_end(XiveRouter *xrtr,
3443aa597f6SCédric Le Goater                               uint8_t end_blk, uint32_t end_idx, XiveEND *end)
3453aa597f6SCédric Le Goater {
346ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3473aa597f6SCédric Le Goater 
3483aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
3493aa597f6SCédric Le Goater         return -1;
3503aa597f6SCédric Le Goater     }
3513aa597f6SCédric Le Goater 
3523aa597f6SCédric Le Goater     memcpy(end, &xive->endt[end_idx], sizeof(XiveEND));
3533aa597f6SCédric Le Goater     return 0;
3543aa597f6SCédric Le Goater }
3553aa597f6SCédric Le Goater 
3563aa597f6SCédric Le Goater static int spapr_xive_write_end(XiveRouter *xrtr, uint8_t end_blk,
3573aa597f6SCédric Le Goater                                 uint32_t end_idx, XiveEND *end,
3583aa597f6SCédric Le Goater                                 uint8_t word_number)
3593aa597f6SCédric Le Goater {
360ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3613aa597f6SCédric Le Goater 
3623aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
3633aa597f6SCédric Le Goater         return -1;
3643aa597f6SCédric Le Goater     }
3653aa597f6SCédric Le Goater 
3663aa597f6SCédric Le Goater     memcpy(&xive->endt[end_idx], end, sizeof(XiveEND));
3673aa597f6SCédric Le Goater     return 0;
3683aa597f6SCédric Le Goater }
3693aa597f6SCédric Le Goater 
3700cddee8dSCédric Le Goater static int spapr_xive_get_nvt(XiveRouter *xrtr,
3710cddee8dSCédric Le Goater                               uint8_t nvt_blk, uint32_t nvt_idx, XiveNVT *nvt)
3720cddee8dSCédric Le Goater {
3730cddee8dSCédric Le Goater     uint32_t vcpu_id = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
3740cddee8dSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
3750cddee8dSCédric Le Goater 
3760cddee8dSCédric Le Goater     if (!cpu) {
3770cddee8dSCédric Le Goater         /* TODO: should we assert() if we can find a NVT ? */
3780cddee8dSCédric Le Goater         return -1;
3790cddee8dSCédric Le Goater     }
3800cddee8dSCédric Le Goater 
3810cddee8dSCédric Le Goater     /*
3820cddee8dSCédric Le Goater      * sPAPR does not maintain a NVT table. Return that the NVT is
3830cddee8dSCédric Le Goater      * valid if we have found a matching CPU
3840cddee8dSCédric Le Goater      */
3850cddee8dSCédric Le Goater     nvt->w0 = cpu_to_be32(NVT_W0_VALID);
3860cddee8dSCédric Le Goater     return 0;
3870cddee8dSCédric Le Goater }
3880cddee8dSCédric Le Goater 
3890cddee8dSCédric Le Goater static int spapr_xive_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk,
3900cddee8dSCédric Le Goater                                 uint32_t nvt_idx, XiveNVT *nvt,
3910cddee8dSCédric Le Goater                                 uint8_t word_number)
3920cddee8dSCédric Le Goater {
3930cddee8dSCédric Le Goater     /*
3940cddee8dSCédric Le Goater      * We don't need to write back to the NVTs because the sPAPR
3950cddee8dSCédric Le Goater      * machine should never hit a non-scheduled NVT. It should never
3960cddee8dSCédric Le Goater      * get called.
3970cddee8dSCédric Le Goater      */
3980cddee8dSCédric Le Goater     g_assert_not_reached();
3990cddee8dSCédric Le Goater }
4000cddee8dSCédric Le Goater 
40140a5056cSCédric Le Goater static XiveTCTX *spapr_xive_get_tctx(XiveRouter *xrtr, CPUState *cs)
40240a5056cSCédric Le Goater {
40340a5056cSCédric Le Goater     PowerPCCPU *cpu = POWERPC_CPU(cs);
40440a5056cSCédric Le Goater 
405a28b9a5aSCédric Le Goater     return spapr_cpu_state(cpu)->tctx;
40640a5056cSCédric Le Goater }
40740a5056cSCédric Le Goater 
4083aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_end = {
4093aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/end",
4103aa597f6SCédric Le Goater     .version_id = 1,
4113aa597f6SCédric Le Goater     .minimum_version_id = 1,
4123aa597f6SCédric Le Goater     .fields = (VMStateField []) {
4133aa597f6SCédric Le Goater         VMSTATE_UINT32(w0, XiveEND),
4143aa597f6SCédric Le Goater         VMSTATE_UINT32(w1, XiveEND),
4153aa597f6SCédric Le Goater         VMSTATE_UINT32(w2, XiveEND),
4163aa597f6SCédric Le Goater         VMSTATE_UINT32(w3, XiveEND),
4173aa597f6SCédric Le Goater         VMSTATE_UINT32(w4, XiveEND),
4183aa597f6SCédric Le Goater         VMSTATE_UINT32(w5, XiveEND),
4193aa597f6SCédric Le Goater         VMSTATE_UINT32(w6, XiveEND),
4203aa597f6SCédric Le Goater         VMSTATE_UINT32(w7, XiveEND),
4213aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
4223aa597f6SCédric Le Goater     },
4233aa597f6SCédric Le Goater };
4243aa597f6SCédric Le Goater 
4253aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_eas = {
4263aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/eas",
4273aa597f6SCédric Le Goater     .version_id = 1,
4283aa597f6SCédric Le Goater     .minimum_version_id = 1,
4293aa597f6SCédric Le Goater     .fields = (VMStateField []) {
4303aa597f6SCédric Le Goater         VMSTATE_UINT64(w, XiveEAS),
4313aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
4323aa597f6SCédric Le Goater     },
4333aa597f6SCédric Le Goater };
4343aa597f6SCédric Le Goater 
435277dd3d7SCédric Le Goater static int vmstate_spapr_xive_pre_save(void *opaque)
436277dd3d7SCédric Le Goater {
437277dd3d7SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
438277dd3d7SCédric Le Goater         return kvmppc_xive_pre_save(SPAPR_XIVE(opaque));
439277dd3d7SCédric Le Goater     }
440277dd3d7SCédric Le Goater 
441277dd3d7SCédric Le Goater     return 0;
442277dd3d7SCédric Le Goater }
443277dd3d7SCédric Le Goater 
444277dd3d7SCédric Le Goater /*
445277dd3d7SCédric Le Goater  * Called by the sPAPR IRQ backend 'post_load' method at the machine
446277dd3d7SCédric Le Goater  * level.
447277dd3d7SCédric Le Goater  */
448605994e5SDavid Gibson static int spapr_xive_post_load(SpaprInterruptController *intc, int version_id)
449277dd3d7SCédric Le Goater {
450277dd3d7SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
451605994e5SDavid Gibson         return kvmppc_xive_post_load(SPAPR_XIVE(intc), version_id);
452277dd3d7SCédric Le Goater     }
453277dd3d7SCédric Le Goater 
454277dd3d7SCédric Le Goater     return 0;
455277dd3d7SCédric Le Goater }
456277dd3d7SCédric Le Goater 
4573aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive = {
4583aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
4593aa597f6SCédric Le Goater     .version_id = 1,
4603aa597f6SCédric Le Goater     .minimum_version_id = 1,
461277dd3d7SCédric Le Goater     .pre_save = vmstate_spapr_xive_pre_save,
462277dd3d7SCédric Le Goater     .post_load = NULL, /* handled at the machine level */
4633aa597f6SCédric Le Goater     .fields = (VMStateField[]) {
464ce2918cbSDavid Gibson         VMSTATE_UINT32_EQUAL(nr_irqs, SpaprXive, NULL),
465ce2918cbSDavid Gibson         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(eat, SpaprXive, nr_irqs,
4663aa597f6SCédric Le Goater                                      vmstate_spapr_xive_eas, XiveEAS),
467ce2918cbSDavid Gibson         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(endt, SpaprXive, nr_ends,
4683aa597f6SCédric Le Goater                                              vmstate_spapr_xive_end, XiveEND),
4693aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
4703aa597f6SCédric Le Goater     },
4713aa597f6SCédric Le Goater };
4723aa597f6SCédric Le Goater 
4730b0e52b1SDavid Gibson static int spapr_xive_claim_irq(SpaprInterruptController *intc, int lisn,
4740b0e52b1SDavid Gibson                                 bool lsi, Error **errp)
4750b0e52b1SDavid Gibson {
4760b0e52b1SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
4770b0e52b1SDavid Gibson     XiveSource *xsrc = &xive->source;
4780b0e52b1SDavid Gibson 
4790b0e52b1SDavid Gibson     assert(lisn < xive->nr_irqs);
4800b0e52b1SDavid Gibson 
4810b0e52b1SDavid Gibson     if (xive_eas_is_valid(&xive->eat[lisn])) {
4820b0e52b1SDavid Gibson         error_setg(errp, "IRQ %d is not free", lisn);
4830b0e52b1SDavid Gibson         return -EBUSY;
4840b0e52b1SDavid Gibson     }
4850b0e52b1SDavid Gibson 
4860b0e52b1SDavid Gibson     /*
4870b0e52b1SDavid Gibson      * Set default values when allocating an IRQ number
4880b0e52b1SDavid Gibson      */
4890b0e52b1SDavid Gibson     xive->eat[lisn].w |= cpu_to_be64(EAS_VALID | EAS_MASKED);
4900b0e52b1SDavid Gibson     if (lsi) {
4910b0e52b1SDavid Gibson         xive_source_irq_set_lsi(xsrc, lisn);
4920b0e52b1SDavid Gibson     }
4930b0e52b1SDavid Gibson 
4940b0e52b1SDavid Gibson     if (kvm_irqchip_in_kernel()) {
4950b0e52b1SDavid Gibson         return kvmppc_xive_source_reset_one(xsrc, lisn, errp);
4960b0e52b1SDavid Gibson     }
4970b0e52b1SDavid Gibson 
4980b0e52b1SDavid Gibson     return 0;
4990b0e52b1SDavid Gibson }
5000b0e52b1SDavid Gibson 
5010b0e52b1SDavid Gibson static void spapr_xive_free_irq(SpaprInterruptController *intc, int lisn)
5020b0e52b1SDavid Gibson {
5030b0e52b1SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
5040b0e52b1SDavid Gibson     assert(lisn < xive->nr_irqs);
5050b0e52b1SDavid Gibson 
5060b0e52b1SDavid Gibson     xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
5070b0e52b1SDavid Gibson }
5080b0e52b1SDavid Gibson 
5093aa597f6SCédric Le Goater static Property spapr_xive_properties[] = {
510ce2918cbSDavid Gibson     DEFINE_PROP_UINT32("nr-irqs", SpaprXive, nr_irqs, 0),
511ce2918cbSDavid Gibson     DEFINE_PROP_UINT32("nr-ends", SpaprXive, nr_ends, 0),
512ce2918cbSDavid Gibson     DEFINE_PROP_UINT64("vc-base", SpaprXive, vc_base, SPAPR_XIVE_VC_BASE),
513ce2918cbSDavid Gibson     DEFINE_PROP_UINT64("tm-base", SpaprXive, tm_base, SPAPR_XIVE_TM_BASE),
5143aa597f6SCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
5153aa597f6SCédric Le Goater };
5163aa597f6SCédric Le Goater 
517ebd6be08SDavid Gibson static int spapr_xive_cpu_intc_create(SpaprInterruptController *intc,
518ebd6be08SDavid Gibson                                       PowerPCCPU *cpu, Error **errp)
519ebd6be08SDavid Gibson {
520ebd6be08SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
521ebd6be08SDavid Gibson     Object *obj;
522ebd6be08SDavid Gibson     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
523ebd6be08SDavid Gibson 
524ebd6be08SDavid Gibson     obj = xive_tctx_create(OBJECT(cpu), XIVE_ROUTER(xive), errp);
525ebd6be08SDavid Gibson     if (!obj) {
526ebd6be08SDavid Gibson         return -1;
527ebd6be08SDavid Gibson     }
528ebd6be08SDavid Gibson 
529ebd6be08SDavid Gibson     spapr_cpu->tctx = XIVE_TCTX(obj);
530ebd6be08SDavid Gibson     return 0;
531ebd6be08SDavid Gibson }
532ebd6be08SDavid Gibson 
533*97c00c54SCédric Le Goater static void xive_tctx_set_os_cam(XiveTCTX *tctx, uint32_t os_cam)
534*97c00c54SCédric Le Goater {
535*97c00c54SCédric Le Goater     uint32_t qw1w2 = cpu_to_be32(TM_QW1W2_VO | os_cam);
536*97c00c54SCédric Le Goater     memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1w2, 4);
537*97c00c54SCédric Le Goater }
538*97c00c54SCédric Le Goater 
539d49e8a9bSCédric Le Goater static void spapr_xive_cpu_intc_reset(SpaprInterruptController *intc,
540d49e8a9bSCédric Le Goater                                      PowerPCCPU *cpu)
541d49e8a9bSCédric Le Goater {
542d49e8a9bSCédric Le Goater     XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx;
543*97c00c54SCédric Le Goater     uint8_t  nvt_blk;
544*97c00c54SCédric Le Goater     uint32_t nvt_idx;
545d49e8a9bSCédric Le Goater 
546d49e8a9bSCédric Le Goater     xive_tctx_reset(tctx);
547*97c00c54SCédric Le Goater 
548*97c00c54SCédric Le Goater     /*
549*97c00c54SCédric Le Goater      * When a Virtual Processor is scheduled to run on a HW thread,
550*97c00c54SCédric Le Goater      * the hypervisor pushes its identifier in the OS CAM line.
551*97c00c54SCédric Le Goater      * Emulate the same behavior under QEMU.
552*97c00c54SCédric Le Goater      */
553*97c00c54SCédric Le Goater     spapr_xive_cpu_to_nvt(cpu, &nvt_blk, &nvt_idx);
554*97c00c54SCédric Le Goater 
555*97c00c54SCédric Le Goater     xive_tctx_set_os_cam(tctx, xive_nvt_cam_line(nvt_blk, nvt_idx));
556d49e8a9bSCédric Le Goater }
557d49e8a9bSCédric Le Goater 
5587bcdbccaSDavid Gibson static void spapr_xive_set_irq(SpaprInterruptController *intc, int irq, int val)
5597bcdbccaSDavid Gibson {
5607bcdbccaSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
5617bcdbccaSDavid Gibson 
5627bcdbccaSDavid Gibson     if (kvm_irqchip_in_kernel()) {
5637bcdbccaSDavid Gibson         kvmppc_xive_source_set_irq(&xive->source, irq, val);
5647bcdbccaSDavid Gibson     } else {
5657bcdbccaSDavid Gibson         xive_source_set_irq(&xive->source, irq, val);
5667bcdbccaSDavid Gibson     }
5677bcdbccaSDavid Gibson }
5687bcdbccaSDavid Gibson 
569328d8eb2SDavid Gibson static void spapr_xive_print_info(SpaprInterruptController *intc, Monitor *mon)
570328d8eb2SDavid Gibson {
571328d8eb2SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
572328d8eb2SDavid Gibson     CPUState *cs;
573328d8eb2SDavid Gibson 
574328d8eb2SDavid Gibson     CPU_FOREACH(cs) {
575328d8eb2SDavid Gibson         PowerPCCPU *cpu = POWERPC_CPU(cs);
576328d8eb2SDavid Gibson 
577328d8eb2SDavid Gibson         xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, mon);
578328d8eb2SDavid Gibson     }
579328d8eb2SDavid Gibson 
580328d8eb2SDavid Gibson     spapr_xive_pic_print_info(xive, mon);
581328d8eb2SDavid Gibson }
582328d8eb2SDavid Gibson 
58305289273SDavid Gibson static void spapr_xive_dt(SpaprInterruptController *intc, uint32_t nr_servers,
58405289273SDavid Gibson                           void *fdt, uint32_t phandle)
58505289273SDavid Gibson {
58605289273SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
58705289273SDavid Gibson     int node;
58805289273SDavid Gibson     uint64_t timas[2 * 2];
58905289273SDavid Gibson     /* Interrupt number ranges for the IPIs */
59005289273SDavid Gibson     uint32_t lisn_ranges[] = {
59105289273SDavid Gibson         cpu_to_be32(0),
59205289273SDavid Gibson         cpu_to_be32(nr_servers),
59305289273SDavid Gibson     };
59405289273SDavid Gibson     /*
59505289273SDavid Gibson      * EQ size - the sizes of pages supported by the system 4K, 64K,
59605289273SDavid Gibson      * 2M, 16M. We only advertise 64K for the moment.
59705289273SDavid Gibson      */
59805289273SDavid Gibson     uint32_t eq_sizes[] = {
59905289273SDavid Gibson         cpu_to_be32(16), /* 64K */
60005289273SDavid Gibson     };
60105289273SDavid Gibson     /*
60205289273SDavid Gibson      * The following array is in sync with the reserved priorities
60305289273SDavid Gibson      * defined by the 'spapr_xive_priority_is_reserved' routine.
60405289273SDavid Gibson      */
60505289273SDavid Gibson     uint32_t plat_res_int_priorities[] = {
60605289273SDavid Gibson         cpu_to_be32(7),    /* start */
60705289273SDavid Gibson         cpu_to_be32(0xf8), /* count */
60805289273SDavid Gibson     };
60905289273SDavid Gibson 
61005289273SDavid Gibson     /* Thread Interrupt Management Area : User (ring 3) and OS (ring 2) */
61105289273SDavid Gibson     timas[0] = cpu_to_be64(xive->tm_base +
61205289273SDavid Gibson                            XIVE_TM_USER_PAGE * (1ull << TM_SHIFT));
61305289273SDavid Gibson     timas[1] = cpu_to_be64(1ull << TM_SHIFT);
61405289273SDavid Gibson     timas[2] = cpu_to_be64(xive->tm_base +
61505289273SDavid Gibson                            XIVE_TM_OS_PAGE * (1ull << TM_SHIFT));
61605289273SDavid Gibson     timas[3] = cpu_to_be64(1ull << TM_SHIFT);
61705289273SDavid Gibson 
61805289273SDavid Gibson     _FDT(node = fdt_add_subnode(fdt, 0, xive->nodename));
61905289273SDavid Gibson 
62005289273SDavid Gibson     _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
62105289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
62205289273SDavid Gibson 
62305289273SDavid Gibson     _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
62405289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
62505289273SDavid Gibson                      sizeof(eq_sizes)));
62605289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
62705289273SDavid Gibson                      sizeof(lisn_ranges)));
62805289273SDavid Gibson 
62905289273SDavid Gibson     /* For Linux to link the LSIs to the interrupt controller. */
63005289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "interrupt-controller", NULL, 0));
63105289273SDavid Gibson     _FDT(fdt_setprop_cell(fdt, node, "#interrupt-cells", 2));
63205289273SDavid Gibson 
63305289273SDavid Gibson     /* For SLOF */
63405289273SDavid Gibson     _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
63505289273SDavid Gibson     _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
63605289273SDavid Gibson 
63705289273SDavid Gibson     /*
63805289273SDavid Gibson      * The "ibm,plat-res-int-priorities" property defines the priority
63905289273SDavid Gibson      * ranges reserved by the hypervisor
64005289273SDavid Gibson      */
64105289273SDavid Gibson     _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
64205289273SDavid Gibson                      plat_res_int_priorities, sizeof(plat_res_int_priorities)));
64305289273SDavid Gibson }
64405289273SDavid Gibson 
645567192d4SDavid Gibson static int spapr_xive_activate(SpaprInterruptController *intc, Error **errp)
646567192d4SDavid Gibson {
647567192d4SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
648567192d4SDavid Gibson 
649567192d4SDavid Gibson     if (kvm_enabled()) {
650567192d4SDavid Gibson         int rc = spapr_irq_init_kvm(kvmppc_xive_connect, intc, errp);
651567192d4SDavid Gibson         if (rc < 0) {
652567192d4SDavid Gibson             return rc;
653567192d4SDavid Gibson         }
654567192d4SDavid Gibson     }
655567192d4SDavid Gibson 
656567192d4SDavid Gibson     /* Activate the XIVE MMIOs */
657567192d4SDavid Gibson     spapr_xive_mmio_set_enabled(xive, true);
658567192d4SDavid Gibson 
659567192d4SDavid Gibson     return 0;
660567192d4SDavid Gibson }
661567192d4SDavid Gibson 
662567192d4SDavid Gibson static void spapr_xive_deactivate(SpaprInterruptController *intc)
663567192d4SDavid Gibson {
664567192d4SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
665567192d4SDavid Gibson 
666567192d4SDavid Gibson     spapr_xive_mmio_set_enabled(xive, false);
667567192d4SDavid Gibson 
668567192d4SDavid Gibson     if (kvm_irqchip_in_kernel()) {
669567192d4SDavid Gibson         kvmppc_xive_disconnect(intc);
670567192d4SDavid Gibson     }
671567192d4SDavid Gibson }
672567192d4SDavid Gibson 
6733aa597f6SCédric Le Goater static void spapr_xive_class_init(ObjectClass *klass, void *data)
6743aa597f6SCédric Le Goater {
6753aa597f6SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
6763aa597f6SCédric Le Goater     XiveRouterClass *xrc = XIVE_ROUTER_CLASS(klass);
677ebd6be08SDavid Gibson     SpaprInterruptControllerClass *sicc = SPAPR_INTC_CLASS(klass);
6783aa597f6SCédric Le Goater 
6793aa597f6SCédric Le Goater     dc->desc    = "sPAPR XIVE Interrupt Controller";
6803aa597f6SCédric Le Goater     dc->props   = spapr_xive_properties;
6813aa597f6SCédric Le Goater     dc->realize = spapr_xive_realize;
6823aa597f6SCédric Le Goater     dc->vmsd    = &vmstate_spapr_xive;
6833aa597f6SCédric Le Goater 
6843aa597f6SCédric Le Goater     xrc->get_eas = spapr_xive_get_eas;
6853aa597f6SCédric Le Goater     xrc->get_end = spapr_xive_get_end;
6863aa597f6SCédric Le Goater     xrc->write_end = spapr_xive_write_end;
6870cddee8dSCédric Le Goater     xrc->get_nvt = spapr_xive_get_nvt;
6880cddee8dSCédric Le Goater     xrc->write_nvt = spapr_xive_write_nvt;
68940a5056cSCédric Le Goater     xrc->get_tctx = spapr_xive_get_tctx;
690ebd6be08SDavid Gibson 
691567192d4SDavid Gibson     sicc->activate = spapr_xive_activate;
692567192d4SDavid Gibson     sicc->deactivate = spapr_xive_deactivate;
693ebd6be08SDavid Gibson     sicc->cpu_intc_create = spapr_xive_cpu_intc_create;
694d49e8a9bSCédric Le Goater     sicc->cpu_intc_reset = spapr_xive_cpu_intc_reset;
6950b0e52b1SDavid Gibson     sicc->claim_irq = spapr_xive_claim_irq;
6960b0e52b1SDavid Gibson     sicc->free_irq = spapr_xive_free_irq;
6977bcdbccaSDavid Gibson     sicc->set_irq = spapr_xive_set_irq;
698328d8eb2SDavid Gibson     sicc->print_info = spapr_xive_print_info;
69905289273SDavid Gibson     sicc->dt = spapr_xive_dt;
700605994e5SDavid Gibson     sicc->post_load = spapr_xive_post_load;
7013aa597f6SCédric Le Goater }
7023aa597f6SCédric Le Goater 
7033aa597f6SCédric Le Goater static const TypeInfo spapr_xive_info = {
7043aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
7053aa597f6SCédric Le Goater     .parent = TYPE_XIVE_ROUTER,
7063aa597f6SCédric Le Goater     .instance_init = spapr_xive_instance_init,
707ce2918cbSDavid Gibson     .instance_size = sizeof(SpaprXive),
7083aa597f6SCédric Le Goater     .class_init = spapr_xive_class_init,
709150e25f8SDavid Gibson     .interfaces = (InterfaceInfo[]) {
710150e25f8SDavid Gibson         { TYPE_SPAPR_INTC },
711150e25f8SDavid Gibson         { }
712150e25f8SDavid Gibson     },
7133aa597f6SCédric Le Goater };
7143aa597f6SCédric Le Goater 
7153aa597f6SCédric Le Goater static void spapr_xive_register_types(void)
7163aa597f6SCédric Le Goater {
7173aa597f6SCédric Le Goater     type_register_static(&spapr_xive_info);
7183aa597f6SCédric Le Goater }
7193aa597f6SCédric Le Goater 
7203aa597f6SCédric Le Goater type_init(spapr_xive_register_types)
7213aa597f6SCédric Le Goater 
72223bcd5ebSCédric Le Goater /*
72323bcd5ebSCédric Le Goater  * XIVE hcalls
72423bcd5ebSCédric Le Goater  *
72523bcd5ebSCédric Le Goater  * The terminology used by the XIVE hcalls is the following :
72623bcd5ebSCédric Le Goater  *
72723bcd5ebSCédric Le Goater  *   TARGET vCPU number
72823bcd5ebSCédric Le Goater  *   EQ     Event Queue assigned by OS to receive event data
72923bcd5ebSCédric Le Goater  *   ESB    page for source interrupt management
73023bcd5ebSCédric Le Goater  *   LISN   Logical Interrupt Source Number identifying a source in the
73123bcd5ebSCédric Le Goater  *          machine
73223bcd5ebSCédric Le Goater  *   EISN   Effective Interrupt Source Number used by guest OS to
73323bcd5ebSCédric Le Goater  *          identify source in the guest
73423bcd5ebSCédric Le Goater  *
73523bcd5ebSCédric Le Goater  * The EAS, END, NVT structures are not exposed.
73623bcd5ebSCédric Le Goater  */
73723bcd5ebSCédric Le Goater 
73823bcd5ebSCédric Le Goater /*
73923bcd5ebSCédric Le Goater  * Linux hosts under OPAL reserve priority 7 for their own escalation
74023bcd5ebSCédric Le Goater  * interrupts (DD2.X POWER9). So we only allow the guest to use
74123bcd5ebSCédric Le Goater  * priorities [0..6].
74223bcd5ebSCédric Le Goater  */
74323bcd5ebSCédric Le Goater static bool spapr_xive_priority_is_reserved(uint8_t priority)
74423bcd5ebSCédric Le Goater {
74523bcd5ebSCédric Le Goater     switch (priority) {
74623bcd5ebSCédric Le Goater     case 0 ... 6:
74723bcd5ebSCédric Le Goater         return false;
74823bcd5ebSCédric Le Goater     case 7: /* OPAL escalation queue */
74923bcd5ebSCédric Le Goater     default:
75023bcd5ebSCédric Le Goater         return true;
75123bcd5ebSCédric Le Goater     }
75223bcd5ebSCédric Le Goater }
75323bcd5ebSCédric Le Goater 
75423bcd5ebSCédric Le Goater /*
75523bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical
75623bcd5ebSCédric Le Goater  * real address of the MMIO page through which the Event State Buffer
75723bcd5ebSCédric Le Goater  * entry associated with the value of the "lisn" parameter is managed.
75823bcd5ebSCédric Le Goater  *
75923bcd5ebSCédric Le Goater  * Parameters:
76023bcd5ebSCédric Le Goater  * Input
76123bcd5ebSCédric Le Goater  * - R4: "flags"
76223bcd5ebSCédric Le Goater  *         Bits 0-63 reserved
76323bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
76423bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
76523bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned
76623bcd5ebSCédric Le Goater  *       by the H_ALLOCATE_VAS_WINDOW hcall
76723bcd5ebSCédric Le Goater  *
76823bcd5ebSCédric Le Goater  * Output
76923bcd5ebSCédric Le Goater  * - R4: "flags"
77023bcd5ebSCédric Le Goater  *         Bits 0-59: Reserved
77123bcd5ebSCédric Le Goater  *         Bit 60: H_INT_ESB must be used for Event State Buffer
77223bcd5ebSCédric Le Goater  *                 management
77323bcd5ebSCédric Le Goater  *         Bit 61: 1 == LSI  0 == MSI
77423bcd5ebSCédric Le Goater  *         Bit 62: the full function page supports trigger
77523bcd5ebSCédric Le Goater  *         Bit 63: Store EOI Supported
77623bcd5ebSCédric Le Goater  * - R5: Logical Real address of full function Event State Buffer
77723bcd5ebSCédric Le Goater  *       management page, -1 if H_INT_ESB hcall flag is set to 1.
77823bcd5ebSCédric Le Goater  * - R6: Logical Real Address of trigger only Event State Buffer
77923bcd5ebSCédric Le Goater  *       management page or -1.
78023bcd5ebSCédric Le Goater  * - R7: Power of 2 page size for the ESB management pages returned in
78123bcd5ebSCédric Le Goater  *       R5 and R6.
78223bcd5ebSCédric Le Goater  */
78323bcd5ebSCédric Le Goater 
78423bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_H_INT_ESB     PPC_BIT(60) /* ESB manage with H_INT_ESB */
78523bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_LSI           PPC_BIT(61) /* Virtual LSI type */
78623bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_TRIGGER       PPC_BIT(62) /* Trigger and management
78723bcd5ebSCédric Le Goater                                                     on same page */
78823bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_STORE_EOI     PPC_BIT(63) /* Store EOI support */
78923bcd5ebSCédric Le Goater 
79023bcd5ebSCédric Le Goater static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
791ce2918cbSDavid Gibson                                           SpaprMachineState *spapr,
79223bcd5ebSCédric Le Goater                                           target_ulong opcode,
79323bcd5ebSCédric Le Goater                                           target_ulong *args)
79423bcd5ebSCédric Le Goater {
795ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
79623bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
79723bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
79823bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
79923bcd5ebSCédric Le Goater 
80023bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
80123bcd5ebSCédric Le Goater         return H_FUNCTION;
80223bcd5ebSCédric Le Goater     }
80323bcd5ebSCédric Le Goater 
80423bcd5ebSCédric Le Goater     if (flags) {
80523bcd5ebSCédric Le Goater         return H_PARAMETER;
80623bcd5ebSCédric Le Goater     }
80723bcd5ebSCédric Le Goater 
80823bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
80923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
81023bcd5ebSCédric Le Goater                       lisn);
81123bcd5ebSCédric Le Goater         return H_P2;
81223bcd5ebSCédric Le Goater     }
81323bcd5ebSCédric Le Goater 
81423bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&xive->eat[lisn])) {
81523bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
81623bcd5ebSCédric Le Goater                       lisn);
81723bcd5ebSCédric Le Goater         return H_P2;
81823bcd5ebSCédric Le Goater     }
81923bcd5ebSCédric Le Goater 
82023bcd5ebSCédric Le Goater     /*
82123bcd5ebSCédric Le Goater      * All sources are emulated under the main XIVE object and share
82223bcd5ebSCédric Le Goater      * the same characteristics.
82323bcd5ebSCédric Le Goater      */
82423bcd5ebSCédric Le Goater     args[0] = 0;
82523bcd5ebSCédric Le Goater     if (!xive_source_esb_has_2page(xsrc)) {
82623bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_TRIGGER;
82723bcd5ebSCédric Le Goater     }
82823bcd5ebSCédric Le Goater     if (xsrc->esb_flags & XIVE_SRC_STORE_EOI) {
82923bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_STORE_EOI;
83023bcd5ebSCédric Le Goater     }
83123bcd5ebSCédric Le Goater 
83223bcd5ebSCédric Le Goater     /*
83323bcd5ebSCédric Le Goater      * Force the use of the H_INT_ESB hcall in case of an LSI
83423bcd5ebSCédric Le Goater      * interrupt. This is necessary under KVM to re-trigger the
83523bcd5ebSCédric Le Goater      * interrupt if the level is still asserted
83623bcd5ebSCédric Le Goater      */
83723bcd5ebSCédric Le Goater     if (xive_source_irq_is_lsi(xsrc, lisn)) {
83823bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_H_INT_ESB | SPAPR_XIVE_SRC_LSI;
83923bcd5ebSCédric Le Goater     }
84023bcd5ebSCédric Le Goater 
84123bcd5ebSCédric Le Goater     if (!(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
84223bcd5ebSCédric Le Goater         args[1] = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn);
84323bcd5ebSCédric Le Goater     } else {
84423bcd5ebSCédric Le Goater         args[1] = -1;
84523bcd5ebSCédric Le Goater     }
84623bcd5ebSCédric Le Goater 
84723bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc) &&
84823bcd5ebSCédric Le Goater         !(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
84923bcd5ebSCédric Le Goater         args[2] = xive->vc_base + xive_source_esb_page(xsrc, lisn);
85023bcd5ebSCédric Le Goater     } else {
85123bcd5ebSCédric Le Goater         args[2] = -1;
85223bcd5ebSCédric Le Goater     }
85323bcd5ebSCédric Le Goater 
85423bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc)) {
85523bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift - 1;
85623bcd5ebSCédric Le Goater     } else {
85723bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift;
85823bcd5ebSCédric Le Goater     }
85923bcd5ebSCédric Le Goater 
86023bcd5ebSCédric Le Goater     return H_SUCCESS;
86123bcd5ebSCédric Le Goater }
86223bcd5ebSCédric Le Goater 
86323bcd5ebSCédric Le Goater /*
86423bcd5ebSCédric Le Goater  * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
86523bcd5ebSCédric Le Goater  * Interrupt Source to a target. The Logical Interrupt Source is
86623bcd5ebSCédric Le Goater  * designated with the "lisn" parameter and the target is designated
86723bcd5ebSCédric Le Goater  * with the "target" and "priority" parameters.  Upon return from the
86823bcd5ebSCédric Le Goater  * hcall(), no additional interrupts will be directed to the old EQ.
86923bcd5ebSCédric Le Goater  *
87023bcd5ebSCédric Le Goater  * Parameters:
87123bcd5ebSCédric Le Goater  * Input:
87223bcd5ebSCédric Le Goater  * - R4: "flags"
87323bcd5ebSCédric Le Goater  *         Bits 0-61: Reserved
87423bcd5ebSCédric Le Goater  *         Bit 62: set the "eisn" in the EAS
87523bcd5ebSCédric Le Goater  *         Bit 63: masks the interrupt source in the hardware interrupt
87623bcd5ebSCédric Le Goater  *       control structure. An interrupt masked by this mechanism will
87723bcd5ebSCédric Le Goater  *       be dropped, but it's source state bits will still be
87823bcd5ebSCédric Le Goater  *       set. There is no race-free way of unmasking and restoring the
87923bcd5ebSCédric Le Goater  *       source. Thus this should only be used in interrupts that are
88023bcd5ebSCédric Le Goater  *       also masked at the source, and only in cases where the
88123bcd5ebSCédric Le Goater  *       interrupt is not meant to be used for a large amount of time
88223bcd5ebSCédric Le Goater  *       because no valid target exists for it for example
88323bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
88423bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
88523bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned by
88623bcd5ebSCédric Le Goater  *       the H_ALLOCATE_VAS_WINDOW hcall
88723bcd5ebSCédric Le Goater  * - R6: "target" is per "ibm,ppc-interrupt-server#s" or
88823bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
88923bcd5ebSCédric Le Goater  * - R7: "priority" is a valid priority not in
89023bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
89123bcd5ebSCédric Le Goater  * - R8: "eisn" is the guest EISN associated with the "lisn"
89223bcd5ebSCédric Le Goater  *
89323bcd5ebSCédric Le Goater  * Output:
89423bcd5ebSCédric Le Goater  * - None
89523bcd5ebSCédric Le Goater  */
89623bcd5ebSCédric Le Goater 
89723bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_SET_EISN PPC_BIT(62)
89823bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_MASK     PPC_BIT(63)
89923bcd5ebSCédric Le Goater 
90023bcd5ebSCédric Le Goater static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
901ce2918cbSDavid Gibson                                             SpaprMachineState *spapr,
90223bcd5ebSCédric Le Goater                                             target_ulong opcode,
90323bcd5ebSCédric Le Goater                                             target_ulong *args)
90423bcd5ebSCédric Le Goater {
905ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
90623bcd5ebSCédric Le Goater     XiveEAS eas, new_eas;
90723bcd5ebSCédric Le Goater     target_ulong flags    = args[0];
90823bcd5ebSCédric Le Goater     target_ulong lisn     = args[1];
90923bcd5ebSCédric Le Goater     target_ulong target   = args[2];
91023bcd5ebSCédric Le Goater     target_ulong priority = args[3];
91123bcd5ebSCédric Le Goater     target_ulong eisn     = args[4];
91223bcd5ebSCédric Le Goater     uint8_t end_blk;
91323bcd5ebSCédric Le Goater     uint32_t end_idx;
91423bcd5ebSCédric Le Goater 
91523bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
91623bcd5ebSCédric Le Goater         return H_FUNCTION;
91723bcd5ebSCédric Le Goater     }
91823bcd5ebSCédric Le Goater 
91923bcd5ebSCédric Le Goater     if (flags & ~(SPAPR_XIVE_SRC_SET_EISN | SPAPR_XIVE_SRC_MASK)) {
92023bcd5ebSCédric Le Goater         return H_PARAMETER;
92123bcd5ebSCédric Le Goater     }
92223bcd5ebSCédric Le Goater 
92323bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
92423bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
92523bcd5ebSCédric Le Goater                       lisn);
92623bcd5ebSCédric Le Goater         return H_P2;
92723bcd5ebSCédric Le Goater     }
92823bcd5ebSCédric Le Goater 
92923bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
93023bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
93123bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
93223bcd5ebSCédric Le Goater                       lisn);
93323bcd5ebSCédric Le Goater         return H_P2;
93423bcd5ebSCédric Le Goater     }
93523bcd5ebSCédric Le Goater 
93623bcd5ebSCédric Le Goater     /* priority 0xff is used to reset the EAS */
93723bcd5ebSCédric Le Goater     if (priority == 0xff) {
93823bcd5ebSCédric Le Goater         new_eas.w = cpu_to_be64(EAS_VALID | EAS_MASKED);
93923bcd5ebSCédric Le Goater         goto out;
94023bcd5ebSCédric Le Goater     }
94123bcd5ebSCédric Le Goater 
94223bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_MASK) {
94323bcd5ebSCédric Le Goater         new_eas.w = eas.w | cpu_to_be64(EAS_MASKED);
94423bcd5ebSCédric Le Goater     } else {
94523bcd5ebSCédric Le Goater         new_eas.w = eas.w & cpu_to_be64(~EAS_MASKED);
94623bcd5ebSCédric Le Goater     }
94723bcd5ebSCédric Le Goater 
94823bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
94923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
95023bcd5ebSCédric Le Goater                       " is reserved\n", priority);
95123bcd5ebSCédric Le Goater         return H_P4;
95223bcd5ebSCédric Le Goater     }
95323bcd5ebSCédric Le Goater 
95423bcd5ebSCédric Le Goater     /*
95523bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
95623bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
95723bcd5ebSCédric Le Goater      * target.
95823bcd5ebSCédric Le Goater      */
95923bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
96023bcd5ebSCédric Le Goater         return H_P3;
96123bcd5ebSCédric Le Goater     }
96223bcd5ebSCédric Le Goater 
96323bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_BLOCK, new_eas.w, end_blk);
96423bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_INDEX, new_eas.w, end_idx);
96523bcd5ebSCédric Le Goater 
96623bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_SET_EISN) {
96723bcd5ebSCédric Le Goater         new_eas.w = xive_set_field64(EAS_END_DATA, new_eas.w, eisn);
96823bcd5ebSCédric Le Goater     }
96923bcd5ebSCédric Le Goater 
9700c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
9710c575703SCédric Le Goater         Error *local_err = NULL;
9720c575703SCédric Le Goater 
9730c575703SCédric Le Goater         kvmppc_xive_set_source_config(xive, lisn, &new_eas, &local_err);
9740c575703SCédric Le Goater         if (local_err) {
9750c575703SCédric Le Goater             error_report_err(local_err);
9760c575703SCédric Le Goater             return H_HARDWARE;
9770c575703SCédric Le Goater         }
9780c575703SCédric Le Goater     }
9790c575703SCédric Le Goater 
98023bcd5ebSCédric Le Goater out:
98123bcd5ebSCédric Le Goater     xive->eat[lisn] = new_eas;
98223bcd5ebSCédric Le Goater     return H_SUCCESS;
98323bcd5ebSCédric Le Goater }
98423bcd5ebSCédric Le Goater 
98523bcd5ebSCédric Le Goater /*
98623bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
98723bcd5ebSCédric Le Goater  * target/priority pair is assigned to the specified Logical Interrupt
98823bcd5ebSCédric Le Goater  * Source.
98923bcd5ebSCédric Le Goater  *
99023bcd5ebSCédric Le Goater  * Parameters:
99123bcd5ebSCédric Le Goater  * Input:
99223bcd5ebSCédric Le Goater  * - R4: "flags"
99323bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
99423bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
99523bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
99623bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
99723bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
99823bcd5ebSCédric Le Goater  *
99923bcd5ebSCédric Le Goater  * Output:
100023bcd5ebSCédric Le Goater  * - R4: Target to which the specified Logical Interrupt Source is
100123bcd5ebSCédric Le Goater  *       assigned
100223bcd5ebSCédric Le Goater  * - R5: Priority to which the specified Logical Interrupt Source is
100323bcd5ebSCédric Le Goater  *       assigned
100423bcd5ebSCédric Le Goater  * - R6: EISN for the specified Logical Interrupt Source (this will be
100523bcd5ebSCédric Le Goater  *       equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
100623bcd5ebSCédric Le Goater  */
100723bcd5ebSCédric Le Goater static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
1008ce2918cbSDavid Gibson                                             SpaprMachineState *spapr,
100923bcd5ebSCédric Le Goater                                             target_ulong opcode,
101023bcd5ebSCédric Le Goater                                             target_ulong *args)
101123bcd5ebSCédric Le Goater {
1012ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
101323bcd5ebSCédric Le Goater     target_ulong flags = args[0];
101423bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
101523bcd5ebSCédric Le Goater     XiveEAS eas;
101623bcd5ebSCédric Le Goater     XiveEND *end;
101723bcd5ebSCédric Le Goater     uint8_t nvt_blk;
101823bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
101923bcd5ebSCédric Le Goater 
102023bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
102123bcd5ebSCédric Le Goater         return H_FUNCTION;
102223bcd5ebSCédric Le Goater     }
102323bcd5ebSCédric Le Goater 
102423bcd5ebSCédric Le Goater     if (flags) {
102523bcd5ebSCédric Le Goater         return H_PARAMETER;
102623bcd5ebSCédric Le Goater     }
102723bcd5ebSCédric Le Goater 
102823bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
102923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
103023bcd5ebSCédric Le Goater                       lisn);
103123bcd5ebSCédric Le Goater         return H_P2;
103223bcd5ebSCédric Le Goater     }
103323bcd5ebSCédric Le Goater 
103423bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
103523bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
103623bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
103723bcd5ebSCédric Le Goater                       lisn);
103823bcd5ebSCédric Le Goater         return H_P2;
103923bcd5ebSCédric Le Goater     }
104023bcd5ebSCédric Le Goater 
104123bcd5ebSCédric Le Goater     /* EAS_END_BLOCK is unused on sPAPR */
104223bcd5ebSCédric Le Goater     end_idx = xive_get_field64(EAS_END_INDEX, eas.w);
104323bcd5ebSCédric Le Goater 
104423bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
104523bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
104623bcd5ebSCédric Le Goater 
104723bcd5ebSCédric Le Goater     nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
104823bcd5ebSCédric Le Goater     nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
104923bcd5ebSCédric Le Goater     args[0] = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
105023bcd5ebSCédric Le Goater 
105123bcd5ebSCédric Le Goater     if (xive_eas_is_masked(&eas)) {
105223bcd5ebSCédric Le Goater         args[1] = 0xff;
105323bcd5ebSCédric Le Goater     } else {
105423bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
105523bcd5ebSCédric Le Goater     }
105623bcd5ebSCédric Le Goater 
105723bcd5ebSCédric Le Goater     args[2] = xive_get_field64(EAS_END_DATA, eas.w);
105823bcd5ebSCédric Le Goater 
105923bcd5ebSCédric Le Goater     return H_SUCCESS;
106023bcd5ebSCédric Le Goater }
106123bcd5ebSCédric Le Goater 
106223bcd5ebSCédric Le Goater /*
106323bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
106423bcd5ebSCédric Le Goater  * address of the notification management page associated with the
106523bcd5ebSCédric Le Goater  * specified target and priority.
106623bcd5ebSCédric Le Goater  *
106723bcd5ebSCédric Le Goater  * Parameters:
106823bcd5ebSCédric Le Goater  * Input:
106923bcd5ebSCédric Le Goater  * - R4: "flags"
107023bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
107123bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
107223bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
107323bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
107423bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
107523bcd5ebSCédric Le Goater  *
107623bcd5ebSCédric Le Goater  * Output:
107723bcd5ebSCédric Le Goater  * - R4: Logical real address of notification page
107823bcd5ebSCédric Le Goater  * - R5: Power of 2 page size of the notification page
107923bcd5ebSCédric Le Goater  */
108023bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
1081ce2918cbSDavid Gibson                                          SpaprMachineState *spapr,
108223bcd5ebSCédric Le Goater                                          target_ulong opcode,
108323bcd5ebSCédric Le Goater                                          target_ulong *args)
108423bcd5ebSCédric Le Goater {
1085ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
108623bcd5ebSCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
108723bcd5ebSCédric Le Goater     target_ulong flags = args[0];
108823bcd5ebSCédric Le Goater     target_ulong target = args[1];
108923bcd5ebSCédric Le Goater     target_ulong priority = args[2];
109023bcd5ebSCédric Le Goater     XiveEND *end;
109123bcd5ebSCédric Le Goater     uint8_t end_blk;
109223bcd5ebSCédric Le Goater     uint32_t end_idx;
109323bcd5ebSCédric Le Goater 
109423bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
109523bcd5ebSCédric Le Goater         return H_FUNCTION;
109623bcd5ebSCédric Le Goater     }
109723bcd5ebSCédric Le Goater 
109823bcd5ebSCédric Le Goater     if (flags) {
109923bcd5ebSCédric Le Goater         return H_PARAMETER;
110023bcd5ebSCédric Le Goater     }
110123bcd5ebSCédric Le Goater 
110223bcd5ebSCédric Le Goater     /*
110323bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
110423bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
110523bcd5ebSCédric Le Goater      */
110623bcd5ebSCédric Le Goater 
110723bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
110823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
110923bcd5ebSCédric Le Goater                       " is reserved\n", priority);
111023bcd5ebSCédric Le Goater         return H_P3;
111123bcd5ebSCédric Le Goater     }
111223bcd5ebSCédric Le Goater 
111323bcd5ebSCédric Le Goater     /*
111423bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
111523bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
111623bcd5ebSCédric Le Goater      * target.
111723bcd5ebSCédric Le Goater      */
111823bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
111923bcd5ebSCédric Le Goater         return H_P2;
112023bcd5ebSCédric Le Goater     }
112123bcd5ebSCédric Le Goater 
112223bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
112323bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
112423bcd5ebSCédric Le Goater 
112523bcd5ebSCédric Le Goater     args[0] = xive->end_base + (1ull << (end_xsrc->esb_shift + 1)) * end_idx;
112623bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
112723bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
112823bcd5ebSCédric Le Goater     } else {
112923bcd5ebSCédric Le Goater         args[1] = 0;
113023bcd5ebSCédric Le Goater     }
113123bcd5ebSCédric Le Goater 
113223bcd5ebSCédric Le Goater     return H_SUCCESS;
113323bcd5ebSCédric Le Goater }
113423bcd5ebSCédric Le Goater 
113523bcd5ebSCédric Le Goater /*
113623bcd5ebSCédric Le Goater  * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
113723bcd5ebSCédric Le Goater  * a given "target" and "priority".  It is also used to set the
113823bcd5ebSCédric Le Goater  * notification config associated with the EQ.  An EQ size of 0 is
113923bcd5ebSCédric Le Goater  * used to reset the EQ config for a given target and priority. If
114023bcd5ebSCédric Le Goater  * resetting the EQ config, the END associated with the given "target"
114123bcd5ebSCédric Le Goater  * and "priority" will be changed to disable queueing.
114223bcd5ebSCédric Le Goater  *
114323bcd5ebSCédric Le Goater  * Upon return from the hcall(), no additional interrupts will be
114423bcd5ebSCédric Le Goater  * directed to the old EQ (if one was set). The old EQ (if one was
114523bcd5ebSCédric Le Goater  * set) should be investigated for interrupts that occurred prior to
114623bcd5ebSCédric Le Goater  * or during the hcall().
114723bcd5ebSCédric Le Goater  *
114823bcd5ebSCédric Le Goater  * Parameters:
114923bcd5ebSCédric Le Goater  * Input:
115023bcd5ebSCédric Le Goater  * - R4: "flags"
115123bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
115223bcd5ebSCédric Le Goater  *         Bit 63: Unconditional Notify (n) per the XIVE spec
115323bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
115423bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
115523bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
115623bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
115723bcd5ebSCédric Le Goater  * - R7: "eventQueue": The logical real address of the start of the EQ
115823bcd5ebSCédric Le Goater  * - R8: "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
115923bcd5ebSCédric Le Goater  *
116023bcd5ebSCédric Le Goater  * Output:
116123bcd5ebSCédric Le Goater  * - None
116223bcd5ebSCédric Le Goater  */
116323bcd5ebSCédric Le Goater 
116423bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_ALWAYS_NOTIFY PPC_BIT(63)
116523bcd5ebSCédric Le Goater 
116623bcd5ebSCédric Le Goater static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
1167ce2918cbSDavid Gibson                                            SpaprMachineState *spapr,
116823bcd5ebSCédric Le Goater                                            target_ulong opcode,
116923bcd5ebSCédric Le Goater                                            target_ulong *args)
117023bcd5ebSCédric Le Goater {
1171ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
117223bcd5ebSCédric Le Goater     target_ulong flags = args[0];
117323bcd5ebSCédric Le Goater     target_ulong target = args[1];
117423bcd5ebSCédric Le Goater     target_ulong priority = args[2];
117523bcd5ebSCédric Le Goater     target_ulong qpage = args[3];
117623bcd5ebSCédric Le Goater     target_ulong qsize = args[4];
117723bcd5ebSCédric Le Goater     XiveEND end;
117823bcd5ebSCédric Le Goater     uint8_t end_blk, nvt_blk;
117923bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
118023bcd5ebSCédric Le Goater 
118123bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
118223bcd5ebSCédric Le Goater         return H_FUNCTION;
118323bcd5ebSCédric Le Goater     }
118423bcd5ebSCédric Le Goater 
118523bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_ALWAYS_NOTIFY) {
118623bcd5ebSCédric Le Goater         return H_PARAMETER;
118723bcd5ebSCédric Le Goater     }
118823bcd5ebSCédric Le Goater 
118923bcd5ebSCédric Le Goater     /*
119023bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
119123bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
119223bcd5ebSCédric Le Goater      */
119323bcd5ebSCédric Le Goater 
119423bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
119523bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
119623bcd5ebSCédric Le Goater                       " is reserved\n", priority);
119723bcd5ebSCédric Le Goater         return H_P3;
119823bcd5ebSCédric Le Goater     }
119923bcd5ebSCédric Le Goater 
120023bcd5ebSCédric Le Goater     /*
120123bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
120223bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
120323bcd5ebSCédric Le Goater      * target.
120423bcd5ebSCédric Le Goater      */
120523bcd5ebSCédric Le Goater 
120623bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
120723bcd5ebSCédric Le Goater         return H_P2;
120823bcd5ebSCédric Le Goater     }
120923bcd5ebSCédric Le Goater 
121023bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
121123bcd5ebSCédric Le Goater     memcpy(&end, &xive->endt[end_idx], sizeof(XiveEND));
121223bcd5ebSCédric Le Goater 
121323bcd5ebSCédric Le Goater     switch (qsize) {
121423bcd5ebSCédric Le Goater     case 12:
121523bcd5ebSCédric Le Goater     case 16:
121623bcd5ebSCédric Le Goater     case 21:
121723bcd5ebSCédric Le Goater     case 24:
12187f9136f9SCédric Le Goater         if (!QEMU_IS_ALIGNED(qpage, 1ul << qsize)) {
12197f9136f9SCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: EQ @0x%" HWADDR_PRIx
12207f9136f9SCédric Le Goater                           " is not naturally aligned with %" HWADDR_PRIx "\n",
12217f9136f9SCédric Le Goater                           qpage, (hwaddr)1 << qsize);
12227f9136f9SCédric Le Goater             return H_P4;
12237f9136f9SCédric Le Goater         }
122423bcd5ebSCédric Le Goater         end.w2 = cpu_to_be32((qpage >> 32) & 0x0fffffff);
122523bcd5ebSCédric Le Goater         end.w3 = cpu_to_be32(qpage & 0xffffffff);
122623bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_ENQUEUE);
122723bcd5ebSCédric Le Goater         end.w0 = xive_set_field32(END_W0_QSIZE, end.w0, qsize - 12);
122823bcd5ebSCédric Le Goater         break;
122923bcd5ebSCédric Le Goater     case 0:
123023bcd5ebSCédric Le Goater         /* reset queue and disable queueing */
123123bcd5ebSCédric Le Goater         spapr_xive_end_reset(&end);
123223bcd5ebSCédric Le Goater         goto out;
123323bcd5ebSCédric Le Goater 
123423bcd5ebSCédric Le Goater     default:
123523bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid EQ size %"PRIx64"\n",
123623bcd5ebSCédric Le Goater                       qsize);
123723bcd5ebSCédric Le Goater         return H_P5;
123823bcd5ebSCédric Le Goater     }
123923bcd5ebSCédric Le Goater 
124023bcd5ebSCédric Le Goater     if (qsize) {
124123bcd5ebSCédric Le Goater         hwaddr plen = 1 << qsize;
124223bcd5ebSCédric Le Goater         void *eq;
124323bcd5ebSCédric Le Goater 
124423bcd5ebSCédric Le Goater         /*
124523bcd5ebSCédric Le Goater          * Validate the guest EQ. We should also check that the queue
124623bcd5ebSCédric Le Goater          * has been zeroed by the OS.
124723bcd5ebSCédric Le Goater          */
124823bcd5ebSCédric Le Goater         eq = address_space_map(CPU(cpu)->as, qpage, &plen, true,
124923bcd5ebSCédric Le Goater                                MEMTXATTRS_UNSPECIFIED);
125023bcd5ebSCédric Le Goater         if (plen != 1 << qsize) {
125123bcd5ebSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to map EQ @0x%"
125223bcd5ebSCédric Le Goater                           HWADDR_PRIx "\n", qpage);
125323bcd5ebSCédric Le Goater             return H_P4;
125423bcd5ebSCédric Le Goater         }
125523bcd5ebSCédric Le Goater         address_space_unmap(CPU(cpu)->as, eq, plen, true, plen);
125623bcd5ebSCédric Le Goater     }
125723bcd5ebSCédric Le Goater 
125823bcd5ebSCédric Le Goater     /* "target" should have been validated above */
125923bcd5ebSCédric Le Goater     if (spapr_xive_target_to_nvt(target, &nvt_blk, &nvt_idx)) {
126023bcd5ebSCédric Le Goater         g_assert_not_reached();
126123bcd5ebSCédric Le Goater     }
126223bcd5ebSCédric Le Goater 
126323bcd5ebSCédric Le Goater     /*
126423bcd5ebSCédric Le Goater      * Ensure the priority and target are correctly set (they will not
126523bcd5ebSCédric Le Goater      * be right after allocation)
126623bcd5ebSCédric Le Goater      */
126723bcd5ebSCédric Le Goater     end.w6 = xive_set_field32(END_W6_NVT_BLOCK, 0ul, nvt_blk) |
126823bcd5ebSCédric Le Goater         xive_set_field32(END_W6_NVT_INDEX, 0ul, nvt_idx);
126923bcd5ebSCédric Le Goater     end.w7 = xive_set_field32(END_W7_F0_PRIORITY, 0ul, priority);
127023bcd5ebSCédric Le Goater 
127123bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_ALWAYS_NOTIFY) {
127223bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_UCOND_NOTIFY);
127323bcd5ebSCédric Le Goater     } else {
127423bcd5ebSCédric Le Goater         end.w0 &= cpu_to_be32((uint32_t)~END_W0_UCOND_NOTIFY);
127523bcd5ebSCédric Le Goater     }
127623bcd5ebSCédric Le Goater 
127723bcd5ebSCédric Le Goater     /*
127823bcd5ebSCédric Le Goater      * The generation bit for the END starts at 1 and The END page
127923bcd5ebSCédric Le Goater      * offset counter starts at 0.
128023bcd5ebSCédric Le Goater      */
128123bcd5ebSCédric Le Goater     end.w1 = cpu_to_be32(END_W1_GENERATION) |
128223bcd5ebSCédric Le Goater         xive_set_field32(END_W1_PAGE_OFF, 0ul, 0ul);
128323bcd5ebSCédric Le Goater     end.w0 |= cpu_to_be32(END_W0_VALID);
128423bcd5ebSCédric Le Goater 
128523bcd5ebSCédric Le Goater     /*
128623bcd5ebSCédric Le Goater      * TODO: issue syncs required to ensure all in-flight interrupts
128723bcd5ebSCédric Le Goater      * are complete on the old END
128823bcd5ebSCédric Le Goater      */
128923bcd5ebSCédric Le Goater 
129023bcd5ebSCédric Le Goater out:
12910c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
12920c575703SCédric Le Goater         Error *local_err = NULL;
12930c575703SCédric Le Goater 
12940c575703SCédric Le Goater         kvmppc_xive_set_queue_config(xive, end_blk, end_idx, &end, &local_err);
12950c575703SCédric Le Goater         if (local_err) {
12960c575703SCédric Le Goater             error_report_err(local_err);
12970c575703SCédric Le Goater             return H_HARDWARE;
12980c575703SCédric Le Goater         }
12990c575703SCédric Le Goater     }
13000c575703SCédric Le Goater 
130123bcd5ebSCédric Le Goater     /* Update END */
130223bcd5ebSCédric Le Goater     memcpy(&xive->endt[end_idx], &end, sizeof(XiveEND));
130323bcd5ebSCédric Le Goater     return H_SUCCESS;
130423bcd5ebSCédric Le Goater }
130523bcd5ebSCédric Le Goater 
130623bcd5ebSCédric Le Goater /*
130723bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
130823bcd5ebSCédric Le Goater  * target and priority.
130923bcd5ebSCédric Le Goater  *
131023bcd5ebSCédric Le Goater  * Parameters:
131123bcd5ebSCédric Le Goater  * Input:
131223bcd5ebSCédric Le Goater  * - R4: "flags"
131323bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
131423bcd5ebSCédric Le Goater  *         Bit 63: Debug: Return debug data
131523bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
131623bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
131723bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
131823bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
131923bcd5ebSCédric Le Goater  *
132023bcd5ebSCédric Le Goater  * Output:
132123bcd5ebSCédric Le Goater  * - R4: "flags":
132223bcd5ebSCédric Le Goater  *       Bits 0-61: Reserved
132323bcd5ebSCédric Le Goater  *       Bit 62: The value of Event Queue Generation Number (g) per
132423bcd5ebSCédric Le Goater  *              the XIVE spec if "Debug" = 1
132523bcd5ebSCédric Le Goater  *       Bit 63: The value of Unconditional Notify (n) per the XIVE spec
132623bcd5ebSCédric Le Goater  * - R5: The logical real address of the start of the EQ
132723bcd5ebSCédric Le Goater  * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
132823bcd5ebSCédric Le Goater  * - R7: The value of Event Queue Offset Counter per XIVE spec
132923bcd5ebSCédric Le Goater  *       if "Debug" = 1, else 0
133023bcd5ebSCédric Le Goater  *
133123bcd5ebSCédric Le Goater  */
133223bcd5ebSCédric Le Goater 
133323bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_DEBUG     PPC_BIT(63)
133423bcd5ebSCédric Le Goater 
133523bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
1336ce2918cbSDavid Gibson                                            SpaprMachineState *spapr,
133723bcd5ebSCédric Le Goater                                            target_ulong opcode,
133823bcd5ebSCédric Le Goater                                            target_ulong *args)
133923bcd5ebSCédric Le Goater {
1340ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
134123bcd5ebSCédric Le Goater     target_ulong flags = args[0];
134223bcd5ebSCédric Le Goater     target_ulong target = args[1];
134323bcd5ebSCédric Le Goater     target_ulong priority = args[2];
134423bcd5ebSCédric Le Goater     XiveEND *end;
134523bcd5ebSCédric Le Goater     uint8_t end_blk;
134623bcd5ebSCédric Le Goater     uint32_t end_idx;
134723bcd5ebSCédric Le Goater 
134823bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
134923bcd5ebSCédric Le Goater         return H_FUNCTION;
135023bcd5ebSCédric Le Goater     }
135123bcd5ebSCédric Le Goater 
135223bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_DEBUG) {
135323bcd5ebSCédric Le Goater         return H_PARAMETER;
135423bcd5ebSCédric Le Goater     }
135523bcd5ebSCédric Le Goater 
135623bcd5ebSCédric Le Goater     /*
135723bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
135823bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
135923bcd5ebSCédric Le Goater      */
136023bcd5ebSCédric Le Goater 
136123bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
136223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
136323bcd5ebSCédric Le Goater                       " is reserved\n", priority);
136423bcd5ebSCédric Le Goater         return H_P3;
136523bcd5ebSCédric Le Goater     }
136623bcd5ebSCédric Le Goater 
136723bcd5ebSCédric Le Goater     /*
136823bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
136923bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
137023bcd5ebSCédric Le Goater      * target.
137123bcd5ebSCédric Le Goater      */
137223bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
137323bcd5ebSCédric Le Goater         return H_P2;
137423bcd5ebSCédric Le Goater     }
137523bcd5ebSCédric Le Goater 
137623bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
137723bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
137823bcd5ebSCédric Le Goater 
137923bcd5ebSCédric Le Goater     args[0] = 0;
138023bcd5ebSCédric Le Goater     if (xive_end_is_notify(end)) {
138123bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_END_ALWAYS_NOTIFY;
138223bcd5ebSCédric Le Goater     }
138323bcd5ebSCédric Le Goater 
138423bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
138513df9324SCédric Le Goater         args[1] = xive_end_qaddr(end);
138623bcd5ebSCédric Le Goater         args[2] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
138723bcd5ebSCédric Le Goater     } else {
138823bcd5ebSCédric Le Goater         args[1] = 0;
138923bcd5ebSCédric Le Goater         args[2] = 0;
139023bcd5ebSCédric Le Goater     }
139123bcd5ebSCédric Le Goater 
13920c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
13930c575703SCédric Le Goater         Error *local_err = NULL;
13940c575703SCédric Le Goater 
13950c575703SCédric Le Goater         kvmppc_xive_get_queue_config(xive, end_blk, end_idx, end, &local_err);
13960c575703SCédric Le Goater         if (local_err) {
13970c575703SCédric Le Goater             error_report_err(local_err);
13980c575703SCédric Le Goater             return H_HARDWARE;
13990c575703SCédric Le Goater         }
14000c575703SCédric Le Goater     }
14010c575703SCédric Le Goater 
140223bcd5ebSCédric Le Goater     /* TODO: do we need any locking on the END ? */
140323bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_DEBUG) {
140423bcd5ebSCédric Le Goater         /* Load the event queue generation number into the return flags */
140523bcd5ebSCédric Le Goater         args[0] |= (uint64_t)xive_get_field32(END_W1_GENERATION, end->w1) << 62;
140623bcd5ebSCédric Le Goater 
140723bcd5ebSCédric Le Goater         /* Load R7 with the event queue offset counter */
140823bcd5ebSCédric Le Goater         args[3] = xive_get_field32(END_W1_PAGE_OFF, end->w1);
140923bcd5ebSCédric Le Goater     } else {
141023bcd5ebSCédric Le Goater         args[3] = 0;
141123bcd5ebSCédric Le Goater     }
141223bcd5ebSCédric Le Goater 
141323bcd5ebSCédric Le Goater     return H_SUCCESS;
141423bcd5ebSCédric Le Goater }
141523bcd5ebSCédric Le Goater 
141623bcd5ebSCédric Le Goater /*
141723bcd5ebSCédric Le Goater  * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
141823bcd5ebSCédric Le Goater  * reporting cache line pair for the calling thread.  The reporting
141923bcd5ebSCédric Le Goater  * cache lines will contain the OS interrupt context when the OS
142023bcd5ebSCédric Le Goater  * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
142123bcd5ebSCédric Le Goater  * interrupt. The reporting cache lines can be reset by inputting -1
142223bcd5ebSCédric Le Goater  * in "reportingLine".  Issuing the CI store byte without reporting
142323bcd5ebSCédric Le Goater  * cache lines registered will result in the data not being accessible
142423bcd5ebSCédric Le Goater  * to the OS.
142523bcd5ebSCédric Le Goater  *
142623bcd5ebSCédric Le Goater  * Parameters:
142723bcd5ebSCédric Le Goater  * Input:
142823bcd5ebSCédric Le Goater  * - R4: "flags"
142923bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
143023bcd5ebSCédric Le Goater  * - R5: "reportingLine": The logical real address of the reporting cache
143123bcd5ebSCédric Le Goater  *       line pair
143223bcd5ebSCédric Le Goater  *
143323bcd5ebSCédric Le Goater  * Output:
143423bcd5ebSCédric Le Goater  * - None
143523bcd5ebSCédric Le Goater  */
143623bcd5ebSCédric Le Goater static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
1437ce2918cbSDavid Gibson                                                 SpaprMachineState *spapr,
143823bcd5ebSCédric Le Goater                                                 target_ulong opcode,
143923bcd5ebSCédric Le Goater                                                 target_ulong *args)
144023bcd5ebSCédric Le Goater {
144123bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
144223bcd5ebSCédric Le Goater         return H_FUNCTION;
144323bcd5ebSCédric Le Goater     }
144423bcd5ebSCédric Le Goater 
144523bcd5ebSCédric Le Goater     /*
144623bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
144723bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
144823bcd5ebSCédric Le Goater      */
144923bcd5ebSCédric Le Goater 
145023bcd5ebSCédric Le Goater     /* TODO: H_INT_SET_OS_REPORTING_LINE */
145123bcd5ebSCédric Le Goater     return H_FUNCTION;
145223bcd5ebSCédric Le Goater }
145323bcd5ebSCédric Le Goater 
145423bcd5ebSCédric Le Goater /*
145523bcd5ebSCédric Le Goater  * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
145623bcd5ebSCédric Le Goater  * real address of the reporting cache line pair set for the input
145723bcd5ebSCédric Le Goater  * "target".  If no reporting cache line pair has been set, -1 is
145823bcd5ebSCédric Le Goater  * returned.
145923bcd5ebSCédric Le Goater  *
146023bcd5ebSCédric Le Goater  * Parameters:
146123bcd5ebSCédric Le Goater  * Input:
146223bcd5ebSCédric Le Goater  * - R4: "flags"
146323bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
146423bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
146523bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
146623bcd5ebSCédric Le Goater  * - R6: "reportingLine": The logical real address of the reporting
146723bcd5ebSCédric Le Goater  *        cache line pair
146823bcd5ebSCédric Le Goater  *
146923bcd5ebSCédric Le Goater  * Output:
147023bcd5ebSCédric Le Goater  * - R4: The logical real address of the reporting line if set, else -1
147123bcd5ebSCédric Le Goater  */
147223bcd5ebSCédric Le Goater static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
1473ce2918cbSDavid Gibson                                                 SpaprMachineState *spapr,
147423bcd5ebSCédric Le Goater                                                 target_ulong opcode,
147523bcd5ebSCédric Le Goater                                                 target_ulong *args)
147623bcd5ebSCédric Le Goater {
147723bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
147823bcd5ebSCédric Le Goater         return H_FUNCTION;
147923bcd5ebSCédric Le Goater     }
148023bcd5ebSCédric Le Goater 
148123bcd5ebSCédric Le Goater     /*
148223bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
148323bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
148423bcd5ebSCédric Le Goater      */
148523bcd5ebSCédric Le Goater 
148623bcd5ebSCédric Le Goater     /* TODO: H_INT_GET_OS_REPORTING_LINE */
148723bcd5ebSCédric Le Goater     return H_FUNCTION;
148823bcd5ebSCédric Le Goater }
148923bcd5ebSCédric Le Goater 
149023bcd5ebSCédric Le Goater /*
149123bcd5ebSCédric Le Goater  * The H_INT_ESB hcall() is used to issue a load or store to the ESB
149223bcd5ebSCédric Le Goater  * page for the input "lisn".  This hcall is only supported for LISNs
149323bcd5ebSCédric Le Goater  * that have the ESB hcall flag set to 1 when returned from hcall()
149423bcd5ebSCédric Le Goater  * H_INT_GET_SOURCE_INFO.
149523bcd5ebSCédric Le Goater  *
149623bcd5ebSCédric Le Goater  * Parameters:
149723bcd5ebSCédric Le Goater  * Input:
149823bcd5ebSCédric Le Goater  * - R4: "flags"
149923bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
150023bcd5ebSCédric Le Goater  *         bit 63: Store: Store=1, store operation, else load operation
150123bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
150223bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
150323bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
150423bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
150523bcd5ebSCédric Le Goater  * - R6: "esbOffset" is the offset into the ESB page for the load or
150623bcd5ebSCédric Le Goater  *       store operation
150723bcd5ebSCédric Le Goater  * - R7: "storeData" is the data to write for a store operation
150823bcd5ebSCédric Le Goater  *
150923bcd5ebSCédric Le Goater  * Output:
151023bcd5ebSCédric Le Goater  * - R4: The value of the load if load operation, else -1
151123bcd5ebSCédric Le Goater  */
151223bcd5ebSCédric Le Goater 
151323bcd5ebSCédric Le Goater #define SPAPR_XIVE_ESB_STORE PPC_BIT(63)
151423bcd5ebSCédric Le Goater 
151523bcd5ebSCédric Le Goater static target_ulong h_int_esb(PowerPCCPU *cpu,
1516ce2918cbSDavid Gibson                               SpaprMachineState *spapr,
151723bcd5ebSCédric Le Goater                               target_ulong opcode,
151823bcd5ebSCédric Le Goater                               target_ulong *args)
151923bcd5ebSCédric Le Goater {
1520ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
152123bcd5ebSCédric Le Goater     XiveEAS eas;
152223bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
152323bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
152423bcd5ebSCédric Le Goater     target_ulong offset = args[2];
152523bcd5ebSCédric Le Goater     target_ulong data   = args[3];
152623bcd5ebSCédric Le Goater     hwaddr mmio_addr;
152723bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
152823bcd5ebSCédric Le Goater 
152923bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
153023bcd5ebSCédric Le Goater         return H_FUNCTION;
153123bcd5ebSCédric Le Goater     }
153223bcd5ebSCédric Le Goater 
153323bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_ESB_STORE) {
153423bcd5ebSCédric Le Goater         return H_PARAMETER;
153523bcd5ebSCédric Le Goater     }
153623bcd5ebSCédric Le Goater 
153723bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
153823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
153923bcd5ebSCédric Le Goater                       lisn);
154023bcd5ebSCédric Le Goater         return H_P2;
154123bcd5ebSCédric Le Goater     }
154223bcd5ebSCédric Le Goater 
154323bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
154423bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
154523bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
154623bcd5ebSCédric Le Goater                       lisn);
154723bcd5ebSCédric Le Goater         return H_P2;
154823bcd5ebSCédric Le Goater     }
154923bcd5ebSCédric Le Goater 
155023bcd5ebSCédric Le Goater     if (offset > (1ull << xsrc->esb_shift)) {
155123bcd5ebSCédric Le Goater         return H_P3;
155223bcd5ebSCédric Le Goater     }
155323bcd5ebSCédric Le Goater 
15540c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
15550c575703SCédric Le Goater         args[0] = kvmppc_xive_esb_rw(xsrc, lisn, offset, data,
15560c575703SCédric Le Goater                                      flags & SPAPR_XIVE_ESB_STORE);
15570c575703SCédric Le Goater     } else {
155823bcd5ebSCédric Le Goater         mmio_addr = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn) + offset;
155923bcd5ebSCédric Le Goater 
156023bcd5ebSCédric Le Goater         if (dma_memory_rw(&address_space_memory, mmio_addr, &data, 8,
156123bcd5ebSCédric Le Goater                           (flags & SPAPR_XIVE_ESB_STORE))) {
156223bcd5ebSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to access ESB @0x%"
156323bcd5ebSCédric Le Goater                           HWADDR_PRIx "\n", mmio_addr);
156423bcd5ebSCédric Le Goater             return H_HARDWARE;
156523bcd5ebSCédric Le Goater         }
156623bcd5ebSCédric Le Goater         args[0] = (flags & SPAPR_XIVE_ESB_STORE) ? -1 : data;
15670c575703SCédric Le Goater     }
156823bcd5ebSCédric Le Goater     return H_SUCCESS;
156923bcd5ebSCédric Le Goater }
157023bcd5ebSCédric Le Goater 
157123bcd5ebSCédric Le Goater /*
157223bcd5ebSCédric Le Goater  * The H_INT_SYNC hcall() is used to issue hardware syncs that will
157323bcd5ebSCédric Le Goater  * ensure any in flight events for the input lisn are in the event
157423bcd5ebSCédric Le Goater  * queue.
157523bcd5ebSCédric Le Goater  *
157623bcd5ebSCédric Le Goater  * Parameters:
157723bcd5ebSCédric Le Goater  * Input:
157823bcd5ebSCédric Le Goater  * - R4: "flags"
157923bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
158023bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
158123bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
158223bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
158323bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
158423bcd5ebSCédric Le Goater  *
158523bcd5ebSCédric Le Goater  * Output:
158623bcd5ebSCédric Le Goater  * - None
158723bcd5ebSCédric Le Goater  */
158823bcd5ebSCédric Le Goater static target_ulong h_int_sync(PowerPCCPU *cpu,
1589ce2918cbSDavid Gibson                                SpaprMachineState *spapr,
159023bcd5ebSCédric Le Goater                                target_ulong opcode,
159123bcd5ebSCédric Le Goater                                target_ulong *args)
159223bcd5ebSCédric Le Goater {
1593ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
159423bcd5ebSCédric Le Goater     XiveEAS eas;
159523bcd5ebSCédric Le Goater     target_ulong flags = args[0];
159623bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
159723bcd5ebSCédric Le Goater 
159823bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
159923bcd5ebSCédric Le Goater         return H_FUNCTION;
160023bcd5ebSCédric Le Goater     }
160123bcd5ebSCédric Le Goater 
160223bcd5ebSCédric Le Goater     if (flags) {
160323bcd5ebSCédric Le Goater         return H_PARAMETER;
160423bcd5ebSCédric Le Goater     }
160523bcd5ebSCédric Le Goater 
160623bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
160723bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
160823bcd5ebSCédric Le Goater                       lisn);
160923bcd5ebSCédric Le Goater         return H_P2;
161023bcd5ebSCédric Le Goater     }
161123bcd5ebSCédric Le Goater 
161223bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
161323bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
161423bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
161523bcd5ebSCédric Le Goater                       lisn);
161623bcd5ebSCédric Le Goater         return H_P2;
161723bcd5ebSCédric Le Goater     }
161823bcd5ebSCédric Le Goater 
161923bcd5ebSCédric Le Goater     /*
162023bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
162123bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
162223bcd5ebSCédric Le Goater      */
162323bcd5ebSCédric Le Goater 
16240c575703SCédric Le Goater     /*
16250c575703SCédric Le Goater      * This is not real hardware. Nothing to be done unless when
16260c575703SCédric Le Goater      * under KVM
16270c575703SCédric Le Goater      */
16280c575703SCédric Le Goater 
16290c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
16300c575703SCédric Le Goater         Error *local_err = NULL;
16310c575703SCédric Le Goater 
16320c575703SCédric Le Goater         kvmppc_xive_sync_source(xive, lisn, &local_err);
16330c575703SCédric Le Goater         if (local_err) {
16340c575703SCédric Le Goater             error_report_err(local_err);
16350c575703SCédric Le Goater             return H_HARDWARE;
16360c575703SCédric Le Goater         }
16370c575703SCédric Le Goater     }
163823bcd5ebSCédric Le Goater     return H_SUCCESS;
163923bcd5ebSCédric Le Goater }
164023bcd5ebSCédric Le Goater 
164123bcd5ebSCédric Le Goater /*
164223bcd5ebSCédric Le Goater  * The H_INT_RESET hcall() is used to reset all of the partition's
164323bcd5ebSCédric Le Goater  * interrupt exploitation structures to their initial state.  This
164423bcd5ebSCédric Le Goater  * means losing all previously set interrupt state set via
164523bcd5ebSCédric Le Goater  * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
164623bcd5ebSCédric Le Goater  *
164723bcd5ebSCédric Le Goater  * Parameters:
164823bcd5ebSCédric Le Goater  * Input:
164923bcd5ebSCédric Le Goater  * - R4: "flags"
165023bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
165123bcd5ebSCédric Le Goater  *
165223bcd5ebSCédric Le Goater  * Output:
165323bcd5ebSCédric Le Goater  * - None
165423bcd5ebSCédric Le Goater  */
165523bcd5ebSCédric Le Goater static target_ulong h_int_reset(PowerPCCPU *cpu,
1656ce2918cbSDavid Gibson                                 SpaprMachineState *spapr,
165723bcd5ebSCédric Le Goater                                 target_ulong opcode,
165823bcd5ebSCédric Le Goater                                 target_ulong *args)
165923bcd5ebSCédric Le Goater {
1660ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
166123bcd5ebSCédric Le Goater     target_ulong flags   = args[0];
166223bcd5ebSCédric Le Goater 
166323bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
166423bcd5ebSCédric Le Goater         return H_FUNCTION;
166523bcd5ebSCédric Le Goater     }
166623bcd5ebSCédric Le Goater 
166723bcd5ebSCédric Le Goater     if (flags) {
166823bcd5ebSCédric Le Goater         return H_PARAMETER;
166923bcd5ebSCédric Le Goater     }
167023bcd5ebSCédric Le Goater 
167123bcd5ebSCédric Le Goater     device_reset(DEVICE(xive));
16720c575703SCédric Le Goater 
16730c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
16740c575703SCédric Le Goater         Error *local_err = NULL;
16750c575703SCédric Le Goater 
16760c575703SCédric Le Goater         kvmppc_xive_reset(xive, &local_err);
16770c575703SCédric Le Goater         if (local_err) {
16780c575703SCédric Le Goater             error_report_err(local_err);
16790c575703SCédric Le Goater             return H_HARDWARE;
16800c575703SCédric Le Goater         }
16810c575703SCédric Le Goater     }
168223bcd5ebSCédric Le Goater     return H_SUCCESS;
168323bcd5ebSCédric Le Goater }
168423bcd5ebSCédric Le Goater 
1685ce2918cbSDavid Gibson void spapr_xive_hcall_init(SpaprMachineState *spapr)
168623bcd5ebSCédric Le Goater {
168723bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
168823bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
168923bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
169023bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
169123bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
169223bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
169323bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
169423bcd5ebSCédric Le Goater                              h_int_set_os_reporting_line);
169523bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
169623bcd5ebSCédric Le Goater                              h_int_get_os_reporting_line);
169723bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_ESB, h_int_esb);
169823bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SYNC, h_int_sync);
169923bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_RESET, h_int_reset);
170023bcd5ebSCédric Le Goater }
1701