xref: /qemu/hw/intc/spapr_xive.c (revision 4f311a70893800f33b57852ca4453823271c022b)
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 
151e519cdd9SGreg Kurz /*
152e519cdd9SGreg Kurz  * kvm_irqchip_in_kernel() will cause the compiler to turn this
153e519cdd9SGreg Kurz  * info a nop if CONFIG_KVM isn't defined.
154e519cdd9SGreg Kurz  */
155e519cdd9SGreg Kurz #define spapr_xive_in_kernel(xive) \
156e519cdd9SGreg Kurz     (kvm_irqchip_in_kernel() && (xive)->fd != -1)
157e519cdd9SGreg Kurz 
158ce2918cbSDavid Gibson void spapr_xive_pic_print_info(SpaprXive *xive, Monitor *mon)
1593aa597f6SCédric Le Goater {
1603aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
1613aa597f6SCédric Le Goater     int i;
1623aa597f6SCédric Le Goater 
163e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
1647bfc759cSCédric Le Goater         Error *local_err = NULL;
1657bfc759cSCédric Le Goater 
1667bfc759cSCédric Le Goater         kvmppc_xive_synchronize_state(xive, &local_err);
1677bfc759cSCédric Le Goater         if (local_err) {
1687bfc759cSCédric Le Goater             error_report_err(local_err);
1697bfc759cSCédric Le Goater             return;
1707bfc759cSCédric Le Goater         }
1717bfc759cSCédric Le Goater     }
1727bfc759cSCédric Le Goater 
173f81d69fcSSatheesh Rajendran     monitor_printf(mon, "  LISN         PQ    EISN     CPU/PRIO EQ\n");
1743aa597f6SCédric Le Goater 
1753aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
1763aa597f6SCédric Le Goater         uint8_t pq = xive_source_esb_get(xsrc, i);
1773aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
1783aa597f6SCédric Le Goater 
1793aa597f6SCédric Le Goater         if (!xive_eas_is_valid(eas)) {
1803aa597f6SCédric Le Goater             continue;
1813aa597f6SCédric Le Goater         }
1823aa597f6SCédric Le Goater 
1833aa597f6SCédric Le Goater         monitor_printf(mon, "  %08x %s %c%c%c %s %08x ", i,
1843aa597f6SCédric Le Goater                        xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
1853aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
1863aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
1873aa597f6SCédric Le Goater                        xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ',
1883aa597f6SCédric Le Goater                        xive_eas_is_masked(eas) ? "M" : " ",
1893aa597f6SCédric Le Goater                        (int) xive_get_field64(EAS_END_DATA, eas->w));
1903aa597f6SCédric Le Goater 
1913aa597f6SCédric Le Goater         if (!xive_eas_is_masked(eas)) {
1923aa597f6SCédric Le Goater             uint32_t end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
1933aa597f6SCédric Le Goater             XiveEND *end;
1943aa597f6SCédric Le Goater 
1953aa597f6SCédric Le Goater             assert(end_idx < xive->nr_ends);
1963aa597f6SCédric Le Goater             end = &xive->endt[end_idx];
1973aa597f6SCédric Le Goater 
1983aa597f6SCédric Le Goater             if (xive_end_is_valid(end)) {
1993aa597f6SCédric Le Goater                 spapr_xive_end_pic_print_info(xive, end, mon);
2003aa597f6SCédric Le Goater             }
2013aa597f6SCédric Le Goater         }
2023aa597f6SCédric Le Goater         monitor_printf(mon, "\n");
2033aa597f6SCédric Le Goater     }
2043aa597f6SCédric Le Goater }
2053aa597f6SCédric Le Goater 
206ce2918cbSDavid Gibson void spapr_xive_mmio_set_enabled(SpaprXive *xive, bool enable)
2073a8eb78eSCédric Le Goater {
2083a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->source.esb_mmio, enable);
2093a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->tm_mmio, enable);
2103a8eb78eSCédric Le Goater 
2113a8eb78eSCédric Le Goater     /* Disable the END ESBs until a guest OS makes use of them */
2123a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->end_source.esb_mmio, false);
2133a8eb78eSCédric Le Goater }
2143a8eb78eSCédric Le Goater 
215d024a2c1SCédric Le Goater static void spapr_xive_tm_write(void *opaque, hwaddr offset,
216d024a2c1SCédric Le Goater                           uint64_t value, unsigned size)
217d024a2c1SCédric Le Goater {
218d024a2c1SCédric Le Goater     XiveTCTX *tctx = spapr_cpu_state(POWERPC_CPU(current_cpu))->tctx;
219d024a2c1SCédric Le Goater 
220d024a2c1SCédric Le Goater     xive_tctx_tm_write(XIVE_PRESENTER(opaque), tctx, offset, value, size);
221d024a2c1SCédric Le Goater }
222d024a2c1SCédric Le Goater 
223d024a2c1SCédric Le Goater static uint64_t spapr_xive_tm_read(void *opaque, hwaddr offset, unsigned size)
224d024a2c1SCédric Le Goater {
225d024a2c1SCédric Le Goater     XiveTCTX *tctx = spapr_cpu_state(POWERPC_CPU(current_cpu))->tctx;
226d024a2c1SCédric Le Goater 
227d024a2c1SCédric Le Goater     return xive_tctx_tm_read(XIVE_PRESENTER(opaque), tctx, offset, size);
228d024a2c1SCédric Le Goater }
229d024a2c1SCédric Le Goater 
230d024a2c1SCédric Le Goater const MemoryRegionOps spapr_xive_tm_ops = {
231d024a2c1SCédric Le Goater     .read = spapr_xive_tm_read,
232d024a2c1SCédric Le Goater     .write = spapr_xive_tm_write,
233d024a2c1SCédric Le Goater     .endianness = DEVICE_BIG_ENDIAN,
234d024a2c1SCédric Le Goater     .valid = {
235d024a2c1SCédric Le Goater         .min_access_size = 1,
236d024a2c1SCédric Le Goater         .max_access_size = 8,
237d024a2c1SCédric Le Goater     },
238d024a2c1SCédric Le Goater     .impl = {
239d024a2c1SCédric Le Goater         .min_access_size = 1,
240d024a2c1SCédric Le Goater         .max_access_size = 8,
241d024a2c1SCédric Le Goater     },
242d024a2c1SCédric Le Goater };
243d024a2c1SCédric Le Goater 
2443aa597f6SCédric Le Goater static void spapr_xive_end_reset(XiveEND *end)
2453aa597f6SCédric Le Goater {
2463aa597f6SCédric Le Goater     memset(end, 0, sizeof(*end));
2473aa597f6SCédric Le Goater 
2483aa597f6SCédric Le Goater     /* switch off the escalation and notification ESBs */
2493aa597f6SCédric Le Goater     end->w1 = cpu_to_be32(END_W1_ESe_Q | END_W1_ESn_Q);
2503aa597f6SCédric Le Goater }
2513aa597f6SCédric Le Goater 
2523aa597f6SCédric Le Goater static void spapr_xive_reset(void *dev)
2533aa597f6SCédric Le Goater {
254ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(dev);
2553aa597f6SCédric Le Goater     int i;
2563aa597f6SCédric Le Goater 
2573aa597f6SCédric Le Goater     /*
2583aa597f6SCédric Le Goater      * The XiveSource has its own reset handler, which mask off all
2593aa597f6SCédric Le Goater      * IRQs (!P|Q)
2603aa597f6SCédric Le Goater      */
2613aa597f6SCédric Le Goater 
2623aa597f6SCédric Le Goater     /* Mask all valid EASs in the IRQ number space. */
2633aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
2643aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
2653aa597f6SCédric Le Goater         if (xive_eas_is_valid(eas)) {
2663aa597f6SCédric Le Goater             eas->w = cpu_to_be64(EAS_VALID | EAS_MASKED);
2673aa597f6SCédric Le Goater         } else {
2683aa597f6SCédric Le Goater             eas->w = 0;
2693aa597f6SCédric Le Goater         }
2703aa597f6SCédric Le Goater     }
2713aa597f6SCédric Le Goater 
2723aa597f6SCédric Le Goater     /* Clear all ENDs */
2733aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_ends; i++) {
2743aa597f6SCédric Le Goater         spapr_xive_end_reset(&xive->endt[i]);
2753aa597f6SCédric Le Goater     }
2763aa597f6SCédric Le Goater }
2773aa597f6SCédric Le Goater 
2783aa597f6SCédric Le Goater static void spapr_xive_instance_init(Object *obj)
2793aa597f6SCédric Le Goater {
280ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(obj);
2813aa597f6SCédric Le Goater 
2829fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "source", &xive->source, TYPE_XIVE_SOURCE);
2833aa597f6SCédric Le Goater 
284f6d4dca8SThomas Huth     object_initialize_child(obj, "end_source", &xive->end_source,
2859fc7fc4dSMarkus Armbruster                             TYPE_XIVE_END_SOURCE);
28638afd772SCédric Le Goater 
28738afd772SCédric Le Goater     /* Not connected to the KVM XIVE device */
28838afd772SCédric Le Goater     xive->fd = -1;
2893aa597f6SCédric Le Goater }
2903aa597f6SCédric Le Goater 
2913aa597f6SCédric Le Goater static void spapr_xive_realize(DeviceState *dev, Error **errp)
2923aa597f6SCédric Le Goater {
293ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(dev);
2946cc64796SGreg Kurz     SpaprXiveClass *sxc = SPAPR_XIVE_GET_CLASS(xive);
2953aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
2963aa597f6SCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
2973aa597f6SCédric Le Goater     Error *local_err = NULL;
2983aa597f6SCédric Le Goater 
2996cc64796SGreg Kurz     sxc->parent_realize(dev, &local_err);
3006cc64796SGreg Kurz     if (local_err) {
3016cc64796SGreg Kurz         error_propagate(errp, local_err);
3026cc64796SGreg Kurz         return;
3036cc64796SGreg Kurz     }
3046cc64796SGreg Kurz 
3053aa597f6SCédric Le Goater     if (!xive->nr_irqs) {
3063aa597f6SCédric Le Goater         error_setg(errp, "Number of interrupt needs to be greater 0");
3073aa597f6SCédric Le Goater         return;
3083aa597f6SCédric Le Goater     }
3093aa597f6SCédric Le Goater 
3103aa597f6SCédric Le Goater     if (!xive->nr_ends) {
3113aa597f6SCédric Le Goater         error_setg(errp, "Number of interrupt needs to be greater 0");
3123aa597f6SCédric Le Goater         return;
3133aa597f6SCédric Le Goater     }
3143aa597f6SCédric Le Goater 
3153aa597f6SCédric Le Goater     /*
3163aa597f6SCédric Le Goater      * Initialize the internal sources, for IPIs and virtual devices.
3173aa597f6SCédric Le Goater      */
3185325cc34SMarkus Armbruster     object_property_set_int(OBJECT(xsrc), "nr-irqs", xive->nr_irqs,
3193aa597f6SCédric Le Goater                             &error_fatal);
3205325cc34SMarkus Armbruster     object_property_set_link(OBJECT(xsrc), "xive", OBJECT(xive), &error_abort);
321668f62ecSMarkus Armbruster     if (!qdev_realize(DEVICE(xsrc), NULL, errp)) {
3223aa597f6SCédric Le Goater         return;
3233aa597f6SCédric Le Goater     }
324981b1c62SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xsrc->esb_mmio);
3253aa597f6SCédric Le Goater 
3263aa597f6SCédric Le Goater     /*
3273aa597f6SCédric Le Goater      * Initialize the END ESB source
3283aa597f6SCédric Le Goater      */
3295325cc34SMarkus Armbruster     object_property_set_int(OBJECT(end_xsrc), "nr-ends", xive->nr_irqs,
3303aa597f6SCédric Le Goater                             &error_fatal);
3315325cc34SMarkus Armbruster     object_property_set_link(OBJECT(end_xsrc), "xive", OBJECT(xive),
3320ab2316eSGreg Kurz                              &error_abort);
333668f62ecSMarkus Armbruster     if (!qdev_realize(DEVICE(end_xsrc), NULL, errp)) {
3343aa597f6SCédric Le Goater         return;
3353aa597f6SCédric Le Goater     }
336981b1c62SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &end_xsrc->esb_mmio);
3373aa597f6SCédric Le Goater 
3383aa597f6SCédric Le Goater     /* Set the mapping address of the END ESB pages after the source ESBs */
3393110f0eeSGreg Kurz     xive->end_base = xive->vc_base + xive_source_esb_len(xsrc);
3403aa597f6SCédric Le Goater 
3413aa597f6SCédric Le Goater     /*
3423aa597f6SCédric Le Goater      * Allocate the routing tables
3433aa597f6SCédric Le Goater      */
3443aa597f6SCédric Le Goater     xive->eat = g_new0(XiveEAS, xive->nr_irqs);
3453aa597f6SCédric Le Goater     xive->endt = g_new0(XiveEND, xive->nr_ends);
3463aa597f6SCédric Le Goater 
34738afd772SCédric Le Goater     xive->nodename = g_strdup_printf("interrupt-controller@%" PRIx64,
34838afd772SCédric Le Goater                            xive->tm_base + XIVE_TM_USER_PAGE * (1 << TM_SHIFT));
34938afd772SCédric Le Goater 
35038afd772SCédric Le Goater     qemu_register_reset(spapr_xive_reset, dev);
351cdd71c8eSCédric Le Goater 
3523aa597f6SCédric Le Goater     /* TIMA initialization */
353d024a2c1SCédric Le Goater     memory_region_init_io(&xive->tm_mmio, OBJECT(xive), &spapr_xive_tm_ops,
354d024a2c1SCédric Le Goater                           xive, "xive.tima", 4ull << TM_SHIFT);
355981b1c62SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xive->tm_mmio);
3563aa597f6SCédric Le Goater 
357981b1c62SCédric Le Goater     /*
358981b1c62SCédric Le Goater      * Map all regions. These will be enabled or disabled at reset and
359981b1c62SCédric Le Goater      * can also be overridden by KVM memory regions if active
360981b1c62SCédric Le Goater      */
361981b1c62SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->vc_base);
362981b1c62SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1, xive->end_base);
363981b1c62SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 2, xive->tm_base);
3643aa597f6SCédric Le Goater }
3653aa597f6SCédric Le Goater 
3663aa597f6SCédric Le Goater static int spapr_xive_get_eas(XiveRouter *xrtr, uint8_t eas_blk,
3673aa597f6SCédric Le Goater                               uint32_t eas_idx, XiveEAS *eas)
3683aa597f6SCédric Le Goater {
369ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3703aa597f6SCédric Le Goater 
3713aa597f6SCédric Le Goater     if (eas_idx >= xive->nr_irqs) {
3723aa597f6SCédric Le Goater         return -1;
3733aa597f6SCédric Le Goater     }
3743aa597f6SCédric Le Goater 
3753aa597f6SCédric Le Goater     *eas = xive->eat[eas_idx];
3763aa597f6SCédric Le Goater     return 0;
3773aa597f6SCédric Le Goater }
3783aa597f6SCédric Le Goater 
3793aa597f6SCédric Le Goater static int spapr_xive_get_end(XiveRouter *xrtr,
3803aa597f6SCédric Le Goater                               uint8_t end_blk, uint32_t end_idx, XiveEND *end)
3813aa597f6SCédric Le Goater {
382ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3833aa597f6SCédric Le Goater 
3843aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
3853aa597f6SCédric Le Goater         return -1;
3863aa597f6SCédric Le Goater     }
3873aa597f6SCédric Le Goater 
3883aa597f6SCédric Le Goater     memcpy(end, &xive->endt[end_idx], sizeof(XiveEND));
3893aa597f6SCédric Le Goater     return 0;
3903aa597f6SCédric Le Goater }
3913aa597f6SCédric Le Goater 
3923aa597f6SCédric Le Goater static int spapr_xive_write_end(XiveRouter *xrtr, uint8_t end_blk,
3933aa597f6SCédric Le Goater                                 uint32_t end_idx, XiveEND *end,
3943aa597f6SCédric Le Goater                                 uint8_t word_number)
3953aa597f6SCédric Le Goater {
396ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3973aa597f6SCédric Le Goater 
3983aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
3993aa597f6SCédric Le Goater         return -1;
4003aa597f6SCédric Le Goater     }
4013aa597f6SCédric Le Goater 
4023aa597f6SCédric Le Goater     memcpy(&xive->endt[end_idx], end, sizeof(XiveEND));
4033aa597f6SCédric Le Goater     return 0;
4043aa597f6SCédric Le Goater }
4053aa597f6SCédric Le Goater 
4060cddee8dSCédric Le Goater static int spapr_xive_get_nvt(XiveRouter *xrtr,
4070cddee8dSCédric Le Goater                               uint8_t nvt_blk, uint32_t nvt_idx, XiveNVT *nvt)
4080cddee8dSCédric Le Goater {
4090cddee8dSCédric Le Goater     uint32_t vcpu_id = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
4100cddee8dSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
4110cddee8dSCédric Le Goater 
4120cddee8dSCédric Le Goater     if (!cpu) {
4130cddee8dSCédric Le Goater         /* TODO: should we assert() if we can find a NVT ? */
4140cddee8dSCédric Le Goater         return -1;
4150cddee8dSCédric Le Goater     }
4160cddee8dSCédric Le Goater 
4170cddee8dSCédric Le Goater     /*
4180cddee8dSCédric Le Goater      * sPAPR does not maintain a NVT table. Return that the NVT is
4190cddee8dSCédric Le Goater      * valid if we have found a matching CPU
4200cddee8dSCédric Le Goater      */
4210cddee8dSCédric Le Goater     nvt->w0 = cpu_to_be32(NVT_W0_VALID);
4220cddee8dSCédric Le Goater     return 0;
4230cddee8dSCédric Le Goater }
4240cddee8dSCédric Le Goater 
4250cddee8dSCédric Le Goater static int spapr_xive_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk,
4260cddee8dSCédric Le Goater                                 uint32_t nvt_idx, XiveNVT *nvt,
4270cddee8dSCédric Le Goater                                 uint8_t word_number)
4280cddee8dSCédric Le Goater {
4290cddee8dSCédric Le Goater     /*
4300cddee8dSCédric Le Goater      * We don't need to write back to the NVTs because the sPAPR
4310cddee8dSCédric Le Goater      * machine should never hit a non-scheduled NVT. It should never
4320cddee8dSCédric Le Goater      * get called.
4330cddee8dSCédric Le Goater      */
4340cddee8dSCédric Le Goater     g_assert_not_reached();
4350cddee8dSCédric Le Goater }
4360cddee8dSCédric Le Goater 
437f87dae18SCédric Le Goater static int spapr_xive_match_nvt(XivePresenter *xptr, uint8_t format,
438f87dae18SCédric Le Goater                                 uint8_t nvt_blk, uint32_t nvt_idx,
439f87dae18SCédric Le Goater                                 bool cam_ignore, uint8_t priority,
440f87dae18SCédric Le Goater                                 uint32_t logic_serv, XiveTCTXMatch *match)
441f87dae18SCédric Le Goater {
442f87dae18SCédric Le Goater     CPUState *cs;
443f87dae18SCédric Le Goater     int count = 0;
444f87dae18SCédric Le Goater 
445f87dae18SCédric Le Goater     CPU_FOREACH(cs) {
446f87dae18SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
447f87dae18SCédric Le Goater         XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx;
448f87dae18SCédric Le Goater         int ring;
449f87dae18SCédric Le Goater 
450f87dae18SCédric Le Goater         /*
451f87dae18SCédric Le Goater          * Skip partially initialized vCPUs. This can happen when
452f87dae18SCédric Le Goater          * vCPUs are hotplugged.
453f87dae18SCédric Le Goater          */
454f87dae18SCédric Le Goater         if (!tctx) {
455f87dae18SCédric Le Goater             continue;
456f87dae18SCédric Le Goater         }
457f87dae18SCédric Le Goater 
458f87dae18SCédric Le Goater         /*
459f87dae18SCédric Le Goater          * Check the thread context CAM lines and record matches.
460f87dae18SCédric Le Goater          */
461f87dae18SCédric Le Goater         ring = xive_presenter_tctx_match(xptr, tctx, format, nvt_blk, nvt_idx,
462f87dae18SCédric Le Goater                                          cam_ignore, logic_serv);
463f87dae18SCédric Le Goater         /*
464f87dae18SCédric Le Goater          * Save the matching thread interrupt context and follow on to
465f87dae18SCédric Le Goater          * check for duplicates which are invalid.
466f87dae18SCédric Le Goater          */
467f87dae18SCédric Le Goater         if (ring != -1) {
468f87dae18SCédric Le Goater             if (match->tctx) {
469f87dae18SCédric Le Goater                 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: already found a thread "
470f87dae18SCédric Le Goater                               "context NVT %x/%x\n", nvt_blk, nvt_idx);
471f87dae18SCédric Le Goater                 return -1;
472f87dae18SCédric Le Goater             }
473f87dae18SCédric Le Goater 
474f87dae18SCédric Le Goater             match->ring = ring;
475f87dae18SCédric Le Goater             match->tctx = tctx;
476f87dae18SCédric Le Goater             count++;
477f87dae18SCédric Le Goater         }
478f87dae18SCédric Le Goater     }
479f87dae18SCédric Le Goater 
480f87dae18SCédric Le Goater     return count;
481f87dae18SCédric Le Goater }
482f87dae18SCédric Le Goater 
483f22f56ddSCédric Le Goater static uint8_t spapr_xive_get_block_id(XiveRouter *xrtr)
484f22f56ddSCédric Le Goater {
485f22f56ddSCédric Le Goater     return SPAPR_XIVE_BLOCK_ID;
486f22f56ddSCédric Le Goater }
487f22f56ddSCédric Le Goater 
4883aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_end = {
4893aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/end",
4903aa597f6SCédric Le Goater     .version_id = 1,
4913aa597f6SCédric Le Goater     .minimum_version_id = 1,
4923aa597f6SCédric Le Goater     .fields = (VMStateField []) {
4933aa597f6SCédric Le Goater         VMSTATE_UINT32(w0, XiveEND),
4943aa597f6SCédric Le Goater         VMSTATE_UINT32(w1, XiveEND),
4953aa597f6SCédric Le Goater         VMSTATE_UINT32(w2, XiveEND),
4963aa597f6SCédric Le Goater         VMSTATE_UINT32(w3, XiveEND),
4973aa597f6SCédric Le Goater         VMSTATE_UINT32(w4, XiveEND),
4983aa597f6SCédric Le Goater         VMSTATE_UINT32(w5, XiveEND),
4993aa597f6SCédric Le Goater         VMSTATE_UINT32(w6, XiveEND),
5003aa597f6SCédric Le Goater         VMSTATE_UINT32(w7, XiveEND),
5013aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
5023aa597f6SCédric Le Goater     },
5033aa597f6SCédric Le Goater };
5043aa597f6SCédric Le Goater 
5053aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_eas = {
5063aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/eas",
5073aa597f6SCédric Le Goater     .version_id = 1,
5083aa597f6SCédric Le Goater     .minimum_version_id = 1,
5093aa597f6SCédric Le Goater     .fields = (VMStateField []) {
5103aa597f6SCédric Le Goater         VMSTATE_UINT64(w, XiveEAS),
5113aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
5123aa597f6SCédric Le Goater     },
5133aa597f6SCédric Le Goater };
5143aa597f6SCédric Le Goater 
515277dd3d7SCédric Le Goater static int vmstate_spapr_xive_pre_save(void *opaque)
516277dd3d7SCédric Le Goater {
517e519cdd9SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(opaque);
518e519cdd9SGreg Kurz 
519e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
520e519cdd9SGreg Kurz         return kvmppc_xive_pre_save(xive);
521277dd3d7SCédric Le Goater     }
522277dd3d7SCédric Le Goater 
523277dd3d7SCédric Le Goater     return 0;
524277dd3d7SCédric Le Goater }
525277dd3d7SCédric Le Goater 
526277dd3d7SCédric Le Goater /*
527277dd3d7SCédric Le Goater  * Called by the sPAPR IRQ backend 'post_load' method at the machine
528277dd3d7SCédric Le Goater  * level.
529277dd3d7SCédric Le Goater  */
530605994e5SDavid Gibson static int spapr_xive_post_load(SpaprInterruptController *intc, int version_id)
531277dd3d7SCédric Le Goater {
532e519cdd9SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(intc);
533e519cdd9SGreg Kurz 
534e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
535e519cdd9SGreg Kurz         return kvmppc_xive_post_load(xive, version_id);
536277dd3d7SCédric Le Goater     }
537277dd3d7SCédric Le Goater 
538277dd3d7SCédric Le Goater     return 0;
539277dd3d7SCédric Le Goater }
540277dd3d7SCédric Le Goater 
5413aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive = {
5423aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
5433aa597f6SCédric Le Goater     .version_id = 1,
5443aa597f6SCédric Le Goater     .minimum_version_id = 1,
545277dd3d7SCédric Le Goater     .pre_save = vmstate_spapr_xive_pre_save,
546277dd3d7SCédric Le Goater     .post_load = NULL, /* handled at the machine level */
5473aa597f6SCédric Le Goater     .fields = (VMStateField[]) {
548ce2918cbSDavid Gibson         VMSTATE_UINT32_EQUAL(nr_irqs, SpaprXive, NULL),
549ce2918cbSDavid Gibson         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(eat, SpaprXive, nr_irqs,
5503aa597f6SCédric Le Goater                                      vmstate_spapr_xive_eas, XiveEAS),
551ce2918cbSDavid Gibson         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(endt, SpaprXive, nr_ends,
5523aa597f6SCédric Le Goater                                              vmstate_spapr_xive_end, XiveEND),
5533aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
5543aa597f6SCédric Le Goater     },
5553aa597f6SCédric Le Goater };
5563aa597f6SCédric Le Goater 
5570b0e52b1SDavid Gibson static int spapr_xive_claim_irq(SpaprInterruptController *intc, int lisn,
5580b0e52b1SDavid Gibson                                 bool lsi, Error **errp)
5590b0e52b1SDavid Gibson {
5600b0e52b1SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
5610b0e52b1SDavid Gibson     XiveSource *xsrc = &xive->source;
5620b0e52b1SDavid Gibson 
5630b0e52b1SDavid Gibson     assert(lisn < xive->nr_irqs);
5640b0e52b1SDavid Gibson 
5650b0e52b1SDavid Gibson     if (xive_eas_is_valid(&xive->eat[lisn])) {
5660b0e52b1SDavid Gibson         error_setg(errp, "IRQ %d is not free", lisn);
5670b0e52b1SDavid Gibson         return -EBUSY;
5680b0e52b1SDavid Gibson     }
5690b0e52b1SDavid Gibson 
5700b0e52b1SDavid Gibson     /*
5710b0e52b1SDavid Gibson      * Set default values when allocating an IRQ number
5720b0e52b1SDavid Gibson      */
5730b0e52b1SDavid Gibson     xive->eat[lisn].w |= cpu_to_be64(EAS_VALID | EAS_MASKED);
5740b0e52b1SDavid Gibson     if (lsi) {
5750b0e52b1SDavid Gibson         xive_source_irq_set_lsi(xsrc, lisn);
5760b0e52b1SDavid Gibson     }
5770b0e52b1SDavid Gibson 
578e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
5790b0e52b1SDavid Gibson         return kvmppc_xive_source_reset_one(xsrc, lisn, errp);
5800b0e52b1SDavid Gibson     }
5810b0e52b1SDavid Gibson 
5820b0e52b1SDavid Gibson     return 0;
5830b0e52b1SDavid Gibson }
5840b0e52b1SDavid Gibson 
5850b0e52b1SDavid Gibson static void spapr_xive_free_irq(SpaprInterruptController *intc, int lisn)
5860b0e52b1SDavid Gibson {
5870b0e52b1SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
5880b0e52b1SDavid Gibson     assert(lisn < xive->nr_irqs);
5890b0e52b1SDavid Gibson 
5900b0e52b1SDavid Gibson     xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
5910b0e52b1SDavid Gibson }
5920b0e52b1SDavid Gibson 
5933aa597f6SCédric Le Goater static Property spapr_xive_properties[] = {
594ce2918cbSDavid Gibson     DEFINE_PROP_UINT32("nr-irqs", SpaprXive, nr_irqs, 0),
595ce2918cbSDavid Gibson     DEFINE_PROP_UINT32("nr-ends", SpaprXive, nr_ends, 0),
596ce2918cbSDavid Gibson     DEFINE_PROP_UINT64("vc-base", SpaprXive, vc_base, SPAPR_XIVE_VC_BASE),
597ce2918cbSDavid Gibson     DEFINE_PROP_UINT64("tm-base", SpaprXive, tm_base, SPAPR_XIVE_TM_BASE),
598*4f311a70SCédric Le Goater     DEFINE_PROP_UINT8("hv-prio", SpaprXive, hv_prio, 7),
5993aa597f6SCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
6003aa597f6SCédric Le Goater };
6013aa597f6SCédric Le Goater 
602ebd6be08SDavid Gibson static int spapr_xive_cpu_intc_create(SpaprInterruptController *intc,
603ebd6be08SDavid Gibson                                       PowerPCCPU *cpu, Error **errp)
604ebd6be08SDavid Gibson {
605ebd6be08SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
606ebd6be08SDavid Gibson     Object *obj;
607ebd6be08SDavid Gibson     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
608ebd6be08SDavid Gibson 
60947950946SCédric Le Goater     obj = xive_tctx_create(OBJECT(cpu), XIVE_PRESENTER(xive), errp);
610ebd6be08SDavid Gibson     if (!obj) {
611ebd6be08SDavid Gibson         return -1;
612ebd6be08SDavid Gibson     }
613ebd6be08SDavid Gibson 
614ebd6be08SDavid Gibson     spapr_cpu->tctx = XIVE_TCTX(obj);
615ebd6be08SDavid Gibson     return 0;
616ebd6be08SDavid Gibson }
617ebd6be08SDavid Gibson 
61897c00c54SCédric Le Goater static void xive_tctx_set_os_cam(XiveTCTX *tctx, uint32_t os_cam)
61997c00c54SCédric Le Goater {
62097c00c54SCédric Le Goater     uint32_t qw1w2 = cpu_to_be32(TM_QW1W2_VO | os_cam);
62197c00c54SCédric Le Goater     memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1w2, 4);
62297c00c54SCédric Le Goater }
62397c00c54SCédric Le Goater 
624d49e8a9bSCédric Le Goater static void spapr_xive_cpu_intc_reset(SpaprInterruptController *intc,
625d49e8a9bSCédric Le Goater                                      PowerPCCPU *cpu)
626d49e8a9bSCédric Le Goater {
627d49e8a9bSCédric Le Goater     XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx;
62897c00c54SCédric Le Goater     uint8_t  nvt_blk;
62997c00c54SCédric Le Goater     uint32_t nvt_idx;
630d49e8a9bSCédric Le Goater 
631d49e8a9bSCédric Le Goater     xive_tctx_reset(tctx);
63297c00c54SCédric Le Goater 
63397c00c54SCédric Le Goater     /*
63497c00c54SCédric Le Goater      * When a Virtual Processor is scheduled to run on a HW thread,
63597c00c54SCédric Le Goater      * the hypervisor pushes its identifier in the OS CAM line.
63697c00c54SCédric Le Goater      * Emulate the same behavior under QEMU.
63797c00c54SCédric Le Goater      */
63897c00c54SCédric Le Goater     spapr_xive_cpu_to_nvt(cpu, &nvt_blk, &nvt_idx);
63997c00c54SCédric Le Goater 
64097c00c54SCédric Le Goater     xive_tctx_set_os_cam(tctx, xive_nvt_cam_line(nvt_blk, nvt_idx));
641d49e8a9bSCédric Le Goater }
642d49e8a9bSCédric Le Goater 
6430990ce6aSGreg Kurz static void spapr_xive_cpu_intc_destroy(SpaprInterruptController *intc,
6440990ce6aSGreg Kurz                                         PowerPCCPU *cpu)
6450990ce6aSGreg Kurz {
6460990ce6aSGreg Kurz     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
6470990ce6aSGreg Kurz 
6480990ce6aSGreg Kurz     xive_tctx_destroy(spapr_cpu->tctx);
6490990ce6aSGreg Kurz     spapr_cpu->tctx = NULL;
6500990ce6aSGreg Kurz }
6510990ce6aSGreg Kurz 
6527bcdbccaSDavid Gibson static void spapr_xive_set_irq(SpaprInterruptController *intc, int irq, int val)
6537bcdbccaSDavid Gibson {
6547bcdbccaSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
6557bcdbccaSDavid Gibson 
656e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
6577bcdbccaSDavid Gibson         kvmppc_xive_source_set_irq(&xive->source, irq, val);
6587bcdbccaSDavid Gibson     } else {
6597bcdbccaSDavid Gibson         xive_source_set_irq(&xive->source, irq, val);
6607bcdbccaSDavid Gibson     }
6617bcdbccaSDavid Gibson }
6627bcdbccaSDavid Gibson 
663328d8eb2SDavid Gibson static void spapr_xive_print_info(SpaprInterruptController *intc, Monitor *mon)
664328d8eb2SDavid Gibson {
665328d8eb2SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
666328d8eb2SDavid Gibson     CPUState *cs;
667328d8eb2SDavid Gibson 
668328d8eb2SDavid Gibson     CPU_FOREACH(cs) {
669328d8eb2SDavid Gibson         PowerPCCPU *cpu = POWERPC_CPU(cs);
670328d8eb2SDavid Gibson 
671328d8eb2SDavid Gibson         xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, mon);
672328d8eb2SDavid Gibson     }
673328d8eb2SDavid Gibson 
674328d8eb2SDavid Gibson     spapr_xive_pic_print_info(xive, mon);
675328d8eb2SDavid Gibson }
676328d8eb2SDavid Gibson 
67705289273SDavid Gibson static void spapr_xive_dt(SpaprInterruptController *intc, uint32_t nr_servers,
67805289273SDavid Gibson                           void *fdt, uint32_t phandle)
67905289273SDavid Gibson {
68005289273SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
68105289273SDavid Gibson     int node;
68205289273SDavid Gibson     uint64_t timas[2 * 2];
68305289273SDavid Gibson     /* Interrupt number ranges for the IPIs */
68405289273SDavid Gibson     uint32_t lisn_ranges[] = {
68552d3403dSCédric Le Goater         cpu_to_be32(SPAPR_IRQ_IPI),
68652d3403dSCédric Le Goater         cpu_to_be32(SPAPR_IRQ_IPI + nr_servers),
68705289273SDavid Gibson     };
68805289273SDavid Gibson     /*
68905289273SDavid Gibson      * EQ size - the sizes of pages supported by the system 4K, 64K,
69005289273SDavid Gibson      * 2M, 16M. We only advertise 64K for the moment.
69105289273SDavid Gibson      */
69205289273SDavid Gibson     uint32_t eq_sizes[] = {
69305289273SDavid Gibson         cpu_to_be32(16), /* 64K */
69405289273SDavid Gibson     };
69505289273SDavid Gibson     /*
696*4f311a70SCédric Le Goater      * QEMU/KVM only needs to define a single range to reserve the
697*4f311a70SCédric Le Goater      * escalation priority. A priority bitmask would have been more
698*4f311a70SCédric Le Goater      * appropriate.
69905289273SDavid Gibson      */
70005289273SDavid Gibson     uint32_t plat_res_int_priorities[] = {
701*4f311a70SCédric Le Goater         cpu_to_be32(xive->hv_prio),    /* start */
702*4f311a70SCédric Le Goater         cpu_to_be32(0xff - xive->hv_prio), /* count */
70305289273SDavid Gibson     };
70405289273SDavid Gibson 
70505289273SDavid Gibson     /* Thread Interrupt Management Area : User (ring 3) and OS (ring 2) */
70605289273SDavid Gibson     timas[0] = cpu_to_be64(xive->tm_base +
70705289273SDavid Gibson                            XIVE_TM_USER_PAGE * (1ull << TM_SHIFT));
70805289273SDavid Gibson     timas[1] = cpu_to_be64(1ull << TM_SHIFT);
70905289273SDavid Gibson     timas[2] = cpu_to_be64(xive->tm_base +
71005289273SDavid Gibson                            XIVE_TM_OS_PAGE * (1ull << TM_SHIFT));
71105289273SDavid Gibson     timas[3] = cpu_to_be64(1ull << TM_SHIFT);
71205289273SDavid Gibson 
71305289273SDavid Gibson     _FDT(node = fdt_add_subnode(fdt, 0, xive->nodename));
71405289273SDavid Gibson 
71505289273SDavid Gibson     _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
71605289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
71705289273SDavid Gibson 
71805289273SDavid Gibson     _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
71905289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
72005289273SDavid Gibson                      sizeof(eq_sizes)));
72105289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
72205289273SDavid Gibson                      sizeof(lisn_ranges)));
72305289273SDavid Gibson 
72405289273SDavid Gibson     /* For Linux to link the LSIs to the interrupt controller. */
72505289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "interrupt-controller", NULL, 0));
72605289273SDavid Gibson     _FDT(fdt_setprop_cell(fdt, node, "#interrupt-cells", 2));
72705289273SDavid Gibson 
72805289273SDavid Gibson     /* For SLOF */
72905289273SDavid Gibson     _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
73005289273SDavid Gibson     _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
73105289273SDavid Gibson 
73205289273SDavid Gibson     /*
73305289273SDavid Gibson      * The "ibm,plat-res-int-priorities" property defines the priority
73405289273SDavid Gibson      * ranges reserved by the hypervisor
73505289273SDavid Gibson      */
73605289273SDavid Gibson     _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
73705289273SDavid Gibson                      plat_res_int_priorities, sizeof(plat_res_int_priorities)));
73805289273SDavid Gibson }
73905289273SDavid Gibson 
7404ffb7496SGreg Kurz static int spapr_xive_activate(SpaprInterruptController *intc,
7414ffb7496SGreg Kurz                                uint32_t nr_servers, Error **errp)
742567192d4SDavid Gibson {
743567192d4SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
744567192d4SDavid Gibson 
745567192d4SDavid Gibson     if (kvm_enabled()) {
7464ffb7496SGreg Kurz         int rc = spapr_irq_init_kvm(kvmppc_xive_connect, intc, nr_servers,
7474ffb7496SGreg Kurz                                     errp);
748567192d4SDavid Gibson         if (rc < 0) {
749567192d4SDavid Gibson             return rc;
750567192d4SDavid Gibson         }
751567192d4SDavid Gibson     }
752567192d4SDavid Gibson 
753567192d4SDavid Gibson     /* Activate the XIVE MMIOs */
754567192d4SDavid Gibson     spapr_xive_mmio_set_enabled(xive, true);
755567192d4SDavid Gibson 
756567192d4SDavid Gibson     return 0;
757567192d4SDavid Gibson }
758567192d4SDavid Gibson 
759567192d4SDavid Gibson static void spapr_xive_deactivate(SpaprInterruptController *intc)
760567192d4SDavid Gibson {
761567192d4SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
762567192d4SDavid Gibson 
763567192d4SDavid Gibson     spapr_xive_mmio_set_enabled(xive, false);
764567192d4SDavid Gibson 
765e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
766567192d4SDavid Gibson         kvmppc_xive_disconnect(intc);
767567192d4SDavid Gibson     }
768567192d4SDavid Gibson }
769567192d4SDavid Gibson 
770e519cdd9SGreg Kurz static bool spapr_xive_in_kernel_xptr(const XivePresenter *xptr)
771e519cdd9SGreg Kurz {
772e519cdd9SGreg Kurz     return spapr_xive_in_kernel(SPAPR_XIVE(xptr));
773e519cdd9SGreg Kurz }
774e519cdd9SGreg Kurz 
7753aa597f6SCédric Le Goater static void spapr_xive_class_init(ObjectClass *klass, void *data)
7763aa597f6SCédric Le Goater {
7773aa597f6SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
7783aa597f6SCédric Le Goater     XiveRouterClass *xrc = XIVE_ROUTER_CLASS(klass);
779ebd6be08SDavid Gibson     SpaprInterruptControllerClass *sicc = SPAPR_INTC_CLASS(klass);
780f87dae18SCédric Le Goater     XivePresenterClass *xpc = XIVE_PRESENTER_CLASS(klass);
7816cc64796SGreg Kurz     SpaprXiveClass *sxc = SPAPR_XIVE_CLASS(klass);
7823aa597f6SCédric Le Goater 
7833aa597f6SCédric Le Goater     dc->desc    = "sPAPR XIVE Interrupt Controller";
7844f67d30bSMarc-André Lureau     device_class_set_props(dc, spapr_xive_properties);
7856cc64796SGreg Kurz     device_class_set_parent_realize(dc, spapr_xive_realize,
7866cc64796SGreg Kurz                                     &sxc->parent_realize);
7873aa597f6SCédric Le Goater     dc->vmsd    = &vmstate_spapr_xive;
7883aa597f6SCédric Le Goater 
7893aa597f6SCédric Le Goater     xrc->get_eas = spapr_xive_get_eas;
7903aa597f6SCédric Le Goater     xrc->get_end = spapr_xive_get_end;
7913aa597f6SCédric Le Goater     xrc->write_end = spapr_xive_write_end;
7920cddee8dSCédric Le Goater     xrc->get_nvt = spapr_xive_get_nvt;
7930cddee8dSCédric Le Goater     xrc->write_nvt = spapr_xive_write_nvt;
794f22f56ddSCédric Le Goater     xrc->get_block_id = spapr_xive_get_block_id;
795ebd6be08SDavid Gibson 
796567192d4SDavid Gibson     sicc->activate = spapr_xive_activate;
797567192d4SDavid Gibson     sicc->deactivate = spapr_xive_deactivate;
798ebd6be08SDavid Gibson     sicc->cpu_intc_create = spapr_xive_cpu_intc_create;
799d49e8a9bSCédric Le Goater     sicc->cpu_intc_reset = spapr_xive_cpu_intc_reset;
8000990ce6aSGreg Kurz     sicc->cpu_intc_destroy = spapr_xive_cpu_intc_destroy;
8010b0e52b1SDavid Gibson     sicc->claim_irq = spapr_xive_claim_irq;
8020b0e52b1SDavid Gibson     sicc->free_irq = spapr_xive_free_irq;
8037bcdbccaSDavid Gibson     sicc->set_irq = spapr_xive_set_irq;
804328d8eb2SDavid Gibson     sicc->print_info = spapr_xive_print_info;
80505289273SDavid Gibson     sicc->dt = spapr_xive_dt;
806605994e5SDavid Gibson     sicc->post_load = spapr_xive_post_load;
807f87dae18SCédric Le Goater 
808f87dae18SCédric Le Goater     xpc->match_nvt  = spapr_xive_match_nvt;
809e519cdd9SGreg Kurz     xpc->in_kernel  = spapr_xive_in_kernel_xptr;
8103aa597f6SCédric Le Goater }
8113aa597f6SCédric Le Goater 
8123aa597f6SCédric Le Goater static const TypeInfo spapr_xive_info = {
8133aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
8143aa597f6SCédric Le Goater     .parent = TYPE_XIVE_ROUTER,
8153aa597f6SCédric Le Goater     .instance_init = spapr_xive_instance_init,
816ce2918cbSDavid Gibson     .instance_size = sizeof(SpaprXive),
8173aa597f6SCédric Le Goater     .class_init = spapr_xive_class_init,
8186cc64796SGreg Kurz     .class_size = sizeof(SpaprXiveClass),
819150e25f8SDavid Gibson     .interfaces = (InterfaceInfo[]) {
820150e25f8SDavid Gibson         { TYPE_SPAPR_INTC },
821150e25f8SDavid Gibson         { }
822150e25f8SDavid Gibson     },
8233aa597f6SCédric Le Goater };
8243aa597f6SCédric Le Goater 
8253aa597f6SCédric Le Goater static void spapr_xive_register_types(void)
8263aa597f6SCédric Le Goater {
8273aa597f6SCédric Le Goater     type_register_static(&spapr_xive_info);
8283aa597f6SCédric Le Goater }
8293aa597f6SCédric Le Goater 
8303aa597f6SCédric Le Goater type_init(spapr_xive_register_types)
8313aa597f6SCédric Le Goater 
83223bcd5ebSCédric Le Goater /*
83323bcd5ebSCédric Le Goater  * XIVE hcalls
83423bcd5ebSCédric Le Goater  *
83523bcd5ebSCédric Le Goater  * The terminology used by the XIVE hcalls is the following :
83623bcd5ebSCédric Le Goater  *
83723bcd5ebSCédric Le Goater  *   TARGET vCPU number
83823bcd5ebSCédric Le Goater  *   EQ     Event Queue assigned by OS to receive event data
83923bcd5ebSCédric Le Goater  *   ESB    page for source interrupt management
84023bcd5ebSCédric Le Goater  *   LISN   Logical Interrupt Source Number identifying a source in the
84123bcd5ebSCédric Le Goater  *          machine
84223bcd5ebSCédric Le Goater  *   EISN   Effective Interrupt Source Number used by guest OS to
84323bcd5ebSCédric Le Goater  *          identify source in the guest
84423bcd5ebSCédric Le Goater  *
84523bcd5ebSCédric Le Goater  * The EAS, END, NVT structures are not exposed.
84623bcd5ebSCédric Le Goater  */
84723bcd5ebSCédric Le Goater 
84823bcd5ebSCédric Le Goater /*
849*4f311a70SCédric Le Goater  * On POWER9, the KVM XIVE device uses priority 7 for the escalation
850*4f311a70SCédric Le Goater  * interrupts. So we only allow the guest to use priorities [0..6].
85123bcd5ebSCédric Le Goater  */
852*4f311a70SCédric Le Goater static bool spapr_xive_priority_is_reserved(SpaprXive *xive, uint8_t priority)
85323bcd5ebSCédric Le Goater {
854*4f311a70SCédric Le Goater     return priority >= xive->hv_prio;
85523bcd5ebSCédric Le Goater }
85623bcd5ebSCédric Le Goater 
85723bcd5ebSCédric Le Goater /*
85823bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical
85923bcd5ebSCédric Le Goater  * real address of the MMIO page through which the Event State Buffer
86023bcd5ebSCédric Le Goater  * entry associated with the value of the "lisn" parameter is managed.
86123bcd5ebSCédric Le Goater  *
86223bcd5ebSCédric Le Goater  * Parameters:
86323bcd5ebSCédric Le Goater  * Input
86423bcd5ebSCédric Le Goater  * - R4: "flags"
86523bcd5ebSCédric Le Goater  *         Bits 0-63 reserved
86623bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
86723bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
86823bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned
86923bcd5ebSCédric Le Goater  *       by the H_ALLOCATE_VAS_WINDOW hcall
87023bcd5ebSCédric Le Goater  *
87123bcd5ebSCédric Le Goater  * Output
87223bcd5ebSCédric Le Goater  * - R4: "flags"
87323bcd5ebSCédric Le Goater  *         Bits 0-59: Reserved
87423bcd5ebSCédric Le Goater  *         Bit 60: H_INT_ESB must be used for Event State Buffer
87523bcd5ebSCédric Le Goater  *                 management
87623bcd5ebSCédric Le Goater  *         Bit 61: 1 == LSI  0 == MSI
87723bcd5ebSCédric Le Goater  *         Bit 62: the full function page supports trigger
87823bcd5ebSCédric Le Goater  *         Bit 63: Store EOI Supported
87923bcd5ebSCédric Le Goater  * - R5: Logical Real address of full function Event State Buffer
88023bcd5ebSCédric Le Goater  *       management page, -1 if H_INT_ESB hcall flag is set to 1.
88123bcd5ebSCédric Le Goater  * - R6: Logical Real Address of trigger only Event State Buffer
88223bcd5ebSCédric Le Goater  *       management page or -1.
88323bcd5ebSCédric Le Goater  * - R7: Power of 2 page size for the ESB management pages returned in
88423bcd5ebSCédric Le Goater  *       R5 and R6.
88523bcd5ebSCédric Le Goater  */
88623bcd5ebSCédric Le Goater 
88723bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_H_INT_ESB     PPC_BIT(60) /* ESB manage with H_INT_ESB */
88823bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_LSI           PPC_BIT(61) /* Virtual LSI type */
88923bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_TRIGGER       PPC_BIT(62) /* Trigger and management
89023bcd5ebSCédric Le Goater                                                     on same page */
89123bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_STORE_EOI     PPC_BIT(63) /* Store EOI support */
89223bcd5ebSCédric Le Goater 
89323bcd5ebSCédric Le Goater static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
894ce2918cbSDavid Gibson                                           SpaprMachineState *spapr,
89523bcd5ebSCédric Le Goater                                           target_ulong opcode,
89623bcd5ebSCédric Le Goater                                           target_ulong *args)
89723bcd5ebSCédric Le Goater {
898ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
89923bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
90023bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
90123bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
90223bcd5ebSCédric Le Goater 
90323bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
90423bcd5ebSCédric Le Goater         return H_FUNCTION;
90523bcd5ebSCédric Le Goater     }
90623bcd5ebSCédric Le Goater 
90723bcd5ebSCédric Le Goater     if (flags) {
90823bcd5ebSCédric Le Goater         return H_PARAMETER;
90923bcd5ebSCédric Le Goater     }
91023bcd5ebSCédric Le Goater 
91123bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
91223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
91323bcd5ebSCédric Le Goater                       lisn);
91423bcd5ebSCédric Le Goater         return H_P2;
91523bcd5ebSCédric Le Goater     }
91623bcd5ebSCédric Le Goater 
91723bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&xive->eat[lisn])) {
91823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
91923bcd5ebSCédric Le Goater                       lisn);
92023bcd5ebSCédric Le Goater         return H_P2;
92123bcd5ebSCédric Le Goater     }
92223bcd5ebSCédric Le Goater 
92323bcd5ebSCédric Le Goater     /*
92423bcd5ebSCédric Le Goater      * All sources are emulated under the main XIVE object and share
92523bcd5ebSCédric Le Goater      * the same characteristics.
92623bcd5ebSCédric Le Goater      */
92723bcd5ebSCédric Le Goater     args[0] = 0;
92823bcd5ebSCédric Le Goater     if (!xive_source_esb_has_2page(xsrc)) {
92923bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_TRIGGER;
93023bcd5ebSCédric Le Goater     }
93123bcd5ebSCédric Le Goater     if (xsrc->esb_flags & XIVE_SRC_STORE_EOI) {
93223bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_STORE_EOI;
93323bcd5ebSCédric Le Goater     }
93423bcd5ebSCédric Le Goater 
93523bcd5ebSCédric Le Goater     /*
93623bcd5ebSCédric Le Goater      * Force the use of the H_INT_ESB hcall in case of an LSI
93723bcd5ebSCédric Le Goater      * interrupt. This is necessary under KVM to re-trigger the
93823bcd5ebSCédric Le Goater      * interrupt if the level is still asserted
93923bcd5ebSCédric Le Goater      */
94023bcd5ebSCédric Le Goater     if (xive_source_irq_is_lsi(xsrc, lisn)) {
94123bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_H_INT_ESB | SPAPR_XIVE_SRC_LSI;
94223bcd5ebSCédric Le Goater     }
94323bcd5ebSCédric Le Goater 
94423bcd5ebSCédric Le Goater     if (!(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
94523bcd5ebSCédric Le Goater         args[1] = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn);
94623bcd5ebSCédric Le Goater     } else {
94723bcd5ebSCédric Le Goater         args[1] = -1;
94823bcd5ebSCédric Le Goater     }
94923bcd5ebSCédric Le Goater 
95023bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc) &&
95123bcd5ebSCédric Le Goater         !(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
95223bcd5ebSCédric Le Goater         args[2] = xive->vc_base + xive_source_esb_page(xsrc, lisn);
95323bcd5ebSCédric Le Goater     } else {
95423bcd5ebSCédric Le Goater         args[2] = -1;
95523bcd5ebSCédric Le Goater     }
95623bcd5ebSCédric Le Goater 
95723bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc)) {
95823bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift - 1;
95923bcd5ebSCédric Le Goater     } else {
96023bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift;
96123bcd5ebSCédric Le Goater     }
96223bcd5ebSCédric Le Goater 
96323bcd5ebSCédric Le Goater     return H_SUCCESS;
96423bcd5ebSCédric Le Goater }
96523bcd5ebSCédric Le Goater 
96623bcd5ebSCédric Le Goater /*
96723bcd5ebSCédric Le Goater  * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
96823bcd5ebSCédric Le Goater  * Interrupt Source to a target. The Logical Interrupt Source is
96923bcd5ebSCédric Le Goater  * designated with the "lisn" parameter and the target is designated
97023bcd5ebSCédric Le Goater  * with the "target" and "priority" parameters.  Upon return from the
97123bcd5ebSCédric Le Goater  * hcall(), no additional interrupts will be directed to the old EQ.
97223bcd5ebSCédric Le Goater  *
97323bcd5ebSCédric Le Goater  * Parameters:
97423bcd5ebSCédric Le Goater  * Input:
97523bcd5ebSCédric Le Goater  * - R4: "flags"
97623bcd5ebSCédric Le Goater  *         Bits 0-61: Reserved
97723bcd5ebSCédric Le Goater  *         Bit 62: set the "eisn" in the EAS
97823bcd5ebSCédric Le Goater  *         Bit 63: masks the interrupt source in the hardware interrupt
97923bcd5ebSCédric Le Goater  *       control structure. An interrupt masked by this mechanism will
98023bcd5ebSCédric Le Goater  *       be dropped, but it's source state bits will still be
98123bcd5ebSCédric Le Goater  *       set. There is no race-free way of unmasking and restoring the
98223bcd5ebSCédric Le Goater  *       source. Thus this should only be used in interrupts that are
98323bcd5ebSCédric Le Goater  *       also masked at the source, and only in cases where the
98423bcd5ebSCédric Le Goater  *       interrupt is not meant to be used for a large amount of time
98523bcd5ebSCédric Le Goater  *       because no valid target exists for it for example
98623bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
98723bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
98823bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned by
98923bcd5ebSCédric Le Goater  *       the H_ALLOCATE_VAS_WINDOW hcall
99023bcd5ebSCédric Le Goater  * - R6: "target" is per "ibm,ppc-interrupt-server#s" or
99123bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
99223bcd5ebSCédric Le Goater  * - R7: "priority" is a valid priority not in
99323bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
99423bcd5ebSCédric Le Goater  * - R8: "eisn" is the guest EISN associated with the "lisn"
99523bcd5ebSCédric Le Goater  *
99623bcd5ebSCédric Le Goater  * Output:
99723bcd5ebSCédric Le Goater  * - None
99823bcd5ebSCédric Le Goater  */
99923bcd5ebSCédric Le Goater 
100023bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_SET_EISN PPC_BIT(62)
100123bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_MASK     PPC_BIT(63)
100223bcd5ebSCédric Le Goater 
100323bcd5ebSCédric Le Goater static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
1004ce2918cbSDavid Gibson                                             SpaprMachineState *spapr,
100523bcd5ebSCédric Le Goater                                             target_ulong opcode,
100623bcd5ebSCédric Le Goater                                             target_ulong *args)
100723bcd5ebSCédric Le Goater {
1008ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
100923bcd5ebSCédric Le Goater     XiveEAS eas, new_eas;
101023bcd5ebSCédric Le Goater     target_ulong flags    = args[0];
101123bcd5ebSCédric Le Goater     target_ulong lisn     = args[1];
101223bcd5ebSCédric Le Goater     target_ulong target   = args[2];
101323bcd5ebSCédric Le Goater     target_ulong priority = args[3];
101423bcd5ebSCédric Le Goater     target_ulong eisn     = args[4];
101523bcd5ebSCédric Le Goater     uint8_t end_blk;
101623bcd5ebSCédric Le Goater     uint32_t end_idx;
101723bcd5ebSCédric Le Goater 
101823bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
101923bcd5ebSCédric Le Goater         return H_FUNCTION;
102023bcd5ebSCédric Le Goater     }
102123bcd5ebSCédric Le Goater 
102223bcd5ebSCédric Le Goater     if (flags & ~(SPAPR_XIVE_SRC_SET_EISN | SPAPR_XIVE_SRC_MASK)) {
102323bcd5ebSCédric Le Goater         return H_PARAMETER;
102423bcd5ebSCédric Le Goater     }
102523bcd5ebSCédric Le Goater 
102623bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
102723bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
102823bcd5ebSCédric Le Goater                       lisn);
102923bcd5ebSCédric Le Goater         return H_P2;
103023bcd5ebSCédric Le Goater     }
103123bcd5ebSCédric Le Goater 
103223bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
103323bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
103423bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
103523bcd5ebSCédric Le Goater                       lisn);
103623bcd5ebSCédric Le Goater         return H_P2;
103723bcd5ebSCédric Le Goater     }
103823bcd5ebSCédric Le Goater 
103923bcd5ebSCédric Le Goater     /* priority 0xff is used to reset the EAS */
104023bcd5ebSCédric Le Goater     if (priority == 0xff) {
104123bcd5ebSCédric Le Goater         new_eas.w = cpu_to_be64(EAS_VALID | EAS_MASKED);
104223bcd5ebSCédric Le Goater         goto out;
104323bcd5ebSCédric Le Goater     }
104423bcd5ebSCédric Le Goater 
104523bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_MASK) {
104623bcd5ebSCédric Le Goater         new_eas.w = eas.w | cpu_to_be64(EAS_MASKED);
104723bcd5ebSCédric Le Goater     } else {
104823bcd5ebSCédric Le Goater         new_eas.w = eas.w & cpu_to_be64(~EAS_MASKED);
104923bcd5ebSCédric Le Goater     }
105023bcd5ebSCédric Le Goater 
1051*4f311a70SCédric Le Goater     if (spapr_xive_priority_is_reserved(xive, priority)) {
105223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
105323bcd5ebSCédric Le Goater                       " is reserved\n", priority);
105423bcd5ebSCédric Le Goater         return H_P4;
105523bcd5ebSCédric Le Goater     }
105623bcd5ebSCédric Le Goater 
105723bcd5ebSCédric Le Goater     /*
105823bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
105923bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
106023bcd5ebSCédric Le Goater      * target.
106123bcd5ebSCédric Le Goater      */
106223bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
106323bcd5ebSCédric Le Goater         return H_P3;
106423bcd5ebSCédric Le Goater     }
106523bcd5ebSCédric Le Goater 
106623bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_BLOCK, new_eas.w, end_blk);
106723bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_INDEX, new_eas.w, end_idx);
106823bcd5ebSCédric Le Goater 
106923bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_SET_EISN) {
107023bcd5ebSCédric Le Goater         new_eas.w = xive_set_field64(EAS_END_DATA, new_eas.w, eisn);
107123bcd5ebSCédric Le Goater     }
107223bcd5ebSCédric Le Goater 
1073e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
10740c575703SCédric Le Goater         Error *local_err = NULL;
10750c575703SCédric Le Goater 
10760c575703SCédric Le Goater         kvmppc_xive_set_source_config(xive, lisn, &new_eas, &local_err);
10770c575703SCédric Le Goater         if (local_err) {
10780c575703SCédric Le Goater             error_report_err(local_err);
10790c575703SCédric Le Goater             return H_HARDWARE;
10800c575703SCédric Le Goater         }
10810c575703SCédric Le Goater     }
10820c575703SCédric Le Goater 
108323bcd5ebSCédric Le Goater out:
108423bcd5ebSCédric Le Goater     xive->eat[lisn] = new_eas;
108523bcd5ebSCédric Le Goater     return H_SUCCESS;
108623bcd5ebSCédric Le Goater }
108723bcd5ebSCédric Le Goater 
108823bcd5ebSCédric Le Goater /*
108923bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
109023bcd5ebSCédric Le Goater  * target/priority pair is assigned to the specified Logical Interrupt
109123bcd5ebSCédric Le Goater  * Source.
109223bcd5ebSCédric Le Goater  *
109323bcd5ebSCédric Le Goater  * Parameters:
109423bcd5ebSCédric Le Goater  * Input:
109523bcd5ebSCédric Le Goater  * - R4: "flags"
109623bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
109723bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
109823bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
109923bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
110023bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
110123bcd5ebSCédric Le Goater  *
110223bcd5ebSCédric Le Goater  * Output:
110323bcd5ebSCédric Le Goater  * - R4: Target to which the specified Logical Interrupt Source is
110423bcd5ebSCédric Le Goater  *       assigned
110523bcd5ebSCédric Le Goater  * - R5: Priority to which the specified Logical Interrupt Source is
110623bcd5ebSCédric Le Goater  *       assigned
110723bcd5ebSCédric Le Goater  * - R6: EISN for the specified Logical Interrupt Source (this will be
110823bcd5ebSCédric Le Goater  *       equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
110923bcd5ebSCédric Le Goater  */
111023bcd5ebSCédric Le Goater static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
1111ce2918cbSDavid Gibson                                             SpaprMachineState *spapr,
111223bcd5ebSCédric Le Goater                                             target_ulong opcode,
111323bcd5ebSCédric Le Goater                                             target_ulong *args)
111423bcd5ebSCédric Le Goater {
1115ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
111623bcd5ebSCédric Le Goater     target_ulong flags = args[0];
111723bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
111823bcd5ebSCédric Le Goater     XiveEAS eas;
111923bcd5ebSCédric Le Goater     XiveEND *end;
112023bcd5ebSCédric Le Goater     uint8_t nvt_blk;
112123bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
112223bcd5ebSCédric Le Goater 
112323bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
112423bcd5ebSCédric Le Goater         return H_FUNCTION;
112523bcd5ebSCédric Le Goater     }
112623bcd5ebSCédric Le Goater 
112723bcd5ebSCédric Le Goater     if (flags) {
112823bcd5ebSCédric Le Goater         return H_PARAMETER;
112923bcd5ebSCédric Le Goater     }
113023bcd5ebSCédric Le Goater 
113123bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
113223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
113323bcd5ebSCédric Le Goater                       lisn);
113423bcd5ebSCédric Le Goater         return H_P2;
113523bcd5ebSCédric Le Goater     }
113623bcd5ebSCédric Le Goater 
113723bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
113823bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
113923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
114023bcd5ebSCédric Le Goater                       lisn);
114123bcd5ebSCédric Le Goater         return H_P2;
114223bcd5ebSCédric Le Goater     }
114323bcd5ebSCédric Le Goater 
114423bcd5ebSCédric Le Goater     /* EAS_END_BLOCK is unused on sPAPR */
114523bcd5ebSCédric Le Goater     end_idx = xive_get_field64(EAS_END_INDEX, eas.w);
114623bcd5ebSCédric Le Goater 
114723bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
114823bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
114923bcd5ebSCédric Le Goater 
115023bcd5ebSCédric Le Goater     nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
115123bcd5ebSCédric Le Goater     nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
115223bcd5ebSCédric Le Goater     args[0] = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
115323bcd5ebSCédric Le Goater 
115423bcd5ebSCédric Le Goater     if (xive_eas_is_masked(&eas)) {
115523bcd5ebSCédric Le Goater         args[1] = 0xff;
115623bcd5ebSCédric Le Goater     } else {
115723bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
115823bcd5ebSCédric Le Goater     }
115923bcd5ebSCédric Le Goater 
116023bcd5ebSCédric Le Goater     args[2] = xive_get_field64(EAS_END_DATA, eas.w);
116123bcd5ebSCédric Le Goater 
116223bcd5ebSCédric Le Goater     return H_SUCCESS;
116323bcd5ebSCédric Le Goater }
116423bcd5ebSCédric Le Goater 
116523bcd5ebSCédric Le Goater /*
116623bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
116723bcd5ebSCédric Le Goater  * address of the notification management page associated with the
116823bcd5ebSCédric Le Goater  * specified target and priority.
116923bcd5ebSCédric Le Goater  *
117023bcd5ebSCédric Le Goater  * Parameters:
117123bcd5ebSCédric Le Goater  * Input:
117223bcd5ebSCédric Le Goater  * - R4: "flags"
117323bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
117423bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
117523bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
117623bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
117723bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
117823bcd5ebSCédric Le Goater  *
117923bcd5ebSCédric Le Goater  * Output:
118023bcd5ebSCédric Le Goater  * - R4: Logical real address of notification page
118123bcd5ebSCédric Le Goater  * - R5: Power of 2 page size of the notification page
118223bcd5ebSCédric Le Goater  */
118323bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
1184ce2918cbSDavid Gibson                                          SpaprMachineState *spapr,
118523bcd5ebSCédric Le Goater                                          target_ulong opcode,
118623bcd5ebSCédric Le Goater                                          target_ulong *args)
118723bcd5ebSCédric Le Goater {
1188ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
118923bcd5ebSCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
119023bcd5ebSCédric Le Goater     target_ulong flags = args[0];
119123bcd5ebSCédric Le Goater     target_ulong target = args[1];
119223bcd5ebSCédric Le Goater     target_ulong priority = args[2];
119323bcd5ebSCédric Le Goater     XiveEND *end;
119423bcd5ebSCédric Le Goater     uint8_t end_blk;
119523bcd5ebSCédric Le Goater     uint32_t end_idx;
119623bcd5ebSCédric Le Goater 
119723bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
119823bcd5ebSCédric Le Goater         return H_FUNCTION;
119923bcd5ebSCédric Le Goater     }
120023bcd5ebSCédric Le Goater 
120123bcd5ebSCédric Le Goater     if (flags) {
120223bcd5ebSCédric Le Goater         return H_PARAMETER;
120323bcd5ebSCédric Le Goater     }
120423bcd5ebSCédric Le Goater 
120523bcd5ebSCédric Le Goater     /*
120623bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
120723bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
120823bcd5ebSCédric Le Goater      */
120923bcd5ebSCédric Le Goater 
1210*4f311a70SCédric Le Goater     if (spapr_xive_priority_is_reserved(xive, priority)) {
121123bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
121223bcd5ebSCédric Le Goater                       " is reserved\n", priority);
121323bcd5ebSCédric Le Goater         return H_P3;
121423bcd5ebSCédric Le Goater     }
121523bcd5ebSCédric Le Goater 
121623bcd5ebSCédric Le Goater     /*
121723bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
121823bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
121923bcd5ebSCédric Le Goater      * target.
122023bcd5ebSCédric Le Goater      */
122123bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
122223bcd5ebSCédric Le Goater         return H_P2;
122323bcd5ebSCédric Le Goater     }
122423bcd5ebSCédric Le Goater 
122523bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
122623bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
122723bcd5ebSCédric Le Goater 
122823bcd5ebSCédric Le Goater     args[0] = xive->end_base + (1ull << (end_xsrc->esb_shift + 1)) * end_idx;
122923bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
123023bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
123123bcd5ebSCédric Le Goater     } else {
123223bcd5ebSCédric Le Goater         args[1] = 0;
123323bcd5ebSCédric Le Goater     }
123423bcd5ebSCédric Le Goater 
123523bcd5ebSCédric Le Goater     return H_SUCCESS;
123623bcd5ebSCédric Le Goater }
123723bcd5ebSCédric Le Goater 
123823bcd5ebSCédric Le Goater /*
123923bcd5ebSCédric Le Goater  * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
124023bcd5ebSCédric Le Goater  * a given "target" and "priority".  It is also used to set the
124123bcd5ebSCédric Le Goater  * notification config associated with the EQ.  An EQ size of 0 is
124223bcd5ebSCédric Le Goater  * used to reset the EQ config for a given target and priority. If
124323bcd5ebSCédric Le Goater  * resetting the EQ config, the END associated with the given "target"
124423bcd5ebSCédric Le Goater  * and "priority" will be changed to disable queueing.
124523bcd5ebSCédric Le Goater  *
124623bcd5ebSCédric Le Goater  * Upon return from the hcall(), no additional interrupts will be
124723bcd5ebSCédric Le Goater  * directed to the old EQ (if one was set). The old EQ (if one was
124823bcd5ebSCédric Le Goater  * set) should be investigated for interrupts that occurred prior to
124923bcd5ebSCédric Le Goater  * or during the hcall().
125023bcd5ebSCédric Le Goater  *
125123bcd5ebSCédric Le Goater  * Parameters:
125223bcd5ebSCédric Le Goater  * Input:
125323bcd5ebSCédric Le Goater  * - R4: "flags"
125423bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
125523bcd5ebSCédric Le Goater  *         Bit 63: Unconditional Notify (n) per the XIVE spec
125623bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
125723bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
125823bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
125923bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
126023bcd5ebSCédric Le Goater  * - R7: "eventQueue": The logical real address of the start of the EQ
126123bcd5ebSCédric Le Goater  * - R8: "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
126223bcd5ebSCédric Le Goater  *
126323bcd5ebSCédric Le Goater  * Output:
126423bcd5ebSCédric Le Goater  * - None
126523bcd5ebSCédric Le Goater  */
126623bcd5ebSCédric Le Goater 
126723bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_ALWAYS_NOTIFY PPC_BIT(63)
126823bcd5ebSCédric Le Goater 
126923bcd5ebSCédric Le Goater static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
1270ce2918cbSDavid Gibson                                            SpaprMachineState *spapr,
127123bcd5ebSCédric Le Goater                                            target_ulong opcode,
127223bcd5ebSCédric Le Goater                                            target_ulong *args)
127323bcd5ebSCédric Le Goater {
1274ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
127523bcd5ebSCédric Le Goater     target_ulong flags = args[0];
127623bcd5ebSCédric Le Goater     target_ulong target = args[1];
127723bcd5ebSCédric Le Goater     target_ulong priority = args[2];
127823bcd5ebSCédric Le Goater     target_ulong qpage = args[3];
127923bcd5ebSCédric Le Goater     target_ulong qsize = args[4];
128023bcd5ebSCédric Le Goater     XiveEND end;
128123bcd5ebSCédric Le Goater     uint8_t end_blk, nvt_blk;
128223bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
128323bcd5ebSCédric Le Goater 
128423bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
128523bcd5ebSCédric Le Goater         return H_FUNCTION;
128623bcd5ebSCédric Le Goater     }
128723bcd5ebSCédric Le Goater 
128823bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_ALWAYS_NOTIFY) {
128923bcd5ebSCédric Le Goater         return H_PARAMETER;
129023bcd5ebSCédric Le Goater     }
129123bcd5ebSCédric Le Goater 
129223bcd5ebSCédric Le Goater     /*
129323bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
129423bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
129523bcd5ebSCédric Le Goater      */
129623bcd5ebSCédric Le Goater 
1297*4f311a70SCédric Le Goater     if (spapr_xive_priority_is_reserved(xive, priority)) {
129823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
129923bcd5ebSCédric Le Goater                       " is reserved\n", priority);
130023bcd5ebSCédric Le Goater         return H_P3;
130123bcd5ebSCédric Le Goater     }
130223bcd5ebSCédric Le Goater 
130323bcd5ebSCédric Le Goater     /*
130423bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
130523bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
130623bcd5ebSCédric Le Goater      * target.
130723bcd5ebSCédric Le Goater      */
130823bcd5ebSCédric Le Goater 
130923bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
131023bcd5ebSCédric Le Goater         return H_P2;
131123bcd5ebSCédric Le Goater     }
131223bcd5ebSCédric Le Goater 
131323bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
131423bcd5ebSCédric Le Goater     memcpy(&end, &xive->endt[end_idx], sizeof(XiveEND));
131523bcd5ebSCédric Le Goater 
131623bcd5ebSCédric Le Goater     switch (qsize) {
131723bcd5ebSCédric Le Goater     case 12:
131823bcd5ebSCédric Le Goater     case 16:
131923bcd5ebSCédric Le Goater     case 21:
132023bcd5ebSCédric Le Goater     case 24:
13217f9136f9SCédric Le Goater         if (!QEMU_IS_ALIGNED(qpage, 1ul << qsize)) {
13227f9136f9SCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: EQ @0x%" HWADDR_PRIx
13237f9136f9SCédric Le Goater                           " is not naturally aligned with %" HWADDR_PRIx "\n",
13247f9136f9SCédric Le Goater                           qpage, (hwaddr)1 << qsize);
13257f9136f9SCédric Le Goater             return H_P4;
13267f9136f9SCédric Le Goater         }
132723bcd5ebSCédric Le Goater         end.w2 = cpu_to_be32((qpage >> 32) & 0x0fffffff);
132823bcd5ebSCédric Le Goater         end.w3 = cpu_to_be32(qpage & 0xffffffff);
132923bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_ENQUEUE);
133023bcd5ebSCédric Le Goater         end.w0 = xive_set_field32(END_W0_QSIZE, end.w0, qsize - 12);
133123bcd5ebSCédric Le Goater         break;
133223bcd5ebSCédric Le Goater     case 0:
133323bcd5ebSCédric Le Goater         /* reset queue and disable queueing */
133423bcd5ebSCédric Le Goater         spapr_xive_end_reset(&end);
133523bcd5ebSCédric Le Goater         goto out;
133623bcd5ebSCédric Le Goater 
133723bcd5ebSCédric Le Goater     default:
133823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid EQ size %"PRIx64"\n",
133923bcd5ebSCédric Le Goater                       qsize);
134023bcd5ebSCédric Le Goater         return H_P5;
134123bcd5ebSCédric Le Goater     }
134223bcd5ebSCédric Le Goater 
134323bcd5ebSCédric Le Goater     if (qsize) {
134423bcd5ebSCédric Le Goater         hwaddr plen = 1 << qsize;
134523bcd5ebSCédric Le Goater         void *eq;
134623bcd5ebSCédric Le Goater 
134723bcd5ebSCédric Le Goater         /*
134823bcd5ebSCédric Le Goater          * Validate the guest EQ. We should also check that the queue
134923bcd5ebSCédric Le Goater          * has been zeroed by the OS.
135023bcd5ebSCédric Le Goater          */
135123bcd5ebSCédric Le Goater         eq = address_space_map(CPU(cpu)->as, qpage, &plen, true,
135223bcd5ebSCédric Le Goater                                MEMTXATTRS_UNSPECIFIED);
135323bcd5ebSCédric Le Goater         if (plen != 1 << qsize) {
135423bcd5ebSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to map EQ @0x%"
135523bcd5ebSCédric Le Goater                           HWADDR_PRIx "\n", qpage);
135623bcd5ebSCédric Le Goater             return H_P4;
135723bcd5ebSCédric Le Goater         }
135823bcd5ebSCédric Le Goater         address_space_unmap(CPU(cpu)->as, eq, plen, true, plen);
135923bcd5ebSCédric Le Goater     }
136023bcd5ebSCédric Le Goater 
136123bcd5ebSCédric Le Goater     /* "target" should have been validated above */
136223bcd5ebSCédric Le Goater     if (spapr_xive_target_to_nvt(target, &nvt_blk, &nvt_idx)) {
136323bcd5ebSCédric Le Goater         g_assert_not_reached();
136423bcd5ebSCédric Le Goater     }
136523bcd5ebSCédric Le Goater 
136623bcd5ebSCédric Le Goater     /*
136723bcd5ebSCédric Le Goater      * Ensure the priority and target are correctly set (they will not
136823bcd5ebSCédric Le Goater      * be right after allocation)
136923bcd5ebSCédric Le Goater      */
137023bcd5ebSCédric Le Goater     end.w6 = xive_set_field32(END_W6_NVT_BLOCK, 0ul, nvt_blk) |
137123bcd5ebSCédric Le Goater         xive_set_field32(END_W6_NVT_INDEX, 0ul, nvt_idx);
137223bcd5ebSCédric Le Goater     end.w7 = xive_set_field32(END_W7_F0_PRIORITY, 0ul, priority);
137323bcd5ebSCédric Le Goater 
137423bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_ALWAYS_NOTIFY) {
137523bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_UCOND_NOTIFY);
137623bcd5ebSCédric Le Goater     } else {
137723bcd5ebSCédric Le Goater         end.w0 &= cpu_to_be32((uint32_t)~END_W0_UCOND_NOTIFY);
137823bcd5ebSCédric Le Goater     }
137923bcd5ebSCédric Le Goater 
138023bcd5ebSCédric Le Goater     /*
138123bcd5ebSCédric Le Goater      * The generation bit for the END starts at 1 and The END page
138223bcd5ebSCédric Le Goater      * offset counter starts at 0.
138323bcd5ebSCédric Le Goater      */
138423bcd5ebSCédric Le Goater     end.w1 = cpu_to_be32(END_W1_GENERATION) |
138523bcd5ebSCédric Le Goater         xive_set_field32(END_W1_PAGE_OFF, 0ul, 0ul);
138623bcd5ebSCédric Le Goater     end.w0 |= cpu_to_be32(END_W0_VALID);
138723bcd5ebSCédric Le Goater 
138823bcd5ebSCédric Le Goater     /*
138923bcd5ebSCédric Le Goater      * TODO: issue syncs required to ensure all in-flight interrupts
139023bcd5ebSCédric Le Goater      * are complete on the old END
139123bcd5ebSCédric Le Goater      */
139223bcd5ebSCédric Le Goater 
139323bcd5ebSCédric Le Goater out:
1394e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
13950c575703SCédric Le Goater         Error *local_err = NULL;
13960c575703SCédric Le Goater 
13970c575703SCédric Le Goater         kvmppc_xive_set_queue_config(xive, end_blk, end_idx, &end, &local_err);
13980c575703SCédric Le Goater         if (local_err) {
13990c575703SCédric Le Goater             error_report_err(local_err);
14000c575703SCédric Le Goater             return H_HARDWARE;
14010c575703SCédric Le Goater         }
14020c575703SCédric Le Goater     }
14030c575703SCédric Le Goater 
140423bcd5ebSCédric Le Goater     /* Update END */
140523bcd5ebSCédric Le Goater     memcpy(&xive->endt[end_idx], &end, sizeof(XiveEND));
140623bcd5ebSCédric Le Goater     return H_SUCCESS;
140723bcd5ebSCédric Le Goater }
140823bcd5ebSCédric Le Goater 
140923bcd5ebSCédric Le Goater /*
141023bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
141123bcd5ebSCédric Le Goater  * target and priority.
141223bcd5ebSCédric Le Goater  *
141323bcd5ebSCédric Le Goater  * Parameters:
141423bcd5ebSCédric Le Goater  * Input:
141523bcd5ebSCédric Le Goater  * - R4: "flags"
141623bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
141723bcd5ebSCédric Le Goater  *         Bit 63: Debug: Return debug data
141823bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
141923bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
142023bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
142123bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
142223bcd5ebSCédric Le Goater  *
142323bcd5ebSCédric Le Goater  * Output:
142423bcd5ebSCédric Le Goater  * - R4: "flags":
142523bcd5ebSCédric Le Goater  *       Bits 0-61: Reserved
142623bcd5ebSCédric Le Goater  *       Bit 62: The value of Event Queue Generation Number (g) per
142723bcd5ebSCédric Le Goater  *              the XIVE spec if "Debug" = 1
142823bcd5ebSCédric Le Goater  *       Bit 63: The value of Unconditional Notify (n) per the XIVE spec
142923bcd5ebSCédric Le Goater  * - R5: The logical real address of the start of the EQ
143023bcd5ebSCédric Le Goater  * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
143123bcd5ebSCédric Le Goater  * - R7: The value of Event Queue Offset Counter per XIVE spec
143223bcd5ebSCédric Le Goater  *       if "Debug" = 1, else 0
143323bcd5ebSCédric Le Goater  *
143423bcd5ebSCédric Le Goater  */
143523bcd5ebSCédric Le Goater 
143623bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_DEBUG     PPC_BIT(63)
143723bcd5ebSCédric Le Goater 
143823bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
1439ce2918cbSDavid Gibson                                            SpaprMachineState *spapr,
144023bcd5ebSCédric Le Goater                                            target_ulong opcode,
144123bcd5ebSCédric Le Goater                                            target_ulong *args)
144223bcd5ebSCédric Le Goater {
1443ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
144423bcd5ebSCédric Le Goater     target_ulong flags = args[0];
144523bcd5ebSCédric Le Goater     target_ulong target = args[1];
144623bcd5ebSCédric Le Goater     target_ulong priority = args[2];
144723bcd5ebSCédric Le Goater     XiveEND *end;
144823bcd5ebSCédric Le Goater     uint8_t end_blk;
144923bcd5ebSCédric Le Goater     uint32_t end_idx;
145023bcd5ebSCédric Le Goater 
145123bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
145223bcd5ebSCédric Le Goater         return H_FUNCTION;
145323bcd5ebSCédric Le Goater     }
145423bcd5ebSCédric Le Goater 
145523bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_DEBUG) {
145623bcd5ebSCédric Le Goater         return H_PARAMETER;
145723bcd5ebSCédric Le Goater     }
145823bcd5ebSCédric Le Goater 
145923bcd5ebSCédric Le Goater     /*
146023bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
146123bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
146223bcd5ebSCédric Le Goater      */
146323bcd5ebSCédric Le Goater 
1464*4f311a70SCédric Le Goater     if (spapr_xive_priority_is_reserved(xive, priority)) {
146523bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
146623bcd5ebSCédric Le Goater                       " is reserved\n", priority);
146723bcd5ebSCédric Le Goater         return H_P3;
146823bcd5ebSCédric Le Goater     }
146923bcd5ebSCédric Le Goater 
147023bcd5ebSCédric Le Goater     /*
147123bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
147223bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
147323bcd5ebSCédric Le Goater      * target.
147423bcd5ebSCédric Le Goater      */
147523bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
147623bcd5ebSCédric Le Goater         return H_P2;
147723bcd5ebSCédric Le Goater     }
147823bcd5ebSCédric Le Goater 
147923bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
148023bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
148123bcd5ebSCédric Le Goater 
148223bcd5ebSCédric Le Goater     args[0] = 0;
148323bcd5ebSCédric Le Goater     if (xive_end_is_notify(end)) {
148423bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_END_ALWAYS_NOTIFY;
148523bcd5ebSCédric Le Goater     }
148623bcd5ebSCédric Le Goater 
148723bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
148813df9324SCédric Le Goater         args[1] = xive_end_qaddr(end);
148923bcd5ebSCédric Le Goater         args[2] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
149023bcd5ebSCédric Le Goater     } else {
149123bcd5ebSCédric Le Goater         args[1] = 0;
149223bcd5ebSCédric Le Goater         args[2] = 0;
149323bcd5ebSCédric Le Goater     }
149423bcd5ebSCédric Le Goater 
1495e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
14960c575703SCédric Le Goater         Error *local_err = NULL;
14970c575703SCédric Le Goater 
14980c575703SCédric Le Goater         kvmppc_xive_get_queue_config(xive, end_blk, end_idx, end, &local_err);
14990c575703SCédric Le Goater         if (local_err) {
15000c575703SCédric Le Goater             error_report_err(local_err);
15010c575703SCédric Le Goater             return H_HARDWARE;
15020c575703SCédric Le Goater         }
15030c575703SCédric Le Goater     }
15040c575703SCédric Le Goater 
150523bcd5ebSCédric Le Goater     /* TODO: do we need any locking on the END ? */
150623bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_DEBUG) {
150723bcd5ebSCédric Le Goater         /* Load the event queue generation number into the return flags */
150823bcd5ebSCédric Le Goater         args[0] |= (uint64_t)xive_get_field32(END_W1_GENERATION, end->w1) << 62;
150923bcd5ebSCédric Le Goater 
151023bcd5ebSCédric Le Goater         /* Load R7 with the event queue offset counter */
151123bcd5ebSCédric Le Goater         args[3] = xive_get_field32(END_W1_PAGE_OFF, end->w1);
151223bcd5ebSCédric Le Goater     } else {
151323bcd5ebSCédric Le Goater         args[3] = 0;
151423bcd5ebSCédric Le Goater     }
151523bcd5ebSCédric Le Goater 
151623bcd5ebSCédric Le Goater     return H_SUCCESS;
151723bcd5ebSCédric Le Goater }
151823bcd5ebSCédric Le Goater 
151923bcd5ebSCédric Le Goater /*
152023bcd5ebSCédric Le Goater  * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
152123bcd5ebSCédric Le Goater  * reporting cache line pair for the calling thread.  The reporting
152223bcd5ebSCédric Le Goater  * cache lines will contain the OS interrupt context when the OS
152323bcd5ebSCédric Le Goater  * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
152423bcd5ebSCédric Le Goater  * interrupt. The reporting cache lines can be reset by inputting -1
152523bcd5ebSCédric Le Goater  * in "reportingLine".  Issuing the CI store byte without reporting
152623bcd5ebSCédric Le Goater  * cache lines registered will result in the data not being accessible
152723bcd5ebSCédric Le Goater  * to the OS.
152823bcd5ebSCédric Le Goater  *
152923bcd5ebSCédric Le Goater  * Parameters:
153023bcd5ebSCédric Le Goater  * Input:
153123bcd5ebSCédric Le Goater  * - R4: "flags"
153223bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
153323bcd5ebSCédric Le Goater  * - R5: "reportingLine": The logical real address of the reporting cache
153423bcd5ebSCédric Le Goater  *       line pair
153523bcd5ebSCédric Le Goater  *
153623bcd5ebSCédric Le Goater  * Output:
153723bcd5ebSCédric Le Goater  * - None
153823bcd5ebSCédric Le Goater  */
153923bcd5ebSCédric Le Goater static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
1540ce2918cbSDavid Gibson                                                 SpaprMachineState *spapr,
154123bcd5ebSCédric Le Goater                                                 target_ulong opcode,
154223bcd5ebSCédric Le Goater                                                 target_ulong *args)
154323bcd5ebSCédric Le Goater {
154423bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
154523bcd5ebSCédric Le Goater         return H_FUNCTION;
154623bcd5ebSCédric Le Goater     }
154723bcd5ebSCédric Le Goater 
154823bcd5ebSCédric Le Goater     /*
154923bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
155023bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
155123bcd5ebSCédric Le Goater      */
155223bcd5ebSCédric Le Goater 
155323bcd5ebSCédric Le Goater     /* TODO: H_INT_SET_OS_REPORTING_LINE */
155423bcd5ebSCédric Le Goater     return H_FUNCTION;
155523bcd5ebSCédric Le Goater }
155623bcd5ebSCédric Le Goater 
155723bcd5ebSCédric Le Goater /*
155823bcd5ebSCédric Le Goater  * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
155923bcd5ebSCédric Le Goater  * real address of the reporting cache line pair set for the input
156023bcd5ebSCédric Le Goater  * "target".  If no reporting cache line pair has been set, -1 is
156123bcd5ebSCédric Le Goater  * returned.
156223bcd5ebSCédric Le Goater  *
156323bcd5ebSCédric Le Goater  * Parameters:
156423bcd5ebSCédric Le Goater  * Input:
156523bcd5ebSCédric Le Goater  * - R4: "flags"
156623bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
156723bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
156823bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
156923bcd5ebSCédric Le Goater  * - R6: "reportingLine": The logical real address of the reporting
157023bcd5ebSCédric Le Goater  *        cache line pair
157123bcd5ebSCédric Le Goater  *
157223bcd5ebSCédric Le Goater  * Output:
157323bcd5ebSCédric Le Goater  * - R4: The logical real address of the reporting line if set, else -1
157423bcd5ebSCédric Le Goater  */
157523bcd5ebSCédric Le Goater static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
1576ce2918cbSDavid Gibson                                                 SpaprMachineState *spapr,
157723bcd5ebSCédric Le Goater                                                 target_ulong opcode,
157823bcd5ebSCédric Le Goater                                                 target_ulong *args)
157923bcd5ebSCédric Le Goater {
158023bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
158123bcd5ebSCédric Le Goater         return H_FUNCTION;
158223bcd5ebSCédric Le Goater     }
158323bcd5ebSCédric Le Goater 
158423bcd5ebSCédric Le Goater     /*
158523bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
158623bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
158723bcd5ebSCédric Le Goater      */
158823bcd5ebSCédric Le Goater 
158923bcd5ebSCédric Le Goater     /* TODO: H_INT_GET_OS_REPORTING_LINE */
159023bcd5ebSCédric Le Goater     return H_FUNCTION;
159123bcd5ebSCédric Le Goater }
159223bcd5ebSCédric Le Goater 
159323bcd5ebSCédric Le Goater /*
159423bcd5ebSCédric Le Goater  * The H_INT_ESB hcall() is used to issue a load or store to the ESB
159523bcd5ebSCédric Le Goater  * page for the input "lisn".  This hcall is only supported for LISNs
159623bcd5ebSCédric Le Goater  * that have the ESB hcall flag set to 1 when returned from hcall()
159723bcd5ebSCédric Le Goater  * H_INT_GET_SOURCE_INFO.
159823bcd5ebSCédric Le Goater  *
159923bcd5ebSCédric Le Goater  * Parameters:
160023bcd5ebSCédric Le Goater  * Input:
160123bcd5ebSCédric Le Goater  * - R4: "flags"
160223bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
160323bcd5ebSCédric Le Goater  *         bit 63: Store: Store=1, store operation, else load operation
160423bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
160523bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
160623bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
160723bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
160823bcd5ebSCédric Le Goater  * - R6: "esbOffset" is the offset into the ESB page for the load or
160923bcd5ebSCédric Le Goater  *       store operation
161023bcd5ebSCédric Le Goater  * - R7: "storeData" is the data to write for a store operation
161123bcd5ebSCédric Le Goater  *
161223bcd5ebSCédric Le Goater  * Output:
161323bcd5ebSCédric Le Goater  * - R4: The value of the load if load operation, else -1
161423bcd5ebSCédric Le Goater  */
161523bcd5ebSCédric Le Goater 
161623bcd5ebSCédric Le Goater #define SPAPR_XIVE_ESB_STORE PPC_BIT(63)
161723bcd5ebSCédric Le Goater 
161823bcd5ebSCédric Le Goater static target_ulong h_int_esb(PowerPCCPU *cpu,
1619ce2918cbSDavid Gibson                               SpaprMachineState *spapr,
162023bcd5ebSCédric Le Goater                               target_ulong opcode,
162123bcd5ebSCédric Le Goater                               target_ulong *args)
162223bcd5ebSCédric Le Goater {
1623ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
162423bcd5ebSCédric Le Goater     XiveEAS eas;
162523bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
162623bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
162723bcd5ebSCédric Le Goater     target_ulong offset = args[2];
162823bcd5ebSCédric Le Goater     target_ulong data   = args[3];
162923bcd5ebSCédric Le Goater     hwaddr mmio_addr;
163023bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
163123bcd5ebSCédric Le Goater 
163223bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
163323bcd5ebSCédric Le Goater         return H_FUNCTION;
163423bcd5ebSCédric Le Goater     }
163523bcd5ebSCédric Le Goater 
163623bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_ESB_STORE) {
163723bcd5ebSCédric Le Goater         return H_PARAMETER;
163823bcd5ebSCédric Le Goater     }
163923bcd5ebSCédric Le Goater 
164023bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
164123bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
164223bcd5ebSCédric Le Goater                       lisn);
164323bcd5ebSCédric Le Goater         return H_P2;
164423bcd5ebSCédric Le Goater     }
164523bcd5ebSCédric Le Goater 
164623bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
164723bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
164823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
164923bcd5ebSCédric Le Goater                       lisn);
165023bcd5ebSCédric Le Goater         return H_P2;
165123bcd5ebSCédric Le Goater     }
165223bcd5ebSCédric Le Goater 
165323bcd5ebSCédric Le Goater     if (offset > (1ull << xsrc->esb_shift)) {
165423bcd5ebSCédric Le Goater         return H_P3;
165523bcd5ebSCédric Le Goater     }
165623bcd5ebSCédric Le Goater 
1657e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
16580c575703SCédric Le Goater         args[0] = kvmppc_xive_esb_rw(xsrc, lisn, offset, data,
16590c575703SCédric Le Goater                                      flags & SPAPR_XIVE_ESB_STORE);
16600c575703SCédric Le Goater     } else {
166123bcd5ebSCédric Le Goater         mmio_addr = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn) + offset;
166223bcd5ebSCédric Le Goater 
166323bcd5ebSCédric Le Goater         if (dma_memory_rw(&address_space_memory, mmio_addr, &data, 8,
166423bcd5ebSCédric Le Goater                           (flags & SPAPR_XIVE_ESB_STORE))) {
166523bcd5ebSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to access ESB @0x%"
166623bcd5ebSCédric Le Goater                           HWADDR_PRIx "\n", mmio_addr);
166723bcd5ebSCédric Le Goater             return H_HARDWARE;
166823bcd5ebSCédric Le Goater         }
166923bcd5ebSCédric Le Goater         args[0] = (flags & SPAPR_XIVE_ESB_STORE) ? -1 : data;
16700c575703SCédric Le Goater     }
167123bcd5ebSCédric Le Goater     return H_SUCCESS;
167223bcd5ebSCédric Le Goater }
167323bcd5ebSCédric Le Goater 
167423bcd5ebSCédric Le Goater /*
167523bcd5ebSCédric Le Goater  * The H_INT_SYNC hcall() is used to issue hardware syncs that will
167623bcd5ebSCédric Le Goater  * ensure any in flight events for the input lisn are in the event
167723bcd5ebSCédric Le Goater  * queue.
167823bcd5ebSCédric Le Goater  *
167923bcd5ebSCédric Le Goater  * Parameters:
168023bcd5ebSCédric Le Goater  * Input:
168123bcd5ebSCédric Le Goater  * - R4: "flags"
168223bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
168323bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
168423bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
168523bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
168623bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
168723bcd5ebSCédric Le Goater  *
168823bcd5ebSCédric Le Goater  * Output:
168923bcd5ebSCédric Le Goater  * - None
169023bcd5ebSCédric Le Goater  */
169123bcd5ebSCédric Le Goater static target_ulong h_int_sync(PowerPCCPU *cpu,
1692ce2918cbSDavid Gibson                                SpaprMachineState *spapr,
169323bcd5ebSCédric Le Goater                                target_ulong opcode,
169423bcd5ebSCédric Le Goater                                target_ulong *args)
169523bcd5ebSCédric Le Goater {
1696ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
169723bcd5ebSCédric Le Goater     XiveEAS eas;
169823bcd5ebSCédric Le Goater     target_ulong flags = args[0];
169923bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
170023bcd5ebSCédric Le Goater 
170123bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
170223bcd5ebSCédric Le Goater         return H_FUNCTION;
170323bcd5ebSCédric Le Goater     }
170423bcd5ebSCédric Le Goater 
170523bcd5ebSCédric Le Goater     if (flags) {
170623bcd5ebSCédric Le Goater         return H_PARAMETER;
170723bcd5ebSCédric Le Goater     }
170823bcd5ebSCédric Le Goater 
170923bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
171023bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
171123bcd5ebSCédric Le Goater                       lisn);
171223bcd5ebSCédric Le Goater         return H_P2;
171323bcd5ebSCédric Le Goater     }
171423bcd5ebSCédric Le Goater 
171523bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
171623bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
171723bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
171823bcd5ebSCédric Le Goater                       lisn);
171923bcd5ebSCédric Le Goater         return H_P2;
172023bcd5ebSCédric Le Goater     }
172123bcd5ebSCédric Le Goater 
172223bcd5ebSCédric Le Goater     /*
172323bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
172423bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
172523bcd5ebSCédric Le Goater      */
172623bcd5ebSCédric Le Goater 
17270c575703SCédric Le Goater     /*
17280c575703SCédric Le Goater      * This is not real hardware. Nothing to be done unless when
17290c575703SCédric Le Goater      * under KVM
17300c575703SCédric Le Goater      */
17310c575703SCédric Le Goater 
1732e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
17330c575703SCédric Le Goater         Error *local_err = NULL;
17340c575703SCédric Le Goater 
17350c575703SCédric Le Goater         kvmppc_xive_sync_source(xive, lisn, &local_err);
17360c575703SCédric Le Goater         if (local_err) {
17370c575703SCédric Le Goater             error_report_err(local_err);
17380c575703SCédric Le Goater             return H_HARDWARE;
17390c575703SCédric Le Goater         }
17400c575703SCédric Le Goater     }
174123bcd5ebSCédric Le Goater     return H_SUCCESS;
174223bcd5ebSCédric Le Goater }
174323bcd5ebSCédric Le Goater 
174423bcd5ebSCédric Le Goater /*
174523bcd5ebSCédric Le Goater  * The H_INT_RESET hcall() is used to reset all of the partition's
174623bcd5ebSCédric Le Goater  * interrupt exploitation structures to their initial state.  This
174723bcd5ebSCédric Le Goater  * means losing all previously set interrupt state set via
174823bcd5ebSCédric Le Goater  * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
174923bcd5ebSCédric Le Goater  *
175023bcd5ebSCédric Le Goater  * Parameters:
175123bcd5ebSCédric Le Goater  * Input:
175223bcd5ebSCédric Le Goater  * - R4: "flags"
175323bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
175423bcd5ebSCédric Le Goater  *
175523bcd5ebSCédric Le Goater  * Output:
175623bcd5ebSCédric Le Goater  * - None
175723bcd5ebSCédric Le Goater  */
175823bcd5ebSCédric Le Goater static target_ulong h_int_reset(PowerPCCPU *cpu,
1759ce2918cbSDavid Gibson                                 SpaprMachineState *spapr,
176023bcd5ebSCédric Le Goater                                 target_ulong opcode,
176123bcd5ebSCédric Le Goater                                 target_ulong *args)
176223bcd5ebSCédric Le Goater {
1763ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
176423bcd5ebSCédric Le Goater     target_ulong flags   = args[0];
176523bcd5ebSCédric Le Goater 
176623bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
176723bcd5ebSCédric Le Goater         return H_FUNCTION;
176823bcd5ebSCédric Le Goater     }
176923bcd5ebSCédric Le Goater 
177023bcd5ebSCédric Le Goater     if (flags) {
177123bcd5ebSCédric Le Goater         return H_PARAMETER;
177223bcd5ebSCédric Le Goater     }
177323bcd5ebSCédric Le Goater 
1774f703a04cSDamien Hedde     device_legacy_reset(DEVICE(xive));
17750c575703SCédric Le Goater 
1776e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
17770c575703SCédric Le Goater         Error *local_err = NULL;
17780c575703SCédric Le Goater 
17790c575703SCédric Le Goater         kvmppc_xive_reset(xive, &local_err);
17800c575703SCédric Le Goater         if (local_err) {
17810c575703SCédric Le Goater             error_report_err(local_err);
17820c575703SCédric Le Goater             return H_HARDWARE;
17830c575703SCédric Le Goater         }
17840c575703SCédric Le Goater     }
178523bcd5ebSCédric Le Goater     return H_SUCCESS;
178623bcd5ebSCédric Le Goater }
178723bcd5ebSCédric Le Goater 
1788ce2918cbSDavid Gibson void spapr_xive_hcall_init(SpaprMachineState *spapr)
178923bcd5ebSCédric Le Goater {
179023bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
179123bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
179223bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
179323bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
179423bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
179523bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
179623bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
179723bcd5ebSCédric Le Goater                              h_int_set_os_reporting_line);
179823bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
179923bcd5ebSCédric Le Goater                              h_int_get_os_reporting_line);
180023bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_ESB, h_int_esb);
180123bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SYNC, h_int_sync);
180223bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_RESET, h_int_reset);
180323bcd5ebSCédric Le Goater }
1804