From nobody Tue Feb 10 01:34:35 2026 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=1659483578; cv=none; d=zohomail.com; s=zohoarc; b=X9SvM1sLbbJLhN0tzJYwUVncQyjHHIgVewDoxOQ/wnQBTHggXirkua1Qbh9Zt5Sh7tTNqUHwULC0Cp1bOxpFOF06gOtfI07Ztp+vxgDGDQVljPxaHPWqOHNRe72I9cIII8KwLtxHTLKghO4tMYfbI+/5n8o6Q4HK5rVazBAhOCk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1659483578; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=STW5zgSAnPfM9o5zUiWG83iqfjpuSrH8mknbFxyp28g=; b=JGcm8Dr5UfOy29Oz8EoYxiR8IPdgMoHz8I4RAxZ672c9iH5u9m/NnXXjBfCGJowtzN3nWZoi70K6ckIQwkpOhZ/SdTXMQfAPjBIwqaY63zg+CkYo8eHq1SHw8XE8yY7/NfTqR41S5lutcABMGbXk2e7AzftClQBf1dCscuRAWd8= 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 1659483578878284.37748799229814; Tue, 2 Aug 2022 16:39:38 -0700 (PDT) Received: from localhost ([::1]:53646 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oJ1U8-0003Cw-Ho for importer@patchew.org; Tue, 02 Aug 2022 19:39:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45300) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oJ1O6-0003Gu-VF for qemu-devel@nongnu.org; Tue, 02 Aug 2022 19:33:22 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]:37772) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oJ1O3-0007mF-ID for qemu-devel@nongnu.org; Tue, 02 Aug 2022 19:33:22 -0400 Received: by mail-pl1-x633.google.com with SMTP id m2so8072735pls.4 for ; Tue, 02 Aug 2022 16:33:19 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id s10-20020a170902a50a00b001636d95fe59sm250543plq.172.2022.08.02.16.33.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Aug 2022 16:33:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=STW5zgSAnPfM9o5zUiWG83iqfjpuSrH8mknbFxyp28g=; b=F19CmX5NUWdyZ/3RDr3bzmDOC1BTWCkp2fPwuk3W0qbo0LgFgQ2an2Uz/en+ZSKfiK vbD4xOt4cI9vwH0LD2PVn60lepojBmvhMx2FsnACEOhnq6KE6xOjmnKdC79MRrjAb25S mHxBi9dUtn4uRd4c9wdwOPYezTz6eOplvZJI9uIxLc/Ifkk4v0G6CNcCyeGKLBXBSV4R hggc8gneWjocqzwfy7uXCtc3zsy43+QuXqO3vEpqyCPxKkefFby0yHk3/pFE6wOCbgSE jph5Rasq1P1hsn6P+pmxfNHFPFJ+t7VCoXET3dVvKiK/xZlJmENJKSCkon5lfknh7++W JxkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=STW5zgSAnPfM9o5zUiWG83iqfjpuSrH8mknbFxyp28g=; b=K/mdOQ/25ZVsFZevT2V7A27zsM2raznMumSm0NYSxROTNYW/gmnvcljGsMreTWLfKl ALcppLCQqcSCDl7ZDPMMO8Ki+wvisIGZXbw2DUaKZ9N5wXf2yPdbjxTRzr0kof4FbkuL eIUhNS/Toe9GC/vRh0TFq3214iulMulDjGEyzEycjLDNE/zx0v9oMW/TdT2oJFdEehUD shZCJErgthWWBLF2hsRWlt/XUa6c+kT8Ql7UgHcmgecW/Me5CbJFXVgq1lxPXxy2FQqV UlGmsXCcN2IFqq28VBBXM2du1q5BP8r2VbAgQK9ognHpGGoKS8IMSrdvdYRo33ElO++V +2sA== X-Gm-Message-State: ACgBeo19kVPTjd4WZ+XAx6cWaUF7k+l9lJ2qz98VZQB9WEZ/qgub6qe7 /xzb1UaZaI7GwNV1Ia5He2DxMdKBUiJ3Kw== X-Google-Smtp-Source: AA6agR7cwJRLFI4EL0A+iR6t0SfJxrR/tgqZmkXSRV1/yAWARBLVrvN20Y7Df1+L2gzqEsORt8Pcxw== X-Received: by 2002:a17:902:6b0c:b0:16e:de0e:9d0e with SMTP id o12-20020a1709026b0c00b0016ede0e9d0emr15184665plk.30.1659483198158; Tue, 02 Aug 2022 16:33:18 -0700 (PDT) From: Atish Patra To: qemu-devel@nongnu.org Cc: Alistair Francis , Atish Patra , Bin Meng , Palmer Dabbelt , qemu-riscv@nongnu.org Subject: [PATCH v12 4/6] hw/riscv: virt: Add PMU DT node to the device tree Date: Tue, 2 Aug 2022 16:33:05 -0700 Message-Id: <20220802233307.2106839-5-atishp@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220802233307.2106839-1-atishp@rivosinc.com> References: <20220802233307.2106839-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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::633; envelope-from=atishp@rivosinc.com; helo=mail-pl1-x633.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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @rivosinc-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1659483581033100001 Content-Type: text/plain; charset="utf-8" Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events. Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well. Acked-by: Alistair Francis Signed-off-by: Atish Patra Signed-off-by: Atish Patra --- hw/riscv/virt.c | 16 +++++++++++++ target/riscv/pmu.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++ target/riscv/pmu.h | 1 + 3 files changed, 74 insertions(+) diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c index f2ce5663a4c7..b58e6ed8b823 100644 --- a/hw/riscv/virt.c +++ b/hw/riscv/virt.c @@ -30,6 +30,7 @@ #include "hw/char/serial.h" #include "target/riscv/cpu.h" #include "hw/core/sysbus-fdt.h" +#include "target/riscv/pmu.h" #include "hw/riscv/riscv_hart.h" #include "hw/riscv/virt.h" #include "hw/riscv/boot.h" @@ -715,6 +716,20 @@ static void create_fdt_socket_aplic(RISCVVirtState *s, aplic_phandles[socket] =3D aplic_s_phandle; } =20 +static void create_fdt_pmu(RISCVVirtState *s) +{ + char *pmu_name; + MachineState *mc =3D MACHINE(s); + RISCVCPU hart =3D s->soc[0].harts[0]; + + pmu_name =3D g_strdup_printf("/soc/pmu"); + qemu_fdt_add_subnode(mc->fdt, pmu_name); + qemu_fdt_setprop_string(mc->fdt, pmu_name, "compatible", "riscv,pmu"); + riscv_pmu_generate_fdt_node(mc->fdt, hart.cfg.pmu_num, pmu_name); + + g_free(pmu_name); +} + static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memma= p, bool is_32_bit, uint32_t *phandle, uint32_t *irq_mmio_phandle, @@ -1043,6 +1058,7 @@ static void create_fdt(RISCVVirtState *s, const MemMa= pEntry *memmap, =20 create_fdt_flash(s, memmap); create_fdt_fw_cfg(s, memmap); + create_fdt_pmu(s); =20 update_bootargs: if (cmdline && *cmdline) { diff --git a/target/riscv/pmu.c b/target/riscv/pmu.c index 34096941c0ce..59feb3c243dd 100644 --- a/target/riscv/pmu.c +++ b/target/riscv/pmu.c @@ -20,11 +20,68 @@ #include "cpu.h" #include "pmu.h" #include "sysemu/cpu-timers.h" +#include "sysemu/device_tree.h" =20 #define RISCV_TIMEBASE_FREQ 1000000000 /* 1Ghz */ #define MAKE_32BIT_MASK(shift, length) \ (((uint32_t)(~0UL) >> (32 - (length))) << (shift)) =20 +/** + * To keep it simple, any event can be mapped to any programmable counters= in + * QEMU. The generic cycle & instruction count events can also be monitored + * using programmable counters. In that case, mcycle & minstret must conti= nue + * to provide the correct value as well. Heterogeneous PMU per hart is not + * supported yet. Thus, number of counters are same across all harts. + */ +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)); +} + static bool riscv_pmu_counter_valid(RISCVCPU *cpu, uint32_t ctr_idx) { if (ctr_idx < 3 || ctr_idx >=3D RV_MAX_MHPMCOUNTERS || diff --git a/target/riscv/pmu.h b/target/riscv/pmu.h index 036653627f78..3004ce37b636 100644 --- a/target/riscv/pmu.h +++ b/target/riscv/pmu.h @@ -31,5 +31,6 @@ int riscv_pmu_init(RISCVCPU *cpu, int num_counters); int riscv_pmu_update_event_map(CPURISCVState *env, uint64_t value, uint32_t ctr_idx); int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx); +void riscv_pmu_generate_fdt_node(void *fdt, int num_counters, char *pmu_na= me); int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx); --=20 2.25.1