All of lore.kernel.org
 help / color / mirror / Atom feed
From: Frank Chang <frank.chang@sifive.com>
To: Anup Patel <anup@brainfault.org>
Cc: Peter Maydell <peter.maydell@linaro.org>,
	"open list:RISC-V" <qemu-riscv@nongnu.org>,
	Sagar Karandikar <sagark@eecs.berkeley.edu>,
	"qemu-devel@nongnu.org Developers" <qemu-devel@nongnu.org>,
	Alistair Francis <Alistair.Francis@wdc.com>,
	Atish Patra <atishp@atishpatra.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Bin Meng <bmeng.cn@gmail.com>
Subject: Re: [PATCH v6 20/23] hw/intc: Add RISC-V AIA IMSIC device emulation
Date: Thu, 13 Jan 2022 15:26:30 +0800	[thread overview]
Message-ID: <CANzO1D3oYigRdBtYLVqsgAAhgU_JzKEB27SHx-7jg7b_qd8R1w@mail.gmail.com> (raw)
In-Reply-To: <20211230123539.52786-21-anup@brainfault.org>

[-- Attachment #1: Type: text/plain, Size: 21081 bytes --]

Anup Patel <anup@brainfault.org> 於 2021年12月30日 週四 下午9:00寫道:

> From: Anup Patel <anup.patel@wdc.com>
>
> The RISC-V AIA (Advanced Interrupt Architecture) defines a new
> interrupt controller for MSIs (message signal interrupts) called
> IMSIC (Incoming Message Signal Interrupt Controller). The IMSIC
> is per-HART device and also suppport virtualizaiton of MSIs using
> dedicated VS-level guest interrupt files.
>
> This patch adds device emulation for RISC-V AIA IMSIC which
> supports M-level, S-level, and VS-level MSIs.
>
> Signed-off-by: Anup Patel <anup.patel@wdc.com>
> Signed-off-by: Anup Patel <anup@brainfault.org>
> ---
>  hw/intc/Kconfig               |   3 +
>  hw/intc/meson.build           |   1 +
>  hw/intc/riscv_imsic.c         | 447 ++++++++++++++++++++++++++++++++++
>  include/hw/intc/riscv_imsic.h |  68 ++++++
>  4 files changed, 519 insertions(+)
>  create mode 100644 hw/intc/riscv_imsic.c
>  create mode 100644 include/hw/intc/riscv_imsic.h
>
> diff --git a/hw/intc/Kconfig b/hw/intc/Kconfig
> index 528e77b4a6..ec8d4cec29 100644
> --- a/hw/intc/Kconfig
> +++ b/hw/intc/Kconfig
> @@ -73,6 +73,9 @@ config RISCV_ACLINT
>  config RISCV_APLIC
>      bool
>
> +config RISCV_IMSIC
> +    bool
> +
>  config SIFIVE_PLIC
>      bool
>
> diff --git a/hw/intc/meson.build b/hw/intc/meson.build
> index 7466024402..5caa337654 100644
> --- a/hw/intc/meson.build
> +++ b/hw/intc/meson.build
> @@ -51,6 +51,7 @@ specific_ss.add(when: 'CONFIG_S390_FLIC_KVM', if_true:
> files('s390_flic_kvm.c'))
>  specific_ss.add(when: 'CONFIG_SH_INTC', if_true: files('sh_intc.c'))
>  specific_ss.add(when: 'CONFIG_RISCV_ACLINT', if_true:
> files('riscv_aclint.c'))
>  specific_ss.add(when: 'CONFIG_RISCV_APLIC', if_true:
> files('riscv_aplic.c'))
> +specific_ss.add(when: 'CONFIG_RISCV_IMSIC', if_true:
> files('riscv_imsic.c'))
>  specific_ss.add(when: 'CONFIG_SIFIVE_PLIC', if_true:
> files('sifive_plic.c'))
>  specific_ss.add(when: 'CONFIG_XICS', if_true: files('xics.c'))
>  specific_ss.add(when: ['CONFIG_KVM', 'CONFIG_XICS'],
> diff --git a/hw/intc/riscv_imsic.c b/hw/intc/riscv_imsic.c
> new file mode 100644
> index 0000000000..753fa11a9c
> --- /dev/null
> +++ b/hw/intc/riscv_imsic.c
> @@ -0,0 +1,447 @@
> +/*
> + * RISC-V IMSIC (Incoming Message Signaled Interrupt Controller)
> + *
> + * Copyright (c) 2021 Western Digital Corporation or its affiliates.
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2 or later, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but WITHOUT
> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> for
> + * more details.
> + *
> + * You should have received a copy of the GNU General Public License
> along with
> + * this program.  If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "qapi/error.h"
> +#include "qemu/log.h"
> +#include "qemu/module.h"
> +#include "qemu/error-report.h"
> +#include "qemu/bswap.h"
> +#include "exec/address-spaces.h"
> +#include "hw/sysbus.h"
> +#include "hw/pci/msi.h"
> +#include "hw/boards.h"
> +#include "hw/qdev-properties.h"
> +#include "hw/intc/riscv_imsic.h"
> +#include "hw/irq.h"
> +#include "target/riscv/cpu.h"
> +#include "target/riscv/cpu_bits.h"
> +#include "sysemu/sysemu.h"
> +#include "migration/vmstate.h"
> +
> +#define IMSIC_MMIO_PAGE_LE             0x00
> +#define IMSIC_MMIO_PAGE_BE             0x04
> +
> +#define IMSIC_MIN_ID                   ((IMSIC_EIPx_BITS * 2) - 1)
> +#define IMSIC_MAX_ID                   (IMSIC_TOPEI_IID_MASK)
> +
> +#define IMSIC_EISTATE_PENDING          (1U << 0)
> +#define IMSIC_EISTATE_ENABLED          (1U << 1)
> +#define IMSIC_EISTATE_ENPEND           (IMSIC_EISTATE_ENABLED | \
> +                                        IMSIC_EISTATE_PENDING)
> +
> +static uint32_t riscv_imsic_topei(RISCVIMSICState *imsic, uint32_t page)
> +{
> +    uint32_t i, max_irq, base;
> +
> +    base = page * imsic->num_irqs;
> +    max_irq = (imsic->num_irqs < imsic->eithreshold[page]) ?
> +              imsic->num_irqs : imsic->eithreshold[page];
>

Do we need to exclude the case which imsic->eithreshold[page] == 0?
  The value of a *topei CSR (mtopei, stopei, or vstopei) indicates the
interrupt file’s current
  highest-priority pending-and-enabled interrupt that also exceeds the
priority threshold specified by
  its eithreshold register if eithreshold is not zero.


> +    for (i = 1; i < max_irq; i++) {
> +        if ((imsic->eistate[base + i] & IMSIC_EISTATE_ENPEND) ==
> +                IMSIC_EISTATE_ENPEND) {
> +            return (i << IMSIC_TOPEI_IID_SHIFT) | i;
> +        }
> +    }
> +
> +    return 0;
> +}
> +
> +static void riscv_imsic_update(RISCVIMSICState *imsic, uint32_t page)
> +{
> +    if (imsic->eidelivery[page] && riscv_imsic_topei(imsic, page)) {
> +        qemu_irq_raise(imsic->external_irqs[page]);
> +    } else {
> +        qemu_irq_lower(imsic->external_irqs[page]);
> +    }
> +}
> +
> +static int riscv_imsic_eidelivery_rmw(RISCVIMSICState *imsic, uint32_t
> page,
> +                                      target_ulong *val,
> +                                      target_ulong new_val,
> +                                      target_ulong wr_mask)
> +{
> +    target_ulong old_val = imsic->eidelivery[page];
> +
> +    if (val) {
> +        *val = old_val;
> +    }
> +
> +    wr_mask &= 0x1;
> +    imsic->eidelivery[page] = (old_val & ~wr_mask) | (new_val & wr_mask);
> +
> +    riscv_imsic_update(imsic, page);
> +    return 0;
> +}
> +
> +static int riscv_imsic_eithreshold_rmw(RISCVIMSICState *imsic, uint32_t
> page,
> +                                      target_ulong *val,
> +                                      target_ulong new_val,
> +                                      target_ulong wr_mask)
> +{
> +    target_ulong old_val = imsic->eithreshold[page];
> +
> +    if (val) {
> +        *val = old_val;
> +    }
> +
> +    wr_mask &= IMSIC_MAX_ID;
> +    imsic->eithreshold[page] = (old_val & ~wr_mask) | (new_val & wr_mask);
> +
> +    riscv_imsic_update(imsic, page);
> +    return 0;
> +}
> +
> +static int riscv_imsic_topei_rmw(RISCVIMSICState *imsic, uint32_t page,
> +                                 target_ulong *val, target_ulong new_val,
> +                                 target_ulong wr_mask)
> +{
> +    uint32_t base, topei = riscv_imsic_topei(imsic, page);
> +
> +    /* Read pending and enabled interrupt with highest priority */
> +    if (val) {
> +        *val = topei;
> +    }
> +
> +    /* Writes ignore value and clear top pending interrupt */
> +    if (topei && wr_mask) {
> +        topei >>= IMSIC_TOPEI_IID_SHIFT;
> +        base = page * imsic->num_irqs;
> +        if (topei) {
> +            imsic->eistate[base + topei] &= ~IMSIC_EISTATE_PENDING;
> +        }
> +
> +        riscv_imsic_update(imsic, page);
> +    }
> +
> +    return 0;
> +}
> +
> +static int riscv_imsic_eix_rmw(RISCVIMSICState *imsic,
> +                               uint32_t xlen, uint32_t page,
> +                               uint32_t num, bool pend, target_ulong *val,
> +                               target_ulong new_val, target_ulong wr_mask)
> +{
> +    uint32_t i, base;
> +    target_ulong mask;
> +    uint32_t state = (pend) ? IMSIC_EISTATE_PENDING :
> IMSIC_EISTATE_ENABLED;
> +
> +    if (xlen != 32) {
> +        if (num & 0x1) {
> +            return -EINVAL;
> +        }
> +        num >>= 1;
> +    }
> +    if (num >= (imsic->num_irqs / xlen)) {
> +        return -EINVAL;
> +    }
> +
> +    base = (page * imsic->num_irqs) + (num * xlen);
> +
> +    if (val) {
> +        *val = 0;
> +        for (i = 0; i < xlen; i++) {
> +            mask = (target_ulong)1 << i;
> +            *val |= (imsic->eistate[base + i] & state) ? mask : 0;
> +        }
> +    }
> +
> +    for (i = 0; i < xlen; i++) {
> +        /* Bit0 of eip0 and eie0 are read-only zero */
> +        if (!num && !i) {
> +            continue;
> +        }
> +
> +        mask = (target_ulong)1 << i;
> +        if (wr_mask & mask) {
> +            if (new_val & mask) {
> +                imsic->eistate[base + i] |= state;
> +            } else {
> +                imsic->eistate[base + i] &= ~state;
> +            }
> +        }
> +    }
> +
> +    riscv_imsic_update(imsic, page);
> +    return 0;
> +}
> +
> +static int riscv_imsic_rmw(void *arg, target_ulong reg, target_ulong *val,
> +                           target_ulong new_val, target_ulong wr_mask)
> +{
> +    RISCVIMSICState *imsic = arg;
> +    uint32_t isel, priv, virt, vgein, xlen, page;
> +
> +    priv = AIA_IREG_PRIV(reg);
> +    virt = AIA_IREG_VIRT(reg);
> +    isel = AIA_IREG_ISEL(reg);
> +    vgein = AIA_IREG_VGEIN(reg);
> +    xlen = AIA_IREG_XLEN(reg);
> +
> +    if (imsic->mmode) {
> +        if (priv == PRV_M && !virt) {
> +            page = 0;
> +        } else {
> +            goto err;
> +        }
> +    } else {
> +        if (priv == PRV_S) {
> +            if (virt) {
> +                if (vgein && vgein < imsic->num_pages) {
> +                    page = vgein;
> +                } else {
> +                    goto err;
> +                }
> +            } else {
> +                page = 0;
> +            }
> +        } else {
> +            goto err;
> +        }
> +    }
> +
> +    switch (isel) {
> +    case ISELECT_IMSIC_EIDELIVERY:
> +        return riscv_imsic_eidelivery_rmw(imsic, page, val,
> +                                          new_val, wr_mask);
> +    case ISELECT_IMSIC_EITHRESHOLD:
> +        return riscv_imsic_eithreshold_rmw(imsic, page, val,
> +                                           new_val, wr_mask);
> +    case ISELECT_IMSIC_TOPEI:
> +        return riscv_imsic_topei_rmw(imsic, page, val, new_val, wr_mask);
> +    case ISELECT_IMSIC_EIP0 ... ISELECT_IMSIC_EIP63:
> +        return riscv_imsic_eix_rmw(imsic, xlen, page,
> +                                   isel - ISELECT_IMSIC_EIP0,
> +                                   true, val, new_val, wr_mask);
> +    case ISELECT_IMSIC_EIE0 ... ISELECT_IMSIC_EIE63:
> +        return riscv_imsic_eix_rmw(imsic, xlen, page,
> +                                   isel - ISELECT_IMSIC_EIE0,
> +                                   false, val, new_val, wr_mask);
> +    default:
> +        break;
> +    };
> +
> +err:
> +    qemu_log_mask(LOG_GUEST_ERROR,
> +                  "%s: Invalid register priv=%d virt=%d isel=%d
> vgein=%d\n",
> +                  __func__, priv, virt, isel, vgein);
> +    return -EINVAL;
> +}
> +
> +static uint64_t riscv_imsic_read(void *opaque, hwaddr addr, unsigned size)
> +{
> +    RISCVIMSICState *imsic = opaque;
> +
> +    /* Reads must be 4 byte words */
> +    if ((addr & 0x3) != 0) {
> +        goto err;
> +    }
> +
> +    /* Reads cannot be out of range */
> +    if (addr > IMSIC_MMIO_SIZE(imsic->num_pages)) {
> +        goto err;
> +    }
> +
> +    return 0;
> +
> +err:
> +    qemu_log_mask(LOG_GUEST_ERROR,
> +                  "%s: Invalid register read 0x%" HWADDR_PRIx "\n",
> +                  __func__, addr);
> +    return 0;
> +}
> +
> +static void riscv_imsic_write(void *opaque, hwaddr addr, uint64_t value,
> +        unsigned size)
> +{
> +    RISCVIMSICState *imsic = opaque;
> +    uint32_t page;
> +
> +    /* Writes must be 4 byte words */
> +    if ((addr & 0x3) != 0) {
> +        goto err;
> +    }
> +
> +    /* Writes cannot be out of range */
> +    if (addr > IMSIC_MMIO_SIZE(imsic->num_pages)) {
> +        goto err;
> +    }
> +
> +    /* Writes only supported for MSI little-endian registers */
> +    page = addr >> IMSIC_MMIO_PAGE_SHIFT;
> +    if ((addr & (IMSIC_MMIO_PAGE_SZ - 1)) == IMSIC_MMIO_PAGE_LE) {
> +        if (value && (value < imsic->num_irqs)) {
> +            imsic->eistate[(page * imsic->num_irqs) + value] |=
> +                                                    IMSIC_EISTATE_PENDING;
> +        }
> +    }
> +
> +    /* Update CPU external interrupt status */
> +    riscv_imsic_update(imsic, page);
> +
> +    return;
> +
> +err:
> +    qemu_log_mask(LOG_GUEST_ERROR,
> +                  "%s: Invalid register write 0x%" HWADDR_PRIx "\n",
> +                  __func__, addr);
> +}
> +
> +static const MemoryRegionOps riscv_imsic_ops = {
> +    .read = riscv_imsic_read,
> +    .write = riscv_imsic_write,
> +    .endianness = DEVICE_LITTLE_ENDIAN,
> +    .valid = {
> +        .min_access_size = 4,
> +        .max_access_size = 4
> +    }
> +};
> +
> +static void riscv_imsic_realize(DeviceState *dev, Error **errp)
> +{
> +    RISCVIMSICState *imsic = RISCV_IMSIC(dev);
> +    RISCVCPU *rcpu = RISCV_CPU(qemu_get_cpu(imsic->hartid));
> +    CPUState *cpu = qemu_get_cpu(imsic->hartid);
> +    CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
> +
> +    imsic->num_eistate = imsic->num_pages * imsic->num_irqs;
> +    imsic->eidelivery = g_new0(uint32_t, imsic->num_pages);
> +    imsic->eithreshold = g_new0(uint32_t, imsic->num_pages);
> +    imsic->eistate = g_new0(uint32_t, imsic->num_eistate);
> +
> +    memory_region_init_io(&imsic->mmio, OBJECT(dev), &riscv_imsic_ops,
> +                          imsic, TYPE_RISCV_IMSIC,
> +                          IMSIC_MMIO_SIZE(imsic->num_pages));
> +    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &imsic->mmio);
> +
> +    /* Claim the CPU interrupt to be triggered by this IMSIC */
> +    if (riscv_cpu_claim_interrupts(rcpu,
> +            (imsic->mmode) ? MIP_MEIP : MIP_SEIP) < 0) {
> +        error_report("%s already claimed",
> +                     (imsic->mmode) ? "MEIP" : "SEIP");
> +        exit(1);
> +    }
> +
> +    /* Create output IRQ lines */
> +    imsic->external_irqs = g_malloc(sizeof(qemu_irq) * imsic->num_pages);
> +    qdev_init_gpio_out(dev, imsic->external_irqs, imsic->num_pages);
> +
> +    /* Force select AIA feature and setup CSR read-modify-write callback
> */
> +    if (env) {
> +        riscv_set_feature(env, RISCV_FEATURE_AIA);
> +        if (!imsic->mmode) {
> +            riscv_cpu_set_geilen(env, imsic->num_pages - 1);
> +        }
> +        riscv_cpu_set_aia_ireg_rmw_fn(env, (imsic->mmode) ? PRV_M : PRV_S,
> +                                      riscv_imsic_rmw, imsic);
> +    }
> +
> +    msi_nonbroken = true;
> +}
> +
> +static Property riscv_imsic_properties[] = {
> +    DEFINE_PROP_BOOL("mmode", RISCVIMSICState, mmode, 0),
> +    DEFINE_PROP_UINT32("hartid", RISCVIMSICState, hartid, 0),
> +    DEFINE_PROP_UINT32("num-pages", RISCVIMSICState, num_pages, 0),
> +    DEFINE_PROP_UINT32("num-irqs", RISCVIMSICState, num_irqs, 0),
> +    DEFINE_PROP_END_OF_LIST(),
> +};
> +
> +static const VMStateDescription vmstate_riscv_imsic = {
> +    .name = "riscv_imsic",
> +    .version_id = 1,
> +    .minimum_version_id = 1,
> +    .fields = (VMStateField[]) {
> +            VMSTATE_VARRAY_UINT32(eidelivery, RISCVIMSICState,
> +                                  num_pages, 0,
> +                                  vmstate_info_uint32, uint32_t),
> +            VMSTATE_VARRAY_UINT32(eithreshold, RISCVIMSICState,
> +                                  num_pages, 0,
> +                                  vmstate_info_uint32, uint32_t),
> +            VMSTATE_VARRAY_UINT32(eistate, RISCVIMSICState,
> +                                  num_eistate, 0,
> +                                  vmstate_info_uint32, uint32_t),
> +            VMSTATE_END_OF_LIST()
> +        }
> +};
> +
> +static void riscv_imsic_class_init(ObjectClass *klass, void *data)
> +{
> +    DeviceClass *dc = DEVICE_CLASS(klass);
> +
> +    device_class_set_props(dc, riscv_imsic_properties);
> +    dc->realize = riscv_imsic_realize;
> +    dc->vmsd = &vmstate_riscv_imsic;
> +}
> +
> +static const TypeInfo riscv_imsic_info = {
> +    .name          = TYPE_RISCV_IMSIC,
> +    .parent        = TYPE_SYS_BUS_DEVICE,
> +    .instance_size = sizeof(RISCVIMSICState),
> +    .class_init    = riscv_imsic_class_init,
> +};
> +
> +static void riscv_imsic_register_types(void)
> +{
> +    type_register_static(&riscv_imsic_info);
> +}
> +
> +type_init(riscv_imsic_register_types)
> +
> +/*
> + * Create IMSIC device.
> + */
> +DeviceState *riscv_imsic_create(hwaddr addr, uint32_t hartid, bool mmode,
> +                                uint32_t num_pages, uint32_t num_ids)
> +{
> +    DeviceState *dev = qdev_new(TYPE_RISCV_IMSIC);
> +    CPUState *cpu = qemu_get_cpu(hartid);
> +    uint32_t i;
> +
> +    assert(!(addr & (IMSIC_MMIO_PAGE_SZ - 1)));
> +    if (mmode) {
> +        assert(num_pages == 1);
> +    } else {
> +        assert(num_pages >= 1 && num_pages <= (IRQ_LOCAL_GUEST_MAX + 1));
> +    }
> +    assert(IMSIC_MIN_ID <= num_ids);
> +    assert(num_ids <= IMSIC_MAX_ID);
> +    assert((num_ids & IMSIC_MIN_ID) == IMSIC_MIN_ID);
> +
> +    qdev_prop_set_bit(dev, "mmode", mmode);
> +    qdev_prop_set_uint32(dev, "hartid", hartid);
> +    qdev_prop_set_uint32(dev, "num-pages", num_pages);
> +    qdev_prop_set_uint32(dev, "num-irqs", num_ids + 1);
> +
> +    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
> +    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
> +
> +    for (i = 0; i < num_pages; i++) {
> +        if (!i) {
> +            qdev_connect_gpio_out_named(dev, NULL, i,
> +                                        qdev_get_gpio_in(DEVICE(cpu),
> +                                            (mmode) ? IRQ_M_EXT :
> IRQ_S_EXT));
> +        } else {
> +            qdev_connect_gpio_out_named(dev, NULL, i,
> +                                        qdev_get_gpio_in(DEVICE(cpu),
> +                                            IRQ_LOCAL_MAX + i - 1));
> +        }
> +    }
> +
> +    return dev;
> +}
> diff --git a/include/hw/intc/riscv_imsic.h b/include/hw/intc/riscv_imsic.h
> new file mode 100644
> index 0000000000..58c2aaa8dc
> --- /dev/null
> +++ b/include/hw/intc/riscv_imsic.h
> @@ -0,0 +1,68 @@
> +/*
> + * RISC-V IMSIC (Incoming Message Signal Interrupt Controller) interface
> + *
> + * Copyright (c) 2021 Western Digital Corporation or its affiliates.
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2 or later, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but WITHOUT
> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> for
> + * more details.
> + *
> + * You should have received a copy of the GNU General Public License
> along with
> + * this program.  If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef HW_RISCV_IMSIC_H
> +#define HW_RISCV_IMSIC_H
> +
> +#include "hw/sysbus.h"
> +#include "qom/object.h"
> +
> +#define TYPE_RISCV_IMSIC "riscv.imsic"
> +
> +typedef struct RISCVIMSICState RISCVIMSICState;
> +DECLARE_INSTANCE_CHECKER(RISCVIMSICState, RISCV_IMSIC, TYPE_RISCV_IMSIC)
> +
> +#define IMSIC_MMIO_PAGE_SHIFT          12
> +#define IMSIC_MMIO_PAGE_SZ             (1UL << IMSIC_MMIO_PAGE_SHIFT)
> +#define IMSIC_MMIO_SIZE(__num_pages)   ((__num_pages) *
> IMSIC_MMIO_PAGE_SZ)
> +
> +#define IMSIC_MMIO_HART_GUEST_MAX_BTIS 6
> +#define IMSIC_MMIO_GROUP_MIN_SHIFT     24
> +
> +#define IMSIC_HART_NUM_GUESTS(__guest_bits)           \
> +    (1U << (__guest_bits))
> +#define IMSIC_HART_SIZE(__guest_bits)                 \
> +    (IMSIC_HART_NUM_GUESTS(__guest_bits) * IMSIC_MMIO_PAGE_SZ)
> +#define IMSIC_GROUP_NUM_HARTS(__hart_bits)            \
> +    (1U << (__hart_bits))
> +#define IMSIC_GROUP_SIZE(__hart_bits, __guest_bits)   \
> +    (IMSIC_GROUP_NUM_HARTS(__hart_bits) * IMSIC_HART_SIZE(__guest_bits))
> +
> +struct RISCVIMSICState {
> +    /*< private >*/
> +    SysBusDevice parent_obj;
> +    qemu_irq *external_irqs;
> +
> +    /*< public >*/
> +    MemoryRegion mmio;
> +    uint32_t num_eistate;
> +    uint32_t *eidelivery;
> +    uint32_t *eithreshold;
> +    uint32_t *eistate;
> +
> +    /* config */
> +    bool mmode;
> +    uint32_t hartid;
> +    uint32_t num_pages;
> +    uint32_t num_irqs;
> +};
> +
> +DeviceState *riscv_imsic_create(hwaddr addr, uint32_t hartid, bool mmode,
> +                                uint32_t num_pages, uint32_t num_ids);
> +
> +#endif
> --
> 2.25.1
>
>
>

[-- Attachment #2: Type: text/html, Size: 25579 bytes --]

WARNING: multiple messages have this Message-ID (diff)
From: Frank Chang <frank.chang@sifive.com>
To: Anup Patel <anup@brainfault.org>
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	 Alistair Francis <Alistair.Francis@wdc.com>,
	Sagar Karandikar <sagark@eecs.berkeley.edu>,
	 Bin Meng <bmeng.cn@gmail.com>,
	"open list:RISC-V" <qemu-riscv@nongnu.org>,
	 "qemu-devel@nongnu.org Developers" <qemu-devel@nongnu.org>,
	Atish Patra <atishp@atishpatra.org>
Subject: Re: [PATCH v6 20/23] hw/intc: Add RISC-V AIA IMSIC device emulation
Date: Thu, 13 Jan 2022 15:26:30 +0800	[thread overview]
Message-ID: <CANzO1D3oYigRdBtYLVqsgAAhgU_JzKEB27SHx-7jg7b_qd8R1w@mail.gmail.com> (raw)
In-Reply-To: <20211230123539.52786-21-anup@brainfault.org>

[-- Attachment #1: Type: text/plain, Size: 21081 bytes --]

Anup Patel <anup@brainfault.org> 於 2021年12月30日 週四 下午9:00寫道:

> From: Anup Patel <anup.patel@wdc.com>
>
> The RISC-V AIA (Advanced Interrupt Architecture) defines a new
> interrupt controller for MSIs (message signal interrupts) called
> IMSIC (Incoming Message Signal Interrupt Controller). The IMSIC
> is per-HART device and also suppport virtualizaiton of MSIs using
> dedicated VS-level guest interrupt files.
>
> This patch adds device emulation for RISC-V AIA IMSIC which
> supports M-level, S-level, and VS-level MSIs.
>
> Signed-off-by: Anup Patel <anup.patel@wdc.com>
> Signed-off-by: Anup Patel <anup@brainfault.org>
> ---
>  hw/intc/Kconfig               |   3 +
>  hw/intc/meson.build           |   1 +
>  hw/intc/riscv_imsic.c         | 447 ++++++++++++++++++++++++++++++++++
>  include/hw/intc/riscv_imsic.h |  68 ++++++
>  4 files changed, 519 insertions(+)
>  create mode 100644 hw/intc/riscv_imsic.c
>  create mode 100644 include/hw/intc/riscv_imsic.h
>
> diff --git a/hw/intc/Kconfig b/hw/intc/Kconfig
> index 528e77b4a6..ec8d4cec29 100644
> --- a/hw/intc/Kconfig
> +++ b/hw/intc/Kconfig
> @@ -73,6 +73,9 @@ config RISCV_ACLINT
>  config RISCV_APLIC
>      bool
>
> +config RISCV_IMSIC
> +    bool
> +
>  config SIFIVE_PLIC
>      bool
>
> diff --git a/hw/intc/meson.build b/hw/intc/meson.build
> index 7466024402..5caa337654 100644
> --- a/hw/intc/meson.build
> +++ b/hw/intc/meson.build
> @@ -51,6 +51,7 @@ specific_ss.add(when: 'CONFIG_S390_FLIC_KVM', if_true:
> files('s390_flic_kvm.c'))
>  specific_ss.add(when: 'CONFIG_SH_INTC', if_true: files('sh_intc.c'))
>  specific_ss.add(when: 'CONFIG_RISCV_ACLINT', if_true:
> files('riscv_aclint.c'))
>  specific_ss.add(when: 'CONFIG_RISCV_APLIC', if_true:
> files('riscv_aplic.c'))
> +specific_ss.add(when: 'CONFIG_RISCV_IMSIC', if_true:
> files('riscv_imsic.c'))
>  specific_ss.add(when: 'CONFIG_SIFIVE_PLIC', if_true:
> files('sifive_plic.c'))
>  specific_ss.add(when: 'CONFIG_XICS', if_true: files('xics.c'))
>  specific_ss.add(when: ['CONFIG_KVM', 'CONFIG_XICS'],
> diff --git a/hw/intc/riscv_imsic.c b/hw/intc/riscv_imsic.c
> new file mode 100644
> index 0000000000..753fa11a9c
> --- /dev/null
> +++ b/hw/intc/riscv_imsic.c
> @@ -0,0 +1,447 @@
> +/*
> + * RISC-V IMSIC (Incoming Message Signaled Interrupt Controller)
> + *
> + * Copyright (c) 2021 Western Digital Corporation or its affiliates.
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2 or later, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but WITHOUT
> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> for
> + * more details.
> + *
> + * You should have received a copy of the GNU General Public License
> along with
> + * this program.  If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "qapi/error.h"
> +#include "qemu/log.h"
> +#include "qemu/module.h"
> +#include "qemu/error-report.h"
> +#include "qemu/bswap.h"
> +#include "exec/address-spaces.h"
> +#include "hw/sysbus.h"
> +#include "hw/pci/msi.h"
> +#include "hw/boards.h"
> +#include "hw/qdev-properties.h"
> +#include "hw/intc/riscv_imsic.h"
> +#include "hw/irq.h"
> +#include "target/riscv/cpu.h"
> +#include "target/riscv/cpu_bits.h"
> +#include "sysemu/sysemu.h"
> +#include "migration/vmstate.h"
> +
> +#define IMSIC_MMIO_PAGE_LE             0x00
> +#define IMSIC_MMIO_PAGE_BE             0x04
> +
> +#define IMSIC_MIN_ID                   ((IMSIC_EIPx_BITS * 2) - 1)
> +#define IMSIC_MAX_ID                   (IMSIC_TOPEI_IID_MASK)
> +
> +#define IMSIC_EISTATE_PENDING          (1U << 0)
> +#define IMSIC_EISTATE_ENABLED          (1U << 1)
> +#define IMSIC_EISTATE_ENPEND           (IMSIC_EISTATE_ENABLED | \
> +                                        IMSIC_EISTATE_PENDING)
> +
> +static uint32_t riscv_imsic_topei(RISCVIMSICState *imsic, uint32_t page)
> +{
> +    uint32_t i, max_irq, base;
> +
> +    base = page * imsic->num_irqs;
> +    max_irq = (imsic->num_irqs < imsic->eithreshold[page]) ?
> +              imsic->num_irqs : imsic->eithreshold[page];
>

Do we need to exclude the case which imsic->eithreshold[page] == 0?
  The value of a *topei CSR (mtopei, stopei, or vstopei) indicates the
interrupt file’s current
  highest-priority pending-and-enabled interrupt that also exceeds the
priority threshold specified by
  its eithreshold register if eithreshold is not zero.


> +    for (i = 1; i < max_irq; i++) {
> +        if ((imsic->eistate[base + i] & IMSIC_EISTATE_ENPEND) ==
> +                IMSIC_EISTATE_ENPEND) {
> +            return (i << IMSIC_TOPEI_IID_SHIFT) | i;
> +        }
> +    }
> +
> +    return 0;
> +}
> +
> +static void riscv_imsic_update(RISCVIMSICState *imsic, uint32_t page)
> +{
> +    if (imsic->eidelivery[page] && riscv_imsic_topei(imsic, page)) {
> +        qemu_irq_raise(imsic->external_irqs[page]);
> +    } else {
> +        qemu_irq_lower(imsic->external_irqs[page]);
> +    }
> +}
> +
> +static int riscv_imsic_eidelivery_rmw(RISCVIMSICState *imsic, uint32_t
> page,
> +                                      target_ulong *val,
> +                                      target_ulong new_val,
> +                                      target_ulong wr_mask)
> +{
> +    target_ulong old_val = imsic->eidelivery[page];
> +
> +    if (val) {
> +        *val = old_val;
> +    }
> +
> +    wr_mask &= 0x1;
> +    imsic->eidelivery[page] = (old_val & ~wr_mask) | (new_val & wr_mask);
> +
> +    riscv_imsic_update(imsic, page);
> +    return 0;
> +}
> +
> +static int riscv_imsic_eithreshold_rmw(RISCVIMSICState *imsic, uint32_t
> page,
> +                                      target_ulong *val,
> +                                      target_ulong new_val,
> +                                      target_ulong wr_mask)
> +{
> +    target_ulong old_val = imsic->eithreshold[page];
> +
> +    if (val) {
> +        *val = old_val;
> +    }
> +
> +    wr_mask &= IMSIC_MAX_ID;
> +    imsic->eithreshold[page] = (old_val & ~wr_mask) | (new_val & wr_mask);
> +
> +    riscv_imsic_update(imsic, page);
> +    return 0;
> +}
> +
> +static int riscv_imsic_topei_rmw(RISCVIMSICState *imsic, uint32_t page,
> +                                 target_ulong *val, target_ulong new_val,
> +                                 target_ulong wr_mask)
> +{
> +    uint32_t base, topei = riscv_imsic_topei(imsic, page);
> +
> +    /* Read pending and enabled interrupt with highest priority */
> +    if (val) {
> +        *val = topei;
> +    }
> +
> +    /* Writes ignore value and clear top pending interrupt */
> +    if (topei && wr_mask) {
> +        topei >>= IMSIC_TOPEI_IID_SHIFT;
> +        base = page * imsic->num_irqs;
> +        if (topei) {
> +            imsic->eistate[base + topei] &= ~IMSIC_EISTATE_PENDING;
> +        }
> +
> +        riscv_imsic_update(imsic, page);
> +    }
> +
> +    return 0;
> +}
> +
> +static int riscv_imsic_eix_rmw(RISCVIMSICState *imsic,
> +                               uint32_t xlen, uint32_t page,
> +                               uint32_t num, bool pend, target_ulong *val,
> +                               target_ulong new_val, target_ulong wr_mask)
> +{
> +    uint32_t i, base;
> +    target_ulong mask;
> +    uint32_t state = (pend) ? IMSIC_EISTATE_PENDING :
> IMSIC_EISTATE_ENABLED;
> +
> +    if (xlen != 32) {
> +        if (num & 0x1) {
> +            return -EINVAL;
> +        }
> +        num >>= 1;
> +    }
> +    if (num >= (imsic->num_irqs / xlen)) {
> +        return -EINVAL;
> +    }
> +
> +    base = (page * imsic->num_irqs) + (num * xlen);
> +
> +    if (val) {
> +        *val = 0;
> +        for (i = 0; i < xlen; i++) {
> +            mask = (target_ulong)1 << i;
> +            *val |= (imsic->eistate[base + i] & state) ? mask : 0;
> +        }
> +    }
> +
> +    for (i = 0; i < xlen; i++) {
> +        /* Bit0 of eip0 and eie0 are read-only zero */
> +        if (!num && !i) {
> +            continue;
> +        }
> +
> +        mask = (target_ulong)1 << i;
> +        if (wr_mask & mask) {
> +            if (new_val & mask) {
> +                imsic->eistate[base + i] |= state;
> +            } else {
> +                imsic->eistate[base + i] &= ~state;
> +            }
> +        }
> +    }
> +
> +    riscv_imsic_update(imsic, page);
> +    return 0;
> +}
> +
> +static int riscv_imsic_rmw(void *arg, target_ulong reg, target_ulong *val,
> +                           target_ulong new_val, target_ulong wr_mask)
> +{
> +    RISCVIMSICState *imsic = arg;
> +    uint32_t isel, priv, virt, vgein, xlen, page;
> +
> +    priv = AIA_IREG_PRIV(reg);
> +    virt = AIA_IREG_VIRT(reg);
> +    isel = AIA_IREG_ISEL(reg);
> +    vgein = AIA_IREG_VGEIN(reg);
> +    xlen = AIA_IREG_XLEN(reg);
> +
> +    if (imsic->mmode) {
> +        if (priv == PRV_M && !virt) {
> +            page = 0;
> +        } else {
> +            goto err;
> +        }
> +    } else {
> +        if (priv == PRV_S) {
> +            if (virt) {
> +                if (vgein && vgein < imsic->num_pages) {
> +                    page = vgein;
> +                } else {
> +                    goto err;
> +                }
> +            } else {
> +                page = 0;
> +            }
> +        } else {
> +            goto err;
> +        }
> +    }
> +
> +    switch (isel) {
> +    case ISELECT_IMSIC_EIDELIVERY:
> +        return riscv_imsic_eidelivery_rmw(imsic, page, val,
> +                                          new_val, wr_mask);
> +    case ISELECT_IMSIC_EITHRESHOLD:
> +        return riscv_imsic_eithreshold_rmw(imsic, page, val,
> +                                           new_val, wr_mask);
> +    case ISELECT_IMSIC_TOPEI:
> +        return riscv_imsic_topei_rmw(imsic, page, val, new_val, wr_mask);
> +    case ISELECT_IMSIC_EIP0 ... ISELECT_IMSIC_EIP63:
> +        return riscv_imsic_eix_rmw(imsic, xlen, page,
> +                                   isel - ISELECT_IMSIC_EIP0,
> +                                   true, val, new_val, wr_mask);
> +    case ISELECT_IMSIC_EIE0 ... ISELECT_IMSIC_EIE63:
> +        return riscv_imsic_eix_rmw(imsic, xlen, page,
> +                                   isel - ISELECT_IMSIC_EIE0,
> +                                   false, val, new_val, wr_mask);
> +    default:
> +        break;
> +    };
> +
> +err:
> +    qemu_log_mask(LOG_GUEST_ERROR,
> +                  "%s: Invalid register priv=%d virt=%d isel=%d
> vgein=%d\n",
> +                  __func__, priv, virt, isel, vgein);
> +    return -EINVAL;
> +}
> +
> +static uint64_t riscv_imsic_read(void *opaque, hwaddr addr, unsigned size)
> +{
> +    RISCVIMSICState *imsic = opaque;
> +
> +    /* Reads must be 4 byte words */
> +    if ((addr & 0x3) != 0) {
> +        goto err;
> +    }
> +
> +    /* Reads cannot be out of range */
> +    if (addr > IMSIC_MMIO_SIZE(imsic->num_pages)) {
> +        goto err;
> +    }
> +
> +    return 0;
> +
> +err:
> +    qemu_log_mask(LOG_GUEST_ERROR,
> +                  "%s: Invalid register read 0x%" HWADDR_PRIx "\n",
> +                  __func__, addr);
> +    return 0;
> +}
> +
> +static void riscv_imsic_write(void *opaque, hwaddr addr, uint64_t value,
> +        unsigned size)
> +{
> +    RISCVIMSICState *imsic = opaque;
> +    uint32_t page;
> +
> +    /* Writes must be 4 byte words */
> +    if ((addr & 0x3) != 0) {
> +        goto err;
> +    }
> +
> +    /* Writes cannot be out of range */
> +    if (addr > IMSIC_MMIO_SIZE(imsic->num_pages)) {
> +        goto err;
> +    }
> +
> +    /* Writes only supported for MSI little-endian registers */
> +    page = addr >> IMSIC_MMIO_PAGE_SHIFT;
> +    if ((addr & (IMSIC_MMIO_PAGE_SZ - 1)) == IMSIC_MMIO_PAGE_LE) {
> +        if (value && (value < imsic->num_irqs)) {
> +            imsic->eistate[(page * imsic->num_irqs) + value] |=
> +                                                    IMSIC_EISTATE_PENDING;
> +        }
> +    }
> +
> +    /* Update CPU external interrupt status */
> +    riscv_imsic_update(imsic, page);
> +
> +    return;
> +
> +err:
> +    qemu_log_mask(LOG_GUEST_ERROR,
> +                  "%s: Invalid register write 0x%" HWADDR_PRIx "\n",
> +                  __func__, addr);
> +}
> +
> +static const MemoryRegionOps riscv_imsic_ops = {
> +    .read = riscv_imsic_read,
> +    .write = riscv_imsic_write,
> +    .endianness = DEVICE_LITTLE_ENDIAN,
> +    .valid = {
> +        .min_access_size = 4,
> +        .max_access_size = 4
> +    }
> +};
> +
> +static void riscv_imsic_realize(DeviceState *dev, Error **errp)
> +{
> +    RISCVIMSICState *imsic = RISCV_IMSIC(dev);
> +    RISCVCPU *rcpu = RISCV_CPU(qemu_get_cpu(imsic->hartid));
> +    CPUState *cpu = qemu_get_cpu(imsic->hartid);
> +    CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
> +
> +    imsic->num_eistate = imsic->num_pages * imsic->num_irqs;
> +    imsic->eidelivery = g_new0(uint32_t, imsic->num_pages);
> +    imsic->eithreshold = g_new0(uint32_t, imsic->num_pages);
> +    imsic->eistate = g_new0(uint32_t, imsic->num_eistate);
> +
> +    memory_region_init_io(&imsic->mmio, OBJECT(dev), &riscv_imsic_ops,
> +                          imsic, TYPE_RISCV_IMSIC,
> +                          IMSIC_MMIO_SIZE(imsic->num_pages));
> +    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &imsic->mmio);
> +
> +    /* Claim the CPU interrupt to be triggered by this IMSIC */
> +    if (riscv_cpu_claim_interrupts(rcpu,
> +            (imsic->mmode) ? MIP_MEIP : MIP_SEIP) < 0) {
> +        error_report("%s already claimed",
> +                     (imsic->mmode) ? "MEIP" : "SEIP");
> +        exit(1);
> +    }
> +
> +    /* Create output IRQ lines */
> +    imsic->external_irqs = g_malloc(sizeof(qemu_irq) * imsic->num_pages);
> +    qdev_init_gpio_out(dev, imsic->external_irqs, imsic->num_pages);
> +
> +    /* Force select AIA feature and setup CSR read-modify-write callback
> */
> +    if (env) {
> +        riscv_set_feature(env, RISCV_FEATURE_AIA);
> +        if (!imsic->mmode) {
> +            riscv_cpu_set_geilen(env, imsic->num_pages - 1);
> +        }
> +        riscv_cpu_set_aia_ireg_rmw_fn(env, (imsic->mmode) ? PRV_M : PRV_S,
> +                                      riscv_imsic_rmw, imsic);
> +    }
> +
> +    msi_nonbroken = true;
> +}
> +
> +static Property riscv_imsic_properties[] = {
> +    DEFINE_PROP_BOOL("mmode", RISCVIMSICState, mmode, 0),
> +    DEFINE_PROP_UINT32("hartid", RISCVIMSICState, hartid, 0),
> +    DEFINE_PROP_UINT32("num-pages", RISCVIMSICState, num_pages, 0),
> +    DEFINE_PROP_UINT32("num-irqs", RISCVIMSICState, num_irqs, 0),
> +    DEFINE_PROP_END_OF_LIST(),
> +};
> +
> +static const VMStateDescription vmstate_riscv_imsic = {
> +    .name = "riscv_imsic",
> +    .version_id = 1,
> +    .minimum_version_id = 1,
> +    .fields = (VMStateField[]) {
> +            VMSTATE_VARRAY_UINT32(eidelivery, RISCVIMSICState,
> +                                  num_pages, 0,
> +                                  vmstate_info_uint32, uint32_t),
> +            VMSTATE_VARRAY_UINT32(eithreshold, RISCVIMSICState,
> +                                  num_pages, 0,
> +                                  vmstate_info_uint32, uint32_t),
> +            VMSTATE_VARRAY_UINT32(eistate, RISCVIMSICState,
> +                                  num_eistate, 0,
> +                                  vmstate_info_uint32, uint32_t),
> +            VMSTATE_END_OF_LIST()
> +        }
> +};
> +
> +static void riscv_imsic_class_init(ObjectClass *klass, void *data)
> +{
> +    DeviceClass *dc = DEVICE_CLASS(klass);
> +
> +    device_class_set_props(dc, riscv_imsic_properties);
> +    dc->realize = riscv_imsic_realize;
> +    dc->vmsd = &vmstate_riscv_imsic;
> +}
> +
> +static const TypeInfo riscv_imsic_info = {
> +    .name          = TYPE_RISCV_IMSIC,
> +    .parent        = TYPE_SYS_BUS_DEVICE,
> +    .instance_size = sizeof(RISCVIMSICState),
> +    .class_init    = riscv_imsic_class_init,
> +};
> +
> +static void riscv_imsic_register_types(void)
> +{
> +    type_register_static(&riscv_imsic_info);
> +}
> +
> +type_init(riscv_imsic_register_types)
> +
> +/*
> + * Create IMSIC device.
> + */
> +DeviceState *riscv_imsic_create(hwaddr addr, uint32_t hartid, bool mmode,
> +                                uint32_t num_pages, uint32_t num_ids)
> +{
> +    DeviceState *dev = qdev_new(TYPE_RISCV_IMSIC);
> +    CPUState *cpu = qemu_get_cpu(hartid);
> +    uint32_t i;
> +
> +    assert(!(addr & (IMSIC_MMIO_PAGE_SZ - 1)));
> +    if (mmode) {
> +        assert(num_pages == 1);
> +    } else {
> +        assert(num_pages >= 1 && num_pages <= (IRQ_LOCAL_GUEST_MAX + 1));
> +    }
> +    assert(IMSIC_MIN_ID <= num_ids);
> +    assert(num_ids <= IMSIC_MAX_ID);
> +    assert((num_ids & IMSIC_MIN_ID) == IMSIC_MIN_ID);
> +
> +    qdev_prop_set_bit(dev, "mmode", mmode);
> +    qdev_prop_set_uint32(dev, "hartid", hartid);
> +    qdev_prop_set_uint32(dev, "num-pages", num_pages);
> +    qdev_prop_set_uint32(dev, "num-irqs", num_ids + 1);
> +
> +    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
> +    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
> +
> +    for (i = 0; i < num_pages; i++) {
> +        if (!i) {
> +            qdev_connect_gpio_out_named(dev, NULL, i,
> +                                        qdev_get_gpio_in(DEVICE(cpu),
> +                                            (mmode) ? IRQ_M_EXT :
> IRQ_S_EXT));
> +        } else {
> +            qdev_connect_gpio_out_named(dev, NULL, i,
> +                                        qdev_get_gpio_in(DEVICE(cpu),
> +                                            IRQ_LOCAL_MAX + i - 1));
> +        }
> +    }
> +
> +    return dev;
> +}
> diff --git a/include/hw/intc/riscv_imsic.h b/include/hw/intc/riscv_imsic.h
> new file mode 100644
> index 0000000000..58c2aaa8dc
> --- /dev/null
> +++ b/include/hw/intc/riscv_imsic.h
> @@ -0,0 +1,68 @@
> +/*
> + * RISC-V IMSIC (Incoming Message Signal Interrupt Controller) interface
> + *
> + * Copyright (c) 2021 Western Digital Corporation or its affiliates.
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2 or later, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but WITHOUT
> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> for
> + * more details.
> + *
> + * You should have received a copy of the GNU General Public License
> along with
> + * this program.  If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef HW_RISCV_IMSIC_H
> +#define HW_RISCV_IMSIC_H
> +
> +#include "hw/sysbus.h"
> +#include "qom/object.h"
> +
> +#define TYPE_RISCV_IMSIC "riscv.imsic"
> +
> +typedef struct RISCVIMSICState RISCVIMSICState;
> +DECLARE_INSTANCE_CHECKER(RISCVIMSICState, RISCV_IMSIC, TYPE_RISCV_IMSIC)
> +
> +#define IMSIC_MMIO_PAGE_SHIFT          12
> +#define IMSIC_MMIO_PAGE_SZ             (1UL << IMSIC_MMIO_PAGE_SHIFT)
> +#define IMSIC_MMIO_SIZE(__num_pages)   ((__num_pages) *
> IMSIC_MMIO_PAGE_SZ)
> +
> +#define IMSIC_MMIO_HART_GUEST_MAX_BTIS 6
> +#define IMSIC_MMIO_GROUP_MIN_SHIFT     24
> +
> +#define IMSIC_HART_NUM_GUESTS(__guest_bits)           \
> +    (1U << (__guest_bits))
> +#define IMSIC_HART_SIZE(__guest_bits)                 \
> +    (IMSIC_HART_NUM_GUESTS(__guest_bits) * IMSIC_MMIO_PAGE_SZ)
> +#define IMSIC_GROUP_NUM_HARTS(__hart_bits)            \
> +    (1U << (__hart_bits))
> +#define IMSIC_GROUP_SIZE(__hart_bits, __guest_bits)   \
> +    (IMSIC_GROUP_NUM_HARTS(__hart_bits) * IMSIC_HART_SIZE(__guest_bits))
> +
> +struct RISCVIMSICState {
> +    /*< private >*/
> +    SysBusDevice parent_obj;
> +    qemu_irq *external_irqs;
> +
> +    /*< public >*/
> +    MemoryRegion mmio;
> +    uint32_t num_eistate;
> +    uint32_t *eidelivery;
> +    uint32_t *eithreshold;
> +    uint32_t *eistate;
> +
> +    /* config */
> +    bool mmode;
> +    uint32_t hartid;
> +    uint32_t num_pages;
> +    uint32_t num_irqs;
> +};
> +
> +DeviceState *riscv_imsic_create(hwaddr addr, uint32_t hartid, bool mmode,
> +                                uint32_t num_pages, uint32_t num_ids);
> +
> +#endif
> --
> 2.25.1
>
>
>

[-- Attachment #2: Type: text/html, Size: 25579 bytes --]

  reply	other threads:[~2022-01-13  7:50 UTC|newest]

Thread overview: 120+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-30 12:35 [PATCH v6 00/23] QEMU RISC-V AIA support Anup Patel
2021-12-30 12:35 ` Anup Patel
2021-12-30 12:35 ` [PATCH v6 01/23] target/riscv: Fix trap cause for RV32 HS-mode CSR access from RV64 HS-mode Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 12:29   ` Frank Chang
2022-01-12 12:29     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 02/23] target/riscv: Implement SGEIP bit in hip and hie CSRs Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-13 14:35   ` Frank Chang
2022-01-13 14:35     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 03/23] target/riscv: Implement hgeie and hgeip CSRs Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-14  6:37   ` Frank Chang
2022-01-14  6:37     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 04/23] target/riscv: Improve delivery of guest external interrupts Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-13  7:51   ` Frank Chang
2022-01-13  7:51     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 05/23] target/riscv: Allow setting CPU feature from machine/device emulation Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 12:34   ` Frank Chang
2022-01-12 12:34     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 06/23] target/riscv: Add AIA cpu feature Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 12:34   ` Frank Chang
2022-01-12 12:34     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 07/23] target/riscv: Add defines for AIA CSRs Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 12:57   ` Frank Chang
2022-01-12 12:57     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 08/23] target/riscv: Allow AIA device emulation to set ireg rmw callback Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 12:59   ` Frank Chang
2022-01-12 12:59     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 09/23] target/riscv: Implement AIA local interrupt priorities Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-10 13:08   ` Frank Chang
2022-01-10 13:08     ` Frank Chang
2022-01-11 17:18     ` Anup Patel
2022-01-11 17:18       ` Anup Patel
2022-01-12  3:00       ` Frank Chang
2022-01-12  3:00         ` Frank Chang
2022-01-13 10:45         ` Anup Patel
2022-01-13 10:45           ` Anup Patel
2022-01-13 14:21           ` Frank Chang
2022-01-13 14:21             ` Frank Chang
2022-01-10 13:25   ` Frank Chang
2022-01-10 13:25     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 10/23] target/riscv: Implement AIA CSRs for 64 local interrupts on RV32 Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-14  9:48   ` Frank Chang
2022-01-14  9:48     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 11/23] target/riscv: Implement AIA hvictl and hviprioX CSRs Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 13:15   ` Frank Chang
2022-01-12 13:15     ` Frank Chang
2022-01-13 10:49     ` Anup Patel
2022-01-13 10:49       ` Anup Patel
2021-12-30 12:35 ` [PATCH v6 12/23] target/riscv: Implement AIA interrupt filtering CSRs Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-11  6:00   ` Frank Chang
2022-01-11  6:00     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 13/23] target/riscv: Implement AIA mtopi, stopi, and vstopi CSRs Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 12:15   ` Frank Chang
2022-01-12 12:15     ` Frank Chang
2022-01-13 10:48     ` Anup Patel
2022-01-13 10:48       ` Anup Patel
2021-12-30 12:35 ` [PATCH v6 14/23] target/riscv: Implement AIA xiselect and xireg CSRs Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 16:40   ` Frank Chang
2022-01-12 16:40     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 15/23] target/riscv: Implement AIA IMSIC interface CSRs Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-05  3:30   ` Frank Chang
2022-01-05  3:30     ` Frank Chang
2022-01-08 12:03     ` Anup Patel
2022-01-08 12:03       ` Anup Patel
2021-12-30 12:35 ` [PATCH v6 16/23] hw/riscv: virt: Use AIA INTC compatible string when available Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 16:47   ` Frank Chang
2022-01-12 16:47     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 17/23] target/riscv: Allow users to force enable AIA CSRs in HART Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 13:19   ` Frank Chang
2022-01-12 13:19     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 18/23] hw/intc: Add RISC-V AIA APLIC device emulation Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-07  8:52   ` Frank Chang
2022-01-07  8:52     ` Frank Chang
2022-01-08 13:28     ` Anup Patel
2022-01-08 13:28       ` Anup Patel
2022-01-08  6:35   ` Frank Chang
2022-01-08  6:35     ` Frank Chang
2022-01-08 12:00     ` Anup Patel
2022-01-08 12:00       ` Anup Patel
2022-01-14 12:02   ` Frank Chang
2022-01-14 12:02     ` Frank Chang
2022-01-14 12:59     ` Anup Patel
2022-01-14 12:59       ` Anup Patel
2021-12-30 12:35 ` [PATCH v6 19/23] hw/riscv: virt: Add optional AIA APLIC support to virt machine Anup Patel
2021-12-30 12:35   ` Anup Patel
2021-12-30 12:35 ` [PATCH v6 20/23] hw/intc: Add RISC-V AIA IMSIC device emulation Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-13  7:26   ` Frank Chang [this message]
2022-01-13  7:26     ` Frank Chang
2022-01-13 12:22     ` Anup Patel
2022-01-13 12:22       ` Anup Patel
2021-12-30 12:35 ` [PATCH v6 21/23] hw/riscv: virt: Add optional AIA IMSIC support to virt machine Anup Patel
2021-12-30 12:35   ` Anup Patel
2021-12-30 12:35 ` [PATCH v6 22/23] docs/system: riscv: Document AIA options for " Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-12 13:23   ` Frank Chang
2022-01-12 13:23     ` Frank Chang
2021-12-30 12:35 ` [PATCH v6 23/23] hw/riscv: virt: Increase maximum number of allowed CPUs Anup Patel
2021-12-30 12:35   ` Anup Patel
2022-01-05 21:50   ` Alistair Francis
2022-01-05 21:50     ` Alistair Francis
2022-01-12 13:26   ` Frank Chang
2022-01-12 13:26     ` Frank Chang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CANzO1D3oYigRdBtYLVqsgAAhgU_JzKEB27SHx-7jg7b_qd8R1w@mail.gmail.com \
    --to=frank.chang@sifive.com \
    --cc=Alistair.Francis@wdc.com \
    --cc=anup@brainfault.org \
    --cc=atishp@atishpatra.org \
    --cc=bmeng.cn@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=sagark@eecs.berkeley.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.