Page MenuHomeFreeBSD

D60548.1796504374.diff
No OneTemporary

Authored By
Unknown
Size
37 KB
Referenced Files
None
Subscribers
None

D60548.1796504374.diff

Index: lib/libpmc/Makefile
===================================================================
--- lib/libpmc/Makefile
+++ lib/libpmc/Makefile
@@ -62,6 +62,7 @@
# PMC-dependent manual pages
MAN+= pmc.amd.3
+MAN+= pmc.amdiommu.3
MAN+= pmc.atom.3
MAN+= pmc.atomsilvermont.3
MAN+= pmc.cmn-600.3
Index: lib/libpmc/libpmc.c
===================================================================
--- lib/libpmc/libpmc.c
+++ lib/libpmc/libpmc.c
@@ -63,6 +63,10 @@
static int perf_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
struct pmc_op_pmcallocate *_pmc_config);
#endif
+#if defined(__amd64__)
+static int amdiommu_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
+ struct pmc_op_pmcallocate *_pmc_config);
+#endif
#if defined(__arm__)
static int armv7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
struct pmc_op_pmcallocate *_pmc_config);
@@ -142,6 +146,7 @@
PMC_CLASSDEP_TABLE(k8, K8);
PMC_CLASSDEP_TABLE(ibs, IBS);
PMC_CLASSDEP_TABLE(perf, PERF);
+PMC_CLASSDEP_TABLE(amdiommu, AMDIOMMU);
PMC_CLASSDEP_TABLE(armv7, ARMV7);
PMC_CLASSDEP_TABLE(armv8, ARMV8);
PMC_CLASSDEP_TABLE(cmn600_pmu, CMN600_PMU);
@@ -221,6 +226,9 @@
PMC_CLASS_TABLE_DESC(rapl, RAPL, rapl, rapl);
PMC_CLASS_TABLE_DESC(perf, PERF, perf, perf);
#endif
+#if defined(__amd64__)
+PMC_CLASS_TABLE_DESC(amdiommu, AMDIOMMU, amdiommu, amdiommu);
+#endif
#if defined(__arm__)
PMC_CLASS_TABLE_DESC(cortex_a8, ARMV7, cortex_a8, armv7);
PMC_CLASS_TABLE_DESC(cortex_a9, ARMV7, cortex_a9, armv7);
@@ -926,6 +934,82 @@
}
#endif
+#if defined(__amd64__)
+/*
+ * AMD IOMMU PMCs.
+ */
+
+#define AMDIOMMU_KW_DEVID "devid"
+#define AMDIOMMU_KW_DOMAIN "domain"
+#define AMDIOMMU_KW_PASID "pasid"
+#define AMDIOMMU_KW_UNIT "unit"
+
+static int
+amdiommu_allocate_pmc(enum pmc_event pe, char *ctrspec,
+ struct pmc_op_pmcallocate *pmc_config)
+{
+ struct pmc_md_amdiommu_op_pmcallocate *q;
+ unsigned long max, n;
+ char *endp, *p, *val;
+ uint8_t filter;
+
+ if (pe < PMC_EV_AMDIOMMU_FIRST || pe > PMC_EV_AMDIOMMU_LAST)
+ return (-1);
+ /* The counters count IOMMU activity: system-wide counting only. */
+ if (pmc_config->pm_mode != PMC_MODE_SC)
+ return (-1);
+
+ q = &pmc_config->pm_md.pm_amdiommu;
+ while ((p = strsep(&ctrspec, ",")) != NULL) {
+ if (KWPREFIXMATCH(p, AMDIOMMU_KW_DEVID "=")) {
+ filter = PMC_AMDIOMMU_FILTER_DEVID;
+ max = PMC_AMDIOMMU_DEVID_MAX;
+ } else if (KWPREFIXMATCH(p, AMDIOMMU_KW_DOMAIN "=")) {
+ filter = PMC_AMDIOMMU_FILTER_DOMAIN;
+ max = PMC_AMDIOMMU_DOMAIN_MAX;
+ } else if (KWPREFIXMATCH(p, AMDIOMMU_KW_PASID "=")) {
+ filter = PMC_AMDIOMMU_FILTER_PASID;
+ max = PMC_AMDIOMMU_PASID_MAX;
+ } else if (KWPREFIXMATCH(p, AMDIOMMU_KW_UNIT "=")) {
+ filter = PMC_AMDIOMMU_FILTER_UNIT;
+ max = PMC_AMDIOMMU_UNIT_MAX;
+ } else
+ return (-1);
+
+ /* Reject repeated, empty, signed and out of range values. */
+ val = strchr(p, '=') + 1;
+ if ((q->pm_amdiommu_filter & filter) != 0 ||
+ !isdigit((unsigned char)*val))
+ return (-1);
+ errno = 0;
+ n = strtoul(val, &endp, 0);
+ if (errno != 0 || *endp != '\0' || n > max)
+ return (-1);
+
+ q->pm_amdiommu_filter |= filter;
+ switch (filter) {
+ case PMC_AMDIOMMU_FILTER_DEVID:
+ q->pm_amdiommu_devid = n;
+ break;
+ case PMC_AMDIOMMU_FILTER_DOMAIN:
+ q->pm_amdiommu_domain = n;
+ break;
+ case PMC_AMDIOMMU_FILTER_PASID:
+ q->pm_amdiommu_pasid = n;
+ break;
+ default:
+ q->pm_amdiommu_unit = n;
+ break;
+ }
+ }
+
+ pmc_config->pm_caps |= PMC_CAP_READ | PMC_CAP_WRITE | PMC_CAP_SYSWIDE;
+ if (q->pm_amdiommu_filter != 0)
+ pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
+ return (0);
+}
+#endif
+
static struct pmc_event_alias generic_aliases[] = {
EV_ALIAS("instructions", "SOFT-CLOCK.HARD"),
EV_ALIAS(NULL, NULL)
@@ -1496,6 +1580,12 @@
ev = ibs_event_table;
count = PMC_EVENT_TABLE_SIZE(ibs);
break;
+#if defined(__amd64__)
+ case PMC_CLASS_AMDIOMMU:
+ ev = amdiommu_event_table;
+ count = PMC_EVENT_TABLE_SIZE(amdiommu);
+ break;
+#endif
case PMC_CLASS_ARMV7:
switch (cpu_info.pm_cputype) {
default:
@@ -1712,6 +1802,11 @@
pmc_class_table[n++] = &ibs_class_table_descr;
break;
#endif
+#if defined(__amd64__)
+ case PMC_CLASS_AMDIOMMU:
+ pmc_class_table[n++] = &amdiommu_class_table_descr;
+ break;
+#endif
case PMC_CLASS_SOFT:
pmc_class_table[n++] = &soft_class_table_descr;
@@ -1982,6 +2077,9 @@
} else if (pe >= PMC_EV_PERF_FIRST && pe <= PMC_EV_PERF_LAST) {
ev = perf_event_table;
evfence = perf_event_table + PMC_EVENT_TABLE_SIZE(perf);
+ } else if (pe >= PMC_EV_AMDIOMMU_FIRST && pe <= PMC_EV_AMDIOMMU_LAST) {
+ ev = amdiommu_event_table;
+ evfence = amdiommu_event_table + PMC_EVENT_TABLE_SIZE(amdiommu);
} else if ((int)pe >= PMC_EV_SOFT_FIRST && (int)pe <= PMC_EV_SOFT_LAST) {
ev = soft_event_table;
evfence = soft_event_table + soft_event_info.pm_nevent;
Index: lib/libpmc/pmc.3
===================================================================
--- lib/libpmc/pmc.3
+++ lib/libpmc/pmc.3
@@ -245,6 +245,8 @@
and
.Tn Intel
CPUs.
+.It Li PMC_CLASS_AMDIOMMU
+Performance counters of AMD IOMMU units.
.It Li PMC_CLASS_TSC
The timestamp counter on i386 and amd64 architecture CPUs.
.It Li PMC_CLASS_ARMV7
@@ -504,8 +506,9 @@
.Ss PMC Architecture Dependent Events
PMC architecture dependent event specifiers are described in the
following manual pages:
-.Bl -column " PMC_CLASS_TSC " "MANUAL PAGE "
+.Bl -column " PMC_CLASS_AMDIOMMU " "MANUAL PAGE "
.It Em "PMC Class" Ta Em "Manual Page"
+.It Li PMC_CLASS_AMDIOMMU Ta Xr pmc.amdiommu 3
.It Li PMC_CLASS_IAF Ta Xr pmc.iaf 3
.It Li PMC_CLASS_IAP Ta Xr pmc.atom 3 , Xr pmc.core 3 , Xr pmc.core2 3
.It Li PMC_CLASS_IBS Ta Xr pmc.ibs 3
@@ -555,6 +558,7 @@
Doing otherwise is unsupported.
.Sh SEE ALSO
.Xr pmc.amd 3 ,
+.Xr pmc.amdiommu 3 ,
.Xr pmc.atom 3 ,
.Xr pmc.core 3 ,
.Xr pmc.core2 3 ,
Index: lib/libpmc/pmc.amdiommu.3
===================================================================
--- /dev/null
+++ lib/libpmc/pmc.amdiommu.3
@@ -0,0 +1,227 @@
+.\"
+.\" Copyright (c) 2026 Advanced Micro Devices, Inc.
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.Dd November 17, 2026
+.Dt PMC.AMDIOMMU 3
+.Os
+.Sh NAME
+.Nm pmc.amdiommu
+.Nd AMD IOMMU performance counters
+.Sh LIBRARY
+.Lb libpmc
+.Sh SYNOPSIS
+.In pmc.h
+.Sh DESCRIPTION
+AMD IOMMU units that implement the optional performance counter
+extension
+.Pq EFR[PCSup]
+have banks of 48-bit event counters in their MMIO register space.
+The
+.Dv PMC_CLASS_AMDIOMMU
+class provides access to them on amd64.
+It needs a kernel built with
+.Cd options IOMMU ,
+with
+.Xr hwpmc 4
+built along with it, and the IOMMU driver enabled with the
+.Va hw.amdiommu.enable
+loader tunable.
+The DMA of a device without a context in the IOMMU is not counted by
+events such as
+.Li AMDIOMMU-MEM_TRANS_TOTAL .
+Set the
+.Va hw.iommu.dma
+loader tunable to 1 to enable device contexts.
+With
+.Va hw.busdma.default
+set to
+.Dq bounce ,
+the contexts are identity-mapped.
+.Pp
+The class has one row per counter, named
+.Sm off
+.Li AMDIOMMU Ar unit Li - Ar bank Li - Ar counter ,
+.Sm on
+for all counters of all units.
+The counters count the activity of an IOMMU unit, not of a CPU:
+only system-wide counting
+.Pq Dv PMC_MODE_SC
+is supported.
+The class does not use the counter overflow reporting of the IOMMU, so
+.Dv PMC_CAP_INTERRUPT
+is not advertised.
+.Pp
+Each counter has a single owner.
+An allocation takes the first free counter, on the unit with the lowest
+number unless the
+.Li unit
+qualifier selects one; once no counter qualifies, allocations fail with
+.Er EINVAL .
+Counters in banks whose match registers are locked by firmware are not used.
+A PMC id holds an 8-bit row number, so on a machine with more counters
+than fit, the units that do not fit are left out with a console
+message.
+The CPU a PMC is allocated on only determines where it is read: any CPU
+can use the counters of any unit.
+.Ss Qualifiers
+The event specifier may be followed by comma-separated qualifiers.
+Values are unsigned decimal, octal
+.Pq leading Ql 0
+or hexadecimal
+.Pq leading Ql 0x ;
+each qualifier may appear at most once.
+A transaction is counted only if it matches every match qualifier
+given.
+.Bl -tag -width domain=N
+.It Li unit= Ns Ar N
+Use a counter of IOMMU unit
+.Ar N ,
+the
+.Li amdiommu Ns Ar N
+device.
+.It Li devid= Ns Ar N
+Count only transactions with PCI requester ID
+.Ar N ,
+as seen by the IOMMU after bridge aliasing.
+It is
+.Ql bus << 8 | slot << 3 | function .
+For example, the device that
+.Xr pciconf 8
+lists as
+.Li pci0:5:0:1 ,
+with the bus number in decimal, has requester ID 0x501.
+.It Li domain= Ns Ar N
+Count only transactions in IOMMU domain
+.Ar N ,
+0 to 0xffff.
+Domain numbers are local to a unit.
+.It Li pasid= Ns Ar N
+Count only transactions tagged with PASID
+.Ar N ,
+0 to 0xfffff.
+.El
+.Pp
+When the IOMMU driver sets a device up, it prints the unit, the
+requester ID in hexadecimal and the domain of the device on the
+console, as in
+.Dl nvme0: amdiommu2 pci0:5:0:0 rid 500 domain 4 id-mapped
+.Ss Event Specifiers
+Event names use the prefix
+.Li AMDIOMMU- .
+.Bl -tag -width indent
+.It Li AMDIOMMU-MEM_PASS_UNTRANS
+Untranslated memory requests passed through without translation.
+.It Li AMDIOMMU-MEM_PASS_PRETRANS
+Memory requests that were already translated
+.Pq ATS
+and passed through.
+.It Li AMDIOMMU-MEM_PASS_EXCL
+Memory requests passed through because they fall in the exclusion
+range.
+.It Li AMDIOMMU-MEM_TARGET_ABORT
+Memory requests that were target-aborted.
+.It Li AMDIOMMU-MEM_TRANS_TOTAL
+Memory requests translated.
+.It Li AMDIOMMU-MEM_IOMMU_TLB_PTE_HIT
+IOMMU TLB hits on PTE-level entries.
+.It Li AMDIOMMU-MEM_IOMMU_TLB_PTE_MIS
+IOMMU TLB misses on PTE-level entries.
+.It Li AMDIOMMU-MEM_IOMMU_TLB_PDE_HIT
+IOMMU TLB hits on PDE-level entries.
+.It Li AMDIOMMU-MEM_IOMMU_TLB_PDE_MIS
+IOMMU TLB misses on PDE-level entries.
+.It Li AMDIOMMU-MEM_DTE_HIT
+Device table entry
+.Pq DTE
+cache hits for memory requests.
+.It Li AMDIOMMU-MEM_DTE_MIS
+DTE cache misses for memory requests.
+.It Li AMDIOMMU-PAGE_TBL_READ_TOT
+Page table reads, all levels of translation.
+.It Li AMDIOMMU-PAGE_TBL_READ_NST
+Nested
+.Pq host
+page table reads.
+.It Li AMDIOMMU-PAGE_TBL_READ_GST
+Guest page table reads.
+.It Li AMDIOMMU-INT_DTE_HIT
+DTE cache hits for interrupt requests.
+.It Li AMDIOMMU-INT_DTE_MIS
+DTE cache misses for interrupt requests.
+.It Li AMDIOMMU-CMD_PROCESSED
+Commands processed from the command buffer.
+.It Li AMDIOMMU-CMD_PROCESSED_INV
+Invalidation commands processed.
+.It Li AMDIOMMU-TLB_INV
+IOMMU TLB invalidations.
+.It Li AMDIOMMU-IGN_RD_WR_MMIO_1FF8H
+Ignored reads and writes of MMIO offset 0x1ff8.
+.It Li AMDIOMMU-VAPIC_INT_NON_GUEST
+Virtual APIC interrupts to a non-guest target.
+.It Li AMDIOMMU-VAPIC_INT_GUEST
+Virtual APIC interrupts to a guest.
+.It Li AMDIOMMU-SMI_RECV
+SMIs received.
+.It Li AMDIOMMU-SMI_BLK
+SMIs blocked.
+.El
+.Sh EXAMPLES
+Count the memory requests translated by IOMMU unit 2 while reading a
+disk:
+.Bd -literal -offset indent
+pmcstat -s AMDIOMMU-MEM_TRANS_TOTAL,unit=2 \e
+ dd if=/dev/nda0 of=/dev/null bs=1m count=512
+.Ed
+.Pp
+Count only the requests of the device
+.Li pci0:5:0:0
+.Pq requester ID 0x500 :
+.Dl pmcstat -s AMDIOMMU-MEM_TRANS_TOTAL,unit=2,devid=0x500 sleep 10
+.Pp
+Count the IOMMU TLB PTE hits of IOMMU domain 4 on unit 0:
+.Dl pmcstat -s AMDIOMMU-MEM_IOMMU_TLB_PTE_HIT,unit=0,domain=4 sleep 10
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The mode is not
+.Dv PMC_MODE_SC ,
+a qualifier is unknown, repeated or out of range, the unit has no
+usable counters, or all qualifying counters are in use.
+It is also returned when the caller lacks the
+.Dv PRIV_PMC_SYSTEM
+privilege and the
+.Va security.bsd.unprivileged_syspmcs
+sysctl is set: the counters see the DMA of every device, so they need
+the privilege regardless.
+.It Bq Er EPERM
+The caller lacks the
+.Dv PRIV_PMC_SYSTEM
+privilege.
+.El
+.Sh SEE ALSO
+.Xr pmc 3 ,
+.Xr pmc_allocate 3 ,
+.Xr pmc_read 3 ,
+.Xr hwpmc 4 ,
+.Xr pciconf 8 ,
+.Xr pmcstat 8
+.Sh HISTORY
+The
+.Dv PMC_CLASS_AMDIOMMU
+class first appeared in
+.Fx 16.0 .
+.Sh CAVEATS
+The counters are 48 bits wide and wrap silently; at 10 billion events
+per second a counter wraps after about 7.8 hours.
+.Pp
+With interrupt remapping disabled, the
+.Li AMDIOMMU-INT_DTE_HIT
+and
+.Li AMDIOMMU-INT_DTE_MIS
+events are not expected to count.
+.Pp
+The IOMMU driver does not restore the counter registers after a
+suspend and resume cycle, so counts taken across one are not valid,
+and a running counter may not count again until it is restarted.
Index: sys/amd64/include/pmc_mdep.h
===================================================================
--- sys/amd64/include/pmc_mdep.h
+++ sys/amd64/include/pmc_mdep.h
@@ -40,6 +40,7 @@
#endif
#include <dev/hwpmc/hwpmc_amd.h>
+#include <dev/hwpmc/hwpmc_amdiommu.h>
#include <dev/hwpmc/hwpmc_core.h>
#include <dev/hwpmc/hwpmc_ibs.h>
#include <dev/hwpmc/hwpmc_perf.h>
@@ -72,6 +73,7 @@
* IAF Intel fixed-function PMCs in Core2 and later CPUs.
* UCP Intel Uncore programmable PMCs.
* UCF Intel Uncore fixed-function PMCs.
+ * AMDIOMMU AMD IOMMU performance counters.
*/
union pmc_md_op_pmcallocate {
@@ -80,6 +82,7 @@
struct pmc_md_iap_op_pmcallocate pm_iap;
struct pmc_md_ucf_op_pmcallocate pm_ucf;
struct pmc_md_ucp_op_pmcallocate pm_ucp;
+ struct pmc_md_amdiommu_op_pmcallocate pm_amdiommu;
uint64_t __pad[4];
};
@@ -96,6 +99,7 @@
struct pmc_md_iap_pmc pm_iap;
struct pmc_md_ucf_pmc pm_ucf;
struct pmc_md_ucp_pmc pm_ucp;
+ struct pmc_md_amdiommu_pmc pm_amdiommu;
};
#define PMC_TRAPFRAME_TO_PC(TF) ((TF)->tf_rip)
Index: sys/conf/files.x86
===================================================================
--- sys/conf/files.x86
+++ sys/conf/files.x86
@@ -121,6 +121,7 @@
dev/hptrr/hptrr_config.c optional hptrr
dev/hptrr/$M-elf.hptrr_lib.o optional hptrr
dev/hwpmc/hwpmc_amd.c optional hwpmc
+dev/hwpmc/hwpmc_amdiommu.c optional hwpmc
dev/hwpmc/hwpmc_perf.c optional hwpmc
dev/hwpmc/hwpmc_ibs.c optional hwpmc
dev/hwpmc/hwpmc_intel.c optional hwpmc
Index: sys/dev/hwpmc/hwpmc_amd.c
===================================================================
--- sys/dev/hwpmc/hwpmc_amd.c
+++ sys/dev/hwpmc/hwpmc_amd.c
@@ -1570,12 +1570,15 @@
struct pmc_classdep *pcd;
struct pmc_mdep *pmc_mdep;
enum pmc_cputype cputype;
- int ncpus, nclasses, i;
+ int ncpus, nclasses, perfidx, i;
int ch, hw, j, per;
int family, model, stepping;
int npmcs_total;
int error;
int pmcs_per_umc;
+#ifdef __amd64__
+ u_int maxrows;
+#endif
/*
* The presence of hardware performance counters on the AMD
@@ -1769,9 +1772,9 @@
M_WAITOK | M_ZERO);
/*
- * These processors have two or three classes of PMCs: the TSC,
- * programmable PMCs, and AMD IBS. One extra class slot is reserved
- * for the optional RAPL energy counters.
+ * These processors have the TSC and programmable PMCs, and optionally
+ * AMD IBS, the PERF MSRs and the AMD IOMMU counters. One extra class
+ * slot is reserved for the optional RAPL energy counters.
*/
if ((amd_feature2 & AMDID2_IBS) != 0) {
nclasses = 4;
@@ -1797,12 +1800,20 @@
* least one of MPERF/APERF or IRPERF is expected to be usable when this
* branch is taken.
*/
+ perfidx = -1;
if (((cpu_power_ecx & CPUID_PERF_STAT) && (tsc_perf_stat == 1)) ||
((amd_extended_feature_extensions & AMDFEID_IRPERF) &&
(rdmsr(MSR_HWCR) & HWCR_IRPERFEN))) {
nclasses++;
+ perfidx = nclasses - 1;
}
+ /* One more class, after PERF's, if an AMD IOMMU has counters. */
+#ifdef __amd64__
+ if (pmc_amdiommu_nclasses() > 0)
+ nclasses++;
+#endif
+
pmc_mdep = pmc_mdep_alloc(nclasses);
ncpus = pmc_cpu_max();
@@ -1861,8 +1872,31 @@
goto error;
}
- /* Initialize PERF class. */
- pmc_perf_initialize(pmc_mdep, ncpus, nclasses - 1);
+ /*
+ * Initialize PERF class. pmc_perf_initialize() repeats the check
+ * above and ignores perfidx when the MSRs are absent.
+ */
+ pmc_perf_initialize(pmc_mdep, ncpus, perfidx);
+
+ /*
+ * The AMD IOMMU class takes the slot between PERF's and RAPL's and is
+ * initialized in that order, since pmc_initialize() renumbers pcd_ri
+ * in slot order. If it fails, RAPL takes the slot instead. PMC ids
+ * hold an 8-bit row index, and the RAPL rows come after the IOMMU ones.
+ */
+#ifdef __amd64__
+ if (pmc_amdiommu_nclasses() > 0) {
+ maxrows = PMC_ID_TO_ROWINDEX(~0u) + 1;
+ if (pmc_mdep->pmd_npmc + RAPL_MAX_NPMCS < maxrows)
+ maxrows -= pmc_mdep->pmd_npmc + RAPL_MAX_NPMCS;
+ else
+ maxrows = 0;
+ error = pmc_amdiommu_initialize(pmc_mdep,
+ pmc_mdep->pmd_nclass - 2, maxrows);
+ if (error != 0)
+ pmc_mdep->pmd_nclass--;
+ }
+#endif
/* RAPL takes the reserved last slot; drop it if the probe fails. */
error = pmc_rapl_initialize(pmc_mdep, ncpus, pmc_mdep->pmd_nclass - 1);
@@ -1896,6 +1930,11 @@
{
PMCDBG0(MDP, INI, 1, "amd-finalize");
+#ifdef __amd64__
+ /* Safe even if the AMDIOMMU class was skipped at initialize time. */
+ pmc_amdiommu_finalize(md);
+#endif
+
/* Safe even if the RAPL class was skipped at initialize time. */
pmc_rapl_finalize(md);
Index: sys/dev/hwpmc/hwpmc_amdiommu.h
===================================================================
--- /dev/null
+++ sys/dev/hwpmc/hwpmc_amdiommu.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * AMD IOMMU performance counters exposed as an hwpmc(4) PMC class.
+ */
+
+#ifndef _DEV_HWPMC_AMDIOMMU_H_
+#define _DEV_HWPMC_AMDIOMMU_H_ 1
+
+/* Qualifiers present in an allocation request (pm_amdiommu_filter). */
+#define PMC_AMDIOMMU_FILTER_PASID 0x01 /* match pm_amdiommu_pasid */
+#define PMC_AMDIOMMU_FILTER_DOMAIN 0x02 /* match pm_amdiommu_domain */
+#define PMC_AMDIOMMU_FILTER_DEVID 0x04 /* match pm_amdiommu_devid */
+#define PMC_AMDIOMMU_FILTER_UNIT 0x08 /* use IOMMU pm_amdiommu_unit */
+#define PMC_AMDIOMMU_FILTER_MASK 0x0f
+
+/* Qualifier value ranges */
+#define PMC_AMDIOMMU_PASID_MAX 0xfffff /* PASIDs are 20 bits wide */
+#define PMC_AMDIOMMU_DOMAIN_MAX 0xffff
+#define PMC_AMDIOMMU_DEVID_MAX 0xffff
+#define PMC_AMDIOMMU_UNIT_MAX 0xff
+
+struct pmc_md_amdiommu_op_pmcallocate {
+ uint32_t pm_amdiommu_pasid;
+ uint16_t pm_amdiommu_domain; /* IOMMU domain ID */
+ uint16_t pm_amdiommu_devid; /* PCI requester ID */
+ uint8_t pm_amdiommu_unit; /* amdiommu device unit */
+ uint8_t pm_amdiommu_filter; /* PMC_AMDIOMMU_FILTER_* */
+ uint8_t pm_amdiommu_spare[2]; /* must be zero */
+};
+
+#ifdef _KERNEL
+
+struct pmc_md_amdiommu_pmc {
+ uint64_t pm_amdiommu_saved; /* count while stopped */
+ uint32_t pm_amdiommu_pasid;
+ uint16_t pm_amdiommu_domain;
+ uint16_t pm_amdiommu_devid;
+ uint8_t pm_amdiommu_filter;
+ uint8_t pm_amdiommu_csource; /* CSource 0x01..0x18 */
+ bool pm_amdiommu_running; /* event select programmed */
+};
+
+struct pmc_mdep;
+
+int pmc_amdiommu_nclasses(void);
+int pmc_amdiommu_initialize(struct pmc_mdep *_md, int _classindex,
+ u_int _maxrows);
+void pmc_amdiommu_finalize(struct pmc_mdep *_md);
+
+#endif /* _KERNEL */
+#endif /* _DEV_HWPMC_AMDIOMMU_H_ */
Index: sys/dev/hwpmc/hwpmc_amdiommu.c
===================================================================
--- /dev/null
+++ sys/dev/hwpmc/hwpmc_amdiommu.c
@@ -0,0 +1,485 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * AMD IOMMU performance counter hwpmc(4) class.
+ *
+ * An IOMMU unit with performance counters (EFR[PCSup]) has NCounterBanks
+ * banks of NCounter 48-bit counters. The class has one row per counter,
+ * unit after unit, named AMDIOMMU<unit>-<bank>-<counter>. The counters
+ * count IOMMU activity rather than CPU activity, so only system-wide
+ * counting is supported and each counter has a single owner. They are
+ * MMIO registers that read the same from every CPU, so an allocation on
+ * any CPU may use the counters of any unit.
+ *
+ * The class needs amd64 and the AMD IOMMU driver; otherwise this file
+ * only reports that there is nothing to count.
+ */
+
+#include "opt_acpi.h"
+#include "opt_iommu.h"
+#include "opt_pci.h"
+
+#include <sys/param.h>
+#include <sys/pmc.h>
+#include <sys/systm.h>
+
+#include <dev/hwpmc/hwpmc_amdiommu.h>
+
+#if defined(__amd64__) && defined(DEV_ACPI) && defined(DEV_PCI) && \
+ defined(IOMMU)
+
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/pmckern.h>
+#include <sys/priv.h>
+#include <sys/sx.h>
+
+#include <x86/iommu/amd_pc.h>
+
+#define AMDIOMMU_CAPS (PMC_CAP_SYSTEM | PMC_CAP_READ | PMC_CAP_WRITE | \
+ PMC_CAP_SYSWIDE | PMC_CAP_QUALIFIER)
+#define AMDIOMMU_WIDTH 48
+
+/* The events are the CSource values 0x01..0x18, in order. */
+CTASSERT(PMC_EV_AMDIOMMU_LAST - PMC_EV_AMDIOMMU_FIRST + 1 == 0x18);
+/* The request must fit the fixed-size allocation ABI. */
+CTASSERT(sizeof(struct pmc_md_amdiommu_op_pmcallocate) <=
+ sizeof(((union pmc_md_op_pmcallocate *)NULL)->__pad));
+
+struct amdiommu_row {
+ struct pmc_descr ar_descr;
+ struct amdiommu_unit *ar_unit;
+ u_int ar_unitno;
+ u_int ar_bank;
+ u_int ar_cntr;
+};
+
+static struct amdiommu_row *amdiommu_rows;
+static struct pmc **amdiommu_owner; /* (x) counter owners */
+static struct pmc_hw **amdiommu_pcpu; /* pmc_hw per CPU */
+static int amdiommu_npmc;
+static int amdiommu_maxcpu;
+static int amdiommu_classidx = -1;
+
+/*
+ * Locking: (x) is protected by pmc_sx held exclusively, as it is for
+ * allocation and release. The MI layer runs start, stop, read and write
+ * of a PMC on the PMC's CPU in a critical section, which serializes the
+ * accesses to its pm_amdiommu_running and pm_amdiommu_saved.
+ */
+
+static int
+amdiommu_pcpu_init(struct pmc_mdep *md, int cpu)
+{
+ struct pmc_cpu *pc;
+ struct pmc_hw *phw;
+ int n, ri;
+
+ KASSERT(cpu >= 0 && cpu < amdiommu_maxcpu,
+ ("[amdiommu,%d] cpu %d out of range", __LINE__, cpu));
+ pc = pmc_pcpu[cpu];
+ KASSERT(pc != NULL, ("[amdiommu,%d] no pmc_cpu for cpu %d", __LINE__,
+ cpu));
+
+ /*
+ * The MI layer wants a pmc_hw per CPU and row; amdiommu_owner[]
+ * keeps a counter from being used through two of them. The row
+ * base is read here because pmc_initialize() renumbers pcd_ri.
+ */
+ ri = md->pmd_classdep[amdiommu_classidx].pcd_ri;
+ phw = malloc(sizeof(*phw) * amdiommu_npmc, M_PMC, M_WAITOK | M_ZERO);
+ for (n = 0; n < amdiommu_npmc; n++) {
+ phw[n].phw_state = PMC_PHW_CPU_TO_STATE(cpu) |
+ PMC_PHW_INDEX_TO_STATE(n) | PMC_PHW_FLAG_IS_ENABLED;
+ pc->pc_hwpmcs[n + ri] = &phw[n];
+ }
+ amdiommu_pcpu[cpu] = phw;
+ return (0);
+}
+
+static int
+amdiommu_pcpu_fini(struct pmc_mdep *md, int cpu)
+{
+ struct pmc_cpu *pc;
+ int n, ri;
+
+ KASSERT(cpu >= 0 && cpu < amdiommu_maxcpu,
+ ("[amdiommu,%d] cpu %d out of range", __LINE__, cpu));
+ pc = pmc_pcpu[cpu];
+ KASSERT(pc != NULL, ("[amdiommu,%d] no pmc_cpu for cpu %d", __LINE__,
+ cpu));
+
+ ri = md->pmd_classdep[amdiommu_classidx].pcd_ri;
+ for (n = 0; n < amdiommu_npmc; n++)
+ pc->pc_hwpmcs[n + ri] = NULL;
+ free(amdiommu_pcpu[cpu], M_PMC);
+ amdiommu_pcpu[cpu] = NULL;
+ return (0);
+}
+
+static int
+amdiommu_read_pmc(int cpu __unused, int ri, struct pmc *pm, pmc_value_t *v)
+{
+ struct amdiommu_row *r;
+
+ KASSERT(ri >= 0 && ri < amdiommu_npmc,
+ ("[amdiommu,%d] ri %d out of range", __LINE__, ri));
+
+ /* A stopped counter may be power-gated; use the value saved then. */
+ if (!pm->pm_md.pm_amdiommu.pm_amdiommu_running) {
+ *v = pm->pm_md.pm_amdiommu.pm_amdiommu_saved;
+ return (0);
+ }
+ r = &amdiommu_rows[ri];
+ *v = amdiommu_pc_read(r->ar_unit, r->ar_bank, r->ar_cntr,
+ AMDIOMMU_PC_FXN_COUNTER) & AMDIOMMU_PC_COUNTER_MASK;
+ return (0);
+}
+
+static int
+amdiommu_write_pmc(int cpu __unused, int ri, struct pmc *pm, pmc_value_t v)
+{
+ KASSERT(ri >= 0 && ri < amdiommu_npmc,
+ ("[amdiommu,%d] ri %d out of range", __LINE__, ri));
+
+ /* Loaded by amdiommu_start_pmc(), once the counter is powered. */
+ pm->pm_md.pm_amdiommu.pm_amdiommu_saved = v & AMDIOMMU_PC_COUNTER_MASK;
+ return (0);
+}
+
+static int
+amdiommu_config_pmc(int cpu, int ri, struct pmc *pm)
+{
+ struct pmc_hw *phw;
+
+ KASSERT(ri >= 0 && ri < amdiommu_npmc,
+ ("[amdiommu,%d] ri %d out of range", __LINE__, ri));
+
+ phw = &amdiommu_pcpu[cpu][ri];
+ KASSERT(pm == NULL || phw->phw_pmc == NULL,
+ ("[amdiommu,%d] ri %d already configured", __LINE__, ri));
+ phw->phw_pmc = pm;
+ return (0);
+}
+
+static int
+amdiommu_get_config(int cpu, int ri, struct pmc **ppm)
+{
+ KASSERT(ri >= 0 && ri < amdiommu_npmc,
+ ("[amdiommu,%d] ri %d out of range", __LINE__, ri));
+
+ *ppm = amdiommu_pcpu[cpu][ri].phw_pmc;
+ return (0);
+}
+
+static int
+amdiommu_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
+{
+ KASSERT(ri >= 0 && ri < amdiommu_npmc,
+ ("[amdiommu,%d] ri %d out of range", __LINE__, ri));
+
+ strlcpy(pi->pm_name, amdiommu_rows[ri].ar_descr.pd_name,
+ sizeof(pi->pm_name));
+ pi->pm_class = PMC_CLASS_AMDIOMMU;
+ pi->pm_enabled = (amdiommu_pcpu[cpu][ri].phw_state &
+ PMC_PHW_FLAG_IS_ENABLED) != 0;
+ /* The counter is shared by all CPUs; report its owner on each. */
+ *ppmc = pi->pm_enabled ? amdiommu_owner[ri] : NULL;
+ return (0);
+}
+
+static int
+amdiommu_allocate_pmc(int cpu __unused, int ri, struct pmc *pm,
+ const struct pmc_op_pmcallocate *a)
+{
+ const struct pmc_md_amdiommu_op_pmcallocate *q;
+ struct pmc_md_amdiommu_pmc *p;
+ struct amdiommu_row *r;
+
+ KASSERT(ri >= 0 && ri < amdiommu_npmc,
+ ("[amdiommu,%d] ri %d out of range", __LINE__, ri));
+ sx_assert(&pmc_sx, SX_XLOCKED);
+
+ r = &amdiommu_rows[ri];
+ q = &a->pm_md.pm_amdiommu;
+ if (a->pm_class != PMC_CLASS_AMDIOMMU || a->pm_mode != PMC_MODE_SC ||
+ (a->pm_caps & ~AMDIOMMU_CAPS) != 0 ||
+ a->pm_ev < PMC_EV_AMDIOMMU_FIRST ||
+ a->pm_ev > PMC_EV_AMDIOMMU_LAST ||
+ (q->pm_amdiommu_filter & ~PMC_AMDIOMMU_FILTER_MASK) != 0 ||
+ q->pm_amdiommu_pasid > PMC_AMDIOMMU_PASID_MAX ||
+ q->pm_amdiommu_spare[0] != 0 || q->pm_amdiommu_spare[1] != 0)
+ return (EINVAL);
+
+ /*
+ * The counters see the DMA of every device, so require privilege
+ * even when security.bsd.unprivileged_syspmcs is set.
+ */
+ if (priv_check(curthread, PRIV_PMC_SYSTEM) != 0)
+ return (EPERM);
+
+ /* With unit=, the rows of other units do not qualify. */
+ if ((q->pm_amdiommu_filter & PMC_AMDIOMMU_FILTER_UNIT) != 0 &&
+ q->pm_amdiommu_unit != r->ar_unitno)
+ return (EINVAL);
+
+ /*
+ * The pmc_hw of this row on another CPU may be free, so check the
+ * counter's owner, not just this CPU's pmc_hw.
+ */
+ if (amdiommu_owner[ri] != NULL)
+ return (EBUSY);
+
+ /*
+ * Firmware can lock the match registers of a bank against writes.
+ * A locked register can take no requested match, and a match that
+ * firmware left enabled can neither be cleared nor reliably read
+ * while the counter may be power-gated, so leave such banks alone.
+ */
+ if (amdiommu_pc_bank_locked(r->ar_unit, r->ar_bank))
+ return (EBUSY);
+
+ amdiommu_owner[ri] = pm;
+ p = &pm->pm_md.pm_amdiommu;
+ p->pm_amdiommu_saved = 0;
+ p->pm_amdiommu_pasid = q->pm_amdiommu_pasid;
+ p->pm_amdiommu_domain = q->pm_amdiommu_domain;
+ p->pm_amdiommu_devid = q->pm_amdiommu_devid;
+ p->pm_amdiommu_filter = q->pm_amdiommu_filter;
+ p->pm_amdiommu_csource = a->pm_ev - PMC_EV_AMDIOMMU_FIRST + 1;
+ p->pm_amdiommu_running = false;
+ return (0);
+}
+
+static int
+amdiommu_release_pmc(int cpu, int ri, struct pmc *pm)
+{
+ KASSERT(ri >= 0 && ri < amdiommu_npmc,
+ ("[amdiommu,%d] ri %d out of range", __LINE__, ri));
+ KASSERT(amdiommu_pcpu[cpu][ri].phw_pmc == NULL,
+ ("[amdiommu,%d] ri %d still configured", __LINE__, ri));
+ KASSERT(amdiommu_owner[ri] == pm,
+ ("[amdiommu,%d] ri %d released by %p, owner %p", __LINE__, ri,
+ pm, amdiommu_owner[ri]));
+ sx_assert(&pmc_sx, SX_XLOCKED);
+
+ amdiommu_owner[ri] = NULL;
+ return (0);
+}
+
+/* Match register value; a disabled match register counts everything. */
+static uint64_t
+amdiommu_match_reg(const struct pmc_md_amdiommu_pmc *p, uint8_t filter,
+ uint64_t value, uint64_t mask)
+{
+ if ((p->pm_amdiommu_filter & filter) == 0)
+ return (0);
+ return (value | (mask << AMDIOMMU_PC_MATCH_MASK_SHIFT) |
+ AMDIOMMU_PC_MATCH_EN);
+}
+
+static int
+amdiommu_start_pmc(int cpu __unused, int ri, struct pmc *pm)
+{
+ struct pmc_md_amdiommu_pmc *p;
+ struct amdiommu_row *r;
+
+ KASSERT(ri >= 0 && ri < amdiommu_npmc,
+ ("[amdiommu,%d] ri %d out of range", __LINE__, ri));
+
+ r = &amdiommu_rows[ri];
+ p = &pm->pm_md.pm_amdiommu;
+
+ /*
+ * A counter whose CSource is zero may be power-gated and drop
+ * writes, so select the event first. Anything counted before the
+ * counter is loaded below is overwritten.
+ */
+ amdiommu_pc_write(r->ar_unit, r->ar_bank, r->ar_cntr,
+ AMDIOMMU_PC_FXN_SRC,
+ p->pm_amdiommu_csource & AMDIOMMU_PC_SRC_CSOURCE_MASK);
+ amdiommu_pc_write(r->ar_unit, r->ar_bank, r->ar_cntr,
+ AMDIOMMU_PC_FXN_PASID, amdiommu_match_reg(p,
+ PMC_AMDIOMMU_FILTER_PASID, p->pm_amdiommu_pasid,
+ PMC_AMDIOMMU_PASID_MAX));
+ amdiommu_pc_write(r->ar_unit, r->ar_bank, r->ar_cntr,
+ AMDIOMMU_PC_FXN_DOMAIN, amdiommu_match_reg(p,
+ PMC_AMDIOMMU_FILTER_DOMAIN, p->pm_amdiommu_domain,
+ PMC_AMDIOMMU_DOMAIN_MAX));
+ amdiommu_pc_write(r->ar_unit, r->ar_bank, r->ar_cntr,
+ AMDIOMMU_PC_FXN_DEVID, amdiommu_match_reg(p,
+ PMC_AMDIOMMU_FILTER_DEVID, p->pm_amdiommu_devid,
+ PMC_AMDIOMMU_DEVID_MAX));
+ amdiommu_pc_write(r->ar_unit, r->ar_bank, r->ar_cntr,
+ AMDIOMMU_PC_FXN_COUNTER, p->pm_amdiommu_saved);
+ p->pm_amdiommu_running = true;
+ return (0);
+}
+
+static int
+amdiommu_stop_pmc(int cpu __unused, int ri, struct pmc *pm)
+{
+ struct pmc_md_amdiommu_pmc *p;
+ struct amdiommu_row *r;
+
+ KASSERT(ri >= 0 && ri < amdiommu_npmc,
+ ("[amdiommu,%d] ri %d out of range", __LINE__, ri));
+
+ r = &amdiommu_rows[ri];
+ p = &pm->pm_md.pm_amdiommu;
+
+ /*
+ * Save the count before clearing CSource may power-gate the counter:
+ * the MI layer reads a stopped PMC only after stopping it.
+ */
+ if (p->pm_amdiommu_running) {
+ p->pm_amdiommu_saved = amdiommu_pc_read(r->ar_unit, r->ar_bank,
+ r->ar_cntr, AMDIOMMU_PC_FXN_COUNTER) &
+ AMDIOMMU_PC_COUNTER_MASK;
+ p->pm_amdiommu_running = false;
+ }
+ amdiommu_pc_write(r->ar_unit, r->ar_bank, r->ar_cntr,
+ AMDIOMMU_PC_FXN_SRC, 0);
+ return (0);
+}
+
+/* The number of classes pmc_amdiommu_initialize() needs: 0 or 1. */
+int
+pmc_amdiommu_nclasses(void)
+{
+ u_int nbanks, ncounters, unitno;
+
+ for (unitno = 0; unitno < AMDIOMMU_PC_MAX_UNITS; unitno++) {
+ if (amdiommu_pc_unit(unitno, &nbanks, &ncounters) != NULL)
+ return (1);
+ }
+ return (0);
+}
+
+/*
+ * Set up the class in md->pmd_classdep[classidx] with at most maxrows
+ * rows; the units that do not fit are left out.
+ */
+int
+pmc_amdiommu_initialize(struct pmc_mdep *md, int classidx, u_int maxrows)
+{
+ struct amdiommu_row *r;
+ struct amdiommu_unit *unit;
+ struct pmc_classdep *pcd;
+ bool used[AMDIOMMU_PC_MAX_UNITS];
+ u_int bank, cntr, nbanks, ncounters, unitno;
+ int ri;
+
+ amdiommu_npmc = 0;
+ for (unitno = 0; unitno < AMDIOMMU_PC_MAX_UNITS; unitno++) {
+ used[unitno] = false;
+ if (amdiommu_pc_unit(unitno, &nbanks, &ncounters) == NULL)
+ continue;
+ if (amdiommu_npmc + nbanks * ncounters > maxrows) {
+ printf("hwpmc: amdiommu%u counters left out, too many "
+ "PMC rows\n", unitno);
+ continue;
+ }
+ used[unitno] = true;
+ amdiommu_npmc += nbanks * ncounters;
+ }
+ if (amdiommu_npmc == 0)
+ return (ENOSPC);
+
+ amdiommu_rows = malloc(sizeof(*amdiommu_rows) * amdiommu_npmc, M_PMC,
+ M_WAITOK | M_ZERO);
+ amdiommu_owner = malloc(sizeof(*amdiommu_owner) * amdiommu_npmc,
+ M_PMC, M_WAITOK | M_ZERO);
+ amdiommu_maxcpu = pmc_cpu_max();
+ amdiommu_pcpu = malloc(sizeof(*amdiommu_pcpu) * amdiommu_maxcpu,
+ M_PMC, M_WAITOK | M_ZERO);
+
+ ri = 0;
+ for (unitno = 0; unitno < AMDIOMMU_PC_MAX_UNITS; unitno++) {
+ if (!used[unitno])
+ continue;
+ unit = amdiommu_pc_unit(unitno, &nbanks, &ncounters);
+ for (bank = 0; bank < nbanks; bank++) {
+ for (cntr = 0; cntr < ncounters; cntr++, ri++) {
+ r = &amdiommu_rows[ri];
+ snprintf(r->ar_descr.pd_name,
+ sizeof(r->ar_descr.pd_name),
+ "AMDIOMMU%u-%u-%u", unitno, bank, cntr);
+ r->ar_descr.pd_class = PMC_CLASS_AMDIOMMU;
+ r->ar_descr.pd_caps = AMDIOMMU_CAPS;
+ r->ar_descr.pd_width = AMDIOMMU_WIDTH;
+ r->ar_unit = unit;
+ r->ar_unitno = unitno;
+ r->ar_bank = bank;
+ r->ar_cntr = cntr;
+ }
+ }
+ }
+
+ pcd = &md->pmd_classdep[classidx];
+ pcd->pcd_caps = AMDIOMMU_CAPS;
+ pcd->pcd_class = PMC_CLASS_AMDIOMMU;
+ pcd->pcd_num = amdiommu_npmc;
+ pcd->pcd_ri = md->pmd_npmc;
+ pcd->pcd_width = AMDIOMMU_WIDTH;
+
+ pcd->pcd_allocate_pmc = amdiommu_allocate_pmc;
+ pcd->pcd_config_pmc = amdiommu_config_pmc;
+ pcd->pcd_describe = amdiommu_describe;
+ pcd->pcd_get_caps = NULL;
+ pcd->pcd_get_config = amdiommu_get_config;
+ pcd->pcd_get_msr = NULL;
+ pcd->pcd_pcpu_fini = amdiommu_pcpu_fini;
+ pcd->pcd_pcpu_init = amdiommu_pcpu_init;
+ pcd->pcd_read_pmc = amdiommu_read_pmc;
+ pcd->pcd_release_pmc = amdiommu_release_pmc;
+ pcd->pcd_start_all = NULL;
+ pcd->pcd_start_pmc = amdiommu_start_pmc;
+ pcd->pcd_stop_all = NULL;
+ pcd->pcd_stop_pmc = amdiommu_stop_pmc;
+ pcd->pcd_write_pmc = amdiommu_write_pmc;
+
+ md->pmd_npmc += amdiommu_npmc;
+ amdiommu_classidx = classidx;
+ return (0);
+}
+
+void
+pmc_amdiommu_finalize(struct pmc_mdep *md __unused)
+{
+ /* Every PMC is released and every CPU finalized by now. */
+ free(amdiommu_pcpu, M_PMC);
+ amdiommu_pcpu = NULL;
+ free(amdiommu_owner, M_PMC);
+ amdiommu_owner = NULL;
+ free(amdiommu_rows, M_PMC);
+ amdiommu_rows = NULL;
+ amdiommu_npmc = 0;
+ amdiommu_classidx = -1;
+}
+
+#else /* !(__amd64__ && DEV_ACPI && DEV_PCI && IOMMU) */
+
+int
+pmc_amdiommu_nclasses(void)
+{
+ return (0);
+}
+
+int
+pmc_amdiommu_initialize(struct pmc_mdep *md __unused, int classidx __unused,
+ u_int maxrows __unused)
+{
+ return (ENXIO);
+}
+
+void
+pmc_amdiommu_finalize(struct pmc_mdep *md __unused)
+{
+}
+
+#endif /* __amd64__ && DEV_ACPI && DEV_PCI && IOMMU */
Index: sys/dev/hwpmc/pmc_events.h
===================================================================
--- sys/dev/hwpmc/pmc_events.h
+++ sys/dev/hwpmc/pmc_events.h
@@ -71,6 +71,36 @@
#define PMC_EV_PERF_FIRST PMC_EV_PERF_MPERF
#define PMC_EV_PERF_LAST PMC_EV_PERF_IRPERF
+/* AMD IOMMU perf counter events (CSource values per AMD 48882 rev 3.10). */
+#define __PMC_EV_AMDIOMMU() \
+ __PMC_EV(AMDIOMMU, MEM_PASS_UNTRANS) \
+ __PMC_EV(AMDIOMMU, MEM_PASS_PRETRANS) \
+ __PMC_EV(AMDIOMMU, MEM_PASS_EXCL) \
+ __PMC_EV(AMDIOMMU, MEM_TARGET_ABORT) \
+ __PMC_EV(AMDIOMMU, MEM_TRANS_TOTAL) \
+ __PMC_EV(AMDIOMMU, MEM_IOMMU_TLB_PTE_HIT) \
+ __PMC_EV(AMDIOMMU, MEM_IOMMU_TLB_PTE_MIS) \
+ __PMC_EV(AMDIOMMU, MEM_IOMMU_TLB_PDE_HIT) \
+ __PMC_EV(AMDIOMMU, MEM_IOMMU_TLB_PDE_MIS) \
+ __PMC_EV(AMDIOMMU, MEM_DTE_HIT) \
+ __PMC_EV(AMDIOMMU, MEM_DTE_MIS) \
+ __PMC_EV(AMDIOMMU, PAGE_TBL_READ_TOT) \
+ __PMC_EV(AMDIOMMU, PAGE_TBL_READ_NST) \
+ __PMC_EV(AMDIOMMU, PAGE_TBL_READ_GST) \
+ __PMC_EV(AMDIOMMU, INT_DTE_HIT) \
+ __PMC_EV(AMDIOMMU, INT_DTE_MIS) \
+ __PMC_EV(AMDIOMMU, CMD_PROCESSED) \
+ __PMC_EV(AMDIOMMU, CMD_PROCESSED_INV) \
+ __PMC_EV(AMDIOMMU, TLB_INV) \
+ __PMC_EV(AMDIOMMU, IGN_RD_WR_MMIO_1FF8H) \
+ __PMC_EV(AMDIOMMU, VAPIC_INT_NON_GUEST) \
+ __PMC_EV(AMDIOMMU, VAPIC_INT_GUEST) \
+ __PMC_EV(AMDIOMMU, SMI_RECV) \
+ __PMC_EV(AMDIOMMU, SMI_BLK)
+
+#define PMC_EV_AMDIOMMU_FIRST PMC_EV_AMDIOMMU_MEM_PASS_UNTRANS
+#define PMC_EV_AMDIOMMU_LAST PMC_EV_AMDIOMMU_SMI_BLK
+
/*
* Software events are dynamically defined.
*/
@@ -2448,6 +2478,7 @@
* 0x14600 0x0100 ARM CMN-600 events
* 0x14700 0x0100 AMD/Intel RAPL energy events
* 0x14800 0x0100 AMD/Intel PERF MSRs events
+ * 0x14900 0x0020 AMD IOMMU performance counter events
* 0x20000 0x1000 Software events
*/
#define __PMC_EVENTS() \
@@ -2478,7 +2509,9 @@
__PMC_EV_BLOCK(RAPL, 0x14700) \
__PMC_EV_RAPL() \
__PMC_EV_BLOCK(PERF, 0x14800) \
- __PMC_EV_PERF()
+ __PMC_EV_PERF() \
+ __PMC_EV_BLOCK(AMDIOMMU, 0x14900) \
+ __PMC_EV_AMDIOMMU()
#define PMC_EVENT_FIRST PMC_EV_TSC_TSC
#define PMC_EVENT_LAST PMC_EV_SOFT_LAST
Index: sys/modules/hwpmc/Makefile
===================================================================
--- sys/modules/hwpmc/Makefile
+++ sys/modules/hwpmc/Makefile
@@ -21,6 +21,7 @@
.elif ${MACHINE_CPUARCH} == "amd64"
SRCS+= hwpmc_amd.c \
+ hwpmc_amdiommu.c \
hwpmc_core.c \
hwpmc_ibs.c \
hwpmc_intel.c \
@@ -29,6 +30,9 @@
hwpmc_tsc.c \
hwpmc_uncore.c \
hwpmc_x86.c
+SRCS+= opt_acpi.h \
+ opt_iommu.h \
+ opt_pci.h
.elif ${MACHINE_CPUARCH} == "arm"
SRCS+= hwpmc_arm.c \
Index: sys/sys/pmc.h
===================================================================
--- sys/sys/pmc.h
+++ sys/sys/pmc.h
@@ -60,7 +60,7 @@
* The patch version is incremented for every bug fix.
*/
#define PMC_VERSION_MAJOR 0x0A
-#define PMC_VERSION_MINOR 0x03
+#define PMC_VERSION_MINOR 0x04
#define PMC_VERSION_PATCH 0x0000
#define PMC_VERSION (PMC_VERSION_MAJOR << 24 | \
@@ -158,7 +158,8 @@
__PMC_CLASS(DMC620_PMU_C, 0x17, "ARM DMC620 Memory Controller PMU CLK") \
__PMC_CLASS(CMN600_PMU, 0x18, "Arm CoreLink CMN600 Coherent Mesh Network PMU") \
__PMC_CLASS(RAPL, 0x19, "AMD/Intel RAPL energy counters") \
- __PMC_CLASS(PERF, 0x1A, "AMD/Intel PERF MSRs")
+ __PMC_CLASS(PERF, 0x1A, "AMD/Intel PERF MSRs") \
+ __PMC_CLASS(AMDIOMMU, 0x1B, "AMD IOMMU performance counters")
enum pmc_class {
#undef __PMC_CLASS
@@ -167,7 +168,7 @@
};
#define PMC_CLASS_FIRST PMC_CLASS_TSC
-#define PMC_CLASS_LAST PMC_CLASS_PERF
+#define PMC_CLASS_LAST PMC_CLASS_AMDIOMMU
/* Compat for the old name. */
#define PMC_CLASS_K8 PMC_CLASS_AMD

File Metadata

Mime Type
text/plain
Expires
Sat, Dec 5, 8:59 PM (9 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42236840
Default Alt Text
D60548.1796504374.diff (37 KB)

Event Timeline