From nobody Fri Apr 4 03:49:06 2025 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=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1742412201; cv=none; d=zohomail.com; s=zohoarc; b=ibhIn3HP8wAz642E+lmAjYklVTesWF8rMqSiAOn6aMHQv4bWZN9GGnr5K9U6YHDyTfgo0vQTqH9zNAnMkhB/NExEfzVqBxcKQEtdyfm0A00MM8TdUZng6WHGI3FKvTnuVlMS1ywTZTXiRZm9cW/0JlkmuaB7TsiE9iHF/RAGuz8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1742412201; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kKVHcadJ/wQJ8iIOiIdf/pZi6zcVqYjt3QxMjbunVFA=; b=YiEorq8mc9mqzwmZ3KSPzc30Fmv0qJyOvULp4W+qjcDVr2KLtzO3SHrI676wOcluUhTv36qmcZ8X0bTpRX7oZbjzzBJ1goqPJspwblHLJI1Kc3J/HYnfaTfuRPEoaqaX17jaNuoq3oiCJdeB4VrONvzheRlR3AX+nY+UX7DG2DE= 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=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1742412201584565.795205372912; Wed, 19 Mar 2025 12:23:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tuyzm-0007U6-Mo; Wed, 19 Mar 2025 15:22:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tuyzk-0007P7-Cb for qemu-devel@nongnu.org; Wed, 19 Mar 2025 15:22:28 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tuyzd-0002D8-AM for qemu-devel@nongnu.org; Wed, 19 Mar 2025 15:22:28 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-22548a28d0cso46825095ad.3 for ; Wed, 19 Mar 2025 12:22:20 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-af56ea947a4sm11432175a12.70.2025.03.19.12.22.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Mar 2025 12:22:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1742412139; x=1743016939; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=kKVHcadJ/wQJ8iIOiIdf/pZi6zcVqYjt3QxMjbunVFA=; b=ezAgp1hCqfrYJ0wMBB/9kg4fBSToBXNaM4NSWRw4iR2J8Lx00Aqkpxr29MPuVZ+Top fELM/Ojr5euyNxmmm0shNmPBItnkpIpUSMltPTrI8kd5IhmeNprDo4mE5ozbn5qIPl6W xPQgPTtG5aKqJwCWrHknIZy0pCw74capAXcq+90arKh2BAXLek4YCQ6AYCYJGvNDqg53 P6MCVFp1sq7z+ohcbwR5HfDZSqSejbglvLApc1TEEnzFrUrv3RObrf3X+jzmry53ieoz eToWLvt3/JqcVTaBFJOvCav0eqfsbxNgsqhCbSO3siakW6dQBB/Saq/tMiZ+kKxc/PXb frAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742412139; x=1743016939; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kKVHcadJ/wQJ8iIOiIdf/pZi6zcVqYjt3QxMjbunVFA=; b=aiAFMyeVPPM/+d0zX1BanjLJkqFOeImWOW+iIHVTreO0ng0MSQn4Y7Y1PGwALk1XVi 95Ifd0lX5OHYZ9Bs5g8oEoVPyOuo9wDRjW0JUq/0Pn7dlPj16O4X7bJoWX6fd4Hcpl6l eP2rkwZk1BTKiDBkzkRiV/tQRyGzwn45uXY2qKyDbFbDCp1opP3/USU7FDnFhJu+hbSC /PAclSryq0L5IA6mjrnGEThcIZ8AS1caiuSMpygfwhlRA1/U0evLPMJOHDYbEdhEqTYM ww5KxvmjhE0uBvCvVxrpPfeBuZPaqOAD1ey9+tRsqYnGDkvjIW6GKZs/Jwo5NDr949j+ YMeg== X-Gm-Message-State: AOJu0Ywf3lVhWQ8r4Lsl7aZXAnZDQnCdEEXme+l4JWdJ9Ejd8ERd3CtX gCbHBWIc9u1AuKvo4tEDF2OVZUrY6nn2p6WvN1FjdFPz4QRApJAV4bPF7diEUWLXCLDOvOExTPS LOb6JhR//3TJg90Zar6iYb2paljUQmRJKd56inhY8+fYojHKizlnP1A/RV7s7qzOaC9JEHvZAz+ WRq3fQwKM27qbZwxdT/qQ1EhTrxS1zM3/FlQ== X-Gm-Gg: ASbGnctTZm4GQDWcaxEXIFaSeu6BReQ2kiQShJkTxWebI4+0y9snPCVE3L5cfF3O2fr mlGNCMv3hurE4gnPOhTSj5ZTNS1Lu4NoYr69uhiBdAD/Fqczyys0iot1s6W/XxnTV07dnMUKKBj vN/ulllwUUS9iEUKG0lDBy763TZIPn5IQ5nt8l/Zz9NfgcfH1KJZD/J8ls2OpVaCwP7CbEqhfat bnQGdGjDDQmuzWabt8OACF94+wN6l0lXQC++cx6vdYwiVa71DrlKh/PfmdMxM/Lhps/vshmN5qt MXXhykGhUlCJmUG2nZpFniSkBhmlnj7oVPSv2U/dDjXIgGiq7j4hkdsnOCSK4Z80vwk= X-Google-Smtp-Source: AGHT+IE+s0ns02b5SoF9eHzgQxIiDo+b24T8XSNbzWahmXzOfeu8JrjHz1xg7U2xiPkEYxI63QKe7g== X-Received: by 2002:a05:6300:614a:b0:1f5:7d57:830f with SMTP id adf61e73a8af0-1fd133f00cemr839571637.33.1742412138660; Wed, 19 Mar 2025 12:22:18 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Jim Shu Subject: [PATCH 4/4] target/riscv: Enable/Disable S/VS-mode Timer when STCE bit is changed Date: Thu, 20 Mar 2025 03:21:53 +0800 Message-Id: <20250319192153.28549-5-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250319192153.28549-1-jim.shu@sifive.com> References: <20250319192153.28549-1-jim.shu@sifive.com> 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::632; envelope-from=jim.shu@sifive.com; helo=mail-pl1-x632.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1742412202023019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Updating STCE will enable/disable SSTC in S-mode or/and VS-mode, so we also need to update S/VS-mode Timer and S/VSTIP bits in $mip CSR. Signed-off-by: Jim Shu --- target/riscv/csr.c | 44 ++++++++++++++++++++++++++++++++ target/riscv/time_helper.c | 51 ++++++++++++++++++++++++++++++++++++++ target/riscv/time_helper.h | 1 + 3 files changed, 96 insertions(+) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index ba026dfc8e..c954e49cae 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -3156,6 +3156,7 @@ static RISCVException write_menvcfg(CPURISCVState *en= v, int csrno, const RISCVCPUConfig *cfg =3D riscv_cpu_cfg(env); uint64_t mask =3D MENVCFG_FIOM | MENVCFG_CBIE | MENVCFG_CBCFE | MENVCFG_CBZE | MENVCFG_CDE; + bool stce_changed =3D false; =20 if (riscv_cpu_mxl(env) =3D=3D MXL_RV64) { mask |=3D (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) | @@ -3181,10 +3182,19 @@ static RISCVException write_menvcfg(CPURISCVState *= env, int csrno, if ((val & MENVCFG_DTE) =3D=3D 0) { env->mstatus &=3D ~MSTATUS_SDT; } + + if (cfg->ext_sstc && + ((env->menvcfg & MENVCFG_STCE) !=3D (val & MENVCFG_STCE))) { + stce_changed =3D true; + } } env->menvcfg =3D (env->menvcfg & ~mask) | (val & mask); write_henvcfg(env, CSR_HENVCFG, env->henvcfg); =20 + if (stce_changed) { + riscv_timer_stce_changed(env, true, !!(val & MENVCFG_STCE)); + } + return RISCV_EXCP_NONE; } =20 @@ -3207,6 +3217,12 @@ static RISCVException write_menvcfgh(CPURISCVState *= env, int csrno, (cfg->ext_smcdeleg ? MENVCFG_CDE : 0) | (cfg->ext_ssdbltrp ? MENVCFG_DTE : 0); uint64_t valh =3D (uint64_t)val << 32; + bool stce_changed =3D false; + + if (cfg->ext_sstc && + ((env->menvcfg & MENVCFG_STCE) !=3D (valh & MENVCFG_STCE))) { + stce_changed =3D true; + } =20 if ((valh & MENVCFG_DTE) =3D=3D 0) { env->mstatus &=3D ~MSTATUS_SDT; @@ -3215,6 +3231,10 @@ static RISCVException write_menvcfgh(CPURISCVState *= env, int csrno, env->menvcfg =3D (env->menvcfg & ~mask) | (valh & mask); write_henvcfgh(env, CSR_HENVCFGH, env->henvcfg >> 32); =20 + if (stce_changed) { + riscv_timer_stce_changed(env, true, !!(valh & MENVCFG_STCE)); + } + return RISCV_EXCP_NONE; } =20 @@ -3292,8 +3312,10 @@ static RISCVException read_henvcfg(CPURISCVState *en= v, int csrno, static RISCVException write_henvcfg(CPURISCVState *env, int csrno, target_ulong val) { + const RISCVCPUConfig *cfg =3D riscv_cpu_cfg(env); uint64_t mask =3D HENVCFG_FIOM | HENVCFG_CBIE | HENVCFG_CBCFE | HENVCF= G_CBZE; RISCVException ret; + bool stce_changed =3D false; =20 ret =3D smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG); if (ret !=3D RISCV_EXCP_NONE) { @@ -3319,6 +3341,11 @@ static RISCVException write_henvcfg(CPURISCVState *e= nv, int csrno, get_field(val, HENVCFG_PMM) !=3D PMM_FIELD_RESERVED) { mask |=3D HENVCFG_PMM; } + + if (cfg->ext_sstc && + ((env->henvcfg & HENVCFG_STCE) !=3D (val & HENVCFG_STCE))) { + stce_changed =3D true; + } } =20 env->henvcfg =3D val & mask; @@ -3326,6 +3353,10 @@ static RISCVException write_henvcfg(CPURISCVState *e= nv, int csrno, env->vsstatus &=3D ~MSTATUS_SDT; } =20 + if (stce_changed) { + riscv_timer_stce_changed(env, false, !!(val & HENVCFG_STCE)); + } + return RISCV_EXCP_NONE; } =20 @@ -3347,19 +3378,32 @@ static RISCVException read_henvcfgh(CPURISCVState *= env, int csrno, static RISCVException write_henvcfgh(CPURISCVState *env, int csrno, target_ulong val) { + const RISCVCPUConfig *cfg =3D riscv_cpu_cfg(env); uint64_t mask =3D env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE | HENVCFG_DTE); uint64_t valh =3D (uint64_t)val << 32; RISCVException ret; + bool stce_changed =3D false; =20 ret =3D smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG); if (ret !=3D RISCV_EXCP_NONE) { return ret; } + + if (cfg->ext_sstc && + ((env->henvcfg & HENVCFG_STCE) !=3D (valh & HENVCFG_STCE))) { + stce_changed =3D true; + } + env->henvcfg =3D (env->henvcfg & 0xFFFFFFFF) | (valh & mask); if ((env->henvcfg & HENVCFG_DTE) =3D=3D 0) { env->vsstatus &=3D ~MSTATUS_SDT; } + + if (stce_changed) { + riscv_timer_stce_changed(env, false, !!(val & HENVCFG_STCE)); + } + return RISCV_EXCP_NONE; } =20 diff --git a/target/riscv/time_helper.c b/target/riscv/time_helper.c index aebf0798d0..c648c9fac7 100644 --- a/target/riscv/time_helper.c +++ b/target/riscv/time_helper.c @@ -140,6 +140,57 @@ void riscv_timer_write_timecmp(CPURISCVState *env, QEM= UTimer *timer, timer_mod(timer, next); } =20 +/* + * When disabling xenvcfg.STCE, the S/VS Timer may be disabled at the same= time. + * It is safe to call this function regardless of whether the timer has be= en + * deleted or not. timer_del() will do nothing if the timer has already + * been deleted. + */ +static void riscv_timer_disable_timecmp(CPURISCVState *env, QEMUTimer *tim= er, + uint32_t timer_irq) +{ + /* Disable S-mode Timer IRQ and HW-based STIP */ + if ((timer_irq =3D=3D MIP_STIP) && !get_field(env->menvcfg, MENVCFG_ST= CE)) { + riscv_cpu_update_mip(env, timer_irq, BOOL_TO_MASK(0)); + timer_del(timer); + return; + } + + /* Disable VS-mode Timer IRQ and HW-based VSTIP */ + if ((timer_irq =3D=3D MIP_VSTIP) && + (!get_field(env->menvcfg, MENVCFG_STCE) || + !get_field(env->henvcfg, HENVCFG_STCE))) { + env->vstime_irq =3D 0; + riscv_cpu_update_mip(env, 0, BOOL_TO_MASK(0)); + timer_del(timer); + return; + } +} + +/* Enable or disable S/VS-mode Timer when xenvcfg.STCE is changed */ +void riscv_timer_stce_changed(CPURISCVState *env, bool is_m_mode, bool ena= ble) +{ + if (is_m_mode) { + /* menvcfg.STCE changes */ + if (enable) { + riscv_timer_write_timecmp(env, env->stimer, env->stimecmp, 0, = MIP_STIP); + riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp, + env->htimedelta, MIP_VSTIP); + } else { + riscv_timer_disable_timecmp(env, env->stimer, MIP_STIP); + riscv_timer_disable_timecmp(env, env->vstimer, MIP_VSTIP); + } + } else { + /* henvcfg.STCE changes */ + if (enable) { + riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp, + env->htimedelta, MIP_VSTIP); + } else { + riscv_timer_disable_timecmp(env, env->vstimer, MIP_VSTIP); + } + } +} + void riscv_timer_init(RISCVCPU *cpu) { CPURISCVState *env; diff --git a/target/riscv/time_helper.h b/target/riscv/time_helper.h index cacd79b80c..af1f634f89 100644 --- a/target/riscv/time_helper.h +++ b/target/riscv/time_helper.h @@ -25,6 +25,7 @@ void riscv_timer_write_timecmp(CPURISCVState *env, QEMUTimer *timer, uint64_t timecmp, uint64_t delta, uint32_t timer_irq); +void riscv_timer_stce_changed(CPURISCVState *env, bool is_m_mode, bool ena= ble); void riscv_timer_init(RISCVCPU *cpu); =20 #endif --=20 2.17.1