Skip to contentSkip to conversation

arm_mpam: Add basic mpam driver

PATCH Superseded James Morse 11 participants 200 messages Sep 10, 2025 – Oct 17, 2025 1 message not in the archive Page 1 of 2

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

James Morse

author maintainer
#0 lore raw

v2 cover arm_mpam: Add basic mpam driver

Hello,

The major changes since v1 are:
* DT got ripped out - see below.
* The mon_sel locking was simplified - but that will come back.

Otherwise the myriad of changes are noted on each patch.

~

This is just enough MPAM driver for ACPI. DT got ripped out. If you need DT support - please share your DTS so the DT folk know the binding is what is needed.
This doesn't contain any of the resctrl code, meaning you can't actually drive it from user-space yet. Becuase of that, its hidden behind CONFIG_EXPERT. This will change once the user interface is connected up.

This is the initial group of patches that allows the resctrl code to be built on top. Including that will increase the number of trees that may need to coordinate, so breaking it up make sense.

The locking got simplified, but is still strange - this is because of the 'mpam-fb' firmware interface specification that is still alpha. That thing needs to wait for an interrupt after every system register write, which significantly impacts the driver. Some features just won't work, e.g. reading the monitor registers via perf.

I've not found a platform that can test all the behaviours around the monitors, so this is where I'd expect the most bugs.

The MPAM spec that describes all the system and MMIO registers can be found here:
https://developer.arm.com/documentation/ddi0598/db/?lang=en
(Ignored the 'RETIRED' warning - that is just arm moving the documentation around.
This document has the best overview)

The expectation is this will go via the arm64 tree.

This series is based on v6.17-rc4, and can be retrieved from:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git mpam/driver/v2

The rest of the driver can be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git mpam/snapshot/v6.17-rc4

What is MPAM? Set your time-machine to 2020:
https://lore.kernel.org/lkml/[email protected]/

This series was previously posted here:
[v1] lore.kernel.org/r/[email protected] [RFC] lore.kernel.org/r/[email protected]

Bugs welcome,
Thanks,

James Morse (27):
ACPI / PPTT: Add a helper to fill a cpumask from a processor container ACPI / PPTT: Stop acpi_count_levels() expecting callers to clear
levels
ACPI / PPTT: Find cache level by cache-id
ACPI / PPTT: Add a helper to fill a cpumask from a cache_id
arm64: kconfig: Add Kconfig entry for MPAM
ACPI / MPAM: Parse the MPAM table
arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate
arm_mpam: Add the class and component structures for firmware
described ris
arm_mpam: Add MPAM MSC register layout definitions
arm_mpam: Add cpuhp callbacks to probe MSC hardware
arm_mpam: Probe hardware to find the supported partid/pmg values
arm_mpam: Add helpers for managing the locking around the mon_sel
registers
arm_mpam: Probe the hardware features resctrl supports
arm_mpam: Merge supported features during mpam_enable() into
mpam_class
arm_mpam: Reset MSC controls from cpu hp callbacks
arm_mpam: Add a helper to touch an MSC from any CPU
arm_mpam: Extend reset logic to allow devices to be reset any time
arm_mpam: Register and enable IRQs
arm_mpam: Use a static key to indicate when mpam is enabled
arm_mpam: Allow configuration to be applied and restored during cpu
online
arm_mpam: Probe and reset the rest of the features
arm_mpam: Add helpers to allocate monitors
arm_mpam: Add mpam_msmon_read() to read monitor value
arm_mpam: Track bandwidth counter state for overflow and power
management
arm_mpam: Add helper to reset saved mbwu state
arm_mpam: Add kunit test for bitmap reset
arm_mpam: Add kunit tests for props_mismatch()

Rohit Mathew (2):
arm_mpam: Probe for long/lwd mbwu counters
arm_mpam: Use long MBWU counters if supported

 arch/arm64/Kconfig                  |   25 +
 drivers/Kconfig                     |    2 +
 drivers/Makefile                    |    1 +
 drivers/acpi/arm64/Kconfig          |    3 +
 drivers/acpi/arm64/Makefile         |    1 +
 drivers/acpi/arm64/mpam.c           |  361 ++++
 drivers/acpi/pptt.c                 |  224 ++-
 drivers/acpi/tables.c               |    2 +-
 drivers/resctrl/Kconfig             |   24 +
 drivers/resctrl/Makefile            |    4 +
 drivers/resctrl/mpam_devices.c      | 2668 +++++++++++++++++++++++++++
 drivers/resctrl/mpam_internal.h     |  676 +++++++
 drivers/resctrl/test_mpam_devices.c |  389 ++++
 include/linux/acpi.h                |   26 +
 include/linux/arm_mpam.h            |   58 +
 15 files changed, 4455 insertions(+), 9 deletions(-)
 create mode 100644 drivers/acpi/arm64/mpam.c
 create mode 100644 drivers/resctrl/Kconfig
 create mode 100644 drivers/resctrl/Makefile
 create mode 100644 drivers/resctrl/mpam_devices.c
 create mode 100644 drivers/resctrl/mpam_internal.h
 create mode 100644 drivers/resctrl/test_mpam_devices.c
 create mode 100644 include/linux/arm_mpam.h
Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH v2 1/29 ACPI / PPTT: Add a helper to fill a cpumask from a processor container

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

---
Changes since v1:
* Replaced commit message with wording from Dave.
* Fixed a stray plural.
* Moved further down in the file to make use of get_pptt() helper.
* Added a break to exit the loop early.

Changes since RFC:
* Removed leaf_flag local variable from acpi_pptt_get_cpus_from_container()

Changes since RFC:
* Dropped has_leaf_flag dodging of acpi_pptt_leaf_node()
* Added missing : in kernel-doc
* Made helper return void as this never actually returns an error.

---
 drivers/acpi/pptt.c  | 83 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  3 ++
 2 files changed, 86 insertions(+)
drivers/acpi/pptt.c +83 -0
index 54676e3d82dd..1728545d90b2 100644
@@ -817,3 +817,86 @@ int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 	return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
 					  ACPI_PPTT_ACPI_IDENTICAL);
 }
+
+/**
+ * acpi_pptt_get_child_cpus() - Find all the CPUs below a PPTT processor node
+ * @table_hdr:		A reference to the PPTT table.
+ * @parent_node:	A pointer to the processor node in the @table_hdr.
+ * @cpus:		A cpumask to fill with the CPUs below @parent_node.
+ *
+ * Walks up the PPTT from every possible CPU to find if the provided
+ * @parent_node is a parent of this CPU.
+ */
+static void acpi_pptt_get_child_cpus(struct acpi_table_header *table_hdr,
+				     struct acpi_pptt_processor *parent_node,
+				     cpumask_t *cpus)
+{
+	struct acpi_pptt_processor *cpu_node;
+	u32 acpi_id;
+	int cpu;
+
+	cpumask_clear(cpus);
+
+	for_each_possible_cpu(cpu) {
+		acpi_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table_hdr, acpi_id);
+
+		while (cpu_node) {
+			if (cpu_node == parent_node) {
+				cpumask_set_cpu(cpu, cpus);
+				break;
+			}
+			cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
+		}
+	}
+}
+
+/**
+ * acpi_pptt_get_cpus_from_container() - Populate a cpumask with all CPUs in a
+ *                                       processor container
+ * @acpi_cpu_id:	The UID of the processor container.
+ * @cpus:		The resulting CPU mask.
+ *
+ * Find the specified Processor Container, and fill @cpus with all the cpus
+ * below it.
+ *
+ * Not all 'Processor' entries in the PPTT are either a CPU or a Processor
+ * Container, they may exist purely to describe a Private resource. CPUs
+ * have to be leaves, so a Processor Container is a non-leaf that has the
+ * 'ACPI Processor ID valid' flag set.
+ *
+ * Return: 0 for a complete walk, or an error if the mask is incomplete.
+ */
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
+{
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table_hdr;
+	struct acpi_subtable_header *entry;
+	unsigned long table_end;
+	u32 proc_sz;
+
+	cpumask_clear(cpus);
+
+	table_hdr = acpi_get_pptt();
+	if (!table_hdr)
+		return;
+
+	table_end = (unsigned long)table_hdr + table_hdr->length;
+	entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
+			     sizeof(struct acpi_table_pptt));
+	proc_sz = sizeof(struct acpi_pptt_processor);
+	while ((unsigned long)entry + proc_sz <= table_end) {
+		cpu_node = (struct acpi_pptt_processor *)entry;
+		if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
+		    cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) {
+			if (!acpi_pptt_leaf_node(table_hdr, cpu_node)) {
+				if (cpu_node->acpi_processor_id == acpi_cpu_id) {
+					acpi_pptt_get_child_cpus(table_hdr, cpu_node, cpus);
+					break;
+				}
+			}
+		}
+		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
+				     entry->length);
+	}
+}
include/linux/acpi.h +3 -0
index 1c5bb1e887cd..f97a9ff678cc 100644
@@ -1541,6 +1541,7 @@ int find_acpi_cpu_topology(unsigned int cpu, int level);
 int find_acpi_cpu_topology_cluster(unsigned int cpu);
 int find_acpi_cpu_topology_package(unsigned int cpu);
 int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
 #else
 static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
 {
@@ -1562,6 +1563,8 @@ static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 {
 	return -EINVAL;
 }
+static inline void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,
+						     cpumask_t *cpus) { }
 #endif
 
 void acpi_arch_init(void);
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH v2 01/29] ACPI / PPTT: Add a helper to fill a cpumask from a processor container

Quoted 7 lines · On Wed, 10 Sep 2025 20:42:41 +0000 James Morse <[email protected]> wrote:

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Quoted 7 lines

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

Hi James,

Sorry I missed v1. Busy few weeks.

I think one resource leak plus a few suggested changes that I'm not that bothered about.

Jonathan

Quoted 45 lines

---
Changes since v1:
* Replaced commit message with wording from Dave.
* Fixed a stray plural.
* Moved further down in the file to make use of get_pptt() helper.
* Added a break to exit the loop early.

Changes since RFC:
* Removed leaf_flag local variable from acpi_pptt_get_cpus_from_container()

Changes since RFC:
* Dropped has_leaf_flag dodging of acpi_pptt_leaf_node()
* Added missing : in kernel-doc
* Made helper return void as this never actually returns an error.

---
 drivers/acpi/pptt.c  | 83 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  3 ++
 2 files changed, 86 insertions(+)
drivers/acpi/pptt.c +17 -0
index 54676e3d82dd..1728545d90b2 100644
@@ -817,3 +817,86 @@ int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 	return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
 					  ACPI_PPTT_ACPI_IDENTICAL);
 }
 
+/**
+ * acpi_pptt_get_cpus_from_container() - Populate a cpumask with all CPUs in a
+ *                                       processor container
+ * @acpi_cpu_id:	The UID of the processor container.
+ * @cpus:		The resulting CPU mask.
+ *
+ * Find the specified Processor Container, and fill @cpus with all the cpus
+ * below it.
+ *
+ * Not all 'Processor' entries in the PPTT are either a CPU or a Processor
+ * Container, they may exist purely to describe a Private resource. CPUs
+ * have to be leaves, so a Processor Container is a non-leaf that has the
+ * 'ACPI Processor ID valid' flag set.
+ *
+ * Return: 0 for a complete walk, or an error if the mask is incomplete.
+ */
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 880

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+{
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table_hdr;
+	struct acpi_subtable_header *entry;
+	unsigned long table_end;
+	u32 proc_sz;
+
+	cpumask_clear(cpus);
+
+	table_hdr = acpi_get_pptt();

This calls acpi_get_table() so you need to put it again or every call to this leaks a reference count. I messed around with DEFINE_FREE() for this but it doesn't fit that well as the underlying call doesn't return the table. This one does though so you could do a pptt specific one.

Or just acpi_put_table(table_hdr); at exit path from this function.

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 886

Quoted 6 lines
drivers/acpi/pptt.c +6 -0
+	if (!table_hdr)
+		return;
+
+	table_end = (unsigned long)table_hdr + table_hdr->length;
+	entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
+			     sizeof(struct acpi_table_pptt));

Hmm. Not related to this patch but I have no idea why acpi_get_pptt() doesn't return a struct acpi_table_pptt as if it did this would be a simple
+ 1 and not require those who only sometimes deal with ACPI code to go check what that macro actually does!

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 887

Quoted 1 line
drivers/acpi/pptt.c +1 -0
+	proc_sz = sizeof(struct acpi_pptt_processor);

Maybe sizeof (*cpu_node) is more helpful to reader.
Also shorter so you could do
while ((unsigned long)entry + sizeof(*cpu_node) <= table_end)

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 889

Quoted 2 lines
drivers/acpi/pptt.c +2 -0
+	while ((unsigned long)entry + proc_sz <= table_end) {
+		cpu_node = (struct acpi_pptt_processor *)entry;

For me, assigning this before checking the type is inelegant. but the nesting does get deep without it so I guess this is ok maybe, though I wonder if better reorganized to combine a different bunch of conditions. I think this is functionally identival.

		if (entry->type == ACPI_PTT_TYPE_PROCESSOR) {
			struct acpi_pptt_processor *cpu_node = 
				(struct acpi_pptt_processor *)entry;
			if ((cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) &&
			    (!acpi_pptt_leaf_node(table_hdr, cpu_node) &&
			    (cpu_node->acpi_processor_id == acpi_cpu_id)) {
				acpi_pptt_get_child_cpus(table_hdr, cpu_node, cpus);
				break;
		
			}
		}
		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
				     entry->length);

More generally I wonder if it is worth adding a for_each_acpi_pptt_entry() macro. There is some precedence in drivers acpi such as for_each_nhlt_endpoint()

That's probably material for another day though unless you think it brings enough benefits to do it here.

Quoted 34 lines
drivers/acpi/pptt.c +13 -0
+		if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
+		    cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) {
+			if (!acpi_pptt_leaf_node(table_hdr, cpu_node)) {
+				if (cpu_node->acpi_processor_id == acpi_cpu_id) {
+					acpi_pptt_get_child_cpus(table_hdr, cpu_node, cpus);
+					break;
+				}
+			}
+		}
+		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
+				     entry->length);
+	}
+}
include/linux/acpi.h +3 -0
index 1c5bb1e887cd..f97a9ff678cc 100644
@@ -1541,6 +1541,7 @@ int find_acpi_cpu_topology(unsigned int cpu, int level);
 int find_acpi_cpu_topology_cluster(unsigned int cpu);
 int find_acpi_cpu_topology_package(unsigned int cpu);
 int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
 #else
 static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
 {
@@ -1562,6 +1563,8 @@ static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 {
 	return -EINVAL;
 }
+static inline void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,
+						     cpumask_t *cpus) { }
 #endif
 
 void acpi_arch_init(void);
Reviewed-by
#3 lore raw

Re: [PATCH v2 01/29] ACPI / PPTT: Add a helper to fill a cpumask from a processor container

Quoted 18 lines · On Thu, 11 Sep 2025 11:43:37 +0100 Jonathan Cameron…
Quoted 7 lines · On Wed, 10 Sep 2025 20:42:41 +0000 James Morse <[email protected]> wrote:

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Quoted 7 lines

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

Quoted 6 lines

Hi James,

Sorry I missed v1. Busy few weeks.

I think one resource leak plus a few suggested changes that I'm not that bothered about.

Ignore the resource leak. I didn't read acpi_get_pptt() properly. No bug there.

So consider the comments below, but I'm fine with this as is.

Quoted 149 lines

Jonathan

Quoted 45 lines

---
Changes since v1:
* Replaced commit message with wording from Dave.
* Fixed a stray plural.
* Moved further down in the file to make use of get_pptt() helper.
* Added a break to exit the loop early.

Changes since RFC:
* Removed leaf_flag local variable from acpi_pptt_get_cpus_from_container()

Changes since RFC:
* Dropped has_leaf_flag dodging of acpi_pptt_leaf_node()
* Added missing : in kernel-doc
* Made helper return void as this never actually returns an error.

---
 drivers/acpi/pptt.c  | 83 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  3 ++
 2 files changed, 86 insertions(+)
drivers/acpi/pptt.c +17 -0
index 54676e3d82dd..1728545d90b2 100644
@@ -817,3 +817,86 @@ int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 	return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
 					  ACPI_PPTT_ACPI_IDENTICAL);
 }  
 
+/**
+ * acpi_pptt_get_cpus_from_container() - Populate a cpumask with all CPUs in a
+ *                                       processor container
+ * @acpi_cpu_id:	The UID of the processor container.
+ * @cpus:		The resulting CPU mask.
+ *
+ * Find the specified Processor Container, and fill @cpus with all the cpus
+ * below it.
+ *
+ * Not all 'Processor' entries in the PPTT are either a CPU or a Processor
+ * Container, they may exist purely to describe a Private resource. CPUs
+ * have to be leaves, so a Processor Container is a non-leaf that has the
+ * 'ACPI Processor ID valid' flag set.
+ *
+ * Return: 0 for a complete walk, or an error if the mask is incomplete.
+ */
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 880

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+{
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table_hdr;
+	struct acpi_subtable_header *entry;
+	unsigned long table_end;
+	u32 proc_sz;
+
+	cpumask_clear(cpus);
+
+	table_hdr = acpi_get_pptt();  

This calls acpi_get_table() so you need to put it again or every call to this leaks a reference count. I messed around with DEFINE_FREE() for this but it doesn't fit that well as the underlying call doesn't return the table. This one does though so you could do a pptt specific one.

Or just acpi_put_table(table_hdr); at exit path from this function.

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 886

Quoted 6 lines
drivers/acpi/pptt.c +6 -0
+	if (!table_hdr)
+		return;
+
+	table_end = (unsigned long)table_hdr + table_hdr->length;
+	entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
+			     sizeof(struct acpi_table_pptt));  

Hmm. Not related to this patch but I have no idea why acpi_get_pptt() doesn't return a struct acpi_table_pptt as if it did this would be a simple
+ 1 and not require those who only sometimes deal with ACPI code to go check what that macro actually does!

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 887

Quoted 1 line
drivers/acpi/pptt.c +1 -0
+	proc_sz = sizeof(struct acpi_pptt_processor);  

Maybe sizeof (*cpu_node) is more helpful to reader.
Also shorter so you could do
while ((unsigned long)entry + sizeof(*cpu_node) <= table_end)

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 889

Quoted 2 lines
drivers/acpi/pptt.c +2 -0
+	while ((unsigned long)entry + proc_sz <= table_end) {
+		cpu_node = (struct acpi_pptt_processor *)entry;  

For me, assigning this before checking the type is inelegant. but the nesting does get deep without it so I guess this is ok maybe, though I wonder if better reorganized to combine a different bunch of conditions. I think this is functionally identival.

		if (entry->type == ACPI_PTT_TYPE_PROCESSOR) {
			struct acpi_pptt_processor *cpu_node = 
				(struct acpi_pptt_processor *)entry;
			if ((cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) &&
			    (!acpi_pptt_leaf_node(table_hdr, cpu_node) &&
			    (cpu_node->acpi_processor_id == acpi_cpu_id)) {
				acpi_pptt_get_child_cpus(table_hdr, cpu_node, cpus);
				break;
		
			}
		}
		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
				     entry->length);

More generally I wonder if it is worth adding a for_each_acpi_pptt_entry() macro. There is some precedence in drivers acpi such as for_each_nhlt_endpoint()

That's probably material for another day though unless you think it brings enough benefits to do it here.

Quoted 34 lines
drivers/acpi/pptt.c +13 -0
+		if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
+		    cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) {
+			if (!acpi_pptt_leaf_node(table_hdr, cpu_node)) {
+				if (cpu_node->acpi_processor_id == acpi_cpu_id) {
+					acpi_pptt_get_child_cpus(table_hdr, cpu_node, cpus);
+					break;
+				}
+			}
+		}
+		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
+				     entry->length);
+	}
+}
include/linux/acpi.h +3 -0
index 1c5bb1e887cd..f97a9ff678cc 100644
@@ -1541,6 +1541,7 @@ int find_acpi_cpu_topology(unsigned int cpu, int level);
 int find_acpi_cpu_topology_cluster(unsigned int cpu);
 int find_acpi_cpu_topology_package(unsigned int cpu);
 int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
 #else
 static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
 {
@@ -1562,6 +1563,8 @@ static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 {
 	return -EINVAL;
 }
+static inline void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,
+						     cpumask_t *cpus) { }
 #endif
 
 void acpi_arch_init(void);  
author maintainer
#4 lore raw

Re: [PATCH v2 01/29] ACPI / PPTT: Add a helper to fill a cpumask from a processor container

In reply to Jonathan Cameron

Hi Jonathan,

Quoted 69 lines · Jonathan Cameron
Quoted 7 lines · On Wed, 10 Sep 2025 20:42:41 +0000 James Morse <[email protected]> wrote:

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Quoted 7 lines

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

Hi James,

Sorry I missed v1. Busy few weeks.

I think one resource leak plus a few suggested changes that I'm not that bothered about.

Quoted 26 lines
drivers/acpi/pptt.c +17 -0
index 54676e3d82dd..1728545d90b2 100644
@@ -817,3 +817,86 @@ int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 	return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
 					  ACPI_PPTT_ACPI_IDENTICAL);
 }
 
+/**
+ * acpi_pptt_get_cpus_from_container() - Populate a cpumask with all CPUs in a
+ *                                       processor container
+ * @acpi_cpu_id:	The UID of the processor container.
+ * @cpus:		The resulting CPU mask.
+ *
+ * Find the specified Processor Container, and fill @cpus with all the cpus
+ * below it.
+ *
+ * Not all 'Processor' entries in the PPTT are either a CPU or a Processor
+ * Container, they may exist purely to describe a Private resource. CPUs
+ * have to be leaves, so a Processor Container is a non-leaf that has the
+ * 'ACPI Processor ID valid' flag set.
+ *
+ * Return: 0 for a complete walk, or an error if the mask is incomplete.
+ */
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 880

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+{
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table_hdr;
+	struct acpi_subtable_header *entry;
+	unsigned long table_end;
+	u32 proc_sz;
+
+	cpumask_clear(cpus);
+
+	table_hdr = acpi_get_pptt();

This calls acpi_get_table() so you need to put it again or every call to this leaks a reference count. I messed around with DEFINE_FREE() for this but it doesn't fit that well as the underlying call doesn't return the table. This one does though so you could do a pptt specific one.

Quoted 1 line

Or just acpi_put_table(table_hdr); at exit path from this function.

This is a strange one - you spotted it on your follow up email.

Quoted 11 lines

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 886

Quoted 6 lines
drivers/acpi/pptt.c +6 -0
+	if (!table_hdr)
+		return;
+
+	table_end = (unsigned long)table_hdr + table_hdr->length;
+	entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
+			     sizeof(struct acpi_table_pptt));

Hmm. Not related to this patch but I have no idea why acpi_get_pptt() doesn't return a struct acpi_table_pptt as if it did this would be a simple
+ 1 and not require those who only sometimes deal with ACPI code to go check what that macro actually does!

Looks like that would have a knock on effect for the types in:

| topology_get_acpi_cpu_tag()
| acpi_find_processor_node()
| cache_setup_acpi_cpu()
| fetch_pptt_node()

... basically everywhere this file uses struct acpi_table_header...

It's simpler to cast it here if you're keen on the '+1' approach, but this pattern is how this file does this. This is the same as acpi_pptt_leaf_node().

Quoted 4 lines

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 887

Quoted 1 line
drivers/acpi/pptt.c +1 -0
+	proc_sz = sizeof(struct acpi_pptt_processor);

Maybe sizeof (*cpu_node) is more helpful to reader.

(again, same as acpi_pptt_leaf_node())

Quoted 7 lines

Also shorter so you could do
while ((unsigned long)entry + sizeof(*cpu_node) <= table_end)

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 889

Quoted 2 lines
drivers/acpi/pptt.c +2 -0
+	while ((unsigned long)entry + proc_sz <= table_end) {
+		cpu_node = (struct acpi_pptt_processor *)entry;

For me, assigning this before checking the type is inelegant.

I agree, but when in pptt.c ...

Quoted 4 lines

but the nesting does get deep without it so I guess this is ok maybe, though I wonder if better reorganized to combine a different bunch of conditions. I think this is functionally identival.

Quoted 13 lines
		if (entry->type == ACPI_PTT_TYPE_PROCESSOR) {
			struct acpi_pptt_processor *cpu_node = 
				(struct acpi_pptt_processor *)entry;
			if ((cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) &&
			    (!acpi_pptt_leaf_node(table_hdr, cpu_node) &&
			    (cpu_node->acpi_processor_id == acpi_cpu_id)) {
				acpi_pptt_get_child_cpus(table_hdr, cpu_node, cpus);
				break;
		
			}
		}
		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
				     entry->length);

It is the same - I think this is better as it reduces the scope of cpu_node.

Quoted 2 lines

More generally I wonder if it is worth adding a for_each_acpi_pptt_entry() macro. There is some precedence in drivers acpi such as for_each_nhlt_endpoint()

When this series had support for ACPI PPI-Partitions there were more PPTT helpers and I had a function pointer based call_on_every_entry() kind of thing. Jeremy L though it obscured the flow. A custom for_each_ is probably better.

I may float it as an RFC after these are done. I don't want to make this series any bigger. It certainly makes the users easier on the eye.

| drivers/acpi/pptt.c | 43 +++++++++++++++----------------------------
| 1 file changed, 15 insertions(+), 28 deletions(-)

Stashed here for posterity:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git/commit/?h=pptt/for_each_pptt_entry/v0&id=353ceeba3d39c6b6a10eeb1a59c49649cdf719d8

Quoted 39 lines

That's probably material for another day though unless you think it brings enough benefits to do it here.

Quoted 34 lines
drivers/acpi/pptt.c +13 -0
+		if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
+		    cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) {
+			if (!acpi_pptt_leaf_node(table_hdr, cpu_node)) {
+				if (cpu_node->acpi_processor_id == acpi_cpu_id) {
+					acpi_pptt_get_child_cpus(table_hdr, cpu_node, cpus);
+					break;
+				}
+			}
+		}
+		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
+				     entry->length);
+	}
+}
include/linux/acpi.h +3 -0
index 1c5bb1e887cd..f97a9ff678cc 100644
@@ -1541,6 +1541,7 @@ int find_acpi_cpu_topology(unsigned int cpu, int level);
 int find_acpi_cpu_topology_cluster(unsigned int cpu);
 int find_acpi_cpu_topology_package(unsigned int cpu);
 int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
 #else
 static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
 {
@@ -1562,6 +1563,8 @@ static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 {
 	return -EINVAL;
 }
+static inline void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,
+						     cpumask_t *cpus) { }
 #endif
 
 void acpi_arch_init(void);
#5 lore raw

Re: [PATCH v2 01/29] ACPI / PPTT: Add a helper to fill a cpumask from a processor container

In reply to James Morse

Hi James,

Quoted 33 lines · James Morse

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

---
Changes since v1:
* Replaced commit message with wording from Dave.
* Fixed a stray plural.
* Moved further down in the file to make use of get_pptt() helper.
* Added a break to exit the loop early.

Changes since RFC:
* Removed leaf_flag local variable from acpi_pptt_get_cpus_from_container()

Changes since RFC:
* Dropped has_leaf_flag dodging of acpi_pptt_leaf_node()
* Added missing : in kernel-doc
* Made helper return void as this never actually returns an error.

---
 drivers/acpi/pptt.c  | 83 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  3 ++
 2 files changed, 86 insertions(+)

<snip>

Quoted 6 lines
drivers/acpi/pptt.c +6 -0
+
+/**
+ * acpi_pptt_get_cpus_from_container() - Populate a cpumask with all CPUs in a
+ *                                       processor container
+ * @acpi_cpu_id:	The UID of the processor container.
+ * @cpus:		The resulting CPU mask.

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 868

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+ *
+ * Find the specified Processor Container, and fill @cpus with all the cpus
+ * below it.
+ *
+ * Not all 'Processor' entries in the PPTT are either a CPU or a Processor
+ * Container, they may exist purely to describe a Private resource. CPUs
+ * have to be leaves, so a Processor Container is a non-leaf that has the
+ * 'ACPI Processor ID valid' flag set.
+ *
+ * Return: 0 for a complete walk, or an error if the mask is incomplete.

Leftover, drop this.

drivers/acpi/pptt.c int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 874

Quoted 6 lines

+ */

drivers/acpi/pptt.c +5 -0
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
+{
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table_hdr;
+	struct acpi_subtable_header *entry;

<snip>

regards,
~Stan

author maintainer
#6 lore raw

Re: [PATCH v2 01/29] ACPI / PPTT: Add a helper to fill a cpumask from a processor container

Hi Stan,

Quoted 22 lines · Stanimir Varbanov
Quoted 21 lines · James Morse

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

drivers/acpi/pptt.c +10 -0
+
+/**
+ * acpi_pptt_get_cpus_from_container() - Populate a cpumask with all CPUs in a
+ *                                       processor container
+ * @acpi_cpu_id:	The UID of the processor container.
+ * @cpus:		The resulting CPU mask.
+ *
+ * Find the specified Processor Container, and fill @cpus with all the cpus
+ * below it.
+ *
Quoted 8 lines
Quoted 6 lines

+ * Not all 'Processor' entries in the PPTT are either a CPU or a Processor
+ * Container, they may exist purely to describe a Private resource. CPUs
+ * have to be leaves, so a Processor Container is a non-leaf that has the
+ * 'ACPI Processor ID valid' flag set.
+ *
+ * Return: 0 for a complete walk, or an error if the mask is incomplete.

Leftover, drop this.

Good spot - thanks,

James

reviewer Reviewed-by on v2 1/29
#7 lore raw

Re: [PATCH v2 01/29] ACPI / PPTT: Add a helper to fill a cpumask from a processor container

In reply to James Morse
Quoted 7 lines · James Morse

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Quoted 7 lines

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

Thanks.

-Fenghua

author maintainer
#8 lore raw

Re: [PATCH v2 01/29] ACPI / PPTT: Add a helper to fill a cpumask from a processor container

Hi Fenghua,

Quoted 12 lines · Fenghua Yu
Quoted 11 lines · James Morse

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

Quoted 5 lines
Quoted 3 lines

Thanks!

James

Reviewed-by on v2 1/29
#9 lore raw

Re: [PATCH v2 01/29] ACPI / PPTT: Add a helper to fill a cpumask from a processor container

In reply to James Morse
Quoted 24 lines · James Morse

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

---
Changes since v1:
* Replaced commit message with wording from Dave.
* Fixed a stray plural.
* Moved further down in the file to make use of get_pptt() helper.
* Added a break to exit the loop early.

Changes since RFC:
* Removed leaf_flag local variable from acpi_pptt_get_cpus_from_container()

Quoted 9 lines

Changes since RFC:
* Dropped has_leaf_flag dodging of acpi_pptt_leaf_node()
* Added missing : in kernel-doc
* Made helper return void as this never actually returns an error.

---
  drivers/acpi/pptt.c  | 83 ++++++++++++++++++++++++++++++++++++++++++++
  include/linux/acpi.h |  3 ++
  2 files changed, 86 insertions(+)

With the description for the return value of acpi_pptt_get_cpus_from_container() is dropped since that function doesn't have a return value, as mentioned by Stanimir Varbanov.

Thanks,
Gavin

Quoted 112 lines
drivers/acpi/pptt.c +83 -0
index 54676e3d82dd..1728545d90b2 100644
@@ -817,3 +817,86 @@ int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
  	return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
  					  ACPI_PPTT_ACPI_IDENTICAL);
  }
+
+/**
+ * acpi_pptt_get_child_cpus() - Find all the CPUs below a PPTT processor node
+ * @table_hdr:		A reference to the PPTT table.
+ * @parent_node:	A pointer to the processor node in the @table_hdr.
+ * @cpus:		A cpumask to fill with the CPUs below @parent_node.
+ *
+ * Walks up the PPTT from every possible CPU to find if the provided
+ * @parent_node is a parent of this CPU.
+ */
+static void acpi_pptt_get_child_cpus(struct acpi_table_header *table_hdr,
+				     struct acpi_pptt_processor *parent_node,
+				     cpumask_t *cpus)
+{
+	struct acpi_pptt_processor *cpu_node;
+	u32 acpi_id;
+	int cpu;
+
+	cpumask_clear(cpus);
+
+	for_each_possible_cpu(cpu) {
+		acpi_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table_hdr, acpi_id);
+
+		while (cpu_node) {
+			if (cpu_node == parent_node) {
+				cpumask_set_cpu(cpu, cpus);
+				break;
+			}
+			cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
+		}
+	}
+}
+
+/**
+ * acpi_pptt_get_cpus_from_container() - Populate a cpumask with all CPUs in a
+ *                                       processor container
+ * @acpi_cpu_id:	The UID of the processor container.
+ * @cpus:		The resulting CPU mask.
+ *
+ * Find the specified Processor Container, and fill @cpus with all the cpus
+ * below it.
+ *
+ * Not all 'Processor' entries in the PPTT are either a CPU or a Processor
+ * Container, they may exist purely to describe a Private resource. CPUs
+ * have to be leaves, so a Processor Container is a non-leaf that has the
+ * 'ACPI Processor ID valid' flag set.
+ *
+ * Return: 0 for a complete walk, or an error if the mask is incomplete.
+ */
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
+{
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table_hdr;
+	struct acpi_subtable_header *entry;
+	unsigned long table_end;
+	u32 proc_sz;
+
+	cpumask_clear(cpus);
+
+	table_hdr = acpi_get_pptt();
+	if (!table_hdr)
+		return;
+
+	table_end = (unsigned long)table_hdr + table_hdr->length;
+	entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
+			     sizeof(struct acpi_table_pptt));
+	proc_sz = sizeof(struct acpi_pptt_processor);
+	while ((unsigned long)entry + proc_sz <= table_end) {
+		cpu_node = (struct acpi_pptt_processor *)entry;
+		if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
+		    cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) {
+			if (!acpi_pptt_leaf_node(table_hdr, cpu_node)) {
+				if (cpu_node->acpi_processor_id == acpi_cpu_id) {
+					acpi_pptt_get_child_cpus(table_hdr, cpu_node, cpus);
+					break;
+				}
+			}
+		}
+		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
+				     entry->length);
+	}
+}
include/linux/acpi.h +3 -0
index 1c5bb1e887cd..f97a9ff678cc 100644
@@ -1541,6 +1541,7 @@ int find_acpi_cpu_topology(unsigned int cpu, int level);
  int find_acpi_cpu_topology_cluster(unsigned int cpu);
  int find_acpi_cpu_topology_package(unsigned int cpu);
  int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
+void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
  #else
  static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
  {
@@ -1562,6 +1563,8 @@ static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
  {
  	return -EINVAL;
  }
+static inline void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,
+						     cpumask_t *cpus) { }
  #endif
 
  void acpi_arch_init(void);
author maintainer
#10 lore raw

Re: [PATCH v2 01/29] ACPI / PPTT: Add a helper to fill a cpumask from a processor container

Hi Gavin,

Quoted 12 lines · Gavin Shan
Quoted 10 lines · James Morse

The ACPI MPAM table uses the UID of a processor container specified in the PPTT to indicate the subset of CPUs and cache topology that can access each MPAM System Component (MSC).

This information is not directly useful to the kernel. The equivalent cpumask is needed instead.

Add a helper to find the processor container by its id, then walk the possible CPUs to fill a cpumask with the CPUs that have this processor container as a parent.

Quoted 5 lines

With the description for the return value of acpi_pptt_get_cpus_from_container() is dropped since that function doesn't have a return value, as mentioned by Stanimir Varbanov.

Thanks!

James

author maintainer
#11 lore raw

PATCH v2 2/29 ACPI / PPTT: Stop acpi_count_levels() expecting callers to clear levels

In reply to James Morse

In acpi_count_levels(), the initial value of *levels passed by the caller is really an implementation detail of acpi_count_levels(), so it is unreasonable to expect the callers of this function to know what to pass in for this parameter. The only sensible initial value is 0, which is what the only upstream caller (acpi_get_cache_info()) passes.

Use a local variable for the starting cache level in acpi_count_levels(), and pass the result back to the caller via the function return value.

Gid rid of the levels parameter, which has no remaining purpose.

Fix acpi_get_cache_info() to match.

---
Changes since v1:
* Rewritten commit message from Dave.
* Minor changes to kernel doc comment.
* Keep the much loved typo.

Changes since RFC:
* Made acpi_count_levels() return the levels value.

---
 drivers/acpi/pptt.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
drivers/acpi/pptt.c +12 -8
index 1728545d90b2..7af7d62597df 100644
@@ -177,14 +177,14 @@ acpi_find_cache_level(struct acpi_table_header *table_hdr,
 }
 
 /**
- * acpi_count_levels() - Given a PPTT table, and a CPU node, count the cache
- * levels and split cache levels (data/instruction).
+ * acpi_count_levels() - Given a PPTT table, and a CPU node, count the
+ * total number of levels and split cache levels (data/instruction).
  * @table_hdr: Pointer to the head of the PPTT table
  * @cpu_node: processor node we wish to count caches for
- * @levels: Number of levels if success.
  * @split_levels:	Number of split cache levels (data/instruction) if
  *			success. Can by NULL.
  *
+ * Return: number of levels.
  * Given a processor node containing a processing unit, walk into it and count
  * how many levels exist solely for it, and then walk up each level until we hit
  * the root node (ignore the package level because it may be possible to have
@@ -192,14 +192,18 @@ acpi_find_cache_level(struct acpi_table_header *table_hdr,
  * split cache levels (data/instruction) that exist at each level on the way
  * up.
  */
-static void acpi_count_levels(struct acpi_table_header *table_hdr,
-			      struct acpi_pptt_processor *cpu_node,
-			      unsigned int *levels, unsigned int *split_levels)
+static int acpi_count_levels(struct acpi_table_header *table_hdr,
+			     struct acpi_pptt_processor *cpu_node,
+			     unsigned int *split_levels)
 {
+	int starting_level = 0;
+
 	do {
-		acpi_find_cache_level(table_hdr, cpu_node, levels, split_levels, 0, 0);
+		acpi_find_cache_level(table_hdr, cpu_node, &starting_level, split_levels, 0, 0);
 		cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
 	} while (cpu_node);
+
+	return starting_level;
 }
 
 /**
@@ -645,7 +649,7 @@ int acpi_get_cache_info(unsigned int cpu, unsigned int *levels,
 	if (!cpu_node)
 		return -ENOENT;
 
-	acpi_count_levels(table, cpu_node, levels, split_levels);
+	*levels = acpi_count_levels(table, cpu_node, split_levels);
 
 	pr_debug("Cache Setup: last_level=%d split_levels=%d\n",
 		 *levels, split_levels ? *split_levels : -1);
Signature · 1 line
2.39.5
Reviewed-by on v2 2/29
#12 lore raw

Re: [PATCH v2 02/29] ACPI / PPTT: Stop acpi_count_levels() expecting callers to clear levels

Quoted 9 lines · On Wed, 10 Sep 2025 20:42:42 +0000 James Morse <[email protected]> wrote:

In acpi_count_levels(), the initial value of *levels passed by the caller is really an implementation detail of acpi_count_levels(), so it is unreasonable to expect the callers of this function to know what to pass in for this parameter. The only sensible initial value is 0, which is what the only upstream caller (acpi_get_cache_info()) passes.

Use a local variable for the starting cache level in acpi_count_levels(), and pass the result back to the caller via the function return value.

Quoted 7 lines

Gid rid of the levels parameter, which has no remaining purpose.

Fix acpi_get_cache_info() to match.

author maintainer
#13 lore raw

Re: [PATCH v2 02/29] ACPI / PPTT: Stop acpi_count_levels() expecting callers to clear levels

Hi Jonathan,

Quoted 12 lines · Jonathan Cameron
Quoted 9 lines · On Wed, 10 Sep 2025 20:42:42 +0000 James Morse <[email protected]> wrote:

In acpi_count_levels(), the initial value of *levels passed by the caller is really an implementation detail of acpi_count_levels(), so it is unreasonable to expect the callers of this function to know what to pass in for this parameter. The only sensible initial value is 0, which is what the only upstream caller (acpi_get_cache_info()) passes.

Use a local variable for the starting cache level in acpi_count_levels(), and pass the result back to the caller via the function return value.

Quoted 8 lines
Quoted 7 lines

Gid rid of the levels parameter, which has no remaining purpose.

Fix acpi_get_cache_info() to match.

Thanks!

James

maintainer
#14 lore raw

Re: [PATCH v2 02/29] ACPI / PPTT: Stop acpi_count_levels() expecting callers to clear levels

In reply to James Morse

Hi James,

Quoted 10 lines · James Morse

In acpi_count_levels(), the initial value of *levels passed by the caller is really an implementation detail of acpi_count_levels(), so it is unreasonable to expect the callers of this function to know what to pass in for this parameter. The only sensible initial value is 0, which is what the only upstream caller (acpi_get_cache_info()) passes.

Use a local variable for the starting cache level in acpi_count_levels(), and pass the result back to the caller via the function return value.

Gid rid of the levels parameter, which has no remaining purpose.

Nit: s/Gid/Get/

Quoted 7 lines

Fix acpi_get_cache_info() to match.

---

Thanks,

Ben

author maintainer
#15 lore raw

Re: [PATCH v2 02/29] ACPI / PPTT: Stop acpi_count_levels() expecting callers to clear levels

Hi Ben,

Quoted 7 lines · Ben Horgan
Quoted 6 lines · James Morse

In acpi_count_levels(), the initial value of *levels passed by the caller is really an implementation detail of acpi_count_levels(), so it is unreasonable to expect the callers of this function to know what to pass in for this parameter. The only sensible initial value is 0, which is what the only upstream caller (acpi_get_cache_info()) passes.

Quoted 6 lines
Quoted 4 lines

Use a local variable for the starting cache level in acpi_count_levels(), and pass the result back to the caller via the function return value.

Gid rid of the levels parameter, which has no remaining purpose.

Nit: s/Gid/Get/

Oops,

Thanks,

James

reviewer Reviewed-by on v2 2/29
#16 lore raw

Re: [PATCH v2 02/29] ACPI / PPTT: Stop acpi_count_levels() expecting callers to clear levels

In reply to James Morse
Quoted 9 lines · James Morse

In acpi_count_levels(), the initial value of *levels passed by the caller is really an implementation detail of acpi_count_levels(), so it is unreasonable to expect the callers of this function to know what to pass in for this parameter. The only sensible initial value is 0, which is what the only upstream caller (acpi_get_cache_info()) passes.

Use a local variable for the starting cache level in acpi_count_levels(), and pass the result back to the caller via the function return value.

Quoted 7 lines

Gid rid of the levels parameter, which has no remaining purpose.

Fix acpi_get_cache_info() to match.

Thanks.

-Fenghua

author maintainer
#17 lore raw

Re: [PATCH v2 02/29] ACPI / PPTT: Stop acpi_count_levels() expecting callers to clear levels

Hi Fenghua,

Quoted 12 lines · Fenghua Yu
Quoted 11 lines · James Morse

In acpi_count_levels(), the initial value of *levels passed by the caller is really an implementation detail of acpi_count_levels(), so it is unreasonable to expect the callers of this function to know what to pass in for this parameter.  The only sensible initial value is 0, which is what the only upstream caller (acpi_get_cache_info()) passes.

Use a local variable for the starting cache level in acpi_count_levels(), and pass the result back to the caller via the function return value.

Gid rid of the levels parameter, which has no remaining purpose.

Quoted 7 lines
Quoted 5 lines

Fix acpi_get_cache_info() to match.

Thanks!

James

Reviewed-by on v2 2/29
#18 lore raw

Re: [PATCH v2 02/29] ACPI / PPTT: Stop acpi_count_levels() expecting callers to clear levels

In reply to James Morse
Quoted 22 lines · James Morse

In acpi_count_levels(), the initial value of *levels passed by the caller is really an implementation detail of acpi_count_levels(), so it is unreasonable to expect the callers of this function to know what to pass in for this parameter. The only sensible initial value is 0, which is what the only upstream caller (acpi_get_cache_info()) passes.

Use a local variable for the starting cache level in acpi_count_levels(), and pass the result back to the caller via the function return value.

Gid rid of the levels parameter, which has no remaining purpose.

Fix acpi_get_cache_info() to match.

---
Changes since v1:
* Rewritten commit message from Dave.
* Minor changes to kernel doc comment.
* Keep the much loved typo.

Quoted 6 lines

Changes since RFC:
* Made acpi_count_levels() return the levels value.

---
  drivers/acpi/pptt.c | 20 ++++++++++++--------
  1 file changed, 12 insertions(+), 8 deletions(-)
author maintainer
#19 lore raw

PATCH v2 3/29 ACPI / PPTT: Find cache level by cache-id

In reply to James Morse

The MPAM table identifies caches by id. The MPAM driver also wants to know the cache level to determine if the platform is of the shape that can be managed via resctrl. Cacheinfo has this information, but only for CPUs that are online.

Waiting for all CPUs to come online is a problem for platforms where CPUs are brought online late by user-space.

Add a helper that walks every possible cache, until it finds the one identified by cache-id, then return the level.

---
Changes since v1:
* Droppeed the cleanup based table freeing, use acpi_get_pptt() instead.
* Removed a confusing comment.
* Clarified the kernel doc.

Changes since RFC:
* acpi_count_levels() now returns a value.
* Converted the table-get stuff to use Jonathan's cleanup helper.
* Dropped Sudeep's Review tag due to the cleanup change.

---
 drivers/acpi/pptt.c  | 62 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  5 ++++
 2 files changed, 67 insertions(+)
drivers/acpi/pptt.c +62 -0
index 7af7d62597df..c5f2a51d280b 100644
@@ -904,3 +904,65 @@ void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
 				     entry->length);
 	}
 }
+
+/*
+ * find_acpi_cache_level_from_id() - Get the level of the specified cache
+ * @cache_id: The id field of the unified cache
+ *
+ * Determine the level relative to any CPU for the unified cache identified by
+ * cache_id. This allows the property to be found even if the CPUs are offline.
+ *
+ * The returned level can be used to group unified caches that are peers.
+ *
+ * The PPTT table must be rev 3 or later,
+ *
+ * If one CPUs L2 is shared with another as L3, this function will return
+ * an unpredictable value.
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, the revision isn't supported or
+ * the cache cannot be found.
+ * Otherwise returns a value which represents the level of the specified cache.
+ */
+int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_table_header *table;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+
+	table = acpi_get_pptt();
+	if (!table)
+		return -ENOENT;
+
+	if (table->revision < 3)
+		return -ENOENT;
+
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (!cpu_node)
+			return -ENOENT;
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				return level;
+		}
+	}
+
+	return -ENOENT;
+}
include/linux/acpi.h +5 -0
index f97a9ff678cc..5bdca5546697 100644
@@ -1542,6 +1542,7 @@ int find_acpi_cpu_topology_cluster(unsigned int cpu);
 int find_acpi_cpu_topology_package(unsigned int cpu);
 int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
 void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
+int find_acpi_cache_level_from_id(u32 cache_id);
 #else
 static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
 {
@@ -1565,6 +1566,10 @@ static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 }
 static inline void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,
 						     cpumask_t *cpus) { }
+static inline int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	return -EINVAL;
+}
 #endif
 
 void acpi_arch_init(void);
Signature · 1 line
2.39.5
#20 lore raw

Re: [PATCH v2 03/29] ACPI / PPTT: Find cache level by cache-id

Quoted 32 lines · On Wed, 10 Sep 2025 20:42:43 +0000 James Morse <[email protected]> wrote:

The MPAM table identifies caches by id. The MPAM driver also wants to know the cache level to determine if the platform is of the shape that can be managed via resctrl. Cacheinfo has this information, but only for CPUs that are online.

Waiting for all CPUs to come online is a problem for platforms where CPUs are brought online late by user-space.

Add a helper that walks every possible cache, until it finds the one identified by cache-id, then return the level.

---
Changes since v1:
* Droppeed the cleanup based table freeing, use acpi_get_pptt() instead.
* Removed a confusing comment.
* Clarified the kernel doc.

Changes since RFC:
* acpi_count_levels() now returns a value.
* Converted the table-get stuff to use Jonathan's cleanup helper.
* Dropped Sudeep's Review tag due to the cleanup change.

---
 drivers/acpi/pptt.c  | 62 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  5 ++++
 2 files changed, 67 insertions(+)
drivers/acpi/pptt.c +0 -0
index 7af7d62597df..c5f2a51d280b 100644

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 908

Quoted 5 lines
drivers/acpi/pptt.c +2 -0
@@ -904,3 +904,65 @@ void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
 				     entry->length);
 	}
 }
+
+/*

/**

It's an exposed interface so nice to have formal kernel-doc and automatic checks that brings.

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 915

Quoted 7 lines
drivers/acpi/pptt.c +7 -0
+ * find_acpi_cache_level_from_id() - Get the level of the specified cache
+ * @cache_id: The id field of the unified cache
+ *
+ * Determine the level relative to any CPU for the unified cache identified by
+ * cache_id. This allows the property to be found even if the CPUs are offline.
+ *
+ * The returned level can be used to group unified caches that are peers.

Silly question but why do we care if this a unified cache?
It's a bit odd to have a general sounding function fail for split caches. The handling would have to be more complex but if we really don't want to do it maybe rename the function to find_acpi_unifiedcache_level_from_id() and if the general version gets added later we can switch to that.

Quoted 34 lines
drivers/acpi/pptt.c +34 -0
+ *
+ * The PPTT table must be rev 3 or later,
+ *
+ * If one CPUs L2 is shared with another as L3, this function will return
+ * an unpredictable value.
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, the revision isn't supported or
+ * the cache cannot be found.
+ * Otherwise returns a value which represents the level of the specified cache.
+ */
+int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_table_header *table;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+
+	table = acpi_get_pptt();
+	if (!table)
+		return -ENOENT;
+
+	if (table->revision < 3)
+		return -ENOENT;
+
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (!cpu_node)
+			return -ENOENT;
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 959

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));

sizeof(*cache) to me makes this more obvious.

Quoted 9 lines
drivers/acpi/pptt.c +9 -0
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				return level;
+		}
+	}
+
+	return -ENOENT;
+}
author maintainer
#21 lore raw

Re: [PATCH v2 03/29] ACPI / PPTT: Find cache level by cache-id

Hi Jonathan,

Quoted 37 lines · Jonathan Cameron
Quoted 16 lines · On Wed, 10 Sep 2025 20:42:43 +0000 James Morse <[email protected]> wrote:

The MPAM table identifies caches by id. The MPAM driver also wants to know the cache level to determine if the platform is of the shape that can be managed via resctrl. Cacheinfo has this information, but only for CPUs that are online.

Waiting for all CPUs to come online is a problem for platforms where CPUs are brought online late by user-space.

Add a helper that walks every possible cache, until it finds the one identified by cache-id, then return the level.

drivers/acpi/pptt.c +0 -0
index 7af7d62597df..c5f2a51d280b 100644

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 908

Quoted 5 lines
drivers/acpi/pptt.c +2 -0
@@ -904,3 +904,65 @@ void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
 				     entry->length);
 	}
 }
+
+/*

/**

It's an exposed interface so nice to have formal kernel-doc and automatic checks that brings.

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 915

Quoted 7 lines
drivers/acpi/pptt.c +7 -0
+ * find_acpi_cache_level_from_id() - Get the level of the specified cache
+ * @cache_id: The id field of the unified cache
+ *
+ * Determine the level relative to any CPU for the unified cache identified by
+ * cache_id. This allows the property to be found even if the CPUs are offline.
+ *
+ * The returned level can be used to group unified caches that are peers.
Quoted 1 line

Silly question but why do we care if this a unified cache?

/me returns from the time-machine trip....

This is legacy, but results in parity with the DT approach.
Really early versions of this generated an ID based on the associated CPUs - like DT does/would-do today. This value isn't unique for non-unified caches as they have the same set of CPUs below them, so every use of cache-id used to have to check it was a unified cache. This isn't a problem for MPAM as there is never likely to be an L1 MSC, but you're right- it hinders re-use of this.

Since ACPI then went and added the ID to the PPTT, we don't need to check this here.

Quoted 4 lines

It's a bit odd to have a general sounding function fail for split caches. The handling would have to be more complex but if we really don't want to do it maybe rename the function to find_acpi_unifiedcache_level_from_id() and if the general version gets added later we can switch to that.

I'll add the extra work - this avoids the call to acpi_count_levels() which was annoying Dave as its another walk of the whole table. (I didn't twig in that conversation that the unified check may no longer be necessary)

Quoted 38 lines
Quoted 38 lines
drivers/acpi/pptt.c +38 -0
+ *
+ * The PPTT table must be rev 3 or later,
+ *
+ * If one CPUs L2 is shared with another as L3, this function will return
+ * an unpredictable value.
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, the revision isn't supported or
+ * the cache cannot be found.
+ * Otherwise returns a value which represents the level of the specified cache.
+ */
+int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_table_header *table;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+
+	table = acpi_get_pptt();
+	if (!table)
+		return -ENOENT;
+
+	if (table->revision < 3)
+		return -ENOENT;
+
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (!cpu_node)
+			return -ENOENT;
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);
Quoted 8 lines

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 959

Quoted 6 lines
drivers/acpi/pptt.c +6 -0
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));

sizeof(*cache) to me makes this more obvious.

Would be the only instance of this in the file - but I agree its more readable, and results in fewer line breaks, which also helps.

Quoted 9 lines

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 968

Quoted 9 lines
drivers/acpi/pptt.c +9 -0
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				return level;
+		}
+	}
+
+	return -ENOENT;
+}

Thanks,

James

maintainer Reviewed-by on v2 3/29
#22 lore raw

Re: [PATCH v2 03/29] ACPI / PPTT: Find cache level by cache-id

In reply to James Morse
Quoted 36 lines · James Morse

The MPAM table identifies caches by id. The MPAM driver also wants to know the cache level to determine if the platform is of the shape that can be managed via resctrl. Cacheinfo has this information, but only for CPUs that are online.

Waiting for all CPUs to come online is a problem for platforms where CPUs are brought online late by user-space.

Add a helper that walks every possible cache, until it finds the one identified by cache-id, then return the level.

---
Changes since v1:
* Droppeed the cleanup based table freeing, use acpi_get_pptt() instead.
* Removed a confusing comment.
* Clarified the kernel doc.

Changes since RFC:
* acpi_count_levels() now returns a value.
* Converted the table-get stuff to use Jonathan's cleanup helper.
* Dropped Sudeep's Review tag due to the cleanup change.

---
 drivers/acpi/pptt.c  | 62 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  5 ++++
 2 files changed, 67 insertions(+)
drivers/acpi/pptt.c +1 -0
index 7af7d62597df..c5f2a51d280b 100644
@@ -904,3 +904,65 @@ void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
 				     entry->length);
 	}
 }
+

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 917

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+/*
+ * find_acpi_cache_level_from_id() - Get the level of the specified cache
+ * @cache_id: The id field of the unified cache
+ *
+ * Determine the level relative to any CPU for the unified cache identified by
+ * cache_id. This allows the property to be found even if the CPUs are offline.
+ *
+ * The returned level can be used to group unified caches that are peers.
+ *
+ * The PPTT table must be rev 3 or later,

* The PPTT table must be rev 3 or later.

Quoted 19 lines
drivers/acpi/pptt.c +19 -0
+ *
+ * If one CPUs L2 is shared with another as L3, this function will return
+ * an unpredictable value.
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, the revision isn't supported or
+ * the cache cannot be found.
+ * Otherwise returns a value which represents the level of the specified cache.
+ */
+int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_table_header *table;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+
+	table = acpi_get_pptt();
+	if (!table)

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 946

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+		return -ENOENT;
+
+	if (table->revision < 3)
+		return -ENOENT;
+
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (!cpu_node)
+			return -ENOENT;

Same comment as in another patch - don't think you want to stop parsing here.

Quoted 35 lines
drivers/acpi/pptt.c +22 -0
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				return level;
+		}
+	}
+
+	return -ENOENT;
+}
include/linux/acpi.h +1 -0
index f97a9ff678cc..5bdca5546697 100644
@@ -1542,6 +1542,7 @@ int find_acpi_cpu_topology_cluster(unsigned int cpu);
 int find_acpi_cpu_topology_package(unsigned int cpu);
 int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
 void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
+int find_acpi_cache_level_from_id(u32 cache_id);
 #else
 static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
 {

include/linux/acpi.h static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 1571

Quoted 6 lines
include/linux/acpi.h +3 -0
@@ -1565,6 +1566,10 @@ static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
 }
 static inline void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,
 						     cpumask_t *cpus) { }
+static inline int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	return -EINVAL;

return -ENOENT;

Quoted 7 lines
include/linux/acpi.h +1 -0
+}
 #endif
 
 void acpi_arch_init(void);
Signature · 1 line
2.39.5
author maintainer
#23 lore raw

Re: [PATCH v2 03/29] ACPI / PPTT: Find cache level by cache-id

Hi Lorenzo,

Quoted 25 lines · Lorenzo Pieralisi
Quoted 24 lines · James Morse

The MPAM table identifies caches by id. The MPAM driver also wants to know the cache level to determine if the platform is of the shape that can be managed via resctrl. Cacheinfo has this information, but only for CPUs that are online.

Waiting for all CPUs to come online is a problem for platforms where CPUs are brought online late by user-space.

Add a helper that walks every possible cache, until it finds the one identified by cache-id, then return the level.

drivers/acpi/pptt.c +5 -0
index 7af7d62597df..c5f2a51d280b 100644
@@ -904,3 +904,65 @@ void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
 				     entry->length);
 	}
 }
+
+/*
+ * find_acpi_cache_level_from_id() - Get the level of the specified cache
+ * @cache_id: The id field of the unified cache
+ *
Quoted 8 lines

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 917

Quoted 6 lines
drivers/acpi/pptt.c +6 -0
+ * Determine the level relative to any CPU for the unified cache identified by
+ * cache_id. This allows the property to be found even if the CPUs are offline.
+ *
+ * The returned level can be used to group unified caches that are peers.
+ *
+ * The PPTT table must be rev 3 or later,

* The PPTT table must be rev 3 or later.

Fixed,

Quoted 24 lines
Quoted 24 lines
drivers/acpi/pptt.c +24 -0
+ *
+ * If one CPUs L2 is shared with another as L3, this function will return
+ * an unpredictable value.
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, the revision isn't supported or
+ * the cache cannot be found.
+ * Otherwise returns a value which represents the level of the specified cache.
+ */
+int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_table_header *table;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+
+	table = acpi_get_pptt();
+	if (!table)
+		return -ENOENT;
+
+	if (table->revision < 3)
+		return -ENOENT;
+
Quoted 8 lines

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 946

Quoted 5 lines
drivers/acpi/pptt.c +5 -0
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (!cpu_node)
+			return -ENOENT;

Same comment as in another patch - don't think you want to stop parsing here.

Yes - this is me throwing my hands up in the air at a nonsense firmware table. 'continue' would be a try harder approach.

Quoted 27 lines
Quoted 27 lines
drivers/acpi/pptt.c +22 -0
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				return level;
+		}
+	}
+
+	return -ENOENT;
+}
include/linux/acpi.h +0 -0
index f97a9ff678cc..5bdca5546697 100644
@@ -1565,6 +1566,10 @@ static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
Quoted 8 lines

include/linux/acpi.h static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 1571

Quoted 6 lines
include/linux/acpi.h +3 -0
 }
 static inline void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,
 						     cpumask_t *cpus) { }
+static inline int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	return -EINVAL;

return -ENOENT;

Yes, same bug as before. Fixed.

Quoted 1 line

Thanks - I think the change to ignoring unified caches and searching all types is substantive enough that I won't pick this up because of the changes for v3...

Thanks,

James

reviewer Reviewed-by on v2 3/29
#24 lore raw

Re: [PATCH v2 03/29] ACPI / PPTT: Find cache level by cache-id

In reply to James Morse
Quoted 5 lines · James Morse

The MPAM table identifies caches by id. The MPAM driver also wants to know the cache level to determine if the platform is of the shape that can be managed via resctrl. Cacheinfo has this information, but only for CPUs that are online.

Quoted 7 lines

Waiting for all CPUs to come online is a problem for platforms where CPUs are brought online late by user-space.

Add a helper that walks every possible cache, until it finds the one identified by cache-id, then return the level.

Other than minor comment issues as follows,

Quoted 24 lines

---
Changes since v1:
* Droppeed the cleanup based table freeing, use acpi_get_pptt() instead.
* Removed a confusing comment.
* Clarified the kernel doc.

Changes since RFC:
* acpi_count_levels() now returns a value.
* Converted the table-get stuff to use Jonathan's cleanup helper.
* Dropped Sudeep's Review tag due to the cleanup change.

---
  drivers/acpi/pptt.c  | 62 ++++++++++++++++++++++++++++++++++++++++++++
  include/linux/acpi.h |  5 ++++
  2 files changed, 67 insertions(+)
drivers/acpi/pptt.c +1 -0
index 7af7d62597df..c5f2a51d280b 100644
@@ -904,3 +904,65 @@ void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
  				     entry->length);
  	}
  }
+

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 917

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+/*
+ * find_acpi_cache_level_from_id() - Get the level of the specified cache
+ * @cache_id: The id field of the unified cache
+ *
+ * Determine the level relative to any CPU for the unified cache identified by
+ * cache_id. This allows the property to be found even if the CPUs are offline.
+ *
+ * The returned level can be used to group unified caches that are peers.
+ *
+ * The PPTT table must be rev 3 or later,

s/,/./

drivers/acpi/pptt.c void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus) · line 919

Quoted 2 lines
drivers/acpi/pptt.c +2 -0
+ *
+ * If one CPUs L2 is shared with another as L3, this function will return

This comment is not clear. Maybe it's better to say:

+ * If one CPU's L2 is shared with another CPU as L3, this function will return

Quoted 62 lines
drivers/acpi/pptt.c +49 -0
+ * an unpredictable value.
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, the revision isn't supported or
+ * the cache cannot be found.
+ * Otherwise returns a value which represents the level of the specified cache.
+ */
+int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_table_header *table;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+
+	table = acpi_get_pptt();
+	if (!table)
+		return -ENOENT;
+
+	if (table->revision < 3)
+		return -ENOENT;
+
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (!cpu_node)
+			return -ENOENT;
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				return level;
+		}
+	}
+
+	return -ENOENT;
+}
include/linux/acpi.h +1 -0
index f97a9ff678cc..5bdca5546697 100644
@@ -1542,6 +1542,7 @@ int find_acpi_cpu_topology_cluster(unsigned int cpu);
  int find_acpi_cpu_topology_package(unsigned int cpu);
  int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
  void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
+int find_acpi_cache_level_from_id(u32 cache_id);
  #else
  static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
  {

include/linux/acpi.h static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu) · line 1575

Quoted 10 lines
include/linux/acpi.h +4 -0
@@ -1565,6 +1566,10 @@ static inline int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
  }
  static inline void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,
  						     cpumask_t *cpus) { }
+static inline int find_acpi_cache_level_from_id(u32 cache_id)
+{
+	return -EINVAL;
+}
  #endif
 
  void acpi_arch_init(void);

Thanks.

-Fenghua

author maintainer
#25 lore raw

Re: [PATCH v2 03/29] ACPI / PPTT: Find cache level by cache-id

Hi Fenghua,

Quoted 9 lines · Fenghua Yu
Quoted 8 lines · James Morse

The MPAM table identifies caches by id. The MPAM driver also wants to know the cache level to determine if the platform is of the shape that can be managed via resctrl. Cacheinfo has this information, but only for CPUs that are online.

Waiting for all CPUs to come online is a problem for platforms where CPUs are brought online late by user-space.

Quoted 8 lines
Quoted 4 lines

Add a helper that walks every possible cache, until it finds the one identified by cache-id, then return the level.

Other than minor comment issues as follows,

Thanks,

Quoted 14 lines
Quoted 14 lines
drivers/acpi/pptt.c +0 -0
index 7af7d62597df..c5f2a51d280b 100644
@@ -904,3 +904,65 @@ void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t
 *cpus)
                        entry->length);
       }

  }

drivers/acpi/pptt.c +5 -0
+
+/*
+ * find_acpi_cache_level_from_id() - Get the level of the specified cache
+ * @cache_id: The id field of the unified cache
+ *
Quoted 8 lines
Quoted 6 lines

+ * Determine the level relative to any CPU for the unified cache identified by
+ * cache_id. This allows the property to be found even if the CPUs are offline.
+ *
+ * The returned level can be used to group unified caches that are peers.
+ *
+ * The PPTT table must be rev 3 or later,

s/,/./

Yup, already fixed. (I need to clean my glasses more often!)

Quoted 6 lines
Quoted 2 lines

+ *
+ * If one CPUs L2 is shared with another as L3, this function will return

This comment is not clear. Maybe it's better to say:

+ * If one CPU's L2 is shared with another CPU as L3, this function will return

Sure,

Quoted 6 lines
Quoted 6 lines

+ * an unpredictable value.
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, the revision isn't supported or
+ * the cache cannot be found.
+ * Otherwise returns a value which represents the level of the specified cache.
+ */

Thanks,

James

Reviewed-by on v2 3/29
#26 lore raw

Re: [PATCH v2 03/29] ACPI / PPTT: Find cache level by cache-id

In reply to James Morse
Quoted 18 lines · James Morse

The MPAM table identifies caches by id. The MPAM driver also wants to know the cache level to determine if the platform is of the shape that can be managed via resctrl. Cacheinfo has this information, but only for CPUs that are online.

Waiting for all CPUs to come online is a problem for platforms where CPUs are brought online late by user-space.

Add a helper that walks every possible cache, until it finds the one identified by cache-id, then return the level.

---
Changes since v1:
* Droppeed the cleanup based table freeing, use acpi_get_pptt() instead.
* Removed a confusing comment.
* Clarified the kernel doc.

Quoted 9 lines

Changes since RFC:
* acpi_count_levels() now returns a value.
* Converted the table-get stuff to use Jonathan's cleanup helper.
* Dropped Sudeep's Review tag due to the cleanup change.

---
  drivers/acpi/pptt.c  | 62 ++++++++++++++++++++++++++++++++++++++++++++
  include/linux/acpi.h |  5 ++++
  2 files changed, 67 insertions(+)

With existing comments addressed, especially those from Lorenzo Pieralisi:

author maintainer
#27 lore raw

PATCH v2 4/29 ACPI / PPTT: Add a helper to fill a cpumask from a cache_id

In reply to James Morse

MPAM identifies CPUs by the cache_id in the PPTT cache structure.

The driver needs to know which CPUs are associated with the cache. The CPUs may not all be online, so cacheinfo does not have the information.

Add a helper to pull this information out of the PPTT.

---
Changes since v1:
* Added punctuation to the commit message.
* Removed a comment about an alternative implementaion.
* Made the loop continue with a warning if a CPU is missing from the PPTT.

Changes since RFC:
* acpi_count_levels() now returns a value.
* Converted the table-get stuff to use Jonathan's cleanup helper.
* Dropped Sudeep's Review tag due to the cleanup change.

---
 drivers/acpi/pptt.c  | 59 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  6 +++++
 2 files changed, 65 insertions(+)
drivers/acpi/pptt.c +59 -0
index c5f2a51d280b..c379a9952b00 100644
@@ -966,3 +966,62 @@ int find_acpi_cache_level_from_id(u32 cache_id)
 
 	return -ENOENT;
 }
+
+/**
+ * acpi_pptt_get_cpumask_from_cache_id() - Get the cpus associated with the
+ *					   specified cache
+ * @cache_id: The id field of the unified cache
+ * @cpus: Where to build the cpumask
+ *
+ * Determine which CPUs are below this cache in the PPTT. This allows the property
+ * to be found even if the CPUs are offline.
+ *
+ * The PPTT table must be rev 3 or later,
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, or the cache cannot be found.
+ * Otherwise returns 0 and sets the cpus in the provided cpumask.
+ */
+int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_PPTT, 0);
+
+	cpumask_clear(cpus);
+
+	if (IS_ERR(table))
+		return -ENOENT;
+
+	if (table->revision < 3)
+		return -ENOENT;
+
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (WARN_ON_ONCE(!cpu_node))
+			continue;
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				cpumask_set_cpu(cpu, cpus);
+		}
+	}
+
+	return 0;
+}
include/linux/acpi.h +6 -0
index 5bdca5546697..c5fd92cda487 100644
@@ -1543,6 +1543,7 @@ int find_acpi_cpu_topology_package(unsigned int cpu);
 int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
 void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
 int find_acpi_cache_level_from_id(u32 cache_id);
+int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus);
 #else
 static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
 {
@@ -1570,6 +1571,11 @@ static inline int find_acpi_cache_level_from_id(u32 cache_id)
 {
 	return -EINVAL;
 }
+static inline int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id,
+						      cpumask_t *cpus)
+{
+	return -ENOENT;
+}
 #endif
 
 void acpi_arch_init(void);
Signature · 1 line
2.39.5
#28 lore raw

Re: [PATCH v2 04/29] ACPI / PPTT: Add a helper to fill a cpumask from a cache_id

Quoted 17 lines · On Wed, 10 Sep 2025 20:42:44 +0000 James Morse <[email protected]> wrote:

MPAM identifies CPUs by the cache_id in the PPTT cache structure.

The driver needs to know which CPUs are associated with the cache. The CPUs may not all be online, so cacheinfo does not have the information.

Add a helper to pull this information out of the PPTT.

---
Changes since v1:
* Added punctuation to the commit message.
* Removed a comment about an alternative implementaion.
* Made the loop continue with a warning if a CPU is missing from the PPTT.

Quoted 3 lines

Changes since RFC:
* acpi_count_levels() now returns a value.
* Converted the table-get stuff to use Jonathan's cleanup helper.

Why for this case does it makes sense to not just use acpi_get_pptt()?

Also you don't introduce the acpi_get_table_reg() helper until patch 6.

Quoted 11 lines

* Dropped Sudeep's Review tag due to the cleanup change.

---
 drivers/acpi/pptt.c  | 59 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  6 +++++
 2 files changed, 65 insertions(+)
drivers/acpi/pptt.c +0 -0
index c5f2a51d280b..c379a9952b00 100644

drivers/acpi/pptt.c int find_acpi_cache_level_from_id(u32 cache_id) · line 973

Quoted 8 lines
drivers/acpi/pptt.c +5 -0
@@ -966,3 +966,62 @@ int find_acpi_cache_level_from_id(u32 cache_id)
 
 	return -ENOENT;
 }
+
+/**
+ * acpi_pptt_get_cpumask_from_cache_id() - Get the cpus associated with the
+ *					   specified cache
+ * @cache_id: The id field of the unified cache

Similar comment to previous patch. If we are going to make this unified only can we reflect that in the function name. I worry this will get reused and that restriction will surprise.

Quoted 35 lines
drivers/acpi/pptt.c +35 -0
+ * @cpus: Where to build the cpumask
+ *
+ * Determine which CPUs are below this cache in the PPTT. This allows the property
+ * to be found even if the CPUs are offline.
+ *
+ * The PPTT table must be rev 3 or later,
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, or the cache cannot be found.
+ * Otherwise returns 0 and sets the cpus in the provided cpumask.
+ */
+int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_PPTT, 0);
+
+	cpumask_clear(cpus);
+
+	if (IS_ERR(table))
+		return -ENOENT;
+
+	if (table->revision < 3)
+		return -ENOENT;
+
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (WARN_ON_ONCE(!cpu_node))
+			continue;
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */

drivers/acpi/pptt.c int find_acpi_cache_level_from_id(u32 cache_id) · line 1018

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));

sizeof(*cache) makes more sense to me.

Quoted 9 lines
drivers/acpi/pptt.c +9 -0
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				cpumask_set_cpu(cpu, cpus);
+		}
+	}
+
+	return 0;
+}
author maintainer
#29 lore raw

Re: [PATCH v2 04/29] ACPI / PPTT: Add a helper to fill a cpumask from a cache_id

Hi Jonathan,

Quoted 9 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 10 Sep 2025 20:42:44 +0000 James Morse <[email protected]> wrote:

MPAM identifies CPUs by the cache_id in the PPTT cache structure.

The driver needs to know which CPUs are associated with the cache. The CPUs may not all be online, so cacheinfo does not have the information.

Quoted 5 lines
Quoted 1 line

Add a helper to pull this information out of the PPTT.

Why for this case does it makes sense to not just use acpi_get_pptt()?

Also you don't introduce the acpi_get_table_reg() helper until patch 6.

I missed fixing this one up. That's done now.

Quoted 6 lines
Quoted 6 lines
drivers/acpi/pptt.c +0 -0
index c5f2a51d280b..c379a9952b00 100644
@@ -966,3 +966,62 @@ int find_acpi_cache_level_from_id(u32 cache_id)
 
Quoted 11 lines

drivers/acpi/pptt.c int find_acpi_cache_level_from_id(u32 cache_id) · line 973

Quoted 7 lines
drivers/acpi/pptt.c +5 -0
 	return -ENOENT;
 }
+
+/**
+ * acpi_pptt_get_cpumask_from_cache_id() - Get the cpus associated with the
+ *					   specified cache
+ * @cache_id: The id field of the unified cache

Similar comment to previous patch. If we are going to make this unified only can we reflect that in the function name. I worry this will get reused and that restriction will surprise.

I agree - the unified restriction turns out only to be of interest to archaeologists. I've ripped it out.

Quoted 24 lines
Quoted 24 lines
drivers/acpi/pptt.c +24 -0
+ * @cpus: Where to build the cpumask
+ *
+ * Determine which CPUs are below this cache in the PPTT. This allows the property
+ * to be found even if the CPUs are offline.
+ *
+ * The PPTT table must be rev 3 or later,
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, or the cache cannot be found.
+ * Otherwise returns 0 and sets the cpus in the provided cpumask.
+ */
+int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_PPTT, 0);
+
+	cpumask_clear(cpus);
+
+	if (IS_ERR(table))
+		return -ENOENT;
+
Quoted 8 lines

drivers/acpi/pptt.c int find_acpi_cache_level_from_id(u32 cache_id) · line 1005

Quoted 8 lines
drivers/acpi/pptt.c +8 -0
+	if (table->revision < 3)
+		return -ENOENT;
+
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (WARN_ON_ONCE(!cpu_node))
+			continue;

I'm not sure why this one is a WARN_ON_ONCE() and the other isn't - both mean the PPTT table is missing CPUs, but this looks like leftover debug. I'll drop it.

Quoted 7 lines
Quoted 7 lines
drivers/acpi/pptt.c +7 -0
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);
Quoted 8 lines

drivers/acpi/pptt.c int find_acpi_cache_level_from_id(u32 cache_id) · line 1018

Quoted 6 lines
drivers/acpi/pptt.c +6 -0
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));

sizeof(*cache) makes more sense to me.

Yup, I've done that in the previous one It's not otherwise done in this file - lets see if someone cares strongly the other way.

Quoted 9 lines

drivers/acpi/pptt.c int find_acpi_cache_level_from_id(u32 cache_id) · line 1027

Quoted 9 lines
drivers/acpi/pptt.c +9 -0
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				cpumask_set_cpu(cpu, cpus);
+		}
+	}
+
+	return 0;
+}

Thanks,

James

reviewer
#30 lore raw

Re: [PATCH v2 04/29] ACPI / PPTT: Add a helper to fill a cpumask from a cache_id

In reply to James Morse

Hi, James,

Quoted 78 lines · James Morse

MPAM identifies CPUs by the cache_id in the PPTT cache structure.

The driver needs to know which CPUs are associated with the cache. The CPUs may not all be online, so cacheinfo does not have the information.

Add a helper to pull this information out of the PPTT.

---
Changes since v1:
* Added punctuation to the commit message.
* Removed a comment about an alternative implementaion.
* Made the loop continue with a warning if a CPU is missing from the PPTT.

Changes since RFC:
* acpi_count_levels() now returns a value.
* Converted the table-get stuff to use Jonathan's cleanup helper.
* Dropped Sudeep's Review tag due to the cleanup change.

---
  drivers/acpi/pptt.c  | 59 ++++++++++++++++++++++++++++++++++++++++++++
  include/linux/acpi.h |  6 +++++
  2 files changed, 65 insertions(+)
drivers/acpi/pptt.c +44 -0
index c5f2a51d280b..c379a9952b00 100644
@@ -966,3 +966,62 @@ int find_acpi_cache_level_from_id(u32 cache_id)
 
  	return -ENOENT;
  }
+
+/**
+ * acpi_pptt_get_cpumask_from_cache_id() - Get the cpus associated with the
+ *					   specified cache
+ * @cache_id: The id field of the unified cache
+ * @cpus: Where to build the cpumask
+ *
+ * Determine which CPUs are below this cache in the PPTT. This allows the property
+ * to be found even if the CPUs are offline.
+ *
+ * The PPTT table must be rev 3 or later,
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, or the cache cannot be found.
+ * Otherwise returns 0 and sets the cpus in the provided cpumask.
+ */
+int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus)
+{
+	u32 acpi_cpu_id;
+	int level, cpu, num_levels;
+	struct acpi_pptt_cache *cache;
+	struct acpi_pptt_cache_v1 *cache_v1;
+	struct acpi_pptt_processor *cpu_node;
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_PPTT, 0);
+
+	cpumask_clear(cpus);
+
+	if (IS_ERR(table))
+		return -ENOENT;
+
+	if (table->revision < 3)
+		return -ENOENT;
+
+	for_each_possible_cpu(cpu) {
+		acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+		if (WARN_ON_ONCE(!cpu_node))
+			continue;
+		num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+		/* Start at 1 for L1 */
+		for (level = 1; level <= num_levels; level++) {
+			cache = acpi_find_cache_node(table, acpi_cpu_id,
+						     ACPI_PPTT_CACHE_TYPE_UNIFIED,
+						     level, &cpu_node);

drivers/acpi/pptt.c int find_acpi_cache_level_from_id(u32 cache_id) · line 1022

Quoted 10 lines
drivers/acpi/pptt.c +10 -0
+			if (!cache)
+				continue;
+
+			cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
+						cache,
+						sizeof(struct acpi_pptt_cache));
+
+			if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+			    cache_v1->cache_id == cache_id)
+				cpumask_set_cpu(cpu, cpus);

This function is almost identical to find_acpi_cache_level_from_id() defined in patch #3.

To reduce code size and complexity, it's better to define a common helper to server both of the two functions.

e.g. define a helper acpi_pptt_get_level_cpumask_from_cache_id(u32 cache_id, int *lvl, cpu_mask_t *cpus)

This helper has the same code body to traverse the cache levels for all CPUs as find_acpi_cache_level_from_id() and acpi_pptt_get_cpumask_from_cache_id(). The major difference is here:

+ if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+ cache_v1->cache_id == cache_id) {
+ if (cpus)
+ cpumask_set_cpu(cpu, cpus);
+ if ((level) {
+ *lvl = level;
+ return 0;
+ }

Then simplify the two callers as follows:
int find_acpi_cache_level_from_id(u32 cache_id)

{
	int level;
	int err = acpi_pptt_get_level_cpumask_from_cache_id(cache_id, &level, NULL);
	if (err)
		return err;

	return level;
}

int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus)

{
	return acpi_pptt_get_level_cpumask_from_cache_id(cache_id, NULL, cpus);
}
Quoted 19 lines
drivers/acpi/pptt.c +5 -0
+		}
+	}
+
+	return 0;
+}
include/linux/acpi.h +1 -0
index 5bdca5546697..c5fd92cda487 100644
@@ -1543,6 +1543,7 @@ int find_acpi_cpu_topology_package(unsigned int cpu);
  int find_acpi_cpu_topology_hetero_id(unsigned int cpu);
  void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus);
  int find_acpi_cache_level_from_id(u32 cache_id);
+int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus);
  #else
  static inline int acpi_pptt_cpu_is_thread(unsigned int cpu)
  {
@@ -1570,6 +1571,11 @@ static inline int find_acpi_cache_level_from_id(u32 cache_id)
  {

include/linux/acpi.h static inline int find_acpi_cache_level_from_id(u32 cache_id) · line 1581

Quoted 10 lines
include/linux/acpi.h +5 -0
  	return -EINVAL;
  }
+static inline int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id,
+						      cpumask_t *cpus)
+{
+	return -ENOENT;
+}
  #endif
 
  void acpi_arch_init(void);

Thanks.

-Fenghua

author maintainer
#31 lore raw

Re: [PATCH v2 04/29] ACPI / PPTT: Add a helper to fill a cpumask from a cache_id

Hi Fenghua,

Quoted 62 lines · Fenghua Yu
Quoted 61 lines · James Morse

MPAM identifies CPUs by the cache_id in the PPTT cache structure.

The driver needs to know which CPUs are associated with the cache. The CPUs may not all be online, so cacheinfo does not have the information.

Add a helper to pull this information out of the PPTT.

drivers/acpi/pptt.c +47 -0
index c5f2a51d280b..c379a9952b00 100644
@@ -966,3 +966,62 @@ int find_acpi_cache_level_from_id(u32 cache_id)
+/**
+ * acpi_pptt_get_cpumask_from_cache_id() - Get the cpus associated with the
+ *                       specified cache
+ * @cache_id: The id field of the unified cache
+ * @cpus: Where to build the cpumask
+ *
+ * Determine which CPUs are below this cache in the PPTT. This allows the property
+ * to be found even if the CPUs are offline.
+ *
+ * The PPTT table must be rev 3 or later,
+ *
+ * Return: -ENOENT if the PPTT doesn't exist, or the cache cannot be found.
+ * Otherwise returns 0 and sets the cpus in the provided cpumask.
+ */
+int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus)
+{
+    u32 acpi_cpu_id;
+    int level, cpu, num_levels;
+    struct acpi_pptt_cache *cache;
+    struct acpi_pptt_cache_v1 *cache_v1;
+    struct acpi_pptt_processor *cpu_node;
+    struct acpi_table_header *table __free(acpi_table) =
 acpi_get_table_ret(ACPI_SIG_PPTT, 0);
+
+    cpumask_clear(cpus);
+
+    if (IS_ERR(table))
+        return -ENOENT;
+
+    if (table->revision < 3)
+        return -ENOENT;
+
+    for_each_possible_cpu(cpu) {
+        acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+        cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
+        if (WARN_ON_ONCE(!cpu_node))
+            continue;
+        num_levels = acpi_count_levels(table, cpu_node, NULL);
+
+        /* Start at 1 for L1 */
+        for (level = 1; level <= num_levels; level++) {
+            cache = acpi_find_cache_node(table, acpi_cpu_id,
+                             ACPI_PPTT_CACHE_TYPE_UNIFIED,
+                             level, &cpu_node);
+            if (!cache)
+                continue;
+
+            cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
Quoted 8 lines

drivers/acpi/pptt.c int find_acpi_cache_level_from_id(u32 cache_id) · line 1022

Quoted 6 lines
drivers/acpi/pptt.c +6 -0
+                        cache,
+                        sizeof(struct acpi_pptt_cache));
+
+            if (cache->flags & ACPI_PPTT_CACHE_ID_VALID &&
+                cache_v1->cache_id == cache_id)
+                cpumask_set_cpu(cpu, cpus);

This function is almost identical to find_acpi_cache_level_from_id() defined in patch #3.

Yes - there is already a lot of repetition in this file.

I'd previously suggested to Jeremy L to ahve a walker with callbacks, but he felt that made it harder to read.
Jonathan suggested a for_each_acpi_pptt_entry() helper:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git/commit/?h=pptt/for_each_pptt_entry/v0&id=353ceeba3d39c6b6a10eeb1a59c49649cdf719d8

I'm avoiding including that here as its ~30 patches already!

Quoted 27 lines

To reduce code size and complexity, it's better to define a common helper to server both of the two functions.

e.g. define a helper acpi_pptt_get_level_cpumask_from_cache_id(u32 cache_id, int *lvl, cpu_mask_t *cpus)

This helper has the same code body to traverse the cache levels for all CPUs as find_acpi_cache_level_from_id() and acpi_pptt_get_cpumask_from_cache_id(). The major difference is here:

+            if (cache->flags & ACPI_PPTT_CACHE_ID_VALID && +                cache_v1->cache_id == cache_id) { +                if (cpus)
+                    cpumask_set_cpu(cpu, cpus);
+                if ((level) {
+                    *lvl = level;
+                    return 0;
+                }

Then simplify the two callers as follows:
int find_acpi_cache_level_from_id(u32 cache_id)
{
    int level;
    int err = acpi_pptt_get_level_cpumask_from_cache_id(cache_id, &level, NULL); if (err)
        return err;

Quoted 8 lines

    return level;
}

int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus) {
    return acpi_pptt_get_level_cpumask_from_cache_id(cache_id, NULL, cpus); }

You've combined two functions that both walk the table (there are quiet a few more in this file) - but they look for very different things. Your common helper is going to be much more complex than either of these standalone.

I think Jonathan's for-each helper is the best path forward, that reduces the boiler plate leaving the relevant differences.

Thanks,

James

author maintainer
#32 lore raw

PATCH v2 5/29 arm64: kconfig: Add Kconfig entry for MPAM

In reply to James Morse

The bulk of the MPAM driver lives outside the arch code because it largely manages MMIO devices that generate interrupts. The driver needs a Kconfig symbol to enable it. As MPAM is only found on arm64 platforms, the arm64 tree is the most natural home for the Kconfig option.

This Kconfig option will later be used by the arch code to enable or disable the MPAM context-switch code, and to register properties of CPUs with the MPAM driver.

---
Changes since v1:
* Help text rewritten by Dave.

---
 arch/arm64/Kconfig | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/arm64/Kconfig +23 -0
index e9bbfacc35a6..4be8a13505bf 100644
@@ -2060,6 +2060,29 @@ config ARM64_TLB_RANGE
 	  ARMv8.4-TLBI provides TLBI invalidation instruction that apply to a
 	  range of input addresses.
 
+config ARM64_MPAM
+	bool "Enable support for MPAM"
+	help
+	  Memory System Resource Partitioning and Monitoring (MPAM) is an
+	  optional extension to the Arm architecture that allows each
+	  transaction issued to the memory system to be labelled with a
+	  Partition identifier (PARTID) and Performance Monitoring Group
+	  identifier (PMG).
+
+	  Memory system components, such as the caches, can be configured with
+	  policies to control how much of various physical resources (such as
+	  memory bandwidth or cache memory) the transactions labelled with each
+	  PARTID can consume.  Depending on the capabilities of the hardware,
+	  the PARTID and PMG can also be used as filtering criteria to measure
+	  the memory system resource consumption of different parts of a
+	  workload.
+
+	  Use of this extension requires CPU support, support in the
+	  Memory System Components (MSC), and a description from firmware
+	  of where the MSCs are in the address space.
+
+	  MPAM is exposed to user-space via the resctrl pseudo filesystem.
+
 endmenu # "ARMv8.4 architectural features"
 
 menu "ARMv8.5 architectural features"
Signature · 1 line
2.39.5
maintainer Reviewed-by on v2 5/29
#33 lore raw

Re: [PATCH v2 05/29] arm64: kconfig: Add Kconfig entry for MPAM

Hi James,

Quoted 44 lines · James Morse

The bulk of the MPAM driver lives outside the arch code because it largely manages MMIO devices that generate interrupts. The driver needs a Kconfig symbol to enable it. As MPAM is only found on arm64 platforms, the arm64 tree is the most natural home for the Kconfig option.

This Kconfig option will later be used by the arch code to enable or disable the MPAM context-switch code, and to register properties of CPUs with the MPAM driver.

---
Changes since v1:
* Help text rewritten by Dave.

---
 arch/arm64/Kconfig | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/arm64/Kconfig +16 -0
index e9bbfacc35a6..4be8a13505bf 100644
@@ -2060,6 +2060,29 @@ config ARM64_TLB_RANGE
 	  ARMv8.4-TLBI provides TLBI invalidation instruction that apply to a
 	  range of input addresses.
 
+config ARM64_MPAM
+	bool "Enable support for MPAM"
+	help
+	  Memory System Resource Partitioning and Monitoring (MPAM) is an
+	  optional extension to the Arm architecture that allows each
+	  transaction issued to the memory system to be labelled with a
+	  Partition identifier (PARTID) and Performance Monitoring Group
+	  identifier (PMG).
+
+	  Memory system components, such as the caches, can be configured with
+	  policies to control how much of various physical resources (such as
+	  memory bandwidth or cache memory) the transactions labelled with each
+	  PARTID can consume.  Depending on the capabilities of the hardware,
+	  the PARTID and PMG can also be used as filtering criteria to measure
+	  the memory system resource consumption of different parts of a
+	  workload.

arch/arm64/Kconfig config ARM64_TLB_RANGE · line 2088

Quoted 10 lines
arch/arm64/Kconfig +7 -0
+
+	  Use of this extension requires CPU support, support in the
+	  Memory System Components (MSC), and a description from firmware
+	  of where the MSCs are in the address space.
+
+	  MPAM is exposed to user-space via the resctrl pseudo filesystem.
+
 endmenu # "ARMv8.4 architectural features"
 
 menu "ARMv8.5 architectural features"

Seems good to me. I guess we can consider separately whether we want this to be default or not.

Thanks,

Ben

reviewer Reviewed-by on v2 5/29
#34 lore raw

Re: [PATCH v2 05/29] arm64: kconfig: Add Kconfig entry for MPAM

In reply to James Morse
Quoted 6 lines · James Morse

The bulk of the MPAM driver lives outside the arch code because it largely manages MMIO devices that generate interrupts. The driver needs a Kconfig symbol to enable it. As MPAM is only found on arm64 platforms, the arm64 tree is the most natural home for the Kconfig option.

Quoted 7 lines

This Kconfig option will later be used by the arch code to enable or disable the MPAM context-switch code, and to register properties of CPUs with the MPAM driver.

Thanks.

-Fenghua

author maintainer
#35 lore raw

Re: [PATCH v2 05/29] arm64: kconfig: Add Kconfig entry for MPAM

Hi Fenghua,

Quoted 11 lines · Fenghua Yu
Quoted 10 lines · James Morse

The bulk of the MPAM driver lives outside the arch code because it largely manages MMIO devices that generate interrupts. The driver needs a Kconfig symbol to enable it. As MPAM is only found on arm64 platforms, the arm64 tree is the most natural home for the Kconfig option.

This Kconfig option will later be used by the arch code to enable or disable the MPAM context-switch code, and to register properties of CPUs with the MPAM driver.

Quoted 5 lines
Quoted 3 lines

Thanks!

James

Reviewed-by on v2 5/29
#36 lore raw

Re: [PATCH v2 05/29] arm64: kconfig: Add Kconfig entry for MPAM

In reply to James Morse
Quoted 13 lines · James Morse

The bulk of the MPAM driver lives outside the arch code because it largely manages MMIO devices that generate interrupts. The driver needs a Kconfig symbol to enable it. As MPAM is only found on arm64 platforms, the arm64 tree is the most natural home for the Kconfig option.

This Kconfig option will later be used by the arch code to enable or disable the MPAM context-switch code, and to register properties of CPUs with the MPAM driver.

Quoted 7 lines

---
Changes since v1:
* Help text rewritten by Dave.

---
  arch/arm64/Kconfig | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
author maintainer
#37 lore raw

Re: [PATCH v2 05/29] arm64: kconfig: Add Kconfig entry for MPAM

Hi Gavin,

Quoted 7 lines · Gavin Shan
Quoted 6 lines · James Morse

The bulk of the MPAM driver lives outside the arch code because it largely manages MMIO devices that generate interrupts. The driver needs a Kconfig symbol to enable it. As MPAM is only found on arm64 platforms, the arm64 tree is the most natural home for the Kconfig option.

Quoted 5 lines
Quoted 3 lines

This Kconfig option will later be used by the arch code to enable or disable the MPAM context-switch code, and to register properties of CPUs with the MPAM driver.

Thanks!

James

author maintainer
#38 lore raw

PATCH v2 6/29 ACPI / MPAM: Parse the MPAM table

In reply to James Morse

Add code to parse the arm64 specific MPAM table, looking up the cache level from the PPTT and feeding the end result into the MPAM driver.

For now the MPAM hook mpam_ris_create() is stubbed out, but will update the MPAM driver with optional discovered data.

---
Changes since v1:
* Whitespace.
* Gave GLOBAL_AFFINITY a pre-processor'd name.
* Fixed assumption that there are zero functional dependencies.
* Bounds check walking of the MSC RIS.
* More bounds checking in the main table walk.
* Check for nonsense numbers of function dependencies.
* Smattering of pr_debug() to help folk feeding line-noise to the parser.
* Changed the comment flavour on the SPDX string.
* Removed additional table check.
* More comment wrangling.

Changes since RFC:
* Used DEFINE_RES_IRQ_NAMED() and friends macros.
* Additional error handling.
* Check for zero sized MSC.
* Allow table revisions greater than 1. (no spec for revision 0!)
* Use cleanup helpers to retrive ACPI tables, which allows some functions to be folded together.

---
 arch/arm64/Kconfig          |   1 +
 drivers/acpi/arm64/Kconfig  |   3 +
 drivers/acpi/arm64/Makefile |   1 +
 drivers/acpi/arm64/mpam.c   | 361 ++++++++++++++++++++++++++++++++++++
 drivers/acpi/tables.c       |   2 +-
 include/linux/acpi.h        |  12 ++
 include/linux/arm_mpam.h    |  48 +++++
 7 files changed, 427 insertions(+), 1 deletion(-)
 create mode 100644 drivers/acpi/arm64/mpam.c
 create mode 100644 include/linux/arm_mpam.h
arch/arm64/Kconfig +1 -0
index 4be8a13505bf..6487c511bdc6 100644
@@ -2062,6 +2062,7 @@ config ARM64_TLB_RANGE
 
 config ARM64_MPAM
 	bool "Enable support for MPAM"
+	select ACPI_MPAM if ACPI
 	help
 	  Memory System Resource Partitioning and Monitoring (MPAM) is an
 	  optional extension to the Arm architecture that allows each
drivers/acpi/arm64/Kconfig +3 -0
index b3ed6212244c..f2fd79f22e7d 100644
@@ -21,3 +21,6 @@ config ACPI_AGDI
 
 config ACPI_APMT
 	bool
+
+config ACPI_MPAM
+	bool
drivers/acpi/arm64/Makefile +1 -0
index 05ecde9eaabe..9390b57cb564 100644
@@ -4,6 +4,7 @@ obj-$(CONFIG_ACPI_APMT) 	+= apmt.o
 obj-$(CONFIG_ACPI_FFH)		+= ffh.o
 obj-$(CONFIG_ACPI_GTDT) 	+= gtdt.o
 obj-$(CONFIG_ACPI_IORT) 	+= iort.o
+obj-$(CONFIG_ACPI_MPAM) 	+= mpam.o
 obj-$(CONFIG_ACPI_PROCESSOR_IDLE) += cpuidle.o
 obj-$(CONFIG_ARM_AMBA)		+= amba.o
 obj-y				+= dma.o init.o
drivers/acpi/arm64/mpam.c new file +361 -0

361 lines not on this page · raw message

drivers/acpi/tables.c +1 -1
index fa9bb8c8ce95..835e3795ede3 100644
@@ -408,7 +408,7 @@ static const char table_sigs[][ACPI_NAMESEG_SIZE] __nonstring_array __initconst
 	ACPI_SIG_PSDT, ACPI_SIG_RSDT, ACPI_SIG_XSDT, ACPI_SIG_SSDT,
 	ACPI_SIG_IORT, ACPI_SIG_NFIT, ACPI_SIG_HMAT, ACPI_SIG_PPTT,
 	ACPI_SIG_NHLT, ACPI_SIG_AEST, ACPI_SIG_CEDT, ACPI_SIG_AGDI,
-	ACPI_SIG_NBFT };
+	ACPI_SIG_NBFT, ACPI_SIG_MPAM };
 
 #define ACPI_HEADER_SIZE sizeof(struct acpi_table_header)
 
include/linux/acpi.h +12 -0
index c5fd92cda487..af449964426b 100644
@@ -8,6 +8,7 @@
 #ifndef _LINUX_ACPI_H
 #define _LINUX_ACPI_H
 
+#include <linux/cleanup.h>
 #include <linux/errno.h>
 #include <linux/ioport.h>	/* for struct resource */
 #include <linux/resource_ext.h>
@@ -221,6 +222,17 @@ void acpi_reserve_initial_tables (void);
 void acpi_table_init_complete (void);
 int acpi_table_init (void);
 
+static inline struct acpi_table_header *acpi_get_table_ret(char *signature, u32 instance)
+{
+	struct acpi_table_header *table;
+	int status = acpi_get_table(signature, instance, &table);
+
+	if (ACPI_FAILURE(status))
+		return ERR_PTR(-ENOENT);
+	return table;
+}
+DEFINE_FREE(acpi_table, struct acpi_table_header *, if (!IS_ERR(_T)) acpi_put_table(_T))
+
 int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
 int __init_or_acpilib acpi_table_parse_entries(char *id,
 		unsigned long table_size, int entry_id,
include/linux/arm_mpam.h new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.39.5
#39 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

Quoted 10 lines · On Wed, 10 Sep 2025 20:42:46 +0000 James Morse <[email protected]> wrote:

Add code to parse the arm64 specific MPAM table, looking up the cache level from the PPTT and feeding the end result into the MPAM driver.

For now the MPAM hook mpam_ris_create() is stubbed out, but will update the MPAM driver with optional discovered data.

Hi James,

A few comments inline. Note I've more or less completely forgotten what was discussed in RFC 1 so I might well be repeating stuff that you replied to then. Always a problem for me with big complex patch sets!

Jonathan

Quoted 21 lines
drivers/acpi/arm64/mpam.c new file +14 -0
new file mode 100644
index 000000000000..fd9cfa143676
 
 
+static int acpi_mpam_parse_resource(struct mpam_msc *msc,
+				    struct acpi_mpam_resource_node *res)
+{
+	int level, nid;
+	u32 cache_id;
+
+	switch (res->locator_type) {
+	case ACPI_MPAM_LOCATION_TYPE_PROCESSOR_CACHE:
+		cache_id = res->locator.cache_locator.cache_reference;
+		level = find_acpi_cache_level_from_id(cache_id);
+		if (level <= 0) {
+			pr_err_once("Bad level (%u) for cache with id %u\n", level, cache_id);
+			return -EINVAL;
+		}

drivers/acpi/arm64/mpam.c line 106

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_CACHE,
+				       level, cache_id);
+	case ACPI_MPAM_LOCATION_TYPE_MEMORY:
+		nid = pxm_to_node(res->locator.memory_locator.proximity_domain);
+		if (nid == NUMA_NO_NODE)
+			nid = 0;
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_MEMORY,
+				       255, nid);
+	default:
+		/* These get discovered later and treated as unknown */

are treated?

Quoted 5 lines
drivers/acpi/arm64/mpam.c new file +4 -0
+		return 0;
+	}
+}
 
+static bool __init parse_msc_pm_link(struct acpi_mpam_msc_node *tbl_msc,

drivers/acpi/arm64/mpam.c line 156

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+				     struct platform_device *pdev,
+				     u32 *acpi_id)
+{
+	char hid[sizeof(tbl_msc->hardware_id_linked_device) + 1];
+	bool acpi_id_valid = false;
+	struct acpi_device *buddy;
+	char uid[11];
+	int err;
+
+	memset(&hid, 0, sizeof(hid));

= {}; above and skip the memset.

Quoted 18 lines
drivers/acpi/arm64/mpam.c new file +18 -0
+	memcpy(hid, &tbl_msc->hardware_id_linked_device,
+	       sizeof(tbl_msc->hardware_id_linked_device));
+
+	if (!strcmp(hid, ACPI_PROCESSOR_CONTAINER_HID)) {
+		*acpi_id = tbl_msc->instance_id_linked_device;
+		acpi_id_valid = true;
+	}
+
+	err = snprintf(uid, sizeof(uid), "%u",
+		       tbl_msc->instance_id_linked_device);
+	if (err >= sizeof(uid)) {
+		pr_debug("Failed to convert uid of device for power management.");
+		return acpi_id_valid;
+	}
+
+	buddy = acpi_dev_get_first_match_dev(hid, uid, -1);
+	if (buddy)
+		device_link_add(&pdev->dev, &buddy->dev, DL_FLAG_STATELESS);

drivers/acpi/arm64/mpam.c line 198

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +9 -0
+
+	return acpi_id_valid;
+}
 
+
+static int __init acpi_mpam_parse(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);
+	struct property_entry props[4]; /* needs a sentinel */

Perhaps move this and res into the loop and use = {};

drivers/acpi/arm64/mpam.c line 204

Quoted 6 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+	struct acpi_mpam_msc_node *tbl_msc;
+	int next_res, next_prop, err = 0;
+	struct acpi_device *companion;
+	struct platform_device *pdev;
+	enum mpam_msc_iface iface;
+	struct resource res[3];

Add a comment here or a check later on why this is large enough.

Quoted 8 lines
drivers/acpi/arm64/mpam.c new file +8 -0
+	char uid[16];
+	u32 acpi_id;
+
+	if (acpi_disabled || !system_supports_mpam() || IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;

drivers/acpi/arm64/mpam.c line 222

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+
+	table_end = (char *)table + table->length;
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+		table_offset += tbl_msc->length;
+
+		if (table_offset > table_end) {
+			pr_debug("MSC entry overlaps end of ACPI table\n");
+			break;

That this isn't considered an error is a bit subtle and made me wonder if there was a use of uninitialized pdev (there isn't because err == 0) Why not return here?

drivers/acpi/arm64/mpam.c line 232

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		}
+
+		/*
+		 * If any of the reserved fields are set, make no attempt to
+		 * parse the MSC structure. This MSC will still be counted,
+		 * meaning the MPAM driver can't probe against all MSC, and
+		 * will never be enabled. There is no way to enable it safely,
+		 * because we cannot determine safe system-wide partid and pmg
+		 * ranges in this situation.
+		 */

This is decidedly paranoid. I'd normally expect the architecture to be based on assumption that is fine for old software to ignore new fields. ACPI itself has fairly firm rules on this (though it goes wrong sometimes :) I'm guessing there is something out there that made this necessary though so keep it if you actually need it.

Quoted 12 lines
drivers/acpi/arm64/mpam.c new file +12 -0
+		if (tbl_msc->reserved || tbl_msc->reserved1 || tbl_msc->reserved2) {
+			pr_err_once("Unrecognised MSC, MPAM not usable\n");
+			pr_debug("MSC.%u: reserved field set\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (!tbl_msc->mmio_size) {
+			pr_debug("MSC.%u: marked as disabled\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (decode_interface_type(tbl_msc, &iface)) {

drivers/acpi/arm64/mpam.c line 254

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+			pr_debug("MSC.%u: unknown interface type\n", tbl_msc->identifier);
+			continue;
+		}
+
+		next_res = 0;
+		next_prop = 0;
+		memset(res, 0, sizeof(res));
+		memset(props, 0, sizeof(props));
+
+		pdev = platform_device_alloc("mpam_msc", tbl_msc->identifier);

https://lore.kernel.org/all/[email protected]/
was a proposal to add a DEFINE_FREE() to clean these up. Might be worth a revisit. Then Greg was against the use it was put to and asking for an example of where it helped. Maybe this is that example.

If you do want to do that, I'd factor out a bunch of the stuff here as a helper so we can have the clean ownership pass of a return_ptr().
Similar to what Shiju did here (this is the usecase for platform device that Greg didn't like).
https://lore.kernel.org/all/[email protected]/

Even without that I think factoring some of this out and hence being able to do returns on errors and put the if (err) into the loop would be a nice improvement to readability.

Quoted 18 lines
drivers/acpi/arm64/mpam.c new file +18 -0
+		if (!pdev) {
+			err = -ENOMEM;
+			break;
+		}
+
+		if (tbl_msc->length < sizeof(*tbl_msc)) {
+			err = -EINVAL;
+			break;
+		}
+
+		/* Some power management is described in the namespace: */
+		err = snprintf(uid, sizeof(uid), "%u", tbl_msc->identifier);
+		if (err > 0 && err < sizeof(uid)) {
+			companion = acpi_dev_get_first_match_dev("ARMHAA5C", uid, -1);
+			if (companion)
+				ACPI_COMPANION_SET(&pdev->dev, companion);
+			else
+				pr_debug("MSC.%u: missing namespace entry\n", tbl_msc->identifier);

drivers/acpi/arm64/mpam.c line 282

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		}
+
+		if (iface == MPAM_IFACE_MMIO) {
+			res[next_res++] = DEFINE_RES_MEM_NAMED(tbl_msc->base_address,
+							       tbl_msc->mmio_size,
+							       "MPAM:MSC");
+		} else if (iface == MPAM_IFACE_PCC) {
+			props[next_prop++] = PROPERTY_ENTRY_U32("pcc-channel",
+								tbl_msc->base_address);
+			next_prop++;

Why the double increment? Needs a comment if that is right thing to do.

Quoted 45 lines
drivers/acpi/arm64/mpam.c new file +45 -0
+		}
+
+		acpi_mpam_parse_irqs(pdev, tbl_msc, res, &next_res);
+		err = platform_device_add_resources(pdev, res, next_res);
+		if (err)
+			break;
+
+		props[next_prop++] = PROPERTY_ENTRY_U32("arm,not-ready-us",
+							tbl_msc->max_nrdy_usec);
+
+		/*
+		 * The MSC's CPU affinity is described via its linked power
+		 * management device, but only if it points at a Processor or
+		 * Processor Container.
+		 */
+		if (parse_msc_pm_link(tbl_msc, pdev, &acpi_id)) {
+			props[next_prop++] = PROPERTY_ENTRY_U32("cpu_affinity",
+								acpi_id);
+		}
+
+		err = device_create_managed_software_node(&pdev->dev, props,
+							  NULL);
+		if (err)
+			break;
+
+		/* Come back later if you want the RIS too */
+		err = platform_device_add_data(pdev, tbl_msc, tbl_msc->length);
+		if (err)
+			break;
+
+		err = platform_device_add(pdev);
+		if (err)
+			break;
+	}
+
+	if (err)
+		platform_device_put(pdev);
+
+	return err;
+}
+
+int acpi_mpam_count_msc(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);

drivers/acpi/arm64/mpam.c line 337

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+	struct acpi_mpam_msc_node *tbl_msc;
+	int count = 0;
+
+	if (IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;

Trivial so feel free to ignore.
Perhaps should aim for consistency. Whilst I prefer pointers for this stuff PPTT did use unsigned longs.

Quoted 8 lines
drivers/acpi/arm64/mpam.c new file +8 -0
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+		if (!tbl_msc->mmio_size)
+			continue;
+
+		if (tbl_msc->length < sizeof(*tbl_msc))
+			return -EINVAL;

drivers/acpi/arm64/mpam.c line 355

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		if (tbl_msc->length > table_end - table_offset)
+			return -EINVAL;
+		table_offset += tbl_msc->length;
+
+		count++;
+	}
+
+	return count;
+}
+

Could reorder to put acpi_mpam_parse and this use of it together?

Quoted 23 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+/*
+ * Call after ACPI devices have been created, which happens behind acpi_scan_init()
+ * called from subsys_initcall(). PCC requires the mailbox driver, which is
+ * initialised from postcore_initcall().
+ */
+subsys_initcall_sync(acpi_mpam_parse);
include/linux/acpi.h +1 -0
index c5fd92cda487..af449964426b 100644
@@ -8,6 +8,7 @@
 #ifndef _LINUX_ACPI_H
 #define _LINUX_ACPI_H
 
+#include <linux/cleanup.h>
 #include <linux/errno.h>
 #include <linux/ioport.h>	/* for struct resource */
 #include <linux/resource_ext.h>
@@ -221,6 +222,17 @@ void acpi_reserve_initial_tables (void);
 void acpi_table_init_complete (void);
 int acpi_table_init (void);
 

include/linux/acpi.h void acpi_reserve_initial_tables (void); · line 234

Quoted 10 lines
include/linux/acpi.h +10 -0
+static inline struct acpi_table_header *acpi_get_table_ret(char *signature, u32 instance)
+{
+	struct acpi_table_header *table;
+	int status = acpi_get_table(signature, instance, &table);
+
+	if (ACPI_FAILURE(status))
+		return ERR_PTR(-ENOENT);
+	return table;
+}
+DEFINE_FREE(acpi_table, struct acpi_table_header *, if (!IS_ERR(_T)) acpi_put_table(_T))

I'd use if (!IS_ERR_OR_NULL(_T)) not because it is functionally necessary but because it will let the compiler optimize this out if it can tell that in a given path _T is NULL (I think it was Peter Z who pointed this out in a similar interface a while back).

I'd like an opinion from Rafael on this in general.

Quoted 4 lines
include/linux/acpi.h +1 -0
+
 int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
 int __init_or_acpilib acpi_table_parse_entries(char *id,
 		unsigned long table_size, int entry_id,
author maintainer
#40 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

Hi Jonathan,

Quoted 6 lines · Jonathan Cameron
Quoted 3 lines · On Wed, 10 Sep 2025 20:42:46 +0000 James Morse <[email protected]> wrote:

Add code to parse the arm64 specific MPAM table, looking up the cache level from the PPTT and feeding the end result into the MPAM driver.

Quoted 6 lines
Quoted 2 lines

For now the MPAM hook mpam_ris_create() is stubbed out, but will update the MPAM driver with optional discovered data.

A few comments inline. Note I've more or less completely forgotten what was discussed in RFC 1 so I might well be repeating stuff that you replied to then. Always a problem for me with big complex patch sets!

No worries - I'll have forgotten too. If we can't work it out, it should have had a comment.

Quoted 25 lines
Quoted 25 lines
drivers/acpi/arm64/mpam.c new file +18 -0
new file mode 100644
index 000000000000..fd9cfa143676
 
 
+static int acpi_mpam_parse_resource(struct mpam_msc *msc,
+				    struct acpi_mpam_resource_node *res)
+{
+	int level, nid;
+	u32 cache_id;
+
+	switch (res->locator_type) {
+	case ACPI_MPAM_LOCATION_TYPE_PROCESSOR_CACHE:
+		cache_id = res->locator.cache_locator.cache_reference;
+		level = find_acpi_cache_level_from_id(cache_id);
+		if (level <= 0) {
+			pr_err_once("Bad level (%u) for cache with id %u\n", level, cache_id);
+			return -EINVAL;
+		}
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_CACHE,
+				       level, cache_id);
+	case ACPI_MPAM_LOCATION_TYPE_MEMORY:
+		nid = pxm_to_node(res->locator.memory_locator.proximity_domain);
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 106

Quoted 6 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+		if (nid == NUMA_NO_NODE)
+			nid = 0;
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_MEMORY,
+				       255, nid);
+	default:
+		/* These get discovered later and treated as unknown */

are treated?

Sure,

Quoted 4 lines
Quoted 3 lines
drivers/acpi/arm64/mpam.c new file +3 -0
+		return 0;
+	}
+}
Quoted 13 lines

drivers/acpi/arm64/mpam.c line 156

Quoted 11 lines
drivers/acpi/arm64/mpam.c new file +11 -0
+static bool __init parse_msc_pm_link(struct acpi_mpam_msc_node *tbl_msc,
+				     struct platform_device *pdev,
+				     u32 *acpi_id)
+{
+	char hid[sizeof(tbl_msc->hardware_id_linked_device) + 1];
+	bool acpi_id_valid = false;
+	struct acpi_device *buddy;
+	char uid[11];
+	int err;
+
+	memset(&hid, 0, sizeof(hid));

= {}; above and skip the memset.

Sure,

Quoted 31 lines
Quoted 18 lines
drivers/acpi/arm64/mpam.c new file +18 -0
+	memcpy(hid, &tbl_msc->hardware_id_linked_device,
+	       sizeof(tbl_msc->hardware_id_linked_device));
+
+	if (!strcmp(hid, ACPI_PROCESSOR_CONTAINER_HID)) {
+		*acpi_id = tbl_msc->instance_id_linked_device;
+		acpi_id_valid = true;
+	}
+
+	err = snprintf(uid, sizeof(uid), "%u",
+		       tbl_msc->instance_id_linked_device);
+	if (err >= sizeof(uid)) {
+		pr_debug("Failed to convert uid of device for power management.");
+		return acpi_id_valid;
+	}
+
+	buddy = acpi_dev_get_first_match_dev(hid, uid, -1);
+	if (buddy)
+		device_link_add(&pdev->dev, &buddy->dev, DL_FLAG_STATELESS);

drivers/acpi/arm64/mpam.c line 198

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +9 -0
+
+	return acpi_id_valid;
+}
 
+
+static int __init acpi_mpam_parse(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);
+	struct property_entry props[4]; /* needs a sentinel */

Perhaps move this and res into the loop and use = {};

Quoted 8 lines

drivers/acpi/arm64/mpam.c line 204

Quoted 6 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+	struct acpi_mpam_msc_node *tbl_msc;
+	int next_res, next_prop, err = 0;
+	struct acpi_device *companion;
+	struct platform_device *pdev;
+	enum mpam_msc_iface iface;
+	struct resource res[3];

Add a comment here or a check later on why this is large enough.

These two are now in the loop and look like this:

| 		/* pcc, nrdy, affinity and a sentinel */
| 		struct property_entry props[4] = { 0 };
| 		/* mmio, 2xirq, no sentinel. */
|		struct resource res[3] = { 0 };
Quoted 13 lines
Quoted 13 lines
drivers/acpi/arm64/mpam.c new file +13 -0
+	char uid[16];
+	u32 acpi_id;
+
+	if (acpi_disabled || !system_supports_mpam() || IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 222

Quoted 5 lines
drivers/acpi/arm64/mpam.c new file +5 -0
+		table_offset += tbl_msc->length;
+
+		if (table_offset > table_end) {
+			pr_debug("MSC entry overlaps end of ACPI table\n");
+			break;

That this isn't considered an error is a bit subtle and made me wonder if there was a use of uninitialized pdev (there isn't because err == 0)

Its somewhat a philosophical arguement. I don't expect the kernel to have to validate these tables, they're not provided by the user and there quickly becomes a point where you have to trust them, and they have to be correct.
At the other extreme is the asusmption the table is line-noise and we should check everything to avoid out of bounds accesses. Dave wanted the diagnostic messages on these.

As this is called from an initcall, the best you get is an inexplicable print message. (what should we say - update your firmware?)

Silently failing in this code is always safe as the driver has a count of the number of MSC, and doesn't start accessing the hardware until its found them all. (this is because to find the system wide minimum value - and its not worth starting if
its not possible to finish).

Quoted 1 line

Why not return here?

Just because there was no other return in the loop, and I hate surprise returns.

I'll change it if it avoids thinking about how that platform_device_put() gets skipped!

Quoted 15 lines

drivers/acpi/arm64/mpam.c line 232

Quoted 11 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		}
+
+		/*
+		 * If any of the reserved fields are set, make no attempt to
+		 * parse the MSC structure. This MSC will still be counted,
+		 * meaning the MPAM driver can't probe against all MSC, and
+		 * will never be enabled. There is no way to enable it safely,
+		 * because we cannot determine safe system-wide partid and pmg
+		 * ranges in this situation.
+		 */

This is decidedly paranoid. I'd normally expect the architecture to be based on assumption that is fine for old software to ignore new fields. ACPI itself has fairly firm rules on this (though it goes wrong sometimes :)

Yeah - the MPAM table isn't properly structured as subtables. I don't see how they are going to extend it if they need to.

The paranoia is that anything set in these reserved fields probably indicates something the driver needs to know about: a case in point is the way PCC was added.

I'd much prefer we skip creation of MSC devices that have properties we don't understand. acpi_mpam_count_msc() still counts them - which means the driver never finds all the MSC, and never touches the hardware.

MPAM isn't a critical feature, its better that it be disabled than make things worse. (the same attitude holds with the response to the MPAM error interrupt - reset everything
and pack up shop. This is bettern than accidentally combining important/unimportant tasks)

Quoted 2 lines

I'm guessing there is something out there that made this necessary though so keep it if you actually need it.

It's a paranoid/violent reaction to the way PCC was added - without something like this, that would have led to the OS trying to map the 0 page and poking around in it - never likely to go well.

Doing this does let them pull another PCC without stable kernels going wrong. Ultimately I think they'll need to replace the table with one that is properly structured. For now - this is working with what we have.

Quoted 34 lines
Quoted 12 lines
drivers/acpi/arm64/mpam.c new file +12 -0
+		if (tbl_msc->reserved || tbl_msc->reserved1 || tbl_msc->reserved2) {
+			pr_err_once("Unrecognised MSC, MPAM not usable\n");
+			pr_debug("MSC.%u: reserved field set\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (!tbl_msc->mmio_size) {
+			pr_debug("MSC.%u: marked as disabled\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (decode_interface_type(tbl_msc, &iface)) {

drivers/acpi/arm64/mpam.c line 254

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+			pr_debug("MSC.%u: unknown interface type\n", tbl_msc->identifier);
+			continue;
+		}
+
+		next_res = 0;
+		next_prop = 0;
+		memset(res, 0, sizeof(res));
+		memset(props, 0, sizeof(props));
+
+		pdev = platform_device_alloc("mpam_msc", tbl_msc->identifier);

https://lore.kernel.org/all/[email protected]/
was a proposal to add a DEFINE_FREE() to clean these up. Might be worth a revisit. Then Greg was against the use it was put to and asking for an example of where it helped. Maybe this is that example.

If you do want to do that, I'd factor out a bunch of the stuff here as a helper so we can have the clean ownership pass of a return_ptr().
Similar to what Shiju did here (this is the usecase for platform device that Greg didn't like).
https://lore.kernel.org/all/[email protected]/

Quoted 3 lines

Even without that I think factoring some of this out and hence being able to do returns on errors and put the if (err) into the loop would be a nice improvement to readability.

If you think its more readable I'll structure it like that.

Quoted 22 lines
Quoted 22 lines
drivers/acpi/arm64/mpam.c new file +22 -0
+		if (!pdev) {
+			err = -ENOMEM;
+			break;
+		}
+
+		if (tbl_msc->length < sizeof(*tbl_msc)) {
+			err = -EINVAL;
+			break;
+		}
+
+		/* Some power management is described in the namespace: */
+		err = snprintf(uid, sizeof(uid), "%u", tbl_msc->identifier);
+		if (err > 0 && err < sizeof(uid)) {
+			companion = acpi_dev_get_first_match_dev("ARMHAA5C", uid, -1);
+			if (companion)
+				ACPI_COMPANION_SET(&pdev->dev, companion);
+			else
+				pr_debug("MSC.%u: missing namespace entry\n", tbl_msc->identifier);
+		}
+
+		if (iface == MPAM_IFACE_MMIO) {
+			res[next_res++] = DEFINE_RES_MEM_NAMED(tbl_msc->base_address,
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 282

Quoted 6 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+							       tbl_msc->mmio_size,
+							       "MPAM:MSC");
+		} else if (iface == MPAM_IFACE_PCC) {
+			props[next_prop++] = PROPERTY_ENTRY_U32("pcc-channel",
+								tbl_msc->base_address);
+			next_prop++;

Why the double increment? Needs a comment if that is right thing to do.

That's a bug.
I'll add some WARN_ON() when these are consumed as per your earlier suggestion.

Quoted 51 lines
Quoted 51 lines
drivers/acpi/arm64/mpam.c new file +51 -0
+		}
+
+		acpi_mpam_parse_irqs(pdev, tbl_msc, res, &next_res);
+		err = platform_device_add_resources(pdev, res, next_res);
+		if (err)
+			break;
+
+		props[next_prop++] = PROPERTY_ENTRY_U32("arm,not-ready-us",
+							tbl_msc->max_nrdy_usec);
+
+		/*
+		 * The MSC's CPU affinity is described via its linked power
+		 * management device, but only if it points at a Processor or
+		 * Processor Container.
+		 */
+		if (parse_msc_pm_link(tbl_msc, pdev, &acpi_id)) {
+			props[next_prop++] = PROPERTY_ENTRY_U32("cpu_affinity",
+								acpi_id);
+		}
+
+		err = device_create_managed_software_node(&pdev->dev, props,
+							  NULL);
+		if (err)
+			break;
+
+		/* Come back later if you want the RIS too */
+		err = platform_device_add_data(pdev, tbl_msc, tbl_msc->length);
+		if (err)
+			break;
+
+		err = platform_device_add(pdev);
+		if (err)
+			break;
+	}
+
+	if (err)
+		platform_device_put(pdev);
+
+	return err;
+}
+
+int acpi_mpam_count_msc(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);
+	struct acpi_mpam_msc_node *tbl_msc;
+	int count = 0;
+
+	if (IS_ERR(table))
+		return 0;
+
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 214

Quoted 4 lines
drivers/acpi/arm64/mpam.c new file +4 -0
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;

Trivial so feel free to ignore.
Perhaps should aim for consistency. Whilst I prefer pointers for this stuff PPTT did use unsigned longs.

I prefer the pointers, and as it's a separate file, I don't think it needs to be concistent with PPTT.

Quoted 12 lines
Quoted 12 lines
drivers/acpi/arm64/mpam.c new file +12 -0
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+		if (!tbl_msc->mmio_size)
+			continue;
+
+		if (tbl_msc->length < sizeof(*tbl_msc))
+			return -EINVAL;
+		if (tbl_msc->length > table_end - table_offset)
+			return -EINVAL;
+		table_offset += tbl_msc->length;
+
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 355

Quoted 6 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+		count++;
+	}
+
+	return count;
+}
+

Could reorder to put acpi_mpam_parse and this use of it together?

mpam_msc_driver_init() calls this from subsys_initcall() to know whether its worth registering the driver at all. Even with that fixed, its still potentially racy: once the first MSC has been platform_device_add()ed, I figure the driver can probe against that, and needs to know if this first MSC was the last one.
acpi_mpam_count_msc() needs to be safe to race with acpi_mpam_parse().

This could be forced far enough away in time by only registering the driver after subsys_initcall_sync() has completed - but the list of dependencies on those is ugly enough as it is.

I'll add a comment:
/**
* acpi_mpam_count_msc() - Count the number of MSC described by firmware. *
* Returns the number of of MSC, or zero for an error.
*
* This can be called before or in parallel with acpi_mpam_parse(). */

Quoted 15 lines
Quoted 15 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+/*
+ * Call after ACPI devices have been created, which happens behind acpi_scan_init()
+ * called from subsys_initcall(). PCC requires the mailbox driver, which is
+ * initialised from postcore_initcall().
+ */
+subsys_initcall_sync(acpi_mpam_parse);
include/linux/acpi.h +0 -0
index c5fd92cda487..af449964426b 100644
@@ -221,6 +222,17 @@ void acpi_reserve_initial_tables (void);
 void acpi_table_init_complete (void);
 int acpi_table_init (void);
 
Quoted 15 lines

include/linux/acpi.h void acpi_reserve_initial_tables (void); · line 234

Quoted 10 lines
include/linux/acpi.h +10 -0
+static inline struct acpi_table_header *acpi_get_table_ret(char *signature, u32 instance)
+{
+	struct acpi_table_header *table;
+	int status = acpi_get_table(signature, instance, &table);
+
+	if (ACPI_FAILURE(status))
+		return ERR_PTR(-ENOENT);
+	return table;
+}
+DEFINE_FREE(acpi_table, struct acpi_table_header *, if (!IS_ERR(_T)) acpi_put_table(_T))

I'd use if (!IS_ERR_OR_NULL(_T)) not because it is functionally necessary but because it will let the compiler optimize this out if it can tell that in a given path _T is NULL (I think it was Peter Z who pointed this out in a similar interface a while back).

Makes sense,

Quoted 1 line

I'd like an opinion from Rafael on this in general.

Thanks,

James

#41 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

Hi James

Just a few things I've picked out to reply to.
Absolutely fine with your other replies.

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
drivers/acpi/arm64/mpam.c new file +13 -0
+	char uid[16];
+	u32 acpi_id;
+
+	if (acpi_disabled || !system_supports_mpam() || IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 222

Quoted 5 lines
drivers/acpi/arm64/mpam.c new file +5 -0
+		table_offset += tbl_msc->length;
+
+		if (table_offset > table_end) {
+			pr_debug("MSC entry overlaps end of ACPI table\n");
+			break;  

That this isn't considered an error is a bit subtle and made me wonder if there was a use of uninitialized pdev (there isn't because err == 0)

Quoted 3 lines

Its somewhat a philosophical arguement. I don't expect the kernel to have to validate these tables, they're not provided by the user and there quickly becomes a point where you have to trust them, and they have to be correct.

Potential buffer overrun is to me always worth error out screaming, but I get your
broader point. Maybe just make it a pr_err()

Quoted 5 lines

At the other extreme is the asusmption the table is line-noise and we should check everything to avoid out of bounds accesses. Dave wanted the diagnostic messages on these.

As this is called from an initcall, the best you get is an inexplicable print message. (what should we say - update your firmware?)

Depends on whether you can lean hard on the firmware team. Much easier for me if I can tell them the board doesn't boot because they got it wrong.

That would have been safer if we had this upstream in advance of hardware, but indeed a little high risk today as who knows what borked tables are out there.

Personal preference though is to error out on things like this and handle the papering over at the top level. Don't put extra effort into checking tables are invalid but if we happen to notice as part of code safety stuff like sizes then good to scream about it.

Quoted 50 lines

Silently failing in this code is always safe as the driver has a count of the number of MSC, and doesn't start accessing the hardware until its found them all. (this is because to find the system wide minimum value - and its not worth starting if
its not possible to finish).

Quoted 1 line

Why not return here?

Just because there was no other return in the loop, and I hate surprise returns.

I'll change it if it avoids thinking about how that platform_device_put() gets skipped!

Quoted 15 lines

drivers/acpi/arm64/mpam.c line 232

Quoted 11 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		}
+
+		/*
+		 * If any of the reserved fields are set, make no attempt to
+		 * parse the MSC structure. This MSC will still be counted,
+		 * meaning the MPAM driver can't probe against all MSC, and
+		 * will never be enabled. There is no way to enable it safely,
+		 * because we cannot determine safe system-wide partid and pmg
+		 * ranges in this situation.
+		 */  

This is decidedly paranoid. I'd normally expect the architecture to be based on assumption that is fine for old software to ignore new fields. ACPI itself has fairly firm rules on this (though it goes wrong sometimes :)

Yeah - the MPAM table isn't properly structured as subtables. I don't see how they are going to extend it if they need to.

The paranoia is that anything set in these reserved fields probably indicates something the driver needs to know about: a case in point is the way PCC was added.

I'd much prefer we skip creation of MSC devices that have properties we don't understand. acpi_mpam_count_msc() still counts them - which means the driver never finds all the MSC, and never touches the hardware.

MPAM isn't a critical feature, its better that it be disabled than make things worse. (the same attitude holds with the response to the MPAM error interrupt - reset everything
and pack up shop. This is bettern than accidentally combining important/unimportant tasks)

Quoted 2 lines

I'm guessing there is something out there that made this necessary though so keep it if you actually need it.

Quoted 7 lines

It's a paranoid/violent reaction to the way PCC was added - without something like this, that would have led to the OS trying to map the 0 page and poking around in it - never likely to go well.

Doing this does let them pull another PCC without stable kernels going wrong. Ultimately I think they'll need to replace the table with one that is properly structured. For now - this is working with what we have.

Fair enough. I'm too lazy / behind with reviews to go scream via our channels about problems here. Paranoia it is. Maybe we'll end up backporting some 'fixes' that ignore nicely added fields with appropriate control bits to turn them on. So be it if that happens.

Quoted 36 lines
Quoted 36 lines
Quoted 14 lines
drivers/acpi/arm64/mpam.c new file +12 -0
+		if (tbl_msc->reserved || tbl_msc->reserved1 || tbl_msc->reserved2) {
+			pr_err_once("Unrecognised MSC, MPAM not usable\n");
+			pr_debug("MSC.%u: reserved field set\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (!tbl_msc->mmio_size) {
+			pr_debug("MSC.%u: marked as disabled\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (decode_interface_type(tbl_msc, &iface)) {

drivers/acpi/arm64/mpam.c line 254

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+			pr_debug("MSC.%u: unknown interface type\n", tbl_msc->identifier);
+			continue;
+		}
+
+		next_res = 0;
+		next_prop = 0;
+		memset(res, 0, sizeof(res));
+		memset(props, 0, sizeof(props));
+
+		pdev = platform_device_alloc("mpam_msc", tbl_msc->identifier);  

https://lore.kernel.org/all/[email protected]/
was a proposal to add a DEFINE_FREE() to clean these up. Might be worth a revisit. Then Greg was against the use it was put to and asking for an example of where it helped. Maybe this is that example.

If you do want to do that, I'd factor out a bunch of the stuff here as a helper so we can have the clean ownership pass of a return_ptr().
Similar to what Shiju did here (this is the usecase for platform device that Greg didn't like).
https://lore.kernel.org/all/[email protected]/

Quoted 5 lines
Quoted 3 lines

Even without that I think factoring some of this out and hence being able to do returns on errors and put the if (err) into the loop would be a nice improvement to readability.

If you think its more readable I'll structure it like that.

The refactor yes. I'd keep clear of the the DEFINE_FREE() unless you have some spare time ;)

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
drivers/acpi/arm64/mpam.c new file +14 -0
+		if (!pdev) {
+			err = -ENOMEM;
+			break;
+		}
 
+int acpi_mpam_count_msc(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);
+	struct acpi_mpam_msc_node *tbl_msc;
+	int count = 0;
+
+	if (IS_ERR(table))
+		return 0;
+
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 214

Quoted 4 lines
drivers/acpi/arm64/mpam.c new file +4 -0
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;  

Trivial so feel free to ignore.
Perhaps should aim for consistency. Whilst I prefer pointers for this stuff PPTT did use unsigned longs.

Quoted 2 lines

I prefer the pointers, and as it's a separate file, I don't think it needs to be concistent with PPTT.

Fair enough. Maybe PPTT is ripe for some cleanup once you are done messing with it. I'm certainly going to add churn now.

J

author maintainer
#42 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

Hi Jonathan,

Quoted 22 lines · Jonathan Cameron
Quoted 22 lines
Quoted 21 lines
Quoted 8 lines
drivers/acpi/arm64/mpam.c new file +8 -0
+	char uid[16];
+	u32 acpi_id;
+
+	if (acpi_disabled || !system_supports_mpam() || IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;

drivers/acpi/arm64/mpam.c line 222

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+
+	table_end = (char *)table + table->length;
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+		table_offset += tbl_msc->length;
+
+		if (table_offset > table_end) {
+			pr_debug("MSC entry overlaps end of ACPI table\n");
+			break;  

That this isn't considered an error is a bit subtle and made me wonder if there was a use of uninitialized pdev (there isn't because err == 0)

Quoted 6 lines
Quoted 3 lines

Its somewhat a philosophical arguement. I don't expect the kernel to have to validate these tables, they're not provided by the user and there quickly becomes a point where you have to trust them, and they have to be correct.

Potential buffer overrun is to me always worth error out screaming, but I get your
broader point. Maybe just make it a pr_err()

Sure, done.

Quoted 71 lines
Quoted 5 lines

At the other extreme is the asusmption the table is line-noise and we should check everything to avoid out of bounds accesses. Dave wanted the diagnostic messages on these.

As this is called from an initcall, the best you get is an inexplicable print message. (what should we say - update your firmware?)

Depends on whether you can lean hard on the firmware team. Much easier for me if I can tell them the board doesn't boot because they got it wrong.

That would have been safer if we had this upstream in advance of hardware, but indeed a little high risk today as who knows what borked tables are out there.

Personal preference though is to error out on things like this and handle the papering over at the top level. Don't put extra effort into checking tables are invalid but if we happen to notice as part of code safety stuff like sizes then good to scream about it.

Quoted 54 lines

Silently failing in this code is always safe as the driver has a count of the number of MSC, and doesn't start accessing the hardware until its found them all. (this is because to find the system wide minimum value - and its not worth starting if
its not possible to finish).

Quoted 1 line

Why not return here?

Just because there was no other return in the loop, and I hate surprise returns.

I'll change it if it avoids thinking about how that platform_device_put() gets skipped!

Quoted 15 lines

drivers/acpi/arm64/mpam.c line 232

Quoted 11 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		}
+
+		/*
+		 * If any of the reserved fields are set, make no attempt to
+		 * parse the MSC structure. This MSC will still be counted,
+		 * meaning the MPAM driver can't probe against all MSC, and
+		 * will never be enabled. There is no way to enable it safely,
+		 * because we cannot determine safe system-wide partid and pmg
+		 * ranges in this situation.
+		 */  

This is decidedly paranoid. I'd normally expect the architecture to be based on assumption that is fine for old software to ignore new fields. ACPI itself has fairly firm rules on this (though it goes wrong sometimes :)

Yeah - the MPAM table isn't properly structured as subtables. I don't see how they are going to extend it if they need to.

The paranoia is that anything set in these reserved fields probably indicates something the driver needs to know about: a case in point is the way PCC was added.

I'd much prefer we skip creation of MSC devices that have properties we don't understand. acpi_mpam_count_msc() still counts them - which means the driver never finds all the MSC, and never touches the hardware.

MPAM isn't a critical feature, its better that it be disabled than make things worse. (the same attitude holds with the response to the MPAM error interrupt - reset everything
and pack up shop. This is bettern than accidentally combining important/unimportant tasks)

Quoted 2 lines

I'm guessing there is something out there that made this necessary though so keep it if you actually need it.

It's a paranoid/violent reaction to the way PCC was added - without something like this, that would have led to the OS trying to map the 0 page and poking around in it - never likely to go well.

Quoted 8 lines
Quoted 3 lines

Doing this does let them pull another PCC without stable kernels going wrong. Ultimately I think they'll need to replace the table with one that is properly structured. For now - this is working with what we have.

Fair enough. I'm too lazy / behind with reviews to go scream via our channels about problems here. Paranoia it is. Maybe we'll end up backporting some 'fixes' that ignore nicely added fields with appropriate control bits to turn them on. So be it if that happens.

Yup - I'm expecting to at least backport "ignore this feature" patches when the table gets changed. It's not how its supposed to work, but the missing subtable header wasn't caught until it was too late.
MPAM isn't a critical feature, so it shouldn't matter too much if old kernels on new hardware can't use it.

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 12 lines
drivers/acpi/arm64/mpam.c new file +12 -0
+		if (tbl_msc->reserved || tbl_msc->reserved1 || tbl_msc->reserved2) {
+			pr_err_once("Unrecognised MSC, MPAM not usable\n");
+			pr_debug("MSC.%u: reserved field set\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (!tbl_msc->mmio_size) {
+			pr_debug("MSC.%u: marked as disabled\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (decode_interface_type(tbl_msc, &iface)) {

drivers/acpi/arm64/mpam.c line 254

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+			pr_debug("MSC.%u: unknown interface type\n", tbl_msc->identifier);
+			continue;
+		}
+
+		next_res = 0;
+		next_prop = 0;
+		memset(res, 0, sizeof(res));
+		memset(props, 0, sizeof(props));
+
+		pdev = platform_device_alloc("mpam_msc", tbl_msc->identifier);  

https://lore.kernel.org/all/[email protected]/
was a proposal to add a DEFINE_FREE() to clean these up. Might be worth a revisit. Then Greg was against the use it was put to and asking for an example of where it helped. Maybe this is that example.

If you do want to do that, I'd factor out a bunch of the stuff here as a helper so we can have the clean ownership pass of a return_ptr().
Similar to what Shiju did here (this is the usecase for platform device that Greg didn't like).
https://lore.kernel.org/all/[email protected]/

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Even without that I think factoring some of this out and hence being able to do returns on errors and put the if (err) into the loop would be a nice improvement to readability.

If you think its more readable I'll structure it like that.

The refactor yes. I'd keep clear of the the DEFINE_FREE() unless you have some spare time ;)

spare what?

Thanks,

James

maintainer Reviewed-by on v2 6/29
#43 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

In reply to James Morse
Quoted 9 lines · James Morse

Add code to parse the arm64 specific MPAM table, looking up the cache level from the PPTT and feeding the end result into the MPAM driver.

For now the MPAM hook mpam_ris_create() is stubbed out, but will update the MPAM driver with optional discovered data.

I have not noticed anything wrong - just some remarks/questions below.

Quoted 87 lines

---
Changes since v1:
* Whitespace.
* Gave GLOBAL_AFFINITY a pre-processor'd name.
* Fixed assumption that there are zero functional dependencies.
* Bounds check walking of the MSC RIS.
* More bounds checking in the main table walk.
* Check for nonsense numbers of function dependencies.
* Smattering of pr_debug() to help folk feeding line-noise to the parser.
* Changed the comment flavour on the SPDX string.
* Removed additional table check.
* More comment wrangling.

Changes since RFC:
* Used DEFINE_RES_IRQ_NAMED() and friends macros.
* Additional error handling.
* Check for zero sized MSC.
* Allow table revisions greater than 1. (no spec for revision 0!)
* Use cleanup helpers to retrive ACPI tables, which allows some functions to be folded together.

---
 arch/arm64/Kconfig          |   1 +
 drivers/acpi/arm64/Kconfig  |   3 +
 drivers/acpi/arm64/Makefile |   1 +
 drivers/acpi/arm64/mpam.c   | 361 ++++++++++++++++++++++++++++++++++++
 drivers/acpi/tables.c       |   2 +-
 include/linux/acpi.h        |  12 ++
 include/linux/arm_mpam.h    |  48 +++++
 7 files changed, 427 insertions(+), 1 deletion(-)
 create mode 100644 drivers/acpi/arm64/mpam.c
 create mode 100644 include/linux/arm_mpam.h
arch/arm64/Kconfig +1 -0
index 4be8a13505bf..6487c511bdc6 100644
@@ -2062,6 +2062,7 @@ config ARM64_TLB_RANGE
 
 config ARM64_MPAM
 	bool "Enable support for MPAM"
+	select ACPI_MPAM if ACPI
 	help
 	  Memory System Resource Partitioning and Monitoring (MPAM) is an
 	  optional extension to the Arm architecture that allows each
drivers/acpi/arm64/Kconfig +3 -0
index b3ed6212244c..f2fd79f22e7d 100644
@@ -21,3 +21,6 @@ config ACPI_AGDI
 
 config ACPI_APMT
 	bool
+
+config ACPI_MPAM
+	bool
drivers/acpi/arm64/Makefile +1 -0
index 05ecde9eaabe..9390b57cb564 100644
@@ -4,6 +4,7 @@ obj-$(CONFIG_ACPI_APMT) 	+= apmt.o
 obj-$(CONFIG_ACPI_FFH)		+= ffh.o
 obj-$(CONFIG_ACPI_GTDT) 	+= gtdt.o
 obj-$(CONFIG_ACPI_IORT) 	+= iort.o
+obj-$(CONFIG_ACPI_MPAM) 	+= mpam.o
 obj-$(CONFIG_ACPI_PROCESSOR_IDLE) += cpuidle.o
 obj-$(CONFIG_ARM_AMBA)		+= amba.o
 obj-y				+= dma.o init.o
drivers/acpi/arm64/mpam.c new file +14 -0
new file mode 100644
index 000000000000..fd9cfa143676
@@ -0,0 +1,361 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2025 Arm Ltd.
+
+/* Parse the MPAM ACPI table feeding the discovered nodes into the driver */
+
+#define pr_fmt(fmt) "ACPI MPAM: " fmt
+
+#include <linux/acpi.h>
+#include <linux/arm_mpam.h>
+#include <linux/bits.h>
+#include <linux/cpu.h>
+#include <linux/cpumask.h>
+#include <linux/platform_device.h>
+

drivers/acpi/arm64/mpam.c line 24

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+#include <acpi/processor.h>
+
+/*
+ * Flags for acpi_table_mpam_msc.*_interrupt_flags.
+ * See 2.1.1 Interrupt Flags, Table 5, of DEN0065B_MPAM_ACPI_3.0-bet.
+ */
+#define ACPI_MPAM_MSC_IRQ_MODE_MASK                    BIT(0)
+#define ACPI_MPAM_MSC_IRQ_TYPE_MASK                    GENMASK(2, 1)
+#define ACPI_MPAM_MSC_IRQ_TYPE_WIRED                   0
+#define ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER BIT(3)

Nit: For consistency, not sure why MODE/TYPE_MASK and AFFINITY_{TYPE/VALID} aren't but that's not a big deal.

BIT(3) to be consistent with the table should be (?)

#define ACPI_MPAM_MSC_AFFINITY_TYPE_MASK BIT(3)

to match the Table 5 (and then add defines for possible values).

drivers/acpi/arm64/mpam.c line 25

Quoted 1 line
drivers/acpi/arm64/mpam.c new file +1 -0
+#define ACPI_MPAM_MSC_IRQ_AFFINITY_VALID               BIT(4)

ACPI_MPAM_MSC_AFFINITY_VALID_MASK ? (or remove _MASK from IRQ_{MODE/TYPE) ?

Just noticed - feel free to ignore this altogether.

Quoted 9 lines
drivers/acpi/arm64/mpam.c new file +9 -0
+static bool acpi_mpam_register_irq(struct platform_device *pdev, int intid,
+				   u32 flags, int *irq,
+				   u32 processor_container_uid)
+{
+	int sense;
+
+	if (!intid)
+		return false;
+

drivers/acpi/arm64/mpam.c line 45

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+	if (FIELD_GET(ACPI_MPAM_MSC_IRQ_TYPE_MASK, flags) !=
+	    ACPI_MPAM_MSC_IRQ_TYPE_WIRED)
+		return false;
+
+	sense = FIELD_GET(ACPI_MPAM_MSC_IRQ_MODE_MASK, flags);
+
+	if (16 <= intid && intid < 32 && processor_container_uid != GLOBAL_AFFINITY) {
+		pr_err_once("Partitioned interrupts not supported\n");
+		return false;
+	}

Please add a comment to explain what you mean here (ie PPIs partitioning isn't supported). I will have to change this code anyway to cater for the GICv5 interrupt model given the hardcoded intid values.

Is the condition allowed by the MPAM architecture so the MPAM table are legitimate (but not supported in Linux) ?

Quoted 101 lines
drivers/acpi/arm64/mpam.c new file +101 -0
+
+	*irq = acpi_register_gsi(&pdev->dev, intid, sense, ACPI_ACTIVE_HIGH);
+	if (*irq <= 0) {
+		pr_err_once("Failed to register interrupt 0x%x with ACPI\n",
+			    intid);
+		return false;
+	}
+
+	return true;
+}
+
+static void acpi_mpam_parse_irqs(struct platform_device *pdev,
+				 struct acpi_mpam_msc_node *tbl_msc,
+				 struct resource *res, int *res_idx)
+{
+	u32 flags, aff;
+	int irq;
+
+	flags = tbl_msc->overflow_interrupt_flags;
+	if (flags & ACPI_MPAM_MSC_IRQ_AFFINITY_VALID &&
+	    flags & ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER)
+		aff = tbl_msc->overflow_interrupt_affinity;
+	else
+		aff = GLOBAL_AFFINITY;
+	if (acpi_mpam_register_irq(pdev, tbl_msc->overflow_interrupt, flags, &irq, aff))
+		res[(*res_idx)++] = DEFINE_RES_IRQ_NAMED(irq, "overflow");
+
+	flags = tbl_msc->error_interrupt_flags;
+	if (flags & ACPI_MPAM_MSC_IRQ_AFFINITY_VALID &&
+	    flags & ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER)
+		aff = tbl_msc->error_interrupt_affinity;
+	else
+		aff = GLOBAL_AFFINITY;
+	if (acpi_mpam_register_irq(pdev, tbl_msc->error_interrupt, flags, &irq, aff))
+		res[(*res_idx)++] = DEFINE_RES_IRQ_NAMED(irq, "error");
+}
+
+static int acpi_mpam_parse_resource(struct mpam_msc *msc,
+				    struct acpi_mpam_resource_node *res)
+{
+	int level, nid;
+	u32 cache_id;
+
+	switch (res->locator_type) {
+	case ACPI_MPAM_LOCATION_TYPE_PROCESSOR_CACHE:
+		cache_id = res->locator.cache_locator.cache_reference;
+		level = find_acpi_cache_level_from_id(cache_id);
+		if (level <= 0) {
+			pr_err_once("Bad level (%u) for cache with id %u\n", level, cache_id);
+			return -EINVAL;
+		}
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_CACHE,
+				       level, cache_id);
+	case ACPI_MPAM_LOCATION_TYPE_MEMORY:
+		nid = pxm_to_node(res->locator.memory_locator.proximity_domain);
+		if (nid == NUMA_NO_NODE)
+			nid = 0;
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_MEMORY,
+				       255, nid);
+	default:
+		/* These get discovered later and treated as unknown */
+		return 0;
+	}
+}
+
+int acpi_mpam_parse_resources(struct mpam_msc *msc,
+			      struct acpi_mpam_msc_node *tbl_msc)
+{
+	int i, err;
+	char *ptr, *table_end;
+	struct acpi_mpam_resource_node *resource;
+
+	ptr = (char *)(tbl_msc + 1);
+	table_end = ptr + tbl_msc->length;
+	for (i = 0; i < tbl_msc->num_resource_nodes; i++) {
+		u64 max_deps, remaining_table;
+
+		if (ptr + sizeof(*resource) > table_end)
+			return -EINVAL;
+
+		resource = (struct acpi_mpam_resource_node *)ptr;
+
+		remaining_table = table_end - ptr;
+		max_deps = remaining_table / sizeof(struct acpi_mpam_func_deps);
+		if (resource->num_functional_deps > max_deps) {
+			pr_debug("MSC has impossible number of functional dependencies\n");
+			return -EINVAL;
+		}
+
+		err = acpi_mpam_parse_resource(msc, resource);
+		if (err)
+			return err;
+
+		ptr += sizeof(*resource);
+		ptr += resource->num_functional_deps * sizeof(struct acpi_mpam_func_deps);
+	}
+
+	return 0;
+}
+
+static bool __init parse_msc_pm_link(struct acpi_mpam_msc_node *tbl_msc,

drivers/acpi/arm64/mpam.c line 156

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+				     struct platform_device *pdev,
+				     u32 *acpi_id)
+{
+	char hid[sizeof(tbl_msc->hardware_id_linked_device) + 1];
+	bool acpi_id_valid = false;
+	struct acpi_device *buddy;
+	char uid[11];
+	int err;
+
+	memset(&hid, 0, sizeof(hid));

Jonathan already commented on this.

Quoted 8 lines
drivers/acpi/arm64/mpam.c new file +8 -0
+	memcpy(hid, &tbl_msc->hardware_id_linked_device,
+	       sizeof(tbl_msc->hardware_id_linked_device));
+
+	if (!strcmp(hid, ACPI_PROCESSOR_CONTAINER_HID)) {
+		*acpi_id = tbl_msc->instance_id_linked_device;
+		acpi_id_valid = true;
+	}
+

drivers/acpi/arm64/mpam.c line 174

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+	err = snprintf(uid, sizeof(uid), "%u",
+		       tbl_msc->instance_id_linked_device);
+	if (err >= sizeof(uid)) {
+		pr_debug("Failed to convert uid of device for power management.");
+		return acpi_id_valid;
+	}
+
+	buddy = acpi_dev_get_first_match_dev(hid, uid, -1);
+	if (buddy)
+		device_link_add(&pdev->dev, &buddy->dev, DL_FLAG_STATELESS);

Is !buddy a FW error to be logged ?

drivers/acpi/arm64/mpam.c line 186

Quoted 12 lines
drivers/acpi/arm64/mpam.c new file +12 -0
+
+	return acpi_id_valid;
+}
+
+static int decode_interface_type(struct acpi_mpam_msc_node *tbl_msc,
+				 enum mpam_msc_iface *iface)
+{
+	switch (tbl_msc->interface_type) {
+	case 0:
+		*iface = MPAM_IFACE_MMIO;
+		return 0;
+	case 0xa:

Worth giving those constants 0x0,0xa a name ?

Quoted 41 lines
drivers/acpi/arm64/mpam.c new file +41 -0
+		*iface = MPAM_IFACE_PCC;
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int __init acpi_mpam_parse(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);
+	struct property_entry props[4]; /* needs a sentinel */
+	struct acpi_mpam_msc_node *tbl_msc;
+	int next_res, next_prop, err = 0;
+	struct acpi_device *companion;
+	struct platform_device *pdev;
+	enum mpam_msc_iface iface;
+	struct resource res[3];
+	char uid[16];
+	u32 acpi_id;
+
+	if (acpi_disabled || !system_supports_mpam() || IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+		table_offset += tbl_msc->length;
+
+		if (table_offset > table_end) {
+			pr_debug("MSC entry overlaps end of ACPI table\n");
+			break;
+		}
+
+		/*
+		 * If any of the reserved fields are set, make no attempt to
+		 * parse the MSC structure. This MSC will still be counted,

drivers/acpi/arm64/mpam.c line 237

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		 * meaning the MPAM driver can't probe against all MSC, and
+		 * will never be enabled. There is no way to enable it safely,
+		 * because we cannot determine safe system-wide partid and pmg
+		 * ranges in this situation.
+		 */
+		if (tbl_msc->reserved || tbl_msc->reserved1 || tbl_msc->reserved2) {
+			pr_err_once("Unrecognised MSC, MPAM not usable\n");
+			pr_debug("MSC.%u: reserved field set\n", tbl_msc->identifier);
+			continue;
+		}

This is a bit obscure - the comment too requires some explanation ("This MSC will still be counted", not very clear what that means).

Quoted 25 lines
drivers/acpi/arm64/mpam.c new file +25 -0
+
+		if (!tbl_msc->mmio_size) {
+			pr_debug("MSC.%u: marked as disabled\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (decode_interface_type(tbl_msc, &iface)) {
+			pr_debug("MSC.%u: unknown interface type\n", tbl_msc->identifier);
+			continue;
+		}
+
+		next_res = 0;
+		next_prop = 0;
+		memset(res, 0, sizeof(res));
+		memset(props, 0, sizeof(props));
+
+		pdev = platform_device_alloc("mpam_msc", tbl_msc->identifier);
+		if (!pdev) {
+			err = -ENOMEM;
+			break;
+		}
+
+		if (tbl_msc->length < sizeof(*tbl_msc)) {
+			err = -EINVAL;
+			break;

drivers/acpi/arm64/mpam.c line 272

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		}
+
+		/* Some power management is described in the namespace: */
+		err = snprintf(uid, sizeof(uid), "%u", tbl_msc->identifier);
+		if (err > 0 && err < sizeof(uid)) {
+			companion = acpi_dev_get_first_match_dev("ARMHAA5C", uid, -1);
+			if (companion)
+				ACPI_COMPANION_SET(&pdev->dev, companion);
+			else
+				pr_debug("MSC.%u: missing namespace entry\n", tbl_msc->identifier);

Here you are linking the platform device to a namespace companion. That's what will make sure that a) the ACPI namespace scan won't add an additional platform device for ARMHAA5C and b) MSIs works -> through the related IORT named component.

Correct ?

drivers/acpi/arm64/mpam.c line 285

Quoted 13 lines
drivers/acpi/arm64/mpam.c new file +13 -0
+		}
+
+		if (iface == MPAM_IFACE_MMIO) {
+			res[next_res++] = DEFINE_RES_MEM_NAMED(tbl_msc->base_address,
+							       tbl_msc->mmio_size,
+							       "MPAM:MSC");
+		} else if (iface == MPAM_IFACE_PCC) {
+			props[next_prop++] = PROPERTY_ENTRY_U32("pcc-channel",
+								tbl_msc->base_address);
+			next_prop++;
+		}
+
+		acpi_mpam_parse_irqs(pdev, tbl_msc, res, &next_res);

Do we _really_ have to resolve IRQs here or we can postpone them at driver probe time like RIS resources (if I understand correctly how it is done - by copying table data into platform data) ?

GICv5 hat in mind - good as it is for GICv3.

Quoted 13 lines
drivers/acpi/arm64/mpam.c new file +13 -0
+		err = platform_device_add_resources(pdev, res, next_res);
+		if (err)
+			break;
+
+		props[next_prop++] = PROPERTY_ENTRY_U32("arm,not-ready-us",
+							tbl_msc->max_nrdy_usec);
+
+		/*
+		 * The MSC's CPU affinity is described via its linked power
+		 * management device, but only if it points at a Processor or
+		 * Processor Container.
+		 */
+		if (parse_msc_pm_link(tbl_msc, pdev, &acpi_id)) {

drivers/acpi/arm64/mpam.c line 308

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+			props[next_prop++] = PROPERTY_ENTRY_U32("cpu_affinity",
+								acpi_id);
+		}
+
+		err = device_create_managed_software_node(&pdev->dev, props,
+							  NULL);
+		if (err)
+			break;
+
+		/* Come back later if you want the RIS too */

I read this as: copy table data to the device so that RIS resources can be parsed later.

Right ? I think it is worth updating the comment to clarify it.

drivers/acpi/arm64/mpam.c line 319

Quoted 11 lines
drivers/acpi/arm64/mpam.c new file +11 -0
+		err = platform_device_add_data(pdev, tbl_msc, tbl_msc->length);
+		if (err)
+			break;
+
+		err = platform_device_add(pdev);
+		if (err)
+			break;
+	}
+
+	if (err)
+		platform_device_put(pdev);

I won't comment on the clean-up here as Jonathan did it already.

Quoted 30 lines
drivers/acpi/arm64/mpam.c new file +30 -0
+
+	return err;
+}
+
+int acpi_mpam_count_msc(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);
+	struct acpi_mpam_msc_node *tbl_msc;
+	int count = 0;
+
+	if (IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+		if (!tbl_msc->mmio_size)
+			continue;
+
+		if (tbl_msc->length < sizeof(*tbl_msc))
+			return -EINVAL;
+		if (tbl_msc->length > table_end - table_offset)
+			return -EINVAL;
+		table_offset += tbl_msc->length;
+

drivers/acpi/arm64/mpam.c line 359

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		count++;
+	}
+
+	return count;
+}
+
+/*
+ * Call after ACPI devices have been created, which happens behind acpi_scan_init()
+ * called from subsys_initcall(). PCC requires the mailbox driver, which is
+ * initialised from postcore_initcall().

I think we will end up setting in stone init ordering for these components created out of static tables (I mean sequencing them in a centralized way) but if that works for the current driver that's fine for the time being.

Quoted 31 lines
drivers/acpi/arm64/mpam.c new file +2 -0
+ */
+subsys_initcall_sync(acpi_mpam_parse);
drivers/acpi/tables.c +1 -1
index fa9bb8c8ce95..835e3795ede3 100644
@@ -408,7 +408,7 @@ static const char table_sigs[][ACPI_NAMESEG_SIZE] __nonstring_array __initconst
 	ACPI_SIG_PSDT, ACPI_SIG_RSDT, ACPI_SIG_XSDT, ACPI_SIG_SSDT,
 	ACPI_SIG_IORT, ACPI_SIG_NFIT, ACPI_SIG_HMAT, ACPI_SIG_PPTT,
 	ACPI_SIG_NHLT, ACPI_SIG_AEST, ACPI_SIG_CEDT, ACPI_SIG_AGDI,
-	ACPI_SIG_NBFT };
+	ACPI_SIG_NBFT, ACPI_SIG_MPAM };
 
 #define ACPI_HEADER_SIZE sizeof(struct acpi_table_header)
 
include/linux/acpi.h +1 -0
index c5fd92cda487..af449964426b 100644
@@ -8,6 +8,7 @@
 #ifndef _LINUX_ACPI_H
 #define _LINUX_ACPI_H
 
+#include <linux/cleanup.h>
 #include <linux/errno.h>
 #include <linux/ioport.h>	/* for struct resource */
 #include <linux/resource_ext.h>
@@ -221,6 +222,17 @@ void acpi_reserve_initial_tables (void);
 void acpi_table_init_complete (void);
 int acpi_table_init (void);
 

include/linux/acpi.h void acpi_reserve_initial_tables (void); · line 234

Quoted 10 lines
include/linux/acpi.h +10 -0
+static inline struct acpi_table_header *acpi_get_table_ret(char *signature, u32 instance)
+{
+	struct acpi_table_header *table;
+	int status = acpi_get_table(signature, instance, &table);
+
+	if (ACPI_FAILURE(status))
+		return ERR_PTR(-ENOENT);
+	return table;
+}
+DEFINE_FREE(acpi_table, struct acpi_table_header *, if (!IS_ERR(_T)) acpi_put_table(_T))

Jonathan commented on this already - worth getting Rafael's opinion, I am fine either way.

I have not found anything that should block this code (other than code that I know I will have to rework when GICv5 ACPI support gets in) so:

Quoted 61 lines
include/linux/acpi.h +1 -0
+
 int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
 int __init_or_acpilib acpi_table_parse_entries(char *id,
 		unsigned long table_size, int entry_id,
include/linux/arm_mpam.h new file +48 -0
new file mode 100644
index 000000000000..3d6c39c667c3
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2025 Arm Ltd. */
+
+#ifndef __LINUX_ARM_MPAM_H
+#define __LINUX_ARM_MPAM_H
+
+#include <linux/acpi.h>
+#include <linux/types.h>
+
+#define GLOBAL_AFFINITY		~0
+
+struct mpam_msc;
+
+enum mpam_msc_iface {
+	MPAM_IFACE_MMIO,	/* a real MPAM MSC */
+	MPAM_IFACE_PCC,		/* a fake MPAM MSC */
+};
+
+enum mpam_class_types {
+	MPAM_CLASS_CACHE,       /* Well known caches, e.g. L2 */
+	MPAM_CLASS_MEMORY,      /* Main memory */
+	MPAM_CLASS_UNKNOWN,     /* Everything else, e.g. SMMU */
+};
+
+#ifdef CONFIG_ACPI_MPAM
+/* Parse the ACPI description of resources entries for this MSC. */
+int acpi_mpam_parse_resources(struct mpam_msc *msc,
+			      struct acpi_mpam_msc_node *tbl_msc);
+
+int acpi_mpam_count_msc(void);
+#else
+static inline int acpi_mpam_parse_resources(struct mpam_msc *msc,
+					    struct acpi_mpam_msc_node *tbl_msc)
+{
+	return -EINVAL;
+}
+
+static inline int acpi_mpam_count_msc(void) { return -EINVAL; }
+#endif
+
+static inline int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
+				  enum mpam_class_types type, u8 class_id,
+				  int component_id)
+{
+	return -EINVAL;
+}
+
+#endif /* __LINUX_ARM_MPAM_H */
Signature · 1 line
2.39.5
author maintainer
#44 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

Hi Lorenzo,

Quoted 32 lines · Lorenzo Pieralisi
Quoted 31 lines · James Morse

Add code to parse the arm64 specific MPAM table, looking up the cache level from the PPTT and feeding the end result into the MPAM driver.

For now the MPAM hook mpam_ris_create() is stubbed out, but will update the MPAM driver with optional discovered data.

drivers/acpi/arm64/mpam.c new file +19 -0
new file mode 100644
index 000000000000..fd9cfa143676
@@ -0,0 +1,361 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2025 Arm Ltd.
+
+/* Parse the MPAM ACPI table feeding the discovered nodes into the driver */
+
+#define pr_fmt(fmt) "ACPI MPAM: " fmt
+
+#include <linux/acpi.h>
+#include <linux/arm_mpam.h>
+#include <linux/bits.h>
+#include <linux/cpu.h>
+#include <linux/cpumask.h>
+#include <linux/platform_device.h>
+
+#include <acpi/processor.h>
+
+/*
+ * Flags for acpi_table_mpam_msc.*_interrupt_flags.
+ * See 2.1.1 Interrupt Flags, Table 5, of DEN0065B_MPAM_ACPI_3.0-bet.
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 24

Quoted 5 lines
drivers/acpi/arm64/mpam.c new file +5 -0
+ */
+#define ACPI_MPAM_MSC_IRQ_MODE_MASK                    BIT(0)
+#define ACPI_MPAM_MSC_IRQ_TYPE_MASK                    GENMASK(2, 1)
+#define ACPI_MPAM_MSC_IRQ_TYPE_WIRED                   0
+#define ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER BIT(3)

Nit: For consistency, not sure why MODE/TYPE_MASK and AFFINITY_{TYPE/VALID} aren't but that's not a big deal.

The missing word is giving me trouble working out what this should be...

There used to be definitions for MODE_LEVEL and MODE_EDGE, hence the MODE_MASK to extract the bit - but Jonathan pointed out the polarity values were pretty standard, and ocul be fed to acpi_register_gsi() directly without mapping 0->0 and 1->1.

Quoted 5 lines

BIT(3) to be consistent with the table should be (?)

#define ACPI_MPAM_MSC_AFFINITY_TYPE_MASK BIT(3)

to match the Table 5 (and then add defines for possible values).

Fixed as:

| #define ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_MASK                BIT(3)
| #define ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_PROCESSOR           0
| #define ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_PROCESSOR_CONTAINER 1
Quoted 5 lines

drivers/acpi/arm64/mpam.c line 25

Quoted 1 line
drivers/acpi/arm64/mpam.c new file +1 -0
+#define ACPI_MPAM_MSC_IRQ_AFFINITY_VALID               BIT(4)

ACPI_MPAM_MSC_AFFINITY_VALID_MASK ? (or remove _MASK from IRQ_{MODE/TYPE) ?

Just noticed - feel free to ignore this altogether.

I think the _MASK stuff encourages use of FIELD_GET(), which in turn encourages local variables with meaningful names.

But I don't think I need a mask and values defined for the valid bit - which is readable enough already, or the mode as the bits are passed through directly.

Quoted 15 lines
Quoted 15 lines
drivers/acpi/arm64/mpam.c new file +14 -0
+static bool acpi_mpam_register_irq(struct platform_device *pdev, int intid,
+				   u32 flags, int *irq,
+				   u32 processor_container_uid)
+{
+	int sense;
+
+	if (!intid)
+		return false;
+
+	if (FIELD_GET(ACPI_MPAM_MSC_IRQ_TYPE_MASK, flags) !=
+	    ACPI_MPAM_MSC_IRQ_TYPE_WIRED)
+		return false;
+
+	sense = FIELD_GET(ACPI_MPAM_MSC_IRQ_MODE_MASK, flags);
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 45

Quoted 5 lines
drivers/acpi/arm64/mpam.c new file +5 -0
+
+	if (16 <= intid && intid < 32 && processor_container_uid != GLOBAL_AFFINITY) {
+		pr_err_once("Partitioned interrupts not supported\n");
+		return false;
+	}

Please add a comment to explain what you mean here (ie PPIs partitioning isn't supported).

The error message isn't enough?

Quoted 2 lines

I will have to change this code anyway to cater for the GICv5 interrupt model given the hardcoded intid values.

Ah. I can probably detect PPIs from the table description instead. It's done like this just because this was where the handling logic used to be, but if that's a nuisance for the GICv5 handling, I'll do it purely from the table.

I have the code to support ppi-paritions, but its not worth it unless someone builds this.

Quoted 2 lines

Is the condition allowed by the MPAM architecture so the MPAM table are legitimate (but not supported in Linux) ?

Yeah, the architecture says the interrupt can be PPI. Global PPI are supported, but not partitions on ACPI platforms. Because its valid/easy for DT, its hard to say no-one will ever do this with ACPI. Describing the affinity in the table lets the OS decide whether to support it.

This ends up as as helper:

| static bool _is_ppi_partition(u32 flags)
| {
| 	u32 aff_type, is_ppi;
| 	bool ret;
|
| 	is_ppi = FIELD_GET(ACPI_MPAM_MSC_IRQ_AFFINITY_VALID, flags);
| 	if (!is_ppi)
| 		return false;
|
| 	aff_type = FIELD_GET(ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_MASK, flags);
| 	ret = (aff_type == ACPI_MPAM_MSC_IRQ_AFFINITY_TYPE_PROCESSOR_CONTAINER);
| 	if (ret)
| 		pr_err_once("Partitioned interrupts not supported\n");
|
| 	return ret;
| }

and a call in acpi_mpam_register_irq() to return false for ppi-partitions. This also allows some simplification in acpi_mpam_parse_irqs().

Quoted 11 lines
Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+
+	*irq = acpi_register_gsi(&pdev->dev, intid, sense, ACPI_ACTIVE_HIGH);
+	if (*irq <= 0) {
+		pr_err_once("Failed to register interrupt 0x%x with ACPI\n",
+			    intid);
+		return false;
+	}
+
+	return true;
+}
Quoted 13 lines

drivers/acpi/arm64/mpam.c line 156

Quoted 11 lines
drivers/acpi/arm64/mpam.c new file +11 -0
+static bool __init parse_msc_pm_link(struct acpi_mpam_msc_node *tbl_msc,
+				     struct platform_device *pdev,
+				     u32 *acpi_id)
+{
+	char hid[sizeof(tbl_msc->hardware_id_linked_device) + 1];
+	bool acpi_id_valid = false;
+	struct acpi_device *buddy;
+	char uid[11];
+	int err;
+
+	memset(&hid, 0, sizeof(hid));

Jonathan already commented on this.

Yup, its gone.

Quoted 12 lines
Quoted 12 lines
drivers/acpi/arm64/mpam.c new file +12 -0
+	memcpy(hid, &tbl_msc->hardware_id_linked_device,
+	       sizeof(tbl_msc->hardware_id_linked_device));
+
+	if (!strcmp(hid, ACPI_PROCESSOR_CONTAINER_HID)) {
+		*acpi_id = tbl_msc->instance_id_linked_device;
+		acpi_id_valid = true;
+	}
+
+	err = snprintf(uid, sizeof(uid), "%u",
+		       tbl_msc->instance_id_linked_device);
+	if (err >= sizeof(uid)) {
+		pr_debug("Failed to convert uid of device for power management.");
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 174

Quoted 6 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+		return acpi_id_valid;
+	}
+
+	buddy = acpi_dev_get_first_match_dev(hid, uid, -1);
+	if (buddy)
+		device_link_add(&pdev->dev, &buddy->dev, DL_FLAG_STATELESS);

Is !buddy a FW error to be logged ?

I'm pretty sure that field is optional, its for platform specific power management. The spec has "This field must be set to zero if there is no linked device for this MSC". This is where I expect a search for "\0\0\0\0" to fail, hence its silent.

I think this thing is for the power management of the parent device, whereas the MSC namespace object is for the power management of the MSC itself. I've no idea why they need to be separate...

Quoted 6 lines
Quoted 6 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+
+	return acpi_id_valid;
+}
+
+static int decode_interface_type(struct acpi_mpam_msc_node *tbl_msc,
+				 enum mpam_msc_iface *iface)
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 186

Quoted 6 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+{
+	switch (tbl_msc->interface_type) {
+	case 0:
+		*iface = MPAM_IFACE_MMIO;
+		return 0;
+	case 0xa:

Worth giving those constants 0x0,0xa a name ?

Sure, added earlier in the file:

| /*
|  * Encodings for the MSC node body interface type field.
|  * See 2.1 MPAM MSC node, Table 4 of DEN0065B_MPAM_ACPI_3.0-bet.
|  */
| #define ACPI_MPAM_MSC_IFACE_MMIO   0x00
| #define ACPI_MPAM_MSC_IFACE_PCC    0x0a
Quoted 48 lines
Quoted 48 lines
drivers/acpi/arm64/mpam.c new file +47 -0
+		*iface = MPAM_IFACE_PCC;
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int __init acpi_mpam_parse(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);
+	struct property_entry props[4]; /* needs a sentinel */
+	struct acpi_mpam_msc_node *tbl_msc;
+	int next_res, next_prop, err = 0;
+	struct acpi_device *companion;
+	struct platform_device *pdev;
+	enum mpam_msc_iface iface;
+	struct resource res[3];
+	char uid[16];
+	u32 acpi_id;
+
+	if (acpi_disabled || !system_supports_mpam() || IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+		table_offset += tbl_msc->length;
+
+		if (table_offset > table_end) {
+			pr_debug("MSC entry overlaps end of ACPI table\n");
+			break;
+		}
+
+		/*
+		 * If any of the reserved fields are set, make no attempt to
+		 * parse the MSC structure. This MSC will still be counted,
+		 * meaning the MPAM driver can't probe against all MSC, and
+		 * will never be enabled. There is no way to enable it safely,
+		 * because we cannot determine safe system-wide partid and pmg
+		 * ranges in this situation.
+		 */
+		if (tbl_msc->reserved || tbl_msc->reserved1 || tbl_msc->reserved2) {
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 237

Quoted 4 lines
drivers/acpi/arm64/mpam.c new file +4 -0
+			pr_err_once("Unrecognised MSC, MPAM not usable\n");
+			pr_debug("MSC.%u: reserved field set\n", tbl_msc->identifier);
+			continue;
+		}

This is a bit obscure - the comment too requires some explanation ("This MSC will still be counted", not very clear what that means).

I'll expand it - counted by acpi_mpam_count_msc(). It's trying to explain why its perfectly safe to just skip MSC here - that prevents the driver from ever touching the hardware.

Quoted 41 lines
Quoted 25 lines
drivers/acpi/arm64/mpam.c new file +25 -0
+
+		if (!tbl_msc->mmio_size) {
+			pr_debug("MSC.%u: marked as disabled\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (decode_interface_type(tbl_msc, &iface)) {
+			pr_debug("MSC.%u: unknown interface type\n", tbl_msc->identifier);
+			continue;
+		}
+
+		next_res = 0;
+		next_prop = 0;
+		memset(res, 0, sizeof(res));
+		memset(props, 0, sizeof(props));
+
+		pdev = platform_device_alloc("mpam_msc", tbl_msc->identifier);
+		if (!pdev) {
+			err = -ENOMEM;
+			break;
+		}
+
+		if (tbl_msc->length < sizeof(*tbl_msc)) {
+			err = -EINVAL;
+			break;

drivers/acpi/arm64/mpam.c line 272

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		}
+
+		/* Some power management is described in the namespace: */
+		err = snprintf(uid, sizeof(uid), "%u", tbl_msc->identifier);
+		if (err > 0 && err < sizeof(uid)) {
+			companion = acpi_dev_get_first_match_dev("ARMHAA5C", uid, -1);
+			if (companion)
+				ACPI_COMPANION_SET(&pdev->dev, companion);
+			else
+				pr_debug("MSC.%u: missing namespace entry\n", tbl_msc->identifier);

Here you are linking the platform device to a namespace companion. That's what will make sure that a) the ACPI namespace scan won't add an additional platform device for ARMHAA5C and b) MSIs works -> through the related IORT named component.

Quoted 1 line

Correct ?

I don't think anyone would put an MSC behind an IOMMU, so hopefully this is not needed for MSI. I've not touched the MSI support yet.

2.6 of the spec says this is for power management ... I've hooked it up here because its there. I assumed the power-management core needed to know about it. It's the same device, hence calling it the companion, (which is how the spec people referred to it verbally).

I do see a the namespace platform device get created, is that not supposed to happen? I'm testing this with the FVP, so don't have a way of actually triggering a power management flow.

Quoted 9 lines
Quoted 9 lines
drivers/acpi/arm64/mpam.c new file +9 -0
+		}
+
+		if (iface == MPAM_IFACE_MMIO) {
+			res[next_res++] = DEFINE_RES_MEM_NAMED(tbl_msc->base_address,
+							       tbl_msc->mmio_size,
+							       "MPAM:MSC");
+		} else if (iface == MPAM_IFACE_PCC) {
+			props[next_prop++] = PROPERTY_ENTRY_U32("pcc-channel",
+								tbl_msc->base_address);
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 285

Quoted 4 lines
drivers/acpi/arm64/mpam.c new file +4 -0
+			next_prop++;
+		}
+
+		acpi_mpam_parse_irqs(pdev, tbl_msc, res, &next_res);

Do we _really_ have to resolve IRQs here or we can postpone them at driver probe time like RIS resources (if I understand correctly how it is done - by copying table data into platform data) ?

Simply because that is the order that DT does it too. We'd probably get away with it, but the interrupts are a property of the MSC, not the description of the resource it controls... I'm not sure what doing this would buy us.

Quoted 1 line

GICv5 hat in mind - good as it is for GICv3.

What changes here for GICv5? I certainly want it to work with whatever irqchip is in use.

Quoted 27 lines
Quoted 13 lines
drivers/acpi/arm64/mpam.c new file +13 -0
+		err = platform_device_add_resources(pdev, res, next_res);
+		if (err)
+			break;
+
+		props[next_prop++] = PROPERTY_ENTRY_U32("arm,not-ready-us",
+							tbl_msc->max_nrdy_usec);
+
+		/*
+		 * The MSC's CPU affinity is described via its linked power
+		 * management device, but only if it points at a Processor or
+		 * Processor Container.
+		 */
+		if (parse_msc_pm_link(tbl_msc, pdev, &acpi_id)) {

drivers/acpi/arm64/mpam.c line 308

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+			props[next_prop++] = PROPERTY_ENTRY_U32("cpu_affinity",
+								acpi_id);
+		}
+
+		err = device_create_managed_software_node(&pdev->dev, props,
+							  NULL);
+		if (err)
+			break;
+
+		/* Come back later if you want the RIS too */

I read this as: copy table data to the device so that RIS resources can be parsed later.

Quoted 1 line

Right ? I think it is worth updating the comment to clarify it.

Sure,

	/*
	 * Stash the table entry for acpi_mpam_parse_resources() to discover
	 * what this MSC controls.
	 */
Quoted 18 lines

drivers/acpi/arm64/mpam.c line 319

Quoted 11 lines
drivers/acpi/arm64/mpam.c new file +11 -0
+		err = platform_device_add_data(pdev, tbl_msc, tbl_msc->length);
+		if (err)
+			break;
+
+		err = platform_device_add(pdev);
+		if (err)
+			break;
+	}
+
+	if (err)
+		platform_device_put(pdev);

I won't comment on the clean-up here as Jonathan did it already.

Quoted 3 lines
drivers/acpi/arm64/mpam.c new file +3 -0
+
+	return err;
+}
Quoted 10 lines

drivers/acpi/arm64/mpam.c line 359

Quoted 5 lines
drivers/acpi/arm64/mpam.c new file +5 -0
+
+/*
+ * Call after ACPI devices have been created, which happens behind acpi_scan_init()
+ * called from subsys_initcall(). PCC requires the mailbox driver, which is
+ * initialised from postcore_initcall().

I think we will end up setting in stone init ordering for these components created out of static tables (I mean sequencing them in a centralized way) but if that works for the current driver that's fine for the time being.

That'd be better - I've noted the dependencies on each one of these.

Quoted 22 lines
Quoted 8 lines
include/linux/acpi.h +0 -0
index c5fd92cda487..af449964426b 100644
@@ -221,6 +222,17 @@ void acpi_reserve_initial_tables (void);
 void acpi_table_init_complete (void);
 int acpi_table_init (void);
 

include/linux/acpi.h void acpi_reserve_initial_tables (void); · line 234

Quoted 10 lines
include/linux/acpi.h +10 -0
+static inline struct acpi_table_header *acpi_get_table_ret(char *signature, u32 instance)
+{
+	struct acpi_table_header *table;
+	int status = acpi_get_table(signature, instance, &table);
+
+	if (ACPI_FAILURE(status))
+		return ERR_PTR(-ENOENT);
+	return table;
+}
+DEFINE_FREE(acpi_table, struct acpi_table_header *, if (!IS_ERR(_T)) acpi_put_table(_T))

Jonathan commented on this already - worth getting Rafael's opinion, I am fine either way.

Quoted 2 lines

I have not found anything that should block this code (other than code that I know I will have to rework when GICv5 ACPI support gets in) so:

I've factored that out.

Quoted 1 line

From your comments I assume you're happy with Jonathan's suggested changes too - which I've picked up.

Thanks!

James

#45 lore raw

PATCH 1/1 arm_mpam: Try reading again if MPAM instance returns not ready

In reply to James Morse

After updating the monitor configuration, the first read of the monitoring result requires waiting for the "not ready" duration before an effective value can be obtained.

Because a component consists of multiple MPAM instances, after updating the configuration of each instance, should wait for the "not ready" period of per single instance before the valid monitoring value can be obtained, not just wait for once interval per component.

---
It's fine to merge this patch directly into patch 7 of the responding patchset.

---
 drivers/resctrl/mpam_devices.c | 36 +++++++++++++++-------------------
 1 file changed, 16 insertions(+), 20 deletions(-)
drivers/resctrl/mpam_devices.c +16 -20
index 2962cd018207..e79a46646863 100644
@@ -1183,11 +1183,14 @@ static void __ris_msmon_read(void *arg)
 	}
 
 	*m->val += now;
+	m->err = 0;
 }
 
 static int _msmon_read(struct mpam_component *comp, struct mon_read *arg)
 {
 	int err, idx;
+	bool read_again;
+	u64 wait_jiffies;
 	struct mpam_msc *msc;
 	struct mpam_vmsc *vmsc;
 	struct mpam_msc_ris *ris;
@@ -1198,10 +1201,22 @@ static int _msmon_read(struct mpam_component *comp, struct mon_read *arg)
 
 		list_for_each_entry_rcu(ris, &vmsc->ris, vmsc_list) {
 			arg->ris = ris;
+			read_again = false;
+again:
 
 			err = smp_call_function_any(&msc->accessibility,
 						    __ris_msmon_read, arg,
 						    true);
+			if (arg->err == -EBUSY && !read_again) {
+				read_again = true;
+
+				wait_jiffies = usecs_to_jiffies(comp->class->nrdy_usec);
+				while (wait_jiffies)
+					wait_jiffies = schedule_timeout_uninterruptible(wait_jiffies);
+
+				goto again;
+			}
+
 			if (!err && arg->err)
 				err = arg->err;
 			if (err)
@@ -1218,9 +1233,7 @@ static int _msmon_read(struct mpam_component *comp, struct mon_read *arg)
 int mpam_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx,
 		    enum mpam_device_features type, u64 *val)
 {
-	int err;
 	struct mon_read arg;
-	u64 wait_jiffies = 0;
 	struct mpam_props *cprops = &comp->class->props;
 
 	might_sleep();
@@ -1237,24 +1250,7 @@ int mpam_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx,
 	arg.val = val;
 	*val = 0;
 
-	err = _msmon_read(comp, &arg);
-	if (err == -EBUSY && comp->class->nrdy_usec)
-		wait_jiffies = usecs_to_jiffies(comp->class->nrdy_usec);
-
-	while (wait_jiffies)
-		wait_jiffies = schedule_timeout_uninterruptible(wait_jiffies);
-
-	if (err == -EBUSY) {
-		memset(&arg, 0, sizeof(arg));
-		arg.ctx = ctx;
-		arg.type = type;
-		arg.val = val;
-		*val = 0;
-
-		err = _msmon_read(comp, &arg);
-	}
-
-	return err;
+	return _msmon_read(comp, &arg);
 }
 
 void mpam_msmon_reset_mbwu(struct mpam_component *comp, struct mon_cfg *ctx)

--
2.25.1

author maintainer
#46 lore raw

Re: [PATCH] arm_mpam: Try reading again if MPAM instance returns not ready

Hi Zeng,

Quoted 3 lines · Zeng Heng

After updating the monitor configuration, the first read of the monitoring result requires waiting for the "not ready" duration before an effective value can be obtained.

May need to wait - some platforms need to do this, some don't. Yours is the first I've heard of that does this!

Quoted 4 lines

Because a component consists of multiple MPAM instances, after updating the configuration of each instance, should wait for the "not ready" period of per single instance before the valid monitoring value can be obtained, not just wait for once interval per component.

I'm really trying to avoid that ... if you have ~200 MSC pretending to be one thing, you'd wait 200x the maximum period. On systems with CMN, the number of MSC scales with the number of CPUs, so 200x isn't totally crazy.

I think the real problem here is the driver doesn't go on to reconfigure MSC-2 if MSC-1 returned not-ready, meaning the "I'll only wait once" logic kicks in and returns not-ready to the user. (which is presumably what you're seeing?)

Does this solve your problem?:
-----------------%<-----------------

drivers/resctrl/mpam_devices.c +1 -1
index 404bd4c1fd5e..2f39d0339349 100644
@@ -1395,7 +1395,7 @@ static void __ris_msmon_read(void *arg)
 
 static int _msmon_read(struct mpam_component *comp, struct mon_read *arg)
 {
-       int err, idx;
+       int err, any_err = 0, idx;
        struct mpam_msc *msc;
        struct mpam_vmsc *vmsc;
        struct mpam_msc_ris *ris;
@@ -1412,15 +1412,19 @@ static int _msmon_read(struct mpam_component *comp, stru

ct mon_read *arg)

                                                    true);
                        if (!err && arg->err)
                                err = arg->err;
+
+                       /*
+                        * Save one error to be returned to the caller, but
+                        * keep reading counters so that the get reprogrammed.
+                        * On platforms with NRDY this lets us wait once.
+                        */
                        if (err)
-                               break;
+                               any_err = err;
                }
-               if (err)
-                       break;
        }
        srcu_read_unlock(&mpam_srcu, idx);

- return err;
+ return any_err;
}

int mpam_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx,
-----------------%<-----------------

Thanks,

James

#47 lore raw

Re: [PATCH] arm_mpam: Try reading again if MPAM instance returns not ready

Quoted 10 lines · James Morse

Hi Zeng,

Quoted 3 lines · Zeng Heng

After updating the monitor configuration, the first read of the monitoring result requires waiting for the "not ready" duration before an effective value can be obtained.

May need to wait - some platforms need to do this, some don't. Yours is the first I've heard of that does this!

I'm afraid similar platforms do exist. As long as one component has more than one MSC, after first updating the component’s monitor every MSC instance needs to wait for MAX_NRDY_USEC us before reading the monitor result.

In fact, most platforms don’t have nearly as many performance monitors as PARTIDs, so the monitors often have to be time-shared, which making the problem even more pronounced.

Quoted 10 lines
Quoted 5 lines

Because a component consists of multiple MPAM instances, after updating the configuration of each instance, should wait for the "not ready" period of per single instance before the valid monitoring value can be obtained, not just wait for once interval per component.

I'm really trying to avoid that ... if you have ~200 MSC pretending to be one thing, you'd wait 200x the maximum period. On systems with CMN, the number of MSC scales with the number of CPUs, so 200x isn't totally crazy.

Quoted 1 line

I think the real problem here is the driver doesn't go on to

reconfigure MSC-2 if MSC-1

Quoted 2 lines

returned not-ready, meaning the "I'll only wait once" logic kicks in and returns not-ready to the user. (which is presumably what you're seeing?)

Yes, exactly.

Quoted 40 lines

Does this solve your problem?:
-----------------%<-----------------

drivers/resctrl/mpam_devices.c +9 -5
index 404bd4c1fd5e..2f39d0339349 100644
@@ -1395,7 +1395,7 @@ static void __ris_msmon_read(void *arg)
 
  static int _msmon_read(struct mpam_component *comp, struct mon_read *arg)
  {
-       int err, idx;
+       int err, any_err = 0, idx;
         struct mpam_msc *msc;
         struct mpam_vmsc *vmsc;
         struct mpam_msc_ris *ris;
@@ -1412,15 +1412,19 @@ static int _msmon_read(struct mpam_component *comp, stru
 ct mon_read *arg)
                                                     true);
                         if (!err && arg->err)
                                 err = arg->err;
+
+                       /*
+                        * Save one error to be returned to the caller, but
+                        * keep reading counters so that the get reprogrammed.
+                        * On platforms with NRDY this lets us wait once.
+                        */
                         if (err)
-                               break;
+                               any_err = err;
                 }
-               if (err)
-                       break;
         }
         srcu_read_unlock(&mpam_srcu, idx);
 
-       return err;
+       return any_err;
  }
 
Quoted 3 lines

int mpam_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx,
-----------------%<-----------------

I agree with this modification: Reconfigure all MSCs first, then if any of them returns EBUSY, wait just once for MAX_NRDY_USEC and re-read monitor result, this guarantees that the monitor result is valid.

Thanks,
Zeng Heng

reviewer
#48 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

In reply to James Morse

Hi, James,

Quoted 167 lines · James Morse

Add code to parse the arm64 specific MPAM table, looking up the cache level from the PPTT and feeding the end result into the MPAM driver.

For now the MPAM hook mpam_ris_create() is stubbed out, but will update the MPAM driver with optional discovered data.

---
Changes since v1:
* Whitespace.
* Gave GLOBAL_AFFINITY a pre-processor'd name.
* Fixed assumption that there are zero functional dependencies.
* Bounds check walking of the MSC RIS.
* More bounds checking in the main table walk.
* Check for nonsense numbers of function dependencies.
* Smattering of pr_debug() to help folk feeding line-noise to the parser.
* Changed the comment flavour on the SPDX string.
* Removed additional table check.
* More comment wrangling.

Changes since RFC:
* Used DEFINE_RES_IRQ_NAMED() and friends macros.
* Additional error handling.
* Check for zero sized MSC.
* Allow table revisions greater than 1. (no spec for revision 0!)
* Use cleanup helpers to retrive ACPI tables, which allows some functions to be folded together.

---
  arch/arm64/Kconfig          |   1 +
  drivers/acpi/arm64/Kconfig  |   3 +
  drivers/acpi/arm64/Makefile |   1 +
  drivers/acpi/arm64/mpam.c   | 361 ++++++++++++++++++++++++++++++++++++
  drivers/acpi/tables.c       |   2 +-
  include/linux/acpi.h        |  12 ++
  include/linux/arm_mpam.h    |  48 +++++
  7 files changed, 427 insertions(+), 1 deletion(-)

create mode 100644 drivers/acpi/arm64/mpam.c
create mode 100644 include/linux/arm_mpam.h

arch/arm64/Kconfig +1 -0
index 4be8a13505bf..6487c511bdc6 100644
@@ -2062,6 +2062,7 @@ config ARM64_TLB_RANGE
 
  config ARM64_MPAM
  	bool "Enable support for MPAM"
+	select ACPI_MPAM if ACPI
  	help
  	  Memory System Resource Partitioning and Monitoring (MPAM) is an
  	  optional extension to the Arm architecture that allows each
drivers/acpi/arm64/Kconfig +3 -0
index b3ed6212244c..f2fd79f22e7d 100644
@@ -21,3 +21,6 @@ config ACPI_AGDI
 
  config ACPI_APMT
  	bool
+
+config ACPI_MPAM
+	bool
drivers/acpi/arm64/Makefile +1 -0
index 05ecde9eaabe..9390b57cb564 100644
@@ -4,6 +4,7 @@ obj-$(CONFIG_ACPI_APMT) 	+= apmt.o
  obj-$(CONFIG_ACPI_FFH)		+= ffh.o
  obj-$(CONFIG_ACPI_GTDT) 	+= gtdt.o
  obj-$(CONFIG_ACPI_IORT) 	+= iort.o
+obj-$(CONFIG_ACPI_MPAM) 	+= mpam.o
  obj-$(CONFIG_ACPI_PROCESSOR_IDLE) += cpuidle.o
  obj-$(CONFIG_ARM_AMBA)		+= amba.o
  obj-y				+= dma.o init.o
drivers/acpi/arm64/mpam.c new file +84 -0
new file mode 100644
index 000000000000..fd9cfa143676
@@ -0,0 +1,361 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2025 Arm Ltd.
+
+/* Parse the MPAM ACPI table feeding the discovered nodes into the driver */
+
+#define pr_fmt(fmt) "ACPI MPAM: " fmt
+
+#include <linux/acpi.h>
+#include <linux/arm_mpam.h>
+#include <linux/bits.h>
+#include <linux/cpu.h>
+#include <linux/cpumask.h>
+#include <linux/platform_device.h>
+
+#include <acpi/processor.h>
+
+/*
+ * Flags for acpi_table_mpam_msc.*_interrupt_flags.
+ * See 2.1.1 Interrupt Flags, Table 5, of DEN0065B_MPAM_ACPI_3.0-bet.
+ */
+#define ACPI_MPAM_MSC_IRQ_MODE_MASK                    BIT(0)
+#define ACPI_MPAM_MSC_IRQ_TYPE_MASK                    GENMASK(2, 1)
+#define ACPI_MPAM_MSC_IRQ_TYPE_WIRED                   0
+#define ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER BIT(3)
+#define ACPI_MPAM_MSC_IRQ_AFFINITY_VALID               BIT(4)
+
+static bool acpi_mpam_register_irq(struct platform_device *pdev, int intid,
+				   u32 flags, int *irq,
+				   u32 processor_container_uid)
+{
+	int sense;
+
+	if (!intid)
+		return false;
+
+	if (FIELD_GET(ACPI_MPAM_MSC_IRQ_TYPE_MASK, flags) !=
+	    ACPI_MPAM_MSC_IRQ_TYPE_WIRED)
+		return false;
+
+	sense = FIELD_GET(ACPI_MPAM_MSC_IRQ_MODE_MASK, flags);
+
+	if (16 <= intid && intid < 32 && processor_container_uid != GLOBAL_AFFINITY) {
+		pr_err_once("Partitioned interrupts not supported\n");
+		return false;
+	}
+
+	*irq = acpi_register_gsi(&pdev->dev, intid, sense, ACPI_ACTIVE_HIGH);
+	if (*irq <= 0) {
+		pr_err_once("Failed to register interrupt 0x%x with ACPI\n",
+			    intid);
+		return false;
+	}
+
+	return true;
+}
+
+static void acpi_mpam_parse_irqs(struct platform_device *pdev,
+				 struct acpi_mpam_msc_node *tbl_msc,
+				 struct resource *res, int *res_idx)
+{
+	u32 flags, aff;
+	int irq;
+
+	flags = tbl_msc->overflow_interrupt_flags;
+	if (flags & ACPI_MPAM_MSC_IRQ_AFFINITY_VALID &&
+	    flags & ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER)
+		aff = tbl_msc->overflow_interrupt_affinity;
+	else
+		aff = GLOBAL_AFFINITY;
+	if (acpi_mpam_register_irq(pdev, tbl_msc->overflow_interrupt, flags, &irq, aff))
+		res[(*res_idx)++] = DEFINE_RES_IRQ_NAMED(irq, "overflow");
+
+	flags = tbl_msc->error_interrupt_flags;
+	if (flags & ACPI_MPAM_MSC_IRQ_AFFINITY_VALID &&
+	    flags & ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER)
+		aff = tbl_msc->error_interrupt_affinity;
+	else
+		aff = GLOBAL_AFFINITY;
+	if (acpi_mpam_register_irq(pdev, tbl_msc->error_interrupt, flags, &irq, aff))
+		res[(*res_idx)++] = DEFINE_RES_IRQ_NAMED(irq, "error");
+}
+
+static int acpi_mpam_parse_resource(struct mpam_msc *msc,
+				    struct acpi_mpam_resource_node *res)

drivers/acpi/arm64/mpam.c line 94

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+{
+	int level, nid;
+	u32 cache_id;
+
+	switch (res->locator_type) {
+	case ACPI_MPAM_LOCATION_TYPE_PROCESSOR_CACHE:
+		cache_id = res->locator.cache_locator.cache_reference;
+		level = find_acpi_cache_level_from_id(cache_id);
+		if (level <= 0) {
+			pr_err_once("Bad level (%u) for cache with id %u\n", level, cache_id);

Since level could be negative value here, printing it as %u converts it to positive value and will cause debug difficulty. For example, -ENOENT returned by find_acpi_cache_level_from_id() will be printed as 4294967294(instead of -2) which is hard to know the error code.

Suggest to change this to %d:

			pr_err_once("Bad level (%d) for cache with id %u\n", level, cache_id);
Quoted 63 lines
drivers/acpi/arm64/mpam.c new file +63 -0
+			return -EINVAL;
+		}
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_CACHE,
+				       level, cache_id);
+	case ACPI_MPAM_LOCATION_TYPE_MEMORY:
+		nid = pxm_to_node(res->locator.memory_locator.proximity_domain);
+		if (nid == NUMA_NO_NODE)
+			nid = 0;
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_MEMORY,
+				       255, nid);
+	default:
+		/* These get discovered later and treated as unknown */
+		return 0;
+	}
+}
+
+int acpi_mpam_parse_resources(struct mpam_msc *msc,
+			      struct acpi_mpam_msc_node *tbl_msc)
+{
+	int i, err;
+	char *ptr, *table_end;
+	struct acpi_mpam_resource_node *resource;
+
+	ptr = (char *)(tbl_msc + 1);
+	table_end = ptr + tbl_msc->length;
+	for (i = 0; i < tbl_msc->num_resource_nodes; i++) {
+		u64 max_deps, remaining_table;
+
+		if (ptr + sizeof(*resource) > table_end)
+			return -EINVAL;
+
+		resource = (struct acpi_mpam_resource_node *)ptr;
+
+		remaining_table = table_end - ptr;
+		max_deps = remaining_table / sizeof(struct acpi_mpam_func_deps);
+		if (resource->num_functional_deps > max_deps) {
+			pr_debug("MSC has impossible number of functional dependencies\n");
+			return -EINVAL;
+		}
+
+		err = acpi_mpam_parse_resource(msc, resource);
+		if (err)
+			return err;
+
+		ptr += sizeof(*resource);
+		ptr += resource->num_functional_deps * sizeof(struct acpi_mpam_func_deps);
+	}
+
+	return 0;
+}
+
+static bool __init parse_msc_pm_link(struct acpi_mpam_msc_node *tbl_msc,
+				     struct platform_device *pdev,
+				     u32 *acpi_id)
+{
+	char hid[sizeof(tbl_msc->hardware_id_linked_device) + 1];
+	bool acpi_id_valid = false;
+	struct acpi_device *buddy;
+	char uid[11];
+	int err;
+
+	memset(&hid, 0, sizeof(hid));
+	memcpy(hid, &tbl_msc->hardware_id_linked_device,

drivers/acpi/arm64/mpam.c line 167

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+	       sizeof(tbl_msc->hardware_id_linked_device));
+
+	if (!strcmp(hid, ACPI_PROCESSOR_CONTAINER_HID)) {
+		*acpi_id = tbl_msc->instance_id_linked_device;
+		acpi_id_valid = true;
+	}
+
+	err = snprintf(uid, sizeof(uid), "%u",
+		       tbl_msc->instance_id_linked_device);
+	if (err >= sizeof(uid)) {

err could be negative error code. This error validation only checks size but not error code.

Better to change it to

        if (err < 0 || err >= sizeof(uid))

[SNIP]

Thanks.

-Fenghua

author maintainer
#49 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

Hi Fenghua,

Quoted 32 lines · Fenghua Yu
Quoted 15 lines · James Morse

Add code to parse the arm64 specific MPAM table, looking up the cache level from the PPTT and feeding the end result into the MPAM driver.

For now the MPAM hook mpam_ris_create() is stubbed out, but will update the MPAM driver with optional discovered data.

drivers/acpi/arm64/mpam.c new file +2 -0
new file mode 100644
index 000000000000..fd9cfa143676
 
+static int acpi_mpam_parse_resource(struct mpam_msc *msc,
+                    struct acpi_mpam_resource_node *res)

drivers/acpi/arm64/mpam.c line 94

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+{
+    int level, nid;
+    u32 cache_id;
+
+    switch (res->locator_type) {
+    case ACPI_MPAM_LOCATION_TYPE_PROCESSOR_CACHE:
+        cache_id = res->locator.cache_locator.cache_reference;
+        level = find_acpi_cache_level_from_id(cache_id);
+        if (level <= 0) {
+            pr_err_once("Bad level (%u) for cache with id %u\n", level, cache_id);

Since level could be negative value here, printing it as %u converts it to positive value and will cause debug difficulty. For example, -ENOENT returned by find_acpi_cache_level_from_id() will be printed as 4294967294(instead of -2) which is hard to know the error code.

Quoted 1 line

Suggest to change this to %d:

Sure, its never meant to be seen!

Quoted 34 lines

            pr_err_once("Bad level (%d) for cache with id %u\n", level, cache_id);

Quoted 32 lines

+            return -EINVAL;
+        }
+        return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_CACHE, +                       level, cache_id);
+    case ACPI_MPAM_LOCATION_TYPE_MEMORY:
+        nid = pxm_to_node(res->locator.memory_locator.proximity_domain); +        if (nid == NUMA_NO_NODE)
+            nid = 0;
+        return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_MEMORY, +                       255, nid);
+    default:
+        /* These get discovered later and treated as unknown */ +        return 0;
+    }

drivers/acpi/arm64/mpam.c new file +17 -0
+}
 
+static bool __init parse_msc_pm_link(struct acpi_mpam_msc_node *tbl_msc,
+                     struct platform_device *pdev,
+                     u32 *acpi_id)
+{
+    char hid[sizeof(tbl_msc->hardware_id_linked_device) + 1];
+    bool acpi_id_valid = false;
+    struct acpi_device *buddy;
+    char uid[11];
+    int err;
+
+    memset(&hid, 0, sizeof(hid));
+    memcpy(hid, &tbl_msc->hardware_id_linked_device,
+           sizeof(tbl_msc->hardware_id_linked_device));
+
+    if (!strcmp(hid, ACPI_PROCESSOR_CONTAINER_HID)) {
+        *acpi_id = tbl_msc->instance_id_linked_device;
Quoted 8 lines
Quoted 6 lines

+        acpi_id_valid = true;
+    }
+
+    err = snprintf(uid, sizeof(uid), "%u", +               tbl_msc->instance_id_linked_device); +    if (err >= sizeof(uid)) {

err could be negative error code.

Not here it can't, from lib/vsprintf.c's documentation of snprintf()

| * The return value is the number of characters which would be
| * generated for the given input, excluding the trailing null,
| * as per ISO C99.  If the return is greater than or equal to
| * @size, the resulting string is truncated.
Quoted 6 lines

This error validation only checks size but not error code.

Better to change it to

        if (err < 0 || err >= sizeof(uid))

Thanks,

James

#50 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

In reply to James Morse
Quoted 128 lines · James Morse

Add code to parse the arm64 specific MPAM table, looking up the cache level from the PPTT and feeding the end result into the MPAM driver.

For now the MPAM hook mpam_ris_create() is stubbed out, but will update the MPAM driver with optional discovered data.

---
Changes since v1:
* Whitespace.
* Gave GLOBAL_AFFINITY a pre-processor'd name.
* Fixed assumption that there are zero functional dependencies.
* Bounds check walking of the MSC RIS.
* More bounds checking in the main table walk.
* Check for nonsense numbers of function dependencies.
* Smattering of pr_debug() to help folk feeding line-noise to the parser.
* Changed the comment flavour on the SPDX string.
* Removed additional table check.
* More comment wrangling.

Changes since RFC:
* Used DEFINE_RES_IRQ_NAMED() and friends macros.
* Additional error handling.
* Check for zero sized MSC.
* Allow table revisions greater than 1. (no spec for revision 0!)
* Use cleanup helpers to retrive ACPI tables, which allows some functions to be folded together.

---
  arch/arm64/Kconfig          |   1 +
  drivers/acpi/arm64/Kconfig  |   3 +
  drivers/acpi/arm64/Makefile |   1 +
  drivers/acpi/arm64/mpam.c   | 361 ++++++++++++++++++++++++++++++++++++
  drivers/acpi/tables.c       |   2 +-
  include/linux/acpi.h        |  12 ++
  include/linux/arm_mpam.h    |  48 +++++
  7 files changed, 427 insertions(+), 1 deletion(-)

create mode 100644 drivers/acpi/arm64/mpam.c
create mode 100644 include/linux/arm_mpam.h

arch/arm64/Kconfig +1 -0
index 4be8a13505bf..6487c511bdc6 100644
@@ -2062,6 +2062,7 @@ config ARM64_TLB_RANGE
 
  config ARM64_MPAM
  	bool "Enable support for MPAM"
+	select ACPI_MPAM if ACPI
  	help
  	  Memory System Resource Partitioning and Monitoring (MPAM) is an
  	  optional extension to the Arm architecture that allows each
drivers/acpi/arm64/Kconfig +3 -0
index b3ed6212244c..f2fd79f22e7d 100644
@@ -21,3 +21,6 @@ config ACPI_AGDI
 
  config ACPI_APMT
  	bool
+
+config ACPI_MPAM
+	bool
drivers/acpi/arm64/Makefile +1 -0
index 05ecde9eaabe..9390b57cb564 100644
@@ -4,6 +4,7 @@ obj-$(CONFIG_ACPI_APMT) 	+= apmt.o
  obj-$(CONFIG_ACPI_FFH)		+= ffh.o
  obj-$(CONFIG_ACPI_GTDT) 	+= gtdt.o
  obj-$(CONFIG_ACPI_IORT) 	+= iort.o
+obj-$(CONFIG_ACPI_MPAM) 	+= mpam.o
  obj-$(CONFIG_ACPI_PROCESSOR_IDLE) += cpuidle.o
  obj-$(CONFIG_ARM_AMBA)		+= amba.o
  obj-y				+= dma.o init.o
drivers/acpi/arm64/mpam.c new file +45 -0
new file mode 100644
index 000000000000..fd9cfa143676
@@ -0,0 +1,361 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2025 Arm Ltd.
+
+/* Parse the MPAM ACPI table feeding the discovered nodes into the driver */
+
+#define pr_fmt(fmt) "ACPI MPAM: " fmt
+
+#include <linux/acpi.h>
+#include <linux/arm_mpam.h>
+#include <linux/bits.h>
+#include <linux/cpu.h>
+#include <linux/cpumask.h>
+#include <linux/platform_device.h>
+
+#include <acpi/processor.h>
+
+/*
+ * Flags for acpi_table_mpam_msc.*_interrupt_flags.
+ * See 2.1.1 Interrupt Flags, Table 5, of DEN0065B_MPAM_ACPI_3.0-bet.
+ */
+#define ACPI_MPAM_MSC_IRQ_MODE_MASK                    BIT(0)
+#define ACPI_MPAM_MSC_IRQ_TYPE_MASK                    GENMASK(2, 1)
+#define ACPI_MPAM_MSC_IRQ_TYPE_WIRED                   0
+#define ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER BIT(3)
+#define ACPI_MPAM_MSC_IRQ_AFFINITY_VALID               BIT(4)
+
+static bool acpi_mpam_register_irq(struct platform_device *pdev, int intid,
+				   u32 flags, int *irq,
+				   u32 processor_container_uid)
+{
+	int sense;
+
+	if (!intid)
+		return false;
+
+	if (FIELD_GET(ACPI_MPAM_MSC_IRQ_TYPE_MASK, flags) !=
+	    ACPI_MPAM_MSC_IRQ_TYPE_WIRED)
+		return false;
+
+	sense = FIELD_GET(ACPI_MPAM_MSC_IRQ_MODE_MASK, flags);
+
+	if (16 <= intid && intid < 32 && processor_container_uid != GLOBAL_AFFINITY) {
+		pr_err_once("Partitioned interrupts not supported\n");
+		return false;
+	}

drivers/acpi/arm64/mpam.c line 55

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+
+	*irq = acpi_register_gsi(&pdev->dev, intid, sense, ACPI_ACTIVE_HIGH);
+	if (*irq <= 0) {
+		pr_err_once("Failed to register interrupt 0x%x with ACPI\n",
+			    intid);
+		return false;
+	}
+
+	return true;
+}

0 is allowed by acpi_register_gsi().

	if (*irq < 0) {
		pr_err_once(...);
		return false;
	}
Quoted 39 lines
drivers/acpi/arm64/mpam.c new file +39 -0
+
+static void acpi_mpam_parse_irqs(struct platform_device *pdev,
+				 struct acpi_mpam_msc_node *tbl_msc,
+				 struct resource *res, int *res_idx)
+{
+	u32 flags, aff;
+	int irq;
+
+	flags = tbl_msc->overflow_interrupt_flags;
+	if (flags & ACPI_MPAM_MSC_IRQ_AFFINITY_VALID &&
+	    flags & ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER)
+		aff = tbl_msc->overflow_interrupt_affinity;
+	else
+		aff = GLOBAL_AFFINITY;
+	if (acpi_mpam_register_irq(pdev, tbl_msc->overflow_interrupt, flags, &irq, aff))
+		res[(*res_idx)++] = DEFINE_RES_IRQ_NAMED(irq, "overflow");
+
+	flags = tbl_msc->error_interrupt_flags;
+	if (flags & ACPI_MPAM_MSC_IRQ_AFFINITY_VALID &&
+	    flags & ACPI_MPAM_MSC_IRQ_AFFINITY_PROCESSOR_CONTAINER)
+		aff = tbl_msc->error_interrupt_affinity;
+	else
+		aff = GLOBAL_AFFINITY;
+	if (acpi_mpam_register_irq(pdev, tbl_msc->error_interrupt, flags, &irq, aff))
+		res[(*res_idx)++] = DEFINE_RES_IRQ_NAMED(irq, "error");
+}
+
+static int acpi_mpam_parse_resource(struct mpam_msc *msc,
+				    struct acpi_mpam_resource_node *res)
+{
+	int level, nid;
+	u32 cache_id;
+
+	switch (res->locator_type) {
+	case ACPI_MPAM_LOCATION_TYPE_PROCESSOR_CACHE:
+		cache_id = res->locator.cache_locator.cache_reference;
+		level = find_acpi_cache_level_from_id(cache_id);
+		if (level <= 0) {
+			pr_err_once("Bad level (%u) for cache with id %u\n", level, cache_id);

drivers/acpi/arm64/mpam.c line 104

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+			return -EINVAL;
+		}
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_CACHE,
+				       level, cache_id);
+	case ACPI_MPAM_LOCATION_TYPE_MEMORY:
+		nid = pxm_to_node(res->locator.memory_locator.proximity_domain);
+		if (nid == NUMA_NO_NODE)
+			nid = 0;
+		return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_MEMORY,
+				       255, nid);

It's perhaps worthy a warning message when @nid is explicitly set to zero due to the bad proximity domain, something like below.

		if (nid == NUMA_NO_NODE) {
			nid = 0;
			if (num_possible_nodes() > 1) {
				pr_warn("Bad proximity domain %d, mapped to node 0\n",
					res->locator.memory_locator.proximity_domain);
			}
		}
Quoted 241 lines
drivers/acpi/arm64/mpam.c new file +241 -0
+	default:
+		/* These get discovered later and treated as unknown */
+		return 0;
+	}
+}
+
+int acpi_mpam_parse_resources(struct mpam_msc *msc,
+			      struct acpi_mpam_msc_node *tbl_msc)
+{
+	int i, err;
+	char *ptr, *table_end;
+	struct acpi_mpam_resource_node *resource;
+
+	ptr = (char *)(tbl_msc + 1);
+	table_end = ptr + tbl_msc->length;
+	for (i = 0; i < tbl_msc->num_resource_nodes; i++) {
+		u64 max_deps, remaining_table;
+
+		if (ptr + sizeof(*resource) > table_end)
+			return -EINVAL;
+
+		resource = (struct acpi_mpam_resource_node *)ptr;
+
+		remaining_table = table_end - ptr;
+		max_deps = remaining_table / sizeof(struct acpi_mpam_func_deps);
+		if (resource->num_functional_deps > max_deps) {
+			pr_debug("MSC has impossible number of functional dependencies\n");
+			return -EINVAL;
+		}
+
+		err = acpi_mpam_parse_resource(msc, resource);
+		if (err)
+			return err;
+
+		ptr += sizeof(*resource);
+		ptr += resource->num_functional_deps * sizeof(struct acpi_mpam_func_deps);
+	}
+
+	return 0;
+}
+
+static bool __init parse_msc_pm_link(struct acpi_mpam_msc_node *tbl_msc,
+				     struct platform_device *pdev,
+				     u32 *acpi_id)
+{
+	char hid[sizeof(tbl_msc->hardware_id_linked_device) + 1];
+	bool acpi_id_valid = false;
+	struct acpi_device *buddy;
+	char uid[11];
+	int err;
+
+	memset(&hid, 0, sizeof(hid));
+	memcpy(hid, &tbl_msc->hardware_id_linked_device,
+	       sizeof(tbl_msc->hardware_id_linked_device));
+
+	if (!strcmp(hid, ACPI_PROCESSOR_CONTAINER_HID)) {
+		*acpi_id = tbl_msc->instance_id_linked_device;
+		acpi_id_valid = true;
+	}
+
+	err = snprintf(uid, sizeof(uid), "%u",
+		       tbl_msc->instance_id_linked_device);
+	if (err >= sizeof(uid)) {
+		pr_debug("Failed to convert uid of device for power management.");
+		return acpi_id_valid;
+	}
+
+	buddy = acpi_dev_get_first_match_dev(hid, uid, -1);
+	if (buddy)
+		device_link_add(&pdev->dev, &buddy->dev, DL_FLAG_STATELESS);
+
+	return acpi_id_valid;
+}
+
+static int decode_interface_type(struct acpi_mpam_msc_node *tbl_msc,
+				 enum mpam_msc_iface *iface)
+{
+	switch (tbl_msc->interface_type) {
+	case 0:
+		*iface = MPAM_IFACE_MMIO;
+		return 0;
+	case 0xa:
+		*iface = MPAM_IFACE_PCC;
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int __init acpi_mpam_parse(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);
+	struct property_entry props[4]; /* needs a sentinel */
+	struct acpi_mpam_msc_node *tbl_msc;
+	int next_res, next_prop, err = 0;
+	struct acpi_device *companion;
+	struct platform_device *pdev;
+	enum mpam_msc_iface iface;
+	struct resource res[3];
+	char uid[16];
+	u32 acpi_id;
+
+	if (acpi_disabled || !system_supports_mpam() || IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+		table_offset += tbl_msc->length;
+
+		if (table_offset > table_end) {
+			pr_debug("MSC entry overlaps end of ACPI table\n");
+			break;
+		}
+
+		/*
+		 * If any of the reserved fields are set, make no attempt to
+		 * parse the MSC structure. This MSC will still be counted,
+		 * meaning the MPAM driver can't probe against all MSC, and
+		 * will never be enabled. There is no way to enable it safely,
+		 * because we cannot determine safe system-wide partid and pmg
+		 * ranges in this situation.
+		 */
+		if (tbl_msc->reserved || tbl_msc->reserved1 || tbl_msc->reserved2) {
+			pr_err_once("Unrecognised MSC, MPAM not usable\n");
+			pr_debug("MSC.%u: reserved field set\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (!tbl_msc->mmio_size) {
+			pr_debug("MSC.%u: marked as disabled\n", tbl_msc->identifier);
+			continue;
+		}
+
+		if (decode_interface_type(tbl_msc, &iface)) {
+			pr_debug("MSC.%u: unknown interface type\n", tbl_msc->identifier);
+			continue;
+		}
+
+		next_res = 0;
+		next_prop = 0;
+		memset(res, 0, sizeof(res));
+		memset(props, 0, sizeof(props));
+
+		pdev = platform_device_alloc("mpam_msc", tbl_msc->identifier);
+		if (!pdev) {
+			err = -ENOMEM;
+			break;
+		}
+
+		if (tbl_msc->length < sizeof(*tbl_msc)) {
+			err = -EINVAL;
+			break;
+		}
+
+		/* Some power management is described in the namespace: */
+		err = snprintf(uid, sizeof(uid), "%u", tbl_msc->identifier);
+		if (err > 0 && err < sizeof(uid)) {
+			companion = acpi_dev_get_first_match_dev("ARMHAA5C", uid, -1);
+			if (companion)
+				ACPI_COMPANION_SET(&pdev->dev, companion);
+			else
+				pr_debug("MSC.%u: missing namespace entry\n", tbl_msc->identifier);
+		}
+
+		if (iface == MPAM_IFACE_MMIO) {
+			res[next_res++] = DEFINE_RES_MEM_NAMED(tbl_msc->base_address,
+							       tbl_msc->mmio_size,
+							       "MPAM:MSC");
+		} else if (iface == MPAM_IFACE_PCC) {
+			props[next_prop++] = PROPERTY_ENTRY_U32("pcc-channel",
+								tbl_msc->base_address);
+			next_prop++;
+		}
+
+		acpi_mpam_parse_irqs(pdev, tbl_msc, res, &next_res);
+		err = platform_device_add_resources(pdev, res, next_res);
+		if (err)
+			break;
+
+		props[next_prop++] = PROPERTY_ENTRY_U32("arm,not-ready-us",
+							tbl_msc->max_nrdy_usec);
+
+		/*
+		 * The MSC's CPU affinity is described via its linked power
+		 * management device, but only if it points at a Processor or
+		 * Processor Container.
+		 */
+		if (parse_msc_pm_link(tbl_msc, pdev, &acpi_id)) {
+			props[next_prop++] = PROPERTY_ENTRY_U32("cpu_affinity",
+								acpi_id);
+		}
+
+		err = device_create_managed_software_node(&pdev->dev, props,
+							  NULL);
+		if (err)
+			break;
+
+		/* Come back later if you want the RIS too */
+		err = platform_device_add_data(pdev, tbl_msc, tbl_msc->length);
+		if (err)
+			break;
+
+		err = platform_device_add(pdev);
+		if (err)
+			break;
+	}
+
+	if (err)
+		platform_device_put(pdev);
+
+	return err;
+}
+
+int acpi_mpam_count_msc(void)
+{
+	struct acpi_table_header *table __free(acpi_table) = acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+	char *table_end, *table_offset = (char *)(table + 1);
+	struct acpi_mpam_msc_node *tbl_msc;
+	int count = 0;
+
+	if (IS_ERR(table))
+		return 0;
+
+	if (table->revision < 1)
+		return 0;
+
+	table_end = (char *)table + table->length;
+
+	while (table_offset < table_end) {
+		tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+		if (!tbl_msc->mmio_size)
+			continue;
+
+		if (tbl_msc->length < sizeof(*tbl_msc))
+			return -EINVAL;

drivers/acpi/arm64/mpam.c line 355

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+		if (tbl_msc->length > table_end - table_offset)
+			return -EINVAL;
+		table_offset += tbl_msc->length;
+
+		count++;
+	}
+
+	return count;
+}
+

acpi_mpam_count_msc() iterates the existing MSC node, which is part of acpi_mpam_parse(). So the question is why we can't drop acpi_mpam_count_msc() and maintain a variable to count the existing MSC nodes in acpi_mpam_parse() ?

Quoted 93 lines
drivers/acpi/arm64/mpam.c new file +6 -0
+/*
+ * Call after ACPI devices have been created, which happens behind acpi_scan_init()
+ * called from subsys_initcall(). PCC requires the mailbox driver, which is
+ * initialised from postcore_initcall().
+ */
+subsys_initcall_sync(acpi_mpam_parse);
drivers/acpi/tables.c +1 -1
index fa9bb8c8ce95..835e3795ede3 100644
@@ -408,7 +408,7 @@ static const char table_sigs[][ACPI_NAMESEG_SIZE] __nonstring_array __initconst
  	ACPI_SIG_PSDT, ACPI_SIG_RSDT, ACPI_SIG_XSDT, ACPI_SIG_SSDT,
  	ACPI_SIG_IORT, ACPI_SIG_NFIT, ACPI_SIG_HMAT, ACPI_SIG_PPTT,
  	ACPI_SIG_NHLT, ACPI_SIG_AEST, ACPI_SIG_CEDT, ACPI_SIG_AGDI,
-	ACPI_SIG_NBFT };
+	ACPI_SIG_NBFT, ACPI_SIG_MPAM };
 
  #define ACPI_HEADER_SIZE sizeof(struct acpi_table_header)
 
include/linux/acpi.h +12 -0
index c5fd92cda487..af449964426b 100644
@@ -8,6 +8,7 @@
  #ifndef _LINUX_ACPI_H
  #define _LINUX_ACPI_H
 
+#include <linux/cleanup.h>
  #include <linux/errno.h>
  #include <linux/ioport.h>	/* for struct resource */
  #include <linux/resource_ext.h>
@@ -221,6 +222,17 @@ void acpi_reserve_initial_tables (void);
  void acpi_table_init_complete (void);
  int acpi_table_init (void);
 
+static inline struct acpi_table_header *acpi_get_table_ret(char *signature, u32 instance)
+{
+	struct acpi_table_header *table;
+	int status = acpi_get_table(signature, instance, &table);
+
+	if (ACPI_FAILURE(status))
+		return ERR_PTR(-ENOENT);
+	return table;
+}
+DEFINE_FREE(acpi_table, struct acpi_table_header *, if (!IS_ERR(_T)) acpi_put_table(_T))
+
  int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
  int __init_or_acpilib acpi_table_parse_entries(char *id,
  		unsigned long table_size, int entry_id,
include/linux/arm_mpam.h new file +38 -0
new file mode 100644
index 000000000000..3d6c39c667c3
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2025 Arm Ltd. */
+
+#ifndef __LINUX_ARM_MPAM_H
+#define __LINUX_ARM_MPAM_H
+
+#include <linux/acpi.h>
+#include <linux/types.h>
+
+#define GLOBAL_AFFINITY		~0
+
+struct mpam_msc;
+
+enum mpam_msc_iface {
+	MPAM_IFACE_MMIO,	/* a real MPAM MSC */
+	MPAM_IFACE_PCC,		/* a fake MPAM MSC */
+};
+
+enum mpam_class_types {
+	MPAM_CLASS_CACHE,       /* Well known caches, e.g. L2 */
+	MPAM_CLASS_MEMORY,      /* Main memory */
+	MPAM_CLASS_UNKNOWN,     /* Everything else, e.g. SMMU */
+};
+
+#ifdef CONFIG_ACPI_MPAM
+/* Parse the ACPI description of resources entries for this MSC. */
+int acpi_mpam_parse_resources(struct mpam_msc *msc,
+			      struct acpi_mpam_msc_node *tbl_msc);
+
+int acpi_mpam_count_msc(void);
+#else
+static inline int acpi_mpam_parse_resources(struct mpam_msc *msc,
+					    struct acpi_mpam_msc_node *tbl_msc)
+{
+	return -EINVAL;
+}
+
+static inline int acpi_mpam_count_msc(void) { return -EINVAL; }

include/linux/arm_mpam.h line 48

Quoted 10 lines
include/linux/arm_mpam.h new file +10 -0
+#endif
+
+static inline int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
+				  enum mpam_class_types type, u8 class_id,
+				  int component_id)
+{
+	return -EINVAL;
+}
+
+#endif /* __LINUX_ARM_MPAM_H */

Thanks,
Gavin

author maintainer
#51 lore raw

Re: [PATCH v2 06/29] ACPI / MPAM: Parse the MPAM table

Hi Gavin,

Quoted 45 lines · Gavin Shan
Quoted 31 lines · James Morse

Add code to parse the arm64 specific MPAM table, looking up the cache level from the PPTT and feeding the end result into the MPAM driver.

For now the MPAM hook mpam_ris_create() is stubbed out, but will update the MPAM driver with optional discovered data.

drivers/acpi/arm64/mpam.c new file +19 -0
new file mode 100644
index 000000000000..fd9cfa143676
 
+static bool acpi_mpam_register_irq(struct platform_device *pdev, int intid,
+                   u32 flags, int *irq,
+                   u32 processor_container_uid)
+{
+    int sense;
+
+    if (!intid)
+        return false;
+
+    if (FIELD_GET(ACPI_MPAM_MSC_IRQ_TYPE_MASK, flags) !=
+        ACPI_MPAM_MSC_IRQ_TYPE_WIRED)
+        return false;
+
+    sense = FIELD_GET(ACPI_MPAM_MSC_IRQ_MODE_MASK, flags);
+
+    if (16 <= intid && intid < 32 && processor_container_uid != GLOBAL_AFFINITY) {
+        pr_err_once("Partitioned interrupts not supported\n");
+        return false;
+    }

drivers/acpi/arm64/mpam.c line 55

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+
+    *irq = acpi_register_gsi(&pdev->dev, intid, sense, ACPI_ACTIVE_HIGH);
+    if (*irq <= 0) {
+        pr_err_once("Failed to register interrupt 0x%x with ACPI\n",
+                intid);
+        return false;
+    }
+
+    return true;
+}

0 is allowed by acpi_register_gsi().

Quoted 4 lines

    if (*irq < 0) {
        pr_err_once(...);
        return false;
    }

Really? I thought irq-zero was nonsense.
acpi_register_gsi() does this:

|        irq = irq_create_fwspec_mapping(&fwspec);
|        if (!irq)
|                return -EINVAL;
|
|        return irq;
Quoted 26 lines
Quoted 12 lines
drivers/acpi/arm64/mpam.c new file +12 -0
+static int acpi_mpam_parse_resource(struct mpam_msc *msc,
+                    struct acpi_mpam_resource_node *res)
+{
+    int level, nid;
+    u32 cache_id;
+
+    switch (res->locator_type) {
+    case ACPI_MPAM_LOCATION_TYPE_PROCESSOR_CACHE:
+        cache_id = res->locator.cache_locator.cache_reference;
+        level = find_acpi_cache_level_from_id(cache_id);
+        if (level <= 0) {
+            pr_err_once("Bad level (%u) for cache with id %u\n", level, cache_id);

drivers/acpi/arm64/mpam.c line 104

Quoted 10 lines
drivers/acpi/arm64/mpam.c new file +10 -0
+            return -EINVAL;
+        }
+        return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_CACHE,
+                       level, cache_id);
+    case ACPI_MPAM_LOCATION_TYPE_MEMORY:
+        nid = pxm_to_node(res->locator.memory_locator.proximity_domain);
+        if (nid == NUMA_NO_NODE)
+            nid = 0;
+        return mpam_ris_create(msc, res->ris_index, MPAM_CLASS_MEMORY,
+                       255, nid);

It's perhaps worthy a warning message when @nid is explicitly set to zero due to the bad proximity domain, something like below.

Quoted 7 lines

        if (nid == NUMA_NO_NODE) {
            nid = 0;
            if (num_possible_nodes() > 1) {
                pr_warn("Bad proximity domain %d, mapped to node 0\n", res->locator.memory_locator.proximity_domain); }
        }

This was to catch the case where you build the kernel without NUMA support - which wouldn't be an error. But that returns 0 instead of NUMA_NO_NODE, so NUMA_NO_NODE only occurs when there is a bug. I'l add this - but it'll be a pr_debug() as the message is only of use to about 4 people!

Quoted 34 lines
Quoted 34 lines

+    default:
+        /* These get discovered later and treated as unknown */ +        return 0;
+    }

drivers/acpi/arm64/mpam.c new file +4 -0
+}
 
+int acpi_mpam_count_msc(void)
+{
+    struct acpi_table_header *table __free(acpi_table) =

acpi_get_table_ret(ACPI_SIG_MPAM, 0);
+    char *table_end, *table_offset = (char *)(table + 1); +    struct acpi_mpam_msc_node *tbl_msc;
+    int count = 0;

drivers/acpi/arm64/mpam.c new file +21 -0
+
+    if (IS_ERR(table))
+        return 0;
+
+    if (table->revision < 1)
+        return 0;
+
+    table_end = (char *)table + table->length;
+
+    while (table_offset < table_end) {
+        tbl_msc = (struct acpi_mpam_msc_node *)table_offset;
+        if (!tbl_msc->mmio_size)
+            continue;
+
+        if (tbl_msc->length < sizeof(*tbl_msc))
+            return -EINVAL;
+        if (tbl_msc->length > table_end - table_offset)
+            return -EINVAL;
+        table_offset += tbl_msc->length;
+
+        count++;
Quoted 8 lines

drivers/acpi/arm64/mpam.c line 354

Quoted 4 lines

+    }

drivers/acpi/arm64/mpam.c new file +3 -0
+
+    return count;
+}

acpi_mpam_count_msc() iterates the existing MSC node, which is part of acpi_mpam_parse(). So the question is why we can't drop acpi_mpam_count_msc() and maintain a variable to count the existing MSC nodes in acpi_mpam_parse() ?

Once the platform device has been created, the driver's probe call can be called, and that needs to know how many MSC there are going to be. Doing it like this means we don't depend on the driver probe function not being called until we got to the end of the list. (the comments about the initcall dependencies are already annoying - I prefer not to add any that are specific to MPAM).

This also lets us catch non-backward compatible ACPI table changes, which has already happened with PCC. acpi_mpam_parse() skips MSC with reserved fields set, acpi_mpam_count_msc() does not - this means we are guaranteed the values will mismatch if any of those reserved fields are set, and the driver will never try to touch the hardware.

Thanks,

James

author maintainer
#52 lore raw

PATCH v2 7/29 arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate

In reply to James Morse

Probing MPAM is convoluted. MSCs that are integrated with a CPU may only be accessible from those CPUs, and they may not be online. Touching the hardware early is pointless as MPAM can't be used until the system-wide common values for num_partid and num_pmg have been discovered.

Start with driver probe/remove and mapping the MSC.

---
Changes since v1:
* Avoid selecting driver on other architectrues.
* Removed PCC support stub.
* Use for_each_available_child_of_node_scoped() and of_property_read_reg()
* Clarified a comment.
* Stopped using mpam_num_msc as an id,a and made it atomic.
* Size of -1 returned from cache_of_calculate_id()
* Renamed some struct members.
* Made a bunch of pr_err() dev_err_ocne().
* Used more cleanup magic.
* Inlined a print message.
* Fixed error propagation from mpam_dt_parse_resources().
* Moved cache accessibility checks earlier.

Changes since RFC:
* Check for status=broken DT devices.
* Moved all the files around.
* Made Kconfig symbols depend on EXPERT

---
 arch/arm64/Kconfig              |   1 +
 drivers/Kconfig                 |   2 +
 drivers/Makefile                |   1 +
 drivers/resctrl/Kconfig         |  14 +++
 drivers/resctrl/Makefile        |   4 +
 drivers/resctrl/mpam_devices.c  | 180 ++++++++++++++++++++++++++++++++
 drivers/resctrl/mpam_internal.h |  65 ++++++++++++
 7 files changed, 267 insertions(+)
 create mode 100644 drivers/resctrl/Kconfig
 create mode 100644 drivers/resctrl/Makefile
 create mode 100644 drivers/resctrl/mpam_devices.c
 create mode 100644 drivers/resctrl/mpam_internal.h
arch/arm64/Kconfig +1 -0
index 6487c511bdc6..93e563e1cce4 100644
@@ -2062,6 +2062,7 @@ config ARM64_TLB_RANGE
 
 config ARM64_MPAM
 	bool "Enable support for MPAM"
+	select ARM64_MPAM_DRIVER if EXPERT
 	select ACPI_MPAM if ACPI
 	help
 	  Memory System Resource Partitioning and Monitoring (MPAM) is an
drivers/Kconfig +2 -0
index 4915a63866b0..3054b50a2f4c 100644
@@ -251,4 +251,6 @@ source "drivers/hte/Kconfig"
 
 source "drivers/cdx/Kconfig"
 
+source "drivers/resctrl/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index b5749cf67044..f41cf4eddeba 100644
@@ -194,5 +194,6 @@ obj-$(CONFIG_HTE)		+= hte/
 obj-$(CONFIG_DRM_ACCEL)		+= accel/
 obj-$(CONFIG_CDX_BUS)		+= cdx/
 obj-$(CONFIG_DPLL)		+= dpll/
+obj-y				+= resctrl/
 
 obj-$(CONFIG_S390)		+= s390/
drivers/resctrl/Kconfig new file +14 -0
new file mode 100644
index 000000000000..c30532a3a3a4
@@ -0,0 +1,14 @@
+menuconfig ARM64_MPAM_DRIVER
+	bool "MPAM driver"
+	depends on ARM64 && ARM64_MPAM && EXPERT
+	help
+	  MPAM driver for System IP, e,g. caches and memory controllers.
+
+if ARM64_MPAM_DRIVER
+config ARM64_MPAM_DRIVER_DEBUG
+	bool "Enable debug messages from the MPAM driver"
+	depends on ARM64_MPAM_DRIVER
+	help
+	  Say yes here to enable debug messages from the MPAM driver.
+
+endif
drivers/resctrl/Makefile new file +4 -0
new file mode 100644
index 000000000000..92b48fa20108
@@ -0,0 +1,4 @@
+obj-$(CONFIG_ARM64_MPAM_DRIVER)			+= mpam.o
+mpam-y						+= mpam_devices.o
+
+cflags-$(CONFIG_ARM64_MPAM_DRIVER_DEBUG)	+= -DDEBUG
drivers/resctrl/mpam_devices.c new file +180 -0

180 lines not on this page · raw message

drivers/resctrl/mpam_internal.h new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.39.5
#53 lore raw

Re: [PATCH v2 07/29] arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate

Quoted 10 lines · On Wed, 10 Sep 2025 20:42:47 +0000 James Morse <[email protected]> wrote:

Probing MPAM is convoluted. MSCs that are integrated with a CPU may only be accessible from those CPUs, and they may not be online. Touching the hardware early is pointless as MPAM can't be used until the system-wide common values for num_partid and num_pmg have been discovered.

Start with driver probe/remove and mapping the MSC.

Hi James,

Various comments inline. You can ignore the do/while(0) one but I'll probably forget and send more grumpy comments about it ;)

Jonathan

Quoted 38 lines

---
Changes since v1:
* Avoid selecting driver on other architectrues.
* Removed PCC support stub.
* Use for_each_available_child_of_node_scoped() and of_property_read_reg()
* Clarified a comment.
* Stopped using mpam_num_msc as an id,a and made it atomic.
* Size of -1 returned from cache_of_calculate_id()
* Renamed some struct members.
* Made a bunch of pr_err() dev_err_ocne().
* Used more cleanup magic.
* Inlined a print message.
* Fixed error propagation from mpam_dt_parse_resources().
* Moved cache accessibility checks earlier.

Changes since RFC:
* Check for status=broken DT devices.
* Moved all the files around.
* Made Kconfig symbols depend on EXPERT

---
 arch/arm64/Kconfig              |   1 +
 drivers/Kconfig                 |   2 +
 drivers/Makefile                |   1 +
 drivers/resctrl/Kconfig         |  14 +++
 drivers/resctrl/Makefile        |   4 +
 drivers/resctrl/mpam_devices.c  | 180 ++++++++++++++++++++++++++++++++
 drivers/resctrl/mpam_internal.h |  65 ++++++++++++
 7 files changed, 267 insertions(+)
 create mode 100644 drivers/resctrl/Kconfig
 create mode 100644 drivers/resctrl/Makefile
 create mode 100644 drivers/resctrl/mpam_devices.c
 create mode 100644 drivers/resctrl/mpam_internal.h
arch/arm64/Kconfig +0 -0
index 6487c511bdc6..93e563e1cce4 100644

arch/arm64/Kconfig config ARM64_TLB_RANGE · line 2065

Quoted 4 lines
arch/arm64/Kconfig +1 -0
@@ -2062,6 +2062,7 @@ config ARM64_TLB_RANGE
 
 config ARM64_MPAM
 	bool "Enable support for MPAM"
+	select ARM64_MPAM_DRIVER if EXPERT

To me that wants a comment as it's unusual.

Quoted 10 lines
arch/arm64/Kconfig +0 -0
 	select ACPI_MPAM if ACPI
 	help
 	  Memory System Resource Partitioning and Monitoring (MPAM) is an
drivers/resctrl/Kconfig new file +0 -0
new file mode 100644
index 000000000000..c30532a3a3a4

drivers/resctrl/Kconfig line 10

Quoted 10 lines
drivers/resctrl/Kconfig new file +10 -0
@@ -0,0 +1,14 @@
+menuconfig ARM64_MPAM_DRIVER
+	bool "MPAM driver"
+	depends on ARM64 && ARM64_MPAM && EXPERT
+	help
+	  MPAM driver for System IP, e,g. caches and memory controllers.
+
+if ARM64_MPAM_DRIVER
+config ARM64_MPAM_DRIVER_DEBUG
+	bool "Enable debug messages from the MPAM driver"
+	depends on ARM64_MPAM_DRIVER

The depends on should make the if unnecessary.

Quoted 47 lines
drivers/resctrl/Kconfig new file +4 -0
+	help
+	  Say yes here to enable debug messages from the MPAM driver.
+
+endif
drivers/resctrl/mpam_devices.c new file +35 -0
new file mode 100644
index 000000000000..efc4738e3b4d
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2025 Arm Ltd.
+
+#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
+
+#include <linux/acpi.h>
+#include <linux/arm_mpam.h>
+#include <linux/cacheinfo.h>
+#include <linux/cpu.h>
+#include <linux/cpumask.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/gfp.h>
+#include <linux/list.h>
+#include <linux/lockdep.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/printk.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/srcu.h>
+#include <linux/types.h>
 
+/*
+ * An MSC can control traffic from a set of CPUs, but may only be accessible
+ * from a (hopefully wider) set of CPUs. The common reason for this is power
+ * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
+ * corresponding cache may also be powered off. By making accesses from
+ * one of those CPUs, we ensure this isn't the case.
+ */
+static int update_msc_accessibility(struct mpam_msc *msc)
+{
+	u32 affinity_id;
+	int err;
+
+	err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",

drivers/resctrl/mpam_devices.c line 64

Quoted 10 lines
drivers/resctrl/mpam_devices.c new file +10 -0
+				       &affinity_id);
+	if (err)
+		cpumask_copy(&msc->accessibility, cpu_possible_mask);
+	else
+		acpi_pptt_get_cpus_from_container(affinity_id,
+						  &msc->accessibility);
+
+	return 0;
+
+	return err;

Curious. I'd do a build test after each patch before v3. A couple of places would have failed or given helpful warnings so far.

drivers/resctrl/mpam_devices.c line 92

Quoted 13 lines
drivers/resctrl/mpam_devices.c new file +12 -0
+}
 
+
+static int mpam_msc_drv_probe(struct platform_device *pdev)
+{
+	int err;
+	struct mpam_msc *msc;
+	struct resource *msc_res;
+	struct device *dev = &pdev->dev;
+	void *plat_data = pdev->dev.platform_data;
+
+	mutex_lock(&mpam_list_lock);
+	do {

I might well have moaned about this before, but I really dislike a do while(0) if it doesn't fit on my screen (and my eyesight is poor so that's not this many lines). To me a non trivial case of this is almost always a place where a '_do' function would have made it more readable.

I'm also not a fan of scoped_guard() plus breaks because it feels like it is dependent on an implementation detail but maybe it's clearer than this.

Quoted 46 lines
drivers/resctrl/mpam_devices.c new file +46 -0
+		msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
+		if (!msc) {
+			err = -ENOMEM;
+			break;
+		}
+
+		mutex_init(&msc->probe_lock);
+		mutex_init(&msc->part_sel_lock);
+		mutex_init(&msc->outer_mon_sel_lock);
+		raw_spin_lock_init(&msc->inner_mon_sel_lock);
+		msc->id = pdev->id;
+		msc->pdev = pdev;
+		INIT_LIST_HEAD_RCU(&msc->all_msc_list);
+		INIT_LIST_HEAD_RCU(&msc->ris);
+
+		err = update_msc_accessibility(msc);
+		if (err)
+			break;
+		if (cpumask_empty(&msc->accessibility)) {
+			dev_err_once(dev, "MSC is not accessible from any CPU!");
+			err = -EINVAL;
+			break;
+		}
+
+		if (device_property_read_u32(&pdev->dev, "pcc-channel",
+					     &msc->pcc_subspace_id))
+			msc->iface = MPAM_IFACE_MMIO;
+		else
+			msc->iface = MPAM_IFACE_PCC;
+
+		if (msc->iface == MPAM_IFACE_MMIO) {
+			void __iomem *io;
+
+			io = devm_platform_get_and_ioremap_resource(pdev, 0,
+								    &msc_res);
+			if (IS_ERR(io)) {
+				dev_err_once(dev, "Failed to map MSC base address\n");
+				err = PTR_ERR(io);
+				break;
+			}
+			msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
+			msc->mapped_hwpage = io;
+		}
+
+		list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
+		platform_set_drvdata(pdev, msc);

drivers/resctrl/mpam_devices.c line 148

Quoted 10 lines
drivers/resctrl/mpam_devices.c new file +10 -0
+	} while (0);
+	mutex_unlock(&mpam_list_lock);
+
+	if (!err) {
+		/* Create RIS entries described by firmware */
+		err = acpi_mpam_parse_resources(msc, plat_data);
+	}
+
+	if (err && msc)
+		mpam_msc_drv_remove(pdev);

Is it worth bothering to remove? We failed probe anyway if we got here and it's not expected to happen on real systems so I'd just leave it around so that you can exit early above.

I'm also not following why the msc check is relevant if you do want to do this. Can only get here without msc if the allocation failed. Why would we leave the driver loaded in only that case?

Quoted 14 lines
drivers/resctrl/mpam_devices.c new file +6 -0
+
+	if (!err && atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
+		pr_info("Discovered all MSC\n");
+
+	return err;
+}
drivers/resctrl/mpam_internal.h new file +1 -0
new file mode 100644
index 000000000000..7c63d590fc98
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-2.0 */

drivers/resctrl/mpam_internal.h line 11

Quoted 10 lines
drivers/resctrl/mpam_internal.h new file +10 -0
+// Copyright (C) 2025 Arm Ltd.
+
+#ifndef MPAM_INTERNAL_H
+#define MPAM_INTERNAL_H
+
+#include <linux/arm_mpam.h>
+#include <linux/cpumask.h>
+#include <linux/io.h>
+#include <linux/mailbox_client.h>
+#include <linux/mutex.h>

spinlock.h

drivers/resctrl/mpam_internal.h line 12

Quoted 1 line
drivers/resctrl/mpam_internal.h new file +1 -0
+#include <linux/resctrl.h>

Not spotting anything rsctl yet. So maybe this belongs later.

drivers/resctrl/mpam_internal.h line 19

Quoted 7 lines
drivers/resctrl/mpam_internal.h new file +7 -0
+#include <linux/sizes.h>
+
+struct mpam_msc {
+	/* member of mpam_all_msc */
+	struct list_head        all_msc_list;
+
+	int			id;

I'd follow (approx) include what you use principles to make later header shuffling easier. So a forward def for this.

drivers/resctrl/mpam_internal.h line 26

Quoted 7 lines
drivers/resctrl/mpam_internal.h new file +7 -0
+	struct platform_device *pdev;
+
+	/* Not modified after mpam_is_enabled() becomes true */
+	enum mpam_msc_iface	iface;
+	u32			pcc_subspace_id;
+	struct mbox_client	pcc_cl;
+	struct pcc_mbox_chan	*pcc_chan;

Forward def or include acpi/pcc.h

Quoted 27 lines
drivers/resctrl/mpam_internal.h new file +27 -0
+	u32			nrdy_usec;
+	cpumask_t		accessibility;
+
+	/*
+	 * probe_lock is only taken during discovery. After discovery these
+	 * properties become read-only and the lists are protected by SRCU.
+	 */
+	struct mutex		probe_lock;
+	unsigned long		ris_idxs;
+	u32			ris_max;
+
+	/* mpam_msc_ris of this component */
+	struct list_head	ris;
+
+	/*
+	 * part_sel_lock protects access to the MSC hardware registers that are
+	 * affected by MPAMCFG_PART_SEL. (including the ID registers that vary
+	 * by RIS).
+	 * If needed, take msc->probe_lock first.
+	 */
+	struct mutex		part_sel_lock;
+
+	/*
+	 * mon_sel_lock protects access to the MSC hardware registers that are
+	 * affected by MPAMCFG_MON_SEL.
+	 * If needed, take msc->probe_lock first.
+	 */

drivers/resctrl/mpam_internal.h line 63

Quoted 10 lines
drivers/resctrl/mpam_internal.h new file +10 -0
+	struct mutex		outer_mon_sel_lock;
+	raw_spinlock_t		inner_mon_sel_lock;
+	unsigned long		inner_mon_sel_flags;
+
+	void __iomem		*mapped_hwpage;
+	size_t			mapped_hwpage_sz;
+};
+
+int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
+				   cpumask_t *affinity);

Where is this?

Quoted 2 lines
drivers/resctrl/mpam_internal.h new file +2 -0
+
+#endif /* MPAM_INTERNAL_H */
author maintainer
#54 lore raw

Re: [PATCH v2 07/29] arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate

Hi Jonathan,

Quoted 9 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 10 Sep 2025 20:42:47 +0000 James Morse <[email protected]> wrote:

Probing MPAM is convoluted. MSCs that are integrated with a CPU may only be accessible from those CPUs, and they may not be online. Touching the hardware early is pointless as MPAM can't be used until the system-wide common values for num_partid and num_pmg have been discovered.

Quoted 6 lines
Quoted 1 line

Start with driver probe/remove and mapping the MSC.

Hi James,

Various comments inline. You can ignore the do/while(0) one but I'll probably forget and send more grumpy comments about it ;)

I got exposed to this quite a lot in some other project - its better than a goto. Due to the heavy exposure, I don't think its odd - I'm just used to seeing it. (not sure its quite Stockholm syndrome yet)

Quoted 11 lines

arch/arm64/Kconfig config ARM64_TLB_RANGE · line 2065

Quoted 9 lines
arch/arm64/Kconfig +1 -0
index 6487c511bdc6..93e563e1cce4 100644
@@ -2062,6 +2062,7 @@ config ARM64_TLB_RANGE
 
 config ARM64_MPAM
 	bool "Enable support for MPAM"
+	select ARM64_MPAM_DRIVER if EXPERT

To me that wants a comment as it's unusual.

Sure - "# does nothing yet".
This is to stop it being enabled in distros as it can't be used until the resctrl part is added.

Quoted 10 lines
Quoted 10 lines
drivers/resctrl/Kconfig new file +4 -0
new file mode 100644
index 000000000000..c30532a3a3a4
@@ -0,0 +1,14 @@
+menuconfig ARM64_MPAM_DRIVER
+	bool "MPAM driver"
+	depends on ARM64 && ARM64_MPAM && EXPERT
+	help
Quoted 8 lines

drivers/resctrl/Kconfig line 10

Quoted 6 lines
drivers/resctrl/Kconfig new file +6 -0
+	  MPAM driver for System IP, e,g. caches and memory controllers.
+
+if ARM64_MPAM_DRIVER
+config ARM64_MPAM_DRIVER_DEBUG
+	bool "Enable debug messages from the MPAM driver"
+	depends on ARM64_MPAM_DRIVER

The depends on should make the if unnecessary.

Yup, that was a recent change and I didn't spot the duplication.

Quoted 47 lines
Quoted 47 lines
drivers/resctrl/mpam_devices.c new file +40 -0
new file mode 100644
index 000000000000..efc4738e3b4d
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2025 Arm Ltd.
+
+#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
+
+#include <linux/acpi.h>
+#include <linux/arm_mpam.h>
+#include <linux/cacheinfo.h>
+#include <linux/cpu.h>
+#include <linux/cpumask.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/gfp.h>
+#include <linux/list.h>
+#include <linux/lockdep.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/printk.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/srcu.h>
+#include <linux/types.h>
 
+/*
+ * An MSC can control traffic from a set of CPUs, but may only be accessible
+ * from a (hopefully wider) set of CPUs. The common reason for this is power
+ * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
+ * corresponding cache may also be powered off. By making accesses from
+ * one of those CPUs, we ensure this isn't the case.
+ */
+static int update_msc_accessibility(struct mpam_msc *msc)
+{
+	u32 affinity_id;
+	int err;
+
+	err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",
+				       &affinity_id);
+	if (err)
+		cpumask_copy(&msc->accessibility, cpu_possible_mask);
+	else
+		acpi_pptt_get_cpus_from_container(affinity_id,
Quoted 8 lines

drivers/resctrl/mpam_devices.c line 64

Quoted 5 lines
drivers/resctrl/mpam_devices.c new file +5 -0
+						  &msc->accessibility);
+
+	return 0;
+
+	return err;

Curious. I'd do a build test after each patch before v3. A couple of places would have failed or given helpful warnings so far.

Oddly - the compiler is quite happy with this. Not sure why. I've fixed it to return err.

This was due to the last second rip-out of the DT support.

Quoted 18 lines

drivers/resctrl/mpam_devices.c line 92

Quoted 13 lines
drivers/resctrl/mpam_devices.c new file +12 -0
+}
 
+
+static int mpam_msc_drv_probe(struct platform_device *pdev)
+{
+	int err;
+	struct mpam_msc *msc;
+	struct resource *msc_res;
+	struct device *dev = &pdev->dev;
+	void *plat_data = pdev->dev.platform_data;
+
+	mutex_lock(&mpam_list_lock);
+	do {

I might well have moaned about this before, but I really dislike a do while(0) if it doesn't fit on my screen (and my eyesight is poor so that's not this many lines). To me a non trivial case of this is almost always a place where a '_do' function would have made it more readable.

I guess I'm very used to seeing this pattern, so I don't notice it.

I've change this to a do_ thing with all this shoved in another function.

Quoted 2 lines

I'm also not a fan of scoped_guard() plus breaks because it feels like it is dependent on an implementation detail but maybe it's clearer than this.

(i.e. the cleanup stuff? I'd always prefer to see the free/unlock call)

Quoted 52 lines
Quoted 52 lines
drivers/resctrl/mpam_devices.c new file +52 -0
+		msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
+		if (!msc) {
+			err = -ENOMEM;
+			break;
+		}
+
+		mutex_init(&msc->probe_lock);
+		mutex_init(&msc->part_sel_lock);
+		mutex_init(&msc->outer_mon_sel_lock);
+		raw_spin_lock_init(&msc->inner_mon_sel_lock);
+		msc->id = pdev->id;
+		msc->pdev = pdev;
+		INIT_LIST_HEAD_RCU(&msc->all_msc_list);
+		INIT_LIST_HEAD_RCU(&msc->ris);
+
+		err = update_msc_accessibility(msc);
+		if (err)
+			break;
+		if (cpumask_empty(&msc->accessibility)) {
+			dev_err_once(dev, "MSC is not accessible from any CPU!");
+			err = -EINVAL;
+			break;
+		}
+
+		if (device_property_read_u32(&pdev->dev, "pcc-channel",
+					     &msc->pcc_subspace_id))
+			msc->iface = MPAM_IFACE_MMIO;
+		else
+			msc->iface = MPAM_IFACE_PCC;
+
+		if (msc->iface == MPAM_IFACE_MMIO) {
+			void __iomem *io;
+
+			io = devm_platform_get_and_ioremap_resource(pdev, 0,
+								    &msc_res);
+			if (IS_ERR(io)) {
+				dev_err_once(dev, "Failed to map MSC base address\n");
+				err = PTR_ERR(io);
+				break;
+			}
+			msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
+			msc->mapped_hwpage = io;
+		}
+
+		list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
+		platform_set_drvdata(pdev, msc);
+	} while (0);
+	mutex_unlock(&mpam_list_lock);
+
+	if (!err) {
+		/* Create RIS entries described by firmware */
+		err = acpi_mpam_parse_resources(msc, plat_data);
Quoted 8 lines

drivers/resctrl/mpam_devices.c line 148

Quoted 4 lines
drivers/resctrl/mpam_devices.c new file +4 -0
+	}
+
+	if (err && msc)
+		mpam_msc_drv_remove(pdev);

Is it worth bothering to remove? We failed probe anyway if we got here and it's not expected to happen on real systems so I'd just leave it around so that you can exit early above.

Symmetry and the principle of least surprise.
Ideally the probing code wouldn't need to unwind what it did, it can rely on the remove code to do the necessary. It was a patch from Carl Worth that did this as the unwind/remove code was duplication.

I agree there is no error that requires it here, but later patches add things like debugfs entries that need removing before the struct mpam_msc memory can be free'd - potentially by the devm stuff if the probe function returns an error due to the firmware tables 'RIS' description being wonky.

Quoted 3 lines

I'm also not following why the msc check is relevant if you do want to do this. Can only get here without msc if the allocation failed. Why would we leave the driver loaded in only that case?

Simply because the remove/destroy code goes dereferencing msc, so that is the one case that mustn't get in there. With your do_ version of the above this got simplified.

Quoted 11 lines
Quoted 11 lines
drivers/resctrl/mpam_internal.h new file +5 -0
new file mode 100644
index 000000000000..7c63d590fc98
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+// Copyright (C) 2025 Arm Ltd.
+
+#ifndef MPAM_INTERNAL_H
+#define MPAM_INTERNAL_H
Quoted 8 lines

drivers/resctrl/mpam_internal.h line 11

Quoted 6 lines
drivers/resctrl/mpam_internal.h new file +6 -0
+
+#include <linux/arm_mpam.h>
+#include <linux/cpumask.h>
+#include <linux/io.h>
+#include <linux/mailbox_client.h>
+#include <linux/mutex.h>

spinlock.h

Fixed,

Quoted 3 lines

drivers/resctrl/mpam_internal.h line 12

Quoted 1 line
drivers/resctrl/mpam_internal.h new file +1 -0
+#include <linux/resctrl.h>

Not spotting anything rsctl yet. So maybe this belongs later.

There shouldn't be anything that depends on resctrl in this series - looks like this is a 2018 era bug in the way I carved this up!

Quoted 10 lines

drivers/resctrl/mpam_internal.h line 19

Quoted 7 lines
drivers/resctrl/mpam_internal.h new file +7 -0
+#include <linux/sizes.h>
+
+struct mpam_msc {
+	/* member of mpam_all_msc */
+	struct list_head        all_msc_list;
+
+	int			id;

I'd follow (approx) include what you use principles to make later header shuffling easier. So a forward def for this.

-ENOPARSE

I'm sure I'll work this out from your later comments.

Quoted 9 lines

drivers/resctrl/mpam_internal.h line 26

Quoted 7 lines
drivers/resctrl/mpam_internal.h new file +7 -0
+	struct platform_device *pdev;
+
+	/* Not modified after mpam_is_enabled() becomes true */
+	enum mpam_msc_iface	iface;
+	u32			pcc_subspace_id;
+	struct mbox_client	pcc_cl;
+	struct pcc_mbox_chan	*pcc_chan;

Forward def or include acpi/pcc.h

The PCC code got pulled later, and out of this series. I missed these bits.

Quoted 31 lines
Quoted 31 lines
drivers/resctrl/mpam_internal.h new file +31 -0
+	u32			nrdy_usec;
+	cpumask_t		accessibility;
+
+	/*
+	 * probe_lock is only taken during discovery. After discovery these
+	 * properties become read-only and the lists are protected by SRCU.
+	 */
+	struct mutex		probe_lock;
+	unsigned long		ris_idxs;
+	u32			ris_max;
+
+	/* mpam_msc_ris of this component */
+	struct list_head	ris;
+
+	/*
+	 * part_sel_lock protects access to the MSC hardware registers that are
+	 * affected by MPAMCFG_PART_SEL. (including the ID registers that vary
+	 * by RIS).
+	 * If needed, take msc->probe_lock first.
+	 */
+	struct mutex		part_sel_lock;
+
+	/*
+	 * mon_sel_lock protects access to the MSC hardware registers that are
+	 * affected by MPAMCFG_MON_SEL.
+	 * If needed, take msc->probe_lock first.
+	 */
+	struct mutex		outer_mon_sel_lock;
+	raw_spinlock_t		inner_mon_sel_lock;
+	unsigned long		inner_mon_sel_flags;
+
Quoted 8 lines

drivers/resctrl/mpam_internal.h line 63

Quoted 6 lines
drivers/resctrl/mpam_internal.h new file +6 -0
+	void __iomem		*mapped_hwpage;
+	size_t			mapped_hwpage_sz;
+};
+
+int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
+				   cpumask_t *affinity);

Where is this?

Ugh, more bits of DT - this one is non-obvious because of the name.

Thanks,

James

#55 lore raw

Re: [PATCH v2 07/29] arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate

Quoted 43 lines
Quoted 12 lines
Quoted 12 lines
drivers/resctrl/mpam_internal.h new file +5 -0
new file mode 100644
index 000000000000..7c63d590fc98
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+// Copyright (C) 2025 Arm Ltd.
+
+#ifndef MPAM_INTERNAL_H
+#define MPAM_INTERNAL_H
Quoted 8 lines

drivers/resctrl/mpam_internal.h line 11

Quoted 6 lines
drivers/resctrl/mpam_internal.h new file +6 -0
+
+#include <linux/arm_mpam.h>
+#include <linux/cpumask.h>
+#include <linux/io.h>
+#include <linux/mailbox_client.h>
+#include <linux/mutex.h>  

spinlock.h

Fixed,

Quoted 3 lines

drivers/resctrl/mpam_internal.h line 12

Quoted 1 line
drivers/resctrl/mpam_internal.h new file +1 -0
+#include <linux/resctrl.h>  

Not spotting anything rsctl yet. So maybe this belongs later.

There shouldn't be anything that depends on resctrl in this series - looks like this is a 2018 era bug in the way I carved this up!

Quoted 10 lines

drivers/resctrl/mpam_internal.h line 19

Quoted 7 lines
drivers/resctrl/mpam_internal.h new file +7 -0
+#include <linux/sizes.h>
+
+struct mpam_msc {
+	/* member of mpam_all_msc */
+	struct list_head        all_msc_list;
+
+	int			id;  

I'd follow (approx) include what you use principles to make later header shuffling easier. So a forward def for this.

Quoted 3 lines

-ENOPARSE

I'm sure I'll work this out from your later comments.

I missed on the comment (I think). Would have made more sense a line later. Add a forwards def

struct platform_device;

as no reason to include the appropriate header
(and you didn't anwyay).

author maintainer
#56 lore raw

Re: [PATCH v2 07/29] arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate

Hi Jonathan,

Quoted 46 lines · Jonathan Cameron
Quoted 42 lines
Quoted 11 lines
Quoted 11 lines
drivers/resctrl/mpam_internal.h new file +5 -0
new file mode 100644
index 000000000000..7c63d590fc98
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+// Copyright (C) 2025 Arm Ltd.
+
+#ifndef MPAM_INTERNAL_H
+#define MPAM_INTERNAL_H
Quoted 8 lines

drivers/resctrl/mpam_internal.h line 11

Quoted 6 lines
drivers/resctrl/mpam_internal.h new file +6 -0
+
+#include <linux/arm_mpam.h>
+#include <linux/cpumask.h>
+#include <linux/io.h>
+#include <linux/mailbox_client.h>
+#include <linux/mutex.h>  

spinlock.h

Fixed,

Quoted 3 lines

drivers/resctrl/mpam_internal.h line 12

Quoted 1 line
drivers/resctrl/mpam_internal.h new file +1 -0
+#include <linux/resctrl.h>  

Not spotting anything rsctl yet. So maybe this belongs later.

There shouldn't be anything that depends on resctrl in this series - looks like this is a 2018 era bug in the way I carved this up!

Quoted 10 lines

drivers/resctrl/mpam_internal.h line 19

Quoted 7 lines
drivers/resctrl/mpam_internal.h new file +7 -0
+#include <linux/sizes.h>
+
+struct mpam_msc {
+	/* member of mpam_all_msc */
+	struct list_head        all_msc_list;
+
+	int			id;  

I'd follow (approx) include what you use principles to make later header shuffling easier. So a forward def for this.

Quoted 3 lines

-ENOPARSE

I'm sure I'll work this out from your later comments.

Quoted 8 lines

I missed on the comment (I think). Would have made more sense a line later. Add a forwards def

struct platform_device;

as no reason to include the appropriate header
(and you didn't anwyay).

Right - gotcha.

Thanks,

James

maintainer
#57 lore raw

Re: [PATCH v2 07/29] arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate

In reply to James Morse

Hi James,

Quoted 18 lines · James Morse

Probing MPAM is convoluted. MSCs that are integrated with a CPU may only be accessible from those CPUs, and they may not be online. Touching the hardware early is pointless as MPAM can't be used until the system-wide common values for num_partid and num_pmg have been discovered.

Start with driver probe/remove and mapping the MSC.

---

drivers/resctrl/Makefile new file +0 -0
new file mode 100644
index 000000000000..92b48fa20108

drivers/resctrl/Makefile line 4

Quoted 4 lines
drivers/resctrl/Makefile new file +4 -0
@@ -0,0 +1,4 @@
+obj-$(CONFIG_ARM64_MPAM_DRIVER)			+= mpam.o
+mpam-y						+= mpam_devices.o
+
+cflags-$(CONFIG_ARM64_MPAM_DRIVER_DEBUG)	+= -DDEBUG

s/cflags/ccflags/

Thanks,

Ben

author maintainer
#58 lore raw

Re: [PATCH v2 07/29] arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate

Hi Ben,

Quoted 9 lines · Ben Horgan
Quoted 7 lines · James Morse

Probing MPAM is convoluted. MSCs that are integrated with a CPU may only be accessible from those CPUs, and they may not be online. Touching the hardware early is pointless as MPAM can't be used until the system-wide common values for num_partid and num_pmg have been discovered.

Start with driver probe/remove and mapping the MSC.

Quoted 12 lines

drivers/resctrl/Makefile line 4

Quoted 10 lines
drivers/resctrl/Makefile new file +4 -0
new file mode 100644
index 000000000000..92b48fa20108
@@ -0,0 +1,4 @@
+obj-$(CONFIG_ARM64_MPAM_DRIVER)			+= mpam.o
+mpam-y						+= mpam_devices.o
+
+cflags-$(CONFIG_ARM64_MPAM_DRIVER_DEBUG)	+= -DDEBUG

s/cflags/ccflags/

Fixed, thanks,

James

#59 lore raw

Re: [PATCH v2 07/29] arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate

In reply to James Morse

Hi James,

Quoted 169 lines · James Morse

Probing MPAM is convoluted. MSCs that are integrated with a CPU may only be accessible from those CPUs, and they may not be online. Touching the hardware early is pointless as MPAM can't be used until the system-wide common values for num_partid and num_pmg have been discovered.

Start with driver probe/remove and mapping the MSC.

---
Changes since v1:
* Avoid selecting driver on other architectrues.
* Removed PCC support stub.
* Use for_each_available_child_of_node_scoped() and of_property_read_reg()
* Clarified a comment.
* Stopped using mpam_num_msc as an id,a and made it atomic.
* Size of -1 returned from cache_of_calculate_id()
* Renamed some struct members.
* Made a bunch of pr_err() dev_err_ocne().
* Used more cleanup magic.
* Inlined a print message.
* Fixed error propagation from mpam_dt_parse_resources().
* Moved cache accessibility checks earlier.

Changes since RFC:
* Check for status=broken DT devices.
* Moved all the files around.
* Made Kconfig symbols depend on EXPERT

---
  arch/arm64/Kconfig              |   1 +
  drivers/Kconfig                 |   2 +
  drivers/Makefile                |   1 +
  drivers/resctrl/Kconfig         |  14 +++
  drivers/resctrl/Makefile        |   4 +
  drivers/resctrl/mpam_devices.c  | 180 ++++++++++++++++++++++++++++++++
  drivers/resctrl/mpam_internal.h |  65 ++++++++++++
  7 files changed, 267 insertions(+)

create mode 100644 drivers/resctrl/Kconfig create mode 100644 drivers/resctrl/Makefile create mode 100644 drivers/resctrl/mpam_devices.c create mode 100644 drivers/resctrl/mpam_internal.h

arch/arm64/Kconfig +1 -0
index 6487c511bdc6..93e563e1cce4 100644
@@ -2062,6 +2062,7 @@ config ARM64_TLB_RANGE
 
  config ARM64_MPAM
  	bool "Enable support for MPAM"
+	select ARM64_MPAM_DRIVER if EXPERT
  	select ACPI_MPAM if ACPI
  	help
  	  Memory System Resource Partitioning and Monitoring (MPAM) is an
drivers/Kconfig +2 -0
index 4915a63866b0..3054b50a2f4c 100644
@@ -251,4 +251,6 @@ source "drivers/hte/Kconfig"
 
  source "drivers/cdx/Kconfig"
 
+source "drivers/resctrl/Kconfig"
+
  endmenu
drivers/Makefile +1 -0
index b5749cf67044..f41cf4eddeba 100644
@@ -194,5 +194,6 @@ obj-$(CONFIG_HTE)		+= hte/
  obj-$(CONFIG_DRM_ACCEL)		+= accel/
  obj-$(CONFIG_CDX_BUS)		+= cdx/
  obj-$(CONFIG_DPLL)		+= dpll/
+obj-y				+= resctrl/
 
  obj-$(CONFIG_S390)		+= s390/
drivers/resctrl/Kconfig new file +14 -0
new file mode 100644
index 000000000000..c30532a3a3a4
@@ -0,0 +1,14 @@
+menuconfig ARM64_MPAM_DRIVER
+	bool "MPAM driver"
+	depends on ARM64 && ARM64_MPAM && EXPERT
+	help
+	  MPAM driver for System IP, e,g. caches and memory controllers.
+
+if ARM64_MPAM_DRIVER
+config ARM64_MPAM_DRIVER_DEBUG
+	bool "Enable debug messages from the MPAM driver"
+	depends on ARM64_MPAM_DRIVER
+	help
+	  Say yes here to enable debug messages from the MPAM driver.
+
+endif
drivers/resctrl/Makefile new file +4 -0
new file mode 100644
index 000000000000..92b48fa20108
@@ -0,0 +1,4 @@
+obj-$(CONFIG_ARM64_MPAM_DRIVER)			+= mpam.o
+mpam-y						+= mpam_devices.o
+
+cflags-$(CONFIG_ARM64_MPAM_DRIVER_DEBUG)	+= -DDEBUG
drivers/resctrl/mpam_devices.c new file +56 -0
new file mode 100644
index 000000000000..efc4738e3b4d
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2025 Arm Ltd.
+
+#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
+
+#include <linux/acpi.h>
+#include <linux/arm_mpam.h>
+#include <linux/cacheinfo.h>
+#include <linux/cpu.h>
+#include <linux/cpumask.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/gfp.h>
+#include <linux/list.h>
+#include <linux/lockdep.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/printk.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/srcu.h>
+#include <linux/types.h>
+
+#include "mpam_internal.h"
+
+/*
+ * mpam_list_lock protects the SRCU lists when writing. Once the
+ * mpam_enabled key is enabled these lists are read-only,
+ * unless the error interrupt disables the driver.
+ */
+static DEFINE_MUTEX(mpam_list_lock);
+static LIST_HEAD(mpam_all_msc);
+
+static struct srcu_struct mpam_srcu;
+
+/*
+ * Number of MSCs that have been probed. Once all MSC have been probed MPAM
+ * can be enabled.
+ */
+static atomic_t mpam_num_msc;
+
+/*
+ * An MSC can control traffic from a set of CPUs, but may only be accessible
+ * from a (hopefully wider) set of CPUs. The common reason for this is power
+ * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
+ * corresponding cache may also be powered off. By making accesses from
+ * one of those CPUs, we ensure this isn't the case.
+ */
+static int update_msc_accessibility(struct mpam_msc *msc)
+{
+	u32 affinity_id;
+	int err;
+
+	err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",
+				       &affinity_id);
+	if (err)

drivers/resctrl/mpam_devices.c line 66

Quoted 10 lines
drivers/resctrl/mpam_devices.c new file +10 -0
+		cpumask_copy(&msc->accessibility, cpu_possible_mask);
+	else
+		acpi_pptt_get_cpus_from_container(affinity_id,
+						  &msc->accessibility);
+
+	return 0;
+
+	return err;
+}
+

Double return here and different values have been returned. I think here we need "return err". In this case, we needn't copy @cpu_possible_mask on error because the caller mpam_msc_drv_probe() will release the MSC instance.

Quoted 40 lines
drivers/resctrl/mpam_devices.c new file +40 -0
+static int fw_num_msc;
+
+static void mpam_msc_drv_remove(struct platform_device *pdev)
+{
+	struct mpam_msc *msc = platform_get_drvdata(pdev);
+
+	if (!msc)
+		return;
+
+	mutex_lock(&mpam_list_lock);
+	platform_set_drvdata(pdev, NULL);
+	list_del_rcu(&msc->all_msc_list);
+	synchronize_srcu(&mpam_srcu);
+	mutex_unlock(&mpam_list_lock);
+}
+
+static int mpam_msc_drv_probe(struct platform_device *pdev)
+{
+	int err;
+	struct mpam_msc *msc;
+	struct resource *msc_res;
+	struct device *dev = &pdev->dev;
+	void *plat_data = pdev->dev.platform_data;
+
+	mutex_lock(&mpam_list_lock);
+	do {
+		msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
+		if (!msc) {
+			err = -ENOMEM;
+			break;
+		}
+
+		mutex_init(&msc->probe_lock);
+		mutex_init(&msc->part_sel_lock);
+		mutex_init(&msc->outer_mon_sel_lock);
+		raw_spin_lock_init(&msc->inner_mon_sel_lock);
+		msc->id = pdev->id;
+		msc->pdev = pdev;
+		INIT_LIST_HEAD_RCU(&msc->all_msc_list);
+		INIT_LIST_HEAD_RCU(&msc->ris);

drivers/resctrl/mpam_devices.c line 116

Quoted 10 lines
drivers/resctrl/mpam_devices.c new file +10 -0
+
+		err = update_msc_accessibility(msc);
+		if (err)
+			break;
+		if (cpumask_empty(&msc->accessibility)) {
+			dev_err_once(dev, "MSC is not accessible from any CPU!");
+			err = -EINVAL;
+			break;
+		}
+

This check (cpumask_empty()) would be part of update_msc_accessibility() since msc->accessibility is sorted out in that function where it should be validated.

Quoted 125 lines
drivers/resctrl/mpam_devices.c new file +64 -0
+		if (device_property_read_u32(&pdev->dev, "pcc-channel",
+					     &msc->pcc_subspace_id))
+			msc->iface = MPAM_IFACE_MMIO;
+		else
+			msc->iface = MPAM_IFACE_PCC;
+
+		if (msc->iface == MPAM_IFACE_MMIO) {
+			void __iomem *io;
+
+			io = devm_platform_get_and_ioremap_resource(pdev, 0,
+								    &msc_res);
+			if (IS_ERR(io)) {
+				dev_err_once(dev, "Failed to map MSC base address\n");
+				err = PTR_ERR(io);
+				break;
+			}
+			msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
+			msc->mapped_hwpage = io;
+		}
+
+		list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
+		platform_set_drvdata(pdev, msc);
+	} while (0);
+	mutex_unlock(&mpam_list_lock);
+
+	if (!err) {
+		/* Create RIS entries described by firmware */
+		err = acpi_mpam_parse_resources(msc, plat_data);
+	}
+
+	if (err && msc)
+		mpam_msc_drv_remove(pdev);
+
+	if (!err && atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
+		pr_info("Discovered all MSC\n");
+
+	return err;
+}
+
+static struct platform_driver mpam_msc_driver = {
+	.driver = {
+		.name = "mpam_msc",
+	},
+	.probe = mpam_msc_drv_probe,
+	.remove = mpam_msc_drv_remove,
+};
+
+static int __init mpam_msc_driver_init(void)
+{
+	if (!system_supports_mpam())
+		return -EOPNOTSUPP;
+
+	init_srcu_struct(&mpam_srcu);
+
+	fw_num_msc = acpi_mpam_count_msc();
+
+	if (fw_num_msc <= 0) {
+		pr_err("No MSC devices found in firmware\n");
+		return -EINVAL;
+	}
+
+	return platform_driver_register(&mpam_msc_driver);
+}
+subsys_initcall(mpam_msc_driver_init);
drivers/resctrl/mpam_internal.h new file +55 -0
new file mode 100644
index 000000000000..7c63d590fc98
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+// Copyright (C) 2025 Arm Ltd.
+
+#ifndef MPAM_INTERNAL_H
+#define MPAM_INTERNAL_H
+
+#include <linux/arm_mpam.h>
+#include <linux/cpumask.h>
+#include <linux/io.h>
+#include <linux/mailbox_client.h>
+#include <linux/mutex.h>
+#include <linux/resctrl.h>
+#include <linux/sizes.h>
+
+struct mpam_msc {
+	/* member of mpam_all_msc */
+	struct list_head        all_msc_list;
+
+	int			id;
+	struct platform_device *pdev;
+
+	/* Not modified after mpam_is_enabled() becomes true */
+	enum mpam_msc_iface	iface;
+	u32			pcc_subspace_id;
+	struct mbox_client	pcc_cl;
+	struct pcc_mbox_chan	*pcc_chan;
+	u32			nrdy_usec;
+	cpumask_t		accessibility;
+
+	/*
+	 * probe_lock is only taken during discovery. After discovery these
+	 * properties become read-only and the lists are protected by SRCU.
+	 */
+	struct mutex		probe_lock;
+	unsigned long		ris_idxs;
+	u32			ris_max;
+
+	/* mpam_msc_ris of this component */
+	struct list_head	ris;
+
+	/*
+	 * part_sel_lock protects access to the MSC hardware registers that are
+	 * affected by MPAMCFG_PART_SEL. (including the ID registers that vary
+	 * by RIS).
+	 * If needed, take msc->probe_lock first.
+	 */
+	struct mutex		part_sel_lock;
+
+	/*
+	 * mon_sel_lock protects access to the MSC hardware registers that are
+	 * affected by MPAMCFG_MON_SEL.
+	 * If needed, take msc->probe_lock first.
+	 */
+	struct mutex		outer_mon_sel_lock;
+	raw_spinlock_t		inner_mon_sel_lock;

drivers/resctrl/mpam_internal.h line 65

Quoted 10 lines
drivers/resctrl/mpam_internal.h new file +10 -0
+	unsigned long		inner_mon_sel_flags;
+
+	void __iomem		*mapped_hwpage;
+	size_t			mapped_hwpage_sz;
+};
+
+int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
+				   cpumask_t *affinity);
+
+#endif /* MPAM_INTERNAL_H */

Thanks,
Gavin

author maintainer
#60 lore raw

Re: [PATCH v2 07/29] arm_mpam: Add probe/remove for mpam msc driver and kbuild boiler plate

Hi Gavin,

Quoted 38 lines · Gavin Shan

Hi James,

Quoted 35 lines · James Morse

Probing MPAM is convoluted. MSCs that are integrated with a CPU may only be accessible from those CPUs, and they may not be online. Touching the hardware early is pointless as MPAM can't be used until the system-wide common values for num_partid and num_pmg have been discovered.

Start with driver probe/remove and mapping the MSC.

drivers/resctrl/mpam_devices.c new file +21 -0
new file mode 100644
index 000000000000..efc4738e3b4d
 
+/*
+ * An MSC can control traffic from a set of CPUs, but may only be accessible
+ * from a (hopefully wider) set of CPUs. The common reason for this is power
+ * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
+ * corresponding cache may also be powered off. By making accesses from
+ * one of those CPUs, we ensure this isn't the case.
+ */
+static int update_msc_accessibility(struct mpam_msc *msc)
+{
+    u32 affinity_id;
+    int err;
+
+    err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",
+                       &affinity_id);
+    if (err)
+        cpumask_copy(&msc->accessibility, cpu_possible_mask);
+    else
+        acpi_pptt_get_cpus_from_container(affinity_id,
+                          &msc->accessibility);
+
+    return 0;
Quoted 8 lines

drivers/resctrl/mpam_devices.c line 66

Quoted 4 lines
drivers/resctrl/mpam_devices.c new file +4 -0
+
+    return err;
+}
+

Double return here and different values have been returned. I think here we need "return err". In this case, we needn't copy @cpu_possible_mask on error because the caller mpam_msc_drv_probe() will release the MSC instance.

This was the botched removal of the DT support. I'm surprised the compiler is so forgiving. (already pointed out and already fixed)

Quoted 29 lines
Quoted 29 lines
drivers/resctrl/mpam_devices.c new file +29 -0
+static int mpam_msc_drv_probe(struct platform_device *pdev)
+{
+    int err;
+    struct mpam_msc *msc;
+    struct resource *msc_res;
+    struct device *dev = &pdev->dev;
+    void *plat_data = pdev->dev.platform_data;
+
+    mutex_lock(&mpam_list_lock);
+    do {
+        msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
+        if (!msc) {
+            err = -ENOMEM;
+            break;
+        }
+
+        mutex_init(&msc->probe_lock);
+        mutex_init(&msc->part_sel_lock);
+        mutex_init(&msc->outer_mon_sel_lock);
+        raw_spin_lock_init(&msc->inner_mon_sel_lock);
+        msc->id = pdev->id;
+        msc->pdev = pdev;
+        INIT_LIST_HEAD_RCU(&msc->all_msc_list);
+        INIT_LIST_HEAD_RCU(&msc->ris);
+
+        err = update_msc_accessibility(msc);
+        if (err)
+            break;
+        if (cpumask_empty(&msc->accessibility)) {
Quoted 8 lines
Quoted 5 lines

+            dev_err_once(dev, "MSC is not accessible from any CPU!"); +            err = -EINVAL;
+            break;
+        }
+

This check (cpumask_empty()) would be part of update_msc_accessibility() since msc->accessibility is sorted out in that function where it should be validated.

Could be - but isn't. This is because with the DT support in update_msc_accessibility() that function is more complex, and its simpler to get the caller to check things like this.

Even if no-one ever gets DT support upstream, I don't think this matters.

Quoted 30 lines
Quoted 30 lines

+        if (device_property_read_u32(&pdev->dev, "pcc-channel", +                         &msc->pcc_subspace_id))
+            msc->iface = MPAM_IFACE_MMIO;
+        else
+            msc->iface = MPAM_IFACE_PCC;

drivers/resctrl/mpam_devices.c new file +25 -0
+
+        if (msc->iface == MPAM_IFACE_MMIO) {
+            void __iomem *io;
+
+            io = devm_platform_get_and_ioremap_resource(pdev, 0,
+                                    &msc_res);
+            if (IS_ERR(io)) {
+                dev_err_once(dev, "Failed to map MSC base address\n");
+                err = PTR_ERR(io);
+                break;
+            }
+            msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
+            msc->mapped_hwpage = io;
+        }
+
+        list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
+        platform_set_drvdata(pdev, msc);
+    } while (0);
+    mutex_unlock(&mpam_list_lock);
+
+    if (!err) {
+        /* Create RIS entries described by firmware */
+        err = acpi_mpam_parse_resources(msc, plat_data);
+    }
+
Quoted 8 lines

drivers/resctrl/mpam_devices.c line 154

Quoted 8 lines

+    if (err && msc)
+        mpam_msc_drv_remove(pdev);

drivers/resctrl/mpam_devices.c new file +6 -0
+
+    if (!err && atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
+        pr_info("Discovered all MSC\n");
+
+    return err;
+}

Thanks,

James

author maintainer
#61 lore raw

PATCH v2 8/29 arm_mpam: Add the class and component structures for firmware described ris

In reply to James Morse

An MSC is a container of resources, each identified by their RIS index. Some RIS are described by firmware to provide their position in the system. Others are discovered when the driver probes the hardware.

To configure a resource it needs to be found by its class, e.g. 'L2'. There are two kinds of grouping, a class is a set of components, which are visible to user-space as there are likely to be multiple instances of the L2 cache. (e.g. one per cluster or package)

Add support for creating and destroying structures to allow a hierarchy of resources to be created.

---
Changes since v1:
* Fixed a comp/vmsc typo.
* Removed duplicate description from the commit message.
* Moved parenthesis in the add_to_garbage() macro.
* Check for out of range ris_idx when creating ris.
* Removed GFP as probe_lock is no longer a spin lock.
* Removed alloc flag as ended up searching the lists itself.
* Added a comment about affinity masks not overlapping.

Changes since RFC:
* removed a pr_err() debug message that crept in.

---
 drivers/resctrl/mpam_devices.c  | 406 +++++++++++++++++++++++++++++++-
 drivers/resctrl/mpam_internal.h |  90 +++++++
 include/linux/arm_mpam.h        |   8 +-
 3 files changed, 493 insertions(+), 11 deletions(-)
drivers/resctrl/mpam_devices.c +401 -5
index efc4738e3b4d..c7f4981b3545 100644
@@ -18,7 +18,6 @@
 #include <linux/printk.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
-#include <linux/srcu.h>
 #include <linux/types.h>
 
 #include "mpam_internal.h"
@@ -31,7 +30,7 @@
 static DEFINE_MUTEX(mpam_list_lock);
 static LIST_HEAD(mpam_all_msc);
 
-static struct srcu_struct mpam_srcu;
+struct srcu_struct mpam_srcu;
 
 /*
  * Number of MSCs that have been probed. Once all MSC have been probed MPAM
@@ -39,6 +38,402 @@ static struct srcu_struct mpam_srcu;
  */
 static atomic_t mpam_num_msc;
 
+/*
+ * An MSC is a physical container for controls and monitors, each identified by
+ * their RIS index. These share a base-address, interrupts and some MMIO
+ * registers. A vMSC is a virtual container for RIS in an MSC that control or
+ * monitor the same thing. Members of a vMSC are all RIS in the same MSC, but
+ * not all RIS in an MSC share a vMSC.
+ * Components are a group of vMSC that control or monitor the same thing but
+ * are from different MSC, so have different base-address, interrupts etc.
+ * Classes are the set components of the same type.
+ *
+ * The features of a vMSC is the union of the RIS it contains.
+ * The features of a Class and Component are the common subset of the vMSC
+ * they contain.
+ *
+ * e.g. The system cache may have bandwidth controls on multiple interfaces,
+ * for regulating traffic from devices independently of traffic from CPUs.
+ * If these are two RIS in one MSC, they will be treated as controlling
+ * different things, and will not share a vMSC/component/class.
+ *
+ * e.g. The L2 may have one MSC and two RIS, one for cache-controls another
+ * for bandwidth. These two RIS are members of the same vMSC.
+ *
+ * e.g. The set of RIS that make up the L2 are grouped as a component. These
+ * are sometimes termed slices. They should be configured the same, as if there
+ * were only one.
+ *
+ * e.g. The SoC probably has more than one L2, each attached to a distinct set
+ * of CPUs. All the L2 components are grouped as a class.
+ *
+ * When creating an MSC, struct mpam_msc is added to the all mpam_all_msc list,
+ * then linked via struct mpam_ris to a vmsc, component and class.
+ * The same MSC may exist under different class->component->vmsc paths, but the
+ * RIS index will be unique.
+ */
+LIST_HEAD(mpam_classes);
+
+/* List of all objects that can be free()d after synchronise_srcu() */
+static LLIST_HEAD(mpam_garbage);
+
+#define init_garbage(x)	init_llist_node(&(x)->garbage.llist)
+
+static struct mpam_vmsc *
+mpam_vmsc_alloc(struct mpam_component *comp, struct mpam_msc *msc)
+{
+	struct mpam_vmsc *vmsc;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	vmsc = kzalloc(sizeof(*vmsc), GFP_KERNEL);
+	if (!vmsc)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(vmsc);
+
+	INIT_LIST_HEAD_RCU(&vmsc->ris);
+	INIT_LIST_HEAD_RCU(&vmsc->comp_list);
+	vmsc->comp = comp;
+	vmsc->msc = msc;
+
+	list_add_rcu(&vmsc->comp_list, &comp->vmsc);
+
+	return vmsc;
+}
+
+static struct mpam_vmsc *mpam_vmsc_get(struct mpam_component *comp,
+				       struct mpam_msc *msc)
+{
+	struct mpam_vmsc *vmsc;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
+		if (vmsc->msc->id == msc->id)
+			return vmsc;
+	}
+
+	return mpam_vmsc_alloc(comp, msc);
+}
+
+static struct mpam_component *
+mpam_component_alloc(struct mpam_class *class, int id)
+{
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	comp = kzalloc(sizeof(*comp), GFP_KERNEL);
+	if (!comp)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(comp);
+
+	comp->comp_id = id;
+	INIT_LIST_HEAD_RCU(&comp->vmsc);
+	/* affinity is updated when ris are added */
+	INIT_LIST_HEAD_RCU(&comp->class_list);
+	comp->class = class;
+
+	list_add_rcu(&comp->class_list, &class->components);
+
+	return comp;
+}
+
+static struct mpam_component *
+mpam_component_get(struct mpam_class *class, int id)
+{
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(comp, &class->components, class_list) {
+		if (comp->comp_id == id)
+			return comp;
+	}
+
+	return mpam_component_alloc(class, id);
+}
+
+static struct mpam_class *
+mpam_class_alloc(u8 level_idx, enum mpam_class_types type)
+{
+	struct mpam_class *class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	class = kzalloc(sizeof(*class), GFP_KERNEL);
+	if (!class)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(class);
+
+	INIT_LIST_HEAD_RCU(&class->components);
+	/* affinity is updated when ris are added */
+	class->level = level_idx;
+	class->type = type;
+	INIT_LIST_HEAD_RCU(&class->classes_list);
+
+	list_add_rcu(&class->classes_list, &mpam_classes);
+
+	return class;
+}
+
+static struct mpam_class *
+mpam_class_get(u8 level_idx, enum mpam_class_types type)
+{
+	bool found = false;
+	struct mpam_class *class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		if (class->type == type && class->level == level_idx) {
+			found = true;
+			break;
+		}
+	}
+
+	if (found)
+		return class;
+
+	return mpam_class_alloc(level_idx, type);
+}
+
+#define add_to_garbage(x)				\
+do {							\
+	__typeof__(x) _x = (x);				\
+	_x->garbage.to_free = _x;			\
+	llist_add(&_x->garbage.llist, &mpam_garbage);	\
+} while (0)
+
+static void mpam_class_destroy(struct mpam_class *class)
+{
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_del_rcu(&class->classes_list);
+	add_to_garbage(class);
+}
+
+static void mpam_comp_destroy(struct mpam_component *comp)
+{
+	struct mpam_class *class = comp->class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_del_rcu(&comp->class_list);
+	add_to_garbage(comp);
+
+	if (list_empty(&class->components))
+		mpam_class_destroy(class);
+}
+
+static void mpam_vmsc_destroy(struct mpam_vmsc *vmsc)
+{
+	struct mpam_component *comp = vmsc->comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_del_rcu(&vmsc->comp_list);
+	add_to_garbage(vmsc);
+
+	if (list_empty(&comp->vmsc))
+		mpam_comp_destroy(comp);
+}
+
+static void mpam_ris_destroy(struct mpam_msc_ris *ris)
+{
+	struct mpam_vmsc *vmsc = ris->vmsc;
+	struct mpam_msc *msc = vmsc->msc;
+	struct platform_device *pdev = msc->pdev;
+	struct mpam_component *comp = vmsc->comp;
+	struct mpam_class *class = comp->class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	/*
+	 * It is assumed affinities don't overlap. If they do the class becomes
+	 * unusable immediately.
+	 */
+	cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity);
+	clear_bit(ris->ris_idx, &msc->ris_idxs);
+	list_del_rcu(&ris->vmsc_list);
+	list_del_rcu(&ris->msc_list);
+	add_to_garbage(ris);
+	ris->garbage.pdev = pdev;
+
+	if (list_empty(&vmsc->ris))
+		mpam_vmsc_destroy(vmsc);
+}
+
+/*
+ * There are two ways of reaching a struct mpam_msc_ris. Via the
+ * class->component->vmsc->ris, or via the msc.
+ * When destroying the msc, the other side needs unlinking and cleaning up too.
+ */
+static void mpam_msc_destroy(struct mpam_msc *msc)
+{
+	struct platform_device *pdev = msc->pdev;
+	struct mpam_msc_ris *ris, *tmp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list)
+		mpam_ris_destroy(ris);
+
+	list_del_rcu(&msc->all_msc_list);
+	platform_set_drvdata(pdev, NULL);
+
+	add_to_garbage(msc);
+	msc->garbage.pdev = pdev;
+}
+
+static void mpam_free_garbage(void)
+{
+	struct mpam_garbage *iter, *tmp;
+	struct llist_node *to_free = llist_del_all(&mpam_garbage);
+
+	if (!to_free)
+		return;
+
+	synchronize_srcu(&mpam_srcu);
+
+	llist_for_each_entry_safe(iter, tmp, to_free, llist) {
+		if (iter->pdev)
+			devm_kfree(&iter->pdev->dev, iter->to_free);
+		else
+			kfree(iter->to_free);
+	}
+}
+
+/*
+ * The cacheinfo structures are only populated when CPUs are online.
+ */
+int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
+				   cpumask_t *affinity)
+{
+	return acpi_pptt_get_cpumask_from_cache_id(cache_id, affinity);
+}
+
+/*
+ * cpumask_of_node() only knows about online CPUs. This can't tell us whether
+ * a class is represented on all possible CPUs.
+ */
+static void get_cpumask_from_node_id(u32 node_id, cpumask_t *affinity)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu) {
+		if (node_id == cpu_to_node(cpu))
+			cpumask_set_cpu(cpu, affinity);
+	}
+}
+
+static int mpam_ris_get_affinity(struct mpam_msc *msc, cpumask_t *affinity,
+				 enum mpam_class_types type,
+				 struct mpam_class *class,
+				 struct mpam_component *comp)
+{
+	int err;
+
+	switch (type) {
+	case MPAM_CLASS_CACHE:
+		err = mpam_get_cpumask_from_cache_id(comp->comp_id, class->level,
+						     affinity);
+		if (err)
+			return err;
+
+		if (cpumask_empty(affinity))
+			pr_warn_once("%s no CPUs associated with cache node",
+				     dev_name(&msc->pdev->dev));
+
+		break;
+	case MPAM_CLASS_MEMORY:
+		get_cpumask_from_node_id(comp->comp_id, affinity);
+		/* affinity may be empty for CPU-less memory nodes */
+		break;
+	case MPAM_CLASS_UNKNOWN:
+		return 0;
+	}
+
+	cpumask_and(affinity, affinity, &msc->accessibility);
+
+	return 0;
+}
+
+static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
+				  enum mpam_class_types type, u8 class_id,
+				  int component_id)
+{
+	int err;
+	struct mpam_vmsc *vmsc;
+	struct mpam_msc_ris *ris;
+	struct mpam_class *class;
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
+		return -EINVAL;
+
+	if (test_and_set_bit(ris_idx, &msc->ris_idxs))
+		return -EBUSY;
+
+	ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL);
+	if (!ris)
+		return -ENOMEM;
+	init_garbage(ris);
+
+	class = mpam_class_get(class_id, type);
+	if (IS_ERR(class))
+		return PTR_ERR(class);
+
+	comp = mpam_component_get(class, component_id);
+	if (IS_ERR(comp)) {
+		if (list_empty(&class->components))
+			mpam_class_destroy(class);
+		return PTR_ERR(comp);
+	}
+
+	vmsc = mpam_vmsc_get(comp, msc);
+	if (IS_ERR(vmsc)) {
+		if (list_empty(&comp->vmsc))
+			mpam_comp_destroy(comp);
+		return PTR_ERR(vmsc);
+	}
+
+	err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
+	if (err) {
+		if (list_empty(&vmsc->ris))
+			mpam_vmsc_destroy(vmsc);
+		return err;
+	}
+
+	ris->ris_idx = ris_idx;
+	INIT_LIST_HEAD_RCU(&ris->vmsc_list);
+	ris->vmsc = vmsc;
+
+	cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
+	list_add_rcu(&ris->vmsc_list, &vmsc->ris);
+
+	return 0;
+}
+
+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
+		    enum mpam_class_types type, u8 class_id, int component_id)
+{
+	int err;
+
+	mutex_lock(&mpam_list_lock);
+	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
+				     component_id);
+	mutex_unlock(&mpam_list_lock);
+	if (err)
+		mpam_free_garbage();
+
+	return err;
+}
+
 /*
  * An MSC can control traffic from a set of CPUs, but may only be accessible
  * from a (hopefully wider) set of CPUs. The common reason for this is power
@@ -74,10 +469,10 @@ static void mpam_msc_drv_remove(struct platform_device *pdev)
 		return;
 
 	mutex_lock(&mpam_list_lock);
-	platform_set_drvdata(pdev, NULL);
-	list_del_rcu(&msc->all_msc_list);
-	synchronize_srcu(&mpam_srcu);
+	mpam_msc_destroy(msc);
 	mutex_unlock(&mpam_list_lock);
+
+	mpam_free_garbage();
 }
 
 static int mpam_msc_drv_probe(struct platform_device *pdev)
@@ -95,6 +490,7 @@ static int mpam_msc_drv_probe(struct platform_device *pdev)
 			err = -ENOMEM;
 			break;
 		}
+		init_garbage(msc);
 
 		mutex_init(&msc->probe_lock);
 		mutex_init(&msc->part_sel_lock);
drivers/resctrl/mpam_internal.h +90 -0

108 lines not on this page · raw message

include/linux/arm_mpam.h +2 -6
index 3d6c39c667c3..3206f5ddc147 100644
@@ -38,11 +38,7 @@ static inline int acpi_mpam_parse_resources(struct mpam_msc *msc,
 static inline int acpi_mpam_count_msc(void) { return -EINVAL; }
 #endif
 
-static inline int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
-				  enum mpam_class_types type, u8 class_id,
-				  int component_id)
-{
-	return -EINVAL;
-}
+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
+		    enum mpam_class_types type, u8 class_id, int component_id);
 
 #endif /* __LINUX_ARM_MPAM_H */
Signature · 1 line
2.39.5
#62 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

Quoted 9 lines · On Wed, 10 Sep 2025 20:42:48 +0000 James Morse <[email protected]> wrote:

An MSC is a container of resources, each identified by their RIS index. Some RIS are described by firmware to provide their position in the system. Others are discovered when the driver probes the hardware.

To configure a resource it needs to be found by its class, e.g. 'L2'. There are two kinds of grouping, a class is a set of components, which are visible to user-space as there are likely to be multiple instances of the L2 cache. (e.g. one per cluster or package)

Quoted 5 lines

Add support for creating and destroying structures to allow a hierarchy of resources to be created.

Various minor things inline.

Biggest is I think maybe just moving to explicit reference counts rather than using the empty list for that might end up easier to read. Mostly because everyone knows what a kref_put() is about.

Obviously a bit pointless in practice, but I prefer not to think too hard.

Quoted 23 lines

---
Changes since v1:
* Fixed a comp/vmsc typo.
* Removed duplicate description from the commit message.
* Moved parenthesis in the add_to_garbage() macro.
* Check for out of range ris_idx when creating ris.
* Removed GFP as probe_lock is no longer a spin lock.
* Removed alloc flag as ended up searching the lists itself.
* Added a comment about affinity masks not overlapping.

Changes since RFC:
* removed a pr_err() debug message that crept in.

---
 drivers/resctrl/mpam_devices.c  | 406 +++++++++++++++++++++++++++++++-
 drivers/resctrl/mpam_internal.h |  90 +++++++
 include/linux/arm_mpam.h        |   8 +-
 3 files changed, 493 insertions(+), 11 deletions(-)
drivers/resctrl/mpam_devices.c +0 -0
index efc4738e3b4d..c7f4981b3545 100644

drivers/resctrl/mpam_devices.c line 21

Quoted 4 lines
drivers/resctrl/mpam_devices.c +0 -1
@@ -18,7 +18,6 @@
 #include <linux/printk.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
-#include <linux/srcu.h>

Why does this include no longer make sense?

Quoted 9 lines
drivers/resctrl/mpam_devices.c +1 -1
 #include <linux/types.h>
 
 #include "mpam_internal.h"
@@ -31,7 +30,7 @@
 static DEFINE_MUTEX(mpam_list_lock);
 static LIST_HEAD(mpam_all_msc);
 
-static struct srcu_struct mpam_srcu;
+struct srcu_struct mpam_srcu;

...

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 80

Quoted 4 lines
drivers/resctrl/mpam_devices.c +4 -0
+/* List of all objects that can be free()d after synchronise_srcu() */
+static LLIST_HEAD(mpam_garbage);
+
+#define init_garbage(x)	init_llist_node(&(x)->garbage.llist)

Whilst this obviously works, I'd rather pass garbage to init_garbage instead of the containing structure (where type varies)

Quoted 45 lines
drivers/resctrl/mpam_devices.c +43 -0
+
+static struct mpam_vmsc *
+mpam_vmsc_alloc(struct mpam_component *comp, struct mpam_msc *msc)
+{
+	struct mpam_vmsc *vmsc;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	vmsc = kzalloc(sizeof(*vmsc), GFP_KERNEL);
+	if (!vmsc)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(vmsc);
+
+	INIT_LIST_HEAD_RCU(&vmsc->ris);
+	INIT_LIST_HEAD_RCU(&vmsc->comp_list);
+	vmsc->comp = comp;
+	vmsc->msc = msc;
+
+	list_add_rcu(&vmsc->comp_list, &comp->vmsc);
+
+	return vmsc;
+}
 
+static struct mpam_component *
+mpam_component_get(struct mpam_class *class, int id)
+{
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(comp, &class->components, class_list) {
+		if (comp->comp_id == id)
+			return comp;
+	}
+
+	return mpam_component_alloc(class, id);
+}
 
+static struct mpam_class *
+mpam_class_get(u8 level_idx, enum mpam_class_types type)
+{
+	bool found = false;
+	struct mpam_class *class;
+
+	lockdep_assert_held(&mpam_list_lock);

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 196

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		if (class->type == type && class->level == level_idx) {
+			found = true;
+			break;
+		}
+	}
+
+	if (found)
+		return class;

Maybe this gets more complex later, but if it doesn't, just return class where you set found above. Matches with pattern used in mpam_component_get() above.

Quoted 18 lines
drivers/resctrl/mpam_devices.c +16 -0
+
+	return mpam_class_alloc(level_idx, type);
+}
 
 
+static void mpam_ris_destroy(struct mpam_msc_ris *ris)
+{
+	struct mpam_vmsc *vmsc = ris->vmsc;
+	struct mpam_msc *msc = vmsc->msc;
+	struct platform_device *pdev = msc->pdev;
+	struct mpam_component *comp = vmsc->comp;
+	struct mpam_class *class = comp->class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	/*
+	 * It is assumed affinities don't overlap. If they do the class becomes
+	 * unusable immediately.

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 264

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+	 */
+	cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity);
+	clear_bit(ris->ris_idx, &msc->ris_idxs);
+	list_del_rcu(&ris->vmsc_list);
+	list_del_rcu(&ris->msc_list);
+	add_to_garbage(ris);
+	ris->garbage.pdev = pdev;
+
+	if (list_empty(&vmsc->ris))

See below. I think it is worth using an explicit reference count even though that will only reach zero when the list is empty.

Quoted 25 lines
drivers/resctrl/mpam_devices.c +23 -0
+		mpam_vmsc_destroy(vmsc);
+}
 
 
+static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
+				  enum mpam_class_types type, u8 class_id,
+				  int component_id)
+{
+	int err;
+	struct mpam_vmsc *vmsc;
+	struct mpam_msc_ris *ris;
+	struct mpam_class *class;
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
+		return -EINVAL;
+
+	if (test_and_set_bit(ris_idx, &msc->ris_idxs))
+		return -EBUSY;
+
+	ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL);
+	if (!ris)
+		return -ENOMEM;

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 393

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+	init_garbage(ris);
+
+	class = mpam_class_get(class_id, type);
+	if (IS_ERR(class))
+		return PTR_ERR(class);
+
+	comp = mpam_component_get(class, component_id);
+	if (IS_ERR(comp)) {
+		if (list_empty(&class->components))
+			mpam_class_destroy(class);

Maybe just reference count the classes with a kref and do a put here?

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 400

Quoted 7 lines
drivers/resctrl/mpam_devices.c +7 -0
+		return PTR_ERR(comp);
+	}
+
+	vmsc = mpam_vmsc_get(comp, msc);
+	if (IS_ERR(vmsc)) {
+		if (list_empty(&comp->vmsc))
+			mpam_comp_destroy(comp);

Similar to classes I wonder if simple reference counting is cleaner.

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 406

Quoted 6 lines
drivers/resctrl/mpam_devices.c +6 -0
+		return PTR_ERR(vmsc);
+	}
+
+	err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
+	if (err) {
+		if (list_empty(&vmsc->ris))

and here as well.

Quoted 19 lines
drivers/resctrl/mpam_devices.c +14 -0
+			mpam_vmsc_destroy(vmsc);
+		return err;
+	}
+
+	ris->ris_idx = ris_idx;
+	INIT_LIST_HEAD_RCU(&ris->vmsc_list);
+	ris->vmsc = vmsc;
+
+	cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
+	list_add_rcu(&ris->vmsc_list, &vmsc->ris);
+
+	return 0;
+}
 
 /*
  * An MSC can control traffic from a set of CPUs, but may only be accessible
  * from a (hopefully wider) set of CPUs. The common reason for this is power

drivers/resctrl/mpam_devices.c static void mpam_msc_drv_remove(struct platform_device *pdev) · line 472

Quoted 7 lines
drivers/resctrl/mpam_devices.c +1 -3
@@ -74,10 +469,10 @@ static void mpam_msc_drv_remove(struct platform_device *pdev)
 		return;
 
 	mutex_lock(&mpam_list_lock);
-	platform_set_drvdata(pdev, NULL);
-	list_del_rcu(&msc->all_msc_list);
-	synchronize_srcu(&mpam_srcu);
+	mpam_msc_destroy(msc);

I'd suggest introducing mpam_msc_destroy() in the earlier patch. Doesn't make a huge difference but 2 out of 3 things removed here would be untouched if you do that.

Quoted 4 lines
drivers/resctrl/mpam_devices.c +2 -0
 	mutex_unlock(&mpam_list_lock);
+
+	mpam_free_garbage();
 }
author maintainer
#63 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

Hi Jonathan,

Quoted 15 lines · Jonathan Cameron
Quoted 11 lines · On Wed, 10 Sep 2025 20:42:48 +0000 James Morse <[email protected]> wrote:

An MSC is a container of resources, each identified by their RIS index. Some RIS are described by firmware to provide their position in the system. Others are discovered when the driver probes the hardware.

To configure a resource it needs to be found by its class, e.g. 'L2'. There are two kinds of grouping, a class is a set of components, which are visible to user-space as there are likely to be multiple instances of the L2 cache. (e.g. one per cluster or package)

Add support for creating and destroying structures to allow a hierarchy of resources to be created.

Quoted 8 lines

Various minor things inline.

Biggest is I think maybe just moving to explicit reference counts rather than using the empty list for that might end up easier to read. Mostly because everyone knows what a kref_put() is about.

Obviously a bit pointless in practice, but I prefer not to think too hard.

I've spent a while playing with this - its the wrong shape for what is going on here.

This builds that tree structure during driver probing. (and adds 'unknown' entries to it when poking the hardware). But by the time mpam_enable() is called - its basically read-only. After that point the only 'write' that will happen is the error interrupt which just free's everything. The deferred free from SRCU makes that safe.
(some of this will be clearer when I add the block comment about the 'phases' the driver
goes through that Dave asked for)

Making it 'reference counted' instead is pointless because callers would be get/put-ing references - but the structure is basically read-only, and not going to go away while the SRCU reference is held.
One of the things reference counting breaks is the devm_kzalloc() usage - as an error from the driver probe function will free all of those regions, regardless of what the reference count says.

I'll look to rename the existing 'get' functions so folk don't immediatly think of get/put!

('find' doesn't really cut it as it does the allocation if it doesn't 'find' anything)

Quoted 11 lines

drivers/resctrl/mpam_devices.c line 21

Quoted 9 lines
drivers/resctrl/mpam_devices.c +0 -1
index efc4738e3b4d..c7f4981b3545 100644
@@ -18,7 +18,6 @@
 #include <linux/printk.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
-#include <linux/srcu.h>

Why does this include no longer make sense?

It gets moved to mpam_internal.h because of the srcu_struct declaration that is needed for callers in the resctrl code to walk the classes list.

I can add it to the mpam_internal.h header from the beginning.

Quoted 9 lines
Quoted 6 lines
drivers/resctrl/mpam_devices.c +1 -1
@@ -31,7 +30,7 @@
 static DEFINE_MUTEX(mpam_list_lock);
 static LIST_HEAD(mpam_all_msc);
 
-static struct srcu_struct mpam_srcu;
+struct srcu_struct mpam_srcu;

...

Quoted 7 lines

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 80

Quoted 4 lines
drivers/resctrl/mpam_devices.c +4 -0
+/* List of all objects that can be free()d after synchronise_srcu() */
+static LLIST_HEAD(mpam_garbage);
+
+#define init_garbage(x)	init_llist_node(&(x)->garbage.llist)

Whilst this obviously works, I'd rather pass garbage to init_garbage instead of the containing structure (where type varies)

Sure,

Quoted 27 lines
Quoted 27 lines
drivers/resctrl/mpam_devices.c +26 -0
+static struct mpam_component *
+mpam_component_get(struct mpam_class *class, int id)
+{
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(comp, &class->components, class_list) {
+		if (comp->comp_id == id)
+			return comp;
+	}
+
+	return mpam_component_alloc(class, id);
+}
 
+static struct mpam_class *
+mpam_class_get(u8 level_idx, enum mpam_class_types type)
+{
+	bool found = false;
+	struct mpam_class *class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		if (class->type == type && class->level == level_idx) {
+			found = true;
+			break;
Quoted 8 lines

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 196

Quoted 5 lines
drivers/resctrl/mpam_devices.c +5 -0
+		}
+	}
+
+	if (found)
+		return class;

Maybe this gets more complex later, but if it doesn't, just return class where you set found above. Matches with pattern used in mpam_component_get() above.

Yup, this is a relic of more complex locking.

Quoted 23 lines
Quoted 23 lines
drivers/resctrl/mpam_devices.c +21 -0
+
+	return mpam_class_alloc(level_idx, type);
+}
 
 
+static void mpam_ris_destroy(struct mpam_msc_ris *ris)
+{
+	struct mpam_vmsc *vmsc = ris->vmsc;
+	struct mpam_msc *msc = vmsc->msc;
+	struct platform_device *pdev = msc->pdev;
+	struct mpam_component *comp = vmsc->comp;
+	struct mpam_class *class = comp->class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	/*
+	 * It is assumed affinities don't overlap. If they do the class becomes
+	 * unusable immediately.
+	 */
+	cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity);
+	clear_bit(ris->ris_idx, &msc->ris_idxs);
+	list_del_rcu(&ris->vmsc_list);
Quoted 8 lines

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 264

Quoted 5 lines
drivers/resctrl/mpam_devices.c +5 -0
+	list_del_rcu(&ris->msc_list);
+	add_to_garbage(ris);
+	ris->garbage.pdev = pdev;
+
+	if (list_empty(&vmsc->ris))

See below. I think it is worth using an explicit reference count even though that will only reach zero when the list is empty.

By the time eveything is probed, its an almost entirely read-only structure. The only writer will free absolutely everything. Changing this to structure-by-structure reference counting will cause busy-work for readers, who are already protected by SRCU.

Quoted 38 lines
Quoted 25 lines
drivers/resctrl/mpam_devices.c +23 -0
+		mpam_vmsc_destroy(vmsc);
+}
 
 
+static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
+				  enum mpam_class_types type, u8 class_id,
+				  int component_id)
+{
+	int err;
+	struct mpam_vmsc *vmsc;
+	struct mpam_msc_ris *ris;
+	struct mpam_class *class;
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
+		return -EINVAL;
+
+	if (test_and_set_bit(ris_idx, &msc->ris_idxs))
+		return -EBUSY;
+
+	ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL);
+	if (!ris)
+		return -ENOMEM;

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 393

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+	init_garbage(ris);
+
+	class = mpam_class_get(class_id, type);
+	if (IS_ERR(class))
+		return PTR_ERR(class);
+
+	comp = mpam_component_get(class, component_id);
+	if (IS_ERR(comp)) {
+		if (list_empty(&class->components))
+			mpam_class_destroy(class);

Maybe just reference count the classes with a kref and do a put here?

Quoted 9 lines

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 400

Quoted 7 lines
drivers/resctrl/mpam_devices.c +7 -0
+		return PTR_ERR(comp);
+	}
+
+	vmsc = mpam_vmsc_get(comp, msc);
+	if (IS_ERR(vmsc)) {
+		if (list_empty(&comp->vmsc))
+			mpam_comp_destroy(comp);

Similar to classes I wonder if simple reference counting is cleaner.

(I spent a few days on it - its prettier, but more work for things like the resctrl code
to get/put references when SRCU already has the write side covered)

Quoted 30 lines

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 406

Quoted 6 lines
drivers/resctrl/mpam_devices.c +6 -0
+		return PTR_ERR(vmsc);
+	}
+
+	err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
+	if (err) {
+		if (list_empty(&vmsc->ris))

and here as well.

Quoted 21 lines
drivers/resctrl/mpam_devices.c +14 -0
+			mpam_vmsc_destroy(vmsc);
+		return err;
+	}
+
+	ris->ris_idx = ris_idx;
+	INIT_LIST_HEAD_RCU(&ris->vmsc_list);
+	ris->vmsc = vmsc;
+
+	cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
+	list_add_rcu(&ris->vmsc_list, &vmsc->ris);
+
+	return 0;
+}
 
 /*
  * An MSC can control traffic from a set of CPUs, but may only be accessible
  * from a (hopefully wider) set of CPUs. The common reason for this is power
@@ -74,10 +469,10 @@ static void mpam_msc_drv_remove(struct platform_device *pdev)
 		return;
 
Quoted 8 lines

drivers/resctrl/mpam_devices.c static void mpam_msc_drv_remove(struct platform_device *pdev) · line 472

Quoted 5 lines
drivers/resctrl/mpam_devices.c +1 -3
 	mutex_lock(&mpam_list_lock);
-	platform_set_drvdata(pdev, NULL);
-	list_del_rcu(&msc->all_msc_list);
-	synchronize_srcu(&mpam_srcu);
+	mpam_msc_destroy(msc);

I'd suggest introducing mpam_msc_destroy() in the earlier patch. Doesn't make a huge difference but 2 out of 3 things removed here would be untouched if you do that.

Sure. This is remnant of a patch from Carl that moved all this around.

Thanks,

James

#64 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

In reply to James Morse

…

Quoted 1 line

+++ b/drivers/resctrl/mpam_devices.c

…

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 430

Quoted 9 lines
Quoted 1 line

+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,

drivers/resctrl/mpam_devices.c +8 -0
+		    enum mpam_class_types type, u8 class_id, int component_id)
+{
+	int err;
+
+	mutex_lock(&mpam_list_lock);
+	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
+				     component_id);
+	mutex_unlock(&mpam_list_lock);

…

Under which circumstances would you become interested to apply a statement like “guard(mutex)(&mpam_list_lock);”?
https://elixir.bootlin.com/linux/v6.17-rc5/source/include/linux/mutex.h#L228

Regards,
Markus

author maintainer
#65 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

Hi Markus,

Quoted 14 lines · Markus Elfring

…

Quoted 1 line

+++ b/drivers/resctrl/mpam_devices.c

…

Quoted 9 lines
Quoted 1 line

+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,

drivers/resctrl/mpam_devices.c +8 -0
+		    enum mpam_class_types type, u8 class_id, int component_id)
+{
+	int err;
+
+	mutex_lock(&mpam_list_lock);
+	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
+				     component_id);
+	mutex_unlock(&mpam_list_lock);

…

Quoted 3 lines

Under which circumstances would you become interested to apply a statement like “guard(mutex)(&mpam_list_lock);”?
https://elixir.bootlin.com/linux/v6.17-rc5/source/include/linux/mutex.h#L228

None! The bit of this you cut out is a call to mpam_free_garbage() which calls synchronize_srcu(). That may sleep for a while. The whole point of the deferred free-ing is it does not happen under the lock. The 'guard' magic means the compiler gets to choose when to call unlock.

Thanks,

James

#66 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

Quoted 14 lines
Quoted 14 lines

…

Quoted 1 line

+++ b/drivers/resctrl/mpam_devices.c

…

Quoted 9 lines
Quoted 1 line

+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,

drivers/resctrl/mpam_devices.c +8 -0
+		    enum mpam_class_types type, u8 class_id, int component_id)
+{
+	int err;
+
+	mutex_lock(&mpam_list_lock);
+	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
+				     component_id);
+	mutex_unlock(&mpam_list_lock);

…

Quoted 8 lines
Quoted 3 lines

Under which circumstances would you become interested to apply a statement like “guard(mutex)(&mpam_list_lock);”?
https://elixir.bootlin.com/linux/v6.17-rc5/source/include/linux/mutex.h#L228

None! The bit of this you cut out is a call to mpam_free_garbage() which calls synchronize_srcu(). That may sleep for a while. The whole point of the deferred free-ing is it does not happen under the lock. The 'guard' magic means the compiler gets to choose when to call unlock.

How does this feedback fit to the proposed addition of a mutex_lock()/mutex_unlock() call combination (which might be achievable also with another programming interface)?

Regards,
Markus

author maintainer
#67 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

Hi Markus,

Quoted 18 lines · Markus Elfring
Quoted 18 lines
Quoted 17 lines

…

Quoted 1 line

+++ b/drivers/resctrl/mpam_devices.c

…

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 430

Quoted 9 lines
Quoted 1 line

+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,

drivers/resctrl/mpam_devices.c +8 -0
+		    enum mpam_class_types type, u8 class_id, int component_id)
+{
+	int err;
+
+	mutex_lock(&mpam_list_lock);
+	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
+				     component_id);
+	mutex_unlock(&mpam_list_lock);

…

Under which circumstances would you become interested to apply a statement like “guard(mutex)(&mpam_list_lock);”?
https://elixir.bootlin.com/linux/v6.17-rc5/source/include/linux/mutex.h#L228

Quoted 7 lines
Quoted 4 lines

None! The bit of this you cut out is a call to mpam_free_garbage() which calls synchronize_srcu(). That may sleep for a while. The whole point of the deferred free-ing is it does not happen under the lock. The 'guard' magic means the compiler gets to choose when to call unlock.

How does this feedback fit to the proposed addition of a mutex_lock()/mutex_unlock() call combination (which might be achievable also with another programming interface)?

Right - I've muddled the horde of "must use guard srcu" with the horde of "must use guard mutex". In this case I'd still prefer we don't spuriously hold the write side lock when doing the deferred free-ing.

Thanks,

James

reviewer
#68 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

In reply to James Morse

Hi, James,

Quoted 301 lines · James Morse

An MSC is a container of resources, each identified by their RIS index. Some RIS are described by firmware to provide their position in the system. Others are discovered when the driver probes the hardware.

To configure a resource it needs to be found by its class, e.g. 'L2'. There are two kinds of grouping, a class is a set of components, which are visible to user-space as there are likely to be multiple instances of the L2 cache. (e.g. one per cluster or package)

Add support for creating and destroying structures to allow a hierarchy of resources to be created.

---
Changes since v1:
* Fixed a comp/vmsc typo.
* Removed duplicate description from the commit message.
* Moved parenthesis in the add_to_garbage() macro.
* Check for out of range ris_idx when creating ris.
* Removed GFP as probe_lock is no longer a spin lock.
* Removed alloc flag as ended up searching the lists itself.
* Added a comment about affinity masks not overlapping.

Changes since RFC:
* removed a pr_err() debug message that crept in.

---
  drivers/resctrl/mpam_devices.c  | 406 +++++++++++++++++++++++++++++++-
  drivers/resctrl/mpam_internal.h |  90 +++++++
  include/linux/arm_mpam.h        |   8 +-
  3 files changed, 493 insertions(+), 11 deletions(-)
drivers/resctrl/mpam_devices.c +245 -2
index efc4738e3b4d..c7f4981b3545 100644
@@ -18,7 +18,6 @@
  #include <linux/printk.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/srcu.h>
  #include <linux/types.h>
 
  #include "mpam_internal.h"
@@ -31,7 +30,7 @@
  static DEFINE_MUTEX(mpam_list_lock);
  static LIST_HEAD(mpam_all_msc);
 
-static struct srcu_struct mpam_srcu;
+struct srcu_struct mpam_srcu;
 
  /*
   * Number of MSCs that have been probed. Once all MSC have been probed MPAM
@@ -39,6 +38,402 @@ static struct srcu_struct mpam_srcu;
   */
  static atomic_t mpam_num_msc;
 
+/*
+ * An MSC is a physical container for controls and monitors, each identified by
+ * their RIS index. These share a base-address, interrupts and some MMIO
+ * registers. A vMSC is a virtual container for RIS in an MSC that control or
+ * monitor the same thing. Members of a vMSC are all RIS in the same MSC, but
+ * not all RIS in an MSC share a vMSC.
+ * Components are a group of vMSC that control or monitor the same thing but
+ * are from different MSC, so have different base-address, interrupts etc.
+ * Classes are the set components of the same type.
+ *
+ * The features of a vMSC is the union of the RIS it contains.
+ * The features of a Class and Component are the common subset of the vMSC
+ * they contain.
+ *
+ * e.g. The system cache may have bandwidth controls on multiple interfaces,
+ * for regulating traffic from devices independently of traffic from CPUs.
+ * If these are two RIS in one MSC, they will be treated as controlling
+ * different things, and will not share a vMSC/component/class.
+ *
+ * e.g. The L2 may have one MSC and two RIS, one for cache-controls another
+ * for bandwidth. These two RIS are members of the same vMSC.
+ *
+ * e.g. The set of RIS that make up the L2 are grouped as a component. These
+ * are sometimes termed slices. They should be configured the same, as if there
+ * were only one.
+ *
+ * e.g. The SoC probably has more than one L2, each attached to a distinct set
+ * of CPUs. All the L2 components are grouped as a class.
+ *
+ * When creating an MSC, struct mpam_msc is added to the all mpam_all_msc list,
+ * then linked via struct mpam_ris to a vmsc, component and class.
+ * The same MSC may exist under different class->component->vmsc paths, but the
+ * RIS index will be unique.
+ */
+LIST_HEAD(mpam_classes);
+
+/* List of all objects that can be free()d after synchronise_srcu() */
+static LLIST_HEAD(mpam_garbage);
+
+#define init_garbage(x)	init_llist_node(&(x)->garbage.llist)
+
+static struct mpam_vmsc *
+mpam_vmsc_alloc(struct mpam_component *comp, struct mpam_msc *msc)
+{
+	struct mpam_vmsc *vmsc;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	vmsc = kzalloc(sizeof(*vmsc), GFP_KERNEL);
+	if (!vmsc)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(vmsc);
+
+	INIT_LIST_HEAD_RCU(&vmsc->ris);
+	INIT_LIST_HEAD_RCU(&vmsc->comp_list);
+	vmsc->comp = comp;
+	vmsc->msc = msc;
+
+	list_add_rcu(&vmsc->comp_list, &comp->vmsc);
+
+	return vmsc;
+}
+
+static struct mpam_vmsc *mpam_vmsc_get(struct mpam_component *comp,
+				       struct mpam_msc *msc)
+{
+	struct mpam_vmsc *vmsc;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
+		if (vmsc->msc->id == msc->id)
+			return vmsc;
+	}
+
+	return mpam_vmsc_alloc(comp, msc);
+}
+
+static struct mpam_component *
+mpam_component_alloc(struct mpam_class *class, int id)
+{
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	comp = kzalloc(sizeof(*comp), GFP_KERNEL);
+	if (!comp)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(comp);
+
+	comp->comp_id = id;
+	INIT_LIST_HEAD_RCU(&comp->vmsc);
+	/* affinity is updated when ris are added */
+	INIT_LIST_HEAD_RCU(&comp->class_list);
+	comp->class = class;
+
+	list_add_rcu(&comp->class_list, &class->components);
+
+	return comp;
+}
+
+static struct mpam_component *
+mpam_component_get(struct mpam_class *class, int id)
+{
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(comp, &class->components, class_list) {
+		if (comp->comp_id == id)
+			return comp;
+	}
+
+	return mpam_component_alloc(class, id);
+}
+
+static struct mpam_class *
+mpam_class_alloc(u8 level_idx, enum mpam_class_types type)
+{
+	struct mpam_class *class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	class = kzalloc(sizeof(*class), GFP_KERNEL);
+	if (!class)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(class);
+
+	INIT_LIST_HEAD_RCU(&class->components);
+	/* affinity is updated when ris are added */
+	class->level = level_idx;
+	class->type = type;
+	INIT_LIST_HEAD_RCU(&class->classes_list);
+
+	list_add_rcu(&class->classes_list, &mpam_classes);
+
+	return class;
+}
+
+static struct mpam_class *
+mpam_class_get(u8 level_idx, enum mpam_class_types type)
+{
+	bool found = false;
+	struct mpam_class *class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		if (class->type == type && class->level == level_idx) {
+			found = true;
+			break;
+		}
+	}
+
+	if (found)
+		return class;
+
+	return mpam_class_alloc(level_idx, type);
+}
+
+#define add_to_garbage(x)				\
+do {							\
+	__typeof__(x) _x = (x);				\
+	_x->garbage.to_free = _x;			\
+	llist_add(&_x->garbage.llist, &mpam_garbage);	\
+} while (0)
+
+static void mpam_class_destroy(struct mpam_class *class)
+{
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_del_rcu(&class->classes_list);
+	add_to_garbage(class);
+}
+
+static void mpam_comp_destroy(struct mpam_component *comp)
+{
+	struct mpam_class *class = comp->class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_del_rcu(&comp->class_list);
+	add_to_garbage(comp);
+
+	if (list_empty(&class->components))
+		mpam_class_destroy(class);
+}
+
+static void mpam_vmsc_destroy(struct mpam_vmsc *vmsc)
+{
+	struct mpam_component *comp = vmsc->comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_del_rcu(&vmsc->comp_list);
+	add_to_garbage(vmsc);
+
+	if (list_empty(&comp->vmsc))
+		mpam_comp_destroy(comp);
+}
+
+static void mpam_ris_destroy(struct mpam_msc_ris *ris)
+{
+	struct mpam_vmsc *vmsc = ris->vmsc;
+	struct mpam_msc *msc = vmsc->msc;
+	struct platform_device *pdev = msc->pdev;
+	struct mpam_component *comp = vmsc->comp;
+	struct mpam_class *class = comp->class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	/*
+	 * It is assumed affinities don't overlap. If they do the class becomes
+	 * unusable immediately.
+	 */
+	cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity);
+	clear_bit(ris->ris_idx, &msc->ris_idxs);
+	list_del_rcu(&ris->vmsc_list);
+	list_del_rcu(&ris->msc_list);
+	add_to_garbage(ris);
+	ris->garbage.pdev = pdev;
+
+	if (list_empty(&vmsc->ris))
+		mpam_vmsc_destroy(vmsc);
+}
+
+/*
+ * There are two ways of reaching a struct mpam_msc_ris. Via the
+ * class->component->vmsc->ris, or via the msc.
+ * When destroying the msc, the other side needs unlinking and cleaning up too.
+ */
+static void mpam_msc_destroy(struct mpam_msc *msc)
+{
+	struct platform_device *pdev = msc->pdev;
+	struct mpam_msc_ris *ris, *tmp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list)
+		mpam_ris_destroy(ris);
+
+	list_del_rcu(&msc->all_msc_list);
+	platform_set_drvdata(pdev, NULL);

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 294

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+
+	add_to_garbage(msc);
+	msc->garbage.pdev = pdev;
+}
+
+static void mpam_free_garbage(void)
+{
+	struct mpam_garbage *iter, *tmp;
+	struct llist_node *to_free = llist_del_all(&mpam_garbage);
+

Should this be protected by mpam_list_lock and check if the lock is held?

+ lockdep_assert_held(&mpam_list_lock);

Multiple threads may add and free garbage in parallel. Please see later free_garbage() is not protected by any lock.

Quoted 76 lines
drivers/resctrl/mpam_devices.c +76 -0
+	if (!to_free)
+		return;
+
+	synchronize_srcu(&mpam_srcu);
+
+	llist_for_each_entry_safe(iter, tmp, to_free, llist) {
+		if (iter->pdev)
+			devm_kfree(&iter->pdev->dev, iter->to_free);
+		else
+			kfree(iter->to_free);
+	}
+}
+
+/*
+ * The cacheinfo structures are only populated when CPUs are online.
+ */
+int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
+				   cpumask_t *affinity)
+{
+	return acpi_pptt_get_cpumask_from_cache_id(cache_id, affinity);
+}
+
+/*
+ * cpumask_of_node() only knows about online CPUs. This can't tell us whether
+ * a class is represented on all possible CPUs.
+ */
+static void get_cpumask_from_node_id(u32 node_id, cpumask_t *affinity)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu) {
+		if (node_id == cpu_to_node(cpu))
+			cpumask_set_cpu(cpu, affinity);
+	}
+}
+
+static int mpam_ris_get_affinity(struct mpam_msc *msc, cpumask_t *affinity,
+				 enum mpam_class_types type,
+				 struct mpam_class *class,
+				 struct mpam_component *comp)
+{
+	int err;
+
+	switch (type) {
+	case MPAM_CLASS_CACHE:
+		err = mpam_get_cpumask_from_cache_id(comp->comp_id, class->level,
+						     affinity);
+		if (err)
+			return err;
+
+		if (cpumask_empty(affinity))
+			pr_warn_once("%s no CPUs associated with cache node",
+				     dev_name(&msc->pdev->dev));
+
+		break;
+	case MPAM_CLASS_MEMORY:
+		get_cpumask_from_node_id(comp->comp_id, affinity);
+		/* affinity may be empty for CPU-less memory nodes */
+		break;
+	case MPAM_CLASS_UNKNOWN:
+		return 0;
+	}
+
+	cpumask_and(affinity, affinity, &msc->accessibility);
+
+	return 0;
+}
+
+static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
+				  enum mpam_class_types type, u8 class_id,
+				  int component_id)
+{
+	int err;
+	struct mpam_vmsc *vmsc;
+	struct mpam_msc_ris *ris;
+	struct mpam_class *class;

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 380

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
+		return -EINVAL;
+
+	if (test_and_set_bit(ris_idx, &msc->ris_idxs))
+		return -EBUSY;
+

Should setting msc->ris_idxs bit be moved to the end of this function after all error handling paths? The reason is this bit is better to be 0 (or recovered) if any error happens. It's hard to recover it to 0 for each error handling. The easiest way is to set it at the end of the function.

Quoted 22 lines
drivers/resctrl/mpam_devices.c +22 -0
+	ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL);
+	if (!ris)
+		return -ENOMEM;
+	init_garbage(ris);
+
+	class = mpam_class_get(class_id, type);
+	if (IS_ERR(class))
+		return PTR_ERR(class);
+
+	comp = mpam_component_get(class, component_id);
+	if (IS_ERR(comp)) {
+		if (list_empty(&class->components))
+			mpam_class_destroy(class);
+		return PTR_ERR(comp);
+	}
+
+	vmsc = mpam_vmsc_get(comp, msc);
+	if (IS_ERR(vmsc)) {
+		if (list_empty(&comp->vmsc))
+			mpam_comp_destroy(comp);
+		return PTR_ERR(vmsc);
+	}

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 412

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+
+	err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
+	if (err) {
+		if (list_empty(&vmsc->ris))
+			mpam_vmsc_destroy(vmsc);
+		return err;
+	}
+
+	ris->ris_idx = ris_idx;
+	INIT_LIST_HEAD_RCU(&ris->vmsc_list);

vmsc_list will be used but not initialized. Missing INIT_LIST_HEAD_RCU(&ris->msc_list) here?

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 418

Quoted 6 lines
drivers/resctrl/mpam_devices.c +6 -0
+	ris->vmsc = vmsc;
+
+	cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
+	list_add_rcu(&ris->vmsc_list, &vmsc->ris);
+

Setting the msc->ris_idxs here is better to avoid to clear it in each error handling path.

Quoted 22 lines
drivers/resctrl/mpam_devices.c +18 -0
+	return 0;
+}
+
+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
+		    enum mpam_class_types type, u8 class_id, int component_id)
+{
+	int err;
+
+	mutex_lock(&mpam_list_lock);
+	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
+				     component_id);
+	mutex_unlock(&mpam_list_lock);
+	if (err)
+		mpam_free_garbage();
+
+	return err;
+}
+
  /*
   * An MSC can control traffic from a set of CPUs, but may only be accessible
   * from a (hopefully wider) set of CPUs. The common reason for this is power

drivers/resctrl/mpam_devices.c static void mpam_msc_drv_remove(struct platform_device *pdev) · line 475

Quoted 10 lines
drivers/resctrl/mpam_devices.c +3 -3
@@ -74,10 +469,10 @@ static void mpam_msc_drv_remove(struct platform_device *pdev)
  		return;
 
  	mutex_lock(&mpam_list_lock);
-	platform_set_drvdata(pdev, NULL);
-	list_del_rcu(&msc->all_msc_list);
-	synchronize_srcu(&mpam_srcu);
+	mpam_msc_destroy(msc);
  	mutex_unlock(&mpam_list_lock);
+
+	mpam_free_garbage();

Should mpam_free_garbage() be protected by mpam_list_lock? It may race with adding garbage. I can see other adding and freeing garbage are protected by mpam_list_lock but not this one.

Quoted 133 lines
drivers/resctrl/mpam_devices.c +1 -0
  }
 
  static int mpam_msc_drv_probe(struct platform_device *pdev)
@@ -95,6 +490,7 @@ static int mpam_msc_drv_probe(struct platform_device *pdev)
  			err = -ENOMEM;
  			break;
  		}
+		init_garbage(msc);
 
  		mutex_init(&msc->probe_lock);
  		mutex_init(&msc->part_sel_lock);
drivers/resctrl/mpam_internal.h +90 -0
index 7c63d590fc98..02e9576ece6b 100644
@@ -7,10 +7,29 @@
  #include <linux/arm_mpam.h>
  #include <linux/cpumask.h>
  #include <linux/io.h>
+#include <linux/llist.h>
  #include <linux/mailbox_client.h>
  #include <linux/mutex.h>
  #include <linux/resctrl.h>
  #include <linux/sizes.h>
+#include <linux/srcu.h>
+
+#define MPAM_MSC_MAX_NUM_RIS	16
+
+/*
+ * Structures protected by SRCU may not be freed for a surprising amount of
+ * time (especially if perf is running). To ensure the MPAM error interrupt can
+ * tear down all the structures, build a list of objects that can be gargbage
+ * collected once synchronize_srcu() has returned.
+ * If pdev is non-NULL, use devm_kfree().
+ */
+struct mpam_garbage {
+	/* member of mpam_garbage */
+	struct llist_node	llist;
+
+	void			*to_free;
+	struct platform_device	*pdev;
+};
 
  struct mpam_msc {
  	/* member of mpam_all_msc */
@@ -57,8 +76,79 @@ struct mpam_msc {
 
  	void __iomem		*mapped_hwpage;
  	size_t			mapped_hwpage_sz;
+
+	struct mpam_garbage	garbage;
  };
 
+struct mpam_class {
+	/* mpam_components in this class */
+	struct list_head	components;
+
+	cpumask_t		affinity;
+
+	u8			level;
+	enum mpam_class_types	type;
+
+	/* member of mpam_classes */
+	struct list_head	classes_list;
+
+	struct mpam_garbage	garbage;
+};
+
+struct mpam_component {
+	u32			comp_id;
+
+	/* mpam_vmsc in this component */
+	struct list_head	vmsc;
+
+	cpumask_t		affinity;
+
+	/* member of mpam_class:components */
+	struct list_head	class_list;
+
+	/* parent: */
+	struct mpam_class	*class;
+
+	struct mpam_garbage	garbage;
+};
+
+struct mpam_vmsc {
+	/* member of mpam_component:vmsc_list */
+	struct list_head	comp_list;
+
+	/* mpam_msc_ris in this dvmsc */
+	struct list_head	ris;
+
+	/* All RIS in this vMSC are members of this MSC */
+	struct mpam_msc		*msc;
+
+	/* parent: */
+	struct mpam_component	*comp;
+
+	struct mpam_garbage	garbage;
+};
+
+struct mpam_msc_ris {
+	u8			ris_idx;
+
+	cpumask_t		affinity;
+
+	/* member of mpam_vmsc:ris */
+	struct list_head	vmsc_list;
+
+	/* member of mpam_msc:ris */
+	struct list_head	msc_list;
+
+	/* parent: */
+	struct mpam_vmsc	*vmsc;
+
+	struct mpam_garbage	garbage;
+};
+
+/* List of all classes - protected by srcu*/
+extern struct srcu_struct mpam_srcu;
+extern struct list_head mpam_classes;
+
  int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
  				   cpumask_t *affinity);
 
include/linux/arm_mpam.h +0 -0
index 3d6c39c667c3..3206f5ddc147 100644
@@ -38,11 +38,7 @@ static inline int acpi_mpam_parse_resources(struct mpam_msc *msc,
  static inline int acpi_mpam_count_msc(void) { return -EINVAL; }
  #endif
 

include/linux/arm_mpam.h static inline int acpi_mpam_parse_resources(struct mpam_msc *msc, · line 44

Quoted 10 lines
include/linux/arm_mpam.h +2 -6
-static inline int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
-				  enum mpam_class_types type, u8 class_id,
-				  int component_id)
-{
-	return -EINVAL;
-}
+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
+		    enum mpam_class_types type, u8 class_id, int component_id);
 
  #endif /* __LINUX_ARM_MPAM_H */

Thanks.

-Fenghua

author maintainer
#69 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

Hi Fenghua,

Quoted 32 lines · Fenghua Yu
Quoted 18 lines · James Morse

An MSC is a container of resources, each identified by their RIS index. Some RIS are described by firmware to provide their position in the system. Others are discovered when the driver probes the hardware.

To configure a resource it needs to be found by its class, e.g. 'L2'. There are two kinds of grouping, a class is a set of components, which are visible to user-space as there are likely to be multiple instances of the L2 cache. (e.g. one per cluster or package)

Add support for creating and destroying structures to allow a hierarchy of resources to be created.

drivers/resctrl/mpam_devices.c +1 -0
index efc4738e3b4d..c7f4981b3545 100644
 
+#define add_to_garbage(x)                \

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 211

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+do {                            \
+    __typeof__(x) _x = (x);                \
+    _x->garbage.to_free = _x;            \
+    llist_add(&_x->garbage.llist, &mpam_garbage);    \
+} while (0)
+static void mpam_free_garbage(void)
+{
+    struct mpam_garbage *iter, *tmp;
+    struct llist_node *to_free = llist_del_all(&mpam_garbage);
+

Should this be protected by mpam_list_lock and check if the lock is held?

Quoted 4 lines

+    lockdep_assert_held(&mpam_list_lock);

Multiple threads may add and free garbage in parallel. Please see later free_garbage() is not protected by any lock.

Indeed - because its using llist instead. That is safe for concurrent use as you can only consume the whole list in one go with a cmpxchg(val, NULL). In the event of a race, one gets to own the llist and walk throught it - the other sees an empty list.

Quoted 29 lines
Quoted 29 lines

+    if (!to_free)
+        return;

drivers/resctrl/mpam_devices.c +25 -0
+
+    synchronize_srcu(&mpam_srcu);
+
+    llist_for_each_entry_safe(iter, tmp, to_free, llist) {
+        if (iter->pdev)
+            devm_kfree(&iter->pdev->dev, iter->to_free);
+        else
+            kfree(iter->to_free);
+    }
+}
 
+static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
+                  enum mpam_class_types type, u8 class_id,
+                  int component_id)
+{
+    int err;
+    struct mpam_vmsc *vmsc;
+    struct mpam_msc_ris *ris;
+    struct mpam_class *class;
+    struct mpam_component *comp;
+
+    lockdep_assert_held(&mpam_list_lock);
+
+    if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
+        return -EINVAL;
+
Quoted 8 lines
Quoted 3 lines

+    if (test_and_set_bit(ris_idx, &msc->ris_idxs)) +        return -EBUSY;
+

Should setting msc->ris_idxs bit be moved to the end of this function after all error handling paths? The reason is this bit is better to be 0 (or recovered) if any error happens. It's hard to recover it to 0 for each error handling. The easiest way is to set it at the end of the function.

This is an up front test for firmware tables that describe one RIS twice. No error recovery is needed as this is all this bitfield is used for.

Quoted 26 lines
Quoted 26 lines

+    ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL); +    if (!ris)
+        return -ENOMEM;
+    init_garbage(ris);

drivers/resctrl/mpam_devices.c +22 -0
+
+    class = mpam_class_get(class_id, type);
+    if (IS_ERR(class))
+        return PTR_ERR(class);
+
+    comp = mpam_component_get(class, component_id);
+    if (IS_ERR(comp)) {
+        if (list_empty(&class->components))
+            mpam_class_destroy(class);
+        return PTR_ERR(comp);
+    }
+
+    vmsc = mpam_vmsc_get(comp, msc);
+    if (IS_ERR(vmsc)) {
+        if (list_empty(&comp->vmsc))
+            mpam_comp_destroy(comp);
+        return PTR_ERR(vmsc);
+    }
+
+    err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
+    if (err) {
+        if (list_empty(&vmsc->ris))
Quoted 8 lines
Quoted 6 lines

+            mpam_vmsc_destroy(vmsc);
+        return err;
+    }
+
+    ris->ris_idx = ris_idx;
+    INIT_LIST_HEAD_RCU(&ris->vmsc_list);

vmsc_list will be used but not initialized. Missing INIT_LIST_HEAD_RCU(&ris->msc_list) here?

Fixed,

Quoted 8 lines

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 418

Quoted 6 lines

+    ris->vmsc = vmsc;

drivers/resctrl/mpam_devices.c +5 -0
+
+    cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
+    cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
+    list_add_rcu(&ris->vmsc_list, &vmsc->ris);
+

Setting the msc->ris_idxs here is better to avoid to clear it in each error handling path.

But misses the error it is supposed to catch...

Quoted 9 lines
Quoted 9 lines

+    return 0;

drivers/resctrl/mpam_devices.c +2 -3
+}
+
@@ -74,10 +469,10 @@ static void mpam_msc_drv_remove(struct platform_device *pdev)
           return;
         mutex_lock(&mpam_list_lock);
-    platform_set_drvdata(pdev, NULL);
-    list_del_rcu(&msc->all_msc_list);
-    synchronize_srcu(&mpam_srcu);
Quoted 8 lines

drivers/resctrl/mpam_devices.c static void mpam_msc_drv_remove(struct platform_device *pdev) · line 475

Quoted 4 lines
drivers/resctrl/mpam_devices.c +3 -0
+    mpam_msc_destroy(msc);
       mutex_unlock(&mpam_list_lock);
+
+    mpam_free_garbage();

Should mpam_free_garbage() be protected by mpam_list_lock? It may race with adding garbage. I can see other adding and freeing garbage are protected by mpam_list_lock but not this one.

No - it uses llist and is part of the deferred freeing, it should not need any locks.

Thanks,

James

#70 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

In reply to James Morse

Hi James,

Quoted 207 lines · James Morse

An MSC is a container of resources, each identified by their RIS index. Some RIS are described by firmware to provide their position in the system. Others are discovered when the driver probes the hardware.

To configure a resource it needs to be found by its class, e.g. 'L2'. There are two kinds of grouping, a class is a set of components, which are visible to user-space as there are likely to be multiple instances of the L2 cache. (e.g. one per cluster or package)

Add support for creating and destroying structures to allow a hierarchy of resources to be created.

---
Changes since v1:
* Fixed a comp/vmsc typo.
* Removed duplicate description from the commit message.
* Moved parenthesis in the add_to_garbage() macro.
* Check for out of range ris_idx when creating ris.
* Removed GFP as probe_lock is no longer a spin lock.
* Removed alloc flag as ended up searching the lists itself.
* Added a comment about affinity masks not overlapping.

Changes since RFC:
* removed a pr_err() debug message that crept in.

---
  drivers/resctrl/mpam_devices.c  | 406 +++++++++++++++++++++++++++++++-
  drivers/resctrl/mpam_internal.h |  90 +++++++
  include/linux/arm_mpam.h        |   8 +-
  3 files changed, 493 insertions(+), 11 deletions(-)
drivers/resctrl/mpam_devices.c +151 -2
index efc4738e3b4d..c7f4981b3545 100644
@@ -18,7 +18,6 @@
  #include <linux/printk.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/srcu.h>
  #include <linux/types.h>
 
  #include "mpam_internal.h"
@@ -31,7 +30,7 @@
  static DEFINE_MUTEX(mpam_list_lock);
  static LIST_HEAD(mpam_all_msc);
 
-static struct srcu_struct mpam_srcu;
+struct srcu_struct mpam_srcu;
 
  /*
   * Number of MSCs that have been probed. Once all MSC have been probed MPAM
@@ -39,6 +38,402 @@ static struct srcu_struct mpam_srcu;
   */
  static atomic_t mpam_num_msc;
 
+/*
+ * An MSC is a physical container for controls and monitors, each identified by
+ * their RIS index. These share a base-address, interrupts and some MMIO
+ * registers. A vMSC is a virtual container for RIS in an MSC that control or
+ * monitor the same thing. Members of a vMSC are all RIS in the same MSC, but
+ * not all RIS in an MSC share a vMSC.
+ * Components are a group of vMSC that control or monitor the same thing but
+ * are from different MSC, so have different base-address, interrupts etc.
+ * Classes are the set components of the same type.
+ *
+ * The features of a vMSC is the union of the RIS it contains.
+ * The features of a Class and Component are the common subset of the vMSC
+ * they contain.
+ *
+ * e.g. The system cache may have bandwidth controls on multiple interfaces,
+ * for regulating traffic from devices independently of traffic from CPUs.
+ * If these are two RIS in one MSC, they will be treated as controlling
+ * different things, and will not share a vMSC/component/class.
+ *
+ * e.g. The L2 may have one MSC and two RIS, one for cache-controls another
+ * for bandwidth. These two RIS are members of the same vMSC.
+ *
+ * e.g. The set of RIS that make up the L2 are grouped as a component. These
+ * are sometimes termed slices. They should be configured the same, as if there
+ * were only one.
+ *
+ * e.g. The SoC probably has more than one L2, each attached to a distinct set
+ * of CPUs. All the L2 components are grouped as a class.
+ *
+ * When creating an MSC, struct mpam_msc is added to the all mpam_all_msc list,
+ * then linked via struct mpam_ris to a vmsc, component and class.
+ * The same MSC may exist under different class->component->vmsc paths, but the
+ * RIS index will be unique.
+ */
+LIST_HEAD(mpam_classes);
+
+/* List of all objects that can be free()d after synchronise_srcu() */
+static LLIST_HEAD(mpam_garbage);
+
+#define init_garbage(x)	init_llist_node(&(x)->garbage.llist)
+
+static struct mpam_vmsc *
+mpam_vmsc_alloc(struct mpam_component *comp, struct mpam_msc *msc)
+{
+	struct mpam_vmsc *vmsc;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	vmsc = kzalloc(sizeof(*vmsc), GFP_KERNEL);
+	if (!vmsc)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(vmsc);
+
+	INIT_LIST_HEAD_RCU(&vmsc->ris);
+	INIT_LIST_HEAD_RCU(&vmsc->comp_list);
+	vmsc->comp = comp;
+	vmsc->msc = msc;
+
+	list_add_rcu(&vmsc->comp_list, &comp->vmsc);
+
+	return vmsc;
+}
+
+static struct mpam_vmsc *mpam_vmsc_get(struct mpam_component *comp,
+				       struct mpam_msc *msc)
+{
+	struct mpam_vmsc *vmsc;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
+		if (vmsc->msc->id == msc->id)
+			return vmsc;
+	}
+
+	return mpam_vmsc_alloc(comp, msc);
+}
+
+static struct mpam_component *
+mpam_component_alloc(struct mpam_class *class, int id)
+{
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	comp = kzalloc(sizeof(*comp), GFP_KERNEL);
+	if (!comp)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(comp);
+
+	comp->comp_id = id;
+	INIT_LIST_HEAD_RCU(&comp->vmsc);
+	/* affinity is updated when ris are added */
+	INIT_LIST_HEAD_RCU(&comp->class_list);
+	comp->class = class;
+
+	list_add_rcu(&comp->class_list, &class->components);
+
+	return comp;
+}
+
+static struct mpam_component *
+mpam_component_get(struct mpam_class *class, int id)
+{
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(comp, &class->components, class_list) {
+		if (comp->comp_id == id)
+			return comp;
+	}
+
+	return mpam_component_alloc(class, id);
+}
+
+static struct mpam_class *
+mpam_class_alloc(u8 level_idx, enum mpam_class_types type)
+{
+	struct mpam_class *class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	class = kzalloc(sizeof(*class), GFP_KERNEL);
+	if (!class)
+		return ERR_PTR(-ENOMEM);
+	init_garbage(class);
+
+	INIT_LIST_HEAD_RCU(&class->components);
+	/* affinity is updated when ris are added */
+	class->level = level_idx;
+	class->type = type;
+	INIT_LIST_HEAD_RCU(&class->classes_list);
+
+	list_add_rcu(&class->classes_list, &mpam_classes);
+
+	return class;
+}
+
+static struct mpam_class *
+mpam_class_get(u8 level_idx, enum mpam_class_types type)
+{
+	bool found = false;
+	struct mpam_class *class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		if (class->type == type && class->level == level_idx) {
+			found = true;

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 200

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+			break;
+		}
+	}
+
+	if (found)
+		return class;
+
+	return mpam_class_alloc(level_idx, type);
+}
+

The variable @found can be avoided if the found class can be returned immediately.

	list_for_each_entry(class, &mpam_classes, classes_list) {
		if (class->type == type && class->level == level_idx)
			return class;
	}

	return mpam_class_alloc(level_idx, type);
Quoted 139 lines
drivers/resctrl/mpam_devices.c +139 -0
+#define add_to_garbage(x)				\
+do {							\
+	__typeof__(x) _x = (x);				\
+	_x->garbage.to_free = _x;			\
+	llist_add(&_x->garbage.llist, &mpam_garbage);	\
+} while (0)
+
+static void mpam_class_destroy(struct mpam_class *class)
+{
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_del_rcu(&class->classes_list);
+	add_to_garbage(class);
+}
+
+static void mpam_comp_destroy(struct mpam_component *comp)
+{
+	struct mpam_class *class = comp->class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_del_rcu(&comp->class_list);
+	add_to_garbage(comp);
+
+	if (list_empty(&class->components))
+		mpam_class_destroy(class);
+}
+
+static void mpam_vmsc_destroy(struct mpam_vmsc *vmsc)
+{
+	struct mpam_component *comp = vmsc->comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_del_rcu(&vmsc->comp_list);
+	add_to_garbage(vmsc);
+
+	if (list_empty(&comp->vmsc))
+		mpam_comp_destroy(comp);
+}
+
+static void mpam_ris_destroy(struct mpam_msc_ris *ris)
+{
+	struct mpam_vmsc *vmsc = ris->vmsc;
+	struct mpam_msc *msc = vmsc->msc;
+	struct platform_device *pdev = msc->pdev;
+	struct mpam_component *comp = vmsc->comp;
+	struct mpam_class *class = comp->class;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	/*
+	 * It is assumed affinities don't overlap. If they do the class becomes
+	 * unusable immediately.
+	 */
+	cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity);
+	clear_bit(ris->ris_idx, &msc->ris_idxs);
+	list_del_rcu(&ris->vmsc_list);
+	list_del_rcu(&ris->msc_list);
+	add_to_garbage(ris);
+	ris->garbage.pdev = pdev;
+
+	if (list_empty(&vmsc->ris))
+		mpam_vmsc_destroy(vmsc);
+}
+
+/*
+ * There are two ways of reaching a struct mpam_msc_ris. Via the
+ * class->component->vmsc->ris, or via the msc.
+ * When destroying the msc, the other side needs unlinking and cleaning up too.
+ */
+static void mpam_msc_destroy(struct mpam_msc *msc)
+{
+	struct platform_device *pdev = msc->pdev;
+	struct mpam_msc_ris *ris, *tmp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list)
+		mpam_ris_destroy(ris);
+
+	list_del_rcu(&msc->all_msc_list);
+	platform_set_drvdata(pdev, NULL);
+
+	add_to_garbage(msc);
+	msc->garbage.pdev = pdev;
+}
+
+static void mpam_free_garbage(void)
+{
+	struct mpam_garbage *iter, *tmp;
+	struct llist_node *to_free = llist_del_all(&mpam_garbage);
+
+	if (!to_free)
+		return;
+
+	synchronize_srcu(&mpam_srcu);
+
+	llist_for_each_entry_safe(iter, tmp, to_free, llist) {
+		if (iter->pdev)
+			devm_kfree(&iter->pdev->dev, iter->to_free);
+		else
+			kfree(iter->to_free);
+	}
+}
+
+/*
+ * The cacheinfo structures are only populated when CPUs are online.
+ */
+int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
+				   cpumask_t *affinity)
+{
+	return acpi_pptt_get_cpumask_from_cache_id(cache_id, affinity);
+}
+
+/*
+ * cpumask_of_node() only knows about online CPUs. This can't tell us whether
+ * a class is represented on all possible CPUs.
+ */
+static void get_cpumask_from_node_id(u32 node_id, cpumask_t *affinity)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu) {
+		if (node_id == cpu_to_node(cpu))
+			cpumask_set_cpu(cpu, affinity);
+	}
+}
+
+static int mpam_ris_get_affinity(struct mpam_msc *msc, cpumask_t *affinity,
+				 enum mpam_class_types type,
+				 struct mpam_class *class,
+				 struct mpam_component *comp)
+{
+	int err;
+
+	switch (type) {
+	case MPAM_CLASS_CACHE:

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 349

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+		err = mpam_get_cpumask_from_cache_id(comp->comp_id, class->level,
+						     affinity);
+		if (err)
+			return err;
+
+		if (cpumask_empty(affinity))
+			pr_warn_once("%s no CPUs associated with cache node",
+				     dev_name(&msc->pdev->dev));
+
+		break;

"\n" missed in the error message and dev_warn_once() can be used:

		if (cpumask_empty(affinity))
			dev_warn_once(&msc->pdev->dev, "No CPUs associated with cache node\n");
Quoted 234 lines
drivers/resctrl/mpam_devices.c +91 -3
+	case MPAM_CLASS_MEMORY:
+		get_cpumask_from_node_id(comp->comp_id, affinity);
+		/* affinity may be empty for CPU-less memory nodes */
+		break;
+	case MPAM_CLASS_UNKNOWN:
+		return 0;
+	}
+
+	cpumask_and(affinity, affinity, &msc->accessibility);
+
+	return 0;
+}
+
+static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
+				  enum mpam_class_types type, u8 class_id,
+				  int component_id)
+{
+	int err;
+	struct mpam_vmsc *vmsc;
+	struct mpam_msc_ris *ris;
+	struct mpam_class *class;
+	struct mpam_component *comp;
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
+		return -EINVAL;
+
+	if (test_and_set_bit(ris_idx, &msc->ris_idxs))
+		return -EBUSY;
+
+	ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL);
+	if (!ris)
+		return -ENOMEM;
+	init_garbage(ris);
+
+	class = mpam_class_get(class_id, type);
+	if (IS_ERR(class))
+		return PTR_ERR(class);
+
+	comp = mpam_component_get(class, component_id);
+	if (IS_ERR(comp)) {
+		if (list_empty(&class->components))
+			mpam_class_destroy(class);
+		return PTR_ERR(comp);
+	}
+
+	vmsc = mpam_vmsc_get(comp, msc);
+	if (IS_ERR(vmsc)) {
+		if (list_empty(&comp->vmsc))
+			mpam_comp_destroy(comp);
+		return PTR_ERR(vmsc);
+	}
+
+	err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
+	if (err) {
+		if (list_empty(&vmsc->ris))
+			mpam_vmsc_destroy(vmsc);
+		return err;
+	}
+
+	ris->ris_idx = ris_idx;
+	INIT_LIST_HEAD_RCU(&ris->vmsc_list);
+	ris->vmsc = vmsc;
+
+	cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
+	cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
+	list_add_rcu(&ris->vmsc_list, &vmsc->ris);
+
+	return 0;
+}
+
+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
+		    enum mpam_class_types type, u8 class_id, int component_id)
+{
+	int err;
+
+	mutex_lock(&mpam_list_lock);
+	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
+				     component_id);
+	mutex_unlock(&mpam_list_lock);
+	if (err)
+		mpam_free_garbage();
+
+	return err;
+}
+
  /*
   * An MSC can control traffic from a set of CPUs, but may only be accessible
   * from a (hopefully wider) set of CPUs. The common reason for this is power
@@ -74,10 +469,10 @@ static void mpam_msc_drv_remove(struct platform_device *pdev)
  		return;
 
  	mutex_lock(&mpam_list_lock);
-	platform_set_drvdata(pdev, NULL);
-	list_del_rcu(&msc->all_msc_list);
-	synchronize_srcu(&mpam_srcu);
+	mpam_msc_destroy(msc);
  	mutex_unlock(&mpam_list_lock);
+
+	mpam_free_garbage();
  }
 
  static int mpam_msc_drv_probe(struct platform_device *pdev)
@@ -95,6 +490,7 @@ static int mpam_msc_drv_probe(struct platform_device *pdev)
  			err = -ENOMEM;
  			break;
  		}
+		init_garbage(msc);
 
  		mutex_init(&msc->probe_lock);
  		mutex_init(&msc->part_sel_lock);
drivers/resctrl/mpam_internal.h +90 -0
index 7c63d590fc98..02e9576ece6b 100644
@@ -7,10 +7,29 @@
  #include <linux/arm_mpam.h>
  #include <linux/cpumask.h>
  #include <linux/io.h>
+#include <linux/llist.h>
  #include <linux/mailbox_client.h>
  #include <linux/mutex.h>
  #include <linux/resctrl.h>
  #include <linux/sizes.h>
+#include <linux/srcu.h>
+
+#define MPAM_MSC_MAX_NUM_RIS	16
+
+/*
+ * Structures protected by SRCU may not be freed for a surprising amount of
+ * time (especially if perf is running). To ensure the MPAM error interrupt can
+ * tear down all the structures, build a list of objects that can be gargbage
+ * collected once synchronize_srcu() has returned.
+ * If pdev is non-NULL, use devm_kfree().
+ */
+struct mpam_garbage {
+	/* member of mpam_garbage */
+	struct llist_node	llist;
+
+	void			*to_free;
+	struct platform_device	*pdev;
+};
 
  struct mpam_msc {
  	/* member of mpam_all_msc */
@@ -57,8 +76,79 @@ struct mpam_msc {
 
  	void __iomem		*mapped_hwpage;
  	size_t			mapped_hwpage_sz;
+
+	struct mpam_garbage	garbage;
  };
 
+struct mpam_class {
+	/* mpam_components in this class */
+	struct list_head	components;
+
+	cpumask_t		affinity;
+
+	u8			level;
+	enum mpam_class_types	type;
+
+	/* member of mpam_classes */
+	struct list_head	classes_list;
+
+	struct mpam_garbage	garbage;
+};
+
+struct mpam_component {
+	u32			comp_id;
+
+	/* mpam_vmsc in this component */
+	struct list_head	vmsc;
+
+	cpumask_t		affinity;
+
+	/* member of mpam_class:components */
+	struct list_head	class_list;
+
+	/* parent: */
+	struct mpam_class	*class;
+
+	struct mpam_garbage	garbage;
+};
+
+struct mpam_vmsc {
+	/* member of mpam_component:vmsc_list */
+	struct list_head	comp_list;
+
+	/* mpam_msc_ris in this vmsc */
+	struct list_head	ris;
+
+	/* All RIS in this vMSC are members of this MSC */
+	struct mpam_msc		*msc;
+
+	/* parent: */
+	struct mpam_component	*comp;
+
+	struct mpam_garbage	garbage;
+};
+
+struct mpam_msc_ris {
+	u8			ris_idx;
+
+	cpumask_t		affinity;
+
+	/* member of mpam_vmsc:ris */
+	struct list_head	vmsc_list;
+
+	/* member of mpam_msc:ris */
+	struct list_head	msc_list;
+
+	/* parent: */
+	struct mpam_vmsc	*vmsc;
+
+	struct mpam_garbage	garbage;
+};
+
+/* List of all classes - protected by srcu*/
+extern struct srcu_struct mpam_srcu;
+extern struct list_head mpam_classes;
+
  int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
  				   cpumask_t *affinity);
 
include/linux/arm_mpam.h +0 -0
index 3d6c39c667c3..3206f5ddc147 100644
@@ -38,11 +38,7 @@ static inline int acpi_mpam_parse_resources(struct mpam_msc *msc,
  static inline int acpi_mpam_count_msc(void) { return -EINVAL; }
  #endif
 

include/linux/arm_mpam.h static inline int acpi_mpam_parse_resources(struct mpam_msc *msc, · line 44

Quoted 10 lines
include/linux/arm_mpam.h +2 -6
-static inline int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
-				  enum mpam_class_types type, u8 class_id,
-				  int component_id)
-{
-	return -EINVAL;
-}
+int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
+		    enum mpam_class_types type, u8 class_id, int component_id);
 
  #endif /* __LINUX_ARM_MPAM_H */

Thanks,
Gavin

author maintainer
#71 lore raw

Re: [PATCH v2 08/29] arm_mpam: Add the class and component structures for firmware described ris

Hi Gavin,

Quoted 42 lines · Gavin Shan
Quoted 28 lines · James Morse

An MSC is a container of resources, each identified by their RIS index. Some RIS are described by firmware to provide their position in the system. Others are discovered when the driver probes the hardware.

To configure a resource it needs to be found by its class, e.g. 'L2'. There are two kinds of grouping, a class is a set of components, which are visible to user-space as there are likely to be multiple instances of the L2 cache. (e.g. one per cluster or package)

Add support for creating and destroying structures to allow a hierarchy of resources to be created.

drivers/resctrl/mpam_devices.c +11 -0
index efc4738e3b4d..c7f4981b3545 100644
 
+static struct mpam_class *
+mpam_class_get(u8 level_idx, enum mpam_class_types type)
+{
+    bool found = false;
+    struct mpam_class *class;
+
+    lockdep_assert_held(&mpam_list_lock);
+
+    list_for_each_entry(class, &mpam_classes, classes_list) {
+        if (class->type == type && class->level == level_idx) {
+            found = true;

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 200

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+            break;
+        }
+    }
+
+    if (found)
+        return class;
+
+    return mpam_class_alloc(level_idx, type);
+}
+

The variable @found can be avoided if the found class can be returned immediately.

Quoted 6 lines

    list_for_each_entry(class, &mpam_classes, classes_list) { if (class->type == type && class->level == level_idx) return class;
    }

    return mpam_class_alloc(level_idx, type);

Yes, feedback like this already came from Jonathan.

Quoted 22 lines
Quoted 9 lines
drivers/resctrl/mpam_devices.c +9 -0
+static int mpam_ris_get_affinity(struct mpam_msc *msc, cpumask_t *affinity,
+                 enum mpam_class_types type,
+                 struct mpam_class *class,
+                 struct mpam_component *comp)
+{
+    int err;
+
+    switch (type) {
+    case MPAM_CLASS_CACHE:

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 349

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+        err = mpam_get_cpumask_from_cache_id(comp->comp_id, class->level,
+                             affinity);
+        if (err)
+            return err;
+
+        if (cpumask_empty(affinity))
+            pr_warn_once("%s no CPUs associated with cache node",
+                     dev_name(&msc->pdev->dev));
+
+        break;

"\n" missed in the error message and dev_warn_once() can be used:

Quoted 2 lines

        if (cpumask_empty(affinity))
            dev_warn_once(&msc->pdev->dev, "No CPUs associated with cache node\n");

Yup, I'd mopped up most of these but missed this one.

Quoted 4 lines
Quoted 4 lines

+    case MPAM_CLASS_MEMORY:
+        get_cpumask_from_node_id(comp->comp_id, affinity); +        /* affinity may be empty for CPU-less memory nodes */ +        break;

Quoted 8 lines

drivers/resctrl/mpam_devices.c static struct srcu_struct mpam_srcu; · line 361

Quoted 8 lines

+    case MPAM_CLASS_UNKNOWN:
+        return 0;
+    }

drivers/resctrl/mpam_devices.c +5 -0
+
+    cpumask_and(affinity, affinity, &msc->accessibility);
+
+    return 0;
+}

Thanks,

James

author maintainer
#72 lore raw

PATCH v2 9/29 arm_mpam: Add MPAM MSC register layout definitions

In reply to James Morse

Memory Partitioning and Monitoring (MPAM) has memory mapped devices (MSCs) with an identity/configuration page.

Add the definitions for these registers as offset within the page(s).

---
Changes since v1:
* Whitespace.
* Added constants for CASSOC and XCL.
* Merged FLT/CTL defines.
* Fixed MSMON_CFG_CSU_CTL_TYPE_CSU definition.

Changes since RFC:
* Renamed MSMON_CFG_MBWU_CTL_TYPE_CSU as MSMON_CFG_CSU_CTL_TYPE_CSU
* Whitepsace churn.
* Cite a more recent document.
* Removed some stale feature, fixed some names etc.

---
 drivers/resctrl/mpam_internal.h | 267 ++++++++++++++++++++++++++++++++
 1 file changed, 267 insertions(+)
drivers/resctrl/mpam_internal.h +267 -0
index 02e9576ece6b..109f03df46c2 100644
@@ -152,4 +152,271 @@ extern struct list_head mpam_classes;
 int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
 				   cpumask_t *affinity);
 
+/*
+ * MPAM MSCs have the following register layout. See:
+ * Arm Memory System Resource Partitioning and Monitoring (MPAM) System
+ * Component Specification.
+ * https://developer.arm.com/documentation/ihi0099/latest/
+ */
+#define MPAM_ARCHITECTURE_V1    0x10
+
+/* Memory mapped control pages: */
+/* ID Register offsets in the memory mapped page */
+#define MPAMF_IDR		0x0000  /* features id register */
+#define MPAMF_MSMON_IDR		0x0080  /* performance monitoring features */
+#define MPAMF_IMPL_IDR		0x0028  /* imp-def partitioning */
+#define MPAMF_CPOR_IDR		0x0030  /* cache-portion partitioning */
+#define MPAMF_CCAP_IDR		0x0038  /* cache-capacity partitioning */
+#define MPAMF_MBW_IDR		0x0040  /* mem-bw partitioning */
+#define MPAMF_PRI_IDR		0x0048  /* priority partitioning */
+#define MPAMF_CSUMON_IDR	0x0088  /* cache-usage monitor */
+#define MPAMF_MBWUMON_IDR	0x0090  /* mem-bw usage monitor */
+#define MPAMF_PARTID_NRW_IDR	0x0050  /* partid-narrowing */
+#define MPAMF_IIDR		0x0018  /* implementer id register */
+#define MPAMF_AIDR		0x0020  /* architectural id register */
+
+/* Configuration and Status Register offsets in the memory mapped page */
+#define MPAMCFG_PART_SEL	0x0100  /* partid to configure: */
+#define MPAMCFG_CPBM		0x1000  /* cache-portion config */
+#define MPAMCFG_CMAX		0x0108  /* cache-capacity config */
+#define MPAMCFG_CMIN		0x0110  /* cache-capacity config */
+#define MPAMCFG_CASSOC		0x0118  /* cache-associativity config */
+#define MPAMCFG_MBW_MIN		0x0200  /* min mem-bw config */
+#define MPAMCFG_MBW_MAX		0x0208  /* max mem-bw config */
+#define MPAMCFG_MBW_WINWD	0x0220  /* mem-bw accounting window config */
+#define MPAMCFG_MBW_PBM		0x2000  /* mem-bw portion bitmap config */
+#define MPAMCFG_PRI		0x0400  /* priority partitioning config */
+#define MPAMCFG_MBW_PROP	0x0500  /* mem-bw stride config */
+#define MPAMCFG_INTPARTID	0x0600  /* partid-narrowing config */
+
+#define MSMON_CFG_MON_SEL	0x0800  /* monitor selector */
+#define MSMON_CFG_CSU_FLT	0x0810  /* cache-usage monitor filter */
+#define MSMON_CFG_CSU_CTL	0x0818  /* cache-usage monitor config */
+#define MSMON_CFG_MBWU_FLT	0x0820  /* mem-bw monitor filter */
+#define MSMON_CFG_MBWU_CTL	0x0828  /* mem-bw monitor config */
+#define MSMON_CSU		0x0840  /* current cache-usage */
+#define MSMON_CSU_CAPTURE	0x0848  /* last cache-usage value captured */
+#define MSMON_MBWU		0x0860  /* current mem-bw usage value */
+#define MSMON_MBWU_CAPTURE	0x0868  /* last mem-bw value captured */
+#define MSMON_MBWU_L		0x0880  /* current long mem-bw usage value */
+#define MSMON_MBWU_CAPTURE_L	0x0890  /* last long mem-bw value captured */
+#define MSMON_CAPT_EVNT		0x0808  /* signal a capture event */
+#define MPAMF_ESR		0x00F8  /* error status register */
+#define MPAMF_ECR		0x00F0  /* error control register */
+
+/* MPAMF_IDR - MPAM features ID register */
+#define MPAMF_IDR_PARTID_MAX		GENMASK(15, 0)
+#define MPAMF_IDR_PMG_MAX		GENMASK(23, 16)
+#define MPAMF_IDR_HAS_CCAP_PART		BIT(24)
+#define MPAMF_IDR_HAS_CPOR_PART		BIT(25)
+#define MPAMF_IDR_HAS_MBW_PART		BIT(26)
+#define MPAMF_IDR_HAS_PRI_PART		BIT(27)
+#define MPAMF_IDR_EXT			BIT(28)
+#define MPAMF_IDR_HAS_IMPL_IDR		BIT(29)
+#define MPAMF_IDR_HAS_MSMON		BIT(30)
+#define MPAMF_IDR_HAS_PARTID_NRW	BIT(31)
+#define MPAMF_IDR_HAS_RIS		BIT(32)
+#define MPAMF_IDR_HAS_EXTD_ESR		BIT(38)
+#define MPAMF_IDR_HAS_ESR		BIT(39)
+#define MPAMF_IDR_RIS_MAX		GENMASK(59, 56)
+
+/* MPAMF_MSMON_IDR - MPAM performance monitoring ID register */
+#define MPAMF_MSMON_IDR_MSMON_CSU		BIT(16)
+#define MPAMF_MSMON_IDR_MSMON_MBWU		BIT(17)
+#define MPAMF_MSMON_IDR_HAS_LOCAL_CAPT_EVNT	BIT(31)
+
+/* MPAMF_CPOR_IDR - MPAM features cache portion partitioning ID register */
+#define MPAMF_CPOR_IDR_CPBM_WD			GENMASK(15, 0)
+
+/* MPAMF_CCAP_IDR - MPAM features cache capacity partitioning ID register */
+#define MPAMF_CCAP_IDR_CMAX_WD			GENMASK(5, 0)
+#define MPAMF_CCAP_IDR_CASSOC_WD		GENMASK(12, 8)
+#define MPAMF_CCAP_IDR_HAS_CASSOC		BIT(28)
+#define MPAMF_CCAP_IDR_HAS_CMIN			BIT(29)
+#define MPAMF_CCAP_IDR_NO_CMAX			BIT(30)
+#define MPAMF_CCAP_IDR_HAS_CMAX_SOFTLIM		BIT(31)
+
+/* MPAMF_MBW_IDR - MPAM features memory bandwidth partitioning ID register */
+#define MPAMF_MBW_IDR_BWA_WD		GENMASK(5, 0)
+#define MPAMF_MBW_IDR_HAS_MIN		BIT(10)
+#define MPAMF_MBW_IDR_HAS_MAX		BIT(11)
+#define MPAMF_MBW_IDR_HAS_PBM		BIT(12)
+#define MPAMF_MBW_IDR_HAS_PROP		BIT(13)
+#define MPAMF_MBW_IDR_WINDWR		BIT(14)
+#define MPAMF_MBW_IDR_BWPBM_WD		GENMASK(28, 16)
+
+/* MPAMF_PRI_IDR - MPAM features priority partitioning ID register */
+#define MPAMF_PRI_IDR_HAS_INTPRI	BIT(0)
+#define MPAMF_PRI_IDR_INTPRI_0_IS_LOW	BIT(1)
+#define MPAMF_PRI_IDR_INTPRI_WD		GENMASK(9, 4)
+#define MPAMF_PRI_IDR_HAS_DSPRI		BIT(16)
+#define MPAMF_PRI_IDR_DSPRI_0_IS_LOW	BIT(17)
+#define MPAMF_PRI_IDR_DSPRI_WD		GENMASK(25, 20)
+
+/* MPAMF_CSUMON_IDR - MPAM cache storage usage monitor ID register */
+#define MPAMF_CSUMON_IDR_NUM_MON	GENMASK(15, 0)
+#define MPAMF_CSUMON_IDR_HAS_OFLOW_CAPT	BIT(24)
+#define MPAMF_CSUMON_IDR_HAS_CEVNT_OFLW	BIT(25)
+#define MPAMF_CSUMON_IDR_HAS_OFSR	BIT(26)
+#define MPAMF_CSUMON_IDR_HAS_OFLOW_LNKG	BIT(27)
+#define MPAMF_CSUMON_IDR_HAS_XCL	BIT(29)
+#define MPAMF_CSUMON_IDR_CSU_RO		BIT(30)
+#define MPAMF_CSUMON_IDR_HAS_CAPTURE	BIT(31)
+
+/* MPAMF_MBWUMON_IDR - MPAM memory bandwidth usage monitor ID register */
+#define MPAMF_MBWUMON_IDR_NUM_MON	GENMASK(15, 0)
+#define MPAMF_MBWUMON_IDR_HAS_RWBW	BIT(28)
+#define MPAMF_MBWUMON_IDR_LWD		BIT(29)
+#define MPAMF_MBWUMON_IDR_HAS_LONG	BIT(30)
+#define MPAMF_MBWUMON_IDR_HAS_CAPTURE	BIT(31)
+
+/* MPAMF_PARTID_NRW_IDR - MPAM PARTID narrowing ID register */
+#define MPAMF_PARTID_NRW_IDR_INTPARTID_MAX	GENMASK(15, 0)
+
+/* MPAMF_IIDR - MPAM implementation ID register */
+#define MPAMF_IIDR_PRODUCTID	GENMASK(31, 20)
+#define MPAMF_IIDR_PRODUCTID_SHIFT	20
+#define MPAMF_IIDR_VARIANT	GENMASK(19, 16)
+#define MPAMF_IIDR_VARIANT_SHIFT	16
+#define MPAMF_IIDR_REVISON	GENMASK(15, 12)
+#define MPAMF_IIDR_REVISON_SHIFT	12
+#define MPAMF_IIDR_IMPLEMENTER	GENMASK(11, 0)
+#define MPAMF_IIDR_IMPLEMENTER_SHIFT	0
+
+/* MPAMF_AIDR - MPAM architecture ID register */
+#define MPAMF_AIDR_ARCH_MAJOR_REV	GENMASK(7, 4)
+#define MPAMF_AIDR_ARCH_MINOR_REV	GENMASK(3, 0)
+
+/* MPAMCFG_PART_SEL - MPAM partition configuration selection register */
+#define MPAMCFG_PART_SEL_PARTID_SEL	GENMASK(15, 0)
+#define MPAMCFG_PART_SEL_INTERNAL	BIT(16)
+#define MPAMCFG_PART_SEL_RIS		GENMASK(27, 24)
+
+/* MPAMCFG_CASSOC - MPAM cache maximum associativity partition configuration register */
+#define MPAMCFG_CASSOC_CASSOC		GENMASK(15, 0)
+
+/* MPAMCFG_CMAX - MPAM cache capacity configuration register */
+#define MPAMCFG_CMAX_SOFTLIM		BIT(31)
+#define MPAMCFG_CMAX_CMAX		GENMASK(15, 0)
+
+/* MPAMCFG_CMIN - MPAM cache capacity configuration register */
+#define MPAMCFG_CMIN_CMIN		GENMASK(15, 0)
+
+/*
+ * MPAMCFG_MBW_MIN - MPAM memory minimum bandwidth partitioning configuration
+ *                   register
+ */
+#define MPAMCFG_MBW_MIN_MIN		GENMASK(15, 0)
+
+/*
+ * MPAMCFG_MBW_MAX - MPAM memory maximum bandwidth partitioning configuration
+ *                   register
+ */
+#define MPAMCFG_MBW_MAX_MAX		GENMASK(15, 0)
+#define MPAMCFG_MBW_MAX_HARDLIM		BIT(31)
+
+/*
+ * MPAMCFG_MBW_WINWD - MPAM memory bandwidth partitioning window width
+ *                     register
+ */
+#define MPAMCFG_MBW_WINWD_US_FRAC	GENMASK(7, 0)
+#define MPAMCFG_MBW_WINWD_US_INT	GENMASK(23, 8)
+
+/* MPAMCFG_PRI - MPAM priority partitioning configuration register */
+#define MPAMCFG_PRI_INTPRI		GENMASK(15, 0)
+#define MPAMCFG_PRI_DSPRI		GENMASK(31, 16)
+
+/*
+ * MPAMCFG_MBW_PROP - Memory bandwidth proportional stride partitioning
+ *                    configuration register
+ */
+#define MPAMCFG_MBW_PROP_STRIDEM1	GENMASK(15, 0)
+#define MPAMCFG_MBW_PROP_EN		BIT(31)
+
+/*
+ * MPAMCFG_INTPARTID - MPAM internal partition narrowing configuration register
+ */
+#define MPAMCFG_INTPARTID_INTPARTID	GENMASK(15, 0)
+#define MPAMCFG_INTPARTID_INTERNAL	BIT(16)
+
+/* MSMON_CFG_MON_SEL - Memory system performance monitor selection register */
+#define MSMON_CFG_MON_SEL_MON_SEL	GENMASK(15, 0)
+#define MSMON_CFG_MON_SEL_RIS		GENMASK(27, 24)
+
+/* MPAMF_ESR - MPAM Error Status Register */
+#define MPAMF_ESR_PARTID_MON	GENMASK(15, 0)
+#define MPAMF_ESR_PMG		GENMASK(23, 16)
+#define MPAMF_ESR_ERRCODE	GENMASK(27, 24)
+#define MPAMF_ESR_OVRWR		BIT(31)
+#define MPAMF_ESR_RIS		GENMASK(35, 32)
+
+/* MPAMF_ECR - MPAM Error Control Register */
+#define MPAMF_ECR_INTEN		BIT(0)
+
+/* Error conditions in accessing memory mapped registers */
+#define MPAM_ERRCODE_NONE			0
+#define MPAM_ERRCODE_PARTID_SEL_RANGE		1
+#define MPAM_ERRCODE_REQ_PARTID_RANGE		2
+#define MPAM_ERRCODE_MSMONCFG_ID_RANGE		3
+#define MPAM_ERRCODE_REQ_PMG_RANGE		4
+#define MPAM_ERRCODE_MONITOR_RANGE		5
+#define MPAM_ERRCODE_INTPARTID_RANGE		6
+#define MPAM_ERRCODE_UNEXPECTED_INTERNAL	7
+
+/*
+ * MSMON_CFG_CSU_CTL - Memory system performance monitor configure cache storage
+ *                    usage monitor control register
+ * MSMON_CFG_MBWU_CTL - Memory system performance monitor configure memory
+ *                     bandwidth usage monitor control register
+ */
+#define MSMON_CFG_x_CTL_TYPE			GENMASK(7, 0)
+#define MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L	BIT(15)
+#define MSMON_CFG_x_CTL_MATCH_PARTID		BIT(16)
+#define MSMON_CFG_x_CTL_MATCH_PMG		BIT(17)
+#define MSMON_CFG_x_CTL_SCLEN			BIT(19)
+#define MSMON_CFG_x_CTL_SUBTYPE			GENMASK(22, 20)
+#define MSMON_CFG_x_CTL_OFLOW_FRZ		BIT(24)
+#define MSMON_CFG_x_CTL_OFLOW_INTR		BIT(25)
+#define MSMON_CFG_x_CTL_OFLOW_STATUS		BIT(26)
+#define MSMON_CFG_x_CTL_CAPT_RESET		BIT(27)
+#define MSMON_CFG_x_CTL_CAPT_EVNT		GENMASK(30, 28)
+#define MSMON_CFG_x_CTL_EN			BIT(31)
+
+#define MSMON_CFG_MBWU_CTL_TYPE_MBWU			0x42
+#define MSMON_CFG_CSU_CTL_TYPE_CSU			0x43
+
+/*
+ * MSMON_CFG_CSU_FLT -  Memory system performance monitor configure cache storage
+ *                      usage monitor filter register
+ * MSMON_CFG_MBWU_FLT - Memory system performance monitor configure memory
+ *                      bandwidth usage monitor filter register
+ */
+#define MSMON_CFG_x_FLT_PARTID			GENMASK(15, 0)
+#define MSMON_CFG_x_FLT_PMG			GENMASK(23, 16)
+
+#define MSMON_CFG_MBWU_FLT_RWBW			GENMASK(31, 30)
+#define MSMON_CFG_CSU_FLT_XCL			BIT(31)
+
+/*
+ * MSMON_CSU - Memory system performance monitor cache storage usage monitor
+ *            register
+ * MSMON_CSU_CAPTURE -  Memory system performance monitor cache storage usage
+ *                     capture register
+ * MSMON_MBWU  - Memory system performance monitor memory bandwidth usage
+ *               monitor register
+ * MSMON_MBWU_CAPTURE - Memory system performance monitor memory bandwidth usage
+ *                     capture register
+ */
+#define MSMON___VALUE		GENMASK(30, 0)
+#define MSMON___NRDY		BIT(31)
+#define MSMON___NRDY_L		BIT(63)
+#define MSMON___L_VALUE		GENMASK(43, 0)
+#define MSMON___LWD_VALUE	GENMASK(62, 0)
+
+/*
+ * MSMON_CAPT_EVNT - Memory system performance monitoring capture event
+ *                  generation register
+ */
+#define MSMON_CAPT_EVNT_NOW	BIT(0)
+
 #endif /* MPAM_INTERNAL_H */
Signature · 1 line
2.39.5
#73 lore raw

Re: [PATCH v2 09/29] arm_mpam: Add MPAM MSC register layout definitions

Quoted 4 lines · On Wed, 10 Sep 2025 20:42:49 +0000 James Morse <[email protected]> wrote:

Memory Partitioning and Monitoring (MPAM) has memory mapped devices (MSCs) with an identity/configuration page.

Add the definitions for these registers as offset within the page(s).

Hi James,

I'm not sure why some things ended up in this patch and others didn't. MPAMCFG_EN for example isn't here.

If doing a separate 'register defines' patch I'd do the lot as of the current spec.

Quoted 2 lines

Maybe link a specific version? I'm not sure if I'm looking at is the same one as you were when you wrote this. That will become worse over time. I'm definitely seeing extra bits in a number of registers.

I'm lazy enough not to go see if the cover letter calls out a version.

Anyhow, various small things on ordering that would have made this easier to review against the spec.

Jonathan

Quoted 23 lines

---
Changes since v1:
* Whitespace.
* Added constants for CASSOC and XCL.
* Merged FLT/CTL defines.
* Fixed MSMON_CFG_CSU_CTL_TYPE_CSU definition.

Changes since RFC:
* Renamed MSMON_CFG_MBWU_CTL_TYPE_CSU as MSMON_CFG_CSU_CTL_TYPE_CSU
* Whitepsace churn.
* Cite a more recent document.
* Removed some stale feature, fixed some names etc.

---
 drivers/resctrl/mpam_internal.h | 267 ++++++++++++++++++++++++++++++++
 1 file changed, 267 insertions(+)
drivers/resctrl/mpam_internal.h +0 -0
index 02e9576ece6b..109f03df46c2 100644

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 159

Quoted 8 lines
drivers/resctrl/mpam_internal.h +5 -0
@@ -152,4 +152,271 @@ extern struct list_head mpam_classes;
 int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
 				   cpumask_t *affinity);
 
+/*
+ * MPAM MSCs have the following register layout. See:
+ * Arm Memory System Resource Partitioning and Monitoring (MPAM) System
+ * Component Specification.
+ * https://developer.arm.com/documentation/ihi0099/latest/

Maybe be friendly and give some section number references.

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 166

Quoted 7 lines
drivers/resctrl/mpam_internal.h +7 -0
+ */
+#define MPAM_ARCHITECTURE_V1    0x10
+
+/* Memory mapped control pages: */
+/* ID Register offsets in the memory mapped page */
+#define MPAMF_IDR		0x0000  /* features id register */
+#define MPAMF_MSMON_IDR		0x0080  /* performance monitoring features */

Any reason this one is out of order with respect to the addresses?

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 176

Quoted 10 lines
drivers/resctrl/mpam_internal.h +10 -0
+#define MPAMF_IMPL_IDR		0x0028  /* imp-def partitioning */
+#define MPAMF_CPOR_IDR		0x0030  /* cache-portion partitioning */
+#define MPAMF_CCAP_IDR		0x0038  /* cache-capacity partitioning */
+#define MPAMF_MBW_IDR		0x0040  /* mem-bw partitioning */
+#define MPAMF_PRI_IDR		0x0048  /* priority partitioning */
+#define MPAMF_CSUMON_IDR	0x0088  /* cache-usage monitor */
+#define MPAMF_MBWUMON_IDR	0x0090  /* mem-bw usage monitor */
+#define MPAMF_PARTID_NRW_IDR	0x0050  /* partid-narrowing */
+#define MPAMF_IIDR		0x0018  /* implementer id register */
+#define MPAMF_AIDR		0x0020  /* architectural id register */

These 3 as well. I'm not sure what the ordering is conveying but probably easier to just to put them in address order.

There are some other cases of this below.

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 284

Quoted 9 lines
drivers/resctrl/mpam_internal.h +9 -0
+/* MPAMF_IIDR - MPAM implementation ID register */
+#define MPAMF_IIDR_PRODUCTID	GENMASK(31, 20)
+#define MPAMF_IIDR_PRODUCTID_SHIFT	20
+#define MPAMF_IIDR_VARIANT	GENMASK(19, 16)
+#define MPAMF_IIDR_VARIANT_SHIFT	16
+#define MPAMF_IIDR_REVISON	GENMASK(15, 12)
+#define MPAMF_IIDR_REVISON_SHIFT	12
+#define MPAMF_IIDR_IMPLEMENTER	GENMASK(11, 0)
+#define MPAMF_IIDR_IMPLEMENTER_SHIFT	0

I'd expect to see FIELD_GET/ PREP rather than use of shifts. Can we drop the defines?

Pick an order for reg field definitions. Until here they've been low to high.

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 364

Quoted 9 lines
drivers/resctrl/mpam_internal.h +9 -0
+/* Error conditions in accessing memory mapped registers */
+#define MPAM_ERRCODE_NONE			0
+#define MPAM_ERRCODE_PARTID_SEL_RANGE		1
+#define MPAM_ERRCODE_REQ_PARTID_RANGE		2
+#define MPAM_ERRCODE_MSMONCFG_ID_RANGE		3
+#define MPAM_ERRCODE_REQ_PMG_RANGE		4
+#define MPAM_ERRCODE_MONITOR_RANGE		5
+#define MPAM_ERRCODE_INTPARTID_RANGE		6
+#define MPAM_ERRCODE_UNEXPECTED_INTERNAL	7

Seems there are more in latest spec..

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 376

Quoted 12 lines
drivers/resctrl/mpam_internal.h +12 -0
+
+/*
+ * MSMON_CFG_CSU_CTL - Memory system performance monitor configure cache storage
+ *                    usage monitor control register
+ * MSMON_CFG_MBWU_CTL - Memory system performance monitor configure memory
+ *                     bandwidth usage monitor control register
+ */
+#define MSMON_CFG_x_CTL_TYPE			GENMASK(7, 0)
+#define MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L	BIT(15)
+#define MSMON_CFG_x_CTL_MATCH_PARTID		BIT(16)
+#define MSMON_CFG_x_CTL_MATCH_PMG		BIT(17)
+#define MSMON_CFG_x_CTL_SCLEN			BIT(19)

On the spec I'm looking at this is reserved in CSU_CTL

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 383

Quoted 7 lines
drivers/resctrl/mpam_internal.h +7 -0
+#define MSMON_CFG_x_CTL_SUBTYPE			GENMASK(22, 20)
+#define MSMON_CFG_x_CTL_OFLOW_FRZ		BIT(24)
+#define MSMON_CFG_x_CTL_OFLOW_INTR		BIT(25)
+#define MSMON_CFG_x_CTL_OFLOW_STATUS		BIT(26)
+#define MSMON_CFG_x_CTL_CAPT_RESET		BIT(27)
+#define MSMON_CFG_x_CTL_CAPT_EVNT		GENMASK(30, 28)
+#define MSMON_CFG_x_CTL_EN			BIT(31)

I guess this combining of definitions will show some advante in common code later but right now it seems confusing given not all bits are present in both.

Quoted 39 lines
drivers/resctrl/mpam_internal.h +38 -0
+
+#define MSMON_CFG_MBWU_CTL_TYPE_MBWU			0x42
+#define MSMON_CFG_CSU_CTL_TYPE_CSU			0x43
+
+/*
+ * MSMON_CFG_CSU_FLT -  Memory system performance monitor configure cache storage
+ *                      usage monitor filter register
+ * MSMON_CFG_MBWU_FLT - Memory system performance monitor configure memory
+ *                      bandwidth usage monitor filter register
+ */
+#define MSMON_CFG_x_FLT_PARTID			GENMASK(15, 0)
+#define MSMON_CFG_x_FLT_PMG			GENMASK(23, 16)
+
+#define MSMON_CFG_MBWU_FLT_RWBW			GENMASK(31, 30)
+#define MSMON_CFG_CSU_FLT_XCL			BIT(31)
+
+/*
+ * MSMON_CSU - Memory system performance monitor cache storage usage monitor
+ *            register
+ * MSMON_CSU_CAPTURE -  Memory system performance monitor cache storage usage
+ *                     capture register
+ * MSMON_MBWU  - Memory system performance monitor memory bandwidth usage
+ *               monitor register
+ * MSMON_MBWU_CAPTURE - Memory system performance monitor memory bandwidth usage
+ *                     capture register
+ */
+#define MSMON___VALUE		GENMASK(30, 0)
+#define MSMON___NRDY		BIT(31)
+#define MSMON___NRDY_L		BIT(63)
+#define MSMON___L_VALUE		GENMASK(43, 0)
+#define MSMON___LWD_VALUE	GENMASK(62, 0)
+
+/*
+ * MSMON_CAPT_EVNT - Memory system performance monitoring capture event
+ *                  generation register
+ */
+#define MSMON_CAPT_EVNT_NOW	BIT(0)
+
 #endif /* MPAM_INTERNAL_H */
author maintainer
#74 lore raw

Re: [PATCH v2 09/29] arm_mpam: Add MPAM MSC register layout definitions

Hi Jonathan,

Quoted 10 lines · Jonathan Cameron
Quoted 4 lines · On Wed, 10 Sep 2025 20:42:49 +0000 James Morse <[email protected]> wrote:

Memory Partitioning and Monitoring (MPAM) has memory mapped devices (MSCs) with an identity/configuration page.

Add the definitions for these registers as offset within the page(s).

I'm not sure why some things ended up in this patch and others didn't. MPAMCFG_EN for example isn't here.

Things were added once I'd already written this, and I only updated it with 'new' features where they were actually useful for feature parity with resctrl/Intel-RDT.

Quoted 2 lines

If doing a separate 'register defines' patch I'd do the lot as of the current spec.

I've not done this because its a time sink for no benefit. The kernel doesn't use any of the 'missing' features. While I agree it would be nice if the list were up to date - it will become stale pretty quickly, so its not an achievable goal...

Quoted 23 lines
Quoted 3 lines

Maybe link a specific version? I'm not sure if I'm looking at is the same one as you were when you wrote this. That will become worse over time. I'm definitely seeing extra bits in a number of registers.

I'm lazy enough not to go see if the cover letter calls out a version.

Anyhow, various small things on ordering that would have made this easier to review against the spec.

Quoted 8 lines
drivers/resctrl/mpam_internal.h +0 -0
index 02e9576ece6b..109f03df46c2 100644
@@ -152,4 +152,271 @@ extern struct list_head mpam_classes;
 int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
 				   cpumask_t *affinity);
 
Quoted 7 lines

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 159

Quoted 5 lines
drivers/resctrl/mpam_internal.h +5 -0
+/*
+ * MPAM MSCs have the following register layout. See:
+ * Arm Memory System Resource Partitioning and Monitoring (MPAM) System
+ * Component Specification.
+ * https://developer.arm.com/documentation/ihi0099/latest/

Maybe be friendly and give some section number references.

Heh, linking to the 'latest' means those will change...

Quoted 10 lines

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 166

Quoted 8 lines
drivers/resctrl/mpam_internal.h +7 -0
+ */
+#define MPAM_ARCHITECTURE_V1    0x10
+
+/* Memory mapped control pages: */
+/* ID Register offsets in the memory mapped page */
+#define MPAMF_IDR		0x0000  /* features id register */
+#define MPAMF_MSMON_IDR		0x0080  /* performance monitoring features */

Any reason this one is out of order with respect to the addresses?

No - I must have been going mad!

Quoted 14 lines

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 176

Quoted 10 lines
drivers/resctrl/mpam_internal.h +10 -0
+#define MPAMF_IMPL_IDR		0x0028  /* imp-def partitioning */
+#define MPAMF_CPOR_IDR		0x0030  /* cache-portion partitioning */
+#define MPAMF_CCAP_IDR		0x0038  /* cache-capacity partitioning */
+#define MPAMF_MBW_IDR		0x0040  /* mem-bw partitioning */
+#define MPAMF_PRI_IDR		0x0048  /* priority partitioning */
+#define MPAMF_CSUMON_IDR	0x0088  /* cache-usage monitor */
+#define MPAMF_MBWUMON_IDR	0x0090  /* mem-bw usage monitor */
+#define MPAMF_PARTID_NRW_IDR	0x0050  /* partid-narrowing */
+#define MPAMF_IIDR		0x0018  /* implementer id register */
+#define MPAMF_AIDR		0x0020  /* architectural id register */

These 3 as well. I'm not sure what the ordering is conveying but probably easier to just to put them in address order.

Quoted 1 line

There are some other cases of this below.

... I reckon the ones in funny places were the ones that the original FVP supported i.e. only the mandatory ones, which wasn't particularly useful.

Quoted 11 lines

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 284

Quoted 9 lines
drivers/resctrl/mpam_internal.h +9 -0
+/* MPAMF_IIDR - MPAM implementation ID register */
+#define MPAMF_IIDR_PRODUCTID	GENMASK(31, 20)
+#define MPAMF_IIDR_PRODUCTID_SHIFT	20
+#define MPAMF_IIDR_VARIANT	GENMASK(19, 16)
+#define MPAMF_IIDR_VARIANT_SHIFT	16
+#define MPAMF_IIDR_REVISON	GENMASK(15, 12)
+#define MPAMF_IIDR_REVISON_SHIFT	12
+#define MPAMF_IIDR_IMPLEMENTER	GENMASK(11, 0)
+#define MPAMF_IIDR_IMPLEMENTER_SHIFT	0

I'd expect to see FIELD_GET/ PREP rather than use of shifts. Can we drop the defines?

Sure,

Quoted 1 line

Pick an order for reg field definitions. Until here they've been low to high.

I think I've got that more consistent now...

Quoted 11 lines

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 364

Quoted 9 lines
drivers/resctrl/mpam_internal.h +9 -0
+/* Error conditions in accessing memory mapped registers */
+#define MPAM_ERRCODE_NONE			0
+#define MPAM_ERRCODE_PARTID_SEL_RANGE		1
+#define MPAM_ERRCODE_REQ_PARTID_RANGE		2
+#define MPAM_ERRCODE_MSMONCFG_ID_RANGE		3
+#define MPAM_ERRCODE_REQ_PMG_RANGE		4
+#define MPAM_ERRCODE_MONITOR_RANGE		5
+#define MPAM_ERRCODE_INTPARTID_RANGE		6
+#define MPAM_ERRCODE_UNEXPECTED_INTERNAL	7

Seems there are more in latest spec..

Yup, it the frequent game of spot-the-difference.
I've updated that as part of your other feedback.

Quoted 6 lines
Quoted 6 lines
drivers/resctrl/mpam_internal.h +6 -0
+
+/*
+ * MSMON_CFG_CSU_CTL - Memory system performance monitor configure cache storage
+ *                    usage monitor control register
+ * MSMON_CFG_MBWU_CTL - Memory system performance monitor configure memory
+ *                     bandwidth usage monitor control register
Quoted 8 lines

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 376

Quoted 6 lines
drivers/resctrl/mpam_internal.h +6 -0
+ */
+#define MSMON_CFG_x_CTL_TYPE			GENMASK(7, 0)
+#define MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L	BIT(15)
+#define MSMON_CFG_x_CTL_MATCH_PARTID		BIT(16)
+#define MSMON_CFG_x_CTL_MATCH_PMG		BIT(17)
+#define MSMON_CFG_x_CTL_SCLEN			BIT(19)

On the spec I'm looking at this is reserved in CSU_CTL

It's only defined for MSMON: "Value scaling enable",. I'll move it after the MSMON_CFG_MBWU_CTL_TYPE_MBWU define below.

Quoted 10 lines

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 383

Quoted 7 lines
drivers/resctrl/mpam_internal.h +7 -0
+#define MSMON_CFG_x_CTL_SUBTYPE			GENMASK(22, 20)
+#define MSMON_CFG_x_CTL_OFLOW_FRZ		BIT(24)
+#define MSMON_CFG_x_CTL_OFLOW_INTR		BIT(25)
+#define MSMON_CFG_x_CTL_OFLOW_STATUS		BIT(26)
+#define MSMON_CFG_x_CTL_CAPT_RESET		BIT(27)
+#define MSMON_CFG_x_CTL_CAPT_EVNT		GENMASK(30, 28)
+#define MSMON_CFG_x_CTL_EN			BIT(31)

I guess this combining of definitions will show some advante in common code later but right now it seems confusing given not all bits are present in both.

When I started these were the same!

It is dealt with in common code, I don't think any of the bits that are different are used by the driver.

Thanks,

James

#75 lore raw

Re: [PATCH v2 09/29] arm_mpam: Add MPAM MSC register layout definitions

In reply to James Morse

…

Quoted 2 lines

+++ b/drivers/resctrl/mpam_internal.h

(quoted diff) +0 -0
@@ -152,4 +152,271 @@ extern struct list_head mpam_classes;

…

Quoted 3 lines
drivers/resctrl/mpam_internal.h +3 -0
+/* Error conditions in accessing memory mapped registers */
+#define MPAM_ERRCODE_NONE			0
+#define MPAM_ERRCODE_PARTID_SEL_RANGE		1

…

Quoted 1 line

+#define MPAM_ERRCODE_UNEXPECTED_INTERNAL 7

…

How do you think about to use an enumeration for such a value collection? (Is there a need to extend implementation details in similar ways at further source code places?)

Regards,
Markus

#76 lore raw

Re: [PATCH v2 09/29] arm_mpam: Add MPAM MSC register layout definitions

In reply to James Morse

Hi James,

Quoted 31 lines · James Morse

Memory Partitioning and Monitoring (MPAM) has memory mapped devices (MSCs) with an identity/configuration page.

Add the definitions for these registers as offset within the page(s).

---
Changes since v1:
* Whitespace.
* Added constants for CASSOC and XCL.
* Merged FLT/CTL defines.
* Fixed MSMON_CFG_CSU_CTL_TYPE_CSU definition.

Changes since RFC:
* Renamed MSMON_CFG_MBWU_CTL_TYPE_CSU as MSMON_CFG_CSU_CTL_TYPE_CSU
* Whitepsace churn.
* Cite a more recent document.
* Removed some stale feature, fixed some names etc.

---
  drivers/resctrl/mpam_internal.h | 267 ++++++++++++++++++++++++++++++++
  1 file changed, 267 insertions(+)
drivers/resctrl/mpam_internal.h +0 -0
index 02e9576ece6b..109f03df46c2 100644
@@ -152,4 +152,271 @@ extern struct list_head mpam_classes;
  int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
  				   cpumask_t *affinity);

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 163

Quoted 10 lines
drivers/resctrl/mpam_internal.h +9 -0
 
+/*
+ * MPAM MSCs have the following register layout. See:
+ * Arm Memory System Resource Partitioning and Monitoring (MPAM) System
+ * Component Specification.
+ * https://developer.arm.com/documentation/ihi0099/latest/
+ */
+#define MPAM_ARCHITECTURE_V1    0x10
+
+/* Memory mapped control pages: */

":" seems unnecessary.

Quoted 6 lines
drivers/resctrl/mpam_internal.h +6 -0
+/* ID Register offsets in the memory mapped page */
+#define MPAMF_IDR		0x0000  /* features id register */
+#define MPAMF_MSMON_IDR		0x0080  /* performance monitoring features */
+#define MPAMF_IMPL_IDR		0x0028  /* imp-def partitioning */
+#define MPAMF_CPOR_IDR		0x0030  /* cache-portion partitioning */
+#define MPAMF_CCAP_IDR		0x0038  /* cache-capacity partitioning */

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 179

Quoted 10 lines
drivers/resctrl/mpam_internal.h +10 -0
+#define MPAMF_MBW_IDR		0x0040  /* mem-bw partitioning */
+#define MPAMF_PRI_IDR		0x0048  /* priority partitioning */
+#define MPAMF_CSUMON_IDR	0x0088  /* cache-usage monitor */
+#define MPAMF_MBWUMON_IDR	0x0090  /* mem-bw usage monitor */
+#define MPAMF_PARTID_NRW_IDR	0x0050  /* partid-narrowing */
+#define MPAMF_IIDR		0x0018  /* implementer id register */
+#define MPAMF_AIDR		0x0020  /* architectural id register */
+
+/* Configuration and Status Register offsets in the memory mapped page */
+#define MPAMCFG_PART_SEL	0x0100  /* partid to configure: */

":" seems unnecessary.

drivers/resctrl/mpam_internal.h extern struct list_head mpam_classes; · line 191

Quoted 12 lines
drivers/resctrl/mpam_internal.h +12 -0
+#define MPAMCFG_CPBM		0x1000  /* cache-portion config */
+#define MPAMCFG_CMAX		0x0108  /* cache-capacity config */
+#define MPAMCFG_CMIN		0x0110  /* cache-capacity config */
+#define MPAMCFG_CASSOC		0x0118  /* cache-associativity config */
+#define MPAMCFG_MBW_MIN		0x0200  /* min mem-bw config */
+#define MPAMCFG_MBW_MAX		0x0208  /* max mem-bw config */
+#define MPAMCFG_MBW_WINWD	0x0220  /* mem-bw accounting window config */
+#define MPAMCFG_MBW_PBM		0x2000  /* mem-bw portion bitmap config */
+#define MPAMCFG_PRI		0x0400  /* priority partitioning config */
+#define MPAMCFG_MBW_PROP	0x0500  /* mem-bw stride config */
+#define MPAMCFG_INTPARTID	0x0600  /* partid-narrowing config */
+

[...]

Thanks,
Gavin

author maintainer
#77 lore raw

PATCH v2 10/29 arm_mpam: Add cpuhp callbacks to probe MSC hardware

In reply to James Morse

Because an MSC can only by accessed from the CPUs in its cpu-affinity set we need to be running on one of those CPUs to probe the MSC hardware.

Do this work in the cpuhp callback. Probing the hardware will only happen before MPAM is enabled, walk all the MSCs and probe those we can reach that haven't already been probed as each CPU's online call is made.

This adds the low-level MSC register accessors.

Once all MSCs reported by the firmware have been probed from a CPU in their respective cpu-affinity set, the probe-time cpuhp callbacks are replaced. The replacement callbacks will ultimately need to handle save/restore of the runtime MSC state across power transitions, but for now there is nothing to do in them: so do nothing.

The architecture's context switch code will be enabled by a static-key, this can be set by mpam_enable(), but must be done from process context, not a cpuhp callback because both take the cpuhp lock. Whenever a new MSC has been probed, the mpam_enable() work is scheduled to test if all the MSCs have been probed. If probing fails, mpam_disable() is scheduled to unregister the cpuhp callbacks and free memory.

---
Changes since v1:
* Removed register bounds check. If the firmware tables are wrong the resulting translation fault should be enough to debug this.
* Removed '&' in front of a function pointer.
* Pulled mpam_disable() into this patch.
* Disable mpam when probing fails to avoid extra work on broken platforms.
* Added mpam_disbale_reason as there are now two non-debug reasons for this to happen.

---
 drivers/resctrl/mpam_devices.c  | 173 +++++++++++++++++++++++++++++++-
 drivers/resctrl/mpam_internal.h |   5 +
 2 files changed, 177 insertions(+), 1 deletion(-)
drivers/resctrl/mpam_devices.c +172 -1
index c7f4981b3545..c265376d936b 100644
@@ -4,6 +4,7 @@
 #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
 
 #include <linux/acpi.h>
+#include <linux/atomic.h>
 #include <linux/arm_mpam.h>
 #include <linux/cacheinfo.h>
 #include <linux/cpu.h>
@@ -19,6 +20,7 @@
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/types.h>
+#include <linux/workqueue.h>
 
 #include "mpam_internal.h"
 
@@ -38,6 +40,22 @@ struct srcu_struct mpam_srcu;
  */
 static atomic_t mpam_num_msc;
 
+static int mpam_cpuhp_state;
+static DEFINE_MUTEX(mpam_cpuhp_state_lock);
+
+/*
+ * mpam is enabled once all devices have been probed from CPU online callbacks,
+ * scheduled via this work_struct. If access to an MSC depends on a CPU that
+ * was not brought online at boot, this can happen surprisingly late.
+ */
+static DECLARE_WORK(mpam_enable_work, &mpam_enable);
+
+/*
+ * All mpam error interrupts indicate a software bug. On receipt, disable the
+ * driver.
+ */
+static DECLARE_WORK(mpam_broken_work, &mpam_disable);
+
 /*
  * An MSC is a physical container for controls and monitors, each identified by
  * their RIS index. These share a base-address, interrupts and some MMIO
@@ -77,6 +95,24 @@ LIST_HEAD(mpam_classes);
 /* List of all objects that can be free()d after synchronise_srcu() */
 static LLIST_HEAD(mpam_garbage);
 
+/* When mpam is disabled, the printed reason to aid debugging */
+static char *mpam_disable_reason;
+
+static u32 __mpam_read_reg(struct mpam_msc *msc, u16 reg)
+{
+	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
+
+	return readl_relaxed(msc->mapped_hwpage + reg);
+}
+
+static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg)
+{
+	lockdep_assert_held_once(&msc->part_sel_lock);
+	return __mpam_read_reg(msc, reg);
+}
+
+#define mpam_read_partsel_reg(msc, reg)        _mpam_read_partsel_reg(msc, MPAMF_##reg)
+
 #define init_garbage(x)	init_llist_node(&(x)->garbage.llist)
 
 static struct mpam_vmsc *
@@ -434,6 +470,86 @@ int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
 	return err;
 }
 
+static int mpam_msc_hw_probe(struct mpam_msc *msc)
+{
+	u64 idr;
+	struct device *dev = &msc->pdev->dev;
+
+	lockdep_assert_held(&msc->probe_lock);
+
+	idr = __mpam_read_reg(msc, MPAMF_AIDR);
+	if ((idr & MPAMF_AIDR_ARCH_MAJOR_REV) != MPAM_ARCHITECTURE_V1) {
+		dev_err_once(dev, "MSC does not match MPAM architecture v1.x\n");
+		return -EIO;
+	}
+
+	msc->probed = true;
+
+	return 0;
+}
+
+static int mpam_cpu_online(unsigned int cpu)
+{
+	return 0;
+}
+
+/* Before mpam is enabled, try to probe new MSC */
+static int mpam_discovery_cpu_online(unsigned int cpu)
+{
+	int err = 0;
+	struct mpam_msc *msc;
+	bool new_device_probed = false;
+
+	guard(srcu)(&mpam_srcu);
+	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
+				 srcu_read_lock_held(&mpam_srcu)) {
+		if (!cpumask_test_cpu(cpu, &msc->accessibility))
+			continue;
+
+		mutex_lock(&msc->probe_lock);
+		if (!msc->probed)
+			err = mpam_msc_hw_probe(msc);
+		mutex_unlock(&msc->probe_lock);
+
+		if (!err)
+			new_device_probed = true;
+		else
+			break;
+	}
+
+	if (new_device_probed && !err)
+		schedule_work(&mpam_enable_work);
+	if (err) {
+		mpam_disable_reason = "error during probing";
+		schedule_work(&mpam_broken_work);
+	}
+
+	return err;
+}
+
+static int mpam_cpu_offline(unsigned int cpu)
+{
+	return 0;
+}
+
+static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online),
+					  int (*offline)(unsigned int offline))
+{
+	mutex_lock(&mpam_cpuhp_state_lock);
+	if (mpam_cpuhp_state) {
+		cpuhp_remove_state(mpam_cpuhp_state);
+		mpam_cpuhp_state = 0;
+	}
+
+	mpam_cpuhp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mpam:online",
+					     online, offline);
+	if (mpam_cpuhp_state <= 0) {
+		pr_err("Failed to register cpuhp callbacks");
+		mpam_cpuhp_state = 0;
+	}
+	mutex_unlock(&mpam_cpuhp_state_lock);
+}
+
 /*
  * An MSC can control traffic from a set of CPUs, but may only be accessible
  * from a (hopefully wider) set of CPUs. The common reason for this is power
@@ -544,7 +660,7 @@ static int mpam_msc_drv_probe(struct platform_device *pdev)
 		mpam_msc_drv_remove(pdev);
 
 	if (!err && atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
-		pr_info("Discovered all MSC\n");
+		mpam_register_cpuhp_callbacks(mpam_discovery_cpu_online, NULL);
 
 	return err;
 }
@@ -557,6 +673,61 @@ static struct platform_driver mpam_msc_driver = {
 	.remove = mpam_msc_drv_remove,
 };
 
+static void mpam_enable_once(void)
+{
+	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline);
+
+	pr_info("MPAM enabled\n");
+}
+
+void mpam_disable(struct work_struct *ignored)
+{
+	struct mpam_msc *msc, *tmp;
+
+	mutex_lock(&mpam_cpuhp_state_lock);
+	if (mpam_cpuhp_state) {
+		cpuhp_remove_state(mpam_cpuhp_state);
+		mpam_cpuhp_state = 0;
+	}
+	mutex_unlock(&mpam_cpuhp_state_lock);
+
+	mutex_lock(&mpam_list_lock);
+	list_for_each_entry_safe(msc, tmp, &mpam_all_msc, all_msc_list)
+		mpam_msc_destroy(msc);
+	mutex_unlock(&mpam_list_lock);
+	mpam_free_garbage();
+
+	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
+}
+
+/*
+ * Enable mpam once all devices have been probed.
+ * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
+ * Also scheduled when new devices are probed when new CPUs come online.
+ */
+void mpam_enable(struct work_struct *work)
+{
+	static atomic_t once;
+	struct mpam_msc *msc;
+	bool all_devices_probed = true;
+
+	/* Have we probed all the hw devices? */
+	guard(srcu)(&mpam_srcu);
+	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
+				 srcu_read_lock_held(&mpam_srcu)) {
+		mutex_lock(&msc->probe_lock);
+		if (!msc->probed)
+			all_devices_probed = false;
+		mutex_unlock(&msc->probe_lock);
+
+		if (!all_devices_probed)
+			break;
+	}
+
+	if (all_devices_probed && !atomic_fetch_inc(&once))
+		mpam_enable_once();
+}
+
 static int __init mpam_msc_driver_init(void)
 {
 	if (!system_supports_mpam())
drivers/resctrl/mpam_internal.h +5 -0
index 109f03df46c2..d4f3febc7a50 100644
@@ -51,6 +51,7 @@ struct mpam_msc {
 	 * properties become read-only and the lists are protected by SRCU.
 	 */
 	struct mutex		probe_lock;
+	bool			probed;
 	unsigned long		ris_idxs;
 	u32			ris_max;
 
@@ -149,6 +150,10 @@ struct mpam_msc_ris {
 extern struct srcu_struct mpam_srcu;
 extern struct list_head mpam_classes;
 
+/* Scheduled work callback to enable mpam once all MSC have been probed */
+void mpam_enable(struct work_struct *work);
+void mpam_disable(struct work_struct *work);
+
 int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
 				   cpumask_t *affinity);
 
Signature · 1 line
2.39.5
Reviewed-by on v2 10/29
#78 lore raw

Re: [PATCH v2 10/29] arm_mpam: Add cpuhp callbacks to probe MSC hardware

Quoted 23 lines · On Wed, 10 Sep 2025 20:42:50 +0000 James Morse <[email protected]> wrote:

Because an MSC can only by accessed from the CPUs in its cpu-affinity set we need to be running on one of those CPUs to probe the MSC hardware.

Do this work in the cpuhp callback. Probing the hardware will only happen before MPAM is enabled, walk all the MSCs and probe those we can reach that haven't already been probed as each CPU's online call is made.

This adds the low-level MSC register accessors.

Once all MSCs reported by the firmware have been probed from a CPU in their respective cpu-affinity set, the probe-time cpuhp callbacks are replaced. The replacement callbacks will ultimately need to handle save/restore of the runtime MSC state across power transitions, but for now there is nothing to do in them: so do nothing.

The architecture's context switch code will be enabled by a static-key, this can be set by mpam_enable(), but must be done from process context, not a cpuhp callback because both take the cpuhp lock. Whenever a new MSC has been probed, the mpam_enable() work is scheduled to test if all the MSCs have been probed. If probing fails, mpam_disable() is scheduled to unregister the cpuhp callbacks and free memory.

Quoted 2 lines

Trivial suggestion inline. Either way

Quoted 13 lines
drivers/resctrl/mpam_devices.c +13 -0
+
+/* Before mpam is enabled, try to probe new MSC */
+static int mpam_discovery_cpu_online(unsigned int cpu)
+{
+	int err = 0;
+	struct mpam_msc *msc;
+	bool new_device_probed = false;
+
+	guard(srcu)(&mpam_srcu);
+	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
+				 srcu_read_lock_held(&mpam_srcu)) {
+		if (!cpumask_test_cpu(cpu, &msc->accessibility))
+			continue;

drivers/resctrl/mpam_devices.c int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx, · line 517

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+
+		mutex_lock(&msc->probe_lock);
+		if (!msc->probed)
+			err = mpam_msc_hw_probe(msc);
+		mutex_unlock(&msc->probe_lock);
+
+		if (!err)
+			new_device_probed = true;
+		else
+			break;

Unless this going to get more complex why not

		if (err)
			break;

		new_device_probed = true;
Quoted 7 lines
drivers/resctrl/mpam_devices.c +7 -0
+	}
+
+	if (new_device_probed && !err)
+		schedule_work(&mpam_enable_work);
+	if (err) {
+		mpam_disable_reason = "error during probing";
+		schedule_work(&mpam_broken_work);

drivers/resctrl/mpam_devices.c int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,

Quoted 10 lines
drivers/resctrl/mpam_devices.c +9 -0
+	}
+
+	return err;
+}
 
+static void mpam_enable_once(void)
+{
+	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline);
+
+	pr_info("MPAM enabled\n");

Feels too noisy given it should be easy enough to tell. pr_dbg() perhaps.

Quoted 1 line

+}

author maintainer
#79 lore raw

Re: [PATCH v2 10/29] arm_mpam: Add cpuhp callbacks to probe MSC hardware

Hi Jonathan,

Quoted 26 lines · Jonathan Cameron
Quoted 16 lines · On Wed, 10 Sep 2025 20:42:50 +0000 James Morse <[email protected]> wrote:

Because an MSC can only by accessed from the CPUs in its cpu-affinity set we need to be running on one of those CPUs to probe the MSC hardware.

Do this work in the cpuhp callback. Probing the hardware will only happen before MPAM is enabled, walk all the MSCs and probe those we can reach that haven't already been probed as each CPU's online call is made.

This adds the low-level MSC register accessors.

Once all MSCs reported by the firmware have been probed from a CPU in their respective cpu-affinity set, the probe-time cpuhp callbacks are replaced. The replacement callbacks will ultimately need to handle save/restore of the runtime MSC state across power transitions, but for now there is nothing to do in them: so do nothing.

Quoted 6 lines

The architecture's context switch code will be enabled by a static-key, this can be set by mpam_enable(), but must be done from process context, not a cpuhp callback because both take the cpuhp lock. Whenever a new MSC has been probed, the mpam_enable() work is scheduled to test if all the MSCs have been probed. If probing fails, mpam_disable() is scheduled to unregister the cpuhp callbacks and free memory.

Quoted 2 lines

Trivial suggestion inline. Either way

Thanks!

Quoted 25 lines
Quoted 12 lines
drivers/resctrl/mpam_devices.c +12 -0
+/* Before mpam is enabled, try to probe new MSC */
+static int mpam_discovery_cpu_online(unsigned int cpu)
+{
+	int err = 0;
+	struct mpam_msc *msc;
+	bool new_device_probed = false;
+
+	guard(srcu)(&mpam_srcu);
+	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
+				 srcu_read_lock_held(&mpam_srcu)) {
+		if (!cpumask_test_cpu(cpu, &msc->accessibility))
+			continue;

drivers/resctrl/mpam_devices.c int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx, · line 517

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+
+		mutex_lock(&msc->probe_lock);
+		if (!msc->probed)
+			err = mpam_msc_hw_probe(msc);
+		mutex_unlock(&msc->probe_lock);
+
+		if (!err)
+			new_device_probed = true;
+		else
+			break;

Unless this going to get more complex why not

Quoted 4 lines
		if (err)
			break;

		new_device_probed = true;

Sure - its been both simpler and more complex in the past!

Quoted 12 lines
Quoted 11 lines
drivers/resctrl/mpam_devices.c +11 -0
+	}
+
+	if (new_device_probed && !err)
+		schedule_work(&mpam_enable_work);
+	if (err) {
+		mpam_disable_reason = "error during probing";
+		schedule_work(&mpam_broken_work);
+	}
+
+	return err;
+}
Quoted 7 lines

drivers/resctrl/mpam_devices.c static struct platform_driver mpam_msc_driver = { · line 680

Quoted 5 lines
drivers/resctrl/mpam_devices.c +5 -0
+static void mpam_enable_once(void)
+{
+	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline);
+
+	pr_info("MPAM enabled\n");

Feels too noisy given it should be easy enough to tell. pr_dbg() perhaps.

I was aiming for the driver to only print one thing - once all the hardware has been probed. Once the driver is assembled, this prints the number of PARTID/PMG that were discovered as the system wide limits.

The reason to print something is that if you see this message, but don't have resctrl appear in /proc/filesystems - its never going to appear because the resctrl glue code couldn't find anything it could use. As this isn't an error, so nothing gets printed in this case.
This is the most common complaint I get - "our platform doesn't look like a Xeon - why doesn't resctrl work with it?"

It also matters for other requesters, like the SMMU. If they probe after this point, they can't reduce the PARTID/PMG range - and may get an error and have their MPAM abilities disabled. Having an entry in the boot log makes this easier to debug.

The alternative would be to keep track of what the driver is up to, and expose that through debugfs - but information that only exists for debug purposes is likely to be wrong. It also doesn't help work out what order different drivers tried to probe in.

Thanks,

James

maintainer Reviewed-by on v2 10/29
#80 lore raw

Re: [PATCH v2 10/29] arm_mpam: Add cpuhp callbacks to probe MSC hardware

In reply to James Morse

Hi James,

Quoted 25 lines · James Morse

Because an MSC can only by accessed from the CPUs in its cpu-affinity set we need to be running on one of those CPUs to probe the MSC hardware.

Do this work in the cpuhp callback. Probing the hardware will only happen before MPAM is enabled, walk all the MSCs and probe those we can reach that haven't already been probed as each CPU's online call is made.

This adds the low-level MSC register accessors.

Once all MSCs reported by the firmware have been probed from a CPU in their respective cpu-affinity set, the probe-time cpuhp callbacks are replaced. The replacement callbacks will ultimately need to handle save/restore of the runtime MSC state across power transitions, but for now there is nothing to do in them: so do nothing.

The architecture's context switch code will be enabled by a static-key, this can be set by mpam_enable(), but must be done from process context, not a cpuhp callback because both take the cpuhp lock. Whenever a new MSC has been probed, the mpam_enable() work is scheduled to test if all the MSCs have been probed. If probing fails, mpam_disable() is scheduled to unregister the cpuhp callbacks and free memory.

Quoted 9 lines

---
Changes since v1:
* Removed register bounds check. If the firmware tables are wrong the resulting translation fault should be enough to debug this.
* Removed '&' in front of a function pointer.
* Pulled mpam_disable() into this patch.
* Disable mpam when probing fails to avoid extra work on broken platforms.
* Added mpam_disbale_reason as there are now two non-debug reasons for this to happen.

Looks good to me.

Thanks,

Ben

author maintainer
#81 lore raw

Re: [PATCH v2 10/29] arm_mpam: Add cpuhp callbacks to probe MSC hardware

Hi Ben,

Quoted 36 lines · Ben Horgan
Quoted 25 lines · James Morse

Because an MSC can only by accessed from the CPUs in its cpu-affinity set we need to be running on one of those CPUs to probe the MSC hardware.

Do this work in the cpuhp callback. Probing the hardware will only happen before MPAM is enabled, walk all the MSCs and probe those we can reach that haven't already been probed as each CPU's online call is made.

This adds the low-level MSC register accessors.

Once all MSCs reported by the firmware have been probed from a CPU in their respective cpu-affinity set, the probe-time cpuhp callbacks are replaced. The replacement callbacks will ultimately need to handle save/restore of the runtime MSC state across power transitions, but for now there is nothing to do in them: so do nothing.

The architecture's context switch code will be enabled by a static-key, this can be set by mpam_enable(), but must be done from process context, not a cpuhp callback because both take the cpuhp lock. Whenever a new MSC has been probed, the mpam_enable() work is scheduled to test if all the MSCs have been probed. If probing fails, mpam_disable() is scheduled to unregister the cpuhp callbacks and free memory.

Quoted 9 lines

---
Changes since v1:
* Removed register bounds check. If the firmware tables are wrong the resulting translation fault should be enough to debug this.
* Removed '&' in front of a function pointer.
* Pulled mpam_disable() into this patch.
* Disable mpam when probing fails to avoid extra work on broken platforms.
* Added mpam_disbale_reason as there are now two non-debug reasons for this to happen.

Quoted 3 lines

Looks good to me.

Thanks!

James

reviewer Reviewed-by on v2 10/29
#82 lore raw

Re: [PATCH v2 10/29] arm_mpam: Add cpuhp callbacks to probe MSC hardware

In reply to James Morse
Quoted 23 lines · James Morse

Because an MSC can only by accessed from the CPUs in its cpu-affinity set we need to be running on one of those CPUs to probe the MSC hardware.

Do this work in the cpuhp callback. Probing the hardware will only happen before MPAM is enabled, walk all the MSCs and probe those we can reach that haven't already been probed as each CPU's online call is made.

This adds the low-level MSC register accessors.

Once all MSCs reported by the firmware have been probed from a CPU in their respective cpu-affinity set, the probe-time cpuhp callbacks are replaced. The replacement callbacks will ultimately need to handle save/restore of the runtime MSC state across power transitions, but for now there is nothing to do in them: so do nothing.

The architecture's context switch code will be enabled by a static-key, this can be set by mpam_enable(), but must be done from process context, not a cpuhp callback because both take the cpuhp lock. Whenever a new MSC has been probed, the mpam_enable() work is scheduled to test if all the MSCs have been probed. If probing fails, mpam_disable() is scheduled to unregister the cpuhp callbacks and free memory.

Quoted 2 lines

Thanks.

-Fenghua

Reviewed-by on v2 10/29
#83 lore raw

Re: [PATCH v2 10/29] arm_mpam: Add cpuhp callbacks to probe MSC hardware

In reply to James Morse
Quoted 34 lines · James Morse

Because an MSC can only by accessed from the CPUs in its cpu-affinity set we need to be running on one of those CPUs to probe the MSC hardware.

Do this work in the cpuhp callback. Probing the hardware will only happen before MPAM is enabled, walk all the MSCs and probe those we can reach that haven't already been probed as each CPU's online call is made.

This adds the low-level MSC register accessors.

Once all MSCs reported by the firmware have been probed from a CPU in their respective cpu-affinity set, the probe-time cpuhp callbacks are replaced. The replacement callbacks will ultimately need to handle save/restore of the runtime MSC state across power transitions, but for now there is nothing to do in them: so do nothing.

The architecture's context switch code will be enabled by a static-key, this can be set by mpam_enable(), but must be done from process context, not a cpuhp callback because both take the cpuhp lock. Whenever a new MSC has been probed, the mpam_enable() work is scheduled to test if all the MSCs have been probed. If probing fails, mpam_disable() is scheduled to unregister the cpuhp callbacks and free memory.

---
Changes since v1:
* Removed register bounds check. If the firmware tables are wrong the resulting translation fault should be enough to debug this.
* Removed '&' in front of a function pointer.
* Pulled mpam_disable() into this patch.
* Disable mpam when probing fails to avoid extra work on broken platforms.
* Added mpam_disbale_reason as there are now two non-debug reasons for this to happen.

Quoted 5 lines
---
  drivers/resctrl/mpam_devices.c  | 173 +++++++++++++++++++++++++++++++-
  drivers/resctrl/mpam_internal.h |   5 +
  2 files changed, 177 insertions(+), 1 deletion(-)
author maintainer
#84 lore raw

PATCH v2 11/29 arm_mpam: Probe hardware to find the supported partid/pmg values

In reply to James Morse

CPUs can generate traffic with a range of PARTID and PMG values, but each MSC may also have its own maximum size for these fields. Before MPAM can be used, the driver needs to probe each RIS on each MSC, to find the system-wide smallest value that can be used. The limits from requestors (e.g. CPUs) also need taking into account.

While doing this, RIS entries that firmware didn't describe are created under MPAM_CLASS_UNKNOWN.

While we're here, implement the mpam_register_requestor() call for the arch code to register the CPU limits. Future callers of this will tell us about the SMMU and ITS.

---
Changes since v1:
* Change to lock ordering now that the list-lock mutex isn't held from the cpuhp call.
* Removed irq-unmaksed assert in requestor register.
* Changed captialisation in print message.

---
 drivers/resctrl/mpam_devices.c  | 150 +++++++++++++++++++++++++++++++-
 drivers/resctrl/mpam_internal.h |   6 ++
 include/linux/arm_mpam.h        |  14 +++
 3 files changed, 169 insertions(+), 1 deletion(-)
drivers/resctrl/mpam_devices.c +149 -1
index c265376d936b..24dc81c15ec8 100644
@@ -6,6 +6,7 @@
 #include <linux/acpi.h>
 #include <linux/atomic.h>
 #include <linux/arm_mpam.h>
+#include <linux/bitfield.h>
 #include <linux/cacheinfo.h>
 #include <linux/cpu.h>
 #include <linux/cpumask.h>
@@ -43,6 +44,15 @@ static atomic_t mpam_num_msc;
 static int mpam_cpuhp_state;
 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
 
+/*
+ * The smallest common values for any CPU or MSC in the system.
+ * Generating traffic outside this range will result in screaming interrupts.
+ */
+u16 mpam_partid_max;
+u8 mpam_pmg_max;
+static bool partid_max_init, partid_max_published;
+static DEFINE_SPINLOCK(partid_max_lock);
+
 /*
  * mpam is enabled once all devices have been probed from CPU online callbacks,
  * scheduled via this work_struct. If access to an MSC depends on a CPU that
@@ -113,6 +123,72 @@ static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg)
 
 #define mpam_read_partsel_reg(msc, reg)        _mpam_read_partsel_reg(msc, MPAMF_##reg)
 
+static void __mpam_write_reg(struct mpam_msc *msc, u16 reg, u32 val)
+{
+	WARN_ON_ONCE(reg + sizeof(u32) > msc->mapped_hwpage_sz);
+	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
+
+	writel_relaxed(val, msc->mapped_hwpage + reg);
+}
+
+static inline void _mpam_write_partsel_reg(struct mpam_msc *msc, u16 reg, u32 val)
+{
+	lockdep_assert_held_once(&msc->part_sel_lock);
+	__mpam_write_reg(msc, reg, val);
+}
+#define mpam_write_partsel_reg(msc, reg, val)  _mpam_write_partsel_reg(msc, MPAMCFG_##reg, val)
+
+static u64 mpam_msc_read_idr(struct mpam_msc *msc)
+{
+	u64 idr_high = 0, idr_low;
+
+	lockdep_assert_held(&msc->part_sel_lock);
+
+	idr_low = mpam_read_partsel_reg(msc, IDR);
+	if (FIELD_GET(MPAMF_IDR_EXT, idr_low))
+		idr_high = mpam_read_partsel_reg(msc, IDR + 4);
+
+	return (idr_high << 32) | idr_low;
+}
+
+static void __mpam_part_sel_raw(u32 partsel, struct mpam_msc *msc)
+{
+	lockdep_assert_held(&msc->part_sel_lock);
+
+	mpam_write_partsel_reg(msc, PART_SEL, partsel);
+}
+
+static void __mpam_part_sel(u8 ris_idx, u16 partid, struct mpam_msc *msc)
+{
+	u32 partsel = FIELD_PREP(MPAMCFG_PART_SEL_RIS, ris_idx) |
+		      FIELD_PREP(MPAMCFG_PART_SEL_PARTID_SEL, partid);
+
+	__mpam_part_sel_raw(partsel, msc);
+}
+
+int mpam_register_requestor(u16 partid_max, u8 pmg_max)
+{
+	int err = 0;
+
+	spin_lock(&partid_max_lock);
+	if (!partid_max_init) {
+		mpam_partid_max = partid_max;
+		mpam_pmg_max = pmg_max;
+		partid_max_init = true;
+	} else if (!partid_max_published) {
+		mpam_partid_max = min(mpam_partid_max, partid_max);
+		mpam_pmg_max = min(mpam_pmg_max, pmg_max);
+	} else {
+		/* New requestors can't lower the values */
+		if (partid_max < mpam_partid_max || pmg_max < mpam_pmg_max)
+			err = -EBUSY;
+	}
+	spin_unlock(&partid_max_lock);
+
+	return err;
+}
+EXPORT_SYMBOL(mpam_register_requestor);
+
 #define init_garbage(x)	init_llist_node(&(x)->garbage.llist)
 
 static struct mpam_vmsc *
@@ -451,6 +527,7 @@ static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
 	cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
 	cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
 	list_add_rcu(&ris->vmsc_list, &vmsc->ris);
+	list_add_rcu(&ris->msc_list, &msc->ris);
 
 	return 0;
 }
@@ -470,9 +547,37 @@ int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
 	return err;
 }
 
+static struct mpam_msc_ris *mpam_get_or_create_ris(struct mpam_msc *msc,
+						   u8 ris_idx)
+{
+	int err;
+	struct mpam_msc_ris *ris, *found = ERR_PTR(-ENOENT);
+
+	lockdep_assert_held(&mpam_list_lock);
+
+	if (!test_bit(ris_idx, &msc->ris_idxs)) {
+		err = mpam_ris_create_locked(msc, ris_idx, MPAM_CLASS_UNKNOWN,
+					     0, 0);
+		if (err)
+			return ERR_PTR(err);
+	}
+
+	list_for_each_entry(ris, &msc->ris, msc_list) {
+		if (ris->ris_idx == ris_idx) {
+			found = ris;
+			break;
+		}
+	}
+
+	return found;
+}
+
 static int mpam_msc_hw_probe(struct mpam_msc *msc)
 {
 	u64 idr;
+	u16 partid_max;
+	u8 ris_idx, pmg_max;
+	struct mpam_msc_ris *ris;
 	struct device *dev = &msc->pdev->dev;
 
 	lockdep_assert_held(&msc->probe_lock);
@@ -483,6 +588,39 @@ static int mpam_msc_hw_probe(struct mpam_msc *msc)
 		return -EIO;
 	}
 
+	/* Grab an IDR value to find out how many RIS there are */
+	mutex_lock(&msc->part_sel_lock);
+	idr = mpam_msc_read_idr(msc);
+	mutex_unlock(&msc->part_sel_lock);
+	msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr);
+
+	/* Use these values so partid/pmg always starts with a valid value */
+	msc->partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
+	msc->pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
+
+	for (ris_idx = 0; ris_idx <= msc->ris_max; ris_idx++) {
+		mutex_lock(&msc->part_sel_lock);
+		__mpam_part_sel(ris_idx, 0, msc);
+		idr = mpam_msc_read_idr(msc);
+		mutex_unlock(&msc->part_sel_lock);
+
+		partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
+		pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
+		msc->partid_max = min(msc->partid_max, partid_max);
+		msc->pmg_max = min(msc->pmg_max, pmg_max);
+
+		mutex_lock(&mpam_list_lock);
+		ris = mpam_get_or_create_ris(msc, ris_idx);
+		mutex_unlock(&mpam_list_lock);
+		if (IS_ERR(ris))
+			return PTR_ERR(ris);
+	}
+
+	spin_lock(&partid_max_lock);
+	mpam_partid_max = min(mpam_partid_max, msc->partid_max);
+	mpam_pmg_max = min(mpam_pmg_max, msc->pmg_max);
+	spin_unlock(&partid_max_lock);
+
 	msc->probed = true;
 
 	return 0;
@@ -675,9 +813,18 @@ static struct platform_driver mpam_msc_driver = {
 
 static void mpam_enable_once(void)
 {
+	/*
+	 * Once the cpuhp callbacks have been changed, mpam_partid_max can no
+	 * longer change.
+	 */
+	spin_lock(&partid_max_lock);
+	partid_max_published = true;
+	spin_unlock(&partid_max_lock);
+
 	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline);
 
-	pr_info("MPAM enabled\n");
+	printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n",
+	       mpam_partid_max + 1, mpam_pmg_max + 1);
 }
 
 void mpam_disable(struct work_struct *ignored)
@@ -744,4 +891,5 @@ static int __init mpam_msc_driver_init(void)
 
 	return platform_driver_register(&mpam_msc_driver);
 }
+/* Must occur after arm64_mpam_register_cpus() from arch_initcall() */
 subsys_initcall(mpam_msc_driver_init);
drivers/resctrl/mpam_internal.h +6 -0
index d4f3febc7a50..828ce93c95d5 100644
@@ -52,6 +52,8 @@ struct mpam_msc {
 	 */
 	struct mutex		probe_lock;
 	bool			probed;
+	u16			partid_max;
+	u8			pmg_max;
 	unsigned long		ris_idxs;
 	u32			ris_max;
 
@@ -150,6 +152,10 @@ struct mpam_msc_ris {
 extern struct srcu_struct mpam_srcu;
 extern struct list_head mpam_classes;
 
+/* System wide partid/pmg values */
+extern u16 mpam_partid_max;
+extern u8 mpam_pmg_max;
+
 /* Scheduled work callback to enable mpam once all MSC have been probed */
 void mpam_enable(struct work_struct *work);
 void mpam_disable(struct work_struct *work);
include/linux/arm_mpam.h +14 -0
index 3206f5ddc147..cb6e6cfbea0b 100644
@@ -41,4 +41,18 @@ static inline int acpi_mpam_count_msc(void) { return -EINVAL; }
 int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
 		    enum mpam_class_types type, u8 class_id, int component_id);
 
+/**
+ * mpam_register_requestor() - Register a requestor with the MPAM driver
+ * @partid_max:		The maximum PARTID value the requestor can generate.
+ * @pmg_max:		The maximum PMG value the requestor can generate.
+ *
+ * Registers a requestor with the MPAM driver to ensure the chosen system-wide
+ * minimum PARTID and PMG values will allow the requestors features to be used.
+ *
+ * Returns an error if the registration is too late, and a larger PARTID/PMG
+ * value has been advertised to user-space. In this case the requestor should
+ * not use its MPAM features. Returns 0 on success.
+ */
+int mpam_register_requestor(u16 partid_max, u8 pmg_max);
+
 #endif /* __LINUX_ARM_MPAM_H */
Signature · 1 line
2.39.5
Reviewed-by on v2 11/29
#85 lore raw

Re: [PATCH v2 11/29] arm_mpam: Probe hardware to find the supported partid/pmg values

Quoted 6 lines · On Wed, 10 Sep 2025 20:42:51 +0000 James Morse <[email protected]> wrote:

CPUs can generate traffic with a range of PARTID and PMG values, but each MSC may also have its own maximum size for these fields. Before MPAM can be used, the driver needs to probe each RIS on each MSC, to find the system-wide smallest value that can be used. The limits from requestors (e.g. CPUs) also need taking into account.

Quoted 8 lines

While doing this, RIS entries that firmware didn't describe are created under MPAM_CLASS_UNKNOWN.

While we're here, implement the mpam_register_requestor() call for the arch code to register the CPU limits. Future callers of this will tell us about the SMMU and ITS.

Trivial stuff inline.

Quoted 16 lines

---
Changes since v1:
* Change to lock ordering now that the list-lock mutex isn't held from the cpuhp call.
* Removed irq-unmaksed assert in requestor register.
* Changed captialisation in print message.

---
 drivers/resctrl/mpam_devices.c  | 150 +++++++++++++++++++++++++++++++-
 drivers/resctrl/mpam_internal.h |   6 ++
 include/linux/arm_mpam.h        |  14 +++
 3 files changed, 169 insertions(+), 1 deletion(-)
drivers/resctrl/mpam_devices.c +0 -0
index c265376d936b..24dc81c15ec8 100644

drivers/resctrl/mpam_devices.c static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg) · line 173

Quoted 7 lines
drivers/resctrl/mpam_devices.c +5 -0
 
 
+int mpam_register_requestor(u16 partid_max, u8 pmg_max)
+{
+	int err = 0;
+
+	spin_lock(&partid_max_lock);

guard() perhaps so you can return early in the error pat and avoid need for local variable err.

Quoted 30 lines
drivers/resctrl/mpam_devices.c +25 -0
+	if (!partid_max_init) {
+		mpam_partid_max = partid_max;
+		mpam_pmg_max = pmg_max;
+		partid_max_init = true;
+	} else if (!partid_max_published) {
+		mpam_partid_max = min(mpam_partid_max, partid_max);
+		mpam_pmg_max = min(mpam_pmg_max, pmg_max);
+	} else {
+		/* New requestors can't lower the values */
+		if (partid_max < mpam_partid_max || pmg_max < mpam_pmg_max)
+			err = -EBUSY;
+	}
+	spin_unlock(&partid_max_lock);
+
+	return err;
+}
+EXPORT_SYMBOL(mpam_register_requestor);
@@ -470,9 +547,37 @@ int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
 	return err;
 }
 
+static struct mpam_msc_ris *mpam_get_or_create_ris(struct mpam_msc *msc,
+						   u8 ris_idx)
+{
+	int err;
+	struct mpam_msc_ris *ris, *found = ERR_PTR(-ENOENT);
+
+	lockdep_assert_held(&mpam_list_lock);
+

drivers/resctrl/mpam_devices.c int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx, · line 567

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+	if (!test_bit(ris_idx, &msc->ris_idxs)) {
+		err = mpam_ris_create_locked(msc, ris_idx, MPAM_CLASS_UNKNOWN,
+					     0, 0);
+		if (err)
+			return ERR_PTR(err);
+	}
+
+	list_for_each_entry(ris, &msc->ris, msc_list) {
+		if (ris->ris_idx == ris_idx) {
+			found = ris;

I'd return ris;

Then can do return ERR_PTR(-ENOENT) below and not bother with found.

Ignore if this gets more complex later.

Quoted 14 lines
drivers/resctrl/mpam_devices.c +9 -0
+			break;
+		}
+	}
+
+	return found;
+}
 
@@ -675,9 +813,18 @@ static struct platform_driver mpam_msc_driver = {
 
 static void mpam_enable_once(void)
 {
+	/*
+	 * Once the cpuhp callbacks have been changed, mpam_partid_max can no
+	 * longer change.

drivers/resctrl/mpam_devices.c static struct platform_driver mpam_msc_driver = { · line 827

Quoted 10 lines
drivers/resctrl/mpam_devices.c +7 -1
+	 */
+	spin_lock(&partid_max_lock);
+	partid_max_published = true;
+	spin_unlock(&partid_max_lock);
+
 	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline);
 
-	pr_info("MPAM enabled\n");
+	printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n",
+	       mpam_partid_max + 1, mpam_pmg_max + 1);

Not sure why pr_info before and printk now.

Quoted 1 line
drivers/resctrl/mpam_devices.c +0 -0
 }
author maintainer
#86 lore raw

Re: [PATCH v2 11/29] arm_mpam: Probe hardware to find the supported partid/pmg values

Quoted 12 lines · Jonathan Cameron
Quoted 9 lines · On Wed, 10 Sep 2025 20:42:51 +0000 James Morse <[email protected]> wrote:

CPUs can generate traffic with a range of PARTID and PMG values, but each MSC may also have its own maximum size for these fields. Before MPAM can be used, the driver needs to probe each RIS on each MSC, to find the system-wide smallest value that can be used. The limits from requestors (e.g. CPUs) also need taking into account.

While doing this, RIS entries that firmware didn't describe are created under MPAM_CLASS_UNKNOWN.

Quoted 8 lines
Quoted 5 lines

While we're here, implement the mpam_register_requestor() call for the arch code to register the CPU limits. Future callers of this will tell us about the SMMU and ITS.

Trivial stuff inline.

Thanks!

Quoted 11 lines
Quoted 9 lines
 drivers/resctrl/mpam_devices.c  | 150 +++++++++++++++++++++++++++++++-
 drivers/resctrl/mpam_internal.h |   6 ++
 include/linux/arm_mpam.h        |  14 +++
 3 files changed, 169 insertions(+), 1 deletion(-)
drivers/resctrl/mpam_devices.c +0 -0
index c265376d936b..24dc81c15ec8 100644
Quoted 8 lines

drivers/resctrl/mpam_devices.c static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg) · line 173

Quoted 5 lines
drivers/resctrl/mpam_devices.c +5 -0
+int mpam_register_requestor(u16 partid_max, u8 pmg_max)
+{
+	int err = 0;
+
+	spin_lock(&partid_max_lock);

guard() perhaps so you can return early in the error pat and avoid need for local variable err.

Negh ... okay. I dislike the guard thing as its never clear when the lock is unlocked. I'm not a fan of spooky action at a distance!

Quoted 39 lines
Quoted 27 lines
drivers/resctrl/mpam_devices.c +25 -0
+	if (!partid_max_init) {
+		mpam_partid_max = partid_max;
+		mpam_pmg_max = pmg_max;
+		partid_max_init = true;
+	} else if (!partid_max_published) {
+		mpam_partid_max = min(mpam_partid_max, partid_max);
+		mpam_pmg_max = min(mpam_pmg_max, pmg_max);
+	} else {
+		/* New requestors can't lower the values */
+		if (partid_max < mpam_partid_max || pmg_max < mpam_pmg_max)
+			err = -EBUSY;
+	}
+	spin_unlock(&partid_max_lock);
+
+	return err;
+}
+EXPORT_SYMBOL(mpam_register_requestor);
@@ -470,9 +547,37 @@ int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
+static struct mpam_msc_ris *mpam_get_or_create_ris(struct mpam_msc *msc,
+						   u8 ris_idx)
+{
+	int err;
+	struct mpam_msc_ris *ris, *found = ERR_PTR(-ENOENT);
+
+	lockdep_assert_held(&mpam_list_lock);
+

drivers/resctrl/mpam_devices.c int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx, · line 564

Quoted 10 lines
drivers/resctrl/mpam_devices.c +10 -0
+	if (!test_bit(ris_idx, &msc->ris_idxs)) {
+		err = mpam_ris_create_locked(msc, ris_idx, MPAM_CLASS_UNKNOWN,
+					     0, 0);
+		if (err)
+			return ERR_PTR(err);
+	}
+
+	list_for_each_entry(ris, &msc->ris, msc_list) {
+		if (ris->ris_idx == ris_idx) {
+			found = ris;

I'd return ris;

Quoted 3 lines

Then can do return ERR_PTR(-ENOENT) below and not bother with found.

Ignore if this gets more complex later.

Thank - this is another relic of more complex locking... Fixed as you suggested.

Quoted 21 lines
Quoted 21 lines
drivers/resctrl/mpam_devices.c +14 -0
+			break;
+		}
+	}
+
+	return found;
+}
 
@@ -675,9 +813,18 @@ static struct platform_driver mpam_msc_driver = {
 
 static void mpam_enable_once(void)
 {
+	/*
+	 * Once the cpuhp callbacks have been changed, mpam_partid_max can no
+	 * longer change.
+	 */
+	spin_lock(&partid_max_lock);
+	partid_max_published = true;
+	spin_unlock(&partid_max_lock);
+
 	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline);
 
Quoted 5 lines

drivers/resctrl/mpam_devices.c static struct platform_driver mpam_msc_driver = { · line 827

Quoted 3 lines
drivers/resctrl/mpam_devices.c +2 -1
-	pr_info("MPAM enabled\n");
+	printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n",
+	       mpam_partid_max + 1, mpam_pmg_max + 1);

Not sure why pr_info before and printk now.

That looks like a conflict gone wrong!
Fixed.

Thanks,

James

maintainer Reviewed-by on v2 11/29
#87 lore raw

Re: [PATCH v2 11/29] arm_mpam: Probe hardware to find the supported partid/pmg values

In reply to James Morse

Hi James,

Quoted 20 lines · James Morse

CPUs can generate traffic with a range of PARTID and PMG values, but each MSC may also have its own maximum size for these fields. Before MPAM can be used, the driver needs to probe each RIS on each MSC, to find the system-wide smallest value that can be used. The limits from requestors (e.g. CPUs) also need taking into account.

While doing this, RIS entries that firmware didn't describe are created under MPAM_CLASS_UNKNOWN.

While we're here, implement the mpam_register_requestor() call for the arch code to register the CPU limits. Future callers of this will tell us about the SMMU and ITS.

---
Changes since v1:
* Change to lock ordering now that the list-lock mutex isn't held from the cpuhp call.
* Removed irq-unmaksed assert in requestor register.
* Changed captialisation in print message.

Quoted 5 lines
---
 drivers/resctrl/mpam_devices.c  | 150 +++++++++++++++++++++++++++++++-
 drivers/resctrl/mpam_internal.h |   6 ++
 include/linux/arm_mpam.h        |  14 +++
 3 files changed, 169 insertions(+), 1 deletion(-)

Looks good to me. I think Jonathan's comment on getting rid of the local variable, 'found', is worthwhile.

Thanks,

Ben

author maintainer
#88 lore raw

Re: [PATCH v2 11/29] arm_mpam: Probe hardware to find the supported partid/pmg values

Hi Ben,

Quoted 10 lines · Ben Horgan
Quoted 9 lines · James Morse

CPUs can generate traffic with a range of PARTID and PMG values, but each MSC may also have its own maximum size for these fields. Before MPAM can be used, the driver needs to probe each RIS on each MSC, to find the system-wide smallest value that can be used. The limits from requestors (e.g. CPUs) also need taking into account.

While doing this, RIS entries that firmware didn't describe are created under MPAM_CLASS_UNKNOWN.

Quoted 8 lines
Quoted 3 lines

While we're here, implement the mpam_register_requestor() call for the arch code to register the CPU limits. Future callers of this will tell us about the SMMU and ITS.

Looks good to me. I think Jonathan's comment on getting rid of the local variable, 'found', is worthwhile.

Thanks!

James

reviewer
#89 lore raw

Re: [PATCH v2 11/29] arm_mpam: Probe hardware to find the supported partid/pmg values

In reply to James Morse

Hi, James,

Quoted 14 lines · James Morse

CPUs can generate traffic with a range of PARTID and PMG values, but each MSC may also have its own maximum size for these fields. Before MPAM can be used, the driver needs to probe each RIS on each MSC, to find the system-wide smallest value that can be used. The limits from requestors (e.g. CPUs) also need taking into account.

While doing this, RIS entries that firmware didn't describe are created under MPAM_CLASS_UNKNOWN.

While we're here, implement the mpam_register_requestor() call for the arch code to register the CPU limits. Future callers of this will tell us about the SMMU and ITS.

[SNIP]

drivers/resctrl/mpam_devices.c static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg) · line 128

Quoted 7 lines
drivers/resctrl/mpam_devices.c +3 -0
@@ -113,6 +123,72 @@ static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg)
 
  #define mpam_read_partsel_reg(msc, reg)        _mpam_read_partsel_reg(msc, MPAMF_##reg)
 
+static void __mpam_write_reg(struct mpam_msc *msc, u16 reg, u32 val)
+{
+	WARN_ON_ONCE(reg + sizeof(u32) > msc->mapped_hwpage_sz);

if reg + 4 == msc->mapped_hwpage_sz, the register is out of boundary as well.

So the validation should be:

WARN_ON_ONCE(reg + sizeof(u32) >= msc->mapped_hwpage_sz);

[SNIP]

Thanks.

-Fenghua

author maintainer
#90 lore raw

PATCH v2 12/29 arm_mpam: Add helpers for managing the locking around the mon_sel registers

In reply to James Morse

The MSC MON_SEL register needs to be accessed from hardirq for the overflow interrupt, and when taking an IPI to access these registers on platforms where MSC are not accesible from every CPU. This makes an irqsave spinlock the obvious lock to protect these registers. On systems with SCMI mailboxes it must be able to sleep, meaning a mutex must be used. The SCMI platforms can't support an overflow interrupt.

Clearly these two can't exist for one MSC at the same time.

Add helpers for the MON_SEL locking. The outer lock must be taken in a pre-emptible context before the inner lock can be taken. On systems with SCMI mailboxes where the MON_SEL accesses must sleep - the inner lock will fail to be 'taken' if the caller is unable to sleep. This will allow callers to fail without having to explicitly check the interface type of each MSC.

---
Change since v1:
* Made accesses to outer_lock_held READ_ONCE() for torn values in the failure case.

---
 drivers/resctrl/mpam_devices.c  |  3 +--
 drivers/resctrl/mpam_internal.h | 37 +++++++++++++++++++++++++++++----
 2 files changed, 34 insertions(+), 6 deletions(-)
drivers/resctrl/mpam_devices.c +1 -2
index 24dc81c15ec8..a26b012452e2 100644
@@ -748,8 +748,7 @@ static int mpam_msc_drv_probe(struct platform_device *pdev)
 
 		mutex_init(&msc->probe_lock);
 		mutex_init(&msc->part_sel_lock);
-		mutex_init(&msc->outer_mon_sel_lock);
-		raw_spin_lock_init(&msc->inner_mon_sel_lock);
+		mpam_mon_sel_lock_init(msc);
 		msc->id = pdev->id;
 		msc->pdev = pdev;
 		INIT_LIST_HEAD_RCU(&msc->all_msc_list);
drivers/resctrl/mpam_internal.h +33 -4
index 828ce93c95d5..4cc44d4e21c4 100644
@@ -70,12 +70,17 @@ struct mpam_msc {
 
 	/*
 	 * mon_sel_lock protects access to the MSC hardware registers that are
-	 * affected by MPAMCFG_MON_SEL.
+	 * affected by MPAMCFG_MON_SEL, and the mbwu_state.
+	 * Access to mon_sel is needed from both process and interrupt contexts,
+	 * but is complicated by firmware-backed platforms that can't make any
+	 * access unless they can sleep.
+	 * Always use the mpam_mon_sel_lock() helpers.
+	 * Accessed to mon_sel need to be able to fail if they occur in the wrong
+	 * context.
 	 * If needed, take msc->probe_lock first.
 	 */
-	struct mutex		outer_mon_sel_lock;
-	raw_spinlock_t		inner_mon_sel_lock;
-	unsigned long		inner_mon_sel_flags;
+	raw_spinlock_t		_mon_sel_lock;
+	unsigned long		_mon_sel_flags;
 
 	void __iomem		*mapped_hwpage;
 	size_t			mapped_hwpage_sz;
@@ -83,6 +88,30 @@ struct mpam_msc {
 	struct mpam_garbage	garbage;
 };
 
+/* Returning false here means accesses to mon_sel must fail and report an error. */
+static inline bool __must_check mpam_mon_sel_lock(struct mpam_msc *msc)
+{
+	WARN_ON_ONCE(msc->iface != MPAM_IFACE_MMIO);
+
+	raw_spin_lock_irqsave(&msc->_mon_sel_lock, msc->_mon_sel_flags);
+	return true;
+}
+
+static inline void mpam_mon_sel_unlock(struct mpam_msc *msc)
+{
+	raw_spin_unlock_irqrestore(&msc->_mon_sel_lock, msc->_mon_sel_flags);
+}
+
+static inline void mpam_mon_sel_lock_held(struct mpam_msc *msc)
+{
+	lockdep_assert_held_once(&msc->_mon_sel_lock);
+}
+
+static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc)
+{
+	raw_spin_lock_init(&msc->_mon_sel_lock);
+}
+
 struct mpam_class {
 	/* mpam_components in this class */
 	struct list_head	components;
Signature · 1 line
2.39.5
#91 lore raw

Re: [PATCH v2 12/29] arm_mpam: Add helpers for managing the locking around the mon_sel registers

Quoted 7 lines · On Wed, 10 Sep 2025 20:42:52 +0000 James Morse <[email protected]> wrote:

The MSC MON_SEL register needs to be accessed from hardirq for the overflow interrupt, and when taking an IPI to access these registers on platforms where MSC are not accesible from every CPU. This makes an irqsave spinlock the obvious lock to protect these registers. On systems with SCMI mailboxes it must be able to sleep, meaning a mutex must be used. The SCMI platforms can't support an overflow interrupt.

Quoted 8 lines

Clearly these two can't exist for one MSC at the same time.

Add helpers for the MON_SEL locking. The outer lock must be taken in a pre-emptible context before the inner lock can be taken. On systems with SCMI mailboxes where the MON_SEL accesses must sleep - the inner lock will fail to be 'taken' if the caller is unable to sleep. This will allow callers to fail without having to explicitly check the interface type of each MSC.

Comments talk about outer locks, but not actually seeing that in the current code.

Quoted 2 lines

---

Quoted 3 lines

Change since v1:
* Made accesses to outer_lock_held READ_ONCE() for torn values in the failure case.

Comment on wrong patch? No READ_ONCE() in here.

Quoted 76 lines
---
 drivers/resctrl/mpam_devices.c  |  3 +--
 drivers/resctrl/mpam_internal.h | 37 +++++++++++++++++++++++++++++----
 2 files changed, 34 insertions(+), 6 deletions(-)
drivers/resctrl/mpam_devices.c +1 -2
index 24dc81c15ec8..a26b012452e2 100644
@@ -748,8 +748,7 @@ static int mpam_msc_drv_probe(struct platform_device *pdev)
 
 		mutex_init(&msc->probe_lock);
 		mutex_init(&msc->part_sel_lock);
-		mutex_init(&msc->outer_mon_sel_lock);
-		raw_spin_lock_init(&msc->inner_mon_sel_lock);
+		mpam_mon_sel_lock_init(msc);
 		msc->id = pdev->id;
 		msc->pdev = pdev;
 		INIT_LIST_HEAD_RCU(&msc->all_msc_list);
drivers/resctrl/mpam_internal.h +33 -4
index 828ce93c95d5..4cc44d4e21c4 100644
@@ -70,12 +70,17 @@ struct mpam_msc {
 
 	/*
 	 * mon_sel_lock protects access to the MSC hardware registers that are
-	 * affected by MPAMCFG_MON_SEL.
+	 * affected by MPAMCFG_MON_SEL, and the mbwu_state.
+	 * Access to mon_sel is needed from both process and interrupt contexts,
+	 * but is complicated by firmware-backed platforms that can't make any
+	 * access unless they can sleep.
+	 * Always use the mpam_mon_sel_lock() helpers.
+	 * Accessed to mon_sel need to be able to fail if they occur in the wrong
+	 * context.
 	 * If needed, take msc->probe_lock first.
 	 */
-	struct mutex		outer_mon_sel_lock;
-	raw_spinlock_t		inner_mon_sel_lock;
-	unsigned long		inner_mon_sel_flags;
+	raw_spinlock_t		_mon_sel_lock;
+	unsigned long		_mon_sel_flags;
 
 	void __iomem		*mapped_hwpage;
 	size_t			mapped_hwpage_sz;
@@ -83,6 +88,30 @@ struct mpam_msc {
 	struct mpam_garbage	garbage;
 };
 
+/* Returning false here means accesses to mon_sel must fail and report an error. */
+static inline bool __must_check mpam_mon_sel_lock(struct mpam_msc *msc)
+{
+	WARN_ON_ONCE(msc->iface != MPAM_IFACE_MMIO);
+
+	raw_spin_lock_irqsave(&msc->_mon_sel_lock, msc->_mon_sel_flags);
+	return true;
+}
+
+static inline void mpam_mon_sel_unlock(struct mpam_msc *msc)
+{
+	raw_spin_unlock_irqrestore(&msc->_mon_sel_lock, msc->_mon_sel_flags);
+}
+
+static inline void mpam_mon_sel_lock_held(struct mpam_msc *msc)
+{
+	lockdep_assert_held_once(&msc->_mon_sel_lock);
+}
+
+static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc)
+{
+	raw_spin_lock_init(&msc->_mon_sel_lock);
+}
+
 struct mpam_class {
 	/* mpam_components in this class */
 	struct list_head	components;
author maintainer
#92 lore raw

Re: [PATCH v2 12/29] arm_mpam: Add helpers for managing the locking around the mon_sel registers

Hi Jonathan,

Quoted 12 lines · Jonathan Cameron
Quoted 9 lines · On Wed, 10 Sep 2025 20:42:52 +0000 James Morse <[email protected]> wrote:

The MSC MON_SEL register needs to be accessed from hardirq for the overflow interrupt, and when taking an IPI to access these registers on platforms where MSC are not accesible from every CPU. This makes an irqsave spinlock the obvious lock to protect these registers. On systems with SCMI mailboxes it must be able to sleep, meaning a mutex must be used. The SCMI platforms can't support an overflow interrupt.

Clearly these two can't exist for one MSC at the same time.

Quoted 8 lines
Quoted 6 lines

Add helpers for the MON_SEL locking. The outer lock must be taken in a pre-emptible context before the inner lock can be taken. On systems with SCMI mailboxes where the MON_SEL accesses must sleep - the inner lock will fail to be 'taken' if the caller is unable to sleep. This will allow callers to fail without having to explicitly check the interface type of each MSC.

Comments talk about outer locks, but not actually seeing that in the current code.

Ugh - I squashed them all together because without the DT support the DT:SCMI support ceases to be relevant, the ACPI PCC support isn't here yet, and Dave complained this was complex. I forgot to rewrite the commit message!

The last paragraph is rewritten as:
------------%<------------
Add helpers for the MON_SEL locking. For now, use a irqsave spinlock and only support 'real' MMIO platforms.

In the future this lock will be split in two allowing SCMI/PCC platforms to take a mutex. Because there are contexts where the SCMI/PCC platforms can't make an access, mpam_mon_sel_lock() needs to be able to fail. Do this now, so that all the error handling on these paths is present. This allows the relevant paths to fail if they are needed on a platform where this isn't possible, instead of having to make explicit checks of the interface type.
------------%<------------

It took invasive changes to make the control path safe for these firmware backed platforms. I really don't want to 'simplify' it pretending they don't exist, to then spend the following month retrofitting this.
I expect the firmware backed platforms to expose one or two global MSC, so they should never hit a case where a mon_sel register access was sent via IPI.

Thanks,

James

maintainer
#93 lore raw

Re: [PATCH v2 12/29] arm_mpam: Add helpers for managing the locking around the mon_sel registers

In reply to James Morse

Hi James,

Quoted 54 lines · James Morse

The MSC MON_SEL register needs to be accessed from hardirq for the overflow interrupt, and when taking an IPI to access these registers on platforms where MSC are not accesible from every CPU. This makes an irqsave spinlock the obvious lock to protect these registers. On systems with SCMI mailboxes it must be able to sleep, meaning a mutex must be used. The SCMI platforms can't support an overflow interrupt.

Clearly these two can't exist for one MSC at the same time.

Add helpers for the MON_SEL locking. The outer lock must be taken in a pre-emptible context before the inner lock can be taken. On systems with SCMI mailboxes where the MON_SEL accesses must sleep - the inner lock will fail to be 'taken' if the caller is unable to sleep. This will allow callers to fail without having to explicitly check the interface type of each MSC.

---
Change since v1:
* Made accesses to outer_lock_held READ_ONCE() for torn values in the failure case.

---
 drivers/resctrl/mpam_devices.c  |  3 +--
 drivers/resctrl/mpam_internal.h | 37 +++++++++++++++++++++++++++++----
 2 files changed, 34 insertions(+), 6 deletions(-)
drivers/resctrl/mpam_devices.c +1 -2
index 24dc81c15ec8..a26b012452e2 100644
@@ -748,8 +748,7 @@ static int mpam_msc_drv_probe(struct platform_device *pdev)
 
 		mutex_init(&msc->probe_lock);
 		mutex_init(&msc->part_sel_lock);
-		mutex_init(&msc->outer_mon_sel_lock);
-		raw_spin_lock_init(&msc->inner_mon_sel_lock);
+		mpam_mon_sel_lock_init(msc);
 		msc->id = pdev->id;
 		msc->pdev = pdev;
 		INIT_LIST_HEAD_RCU(&msc->all_msc_list);
drivers/resctrl/mpam_internal.h +5 -1
index 828ce93c95d5..4cc44d4e21c4 100644
@@ -70,12 +70,17 @@ struct mpam_msc {
 
 	/*
 	 * mon_sel_lock protects access to the MSC hardware registers that are
-	 * affected by MPAMCFG_MON_SEL.
+	 * affected by MPAMCFG_MON_SEL, and the mbwu_state.
+	 * Access to mon_sel is needed from both process and interrupt contexts,
+	 * but is complicated by firmware-backed platforms that can't make any
+	 * access unless they can sleep.
+	 * Always use the mpam_mon_sel_lock() helpers.

drivers/resctrl/mpam_internal.h struct mpam_msc { · line 84

Quoted 10 lines
drivers/resctrl/mpam_internal.h +4 -3
+	 * Accessed to mon_sel need to be able to fail if they occur in the wrong
+	 * context.
 	 * If needed, take msc->probe_lock first.
 	 */
-	struct mutex		outer_mon_sel_lock;
-	raw_spinlock_t		inner_mon_sel_lock;
-	unsigned long		inner_mon_sel_flags;
+	raw_spinlock_t		_mon_sel_lock;
+	unsigned long		_mon_sel_flags;
 

These stale variables can be removed in the patch that introduced them, outer_mon_sel_lock, inner_mon_sel_lock, inner_mon_sel_flags. Jonathan has already pointed out the stale comment and paragraph in the commit message.

Thanks,

Ben

author maintainer
#94 lore raw

Re: [PATCH v2 12/29] arm_mpam: Add helpers for managing the locking around the mon_sel registers

Hi Ben,

Quoted 38 lines · Ben Horgan
Quoted 37 lines · James Morse

The MSC MON_SEL register needs to be accessed from hardirq for the overflow interrupt, and when taking an IPI to access these registers on platforms where MSC are not accesible from every CPU. This makes an irqsave spinlock the obvious lock to protect these registers. On systems with SCMI mailboxes it must be able to sleep, meaning a mutex must be used. The SCMI platforms can't support an overflow interrupt.

Clearly these two can't exist for one MSC at the same time.

Add helpers for the MON_SEL locking. The outer lock must be taken in a pre-emptible context before the inner lock can be taken. On systems with SCMI mailboxes where the MON_SEL accesses must sleep - the inner lock will fail to be 'taken' if the caller is unable to sleep. This will allow callers to fail without having to explicitly check the interface type of each MSC.

drivers/resctrl/mpam_internal.h +7 -4
index 828ce93c95d5..4cc44d4e21c4 100644
@@ -70,12 +70,17 @@ struct mpam_msc {
 
 	/*
 	 * mon_sel_lock protects access to the MSC hardware registers that are
-	 * affected by MPAMCFG_MON_SEL.
+	 * affected by MPAMCFG_MON_SEL, and the mbwu_state.
+	 * Access to mon_sel is needed from both process and interrupt contexts,
+	 * but is complicated by firmware-backed platforms that can't make any
+	 * access unless they can sleep.
+	 * Always use the mpam_mon_sel_lock() helpers.
+	 * Accessed to mon_sel need to be able to fail if they occur in the wrong
+	 * context.
 	 * If needed, take msc->probe_lock first.
 	 */
-	struct mutex		outer_mon_sel_lock;
-	raw_spinlock_t		inner_mon_sel_lock;
-	unsigned long		inner_mon_sel_flags;
Quoted 8 lines

drivers/resctrl/mpam_internal.h struct mpam_msc { · line 84

Quoted 3 lines
drivers/resctrl/mpam_internal.h +2 -0
+	raw_spinlock_t		_mon_sel_lock;
+	unsigned long		_mon_sel_flags;
 

These stale variables can be removed in the patch that introduced them, outer_mon_sel_lock, inner_mon_sel_lock, inner_mon_sel_flags. Jonathan has already pointed out the stale comment and paragraph in the commit message.

Yeah - I forgot to rewrite the commit message when I split this patch. I'll pull those earlier bits into the now:later patch that splits the locking up.

Thanks,

James

reviewer
#95 lore raw

Re: [PATCH v2 12/29] arm_mpam: Add helpers for managing the locking around the mon_sel registers

In reply to James Morse

Hi, James,

Quoted 87 lines · James Morse

The MSC MON_SEL register needs to be accessed from hardirq for the overflow interrupt, and when taking an IPI to access these registers on platforms where MSC are not accesible from every CPU. This makes an irqsave spinlock the obvious lock to protect these registers. On systems with SCMI mailboxes it must be able to sleep, meaning a mutex must be used. The SCMI platforms can't support an overflow interrupt.

Clearly these two can't exist for one MSC at the same time.

Add helpers for the MON_SEL locking. The outer lock must be taken in a pre-emptible context before the inner lock can be taken. On systems with SCMI mailboxes where the MON_SEL accesses must sleep - the inner lock will fail to be 'taken' if the caller is unable to sleep. This will allow callers to fail without having to explicitly check the interface type of each MSC.

---
Change since v1:
* Made accesses to outer_lock_held READ_ONCE() for torn values in the failure case.

---
  drivers/resctrl/mpam_devices.c  |  3 +--
  drivers/resctrl/mpam_internal.h | 37 +++++++++++++++++++++++++++++----
  2 files changed, 34 insertions(+), 6 deletions(-)
drivers/resctrl/mpam_devices.c +1 -2
index 24dc81c15ec8..a26b012452e2 100644
@@ -748,8 +748,7 @@ static int mpam_msc_drv_probe(struct platform_device *pdev)
 
  		mutex_init(&msc->probe_lock);
  		mutex_init(&msc->part_sel_lock);
-		mutex_init(&msc->outer_mon_sel_lock);
-		raw_spin_lock_init(&msc->inner_mon_sel_lock);
+		mpam_mon_sel_lock_init(msc);
  		msc->id = pdev->id;
  		msc->pdev = pdev;
  		INIT_LIST_HEAD_RCU(&msc->all_msc_list);
drivers/resctrl/mpam_internal.h +26 -4
index 828ce93c95d5..4cc44d4e21c4 100644
@@ -70,12 +70,17 @@ struct mpam_msc {
 
  	/*
  	 * mon_sel_lock protects access to the MSC hardware registers that are
-	 * affected by MPAMCFG_MON_SEL.
+	 * affected by MPAMCFG_MON_SEL, and the mbwu_state.
+	 * Access to mon_sel is needed from both process and interrupt contexts,
+	 * but is complicated by firmware-backed platforms that can't make any
+	 * access unless they can sleep.
+	 * Always use the mpam_mon_sel_lock() helpers.
+	 * Accessed to mon_sel need to be able to fail if they occur in the wrong
+	 * context.
  	 * If needed, take msc->probe_lock first.
  	 */
-	struct mutex		outer_mon_sel_lock;
-	raw_spinlock_t		inner_mon_sel_lock;
-	unsigned long		inner_mon_sel_flags;
+	raw_spinlock_t		_mon_sel_lock;
+	unsigned long		_mon_sel_flags;
 
  	void __iomem		*mapped_hwpage;
  	size_t			mapped_hwpage_sz;
@@ -83,6 +88,30 @@ struct mpam_msc {
  	struct mpam_garbage	garbage;
  };
 
+/* Returning false here means accesses to mon_sel must fail and report an error. */
+static inline bool __must_check mpam_mon_sel_lock(struct mpam_msc *msc)
+{
+	WARN_ON_ONCE(msc->iface != MPAM_IFACE_MMIO);
+
+	raw_spin_lock_irqsave(&msc->_mon_sel_lock, msc->_mon_sel_flags);
+	return true;
+}
+
+static inline void mpam_mon_sel_unlock(struct mpam_msc *msc)
+{
+	raw_spin_unlock_irqrestore(&msc->_mon_sel_lock, msc->_mon_sel_flags);
+}
+
+static inline void mpam_mon_sel_lock_held(struct mpam_msc *msc)
+{
+	lockdep_assert_held_once(&msc->_mon_sel_lock);

drivers/resctrl/mpam_internal.h struct mpam_msc { · line 117

Quoted 10 lines
drivers/resctrl/mpam_internal.h +7 -0
+}
+
+static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc)
+{
+	raw_spin_lock_init(&msc->_mon_sel_lock);
+}
+
  struct mpam_class {
  	/* mpam_components in this class */
  	struct list_head	components;

The inner and outer locks were defined and used in patch #7; but they are replaced by _mon_sel_lock in this patch.

I'm wondering if this patch should be merged into patch #7. This patch seems is redundant.

Thanks.

-Fenghua

author maintainer
#96 lore raw

PATCH v2 13/29 arm_mpam: Probe the hardware features resctrl supports

In reply to James Morse

Expand the probing support with the control and monitor types we can use with resctrl.

---
Changes since v1:
* added an underscore to a variable name.

Changes since RFC:
* Made mpam_ris_hw_probe_hw_nrdy() more in C.
* Added static assert on features bitmap size.

---
 drivers/resctrl/mpam_devices.c  | 151 ++++++++++++++++++++++++++++++++
 drivers/resctrl/mpam_internal.h |  53 +++++++++++
 2 files changed, 204 insertions(+)
drivers/resctrl/mpam_devices.c +151 -0
index a26b012452e2..ba8e8839cdc4 100644
@@ -138,6 +138,20 @@ static inline void _mpam_write_partsel_reg(struct mpam_msc *msc, u16 reg, u32 va
 }
 #define mpam_write_partsel_reg(msc, reg, val)  _mpam_write_partsel_reg(msc, MPAMCFG_##reg, val)
 
+static inline u32 _mpam_read_monsel_reg(struct mpam_msc *msc, u16 reg)
+{
+	mpam_mon_sel_lock_held(msc);
+	return __mpam_read_reg(msc, reg);
+}
+#define mpam_read_monsel_reg(msc, reg) _mpam_read_monsel_reg(msc, MSMON_##reg)
+
+static inline void _mpam_write_monsel_reg(struct mpam_msc *msc, u16 reg, u32 val)
+{
+	mpam_mon_sel_lock_held(msc);
+	__mpam_write_reg(msc, reg, val);
+}
+#define mpam_write_monsel_reg(msc, reg, val)   _mpam_write_monsel_reg(msc, MSMON_##reg, val)
+
 static u64 mpam_msc_read_idr(struct mpam_msc *msc)
 {
 	u64 idr_high = 0, idr_low;
@@ -572,6 +586,136 @@ static struct mpam_msc_ris *mpam_get_or_create_ris(struct mpam_msc *msc,
 	return found;
 }
 
+/*
+ * IHI009A.a has this nugget: "If a monitor does not support automatic behaviour
+ * of NRDY, software can use this bit for any purpose" - so hardware might not
+ * implement this - but it isn't RES0.
+ *
+ * Try and see what values stick in this bit. If we can write either value,
+ * its probably not implemented by hardware.
+ */
+static bool _mpam_ris_hw_probe_hw_nrdy(struct mpam_msc_ris * ris, u32 mon_reg)
+{
+	u32 now;
+	u64 mon_sel;
+	bool can_set, can_clear;
+	struct mpam_msc *msc = ris->vmsc->msc;
+
+	if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc)))
+		return false;
+
+	mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, 0) |
+		  FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx);
+	_mpam_write_monsel_reg(msc, mon_reg, mon_sel);
+
+	_mpam_write_monsel_reg(msc, mon_reg, MSMON___NRDY);
+	now = _mpam_read_monsel_reg(msc, mon_reg);
+	can_set = now & MSMON___NRDY;
+
+	_mpam_write_monsel_reg(msc, mon_reg, 0);
+	now = _mpam_read_monsel_reg(msc, mon_reg);
+	can_clear = !(now & MSMON___NRDY);
+	mpam_mon_sel_unlock(msc);
+
+	return (!can_set || !can_clear);
+}
+
+#define mpam_ris_hw_probe_hw_nrdy(_ris, _mon_reg)			\
+        _mpam_ris_hw_probe_hw_nrdy(_ris, MSMON_##_mon_reg)
+
+static void mpam_ris_hw_probe(struct mpam_msc_ris *ris)
+{
+	int err;
+	struct mpam_msc *msc = ris->vmsc->msc;
+	struct device *dev = &msc->pdev->dev;
+	struct mpam_props *props = &ris->props;
+
+	lockdep_assert_held(&msc->probe_lock);
+	lockdep_assert_held(&msc->part_sel_lock);
+
+	/* Cache Portion partitioning */
+	if (FIELD_GET(MPAMF_IDR_HAS_CPOR_PART, ris->idr)) {
+		u32 cpor_features = mpam_read_partsel_reg(msc, CPOR_IDR);
+
+		props->cpbm_wd = FIELD_GET(MPAMF_CPOR_IDR_CPBM_WD, cpor_features);
+		if (props->cpbm_wd)
+			mpam_set_feature(mpam_feat_cpor_part, props);
+	}
+
+	/* Memory bandwidth partitioning */
+	if (FIELD_GET(MPAMF_IDR_HAS_MBW_PART, ris->idr)) {
+		u32 mbw_features = mpam_read_partsel_reg(msc, MBW_IDR);
+
+		/* portion bitmap resolution */
+		props->mbw_pbm_bits = FIELD_GET(MPAMF_MBW_IDR_BWPBM_WD, mbw_features);
+		if (props->mbw_pbm_bits &&
+		    FIELD_GET(MPAMF_MBW_IDR_HAS_PBM, mbw_features))
+			mpam_set_feature(mpam_feat_mbw_part, props);
+
+		props->bwa_wd = FIELD_GET(MPAMF_MBW_IDR_BWA_WD, mbw_features);
+		if (props->bwa_wd && FIELD_GET(MPAMF_MBW_IDR_HAS_MAX, mbw_features))
+			mpam_set_feature(mpam_feat_mbw_max, props);
+	}
+
+	/* Performance Monitoring */
+	if (FIELD_GET(MPAMF_IDR_HAS_MSMON, ris->idr)) {
+		u32 msmon_features = mpam_read_partsel_reg(msc, MSMON_IDR);
+
+		/*
+		 * If the firmware max-nrdy-us property is missing, the
+		 * CSU counters can't be used. Should we wait forever?
+		 */
+		err = device_property_read_u32(&msc->pdev->dev,
+					       "arm,not-ready-us",
+					       &msc->nrdy_usec);
+
+		if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_CSU, msmon_features)) {
+			u32 csumonidr;
+
+			csumonidr = mpam_read_partsel_reg(msc, CSUMON_IDR);
+			props->num_csu_mon = FIELD_GET(MPAMF_CSUMON_IDR_NUM_MON, csumonidr);
+			if (props->num_csu_mon) {
+				bool hw_managed;
+
+				mpam_set_feature(mpam_feat_msmon_csu, props);
+
+				/* Is NRDY hardware managed? */
+				hw_managed = mpam_ris_hw_probe_hw_nrdy(ris, CSU);
+				if (hw_managed)
+					mpam_set_feature(mpam_feat_msmon_csu_hw_nrdy, props);
+			}
+
+			/*
+			 * Accept the missing firmware property if NRDY appears
+			 * un-implemented.
+			 */
+			if (err && mpam_has_feature(mpam_feat_msmon_csu_hw_nrdy, props))
+				dev_err_once(dev, "Counters are not usable because not-ready timeout was not provided by firmware.");
+		}
+		if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_MBWU, msmon_features)) {
+			bool hw_managed;
+			u32 mbwumon_idr = mpam_read_partsel_reg(msc, MBWUMON_IDR);
+
+			props->num_mbwu_mon = FIELD_GET(MPAMF_MBWUMON_IDR_NUM_MON, mbwumon_idr);
+			if (props->num_mbwu_mon)
+				mpam_set_feature(mpam_feat_msmon_mbwu, props);
+
+			if (FIELD_GET(MPAMF_MBWUMON_IDR_HAS_RWBW, mbwumon_idr))
+				mpam_set_feature(mpam_feat_msmon_mbwu_rwbw, props);
+
+			/* Is NRDY hardware managed? */
+			hw_managed = mpam_ris_hw_probe_hw_nrdy(ris, MBWU);
+			if (hw_managed)
+				mpam_set_feature(mpam_feat_msmon_mbwu_hw_nrdy, props);
+
+			/*
+			 * Don't warn about any missing firmware property for
+			 * MBWU NRDY - it doesn't make any sense!
+			 */
+		}
+	}
+}
+
 static int mpam_msc_hw_probe(struct mpam_msc *msc)
 {
 	u64 idr;
@@ -592,6 +736,7 @@ static int mpam_msc_hw_probe(struct mpam_msc *msc)
 	mutex_lock(&msc->part_sel_lock);
 	idr = mpam_msc_read_idr(msc);
 	mutex_unlock(&msc->part_sel_lock);
+
 	msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr);
 
 	/* Use these values so partid/pmg always starts with a valid value */
@@ -614,6 +759,12 @@ static int mpam_msc_hw_probe(struct mpam_msc *msc)
 		mutex_unlock(&mpam_list_lock);
 		if (IS_ERR(ris))
 			return PTR_ERR(ris);
+		ris->idr = idr;
+
+		mutex_lock(&msc->part_sel_lock);
+		__mpam_part_sel(ris_idx, 0, msc);
+		mpam_ris_hw_probe(ris);
+		mutex_unlock(&msc->part_sel_lock);
 	}
 
 	spin_lock(&partid_max_lock);
drivers/resctrl/mpam_internal.h +53 -0

71 lines not on this page · raw message

Signature · 1 line
2.39.5
Reviewed-by on v2 13/29
#97 lore raw

Re: [PATCH v2 13/29] arm_mpam: Probe the hardware features resctrl supports

Quoted 5 lines · On Wed, 10 Sep 2025 20:42:53 +0000 James Morse <[email protected]> wrote:

Expand the probing support with the control and monitor types we can use with resctrl.

A few trivial things inline.
LGTM

Out of time for today, fingers crossed I can get to the others tomorrow.

J

drivers/resctrl/mpam_devices.c static int mpam_msc_hw_probe(struct mpam_msc *msc) · line 739

Quoted 8 lines

static int mpam_msc_hw_probe(struct mpam_msc *msc) {

drivers/resctrl/mpam_devices.c +1 -0
 	u64 idr;
@@ -592,6 +736,7 @@ static int mpam_msc_hw_probe(struct mpam_msc *msc)
 	mutex_lock(&msc->part_sel_lock);
 	idr = mpam_msc_read_idr(msc);
 	mutex_unlock(&msc->part_sel_lock);
+

Stray change - push it to earlier patch.

Quoted 21 lines
drivers/resctrl/mpam_devices.c +6 -0
 	msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr);
 
 	/* Use these values so partid/pmg always starts with a valid value */
@@ -614,6 +759,12 @@ static int mpam_msc_hw_probe(struct mpam_msc *msc)
 		mutex_unlock(&mpam_list_lock);
 		if (IS_ERR(ris))
 			return PTR_ERR(ris);
+		ris->idr = idr;
+
+		mutex_lock(&msc->part_sel_lock);
+		__mpam_part_sel(ris_idx, 0, msc);
+		mpam_ris_hw_probe(ris);
+		mutex_unlock(&msc->part_sel_lock);
 	}
 
 	spin_lock(&partid_max_lock);
drivers/resctrl/mpam_internal.h +0 -0
index 4cc44d4e21c4..5ae5d4eee8ec 100644

drivers/resctrl/mpam_internal.h static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc) · line 119

Quoted 8 lines
drivers/resctrl/mpam_internal.h +5 -0
@@ -112,6 +112,55 @@ static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc)
 	raw_spin_lock_init(&msc->_mon_sel_lock);
 }
 
+/*
+ * When we compact the supported features, we don't care what they are.
+ * Storing them as a bitmap makes life easy.
+ */
+typedef u16 mpam_features_t;

Maybe use a bitmap type and avoid the need to be careful on sizing etc?

Quoted 9 lines
drivers/resctrl/mpam_internal.h +9 -0
+
+/* Bits for mpam_features_t */
+enum mpam_device_features {
+	mpam_feat_ccap_part = 0,
+	mpam_feat_cpor_part,
+	mpam_feat_mbw_part,
+	mpam_feat_mbw_min,
+	mpam_feat_mbw_max,
+	mpam_feat_mbw_prop,

drivers/resctrl/mpam_internal.h static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc) · line 138

Quoted 10 lines
drivers/resctrl/mpam_internal.h +10 -0
+	mpam_feat_msmon,
+	mpam_feat_msmon_csu,
+	mpam_feat_msmon_csu_capture,
+	mpam_feat_msmon_csu_hw_nrdy,
+	mpam_feat_msmon_mbwu,
+	mpam_feat_msmon_mbwu_capture,
+	mpam_feat_msmon_mbwu_rwbw,
+	mpam_feat_msmon_mbwu_hw_nrdy,
+	mpam_feat_msmon_capt,
+	MPAM_FEATURE_LAST,

If it's always meant to be LAST, I'd drop the trailing comma.

Quoted 2 lines
drivers/resctrl/mpam_internal.h +2 -0
+};
+static_assert(BITS_PER_TYPE(mpam_features_t) >= MPAM_FEATURE_LAST);
author maintainer
#98 lore raw

Re: [PATCH v2 13/29] arm_mpam: Probe the hardware features resctrl supports

Hi Jonathan,

Quoted 6 lines · Jonathan Cameron
Quoted 3 lines · On Wed, 10 Sep 2025 20:42:53 +0000 James Morse <[email protected]> wrote:

Expand the probing support with the control and monitor types we can use with resctrl.

Quoted 6 lines
Quoted 2 lines

A few trivial things inline.
LGTM

Thanks!

Quoted 6 lines

drivers/resctrl/mpam_devices.c static int mpam_msc_hw_probe(struct mpam_msc *msc) · line 739

Quoted 5 lines
drivers/resctrl/mpam_devices.c +1 -0
@@ -592,6 +736,7 @@ static int mpam_msc_hw_probe(struct mpam_msc *msc)
 	mutex_lock(&msc->part_sel_lock);
 	idr = mpam_msc_read_idr(msc);
 	mutex_unlock(&msc->part_sel_lock);
+

Stray change - push it to earlier patch.

Fixed,

Quoted 8 lines
Quoted 8 lines
drivers/resctrl/mpam_internal.h +0 -0
index 4cc44d4e21c4..5ae5d4eee8ec 100644
@@ -112,6 +112,55 @@ static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc)
 	raw_spin_lock_init(&msc->_mon_sel_lock);
 }
 
Quoted 7 lines

drivers/resctrl/mpam_internal.h static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc) · line 119

Quoted 5 lines
drivers/resctrl/mpam_internal.h +5 -0
+/*
+ * When we compact the supported features, we don't care what they are.
+ * Storing them as a bitmap makes life easy.
+ */
+typedef u16 mpam_features_t;

Maybe use a bitmap type and avoid the need to be careful on sizing etc?

That would be unsigned long at a minimum, which is four times larger than needed. As there is a build-time check, I'm not worried about this ever being wrong...

Quoted 13 lines
Quoted 13 lines
drivers/resctrl/mpam_internal.h +13 -0
+
+/* Bits for mpam_features_t */
+enum mpam_device_features {
+	mpam_feat_ccap_part = 0,
+	mpam_feat_cpor_part,
+	mpam_feat_mbw_part,
+	mpam_feat_mbw_min,
+	mpam_feat_mbw_max,
+	mpam_feat_mbw_prop,
+	mpam_feat_msmon,
+	mpam_feat_msmon_csu,
+	mpam_feat_msmon_csu_capture,
+	mpam_feat_msmon_csu_hw_nrdy,
Quoted 8 lines

drivers/resctrl/mpam_internal.h static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc) · line 138

Quoted 6 lines
drivers/resctrl/mpam_internal.h +6 -0
+	mpam_feat_msmon_mbwu,
+	mpam_feat_msmon_mbwu_capture,
+	mpam_feat_msmon_mbwu_rwbw,
+	mpam_feat_msmon_mbwu_hw_nrdy,
+	mpam_feat_msmon_capt,
+	MPAM_FEATURE_LAST,

If it's always meant to be LAST, I'd drop the trailing comma.

Sure. Full-stops for enums!

Thanks,

James

maintainer
#99 lore raw

Re: [PATCH v2 13/29] arm_mpam: Probe the hardware features resctrl supports

In reply to James Morse

Hi James,

Quoted 17 lines · James Morse

Expand the probing support with the control and monitor types we can use with resctrl.

---
Changes since v1:
* added an underscore to a variable name.

Changes since RFC:
* Made mpam_ris_hw_probe_hw_nrdy() more in C.
* Added static assert on features bitmap size.

---
 drivers/resctrl/mpam_devices.c  | 151 ++++++++++++++++++++++++++++++++
 drivers/resctrl/mpam_internal.h |  53 +++++++++++
 2 files changed, 204 insertions(+)

[snip]

Quoted 25 lines
drivers/resctrl/mpam_devices.c +0 -0
 	spin_lock(&partid_max_lock);
drivers/resctrl/mpam_internal.h +15 -0
index 4cc44d4e21c4..5ae5d4eee8ec 100644
@@ -112,6 +112,55 @@ static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc)
 	raw_spin_lock_init(&msc->_mon_sel_lock);
 }
 
+/*
+ * When we compact the supported features, we don't care what they are.
+ * Storing them as a bitmap makes life easy.
+ */
+typedef u16 mpam_features_t;
+
+/* Bits for mpam_features_t */
+enum mpam_device_features {
+	mpam_feat_ccap_part = 0,
+	mpam_feat_cpor_part,
+	mpam_feat_mbw_part,
+	mpam_feat_mbw_min,
+	mpam_feat_mbw_max,
+	mpam_feat_mbw_prop,
+	mpam_feat_msmon,

drivers/resctrl/mpam_internal.h static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc) · line 139

Quoted 10 lines
drivers/resctrl/mpam_internal.h +10 -0
+	mpam_feat_msmon_csu,
+	mpam_feat_msmon_csu_capture,
+	mpam_feat_msmon_csu_hw_nrdy,
+	mpam_feat_msmon_mbwu,
+	mpam_feat_msmon_mbwu_capture,
+	mpam_feat_msmon_mbwu_rwbw,
+	mpam_feat_msmon_mbwu_hw_nrdy,
+	mpam_feat_msmon_capt,
+	MPAM_FEATURE_LAST,
+};

I added a garbled comment about this for v1. What I was trying to say is that I don't think this quite matches what resctrl supports. For instance, I don't think mpam_feat_ccap_part matches a resctrl feature.

Thanks,

Ben

author maintainer
#100 lore raw

Re: [PATCH v2 13/29] arm_mpam: Probe the hardware features resctrl supports

Hi Ben,

Quoted 33 lines · Ben Horgan
Quoted 32 lines · James Morse

Expand the probing support with the control and monitor types we can use with resctrl.

drivers/resctrl/mpam_internal.h +21 -0
index 4cc44d4e21c4..5ae5d4eee8ec 100644
@@ -112,6 +112,55 @@ static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc)
 	raw_spin_lock_init(&msc->_mon_sel_lock);
 }
 
+/*
+ * When we compact the supported features, we don't care what they are.
+ * Storing them as a bitmap makes life easy.
+ */
+typedef u16 mpam_features_t;
+
+/* Bits for mpam_features_t */
+enum mpam_device_features {
+	mpam_feat_ccap_part = 0,
+	mpam_feat_cpor_part,
+	mpam_feat_mbw_part,
+	mpam_feat_mbw_min,
+	mpam_feat_mbw_max,
+	mpam_feat_mbw_prop,
+	mpam_feat_msmon,
+	mpam_feat_msmon_csu,
+	mpam_feat_msmon_csu_capture,
+	mpam_feat_msmon_csu_hw_nrdy,
+	mpam_feat_msmon_mbwu,
+	mpam_feat_msmon_mbwu_capture,
+	mpam_feat_msmon_mbwu_rwbw,
Quoted 8 lines

drivers/resctrl/mpam_internal.h static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc) · line 139

Quoted 4 lines
drivers/resctrl/mpam_internal.h +4 -0
+	mpam_feat_msmon_mbwu_hw_nrdy,
+	mpam_feat_msmon_capt,
+	MPAM_FEATURE_LAST,
+};

I added a garbled comment about this for v1. What I was trying to say is that I don't think this quite matches what resctrl supports. For instance, I don't think mpam_feat_ccap_part matches a resctrl feature.

Ah - right. I thought you meant something was removed later.
Looks like I thought something could be emulated with CCAP, but that turns out not to be true because it doesn't have an implicit isolation property, which the resctrl:bitmap-from-userspace requires.
(I think rwbw was a later addition to the architecture and I added it to the wrong patch).

I'll move that, _prop and _rwbw to the later patch. The split is fairly arbitrary - it was just somewhere to split an otherwise large patch, and does help determine if a bug is going to be visible to user-space or not.

_capt can go completely. Last I heard no-one was interested in firmware descriptions of how the capture hardware can be triggered. I suspect no-one has done anything with it.

Thanks,

James

maintainer
#101 lore raw

Re: [PATCH v2 13/29] arm_mpam: Probe the hardware features resctrl supports

Hi James,

Quoted 45 lines · James Morse

Hi Ben,

Quoted 33 lines · Ben Horgan
Quoted 32 lines · James Morse

Expand the probing support with the control and monitor types we can use with resctrl.

drivers/resctrl/mpam_internal.h +21 -0
index 4cc44d4e21c4..5ae5d4eee8ec 100644
@@ -112,6 +112,55 @@ static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc)
 	raw_spin_lock_init(&msc->_mon_sel_lock);
 }
 
+/*
+ * When we compact the supported features, we don't care what they are.
+ * Storing them as a bitmap makes life easy.
+ */
+typedef u16 mpam_features_t;
+
+/* Bits for mpam_features_t */
+enum mpam_device_features {
+	mpam_feat_ccap_part = 0,
+	mpam_feat_cpor_part,
+	mpam_feat_mbw_part,
+	mpam_feat_mbw_min,
+	mpam_feat_mbw_max,
+	mpam_feat_mbw_prop,
+	mpam_feat_msmon,
+	mpam_feat_msmon_csu,
+	mpam_feat_msmon_csu_capture,
+	mpam_feat_msmon_csu_hw_nrdy,
+	mpam_feat_msmon_mbwu,
+	mpam_feat_msmon_mbwu_capture,
+	mpam_feat_msmon_mbwu_rwbw,
Quoted 8 lines

drivers/resctrl/mpam_internal.h static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc) · line 139

Quoted 4 lines
drivers/resctrl/mpam_internal.h +4 -0
+	mpam_feat_msmon_mbwu_hw_nrdy,
+	mpam_feat_msmon_capt,
+	MPAM_FEATURE_LAST,
+};

I added a garbled comment about this for v1. What I was trying to say is that I don't think this quite matches what resctrl supports. For instance, I don't think mpam_feat_ccap_part matches a resctrl feature.

Quoted 7 lines

Ah - right. I thought you meant something was removed later.
Looks like I thought something could be emulated with CCAP, but that turns out not to be true because it doesn't have an implicit isolation property, which the resctrl:bitmap-from-userspace requires.
(I think rwbw was a later addition to the architecture and I added it to the wrong patch).

I'll move that, _prop and _rwbw to the later patch. The split is fairly arbitrary - it was

I think ccap gets split into finer grained features later on which seems fine.

Quoted 2 lines

just somewhere to split an otherwise large patch, and does help determine if a bug is going to be visible to user-space or not.

Ok, sensible, I hadn't appreciated the user-space visibility aspect.

Quoted 3 lines

_capt can go completely. Last I heard no-one was interested in firmware descriptions of how the capture hardware can be triggered. I suspect no-one has done anything with it.

So, if I've understood correctly this leaves you with the following in this patch.

mpam_feat_cpor_part,
mpam_feat_mbw_part,
mpam_feat_mbw_min,
mpam_feat_mbw_max,
mpam_feat_mbw_prop,
mpam_feat_msmon,
mpam_feat_msmon_csu,
mpam_feat_msmon_csu_hw_nrdy,
mpam_feat_msmon_mbwu,
mpam_feat_msmon_mbwu_hw_nrdy,

Looks like the correct, resctrl based, split.

Quoted 5 lines

Thanks,

James

Thanks,

Ben

reviewer Reviewed-by on v2 13/29
#102 lore raw

Re: [PATCH v2 13/29] arm_mpam: Probe the hardware features resctrl supports

In reply to James Morse
Quoted 5 lines · James Morse

Expand the probing support with the control and monitor types we can use with resctrl.

A couple of minor comments below.

Quoted 16 lines

---
Changes since v1:
* added an underscore to a variable name.

Changes since RFC:
* Made mpam_ris_hw_probe_hw_nrdy() more in C.
* Added static assert on features bitmap size.

---
  drivers/resctrl/mpam_devices.c  | 151 ++++++++++++++++++++++++++++++++
  drivers/resctrl/mpam_internal.h |  53 +++++++++++
  2 files changed, 204 insertions(+)
drivers/resctrl/mpam_devices.c +0 -0
index a26b012452e2..ba8e8839cdc4 100644

[SNIP]

drivers/resctrl/mpam_devices.c static int mpam_msc_hw_probe(struct mpam_msc *msc) · line 739

Quoted 5 lines
drivers/resctrl/mpam_devices.c +1 -0
@@ -592,6 +736,7 @@ static int mpam_msc_hw_probe(struct mpam_msc *msc)
  	mutex_lock(&msc->part_sel_lock);
  	idr = mpam_msc_read_idr(msc);
  	mutex_unlock(&msc->part_sel_lock);
+

Adding this blank line is irrelevant to this patch. It's better to move this blank line to the original patch #11.

Quoted 21 lines
drivers/resctrl/mpam_devices.c +6 -0
  	msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr);
 
  	/* Use these values so partid/pmg always starts with a valid value */
@@ -614,6 +759,12 @@ static int mpam_msc_hw_probe(struct mpam_msc *msc)
  		mutex_unlock(&mpam_list_lock);
  		if (IS_ERR(ris))
  			return PTR_ERR(ris);
+		ris->idr = idr;
+
+		mutex_lock(&msc->part_sel_lock);
+		__mpam_part_sel(ris_idx, 0, msc);
+		mpam_ris_hw_probe(ris);
+		mutex_unlock(&msc->part_sel_lock);
  	}
 
  	spin_lock(&partid_max_lock);
drivers/resctrl/mpam_internal.h +0 -0
index 4cc44d4e21c4..5ae5d4eee8ec 100644

drivers/resctrl/mpam_internal.h static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc) · line 120

Quoted 9 lines
drivers/resctrl/mpam_internal.h +6 -0
@@ -112,6 +112,55 @@ static inline void mpam_mon_sel_lock_init(struct mpam_msc *msc)
  	raw_spin_lock_init(&msc->_mon_sel_lock);
  }
 
+/*
+ * When we compact the supported features, we don't care what they are.
+ * Storing them as a bitmap makes life easy.
+ */
+typedef u16 mpam_features_t;
+

mpam_features_t is changed to u32 later in patch #21. It's better to directly define it as u32 here and remove the type change in patch #21.

[SNIP]

Thanks.

-Fenghua