xref: /qemu/hw/intc/spapr_xive.c (revision 3a8eb78e6c135422017888380db091793039b6dd)
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"
123aa597f6SCédric Le Goater #include "qapi/error.h"
133aa597f6SCédric Le Goater #include "qemu/error-report.h"
143aa597f6SCédric Le Goater #include "target/ppc/cpu.h"
153aa597f6SCédric Le Goater #include "sysemu/cpus.h"
163aa597f6SCédric Le Goater #include "monitor/monitor.h"
176e21de4aSCédric Le Goater #include "hw/ppc/fdt.h"
183aa597f6SCédric Le Goater #include "hw/ppc/spapr.h"
193aa597f6SCédric Le Goater #include "hw/ppc/spapr_xive.h"
203aa597f6SCédric Le Goater #include "hw/ppc/xive.h"
213aa597f6SCédric Le Goater #include "hw/ppc/xive_regs.h"
223aa597f6SCédric Le Goater 
233aa597f6SCédric Le Goater /*
243aa597f6SCédric Le Goater  * XIVE Virtualization Controller BAR and Thread Managment BAR that we
253aa597f6SCédric Le Goater  * use for the ESB pages and the TIMA pages
263aa597f6SCédric Le Goater  */
273aa597f6SCédric Le Goater #define SPAPR_XIVE_VC_BASE   0x0006010000000000ull
283aa597f6SCédric Le Goater #define SPAPR_XIVE_TM_BASE   0x0006030203180000ull
293aa597f6SCédric Le Goater 
303aa597f6SCédric Le Goater /*
310cddee8dSCédric Le Goater  * The allocation of VP blocks is a complex operation in OPAL and the
320cddee8dSCédric Le Goater  * VP identifiers have a relation with the number of HW chips, the
330cddee8dSCédric Le Goater  * size of the VP blocks, VP grouping, etc. The QEMU sPAPR XIVE
340cddee8dSCédric Le Goater  * controller model does not have the same constraints and can use a
350cddee8dSCédric Le Goater  * simple mapping scheme of the CPU vcpu_id
360cddee8dSCédric Le Goater  *
370cddee8dSCédric Le Goater  * These identifiers are never returned to the OS.
380cddee8dSCédric Le Goater  */
390cddee8dSCédric Le Goater 
400cddee8dSCédric Le Goater #define SPAPR_XIVE_NVT_BASE 0x400
410cddee8dSCédric Le Goater 
420cddee8dSCédric Le Goater /*
4323bcd5ebSCédric Le Goater  * The sPAPR machine has a unique XIVE IC device. Assign a fixed value
4423bcd5ebSCédric Le Goater  * to the controller block id value. It can nevertheless be changed
4523bcd5ebSCédric Le Goater  * for testing purpose.
4623bcd5ebSCédric Le Goater  */
4723bcd5ebSCédric Le Goater #define SPAPR_XIVE_BLOCK_ID 0x0
4823bcd5ebSCédric Le Goater 
4923bcd5ebSCédric Le Goater /*
500cddee8dSCédric Le Goater  * sPAPR NVT and END indexing helpers
510cddee8dSCédric Le Goater  */
520cddee8dSCédric Le Goater static uint32_t spapr_xive_nvt_to_target(uint8_t nvt_blk, uint32_t nvt_idx)
530cddee8dSCédric Le Goater {
540cddee8dSCédric Le Goater     return nvt_idx - SPAPR_XIVE_NVT_BASE;
550cddee8dSCédric Le Goater }
560cddee8dSCédric Le Goater 
5723bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_nvt(PowerPCCPU *cpu,
5823bcd5ebSCédric Le Goater                                   uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
5923bcd5ebSCédric Le Goater {
6023bcd5ebSCédric Le Goater     assert(cpu);
6123bcd5ebSCédric Le Goater 
6223bcd5ebSCédric Le Goater     if (out_nvt_blk) {
6323bcd5ebSCédric Le Goater         *out_nvt_blk = SPAPR_XIVE_BLOCK_ID;
6423bcd5ebSCédric Le Goater     }
6523bcd5ebSCédric Le Goater 
6623bcd5ebSCédric Le Goater     if (out_nvt_blk) {
6723bcd5ebSCédric Le Goater         *out_nvt_idx = SPAPR_XIVE_NVT_BASE + cpu->vcpu_id;
6823bcd5ebSCédric Le Goater     }
6923bcd5ebSCédric Le Goater }
7023bcd5ebSCédric Le Goater 
7123bcd5ebSCédric Le Goater static int spapr_xive_target_to_nvt(uint32_t target,
7223bcd5ebSCédric Le Goater                                     uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
7323bcd5ebSCédric Le Goater {
7423bcd5ebSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(target);
7523bcd5ebSCédric Le Goater 
7623bcd5ebSCédric Le Goater     if (!cpu) {
7723bcd5ebSCédric Le Goater         return -1;
7823bcd5ebSCédric Le Goater     }
7923bcd5ebSCédric Le Goater 
8023bcd5ebSCédric Le Goater     spapr_xive_cpu_to_nvt(cpu, out_nvt_blk, out_nvt_idx);
8123bcd5ebSCédric Le Goater     return 0;
8223bcd5ebSCédric Le Goater }
8323bcd5ebSCédric Le Goater 
8423bcd5ebSCédric Le Goater /*
8523bcd5ebSCédric Le Goater  * sPAPR END indexing uses a simple mapping of the CPU vcpu_id, 8
8623bcd5ebSCédric Le Goater  * priorities per CPU
8723bcd5ebSCédric Le Goater  */
8823bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_end(PowerPCCPU *cpu, uint8_t prio,
8923bcd5ebSCédric Le Goater                                   uint8_t *out_end_blk, uint32_t *out_end_idx)
9023bcd5ebSCédric Le Goater {
9123bcd5ebSCédric Le Goater     assert(cpu);
9223bcd5ebSCédric Le Goater 
9323bcd5ebSCédric Le Goater     if (out_end_blk) {
9423bcd5ebSCédric Le Goater         *out_end_blk = SPAPR_XIVE_BLOCK_ID;
9523bcd5ebSCédric Le Goater     }
9623bcd5ebSCédric Le Goater 
9723bcd5ebSCédric Le Goater     if (out_end_idx) {
9823bcd5ebSCédric Le Goater         *out_end_idx = (cpu->vcpu_id << 3) + prio;
9923bcd5ebSCédric Le Goater     }
10023bcd5ebSCédric Le Goater }
10123bcd5ebSCédric Le Goater 
10223bcd5ebSCédric Le Goater static int spapr_xive_target_to_end(uint32_t target, 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     PowerPCCPU *cpu = spapr_find_cpu(target);
10623bcd5ebSCédric Le Goater 
10723bcd5ebSCédric Le Goater     if (!cpu) {
10823bcd5ebSCédric Le Goater         return -1;
10923bcd5ebSCédric Le Goater     }
11023bcd5ebSCédric Le Goater 
11123bcd5ebSCédric Le Goater     spapr_xive_cpu_to_end(cpu, prio, out_end_blk, out_end_idx);
11223bcd5ebSCédric Le Goater     return 0;
11323bcd5ebSCédric Le Goater }
11423bcd5ebSCédric Le Goater 
1150cddee8dSCédric Le Goater /*
1163aa597f6SCédric Le Goater  * On sPAPR machines, use a simplified output for the XIVE END
1173aa597f6SCédric Le Goater  * structure dumping only the information related to the OS EQ.
1183aa597f6SCédric Le Goater  */
1193aa597f6SCédric Le Goater static void spapr_xive_end_pic_print_info(sPAPRXive *xive, XiveEND *end,
1203aa597f6SCédric Le Goater                                           Monitor *mon)
1213aa597f6SCédric Le Goater {
1223aa597f6SCédric Le Goater     uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
1233aa597f6SCédric Le Goater     uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
1243aa597f6SCédric Le Goater     uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
1253aa597f6SCédric Le Goater     uint32_t qentries = 1 << (qsize + 10);
1263aa597f6SCédric Le Goater     uint32_t nvt = xive_get_field32(END_W6_NVT_INDEX, end->w6);
1273aa597f6SCédric Le Goater     uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
1283aa597f6SCédric Le Goater 
1290cddee8dSCédric Le Goater     monitor_printf(mon, "%3d/%d % 6d/%5d ^%d",
1300cddee8dSCédric Le Goater                    spapr_xive_nvt_to_target(0, nvt),
1313aa597f6SCédric Le Goater                    priority, qindex, qentries, qgen);
1323aa597f6SCédric Le Goater 
1333aa597f6SCédric Le Goater     xive_end_queue_pic_print_info(end, 6, mon);
1343aa597f6SCédric Le Goater     monitor_printf(mon, "]");
1353aa597f6SCédric Le Goater }
1363aa597f6SCédric Le Goater 
1373aa597f6SCédric Le Goater void spapr_xive_pic_print_info(sPAPRXive *xive, Monitor *mon)
1383aa597f6SCédric Le Goater {
1393aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
1403aa597f6SCédric Le Goater     int i;
1413aa597f6SCédric Le Goater 
1423aa597f6SCédric Le Goater     monitor_printf(mon, "  LSIN         PQ    EISN     CPU/PRIO EQ\n");
1433aa597f6SCédric Le Goater 
1443aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
1453aa597f6SCédric Le Goater         uint8_t pq = xive_source_esb_get(xsrc, i);
1463aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
1473aa597f6SCédric Le Goater 
1483aa597f6SCédric Le Goater         if (!xive_eas_is_valid(eas)) {
1493aa597f6SCédric Le Goater             continue;
1503aa597f6SCédric Le Goater         }
1513aa597f6SCédric Le Goater 
1523aa597f6SCédric Le Goater         monitor_printf(mon, "  %08x %s %c%c%c %s %08x ", i,
1533aa597f6SCédric Le Goater                        xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
1543aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
1553aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
1563aa597f6SCédric Le Goater                        xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ',
1573aa597f6SCédric Le Goater                        xive_eas_is_masked(eas) ? "M" : " ",
1583aa597f6SCédric Le Goater                        (int) xive_get_field64(EAS_END_DATA, eas->w));
1593aa597f6SCédric Le Goater 
1603aa597f6SCédric Le Goater         if (!xive_eas_is_masked(eas)) {
1613aa597f6SCédric Le Goater             uint32_t end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
1623aa597f6SCédric Le Goater             XiveEND *end;
1633aa597f6SCédric Le Goater 
1643aa597f6SCédric Le Goater             assert(end_idx < xive->nr_ends);
1653aa597f6SCédric Le Goater             end = &xive->endt[end_idx];
1663aa597f6SCédric Le Goater 
1673aa597f6SCédric Le Goater             if (xive_end_is_valid(end)) {
1683aa597f6SCédric Le Goater                 spapr_xive_end_pic_print_info(xive, end, mon);
1693aa597f6SCédric Le Goater             }
1703aa597f6SCédric Le Goater         }
1713aa597f6SCédric Le Goater         monitor_printf(mon, "\n");
1723aa597f6SCédric Le Goater     }
1733aa597f6SCédric Le Goater }
1743aa597f6SCédric Le Goater 
1753aa597f6SCédric Le Goater static void spapr_xive_map_mmio(sPAPRXive *xive)
1763aa597f6SCédric Le Goater {
1773aa597f6SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->vc_base);
1783aa597f6SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1, xive->end_base);
1793aa597f6SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 2, xive->tm_base);
1803aa597f6SCédric Le Goater }
1813aa597f6SCédric Le Goater 
182*3a8eb78eSCédric Le Goater void spapr_xive_mmio_set_enabled(sPAPRXive *xive, bool enable)
183*3a8eb78eSCédric Le Goater {
184*3a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->source.esb_mmio, enable);
185*3a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->tm_mmio, enable);
186*3a8eb78eSCédric Le Goater 
187*3a8eb78eSCédric Le Goater     /* Disable the END ESBs until a guest OS makes use of them */
188*3a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->end_source.esb_mmio, false);
189*3a8eb78eSCédric Le Goater }
190*3a8eb78eSCédric Le Goater 
191b2e22477SCédric Le Goater /*
192b2e22477SCédric Le Goater  * When a Virtual Processor is scheduled to run on a HW thread, the
193b2e22477SCédric Le Goater  * hypervisor pushes its identifier in the OS CAM line. Emulate the
194b2e22477SCédric Le Goater  * same behavior under QEMU.
195b2e22477SCédric Le Goater  */
196b2e22477SCédric Le Goater void spapr_xive_set_tctx_os_cam(XiveTCTX *tctx)
197b2e22477SCédric Le Goater {
198b2e22477SCédric Le Goater     uint8_t  nvt_blk;
199b2e22477SCédric Le Goater     uint32_t nvt_idx;
200b2e22477SCédric Le Goater     uint32_t nvt_cam;
201b2e22477SCédric Le Goater 
202b2e22477SCédric Le Goater     spapr_xive_cpu_to_nvt(POWERPC_CPU(tctx->cs), &nvt_blk, &nvt_idx);
203b2e22477SCédric Le Goater 
204b2e22477SCédric Le Goater     nvt_cam = cpu_to_be32(TM_QW1W2_VO | xive_nvt_cam_line(nvt_blk, nvt_idx));
205b2e22477SCédric Le Goater     memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &nvt_cam, 4);
206b2e22477SCédric Le Goater }
207b2e22477SCédric Le Goater 
2083aa597f6SCédric Le Goater static void spapr_xive_end_reset(XiveEND *end)
2093aa597f6SCédric Le Goater {
2103aa597f6SCédric Le Goater     memset(end, 0, sizeof(*end));
2113aa597f6SCédric Le Goater 
2123aa597f6SCédric Le Goater     /* switch off the escalation and notification ESBs */
2133aa597f6SCédric Le Goater     end->w1 = cpu_to_be32(END_W1_ESe_Q | END_W1_ESn_Q);
2143aa597f6SCédric Le Goater }
2153aa597f6SCédric Le Goater 
2163aa597f6SCédric Le Goater static void spapr_xive_reset(void *dev)
2173aa597f6SCédric Le Goater {
2183aa597f6SCédric Le Goater     sPAPRXive *xive = SPAPR_XIVE(dev);
2193aa597f6SCédric Le Goater     int i;
2203aa597f6SCédric Le Goater 
2213aa597f6SCédric Le Goater     /*
2223aa597f6SCédric Le Goater      * The XiveSource has its own reset handler, which mask off all
2233aa597f6SCédric Le Goater      * IRQs (!P|Q)
2243aa597f6SCédric Le Goater      */
2253aa597f6SCédric Le Goater 
2263aa597f6SCédric Le Goater     /* Mask all valid EASs in the IRQ number space. */
2273aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
2283aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
2293aa597f6SCédric Le Goater         if (xive_eas_is_valid(eas)) {
2303aa597f6SCédric Le Goater             eas->w = cpu_to_be64(EAS_VALID | EAS_MASKED);
2313aa597f6SCédric Le Goater         } else {
2323aa597f6SCédric Le Goater             eas->w = 0;
2333aa597f6SCédric Le Goater         }
2343aa597f6SCédric Le Goater     }
2353aa597f6SCédric Le Goater 
2363aa597f6SCédric Le Goater     /* Clear all ENDs */
2373aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_ends; i++) {
2383aa597f6SCédric Le Goater         spapr_xive_end_reset(&xive->endt[i]);
2393aa597f6SCédric Le Goater     }
2403aa597f6SCédric Le Goater }
2413aa597f6SCédric Le Goater 
2423aa597f6SCédric Le Goater static void spapr_xive_instance_init(Object *obj)
2433aa597f6SCédric Le Goater {
2443aa597f6SCédric Le Goater     sPAPRXive *xive = SPAPR_XIVE(obj);
2453aa597f6SCédric Le Goater 
2463aa597f6SCédric Le Goater     object_initialize(&xive->source, sizeof(xive->source), TYPE_XIVE_SOURCE);
2473aa597f6SCédric Le Goater     object_property_add_child(obj, "source", OBJECT(&xive->source), NULL);
2483aa597f6SCédric Le Goater 
2493aa597f6SCédric Le Goater     object_initialize(&xive->end_source, sizeof(xive->end_source),
2503aa597f6SCédric Le Goater                       TYPE_XIVE_END_SOURCE);
2513aa597f6SCédric Le Goater     object_property_add_child(obj, "end_source", OBJECT(&xive->end_source),
2523aa597f6SCédric Le Goater                               NULL);
2533aa597f6SCédric Le Goater }
2543aa597f6SCédric Le Goater 
2553aa597f6SCédric Le Goater static void spapr_xive_realize(DeviceState *dev, Error **errp)
2563aa597f6SCédric Le Goater {
2573aa597f6SCédric Le Goater     sPAPRXive *xive = SPAPR_XIVE(dev);
2583aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
2593aa597f6SCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
2603aa597f6SCédric Le Goater     Error *local_err = NULL;
2613aa597f6SCédric Le Goater 
2623aa597f6SCédric Le Goater     if (!xive->nr_irqs) {
2633aa597f6SCédric Le Goater         error_setg(errp, "Number of interrupt needs to be greater 0");
2643aa597f6SCédric Le Goater         return;
2653aa597f6SCédric Le Goater     }
2663aa597f6SCédric Le Goater 
2673aa597f6SCédric Le Goater     if (!xive->nr_ends) {
2683aa597f6SCédric Le Goater         error_setg(errp, "Number of interrupt needs to be greater 0");
2693aa597f6SCédric Le Goater         return;
2703aa597f6SCédric Le Goater     }
2713aa597f6SCédric Le Goater 
2723aa597f6SCédric Le Goater     /*
2733aa597f6SCédric Le Goater      * Initialize the internal sources, for IPIs and virtual devices.
2743aa597f6SCédric Le Goater      */
2753aa597f6SCédric Le Goater     object_property_set_int(OBJECT(xsrc), xive->nr_irqs, "nr-irqs",
2763aa597f6SCédric Le Goater                             &error_fatal);
2773aa597f6SCédric Le Goater     object_property_add_const_link(OBJECT(xsrc), "xive", OBJECT(xive),
2783aa597f6SCédric Le Goater                                    &error_fatal);
2793aa597f6SCédric Le Goater     object_property_set_bool(OBJECT(xsrc), true, "realized", &local_err);
2803aa597f6SCédric Le Goater     if (local_err) {
2813aa597f6SCédric Le Goater         error_propagate(errp, local_err);
2823aa597f6SCédric Le Goater         return;
2833aa597f6SCédric Le Goater     }
2843aa597f6SCédric Le Goater 
2853aa597f6SCédric Le Goater     /*
2863aa597f6SCédric Le Goater      * Initialize the END ESB source
2873aa597f6SCédric Le Goater      */
2883aa597f6SCédric Le Goater     object_property_set_int(OBJECT(end_xsrc), xive->nr_irqs, "nr-ends",
2893aa597f6SCédric Le Goater                             &error_fatal);
2903aa597f6SCédric Le Goater     object_property_add_const_link(OBJECT(end_xsrc), "xive", OBJECT(xive),
2913aa597f6SCédric Le Goater                                    &error_fatal);
2923aa597f6SCédric Le Goater     object_property_set_bool(OBJECT(end_xsrc), true, "realized", &local_err);
2933aa597f6SCédric Le Goater     if (local_err) {
2943aa597f6SCédric Le Goater         error_propagate(errp, local_err);
2953aa597f6SCédric Le Goater         return;
2963aa597f6SCédric Le Goater     }
2973aa597f6SCédric Le Goater 
2983aa597f6SCédric Le Goater     /* Set the mapping address of the END ESB pages after the source ESBs */
2993aa597f6SCédric Le Goater     xive->end_base = xive->vc_base + (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
3003aa597f6SCédric Le Goater 
3013aa597f6SCédric Le Goater     /*
3023aa597f6SCédric Le Goater      * Allocate the routing tables
3033aa597f6SCédric Le Goater      */
3043aa597f6SCédric Le Goater     xive->eat = g_new0(XiveEAS, xive->nr_irqs);
3053aa597f6SCédric Le Goater     xive->endt = g_new0(XiveEND, xive->nr_ends);
3063aa597f6SCédric Le Goater 
3073aa597f6SCédric Le Goater     /* TIMA initialization */
3083aa597f6SCédric Le Goater     memory_region_init_io(&xive->tm_mmio, OBJECT(xive), &xive_tm_ops, xive,
3093aa597f6SCédric Le Goater                           "xive.tima", 4ull << TM_SHIFT);
3103aa597f6SCédric Le Goater 
3113aa597f6SCédric Le Goater     /* Define all XIVE MMIO regions on SysBus */
3123aa597f6SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xsrc->esb_mmio);
3133aa597f6SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &end_xsrc->esb_mmio);
3143aa597f6SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xive->tm_mmio);
3153aa597f6SCédric Le Goater 
3163aa597f6SCédric Le Goater     /* Map all regions */
3173aa597f6SCédric Le Goater     spapr_xive_map_mmio(xive);
3183aa597f6SCédric Le Goater 
3193aa597f6SCédric Le Goater     qemu_register_reset(spapr_xive_reset, dev);
3203aa597f6SCédric Le Goater }
3213aa597f6SCédric Le Goater 
3223aa597f6SCédric Le Goater static int spapr_xive_get_eas(XiveRouter *xrtr, uint8_t eas_blk,
3233aa597f6SCédric Le Goater                               uint32_t eas_idx, XiveEAS *eas)
3243aa597f6SCédric Le Goater {
3253aa597f6SCédric Le Goater     sPAPRXive *xive = SPAPR_XIVE(xrtr);
3263aa597f6SCédric Le Goater 
3273aa597f6SCédric Le Goater     if (eas_idx >= xive->nr_irqs) {
3283aa597f6SCédric Le Goater         return -1;
3293aa597f6SCédric Le Goater     }
3303aa597f6SCédric Le Goater 
3313aa597f6SCédric Le Goater     *eas = xive->eat[eas_idx];
3323aa597f6SCédric Le Goater     return 0;
3333aa597f6SCédric Le Goater }
3343aa597f6SCédric Le Goater 
3353aa597f6SCédric Le Goater static int spapr_xive_get_end(XiveRouter *xrtr,
3363aa597f6SCédric Le Goater                               uint8_t end_blk, uint32_t end_idx, XiveEND *end)
3373aa597f6SCédric Le Goater {
3383aa597f6SCédric Le Goater     sPAPRXive *xive = SPAPR_XIVE(xrtr);
3393aa597f6SCédric Le Goater 
3403aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
3413aa597f6SCédric Le Goater         return -1;
3423aa597f6SCédric Le Goater     }
3433aa597f6SCédric Le Goater 
3443aa597f6SCédric Le Goater     memcpy(end, &xive->endt[end_idx], sizeof(XiveEND));
3453aa597f6SCédric Le Goater     return 0;
3463aa597f6SCédric Le Goater }
3473aa597f6SCédric Le Goater 
3483aa597f6SCédric Le Goater static int spapr_xive_write_end(XiveRouter *xrtr, uint8_t end_blk,
3493aa597f6SCédric Le Goater                                 uint32_t end_idx, XiveEND *end,
3503aa597f6SCédric Le Goater                                 uint8_t word_number)
3513aa597f6SCédric Le Goater {
3523aa597f6SCédric Le Goater     sPAPRXive *xive = SPAPR_XIVE(xrtr);
3533aa597f6SCédric Le Goater 
3543aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
3553aa597f6SCédric Le Goater         return -1;
3563aa597f6SCédric Le Goater     }
3573aa597f6SCédric Le Goater 
3583aa597f6SCédric Le Goater     memcpy(&xive->endt[end_idx], end, sizeof(XiveEND));
3593aa597f6SCédric Le Goater     return 0;
3603aa597f6SCédric Le Goater }
3613aa597f6SCédric Le Goater 
3620cddee8dSCédric Le Goater static int spapr_xive_get_nvt(XiveRouter *xrtr,
3630cddee8dSCédric Le Goater                               uint8_t nvt_blk, uint32_t nvt_idx, XiveNVT *nvt)
3640cddee8dSCédric Le Goater {
3650cddee8dSCédric Le Goater     uint32_t vcpu_id = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
3660cddee8dSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
3670cddee8dSCédric Le Goater 
3680cddee8dSCédric Le Goater     if (!cpu) {
3690cddee8dSCédric Le Goater         /* TODO: should we assert() if we can find a NVT ? */
3700cddee8dSCédric Le Goater         return -1;
3710cddee8dSCédric Le Goater     }
3720cddee8dSCédric Le Goater 
3730cddee8dSCédric Le Goater     /*
3740cddee8dSCédric Le Goater      * sPAPR does not maintain a NVT table. Return that the NVT is
3750cddee8dSCédric Le Goater      * valid if we have found a matching CPU
3760cddee8dSCédric Le Goater      */
3770cddee8dSCédric Le Goater     nvt->w0 = cpu_to_be32(NVT_W0_VALID);
3780cddee8dSCédric Le Goater     return 0;
3790cddee8dSCédric Le Goater }
3800cddee8dSCédric Le Goater 
3810cddee8dSCédric Le Goater static int spapr_xive_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk,
3820cddee8dSCédric Le Goater                                 uint32_t nvt_idx, XiveNVT *nvt,
3830cddee8dSCédric Le Goater                                 uint8_t word_number)
3840cddee8dSCédric Le Goater {
3850cddee8dSCédric Le Goater     /*
3860cddee8dSCédric Le Goater      * We don't need to write back to the NVTs because the sPAPR
3870cddee8dSCédric Le Goater      * machine should never hit a non-scheduled NVT. It should never
3880cddee8dSCédric Le Goater      * get called.
3890cddee8dSCédric Le Goater      */
3900cddee8dSCédric Le Goater     g_assert_not_reached();
3910cddee8dSCédric Le Goater }
3920cddee8dSCédric Le Goater 
3933aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_end = {
3943aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/end",
3953aa597f6SCédric Le Goater     .version_id = 1,
3963aa597f6SCédric Le Goater     .minimum_version_id = 1,
3973aa597f6SCédric Le Goater     .fields = (VMStateField []) {
3983aa597f6SCédric Le Goater         VMSTATE_UINT32(w0, XiveEND),
3993aa597f6SCédric Le Goater         VMSTATE_UINT32(w1, XiveEND),
4003aa597f6SCédric Le Goater         VMSTATE_UINT32(w2, XiveEND),
4013aa597f6SCédric Le Goater         VMSTATE_UINT32(w3, XiveEND),
4023aa597f6SCédric Le Goater         VMSTATE_UINT32(w4, XiveEND),
4033aa597f6SCédric Le Goater         VMSTATE_UINT32(w5, XiveEND),
4043aa597f6SCédric Le Goater         VMSTATE_UINT32(w6, XiveEND),
4053aa597f6SCédric Le Goater         VMSTATE_UINT32(w7, XiveEND),
4063aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
4073aa597f6SCédric Le Goater     },
4083aa597f6SCédric Le Goater };
4093aa597f6SCédric Le Goater 
4103aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_eas = {
4113aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/eas",
4123aa597f6SCédric Le Goater     .version_id = 1,
4133aa597f6SCédric Le Goater     .minimum_version_id = 1,
4143aa597f6SCédric Le Goater     .fields = (VMStateField []) {
4153aa597f6SCédric Le Goater         VMSTATE_UINT64(w, XiveEAS),
4163aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
4173aa597f6SCédric Le Goater     },
4183aa597f6SCédric Le Goater };
4193aa597f6SCédric Le Goater 
4203aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive = {
4213aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
4223aa597f6SCédric Le Goater     .version_id = 1,
4233aa597f6SCédric Le Goater     .minimum_version_id = 1,
4243aa597f6SCédric Le Goater     .fields = (VMStateField[]) {
4253aa597f6SCédric Le Goater         VMSTATE_UINT32_EQUAL(nr_irqs, sPAPRXive, NULL),
4263aa597f6SCédric Le Goater         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(eat, sPAPRXive, nr_irqs,
4273aa597f6SCédric Le Goater                                      vmstate_spapr_xive_eas, XiveEAS),
4283aa597f6SCédric Le Goater         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(endt, sPAPRXive, nr_ends,
4293aa597f6SCédric Le Goater                                              vmstate_spapr_xive_end, XiveEND),
4303aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
4313aa597f6SCédric Le Goater     },
4323aa597f6SCédric Le Goater };
4333aa597f6SCédric Le Goater 
4343aa597f6SCédric Le Goater static Property spapr_xive_properties[] = {
4353aa597f6SCédric Le Goater     DEFINE_PROP_UINT32("nr-irqs", sPAPRXive, nr_irqs, 0),
4363aa597f6SCédric Le Goater     DEFINE_PROP_UINT32("nr-ends", sPAPRXive, nr_ends, 0),
4373aa597f6SCédric Le Goater     DEFINE_PROP_UINT64("vc-base", sPAPRXive, vc_base, SPAPR_XIVE_VC_BASE),
4383aa597f6SCédric Le Goater     DEFINE_PROP_UINT64("tm-base", sPAPRXive, tm_base, SPAPR_XIVE_TM_BASE),
4393aa597f6SCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
4403aa597f6SCédric Le Goater };
4413aa597f6SCédric Le Goater 
4423aa597f6SCédric Le Goater static void spapr_xive_class_init(ObjectClass *klass, void *data)
4433aa597f6SCédric Le Goater {
4443aa597f6SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
4453aa597f6SCédric Le Goater     XiveRouterClass *xrc = XIVE_ROUTER_CLASS(klass);
4463aa597f6SCédric Le Goater 
4473aa597f6SCédric Le Goater     dc->desc    = "sPAPR XIVE Interrupt Controller";
4483aa597f6SCédric Le Goater     dc->props   = spapr_xive_properties;
4493aa597f6SCédric Le Goater     dc->realize = spapr_xive_realize;
4503aa597f6SCédric Le Goater     dc->vmsd    = &vmstate_spapr_xive;
4513aa597f6SCédric Le Goater 
4523aa597f6SCédric Le Goater     xrc->get_eas = spapr_xive_get_eas;
4533aa597f6SCédric Le Goater     xrc->get_end = spapr_xive_get_end;
4543aa597f6SCédric Le Goater     xrc->write_end = spapr_xive_write_end;
4550cddee8dSCédric Le Goater     xrc->get_nvt = spapr_xive_get_nvt;
4560cddee8dSCédric Le Goater     xrc->write_nvt = spapr_xive_write_nvt;
4573aa597f6SCédric Le Goater }
4583aa597f6SCédric Le Goater 
4593aa597f6SCédric Le Goater static const TypeInfo spapr_xive_info = {
4603aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
4613aa597f6SCédric Le Goater     .parent = TYPE_XIVE_ROUTER,
4623aa597f6SCédric Le Goater     .instance_init = spapr_xive_instance_init,
4633aa597f6SCédric Le Goater     .instance_size = sizeof(sPAPRXive),
4643aa597f6SCédric Le Goater     .class_init = spapr_xive_class_init,
4653aa597f6SCédric Le Goater };
4663aa597f6SCédric Le Goater 
4673aa597f6SCédric Le Goater static void spapr_xive_register_types(void)
4683aa597f6SCédric Le Goater {
4693aa597f6SCédric Le Goater     type_register_static(&spapr_xive_info);
4703aa597f6SCédric Le Goater }
4713aa597f6SCédric Le Goater 
4723aa597f6SCédric Le Goater type_init(spapr_xive_register_types)
4733aa597f6SCédric Le Goater 
4743aa597f6SCédric Le Goater bool spapr_xive_irq_claim(sPAPRXive *xive, uint32_t lisn, bool lsi)
4753aa597f6SCédric Le Goater {
4763aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
4773aa597f6SCédric Le Goater 
4783aa597f6SCédric Le Goater     if (lisn >= xive->nr_irqs) {
4793aa597f6SCédric Le Goater         return false;
4803aa597f6SCédric Le Goater     }
4813aa597f6SCédric Le Goater 
4823aa597f6SCédric Le Goater     xive->eat[lisn].w |= cpu_to_be64(EAS_VALID);
4833aa597f6SCédric Le Goater     xive_source_irq_set(xsrc, lisn, lsi);
4843aa597f6SCédric Le Goater     return true;
4853aa597f6SCédric Le Goater }
4863aa597f6SCédric Le Goater 
4873aa597f6SCédric Le Goater bool spapr_xive_irq_free(sPAPRXive *xive, uint32_t lisn)
4883aa597f6SCédric Le Goater {
4893aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
4903aa597f6SCédric Le Goater 
4913aa597f6SCédric Le Goater     if (lisn >= xive->nr_irqs) {
4923aa597f6SCédric Le Goater         return false;
4933aa597f6SCédric Le Goater     }
4943aa597f6SCédric Le Goater 
4953aa597f6SCédric Le Goater     xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
4963aa597f6SCédric Le Goater     xive_source_irq_set(xsrc, lisn, false);
4973aa597f6SCédric Le Goater     return true;
4983aa597f6SCédric Le Goater }
4993aa597f6SCédric Le Goater 
50023bcd5ebSCédric Le Goater /*
50123bcd5ebSCédric Le Goater  * XIVE hcalls
50223bcd5ebSCédric Le Goater  *
50323bcd5ebSCédric Le Goater  * The terminology used by the XIVE hcalls is the following :
50423bcd5ebSCédric Le Goater  *
50523bcd5ebSCédric Le Goater  *   TARGET vCPU number
50623bcd5ebSCédric Le Goater  *   EQ     Event Queue assigned by OS to receive event data
50723bcd5ebSCédric Le Goater  *   ESB    page for source interrupt management
50823bcd5ebSCédric Le Goater  *   LISN   Logical Interrupt Source Number identifying a source in the
50923bcd5ebSCédric Le Goater  *          machine
51023bcd5ebSCédric Le Goater  *   EISN   Effective Interrupt Source Number used by guest OS to
51123bcd5ebSCédric Le Goater  *          identify source in the guest
51223bcd5ebSCédric Le Goater  *
51323bcd5ebSCédric Le Goater  * The EAS, END, NVT structures are not exposed.
51423bcd5ebSCédric Le Goater  */
51523bcd5ebSCédric Le Goater 
51623bcd5ebSCédric Le Goater /*
51723bcd5ebSCédric Le Goater  * Linux hosts under OPAL reserve priority 7 for their own escalation
51823bcd5ebSCédric Le Goater  * interrupts (DD2.X POWER9). So we only allow the guest to use
51923bcd5ebSCédric Le Goater  * priorities [0..6].
52023bcd5ebSCédric Le Goater  */
52123bcd5ebSCédric Le Goater static bool spapr_xive_priority_is_reserved(uint8_t priority)
52223bcd5ebSCédric Le Goater {
52323bcd5ebSCédric Le Goater     switch (priority) {
52423bcd5ebSCédric Le Goater     case 0 ... 6:
52523bcd5ebSCédric Le Goater         return false;
52623bcd5ebSCédric Le Goater     case 7: /* OPAL escalation queue */
52723bcd5ebSCédric Le Goater     default:
52823bcd5ebSCédric Le Goater         return true;
52923bcd5ebSCédric Le Goater     }
53023bcd5ebSCédric Le Goater }
53123bcd5ebSCédric Le Goater 
53223bcd5ebSCédric Le Goater /*
53323bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical
53423bcd5ebSCédric Le Goater  * real address of the MMIO page through which the Event State Buffer
53523bcd5ebSCédric Le Goater  * entry associated with the value of the "lisn" parameter is managed.
53623bcd5ebSCédric Le Goater  *
53723bcd5ebSCédric Le Goater  * Parameters:
53823bcd5ebSCédric Le Goater  * Input
53923bcd5ebSCédric Le Goater  * - R4: "flags"
54023bcd5ebSCédric Le Goater  *         Bits 0-63 reserved
54123bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
54223bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
54323bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned
54423bcd5ebSCédric Le Goater  *       by the H_ALLOCATE_VAS_WINDOW hcall
54523bcd5ebSCédric Le Goater  *
54623bcd5ebSCédric Le Goater  * Output
54723bcd5ebSCédric Le Goater  * - R4: "flags"
54823bcd5ebSCédric Le Goater  *         Bits 0-59: Reserved
54923bcd5ebSCédric Le Goater  *         Bit 60: H_INT_ESB must be used for Event State Buffer
55023bcd5ebSCédric Le Goater  *                 management
55123bcd5ebSCédric Le Goater  *         Bit 61: 1 == LSI  0 == MSI
55223bcd5ebSCédric Le Goater  *         Bit 62: the full function page supports trigger
55323bcd5ebSCédric Le Goater  *         Bit 63: Store EOI Supported
55423bcd5ebSCédric Le Goater  * - R5: Logical Real address of full function Event State Buffer
55523bcd5ebSCédric Le Goater  *       management page, -1 if H_INT_ESB hcall flag is set to 1.
55623bcd5ebSCédric Le Goater  * - R6: Logical Real Address of trigger only Event State Buffer
55723bcd5ebSCédric Le Goater  *       management page or -1.
55823bcd5ebSCédric Le Goater  * - R7: Power of 2 page size for the ESB management pages returned in
55923bcd5ebSCédric Le Goater  *       R5 and R6.
56023bcd5ebSCédric Le Goater  */
56123bcd5ebSCédric Le Goater 
56223bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_H_INT_ESB     PPC_BIT(60) /* ESB manage with H_INT_ESB */
56323bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_LSI           PPC_BIT(61) /* Virtual LSI type */
56423bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_TRIGGER       PPC_BIT(62) /* Trigger and management
56523bcd5ebSCédric Le Goater                                                     on same page */
56623bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_STORE_EOI     PPC_BIT(63) /* Store EOI support */
56723bcd5ebSCédric Le Goater 
56823bcd5ebSCédric Le Goater static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
56923bcd5ebSCédric Le Goater                                           sPAPRMachineState *spapr,
57023bcd5ebSCédric Le Goater                                           target_ulong opcode,
57123bcd5ebSCédric Le Goater                                           target_ulong *args)
57223bcd5ebSCédric Le Goater {
57323bcd5ebSCédric Le Goater     sPAPRXive *xive = spapr->xive;
57423bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
57523bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
57623bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
57723bcd5ebSCédric Le Goater 
57823bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
57923bcd5ebSCédric Le Goater         return H_FUNCTION;
58023bcd5ebSCédric Le Goater     }
58123bcd5ebSCédric Le Goater 
58223bcd5ebSCédric Le Goater     if (flags) {
58323bcd5ebSCédric Le Goater         return H_PARAMETER;
58423bcd5ebSCédric Le Goater     }
58523bcd5ebSCédric Le Goater 
58623bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
58723bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
58823bcd5ebSCédric Le Goater                       lisn);
58923bcd5ebSCédric Le Goater         return H_P2;
59023bcd5ebSCédric Le Goater     }
59123bcd5ebSCédric Le Goater 
59223bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&xive->eat[lisn])) {
59323bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
59423bcd5ebSCédric Le Goater                       lisn);
59523bcd5ebSCédric Le Goater         return H_P2;
59623bcd5ebSCédric Le Goater     }
59723bcd5ebSCédric Le Goater 
59823bcd5ebSCédric Le Goater     /*
59923bcd5ebSCédric Le Goater      * All sources are emulated under the main XIVE object and share
60023bcd5ebSCédric Le Goater      * the same characteristics.
60123bcd5ebSCédric Le Goater      */
60223bcd5ebSCédric Le Goater     args[0] = 0;
60323bcd5ebSCédric Le Goater     if (!xive_source_esb_has_2page(xsrc)) {
60423bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_TRIGGER;
60523bcd5ebSCédric Le Goater     }
60623bcd5ebSCédric Le Goater     if (xsrc->esb_flags & XIVE_SRC_STORE_EOI) {
60723bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_STORE_EOI;
60823bcd5ebSCédric Le Goater     }
60923bcd5ebSCédric Le Goater 
61023bcd5ebSCédric Le Goater     /*
61123bcd5ebSCédric Le Goater      * Force the use of the H_INT_ESB hcall in case of an LSI
61223bcd5ebSCédric Le Goater      * interrupt. This is necessary under KVM to re-trigger the
61323bcd5ebSCédric Le Goater      * interrupt if the level is still asserted
61423bcd5ebSCédric Le Goater      */
61523bcd5ebSCédric Le Goater     if (xive_source_irq_is_lsi(xsrc, lisn)) {
61623bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_H_INT_ESB | SPAPR_XIVE_SRC_LSI;
61723bcd5ebSCédric Le Goater     }
61823bcd5ebSCédric Le Goater 
61923bcd5ebSCédric Le Goater     if (!(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
62023bcd5ebSCédric Le Goater         args[1] = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn);
62123bcd5ebSCédric Le Goater     } else {
62223bcd5ebSCédric Le Goater         args[1] = -1;
62323bcd5ebSCédric Le Goater     }
62423bcd5ebSCédric Le Goater 
62523bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc) &&
62623bcd5ebSCédric Le Goater         !(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
62723bcd5ebSCédric Le Goater         args[2] = xive->vc_base + xive_source_esb_page(xsrc, lisn);
62823bcd5ebSCédric Le Goater     } else {
62923bcd5ebSCédric Le Goater         args[2] = -1;
63023bcd5ebSCédric Le Goater     }
63123bcd5ebSCédric Le Goater 
63223bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc)) {
63323bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift - 1;
63423bcd5ebSCédric Le Goater     } else {
63523bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift;
63623bcd5ebSCédric Le Goater     }
63723bcd5ebSCédric Le Goater 
63823bcd5ebSCédric Le Goater     return H_SUCCESS;
63923bcd5ebSCédric Le Goater }
64023bcd5ebSCédric Le Goater 
64123bcd5ebSCédric Le Goater /*
64223bcd5ebSCédric Le Goater  * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
64323bcd5ebSCédric Le Goater  * Interrupt Source to a target. The Logical Interrupt Source is
64423bcd5ebSCédric Le Goater  * designated with the "lisn" parameter and the target is designated
64523bcd5ebSCédric Le Goater  * with the "target" and "priority" parameters.  Upon return from the
64623bcd5ebSCédric Le Goater  * hcall(), no additional interrupts will be directed to the old EQ.
64723bcd5ebSCédric Le Goater  *
64823bcd5ebSCédric Le Goater  * Parameters:
64923bcd5ebSCédric Le Goater  * Input:
65023bcd5ebSCédric Le Goater  * - R4: "flags"
65123bcd5ebSCédric Le Goater  *         Bits 0-61: Reserved
65223bcd5ebSCédric Le Goater  *         Bit 62: set the "eisn" in the EAS
65323bcd5ebSCédric Le Goater  *         Bit 63: masks the interrupt source in the hardware interrupt
65423bcd5ebSCédric Le Goater  *       control structure. An interrupt masked by this mechanism will
65523bcd5ebSCédric Le Goater  *       be dropped, but it's source state bits will still be
65623bcd5ebSCédric Le Goater  *       set. There is no race-free way of unmasking and restoring the
65723bcd5ebSCédric Le Goater  *       source. Thus this should only be used in interrupts that are
65823bcd5ebSCédric Le Goater  *       also masked at the source, and only in cases where the
65923bcd5ebSCédric Le Goater  *       interrupt is not meant to be used for a large amount of time
66023bcd5ebSCédric Le Goater  *       because no valid target exists for it for example
66123bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
66223bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
66323bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned by
66423bcd5ebSCédric Le Goater  *       the H_ALLOCATE_VAS_WINDOW hcall
66523bcd5ebSCédric Le Goater  * - R6: "target" is per "ibm,ppc-interrupt-server#s" or
66623bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
66723bcd5ebSCédric Le Goater  * - R7: "priority" is a valid priority not in
66823bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
66923bcd5ebSCédric Le Goater  * - R8: "eisn" is the guest EISN associated with the "lisn"
67023bcd5ebSCédric Le Goater  *
67123bcd5ebSCédric Le Goater  * Output:
67223bcd5ebSCédric Le Goater  * - None
67323bcd5ebSCédric Le Goater  */
67423bcd5ebSCédric Le Goater 
67523bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_SET_EISN PPC_BIT(62)
67623bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_MASK     PPC_BIT(63)
67723bcd5ebSCédric Le Goater 
67823bcd5ebSCédric Le Goater static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
67923bcd5ebSCédric Le Goater                                             sPAPRMachineState *spapr,
68023bcd5ebSCédric Le Goater                                             target_ulong opcode,
68123bcd5ebSCédric Le Goater                                             target_ulong *args)
68223bcd5ebSCédric Le Goater {
68323bcd5ebSCédric Le Goater     sPAPRXive *xive = spapr->xive;
68423bcd5ebSCédric Le Goater     XiveEAS eas, new_eas;
68523bcd5ebSCédric Le Goater     target_ulong flags    = args[0];
68623bcd5ebSCédric Le Goater     target_ulong lisn     = args[1];
68723bcd5ebSCédric Le Goater     target_ulong target   = args[2];
68823bcd5ebSCédric Le Goater     target_ulong priority = args[3];
68923bcd5ebSCédric Le Goater     target_ulong eisn     = args[4];
69023bcd5ebSCédric Le Goater     uint8_t end_blk;
69123bcd5ebSCédric Le Goater     uint32_t end_idx;
69223bcd5ebSCédric Le Goater 
69323bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
69423bcd5ebSCédric Le Goater         return H_FUNCTION;
69523bcd5ebSCédric Le Goater     }
69623bcd5ebSCédric Le Goater 
69723bcd5ebSCédric Le Goater     if (flags & ~(SPAPR_XIVE_SRC_SET_EISN | SPAPR_XIVE_SRC_MASK)) {
69823bcd5ebSCédric Le Goater         return H_PARAMETER;
69923bcd5ebSCédric Le Goater     }
70023bcd5ebSCédric Le Goater 
70123bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
70223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
70323bcd5ebSCédric Le Goater                       lisn);
70423bcd5ebSCédric Le Goater         return H_P2;
70523bcd5ebSCédric Le Goater     }
70623bcd5ebSCédric Le Goater 
70723bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
70823bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
70923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
71023bcd5ebSCédric Le Goater                       lisn);
71123bcd5ebSCédric Le Goater         return H_P2;
71223bcd5ebSCédric Le Goater     }
71323bcd5ebSCédric Le Goater 
71423bcd5ebSCédric Le Goater     /* priority 0xff is used to reset the EAS */
71523bcd5ebSCédric Le Goater     if (priority == 0xff) {
71623bcd5ebSCédric Le Goater         new_eas.w = cpu_to_be64(EAS_VALID | EAS_MASKED);
71723bcd5ebSCédric Le Goater         goto out;
71823bcd5ebSCédric Le Goater     }
71923bcd5ebSCédric Le Goater 
72023bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_MASK) {
72123bcd5ebSCédric Le Goater         new_eas.w = eas.w | cpu_to_be64(EAS_MASKED);
72223bcd5ebSCédric Le Goater     } else {
72323bcd5ebSCédric Le Goater         new_eas.w = eas.w & cpu_to_be64(~EAS_MASKED);
72423bcd5ebSCédric Le Goater     }
72523bcd5ebSCédric Le Goater 
72623bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
72723bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
72823bcd5ebSCédric Le Goater                       " is reserved\n", priority);
72923bcd5ebSCédric Le Goater         return H_P4;
73023bcd5ebSCédric Le Goater     }
73123bcd5ebSCédric Le Goater 
73223bcd5ebSCédric Le Goater     /*
73323bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
73423bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
73523bcd5ebSCédric Le Goater      * target.
73623bcd5ebSCédric Le Goater      */
73723bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
73823bcd5ebSCédric Le Goater         return H_P3;
73923bcd5ebSCédric Le Goater     }
74023bcd5ebSCédric Le Goater 
74123bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_BLOCK, new_eas.w, end_blk);
74223bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_INDEX, new_eas.w, end_idx);
74323bcd5ebSCédric Le Goater 
74423bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_SET_EISN) {
74523bcd5ebSCédric Le Goater         new_eas.w = xive_set_field64(EAS_END_DATA, new_eas.w, eisn);
74623bcd5ebSCédric Le Goater     }
74723bcd5ebSCédric Le Goater 
74823bcd5ebSCédric Le Goater out:
74923bcd5ebSCédric Le Goater     xive->eat[lisn] = new_eas;
75023bcd5ebSCédric Le Goater     return H_SUCCESS;
75123bcd5ebSCédric Le Goater }
75223bcd5ebSCédric Le Goater 
75323bcd5ebSCédric Le Goater /*
75423bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
75523bcd5ebSCédric Le Goater  * target/priority pair is assigned to the specified Logical Interrupt
75623bcd5ebSCédric Le Goater  * Source.
75723bcd5ebSCédric Le Goater  *
75823bcd5ebSCédric Le Goater  * Parameters:
75923bcd5ebSCédric Le Goater  * Input:
76023bcd5ebSCédric Le Goater  * - R4: "flags"
76123bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
76223bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
76323bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
76423bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
76523bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
76623bcd5ebSCédric Le Goater  *
76723bcd5ebSCédric Le Goater  * Output:
76823bcd5ebSCédric Le Goater  * - R4: Target to which the specified Logical Interrupt Source is
76923bcd5ebSCédric Le Goater  *       assigned
77023bcd5ebSCédric Le Goater  * - R5: Priority to which the specified Logical Interrupt Source is
77123bcd5ebSCédric Le Goater  *       assigned
77223bcd5ebSCédric Le Goater  * - R6: EISN for the specified Logical Interrupt Source (this will be
77323bcd5ebSCédric Le Goater  *       equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
77423bcd5ebSCédric Le Goater  */
77523bcd5ebSCédric Le Goater static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
77623bcd5ebSCédric Le Goater                                             sPAPRMachineState *spapr,
77723bcd5ebSCédric Le Goater                                             target_ulong opcode,
77823bcd5ebSCédric Le Goater                                             target_ulong *args)
77923bcd5ebSCédric Le Goater {
78023bcd5ebSCédric Le Goater     sPAPRXive *xive = spapr->xive;
78123bcd5ebSCédric Le Goater     target_ulong flags = args[0];
78223bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
78323bcd5ebSCédric Le Goater     XiveEAS eas;
78423bcd5ebSCédric Le Goater     XiveEND *end;
78523bcd5ebSCédric Le Goater     uint8_t nvt_blk;
78623bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
78723bcd5ebSCédric Le Goater 
78823bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
78923bcd5ebSCédric Le Goater         return H_FUNCTION;
79023bcd5ebSCédric Le Goater     }
79123bcd5ebSCédric Le Goater 
79223bcd5ebSCédric Le Goater     if (flags) {
79323bcd5ebSCédric Le Goater         return H_PARAMETER;
79423bcd5ebSCédric Le Goater     }
79523bcd5ebSCédric Le Goater 
79623bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
79723bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
79823bcd5ebSCédric Le Goater                       lisn);
79923bcd5ebSCédric Le Goater         return H_P2;
80023bcd5ebSCédric Le Goater     }
80123bcd5ebSCédric Le Goater 
80223bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
80323bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
80423bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
80523bcd5ebSCédric Le Goater                       lisn);
80623bcd5ebSCédric Le Goater         return H_P2;
80723bcd5ebSCédric Le Goater     }
80823bcd5ebSCédric Le Goater 
80923bcd5ebSCédric Le Goater     /* EAS_END_BLOCK is unused on sPAPR */
81023bcd5ebSCédric Le Goater     end_idx = xive_get_field64(EAS_END_INDEX, eas.w);
81123bcd5ebSCédric Le Goater 
81223bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
81323bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
81423bcd5ebSCédric Le Goater 
81523bcd5ebSCédric Le Goater     nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
81623bcd5ebSCédric Le Goater     nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
81723bcd5ebSCédric Le Goater     args[0] = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
81823bcd5ebSCédric Le Goater 
81923bcd5ebSCédric Le Goater     if (xive_eas_is_masked(&eas)) {
82023bcd5ebSCédric Le Goater         args[1] = 0xff;
82123bcd5ebSCédric Le Goater     } else {
82223bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
82323bcd5ebSCédric Le Goater     }
82423bcd5ebSCédric Le Goater 
82523bcd5ebSCédric Le Goater     args[2] = xive_get_field64(EAS_END_DATA, eas.w);
82623bcd5ebSCédric Le Goater 
82723bcd5ebSCédric Le Goater     return H_SUCCESS;
82823bcd5ebSCédric Le Goater }
82923bcd5ebSCédric Le Goater 
83023bcd5ebSCédric Le Goater /*
83123bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
83223bcd5ebSCédric Le Goater  * address of the notification management page associated with the
83323bcd5ebSCédric Le Goater  * specified target and priority.
83423bcd5ebSCédric Le Goater  *
83523bcd5ebSCédric Le Goater  * Parameters:
83623bcd5ebSCédric Le Goater  * Input:
83723bcd5ebSCédric Le Goater  * - R4: "flags"
83823bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
83923bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
84023bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
84123bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
84223bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
84323bcd5ebSCédric Le Goater  *
84423bcd5ebSCédric Le Goater  * Output:
84523bcd5ebSCédric Le Goater  * - R4: Logical real address of notification page
84623bcd5ebSCédric Le Goater  * - R5: Power of 2 page size of the notification page
84723bcd5ebSCédric Le Goater  */
84823bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
84923bcd5ebSCédric Le Goater                                          sPAPRMachineState *spapr,
85023bcd5ebSCédric Le Goater                                          target_ulong opcode,
85123bcd5ebSCédric Le Goater                                          target_ulong *args)
85223bcd5ebSCédric Le Goater {
85323bcd5ebSCédric Le Goater     sPAPRXive *xive = spapr->xive;
85423bcd5ebSCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
85523bcd5ebSCédric Le Goater     target_ulong flags = args[0];
85623bcd5ebSCédric Le Goater     target_ulong target = args[1];
85723bcd5ebSCédric Le Goater     target_ulong priority = args[2];
85823bcd5ebSCédric Le Goater     XiveEND *end;
85923bcd5ebSCédric Le Goater     uint8_t end_blk;
86023bcd5ebSCédric Le Goater     uint32_t end_idx;
86123bcd5ebSCédric Le Goater 
86223bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
86323bcd5ebSCédric Le Goater         return H_FUNCTION;
86423bcd5ebSCédric Le Goater     }
86523bcd5ebSCédric Le Goater 
86623bcd5ebSCédric Le Goater     if (flags) {
86723bcd5ebSCédric Le Goater         return H_PARAMETER;
86823bcd5ebSCédric Le Goater     }
86923bcd5ebSCédric Le Goater 
87023bcd5ebSCédric Le Goater     /*
87123bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
87223bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
87323bcd5ebSCédric Le Goater      */
87423bcd5ebSCédric Le Goater 
87523bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
87623bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
87723bcd5ebSCédric Le Goater                       " is reserved\n", priority);
87823bcd5ebSCédric Le Goater         return H_P3;
87923bcd5ebSCédric Le Goater     }
88023bcd5ebSCédric Le Goater 
88123bcd5ebSCédric Le Goater     /*
88223bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
88323bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
88423bcd5ebSCédric Le Goater      * target.
88523bcd5ebSCédric Le Goater      */
88623bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
88723bcd5ebSCédric Le Goater         return H_P2;
88823bcd5ebSCédric Le Goater     }
88923bcd5ebSCédric Le Goater 
89023bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
89123bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
89223bcd5ebSCédric Le Goater 
89323bcd5ebSCédric Le Goater     args[0] = xive->end_base + (1ull << (end_xsrc->esb_shift + 1)) * end_idx;
89423bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
89523bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
89623bcd5ebSCédric Le Goater     } else {
89723bcd5ebSCédric Le Goater         args[1] = 0;
89823bcd5ebSCédric Le Goater     }
89923bcd5ebSCédric Le Goater 
90023bcd5ebSCédric Le Goater     return H_SUCCESS;
90123bcd5ebSCédric Le Goater }
90223bcd5ebSCédric Le Goater 
90323bcd5ebSCédric Le Goater /*
90423bcd5ebSCédric Le Goater  * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
90523bcd5ebSCédric Le Goater  * a given "target" and "priority".  It is also used to set the
90623bcd5ebSCédric Le Goater  * notification config associated with the EQ.  An EQ size of 0 is
90723bcd5ebSCédric Le Goater  * used to reset the EQ config for a given target and priority. If
90823bcd5ebSCédric Le Goater  * resetting the EQ config, the END associated with the given "target"
90923bcd5ebSCédric Le Goater  * and "priority" will be changed to disable queueing.
91023bcd5ebSCédric Le Goater  *
91123bcd5ebSCédric Le Goater  * Upon return from the hcall(), no additional interrupts will be
91223bcd5ebSCédric Le Goater  * directed to the old EQ (if one was set). The old EQ (if one was
91323bcd5ebSCédric Le Goater  * set) should be investigated for interrupts that occurred prior to
91423bcd5ebSCédric Le Goater  * or during the hcall().
91523bcd5ebSCédric Le Goater  *
91623bcd5ebSCédric Le Goater  * Parameters:
91723bcd5ebSCédric Le Goater  * Input:
91823bcd5ebSCédric Le Goater  * - R4: "flags"
91923bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
92023bcd5ebSCédric Le Goater  *         Bit 63: Unconditional Notify (n) per the XIVE spec
92123bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
92223bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
92323bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
92423bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
92523bcd5ebSCédric Le Goater  * - R7: "eventQueue": The logical real address of the start of the EQ
92623bcd5ebSCédric Le Goater  * - R8: "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
92723bcd5ebSCédric Le Goater  *
92823bcd5ebSCédric Le Goater  * Output:
92923bcd5ebSCédric Le Goater  * - None
93023bcd5ebSCédric Le Goater  */
93123bcd5ebSCédric Le Goater 
93223bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_ALWAYS_NOTIFY PPC_BIT(63)
93323bcd5ebSCédric Le Goater 
93423bcd5ebSCédric Le Goater static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
93523bcd5ebSCédric Le Goater                                            sPAPRMachineState *spapr,
93623bcd5ebSCédric Le Goater                                            target_ulong opcode,
93723bcd5ebSCédric Le Goater                                            target_ulong *args)
93823bcd5ebSCédric Le Goater {
93923bcd5ebSCédric Le Goater     sPAPRXive *xive = spapr->xive;
94023bcd5ebSCédric Le Goater     target_ulong flags = args[0];
94123bcd5ebSCédric Le Goater     target_ulong target = args[1];
94223bcd5ebSCédric Le Goater     target_ulong priority = args[2];
94323bcd5ebSCédric Le Goater     target_ulong qpage = args[3];
94423bcd5ebSCédric Le Goater     target_ulong qsize = args[4];
94523bcd5ebSCédric Le Goater     XiveEND end;
94623bcd5ebSCédric Le Goater     uint8_t end_blk, nvt_blk;
94723bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
94823bcd5ebSCédric Le Goater 
94923bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
95023bcd5ebSCédric Le Goater         return H_FUNCTION;
95123bcd5ebSCédric Le Goater     }
95223bcd5ebSCédric Le Goater 
95323bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_ALWAYS_NOTIFY) {
95423bcd5ebSCédric Le Goater         return H_PARAMETER;
95523bcd5ebSCédric Le Goater     }
95623bcd5ebSCédric Le Goater 
95723bcd5ebSCédric Le Goater     /*
95823bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
95923bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
96023bcd5ebSCédric Le Goater      */
96123bcd5ebSCédric Le Goater 
96223bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
96323bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
96423bcd5ebSCédric Le Goater                       " is reserved\n", priority);
96523bcd5ebSCédric Le Goater         return H_P3;
96623bcd5ebSCédric Le Goater     }
96723bcd5ebSCédric Le Goater 
96823bcd5ebSCédric Le Goater     /*
96923bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
97023bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
97123bcd5ebSCédric Le Goater      * target.
97223bcd5ebSCédric Le Goater      */
97323bcd5ebSCédric Le Goater 
97423bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
97523bcd5ebSCédric Le Goater         return H_P2;
97623bcd5ebSCédric Le Goater     }
97723bcd5ebSCédric Le Goater 
97823bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
97923bcd5ebSCédric Le Goater     memcpy(&end, &xive->endt[end_idx], sizeof(XiveEND));
98023bcd5ebSCédric Le Goater 
98123bcd5ebSCédric Le Goater     switch (qsize) {
98223bcd5ebSCédric Le Goater     case 12:
98323bcd5ebSCédric Le Goater     case 16:
98423bcd5ebSCédric Le Goater     case 21:
98523bcd5ebSCédric Le Goater     case 24:
98623bcd5ebSCédric Le Goater         end.w2 = cpu_to_be32((qpage >> 32) & 0x0fffffff);
98723bcd5ebSCédric Le Goater         end.w3 = cpu_to_be32(qpage & 0xffffffff);
98823bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_ENQUEUE);
98923bcd5ebSCédric Le Goater         end.w0 = xive_set_field32(END_W0_QSIZE, end.w0, qsize - 12);
99023bcd5ebSCédric Le Goater         break;
99123bcd5ebSCédric Le Goater     case 0:
99223bcd5ebSCédric Le Goater         /* reset queue and disable queueing */
99323bcd5ebSCédric Le Goater         spapr_xive_end_reset(&end);
99423bcd5ebSCédric Le Goater         goto out;
99523bcd5ebSCédric Le Goater 
99623bcd5ebSCédric Le Goater     default:
99723bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid EQ size %"PRIx64"\n",
99823bcd5ebSCédric Le Goater                       qsize);
99923bcd5ebSCédric Le Goater         return H_P5;
100023bcd5ebSCédric Le Goater     }
100123bcd5ebSCédric Le Goater 
100223bcd5ebSCédric Le Goater     if (qsize) {
100323bcd5ebSCédric Le Goater         hwaddr plen = 1 << qsize;
100423bcd5ebSCédric Le Goater         void *eq;
100523bcd5ebSCédric Le Goater 
100623bcd5ebSCédric Le Goater         /*
100723bcd5ebSCédric Le Goater          * Validate the guest EQ. We should also check that the queue
100823bcd5ebSCédric Le Goater          * has been zeroed by the OS.
100923bcd5ebSCédric Le Goater          */
101023bcd5ebSCédric Le Goater         eq = address_space_map(CPU(cpu)->as, qpage, &plen, true,
101123bcd5ebSCédric Le Goater                                MEMTXATTRS_UNSPECIFIED);
101223bcd5ebSCédric Le Goater         if (plen != 1 << qsize) {
101323bcd5ebSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to map EQ @0x%"
101423bcd5ebSCédric Le Goater                           HWADDR_PRIx "\n", qpage);
101523bcd5ebSCédric Le Goater             return H_P4;
101623bcd5ebSCédric Le Goater         }
101723bcd5ebSCédric Le Goater         address_space_unmap(CPU(cpu)->as, eq, plen, true, plen);
101823bcd5ebSCédric Le Goater     }
101923bcd5ebSCédric Le Goater 
102023bcd5ebSCédric Le Goater     /* "target" should have been validated above */
102123bcd5ebSCédric Le Goater     if (spapr_xive_target_to_nvt(target, &nvt_blk, &nvt_idx)) {
102223bcd5ebSCédric Le Goater         g_assert_not_reached();
102323bcd5ebSCédric Le Goater     }
102423bcd5ebSCédric Le Goater 
102523bcd5ebSCédric Le Goater     /*
102623bcd5ebSCédric Le Goater      * Ensure the priority and target are correctly set (they will not
102723bcd5ebSCédric Le Goater      * be right after allocation)
102823bcd5ebSCédric Le Goater      */
102923bcd5ebSCédric Le Goater     end.w6 = xive_set_field32(END_W6_NVT_BLOCK, 0ul, nvt_blk) |
103023bcd5ebSCédric Le Goater         xive_set_field32(END_W6_NVT_INDEX, 0ul, nvt_idx);
103123bcd5ebSCédric Le Goater     end.w7 = xive_set_field32(END_W7_F0_PRIORITY, 0ul, priority);
103223bcd5ebSCédric Le Goater 
103323bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_ALWAYS_NOTIFY) {
103423bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_UCOND_NOTIFY);
103523bcd5ebSCédric Le Goater     } else {
103623bcd5ebSCédric Le Goater         end.w0 &= cpu_to_be32((uint32_t)~END_W0_UCOND_NOTIFY);
103723bcd5ebSCédric Le Goater     }
103823bcd5ebSCédric Le Goater 
103923bcd5ebSCédric Le Goater     /*
104023bcd5ebSCédric Le Goater      * The generation bit for the END starts at 1 and The END page
104123bcd5ebSCédric Le Goater      * offset counter starts at 0.
104223bcd5ebSCédric Le Goater      */
104323bcd5ebSCédric Le Goater     end.w1 = cpu_to_be32(END_W1_GENERATION) |
104423bcd5ebSCédric Le Goater         xive_set_field32(END_W1_PAGE_OFF, 0ul, 0ul);
104523bcd5ebSCédric Le Goater     end.w0 |= cpu_to_be32(END_W0_VALID);
104623bcd5ebSCédric Le Goater 
104723bcd5ebSCédric Le Goater     /*
104823bcd5ebSCédric Le Goater      * TODO: issue syncs required to ensure all in-flight interrupts
104923bcd5ebSCédric Le Goater      * are complete on the old END
105023bcd5ebSCédric Le Goater      */
105123bcd5ebSCédric Le Goater 
105223bcd5ebSCédric Le Goater out:
105323bcd5ebSCédric Le Goater     /* Update END */
105423bcd5ebSCédric Le Goater     memcpy(&xive->endt[end_idx], &end, sizeof(XiveEND));
105523bcd5ebSCédric Le Goater     return H_SUCCESS;
105623bcd5ebSCédric Le Goater }
105723bcd5ebSCédric Le Goater 
105823bcd5ebSCédric Le Goater /*
105923bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
106023bcd5ebSCédric Le Goater  * target and priority.
106123bcd5ebSCédric Le Goater  *
106223bcd5ebSCédric Le Goater  * Parameters:
106323bcd5ebSCédric Le Goater  * Input:
106423bcd5ebSCédric Le Goater  * - R4: "flags"
106523bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
106623bcd5ebSCédric Le Goater  *         Bit 63: Debug: Return debug data
106723bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
106823bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
106923bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
107023bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
107123bcd5ebSCédric Le Goater  *
107223bcd5ebSCédric Le Goater  * Output:
107323bcd5ebSCédric Le Goater  * - R4: "flags":
107423bcd5ebSCédric Le Goater  *       Bits 0-61: Reserved
107523bcd5ebSCédric Le Goater  *       Bit 62: The value of Event Queue Generation Number (g) per
107623bcd5ebSCédric Le Goater  *              the XIVE spec if "Debug" = 1
107723bcd5ebSCédric Le Goater  *       Bit 63: The value of Unconditional Notify (n) per the XIVE spec
107823bcd5ebSCédric Le Goater  * - R5: The logical real address of the start of the EQ
107923bcd5ebSCédric Le Goater  * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
108023bcd5ebSCédric Le Goater  * - R7: The value of Event Queue Offset Counter per XIVE spec
108123bcd5ebSCédric Le Goater  *       if "Debug" = 1, else 0
108223bcd5ebSCédric Le Goater  *
108323bcd5ebSCédric Le Goater  */
108423bcd5ebSCédric Le Goater 
108523bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_DEBUG     PPC_BIT(63)
108623bcd5ebSCédric Le Goater 
108723bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
108823bcd5ebSCédric Le Goater                                            sPAPRMachineState *spapr,
108923bcd5ebSCédric Le Goater                                            target_ulong opcode,
109023bcd5ebSCédric Le Goater                                            target_ulong *args)
109123bcd5ebSCédric Le Goater {
109223bcd5ebSCédric Le Goater     sPAPRXive *xive = spapr->xive;
109323bcd5ebSCédric Le Goater     target_ulong flags = args[0];
109423bcd5ebSCédric Le Goater     target_ulong target = args[1];
109523bcd5ebSCédric Le Goater     target_ulong priority = args[2];
109623bcd5ebSCédric Le Goater     XiveEND *end;
109723bcd5ebSCédric Le Goater     uint8_t end_blk;
109823bcd5ebSCédric Le Goater     uint32_t end_idx;
109923bcd5ebSCédric Le Goater 
110023bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
110123bcd5ebSCédric Le Goater         return H_FUNCTION;
110223bcd5ebSCédric Le Goater     }
110323bcd5ebSCédric Le Goater 
110423bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_DEBUG) {
110523bcd5ebSCédric Le Goater         return H_PARAMETER;
110623bcd5ebSCédric Le Goater     }
110723bcd5ebSCédric Le Goater 
110823bcd5ebSCédric Le Goater     /*
110923bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
111023bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
111123bcd5ebSCédric Le Goater      */
111223bcd5ebSCédric Le Goater 
111323bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
111423bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
111523bcd5ebSCédric Le Goater                       " is reserved\n", priority);
111623bcd5ebSCédric Le Goater         return H_P3;
111723bcd5ebSCédric Le Goater     }
111823bcd5ebSCédric Le Goater 
111923bcd5ebSCédric Le Goater     /*
112023bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
112123bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
112223bcd5ebSCédric Le Goater      * target.
112323bcd5ebSCédric Le Goater      */
112423bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
112523bcd5ebSCédric Le Goater         return H_P2;
112623bcd5ebSCédric Le Goater     }
112723bcd5ebSCédric Le Goater 
112823bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
112923bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
113023bcd5ebSCédric Le Goater 
113123bcd5ebSCédric Le Goater     args[0] = 0;
113223bcd5ebSCédric Le Goater     if (xive_end_is_notify(end)) {
113323bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_END_ALWAYS_NOTIFY;
113423bcd5ebSCédric Le Goater     }
113523bcd5ebSCédric Le Goater 
113623bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
113723bcd5ebSCédric Le Goater         args[1] = (uint64_t) be32_to_cpu(end->w2 & 0x0fffffff) << 32
113823bcd5ebSCédric Le Goater             | be32_to_cpu(end->w3);
113923bcd5ebSCédric Le Goater         args[2] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
114023bcd5ebSCédric Le Goater     } else {
114123bcd5ebSCédric Le Goater         args[1] = 0;
114223bcd5ebSCédric Le Goater         args[2] = 0;
114323bcd5ebSCédric Le Goater     }
114423bcd5ebSCédric Le Goater 
114523bcd5ebSCédric Le Goater     /* TODO: do we need any locking on the END ? */
114623bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_DEBUG) {
114723bcd5ebSCédric Le Goater         /* Load the event queue generation number into the return flags */
114823bcd5ebSCédric Le Goater         args[0] |= (uint64_t)xive_get_field32(END_W1_GENERATION, end->w1) << 62;
114923bcd5ebSCédric Le Goater 
115023bcd5ebSCédric Le Goater         /* Load R7 with the event queue offset counter */
115123bcd5ebSCédric Le Goater         args[3] = xive_get_field32(END_W1_PAGE_OFF, end->w1);
115223bcd5ebSCédric Le Goater     } else {
115323bcd5ebSCédric Le Goater         args[3] = 0;
115423bcd5ebSCédric Le Goater     }
115523bcd5ebSCédric Le Goater 
115623bcd5ebSCédric Le Goater     return H_SUCCESS;
115723bcd5ebSCédric Le Goater }
115823bcd5ebSCédric Le Goater 
115923bcd5ebSCédric Le Goater /*
116023bcd5ebSCédric Le Goater  * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
116123bcd5ebSCédric Le Goater  * reporting cache line pair for the calling thread.  The reporting
116223bcd5ebSCédric Le Goater  * cache lines will contain the OS interrupt context when the OS
116323bcd5ebSCédric Le Goater  * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
116423bcd5ebSCédric Le Goater  * interrupt. The reporting cache lines can be reset by inputting -1
116523bcd5ebSCédric Le Goater  * in "reportingLine".  Issuing the CI store byte without reporting
116623bcd5ebSCédric Le Goater  * cache lines registered will result in the data not being accessible
116723bcd5ebSCédric Le Goater  * to the OS.
116823bcd5ebSCédric Le Goater  *
116923bcd5ebSCédric Le Goater  * Parameters:
117023bcd5ebSCédric Le Goater  * Input:
117123bcd5ebSCédric Le Goater  * - R4: "flags"
117223bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
117323bcd5ebSCédric Le Goater  * - R5: "reportingLine": The logical real address of the reporting cache
117423bcd5ebSCédric Le Goater  *       line pair
117523bcd5ebSCédric Le Goater  *
117623bcd5ebSCédric Le Goater  * Output:
117723bcd5ebSCédric Le Goater  * - None
117823bcd5ebSCédric Le Goater  */
117923bcd5ebSCédric Le Goater static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
118023bcd5ebSCédric Le Goater                                                 sPAPRMachineState *spapr,
118123bcd5ebSCédric Le Goater                                                 target_ulong opcode,
118223bcd5ebSCédric Le Goater                                                 target_ulong *args)
118323bcd5ebSCédric Le Goater {
118423bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
118523bcd5ebSCédric Le Goater         return H_FUNCTION;
118623bcd5ebSCédric Le Goater     }
118723bcd5ebSCédric Le Goater 
118823bcd5ebSCédric Le Goater     /*
118923bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
119023bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
119123bcd5ebSCédric Le Goater      */
119223bcd5ebSCédric Le Goater 
119323bcd5ebSCédric Le Goater     /* TODO: H_INT_SET_OS_REPORTING_LINE */
119423bcd5ebSCédric Le Goater     return H_FUNCTION;
119523bcd5ebSCédric Le Goater }
119623bcd5ebSCédric Le Goater 
119723bcd5ebSCédric Le Goater /*
119823bcd5ebSCédric Le Goater  * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
119923bcd5ebSCédric Le Goater  * real address of the reporting cache line pair set for the input
120023bcd5ebSCédric Le Goater  * "target".  If no reporting cache line pair has been set, -1 is
120123bcd5ebSCédric Le Goater  * returned.
120223bcd5ebSCédric Le Goater  *
120323bcd5ebSCédric Le Goater  * Parameters:
120423bcd5ebSCédric Le Goater  * Input:
120523bcd5ebSCédric Le Goater  * - R4: "flags"
120623bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
120723bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
120823bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
120923bcd5ebSCédric Le Goater  * - R6: "reportingLine": The logical real address of the reporting
121023bcd5ebSCédric Le Goater  *        cache line pair
121123bcd5ebSCédric Le Goater  *
121223bcd5ebSCédric Le Goater  * Output:
121323bcd5ebSCédric Le Goater  * - R4: The logical real address of the reporting line if set, else -1
121423bcd5ebSCédric Le Goater  */
121523bcd5ebSCédric Le Goater static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
121623bcd5ebSCédric Le Goater                                                 sPAPRMachineState *spapr,
121723bcd5ebSCédric Le Goater                                                 target_ulong opcode,
121823bcd5ebSCédric Le Goater                                                 target_ulong *args)
121923bcd5ebSCédric Le Goater {
122023bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
122123bcd5ebSCédric Le Goater         return H_FUNCTION;
122223bcd5ebSCédric Le Goater     }
122323bcd5ebSCédric Le Goater 
122423bcd5ebSCédric Le Goater     /*
122523bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
122623bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
122723bcd5ebSCédric Le Goater      */
122823bcd5ebSCédric Le Goater 
122923bcd5ebSCédric Le Goater     /* TODO: H_INT_GET_OS_REPORTING_LINE */
123023bcd5ebSCédric Le Goater     return H_FUNCTION;
123123bcd5ebSCédric Le Goater }
123223bcd5ebSCédric Le Goater 
123323bcd5ebSCédric Le Goater /*
123423bcd5ebSCédric Le Goater  * The H_INT_ESB hcall() is used to issue a load or store to the ESB
123523bcd5ebSCédric Le Goater  * page for the input "lisn".  This hcall is only supported for LISNs
123623bcd5ebSCédric Le Goater  * that have the ESB hcall flag set to 1 when returned from hcall()
123723bcd5ebSCédric Le Goater  * H_INT_GET_SOURCE_INFO.
123823bcd5ebSCédric Le Goater  *
123923bcd5ebSCédric Le Goater  * Parameters:
124023bcd5ebSCédric Le Goater  * Input:
124123bcd5ebSCédric Le Goater  * - R4: "flags"
124223bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
124323bcd5ebSCédric Le Goater  *         bit 63: Store: Store=1, store operation, else load operation
124423bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
124523bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
124623bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
124723bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
124823bcd5ebSCédric Le Goater  * - R6: "esbOffset" is the offset into the ESB page for the load or
124923bcd5ebSCédric Le Goater  *       store operation
125023bcd5ebSCédric Le Goater  * - R7: "storeData" is the data to write for a store operation
125123bcd5ebSCédric Le Goater  *
125223bcd5ebSCédric Le Goater  * Output:
125323bcd5ebSCédric Le Goater  * - R4: The value of the load if load operation, else -1
125423bcd5ebSCédric Le Goater  */
125523bcd5ebSCédric Le Goater 
125623bcd5ebSCédric Le Goater #define SPAPR_XIVE_ESB_STORE PPC_BIT(63)
125723bcd5ebSCédric Le Goater 
125823bcd5ebSCédric Le Goater static target_ulong h_int_esb(PowerPCCPU *cpu,
125923bcd5ebSCédric Le Goater                               sPAPRMachineState *spapr,
126023bcd5ebSCédric Le Goater                               target_ulong opcode,
126123bcd5ebSCédric Le Goater                               target_ulong *args)
126223bcd5ebSCédric Le Goater {
126323bcd5ebSCédric Le Goater     sPAPRXive *xive = spapr->xive;
126423bcd5ebSCédric Le Goater     XiveEAS eas;
126523bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
126623bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
126723bcd5ebSCédric Le Goater     target_ulong offset = args[2];
126823bcd5ebSCédric Le Goater     target_ulong data   = args[3];
126923bcd5ebSCédric Le Goater     hwaddr mmio_addr;
127023bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
127123bcd5ebSCédric Le Goater 
127223bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
127323bcd5ebSCédric Le Goater         return H_FUNCTION;
127423bcd5ebSCédric Le Goater     }
127523bcd5ebSCédric Le Goater 
127623bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_ESB_STORE) {
127723bcd5ebSCédric Le Goater         return H_PARAMETER;
127823bcd5ebSCédric Le Goater     }
127923bcd5ebSCédric Le Goater 
128023bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
128123bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
128223bcd5ebSCédric Le Goater                       lisn);
128323bcd5ebSCédric Le Goater         return H_P2;
128423bcd5ebSCédric Le Goater     }
128523bcd5ebSCédric Le Goater 
128623bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
128723bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
128823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
128923bcd5ebSCédric Le Goater                       lisn);
129023bcd5ebSCédric Le Goater         return H_P2;
129123bcd5ebSCédric Le Goater     }
129223bcd5ebSCédric Le Goater 
129323bcd5ebSCédric Le Goater     if (offset > (1ull << xsrc->esb_shift)) {
129423bcd5ebSCédric Le Goater         return H_P3;
129523bcd5ebSCédric Le Goater     }
129623bcd5ebSCédric Le Goater 
129723bcd5ebSCédric Le Goater     mmio_addr = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn) + offset;
129823bcd5ebSCédric Le Goater 
129923bcd5ebSCédric Le Goater     if (dma_memory_rw(&address_space_memory, mmio_addr, &data, 8,
130023bcd5ebSCédric Le Goater                       (flags & SPAPR_XIVE_ESB_STORE))) {
130123bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to access ESB @0x%"
130223bcd5ebSCédric Le Goater                       HWADDR_PRIx "\n", mmio_addr);
130323bcd5ebSCédric Le Goater         return H_HARDWARE;
130423bcd5ebSCédric Le Goater     }
130523bcd5ebSCédric Le Goater     args[0] = (flags & SPAPR_XIVE_ESB_STORE) ? -1 : data;
130623bcd5ebSCédric Le Goater     return H_SUCCESS;
130723bcd5ebSCédric Le Goater }
130823bcd5ebSCédric Le Goater 
130923bcd5ebSCédric Le Goater /*
131023bcd5ebSCédric Le Goater  * The H_INT_SYNC hcall() is used to issue hardware syncs that will
131123bcd5ebSCédric Le Goater  * ensure any in flight events for the input lisn are in the event
131223bcd5ebSCédric Le Goater  * queue.
131323bcd5ebSCédric Le Goater  *
131423bcd5ebSCédric Le Goater  * Parameters:
131523bcd5ebSCédric Le Goater  * Input:
131623bcd5ebSCédric Le Goater  * - R4: "flags"
131723bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
131823bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
131923bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
132023bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
132123bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
132223bcd5ebSCédric Le Goater  *
132323bcd5ebSCédric Le Goater  * Output:
132423bcd5ebSCédric Le Goater  * - None
132523bcd5ebSCédric Le Goater  */
132623bcd5ebSCédric Le Goater static target_ulong h_int_sync(PowerPCCPU *cpu,
132723bcd5ebSCédric Le Goater                                sPAPRMachineState *spapr,
132823bcd5ebSCédric Le Goater                                target_ulong opcode,
132923bcd5ebSCédric Le Goater                                target_ulong *args)
133023bcd5ebSCédric Le Goater {
133123bcd5ebSCédric Le Goater     sPAPRXive *xive = spapr->xive;
133223bcd5ebSCédric Le Goater     XiveEAS eas;
133323bcd5ebSCédric Le Goater     target_ulong flags = args[0];
133423bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
133523bcd5ebSCédric Le Goater 
133623bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
133723bcd5ebSCédric Le Goater         return H_FUNCTION;
133823bcd5ebSCédric Le Goater     }
133923bcd5ebSCédric Le Goater 
134023bcd5ebSCédric Le Goater     if (flags) {
134123bcd5ebSCédric Le Goater         return H_PARAMETER;
134223bcd5ebSCédric Le Goater     }
134323bcd5ebSCédric Le Goater 
134423bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
134523bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
134623bcd5ebSCédric Le Goater                       lisn);
134723bcd5ebSCédric Le Goater         return H_P2;
134823bcd5ebSCédric Le Goater     }
134923bcd5ebSCédric Le Goater 
135023bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
135123bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
135223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
135323bcd5ebSCédric Le Goater                       lisn);
135423bcd5ebSCédric Le Goater         return H_P2;
135523bcd5ebSCédric Le Goater     }
135623bcd5ebSCédric Le Goater 
135723bcd5ebSCédric Le Goater     /*
135823bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
135923bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
136023bcd5ebSCédric Le Goater      */
136123bcd5ebSCédric Le Goater 
136223bcd5ebSCédric Le Goater     /* This is not real hardware. Nothing to be done */
136323bcd5ebSCédric Le Goater     return H_SUCCESS;
136423bcd5ebSCédric Le Goater }
136523bcd5ebSCédric Le Goater 
136623bcd5ebSCédric Le Goater /*
136723bcd5ebSCédric Le Goater  * The H_INT_RESET hcall() is used to reset all of the partition's
136823bcd5ebSCédric Le Goater  * interrupt exploitation structures to their initial state.  This
136923bcd5ebSCédric Le Goater  * means losing all previously set interrupt state set via
137023bcd5ebSCédric Le Goater  * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
137123bcd5ebSCédric Le Goater  *
137223bcd5ebSCédric Le Goater  * Parameters:
137323bcd5ebSCédric Le Goater  * Input:
137423bcd5ebSCédric Le Goater  * - R4: "flags"
137523bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
137623bcd5ebSCédric Le Goater  *
137723bcd5ebSCédric Le Goater  * Output:
137823bcd5ebSCédric Le Goater  * - None
137923bcd5ebSCédric Le Goater  */
138023bcd5ebSCédric Le Goater static target_ulong h_int_reset(PowerPCCPU *cpu,
138123bcd5ebSCédric Le Goater                                 sPAPRMachineState *spapr,
138223bcd5ebSCédric Le Goater                                 target_ulong opcode,
138323bcd5ebSCédric Le Goater                                 target_ulong *args)
138423bcd5ebSCédric Le Goater {
138523bcd5ebSCédric Le Goater     sPAPRXive *xive = spapr->xive;
138623bcd5ebSCédric Le Goater     target_ulong flags   = args[0];
138723bcd5ebSCédric Le Goater 
138823bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
138923bcd5ebSCédric Le Goater         return H_FUNCTION;
139023bcd5ebSCédric Le Goater     }
139123bcd5ebSCédric Le Goater 
139223bcd5ebSCédric Le Goater     if (flags) {
139323bcd5ebSCédric Le Goater         return H_PARAMETER;
139423bcd5ebSCédric Le Goater     }
139523bcd5ebSCédric Le Goater 
139623bcd5ebSCédric Le Goater     device_reset(DEVICE(xive));
139723bcd5ebSCédric Le Goater     return H_SUCCESS;
139823bcd5ebSCédric Le Goater }
139923bcd5ebSCédric Le Goater 
140023bcd5ebSCédric Le Goater void spapr_xive_hcall_init(sPAPRMachineState *spapr)
140123bcd5ebSCédric Le Goater {
140223bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
140323bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
140423bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
140523bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
140623bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
140723bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
140823bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
140923bcd5ebSCédric Le Goater                              h_int_set_os_reporting_line);
141023bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
141123bcd5ebSCédric Le Goater                              h_int_get_os_reporting_line);
141223bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_ESB, h_int_esb);
141323bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SYNC, h_int_sync);
141423bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_RESET, h_int_reset);
141523bcd5ebSCédric Le Goater }
14166e21de4aSCédric Le Goater 
14176e21de4aSCédric Le Goater void spapr_dt_xive(sPAPRMachineState *spapr, uint32_t nr_servers, void *fdt,
14186e21de4aSCédric Le Goater                    uint32_t phandle)
14196e21de4aSCédric Le Goater {
14206e21de4aSCédric Le Goater     sPAPRXive *xive = spapr->xive;
14216e21de4aSCédric Le Goater     int node;
14226e21de4aSCédric Le Goater     uint64_t timas[2 * 2];
14236e21de4aSCédric Le Goater     /* Interrupt number ranges for the IPIs */
14246e21de4aSCédric Le Goater     uint32_t lisn_ranges[] = {
14256e21de4aSCédric Le Goater         cpu_to_be32(0),
14266e21de4aSCédric Le Goater         cpu_to_be32(nr_servers),
14276e21de4aSCédric Le Goater     };
14286e21de4aSCédric Le Goater     /*
14296e21de4aSCédric Le Goater      * EQ size - the sizes of pages supported by the system 4K, 64K,
14306e21de4aSCédric Le Goater      * 2M, 16M. We only advertise 64K for the moment.
14316e21de4aSCédric Le Goater      */
14326e21de4aSCédric Le Goater     uint32_t eq_sizes[] = {
14336e21de4aSCédric Le Goater         cpu_to_be32(16), /* 64K */
14346e21de4aSCédric Le Goater     };
14356e21de4aSCédric Le Goater     /*
14366e21de4aSCédric Le Goater      * The following array is in sync with the reserved priorities
14376e21de4aSCédric Le Goater      * defined by the 'spapr_xive_priority_is_reserved' routine.
14386e21de4aSCédric Le Goater      */
14396e21de4aSCédric Le Goater     uint32_t plat_res_int_priorities[] = {
14406e21de4aSCédric Le Goater         cpu_to_be32(7),    /* start */
14416e21de4aSCédric Le Goater         cpu_to_be32(0xf8), /* count */
14426e21de4aSCédric Le Goater     };
14436e21de4aSCédric Le Goater     gchar *nodename;
14446e21de4aSCédric Le Goater 
14456e21de4aSCédric Le Goater     /* Thread Interrupt Management Area : User (ring 3) and OS (ring 2) */
14466e21de4aSCédric Le Goater     timas[0] = cpu_to_be64(xive->tm_base +
14476e21de4aSCédric Le Goater                            XIVE_TM_USER_PAGE * (1ull << TM_SHIFT));
14486e21de4aSCédric Le Goater     timas[1] = cpu_to_be64(1ull << TM_SHIFT);
14496e21de4aSCédric Le Goater     timas[2] = cpu_to_be64(xive->tm_base +
14506e21de4aSCédric Le Goater                            XIVE_TM_OS_PAGE * (1ull << TM_SHIFT));
14516e21de4aSCédric Le Goater     timas[3] = cpu_to_be64(1ull << TM_SHIFT);
14526e21de4aSCédric Le Goater 
14536e21de4aSCédric Le Goater     nodename = g_strdup_printf("interrupt-controller@%" PRIx64,
14546e21de4aSCédric Le Goater                            xive->tm_base + XIVE_TM_USER_PAGE * (1 << TM_SHIFT));
14556e21de4aSCédric Le Goater     _FDT(node = fdt_add_subnode(fdt, 0, nodename));
14566e21de4aSCédric Le Goater     g_free(nodename);
14576e21de4aSCédric Le Goater 
14586e21de4aSCédric Le Goater     _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
14596e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
14606e21de4aSCédric Le Goater 
14616e21de4aSCédric Le Goater     _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
14626e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
14636e21de4aSCédric Le Goater                      sizeof(eq_sizes)));
14646e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
14656e21de4aSCédric Le Goater                      sizeof(lisn_ranges)));
14666e21de4aSCédric Le Goater 
14676e21de4aSCédric Le Goater     /* For Linux to link the LSIs to the interrupt controller. */
14686e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, node, "interrupt-controller", NULL, 0));
14696e21de4aSCédric Le Goater     _FDT(fdt_setprop_cell(fdt, node, "#interrupt-cells", 2));
14706e21de4aSCédric Le Goater 
14716e21de4aSCédric Le Goater     /* For SLOF */
14726e21de4aSCédric Le Goater     _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
14736e21de4aSCédric Le Goater     _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
14746e21de4aSCédric Le Goater 
14756e21de4aSCédric Le Goater     /*
14766e21de4aSCédric Le Goater      * The "ibm,plat-res-int-priorities" property defines the priority
14776e21de4aSCédric Le Goater      * ranges reserved by the hypervisor
14786e21de4aSCédric Le Goater      */
14796e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
14806e21de4aSCédric Le Goater                      plat_res_int_priorities, sizeof(plat_res_int_priorities)));
14816e21de4aSCédric Le Goater }
1482