xref: /qemu/hw/riscv/riscv-iommu-hpm.c (revision ffb37df056490b34cecc7958bf5f83fe5497b2d4)
14faea7e0STomasz Jeznach /*
24faea7e0STomasz Jeznach  * RISC-V IOMMU - Hardware Performance Monitor (HPM) helpers
34faea7e0STomasz Jeznach  *
44faea7e0STomasz Jeznach  * Copyright (C) 2022-2023 Rivos Inc.
54faea7e0STomasz Jeznach  *
64faea7e0STomasz Jeznach  * This program is free software; you can redistribute it and/or modify it
74faea7e0STomasz Jeznach  * under the terms and conditions of the GNU General Public License,
84faea7e0STomasz Jeznach  * version 2 or later, as published by the Free Software Foundation.
94faea7e0STomasz Jeznach  *
104faea7e0STomasz Jeznach  * This program is distributed in the hope that it will be useful,
114faea7e0STomasz Jeznach  * but WITHOUT ANY WARRANTY; without even the implied warranty of
124faea7e0STomasz Jeznach  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
134faea7e0STomasz Jeznach  * GNU General Public License for more details.
144faea7e0STomasz Jeznach  *
154faea7e0STomasz Jeznach  * You should have received a copy of the GNU General Public License along
164faea7e0STomasz Jeznach  * with this program; if not, see <http://www.gnu.org/licenses/>.
174faea7e0STomasz Jeznach  */
184faea7e0STomasz Jeznach 
194faea7e0STomasz Jeznach #include "qemu/osdep.h"
204faea7e0STomasz Jeznach #include "qemu/timer.h"
214faea7e0STomasz Jeznach #include "cpu_bits.h"
224faea7e0STomasz Jeznach #include "riscv-iommu-hpm.h"
234faea7e0STomasz Jeznach #include "riscv-iommu.h"
244faea7e0STomasz Jeznach #include "riscv-iommu-bits.h"
254faea7e0STomasz Jeznach #include "trace.h"
264faea7e0STomasz Jeznach 
274faea7e0STomasz Jeznach /* For now we assume IOMMU HPM frequency to be 1GHz so 1-cycle is of 1-ns. */
284faea7e0STomasz Jeznach static inline uint64_t get_cycles(void)
294faea7e0STomasz Jeznach {
304faea7e0STomasz Jeznach     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
314faea7e0STomasz Jeznach }
324faea7e0STomasz Jeznach 
334faea7e0STomasz Jeznach uint64_t riscv_iommu_hpmcycle_read(RISCVIOMMUState *s)
344faea7e0STomasz Jeznach {
354faea7e0STomasz Jeznach     const uint64_t cycle = riscv_iommu_reg_get64(
364faea7e0STomasz Jeznach         s, RISCV_IOMMU_REG_IOHPMCYCLES);
374faea7e0STomasz Jeznach     const uint32_t inhibit = riscv_iommu_reg_get32(
384faea7e0STomasz Jeznach         s, RISCV_IOMMU_REG_IOCOUNTINH);
394faea7e0STomasz Jeznach     const uint64_t ctr_prev = s->hpmcycle_prev;
404faea7e0STomasz Jeznach     const uint64_t ctr_val = s->hpmcycle_val;
414faea7e0STomasz Jeznach 
424faea7e0STomasz Jeznach     if (get_field(inhibit, RISCV_IOMMU_IOCOUNTINH_CY)) {
434faea7e0STomasz Jeznach         /*
444faea7e0STomasz Jeznach          * Counter should not increment if inhibit bit is set. We can't really
454faea7e0STomasz Jeznach          * stop the QEMU_CLOCK_VIRTUAL, so we just return the last updated
464faea7e0STomasz Jeznach          * counter value to indicate that counter was not incremented.
474faea7e0STomasz Jeznach          */
484faea7e0STomasz Jeznach         return (ctr_val & RISCV_IOMMU_IOHPMCYCLES_COUNTER) |
494faea7e0STomasz Jeznach                (cycle & RISCV_IOMMU_IOHPMCYCLES_OVF);
504faea7e0STomasz Jeznach     }
514faea7e0STomasz Jeznach 
524faea7e0STomasz Jeznach     return (ctr_val + get_cycles() - ctr_prev) |
534faea7e0STomasz Jeznach         (cycle & RISCV_IOMMU_IOHPMCYCLES_OVF);
544faea7e0STomasz Jeznach }
5511ecf24cSTomasz Jeznach 
5611ecf24cSTomasz Jeznach static void hpm_incr_ctr(RISCVIOMMUState *s, uint32_t ctr_idx)
5711ecf24cSTomasz Jeznach {
5811ecf24cSTomasz Jeznach     const uint32_t off = ctr_idx << 3;
5911ecf24cSTomasz Jeznach     uint64_t cntr_val;
6011ecf24cSTomasz Jeznach 
6111ecf24cSTomasz Jeznach     cntr_val = ldq_le_p(&s->regs_rw[RISCV_IOMMU_REG_IOHPMCTR_BASE + off]);
6211ecf24cSTomasz Jeznach     stq_le_p(&s->regs_rw[RISCV_IOMMU_REG_IOHPMCTR_BASE + off], cntr_val + 1);
6311ecf24cSTomasz Jeznach 
6411ecf24cSTomasz Jeznach     /* Handle the overflow scenario. */
6511ecf24cSTomasz Jeznach     if (cntr_val == UINT64_MAX) {
6611ecf24cSTomasz Jeznach         /*
6711ecf24cSTomasz Jeznach          * Generate interrupt only if OF bit is clear. +1 to offset the cycle
6811ecf24cSTomasz Jeznach          * register OF bit.
6911ecf24cSTomasz Jeznach          */
7011ecf24cSTomasz Jeznach         const uint32_t ovf =
7111ecf24cSTomasz Jeznach             riscv_iommu_reg_mod32(s, RISCV_IOMMU_REG_IOCOUNTOVF,
7211ecf24cSTomasz Jeznach                                   BIT(ctr_idx + 1), 0);
7311ecf24cSTomasz Jeznach         if (!get_field(ovf, BIT(ctr_idx + 1))) {
7411ecf24cSTomasz Jeznach             riscv_iommu_reg_mod64(s,
7511ecf24cSTomasz Jeznach                                   RISCV_IOMMU_REG_IOHPMEVT_BASE + off,
7611ecf24cSTomasz Jeznach                                   RISCV_IOMMU_IOHPMEVT_OF,
7711ecf24cSTomasz Jeznach                                   0);
7811ecf24cSTomasz Jeznach             riscv_iommu_notify(s, RISCV_IOMMU_INTR_PM);
7911ecf24cSTomasz Jeznach         }
8011ecf24cSTomasz Jeznach     }
8111ecf24cSTomasz Jeznach }
8211ecf24cSTomasz Jeznach 
8311ecf24cSTomasz Jeznach void riscv_iommu_hpm_incr_ctr(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
8411ecf24cSTomasz Jeznach                               unsigned event_id)
8511ecf24cSTomasz Jeznach {
8611ecf24cSTomasz Jeznach     const uint32_t inhibit = riscv_iommu_reg_get32(
8711ecf24cSTomasz Jeznach         s, RISCV_IOMMU_REG_IOCOUNTINH);
8811ecf24cSTomasz Jeznach     uint32_t did_gscid;
8911ecf24cSTomasz Jeznach     uint32_t pid_pscid;
9011ecf24cSTomasz Jeznach     uint32_t ctr_idx;
9111ecf24cSTomasz Jeznach     gpointer value;
9211ecf24cSTomasz Jeznach     uint32_t ctrs;
9311ecf24cSTomasz Jeznach     uint64_t evt;
9411ecf24cSTomasz Jeznach 
9511ecf24cSTomasz Jeznach     if (!(s->cap & RISCV_IOMMU_CAP_HPM)) {
9611ecf24cSTomasz Jeznach         return;
9711ecf24cSTomasz Jeznach     }
9811ecf24cSTomasz Jeznach 
9911ecf24cSTomasz Jeznach     value = g_hash_table_lookup(s->hpm_event_ctr_map,
10011ecf24cSTomasz Jeznach                                 GUINT_TO_POINTER(event_id));
10111ecf24cSTomasz Jeznach     if (value == NULL) {
10211ecf24cSTomasz Jeznach         return;
10311ecf24cSTomasz Jeznach     }
10411ecf24cSTomasz Jeznach 
10511ecf24cSTomasz Jeznach     for (ctrs = GPOINTER_TO_UINT(value); ctrs != 0; ctrs &= ctrs - 1) {
10611ecf24cSTomasz Jeznach         ctr_idx = ctz32(ctrs);
10711ecf24cSTomasz Jeznach         if (get_field(inhibit, BIT(ctr_idx + 1))) {
10811ecf24cSTomasz Jeznach             continue;
10911ecf24cSTomasz Jeznach         }
11011ecf24cSTomasz Jeznach 
11111ecf24cSTomasz Jeznach         evt = riscv_iommu_reg_get64(s,
11211ecf24cSTomasz Jeznach             RISCV_IOMMU_REG_IOHPMEVT_BASE + (ctr_idx << 3));
11311ecf24cSTomasz Jeznach 
11411ecf24cSTomasz Jeznach         /*
11511ecf24cSTomasz Jeznach          * It's quite possible that event ID has been changed in counter
11611ecf24cSTomasz Jeznach          * but hashtable hasn't been updated yet. We don't want to increment
11711ecf24cSTomasz Jeznach          * counter for the old event ID.
11811ecf24cSTomasz Jeznach          */
11911ecf24cSTomasz Jeznach         if (event_id != get_field(evt, RISCV_IOMMU_IOHPMEVT_EVENT_ID)) {
12011ecf24cSTomasz Jeznach             continue;
12111ecf24cSTomasz Jeznach         }
12211ecf24cSTomasz Jeznach 
12311ecf24cSTomasz Jeznach         if (get_field(evt, RISCV_IOMMU_IOHPMEVT_IDT)) {
12411ecf24cSTomasz Jeznach             did_gscid = get_field(ctx->gatp, RISCV_IOMMU_DC_IOHGATP_GSCID);
12511ecf24cSTomasz Jeznach             pid_pscid = get_field(ctx->ta, RISCV_IOMMU_DC_TA_PSCID);
12611ecf24cSTomasz Jeznach         } else {
12711ecf24cSTomasz Jeznach             did_gscid = ctx->devid;
12811ecf24cSTomasz Jeznach             pid_pscid = ctx->process_id;
12911ecf24cSTomasz Jeznach         }
13011ecf24cSTomasz Jeznach 
13111ecf24cSTomasz Jeznach         if (get_field(evt, RISCV_IOMMU_IOHPMEVT_PV_PSCV)) {
13211ecf24cSTomasz Jeznach             /*
13311ecf24cSTomasz Jeznach              * If the transaction does not have a valid process_id, counter
13411ecf24cSTomasz Jeznach              * increments if device_id matches DID_GSCID. If the transaction
13511ecf24cSTomasz Jeznach              * has a valid process_id, counter increments if device_id
13611ecf24cSTomasz Jeznach              * matches DID_GSCID and process_id matches PID_PSCID. See
13711ecf24cSTomasz Jeznach              * IOMMU Specification, Chapter 5.23. Performance-monitoring
13811ecf24cSTomasz Jeznach              * event selector.
13911ecf24cSTomasz Jeznach              */
14011ecf24cSTomasz Jeznach             if (ctx->process_id &&
14111ecf24cSTomasz Jeznach                 get_field(evt, RISCV_IOMMU_IOHPMEVT_PID_PSCID) != pid_pscid) {
14211ecf24cSTomasz Jeznach                 continue;
14311ecf24cSTomasz Jeznach             }
14411ecf24cSTomasz Jeznach         }
14511ecf24cSTomasz Jeznach 
14611ecf24cSTomasz Jeznach         if (get_field(evt, RISCV_IOMMU_IOHPMEVT_DV_GSCV)) {
14711ecf24cSTomasz Jeznach             uint32_t mask = ~0;
14811ecf24cSTomasz Jeznach 
14911ecf24cSTomasz Jeznach             if (get_field(evt, RISCV_IOMMU_IOHPMEVT_DMASK)) {
15011ecf24cSTomasz Jeznach                 /*
15111ecf24cSTomasz Jeznach                  * 1001 1011   mask = GSCID
15211ecf24cSTomasz Jeznach                  * 0000 0111   mask = mask ^ (mask + 1)
15311ecf24cSTomasz Jeznach                  * 1111 1000   mask = ~mask;
15411ecf24cSTomasz Jeznach                  */
15511ecf24cSTomasz Jeznach                 mask = get_field(evt, RISCV_IOMMU_IOHPMEVT_DID_GSCID);
15611ecf24cSTomasz Jeznach                 mask = mask ^ (mask + 1);
15711ecf24cSTomasz Jeznach                 mask = ~mask;
15811ecf24cSTomasz Jeznach             }
15911ecf24cSTomasz Jeznach 
16011ecf24cSTomasz Jeznach             if ((get_field(evt, RISCV_IOMMU_IOHPMEVT_DID_GSCID) & mask) !=
16111ecf24cSTomasz Jeznach                 (did_gscid & mask)) {
16211ecf24cSTomasz Jeznach                 continue;
16311ecf24cSTomasz Jeznach             }
16411ecf24cSTomasz Jeznach         }
16511ecf24cSTomasz Jeznach 
16611ecf24cSTomasz Jeznach         hpm_incr_ctr(s, ctr_idx);
16711ecf24cSTomasz Jeznach     }
16811ecf24cSTomasz Jeznach }
169*ffb37df0STomasz Jeznach 
170*ffb37df0STomasz Jeznach /* Timer callback for cycle counter overflow. */
171*ffb37df0STomasz Jeznach void riscv_iommu_hpm_timer_cb(void *priv)
172*ffb37df0STomasz Jeznach {
173*ffb37df0STomasz Jeznach     RISCVIOMMUState *s = priv;
174*ffb37df0STomasz Jeznach     const uint32_t inhibit = riscv_iommu_reg_get32(
175*ffb37df0STomasz Jeznach         s, RISCV_IOMMU_REG_IOCOUNTINH);
176*ffb37df0STomasz Jeznach     uint32_t ovf;
177*ffb37df0STomasz Jeznach 
178*ffb37df0STomasz Jeznach     if (get_field(inhibit, RISCV_IOMMU_IOCOUNTINH_CY)) {
179*ffb37df0STomasz Jeznach         return;
180*ffb37df0STomasz Jeznach     }
181*ffb37df0STomasz Jeznach 
182*ffb37df0STomasz Jeznach     if (s->irq_overflow_left > 0) {
183*ffb37df0STomasz Jeznach         uint64_t irq_trigger_at =
184*ffb37df0STomasz Jeznach             qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->irq_overflow_left;
185*ffb37df0STomasz Jeznach         timer_mod_anticipate_ns(s->hpm_timer, irq_trigger_at);
186*ffb37df0STomasz Jeznach         s->irq_overflow_left = 0;
187*ffb37df0STomasz Jeznach         return;
188*ffb37df0STomasz Jeznach     }
189*ffb37df0STomasz Jeznach 
190*ffb37df0STomasz Jeznach     ovf = riscv_iommu_reg_get32(s, RISCV_IOMMU_REG_IOCOUNTOVF);
191*ffb37df0STomasz Jeznach     if (!get_field(ovf, RISCV_IOMMU_IOCOUNTOVF_CY)) {
192*ffb37df0STomasz Jeznach         /*
193*ffb37df0STomasz Jeznach          * We don't need to set hpmcycle_val to zero and update hpmcycle_prev to
194*ffb37df0STomasz Jeznach          * current clock value. The way we calculate iohpmcycs will overflow
195*ffb37df0STomasz Jeznach          * and return the correct value. This avoids the need to synchronize
196*ffb37df0STomasz Jeznach          * timer callback and write callback.
197*ffb37df0STomasz Jeznach          */
198*ffb37df0STomasz Jeznach         riscv_iommu_reg_mod32(s, RISCV_IOMMU_REG_IOCOUNTOVF,
199*ffb37df0STomasz Jeznach             RISCV_IOMMU_IOCOUNTOVF_CY, 0);
200*ffb37df0STomasz Jeznach         riscv_iommu_reg_mod64(s, RISCV_IOMMU_REG_IOHPMCYCLES,
201*ffb37df0STomasz Jeznach             RISCV_IOMMU_IOHPMCYCLES_OVF, 0);
202*ffb37df0STomasz Jeznach         riscv_iommu_notify(s, RISCV_IOMMU_INTR_PM);
203*ffb37df0STomasz Jeznach     }
204*ffb37df0STomasz Jeznach }
205