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