xref: /linux/drivers/resctrl/mpam_devices.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
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 	u16 cmax = MPAMCFG_CMAX_CMAX;
1554 	struct mpam_msc *msc = ris->vmsc->msc;
1555 	struct mpam_props *rprops = &ris->props;
1556 
1557 	mutex_lock(&msc->part_sel_lock);
1558 	__mpam_part_sel(ris->ris_idx, partid, msc);
1559 
1560 	if (mpam_has_feature(mpam_feat_partid_nrw, rprops)) {
1561 		/* Update the intpartid mapping */
1562 		mpam_write_partsel_reg(msc, INTPARTID,
1563 				       MPAMCFG_INTPARTID_INTERNAL | partid);
1564 
1565 		/*
1566 		 * Then switch to the 'internal' partid to update the
1567 		 * configuration.
1568 		 */
1569 		__mpam_intpart_sel(ris->ris_idx, partid, msc);
1570 	}
1571 
1572 	if (mpam_has_feature(mpam_feat_cpor_part, rprops)) {
1573 		if (mpam_has_feature(mpam_feat_cpor_part, cfg))
1574 			mpam_write_partsel_reg(msc, CPBM, cfg->cpbm);
1575 		else
1576 			mpam_reset_msc_bitmap(msc, MPAMCFG_CPBM, rprops->cpbm_wd);
1577 	}
1578 
1579 	if (mpam_has_feature(mpam_feat_mbw_part, rprops)) {
1580 		if (mpam_has_feature(mpam_feat_mbw_part, cfg))
1581 			mpam_write_partsel_reg(msc, MBW_PBM, cfg->mbw_pbm);
1582 		else
1583 			mpam_reset_msc_bitmap(msc, MPAMCFG_MBW_PBM, rprops->mbw_pbm_bits);
1584 	}
1585 
1586 	if (mpam_has_feature(mpam_feat_mbw_min, rprops)) {
1587 		u16 val = 0;
1588 
1589 		if (mpam_has_quirk(T241_FORCE_MBW_MIN_TO_ONE, msc)) {
1590 			u16 min = mpam_wa_t241_force_mbw_min_to_one(rprops);
1591 
1592 			val = mpam_wa_t241_calc_min_from_max(rprops, cfg);
1593 			val = max(val, min);
1594 		}
1595 
1596 		mpam_write_partsel_reg(msc, MBW_MIN, val);
1597 	}
1598 
1599 	if (mpam_has_feature(mpam_feat_mbw_max, rprops)) {
1600 		if (mpam_has_feature(mpam_feat_mbw_max, cfg))
1601 			mpam_write_partsel_reg(msc, MBW_MAX, cfg->mbw_max);
1602 		else
1603 			mpam_write_partsel_reg(msc, MBW_MAX, MPAMCFG_MBW_MAX_MAX);
1604 	}
1605 
1606 	if (mpam_has_feature(mpam_feat_mbw_prop, rprops))
1607 		mpam_write_partsel_reg(msc, MBW_PROP, 0);
1608 
1609 	if (mpam_has_feature(mpam_feat_cmax_cmax, rprops))
1610 		mpam_write_partsel_reg(msc, CMAX, cmax);
1611 
1612 	if (mpam_has_feature(mpam_feat_cmax_cmin, rprops))
1613 		mpam_write_partsel_reg(msc, CMIN, 0);
1614 
1615 	if (mpam_has_feature(mpam_feat_cmax_cassoc, rprops))
1616 		mpam_write_partsel_reg(msc, CASSOC, MPAMCFG_CASSOC_CASSOC);
1617 
1618 	if (mpam_has_feature(mpam_feat_intpri_part, rprops) ||
1619 	    mpam_has_feature(mpam_feat_dspri_part, rprops)) {
1620 		u32 pri_val = 0;
1621 
1622 		if (mpam_has_feature(mpam_feat_intpri_part, rprops)) {
1623 			u16 intpri = GENMASK(rprops->intpri_wd - 1, 0);
1624 
1625 			/* aces high? */
1626 			if (!mpam_has_feature(mpam_feat_intpri_part_0_low, rprops))
1627 				intpri = 0;
1628 
1629 			pri_val |= FIELD_PREP(MPAMCFG_PRI_INTPRI, intpri);
1630 		}
1631 		if (mpam_has_feature(mpam_feat_dspri_part, rprops)) {
1632 			u16 dspri = GENMASK(rprops->dspri_wd - 1, 0);
1633 
1634 			if (!mpam_has_feature(mpam_feat_dspri_part_0_low, rprops))
1635 				dspri = 0;
1636 
1637 			pri_val |= FIELD_PREP(MPAMCFG_PRI_DSPRI, dspri);
1638 		}
1639 
1640 		mpam_write_partsel_reg(msc, PRI, pri_val);
1641 	}
1642 
1643 	mpam_quirk_post_config_change(ris, partid, cfg);
1644 
1645 	mutex_unlock(&msc->part_sel_lock);
1646 }
1647 
1648 /* Call with msc cfg_lock held */
1649 static int mpam_restore_mbwu_state(void *_ris)
1650 {
1651 	int i;
1652 	u64 val;
1653 	struct mon_read mwbu_arg;
1654 	struct mpam_msc_ris *ris = _ris;
1655 	struct mpam_class *class = ris->vmsc->comp->class;
1656 
1657 	for (i = 0; i < ris->props.num_mbwu_mon; i++) {
1658 		if (ris->mbwu_state[i].enabled) {
1659 			mwbu_arg.ris = ris;
1660 			mwbu_arg.ctx = &ris->mbwu_state[i].cfg;
1661 			mwbu_arg.type = mpam_msmon_choose_counter(class);
1662 			mwbu_arg.val = &val;
1663 
1664 			__ris_msmon_read(&mwbu_arg);
1665 		}
1666 	}
1667 
1668 	return 0;
1669 }
1670 
1671 /* Call with MSC cfg_lock held */
1672 static int mpam_save_mbwu_state(void *arg)
1673 {
1674 	int i;
1675 	u64 val;
1676 	struct mon_cfg *cfg;
1677 	u32 cur_flt, cur_ctl, mon_sel;
1678 	struct mpam_msc_ris *ris = arg;
1679 	struct msmon_mbwu_state *mbwu_state;
1680 	struct mpam_msc *msc = ris->vmsc->msc;
1681 
1682 	for (i = 0; i < ris->props.num_mbwu_mon; i++) {
1683 		mbwu_state = &ris->mbwu_state[i];
1684 		cfg = &mbwu_state->cfg;
1685 
1686 		if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc)))
1687 			return -EIO;
1688 
1689 		mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, i) |
1690 			  FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx);
1691 		mpam_write_monsel_reg(msc, CFG_MON_SEL, mon_sel);
1692 
1693 		cur_flt = mpam_read_monsel_reg(msc, CFG_MBWU_FLT);
1694 		cur_ctl = mpam_read_monsel_reg(msc, CFG_MBWU_CTL);
1695 		mpam_write_monsel_reg(msc, CFG_MBWU_CTL, 0);
1696 
1697 		if (mpam_ris_has_mbwu_long_counter(ris)) {
1698 			val = mpam_msc_read_mbwu_l(msc);
1699 			mpam_msc_zero_mbwu_l(msc);
1700 		} else {
1701 			val = mpam_read_monsel_reg(msc, MBWU);
1702 			mpam_write_monsel_reg(msc, MBWU, 0);
1703 		}
1704 
1705 		cfg->mon = i;
1706 		cfg->pmg = FIELD_GET(MSMON_CFG_x_FLT_PMG, cur_flt);
1707 		cfg->match_pmg = FIELD_GET(MSMON_CFG_x_CTL_MATCH_PMG, cur_ctl);
1708 		cfg->partid = FIELD_GET(MSMON_CFG_x_FLT_PARTID, cur_flt);
1709 		mbwu_state->correction += val;
1710 		mbwu_state->enabled = FIELD_GET(MSMON_CFG_x_CTL_EN, cur_ctl);
1711 		mpam_mon_sel_unlock(msc);
1712 	}
1713 
1714 	return 0;
1715 }
1716 
1717 /*
1718  * Called via smp_call_on_cpu() to prevent migration, while still being
1719  * pre-emptible. Caller must hold mpam_srcu.
1720  */
1721 static int mpam_reset_ris(void *arg)
1722 {
1723 	u16 partid, partid_max;
1724 	struct mpam_config reset_cfg = {};
1725 	struct mpam_msc_ris *ris = arg;
1726 
1727 	if (ris->in_reset_state)
1728 		return 0;
1729 
1730 	spin_lock(&partid_max_lock);
1731 	partid_max = mpam_partid_max;
1732 	spin_unlock(&partid_max_lock);
1733 	for (partid = 0; partid <= partid_max; partid++)
1734 		mpam_reprogram_ris_partid(ris, partid, &reset_cfg);
1735 
1736 	return 0;
1737 }
1738 
1739 /*
1740  * Get the preferred CPU for this MSC. If it is accessible from this CPU,
1741  * this CPU is preferred. This can be preempted/migrated, it will only result
1742  * in more work.
1743  */
1744 static int mpam_get_msc_preferred_cpu(struct mpam_msc *msc)
1745 {
1746 	int cpu = raw_smp_processor_id();
1747 
1748 	if (cpumask_test_cpu(cpu, &msc->accessibility))
1749 		return cpu;
1750 
1751 	return cpumask_first_and(&msc->accessibility, cpu_online_mask);
1752 }
1753 
1754 static int mpam_touch_msc(struct mpam_msc *msc, int (*fn)(void *a), void *arg)
1755 {
1756 	lockdep_assert_irqs_enabled();
1757 	lockdep_assert_cpus_held();
1758 	WARN_ON_ONCE(!srcu_read_lock_held((&mpam_srcu)));
1759 
1760 	return smp_call_on_cpu(mpam_get_msc_preferred_cpu(msc), fn, arg, true);
1761 }
1762 
1763 struct mpam_write_config_arg {
1764 	struct mpam_msc_ris *ris;
1765 	struct mpam_component *comp;
1766 	u16 partid;
1767 };
1768 
1769 static int __write_config(void *arg)
1770 {
1771 	struct mpam_write_config_arg *c = arg;
1772 
1773 	mpam_reprogram_ris_partid(c->ris, c->partid, &c->comp->cfg[c->partid]);
1774 
1775 	return 0;
1776 }
1777 
1778 static void mpam_reprogram_msc(struct mpam_msc *msc)
1779 {
1780 	u16 partid;
1781 	bool reset;
1782 	struct mpam_config *cfg;
1783 	struct mpam_msc_ris *ris;
1784 	struct mpam_write_config_arg arg;
1785 
1786 	/*
1787 	 * No lock for mpam_partid_max as partid_max_published has been
1788 	 * set by mpam_enabled(), so the values can no longer change.
1789 	 */
1790 	mpam_assert_partid_sizes_fixed();
1791 
1792 	mutex_lock(&msc->cfg_lock);
1793 	list_for_each_entry_srcu(ris, &msc->ris, msc_list,
1794 				 srcu_read_lock_held(&mpam_srcu)) {
1795 		if (!mpam_is_enabled() && !ris->in_reset_state) {
1796 			mpam_touch_msc(msc, &mpam_reset_ris, ris);
1797 			ris->in_reset_state = true;
1798 			continue;
1799 		}
1800 
1801 		arg.comp = ris->vmsc->comp;
1802 		arg.ris = ris;
1803 		reset = true;
1804 		for (partid = 0; partid <= mpam_partid_max; partid++) {
1805 			cfg = &ris->vmsc->comp->cfg[partid];
1806 			if (!bitmap_empty(cfg->features, MPAM_FEATURE_LAST))
1807 				reset = false;
1808 
1809 			arg.partid = partid;
1810 			mpam_touch_msc(msc, __write_config, &arg);
1811 		}
1812 		ris->in_reset_state = reset;
1813 
1814 		if (mpam_has_feature(mpam_feat_msmon_mbwu, &ris->props))
1815 			mpam_touch_msc(msc, &mpam_restore_mbwu_state, ris);
1816 	}
1817 	mutex_unlock(&msc->cfg_lock);
1818 }
1819 
1820 static void _enable_percpu_irq(void *_irq)
1821 {
1822 	int *irq = _irq;
1823 
1824 	enable_percpu_irq(*irq, IRQ_TYPE_NONE);
1825 }
1826 
1827 static int mpam_cpu_online(unsigned int cpu)
1828 {
1829 	struct mpam_msc *msc;
1830 
1831 	guard(srcu)(&mpam_srcu);
1832 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1833 				 srcu_read_lock_held(&mpam_srcu)) {
1834 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1835 			continue;
1836 
1837 		if (msc->reenable_error_ppi)
1838 			_enable_percpu_irq(&msc->reenable_error_ppi);
1839 
1840 		if (atomic_fetch_inc(&msc->online_refs) == 0)
1841 			mpam_reprogram_msc(msc);
1842 	}
1843 
1844 	if (mpam_resctrl_enabled)
1845 		return mpam_resctrl_online_cpu(cpu);
1846 
1847 	return 0;
1848 }
1849 
1850 /* Before mpam is enabled, try to probe new MSC */
1851 static int mpam_discovery_cpu_online(unsigned int cpu)
1852 {
1853 	int err = 0;
1854 	struct mpam_msc *msc;
1855 	bool new_device_probed = false;
1856 
1857 	if (mpam_is_enabled())
1858 		return 0;
1859 
1860 	guard(srcu)(&mpam_srcu);
1861 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1862 				 srcu_read_lock_held(&mpam_srcu)) {
1863 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1864 			continue;
1865 
1866 		mutex_lock(&msc->probe_lock);
1867 		if (!msc->probed)
1868 			err = mpam_msc_hw_probe(msc);
1869 		mutex_unlock(&msc->probe_lock);
1870 
1871 		if (err)
1872 			break;
1873 		new_device_probed = true;
1874 	}
1875 
1876 	if (new_device_probed && !err)
1877 		schedule_work(&mpam_enable_work);
1878 	if (err) {
1879 		mpam_disable_reason = "error during probing";
1880 		schedule_work(&mpam_broken_work);
1881 	}
1882 
1883 	return err;
1884 }
1885 
1886 static int mpam_cpu_offline(unsigned int cpu)
1887 {
1888 	struct mpam_msc *msc;
1889 
1890 	if (mpam_resctrl_enabled)
1891 		mpam_resctrl_offline_cpu(cpu);
1892 
1893 	guard(srcu)(&mpam_srcu);
1894 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1895 				 srcu_read_lock_held(&mpam_srcu)) {
1896 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1897 			continue;
1898 
1899 		if (msc->reenable_error_ppi)
1900 			disable_percpu_irq(msc->reenable_error_ppi);
1901 
1902 		if (atomic_dec_and_test(&msc->online_refs)) {
1903 			struct mpam_msc_ris *ris;
1904 
1905 			mutex_lock(&msc->cfg_lock);
1906 			list_for_each_entry_srcu(ris, &msc->ris, msc_list,
1907 						 srcu_read_lock_held(&mpam_srcu)) {
1908 				mpam_touch_msc(msc, &mpam_reset_ris, ris);
1909 
1910 				/*
1911 				 * The reset state for non-zero partid may be
1912 				 * lost while the CPUs are offline.
1913 				 */
1914 				ris->in_reset_state = false;
1915 
1916 				if (mpam_is_enabled())
1917 					mpam_touch_msc(msc, &mpam_save_mbwu_state, ris);
1918 			}
1919 			mutex_unlock(&msc->cfg_lock);
1920 		}
1921 	}
1922 
1923 	return 0;
1924 }
1925 
1926 static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online),
1927 					  int (*offline)(unsigned int offline),
1928 					  char *name)
1929 {
1930 	mutex_lock(&mpam_cpuhp_state_lock);
1931 	if (mpam_cpuhp_state) {
1932 		cpuhp_remove_state(mpam_cpuhp_state);
1933 		mpam_cpuhp_state = 0;
1934 	}
1935 
1936 	mpam_cpuhp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, name, online,
1937 					     offline);
1938 	if (mpam_cpuhp_state <= 0) {
1939 		pr_err("Failed to register cpuhp callbacks");
1940 		mpam_cpuhp_state = 0;
1941 	}
1942 	mutex_unlock(&mpam_cpuhp_state_lock);
1943 }
1944 
1945 static int __setup_ppi(struct mpam_msc *msc)
1946 {
1947 	int cpu;
1948 
1949 	msc->error_dev_id = alloc_percpu(struct mpam_msc *);
1950 	if (!msc->error_dev_id)
1951 		return -ENOMEM;
1952 
1953 	for_each_cpu(cpu, &msc->accessibility)
1954 		*per_cpu_ptr(msc->error_dev_id, cpu) = msc;
1955 
1956 	return 0;
1957 }
1958 
1959 static int mpam_msc_setup_error_irq(struct mpam_msc *msc)
1960 {
1961 	int irq;
1962 
1963 	irq = platform_get_irq_byname_optional(msc->pdev, "error");
1964 	if (irq <= 0)
1965 		return 0;
1966 
1967 	/* Allocate and initialise the percpu device pointer for PPI */
1968 	if (irq_is_percpu(irq))
1969 		return __setup_ppi(msc);
1970 
1971 	/* sanity check: shared interrupts can be routed anywhere? */
1972 	if (!cpumask_equal(&msc->accessibility, cpu_possible_mask)) {
1973 		pr_err_once("msc:%u is a private resource with a shared error interrupt",
1974 			    msc->id);
1975 		return -EINVAL;
1976 	}
1977 
1978 	return 0;
1979 }
1980 
1981 /*
1982  * An MSC can control traffic from a set of CPUs, but may only be accessible
1983  * from a (hopefully wider) set of CPUs. The common reason for this is power
1984  * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
1985  * corresponding cache may also be powered off. By making accesses from
1986  * one of those CPUs, we ensure we don't access a cache that's powered off.
1987  */
1988 static void update_msc_accessibility(struct mpam_msc *msc)
1989 {
1990 	u32 affinity_id;
1991 	int err;
1992 
1993 	err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",
1994 				       &affinity_id);
1995 	if (err)
1996 		cpumask_copy(&msc->accessibility, cpu_possible_mask);
1997 	else
1998 		acpi_pptt_get_cpus_from_container(affinity_id, &msc->accessibility);
1999 }
2000 
2001 /*
2002  * There are two ways of reaching a struct mpam_msc_ris. Via the
2003  * class->component->vmsc->ris, or via the msc.
2004  * When destroying the msc, the other side needs unlinking and cleaning up too.
2005  */
2006 static void mpam_msc_destroy(struct mpam_msc *msc)
2007 {
2008 	struct platform_device *pdev = msc->pdev;
2009 	struct mpam_msc_ris *ris, *tmp;
2010 
2011 	lockdep_assert_held(&mpam_list_lock);
2012 
2013 	list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list)
2014 		mpam_ris_destroy(ris);
2015 
2016 	list_del_rcu(&msc->all_msc_list);
2017 	platform_set_drvdata(pdev, NULL);
2018 
2019 	add_to_garbage(msc);
2020 }
2021 
2022 static void mpam_msc_drv_remove(struct platform_device *pdev)
2023 {
2024 	struct mpam_msc *msc = platform_get_drvdata(pdev);
2025 
2026 	if (!msc)
2027 		return;
2028 
2029 	mutex_lock(&mpam_list_lock);
2030 	mpam_msc_destroy(msc);
2031 	mutex_unlock(&mpam_list_lock);
2032 
2033 	mpam_free_garbage();
2034 }
2035 
2036 static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
2037 {
2038 	int err;
2039 	u32 tmp;
2040 	struct mpam_msc *msc;
2041 	struct resource *msc_res;
2042 	struct device *dev = &pdev->dev;
2043 
2044 	lockdep_assert_held(&mpam_list_lock);
2045 
2046 	msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
2047 	if (!msc)
2048 		return ERR_PTR(-ENOMEM);
2049 	init_garbage(&msc->garbage);
2050 	msc->garbage.pdev = pdev;
2051 
2052 	err = devm_mutex_init(dev, &msc->probe_lock);
2053 	if (err)
2054 		return ERR_PTR(err);
2055 
2056 	err = devm_mutex_init(dev, &msc->part_sel_lock);
2057 	if (err)
2058 		return ERR_PTR(err);
2059 
2060 	err = devm_mutex_init(dev, &msc->error_irq_lock);
2061 	if (err)
2062 		return ERR_PTR(err);
2063 
2064 	err = devm_mutex_init(dev, &msc->cfg_lock);
2065 	if (err)
2066 		return ERR_PTR(err);
2067 
2068 	mpam_mon_sel_lock_init(msc);
2069 	msc->id = pdev->id;
2070 	msc->pdev = pdev;
2071 	INIT_LIST_HEAD_RCU(&msc->all_msc_list);
2072 	INIT_LIST_HEAD_RCU(&msc->ris);
2073 
2074 	update_msc_accessibility(msc);
2075 	if (cpumask_empty(&msc->accessibility)) {
2076 		dev_err_once(dev, "MSC is not accessible from any CPU!");
2077 		return ERR_PTR(-EINVAL);
2078 	}
2079 
2080 	err = mpam_msc_setup_error_irq(msc);
2081 	if (err)
2082 		return ERR_PTR(err);
2083 
2084 	if (device_property_read_u32(&pdev->dev, "pcc-channel", &tmp))
2085 		msc->iface = MPAM_IFACE_MMIO;
2086 	else
2087 		msc->iface = MPAM_IFACE_PCC;
2088 
2089 	if (msc->iface == MPAM_IFACE_MMIO) {
2090 		void __iomem *io;
2091 
2092 		io = devm_platform_get_and_ioremap_resource(pdev, 0,
2093 							    &msc_res);
2094 		if (IS_ERR(io)) {
2095 			dev_err_once(dev, "Failed to map MSC base address\n");
2096 			return ERR_CAST(io);
2097 		}
2098 		msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
2099 		msc->mapped_hwpage = io;
2100 	} else {
2101 		return ERR_PTR(-EINVAL);
2102 	}
2103 
2104 	list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
2105 	platform_set_drvdata(pdev, msc);
2106 
2107 	return msc;
2108 }
2109 
2110 static int fw_num_msc;
2111 
2112 static int mpam_msc_drv_probe(struct platform_device *pdev)
2113 {
2114 	int err;
2115 	struct mpam_msc *msc = NULL;
2116 	void *plat_data = pdev->dev.platform_data;
2117 
2118 	mutex_lock(&mpam_list_lock);
2119 	msc = do_mpam_msc_drv_probe(pdev);
2120 	mutex_unlock(&mpam_list_lock);
2121 
2122 	if (IS_ERR(msc))
2123 		return PTR_ERR(msc);
2124 
2125 	/* Create RIS entries described by firmware */
2126 	err = acpi_mpam_parse_resources(msc, plat_data);
2127 	if (err) {
2128 		mpam_msc_drv_remove(pdev);
2129 		return err;
2130 	}
2131 
2132 	if (atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
2133 		mpam_register_cpuhp_callbacks(mpam_discovery_cpu_online, NULL,
2134 					      "mpam:drv_probe");
2135 
2136 	return 0;
2137 }
2138 
2139 static struct platform_driver mpam_msc_driver = {
2140 	.driver = {
2141 		.name = "mpam_msc",
2142 		.suppress_bind_attrs = true,
2143 	},
2144 	.probe = mpam_msc_drv_probe,
2145 	.remove = mpam_msc_drv_remove,
2146 };
2147 
2148 /* Any of these features mean the BWA_WD field is valid. */
2149 static bool mpam_has_bwa_wd_feature(struct mpam_props *props)
2150 {
2151 	if (mpam_has_feature(mpam_feat_mbw_min, props))
2152 		return true;
2153 	if (mpam_has_feature(mpam_feat_mbw_max, props))
2154 		return true;
2155 	if (mpam_has_feature(mpam_feat_mbw_prop, props))
2156 		return true;
2157 	return false;
2158 }
2159 
2160 /* Any of these features mean the CMAX_WD field is valid. */
2161 static bool mpam_has_cmax_wd_feature(struct mpam_props *props)
2162 {
2163 	if (mpam_has_feature(mpam_feat_cmax_cmax, props))
2164 		return true;
2165 	if (mpam_has_feature(mpam_feat_cmax_cmin, props))
2166 		return true;
2167 	return false;
2168 }
2169 
2170 #define MISMATCHED_HELPER(parent, child, helper, field, alias)		\
2171 	helper(parent) &&						\
2172 	((helper(child) && (parent)->field != (child)->field) ||	\
2173 	 (!helper(child) && !(alias)))
2174 
2175 #define MISMATCHED_FEAT(parent, child, feat, field, alias)		     \
2176 	mpam_has_feature((feat), (parent)) &&				     \
2177 	((mpam_has_feature((feat), (child)) && (parent)->field != (child)->field) || \
2178 	 (!mpam_has_feature((feat), (child)) && !(alias)))
2179 
2180 #define CAN_MERGE_FEAT(parent, child, feat, alias)			\
2181 	(alias) && !mpam_has_feature((feat), (parent)) &&		\
2182 	mpam_has_feature((feat), (child))
2183 
2184 /*
2185  * Combine two props fields.
2186  * If this is for controls that alias the same resource, it is safe to just
2187  * copy the values over. If two aliasing controls implement the same scheme
2188  * a safe value must be picked.
2189  * For non-aliasing controls, these control different resources, and the
2190  * resulting safe value must be compatible with both. When merging values in
2191  * the tree, all the aliasing resources must be handled first.
2192  * On mismatch, parent is modified.
2193  * Quirks on an MSC will apply to all MSC in that class.
2194  */
2195 static void __props_mismatch(struct mpam_props *parent,
2196 			     struct mpam_props *child, bool alias)
2197 {
2198 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_cpor_part, alias)) {
2199 		parent->cpbm_wd = child->cpbm_wd;
2200 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_cpor_part,
2201 				   cpbm_wd, alias)) {
2202 		pr_debug("cleared cpor_part\n");
2203 		mpam_clear_feature(mpam_feat_cpor_part, parent);
2204 		parent->cpbm_wd = 0;
2205 	}
2206 
2207 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_mbw_part, alias)) {
2208 		parent->mbw_pbm_bits = child->mbw_pbm_bits;
2209 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_mbw_part,
2210 				   mbw_pbm_bits, alias)) {
2211 		pr_debug("cleared mbw_part\n");
2212 		mpam_clear_feature(mpam_feat_mbw_part, parent);
2213 		parent->mbw_pbm_bits = 0;
2214 	}
2215 
2216 	/* bwa_wd is a count of bits, fewer bits means less precision */
2217 	if (alias && !mpam_has_bwa_wd_feature(parent) &&
2218 	    mpam_has_bwa_wd_feature(child)) {
2219 		parent->bwa_wd = child->bwa_wd;
2220 	} else if (MISMATCHED_HELPER(parent, child, mpam_has_bwa_wd_feature,
2221 				     bwa_wd, alias)) {
2222 		pr_debug("took the min bwa_wd\n");
2223 		parent->bwa_wd = min(parent->bwa_wd, child->bwa_wd);
2224 	}
2225 
2226 	if (alias && !mpam_has_cmax_wd_feature(parent) && mpam_has_cmax_wd_feature(child)) {
2227 		parent->cmax_wd = child->cmax_wd;
2228 	} else if (MISMATCHED_HELPER(parent, child, mpam_has_cmax_wd_feature,
2229 				     cmax_wd, alias)) {
2230 		pr_debug("%s took the min cmax_wd\n", __func__);
2231 		parent->cmax_wd = min(parent->cmax_wd, child->cmax_wd);
2232 	}
2233 
2234 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_cmax_cassoc, alias)) {
2235 		parent->cassoc_wd = child->cassoc_wd;
2236 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_cmax_cassoc,
2237 				   cassoc_wd, alias)) {
2238 		pr_debug("%s cleared cassoc_wd\n", __func__);
2239 		mpam_clear_feature(mpam_feat_cmax_cassoc, parent);
2240 		parent->cassoc_wd = 0;
2241 	}
2242 
2243 	/* For num properties, take the minimum */
2244 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_csu, alias)) {
2245 		parent->num_csu_mon = child->num_csu_mon;
2246 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_csu,
2247 				   num_csu_mon, alias)) {
2248 		pr_debug("took the min num_csu_mon\n");
2249 		parent->num_csu_mon = min(parent->num_csu_mon,
2250 					  child->num_csu_mon);
2251 	}
2252 
2253 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_mbwu, alias)) {
2254 		parent->num_mbwu_mon = child->num_mbwu_mon;
2255 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_mbwu,
2256 				   num_mbwu_mon, alias)) {
2257 		pr_debug("took the min num_mbwu_mon\n");
2258 		parent->num_mbwu_mon = min(parent->num_mbwu_mon,
2259 					   child->num_mbwu_mon);
2260 	}
2261 
2262 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_intpri_part, alias)) {
2263 		parent->intpri_wd = child->intpri_wd;
2264 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_intpri_part,
2265 				   intpri_wd, alias)) {
2266 		pr_debug("%s took the min intpri_wd\n", __func__);
2267 		parent->intpri_wd = min(parent->intpri_wd, child->intpri_wd);
2268 	}
2269 
2270 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_dspri_part, alias)) {
2271 		parent->dspri_wd = child->dspri_wd;
2272 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_dspri_part,
2273 				   dspri_wd, alias)) {
2274 		pr_debug("%s took the min dspri_wd\n", __func__);
2275 		parent->dspri_wd = min(parent->dspri_wd, child->dspri_wd);
2276 	}
2277 
2278 	/* TODO: alias support for these two */
2279 	/* {int,ds}pri may not have differing 0-low behaviour */
2280 	if (mpam_has_feature(mpam_feat_intpri_part, parent) &&
2281 	    (!mpam_has_feature(mpam_feat_intpri_part, child) ||
2282 	     mpam_has_feature(mpam_feat_intpri_part_0_low, parent) !=
2283 	     mpam_has_feature(mpam_feat_intpri_part_0_low, child))) {
2284 		pr_debug("%s cleared intpri_part\n", __func__);
2285 		mpam_clear_feature(mpam_feat_intpri_part, parent);
2286 		mpam_clear_feature(mpam_feat_intpri_part_0_low, parent);
2287 	}
2288 	if (mpam_has_feature(mpam_feat_dspri_part, parent) &&
2289 	    (!mpam_has_feature(mpam_feat_dspri_part, child) ||
2290 	     mpam_has_feature(mpam_feat_dspri_part_0_low, parent) !=
2291 	     mpam_has_feature(mpam_feat_dspri_part_0_low, child))) {
2292 		pr_debug("%s cleared dspri_part\n", __func__);
2293 		mpam_clear_feature(mpam_feat_dspri_part, parent);
2294 		mpam_clear_feature(mpam_feat_dspri_part_0_low, parent);
2295 	}
2296 
2297 	if (alias) {
2298 		/* Merge features for aliased resources */
2299 		bitmap_or(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
2300 	} else {
2301 		/* Clear missing features for non aliasing */
2302 		bitmap_and(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
2303 	}
2304 }
2305 
2306 /*
2307  * If a vmsc doesn't match class feature/configuration, do the right thing(tm).
2308  * For 'num' properties we can just take the minimum.
2309  * For properties where the mismatched unused bits would make a difference, we
2310  * nobble the class feature, as we can't configure all the resources.
2311  * e.g. The L3 cache is composed of two resources with 13 and 17 portion
2312  * bitmaps respectively.
2313  * Quirks on an MSC will apply to all MSC in that class.
2314  */
2315 static void
2316 __class_props_mismatch(struct mpam_class *class, struct mpam_vmsc *vmsc)
2317 {
2318 	struct mpam_props *cprops = &class->props;
2319 	struct mpam_props *vprops = &vmsc->props;
2320 	struct device *dev = &vmsc->msc->pdev->dev;
2321 
2322 	lockdep_assert_held(&mpam_list_lock); /* we modify class */
2323 
2324 	dev_dbg(dev, "Merging features for class:0x%lx &= vmsc:0x%lx\n",
2325 		(long)cprops->features, (long)vprops->features);
2326 
2327 	/* Merge quirks */
2328 	class->quirks |= vmsc->msc->quirks;
2329 
2330 	/* Take the safe value for any common features */
2331 	__props_mismatch(cprops, vprops, false);
2332 }
2333 
2334 static void
2335 __vmsc_props_mismatch(struct mpam_vmsc *vmsc, struct mpam_msc_ris *ris)
2336 {
2337 	struct mpam_props *rprops = &ris->props;
2338 	struct mpam_props *vprops = &vmsc->props;
2339 	struct device *dev = &vmsc->msc->pdev->dev;
2340 
2341 	lockdep_assert_held(&mpam_list_lock); /* we modify vmsc */
2342 
2343 	dev_dbg(dev, "Merging features for vmsc:0x%lx |= ris:0x%lx\n",
2344 		(long)vprops->features, (long)rprops->features);
2345 
2346 	/*
2347 	 * Merge mismatched features - Copy any features that aren't common,
2348 	 * but take the safe value for any common features.
2349 	 */
2350 	__props_mismatch(vprops, rprops, true);
2351 }
2352 
2353 /*
2354  * Copy the first component's first vMSC's properties and features to the
2355  * class. __class_props_mismatch() will remove conflicts.
2356  * It is not possible to have a class with no components, or a component with
2357  * no resources. The vMSC properties have already been built.
2358  */
2359 static void mpam_enable_init_class_features(struct mpam_class *class)
2360 {
2361 	struct mpam_vmsc *vmsc;
2362 	struct mpam_component *comp;
2363 
2364 	comp = list_first_entry(&class->components,
2365 				struct mpam_component, class_list);
2366 	vmsc = list_first_entry(&comp->vmsc,
2367 				struct mpam_vmsc, comp_list);
2368 
2369 	class->props = vmsc->props;
2370 }
2371 
2372 static void mpam_enable_merge_vmsc_features(struct mpam_component *comp)
2373 {
2374 	struct mpam_vmsc *vmsc;
2375 	struct mpam_msc_ris *ris;
2376 	struct mpam_class *class = comp->class;
2377 
2378 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
2379 		list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
2380 			__vmsc_props_mismatch(vmsc, ris);
2381 			class->nrdy_usec = max(class->nrdy_usec,
2382 					       vmsc->msc->nrdy_usec);
2383 		}
2384 	}
2385 }
2386 
2387 static void mpam_enable_merge_class_features(struct mpam_component *comp)
2388 {
2389 	struct mpam_vmsc *vmsc;
2390 	struct mpam_class *class = comp->class;
2391 
2392 	list_for_each_entry(vmsc, &comp->vmsc, comp_list)
2393 		__class_props_mismatch(class, vmsc);
2394 
2395 	if (mpam_has_quirk(T241_FORCE_MBW_MIN_TO_ONE, class))
2396 		mpam_clear_feature(mpam_feat_mbw_min, &class->props);
2397 }
2398 
2399 /*
2400  * Merge all the common resource features into class.
2401  * vmsc features are bitwise-or'd together by mpam_enable_merge_vmsc_features()
2402  * as the first step so that mpam_enable_init_class_features() can initialise
2403  * the class with a representative set of features.
2404  * Next the mpam_enable_merge_class_features() bitwise-and's all the vmsc
2405  * features to form the class features.
2406  * Other features are the min/max as appropriate.
2407  *
2408  * To avoid walking the whole tree twice, the class->nrdy_usec property is
2409  * updated when working with the vmsc as it is a max(), and doesn't need
2410  * initialising first.
2411  */
2412 static void mpam_enable_merge_features(struct list_head *all_classes_list)
2413 {
2414 	struct mpam_class *class;
2415 	struct mpam_component *comp;
2416 
2417 	lockdep_assert_held(&mpam_list_lock);
2418 
2419 	list_for_each_entry(class, all_classes_list, classes_list) {
2420 		list_for_each_entry(comp, &class->components, class_list)
2421 			mpam_enable_merge_vmsc_features(comp);
2422 
2423 		mpam_enable_init_class_features(class);
2424 
2425 		list_for_each_entry(comp, &class->components, class_list)
2426 			mpam_enable_merge_class_features(comp);
2427 	}
2428 }
2429 
2430 static char *mpam_errcode_names[16] = {
2431 	[MPAM_ERRCODE_NONE]			= "No error",
2432 	[MPAM_ERRCODE_PARTID_SEL_RANGE]		= "PARTID_SEL_Range",
2433 	[MPAM_ERRCODE_REQ_PARTID_RANGE]		= "Req_PARTID_Range",
2434 	[MPAM_ERRCODE_MSMONCFG_ID_RANGE]	= "MSMONCFG_ID_RANGE",
2435 	[MPAM_ERRCODE_REQ_PMG_RANGE]		= "Req_PMG_Range",
2436 	[MPAM_ERRCODE_MONITOR_RANGE]		= "Monitor_Range",
2437 	[MPAM_ERRCODE_INTPARTID_RANGE]		= "intPARTID_Range",
2438 	[MPAM_ERRCODE_UNEXPECTED_INTERNAL]	= "Unexpected_INTERNAL",
2439 	[MPAM_ERRCODE_UNDEFINED_RIS_PART_SEL]	= "Undefined_RIS_PART_SEL",
2440 	[MPAM_ERRCODE_RIS_NO_CONTROL]		= "RIS_No_Control",
2441 	[MPAM_ERRCODE_UNDEFINED_RIS_MON_SEL]	= "Undefined_RIS_MON_SEL",
2442 	[MPAM_ERRCODE_RIS_NO_MONITOR]		= "RIS_No_Monitor",
2443 	[12 ... 15] = "Reserved"
2444 };
2445 
2446 static int mpam_enable_msc_ecr(void *_msc)
2447 {
2448 	struct mpam_msc *msc = _msc;
2449 
2450 	__mpam_write_reg(msc, MPAMF_ECR, MPAMF_ECR_INTEN);
2451 
2452 	return 0;
2453 }
2454 
2455 /* This can run in mpam_disable(), and the interrupt handler on the same CPU */
2456 static int mpam_disable_msc_ecr(void *_msc)
2457 {
2458 	struct mpam_msc *msc = _msc;
2459 
2460 	__mpam_write_reg(msc, MPAMF_ECR, 0);
2461 
2462 	return 0;
2463 }
2464 
2465 static irqreturn_t __mpam_irq_handler(int irq, struct mpam_msc *msc)
2466 {
2467 	u64 reg;
2468 	u16 partid;
2469 	u8 errcode, pmg, ris;
2470 
2471 	if (WARN_ON_ONCE(!msc) ||
2472 	    WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(),
2473 					   &msc->accessibility)))
2474 		return IRQ_NONE;
2475 
2476 	reg = mpam_msc_read_esr(msc);
2477 
2478 	errcode = FIELD_GET(MPAMF_ESR_ERRCODE, reg);
2479 	if (!errcode)
2480 		return IRQ_NONE;
2481 
2482 	/* Clear level triggered irq */
2483 	mpam_msc_clear_esr(msc);
2484 
2485 	partid = FIELD_GET(MPAMF_ESR_PARTID_MON, reg);
2486 	pmg = FIELD_GET(MPAMF_ESR_PMG, reg);
2487 	ris = FIELD_GET(MPAMF_ESR_RIS, reg);
2488 
2489 	pr_err_ratelimited("error irq from msc:%u '%s', partid:%u, pmg: %u, ris: %u\n",
2490 			   msc->id, mpam_errcode_names[errcode], partid, pmg,
2491 			   ris);
2492 
2493 	/* Disable this interrupt. */
2494 	mpam_disable_msc_ecr(msc);
2495 
2496 	/* Are we racing with the thread disabling MPAM? */
2497 	if (!mpam_is_enabled())
2498 		return IRQ_HANDLED;
2499 
2500 	/*
2501 	 * Schedule the teardown work. Don't use a threaded IRQ as we can't
2502 	 * unregister the interrupt from the threaded part of the handler.
2503 	 */
2504 	mpam_disable_reason = "hardware error interrupt";
2505 	schedule_work(&mpam_broken_work);
2506 
2507 	return IRQ_HANDLED;
2508 }
2509 
2510 static irqreturn_t mpam_ppi_handler(int irq, void *dev_id)
2511 {
2512 	struct mpam_msc *msc = *(struct mpam_msc **)dev_id;
2513 
2514 	return __mpam_irq_handler(irq, msc);
2515 }
2516 
2517 static irqreturn_t mpam_spi_handler(int irq, void *dev_id)
2518 {
2519 	struct mpam_msc *msc = dev_id;
2520 
2521 	return __mpam_irq_handler(irq, msc);
2522 }
2523 
2524 static int mpam_register_irqs(void)
2525 {
2526 	int err, irq;
2527 	struct mpam_msc *msc;
2528 
2529 	lockdep_assert_cpus_held();
2530 
2531 	guard(srcu)(&mpam_srcu);
2532 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
2533 				 srcu_read_lock_held(&mpam_srcu)) {
2534 		irq = platform_get_irq_byname_optional(msc->pdev, "error");
2535 		if (irq <= 0)
2536 			continue;
2537 
2538 		/* The MPAM spec says the interrupt can be SPI, PPI or LPI */
2539 		/* We anticipate sharing the interrupt with other MSCs */
2540 		if (irq_is_percpu(irq)) {
2541 			err = request_percpu_irq(irq, &mpam_ppi_handler,
2542 						 "mpam:msc:error",
2543 						 msc->error_dev_id);
2544 			if (err)
2545 				return err;
2546 
2547 			msc->reenable_error_ppi = irq;
2548 			smp_call_function_many(&msc->accessibility,
2549 					       &_enable_percpu_irq, &irq,
2550 					       true);
2551 		} else {
2552 			err = devm_request_irq(&msc->pdev->dev, irq,
2553 					       &mpam_spi_handler, IRQF_SHARED,
2554 					       "mpam:msc:error", msc);
2555 			if (err)
2556 				return err;
2557 		}
2558 
2559 		mutex_lock(&msc->error_irq_lock);
2560 		msc->error_irq_req = true;
2561 		mpam_touch_msc(msc, mpam_enable_msc_ecr, msc);
2562 		msc->error_irq_hw_enabled = true;
2563 		mutex_unlock(&msc->error_irq_lock);
2564 	}
2565 
2566 	return 0;
2567 }
2568 
2569 static void mpam_unregister_irqs(void)
2570 {
2571 	int irq;
2572 	struct mpam_msc *msc;
2573 
2574 	guard(cpus_read_lock)();
2575 	guard(srcu)(&mpam_srcu);
2576 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
2577 				 srcu_read_lock_held(&mpam_srcu)) {
2578 		irq = platform_get_irq_byname_optional(msc->pdev, "error");
2579 		if (irq <= 0)
2580 			continue;
2581 
2582 		mutex_lock(&msc->error_irq_lock);
2583 		if (msc->error_irq_hw_enabled) {
2584 			mpam_touch_msc(msc, mpam_disable_msc_ecr, msc);
2585 			msc->error_irq_hw_enabled = false;
2586 		}
2587 
2588 		if (msc->error_irq_req) {
2589 			if (irq_is_percpu(irq)) {
2590 				msc->reenable_error_ppi = 0;
2591 				free_percpu_irq(irq, msc->error_dev_id);
2592 			} else {
2593 				devm_free_irq(&msc->pdev->dev, irq, msc);
2594 			}
2595 			msc->error_irq_req = false;
2596 		}
2597 		mutex_unlock(&msc->error_irq_lock);
2598 	}
2599 }
2600 
2601 static void __destroy_component_cfg(struct mpam_component *comp)
2602 {
2603 	struct mpam_msc *msc;
2604 	struct mpam_vmsc *vmsc;
2605 	struct mpam_msc_ris *ris;
2606 
2607 	lockdep_assert_held(&mpam_list_lock);
2608 
2609 	if (!comp->cfg)
2610 		return;
2611 
2612 	add_to_garbage(comp->cfg);
2613 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
2614 		msc = vmsc->msc;
2615 
2616 		if (mpam_mon_sel_lock(msc)) {
2617 			list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
2618 				if (ris->mbwu_state)
2619 					add_to_garbage(ris->mbwu_state);
2620 			}
2621 			mpam_mon_sel_unlock(msc);
2622 		}
2623 	}
2624 }
2625 
2626 static void mpam_reset_component_cfg(struct mpam_component *comp)
2627 {
2628 	int i;
2629 	struct mpam_props *cprops = &comp->class->props;
2630 
2631 	mpam_assert_partid_sizes_fixed();
2632 
2633 	if (!comp->cfg)
2634 		return;
2635 
2636 	for (i = 0; i <= mpam_partid_max; i++) {
2637 		comp->cfg[i] = (struct mpam_config) {};
2638 		if (cprops->cpbm_wd)
2639 			comp->cfg[i].cpbm = GENMASK(cprops->cpbm_wd - 1, 0);
2640 		if (cprops->mbw_pbm_bits)
2641 			comp->cfg[i].mbw_pbm = GENMASK(cprops->mbw_pbm_bits - 1, 0);
2642 		if (cprops->bwa_wd)
2643 			comp->cfg[i].mbw_max = GENMASK(15, 16 - cprops->bwa_wd);
2644 	}
2645 }
2646 
2647 static int __allocate_component_cfg(struct mpam_component *comp)
2648 {
2649 	struct mpam_vmsc *vmsc;
2650 
2651 	mpam_assert_partid_sizes_fixed();
2652 
2653 	if (comp->cfg)
2654 		return 0;
2655 
2656 	comp->cfg = kzalloc_objs(*comp->cfg, mpam_partid_max + 1);
2657 	if (!comp->cfg)
2658 		return -ENOMEM;
2659 
2660 	/*
2661 	 * The array is free()d in one go, so only cfg[0]'s structure needs
2662 	 * to be initialised.
2663 	 */
2664 	init_garbage(&comp->cfg[0].garbage);
2665 
2666 	mpam_reset_component_cfg(comp);
2667 
2668 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
2669 		struct mpam_msc *msc;
2670 		struct mpam_msc_ris *ris;
2671 		struct msmon_mbwu_state *mbwu_state;
2672 
2673 		if (!vmsc->props.num_mbwu_mon)
2674 			continue;
2675 
2676 		msc = vmsc->msc;
2677 		list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
2678 			if (!ris->props.num_mbwu_mon)
2679 				continue;
2680 
2681 			mbwu_state = kzalloc_objs(*ris->mbwu_state,
2682 						  ris->props.num_mbwu_mon);
2683 			if (!mbwu_state) {
2684 				__destroy_component_cfg(comp);
2685 				return -ENOMEM;
2686 			}
2687 
2688 			init_garbage(&mbwu_state[0].garbage);
2689 
2690 			if (mpam_mon_sel_lock(msc)) {
2691 				ris->mbwu_state = mbwu_state;
2692 				mpam_mon_sel_unlock(msc);
2693 			}
2694 		}
2695 	}
2696 
2697 	return 0;
2698 }
2699 
2700 static int mpam_allocate_config(void)
2701 {
2702 	struct mpam_class *class;
2703 	struct mpam_component *comp;
2704 
2705 	lockdep_assert_held(&mpam_list_lock);
2706 
2707 	list_for_each_entry(class, &mpam_classes, classes_list) {
2708 		list_for_each_entry(comp, &class->components, class_list) {
2709 			int err = __allocate_component_cfg(comp);
2710 			if (err)
2711 				return err;
2712 		}
2713 	}
2714 
2715 	return 0;
2716 }
2717 
2718 static void mpam_enable_once(void)
2719 {
2720 	int err;
2721 
2722 	/*
2723 	 * Once the cpuhp callbacks have been changed, mpam_partid_max can no
2724 	 * longer change.
2725 	 */
2726 	spin_lock(&partid_max_lock);
2727 	partid_max_published = true;
2728 	spin_unlock(&partid_max_lock);
2729 
2730 	/*
2731 	 * If all the MSC have been probed, enabling the IRQs happens next.
2732 	 * That involves cross-calling to a CPU that can reach the MSC, and
2733 	 * the locks must be taken in this order:
2734 	 */
2735 	cpus_read_lock();
2736 	mutex_lock(&mpam_list_lock);
2737 	do {
2738 		mpam_enable_merge_features(&mpam_classes);
2739 
2740 		err = mpam_register_irqs();
2741 		if (err) {
2742 			pr_warn("Failed to register irqs: %d\n", err);
2743 			break;
2744 		}
2745 
2746 		err = mpam_allocate_config();
2747 		if (err) {
2748 			pr_err("Failed to allocate configuration arrays.\n");
2749 			break;
2750 		}
2751 	} while (0);
2752 	mutex_unlock(&mpam_list_lock);
2753 	cpus_read_unlock();
2754 
2755 	if (!err) {
2756 		err = mpam_resctrl_setup();
2757 		if (err)
2758 			pr_err("Failed to initialise resctrl: %d\n", err);
2759 	}
2760 
2761 	if (err) {
2762 		mpam_disable_reason = "Failed to enable.";
2763 		schedule_work(&mpam_broken_work);
2764 		return;
2765 	}
2766 
2767 	static_branch_enable(&mpam_enabled);
2768 	mpam_resctrl_enabled = true;
2769 	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline,
2770 				      "mpam:online");
2771 
2772 	/* Use printk() to avoid the pr_fmt adding the function name. */
2773 	printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n",
2774 	       mpam_partid_max + 1, mpam_pmg_max + 1);
2775 }
2776 
2777 static void mpam_reset_component_locked(struct mpam_component *comp)
2778 {
2779 	struct mpam_vmsc *vmsc;
2780 
2781 	lockdep_assert_cpus_held();
2782 	mpam_assert_partid_sizes_fixed();
2783 
2784 	mpam_reset_component_cfg(comp);
2785 
2786 	guard(srcu)(&mpam_srcu);
2787 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
2788 				 srcu_read_lock_held(&mpam_srcu)) {
2789 		struct mpam_msc *msc = vmsc->msc;
2790 		struct mpam_msc_ris *ris;
2791 
2792 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
2793 					 srcu_read_lock_held(&mpam_srcu)) {
2794 			if (!ris->in_reset_state)
2795 				mpam_touch_msc(msc, mpam_reset_ris, ris);
2796 			ris->in_reset_state = true;
2797 		}
2798 	}
2799 }
2800 
2801 void mpam_reset_class_locked(struct mpam_class *class)
2802 {
2803 	struct mpam_component *comp;
2804 
2805 	lockdep_assert_cpus_held();
2806 
2807 	guard(srcu)(&mpam_srcu);
2808 	list_for_each_entry_srcu(comp, &class->components, class_list,
2809 				 srcu_read_lock_held(&mpam_srcu))
2810 		mpam_reset_component_locked(comp);
2811 }
2812 
2813 static void mpam_reset_class(struct mpam_class *class)
2814 {
2815 	cpus_read_lock();
2816 	mpam_reset_class_locked(class);
2817 	cpus_read_unlock();
2818 }
2819 
2820 /*
2821  * Called in response to an error IRQ.
2822  * All of MPAMs errors indicate a software bug, restore any modified
2823  * controls to their reset values.
2824  */
2825 void mpam_disable(struct work_struct *ignored)
2826 {
2827 	int idx;
2828 	bool do_resctrl_exit;
2829 	struct mpam_class *class;
2830 	struct mpam_msc *msc, *tmp;
2831 
2832 	if (mpam_is_enabled())
2833 		static_branch_disable(&mpam_enabled);
2834 
2835 	mutex_lock(&mpam_cpuhp_state_lock);
2836 	if (mpam_cpuhp_state) {
2837 		cpuhp_remove_state(mpam_cpuhp_state);
2838 		mpam_cpuhp_state = 0;
2839 	}
2840 
2841 	/*
2842 	 * Removing the cpuhp state called mpam_cpu_offline() and told resctrl
2843 	 * all the CPUs are offline.
2844 	 */
2845 	do_resctrl_exit = mpam_resctrl_enabled;
2846 	mpam_resctrl_enabled = false;
2847 	mutex_unlock(&mpam_cpuhp_state_lock);
2848 
2849 	if (do_resctrl_exit)
2850 		mpam_resctrl_exit();
2851 
2852 	mpam_unregister_irqs();
2853 
2854 	idx = srcu_read_lock(&mpam_srcu);
2855 	list_for_each_entry_srcu(class, &mpam_classes, classes_list,
2856 				 srcu_read_lock_held(&mpam_srcu)) {
2857 		mpam_reset_class(class);
2858 		if (do_resctrl_exit)
2859 			mpam_resctrl_teardown_class(class);
2860 	}
2861 	srcu_read_unlock(&mpam_srcu, idx);
2862 
2863 	mutex_lock(&mpam_list_lock);
2864 	list_for_each_entry_safe(msc, tmp, &mpam_all_msc, all_msc_list)
2865 		mpam_msc_destroy(msc);
2866 	mutex_unlock(&mpam_list_lock);
2867 	mpam_free_garbage();
2868 
2869 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
2870 }
2871 
2872 /*
2873  * Enable mpam once all devices have been probed.
2874  * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
2875  * Also scheduled when new devices are probed when new CPUs come online.
2876  */
2877 void mpam_enable(struct work_struct *work)
2878 {
2879 	static atomic_t once;
2880 	struct mpam_msc *msc;
2881 	bool all_devices_probed = true;
2882 
2883 	/* Have we probed all the hw devices? */
2884 	guard(srcu)(&mpam_srcu);
2885 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
2886 				 srcu_read_lock_held(&mpam_srcu)) {
2887 		mutex_lock(&msc->probe_lock);
2888 		if (!msc->probed)
2889 			all_devices_probed = false;
2890 		mutex_unlock(&msc->probe_lock);
2891 
2892 		if (!all_devices_probed)
2893 			break;
2894 	}
2895 
2896 	if (all_devices_probed && !atomic_fetch_inc(&once))
2897 		mpam_enable_once();
2898 }
2899 
2900 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
2901 	if (mpam_has_feature(feature, newcfg) &&			\
2902 	    (newcfg)->member != (cfg)->member) {			\
2903 		(cfg)->member = (newcfg)->member;			\
2904 		mpam_set_feature(feature, cfg);				\
2905 									\
2906 		(changes) = true;					\
2907 	}								\
2908 } while (0)
2909 
2910 static bool mpam_update_config(struct mpam_config *cfg,
2911 			       const struct mpam_config *newcfg)
2912 {
2913 	bool has_changes = false;
2914 
2915 	maybe_update_config(cfg, mpam_feat_cpor_part, newcfg, cpbm, has_changes);
2916 	maybe_update_config(cfg, mpam_feat_mbw_part, newcfg, mbw_pbm, has_changes);
2917 	maybe_update_config(cfg, mpam_feat_mbw_max, newcfg, mbw_max, has_changes);
2918 
2919 	return has_changes;
2920 }
2921 
2922 int mpam_apply_config(struct mpam_component *comp, u16 partid,
2923 		      struct mpam_config *cfg)
2924 {
2925 	struct mpam_write_config_arg arg;
2926 	struct mpam_msc_ris *ris;
2927 	struct mpam_vmsc *vmsc;
2928 	struct mpam_msc *msc;
2929 
2930 	lockdep_assert_cpus_held();
2931 
2932 	/* Don't pass in the current config! */
2933 	WARN_ON_ONCE(&comp->cfg[partid] == cfg);
2934 
2935 	if (!mpam_update_config(&comp->cfg[partid], cfg))
2936 		return 0;
2937 
2938 	arg.comp = comp;
2939 	arg.partid = partid;
2940 
2941 	guard(srcu)(&mpam_srcu);
2942 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
2943 				 srcu_read_lock_held(&mpam_srcu)) {
2944 		msc = vmsc->msc;
2945 
2946 		mutex_lock(&msc->cfg_lock);
2947 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
2948 					 srcu_read_lock_held(&mpam_srcu)) {
2949 			arg.ris = ris;
2950 			mpam_touch_msc(msc, __write_config, &arg);
2951 			ris->in_reset_state = false;
2952 		}
2953 		mutex_unlock(&msc->cfg_lock);
2954 	}
2955 
2956 	return 0;
2957 }
2958 
2959 static int __init mpam_msc_driver_init(void)
2960 {
2961 	if (!system_supports_mpam())
2962 		return -EOPNOTSUPP;
2963 
2964 	init_srcu_struct(&mpam_srcu);
2965 
2966 	fw_num_msc = acpi_mpam_count_msc();
2967 	if (fw_num_msc <= 0) {
2968 		pr_err("No MSC devices found in firmware\n");
2969 		return -EINVAL;
2970 	}
2971 
2972 	return platform_driver_register(&mpam_msc_driver);
2973 }
2974 
2975 /* Must occur after arm64_mpam_register_cpus() from arch_initcall() */
2976 subsys_initcall(mpam_msc_driver_init);
2977 
2978 #ifdef CONFIG_MPAM_KUNIT_TEST
2979 #include "test_mpam_devices.c"
2980 #endif
2981