From nobody Tue Feb 10 04:58:45 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 168208942107714.557632483700786; Fri, 21 Apr 2023 08:03:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ppsIP-0001Qr-W1; Fri, 21 Apr 2023 11:03:34 -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 1ppquz-0003EM-9q for qemu-devel@nongnu.org; Fri, 21 Apr 2023 09:35:17 -0400 Received: from 60-248-80-70.hinet-ip.hinet.net ([60.248.80.70] helo=Atcsqr.andestech.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ppquw-000667-BF for qemu-devel@nongnu.org; Fri, 21 Apr 2023 09:35:17 -0400 Received: from Atcsqr.andestech.com (localhost [127.0.0.2] (may be forged)) by Atcsqr.andestech.com with ESMTP id 33LDDkwo021691 for ; Fri, 21 Apr 2023 21:13:46 +0800 (+08) (envelope-from peterlin@andestech.com) Received: from mail.andestech.com (ATCPCS16.andestech.com [10.0.1.222]) by Atcsqr.andestech.com with ESMTP id 33LDDTLd021311; Fri, 21 Apr 2023 21:13:29 +0800 (+08) (envelope-from peterlin@andestech.com) Received: from atcfdc88.andestech.com (10.0.15.158) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.498.0; Fri, 21 Apr 2023 21:13:26 +0800 From: Yu Chien Peter Lin To: CC: , , , Yu Chien Peter Lin Subject: [PATCH] target/riscv: Fix PMU node property for virt machine Date: Fri, 21 Apr 2023 21:14:37 +0800 Message-ID: <20230421131437.19036-1-peterlin@andestech.com> X-Mailer: git-send-email 2.38.0.68.ge85701b4af.dirty MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.0.15.158] X-DNSRBL: X-SPAM-SOURCE-CHECK: pass X-MAIL: Atcsqr.andestech.com 33LDDkwo021691 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=60.248.80.70; envelope-from=peterlin@andestech.com; helo=Atcsqr.andestech.com X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, RDNS_DYNAMIC=0.982, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, TVD_RCVD_IP=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 21 Apr 2023 11:03:31 -0400 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-ZM-MESSAGEID: 1682089423610100003 Content-Type: text/plain; charset="utf-8" The length of fdt_event_ctr_map[20] will add 5 dummy cells in "riscv,event-to-mhpmcounters" property, so directly initialize the array without an explicit size. This patch also fixes the typo of PMU cache operation result ID of MISS (0x1) in the comments, and renames event idx 0x10021 to RISCV_PMU_EVENT_CACHE_ITLB_READ_MISS. Signed-off-by: Yu Chien Peter Lin --- $ ./build/qemu-system-riscv64 -M virt,dumpdtb=3D/tmp/virt.dtb -cpu rv64,s= scofpmf=3Don && dtc /tmp/virt.dtb | grep mhpmcounters [...] riscv,event-to-mhpmcounters =3D <0x01 0x01 0x7fff9=20 0x02 0x02 0x7fffc 0x10019 0x10019 0x7fff8 0x1001b 0x1001b 0x7fff8 0x10021 0x10021 0x7fff8 dummy cells ---> 0x00 0x00 0x00 0x00 0x00>; This won't break the OpenSBI, but will cause it to incorrectly increment num_hw_events [1] to 6, and DT validation failure in kernel [2]. $ dt-validate -p Documentation/devicetree/bindings/processed-schema.json = virt.dtb [...] virt.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281], [2, = 2, 524284], [65561, 65561, 524280], [65563, 65563, 524280], [65569, 65569, = 524280], [0, 0, 0], [0, 0]], 'compatible': ['riscv,pmu']} should not be val= id under {'type': 'object'} From schema: /home/peterlin/.local/lib/python3.10/site-packages/d= tschema/schemas/simple-bus.yaml virt.dtb: pmu: riscv,event-to-mhpmcounters:6: [0, 0] is too short [...] [1] https://github.com/riscv-software-src/opensbi/blob/master/lib/sbi/sbi_p= mu.c#L255 [2] https://github.com/torvalds/linux/blob/v6.3-rc7/Documentation/devicetre= e/bindings/perf/riscv%2Cpmu.yaml#L54-L66 --- target/riscv/cpu.h | 2 +- target/riscv/cpu_helper.c | 2 +- target/riscv/pmu.c | 88 +++++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 47 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 638e47c75a..eab518542c 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -812,7 +812,7 @@ enum riscv_pmu_event_idx { RISCV_PMU_EVENT_HW_INSTRUCTIONS =3D 0x02, RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS =3D 0x10019, RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS =3D 0x1001B, - RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS =3D 0x10021, + RISCV_PMU_EVENT_CACHE_ITLB_READ_MISS =3D 0x10021, }; =20 /* CSR function table */ diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index f88c503cf4..5d3e032ec9 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1210,7 +1210,7 @@ static void pmu_tlb_fill_incr_ctr(RISCVCPU *cpu, MMUA= ccessType access_type) =20 switch (access_type) { case MMU_INST_FETCH: - pmu_event_type =3D RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS; + pmu_event_type =3D RISCV_PMU_EVENT_CACHE_ITLB_READ_MISS; break; case MMU_DATA_LOAD: pmu_event_type =3D RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS; diff --git a/target/riscv/pmu.c b/target/riscv/pmu.c index b8e56d2b7b..0b21c3fa38 100644 --- a/target/riscv/pmu.c +++ b/target/riscv/pmu.c @@ -35,51 +35,49 @@ */ void riscv_pmu_generate_fdt_node(void *fdt, int num_ctrs, char *pmu_name) { - uint32_t fdt_event_ctr_map[20] =3D {}; - uint32_t cmask; - /* All the programmable counters can map to any event */ - cmask =3D MAKE_32BIT_MASK(3, num_ctrs); - - /* - * The event encoding is specified in the SBI specification - * Event idx is a 20bits wide number encoded as follows: - * event_idx[19:16] =3D type - * event_idx[15:0] =3D code - * The code field in cache events are encoded as follows: - * event_idx.code[15:3] =3D cache_id - * event_idx.code[2:1] =3D op_id - * event_idx.code[0:0] =3D result_id - */ - - /* SBI_PMU_HW_CPU_CYCLES: 0x01 : type(0x00) */ - fdt_event_ctr_map[0] =3D cpu_to_be32(0x00000001); - fdt_event_ctr_map[1] =3D cpu_to_be32(0x00000001); - fdt_event_ctr_map[2] =3D cpu_to_be32(cmask | 1 << 0); - - /* SBI_PMU_HW_INSTRUCTIONS: 0x02 : type(0x00) */ - fdt_event_ctr_map[3] =3D cpu_to_be32(0x00000002); - fdt_event_ctr_map[4] =3D cpu_to_be32(0x00000002); - fdt_event_ctr_map[5] =3D cpu_to_be32(cmask | 1 << 2); - - /* SBI_PMU_HW_CACHE_DTLB : 0x03 READ : 0x00 MISS : 0x00 type(0x01) */ - fdt_event_ctr_map[6] =3D cpu_to_be32(0x00010019); - fdt_event_ctr_map[7] =3D cpu_to_be32(0x00010019); - fdt_event_ctr_map[8] =3D cpu_to_be32(cmask); - - /* SBI_PMU_HW_CACHE_DTLB : 0x03 WRITE : 0x01 MISS : 0x00 type(0x01) */ - fdt_event_ctr_map[9] =3D cpu_to_be32(0x0001001B); - fdt_event_ctr_map[10] =3D cpu_to_be32(0x0001001B); - fdt_event_ctr_map[11] =3D cpu_to_be32(cmask); - - /* SBI_PMU_HW_CACHE_ITLB : 0x04 READ : 0x00 MISS : 0x00 type(0x01) */ - fdt_event_ctr_map[12] =3D cpu_to_be32(0x00010021); - fdt_event_ctr_map[13] =3D cpu_to_be32(0x00010021); - fdt_event_ctr_map[14] =3D cpu_to_be32(cmask); - - /* This a OpenSBI specific DT property documented in OpenSBI docs */ - qemu_fdt_setprop(fdt, pmu_name, "riscv,event-to-mhpmcounters", - fdt_event_ctr_map, sizeof(fdt_event_ctr_map)); + uint32_t cmask =3D MAKE_32BIT_MASK(3, num_ctrs); + + /* + * The event encoding is specified in the SBI specification + * Event idx is a 20bits wide number encoded as follows: + * event_idx[19:16] =3D type + * event_idx[15:0] =3D code + * The code field in cache events are encoded as follows: + * event_idx.code[15:3] =3D cache_id + * event_idx.code[2:1] =3D op_id + * event_idx.code[0:0] =3D result_id + */ + const uint32_t fdt_event_ctr_map[] =3D { + /* SBI_PMU_HW_CPU_CYCLES: 0x01 : type(0x00) */ + cpu_to_be32(RISCV_PMU_EVENT_HW_CPU_CYCLES), + cpu_to_be32(RISCV_PMU_EVENT_HW_CPU_CYCLES), + cpu_to_be32(cmask | 1 << 0), + + /* SBI_PMU_HW_INSTRUCTIONS: 0x02 : type(0x00) */ + cpu_to_be32(RISCV_PMU_EVENT_HW_INSTRUCTIONS), + cpu_to_be32(RISCV_PMU_EVENT_HW_INSTRUCTIONS), + cpu_to_be32(cmask | 1 << 2), + + /* SBI_PMU_HW_CACHE_DTLB : 0x03 READ : 0x00 MISS : 0x01 type(0x01)= */ + cpu_to_be32(RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS), + cpu_to_be32(RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS), + cpu_to_be32(cmask), + + /* SBI_PMU_HW_CACHE_DTLB : 0x03 WRITE : 0x01 MISS : 0x01 type(0x01= ) */ + cpu_to_be32(RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS), + cpu_to_be32(RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS), + cpu_to_be32(cmask), + + /* SBI_PMU_HW_CACHE_ITLB : 0x04 READ : 0x00 MISS : 0x01 type(0x01)= */ + cpu_to_be32(RISCV_PMU_EVENT_CACHE_ITLB_READ_MISS), + cpu_to_be32(RISCV_PMU_EVENT_CACHE_ITLB_READ_MISS), + cpu_to_be32(cmask), + }; + + /* This a OpenSBI specific DT property documented in OpenSBI docs */ + qemu_fdt_setprop(fdt, pmu_name, "riscv,event-to-mhpmcounters", + fdt_event_ctr_map, sizeof(fdt_event_ctr_map)); } =20 static bool riscv_pmu_counter_valid(RISCVCPU *cpu, uint32_t ctr_idx) @@ -317,7 +315,7 @@ int riscv_pmu_update_event_map(CPURISCVState *env, uint= 64_t value, case RISCV_PMU_EVENT_HW_INSTRUCTIONS: case RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS: case RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS: - case RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS: + case RISCV_PMU_EVENT_CACHE_ITLB_READ_MISS: break; default: /* We don't support any raw events right now */ --=20 2.34.1