xref: /qemu/hw/intc/spapr_xive.c (revision 950f1273abed699227d0fde2c7436a6fbc16fd53)
13aa597f6SCédric Le Goater /*
23aa597f6SCédric Le Goater  * QEMU PowerPC sPAPR XIVE interrupt controller model
33aa597f6SCédric Le Goater  *
43aa597f6SCédric Le Goater  * Copyright (c) 2017-2018, IBM Corporation.
53aa597f6SCédric Le Goater  *
63aa597f6SCédric Le Goater  * This code is licensed under the GPL version 2 or later. See the
73aa597f6SCédric Le Goater  * COPYING file in the top-level directory.
83aa597f6SCédric Le Goater  */
93aa597f6SCédric Le Goater 
103aa597f6SCédric Le Goater #include "qemu/osdep.h"
113aa597f6SCédric Le Goater #include "qemu/log.h"
120b8fa32fSMarkus Armbruster #include "qemu/module.h"
133aa597f6SCédric Le Goater #include "qapi/error.h"
14f163e270SPhilippe Mathieu-Daudé #include "qapi/type-helpers.h"
153aa597f6SCédric Le Goater #include "qemu/error-report.h"
163aa597f6SCédric Le Goater #include "target/ppc/cpu.h"
173aa597f6SCédric Le Goater #include "sysemu/cpus.h"
1871e8a915SMarkus Armbruster #include "sysemu/reset.h"
19d6454270SMarkus Armbruster #include "migration/vmstate.h"
203aa597f6SCédric Le Goater #include "monitor/monitor.h"
216e21de4aSCédric Le Goater #include "hw/ppc/fdt.h"
223aa597f6SCédric Le Goater #include "hw/ppc/spapr.h"
23a28b9a5aSCédric Le Goater #include "hw/ppc/spapr_cpu_core.h"
243aa597f6SCédric Le Goater #include "hw/ppc/spapr_xive.h"
253aa597f6SCédric Le Goater #include "hw/ppc/xive.h"
263aa597f6SCédric Le Goater #include "hw/ppc/xive_regs.h"
27a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
284e960974SCédric Le Goater #include "trace.h"
293aa597f6SCédric Le Goater 
303aa597f6SCédric Le Goater /*
319b4b4e51SMichael Tokarev  * XIVE Virtualization Controller BAR and Thread Management BAR that we
323aa597f6SCédric Le Goater  * use for the ESB pages and the TIMA pages
333aa597f6SCédric Le Goater  */
343aa597f6SCédric Le Goater #define SPAPR_XIVE_VC_BASE   0x0006010000000000ull
353aa597f6SCédric Le Goater #define SPAPR_XIVE_TM_BASE   0x0006030203180000ull
363aa597f6SCédric Le Goater 
373aa597f6SCédric Le Goater /*
380cddee8dSCédric Le Goater  * The allocation of VP blocks is a complex operation in OPAL and the
390cddee8dSCédric Le Goater  * VP identifiers have a relation with the number of HW chips, the
400cddee8dSCédric Le Goater  * size of the VP blocks, VP grouping, etc. The QEMU sPAPR XIVE
410cddee8dSCédric Le Goater  * controller model does not have the same constraints and can use a
420cddee8dSCédric Le Goater  * simple mapping scheme of the CPU vcpu_id
430cddee8dSCédric Le Goater  *
440cddee8dSCédric Le Goater  * These identifiers are never returned to the OS.
450cddee8dSCédric Le Goater  */
460cddee8dSCédric Le Goater 
470cddee8dSCédric Le Goater #define SPAPR_XIVE_NVT_BASE 0x400
480cddee8dSCédric Le Goater 
490cddee8dSCédric Le Goater /*
500cddee8dSCédric Le Goater  * sPAPR NVT and END indexing helpers
510cddee8dSCédric Le Goater  */
520cddee8dSCédric Le Goater static uint32_t spapr_xive_nvt_to_target(uint8_t nvt_blk, uint32_t nvt_idx)
530cddee8dSCédric Le Goater {
540cddee8dSCédric Le Goater     return nvt_idx - SPAPR_XIVE_NVT_BASE;
550cddee8dSCédric Le Goater }
560cddee8dSCédric Le Goater 
5723bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_nvt(PowerPCCPU *cpu,
5823bcd5ebSCédric Le Goater                                   uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
5923bcd5ebSCédric Le Goater {
6023bcd5ebSCédric Le Goater     assert(cpu);
6123bcd5ebSCédric Le Goater 
6223bcd5ebSCédric Le Goater     if (out_nvt_blk) {
6323bcd5ebSCédric Le Goater         *out_nvt_blk = SPAPR_XIVE_BLOCK_ID;
6423bcd5ebSCédric Le Goater     }
6523bcd5ebSCédric Le Goater 
6623bcd5ebSCédric Le Goater     if (out_nvt_blk) {
6723bcd5ebSCédric Le Goater         *out_nvt_idx = SPAPR_XIVE_NVT_BASE + cpu->vcpu_id;
6823bcd5ebSCédric Le Goater     }
6923bcd5ebSCédric Le Goater }
7023bcd5ebSCédric Le Goater 
7123bcd5ebSCédric Le Goater static int spapr_xive_target_to_nvt(uint32_t target,
7223bcd5ebSCédric Le Goater                                     uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
7323bcd5ebSCédric Le Goater {
7423bcd5ebSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(target);
7523bcd5ebSCédric Le Goater 
7623bcd5ebSCédric Le Goater     if (!cpu) {
7723bcd5ebSCédric Le Goater         return -1;
7823bcd5ebSCédric Le Goater     }
7923bcd5ebSCédric Le Goater 
8023bcd5ebSCédric Le Goater     spapr_xive_cpu_to_nvt(cpu, out_nvt_blk, out_nvt_idx);
8123bcd5ebSCédric Le Goater     return 0;
8223bcd5ebSCédric Le Goater }
8323bcd5ebSCédric Le Goater 
8423bcd5ebSCédric Le Goater /*
8523bcd5ebSCédric Le Goater  * sPAPR END indexing uses a simple mapping of the CPU vcpu_id, 8
8623bcd5ebSCédric Le Goater  * priorities per CPU
8723bcd5ebSCédric Le Goater  */
880c575703SCédric Le Goater int spapr_xive_end_to_target(uint8_t end_blk, uint32_t end_idx,
890c575703SCédric Le Goater                              uint32_t *out_server, uint8_t *out_prio)
900c575703SCédric Le Goater {
910c575703SCédric Le Goater 
920c575703SCédric Le Goater     assert(end_blk == SPAPR_XIVE_BLOCK_ID);
930c575703SCédric Le Goater 
940c575703SCédric Le Goater     if (out_server) {
950c575703SCédric Le Goater         *out_server = end_idx >> 3;
960c575703SCédric Le Goater     }
970c575703SCédric Le Goater 
980c575703SCédric Le Goater     if (out_prio) {
990c575703SCédric Le Goater         *out_prio = end_idx & 0x7;
1000c575703SCédric Le Goater     }
1010c575703SCédric Le Goater     return 0;
1020c575703SCédric Le Goater }
1030c575703SCédric Le Goater 
10423bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_end(PowerPCCPU *cpu, uint8_t prio,
10523bcd5ebSCédric Le Goater                                   uint8_t *out_end_blk, uint32_t *out_end_idx)
10623bcd5ebSCédric Le Goater {
10723bcd5ebSCédric Le Goater     assert(cpu);
10823bcd5ebSCédric Le Goater 
10923bcd5ebSCédric Le Goater     if (out_end_blk) {
11023bcd5ebSCédric Le Goater         *out_end_blk = SPAPR_XIVE_BLOCK_ID;
11123bcd5ebSCédric Le Goater     }
11223bcd5ebSCédric Le Goater 
11323bcd5ebSCédric Le Goater     if (out_end_idx) {
11423bcd5ebSCédric Le Goater         *out_end_idx = (cpu->vcpu_id << 3) + prio;
11523bcd5ebSCédric Le Goater     }
11623bcd5ebSCédric Le Goater }
11723bcd5ebSCédric Le Goater 
11823bcd5ebSCédric Le Goater static int spapr_xive_target_to_end(uint32_t target, uint8_t prio,
11923bcd5ebSCédric Le Goater                                     uint8_t *out_end_blk, uint32_t *out_end_idx)
12023bcd5ebSCédric Le Goater {
12123bcd5ebSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(target);
12223bcd5ebSCédric Le Goater 
12323bcd5ebSCédric Le Goater     if (!cpu) {
12423bcd5ebSCédric Le Goater         return -1;
12523bcd5ebSCédric Le Goater     }
12623bcd5ebSCédric Le Goater 
12723bcd5ebSCédric Le Goater     spapr_xive_cpu_to_end(cpu, prio, out_end_blk, out_end_idx);
12823bcd5ebSCédric Le Goater     return 0;
12923bcd5ebSCédric Le Goater }
13023bcd5ebSCédric Le Goater 
1310cddee8dSCédric Le Goater /*
1323aa597f6SCédric Le Goater  * On sPAPR machines, use a simplified output for the XIVE END
1333aa597f6SCédric Le Goater  * structure dumping only the information related to the OS EQ.
1343aa597f6SCédric Le Goater  */
135ce2918cbSDavid Gibson static void spapr_xive_end_pic_print_info(SpaprXive *xive, XiveEND *end,
136*950f1273SPhilippe Mathieu-Daudé                                           GString *buf)
1373aa597f6SCédric Le Goater {
138fb2e8b51SCédric Le Goater     uint64_t qaddr_base = xive_end_qaddr(end);
1393aa597f6SCédric Le Goater     uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
1403aa597f6SCédric Le Goater     uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
1413aa597f6SCédric Le Goater     uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
1423aa597f6SCédric Le Goater     uint32_t qentries = 1 << (qsize + 10);
1433aa597f6SCédric Le Goater     uint32_t nvt = xive_get_field32(END_W6_NVT_INDEX, end->w6);
1443aa597f6SCédric Le Goater     uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
1453aa597f6SCédric Le Goater 
146*950f1273SPhilippe Mathieu-Daudé     g_string_append_printf(buf, "%3d/%d % 6d/%5d @%"PRIx64" ^%d",
1470cddee8dSCédric Le Goater                            spapr_xive_nvt_to_target(0, nvt),
148fb2e8b51SCédric Le Goater                            priority, qindex, qentries, qaddr_base, qgen);
1493aa597f6SCédric Le Goater 
150ace6fcdeSPhilippe Mathieu-Daudé     xive_end_queue_pic_print_info(end, 6, buf);
1513aa597f6SCédric Le Goater }
1523aa597f6SCédric Le Goater 
153e519cdd9SGreg Kurz /*
154e519cdd9SGreg Kurz  * kvm_irqchip_in_kernel() will cause the compiler to turn this
155e519cdd9SGreg Kurz  * info a nop if CONFIG_KVM isn't defined.
156e519cdd9SGreg Kurz  */
157e519cdd9SGreg Kurz #define spapr_xive_in_kernel(xive) \
158e519cdd9SGreg Kurz     (kvm_irqchip_in_kernel() && (xive)->fd != -1)
159e519cdd9SGreg Kurz 
160ab9c93c2SCédric Le Goater static void spapr_xive_pic_print_info(SpaprXive *xive, Monitor *mon)
1613aa597f6SCédric Le Goater {
1623aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
1633aa597f6SCédric Le Goater     int i;
1643aa597f6SCédric Le Goater 
165e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
1667bfc759cSCédric Le Goater         Error *local_err = NULL;
1677bfc759cSCédric Le Goater 
1687bfc759cSCédric Le Goater         kvmppc_xive_synchronize_state(xive, &local_err);
1697bfc759cSCédric Le Goater         if (local_err) {
1707bfc759cSCédric Le Goater             error_report_err(local_err);
1717bfc759cSCédric Le Goater             return;
1727bfc759cSCédric Le Goater         }
1737bfc759cSCédric Le Goater     }
1747bfc759cSCédric Le Goater 
175f81d69fcSSatheesh Rajendran     monitor_printf(mon, "  LISN         PQ    EISN     CPU/PRIO EQ\n");
1763aa597f6SCédric Le Goater 
1773aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
1783aa597f6SCédric Le Goater         uint8_t pq = xive_source_esb_get(xsrc, i);
1793aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
1803aa597f6SCédric Le Goater 
1813aa597f6SCédric Le Goater         if (!xive_eas_is_valid(eas)) {
1823aa597f6SCédric Le Goater             continue;
1833aa597f6SCédric Le Goater         }
1843aa597f6SCédric Le Goater 
1853aa597f6SCédric Le Goater         monitor_printf(mon, "  %08x %s %c%c%c %s %08x ", i,
1863aa597f6SCédric Le Goater                        xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
1873aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
1883aa597f6SCédric Le Goater                        pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
189621f70d2SCédric Le Goater                        xive_source_is_asserted(xsrc, i) ? 'A' : ' ',
1903aa597f6SCédric Le Goater                        xive_eas_is_masked(eas) ? "M" : " ",
1913aa597f6SCédric Le Goater                        (int) xive_get_field64(EAS_END_DATA, eas->w));
1923aa597f6SCédric Le Goater 
1933aa597f6SCédric Le Goater         if (!xive_eas_is_masked(eas)) {
1943aa597f6SCédric Le Goater             uint32_t end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
1953aa597f6SCédric Le Goater             XiveEND *end;
196*950f1273SPhilippe Mathieu-Daudé             g_autoptr(GString) buf = g_string_new("");
197*950f1273SPhilippe Mathieu-Daudé             g_autoptr(HumanReadableText) info = NULL;
1983aa597f6SCédric Le Goater 
1993aa597f6SCédric Le Goater             assert(end_idx < xive->nr_ends);
2003aa597f6SCédric Le Goater             end = &xive->endt[end_idx];
2013aa597f6SCédric Le Goater 
2023aa597f6SCédric Le Goater             if (xive_end_is_valid(end)) {
203*950f1273SPhilippe Mathieu-Daudé                 spapr_xive_end_pic_print_info(xive, end, buf);
2043aa597f6SCédric Le Goater             }
205*950f1273SPhilippe Mathieu-Daudé 
206*950f1273SPhilippe Mathieu-Daudé             info = human_readable_text_from_str(buf);
207*950f1273SPhilippe Mathieu-Daudé             monitor_puts(mon, info->human_readable_text);
2083aa597f6SCédric Le Goater         }
2093aa597f6SCédric Le Goater         monitor_printf(mon, "\n");
2103aa597f6SCédric Le Goater     }
2113aa597f6SCédric Le Goater }
2123aa597f6SCédric Le Goater 
213ce2918cbSDavid Gibson void spapr_xive_mmio_set_enabled(SpaprXive *xive, bool enable)
2143a8eb78eSCédric Le Goater {
2153a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->source.esb_mmio, enable);
2163a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->tm_mmio, enable);
2173a8eb78eSCédric Le Goater 
2183a8eb78eSCédric Le Goater     /* Disable the END ESBs until a guest OS makes use of them */
2193a8eb78eSCédric Le Goater     memory_region_set_enabled(&xive->end_source.esb_mmio, false);
2203a8eb78eSCédric Le Goater }
2213a8eb78eSCédric Le Goater 
222d024a2c1SCédric Le Goater static void spapr_xive_tm_write(void *opaque, hwaddr offset,
223d024a2c1SCédric Le Goater                           uint64_t value, unsigned size)
224d024a2c1SCédric Le Goater {
225d024a2c1SCédric Le Goater     XiveTCTX *tctx = spapr_cpu_state(POWERPC_CPU(current_cpu))->tctx;
226d024a2c1SCédric Le Goater 
227d024a2c1SCédric Le Goater     xive_tctx_tm_write(XIVE_PRESENTER(opaque), tctx, offset, value, size);
228d024a2c1SCédric Le Goater }
229d024a2c1SCédric Le Goater 
230d024a2c1SCédric Le Goater static uint64_t spapr_xive_tm_read(void *opaque, hwaddr offset, unsigned size)
231d024a2c1SCédric Le Goater {
232d024a2c1SCédric Le Goater     XiveTCTX *tctx = spapr_cpu_state(POWERPC_CPU(current_cpu))->tctx;
233d024a2c1SCédric Le Goater 
234d024a2c1SCédric Le Goater     return xive_tctx_tm_read(XIVE_PRESENTER(opaque), tctx, offset, size);
235d024a2c1SCédric Le Goater }
236d024a2c1SCédric Le Goater 
237d024a2c1SCédric Le Goater const MemoryRegionOps spapr_xive_tm_ops = {
238d024a2c1SCédric Le Goater     .read = spapr_xive_tm_read,
239d024a2c1SCédric Le Goater     .write = spapr_xive_tm_write,
240d024a2c1SCédric Le Goater     .endianness = DEVICE_BIG_ENDIAN,
241d024a2c1SCédric Le Goater     .valid = {
242d024a2c1SCédric Le Goater         .min_access_size = 1,
243d024a2c1SCédric Le Goater         .max_access_size = 8,
244d024a2c1SCédric Le Goater     },
245d024a2c1SCédric Le Goater     .impl = {
246d024a2c1SCédric Le Goater         .min_access_size = 1,
247d024a2c1SCédric Le Goater         .max_access_size = 8,
248d024a2c1SCédric Le Goater     },
249d024a2c1SCédric Le Goater };
250d024a2c1SCédric Le Goater 
2513aa597f6SCédric Le Goater static void spapr_xive_end_reset(XiveEND *end)
2523aa597f6SCédric Le Goater {
2533aa597f6SCédric Le Goater     memset(end, 0, sizeof(*end));
2543aa597f6SCédric Le Goater 
2553aa597f6SCédric Le Goater     /* switch off the escalation and notification ESBs */
2563aa597f6SCédric Le Goater     end->w1 = cpu_to_be32(END_W1_ESe_Q | END_W1_ESn_Q);
2573aa597f6SCédric Le Goater }
2583aa597f6SCédric Le Goater 
2593aa597f6SCédric Le Goater static void spapr_xive_reset(void *dev)
2603aa597f6SCédric Le Goater {
261ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(dev);
2623aa597f6SCédric Le Goater     int i;
2633aa597f6SCédric Le Goater 
2643aa597f6SCédric Le Goater     /*
2653aa597f6SCédric Le Goater      * The XiveSource has its own reset handler, which mask off all
2663aa597f6SCédric Le Goater      * IRQs (!P|Q)
2673aa597f6SCédric Le Goater      */
2683aa597f6SCédric Le Goater 
2693aa597f6SCédric Le Goater     /* Mask all valid EASs in the IRQ number space. */
2703aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_irqs; i++) {
2713aa597f6SCédric Le Goater         XiveEAS *eas = &xive->eat[i];
2723aa597f6SCédric Le Goater         if (xive_eas_is_valid(eas)) {
2733aa597f6SCédric Le Goater             eas->w = cpu_to_be64(EAS_VALID | EAS_MASKED);
2743aa597f6SCédric Le Goater         } else {
2753aa597f6SCédric Le Goater             eas->w = 0;
2763aa597f6SCédric Le Goater         }
2773aa597f6SCédric Le Goater     }
2783aa597f6SCédric Le Goater 
2793aa597f6SCédric Le Goater     /* Clear all ENDs */
2803aa597f6SCédric Le Goater     for (i = 0; i < xive->nr_ends; i++) {
2813aa597f6SCédric Le Goater         spapr_xive_end_reset(&xive->endt[i]);
2823aa597f6SCédric Le Goater     }
2833aa597f6SCédric Le Goater }
2843aa597f6SCédric Le Goater 
2853aa597f6SCédric Le Goater static void spapr_xive_instance_init(Object *obj)
2863aa597f6SCédric Le Goater {
287ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(obj);
2883aa597f6SCédric Le Goater 
2899fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "source", &xive->source, TYPE_XIVE_SOURCE);
2903aa597f6SCédric Le Goater 
291f6d4dca8SThomas Huth     object_initialize_child(obj, "end_source", &xive->end_source,
2929fc7fc4dSMarkus Armbruster                             TYPE_XIVE_END_SOURCE);
29338afd772SCédric Le Goater 
29438afd772SCédric Le Goater     /* Not connected to the KVM XIVE device */
29538afd772SCédric Le Goater     xive->fd = -1;
2963aa597f6SCédric Le Goater }
2973aa597f6SCédric Le Goater 
2983aa597f6SCédric Le Goater static void spapr_xive_realize(DeviceState *dev, Error **errp)
2993aa597f6SCédric Le Goater {
300ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(dev);
3016cc64796SGreg Kurz     SpaprXiveClass *sxc = SPAPR_XIVE_GET_CLASS(xive);
3023aa597f6SCédric Le Goater     XiveSource *xsrc = &xive->source;
3033aa597f6SCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
3043aa597f6SCédric Le Goater     Error *local_err = NULL;
3053aa597f6SCédric Le Goater 
306484d774cSGreg Kurz     /* Set by spapr_irq_init() */
307484d774cSGreg Kurz     g_assert(xive->nr_irqs);
308484d774cSGreg Kurz     g_assert(xive->nr_ends);
309484d774cSGreg Kurz 
3106cc64796SGreg Kurz     sxc->parent_realize(dev, &local_err);
3116cc64796SGreg Kurz     if (local_err) {
3126cc64796SGreg Kurz         error_propagate(errp, local_err);
3136cc64796SGreg Kurz         return;
3146cc64796SGreg Kurz     }
3156cc64796SGreg Kurz 
3163aa597f6SCédric Le Goater     /*
3173aa597f6SCédric Le Goater      * Initialize the internal sources, for IPIs and virtual devices.
3183aa597f6SCédric Le Goater      */
3195325cc34SMarkus Armbruster     object_property_set_int(OBJECT(xsrc), "nr-irqs", xive->nr_irqs,
3203aa597f6SCédric Le Goater                             &error_fatal);
3215325cc34SMarkus Armbruster     object_property_set_link(OBJECT(xsrc), "xive", OBJECT(xive), &error_abort);
322668f62ecSMarkus Armbruster     if (!qdev_realize(DEVICE(xsrc), NULL, errp)) {
3233aa597f6SCédric Le Goater         return;
3243aa597f6SCédric Le Goater     }
3253aa597f6SCédric Le Goater 
3263aa597f6SCédric Le Goater     /*
3273aa597f6SCédric Le Goater      * Initialize the END ESB source
3283aa597f6SCédric Le Goater      */
3295325cc34SMarkus Armbruster     object_property_set_int(OBJECT(end_xsrc), "nr-ends", xive->nr_irqs,
3303aa597f6SCédric Le Goater                             &error_fatal);
3315325cc34SMarkus Armbruster     object_property_set_link(OBJECT(end_xsrc), "xive", OBJECT(xive),
3320ab2316eSGreg Kurz                              &error_abort);
333668f62ecSMarkus Armbruster     if (!qdev_realize(DEVICE(end_xsrc), NULL, errp)) {
3343aa597f6SCédric Le Goater         return;
3353aa597f6SCédric Le Goater     }
3363aa597f6SCédric Le Goater 
3373aa597f6SCédric Le Goater     /* Set the mapping address of the END ESB pages after the source ESBs */
3383110f0eeSGreg Kurz     xive->end_base = xive->vc_base + xive_source_esb_len(xsrc);
3393aa597f6SCédric Le Goater 
3403aa597f6SCédric Le Goater     /*
3413aa597f6SCédric Le Goater      * Allocate the routing tables
3423aa597f6SCédric Le Goater      */
3433aa597f6SCédric Le Goater     xive->eat = g_new0(XiveEAS, xive->nr_irqs);
3443aa597f6SCédric Le Goater     xive->endt = g_new0(XiveEND, xive->nr_ends);
3453aa597f6SCédric Le Goater 
34638afd772SCédric Le Goater     xive->nodename = g_strdup_printf("interrupt-controller@%" PRIx64,
34738afd772SCédric Le Goater                            xive->tm_base + XIVE_TM_USER_PAGE * (1 << TM_SHIFT));
34838afd772SCédric Le Goater 
34938afd772SCédric Le Goater     qemu_register_reset(spapr_xive_reset, dev);
350cdd71c8eSCédric Le Goater 
3513aa597f6SCédric Le Goater     /* TIMA initialization */
352d024a2c1SCédric Le Goater     memory_region_init_io(&xive->tm_mmio, OBJECT(xive), &spapr_xive_tm_ops,
353d024a2c1SCédric Le Goater                           xive, "xive.tima", 4ull << TM_SHIFT);
3543aa597f6SCédric Le Goater 
355981b1c62SCédric Le Goater     /*
356981b1c62SCédric Le Goater      * Map all regions. These will be enabled or disabled at reset and
357981b1c62SCédric Le Goater      * can also be overridden by KVM memory regions if active
358981b1c62SCédric Le Goater      */
3596c9dcd87SPhilippe Mathieu-Daudé     memory_region_add_subregion(get_system_memory(), xive->vc_base,
3606c9dcd87SPhilippe Mathieu-Daudé                                 &xsrc->esb_mmio);
3616c9dcd87SPhilippe Mathieu-Daudé     memory_region_add_subregion(get_system_memory(), xive->end_base,
3626c9dcd87SPhilippe Mathieu-Daudé                                 &end_xsrc->esb_mmio);
3636c9dcd87SPhilippe Mathieu-Daudé     memory_region_add_subregion(get_system_memory(), xive->tm_base,
3646c9dcd87SPhilippe Mathieu-Daudé                                 &xive->tm_mmio);
3653aa597f6SCédric Le Goater }
3663aa597f6SCédric Le Goater 
3673aa597f6SCédric Le Goater static int spapr_xive_get_eas(XiveRouter *xrtr, uint8_t eas_blk,
3683aa597f6SCédric Le Goater                               uint32_t eas_idx, XiveEAS *eas)
3693aa597f6SCédric Le Goater {
370ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3713aa597f6SCédric Le Goater 
3723aa597f6SCédric Le Goater     if (eas_idx >= xive->nr_irqs) {
3733aa597f6SCédric Le Goater         return -1;
3743aa597f6SCédric Le Goater     }
3753aa597f6SCédric Le Goater 
3763aa597f6SCédric Le Goater     *eas = xive->eat[eas_idx];
3773aa597f6SCédric Le Goater     return 0;
3783aa597f6SCédric Le Goater }
3793aa597f6SCédric Le Goater 
3803aa597f6SCédric Le Goater static int spapr_xive_get_end(XiveRouter *xrtr,
3813aa597f6SCédric Le Goater                               uint8_t end_blk, uint32_t end_idx, XiveEND *end)
3823aa597f6SCédric Le Goater {
383ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3843aa597f6SCédric Le Goater 
3853aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
3863aa597f6SCédric Le Goater         return -1;
3873aa597f6SCédric Le Goater     }
3883aa597f6SCédric Le Goater 
3893aa597f6SCédric Le Goater     memcpy(end, &xive->endt[end_idx], sizeof(XiveEND));
3903aa597f6SCédric Le Goater     return 0;
3913aa597f6SCédric Le Goater }
3923aa597f6SCédric Le Goater 
3933aa597f6SCédric Le Goater static int spapr_xive_write_end(XiveRouter *xrtr, uint8_t end_blk,
3943aa597f6SCédric Le Goater                                 uint32_t end_idx, XiveEND *end,
3953aa597f6SCédric Le Goater                                 uint8_t word_number)
3963aa597f6SCédric Le Goater {
397ce2918cbSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(xrtr);
3983aa597f6SCédric Le Goater 
3993aa597f6SCédric Le Goater     if (end_idx >= xive->nr_ends) {
4003aa597f6SCédric Le Goater         return -1;
4013aa597f6SCédric Le Goater     }
4023aa597f6SCédric Le Goater 
4033aa597f6SCédric Le Goater     memcpy(&xive->endt[end_idx], end, sizeof(XiveEND));
4043aa597f6SCédric Le Goater     return 0;
4053aa597f6SCédric Le Goater }
4063aa597f6SCédric Le Goater 
4070cddee8dSCédric Le Goater static int spapr_xive_get_nvt(XiveRouter *xrtr,
4080cddee8dSCédric Le Goater                               uint8_t nvt_blk, uint32_t nvt_idx, XiveNVT *nvt)
4090cddee8dSCédric Le Goater {
4100cddee8dSCédric Le Goater     uint32_t vcpu_id = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
4110cddee8dSCédric Le Goater     PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
4120cddee8dSCédric Le Goater 
4130cddee8dSCédric Le Goater     if (!cpu) {
4140cddee8dSCédric Le Goater         /* TODO: should we assert() if we can find a NVT ? */
4150cddee8dSCédric Le Goater         return -1;
4160cddee8dSCédric Le Goater     }
4170cddee8dSCédric Le Goater 
4180cddee8dSCédric Le Goater     /*
4190cddee8dSCédric Le Goater      * sPAPR does not maintain a NVT table. Return that the NVT is
4200cddee8dSCédric Le Goater      * valid if we have found a matching CPU
4210cddee8dSCédric Le Goater      */
4220cddee8dSCédric Le Goater     nvt->w0 = cpu_to_be32(NVT_W0_VALID);
4230cddee8dSCédric Le Goater     return 0;
4240cddee8dSCédric Le Goater }
4250cddee8dSCédric Le Goater 
4260cddee8dSCédric Le Goater static int spapr_xive_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk,
4270cddee8dSCédric Le Goater                                 uint32_t nvt_idx, XiveNVT *nvt,
4280cddee8dSCédric Le Goater                                 uint8_t word_number)
4290cddee8dSCédric Le Goater {
4300cddee8dSCédric Le Goater     /*
4310cddee8dSCédric Le Goater      * We don't need to write back to the NVTs because the sPAPR
4320cddee8dSCédric Le Goater      * machine should never hit a non-scheduled NVT. It should never
4330cddee8dSCédric Le Goater      * get called.
4340cddee8dSCédric Le Goater      */
4350cddee8dSCédric Le Goater     g_assert_not_reached();
4360cddee8dSCédric Le Goater }
4370cddee8dSCédric Le Goater 
438f87dae18SCédric Le Goater static int spapr_xive_match_nvt(XivePresenter *xptr, uint8_t format,
439f87dae18SCédric Le Goater                                 uint8_t nvt_blk, uint32_t nvt_idx,
440f87dae18SCédric Le Goater                                 bool cam_ignore, uint8_t priority,
441f87dae18SCédric Le Goater                                 uint32_t logic_serv, XiveTCTXMatch *match)
442f87dae18SCédric Le Goater {
443f87dae18SCédric Le Goater     CPUState *cs;
444f87dae18SCédric Le Goater     int count = 0;
445f87dae18SCédric Le Goater 
446f87dae18SCédric Le Goater     CPU_FOREACH(cs) {
447f87dae18SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
448f87dae18SCédric Le Goater         XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx;
449f87dae18SCédric Le Goater         int ring;
450f87dae18SCédric Le Goater 
451f87dae18SCédric Le Goater         /*
452f87dae18SCédric Le Goater          * Skip partially initialized vCPUs. This can happen when
453f87dae18SCédric Le Goater          * vCPUs are hotplugged.
454f87dae18SCédric Le Goater          */
455f87dae18SCédric Le Goater         if (!tctx) {
456f87dae18SCédric Le Goater             continue;
457f87dae18SCédric Le Goater         }
458f87dae18SCédric Le Goater 
459f87dae18SCédric Le Goater         /*
460f87dae18SCédric Le Goater          * Check the thread context CAM lines and record matches.
461f87dae18SCédric Le Goater          */
462f87dae18SCédric Le Goater         ring = xive_presenter_tctx_match(xptr, tctx, format, nvt_blk, nvt_idx,
463f87dae18SCédric Le Goater                                          cam_ignore, logic_serv);
464f87dae18SCédric Le Goater         /*
465f87dae18SCédric Le Goater          * Save the matching thread interrupt context and follow on to
466f87dae18SCédric Le Goater          * check for duplicates which are invalid.
467f87dae18SCédric Le Goater          */
468f87dae18SCédric Le Goater         if (ring != -1) {
469f87dae18SCédric Le Goater             if (match->tctx) {
470f87dae18SCédric Le Goater                 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: already found a thread "
471f87dae18SCédric Le Goater                               "context NVT %x/%x\n", nvt_blk, nvt_idx);
472f87dae18SCédric Le Goater                 return -1;
473f87dae18SCédric Le Goater             }
474f87dae18SCédric Le Goater 
475f87dae18SCédric Le Goater             match->ring = ring;
476f87dae18SCédric Le Goater             match->tctx = tctx;
477f87dae18SCédric Le Goater             count++;
478f87dae18SCédric Le Goater         }
479f87dae18SCédric Le Goater     }
480f87dae18SCédric Le Goater 
481f87dae18SCédric Le Goater     return count;
482f87dae18SCédric Le Goater }
483f87dae18SCédric Le Goater 
4842a24e6e3SFrederic Barrat static uint32_t spapr_xive_presenter_get_config(XivePresenter *xptr)
4852a24e6e3SFrederic Barrat {
4862a24e6e3SFrederic Barrat     uint32_t cfg = 0;
4872a24e6e3SFrederic Barrat 
4882a24e6e3SFrederic Barrat     /*
4892a24e6e3SFrederic Barrat      * Let's claim GEN1 TIMA format. If running with KVM on P10, the
4902a24e6e3SFrederic Barrat      * correct answer is deep in the hardware and not accessible to
4912a24e6e3SFrederic Barrat      * us.  But it shouldn't matter as it only affects the presenter
4922a24e6e3SFrederic Barrat      * as seen by a guest OS.
4932a24e6e3SFrederic Barrat      */
4942a24e6e3SFrederic Barrat     cfg |= XIVE_PRESENTER_GEN1_TIMA_OS;
4952a24e6e3SFrederic Barrat 
4962a24e6e3SFrederic Barrat     return cfg;
4972a24e6e3SFrederic Barrat }
4982a24e6e3SFrederic Barrat 
499f22f56ddSCédric Le Goater static uint8_t spapr_xive_get_block_id(XiveRouter *xrtr)
500f22f56ddSCédric Le Goater {
501f22f56ddSCédric Le Goater     return SPAPR_XIVE_BLOCK_ID;
502f22f56ddSCédric Le Goater }
503f22f56ddSCédric Le Goater 
5040aa2612aSCédric Le Goater static int spapr_xive_get_pq(XiveRouter *xrtr, uint8_t blk, uint32_t idx,
5050aa2612aSCédric Le Goater                              uint8_t *pq)
5060aa2612aSCédric Le Goater {
5070aa2612aSCédric Le Goater     SpaprXive *xive = SPAPR_XIVE(xrtr);
5080aa2612aSCédric Le Goater 
5090aa2612aSCédric Le Goater     assert(SPAPR_XIVE_BLOCK_ID == blk);
5100aa2612aSCédric Le Goater 
5110aa2612aSCédric Le Goater     *pq = xive_source_esb_get(&xive->source, idx);
5120aa2612aSCédric Le Goater     return 0;
5130aa2612aSCédric Le Goater }
5140aa2612aSCédric Le Goater 
5150aa2612aSCédric Le Goater static int spapr_xive_set_pq(XiveRouter *xrtr, uint8_t blk, uint32_t idx,
5160aa2612aSCédric Le Goater                              uint8_t *pq)
5170aa2612aSCédric Le Goater {
5180aa2612aSCédric Le Goater     SpaprXive *xive = SPAPR_XIVE(xrtr);
5190aa2612aSCédric Le Goater 
5200aa2612aSCédric Le Goater     assert(SPAPR_XIVE_BLOCK_ID == blk);
5210aa2612aSCédric Le Goater 
5220aa2612aSCédric Le Goater     *pq = xive_source_esb_set(&xive->source, idx, *pq);
5230aa2612aSCédric Le Goater     return 0;
5240aa2612aSCédric Le Goater }
5250aa2612aSCédric Le Goater 
5260aa2612aSCédric Le Goater 
5273aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_end = {
5283aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/end",
5293aa597f6SCédric Le Goater     .version_id = 1,
5303aa597f6SCédric Le Goater     .minimum_version_id = 1,
53145b1f81dSRichard Henderson     .fields = (const VMStateField []) {
5323aa597f6SCédric Le Goater         VMSTATE_UINT32(w0, XiveEND),
5333aa597f6SCédric Le Goater         VMSTATE_UINT32(w1, XiveEND),
5343aa597f6SCédric Le Goater         VMSTATE_UINT32(w2, XiveEND),
5353aa597f6SCédric Le Goater         VMSTATE_UINT32(w3, XiveEND),
5363aa597f6SCédric Le Goater         VMSTATE_UINT32(w4, XiveEND),
5373aa597f6SCédric Le Goater         VMSTATE_UINT32(w5, XiveEND),
5383aa597f6SCédric Le Goater         VMSTATE_UINT32(w6, XiveEND),
5393aa597f6SCédric Le Goater         VMSTATE_UINT32(w7, XiveEND),
5403aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
5413aa597f6SCédric Le Goater     },
5423aa597f6SCédric Le Goater };
5433aa597f6SCédric Le Goater 
5443aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_eas = {
5453aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE "/eas",
5463aa597f6SCédric Le Goater     .version_id = 1,
5473aa597f6SCédric Le Goater     .minimum_version_id = 1,
54845b1f81dSRichard Henderson     .fields = (const VMStateField []) {
5493aa597f6SCédric Le Goater         VMSTATE_UINT64(w, XiveEAS),
5503aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
5513aa597f6SCédric Le Goater     },
5523aa597f6SCédric Le Goater };
5533aa597f6SCédric Le Goater 
554277dd3d7SCédric Le Goater static int vmstate_spapr_xive_pre_save(void *opaque)
555277dd3d7SCédric Le Goater {
556e519cdd9SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(opaque);
557e519cdd9SGreg Kurz 
558e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
559e519cdd9SGreg Kurz         return kvmppc_xive_pre_save(xive);
560277dd3d7SCédric Le Goater     }
561277dd3d7SCédric Le Goater 
562277dd3d7SCédric Le Goater     return 0;
563277dd3d7SCédric Le Goater }
564277dd3d7SCédric Le Goater 
565277dd3d7SCédric Le Goater /*
566277dd3d7SCédric Le Goater  * Called by the sPAPR IRQ backend 'post_load' method at the machine
567277dd3d7SCédric Le Goater  * level.
568277dd3d7SCédric Le Goater  */
569605994e5SDavid Gibson static int spapr_xive_post_load(SpaprInterruptController *intc, int version_id)
570277dd3d7SCédric Le Goater {
571e519cdd9SGreg Kurz     SpaprXive *xive = SPAPR_XIVE(intc);
572e519cdd9SGreg Kurz 
573e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
574e519cdd9SGreg Kurz         return kvmppc_xive_post_load(xive, version_id);
575277dd3d7SCédric Le Goater     }
576277dd3d7SCédric Le Goater 
577277dd3d7SCédric Le Goater     return 0;
578277dd3d7SCédric Le Goater }
579277dd3d7SCédric Le Goater 
5803aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive = {
5813aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
5823aa597f6SCédric Le Goater     .version_id = 1,
5833aa597f6SCédric Le Goater     .minimum_version_id = 1,
584277dd3d7SCédric Le Goater     .pre_save = vmstate_spapr_xive_pre_save,
585277dd3d7SCédric Le Goater     .post_load = NULL, /* handled at the machine level */
58645b1f81dSRichard Henderson     .fields = (const VMStateField[]) {
587ce2918cbSDavid Gibson         VMSTATE_UINT32_EQUAL(nr_irqs, SpaprXive, NULL),
588ce2918cbSDavid Gibson         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(eat, SpaprXive, nr_irqs,
5893aa597f6SCédric Le Goater                                      vmstate_spapr_xive_eas, XiveEAS),
590ce2918cbSDavid Gibson         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(endt, SpaprXive, nr_ends,
5913aa597f6SCédric Le Goater                                              vmstate_spapr_xive_end, XiveEND),
5923aa597f6SCédric Le Goater         VMSTATE_END_OF_LIST()
5933aa597f6SCédric Le Goater     },
5943aa597f6SCédric Le Goater };
5953aa597f6SCédric Le Goater 
5960b0e52b1SDavid Gibson static int spapr_xive_claim_irq(SpaprInterruptController *intc, int lisn,
5970b0e52b1SDavid Gibson                                 bool lsi, Error **errp)
5980b0e52b1SDavid Gibson {
5990b0e52b1SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
6000b0e52b1SDavid Gibson     XiveSource *xsrc = &xive->source;
6010b0e52b1SDavid Gibson 
6020b0e52b1SDavid Gibson     assert(lisn < xive->nr_irqs);
6030b0e52b1SDavid Gibson 
6044e960974SCédric Le Goater     trace_spapr_xive_claim_irq(lisn, lsi);
6054e960974SCédric Le Goater 
6060b0e52b1SDavid Gibson     if (xive_eas_is_valid(&xive->eat[lisn])) {
6070b0e52b1SDavid Gibson         error_setg(errp, "IRQ %d is not free", lisn);
6080b0e52b1SDavid Gibson         return -EBUSY;
6090b0e52b1SDavid Gibson     }
6100b0e52b1SDavid Gibson 
6110b0e52b1SDavid Gibson     /*
6120b0e52b1SDavid Gibson      * Set default values when allocating an IRQ number
6130b0e52b1SDavid Gibson      */
6140b0e52b1SDavid Gibson     xive->eat[lisn].w |= cpu_to_be64(EAS_VALID | EAS_MASKED);
6150b0e52b1SDavid Gibson     if (lsi) {
6160b0e52b1SDavid Gibson         xive_source_irq_set_lsi(xsrc, lisn);
6170b0e52b1SDavid Gibson     }
6180b0e52b1SDavid Gibson 
619e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
6200b0e52b1SDavid Gibson         return kvmppc_xive_source_reset_one(xsrc, lisn, errp);
6210b0e52b1SDavid Gibson     }
6220b0e52b1SDavid Gibson 
6230b0e52b1SDavid Gibson     return 0;
6240b0e52b1SDavid Gibson }
6250b0e52b1SDavid Gibson 
6260b0e52b1SDavid Gibson static void spapr_xive_free_irq(SpaprInterruptController *intc, int lisn)
6270b0e52b1SDavid Gibson {
6280b0e52b1SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
6290b0e52b1SDavid Gibson     assert(lisn < xive->nr_irqs);
6300b0e52b1SDavid Gibson 
6314e960974SCédric Le Goater     trace_spapr_xive_free_irq(lisn);
6324e960974SCédric Le Goater 
6330b0e52b1SDavid Gibson     xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
6340b0e52b1SDavid Gibson }
6350b0e52b1SDavid Gibson 
6363aa597f6SCédric Le Goater static Property spapr_xive_properties[] = {
637ce2918cbSDavid Gibson     DEFINE_PROP_UINT32("nr-irqs", SpaprXive, nr_irqs, 0),
638ce2918cbSDavid Gibson     DEFINE_PROP_UINT32("nr-ends", SpaprXive, nr_ends, 0),
639ce2918cbSDavid Gibson     DEFINE_PROP_UINT64("vc-base", SpaprXive, vc_base, SPAPR_XIVE_VC_BASE),
640ce2918cbSDavid Gibson     DEFINE_PROP_UINT64("tm-base", SpaprXive, tm_base, SPAPR_XIVE_TM_BASE),
6414f311a70SCédric Le Goater     DEFINE_PROP_UINT8("hv-prio", SpaprXive, hv_prio, 7),
6423aa597f6SCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
6433aa597f6SCédric Le Goater };
6443aa597f6SCédric Le Goater 
645ebd6be08SDavid Gibson static int spapr_xive_cpu_intc_create(SpaprInterruptController *intc,
646ebd6be08SDavid Gibson                                       PowerPCCPU *cpu, Error **errp)
647ebd6be08SDavid Gibson {
648ebd6be08SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
649ebd6be08SDavid Gibson     Object *obj;
650ebd6be08SDavid Gibson     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
651ebd6be08SDavid Gibson 
65247950946SCédric Le Goater     obj = xive_tctx_create(OBJECT(cpu), XIVE_PRESENTER(xive), errp);
653ebd6be08SDavid Gibson     if (!obj) {
654ebd6be08SDavid Gibson         return -1;
655ebd6be08SDavid Gibson     }
656ebd6be08SDavid Gibson 
657ebd6be08SDavid Gibson     spapr_cpu->tctx = XIVE_TCTX(obj);
658ebd6be08SDavid Gibson     return 0;
659ebd6be08SDavid Gibson }
660ebd6be08SDavid Gibson 
66197c00c54SCédric Le Goater static void xive_tctx_set_os_cam(XiveTCTX *tctx, uint32_t os_cam)
66297c00c54SCédric Le Goater {
66397c00c54SCédric Le Goater     uint32_t qw1w2 = cpu_to_be32(TM_QW1W2_VO | os_cam);
66497c00c54SCédric Le Goater     memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1w2, 4);
66597c00c54SCédric Le Goater }
66697c00c54SCédric Le Goater 
667d49e8a9bSCédric Le Goater static void spapr_xive_cpu_intc_reset(SpaprInterruptController *intc,
668d49e8a9bSCédric Le Goater                                      PowerPCCPU *cpu)
669d49e8a9bSCédric Le Goater {
670d49e8a9bSCédric Le Goater     XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx;
67197c00c54SCédric Le Goater     uint8_t  nvt_blk;
67297c00c54SCédric Le Goater     uint32_t nvt_idx;
673d49e8a9bSCédric Le Goater 
674d49e8a9bSCédric Le Goater     xive_tctx_reset(tctx);
67597c00c54SCédric Le Goater 
67697c00c54SCédric Le Goater     /*
67797c00c54SCédric Le Goater      * When a Virtual Processor is scheduled to run on a HW thread,
67897c00c54SCédric Le Goater      * the hypervisor pushes its identifier in the OS CAM line.
67997c00c54SCédric Le Goater      * Emulate the same behavior under QEMU.
68097c00c54SCédric Le Goater      */
68197c00c54SCédric Le Goater     spapr_xive_cpu_to_nvt(cpu, &nvt_blk, &nvt_idx);
68297c00c54SCédric Le Goater 
68397c00c54SCédric Le Goater     xive_tctx_set_os_cam(tctx, xive_nvt_cam_line(nvt_blk, nvt_idx));
684d49e8a9bSCédric Le Goater }
685d49e8a9bSCédric Le Goater 
6860990ce6aSGreg Kurz static void spapr_xive_cpu_intc_destroy(SpaprInterruptController *intc,
6870990ce6aSGreg Kurz                                         PowerPCCPU *cpu)
6880990ce6aSGreg Kurz {
6890990ce6aSGreg Kurz     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
6900990ce6aSGreg Kurz 
6910990ce6aSGreg Kurz     xive_tctx_destroy(spapr_cpu->tctx);
6920990ce6aSGreg Kurz     spapr_cpu->tctx = NULL;
6930990ce6aSGreg Kurz }
6940990ce6aSGreg Kurz 
6957bcdbccaSDavid Gibson static void spapr_xive_set_irq(SpaprInterruptController *intc, int irq, int val)
6967bcdbccaSDavid Gibson {
6977bcdbccaSDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
6987bcdbccaSDavid Gibson 
6994e960974SCédric Le Goater     trace_spapr_xive_set_irq(irq, val);
7004e960974SCédric Le Goater 
701e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
7027bcdbccaSDavid Gibson         kvmppc_xive_source_set_irq(&xive->source, irq, val);
7037bcdbccaSDavid Gibson     } else {
7047bcdbccaSDavid Gibson         xive_source_set_irq(&xive->source, irq, val);
7057bcdbccaSDavid Gibson     }
7067bcdbccaSDavid Gibson }
7077bcdbccaSDavid Gibson 
708328d8eb2SDavid Gibson static void spapr_xive_print_info(SpaprInterruptController *intc, Monitor *mon)
709328d8eb2SDavid Gibson {
710328d8eb2SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
711328d8eb2SDavid Gibson     CPUState *cs;
712f163e270SPhilippe Mathieu-Daudé     g_autoptr(GString) buf = g_string_new("");
713f163e270SPhilippe Mathieu-Daudé     g_autoptr(HumanReadableText) info = NULL;
714328d8eb2SDavid Gibson 
715328d8eb2SDavid Gibson     CPU_FOREACH(cs) {
716328d8eb2SDavid Gibson         PowerPCCPU *cpu = POWERPC_CPU(cs);
717328d8eb2SDavid Gibson 
718f163e270SPhilippe Mathieu-Daudé         xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, buf);
719328d8eb2SDavid Gibson     }
720f163e270SPhilippe Mathieu-Daudé     info = human_readable_text_from_str(buf);
721f163e270SPhilippe Mathieu-Daudé     monitor_puts(mon, info->human_readable_text);
722328d8eb2SDavid Gibson 
723328d8eb2SDavid Gibson     spapr_xive_pic_print_info(xive, mon);
724328d8eb2SDavid Gibson }
725328d8eb2SDavid Gibson 
72605289273SDavid Gibson static void spapr_xive_dt(SpaprInterruptController *intc, uint32_t nr_servers,
72705289273SDavid Gibson                           void *fdt, uint32_t phandle)
72805289273SDavid Gibson {
72905289273SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
73005289273SDavid Gibson     int node;
73105289273SDavid Gibson     uint64_t timas[2 * 2];
73205289273SDavid Gibson     /* Interrupt number ranges for the IPIs */
73305289273SDavid Gibson     uint32_t lisn_ranges[] = {
73452d3403dSCédric Le Goater         cpu_to_be32(SPAPR_IRQ_IPI),
73552d3403dSCédric Le Goater         cpu_to_be32(SPAPR_IRQ_IPI + nr_servers),
73605289273SDavid Gibson     };
73705289273SDavid Gibson     /*
73805289273SDavid Gibson      * EQ size - the sizes of pages supported by the system 4K, 64K,
73905289273SDavid Gibson      * 2M, 16M. We only advertise 64K for the moment.
74005289273SDavid Gibson      */
74105289273SDavid Gibson     uint32_t eq_sizes[] = {
74205289273SDavid Gibson         cpu_to_be32(16), /* 64K */
74305289273SDavid Gibson     };
74405289273SDavid Gibson     /*
7454f311a70SCédric Le Goater      * QEMU/KVM only needs to define a single range to reserve the
7464f311a70SCédric Le Goater      * escalation priority. A priority bitmask would have been more
7474f311a70SCédric Le Goater      * appropriate.
74805289273SDavid Gibson      */
74905289273SDavid Gibson     uint32_t plat_res_int_priorities[] = {
7504f311a70SCédric Le Goater         cpu_to_be32(xive->hv_prio),    /* start */
7514f311a70SCédric Le Goater         cpu_to_be32(0xff - xive->hv_prio), /* count */
75205289273SDavid Gibson     };
75305289273SDavid Gibson 
75405289273SDavid Gibson     /* Thread Interrupt Management Area : User (ring 3) and OS (ring 2) */
75505289273SDavid Gibson     timas[0] = cpu_to_be64(xive->tm_base +
75605289273SDavid Gibson                            XIVE_TM_USER_PAGE * (1ull << TM_SHIFT));
75705289273SDavid Gibson     timas[1] = cpu_to_be64(1ull << TM_SHIFT);
75805289273SDavid Gibson     timas[2] = cpu_to_be64(xive->tm_base +
75905289273SDavid Gibson                            XIVE_TM_OS_PAGE * (1ull << TM_SHIFT));
76005289273SDavid Gibson     timas[3] = cpu_to_be64(1ull << TM_SHIFT);
76105289273SDavid Gibson 
76205289273SDavid Gibson     _FDT(node = fdt_add_subnode(fdt, 0, xive->nodename));
76305289273SDavid Gibson 
76405289273SDavid Gibson     _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
76505289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
76605289273SDavid Gibson 
76705289273SDavid Gibson     _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
76805289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
76905289273SDavid Gibson                      sizeof(eq_sizes)));
77005289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
77105289273SDavid Gibson                      sizeof(lisn_ranges)));
77205289273SDavid Gibson 
77305289273SDavid Gibson     /* For Linux to link the LSIs to the interrupt controller. */
77405289273SDavid Gibson     _FDT(fdt_setprop(fdt, node, "interrupt-controller", NULL, 0));
77505289273SDavid Gibson     _FDT(fdt_setprop_cell(fdt, node, "#interrupt-cells", 2));
77605289273SDavid Gibson 
77705289273SDavid Gibson     /* For SLOF */
77805289273SDavid Gibson     _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
77905289273SDavid Gibson     _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
78005289273SDavid Gibson 
78105289273SDavid Gibson     /*
78205289273SDavid Gibson      * The "ibm,plat-res-int-priorities" property defines the priority
78305289273SDavid Gibson      * ranges reserved by the hypervisor
78405289273SDavid Gibson      */
78505289273SDavid Gibson     _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
78605289273SDavid Gibson                      plat_res_int_priorities, sizeof(plat_res_int_priorities)));
78705289273SDavid Gibson }
78805289273SDavid Gibson 
7894ffb7496SGreg Kurz static int spapr_xive_activate(SpaprInterruptController *intc,
7904ffb7496SGreg Kurz                                uint32_t nr_servers, Error **errp)
791567192d4SDavid Gibson {
792567192d4SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
793567192d4SDavid Gibson 
794567192d4SDavid Gibson     if (kvm_enabled()) {
7954ffb7496SGreg Kurz         int rc = spapr_irq_init_kvm(kvmppc_xive_connect, intc, nr_servers,
7964ffb7496SGreg Kurz                                     errp);
797567192d4SDavid Gibson         if (rc < 0) {
798567192d4SDavid Gibson             return rc;
799567192d4SDavid Gibson         }
800567192d4SDavid Gibson     }
801567192d4SDavid Gibson 
802567192d4SDavid Gibson     /* Activate the XIVE MMIOs */
803567192d4SDavid Gibson     spapr_xive_mmio_set_enabled(xive, true);
804567192d4SDavid Gibson 
805567192d4SDavid Gibson     return 0;
806567192d4SDavid Gibson }
807567192d4SDavid Gibson 
808567192d4SDavid Gibson static void spapr_xive_deactivate(SpaprInterruptController *intc)
809567192d4SDavid Gibson {
810567192d4SDavid Gibson     SpaprXive *xive = SPAPR_XIVE(intc);
811567192d4SDavid Gibson 
812567192d4SDavid Gibson     spapr_xive_mmio_set_enabled(xive, false);
813567192d4SDavid Gibson 
814e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
815567192d4SDavid Gibson         kvmppc_xive_disconnect(intc);
816567192d4SDavid Gibson     }
817567192d4SDavid Gibson }
818567192d4SDavid Gibson 
819e519cdd9SGreg Kurz static bool spapr_xive_in_kernel_xptr(const XivePresenter *xptr)
820e519cdd9SGreg Kurz {
821e519cdd9SGreg Kurz     return spapr_xive_in_kernel(SPAPR_XIVE(xptr));
822e519cdd9SGreg Kurz }
823e519cdd9SGreg Kurz 
8243aa597f6SCédric Le Goater static void spapr_xive_class_init(ObjectClass *klass, void *data)
8253aa597f6SCédric Le Goater {
8263aa597f6SCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
8273aa597f6SCédric Le Goater     XiveRouterClass *xrc = XIVE_ROUTER_CLASS(klass);
828ebd6be08SDavid Gibson     SpaprInterruptControllerClass *sicc = SPAPR_INTC_CLASS(klass);
829f87dae18SCédric Le Goater     XivePresenterClass *xpc = XIVE_PRESENTER_CLASS(klass);
8306cc64796SGreg Kurz     SpaprXiveClass *sxc = SPAPR_XIVE_CLASS(klass);
8313aa597f6SCédric Le Goater 
8323aa597f6SCédric Le Goater     dc->desc    = "sPAPR XIVE Interrupt Controller";
8334f67d30bSMarc-André Lureau     device_class_set_props(dc, spapr_xive_properties);
8346cc64796SGreg Kurz     device_class_set_parent_realize(dc, spapr_xive_realize,
8356cc64796SGreg Kurz                                     &sxc->parent_realize);
8363aa597f6SCédric Le Goater     dc->vmsd    = &vmstate_spapr_xive;
8373aa597f6SCédric Le Goater 
8383aa597f6SCédric Le Goater     xrc->get_eas = spapr_xive_get_eas;
8390aa2612aSCédric Le Goater     xrc->get_pq  = spapr_xive_get_pq;
8400aa2612aSCédric Le Goater     xrc->set_pq  = spapr_xive_set_pq;
8413aa597f6SCédric Le Goater     xrc->get_end = spapr_xive_get_end;
8423aa597f6SCédric Le Goater     xrc->write_end = spapr_xive_write_end;
8430cddee8dSCédric Le Goater     xrc->get_nvt = spapr_xive_get_nvt;
8440cddee8dSCédric Le Goater     xrc->write_nvt = spapr_xive_write_nvt;
845f22f56ddSCédric Le Goater     xrc->get_block_id = spapr_xive_get_block_id;
846ebd6be08SDavid Gibson 
847567192d4SDavid Gibson     sicc->activate = spapr_xive_activate;
848567192d4SDavid Gibson     sicc->deactivate = spapr_xive_deactivate;
849ebd6be08SDavid Gibson     sicc->cpu_intc_create = spapr_xive_cpu_intc_create;
850d49e8a9bSCédric Le Goater     sicc->cpu_intc_reset = spapr_xive_cpu_intc_reset;
8510990ce6aSGreg Kurz     sicc->cpu_intc_destroy = spapr_xive_cpu_intc_destroy;
8520b0e52b1SDavid Gibson     sicc->claim_irq = spapr_xive_claim_irq;
8530b0e52b1SDavid Gibson     sicc->free_irq = spapr_xive_free_irq;
8547bcdbccaSDavid Gibson     sicc->set_irq = spapr_xive_set_irq;
855328d8eb2SDavid Gibson     sicc->print_info = spapr_xive_print_info;
85605289273SDavid Gibson     sicc->dt = spapr_xive_dt;
857605994e5SDavid Gibson     sicc->post_load = spapr_xive_post_load;
858f87dae18SCédric Le Goater 
859f87dae18SCédric Le Goater     xpc->match_nvt  = spapr_xive_match_nvt;
8602a24e6e3SFrederic Barrat     xpc->get_config = spapr_xive_presenter_get_config;
861e519cdd9SGreg Kurz     xpc->in_kernel  = spapr_xive_in_kernel_xptr;
8623aa597f6SCédric Le Goater }
8633aa597f6SCédric Le Goater 
8643aa597f6SCédric Le Goater static const TypeInfo spapr_xive_info = {
8653aa597f6SCédric Le Goater     .name = TYPE_SPAPR_XIVE,
8663aa597f6SCédric Le Goater     .parent = TYPE_XIVE_ROUTER,
8673aa597f6SCédric Le Goater     .instance_init = spapr_xive_instance_init,
868ce2918cbSDavid Gibson     .instance_size = sizeof(SpaprXive),
8693aa597f6SCédric Le Goater     .class_init = spapr_xive_class_init,
8706cc64796SGreg Kurz     .class_size = sizeof(SpaprXiveClass),
871150e25f8SDavid Gibson     .interfaces = (InterfaceInfo[]) {
872150e25f8SDavid Gibson         { TYPE_SPAPR_INTC },
873150e25f8SDavid Gibson         { }
874150e25f8SDavid Gibson     },
8753aa597f6SCédric Le Goater };
8763aa597f6SCédric Le Goater 
8773aa597f6SCédric Le Goater static void spapr_xive_register_types(void)
8783aa597f6SCédric Le Goater {
8793aa597f6SCédric Le Goater     type_register_static(&spapr_xive_info);
8803aa597f6SCédric Le Goater }
8813aa597f6SCédric Le Goater 
8823aa597f6SCédric Le Goater type_init(spapr_xive_register_types)
8833aa597f6SCédric Le Goater 
88423bcd5ebSCédric Le Goater /*
88523bcd5ebSCédric Le Goater  * XIVE hcalls
88623bcd5ebSCédric Le Goater  *
88723bcd5ebSCédric Le Goater  * The terminology used by the XIVE hcalls is the following :
88823bcd5ebSCédric Le Goater  *
88923bcd5ebSCédric Le Goater  *   TARGET vCPU number
89023bcd5ebSCédric Le Goater  *   EQ     Event Queue assigned by OS to receive event data
89123bcd5ebSCédric Le Goater  *   ESB    page for source interrupt management
89223bcd5ebSCédric Le Goater  *   LISN   Logical Interrupt Source Number identifying a source in the
89323bcd5ebSCédric Le Goater  *          machine
89423bcd5ebSCédric Le Goater  *   EISN   Effective Interrupt Source Number used by guest OS to
89523bcd5ebSCédric Le Goater  *          identify source in the guest
89623bcd5ebSCédric Le Goater  *
89723bcd5ebSCédric Le Goater  * The EAS, END, NVT structures are not exposed.
89823bcd5ebSCédric Le Goater  */
89923bcd5ebSCédric Le Goater 
90023bcd5ebSCédric Le Goater /*
9014f311a70SCédric Le Goater  * On POWER9, the KVM XIVE device uses priority 7 for the escalation
9024f311a70SCédric Le Goater  * interrupts. So we only allow the guest to use priorities [0..6].
90323bcd5ebSCédric Le Goater  */
9044f311a70SCédric Le Goater static bool spapr_xive_priority_is_reserved(SpaprXive *xive, uint8_t priority)
90523bcd5ebSCédric Le Goater {
9064f311a70SCédric Le Goater     return priority >= xive->hv_prio;
90723bcd5ebSCédric Le Goater }
90823bcd5ebSCédric Le Goater 
90923bcd5ebSCédric Le Goater /*
91023bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical
91123bcd5ebSCédric Le Goater  * real address of the MMIO page through which the Event State Buffer
91223bcd5ebSCédric Le Goater  * entry associated with the value of the "lisn" parameter is managed.
91323bcd5ebSCédric Le Goater  *
91423bcd5ebSCédric Le Goater  * Parameters:
91523bcd5ebSCédric Le Goater  * Input
91623bcd5ebSCédric Le Goater  * - R4: "flags"
91723bcd5ebSCédric Le Goater  *         Bits 0-63 reserved
91823bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
91923bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
92023bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned
92123bcd5ebSCédric Le Goater  *       by the H_ALLOCATE_VAS_WINDOW hcall
92223bcd5ebSCédric Le Goater  *
92323bcd5ebSCédric Le Goater  * Output
92423bcd5ebSCédric Le Goater  * - R4: "flags"
92523bcd5ebSCédric Le Goater  *         Bits 0-59: Reserved
92623bcd5ebSCédric Le Goater  *         Bit 60: H_INT_ESB must be used for Event State Buffer
92723bcd5ebSCédric Le Goater  *                 management
92823bcd5ebSCédric Le Goater  *         Bit 61: 1 == LSI  0 == MSI
92923bcd5ebSCédric Le Goater  *         Bit 62: the full function page supports trigger
93023bcd5ebSCédric Le Goater  *         Bit 63: Store EOI Supported
93123bcd5ebSCédric Le Goater  * - R5: Logical Real address of full function Event State Buffer
93223bcd5ebSCédric Le Goater  *       management page, -1 if H_INT_ESB hcall flag is set to 1.
93323bcd5ebSCédric Le Goater  * - R6: Logical Real Address of trigger only Event State Buffer
93423bcd5ebSCédric Le Goater  *       management page or -1.
93523bcd5ebSCédric Le Goater  * - R7: Power of 2 page size for the ESB management pages returned in
93623bcd5ebSCédric Le Goater  *       R5 and R6.
93723bcd5ebSCédric Le Goater  */
93823bcd5ebSCédric Le Goater 
93923bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_H_INT_ESB     PPC_BIT(60) /* ESB manage with H_INT_ESB */
94023bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_LSI           PPC_BIT(61) /* Virtual LSI type */
94123bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_TRIGGER       PPC_BIT(62) /* Trigger and management
94223bcd5ebSCédric Le Goater                                                     on same page */
94323bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_STORE_EOI     PPC_BIT(63) /* Store EOI support */
94423bcd5ebSCédric Le Goater 
94523bcd5ebSCédric Le Goater static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
946ce2918cbSDavid Gibson                                           SpaprMachineState *spapr,
94723bcd5ebSCédric Le Goater                                           target_ulong opcode,
94823bcd5ebSCédric Le Goater                                           target_ulong *args)
94923bcd5ebSCédric Le Goater {
950ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
95123bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
95223bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
95323bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
95423bcd5ebSCédric Le Goater 
9554e960974SCédric Le Goater     trace_spapr_xive_get_source_info(flags, lisn);
9564e960974SCédric Le Goater 
95723bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
95823bcd5ebSCédric Le Goater         return H_FUNCTION;
95923bcd5ebSCédric Le Goater     }
96023bcd5ebSCédric Le Goater 
96123bcd5ebSCédric Le Goater     if (flags) {
96223bcd5ebSCédric Le Goater         return H_PARAMETER;
96323bcd5ebSCédric Le Goater     }
96423bcd5ebSCédric Le Goater 
96523bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
96623bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
96723bcd5ebSCédric Le Goater                       lisn);
96823bcd5ebSCédric Le Goater         return H_P2;
96923bcd5ebSCédric Le Goater     }
97023bcd5ebSCédric Le Goater 
97123bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&xive->eat[lisn])) {
97223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
97323bcd5ebSCédric Le Goater                       lisn);
97423bcd5ebSCédric Le Goater         return H_P2;
97523bcd5ebSCédric Le Goater     }
97623bcd5ebSCédric Le Goater 
97723bcd5ebSCédric Le Goater     /*
97823bcd5ebSCédric Le Goater      * All sources are emulated under the main XIVE object and share
97923bcd5ebSCédric Le Goater      * the same characteristics.
98023bcd5ebSCédric Le Goater      */
98123bcd5ebSCédric Le Goater     args[0] = 0;
98223bcd5ebSCédric Le Goater     if (!xive_source_esb_has_2page(xsrc)) {
98323bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_TRIGGER;
98423bcd5ebSCédric Le Goater     }
98523bcd5ebSCédric Le Goater     if (xsrc->esb_flags & XIVE_SRC_STORE_EOI) {
98623bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_STORE_EOI;
98723bcd5ebSCédric Le Goater     }
98823bcd5ebSCédric Le Goater 
98923bcd5ebSCédric Le Goater     /*
99023bcd5ebSCédric Le Goater      * Force the use of the H_INT_ESB hcall in case of an LSI
99123bcd5ebSCédric Le Goater      * interrupt. This is necessary under KVM to re-trigger the
99223bcd5ebSCédric Le Goater      * interrupt if the level is still asserted
99323bcd5ebSCédric Le Goater      */
99423bcd5ebSCédric Le Goater     if (xive_source_irq_is_lsi(xsrc, lisn)) {
99523bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_SRC_H_INT_ESB | SPAPR_XIVE_SRC_LSI;
99623bcd5ebSCédric Le Goater     }
99723bcd5ebSCédric Le Goater 
99823bcd5ebSCédric Le Goater     if (!(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
99923bcd5ebSCédric Le Goater         args[1] = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn);
100023bcd5ebSCédric Le Goater     } else {
100123bcd5ebSCédric Le Goater         args[1] = -1;
100223bcd5ebSCédric Le Goater     }
100323bcd5ebSCédric Le Goater 
100423bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc) &&
100523bcd5ebSCédric Le Goater         !(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
100623bcd5ebSCédric Le Goater         args[2] = xive->vc_base + xive_source_esb_page(xsrc, lisn);
100723bcd5ebSCédric Le Goater     } else {
100823bcd5ebSCédric Le Goater         args[2] = -1;
100923bcd5ebSCédric Le Goater     }
101023bcd5ebSCédric Le Goater 
101123bcd5ebSCédric Le Goater     if (xive_source_esb_has_2page(xsrc)) {
101223bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift - 1;
101323bcd5ebSCédric Le Goater     } else {
101423bcd5ebSCédric Le Goater         args[3] = xsrc->esb_shift;
101523bcd5ebSCédric Le Goater     }
101623bcd5ebSCédric Le Goater 
101723bcd5ebSCédric Le Goater     return H_SUCCESS;
101823bcd5ebSCédric Le Goater }
101923bcd5ebSCédric Le Goater 
102023bcd5ebSCédric Le Goater /*
102123bcd5ebSCédric Le Goater  * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
102223bcd5ebSCédric Le Goater  * Interrupt Source to a target. The Logical Interrupt Source is
102323bcd5ebSCédric Le Goater  * designated with the "lisn" parameter and the target is designated
102423bcd5ebSCédric Le Goater  * with the "target" and "priority" parameters.  Upon return from the
102523bcd5ebSCédric Le Goater  * hcall(), no additional interrupts will be directed to the old EQ.
102623bcd5ebSCédric Le Goater  *
102723bcd5ebSCédric Le Goater  * Parameters:
102823bcd5ebSCédric Le Goater  * Input:
102923bcd5ebSCédric Le Goater  * - R4: "flags"
103023bcd5ebSCédric Le Goater  *         Bits 0-61: Reserved
103123bcd5ebSCédric Le Goater  *         Bit 62: set the "eisn" in the EAS
103223bcd5ebSCédric Le Goater  *         Bit 63: masks the interrupt source in the hardware interrupt
103323bcd5ebSCédric Le Goater  *       control structure. An interrupt masked by this mechanism will
103423bcd5ebSCédric Le Goater  *       be dropped, but it's source state bits will still be
103523bcd5ebSCédric Le Goater  *       set. There is no race-free way of unmasking and restoring the
103623bcd5ebSCédric Le Goater  *       source. Thus this should only be used in interrupts that are
103723bcd5ebSCédric Le Goater  *       also masked at the source, and only in cases where the
103823bcd5ebSCédric Le Goater  *       interrupt is not meant to be used for a large amount of time
103923bcd5ebSCédric Le Goater  *       because no valid target exists for it for example
104023bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
104123bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
104223bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as returned by
104323bcd5ebSCédric Le Goater  *       the H_ALLOCATE_VAS_WINDOW hcall
104423bcd5ebSCédric Le Goater  * - R6: "target" is per "ibm,ppc-interrupt-server#s" or
104523bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
104623bcd5ebSCédric Le Goater  * - R7: "priority" is a valid priority not in
104723bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
104823bcd5ebSCédric Le Goater  * - R8: "eisn" is the guest EISN associated with the "lisn"
104923bcd5ebSCédric Le Goater  *
105023bcd5ebSCédric Le Goater  * Output:
105123bcd5ebSCédric Le Goater  * - None
105223bcd5ebSCédric Le Goater  */
105323bcd5ebSCédric Le Goater 
105423bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_SET_EISN PPC_BIT(62)
105523bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_MASK     PPC_BIT(63)
105623bcd5ebSCédric Le Goater 
105723bcd5ebSCédric Le Goater static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
1058ce2918cbSDavid Gibson                                             SpaprMachineState *spapr,
105923bcd5ebSCédric Le Goater                                             target_ulong opcode,
106023bcd5ebSCédric Le Goater                                             target_ulong *args)
106123bcd5ebSCédric Le Goater {
1062ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
106323bcd5ebSCédric Le Goater     XiveEAS eas, new_eas;
106423bcd5ebSCédric Le Goater     target_ulong flags    = args[0];
106523bcd5ebSCédric Le Goater     target_ulong lisn     = args[1];
106623bcd5ebSCédric Le Goater     target_ulong target   = args[2];
106723bcd5ebSCédric Le Goater     target_ulong priority = args[3];
106823bcd5ebSCédric Le Goater     target_ulong eisn     = args[4];
106923bcd5ebSCédric Le Goater     uint8_t end_blk;
107023bcd5ebSCédric Le Goater     uint32_t end_idx;
107123bcd5ebSCédric Le Goater 
10724e960974SCédric Le Goater     trace_spapr_xive_set_source_config(flags, lisn, target, priority, eisn);
10734e960974SCédric Le Goater 
107423bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
107523bcd5ebSCédric Le Goater         return H_FUNCTION;
107623bcd5ebSCédric Le Goater     }
107723bcd5ebSCédric Le Goater 
107823bcd5ebSCédric Le Goater     if (flags & ~(SPAPR_XIVE_SRC_SET_EISN | SPAPR_XIVE_SRC_MASK)) {
107923bcd5ebSCédric Le Goater         return H_PARAMETER;
108023bcd5ebSCédric Le Goater     }
108123bcd5ebSCédric Le Goater 
108223bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
108323bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
108423bcd5ebSCédric Le Goater                       lisn);
108523bcd5ebSCédric Le Goater         return H_P2;
108623bcd5ebSCédric Le Goater     }
108723bcd5ebSCédric Le Goater 
108823bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
108923bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
109023bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
109123bcd5ebSCédric Le Goater                       lisn);
109223bcd5ebSCédric Le Goater         return H_P2;
109323bcd5ebSCédric Le Goater     }
109423bcd5ebSCédric Le Goater 
109523bcd5ebSCédric Le Goater     /* priority 0xff is used to reset the EAS */
109623bcd5ebSCédric Le Goater     if (priority == 0xff) {
109723bcd5ebSCédric Le Goater         new_eas.w = cpu_to_be64(EAS_VALID | EAS_MASKED);
109823bcd5ebSCédric Le Goater         goto out;
109923bcd5ebSCédric Le Goater     }
110023bcd5ebSCédric Le Goater 
110123bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_MASK) {
110223bcd5ebSCédric Le Goater         new_eas.w = eas.w | cpu_to_be64(EAS_MASKED);
110323bcd5ebSCédric Le Goater     } else {
110423bcd5ebSCédric Le Goater         new_eas.w = eas.w & cpu_to_be64(~EAS_MASKED);
110523bcd5ebSCédric Le Goater     }
110623bcd5ebSCédric Le Goater 
11074f311a70SCédric Le Goater     if (spapr_xive_priority_is_reserved(xive, priority)) {
110823bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
110923bcd5ebSCédric Le Goater                       " is reserved\n", priority);
111023bcd5ebSCédric Le Goater         return H_P4;
111123bcd5ebSCédric Le Goater     }
111223bcd5ebSCédric Le Goater 
111323bcd5ebSCédric Le Goater     /*
111423bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
111523bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
111623bcd5ebSCédric Le Goater      * target.
111723bcd5ebSCédric Le Goater      */
111823bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
111923bcd5ebSCédric Le Goater         return H_P3;
112023bcd5ebSCédric Le Goater     }
112123bcd5ebSCédric Le Goater 
112223bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_BLOCK, new_eas.w, end_blk);
112323bcd5ebSCédric Le Goater     new_eas.w = xive_set_field64(EAS_END_INDEX, new_eas.w, end_idx);
112423bcd5ebSCédric Le Goater 
112523bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_SRC_SET_EISN) {
112623bcd5ebSCédric Le Goater         new_eas.w = xive_set_field64(EAS_END_DATA, new_eas.w, eisn);
112723bcd5ebSCédric Le Goater     }
112823bcd5ebSCédric Le Goater 
1129e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
11300c575703SCédric Le Goater         Error *local_err = NULL;
11310c575703SCédric Le Goater 
11320c575703SCédric Le Goater         kvmppc_xive_set_source_config(xive, lisn, &new_eas, &local_err);
11330c575703SCédric Le Goater         if (local_err) {
11340c575703SCédric Le Goater             error_report_err(local_err);
11350c575703SCédric Le Goater             return H_HARDWARE;
11360c575703SCédric Le Goater         }
11370c575703SCédric Le Goater     }
11380c575703SCédric Le Goater 
113923bcd5ebSCédric Le Goater out:
114023bcd5ebSCédric Le Goater     xive->eat[lisn] = new_eas;
114123bcd5ebSCédric Le Goater     return H_SUCCESS;
114223bcd5ebSCédric Le Goater }
114323bcd5ebSCédric Le Goater 
114423bcd5ebSCédric Le Goater /*
114523bcd5ebSCédric Le Goater  * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
114623bcd5ebSCédric Le Goater  * target/priority pair is assigned to the specified Logical Interrupt
114723bcd5ebSCédric Le Goater  * Source.
114823bcd5ebSCédric Le Goater  *
114923bcd5ebSCédric Le Goater  * Parameters:
115023bcd5ebSCédric Le Goater  * Input:
115123bcd5ebSCédric Le Goater  * - R4: "flags"
115223bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
115323bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
115423bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
115523bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
115623bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
115723bcd5ebSCédric Le Goater  *
115823bcd5ebSCédric Le Goater  * Output:
115923bcd5ebSCédric Le Goater  * - R4: Target to which the specified Logical Interrupt Source is
116023bcd5ebSCédric Le Goater  *       assigned
116123bcd5ebSCédric Le Goater  * - R5: Priority to which the specified Logical Interrupt Source is
116223bcd5ebSCédric Le Goater  *       assigned
116323bcd5ebSCédric Le Goater  * - R6: EISN for the specified Logical Interrupt Source (this will be
116423bcd5ebSCédric Le Goater  *       equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
116523bcd5ebSCédric Le Goater  */
116623bcd5ebSCédric Le Goater static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
1167ce2918cbSDavid Gibson                                             SpaprMachineState *spapr,
116823bcd5ebSCédric Le Goater                                             target_ulong opcode,
116923bcd5ebSCédric Le Goater                                             target_ulong *args)
117023bcd5ebSCédric Le Goater {
1171ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
117223bcd5ebSCédric Le Goater     target_ulong flags = args[0];
117323bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
117423bcd5ebSCédric Le Goater     XiveEAS eas;
117523bcd5ebSCédric Le Goater     XiveEND *end;
117623bcd5ebSCédric Le Goater     uint8_t nvt_blk;
117723bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
117823bcd5ebSCédric Le Goater 
11794e960974SCédric Le Goater     trace_spapr_xive_get_source_config(flags, lisn);
11804e960974SCédric Le Goater 
118123bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
118223bcd5ebSCédric Le Goater         return H_FUNCTION;
118323bcd5ebSCédric Le Goater     }
118423bcd5ebSCédric Le Goater 
118523bcd5ebSCédric Le Goater     if (flags) {
118623bcd5ebSCédric Le Goater         return H_PARAMETER;
118723bcd5ebSCédric Le Goater     }
118823bcd5ebSCédric Le Goater 
118923bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
119023bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
119123bcd5ebSCédric Le Goater                       lisn);
119223bcd5ebSCédric Le Goater         return H_P2;
119323bcd5ebSCédric Le Goater     }
119423bcd5ebSCédric Le Goater 
119523bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
119623bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
119723bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
119823bcd5ebSCédric Le Goater                       lisn);
119923bcd5ebSCédric Le Goater         return H_P2;
120023bcd5ebSCédric Le Goater     }
120123bcd5ebSCédric Le Goater 
120223bcd5ebSCédric Le Goater     /* EAS_END_BLOCK is unused on sPAPR */
120323bcd5ebSCédric Le Goater     end_idx = xive_get_field64(EAS_END_INDEX, eas.w);
120423bcd5ebSCédric Le Goater 
120523bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
120623bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
120723bcd5ebSCédric Le Goater 
120823bcd5ebSCédric Le Goater     nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
120923bcd5ebSCédric Le Goater     nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
121023bcd5ebSCédric Le Goater     args[0] = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
121123bcd5ebSCédric Le Goater 
121223bcd5ebSCédric Le Goater     if (xive_eas_is_masked(&eas)) {
121323bcd5ebSCédric Le Goater         args[1] = 0xff;
121423bcd5ebSCédric Le Goater     } else {
121523bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
121623bcd5ebSCédric Le Goater     }
121723bcd5ebSCédric Le Goater 
121823bcd5ebSCédric Le Goater     args[2] = xive_get_field64(EAS_END_DATA, eas.w);
121923bcd5ebSCédric Le Goater 
122023bcd5ebSCédric Le Goater     return H_SUCCESS;
122123bcd5ebSCédric Le Goater }
122223bcd5ebSCédric Le Goater 
122323bcd5ebSCédric Le Goater /*
122423bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
122523bcd5ebSCédric Le Goater  * address of the notification management page associated with the
122623bcd5ebSCédric Le Goater  * specified target and priority.
122723bcd5ebSCédric Le Goater  *
122823bcd5ebSCédric Le Goater  * Parameters:
122923bcd5ebSCédric Le Goater  * Input:
123023bcd5ebSCédric Le Goater  * - R4: "flags"
123123bcd5ebSCédric Le Goater  *         Bits 0-63 Reserved
123223bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
123323bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
123423bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
123523bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
123623bcd5ebSCédric Le Goater  *
123723bcd5ebSCédric Le Goater  * Output:
123823bcd5ebSCédric Le Goater  * - R4: Logical real address of notification page
123923bcd5ebSCédric Le Goater  * - R5: Power of 2 page size of the notification page
124023bcd5ebSCédric Le Goater  */
124123bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
1242ce2918cbSDavid Gibson                                          SpaprMachineState *spapr,
124323bcd5ebSCédric Le Goater                                          target_ulong opcode,
124423bcd5ebSCédric Le Goater                                          target_ulong *args)
124523bcd5ebSCédric Le Goater {
1246ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
124723bcd5ebSCédric Le Goater     XiveENDSource *end_xsrc = &xive->end_source;
124823bcd5ebSCédric Le Goater     target_ulong flags = args[0];
124923bcd5ebSCédric Le Goater     target_ulong target = args[1];
125023bcd5ebSCédric Le Goater     target_ulong priority = args[2];
125123bcd5ebSCédric Le Goater     XiveEND *end;
125223bcd5ebSCédric Le Goater     uint8_t end_blk;
125323bcd5ebSCédric Le Goater     uint32_t end_idx;
125423bcd5ebSCédric Le Goater 
12554e960974SCédric Le Goater     trace_spapr_xive_get_queue_info(flags, target, priority);
12564e960974SCédric Le Goater 
125723bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
125823bcd5ebSCédric Le Goater         return H_FUNCTION;
125923bcd5ebSCédric Le Goater     }
126023bcd5ebSCédric Le Goater 
126123bcd5ebSCédric Le Goater     if (flags) {
126223bcd5ebSCédric Le Goater         return H_PARAMETER;
126323bcd5ebSCédric Le Goater     }
126423bcd5ebSCédric Le Goater 
126523bcd5ebSCédric Le Goater     /*
126623bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
126723bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
126823bcd5ebSCédric Le Goater      */
126923bcd5ebSCédric Le Goater 
12704f311a70SCédric Le Goater     if (spapr_xive_priority_is_reserved(xive, priority)) {
127123bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
127223bcd5ebSCédric Le Goater                       " is reserved\n", priority);
127323bcd5ebSCédric Le Goater         return H_P3;
127423bcd5ebSCédric Le Goater     }
127523bcd5ebSCédric Le Goater 
127623bcd5ebSCédric Le Goater     /*
127723bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
127823bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
127923bcd5ebSCédric Le Goater      * target.
128023bcd5ebSCédric Le Goater      */
128123bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
128223bcd5ebSCédric Le Goater         return H_P2;
128323bcd5ebSCédric Le Goater     }
128423bcd5ebSCédric Le Goater 
128523bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
128623bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
128723bcd5ebSCédric Le Goater 
128823bcd5ebSCédric Le Goater     args[0] = xive->end_base + (1ull << (end_xsrc->esb_shift + 1)) * end_idx;
128923bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
129023bcd5ebSCédric Le Goater         args[1] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
129123bcd5ebSCédric Le Goater     } else {
129223bcd5ebSCédric Le Goater         args[1] = 0;
129323bcd5ebSCédric Le Goater     }
129423bcd5ebSCédric Le Goater 
129523bcd5ebSCédric Le Goater     return H_SUCCESS;
129623bcd5ebSCédric Le Goater }
129723bcd5ebSCédric Le Goater 
129823bcd5ebSCédric Le Goater /*
129923bcd5ebSCédric Le Goater  * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
130023bcd5ebSCédric Le Goater  * a given "target" and "priority".  It is also used to set the
130123bcd5ebSCédric Le Goater  * notification config associated with the EQ.  An EQ size of 0 is
130223bcd5ebSCédric Le Goater  * used to reset the EQ config for a given target and priority. If
130323bcd5ebSCédric Le Goater  * resetting the EQ config, the END associated with the given "target"
130423bcd5ebSCédric Le Goater  * and "priority" will be changed to disable queueing.
130523bcd5ebSCédric Le Goater  *
130623bcd5ebSCédric Le Goater  * Upon return from the hcall(), no additional interrupts will be
130723bcd5ebSCédric Le Goater  * directed to the old EQ (if one was set). The old EQ (if one was
130823bcd5ebSCédric Le Goater  * set) should be investigated for interrupts that occurred prior to
130923bcd5ebSCédric Le Goater  * or during the hcall().
131023bcd5ebSCédric Le Goater  *
131123bcd5ebSCédric Le Goater  * Parameters:
131223bcd5ebSCédric Le Goater  * Input:
131323bcd5ebSCédric Le Goater  * - R4: "flags"
131423bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
131523bcd5ebSCédric Le Goater  *         Bit 63: Unconditional Notify (n) per the XIVE spec
131623bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
131723bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
131823bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
131923bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
132023bcd5ebSCédric Le Goater  * - R7: "eventQueue": The logical real address of the start of the EQ
132123bcd5ebSCédric Le Goater  * - R8: "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
132223bcd5ebSCédric Le Goater  *
132323bcd5ebSCédric Le Goater  * Output:
132423bcd5ebSCédric Le Goater  * - None
132523bcd5ebSCédric Le Goater  */
132623bcd5ebSCédric Le Goater 
132723bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_ALWAYS_NOTIFY PPC_BIT(63)
132823bcd5ebSCédric Le Goater 
132923bcd5ebSCédric Le Goater static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
1330ce2918cbSDavid Gibson                                            SpaprMachineState *spapr,
133123bcd5ebSCédric Le Goater                                            target_ulong opcode,
133223bcd5ebSCédric Le Goater                                            target_ulong *args)
133323bcd5ebSCédric Le Goater {
1334ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
133523bcd5ebSCédric Le Goater     target_ulong flags = args[0];
133623bcd5ebSCédric Le Goater     target_ulong target = args[1];
133723bcd5ebSCédric Le Goater     target_ulong priority = args[2];
133823bcd5ebSCédric Le Goater     target_ulong qpage = args[3];
133923bcd5ebSCédric Le Goater     target_ulong qsize = args[4];
134023bcd5ebSCédric Le Goater     XiveEND end;
134123bcd5ebSCédric Le Goater     uint8_t end_blk, nvt_blk;
134223bcd5ebSCédric Le Goater     uint32_t end_idx, nvt_idx;
134323bcd5ebSCédric Le Goater 
13444e960974SCédric Le Goater     trace_spapr_xive_set_queue_config(flags, target, priority, qpage, qsize);
13454e960974SCédric Le Goater 
134623bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
134723bcd5ebSCédric Le Goater         return H_FUNCTION;
134823bcd5ebSCédric Le Goater     }
134923bcd5ebSCédric Le Goater 
135023bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_ALWAYS_NOTIFY) {
135123bcd5ebSCédric Le Goater         return H_PARAMETER;
135223bcd5ebSCédric Le Goater     }
135323bcd5ebSCédric Le Goater 
135423bcd5ebSCédric Le Goater     /*
135523bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
135623bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
135723bcd5ebSCédric Le Goater      */
135823bcd5ebSCédric Le Goater 
13594f311a70SCédric Le Goater     if (spapr_xive_priority_is_reserved(xive, priority)) {
136023bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
136123bcd5ebSCédric Le Goater                       " is reserved\n", priority);
136223bcd5ebSCédric Le Goater         return H_P3;
136323bcd5ebSCédric Le Goater     }
136423bcd5ebSCédric Le Goater 
136523bcd5ebSCédric Le Goater     /*
136623bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
136723bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
136823bcd5ebSCédric Le Goater      * target.
136923bcd5ebSCédric Le Goater      */
137023bcd5ebSCédric Le Goater 
137123bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
137223bcd5ebSCédric Le Goater         return H_P2;
137323bcd5ebSCédric Le Goater     }
137423bcd5ebSCédric Le Goater 
137523bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
137623bcd5ebSCédric Le Goater     memcpy(&end, &xive->endt[end_idx], sizeof(XiveEND));
137723bcd5ebSCédric Le Goater 
137823bcd5ebSCédric Le Goater     switch (qsize) {
137923bcd5ebSCédric Le Goater     case 12:
138023bcd5ebSCédric Le Goater     case 16:
138123bcd5ebSCédric Le Goater     case 21:
138223bcd5ebSCédric Le Goater     case 24:
13837f9136f9SCédric Le Goater         if (!QEMU_IS_ALIGNED(qpage, 1ul << qsize)) {
13847f9136f9SCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: EQ @0x%" HWADDR_PRIx
13857f9136f9SCédric Le Goater                           " is not naturally aligned with %" HWADDR_PRIx "\n",
13867f9136f9SCédric Le Goater                           qpage, (hwaddr)1 << qsize);
13877f9136f9SCédric Le Goater             return H_P4;
13887f9136f9SCédric Le Goater         }
138923bcd5ebSCédric Le Goater         end.w2 = cpu_to_be32((qpage >> 32) & 0x0fffffff);
139023bcd5ebSCédric Le Goater         end.w3 = cpu_to_be32(qpage & 0xffffffff);
139123bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_ENQUEUE);
139223bcd5ebSCédric Le Goater         end.w0 = xive_set_field32(END_W0_QSIZE, end.w0, qsize - 12);
139323bcd5ebSCédric Le Goater         break;
139423bcd5ebSCédric Le Goater     case 0:
139523bcd5ebSCédric Le Goater         /* reset queue and disable queueing */
139623bcd5ebSCédric Le Goater         spapr_xive_end_reset(&end);
139723bcd5ebSCédric Le Goater         goto out;
139823bcd5ebSCédric Le Goater 
139923bcd5ebSCédric Le Goater     default:
140023bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid EQ size %"PRIx64"\n",
140123bcd5ebSCédric Le Goater                       qsize);
140223bcd5ebSCédric Le Goater         return H_P5;
140323bcd5ebSCédric Le Goater     }
140423bcd5ebSCédric Le Goater 
140523bcd5ebSCédric Le Goater     if (qsize) {
140623bcd5ebSCédric Le Goater         hwaddr plen = 1 << qsize;
140723bcd5ebSCédric Le Goater         void *eq;
140823bcd5ebSCédric Le Goater 
140923bcd5ebSCédric Le Goater         /*
141023bcd5ebSCédric Le Goater          * Validate the guest EQ. We should also check that the queue
141123bcd5ebSCédric Le Goater          * has been zeroed by the OS.
141223bcd5ebSCédric Le Goater          */
141323bcd5ebSCédric Le Goater         eq = address_space_map(CPU(cpu)->as, qpage, &plen, true,
141423bcd5ebSCédric Le Goater                                MEMTXATTRS_UNSPECIFIED);
141523bcd5ebSCédric Le Goater         if (plen != 1 << qsize) {
141623bcd5ebSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to map EQ @0x%"
141723bcd5ebSCédric Le Goater                           HWADDR_PRIx "\n", qpage);
141823bcd5ebSCédric Le Goater             return H_P4;
141923bcd5ebSCédric Le Goater         }
142023bcd5ebSCédric Le Goater         address_space_unmap(CPU(cpu)->as, eq, plen, true, plen);
142123bcd5ebSCédric Le Goater     }
142223bcd5ebSCédric Le Goater 
142323bcd5ebSCédric Le Goater     /* "target" should have been validated above */
142423bcd5ebSCédric Le Goater     if (spapr_xive_target_to_nvt(target, &nvt_blk, &nvt_idx)) {
142523bcd5ebSCédric Le Goater         g_assert_not_reached();
142623bcd5ebSCédric Le Goater     }
142723bcd5ebSCédric Le Goater 
142823bcd5ebSCédric Le Goater     /*
142923bcd5ebSCédric Le Goater      * Ensure the priority and target are correctly set (they will not
143023bcd5ebSCédric Le Goater      * be right after allocation)
143123bcd5ebSCédric Le Goater      */
143223bcd5ebSCédric Le Goater     end.w6 = xive_set_field32(END_W6_NVT_BLOCK, 0ul, nvt_blk) |
143323bcd5ebSCédric Le Goater         xive_set_field32(END_W6_NVT_INDEX, 0ul, nvt_idx);
143423bcd5ebSCédric Le Goater     end.w7 = xive_set_field32(END_W7_F0_PRIORITY, 0ul, priority);
143523bcd5ebSCédric Le Goater 
143623bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_ALWAYS_NOTIFY) {
143723bcd5ebSCédric Le Goater         end.w0 |= cpu_to_be32(END_W0_UCOND_NOTIFY);
143823bcd5ebSCédric Le Goater     } else {
143923bcd5ebSCédric Le Goater         end.w0 &= cpu_to_be32((uint32_t)~END_W0_UCOND_NOTIFY);
144023bcd5ebSCédric Le Goater     }
144123bcd5ebSCédric Le Goater 
144223bcd5ebSCédric Le Goater     /*
144323bcd5ebSCédric Le Goater      * The generation bit for the END starts at 1 and The END page
144423bcd5ebSCédric Le Goater      * offset counter starts at 0.
144523bcd5ebSCédric Le Goater      */
144623bcd5ebSCédric Le Goater     end.w1 = cpu_to_be32(END_W1_GENERATION) |
144723bcd5ebSCédric Le Goater         xive_set_field32(END_W1_PAGE_OFF, 0ul, 0ul);
144823bcd5ebSCédric Le Goater     end.w0 |= cpu_to_be32(END_W0_VALID);
144923bcd5ebSCédric Le Goater 
145023bcd5ebSCédric Le Goater     /*
145123bcd5ebSCédric Le Goater      * TODO: issue syncs required to ensure all in-flight interrupts
145223bcd5ebSCédric Le Goater      * are complete on the old END
145323bcd5ebSCédric Le Goater      */
145423bcd5ebSCédric Le Goater 
145523bcd5ebSCédric Le Goater out:
1456e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
14570c575703SCédric Le Goater         Error *local_err = NULL;
14580c575703SCédric Le Goater 
14590c575703SCédric Le Goater         kvmppc_xive_set_queue_config(xive, end_blk, end_idx, &end, &local_err);
14600c575703SCédric Le Goater         if (local_err) {
14610c575703SCédric Le Goater             error_report_err(local_err);
14620c575703SCédric Le Goater             return H_HARDWARE;
14630c575703SCédric Le Goater         }
14640c575703SCédric Le Goater     }
14650c575703SCédric Le Goater 
146623bcd5ebSCédric Le Goater     /* Update END */
146723bcd5ebSCédric Le Goater     memcpy(&xive->endt[end_idx], &end, sizeof(XiveEND));
146823bcd5ebSCédric Le Goater     return H_SUCCESS;
146923bcd5ebSCédric Le Goater }
147023bcd5ebSCédric Le Goater 
147123bcd5ebSCédric Le Goater /*
147223bcd5ebSCédric Le Goater  * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
147323bcd5ebSCédric Le Goater  * target and priority.
147423bcd5ebSCédric Le Goater  *
147523bcd5ebSCédric Le Goater  * Parameters:
147623bcd5ebSCédric Le Goater  * Input:
147723bcd5ebSCédric Le Goater  * - R4: "flags"
147823bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
147923bcd5ebSCédric Le Goater  *         Bit 63: Debug: Return debug data
148023bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
148123bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
148223bcd5ebSCédric Le Goater  * - R6: "priority" is a valid priority not in
148323bcd5ebSCédric Le Goater  *       "ibm,plat-res-int-priorities"
148423bcd5ebSCédric Le Goater  *
148523bcd5ebSCédric Le Goater  * Output:
148623bcd5ebSCédric Le Goater  * - R4: "flags":
148723bcd5ebSCédric Le Goater  *       Bits 0-61: Reserved
148823bcd5ebSCédric Le Goater  *       Bit 62: The value of Event Queue Generation Number (g) per
148923bcd5ebSCédric Le Goater  *              the XIVE spec if "Debug" = 1
149023bcd5ebSCédric Le Goater  *       Bit 63: The value of Unconditional Notify (n) per the XIVE spec
149123bcd5ebSCédric Le Goater  * - R5: The logical real address of the start of the EQ
149223bcd5ebSCédric Le Goater  * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
149323bcd5ebSCédric Le Goater  * - R7: The value of Event Queue Offset Counter per XIVE spec
149423bcd5ebSCédric Le Goater  *       if "Debug" = 1, else 0
149523bcd5ebSCédric Le Goater  *
149623bcd5ebSCédric Le Goater  */
149723bcd5ebSCédric Le Goater 
149823bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_DEBUG     PPC_BIT(63)
149923bcd5ebSCédric Le Goater 
150023bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
1501ce2918cbSDavid Gibson                                            SpaprMachineState *spapr,
150223bcd5ebSCédric Le Goater                                            target_ulong opcode,
150323bcd5ebSCédric Le Goater                                            target_ulong *args)
150423bcd5ebSCédric Le Goater {
1505ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
150623bcd5ebSCédric Le Goater     target_ulong flags = args[0];
150723bcd5ebSCédric Le Goater     target_ulong target = args[1];
150823bcd5ebSCédric Le Goater     target_ulong priority = args[2];
150923bcd5ebSCédric Le Goater     XiveEND *end;
151023bcd5ebSCédric Le Goater     uint8_t end_blk;
151123bcd5ebSCédric Le Goater     uint32_t end_idx;
151223bcd5ebSCédric Le Goater 
15134e960974SCédric Le Goater     trace_spapr_xive_get_queue_config(flags, target, priority);
15144e960974SCédric Le Goater 
151523bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
151623bcd5ebSCédric Le Goater         return H_FUNCTION;
151723bcd5ebSCédric Le Goater     }
151823bcd5ebSCédric Le Goater 
151923bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_END_DEBUG) {
152023bcd5ebSCédric Le Goater         return H_PARAMETER;
152123bcd5ebSCédric Le Goater     }
152223bcd5ebSCédric Le Goater 
152323bcd5ebSCédric Le Goater     /*
152423bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
152523bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
152623bcd5ebSCédric Le Goater      */
152723bcd5ebSCédric Le Goater 
15284f311a70SCédric Le Goater     if (spapr_xive_priority_is_reserved(xive, priority)) {
152923bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
153023bcd5ebSCédric Le Goater                       " is reserved\n", priority);
153123bcd5ebSCédric Le Goater         return H_P3;
153223bcd5ebSCédric Le Goater     }
153323bcd5ebSCédric Le Goater 
153423bcd5ebSCédric Le Goater     /*
153523bcd5ebSCédric Le Goater      * Validate that "target" is part of the list of threads allocated
153623bcd5ebSCédric Le Goater      * to the partition. For that, find the END corresponding to the
153723bcd5ebSCédric Le Goater      * target.
153823bcd5ebSCédric Le Goater      */
153923bcd5ebSCédric Le Goater     if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
154023bcd5ebSCédric Le Goater         return H_P2;
154123bcd5ebSCédric Le Goater     }
154223bcd5ebSCédric Le Goater 
154323bcd5ebSCédric Le Goater     assert(end_idx < xive->nr_ends);
154423bcd5ebSCédric Le Goater     end = &xive->endt[end_idx];
154523bcd5ebSCédric Le Goater 
154623bcd5ebSCédric Le Goater     args[0] = 0;
154723bcd5ebSCédric Le Goater     if (xive_end_is_notify(end)) {
154823bcd5ebSCédric Le Goater         args[0] |= SPAPR_XIVE_END_ALWAYS_NOTIFY;
154923bcd5ebSCédric Le Goater     }
155023bcd5ebSCédric Le Goater 
155123bcd5ebSCédric Le Goater     if (xive_end_is_enqueue(end)) {
155213df9324SCédric Le Goater         args[1] = xive_end_qaddr(end);
155323bcd5ebSCédric Le Goater         args[2] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
155423bcd5ebSCédric Le Goater     } else {
155523bcd5ebSCédric Le Goater         args[1] = 0;
155623bcd5ebSCédric Le Goater         args[2] = 0;
155723bcd5ebSCédric Le Goater     }
155823bcd5ebSCédric Le Goater 
1559e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
15600c575703SCédric Le Goater         Error *local_err = NULL;
15610c575703SCédric Le Goater 
15620c575703SCédric Le Goater         kvmppc_xive_get_queue_config(xive, end_blk, end_idx, end, &local_err);
15630c575703SCédric Le Goater         if (local_err) {
15640c575703SCédric Le Goater             error_report_err(local_err);
15650c575703SCédric Le Goater             return H_HARDWARE;
15660c575703SCédric Le Goater         }
15670c575703SCédric Le Goater     }
15680c575703SCédric Le Goater 
156923bcd5ebSCédric Le Goater     /* TODO: do we need any locking on the END ? */
157023bcd5ebSCédric Le Goater     if (flags & SPAPR_XIVE_END_DEBUG) {
157123bcd5ebSCédric Le Goater         /* Load the event queue generation number into the return flags */
157223bcd5ebSCédric Le Goater         args[0] |= (uint64_t)xive_get_field32(END_W1_GENERATION, end->w1) << 62;
157323bcd5ebSCédric Le Goater 
157423bcd5ebSCédric Le Goater         /* Load R7 with the event queue offset counter */
157523bcd5ebSCédric Le Goater         args[3] = xive_get_field32(END_W1_PAGE_OFF, end->w1);
157623bcd5ebSCédric Le Goater     } else {
157723bcd5ebSCédric Le Goater         args[3] = 0;
157823bcd5ebSCédric Le Goater     }
157923bcd5ebSCédric Le Goater 
158023bcd5ebSCédric Le Goater     return H_SUCCESS;
158123bcd5ebSCédric Le Goater }
158223bcd5ebSCédric Le Goater 
158323bcd5ebSCédric Le Goater /*
158423bcd5ebSCédric Le Goater  * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
158523bcd5ebSCédric Le Goater  * reporting cache line pair for the calling thread.  The reporting
158623bcd5ebSCédric Le Goater  * cache lines will contain the OS interrupt context when the OS
158723bcd5ebSCédric Le Goater  * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
158823bcd5ebSCédric Le Goater  * interrupt. The reporting cache lines can be reset by inputting -1
158923bcd5ebSCédric Le Goater  * in "reportingLine".  Issuing the CI store byte without reporting
159023bcd5ebSCédric Le Goater  * cache lines registered will result in the data not being accessible
159123bcd5ebSCédric Le Goater  * to the OS.
159223bcd5ebSCédric Le Goater  *
159323bcd5ebSCédric Le Goater  * Parameters:
159423bcd5ebSCédric Le Goater  * Input:
159523bcd5ebSCédric Le Goater  * - R4: "flags"
159623bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
159723bcd5ebSCédric Le Goater  * - R5: "reportingLine": The logical real address of the reporting cache
159823bcd5ebSCédric Le Goater  *       line pair
159923bcd5ebSCédric Le Goater  *
160023bcd5ebSCédric Le Goater  * Output:
160123bcd5ebSCédric Le Goater  * - None
160223bcd5ebSCédric Le Goater  */
160323bcd5ebSCédric Le Goater static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
1604ce2918cbSDavid Gibson                                                 SpaprMachineState *spapr,
160523bcd5ebSCédric Le Goater                                                 target_ulong opcode,
160623bcd5ebSCédric Le Goater                                                 target_ulong *args)
160723bcd5ebSCédric Le Goater {
16084e960974SCédric Le Goater     target_ulong flags   = args[0];
16094e960974SCédric Le Goater 
16104e960974SCédric Le Goater     trace_spapr_xive_set_os_reporting_line(flags);
16114e960974SCédric Le Goater 
161223bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
161323bcd5ebSCédric Le Goater         return H_FUNCTION;
161423bcd5ebSCédric Le Goater     }
161523bcd5ebSCédric Le Goater 
161623bcd5ebSCédric Le Goater     /*
161723bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
161823bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
161923bcd5ebSCédric Le Goater      */
162023bcd5ebSCédric Le Goater 
162123bcd5ebSCédric Le Goater     /* TODO: H_INT_SET_OS_REPORTING_LINE */
162223bcd5ebSCédric Le Goater     return H_FUNCTION;
162323bcd5ebSCédric Le Goater }
162423bcd5ebSCédric Le Goater 
162523bcd5ebSCédric Le Goater /*
162623bcd5ebSCédric Le Goater  * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
162723bcd5ebSCédric Le Goater  * real address of the reporting cache line pair set for the input
162823bcd5ebSCédric Le Goater  * "target".  If no reporting cache line pair has been set, -1 is
162923bcd5ebSCédric Le Goater  * returned.
163023bcd5ebSCédric Le Goater  *
163123bcd5ebSCédric Le Goater  * Parameters:
163223bcd5ebSCédric Le Goater  * Input:
163323bcd5ebSCédric Le Goater  * - R4: "flags"
163423bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
163523bcd5ebSCédric Le Goater  * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
163623bcd5ebSCédric Le Goater  *       "ibm,ppc-interrupt-gserver#s"
163723bcd5ebSCédric Le Goater  * - R6: "reportingLine": The logical real address of the reporting
163823bcd5ebSCédric Le Goater  *        cache line pair
163923bcd5ebSCédric Le Goater  *
164023bcd5ebSCédric Le Goater  * Output:
164123bcd5ebSCédric Le Goater  * - R4: The logical real address of the reporting line if set, else -1
164223bcd5ebSCédric Le Goater  */
164323bcd5ebSCédric Le Goater static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
1644ce2918cbSDavid Gibson                                                 SpaprMachineState *spapr,
164523bcd5ebSCédric Le Goater                                                 target_ulong opcode,
164623bcd5ebSCédric Le Goater                                                 target_ulong *args)
164723bcd5ebSCédric Le Goater {
16484e960974SCédric Le Goater     target_ulong flags   = args[0];
16494e960974SCédric Le Goater 
16504e960974SCédric Le Goater     trace_spapr_xive_get_os_reporting_line(flags);
16514e960974SCédric Le Goater 
165223bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
165323bcd5ebSCédric Le Goater         return H_FUNCTION;
165423bcd5ebSCédric Le Goater     }
165523bcd5ebSCédric Le Goater 
165623bcd5ebSCédric Le Goater     /*
165723bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
165823bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
165923bcd5ebSCédric Le Goater      */
166023bcd5ebSCédric Le Goater 
166123bcd5ebSCédric Le Goater     /* TODO: H_INT_GET_OS_REPORTING_LINE */
166223bcd5ebSCédric Le Goater     return H_FUNCTION;
166323bcd5ebSCédric Le Goater }
166423bcd5ebSCédric Le Goater 
166523bcd5ebSCédric Le Goater /*
166623bcd5ebSCédric Le Goater  * The H_INT_ESB hcall() is used to issue a load or store to the ESB
166723bcd5ebSCédric Le Goater  * page for the input "lisn".  This hcall is only supported for LISNs
166823bcd5ebSCédric Le Goater  * that have the ESB hcall flag set to 1 when returned from hcall()
166923bcd5ebSCédric Le Goater  * H_INT_GET_SOURCE_INFO.
167023bcd5ebSCédric Le Goater  *
167123bcd5ebSCédric Le Goater  * Parameters:
167223bcd5ebSCédric Le Goater  * Input:
167323bcd5ebSCédric Le Goater  * - R4: "flags"
167423bcd5ebSCédric Le Goater  *         Bits 0-62: Reserved
167523bcd5ebSCédric Le Goater  *         bit 63: Store: Store=1, store operation, else load operation
167623bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
167723bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
167823bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
167923bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
168023bcd5ebSCédric Le Goater  * - R6: "esbOffset" is the offset into the ESB page for the load or
168123bcd5ebSCédric Le Goater  *       store operation
168223bcd5ebSCédric Le Goater  * - R7: "storeData" is the data to write for a store operation
168323bcd5ebSCédric Le Goater  *
168423bcd5ebSCédric Le Goater  * Output:
168523bcd5ebSCédric Le Goater  * - R4: The value of the load if load operation, else -1
168623bcd5ebSCédric Le Goater  */
168723bcd5ebSCédric Le Goater 
168823bcd5ebSCédric Le Goater #define SPAPR_XIVE_ESB_STORE PPC_BIT(63)
168923bcd5ebSCédric Le Goater 
169023bcd5ebSCédric Le Goater static target_ulong h_int_esb(PowerPCCPU *cpu,
1691ce2918cbSDavid Gibson                               SpaprMachineState *spapr,
169223bcd5ebSCédric Le Goater                               target_ulong opcode,
169323bcd5ebSCédric Le Goater                               target_ulong *args)
169423bcd5ebSCédric Le Goater {
1695ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
169623bcd5ebSCédric Le Goater     XiveEAS eas;
169723bcd5ebSCédric Le Goater     target_ulong flags  = args[0];
169823bcd5ebSCédric Le Goater     target_ulong lisn   = args[1];
169923bcd5ebSCédric Le Goater     target_ulong offset = args[2];
170023bcd5ebSCédric Le Goater     target_ulong data   = args[3];
170123bcd5ebSCédric Le Goater     hwaddr mmio_addr;
170223bcd5ebSCédric Le Goater     XiveSource *xsrc = &xive->source;
170323bcd5ebSCédric Le Goater 
17044e960974SCédric Le Goater     trace_spapr_xive_esb(flags, lisn, offset, data);
17054e960974SCédric Le Goater 
170623bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
170723bcd5ebSCédric Le Goater         return H_FUNCTION;
170823bcd5ebSCédric Le Goater     }
170923bcd5ebSCédric Le Goater 
171023bcd5ebSCédric Le Goater     if (flags & ~SPAPR_XIVE_ESB_STORE) {
171123bcd5ebSCédric Le Goater         return H_PARAMETER;
171223bcd5ebSCédric Le Goater     }
171323bcd5ebSCédric Le Goater 
171423bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
171523bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
171623bcd5ebSCédric Le Goater                       lisn);
171723bcd5ebSCédric Le Goater         return H_P2;
171823bcd5ebSCédric Le Goater     }
171923bcd5ebSCédric Le Goater 
172023bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
172123bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
172223bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
172323bcd5ebSCédric Le Goater                       lisn);
172423bcd5ebSCédric Le Goater         return H_P2;
172523bcd5ebSCédric Le Goater     }
172623bcd5ebSCédric Le Goater 
172723bcd5ebSCédric Le Goater     if (offset > (1ull << xsrc->esb_shift)) {
172823bcd5ebSCédric Le Goater         return H_P3;
172923bcd5ebSCédric Le Goater     }
173023bcd5ebSCédric Le Goater 
1731e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
17320c575703SCédric Le Goater         args[0] = kvmppc_xive_esb_rw(xsrc, lisn, offset, data,
17330c575703SCédric Le Goater                                      flags & SPAPR_XIVE_ESB_STORE);
17340c575703SCédric Le Goater     } else {
173523bcd5ebSCédric Le Goater         mmio_addr = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn) + offset;
173623bcd5ebSCédric Le Goater 
173723bcd5ebSCédric Le Goater         if (dma_memory_rw(&address_space_memory, mmio_addr, &data, 8,
173823faf569SPhilippe Mathieu-Daudé                           (flags & SPAPR_XIVE_ESB_STORE),
173923faf569SPhilippe Mathieu-Daudé                           MEMTXATTRS_UNSPECIFIED)) {
174023bcd5ebSCédric Le Goater             qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to access ESB @0x%"
174123bcd5ebSCédric Le Goater                           HWADDR_PRIx "\n", mmio_addr);
174223bcd5ebSCédric Le Goater             return H_HARDWARE;
174323bcd5ebSCédric Le Goater         }
174423bcd5ebSCédric Le Goater         args[0] = (flags & SPAPR_XIVE_ESB_STORE) ? -1 : data;
17450c575703SCédric Le Goater     }
174623bcd5ebSCédric Le Goater     return H_SUCCESS;
174723bcd5ebSCédric Le Goater }
174823bcd5ebSCédric Le Goater 
174923bcd5ebSCédric Le Goater /*
175023bcd5ebSCédric Le Goater  * The H_INT_SYNC hcall() is used to issue hardware syncs that will
175123bcd5ebSCédric Le Goater  * ensure any in flight events for the input lisn are in the event
175223bcd5ebSCédric Le Goater  * queue.
175323bcd5ebSCédric Le Goater  *
175423bcd5ebSCédric Le Goater  * Parameters:
175523bcd5ebSCédric Le Goater  * Input:
175623bcd5ebSCédric Le Goater  * - R4: "flags"
175723bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
175823bcd5ebSCédric Le Goater  * - R5: "lisn" is per "interrupts", "interrupt-map", or
175923bcd5ebSCédric Le Goater  *       "ibm,xive-lisn-ranges" properties, or as returned by the
176023bcd5ebSCédric Le Goater  *       ibm,query-interrupt-source-number RTAS call, or as
176123bcd5ebSCédric Le Goater  *       returned by the H_ALLOCATE_VAS_WINDOW hcall
176223bcd5ebSCédric Le Goater  *
176323bcd5ebSCédric Le Goater  * Output:
176423bcd5ebSCédric Le Goater  * - None
176523bcd5ebSCédric Le Goater  */
176623bcd5ebSCédric Le Goater static target_ulong h_int_sync(PowerPCCPU *cpu,
1767ce2918cbSDavid Gibson                                SpaprMachineState *spapr,
176823bcd5ebSCédric Le Goater                                target_ulong opcode,
176923bcd5ebSCédric Le Goater                                target_ulong *args)
177023bcd5ebSCédric Le Goater {
1771ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
177223bcd5ebSCédric Le Goater     XiveEAS eas;
177323bcd5ebSCédric Le Goater     target_ulong flags = args[0];
177423bcd5ebSCédric Le Goater     target_ulong lisn = args[1];
177523bcd5ebSCédric Le Goater 
17764e960974SCédric Le Goater     trace_spapr_xive_sync(flags, lisn);
17774e960974SCédric Le Goater 
177823bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
177923bcd5ebSCédric Le Goater         return H_FUNCTION;
178023bcd5ebSCédric Le Goater     }
178123bcd5ebSCédric Le Goater 
178223bcd5ebSCédric Le Goater     if (flags) {
178323bcd5ebSCédric Le Goater         return H_PARAMETER;
178423bcd5ebSCédric Le Goater     }
178523bcd5ebSCédric Le Goater 
178623bcd5ebSCédric Le Goater     if (lisn >= xive->nr_irqs) {
178723bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
178823bcd5ebSCédric Le Goater                       lisn);
178923bcd5ebSCédric Le Goater         return H_P2;
179023bcd5ebSCédric Le Goater     }
179123bcd5ebSCédric Le Goater 
179223bcd5ebSCédric Le Goater     eas = xive->eat[lisn];
179323bcd5ebSCédric Le Goater     if (!xive_eas_is_valid(&eas)) {
179423bcd5ebSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
179523bcd5ebSCédric Le Goater                       lisn);
179623bcd5ebSCédric Le Goater         return H_P2;
179723bcd5ebSCédric Le Goater     }
179823bcd5ebSCédric Le Goater 
179923bcd5ebSCédric Le Goater     /*
180023bcd5ebSCédric Le Goater      * H_STATE should be returned if a H_INT_RESET is in progress.
180123bcd5ebSCédric Le Goater      * This is not needed when running the emulation under QEMU
180223bcd5ebSCédric Le Goater      */
180323bcd5ebSCédric Le Goater 
18040c575703SCédric Le Goater     /*
18050c575703SCédric Le Goater      * This is not real hardware. Nothing to be done unless when
18060c575703SCédric Le Goater      * under KVM
18070c575703SCédric Le Goater      */
18080c575703SCédric Le Goater 
1809e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
18100c575703SCédric Le Goater         Error *local_err = NULL;
18110c575703SCédric Le Goater 
18120c575703SCédric Le Goater         kvmppc_xive_sync_source(xive, lisn, &local_err);
18130c575703SCédric Le Goater         if (local_err) {
18140c575703SCédric Le Goater             error_report_err(local_err);
18150c575703SCédric Le Goater             return H_HARDWARE;
18160c575703SCédric Le Goater         }
18170c575703SCédric Le Goater     }
181823bcd5ebSCédric Le Goater     return H_SUCCESS;
181923bcd5ebSCédric Le Goater }
182023bcd5ebSCédric Le Goater 
182123bcd5ebSCédric Le Goater /*
182223bcd5ebSCédric Le Goater  * The H_INT_RESET hcall() is used to reset all of the partition's
182323bcd5ebSCédric Le Goater  * interrupt exploitation structures to their initial state.  This
182423bcd5ebSCédric Le Goater  * means losing all previously set interrupt state set via
182523bcd5ebSCédric Le Goater  * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
182623bcd5ebSCédric Le Goater  *
182723bcd5ebSCédric Le Goater  * Parameters:
182823bcd5ebSCédric Le Goater  * Input:
182923bcd5ebSCédric Le Goater  * - R4: "flags"
183023bcd5ebSCédric Le Goater  *         Bits 0-63: Reserved
183123bcd5ebSCédric Le Goater  *
183223bcd5ebSCédric Le Goater  * Output:
183323bcd5ebSCédric Le Goater  * - None
183423bcd5ebSCédric Le Goater  */
183523bcd5ebSCédric Le Goater static target_ulong h_int_reset(PowerPCCPU *cpu,
1836ce2918cbSDavid Gibson                                 SpaprMachineState *spapr,
183723bcd5ebSCédric Le Goater                                 target_ulong opcode,
183823bcd5ebSCédric Le Goater                                 target_ulong *args)
183923bcd5ebSCédric Le Goater {
1840ce2918cbSDavid Gibson     SpaprXive *xive = spapr->xive;
184123bcd5ebSCédric Le Goater     target_ulong flags   = args[0];
184223bcd5ebSCédric Le Goater 
18434e960974SCédric Le Goater     trace_spapr_xive_reset(flags);
18444e960974SCédric Le Goater 
184523bcd5ebSCédric Le Goater     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
184623bcd5ebSCédric Le Goater         return H_FUNCTION;
184723bcd5ebSCédric Le Goater     }
184823bcd5ebSCédric Le Goater 
184923bcd5ebSCédric Le Goater     if (flags) {
185023bcd5ebSCédric Le Goater         return H_PARAMETER;
185123bcd5ebSCédric Le Goater     }
185223bcd5ebSCédric Le Goater 
1853b2df46fdSPeter Maydell     device_cold_reset(DEVICE(xive));
18540c575703SCédric Le Goater 
1855e519cdd9SGreg Kurz     if (spapr_xive_in_kernel(xive)) {
18560c575703SCédric Le Goater         Error *local_err = NULL;
18570c575703SCédric Le Goater 
18580c575703SCédric Le Goater         kvmppc_xive_reset(xive, &local_err);
18590c575703SCédric Le Goater         if (local_err) {
18600c575703SCédric Le Goater             error_report_err(local_err);
18610c575703SCédric Le Goater             return H_HARDWARE;
18620c575703SCédric Le Goater         }
18630c575703SCédric Le Goater     }
186423bcd5ebSCédric Le Goater     return H_SUCCESS;
186523bcd5ebSCédric Le Goater }
186623bcd5ebSCédric Le Goater 
1867ce2918cbSDavid Gibson void spapr_xive_hcall_init(SpaprMachineState *spapr)
186823bcd5ebSCédric Le Goater {
186923bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
187023bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
187123bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
187223bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
187323bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
187423bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
187523bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
187623bcd5ebSCédric Le Goater                              h_int_set_os_reporting_line);
187723bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
187823bcd5ebSCédric Le Goater                              h_int_get_os_reporting_line);
187923bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_ESB, h_int_esb);
188023bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_SYNC, h_int_sync);
188123bcd5ebSCédric Le Goater     spapr_register_hypercall(H_INT_RESET, h_int_reset);
188223bcd5ebSCédric Le Goater }
1883