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