From patchwork Tue Jul 23 23:29:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740456 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D49B9C3DA49 for ; Tue, 23 Jul 2024 23:31:44 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOxr-00025V-92; Tue, 23 Jul 2024 19:30:39 -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 1sWOxa-00020J-Op for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:22 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxX-0007nM-Vb for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:22 -0400 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2cdadce1a57so323964a91.2 for ; Tue, 23 Jul 2024 16:30:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777417; x=1722382217; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=lNRGvwJvd6a6G7pngvPVFSLDlElUvvSu62TJByB++KY=; b=S95pfgR6z4BcF3q9Fg8PDl8MQZCuGqvMNJVjcUi4VUx0LVQbTx/24yY6etmWo25Yz7 0K4e7IuJrr5wO05CRghN4cBb+bBsMh4wnOIuv+E6Ba3/71zu0yCswfLyYtlmLcZuccb5 oxRWSoT5j4EZS/iYYDr2tObchdr/i0uFXqfEplklJvwqglQXDJwLFozyuurq777q00ym xuBCxvaWpqDwLuvxcKS+lPSBY9iVNqiLlmM0ef9nRcMGms9GLo3nF+42U7QtQf/nSlHc 4NigQT+Zlv6zIkY9GRksjw89ZzzelbFrXNZdiotrVQz+URKG4gO+C5cLg9UUpNW/XaYP PH9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777417; x=1722382217; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lNRGvwJvd6a6G7pngvPVFSLDlElUvvSu62TJByB++KY=; b=dksnEZFbfGyn1DH/xLof9bwz543xbMi/W2K4P6RYR1olH5HeJzJqrxa0aMSU2T9xTx k8/DRx2W3AWm+rBu0XOQkieRTNDvOHohzZIU4caIosrAwQfZVreHqQs54ZQLwv0NLXXz aWT2ftJYvIudtaN68rrewGHBGI62vLiXVab2gf4209mgsf80JYTe/GHxu0KVYx7cD2gz zuWM67ABlxMv6NFQpFPt+YeAoKtaDCiYyBNQuTeemax5mmo2eP6TSw878aeUlJHJVBQx JW2l2T4s57N63urRAXiEtoMm/u6GFLANDkAvwfvwDuRl0h6RUxd9hhZ4zWMleEVdgd8X prHg== X-Forwarded-Encrypted: i=1; AJvYcCXExXaQaiDbAeBXhjBnkzOVvKL1TotK7WwZMsJoKaVvX0xAYtpwxgjB2TLgc1TbP2MTM0PuPkiLJJP9xtGdF7sjImVrd4s= X-Gm-Message-State: AOJu0YzUtKG+EN2D8Ebey48MN+8iwk6/JVuwG2sgHjfspwzYTb+3XNsc /VpeVY+27SRvDNJ4kaZpWMA6RLS03zlscxS+Pbb4Wdb49Z7cwPMcKq3AykJeT8Y= X-Google-Smtp-Source: AGHT+IF1w8noqVocF3lV+ZJLzFoeKSDfbDj1N65r4YOPVLI1PEnkezhMXL/JyxsvzL5DhFsShYmkyA== X-Received: by 2002:a17:90a:f10a:b0:2c8:f3b4:425 with SMTP id 98e67ed59e1d1-2cdb515ea88mr613216a91.23.1721777417650; Tue, 23 Jul 2024 16:30:17 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:16 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:29:58 -0700 Subject: [PATCH v2 01/13] target/riscv: Add properties for Indirect CSR Access extension MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-1-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Kaiwen Xue This adds the properties for sxcsrind. Definitions of new registers and implementations will come with future patches. Signed-off-by: Atish Patra Signed-off-by: Kaiwen Xue --- target/riscv/cpu.c | 2 ++ target/riscv/cpu_cfg.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index a90808a3bace..ebc19090b40d 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -183,12 +183,14 @@ const RISCVIsaExtData isa_edata_arr[] = { ISA_EXT_DATA_ENTRY(zhinxmin, PRIV_VERSION_1_12_0, ext_zhinxmin), ISA_EXT_DATA_ENTRY(smaia, PRIV_VERSION_1_12_0, ext_smaia), ISA_EXT_DATA_ENTRY(smcntrpmf, PRIV_VERSION_1_12_0, ext_smcntrpmf), + ISA_EXT_DATA_ENTRY(smcsrind, PRIV_VERSION_1_12_0, ext_smcsrind), ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp), ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen), ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia), ISA_EXT_DATA_ENTRY(ssccptr, PRIV_VERSION_1_11_0, has_priv_1_11), ISA_EXT_DATA_ENTRY(sscofpmf, PRIV_VERSION_1_12_0, ext_sscofpmf), ISA_EXT_DATA_ENTRY(sscounterenw, PRIV_VERSION_1_12_0, has_priv_1_12), + ISA_EXT_DATA_ENTRY(sscsrind, PRIV_VERSION_1_12_0, ext_sscsrind), ISA_EXT_DATA_ENTRY(sstc, PRIV_VERSION_1_12_0, ext_sstc), ISA_EXT_DATA_ENTRY(sstvala, PRIV_VERSION_1_12_0, has_priv_1_12), ISA_EXT_DATA_ENTRY(sstvecd, PRIV_VERSION_1_12_0, has_priv_1_12), diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h index 8b272fb826ef..b8a5174bc871 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -77,6 +77,8 @@ struct RISCVCPUConfig { bool ext_smstateen; bool ext_sstc; bool ext_smcntrpmf; + bool ext_smcsrind; + bool ext_sscsrind; bool ext_svadu; bool ext_svinval; bool ext_svnapot; From patchwork Tue Jul 23 23:29:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740458 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 84D64C3DA63 for ; Tue, 23 Jul 2024 23:32:20 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOxz-0002Sw-Um; Tue, 23 Jul 2024 19:30:48 -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 1sWOxb-00021N-SH for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:30 -0400 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxY-0007nj-Rd for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:23 -0400 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-2cb55418470so3136273a91.1 for ; Tue, 23 Jul 2024 16:30:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777419; x=1722382219; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=ZgJi5SQCSbMcc3JoLYiq60wkxC/rvzt57z6OyFhCuXg=; b=KACedaErwKC0sgOzESimiqM0prM8urh4rQEMH8LtUsRbKZySjvysEq/1FbXIgiMbq3 Ut57Zv4Itvi1cZSFM3d3VSKLbbbSbnFRzGyQRiYK/XHRrEdE64/tpwPjY+GvPD7CMrxs S1HFediU6JgEDrtnzU94askZ0C4FmlLKx2lo6eJdLz1GvoDnDv0iCXyfw11kQA76yFOw +G0IY2EkMGUfEMh/6nTCQ+aIZz52peqJNm78RaXI8Feq1Uj89iTJnTaY+dBSp91MUWoG wjH0BdiayIMHqNV2w6GIQHU+NYwVMqoM8D23jUAvYkhDrmoN+W1Z77HmiYNwOq/Ymzwi xkRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777419; x=1722382219; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZgJi5SQCSbMcc3JoLYiq60wkxC/rvzt57z6OyFhCuXg=; b=jEhTAP1+oGcpU5W9OX2Svf/JnlDtI4eMcXaegoFXZ7gWBHDsJ9XN/Qn+cwsMvzYe5o Xz6VqBtOf61VtvEYZ1wGeZybDdqQj48CRWNt14VaGvmu/seldRazBXEsfVPnZfgH1mA2 LQKngm9dtaqZC6p5wObno73MhJpBoCUKA3r+inj0hCt8dIKxLEe8wNL8r0sptkuyHnsp sylAAp3giemEJFLD6S0VyUdZ1UrlDFzg1tbZHEJVRjRXgZgTUU3pbw8OUQAwS8DP7zTF HBsO5zr3HksEK+a1NzYBDCV6T/1fVQrJxy5umWxuODDwdtwxU47c8uAcKlgrmMCZWUlZ kjTw== X-Forwarded-Encrypted: i=1; AJvYcCXKsHpCzfjVAizpipp/vgFQBrJS7bamSlxQPPLgQcbA1ADt4I7fKDpnVjB8cxuLpZUR9abNmbMJcytEbbiVLDXR05yXXXM= X-Gm-Message-State: AOJu0YyW3XYzAcXft5MaxzOkQfcgpa1wGWruHk3Gajq+Pw3ys7i9YidE RXqofnj0eQeMl27NFqYThjnN6kffESkAX44FFj388akdsfOqSpMUeAnxaVNfQFo= X-Google-Smtp-Source: AGHT+IH9mMKm7G+VL4TF6/KVmF34yzCRT45M12U+7GwjiBVgJ5/6n8D+tV0UJaSd5ggsmRVTUMpitw== X-Received: by 2002:a17:90b:1090:b0:2c9:5f1e:1a62 with SMTP id 98e67ed59e1d1-2cdaf779b22mr1373930a91.36.1721777419203; Tue, 23 Jul 2024 16:30:19 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:18 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:29:59 -0700 Subject: [PATCH v2 02/13] target/riscv: Decouple AIA processing from xiselect and xireg MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-2-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::1029; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x1029.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Kaiwen Xue Since xiselect and xireg also will be of use in sxcsrind, AIA should have its own separated interface when those CSRs are accessed. Signed-off-by: Atish Patra Signed-off-by: Kaiwen Xue Reviewed-by: Alistair Francis --- target/riscv/csr.c | 165 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 139 insertions(+), 26 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index ea3560342c4f..58be8bc3cc8c 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -29,6 +29,7 @@ #include "sysemu/cpu-timers.h" #include "qemu/guest-random.h" #include "qapi/error.h" +#include /* CSR function table public API */ void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops) @@ -286,6 +287,15 @@ static RISCVException aia_any32(CPURISCVState *env, int csrno) return any32(env, csrno); } +static RISCVException csrind_or_aia_any(CPURISCVState *env, int csrno) +{ + if (!riscv_cpu_cfg(env)->ext_smaia && !riscv_cpu_cfg(env)->ext_smcsrind) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return any(env, csrno); +} + static RISCVException smode(CPURISCVState *env, int csrno) { if (riscv_has_ext(env, RVS)) { @@ -322,6 +332,30 @@ static RISCVException aia_smode32(CPURISCVState *env, int csrno) return smode32(env, csrno); } +static bool csrind_extensions_present(CPURISCVState *env) +{ + return riscv_cpu_cfg(env)->ext_smcsrind || riscv_cpu_cfg(env)->ext_sscsrind; +} + +static bool aia_extensions_present(CPURISCVState *env) +{ + return riscv_cpu_cfg(env)->ext_smaia || riscv_cpu_cfg(env)->ext_ssaia; +} + +static bool csrind_or_aia_extensions_present(CPURISCVState *env) +{ + return csrind_extensions_present(env) || aia_extensions_present(env); +} + +static RISCVException csrind_or_aia_smode(CPURISCVState *env, int csrno) +{ + if (!csrind_or_aia_extensions_present(env)) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return smode(env, csrno); +} + static RISCVException hmode(CPURISCVState *env, int csrno) { if (riscv_has_ext(env, RVH)) { @@ -341,6 +375,15 @@ static RISCVException hmode32(CPURISCVState *env, int csrno) } +static RISCVException csrind_or_aia_hmode(CPURISCVState *env, int csrno) +{ + if (!csrind_or_aia_extensions_present(env)) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return hmode(env, csrno); +} + static RISCVException umode(CPURISCVState *env, int csrno) { if (riscv_has_ext(env, RVU)) { @@ -1928,6 +1971,22 @@ static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno) }; } +static int csrind_xlate_vs_csrno(CPURISCVState *env, int csrno) +{ + if (!env->virt_enabled) { + return csrno; + } + + switch (csrno) { + case CSR_SISELECT: + return CSR_VSISELECT; + case CSR_SIREG: + return CSR_VSIREG; + default: + return csrno; + }; +} + static RISCVException rmw_xiselect(CPURISCVState *env, int csrno, target_ulong *val, target_ulong new_val, target_ulong wr_mask) @@ -1935,7 +1994,7 @@ static RISCVException rmw_xiselect(CPURISCVState *env, int csrno, target_ulong *iselect; /* Translate CSR number for VS-mode */ - csrno = aia_xlate_vs_csrno(env, csrno); + csrno = csrind_xlate_vs_csrno(env, csrno); /* Find the iselect CSR based on CSR number */ switch (csrno) { @@ -1964,6 +2023,12 @@ static RISCVException rmw_xiselect(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } +static bool xiselect_aia_range(target_ulong isel) +{ + return (ISELECT_IPRIO0 <= isel && isel <= ISELECT_IPRIO15) || + (ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST); +} + static int rmw_iprio(target_ulong xlen, target_ulong iselect, uint8_t *iprio, target_ulong *val, target_ulong new_val, @@ -2009,45 +2074,44 @@ static int rmw_iprio(target_ulong xlen, return 0; } -static RISCVException rmw_xireg(CPURISCVState *env, int csrno, - target_ulong *val, target_ulong new_val, - target_ulong wr_mask) +static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno, + target_ulong isel, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) { - bool virt, isel_reserved; - uint8_t *iprio; + bool virt = false, isel_reserved = false; int ret = -EINVAL; - target_ulong priv, isel, vgein; - - /* Translate CSR number for VS-mode */ - csrno = aia_xlate_vs_csrno(env, csrno); + uint8_t *iprio; + target_ulong priv, vgein; - /* Decode register details from CSR number */ - virt = false; - isel_reserved = false; + /* VS-mode CSR number passed in has already been translated */ switch (csrno) { case CSR_MIREG: + if (!riscv_cpu_cfg(env)->ext_smaia) { + goto done; + } iprio = env->miprio; - isel = env->miselect; priv = PRV_M; break; case CSR_SIREG: - if (env->priv == PRV_S && env->mvien & MIP_SEIP && + if (!riscv_cpu_cfg(env)->ext_ssaia || + (env->priv == PRV_S && env->mvien & MIP_SEIP && env->siselect >= ISELECT_IMSIC_EIDELIVERY && - env->siselect <= ISELECT_IMSIC_EIE63) { + env->siselect <= ISELECT_IMSIC_EIE63)) { goto done; } iprio = env->siprio; - isel = env->siselect; priv = PRV_S; break; case CSR_VSIREG: + if (!riscv_cpu_cfg(env)->ext_ssaia) { + goto done; + } iprio = env->hviprio; - isel = env->vsiselect; priv = PRV_S; virt = true; break; default: - goto done; + goto done; }; /* Find the selected guest interrupt file */ @@ -2078,10 +2142,54 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, } done: + /* + * If AIA is not enabled, illegal instruction exception is always + * returned regardless of whether we are in VS-mode or not + */ if (ret) { return (env->virt_enabled && virt && !isel_reserved) ? RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; } + + return RISCV_EXCP_NONE; +} + +static RISCVException rmw_xireg(CPURISCVState *env, int csrno, + target_ulong *val, target_ulong new_val, + target_ulong wr_mask) +{ + bool virt = false; + int ret = -EINVAL; + target_ulong isel; + + /* Translate CSR number for VS-mode */ + csrno = csrind_xlate_vs_csrno(env, csrno); + + /* Decode register details from CSR number */ + switch (csrno) { + case CSR_MIREG: + isel = env->miselect; + break; + case CSR_SIREG: + isel = env->siselect; + break; + case CSR_VSIREG: + isel = env->vsiselect; + virt = true; + break; + default: + goto done; + }; + + if (xiselect_aia_range(isel)) { + return rmw_xireg_aia(env, csrno, isel, val, new_val, wr_mask); + } + +done: + if (ret) { + return (env->virt_enabled && virt) ? + RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; + } return RISCV_EXCP_NONE; } @@ -4981,8 +5089,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip }, /* Machine-Level Window to Indirectly Accessed Registers (AIA) */ - [CSR_MISELECT] = { "miselect", aia_any, NULL, NULL, rmw_xiselect }, - [CSR_MIREG] = { "mireg", aia_any, NULL, NULL, rmw_xireg }, + [CSR_MISELECT] = { "miselect", csrind_or_aia_any, NULL, NULL, + rmw_xiselect }, + [CSR_MIREG] = { "mireg", csrind_or_aia_any, NULL, NULL, + rmw_xireg }, /* Machine-Level Interrupts (AIA) */ [CSR_MTOPEI] = { "mtopei", aia_any, NULL, NULL, rmw_xtopei }, @@ -5100,8 +5210,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_SATP] = { "satp", satp, read_satp, write_satp }, /* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */ - [CSR_SISELECT] = { "siselect", aia_smode, NULL, NULL, rmw_xiselect }, - [CSR_SIREG] = { "sireg", aia_smode, NULL, NULL, rmw_xireg }, + [CSR_SISELECT] = { "siselect", csrind_or_aia_smode, NULL, NULL, + rmw_xiselect }, + [CSR_SIREG] = { "sireg", csrind_or_aia_smode, NULL, NULL, + rmw_xireg }, /* Supervisor-Level Interrupts (AIA) */ [CSR_STOPEI] = { "stopei", aia_smode, NULL, NULL, rmw_xtopei }, @@ -5180,9 +5292,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* * VS-Level Window to Indirectly Accessed Registers (H-extension with AIA) */ - [CSR_VSISELECT] = { "vsiselect", aia_hmode, NULL, NULL, - rmw_xiselect }, - [CSR_VSIREG] = { "vsireg", aia_hmode, NULL, NULL, rmw_xireg }, + [CSR_VSISELECT] = { "vsiselect", csrind_or_aia_hmode, NULL, NULL, + rmw_xiselect }, + [CSR_VSIREG] = { "vsireg", csrind_or_aia_hmode, NULL, NULL, + rmw_xireg }, /* VS-Level Interrupts (H-extension with AIA) */ [CSR_VSTOPEI] = { "vstopei", aia_hmode, NULL, NULL, rmw_xtopei }, From patchwork Tue Jul 23 23:30:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740466 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 96664C3DA7F for ; Tue, 23 Jul 2024 23:32:53 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOy1-0002c1-VP; Tue, 23 Jul 2024 19:30:50 -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 1sWOxf-00021X-St for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:30 -0400 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxd-0007oo-2q for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:27 -0400 Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-6c5bcb8e8edso808430a12.2 for ; Tue, 23 Jul 2024 16:30:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777421; x=1722382221; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=T8to1DWCZWdUlgFa0/TKXbDihDuXgZx3Iusd/IEEBEY=; b=f1YEPCdzHAdC2jcR/ndAcL2LAuImsYe76rgARHFZhDMm1KR9NFf8ab9c7pLjPe0t3R VE2fyEcZwfF/0Z/EmeXnKjY8NIzd16KC12Rn/p0pkMm7Dl5y5oqxNAtUh+3Xy7G81FIm yaxORxvrvQvEdg4E1qnPIwEFlRm3sC/IbZOk5yKnPZ6G465lU2/pjYG8SCWlHCJSjqTu 5XIIttnrYuGVpqeSYoTgjneoJAS9prwKl7n5Xg8qTgPmadBcAiV5S2tWKr6yeZnowM9G ZldIOK+67IunvzlTlCsTWNtSpbr4SvTBWQjw10w8Wi+J77QFjvmQ8OqJO29fIUrF56dG /DCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777421; x=1722382221; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T8to1DWCZWdUlgFa0/TKXbDihDuXgZx3Iusd/IEEBEY=; b=NlIQZxjMTguJOB0hEa8sOMZxxcqZDNZ9KhzDy9yxFqcW3SXhfKxNi48TA/3rQ1w7Mn 6c3SLIfJqyao92CCmHE0fJa71eaDrGaDucjeFCxk89kIleBtSgQMDufvhIiTFADEPcSO 0fHo7p1GWscVqjk6NmbgG5LGWas8js9W0Di9hEHDEpSiGJzEsXQD5T3FDmPpg5rJV6GW r0gvvmRyegowptReAI7xTg2n8LczaTquNM5GoOtzV5JdGAvGB6HC8k9MWZz9FZOlM8NI 8UbP1atihR+WPexvHmCK3wsKw81SvtvS4XfJMrsN7/QGvtF5G/AwY3Nte0j8pmphXPCw 7bGg== X-Forwarded-Encrypted: i=1; AJvYcCUIWB7gAkUKBCvCIMrY/wNfZU+/79uratzm4PZq38GAkRSuloVwKQzNsK6OteA7BZE5/PGGyFRPWQUyidiuY+HHX70ZOAE= X-Gm-Message-State: AOJu0Yx0/2jnQHWX5QSdv9F4XnSS0+Fs0pmFvc16uPuZQMo/Gixj4NPM ivtCHQW1GjKyMLbou/S0LuzGCfPkq5vf9zi4MjZJksk4mkvdIxlmtfJbs5XnR/8= X-Google-Smtp-Source: AGHT+IHiZTDwswp7V8b+sTiNlXPvLCU6pmAwLgdWgRg3oyAWI9BstXVanapejSPzbNS1d6+6w+piKw== X-Received: by 2002:a17:90a:9308:b0:2cd:3e23:fc85 with SMTP id 98e67ed59e1d1-2cdae3166f8mr1527161a91.13.1721777420850; Tue, 23 Jul 2024 16:30:20 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:20 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:00 -0700 Subject: [PATCH v2 03/13] target/riscv: Enable S*stateen bits for AIA MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-3-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::530; envelope-from=atishp@rivosinc.com; helo=mail-pg1-x530.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org As per the ratified AIA spec v1.0, three stateen bits control AIA CSR access. Bit 60 controls the indirect CSRs Bit 59 controls the most AIA CSR state Bit 58 controls the IMSIC state such as stopei and vstopei Enable the corresponding bits in [m|h]stateen and enable corresponding checks in the CSR accessor functions. Signed-off-by: Atish Patra Reviewed-by: Alistair Francis --- target/riscv/csr.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 58be8bc3cc8c..18b9ae802b15 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -316,19 +316,42 @@ static RISCVException smode32(CPURISCVState *env, int csrno) static RISCVException aia_smode(CPURISCVState *env, int csrno) { + int ret; + if (!riscv_cpu_cfg(env)->ext_ssaia) { return RISCV_EXCP_ILLEGAL_INST; } + if (csrno == CSR_STOPEI) { + ret = smstateen_acc_ok(env, 0, SMSTATEEN0_IMSIC); + } else { + ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); + } + + if (ret != RISCV_EXCP_NONE) { + return ret; + } + return smode(env, csrno); } static RISCVException aia_smode32(CPURISCVState *env, int csrno) { + int ret; + if (!riscv_cpu_cfg(env)->ext_ssaia) { return RISCV_EXCP_ILLEGAL_INST; } + ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); + if (ret != RISCV_EXCP_NONE) { + return ret; + } + + if (ret != RISCV_EXCP_NONE) { + return ret; + } + return smode32(env, csrno); } @@ -567,15 +590,38 @@ static RISCVException pointer_masking(CPURISCVState *env, int csrno) static RISCVException aia_hmode(CPURISCVState *env, int csrno) { + int ret; + if (!riscv_cpu_cfg(env)->ext_ssaia) { return RISCV_EXCP_ILLEGAL_INST; } - return hmode(env, csrno); + if (csrno == CSR_VSTOPEI) { + ret = smstateen_acc_ok(env, 0, SMSTATEEN0_IMSIC); + } else { + ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); + } + + if (ret != RISCV_EXCP_NONE) { + return ret; + } + + return hmode(env, csrno); } static RISCVException aia_hmode32(CPURISCVState *env, int csrno) { + int ret; + + if (!riscv_cpu_cfg(env)->ext_ssaia) { + return RISCV_EXCP_ILLEGAL_INST; + } + + ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); + if (ret != RISCV_EXCP_NONE) { + return ret; + } + if (!riscv_cpu_cfg(env)->ext_ssaia) { return RISCV_EXCP_ILLEGAL_INST; } @@ -1992,6 +2038,12 @@ static RISCVException rmw_xiselect(CPURISCVState *env, int csrno, target_ulong wr_mask) { target_ulong *iselect; + int ret; + + ret = smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT); + if (ret != RISCV_EXCP_NONE) { + return ret; + } /* Translate CSR number for VS-mode */ csrno = csrind_xlate_vs_csrno(env, csrno); @@ -2162,6 +2214,11 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, int ret = -EINVAL; target_ulong isel; + ret = smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT); + if (ret != RISCV_EXCP_NONE) { + return ret; + } + /* Translate CSR number for VS-mode */ csrno = csrind_xlate_vs_csrno(env, csrno); @@ -2610,6 +2667,22 @@ static RISCVException write_mstateen0(CPURISCVState *env, int csrno, if (env->priv_ver >= PRIV_VERSION_1_13_0) { wr_mask |= SMSTATEEN0_P1P13; } + /* + * TODO: Do we need to check ssaia as well ? Can we enable ssaia without + * smaia ? + */ + if (riscv_cpu_cfg(env)->ext_smaia) { + wr_mask |= SMSTATEEN0_SVSLCT; + } + + /* + * As per the AIA specification, SMSTATEEN0_IMSIC is valid only if IMSIC is + * implemented. However, that information is with MachineState and we can't + * figure that out in csr.c. Just enable if Smaia is available. + */ + if (riscv_cpu_cfg(env)->ext_smaia) { + wr_mask |= (SMSTATEEN0_AIA | SMSTATEEN0_IMSIC); + } return write_mstateen(env, csrno, wr_mask, new_val); } @@ -2691,6 +2764,19 @@ static RISCVException write_hstateen0(CPURISCVState *env, int csrno, wr_mask |= SMSTATEEN0_FCSR; } + if (riscv_cpu_cfg(env)->ext_ssaia) { + wr_mask |= SMSTATEEN0_SVSLCT; + } + + /* + * As per the AIA specification, SMSTATEEN0_IMSIC is valid only if IMSIC is + * implemented. However, that information is with MachineState and we can't + * figure that out in csr.c. Just enable if Ssaia is available. + */ + if (riscv_cpu_cfg(env)->ext_ssaia) { + wr_mask |= (SMSTATEEN0_AIA | SMSTATEEN0_IMSIC); + } + return write_hstateen(env, csrno, wr_mask, new_val); } From patchwork Tue Jul 23 23:30:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740463 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 18264C3DA49 for ; Tue, 23 Jul 2024 23:32:44 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOy0-0002YD-Up; Tue, 23 Jul 2024 19:30:49 -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 1sWOxg-00021Z-6N for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:30 -0400 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxd-0007pf-2x for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:27 -0400 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-2cb566d528aso3858713a91.1 for ; Tue, 23 Jul 2024 16:30:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777422; x=1722382222; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=t3IUQ3lWMCE317aXyY0qnJM5cCIYZlieRf6PY9LMv1g=; b=bDvKR4coaNpXOHnhbYanwCzYujKYJi3IuwQ7R3xVhvU06je1cNF0jxMtRLuggHSMBI +T6Dy4XGmzdB2C0D5LFeqS574VI2VFuxVwhW+qCPRo8fUGhwjW8DDr8lnHH1I8/PgpLR 5t+zdoY+elSmACkFBl4Qs6XIjN61iOoESr6hnFLDl3ZMmFos865yckXFQ+iL/cPWe+E9 RaLTGP9DRbOHbIn4WTFz9KoCkY9wfUZJHePp133vfMLVZJmiRbuguVKAjX8yOCdwdztg xYbFmMwoMSOdR0aNcdhoGOJjFQHM44uk1ywmY9xFw1AzDFujESlcU1NdfHrjbv9mK2Hj W39Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777422; x=1722382222; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t3IUQ3lWMCE317aXyY0qnJM5cCIYZlieRf6PY9LMv1g=; b=ligiuGSDrPhjDmBCUiCPNrAEQ7wk9yWes3WmPPGdy5v8oNBNW7b5rTnsJAuTbb3sRp 6DIhctyIlmJhAUAtLF5sE4QLK1quzrhNg7Hg/NaHtyfZT8NFTd7Mc0ewFpfFWgo0Op8t TY/eOg88Mz09P641J57kz3CvgkKu/a9FMDJn9BOkOrzdjSU3qHgtzeFArlhFp0lfbkFZ NFe6qHodP9ZaSpHwx1hQT0pBOYn2Oz0/33FU9aX97X7P5pvw4Vv4z4ilGPCshLG+ueSW aCK2jLgYecggP8u116/xkOQWrPvRs04nu7qGdoMaGwGtt8i8EU2EW/RXPnXCK/Dza1Hm 7rcA== X-Forwarded-Encrypted: i=1; AJvYcCUEkpJGLIyIT8VAQU1QZZMG9lTlnHMmzVAG5j/oScgbmT7ZoeGtr1mBZKTEAXlFTF9QmiPbHLn/YAsfSXdtgXcy6z8eGus= X-Gm-Message-State: AOJu0Yz6+dYle/Nr3Axf6ixbobxvL1UNPOxfBgEgGQHiW5ZYbSz+7aQr 4lubyzpFYO9/N+kcmZy975mV2RHAFHYeLD15kz2SVB7+9pp2y/gnIQGAN1zyKDc= X-Google-Smtp-Source: AGHT+IE+XH9QdlCHWOL+Xm5HwmCPmNIQ6adqK3WyoKqyB/gmR8pUNJwXfVbScDKbJm6QdfB5gG9vHg== X-Received: by 2002:a17:90b:4d8d:b0:2cb:3748:f5ce with SMTP id 98e67ed59e1d1-2cdae3143a0mr1408416a91.10.1721777422348; Tue, 23 Jul 2024 16:30:22 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:21 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:01 -0700 Subject: [PATCH v2 04/13] target/riscv: Support generic CSR indirect access MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-4-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::102f; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x102f.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Kaiwen Xue This adds the indirect access registers required by sscsrind/smcsrind and the operations on them. Note that xiselect and xireg are used for both AIA and sxcsrind, and the behavior of accessing them depends on whether each extension is enabled and the value stored in xiselect. Co-developed-by: Atish Patra Signed-off-by: Atish Patra Signed-off-by: Kaiwen Xue --- target/riscv/cpu.c | 2 + target/riscv/cpu_bits.h | 28 ++++++++- target/riscv/cpu_cfg.h | 2 + target/riscv/csr.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 175 insertions(+), 6 deletions(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index ebc19090b40d..ac2dce734d80 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -182,12 +182,14 @@ const RISCVIsaExtData isa_edata_arr[] = { ISA_EXT_DATA_ENTRY(zhinx, PRIV_VERSION_1_12_0, ext_zhinx), ISA_EXT_DATA_ENTRY(zhinxmin, PRIV_VERSION_1_12_0, ext_zhinxmin), ISA_EXT_DATA_ENTRY(smaia, PRIV_VERSION_1_12_0, ext_smaia), + ISA_EXT_DATA_ENTRY(smcdeleg, PRIV_VERSION_1_12_0, ext_smcdeleg), ISA_EXT_DATA_ENTRY(smcntrpmf, PRIV_VERSION_1_12_0, ext_smcntrpmf), ISA_EXT_DATA_ENTRY(smcsrind, PRIV_VERSION_1_12_0, ext_smcsrind), ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp), ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen), ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia), ISA_EXT_DATA_ENTRY(ssccptr, PRIV_VERSION_1_11_0, has_priv_1_11), + ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_12_0, ext_ssccfg), ISA_EXT_DATA_ENTRY(sscofpmf, PRIV_VERSION_1_12_0, ext_sscofpmf), ISA_EXT_DATA_ENTRY(sscounterenw, PRIV_VERSION_1_12_0, has_priv_1_12), ISA_EXT_DATA_ENTRY(sscsrind, PRIV_VERSION_1_12_0, ext_sscsrind), diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 32b068f18aa5..2a8b53a6622e 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -170,6 +170,13 @@ #define CSR_MISELECT 0x350 #define CSR_MIREG 0x351 +/* Machine Indirect Register Alias */ +#define CSR_MIREG2 0x352 +#define CSR_MIREG3 0x353 +#define CSR_MIREG4 0x355 +#define CSR_MIREG5 0x356 +#define CSR_MIREG6 0x357 + /* Machine-Level Interrupts (AIA) */ #define CSR_MTOPEI 0x35c #define CSR_MTOPI 0xfb0 @@ -219,6 +226,13 @@ #define CSR_SISELECT 0x150 #define CSR_SIREG 0x151 +/* Supervisor Indirect Register Alias */ +#define CSR_SIREG2 0x152 +#define CSR_SIREG3 0x153 +#define CSR_SIREG4 0x155 +#define CSR_SIREG5 0x156 +#define CSR_SIREG6 0x157 + /* Supervisor-Level Interrupts (AIA) */ #define CSR_STOPEI 0x15c #define CSR_STOPI 0xdb0 @@ -285,6 +299,13 @@ #define CSR_VSISELECT 0x250 #define CSR_VSIREG 0x251 +/* Virtual Supervisor Indirect Alias */ +#define CSR_VSIREG2 0x252 +#define CSR_VSIREG3 0x253 +#define CSR_VSIREG4 0x255 +#define CSR_VSIREG5 0x256 +#define CSR_VSIREG6 0x257 + /* VS-Level Interrupts (H-extension with AIA) */ #define CSR_VSTOPEI 0x25c #define CSR_VSTOPI 0xeb0 @@ -846,10 +867,13 @@ typedef enum RISCVException { #define ISELECT_IMSIC_EIE63 0xff #define ISELECT_IMSIC_FIRST ISELECT_IMSIC_EIDELIVERY #define ISELECT_IMSIC_LAST ISELECT_IMSIC_EIE63 -#define ISELECT_MASK 0x1ff +#define ISELECT_MASK_AIA 0x1ff + +/* MISELECT, SISELECT, and VSISELECT bits (AIA) */ +#define ISELECT_MASK_SXCSRIND 0xfff /* Dummy [M|S|VS]ISELECT value for emulating [M|S|VS]TOPEI CSRs */ -#define ISELECT_IMSIC_TOPEI (ISELECT_MASK + 1) +#define ISELECT_IMSIC_TOPEI (ISELECT_MASK_AIA + 1) /* IMSIC bits (AIA) */ #define IMSIC_TOPEI_IID_SHIFT 16 diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h index b8a5174bc871..b2be3d47c7a3 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -79,6 +79,8 @@ struct RISCVCPUConfig { bool ext_smcntrpmf; bool ext_smcsrind; bool ext_sscsrind; + bool ext_smcdeleg; + bool ext_ssccfg; bool ext_svadu; bool ext_svinval; bool ext_svnapot; diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 18b9ae802b15..4ae3931f0ada 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -287,6 +287,17 @@ static RISCVException aia_any32(CPURISCVState *env, int csrno) return any32(env, csrno); } +static RISCVException csrind_any(CPURISCVState *env, int csrno) +{ + RISCVCPU *cpu = env_archcpu(env); + + if (!cpu->cfg.ext_smcsrind) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return RISCV_EXCP_NONE; +} + static RISCVException csrind_or_aia_any(CPURISCVState *env, int csrno) { if (!riscv_cpu_cfg(env)->ext_smaia && !riscv_cpu_cfg(env)->ext_smcsrind) { @@ -370,6 +381,15 @@ static bool csrind_or_aia_extensions_present(CPURISCVState *env) return csrind_extensions_present(env) || aia_extensions_present(env); } +static RISCVException csrind_smode(CPURISCVState *env, int csrno) +{ + if (!csrind_extensions_present(env)) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return smode(env, csrno); +} + static RISCVException csrind_or_aia_smode(CPURISCVState *env, int csrno) { if (!csrind_or_aia_extensions_present(env)) { @@ -398,6 +418,15 @@ static RISCVException hmode32(CPURISCVState *env, int csrno) } +static RISCVException csrind_hmode(CPURISCVState *env, int csrno) +{ + if (!csrind_extensions_present(env)) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return hmode(env, csrno); +} + static RISCVException csrind_or_aia_hmode(CPURISCVState *env, int csrno) { if (!csrind_or_aia_extensions_present(env)) { @@ -2027,7 +2056,12 @@ static int csrind_xlate_vs_csrno(CPURISCVState *env, int csrno) case CSR_SISELECT: return CSR_VSISELECT; case CSR_SIREG: - return CSR_VSIREG; + case CSR_SIREG2: + case CSR_SIREG3: + case CSR_SIREG4: + case CSR_SIREG5: + case CSR_SIREG6: + return CSR_VSIREG + (csrno - CSR_SIREG); default: return csrno; }; @@ -2067,7 +2101,12 @@ static RISCVException rmw_xiselect(CPURISCVState *env, int csrno, *val = *iselect; } - wr_mask &= ISELECT_MASK; + if (riscv_cpu_cfg(env)->ext_smcsrind || riscv_cpu_cfg(env)->ext_sscsrind) { + wr_mask &= ISELECT_MASK_SXCSRIND; + } else { + wr_mask &= ISELECT_MASK_AIA; + } + if (wr_mask) { *iselect = (*iselect & ~wr_mask) | (new_val & wr_mask); } @@ -2206,6 +2245,59 @@ done: return RISCV_EXCP_NONE; } +/* + * rmw_xireg_csrind: Perform indirect access to xireg and xireg2-xireg6 + * + * Perform indirect access to xireg and xireg2-xireg6. + * This is a generic interface for all xireg CSRs. Apart from AIA, all other + * extension using csrind should be implemented here. + */ +static int rmw_xireg_csrind(CPURISCVState *env, int csrno, + target_ulong isel, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + return -EINVAL; +} + +static int rmw_xiregi(CPURISCVState *env, int csrno, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + bool virt = false; + int ret = -EINVAL; + target_ulong isel; + + ret = smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT); + if (ret != RISCV_EXCP_NONE) { + return ret; + } + + /* Translate CSR number for VS-mode */ + csrno = csrind_xlate_vs_csrno(env, csrno); + + if (CSR_MIREG <= csrno && csrno <= CSR_MIREG6 && + csrno != CSR_MIREG4 - 1) { + isel = env->miselect; + } else if (CSR_SIREG <= csrno && csrno <= CSR_SIREG6 && + csrno != CSR_SIREG4 - 1) { + isel = env->siselect; + } else if (CSR_VSIREG <= csrno && csrno <= CSR_VSIREG6 && + csrno != CSR_VSIREG4 - 1) { + isel = env->vsiselect; + virt = true; + } else { + goto done; + } + + return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask); + +done: + if (ret) { + return (env->virt_enabled && virt) ? + RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; + } + return RISCV_EXCP_NONE; +} + static RISCVException rmw_xireg(CPURISCVState *env, int csrno, target_ulong *val, target_ulong new_val, target_ulong wr_mask) @@ -2238,8 +2330,21 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, goto done; }; + /* + * Use the xiselect range to determine actual op on xireg. + * + * Since we only checked the existence of AIA or Indirect Access in the + * predicate, we should check the existence of the exact extension when + * we get to a specific range and return illegal instruction exception even + * in VS-mode. + */ if (xiselect_aia_range(isel)) { return rmw_xireg_aia(env, csrno, isel, val, new_val, wr_mask); + } else if (riscv_cpu_cfg(env)->ext_smcsrind || + riscv_cpu_cfg(env)->ext_sscsrind) { + return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask); + } else { + return RISCV_EXCP_ILLEGAL_INST; } done: @@ -2671,7 +2776,7 @@ static RISCVException write_mstateen0(CPURISCVState *env, int csrno, * TODO: Do we need to check ssaia as well ? Can we enable ssaia without * smaia ? */ - if (riscv_cpu_cfg(env)->ext_smaia) { + if (riscv_cpu_cfg(env)->ext_smaia || riscv_cpu_cfg(env)->ext_smcsrind) { wr_mask |= SMSTATEEN0_SVSLCT; } @@ -2764,7 +2869,7 @@ static RISCVException write_hstateen0(CPURISCVState *env, int csrno, wr_mask |= SMSTATEEN0_FCSR; } - if (riscv_cpu_cfg(env)->ext_ssaia) { + if (riscv_cpu_cfg(env)->ext_ssaia || riscv_cpu_cfg(env)->ext_sscsrind) { wr_mask |= SMSTATEEN0_SVSLCT; } @@ -5180,6 +5285,18 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MIREG] = { "mireg", csrind_or_aia_any, NULL, NULL, rmw_xireg }, + /* Machine Indirect Register Alias */ + [CSR_MIREG2] = { "mireg2", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MIREG3] = { "mireg3", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MIREG4] = { "mireg4", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MIREG5] = { "mireg5", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MIREG6] = { "mireg6", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + /* Machine-Level Interrupts (AIA) */ [CSR_MTOPEI] = { "mtopei", aia_any, NULL, NULL, rmw_xtopei }, [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi }, @@ -5301,6 +5418,18 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_SIREG] = { "sireg", csrind_or_aia_smode, NULL, NULL, rmw_xireg }, + /* Supervisor Indirect Register Alias */ + [CSR_SIREG2] = { "sireg2", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_SIREG3] = { "sireg3", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_SIREG4] = { "sireg4", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_SIREG5] = { "sireg5", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_SIREG6] = { "sireg6", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + /* Supervisor-Level Interrupts (AIA) */ [CSR_STOPEI] = { "stopei", aia_smode, NULL, NULL, rmw_xtopei }, [CSR_STOPI] = { "stopi", aia_smode, read_stopi }, @@ -5383,6 +5512,18 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_VSIREG] = { "vsireg", csrind_or_aia_hmode, NULL, NULL, rmw_xireg }, + /* Virtual Supervisor Indirect Alias */ + [CSR_VSIREG2] = { "vsireg2", csrind_hmode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_VSIREG3] = { "vsireg3", csrind_hmode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_VSIREG4] = { "vsireg4", csrind_hmode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_VSIREG5] = { "vsireg5", csrind_hmode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_VSIREG6] = { "vsireg6", csrind_hmode, NULL, NULL, rmw_xiregi, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + /* VS-Level Interrupts (H-extension with AIA) */ [CSR_VSTOPEI] = { "vstopei", aia_hmode, NULL, NULL, rmw_xtopei }, [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi }, From patchwork Tue Jul 23 23:30:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740454 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8705EC3DA49 for ; Tue, 23 Jul 2024 23:31:25 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOy2-0002ed-Gq; Tue, 23 Jul 2024 19:30:50 -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 1sWOxo-00024n-5f for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:36 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxd-0007qu-U2 for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:29 -0400 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-2cb566d528aso3858724a91.1 for ; Tue, 23 Jul 2024 16:30:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777424; x=1722382224; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=QmaN8kFgJMv1sx+5f9CLn6Vn3miNSyaJkILJElq5Mo0=; b=CxCpIiiAGsulUAV1hvwIgnDUxszWNkXD7VA6gsVbdLBaUDYt2NgensEA/0V7HpBwi1 0Wn+OretYNTXn2XHLDgHfzqyM6HJtXu9BiPUUO38VaQql+st9xIErH6xkKHhE7OGvRuO KaDu3MGf5kcHiaocULRlzEMkYgGfU2x3rBO2pZR1f23TNlqDZ9QiQgO+WrgX5qRgWtqs RLrcEkrGKU+H2Q6I8UI+iqvvm0eXsQ1DHi3zu5zKEZSuqc1aMfNjNsNRVwhAeq299813 4+LRRIW2+vxXYzJ228HFs8wYZVXGgLbp1HmL1TQj4mpZgoYvV9M5DDIFT0wVpctYU3yp 3CJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777424; x=1722382224; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QmaN8kFgJMv1sx+5f9CLn6Vn3miNSyaJkILJElq5Mo0=; b=WbI3EfJ0DFgLvi7D5hBtCMLIrl3foxpyjwxxicHjq5vP7aCg1JDpbSN/q6m3leNi8x R/1eXaMTOwJJLUMussFFlm5Axpm4oMTyWBJ/ChopSnPFl5YVWIUEomEGB+k/6TLoWm/G 7ml4hM9bztzJYdTsA+/k0v4OGV7ItH6zlOrvlA+uyFuF00AQMwgSONfp13unChrRIAt8 VQgro12vIYFdEVSrtuhAlw1OZ8hL/flQUaw3oG7/rnkcAGNhrCC8uhhqtfzoegSVBTBM fgh+NAoWRloi+2i1dITB42tr7Rmy4bwc2T5Rzh9A5LQeYSbCw/l3b7d4h/6oD7wZ4Zye ck5w== X-Forwarded-Encrypted: i=1; AJvYcCUbKjMMc8dDS2IZD8iqSxFfnuDZIEaFAVdIz8ucnlGhzVAqs6kodTa3+d06/tlJLJeiy5W+8vbWoNIOuiNRcbcGHMlGJeI= X-Gm-Message-State: AOJu0Yx3S7rqUuV6Hxd8WU59MApcW8MKTd+PrQ9oA5FIG/XIE02xyqL6 27W7/oWFBtj8TfOhikMFBeubUlztGtHRHQyAUGA2jQqWCCt2DB8PwalPAj1Scng= X-Google-Smtp-Source: AGHT+IH/izxjqUyzzk6c4iK0HboPOdJIfcGvrcdbaoBpHTc/U+VcDkr1UC3pRjTRnW+P/HsaTwNO1Q== X-Received: by 2002:a17:90a:2c01:b0:2cd:49c6:e2d7 with SMTP id 98e67ed59e1d1-2cdae337b80mr1318316a91.19.1721777423925; Tue, 23 Jul 2024 16:30:23 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:23 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:02 -0700 Subject: [PATCH v2 05/13] target/riscv: Add counter delegation definitions MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-5-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x1030.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Kaiwen Xue This adds definitions for counter delegation, including the new scountinhibit register and the mstateen.CD bit. Signed-off-by: Atish Patra Signed-off-by: Kaiwen Xue Reviewed-by: Alistair Francis --- target/riscv/cpu.h | 1 + target/riscv/cpu_bits.h | 8 +++++++- target/riscv/machine.c | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 1619c3acb666..af25550a4a54 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -368,6 +368,7 @@ struct CPUArchState { uint32_t scounteren; uint32_t mcounteren; + uint32_t scountinhibit; uint32_t mcountinhibit; /* PMU cycle & instret privilege mode filtering */ diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 2a8b53a6622e..d20468412dca 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -207,6 +207,9 @@ #define CSR_SSTATEEN2 0x10E #define CSR_SSTATEEN3 0x10F +/* Supervisor Counter Delegation */ +#define CSR_SCOUNTINHIBIT 0x120 + /* Supervisor Trap Handling */ #define CSR_SSCRATCH 0x140 #define CSR_SEPC 0x141 @@ -778,6 +781,7 @@ typedef enum RISCVException { #define MENVCFG_CBIE (3UL << 4) #define MENVCFG_CBCFE BIT(6) #define MENVCFG_CBZE BIT(7) +#define MENVCFG_CDE (1ULL << 60) #define MENVCFG_ADUE (1ULL << 61) #define MENVCFG_PBMTE (1ULL << 62) #define MENVCFG_STCE (1ULL << 63) @@ -869,7 +873,9 @@ typedef enum RISCVException { #define ISELECT_IMSIC_LAST ISELECT_IMSIC_EIE63 #define ISELECT_MASK_AIA 0x1ff -/* MISELECT, SISELECT, and VSISELECT bits (AIA) */ +/* [M|S|VS]SELCT value for Indirect CSR Access Extension */ +#define ISELECT_CD_FIRST 0x40 +#define ISELECT_CD_LAST 0x5f #define ISELECT_MASK_SXCSRIND 0xfff /* Dummy [M|S|VS]ISELECT value for emulating [M|S|VS]TOPEI CSRs */ diff --git a/target/riscv/machine.c b/target/riscv/machine.c index 492c2c6d9d79..1a2a68407852 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -398,6 +398,7 @@ const VMStateDescription vmstate_riscv_cpu = { VMSTATE_UINTTL(env.siselect, RISCVCPU), VMSTATE_UINT32(env.scounteren, RISCVCPU), VMSTATE_UINT32(env.mcounteren, RISCVCPU), + VMSTATE_UINT32(env.scountinhibit, RISCVCPU), VMSTATE_UINT32(env.mcountinhibit, RISCVCPU), VMSTATE_STRUCT_ARRAY(env.pmu_ctrs, RISCVCPU, RV_MAX_MHPMCOUNTERS, 0, vmstate_pmu_ctr_state, PMUCTRState), From patchwork Tue Jul 23 23:30:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740457 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 40662C3DA64 for ; Tue, 23 Jul 2024 23:31:50 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOy9-000379-Mw; Tue, 23 Jul 2024 19:30:57 -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 1sWOxo-00024l-4x for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:36 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxf-0007ry-8X for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:29 -0400 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2cb5243766dso3597370a91.0 for ; Tue, 23 Jul 2024 16:30:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777425; x=1722382225; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=rtkfuDYbgn3SsrC+WRSqtlppsRiTPZyE6z1FNFDI/rs=; b=UE/yubEnoythtAqy6WOGITeXVgyPbhOaSF69IBwQOImfSx+3TxffZWH4GOXiMYV2V7 hE/aucvRsXpVoWSfrq1xDrNt9cWcr71n5pBokH1tQBkIiq/qJfunsfn/Mf7vHzS4j7Nf oJqsg8+7H/5+EMFeG+2GsQ4hPZ/W4Gpr8fvLMm+8aBaADeTyH3qvdF3p15TOT/3kSSFG aydyRuDY6cqVYbcZQq9R+xU16NfBo0HWMWZbbmyeLTDOPwauolz4cDj0MMwnBrbsInpQ PN1egTbz/nnEPXmpW6k1hdjzVJzkhxJu+xmmpOx51/YrvBcL1FR2IChKD+9dTHn0rdzp a87w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777425; x=1722382225; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rtkfuDYbgn3SsrC+WRSqtlppsRiTPZyE6z1FNFDI/rs=; b=k8oMSPdpRh0HQbMosvsMn0q9tkaT5oYFeG/px6P3in+tH0nYuJcRm2/ImIdju34+rW SJsSqTa2wj/9QnbIbj7FKXKkJxsOjguhH7rXh3hIIDDObdGPpcxMt/kjboH+WOI5S1mh qzE3SVBbu9RuWN9qb/b3M5XMjRQ6cALDexLwBrvtYovWVnuaef89e5AER15YBmayIwWi afkDAujiB+4frO1ZIxUngjor3RN25lYScTx8q/jUNC+S88/uwBRa4pjc7CwFCWU3L0IU WJsDj4fYanEyzFfLUX8ph4URK50xsDJWAqlUMsLcCa2PX7Ond4OIokjN+L42VIP24eTn jN1A== X-Forwarded-Encrypted: i=1; AJvYcCU/vtuajeT0pGc9ESuGNdhhPVWzBmO3VIlukT4NZemk06Ye1G0/lxD1tPbZDdxReyBdpBj9FQ4vP+FgsJ/D3zf96JU+qzk= X-Gm-Message-State: AOJu0Yzq0hUwvr/w7KnuvWwxVcsRnyAGbXGmYEmbKXeSoqLqeZR2SSHs uIsymCvMKXVbX8ezxvPd4B9dax1OCxPPj9bpJSIv2quS1ne2DSgj0XxJQPdO1QU= X-Google-Smtp-Source: AGHT+IHLbaHCSUu/cyDKpIsG3L2t+NSbPjJVYU2lj4yVwiBncf8A369ERDrfeLSnwhUAJ6APheRl9g== X-Received: by 2002:a17:90a:1608:b0:2c3:40b6:293b with SMTP id 98e67ed59e1d1-2cdb51bda20mr637960a91.39.1721777425356; Tue, 23 Jul 2024 16:30:25 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:24 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:03 -0700 Subject: [PATCH v2 06/13] target/riscv: Add select value range check for counter delegation MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-6-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Kaiwen Xue This adds checks in ops performed on xireg and xireg2-xireg6 so that the counter delegation function will receive a valid xiselect value with the proper extensions enabled. Co-developed-by: Atish Patra Signed-off-by: Kaiwen Xue Signed-off-by: Atish Patra Reviewed-by: Alistair Francis --- target/riscv/csr.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 4ae3931f0ada..da27ba1b7580 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -2120,6 +2120,11 @@ static bool xiselect_aia_range(target_ulong isel) (ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST); } +static bool xiselect_cd_range(target_ulong isel) +{ + return (ISELECT_CD_FIRST <= isel && isel <= ISELECT_CD_LAST); +} + static int rmw_iprio(target_ulong xlen, target_ulong iselect, uint8_t *iprio, target_ulong *val, target_ulong new_val, @@ -2245,6 +2250,17 @@ done: return RISCV_EXCP_NONE; } +static int rmw_xireg_cd(CPURISCVState *env, int csrno, + target_ulong isel, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + if (!riscv_cpu_cfg(env)->ext_smcdeleg) { + return RISCV_EXCP_ILLEGAL_INST; + } + /* TODO: Implement the functionality later */ + return RISCV_EXCP_NONE; +} + /* * rmw_xireg_csrind: Perform indirect access to xireg and xireg2-xireg6 * @@ -2256,7 +2272,25 @@ static int rmw_xireg_csrind(CPURISCVState *env, int csrno, target_ulong isel, target_ulong *val, target_ulong new_val, target_ulong wr_mask) { - return -EINVAL; + int ret = -EINVAL; + bool virt = csrno == CSR_VSIREG ? true : false; + + if (xiselect_cd_range(isel)) { + ret = rmw_xireg_cd(env, csrno, isel, val, new_val, wr_mask); + } else { + /* + * As per the specification, access to unimplented region is undefined + * but recommendation is to raise illegal instruction exception. + */ + return RISCV_EXCP_ILLEGAL_INST; + } + + if (ret) { + return (env->virt_enabled && virt) ? + RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; + } + + return RISCV_EXCP_NONE; } static int rmw_xiregi(CPURISCVState *env, int csrno, target_ulong *val, From patchwork Tue Jul 23 23:30:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740468 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D4589C3DA63 for ; Tue, 23 Jul 2024 23:33:05 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOy6-0002nC-Du; Tue, 23 Jul 2024 19:30:54 -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 1sWOxp-00025a-WA for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:39 -0400 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxh-0007vI-NV for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:35 -0400 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-2cb5deb027dso3565007a91.1 for ; Tue, 23 Jul 2024 16:30:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777427; x=1722382227; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=cNT2hF9HLfBSmOaYJGBJ5QIholz9xkt8m/D+QTefv9s=; b=LeZX16rTVercNagiQNou/h3dV8VbGAFWTmv4d9wVcxhu/MWXHnK4WriP8/Aq+2MX9/ K7FB1RUWVpAruBeVTQVpdsoJLFJZxZEqjmXjdefVqaLOH2yxKL+vNeBYsOvfMjQ5+om0 04GfOe9H9AJAIhyT3dMos5KmR7R7JuX7TCs6S4VJ20+8n8ktOvPOkhxGWTjFkuQP0/nE FjisObMOY9x5w+RZBLXnQmmmNsWaV6UPYeIqhcqCQKEWLof53ccpgfK1P0yRD3q0cXSX piCVd6+sL9Q8zfzso63MdclsQQltaO4d/q3FzB/KkV+RCv2XEg2Uu95q1QtPr/Czf4Dk Ouog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777427; x=1722382227; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cNT2hF9HLfBSmOaYJGBJ5QIholz9xkt8m/D+QTefv9s=; b=e+M1bzlfWWq6fhLQ/D+M+mkxdfYdLPIiQO4czrBShgiHfk3qK21jL7iKk03kwIZYI/ C65ObqsVFYohMnuEjPJ9ZY8o72BSZPtPOaTG/RTebW1S1f6lVq65ToYDYl4l1PeyDWn9 ombuAzaiQf8RUkSR/xeNRovraximy0OfL+1vnVo74S0Ptd9eTHwNOBmkPfyyCkFdCiHX 3F5s/3AfgmnTa1bxUm7UJs4dnV+ALRGrjVKojpYI7R/TE/67k802H7u7VwWnt2N9GdXf q9qu3LqUj0pr4HBZfSg9r9ZVH2kGHpV8C68VlzpNaiQPu0piUoF195C/LZ00djC2OFv5 BAqw== X-Forwarded-Encrypted: i=1; AJvYcCXWeD2sh6fYaP5trGF+wRsiZ4mLRDVbmJld3QKdOF3XXvIUxIdpTPPlXa+UPEXntLGcMXeGPuZd2X685r5i5n+z7nNPjLw= X-Gm-Message-State: AOJu0YzXvuyiYsxpwU/UzZAZ6HZz6RVLKLTAHWZb1aQ4vaAIBn29KVCh 3KV39J+bF3DQmdb+7RwqIvpqzt84u/eIkHcHRi2plVzgWsYKbpJLuAxTXATzCjQ= X-Google-Smtp-Source: AGHT+IHVp8S4ZytWO0oHWj1/TPdyUGtW9ebfhOxY/DauM74sf9liX7jIibvG0jti3wGAviQkdJ03kg== X-Received: by 2002:a17:90b:4f4c:b0:2c8:1f30:4e04 with SMTP id 98e67ed59e1d1-2cd274a5198mr10793852a91.36.1721777426981; Tue, 23 Jul 2024 16:30:26 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:26 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:04 -0700 Subject: [PATCH v2 07/13] target/riscv: Add counter delegation/configuration support MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-7-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::1034; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x1034.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Kaiwen Xue The Smcdeleg/Ssccfg adds the support for counter delegation via S*indcsr and Ssccfg. It also adds a new shadow CSR scountinhibit and menvcfg enable bit (CDE) to enable this extension and scountovf virtualization. Signed-off-by: Kaiwen Xue Co-developed-by: Atish Patra Signed-off-by: Atish Patra --- target/riscv/csr.c | 302 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 290 insertions(+), 12 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index da27ba1b7580..2369a746a285 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -366,6 +366,21 @@ static RISCVException aia_smode32(CPURISCVState *env, int csrno) return smode32(env, csrno); } +static RISCVException scountinhibit_pred(CPURISCVState *env, int csrno) +{ + RISCVCPU *cpu = env_archcpu(env); + + if (!cpu->cfg.ext_ssccfg || !cpu->cfg.ext_smcdeleg) { + return RISCV_EXCP_ILLEGAL_INST; + } + + if (env->virt_enabled) { + return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; + } + + return smode(env, csrno); +} + static bool csrind_extensions_present(CPURISCVState *env) { return riscv_cpu_cfg(env)->ext_smcsrind || riscv_cpu_cfg(env)->ext_sscsrind; @@ -1190,10 +1205,9 @@ done: return result; } -static RISCVException write_mhpmcounter(CPURISCVState *env, int csrno, - target_ulong val) +static RISCVException riscv_pmu_write_ctr(CPURISCVState *env, target_ulong val, + uint32_t ctr_idx) { - int ctr_idx = csrno - CSR_MCYCLE; PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; uint64_t mhpmctr_val = val; @@ -1218,10 +1232,9 @@ static RISCVException write_mhpmcounter(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } -static RISCVException write_mhpmcounterh(CPURISCVState *env, int csrno, - target_ulong val) +static RISCVException riscv_pmu_write_ctrh(CPURISCVState *env, target_ulong val, + uint32_t ctr_idx) { - int ctr_idx = csrno - CSR_MCYCLEH; PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; uint64_t mhpmctr_val = counter->mhpmcounter_val; uint64_t mhpmctrh_val = val; @@ -1243,6 +1256,20 @@ static RISCVException write_mhpmcounterh(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } +static int write_mhpmcounter(CPURISCVState *env, int csrno, target_ulong val) +{ + int ctr_idx = csrno - CSR_MCYCLE; + + return riscv_pmu_write_ctr(env, val, ctr_idx); +} + +static int write_mhpmcounterh(CPURISCVState *env, int csrno, target_ulong val) +{ + int ctr_idx = csrno - CSR_MCYCLEH; + + return riscv_pmu_write_ctrh(env, val, ctr_idx); +} + RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val, bool upper_half, uint32_t ctr_idx) { @@ -1308,6 +1335,167 @@ static RISCVException read_hpmcounterh(CPURISCVState *env, int csrno, return riscv_pmu_read_ctr(env, val, true, ctr_index); } +static int rmw_cd_mhpmcounter(CPURISCVState *env, int ctr_idx, + target_ulong *val, target_ulong new_val, + target_ulong wr_mask) +{ + if (wr_mask != 0 && wr_mask != -1) { + return -EINVAL; + } + + if (!wr_mask && val) { + riscv_pmu_read_ctr(env, val, false, ctr_idx); + } else if (wr_mask) { + riscv_pmu_write_ctr(env, new_val, ctr_idx); + } else { + return -EINVAL; + } + + return 0; +} + +static int rmw_cd_mhpmcounterh(CPURISCVState *env, int ctr_idx, + target_ulong *val, target_ulong new_val, + target_ulong wr_mask) +{ + if (wr_mask != 0 && wr_mask != -1) { + return -EINVAL; + } + + if (!wr_mask && val) { + riscv_pmu_read_ctr(env, val, true, ctr_idx); + } else if (wr_mask) { + riscv_pmu_write_ctrh(env, new_val, ctr_idx); + } else { + return -EINVAL; + } + + return 0; +} + +static int rmw_cd_mhpmevent(CPURISCVState *env, int evt_index, + target_ulong *val, target_ulong new_val, + target_ulong wr_mask) +{ + uint64_t mhpmevt_val = new_val; + + if (wr_mask != 0 && wr_mask != -1) { + return -EINVAL; + } + + if (!wr_mask && val) { + *val = env->mhpmevent_val[evt_index]; + if (riscv_cpu_cfg(env)->ext_sscofpmf) { + *val &= ~MHPMEVENT_BIT_MINH; + } + } else if (wr_mask) { + wr_mask &= ~MHPMEVENT_BIT_MINH; + mhpmevt_val = (new_val & wr_mask) | + (env->mhpmevent_val[evt_index] & ~wr_mask); + if (riscv_cpu_mxl(env) == MXL_RV32) { + mhpmevt_val = mhpmevt_val | + ((uint64_t)env->mhpmeventh_val[evt_index] << 32); + } + env->mhpmevent_val[evt_index] = mhpmevt_val; + riscv_pmu_update_event_map(env, mhpmevt_val, evt_index); + } else { + return -EINVAL; + } + + return 0; +} + +static int rmw_cd_mhpmeventh(CPURISCVState *env, int evt_index, + target_ulong *val, target_ulong new_val, + target_ulong wr_mask) +{ + uint64_t mhpmevth_val; + uint64_t mhpmevt_val = env->mhpmevent_val[evt_index]; + + if (wr_mask != 0 && wr_mask != -1) { + return -EINVAL; + } + + if (!wr_mask && val) { + *val = env->mhpmeventh_val[evt_index]; + if (riscv_cpu_cfg(env)->ext_sscofpmf) { + *val &= ~MHPMEVENTH_BIT_MINH; + } + } else if (wr_mask) { + wr_mask &= ~MHPMEVENTH_BIT_MINH; + env->mhpmeventh_val[evt_index] = + (new_val & wr_mask) | (env->mhpmeventh_val[evt_index] & ~wr_mask); + mhpmevth_val = env->mhpmeventh_val[evt_index]; + mhpmevt_val = mhpmevt_val | (mhpmevth_val << 32); + riscv_pmu_update_event_map(env, mhpmevt_val, evt_index); + } else { + return -EINVAL; + } + + return 0; +} + +static int rmw_cd_ctr_cfg(CPURISCVState *env, int cfg_index, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + switch (cfg_index) { + case 0: /* CYCLECFG */ + if (wr_mask) { + wr_mask &= ~MCYCLECFG_BIT_MINH; + env->mcyclecfg = (new_val & wr_mask) | (env->mcyclecfg & ~wr_mask); + } else { + *val = env->mcyclecfg &= ~MHPMEVENTH_BIT_MINH; + } + break; + case 2: /* INSTRETCFG */ + if (wr_mask) { + wr_mask &= ~MINSTRETCFG_BIT_MINH; + env->minstretcfg = (new_val & wr_mask) | + (env->minstretcfg & ~wr_mask); + } else { + *val = env->minstretcfg &= ~MHPMEVENTH_BIT_MINH; + } + break; + default: + return -EINVAL; + } + return 0; +} + +static int rmw_cd_ctr_cfgh(CPURISCVState *env, int cfg_index, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + + if (riscv_cpu_mxl(env) != MXL_RV32) { + return RISCV_EXCP_ILLEGAL_INST; + } + + switch (cfg_index) { + case 0: /* CYCLECFGH */ + if (wr_mask) { + wr_mask &= ~MCYCLECFGH_BIT_MINH; + env->mcyclecfgh = (new_val & wr_mask) | + (env->mcyclecfgh & ~wr_mask); + } else { + *val = env->mcyclecfgh; + } + break; + case 2: /* INSTRETCFGH */ + if (wr_mask) { + wr_mask &= ~MINSTRETCFGH_BIT_MINH; + env->minstretcfgh = (new_val & wr_mask) | + (env->minstretcfgh & ~wr_mask); + } else { + *val = env->minstretcfgh; + } + break; + default: + return -EINVAL; + } + return 0; +} + + static RISCVException read_scountovf(CPURISCVState *env, int csrno, target_ulong *val) { @@ -1317,6 +1505,14 @@ static RISCVException read_scountovf(CPURISCVState *env, int csrno, target_ulong *mhpm_evt_val; uint64_t of_bit_mask; + /* Virtualize scountovf for counter delegation */ + if (riscv_cpu_cfg(env)->ext_sscofpmf && + riscv_cpu_cfg(env)->ext_ssccfg && + get_field(env->menvcfg, MENVCFG_CDE) && + env->virt_enabled) { + return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; + } + if (riscv_cpu_mxl(env) == MXL_RV32) { mhpm_evt_val = env->mhpmeventh_val; of_bit_mask = MHPMEVENTH_BIT_OF; @@ -2254,11 +2450,70 @@ static int rmw_xireg_cd(CPURISCVState *env, int csrno, target_ulong isel, target_ulong *val, target_ulong new_val, target_ulong wr_mask) { - if (!riscv_cpu_cfg(env)->ext_smcdeleg) { + int ret = -EINVAL; + int ctr_index = isel - ISELECT_CD_FIRST; + int isel_hpm_start = ISELECT_CD_FIRST + 3; + + if (!riscv_cpu_cfg(env)->ext_smcdeleg || !riscv_cpu_cfg(env)->ext_ssccfg) { return RISCV_EXCP_ILLEGAL_INST; } - /* TODO: Implement the functionality later */ - return RISCV_EXCP_NONE; + + /* Invalid siselect value for reserved */ + if (ctr_index == 1) { + goto done; + } + + /* sireg4 and sireg5 provides access RV32 only CSRs */ + if (((csrno == CSR_SIREG5) || (csrno == CSR_SIREG4)) && + (riscv_cpu_mxl(env) != MXL_RV32)) { + return RISCV_EXCP_ILLEGAL_INST; + } + + /* Check Sscofpmf dependancy */ + if (!riscv_cpu_cfg(env)->ext_sscofpmf && csrno == CSR_SIREG5 && + (isel_hpm_start <= isel && isel <= ISELECT_CD_LAST)) { + goto done; + } + + /* Check smcntrpmf dependancy */ + if (!riscv_cpu_cfg(env)->ext_smcntrpmf && + (csrno == CSR_SIREG2 || csrno == CSR_SIREG5) && + (ISELECT_CD_FIRST <= isel && isel < isel_hpm_start)) { + goto done; + } + + if (!get_field(env->mcounteren, BIT(ctr_index)) || + !get_field(env->menvcfg, MENVCFG_CDE)) { + goto done; + } + + switch (csrno) { + case CSR_SIREG: + ret = rmw_cd_mhpmcounter(env, ctr_index, val, new_val, wr_mask); + break; + case CSR_SIREG4: + ret = rmw_cd_mhpmcounterh(env, ctr_index, val, new_val, wr_mask); + break; + case CSR_SIREG2: + if (ctr_index <= 2) { + ret = rmw_cd_ctr_cfg(env, ctr_index, val, new_val, wr_mask); + } else { + ret = rmw_cd_mhpmevent(env, ctr_index, val, new_val, wr_mask); + } + break; + case CSR_SIREG5: + if (ctr_index <= 2) { + ret = rmw_cd_ctr_cfgh(env, ctr_index, val, new_val, wr_mask); + } else { + ret = rmw_cd_mhpmeventh(env, ctr_index, val, new_val, wr_mask); + } + break; + default: + goto done; + } + +done: + return ret; } /* @@ -2540,6 +2795,21 @@ static RISCVException write_mcountinhibit(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } +static RISCVException read_scountinhibit(CPURISCVState *env, int csrno, + target_ulong *val) +{ + /* S-mode can only access the bits delegated by M-mode */ + *val = env->mcountinhibit & env->mcounteren; + return RISCV_EXCP_NONE; +} + +static RISCVException write_scountinhibit(CPURISCVState *env, int csrno, + target_ulong val) +{ + write_mcountinhibit(env, csrno, val & env->mcounteren); + return RISCV_EXCP_NONE; +} + static RISCVException read_mcounteren(CPURISCVState *env, int csrno, target_ulong *val) { @@ -2642,12 +2912,14 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno, target_ulong val) { const RISCVCPUConfig *cfg = riscv_cpu_cfg(env); - uint64_t mask = MENVCFG_FIOM | MENVCFG_CBIE | MENVCFG_CBCFE | MENVCFG_CBZE; + uint64_t mask = MENVCFG_FIOM | MENVCFG_CBIE | MENVCFG_CBCFE | + MENVCFG_CBZE | MENVCFG_CDE; if (riscv_cpu_mxl(env) == MXL_RV64) { mask |= (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) | (cfg->ext_sstc ? MENVCFG_STCE : 0) | - (cfg->ext_svadu ? MENVCFG_ADUE : 0); + (cfg->ext_svadu ? MENVCFG_ADUE : 0) | + (cfg->ext_smcdeleg ? MENVCFG_CDE : 0); } env->menvcfg = (env->menvcfg & ~mask) | (val & mask); @@ -2667,7 +2939,8 @@ static RISCVException write_menvcfgh(CPURISCVState *env, int csrno, const RISCVCPUConfig *cfg = riscv_cpu_cfg(env); uint64_t mask = (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) | (cfg->ext_sstc ? MENVCFG_STCE : 0) | - (cfg->ext_svadu ? MENVCFG_ADUE : 0); + (cfg->ext_svadu ? MENVCFG_ADUE : 0) | + (cfg->ext_smcdeleg ? MENVCFG_CDE : 0); uint64_t valh = (uint64_t)val << 32; env->menvcfg = (env->menvcfg & ~mask) | (valh & mask); @@ -5417,6 +5690,11 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_sstateen_1_3, .min_priv_ver = PRIV_VERSION_1_12_0 }, + /* Supervisor Counter Delegation */ + [CSR_SCOUNTINHIBIT] = {"scountinhibit", scountinhibit_pred, + read_scountinhibit, write_scountinhibit, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + /* Supervisor Trap Setup */ [CSR_SSTATUS] = { "sstatus", smode, read_sstatus, write_sstatus, NULL, read_sstatus_i128 }, From patchwork Tue Jul 23 23:30:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740461 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1DD41C3DA7F for ; Tue, 23 Jul 2024 23:32:34 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOyU-0005BC-QM; Tue, 23 Jul 2024 19:31:18 -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 1sWOxu-0002Hn-1J for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:43 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxp-0007wr-Mu for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:40 -0400 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-70aec66c936so3507677b3a.0 for ; Tue, 23 Jul 2024 16:30:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777429; x=1722382229; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=fzpl2FDaqQI90Wgs2VAECd6YBhDpt1N/LBc0wkqE4+E=; b=bAHZWqWnN1FcfROYNmkrw/jtSbsqy2nZ0siQGnk+Qc3wOCHCNF2U3CUQZap8I2QVMs +LtXIMz3sHKd6VQg3UcwZYJZmxHGb1dtAc8GzCq5xzsOkpkjuagL93TsrdOExOpjH9gV 2QhUxpAB2vJXAPJ8RZ/sgDoLfBUWf/Ahkvgkk/htzavp+BLxeSLEbdGdGZSxKUT0uqbo cHyPI8+rvpGSIZaPAPC6A+9brJQOh9+pc+/OpP7zRJzZ5NqUe5q+shiMTADeCRIUiSbQ aN94a1Dw+HTv0Zsq4g0WnsOZf/fMDPAFd58OsYaMXAdgwNo9ssE1LGZHLyN7AapryfT5 jtMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777429; x=1722382229; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fzpl2FDaqQI90Wgs2VAECd6YBhDpt1N/LBc0wkqE4+E=; b=AVDLqL24U3taUJ24DzCET9SyF+5D6v7iFV/5+4tTfrfl0GTqLerRpYMiZ59cBOxrGW pRel5nZitDzAqiay2aNO5z4t2Re7fFEq4aB4SEataXYoSRacJ+WrS1QObO1yRMbhGweo /zph4x86dsoLPKvvToYUvnJYvAlJvZ9rpxe0uGBelk4vfY5qYf9T151aopxcHb4dfDoo eICWiiRQjq+EobPIo0tXp/ktBktB4IW0nNx/q1pv23tABzoTLP8SQWrRTfN0gzuqVFMs vABRqZ0mcTJSQ3WYCYrKNnfiFB4UOXXniwElOUSug0EpbdMn4ghbUWVsRYeBR0zrkJtD NyYQ== X-Forwarded-Encrypted: i=1; AJvYcCWXjpIfUlkxAD/Ijz2vjf6dzswxXKfJkUcKSEpfxp7JqPQFCWgxhzR638GXRBa+nlegRsUzTLLJfPuva0jBiPZridGWy0c= X-Gm-Message-State: AOJu0YxOVRGOcLCpBLmNMb0MSE5JLTGgNLO/w5WTcpykCdm96EWI91J/ ky8/3H1WfArKPauzL7znAN16sQc+WsL90ioEcXseNpRS5OEoBHVjAj6Ln41tExw= X-Google-Smtp-Source: AGHT+IH0NWVEl40As2AO92BFslELDwbnckmlPe6E/sFRyPxqj/0QZptSc68983+kEZoJL52gCPSmBw== X-Received: by 2002:a05:6a21:9688:b0:1c0:f5be:a3ca with SMTP id adf61e73a8af0-1c461972facmr603979637.30.1721777428792; Tue, 23 Jul 2024 16:30:28 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:27 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:05 -0700 Subject: [PATCH v2 08/13] target/riscv: Add configuration for S[m|s]csrind, Smcdeleg/Ssccfg MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-8-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=atishp@rivosinc.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Add configuration options so that they can be enabled/disabld from qemu commandline. Signed-off-by: Atish Patra Acked-by: Alistair Francis --- target/riscv/cpu.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index ac2dce734d80..1731dc461376 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1477,6 +1477,10 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = { /* Defaults for standard extensions */ MULTI_EXT_CFG_BOOL("sscofpmf", ext_sscofpmf, false), MULTI_EXT_CFG_BOOL("smcntrpmf", ext_smcntrpmf, false), + MULTI_EXT_CFG_BOOL("smcsrind", ext_smcsrind, false), + MULTI_EXT_CFG_BOOL("smcdeleg", ext_smcdeleg, false), + MULTI_EXT_CFG_BOOL("sscsrind", ext_sscsrind, false), + MULTI_EXT_CFG_BOOL("ssccfg", ext_ssccfg, false), MULTI_EXT_CFG_BOOL("zifencei", ext_zifencei, true), MULTI_EXT_CFG_BOOL("zicsr", ext_zicsr, true), MULTI_EXT_CFG_BOOL("zihintntl", ext_zihintntl, true), From patchwork Tue Jul 23 23:30:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740464 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BC113C3DA49 for ; Tue, 23 Jul 2024 23:32:50 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOy7-0002xM-7S; Tue, 23 Jul 2024 19:30:55 -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 1sWOxr-00029U-VI for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:41 -0400 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxl-0007xS-P1 for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:38 -0400 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-70d138e06e3so2756219b3a.2 for ; Tue, 23 Jul 2024 16:30:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777430; x=1722382230; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=jI6U0BvTHQQ9+y5jpbqfCCV572dEAtDwxnif55veu+c=; b=0Yf1ipVjQvWy+fkEylTF3+W0hd+MoUCbRP0ISzY5CVG0H4tozC8WE2rBvlHWkoysWa qr43C9/5nueGKbC2Fjpp84L7yWqMCWsUwQ467KF/WepJm8ymSVBtMhrJv3Ekr9Bmq57D JI/C+mERktfmpWax2UgrkThhHv0DzPXl4dsZ9WpQglXru2B1l30gUKbd232xbPz3ziDX L9vTygu4pgywq9jipZlt7hhIbNVVrEZzPNFzlEoVCVr/Q0wnCxsgBEuLl5PmrpI27a12 XwChulEf2FC5B1f7ituq2pRJbZJJKFoeXFeINknM9OVCIOOp6NulYdvxgKCKdue1h5Cf j17A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777430; x=1722382230; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jI6U0BvTHQQ9+y5jpbqfCCV572dEAtDwxnif55veu+c=; b=E4MJn3mzknKr4qyMgon4dbRfmACv7vd5V5YxsMSfSV6+ml51UUlTMuD0ecBklaN7GY Y1ABzodKc+Q1NRn7xX8+3u4egeyAE+TzhuTKnoMF06kp+JXBLrS6grzItsda5V0KtTs3 x2GKFfO/XtvS7VgCCqku9A4AnHgn0uTnK5ccEMt+K4bSL9GkuehU/vrZLoNX+O3DPliC j5IUG688DCw6xHDZzCvUYLp0GbWqpKWK/lLNIObWM83pk2S/P4sY/htcBRvZ+LFUmm5j Ni+Z6iQCrJZj+VZut1fSS1XkTk5N+L2Z21pI2wml+AhPJvjZkm7sG02WS7Ns0xveTXmi XvCQ== X-Forwarded-Encrypted: i=1; AJvYcCV7AxMphuDeo4H2QoFb2FSBm38oJ/01OMV2vuoERQWR5mHH1lRTHNr8Y3hxSqSuR03ts8JFrZgKFIj0H/yb/bJFwxyjy/4= X-Gm-Message-State: AOJu0YwyrKz5Xer6bF7eWMwZgF7jUEoq4NXXQ7/QnZOWes8yCg7O5VxZ sTjXNH0LxOCxW4VgPOsvBMq6u/urI63W/61401X5/GHzXB8KyFzliaceuW4OQSg= X-Google-Smtp-Source: AGHT+IEMB/uTTk7trNXTFSXtM++S7cdg97r/VHKtFqFK5NEizxBks1RXkuGIK3pPek9tQcksCjsYMQ== X-Received: by 2002:a05:6a21:3390:b0:1c1:e75a:5504 with SMTP id adf61e73a8af0-1c42287f402mr17626902637.15.1721777430306; Tue, 23 Jul 2024 16:30:30 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:29 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:06 -0700 Subject: [PATCH v2 09/13] target/riscv: Invoke pmu init after feature enable MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-9-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=atishp@rivosinc.com; helo=mail-pf1-x42a.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org The dependant ISA features are enabled at the end of cpu_realize in finalize_features. Thus, PMU init should be invoked after that only. Move the init invocation to riscv_tcg_cpu_finalize_features. Signed-off-by: Atish Patra Reviewed-by: Alistair Francis --- target/riscv/tcg/tcg-cpu.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/target/riscv/tcg/tcg-cpu.c b/target/riscv/tcg/tcg-cpu.c index b8814ab753bd..d78d5960cf30 100644 --- a/target/riscv/tcg/tcg-cpu.c +++ b/target/riscv/tcg/tcg-cpu.c @@ -898,6 +898,20 @@ void riscv_tcg_cpu_finalize_features(RISCVCPU *cpu, Error **errp) error_propagate(errp, local_err); return; } +#ifndef CONFIG_USER_ONLY + if (cpu->cfg.pmu_mask) { + riscv_pmu_init(cpu, &local_err); + if (local_err != NULL) { + error_propagate(errp, local_err); + return; + } + + if (cpu->cfg.ext_sscofpmf) { + cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, + riscv_pmu_timer_cb, cpu); + } + } +#endif } void riscv_tcg_cpu_finalize_dynamic_decoder(RISCVCPU *cpu) @@ -945,7 +959,6 @@ static bool riscv_tcg_cpu_realize(CPUState *cs, Error **errp) #ifndef CONFIG_USER_ONLY CPURISCVState *env = &cpu->env; - Error *local_err = NULL; tcg_cflags_set(CPU(cs), CF_PCREL); @@ -953,19 +966,6 @@ static bool riscv_tcg_cpu_realize(CPUState *cs, Error **errp) riscv_timer_init(cpu); } - if (cpu->cfg.pmu_mask) { - riscv_pmu_init(cpu, &local_err); - if (local_err != NULL) { - error_propagate(errp, local_err); - return false; - } - - if (cpu->cfg.ext_sscofpmf) { - cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, - riscv_pmu_timer_cb, cpu); - } - } - /* With H-Ext, VSSIP, VSTIP, VSEIP and SGEIP are hardwired to one. */ if (riscv_has_ext(env, RVH)) { env->mideleg = MIP_VSSIP | MIP_VSTIP | MIP_VSEIP | MIP_SGEIP; From patchwork Tue Jul 23 23:30:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740462 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D4BA2C3DA63 for ; Tue, 23 Jul 2024 23:32:35 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOyR-0004Xv-4G; Tue, 23 Jul 2024 19:31:15 -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 1sWOxw-0002Ko-0l for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:45 -0400 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxp-0007xu-Mj for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:42 -0400 Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-70b2421471aso710069a12.0 for ; Tue, 23 Jul 2024 16:30:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777431; x=1722382231; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=T3wPalTrTfAP9mml+0G2G5CLa2ONQtu46tPm2ADYEhI=; b=L9PzayC7WPYgjkmkxrFLf0By50EzcySZ4L+p5WMiWvUsDkZUeWRAji3UaXVZ7eVHZ2 3I29EOU180h9yboF4sB5x0rqB9Sgum+PqOv+pZhINW8iKBTLuhv+jNdyDTA9+hQLrfht Js0cgQap5xOHueONuB9WeSo8Nu6+OMOGFVSz2EAK68358FyWmtxlL/39oSXLwek6qvkd Ok9S8kAIGdIwE541K5HTmcDcDojzbyA4dmDYrlDXllkajRcDZaKAmZdz0gyBdxbtBwmO paBtj/IMYQt4DLXhHAgNZz6bynWS56Y1RF4GXe4yS7hIF70/sbmtRYlrUZXR8qW6jy4J lj8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777431; x=1722382231; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T3wPalTrTfAP9mml+0G2G5CLa2ONQtu46tPm2ADYEhI=; b=JzioIzfnr8B6WMP3iMWo8BT5lkHfjW8r+6Aje3TIEf/eKmf+w41ilDe576oGhmONDv YP8Yn0SgCCtKMB7J0XjmJsiHRQUMxS5hYiDU66D2TMrniRBpBZ7op+cwZ/8U1PAiTRaS eVy9R1iAnmtmeqF6zFNL9SVVSc1vlRxxTqH3rpSa4L/nnnUUIg5JqwfncDHChvp+eqkm oB52i62bVP41eyAUtNgk0wRr5ihC/SWsvE+k6Y6gpcDHWGf0U9DN2f/h5R3DFEe+Gp20 VRu2Emj+mQ2E61s3NWXZnazLq+DSReYfzk11vp5yi5Tr4DCiGPAExHRMrmMdbaeZtusU sXjg== X-Forwarded-Encrypted: i=1; AJvYcCVlwlsQhj0AIozzqXoNMjoXwg9KYWiowmzDW6SQcrjfHr//7Z+dXyF2J7upso/9FP6KGblNiF0IV/gHjzb0L08YZN+ggyk= X-Gm-Message-State: AOJu0YwiBbmyzOtcQg7vECRsZI+cE6awrz+R8KPPW8tdWVq6bZOJLuyr rsXb/zAEt03HtfBCoTKn6aLtUMwKzUnkSwngnlrxzYQsvT2A7baZngWgDmMcmTsKIZisaicdjeu g X-Google-Smtp-Source: AGHT+IElPfelTOZkDhOJa8WCZlRLBE3szMwqKi995ymecoVThCSkNwjVSzUy6QXR71erWMs2QkDt/w== X-Received: by 2002:a17:90a:4b48:b0:2cd:3445:f87b with SMTP id 98e67ed59e1d1-2cdb51afd8amr605297a91.29.1721777431556; Tue, 23 Jul 2024 16:30:31 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:31 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:07 -0700 Subject: [PATCH v2 10/13] target/riscv: Enable sscofpmf for bare cpu by default MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-10-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::533; envelope-from=atishp@rivosinc.com; helo=mail-pg1-x533.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sscofpmf has been supported on virt machine for a long time. It is required to enable profiling on virt machines. Let's enable it by default for ease of usage. Signed-off-by: Atish Patra --- target/riscv/cpu.c | 1 + 1 file changed, 1 insertion(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 1731dc461376..393d1d67120e 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -494,6 +494,7 @@ static void rv64_base_cpu_init(Object *obj) env->priv_ver = PRIV_VERSION_LATEST; #ifndef CONFIG_USER_ONLY set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV57); + cpu->cfg.ext_sscofpmf = true; #endif } From patchwork Tue Jul 23 23:30:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740467 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 09E44C3DA63 for ; Tue, 23 Jul 2024 23:33:01 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOy6-0002tq-DK; Tue, 23 Jul 2024 19:30:54 -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 1sWOxw-0002Km-0d for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:45 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxp-0007y6-Ml for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:42 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-2cb55418470so3136392a91.1 for ; Tue, 23 Jul 2024 16:30:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777433; x=1722382233; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=RA26pTTCCGZrMwpdrKiBc0SSPwJQN39A/vQ5CD8f/Ik=; b=IsXdODkZs+yOIsfGsqt7uOkaOyYQHPUsANbmCuMfsBV+aM9BFtCbyv+EYZjatefluq peMbmq1bhVsrMsLyXsYvbf0KgEd6AIivmeAE1Is57BP0trGxLD9ImFvONb1HVN3l3341 PZO3IyFvALo3sqQBMMu0raAMUmT7QM/AaM0N+vpgEeZtdiU1Xqe89z+Z6Ya79bSIlNzD mvJLHRSHfLZdFoUwiQxd8MiidZ+WD47s2v7BivDaqWUE7jk1SXDLq/O42K151nyvSYyV 35okYqAR9zDWjsuKaoz1SL4f3+BnlSYIhw6rTqcvp2cRx0Ps+KmXQWtvdee9h3t6vEnD oyHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777433; x=1722382233; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RA26pTTCCGZrMwpdrKiBc0SSPwJQN39A/vQ5CD8f/Ik=; b=nlwexpekOzOYkJ/qm3UjKh0Xb7eL9YTYu9zYs0LjbvtYzyHl2uZDQ7kiUODXNBYbHY wOEQvfi5fwO2EzvIvFrOmonCdS/snNb3hi13BVTjF9BSpoHSLT+mqZDokr+tshOE5f9Z jL017+q7ykNQTXC35oqcq7m00utIqs2zuldNOqkzYkRJDSWoK65Q9elhTRzM3ALYgoI3 wgfNn4wPCV4FtxbFvE6EFYEhaShqY7ZvA65zY58cDKvI1mkbOgWpW6hKAcd4+KI7qq6a ip8J9vAKy6oBlqN2nhOXFJtp6iw7SSu+j92+EN/39+QJurZ4lfsDXQlw6N8INASzmozG z/qA== X-Forwarded-Encrypted: i=1; AJvYcCVJkUPvR2Zj7GAEqqB5VqI1LXIVKAVJRyIb3YBdj3XgJ+Cy/PfivU9gS9fsrNU9eMM1qIPXA7xTLvKfOp7Z2zykMGXVLRQ= X-Gm-Message-State: AOJu0YwwOomc/J0FNROIfW0V/BUcE3gdu5fFxburQ6uEIjPFVOi8a0vw j/b87+zd6IXryQJPjHO4WPh6VlLBcJT3X8xx6v2PioBjmeTDKUX8xuoCYoxrD/0= X-Google-Smtp-Source: AGHT+IFn6T8UUPLvJ8lAom0orLgIB6YlSSvE4DjPyecnjT5NqeUo8OINB5Byiv45uhr9LJFHlGXzzQ== X-Received: by 2002:a17:90b:390e:b0:2c9:8b33:3197 with SMTP id 98e67ed59e1d1-2cdae35852fmr1368962a91.10.1721777432784; Tue, 23 Jul 2024 16:30:32 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:32 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:08 -0700 Subject: [PATCH v2 11/13] target/riscv: Repurpose the implied rule startergy MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-11-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x102e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org The current infrastructure for implied ISA extension enabling can be used for other cases where a particular ISA is dependant on multiple other ISA extension to enable all the features. Rename the implied rule functions/data structures to accomodate that. Signed-off-by: Atish Patra --- target/riscv/tcg/tcg-cpu.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/target/riscv/tcg/tcg-cpu.c b/target/riscv/tcg/tcg-cpu.c index d78d5960cf30..1c9a87029423 100644 --- a/target/riscv/tcg/tcg-cpu.c +++ b/target/riscv/tcg/tcg-cpu.c @@ -40,7 +40,7 @@ static GHashTable *multi_ext_user_opts; static GHashTable *misa_ext_user_opts; -static GHashTable *multi_ext_implied_rules; +static GHashTable *multi_ext_enabling_rules; static GHashTable *misa_ext_implied_rules; static bool cpu_cfg_ext_is_user_set(uint32_t ext_offset) @@ -730,7 +730,7 @@ static void riscv_cpu_validate_profiles(RISCVCPU *cpu) } } -static void riscv_cpu_init_implied_exts_rules(void) +static void riscv_cpu_init_ext_rules(void) { RISCVCPUImpliedExtsRule *rule; #ifndef CONFIG_USER_ONLY @@ -756,14 +756,14 @@ static void riscv_cpu_init_implied_exts_rules(void) #ifndef CONFIG_USER_ONLY rule->enabled = bitmap_new(ms->smp.cpus); #endif - g_hash_table_insert(multi_ext_implied_rules, + g_hash_table_insert(multi_ext_enabling_rules, GUINT_TO_POINTER(rule->ext), (gpointer)rule); } initialized = true; } -static void cpu_enable_implied_rule(RISCVCPU *cpu, +static void cpu_enable_ext_rule(RISCVCPU *cpu, RISCVCPUImpliedExtsRule *rule) { CPURISCVState *env = &cpu->env; @@ -787,7 +787,7 @@ static void cpu_enable_implied_rule(RISCVCPU *cpu, GUINT_TO_POINTER(misa_bits[i])); if (ir) { - cpu_enable_implied_rule(cpu, ir); + cpu_enable_ext_rule(cpu, ir); } } } @@ -798,12 +798,12 @@ static void cpu_enable_implied_rule(RISCVCPU *cpu, rule->implied_multi_exts[i] != RISCV_IMPLIED_EXTS_RULE_END; i++) { cpu_cfg_ext_auto_update(cpu, rule->implied_multi_exts[i], true); - ir = g_hash_table_lookup(multi_ext_implied_rules, - GUINT_TO_POINTER( - rule->implied_multi_exts[i])); + ir = g_hash_table_lookup(multi_ext_enabling_rules, + GUINT_TO_POINTER( + rule->implied_multi_exts[i])); if (ir) { - cpu_enable_implied_rule(cpu, ir); + cpu_enable_ext_rule(cpu, ir); } } @@ -844,7 +844,7 @@ static void cpu_enable_zc_implied_rules(RISCVCPU *cpu) } } -static void riscv_cpu_enable_implied_rules(RISCVCPU *cpu) +static void riscv_cpu_enable_ext_rules(RISCVCPU *cpu) { RISCVCPUImpliedExtsRule *rule; int i; @@ -855,14 +855,14 @@ static void riscv_cpu_enable_implied_rules(RISCVCPU *cpu) /* Enable the implied MISAs. */ for (i = 0; (rule = riscv_misa_ext_implied_rules[i]); i++) { if (riscv_has_ext(&cpu->env, rule->ext)) { - cpu_enable_implied_rule(cpu, rule); + cpu_enable_ext_rule(cpu, rule); } } /* Enable the implied extensions. */ for (i = 0; (rule = riscv_multi_ext_implied_rules[i]); i++) { if (isa_ext_is_enabled(cpu, rule->ext)) { - cpu_enable_implied_rule(cpu, rule); + cpu_enable_ext_rule(cpu, rule); } } } @@ -872,8 +872,8 @@ void riscv_tcg_cpu_finalize_features(RISCVCPU *cpu, Error **errp) CPURISCVState *env = &cpu->env; Error *local_err = NULL; - riscv_cpu_init_implied_exts_rules(); - riscv_cpu_enable_implied_rules(cpu); + riscv_cpu_init_ext_rules(); + riscv_cpu_enable_ext_rules(cpu); riscv_cpu_validate_misa_priv(env, &local_err); if (local_err != NULL) { @@ -1385,8 +1385,8 @@ static void riscv_tcg_cpu_instance_init(CPUState *cs) misa_ext_implied_rules = g_hash_table_new(NULL, g_direct_equal); } - if (!multi_ext_implied_rules) { - multi_ext_implied_rules = g_hash_table_new(NULL, g_direct_equal); + if (!multi_ext_enabling_rules) { + multi_ext_enabling_rules = g_hash_table_new(NULL, g_direct_equal); } riscv_cpu_add_user_properties(obj); From patchwork Tue Jul 23 23:30:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740465 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 73757C3DA64 for ; Tue, 23 Jul 2024 23:32:52 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOy7-0002yJ-Di; Tue, 23 Jul 2024 19:30:55 -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 1sWOxz-0002Tt-U2 for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:47 -0400 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxr-0007zF-Mo for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:43 -0400 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-2cb5243766dso3597476a91.0 for ; Tue, 23 Jul 2024 16:30:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777437; x=1722382237; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=8g3egpPSQQCB4Jzut23yj1uXHYNkJTWfJCb9u6Ykb48=; b=BwXSjbXAXBx+ee0YqPviF7GoXGbJGZwnzlHtbjG3DgT3TT4w43AUfRwAa68OHx4rVD FoafECMyKihPatUBZQTPmJVok4O18rR/l8Wbzj8FQ0XvgNpjxqgnkTBPhQ9fJx0Lhi5t sl5/Qd4jsZxtbcdYgRCzXF8BW1B2ctHw0QbSEv5z04p1Bd3dW5+etgZ7jpdiR0r5wiX+ /5jgOtLPksBkKw+2649h68z0P6/fRWYP7eBcUoiISILI7PyTE2OXYN5RRa3ZtOGf6gio xGAKo6iP7JxrSm/oS13O7bBswbcjNF4c4Z8Jg/zZc2yMlqJUGmU+PWY1ww3R131bICoj snwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777437; x=1722382237; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8g3egpPSQQCB4Jzut23yj1uXHYNkJTWfJCb9u6Ykb48=; b=AwrmE4E7fsxZKJkjsphLrAgXLf5WnmTUfqLWrwr6vEv9PMA+Y8vuk+uIlG1hTjN56W ZZMJ/m2TRVg8Sool6R2DtJoU6qIzRxYzWO5Tkm8LmCn3m5Fri564czJKiO4aPyvcLALD AV/KLHanLJ7icPEyCOEJztMS8D8ujUi83UitqANmQlaoLUuUBbI2KC/yy/BIlzBUa+3X 1C3t+MMDOGsDkulvqyYcGHA9WzC1BzgweoEeAXcjKhkBXZAV3FL/55QKcdgHlV/wG1O6 MugyQJ6gWU2H3g+sBAoEQvfYFMBXZlgTjeRzoXz6FhgWmiEKc93z214HivEz/n938x6n X6vg== X-Forwarded-Encrypted: i=1; AJvYcCWGqtkNxdOU2HSKe6uc7uE2Zs1xRQyUVXT3YBHCGu4+nfY/awJDAILPadZczgfFEQ1ekefT3fAvtXi6H3yaHbfS0ck1gF0= X-Gm-Message-State: AOJu0YwLkYk6nbezE0bY8PCcVlB8x+gbuFnrHzgOk2JSS6t0ArOByJuw oDxaJt4hrERYHtXo06S5J+Zw75z+y+zoImS/rI6zX8iQmQIWKCuIyy5rV97EP958j2lxWbbEvEg v X-Google-Smtp-Source: AGHT+IHpOiDxe5VD8grEVKf0ieVZookbXdlomlpuMXrUs3iJpeqcWgNUeQ2jb0KHLHpKiVed0Q3AGg== X-Received: by 2002:a17:90b:494:b0:2c9:6d07:18f4 with SMTP id 98e67ed59e1d1-2cdb51bc73fmr614420a91.35.1721777434217; Tue, 23 Jul 2024 16:30:34 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:33 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:09 -0700 Subject: [PATCH v2 12/13] target/riscv: Add a preferred ISA extension rule MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-12-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::1029; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x1029.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org In addition to the implied rule, a preferred rule will be useful where an ISA extension may require a list of ISA extension to be enabled to use all the features defined in that extension. All these extensions may not be implied in the ISA. This patch just introduces a new preferred rule which allows to enable multiple extensions together without burdening the qemu commandline user. Signed-off-by: Atish Patra --- target/riscv/cpu.c | 4 ++++ target/riscv/cpu.h | 17 ++++++++++++++ target/riscv/tcg/tcg-cpu.c | 57 ++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 393d1d67120e..22ba43c7ff2a 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -2665,6 +2665,10 @@ RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = { NULL }; +RISCVCPUPreferredExtsRule *riscv_multi_ext_preferred_rules[] = { + NULL +}; + static Property riscv_cpu_properties[] = { DEFINE_PROP_BOOL("debug", RISCVCPU, cfg.debug, true), diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index af25550a4a54..d775866344f5 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -142,10 +142,27 @@ typedef struct riscv_cpu_implied_exts_rule { const uint32_t implied_multi_exts[]; } RISCVCPUImpliedExtsRule; +typedef struct riscv_cpu_preferred_exts_rule { +#ifndef CONFIG_USER_ONLY + /* + * Bitmask indicates the rule enabled status for the harts. + * This enhancement is only available in system-mode QEMU, + * as we don't have a good way (e.g. mhartid) to distinguish + * the SMP cores in user-mode QEMU. + */ + unsigned long *enabled; +#endif + /* multi extension offset. */ + const uint32_t ext; + const uint32_t preferred_multi_exts[]; +} RISCVCPUPreferredExtsRule; + extern RISCVCPUImpliedExtsRule *riscv_misa_ext_implied_rules[]; extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[]; +extern RISCVCPUPreferredExtsRule *riscv_multi_ext_preferred_rules[]; #define RISCV_IMPLIED_EXTS_RULE_END -1 +#define RISCV_PREFRRED_EXTS_RULE_END RISCV_IMPLIED_EXTS_RULE_END #define MMU_USER_IDX 3 diff --git a/target/riscv/tcg/tcg-cpu.c b/target/riscv/tcg/tcg-cpu.c index 1c9a87029423..d8f74720815a 100644 --- a/target/riscv/tcg/tcg-cpu.c +++ b/target/riscv/tcg/tcg-cpu.c @@ -32,6 +32,7 @@ #include "hw/core/accel-cpu.h" #include "hw/core/tcg-cpu-ops.h" #include "tcg/tcg.h" +#include #ifndef CONFIG_USER_ONLY #include "hw/boards.h" #endif @@ -733,6 +734,7 @@ static void riscv_cpu_validate_profiles(RISCVCPU *cpu) static void riscv_cpu_init_ext_rules(void) { RISCVCPUImpliedExtsRule *rule; + RISCVCPUPreferredExtsRule *prule; #ifndef CONFIG_USER_ONLY MachineState *ms = MACHINE(qdev_get_machine()); #endif @@ -760,22 +762,40 @@ static void riscv_cpu_init_ext_rules(void) GUINT_TO_POINTER(rule->ext), (gpointer)rule); } + for (i = 0; (prule = riscv_multi_ext_preferred_rules[i]); i++) { +#ifndef CONFIG_USER_ONLY + prule->enabled = bitmap_new(ms->smp.cpus); +#endif + g_hash_table_insert(multi_ext_enabling_rules, + GUINT_TO_POINTER(prule->ext), (gpointer)prule); + } + initialized = true; } static void cpu_enable_ext_rule(RISCVCPU *cpu, - RISCVCPUImpliedExtsRule *rule) + RISCVCPUImpliedExtsRule *rule, + RISCVCPUPreferredExtsRule *prule) { CPURISCVState *env = &cpu->env; RISCVCPUImpliedExtsRule *ir; + RISCVCPUPreferredExtsRule *pr; bool enabled = false; int i; #ifndef CONFIG_USER_ONLY - enabled = test_bit(cpu->env.mhartid, rule->enabled); + if (rule) { + enabled = test_bit(cpu->env.mhartid, rule->enabled); + } else if (prule) { + enabled = test_bit(cpu->env.mhartid, prule->enabled); + } else { + return; + } #endif + if (enabled) + return; - if (!enabled) { + if (rule) { /* Enable the implied MISAs. */ if (rule->implied_misa_exts) { riscv_cpu_set_misa_ext(env, @@ -787,7 +807,7 @@ static void cpu_enable_ext_rule(RISCVCPU *cpu, GUINT_TO_POINTER(misa_bits[i])); if (ir) { - cpu_enable_ext_rule(cpu, ir); + cpu_enable_ext_rule(cpu, ir, NULL); } } } @@ -803,12 +823,27 @@ static void cpu_enable_ext_rule(RISCVCPU *cpu, rule->implied_multi_exts[i])); if (ir) { - cpu_enable_ext_rule(cpu, ir); + cpu_enable_ext_rule(cpu, ir, NULL); } } #ifndef CONFIG_USER_ONLY bitmap_set(rule->enabled, cpu->env.mhartid, 1); +#endif + } else if (prule) { + /* Enable the preferred extensions. */ + for (i = 0; + prule->preferred_multi_exts[i] != RISCV_PREFRRED_EXTS_RULE_END; i++) { + cpu_cfg_ext_auto_update(cpu, prule->preferred_multi_exts[i], true); + pr = g_hash_table_lookup(multi_ext_enabling_rules, + GUINT_TO_POINTER( + prule->preferred_multi_exts[i])); + if (pr) { + cpu_enable_ext_rule(cpu, NULL, prule); + } + } +#ifndef CONFIG_USER_ONLY + bitmap_set(prule->enabled, cpu->env.mhartid, 1); #endif } } @@ -847,6 +882,7 @@ static void cpu_enable_zc_implied_rules(RISCVCPU *cpu) static void riscv_cpu_enable_ext_rules(RISCVCPU *cpu) { RISCVCPUImpliedExtsRule *rule; + RISCVCPUPreferredExtsRule *prule; int i; /* Enable the implied extensions for Zc. */ @@ -855,14 +891,21 @@ static void riscv_cpu_enable_ext_rules(RISCVCPU *cpu) /* Enable the implied MISAs. */ for (i = 0; (rule = riscv_misa_ext_implied_rules[i]); i++) { if (riscv_has_ext(&cpu->env, rule->ext)) { - cpu_enable_ext_rule(cpu, rule); + cpu_enable_ext_rule(cpu, rule, NULL); } } /* Enable the implied extensions. */ for (i = 0; (rule = riscv_multi_ext_implied_rules[i]); i++) { if (isa_ext_is_enabled(cpu, rule->ext)) { - cpu_enable_ext_rule(cpu, rule); + cpu_enable_ext_rule(cpu, rule, NULL); + } + } + + /* Enable the preferred extensions. */ + for (i = 0; (prule = riscv_multi_ext_preferred_rules[i]); i++) { + if (isa_ext_is_enabled(cpu, prule->ext)) { + cpu_enable_ext_rule(cpu, NULL, prule); } } } From patchwork Tue Jul 23 23:30:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13740455 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 51C60C3DA49 for ; Tue, 23 Jul 2024 23:31:39 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWOy9-00034p-L9; Tue, 23 Jul 2024 19:30:57 -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 1sWOxz-0002Tk-TA for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:47 -0400 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWOxt-0007zf-Pn for qemu-devel@nongnu.org; Tue, 23 Jul 2024 19:30:45 -0400 Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-2cb5243766dso3597498a91.0 for ; Tue, 23 Jul 2024 16:30:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721777439; x=1722382239; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=xoi8tQlmstuJQE5BtIWLC+NlXYaPeyqDg2JVEoxweos=; b=gGqnxuWvMvnUep896uXT7C50W3a4UdQRkZV36bHbxKdScPrRQOU7SfszWdb3gJSQ9F ellMISAEhvZKfIXzKkVh0SN1LcAfNeMcdJTa+R1q3D9InSrwWmy8IABwiP9587Cwv4Ej ZYoEO+PfueQMT/zr2faK8+W7EvlCtLIQxHhm7ZCNiClDT4bI2uM+CyAJ/w151/snPCK+ /7jQMu+bQv6A5AXVP5zD4Ry6n+2IcRWnXFuIPgM/aOR3an7n+w1fRlW1FqaYZdHco2Xg mAqjJjC4irxzm2tb6+CuVtavcrF+a9ukiOQqSnSXMvaqlA8oaOiwAJUsrsrM6VZRxbvC +Ddw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721777439; x=1722382239; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xoi8tQlmstuJQE5BtIWLC+NlXYaPeyqDg2JVEoxweos=; b=nTKPCLjuDIbdKJdkGTrUOAPdOPxh56hB/iTMiKv2dvdf00uYGrh9GBSvh1kvmA0BHp /ozMjCN/qAADy3sD6CklUu9fM0hZDEcQWI6NoxDO5NNdeGcUwonZ+x7ZctpKNGtvN+lJ p5dQzcx0MBokRJw37uYaAR3eoa2RUWcKA2V/U4VZmGVx43YKd0r2/EpSzkVQSG3CmqRS f6Wnyep7YeCR1I2liMb8J5zkWWVirIBjn4CcrKg1yT5j/QSbJw2FAhijGM5OrFmK7LGZ w6Qdz8atyIe/aYCRbVxw4wKt3/rJQBNBenxXieFNYMS3DhcgoU2tu+y3PzhiQ0J1Xdvn mNCQ== X-Forwarded-Encrypted: i=1; AJvYcCX1Uhf0oU7JTm57LFKW4F0Of+sVlWXWUb6lYHy3jVKLYu7Dpqr9+2ArFPlG3kjkDV/qJfUMfvCKzXtHhFQ5XMGzk5e8tmo= X-Gm-Message-State: AOJu0YzOeMs4Z8sQCkFC8NUeCx8/YfIEe/1Kb73AMW3m/ykNbeQ8G0Cw 4B1flj58J3kz08eLaM071pFQROk+Ims3cUtA78DegBkjT4u1WE2VBqgf4/5M3Mg= X-Google-Smtp-Source: AGHT+IF6UbMkHQ+IeO2x0d8mYCC8dzs0JCjlRdGh0Lg3sEh4YYIWpQwu3frc/rBq7VIcgYvwRWOJnQ== X-Received: by 2002:a17:90a:f28e:b0:2cb:e429:f525 with SMTP id 98e67ed59e1d1-2cdb51a6543mr585954a91.33.1721777439152; Tue, 23 Jul 2024 16:30:39 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cdb74e894csm179217a91.41.2024.07.23.16.30.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 16:30:38 -0700 (PDT) From: Atish Patra Date: Tue, 23 Jul 2024 16:30:10 -0700 Subject: [PATCH v2 13/13] target/riscv: Enable PMU related extensions to preferred rule MIME-Version: 1.0 Message-Id: <20240723-counter_delegation-v2-13-c4170a5348ca@rivosinc.com> References: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> In-Reply-To: <20240723-counter_delegation-v2-0-c4170a5348ca@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com X-Mailer: b4 0.15-dev-13183 Received-SPF: pass client-ip=2607:f8b0:4864:20::102b; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x102b.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Counter delegation/configuration extension requires the following extensions to be enabled. 1. Smcdeleg - To enable counter delegation from M to S 2. S[m|s]csrind - To enable indirect access CSRs 3. Smstateen - Indirect CSR extensions depend on it. 4. Sscofpmf - To enable counter overflow feature 5. S[m|s]aia - To enable counter overflow feature in virtualization 6. Smcntrpmf - To enable privilege mode filtering for cycle/instret While first 3 are mandatory to enable the counter delegation, next 3 set of extension are preferred to enable all the PMU related features. That's why, enable all of these if Ssccfg extension is enabled from the commandline. Signed-off-by: Atish Patra --- target/riscv/cpu.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 22ba43c7ff2a..abebfcc46dea 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -2665,8 +2665,20 @@ RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = { NULL }; +static RISCVCPUPreferredExtsRule SSCCFG_PREFERRED = { + .ext = CPU_CFG_OFFSET(ext_ssccfg), + .preferred_multi_exts = { + CPU_CFG_OFFSET(ext_smcsrind), CPU_CFG_OFFSET(ext_sscsrind), + CPU_CFG_OFFSET(ext_ssaia), CPU_CFG_OFFSET(ext_smaia), + CPU_CFG_OFFSET(ext_smstateen), CPU_CFG_OFFSET(ext_sscofpmf), + CPU_CFG_OFFSET(ext_smcntrpmf), CPU_CFG_OFFSET(ext_smcdeleg), + + RISCV_PREFRRED_EXTS_RULE_END + }, +}; + RISCVCPUPreferredExtsRule *riscv_multi_ext_preferred_rules[] = { - NULL + &SSCCFG_PREFERRED, NULL }; static Property riscv_cpu_properties[] = {