From nobody Mon Nov 25 02:12:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1719446485; cv=none; d=zohomail.com; s=zohoarc; b=igLz2YrBfET4d7Ae+SEPMC1i8gIlDl7C9lOkXPlDtQBnySgKDy3eFpmID570o5Qk+XXqB+gPeSiUd0F9UyKbpksXbxPlEYkHUXsyIeRjhh7bGFO3MicBEe/O2w2gI/Fd5R8M7+/c9Tbh1cfXjWnLzMUfCWpRVAaJq2NWW5Z+/Rc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1719446485; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=z+xq4MaVCyGPtCT3I9EtIqPDhGf+/u6LMxxHtdn+yrc=; b=MHwUZ+A3+kg5ExLdrx8fcZPInOGmEMA5Byo9SzAI1URCTUJM0B752hSw9Pylw4b7D4TBrD3eQN88t2JvbIrgAEkeYdRBvWbSxG2VZB+Ru+nlkTbkCL/nESS40zMTMcH5bXOHNaEUS2Rn0gSwKf6pnq6DqAoYlY5wMH0WKz8VrQw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1719446485905341.0151355642204; Wed, 26 Jun 2024 17:01:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sMcWP-0007qi-Dn; Wed, 26 Jun 2024 19:57:53 -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 1sMcWM-0007o1-5Y for qemu-devel@nongnu.org; Wed, 26 Jun 2024 19:57:50 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sMcWK-0005Y9-BC for qemu-devel@nongnu.org; Wed, 26 Jun 2024 19:57:49 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1f9a78c6c5dso57000745ad.1 for ; Wed, 26 Jun 2024 16:57:47 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1faac998398sm630965ad.225.2024.06.26.16.57.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jun 2024 16:57:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719446267; x=1720051067; 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=z+xq4MaVCyGPtCT3I9EtIqPDhGf+/u6LMxxHtdn+yrc=; b=XzfCAmlAqdqO8sOTzqfEXzhpq+G0hfruK6gSk45p2Muov+BruSvvCnDt0VpctNsgAp nUYK9p/bRTinlx6fTU/x/O6oLHOvaFaj17tNv+96l13P7BRn/TG4GaDFF9Hef64shOLj FOcj7M0+qbZU7u4QkWLVr3E7qrOdHhw3TO5iAAIURC3QkxPqaq837Rj2olq6J2016dby m3pArJZD8uaLKLNfgUDx0Hded8pKX/z+OJqQ+g7GUrRNpLugfQCOOQMVogVs0gtk+lwt RXyk+cKuuizByIy3I5lm1VskJ1figj6jSmYJV/baT0QFmkZOL/aerbgTjPd2CSXFpEdH o2Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719446267; x=1720051067; 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=z+xq4MaVCyGPtCT3I9EtIqPDhGf+/u6LMxxHtdn+yrc=; b=ChocA8ugwo5+dcF4dL51bH+lVkbMTAMFLluBxHjz5SeGr6TpAjnyfYKdTimx2xOMbC lUJF1DDBCt/cBAfLfuoOwZi1B+TZRu0+OBeKZeU23IXH8pPabG7Lz9Dr+sIqoazIEBIX bOgXSAJV+VN5NLlhXdUAkpb8nqsLalxMlz5yb5uIJ0OIRUKbdPW3ZtHd7wK8b5KXEjjH 1ZpWlJJFijdXR6Q4fUFkeTxQiXfG1TXJpgNFo6S/b5sK3CVVBNdFymWsCGuqkxOkkcvQ 0lPCXL/d0K/fkws4cMIj+8YQfcpNH2FABNEEWnyvw40lM9zxdEUN+635LOxJVAKn6yG5 OAGA== X-Forwarded-Encrypted: i=1; AJvYcCXB7ZSEhZ/BYWSVEr3HFpiAKWxrMmHAYRu1p7eJ1y+QrsvOetdSG35JYjgBX+Jw1dCi8MoZLPYWBA7SKh86yOqD/HHQMfU= X-Gm-Message-State: AOJu0YzqnPt03r9W8Zx03dTc1F9eFvfOYF8r3AZTJRk+LfyDOHfRJXHf JUKpERNph8UTIHttIP1+xd15GmvYqgLS10ODe5lei4huK8Fkfl6+60ryURGW+XI= X-Google-Smtp-Source: AGHT+IFd0kvcg70ASNvT9W/l4oS7i8oziE9iPNdCy+gbqCxLMApNC6T8bFmiKQRbbm4pL17EYMFVHg== X-Received: by 2002:a17:902:f54c:b0:1fa:ff8:e66e with SMTP id d9443c01a7336-1fa23f1f418mr137801435ad.59.1719446267050; Wed, 26 Jun 2024 16:57:47 -0700 (PDT) From: Atish Patra Date: Wed, 26 Jun 2024 16:57:29 -0700 Subject: [PATCH v7 09/11] target/riscv: Start counters from both mhpmcounter and mcountinhibit MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240626-smcntrpmf_v7-v7-9-bb0f10af7fa9@rivosinc.com> References: <20240626-smcntrpmf_v7-v7-0-bb0f10af7fa9@rivosinc.com> In-Reply-To: <20240626-smcntrpmf_v7-v7-0-bb0f10af7fa9@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Rajnesh Kanwal , Atish Patra , 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 (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=atishp@rivosinc.com; helo=mail-pl1-x632.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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1719446487200100003 From: Rajnesh Kanwal Currently we start timer counter from write_mhpmcounter path only without checking for mcountinhibit bit. This changes adds mcountinhibit check and also programs the counter from write_mcountinhibit as well. When a counter is stopped using mcountinhibit we simply update the value of the counter based on current host ticks and save it for future reads. We don't need to disable running timer as pmu_timer_trigger_irq will discard the interrupt if the counter has been inhibited. Signed-off-by: Rajnesh Kanwal Reviewed-by: Daniel Henrique Barboza --- target/riscv/csr.c | 75 ++++++++++++++++++++++++++++++++++++++------------= ---- target/riscv/pmu.c | 3 +-- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 6c1a884eec82..150e02f080ec 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -1008,8 +1008,9 @@ static RISCVException write_mhpmcounter(CPURISCVState= *env, int csrno, uint64_t mhpmctr_val =3D val; =20 counter->mhpmcounter_val =3D val; - if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || - riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { + if (!get_field(env->mcountinhibit, BIT(ctr_idx)) && + (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || + riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) { counter->mhpmcounter_prev =3D riscv_pmu_ctr_get_fixed_counters_val= (env, ctr_idx, f= alse); if (ctr_idx > 2) { @@ -1037,8 +1038,9 @@ static RISCVException write_mhpmcounterh(CPURISCVStat= e *env, int csrno, =20 counter->mhpmcounterh_val =3D val; mhpmctr_val =3D mhpmctr_val | (mhpmctrh_val << 32); - if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || - riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { + if (!get_field(env->mcountinhibit, BIT(ctr_idx)) && + (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || + riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) { counter->mhpmcounterh_prev =3D riscv_pmu_ctr_get_fixed_counters_va= l(env, ctr_idx, = true); if (ctr_idx > 2) { @@ -2101,31 +2103,60 @@ static RISCVException write_mcountinhibit(CPURISCVS= tate *env, int csrno, int cidx; PMUCTRState *counter; RISCVCPU *cpu =3D env_archcpu(env); + uint32_t present_ctrs =3D cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTER= EN_IR; + target_ulong updated_ctrs =3D (env->mcountinhibit ^ val) & present_ctr= s; + uint64_t mhpmctr_val, prev_count, curr_count; =20 /* WARL register - disable unavailable counters; TM bit is always 0 */ - env->mcountinhibit =3D - val & (cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_IR); + env->mcountinhibit =3D val & present_ctrs; =20 /* Check if any other counter is also monitoring cycles/instructions */ for (cidx =3D 0; cidx < RV_MAX_MHPMCOUNTERS; cidx++) { - counter =3D &env->pmu_ctrs[cidx]; - if (get_field(env->mcountinhibit, BIT(cidx)) && (val & BIT(cidx)))= { - /* - * Update the counter value for cycle/instret as we can't stop= the - * host ticks. But we should show the current value at this mo= ment. - */ - if (riscv_pmu_ctr_monitor_cycles(env, cidx) || - riscv_pmu_ctr_monitor_instructions(env, cidx)) { - counter->mhpmcounter_val =3D - riscv_pmu_ctr_get_fixed_counters_val(env, cidx, false)= - - counter->mhpmcounter_prev + - counter->mhpmcounter_val; + if (!(updated_ctrs & BIT(cidx)) || + (!riscv_pmu_ctr_monitor_cycles(env, cidx) && + !riscv_pmu_ctr_monitor_instructions(env, cidx))) { + continue; + } + + counter =3D &env->pmu_ctrs[cidx]; + + if (!get_field(env->mcountinhibit, BIT(cidx))) { + counter->mhpmcounter_prev =3D + riscv_pmu_ctr_get_fixed_counters_val(env, cidx, false); + if (riscv_cpu_mxl(env) =3D=3D MXL_RV32) { + counter->mhpmcounterh_prev =3D + riscv_pmu_ctr_get_fixed_counters_val(env, cidx, true); + } + + if (cidx > 2) { + mhpmctr_val =3D counter->mhpmcounter_val; if (riscv_cpu_mxl(env) =3D=3D MXL_RV32) { - counter->mhpmcounterh_val =3D - riscv_pmu_ctr_get_fixed_counters_val(env, cidx, tr= ue) - - counter->mhpmcounterh_prev= + - counter->mhpmcounterh_val; + mhpmctr_val =3D mhpmctr_val | + ((uint64_t)counter->mhpmcounterh_val << 32); } + riscv_pmu_setup_timer(env, mhpmctr_val, cidx); + } + } else { + curr_count =3D riscv_pmu_ctr_get_fixed_counters_val(env, cidx,= false); + + mhpmctr_val =3D counter->mhpmcounter_val; + prev_count =3D counter->mhpmcounter_prev; + if (riscv_cpu_mxl(env) =3D=3D MXL_RV32) { + uint64_t tmp =3D + riscv_pmu_ctr_get_fixed_counters_val(env, cidx, true); + + curr_count =3D curr_count | (tmp << 32); + mhpmctr_val =3D mhpmctr_val | + ((uint64_t)counter->mhpmcounterh_val << 32); + prev_count =3D prev_count | + ((uint64_t)counter->mhpmcounterh_prev << 32); + } + + /* Adjust the counter for later reads. */ + mhpmctr_val =3D curr_count - prev_count + mhpmctr_val; + counter->mhpmcounter_val =3D mhpmctr_val; + if (riscv_cpu_mxl(env) =3D=3D MXL_RV32) { + counter->mhpmcounterh_val =3D mhpmctr_val >> 32; } } } diff --git a/target/riscv/pmu.c b/target/riscv/pmu.c index ac648cff8d7c..63420d9f3679 100644 --- a/target/riscv/pmu.c +++ b/target/riscv/pmu.c @@ -285,8 +285,7 @@ int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_ev= ent_idx event_idx) } =20 ctr_idx =3D GPOINTER_TO_UINT(value); - if (!riscv_pmu_counter_enabled(cpu, ctr_idx) || - get_field(env->mcountinhibit, BIT(ctr_idx))) { + if (!riscv_pmu_counter_enabled(cpu, ctr_idx)) { return -1; } =20 --=20 2.34.1