From nobody Mon Nov 23 04:26:39 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1793369430; cv=none; d=zohomail.com; s=zohoarc; b=TDI20Gt0uHbF2JC4bCUajb78b2nTbGGEIg+ElQaNCqk5nwvAmxys0ZNk6Xdslz2hKkajB+VJFNppZjcE1npI8GFwzMXY76eFOO7L60Dm5PPTyfSpcI+rnxsmczjPbs3v9O3BFz+n+l4gqh0/zsf1Ib1x0FPW7FEdq22NljkXiqw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793369430; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=vSax/7NYgw14hOC86mZw2+h8awfxbApPP/+c4jyPbE0=; b=TdMR0OUXqecCmWSiVs9ZvmbmdvfG8hM9emiN9XDZhIC/vNLMgwSF1VvxMTSy6aRd7YqkCj65oEkd/KTWiKafqg6CE611E91X3owDK8vtyfVO0r+a0J2u+r5ebDAnxNa4mmj+FW0Ju64fk8Q9RGXtpj3Ul+IUCLngMQ4Fc1/Jn3o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793369430750807.7600777975459; Fri, 30 Oct 2026 07:10:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMnJ0-0004ro-SF; Fri, 30 Oct 2026 10:10:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMmoG-000600-FD for qemu-devel@nongnu.org; Fri, 30 Oct 2026 09:38:24 -0400 Received: from mail-oo1-xc2a.google.com ([2607:f8b0:4864:20::c2a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xMmoE-0007aE-2z for qemu-devel@nongnu.org; Fri, 30 Oct 2026 09:38:20 -0400 Received: by mail-oo1-xc2a.google.com with SMTP id 006d021491bc7-705cb6cedd5so1267522eaf.1 for ; Fri, 30 Oct 2026 06:38:17 -0700 (PDT) Received: from DESKTOP-DFGP8OG.lan ([120.235.190.244]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-714f30600f0sm1861421eaf.12.2026.10.30.06.38.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Oct 2026 06:38:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793367497; x=1793972297; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=vSax/7NYgw14hOC86mZw2+h8awfxbApPP/+c4jyPbE0=; b=uoyCOtXYiSz8VJqHmTvA6SDp0GHrj6p184vjQrOWdP3gEK0/PgGHo9RxF4tC5E7pJQ 7BEsjbn/YHRzcios1krpkqWcmg343CIl/WcVW1cwX34yQcty6qBTKE60If7soM0C9TZC RpAITSHuP017l81TTn4Rxj/BXrkVu0a0FIko0JoUV5GuR8IXJdNY/hI04N4bek/2xwgM kviQLIOL1TWjFiQrdL+oLN74fkfcjJLynr2yh8Zl//W5DI+uOW7/63aO7Kio7E2Wlz+8 wnLkrby/2kjva80GQeo7UR3fOMRj/NJlB08L7HPWJtxJge/JzDSn47Y52tYx5HL9yS4W eShA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793367497; x=1793972297; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=vSax/7NYgw14hOC86mZw2+h8awfxbApPP/+c4jyPbE0=; b=t7VJ9SJYk2bOZ3Uo9k20RCy4ZMk1S+9NSGGvki6E1ItG6GNG3iApvD7epHmSZn4A+s ziBs5LJxVv2fCLSzh3WXtFAsA5ssCzQlzEOcBR4R8yfUHYrt5G7QghbzAWdw9obH+hMw uIfJrqkW5xhAapNiNLkuSAzj+qksy5zqUXLBApy/+7E/a+29bSj/Ts0nlqAAb0yczwxZ /vOh/DRe3dFEaK8KPFgyly8BZUOfvVuLvxwGL559GG75pO4GFaMX40LaO+vjCDSmNZpi v77h6vB8nmdp9iiwthWFeWwxQLhTCQPrZAhKuiCpG+g8GG7TibRxUyRWejQTVc6iwBqF QUFQ== X-Gm-Message-State: AFq9FYLtrNNNuHhICsm70uAgQ1FbcQ1TJSaJ2pplreZpxnasFG4GGAhW fF+NOoy8pT4cT11mY3WZUkaxqS6A1y+nHbDmIkGhi78eJxv9Wfa6HoVDTUB7Mw== X-Gm-Gg: AYBFou3jJt/H63DzGBZJGPgb8MKv0aAyoZLcN7Lg3cFxGXQKnpnIj1bpPsprGvK14kR aGmiIernazUPkhuYeRvmnJ0rAGwyl28Fp8KyhG9g0MnFxmxmlb2EwDQZRpW/c8gC5bG1NVOIokS jBjCCHLLUlHqlh1WCbXw+rbYJqf+mrjV+U+brQnClP0WN79fg/EMsWGdaHhBjUYDtk9N4nMGAGy 1syQf35Ssr07WhxJmbl0u9RnsPvxAFkFc4a5B72aqpRTAJuxQXbDN/0GQHT7obdsx3ACXpoWPog w3nQnC03WV991mpM6UnLxTX+BjOej3mLTu3T/LECGL5T71lcnbvmpOVAH9LKFO+6drEej8NPy+9 rKfoCkwQe3EV6ygZ+7i9GKHxaDckQt0ttAN/EY0eLrOQkt5TKNfIFAj7ACPkcpmJ8PGcS1bTBLM ujY7aROPjENQMlj/VZlBJugJ0mYu5zK7B1DMPL2aoqqoxOf1Q2zH19IXeHGd9Zv51XjM9DINWrX V3BxYC0VyJiVBmjkpUyHpflGa14F+EE0K+FdztDXT2Oi0EELDREn/gUB/7etkL+Sr4VPTpy8gE7 D/P0AVfKWZ2yTyBwjgsjfvhEwxEieiQB/lkFRRcOBg== X-Received: by 2002:a05:6820:571c:20b0:6cd:3fdc:2663 with SMTP id 006d021491bc7-714f2aa6fc1mr674818eaf.36.1793367496767; Fri, 30 Oct 2026 06:38:16 -0700 (PDT) From: raoyi To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, alistair.francis@wdc.com, palmer@dabbelt.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, bmeng.cn@gmail.com, philmd@oss.qualcomm.com, pbonzini@redhat.com, lvivier@redhat.com, farosas@suse.de, caojunze424@gmail.com, raoyi Subject: [PATCH v6 1/3] hw/timer: add DesignWare APB timer model Date: Fri, 30 Oct 2026 21:37:02 +0800 Message-ID: <20261030133704.99678-2-rao232328@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261030133704.99678-1-rao232328@gmail.com> References: <20261030133704.99678-1-rao232328@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::c2a; envelope-from=rao232328@gmail.com; helo=mail-oo1-xc2a.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 30 Oct 2026 10:09:55 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1793369433144158500 Content-Type: text/plain; charset="utf-8" Add generic DesignWare APB timer device model. The number of timer channels is configurable via the num-timers property. Add timer files to MAINTAINERS. Signed-off-by: raoyi --- MAINTAINERS | 2 + hw/timer/Kconfig | 4 + hw/timer/dw-apb-timer.c | 376 ++++++++++++++++++++++++++++++++ hw/timer/meson.build | 1 + hw/timer/trace-events | 8 + include/hw/timer/dw-apb-timer.h | 40 ++++ 6 files changed, 431 insertions(+) create mode 100644 hw/timer/dw-apb-timer.c create mode 100644 include/hw/timer/dw-apb-timer.h diff --git a/MAINTAINERS b/MAINTAINERS index 70e9fed776..60a5664ce5 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1888,11 +1888,13 @@ F: hw/riscv/k230.c F: hw/watchdog/k230_wdt.c F: hw/dma/k230_gsdma.c F: hw/misc/k230_decomp_gzip.c +F: hw/timer/dw-apb-timer.c F: include/hw/misc/k230_ddr.h F: include/hw/riscv/k230.h F: include/hw/watchdog/k230_wdt.h F: include/hw/dma/k230_gsdma.h F: include/hw/misc/k230_decomp_gzip.h +F: include/hw/timer/dw-apb-timer.h F: tests/functional/riscv64/test_k230.py F: tests/qtest/k230-ddr-test.c F: tests/qtest/k230-wdt-test.c diff --git a/hw/timer/Kconfig b/hw/timer/Kconfig index e098376d40..996f5dfde7 100644 --- a/hw/timer/Kconfig +++ b/hw/timer/Kconfig @@ -78,3 +78,7 @@ config AVR_TIMER16 =20 config HEX_QTIMER bool + +config DW_APB_TIMER + bool + select PTIMER diff --git a/hw/timer/dw-apb-timer.c b/hw/timer/dw-apb-timer.c new file mode 100644 index 0000000000..86e1fac43f --- /dev/null +++ b/hw/timer/dw-apb-timer.c @@ -0,0 +1,376 @@ +/* + * Synopsys DesignWare APB timer + * + * Copyright (c) 2026 raoyi + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/bitops.h" +#include "qemu/module.h" +#include "qapi/error.h" +#include "migration/vmstate.h" +#include "hw/core/ptimer.h" +#include "hw/core/qdev-clock.h" +#include "hw/core/sysbus.h" +#include "hw/core/qdev-properties.h" +#include "hw/timer/dw-apb-timer.h" +#include "trace.h" + +#define DW_APB_TIMER_STRIDE 0x14 +#define DW_APB_TIMER_MMIO_SIZE 0x100 + +/* Per-timer register offsets */ +#define DW_APB_TIMER_N_LOAD_COUNT 0x00 +#define DW_APB_TIMER_N_CURRENT_VALUE 0x04 +#define DW_APB_TIMER_N_CONTROL 0x08 +#define DW_APB_TIMER_N_EOI 0x0c +#define DW_APB_TIMER_N_INT_STATUS 0x10 +/* Global register offsets */ +#define DW_APB_TIMER_INT_STATUS 0xa0 +#define DW_APB_TIMER_EOI 0xa4 +#define DW_APB_TIMER_RAW_INT_STATUS 0xa8 +#define DW_APB_TIMER_COMP_VERSION 0xac + +/* Control register bits */ +#define DW_APB_TIMER_CONTROL_ENABLE BIT(0) +/* 1: periodic, 0: free running. */ +#define DW_APB_TIMER_CONTROL_MODE_PERIODIC BIT(1) +#define DW_APB_TIMER_CONTROL_INT_MASK BIT(2) +#define DW_APB_TIMER_CONTROL_RW_MASK 0x7 + +/* + * Component version of the DW_apb_timers IP, fixed in silicon. + * Decodes as the ASCII string "211*" (2.11a series); the model reports + * the version integrated in K230. Linux does not read this register. + */ +#define DW_APB_TIMER_COMP_VERSION_VAL 0x3231312A + +static void dw_apb_timer_update_irq(DWAPBTimerChannel *t) +{ + qemu_set_irq(t->irq, t->int_status && + !(t->control & DW_APB_TIMER_CONTROL_INT_MASK)); +} + +static void dw_apb_timer_clk_update(void *opaque, ClockEvent event) +{ + DWAPBTimerChannel *t =3D opaque; + + if (!t->ptimer) { + return; + } + ptimer_transaction_begin(t->ptimer); + ptimer_set_period_from_clock(t->ptimer, t->clk, 1); + ptimer_transaction_commit(t->ptimer); +} + +static void dw_apb_timer_enable(DWAPBTimerChannel *t) +{ + trace_dw_apb_timer_enable(t->id, t->load); + + ptimer_transaction_begin(t->ptimer); + ptimer_set_limit(t->ptimer, t->load ? t->load : 1, 1); + ptimer_run(t->ptimer, 1); + ptimer_transaction_commit(t->ptimer); +} + +static void dw_apb_timer_disable(DWAPBTimerChannel *t) +{ + trace_dw_apb_timer_disable(t->id); + + ptimer_transaction_begin(t->ptimer); + ptimer_stop(t->ptimer); + ptimer_transaction_commit(t->ptimer); + + t->int_status =3D 0; + dw_apb_timer_update_irq(t); +} + +static void dw_apb_timer_tick(void *opaque) +{ + DWAPBTimerChannel *t =3D opaque; + uint32_t reload; + + trace_dw_apb_timer_tick(t->id); + + t->int_status =3D 1; + dw_apb_timer_update_irq(t); + + if (t->control & DW_APB_TIMER_CONTROL_MODE_PERIODIC) { + reload =3D t->load ? t->load : 1; + } else { + reload =3D UINT32_MAX; + } + + ptimer_set_limit(t->ptimer, reload, 1); + ptimer_run(t->ptimer, 1); +} + +static uint64_t dw_apb_timer_read(void *opaque, hwaddr addr, + unsigned int size) +{ + DWAPBTimerState *s =3D DW_APB_TIMER(opaque); + uint32_t value =3D 0; + + if (addr < DW_APB_TIMER_INT_STATUS) { + unsigned int idx =3D addr / DW_APB_TIMER_STRIDE; + hwaddr reg =3D addr % DW_APB_TIMER_STRIDE; + + if (idx < s->num_timers) { + DWAPBTimerChannel *t =3D &s->timers[idx]; + + switch (reg) { + case DW_APB_TIMER_N_LOAD_COUNT: + value =3D t->load; + break; + case DW_APB_TIMER_N_CURRENT_VALUE: + if (t->control & DW_APB_TIMER_CONTROL_ENABLE) { + value =3D ptimer_get_count(t->ptimer); + } + break; + case DW_APB_TIMER_N_CONTROL: + value =3D t->control; + break; + case DW_APB_TIMER_N_EOI: + t->int_status =3D 0; + dw_apb_timer_update_irq(t); + trace_dw_apb_timer_irq_clear(t->id); + break; + case DW_APB_TIMER_N_INT_STATUS: + value =3D t->int_status && + !(t->control & DW_APB_TIMER_CONTROL_INT_MASK); + break; + default: + break; + } + } + } else { + switch (addr) { + case DW_APB_TIMER_INT_STATUS: + for (int i =3D 0; i < s->num_timers; i++) { + DWAPBTimerChannel *t =3D &s->timers[i]; + + value |=3D (t->int_status && + !(t->control & DW_APB_TIMER_CONTROL_INT_MASK)) <= < i; + } + break; + case DW_APB_TIMER_EOI: + for (int i =3D 0; i < s->num_timers; i++) { + DWAPBTimerChannel *t =3D &s->timers[i]; + + t->int_status =3D 0; + dw_apb_timer_update_irq(t); + } + break; + case DW_APB_TIMER_RAW_INT_STATUS: + for (int i =3D 0; i < s->num_timers; i++) { + value |=3D s->timers[i].int_status << i; + } + break; + case DW_APB_TIMER_COMP_VERSION: + value =3D DW_APB_TIMER_COMP_VERSION_VAL; + break; + default: + break; + } + } + + trace_dw_apb_timer_read(addr, value); + return value; +} + +static void dw_apb_timer_write(void *opaque, hwaddr addr, + uint64_t value, unsigned int size) +{ + DWAPBTimerState *s =3D DW_APB_TIMER(opaque); + + if (addr < DW_APB_TIMER_INT_STATUS) { + unsigned int idx =3D addr / DW_APB_TIMER_STRIDE; + hwaddr reg =3D addr % DW_APB_TIMER_STRIDE; + + if (idx < s->num_timers) { + DWAPBTimerChannel *t =3D &s->timers[idx]; + + switch (reg) { + case DW_APB_TIMER_N_LOAD_COUNT: + t->load =3D value; + break; + case DW_APB_TIMER_N_CONTROL: { + uint32_t old_control =3D t->control; + uint32_t new_control =3D value & DW_APB_TIMER_CONTROL_RW_M= ASK; + + t->control =3D new_control; + if ((new_control ^ old_control) & + DW_APB_TIMER_CONTROL_ENABLE) { + if (new_control & DW_APB_TIMER_CONTROL_ENABLE) { + dw_apb_timer_enable(t); + } else { + dw_apb_timer_disable(t); + } + } + if ((new_control ^ old_control) & + DW_APB_TIMER_CONTROL_INT_MASK) { + dw_apb_timer_update_irq(t); + } + break; + } + default: + break; + } + } + } + + trace_dw_apb_timer_write(addr, value); +} + +static const MemoryRegionOps dw_apb_timer_ops =3D { + .read =3D dw_apb_timer_read, + .write =3D dw_apb_timer_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .impl =3D { + .min_access_size =3D 4, + .max_access_size =3D 4, + }, +}; + +static void dw_apb_timer_reset_hold(Object *obj, ResetType type) +{ + DWAPBTimerState *s =3D DW_APB_TIMER(obj); + + for (int i =3D 0; i < s->num_timers; i++) { + DWAPBTimerChannel *t =3D &s->timers[i]; + + ptimer_transaction_begin(t->ptimer); + ptimer_stop(t->ptimer); + ptimer_transaction_commit(t->ptimer); + + t->load =3D 0; + t->control =3D 0; + t->int_status =3D 0; + dw_apb_timer_update_irq(t); + } +} + +static const VMStateDescription vmstate_dw_apb_timer_channel =3D { + .name =3D "dw-apb-timer-channel", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (const VMStateField[]) { + VMSTATE_PTIMER(ptimer, DWAPBTimerChannel), + VMSTATE_CLOCK(clk, DWAPBTimerChannel), + VMSTATE_UINT32(load, DWAPBTimerChannel), + VMSTATE_UINT32(control, DWAPBTimerChannel), + VMSTATE_UINT32(int_status, DWAPBTimerChannel), + VMSTATE_END_OF_LIST() + } +}; + +static const VMStateDescription vmstate_dw_apb_timer =3D { + .name =3D "dw-apb-timer", + .fields =3D (const VMStateField[]) { + VMSTATE_STRUCT_VARRAY_UINT32(timers, DWAPBTimerState, + num_timers, 0, + vmstate_dw_apb_timer_channel, + DWAPBTimerChannel), + VMSTATE_END_OF_LIST() + } +}; + +static const Property dw_apb_timer_properties[] =3D { + DEFINE_PROP_UINT32("num-timers", DWAPBTimerState, num_timers, 1), +}; + +static void dw_apb_timer_init(Object *obj) +{ + DWAPBTimerState *s =3D DW_APB_TIMER(obj); + + for (int i =3D 0; i < DW_APB_TIMER_MAX_TIMERS; i++) { + DWAPBTimerChannel *t =3D &s->timers[i]; + g_autofree char *name =3D g_strdup_printf("timer[%d]", i); + + t->id =3D i; + t->clk =3D qdev_init_clock_in(DEVICE(obj), name, + dw_apb_timer_clk_update, t, + ClockUpdate); + } +} + +static void dw_apb_timer_realize(DeviceState *dev, Error **errp) +{ + DWAPBTimerState *s =3D DW_APB_TIMER(dev); + SysBusDevice *sbd =3D SYS_BUS_DEVICE(dev); + + if (s->num_timers =3D=3D 0 || s->num_timers > DW_APB_TIMER_MAX_TIMERS)= { + error_setg(errp, "dw-apb-timer: num-timers must be between 1 and %= u", + DW_APB_TIMER_MAX_TIMERS); + return; + } + + for (int i =3D 0; i < s->num_timers; i++) { + DWAPBTimerChannel *t =3D &s->timers[i]; + + if (!clock_has_source(t->clk)) { + error_setg(errp, "dw-apb-timer: timer[%u] clock must be connec= ted", + i); + return; + } + } + + for (int i =3D 0; i < s->num_timers; i++) { + DWAPBTimerChannel *t =3D &s->timers[i]; + + t->ptimer =3D ptimer_init(dw_apb_timer_tick, t, + PTIMER_POLICY_NO_IMMEDIATE_TRIGGER | + PTIMER_POLICY_NO_IMMEDIATE_RELOAD | + PTIMER_POLICY_NO_COUNTER_ROUND_DOWN); + ptimer_transaction_begin(t->ptimer); + ptimer_set_limit(t->ptimer, UINT32_MAX, 1); + ptimer_transaction_commit(t->ptimer); + sysbus_init_irq(sbd, &t->irq); + + /* The source may have been connected before the ptimer existed. */ + dw_apb_timer_clk_update(t, ClockUpdate); + } + + memory_region_init_io(&s->mmio, OBJECT(dev), &dw_apb_timer_ops, + s, TYPE_DW_APB_TIMER, DW_APB_TIMER_MMIO_SIZE); + sysbus_init_mmio(sbd, &s->mmio); +} + +static void dw_apb_timer_unrealize(DeviceState *dev) +{ + DWAPBTimerState *s =3D DW_APB_TIMER(dev); + + for (int i =3D 0; i < s->num_timers; i++) { + ptimer_free(s->timers[i].ptimer); + } +} + +static void dw_apb_timer_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + + dc->realize =3D dw_apb_timer_realize; + dc->unrealize =3D dw_apb_timer_unrealize; + dc->vmsd =3D &vmstate_dw_apb_timer; + dc->desc =3D "Synopsys DesignWare APB timer"; + rc->phases.hold =3D dw_apb_timer_reset_hold; + device_class_set_props(dc, dw_apb_timer_properties); +} + +static const TypeInfo dw_apb_timer_info =3D { + .name =3D TYPE_DW_APB_TIMER, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(DWAPBTimerState), + .instance_init =3D dw_apb_timer_init, + .class_init =3D dw_apb_timer_class_init, +}; + +static void dw_apb_timer_register_type(void) +{ + type_register_static(&dw_apb_timer_info); +} + +type_init(dw_apb_timer_register_type) diff --git a/hw/timer/meson.build b/hw/timer/meson.build index e2bcdbdf65..95cf83ec73 100644 --- a/hw/timer/meson.build +++ b/hw/timer/meson.build @@ -36,3 +36,4 @@ system_ss.add(when: 'CONFIG_SIFIVE_PWM', if_true: files('= sifive_pwm.c')) system_ss.add(when: 'CONFIG_AVR_TIMER16', if_true: files('avr_timer16.c')) =20 system_ss.add(when: 'CONFIG_HEX_QTIMER', if_true: files('qct-qtimer.c')) +system_ss.add(when: 'CONFIG_DW_APB_TIMER', if_true: files('dw-apb-timer.c'= )) diff --git a/hw/timer/trace-events b/hw/timer/trace-events index 636310f8ca..2393f55366 100644 --- a/hw/timer/trace-events +++ b/hw/timer/trace-events @@ -1,5 +1,13 @@ # See docs/devel/tracing.rst for syntax documentation. =20 +# dw-apb-timer.c +dw_apb_timer_read(uint64_t addr, uint32_t val) "DW APB timer read: [0x%" P= RIx64 "] -> 0x%" PRIx32 +dw_apb_timer_write(uint64_t addr, uint64_t val) "DW APB timer write: [0x%"= PRIx64 "] <- 0x%" PRIx64 +dw_apb_timer_tick(int idx) "DW APB timer %d tick" +dw_apb_timer_irq_clear(int idx) "DW APB timer %d IRQ cleared" +dw_apb_timer_enable(int idx, uint32_t load) "DW APB timer %d enabled load= =3D0x%" PRIx32 +dw_apb_timer_disable(int idx) "DW APB timer %d disabled" + # slavio_timer.c slavio_timer_get_out(uint64_t limit, uint32_t counthigh, uint32_t count) "= limit 0x%"PRIx64" count 0x%x0x%08x" slavio_timer_irq(uint32_t counthigh, uint32_t count) "callback: count 0x%x= 0x%08x" diff --git a/include/hw/timer/dw-apb-timer.h b/include/hw/timer/dw-apb-time= r.h new file mode 100644 index 0000000000..f6b83ae5ca --- /dev/null +++ b/include/hw/timer/dw-apb-timer.h @@ -0,0 +1,40 @@ +/* + * Synopsys DesignWare APB timer + * + * Copyright (c) 2026 raoyi + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef DW_APB_TIMER_H +#define DW_APB_TIMER_H + +#include "hw/core/sysbus.h" +#include "hw/core/irq.h" +#include "hw/core/clock.h" +#include "qom/object.h" + +#define TYPE_DW_APB_TIMER "dw-apb-timer" +OBJECT_DECLARE_SIMPLE_TYPE(DWAPBTimerState, DW_APB_TIMER) + +#define DW_APB_TIMER_MAX_TIMERS 8 + +typedef struct DWAPBTimerChannel { + struct ptimer_state *ptimer; + Clock *clk; + qemu_irq irq; + unsigned int id; + uint32_t load; + uint32_t control; + uint32_t int_status; +} DWAPBTimerChannel; + +struct DWAPBTimerState { + SysBusDevice parent_obj; + + MemoryRegion mmio; + uint32_t num_timers; + DWAPBTimerChannel timers[DW_APB_TIMER_MAX_TIMERS]; +}; + +#endif --=20 2.53.0 From nobody Mon Nov 23 04:26:39 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1793369448; cv=none; d=zohomail.com; s=zohoarc; b=Wq/JP2MjAeXIAuQKRvf56vWWJ1xL+uYAOvmrVJNnE6/MplnA5t1lSvYsQxcSGDi1pcSczQ/wRNVSQBLkXT8yIlIjeO/DYjlUsYRaxaSQ5GjS2YFyATQW3kjXGq5KroRGQ8f5lk0yNQMzUVIMj4uSFeQ95njPXFqjjdpQc8g/T+8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793369448; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=M54vx+wx4hKVGaumjpxsRCNAzEUUl0ekxzc9Veev8CM=; b=NhwIPzho7inMR38fjZF6pYByogRhLEmsNbYcoueNu4U71dCfXrl4eN+NlNT1zreaV4+Qdzx4uodF0D2HIeAUMcsO1nNu+Ar6ThnlZRrooPx/CJ/xoOj8wSDWOqdoigJVtEelLbd2NY809MAb+5uMLfYUEtimIu9+wUQOhogl1WA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793369448514265.84307238575286; Fri, 30 Oct 2026 07:10:48 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMnJ3-0004ux-IZ; Fri, 30 Oct 2026 10:10:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMmoU-000639-5p for qemu-devel@nongnu.org; Fri, 30 Oct 2026 09:38:34 -0400 Received: from mail-oo1-xc29.google.com ([2607:f8b0:4864:20::c29]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xMmoS-0007c3-BL for qemu-devel@nongnu.org; Fri, 30 Oct 2026 09:38:33 -0400 Received: by mail-oo1-xc29.google.com with SMTP id 006d021491bc7-702dedeb70dso487766eaf.1 for ; Fri, 30 Oct 2026 06:38:31 -0700 (PDT) Received: from DESKTOP-DFGP8OG.lan ([120.235.190.244]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-714f30600f0sm1861421eaf.12.2026.10.30.06.38.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Oct 2026 06:38:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793367511; x=1793972311; darn=nongnu.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=M54vx+wx4hKVGaumjpxsRCNAzEUUl0ekxzc9Veev8CM=; b=UpI0Xd7vRGBDv0LT9YQCgKt5wKEhi5571imhpKs62zdndgBNBv4g8NcBpyve2S4H4V WWnZoLXZW0VHqKuDxGq0+z7vitzEHnXJ8H7ye14g4mUJbO/2SV9ATSveE2fU85BsDlqv k+2OHhU8kmm4Okj7iZmPK7zM1nVrxuYzZU1vilcV6KKm/aVL8JvwvsbV57n3O8hy8mXf bd9vlbPU5uCyXqD11QxfJSnGJ8FQ9DvxnH/xYXqwyc5YNI7qzqQeU23vCmhPdSQ2l4Sk 4eGKkL0d0AMf/+MGN12hvhBFGQkCh52t1kwntGCkDsK4y1LZpjPcVzQCahS/GH8pwXuv xzvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793367511; x=1793972311; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=M54vx+wx4hKVGaumjpxsRCNAzEUUl0ekxzc9Veev8CM=; b=vOoUKzE92d+whOSZaFXe52LW4TtkH6aEuVzAtZaTNrt/VdV2KiwHFGSoBSsiFPxg2Y FZqslSR6vU5arym59GRw2yrLERYnOcgGXkuspFeRJVY/T3Tb5ZnRYUsJ34EW4HjXFZpn Hezq7w0ACePUlkJOrro8qiNj2DIlcPtAW15Z4XNY1JynouNatWc4Lvoaj6Lvdoq76jbv YbdEDlKAkZ61BfFT80x/lJkec6BIXXKSI75cC/u2gS5T1ey1mijkB3comDvXVEC/XvLW UTM+tCX4NwItc/z73v5a25lyRUCqvP6Rl47rOo3XVh4pp/ON6HtrgfIHEee0pqP3Fp47 hWKA== X-Gm-Message-State: AFq9FYJnM03fZg+8TxqpKeuyxAsIcciAgIme7EC9OazMj5FGJqeeQobl grmd1v87+VTjCsRE0n7R1DJbc5yYiCxMjgZpcEiCQARgBnjTOowtyNe8LV8G/Q== X-Gm-Gg: AYBFou2TCPgfvxpwn/vQjR15/FwlI4dABSd62YGjjxm5hEXLVsYwHmDPmnnmbYlD+pb szNRcHoQEp8v8ARgbat8Xbn9uL4u8/FvWCGLbiaZjpGFmh+wk9eH5/SbY/0k9jR9VuVEuSpVSKc V8/huhOQr2gob+aM0VMKqpnMvryrlGNVxfBfWmdkABQAhQh46nTztuUCsY3QcZtZyl5EyO5R4Jx Q8h35zxcv06BYcNylEGJifvTGy1AlhhCwIo+UCV+UKzn07ix9XdQZaznnm1tRYZweErlHs3LvmP ozYxxVJ3WUjDMOWcLCVMzvX+WYHVRpBSGV1W5eOgUhFLPJpRzINpKjBIT6eIOsvBrNemg4W4SC5 tSFOIRsWKqiKe6pCvHi+nx8p20PxVKjgBs6YkyhTuoTKY0SpChE4bPAVHwshGtcNcCb6ylRHiOw VjFoAnvh7Z81qm/GpEgXrQK1c5oKn5cjcyjfh03nxiScSKB3qWo3ZxJoyRE0eMkbJgAn7WMrnyg HQdhSJoel7uHGJx6nMQkOjk6Uo2weoSjuQd+qSSx7hd/ngzOlqq22Qq782QtFgagk2nOOt8TcwJ VxO8H6jO+gNw/1sUlsecfrqvx4hJ3FoPXUiEai40S9eD+/lkCTfYtKcF1Sb7dQA= X-Received: by 2002:a05:6820:1ca5:b0:700:52ae:7e4a with SMTP id 006d021491bc7-714f27855cdmr1319617eaf.71.1793367510876; Fri, 30 Oct 2026 06:38:30 -0700 (PDT) From: raoyi To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, alistair.francis@wdc.com, palmer@dabbelt.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, bmeng.cn@gmail.com, philmd@oss.qualcomm.com, pbonzini@redhat.com, lvivier@redhat.com, farosas@suse.de, caojunze424@gmail.com, raoyi Subject: [PATCH v6 2/3] hw/riscv: add DesignWare APB timer to K230 board Date: Fri, 30 Oct 2026 21:37:03 +0800 Message-ID: <20261030133704.99678-3-rao232328@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261030133704.99678-1-rao232328@gmail.com> References: <20261030133704.99678-1-rao232328@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::c29; envelope-from=rao232328@gmail.com; helo=mail-oo1-xc29.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 30 Oct 2026 10:09:55 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1793369449384158500 Wire up the DesignWare APB timer to the K230 SoC with num-timers=3D6 and a 50 MHz clock. Connect timer IRQs to the PLIC. Remove the previous unimplemented timer stub. Signed-off-by: raoyi Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/riscv/Kconfig | 1 + hw/riscv/k230.c | 33 ++++++++++++++++++++++++++++++--- include/hw/riscv/k230.h | 8 ++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index d89fdfcc86..dea1430b94 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -181,6 +181,7 @@ config K230 select K230_WDT select K230_GSDMA select K230_DECOMP_GZIP + select DW_APB_TIMER =20 config SOPHGO_CV1800B bool diff --git a/hw/riscv/k230.c b/hw/riscv/k230.c index 1e4ca38579..a5d789c070 100644 --- a/hw/riscv/k230.c +++ b/hw/riscv/k230.c @@ -24,6 +24,8 @@ #include "target/riscv/cpu.h" #include "hw/core/loader.h" #include "hw/core/sysbus.h" +#include "hw/core/clock.h" +#include "hw/core/qdev-clock.h" #include "hw/riscv/k230.h" #include "hw/riscv/boot.h" #include "hw/intc/riscv_aclint.h" @@ -36,6 +38,9 @@ #define K230_DIRECT_KERNEL_ADDR 0x8200000 #define K230_DIRECT_DTB_ADDR 0xa000000 =20 +#define K230_TIMER_NUM_CHANNELS 6 +#define K230_TIMER_DEFAULT_FREQ 50000000 + static const MemMapEntry memmap[] =3D { [K230_DEV_DDRC] =3D { 0x00000000, 0x80000000 }, [K230_DEV_KPU_L2_CACHE] =3D { 0x80000000, 0x00200000 }, @@ -118,6 +123,19 @@ static void k230_soc_init(Object *obj) object_initialize_child(obj, "k230-gsdma", &s->gsdma, TYPE_K230_GSDMA); object_initialize_child(obj, "k230-decomp-gzip", &s->decomp_gzip, TYPE_K230_DECOMP_GZIP); + object_initialize_child(obj, "dw-apb-timer", &s->timer, + TYPE_DW_APB_TIMER); + + qdev_prop_set_uint32(DEVICE(&s->timer), "num-timers", + K230_TIMER_NUM_CHANNELS); + + Clock *timer_clk =3D clock_new(OBJECT(s), "timer-clk"); + clock_set_hz(timer_clk, K230_TIMER_DEFAULT_FREQ); + for (int i =3D 0; i < K230_TIMER_NUM_CHANNELS; i++) { + g_autofree char *clock_name =3D g_strdup_printf("timer[%d]", i); + + qdev_connect_clock_in(DEVICE(&s->timer), clock_name, timer_clk); + } =20 qdev_prop_set_uint32(DEVICE(cpu0), "hartid-base", 0); qdev_prop_set_string(DEVICE(cpu0), "cpu-type", TYPE_RISCV_CPU_THEAD_C9= 08); @@ -212,6 +230,18 @@ static void k230_soc_realize(DeviceState *dev, Error *= *errp) k230_create_uart(sys_mem, DEVICE(s->c908_plic), i); } =20 + /* Timer */ + if (!sysbus_realize(SYS_BUS_DEVICE(&s->timer), errp)) { + return; + } + sysbus_mmio_map(SYS_BUS_DEVICE(&s->timer), 0, memmap[K230_DEV_TIMER].b= ase); + + for (int i =3D 0; i < K230_TIMER_NUM_CHANNELS; i++) { + sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer), i, + qdev_get_gpio_in(DEVICE(s->c908_plic), + K230_TIMER0_IRQ + i)); + } + /* Watchdog */ for (int i =3D 0; i < 2; i++) { if (!sysbus_realize(SYS_BUS_DEVICE(&s->wdt[i]), errp)) { @@ -325,9 +355,6 @@ static void k230_soc_realize(DeviceState *dev, Error **= errp) create_unimplemented_device("iomux", memmap[K230_DEV_IOMUX].base, memmap[K230_DEV_IOMUX].size); =20 - create_unimplemented_device("timer", memmap[K230_DEV_TIMER].base, - memmap[K230_DEV_TIMER].size); - create_unimplemented_device("wdt0", memmap[K230_DEV_WDT0].base, memmap[K230_DEV_WDT0].size); =20 diff --git a/include/hw/riscv/k230.h b/include/hw/riscv/k230.h index 8679d09891..4c416573eb 100644 --- a/include/hw/riscv/k230.h +++ b/include/hw/riscv/k230.h @@ -21,6 +21,7 @@ #include "hw/watchdog/k230_wdt.h" #include "hw/dma/k230_gsdma.h" #include "hw/misc/k230_decomp_gzip.h" +#include "hw/timer/dw-apb-timer.h" =20 #define C908_CPU_HARTID (0) =20 @@ -40,6 +41,7 @@ typedef struct K230SoCState { K230WdtState wdt[2]; K230GSDMAState gsdma; K230DecompGzipState decomp_gzip; + DWAPBTimerState timer; MemoryRegion sram; MemoryRegion bootrom; =20 @@ -135,6 +137,12 @@ enum { K230_UART2_IRQ =3D 18, K230_UART3_IRQ =3D 19, K230_UART4_IRQ =3D 20, + K230_TIMER0_IRQ =3D 101, + K230_TIMER1_IRQ =3D 102, + K230_TIMER2_IRQ =3D 103, + K230_TIMER3_IRQ =3D 104, + K230_TIMER4_IRQ =3D 105, + K230_TIMER5_IRQ =3D 106, K230_WDT0_IRQ =3D 107, K230_WDT1_IRQ =3D 108, K230_GSDMA_IRQ =3D 140, --=20 2.53.0 From nobody Mon Nov 23 04:26:39 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1793369437; cv=none; d=zohomail.com; s=zohoarc; b=HlifOEsbEXGS4zPfZB7uyQL15eoIOz2s/iIY0wLzZvRKi4/vgdMYUd+S+/OUdTH7mRvoWsIYIGq0UfD2F6Bo9Xpqa8Jckxf1wNEDwlvjGeQh5GRw+YO6E4o2UGl8DrgjeS/YK4RLl3D+tDI5NqMIADHYtZJw8y4UnymnzcbR9F8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793369437; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=DvwnfkxM9sorbQR/zXxvf5R6xwOBdfOZMc+U57NOVTs=; b=nDR2Vb8U6KaaNe1d4RNGod/lc2GzUSsleERxF8vJFAk4bCyOH8BioQPLYlkVP0rHbiV9wbYr5ELuctYozykK0V4QSrMb3X9kNVWL2vup0K1fTwJukrTnen4SLBPo6OENWSGD8auhYDaWpJp47Fe0iZ9UDAvUYw/9XJO1MoJSS00= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793369437251183.1834592342542; Fri, 30 Oct 2026 07:10:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMnJ4-0004xO-IN; Fri, 30 Oct 2026 10:10:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMmoc-000699-5u for qemu-devel@nongnu.org; Fri, 30 Oct 2026 09:38:42 -0400 Received: from mail-oo1-xc34.google.com ([2607:f8b0:4864:20::c34]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xMmoa-0007cQ-3x for qemu-devel@nongnu.org; Fri, 30 Oct 2026 09:38:41 -0400 Received: by mail-oo1-xc34.google.com with SMTP id 006d021491bc7-6df7f544919so455827eaf.1 for ; Fri, 30 Oct 2026 06:38:39 -0700 (PDT) Received: from DESKTOP-DFGP8OG.lan ([120.235.190.244]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-714f30600f0sm1861421eaf.12.2026.10.30.06.38.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Oct 2026 06:38:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793367519; x=1793972319; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=DvwnfkxM9sorbQR/zXxvf5R6xwOBdfOZMc+U57NOVTs=; b=mF3vkaOyEmNgBRRpRjxCjJ2XJdRBqskTbd/ZFZvn901Ra0YF9SMHjvQELxSeojnaij Joz/3LmSWKaMQKTjSnlgJ6fR8ha80yuSc4KLsTct7Z+jH4LFRAxKOPLS8RIVOPV8SeQ4 OGtg18fN42ElR+bLMvl+nM3yX4LEYv7LHhrQrUIKbBm67+PjIt8NZEBbcxfrlu9nGHiE 6Jb+lzh3GXBS0/ejhCLYwOpzRanhy0KtExqLyBSEPIVvPRFGB4bvWQCNjyAZO2Ea8az7 kFSG28/4SxPwTgvIA1A8jT+q7gdzwMmFck9FEl9xLXKM30DoHOScIL9vOPgoOI1h5iTr oBvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793367519; x=1793972319; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=DvwnfkxM9sorbQR/zXxvf5R6xwOBdfOZMc+U57NOVTs=; b=jUgIKB6Sqb9lMNT1IH8Y2DjLLfHzefRuPW0dTzTOupHdfT+0JkujBWqfJx945tCqY8 KTDtgb0PfvBxoPEQM/AmHdA3wwwYMZMF6wLLP6WpF0tSO9jN1pROxLzoW9YU4DS3wsE/ HINiS5cAFjC2n2u+fIoxLd+3YWHCpPqA686Eh9qUWRY+vNEQUyg2yxiTXSqihaqUVaZq Op0D8CYtkjquWyvjJEfoBrbvI9djg4+9j59iNaxhGHv9eaXFiRWqASDVOjYQWgbplpCb GhXjNUFShEqGfwRZRQJ3+SxFyZ8ToCyRAdZvPaRS6foniJCHfEQIh4LuSh6MjCgjRMVI lhxw== X-Gm-Message-State: AFq9FYIJTyGa0QXSmF1Hk8qhFeJ9GU4VurRc7kb3pkPhEO5ww1WG0gD6 R2kAQUlmy/dS/IkkMs7noWzrL05V1plDKCWgjUmGAdOHm7GhDiS/SYZGQHXc8w== X-Gm-Gg: AYBFou2W9J0GWupuLytdcrOSlurPMomjKtAGgPYjlbdbHjNBYClPxSRgWU8ogxWV5Nr tGroIA4vkPR0afwByo/VGlyiFc99mgaODv4xz2ySXXm0/jKznu23d4CXWTGFI1iOFb5QNM89+N/ ryuqbStPwlNWR8icuqNe6ZU4cCYGM3qjpJs/paJezW2KT+wFamgxgP70MA5SnWwZK3DF0G7ot3S YLIhqyH+3EAiOULbC+tO3ckYqnPsb8iqKXT3TLHZp3InzuJH2RqfCnIxJD2cbOCOTnv5qW6rr2U 3oIfc/ohtvDgfDV3kdeGPsq+oiBfLHiaWnJXQP16C6D+2ysET28VMa9dIovC0/rPUYKVtQNXeBl 3ffmjYs53FW5o04hokytWGIdYrlwSH1N0DuJOolVP6IeoyDzVbuTI7iJF1BN2tC0i39GVz8a7aJ g7vaEjLLdZaE25QnBwFGjr4mjoDkpIF2bERTcYIueSHWyZZZd+w16m4vOlpLtYAjvx0rS7AwLCG tQK6800j81EapMcjMYmSraBjUH5GlSbYwIp31AjBLksMKezLf2ORvs+Gbm3zfgkZgN/z/KECHt3 4jNxs6q49mAPX0mKIJU/vlwjpVlQtqG0UpEM2nPGXA== X-Received: by 2002:a05:6820:80c:b0:713:e433:7e6d with SMTP id 006d021491bc7-714f2927fe1mr1479673eaf.36.1793367518737; Fri, 30 Oct 2026 06:38:38 -0700 (PDT) From: raoyi To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, alistair.francis@wdc.com, palmer@dabbelt.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, bmeng.cn@gmail.com, philmd@oss.qualcomm.com, pbonzini@redhat.com, lvivier@redhat.com, farosas@suse.de, caojunze424@gmail.com, raoyi , Bin Meng Subject: [PATCH v6 3/3] tests/qtest: add k230 dwapb timer test Date: Fri, 30 Oct 2026 21:37:04 +0800 Message-ID: <20261030133704.99678-4-rao232328@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261030133704.99678-1-rao232328@gmail.com> References: <20261030133704.99678-1-rao232328@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::c34; envelope-from=rao232328@gmail.com; helo=mail-oo1-xc34.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 30 Oct 2026 10:09:57 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1793369442546154100 Content-Type: text/plain; charset="utf-8" Add qtest coverage for K230 DW APB timer, including free-running, periodic, interrupt mask, disable behavior, current value, dynamic reload, and multi-channel scenarios. Add timer test file to MAINTAINERS. Signed-off-by: raoyi Reviewed-by: Bin Meng --- MAINTAINERS | 1 + tests/qtest/k230-dwapb-timer-test.c | 274 ++++++++++++++++++++++++++++ tests/qtest/meson.build | 3 +- 3 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/k230-dwapb-timer-test.c diff --git a/MAINTAINERS b/MAINTAINERS index 60a5664ce5..3817598c29 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1897,6 +1897,7 @@ F: include/hw/misc/k230_decomp_gzip.h F: include/hw/timer/dw-apb-timer.h F: tests/functional/riscv64/test_k230.py F: tests/qtest/k230-ddr-test.c +F: tests/qtest/k230-dwapb-timer-test.c F: tests/qtest/k230-wdt-test.c F: tests/qtest/k230-gsdma-test.c F: tests/qtest/k230-decomp-gzip-test.c diff --git a/tests/qtest/k230-dwapb-timer-test.c b/tests/qtest/k230-dwapb-t= imer-test.c new file mode 100644 index 0000000000..01bf03a8b6 --- /dev/null +++ b/tests/qtest/k230-dwapb-timer-test.c @@ -0,0 +1,274 @@ +/* + * QTest testcase for the DesignWare APB timer through the K230 machine + * + * Copyright (c) 2026 raoyi + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include "qemu/osdep.h" +#include "qemu/timer.h" +#include "libqtest.h" + +#define TIMER_BASE 0x91105800 +#define K230_TIMER_NUM_CHANNELS 6 +#define K230_TIMER_DEFAULT_FREQ 50000000 + +#define DW_APB_TIMER_STRIDE 0x14 +#define DW_APB_TIMER_N_LOAD_COUNT 0x00 +#define DW_APB_TIMER_N_CURRENT_VALUE 0x04 +#define DW_APB_TIMER_N_CONTROL 0x08 +#define DW_APB_TIMER_N_EOI 0x0c +#define DW_APB_TIMER_N_INT_STATUS 0x10 +#define DW_APB_TIMER_INT_STATUS 0xa0 +#define DW_APB_TIMER_EOI 0xa4 +#define DW_APB_TIMER_RAW_INT_STATUS 0xa8 +#define DW_APB_TIMER_CONTROL_ENABLE BIT(0) +#define DW_APB_TIMER_CONTROL_MODE_PERIODIC BIT(1) +#define DW_APB_TIMER_CONTROL_INT_MASK BIT(2) + +#define TIMER_REG(n, reg) (TIMER_BASE + (n) * DW_APB_TIMER_STRIDE + (reg)) +#define TIMER_TICK_NS (NANOSECONDS_PER_SECOND / K230_TIMER_DEFAULT_FREQ) + +static void timer_load(QTestState *qts, int n, uint32_t value) +{ + qtest_writel(qts, TIMER_REG(n, DW_APB_TIMER_N_LOAD_COUNT), value); +} + +static void timer_enable(QTestState *qts, int n, uint32_t ctrl) +{ + qtest_writel(qts, TIMER_REG(n, DW_APB_TIMER_N_CONTROL), ctrl); +} + +static void timer_disable(QTestState *qts, int n) +{ + qtest_writel(qts, TIMER_REG(n, DW_APB_TIMER_N_CONTROL), 0); +} + +static uint32_t timer_get_and_clear_status(QTestState *qts, int n) +{ + uint32_t int_status =3D qtest_readl(qts, + TIMER_REG(n, DW_APB_TIMER_N_INT_STATUS)); + + if (int_status) { + qtest_readl(qts, TIMER_REG(n, DW_APB_TIMER_N_EOI)); + } + + return int_status; +} + +static void test_timer_free_running(void) +{ + QTestState *qts =3D qtest_init("-machine k230"); + + timer_load(qts, 0, 100); + timer_enable(qts, 0, DW_APB_TIMER_CONTROL_ENABLE); + + qtest_clock_step(qts, 101ULL * TIMER_TICK_NS + 1); + g_assert_cmphex(timer_get_and_clear_status(qts, 0), =3D=3D, 1); + + qtest_clock_step(qts, 10ULL * TIMER_TICK_NS); + uint32_t cur =3D qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_CURRENT_VALUE)); + g_assert_cmphex(cur, <, UINT32_MAX); + g_assert_cmphex(cur, >, 0); + + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_INT_STATUS)), =3D=3D, 0); + + qtest_quit(qts); +} + +static void test_timer_periodic(void) +{ + QTestState *qts =3D qtest_init("-machine k230"); + int i; + + timer_load(qts, 0, 100); + timer_enable(qts, 0, DW_APB_TIMER_CONTROL_ENABLE | + DW_APB_TIMER_CONTROL_MODE_PERIODIC); + + for (i =3D 0; i < 3; i++) { + qtest_clock_step(qts, 101ULL * TIMER_TICK_NS + 1); + g_assert_cmphex(timer_get_and_clear_status(qts, 0), =3D=3D, 1); + uint32_t cur =3D qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_CURRENT_VALUE)); + g_assert_cmphex(cur, <=3D, 100); + } + + qtest_quit(qts); +} + +static void test_timer_int_mask(void) +{ + QTestState *qts =3D qtest_init("-machine k230"); + + timer_load(qts, 0, 100); + timer_enable(qts, 0, DW_APB_TIMER_CONTROL_ENABLE | + DW_APB_TIMER_CONTROL_MODE_PERIODIC | + DW_APB_TIMER_CONTROL_INT_MASK); + + qtest_clock_step(qts, 101ULL * TIMER_TICK_NS + 1); + + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_INT_STATUS)) & 1, =3D=3D= , 0); + g_assert_cmphex(qtest_readl(qts, + TIMER_BASE + DW_APB_TIMER_INT_STATUS), =3D=3D, 0); + g_assert_cmphex(qtest_readl(qts, + TIMER_BASE + DW_APB_TIMER_RAW_INT_STATUS) & 1, =3D= =3D, 1); + + /* Unmasking a pending interrupt must assert it immediately. */ + timer_enable(qts, 0, DW_APB_TIMER_CONTROL_ENABLE | + DW_APB_TIMER_CONTROL_MODE_PERIODIC); + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_INT_STATUS)), =3D=3D, 1); + g_assert_cmphex(qtest_readl(qts, + TIMER_BASE + DW_APB_TIMER_INT_STATUS) & 1, =3D=3D, 1= ); + + qtest_quit(qts); +} + +static void test_timer_disable_clears_irq(void) +{ + QTestState *qts =3D qtest_init("-machine k230"); + + timer_load(qts, 0, 100); + timer_enable(qts, 0, DW_APB_TIMER_CONTROL_ENABLE | + DW_APB_TIMER_CONTROL_MODE_PERIODIC); + + qtest_clock_step(qts, 101ULL * TIMER_TICK_NS + 1); + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_INT_STATUS)) & 1, =3D=3D= , 1); + + timer_disable(qts, 0); + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_INT_STATUS)) & 1, =3D=3D= , 0); + + qtest_quit(qts); +} + +static void test_timer_current_disabled(void) +{ + QTestState *qts =3D qtest_init("-machine k230"); + + /* TRM: "A '0' is always read back when the timer is not enabled" */ + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_CURRENT_VALUE)), =3D=3D,= 0); + + timer_load(qts, 0, 9999); + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_CURRENT_VALUE)), =3D=3D,= 0); + + timer_enable(qts, 0, DW_APB_TIMER_CONTROL_ENABLE); + qtest_clock_step(qts, 10ULL * TIMER_TICK_NS); + uint32_t cur_enabled =3D qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_CURRENT_VALUE)); + g_assert_cmphex(cur_enabled, >, 0); + g_assert_cmphex(cur_enabled, <, UINT32_MAX); + + timer_disable(qts, 0); + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_CURRENT_VALUE)), =3D=3D,= 0); + + qtest_quit(qts); +} + +static void test_timer_dynamic_reload(void) +{ + QTestState *qts =3D qtest_init("-machine k230"); + uint32_t cur; + + timer_load(qts, 0, 1000); + timer_enable(qts, 0, DW_APB_TIMER_CONTROL_ENABLE | + DW_APB_TIMER_CONTROL_MODE_PERIODIC); + + qtest_clock_step(qts, 100ULL * TIMER_TICK_NS); + + timer_load(qts, 0, 200); + + qtest_clock_step(qts, 901ULL * TIMER_TICK_NS + 1); + g_assert_cmphex(timer_get_and_clear_status(qts, 0), =3D=3D, 1); + + qtest_clock_step(qts, 100ULL * TIMER_TICK_NS); + cur =3D qtest_readl(qts, TIMER_REG(0, DW_APB_TIMER_N_CURRENT_VALUE)); + g_assert_cmphex(cur, =3D=3D, 99); + + qtest_quit(qts); +} + +static void test_timer_all_channels(void) +{ + QTestState *qts =3D qtest_init("-machine k230"); + int i; + + for (i =3D 0; i < K230_TIMER_NUM_CHANNELS; i++) { + timer_load(qts, i, 100 + i * 50); + timer_enable(qts, i, DW_APB_TIMER_CONTROL_ENABLE | + DW_APB_TIMER_CONTROL_MODE_PERIODIC); + } + + /* Step past timer 0 expiry (load=3D100) - only timer 0 should fire */ + qtest_clock_step(qts, 101ULL * TIMER_TICK_NS + 1); + uint32_t sts =3D qtest_readl(qts, TIMER_BASE + DW_APB_TIMER_INT_STATUS= ); + g_assert_cmphex(sts & 1, =3D=3D, 1); + g_assert_cmphex(sts & 0x3e, =3D=3D, 0); + + /* Step to timer 1 expiry (another 50 ticks) */ + qtest_clock_step(qts, 50ULL * TIMER_TICK_NS + 1); + sts =3D qtest_readl(qts, TIMER_BASE + DW_APB_TIMER_INT_STATUS); + g_assert_cmphex(sts & 0x3, =3D=3D, 0x3); + g_assert_cmphex(sts & 0x3c, =3D=3D, 0); + + /* Step to expiry of all remaining timers (another 200 ticks) */ + qtest_clock_step(qts, 200ULL * TIMER_TICK_NS + 1); + sts =3D qtest_readl(qts, TIMER_BASE + DW_APB_TIMER_INT_STATUS); + g_assert_cmphex(sts, =3D=3D, 0x3f); + + /* EOI_ALL clears all */ + qtest_readl(qts, TIMER_BASE + DW_APB_TIMER_EOI); + sts =3D qtest_readl(qts, TIMER_BASE + DW_APB_TIMER_INT_STATUS); + g_assert_cmphex(sts, =3D=3D, 0); + + qtest_quit(qts); +} + +static void test_timer_reset(void) +{ + QTestState *qts =3D qtest_init("-machine k230"); + + timer_load(qts, 0, 100); + timer_enable(qts, 0, DW_APB_TIMER_CONTROL_ENABLE | + DW_APB_TIMER_CONTROL_MODE_PERIODIC); + qtest_clock_step(qts, 10ULL * TIMER_TICK_NS); + + qtest_system_reset(qts); + + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_LOAD_COUNT)), =3D=3D, 0); + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_CONTROL)), =3D=3D, 0); + g_assert_cmphex(qtest_readl(qts, + TIMER_REG(0, DW_APB_TIMER_N_CURRENT_VALUE)), =3D=3D,= 0); + g_assert_cmphex(qtest_readl(qts, + TIMER_BASE + DW_APB_TIMER_RAW_INT_STATUS), =3D=3D, 0= ); + + qtest_quit(qts); +} + +int main(int argc, char *argv[]) +{ + g_test_init(&argc, &argv, NULL); + + qtest_add_func("/dw-apb-timer/free_running", test_timer_free_running); + qtest_add_func("/dw-apb-timer/periodic", test_timer_periodic); + qtest_add_func("/dw-apb-timer/int_mask", test_timer_int_mask); + qtest_add_func("/dw-apb-timer/disable_clears_irq", + test_timer_disable_clears_irq); + qtest_add_func("/dw-apb-timer/current_disabled", + test_timer_current_disabled); + qtest_add_func("/dw-apb-timer/dynamic_reload", + test_timer_dynamic_reload); + qtest_add_func("/dw-apb-timer/all_channels", test_timer_all_channels); + qtest_add_func("/dw-apb-timer/reset", test_timer_reset); + + return g_test_run(); +} diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index 46d4fc1d2b..9cadae79f4 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -318,7 +318,8 @@ qtests_riscv64 =3D ['riscv-csr-test'] + \ config_all_devices.has_key('CONFIG_RISCV_IOMMU') ? ['iommu-riscv-test'] : []) + \ (config_all_devices.has_key('CONFIG_K230') ? - ['k230-ddr-test', 'k230-wdt-test', 'k230-gsdma-test', 'k230-decomp-gzip= -test'] : []) + \ + ['k230-ddr-test', 'k230-wdt-test', 'k230-gsdma-test', + 'k230-decomp-gzip-test', 'k230-dwapb-timer-test'] : []) + \ ['milkv-duo-test'] =20 qtests_hexagon =3D ['boot-serial-test', 'hexagon-config-test', 'l2vic-test= ', --=20 2.53.0