git: 662deb47077d - main - hwpmc/amd: prime UMC perfmon block before programming counter registers
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 03 Nov 2026 14:44:24 UTC
The branch main has been updated by mhorne:
URL: https://cgit.FreeBSD.org/src/commit/?id=662deb47077d5b9c9c71d419dc0c2ec462b07542
commit 662deb47077d5b9c9c71d419dc0c2ec462b07542
Author: Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-09-10 16:20:31 +0000
Commit: Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-03 14:43:57 +0000
hwpmc/amd: prime UMC perfmon block before programming counter registers
UMC counter register writes do not take effect while no counter in the
channel is enabled. Reserve the last counter slot of each UMC channel
and keep it enabled with EventSelect=0 so writes to the remaining slots
take effect. Reserved slots are not exposed to MI code.
UMC perfmon MSRs are package-scoped. A per-socket reference count arms
the reserved slots on the first amd_pcpu_init() for a socket and
disarms them on the last amd_pcpu_fini().
Reviewed by: mhorne
Sponsored by: AMD
Signed-off-by: Osvaldo Janeri Filho <ojanerif@amd.com>
Pull Request: https://github.com/freebsd/freebsd-src/pull/2422
---
sys/dev/hwpmc/hwpmc_amd.c | 120 ++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 110 insertions(+), 10 deletions(-)
diff --git a/sys/dev/hwpmc/hwpmc_amd.c b/sys/dev/hwpmc/hwpmc_amd.c
index 2231824df6a8..14e850ee7410 100644
--- a/sys/dev/hwpmc/hwpmc_amd.c
+++ b/sys/dev/hwpmc/hwpmc_amd.c
@@ -51,6 +51,7 @@
#include <machine/cpufunc.h>
#include <machine/md_var.h>
#include <machine/specialreg.h>
+#include <machine/smp.h>
#define OVERFLOW_WAIT_COUNT 50
@@ -68,6 +69,11 @@ static struct amd_descr *amd_pmcdesc;
static int amd_npmcs;
static int amd_core_npmcs, amd_l3_npmcs, amd_df_npmcs, amd_umc_npmcs;
+static u_int amd_umc_nchannels;
+static uint32_t *amd_umc_gate_evsel; /* [amd_umc_nchannels] */
+static u_int amd_umc_pkg_shift;
+static u_int amd_umc_npkgs;
+static u_int *amd_umc_gate_refcnt; /* [amd_umc_npkgs] */
struct amd_event_code_map {
enum pmc_event pe_ev; /* enum value */
@@ -1211,6 +1217,15 @@ amd_get_caps(int ri, uint32_t *caps)
return (0);
}
+static u_int
+amd_umc_pkg_of(int cpu)
+{
+ /* Single-socket or undetected topology: all CPUs map to package 0. */
+ if (amd_umc_pkg_shift == 0)
+ return (0);
+ return ((u_int)cpu_apic_ids[cpu] >> amd_umc_pkg_shift);
+}
+
/*
* Processor-dependent initialization.
*/
@@ -1221,6 +1236,7 @@ amd_pcpu_init(struct pmc_mdep *md, int cpu)
struct pmc_cpu *pc;
struct pmc_hw *phw;
int first_ri, n;
+ u_int ch, pkg;
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[amd,%d] insane cpu number %d", __LINE__, cpu));
@@ -1244,6 +1260,14 @@ amd_pcpu_init(struct pmc_mdep *md, int cpu)
__LINE__, cpu));
amd_v2_disable_all();
}
+ if (amd_umc_gate_evsel != NULL) {
+ pkg = amd_umc_pkg_of(cpu);
+ if (atomic_fetchadd_int(&amd_umc_gate_refcnt[pkg], 1) == 0) {
+ for (ch = 0; ch < amd_umc_nchannels; ch++)
+ wrmsr(amd_umc_gate_evsel[ch],
+ AMD_PMC_UMC_ENABLE);
+ }
+ }
/*
* Set the content of the hardware descriptors to a known
@@ -1273,6 +1297,7 @@ amd_pcpu_fini(struct pmc_mdep *md, int cpu)
struct amd_cpu *pac;
struct pmc_cpu *pc;
int first_ri, i;
+ u_int ch, pkg;
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[amd,%d] insane cpu number (%d)", __LINE__, cpu));
@@ -1316,6 +1341,17 @@ amd_pcpu_fini(struct pmc_mdep *md, int cpu)
for (i = 0; i < amd_npmcs; i++)
pc->pc_hwpmcs[i + first_ri] = NULL;
+ if (amd_umc_gate_evsel != NULL) {
+ pkg = amd_umc_pkg_of(cpu);
+ KASSERT(atomic_load_acq_int(
+ &amd_umc_gate_refcnt[pkg]) >= 1,
+ ("[amd,%d] UMC gate refcnt underflow pkg %u",
+ __LINE__, pkg));
+ if (atomic_fetchadd_int(&amd_umc_gate_refcnt[pkg], -1) == 1) {
+ for (ch = 0; ch < amd_umc_nchannels; ch++)
+ wrmsr(amd_umc_gate_evsel[ch], 0);
+ }
+ }
free(pac->pc_amdpmcs, M_PMC);
free(pac, M_PMC);
return (0);
@@ -1433,6 +1469,43 @@ amd_hwcheck(void)
return (0);
}
+/*
+ * UMC perfmon registers are clock-gated when no counter in the channel is
+ * enabled. Reserve the last slot of each channel and keep it enabled with
+ * EventSelect=0 so writes to the other slots take effect. Assumes all
+ * sockets have the same UMC layout as the boot CPU.
+ */
+static void
+pmc_amd_initialize_umc(int pmcs_per_umc)
+{
+ u_int c, max_pkg, pkg;
+
+ if (amd_umc_npmcs == 0 || pmcs_per_umc < 2)
+ return;
+
+ amd_umc_nchannels = amd_umc_npmcs / pmcs_per_umc;
+ amd_umc_pkg_shift = (cpu_procinfo2 & AMDID_COREID_SIZE) >>
+ AMDID_COREID_SIZE_SHIFT;
+
+ max_pkg = 0;
+ CPU_FOREACH(c) {
+ pkg = amd_umc_pkg_of(c);
+ if (pkg > max_pkg)
+ max_pkg = pkg;
+ }
+ amd_umc_npkgs = max_pkg + 1;
+
+ amd_umc_gate_evsel = mallocarray(amd_umc_nchannels,
+ sizeof(*amd_umc_gate_evsel), M_PMC, M_WAITOK | M_ZERO);
+ amd_umc_gate_refcnt = mallocarray(amd_umc_npkgs,
+ sizeof(*amd_umc_gate_refcnt), M_PMC, M_WAITOK | M_ZERO);
+ for (c = 0; c < amd_umc_nchannels; c++)
+ amd_umc_gate_evsel[c] = AMD_PMC_UMC_BASE +
+ 2 * (c * pmcs_per_umc + (pmcs_per_umc - 1));
+
+ amd_umc_npmcs -= amd_umc_nchannels;
+}
+
/*
* Initialize ourselves.
*/
@@ -1445,6 +1518,7 @@ pmc_amd_initialize(void)
struct pmc_mdep *pmc_mdep;
enum pmc_cputype cputype;
int ncpus, nclasses, i;
+ int ch, hw, j, per;
int family, model, stepping;
int npmcs_total;
int error;
@@ -1519,6 +1593,9 @@ pmc_amd_initialize(void)
amd_l3_npmcs = 0;
if ((amd_feature2 & AMDID2_PNXC) == 0)
amd_df_npmcs = 0;
+
+ pmc_amd_initialize_umc(pmcs_per_umc);
+
npmcs_total = amd_core_npmcs + amd_l3_npmcs + amd_df_npmcs +
amd_umc_npmcs;
KASSERT(npmcs_total <= AMD_NPMCS_MAX,
@@ -1592,16 +1669,24 @@ pmc_amd_initialize(void)
KASSERT(amd_npmcs == npmcs_total - amd_umc_npmcs,
("%s: UMC cursor wrong: got %d expected %d",
__func__, amd_npmcs, npmcs_total - amd_umc_npmcs));
- for (i = 0; i < amd_umc_npmcs; i++) {
- d = &amd_pmcdesc[amd_npmcs + i];
- snprintf(d->pm_descr.pd_name, PMC_NAME_MAX,
- "K8-UMC%d-%d", i / pmcs_per_umc, i);
- d->pm_descr.pd_class = PMC_CLASS_K8;
- d->pm_descr.pd_caps = AMD_PMC_UMC_CAPS;
- d->pm_descr.pd_width = 48;
- d->pm_evsel = AMD_PMC_UMC_BASE + 2 * i;
- d->pm_perfctr = AMD_PMC_UMC_BASE + 2 * i + 1;
- d->pm_subclass = PMC_AMD_SUB_CLASS_UMC;
+ /*
+ * Expose 'per' counters for each UMC channel. When the clock gate
+ * is armed, the last hardware slot of every channel is reserved.
+ */
+ per = amd_umc_gate_evsel != NULL ? pmcs_per_umc - 1 : pmcs_per_umc;
+ for (ch = 0, i = 0; i < amd_umc_npmcs; ch++) {
+ for (j = 0; j < per && i < amd_umc_npmcs; j++, i++) {
+ hw = ch * pmcs_per_umc + j;
+ d = &amd_pmcdesc[amd_npmcs + i];
+ snprintf(d->pm_descr.pd_name, PMC_NAME_MAX,
+ "K8-UMC%d-%d", ch, i);
+ d->pm_descr.pd_class = PMC_CLASS_K8;
+ d->pm_descr.pd_caps = AMD_PMC_UMC_CAPS;
+ d->pm_descr.pd_width = 48;
+ d->pm_evsel = AMD_PMC_UMC_BASE + 2 * hw;
+ d->pm_perfctr = AMD_PMC_UMC_BASE + 2 * hw + 1;
+ d->pm_subclass = PMC_AMD_SUB_CLASS_UMC;
+ }
}
amd_npmcs += amd_umc_npmcs;
@@ -1733,6 +1818,13 @@ error:
free(pmc_mdep, M_PMC);
free(amd_pcpu, M_PMC);
amd_pcpu = NULL;
+ free(amd_umc_gate_refcnt, M_PMC);
+ amd_umc_gate_refcnt = NULL;
+ free(amd_umc_gate_evsel, M_PMC);
+ amd_umc_gate_evsel = NULL;
+ amd_umc_nchannels = 0;
+ amd_umc_npkgs = 0;
+ amd_umc_pkg_shift = 0;
free(amd_pmcdesc, M_PMC);
amd_pmcdesc = NULL;
amd_npmcs = 0;
@@ -1763,6 +1855,14 @@ pmc_amd_finalize(struct pmc_mdep *md)
free(amd_pcpu, M_PMC);
amd_pcpu = NULL;
+ free(amd_umc_gate_refcnt, M_PMC);
+ amd_umc_gate_refcnt = NULL;
+ free(amd_umc_gate_evsel, M_PMC);
+ amd_umc_gate_evsel = NULL;
+ amd_umc_nchannels = 0;
+ amd_umc_npkgs = 0;
+ amd_umc_pkg_shift = 0;
+
free(amd_pmcdesc, M_PMC);
amd_pmcdesc = NULL;
}