Skip to content

Commit 1e79fc2

Browse files
committed
[ot] hw/opentitan: ot_earlgrey: rework machine reset for QEMU v9.2
Signed-off-by: Emmanuel Blot <eblot@rivosinc.com>
1 parent 22dfdf8 commit 1e79fc2

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

hw/riscv/ot_earlgrey.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "hw/ssi/ssi.h"
7474
#include "sysemu/blockdev.h"
7575
#include "sysemu/hw_accel.h"
76+
#include "sysemu/reset.h"
7677
#include "sysemu/sysemu.h"
7778

7879
/* ------------------------------------------------------------------------ */
@@ -1158,10 +1159,17 @@ struct OtEGBoardState {
11581159
struct OtEGMachineState {
11591160
MachineState parent_obj;
11601161

1162+
ResettableState reset;
1163+
11611164
bool no_epmp_cfg;
11621165
bool ignore_elf_entry;
11631166
};
11641167

1168+
struct OtEGMachineClass {
1169+
MachineClass parent_class;
1170+
ResettablePhases parent_phases;
1171+
};
1172+
11651173
/* ------------------------------------------------------------------------ */
11661174
/* Device Configuration */
11671175
/* ------------------------------------------------------------------------ */
@@ -1562,6 +1570,37 @@ ot_eg_machine_set_ignore_elf_entry(Object *obj, bool value, Error **errp)
15621570
s->ignore_elf_entry = value;
15631571
}
15641572

1573+
static ResettableState *ot_eg_get_reset_state(Object *obj)
1574+
{
1575+
OtEGMachineState *s = RISCV_OT_EG_MACHINE(obj);
1576+
1577+
return &s->reset;
1578+
}
1579+
1580+
static void ot_eg_reset_hold(Object *obj, ResetType type)
1581+
{
1582+
(void)obj;
1583+
1584+
/*
1585+
* The way the resettable APIs are implemented does not allow to call the
1586+
* legacy qemu_devices_reset from the enter phase, where a global static
1587+
* variable singleton enforces that entering reset is exclusive. However
1588+
* qemu_devices_reset implements the full enter/hold/exit reset sequence.
1589+
* This legacy function is therefore invoked from the hold stage of the
1590+
* machine reset sequence.
1591+
*/
1592+
qemu_devices_reset(type);
1593+
}
1594+
1595+
static void ot_eg_machine_reset(MachineState *ms, ResetType reason)
1596+
{
1597+
OtEGMachineState *s = RISCV_OT_EG_MACHINE(ms);
1598+
1599+
g_assert(reason == RESET_TYPE_COLD);
1600+
1601+
resettable_reset(OBJECT(s), reason);
1602+
}
1603+
15651604
static void ot_eg_machine_instance_init(Object *obj)
15661605
{
15671606
OtEGMachineState *s = RISCV_OT_EG_MACHINE(obj);
@@ -1583,6 +1622,13 @@ static void ot_eg_machine_init(MachineState *state)
15831622
DeviceState *dev = qdev_new(TYPE_RISCV_OT_EG_BOARD);
15841623

15851624
object_property_add_child(OBJECT(state), "board", OBJECT(dev));
1625+
1626+
/*
1627+
* any object not part of the default system bus hiearchy is never reset
1628+
* otherwise
1629+
*/
1630+
qemu_register_reset(resettable_cold_reset_fn, dev);
1631+
15861632
qdev_realize(dev, NULL, &error_fatal);
15871633
}
15881634

@@ -1593,20 +1639,35 @@ static void ot_eg_machine_class_init(ObjectClass *oc, void *data)
15931639

15941640
mc->desc = "RISC-V Board compatible with OpenTitan EarlGrey FPGA platform";
15951641
mc->init = ot_eg_machine_init;
1642+
mc->reset = &ot_eg_machine_reset;
15961643
mc->max_cpus = 1u;
15971644
mc->default_cpu_type = ot_eg_soc_devices[OT_EG_SOC_DEV_HART].type;
15981645
const IbexDeviceDef *sram =
15991646
&ot_eg_soc_devices[OT_EG_SOC_DEV_SRAM_MAIN_CTRL];
16001647
mc->default_ram_id = sram->type;
16011648
mc->default_ram_size = SRAM_MAIN_SIZE;
1649+
/*
1650+
* Implement the resettable interface to ensure the proper initialization
1651+
* sequence.
1652+
* The hold stage is used to perform most of the device reset sequence.
1653+
*/
1654+
ResettableClass *rc = RESETTABLE_CLASS(oc);
1655+
1656+
rc->get_state = &ot_eg_get_reset_state;
1657+
1658+
OtEGMachineClass *sc = RISCV_OT_EG_MACHINE_CLASS(oc);
1659+
resettable_class_set_parent_phases(rc, NULL, &ot_eg_reset_hold, NULL,
1660+
&sc->parent_phases);
16021661
}
16031662

16041663
static const TypeInfo ot_eg_machine_type_info = {
16051664
.name = TYPE_RISCV_OT_EG_MACHINE,
16061665
.parent = TYPE_MACHINE,
16071666
.instance_size = sizeof(OtEGMachineState),
16081667
.instance_init = &ot_eg_machine_instance_init,
1668+
.class_size = sizeof(OtEGMachineClass),
16091669
.class_init = &ot_eg_machine_class_init,
1670+
.interfaces = (InterfaceInfo[]){ { TYPE_RESETTABLE_INTERFACE }, {} },
16101671
};
16111672

16121673
static void ot_eg_machine_register_types(void)

include/hw/riscv/ot_earlgrey.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* QEMU RISC-V Board Compatible with OpenTitan EarlGrey FPGA platform
33
*
44
* Copyright (c) 2022-2024 Rivos, Inc.
5+
* Copyright (c) 2024-2025 lowRISC contributors.
56
*
67
* Author(s):
78
* Loïc Lefort <loic@rivosinc.com>
@@ -27,7 +28,7 @@
2728
#define OT_EARLGREY "ot-earlgrey"
2829

2930
#define TYPE_RISCV_OT_EG_MACHINE MACHINE_TYPE_NAME(OT_EARLGREY)
30-
OBJECT_DECLARE_SIMPLE_TYPE(OtEGMachineState, RISCV_OT_EG_MACHINE)
31+
OBJECT_DECLARE_TYPE(OtEGMachineState, OtEGMachineClass, RISCV_OT_EG_MACHINE)
3132

3233
#define TYPE_RISCV_OT_EG_BOARD OT_EARLGREY "-board"
3334
OBJECT_DECLARE_SIMPLE_TYPE(OtEGBoardState, RISCV_OT_EG_BOARD)

0 commit comments

Comments
 (0)