git: 1768d483ef9d - main - hwpmc: keep AMD UMC counters from saturating

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Thu, 19 Nov 2026 17:40:10 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=1768d483ef9dedd6ea5652401c5a693f49b2066c

commit 1768d483ef9dedd6ea5652401c5a693f49b2066c
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-19 17:36:51 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-19 17:37:53 +0000

    hwpmc: keep AMD UMC counters from saturating
    
    UMC counters saturate at 2^48 instead of wrapping, after about a day
    and a half of memory clock cycles.  Clear bit 47 on a read once it is
    set and count the carries in the per-CPU overflow count of the PMC,
    like the arm64 and armv7 drivers do for their 32-bit counters.
    
    Reviewed by:    mhorne
    Sponsored by:   Advanced Micro Devices, Inc.
    Signed-off-by:  Osvaldo Janeri Filho <ojanerif@amd.com>
    Differential Revision:  https://reviews.freebsd.org/D60771
---
 sys/dev/hwpmc/hwpmc_amd.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/sys/dev/hwpmc/hwpmc_amd.c b/sys/dev/hwpmc/hwpmc_amd.c
index 7f505e568c68..101c583352fd 100644
--- a/sys/dev/hwpmc/hwpmc_amd.c
+++ b/sys/dev/hwpmc/hwpmc_amd.c
@@ -413,6 +413,7 @@ static int
 amd_read_pmc(int cpu, int ri, struct pmc *pm, pmc_value_t *v)
 {
 	const struct amd_descr *pd;
+	struct pmc_pcpu_state *pps;
 	pmc_value_t tmp;
 	enum pmc_mode mode;
 
@@ -431,7 +432,19 @@ amd_read_pmc(int cpu, int ri, struct pmc *pm, pmc_value_t *v)
 
 	tmp = rdmsr(pd->pm_perfctr); /* RDMSR serializes */
 	PMCDBG2(MDP, REA, 2, "amd-read (pre-munge) id=%d -> %jd", ri, tmp);
-	if (PMC_IS_SAMPLING_MODE(mode)) {
+	if (pd->pm_subclass == PMC_AMD_SUB_CLASS_UMC) {
+		/*
+		 * UMC counters saturate at 2^48 instead of wrapping.  Clear
+		 * bit 47 once it is set and keep the carries in software.
+		 */
+		pps = &pm->pm_pcpu_state[cpu];
+		if ((tmp & (1ULL << 47)) != 0) {
+			tmp &= ~(1ULL << 47);
+			wrmsr(pd->pm_perfctr, tmp);
+			pps->pps_overflowcnt++;
+		}
+		tmp += (pmc_value_t)pps->pps_overflowcnt << 47;
+	} else if (PMC_IS_SAMPLING_MODE(mode)) {
 		/*
 		 * Clamp value to 0 if the counter just overflowed,
 		 * otherwise the returned reload count would wrap to a
@@ -469,9 +482,14 @@ amd_write_pmc(int cpu, int ri, struct pmc *pm, pmc_value_t v)
 	pd = &amd_pmcdesc[ri];
 	mode = PMC_TO_MODE(pm);
 
-	/* use 2's complement of the count for sampling mode PMCs */
-	if (PMC_IS_SAMPLING_MODE(mode))
+	if (pd->pm_subclass == PMC_AMD_SUB_CLASS_UMC) {
+		/* Bits 47 and up are kept in software, see amd_read_pmc(). */
+		pm->pm_pcpu_state[cpu].pps_overflowcnt = v >> 47;
+		v &= (1ULL << 47) - 1;
+	} else if (PMC_IS_SAMPLING_MODE(mode)) {
+		/* use 2's complement of the count for sampling mode PMCs */
 		v = AMD_RELOAD_COUNT_TO_PERFCTR_VALUE(v);
+	}
 
 	PMCDBG3(MDP, WRI, 1, "amd-write cpu=%d ri=%d v=%jx", cpu, ri, v);