Linux Perf Users
 help / color / mirror / Atom feed
From: Zide Chen <zide.chen@intel.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Andi Kleen <ak@linux.intel.com>,
	Eranian Stephane <eranian@google.com>
Cc: Chun-Tse Shao <ctshao@google.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	Dapeng Mi <dapeng1.mi@linux.intel.com>,
	Zide Chen <zide.chen@intel.com>
Subject: [PATCH 07/12] perf/x86/intel/uncore: Program upper dword in generic ops
Date: Fri,  4 Dec 2026 20:22:48 -0700	[thread overview]
Message-ID: <20261205032253.258646-8-zide.chen@intel.com> (raw)
In-Reply-To: <20261205032253.258646-1-zide.chen@intel.com>

Generic MMIO and PCI {enable,disable}_event() callbacks only write to
the lower 32 bits of counter control registers, forcing extra custom
ops to write to the upper 32 bits for types that support 64-bit
registers.

Add uncore_box_has_ext_event_ctl() to check whether a box supports
upper control dwords: event_mask_ext indicates that the type supports
it, while ctl_offs8 serves as a safety guard against clobbering
neighboring registers on 4-byte stride units.

Add uncore_{pci,mmio}_{write,clear}_event_ctl() helpers and use them
in the generic PCI and MMIO ops to safely write and clear the upper 32
bits.

This allows custom ops like spr_uncore_pci_ops to be dropped, and
64-bit control registers to be handled in a generic way for any type
that sets both ctl_offs8 and event_mask_ext.

Signed-off-by: Zide Chen <zide.chen@intel.com>
---
 arch/x86/events/intel/uncore.h           | 46 ++++++++++++++++++++++++
 arch/x86/events/intel/uncore_discovery.c | 14 +++-----
 2 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/arch/x86/events/intel/uncore.h b/arch/x86/events/intel/uncore.h
index c88949387b7c..449512cab817 100644
--- a/arch/x86/events/intel/uncore.h
+++ b/arch/x86/events/intel/uncore.h
@@ -338,6 +338,52 @@ unsigned uncore_pci_perf_ctr(struct intel_uncore_box *box, int idx)
 	return idx * 8 + box->pmu->type->perf_ctr;
 }
 
+static inline bool uncore_box_has_ext_event_ctl(struct intel_uncore_box *box)
+{
+	return box->pmu->type->ctl_offs8 && box->pmu->type->event_mask_ext;
+}
+
+static inline void uncore_pci_write_event_ctl(struct intel_uncore_box *box,
+					      struct hw_perf_event *hwc, u64 config)
+{
+	/* Upper dword first so the counter does not use stale filters. */
+	if (uncore_box_has_ext_event_ctl(box))
+		pci_write_config_dword(box->pci_dev, hwc->config_base + 4,
+				       upper_32_bits(config));
+	pci_write_config_dword(box->pci_dev, hwc->config_base,
+			       lower_32_bits(config));
+}
+
+static inline void uncore_pci_clear_event_ctl(struct intel_uncore_box *box,
+					      struct hw_perf_event *hwc)
+{
+	/* Stop the counter before clearing upper dword filters. */
+	pci_write_config_dword(box->pci_dev, hwc->config_base, 0);
+	if (uncore_box_has_ext_event_ctl(box))
+		pci_write_config_dword(box->pci_dev, hwc->config_base + 4, 0);
+}
+
+static inline void uncore_mmio_write_event_ctl(struct intel_uncore_box *box,
+					       struct hw_perf_event *hwc, u64 config)
+{
+	/* Upper dword first so the counter does not use stale filters. */
+	if (uncore_box_has_ext_event_ctl(box))
+		writel(upper_32_bits(config),
+		       box->io_addr + hwc->config_base + 4);
+	writel(lower_32_bits(config), box->io_addr + hwc->config_base);
+}
+
+static inline void uncore_mmio_clear_event_ctl(struct intel_uncore_box *box,
+					       struct hw_perf_event *hwc)
+{
+	/* Stop the counter before clearing upper dword filters. */
+	writel(0, box->io_addr + hwc->config_base);
+
+	/* Fixed counter controls are 32-bit with no upper dword filters. */
+	if (!uncore_pmc_fixed(hwc->idx) && uncore_box_has_ext_event_ctl(box))
+		writel(0, box->io_addr + hwc->config_base + 4);
+}
+
 static inline unsigned uncore_msr_box_offset(struct intel_uncore_box *box)
 {
 	struct intel_uncore_pmu *pmu = box->pmu;
diff --git a/arch/x86/events/intel/uncore_discovery.c b/arch/x86/events/intel/uncore_discovery.c
index 979d3da466e3..07df52e2e265 100644
--- a/arch/x86/events/intel/uncore_discovery.c
+++ b/arch/x86/events/intel/uncore_discovery.c
@@ -609,19 +609,15 @@ void intel_generic_uncore_pci_enable_box(struct intel_uncore_box *box)
 static void intel_generic_uncore_pci_enable_event(struct intel_uncore_box *box,
 					    struct perf_event *event)
 {
-	struct pci_dev *pdev = box->pci_dev;
 	struct hw_perf_event *hwc = &event->hw;
 
-	pci_write_config_dword(pdev, hwc->config_base, hwc->config);
+	uncore_pci_write_event_ctl(box, hwc, hwc->config);
 }
 
 void intel_generic_uncore_pci_disable_event(struct intel_uncore_box *box,
 					    struct perf_event *event)
 {
-	struct pci_dev *pdev = box->pci_dev;
-	struct hw_perf_event *hwc = &event->hw;
-
-	pci_write_config_dword(pdev, hwc->config_base, 0);
+	uncore_pci_clear_event_ctl(box, &event->hw);
 }
 
 u64 intel_generic_uncore_pci_read_counter(struct intel_uncore_box *box,
@@ -708,18 +704,16 @@ void intel_generic_uncore_mmio_enable_event(struct intel_uncore_box *box,
 		return;
 	}
 
-	writel(hwc->config, box->io_addr + hwc->config_base);
+	uncore_mmio_write_event_ctl(box, hwc, hwc->config);
 }
 
 void intel_generic_uncore_mmio_disable_event(struct intel_uncore_box *box,
 					     struct perf_event *event)
 {
-	struct hw_perf_event *hwc = &event->hw;
-
 	if (!box->io_addr)
 		return;
 
-	writel(0, box->io_addr + hwc->config_base);
+	uncore_mmio_clear_event_ctl(box, &event->hw);
 }
 
 static struct intel_uncore_ops generic_uncore_mmio_ops = {
-- 
2.56.1


  parent reply	other threads:[~2026-12-05  3:34 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  3:22 [PATCH 00/12] perf/x86/intel/uncore: Fix 64-bit counter config Zide Chen
2026-12-05  3:22 ` [PATCH 01/12] perf/x86/intel/uncore: Fix PCU filter band mask calculation Zide Chen
2026-12-05  3:22 ` [PATCH 02/12] perf/x86/intel/uncore: Fix ICX UPI and M3UPI box control offset Zide Chen
2026-12-05  3:22 ` [PATCH 03/12] perf/x86/intel/uncore: Replace CTL_OFFS8 box flag with type flag Zide Chen
2026-12-05  3:22 ` [PATCH 04/12] perf/x86/intel/uncore: Fix DMR MMIO counter control stride Zide Chen
2026-12-05  3:22 ` [PATCH 05/12] perf/x86/intel/uncore: Drop spr_uncore_mmio_ops for generic MMIO Zide Chen
2026-12-05  3:22 ` [PATCH 06/12] perf/x86/intel/uncore: Fix SPR/EMR/GNR/SRF IMC and HBM event formats Zide Chen
2026-12-05  3:49   ` sashiko-bot
2026-12-05  4:41     ` Chen, Zide
2026-12-05  3:22 ` Zide Chen [this message]
2026-12-05  3:22 ` [PATCH 08/12] perf/x86/intel/uncore: Drop spr_uncore_pci_ops for generic PCI Zide Chen
2026-12-05  3:46   ` sashiko-bot
2026-12-05  4:41     ` Chen, Zide
2026-12-05  3:22 ` [PATCH 09/12] perf/x86/intel/uncore: Fix SKX/SNR/ICX PCI extended umask truncation Zide Chen
2026-12-05  3:22 ` [PATCH 10/12] perf/x86/intel/uncore: Program SNR PCIe3 upper filter before enable Zide Chen
2026-12-05  3:22 ` [PATCH 11/12] perf/x86/intel/uncore: Consolidate SKX, SNR and ICX PCI uncore ops Zide Chen
2026-12-05  3:53   ` sashiko-bot
2026-12-05  4:42     ` Chen, Zide
2026-12-05  3:22 ` [PATCH 12/12] perf/x86/intel/uncore: Fix DMR IMC threshold field Zide Chen
2026-12-05  3:54 ` [PATCH 00/12] perf/x86/intel/uncore: Fix 64-bit counter config sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205032253.258646-8-zide.chen@intel.com \
    --to=zide.chen@intel.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=ctshao@google.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox