From nobody Tue Dec 16 16:53:52 2025 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 1643998462149750.0214809906716; Fri, 4 Feb 2022 10:14:22 -0800 (PST) Received: from localhost ([::1]:35886 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nG36B-0006v0-Hm for importer@patchew.org; Fri, 04 Feb 2022 13:14:19 -0500 Received: from eggs.gnu.org ([209.51.188.92]:39792) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nG2hM-0005rl-IO for qemu-devel@nongnu.org; Fri, 04 Feb 2022 12:48:40 -0500 Received: from [2a00:1450:4864:20::42c] (port=38713 helo=mail-wr1-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 1nG2hF-0003wu-S3 for qemu-devel@nongnu.org; Fri, 04 Feb 2022 12:48:40 -0500 Received: by mail-wr1-x42c.google.com with SMTP id s10so10150653wra.5 for ; Fri, 04 Feb 2022 09:48:32 -0800 (PST) Received: from localhost.localdomain ([122.167.157.188]) by smtp.gmail.com with ESMTPSA id f13sm11381876wmq.29.2022.02.04.09.48.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 09:48:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5iofbV+eQQr3W3ompIyVSp6/gtSl1Y7cNrfFo9LhVpQ=; b=sgh3zzFQWiZvDebQv+j3AqUSp5+aHxlClf+d5L/4IdWrCznAOH2YK5pOFm4liYDjI6 FVTA+jB4H6Rv75HzfjEOWeFHJClrMh3PfpVKYuNvqiI8zBQvNVwI5Jx8IhfmBO3oq5CT bVJaBy8iGJkLRQ2KbN82H1AMMd7QBcTINVzFZu5KTo37UEed0/22ln9X7Z75tqkVe3Bi cPjwz0WhaNOyh0qoukkeY65A/ZmTe7FXvSuuyE2xHHuPn7d2S7OSBXe8UjBjKNlps8bb zNkVesrnjifsWg2qdo/UlmtqS4u33/MwdtrFIMZe5gWuDtYws0oyhYQIVnvIg7SJ3sUG 4Xcw== 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=5iofbV+eQQr3W3ompIyVSp6/gtSl1Y7cNrfFo9LhVpQ=; b=rN1tJCIk/2JokhNh87WKnAEKG9URDDSIJgKjRkihbfulKUE1B7ckriQHUe1R+/CBqd KLf+zOjfyGp19I7HW4S7DGygr1p0ycY+ZL9sDJKlwQVuFoMvONHVgQ2ch/CeiiY6ovX5 DnZ3JM0brFrtKtzy7CPpHaWDm1eqzbY2JYAgr0F6pYau9ezDTRSAVx9bfVSn0D8YnT2o 5dKYZao2tCeiKPMxyZfP2BQcHqCKxBxb0+rIr9x1MxKfEmWrGwJR7SJNhrinm1wNGkFf 2MmsWsB3YOGoO+qVYskMsKwO2hEw4+fFtsYlcK5cBvscLIYoZ/FX03mV5aFxE3ZCsC0n ezTg== X-Gm-Message-State: AOAM531blYsiiacyCaX09wOZbIzPXkXD+/zFuSGDkyGmyC0E4DmzoqFn cmHmwRmT6Wgf6BldTGS/S1/hGw== X-Google-Smtp-Source: ABdhPJwAGpUNqeYsZ7fv6CYybHL24uBjxGV8ebi5HRQQhtET945imitpEtzV5RRJrlDVQ+yKcUByJg== X-Received: by 2002:a5d:500c:: with SMTP id e12mr3430477wrt.187.1643996911018; Fri, 04 Feb 2022 09:48:31 -0800 (PST) From: Anup Patel To: Peter Maydell , Palmer Dabbelt , Alistair Francis , Sagar Karandikar Subject: [PATCH v9 15/23] target/riscv: Implement AIA IMSIC interface CSRs Date: Fri, 4 Feb 2022 23:16:51 +0530 Message-Id: <20220204174700.534953-16-anup@brainfault.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220204174700.534953-1-anup@brainfault.org> References: <20220204174700.534953-1-anup@brainfault.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450: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: none client-ip=2a00:1450:4864:20::42c; envelope-from=anup@brainfault.org; helo=mail-wr1-x42c.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_NONE=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: qemu-riscv@nongnu.org, Frank Chang , Anup Patel , qemu-devel@nongnu.org, Atish Patra , Bin Meng 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: 1643998464550100001 Content-Type: text/plain; charset="utf-8" From: Anup Patel The AIA specification defines IMSIC interface CSRs for easy access to the per-HART IMSIC registers without using indirect xiselect and xireg CSRs. This patch implements the AIA IMSIC interface CSRs. Signed-off-by: Anup Patel Signed-off-by: Anup Patel Reviewed-by: Frank Chang --- target/riscv/csr.c | 203 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index f310bbdd8d..b320344a8f 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -942,6 +942,16 @@ static int aia_xlate_vs_csrno(CPURISCVState *env, int = csrno) return CSR_VSISELECT; case CSR_SIREG: return CSR_VSIREG; + case CSR_SSETEIPNUM: + return CSR_VSSETEIPNUM; + case CSR_SCLREIPNUM: + return CSR_VSCLREIPNUM; + case CSR_SSETEIENUM: + return CSR_VSSETEIENUM; + case CSR_SCLREIENUM: + return CSR_VSCLREIENUM; + case CSR_STOPEI: + return CSR_VSTOPEI; default: return csrno; }; @@ -1094,6 +1104,178 @@ done: return RISCV_EXCP_NONE; } =20 +static int rmw_xsetclreinum(CPURISCVState *env, int csrno, target_ulong *v= al, + target_ulong new_val, target_ulong wr_mask) +{ + int ret =3D -EINVAL; + bool set, pend, virt; + target_ulong priv, isel, vgein, xlen, nval, wmask; + + /* Translate CSR number for VS-mode */ + csrno =3D aia_xlate_vs_csrno(env, csrno); + + /* Decode register details from CSR number */ + virt =3D set =3D pend =3D false; + switch (csrno) { + case CSR_MSETEIPNUM: + priv =3D PRV_M; + set =3D true; + pend =3D true; + break; + case CSR_MCLREIPNUM: + priv =3D PRV_M; + pend =3D true; + break; + case CSR_MSETEIENUM: + priv =3D PRV_M; + set =3D true; + break; + case CSR_MCLREIENUM: + priv =3D PRV_M; + break; + case CSR_SSETEIPNUM: + priv =3D PRV_S; + set =3D true; + pend =3D true; + break; + case CSR_SCLREIPNUM: + priv =3D PRV_S; + pend =3D true; + break; + case CSR_SSETEIENUM: + priv =3D PRV_S; + set =3D true; + break; + case CSR_SCLREIENUM: + priv =3D PRV_S; + break; + case CSR_VSSETEIPNUM: + priv =3D PRV_S; + virt =3D true; + set =3D true; + pend =3D true; + break; + case CSR_VSCLREIPNUM: + priv =3D PRV_S; + virt =3D true; + pend =3D true; + break; + case CSR_VSSETEIENUM: + priv =3D PRV_S; + virt =3D true; + set =3D true; + break; + case CSR_VSCLREIENUM: + priv =3D PRV_S; + virt =3D true; + break; + default: + goto done; + }; + + /* IMSIC CSRs only available when machine implements IMSIC. */ + if (!env->aia_ireg_rmw_fn[priv]) { + goto done; + } + + /* Find the selected guest interrupt file */ + vgein =3D (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0; + + /* Selected guest interrupt file should be valid */ + if (virt && (!vgein || env->geilen < vgein)) { + goto done; + } + + /* Set/Clear CSRs always read zero */ + if (val) { + *val =3D 0; + } + + if (wr_mask) { + /* Get interrupt number */ + new_val &=3D wr_mask; + + /* Find target interrupt pending/enable register */ + xlen =3D riscv_cpu_mxl_bits(env); + isel =3D (new_val / xlen); + isel *=3D (xlen / IMSIC_EIPx_BITS); + isel +=3D (pend) ? ISELECT_IMSIC_EIP0 : ISELECT_IMSIC_EIE0; + + /* Find the interrupt bit to be set/clear */ + wmask =3D ((target_ulong)1) << (new_val % xlen); + nval =3D (set) ? wmask : 0; + + /* Call machine specific IMSIC register emulation */ + ret =3D env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], + AIA_MAKE_IREG(isel, priv, virt, + vgein, xlen), + NULL, nval, wmask); + } else { + ret =3D 0; + } + +done: + if (ret) { + return (riscv_cpu_virt_enabled(env) && virt) ? + RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; + } + return RISCV_EXCP_NONE; +} + +static int rmw_xtopei(CPURISCVState *env, int csrno, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + bool virt; + int ret =3D -EINVAL; + target_ulong priv, vgein; + + /* Translate CSR number for VS-mode */ + csrno =3D aia_xlate_vs_csrno(env, csrno); + + /* Decode register details from CSR number */ + virt =3D false; + switch (csrno) { + case CSR_MTOPEI: + priv =3D PRV_M; + break; + case CSR_STOPEI: + priv =3D PRV_S; + break; + case CSR_VSTOPEI: + priv =3D PRV_S; + virt =3D true; + break; + default: + goto done; + }; + + /* IMSIC CSRs only available when machine implements IMSIC. */ + if (!env->aia_ireg_rmw_fn[priv]) { + goto done; + } + + /* Find the selected guest interrupt file */ + vgein =3D (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0; + + /* Selected guest interrupt file should be valid */ + if (virt && (!vgein || env->geilen < vgein)) { + goto done; + } + + /* Call machine specific IMSIC register emulation for TOPEI */ + ret =3D env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], + AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein, + riscv_cpu_mxl_bits(env)), + val, new_val, wr_mask); + +done: + if (ret) { + return (riscv_cpu_virt_enabled(env) && virt) ? + RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; + } + return RISCV_EXCP_NONE; +} + static RISCVException read_mtvec(CPURISCVState *env, int csrno, target_ulong *val) { @@ -2930,6 +3112,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { /* Machine-Level Interrupts (AIA) */ [CSR_MTOPI] =3D { "mtopi", aia_any, read_mtopi }, =20 + /* Machine-Level IMSIC Interface (AIA) */ + [CSR_MSETEIPNUM] =3D { "mseteipnum", aia_any, NULL, NULL, rmw_xsetclre= inum }, + [CSR_MCLREIPNUM] =3D { "mclreipnum", aia_any, NULL, NULL, rmw_xsetclre= inum }, + [CSR_MSETEIENUM] =3D { "mseteienum", aia_any, NULL, NULL, rmw_xsetclre= inum }, + [CSR_MCLREIENUM] =3D { "mclreienum", aia_any, NULL, NULL, rmw_xsetclre= inum }, + [CSR_MTOPEI] =3D { "mtopei", aia_any, NULL, NULL, rmw_xtopei }, + /* Virtual Interrupts for Supervisor Level (AIA) */ [CSR_MVIEN] =3D { "mvien", aia_any, read_zero, write_ignore }, [CSR_MVIP] =3D { "mvip", aia_any, read_zero, write_ignore }, @@ -2966,6 +3155,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { /* Supervisor-Level Interrupts (AIA) */ [CSR_STOPI] =3D { "stopi", aia_smode, read_stopi }, =20 + /* Supervisor-Level IMSIC Interface (AIA) */ + [CSR_SSETEIPNUM] =3D { "sseteipnum", aia_smode, NULL, NULL, rmw_xsetcl= reinum }, + [CSR_SCLREIPNUM] =3D { "sclreipnum", aia_smode, NULL, NULL, rmw_xsetcl= reinum }, + [CSR_SSETEIENUM] =3D { "sseteienum", aia_smode, NULL, NULL, rmw_xsetcl= reinum }, + [CSR_SCLREIENUM] =3D { "sclreienum", aia_smode, NULL, NULL, rmw_xsetcl= reinum }, + [CSR_STOPEI] =3D { "stopei", aia_smode, NULL, NULL, rmw_xtopei= }, + /* Supervisor-Level High-Half CSRs (AIA) */ [CSR_SIEH] =3D { "sieh", aia_smode32, NULL, NULL, rmw_sieh }, [CSR_SIPH] =3D { "siph", aia_smode32, NULL, NULL, rmw_siph }, @@ -3011,6 +3207,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { /* VS-Level Interrupts (H-extension with AIA) */ [CSR_VSTOPI] =3D { "vstopi", aia_hmode, read_vstopi }, =20 + /* VS-Level IMSIC Interface (H-extension with AIA) */ + [CSR_VSSETEIPNUM] =3D { "vsseteipnum", aia_hmode, NULL, NULL, rmw_xset= clreinum }, + [CSR_VSCLREIPNUM] =3D { "vsclreipnum", aia_hmode, NULL, NULL, rmw_xset= clreinum }, + [CSR_VSSETEIENUM] =3D { "vsseteienum", aia_hmode, NULL, NULL, rmw_xset= clreinum }, + [CSR_VSCLREIENUM] =3D { "vsclreienum", aia_hmode, NULL, NULL, rmw_xset= clreinum }, + [CSR_VSTOPEI] =3D { "vstopei", aia_hmode, NULL, NULL, rmw_xtop= ei }, + /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ [CSR_HIDELEGH] =3D { "hidelegh", aia_hmode32, NULL, NULL, rmw_hi= delegh }, [CSR_HVIENH] =3D { "hvienh", aia_hmode32, read_zero, write_i= gnore }, --=20 2.25.1