Commit 171199f5 authored by Peter Maydell's avatar Peter Maydell
Browse files

Merge remote-tracking branch 'remotes/alistair/tags/pull-riscv-to-apply-20200619-3' into staging



This is a range of patches for RISC-V.

Some key points are:
 - Generalise the CPU init functions
 - Support the SiFive revB machine
 - Improvements to the Hypervisor implementation and error checking
 - Connect some OpenTitan devices
 - Changes to the sifive_u machine to support U-boot

v2:
 - Fix missing realise assert

# gpg: Signature made Fri 19 Jun 2020 17:34:34 BST
# gpg:                using RSA key F6C4AC46D4934868D3B8CE8F21E10D29DF977054
# gpg: Good signature from "Alistair Francis <alistair@alistair23.me>" [full]
# Primary key fingerprint: F6C4 AC46 D493 4868 D3B8  CE8F 21E1 0D29 DF97 7054

* remotes/alistair/tags/pull-riscv-to-apply-20200619-3: (32 commits)
  hw/riscv: sifive_u: Add a dummy DDR memory controller device
  hw/riscv: sifive_u: Sort the SoC memmap table entries
  hw/riscv: sifive_u: Support different boot source per MSEL pin state
  hw/riscv: sifive: Change SiFive E/U CPU reset vector to 0x1004
  target/riscv: Rename IBEX CPU init routine
  hw/riscv: sifive_u: Add a new property msel for MSEL pin state
  hw/riscv: sifive_u: Rename serial property get/set functions to a generic name
  hw/riscv: sifive_u: Add reset functionality
  hw/riscv: sifive_gpio: Do not blindly trigger output IRQs
  hw/riscv: sifive_u: Hook a GPIO controller
  hw/riscv: sifive_gpio: Add a new 'ngpio' property
  hw/riscv: sifive_gpio: Clean up the codes
  hw/riscv: sifive_u: Generate device tree node for OTP
  hw/riscv: sifive_u: Simplify the GEM IRQ connect code a little bit
  hw/riscv: opentitan: Remove the riscv_ prefix of the machine* and soc* functions
  hw/riscv: sifive_e: Remove the riscv_ prefix of the machine* and soc* functions
  target/riscv: Use a smaller guess size for no-MMU PMP
  riscv/opentitan: Connect the UART device
  riscv/opentitan: Connect the PLIC device
  hw/intc: Initial commit of lowRISC Ibex PLIC
  ...

Signed-off-by: default avatarPeter Maydell <peter.maydell@linaro.org>
parents bae31bfa 3eaea6eb
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -1250,7 +1250,11 @@ M: Alistair Francis <Alistair.Francis@wdc.com>
L: qemu-riscv@nongnu.org
S: Supported
F: hw/riscv/opentitan.c
F: hw/char/ibex_uart.c
F: hw/intc/ibex_plic.c
F: include/hw/riscv/opentitan.h
F: include/hw/char/ibex_uart.h
F: include/hw/intc/ibex_plic.h

SH4 Machines
------------
+1 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ common-obj-$(CONFIG_VIRTIO_SERIAL) += virtio-console.o
common-obj-$(CONFIG_XILINX) += xilinx_uartlite.o
common-obj-$(CONFIG_XEN) += xen_console.o
common-obj-$(CONFIG_CADENCE) += cadence_uart.o
common-obj-$(CONFIG_IBEX) += ibex_uart.o

common-obj-$(CONFIG_EXYNOS4) += exynos4210_uart.o
common-obj-$(CONFIG_COLDFIRE) += mcf_uart.o

hw/char/ibex_uart.c

0 → 100644
+492 −0
Original line number Diff line number Diff line
/*
 * QEMU lowRISC Ibex UART device
 *
 * Copyright (c) 2020 Western Digital
 *
 * For details check the documentation here:
 *    https://docs.opentitan.org/hw/ip/uart/doc/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "qemu/osdep.h"
#include "hw/char/ibex_uart.h"
#include "hw/irq.h"
#include "hw/qdev-properties.h"
#include "migration/vmstate.h"
#include "qemu/log.h"
#include "qemu/module.h"

static void ibex_uart_update_irqs(IbexUartState *s)
{
    if (s->uart_intr_state & s->uart_intr_enable & INTR_STATE_TX_WATERMARK) {
        qemu_set_irq(s->tx_watermark, 1);
    } else {
        qemu_set_irq(s->tx_watermark, 0);
    }

    if (s->uart_intr_state & s->uart_intr_enable & INTR_STATE_RX_WATERMARK) {
        qemu_set_irq(s->rx_watermark, 1);
    } else {
        qemu_set_irq(s->rx_watermark, 0);
    }

    if (s->uart_intr_state & s->uart_intr_enable & INTR_STATE_TX_EMPTY) {
        qemu_set_irq(s->tx_empty, 1);
    } else {
        qemu_set_irq(s->tx_empty, 0);
    }

    if (s->uart_intr_state & s->uart_intr_enable & INTR_STATE_RX_OVERFLOW) {
        qemu_set_irq(s->rx_overflow, 1);
    } else {
        qemu_set_irq(s->rx_overflow, 0);
    }
}

static int ibex_uart_can_receive(void *opaque)
{
    IbexUartState *s = opaque;

    if (s->uart_ctrl & UART_CTRL_RX_ENABLE) {
        return 1;
    }

    return 0;
}

static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)
{
    IbexUartState *s = opaque;
    uint8_t rx_fifo_level = (s->uart_fifo_ctrl & FIFO_CTRL_RXILVL)
                            >> FIFO_CTRL_RXILVL_SHIFT;

    s->uart_rdata = *buf;

    s->uart_status &= ~UART_STATUS_RXIDLE;
    s->uart_status &= ~UART_STATUS_RXEMPTY;

    if (size > rx_fifo_level) {
        s->uart_intr_state |= INTR_STATE_RX_WATERMARK;
    }

    ibex_uart_update_irqs(s);
}

static gboolean ibex_uart_xmit(GIOChannel *chan, GIOCondition cond,
                               void *opaque)
{
    IbexUartState *s = opaque;
    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & FIFO_CTRL_TXILVL)
                            >> FIFO_CTRL_TXILVL_SHIFT;
    int ret;

    /* instant drain the fifo when there's no back-end */
    if (!qemu_chr_fe_backend_connected(&s->chr)) {
        s->tx_level = 0;
        return FALSE;
    }

    if (!s->tx_level) {
        s->uart_status &= ~UART_STATUS_TXFULL;
        s->uart_status |= UART_STATUS_TXEMPTY;
        s->uart_intr_state |= INTR_STATE_TX_EMPTY;
        s->uart_intr_state &= ~INTR_STATE_TX_WATERMARK;
        ibex_uart_update_irqs(s);
        return FALSE;
    }

    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);

    if (ret >= 0) {
        s->tx_level -= ret;
        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
    }

    if (s->tx_level) {
        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
                                        ibex_uart_xmit, s);
        if (!r) {
            s->tx_level = 0;
            return FALSE;
        }
    }

    /* Clear the TX Full bit */
    if (s->tx_level != IBEX_UART_TX_FIFO_SIZE) {
        s->uart_status &= ~UART_STATUS_TXFULL;
    }

    /* Disable the TX_WATERMARK IRQ */
    if (s->tx_level < tx_fifo_level) {
        s->uart_intr_state &= ~INTR_STATE_TX_WATERMARK;
    }

    /* Set TX empty */
    if (s->tx_level == 0) {
        s->uart_status |= UART_STATUS_TXEMPTY;
        s->uart_intr_state |= INTR_STATE_TX_EMPTY;
    }

    ibex_uart_update_irqs(s);
    return FALSE;
}

static void uart_write_tx_fifo(IbexUartState *s, const uint8_t *buf,
                               int size)
{
    uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & FIFO_CTRL_TXILVL)
                            >> FIFO_CTRL_TXILVL_SHIFT;

    if (size > IBEX_UART_TX_FIFO_SIZE - s->tx_level) {
        size = IBEX_UART_TX_FIFO_SIZE - s->tx_level;
        qemu_log_mask(LOG_GUEST_ERROR, "ibex_uart: TX FIFO overflow");
    }

    memcpy(s->tx_fifo + s->tx_level, buf, size);
    s->tx_level += size;

    if (s->tx_level > 0) {
        s->uart_status &= ~UART_STATUS_TXEMPTY;
    }

    if (s->tx_level >= tx_fifo_level) {
        s->uart_intr_state |= INTR_STATE_TX_WATERMARK;
        ibex_uart_update_irqs(s);
    }

    if (s->tx_level == IBEX_UART_TX_FIFO_SIZE) {
        s->uart_status |= UART_STATUS_TXFULL;
    }

    timer_mod(s->fifo_trigger_handle, current_time +
              (s->char_tx_time * 4));
}

static void ibex_uart_reset(DeviceState *dev)
{
    IbexUartState *s = IBEX_UART(dev);

    s->uart_intr_state = 0x00000000;
    s->uart_intr_state = 0x00000000;
    s->uart_intr_enable = 0x00000000;
    s->uart_ctrl = 0x00000000;
    s->uart_status = 0x0000003c;
    s->uart_rdata = 0x00000000;
    s->uart_fifo_ctrl = 0x00000000;
    s->uart_fifo_status = 0x00000000;
    s->uart_ovrd = 0x00000000;
    s->uart_val = 0x00000000;
    s->uart_timeout_ctrl = 0x00000000;

    s->tx_level = 0;

    s->char_tx_time = (NANOSECONDS_PER_SECOND / 230400) * 10;

    ibex_uart_update_irqs(s);
}

static uint64_t ibex_uart_read(void *opaque, hwaddr addr,
                                       unsigned int size)
{
    IbexUartState *s = opaque;
    uint64_t retvalue = 0;

    switch (addr) {
    case IBEX_UART_INTR_STATE:
        retvalue = s->uart_intr_state;
        break;
    case IBEX_UART_INTR_ENABLE:
        retvalue = s->uart_intr_enable;
        break;
    case IBEX_UART_INTR_TEST:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: wdata is write only\n", __func__);
        break;

    case IBEX_UART_CTRL:
        retvalue = s->uart_ctrl;
        break;
    case IBEX_UART_STATUS:
        retvalue = s->uart_status;
        break;

    case IBEX_UART_RDATA:
        retvalue = s->uart_rdata;
        if (s->uart_ctrl & UART_CTRL_RX_ENABLE) {
            qemu_chr_fe_accept_input(&s->chr);

            s->uart_status |= UART_STATUS_RXIDLE;
            s->uart_status |= UART_STATUS_RXEMPTY;
        }
        break;
    case IBEX_UART_WDATA:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: wdata is write only\n", __func__);
        break;

    case IBEX_UART_FIFO_CTRL:
        retvalue = s->uart_fifo_ctrl;
        break;
    case IBEX_UART_FIFO_STATUS:
        retvalue = s->uart_fifo_status;

        retvalue |= s->tx_level & 0x1F;

        qemu_log_mask(LOG_UNIMP,
                      "%s: RX fifos are not supported\n", __func__);
        break;

    case IBEX_UART_OVRD:
        retvalue = s->uart_ovrd;
        qemu_log_mask(LOG_UNIMP,
                      "%s: ovrd is not supported\n", __func__);
        break;
    case IBEX_UART_VAL:
        retvalue = s->uart_val;
        qemu_log_mask(LOG_UNIMP,
                      "%s: val is not supported\n", __func__);
        break;
    case IBEX_UART_TIMEOUT_CTRL:
        retvalue = s->uart_timeout_ctrl;
        qemu_log_mask(LOG_UNIMP,
                      "%s: timeout_ctrl is not supported\n", __func__);
        break;
    default:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
        return 0;
    }

    return retvalue;
}

static void ibex_uart_write(void *opaque, hwaddr addr,
                                  uint64_t val64, unsigned int size)
{
    IbexUartState *s = opaque;
    uint32_t value = val64;

    switch (addr) {
    case IBEX_UART_INTR_STATE:
        /* Write 1 clear */
        s->uart_intr_state &= ~value;
        ibex_uart_update_irqs(s);
        break;
    case IBEX_UART_INTR_ENABLE:
        s->uart_intr_enable = value;
        ibex_uart_update_irqs(s);
        break;
    case IBEX_UART_INTR_TEST:
        s->uart_intr_state |= value;
        ibex_uart_update_irqs(s);
        break;

    case IBEX_UART_CTRL:
        s->uart_ctrl = value;

        if (value & UART_CTRL_NF) {
            qemu_log_mask(LOG_UNIMP,
                          "%s: UART_CTRL_NF is not supported\n", __func__);
        }
        if (value & UART_CTRL_SLPBK) {
            qemu_log_mask(LOG_UNIMP,
                          "%s: UART_CTRL_SLPBK is not supported\n", __func__);
        }
        if (value & UART_CTRL_LLPBK) {
            qemu_log_mask(LOG_UNIMP,
                          "%s: UART_CTRL_LLPBK is not supported\n", __func__);
        }
        if (value & UART_CTRL_PARITY_EN) {
            qemu_log_mask(LOG_UNIMP,
                          "%s: UART_CTRL_PARITY_EN is not supported\n",
                          __func__);
        }
        if (value & UART_CTRL_PARITY_ODD) {
            qemu_log_mask(LOG_UNIMP,
                          "%s: UART_CTRL_PARITY_ODD is not supported\n",
                          __func__);
        }
        if (value & UART_CTRL_RXBLVL) {
            qemu_log_mask(LOG_UNIMP,
                          "%s: UART_CTRL_RXBLVL is not supported\n", __func__);
        }
        if (value & UART_CTRL_NCO) {
            uint64_t baud = ((value & UART_CTRL_NCO) >> 16);
            baud *= 1000;
            baud /= 2 ^ 20;

            s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
        }
        break;
    case IBEX_UART_STATUS:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: status is read only\n", __func__);
        break;

    case IBEX_UART_RDATA:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: rdata is read only\n", __func__);
        break;
    case IBEX_UART_WDATA:
        uart_write_tx_fifo(s, (uint8_t *) &value, 1);
        break;

    case IBEX_UART_FIFO_CTRL:
        s->uart_fifo_ctrl = value;

        if (value & FIFO_CTRL_RXRST) {
            qemu_log_mask(LOG_UNIMP,
                          "%s: RX fifos are not supported\n", __func__);
        }
        if (value & FIFO_CTRL_TXRST) {
            s->tx_level = 0;
        }
        break;
    case IBEX_UART_FIFO_STATUS:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: fifo_status is read only\n", __func__);
        break;

    case IBEX_UART_OVRD:
        s->uart_ovrd = value;
        qemu_log_mask(LOG_UNIMP,
                      "%s: ovrd is not supported\n", __func__);
        break;
    case IBEX_UART_VAL:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: val is read only\n", __func__);
        break;
    case IBEX_UART_TIMEOUT_CTRL:
        s->uart_timeout_ctrl = value;
        qemu_log_mask(LOG_UNIMP,
                      "%s: timeout_ctrl is not supported\n", __func__);
        break;
    default:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
    }
}

static void fifo_trigger_update(void *opaque)
{
    IbexUartState *s = opaque;

    if (s->uart_ctrl & UART_CTRL_TX_ENABLE) {
        ibex_uart_xmit(NULL, G_IO_OUT, s);
    }
}

static const MemoryRegionOps ibex_uart_ops = {
    .read = ibex_uart_read,
    .write = ibex_uart_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
    .impl.min_access_size = 4,
    .impl.max_access_size = 4,
};

static int ibex_uart_post_load(void *opaque, int version_id)
{
    IbexUartState *s = opaque;

    ibex_uart_update_irqs(s);
    return 0;
}

static const VMStateDescription vmstate_ibex_uart = {
    .name = TYPE_IBEX_UART,
    .version_id = 1,
    .minimum_version_id = 1,
    .post_load = ibex_uart_post_load,
    .fields = (VMStateField[]) {
        VMSTATE_UINT8_ARRAY(tx_fifo, IbexUartState,
                            IBEX_UART_TX_FIFO_SIZE),
        VMSTATE_UINT32(tx_level, IbexUartState),
        VMSTATE_UINT64(char_tx_time, IbexUartState),
        VMSTATE_TIMER_PTR(fifo_trigger_handle, IbexUartState),
        VMSTATE_UINT32(uart_intr_state, IbexUartState),
        VMSTATE_UINT32(uart_intr_enable, IbexUartState),
        VMSTATE_UINT32(uart_ctrl, IbexUartState),
        VMSTATE_UINT32(uart_status, IbexUartState),
        VMSTATE_UINT32(uart_rdata, IbexUartState),
        VMSTATE_UINT32(uart_fifo_ctrl, IbexUartState),
        VMSTATE_UINT32(uart_fifo_status, IbexUartState),
        VMSTATE_UINT32(uart_ovrd, IbexUartState),
        VMSTATE_UINT32(uart_val, IbexUartState),
        VMSTATE_UINT32(uart_timeout_ctrl, IbexUartState),
        VMSTATE_END_OF_LIST()
    }
};

static Property ibex_uart_properties[] = {
    DEFINE_PROP_CHR("chardev", IbexUartState, chr),
    DEFINE_PROP_END_OF_LIST(),
};

static void ibex_uart_init(Object *obj)
{
    IbexUartState *s = IBEX_UART(obj);

    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_overflow);

    memory_region_init_io(&s->mmio, obj, &ibex_uart_ops, s,
                          TYPE_IBEX_UART, 0x400);
    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
}

static void ibex_uart_realize(DeviceState *dev, Error **errp)
{
    IbexUartState *s = IBEX_UART(dev);

    s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                          fifo_trigger_update, s);

    qemu_chr_fe_set_handlers(&s->chr, ibex_uart_can_receive,
                             ibex_uart_receive, NULL, NULL,
                             s, NULL, true);
}

static void ibex_uart_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);

    dc->reset = ibex_uart_reset;
    dc->realize = ibex_uart_realize;
    dc->vmsd = &vmstate_ibex_uart;
    device_class_set_props(dc, ibex_uart_properties);
}

static const TypeInfo ibex_uart_info = {
    .name          = TYPE_IBEX_UART,
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(IbexUartState),
    .instance_init = ibex_uart_init,
    .class_init    = ibex_uart_class_init,
};

static void ibex_uart_register_types(void)
{
    type_register_static(&ibex_uart_info);
}

type_init(ibex_uart_register_types)
+1 −0
Original line number Diff line number Diff line
@@ -49,3 +49,4 @@ obj-$(CONFIG_ARM_GIC) += arm_gicv3_cpuif.o
obj-$(CONFIG_MIPS_CPS) += mips_gic.o
obj-$(CONFIG_NIOS2) += nios2_iic.o
obj-$(CONFIG_OMPIC) += ompic.o
obj-$(CONFIG_IBEX) += ibex_plic.o

hw/intc/ibex_plic.c

0 → 100644
+261 −0
Original line number Diff line number Diff line
/*
 * QEMU RISC-V lowRISC Ibex PLIC
 *
 * Copyright (c) 2020 Western Digital
 *
 * Documentation avaliable: https://docs.opentitan.org/hw/ip/rv_plic/doc/
 *
 * 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 "qemu/log.h"
#include "hw/qdev-properties.h"
#include "hw/core/cpu.h"
#include "hw/boards.h"
#include "hw/pci/msi.h"
#include "target/riscv/cpu_bits.h"
#include "target/riscv/cpu.h"
#include "hw/intc/ibex_plic.h"

static bool addr_between(uint32_t addr, uint32_t base, uint32_t num)
{
    uint32_t end = base + (num * 0x04);

    if (addr >= base && addr < end) {
        return true;
    }

    return false;
}

static void ibex_plic_irqs_set_pending(IbexPlicState *s, int irq, bool level)
{
    int pending_num = irq / 32;

    s->pending[pending_num] |= level << (irq % 32);
}

static bool ibex_plic_irqs_pending(IbexPlicState *s, uint32_t context)
{
    int i;

    for (i = 0; i < s->pending_num; i++) {
        uint32_t irq_num = ctz64(s->pending[i]) + (i * 32);

        if (!(s->pending[i] & s->enable[i])) {
            /* No pending and enabled IRQ */
            continue;
        }

        if (s->priority[irq_num] > s->threshold) {
            if (!s->claim) {
                s->claim = irq_num;
            }
            return true;
        }
    }

    return false;
}

static void ibex_plic_update(IbexPlicState *s)
{
    CPUState *cpu;
    int level, i;

    for (i = 0; i < s->num_cpus; i++) {
        cpu = qemu_get_cpu(i);

        if (!cpu) {
            continue;
        }

        level = ibex_plic_irqs_pending(s, 0);

        riscv_cpu_update_mip(RISCV_CPU(cpu), MIP_MEIP, BOOL_TO_MASK(level));
    }
}

static void ibex_plic_reset(DeviceState *dev)
{
    IbexPlicState *s = IBEX_PLIC(dev);

    s->threshold = 0x00000000;
    s->claim = 0x00000000;
}

static uint64_t ibex_plic_read(void *opaque, hwaddr addr,
                               unsigned int size)
{
    IbexPlicState *s = opaque;
    int offset;
    uint32_t ret = 0;

    if (addr_between(addr, s->pending_base, s->pending_num)) {
        offset = (addr - s->pending_base) / 4;
        ret = s->pending[offset];
    } else if (addr_between(addr, s->source_base, s->source_num)) {
        qemu_log_mask(LOG_UNIMP,
                      "%s: Interrupt source mode not supported\n", __func__);
    } else if (addr_between(addr, s->priority_base, s->priority_num)) {
        offset = (addr - s->priority_base) / 4;
        ret = s->priority[offset];
    } else if (addr_between(addr, s->enable_base, s->enable_num)) {
        offset = (addr - s->enable_base) / 4;
        ret = s->enable[offset];
    } else if (addr_between(addr, s->threshold_base, 1)) {
        ret = s->threshold;
    } else if (addr_between(addr, s->claim_base, 1)) {
        int pending_num = s->claim / 32;
        s->pending[pending_num] &= ~(1 << (s->claim % 32));

        ret = s->claim;
    }

    return ret;
}

static void ibex_plic_write(void *opaque, hwaddr addr,
                            uint64_t value, unsigned int size)
{
    IbexPlicState *s = opaque;

    if (addr_between(addr, s->pending_base, s->pending_num)) {
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: Pending registers are read only\n", __func__);
    } else if (addr_between(addr, s->source_base, s->source_num)) {
        qemu_log_mask(LOG_UNIMP,
                      "%s: Interrupt source mode not supported\n", __func__);
    } else if (addr_between(addr, s->priority_base, s->priority_num)) {
        uint32_t irq = ((addr - s->priority_base) >> 2) + 1;
        s->priority[irq] = value & 7;
    } else if (addr_between(addr, s->enable_base, s->enable_num)) {
        uint32_t enable_reg = (addr - s->enable_base) / 4;

        s->enable[enable_reg] = value;
    } else if (addr_between(addr, s->threshold_base, 1)) {
        s->threshold = value & 3;
    } else if (addr_between(addr, s->claim_base, 1)) {
        if (s->claim == value) {
            /* Interrupt was completed */
            s->claim = 0;
        }
    }

    ibex_plic_update(s);
}

static const MemoryRegionOps ibex_plic_ops = {
    .read = ibex_plic_read,
    .write = ibex_plic_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
    .valid = {
        .min_access_size = 4,
        .max_access_size = 4
    }
};

static void ibex_plic_irq_request(void *opaque, int irq, int level)
{
    IbexPlicState *s = opaque;

    ibex_plic_irqs_set_pending(s, irq, level > 0);
    ibex_plic_update(s);
}

static Property ibex_plic_properties[] = {
    DEFINE_PROP_UINT32("num-cpus", IbexPlicState, num_cpus, 1),
    DEFINE_PROP_UINT32("num-sources", IbexPlicState, num_sources, 80),

    DEFINE_PROP_UINT32("pending-base", IbexPlicState, pending_base, 0),
    DEFINE_PROP_UINT32("pending-num", IbexPlicState, pending_num, 3),

    DEFINE_PROP_UINT32("source-base", IbexPlicState, source_base, 0x0c),
    DEFINE_PROP_UINT32("source-num", IbexPlicState, source_num, 3),

    DEFINE_PROP_UINT32("priority-base", IbexPlicState, priority_base, 0x18),
    DEFINE_PROP_UINT32("priority-num", IbexPlicState, priority_num, 80),

    DEFINE_PROP_UINT32("enable-base", IbexPlicState, enable_base, 0x200),
    DEFINE_PROP_UINT32("enable-num", IbexPlicState, enable_num, 3),

    DEFINE_PROP_UINT32("threshold-base", IbexPlicState, threshold_base, 0x20c),

    DEFINE_PROP_UINT32("claim-base", IbexPlicState, claim_base, 0x210),
    DEFINE_PROP_END_OF_LIST(),
};

static void ibex_plic_init(Object *obj)
{
    IbexPlicState *s = IBEX_PLIC(obj);

    memory_region_init_io(&s->mmio, obj, &ibex_plic_ops, s,
                          TYPE_IBEX_PLIC, 0x400);
    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
}

static void ibex_plic_realize(DeviceState *dev, Error **errp)
{
    IbexPlicState *s = IBEX_PLIC(dev);
    int i;

    s->pending = g_new0(uint32_t, s->pending_num);
    s->source = g_new0(uint32_t, s->source_num);
    s->priority = g_new0(uint32_t, s->priority_num);
    s->enable = g_new0(uint32_t, s->enable_num);

    qdev_init_gpio_in(dev, ibex_plic_irq_request, s->num_sources);

    /*
     * We can't allow the supervisor to control SEIP as this would allow the
     * supervisor to clear a pending external interrupt which will result in
     * a lost interrupt in the case a PLIC is attached. The SEIP bit must be
     * hardware controlled when a PLIC is attached.
     */
    MachineState *ms = MACHINE(qdev_get_machine());
    unsigned int smp_cpus = ms->smp.cpus;
    for (i = 0; i < smp_cpus; i++) {
        RISCVCPU *cpu = RISCV_CPU(qemu_get_cpu(i));
        if (riscv_cpu_claim_interrupts(cpu, MIP_SEIP) < 0) {
            error_report("SEIP already claimed");
            exit(1);
        }
    }

    msi_nonbroken = true;
}

static void ibex_plic_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);

    dc->reset = ibex_plic_reset;
    device_class_set_props(dc, ibex_plic_properties);
    dc->realize = ibex_plic_realize;
}

static const TypeInfo ibex_plic_info = {
    .name          = TYPE_IBEX_PLIC,
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(IbexPlicState),
    .instance_init = ibex_plic_init,
    .class_init    = ibex_plic_class_init,
};

static void ibex_plic_register_types(void)
{
    type_register_static(&ibex_plic_info);
}

type_init(ibex_plic_register_types)
Loading