/* * RISC-V IOMMU - Hardware Performance Monitor (HPM) helpers * * Copyright (C) 2022-2023 Rivos Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, * version 2 or later, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, see . */ #include "qemu/osdep.h" #include "qemu/timer.h" #include "cpu_bits.h" #include "riscv-iommu-hpm.h" #include "riscv-iommu.h" #include "riscv-iommu-bits.h" #include "trace.h" /* For now we assume IOMMU HPM frequency to be 1GHz so 1-cycle is of 1-ns. */ static inline uint64_t get_cycles(void) { return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); } uint64_t riscv_iommu_hpmcycle_read(RISCVIOMMUState *s) { const uint64_t cycle = riscv_iommu_reg_get64( s, RISCV_IOMMU_REG_IOHPMCYCLES); const uint32_t inhibit = riscv_iommu_reg_get32( s, RISCV_IOMMU_REG_IOCOUNTINH); const uint64_t ctr_prev = s->hpmcycle_prev; const uint64_t ctr_val = s->hpmcycle_val; if (get_field(inhibit, RISCV_IOMMU_IOCOUNTINH_CY)) { /* * Counter should not increment if inhibit bit is set. We can't really * stop the QEMU_CLOCK_VIRTUAL, so we just return the last updated * counter value to indicate that counter was not incremented. */ return (ctr_val & RISCV_IOMMU_IOHPMCYCLES_COUNTER) | (cycle & RISCV_IOMMU_IOHPMCYCLES_OVF); } return (ctr_val + get_cycles() - ctr_prev) | (cycle & RISCV_IOMMU_IOHPMCYCLES_OVF); } static void hpm_incr_ctr(RISCVIOMMUState *s, uint32_t ctr_idx) { const uint32_t off = ctr_idx << 3; uint64_t cntr_val; cntr_val = ldq_le_p(&s->regs_rw[RISCV_IOMMU_REG_IOHPMCTR_BASE + off]); stq_le_p(&s->regs_rw[RISCV_IOMMU_REG_IOHPMCTR_BASE + off], cntr_val + 1); /* Handle the overflow scenario. */ if (cntr_val == UINT64_MAX) { /* * Generate interrupt only if OF bit is clear. +1 to offset the cycle * register OF bit. */ const uint32_t ovf = riscv_iommu_reg_mod32(s, RISCV_IOMMU_REG_IOCOUNTOVF, BIT(ctr_idx + 1), 0); if (!get_field(ovf, BIT(ctr_idx + 1))) { riscv_iommu_reg_mod64(s, RISCV_IOMMU_REG_IOHPMEVT_BASE + off, RISCV_IOMMU_IOHPMEVT_OF, 0); riscv_iommu_notify(s, RISCV_IOMMU_INTR_PM); } } } void riscv_iommu_hpm_incr_ctr(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, unsigned event_id) { const uint32_t inhibit = riscv_iommu_reg_get32( s, RISCV_IOMMU_REG_IOCOUNTINH); uint32_t did_gscid; uint32_t pid_pscid; uint32_t ctr_idx; gpointer value; uint32_t ctrs; uint64_t evt; if (!(s->cap & RISCV_IOMMU_CAP_HPM)) { return; } value = g_hash_table_lookup(s->hpm_event_ctr_map, GUINT_TO_POINTER(event_id)); if (value == NULL) { return; } for (ctrs = GPOINTER_TO_UINT(value); ctrs != 0; ctrs &= ctrs - 1) { ctr_idx = ctz32(ctrs); if (get_field(inhibit, BIT(ctr_idx + 1))) { continue; } evt = riscv_iommu_reg_get64(s, RISCV_IOMMU_REG_IOHPMEVT_BASE + (ctr_idx << 3)); /* * It's quite possible that event ID has been changed in counter * but hashtable hasn't been updated yet. We don't want to increment * counter for the old event ID. */ if (event_id != get_field(evt, RISCV_IOMMU_IOHPMEVT_EVENT_ID)) { continue; } if (get_field(evt, RISCV_IOMMU_IOHPMEVT_IDT)) { did_gscid = get_field(ctx->gatp, RISCV_IOMMU_DC_IOHGATP_GSCID); pid_pscid = get_field(ctx->ta, RISCV_IOMMU_DC_TA_PSCID); } else { did_gscid = ctx->devid; pid_pscid = ctx->process_id; } if (get_field(evt, RISCV_IOMMU_IOHPMEVT_PV_PSCV)) { /* * If the transaction does not have a valid process_id, counter * increments if device_id matches DID_GSCID. If the transaction * has a valid process_id, counter increments if device_id * matches DID_GSCID and process_id matches PID_PSCID. See * IOMMU Specification, Chapter 5.23. Performance-monitoring * event selector. */ if (ctx->process_id && get_field(evt, RISCV_IOMMU_IOHPMEVT_PID_PSCID) != pid_pscid) { continue; } } if (get_field(evt, RISCV_IOMMU_IOHPMEVT_DV_GSCV)) { uint32_t mask = ~0; if (get_field(evt, RISCV_IOMMU_IOHPMEVT_DMASK)) { /* * 1001 1011 mask = GSCID * 0000 0111 mask = mask ^ (mask + 1) * 1111 1000 mask = ~mask; */ mask = get_field(evt, RISCV_IOMMU_IOHPMEVT_DID_GSCID); mask = mask ^ (mask + 1); mask = ~mask; } if ((get_field(evt, RISCV_IOMMU_IOHPMEVT_DID_GSCID) & mask) != (did_gscid & mask)) { continue; } } hpm_incr_ctr(s, ctr_idx); } }