xref: /linux/drivers/resctrl/mpam_devices.c (revision 09b89d2a72f37b078198cbb09d5b9e13ba9d68b9)
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 DEFINE_STATIC_KEY_FALSE(mpam_enabled); /* This moves to arch code */
33 
34 /*
35  * mpam_list_lock protects the SRCU lists when writing. Once the
36  * mpam_enabled key is enabled these lists are read-only,
37  * unless the error interrupt disables the driver.
38  */
39 static DEFINE_MUTEX(mpam_list_lock);
40 static LIST_HEAD(mpam_all_msc);
41 
42 struct srcu_struct mpam_srcu;
43 
44 /*
45  * Number of MSCs that have been probed. Once all MSCs have been probed MPAM
46  * can be enabled.
47  */
48 static atomic_t mpam_num_msc;
49 
50 static int mpam_cpuhp_state;
51 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
52 
53 /*
54  * The smallest common values for any CPU or MSC in the system.
55  * Generating traffic outside this range will result in screaming interrupts.
56  */
57 u16 mpam_partid_max;
58 u8 mpam_pmg_max;
59 static bool partid_max_init, partid_max_published;
60 static DEFINE_SPINLOCK(partid_max_lock);
61 
62 /*
63  * mpam is enabled once all devices have been probed from CPU online callbacks,
64  * scheduled via this work_struct. If access to an MSC depends on a CPU that
65  * was not brought online at boot, this can happen surprisingly late.
66  */
67 static DECLARE_WORK(mpam_enable_work, &mpam_enable);
68 
69 /*
70  * All mpam error interrupts indicate a software bug. On receipt, disable the
71  * driver.
72  */
73 static DECLARE_WORK(mpam_broken_work, &mpam_disable);
74 
75 /* When mpam is disabled, the printed reason to aid debugging */
76 static char *mpam_disable_reason;
77 
78 /*
79  * An MSC is a physical container for controls and monitors, each identified by
80  * their RIS index. These share a base-address, interrupts and some MMIO
81  * registers. A vMSC is a virtual container for RIS in an MSC that control or
82  * monitor the same thing. Members of a vMSC are all RIS in the same MSC, but
83  * not all RIS in an MSC share a vMSC.
84  *
85  * Components are a group of vMSC that control or monitor the same thing but
86  * are from different MSC, so have different base-address, interrupts etc.
87  * Classes are the set components of the same type.
88  *
89  * The features of a vMSC is the union of the RIS it contains.
90  * The features of a Class and Component are the common subset of the vMSC
91  * they contain.
92  *
93  * e.g. The system cache may have bandwidth controls on multiple interfaces,
94  * for regulating traffic from devices independently of traffic from CPUs.
95  * If these are two RIS in one MSC, they will be treated as controlling
96  * different things, and will not share a vMSC/component/class.
97  *
98  * e.g. The L2 may have one MSC and two RIS, one for cache-controls another
99  * for bandwidth. These two RIS are members of the same vMSC.
100  *
101  * e.g. The set of RIS that make up the L2 are grouped as a component. These
102  * are sometimes termed slices. They should be configured the same, as if there
103  * were only one.
104  *
105  * e.g. The SoC probably has more than one L2, each attached to a distinct set
106  * of CPUs. All the L2 components are grouped as a class.
107  *
108  * When creating an MSC, struct mpam_msc is added to the all mpam_all_msc list,
109  * then linked via struct mpam_ris to a vmsc, component and class.
110  * The same MSC may exist under different class->component->vmsc paths, but the
111  * RIS index will be unique.
112  */
113 LIST_HEAD(mpam_classes);
114 
115 /* List of all objects that can be free()d after synchronise_srcu() */
116 static LLIST_HEAD(mpam_garbage);
117 
118 static inline void init_garbage(struct mpam_garbage *garbage)
119 {
120 	init_llist_node(&garbage->llist);
121 }
122 
123 #define add_to_garbage(x)				\
124 do {							\
125 	__typeof__(x) _x = (x);				\
126 	_x->garbage.to_free = _x;			\
127 	llist_add(&_x->garbage.llist, &mpam_garbage);	\
128 } while (0)
129 
130 static void mpam_free_garbage(void)
131 {
132 	struct mpam_garbage *iter, *tmp;
133 	struct llist_node *to_free = llist_del_all(&mpam_garbage);
134 
135 	if (!to_free)
136 		return;
137 
138 	synchronize_srcu(&mpam_srcu);
139 
140 	llist_for_each_entry_safe(iter, tmp, to_free, llist) {
141 		if (iter->pdev)
142 			devm_kfree(&iter->pdev->dev, iter->to_free);
143 		else
144 			kfree(iter->to_free);
145 	}
146 }
147 
148 /*
149  * Once mpam is enabled, new requestors cannot further reduce the available
150  * partid. Assert that the size is fixed, and new requestors will be turned
151  * away.
152  */
153 static void mpam_assert_partid_sizes_fixed(void)
154 {
155 	WARN_ON_ONCE(!partid_max_published);
156 }
157 
158 static u32 __mpam_read_reg(struct mpam_msc *msc, u16 reg)
159 {
160 	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
161 
162 	return readl_relaxed(msc->mapped_hwpage + reg);
163 }
164 
165 static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg)
166 {
167 	lockdep_assert_held_once(&msc->part_sel_lock);
168 	return __mpam_read_reg(msc, reg);
169 }
170 
171 #define mpam_read_partsel_reg(msc, reg) _mpam_read_partsel_reg(msc, MPAMF_##reg)
172 
173 static void __mpam_write_reg(struct mpam_msc *msc, u16 reg, u32 val)
174 {
175 	WARN_ON_ONCE(reg + sizeof(u32) > msc->mapped_hwpage_sz);
176 	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
177 
178 	writel_relaxed(val, msc->mapped_hwpage + reg);
179 }
180 
181 static inline void _mpam_write_partsel_reg(struct mpam_msc *msc, u16 reg, u32 val)
182 {
183 	lockdep_assert_held_once(&msc->part_sel_lock);
184 	__mpam_write_reg(msc, reg, val);
185 }
186 
187 #define mpam_write_partsel_reg(msc, reg, val)  _mpam_write_partsel_reg(msc, MPAMCFG_##reg, val)
188 
189 static inline u32 _mpam_read_monsel_reg(struct mpam_msc *msc, u16 reg)
190 {
191 	mpam_mon_sel_lock_held(msc);
192 	return __mpam_read_reg(msc, reg);
193 }
194 
195 #define mpam_read_monsel_reg(msc, reg) _mpam_read_monsel_reg(msc, MSMON_##reg)
196 
197 static inline void _mpam_write_monsel_reg(struct mpam_msc *msc, u16 reg, u32 val)
198 {
199 	mpam_mon_sel_lock_held(msc);
200 	__mpam_write_reg(msc, reg, val);
201 }
202 
203 #define mpam_write_monsel_reg(msc, reg, val)   _mpam_write_monsel_reg(msc, MSMON_##reg, val)
204 
205 static u64 mpam_msc_read_idr(struct mpam_msc *msc)
206 {
207 	u64 idr_high = 0, idr_low;
208 
209 	lockdep_assert_held(&msc->part_sel_lock);
210 
211 	idr_low = mpam_read_partsel_reg(msc, IDR);
212 	if (FIELD_GET(MPAMF_IDR_EXT, idr_low))
213 		idr_high = mpam_read_partsel_reg(msc, IDR + 4);
214 
215 	return (idr_high << 32) | idr_low;
216 }
217 
218 static void mpam_msc_clear_esr(struct mpam_msc *msc)
219 {
220 	u64 esr_low = __mpam_read_reg(msc, MPAMF_ESR);
221 
222 	if (!esr_low)
223 		return;
224 
225 	/*
226 	 * Clearing the high/low bits of MPAMF_ESR can not be atomic.
227 	 * Clear the top half first, so that the pending error bits in the
228 	 * lower half prevent hardware from updating either half of the
229 	 * register.
230 	 */
231 	if (msc->has_extd_esr)
232 		__mpam_write_reg(msc, MPAMF_ESR + 4, 0);
233 	__mpam_write_reg(msc, MPAMF_ESR, 0);
234 }
235 
236 static u64 mpam_msc_read_esr(struct mpam_msc *msc)
237 {
238 	u64 esr_high = 0, esr_low;
239 
240 	esr_low = __mpam_read_reg(msc, MPAMF_ESR);
241 	if (msc->has_extd_esr)
242 		esr_high = __mpam_read_reg(msc, MPAMF_ESR + 4);
243 
244 	return (esr_high << 32) | esr_low;
245 }
246 
247 static void __mpam_part_sel_raw(u32 partsel, struct mpam_msc *msc)
248 {
249 	lockdep_assert_held(&msc->part_sel_lock);
250 
251 	mpam_write_partsel_reg(msc, PART_SEL, partsel);
252 }
253 
254 static void __mpam_part_sel(u8 ris_idx, u16 partid, struct mpam_msc *msc)
255 {
256 	u32 partsel = FIELD_PREP(MPAMCFG_PART_SEL_RIS, ris_idx) |
257 		      FIELD_PREP(MPAMCFG_PART_SEL_PARTID_SEL, partid);
258 
259 	__mpam_part_sel_raw(partsel, msc);
260 }
261 
262 int mpam_register_requestor(u16 partid_max, u8 pmg_max)
263 {
264 	guard(spinlock)(&partid_max_lock);
265 	if (!partid_max_init) {
266 		mpam_partid_max = partid_max;
267 		mpam_pmg_max = pmg_max;
268 		partid_max_init = true;
269 	} else if (!partid_max_published) {
270 		mpam_partid_max = min(mpam_partid_max, partid_max);
271 		mpam_pmg_max = min(mpam_pmg_max, pmg_max);
272 	} else {
273 		/* New requestors can't lower the values */
274 		if (partid_max < mpam_partid_max || pmg_max < mpam_pmg_max)
275 			return -EBUSY;
276 	}
277 
278 	return 0;
279 }
280 EXPORT_SYMBOL(mpam_register_requestor);
281 
282 static struct mpam_class *
283 mpam_class_alloc(u8 level_idx, enum mpam_class_types type)
284 {
285 	struct mpam_class *class;
286 
287 	lockdep_assert_held(&mpam_list_lock);
288 
289 	class = kzalloc(sizeof(*class), GFP_KERNEL);
290 	if (!class)
291 		return ERR_PTR(-ENOMEM);
292 	init_garbage(&class->garbage);
293 
294 	INIT_LIST_HEAD_RCU(&class->components);
295 	/* Affinity is updated when ris are added */
296 	class->level = level_idx;
297 	class->type = type;
298 	INIT_LIST_HEAD_RCU(&class->classes_list);
299 
300 	list_add_rcu(&class->classes_list, &mpam_classes);
301 
302 	return class;
303 }
304 
305 static void mpam_class_destroy(struct mpam_class *class)
306 {
307 	lockdep_assert_held(&mpam_list_lock);
308 
309 	list_del_rcu(&class->classes_list);
310 	add_to_garbage(class);
311 }
312 
313 static struct mpam_class *
314 mpam_class_find(u8 level_idx, enum mpam_class_types type)
315 {
316 	struct mpam_class *class;
317 
318 	lockdep_assert_held(&mpam_list_lock);
319 
320 	list_for_each_entry(class, &mpam_classes, classes_list) {
321 		if (class->type == type && class->level == level_idx)
322 			return class;
323 	}
324 
325 	return mpam_class_alloc(level_idx, type);
326 }
327 
328 static struct mpam_component *
329 mpam_component_alloc(struct mpam_class *class, int id)
330 {
331 	struct mpam_component *comp;
332 
333 	lockdep_assert_held(&mpam_list_lock);
334 
335 	comp = kzalloc(sizeof(*comp), GFP_KERNEL);
336 	if (!comp)
337 		return ERR_PTR(-ENOMEM);
338 	init_garbage(&comp->garbage);
339 
340 	comp->comp_id = id;
341 	INIT_LIST_HEAD_RCU(&comp->vmsc);
342 	/* Affinity is updated when RIS are added */
343 	INIT_LIST_HEAD_RCU(&comp->class_list);
344 	comp->class = class;
345 
346 	list_add_rcu(&comp->class_list, &class->components);
347 
348 	return comp;
349 }
350 
351 static void __destroy_component_cfg(struct mpam_component *comp);
352 
353 static void mpam_component_destroy(struct mpam_component *comp)
354 {
355 	struct mpam_class *class = comp->class;
356 
357 	lockdep_assert_held(&mpam_list_lock);
358 
359 	__destroy_component_cfg(comp);
360 
361 	list_del_rcu(&comp->class_list);
362 	add_to_garbage(comp);
363 
364 	if (list_empty(&class->components))
365 		mpam_class_destroy(class);
366 }
367 
368 static struct mpam_component *
369 mpam_component_find(struct mpam_class *class, int id)
370 {
371 	struct mpam_component *comp;
372 
373 	lockdep_assert_held(&mpam_list_lock);
374 
375 	list_for_each_entry(comp, &class->components, class_list) {
376 		if (comp->comp_id == id)
377 			return comp;
378 	}
379 
380 	return mpam_component_alloc(class, id);
381 }
382 
383 static struct mpam_vmsc *
384 mpam_vmsc_alloc(struct mpam_component *comp, struct mpam_msc *msc)
385 {
386 	struct mpam_vmsc *vmsc;
387 
388 	lockdep_assert_held(&mpam_list_lock);
389 
390 	vmsc = kzalloc(sizeof(*vmsc), GFP_KERNEL);
391 	if (!vmsc)
392 		return ERR_PTR(-ENOMEM);
393 	init_garbage(&vmsc->garbage);
394 
395 	INIT_LIST_HEAD_RCU(&vmsc->ris);
396 	INIT_LIST_HEAD_RCU(&vmsc->comp_list);
397 	vmsc->comp = comp;
398 	vmsc->msc = msc;
399 
400 	list_add_rcu(&vmsc->comp_list, &comp->vmsc);
401 
402 	return vmsc;
403 }
404 
405 static void mpam_vmsc_destroy(struct mpam_vmsc *vmsc)
406 {
407 	struct mpam_component *comp = vmsc->comp;
408 
409 	lockdep_assert_held(&mpam_list_lock);
410 
411 	list_del_rcu(&vmsc->comp_list);
412 	add_to_garbage(vmsc);
413 
414 	if (list_empty(&comp->vmsc))
415 		mpam_component_destroy(comp);
416 }
417 
418 static struct mpam_vmsc *
419 mpam_vmsc_find(struct mpam_component *comp, struct mpam_msc *msc)
420 {
421 	struct mpam_vmsc *vmsc;
422 
423 	lockdep_assert_held(&mpam_list_lock);
424 
425 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
426 		if (vmsc->msc->id == msc->id)
427 			return vmsc;
428 	}
429 
430 	return mpam_vmsc_alloc(comp, msc);
431 }
432 
433 /*
434  * The cacheinfo structures are only populated when CPUs are online.
435  * This helper walks the acpi tables to include offline CPUs too.
436  */
437 int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
438 				   cpumask_t *affinity)
439 {
440 	return acpi_pptt_get_cpumask_from_cache_id(cache_id, affinity);
441 }
442 
443 /*
444  * cpumask_of_node() only knows about online CPUs. This can't tell us whether
445  * a class is represented on all possible CPUs.
446  */
447 static void get_cpumask_from_node_id(u32 node_id, cpumask_t *affinity)
448 {
449 	int cpu;
450 
451 	for_each_possible_cpu(cpu) {
452 		if (node_id == cpu_to_node(cpu))
453 			cpumask_set_cpu(cpu, affinity);
454 	}
455 }
456 
457 static int mpam_ris_get_affinity(struct mpam_msc *msc, cpumask_t *affinity,
458 				 enum mpam_class_types type,
459 				 struct mpam_class *class,
460 				 struct mpam_component *comp)
461 {
462 	int err;
463 
464 	switch (type) {
465 	case MPAM_CLASS_CACHE:
466 		err = mpam_get_cpumask_from_cache_id(comp->comp_id, class->level,
467 						     affinity);
468 		if (err) {
469 			dev_warn_once(&msc->pdev->dev,
470 				      "Failed to determine CPU affinity\n");
471 			return err;
472 		}
473 
474 		if (cpumask_empty(affinity))
475 			dev_warn_once(&msc->pdev->dev, "no CPUs associated with cache node\n");
476 
477 		break;
478 	case MPAM_CLASS_MEMORY:
479 		get_cpumask_from_node_id(comp->comp_id, affinity);
480 		/* affinity may be empty for CPU-less memory nodes */
481 		break;
482 	case MPAM_CLASS_UNKNOWN:
483 		return 0;
484 	}
485 
486 	cpumask_and(affinity, affinity, &msc->accessibility);
487 
488 	return 0;
489 }
490 
491 static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
492 				  enum mpam_class_types type, u8 class_id,
493 				  int component_id)
494 {
495 	int err;
496 	struct mpam_vmsc *vmsc;
497 	struct mpam_msc_ris *ris;
498 	struct mpam_class *class;
499 	struct mpam_component *comp;
500 	struct platform_device *pdev = msc->pdev;
501 
502 	lockdep_assert_held(&mpam_list_lock);
503 
504 	if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
505 		return -EINVAL;
506 
507 	if (test_and_set_bit(ris_idx, &msc->ris_idxs))
508 		return -EBUSY;
509 
510 	ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL);
511 	if (!ris)
512 		return -ENOMEM;
513 	init_garbage(&ris->garbage);
514 	ris->garbage.pdev = pdev;
515 
516 	class = mpam_class_find(class_id, type);
517 	if (IS_ERR(class))
518 		return PTR_ERR(class);
519 
520 	comp = mpam_component_find(class, component_id);
521 	if (IS_ERR(comp)) {
522 		if (list_empty(&class->components))
523 			mpam_class_destroy(class);
524 		return PTR_ERR(comp);
525 	}
526 
527 	vmsc = mpam_vmsc_find(comp, msc);
528 	if (IS_ERR(vmsc)) {
529 		if (list_empty(&comp->vmsc))
530 			mpam_component_destroy(comp);
531 		return PTR_ERR(vmsc);
532 	}
533 
534 	err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
535 	if (err) {
536 		if (list_empty(&vmsc->ris))
537 			mpam_vmsc_destroy(vmsc);
538 		return err;
539 	}
540 
541 	ris->ris_idx = ris_idx;
542 	INIT_LIST_HEAD_RCU(&ris->msc_list);
543 	INIT_LIST_HEAD_RCU(&ris->vmsc_list);
544 	ris->vmsc = vmsc;
545 
546 	cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
547 	cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
548 	list_add_rcu(&ris->vmsc_list, &vmsc->ris);
549 	list_add_rcu(&ris->msc_list, &msc->ris);
550 
551 	return 0;
552 }
553 
554 static void mpam_ris_destroy(struct mpam_msc_ris *ris)
555 {
556 	struct mpam_vmsc *vmsc = ris->vmsc;
557 	struct mpam_msc *msc = vmsc->msc;
558 	struct mpam_component *comp = vmsc->comp;
559 	struct mpam_class *class = comp->class;
560 
561 	lockdep_assert_held(&mpam_list_lock);
562 
563 	/*
564 	 * It is assumed affinities don't overlap. If they do the class becomes
565 	 * unusable immediately.
566 	 */
567 	cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity);
568 	cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity);
569 	clear_bit(ris->ris_idx, &msc->ris_idxs);
570 	list_del_rcu(&ris->msc_list);
571 	list_del_rcu(&ris->vmsc_list);
572 	add_to_garbage(ris);
573 
574 	if (list_empty(&vmsc->ris))
575 		mpam_vmsc_destroy(vmsc);
576 }
577 
578 int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
579 		    enum mpam_class_types type, u8 class_id, int component_id)
580 {
581 	int err;
582 
583 	mutex_lock(&mpam_list_lock);
584 	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
585 				     component_id);
586 	mutex_unlock(&mpam_list_lock);
587 	if (err)
588 		mpam_free_garbage();
589 
590 	return err;
591 }
592 
593 static struct mpam_msc_ris *mpam_get_or_create_ris(struct mpam_msc *msc,
594 						   u8 ris_idx)
595 {
596 	int err;
597 	struct mpam_msc_ris *ris;
598 
599 	lockdep_assert_held(&mpam_list_lock);
600 
601 	if (!test_bit(ris_idx, &msc->ris_idxs)) {
602 		err = mpam_ris_create_locked(msc, ris_idx, MPAM_CLASS_UNKNOWN,
603 					     0, 0);
604 		if (err)
605 			return ERR_PTR(err);
606 	}
607 
608 	list_for_each_entry(ris, &msc->ris, msc_list) {
609 		if (ris->ris_idx == ris_idx)
610 			return ris;
611 	}
612 
613 	return ERR_PTR(-ENOENT);
614 }
615 
616 /*
617  * IHI009A.a has this nugget: "If a monitor does not support automatic behaviour
618  * of NRDY, software can use this bit for any purpose" - so hardware might not
619  * implement this - but it isn't RES0.
620  *
621  * Try and see what values stick in this bit. If we can write either value,
622  * its probably not implemented by hardware.
623  */
624 static bool _mpam_ris_hw_probe_hw_nrdy(struct mpam_msc_ris *ris, u32 mon_reg)
625 {
626 	u32 now;
627 	u64 mon_sel;
628 	bool can_set, can_clear;
629 	struct mpam_msc *msc = ris->vmsc->msc;
630 
631 	if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc)))
632 		return false;
633 
634 	mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, 0) |
635 		  FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx);
636 	_mpam_write_monsel_reg(msc, mon_reg, mon_sel);
637 
638 	_mpam_write_monsel_reg(msc, mon_reg, MSMON___NRDY);
639 	now = _mpam_read_monsel_reg(msc, mon_reg);
640 	can_set = now & MSMON___NRDY;
641 
642 	_mpam_write_monsel_reg(msc, mon_reg, 0);
643 	now = _mpam_read_monsel_reg(msc, mon_reg);
644 	can_clear = !(now & MSMON___NRDY);
645 	mpam_mon_sel_unlock(msc);
646 
647 	return (!can_set || !can_clear);
648 }
649 
650 #define mpam_ris_hw_probe_hw_nrdy(_ris, _mon_reg)			\
651 	_mpam_ris_hw_probe_hw_nrdy(_ris, MSMON_##_mon_reg)
652 
653 static void mpam_ris_hw_probe(struct mpam_msc_ris *ris)
654 {
655 	int err;
656 	struct mpam_msc *msc = ris->vmsc->msc;
657 	struct device *dev = &msc->pdev->dev;
658 	struct mpam_props *props = &ris->props;
659 
660 	lockdep_assert_held(&msc->probe_lock);
661 	lockdep_assert_held(&msc->part_sel_lock);
662 
663 	/* Cache Portion partitioning */
664 	if (FIELD_GET(MPAMF_IDR_HAS_CPOR_PART, ris->idr)) {
665 		u32 cpor_features = mpam_read_partsel_reg(msc, CPOR_IDR);
666 
667 		props->cpbm_wd = FIELD_GET(MPAMF_CPOR_IDR_CPBM_WD, cpor_features);
668 		if (props->cpbm_wd)
669 			mpam_set_feature(mpam_feat_cpor_part, props);
670 	}
671 
672 	/* Memory bandwidth partitioning */
673 	if (FIELD_GET(MPAMF_IDR_HAS_MBW_PART, ris->idr)) {
674 		u32 mbw_features = mpam_read_partsel_reg(msc, MBW_IDR);
675 
676 		/* portion bitmap resolution */
677 		props->mbw_pbm_bits = FIELD_GET(MPAMF_MBW_IDR_BWPBM_WD, mbw_features);
678 		if (props->mbw_pbm_bits &&
679 		    FIELD_GET(MPAMF_MBW_IDR_HAS_PBM, mbw_features))
680 			mpam_set_feature(mpam_feat_mbw_part, props);
681 
682 		props->bwa_wd = FIELD_GET(MPAMF_MBW_IDR_BWA_WD, mbw_features);
683 		if (props->bwa_wd && FIELD_GET(MPAMF_MBW_IDR_HAS_MAX, mbw_features))
684 			mpam_set_feature(mpam_feat_mbw_max, props);
685 	}
686 
687 	/* Performance Monitoring */
688 	if (FIELD_GET(MPAMF_IDR_HAS_MSMON, ris->idr)) {
689 		u32 msmon_features = mpam_read_partsel_reg(msc, MSMON_IDR);
690 
691 		/*
692 		 * If the firmware max-nrdy-us property is missing, the
693 		 * CSU counters can't be used. Should we wait forever?
694 		 */
695 		err = device_property_read_u32(&msc->pdev->dev,
696 					       "arm,not-ready-us",
697 					       &msc->nrdy_usec);
698 
699 		if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_CSU, msmon_features)) {
700 			u32 csumonidr;
701 
702 			csumonidr = mpam_read_partsel_reg(msc, CSUMON_IDR);
703 			props->num_csu_mon = FIELD_GET(MPAMF_CSUMON_IDR_NUM_MON, csumonidr);
704 			if (props->num_csu_mon) {
705 				bool hw_managed;
706 
707 				mpam_set_feature(mpam_feat_msmon_csu, props);
708 
709 				/* Is NRDY hardware managed? */
710 				hw_managed = mpam_ris_hw_probe_hw_nrdy(ris, CSU);
711 				if (hw_managed)
712 					mpam_set_feature(mpam_feat_msmon_csu_hw_nrdy, props);
713 			}
714 
715 			/*
716 			 * Accept the missing firmware property if NRDY appears
717 			 * un-implemented.
718 			 */
719 			if (err && mpam_has_feature(mpam_feat_msmon_csu_hw_nrdy, props))
720 				dev_err_once(dev, "Counters are not usable because not-ready timeout was not provided by firmware.");
721 		}
722 		if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_MBWU, msmon_features)) {
723 			bool hw_managed;
724 			u32 mbwumon_idr = mpam_read_partsel_reg(msc, MBWUMON_IDR);
725 
726 			props->num_mbwu_mon = FIELD_GET(MPAMF_MBWUMON_IDR_NUM_MON, mbwumon_idr);
727 			if (props->num_mbwu_mon)
728 				mpam_set_feature(mpam_feat_msmon_mbwu, props);
729 
730 			/* Is NRDY hardware managed? */
731 			hw_managed = mpam_ris_hw_probe_hw_nrdy(ris, MBWU);
732 			if (hw_managed)
733 				mpam_set_feature(mpam_feat_msmon_mbwu_hw_nrdy, props);
734 
735 			/*
736 			 * Don't warn about any missing firmware property for
737 			 * MBWU NRDY - it doesn't make any sense!
738 			 */
739 		}
740 	}
741 }
742 
743 static int mpam_msc_hw_probe(struct mpam_msc *msc)
744 {
745 	u64 idr;
746 	u16 partid_max;
747 	u8 ris_idx, pmg_max;
748 	struct mpam_msc_ris *ris;
749 	struct device *dev = &msc->pdev->dev;
750 
751 	lockdep_assert_held(&msc->probe_lock);
752 
753 	idr = __mpam_read_reg(msc, MPAMF_AIDR);
754 	if ((idr & MPAMF_AIDR_ARCH_MAJOR_REV) != MPAM_ARCHITECTURE_V1) {
755 		dev_err_once(dev, "MSC does not match MPAM architecture v1.x\n");
756 		return -EIO;
757 	}
758 
759 	/* Grab an IDR value to find out how many RIS there are */
760 	mutex_lock(&msc->part_sel_lock);
761 	idr = mpam_msc_read_idr(msc);
762 	mutex_unlock(&msc->part_sel_lock);
763 
764 	msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr);
765 
766 	/* Use these values so partid/pmg always starts with a valid value */
767 	msc->partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
768 	msc->pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
769 
770 	for (ris_idx = 0; ris_idx <= msc->ris_max; ris_idx++) {
771 		mutex_lock(&msc->part_sel_lock);
772 		__mpam_part_sel(ris_idx, 0, msc);
773 		idr = mpam_msc_read_idr(msc);
774 		mutex_unlock(&msc->part_sel_lock);
775 
776 		partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
777 		pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
778 		msc->partid_max = min(msc->partid_max, partid_max);
779 		msc->pmg_max = min(msc->pmg_max, pmg_max);
780 		msc->has_extd_esr = FIELD_GET(MPAMF_IDR_HAS_EXTD_ESR, idr);
781 
782 		mutex_lock(&mpam_list_lock);
783 		ris = mpam_get_or_create_ris(msc, ris_idx);
784 		mutex_unlock(&mpam_list_lock);
785 		if (IS_ERR(ris))
786 			return PTR_ERR(ris);
787 		ris->idr = idr;
788 
789 		mutex_lock(&msc->part_sel_lock);
790 		__mpam_part_sel(ris_idx, 0, msc);
791 		mpam_ris_hw_probe(ris);
792 		mutex_unlock(&msc->part_sel_lock);
793 	}
794 
795 	/* Clear any stale errors */
796 	mpam_msc_clear_esr(msc);
797 
798 	spin_lock(&partid_max_lock);
799 	mpam_partid_max = min(mpam_partid_max, msc->partid_max);
800 	mpam_pmg_max = min(mpam_pmg_max, msc->pmg_max);
801 	spin_unlock(&partid_max_lock);
802 
803 	msc->probed = true;
804 
805 	return 0;
806 }
807 
808 static void mpam_reset_msc_bitmap(struct mpam_msc *msc, u16 reg, u16 wd)
809 {
810 	u32 num_words, msb;
811 	u32 bm = ~0;
812 	int i;
813 
814 	lockdep_assert_held(&msc->part_sel_lock);
815 
816 	if (wd == 0)
817 		return;
818 
819 	/*
820 	 * Write all ~0 to all but the last 32bit-word, which may
821 	 * have fewer bits...
822 	 */
823 	num_words = DIV_ROUND_UP(wd, 32);
824 	for (i = 0; i < num_words - 1; i++, reg += sizeof(bm))
825 		__mpam_write_reg(msc, reg, bm);
826 
827 	/*
828 	 * ....and then the last (maybe) partial 32bit word. When wd is a
829 	 * multiple of 32, msb should be 31 to write a full 32bit word.
830 	 */
831 	msb = (wd - 1) % 32;
832 	bm = GENMASK(msb, 0);
833 	__mpam_write_reg(msc, reg, bm);
834 }
835 
836 /* Called via IPI. Call while holding an SRCU reference */
837 static void mpam_reprogram_ris_partid(struct mpam_msc_ris *ris, u16 partid,
838 				      struct mpam_config *cfg)
839 {
840 	struct mpam_msc *msc = ris->vmsc->msc;
841 	struct mpam_props *rprops = &ris->props;
842 
843 	mutex_lock(&msc->part_sel_lock);
844 	__mpam_part_sel(ris->ris_idx, partid, msc);
845 
846 	if (mpam_has_feature(mpam_feat_cpor_part, rprops) &&
847 	    mpam_has_feature(mpam_feat_cpor_part, cfg)) {
848 		if (cfg->reset_cpbm)
849 			mpam_reset_msc_bitmap(msc, MPAMCFG_CPBM, rprops->cpbm_wd);
850 		else
851 			mpam_write_partsel_reg(msc, CPBM, cfg->cpbm);
852 	}
853 
854 	if (mpam_has_feature(mpam_feat_mbw_part, rprops) &&
855 	    mpam_has_feature(mpam_feat_mbw_part, cfg)) {
856 		if (cfg->reset_mbw_pbm)
857 			mpam_reset_msc_bitmap(msc, MPAMCFG_MBW_PBM, rprops->mbw_pbm_bits);
858 		else
859 			mpam_write_partsel_reg(msc, MBW_PBM, cfg->mbw_pbm);
860 	}
861 
862 	if (mpam_has_feature(mpam_feat_mbw_min, rprops) &&
863 	    mpam_has_feature(mpam_feat_mbw_min, cfg))
864 		mpam_write_partsel_reg(msc, MBW_MIN, 0);
865 
866 	if (mpam_has_feature(mpam_feat_mbw_max, rprops) &&
867 	    mpam_has_feature(mpam_feat_mbw_max, cfg)) {
868 		if (cfg->reset_mbw_max)
869 			mpam_write_partsel_reg(msc, MBW_MAX, MPAMCFG_MBW_MAX_MAX);
870 		else
871 			mpam_write_partsel_reg(msc, MBW_MAX, cfg->mbw_max);
872 	}
873 
874 	mutex_unlock(&msc->part_sel_lock);
875 }
876 
877 static void mpam_init_reset_cfg(struct mpam_config *reset_cfg)
878 {
879 	*reset_cfg = (struct mpam_config) {
880 		.reset_cpbm = true,
881 		.reset_mbw_pbm = true,
882 		.reset_mbw_max = true,
883 	};
884 	bitmap_fill(reset_cfg->features, MPAM_FEATURE_LAST);
885 }
886 
887 /*
888  * Called via smp_call_on_cpu() to prevent migration, while still being
889  * pre-emptible. Caller must hold mpam_srcu.
890  */
891 static int mpam_reset_ris(void *arg)
892 {
893 	u16 partid, partid_max;
894 	struct mpam_config reset_cfg;
895 	struct mpam_msc_ris *ris = arg;
896 
897 	if (ris->in_reset_state)
898 		return 0;
899 
900 	mpam_init_reset_cfg(&reset_cfg);
901 
902 	spin_lock(&partid_max_lock);
903 	partid_max = mpam_partid_max;
904 	spin_unlock(&partid_max_lock);
905 	for (partid = 0; partid <= partid_max; partid++)
906 		mpam_reprogram_ris_partid(ris, partid, &reset_cfg);
907 
908 	return 0;
909 }
910 
911 /*
912  * Get the preferred CPU for this MSC. If it is accessible from this CPU,
913  * this CPU is preferred. This can be preempted/migrated, it will only result
914  * in more work.
915  */
916 static int mpam_get_msc_preferred_cpu(struct mpam_msc *msc)
917 {
918 	int cpu = raw_smp_processor_id();
919 
920 	if (cpumask_test_cpu(cpu, &msc->accessibility))
921 		return cpu;
922 
923 	return cpumask_first_and(&msc->accessibility, cpu_online_mask);
924 }
925 
926 static int mpam_touch_msc(struct mpam_msc *msc, int (*fn)(void *a), void *arg)
927 {
928 	lockdep_assert_irqs_enabled();
929 	lockdep_assert_cpus_held();
930 	WARN_ON_ONCE(!srcu_read_lock_held((&mpam_srcu)));
931 
932 	return smp_call_on_cpu(mpam_get_msc_preferred_cpu(msc), fn, arg, true);
933 }
934 
935 struct mpam_write_config_arg {
936 	struct mpam_msc_ris *ris;
937 	struct mpam_component *comp;
938 	u16 partid;
939 };
940 
941 static int __write_config(void *arg)
942 {
943 	struct mpam_write_config_arg *c = arg;
944 
945 	mpam_reprogram_ris_partid(c->ris, c->partid, &c->comp->cfg[c->partid]);
946 
947 	return 0;
948 }
949 
950 static void mpam_reprogram_msc(struct mpam_msc *msc)
951 {
952 	u16 partid;
953 	bool reset;
954 	struct mpam_config *cfg;
955 	struct mpam_msc_ris *ris;
956 	struct mpam_write_config_arg arg;
957 
958 	/*
959 	 * No lock for mpam_partid_max as partid_max_published has been
960 	 * set by mpam_enabled(), so the values can no longer change.
961 	 */
962 	mpam_assert_partid_sizes_fixed();
963 
964 	mutex_lock(&msc->cfg_lock);
965 	list_for_each_entry_srcu(ris, &msc->ris, msc_list,
966 				 srcu_read_lock_held(&mpam_srcu)) {
967 		if (!mpam_is_enabled() && !ris->in_reset_state) {
968 			mpam_touch_msc(msc, &mpam_reset_ris, ris);
969 			ris->in_reset_state = true;
970 			continue;
971 		}
972 
973 		arg.comp = ris->vmsc->comp;
974 		arg.ris = ris;
975 		reset = true;
976 		for (partid = 0; partid <= mpam_partid_max; partid++) {
977 			cfg = &ris->vmsc->comp->cfg[partid];
978 			if (!bitmap_empty(cfg->features, MPAM_FEATURE_LAST))
979 				reset = false;
980 
981 			arg.partid = partid;
982 			mpam_touch_msc(msc, __write_config, &arg);
983 		}
984 		ris->in_reset_state = reset;
985 	}
986 	mutex_unlock(&msc->cfg_lock);
987 }
988 
989 static void _enable_percpu_irq(void *_irq)
990 {
991 	int *irq = _irq;
992 
993 	enable_percpu_irq(*irq, IRQ_TYPE_NONE);
994 }
995 
996 static int mpam_cpu_online(unsigned int cpu)
997 {
998 	struct mpam_msc *msc;
999 
1000 	guard(srcu)(&mpam_srcu);
1001 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1002 				 srcu_read_lock_held(&mpam_srcu)) {
1003 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1004 			continue;
1005 
1006 		if (msc->reenable_error_ppi)
1007 			_enable_percpu_irq(&msc->reenable_error_ppi);
1008 
1009 		if (atomic_fetch_inc(&msc->online_refs) == 0)
1010 			mpam_reprogram_msc(msc);
1011 	}
1012 
1013 	return 0;
1014 }
1015 
1016 /* Before mpam is enabled, try to probe new MSC */
1017 static int mpam_discovery_cpu_online(unsigned int cpu)
1018 {
1019 	int err = 0;
1020 	struct mpam_msc *msc;
1021 	bool new_device_probed = false;
1022 
1023 	if (mpam_is_enabled())
1024 		return 0;
1025 
1026 	guard(srcu)(&mpam_srcu);
1027 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1028 				 srcu_read_lock_held(&mpam_srcu)) {
1029 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1030 			continue;
1031 
1032 		mutex_lock(&msc->probe_lock);
1033 		if (!msc->probed)
1034 			err = mpam_msc_hw_probe(msc);
1035 		mutex_unlock(&msc->probe_lock);
1036 
1037 		if (err)
1038 			break;
1039 		new_device_probed = true;
1040 	}
1041 
1042 	if (new_device_probed && !err)
1043 		schedule_work(&mpam_enable_work);
1044 	if (err) {
1045 		mpam_disable_reason = "error during probing";
1046 		schedule_work(&mpam_broken_work);
1047 	}
1048 
1049 	return err;
1050 }
1051 
1052 static int mpam_cpu_offline(unsigned int cpu)
1053 {
1054 	struct mpam_msc *msc;
1055 
1056 	guard(srcu)(&mpam_srcu);
1057 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1058 				 srcu_read_lock_held(&mpam_srcu)) {
1059 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1060 			continue;
1061 
1062 		if (msc->reenable_error_ppi)
1063 			disable_percpu_irq(msc->reenable_error_ppi);
1064 
1065 		if (atomic_dec_and_test(&msc->online_refs)) {
1066 			struct mpam_msc_ris *ris;
1067 
1068 			mutex_lock(&msc->cfg_lock);
1069 			list_for_each_entry_srcu(ris, &msc->ris, msc_list,
1070 						 srcu_read_lock_held(&mpam_srcu)) {
1071 				mpam_touch_msc(msc, &mpam_reset_ris, ris);
1072 
1073 				/*
1074 				 * The reset state for non-zero partid may be
1075 				 * lost while the CPUs are offline.
1076 				 */
1077 				ris->in_reset_state = false;
1078 			}
1079 			mutex_unlock(&msc->cfg_lock);
1080 		}
1081 	}
1082 
1083 	return 0;
1084 }
1085 
1086 static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online),
1087 					  int (*offline)(unsigned int offline),
1088 					  char *name)
1089 {
1090 	mutex_lock(&mpam_cpuhp_state_lock);
1091 	if (mpam_cpuhp_state) {
1092 		cpuhp_remove_state(mpam_cpuhp_state);
1093 		mpam_cpuhp_state = 0;
1094 	}
1095 
1096 	mpam_cpuhp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, name, online,
1097 					     offline);
1098 	if (mpam_cpuhp_state <= 0) {
1099 		pr_err("Failed to register cpuhp callbacks");
1100 		mpam_cpuhp_state = 0;
1101 	}
1102 	mutex_unlock(&mpam_cpuhp_state_lock);
1103 }
1104 
1105 static int __setup_ppi(struct mpam_msc *msc)
1106 {
1107 	int cpu;
1108 
1109 	msc->error_dev_id = alloc_percpu(struct mpam_msc *);
1110 	if (!msc->error_dev_id)
1111 		return -ENOMEM;
1112 
1113 	for_each_cpu(cpu, &msc->accessibility)
1114 		*per_cpu_ptr(msc->error_dev_id, cpu) = msc;
1115 
1116 	return 0;
1117 }
1118 
1119 static int mpam_msc_setup_error_irq(struct mpam_msc *msc)
1120 {
1121 	int irq;
1122 
1123 	irq = platform_get_irq_byname_optional(msc->pdev, "error");
1124 	if (irq <= 0)
1125 		return 0;
1126 
1127 	/* Allocate and initialise the percpu device pointer for PPI */
1128 	if (irq_is_percpu(irq))
1129 		return __setup_ppi(msc);
1130 
1131 	/* sanity check: shared interrupts can be routed anywhere? */
1132 	if (!cpumask_equal(&msc->accessibility, cpu_possible_mask)) {
1133 		pr_err_once("msc:%u is a private resource with a shared error interrupt",
1134 			    msc->id);
1135 		return -EINVAL;
1136 	}
1137 
1138 	return 0;
1139 }
1140 
1141 /*
1142  * An MSC can control traffic from a set of CPUs, but may only be accessible
1143  * from a (hopefully wider) set of CPUs. The common reason for this is power
1144  * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
1145  * corresponding cache may also be powered off. By making accesses from
1146  * one of those CPUs, we ensure we don't access a cache that's powered off.
1147  */
1148 static void update_msc_accessibility(struct mpam_msc *msc)
1149 {
1150 	u32 affinity_id;
1151 	int err;
1152 
1153 	err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",
1154 				       &affinity_id);
1155 	if (err)
1156 		cpumask_copy(&msc->accessibility, cpu_possible_mask);
1157 	else
1158 		acpi_pptt_get_cpus_from_container(affinity_id, &msc->accessibility);
1159 }
1160 
1161 /*
1162  * There are two ways of reaching a struct mpam_msc_ris. Via the
1163  * class->component->vmsc->ris, or via the msc.
1164  * When destroying the msc, the other side needs unlinking and cleaning up too.
1165  */
1166 static void mpam_msc_destroy(struct mpam_msc *msc)
1167 {
1168 	struct platform_device *pdev = msc->pdev;
1169 	struct mpam_msc_ris *ris, *tmp;
1170 
1171 	lockdep_assert_held(&mpam_list_lock);
1172 
1173 	list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list)
1174 		mpam_ris_destroy(ris);
1175 
1176 	list_del_rcu(&msc->all_msc_list);
1177 	platform_set_drvdata(pdev, NULL);
1178 
1179 	add_to_garbage(msc);
1180 }
1181 
1182 static void mpam_msc_drv_remove(struct platform_device *pdev)
1183 {
1184 	struct mpam_msc *msc = platform_get_drvdata(pdev);
1185 
1186 	mutex_lock(&mpam_list_lock);
1187 	mpam_msc_destroy(msc);
1188 	mutex_unlock(&mpam_list_lock);
1189 
1190 	mpam_free_garbage();
1191 }
1192 
1193 static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
1194 {
1195 	int err;
1196 	u32 tmp;
1197 	struct mpam_msc *msc;
1198 	struct resource *msc_res;
1199 	struct device *dev = &pdev->dev;
1200 
1201 	lockdep_assert_held(&mpam_list_lock);
1202 
1203 	msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
1204 	if (!msc)
1205 		return ERR_PTR(-ENOMEM);
1206 	init_garbage(&msc->garbage);
1207 	msc->garbage.pdev = pdev;
1208 
1209 	err = devm_mutex_init(dev, &msc->probe_lock);
1210 	if (err)
1211 		return ERR_PTR(err);
1212 
1213 	err = devm_mutex_init(dev, &msc->part_sel_lock);
1214 	if (err)
1215 		return ERR_PTR(err);
1216 
1217 	err = devm_mutex_init(dev, &msc->error_irq_lock);
1218 	if (err)
1219 		return ERR_PTR(err);
1220 
1221 	err = devm_mutex_init(dev, &msc->cfg_lock);
1222 	if (err)
1223 		return ERR_PTR(err);
1224 
1225 	mpam_mon_sel_lock_init(msc);
1226 	msc->id = pdev->id;
1227 	msc->pdev = pdev;
1228 	INIT_LIST_HEAD_RCU(&msc->all_msc_list);
1229 	INIT_LIST_HEAD_RCU(&msc->ris);
1230 
1231 	update_msc_accessibility(msc);
1232 	if (cpumask_empty(&msc->accessibility)) {
1233 		dev_err_once(dev, "MSC is not accessible from any CPU!");
1234 		return ERR_PTR(-EINVAL);
1235 	}
1236 
1237 	err = mpam_msc_setup_error_irq(msc);
1238 	if (err)
1239 		return ERR_PTR(err);
1240 
1241 	if (device_property_read_u32(&pdev->dev, "pcc-channel", &tmp))
1242 		msc->iface = MPAM_IFACE_MMIO;
1243 	else
1244 		msc->iface = MPAM_IFACE_PCC;
1245 
1246 	if (msc->iface == MPAM_IFACE_MMIO) {
1247 		void __iomem *io;
1248 
1249 		io = devm_platform_get_and_ioremap_resource(pdev, 0,
1250 							    &msc_res);
1251 		if (IS_ERR(io)) {
1252 			dev_err_once(dev, "Failed to map MSC base address\n");
1253 			return ERR_CAST(io);
1254 		}
1255 		msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
1256 		msc->mapped_hwpage = io;
1257 	} else {
1258 		return ERR_PTR(-EINVAL);
1259 	}
1260 
1261 	list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
1262 	platform_set_drvdata(pdev, msc);
1263 
1264 	return msc;
1265 }
1266 
1267 static int fw_num_msc;
1268 
1269 static int mpam_msc_drv_probe(struct platform_device *pdev)
1270 {
1271 	int err;
1272 	struct mpam_msc *msc = NULL;
1273 	void *plat_data = pdev->dev.platform_data;
1274 
1275 	mutex_lock(&mpam_list_lock);
1276 	msc = do_mpam_msc_drv_probe(pdev);
1277 	mutex_unlock(&mpam_list_lock);
1278 
1279 	if (IS_ERR(msc))
1280 		return PTR_ERR(msc);
1281 
1282 	/* Create RIS entries described by firmware */
1283 	err = acpi_mpam_parse_resources(msc, plat_data);
1284 	if (err) {
1285 		mpam_msc_drv_remove(pdev);
1286 		return err;
1287 	}
1288 
1289 	if (atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
1290 		mpam_register_cpuhp_callbacks(mpam_discovery_cpu_online, NULL,
1291 					      "mpam:drv_probe");
1292 
1293 	return 0;
1294 }
1295 
1296 static struct platform_driver mpam_msc_driver = {
1297 	.driver = {
1298 		.name = "mpam_msc",
1299 	},
1300 	.probe = mpam_msc_drv_probe,
1301 	.remove = mpam_msc_drv_remove,
1302 };
1303 
1304 /* Any of these features mean the BWA_WD field is valid. */
1305 static bool mpam_has_bwa_wd_feature(struct mpam_props *props)
1306 {
1307 	if (mpam_has_feature(mpam_feat_mbw_min, props))
1308 		return true;
1309 	if (mpam_has_feature(mpam_feat_mbw_max, props))
1310 		return true;
1311 	return false;
1312 }
1313 
1314 #define MISMATCHED_HELPER(parent, child, helper, field, alias)		\
1315 	helper(parent) &&						\
1316 	((helper(child) && (parent)->field != (child)->field) ||	\
1317 	 (!helper(child) && !(alias)))
1318 
1319 #define MISMATCHED_FEAT(parent, child, feat, field, alias)		     \
1320 	mpam_has_feature((feat), (parent)) &&				     \
1321 	((mpam_has_feature((feat), (child)) && (parent)->field != (child)->field) || \
1322 	 (!mpam_has_feature((feat), (child)) && !(alias)))
1323 
1324 #define CAN_MERGE_FEAT(parent, child, feat, alias)			\
1325 	(alias) && !mpam_has_feature((feat), (parent)) &&		\
1326 	mpam_has_feature((feat), (child))
1327 
1328 /*
1329  * Combine two props fields.
1330  * If this is for controls that alias the same resource, it is safe to just
1331  * copy the values over. If two aliasing controls implement the same scheme
1332  * a safe value must be picked.
1333  * For non-aliasing controls, these control different resources, and the
1334  * resulting safe value must be compatible with both. When merging values in
1335  * the tree, all the aliasing resources must be handled first.
1336  * On mismatch, parent is modified.
1337  */
1338 static void __props_mismatch(struct mpam_props *parent,
1339 			     struct mpam_props *child, bool alias)
1340 {
1341 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_cpor_part, alias)) {
1342 		parent->cpbm_wd = child->cpbm_wd;
1343 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_cpor_part,
1344 				   cpbm_wd, alias)) {
1345 		pr_debug("cleared cpor_part\n");
1346 		mpam_clear_feature(mpam_feat_cpor_part, parent);
1347 		parent->cpbm_wd = 0;
1348 	}
1349 
1350 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_mbw_part, alias)) {
1351 		parent->mbw_pbm_bits = child->mbw_pbm_bits;
1352 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_mbw_part,
1353 				   mbw_pbm_bits, alias)) {
1354 		pr_debug("cleared mbw_part\n");
1355 		mpam_clear_feature(mpam_feat_mbw_part, parent);
1356 		parent->mbw_pbm_bits = 0;
1357 	}
1358 
1359 	/* bwa_wd is a count of bits, fewer bits means less precision */
1360 	if (alias && !mpam_has_bwa_wd_feature(parent) &&
1361 	    mpam_has_bwa_wd_feature(child)) {
1362 		parent->bwa_wd = child->bwa_wd;
1363 	} else if (MISMATCHED_HELPER(parent, child, mpam_has_bwa_wd_feature,
1364 				     bwa_wd, alias)) {
1365 		pr_debug("took the min bwa_wd\n");
1366 		parent->bwa_wd = min(parent->bwa_wd, child->bwa_wd);
1367 	}
1368 
1369 	/* For num properties, take the minimum */
1370 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_csu, alias)) {
1371 		parent->num_csu_mon = child->num_csu_mon;
1372 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_csu,
1373 				   num_csu_mon, alias)) {
1374 		pr_debug("took the min num_csu_mon\n");
1375 		parent->num_csu_mon = min(parent->num_csu_mon,
1376 					  child->num_csu_mon);
1377 	}
1378 
1379 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_mbwu, alias)) {
1380 		parent->num_mbwu_mon = child->num_mbwu_mon;
1381 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_mbwu,
1382 				   num_mbwu_mon, alias)) {
1383 		pr_debug("took the min num_mbwu_mon\n");
1384 		parent->num_mbwu_mon = min(parent->num_mbwu_mon,
1385 					   child->num_mbwu_mon);
1386 	}
1387 
1388 	if (alias) {
1389 		/* Merge features for aliased resources */
1390 		bitmap_or(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
1391 	} else {
1392 		/* Clear missing features for non aliasing */
1393 		bitmap_and(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
1394 	}
1395 }
1396 
1397 /*
1398  * If a vmsc doesn't match class feature/configuration, do the right thing(tm).
1399  * For 'num' properties we can just take the minimum.
1400  * For properties where the mismatched unused bits would make a difference, we
1401  * nobble the class feature, as we can't configure all the resources.
1402  * e.g. The L3 cache is composed of two resources with 13 and 17 portion
1403  * bitmaps respectively.
1404  */
1405 static void
1406 __class_props_mismatch(struct mpam_class *class, struct mpam_vmsc *vmsc)
1407 {
1408 	struct mpam_props *cprops = &class->props;
1409 	struct mpam_props *vprops = &vmsc->props;
1410 	struct device *dev = &vmsc->msc->pdev->dev;
1411 
1412 	lockdep_assert_held(&mpam_list_lock); /* we modify class */
1413 
1414 	dev_dbg(dev, "Merging features for class:0x%lx &= vmsc:0x%lx\n",
1415 		(long)cprops->features, (long)vprops->features);
1416 
1417 	/* Take the safe value for any common features */
1418 	__props_mismatch(cprops, vprops, false);
1419 }
1420 
1421 static void
1422 __vmsc_props_mismatch(struct mpam_vmsc *vmsc, struct mpam_msc_ris *ris)
1423 {
1424 	struct mpam_props *rprops = &ris->props;
1425 	struct mpam_props *vprops = &vmsc->props;
1426 	struct device *dev = &vmsc->msc->pdev->dev;
1427 
1428 	lockdep_assert_held(&mpam_list_lock); /* we modify vmsc */
1429 
1430 	dev_dbg(dev, "Merging features for vmsc:0x%lx |= ris:0x%lx\n",
1431 		(long)vprops->features, (long)rprops->features);
1432 
1433 	/*
1434 	 * Merge mismatched features - Copy any features that aren't common,
1435 	 * but take the safe value for any common features.
1436 	 */
1437 	__props_mismatch(vprops, rprops, true);
1438 }
1439 
1440 /*
1441  * Copy the first component's first vMSC's properties and features to the
1442  * class. __class_props_mismatch() will remove conflicts.
1443  * It is not possible to have a class with no components, or a component with
1444  * no resources. The vMSC properties have already been built.
1445  */
1446 static void mpam_enable_init_class_features(struct mpam_class *class)
1447 {
1448 	struct mpam_vmsc *vmsc;
1449 	struct mpam_component *comp;
1450 
1451 	comp = list_first_entry(&class->components,
1452 				struct mpam_component, class_list);
1453 	vmsc = list_first_entry(&comp->vmsc,
1454 				struct mpam_vmsc, comp_list);
1455 
1456 	class->props = vmsc->props;
1457 }
1458 
1459 static void mpam_enable_merge_vmsc_features(struct mpam_component *comp)
1460 {
1461 	struct mpam_vmsc *vmsc;
1462 	struct mpam_msc_ris *ris;
1463 	struct mpam_class *class = comp->class;
1464 
1465 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
1466 		list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
1467 			__vmsc_props_mismatch(vmsc, ris);
1468 			class->nrdy_usec = max(class->nrdy_usec,
1469 					       vmsc->msc->nrdy_usec);
1470 		}
1471 	}
1472 }
1473 
1474 static void mpam_enable_merge_class_features(struct mpam_component *comp)
1475 {
1476 	struct mpam_vmsc *vmsc;
1477 	struct mpam_class *class = comp->class;
1478 
1479 	list_for_each_entry(vmsc, &comp->vmsc, comp_list)
1480 		__class_props_mismatch(class, vmsc);
1481 }
1482 
1483 /*
1484  * Merge all the common resource features into class.
1485  * vmsc features are bitwise-or'd together by mpam_enable_merge_vmsc_features()
1486  * as the first step so that mpam_enable_init_class_features() can initialise
1487  * the class with a representative set of features.
1488  * Next the mpam_enable_merge_class_features() bitwise-and's all the vmsc
1489  * features to form the class features.
1490  * Other features are the min/max as appropriate.
1491  *
1492  * To avoid walking the whole tree twice, the class->nrdy_usec property is
1493  * updated when working with the vmsc as it is a max(), and doesn't need
1494  * initialising first.
1495  */
1496 static void mpam_enable_merge_features(struct list_head *all_classes_list)
1497 {
1498 	struct mpam_class *class;
1499 	struct mpam_component *comp;
1500 
1501 	lockdep_assert_held(&mpam_list_lock);
1502 
1503 	list_for_each_entry(class, all_classes_list, classes_list) {
1504 		list_for_each_entry(comp, &class->components, class_list)
1505 			mpam_enable_merge_vmsc_features(comp);
1506 
1507 		mpam_enable_init_class_features(class);
1508 
1509 		list_for_each_entry(comp, &class->components, class_list)
1510 			mpam_enable_merge_class_features(comp);
1511 	}
1512 }
1513 
1514 static char *mpam_errcode_names[16] = {
1515 	[MPAM_ERRCODE_NONE]			= "No error",
1516 	[MPAM_ERRCODE_PARTID_SEL_RANGE]		= "PARTID_SEL_Range",
1517 	[MPAM_ERRCODE_REQ_PARTID_RANGE]		= "Req_PARTID_Range",
1518 	[MPAM_ERRCODE_MSMONCFG_ID_RANGE]	= "MSMONCFG_ID_RANGE",
1519 	[MPAM_ERRCODE_REQ_PMG_RANGE]		= "Req_PMG_Range",
1520 	[MPAM_ERRCODE_MONITOR_RANGE]		= "Monitor_Range",
1521 	[MPAM_ERRCODE_INTPARTID_RANGE]		= "intPARTID_Range",
1522 	[MPAM_ERRCODE_UNEXPECTED_INTERNAL]	= "Unexpected_INTERNAL",
1523 	[MPAM_ERRCODE_UNDEFINED_RIS_PART_SEL]	= "Undefined_RIS_PART_SEL",
1524 	[MPAM_ERRCODE_RIS_NO_CONTROL]		= "RIS_No_Control",
1525 	[MPAM_ERRCODE_UNDEFINED_RIS_MON_SEL]	= "Undefined_RIS_MON_SEL",
1526 	[MPAM_ERRCODE_RIS_NO_MONITOR]		= "RIS_No_Monitor",
1527 	[12 ... 15] = "Reserved"
1528 };
1529 
1530 static int mpam_enable_msc_ecr(void *_msc)
1531 {
1532 	struct mpam_msc *msc = _msc;
1533 
1534 	__mpam_write_reg(msc, MPAMF_ECR, MPAMF_ECR_INTEN);
1535 
1536 	return 0;
1537 }
1538 
1539 /* This can run in mpam_disable(), and the interrupt handler on the same CPU */
1540 static int mpam_disable_msc_ecr(void *_msc)
1541 {
1542 	struct mpam_msc *msc = _msc;
1543 
1544 	__mpam_write_reg(msc, MPAMF_ECR, 0);
1545 
1546 	return 0;
1547 }
1548 
1549 static irqreturn_t __mpam_irq_handler(int irq, struct mpam_msc *msc)
1550 {
1551 	u64 reg;
1552 	u16 partid;
1553 	u8 errcode, pmg, ris;
1554 
1555 	if (WARN_ON_ONCE(!msc) ||
1556 	    WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(),
1557 					   &msc->accessibility)))
1558 		return IRQ_NONE;
1559 
1560 	reg = mpam_msc_read_esr(msc);
1561 
1562 	errcode = FIELD_GET(MPAMF_ESR_ERRCODE, reg);
1563 	if (!errcode)
1564 		return IRQ_NONE;
1565 
1566 	/* Clear level triggered irq */
1567 	mpam_msc_clear_esr(msc);
1568 
1569 	partid = FIELD_GET(MPAMF_ESR_PARTID_MON, reg);
1570 	pmg = FIELD_GET(MPAMF_ESR_PMG, reg);
1571 	ris = FIELD_GET(MPAMF_ESR_RIS, reg);
1572 
1573 	pr_err_ratelimited("error irq from msc:%u '%s', partid:%u, pmg: %u, ris: %u\n",
1574 			   msc->id, mpam_errcode_names[errcode], partid, pmg,
1575 			   ris);
1576 
1577 	/* Disable this interrupt. */
1578 	mpam_disable_msc_ecr(msc);
1579 
1580 	/* Are we racing with the thread disabling MPAM? */
1581 	if (!mpam_is_enabled())
1582 		return IRQ_HANDLED;
1583 
1584 	/*
1585 	 * Schedule the teardown work. Don't use a threaded IRQ as we can't
1586 	 * unregister the interrupt from the threaded part of the handler.
1587 	 */
1588 	mpam_disable_reason = "hardware error interrupt";
1589 	schedule_work(&mpam_broken_work);
1590 
1591 	return IRQ_HANDLED;
1592 }
1593 
1594 static irqreturn_t mpam_ppi_handler(int irq, void *dev_id)
1595 {
1596 	struct mpam_msc *msc = *(struct mpam_msc **)dev_id;
1597 
1598 	return __mpam_irq_handler(irq, msc);
1599 }
1600 
1601 static irqreturn_t mpam_spi_handler(int irq, void *dev_id)
1602 {
1603 	struct mpam_msc *msc = dev_id;
1604 
1605 	return __mpam_irq_handler(irq, msc);
1606 }
1607 
1608 static int mpam_register_irqs(void)
1609 {
1610 	int err, irq;
1611 	struct mpam_msc *msc;
1612 
1613 	lockdep_assert_cpus_held();
1614 
1615 	guard(srcu)(&mpam_srcu);
1616 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1617 				 srcu_read_lock_held(&mpam_srcu)) {
1618 		irq = platform_get_irq_byname_optional(msc->pdev, "error");
1619 		if (irq <= 0)
1620 			continue;
1621 
1622 		/* The MPAM spec says the interrupt can be SPI, PPI or LPI */
1623 		/* We anticipate sharing the interrupt with other MSCs */
1624 		if (irq_is_percpu(irq)) {
1625 			err = request_percpu_irq(irq, &mpam_ppi_handler,
1626 						 "mpam:msc:error",
1627 						 msc->error_dev_id);
1628 			if (err)
1629 				return err;
1630 
1631 			msc->reenable_error_ppi = irq;
1632 			smp_call_function_many(&msc->accessibility,
1633 					       &_enable_percpu_irq, &irq,
1634 					       true);
1635 		} else {
1636 			err = devm_request_irq(&msc->pdev->dev, irq,
1637 					       &mpam_spi_handler, IRQF_SHARED,
1638 					       "mpam:msc:error", msc);
1639 			if (err)
1640 				return err;
1641 		}
1642 
1643 		mutex_lock(&msc->error_irq_lock);
1644 		msc->error_irq_req = true;
1645 		mpam_touch_msc(msc, mpam_enable_msc_ecr, msc);
1646 		msc->error_irq_hw_enabled = true;
1647 		mutex_unlock(&msc->error_irq_lock);
1648 	}
1649 
1650 	return 0;
1651 }
1652 
1653 static void mpam_unregister_irqs(void)
1654 {
1655 	int irq;
1656 	struct mpam_msc *msc;
1657 
1658 	guard(cpus_read_lock)();
1659 	guard(srcu)(&mpam_srcu);
1660 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1661 				 srcu_read_lock_held(&mpam_srcu)) {
1662 		irq = platform_get_irq_byname_optional(msc->pdev, "error");
1663 		if (irq <= 0)
1664 			continue;
1665 
1666 		mutex_lock(&msc->error_irq_lock);
1667 		if (msc->error_irq_hw_enabled) {
1668 			mpam_touch_msc(msc, mpam_disable_msc_ecr, msc);
1669 			msc->error_irq_hw_enabled = false;
1670 		}
1671 
1672 		if (msc->error_irq_req) {
1673 			if (irq_is_percpu(irq)) {
1674 				msc->reenable_error_ppi = 0;
1675 				free_percpu_irq(irq, msc->error_dev_id);
1676 			} else {
1677 				devm_free_irq(&msc->pdev->dev, irq, msc);
1678 			}
1679 			msc->error_irq_req = false;
1680 		}
1681 		mutex_unlock(&msc->error_irq_lock);
1682 	}
1683 }
1684 
1685 static void __destroy_component_cfg(struct mpam_component *comp)
1686 {
1687 	add_to_garbage(comp->cfg);
1688 }
1689 
1690 static void mpam_reset_component_cfg(struct mpam_component *comp)
1691 {
1692 	int i;
1693 	struct mpam_props *cprops = &comp->class->props;
1694 
1695 	mpam_assert_partid_sizes_fixed();
1696 
1697 	if (!comp->cfg)
1698 		return;
1699 
1700 	for (i = 0; i <= mpam_partid_max; i++) {
1701 		comp->cfg[i] = (struct mpam_config) {};
1702 		if (cprops->cpbm_wd)
1703 			comp->cfg[i].cpbm = GENMASK(cprops->cpbm_wd - 1, 0);
1704 		if (cprops->mbw_pbm_bits)
1705 			comp->cfg[i].mbw_pbm = GENMASK(cprops->mbw_pbm_bits - 1, 0);
1706 		if (cprops->bwa_wd)
1707 			comp->cfg[i].mbw_max = GENMASK(15, 16 - cprops->bwa_wd);
1708 	}
1709 }
1710 
1711 static int __allocate_component_cfg(struct mpam_component *comp)
1712 {
1713 	mpam_assert_partid_sizes_fixed();
1714 
1715 	if (comp->cfg)
1716 		return 0;
1717 
1718 	comp->cfg = kcalloc(mpam_partid_max + 1, sizeof(*comp->cfg), GFP_KERNEL);
1719 	if (!comp->cfg)
1720 		return -ENOMEM;
1721 
1722 	/*
1723 	 * The array is free()d in one go, so only cfg[0]'s structure needs
1724 	 * to be initialised.
1725 	 */
1726 	init_garbage(&comp->cfg[0].garbage);
1727 
1728 	mpam_reset_component_cfg(comp);
1729 
1730 	return 0;
1731 }
1732 
1733 static int mpam_allocate_config(void)
1734 {
1735 	struct mpam_class *class;
1736 	struct mpam_component *comp;
1737 
1738 	lockdep_assert_held(&mpam_list_lock);
1739 
1740 	list_for_each_entry(class, &mpam_classes, classes_list) {
1741 		list_for_each_entry(comp, &class->components, class_list) {
1742 			int err = __allocate_component_cfg(comp);
1743 			if (err)
1744 				return err;
1745 		}
1746 	}
1747 
1748 	return 0;
1749 }
1750 
1751 static void mpam_enable_once(void)
1752 {
1753 	int err;
1754 
1755 	/*
1756 	 * Once the cpuhp callbacks have been changed, mpam_partid_max can no
1757 	 * longer change.
1758 	 */
1759 	spin_lock(&partid_max_lock);
1760 	partid_max_published = true;
1761 	spin_unlock(&partid_max_lock);
1762 
1763 	/*
1764 	 * If all the MSC have been probed, enabling the IRQs happens next.
1765 	 * That involves cross-calling to a CPU that can reach the MSC, and
1766 	 * the locks must be taken in this order:
1767 	 */
1768 	cpus_read_lock();
1769 	mutex_lock(&mpam_list_lock);
1770 	do {
1771 		mpam_enable_merge_features(&mpam_classes);
1772 
1773 		err = mpam_register_irqs();
1774 		if (err) {
1775 			pr_warn("Failed to register irqs: %d\n", err);
1776 			break;
1777 		}
1778 
1779 		err = mpam_allocate_config();
1780 		if (err) {
1781 			pr_err("Failed to allocate configuration arrays.\n");
1782 			break;
1783 		}
1784 	} while (0);
1785 	mutex_unlock(&mpam_list_lock);
1786 	cpus_read_unlock();
1787 
1788 	if (err) {
1789 		mpam_disable_reason = "Failed to enable.";
1790 		schedule_work(&mpam_broken_work);
1791 		return;
1792 	}
1793 
1794 	static_branch_enable(&mpam_enabled);
1795 	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline,
1796 				      "mpam:online");
1797 
1798 	/* Use printk() to avoid the pr_fmt adding the function name. */
1799 	printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n",
1800 	       mpam_partid_max + 1, mpam_pmg_max + 1);
1801 }
1802 
1803 static void mpam_reset_component_locked(struct mpam_component *comp)
1804 {
1805 	struct mpam_vmsc *vmsc;
1806 
1807 	lockdep_assert_cpus_held();
1808 	mpam_assert_partid_sizes_fixed();
1809 
1810 	mpam_reset_component_cfg(comp);
1811 
1812 	guard(srcu)(&mpam_srcu);
1813 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
1814 				 srcu_read_lock_held(&mpam_srcu)) {
1815 		struct mpam_msc *msc = vmsc->msc;
1816 		struct mpam_msc_ris *ris;
1817 
1818 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
1819 					 srcu_read_lock_held(&mpam_srcu)) {
1820 			if (!ris->in_reset_state)
1821 				mpam_touch_msc(msc, mpam_reset_ris, ris);
1822 			ris->in_reset_state = true;
1823 		}
1824 	}
1825 }
1826 
1827 static void mpam_reset_class_locked(struct mpam_class *class)
1828 {
1829 	struct mpam_component *comp;
1830 
1831 	lockdep_assert_cpus_held();
1832 
1833 	guard(srcu)(&mpam_srcu);
1834 	list_for_each_entry_srcu(comp, &class->components, class_list,
1835 				 srcu_read_lock_held(&mpam_srcu))
1836 		mpam_reset_component_locked(comp);
1837 }
1838 
1839 static void mpam_reset_class(struct mpam_class *class)
1840 {
1841 	cpus_read_lock();
1842 	mpam_reset_class_locked(class);
1843 	cpus_read_unlock();
1844 }
1845 
1846 /*
1847  * Called in response to an error IRQ.
1848  * All of MPAMs errors indicate a software bug, restore any modified
1849  * controls to their reset values.
1850  */
1851 void mpam_disable(struct work_struct *ignored)
1852 {
1853 	int idx;
1854 	struct mpam_class *class;
1855 	struct mpam_msc *msc, *tmp;
1856 
1857 	mutex_lock(&mpam_cpuhp_state_lock);
1858 	if (mpam_cpuhp_state) {
1859 		cpuhp_remove_state(mpam_cpuhp_state);
1860 		mpam_cpuhp_state = 0;
1861 	}
1862 	mutex_unlock(&mpam_cpuhp_state_lock);
1863 
1864 	static_branch_disable(&mpam_enabled);
1865 
1866 	mpam_unregister_irqs();
1867 
1868 	idx = srcu_read_lock(&mpam_srcu);
1869 	list_for_each_entry_srcu(class, &mpam_classes, classes_list,
1870 				 srcu_read_lock_held(&mpam_srcu))
1871 		mpam_reset_class(class);
1872 	srcu_read_unlock(&mpam_srcu, idx);
1873 
1874 	mutex_lock(&mpam_list_lock);
1875 	list_for_each_entry_safe(msc, tmp, &mpam_all_msc, all_msc_list)
1876 		mpam_msc_destroy(msc);
1877 	mutex_unlock(&mpam_list_lock);
1878 	mpam_free_garbage();
1879 
1880 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
1881 }
1882 
1883 /*
1884  * Enable mpam once all devices have been probed.
1885  * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
1886  * Also scheduled when new devices are probed when new CPUs come online.
1887  */
1888 void mpam_enable(struct work_struct *work)
1889 {
1890 	static atomic_t once;
1891 	struct mpam_msc *msc;
1892 	bool all_devices_probed = true;
1893 
1894 	/* Have we probed all the hw devices? */
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 		mutex_lock(&msc->probe_lock);
1899 		if (!msc->probed)
1900 			all_devices_probed = false;
1901 		mutex_unlock(&msc->probe_lock);
1902 
1903 		if (!all_devices_probed)
1904 			break;
1905 	}
1906 
1907 	if (all_devices_probed && !atomic_fetch_inc(&once))
1908 		mpam_enable_once();
1909 }
1910 
1911 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
1912 	if (mpam_has_feature(feature, newcfg) &&			\
1913 	    (newcfg)->member != (cfg)->member) {			\
1914 		(cfg)->member = (newcfg)->member;			\
1915 		mpam_set_feature(feature, cfg);				\
1916 									\
1917 		(changes) = true;					\
1918 	}								\
1919 } while (0)
1920 
1921 static bool mpam_update_config(struct mpam_config *cfg,
1922 			       const struct mpam_config *newcfg)
1923 {
1924 	bool has_changes = false;
1925 
1926 	maybe_update_config(cfg, mpam_feat_cpor_part, newcfg, cpbm, has_changes);
1927 	maybe_update_config(cfg, mpam_feat_mbw_part, newcfg, mbw_pbm, has_changes);
1928 	maybe_update_config(cfg, mpam_feat_mbw_max, newcfg, mbw_max, has_changes);
1929 
1930 	return has_changes;
1931 }
1932 
1933 int mpam_apply_config(struct mpam_component *comp, u16 partid,
1934 		      struct mpam_config *cfg)
1935 {
1936 	struct mpam_write_config_arg arg;
1937 	struct mpam_msc_ris *ris;
1938 	struct mpam_vmsc *vmsc;
1939 	struct mpam_msc *msc;
1940 
1941 	lockdep_assert_cpus_held();
1942 
1943 	/* Don't pass in the current config! */
1944 	WARN_ON_ONCE(&comp->cfg[partid] == cfg);
1945 
1946 	if (!mpam_update_config(&comp->cfg[partid], cfg))
1947 		return 0;
1948 
1949 	arg.comp = comp;
1950 	arg.partid = partid;
1951 
1952 	guard(srcu)(&mpam_srcu);
1953 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
1954 				 srcu_read_lock_held(&mpam_srcu)) {
1955 		msc = vmsc->msc;
1956 
1957 		mutex_lock(&msc->cfg_lock);
1958 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
1959 					 srcu_read_lock_held(&mpam_srcu)) {
1960 			arg.ris = ris;
1961 			mpam_touch_msc(msc, __write_config, &arg);
1962 		}
1963 		mutex_unlock(&msc->cfg_lock);
1964 	}
1965 
1966 	return 0;
1967 }
1968 
1969 static int __init mpam_msc_driver_init(void)
1970 {
1971 	if (!system_supports_mpam())
1972 		return -EOPNOTSUPP;
1973 
1974 	init_srcu_struct(&mpam_srcu);
1975 
1976 	fw_num_msc = acpi_mpam_count_msc();
1977 	if (fw_num_msc <= 0) {
1978 		pr_err("No MSC devices found in firmware\n");
1979 		return -EINVAL;
1980 	}
1981 
1982 	return platform_driver_register(&mpam_msc_driver);
1983 }
1984 
1985 /* Must occur after arm64_mpam_register_cpus() from arch_initcall() */
1986 subsys_initcall(mpam_msc_driver_init);
1987