Skip to contentSkip to conversation

perf: CXL 3.0 Performance Monitoring Unit support

PATCH Superseded Jonathan Cameron 4 participants 11 messages May 26, 2023 – May 31, 2023

Subsystems (5)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v7 cover perf: CXL 3.0 Performance Monitoring Unit support

v7:
- Reordered code to do validation before updating any event specific data.
- Fixed example in patch 4 description and added -a parameter to make the all CPUs aspect explicit rather than relying on default.

Dan Williams has expressed that he is happy to take this through the CXL tree once drivers/perf reviewers are happy with the perf aspects.

Will Deacon replied to v6 to say that whilst he and Mark Rutland try to review as much as possible under drivers/perf/ in this case they are happy for the code to go through the CXL tree to avoid them becoming a bottleneck.

Obviously any additional review is still welcome, but if time doesn't allow, Dan, can you pick this up through CXL for 6.5?

Patch 1/5 is also present in the series:
[PATCH 00/32] Add parents to struct pmu -> dev
which may merge first.

Updated introduction

The CXL rev 3.0 specification introduces a CXL Performance Monitoring Unit definition. CXL components may have any number of these blocks. The definition is highly flexible, but that does bring complexity in the driver. All instances are self describing, though for vendor defined events we expect mapping from numeric values to counter names to be performed in userspace tooling. In common with many CXL features, this driver precedes any announced hardware (that I'm aware of anyway!).

Supported features are:
- Devices that allow counters to be written when frozen (allows a single register write to start / stop all counters).
- Fixed purpose counters
- Configurable counters
- CXL specification defined events + HDM filters.

This initial series covers a useful subset of functionality and is expected to be followed by patch series addressing:
* Discoverability beyond fine grained events. This will cover both vendor defined events and providing perf tool with the visibility to be able construct 'summed' events. For example if a single counter can cover all host to device read traffic.
Perf tool patches will use this information and appropriate schema to present a richer set of countable events.
* CXL PMU instances on Upstream and Downstream CXL switch ports and root ports.
* Free running counters. Often used for vendor defined debug type events and error counters. Likely to appear on real devices.
* Devices without interrupt support for overflow.
* Devices that don't support freeze (counters need to be enabled individually).

Exact priority order for the above features will depend on early hardware though (a) will definitely be top of the list as any likely hardware will be able to take advantage of that feature.

Notes.

1) The QEMU model against which this was developed needs tidying up. Latest tree at https://gitlab.com/jic23/qemu branch cxl-2023-02-28 It's backed up behind other series that I plan to upstream first.
2) V1 led to a discussion of how to handle the self describing and extensible nature of the CPMU counters. That is likely to involve a description in the "caps" sysfs directory and perf tool code that is aware of the different event combinations that make sense and can establish which sets are available on a given device.
That is likely to take a while to converge on, so as the driver is useful in the current state, I'm looking to upstream this first and deal with the more complex handling later.
3) There is a lot of other functionality that can be added in future include allowing for simpler hardware implementations that may not support the minimum level of features currently required by the driver (freeze, overflow interrupts etc).
4) Adding support for ports will require solving msi/msix vector requests from portdrv and how to pass those to the CXL port drivers. (or some other way to instantiate the CXL PMU devices.) RFC on that to follow.

CXL rev 3.0 specification available from https://www.computeexpresslink.org

Jonathan Cameron (5):
perf: Allow a PMU to have a parent
cxl: Add functions to get an instance of / count regblocks of a given
type
cxl/pci: Find and register CXL PMU devices
perf: CXL Performance Monitoring Unit driver
docs: perf: Minimal introduction the the CXL PMU device and driver

 Documentation/admin-guide/perf/cxl.rst   |  68 ++
 Documentation/admin-guide/perf/index.rst |   1 +
 MAINTAINERS                              |   7 +
 drivers/cxl/Kconfig                      |  13 +
 drivers/cxl/core/Makefile                |   1 +
 drivers/cxl/core/core.h                  |   1 +
 drivers/cxl/core/pmu.c                   |  68 ++
 drivers/cxl/core/port.c                  |   2 +
 drivers/cxl/core/regs.c                  |  75 +-
 drivers/cxl/cxl.h                        |  16 +
 drivers/cxl/cxlpci.h                     |   1 +
 drivers/cxl/pci.c                        |  26 +-
 drivers/cxl/pmu.h                        |  28 +
 drivers/perf/Kconfig                     |  13 +
 drivers/perf/Makefile                    |   1 +
 drivers/perf/cxl_pmu.c                   | 990 +++++++++++++++++++++++
 include/linux/perf_event.h               |   1 +
 kernel/events/core.c                     |   1 +
 18 files changed, 1306 insertions(+), 7 deletions(-)
 create mode 100644 Documentation/admin-guide/perf/cxl.rst
 create mode 100644 drivers/cxl/core/pmu.c
 create mode 100644 drivers/cxl/pmu.h
 create mode 100644 drivers/perf/cxl_pmu.c

base-commit: f1fcbaa18b28dec10281551dfe6ed3a3ed80e3d6

Signature · 1 line
2.39.2
#1 lore raw

PATCH v7 1/5 perf: Allow a PMU to have a parent

Some PMUs have well defined parents such as PCI devices. As the device_initialize() and device_add() are all within pmu_dev_alloc() which is called from perf_pmu_register() there is no opportunity to set the parent from within a driver.

Add a struct device *parent field to struct pmu and use that to set the parent.

---
 include/linux/perf_event.h | 1 +
 kernel/events/core.c       | 1 +
 2 files changed, 2 insertions(+)
include/linux/perf_event.h +1 -0
index d5628a7b5eaa..b99db1eda72c 100644
@@ -303,6 +303,7 @@ struct pmu {
 
 	struct module			*module;
 	struct device			*dev;
+	struct device			*parent;
 	const struct attribute_group	**attr_groups;
 	const struct attribute_group	**attr_update;
 	const char			*name;
kernel/events/core.c +1 -0
index db016e418931..285cf6ca6e81 100644
@@ -11379,6 +11379,7 @@ static int pmu_dev_alloc(struct pmu *pmu)
 
 	dev_set_drvdata(pmu->dev, pmu);
 	pmu->dev->bus = &pmu_bus;
+	pmu->dev->parent = pmu->parent;
 	pmu->dev->release = pmu_dev_release;
 
 	ret = dev_set_name(pmu->dev, "%s", pmu->name);
Signature · 1 line
2.39.2
#2 lore raw

PATCH v7 2/5 cxl: Add functions to get an instance of / count regblocks of a given type

In reply to Jonathan Cameron

Until the recently release CXL 3.0 specification, there was only ever one instance of any given register block pointed to by the Register Block Locator DVSEC. Now, the specification allows for multiple CXL PMU instances, each with their own register block.

To enable this add cxl_find_regblock_instance() that takes an index parameter and use that to implement cxl_count_regblock() and cxl_find_regblock().

---
 drivers/cxl/core/regs.c | 59 ++++++++++++++++++++++++++++++++++++-----
 drivers/cxl/cxl.h       |  3 +++
 2 files changed, 56 insertions(+), 6 deletions(-)
drivers/cxl/core/regs.c +53 -6
index 1476a0299c9b..4b9672db867d 100644
@@ -286,20 +286,23 @@ static bool cxl_decode_regblock(struct pci_dev *pdev, u32 reg_lo, u32 reg_hi,
 }
 
 /**
- * cxl_find_regblock() - Locate register blocks by type
+ * cxl_find_regblock_instance() - Locate a register block by type / index
  * @pdev: The CXL PCI device to enumerate.
  * @type: Register Block Indicator id
  * @map: Enumeration output, clobbered on error
+ * @index: Index into which particular instance of a regblock wanted in the
+ *	   order found in register locator DVSEC.
  *
  * Return: 0 if register block enumerated, negative error code otherwise
  *
  * A CXL DVSEC may point to one or more register blocks, search for them
- * by @type.
+ * by @type and @index.
  */
-int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
-		      struct cxl_register_map *map)
+int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
+			       struct cxl_register_map *map, int index)
 {
 	u32 regloc_size, regblocks;
+	int instance = 0;
 	int regloc, i;
 
 	map->resource = CXL_RESOURCE_NONE;
@@ -323,15 +326,59 @@ int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
 		if (!cxl_decode_regblock(pdev, reg_lo, reg_hi, map))
 			continue;
 
-		if (map->reg_type == type)
-			return 0;
+		if (map->reg_type == type) {
+			if (index == instance)
+				return 0;
+			instance++;
+		}
 	}
 
 	map->resource = CXL_RESOURCE_NONE;
 	return -ENODEV;
 }
+EXPORT_SYMBOL_NS_GPL(cxl_find_regblock_instance, CXL);
+
+/**
+ * cxl_find_regblock() - Locate register blocks by type
+ * @pdev: The CXL PCI device to enumerate.
+ * @type: Register Block Indicator id
+ * @map: Enumeration output, clobbered on error
+ *
+ * Return: 0 if register block enumerated, negative error code otherwise
+ *
+ * A CXL DVSEC may point to one or more register blocks, search for them
+ * by @type.
+ */
+int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
+		      struct cxl_register_map *map)
+{
+	return cxl_find_regblock_instance(pdev, type, map, 0);
+}
 EXPORT_SYMBOL_NS_GPL(cxl_find_regblock, CXL);
 
+/**
+ * cxl_count_regblock() - Count instances of a given regblock type.
+ * @pdev: The CXL PCI device to enumerate.
+ * @type: Register Block Indicator id
+ *
+ * Some regblocks may be repeated. Count how many instances.
+ *
+ * Return: count of matching regblocks.
+ */
+int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type)
+{
+	struct cxl_register_map map;
+	int rc, count = 0;
+
+	while (1) {
+		rc = cxl_find_regblock_instance(pdev, type, &map, count);
+		if (rc)
+			return count;
+		count++;
+	}
+}
+EXPORT_SYMBOL_NS_GPL(cxl_count_regblock, CXL);
+
 resource_size_t cxl_rcrb_to_component(struct device *dev,
 				      resource_size_t rcrb,
 				      enum cxl_rcrb which)
drivers/cxl/cxl.h +3 -0
index 044a92d9813e..f6e2a9ea5f41 100644
@@ -260,6 +260,9 @@ int cxl_map_device_regs(struct device *dev, struct cxl_device_regs *regs,
 			struct cxl_register_map *map);
 
 enum cxl_regloc_type;
+int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type);
+int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
+			       struct cxl_register_map *map, int index);
 int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
 		      struct cxl_register_map *map);
 
Signature · 1 line
2.39.2
#4 lore raw

PATCH v7 3/5 cxl/pci: Find and register CXL PMU devices

In reply to Jonathan Cameron

CXL PMU devices can be found from entries in the Register Locator DVSEC.

---
 drivers/cxl/core/Makefile |  1 +
 drivers/cxl/core/core.h   |  1 +
 drivers/cxl/core/pmu.c    | 68 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/core/port.c   |  2 ++
 drivers/cxl/core/regs.c   | 16 +++++++++
 drivers/cxl/cxl.h         | 13 ++++++++
 drivers/cxl/cxlpci.h      |  1 +
 drivers/cxl/pci.c         | 26 ++++++++++++++-
 drivers/cxl/pmu.h         | 28 ++++++++++++++++
 9 files changed, 155 insertions(+), 1 deletion(-)
drivers/cxl/core/Makefile +1 -0
index ca4ae31d8f57..1f66b5d4d935 100644
@@ -12,5 +12,6 @@ cxl_core-y += memdev.o
 cxl_core-y += mbox.o
 cxl_core-y += pci.o
 cxl_core-y += hdm.o
+cxl_core-y += pmu.o
 cxl_core-$(CONFIG_TRACING) += trace.o
 cxl_core-$(CONFIG_CXL_REGION) += region.o
drivers/cxl/core/core.h +1 -0
index 27f0968449de..99d4a967eca6 100644
@@ -6,6 +6,7 @@
 
 extern const struct device_type cxl_nvdimm_bridge_type;
 extern const struct device_type cxl_nvdimm_type;
+extern const struct device_type cxl_pmu_type;
 
 extern struct attribute_group cxl_base_attribute_group;
 
drivers/cxl/core/pmu.c new file +68 -0

68 lines not on this page · raw message

drivers/cxl/core/port.c +2 -0
index da2068475fa2..eee1fcb9199b 100644
@@ -56,6 +56,8 @@ static int cxl_device_id(const struct device *dev)
 		return CXL_DEVICE_MEMORY_EXPANDER;
 	if (dev->type == CXL_REGION_TYPE())
 		return CXL_DEVICE_REGION;
+	if (dev->type == &cxl_pmu_type)
+		return CXL_DEVICE_PMU;
 	return 0;
 }
 
drivers/cxl/core/regs.c +16 -0
index 4b9672db867d..518bc2ad2c1e 100644
@@ -6,6 +6,7 @@
 #include <linux/pci.h>
 #include <cxlmem.h>
 #include <cxlpci.h>
+#include <pmu.h>
 
 #include "core.h"
 
@@ -379,6 +380,21 @@ int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_count_regblock, CXL);
 
+int cxl_map_pmu_regs(struct pci_dev *pdev, struct cxl_pmu_regs *regs,
+		     struct cxl_register_map *map)
+{
+	struct device *dev = &pdev->dev;
+	resource_size_t phys_addr;
+
+	phys_addr = map->resource;
+	regs->pmu = devm_cxl_iomap_block(dev, phys_addr, CXL_PMU_REGMAP_SIZE);
+	if (!regs->pmu)
+		return -ENOMEM;
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_map_pmu_regs, CXL);
+
 resource_size_t cxl_rcrb_to_component(struct device *dev,
 				      resource_size_t rcrb,
 				      enum cxl_rcrb which)
drivers/cxl/cxl.h +13 -0
index f6e2a9ea5f41..610aa430276b 100644
@@ -209,6 +209,10 @@ struct cxl_regs {
 	struct_group_tagged(cxl_device_regs, device_regs,
 		void __iomem *status, *mbox, *memdev;
 	);
+
+	struct_group_tagged(cxl_pmu_regs, pmu_regs,
+		void __iomem *pmu;
+	);
 };
 
 struct cxl_reg_map {
@@ -229,6 +233,10 @@ struct cxl_device_reg_map {
 	struct cxl_reg_map memdev;
 };
 
+struct cxl_pmu_reg_map {
+	struct cxl_reg_map pmu;
+};
+
 /**
  * struct cxl_register_map - DVSEC harvested register block mapping parameters
  * @base: virtual base of the register-block-BAR + @block_offset
@@ -237,6 +245,7 @@ struct cxl_device_reg_map {
  * @reg_type: see enum cxl_regloc_type
  * @component_map: cxl_reg_map for component registers
  * @device_map: cxl_reg_maps for device registers
+ * @pmu_map: cxl_reg_maps for CXL Performance Monitoring Units
  */
 struct cxl_register_map {
 	void __iomem *base;
@@ -246,6 +255,7 @@ struct cxl_register_map {
 	union {
 		struct cxl_component_reg_map component_map;
 		struct cxl_device_reg_map device_map;
+		struct cxl_pmu_reg_map pmu_map;
 	};
 };
 
@@ -258,6 +268,8 @@ int cxl_map_component_regs(struct device *dev, struct cxl_component_regs *regs,
 			   unsigned long map_mask);
 int cxl_map_device_regs(struct device *dev, struct cxl_device_regs *regs,
 			struct cxl_register_map *map);
+int cxl_map_pmu_regs(struct pci_dev *pdev, struct cxl_pmu_regs *regs,
+		     struct cxl_register_map *map);
 
 enum cxl_regloc_type;
 int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type);
@@ -752,6 +764,7 @@ void cxl_driver_unregister(struct cxl_driver *cxl_drv);
 #define CXL_DEVICE_REGION		6
 #define CXL_DEVICE_PMEM_REGION		7
 #define CXL_DEVICE_DAX_REGION		8
+#define CXL_DEVICE_PMU			9
 
 #define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
 #define CXL_MODALIAS_FMT "cxl:t%d"
drivers/cxl/cxlpci.h +1 -0
index 0465ef963cd6..b1bbf69b62c5 100644
@@ -65,6 +65,7 @@ enum cxl_regloc_type {
 	CXL_REGLOC_RBI_COMPONENT,
 	CXL_REGLOC_RBI_VIRT,
 	CXL_REGLOC_RBI_MEMDEV,
+	CXL_REGLOC_RBI_PMU,
 	CXL_REGLOC_RBI_TYPES
 };
 
drivers/cxl/pci.c +25 -1
index f7a5b8e9c102..2ad043d77975 100644
@@ -13,6 +13,7 @@
 #include "cxlmem.h"
 #include "cxlpci.h"
 #include "cxl.h"
+#include "pmu.h"
 
 /**
  * DOC: cxl pci
@@ -657,7 +658,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct cxl_register_map map;
 	struct cxl_memdev *cxlmd;
 	struct cxl_dev_state *cxlds;
-	int rc;
+	int i, rc, pmu_count;
 
 	/*
 	 * Double check the anonymous union trickery in struct cxl_regs
@@ -740,6 +741,29 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
 
+	pmu_count = cxl_count_regblock(pdev, CXL_REGLOC_RBI_PMU);
+	for (i = 0; i < pmu_count; i++) {
+		struct cxl_pmu_regs pmu_regs;
+
+		rc = cxl_find_regblock_instance(pdev, CXL_REGLOC_RBI_PMU, &map, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not find PMU regblock\n");
+			break;
+		}
+
+		rc = cxl_map_pmu_regs(pdev, &pmu_regs, &map);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not map PMU regs\n");
+			break;
+		}
+
+		rc = devm_cxl_pmu_add(cxlds->dev, &pmu_regs, cxlmd->id, i, CXL_PMU_MEMDEV);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not add PMU instance\n");
+			break;
+		}
+	}
+
 	rc = cxl_event_config(host_bridge, cxlds);
 	if (rc)
 		return rc;
drivers/cxl/pmu.h new file +28 -0
new file mode 100644
index 000000000000..b1e9bcd9f28c
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright(c) 2023 Huawei
+ * CXL Specification rev 3.0 Setion 8.2.7 (CPMU Register Interface)
+ */
+#ifndef CXL_PMU_H
+#define CXL_PMU_H
+#include <linux/device.h>
+
+enum cxl_pmu_type {
+	CXL_PMU_MEMDEV,
+};
+
+#define CXL_PMU_REGMAP_SIZE 0xe00 /* Table 8-32 CXL 3.0 specification */
+struct cxl_pmu {
+	struct device dev;
+	void __iomem *base;
+	int assoc_id;
+	int index;
+	enum cxl_pmu_type type;
+};
+
+#define to_cxl_pmu(dev) container_of(dev, struct cxl_pmu, dev)
+struct cxl_pmu_regs;
+int devm_cxl_pmu_add(struct device *parent, struct cxl_pmu_regs *regs,
+		     int assoc_id, int idx, enum cxl_pmu_type type);
+
+#endif
Signature · 1 line
2.39.2
#5 lore raw

PATCH v7 4/5 perf: CXL Performance Monitoring Unit driver

In reply to Jonathan Cameron

CXL rev 3.0 introduces a standard performance monitoring hardware block to CXL. Instances are discovered using CXL Register Locator DVSEC entries. Each CXL component may have multiple PMUs.

This initial driver supports a subset of types of counter. It supports counters that are either fixed or configurable, but requires that they support the ability to freeze and write value whilst frozen.

Development done with QEMU model which will be posted shortly.

Example:

$ perf stat -a -e cxl_pmu_mem0.0/h2d_req_snpcur/ -e cxl_pmu_mem0.0/h2d_req_snpdata/ -e cxl_pmu_mem0.0/clock_ticks/ sleep 1

Performance counter stats for 'system wide':

96,757,023,244,321 cxl_pmu_mem0.0/h2d_req_snpcur/
96,757,023,244,365 cxl_pmu_mem0.0/h2d_req_snpdata/
193,514,046,488,653 cxl_pmu_mem0.0/clock_ticks/

       1.090539600 seconds time elapsed

---
v7: THanks to Namhyung and Stephane for reviews
- Set the cpu for an event only after validation checks.
- Added -a to make perf default of all CPUs explicit in command line.
- Fix wrong event names in example.

---
 MAINTAINERS            |   6 +
 drivers/cxl/Kconfig    |  13 +
 drivers/perf/Kconfig   |  13 +
 drivers/perf/Makefile  |   1 +
 drivers/perf/cxl_pmu.c | 990 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 1023 insertions(+)
MAINTAINERS +6 -0
index e0ad886d3163..976b3e982d3a 100644
@@ -5191,6 +5191,12 @@ S:	Maintained
 F:	drivers/cxl/
 F:	include/uapi/linux/cxl_mem.h
 
+COMPUTE EXPRESS LINK PMU (CPMU)
+M:	Jonathan Cameron <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/perf/cxl_pmu.c
+
 CONEXANT ACCESSRUNNER USB DRIVER
 L:	[email protected]
 S:	Orphan
drivers/cxl/Kconfig +13 -0
index ff4e78117b31..d0a4b4cfa50c 100644
@@ -139,4 +139,17 @@ config CXL_REGION_INVALIDATION_TEST
 	  If unsure, or if this kernel is meant for production environments,
 	  say N.
 
+config CXL_PMU
+	tristate "CXL Performance Monitoring Unit"
+	default CXL_BUS
+	depends on PERF_EVENTS
+	help
+	  Support performance monitoring as defined in CXL rev 3.0
+	  section 13.2: Performance Monitoring. CXL components may have
+	  one or more CXL Performance Monitoring Units (CPMUs).
+
+	  Say 'y/m' to enable a driver that will attach to performance
+	  monitoring units and provide standard perf based interfaces.
+
+	  If unsure say 'm'.
 endif
drivers/perf/Kconfig +13 -0
index 711f82400086..2d2dd400fed2 100644
@@ -213,4 +213,17 @@ source "drivers/perf/arm_cspmu/Kconfig"
 
 source "drivers/perf/amlogic/Kconfig"
 
+config CXL_PMU
+	tristate "CXL Performance Monitoring Unit"
+	depends on CXL_BUS
+	help
+	  Support performance monitoring as defined in CXL rev 3.0
+	  section 13.2: Performance Monitoring. CXL components may have
+	  one or more CXL Performance Monitoring Units (CPMUs).
+
+	  Say 'y/m' to enable a driver that will attach to performance
+	  monitoring units and provide standard perf based interfaces.
+
+	  If unsure say 'm'.
+
 endmenu
drivers/perf/Makefile +1 -0
index dabc859540ce..f1d7ce9da275 100644
@@ -24,3 +24,4 @@ obj-$(CONFIG_APPLE_M1_CPU_PMU) += apple_m1_cpu_pmu.o
 obj-$(CONFIG_ALIBABA_UNCORE_DRW_PMU) += alibaba_uncore_drw_pmu.o
 obj-$(CONFIG_ARM_CORESIGHT_PMU_ARCH_SYSTEM_PMU) += arm_cspmu/
 obj-$(CONFIG_MESON_DDR_PMU) += amlogic/
+obj-$(CONFIG_CXL_PMU) += cxl_pmu.o
drivers/perf/cxl_pmu.c new file +990 -0

990 lines not on this page · raw message

Signature · 1 line
2.39.2
#10 lore raw

PATCH v7 5/5 docs: perf: Minimal introduction the the CXL PMU device and driver

In reply to Jonathan Cameron

Very basic introduction to the device and the current driver support provided. I expect to expand on this in future versions of this patch set.

---
v7
- Added explict -a to command line examples to make it obvious the counters apply only to all CPUs (no per thread or per process).

---
 Documentation/admin-guide/perf/cxl.rst   | 68 ++++++++++++++++++++++++
 Documentation/admin-guide/perf/index.rst |  1 +
 MAINTAINERS                              |  1 +
 3 files changed, 70 insertions(+)
Documentation/admin-guide/perf/cxl.rst new file +68 -0
new file mode 100644
index 000000000000..9233ea0d0b10
@@ -0,0 +1,68 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+======================================
+CXL Performance Monitoring Unit (CPMU)
+======================================
+
+The CXL rev 3.0 specification provides a definition of CXL Performance
+Monitoring Unit in section 13.2: Performance Monitoring.
+
+CXL components (e.g. Root Port, Switch Upstream Port, End Point) may have
+any number of CPMU instances. CPMU capabilities are fully discoverable from
+the devices. The specification provides event definitions for all CXL protocol
+message types and a set of additional events for things commonly counted on
+CXL devices (e.g. DRAM events).
+
+CPMU driver
+===========
+
+The CPMU driver registers a perf PMU with the name pmu_mem<X>.<Y> on the CXL bus
+representing the Yth CPMU for memX.
+
+    /sys/bus/cxl/device/pmu_mem<X>.<Y>
+
+The associated PMU is registered as
+
+   /sys/bus/event_sources/devices/cxl_pmu_mem<X>.<Y>
+
+In common with other CXL bus devices, the id has no specific meaning and the
+relationship to specific CXL device should be established via the device parent
+of the device on the CXL bus.
+
+PMU driver provides description of available events and filter options in sysfs.
+
+The "format" directory describes all formats of the config (event vendor id,
+group id and mask) config1 (threshold, filter enables) and config2 (filter
+parameters) fields of the perf_event_attr structure.  The "events" directory
+describes all documented events show in perf list.
+
+The events shown in perf list are the most fine grained events with a single
+bit of the event mask set. More general events may be enable by setting
+multiple mask bits in config. For example, all Device to Host Read Requests
+may be captured on a single counter by setting the bits for all of
+
+* d2h_req_rdcurr
+* d2h_req_rdown
+* d2h_req_rdshared
+* d2h_req_rdany
+* d2h_req_rdownnodata
+
+Example of usage::
+
+  $#perf list
+  cxl_pmu_mem0.0/clock_ticks/                        [Kernel PMU event]
+  cxl_pmu_mem0.0/d2h_req_rdshared/                   [Kernel PMU event]
+  cxl_pmu_mem0.0/h2d_req_snpcur/                     [Kernel PMU event]
+  cxl_pmu_mem0.0/h2d_req_snpdata/                    [Kernel PMU event]
+  cxl_pmu_mem0.0/h2d_req_snpinv/                     [Kernel PMU event]
+  -----------------------------------------------------------
+
+  $# perf stat -a -e cxl_pmu_mem0.0/clock_ticks/ -e cxl_pmu_mem0.0/d2h_req_rdshared/
+
+Vendor specific events may also be available and if so can be used via
+
+  $# perf stat -a -e cxl_pmu_mem0.0/vid=VID,gid=GID,mask=MASK/
+
+The driver does not support sampling so "perf record" is unsupported.
+It only supports system-wide counting so attaching to a task is
+unsupported.
Documentation/admin-guide/perf/index.rst +1 -0
index 9de64a40adab..f60be04e4e33 100644
@@ -21,3 +21,4 @@ Performance monitor support
    alibaba_pmu
    nvidia-pmu
    meson-ddr-pmu
+   cxl
MAINTAINERS +1 -0
index 976b3e982d3a..5388b5c2e5bb 100644
@@ -5195,6 +5195,7 @@ COMPUTE EXPRESS LINK PMU (CPMU)
 M:	Jonathan Cameron <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/admin-guide/perf/cxl.rst
 F:	drivers/perf/cxl_pmu.c
 
 CONEXANT ACCESSRUNNER USB DRIVER
Signature · 1 line
2.39.2
maintainer
#6 lore raw

Re: [PATCH v7 4/5] perf: CXL Performance Monitoring Unit driver

In reply to Jonathan Cameron
Quoted 32 lines · Jonathan Cameron

CXL rev 3.0 introduces a standard performance monitoring hardware block to CXL. Instances are discovered using CXL Register Locator DVSEC entries. Each CXL component may have multiple PMUs.

This initial driver supports a subset of types of counter. It supports counters that are either fixed or configurable, but requires that they support the ability to freeze and write value whilst frozen.

Development done with QEMU model which will be posted shortly.

Example:

$ perf stat -a -e cxl_pmu_mem0.0/h2d_req_snpcur/ -e cxl_pmu_mem0.0/h2d_req_snpdata/ -e cxl_pmu_mem0.0/clock_ticks/ sleep 1

Performance counter stats for 'system wide':

96,757,023,244,321 cxl_pmu_mem0.0/h2d_req_snpcur/
96,757,023,244,365 cxl_pmu_mem0.0/h2d_req_snpdata/
193,514,046,488,653 cxl_pmu_mem0.0/clock_ticks/

       1.090539600 seconds time elapsed

---
v7: THanks to Namhyung and Stephane for reviews
- Set the cpu for an event only after validation checks.
- Added -a to make perf default of all CPUs explicit in command line.
- Fix wrong event names in example.
---

[SNIP]

Quoted 9 lines
drivers/perf/cxl_pmu.c new file +9 -0
+static void cxl_pmu_event_start(struct perf_event *event, int flags)
+{
+       struct cxl_pmu_info *info = pmu_to_cxl_pmu_info(event->pmu);
+       struct hw_perf_event *hwc = &event->hw;
+       void __iomem *base = info->base;
+       u64 cfg;
+
+       /*
+        * All paths to here should either set these flags directly or

drivers/perf/cxl_pmu.c line 638

Quoted 10 lines
drivers/perf/cxl_pmu.c new file +10 -0
+        * call cxl_pmu_event_stop() which will ensure the correct state.
+        */
+       if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
+               return;
+
+       WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
+       hwc->state = 0;
+
+       /*
+        * Currently only hdm filter control is implemnted, this code will

Typo: implemented

Quoted 21 lines
drivers/perf/cxl_pmu.c new file +21 -0
+        * want generalizing when more filters are added.
+        */
+       if (info->filter_hdm) {
+               if (cxl_pmu_config1_hdm_filter_en(event))
+                       cfg = cxl_pmu_config2_get_hdm_decoder(event);
+               else
+                       cfg = GENMASK(15, 0); /* No filtering if 0xFFFF_FFFF */
+               writeq(cfg, base + CXL_PMU_FILTER_CFG_REG(hwc->idx, 0));
+       }
+
+       cfg = readq(base + CXL_PMU_COUNTER_CFG_REG(hwc->idx));
+       cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_INT_ON_OVRFLW, 1);
+       cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_FREEZE_ON_OVRFLW, 1);
+       cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_ENABLE, 1);
+       cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_EDGE,
+                         cxl_pmu_config1_get_edge(event) ? 1 : 0);
+       cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_INVERT,
+                         cxl_pmu_config1_get_invert(event) ? 1 : 0);
+
+       /* Fixed purpose counters have next two fields RO */
+       if (test_bit(hwc->idx, info->conf_counter_bm)) {

drivers/perf/cxl_pmu.c line 669

Quoted 10 lines
drivers/perf/cxl_pmu.c new file +10 -0
+               cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK,
+                                 hwc->event_base);
+               cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_EVENTS_MSK,
+                                 cxl_pmu_config_get_mask(event));
+       }
+       cfg &= ~CXL_PMU_COUNTER_CFG_THRESHOLD_MSK;
+       /*
+        * For events that generate only 1 count per clock the CXL 3.0 spec
+        * states the threshold shall be set to 1 but if set to 0 it will
+        * count the raw value anwyay?

Typo: anyway?

Quoted 14 lines
drivers/perf/cxl_pmu.c new file +14 -0
+        * There is no definition of what events will count multiple per cycle
+        * and hence to which non 1 values of threshold can apply.
+        * (CXL 3.0 8.2.7.2.1 Counter Configuration - threshold field definition)
+        */
+       cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_THRESHOLD_MSK,
+                         cxl_pmu_config1_get_threshold(event));
+       writeq(cfg, base + CXL_PMU_COUNTER_CFG_REG(hwc->idx));
+
+       local64_set(&hwc->prev_count, 0);
+       writeq(0, base + CXL_PMU_COUNTER_REG(hwc->idx));
+
+       perf_event_update_userpage(event);
+}
+

[SNIP]

Quoted 33 lines
drivers/perf/cxl_pmu.c new file +33 -0
+
+static int cxl_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
+{
+       struct cxl_pmu_info *info = hlist_entry_safe(node, struct cxl_pmu_info, node);
+
+       if (info->on_cpu != -1)
+               return 0;
+
+       info->on_cpu = cpu;
+       /*
+        * CPU HP lock is held so we should be guaranteed that the CPU hasn't yet
+        * gone away again.
+        */
+       WARN_ON(irq_set_affinity(info->irq, cpumask_of(cpu)));
+
+       return 0;
+}
+
+static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
+{
+       struct cxl_pmu_info *info = hlist_entry_safe(node, struct cxl_pmu_info, node);
+       unsigned int target;
+
+       if (info->on_cpu != cpu)
+               return 0;
+
+       info->on_cpu = -1;
+       target = cpumask_any_but(cpu_online_mask, cpu);
+       if (target >= nr_cpu_ids) {
+               dev_err(info->pmu.dev, "Unable to find a suitable CPU\n");
+               return 0;
+       }
+

drivers/perf/cxl_pmu.c line 960

Quoted 10 lines
drivers/perf/cxl_pmu.c new file +10 -0
+       perf_pmu_migrate_context(&info->pmu, cpu, target);
+       info->on_cpu = target;
+       /*
+        * CPU HP lock is held so we should be guaranteed that this CPU hasn't yet
+        * gone away.
+        */
+       WARN_ON(irq_set_affinity(info->irq, cpumask_of(target)));
+
+       return 0;
+}

IIUC a CXL PMU hardware (say cxl_pmu_mem0.0) is shared across all CPUs and it would return the same value when read from any CPU, right?

Thanks,
Namhyung

Quoted 33 lines
drivers/perf/cxl_pmu.c new file +30 -1
+
+static __init int cxl_pmu_init(void)
+{
+       int rc;
+
+       rc = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
+                                    "AP_PERF_CXL_PMU_ONLINE",
+                                    cxl_pmu_online_cpu, cxl_pmu_offline_cpu);
+       if (rc < 0)
+               return rc;
+       cxl_pmu_cpuhp_state_num = rc;
+
+       rc = cxl_driver_register(&cxl_pmu_driver);
+       if (rc)
+               cpuhp_remove_multi_state(cxl_pmu_cpuhp_state_num);
+
+       return rc;
+}
+
+static __exit void cxl_pmu_exit(void)
+{
+       cxl_driver_unregister(&cxl_pmu_driver);
+       cpuhp_remove_multi_state(cxl_pmu_cpuhp_state_num);
+}
+
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(CXL);
+module_init(cxl_pmu_init);
+module_exit(cxl_pmu_exit);
+MODULE_ALIAS_CXL(CXL_DEVICE_PMU);
--

2.39.2

#7 lore raw

Re: [PATCH v7 4/5] perf: CXL Performance Monitoring Unit driver

Hi,

Tidied up the typos. Thanks,

Quoted 21 lines
Quoted 21 lines
drivers/perf/cxl_pmu.c new file +21 -0
+static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
+{
+       struct cxl_pmu_info *info = hlist_entry_safe(node, struct cxl_pmu_info, node);
+       unsigned int target;
+
+       if (info->on_cpu != cpu)
+               return 0;
+
+       info->on_cpu = -1;
+       target = cpumask_any_but(cpu_online_mask, cpu);
+       if (target >= nr_cpu_ids) {
+               dev_err(info->pmu.dev, "Unable to find a suitable CPU\n");
+               return 0;
+       }
+
+       perf_pmu_migrate_context(&info->pmu, cpu, target);
+       info->on_cpu = target;
+       /*
+        * CPU HP lock is held so we should be guaranteed that this CPU hasn't yet
+        * gone away.
+        */
Quoted 8 lines

drivers/perf/cxl_pmu.c line 960

Quoted 4 lines
drivers/perf/cxl_pmu.c new file +4 -0
+       WARN_ON(irq_set_affinity(info->irq, cpumask_of(target)));
+
+       return 0;
+}  

IIUC a CXL PMU hardware (say cxl_pmu_mem0.0) is shared across all CPUs and it would return the same value when read from any CPU, right?

Correct, it will return the same value when used from any CPU. I'm not sure what issue you are indicating.

My understanding is that, even for such cases, perf uses percpu variables that mean we still have to ensure that the interrupt handling occurs on the CPU we have migrated the context to.

There are a lot of similar driver in perf already from a quick git grep cpumask_any_but\(cpu_online_mask,

It might be nice to enable perf to operate for these devices without the percpu context though. I haven't looked into whether that is worth doing.

Jonathan

Quoted 39 lines

Thanks,
Namhyung

Quoted 33 lines
drivers/perf/cxl_pmu.c new file +30 -1
+
+static __init int cxl_pmu_init(void)
+{
+       int rc;
+
+       rc = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
+                                    "AP_PERF_CXL_PMU_ONLINE",
+                                    cxl_pmu_online_cpu, cxl_pmu_offline_cpu);
+       if (rc < 0)
+               return rc;
+       cxl_pmu_cpuhp_state_num = rc;
+
+       rc = cxl_driver_register(&cxl_pmu_driver);
+       if (rc)
+               cpuhp_remove_multi_state(cxl_pmu_cpuhp_state_num);
+
+       return rc;
+}
+
+static __exit void cxl_pmu_exit(void)
+{
+       cxl_driver_unregister(&cxl_pmu_driver);
+       cpuhp_remove_multi_state(cxl_pmu_cpuhp_state_num);
+}
+
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(CXL);
+module_init(cxl_pmu_init);
+module_exit(cxl_pmu_exit);
+MODULE_ALIAS_CXL(CXL_DEVICE_PMU);
--

2.39.2

maintainer Reviewed-by on v7 2/5
#3 lore raw

Re: [PATCH v7 2/5] cxl: Add functions to get an instance of / count regblocks of a given type

In reply to Jonathan Cameron
Quoted 10 lines · Jonathan Cameron

Until the recently release CXL 3.0 specification, there was only ever one instance of any given register block pointed to by the Register Block Locator DVSEC. Now, the specification allows for multiple CXL PMU instances, each with their own register block.

To enable this add cxl_find_regblock_instance() that takes an index parameter and use that to implement cxl_count_regblock() and cxl_find_regblock().

Quoted 113 lines
---
  drivers/cxl/core/regs.c | 59 ++++++++++++++++++++++++++++++++++++-----
  drivers/cxl/cxl.h       |  3 +++
  2 files changed, 56 insertions(+), 6 deletions(-)
drivers/cxl/core/regs.c +53 -6
index 1476a0299c9b..4b9672db867d 100644
@@ -286,20 +286,23 @@ static bool cxl_decode_regblock(struct pci_dev *pdev, u32 reg_lo, u32 reg_hi,
  }
 
  /**
- * cxl_find_regblock() - Locate register blocks by type
+ * cxl_find_regblock_instance() - Locate a register block by type / index
   * @pdev: The CXL PCI device to enumerate.
   * @type: Register Block Indicator id
   * @map: Enumeration output, clobbered on error
+ * @index: Index into which particular instance of a regblock wanted in the
+ *	   order found in register locator DVSEC.
   *
   * Return: 0 if register block enumerated, negative error code otherwise
   *
   * A CXL DVSEC may point to one or more register blocks, search for them
- * by @type.
+ * by @type and @index.
   */
-int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
-		      struct cxl_register_map *map)
+int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
+			       struct cxl_register_map *map, int index)
  {
  	u32 regloc_size, regblocks;
+	int instance = 0;
  	int regloc, i;
 
  	map->resource = CXL_RESOURCE_NONE;
@@ -323,15 +326,59 @@ int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
  		if (!cxl_decode_regblock(pdev, reg_lo, reg_hi, map))
  			continue;
 
-		if (map->reg_type == type)
-			return 0;
+		if (map->reg_type == type) {
+			if (index == instance)
+				return 0;
+			instance++;
+		}
  	}
 
  	map->resource = CXL_RESOURCE_NONE;
  	return -ENODEV;
  }
+EXPORT_SYMBOL_NS_GPL(cxl_find_regblock_instance, CXL);
+
+/**
+ * cxl_find_regblock() - Locate register blocks by type
+ * @pdev: The CXL PCI device to enumerate.
+ * @type: Register Block Indicator id
+ * @map: Enumeration output, clobbered on error
+ *
+ * Return: 0 if register block enumerated, negative error code otherwise
+ *
+ * A CXL DVSEC may point to one or more register blocks, search for them
+ * by @type.
+ */
+int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
+		      struct cxl_register_map *map)
+{
+	return cxl_find_regblock_instance(pdev, type, map, 0);
+}
  EXPORT_SYMBOL_NS_GPL(cxl_find_regblock, CXL);
 
+/**
+ * cxl_count_regblock() - Count instances of a given regblock type.
+ * @pdev: The CXL PCI device to enumerate.
+ * @type: Register Block Indicator id
+ *
+ * Some regblocks may be repeated. Count how many instances.
+ *
+ * Return: count of matching regblocks.
+ */
+int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type)
+{
+	struct cxl_register_map map;
+	int rc, count = 0;
+
+	while (1) {
+		rc = cxl_find_regblock_instance(pdev, type, &map, count);
+		if (rc)
+			return count;
+		count++;
+	}
+}
+EXPORT_SYMBOL_NS_GPL(cxl_count_regblock, CXL);
+
  resource_size_t cxl_rcrb_to_component(struct device *dev,
  				      resource_size_t rcrb,
  				      enum cxl_rcrb which)
drivers/cxl/cxl.h +3 -0
index 044a92d9813e..f6e2a9ea5f41 100644
@@ -260,6 +260,9 @@ int cxl_map_device_regs(struct device *dev, struct cxl_device_regs *regs,
  			struct cxl_register_map *map);
 
  enum cxl_regloc_type;
+int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type);
+int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
+			       struct cxl_register_map *map, int index);
  int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
  		      struct cxl_register_map *map);
 
maintainer
#9 lore raw

Re: [PATCH v7 4/5] perf: CXL Performance Monitoring Unit driver

In reply to Jonathan Cameron
Quoted 43 lines · Jonathan Cameron

Hi,

Tidied up the typos. Thanks,

Quoted 21 lines
Quoted 21 lines
drivers/perf/cxl_pmu.c new file +21 -0
+static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
+{
+       struct cxl_pmu_info *info = hlist_entry_safe(node, struct cxl_pmu_info, node);
+       unsigned int target;
+
+       if (info->on_cpu != cpu)
+               return 0;
+
+       info->on_cpu = -1;
+       target = cpumask_any_but(cpu_online_mask, cpu);
+       if (target >= nr_cpu_ids) {
+               dev_err(info->pmu.dev, "Unable to find a suitable CPU\n");
+               return 0;
+       }
+
+       perf_pmu_migrate_context(&info->pmu, cpu, target);
+       info->on_cpu = target;
+       /*
+        * CPU HP lock is held so we should be guaranteed that this CPU hasn't yet
+        * gone away.
+        */
Quoted 8 lines

drivers/perf/cxl_pmu.c line 960

Quoted 4 lines
drivers/perf/cxl_pmu.c new file +4 -0
+       WARN_ON(irq_set_affinity(info->irq, cpumask_of(target)));
+
+       return 0;
+}

IIUC a CXL PMU hardware (say cxl_pmu_mem0.0) is shared across all CPUs and it would return the same value when read from any CPU, right?

Correct, it will return the same value when used from any CPU. I'm not sure what issue you are indicating.

My understanding is that, even for such cases, perf uses percpu variables that mean we still have to ensure that the interrupt handling occurs on the CPU we have migrated the context to.

Quoted 6 lines

There are a lot of similar driver in perf already from a quick git grep cpumask_any_but\(cpu_online_mask,

It might be nice to enable perf to operate for these devices without the percpu context though. I haven't looked into whether that is worth doing.

I was thinking if we could avoid IPIs for read from other CPUs. It has PERF_EV_CAP_READ_ACTIVE_PKG for some uncore events which can be shared among CPUs in the same package
to skip the IPI and to read it from the local CPU.

But I think this can be a separate step.

Thanks,
Namhyung