qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Alistair Francis <alistair23@gmail.com>
To: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Cc: qemu-devel@nongnu.org, qemu-riscv@nongnu.org,
	alistair.francis@wdc.com,  bmeng@tinylab.org,
	liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com,
	 palmer@rivosinc.com, Tomasz Jeznach <tjeznach@rivosinc.com>
Subject: Re: [PATCH for-10.0 05/11] hw/riscv/riscv-iommu: instantiate hpm_timer
Date: Mon, 24 Feb 2025 12:55:05 +1000	[thread overview]
Message-ID: <CAKmqyKNNJn38v22AEuZo2CkDUDMAaFuyPmyL0dtKcZ2uxPdXtQ@mail.gmail.com> (raw)
In-Reply-To: <20241205133003.184581-6-dbarboza@ventanamicro.com>

On Thu, Dec 5, 2024 at 11:35 PM Daniel Henrique Barboza
<dbarboza@ventanamicro.com> wrote:
>
> From: Tomasz Jeznach <tjeznach@rivosinc.com>
>
> The next HPM related changes requires the HPM overflow timer to be
> initialized by the riscv-iommu base emulation.
>
> Signed-off-by: Tomasz Jeznach <tjeznach@rivosinc.com>
> Signed-off-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>

Acked-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

> ---
>  hw/riscv/riscv-iommu-hpm.c | 36 ++++++++++++++++++++++++++++++++++++
>  hw/riscv/riscv-iommu-hpm.h |  1 +
>  hw/riscv/riscv-iommu.c     |  3 +++
>  hw/riscv/riscv-iommu.h     |  2 ++
>  4 files changed, 42 insertions(+)
>
> diff --git a/hw/riscv/riscv-iommu-hpm.c b/hw/riscv/riscv-iommu-hpm.c
> index 8eca5ee17e..325088333e 100644
> --- a/hw/riscv/riscv-iommu-hpm.c
> +++ b/hw/riscv/riscv-iommu-hpm.c
> @@ -166,3 +166,39 @@ void riscv_iommu_hpm_incr_ctr(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
>          hpm_incr_ctr(s, ctr_idx);
>      }
>  }
> +
> +/* Timer callback for cycle counter overflow. */
> +void riscv_iommu_hpm_timer_cb(void *priv)
> +{
> +    RISCVIOMMUState *s = priv;
> +    const uint32_t inhibit = riscv_iommu_reg_get32(
> +        s, RISCV_IOMMU_REG_IOCOUNTINH);
> +    uint32_t ovf;
> +
> +    if (get_field(inhibit, RISCV_IOMMU_IOCOUNTINH_CY)) {
> +        return;
> +    }
> +
> +    if (s->irq_overflow_left > 0) {
> +        uint64_t irq_trigger_at =
> +            qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->irq_overflow_left;
> +        timer_mod_anticipate_ns(s->hpm_timer, irq_trigger_at);
> +        s->irq_overflow_left = 0;
> +        return;
> +    }
> +
> +    ovf = riscv_iommu_reg_get32(s, RISCV_IOMMU_REG_IOCOUNTOVF);
> +    if (!get_field(ovf, RISCV_IOMMU_IOCOUNTOVF_CY)) {
> +        /*
> +         * We don't need to set hpmcycle_val to zero and update hpmcycle_prev to
> +         * current clock value. The way we calculate iohpmcycs will overflow
> +         * and return the correct value. This avoids the need to synchronize
> +         * timer callback and write callback.
> +         */
> +        riscv_iommu_reg_mod32(s, RISCV_IOMMU_REG_IOCOUNTOVF,
> +            RISCV_IOMMU_IOCOUNTOVF_CY, 0);
> +        riscv_iommu_reg_mod64(s, RISCV_IOMMU_REG_IOHPMCYCLES,
> +            RISCV_IOMMU_IOHPMCYCLES_OVF, 0);
> +        riscv_iommu_notify(s, RISCV_IOMMU_INTR_PM);
> +    }
> +}
> diff --git a/hw/riscv/riscv-iommu-hpm.h b/hw/riscv/riscv-iommu-hpm.h
> index 411d869dce..cd896d3b7c 100644
> --- a/hw/riscv/riscv-iommu-hpm.h
> +++ b/hw/riscv/riscv-iommu-hpm.h
> @@ -25,5 +25,6 @@
>  uint64_t riscv_iommu_hpmcycle_read(RISCVIOMMUState *s);
>  void riscv_iommu_hpm_incr_ctr(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
>                                unsigned event_id);
> +void riscv_iommu_hpm_timer_cb(void *priv);
>
>  #endif
> diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c
> index 5ce0d24359..2ec388ff3d 100644
> --- a/hw/riscv/riscv-iommu.c
> +++ b/hw/riscv/riscv-iommu.c
> @@ -2281,6 +2281,8 @@ static void riscv_iommu_realize(DeviceState *dev, Error **errp)
>      address_space_init(&s->trap_as, &s->trap_mr, "riscv-iommu-trap-as");
>
>      if (s->cap & RISCV_IOMMU_CAP_HPM) {
> +        s->hpm_timer =
> +            timer_new_ns(QEMU_CLOCK_VIRTUAL, riscv_iommu_hpm_timer_cb, s);
>          s->hpm_event_ctr_map = g_hash_table_new(g_direct_hash, g_direct_equal);
>      }
>  }
> @@ -2294,6 +2296,7 @@ static void riscv_iommu_unrealize(DeviceState *dev)
>
>      if (s->cap & RISCV_IOMMU_CAP_HPM) {
>          g_hash_table_unref(s->hpm_event_ctr_map);
> +        timer_free(s->hpm_timer);
>      }
>  }
>
> diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h
> index a21ab51491..6ddc59f474 100644
> --- a/hw/riscv/riscv-iommu.h
> +++ b/hw/riscv/riscv-iommu.h
> @@ -88,8 +88,10 @@ struct RISCVIOMMUState {
>      QLIST_HEAD(, RISCVIOMMUSpace) spaces;
>
>      /* HPM cycle counter */
> +    QEMUTimer *hpm_timer;
>      uint64_t hpmcycle_val;      /* Current value of cycle register */
>      uint64_t hpmcycle_prev;     /* Saved value of QEMU_CLOCK_VIRTUAL clock */
> +    uint64_t irq_overflow_left; /* Value beyond INT64_MAX after overflow */
>
>      /* HPM event counters */
>      GHashTable *hpm_event_ctr_map; /* Mapping of events to counters */
> --
> 2.47.1
>
>


  reply	other threads:[~2025-02-24  2:56 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-05 13:29 [PATCH for-10.0 00/11] riscv: IOMMU HPM support Daniel Henrique Barboza
2024-12-05 13:29 ` [PATCH for-10.0 01/11] hw/riscv/riscv-iommu.h: add missing headers Daniel Henrique Barboza
2025-02-24  2:08   ` Alistair Francis
2024-12-05 13:29 ` [PATCH for-10.0 02/11] hw/riscv/riscv-iommu-bits.h: HPM bits Daniel Henrique Barboza
2025-02-24  2:10   ` Alistair Francis
2024-12-05 13:29 ` [PATCH for-10.0 03/11] hw/riscv/riscv-iommu: add riscv-iommu-hpm file Daniel Henrique Barboza
2025-02-24  2:16   ` Alistair Francis
2024-12-05 13:29 ` [PATCH for-10.0 04/11] hw/riscv/riscv-iommu: add riscv_iommu_hpm_incr_ctr() Daniel Henrique Barboza
2025-02-24  2:50   ` Alistair Francis
2024-12-05 13:29 ` [PATCH for-10.0 05/11] hw/riscv/riscv-iommu: instantiate hpm_timer Daniel Henrique Barboza
2025-02-24  2:55   ` Alistair Francis [this message]
2024-12-05 13:29 ` [PATCH for-10.0 06/11] hw/riscv/riscv-iommu: add IOCOUNTINH mmio writes Daniel Henrique Barboza
2025-02-24  3:04   ` Alistair Francis
2024-12-05 13:29 ` [PATCH for-10.0 07/11] hw/riscv/riscv-iommu: add IOHPMCYCLES mmio write Daniel Henrique Barboza
2025-02-24  3:06   ` Alistair Francis
2024-12-05 13:30 ` [PATCH for-10.0 08/11] hw/riscv/riscv-iommu: add hpm events " Daniel Henrique Barboza
2025-02-24  3:31   ` Alistair Francis
2024-12-05 13:30 ` [PATCH for-10.0 09/11] hw/riscv/riscv-iommu.c: add RISCV_IOMMU_CAP_HPM cap Daniel Henrique Barboza
2025-02-24  3:32   ` Alistair Francis
2024-12-05 13:30 ` [PATCH for-10.0 10/11] hw/riscv: add IOMMU HPM trace events Daniel Henrique Barboza
2025-02-24  3:33   ` Alistair Francis
2024-12-05 13:30 ` [PATCH for-10.0 11/11] docs/specs/riscv-iommu.rst: add HPM support info Daniel Henrique Barboza
2025-02-24  3:34   ` Alistair Francis
2025-02-24  3:59 ` [PATCH for-10.0 00/11] riscv: IOMMU HPM support Alistair Francis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAKmqyKNNJn38v22AEuZo2CkDUDMAaFuyPmyL0dtKcZ2uxPdXtQ@mail.gmail.com \
    --to=alistair23@gmail.com \
    --cc=alistair.francis@wdc.com \
    --cc=bmeng@tinylab.org \
    --cc=dbarboza@ventanamicro.com \
    --cc=liwei1518@gmail.com \
    --cc=palmer@rivosinc.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=tjeznach@rivosinc.com \
    --cc=zhiwei_liu@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).