xref: /linux/arch/arm64/kernel/mpam.c (revision e2683c8868d03382da7e1ce8453b543a043066d1)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2025 Arm Ltd. */
3 
4 #include <asm/mpam.h>
5 
6 #include <linux/arm_mpam.h>
7 #include <linux/cpu_pm.h>
8 #include <linux/jump_label.h>
9 #include <linux/percpu.h>
10 
11 DEFINE_STATIC_KEY_FALSE(mpam_enabled);
12 DEFINE_PER_CPU(u64, arm64_mpam_default);
13 DEFINE_PER_CPU(u64, arm64_mpam_current);
14 
15 u64 arm64_mpam_global_default;
16 
17 static int mpam_pm_notifier(struct notifier_block *self,
18 			    unsigned long cmd, void *v)
19 {
20 	u64 regval;
21 	int cpu = smp_processor_id();
22 
23 	switch (cmd) {
24 	case CPU_PM_EXIT:
25 		/*
26 		 * Don't use mpam_thread_switch() as the system register
27 		 * value has changed under our feet.
28 		 */
29 		regval = READ_ONCE(per_cpu(arm64_mpam_current, cpu));
30 		write_sysreg_s(regval | MPAM1_EL1_MPAMEN, SYS_MPAM1_EL1);
31 		if (system_supports_sme()) {
32 			write_sysreg_s(regval & (MPAMSM_EL1_PARTID_D | MPAMSM_EL1_PMG_D),
33 				       SYS_MPAMSM_EL1);
34 		}
35 		isb();
36 
37 		write_sysreg_s(regval, SYS_MPAM0_EL1);
38 
39 		return NOTIFY_OK;
40 	default:
41 		return NOTIFY_DONE;
42 	}
43 }
44 
45 static struct notifier_block mpam_pm_nb = {
46 	.notifier_call = mpam_pm_notifier,
47 };
48 
49 static int __init arm64_mpam_register_cpus(void)
50 {
51 	u64 mpamidr = read_sanitised_ftr_reg(SYS_MPAMIDR_EL1);
52 	u16 partid_max = FIELD_GET(MPAMIDR_EL1_PARTID_MAX, mpamidr);
53 	u8 pmg_max = FIELD_GET(MPAMIDR_EL1_PMG_MAX, mpamidr);
54 
55 	if (!system_supports_mpam())
56 		return 0;
57 
58 	cpu_pm_register_notifier(&mpam_pm_nb);
59 	return mpam_register_requestor(partid_max, pmg_max);
60 }
61 /* Must occur before mpam_msc_driver_init() from subsys_initcall() */
62 arch_initcall(arm64_mpam_register_cpus)
63