2018-03-02 15:31:12 +03:00
|
|
|
/*
|
|
|
|
* SiFive CLINT (Core Local Interruptor)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
|
|
|
|
* Copyright (c) 2017 SiFive, Inc.
|
|
|
|
*
|
|
|
|
* This provides real-time clock, timer and interprocessor interrupts.
|
|
|
|
*
|
|
|
|
* 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"
|
qdev: Convert uses of qdev_create() with Coccinelle
This is the transformation explained in the commit before previous.
Takes care of just one pattern that needs conversion. More to come in
this series.
Coccinelle script:
@ depends on !(file in "hw/arm/highbank.c")@
expression bus, type_name, dev, expr;
@@
- dev = qdev_create(bus, type_name);
+ dev = qdev_new(type_name);
... when != dev = expr
- qdev_init_nofail(dev);
+ qdev_realize_and_unref(dev, bus, &error_fatal);
@@
expression bus, type_name, dev, expr;
identifier DOWN;
@@
- dev = DOWN(qdev_create(bus, type_name));
+ dev = DOWN(qdev_new(type_name));
... when != dev = expr
- qdev_init_nofail(DEVICE(dev));
+ qdev_realize_and_unref(DEVICE(dev), bus, &error_fatal);
@@
expression bus, type_name, expr;
identifier dev;
@@
- DeviceState *dev = qdev_create(bus, type_name);
+ DeviceState *dev = qdev_new(type_name);
... when != dev = expr
- qdev_init_nofail(dev);
+ qdev_realize_and_unref(dev, bus, &error_fatal);
@@
expression bus, type_name, dev, expr, errp;
symbol true;
@@
- dev = qdev_create(bus, type_name);
+ dev = qdev_new(type_name);
... when != dev = expr
- object_property_set_bool(OBJECT(dev), true, "realized", errp);
+ qdev_realize_and_unref(dev, bus, errp);
@@
expression bus, type_name, expr, errp;
identifier dev;
symbol true;
@@
- DeviceState *dev = qdev_create(bus, type_name);
+ DeviceState *dev = qdev_new(type_name);
... when != dev = expr
- object_property_set_bool(OBJECT(dev), true, "realized", errp);
+ qdev_realize_and_unref(dev, bus, errp);
The first rule exempts hw/arm/highbank.c, because it matches along two
control flow paths there, with different @type_name. Covered by the
next commit's manual conversions.
Missing #include "qapi/error.h" added manually.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200610053247.1583243-10-armbru@redhat.com>
[Conflicts in hw/misc/empty_slot.c and hw/sparc/leon3.c resolved]
2020-06-10 08:31:58 +03:00
|
|
|
#include "qapi/error.h"
|
2018-03-02 15:31:12 +03:00
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2018-03-02 15:31:12 +03:00
|
|
|
#include "hw/sysbus.h"
|
|
|
|
#include "target/riscv/cpu.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2021-08-31 14:06:00 +03:00
|
|
|
#include "hw/intc/riscv_aclint.h"
|
2018-03-02 15:31:12 +03:00
|
|
|
#include "qemu/timer.h"
|
2021-08-30 08:34:36 +03:00
|
|
|
#include "hw/irq.h"
|
|
|
|
|
|
|
|
typedef struct sifive_clint_callback {
|
|
|
|
SiFiveCLINTState *s;
|
|
|
|
int num;
|
|
|
|
} sifive_clint_callback;
|
2018-03-02 15:31:12 +03:00
|
|
|
|
2020-09-01 04:39:10 +03:00
|
|
|
static uint64_t cpu_riscv_read_rtc(uint32_t timebase_freq)
|
2018-03-02 15:31:12 +03:00
|
|
|
{
|
2018-03-03 04:30:07 +03:00
|
|
|
return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
|
2020-09-01 04:39:10 +03:00
|
|
|
timebase_freq, NANOSECONDS_PER_SECOND);
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when timecmp is written to update the QEMU timer or immediately
|
|
|
|
* trigger timer interrupt if mtimecmp <= current timer value.
|
|
|
|
*/
|
2021-08-30 08:34:36 +03:00
|
|
|
static void sifive_clint_write_timecmp(SiFiveCLINTState *s, RISCVCPU *cpu,
|
|
|
|
int hartid,
|
|
|
|
uint64_t value,
|
2020-09-01 04:39:10 +03:00
|
|
|
uint32_t timebase_freq)
|
2018-03-02 15:31:12 +03:00
|
|
|
{
|
|
|
|
uint64_t next;
|
|
|
|
uint64_t diff;
|
|
|
|
|
2020-09-01 04:39:10 +03:00
|
|
|
uint64_t rtc_r = cpu_riscv_read_rtc(timebase_freq);
|
2018-03-02 15:31:12 +03:00
|
|
|
|
|
|
|
cpu->env.timecmp = value;
|
|
|
|
if (cpu->env.timecmp <= rtc_r) {
|
|
|
|
/* if we're setting an MTIMECMP value in the "past",
|
|
|
|
immediately raise the timer interrupt */
|
2021-08-30 08:34:36 +03:00
|
|
|
qemu_irq_raise(s->timer_irqs[hartid - s->hartid_base]);
|
2018-03-02 15:31:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise, set up the future timer interrupt */
|
2021-08-30 08:34:36 +03:00
|
|
|
qemu_irq_lower(s->timer_irqs[hartid - s->hartid_base]);
|
2018-03-02 15:31:12 +03:00
|
|
|
diff = cpu->env.timecmp - rtc_r;
|
|
|
|
/* back to ns (note args switched in muldiv64) */
|
2021-08-27 18:23:25 +03:00
|
|
|
uint64_t ns_diff = muldiv64(diff, NANOSECONDS_PER_SECOND, timebase_freq);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check if ns_diff overflowed and check if the addition would potentially
|
|
|
|
* overflow
|
|
|
|
*/
|
|
|
|
if ((NANOSECONDS_PER_SECOND > timebase_freq && ns_diff < diff) ||
|
|
|
|
ns_diff > INT64_MAX) {
|
|
|
|
next = INT64_MAX;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* as it is very unlikely qemu_clock_get_ns will return a value
|
|
|
|
* greater than INT64_MAX, no additional check is needed for an
|
|
|
|
* unsigned integer overflow.
|
|
|
|
*/
|
|
|
|
next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns_diff;
|
|
|
|
/*
|
|
|
|
* if ns_diff is INT64_MAX next may still be outside the range
|
|
|
|
* of a signed integer.
|
|
|
|
*/
|
|
|
|
next = MIN(next, INT64_MAX);
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:31:12 +03:00
|
|
|
timer_mod(cpu->env.timer, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callback used when the timer set using timer_mod expires.
|
|
|
|
* Should raise the timer interrupt line
|
|
|
|
*/
|
|
|
|
static void sifive_clint_timer_cb(void *opaque)
|
|
|
|
{
|
2021-08-30 08:34:36 +03:00
|
|
|
sifive_clint_callback *state = opaque;
|
|
|
|
|
|
|
|
qemu_irq_raise(state->s->timer_irqs[state->num]);
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CPU wants to read rtc or timecmp register */
|
|
|
|
static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)
|
|
|
|
{
|
|
|
|
SiFiveCLINTState *clint = opaque;
|
|
|
|
if (addr >= clint->sip_base &&
|
|
|
|
addr < clint->sip_base + (clint->num_harts << 2)) {
|
2020-05-14 13:21:31 +03:00
|
|
|
size_t hartid = clint->hartid_base + ((addr - clint->sip_base) >> 2);
|
2018-03-02 15:31:12 +03:00
|
|
|
CPUState *cpu = qemu_get_cpu(hartid);
|
|
|
|
CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
|
|
|
|
if (!env) {
|
|
|
|
error_report("clint: invalid timecmp hartid: %zu", hartid);
|
|
|
|
} else if ((addr & 0x3) == 0) {
|
|
|
|
return (env->mip & MIP_MSIP) > 0;
|
|
|
|
} else {
|
|
|
|
error_report("clint: invalid read: %08x", (uint32_t)addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (addr >= clint->timecmp_base &&
|
|
|
|
addr < clint->timecmp_base + (clint->num_harts << 3)) {
|
2020-05-14 13:21:31 +03:00
|
|
|
size_t hartid = clint->hartid_base +
|
|
|
|
((addr - clint->timecmp_base) >> 3);
|
2018-03-02 15:31:12 +03:00
|
|
|
CPUState *cpu = qemu_get_cpu(hartid);
|
|
|
|
CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
|
|
|
|
if (!env) {
|
|
|
|
error_report("clint: invalid timecmp hartid: %zu", hartid);
|
|
|
|
} else if ((addr & 0x7) == 0) {
|
|
|
|
/* timecmp_lo */
|
|
|
|
uint64_t timecmp = env->timecmp;
|
|
|
|
return timecmp & 0xFFFFFFFF;
|
|
|
|
} else if ((addr & 0x7) == 4) {
|
|
|
|
/* timecmp_hi */
|
|
|
|
uint64_t timecmp = env->timecmp;
|
|
|
|
return (timecmp >> 32) & 0xFFFFFFFF;
|
|
|
|
} else {
|
|
|
|
error_report("clint: invalid read: %08x", (uint32_t)addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (addr == clint->time_base) {
|
|
|
|
/* time_lo */
|
2020-09-01 04:39:10 +03:00
|
|
|
return cpu_riscv_read_rtc(clint->timebase_freq) & 0xFFFFFFFF;
|
2018-03-02 15:31:12 +03:00
|
|
|
} else if (addr == clint->time_base + 4) {
|
|
|
|
/* time_hi */
|
2020-09-01 04:39:10 +03:00
|
|
|
return (cpu_riscv_read_rtc(clint->timebase_freq) >> 32) & 0xFFFFFFFF;
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
error_report("clint: invalid read: %08x", (uint32_t)addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CPU wrote to rtc or timecmp register */
|
|
|
|
static void sifive_clint_write(void *opaque, hwaddr addr, uint64_t value,
|
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
SiFiveCLINTState *clint = opaque;
|
|
|
|
|
|
|
|
if (addr >= clint->sip_base &&
|
|
|
|
addr < clint->sip_base + (clint->num_harts << 2)) {
|
2020-05-14 13:21:31 +03:00
|
|
|
size_t hartid = clint->hartid_base + ((addr - clint->sip_base) >> 2);
|
2018-03-02 15:31:12 +03:00
|
|
|
CPUState *cpu = qemu_get_cpu(hartid);
|
|
|
|
CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
|
|
|
|
if (!env) {
|
|
|
|
error_report("clint: invalid timecmp hartid: %zu", hartid);
|
|
|
|
} else if ((addr & 0x3) == 0) {
|
2021-08-30 08:34:36 +03:00
|
|
|
qemu_set_irq(clint->soft_irqs[hartid - clint->hartid_base], value);
|
2018-03-02 15:31:12 +03:00
|
|
|
} else {
|
|
|
|
error_report("clint: invalid sip write: %08x", (uint32_t)addr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (addr >= clint->timecmp_base &&
|
|
|
|
addr < clint->timecmp_base + (clint->num_harts << 3)) {
|
2020-05-14 13:21:31 +03:00
|
|
|
size_t hartid = clint->hartid_base +
|
|
|
|
((addr - clint->timecmp_base) >> 3);
|
2018-03-02 15:31:12 +03:00
|
|
|
CPUState *cpu = qemu_get_cpu(hartid);
|
|
|
|
CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
|
|
|
|
if (!env) {
|
|
|
|
error_report("clint: invalid timecmp hartid: %zu", hartid);
|
|
|
|
} else if ((addr & 0x7) == 0) {
|
|
|
|
/* timecmp_lo */
|
2018-12-14 03:18:39 +03:00
|
|
|
uint64_t timecmp_hi = env->timecmp >> 32;
|
2021-08-30 08:34:36 +03:00
|
|
|
sifive_clint_write_timecmp(clint, RISCV_CPU(cpu), hartid,
|
2020-09-01 04:39:10 +03:00
|
|
|
timecmp_hi << 32 | (value & 0xFFFFFFFF), clint->timebase_freq);
|
2018-03-02 15:31:12 +03:00
|
|
|
return;
|
|
|
|
} else if ((addr & 0x7) == 4) {
|
|
|
|
/* timecmp_hi */
|
2018-12-14 03:18:39 +03:00
|
|
|
uint64_t timecmp_lo = env->timecmp;
|
2021-08-30 08:34:36 +03:00
|
|
|
sifive_clint_write_timecmp(clint, RISCV_CPU(cpu), hartid,
|
2020-09-01 04:39:10 +03:00
|
|
|
value << 32 | (timecmp_lo & 0xFFFFFFFF), clint->timebase_freq);
|
2018-03-02 15:31:12 +03:00
|
|
|
} else {
|
|
|
|
error_report("clint: invalid timecmp write: %08x", (uint32_t)addr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (addr == clint->time_base) {
|
|
|
|
/* time_lo */
|
|
|
|
error_report("clint: time_lo write not implemented");
|
|
|
|
return;
|
|
|
|
} else if (addr == clint->time_base + 4) {
|
|
|
|
/* time_hi */
|
|
|
|
error_report("clint: time_hi write not implemented");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_report("clint: invalid write: %08x", (uint32_t)addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps sifive_clint_ops = {
|
|
|
|
.read = sifive_clint_read,
|
|
|
|
.write = sifive_clint_write,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
2020-06-30 23:12:11 +03:00
|
|
|
.max_access_size = 8
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static Property sifive_clint_properties[] = {
|
2020-05-14 13:21:31 +03:00
|
|
|
DEFINE_PROP_UINT32("hartid-base", SiFiveCLINTState, hartid_base, 0),
|
2018-03-02 15:31:12 +03:00
|
|
|
DEFINE_PROP_UINT32("num-harts", SiFiveCLINTState, num_harts, 0),
|
|
|
|
DEFINE_PROP_UINT32("sip-base", SiFiveCLINTState, sip_base, 0),
|
|
|
|
DEFINE_PROP_UINT32("timecmp-base", SiFiveCLINTState, timecmp_base, 0),
|
|
|
|
DEFINE_PROP_UINT32("time-base", SiFiveCLINTState, time_base, 0),
|
|
|
|
DEFINE_PROP_UINT32("aperture-size", SiFiveCLINTState, aperture_size, 0),
|
2020-09-01 04:39:10 +03:00
|
|
|
DEFINE_PROP_UINT32("timebase-freq", SiFiveCLINTState, timebase_freq, 0),
|
2018-03-02 15:31:12 +03:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void sifive_clint_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
SiFiveCLINTState *s = SIFIVE_CLINT(dev);
|
|
|
|
memory_region_init_io(&s->mmio, OBJECT(dev), &sifive_clint_ops, s,
|
|
|
|
TYPE_SIFIVE_CLINT, s->aperture_size);
|
|
|
|
sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio);
|
2021-08-30 08:34:36 +03:00
|
|
|
|
|
|
|
s->timer_irqs = g_malloc(sizeof(qemu_irq) * s->num_harts);
|
|
|
|
qdev_init_gpio_out(dev, s->timer_irqs, s->num_harts);
|
|
|
|
|
|
|
|
s->soft_irqs = g_malloc(sizeof(qemu_irq) * s->num_harts);
|
|
|
|
qdev_init_gpio_out(dev, s->soft_irqs, s->num_harts);
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sifive_clint_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
dc->realize = sifive_clint_realize;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, sifive_clint_properties);
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo sifive_clint_info = {
|
|
|
|
.name = TYPE_SIFIVE_CLINT,
|
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(SiFiveCLINTState),
|
|
|
|
.class_init = sifive_clint_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void sifive_clint_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&sifive_clint_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(sifive_clint_register_types)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create CLINT device.
|
|
|
|
*/
|
2020-05-14 13:21:31 +03:00
|
|
|
DeviceState *sifive_clint_create(hwaddr addr, hwaddr size,
|
|
|
|
uint32_t hartid_base, uint32_t num_harts, uint32_t sip_base,
|
2020-09-01 04:39:10 +03:00
|
|
|
uint32_t timecmp_base, uint32_t time_base, uint32_t timebase_freq,
|
|
|
|
bool provide_rdtime)
|
2018-03-02 15:31:12 +03:00
|
|
|
{
|
|
|
|
int i;
|
2021-08-30 08:34:36 +03:00
|
|
|
|
|
|
|
DeviceState *dev = qdev_new(TYPE_SIFIVE_CLINT);
|
|
|
|
qdev_prop_set_uint32(dev, "hartid-base", hartid_base);
|
|
|
|
qdev_prop_set_uint32(dev, "num-harts", num_harts);
|
|
|
|
qdev_prop_set_uint32(dev, "sip-base", sip_base);
|
|
|
|
qdev_prop_set_uint32(dev, "timecmp-base", timecmp_base);
|
|
|
|
qdev_prop_set_uint32(dev, "time-base", time_base);
|
|
|
|
qdev_prop_set_uint32(dev, "aperture-size", size);
|
|
|
|
qdev_prop_set_uint32(dev, "timebase-freq", timebase_freq);
|
|
|
|
sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
|
|
|
|
sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
|
|
|
|
|
2018-03-02 15:31:12 +03:00
|
|
|
for (i = 0; i < num_harts; i++) {
|
2020-05-14 13:21:31 +03:00
|
|
|
CPUState *cpu = qemu_get_cpu(hartid_base + i);
|
2021-08-30 08:34:36 +03:00
|
|
|
RISCVCPU *rvcpu = RISCV_CPU(cpu);
|
2018-03-02 15:31:12 +03:00
|
|
|
CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
|
2021-08-30 08:34:36 +03:00
|
|
|
sifive_clint_callback *cb = g_malloc0(sizeof(sifive_clint_callback));
|
|
|
|
|
2018-03-02 15:31:12 +03:00
|
|
|
if (!env) {
|
2021-08-30 08:34:36 +03:00
|
|
|
g_free(cb);
|
2018-03-02 15:31:12 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-02 16:42:17 +03:00
|
|
|
if (provide_rdtime) {
|
2020-09-01 04:39:10 +03:00
|
|
|
riscv_cpu_set_rdtime_fn(env, cpu_riscv_read_rtc, timebase_freq);
|
2020-02-02 16:42:17 +03:00
|
|
|
}
|
2021-08-30 08:34:36 +03:00
|
|
|
|
|
|
|
cb->s = SIFIVE_CLINT(dev);
|
|
|
|
cb->num = i;
|
2018-03-02 15:31:12 +03:00
|
|
|
env->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
|
2021-08-30 08:34:36 +03:00
|
|
|
&sifive_clint_timer_cb, cb);
|
2018-03-02 15:31:12 +03:00
|
|
|
env->timecmp = 0;
|
2021-08-30 08:34:36 +03:00
|
|
|
|
|
|
|
qdev_connect_gpio_out(dev, i,
|
|
|
|
qdev_get_gpio_in(DEVICE(rvcpu), IRQ_M_TIMER));
|
|
|
|
qdev_connect_gpio_out(dev, num_harts + i,
|
|
|
|
qdev_get_gpio_in(DEVICE(rvcpu), IRQ_M_SOFT));
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|