From nobody Mon Feb 9 17:59:59 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1644474320855148.05672091524912; Wed, 9 Feb 2022 22:25:20 -0800 (PST) Received: from localhost ([::1]:55702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nI2tH-0007Ty-EU for importer@patchew.org; Thu, 10 Feb 2022 01:25:17 -0500 Received: from eggs.gnu.org ([209.51.188.92]:49410) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nI2mP-0003eC-Pf for qemu-devel@nongnu.org; Thu, 10 Feb 2022 01:18:11 -0500 Received: from [2607:f8b0:4864:20::42c] (port=42774 helo=mail-pf1-x42c.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nI2mA-0001Rm-Oq for qemu-devel@nongnu.org; Thu, 10 Feb 2022 01:18:09 -0500 Received: by mail-pf1-x42c.google.com with SMTP id i6so6554280pfc.9 for ; Wed, 09 Feb 2022 22:17:54 -0800 (PST) Received: from hsinchu16.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id q2sm23110266pfj.94.2022.02.09.22.17.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 22:17:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=I68MaYP/47cvyogFQOxoaIKfudeSxoDKYkIw4NjBFh0=; b=Skrce7O3f7pAqHwb9Ayp3OuHahMsNsMAxtCxRq0Pjb99+/+rBMUAfdzpct7TuJPBNW JaP1OwBC6cGOrYZY21fR7du7YCE5JgkNnXFbL6s1AlcPR0U4PR1feSjxzjg5q0ZAKrPl jCGandgKzjzGH9fqIcSkar7GV82lrXnTCmB5oR6yMHaRxDJwnm6bI0UEFIFK92+aEw78 g71AoF+V/xXbKSwdXgTWQ3mJa1MloSJQxQiaK89OvK60w1AwiaSFKe9QZLM/km6Thbaa 9IW5twLHSXOvSyVK3WzLKLWXln45FikizE0jezD9+oQfRH4Fge9inTsMrBVZDXNvm/DY XwYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=I68MaYP/47cvyogFQOxoaIKfudeSxoDKYkIw4NjBFh0=; b=i+orfm3++9yTwbICZL/UZI3rL6gJC4gwQQ1fTS4fNO3nb/ec5jPITg2/bntwDc0KbD 3HFnnXpGNTPyqTO9PIoOhVpI5xPq8rOQAD5M6duwz3JZXcf8UEZdSu3SYuLBGKbjFpLq BfNrpdGrBd5+xfekddw6HZIVQXUOP6sms4hlz7GiD3Pu6QnuCOnuc/5ct/V/LT4ZFDkt k74HkvpMTBsQhFiQGb6BGpyNHeJi0/E1sk+tCWdcDPxWP02HsZyMv38dSdwEDmUb/aGr RW9Ai/1z82wTyM+tYweQ5WU4jDy7D/RcgldoPD8TqQGDq8zioybclKuEBDpwccJ5qpaY HkPA== X-Gm-Message-State: AOAM531FHVePcdh/WeImKErnMxPMMD6B2b8M9UA9MklqEYHutcnxy6ko ybsinN/3ueavZwGHrSiAWuc8RK4IegOGxCT8 X-Google-Smtp-Source: ABdhPJw00x5V16QkU/4kMLiLhpAMEGrQ7K/RfLcUlpBPm/uMaWekB9gYeMf7oOCWEoN/ZpFvq0j9EQ== X-Received: by 2002:a63:e70b:: with SMTP id b11mr5000458pgi.142.1644473872733; Wed, 09 Feb 2022 22:17:52 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org Subject: [RFC PATCH v2 3/3] hw/intc: Make RISC-V ACLINT mtime MMIO register writable Date: Thu, 10 Feb 2022 14:17:35 +0800 Message-Id: <20220210061737.1171-4-frank.chang@sifive.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220210061737.1171-1-frank.chang@sifive.com> References: <20220210061737.1171-1-frank.chang@sifive.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::42c (failed) 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=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=frank.chang@sifive.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 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, PDS_HP_HELO_NORDNS=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Anup Patel , qemu-riscv@nongnu.org, Frank Chang , Bin Meng , Palmer Dabbelt , Alistair Francis , David Hoppenbrouwers , LIU Zhiwei Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1644474321757100001 Content-Type: text/plain; charset="utf-8" From: Frank Chang RISC-V privilege spec defines that mtime is exposed as a memory-mapped machine-mode read-write register. However, as QEMU uses host monotonic timer as timer source, this makes mtime to be read-only in RISC-V ACLINT. This patch makes mtime to be writable by recording the time delta value between the mtime value to be written and the timer value at the time mtime is written. Time delta value is then added back whenever the timer value is retrieved. Signed-off-by: Frank Chang --- hw/intc/riscv_aclint.c | 65 ++++++++++++++++++++++------------ include/hw/intc/riscv_aclint.h | 1 + target/riscv/cpu.h | 8 ++--- target/riscv/cpu_helper.c | 4 +-- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/hw/intc/riscv_aclint.c b/hw/intc/riscv_aclint.c index e7b103e83a..2d7d7361be 100644 --- a/hw/intc/riscv_aclint.c +++ b/hw/intc/riscv_aclint.c @@ -38,12 +38,18 @@ typedef struct riscv_aclint_mtimer_callback { int num; } riscv_aclint_mtimer_callback; =20 -static uint64_t cpu_riscv_read_rtc(uint32_t timebase_freq) +static uint64_t cpu_riscv_read_rtc_raw(uint32_t timebase_freq) { return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), timebase_freq, NANOSECONDS_PER_SECOND); } =20 +static uint64_t cpu_riscv_read_rtc(void *opaque) +{ + RISCVAclintMTimerState *mtimer =3D opaque; + return cpu_riscv_read_rtc_raw(mtimer->timebase_freq) + mtimer->time_de= lta; +} + /* * Called when timecmp is written to update the QEMU timer or immediately * trigger timer interrupt if mtimecmp <=3D current timer value. @@ -51,13 +57,13 @@ static uint64_t cpu_riscv_read_rtc(uint32_t timebase_fr= eq) static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtim= er, RISCVCPU *cpu, int hartid, - uint64_t value, - uint32_t timebase_freq) + uint64_t value) { + uint32_t timebase_freq =3D mtimer->timebase_freq; uint64_t next; uint64_t diff; =20 - uint64_t rtc_r =3D cpu_riscv_read_rtc(timebase_freq); + uint64_t rtc_r =3D cpu_riscv_read_rtc(mtimer); =20 cpu->env.timecmp =3D value; if (cpu->env.timecmp <=3D rtc_r) { @@ -140,11 +146,11 @@ static uint64_t riscv_aclint_mtimer_read(void *opaque= , hwaddr addr, } } else if (addr =3D=3D mtimer->time_base) { /* time_lo for RV32/RV64 or timecmp for RV64 */ - uint64_t rtc =3D cpu_riscv_read_rtc(mtimer->timebase_freq); + uint64_t rtc =3D cpu_riscv_read_rtc(mtimer); return (size =3D=3D 4) ? (rtc & 0xFFFFFFFF) : rtc; } else if (addr =3D=3D mtimer->time_base + 4) { /* time_hi */ - return (cpu_riscv_read_rtc(mtimer->timebase_freq) >> 32) & 0xFFFFF= FFF; + return (cpu_riscv_read_rtc(mtimer) >> 32) & 0xFFFFFFFF; } =20 qemu_log_mask(LOG_UNIMP, @@ -157,6 +163,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwa= ddr addr, uint64_t value, unsigned size) { RISCVAclintMTimerState *mtimer =3D opaque; + int i; =20 if (addr >=3D mtimer->timecmp_base && addr < (mtimer->timecmp_base + (mtimer->num_harts << 3))) { @@ -172,35 +179,49 @@ static void riscv_aclint_mtimer_write(void *opaque, h= waddr addr, /* timecmp_lo for RV32/RV64 */ uint64_t timecmp_hi =3D env->timecmp >> 32; riscv_aclint_mtimer_write_timecmp(mtimer, RISCV_CPU(cpu), = hartid, - timecmp_hi << 32 | (value & 0xFFFFFFFF), - mtimer->timebase_freq); + timecmp_hi << 32 | (value & 0xFFFFFFFF)); } else { /* timecmp for RV64 */ riscv_aclint_mtimer_write_timecmp(mtimer, RISCV_CPU(cpu), = hartid, - value, mtimer->timebase_= freq); + value); } } else if ((addr & 0x7) =3D=3D 4) { /* timecmp_hi */ uint64_t timecmp_lo =3D env->timecmp; riscv_aclint_mtimer_write_timecmp(mtimer, RISCV_CPU(cpu), hart= id, - value << 32 | (timecmp_lo & 0xFFFFFFFF), - mtimer->timebase_freq); + value << 32 | (timecmp_lo & 0xFFFFFFFF)); } else { qemu_log_mask(LOG_UNIMP, "aclint-mtimer: invalid timecmp write: %08x", (uint32_t)addr); } return; - } else if (addr =3D=3D mtimer->time_base) { - /* time_lo */ - qemu_log_mask(LOG_UNIMP, - "aclint-mtimer: time_lo write not implemented"); - return; - } else if (addr =3D=3D mtimer->time_base + 4) { - /* time_hi */ - qemu_log_mask(LOG_UNIMP, - "aclint-mtimer: time_hi write not implemented"); - return; + } else if (addr =3D=3D mtimer->time_base || addr =3D=3D mtimer->time_b= ase + 4) { + uint64_t rtc_r =3D cpu_riscv_read_rtc_raw(mtimer->timebase_freq); + + if (addr =3D=3D mtimer->time_base) { + if (size =3D=3D 4) { + /* time_lo for RV32/RV64 */ + mtimer->time_delta =3D ((rtc_r & ~0xFFFFFFFFULL) | value) = - rtc_r; + } else { + /* time for RV64 */ + mtimer->time_delta =3D value - rtc_r; + } + } else { + /* time_hi */ + mtimer->time_delta =3D (value << 32 | (rtc_r & 0xFFFFFFFF)) - = rtc_r; + } + + /* Check if timer interrupt is triggered for each hart. */ + for (i =3D 0; i < mtimer->num_harts; i++) { + CPUState *cpu =3D qemu_get_cpu(mtimer->hartid_base + i); + CPURISCVState *env =3D cpu ? cpu->env_ptr : NULL; + if (!env) { + continue; + } + riscv_aclint_mtimer_write_timecmp(mtimer, RISCV_CPU(cpu), + i, env->timecmp); + } } =20 qemu_log_mask(LOG_UNIMP, @@ -309,7 +330,7 @@ DeviceState *riscv_aclint_mtimer_create(hwaddr addr, hw= addr size, continue; } if (provide_rdtime) { - riscv_cpu_set_rdtime_fn(env, cpu_riscv_read_rtc, timebase_freq= ); + riscv_cpu_set_rdtime_fn(env, cpu_riscv_read_rtc, dev); } =20 cb->s =3D RISCV_ACLINT_MTIMER(dev); diff --git a/include/hw/intc/riscv_aclint.h b/include/hw/intc/riscv_aclint.h index 229bd08d25..26d4048687 100644 --- a/include/hw/intc/riscv_aclint.h +++ b/include/hw/intc/riscv_aclint.h @@ -31,6 +31,7 @@ typedef struct RISCVAclintMTimerState { /*< private >*/ SysBusDevice parent_obj; + uint64_t time_delta; =20 /*< public >*/ MemoryRegion mmio; diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 7ecb1387dd..b5e50d6e75 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -268,8 +268,8 @@ struct CPURISCVState { target_ulong mseccfg; =20 /* machine specific rdtime callback */ - uint64_t (*rdtime_fn)(uint32_t); - uint32_t rdtime_fn_arg; + uint64_t (*rdtime_fn)(void *); + void *rdtime_fn_arg; =20 /* machine specific AIA ireg read-modify-write callback */ #define AIA_MAKE_IREG(__isel, __priv, __virt, __vgein, __xlen) \ @@ -468,8 +468,8 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env); int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint64_t interrupts); uint64_t riscv_cpu_update_mip(RISCVCPU *cpu, uint64_t mask, uint64_t value= ); #define BOOL_TO_MASK(x) (-!!(x)) /* helper for riscv_cpu_update_mip value = */ -void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(uint32_t), - uint32_t arg); +void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(void *), + void *arg); void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env, uint32_t priv, int (*rmw_fn)(void *arg, target_ulong reg, diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 05a90b50ea..3626a3a57e 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -628,8 +628,8 @@ uint64_t riscv_cpu_update_mip(RISCVCPU *cpu, uint64_t m= ask, uint64_t value) return old; } =20 -void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(uint32_t), - uint32_t arg) +void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(void *), + void *arg) { env->rdtime_fn =3D fn; env->rdtime_fn_arg =3D arg; --=20 2.31.1