[PATCH v3 09/47] arm64: mpam: Add cpu_pm notifier to restore MPAM sysregs

Ben Horgan posted 47 patches 4 weeks ago
There is a newer version of this series
[PATCH v3 09/47] arm64: mpam: Add cpu_pm notifier to restore MPAM sysregs
Posted by Ben Horgan 4 weeks ago
From: James Morse <james.morse@arm.com>

The MPAM system registers will be lost if the CPU is reset during PSCI's
CPU_SUSPEND.

Add a PM notifier to restore them.

mpam_thread_switch(current) can't be used as this won't make any changes if
the in-memory copy says the register already has the correct value. In
reality the system register is UNKNOWN out of reset.

Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
Signed-off-by: James Morse <james.morse@arm.com>
Signed-off-by: Ben Horgan <ben.horgan@arm.com>
---
 arch/arm64/kernel/mpam.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/arch/arm64/kernel/mpam.c b/arch/arm64/kernel/mpam.c
index e6feff2324ac..dbe0a2d05abb 100644
--- a/arch/arm64/kernel/mpam.c
+++ b/arch/arm64/kernel/mpam.c
@@ -4,6 +4,7 @@
 #include <asm/mpam.h>
 
 #include <linux/arm_mpam.h>
+#include <linux/cpu_pm.h>
 #include <linux/jump_label.h>
 #include <linux/percpu.h>
 
@@ -13,12 +14,41 @@ DEFINE_PER_CPU(u64, arm64_mpam_current);
 
 u64 arm64_mpam_global_default;
 
+static int mpam_pm_notifier(struct notifier_block *self,
+			    unsigned long cmd, void *v)
+{
+	u64 regval;
+	int cpu = smp_processor_id();
+
+	switch (cmd) {
+	case CPU_PM_EXIT:
+		/*
+		 * Don't use mpam_thread_switch() as the system register
+		 * value has changed under our feet.
+		 */
+		regval = READ_ONCE(per_cpu(arm64_mpam_current, cpu));
+		write_sysreg_s(regval, SYS_MPAM1_EL1);
+		isb();
+
+		write_sysreg_s(regval, SYS_MPAM0_EL1);
+
+		return NOTIFY_OK;
+	default:
+		return NOTIFY_DONE;
+	}
+}
+
+static struct notifier_block mpam_pm_nb = {
+	.notifier_call = mpam_pm_notifier,
+};
+
 static int __init arm64_mpam_register_cpus(void)
 {
 	u64 mpamidr = read_sanitised_ftr_reg(SYS_MPAMIDR_EL1);
 	u16 partid_max = FIELD_GET(MPAMIDR_EL1_PARTID_MAX, mpamidr);
 	u8 pmg_max = FIELD_GET(MPAMIDR_EL1_PMG_MAX, mpamidr);
 
+	cpu_pm_register_notifier(&mpam_pm_nb);
 	return mpam_register_requestor(partid_max, pmg_max);
 }
 /* Must occur before mpam_msc_driver_init() from subsys_initcall() */
-- 
2.43.0
Re: [PATCH v3 09/47] arm64: mpam: Add cpu_pm notifier to restore MPAM sysregs
Posted by Gavin Shan 3 weeks ago
Hi Ben,

On 1/13/26 12:58 AM, Ben Horgan wrote:
> From: James Morse <james.morse@arm.com>
> 
> The MPAM system registers will be lost if the CPU is reset during PSCI's
> CPU_SUSPEND.
> 
> Add a PM notifier to restore them.
> 
> mpam_thread_switch(current) can't be used as this won't make any changes if
> the in-memory copy says the register already has the correct value. In
> reality the system register is UNKNOWN out of reset.
> 
> Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
> Signed-off-by: James Morse <james.morse@arm.com>
> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
> ---
>   arch/arm64/kernel/mpam.c | 30 ++++++++++++++++++++++++++++++
>   1 file changed, 30 insertions(+)
> 

One question below...

> diff --git a/arch/arm64/kernel/mpam.c b/arch/arm64/kernel/mpam.c
> index e6feff2324ac..dbe0a2d05abb 100644
> --- a/arch/arm64/kernel/mpam.c
> +++ b/arch/arm64/kernel/mpam.c
> @@ -4,6 +4,7 @@
>   #include <asm/mpam.h>
>   
>   #include <linux/arm_mpam.h>
> +#include <linux/cpu_pm.h>
>   #include <linux/jump_label.h>
>   #include <linux/percpu.h>
>   
> @@ -13,12 +14,41 @@ DEFINE_PER_CPU(u64, arm64_mpam_current);
>   
>   u64 arm64_mpam_global_default;
>   
> +static int mpam_pm_notifier(struct notifier_block *self,
> +			    unsigned long cmd, void *v)
> +{
> +	u64 regval;
> +	int cpu = smp_processor_id();
> +
> +	switch (cmd) {
> +	case CPU_PM_EXIT:
> +		/*
> +		 * Don't use mpam_thread_switch() as the system register
> +		 * value has changed under our feet.
> +		 */
> +		regval = READ_ONCE(per_cpu(arm64_mpam_current, cpu));
> +		write_sysreg_s(regval, SYS_MPAM1_EL1);
> +		isb();
> +
> +		write_sysreg_s(regval, SYS_MPAM0_EL1);
> +
> +		return NOTIFY_OK;
> +	default:
> +		return NOTIFY_DONE;
> +	}
> +}
> +
> +static struct notifier_block mpam_pm_nb = {
> +	.notifier_call = mpam_pm_notifier,
> +};
> +
>   static int __init arm64_mpam_register_cpus(void)
>   {
>   	u64 mpamidr = read_sanitised_ftr_reg(SYS_MPAMIDR_EL1);
>   	u16 partid_max = FIELD_GET(MPAMIDR_EL1_PARTID_MAX, mpamidr);
>   	u8 pmg_max = FIELD_GET(MPAMIDR_EL1_PMG_MAX, mpamidr);
>   
> +	cpu_pm_register_notifier(&mpam_pm_nb);

Need we ensure MPAM capability exists in the hardware before the notifier is registerred?
Otherwise, mpam_pm_notifier() can accesses SYS_MPAM0_EL1 and SYS_MPAM1_EL1 system registers
which may not supported by the hardware.

>   	return mpam_register_requestor(partid_max, pmg_max);
>   }
>   /* Must occur before mpam_msc_driver_init() from subsys_initcall() */

Thanks,
Gavin
Re: [PATCH v3 09/47] arm64: mpam: Add cpu_pm notifier to restore MPAM sysregs
Posted by Ben Horgan 3 weeks ago
Hi Gavin,

On 1/19/26 06:50, Gavin Shan wrote:
> Hi Ben,
> 
> On 1/13/26 12:58 AM, Ben Horgan wrote:
>> From: James Morse <james.morse@arm.com>
>>
>> The MPAM system registers will be lost if the CPU is reset during PSCI's
>> CPU_SUSPEND.
>>
>> Add a PM notifier to restore them.
>>
>> mpam_thread_switch(current) can't be used as this won't make any
>> changes if
>> the in-memory copy says the register already has the correct value. In
>> reality the system register is UNKNOWN out of reset.
>>
>> Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
>> Signed-off-by: James Morse <james.morse@arm.com>
>> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
>> ---
>>   arch/arm64/kernel/mpam.c | 30 ++++++++++++++++++++++++++++++
>>   1 file changed, 30 insertions(+)
>>
> 
> One question below...
> 
>> diff --git a/arch/arm64/kernel/mpam.c b/arch/arm64/kernel/mpam.c
>> index e6feff2324ac..dbe0a2d05abb 100644
>> --- a/arch/arm64/kernel/mpam.c
>> +++ b/arch/arm64/kernel/mpam.c
>> @@ -4,6 +4,7 @@
>>   #include <asm/mpam.h>
>>     #include <linux/arm_mpam.h>
>> +#include <linux/cpu_pm.h>
>>   #include <linux/jump_label.h>
>>   #include <linux/percpu.h>
>>   @@ -13,12 +14,41 @@ DEFINE_PER_CPU(u64, arm64_mpam_current);
>>     u64 arm64_mpam_global_default;
>>   +static int mpam_pm_notifier(struct notifier_block *self,
>> +                unsigned long cmd, void *v)
>> +{
>> +    u64 regval;
>> +    int cpu = smp_processor_id();
>> +
>> +    switch (cmd) {
>> +    case CPU_PM_EXIT:
>> +        /*
>> +         * Don't use mpam_thread_switch() as the system register
>> +         * value has changed under our feet.
>> +         */
>> +        regval = READ_ONCE(per_cpu(arm64_mpam_current, cpu));
>> +        write_sysreg_s(regval, SYS_MPAM1_EL1);
>> +        isb();
>> +
>> +        write_sysreg_s(regval, SYS_MPAM0_EL1);
>> +
>> +        return NOTIFY_OK;
>> +    default:
>> +        return NOTIFY_DONE;
>> +    }
>> +}
>> +
>> +static struct notifier_block mpam_pm_nb = {
>> +    .notifier_call = mpam_pm_notifier,
>> +};
>> +
>>   static int __init arm64_mpam_register_cpus(void)
>>   {
>>       u64 mpamidr = read_sanitised_ftr_reg(SYS_MPAMIDR_EL1);
>>       u16 partid_max = FIELD_GET(MPAMIDR_EL1_PARTID_MAX, mpamidr);
>>       u8 pmg_max = FIELD_GET(MPAMIDR_EL1_PMG_MAX, mpamidr);
>>   +    cpu_pm_register_notifier(&mpam_pm_nb);
> 
> Need we ensure MPAM capability exists in the hardware before the
> notifier is registerred?
> Otherwise, mpam_pm_notifier() can accesses SYS_MPAM0_EL1 and
> SYS_MPAM1_EL1 system registers
> which may not supported by the hardware.

Ah yes, I'll add a system_supports_mpam() check here.

> 
>>       return mpam_register_requestor(partid_max, pmg_max);
>>   }
>>   /* Must occur before mpam_msc_driver_init() from subsys_initcall() */
> 
> Thanks,
> Gavin
> 

Thanks,

Ben

Re: [PATCH v3 09/47] arm64: mpam: Add cpu_pm notifier to restore MPAM sysregs
Posted by Gavin Shan 3 weeks ago
On 1/13/26 12:58 AM, Ben Horgan wrote:
> From: James Morse <james.morse@arm.com>
> 
> The MPAM system registers will be lost if the CPU is reset during PSCI's
> CPU_SUSPEND.
> 
> Add a PM notifier to restore them.
> 
> mpam_thread_switch(current) can't be used as this won't make any changes if
> the in-memory copy says the register already has the correct value. In
> reality the system register is UNKNOWN out of reset.
> 
> Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
> Signed-off-by: James Morse <james.morse@arm.com>
> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
> ---
>   arch/arm64/kernel/mpam.c | 30 ++++++++++++++++++++++++++++++
>   1 file changed, 30 insertions(+)
> 

Reviewed-by: Gavin Shan <gshan@redhat.com>
Re: [PATCH v3 09/47] arm64: mpam: Add cpu_pm notifier to restore MPAM sysregs
Posted by Catalin Marinas 3 weeks, 3 days ago
On Mon, Jan 12, 2026 at 04:58:36PM +0000, Ben Horgan wrote:
> +static int mpam_pm_notifier(struct notifier_block *self,
> +			    unsigned long cmd, void *v)
> +{
> +	u64 regval;
> +	int cpu = smp_processor_id();
> +
> +	switch (cmd) {
> +	case CPU_PM_EXIT:
> +		/*
> +		 * Don't use mpam_thread_switch() as the system register
> +		 * value has changed under our feet.
> +		 */
> +		regval = READ_ONCE(per_cpu(arm64_mpam_current, cpu));
> +		write_sysreg_s(regval, SYS_MPAM1_EL1);
> +		isb();
> +
> +		write_sysreg_s(regval, SYS_MPAM0_EL1);
> +
> +		return NOTIFY_OK;
> +	default:
> +		return NOTIFY_DONE;
> +	}
> +}

This looks fine unless we decide to save/restore them in the low-level
suspend/resume functions.

-- 
Catalin