13aa597f6SCédric Le Goater /*
23aa597f6SCédric Le Goater * QEMU PowerPC sPAPR XIVE interrupt controller model
33aa597f6SCédric Le Goater *
41a3cc120SFrederic Barrat * Copyright (c) 2017-2024, IBM Corporation.
53aa597f6SCédric Le Goater *
61a3cc120SFrederic Barrat * SPDX-License-Identifier: GPL-2.0-or-later
73aa597f6SCédric Le Goater */
83aa597f6SCédric Le Goater
93aa597f6SCédric Le Goater #include "qemu/osdep.h"
103aa597f6SCédric Le Goater #include "qemu/log.h"
110b8fa32fSMarkus Armbruster #include "qemu/module.h"
123aa597f6SCédric Le Goater #include "qapi/error.h"
133aa597f6SCédric Le Goater #include "qemu/error-report.h"
143aa597f6SCédric Le Goater #include "target/ppc/cpu.h"
1532cad1ffSPhilippe Mathieu-Daudé #include "system/cpus.h"
1632cad1ffSPhilippe Mathieu-Daudé #include "system/reset.h"
17d6454270SMarkus Armbruster #include "migration/vmstate.h"
186e21de4aSCédric Le Goater #include "hw/ppc/fdt.h"
193aa597f6SCédric Le Goater #include "hw/ppc/spapr.h"
20a28b9a5aSCédric Le Goater #include "hw/ppc/spapr_cpu_core.h"
213aa597f6SCédric Le Goater #include "hw/ppc/spapr_xive.h"
223aa597f6SCédric Le Goater #include "hw/ppc/xive.h"
233aa597f6SCédric Le Goater #include "hw/ppc/xive_regs.h"
24a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
254e960974SCédric Le Goater #include "trace.h"
263aa597f6SCédric Le Goater
273aa597f6SCédric Le Goater /*
289b4b4e51SMichael Tokarev * XIVE Virtualization Controller BAR and Thread Management BAR that we
293aa597f6SCédric Le Goater * use for the ESB pages and the TIMA pages
303aa597f6SCédric Le Goater */
313aa597f6SCédric Le Goater #define SPAPR_XIVE_VC_BASE 0x0006010000000000ull
323aa597f6SCédric Le Goater #define SPAPR_XIVE_TM_BASE 0x0006030203180000ull
333aa597f6SCédric Le Goater
343aa597f6SCédric Le Goater /*
350cddee8dSCédric Le Goater * The allocation of VP blocks is a complex operation in OPAL and the
360cddee8dSCédric Le Goater * VP identifiers have a relation with the number of HW chips, the
370cddee8dSCédric Le Goater * size of the VP blocks, VP grouping, etc. The QEMU sPAPR XIVE
380cddee8dSCédric Le Goater * controller model does not have the same constraints and can use a
390cddee8dSCédric Le Goater * simple mapping scheme of the CPU vcpu_id
400cddee8dSCédric Le Goater *
410cddee8dSCédric Le Goater * These identifiers are never returned to the OS.
420cddee8dSCédric Le Goater */
430cddee8dSCédric Le Goater
440cddee8dSCédric Le Goater #define SPAPR_XIVE_NVT_BASE 0x400
450cddee8dSCédric Le Goater
460cddee8dSCédric Le Goater /*
470cddee8dSCédric Le Goater * sPAPR NVT and END indexing helpers
480cddee8dSCédric Le Goater */
spapr_xive_nvt_to_target(uint8_t nvt_blk,uint32_t nvt_idx)490cddee8dSCédric Le Goater static uint32_t spapr_xive_nvt_to_target(uint8_t nvt_blk, uint32_t nvt_idx)
500cddee8dSCédric Le Goater {
510cddee8dSCédric Le Goater return nvt_idx - SPAPR_XIVE_NVT_BASE;
520cddee8dSCédric Le Goater }
530cddee8dSCédric Le Goater
spapr_xive_cpu_to_nvt(PowerPCCPU * cpu,uint8_t * out_nvt_blk,uint32_t * out_nvt_idx)5423bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_nvt(PowerPCCPU *cpu,
5523bcd5ebSCédric Le Goater uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
5623bcd5ebSCédric Le Goater {
5723bcd5ebSCédric Le Goater assert(cpu);
5823bcd5ebSCédric Le Goater
5923bcd5ebSCédric Le Goater if (out_nvt_blk) {
6023bcd5ebSCédric Le Goater *out_nvt_blk = SPAPR_XIVE_BLOCK_ID;
6123bcd5ebSCédric Le Goater }
6223bcd5ebSCédric Le Goater
6323bcd5ebSCédric Le Goater if (out_nvt_blk) {
6423bcd5ebSCédric Le Goater *out_nvt_idx = SPAPR_XIVE_NVT_BASE + cpu->vcpu_id;
6523bcd5ebSCédric Le Goater }
6623bcd5ebSCédric Le Goater }
6723bcd5ebSCédric Le Goater
spapr_xive_target_to_nvt(uint32_t target,uint8_t * out_nvt_blk,uint32_t * out_nvt_idx)6823bcd5ebSCédric Le Goater static int spapr_xive_target_to_nvt(uint32_t target,
6923bcd5ebSCédric Le Goater uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
7023bcd5ebSCédric Le Goater {
7123bcd5ebSCédric Le Goater PowerPCCPU *cpu = spapr_find_cpu(target);
7223bcd5ebSCédric Le Goater
7323bcd5ebSCédric Le Goater if (!cpu) {
7423bcd5ebSCédric Le Goater return -1;
7523bcd5ebSCédric Le Goater }
7623bcd5ebSCédric Le Goater
7723bcd5ebSCédric Le Goater spapr_xive_cpu_to_nvt(cpu, out_nvt_blk, out_nvt_idx);
7823bcd5ebSCédric Le Goater return 0;
7923bcd5ebSCédric Le Goater }
8023bcd5ebSCédric Le Goater
8123bcd5ebSCédric Le Goater /*
8223bcd5ebSCédric Le Goater * sPAPR END indexing uses a simple mapping of the CPU vcpu_id, 8
8323bcd5ebSCédric Le Goater * priorities per CPU
8423bcd5ebSCédric Le Goater */
spapr_xive_end_to_target(uint8_t end_blk,uint32_t end_idx,uint32_t * out_server,uint8_t * out_prio)850c575703SCédric Le Goater int spapr_xive_end_to_target(uint8_t end_blk, uint32_t end_idx,
860c575703SCédric Le Goater uint32_t *out_server, uint8_t *out_prio)
870c575703SCédric Le Goater {
880c575703SCédric Le Goater
890c575703SCédric Le Goater assert(end_blk == SPAPR_XIVE_BLOCK_ID);
900c575703SCédric Le Goater
910c575703SCédric Le Goater if (out_server) {
920c575703SCédric Le Goater *out_server = end_idx >> 3;
930c575703SCédric Le Goater }
940c575703SCédric Le Goater
950c575703SCédric Le Goater if (out_prio) {
960c575703SCédric Le Goater *out_prio = end_idx & 0x7;
970c575703SCédric Le Goater }
980c575703SCédric Le Goater return 0;
990c575703SCédric Le Goater }
1000c575703SCédric Le Goater
spapr_xive_cpu_to_end(PowerPCCPU * cpu,uint8_t prio,uint8_t * out_end_blk,uint32_t * out_end_idx)10123bcd5ebSCédric Le Goater static void spapr_xive_cpu_to_end(PowerPCCPU *cpu, uint8_t prio,
10223bcd5ebSCédric Le Goater uint8_t *out_end_blk, uint32_t *out_end_idx)
10323bcd5ebSCédric Le Goater {
10423bcd5ebSCédric Le Goater assert(cpu);
10523bcd5ebSCédric Le Goater
10623bcd5ebSCédric Le Goater if (out_end_blk) {
10723bcd5ebSCédric Le Goater *out_end_blk = SPAPR_XIVE_BLOCK_ID;
10823bcd5ebSCédric Le Goater }
10923bcd5ebSCédric Le Goater
11023bcd5ebSCédric Le Goater if (out_end_idx) {
11123bcd5ebSCédric Le Goater *out_end_idx = (cpu->vcpu_id << 3) + prio;
11223bcd5ebSCédric Le Goater }
11323bcd5ebSCédric Le Goater }
11423bcd5ebSCédric Le Goater
spapr_xive_target_to_end(uint32_t target,uint8_t prio,uint8_t * out_end_blk,uint32_t * out_end_idx)11523bcd5ebSCédric Le Goater static int spapr_xive_target_to_end(uint32_t target, uint8_t prio,
11623bcd5ebSCédric Le Goater uint8_t *out_end_blk, uint32_t *out_end_idx)
11723bcd5ebSCédric Le Goater {
11823bcd5ebSCédric Le Goater PowerPCCPU *cpu = spapr_find_cpu(target);
11923bcd5ebSCédric Le Goater
12023bcd5ebSCédric Le Goater if (!cpu) {
12123bcd5ebSCédric Le Goater return -1;
12223bcd5ebSCédric Le Goater }
12323bcd5ebSCédric Le Goater
12423bcd5ebSCédric Le Goater spapr_xive_cpu_to_end(cpu, prio, out_end_blk, out_end_idx);
12523bcd5ebSCédric Le Goater return 0;
12623bcd5ebSCédric Le Goater }
12723bcd5ebSCédric Le Goater
1280cddee8dSCédric Le Goater /*
1293aa597f6SCédric Le Goater * On sPAPR machines, use a simplified output for the XIVE END
1303aa597f6SCédric Le Goater * structure dumping only the information related to the OS EQ.
1313aa597f6SCédric Le Goater */
spapr_xive_end_pic_print_info(SpaprXive * xive,XiveEND * end,GString * buf)132ce2918cbSDavid Gibson static void spapr_xive_end_pic_print_info(SpaprXive *xive, XiveEND *end,
133950f1273SPhilippe Mathieu-Daudé GString *buf)
1343aa597f6SCédric Le Goater {
135fb2e8b51SCédric Le Goater uint64_t qaddr_base = xive_end_qaddr(end);
1363aa597f6SCédric Le Goater uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
1373aa597f6SCédric Le Goater uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
1383aa597f6SCédric Le Goater uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
1393aa597f6SCédric Le Goater uint32_t qentries = 1 << (qsize + 10);
1403aa597f6SCédric Le Goater uint32_t nvt = xive_get_field32(END_W6_NVT_INDEX, end->w6);
1413aa597f6SCédric Le Goater uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
1423aa597f6SCédric Le Goater
143950f1273SPhilippe Mathieu-Daudé g_string_append_printf(buf, "%3d/%d % 6d/%5d @%"PRIx64" ^%d",
1440cddee8dSCédric Le Goater spapr_xive_nvt_to_target(0, nvt),
145fb2e8b51SCédric Le Goater priority, qindex, qentries, qaddr_base, qgen);
1463aa597f6SCédric Le Goater
147ace6fcdeSPhilippe Mathieu-Daudé xive_end_queue_pic_print_info(end, 6, buf);
1483aa597f6SCédric Le Goater }
1493aa597f6SCédric Le Goater
150e519cdd9SGreg Kurz /*
151e519cdd9SGreg Kurz * kvm_irqchip_in_kernel() will cause the compiler to turn this
152e519cdd9SGreg Kurz * info a nop if CONFIG_KVM isn't defined.
153e519cdd9SGreg Kurz */
154e519cdd9SGreg Kurz #define spapr_xive_in_kernel(xive) \
155e519cdd9SGreg Kurz (kvm_irqchip_in_kernel() && (xive)->fd != -1)
156e519cdd9SGreg Kurz
spapr_xive_pic_print_info(SpaprXive * xive,GString * buf)1574d62448cSPhilippe Mathieu-Daudé static void spapr_xive_pic_print_info(SpaprXive *xive, GString *buf)
1583aa597f6SCédric Le Goater {
1593aa597f6SCédric Le Goater XiveSource *xsrc = &xive->source;
1603aa597f6SCédric Le Goater int i;
1613aa597f6SCédric Le Goater
162e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
1637bfc759cSCédric Le Goater Error *local_err = NULL;
1647bfc759cSCédric Le Goater
1657bfc759cSCédric Le Goater kvmppc_xive_synchronize_state(xive, &local_err);
1667bfc759cSCédric Le Goater if (local_err) {
1677bfc759cSCédric Le Goater error_report_err(local_err);
1687bfc759cSCédric Le Goater return;
1697bfc759cSCédric Le Goater }
1707bfc759cSCédric Le Goater }
1717bfc759cSCédric Le Goater
1724d62448cSPhilippe Mathieu-Daudé g_string_append_printf(buf, " LISN PQ EISN CPU/PRIO EQ\n");
1733aa597f6SCédric Le Goater
1743aa597f6SCédric Le Goater for (i = 0; i < xive->nr_irqs; i++) {
1753aa597f6SCédric Le Goater uint8_t pq = xive_source_esb_get(xsrc, i);
1763aa597f6SCédric Le Goater XiveEAS *eas = &xive->eat[i];
1773aa597f6SCédric Le Goater
1783aa597f6SCédric Le Goater if (!xive_eas_is_valid(eas)) {
1793aa597f6SCédric Le Goater continue;
1803aa597f6SCédric Le Goater }
1813aa597f6SCédric Le Goater
1824d62448cSPhilippe Mathieu-Daudé g_string_append_printf(buf, " %08x %s %c%c%c %s %08x ", i,
1833aa597f6SCédric Le Goater xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
1843aa597f6SCédric Le Goater pq & XIVE_ESB_VAL_P ? 'P' : '-',
1853aa597f6SCédric Le Goater pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
186621f70d2SCédric Le Goater xive_source_is_asserted(xsrc, i) ? 'A' : ' ',
1873aa597f6SCédric Le Goater xive_eas_is_masked(eas) ? "M" : " ",
1883aa597f6SCédric Le Goater (int) xive_get_field64(EAS_END_DATA, eas->w));
1893aa597f6SCédric Le Goater
1903aa597f6SCédric Le Goater if (!xive_eas_is_masked(eas)) {
1913aa597f6SCédric Le Goater uint32_t end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
1923aa597f6SCédric Le Goater XiveEND *end;
1933aa597f6SCédric Le Goater
1943aa597f6SCédric Le Goater assert(end_idx < xive->nr_ends);
1953aa597f6SCédric Le Goater end = &xive->endt[end_idx];
1963aa597f6SCédric Le Goater
1973aa597f6SCédric Le Goater if (xive_end_is_valid(end)) {
198950f1273SPhilippe Mathieu-Daudé spapr_xive_end_pic_print_info(xive, end, buf);
1993aa597f6SCédric Le Goater }
200950f1273SPhilippe Mathieu-Daudé
2013aa597f6SCédric Le Goater }
2024d62448cSPhilippe Mathieu-Daudé g_string_append_c(buf, '\n');
2033aa597f6SCédric Le Goater }
2043aa597f6SCédric Le Goater }
2053aa597f6SCédric Le Goater
spapr_xive_mmio_set_enabled(SpaprXive * xive,bool enable)206ce2918cbSDavid Gibson void spapr_xive_mmio_set_enabled(SpaprXive *xive, bool enable)
2073a8eb78eSCédric Le Goater {
2083a8eb78eSCédric Le Goater memory_region_set_enabled(&xive->source.esb_mmio, enable);
2093a8eb78eSCédric Le Goater memory_region_set_enabled(&xive->tm_mmio, enable);
2103a8eb78eSCédric Le Goater
2113a8eb78eSCédric Le Goater /* Disable the END ESBs until a guest OS makes use of them */
2123a8eb78eSCédric Le Goater memory_region_set_enabled(&xive->end_source.esb_mmio, false);
2133a8eb78eSCédric Le Goater }
2143a8eb78eSCédric Le Goater
spapr_xive_tm_write(void * opaque,hwaddr offset,uint64_t value,unsigned size)215d024a2c1SCédric Le Goater static void spapr_xive_tm_write(void *opaque, hwaddr offset,
216d024a2c1SCédric Le Goater uint64_t value, unsigned size)
217d024a2c1SCédric Le Goater {
218d024a2c1SCédric Le Goater XiveTCTX *tctx = spapr_cpu_state(POWERPC_CPU(current_cpu))->tctx;
219d024a2c1SCédric Le Goater
220d024a2c1SCédric Le Goater xive_tctx_tm_write(XIVE_PRESENTER(opaque), tctx, offset, value, size);
221d024a2c1SCédric Le Goater }
222d024a2c1SCédric Le Goater
spapr_xive_tm_read(void * opaque,hwaddr offset,unsigned size)223d024a2c1SCédric Le Goater static uint64_t spapr_xive_tm_read(void *opaque, hwaddr offset, unsigned size)
224d024a2c1SCédric Le Goater {
225d024a2c1SCédric Le Goater XiveTCTX *tctx = spapr_cpu_state(POWERPC_CPU(current_cpu))->tctx;
226d024a2c1SCédric Le Goater
227d024a2c1SCédric Le Goater return xive_tctx_tm_read(XIVE_PRESENTER(opaque), tctx, offset, size);
228d024a2c1SCédric Le Goater }
229d024a2c1SCédric Le Goater
230d024a2c1SCédric Le Goater const MemoryRegionOps spapr_xive_tm_ops = {
231d024a2c1SCédric Le Goater .read = spapr_xive_tm_read,
232d024a2c1SCédric Le Goater .write = spapr_xive_tm_write,
233d024a2c1SCédric Le Goater .endianness = DEVICE_BIG_ENDIAN,
234d024a2c1SCédric Le Goater .valid = {
235d024a2c1SCédric Le Goater .min_access_size = 1,
236d024a2c1SCédric Le Goater .max_access_size = 8,
237d024a2c1SCédric Le Goater },
238d024a2c1SCédric Le Goater .impl = {
239d024a2c1SCédric Le Goater .min_access_size = 1,
240d024a2c1SCédric Le Goater .max_access_size = 8,
241d024a2c1SCédric Le Goater },
242d024a2c1SCédric Le Goater };
243d024a2c1SCédric Le Goater
spapr_xive_end_reset(XiveEND * end)2443aa597f6SCédric Le Goater static void spapr_xive_end_reset(XiveEND *end)
2453aa597f6SCédric Le Goater {
2463aa597f6SCédric Le Goater memset(end, 0, sizeof(*end));
2473aa597f6SCédric Le Goater
2483aa597f6SCédric Le Goater /* switch off the escalation and notification ESBs */
2493aa597f6SCédric Le Goater end->w1 = cpu_to_be32(END_W1_ESe_Q | END_W1_ESn_Q);
2503aa597f6SCédric Le Goater }
2513aa597f6SCédric Le Goater
spapr_xive_reset(void * dev)2523aa597f6SCédric Le Goater static void spapr_xive_reset(void *dev)
2533aa597f6SCédric Le Goater {
254ce2918cbSDavid Gibson SpaprXive *xive = SPAPR_XIVE(dev);
2553aa597f6SCédric Le Goater int i;
2563aa597f6SCédric Le Goater
2573aa597f6SCédric Le Goater /*
2583aa597f6SCédric Le Goater * The XiveSource has its own reset handler, which mask off all
2593aa597f6SCédric Le Goater * IRQs (!P|Q)
2603aa597f6SCédric Le Goater */
2613aa597f6SCédric Le Goater
2623aa597f6SCédric Le Goater /* Mask all valid EASs in the IRQ number space. */
2633aa597f6SCédric Le Goater for (i = 0; i < xive->nr_irqs; i++) {
2643aa597f6SCédric Le Goater XiveEAS *eas = &xive->eat[i];
2653aa597f6SCédric Le Goater if (xive_eas_is_valid(eas)) {
2663aa597f6SCédric Le Goater eas->w = cpu_to_be64(EAS_VALID | EAS_MASKED);
2673aa597f6SCédric Le Goater } else {
2683aa597f6SCédric Le Goater eas->w = 0;
2693aa597f6SCédric Le Goater }
2703aa597f6SCédric Le Goater }
2713aa597f6SCédric Le Goater
2723aa597f6SCédric Le Goater /* Clear all ENDs */
2733aa597f6SCédric Le Goater for (i = 0; i < xive->nr_ends; i++) {
2743aa597f6SCédric Le Goater spapr_xive_end_reset(&xive->endt[i]);
2753aa597f6SCédric Le Goater }
2763aa597f6SCédric Le Goater }
2773aa597f6SCédric Le Goater
spapr_xive_instance_init(Object * obj)2783aa597f6SCédric Le Goater static void spapr_xive_instance_init(Object *obj)
2793aa597f6SCédric Le Goater {
280ce2918cbSDavid Gibson SpaprXive *xive = SPAPR_XIVE(obj);
2813aa597f6SCédric Le Goater
2829fc7fc4dSMarkus Armbruster object_initialize_child(obj, "source", &xive->source, TYPE_XIVE_SOURCE);
2833aa597f6SCédric Le Goater
284f6d4dca8SThomas Huth object_initialize_child(obj, "end_source", &xive->end_source,
2859fc7fc4dSMarkus Armbruster TYPE_XIVE_END_SOURCE);
28638afd772SCédric Le Goater
28738afd772SCédric Le Goater /* Not connected to the KVM XIVE device */
28838afd772SCédric Le Goater xive->fd = -1;
2893aa597f6SCédric Le Goater }
2903aa597f6SCédric Le Goater
spapr_xive_realize(DeviceState * dev,Error ** errp)2913aa597f6SCédric Le Goater static void spapr_xive_realize(DeviceState *dev, Error **errp)
2923aa597f6SCédric Le Goater {
293ce2918cbSDavid Gibson SpaprXive *xive = SPAPR_XIVE(dev);
2946cc64796SGreg Kurz SpaprXiveClass *sxc = SPAPR_XIVE_GET_CLASS(xive);
2953aa597f6SCédric Le Goater XiveSource *xsrc = &xive->source;
2963aa597f6SCédric Le Goater XiveENDSource *end_xsrc = &xive->end_source;
2973aa597f6SCédric Le Goater Error *local_err = NULL;
2983aa597f6SCédric Le Goater
299484d774cSGreg Kurz /* Set by spapr_irq_init() */
300484d774cSGreg Kurz g_assert(xive->nr_irqs);
301484d774cSGreg Kurz g_assert(xive->nr_ends);
302484d774cSGreg Kurz
3036cc64796SGreg Kurz sxc->parent_realize(dev, &local_err);
3046cc64796SGreg Kurz if (local_err) {
3056cc64796SGreg Kurz error_propagate(errp, local_err);
3066cc64796SGreg Kurz return;
3076cc64796SGreg Kurz }
3086cc64796SGreg Kurz
3093aa597f6SCédric Le Goater /*
3103aa597f6SCédric Le Goater * Initialize the internal sources, for IPIs and virtual devices.
3113aa597f6SCédric Le Goater */
3125325cc34SMarkus Armbruster object_property_set_int(OBJECT(xsrc), "nr-irqs", xive->nr_irqs,
3133aa597f6SCédric Le Goater &error_fatal);
3145325cc34SMarkus Armbruster object_property_set_link(OBJECT(xsrc), "xive", OBJECT(xive), &error_abort);
315668f62ecSMarkus Armbruster if (!qdev_realize(DEVICE(xsrc), NULL, errp)) {
3163aa597f6SCédric Le Goater return;
3173aa597f6SCédric Le Goater }
3183aa597f6SCédric Le Goater
3193aa597f6SCédric Le Goater /*
3203aa597f6SCédric Le Goater * Initialize the END ESB source
3213aa597f6SCédric Le Goater */
3225325cc34SMarkus Armbruster object_property_set_int(OBJECT(end_xsrc), "nr-ends", xive->nr_irqs,
3233aa597f6SCédric Le Goater &error_fatal);
3245325cc34SMarkus Armbruster object_property_set_link(OBJECT(end_xsrc), "xive", OBJECT(xive),
3250ab2316eSGreg Kurz &error_abort);
326668f62ecSMarkus Armbruster if (!qdev_realize(DEVICE(end_xsrc), NULL, errp)) {
3273aa597f6SCédric Le Goater return;
3283aa597f6SCédric Le Goater }
3293aa597f6SCédric Le Goater
3303aa597f6SCédric Le Goater /* Set the mapping address of the END ESB pages after the source ESBs */
3313110f0eeSGreg Kurz xive->end_base = xive->vc_base + xive_source_esb_len(xsrc);
3323aa597f6SCédric Le Goater
3333aa597f6SCédric Le Goater /*
3343aa597f6SCédric Le Goater * Allocate the routing tables
3353aa597f6SCédric Le Goater */
3363aa597f6SCédric Le Goater xive->eat = g_new0(XiveEAS, xive->nr_irqs);
3373aa597f6SCédric Le Goater xive->endt = g_new0(XiveEND, xive->nr_ends);
3383aa597f6SCédric Le Goater
33938afd772SCédric Le Goater xive->nodename = g_strdup_printf("interrupt-controller@%" PRIx64,
34038afd772SCédric Le Goater xive->tm_base + XIVE_TM_USER_PAGE * (1 << TM_SHIFT));
34138afd772SCédric Le Goater
34238afd772SCédric Le Goater qemu_register_reset(spapr_xive_reset, dev);
343cdd71c8eSCédric Le Goater
3443aa597f6SCédric Le Goater /* TIMA initialization */
345d024a2c1SCédric Le Goater memory_region_init_io(&xive->tm_mmio, OBJECT(xive), &spapr_xive_tm_ops,
346d024a2c1SCédric Le Goater xive, "xive.tima", 4ull << TM_SHIFT);
3473aa597f6SCédric Le Goater
348981b1c62SCédric Le Goater /*
349981b1c62SCédric Le Goater * Map all regions. These will be enabled or disabled at reset and
350981b1c62SCédric Le Goater * can also be overridden by KVM memory regions if active
351981b1c62SCédric Le Goater */
3526c9dcd87SPhilippe Mathieu-Daudé memory_region_add_subregion(get_system_memory(), xive->vc_base,
3536c9dcd87SPhilippe Mathieu-Daudé &xsrc->esb_mmio);
3546c9dcd87SPhilippe Mathieu-Daudé memory_region_add_subregion(get_system_memory(), xive->end_base,
3556c9dcd87SPhilippe Mathieu-Daudé &end_xsrc->esb_mmio);
3566c9dcd87SPhilippe Mathieu-Daudé memory_region_add_subregion(get_system_memory(), xive->tm_base,
3576c9dcd87SPhilippe Mathieu-Daudé &xive->tm_mmio);
3583aa597f6SCédric Le Goater }
3593aa597f6SCédric Le Goater
spapr_xive_get_eas(XiveRouter * xrtr,uint8_t eas_blk,uint32_t eas_idx,XiveEAS * eas)3603aa597f6SCédric Le Goater static int spapr_xive_get_eas(XiveRouter *xrtr, uint8_t eas_blk,
3613aa597f6SCédric Le Goater uint32_t eas_idx, XiveEAS *eas)
3623aa597f6SCédric Le Goater {
363ce2918cbSDavid Gibson SpaprXive *xive = SPAPR_XIVE(xrtr);
3643aa597f6SCédric Le Goater
3653aa597f6SCédric Le Goater if (eas_idx >= xive->nr_irqs) {
3663aa597f6SCédric Le Goater return -1;
3673aa597f6SCédric Le Goater }
3683aa597f6SCédric Le Goater
3693aa597f6SCédric Le Goater *eas = xive->eat[eas_idx];
3703aa597f6SCédric Le Goater return 0;
3713aa597f6SCédric Le Goater }
3723aa597f6SCédric Le Goater
spapr_xive_get_end(XiveRouter * xrtr,uint8_t end_blk,uint32_t end_idx,XiveEND * end)3733aa597f6SCédric Le Goater static int spapr_xive_get_end(XiveRouter *xrtr,
3743aa597f6SCédric Le Goater uint8_t end_blk, uint32_t end_idx, XiveEND *end)
3753aa597f6SCédric Le Goater {
376ce2918cbSDavid Gibson SpaprXive *xive = SPAPR_XIVE(xrtr);
3773aa597f6SCédric Le Goater
3783aa597f6SCédric Le Goater if (end_idx >= xive->nr_ends) {
3793aa597f6SCédric Le Goater return -1;
3803aa597f6SCédric Le Goater }
3813aa597f6SCédric Le Goater
3823aa597f6SCédric Le Goater memcpy(end, &xive->endt[end_idx], sizeof(XiveEND));
3833aa597f6SCédric Le Goater return 0;
3843aa597f6SCédric Le Goater }
3853aa597f6SCédric Le Goater
spapr_xive_write_end(XiveRouter * xrtr,uint8_t end_blk,uint32_t end_idx,XiveEND * end,uint8_t word_number)3863aa597f6SCédric Le Goater static int spapr_xive_write_end(XiveRouter *xrtr, uint8_t end_blk,
3873aa597f6SCédric Le Goater uint32_t end_idx, XiveEND *end,
3883aa597f6SCédric Le Goater uint8_t word_number)
3893aa597f6SCédric Le Goater {
390ce2918cbSDavid Gibson SpaprXive *xive = SPAPR_XIVE(xrtr);
3913aa597f6SCédric Le Goater
3923aa597f6SCédric Le Goater if (end_idx >= xive->nr_ends) {
3933aa597f6SCédric Le Goater return -1;
3943aa597f6SCédric Le Goater }
3953aa597f6SCédric Le Goater
3963aa597f6SCédric Le Goater memcpy(&xive->endt[end_idx], end, sizeof(XiveEND));
3973aa597f6SCédric Le Goater return 0;
3983aa597f6SCédric Le Goater }
3993aa597f6SCédric Le Goater
spapr_xive_get_nvt(XiveRouter * xrtr,uint8_t nvt_blk,uint32_t nvt_idx,XiveNVT * nvt)4000cddee8dSCédric Le Goater static int spapr_xive_get_nvt(XiveRouter *xrtr,
4010cddee8dSCédric Le Goater uint8_t nvt_blk, uint32_t nvt_idx, XiveNVT *nvt)
4020cddee8dSCédric Le Goater {
4030cddee8dSCédric Le Goater uint32_t vcpu_id = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
4040cddee8dSCédric Le Goater PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
4050cddee8dSCédric Le Goater
4060cddee8dSCédric Le Goater if (!cpu) {
4070cddee8dSCédric Le Goater /* TODO: should we assert() if we can find a NVT ? */
4080cddee8dSCédric Le Goater return -1;
4090cddee8dSCédric Le Goater }
4100cddee8dSCédric Le Goater
4110cddee8dSCédric Le Goater /*
4120cddee8dSCédric Le Goater * sPAPR does not maintain a NVT table. Return that the NVT is
4130cddee8dSCédric Le Goater * valid if we have found a matching CPU
4140cddee8dSCédric Le Goater */
4150cddee8dSCédric Le Goater nvt->w0 = cpu_to_be32(NVT_W0_VALID);
4160cddee8dSCédric Le Goater return 0;
4170cddee8dSCédric Le Goater }
4180cddee8dSCédric Le Goater
spapr_xive_write_nvt(XiveRouter * xrtr,uint8_t nvt_blk,uint32_t nvt_idx,XiveNVT * nvt,uint8_t word_number)4190cddee8dSCédric Le Goater static int spapr_xive_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk,
4200cddee8dSCédric Le Goater uint32_t nvt_idx, XiveNVT *nvt,
4210cddee8dSCédric Le Goater uint8_t word_number)
4220cddee8dSCédric Le Goater {
4230cddee8dSCédric Le Goater /*
4240cddee8dSCédric Le Goater * We don't need to write back to the NVTs because the sPAPR
4250cddee8dSCédric Le Goater * machine should never hit a non-scheduled NVT. It should never
4260cddee8dSCédric Le Goater * get called.
4270cddee8dSCédric Le Goater */
4280cddee8dSCédric Le Goater g_assert_not_reached();
4290cddee8dSCédric Le Goater }
4300cddee8dSCédric Le Goater
spapr_xive_match_nvt(XivePresenter * xptr,uint8_t format,uint8_t nvt_blk,uint32_t nvt_idx,bool crowd,bool cam_ignore,uint8_t priority,uint32_t logic_serv,XiveTCTXMatch * match)431f87dae18SCédric Le Goater static int spapr_xive_match_nvt(XivePresenter *xptr, uint8_t format,
432f87dae18SCédric Le Goater uint8_t nvt_blk, uint32_t nvt_idx,
4331a3cc120SFrederic Barrat bool crowd, bool cam_ignore,
4341a3cc120SFrederic Barrat uint8_t priority,
435f87dae18SCédric Le Goater uint32_t logic_serv, XiveTCTXMatch *match)
436f87dae18SCédric Le Goater {
437f87dae18SCédric Le Goater CPUState *cs;
438f87dae18SCédric Le Goater int count = 0;
439f87dae18SCédric Le Goater
440f87dae18SCédric Le Goater CPU_FOREACH(cs) {
441f87dae18SCédric Le Goater PowerPCCPU *cpu = POWERPC_CPU(cs);
442f87dae18SCédric Le Goater XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx;
443f87dae18SCédric Le Goater int ring;
444f87dae18SCédric Le Goater
445f87dae18SCédric Le Goater /*
446f87dae18SCédric Le Goater * Skip partially initialized vCPUs. This can happen when
447f87dae18SCédric Le Goater * vCPUs are hotplugged.
448f87dae18SCédric Le Goater */
449f87dae18SCédric Le Goater if (!tctx) {
450f87dae18SCédric Le Goater continue;
451f87dae18SCédric Le Goater }
452f87dae18SCédric Le Goater
453f87dae18SCédric Le Goater /*
454f87dae18SCédric Le Goater * Check the thread context CAM lines and record matches.
455f87dae18SCédric Le Goater */
456f87dae18SCédric Le Goater ring = xive_presenter_tctx_match(xptr, tctx, format, nvt_blk, nvt_idx,
457f87dae18SCédric Le Goater cam_ignore, logic_serv);
458f87dae18SCédric Le Goater /*
459f87dae18SCédric Le Goater * Save the matching thread interrupt context and follow on to
460f87dae18SCédric Le Goater * check for duplicates which are invalid.
461f87dae18SCédric Le Goater */
462f87dae18SCédric Le Goater if (ring != -1) {
463f87dae18SCédric Le Goater if (match->tctx) {
464f87dae18SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: already found a thread "
465f87dae18SCédric Le Goater "context NVT %x/%x\n", nvt_blk, nvt_idx);
466f87dae18SCédric Le Goater return -1;
467f87dae18SCédric Le Goater }
468f87dae18SCédric Le Goater
469f87dae18SCédric Le Goater match->ring = ring;
470f87dae18SCédric Le Goater match->tctx = tctx;
471f87dae18SCédric Le Goater count++;
472f87dae18SCédric Le Goater }
473f87dae18SCédric Le Goater }
474f87dae18SCédric Le Goater
475f87dae18SCédric Le Goater return count;
476f87dae18SCédric Le Goater }
477f87dae18SCédric Le Goater
spapr_xive_presenter_get_config(XivePresenter * xptr)4782a24e6e3SFrederic Barrat static uint32_t spapr_xive_presenter_get_config(XivePresenter *xptr)
4792a24e6e3SFrederic Barrat {
4802a24e6e3SFrederic Barrat uint32_t cfg = 0;
4812a24e6e3SFrederic Barrat
4822a24e6e3SFrederic Barrat /*
4832a24e6e3SFrederic Barrat * Let's claim GEN1 TIMA format. If running with KVM on P10, the
4842a24e6e3SFrederic Barrat * correct answer is deep in the hardware and not accessible to
4852a24e6e3SFrederic Barrat * us. But it shouldn't matter as it only affects the presenter
4862a24e6e3SFrederic Barrat * as seen by a guest OS.
4872a24e6e3SFrederic Barrat */
4882a24e6e3SFrederic Barrat cfg |= XIVE_PRESENTER_GEN1_TIMA_OS;
4892a24e6e3SFrederic Barrat
4902a24e6e3SFrederic Barrat return cfg;
4912a24e6e3SFrederic Barrat }
4922a24e6e3SFrederic Barrat
spapr_xive_get_block_id(XiveRouter * xrtr)493f22f56ddSCédric Le Goater static uint8_t spapr_xive_get_block_id(XiveRouter *xrtr)
494f22f56ddSCédric Le Goater {
495f22f56ddSCédric Le Goater return SPAPR_XIVE_BLOCK_ID;
496f22f56ddSCédric Le Goater }
497f22f56ddSCédric Le Goater
spapr_xive_get_pq(XiveRouter * xrtr,uint8_t blk,uint32_t idx,uint8_t * pq)4980aa2612aSCédric Le Goater static int spapr_xive_get_pq(XiveRouter *xrtr, uint8_t blk, uint32_t idx,
4990aa2612aSCédric Le Goater uint8_t *pq)
5000aa2612aSCédric Le Goater {
5010aa2612aSCédric Le Goater SpaprXive *xive = SPAPR_XIVE(xrtr);
5020aa2612aSCédric Le Goater
5030aa2612aSCédric Le Goater assert(SPAPR_XIVE_BLOCK_ID == blk);
5040aa2612aSCédric Le Goater
5050aa2612aSCédric Le Goater *pq = xive_source_esb_get(&xive->source, idx);
5060aa2612aSCédric Le Goater return 0;
5070aa2612aSCédric Le Goater }
5080aa2612aSCédric Le Goater
spapr_xive_set_pq(XiveRouter * xrtr,uint8_t blk,uint32_t idx,uint8_t * pq)5090aa2612aSCédric Le Goater static int spapr_xive_set_pq(XiveRouter *xrtr, uint8_t blk, uint32_t idx,
5100aa2612aSCédric Le Goater uint8_t *pq)
5110aa2612aSCédric Le Goater {
5120aa2612aSCédric Le Goater SpaprXive *xive = SPAPR_XIVE(xrtr);
5130aa2612aSCédric Le Goater
5140aa2612aSCédric Le Goater assert(SPAPR_XIVE_BLOCK_ID == blk);
5150aa2612aSCédric Le Goater
5160aa2612aSCédric Le Goater *pq = xive_source_esb_set(&xive->source, idx, *pq);
5170aa2612aSCédric Le Goater return 0;
5180aa2612aSCédric Le Goater }
5190aa2612aSCédric Le Goater
5200aa2612aSCédric Le Goater
5213aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_end = {
5223aa597f6SCédric Le Goater .name = TYPE_SPAPR_XIVE "/end",
5233aa597f6SCédric Le Goater .version_id = 1,
5243aa597f6SCédric Le Goater .minimum_version_id = 1,
52545b1f81dSRichard Henderson .fields = (const VMStateField []) {
5263aa597f6SCédric Le Goater VMSTATE_UINT32(w0, XiveEND),
5273aa597f6SCédric Le Goater VMSTATE_UINT32(w1, XiveEND),
5283aa597f6SCédric Le Goater VMSTATE_UINT32(w2, XiveEND),
5293aa597f6SCédric Le Goater VMSTATE_UINT32(w3, XiveEND),
5303aa597f6SCédric Le Goater VMSTATE_UINT32(w4, XiveEND),
5313aa597f6SCédric Le Goater VMSTATE_UINT32(w5, XiveEND),
5323aa597f6SCédric Le Goater VMSTATE_UINT32(w6, XiveEND),
5333aa597f6SCédric Le Goater VMSTATE_UINT32(w7, XiveEND),
5343aa597f6SCédric Le Goater VMSTATE_END_OF_LIST()
5353aa597f6SCédric Le Goater },
5363aa597f6SCédric Le Goater };
5373aa597f6SCédric Le Goater
5383aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive_eas = {
5393aa597f6SCédric Le Goater .name = TYPE_SPAPR_XIVE "/eas",
5403aa597f6SCédric Le Goater .version_id = 1,
5413aa597f6SCédric Le Goater .minimum_version_id = 1,
54245b1f81dSRichard Henderson .fields = (const VMStateField []) {
5433aa597f6SCédric Le Goater VMSTATE_UINT64(w, XiveEAS),
5443aa597f6SCédric Le Goater VMSTATE_END_OF_LIST()
5453aa597f6SCédric Le Goater },
5463aa597f6SCédric Le Goater };
5473aa597f6SCédric Le Goater
vmstate_spapr_xive_pre_save(void * opaque)548277dd3d7SCédric Le Goater static int vmstate_spapr_xive_pre_save(void *opaque)
549277dd3d7SCédric Le Goater {
550e519cdd9SGreg Kurz SpaprXive *xive = SPAPR_XIVE(opaque);
551e519cdd9SGreg Kurz
552e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
553e519cdd9SGreg Kurz return kvmppc_xive_pre_save(xive);
554277dd3d7SCédric Le Goater }
555277dd3d7SCédric Le Goater
556277dd3d7SCédric Le Goater return 0;
557277dd3d7SCédric Le Goater }
558277dd3d7SCédric Le Goater
559277dd3d7SCédric Le Goater /*
560277dd3d7SCédric Le Goater * Called by the sPAPR IRQ backend 'post_load' method at the machine
561277dd3d7SCédric Le Goater * level.
562277dd3d7SCédric Le Goater */
spapr_xive_post_load(SpaprInterruptController * intc,int version_id)563605994e5SDavid Gibson static int spapr_xive_post_load(SpaprInterruptController *intc, int version_id)
564277dd3d7SCédric Le Goater {
565e519cdd9SGreg Kurz SpaprXive *xive = SPAPR_XIVE(intc);
566e519cdd9SGreg Kurz
567e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
568e519cdd9SGreg Kurz return kvmppc_xive_post_load(xive, version_id);
569277dd3d7SCédric Le Goater }
570277dd3d7SCédric Le Goater
571277dd3d7SCédric Le Goater return 0;
572277dd3d7SCédric Le Goater }
573277dd3d7SCédric Le Goater
5743aa597f6SCédric Le Goater static const VMStateDescription vmstate_spapr_xive = {
5753aa597f6SCédric Le Goater .name = TYPE_SPAPR_XIVE,
5763aa597f6SCédric Le Goater .version_id = 1,
5773aa597f6SCédric Le Goater .minimum_version_id = 1,
578277dd3d7SCédric Le Goater .pre_save = vmstate_spapr_xive_pre_save,
579277dd3d7SCédric Le Goater .post_load = NULL, /* handled at the machine level */
58045b1f81dSRichard Henderson .fields = (const VMStateField[]) {
581ce2918cbSDavid Gibson VMSTATE_UINT32_EQUAL(nr_irqs, SpaprXive, NULL),
582ce2918cbSDavid Gibson VMSTATE_STRUCT_VARRAY_POINTER_UINT32(eat, SpaprXive, nr_irqs,
5833aa597f6SCédric Le Goater vmstate_spapr_xive_eas, XiveEAS),
584ce2918cbSDavid Gibson VMSTATE_STRUCT_VARRAY_POINTER_UINT32(endt, SpaprXive, nr_ends,
5853aa597f6SCédric Le Goater vmstate_spapr_xive_end, XiveEND),
5863aa597f6SCédric Le Goater VMSTATE_END_OF_LIST()
5873aa597f6SCédric Le Goater },
5883aa597f6SCédric Le Goater };
5893aa597f6SCédric Le Goater
spapr_xive_claim_irq(SpaprInterruptController * intc,int lisn,bool lsi,Error ** errp)5900b0e52b1SDavid Gibson static int spapr_xive_claim_irq(SpaprInterruptController *intc, int lisn,
5910b0e52b1SDavid Gibson bool lsi, Error **errp)
5920b0e52b1SDavid Gibson {
5930b0e52b1SDavid Gibson SpaprXive *xive = SPAPR_XIVE(intc);
5940b0e52b1SDavid Gibson XiveSource *xsrc = &xive->source;
5950b0e52b1SDavid Gibson
5960b0e52b1SDavid Gibson assert(lisn < xive->nr_irqs);
5970b0e52b1SDavid Gibson
5984e960974SCédric Le Goater trace_spapr_xive_claim_irq(lisn, lsi);
5994e960974SCédric Le Goater
6000b0e52b1SDavid Gibson if (xive_eas_is_valid(&xive->eat[lisn])) {
6010b0e52b1SDavid Gibson error_setg(errp, "IRQ %d is not free", lisn);
6020b0e52b1SDavid Gibson return -EBUSY;
6030b0e52b1SDavid Gibson }
6040b0e52b1SDavid Gibson
6050b0e52b1SDavid Gibson /*
6060b0e52b1SDavid Gibson * Set default values when allocating an IRQ number
6070b0e52b1SDavid Gibson */
6080b0e52b1SDavid Gibson xive->eat[lisn].w |= cpu_to_be64(EAS_VALID | EAS_MASKED);
6090b0e52b1SDavid Gibson if (lsi) {
6100b0e52b1SDavid Gibson xive_source_irq_set_lsi(xsrc, lisn);
6110b0e52b1SDavid Gibson }
6120b0e52b1SDavid Gibson
613e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
6140b0e52b1SDavid Gibson return kvmppc_xive_source_reset_one(xsrc, lisn, errp);
6150b0e52b1SDavid Gibson }
6160b0e52b1SDavid Gibson
6170b0e52b1SDavid Gibson return 0;
6180b0e52b1SDavid Gibson }
6190b0e52b1SDavid Gibson
spapr_xive_free_irq(SpaprInterruptController * intc,int lisn)6200b0e52b1SDavid Gibson static void spapr_xive_free_irq(SpaprInterruptController *intc, int lisn)
6210b0e52b1SDavid Gibson {
6220b0e52b1SDavid Gibson SpaprXive *xive = SPAPR_XIVE(intc);
6230b0e52b1SDavid Gibson assert(lisn < xive->nr_irqs);
6240b0e52b1SDavid Gibson
6254e960974SCédric Le Goater trace_spapr_xive_free_irq(lisn);
6264e960974SCédric Le Goater
6270b0e52b1SDavid Gibson xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
6280b0e52b1SDavid Gibson }
6290b0e52b1SDavid Gibson
630783e3b21SRichard Henderson static const Property spapr_xive_properties[] = {
631ce2918cbSDavid Gibson DEFINE_PROP_UINT32("nr-irqs", SpaprXive, nr_irqs, 0),
632ce2918cbSDavid Gibson DEFINE_PROP_UINT32("nr-ends", SpaprXive, nr_ends, 0),
633ce2918cbSDavid Gibson DEFINE_PROP_UINT64("vc-base", SpaprXive, vc_base, SPAPR_XIVE_VC_BASE),
634ce2918cbSDavid Gibson DEFINE_PROP_UINT64("tm-base", SpaprXive, tm_base, SPAPR_XIVE_TM_BASE),
6354f311a70SCédric Le Goater DEFINE_PROP_UINT8("hv-prio", SpaprXive, hv_prio, 7),
6363aa597f6SCédric Le Goater };
6373aa597f6SCédric Le Goater
spapr_xive_cpu_intc_create(SpaprInterruptController * intc,PowerPCCPU * cpu,Error ** errp)638ebd6be08SDavid Gibson static int spapr_xive_cpu_intc_create(SpaprInterruptController *intc,
639ebd6be08SDavid Gibson PowerPCCPU *cpu, Error **errp)
640ebd6be08SDavid Gibson {
641ebd6be08SDavid Gibson SpaprXive *xive = SPAPR_XIVE(intc);
642ebd6be08SDavid Gibson Object *obj;
643ebd6be08SDavid Gibson SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
644ebd6be08SDavid Gibson
64547950946SCédric Le Goater obj = xive_tctx_create(OBJECT(cpu), XIVE_PRESENTER(xive), errp);
646ebd6be08SDavid Gibson if (!obj) {
647ebd6be08SDavid Gibson return -1;
648ebd6be08SDavid Gibson }
649ebd6be08SDavid Gibson
650ebd6be08SDavid Gibson spapr_cpu->tctx = XIVE_TCTX(obj);
651ebd6be08SDavid Gibson return 0;
652ebd6be08SDavid Gibson }
653ebd6be08SDavid Gibson
xive_tctx_set_os_cam(XiveTCTX * tctx,uint32_t os_cam)65497c00c54SCédric Le Goater static void xive_tctx_set_os_cam(XiveTCTX *tctx, uint32_t os_cam)
65597c00c54SCédric Le Goater {
65697c00c54SCédric Le Goater uint32_t qw1w2 = cpu_to_be32(TM_QW1W2_VO | os_cam);
65797c00c54SCédric Le Goater memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1w2, 4);
65897c00c54SCédric Le Goater }
65997c00c54SCédric Le Goater
spapr_xive_cpu_intc_reset(SpaprInterruptController * intc,PowerPCCPU * cpu)660d49e8a9bSCédric Le Goater static void spapr_xive_cpu_intc_reset(SpaprInterruptController *intc,
661d49e8a9bSCédric Le Goater PowerPCCPU *cpu)
662d49e8a9bSCédric Le Goater {
663d49e8a9bSCédric Le Goater XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx;
66497c00c54SCédric Le Goater uint8_t nvt_blk;
66597c00c54SCédric Le Goater uint32_t nvt_idx;
666d49e8a9bSCédric Le Goater
667d49e8a9bSCédric Le Goater xive_tctx_reset(tctx);
66897c00c54SCédric Le Goater
66997c00c54SCédric Le Goater /*
67097c00c54SCédric Le Goater * When a Virtual Processor is scheduled to run on a HW thread,
67197c00c54SCédric Le Goater * the hypervisor pushes its identifier in the OS CAM line.
67297c00c54SCédric Le Goater * Emulate the same behavior under QEMU.
67397c00c54SCédric Le Goater */
67497c00c54SCédric Le Goater spapr_xive_cpu_to_nvt(cpu, &nvt_blk, &nvt_idx);
67597c00c54SCédric Le Goater
67697c00c54SCédric Le Goater xive_tctx_set_os_cam(tctx, xive_nvt_cam_line(nvt_blk, nvt_idx));
677d49e8a9bSCédric Le Goater }
678d49e8a9bSCédric Le Goater
spapr_xive_cpu_intc_destroy(SpaprInterruptController * intc,PowerPCCPU * cpu)6790990ce6aSGreg Kurz static void spapr_xive_cpu_intc_destroy(SpaprInterruptController *intc,
6800990ce6aSGreg Kurz PowerPCCPU *cpu)
6810990ce6aSGreg Kurz {
6820990ce6aSGreg Kurz SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
6830990ce6aSGreg Kurz
6840990ce6aSGreg Kurz xive_tctx_destroy(spapr_cpu->tctx);
6850990ce6aSGreg Kurz spapr_cpu->tctx = NULL;
6860990ce6aSGreg Kurz }
6870990ce6aSGreg Kurz
spapr_xive_set_irq(SpaprInterruptController * intc,int irq,int val)6887bcdbccaSDavid Gibson static void spapr_xive_set_irq(SpaprInterruptController *intc, int irq, int val)
6897bcdbccaSDavid Gibson {
6907bcdbccaSDavid Gibson SpaprXive *xive = SPAPR_XIVE(intc);
6917bcdbccaSDavid Gibson
6924e960974SCédric Le Goater trace_spapr_xive_set_irq(irq, val);
6934e960974SCédric Le Goater
694e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
6957bcdbccaSDavid Gibson kvmppc_xive_source_set_irq(&xive->source, irq, val);
6967bcdbccaSDavid Gibson } else {
6977bcdbccaSDavid Gibson xive_source_set_irq(&xive->source, irq, val);
6987bcdbccaSDavid Gibson }
6997bcdbccaSDavid Gibson }
7007bcdbccaSDavid Gibson
spapr_xive_print_info(SpaprInterruptController * intc,GString * buf)7014abeadf6SPhilippe Mathieu-Daudé static void spapr_xive_print_info(SpaprInterruptController *intc, GString *buf)
702328d8eb2SDavid Gibson {
703328d8eb2SDavid Gibson SpaprXive *xive = SPAPR_XIVE(intc);
704328d8eb2SDavid Gibson CPUState *cs;
705328d8eb2SDavid Gibson
706328d8eb2SDavid Gibson CPU_FOREACH(cs) {
707328d8eb2SDavid Gibson PowerPCCPU *cpu = POWERPC_CPU(cs);
708328d8eb2SDavid Gibson
709f163e270SPhilippe Mathieu-Daudé xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, buf);
710328d8eb2SDavid Gibson }
7114d62448cSPhilippe Mathieu-Daudé spapr_xive_pic_print_info(xive, buf);
712328d8eb2SDavid Gibson }
713328d8eb2SDavid Gibson
spapr_xive_dt(SpaprInterruptController * intc,uint32_t nr_servers,void * fdt,uint32_t phandle)71405289273SDavid Gibson static void spapr_xive_dt(SpaprInterruptController *intc, uint32_t nr_servers,
71505289273SDavid Gibson void *fdt, uint32_t phandle)
71605289273SDavid Gibson {
71705289273SDavid Gibson SpaprXive *xive = SPAPR_XIVE(intc);
71805289273SDavid Gibson int node;
71905289273SDavid Gibson uint64_t timas[2 * 2];
72005289273SDavid Gibson /* Interrupt number ranges for the IPIs */
72105289273SDavid Gibson uint32_t lisn_ranges[] = {
72252d3403dSCédric Le Goater cpu_to_be32(SPAPR_IRQ_IPI),
72352d3403dSCédric Le Goater cpu_to_be32(SPAPR_IRQ_IPI + nr_servers),
72405289273SDavid Gibson };
72505289273SDavid Gibson /*
72605289273SDavid Gibson * EQ size - the sizes of pages supported by the system 4K, 64K,
72705289273SDavid Gibson * 2M, 16M. We only advertise 64K for the moment.
72805289273SDavid Gibson */
72905289273SDavid Gibson uint32_t eq_sizes[] = {
73005289273SDavid Gibson cpu_to_be32(16), /* 64K */
73105289273SDavid Gibson };
73205289273SDavid Gibson /*
7334f311a70SCédric Le Goater * QEMU/KVM only needs to define a single range to reserve the
7344f311a70SCédric Le Goater * escalation priority. A priority bitmask would have been more
7354f311a70SCédric Le Goater * appropriate.
73605289273SDavid Gibson */
73705289273SDavid Gibson uint32_t plat_res_int_priorities[] = {
7384f311a70SCédric Le Goater cpu_to_be32(xive->hv_prio), /* start */
7394f311a70SCédric Le Goater cpu_to_be32(0xff - xive->hv_prio), /* count */
74005289273SDavid Gibson };
74105289273SDavid Gibson
74205289273SDavid Gibson /* Thread Interrupt Management Area : User (ring 3) and OS (ring 2) */
74305289273SDavid Gibson timas[0] = cpu_to_be64(xive->tm_base +
74405289273SDavid Gibson XIVE_TM_USER_PAGE * (1ull << TM_SHIFT));
74505289273SDavid Gibson timas[1] = cpu_to_be64(1ull << TM_SHIFT);
74605289273SDavid Gibson timas[2] = cpu_to_be64(xive->tm_base +
74705289273SDavid Gibson XIVE_TM_OS_PAGE * (1ull << TM_SHIFT));
74805289273SDavid Gibson timas[3] = cpu_to_be64(1ull << TM_SHIFT);
74905289273SDavid Gibson
75005289273SDavid Gibson _FDT(node = fdt_add_subnode(fdt, 0, xive->nodename));
75105289273SDavid Gibson
75205289273SDavid Gibson _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
75305289273SDavid Gibson _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
75405289273SDavid Gibson
75505289273SDavid Gibson _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
75605289273SDavid Gibson _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
75705289273SDavid Gibson sizeof(eq_sizes)));
75805289273SDavid Gibson _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
75905289273SDavid Gibson sizeof(lisn_ranges)));
76005289273SDavid Gibson
76105289273SDavid Gibson /* For Linux to link the LSIs to the interrupt controller. */
76205289273SDavid Gibson _FDT(fdt_setprop(fdt, node, "interrupt-controller", NULL, 0));
76305289273SDavid Gibson _FDT(fdt_setprop_cell(fdt, node, "#interrupt-cells", 2));
76405289273SDavid Gibson
76505289273SDavid Gibson /* For SLOF */
76605289273SDavid Gibson _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
76705289273SDavid Gibson _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
76805289273SDavid Gibson
76905289273SDavid Gibson /*
77005289273SDavid Gibson * The "ibm,plat-res-int-priorities" property defines the priority
77105289273SDavid Gibson * ranges reserved by the hypervisor
77205289273SDavid Gibson */
77305289273SDavid Gibson _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
77405289273SDavid Gibson plat_res_int_priorities, sizeof(plat_res_int_priorities)));
77505289273SDavid Gibson }
77605289273SDavid Gibson
spapr_xive_activate(SpaprInterruptController * intc,uint32_t nr_servers,Error ** errp)7774ffb7496SGreg Kurz static int spapr_xive_activate(SpaprInterruptController *intc,
7784ffb7496SGreg Kurz uint32_t nr_servers, Error **errp)
779567192d4SDavid Gibson {
780567192d4SDavid Gibson SpaprXive *xive = SPAPR_XIVE(intc);
781567192d4SDavid Gibson
782567192d4SDavid Gibson if (kvm_enabled()) {
7834ffb7496SGreg Kurz int rc = spapr_irq_init_kvm(kvmppc_xive_connect, intc, nr_servers,
7844ffb7496SGreg Kurz errp);
785567192d4SDavid Gibson if (rc < 0) {
786567192d4SDavid Gibson return rc;
787567192d4SDavid Gibson }
788567192d4SDavid Gibson }
789567192d4SDavid Gibson
790567192d4SDavid Gibson /* Activate the XIVE MMIOs */
791567192d4SDavid Gibson spapr_xive_mmio_set_enabled(xive, true);
792567192d4SDavid Gibson
793567192d4SDavid Gibson return 0;
794567192d4SDavid Gibson }
795567192d4SDavid Gibson
spapr_xive_deactivate(SpaprInterruptController * intc)796567192d4SDavid Gibson static void spapr_xive_deactivate(SpaprInterruptController *intc)
797567192d4SDavid Gibson {
798567192d4SDavid Gibson SpaprXive *xive = SPAPR_XIVE(intc);
799567192d4SDavid Gibson
800567192d4SDavid Gibson spapr_xive_mmio_set_enabled(xive, false);
801567192d4SDavid Gibson
802e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
803567192d4SDavid Gibson kvmppc_xive_disconnect(intc);
804567192d4SDavid Gibson }
805567192d4SDavid Gibson }
806567192d4SDavid Gibson
spapr_xive_in_kernel_xptr(const XivePresenter * xptr)807e519cdd9SGreg Kurz static bool spapr_xive_in_kernel_xptr(const XivePresenter *xptr)
808e519cdd9SGreg Kurz {
809e519cdd9SGreg Kurz return spapr_xive_in_kernel(SPAPR_XIVE(xptr));
810e519cdd9SGreg Kurz }
811e519cdd9SGreg Kurz
spapr_xive_class_init(ObjectClass * klass,const void * data)81212d1a768SPhilippe Mathieu-Daudé static void spapr_xive_class_init(ObjectClass *klass, const void *data)
8133aa597f6SCédric Le Goater {
8143aa597f6SCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass);
8153aa597f6SCédric Le Goater XiveRouterClass *xrc = XIVE_ROUTER_CLASS(klass);
816ebd6be08SDavid Gibson SpaprInterruptControllerClass *sicc = SPAPR_INTC_CLASS(klass);
817f87dae18SCédric Le Goater XivePresenterClass *xpc = XIVE_PRESENTER_CLASS(klass);
8186cc64796SGreg Kurz SpaprXiveClass *sxc = SPAPR_XIVE_CLASS(klass);
8193aa597f6SCédric Le Goater
8203aa597f6SCédric Le Goater dc->desc = "sPAPR XIVE Interrupt Controller";
8214f67d30bSMarc-André Lureau device_class_set_props(dc, spapr_xive_properties);
8226cc64796SGreg Kurz device_class_set_parent_realize(dc, spapr_xive_realize,
8236cc64796SGreg Kurz &sxc->parent_realize);
8243aa597f6SCédric Le Goater dc->vmsd = &vmstate_spapr_xive;
8253aa597f6SCédric Le Goater
8263aa597f6SCédric Le Goater xrc->get_eas = spapr_xive_get_eas;
8270aa2612aSCédric Le Goater xrc->get_pq = spapr_xive_get_pq;
8280aa2612aSCédric Le Goater xrc->set_pq = spapr_xive_set_pq;
8293aa597f6SCédric Le Goater xrc->get_end = spapr_xive_get_end;
8303aa597f6SCédric Le Goater xrc->write_end = spapr_xive_write_end;
8310cddee8dSCédric Le Goater xrc->get_nvt = spapr_xive_get_nvt;
8320cddee8dSCédric Le Goater xrc->write_nvt = spapr_xive_write_nvt;
833f22f56ddSCédric Le Goater xrc->get_block_id = spapr_xive_get_block_id;
834ebd6be08SDavid Gibson
835567192d4SDavid Gibson sicc->activate = spapr_xive_activate;
836567192d4SDavid Gibson sicc->deactivate = spapr_xive_deactivate;
837ebd6be08SDavid Gibson sicc->cpu_intc_create = spapr_xive_cpu_intc_create;
838d49e8a9bSCédric Le Goater sicc->cpu_intc_reset = spapr_xive_cpu_intc_reset;
8390990ce6aSGreg Kurz sicc->cpu_intc_destroy = spapr_xive_cpu_intc_destroy;
8400b0e52b1SDavid Gibson sicc->claim_irq = spapr_xive_claim_irq;
8410b0e52b1SDavid Gibson sicc->free_irq = spapr_xive_free_irq;
8427bcdbccaSDavid Gibson sicc->set_irq = spapr_xive_set_irq;
843328d8eb2SDavid Gibson sicc->print_info = spapr_xive_print_info;
84405289273SDavid Gibson sicc->dt = spapr_xive_dt;
845605994e5SDavid Gibson sicc->post_load = spapr_xive_post_load;
846f87dae18SCédric Le Goater
847f87dae18SCédric Le Goater xpc->match_nvt = spapr_xive_match_nvt;
8482a24e6e3SFrederic Barrat xpc->get_config = spapr_xive_presenter_get_config;
849e519cdd9SGreg Kurz xpc->in_kernel = spapr_xive_in_kernel_xptr;
8503aa597f6SCédric Le Goater }
8513aa597f6SCédric Le Goater
8523aa597f6SCédric Le Goater static const TypeInfo spapr_xive_info = {
8533aa597f6SCédric Le Goater .name = TYPE_SPAPR_XIVE,
8543aa597f6SCédric Le Goater .parent = TYPE_XIVE_ROUTER,
8553aa597f6SCédric Le Goater .instance_init = spapr_xive_instance_init,
856ce2918cbSDavid Gibson .instance_size = sizeof(SpaprXive),
8573aa597f6SCédric Le Goater .class_init = spapr_xive_class_init,
8586cc64796SGreg Kurz .class_size = sizeof(SpaprXiveClass),
859*2cd09e47SPhilippe Mathieu-Daudé .interfaces = (const InterfaceInfo[]) {
860150e25f8SDavid Gibson { TYPE_SPAPR_INTC },
861150e25f8SDavid Gibson { }
862150e25f8SDavid Gibson },
8633aa597f6SCédric Le Goater };
8643aa597f6SCédric Le Goater
spapr_xive_register_types(void)8653aa597f6SCédric Le Goater static void spapr_xive_register_types(void)
8663aa597f6SCédric Le Goater {
8673aa597f6SCédric Le Goater type_register_static(&spapr_xive_info);
8683aa597f6SCédric Le Goater }
8693aa597f6SCédric Le Goater
type_init(spapr_xive_register_types)8703aa597f6SCédric Le Goater type_init(spapr_xive_register_types)
8713aa597f6SCédric Le Goater
87223bcd5ebSCédric Le Goater /*
87323bcd5ebSCédric Le Goater * XIVE hcalls
87423bcd5ebSCédric Le Goater *
87523bcd5ebSCédric Le Goater * The terminology used by the XIVE hcalls is the following :
87623bcd5ebSCédric Le Goater *
87723bcd5ebSCédric Le Goater * TARGET vCPU number
87823bcd5ebSCédric Le Goater * EQ Event Queue assigned by OS to receive event data
87923bcd5ebSCédric Le Goater * ESB page for source interrupt management
88023bcd5ebSCédric Le Goater * LISN Logical Interrupt Source Number identifying a source in the
88123bcd5ebSCédric Le Goater * machine
88223bcd5ebSCédric Le Goater * EISN Effective Interrupt Source Number used by guest OS to
88323bcd5ebSCédric Le Goater * identify source in the guest
88423bcd5ebSCédric Le Goater *
88523bcd5ebSCédric Le Goater * The EAS, END, NVT structures are not exposed.
88623bcd5ebSCédric Le Goater */
88723bcd5ebSCédric Le Goater
88823bcd5ebSCédric Le Goater /*
8894f311a70SCédric Le Goater * On POWER9, the KVM XIVE device uses priority 7 for the escalation
8904f311a70SCédric Le Goater * interrupts. So we only allow the guest to use priorities [0..6].
89123bcd5ebSCédric Le Goater */
8924f311a70SCédric Le Goater static bool spapr_xive_priority_is_reserved(SpaprXive *xive, uint8_t priority)
89323bcd5ebSCédric Le Goater {
8944f311a70SCédric Le Goater return priority >= xive->hv_prio;
89523bcd5ebSCédric Le Goater }
89623bcd5ebSCédric Le Goater
89723bcd5ebSCédric Le Goater /*
89823bcd5ebSCédric Le Goater * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical
89923bcd5ebSCédric Le Goater * real address of the MMIO page through which the Event State Buffer
90023bcd5ebSCédric Le Goater * entry associated with the value of the "lisn" parameter is managed.
90123bcd5ebSCédric Le Goater *
90223bcd5ebSCédric Le Goater * Parameters:
90323bcd5ebSCédric Le Goater * Input
90423bcd5ebSCédric Le Goater * - R4: "flags"
90523bcd5ebSCédric Le Goater * Bits 0-63 reserved
90623bcd5ebSCédric Le Goater * - R5: "lisn" is per "interrupts", "interrupt-map", or
90723bcd5ebSCédric Le Goater * "ibm,xive-lisn-ranges" properties, or as returned by the
90823bcd5ebSCédric Le Goater * ibm,query-interrupt-source-number RTAS call, or as returned
90923bcd5ebSCédric Le Goater * by the H_ALLOCATE_VAS_WINDOW hcall
91023bcd5ebSCédric Le Goater *
91123bcd5ebSCédric Le Goater * Output
91223bcd5ebSCédric Le Goater * - R4: "flags"
91323bcd5ebSCédric Le Goater * Bits 0-59: Reserved
91423bcd5ebSCédric Le Goater * Bit 60: H_INT_ESB must be used for Event State Buffer
91523bcd5ebSCédric Le Goater * management
91623bcd5ebSCédric Le Goater * Bit 61: 1 == LSI 0 == MSI
91723bcd5ebSCédric Le Goater * Bit 62: the full function page supports trigger
91823bcd5ebSCédric Le Goater * Bit 63: Store EOI Supported
91923bcd5ebSCédric Le Goater * - R5: Logical Real address of full function Event State Buffer
92023bcd5ebSCédric Le Goater * management page, -1 if H_INT_ESB hcall flag is set to 1.
92123bcd5ebSCédric Le Goater * - R6: Logical Real Address of trigger only Event State Buffer
92223bcd5ebSCédric Le Goater * management page or -1.
92323bcd5ebSCédric Le Goater * - R7: Power of 2 page size for the ESB management pages returned in
92423bcd5ebSCédric Le Goater * R5 and R6.
92523bcd5ebSCédric Le Goater */
92623bcd5ebSCédric Le Goater
92723bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_H_INT_ESB PPC_BIT(60) /* ESB manage with H_INT_ESB */
92823bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_LSI PPC_BIT(61) /* Virtual LSI type */
92923bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_TRIGGER PPC_BIT(62) /* Trigger and management
93023bcd5ebSCédric Le Goater on same page */
93123bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_STORE_EOI PPC_BIT(63) /* Store EOI support */
93223bcd5ebSCédric Le Goater
h_int_get_source_info(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)93323bcd5ebSCédric Le Goater static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
934ce2918cbSDavid Gibson SpaprMachineState *spapr,
93523bcd5ebSCédric Le Goater target_ulong opcode,
93623bcd5ebSCédric Le Goater target_ulong *args)
93723bcd5ebSCédric Le Goater {
938ce2918cbSDavid Gibson SpaprXive *xive = spapr->xive;
93923bcd5ebSCédric Le Goater XiveSource *xsrc = &xive->source;
94023bcd5ebSCédric Le Goater target_ulong flags = args[0];
94123bcd5ebSCédric Le Goater target_ulong lisn = args[1];
94223bcd5ebSCédric Le Goater
9434e960974SCédric Le Goater trace_spapr_xive_get_source_info(flags, lisn);
9444e960974SCédric Le Goater
94523bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
94623bcd5ebSCédric Le Goater return H_FUNCTION;
94723bcd5ebSCédric Le Goater }
94823bcd5ebSCédric Le Goater
94923bcd5ebSCédric Le Goater if (flags) {
95023bcd5ebSCédric Le Goater return H_PARAMETER;
95123bcd5ebSCédric Le Goater }
95223bcd5ebSCédric Le Goater
95323bcd5ebSCédric Le Goater if (lisn >= xive->nr_irqs) {
95423bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
95523bcd5ebSCédric Le Goater lisn);
95623bcd5ebSCédric Le Goater return H_P2;
95723bcd5ebSCédric Le Goater }
95823bcd5ebSCédric Le Goater
95923bcd5ebSCédric Le Goater if (!xive_eas_is_valid(&xive->eat[lisn])) {
96023bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
96123bcd5ebSCédric Le Goater lisn);
96223bcd5ebSCédric Le Goater return H_P2;
96323bcd5ebSCédric Le Goater }
96423bcd5ebSCédric Le Goater
96523bcd5ebSCédric Le Goater /*
96623bcd5ebSCédric Le Goater * All sources are emulated under the main XIVE object and share
96723bcd5ebSCédric Le Goater * the same characteristics.
96823bcd5ebSCédric Le Goater */
96923bcd5ebSCédric Le Goater args[0] = 0;
97023bcd5ebSCédric Le Goater if (!xive_source_esb_has_2page(xsrc)) {
97123bcd5ebSCédric Le Goater args[0] |= SPAPR_XIVE_SRC_TRIGGER;
97223bcd5ebSCédric Le Goater }
97323bcd5ebSCédric Le Goater if (xsrc->esb_flags & XIVE_SRC_STORE_EOI) {
97423bcd5ebSCédric Le Goater args[0] |= SPAPR_XIVE_SRC_STORE_EOI;
97523bcd5ebSCédric Le Goater }
97623bcd5ebSCédric Le Goater
97723bcd5ebSCédric Le Goater /*
97823bcd5ebSCédric Le Goater * Force the use of the H_INT_ESB hcall in case of an LSI
97923bcd5ebSCédric Le Goater * interrupt. This is necessary under KVM to re-trigger the
98023bcd5ebSCédric Le Goater * interrupt if the level is still asserted
98123bcd5ebSCédric Le Goater */
98223bcd5ebSCédric Le Goater if (xive_source_irq_is_lsi(xsrc, lisn)) {
98323bcd5ebSCédric Le Goater args[0] |= SPAPR_XIVE_SRC_H_INT_ESB | SPAPR_XIVE_SRC_LSI;
98423bcd5ebSCédric Le Goater }
98523bcd5ebSCédric Le Goater
98623bcd5ebSCédric Le Goater if (!(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
98723bcd5ebSCédric Le Goater args[1] = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn);
98823bcd5ebSCédric Le Goater } else {
98923bcd5ebSCédric Le Goater args[1] = -1;
99023bcd5ebSCédric Le Goater }
99123bcd5ebSCédric Le Goater
99223bcd5ebSCédric Le Goater if (xive_source_esb_has_2page(xsrc) &&
99323bcd5ebSCédric Le Goater !(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
99423bcd5ebSCédric Le Goater args[2] = xive->vc_base + xive_source_esb_page(xsrc, lisn);
99523bcd5ebSCédric Le Goater } else {
99623bcd5ebSCédric Le Goater args[2] = -1;
99723bcd5ebSCédric Le Goater }
99823bcd5ebSCédric Le Goater
99923bcd5ebSCédric Le Goater if (xive_source_esb_has_2page(xsrc)) {
100023bcd5ebSCédric Le Goater args[3] = xsrc->esb_shift - 1;
100123bcd5ebSCédric Le Goater } else {
100223bcd5ebSCédric Le Goater args[3] = xsrc->esb_shift;
100323bcd5ebSCédric Le Goater }
100423bcd5ebSCédric Le Goater
100523bcd5ebSCédric Le Goater return H_SUCCESS;
100623bcd5ebSCédric Le Goater }
100723bcd5ebSCédric Le Goater
100823bcd5ebSCédric Le Goater /*
100923bcd5ebSCédric Le Goater * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
101023bcd5ebSCédric Le Goater * Interrupt Source to a target. The Logical Interrupt Source is
101123bcd5ebSCédric Le Goater * designated with the "lisn" parameter and the target is designated
101223bcd5ebSCédric Le Goater * with the "target" and "priority" parameters. Upon return from the
101323bcd5ebSCédric Le Goater * hcall(), no additional interrupts will be directed to the old EQ.
101423bcd5ebSCédric Le Goater *
101523bcd5ebSCédric Le Goater * Parameters:
101623bcd5ebSCédric Le Goater * Input:
101723bcd5ebSCédric Le Goater * - R4: "flags"
101823bcd5ebSCédric Le Goater * Bits 0-61: Reserved
101923bcd5ebSCédric Le Goater * Bit 62: set the "eisn" in the EAS
102023bcd5ebSCédric Le Goater * Bit 63: masks the interrupt source in the hardware interrupt
102123bcd5ebSCédric Le Goater * control structure. An interrupt masked by this mechanism will
102223bcd5ebSCédric Le Goater * be dropped, but it's source state bits will still be
102323bcd5ebSCédric Le Goater * set. There is no race-free way of unmasking and restoring the
102423bcd5ebSCédric Le Goater * source. Thus this should only be used in interrupts that are
102523bcd5ebSCédric Le Goater * also masked at the source, and only in cases where the
102623bcd5ebSCédric Le Goater * interrupt is not meant to be used for a large amount of time
102723bcd5ebSCédric Le Goater * because no valid target exists for it for example
102823bcd5ebSCédric Le Goater * - R5: "lisn" is per "interrupts", "interrupt-map", or
102923bcd5ebSCédric Le Goater * "ibm,xive-lisn-ranges" properties, or as returned by the
103023bcd5ebSCédric Le Goater * ibm,query-interrupt-source-number RTAS call, or as returned by
103123bcd5ebSCédric Le Goater * the H_ALLOCATE_VAS_WINDOW hcall
103223bcd5ebSCédric Le Goater * - R6: "target" is per "ibm,ppc-interrupt-server#s" or
103323bcd5ebSCédric Le Goater * "ibm,ppc-interrupt-gserver#s"
103423bcd5ebSCédric Le Goater * - R7: "priority" is a valid priority not in
103523bcd5ebSCédric Le Goater * "ibm,plat-res-int-priorities"
103623bcd5ebSCédric Le Goater * - R8: "eisn" is the guest EISN associated with the "lisn"
103723bcd5ebSCédric Le Goater *
103823bcd5ebSCédric Le Goater * Output:
103923bcd5ebSCédric Le Goater * - None
104023bcd5ebSCédric Le Goater */
104123bcd5ebSCédric Le Goater
104223bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_SET_EISN PPC_BIT(62)
104323bcd5ebSCédric Le Goater #define SPAPR_XIVE_SRC_MASK PPC_BIT(63)
104423bcd5ebSCédric Le Goater
h_int_set_source_config(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)104523bcd5ebSCédric Le Goater static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
1046ce2918cbSDavid Gibson SpaprMachineState *spapr,
104723bcd5ebSCédric Le Goater target_ulong opcode,
104823bcd5ebSCédric Le Goater target_ulong *args)
104923bcd5ebSCédric Le Goater {
1050ce2918cbSDavid Gibson SpaprXive *xive = spapr->xive;
105123bcd5ebSCédric Le Goater XiveEAS eas, new_eas;
105223bcd5ebSCédric Le Goater target_ulong flags = args[0];
105323bcd5ebSCédric Le Goater target_ulong lisn = args[1];
105423bcd5ebSCédric Le Goater target_ulong target = args[2];
105523bcd5ebSCédric Le Goater target_ulong priority = args[3];
105623bcd5ebSCédric Le Goater target_ulong eisn = args[4];
105723bcd5ebSCédric Le Goater uint8_t end_blk;
105823bcd5ebSCédric Le Goater uint32_t end_idx;
105923bcd5ebSCédric Le Goater
10604e960974SCédric Le Goater trace_spapr_xive_set_source_config(flags, lisn, target, priority, eisn);
10614e960974SCédric Le Goater
106223bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
106323bcd5ebSCédric Le Goater return H_FUNCTION;
106423bcd5ebSCédric Le Goater }
106523bcd5ebSCédric Le Goater
106623bcd5ebSCédric Le Goater if (flags & ~(SPAPR_XIVE_SRC_SET_EISN | SPAPR_XIVE_SRC_MASK)) {
106723bcd5ebSCédric Le Goater return H_PARAMETER;
106823bcd5ebSCédric Le Goater }
106923bcd5ebSCédric Le Goater
107023bcd5ebSCédric Le Goater if (lisn >= xive->nr_irqs) {
107123bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
107223bcd5ebSCédric Le Goater lisn);
107323bcd5ebSCédric Le Goater return H_P2;
107423bcd5ebSCédric Le Goater }
107523bcd5ebSCédric Le Goater
107623bcd5ebSCédric Le Goater eas = xive->eat[lisn];
107723bcd5ebSCédric Le Goater if (!xive_eas_is_valid(&eas)) {
107823bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
107923bcd5ebSCédric Le Goater lisn);
108023bcd5ebSCédric Le Goater return H_P2;
108123bcd5ebSCédric Le Goater }
108223bcd5ebSCédric Le Goater
108323bcd5ebSCédric Le Goater /* priority 0xff is used to reset the EAS */
108423bcd5ebSCédric Le Goater if (priority == 0xff) {
108523bcd5ebSCédric Le Goater new_eas.w = cpu_to_be64(EAS_VALID | EAS_MASKED);
108623bcd5ebSCédric Le Goater goto out;
108723bcd5ebSCédric Le Goater }
108823bcd5ebSCédric Le Goater
108923bcd5ebSCédric Le Goater if (flags & SPAPR_XIVE_SRC_MASK) {
109023bcd5ebSCédric Le Goater new_eas.w = eas.w | cpu_to_be64(EAS_MASKED);
109123bcd5ebSCédric Le Goater } else {
109223bcd5ebSCédric Le Goater new_eas.w = eas.w & cpu_to_be64(~EAS_MASKED);
109323bcd5ebSCédric Le Goater }
109423bcd5ebSCédric Le Goater
10954f311a70SCédric Le Goater if (spapr_xive_priority_is_reserved(xive, priority)) {
109623bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
109723bcd5ebSCédric Le Goater " is reserved\n", priority);
109823bcd5ebSCédric Le Goater return H_P4;
109923bcd5ebSCédric Le Goater }
110023bcd5ebSCédric Le Goater
110123bcd5ebSCédric Le Goater /*
110223bcd5ebSCédric Le Goater * Validate that "target" is part of the list of threads allocated
110323bcd5ebSCédric Le Goater * to the partition. For that, find the END corresponding to the
110423bcd5ebSCédric Le Goater * target.
110523bcd5ebSCédric Le Goater */
110623bcd5ebSCédric Le Goater if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
110723bcd5ebSCédric Le Goater return H_P3;
110823bcd5ebSCédric Le Goater }
110923bcd5ebSCédric Le Goater
111023bcd5ebSCédric Le Goater new_eas.w = xive_set_field64(EAS_END_BLOCK, new_eas.w, end_blk);
111123bcd5ebSCédric Le Goater new_eas.w = xive_set_field64(EAS_END_INDEX, new_eas.w, end_idx);
111223bcd5ebSCédric Le Goater
111323bcd5ebSCédric Le Goater if (flags & SPAPR_XIVE_SRC_SET_EISN) {
111423bcd5ebSCédric Le Goater new_eas.w = xive_set_field64(EAS_END_DATA, new_eas.w, eisn);
111523bcd5ebSCédric Le Goater }
111623bcd5ebSCédric Le Goater
1117e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
11180c575703SCédric Le Goater Error *local_err = NULL;
11190c575703SCédric Le Goater
11200c575703SCédric Le Goater kvmppc_xive_set_source_config(xive, lisn, &new_eas, &local_err);
11210c575703SCédric Le Goater if (local_err) {
11220c575703SCédric Le Goater error_report_err(local_err);
11230c575703SCédric Le Goater return H_HARDWARE;
11240c575703SCédric Le Goater }
11250c575703SCédric Le Goater }
11260c575703SCédric Le Goater
112723bcd5ebSCédric Le Goater out:
112823bcd5ebSCédric Le Goater xive->eat[lisn] = new_eas;
112923bcd5ebSCédric Le Goater return H_SUCCESS;
113023bcd5ebSCédric Le Goater }
113123bcd5ebSCédric Le Goater
113223bcd5ebSCédric Le Goater /*
113323bcd5ebSCédric Le Goater * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
113423bcd5ebSCédric Le Goater * target/priority pair is assigned to the specified Logical Interrupt
113523bcd5ebSCédric Le Goater * Source.
113623bcd5ebSCédric Le Goater *
113723bcd5ebSCédric Le Goater * Parameters:
113823bcd5ebSCédric Le Goater * Input:
113923bcd5ebSCédric Le Goater * - R4: "flags"
114023bcd5ebSCédric Le Goater * Bits 0-63 Reserved
114123bcd5ebSCédric Le Goater * - R5: "lisn" is per "interrupts", "interrupt-map", or
114223bcd5ebSCédric Le Goater * "ibm,xive-lisn-ranges" properties, or as returned by the
114323bcd5ebSCédric Le Goater * ibm,query-interrupt-source-number RTAS call, or as
114423bcd5ebSCédric Le Goater * returned by the H_ALLOCATE_VAS_WINDOW hcall
114523bcd5ebSCédric Le Goater *
114623bcd5ebSCédric Le Goater * Output:
114723bcd5ebSCédric Le Goater * - R4: Target to which the specified Logical Interrupt Source is
114823bcd5ebSCédric Le Goater * assigned
114923bcd5ebSCédric Le Goater * - R5: Priority to which the specified Logical Interrupt Source is
115023bcd5ebSCédric Le Goater * assigned
115123bcd5ebSCédric Le Goater * - R6: EISN for the specified Logical Interrupt Source (this will be
115223bcd5ebSCédric Le Goater * equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
115323bcd5ebSCédric Le Goater */
h_int_get_source_config(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)115423bcd5ebSCédric Le Goater static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
1155ce2918cbSDavid Gibson SpaprMachineState *spapr,
115623bcd5ebSCédric Le Goater target_ulong opcode,
115723bcd5ebSCédric Le Goater target_ulong *args)
115823bcd5ebSCédric Le Goater {
1159ce2918cbSDavid Gibson SpaprXive *xive = spapr->xive;
116023bcd5ebSCédric Le Goater target_ulong flags = args[0];
116123bcd5ebSCédric Le Goater target_ulong lisn = args[1];
116223bcd5ebSCédric Le Goater XiveEAS eas;
116323bcd5ebSCédric Le Goater XiveEND *end;
116423bcd5ebSCédric Le Goater uint8_t nvt_blk;
116523bcd5ebSCédric Le Goater uint32_t end_idx, nvt_idx;
116623bcd5ebSCédric Le Goater
11674e960974SCédric Le Goater trace_spapr_xive_get_source_config(flags, lisn);
11684e960974SCédric Le Goater
116923bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
117023bcd5ebSCédric Le Goater return H_FUNCTION;
117123bcd5ebSCédric Le Goater }
117223bcd5ebSCédric Le Goater
117323bcd5ebSCédric Le Goater if (flags) {
117423bcd5ebSCédric Le Goater return H_PARAMETER;
117523bcd5ebSCédric Le Goater }
117623bcd5ebSCédric Le Goater
117723bcd5ebSCédric Le Goater if (lisn >= xive->nr_irqs) {
117823bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
117923bcd5ebSCédric Le Goater lisn);
118023bcd5ebSCédric Le Goater return H_P2;
118123bcd5ebSCédric Le Goater }
118223bcd5ebSCédric Le Goater
118323bcd5ebSCédric Le Goater eas = xive->eat[lisn];
118423bcd5ebSCédric Le Goater if (!xive_eas_is_valid(&eas)) {
118523bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
118623bcd5ebSCédric Le Goater lisn);
118723bcd5ebSCédric Le Goater return H_P2;
118823bcd5ebSCédric Le Goater }
118923bcd5ebSCédric Le Goater
119023bcd5ebSCédric Le Goater /* EAS_END_BLOCK is unused on sPAPR */
119123bcd5ebSCédric Le Goater end_idx = xive_get_field64(EAS_END_INDEX, eas.w);
119223bcd5ebSCédric Le Goater
119323bcd5ebSCédric Le Goater assert(end_idx < xive->nr_ends);
119423bcd5ebSCédric Le Goater end = &xive->endt[end_idx];
119523bcd5ebSCédric Le Goater
119623bcd5ebSCédric Le Goater nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
119723bcd5ebSCédric Le Goater nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
119823bcd5ebSCédric Le Goater args[0] = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
119923bcd5ebSCédric Le Goater
120023bcd5ebSCédric Le Goater if (xive_eas_is_masked(&eas)) {
120123bcd5ebSCédric Le Goater args[1] = 0xff;
120223bcd5ebSCédric Le Goater } else {
120323bcd5ebSCédric Le Goater args[1] = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
120423bcd5ebSCédric Le Goater }
120523bcd5ebSCédric Le Goater
120623bcd5ebSCédric Le Goater args[2] = xive_get_field64(EAS_END_DATA, eas.w);
120723bcd5ebSCédric Le Goater
120823bcd5ebSCédric Le Goater return H_SUCCESS;
120923bcd5ebSCédric Le Goater }
121023bcd5ebSCédric Le Goater
121123bcd5ebSCédric Le Goater /*
121223bcd5ebSCédric Le Goater * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
121323bcd5ebSCédric Le Goater * address of the notification management page associated with the
121423bcd5ebSCédric Le Goater * specified target and priority.
121523bcd5ebSCédric Le Goater *
121623bcd5ebSCédric Le Goater * Parameters:
121723bcd5ebSCédric Le Goater * Input:
121823bcd5ebSCédric Le Goater * - R4: "flags"
121923bcd5ebSCédric Le Goater * Bits 0-63 Reserved
122023bcd5ebSCédric Le Goater * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
122123bcd5ebSCédric Le Goater * "ibm,ppc-interrupt-gserver#s"
122223bcd5ebSCédric Le Goater * - R6: "priority" is a valid priority not in
122323bcd5ebSCédric Le Goater * "ibm,plat-res-int-priorities"
122423bcd5ebSCédric Le Goater *
122523bcd5ebSCédric Le Goater * Output:
122623bcd5ebSCédric Le Goater * - R4: Logical real address of notification page
122723bcd5ebSCédric Le Goater * - R5: Power of 2 page size of the notification page
122823bcd5ebSCédric Le Goater */
h_int_get_queue_info(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)122923bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
1230ce2918cbSDavid Gibson SpaprMachineState *spapr,
123123bcd5ebSCédric Le Goater target_ulong opcode,
123223bcd5ebSCédric Le Goater target_ulong *args)
123323bcd5ebSCédric Le Goater {
1234ce2918cbSDavid Gibson SpaprXive *xive = spapr->xive;
123523bcd5ebSCédric Le Goater XiveENDSource *end_xsrc = &xive->end_source;
123623bcd5ebSCédric Le Goater target_ulong flags = args[0];
123723bcd5ebSCédric Le Goater target_ulong target = args[1];
123823bcd5ebSCédric Le Goater target_ulong priority = args[2];
123923bcd5ebSCédric Le Goater XiveEND *end;
124023bcd5ebSCédric Le Goater uint8_t end_blk;
124123bcd5ebSCédric Le Goater uint32_t end_idx;
124223bcd5ebSCédric Le Goater
12434e960974SCédric Le Goater trace_spapr_xive_get_queue_info(flags, target, priority);
12444e960974SCédric Le Goater
124523bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
124623bcd5ebSCédric Le Goater return H_FUNCTION;
124723bcd5ebSCédric Le Goater }
124823bcd5ebSCédric Le Goater
124923bcd5ebSCédric Le Goater if (flags) {
125023bcd5ebSCédric Le Goater return H_PARAMETER;
125123bcd5ebSCédric Le Goater }
125223bcd5ebSCédric Le Goater
125323bcd5ebSCédric Le Goater /*
125423bcd5ebSCédric Le Goater * H_STATE should be returned if a H_INT_RESET is in progress.
125523bcd5ebSCédric Le Goater * This is not needed when running the emulation under QEMU
125623bcd5ebSCédric Le Goater */
125723bcd5ebSCédric Le Goater
12584f311a70SCédric Le Goater if (spapr_xive_priority_is_reserved(xive, priority)) {
125923bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
126023bcd5ebSCédric Le Goater " is reserved\n", priority);
126123bcd5ebSCédric Le Goater return H_P3;
126223bcd5ebSCédric Le Goater }
126323bcd5ebSCédric Le Goater
126423bcd5ebSCédric Le Goater /*
126523bcd5ebSCédric Le Goater * Validate that "target" is part of the list of threads allocated
126623bcd5ebSCédric Le Goater * to the partition. For that, find the END corresponding to the
126723bcd5ebSCédric Le Goater * target.
126823bcd5ebSCédric Le Goater */
126923bcd5ebSCédric Le Goater if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
127023bcd5ebSCédric Le Goater return H_P2;
127123bcd5ebSCédric Le Goater }
127223bcd5ebSCédric Le Goater
127323bcd5ebSCédric Le Goater assert(end_idx < xive->nr_ends);
127423bcd5ebSCédric Le Goater end = &xive->endt[end_idx];
127523bcd5ebSCédric Le Goater
127623bcd5ebSCédric Le Goater args[0] = xive->end_base + (1ull << (end_xsrc->esb_shift + 1)) * end_idx;
127723bcd5ebSCédric Le Goater if (xive_end_is_enqueue(end)) {
127823bcd5ebSCédric Le Goater args[1] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
127923bcd5ebSCédric Le Goater } else {
128023bcd5ebSCédric Le Goater args[1] = 0;
128123bcd5ebSCédric Le Goater }
128223bcd5ebSCédric Le Goater
128323bcd5ebSCédric Le Goater return H_SUCCESS;
128423bcd5ebSCédric Le Goater }
128523bcd5ebSCédric Le Goater
128623bcd5ebSCédric Le Goater /*
128723bcd5ebSCédric Le Goater * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
128823bcd5ebSCédric Le Goater * a given "target" and "priority". It is also used to set the
128923bcd5ebSCédric Le Goater * notification config associated with the EQ. An EQ size of 0 is
129023bcd5ebSCédric Le Goater * used to reset the EQ config for a given target and priority. If
129123bcd5ebSCédric Le Goater * resetting the EQ config, the END associated with the given "target"
129223bcd5ebSCédric Le Goater * and "priority" will be changed to disable queueing.
129323bcd5ebSCédric Le Goater *
129423bcd5ebSCédric Le Goater * Upon return from the hcall(), no additional interrupts will be
129523bcd5ebSCédric Le Goater * directed to the old EQ (if one was set). The old EQ (if one was
129623bcd5ebSCédric Le Goater * set) should be investigated for interrupts that occurred prior to
129723bcd5ebSCédric Le Goater * or during the hcall().
129823bcd5ebSCédric Le Goater *
129923bcd5ebSCédric Le Goater * Parameters:
130023bcd5ebSCédric Le Goater * Input:
130123bcd5ebSCédric Le Goater * - R4: "flags"
130223bcd5ebSCédric Le Goater * Bits 0-62: Reserved
130323bcd5ebSCédric Le Goater * Bit 63: Unconditional Notify (n) per the XIVE spec
130423bcd5ebSCédric Le Goater * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
130523bcd5ebSCédric Le Goater * "ibm,ppc-interrupt-gserver#s"
130623bcd5ebSCédric Le Goater * - R6: "priority" is a valid priority not in
130723bcd5ebSCédric Le Goater * "ibm,plat-res-int-priorities"
130823bcd5ebSCédric Le Goater * - R7: "eventQueue": The logical real address of the start of the EQ
130923bcd5ebSCédric Le Goater * - R8: "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
131023bcd5ebSCédric Le Goater *
131123bcd5ebSCédric Le Goater * Output:
131223bcd5ebSCédric Le Goater * - None
131323bcd5ebSCédric Le Goater */
131423bcd5ebSCédric Le Goater
131523bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_ALWAYS_NOTIFY PPC_BIT(63)
131623bcd5ebSCédric Le Goater
h_int_set_queue_config(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)131723bcd5ebSCédric Le Goater static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
1318ce2918cbSDavid Gibson SpaprMachineState *spapr,
131923bcd5ebSCédric Le Goater target_ulong opcode,
132023bcd5ebSCédric Le Goater target_ulong *args)
132123bcd5ebSCédric Le Goater {
1322ce2918cbSDavid Gibson SpaprXive *xive = spapr->xive;
132323bcd5ebSCédric Le Goater target_ulong flags = args[0];
132423bcd5ebSCédric Le Goater target_ulong target = args[1];
132523bcd5ebSCédric Le Goater target_ulong priority = args[2];
132623bcd5ebSCédric Le Goater target_ulong qpage = args[3];
132723bcd5ebSCédric Le Goater target_ulong qsize = args[4];
132823bcd5ebSCédric Le Goater XiveEND end;
132923bcd5ebSCédric Le Goater uint8_t end_blk, nvt_blk;
133023bcd5ebSCédric Le Goater uint32_t end_idx, nvt_idx;
133123bcd5ebSCédric Le Goater
13324e960974SCédric Le Goater trace_spapr_xive_set_queue_config(flags, target, priority, qpage, qsize);
13334e960974SCédric Le Goater
133423bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
133523bcd5ebSCédric Le Goater return H_FUNCTION;
133623bcd5ebSCédric Le Goater }
133723bcd5ebSCédric Le Goater
133823bcd5ebSCédric Le Goater if (flags & ~SPAPR_XIVE_END_ALWAYS_NOTIFY) {
133923bcd5ebSCédric Le Goater return H_PARAMETER;
134023bcd5ebSCédric Le Goater }
134123bcd5ebSCédric Le Goater
134223bcd5ebSCédric Le Goater /*
134323bcd5ebSCédric Le Goater * H_STATE should be returned if a H_INT_RESET is in progress.
134423bcd5ebSCédric Le Goater * This is not needed when running the emulation under QEMU
134523bcd5ebSCédric Le Goater */
134623bcd5ebSCédric Le Goater
13474f311a70SCédric Le Goater if (spapr_xive_priority_is_reserved(xive, priority)) {
134823bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
134923bcd5ebSCédric Le Goater " is reserved\n", priority);
135023bcd5ebSCédric Le Goater return H_P3;
135123bcd5ebSCédric Le Goater }
135223bcd5ebSCédric Le Goater
135323bcd5ebSCédric Le Goater /*
135423bcd5ebSCédric Le Goater * Validate that "target" is part of the list of threads allocated
135523bcd5ebSCédric Le Goater * to the partition. For that, find the END corresponding to the
135623bcd5ebSCédric Le Goater * target.
135723bcd5ebSCédric Le Goater */
135823bcd5ebSCédric Le Goater
135923bcd5ebSCédric Le Goater if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
136023bcd5ebSCédric Le Goater return H_P2;
136123bcd5ebSCédric Le Goater }
136223bcd5ebSCédric Le Goater
136323bcd5ebSCédric Le Goater assert(end_idx < xive->nr_ends);
136423bcd5ebSCédric Le Goater memcpy(&end, &xive->endt[end_idx], sizeof(XiveEND));
136523bcd5ebSCédric Le Goater
136623bcd5ebSCédric Le Goater switch (qsize) {
136723bcd5ebSCédric Le Goater case 12:
136823bcd5ebSCédric Le Goater case 16:
136923bcd5ebSCédric Le Goater case 21:
137023bcd5ebSCédric Le Goater case 24:
13717f9136f9SCédric Le Goater if (!QEMU_IS_ALIGNED(qpage, 1ul << qsize)) {
13727f9136f9SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: EQ @0x%" HWADDR_PRIx
13737f9136f9SCédric Le Goater " is not naturally aligned with %" HWADDR_PRIx "\n",
13747f9136f9SCédric Le Goater qpage, (hwaddr)1 << qsize);
13757f9136f9SCédric Le Goater return H_P4;
13767f9136f9SCédric Le Goater }
137723bcd5ebSCédric Le Goater end.w2 = cpu_to_be32((qpage >> 32) & 0x0fffffff);
137823bcd5ebSCédric Le Goater end.w3 = cpu_to_be32(qpage & 0xffffffff);
137923bcd5ebSCédric Le Goater end.w0 |= cpu_to_be32(END_W0_ENQUEUE);
138023bcd5ebSCédric Le Goater end.w0 = xive_set_field32(END_W0_QSIZE, end.w0, qsize - 12);
138123bcd5ebSCédric Le Goater break;
138223bcd5ebSCédric Le Goater case 0:
138323bcd5ebSCédric Le Goater /* reset queue and disable queueing */
138423bcd5ebSCédric Le Goater spapr_xive_end_reset(&end);
138523bcd5ebSCédric Le Goater goto out;
138623bcd5ebSCédric Le Goater
138723bcd5ebSCédric Le Goater default:
138823bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid EQ size %"PRIx64"\n",
138923bcd5ebSCédric Le Goater qsize);
139023bcd5ebSCédric Le Goater return H_P5;
139123bcd5ebSCédric Le Goater }
139223bcd5ebSCédric Le Goater
139323bcd5ebSCédric Le Goater if (qsize) {
139423bcd5ebSCédric Le Goater hwaddr plen = 1 << qsize;
139523bcd5ebSCédric Le Goater void *eq;
139623bcd5ebSCédric Le Goater
139723bcd5ebSCédric Le Goater /*
139823bcd5ebSCédric Le Goater * Validate the guest EQ. We should also check that the queue
139923bcd5ebSCédric Le Goater * has been zeroed by the OS.
140023bcd5ebSCédric Le Goater */
140123bcd5ebSCédric Le Goater eq = address_space_map(CPU(cpu)->as, qpage, &plen, true,
140223bcd5ebSCédric Le Goater MEMTXATTRS_UNSPECIFIED);
140323bcd5ebSCédric Le Goater if (plen != 1 << qsize) {
140423bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to map EQ @0x%"
140523bcd5ebSCédric Le Goater HWADDR_PRIx "\n", qpage);
140623bcd5ebSCédric Le Goater return H_P4;
140723bcd5ebSCédric Le Goater }
140823bcd5ebSCédric Le Goater address_space_unmap(CPU(cpu)->as, eq, plen, true, plen);
140923bcd5ebSCédric Le Goater }
141023bcd5ebSCédric Le Goater
141123bcd5ebSCédric Le Goater /* "target" should have been validated above */
141223bcd5ebSCédric Le Goater if (spapr_xive_target_to_nvt(target, &nvt_blk, &nvt_idx)) {
141323bcd5ebSCédric Le Goater g_assert_not_reached();
141423bcd5ebSCédric Le Goater }
141523bcd5ebSCédric Le Goater
141623bcd5ebSCédric Le Goater /*
141723bcd5ebSCédric Le Goater * Ensure the priority and target are correctly set (they will not
141823bcd5ebSCédric Le Goater * be right after allocation)
141923bcd5ebSCédric Le Goater */
142023bcd5ebSCédric Le Goater end.w6 = xive_set_field32(END_W6_NVT_BLOCK, 0ul, nvt_blk) |
142123bcd5ebSCédric Le Goater xive_set_field32(END_W6_NVT_INDEX, 0ul, nvt_idx);
142223bcd5ebSCédric Le Goater end.w7 = xive_set_field32(END_W7_F0_PRIORITY, 0ul, priority);
142323bcd5ebSCédric Le Goater
142423bcd5ebSCédric Le Goater if (flags & SPAPR_XIVE_END_ALWAYS_NOTIFY) {
142523bcd5ebSCédric Le Goater end.w0 |= cpu_to_be32(END_W0_UCOND_NOTIFY);
142623bcd5ebSCédric Le Goater } else {
142723bcd5ebSCédric Le Goater end.w0 &= cpu_to_be32((uint32_t)~END_W0_UCOND_NOTIFY);
142823bcd5ebSCédric Le Goater }
142923bcd5ebSCédric Le Goater
143023bcd5ebSCédric Le Goater /*
143123bcd5ebSCédric Le Goater * The generation bit for the END starts at 1 and The END page
143223bcd5ebSCédric Le Goater * offset counter starts at 0.
143323bcd5ebSCédric Le Goater */
143423bcd5ebSCédric Le Goater end.w1 = cpu_to_be32(END_W1_GENERATION) |
143523bcd5ebSCédric Le Goater xive_set_field32(END_W1_PAGE_OFF, 0ul, 0ul);
143623bcd5ebSCédric Le Goater end.w0 |= cpu_to_be32(END_W0_VALID);
143723bcd5ebSCédric Le Goater
143823bcd5ebSCédric Le Goater /*
143923bcd5ebSCédric Le Goater * TODO: issue syncs required to ensure all in-flight interrupts
144023bcd5ebSCédric Le Goater * are complete on the old END
144123bcd5ebSCédric Le Goater */
144223bcd5ebSCédric Le Goater
144323bcd5ebSCédric Le Goater out:
1444e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
14450c575703SCédric Le Goater Error *local_err = NULL;
14460c575703SCédric Le Goater
14470c575703SCédric Le Goater kvmppc_xive_set_queue_config(xive, end_blk, end_idx, &end, &local_err);
14480c575703SCédric Le Goater if (local_err) {
14490c575703SCédric Le Goater error_report_err(local_err);
14500c575703SCédric Le Goater return H_HARDWARE;
14510c575703SCédric Le Goater }
14520c575703SCédric Le Goater }
14530c575703SCédric Le Goater
145423bcd5ebSCédric Le Goater /* Update END */
145523bcd5ebSCédric Le Goater memcpy(&xive->endt[end_idx], &end, sizeof(XiveEND));
145623bcd5ebSCédric Le Goater return H_SUCCESS;
145723bcd5ebSCédric Le Goater }
145823bcd5ebSCédric Le Goater
145923bcd5ebSCédric Le Goater /*
146023bcd5ebSCédric Le Goater * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
146123bcd5ebSCédric Le Goater * target and priority.
146223bcd5ebSCédric Le Goater *
146323bcd5ebSCédric Le Goater * Parameters:
146423bcd5ebSCédric Le Goater * Input:
146523bcd5ebSCédric Le Goater * - R4: "flags"
146623bcd5ebSCédric Le Goater * Bits 0-62: Reserved
146723bcd5ebSCédric Le Goater * Bit 63: Debug: Return debug data
146823bcd5ebSCédric Le Goater * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
146923bcd5ebSCédric Le Goater * "ibm,ppc-interrupt-gserver#s"
147023bcd5ebSCédric Le Goater * - R6: "priority" is a valid priority not in
147123bcd5ebSCédric Le Goater * "ibm,plat-res-int-priorities"
147223bcd5ebSCédric Le Goater *
147323bcd5ebSCédric Le Goater * Output:
147423bcd5ebSCédric Le Goater * - R4: "flags":
147523bcd5ebSCédric Le Goater * Bits 0-61: Reserved
147623bcd5ebSCédric Le Goater * Bit 62: The value of Event Queue Generation Number (g) per
147723bcd5ebSCédric Le Goater * the XIVE spec if "Debug" = 1
147823bcd5ebSCédric Le Goater * Bit 63: The value of Unconditional Notify (n) per the XIVE spec
147923bcd5ebSCédric Le Goater * - R5: The logical real address of the start of the EQ
148023bcd5ebSCédric Le Goater * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
148123bcd5ebSCédric Le Goater * - R7: The value of Event Queue Offset Counter per XIVE spec
148223bcd5ebSCédric Le Goater * if "Debug" = 1, else 0
148323bcd5ebSCédric Le Goater *
148423bcd5ebSCédric Le Goater */
148523bcd5ebSCédric Le Goater
148623bcd5ebSCédric Le Goater #define SPAPR_XIVE_END_DEBUG PPC_BIT(63)
148723bcd5ebSCédric Le Goater
h_int_get_queue_config(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)148823bcd5ebSCédric Le Goater static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
1489ce2918cbSDavid Gibson SpaprMachineState *spapr,
149023bcd5ebSCédric Le Goater target_ulong opcode,
149123bcd5ebSCédric Le Goater target_ulong *args)
149223bcd5ebSCédric Le Goater {
1493ce2918cbSDavid Gibson SpaprXive *xive = spapr->xive;
149423bcd5ebSCédric Le Goater target_ulong flags = args[0];
149523bcd5ebSCédric Le Goater target_ulong target = args[1];
149623bcd5ebSCédric Le Goater target_ulong priority = args[2];
149723bcd5ebSCédric Le Goater XiveEND *end;
149823bcd5ebSCédric Le Goater uint8_t end_blk;
149923bcd5ebSCédric Le Goater uint32_t end_idx;
150023bcd5ebSCédric Le Goater
15014e960974SCédric Le Goater trace_spapr_xive_get_queue_config(flags, target, priority);
15024e960974SCédric Le Goater
150323bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
150423bcd5ebSCédric Le Goater return H_FUNCTION;
150523bcd5ebSCédric Le Goater }
150623bcd5ebSCédric Le Goater
150723bcd5ebSCédric Le Goater if (flags & ~SPAPR_XIVE_END_DEBUG) {
150823bcd5ebSCédric Le Goater return H_PARAMETER;
150923bcd5ebSCédric Le Goater }
151023bcd5ebSCédric Le Goater
151123bcd5ebSCédric Le Goater /*
151223bcd5ebSCédric Le Goater * H_STATE should be returned if a H_INT_RESET is in progress.
151323bcd5ebSCédric Le Goater * This is not needed when running the emulation under QEMU
151423bcd5ebSCédric Le Goater */
151523bcd5ebSCédric Le Goater
15164f311a70SCédric Le Goater if (spapr_xive_priority_is_reserved(xive, priority)) {
151723bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
151823bcd5ebSCédric Le Goater " is reserved\n", priority);
151923bcd5ebSCédric Le Goater return H_P3;
152023bcd5ebSCédric Le Goater }
152123bcd5ebSCédric Le Goater
152223bcd5ebSCédric Le Goater /*
152323bcd5ebSCédric Le Goater * Validate that "target" is part of the list of threads allocated
152423bcd5ebSCédric Le Goater * to the partition. For that, find the END corresponding to the
152523bcd5ebSCédric Le Goater * target.
152623bcd5ebSCédric Le Goater */
152723bcd5ebSCédric Le Goater if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
152823bcd5ebSCédric Le Goater return H_P2;
152923bcd5ebSCédric Le Goater }
153023bcd5ebSCédric Le Goater
153123bcd5ebSCédric Le Goater assert(end_idx < xive->nr_ends);
153223bcd5ebSCédric Le Goater end = &xive->endt[end_idx];
153323bcd5ebSCédric Le Goater
153423bcd5ebSCédric Le Goater args[0] = 0;
153523bcd5ebSCédric Le Goater if (xive_end_is_notify(end)) {
153623bcd5ebSCédric Le Goater args[0] |= SPAPR_XIVE_END_ALWAYS_NOTIFY;
153723bcd5ebSCédric Le Goater }
153823bcd5ebSCédric Le Goater
153923bcd5ebSCédric Le Goater if (xive_end_is_enqueue(end)) {
154013df9324SCédric Le Goater args[1] = xive_end_qaddr(end);
154123bcd5ebSCédric Le Goater args[2] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
154223bcd5ebSCédric Le Goater } else {
154323bcd5ebSCédric Le Goater args[1] = 0;
154423bcd5ebSCédric Le Goater args[2] = 0;
154523bcd5ebSCédric Le Goater }
154623bcd5ebSCédric Le Goater
1547e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
15480c575703SCédric Le Goater Error *local_err = NULL;
15490c575703SCédric Le Goater
15500c575703SCédric Le Goater kvmppc_xive_get_queue_config(xive, end_blk, end_idx, end, &local_err);
15510c575703SCédric Le Goater if (local_err) {
15520c575703SCédric Le Goater error_report_err(local_err);
15530c575703SCédric Le Goater return H_HARDWARE;
15540c575703SCédric Le Goater }
15550c575703SCédric Le Goater }
15560c575703SCédric Le Goater
155723bcd5ebSCédric Le Goater /* TODO: do we need any locking on the END ? */
155823bcd5ebSCédric Le Goater if (flags & SPAPR_XIVE_END_DEBUG) {
155923bcd5ebSCédric Le Goater /* Load the event queue generation number into the return flags */
156023bcd5ebSCédric Le Goater args[0] |= (uint64_t)xive_get_field32(END_W1_GENERATION, end->w1) << 62;
156123bcd5ebSCédric Le Goater
156223bcd5ebSCédric Le Goater /* Load R7 with the event queue offset counter */
156323bcd5ebSCédric Le Goater args[3] = xive_get_field32(END_W1_PAGE_OFF, end->w1);
156423bcd5ebSCédric Le Goater } else {
156523bcd5ebSCédric Le Goater args[3] = 0;
156623bcd5ebSCédric Le Goater }
156723bcd5ebSCédric Le Goater
156823bcd5ebSCédric Le Goater return H_SUCCESS;
156923bcd5ebSCédric Le Goater }
157023bcd5ebSCédric Le Goater
157123bcd5ebSCédric Le Goater /*
157223bcd5ebSCédric Le Goater * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
157323bcd5ebSCédric Le Goater * reporting cache line pair for the calling thread. The reporting
157423bcd5ebSCédric Le Goater * cache lines will contain the OS interrupt context when the OS
157523bcd5ebSCédric Le Goater * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
157623bcd5ebSCédric Le Goater * interrupt. The reporting cache lines can be reset by inputting -1
157723bcd5ebSCédric Le Goater * in "reportingLine". Issuing the CI store byte without reporting
157823bcd5ebSCédric Le Goater * cache lines registered will result in the data not being accessible
157923bcd5ebSCédric Le Goater * to the OS.
158023bcd5ebSCédric Le Goater *
158123bcd5ebSCédric Le Goater * Parameters:
158223bcd5ebSCédric Le Goater * Input:
158323bcd5ebSCédric Le Goater * - R4: "flags"
158423bcd5ebSCédric Le Goater * Bits 0-63: Reserved
158523bcd5ebSCédric Le Goater * - R5: "reportingLine": The logical real address of the reporting cache
158623bcd5ebSCédric Le Goater * line pair
158723bcd5ebSCédric Le Goater *
158823bcd5ebSCédric Le Goater * Output:
158923bcd5ebSCédric Le Goater * - None
159023bcd5ebSCédric Le Goater */
h_int_set_os_reporting_line(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)159123bcd5ebSCédric Le Goater static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
1592ce2918cbSDavid Gibson SpaprMachineState *spapr,
159323bcd5ebSCédric Le Goater target_ulong opcode,
159423bcd5ebSCédric Le Goater target_ulong *args)
159523bcd5ebSCédric Le Goater {
15964e960974SCédric Le Goater target_ulong flags = args[0];
15974e960974SCédric Le Goater
15984e960974SCédric Le Goater trace_spapr_xive_set_os_reporting_line(flags);
15994e960974SCédric Le Goater
160023bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
160123bcd5ebSCédric Le Goater return H_FUNCTION;
160223bcd5ebSCédric Le Goater }
160323bcd5ebSCédric Le Goater
160423bcd5ebSCédric Le Goater /*
160523bcd5ebSCédric Le Goater * H_STATE should be returned if a H_INT_RESET is in progress.
160623bcd5ebSCédric Le Goater * This is not needed when running the emulation under QEMU
160723bcd5ebSCédric Le Goater */
160823bcd5ebSCédric Le Goater
160923bcd5ebSCédric Le Goater /* TODO: H_INT_SET_OS_REPORTING_LINE */
161023bcd5ebSCédric Le Goater return H_FUNCTION;
161123bcd5ebSCédric Le Goater }
161223bcd5ebSCédric Le Goater
161323bcd5ebSCédric Le Goater /*
161423bcd5ebSCédric Le Goater * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
161523bcd5ebSCédric Le Goater * real address of the reporting cache line pair set for the input
161623bcd5ebSCédric Le Goater * "target". If no reporting cache line pair has been set, -1 is
161723bcd5ebSCédric Le Goater * returned.
161823bcd5ebSCédric Le Goater *
161923bcd5ebSCédric Le Goater * Parameters:
162023bcd5ebSCédric Le Goater * Input:
162123bcd5ebSCédric Le Goater * - R4: "flags"
162223bcd5ebSCédric Le Goater * Bits 0-63: Reserved
162323bcd5ebSCédric Le Goater * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
162423bcd5ebSCédric Le Goater * "ibm,ppc-interrupt-gserver#s"
162523bcd5ebSCédric Le Goater * - R6: "reportingLine": The logical real address of the reporting
162623bcd5ebSCédric Le Goater * cache line pair
162723bcd5ebSCédric Le Goater *
162823bcd5ebSCédric Le Goater * Output:
162923bcd5ebSCédric Le Goater * - R4: The logical real address of the reporting line if set, else -1
163023bcd5ebSCédric Le Goater */
h_int_get_os_reporting_line(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)163123bcd5ebSCédric Le Goater static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
1632ce2918cbSDavid Gibson SpaprMachineState *spapr,
163323bcd5ebSCédric Le Goater target_ulong opcode,
163423bcd5ebSCédric Le Goater target_ulong *args)
163523bcd5ebSCédric Le Goater {
16364e960974SCédric Le Goater target_ulong flags = args[0];
16374e960974SCédric Le Goater
16384e960974SCédric Le Goater trace_spapr_xive_get_os_reporting_line(flags);
16394e960974SCédric Le Goater
164023bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
164123bcd5ebSCédric Le Goater return H_FUNCTION;
164223bcd5ebSCédric Le Goater }
164323bcd5ebSCédric Le Goater
164423bcd5ebSCédric Le Goater /*
164523bcd5ebSCédric Le Goater * H_STATE should be returned if a H_INT_RESET is in progress.
164623bcd5ebSCédric Le Goater * This is not needed when running the emulation under QEMU
164723bcd5ebSCédric Le Goater */
164823bcd5ebSCédric Le Goater
164923bcd5ebSCédric Le Goater /* TODO: H_INT_GET_OS_REPORTING_LINE */
165023bcd5ebSCédric Le Goater return H_FUNCTION;
165123bcd5ebSCédric Le Goater }
165223bcd5ebSCédric Le Goater
165323bcd5ebSCédric Le Goater /*
165423bcd5ebSCédric Le Goater * The H_INT_ESB hcall() is used to issue a load or store to the ESB
165523bcd5ebSCédric Le Goater * page for the input "lisn". This hcall is only supported for LISNs
165623bcd5ebSCédric Le Goater * that have the ESB hcall flag set to 1 when returned from hcall()
165723bcd5ebSCédric Le Goater * H_INT_GET_SOURCE_INFO.
165823bcd5ebSCédric Le Goater *
165923bcd5ebSCédric Le Goater * Parameters:
166023bcd5ebSCédric Le Goater * Input:
166123bcd5ebSCédric Le Goater * - R4: "flags"
166223bcd5ebSCédric Le Goater * Bits 0-62: Reserved
166323bcd5ebSCédric Le Goater * bit 63: Store: Store=1, store operation, else load operation
166423bcd5ebSCédric Le Goater * - R5: "lisn" is per "interrupts", "interrupt-map", or
166523bcd5ebSCédric Le Goater * "ibm,xive-lisn-ranges" properties, or as returned by the
166623bcd5ebSCédric Le Goater * ibm,query-interrupt-source-number RTAS call, or as
166723bcd5ebSCédric Le Goater * returned by the H_ALLOCATE_VAS_WINDOW hcall
166823bcd5ebSCédric Le Goater * - R6: "esbOffset" is the offset into the ESB page for the load or
166923bcd5ebSCédric Le Goater * store operation
167023bcd5ebSCédric Le Goater * - R7: "storeData" is the data to write for a store operation
167123bcd5ebSCédric Le Goater *
167223bcd5ebSCédric Le Goater * Output:
167323bcd5ebSCédric Le Goater * - R4: The value of the load if load operation, else -1
167423bcd5ebSCédric Le Goater */
167523bcd5ebSCédric Le Goater
167623bcd5ebSCédric Le Goater #define SPAPR_XIVE_ESB_STORE PPC_BIT(63)
167723bcd5ebSCédric Le Goater
h_int_esb(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)167823bcd5ebSCédric Le Goater static target_ulong h_int_esb(PowerPCCPU *cpu,
1679ce2918cbSDavid Gibson SpaprMachineState *spapr,
168023bcd5ebSCédric Le Goater target_ulong opcode,
168123bcd5ebSCédric Le Goater target_ulong *args)
168223bcd5ebSCédric Le Goater {
1683ce2918cbSDavid Gibson SpaprXive *xive = spapr->xive;
168423bcd5ebSCédric Le Goater XiveEAS eas;
168523bcd5ebSCédric Le Goater target_ulong flags = args[0];
168623bcd5ebSCédric Le Goater target_ulong lisn = args[1];
168723bcd5ebSCédric Le Goater target_ulong offset = args[2];
168823bcd5ebSCédric Le Goater target_ulong data = args[3];
168923bcd5ebSCédric Le Goater hwaddr mmio_addr;
169023bcd5ebSCédric Le Goater XiveSource *xsrc = &xive->source;
169123bcd5ebSCédric Le Goater
16924e960974SCédric Le Goater trace_spapr_xive_esb(flags, lisn, offset, data);
16934e960974SCédric Le Goater
169423bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
169523bcd5ebSCédric Le Goater return H_FUNCTION;
169623bcd5ebSCédric Le Goater }
169723bcd5ebSCédric Le Goater
169823bcd5ebSCédric Le Goater if (flags & ~SPAPR_XIVE_ESB_STORE) {
169923bcd5ebSCédric Le Goater return H_PARAMETER;
170023bcd5ebSCédric Le Goater }
170123bcd5ebSCédric Le Goater
170223bcd5ebSCédric Le Goater if (lisn >= xive->nr_irqs) {
170323bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
170423bcd5ebSCédric Le Goater lisn);
170523bcd5ebSCédric Le Goater return H_P2;
170623bcd5ebSCédric Le Goater }
170723bcd5ebSCédric Le Goater
170823bcd5ebSCédric Le Goater eas = xive->eat[lisn];
170923bcd5ebSCédric Le Goater if (!xive_eas_is_valid(&eas)) {
171023bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
171123bcd5ebSCédric Le Goater lisn);
171223bcd5ebSCédric Le Goater return H_P2;
171323bcd5ebSCédric Le Goater }
171423bcd5ebSCédric Le Goater
171523bcd5ebSCédric Le Goater if (offset > (1ull << xsrc->esb_shift)) {
171623bcd5ebSCédric Le Goater return H_P3;
171723bcd5ebSCédric Le Goater }
171823bcd5ebSCédric Le Goater
1719e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
17200c575703SCédric Le Goater args[0] = kvmppc_xive_esb_rw(xsrc, lisn, offset, data,
17210c575703SCédric Le Goater flags & SPAPR_XIVE_ESB_STORE);
17220c575703SCédric Le Goater } else {
172323bcd5ebSCédric Le Goater mmio_addr = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn) + offset;
172423bcd5ebSCédric Le Goater
172523bcd5ebSCédric Le Goater if (dma_memory_rw(&address_space_memory, mmio_addr, &data, 8,
172623faf569SPhilippe Mathieu-Daudé (flags & SPAPR_XIVE_ESB_STORE),
172723faf569SPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED)) {
172823bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to access ESB @0x%"
172923bcd5ebSCédric Le Goater HWADDR_PRIx "\n", mmio_addr);
173023bcd5ebSCédric Le Goater return H_HARDWARE;
173123bcd5ebSCédric Le Goater }
173223bcd5ebSCédric Le Goater args[0] = (flags & SPAPR_XIVE_ESB_STORE) ? -1 : data;
17330c575703SCédric Le Goater }
173423bcd5ebSCédric Le Goater return H_SUCCESS;
173523bcd5ebSCédric Le Goater }
173623bcd5ebSCédric Le Goater
173723bcd5ebSCédric Le Goater /*
173823bcd5ebSCédric Le Goater * The H_INT_SYNC hcall() is used to issue hardware syncs that will
173923bcd5ebSCédric Le Goater * ensure any in flight events for the input lisn are in the event
174023bcd5ebSCédric Le Goater * queue.
174123bcd5ebSCédric Le Goater *
174223bcd5ebSCédric Le Goater * Parameters:
174323bcd5ebSCédric Le Goater * Input:
174423bcd5ebSCédric Le Goater * - R4: "flags"
174523bcd5ebSCédric Le Goater * Bits 0-63: Reserved
174623bcd5ebSCédric Le Goater * - R5: "lisn" is per "interrupts", "interrupt-map", or
174723bcd5ebSCédric Le Goater * "ibm,xive-lisn-ranges" properties, or as returned by the
174823bcd5ebSCédric Le Goater * ibm,query-interrupt-source-number RTAS call, or as
174923bcd5ebSCédric Le Goater * returned by the H_ALLOCATE_VAS_WINDOW hcall
175023bcd5ebSCédric Le Goater *
175123bcd5ebSCédric Le Goater * Output:
175223bcd5ebSCédric Le Goater * - None
175323bcd5ebSCédric Le Goater */
h_int_sync(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)175423bcd5ebSCédric Le Goater static target_ulong h_int_sync(PowerPCCPU *cpu,
1755ce2918cbSDavid Gibson SpaprMachineState *spapr,
175623bcd5ebSCédric Le Goater target_ulong opcode,
175723bcd5ebSCédric Le Goater target_ulong *args)
175823bcd5ebSCédric Le Goater {
1759ce2918cbSDavid Gibson SpaprXive *xive = spapr->xive;
176023bcd5ebSCédric Le Goater XiveEAS eas;
176123bcd5ebSCédric Le Goater target_ulong flags = args[0];
176223bcd5ebSCédric Le Goater target_ulong lisn = args[1];
176323bcd5ebSCédric Le Goater
17644e960974SCédric Le Goater trace_spapr_xive_sync(flags, lisn);
17654e960974SCédric Le Goater
176623bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
176723bcd5ebSCédric Le Goater return H_FUNCTION;
176823bcd5ebSCédric Le Goater }
176923bcd5ebSCédric Le Goater
177023bcd5ebSCédric Le Goater if (flags) {
177123bcd5ebSCédric Le Goater return H_PARAMETER;
177223bcd5ebSCédric Le Goater }
177323bcd5ebSCédric Le Goater
177423bcd5ebSCédric Le Goater if (lisn >= xive->nr_irqs) {
177523bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
177623bcd5ebSCédric Le Goater lisn);
177723bcd5ebSCédric Le Goater return H_P2;
177823bcd5ebSCédric Le Goater }
177923bcd5ebSCédric Le Goater
178023bcd5ebSCédric Le Goater eas = xive->eat[lisn];
178123bcd5ebSCédric Le Goater if (!xive_eas_is_valid(&eas)) {
178223bcd5ebSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
178323bcd5ebSCédric Le Goater lisn);
178423bcd5ebSCédric Le Goater return H_P2;
178523bcd5ebSCédric Le Goater }
178623bcd5ebSCédric Le Goater
178723bcd5ebSCédric Le Goater /*
178823bcd5ebSCédric Le Goater * H_STATE should be returned if a H_INT_RESET is in progress.
178923bcd5ebSCédric Le Goater * This is not needed when running the emulation under QEMU
179023bcd5ebSCédric Le Goater */
179123bcd5ebSCédric Le Goater
17920c575703SCédric Le Goater /*
17930c575703SCédric Le Goater * This is not real hardware. Nothing to be done unless when
17940c575703SCédric Le Goater * under KVM
17950c575703SCédric Le Goater */
17960c575703SCédric Le Goater
1797e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
17980c575703SCédric Le Goater Error *local_err = NULL;
17990c575703SCédric Le Goater
18000c575703SCédric Le Goater kvmppc_xive_sync_source(xive, lisn, &local_err);
18010c575703SCédric Le Goater if (local_err) {
18020c575703SCédric Le Goater error_report_err(local_err);
18030c575703SCédric Le Goater return H_HARDWARE;
18040c575703SCédric Le Goater }
18050c575703SCédric Le Goater }
180623bcd5ebSCédric Le Goater return H_SUCCESS;
180723bcd5ebSCédric Le Goater }
180823bcd5ebSCédric Le Goater
180923bcd5ebSCédric Le Goater /*
181023bcd5ebSCédric Le Goater * The H_INT_RESET hcall() is used to reset all of the partition's
181123bcd5ebSCédric Le Goater * interrupt exploitation structures to their initial state. This
181223bcd5ebSCédric Le Goater * means losing all previously set interrupt state set via
181323bcd5ebSCédric Le Goater * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
181423bcd5ebSCédric Le Goater *
181523bcd5ebSCédric Le Goater * Parameters:
181623bcd5ebSCédric Le Goater * Input:
181723bcd5ebSCédric Le Goater * - R4: "flags"
181823bcd5ebSCédric Le Goater * Bits 0-63: Reserved
181923bcd5ebSCédric Le Goater *
182023bcd5ebSCédric Le Goater * Output:
182123bcd5ebSCédric Le Goater * - None
182223bcd5ebSCédric Le Goater */
h_int_reset(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)182323bcd5ebSCédric Le Goater static target_ulong h_int_reset(PowerPCCPU *cpu,
1824ce2918cbSDavid Gibson SpaprMachineState *spapr,
182523bcd5ebSCédric Le Goater target_ulong opcode,
182623bcd5ebSCédric Le Goater target_ulong *args)
182723bcd5ebSCédric Le Goater {
1828ce2918cbSDavid Gibson SpaprXive *xive = spapr->xive;
182923bcd5ebSCédric Le Goater target_ulong flags = args[0];
183023bcd5ebSCédric Le Goater
18314e960974SCédric Le Goater trace_spapr_xive_reset(flags);
18324e960974SCédric Le Goater
183323bcd5ebSCédric Le Goater if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
183423bcd5ebSCédric Le Goater return H_FUNCTION;
183523bcd5ebSCédric Le Goater }
183623bcd5ebSCédric Le Goater
183723bcd5ebSCédric Le Goater if (flags) {
183823bcd5ebSCédric Le Goater return H_PARAMETER;
183923bcd5ebSCédric Le Goater }
184023bcd5ebSCédric Le Goater
1841b2df46fdSPeter Maydell device_cold_reset(DEVICE(xive));
18420c575703SCédric Le Goater
1843e519cdd9SGreg Kurz if (spapr_xive_in_kernel(xive)) {
18440c575703SCédric Le Goater Error *local_err = NULL;
18450c575703SCédric Le Goater
18460c575703SCédric Le Goater kvmppc_xive_reset(xive, &local_err);
18470c575703SCédric Le Goater if (local_err) {
18480c575703SCédric Le Goater error_report_err(local_err);
18490c575703SCédric Le Goater return H_HARDWARE;
18500c575703SCédric Le Goater }
18510c575703SCédric Le Goater }
185223bcd5ebSCédric Le Goater return H_SUCCESS;
185323bcd5ebSCédric Le Goater }
185423bcd5ebSCédric Le Goater
spapr_xive_hcall_init(SpaprMachineState * spapr)1855ce2918cbSDavid Gibson void spapr_xive_hcall_init(SpaprMachineState *spapr)
185623bcd5ebSCédric Le Goater {
185723bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
185823bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
185923bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
186023bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
186123bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
186223bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
186323bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
186423bcd5ebSCédric Le Goater h_int_set_os_reporting_line);
186523bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
186623bcd5ebSCédric Le Goater h_int_get_os_reporting_line);
186723bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_ESB, h_int_esb);
186823bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_SYNC, h_int_sync);
186923bcd5ebSCédric Le Goater spapr_register_hypercall(H_INT_RESET, h_int_reset);
187023bcd5ebSCédric Le Goater }
1871