xref: /qemu/hw/intc/spapr_xive.c (revision 7bfc759c02b8d19fd76d70d138deea0025f6f461)
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"
19a28b9a5aSCédric Le Goater #include "hw/ppc/spapr_cpu_core.h"
203aa597f6SCédric Le Goater #include "hw/ppc/spapr_xive.h"
213aa597f6SCédric Le Goater #include "hw/ppc/xive.h"
223aa597f6SCédric Le Goater #include "hw/ppc/xive_regs.h"
233aa597f6SCédric Le Goater 
243aa597f6SCédric Le Goater /*
253aa597f6SCédric Le Goater  * XIVE Virtualization Controller BAR and Thread Managment BAR that we
263aa597f6SCédric Le Goater  * use for the ESB pages and the TIMA pages
273aa597f6SCédric Le Goater  */
283aa597f6SCédric Le Goater #define SPAPR_XIVE_VC_BASE   0x0006010000000000ull
293aa597f6SCédric Le Goater #define SPAPR_XIVE_TM_BASE   0x0006030203180000ull
303aa597f6SCédric Le Goater 
313aa597f6SCédric Le Goater /*
320cddee8dSCédric Le Goater  * The allocation of VP blocks is a complex operation in OPAL and the
330cddee8dSCédric Le Goater  * VP identifiers have a relation with the number of HW chips, the
340cddee8dSCédric Le Goater  * size of the VP blocks, VP grouping, etc. The QEMU sPAPR XIVE
350cddee8dSCédric Le Goater  * controller model does not have the same constraints and can use a
360cddee8dSCédric Le Goater  * simple mapping scheme of the CPU vcpu_id
370cddee8dSCédric Le Goater  *
380cddee8dSCédric Le Goater  * These identifiers are never returned to the OS.
390cddee8dSCédric Le Goater  */
400cddee8dSCédric Le Goater 
410cddee8dSCédric Le Goater #define SPAPR_XIVE_NVT_BASE 0x400
420cddee8dSCédric Le Goater 
430cddee8dSCédric Le Goater /*
440cddee8dSCédric Le Goater  * sPAPR NVT and END indexing helpers
450cddee8dSCédric Le Goater  */
460cddee8dSCédric Le Goater static uint32_t spapr_xive_nvt_to_target(uint8_t nvt_blk, uint32_t nvt_idx)
470cddee8dSCédric Le Goater {
480cddee8dSCédric Le Goater     return nvt_idx - SPAPR_XIVE_NVT_BASE;
490cddee8dSCédric Le Goater }
500cddee8dSCédric Le Goater 
5123bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_nvt(PowerPCCPU *cpu,
5223bcd5ebSCédric Le Goater                                   uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
5323bcd5ebSCédric Le Goater {
5423bcd5ebSCédric Le Goater     assert(cpu);
5523bcd5ebSCédric Le Goater 
5623bcd5ebSCédric Le Goater     if (out_nvt_blk) {
5723bcd5ebSCédric Le Goater         *out_nvt_blk = SPAPR_XIVE_BLOCK_ID;
5823bcd5ebSCédric Le Goater     }
5923bcd5ebSCédric Le Goater 
6023bcd5ebSCédric Le Goater     if (out_nvt_blk) {
6123bcd5ebSCédric Le Goater         *out_nvt_idx = SPAPR_XIVE_NVT_BASE + cpu->vcpu_id;
6223bcd5ebSCédric Le Goater     }
6323bcd5ebSCédric Le Goater }
6423bcd5ebSCédric Le Goater 
6523bcd5ebSCédric Le Goater static int spapr_xive_target_to_nvt(uint32_t target,
6623bcd5ebSCédric Le Goater                                     uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
6723bcd5ebSCédric Le Goater {
6823bcd5ebSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(target);
6923bcd5ebSCédric Le Goater 
7023bcd5ebSCédric Le Goater     if (!cpu) {
7123bcd5ebSCédric Le Goater         return -1;
7223bcd5ebSCédric Le Goater     }
7323bcd5ebSCédric Le Goater 
7423bcd5ebSCédric Le Goater     spapr_xive_cpu_to_nvt(cpu, out_nvt_blk, out_nvt_idx);
7523bcd5ebSCédric Le Goater     return 0;
7623bcd5ebSCédric Le Goater }
7723bcd5ebSCédric Le Goater 
7823bcd5ebSCédric Le Goater /*
7923bcd5ebSCédric Le Goater  * sPAPR END indexing uses a simple mapping of the CPU vcpu_id, 8
8023bcd5ebSCédric Le Goater  * priorities per CPU
8123bcd5ebSCédric Le Goater  */
820c575703SCédric Le Goater int spapr_xive_end_to_target(uint8_t end_blk, uint32_t end_idx,
830c575703SCédric Le Goater                              uint32_t *out_server, uint8_t *out_prio)
840c575703SCédric Le Goater {
850c575703SCédric Le Goater 
860c575703SCédric Le Goater     assert(end_blk == SPAPR_XIVE_BLOCK_ID);
870c575703SCédric Le Goater 
880c575703SCédric Le Goater     if (out_server) {
890c575703SCédric Le Goater         *out_server = end_idx >> 3;
900c575703SCédric Le Goater     }
910c575703SCédric Le Goater 
920c575703SCédric Le Goater     if (out_prio) {
930c575703SCédric Le Goater         *out_prio = end_idx & 0x7;
940c575703SCédric Le Goater     }
950c575703SCédric Le Goater     return 0;
960c575703SCédric Le Goater }
970c575703SCédric Le Goater 
9823bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_end(PowerPCCPU *cpu, uint8_t prio,
9923bcd5ebSCédric Le Goater                                   uint8_t *out_end_blk, uint32_t *out_end_idx)
10023bcd5ebSCédric Le Goater {
10123bcd5ebSCédric Le Goater     assert(cpu);
10223bcd5ebSCédric Le Goater 
10323bcd5ebSCédric Le Goater     if (out_end_blk) {
10423bcd5ebSCédric Le Goater         *out_end_blk = SPAPR_XIVE_BLOCK_ID;
10523bcd5ebSCédric Le Goater     }
10623bcd5ebSCédric Le Goater 
10723bcd5ebSCédric Le Goater     if (out_end_idx) {
10823bcd5ebSCédric Le Goater         *out_end_idx = (cpu->vcpu_id << 3) + prio;
10923bcd5ebSCédric Le Goater     }
11023bcd5ebSCédric Le Goater }
11123bcd5ebSCédric Le Goater 
11223bcd5ebSCédric Le Goater static int spapr_xive_target_to_end(uint32_t target, uint8_t prio,
11323bcd5ebSCédric Le Goater                                     uint8_t *out_end_blk, uint32_t *out_end_idx)
11423bcd5ebSCédric Le Goater {
11523bcd5ebSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(target);
11623bcd5ebSCédric Le Goater 
11723bcd5ebSCédric Le Goater     if (!cpu) {
11823bcd5ebSCédric Le Goater         return -1;
11923bcd5ebSCédric Le Goater     }
12023bcd5ebSCédric Le Goater 
12123bcd5ebSCédric Le Goater     spapr_xive_cpu_to_end(cpu, prio, out_end_blk, out_end_idx);
12223bcd5ebSCédric Le Goater     return 0;
12323bcd5ebSCédric Le Goater }
12423bcd5ebSCédric Le Goater 
1250cddee8dSCédric Le Goater /*
1263aa597f6SCédric Le Goater  * On sPAPR machines, use a simplified output for the XIVE END
1273aa597f6SCédric Le Goater  * structure dumping only the information related to the OS EQ.
1283aa597f6SCédric Le Goater  */
129ce2918cbSDavid Gibson static void spapr_xive_end_pic_print_info(SpaprXive *xive, XiveEND *end,
1303aa597f6SCédric Le Goater                                           Monitor *mon)
1313aa597f6SCédric Le Goater {
132fb2e8b51SCédric Le Goater     uint64_t qaddr_base = xive_end_qaddr(end);
1333aa597f6SCédric Le Goater     uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
1343aa597f6SCédric Le Goater     uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
1353aa597f6SCédric Le Goater     uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
1363aa597f6SCédric Le Goater     uint32_t qentries = 1 << (qsize + 10);
1373aa597f6SCédric Le Goater     uint32_t nvt = xive_get_field32(END_W6_NVT_INDEX, end->w6);
1383aa597f6SCédric Le Goater     uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
1393aa597f6SCédric Le Goater 
140fb2e8b51SCédric Le Goater     monitor_printf(mon, "%3d/%d % 6d/%5d @%"PRIx64" ^%d",
1410cddee8dSCédric Le Goater                    spapr_xive_nvt_to_target(0, nvt),
142fb2e8b51SCédric Le Goater                    priority, qindex, qentries, qaddr_base, qgen);
1433aa597f6SCédric Le Goater 
1443aa597f6SCédric Le Goater     xive_end_queue_pic_print_info(end, 6, mon);
1453aa597f6SCédric Le Goater     monitor_printf(mon, "]");
1463aa597f6SCédric Le Goater }
1473aa597f6SCédric Le Goater 
148ce2918cbSDavid Gibson void spapr_xive_pic_print_info(SpaprXive *xive, Monitor *mon)
1493aa597f6SCédric Le Goater {
1503aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
1513aa597f6SCédric Le Goater     int i;
1523aa597f6SCédric Le Goater 
153*7bfc759cSCédric Le Goater     if (kvm_irqchip_in_kernel()) {
154*7bfc759cSCédric Le Goater         Error *local_err = NULL;
155*7bfc759cSCédric Le Goater 
156*7bfc759cSCédric Le Goater         kvmppc_xive_synchronize_state(xive, &local_err);
157*7bfc759cSCédric Le Goater         if (local_err) {
158*7bfc759cSCédric Le Goater             error_report_err(local_err);
159*7bfc759cSCédric Le Goater             return;
160*7bfc759cSCédric Le Goater         }
161*7bfc759cSCédric Le Goater     }
162*7bfc759cSCédric Le Goater 
163f81d69fcSSatheesh Rajendran     monitor_printf(mon, "  LISN         PQ    EISN     CPU/PRIO EQ\n");
1643aa597f6SCédric Le Goater 
1653aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
1663aa597f6SCédric Le Goater         uint8_t pq = xive_source_esb_get(xsrc, i);
1673aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
1683aa597f6SCédric Le Goater 
1693aa597f6SCédric Le Goater         if (!xive_eas_is_valid(eas)) {
1703aa597f6SCédric Le Goater             continue;
1713aa597f6SCédric Le Goater         }
1723aa597f6SCédric Le Goater 
1733aa597f6SCédric Le Goater         monitor_printf(mon, "  %08x %s %c%c%c %s %08x ", i,
1743aa597f6SCédric Le Goater                        xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
1753aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
1763aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
1773aa597f6SCédric Le Goater                        xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ',
1783aa597f6SCédric Le Goater                        xive_eas_is_masked(eas) ? "M" : " ",
1793aa597f6SCédric Le Goater                        (int) xive_get_field64(EAS_END_DATA, eas->w));
1803aa597f6SCédric Le Goater 
1813aa597f6SCédric Le Goater         if (!xive_eas_is_masked(eas)) {
1823aa597f6SCédric Le Goater             uint32_t end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
1833aa597f6SCédric Le Goater             XiveEND *end;
1843aa597f6SCédric Le Goater 
1853aa597f6SCédric Le Goater             assert(end_idx < xive->nr_ends);
1863aa597f6SCédric Le Goater             end = &xive->endt[end_idx];
1873aa597f6SCédric Le Goater 
1883aa597f6SCédric Le Goater             if (xive_end_is_valid(end)) {
1893aa597f6SCédric Le Goater                 spapr_xive_end_pic_print_info(xive, end, mon);
1903aa597f6SCédric Le Goater             }
1913aa597f6SCédric Le Goater         }
1923aa597f6SCédric Le Goater         monitor_printf(mon, "\n");
1933aa597f6SCédric Le Goater     }
1943aa597f6SCédric Le Goater }
1953aa597f6SCédric Le Goater 
19638afd772SCédric Le Goater void spapr_xive_map_mmio(SpaprXive *xive)
1973aa597f6SCédric Le Goater {
1983aa597f6SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->vc_base);
1993aa597f6SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1, xive->end_base);
2003aa597f6SCédric Le Goater     sysbus_mmio_map(SYS_BUS_DEVICE(xive), 2, xive->tm_base);
2013aa597f6SCédric Le Goater }
2023aa597f6SCédric Le Goater 
203ce2918cbSDavid Gibson void spapr_xive_mmio_set_enabled(SpaprXive *xive, bool enable)
2043a8eb78eSCédric Le Goater {
2053a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->source.esb_mmio, enable);
2063a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->tm_mmio, enable);
2073a8eb78eSCédric Le Goater 
2083a8eb78eSCédric Le Goater     /* Disable the END ESBs until a guest OS makes use of them */
2093a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->end_source.esb_mmio, false);
2103a8eb78eSCédric Le Goater }
2113a8eb78eSCédric Le Goater 
212b2e22477SCédric Le Goater /*
213b2e22477SCédric Le Goater  * When a Virtual Processor is scheduled to run on a HW thread, the
214b2e22477SCédric Le Goater  * hypervisor pushes its identifier in the OS CAM line. Emulate the
215b2e22477SCédric Le Goater  * same behavior under QEMU.
216b2e22477SCédric Le Goater  */
217b2e22477SCédric Le Goater void spapr_xive_set_tctx_os_cam(XiveTCTX *tctx)
218b2e22477SCédric Le Goater {
219b2e22477SCédric Le Goater     uint8_t  nvt_blk;
220b2e22477SCédric Le Goater     uint32_t nvt_idx;
221b2e22477SCédric Le Goater     uint32_t nvt_cam;
222b2e22477SCédric Le Goater 
223b2e22477SCédric Le Goater     spapr_xive_cpu_to_nvt(POWERPC_CPU(tctx->cs), &nvt_blk, &nvt_idx);
224b2e22477SCédric Le Goater 
225b2e22477SCédric Le Goater     nvt_cam = cpu_to_be32(TM_QW1W2_VO | xive_nvt_cam_line(nvt_blk, nvt_idx));
226b2e22477SCédric Le Goater     memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &nvt_cam, 4);
227b2e22477SCédric Le Goater }
228b2e22477SCédric Le Goater 
2293aa597f6SCédric Le Goater static void spapr_xive_end_reset(XiveEND *end)
2303aa597f6SCédric Le Goater {
2313aa597f6SCédric Le Goater     memset(end, 0, sizeof(*end));
2323aa597f6SCédric Le Goater 
2333aa597f6SCédric Le Goater     /* switch off the escalation and notification ESBs */
2343aa597f6SCédric Le Goater     end->w1 = cpu_to_be32(END_W1_ESe_Q | END_W1_ESn_Q);
2353aa597f6SCédric Le Goater }
2363aa597f6SCédric Le Goater 
2373aa597f6SCédric Le Goater static void spapr_xive_reset(void *dev)
2383aa597f6SCédric Le Goater {
239ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(dev);
2403aa597f6SCédric Le Goater     int i;
2413aa597f6SCédric Le Goater 
2423aa597f6SCédric Le Goater     /*
2433aa597f6SCédric Le Goater      * The XiveSource has its own reset handler, which mask off all
2443aa597f6SCédric Le Goater      * IRQs (!P|Q)
2453aa597f6SCédric Le Goater      */
2463aa597f6SCédric Le Goater 
2473aa597f6SCédric Le Goater     /* Mask all valid EASs in the IRQ number space. */
2483aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
2493aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
2503aa597f6SCédric Le Goater         if (xive_eas_is_valid(eas)) {
2513aa597f6SCédric Le Goater             eas->w = cpu_to_be64(EAS_VALID | EAS_MASKED);
2523aa597f6SCédric Le Goater         } else {
2533aa597f6SCédric Le Goater             eas->w = 0;
2543aa597f6SCédric Le Goater         }
2553aa597f6SCédric Le Goater     }
2563aa597f6SCédric Le Goater 
2573aa597f6SCédric Le Goater     /* Clear all ENDs */
2583aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_ends; i++) {
2593aa597f6SCédric Le Goater         spapr_xive_end_reset(&xive->endt[i]);
2603aa597f6SCédric Le Goater     }
2613aa597f6SCédric Le Goater }
2623aa597f6SCédric Le Goater 
2633aa597f6SCédric Le Goater static void spapr_xive_instance_init(Object *obj)
2643aa597f6SCédric Le Goater {
265ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(obj);
2663aa597f6SCédric Le Goater 
267f6d4dca8SThomas Huth     object_initialize_child(obj, "source", &xive->source, sizeof(xive->source),
268f6d4dca8SThomas Huth                             TYPE_XIVE_SOURCE, &error_abort, NULL);
2693aa597f6SCédric Le Goater 
270f6d4dca8SThomas Huth     object_initialize_child(obj, "end_source", &xive->end_source,
271f6d4dca8SThomas Huth                             sizeof(xive->end_source), TYPE_XIVE_END_SOURCE,
272f6d4dca8SThomas Huth                             &error_abort, NULL);
27338afd772SCédric Le Goater 
27438afd772SCédric Le Goater     /* Not connected to the KVM XIVE device */
27538afd772SCédric Le Goater     xive->fd = -1;
2763aa597f6SCédric Le Goater }
2773aa597f6SCédric Le Goater 
2783aa597f6SCédric Le Goater static void spapr_xive_realize(DeviceState *dev, Error **errp)
2793aa597f6SCédric Le Goater {
280ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(dev);
2813aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
2823aa597f6SCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
2833aa597f6SCédric Le Goater     Error *local_err = NULL;
28438afd772SCédric Le Goater     MachineState *machine = MACHINE(qdev_get_machine());
2853aa597f6SCédric Le Goater 
2863aa597f6SCédric Le Goater     if (!xive->nr_irqs) {
2873aa597f6SCédric Le Goater         error_setg(errp, "Number of interrupt needs to be greater 0");
2883aa597f6SCédric Le Goater         return;
2893aa597f6SCédric Le Goater     }
2903aa597f6SCédric Le Goater 
2913aa597f6SCédric Le Goater     if (!xive->nr_ends) {
2923aa597f6SCédric Le Goater         error_setg(errp, "Number of interrupt needs to be greater 0");
2933aa597f6SCédric Le Goater         return;
2943aa597f6SCédric Le Goater     }
2953aa597f6SCédric Le Goater 
2963aa597f6SCédric Le Goater     /*
2973aa597f6SCédric Le Goater      * Initialize the internal sources, for IPIs and virtual devices.
2983aa597f6SCédric Le Goater      */
2993aa597f6SCédric Le Goater     object_property_set_int(OBJECT(xsrc), xive->nr_irqs, "nr-irqs",
3003aa597f6SCédric Le Goater                             &error_fatal);
3013aa597f6SCédric Le Goater     object_property_add_const_link(OBJECT(xsrc), "xive", OBJECT(xive),
3023aa597f6SCédric Le Goater                                    &error_fatal);
3033aa597f6SCédric Le Goater     object_property_set_bool(OBJECT(xsrc), true, "realized", &local_err);
3043aa597f6SCédric Le Goater     if (local_err) {
3053aa597f6SCédric Le Goater         error_propagate(errp, local_err);
3063aa597f6SCédric Le Goater         return;
3073aa597f6SCédric Le Goater     }
3083aa597f6SCédric Le Goater 
3093aa597f6SCédric Le Goater     /*
3103aa597f6SCédric Le Goater      * Initialize the END ESB source
3113aa597f6SCédric Le Goater      */
3123aa597f6SCédric Le Goater     object_property_set_int(OBJECT(end_xsrc), xive->nr_irqs, "nr-ends",
3133aa597f6SCédric Le Goater                             &error_fatal);
3143aa597f6SCédric Le Goater     object_property_add_const_link(OBJECT(end_xsrc), "xive", OBJECT(xive),
3153aa597f6SCédric Le Goater                                    &error_fatal);
3163aa597f6SCédric Le Goater     object_property_set_bool(OBJECT(end_xsrc), true, "realized", &local_err);
3173aa597f6SCédric Le Goater     if (local_err) {
3183aa597f6SCédric Le Goater         error_propagate(errp, local_err);
3193aa597f6SCédric Le Goater         return;
3203aa597f6SCédric Le Goater     }
3213aa597f6SCédric Le Goater 
3223aa597f6SCédric Le Goater     /* Set the mapping address of the END ESB pages after the source ESBs */
3233aa597f6SCédric Le Goater     xive->end_base = xive->vc_base + (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
3243aa597f6SCédric Le Goater 
3253aa597f6SCédric Le Goater     /*
3263aa597f6SCédric Le Goater      * Allocate the routing tables
3273aa597f6SCédric Le Goater      */
3283aa597f6SCédric Le Goater     xive->eat = g_new0(XiveEAS, xive->nr_irqs);
3293aa597f6SCédric Le Goater     xive->endt = g_new0(XiveEND, xive->nr_ends);
3303aa597f6SCédric Le Goater 
33138afd772SCédric Le Goater     xive->nodename = g_strdup_printf("interrupt-controller@%" PRIx64,
33238afd772SCédric Le Goater                            xive->tm_base + XIVE_TM_USER_PAGE * (1 << TM_SHIFT));
33338afd772SCédric Le Goater 
33438afd772SCédric Le Goater     qemu_register_reset(spapr_xive_reset, dev);
33538afd772SCédric Le Goater 
33638afd772SCédric Le Goater     if (kvm_enabled() && machine_kernel_irqchip_allowed(machine)) {
33738afd772SCédric Le Goater         kvmppc_xive_connect(xive, &local_err);
33838afd772SCédric Le Goater         if (local_err && machine_kernel_irqchip_required(machine)) {
33938afd772SCédric Le Goater             error_prepend(&local_err,
34038afd772SCédric Le Goater                           "kernel_irqchip requested but unavailable: ");
34138afd772SCédric Le Goater             error_propagate(errp, local_err);
34238afd772SCédric Le Goater             return;
34338afd772SCédric Le Goater         }
34438afd772SCédric Le Goater 
34538afd772SCédric Le Goater         if (!local_err) {
34638afd772SCédric Le Goater             return;
34738afd772SCédric Le Goater         }
34838afd772SCédric Le Goater 
34938afd772SCédric Le Goater         /*
35038afd772SCédric Le Goater          * We failed to initialize the XIVE KVM device, fallback to
35138afd772SCédric Le Goater          * emulated mode
35238afd772SCédric Le Goater          */
35338afd772SCédric Le Goater         error_prepend(&local_err, "kernel_irqchip allowed but unavailable: ");
35438afd772SCédric Le Goater         warn_report_err(local_err);
35538afd772SCédric Le Goater     }
35638afd772SCédric Le Goater 
3573aa597f6SCédric Le Goater     /* TIMA initialization */
3583aa597f6SCédric Le Goater     memory_region_init_io(&xive->tm_mmio, OBJECT(xive), &xive_tm_ops, xive,
3593aa597f6SCédric Le Goater                           "xive.tima", 4ull << TM_SHIFT);
3603aa597f6SCédric Le Goater 
3613aa597f6SCédric Le Goater     /* Define all XIVE MMIO regions on SysBus */
3623aa597f6SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xsrc->esb_mmio);
3633aa597f6SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &end_xsrc->esb_mmio);
3643aa597f6SCédric Le Goater     sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xive->tm_mmio);
3653aa597f6SCédric Le Goater 
3663aa597f6SCédric Le Goater     /* Map all regions */
3673aa597f6SCédric Le Goater     spapr_xive_map_mmio(xive);
3683aa597f6SCédric Le Goater }
3693aa597f6SCédric Le Goater 
3703aa597f6SCédric Le Goater static int spapr_xive_get_eas(XiveRouter *xrtr, uint8_t eas_blk,
3713aa597f6SCédric Le Goater                               uint32_t eas_idx, XiveEAS *eas)
3723aa597f6SCédric Le Goater {
373ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3743aa597f6SCédric Le Goater 
3753aa597f6SCédric Le Goater     if (eas_idx >= xive->nr_irqs) {
3763aa597f6SCédric Le Goater         return -1;
3773aa597f6SCédric Le Goater     }
3783aa597f6SCédric Le Goater 
3793aa597f6SCédric Le Goater     *eas = xive->eat[eas_idx];
3803aa597f6SCédric Le Goater     return 0;
3813aa597f6SCédric Le Goater }
3823aa597f6SCédric Le Goater 
3833aa597f6SCédric Le Goater static int spapr_xive_get_end(XiveRouter *xrtr,
3843aa597f6SCédric Le Goater                               uint8_t end_blk, uint32_t end_idx, XiveEND *end)
3853aa597f6SCédric Le Goater {
386ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3873aa597f6SCédric Le Goater 
3883aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
3893aa597f6SCédric Le Goater         return -1;
3903aa597f6SCédric Le Goater     }
3913aa597f6SCédric Le Goater 
3923aa597f6SCédric Le Goater     memcpy(end, &xive->endt[end_idx], sizeof(XiveEND));
3933aa597f6SCédric Le Goater     return 0;
3943aa597f6SCédric Le Goater }
3953aa597f6SCédric Le Goater 
3963aa597f6SCédric Le Goater static int spapr_xive_write_end(XiveRouter *xrtr, uint8_t end_blk,
3973aa597f6SCédric Le Goater                                 uint32_t end_idx, XiveEND *end,
3983aa597f6SCédric Le Goater                                 uint8_t word_number)
3993aa597f6SCédric Le Goater {
400ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
4013aa597f6SCédric Le Goater 
4023aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
4033aa597f6SCédric Le Goater         return -1;
4043aa597f6SCédric Le Goater     }
4053aa597f6SCédric Le Goater 
4063aa597f6SCédric Le Goater     memcpy(&xive->endt[end_idx], end, sizeof(XiveEND));
4073aa597f6SCédric Le Goater     return 0;
4083aa597f6SCédric Le Goater }
4093aa597f6SCédric Le Goater 
4100cddee8dSCédric Le Goater static int spapr_xive_get_nvt(XiveRouter *xrtr,
4110cddee8dSCédric Le Goater                               uint8_t nvt_blk, uint32_t nvt_idx, XiveNVT *nvt)
4120cddee8dSCédric Le Goater {
4130cddee8dSCédric Le Goater     uint32_t vcpu_id = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
4140cddee8dSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
4150cddee8dSCédric Le Goater 
4160cddee8dSCédric Le Goater     if (!cpu) {
4170cddee8dSCédric Le Goater         /* TODO: should we assert() if we can find a NVT ? */
4180cddee8dSCédric Le Goater         return -1;
4190cddee8dSCédric Le Goater     }
4200cddee8dSCédric Le Goater 
4210cddee8dSCédric Le Goater     /*
4220cddee8dSCédric Le Goater      * sPAPR does not maintain a NVT table. Return that the NVT is
4230cddee8dSCédric Le Goater      * valid if we have found a matching CPU
4240cddee8dSCédric Le Goater      */
4250cddee8dSCédric Le Goater     nvt->w0 = cpu_to_be32(NVT_W0_VALID);
4260cddee8dSCédric Le Goater     return 0;
4270cddee8dSCédric Le Goater }
4280cddee8dSCédric Le Goater 
4290cddee8dSCédric Le Goater static int spapr_xive_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk,
4300cddee8dSCédric Le Goater                                 uint32_t nvt_idx, XiveNVT *nvt,
4310cddee8dSCédric Le Goater                                 uint8_t word_number)
4320cddee8dSCédric Le Goater {
4330cddee8dSCédric Le Goater     /*
4340cddee8dSCédric Le Goater      * We don't need to write back to the NVTs because the sPAPR
4350cddee8dSCédric Le Goater      * machine should never hit a non-scheduled NVT. It should never
4360cddee8dSCédric Le Goater      * get called.
4370cddee8dSCédric Le Goater      */
4380cddee8dSCédric Le Goater     g_assert_not_reached();
4390cddee8dSCédric Le Goater }
4400cddee8dSCédric Le Goater 
44140a5056cSCédric Le Goater static XiveTCTX *spapr_xive_get_tctx(XiveRouter *xrtr, CPUState *cs)
44240a5056cSCédric Le Goater {
44340a5056cSCédric Le Goater     PowerPCCPU *cpu = POWERPC_CPU(cs);
44440a5056cSCédric Le Goater 
445a28b9a5aSCédric Le Goater     return spapr_cpu_state(cpu)->tctx;
44640a5056cSCédric Le Goater }
44740a5056cSCédric Le Goater 
4483aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_end = {
4493aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/end",
4503aa597f6SCédric Le Goater     .version_id = 1,
4513aa597f6SCédric Le Goater     .minimum_version_id = 1,
4523aa597f6SCédric Le Goater     .fields = (VMStateField []) {
4533aa597f6SCédric Le Goater         VMSTATE_UINT32(w0, XiveEND),
4543aa597f6SCédric Le Goater         VMSTATE_UINT32(w1, XiveEND),
4553aa597f6SCédric Le Goater         VMSTATE_UINT32(w2, XiveEND),
4563aa597f6SCédric Le Goater         VMSTATE_UINT32(w3, XiveEND),
4573aa597f6SCédric Le Goater         VMSTATE_UINT32(w4, XiveEND),
4583aa597f6SCédric Le Goater         VMSTATE_UINT32(w5, XiveEND),
4593aa597f6SCédric Le Goater         VMSTATE_UINT32(w6, XiveEND),
4603aa597f6SCédric Le Goater         VMSTATE_UINT32(w7, XiveEND),
4613aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
4623aa597f6SCédric Le Goater     },
4633aa597f6SCédric Le Goater };
4643aa597f6SCédric Le Goater 
4653aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_eas = {
4663aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/eas",
4673aa597f6SCédric Le Goater     .version_id = 1,
4683aa597f6SCédric Le Goater     .minimum_version_id = 1,
4693aa597f6SCédric Le Goater     .fields = (VMStateField []) {
4703aa597f6SCédric Le Goater         VMSTATE_UINT64(w, XiveEAS),
4713aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
4723aa597f6SCédric Le Goater     },
4733aa597f6SCédric Le Goater };
4743aa597f6SCédric Le Goater 
4753aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive = {
4763aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
4773aa597f6SCédric Le Goater     .version_id = 1,
4783aa597f6SCédric Le Goater     .minimum_version_id = 1,
4793aa597f6SCédric Le Goater     .fields = (VMStateField[]) {
480ce2918cbSDavid Gibson         VMSTATE_UINT32_EQUAL(nr_irqs, SpaprXive, NULL),
481ce2918cbSDavid Gibson         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(eat, SpaprXive, nr_irqs,
4823aa597f6SCédric Le Goater                                      vmstate_spapr_xive_eas, XiveEAS),
483ce2918cbSDavid Gibson         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(endt, SpaprXive, nr_ends,
4843aa597f6SCédric Le Goater                                              vmstate_spapr_xive_end, XiveEND),
4853aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
4863aa597f6SCédric Le Goater     },
4873aa597f6SCédric Le Goater };
4883aa597f6SCédric Le Goater 
4893aa597f6SCédric Le Goater static Property spapr_xive_properties[] = {
490ce2918cbSDavid Gibson     DEFINE_PROP_UINT32("nr-irqs", SpaprXive, nr_irqs, 0),
491ce2918cbSDavid Gibson     DEFINE_PROP_UINT32("nr-ends", SpaprXive, nr_ends, 0),
492ce2918cbSDavid Gibson     DEFINE_PROP_UINT64("vc-base", SpaprXive, vc_base, SPAPR_XIVE_VC_BASE),
493ce2918cbSDavid Gibson     DEFINE_PROP_UINT64("tm-base", SpaprXive, tm_base, SPAPR_XIVE_TM_BASE),
4943aa597f6SCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
4953aa597f6SCédric Le Goater };
4963aa597f6SCédric Le Goater 
4973aa597f6SCédric Le Goater static void spapr_xive_class_init(ObjectClass *klass, void *data)
4983aa597f6SCédric Le Goater {
4993aa597f6SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
5003aa597f6SCédric Le Goater     XiveRouterClass *xrc = XIVE_ROUTER_CLASS(klass);
5013aa597f6SCédric Le Goater 
5023aa597f6SCédric Le Goater     dc->desc    = "sPAPR XIVE Interrupt Controller";
5033aa597f6SCédric Le Goater     dc->props   = spapr_xive_properties;
5043aa597f6SCédric Le Goater     dc->realize = spapr_xive_realize;
5053aa597f6SCédric Le Goater     dc->vmsd    = &vmstate_spapr_xive;
5063aa597f6SCédric Le Goater 
5073aa597f6SCédric Le Goater     xrc->get_eas = spapr_xive_get_eas;
5083aa597f6SCédric Le Goater     xrc->get_end = spapr_xive_get_end;
5093aa597f6SCédric Le Goater     xrc->write_end = spapr_xive_write_end;
5100cddee8dSCédric Le Goater     xrc->get_nvt = spapr_xive_get_nvt;
5110cddee8dSCédric Le Goater     xrc->write_nvt = spapr_xive_write_nvt;
51240a5056cSCédric Le Goater     xrc->get_tctx = spapr_xive_get_tctx;
5133aa597f6SCédric Le Goater }
5143aa597f6SCédric Le Goater 
5153aa597f6SCédric Le Goater static const TypeInfo spapr_xive_info = {
5163aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
5173aa597f6SCédric Le Goater     .parent = TYPE_XIVE_ROUTER,
5183aa597f6SCédric Le Goater     .instance_init = spapr_xive_instance_init,
519ce2918cbSDavid Gibson     .instance_size = sizeof(SpaprXive),
5203aa597f6SCédric Le Goater     .class_init = spapr_xive_class_init,
5213aa597f6SCédric Le Goater };
5223aa597f6SCédric Le Goater 
5233aa597f6SCédric Le Goater static void spapr_xive_register_types(void)
5243aa597f6SCédric Le Goater {
5253aa597f6SCédric Le Goater     type_register_static(&spapr_xive_info);
5263aa597f6SCédric Le Goater }
5273aa597f6SCédric Le Goater 
5283aa597f6SCédric Le Goater type_init(spapr_xive_register_types)
5293aa597f6SCédric Le Goater 
530ce2918cbSDavid Gibson bool spapr_xive_irq_claim(SpaprXive *xive, uint32_t lisn, bool lsi)
5313aa597f6SCédric Le Goater {
5323aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
5333aa597f6SCédric Le Goater 
5343aa597f6SCédric Le Goater     if (lisn >= xive->nr_irqs) {
5353aa597f6SCédric Le Goater         return false;
5363aa597f6SCédric Le Goater     }
5373aa597f6SCédric Le Goater 
5383aa597f6SCédric Le Goater     xive->eat[lisn].w |= cpu_to_be64(EAS_VALID);
5390afed8c8SGreg Kurz     if (lsi) {
5400afed8c8SGreg Kurz         xive_source_irq_set_lsi(xsrc, lisn);
5410afed8c8SGreg Kurz     }
54238afd772SCédric Le Goater 
54338afd772SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
54438afd772SCédric Le Goater         Error *local_err = NULL;
54538afd772SCédric Le Goater 
54638afd772SCédric Le Goater         kvmppc_xive_source_reset_one(xsrc, lisn, &local_err);
54738afd772SCédric Le Goater         if (local_err) {
54838afd772SCédric Le Goater             error_report_err(local_err);
54938afd772SCédric Le Goater             return false;
55038afd772SCédric Le Goater         }
55138afd772SCédric Le Goater     }
55238afd772SCédric Le Goater 
5533aa597f6SCédric Le Goater     return true;
5543aa597f6SCédric Le Goater }
5553aa597f6SCédric Le Goater 
556ce2918cbSDavid Gibson bool spapr_xive_irq_free(SpaprXive *xive, uint32_t lisn)
5573aa597f6SCédric Le Goater {
5583aa597f6SCédric Le Goater     if (lisn >= xive->nr_irqs) {
5593aa597f6SCédric Le Goater         return false;
5603aa597f6SCédric Le Goater     }
5613aa597f6SCédric Le Goater 
5623aa597f6SCédric Le Goater     xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
5633aa597f6SCédric Le Goater     return true;
5643aa597f6SCédric Le Goater }
5653aa597f6SCédric Le Goater 
56623bcd5ebSCédric Le Goater /*
56723bcd5ebSCédric Le Goater  * XIVE hcalls
56823bcd5ebSCédric Le Goater  *
56923bcd5ebSCédric Le Goater  * The terminology used by the XIVE hcalls is the following :
57023bcd5ebSCédric Le Goater  *
57123bcd5ebSCédric Le Goater  *   TARGET vCPU number
57223bcd5ebSCédric Le Goater  *   EQ     Event Queue assigned by OS to receive event data
57323bcd5ebSCédric Le Goater  *   ESB    page for source interrupt management
57423bcd5ebSCédric Le Goater  *   LISN   Logical Interrupt Source Number identifying a source in the
57523bcd5ebSCédric Le Goater  *          machine
57623bcd5ebSCédric Le Goater  *   EISN   Effective Interrupt Source Number used by guest OS to
57723bcd5ebSCédric Le Goater  *          identify source in the guest
57823bcd5ebSCédric Le Goater  *
57923bcd5ebSCédric Le Goater  * The EAS, END, NVT structures are not exposed.
58023bcd5ebSCédric Le Goater  */
58123bcd5ebSCédric Le Goater 
58223bcd5ebSCédric Le Goater /*
58323bcd5ebSCédric Le Goater  * Linux hosts under OPAL reserve priority 7 for their own escalation
58423bcd5ebSCédric Le Goater  * interrupts (DD2.X POWER9). So we only allow the guest to use
58523bcd5ebSCédric Le Goater  * priorities [0..6].
58623bcd5ebSCédric Le Goater  */
58723bcd5ebSCédric Le Goater static bool spapr_xive_priority_is_reserved(uint8_t priority)
58823bcd5ebSCédric Le Goater {
58923bcd5ebSCédric Le Goater     switch (priority) {
59023bcd5ebSCédric Le Goater     case 0 ... 6:
59123bcd5ebSCédric Le Goater         return false;
59223bcd5ebSCédric Le Goater     case 7: /* OPAL escalation queue */
59323bcd5ebSCédric Le Goater     default:
59423bcd5ebSCédric Le Goater         return true;
59523bcd5ebSCédric Le Goater     }
59623bcd5ebSCédric Le Goater }
59723bcd5ebSCédric Le Goater 
59823bcd5ebSCédric Le Goater /*
59923bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical
60023bcd5ebSCédric Le Goater  * real address of the MMIO page through which the Event State Buffer
60123bcd5ebSCédric Le Goater  * entry associated with the value of the "lisn" parameter is managed.
60223bcd5ebSCédric Le Goater  *
60323bcd5ebSCédric Le Goater  * Parameters:
60423bcd5ebSCédric Le Goater  * Input
60523bcd5ebSCédric Le Goater  * - R4: "flags"
60623bcd5ebSCédric Le Goater  *         Bits 0-63 reserved
60723bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
60823bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
60923bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned
61023bcd5ebSCédric Le Goater  *       by the H_ALLOCATE_VAS_WINDOW hcall
61123bcd5ebSCédric Le Goater  *
61223bcd5ebSCédric Le Goater  * Output
61323bcd5ebSCédric Le Goater  * - R4: "flags"
61423bcd5ebSCédric Le Goater  *         Bits 0-59: Reserved
61523bcd5ebSCédric Le Goater  *         Bit 60: H_INT_ESB must be used for Event State Buffer
61623bcd5ebSCédric Le Goater  *                 management
61723bcd5ebSCédric Le Goater  *         Bit 61: 1 == LSI  0 == MSI
61823bcd5ebSCédric Le Goater  *         Bit 62: the full function page supports trigger
61923bcd5ebSCédric Le Goater  *         Bit 63: Store EOI Supported
62023bcd5ebSCédric Le Goater  * - R5: Logical Real address of full function Event State Buffer
62123bcd5ebSCédric Le Goater  *       management page, -1 if H_INT_ESB hcall flag is set to 1.
62223bcd5ebSCédric Le Goater  * - R6: Logical Real Address of trigger only Event State Buffer
62323bcd5ebSCédric Le Goater  *       management page or -1.
62423bcd5ebSCédric Le Goater  * - R7: Power of 2 page size for the ESB management pages returned in
62523bcd5ebSCédric Le Goater  *       R5 and R6.
62623bcd5ebSCédric Le Goater  */
62723bcd5ebSCédric Le Goater 
62823bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_H_INT_ESB     PPC_BIT(60) /* ESB manage with H_INT_ESB */
62923bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_LSI           PPC_BIT(61) /* Virtual LSI type */
63023bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_TRIGGER       PPC_BIT(62) /* Trigger and management
63123bcd5ebSCédric Le Goater                                                     on same page */
63223bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_STORE_EOI     PPC_BIT(63) /* Store EOI support */
63323bcd5ebSCédric Le Goater 
63423bcd5ebSCédric Le Goater static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
635ce2918cbSDavid Gibson                                           SpaprMachineState *spapr,
63623bcd5ebSCédric Le Goater                                           target_ulong opcode,
63723bcd5ebSCédric Le Goater                                           target_ulong *args)
63823bcd5ebSCédric Le Goater {
639ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
64023bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
64123bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
64223bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
64323bcd5ebSCédric Le Goater 
64423bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
64523bcd5ebSCédric Le Goater         return H_FUNCTION;
64623bcd5ebSCédric Le Goater     }
64723bcd5ebSCédric Le Goater 
64823bcd5ebSCédric Le Goater     if (flags) {
64923bcd5ebSCédric Le Goater         return H_PARAMETER;
65023bcd5ebSCédric Le Goater     }
65123bcd5ebSCédric Le Goater 
65223bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
65323bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
65423bcd5ebSCédric Le Goater                       lisn);
65523bcd5ebSCédric Le Goater         return H_P2;
65623bcd5ebSCédric Le Goater     }
65723bcd5ebSCédric Le Goater 
65823bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&xive->eat[lisn])) {
65923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
66023bcd5ebSCédric Le Goater                       lisn);
66123bcd5ebSCédric Le Goater         return H_P2;
66223bcd5ebSCédric Le Goater     }
66323bcd5ebSCédric Le Goater 
66423bcd5ebSCédric Le Goater     /*
66523bcd5ebSCédric Le Goater      * All sources are emulated under the main XIVE object and share
66623bcd5ebSCédric Le Goater      * the same characteristics.
66723bcd5ebSCédric Le Goater      */
66823bcd5ebSCédric Le Goater     args[0] = 0;
66923bcd5ebSCédric Le Goater     if (!xive_source_esb_has_2page(xsrc)) {
67023bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_TRIGGER;
67123bcd5ebSCédric Le Goater     }
67223bcd5ebSCédric Le Goater     if (xsrc->esb_flags & XIVE_SRC_STORE_EOI) {
67323bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_STORE_EOI;
67423bcd5ebSCédric Le Goater     }
67523bcd5ebSCédric Le Goater 
67623bcd5ebSCédric Le Goater     /*
67723bcd5ebSCédric Le Goater      * Force the use of the H_INT_ESB hcall in case of an LSI
67823bcd5ebSCédric Le Goater      * interrupt. This is necessary under KVM to re-trigger the
67923bcd5ebSCédric Le Goater      * interrupt if the level is still asserted
68023bcd5ebSCédric Le Goater      */
68123bcd5ebSCédric Le Goater     if (xive_source_irq_is_lsi(xsrc, lisn)) {
68223bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_H_INT_ESB | SPAPR_XIVE_SRC_LSI;
68323bcd5ebSCédric Le Goater     }
68423bcd5ebSCédric Le Goater 
68523bcd5ebSCédric Le Goater     if (!(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
68623bcd5ebSCédric Le Goater         args[1] = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn);
68723bcd5ebSCédric Le Goater     } else {
68823bcd5ebSCédric Le Goater         args[1] = -1;
68923bcd5ebSCédric Le Goater     }
69023bcd5ebSCédric Le Goater 
69123bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc) &&
69223bcd5ebSCédric Le Goater         !(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
69323bcd5ebSCédric Le Goater         args[2] = xive->vc_base + xive_source_esb_page(xsrc, lisn);
69423bcd5ebSCédric Le Goater     } else {
69523bcd5ebSCédric Le Goater         args[2] = -1;
69623bcd5ebSCédric Le Goater     }
69723bcd5ebSCédric Le Goater 
69823bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc)) {
69923bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift - 1;
70023bcd5ebSCédric Le Goater     } else {
70123bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift;
70223bcd5ebSCédric Le Goater     }
70323bcd5ebSCédric Le Goater 
70423bcd5ebSCédric Le Goater     return H_SUCCESS;
70523bcd5ebSCédric Le Goater }
70623bcd5ebSCédric Le Goater 
70723bcd5ebSCédric Le Goater /*
70823bcd5ebSCédric Le Goater  * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
70923bcd5ebSCédric Le Goater  * Interrupt Source to a target. The Logical Interrupt Source is
71023bcd5ebSCédric Le Goater  * designated with the "lisn" parameter and the target is designated
71123bcd5ebSCédric Le Goater  * with the "target" and "priority" parameters.  Upon return from the
71223bcd5ebSCédric Le Goater  * hcall(), no additional interrupts will be directed to the old EQ.
71323bcd5ebSCédric Le Goater  *
71423bcd5ebSCédric Le Goater  * Parameters:
71523bcd5ebSCédric Le Goater  * Input:
71623bcd5ebSCédric Le Goater  * - R4: "flags"
71723bcd5ebSCédric Le Goater  *         Bits 0-61: Reserved
71823bcd5ebSCédric Le Goater  *         Bit 62: set the "eisn" in the EAS
71923bcd5ebSCédric Le Goater  *         Bit 63: masks the interrupt source in the hardware interrupt
72023bcd5ebSCédric Le Goater  *       control structure. An interrupt masked by this mechanism will
72123bcd5ebSCédric Le Goater  *       be dropped, but it's source state bits will still be
72223bcd5ebSCédric Le Goater  *       set. There is no race-free way of unmasking and restoring the
72323bcd5ebSCédric Le Goater  *       source. Thus this should only be used in interrupts that are
72423bcd5ebSCédric Le Goater  *       also masked at the source, and only in cases where the
72523bcd5ebSCédric Le Goater  *       interrupt is not meant to be used for a large amount of time
72623bcd5ebSCédric Le Goater  *       because no valid target exists for it for example
72723bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
72823bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
72923bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned by
73023bcd5ebSCédric Le Goater  *       the H_ALLOCATE_VAS_WINDOW hcall
73123bcd5ebSCédric Le Goater  * - R6: "target" is per "ibm,ppc-interrupt-server#s" or
73223bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
73323bcd5ebSCédric Le Goater  * - R7: "priority" is a valid priority not in
73423bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
73523bcd5ebSCédric Le Goater  * - R8: "eisn" is the guest EISN associated with the "lisn"
73623bcd5ebSCédric Le Goater  *
73723bcd5ebSCédric Le Goater  * Output:
73823bcd5ebSCédric Le Goater  * - None
73923bcd5ebSCédric Le Goater  */
74023bcd5ebSCédric Le Goater 
74123bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_SET_EISN PPC_BIT(62)
74223bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_MASK     PPC_BIT(63)
74323bcd5ebSCédric Le Goater 
74423bcd5ebSCédric Le Goater static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
745ce2918cbSDavid Gibson                                             SpaprMachineState *spapr,
74623bcd5ebSCédric Le Goater                                             target_ulong opcode,
74723bcd5ebSCédric Le Goater                                             target_ulong *args)
74823bcd5ebSCédric Le Goater {
749ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
75023bcd5ebSCédric Le Goater     XiveEAS eas, new_eas;
75123bcd5ebSCédric Le Goater     target_ulong flags    = args[0];
75223bcd5ebSCédric Le Goater     target_ulong lisn     = args[1];
75323bcd5ebSCédric Le Goater     target_ulong target   = args[2];
75423bcd5ebSCédric Le Goater     target_ulong priority = args[3];
75523bcd5ebSCédric Le Goater     target_ulong eisn     = args[4];
75623bcd5ebSCédric Le Goater     uint8_t end_blk;
75723bcd5ebSCédric Le Goater     uint32_t end_idx;
75823bcd5ebSCédric Le Goater 
75923bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
76023bcd5ebSCédric Le Goater         return H_FUNCTION;
76123bcd5ebSCédric Le Goater     }
76223bcd5ebSCédric Le Goater 
76323bcd5ebSCédric Le Goater     if (flags & ~(SPAPR_XIVE_SRC_SET_EISN | SPAPR_XIVE_SRC_MASK)) {
76423bcd5ebSCédric Le Goater         return H_PARAMETER;
76523bcd5ebSCédric Le Goater     }
76623bcd5ebSCédric Le Goater 
76723bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
76823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
76923bcd5ebSCédric Le Goater                       lisn);
77023bcd5ebSCédric Le Goater         return H_P2;
77123bcd5ebSCédric Le Goater     }
77223bcd5ebSCédric Le Goater 
77323bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
77423bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
77523bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
77623bcd5ebSCédric Le Goater                       lisn);
77723bcd5ebSCédric Le Goater         return H_P2;
77823bcd5ebSCédric Le Goater     }
77923bcd5ebSCédric Le Goater 
78023bcd5ebSCédric Le Goater     /* priority 0xff is used to reset the EAS */
78123bcd5ebSCédric Le Goater     if (priority == 0xff) {
78223bcd5ebSCédric Le Goater         new_eas.w = cpu_to_be64(EAS_VALID | EAS_MASKED);
78323bcd5ebSCédric Le Goater         goto out;
78423bcd5ebSCédric Le Goater     }
78523bcd5ebSCédric Le Goater 
78623bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_MASK) {
78723bcd5ebSCédric Le Goater         new_eas.w = eas.w | cpu_to_be64(EAS_MASKED);
78823bcd5ebSCédric Le Goater     } else {
78923bcd5ebSCédric Le Goater         new_eas.w = eas.w & cpu_to_be64(~EAS_MASKED);
79023bcd5ebSCédric Le Goater     }
79123bcd5ebSCédric Le Goater 
79223bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
79323bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
79423bcd5ebSCédric Le Goater                       " is reserved\n", priority);
79523bcd5ebSCédric Le Goater         return H_P4;
79623bcd5ebSCédric Le Goater     }
79723bcd5ebSCédric Le Goater 
79823bcd5ebSCédric Le Goater     /*
79923bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
80023bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
80123bcd5ebSCédric Le Goater      * target.
80223bcd5ebSCédric Le Goater      */
80323bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
80423bcd5ebSCédric Le Goater         return H_P3;
80523bcd5ebSCédric Le Goater     }
80623bcd5ebSCédric Le Goater 
80723bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_BLOCK, new_eas.w, end_blk);
80823bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_INDEX, new_eas.w, end_idx);
80923bcd5ebSCédric Le Goater 
81023bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_SET_EISN) {
81123bcd5ebSCédric Le Goater         new_eas.w = xive_set_field64(EAS_END_DATA, new_eas.w, eisn);
81223bcd5ebSCédric Le Goater     }
81323bcd5ebSCédric Le Goater 
8140c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
8150c575703SCédric Le Goater         Error *local_err = NULL;
8160c575703SCédric Le Goater 
8170c575703SCédric Le Goater         kvmppc_xive_set_source_config(xive, lisn, &new_eas, &local_err);
8180c575703SCédric Le Goater         if (local_err) {
8190c575703SCédric Le Goater             error_report_err(local_err);
8200c575703SCédric Le Goater             return H_HARDWARE;
8210c575703SCédric Le Goater         }
8220c575703SCédric Le Goater     }
8230c575703SCédric Le Goater 
82423bcd5ebSCédric Le Goater out:
82523bcd5ebSCédric Le Goater     xive->eat[lisn] = new_eas;
82623bcd5ebSCédric Le Goater     return H_SUCCESS;
82723bcd5ebSCédric Le Goater }
82823bcd5ebSCédric Le Goater 
82923bcd5ebSCédric Le Goater /*
83023bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
83123bcd5ebSCédric Le Goater  * target/priority pair is assigned to the specified Logical Interrupt
83223bcd5ebSCédric Le Goater  * Source.
83323bcd5ebSCédric Le Goater  *
83423bcd5ebSCédric Le Goater  * Parameters:
83523bcd5ebSCédric Le Goater  * Input:
83623bcd5ebSCédric Le Goater  * - R4: "flags"
83723bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
83823bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
83923bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
84023bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
84123bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
84223bcd5ebSCédric Le Goater  *
84323bcd5ebSCédric Le Goater  * Output:
84423bcd5ebSCédric Le Goater  * - R4: Target to which the specified Logical Interrupt Source is
84523bcd5ebSCédric Le Goater  *       assigned
84623bcd5ebSCédric Le Goater  * - R5: Priority to which the specified Logical Interrupt Source is
84723bcd5ebSCédric Le Goater  *       assigned
84823bcd5ebSCédric Le Goater  * - R6: EISN for the specified Logical Interrupt Source (this will be
84923bcd5ebSCédric Le Goater  *       equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
85023bcd5ebSCédric Le Goater  */
85123bcd5ebSCédric Le Goater static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
852ce2918cbSDavid Gibson                                             SpaprMachineState *spapr,
85323bcd5ebSCédric Le Goater                                             target_ulong opcode,
85423bcd5ebSCédric Le Goater                                             target_ulong *args)
85523bcd5ebSCédric Le Goater {
856ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
85723bcd5ebSCédric Le Goater     target_ulong flags = args[0];
85823bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
85923bcd5ebSCédric Le Goater     XiveEAS eas;
86023bcd5ebSCédric Le Goater     XiveEND *end;
86123bcd5ebSCédric Le Goater     uint8_t nvt_blk;
86223bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
86323bcd5ebSCédric Le Goater 
86423bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
86523bcd5ebSCédric Le Goater         return H_FUNCTION;
86623bcd5ebSCédric Le Goater     }
86723bcd5ebSCédric Le Goater 
86823bcd5ebSCédric Le Goater     if (flags) {
86923bcd5ebSCédric Le Goater         return H_PARAMETER;
87023bcd5ebSCédric Le Goater     }
87123bcd5ebSCédric Le Goater 
87223bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
87323bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
87423bcd5ebSCédric Le Goater                       lisn);
87523bcd5ebSCédric Le Goater         return H_P2;
87623bcd5ebSCédric Le Goater     }
87723bcd5ebSCédric Le Goater 
87823bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
87923bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
88023bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
88123bcd5ebSCédric Le Goater                       lisn);
88223bcd5ebSCédric Le Goater         return H_P2;
88323bcd5ebSCédric Le Goater     }
88423bcd5ebSCédric Le Goater 
88523bcd5ebSCédric Le Goater     /* EAS_END_BLOCK is unused on sPAPR */
88623bcd5ebSCédric Le Goater     end_idx = xive_get_field64(EAS_END_INDEX, eas.w);
88723bcd5ebSCédric Le Goater 
88823bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
88923bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
89023bcd5ebSCédric Le Goater 
89123bcd5ebSCédric Le Goater     nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
89223bcd5ebSCédric Le Goater     nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
89323bcd5ebSCédric Le Goater     args[0] = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
89423bcd5ebSCédric Le Goater 
89523bcd5ebSCédric Le Goater     if (xive_eas_is_masked(&eas)) {
89623bcd5ebSCédric Le Goater         args[1] = 0xff;
89723bcd5ebSCédric Le Goater     } else {
89823bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
89923bcd5ebSCédric Le Goater     }
90023bcd5ebSCédric Le Goater 
90123bcd5ebSCédric Le Goater     args[2] = xive_get_field64(EAS_END_DATA, eas.w);
90223bcd5ebSCédric Le Goater 
90323bcd5ebSCédric Le Goater     return H_SUCCESS;
90423bcd5ebSCédric Le Goater }
90523bcd5ebSCédric Le Goater 
90623bcd5ebSCédric Le Goater /*
90723bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
90823bcd5ebSCédric Le Goater  * address of the notification management page associated with the
90923bcd5ebSCédric Le Goater  * specified target and priority.
91023bcd5ebSCédric Le Goater  *
91123bcd5ebSCédric Le Goater  * Parameters:
91223bcd5ebSCédric Le Goater  * Input:
91323bcd5ebSCédric Le Goater  * - R4: "flags"
91423bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
91523bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
91623bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
91723bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
91823bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
91923bcd5ebSCédric Le Goater  *
92023bcd5ebSCédric Le Goater  * Output:
92123bcd5ebSCédric Le Goater  * - R4: Logical real address of notification page
92223bcd5ebSCédric Le Goater  * - R5: Power of 2 page size of the notification page
92323bcd5ebSCédric Le Goater  */
92423bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
925ce2918cbSDavid Gibson                                          SpaprMachineState *spapr,
92623bcd5ebSCédric Le Goater                                          target_ulong opcode,
92723bcd5ebSCédric Le Goater                                          target_ulong *args)
92823bcd5ebSCédric Le Goater {
929ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
93023bcd5ebSCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
93123bcd5ebSCédric Le Goater     target_ulong flags = args[0];
93223bcd5ebSCédric Le Goater     target_ulong target = args[1];
93323bcd5ebSCédric Le Goater     target_ulong priority = args[2];
93423bcd5ebSCédric Le Goater     XiveEND *end;
93523bcd5ebSCédric Le Goater     uint8_t end_blk;
93623bcd5ebSCédric Le Goater     uint32_t end_idx;
93723bcd5ebSCédric Le Goater 
93823bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
93923bcd5ebSCédric Le Goater         return H_FUNCTION;
94023bcd5ebSCédric Le Goater     }
94123bcd5ebSCédric Le Goater 
94223bcd5ebSCédric Le Goater     if (flags) {
94323bcd5ebSCédric Le Goater         return H_PARAMETER;
94423bcd5ebSCédric Le Goater     }
94523bcd5ebSCédric Le Goater 
94623bcd5ebSCédric Le Goater     /*
94723bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
94823bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
94923bcd5ebSCédric Le Goater      */
95023bcd5ebSCédric Le Goater 
95123bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
95223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
95323bcd5ebSCédric Le Goater                       " is reserved\n", priority);
95423bcd5ebSCédric Le Goater         return H_P3;
95523bcd5ebSCédric Le Goater     }
95623bcd5ebSCédric Le Goater 
95723bcd5ebSCédric Le Goater     /*
95823bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
95923bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
96023bcd5ebSCédric Le Goater      * target.
96123bcd5ebSCédric Le Goater      */
96223bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
96323bcd5ebSCédric Le Goater         return H_P2;
96423bcd5ebSCédric Le Goater     }
96523bcd5ebSCédric Le Goater 
96623bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
96723bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
96823bcd5ebSCédric Le Goater 
96923bcd5ebSCédric Le Goater     args[0] = xive->end_base + (1ull << (end_xsrc->esb_shift + 1)) * end_idx;
97023bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
97123bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
97223bcd5ebSCédric Le Goater     } else {
97323bcd5ebSCédric Le Goater         args[1] = 0;
97423bcd5ebSCédric Le Goater     }
97523bcd5ebSCédric Le Goater 
97623bcd5ebSCédric Le Goater     return H_SUCCESS;
97723bcd5ebSCédric Le Goater }
97823bcd5ebSCédric Le Goater 
97923bcd5ebSCédric Le Goater /*
98023bcd5ebSCédric Le Goater  * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
98123bcd5ebSCédric Le Goater  * a given "target" and "priority".  It is also used to set the
98223bcd5ebSCédric Le Goater  * notification config associated with the EQ.  An EQ size of 0 is
98323bcd5ebSCédric Le Goater  * used to reset the EQ config for a given target and priority. If
98423bcd5ebSCédric Le Goater  * resetting the EQ config, the END associated with the given "target"
98523bcd5ebSCédric Le Goater  * and "priority" will be changed to disable queueing.
98623bcd5ebSCédric Le Goater  *
98723bcd5ebSCédric Le Goater  * Upon return from the hcall(), no additional interrupts will be
98823bcd5ebSCédric Le Goater  * directed to the old EQ (if one was set). The old EQ (if one was
98923bcd5ebSCédric Le Goater  * set) should be investigated for interrupts that occurred prior to
99023bcd5ebSCédric Le Goater  * or during the hcall().
99123bcd5ebSCédric Le Goater  *
99223bcd5ebSCédric Le Goater  * Parameters:
99323bcd5ebSCédric Le Goater  * Input:
99423bcd5ebSCédric Le Goater  * - R4: "flags"
99523bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
99623bcd5ebSCédric Le Goater  *         Bit 63: Unconditional Notify (n) per the XIVE spec
99723bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
99823bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
99923bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
100023bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
100123bcd5ebSCédric Le Goater  * - R7: "eventQueue": The logical real address of the start of the EQ
100223bcd5ebSCédric Le Goater  * - R8: "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
100323bcd5ebSCédric Le Goater  *
100423bcd5ebSCédric Le Goater  * Output:
100523bcd5ebSCédric Le Goater  * - None
100623bcd5ebSCédric Le Goater  */
100723bcd5ebSCédric Le Goater 
100823bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_ALWAYS_NOTIFY PPC_BIT(63)
100923bcd5ebSCédric Le Goater 
101023bcd5ebSCédric Le Goater static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
1011ce2918cbSDavid Gibson                                            SpaprMachineState *spapr,
101223bcd5ebSCédric Le Goater                                            target_ulong opcode,
101323bcd5ebSCédric Le Goater                                            target_ulong *args)
101423bcd5ebSCédric Le Goater {
1015ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
101623bcd5ebSCédric Le Goater     target_ulong flags = args[0];
101723bcd5ebSCédric Le Goater     target_ulong target = args[1];
101823bcd5ebSCédric Le Goater     target_ulong priority = args[2];
101923bcd5ebSCédric Le Goater     target_ulong qpage = args[3];
102023bcd5ebSCédric Le Goater     target_ulong qsize = args[4];
102123bcd5ebSCédric Le Goater     XiveEND end;
102223bcd5ebSCédric Le Goater     uint8_t end_blk, nvt_blk;
102323bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
102423bcd5ebSCédric Le Goater 
102523bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
102623bcd5ebSCédric Le Goater         return H_FUNCTION;
102723bcd5ebSCédric Le Goater     }
102823bcd5ebSCédric Le Goater 
102923bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_ALWAYS_NOTIFY) {
103023bcd5ebSCédric Le Goater         return H_PARAMETER;
103123bcd5ebSCédric Le Goater     }
103223bcd5ebSCédric Le Goater 
103323bcd5ebSCédric Le Goater     /*
103423bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
103523bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
103623bcd5ebSCédric Le Goater      */
103723bcd5ebSCédric Le Goater 
103823bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
103923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
104023bcd5ebSCédric Le Goater                       " is reserved\n", priority);
104123bcd5ebSCédric Le Goater         return H_P3;
104223bcd5ebSCédric Le Goater     }
104323bcd5ebSCédric Le Goater 
104423bcd5ebSCédric Le Goater     /*
104523bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
104623bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
104723bcd5ebSCédric Le Goater      * target.
104823bcd5ebSCédric Le Goater      */
104923bcd5ebSCédric Le Goater 
105023bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
105123bcd5ebSCédric Le Goater         return H_P2;
105223bcd5ebSCédric Le Goater     }
105323bcd5ebSCédric Le Goater 
105423bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
105523bcd5ebSCédric Le Goater     memcpy(&end, &xive->endt[end_idx], sizeof(XiveEND));
105623bcd5ebSCédric Le Goater 
105723bcd5ebSCédric Le Goater     switch (qsize) {
105823bcd5ebSCédric Le Goater     case 12:
105923bcd5ebSCédric Le Goater     case 16:
106023bcd5ebSCédric Le Goater     case 21:
106123bcd5ebSCédric Le Goater     case 24:
10627f9136f9SCédric Le Goater         if (!QEMU_IS_ALIGNED(qpage, 1ul << qsize)) {
10637f9136f9SCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: EQ @0x%" HWADDR_PRIx
10647f9136f9SCédric Le Goater                           " is not naturally aligned with %" HWADDR_PRIx "\n",
10657f9136f9SCédric Le Goater                           qpage, (hwaddr)1 << qsize);
10667f9136f9SCédric Le Goater             return H_P4;
10677f9136f9SCédric Le Goater         }
106823bcd5ebSCédric Le Goater         end.w2 = cpu_to_be32((qpage >> 32) & 0x0fffffff);
106923bcd5ebSCédric Le Goater         end.w3 = cpu_to_be32(qpage & 0xffffffff);
107023bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_ENQUEUE);
107123bcd5ebSCédric Le Goater         end.w0 = xive_set_field32(END_W0_QSIZE, end.w0, qsize - 12);
107223bcd5ebSCédric Le Goater         break;
107323bcd5ebSCédric Le Goater     case 0:
107423bcd5ebSCédric Le Goater         /* reset queue and disable queueing */
107523bcd5ebSCédric Le Goater         spapr_xive_end_reset(&end);
107623bcd5ebSCédric Le Goater         goto out;
107723bcd5ebSCédric Le Goater 
107823bcd5ebSCédric Le Goater     default:
107923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid EQ size %"PRIx64"\n",
108023bcd5ebSCédric Le Goater                       qsize);
108123bcd5ebSCédric Le Goater         return H_P5;
108223bcd5ebSCédric Le Goater     }
108323bcd5ebSCédric Le Goater 
108423bcd5ebSCédric Le Goater     if (qsize) {
108523bcd5ebSCédric Le Goater         hwaddr plen = 1 << qsize;
108623bcd5ebSCédric Le Goater         void *eq;
108723bcd5ebSCédric Le Goater 
108823bcd5ebSCédric Le Goater         /*
108923bcd5ebSCédric Le Goater          * Validate the guest EQ. We should also check that the queue
109023bcd5ebSCédric Le Goater          * has been zeroed by the OS.
109123bcd5ebSCédric Le Goater          */
109223bcd5ebSCédric Le Goater         eq = address_space_map(CPU(cpu)->as, qpage, &plen, true,
109323bcd5ebSCédric Le Goater                                MEMTXATTRS_UNSPECIFIED);
109423bcd5ebSCédric Le Goater         if (plen != 1 << qsize) {
109523bcd5ebSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to map EQ @0x%"
109623bcd5ebSCédric Le Goater                           HWADDR_PRIx "\n", qpage);
109723bcd5ebSCédric Le Goater             return H_P4;
109823bcd5ebSCédric Le Goater         }
109923bcd5ebSCédric Le Goater         address_space_unmap(CPU(cpu)->as, eq, plen, true, plen);
110023bcd5ebSCédric Le Goater     }
110123bcd5ebSCédric Le Goater 
110223bcd5ebSCédric Le Goater     /* "target" should have been validated above */
110323bcd5ebSCédric Le Goater     if (spapr_xive_target_to_nvt(target, &nvt_blk, &nvt_idx)) {
110423bcd5ebSCédric Le Goater         g_assert_not_reached();
110523bcd5ebSCédric Le Goater     }
110623bcd5ebSCédric Le Goater 
110723bcd5ebSCédric Le Goater     /*
110823bcd5ebSCédric Le Goater      * Ensure the priority and target are correctly set (they will not
110923bcd5ebSCédric Le Goater      * be right after allocation)
111023bcd5ebSCédric Le Goater      */
111123bcd5ebSCédric Le Goater     end.w6 = xive_set_field32(END_W6_NVT_BLOCK, 0ul, nvt_blk) |
111223bcd5ebSCédric Le Goater         xive_set_field32(END_W6_NVT_INDEX, 0ul, nvt_idx);
111323bcd5ebSCédric Le Goater     end.w7 = xive_set_field32(END_W7_F0_PRIORITY, 0ul, priority);
111423bcd5ebSCédric Le Goater 
111523bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_ALWAYS_NOTIFY) {
111623bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_UCOND_NOTIFY);
111723bcd5ebSCédric Le Goater     } else {
111823bcd5ebSCédric Le Goater         end.w0 &= cpu_to_be32((uint32_t)~END_W0_UCOND_NOTIFY);
111923bcd5ebSCédric Le Goater     }
112023bcd5ebSCédric Le Goater 
112123bcd5ebSCédric Le Goater     /*
112223bcd5ebSCédric Le Goater      * The generation bit for the END starts at 1 and The END page
112323bcd5ebSCédric Le Goater      * offset counter starts at 0.
112423bcd5ebSCédric Le Goater      */
112523bcd5ebSCédric Le Goater     end.w1 = cpu_to_be32(END_W1_GENERATION) |
112623bcd5ebSCédric Le Goater         xive_set_field32(END_W1_PAGE_OFF, 0ul, 0ul);
112723bcd5ebSCédric Le Goater     end.w0 |= cpu_to_be32(END_W0_VALID);
112823bcd5ebSCédric Le Goater 
112923bcd5ebSCédric Le Goater     /*
113023bcd5ebSCédric Le Goater      * TODO: issue syncs required to ensure all in-flight interrupts
113123bcd5ebSCédric Le Goater      * are complete on the old END
113223bcd5ebSCédric Le Goater      */
113323bcd5ebSCédric Le Goater 
113423bcd5ebSCédric Le Goater out:
11350c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
11360c575703SCédric Le Goater         Error *local_err = NULL;
11370c575703SCédric Le Goater 
11380c575703SCédric Le Goater         kvmppc_xive_set_queue_config(xive, end_blk, end_idx, &end, &local_err);
11390c575703SCédric Le Goater         if (local_err) {
11400c575703SCédric Le Goater             error_report_err(local_err);
11410c575703SCédric Le Goater             return H_HARDWARE;
11420c575703SCédric Le Goater         }
11430c575703SCédric Le Goater     }
11440c575703SCédric Le Goater 
114523bcd5ebSCédric Le Goater     /* Update END */
114623bcd5ebSCédric Le Goater     memcpy(&xive->endt[end_idx], &end, sizeof(XiveEND));
114723bcd5ebSCédric Le Goater     return H_SUCCESS;
114823bcd5ebSCédric Le Goater }
114923bcd5ebSCédric Le Goater 
115023bcd5ebSCédric Le Goater /*
115123bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
115223bcd5ebSCédric Le Goater  * target and priority.
115323bcd5ebSCédric Le Goater  *
115423bcd5ebSCédric Le Goater  * Parameters:
115523bcd5ebSCédric Le Goater  * Input:
115623bcd5ebSCédric Le Goater  * - R4: "flags"
115723bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
115823bcd5ebSCédric Le Goater  *         Bit 63: Debug: Return debug data
115923bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
116023bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
116123bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
116223bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
116323bcd5ebSCédric Le Goater  *
116423bcd5ebSCédric Le Goater  * Output:
116523bcd5ebSCédric Le Goater  * - R4: "flags":
116623bcd5ebSCédric Le Goater  *       Bits 0-61: Reserved
116723bcd5ebSCédric Le Goater  *       Bit 62: The value of Event Queue Generation Number (g) per
116823bcd5ebSCédric Le Goater  *              the XIVE spec if "Debug" = 1
116923bcd5ebSCédric Le Goater  *       Bit 63: The value of Unconditional Notify (n) per the XIVE spec
117023bcd5ebSCédric Le Goater  * - R5: The logical real address of the start of the EQ
117123bcd5ebSCédric Le Goater  * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
117223bcd5ebSCédric Le Goater  * - R7: The value of Event Queue Offset Counter per XIVE spec
117323bcd5ebSCédric Le Goater  *       if "Debug" = 1, else 0
117423bcd5ebSCédric Le Goater  *
117523bcd5ebSCédric Le Goater  */
117623bcd5ebSCédric Le Goater 
117723bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_DEBUG     PPC_BIT(63)
117823bcd5ebSCédric Le Goater 
117923bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
1180ce2918cbSDavid Gibson                                            SpaprMachineState *spapr,
118123bcd5ebSCédric Le Goater                                            target_ulong opcode,
118223bcd5ebSCédric Le Goater                                            target_ulong *args)
118323bcd5ebSCédric Le Goater {
1184ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
118523bcd5ebSCédric Le Goater     target_ulong flags = args[0];
118623bcd5ebSCédric Le Goater     target_ulong target = args[1];
118723bcd5ebSCédric Le Goater     target_ulong priority = args[2];
118823bcd5ebSCédric Le Goater     XiveEND *end;
118923bcd5ebSCédric Le Goater     uint8_t end_blk;
119023bcd5ebSCédric Le Goater     uint32_t end_idx;
119123bcd5ebSCédric Le Goater 
119223bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
119323bcd5ebSCédric Le Goater         return H_FUNCTION;
119423bcd5ebSCédric Le Goater     }
119523bcd5ebSCédric Le Goater 
119623bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_DEBUG) {
119723bcd5ebSCédric Le Goater         return H_PARAMETER;
119823bcd5ebSCédric Le Goater     }
119923bcd5ebSCédric Le Goater 
120023bcd5ebSCédric Le Goater     /*
120123bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
120223bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
120323bcd5ebSCédric Le Goater      */
120423bcd5ebSCédric Le Goater 
120523bcd5ebSCédric Le Goater     if (spapr_xive_priority_is_reserved(priority)) {
120623bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
120723bcd5ebSCédric Le Goater                       " is reserved\n", priority);
120823bcd5ebSCédric Le Goater         return H_P3;
120923bcd5ebSCédric Le Goater     }
121023bcd5ebSCédric Le Goater 
121123bcd5ebSCédric Le Goater     /*
121223bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
121323bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
121423bcd5ebSCédric Le Goater      * target.
121523bcd5ebSCédric Le Goater      */
121623bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
121723bcd5ebSCédric Le Goater         return H_P2;
121823bcd5ebSCédric Le Goater     }
121923bcd5ebSCédric Le Goater 
122023bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
122123bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
122223bcd5ebSCédric Le Goater 
122323bcd5ebSCédric Le Goater     args[0] = 0;
122423bcd5ebSCédric Le Goater     if (xive_end_is_notify(end)) {
122523bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_END_ALWAYS_NOTIFY;
122623bcd5ebSCédric Le Goater     }
122723bcd5ebSCédric Le Goater 
122823bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
122913df9324SCédric Le Goater         args[1] = xive_end_qaddr(end);
123023bcd5ebSCédric Le Goater         args[2] = 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         args[2] = 0;
123423bcd5ebSCédric Le Goater     }
123523bcd5ebSCédric Le Goater 
12360c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
12370c575703SCédric Le Goater         Error *local_err = NULL;
12380c575703SCédric Le Goater 
12390c575703SCédric Le Goater         kvmppc_xive_get_queue_config(xive, end_blk, end_idx, end, &local_err);
12400c575703SCédric Le Goater         if (local_err) {
12410c575703SCédric Le Goater             error_report_err(local_err);
12420c575703SCédric Le Goater             return H_HARDWARE;
12430c575703SCédric Le Goater         }
12440c575703SCédric Le Goater     }
12450c575703SCédric Le Goater 
124623bcd5ebSCédric Le Goater     /* TODO: do we need any locking on the END ? */
124723bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_DEBUG) {
124823bcd5ebSCédric Le Goater         /* Load the event queue generation number into the return flags */
124923bcd5ebSCédric Le Goater         args[0] |= (uint64_t)xive_get_field32(END_W1_GENERATION, end->w1) << 62;
125023bcd5ebSCédric Le Goater 
125123bcd5ebSCédric Le Goater         /* Load R7 with the event queue offset counter */
125223bcd5ebSCédric Le Goater         args[3] = xive_get_field32(END_W1_PAGE_OFF, end->w1);
125323bcd5ebSCédric Le Goater     } else {
125423bcd5ebSCédric Le Goater         args[3] = 0;
125523bcd5ebSCédric Le Goater     }
125623bcd5ebSCédric Le Goater 
125723bcd5ebSCédric Le Goater     return H_SUCCESS;
125823bcd5ebSCédric Le Goater }
125923bcd5ebSCédric Le Goater 
126023bcd5ebSCédric Le Goater /*
126123bcd5ebSCédric Le Goater  * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
126223bcd5ebSCédric Le Goater  * reporting cache line pair for the calling thread.  The reporting
126323bcd5ebSCédric Le Goater  * cache lines will contain the OS interrupt context when the OS
126423bcd5ebSCédric Le Goater  * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
126523bcd5ebSCédric Le Goater  * interrupt. The reporting cache lines can be reset by inputting -1
126623bcd5ebSCédric Le Goater  * in "reportingLine".  Issuing the CI store byte without reporting
126723bcd5ebSCédric Le Goater  * cache lines registered will result in the data not being accessible
126823bcd5ebSCédric Le Goater  * to the OS.
126923bcd5ebSCédric Le Goater  *
127023bcd5ebSCédric Le Goater  * Parameters:
127123bcd5ebSCédric Le Goater  * Input:
127223bcd5ebSCédric Le Goater  * - R4: "flags"
127323bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
127423bcd5ebSCédric Le Goater  * - R5: "reportingLine": The logical real address of the reporting cache
127523bcd5ebSCédric Le Goater  *       line pair
127623bcd5ebSCédric Le Goater  *
127723bcd5ebSCédric Le Goater  * Output:
127823bcd5ebSCédric Le Goater  * - None
127923bcd5ebSCédric Le Goater  */
128023bcd5ebSCédric Le Goater static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
1281ce2918cbSDavid Gibson                                                 SpaprMachineState *spapr,
128223bcd5ebSCédric Le Goater                                                 target_ulong opcode,
128323bcd5ebSCédric Le Goater                                                 target_ulong *args)
128423bcd5ebSCédric Le Goater {
128523bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
128623bcd5ebSCédric Le Goater         return H_FUNCTION;
128723bcd5ebSCédric Le Goater     }
128823bcd5ebSCédric Le Goater 
128923bcd5ebSCédric Le Goater     /*
129023bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
129123bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
129223bcd5ebSCédric Le Goater      */
129323bcd5ebSCédric Le Goater 
129423bcd5ebSCédric Le Goater     /* TODO: H_INT_SET_OS_REPORTING_LINE */
129523bcd5ebSCédric Le Goater     return H_FUNCTION;
129623bcd5ebSCédric Le Goater }
129723bcd5ebSCédric Le Goater 
129823bcd5ebSCédric Le Goater /*
129923bcd5ebSCédric Le Goater  * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
130023bcd5ebSCédric Le Goater  * real address of the reporting cache line pair set for the input
130123bcd5ebSCédric Le Goater  * "target".  If no reporting cache line pair has been set, -1 is
130223bcd5ebSCédric Le Goater  * returned.
130323bcd5ebSCédric Le Goater  *
130423bcd5ebSCédric Le Goater  * Parameters:
130523bcd5ebSCédric Le Goater  * Input:
130623bcd5ebSCédric Le Goater  * - R4: "flags"
130723bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
130823bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
130923bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
131023bcd5ebSCédric Le Goater  * - R6: "reportingLine": The logical real address of the reporting
131123bcd5ebSCédric Le Goater  *        cache line pair
131223bcd5ebSCédric Le Goater  *
131323bcd5ebSCédric Le Goater  * Output:
131423bcd5ebSCédric Le Goater  * - R4: The logical real address of the reporting line if set, else -1
131523bcd5ebSCédric Le Goater  */
131623bcd5ebSCédric Le Goater static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
1317ce2918cbSDavid Gibson                                                 SpaprMachineState *spapr,
131823bcd5ebSCédric Le Goater                                                 target_ulong opcode,
131923bcd5ebSCédric Le Goater                                                 target_ulong *args)
132023bcd5ebSCédric Le Goater {
132123bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
132223bcd5ebSCédric Le Goater         return H_FUNCTION;
132323bcd5ebSCédric Le Goater     }
132423bcd5ebSCédric Le Goater 
132523bcd5ebSCédric Le Goater     /*
132623bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
132723bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
132823bcd5ebSCédric Le Goater      */
132923bcd5ebSCédric Le Goater 
133023bcd5ebSCédric Le Goater     /* TODO: H_INT_GET_OS_REPORTING_LINE */
133123bcd5ebSCédric Le Goater     return H_FUNCTION;
133223bcd5ebSCédric Le Goater }
133323bcd5ebSCédric Le Goater 
133423bcd5ebSCédric Le Goater /*
133523bcd5ebSCédric Le Goater  * The H_INT_ESB hcall() is used to issue a load or store to the ESB
133623bcd5ebSCédric Le Goater  * page for the input "lisn".  This hcall is only supported for LISNs
133723bcd5ebSCédric Le Goater  * that have the ESB hcall flag set to 1 when returned from hcall()
133823bcd5ebSCédric Le Goater  * H_INT_GET_SOURCE_INFO.
133923bcd5ebSCédric Le Goater  *
134023bcd5ebSCédric Le Goater  * Parameters:
134123bcd5ebSCédric Le Goater  * Input:
134223bcd5ebSCédric Le Goater  * - R4: "flags"
134323bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
134423bcd5ebSCédric Le Goater  *         bit 63: Store: Store=1, store operation, else load operation
134523bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
134623bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
134723bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
134823bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
134923bcd5ebSCédric Le Goater  * - R6: "esbOffset" is the offset into the ESB page for the load or
135023bcd5ebSCédric Le Goater  *       store operation
135123bcd5ebSCédric Le Goater  * - R7: "storeData" is the data to write for a store operation
135223bcd5ebSCédric Le Goater  *
135323bcd5ebSCédric Le Goater  * Output:
135423bcd5ebSCédric Le Goater  * - R4: The value of the load if load operation, else -1
135523bcd5ebSCédric Le Goater  */
135623bcd5ebSCédric Le Goater 
135723bcd5ebSCédric Le Goater #define SPAPR_XIVE_ESB_STORE PPC_BIT(63)
135823bcd5ebSCédric Le Goater 
135923bcd5ebSCédric Le Goater static target_ulong h_int_esb(PowerPCCPU *cpu,
1360ce2918cbSDavid Gibson                               SpaprMachineState *spapr,
136123bcd5ebSCédric Le Goater                               target_ulong opcode,
136223bcd5ebSCédric Le Goater                               target_ulong *args)
136323bcd5ebSCédric Le Goater {
1364ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
136523bcd5ebSCédric Le Goater     XiveEAS eas;
136623bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
136723bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
136823bcd5ebSCédric Le Goater     target_ulong offset = args[2];
136923bcd5ebSCédric Le Goater     target_ulong data   = args[3];
137023bcd5ebSCédric Le Goater     hwaddr mmio_addr;
137123bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
137223bcd5ebSCédric Le Goater 
137323bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
137423bcd5ebSCédric Le Goater         return H_FUNCTION;
137523bcd5ebSCédric Le Goater     }
137623bcd5ebSCédric Le Goater 
137723bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_ESB_STORE) {
137823bcd5ebSCédric Le Goater         return H_PARAMETER;
137923bcd5ebSCédric Le Goater     }
138023bcd5ebSCédric Le Goater 
138123bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
138223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
138323bcd5ebSCédric Le Goater                       lisn);
138423bcd5ebSCédric Le Goater         return H_P2;
138523bcd5ebSCédric Le Goater     }
138623bcd5ebSCédric Le Goater 
138723bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
138823bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
138923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
139023bcd5ebSCédric Le Goater                       lisn);
139123bcd5ebSCédric Le Goater         return H_P2;
139223bcd5ebSCédric Le Goater     }
139323bcd5ebSCédric Le Goater 
139423bcd5ebSCédric Le Goater     if (offset > (1ull << xsrc->esb_shift)) {
139523bcd5ebSCédric Le Goater         return H_P3;
139623bcd5ebSCédric Le Goater     }
139723bcd5ebSCédric Le Goater 
13980c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
13990c575703SCédric Le Goater         args[0] = kvmppc_xive_esb_rw(xsrc, lisn, offset, data,
14000c575703SCédric Le Goater                                      flags & SPAPR_XIVE_ESB_STORE);
14010c575703SCédric Le Goater     } else {
140223bcd5ebSCédric Le Goater         mmio_addr = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn) + offset;
140323bcd5ebSCédric Le Goater 
140423bcd5ebSCédric Le Goater         if (dma_memory_rw(&address_space_memory, mmio_addr, &data, 8,
140523bcd5ebSCédric Le Goater                           (flags & SPAPR_XIVE_ESB_STORE))) {
140623bcd5ebSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to access ESB @0x%"
140723bcd5ebSCédric Le Goater                           HWADDR_PRIx "\n", mmio_addr);
140823bcd5ebSCédric Le Goater             return H_HARDWARE;
140923bcd5ebSCédric Le Goater         }
141023bcd5ebSCédric Le Goater         args[0] = (flags & SPAPR_XIVE_ESB_STORE) ? -1 : data;
14110c575703SCédric Le Goater     }
141223bcd5ebSCédric Le Goater     return H_SUCCESS;
141323bcd5ebSCédric Le Goater }
141423bcd5ebSCédric Le Goater 
141523bcd5ebSCédric Le Goater /*
141623bcd5ebSCédric Le Goater  * The H_INT_SYNC hcall() is used to issue hardware syncs that will
141723bcd5ebSCédric Le Goater  * ensure any in flight events for the input lisn are in the event
141823bcd5ebSCédric Le Goater  * queue.
141923bcd5ebSCédric Le Goater  *
142023bcd5ebSCédric Le Goater  * Parameters:
142123bcd5ebSCédric Le Goater  * Input:
142223bcd5ebSCédric Le Goater  * - R4: "flags"
142323bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
142423bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
142523bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
142623bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
142723bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
142823bcd5ebSCédric Le Goater  *
142923bcd5ebSCédric Le Goater  * Output:
143023bcd5ebSCédric Le Goater  * - None
143123bcd5ebSCédric Le Goater  */
143223bcd5ebSCédric Le Goater static target_ulong h_int_sync(PowerPCCPU *cpu,
1433ce2918cbSDavid Gibson                                SpaprMachineState *spapr,
143423bcd5ebSCédric Le Goater                                target_ulong opcode,
143523bcd5ebSCédric Le Goater                                target_ulong *args)
143623bcd5ebSCédric Le Goater {
1437ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
143823bcd5ebSCédric Le Goater     XiveEAS eas;
143923bcd5ebSCédric Le Goater     target_ulong flags = args[0];
144023bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
144123bcd5ebSCédric Le Goater 
144223bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
144323bcd5ebSCédric Le Goater         return H_FUNCTION;
144423bcd5ebSCédric Le Goater     }
144523bcd5ebSCédric Le Goater 
144623bcd5ebSCédric Le Goater     if (flags) {
144723bcd5ebSCédric Le Goater         return H_PARAMETER;
144823bcd5ebSCédric Le Goater     }
144923bcd5ebSCédric Le Goater 
145023bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
145123bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
145223bcd5ebSCédric Le Goater                       lisn);
145323bcd5ebSCédric Le Goater         return H_P2;
145423bcd5ebSCédric Le Goater     }
145523bcd5ebSCédric Le Goater 
145623bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
145723bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
145823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
145923bcd5ebSCédric Le Goater                       lisn);
146023bcd5ebSCédric Le Goater         return H_P2;
146123bcd5ebSCédric Le Goater     }
146223bcd5ebSCédric Le Goater 
146323bcd5ebSCédric Le Goater     /*
146423bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
146523bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
146623bcd5ebSCédric Le Goater      */
146723bcd5ebSCédric Le Goater 
14680c575703SCédric Le Goater     /*
14690c575703SCédric Le Goater      * This is not real hardware. Nothing to be done unless when
14700c575703SCédric Le Goater      * under KVM
14710c575703SCédric Le Goater      */
14720c575703SCédric Le Goater 
14730c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
14740c575703SCédric Le Goater         Error *local_err = NULL;
14750c575703SCédric Le Goater 
14760c575703SCédric Le Goater         kvmppc_xive_sync_source(xive, lisn, &local_err);
14770c575703SCédric Le Goater         if (local_err) {
14780c575703SCédric Le Goater             error_report_err(local_err);
14790c575703SCédric Le Goater             return H_HARDWARE;
14800c575703SCédric Le Goater         }
14810c575703SCédric Le Goater     }
148223bcd5ebSCédric Le Goater     return H_SUCCESS;
148323bcd5ebSCédric Le Goater }
148423bcd5ebSCédric Le Goater 
148523bcd5ebSCédric Le Goater /*
148623bcd5ebSCédric Le Goater  * The H_INT_RESET hcall() is used to reset all of the partition's
148723bcd5ebSCédric Le Goater  * interrupt exploitation structures to their initial state.  This
148823bcd5ebSCédric Le Goater  * means losing all previously set interrupt state set via
148923bcd5ebSCédric Le Goater  * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
149023bcd5ebSCédric Le Goater  *
149123bcd5ebSCédric Le Goater  * Parameters:
149223bcd5ebSCédric Le Goater  * Input:
149323bcd5ebSCédric Le Goater  * - R4: "flags"
149423bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
149523bcd5ebSCédric Le Goater  *
149623bcd5ebSCédric Le Goater  * Output:
149723bcd5ebSCédric Le Goater  * - None
149823bcd5ebSCédric Le Goater  */
149923bcd5ebSCédric Le Goater static target_ulong h_int_reset(PowerPCCPU *cpu,
1500ce2918cbSDavid Gibson                                 SpaprMachineState *spapr,
150123bcd5ebSCédric Le Goater                                 target_ulong opcode,
150223bcd5ebSCédric Le Goater                                 target_ulong *args)
150323bcd5ebSCédric Le Goater {
1504ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
150523bcd5ebSCédric Le Goater     target_ulong flags   = args[0];
150623bcd5ebSCédric Le Goater 
150723bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
150823bcd5ebSCédric Le Goater         return H_FUNCTION;
150923bcd5ebSCédric Le Goater     }
151023bcd5ebSCédric Le Goater 
151123bcd5ebSCédric Le Goater     if (flags) {
151223bcd5ebSCédric Le Goater         return H_PARAMETER;
151323bcd5ebSCédric Le Goater     }
151423bcd5ebSCédric Le Goater 
151523bcd5ebSCédric Le Goater     device_reset(DEVICE(xive));
15160c575703SCédric Le Goater 
15170c575703SCédric Le Goater     if (kvm_irqchip_in_kernel()) {
15180c575703SCédric Le Goater         Error *local_err = NULL;
15190c575703SCédric Le Goater 
15200c575703SCédric Le Goater         kvmppc_xive_reset(xive, &local_err);
15210c575703SCédric Le Goater         if (local_err) {
15220c575703SCédric Le Goater             error_report_err(local_err);
15230c575703SCédric Le Goater             return H_HARDWARE;
15240c575703SCédric Le Goater         }
15250c575703SCédric Le Goater     }
152623bcd5ebSCédric Le Goater     return H_SUCCESS;
152723bcd5ebSCédric Le Goater }
152823bcd5ebSCédric Le Goater 
1529ce2918cbSDavid Gibson void spapr_xive_hcall_init(SpaprMachineState *spapr)
153023bcd5ebSCédric Le Goater {
153123bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
153223bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
153323bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
153423bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
153523bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
153623bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
153723bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
153823bcd5ebSCédric Le Goater                              h_int_set_os_reporting_line);
153923bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
154023bcd5ebSCédric Le Goater                              h_int_get_os_reporting_line);
154123bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_ESB, h_int_esb);
154223bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SYNC, h_int_sync);
154323bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_RESET, h_int_reset);
154423bcd5ebSCédric Le Goater }
15456e21de4aSCédric Le Goater 
1546ce2918cbSDavid Gibson void spapr_dt_xive(SpaprMachineState *spapr, uint32_t nr_servers, void *fdt,
15476e21de4aSCédric Le Goater                    uint32_t phandle)
15486e21de4aSCédric Le Goater {
1549ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
15506e21de4aSCédric Le Goater     int node;
15516e21de4aSCédric Le Goater     uint64_t timas[2 * 2];
15526e21de4aSCédric Le Goater     /* Interrupt number ranges for the IPIs */
15536e21de4aSCédric Le Goater     uint32_t lisn_ranges[] = {
15546e21de4aSCédric Le Goater         cpu_to_be32(0),
15556e21de4aSCédric Le Goater         cpu_to_be32(nr_servers),
15566e21de4aSCédric Le Goater     };
15576e21de4aSCédric Le Goater     /*
15586e21de4aSCédric Le Goater      * EQ size - the sizes of pages supported by the system 4K, 64K,
15596e21de4aSCédric Le Goater      * 2M, 16M. We only advertise 64K for the moment.
15606e21de4aSCédric Le Goater      */
15616e21de4aSCédric Le Goater     uint32_t eq_sizes[] = {
15626e21de4aSCédric Le Goater         cpu_to_be32(16), /* 64K */
15636e21de4aSCédric Le Goater     };
15646e21de4aSCédric Le Goater     /*
15656e21de4aSCédric Le Goater      * The following array is in sync with the reserved priorities
15666e21de4aSCédric Le Goater      * defined by the 'spapr_xive_priority_is_reserved' routine.
15676e21de4aSCédric Le Goater      */
15686e21de4aSCédric Le Goater     uint32_t plat_res_int_priorities[] = {
15696e21de4aSCédric Le Goater         cpu_to_be32(7),    /* start */
15706e21de4aSCédric Le Goater         cpu_to_be32(0xf8), /* count */
15716e21de4aSCédric Le Goater     };
15726e21de4aSCédric Le Goater 
15736e21de4aSCédric Le Goater     /* Thread Interrupt Management Area : User (ring 3) and OS (ring 2) */
15746e21de4aSCédric Le Goater     timas[0] = cpu_to_be64(xive->tm_base +
15756e21de4aSCédric Le Goater                            XIVE_TM_USER_PAGE * (1ull << TM_SHIFT));
15766e21de4aSCédric Le Goater     timas[1] = cpu_to_be64(1ull << TM_SHIFT);
15776e21de4aSCédric Le Goater     timas[2] = cpu_to_be64(xive->tm_base +
15786e21de4aSCédric Le Goater                            XIVE_TM_OS_PAGE * (1ull << TM_SHIFT));
15796e21de4aSCédric Le Goater     timas[3] = cpu_to_be64(1ull << TM_SHIFT);
15806e21de4aSCédric Le Goater 
1581743ed566SGreg Kurz     _FDT(node = fdt_add_subnode(fdt, 0, xive->nodename));
15826e21de4aSCédric Le Goater 
15836e21de4aSCédric Le Goater     _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
15846e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
15856e21de4aSCédric Le Goater 
15866e21de4aSCédric Le Goater     _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
15876e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
15886e21de4aSCédric Le Goater                      sizeof(eq_sizes)));
15896e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
15906e21de4aSCédric Le Goater                      sizeof(lisn_ranges)));
15916e21de4aSCédric Le Goater 
15926e21de4aSCédric Le Goater     /* For Linux to link the LSIs to the interrupt controller. */
15936e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, node, "interrupt-controller", NULL, 0));
15946e21de4aSCédric Le Goater     _FDT(fdt_setprop_cell(fdt, node, "#interrupt-cells", 2));
15956e21de4aSCédric Le Goater 
15966e21de4aSCédric Le Goater     /* For SLOF */
15976e21de4aSCédric Le Goater     _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
15986e21de4aSCédric Le Goater     _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
15996e21de4aSCédric Le Goater 
16006e21de4aSCédric Le Goater     /*
16016e21de4aSCédric Le Goater      * The "ibm,plat-res-int-priorities" property defines the priority
16026e21de4aSCédric Le Goater      * ranges reserved by the hypervisor
16036e21de4aSCédric Le Goater      */
16046e21de4aSCédric Le Goater     _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
16056e21de4aSCédric Le Goater                      plat_res_int_priorities, sizeof(plat_res_int_priorities)));
16066e21de4aSCédric Le Goater }
1607