Page MenuHomeFreeBSD

D60547.1796529495.diff
No OneTemporary

Authored By
Unknown
Size
9 KB
Referenced Files
None
Subscribers
None

D60547.1796529495.diff

Index: sys/x86/iommu/amd_drv.c
===================================================================
--- sys/x86/iommu/amd_drv.c
+++ sys/x86/iommu/amd_drv.c
@@ -69,6 +69,7 @@
#include <x86/iommu/amd_reg.h>
#include <x86/iommu/x86_iommu.h>
#include <x86/iommu/amd_iommu.h>
+#include <x86/iommu/amd_pc.h>
static int amdiommu_enable = 0;
static bool amdiommu_running = false;
@@ -81,6 +82,13 @@
static TAILQ_HEAD(, amdiommu_unit) amdiommu_units =
TAILQ_HEAD_INITIALIZER(amdiommu_units);
+/*
+ * Units with performance counters, by device unit number, for hwpmc(4).
+ * An entry is set once at attach, before hwpmc(4) can look it up, and is
+ * never cleared.
+ */
+static struct amdiommu_unit *amdiommu_pc_units[AMDIOMMU_PC_MAX_UNITS];
+
typedef bool (*amdiommu_itercc_t)(void *, void *);
typedef bool (*amdiommu_iter40_t)(ACPI_IVRS_HARDWARE2 *, void *);
typedef bool (*amdiommu_iter11_t)(ACPI_IVRS_HARDWARE2 *, void *);
@@ -468,6 +476,102 @@
return (BUS_PROBE_SPECIFIC);
}
+static void
+amdiommu_pc_attach(struct amdiommu_unit *sc)
+{
+ uint64_t cfg;
+ u_int nbanks, ncounters;
+ int unitno;
+
+ /*
+ * sc->efr is the EFR image from an IVHD type 11h or 40h; PCSup
+ * there also made the MMIO window 512 KB, see mmio_sz.
+ */
+ if ((sc->efr & AMDIOMMU_EFR_PC_SUP) == 0)
+ return;
+ cfg = amdiommu_read8(sc, AMDIOMMU_PC_COUNTER_CFG);
+ nbanks = (cfg >> AMDIOMMU_PC_CFG_NBANKS_SHIFT) &
+ AMDIOMMU_PC_CFG_NBANKS_MASK;
+ ncounters = (cfg >> AMDIOMMU_PC_CFG_NCOUNTERS_SHIFT) &
+ AMDIOMMU_PC_CFG_NCOUNTERS_MASK;
+ /* NCounter value 1 is reserved (AMD 48882 rev 3.10, 3.4.22). */
+ if (nbanks == 0 || ncounters <= 1) {
+ if (bootverbose)
+ device_printf(sc->iommu.dev,
+ "no usable performance counters, config %#jx\n",
+ (uintmax_t)cfg);
+ return;
+ }
+ unitno = AMD2IOMMU(sc)->unit;
+ if (unitno < 0 || unitno >= AMDIOMMU_PC_MAX_UNITS) {
+ device_printf(sc->iommu.dev,
+ "performance counters not exported, unit %d\n", unitno);
+ return;
+ }
+ sc->pc_nbanks = nbanks;
+ sc->pc_ncounters = ncounters;
+ amdiommu_pc_units[unitno] = sc;
+ if (bootverbose)
+ device_printf(sc->iommu.dev,
+ "performance counters: %u banks of %u\n", nbanks,
+ ncounters);
+}
+
+struct amdiommu_unit *
+amdiommu_pc_unit(u_int unitno, u_int *nbanks, u_int *ncounters)
+{
+ struct amdiommu_unit *unit;
+
+ if (unitno >= AMDIOMMU_PC_MAX_UNITS)
+ return (NULL);
+ unit = amdiommu_pc_units[unitno];
+ if (unit != NULL) {
+ *nbanks = unit->pc_nbanks;
+ *ncounters = unit->pc_ncounters;
+ }
+ return (unit);
+}
+
+static int
+amdiommu_pc_reg(struct amdiommu_unit *unit, u_int bank, u_int cntr,
+ u_int fxn)
+{
+ KASSERT(bank < unit->pc_nbanks && cntr < unit->pc_ncounters,
+ ("amdiommu%d: bad counter %u/%u", AMD2IOMMU(unit)->unit, bank,
+ cntr));
+ KASSERT(fxn <= AMDIOMMU_PC_FXN_REPORT && (fxn & 7) == 0,
+ ("amdiommu%d: bad counter register %#x", AMD2IOMMU(unit)->unit,
+ fxn));
+ return (((AMDIOMMU_PC_ADDR_FIXED | bank) << AMDIOMMU_PC_BANK_SHIFT) |
+ (cntr << AMDIOMMU_PC_CNTR_SHIFT) | fxn);
+}
+
+uint64_t
+amdiommu_pc_read(struct amdiommu_unit *unit, u_int bank, u_int cntr,
+ u_int fxn)
+{
+ return (amdiommu_read8(unit, amdiommu_pc_reg(unit, bank, cntr, fxn)));
+}
+
+void
+amdiommu_pc_write(struct amdiommu_unit *unit, u_int bank, u_int cntr,
+ u_int fxn, uint64_t val)
+{
+ amdiommu_write8(unit, amdiommu_pc_reg(unit, bank, cntr, fxn), val);
+}
+
+/* Has firmware locked any match register of the counters in bank? */
+bool
+amdiommu_pc_bank_locked(struct amdiommu_unit *unit, u_int bank)
+{
+ uint64_t locks;
+
+ locks = amdiommu_read8(unit, AMDIOMMU_PC_PASID_LOCK) |
+ amdiommu_read8(unit, AMDIOMMU_PC_DOMAIN_LOCK) |
+ amdiommu_read8(unit, AMDIOMMU_PC_DEVID_LOCK);
+ return ((locks & (1ULL << bank)) != 0);
+}
+
static int
amdiommu_attach(device_t dev)
{
@@ -586,6 +690,7 @@
TAILQ_INSERT_TAIL(&amdiommu_units, sc, unit_next);
amdiommu_running = true;
+ amdiommu_pc_attach(sc);
return (0);
errout8:
@@ -613,6 +718,11 @@
static int
amdiommu_detach(device_t dev)
{
+ /*
+ * Not supported. Supporting it would also require withdrawing the
+ * unit from hwpmc(4), which keeps the pointers amdiommu_pc_unit()
+ * returned.
+ */
return (EBUSY);
}
Index: sys/x86/iommu/amd_iommu.h
===================================================================
--- sys/x86/iommu/amd_iommu.h
+++ sys/x86/iommu/amd_iommu.h
@@ -99,6 +99,10 @@
int irte_enabled; /* int for sysctl type */
bool irte_x2apic;
u_int irte_nentries;
+
+ /* Performance counters, zero unless published to hwpmc(4) */
+ u_int pc_nbanks;
+ u_int pc_ncounters;
};
#define AMD2IOMMU(unit) (&((unit)->iommu))
Index: sys/x86/iommu/amd_pc.h
===================================================================
--- /dev/null
+++ sys/x86/iommu/amd_pc.h
@@ -0,0 +1,75 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef __X86_IOMMU_AMD_PC_H
+#define __X86_IOMMU_AMD_PC_H
+
+/*
+ * AMD IOMMU performance counters, for the amd64 hwpmc(4) AMDIOMMU class.
+ *
+ * amdiommu_attach() publishes each unit that has counters under its device
+ * unit number. A published unit is never withdrawn, since amdiommu_detach()
+ * fails; the hwpmc(4) class keeps the unit pointers. On amd64 the counter
+ * registers are accessed with single 64-bit MMIO operations.
+ */
+
+#define AMDIOMMU_PC_MAX_UNITS 64
+
+/* Registers of a counter (AMD 48882 rev 3.10, section 3.4.22) */
+#define AMDIOMMU_PC_FXN_COUNTER 0x00 /* Counter */
+#define AMDIOMMU_PC_FXN_SRC 0x08 /* Counter Source */
+#define AMDIOMMU_PC_FXN_PASID 0x10 /* PASID Match */
+#define AMDIOMMU_PC_FXN_DOMAIN 0x18 /* Domain Match */
+#define AMDIOMMU_PC_FXN_DEVID 0x20 /* DeviceID Match */
+#define AMDIOMMU_PC_FXN_REPORT 0x28 /* Counter Report */
+
+#define AMDIOMMU_PC_SRC_CSOURCE_MASK 0xffull /* CSource[7:0] */
+
+/*
+ * The counter is 48 bits wide: the Counter register figure shows
+ * ICounter[47:0], and the "47:28" in the text is a typo.
+ */
+#define AMDIOMMU_PC_COUNTER_MASK ((1ULL << 48) - 1)
+
+/*
+ * Match registers: the match value in the low bits, the match enable in
+ * bit 31 and the compare mask from bit 32 up.
+ */
+#define AMDIOMMU_PC_MATCH_EN (1ULL << 31)
+#define AMDIOMMU_PC_MATCH_MASK_SHIFT 32
+
+struct amdiommu_unit;
+
+struct amdiommu_unit *amdiommu_pc_unit(u_int unitno, u_int *nbanks,
+ u_int *ncounters);
+uint64_t amdiommu_pc_read(struct amdiommu_unit *unit, u_int bank,
+ u_int cntr, u_int fxn);
+void amdiommu_pc_write(struct amdiommu_unit *unit, u_int bank, u_int cntr,
+ u_int fxn, uint64_t val);
+bool amdiommu_pc_bank_locked(struct amdiommu_unit *unit, u_int bank);
+
+#endif /* __X86_IOMMU_AMD_PC_H */
Index: sys/x86/iommu/amd_reg.h
===================================================================
--- sys/x86/iommu/amd_reg.h
+++ sys/x86/iommu/amd_reg.h
@@ -129,6 +129,33 @@
#define AMDIOMMU_PPRLOG_EAI 0x2088
#define AMDIOMMU_PPRLOGB_AUR 0x2090
+/*
+ * Performance counters, present when EFR[PCSup] is set, in which case the
+ * MMIO region is 512 KB (AMD 48882 rev 3.10, section 3.4.22).
+ */
+#define AMDIOMMU_PC_COUNTER_CFG 0x4000 /* Counter Configuration */
+#define AMDIOMMU_PC_CFG_NBANKS_SHIFT 12 /* NCounterBanks[5:0] */
+#define AMDIOMMU_PC_CFG_NBANKS_MASK 0x3full
+#define AMDIOMMU_PC_CFG_NCOUNTERS_SHIFT 7 /* NCounter[3:0] */
+#define AMDIOMMU_PC_CFG_NCOUNTERS_MASK 0xfull
+
+/*
+ * Counter bank lock registers, one bit per bank. The driver takes a set
+ * bit to mean that firmware locked the corresponding match registers of
+ * the counters in that bank, and does not use the bank.
+ */
+#define AMDIOMMU_PC_PASID_LOCK 0x4008
+#define AMDIOMMU_PC_DOMAIN_LOCK 0x4010
+#define AMDIOMMU_PC_DEVID_LOCK 0x4018
+
+/*
+ * Counter register address: bit 18 set, bank in bits 17:12, counter in
+ * bits 11:8 and the register (AMDIOMMU_PC_FXN_*) in bits 7:0.
+ */
+#define AMDIOMMU_PC_ADDR_FIXED 0x40 /* bit 18 after the bank shift */
+#define AMDIOMMU_PC_BANK_SHIFT 12
+#define AMDIOMMU_PC_CNTR_SHIFT 8
+
/*
* IOMMU Control Register AMDIOMMU_CTRL fields
*/

File Metadata

Mime Type
text/plain
Expires
Sun, Dec 6, 3:58 AM (16 h, 24 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42209332
Default Alt Text
D60547.1796529495.diff (9 KB)

Event Timeline