xref: /linux/drivers/resctrl/mpam_devices.c (revision bb1a0f582d519c0461b0940116e2b52c5ba31459)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2025 Arm Ltd.
3 
4 #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
5 
6 #include <linux/acpi.h>
7 #include <linux/atomic.h>
8 #include <linux/arm_mpam.h>
9 #include <linux/bitfield.h>
10 #include <linux/bitmap.h>
11 #include <linux/cacheinfo.h>
12 #include <linux/cpu.h>
13 #include <linux/cpumask.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
16 #include <linux/gfp.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/irqdesc.h>
20 #include <linux/list.h>
21 #include <linux/lockdep.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/printk.h>
25 #include <linux/srcu.h>
26 #include <linux/spinlock.h>
27 #include <linux/types.h>
28 #include <linux/workqueue.h>
29 
30 #include "mpam_internal.h"
31 
32 /* Values for the T241 errata workaround */
33 #define T241_CHIPS_MAX			4
34 #define T241_CHIP_NSLICES		12
35 #define T241_SPARE_REG0_OFF		0x1b0000
36 #define T241_SPARE_REG1_OFF		0x1c0000
37 #define T241_CHIP_ID(phys)		FIELD_GET(GENMASK_ULL(44, 43), phys)
38 #define T241_SHADOW_REG_OFF(sidx, pid)	(0x360048 + (sidx) * 0x10000 + (pid) * 8)
39 #define SMCCC_SOC_ID_T241		0x036b0241
40 static void __iomem *t241_scratch_regs[T241_CHIPS_MAX];
41 
42 /*
43  * mpam_list_lock protects the SRCU lists when writing. Once the
44  * mpam_enabled key is enabled these lists are read-only,
45  * unless the error interrupt disables the driver.
46  */
47 static DEFINE_MUTEX(mpam_list_lock);
48 static LIST_HEAD(mpam_all_msc);
49 
50 struct srcu_struct mpam_srcu;
51 
52 /*
53  * Number of MSCs that have been probed. Once all MSCs have been probed MPAM
54  * can be enabled.
55  */
56 static atomic_t mpam_num_msc;
57 
58 static int mpam_cpuhp_state;
59 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
60 
61 /*
62  * The smallest common values for any CPU or MSC in the system.
63  * Generating traffic outside this range will result in screaming interrupts.
64  */
65 u16 mpam_partid_max;
66 u8 mpam_pmg_max;
67 static bool partid_max_init, partid_max_published;
68 static DEFINE_SPINLOCK(partid_max_lock);
69 
70 /*
71  * mpam is enabled once all devices have been probed from CPU online callbacks,
72  * scheduled via this work_struct. If access to an MSC depends on a CPU that
73  * was not brought online at boot, this can happen surprisingly late.
74  */
75 static DECLARE_WORK(mpam_enable_work, &mpam_enable);
76 
77 /*
78  * All mpam error interrupts indicate a software bug. On receipt, disable the
79  * driver.
80  */
81 static DECLARE_WORK(mpam_broken_work, &mpam_disable);
82 
83 /* When mpam is disabled, the printed reason to aid debugging */
84 static char *mpam_disable_reason;
85 
86 /*
87  * Whether resctrl has been setup. Used by cpuhp in preference to
88  * mpam_is_enabled(). The disable call after an error interrupt makes
89  * mpam_is_enabled() false before the cpuhp callbacks are made.
90  * Reads/writes should hold mpam_cpuhp_state_lock, (or be cpuhp callbacks).
91  */
92 static bool mpam_resctrl_enabled;
93 
94 /*
95  * An MSC is a physical container for controls and monitors, each identified by
96  * their RIS index. These share a base-address, interrupts and some MMIO
97  * registers. A vMSC is a virtual container for RIS in an MSC that control or
98  * monitor the same thing. Members of a vMSC are all RIS in the same MSC, but
99  * not all RIS in an MSC share a vMSC.
100  *
101  * Components are a group of vMSC that control or monitor the same thing but
102  * are from different MSC, so have different base-address, interrupts etc.
103  * Classes are the set components of the same type.
104  *
105  * The features of a vMSC is the union of the RIS it contains.
106  * The features of a Class and Component are the common subset of the vMSC
107  * they contain.
108  *
109  * e.g. The system cache may have bandwidth controls on multiple interfaces,
110  * for regulating traffic from devices independently of traffic from CPUs.
111  * If these are two RIS in one MSC, they will be treated as controlling
112  * different things, and will not share a vMSC/component/class.
113  *
114  * e.g. The L2 may have one MSC and two RIS, one for cache-controls another
115  * for bandwidth. These two RIS are members of the same vMSC.
116  *
117  * e.g. The set of RIS that make up the L2 are grouped as a component. These
118  * are sometimes termed slices. They should be configured the same, as if there
119  * were only one.
120  *
121  * e.g. The SoC probably has more than one L2, each attached to a distinct set
122  * of CPUs. All the L2 components are grouped as a class.
123  *
124  * When creating an MSC, struct mpam_msc is added to the all mpam_all_msc list,
125  * then linked via struct mpam_ris to a vmsc, component and class.
126  * The same MSC may exist under different class->component->vmsc paths, but the
127  * RIS index will be unique.
128  */
129 LIST_HEAD(mpam_classes);
130 
131 /* List of all objects that can be free()d after synchronise_srcu() */
132 static LLIST_HEAD(mpam_garbage);
133 
134 static inline void init_garbage(struct mpam_garbage *garbage)
135 {
136 	init_llist_node(&garbage->llist);
137 }
138 
139 #define add_to_garbage(x)				\
140 do {							\
141 	__typeof__(x) _x = (x);				\
142 	_x->garbage.to_free = _x;			\
143 	llist_add(&_x->garbage.llist, &mpam_garbage);	\
144 } while (0)
145 
146 static void mpam_free_garbage(void)
147 {
148 	struct mpam_garbage *iter, *tmp;
149 	struct llist_node *to_free = llist_del_all(&mpam_garbage);
150 
151 	if (!to_free)
152 		return;
153 
154 	synchronize_srcu(&mpam_srcu);
155 
156 	llist_for_each_entry_safe(iter, tmp, to_free, llist) {
157 		if (iter->pdev)
158 			devm_kfree(&iter->pdev->dev, iter->to_free);
159 		else
160 			kfree(iter->to_free);
161 	}
162 }
163 
164 /*
165  * Once mpam is enabled, new requestors cannot further reduce the available
166  * partid. Assert that the size is fixed, and new requestors will be turned
167  * away. This is needed when walking over structures sized by PARTID.
168  *
169  * During mpam_disable() these structures are not fixed, but the MSC state
170  * is still reset using whatever sizes have been discovered so far. As only
171  * PARTID 0 will be used after mpam_disable(), any race would be benign.
172  * Skip the check if a mpam_disable_reason has been set.
173  */
174 static void mpam_assert_partid_sizes_fixed(void)
175 {
176 	if (!mpam_disable_reason)
177 		WARN_ON_ONCE(!partid_max_published);
178 }
179 
180 static u32 __mpam_read_reg(struct mpam_msc *msc, u16 reg)
181 {
182 	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
183 
184 	return readl_relaxed(msc->mapped_hwpage + reg);
185 }
186 
187 static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg)
188 {
189 	lockdep_assert_held_once(&msc->part_sel_lock);
190 	return __mpam_read_reg(msc, reg);
191 }
192 
193 #define mpam_read_partsel_reg(msc, reg) _mpam_read_partsel_reg(msc, MPAMF_##reg)
194 
195 static void __mpam_write_reg(struct mpam_msc *msc, u16 reg, u32 val)
196 {
197 	WARN_ON_ONCE(reg + sizeof(u32) > msc->mapped_hwpage_sz);
198 	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
199 
200 	writel_relaxed(val, msc->mapped_hwpage + reg);
201 }
202 
203 static inline void _mpam_write_partsel_reg(struct mpam_msc *msc, u16 reg, u32 val)
204 {
205 	lockdep_assert_held_once(&msc->part_sel_lock);
206 	__mpam_write_reg(msc, reg, val);
207 }
208 
209 #define mpam_write_partsel_reg(msc, reg, val)  _mpam_write_partsel_reg(msc, MPAMCFG_##reg, val)
210 
211 static inline u32 _mpam_read_monsel_reg(struct mpam_msc *msc, u16 reg)
212 {
213 	mpam_mon_sel_lock_held(msc);
214 	return __mpam_read_reg(msc, reg);
215 }
216 
217 #define mpam_read_monsel_reg(msc, reg) _mpam_read_monsel_reg(msc, MSMON_##reg)
218 
219 static inline void _mpam_write_monsel_reg(struct mpam_msc *msc, u16 reg, u32 val)
220 {
221 	mpam_mon_sel_lock_held(msc);
222 	__mpam_write_reg(msc, reg, val);
223 }
224 
225 #define mpam_write_monsel_reg(msc, reg, val)   _mpam_write_monsel_reg(msc, MSMON_##reg, val)
226 
227 static bool mpam_msc_check_aidr(struct mpam_msc *msc)
228 {
229 	u32 aidr = __mpam_read_reg(msc, MPAMF_AIDR);
230 	u32 major = FIELD_GET(MPAMF_AIDR_ARCH_MAJOR_REV, aidr);
231 	u32 minor = FIELD_GET(MPAMF_AIDR_ARCH_MINOR_REV, aidr);
232 
233 	/*
234 	 * v0.0 and >v2.x aren't supported, but anything else should be backward
235 	 * compatible to v0.1 or v1.0.
236 	 */
237 	if (!major && !minor)
238 		return false;
239 	if (major > 1)
240 		return false;
241 
242 	return true;
243 }
244 
245 static u64 mpam_msc_read_idr(struct mpam_msc *msc)
246 {
247 	u64 idr_high = 0, idr_low;
248 
249 	lockdep_assert_held(&msc->part_sel_lock);
250 
251 	idr_low = mpam_read_partsel_reg(msc, IDR);
252 	if (FIELD_GET(MPAMF_IDR_EXT, idr_low))
253 		idr_high = mpam_read_partsel_reg(msc, IDR + 4);
254 
255 	return (idr_high << 32) | idr_low;
256 }
257 
258 static void mpam_msc_clear_esr(struct mpam_msc *msc)
259 {
260 	u64 esr_low = __mpam_read_reg(msc, MPAMF_ESR);
261 
262 	if (!esr_low)
263 		return;
264 
265 	/*
266 	 * Clearing the high/low bits of MPAMF_ESR can not be atomic.
267 	 * Clear the top half first, so that the pending error bits in the
268 	 * lower half prevent hardware from updating either half of the
269 	 * register.
270 	 */
271 	if (msc->has_extd_esr)
272 		__mpam_write_reg(msc, MPAMF_ESR + 4, 0);
273 	__mpam_write_reg(msc, MPAMF_ESR, 0);
274 }
275 
276 static u64 mpam_msc_read_esr(struct mpam_msc *msc)
277 {
278 	u64 esr_high = 0, esr_low;
279 
280 	esr_low = __mpam_read_reg(msc, MPAMF_ESR);
281 	if (msc->has_extd_esr)
282 		esr_high = __mpam_read_reg(msc, MPAMF_ESR + 4);
283 
284 	return (esr_high << 32) | esr_low;
285 }
286 
287 static void __mpam_part_sel_raw(u32 partsel, struct mpam_msc *msc)
288 {
289 	lockdep_assert_held(&msc->part_sel_lock);
290 
291 	mpam_write_partsel_reg(msc, PART_SEL, partsel);
292 }
293 
294 static void __mpam_part_sel(u8 ris_idx, u16 partid, struct mpam_msc *msc)
295 {
296 	u32 partsel = FIELD_PREP(MPAMCFG_PART_SEL_RIS, ris_idx) |
297 		      FIELD_PREP(MPAMCFG_PART_SEL_PARTID_SEL, partid);
298 
299 	__mpam_part_sel_raw(partsel, msc);
300 }
301 
302 static void __mpam_intpart_sel(u8 ris_idx, u16 intpartid, struct mpam_msc *msc)
303 {
304 	u32 partsel = FIELD_PREP(MPAMCFG_PART_SEL_RIS, ris_idx) |
305 		      FIELD_PREP(MPAMCFG_PART_SEL_PARTID_SEL, intpartid) |
306 		      MPAMCFG_PART_SEL_INTERNAL;
307 
308 	__mpam_part_sel_raw(partsel, msc);
309 }
310 
311 int mpam_register_requestor(u16 partid_max, u8 pmg_max)
312 {
313 	guard(spinlock)(&partid_max_lock);
314 	if (!partid_max_init) {
315 		mpam_partid_max = partid_max;
316 		mpam_pmg_max = pmg_max;
317 		partid_max_init = true;
318 	} else if (!partid_max_published) {
319 		mpam_partid_max = min(mpam_partid_max, partid_max);
320 		mpam_pmg_max = min(mpam_pmg_max, pmg_max);
321 	} else {
322 		/* New requestors can't lower the values */
323 		if (partid_max < mpam_partid_max || pmg_max < mpam_pmg_max)
324 			return -EBUSY;
325 	}
326 
327 	return 0;
328 }
329 EXPORT_SYMBOL(mpam_register_requestor);
330 
331 static struct mpam_class *
332 mpam_class_alloc(u8 level_idx, enum mpam_class_types type)
333 {
334 	struct mpam_class *class;
335 
336 	lockdep_assert_held(&mpam_list_lock);
337 
338 	class = kzalloc_obj(*class);
339 	if (!class)
340 		return ERR_PTR(-ENOMEM);
341 	init_garbage(&class->garbage);
342 
343 	INIT_LIST_HEAD_RCU(&class->components);
344 	/* Affinity is updated when ris are added */
345 	class->level = level_idx;
346 	class->type = type;
347 	INIT_LIST_HEAD_RCU(&class->classes_list);
348 	ida_init(&class->ida_csu_mon);
349 	ida_init(&class->ida_mbwu_mon);
350 
351 	list_add_rcu(&class->classes_list, &mpam_classes);
352 
353 	return class;
354 }
355 
356 static void mpam_class_destroy(struct mpam_class *class)
357 {
358 	lockdep_assert_held(&mpam_list_lock);
359 
360 	list_del_rcu(&class->classes_list);
361 	add_to_garbage(class);
362 }
363 
364 static struct mpam_class *
365 mpam_class_find(u8 level_idx, enum mpam_class_types type)
366 {
367 	struct mpam_class *class;
368 
369 	lockdep_assert_held(&mpam_list_lock);
370 
371 	list_for_each_entry(class, &mpam_classes, classes_list) {
372 		if (class->type == type && class->level == level_idx)
373 			return class;
374 	}
375 
376 	return mpam_class_alloc(level_idx, type);
377 }
378 
379 static struct mpam_component *
380 mpam_component_alloc(struct mpam_class *class, int id)
381 {
382 	struct mpam_component *comp;
383 
384 	lockdep_assert_held(&mpam_list_lock);
385 
386 	comp = kzalloc_obj(*comp);
387 	if (!comp)
388 		return ERR_PTR(-ENOMEM);
389 	init_garbage(&comp->garbage);
390 
391 	comp->comp_id = id;
392 	INIT_LIST_HEAD_RCU(&comp->vmsc);
393 	/* Affinity is updated when RIS are added */
394 	INIT_LIST_HEAD_RCU(&comp->class_list);
395 	comp->class = class;
396 
397 	list_add_rcu(&comp->class_list, &class->components);
398 
399 	return comp;
400 }
401 
402 static void __destroy_component_cfg(struct mpam_component *comp);
403 
404 static void mpam_component_destroy(struct mpam_component *comp)
405 {
406 	struct mpam_class *class = comp->class;
407 
408 	lockdep_assert_held(&mpam_list_lock);
409 
410 	__destroy_component_cfg(comp);
411 
412 	list_del_rcu(&comp->class_list);
413 	add_to_garbage(comp);
414 
415 	if (list_empty(&class->components))
416 		mpam_class_destroy(class);
417 }
418 
419 static struct mpam_component *
420 mpam_component_find(struct mpam_class *class, int id)
421 {
422 	struct mpam_component *comp;
423 
424 	lockdep_assert_held(&mpam_list_lock);
425 
426 	list_for_each_entry(comp, &class->components, class_list) {
427 		if (comp->comp_id == id)
428 			return comp;
429 	}
430 
431 	return mpam_component_alloc(class, id);
432 }
433 
434 static struct mpam_vmsc *
435 mpam_vmsc_alloc(struct mpam_component *comp, struct mpam_msc *msc)
436 {
437 	struct mpam_vmsc *vmsc;
438 
439 	lockdep_assert_held(&mpam_list_lock);
440 
441 	vmsc = kzalloc_obj(*vmsc);
442 	if (!vmsc)
443 		return ERR_PTR(-ENOMEM);
444 	init_garbage(&vmsc->garbage);
445 
446 	INIT_LIST_HEAD_RCU(&vmsc->ris);
447 	INIT_LIST_HEAD_RCU(&vmsc->comp_list);
448 	vmsc->comp = comp;
449 	vmsc->msc = msc;
450 
451 	list_add_rcu(&vmsc->comp_list, &comp->vmsc);
452 
453 	return vmsc;
454 }
455 
456 static void mpam_vmsc_destroy(struct mpam_vmsc *vmsc)
457 {
458 	struct mpam_component *comp = vmsc->comp;
459 
460 	lockdep_assert_held(&mpam_list_lock);
461 
462 	list_del_rcu(&vmsc->comp_list);
463 	add_to_garbage(vmsc);
464 
465 	if (list_empty(&comp->vmsc))
466 		mpam_component_destroy(comp);
467 }
468 
469 static struct mpam_vmsc *
470 mpam_vmsc_find(struct mpam_component *comp, struct mpam_msc *msc)
471 {
472 	struct mpam_vmsc *vmsc;
473 
474 	lockdep_assert_held(&mpam_list_lock);
475 
476 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
477 		if (vmsc->msc->id == msc->id)
478 			return vmsc;
479 	}
480 
481 	return mpam_vmsc_alloc(comp, msc);
482 }
483 
484 /*
485  * The cacheinfo structures are only populated when CPUs are online.
486  * This helper walks the acpi tables to include offline CPUs too.
487  */
488 int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
489 				   cpumask_t *affinity)
490 {
491 	return acpi_pptt_get_cpumask_from_cache_id(cache_id, affinity);
492 }
493 
494 /*
495  * cpumask_of_node() only knows about online CPUs. This can't tell us whether
496  * a class is represented on all possible CPUs.
497  */
498 static void get_cpumask_from_node_id(u32 node_id, cpumask_t *affinity)
499 {
500 	int cpu;
501 
502 	for_each_possible_cpu(cpu) {
503 		if (node_id == cpu_to_node(cpu))
504 			cpumask_set_cpu(cpu, affinity);
505 	}
506 }
507 
508 static int mpam_ris_get_affinity(struct mpam_msc *msc, cpumask_t *affinity,
509 				 enum mpam_class_types type,
510 				 struct mpam_class *class,
511 				 struct mpam_component *comp)
512 {
513 	int err;
514 
515 	switch (type) {
516 	case MPAM_CLASS_CACHE:
517 		err = mpam_get_cpumask_from_cache_id(comp->comp_id, class->level,
518 						     affinity);
519 		if (err) {
520 			dev_warn_once(&msc->pdev->dev,
521 				      "Failed to determine CPU affinity\n");
522 			return err;
523 		}
524 
525 		if (cpumask_empty(affinity))
526 			dev_warn_once(&msc->pdev->dev, "no CPUs associated with cache node\n");
527 
528 		break;
529 	case MPAM_CLASS_MEMORY:
530 		get_cpumask_from_node_id(comp->comp_id, affinity);
531 		/* affinity may be empty for CPU-less memory nodes */
532 		break;
533 	case MPAM_CLASS_UNKNOWN:
534 		return 0;
535 	}
536 
537 	cpumask_and(affinity, affinity, &msc->accessibility);
538 
539 	return 0;
540 }
541 
542 static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
543 				  enum mpam_class_types type, u8 class_id,
544 				  int component_id)
545 {
546 	int err;
547 	struct mpam_vmsc *vmsc;
548 	struct mpam_msc_ris *ris;
549 	struct mpam_class *class;
550 	struct mpam_component *comp;
551 	struct platform_device *pdev = msc->pdev;
552 
553 	lockdep_assert_held(&mpam_list_lock);
554 
555 	if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
556 		return -EINVAL;
557 
558 	if (test_and_set_bit(ris_idx, &msc->ris_idxs))
559 		return -EBUSY;
560 
561 	ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL);
562 	if (!ris)
563 		return -ENOMEM;
564 	init_garbage(&ris->garbage);
565 	ris->garbage.pdev = pdev;
566 
567 	class = mpam_class_find(class_id, type);
568 	if (IS_ERR(class))
569 		return PTR_ERR(class);
570 
571 	comp = mpam_component_find(class, component_id);
572 	if (IS_ERR(comp)) {
573 		if (list_empty(&class->components))
574 			mpam_class_destroy(class);
575 		return PTR_ERR(comp);
576 	}
577 
578 	vmsc = mpam_vmsc_find(comp, msc);
579 	if (IS_ERR(vmsc)) {
580 		if (list_empty(&comp->vmsc))
581 			mpam_component_destroy(comp);
582 		return PTR_ERR(vmsc);
583 	}
584 
585 	err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
586 	if (err) {
587 		if (list_empty(&vmsc->ris))
588 			mpam_vmsc_destroy(vmsc);
589 		return err;
590 	}
591 
592 	ris->ris_idx = ris_idx;
593 	INIT_LIST_HEAD_RCU(&ris->msc_list);
594 	INIT_LIST_HEAD_RCU(&ris->vmsc_list);
595 	ris->vmsc = vmsc;
596 
597 	cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
598 	cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
599 	list_add_rcu(&ris->vmsc_list, &vmsc->ris);
600 	list_add_rcu(&ris->msc_list, &msc->ris);
601 
602 	return 0;
603 }
604 
605 static void mpam_ris_destroy(struct mpam_msc_ris *ris)
606 {
607 	struct mpam_vmsc *vmsc = ris->vmsc;
608 	struct mpam_msc *msc = vmsc->msc;
609 	struct mpam_component *comp = vmsc->comp;
610 	struct mpam_class *class = comp->class;
611 
612 	lockdep_assert_held(&mpam_list_lock);
613 
614 	/*
615 	 * It is assumed affinities don't overlap. If they do the class becomes
616 	 * unusable immediately.
617 	 */
618 	cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity);
619 	cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity);
620 	clear_bit(ris->ris_idx, &msc->ris_idxs);
621 	list_del_rcu(&ris->msc_list);
622 	list_del_rcu(&ris->vmsc_list);
623 	add_to_garbage(ris);
624 
625 	if (list_empty(&vmsc->ris))
626 		mpam_vmsc_destroy(vmsc);
627 }
628 
629 int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
630 		    enum mpam_class_types type, u8 class_id, int component_id)
631 {
632 	int err;
633 
634 	mutex_lock(&mpam_list_lock);
635 	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
636 				     component_id);
637 	mutex_unlock(&mpam_list_lock);
638 	if (err)
639 		mpam_free_garbage();
640 
641 	return err;
642 }
643 
644 static struct mpam_msc_ris *mpam_get_or_create_ris(struct mpam_msc *msc,
645 						   u8 ris_idx)
646 {
647 	int err;
648 	struct mpam_msc_ris *ris;
649 
650 	lockdep_assert_held(&mpam_list_lock);
651 
652 	if (!test_bit(ris_idx, &msc->ris_idxs)) {
653 		err = mpam_ris_create_locked(msc, ris_idx, MPAM_CLASS_UNKNOWN,
654 					     0, 0);
655 		if (err)
656 			return ERR_PTR(err);
657 	}
658 
659 	list_for_each_entry(ris, &msc->ris, msc_list) {
660 		if (ris->ris_idx == ris_idx)
661 			return ris;
662 	}
663 
664 	return ERR_PTR(-ENOENT);
665 }
666 
667 static int mpam_enable_quirk_nvidia_t241_1(struct mpam_msc *msc,
668 					   const struct mpam_quirk *quirk)
669 {
670 	s32 soc_id = arm_smccc_get_soc_id_version();
671 	struct resource *r;
672 	phys_addr_t phys;
673 
674 	/*
675 	 * A mapping to a device other than the MSC is needed, check
676 	 * SOC_ID is  NVIDIA T241 chip (036b:0241)
677 	 */
678 	if (soc_id < 0 || soc_id != SMCCC_SOC_ID_T241)
679 		return -EINVAL;
680 
681 	r = platform_get_resource(msc->pdev, IORESOURCE_MEM, 0);
682 	if (!r)
683 		return -EINVAL;
684 
685 	/* Find the internal registers base addr from the CHIP ID */
686 	msc->t241_id = T241_CHIP_ID(r->start);
687 	phys = FIELD_PREP(GENMASK_ULL(45, 44), msc->t241_id) | 0x19000000ULL;
688 
689 	t241_scratch_regs[msc->t241_id] = ioremap(phys, SZ_8M);
690 	if (WARN_ON_ONCE(!t241_scratch_regs[msc->t241_id]))
691 		return -EINVAL;
692 
693 	pr_info_once("Enabled workaround for NVIDIA T241 erratum T241-MPAM-1\n");
694 
695 	return 0;
696 }
697 
698 static const struct mpam_quirk mpam_quirks[] = {
699 	{
700 		/* NVIDIA t241 erratum T241-MPAM-1 */
701 		.init       = mpam_enable_quirk_nvidia_t241_1,
702 		.iidr       = MPAM_IIDR_NVIDIA_T241,
703 		.iidr_mask  = MPAM_IIDR_MATCH_ONE,
704 		.workaround = T241_SCRUB_SHADOW_REGS,
705 	},
706 	{
707 		/* NVIDIA t241 erratum T241-MPAM-4 */
708 		.iidr       = MPAM_IIDR_NVIDIA_T241,
709 		.iidr_mask  = MPAM_IIDR_MATCH_ONE,
710 		.workaround = T241_FORCE_MBW_MIN_TO_ONE,
711 	},
712 	{
713 		/* NVIDIA t241 erratum T241-MPAM-6 */
714 		.iidr       = MPAM_IIDR_NVIDIA_T241,
715 		.iidr_mask  = MPAM_IIDR_MATCH_ONE,
716 		.workaround = T241_MBW_COUNTER_SCALE_64,
717 	},
718 	{
719 	/* ARM CMN-650 CSU erratum 3642720 */
720 	.iidr       = MPAM_IIDR_ARM_CMN_650,
721 	.iidr_mask  = MPAM_IIDR_MATCH_ONE,
722 	.workaround = IGNORE_CSU_NRDY,
723 	},
724 	{ NULL } /* Sentinel */
725 };
726 
727 static void mpam_enable_quirks(struct mpam_msc *msc)
728 {
729 	const struct mpam_quirk *quirk;
730 
731 	for (quirk = &mpam_quirks[0]; quirk->iidr_mask; quirk++) {
732 		int err = 0;
733 
734 		if (quirk->iidr != (msc->iidr & quirk->iidr_mask))
735 			continue;
736 
737 		if (quirk->init)
738 			err = quirk->init(msc, quirk);
739 
740 		if (err)
741 			continue;
742 
743 		mpam_set_quirk(quirk->workaround, msc);
744 	}
745 }
746 
747 /*
748  * IHI009A.a has this nugget: "If a monitor does not support automatic behaviour
749  * of NRDY, software can use this bit for any purpose" - so hardware might not
750  * implement this - but it isn't RES0.
751  *
752  * Try and see what values stick in this bit. If we can write either value,
753  * its probably not implemented by hardware.
754  */
755 static bool mpam_ris_hw_probe_csu_nrdy(struct mpam_msc_ris *ris)
756 {
757 	u32 now, mon_sel, ctl_val;
758 	bool can_set, can_clear;
759 	struct mpam_msc *msc = ris->vmsc->msc;
760 
761 	if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc)))
762 		return false;
763 
764 	mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, 0) |
765 		  FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx);
766 	mpam_write_monsel_reg(msc, CFG_MON_SEL, mon_sel);
767 
768 	/* Hardware might ignore nrdy if it's not enabled */
769 	ctl_val = MSMON_CFG_CSU_CTL_TYPE_CSU;
770 	ctl_val |= MSMON_CFG_x_CTL_MATCH_PARTID;
771 	ctl_val |= MSMON_CFG_x_CTL_MATCH_PMG;
772 	ctl_val |= MSMON_CFG_x_CTL_EN;
773 	mpam_write_monsel_reg(msc, CFG_CSU_FLT, 0);
774 	mpam_write_monsel_reg(msc, CFG_CSU_CTL, ctl_val);
775 
776 	_mpam_write_monsel_reg(msc, MSMON_CSU, MSMON___NRDY);
777 	now = _mpam_read_monsel_reg(msc, MSMON_CSU);
778 	can_set = now & MSMON___NRDY;
779 
780 	_mpam_write_monsel_reg(msc, MSMON_CSU, 0);
781 	/* Configuration change to try and coax hardware into setting nrdy */
782 	mpam_write_monsel_reg(msc, CFG_CSU_FLT, 0x1);
783 	now = _mpam_read_monsel_reg(msc, MSMON_CSU);
784 	can_clear = !(now & MSMON___NRDY);
785 	mpam_mon_sel_unlock(msc);
786 
787 	return (!can_set || !can_clear);
788 }
789 
790 static void mpam_ris_hw_probe(struct mpam_msc_ris *ris)
791 {
792 	int err;
793 	struct mpam_msc *msc = ris->vmsc->msc;
794 	struct device *dev = &msc->pdev->dev;
795 	struct mpam_props *props = &ris->props;
796 	struct mpam_class *class = ris->vmsc->comp->class;
797 
798 	lockdep_assert_held(&msc->probe_lock);
799 	lockdep_assert_held(&msc->part_sel_lock);
800 
801 	/* Cache Capacity Partitioning */
802 	if (FIELD_GET(MPAMF_IDR_HAS_CCAP_PART, ris->idr)) {
803 		u32 ccap_features = mpam_read_partsel_reg(msc, CCAP_IDR);
804 
805 		props->cmax_wd = FIELD_GET(MPAMF_CCAP_IDR_CMAX_WD, ccap_features);
806 		if (props->cmax_wd &&
807 		    FIELD_GET(MPAMF_CCAP_IDR_HAS_CMAX_SOFTLIM, ccap_features))
808 			mpam_set_feature(mpam_feat_cmax_softlim, props);
809 
810 		if (props->cmax_wd &&
811 		    !FIELD_GET(MPAMF_CCAP_IDR_NO_CMAX, ccap_features))
812 			mpam_set_feature(mpam_feat_cmax_cmax, props);
813 
814 		if (props->cmax_wd &&
815 		    FIELD_GET(MPAMF_CCAP_IDR_HAS_CMIN, ccap_features))
816 			mpam_set_feature(mpam_feat_cmax_cmin, props);
817 
818 		props->cassoc_wd = FIELD_GET(MPAMF_CCAP_IDR_CASSOC_WD, ccap_features);
819 		if (props->cassoc_wd &&
820 		    FIELD_GET(MPAMF_CCAP_IDR_HAS_CASSOC, ccap_features))
821 			mpam_set_feature(mpam_feat_cmax_cassoc, props);
822 	}
823 
824 	/* Cache Portion partitioning */
825 	if (FIELD_GET(MPAMF_IDR_HAS_CPOR_PART, ris->idr)) {
826 		u32 cpor_features = mpam_read_partsel_reg(msc, CPOR_IDR);
827 
828 		props->cpbm_wd = FIELD_GET(MPAMF_CPOR_IDR_CPBM_WD, cpor_features);
829 		if (props->cpbm_wd)
830 			mpam_set_feature(mpam_feat_cpor_part, props);
831 	}
832 
833 	/* Memory bandwidth partitioning */
834 	if (FIELD_GET(MPAMF_IDR_HAS_MBW_PART, ris->idr)) {
835 		u32 mbw_features = mpam_read_partsel_reg(msc, MBW_IDR);
836 
837 		/* portion bitmap resolution */
838 		props->mbw_pbm_bits = FIELD_GET(MPAMF_MBW_IDR_BWPBM_WD, mbw_features);
839 		if (props->mbw_pbm_bits &&
840 		    FIELD_GET(MPAMF_MBW_IDR_HAS_PBM, mbw_features))
841 			mpam_set_feature(mpam_feat_mbw_part, props);
842 
843 		props->bwa_wd = FIELD_GET(MPAMF_MBW_IDR_BWA_WD, mbw_features);
844 
845 		/*
846 		 * The BWA_WD field can represent 0-63, but the control fields it
847 		 * describes have a maximum of 16 bits.
848 		 */
849 		props->bwa_wd = min(props->bwa_wd, 16);
850 
851 		if (props->bwa_wd && FIELD_GET(MPAMF_MBW_IDR_HAS_MAX, mbw_features))
852 			mpam_set_feature(mpam_feat_mbw_max, props);
853 
854 		if (props->bwa_wd && FIELD_GET(MPAMF_MBW_IDR_HAS_MIN, mbw_features))
855 			mpam_set_feature(mpam_feat_mbw_min, props);
856 
857 		if (props->bwa_wd && FIELD_GET(MPAMF_MBW_IDR_HAS_PROP, mbw_features))
858 			mpam_set_feature(mpam_feat_mbw_prop, props);
859 	}
860 
861 	/* Priority partitioning */
862 	if (FIELD_GET(MPAMF_IDR_HAS_PRI_PART, ris->idr)) {
863 		u32 pri_features = mpam_read_partsel_reg(msc, PRI_IDR);
864 
865 		props->intpri_wd = FIELD_GET(MPAMF_PRI_IDR_INTPRI_WD, pri_features);
866 		if (props->intpri_wd && FIELD_GET(MPAMF_PRI_IDR_HAS_INTPRI, pri_features)) {
867 			mpam_set_feature(mpam_feat_intpri_part, props);
868 			if (FIELD_GET(MPAMF_PRI_IDR_INTPRI_0_IS_LOW, pri_features))
869 				mpam_set_feature(mpam_feat_intpri_part_0_low, props);
870 		}
871 
872 		props->dspri_wd = FIELD_GET(MPAMF_PRI_IDR_DSPRI_WD, pri_features);
873 		if (props->dspri_wd && FIELD_GET(MPAMF_PRI_IDR_HAS_DSPRI, pri_features)) {
874 			mpam_set_feature(mpam_feat_dspri_part, props);
875 			if (FIELD_GET(MPAMF_PRI_IDR_DSPRI_0_IS_LOW, pri_features))
876 				mpam_set_feature(mpam_feat_dspri_part_0_low, props);
877 		}
878 	}
879 
880 	/* Performance Monitoring */
881 	if (FIELD_GET(MPAMF_IDR_HAS_MSMON, ris->idr)) {
882 		u32 msmon_features = mpam_read_partsel_reg(msc, MSMON_IDR);
883 
884 		/*
885 		 * If the firmware max-nrdy-us property is missing, the
886 		 * CSU counters can't be used. Should we wait forever?
887 		 */
888 		err = device_property_read_u32(&msc->pdev->dev,
889 					       "arm,not-ready-us",
890 					       &msc->nrdy_usec);
891 
892 		if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_CSU, msmon_features)) {
893 			u32 csumonidr;
894 
895 			csumonidr = mpam_read_partsel_reg(msc, CSUMON_IDR);
896 			props->num_csu_mon = FIELD_GET(MPAMF_CSUMON_IDR_NUM_MON, csumonidr);
897 			if (props->num_csu_mon) {
898 				bool hw_managed;
899 
900 				mpam_set_feature(mpam_feat_msmon_csu, props);
901 
902 				if (FIELD_GET(MPAMF_CSUMON_IDR_HAS_XCL, csumonidr))
903 					mpam_set_feature(mpam_feat_msmon_csu_xcl, props);
904 
905 				/* Is NRDY hardware managed? */
906 				hw_managed = mpam_ris_hw_probe_csu_nrdy(ris);
907 
908 				/*
909 				 * Accept the missing firmware property if NRDY appears
910 				 * un-implemented.
911 				 */
912 				if (err && hw_managed)
913 					dev_err_once(dev, "Counters are not usable because not-ready timeout was not provided by firmware.");
914 			}
915 		}
916 		if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_MBWU, msmon_features)) {
917 			bool has_long;
918 			u32 mbwumon_idr = mpam_read_partsel_reg(msc, MBWUMON_IDR);
919 
920 			props->num_mbwu_mon = FIELD_GET(MPAMF_MBWUMON_IDR_NUM_MON, mbwumon_idr);
921 			if (props->num_mbwu_mon) {
922 				mpam_set_feature(mpam_feat_msmon_mbwu, props);
923 
924 				if (FIELD_GET(MPAMF_MBWUMON_IDR_HAS_RWBW, mbwumon_idr))
925 					mpam_set_feature(mpam_feat_msmon_mbwu_rwbw, props);
926 
927 				has_long = FIELD_GET(MPAMF_MBWUMON_IDR_HAS_LONG, mbwumon_idr);
928 				if (has_long) {
929 					if (FIELD_GET(MPAMF_MBWUMON_IDR_LWD, mbwumon_idr))
930 						mpam_set_feature(mpam_feat_msmon_mbwu_63counter, props);
931 					else
932 						mpam_set_feature(mpam_feat_msmon_mbwu_44counter, props);
933 				} else {
934 					mpam_set_feature(mpam_feat_msmon_mbwu_31counter, props);
935 				}
936 			}
937 		}
938 	}
939 
940 	/*
941 	 * RIS with PARTID narrowing don't have enough storage for one
942 	 * configuration per PARTID. If these are in a class we could use,
943 	 * reduce the supported partid_max to match the number of intpartid.
944 	 * If the class is unknown, just ignore it.
945 	 */
946 	if (FIELD_GET(MPAMF_IDR_HAS_PARTID_NRW, ris->idr) &&
947 	    class->type != MPAM_CLASS_UNKNOWN) {
948 		u32 nrwidr = mpam_read_partsel_reg(msc, PARTID_NRW_IDR);
949 		u16 partid_max = FIELD_GET(MPAMF_PARTID_NRW_IDR_INTPARTID_MAX, nrwidr);
950 
951 		mpam_set_feature(mpam_feat_partid_nrw, props);
952 		msc->partid_max = min(msc->partid_max, partid_max);
953 	}
954 }
955 
956 static int mpam_msc_hw_probe(struct mpam_msc *msc)
957 {
958 	u64 idr;
959 	u16 partid_max;
960 	u8 ris_idx, pmg_max;
961 	struct mpam_msc_ris *ris;
962 	struct device *dev = &msc->pdev->dev;
963 
964 	lockdep_assert_held(&msc->probe_lock);
965 
966 	if (!mpam_msc_check_aidr(msc)) {
967 		dev_err_once(dev, "MSC does not match architecture v1.x\n");
968 		return -EIO;
969 	}
970 
971 	/* Grab an IDR value to find out how many RIS there are */
972 	mutex_lock(&msc->part_sel_lock);
973 	idr = mpam_msc_read_idr(msc);
974 	msc->iidr = mpam_read_partsel_reg(msc, IIDR);
975 	mutex_unlock(&msc->part_sel_lock);
976 
977 	mpam_enable_quirks(msc);
978 
979 	msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr);
980 
981 	/* Use these values so partid/pmg always starts with a valid value */
982 	msc->partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
983 	msc->pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
984 
985 	for (ris_idx = 0; ris_idx <= msc->ris_max; ris_idx++) {
986 		mutex_lock(&msc->part_sel_lock);
987 		__mpam_part_sel(ris_idx, 0, msc);
988 		idr = mpam_msc_read_idr(msc);
989 		mutex_unlock(&msc->part_sel_lock);
990 
991 		partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
992 		pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
993 		msc->partid_max = min(msc->partid_max, partid_max);
994 		msc->pmg_max = min(msc->pmg_max, pmg_max);
995 		msc->has_extd_esr = FIELD_GET(MPAMF_IDR_HAS_EXTD_ESR, idr);
996 
997 		mutex_lock(&mpam_list_lock);
998 		ris = mpam_get_or_create_ris(msc, ris_idx);
999 		mutex_unlock(&mpam_list_lock);
1000 		if (IS_ERR(ris))
1001 			return PTR_ERR(ris);
1002 		ris->idr = idr;
1003 
1004 		mutex_lock(&msc->part_sel_lock);
1005 		__mpam_part_sel(ris_idx, 0, msc);
1006 		mpam_ris_hw_probe(ris);
1007 		mutex_unlock(&msc->part_sel_lock);
1008 	}
1009 
1010 	/* Clear any stale errors */
1011 	mpam_msc_clear_esr(msc);
1012 
1013 	spin_lock(&partid_max_lock);
1014 	mpam_partid_max = min(mpam_partid_max, msc->partid_max);
1015 	mpam_pmg_max = min(mpam_pmg_max, msc->pmg_max);
1016 	spin_unlock(&partid_max_lock);
1017 
1018 	msc->probed = true;
1019 
1020 	return 0;
1021 }
1022 
1023 struct mon_read {
1024 	struct mpam_msc_ris		*ris;
1025 	struct mon_cfg			*ctx;
1026 	enum mpam_device_features	type;
1027 	u64				*val;
1028 	int				err;
1029 	bool				waited_timeout;
1030 };
1031 
1032 static bool mpam_ris_has_mbwu_long_counter(struct mpam_msc_ris *ris)
1033 {
1034 	return (mpam_has_feature(mpam_feat_msmon_mbwu_63counter, &ris->props) ||
1035 		mpam_has_feature(mpam_feat_msmon_mbwu_44counter, &ris->props));
1036 }
1037 
1038 static u64 mpam_msc_read_mbwu_l(struct mpam_msc *msc)
1039 {
1040 	int retry = 3;
1041 	u32 mbwu_l_low;
1042 	u64 mbwu_l_high1, mbwu_l_high2;
1043 
1044 	mpam_mon_sel_lock_held(msc);
1045 
1046 	WARN_ON_ONCE((MSMON_MBWU_L + sizeof(u64)) > msc->mapped_hwpage_sz);
1047 	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
1048 
1049 	mbwu_l_high2 = __mpam_read_reg(msc, MSMON_MBWU_L + 4);
1050 	do {
1051 		mbwu_l_high1 = mbwu_l_high2;
1052 		mbwu_l_low = __mpam_read_reg(msc, MSMON_MBWU_L);
1053 		mbwu_l_high2 = __mpam_read_reg(msc, MSMON_MBWU_L + 4);
1054 
1055 		retry--;
1056 	} while (mbwu_l_high1 != mbwu_l_high2 && retry > 0);
1057 
1058 	if (mbwu_l_high1 == mbwu_l_high2)
1059 		return (mbwu_l_high1 << 32) | mbwu_l_low;
1060 
1061 	pr_warn("Failed to read a stable value\n");
1062 	return MSMON___L_NRDY;
1063 }
1064 
1065 static void mpam_msc_zero_mbwu_l(struct mpam_msc *msc)
1066 {
1067 	mpam_mon_sel_lock_held(msc);
1068 
1069 	WARN_ON_ONCE((MSMON_MBWU_L + sizeof(u64)) > msc->mapped_hwpage_sz);
1070 	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
1071 
1072 	__mpam_write_reg(msc, MSMON_MBWU_L, 0);
1073 	__mpam_write_reg(msc, MSMON_MBWU_L + 4, 0);
1074 }
1075 
1076 static void gen_msmon_ctl_flt_vals(struct mon_read *m, u32 *ctl_val,
1077 				   u32 *flt_val)
1078 {
1079 	struct mon_cfg *ctx = m->ctx;
1080 
1081 	/*
1082 	 * For CSU counters its implementation-defined what happens when not
1083 	 * filtering by partid.
1084 	 */
1085 	*ctl_val = MSMON_CFG_x_CTL_MATCH_PARTID;
1086 
1087 	*flt_val = FIELD_PREP(MSMON_CFG_x_FLT_PARTID, ctx->partid);
1088 
1089 	if (m->ctx->match_pmg) {
1090 		*ctl_val |= MSMON_CFG_x_CTL_MATCH_PMG;
1091 		*flt_val |= FIELD_PREP(MSMON_CFG_x_FLT_PMG, ctx->pmg);
1092 	}
1093 
1094 	switch (m->type) {
1095 	case mpam_feat_msmon_csu:
1096 		*ctl_val |= MSMON_CFG_CSU_CTL_TYPE_CSU;
1097 
1098 		if (mpam_has_feature(mpam_feat_msmon_csu_xcl, &m->ris->props))
1099 			*flt_val |= FIELD_PREP(MSMON_CFG_CSU_FLT_XCL, ctx->csu_exclude_clean);
1100 
1101 		break;
1102 	case mpam_feat_msmon_mbwu_31counter:
1103 	case mpam_feat_msmon_mbwu_44counter:
1104 	case mpam_feat_msmon_mbwu_63counter:
1105 		*ctl_val |= MSMON_CFG_MBWU_CTL_TYPE_MBWU;
1106 
1107 		if (mpam_has_feature(mpam_feat_msmon_mbwu_rwbw, &m->ris->props))
1108 			*flt_val |= FIELD_PREP(MSMON_CFG_MBWU_FLT_RWBW, ctx->opts);
1109 
1110 		break;
1111 	default:
1112 		pr_warn("Unexpected monitor type %d\n", m->type);
1113 	}
1114 }
1115 
1116 static void read_msmon_ctl_flt_vals(struct mon_read *m, u32 *ctl_val,
1117 				    u32 *flt_val)
1118 {
1119 	struct mpam_msc *msc = m->ris->vmsc->msc;
1120 
1121 	switch (m->type) {
1122 	case mpam_feat_msmon_csu:
1123 		*ctl_val = mpam_read_monsel_reg(msc, CFG_CSU_CTL);
1124 		*flt_val = mpam_read_monsel_reg(msc, CFG_CSU_FLT);
1125 		break;
1126 	case mpam_feat_msmon_mbwu_31counter:
1127 	case mpam_feat_msmon_mbwu_44counter:
1128 	case mpam_feat_msmon_mbwu_63counter:
1129 		*ctl_val = mpam_read_monsel_reg(msc, CFG_MBWU_CTL);
1130 		*flt_val = mpam_read_monsel_reg(msc, CFG_MBWU_FLT);
1131 		break;
1132 	default:
1133 		pr_warn("Unexpected monitor type %d\n", m->type);
1134 	}
1135 }
1136 
1137 /* Remove values set by the hardware to prevent apparent mismatches. */
1138 static inline void clean_msmon_ctl_val(u32 *cur_ctl)
1139 {
1140 	*cur_ctl &= ~MSMON_CFG_x_CTL_OFLOW_STATUS;
1141 
1142 	if (FIELD_GET(MSMON_CFG_x_CTL_TYPE, *cur_ctl) == MSMON_CFG_MBWU_CTL_TYPE_MBWU)
1143 		*cur_ctl &= ~MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L;
1144 }
1145 
1146 static void write_msmon_ctl_flt_vals(struct mon_read *m, u32 ctl_val,
1147 				     u32 flt_val)
1148 {
1149 	struct mpam_msc *msc = m->ris->vmsc->msc;
1150 
1151 	/*
1152 	 * Write the ctl_val with the enable bit cleared, reset the counter,
1153 	 * then enable counter.
1154 	 */
1155 	switch (m->type) {
1156 	case mpam_feat_msmon_csu:
1157 		mpam_write_monsel_reg(msc, CFG_CSU_FLT, flt_val);
1158 		mpam_write_monsel_reg(msc, CFG_CSU_CTL, ctl_val);
1159 		mpam_write_monsel_reg(msc, CSU, 0);
1160 		mpam_write_monsel_reg(msc, CFG_CSU_CTL, ctl_val | MSMON_CFG_x_CTL_EN);
1161 		break;
1162 	case mpam_feat_msmon_mbwu_31counter:
1163 	case mpam_feat_msmon_mbwu_44counter:
1164 	case mpam_feat_msmon_mbwu_63counter:
1165 		mpam_write_monsel_reg(msc, CFG_MBWU_FLT, flt_val);
1166 		mpam_write_monsel_reg(msc, CFG_MBWU_CTL, ctl_val);
1167 		mpam_write_monsel_reg(msc, CFG_MBWU_CTL, ctl_val | MSMON_CFG_x_CTL_EN);
1168 		/* Counting monitors require NRDY to be reset by software */
1169 		if (m->type == mpam_feat_msmon_mbwu_31counter)
1170 			mpam_write_monsel_reg(msc, MBWU, 0);
1171 		else
1172 			mpam_msc_zero_mbwu_l(m->ris->vmsc->msc);
1173 		break;
1174 	default:
1175 		pr_warn("Unexpected monitor type %d\n", m->type);
1176 	}
1177 }
1178 
1179 static u64 __mpam_msmon_overflow_val(enum mpam_device_features type)
1180 {
1181 	/* TODO: implement scaling counters */
1182 	switch (type) {
1183 	case mpam_feat_msmon_mbwu_63counter:
1184 		return BIT_ULL(hweight_long(MSMON___LWD_VALUE));
1185 	case mpam_feat_msmon_mbwu_44counter:
1186 		return BIT_ULL(hweight_long(MSMON___L_VALUE));
1187 	case mpam_feat_msmon_mbwu_31counter:
1188 		return BIT_ULL(hweight_long(MSMON___VALUE));
1189 	default:
1190 		return 0;
1191 	}
1192 }
1193 
1194 static u64 mpam_msmon_overflow_val(enum mpam_device_features type,
1195 				   struct mpam_msc *msc)
1196 {
1197 	u64 overflow_val = __mpam_msmon_overflow_val(type);
1198 
1199 	if (mpam_has_quirk(T241_MBW_COUNTER_SCALE_64, msc))
1200 		overflow_val *= 64;
1201 
1202 	return overflow_val;
1203 }
1204 
1205 static void __ris_msmon_read(void *arg)
1206 {
1207 	u64 now;
1208 	bool nrdy = false;
1209 	bool config_mismatch;
1210 	bool overflow = false;
1211 	struct mon_read *m = arg;
1212 	struct mon_cfg *ctx = m->ctx;
1213 	bool reset_on_next_read = false;
1214 	struct mpam_msc_ris *ris = m->ris;
1215 	struct msmon_mbwu_state *mbwu_state;
1216 	struct mpam_msc *msc = m->ris->vmsc->msc;
1217 	u32 mon_sel, ctl_val, flt_val, cur_ctl, cur_flt;
1218 
1219 	if (!mpam_mon_sel_lock(msc)) {
1220 		m->err = -EIO;
1221 		return;
1222 	}
1223 	mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, ctx->mon) |
1224 		  FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx);
1225 	mpam_write_monsel_reg(msc, CFG_MON_SEL, mon_sel);
1226 
1227 	switch (m->type) {
1228 	case mpam_feat_msmon_mbwu_31counter:
1229 	case mpam_feat_msmon_mbwu_44counter:
1230 	case mpam_feat_msmon_mbwu_63counter:
1231 		mbwu_state = &ris->mbwu_state[ctx->mon];
1232 		if (mbwu_state) {
1233 			reset_on_next_read = mbwu_state->reset_on_next_read;
1234 			mbwu_state->reset_on_next_read = false;
1235 		}
1236 		break;
1237 	default:
1238 		break;
1239 	}
1240 
1241 	/*
1242 	 * Read the existing configuration to avoid re-writing the same values.
1243 	 * This saves waiting for 'nrdy' on subsequent reads.
1244 	 */
1245 	read_msmon_ctl_flt_vals(m, &cur_ctl, &cur_flt);
1246 
1247 	if (mpam_feat_msmon_mbwu_31counter == m->type)
1248 		overflow = cur_ctl & MSMON_CFG_x_CTL_OFLOW_STATUS;
1249 	else if (mpam_feat_msmon_mbwu_44counter == m->type ||
1250 		 mpam_feat_msmon_mbwu_63counter == m->type)
1251 		overflow = cur_ctl & MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L;
1252 
1253 	clean_msmon_ctl_val(&cur_ctl);
1254 	gen_msmon_ctl_flt_vals(m, &ctl_val, &flt_val);
1255 	config_mismatch = cur_flt != flt_val ||
1256 			  cur_ctl != (ctl_val | MSMON_CFG_x_CTL_EN);
1257 
1258 	if (config_mismatch || reset_on_next_read) {
1259 		write_msmon_ctl_flt_vals(m, ctl_val, flt_val);
1260 		overflow = false;
1261 	} else if (overflow) {
1262 		mpam_write_monsel_reg(msc, CFG_MBWU_CTL,
1263 				      cur_ctl &
1264 				      ~(MSMON_CFG_x_CTL_OFLOW_STATUS |
1265 					MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L));
1266 	}
1267 
1268 	switch (m->type) {
1269 	case mpam_feat_msmon_csu:
1270 		now = mpam_read_monsel_reg(msc, CSU);
1271 		nrdy = now & MSMON___NRDY;
1272 		now = FIELD_GET(MSMON___VALUE, now);
1273 
1274 		if (mpam_has_quirk(IGNORE_CSU_NRDY, msc) && m->waited_timeout)
1275 			nrdy = false;
1276 
1277 		break;
1278 	case mpam_feat_msmon_mbwu_31counter:
1279 	case mpam_feat_msmon_mbwu_44counter:
1280 	case mpam_feat_msmon_mbwu_63counter:
1281 		if (m->type != mpam_feat_msmon_mbwu_31counter) {
1282 			now = mpam_msc_read_mbwu_l(msc);
1283 			nrdy = now & MSMON___L_NRDY;
1284 
1285 			if (m->type == mpam_feat_msmon_mbwu_63counter)
1286 				now = FIELD_GET(MSMON___LWD_VALUE, now);
1287 			else
1288 				now = FIELD_GET(MSMON___L_VALUE, now);
1289 		} else {
1290 			now = mpam_read_monsel_reg(msc, MBWU);
1291 			nrdy = now & MSMON___NRDY;
1292 			now = FIELD_GET(MSMON___VALUE, now);
1293 		}
1294 
1295 		if (mpam_has_quirk(T241_MBW_COUNTER_SCALE_64, msc))
1296 			now *= 64;
1297 
1298 		if (nrdy)
1299 			break;
1300 
1301 		mbwu_state = &ris->mbwu_state[ctx->mon];
1302 
1303 		if (overflow)
1304 			mbwu_state->correction += mpam_msmon_overflow_val(m->type, msc);
1305 
1306 		/*
1307 		 * Include bandwidth consumed before the last hardware reset and
1308 		 * a counter size increment for each overflow.
1309 		 */
1310 		now += mbwu_state->correction;
1311 		break;
1312 	default:
1313 		m->err = -EINVAL;
1314 	}
1315 	mpam_mon_sel_unlock(msc);
1316 
1317 	if (nrdy)
1318 		m->err = -EBUSY;
1319 
1320 	if (m->err)
1321 		return;
1322 
1323 	*m->val += now;
1324 }
1325 
1326 static int _msmon_read(struct mpam_component *comp, struct mon_read *arg)
1327 {
1328 	int err, any_err = 0;
1329 	struct mpam_vmsc *vmsc;
1330 
1331 	guard(srcu)(&mpam_srcu);
1332 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
1333 				 srcu_read_lock_held(&mpam_srcu)) {
1334 		struct mpam_msc *msc = vmsc->msc;
1335 		struct mpam_msc_ris *ris;
1336 
1337 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
1338 					 srcu_read_lock_held(&mpam_srcu)) {
1339 			arg->ris = ris;
1340 
1341 			err = smp_call_function_any(&msc->accessibility,
1342 						    __ris_msmon_read, arg,
1343 						    true);
1344 			if (!err && arg->err)
1345 				err = arg->err;
1346 
1347 			/*
1348 			 * Save one error to be returned to the caller, but
1349 			 * keep reading counters so that get reprogrammed. On
1350 			 * platforms with NRDY this lets us wait once.
1351 			 */
1352 			if (err)
1353 				any_err = err;
1354 		}
1355 	}
1356 
1357 	return any_err;
1358 }
1359 
1360 static enum mpam_device_features mpam_msmon_choose_counter(struct mpam_class *class)
1361 {
1362 	struct mpam_props *cprops = &class->props;
1363 
1364 	if (mpam_has_feature(mpam_feat_msmon_mbwu_63counter, cprops))
1365 		return mpam_feat_msmon_mbwu_63counter;
1366 	if (mpam_has_feature(mpam_feat_msmon_mbwu_44counter, cprops))
1367 		return mpam_feat_msmon_mbwu_44counter;
1368 
1369 	return mpam_feat_msmon_mbwu_31counter;
1370 }
1371 
1372 int mpam_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx,
1373 		    enum mpam_device_features type, u64 *val)
1374 {
1375 	int err;
1376 	struct mon_read arg;
1377 	u64 wait_jiffies = 0;
1378 	struct mpam_class *class = comp->class;
1379 	struct mpam_props *cprops = &class->props;
1380 
1381 	might_sleep();
1382 
1383 	if (!mpam_is_enabled())
1384 		return -EIO;
1385 
1386 	if (!mpam_has_feature(type, cprops))
1387 		return -EOPNOTSUPP;
1388 
1389 	if (type == mpam_feat_msmon_mbwu)
1390 		type = mpam_msmon_choose_counter(class);
1391 
1392 	arg = (struct mon_read) {
1393 		.ctx = ctx,
1394 		.type = type,
1395 		.val = val,
1396 	};
1397 	*val = 0;
1398 
1399 	err = _msmon_read(comp, &arg);
1400 	if (err == -EBUSY && class->nrdy_usec)
1401 		wait_jiffies = usecs_to_jiffies(class->nrdy_usec);
1402 
1403 	while (wait_jiffies)
1404 		wait_jiffies = schedule_timeout_uninterruptible(wait_jiffies);
1405 
1406 	if (err == -EBUSY) {
1407 		arg = (struct mon_read) {
1408 			.ctx = ctx,
1409 			.type = type,
1410 			.val = val,
1411 			.waited_timeout = true,
1412 		};
1413 		*val = 0;
1414 
1415 		err = _msmon_read(comp, &arg);
1416 	}
1417 
1418 	return err;
1419 }
1420 
1421 void mpam_msmon_reset_mbwu(struct mpam_component *comp, struct mon_cfg *ctx)
1422 {
1423 	struct mpam_msc *msc;
1424 	struct mpam_vmsc *vmsc;
1425 	struct mpam_msc_ris *ris;
1426 
1427 	if (!mpam_is_enabled())
1428 		return;
1429 
1430 	guard(srcu)(&mpam_srcu);
1431 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
1432 				 srcu_read_lock_held(&mpam_srcu)) {
1433 		if (!mpam_has_feature(mpam_feat_msmon_mbwu, &vmsc->props))
1434 			continue;
1435 
1436 		msc = vmsc->msc;
1437 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
1438 					 srcu_read_lock_held(&mpam_srcu)) {
1439 			if (!mpam_has_feature(mpam_feat_msmon_mbwu, &ris->props))
1440 				continue;
1441 
1442 			if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc)))
1443 				continue;
1444 
1445 			ris->mbwu_state[ctx->mon].correction = 0;
1446 			ris->mbwu_state[ctx->mon].reset_on_next_read = true;
1447 			mpam_mon_sel_unlock(msc);
1448 		}
1449 	}
1450 }
1451 
1452 static void mpam_reset_msc_bitmap(struct mpam_msc *msc, u16 reg, u16 wd)
1453 {
1454 	u32 num_words, msb;
1455 	u32 bm = ~0;
1456 	int i;
1457 
1458 	lockdep_assert_held(&msc->part_sel_lock);
1459 
1460 	if (wd == 0)
1461 		return;
1462 
1463 	/*
1464 	 * Write all ~0 to all but the last 32bit-word, which may
1465 	 * have fewer bits...
1466 	 */
1467 	num_words = DIV_ROUND_UP(wd, 32);
1468 	for (i = 0; i < num_words - 1; i++, reg += sizeof(bm))
1469 		__mpam_write_reg(msc, reg, bm);
1470 
1471 	/*
1472 	 * ....and then the last (maybe) partial 32bit word. When wd is a
1473 	 * multiple of 32, msb should be 31 to write a full 32bit word.
1474 	 */
1475 	msb = (wd - 1) % 32;
1476 	bm = GENMASK(msb, 0);
1477 	__mpam_write_reg(msc, reg, bm);
1478 }
1479 
1480 static void mpam_apply_t241_erratum(struct mpam_msc_ris *ris, u16 partid)
1481 {
1482 	int sidx, i, lcount = 1000;
1483 	void __iomem *regs;
1484 	u64 val0, val;
1485 
1486 	regs = t241_scratch_regs[ris->vmsc->msc->t241_id];
1487 
1488 	for (i = 0; i < lcount; i++) {
1489 		/* Read the shadow register at index 0 */
1490 		val0 = readq_relaxed(regs + T241_SHADOW_REG_OFF(0, partid));
1491 
1492 		/* Check if all the shadow registers have the same value */
1493 		for (sidx = 1; sidx < T241_CHIP_NSLICES; sidx++) {
1494 			val = readq_relaxed(regs +
1495 					    T241_SHADOW_REG_OFF(sidx, partid));
1496 			if (val != val0)
1497 				break;
1498 		}
1499 		if (sidx == T241_CHIP_NSLICES)
1500 			break;
1501 	}
1502 
1503 	if (i == lcount)
1504 		pr_warn_once("t241: inconsistent values in shadow regs");
1505 
1506 	/* Write a value zero to spare registers to take effect of MBW conf */
1507 	writeq_relaxed(0, regs + T241_SPARE_REG0_OFF);
1508 	writeq_relaxed(0, regs + T241_SPARE_REG1_OFF);
1509 }
1510 
1511 static void mpam_quirk_post_config_change(struct mpam_msc_ris *ris, u16 partid,
1512 					  struct mpam_config *cfg)
1513 {
1514 	if (mpam_has_quirk(T241_SCRUB_SHADOW_REGS, ris->vmsc->msc))
1515 		mpam_apply_t241_erratum(ris, partid);
1516 }
1517 
1518 static u16 mpam_wa_t241_force_mbw_min_to_one(struct mpam_props *props)
1519 {
1520 	u16 max_hw_value, min_hw_granule, res0_bits;
1521 
1522 	res0_bits = 16 - props->bwa_wd;
1523 	max_hw_value = ((1 << props->bwa_wd) - 1) << res0_bits;
1524 	min_hw_granule = ~max_hw_value;
1525 
1526 	return min_hw_granule + 1;
1527 }
1528 
1529 static u16 mpam_wa_t241_calc_min_from_max(struct mpam_props *props,
1530 					  struct mpam_config *cfg)
1531 {
1532 	u16 val = 0;
1533 	u16 max;
1534 	u16 delta = ((5 * MPAMCFG_MBW_MAX_MAX) / 100) - 1;
1535 
1536 	if (mpam_has_feature(mpam_feat_mbw_max, cfg)) {
1537 		max = cfg->mbw_max;
1538 	} else {
1539 		/* Resetting. Hence, use the ris specific default. */
1540 		max = GENMASK(15, 16 - props->bwa_wd);
1541 	}
1542 
1543 	if (max > delta)
1544 		val = max - delta;
1545 
1546 	return val;
1547 }
1548 
1549 /* Called via IPI. Call while holding an SRCU reference */
1550 static void mpam_reprogram_ris_partid(struct mpam_msc_ris *ris, u16 partid,
1551 				      struct mpam_config *cfg)
1552 {
1553 	u32 pri_val = 0;
1554 	u16 cmax = MPAMCFG_CMAX_CMAX;
1555 	struct mpam_msc *msc = ris->vmsc->msc;
1556 	struct mpam_props *rprops = &ris->props;
1557 	u16 dspri = GENMASK(rprops->dspri_wd, 0);
1558 	u16 intpri = GENMASK(rprops->intpri_wd, 0);
1559 
1560 	mutex_lock(&msc->part_sel_lock);
1561 	__mpam_part_sel(ris->ris_idx, partid, msc);
1562 
1563 	if (mpam_has_feature(mpam_feat_partid_nrw, rprops)) {
1564 		/* Update the intpartid mapping */
1565 		mpam_write_partsel_reg(msc, INTPARTID,
1566 				       MPAMCFG_INTPARTID_INTERNAL | partid);
1567 
1568 		/*
1569 		 * Then switch to the 'internal' partid to update the
1570 		 * configuration.
1571 		 */
1572 		__mpam_intpart_sel(ris->ris_idx, partid, msc);
1573 	}
1574 
1575 	if (mpam_has_feature(mpam_feat_cpor_part, rprops)) {
1576 		if (mpam_has_feature(mpam_feat_cpor_part, cfg))
1577 			mpam_write_partsel_reg(msc, CPBM, cfg->cpbm);
1578 		else
1579 			mpam_reset_msc_bitmap(msc, MPAMCFG_CPBM, rprops->cpbm_wd);
1580 	}
1581 
1582 	if (mpam_has_feature(mpam_feat_mbw_part, rprops)) {
1583 		if (mpam_has_feature(mpam_feat_mbw_part, cfg))
1584 			mpam_reset_msc_bitmap(msc, MPAMCFG_MBW_PBM, rprops->mbw_pbm_bits);
1585 		else
1586 			mpam_write_partsel_reg(msc, MBW_PBM, cfg->mbw_pbm);
1587 	}
1588 
1589 	if (mpam_has_feature(mpam_feat_mbw_min, rprops)) {
1590 		u16 val = 0;
1591 
1592 		if (mpam_has_quirk(T241_FORCE_MBW_MIN_TO_ONE, msc)) {
1593 			u16 min = mpam_wa_t241_force_mbw_min_to_one(rprops);
1594 
1595 			val = mpam_wa_t241_calc_min_from_max(rprops, cfg);
1596 			val = max(val, min);
1597 		}
1598 
1599 		mpam_write_partsel_reg(msc, MBW_MIN, val);
1600 	}
1601 
1602 	if (mpam_has_feature(mpam_feat_mbw_max, rprops)) {
1603 		if (mpam_has_feature(mpam_feat_mbw_max, cfg))
1604 			mpam_write_partsel_reg(msc, MBW_MAX, cfg->mbw_max);
1605 		else
1606 			mpam_write_partsel_reg(msc, MBW_MAX, MPAMCFG_MBW_MAX_MAX);
1607 	}
1608 
1609 	if (mpam_has_feature(mpam_feat_mbw_prop, rprops))
1610 		mpam_write_partsel_reg(msc, MBW_PROP, 0);
1611 
1612 	if (mpam_has_feature(mpam_feat_cmax_cmax, rprops))
1613 		mpam_write_partsel_reg(msc, CMAX, cmax);
1614 
1615 	if (mpam_has_feature(mpam_feat_cmax_cmin, rprops))
1616 		mpam_write_partsel_reg(msc, CMIN, 0);
1617 
1618 	if (mpam_has_feature(mpam_feat_cmax_cassoc, rprops))
1619 		mpam_write_partsel_reg(msc, CASSOC, MPAMCFG_CASSOC_CASSOC);
1620 
1621 	if (mpam_has_feature(mpam_feat_intpri_part, rprops) ||
1622 	    mpam_has_feature(mpam_feat_dspri_part, rprops)) {
1623 		/* aces high? */
1624 		if (!mpam_has_feature(mpam_feat_intpri_part_0_low, rprops))
1625 			intpri = 0;
1626 		if (!mpam_has_feature(mpam_feat_dspri_part_0_low, rprops))
1627 			dspri = 0;
1628 
1629 		if (mpam_has_feature(mpam_feat_intpri_part, rprops))
1630 			pri_val |= FIELD_PREP(MPAMCFG_PRI_INTPRI, intpri);
1631 		if (mpam_has_feature(mpam_feat_dspri_part, rprops))
1632 			pri_val |= FIELD_PREP(MPAMCFG_PRI_DSPRI, dspri);
1633 
1634 		mpam_write_partsel_reg(msc, PRI, pri_val);
1635 	}
1636 
1637 	mpam_quirk_post_config_change(ris, partid, cfg);
1638 
1639 	mutex_unlock(&msc->part_sel_lock);
1640 }
1641 
1642 /* Call with msc cfg_lock held */
1643 static int mpam_restore_mbwu_state(void *_ris)
1644 {
1645 	int i;
1646 	u64 val;
1647 	struct mon_read mwbu_arg;
1648 	struct mpam_msc_ris *ris = _ris;
1649 	struct mpam_class *class = ris->vmsc->comp->class;
1650 
1651 	for (i = 0; i < ris->props.num_mbwu_mon; i++) {
1652 		if (ris->mbwu_state[i].enabled) {
1653 			mwbu_arg.ris = ris;
1654 			mwbu_arg.ctx = &ris->mbwu_state[i].cfg;
1655 			mwbu_arg.type = mpam_msmon_choose_counter(class);
1656 			mwbu_arg.val = &val;
1657 
1658 			__ris_msmon_read(&mwbu_arg);
1659 		}
1660 	}
1661 
1662 	return 0;
1663 }
1664 
1665 /* Call with MSC cfg_lock held */
1666 static int mpam_save_mbwu_state(void *arg)
1667 {
1668 	int i;
1669 	u64 val;
1670 	struct mon_cfg *cfg;
1671 	u32 cur_flt, cur_ctl, mon_sel;
1672 	struct mpam_msc_ris *ris = arg;
1673 	struct msmon_mbwu_state *mbwu_state;
1674 	struct mpam_msc *msc = ris->vmsc->msc;
1675 
1676 	for (i = 0; i < ris->props.num_mbwu_mon; i++) {
1677 		mbwu_state = &ris->mbwu_state[i];
1678 		cfg = &mbwu_state->cfg;
1679 
1680 		if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc)))
1681 			return -EIO;
1682 
1683 		mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, i) |
1684 			  FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx);
1685 		mpam_write_monsel_reg(msc, CFG_MON_SEL, mon_sel);
1686 
1687 		cur_flt = mpam_read_monsel_reg(msc, CFG_MBWU_FLT);
1688 		cur_ctl = mpam_read_monsel_reg(msc, CFG_MBWU_CTL);
1689 		mpam_write_monsel_reg(msc, CFG_MBWU_CTL, 0);
1690 
1691 		if (mpam_ris_has_mbwu_long_counter(ris)) {
1692 			val = mpam_msc_read_mbwu_l(msc);
1693 			mpam_msc_zero_mbwu_l(msc);
1694 		} else {
1695 			val = mpam_read_monsel_reg(msc, MBWU);
1696 			mpam_write_monsel_reg(msc, MBWU, 0);
1697 		}
1698 
1699 		cfg->mon = i;
1700 		cfg->pmg = FIELD_GET(MSMON_CFG_x_FLT_PMG, cur_flt);
1701 		cfg->match_pmg = FIELD_GET(MSMON_CFG_x_CTL_MATCH_PMG, cur_ctl);
1702 		cfg->partid = FIELD_GET(MSMON_CFG_x_FLT_PARTID, cur_flt);
1703 		mbwu_state->correction += val;
1704 		mbwu_state->enabled = FIELD_GET(MSMON_CFG_x_CTL_EN, cur_ctl);
1705 		mpam_mon_sel_unlock(msc);
1706 	}
1707 
1708 	return 0;
1709 }
1710 
1711 /*
1712  * Called via smp_call_on_cpu() to prevent migration, while still being
1713  * pre-emptible. Caller must hold mpam_srcu.
1714  */
1715 static int mpam_reset_ris(void *arg)
1716 {
1717 	u16 partid, partid_max;
1718 	struct mpam_config reset_cfg = {};
1719 	struct mpam_msc_ris *ris = arg;
1720 
1721 	if (ris->in_reset_state)
1722 		return 0;
1723 
1724 	spin_lock(&partid_max_lock);
1725 	partid_max = mpam_partid_max;
1726 	spin_unlock(&partid_max_lock);
1727 	for (partid = 0; partid <= partid_max; partid++)
1728 		mpam_reprogram_ris_partid(ris, partid, &reset_cfg);
1729 
1730 	return 0;
1731 }
1732 
1733 /*
1734  * Get the preferred CPU for this MSC. If it is accessible from this CPU,
1735  * this CPU is preferred. This can be preempted/migrated, it will only result
1736  * in more work.
1737  */
1738 static int mpam_get_msc_preferred_cpu(struct mpam_msc *msc)
1739 {
1740 	int cpu = raw_smp_processor_id();
1741 
1742 	if (cpumask_test_cpu(cpu, &msc->accessibility))
1743 		return cpu;
1744 
1745 	return cpumask_first_and(&msc->accessibility, cpu_online_mask);
1746 }
1747 
1748 static int mpam_touch_msc(struct mpam_msc *msc, int (*fn)(void *a), void *arg)
1749 {
1750 	lockdep_assert_irqs_enabled();
1751 	lockdep_assert_cpus_held();
1752 	WARN_ON_ONCE(!srcu_read_lock_held((&mpam_srcu)));
1753 
1754 	return smp_call_on_cpu(mpam_get_msc_preferred_cpu(msc), fn, arg, true);
1755 }
1756 
1757 struct mpam_write_config_arg {
1758 	struct mpam_msc_ris *ris;
1759 	struct mpam_component *comp;
1760 	u16 partid;
1761 };
1762 
1763 static int __write_config(void *arg)
1764 {
1765 	struct mpam_write_config_arg *c = arg;
1766 
1767 	mpam_reprogram_ris_partid(c->ris, c->partid, &c->comp->cfg[c->partid]);
1768 
1769 	return 0;
1770 }
1771 
1772 static void mpam_reprogram_msc(struct mpam_msc *msc)
1773 {
1774 	u16 partid;
1775 	bool reset;
1776 	struct mpam_config *cfg;
1777 	struct mpam_msc_ris *ris;
1778 	struct mpam_write_config_arg arg;
1779 
1780 	/*
1781 	 * No lock for mpam_partid_max as partid_max_published has been
1782 	 * set by mpam_enabled(), so the values can no longer change.
1783 	 */
1784 	mpam_assert_partid_sizes_fixed();
1785 
1786 	mutex_lock(&msc->cfg_lock);
1787 	list_for_each_entry_srcu(ris, &msc->ris, msc_list,
1788 				 srcu_read_lock_held(&mpam_srcu)) {
1789 		if (!mpam_is_enabled() && !ris->in_reset_state) {
1790 			mpam_touch_msc(msc, &mpam_reset_ris, ris);
1791 			ris->in_reset_state = true;
1792 			continue;
1793 		}
1794 
1795 		arg.comp = ris->vmsc->comp;
1796 		arg.ris = ris;
1797 		reset = true;
1798 		for (partid = 0; partid <= mpam_partid_max; partid++) {
1799 			cfg = &ris->vmsc->comp->cfg[partid];
1800 			if (!bitmap_empty(cfg->features, MPAM_FEATURE_LAST))
1801 				reset = false;
1802 
1803 			arg.partid = partid;
1804 			mpam_touch_msc(msc, __write_config, &arg);
1805 		}
1806 		ris->in_reset_state = reset;
1807 
1808 		if (mpam_has_feature(mpam_feat_msmon_mbwu, &ris->props))
1809 			mpam_touch_msc(msc, &mpam_restore_mbwu_state, ris);
1810 	}
1811 	mutex_unlock(&msc->cfg_lock);
1812 }
1813 
1814 static void _enable_percpu_irq(void *_irq)
1815 {
1816 	int *irq = _irq;
1817 
1818 	enable_percpu_irq(*irq, IRQ_TYPE_NONE);
1819 }
1820 
1821 static int mpam_cpu_online(unsigned int cpu)
1822 {
1823 	struct mpam_msc *msc;
1824 
1825 	guard(srcu)(&mpam_srcu);
1826 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1827 				 srcu_read_lock_held(&mpam_srcu)) {
1828 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1829 			continue;
1830 
1831 		if (msc->reenable_error_ppi)
1832 			_enable_percpu_irq(&msc->reenable_error_ppi);
1833 
1834 		if (atomic_fetch_inc(&msc->online_refs) == 0)
1835 			mpam_reprogram_msc(msc);
1836 	}
1837 
1838 	if (mpam_resctrl_enabled)
1839 		return mpam_resctrl_online_cpu(cpu);
1840 
1841 	return 0;
1842 }
1843 
1844 /* Before mpam is enabled, try to probe new MSC */
1845 static int mpam_discovery_cpu_online(unsigned int cpu)
1846 {
1847 	int err = 0;
1848 	struct mpam_msc *msc;
1849 	bool new_device_probed = false;
1850 
1851 	if (mpam_is_enabled())
1852 		return 0;
1853 
1854 	guard(srcu)(&mpam_srcu);
1855 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1856 				 srcu_read_lock_held(&mpam_srcu)) {
1857 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1858 			continue;
1859 
1860 		mutex_lock(&msc->probe_lock);
1861 		if (!msc->probed)
1862 			err = mpam_msc_hw_probe(msc);
1863 		mutex_unlock(&msc->probe_lock);
1864 
1865 		if (err)
1866 			break;
1867 		new_device_probed = true;
1868 	}
1869 
1870 	if (new_device_probed && !err)
1871 		schedule_work(&mpam_enable_work);
1872 	if (err) {
1873 		mpam_disable_reason = "error during probing";
1874 		schedule_work(&mpam_broken_work);
1875 	}
1876 
1877 	return err;
1878 }
1879 
1880 static int mpam_cpu_offline(unsigned int cpu)
1881 {
1882 	struct mpam_msc *msc;
1883 
1884 	if (mpam_resctrl_enabled)
1885 		mpam_resctrl_offline_cpu(cpu);
1886 
1887 	guard(srcu)(&mpam_srcu);
1888 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1889 				 srcu_read_lock_held(&mpam_srcu)) {
1890 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1891 			continue;
1892 
1893 		if (msc->reenable_error_ppi)
1894 			disable_percpu_irq(msc->reenable_error_ppi);
1895 
1896 		if (atomic_dec_and_test(&msc->online_refs)) {
1897 			struct mpam_msc_ris *ris;
1898 
1899 			mutex_lock(&msc->cfg_lock);
1900 			list_for_each_entry_srcu(ris, &msc->ris, msc_list,
1901 						 srcu_read_lock_held(&mpam_srcu)) {
1902 				mpam_touch_msc(msc, &mpam_reset_ris, ris);
1903 
1904 				/*
1905 				 * The reset state for non-zero partid may be
1906 				 * lost while the CPUs are offline.
1907 				 */
1908 				ris->in_reset_state = false;
1909 
1910 				if (mpam_is_enabled())
1911 					mpam_touch_msc(msc, &mpam_save_mbwu_state, ris);
1912 			}
1913 			mutex_unlock(&msc->cfg_lock);
1914 		}
1915 	}
1916 
1917 	return 0;
1918 }
1919 
1920 static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online),
1921 					  int (*offline)(unsigned int offline),
1922 					  char *name)
1923 {
1924 	mutex_lock(&mpam_cpuhp_state_lock);
1925 	if (mpam_cpuhp_state) {
1926 		cpuhp_remove_state(mpam_cpuhp_state);
1927 		mpam_cpuhp_state = 0;
1928 	}
1929 
1930 	mpam_cpuhp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, name, online,
1931 					     offline);
1932 	if (mpam_cpuhp_state <= 0) {
1933 		pr_err("Failed to register cpuhp callbacks");
1934 		mpam_cpuhp_state = 0;
1935 	}
1936 	mutex_unlock(&mpam_cpuhp_state_lock);
1937 }
1938 
1939 static int __setup_ppi(struct mpam_msc *msc)
1940 {
1941 	int cpu;
1942 
1943 	msc->error_dev_id = alloc_percpu(struct mpam_msc *);
1944 	if (!msc->error_dev_id)
1945 		return -ENOMEM;
1946 
1947 	for_each_cpu(cpu, &msc->accessibility)
1948 		*per_cpu_ptr(msc->error_dev_id, cpu) = msc;
1949 
1950 	return 0;
1951 }
1952 
1953 static int mpam_msc_setup_error_irq(struct mpam_msc *msc)
1954 {
1955 	int irq;
1956 
1957 	irq = platform_get_irq_byname_optional(msc->pdev, "error");
1958 	if (irq <= 0)
1959 		return 0;
1960 
1961 	/* Allocate and initialise the percpu device pointer for PPI */
1962 	if (irq_is_percpu(irq))
1963 		return __setup_ppi(msc);
1964 
1965 	/* sanity check: shared interrupts can be routed anywhere? */
1966 	if (!cpumask_equal(&msc->accessibility, cpu_possible_mask)) {
1967 		pr_err_once("msc:%u is a private resource with a shared error interrupt",
1968 			    msc->id);
1969 		return -EINVAL;
1970 	}
1971 
1972 	return 0;
1973 }
1974 
1975 /*
1976  * An MSC can control traffic from a set of CPUs, but may only be accessible
1977  * from a (hopefully wider) set of CPUs. The common reason for this is power
1978  * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
1979  * corresponding cache may also be powered off. By making accesses from
1980  * one of those CPUs, we ensure we don't access a cache that's powered off.
1981  */
1982 static void update_msc_accessibility(struct mpam_msc *msc)
1983 {
1984 	u32 affinity_id;
1985 	int err;
1986 
1987 	err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",
1988 				       &affinity_id);
1989 	if (err)
1990 		cpumask_copy(&msc->accessibility, cpu_possible_mask);
1991 	else
1992 		acpi_pptt_get_cpus_from_container(affinity_id, &msc->accessibility);
1993 }
1994 
1995 /*
1996  * There are two ways of reaching a struct mpam_msc_ris. Via the
1997  * class->component->vmsc->ris, or via the msc.
1998  * When destroying the msc, the other side needs unlinking and cleaning up too.
1999  */
2000 static void mpam_msc_destroy(struct mpam_msc *msc)
2001 {
2002 	struct platform_device *pdev = msc->pdev;
2003 	struct mpam_msc_ris *ris, *tmp;
2004 
2005 	lockdep_assert_held(&mpam_list_lock);
2006 
2007 	list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list)
2008 		mpam_ris_destroy(ris);
2009 
2010 	list_del_rcu(&msc->all_msc_list);
2011 	platform_set_drvdata(pdev, NULL);
2012 
2013 	add_to_garbage(msc);
2014 }
2015 
2016 static void mpam_msc_drv_remove(struct platform_device *pdev)
2017 {
2018 	struct mpam_msc *msc = platform_get_drvdata(pdev);
2019 
2020 	if (!msc)
2021 		return;
2022 
2023 	mutex_lock(&mpam_list_lock);
2024 	mpam_msc_destroy(msc);
2025 	mutex_unlock(&mpam_list_lock);
2026 
2027 	mpam_free_garbage();
2028 }
2029 
2030 static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
2031 {
2032 	int err;
2033 	u32 tmp;
2034 	struct mpam_msc *msc;
2035 	struct resource *msc_res;
2036 	struct device *dev = &pdev->dev;
2037 
2038 	lockdep_assert_held(&mpam_list_lock);
2039 
2040 	msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
2041 	if (!msc)
2042 		return ERR_PTR(-ENOMEM);
2043 	init_garbage(&msc->garbage);
2044 	msc->garbage.pdev = pdev;
2045 
2046 	err = devm_mutex_init(dev, &msc->probe_lock);
2047 	if (err)
2048 		return ERR_PTR(err);
2049 
2050 	err = devm_mutex_init(dev, &msc->part_sel_lock);
2051 	if (err)
2052 		return ERR_PTR(err);
2053 
2054 	err = devm_mutex_init(dev, &msc->error_irq_lock);
2055 	if (err)
2056 		return ERR_PTR(err);
2057 
2058 	err = devm_mutex_init(dev, &msc->cfg_lock);
2059 	if (err)
2060 		return ERR_PTR(err);
2061 
2062 	mpam_mon_sel_lock_init(msc);
2063 	msc->id = pdev->id;
2064 	msc->pdev = pdev;
2065 	INIT_LIST_HEAD_RCU(&msc->all_msc_list);
2066 	INIT_LIST_HEAD_RCU(&msc->ris);
2067 
2068 	update_msc_accessibility(msc);
2069 	if (cpumask_empty(&msc->accessibility)) {
2070 		dev_err_once(dev, "MSC is not accessible from any CPU!");
2071 		return ERR_PTR(-EINVAL);
2072 	}
2073 
2074 	err = mpam_msc_setup_error_irq(msc);
2075 	if (err)
2076 		return ERR_PTR(err);
2077 
2078 	if (device_property_read_u32(&pdev->dev, "pcc-channel", &tmp))
2079 		msc->iface = MPAM_IFACE_MMIO;
2080 	else
2081 		msc->iface = MPAM_IFACE_PCC;
2082 
2083 	if (msc->iface == MPAM_IFACE_MMIO) {
2084 		void __iomem *io;
2085 
2086 		io = devm_platform_get_and_ioremap_resource(pdev, 0,
2087 							    &msc_res);
2088 		if (IS_ERR(io)) {
2089 			dev_err_once(dev, "Failed to map MSC base address\n");
2090 			return ERR_CAST(io);
2091 		}
2092 		msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
2093 		msc->mapped_hwpage = io;
2094 	} else {
2095 		return ERR_PTR(-EINVAL);
2096 	}
2097 
2098 	list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
2099 	platform_set_drvdata(pdev, msc);
2100 
2101 	return msc;
2102 }
2103 
2104 static int fw_num_msc;
2105 
2106 static int mpam_msc_drv_probe(struct platform_device *pdev)
2107 {
2108 	int err;
2109 	struct mpam_msc *msc = NULL;
2110 	void *plat_data = pdev->dev.platform_data;
2111 
2112 	mutex_lock(&mpam_list_lock);
2113 	msc = do_mpam_msc_drv_probe(pdev);
2114 	mutex_unlock(&mpam_list_lock);
2115 
2116 	if (IS_ERR(msc))
2117 		return PTR_ERR(msc);
2118 
2119 	/* Create RIS entries described by firmware */
2120 	err = acpi_mpam_parse_resources(msc, plat_data);
2121 	if (err) {
2122 		mpam_msc_drv_remove(pdev);
2123 		return err;
2124 	}
2125 
2126 	if (atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
2127 		mpam_register_cpuhp_callbacks(mpam_discovery_cpu_online, NULL,
2128 					      "mpam:drv_probe");
2129 
2130 	return 0;
2131 }
2132 
2133 static struct platform_driver mpam_msc_driver = {
2134 	.driver = {
2135 		.name = "mpam_msc",
2136 		.suppress_bind_attrs = true,
2137 	},
2138 	.probe = mpam_msc_drv_probe,
2139 	.remove = mpam_msc_drv_remove,
2140 };
2141 
2142 /* Any of these features mean the BWA_WD field is valid. */
2143 static bool mpam_has_bwa_wd_feature(struct mpam_props *props)
2144 {
2145 	if (mpam_has_feature(mpam_feat_mbw_min, props))
2146 		return true;
2147 	if (mpam_has_feature(mpam_feat_mbw_max, props))
2148 		return true;
2149 	if (mpam_has_feature(mpam_feat_mbw_prop, props))
2150 		return true;
2151 	return false;
2152 }
2153 
2154 /* Any of these features mean the CMAX_WD field is valid. */
2155 static bool mpam_has_cmax_wd_feature(struct mpam_props *props)
2156 {
2157 	if (mpam_has_feature(mpam_feat_cmax_cmax, props))
2158 		return true;
2159 	if (mpam_has_feature(mpam_feat_cmax_cmin, props))
2160 		return true;
2161 	return false;
2162 }
2163 
2164 #define MISMATCHED_HELPER(parent, child, helper, field, alias)		\
2165 	helper(parent) &&						\
2166 	((helper(child) && (parent)->field != (child)->field) ||	\
2167 	 (!helper(child) && !(alias)))
2168 
2169 #define MISMATCHED_FEAT(parent, child, feat, field, alias)		     \
2170 	mpam_has_feature((feat), (parent)) &&				     \
2171 	((mpam_has_feature((feat), (child)) && (parent)->field != (child)->field) || \
2172 	 (!mpam_has_feature((feat), (child)) && !(alias)))
2173 
2174 #define CAN_MERGE_FEAT(parent, child, feat, alias)			\
2175 	(alias) && !mpam_has_feature((feat), (parent)) &&		\
2176 	mpam_has_feature((feat), (child))
2177 
2178 /*
2179  * Combine two props fields.
2180  * If this is for controls that alias the same resource, it is safe to just
2181  * copy the values over. If two aliasing controls implement the same scheme
2182  * a safe value must be picked.
2183  * For non-aliasing controls, these control different resources, and the
2184  * resulting safe value must be compatible with both. When merging values in
2185  * the tree, all the aliasing resources must be handled first.
2186  * On mismatch, parent is modified.
2187  * Quirks on an MSC will apply to all MSC in that class.
2188  */
2189 static void __props_mismatch(struct mpam_props *parent,
2190 			     struct mpam_props *child, bool alias)
2191 {
2192 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_cpor_part, alias)) {
2193 		parent->cpbm_wd = child->cpbm_wd;
2194 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_cpor_part,
2195 				   cpbm_wd, alias)) {
2196 		pr_debug("cleared cpor_part\n");
2197 		mpam_clear_feature(mpam_feat_cpor_part, parent);
2198 		parent->cpbm_wd = 0;
2199 	}
2200 
2201 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_mbw_part, alias)) {
2202 		parent->mbw_pbm_bits = child->mbw_pbm_bits;
2203 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_mbw_part,
2204 				   mbw_pbm_bits, alias)) {
2205 		pr_debug("cleared mbw_part\n");
2206 		mpam_clear_feature(mpam_feat_mbw_part, parent);
2207 		parent->mbw_pbm_bits = 0;
2208 	}
2209 
2210 	/* bwa_wd is a count of bits, fewer bits means less precision */
2211 	if (alias && !mpam_has_bwa_wd_feature(parent) &&
2212 	    mpam_has_bwa_wd_feature(child)) {
2213 		parent->bwa_wd = child->bwa_wd;
2214 	} else if (MISMATCHED_HELPER(parent, child, mpam_has_bwa_wd_feature,
2215 				     bwa_wd, alias)) {
2216 		pr_debug("took the min bwa_wd\n");
2217 		parent->bwa_wd = min(parent->bwa_wd, child->bwa_wd);
2218 	}
2219 
2220 	if (alias && !mpam_has_cmax_wd_feature(parent) && mpam_has_cmax_wd_feature(child)) {
2221 		parent->cmax_wd = child->cmax_wd;
2222 	} else if (MISMATCHED_HELPER(parent, child, mpam_has_cmax_wd_feature,
2223 				     cmax_wd, alias)) {
2224 		pr_debug("%s took the min cmax_wd\n", __func__);
2225 		parent->cmax_wd = min(parent->cmax_wd, child->cmax_wd);
2226 	}
2227 
2228 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_cmax_cassoc, alias)) {
2229 		parent->cassoc_wd = child->cassoc_wd;
2230 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_cmax_cassoc,
2231 				   cassoc_wd, alias)) {
2232 		pr_debug("%s cleared cassoc_wd\n", __func__);
2233 		mpam_clear_feature(mpam_feat_cmax_cassoc, parent);
2234 		parent->cassoc_wd = 0;
2235 	}
2236 
2237 	/* For num properties, take the minimum */
2238 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_csu, alias)) {
2239 		parent->num_csu_mon = child->num_csu_mon;
2240 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_csu,
2241 				   num_csu_mon, alias)) {
2242 		pr_debug("took the min num_csu_mon\n");
2243 		parent->num_csu_mon = min(parent->num_csu_mon,
2244 					  child->num_csu_mon);
2245 	}
2246 
2247 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_mbwu, alias)) {
2248 		parent->num_mbwu_mon = child->num_mbwu_mon;
2249 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_mbwu,
2250 				   num_mbwu_mon, alias)) {
2251 		pr_debug("took the min num_mbwu_mon\n");
2252 		parent->num_mbwu_mon = min(parent->num_mbwu_mon,
2253 					   child->num_mbwu_mon);
2254 	}
2255 
2256 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_intpri_part, alias)) {
2257 		parent->intpri_wd = child->intpri_wd;
2258 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_intpri_part,
2259 				   intpri_wd, alias)) {
2260 		pr_debug("%s took the min intpri_wd\n", __func__);
2261 		parent->intpri_wd = min(parent->intpri_wd, child->intpri_wd);
2262 	}
2263 
2264 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_dspri_part, alias)) {
2265 		parent->dspri_wd = child->dspri_wd;
2266 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_dspri_part,
2267 				   dspri_wd, alias)) {
2268 		pr_debug("%s took the min dspri_wd\n", __func__);
2269 		parent->dspri_wd = min(parent->dspri_wd, child->dspri_wd);
2270 	}
2271 
2272 	/* TODO: alias support for these two */
2273 	/* {int,ds}pri may not have differing 0-low behaviour */
2274 	if (mpam_has_feature(mpam_feat_intpri_part, parent) &&
2275 	    (!mpam_has_feature(mpam_feat_intpri_part, child) ||
2276 	     mpam_has_feature(mpam_feat_intpri_part_0_low, parent) !=
2277 	     mpam_has_feature(mpam_feat_intpri_part_0_low, child))) {
2278 		pr_debug("%s cleared intpri_part\n", __func__);
2279 		mpam_clear_feature(mpam_feat_intpri_part, parent);
2280 		mpam_clear_feature(mpam_feat_intpri_part_0_low, parent);
2281 	}
2282 	if (mpam_has_feature(mpam_feat_dspri_part, parent) &&
2283 	    (!mpam_has_feature(mpam_feat_dspri_part, child) ||
2284 	     mpam_has_feature(mpam_feat_dspri_part_0_low, parent) !=
2285 	     mpam_has_feature(mpam_feat_dspri_part_0_low, child))) {
2286 		pr_debug("%s cleared dspri_part\n", __func__);
2287 		mpam_clear_feature(mpam_feat_dspri_part, parent);
2288 		mpam_clear_feature(mpam_feat_dspri_part_0_low, parent);
2289 	}
2290 
2291 	if (alias) {
2292 		/* Merge features for aliased resources */
2293 		bitmap_or(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
2294 	} else {
2295 		/* Clear missing features for non aliasing */
2296 		bitmap_and(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
2297 	}
2298 }
2299 
2300 /*
2301  * If a vmsc doesn't match class feature/configuration, do the right thing(tm).
2302  * For 'num' properties we can just take the minimum.
2303  * For properties where the mismatched unused bits would make a difference, we
2304  * nobble the class feature, as we can't configure all the resources.
2305  * e.g. The L3 cache is composed of two resources with 13 and 17 portion
2306  * bitmaps respectively.
2307  * Quirks on an MSC will apply to all MSC in that class.
2308  */
2309 static void
2310 __class_props_mismatch(struct mpam_class *class, struct mpam_vmsc *vmsc)
2311 {
2312 	struct mpam_props *cprops = &class->props;
2313 	struct mpam_props *vprops = &vmsc->props;
2314 	struct device *dev = &vmsc->msc->pdev->dev;
2315 
2316 	lockdep_assert_held(&mpam_list_lock); /* we modify class */
2317 
2318 	dev_dbg(dev, "Merging features for class:0x%lx &= vmsc:0x%lx\n",
2319 		(long)cprops->features, (long)vprops->features);
2320 
2321 	/* Merge quirks */
2322 	class->quirks |= vmsc->msc->quirks;
2323 
2324 	/* Take the safe value for any common features */
2325 	__props_mismatch(cprops, vprops, false);
2326 }
2327 
2328 static void
2329 __vmsc_props_mismatch(struct mpam_vmsc *vmsc, struct mpam_msc_ris *ris)
2330 {
2331 	struct mpam_props *rprops = &ris->props;
2332 	struct mpam_props *vprops = &vmsc->props;
2333 	struct device *dev = &vmsc->msc->pdev->dev;
2334 
2335 	lockdep_assert_held(&mpam_list_lock); /* we modify vmsc */
2336 
2337 	dev_dbg(dev, "Merging features for vmsc:0x%lx |= ris:0x%lx\n",
2338 		(long)vprops->features, (long)rprops->features);
2339 
2340 	/*
2341 	 * Merge mismatched features - Copy any features that aren't common,
2342 	 * but take the safe value for any common features.
2343 	 */
2344 	__props_mismatch(vprops, rprops, true);
2345 }
2346 
2347 /*
2348  * Copy the first component's first vMSC's properties and features to the
2349  * class. __class_props_mismatch() will remove conflicts.
2350  * It is not possible to have a class with no components, or a component with
2351  * no resources. The vMSC properties have already been built.
2352  */
2353 static void mpam_enable_init_class_features(struct mpam_class *class)
2354 {
2355 	struct mpam_vmsc *vmsc;
2356 	struct mpam_component *comp;
2357 
2358 	comp = list_first_entry(&class->components,
2359 				struct mpam_component, class_list);
2360 	vmsc = list_first_entry(&comp->vmsc,
2361 				struct mpam_vmsc, comp_list);
2362 
2363 	class->props = vmsc->props;
2364 }
2365 
2366 static void mpam_enable_merge_vmsc_features(struct mpam_component *comp)
2367 {
2368 	struct mpam_vmsc *vmsc;
2369 	struct mpam_msc_ris *ris;
2370 	struct mpam_class *class = comp->class;
2371 
2372 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
2373 		list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
2374 			__vmsc_props_mismatch(vmsc, ris);
2375 			class->nrdy_usec = max(class->nrdy_usec,
2376 					       vmsc->msc->nrdy_usec);
2377 		}
2378 	}
2379 }
2380 
2381 static void mpam_enable_merge_class_features(struct mpam_component *comp)
2382 {
2383 	struct mpam_vmsc *vmsc;
2384 	struct mpam_class *class = comp->class;
2385 
2386 	list_for_each_entry(vmsc, &comp->vmsc, comp_list)
2387 		__class_props_mismatch(class, vmsc);
2388 
2389 	if (mpam_has_quirk(T241_FORCE_MBW_MIN_TO_ONE, class))
2390 		mpam_clear_feature(mpam_feat_mbw_min, &class->props);
2391 }
2392 
2393 /*
2394  * Merge all the common resource features into class.
2395  * vmsc features are bitwise-or'd together by mpam_enable_merge_vmsc_features()
2396  * as the first step so that mpam_enable_init_class_features() can initialise
2397  * the class with a representative set of features.
2398  * Next the mpam_enable_merge_class_features() bitwise-and's all the vmsc
2399  * features to form the class features.
2400  * Other features are the min/max as appropriate.
2401  *
2402  * To avoid walking the whole tree twice, the class->nrdy_usec property is
2403  * updated when working with the vmsc as it is a max(), and doesn't need
2404  * initialising first.
2405  */
2406 static void mpam_enable_merge_features(struct list_head *all_classes_list)
2407 {
2408 	struct mpam_class *class;
2409 	struct mpam_component *comp;
2410 
2411 	lockdep_assert_held(&mpam_list_lock);
2412 
2413 	list_for_each_entry(class, all_classes_list, classes_list) {
2414 		list_for_each_entry(comp, &class->components, class_list)
2415 			mpam_enable_merge_vmsc_features(comp);
2416 
2417 		mpam_enable_init_class_features(class);
2418 
2419 		list_for_each_entry(comp, &class->components, class_list)
2420 			mpam_enable_merge_class_features(comp);
2421 	}
2422 }
2423 
2424 static char *mpam_errcode_names[16] = {
2425 	[MPAM_ERRCODE_NONE]			= "No error",
2426 	[MPAM_ERRCODE_PARTID_SEL_RANGE]		= "PARTID_SEL_Range",
2427 	[MPAM_ERRCODE_REQ_PARTID_RANGE]		= "Req_PARTID_Range",
2428 	[MPAM_ERRCODE_MSMONCFG_ID_RANGE]	= "MSMONCFG_ID_RANGE",
2429 	[MPAM_ERRCODE_REQ_PMG_RANGE]		= "Req_PMG_Range",
2430 	[MPAM_ERRCODE_MONITOR_RANGE]		= "Monitor_Range",
2431 	[MPAM_ERRCODE_INTPARTID_RANGE]		= "intPARTID_Range",
2432 	[MPAM_ERRCODE_UNEXPECTED_INTERNAL]	= "Unexpected_INTERNAL",
2433 	[MPAM_ERRCODE_UNDEFINED_RIS_PART_SEL]	= "Undefined_RIS_PART_SEL",
2434 	[MPAM_ERRCODE_RIS_NO_CONTROL]		= "RIS_No_Control",
2435 	[MPAM_ERRCODE_UNDEFINED_RIS_MON_SEL]	= "Undefined_RIS_MON_SEL",
2436 	[MPAM_ERRCODE_RIS_NO_MONITOR]		= "RIS_No_Monitor",
2437 	[12 ... 15] = "Reserved"
2438 };
2439 
2440 static int mpam_enable_msc_ecr(void *_msc)
2441 {
2442 	struct mpam_msc *msc = _msc;
2443 
2444 	__mpam_write_reg(msc, MPAMF_ECR, MPAMF_ECR_INTEN);
2445 
2446 	return 0;
2447 }
2448 
2449 /* This can run in mpam_disable(), and the interrupt handler on the same CPU */
2450 static int mpam_disable_msc_ecr(void *_msc)
2451 {
2452 	struct mpam_msc *msc = _msc;
2453 
2454 	__mpam_write_reg(msc, MPAMF_ECR, 0);
2455 
2456 	return 0;
2457 }
2458 
2459 static irqreturn_t __mpam_irq_handler(int irq, struct mpam_msc *msc)
2460 {
2461 	u64 reg;
2462 	u16 partid;
2463 	u8 errcode, pmg, ris;
2464 
2465 	if (WARN_ON_ONCE(!msc) ||
2466 	    WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(),
2467 					   &msc->accessibility)))
2468 		return IRQ_NONE;
2469 
2470 	reg = mpam_msc_read_esr(msc);
2471 
2472 	errcode = FIELD_GET(MPAMF_ESR_ERRCODE, reg);
2473 	if (!errcode)
2474 		return IRQ_NONE;
2475 
2476 	/* Clear level triggered irq */
2477 	mpam_msc_clear_esr(msc);
2478 
2479 	partid = FIELD_GET(MPAMF_ESR_PARTID_MON, reg);
2480 	pmg = FIELD_GET(MPAMF_ESR_PMG, reg);
2481 	ris = FIELD_GET(MPAMF_ESR_RIS, reg);
2482 
2483 	pr_err_ratelimited("error irq from msc:%u '%s', partid:%u, pmg: %u, ris: %u\n",
2484 			   msc->id, mpam_errcode_names[errcode], partid, pmg,
2485 			   ris);
2486 
2487 	/* Disable this interrupt. */
2488 	mpam_disable_msc_ecr(msc);
2489 
2490 	/* Are we racing with the thread disabling MPAM? */
2491 	if (!mpam_is_enabled())
2492 		return IRQ_HANDLED;
2493 
2494 	/*
2495 	 * Schedule the teardown work. Don't use a threaded IRQ as we can't
2496 	 * unregister the interrupt from the threaded part of the handler.
2497 	 */
2498 	mpam_disable_reason = "hardware error interrupt";
2499 	schedule_work(&mpam_broken_work);
2500 
2501 	return IRQ_HANDLED;
2502 }
2503 
2504 static irqreturn_t mpam_ppi_handler(int irq, void *dev_id)
2505 {
2506 	struct mpam_msc *msc = *(struct mpam_msc **)dev_id;
2507 
2508 	return __mpam_irq_handler(irq, msc);
2509 }
2510 
2511 static irqreturn_t mpam_spi_handler(int irq, void *dev_id)
2512 {
2513 	struct mpam_msc *msc = dev_id;
2514 
2515 	return __mpam_irq_handler(irq, msc);
2516 }
2517 
2518 static int mpam_register_irqs(void)
2519 {
2520 	int err, irq;
2521 	struct mpam_msc *msc;
2522 
2523 	lockdep_assert_cpus_held();
2524 
2525 	guard(srcu)(&mpam_srcu);
2526 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
2527 				 srcu_read_lock_held(&mpam_srcu)) {
2528 		irq = platform_get_irq_byname_optional(msc->pdev, "error");
2529 		if (irq <= 0)
2530 			continue;
2531 
2532 		/* The MPAM spec says the interrupt can be SPI, PPI or LPI */
2533 		/* We anticipate sharing the interrupt with other MSCs */
2534 		if (irq_is_percpu(irq)) {
2535 			err = request_percpu_irq(irq, &mpam_ppi_handler,
2536 						 "mpam:msc:error",
2537 						 msc->error_dev_id);
2538 			if (err)
2539 				return err;
2540 
2541 			msc->reenable_error_ppi = irq;
2542 			smp_call_function_many(&msc->accessibility,
2543 					       &_enable_percpu_irq, &irq,
2544 					       true);
2545 		} else {
2546 			err = devm_request_irq(&msc->pdev->dev, irq,
2547 					       &mpam_spi_handler, IRQF_SHARED,
2548 					       "mpam:msc:error", msc);
2549 			if (err)
2550 				return err;
2551 		}
2552 
2553 		mutex_lock(&msc->error_irq_lock);
2554 		msc->error_irq_req = true;
2555 		mpam_touch_msc(msc, mpam_enable_msc_ecr, msc);
2556 		msc->error_irq_hw_enabled = true;
2557 		mutex_unlock(&msc->error_irq_lock);
2558 	}
2559 
2560 	return 0;
2561 }
2562 
2563 static void mpam_unregister_irqs(void)
2564 {
2565 	int irq;
2566 	struct mpam_msc *msc;
2567 
2568 	guard(cpus_read_lock)();
2569 	guard(srcu)(&mpam_srcu);
2570 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
2571 				 srcu_read_lock_held(&mpam_srcu)) {
2572 		irq = platform_get_irq_byname_optional(msc->pdev, "error");
2573 		if (irq <= 0)
2574 			continue;
2575 
2576 		mutex_lock(&msc->error_irq_lock);
2577 		if (msc->error_irq_hw_enabled) {
2578 			mpam_touch_msc(msc, mpam_disable_msc_ecr, msc);
2579 			msc->error_irq_hw_enabled = false;
2580 		}
2581 
2582 		if (msc->error_irq_req) {
2583 			if (irq_is_percpu(irq)) {
2584 				msc->reenable_error_ppi = 0;
2585 				free_percpu_irq(irq, msc->error_dev_id);
2586 			} else {
2587 				devm_free_irq(&msc->pdev->dev, irq, msc);
2588 			}
2589 			msc->error_irq_req = false;
2590 		}
2591 		mutex_unlock(&msc->error_irq_lock);
2592 	}
2593 }
2594 
2595 static void __destroy_component_cfg(struct mpam_component *comp)
2596 {
2597 	struct mpam_msc *msc;
2598 	struct mpam_vmsc *vmsc;
2599 	struct mpam_msc_ris *ris;
2600 
2601 	lockdep_assert_held(&mpam_list_lock);
2602 
2603 	if (!comp->cfg)
2604 		return;
2605 
2606 	add_to_garbage(comp->cfg);
2607 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
2608 		msc = vmsc->msc;
2609 
2610 		if (mpam_mon_sel_lock(msc)) {
2611 			list_for_each_entry(ris, &vmsc->ris, vmsc_list)
2612 				add_to_garbage(ris->mbwu_state);
2613 			mpam_mon_sel_unlock(msc);
2614 		}
2615 	}
2616 }
2617 
2618 static void mpam_reset_component_cfg(struct mpam_component *comp)
2619 {
2620 	int i;
2621 	struct mpam_props *cprops = &comp->class->props;
2622 
2623 	mpam_assert_partid_sizes_fixed();
2624 
2625 	if (!comp->cfg)
2626 		return;
2627 
2628 	for (i = 0; i <= mpam_partid_max; i++) {
2629 		comp->cfg[i] = (struct mpam_config) {};
2630 		if (cprops->cpbm_wd)
2631 			comp->cfg[i].cpbm = GENMASK(cprops->cpbm_wd - 1, 0);
2632 		if (cprops->mbw_pbm_bits)
2633 			comp->cfg[i].mbw_pbm = GENMASK(cprops->mbw_pbm_bits - 1, 0);
2634 		if (cprops->bwa_wd)
2635 			comp->cfg[i].mbw_max = GENMASK(15, 16 - cprops->bwa_wd);
2636 	}
2637 }
2638 
2639 static int __allocate_component_cfg(struct mpam_component *comp)
2640 {
2641 	struct mpam_vmsc *vmsc;
2642 
2643 	mpam_assert_partid_sizes_fixed();
2644 
2645 	if (comp->cfg)
2646 		return 0;
2647 
2648 	comp->cfg = kzalloc_objs(*comp->cfg, mpam_partid_max + 1);
2649 	if (!comp->cfg)
2650 		return -ENOMEM;
2651 
2652 	/*
2653 	 * The array is free()d in one go, so only cfg[0]'s structure needs
2654 	 * to be initialised.
2655 	 */
2656 	init_garbage(&comp->cfg[0].garbage);
2657 
2658 	mpam_reset_component_cfg(comp);
2659 
2660 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
2661 		struct mpam_msc *msc;
2662 		struct mpam_msc_ris *ris;
2663 		struct msmon_mbwu_state *mbwu_state;
2664 
2665 		if (!vmsc->props.num_mbwu_mon)
2666 			continue;
2667 
2668 		msc = vmsc->msc;
2669 		list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
2670 			if (!ris->props.num_mbwu_mon)
2671 				continue;
2672 
2673 			mbwu_state = kzalloc_objs(*ris->mbwu_state,
2674 						  ris->props.num_mbwu_mon);
2675 			if (!mbwu_state) {
2676 				__destroy_component_cfg(comp);
2677 				return -ENOMEM;
2678 			}
2679 
2680 			init_garbage(&mbwu_state[0].garbage);
2681 
2682 			if (mpam_mon_sel_lock(msc)) {
2683 				ris->mbwu_state = mbwu_state;
2684 				mpam_mon_sel_unlock(msc);
2685 			}
2686 		}
2687 	}
2688 
2689 	return 0;
2690 }
2691 
2692 static int mpam_allocate_config(void)
2693 {
2694 	struct mpam_class *class;
2695 	struct mpam_component *comp;
2696 
2697 	lockdep_assert_held(&mpam_list_lock);
2698 
2699 	list_for_each_entry(class, &mpam_classes, classes_list) {
2700 		list_for_each_entry(comp, &class->components, class_list) {
2701 			int err = __allocate_component_cfg(comp);
2702 			if (err)
2703 				return err;
2704 		}
2705 	}
2706 
2707 	return 0;
2708 }
2709 
2710 static void mpam_enable_once(void)
2711 {
2712 	int err;
2713 
2714 	/*
2715 	 * Once the cpuhp callbacks have been changed, mpam_partid_max can no
2716 	 * longer change.
2717 	 */
2718 	spin_lock(&partid_max_lock);
2719 	partid_max_published = true;
2720 	spin_unlock(&partid_max_lock);
2721 
2722 	/*
2723 	 * If all the MSC have been probed, enabling the IRQs happens next.
2724 	 * That involves cross-calling to a CPU that can reach the MSC, and
2725 	 * the locks must be taken in this order:
2726 	 */
2727 	cpus_read_lock();
2728 	mutex_lock(&mpam_list_lock);
2729 	do {
2730 		mpam_enable_merge_features(&mpam_classes);
2731 
2732 		err = mpam_register_irqs();
2733 		if (err) {
2734 			pr_warn("Failed to register irqs: %d\n", err);
2735 			break;
2736 		}
2737 
2738 		err = mpam_allocate_config();
2739 		if (err) {
2740 			pr_err("Failed to allocate configuration arrays.\n");
2741 			break;
2742 		}
2743 	} while (0);
2744 	mutex_unlock(&mpam_list_lock);
2745 	cpus_read_unlock();
2746 
2747 	if (!err) {
2748 		err = mpam_resctrl_setup();
2749 		if (err)
2750 			pr_err("Failed to initialise resctrl: %d\n", err);
2751 	}
2752 
2753 	if (err) {
2754 		mpam_disable_reason = "Failed to enable.";
2755 		schedule_work(&mpam_broken_work);
2756 		return;
2757 	}
2758 
2759 	static_branch_enable(&mpam_enabled);
2760 	mpam_resctrl_enabled = true;
2761 	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline,
2762 				      "mpam:online");
2763 
2764 	/* Use printk() to avoid the pr_fmt adding the function name. */
2765 	printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n",
2766 	       mpam_partid_max + 1, mpam_pmg_max + 1);
2767 }
2768 
2769 static void mpam_reset_component_locked(struct mpam_component *comp)
2770 {
2771 	struct mpam_vmsc *vmsc;
2772 
2773 	lockdep_assert_cpus_held();
2774 	mpam_assert_partid_sizes_fixed();
2775 
2776 	mpam_reset_component_cfg(comp);
2777 
2778 	guard(srcu)(&mpam_srcu);
2779 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
2780 				 srcu_read_lock_held(&mpam_srcu)) {
2781 		struct mpam_msc *msc = vmsc->msc;
2782 		struct mpam_msc_ris *ris;
2783 
2784 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
2785 					 srcu_read_lock_held(&mpam_srcu)) {
2786 			if (!ris->in_reset_state)
2787 				mpam_touch_msc(msc, mpam_reset_ris, ris);
2788 			ris->in_reset_state = true;
2789 		}
2790 	}
2791 }
2792 
2793 void mpam_reset_class_locked(struct mpam_class *class)
2794 {
2795 	struct mpam_component *comp;
2796 
2797 	lockdep_assert_cpus_held();
2798 
2799 	guard(srcu)(&mpam_srcu);
2800 	list_for_each_entry_srcu(comp, &class->components, class_list,
2801 				 srcu_read_lock_held(&mpam_srcu))
2802 		mpam_reset_component_locked(comp);
2803 }
2804 
2805 static void mpam_reset_class(struct mpam_class *class)
2806 {
2807 	cpus_read_lock();
2808 	mpam_reset_class_locked(class);
2809 	cpus_read_unlock();
2810 }
2811 
2812 /*
2813  * Called in response to an error IRQ.
2814  * All of MPAMs errors indicate a software bug, restore any modified
2815  * controls to their reset values.
2816  */
2817 void mpam_disable(struct work_struct *ignored)
2818 {
2819 	int idx;
2820 	bool do_resctrl_exit;
2821 	struct mpam_class *class;
2822 	struct mpam_msc *msc, *tmp;
2823 
2824 	if (mpam_is_enabled())
2825 		static_branch_disable(&mpam_enabled);
2826 
2827 	mutex_lock(&mpam_cpuhp_state_lock);
2828 	if (mpam_cpuhp_state) {
2829 		cpuhp_remove_state(mpam_cpuhp_state);
2830 		mpam_cpuhp_state = 0;
2831 	}
2832 
2833 	/*
2834 	 * Removing the cpuhp state called mpam_cpu_offline() and told resctrl
2835 	 * all the CPUs are offline.
2836 	 */
2837 	do_resctrl_exit = mpam_resctrl_enabled;
2838 	mpam_resctrl_enabled = false;
2839 	mutex_unlock(&mpam_cpuhp_state_lock);
2840 
2841 	if (do_resctrl_exit)
2842 		mpam_resctrl_exit();
2843 
2844 	mpam_unregister_irqs();
2845 
2846 	idx = srcu_read_lock(&mpam_srcu);
2847 	list_for_each_entry_srcu(class, &mpam_classes, classes_list,
2848 				 srcu_read_lock_held(&mpam_srcu)) {
2849 		mpam_reset_class(class);
2850 		if (do_resctrl_exit)
2851 			mpam_resctrl_teardown_class(class);
2852 	}
2853 	srcu_read_unlock(&mpam_srcu, idx);
2854 
2855 	mutex_lock(&mpam_list_lock);
2856 	list_for_each_entry_safe(msc, tmp, &mpam_all_msc, all_msc_list)
2857 		mpam_msc_destroy(msc);
2858 	mutex_unlock(&mpam_list_lock);
2859 	mpam_free_garbage();
2860 
2861 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
2862 }
2863 
2864 /*
2865  * Enable mpam once all devices have been probed.
2866  * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
2867  * Also scheduled when new devices are probed when new CPUs come online.
2868  */
2869 void mpam_enable(struct work_struct *work)
2870 {
2871 	static atomic_t once;
2872 	struct mpam_msc *msc;
2873 	bool all_devices_probed = true;
2874 
2875 	/* Have we probed all the hw devices? */
2876 	guard(srcu)(&mpam_srcu);
2877 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
2878 				 srcu_read_lock_held(&mpam_srcu)) {
2879 		mutex_lock(&msc->probe_lock);
2880 		if (!msc->probed)
2881 			all_devices_probed = false;
2882 		mutex_unlock(&msc->probe_lock);
2883 
2884 		if (!all_devices_probed)
2885 			break;
2886 	}
2887 
2888 	if (all_devices_probed && !atomic_fetch_inc(&once))
2889 		mpam_enable_once();
2890 }
2891 
2892 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
2893 	if (mpam_has_feature(feature, newcfg) &&			\
2894 	    (newcfg)->member != (cfg)->member) {			\
2895 		(cfg)->member = (newcfg)->member;			\
2896 		mpam_set_feature(feature, cfg);				\
2897 									\
2898 		(changes) = true;					\
2899 	}								\
2900 } while (0)
2901 
2902 static bool mpam_update_config(struct mpam_config *cfg,
2903 			       const struct mpam_config *newcfg)
2904 {
2905 	bool has_changes = false;
2906 
2907 	maybe_update_config(cfg, mpam_feat_cpor_part, newcfg, cpbm, has_changes);
2908 	maybe_update_config(cfg, mpam_feat_mbw_part, newcfg, mbw_pbm, has_changes);
2909 	maybe_update_config(cfg, mpam_feat_mbw_max, newcfg, mbw_max, has_changes);
2910 
2911 	return has_changes;
2912 }
2913 
2914 int mpam_apply_config(struct mpam_component *comp, u16 partid,
2915 		      struct mpam_config *cfg)
2916 {
2917 	struct mpam_write_config_arg arg;
2918 	struct mpam_msc_ris *ris;
2919 	struct mpam_vmsc *vmsc;
2920 	struct mpam_msc *msc;
2921 
2922 	lockdep_assert_cpus_held();
2923 
2924 	/* Don't pass in the current config! */
2925 	WARN_ON_ONCE(&comp->cfg[partid] == cfg);
2926 
2927 	if (!mpam_update_config(&comp->cfg[partid], cfg))
2928 		return 0;
2929 
2930 	arg.comp = comp;
2931 	arg.partid = partid;
2932 
2933 	guard(srcu)(&mpam_srcu);
2934 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
2935 				 srcu_read_lock_held(&mpam_srcu)) {
2936 		msc = vmsc->msc;
2937 
2938 		mutex_lock(&msc->cfg_lock);
2939 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
2940 					 srcu_read_lock_held(&mpam_srcu)) {
2941 			arg.ris = ris;
2942 			mpam_touch_msc(msc, __write_config, &arg);
2943 			ris->in_reset_state = false;
2944 		}
2945 		mutex_unlock(&msc->cfg_lock);
2946 	}
2947 
2948 	return 0;
2949 }
2950 
2951 static int __init mpam_msc_driver_init(void)
2952 {
2953 	if (!system_supports_mpam())
2954 		return -EOPNOTSUPP;
2955 
2956 	init_srcu_struct(&mpam_srcu);
2957 
2958 	fw_num_msc = acpi_mpam_count_msc();
2959 	if (fw_num_msc <= 0) {
2960 		pr_err("No MSC devices found in firmware\n");
2961 		return -EINVAL;
2962 	}
2963 
2964 	return platform_driver_register(&mpam_msc_driver);
2965 }
2966 
2967 /* Must occur after arm64_mpam_register_cpus() from arch_initcall() */
2968 subsys_initcall(mpam_msc_driver_init);
2969 
2970 #ifdef CONFIG_MPAM_KUNIT_TEST
2971 #include "test_mpam_devices.c"
2972 #endif
2973