Skip to contentSkip to conversation

hw/riscv: add K230 SRAM device model

RFC PATCH In review Jian Cai 2 participants 5 messages Jul 20 – Jul 21 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Jian Cai

author
#1 lore raw

RFC PATCH 1/3 hw/riscv: add K230 SRAM device model

The K230 shared SRAM (2 MB at 0x80200000) has no software-visible controller registers: it is a pure on-chip RAM block accessed via the AXI bus. This patch wraps it as a SysBusDevice so that it appears in the QOM tree, supports VMState migration, and can be introspected by management tools.

Clock gating (CMU at 0x91100000, offset 0x5c) and reset control (RMU at 0x91101000, offsets 0x60/0x64) are handled by separate system-controller peripherals and are not modelled here.

Reference:
K230 TRM V0.3.1 (2024-11-18), Section 5.2 Sram

---
 MAINTAINERS                  |  5 +++
 hw/riscv/k230_sram.c         | 81 ++++++++++++++++++++++++++++++++++++
 include/hw/riscv/k230_sram.h | 35 ++++++++++++++++
 3 files changed, 121 insertions(+)
 create mode 100644 hw/riscv/k230_sram.c
 create mode 100644 include/hw/riscv/k230_sram.h
MAINTAINERS +5 -0
index 6171cc7494..3567563ba6 100644
@@ -1825,6 +1825,11 @@ L: [email protected]
 S: Maintained
 F: docs/system/riscv/k230.rst
 F: hw/riscv/k230.c
+F: hw/riscv/k230_sram.c
+F: include/hw/riscv/k230_sram.h
+F: hw/riscv/k230_sram.c
+F: include/hw/riscv/k230_sram.h
+F: tests/qtest/k230-sram-test.c
 F: hw/watchdog/k230_wdt.c
 F: include/hw/riscv/k230.h
 F: include/hw/watchdog/k230_wdt.h
hw/riscv/k230_sram.c new file +81 -0

81 lines not on this page · raw message

include/hw/riscv/k230_sram.h new file +35 -0
new file mode 100644
index 0000000000..6d15970ab9
@@ -0,0 +1,35 @@
+/*
+ * K230 SRAM Controller
+ *
+ * K230 Technical Reference Manual V0.3.1 (2024-11-18):
+ * https://github.com/revyos/external-docs/blob/master/K230/en-us/K230_Technical_Reference_Manual_V0.3.1_20241118.pdf
+ *
+ * The K230 shared SRAM (2 MB at 0x80200000) has no software-visible
+ * controller registers: it is a pure on-chip RAM block accessed directly
+ * via the AXI bus.  Clock gating (CMU at 0x91100000, offset 0x5c) and
+ * reset control (RMU at 0x91101000, offsets 0x60/0x64) are handled by
+ * separate system-controller peripherals and are not modelled here yet.
+ *
+ * Copyright (c) 2026 Jian Cai <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef K230_SRAM_H
+#define K230_SRAM_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_K230_SRAM "riscv.k230.sram"
+OBJECT_DECLARE_SIMPLE_TYPE(K230SramState, K230_SRAM)
+
+struct K230SramState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    MemoryRegion sram;          /* 2 MB SRAM storage */
+};
+
+#endif /* K230_SRAM_H */
Signature · 1 line
2.43.0
#2 lore raw

Hi Jian,

Quoted 6 lines · Jian Cai

The K230 shared SRAM (2 MB at 0x80200000) has no software-visible controller registers: it is a pure on-chip RAM block accessed via the AXI bus. This patch wraps it as a SysBusDevice so that it appears in the QOM tree, supports VMState migration, and can be introspected by management tools.

Quoted 6 lines

Clock gating (CMU at 0x91100000, offset 0x5c) and reset control (RMU at 0x91101000, offsets 0x60/0x64) are handled by separate system-controller peripherals and are not modelled here.

Reference:
K230 TRM V0.3.1 (2024-11-18), Section 5.2 Sram

Thanks for the contribution!

But I didn't receive the cover letter for this patch set. Please check if you prepared one, or if it might have been lost during transmission.

Could you please include a cover letter when you send the next revision of this series? It should briefly explain what the series does as a whole and what problem it is intended to solve.

Here are two ways to generate one:

1. Store the cover letter in the branch description:

   git branch --edit-description
   git format-patch --cover-letter --cover-from-description=subject \
       -o outgoing

2. Generate a cover letter template and edit it manually:

   git format-patch --cover-letter -o k230-patches
   $EDITOR k230-patches/0000-cover-letter.patch

In general, a single-patch submission does not need a separate cover letter. For a multi-patch series like this one, however, we prefer a cover letter that provides an overview of the whole series.

Quoted 43 lines
---
 MAINTAINERS                  |  5 +++
 hw/riscv/k230_sram.c         | 81 ++++++++++++++++++++++++++++++++++++
 include/hw/riscv/k230_sram.h | 35 ++++++++++++++++
 3 files changed, 121 insertions(+)
 create mode 100644 hw/riscv/k230_sram.c
 create mode 100644 include/hw/riscv/k230_sram.h
MAINTAINERS +5 -0
index 6171cc7494..3567563ba6 100644
@@ -1825,6 +1825,11 @@ L: [email protected]
 S: Maintained
 F: docs/system/riscv/k230.rst
 F: hw/riscv/k230.c
+F: hw/riscv/k230_sram.c
+F: include/hw/riscv/k230_sram.h
+F: hw/riscv/k230_sram.c
+F: include/hw/riscv/k230_sram.h
+F: tests/qtest/k230-sram-test.c
 F: hw/watchdog/k230_wdt.c
 F: include/hw/riscv/k230.h
 F: include/hw/watchdog/k230_wdt.h
hw/riscv/k230_sram.c new file +11 -0
new file mode 100644
index 0000000000..1d960e733e
@@ -0,0 +1,81 @@
+/*
+ * K230 SRAM Controller
+ *
+ * K230 Technical Reference Manual V0.3.1 (2024-11-18):
+ * https://github.com/revyos/external-docs/blob/master/K230/en-us/K230_Technical_Reference_Manual_V0.3.1_20241118.pdf
+ *
+ * The K230 shared SRAM (2 MB at 0x80200000) has no software-visible
+ * controller registers.  This device wraps the SRAM as a SysBusDevice
+ * so that it appears in the QOM tree, supports migration (VMState),
+ * and can be introspected by management tools.
+ *

hw/riscv/k230_sram.c line 21

Quoted 10 lines
hw/riscv/k230_sram.c new file +10 -0
+ * Clock gating is controlled by the CMU at 0x91100000 (shrm_CLK_CFG,
+ * offset 0x5c, bit 10: sram_aclk_enable).  Reset is controlled by the
+ * RMU at 0x91101000 (SRAM_RST_TIM/SRAM_RST_CTL, offsets 0x60/0x64).
+ * Those peripherals are not modelled yet, so SRAM is always enabled
+ * in the current QEMU implementation.
+ *
+ * Copyright (c) 2026 Jian Cai <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */

We probably don't need to create a dedicated device model for SRAM. It can actually be replaced directly by using a QEMU memory region RAM.

A simpler approach would be to keep this code in k230.c, perhaps in a helper function such as k230_sram_create().

Thanks,
Chao

Quoted 104 lines
hw/riscv/k230_sram.c new file +60 -0
+
+#include "qemu/osdep.h"
+#include "qemu/units.h"
+#include "hw/core/sysbus.h"
+#include "migration/vmstate.h"
+#include "qapi/error.h"
+#include "hw/riscv/k230_sram.h"
+
+static void k230_sram_realize(DeviceState *dev, Error **errp)
+{
+    K230SramState *s = K230_SRAM(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+    memory_region_init_ram(&s->sram, OBJECT(dev), "k230.sram",
+                           2 * MiB, &error_fatal);
+    sysbus_init_mmio(sbd, &s->sram);
+}
+
+static void k230_sram_reset_hold(Object *obj, ResetType type)
+{
+    /*
+     * No software-visible registers to reset.  SRAM content is preserved
+     * across warm reset on real hardware; a cold reset would clear it,
+     * but QEMU memory_region_init_ram already zeroes the region on init.
+     */
+}
+
+static const VMStateDescription vmstate_k230_sram = {
+    .name = "k230.sram",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (const VMStateField[]) {
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static void k230_sram_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+
+    dc->realize = k230_sram_realize;
+    rc->phases.hold = k230_sram_reset_hold;
+    dc->vmsd = &vmstate_k230_sram;
+    dc->desc = "K230 SRAM";
+}
+
+static const TypeInfo k230_sram_info = {
+    .name          = TYPE_K230_SRAM,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(K230SramState),
+    .class_init    = k230_sram_class_init,
+};
+
+static void k230_sram_register_type(void)
+{
+    type_register_static(&k230_sram_info);
+}
+
+type_init(k230_sram_register_type)
include/hw/riscv/k230_sram.h new file +35 -0
new file mode 100644
index 0000000000..6d15970ab9
@@ -0,0 +1,35 @@
+/*
+ * K230 SRAM Controller
+ *
+ * K230 Technical Reference Manual V0.3.1 (2024-11-18):
+ * https://github.com/revyos/external-docs/blob/master/K230/en-us/K230_Technical_Reference_Manual_V0.3.1_20241118.pdf
+ *
+ * The K230 shared SRAM (2 MB at 0x80200000) has no software-visible
+ * controller registers: it is a pure on-chip RAM block accessed directly
+ * via the AXI bus.  Clock gating (CMU at 0x91100000, offset 0x5c) and
+ * reset control (RMU at 0x91101000, offsets 0x60/0x64) are handled by
+ * separate system-controller peripherals and are not modelled here yet.
+ *
+ * Copyright (c) 2026 Jian Cai <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef K230_SRAM_H
+#define K230_SRAM_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_K230_SRAM "riscv.k230.sram"
+OBJECT_DECLARE_SIMPLE_TYPE(K230SramState, K230_SRAM)
+
+struct K230SramState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    MemoryRegion sram;          /* 2 MB SRAM storage */
+};
+
+#endif /* K230_SRAM_H */
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v2 1/1 hw/riscv/k230: add k230_sram_create() helper function

In reply to Jian Cai

The K230 shared SRAM (2 MB at 0x80200000) has no software-visible controller registers: it is a pure on-chip RAM block accessed via the AXI bus. Replace the inline memory_region_init_ram calls in k230_soc_realize() with a static k230_sram_create() helper, which follows the same pattern as the existing k230_create_plic() and k230_create_uart() helpers.

Also fix two non-ASCII characters (registered sign and em-dash) in k230.rst that would cause git send-email to reject the patch as 8-bit.

Reference:
K230 TRM V0.3.1 (2024-11-18), Section 5.2 Sram
https://github.com/revyos/external-docs/blob/master/K230/en-us/
K230_Technical_Reference_Manual_V0.3.1_20241118.pdf

---

Changes in v2:
- Dropped SysBusDevice model per Chao Liu's review; use a static helper function k230_sram_create() in k230.c instead.
- Removed k230_sram.c, k230_sram.h, and k230-sram-test.c.
- Fixed two non-ASCII characters (registered sign, em-dash) in k230.rst.
- Removed SRAM entries from MAINTAINERS (no new files added).
- v1: [RFC PATCH 0/3] hw/riscv: add K230 SRAM device model
https://lore.kernel.org/qemu-devel/

 docs/system/riscv/k230.rst |  3 ++-
 hw/riscv/k230.c            | 14 ++++++++++----
 2 files changed, 12 insertions(+), 5 deletions(-)
docs/system/riscv/k230.rst +2 -1
index cea8202e55..18eb6806f6 100644
@@ -2,7 +2,7 @@ Kendryte K230 virt reference platform (``k230``)
 ==========================================================================
 The ``k230`` machine is compatible with the Kendryte K230 SDK.
 
-The K230 is a chip from the AIoT SoC series made by Kendryte ® — a part of
+The K230 is a chip from the AIoT SoC series made by Kendryte (R) -- a part of
 Canaan Inc. It uses a brand-new multi-heterogeneous unit accelerated computing
 structure.
 
@@ -111,3 +111,4 @@ this machine does not emulate those controllers yet. Replace ``${INITRD_END}``
 with the host-calculated value above when typing the command. ``cma=0`` avoids
 the SDK kernel reserving too much of the little-core memory window for initramfs
 boot.
+
hw/riscv/k230.c +10 -4
index 656f28190c..96add64c2a 100644
@@ -14,6 +14,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/units.h"
 #include "cpu-qom.h"
 #include "qemu/cutils.h"
 #include "qemu/error-report.h"
@@ -151,6 +152,14 @@ static void k230_create_uart(MemoryRegion *sys_mem, DeviceState *plic,
                    399193, serial_hd(index), DEVICE_LITTLE_ENDIAN);
 }
 
+static void k230_sram_create(K230SoCState *s)
+{
+    memory_region_init_ram(&s->sram, OBJECT(s), "k230.sram",
+                           memmap[K230_DEV_SRAM].size, &error_fatal);
+    memory_region_add_subregion(get_system_memory(),
+                                memmap[K230_DEV_SRAM].base, &s->sram);
+}
+
 static void k230_soc_realize(DeviceState *dev, Error **errp)
 {
     K230SoCState *s = RISCV_K230_SOC(dev);
@@ -162,10 +171,7 @@ static void k230_soc_realize(DeviceState *dev, Error **errp)
     c908_cpus = s->c908_cpu.num_harts;
 
     /* SRAM */
-    memory_region_init_ram(&s->sram, OBJECT(dev), "sram",
-                           memmap[K230_DEV_SRAM].size, &error_fatal);
-    memory_region_add_subregion(sys_mem, memmap[K230_DEV_SRAM].base,
-                                &s->sram);
+    k230_sram_create(s);
 
     /* BootROM */
     memory_region_init_rom(&s->bootrom, OBJECT(dev), "bootrom",
Signature · 1 line
2.43.0
author
#4 lore raw

RFC PATCH 2/3 hw/riscv/k230: integrate SRAM device into SoC

In reply to a message not in the archive

Replace the bare memory_region_init_ram() in k230_soc_realize() with a proper SysBusDevice. Wire up object_initialize_child() in k230_soc_init(), call sysbus_realize() and sysbus_mmio_map() in k230_soc_realize(), and add the Kconfig + meson.build entries.

Update docs/system/riscv/k230.rst.

---
 docs/system/riscv/k230.rst | 8 ++++++++
 hw/riscv/Kconfig           | 4 ++++
 hw/riscv/k230.c            | 9 +++++----
 hw/riscv/meson.build       | 1 +
 include/hw/riscv/k230.h    | 3 ++-
 5 files changed, 20 insertions(+), 5 deletions(-)
docs/system/riscv/k230.rst +8 -0
index cea8202e55..a7668fa065 100644
@@ -111,3 +111,11 @@ this machine does not emulate those controllers yet. Replace ``${INITRD_END}``
 with the host-calculated value above when typing the command. ``cma=0`` avoids
 the SDK kernel reserving too much of the little-core memory window for initramfs
 boot.
+
+SRAM
+~~~~
+
+The K230 shared SRAM is a 2 MB on-chip RAM block at ``0x80200000``.  It has no
+software-visible controller registers -- the region is accessed directly via the
+AXI bus.  Clock gating and reset control are handled by separate
+system-controller peripherals (CMU and RMU).
hw/riscv/Kconfig +4 -0
index de37c08cae..ee48ea530d 100644
@@ -142,6 +142,9 @@ config XIANGSHAN_KUNMINGHU
     select RISCV_IMSIC
     select SERIAL_MM
 
+config K230_SRAM
+    bool
+
 config MIPS_BOSTON_AIA
     bool
     default y
@@ -161,4 +164,5 @@ config K230
     select RISCV_IMSIC
     select SERIAL_MM
     select UNIMP
+    select K230_SRAM
     select K230_WDT
hw/riscv/k230.c +5 -4
index 656f28190c..3469a9d43b 100644
@@ -110,6 +110,7 @@ static void k230_soc_init(Object *obj)
     object_initialize_child(obj, "c908-cpu", cpu0, TYPE_RISCV_HART_ARRAY);
     object_initialize_child(obj, "k230-wdt0", &s->wdt[0], TYPE_K230_WDT);
     object_initialize_child(obj, "k230-wdt1", &s->wdt[1], TYPE_K230_WDT);
+    object_initialize_child(obj, "k230-sram", &s->sram, TYPE_K230_SRAM);
 
     qdev_prop_set_uint32(DEVICE(cpu0), "hartid-base", 0);
     qdev_prop_set_string(DEVICE(cpu0), "cpu-type", TYPE_RISCV_CPU_THEAD_C908);
@@ -162,10 +163,10 @@ static void k230_soc_realize(DeviceState *dev, Error **errp)
     c908_cpus = s->c908_cpu.num_harts;
 
     /* SRAM */
-    memory_region_init_ram(&s->sram, OBJECT(dev), "sram",
-                           memmap[K230_DEV_SRAM].size, &error_fatal);
-    memory_region_add_subregion(sys_mem, memmap[K230_DEV_SRAM].base,
-                                &s->sram);
+    if (!sysbus_realize(SYS_BUS_DEVICE(&s->sram), errp)) {
+        return;
+    }
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->sram), 0, memmap[K230_DEV_SRAM].base);
 
     /* BootROM */
     memory_region_init_rom(&s->bootrom, OBJECT(dev), "bootrom",
hw/riscv/meson.build +1 -0
index 0d82ceacc4..80b7455141 100644
@@ -20,5 +20,6 @@ riscv_ss.add(when: 'CONFIG_XIANGSHAN_KUNMINGHU', if_true: files('xiangshan_kmh.c
 riscv_ss.add(when: 'CONFIG_RISCV_MIPS_CPS', if_true: files('cps.c'))
 riscv_ss.add(when: 'CONFIG_MIPS_BOSTON_AIA', if_true: files('boston-aia.c'))
 riscv_ss.add(when: 'CONFIG_K230', if_true: files('k230.c'))
+riscv_ss.add(when: 'CONFIG_K230_SRAM', if_true: files('k230_sram.c'))
 
 hw_arch += {'riscv': riscv_ss}
include/hw/riscv/k230.h +2 -1
index 592e1c26bf..9f7baa513c 100644
@@ -18,6 +18,7 @@
 #include "hw/core/boards.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/watchdog/k230_wdt.h"
+#include "hw/riscv/k230_sram.h"
 
 #define C908_CPU_HARTID   (0)
 
@@ -33,7 +34,7 @@ typedef struct K230SoCState {
     RISCVHartArrayState c908_cpu; /* Small core */
 
     K230WdtState wdt[2];
-    MemoryRegion sram;
+    K230SramState sram;
     MemoryRegion bootrom;
 
     DeviceState *c908_plic;
Signature · 1 line
2.43.0
author
#5 lore raw

RFC PATCH 3/3 tests/qtest: add K230 SRAM qtest

In reply to a message not in the archive

Add qtest coverage for the K230 SRAM device model. Six test cases verify the MMIO region:

- address_mapped: write/read at SRAM base address
- read_write_consistency: 4-offset write-read verification
- byte_access: byte-level R/W and 32-bit LE composition
- initial_zeroed: SRAM is zero-initialized on startup
- unmapped_beyond_range: out-of-range access does not crash QEMU
- write_and_verify_range: 64 KiB step full-range verification

---
 MAINTAINERS                  |   2 +
 tests/qtest/k230-sram-test.c | 161 +++++++++++++++++++++++++++++++++++
 tests/qtest/meson.build      |   3 +-
 3 files changed, 165 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/k230-sram-test.c
MAINTAINERS +2 -0
index 3567563ba6..eebe8b3def 100644
@@ -1827,9 +1827,11 @@ F: docs/system/riscv/k230.rst
 F: hw/riscv/k230.c
 F: hw/riscv/k230_sram.c
 F: include/hw/riscv/k230_sram.h
+F: tests/qtest/k230-sram-test.c
 F: hw/riscv/k230_sram.c
 F: include/hw/riscv/k230_sram.h
 F: tests/qtest/k230-sram-test.c
+F: tests/qtest/k230-sram-test.c
 F: hw/watchdog/k230_wdt.c
 F: include/hw/riscv/k230.h
 F: include/hw/watchdog/k230_wdt.h
tests/qtest/k230-sram-test.c new file +161 -0

161 lines not on this page · raw message

tests/qtest/meson.build +2 -1
index 56ff860e21..cf64539acb 100644
@@ -297,7 +297,8 @@ qtests_riscv64 = ['riscv-csr-test'] + \
   (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
    config_all_devices.has_key('CONFIG_RISCV_IOMMU') ?
    ['iommu-riscv-test'] : []) + \
-  (config_all_devices.has_key('CONFIG_K230') ? ['k230-wdt-test'] : [])
+  (config_all_devices.has_key('CONFIG_K230') ? ['k230-wdt-test',
+   'k230-sram-test'] : [])
 
 qtests_hexagon = ['boot-serial-test']
 
Signature · 1 line
2.43.0