xref: /qemu/hw/riscv/riscv-iommu-hpm.c (revision 91dd0bd0216f7a70e5e30cfc24eeea455b4f6993)
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 }
169ffb37df0STomasz Jeznach 
170ffb37df0STomasz Jeznach /* Timer callback for cycle counter overflow. */
171ffb37df0STomasz Jeznach void riscv_iommu_hpm_timer_cb(void *priv)
172ffb37df0STomasz Jeznach {
173ffb37df0STomasz Jeznach     RISCVIOMMUState *s = priv;
174ffb37df0STomasz Jeznach     const uint32_t inhibit = riscv_iommu_reg_get32(
175ffb37df0STomasz Jeznach         s, RISCV_IOMMU_REG_IOCOUNTINH);
176ffb37df0STomasz Jeznach     uint32_t ovf;
177ffb37df0STomasz Jeznach 
178ffb37df0STomasz Jeznach     if (get_field(inhibit, RISCV_IOMMU_IOCOUNTINH_CY)) {
179ffb37df0STomasz Jeznach         return;
180ffb37df0STomasz Jeznach     }
181ffb37df0STomasz Jeznach 
182ffb37df0STomasz Jeznach     if (s->irq_overflow_left > 0) {
183ffb37df0STomasz Jeznach         uint64_t irq_trigger_at =
184ffb37df0STomasz Jeznach             qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->irq_overflow_left;
185ffb37df0STomasz Jeznach         timer_mod_anticipate_ns(s->hpm_timer, irq_trigger_at);
186ffb37df0STomasz Jeznach         s->irq_overflow_left = 0;
187ffb37df0STomasz Jeznach         return;
188ffb37df0STomasz Jeznach     }
189ffb37df0STomasz Jeznach 
190ffb37df0STomasz Jeznach     ovf = riscv_iommu_reg_get32(s, RISCV_IOMMU_REG_IOCOUNTOVF);
191ffb37df0STomasz Jeznach     if (!get_field(ovf, RISCV_IOMMU_IOCOUNTOVF_CY)) {
192ffb37df0STomasz Jeznach         /*
193ffb37df0STomasz Jeznach          * We don't need to set hpmcycle_val to zero and update hpmcycle_prev to
194ffb37df0STomasz Jeznach          * current clock value. The way we calculate iohpmcycs will overflow
195ffb37df0STomasz Jeznach          * and return the correct value. This avoids the need to synchronize
196ffb37df0STomasz Jeznach          * timer callback and write callback.
197ffb37df0STomasz Jeznach          */
198ffb37df0STomasz Jeznach         riscv_iommu_reg_mod32(s, RISCV_IOMMU_REG_IOCOUNTOVF,
199ffb37df0STomasz Jeznach             RISCV_IOMMU_IOCOUNTOVF_CY, 0);
200ffb37df0STomasz Jeznach         riscv_iommu_reg_mod64(s, RISCV_IOMMU_REG_IOHPMCYCLES,
201ffb37df0STomasz Jeznach             RISCV_IOMMU_IOHPMCYCLES_OVF, 0);
202ffb37df0STomasz Jeznach         riscv_iommu_notify(s, RISCV_IOMMU_INTR_PM);
203ffb37df0STomasz Jeznach     }
204ffb37df0STomasz Jeznach }
2052cf2a6c0STomasz Jeznach 
2062cf2a6c0STomasz Jeznach static void hpm_setup_timer(RISCVIOMMUState *s, uint64_t value)
2072cf2a6c0STomasz Jeznach {
2082cf2a6c0STomasz Jeznach     const uint32_t inhibit = riscv_iommu_reg_get32(
2092cf2a6c0STomasz Jeznach         s, RISCV_IOMMU_REG_IOCOUNTINH);
2102cf2a6c0STomasz Jeznach     uint64_t overflow_at, overflow_ns;
2112cf2a6c0STomasz Jeznach 
2122cf2a6c0STomasz Jeznach     if (get_field(inhibit, RISCV_IOMMU_IOCOUNTINH_CY)) {
2132cf2a6c0STomasz Jeznach         return;
2142cf2a6c0STomasz Jeznach     }
2152cf2a6c0STomasz Jeznach 
2162cf2a6c0STomasz Jeznach     /*
2172cf2a6c0STomasz Jeznach      * We are using INT64_MAX here instead to UINT64_MAX because cycle counter
2182cf2a6c0STomasz Jeznach      * has 63-bit precision and INT64_MAX is the maximum it can store.
2192cf2a6c0STomasz Jeznach      */
2202cf2a6c0STomasz Jeznach     if (value) {
2212cf2a6c0STomasz Jeznach         overflow_ns = INT64_MAX - value + 1;
2222cf2a6c0STomasz Jeznach     } else {
2232cf2a6c0STomasz Jeznach         overflow_ns = INT64_MAX;
2242cf2a6c0STomasz Jeznach     }
2252cf2a6c0STomasz Jeznach 
2262cf2a6c0STomasz Jeznach     overflow_at = (uint64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + overflow_ns;
2272cf2a6c0STomasz Jeznach 
2282cf2a6c0STomasz Jeznach     if (overflow_at > INT64_MAX) {
2292cf2a6c0STomasz Jeznach         s->irq_overflow_left = overflow_at - INT64_MAX;
2302cf2a6c0STomasz Jeznach         overflow_at = INT64_MAX;
2312cf2a6c0STomasz Jeznach     }
2322cf2a6c0STomasz Jeznach 
2332cf2a6c0STomasz Jeznach     timer_mod_anticipate_ns(s->hpm_timer, overflow_at);
2342cf2a6c0STomasz Jeznach }
2352cf2a6c0STomasz Jeznach 
2362cf2a6c0STomasz Jeznach /* Updates the internal cycle counter state when iocntinh:CY is changed. */
2372cf2a6c0STomasz Jeznach void riscv_iommu_process_iocntinh_cy(RISCVIOMMUState *s, bool prev_cy_inh)
2382cf2a6c0STomasz Jeznach {
2392cf2a6c0STomasz Jeznach     const uint32_t inhibit = riscv_iommu_reg_get32(
2402cf2a6c0STomasz Jeznach         s, RISCV_IOMMU_REG_IOCOUNTINH);
2412cf2a6c0STomasz Jeznach 
2422cf2a6c0STomasz Jeznach     /* We only need to process CY bit toggle. */
2432cf2a6c0STomasz Jeznach     if (!(inhibit ^ prev_cy_inh)) {
2442cf2a6c0STomasz Jeznach         return;
2452cf2a6c0STomasz Jeznach     }
2462cf2a6c0STomasz Jeznach 
2472cf2a6c0STomasz Jeznach     if (!(inhibit & RISCV_IOMMU_IOCOUNTINH_CY)) {
2482cf2a6c0STomasz Jeznach         /*
2492cf2a6c0STomasz Jeznach          * Cycle counter is enabled. Just start the timer again and update
2502cf2a6c0STomasz Jeznach          * the clock snapshot value to point to the current time to make
2512cf2a6c0STomasz Jeznach          * sure iohpmcycles read is correct.
2522cf2a6c0STomasz Jeznach          */
2532cf2a6c0STomasz Jeznach         s->hpmcycle_prev = get_cycles();
2542cf2a6c0STomasz Jeznach         hpm_setup_timer(s, s->hpmcycle_val);
2552cf2a6c0STomasz Jeznach     } else {
2562cf2a6c0STomasz Jeznach         /*
2572cf2a6c0STomasz Jeznach          * Cycle counter is disabled. Stop the timer and update the cycle
2582cf2a6c0STomasz Jeznach          * counter to record the current value which is last programmed
2592cf2a6c0STomasz Jeznach          * value + the cycles passed so far.
2602cf2a6c0STomasz Jeznach          */
2612cf2a6c0STomasz Jeznach         s->hpmcycle_val = s->hpmcycle_val + (get_cycles() - s->hpmcycle_prev);
2622cf2a6c0STomasz Jeznach         timer_del(s->hpm_timer);
2632cf2a6c0STomasz Jeznach     }
2642cf2a6c0STomasz Jeznach }
265*91dd0bd0STomasz Jeznach 
266*91dd0bd0STomasz Jeznach void riscv_iommu_process_hpmcycle_write(RISCVIOMMUState *s)
267*91dd0bd0STomasz Jeznach {
268*91dd0bd0STomasz Jeznach     const uint64_t val = riscv_iommu_reg_get64(s, RISCV_IOMMU_REG_IOHPMCYCLES);
269*91dd0bd0STomasz Jeznach     const uint32_t ovf = riscv_iommu_reg_get32(s, RISCV_IOMMU_REG_IOCOUNTOVF);
270*91dd0bd0STomasz Jeznach 
271*91dd0bd0STomasz Jeznach     /*
272*91dd0bd0STomasz Jeznach      * Clear OF bit in IOCNTOVF if it's being cleared in IOHPMCYCLES register.
273*91dd0bd0STomasz Jeznach      */
274*91dd0bd0STomasz Jeznach     if (get_field(ovf, RISCV_IOMMU_IOCOUNTOVF_CY) &&
275*91dd0bd0STomasz Jeznach         !get_field(val, RISCV_IOMMU_IOHPMCYCLES_OVF)) {
276*91dd0bd0STomasz Jeznach         riscv_iommu_reg_mod32(s, RISCV_IOMMU_REG_IOCOUNTOVF, 0,
277*91dd0bd0STomasz Jeznach             RISCV_IOMMU_IOCOUNTOVF_CY);
278*91dd0bd0STomasz Jeznach     }
279*91dd0bd0STomasz Jeznach 
280*91dd0bd0STomasz Jeznach     s->hpmcycle_val = val & ~RISCV_IOMMU_IOHPMCYCLES_OVF;
281*91dd0bd0STomasz Jeznach     s->hpmcycle_prev = get_cycles();
282*91dd0bd0STomasz Jeznach     hpm_setup_timer(s, s->hpmcycle_val);
283*91dd0bd0STomasz Jeznach }
284