xref: /linux/drivers/resctrl/mpam_devices.c (revision c891bae66423bc69a680ca1de34940132e2c8ace)
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 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 			/* Is NRDY hardware managed? */
797 			hw_managed = mpam_ris_hw_probe_hw_nrdy(ris, MBWU);
798 			if (hw_managed)
799 				mpam_set_feature(mpam_feat_msmon_mbwu_hw_nrdy, props);
800 
801 			/*
802 			 * Don't warn about any missing firmware property for
803 			 * MBWU NRDY - it doesn't make any sense!
804 			 */
805 		}
806 	}
807 
808 	/*
809 	 * RIS with PARTID narrowing don't have enough storage for one
810 	 * configuration per PARTID. If these are in a class we could use,
811 	 * reduce the supported partid_max to match the number of intpartid.
812 	 * If the class is unknown, just ignore it.
813 	 */
814 	if (FIELD_GET(MPAMF_IDR_HAS_PARTID_NRW, ris->idr) &&
815 	    class->type != MPAM_CLASS_UNKNOWN) {
816 		u32 nrwidr = mpam_read_partsel_reg(msc, PARTID_NRW_IDR);
817 		u16 partid_max = FIELD_GET(MPAMF_PARTID_NRW_IDR_INTPARTID_MAX, nrwidr);
818 
819 		mpam_set_feature(mpam_feat_partid_nrw, props);
820 		msc->partid_max = min(msc->partid_max, partid_max);
821 	}
822 }
823 
824 static int mpam_msc_hw_probe(struct mpam_msc *msc)
825 {
826 	u64 idr;
827 	u16 partid_max;
828 	u8 ris_idx, pmg_max;
829 	struct mpam_msc_ris *ris;
830 	struct device *dev = &msc->pdev->dev;
831 
832 	lockdep_assert_held(&msc->probe_lock);
833 
834 	idr = __mpam_read_reg(msc, MPAMF_AIDR);
835 	if ((idr & MPAMF_AIDR_ARCH_MAJOR_REV) != MPAM_ARCHITECTURE_V1) {
836 		dev_err_once(dev, "MSC does not match MPAM architecture v1.x\n");
837 		return -EIO;
838 	}
839 
840 	/* Grab an IDR value to find out how many RIS there are */
841 	mutex_lock(&msc->part_sel_lock);
842 	idr = mpam_msc_read_idr(msc);
843 	mutex_unlock(&msc->part_sel_lock);
844 
845 	msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr);
846 
847 	/* Use these values so partid/pmg always starts with a valid value */
848 	msc->partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
849 	msc->pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
850 
851 	for (ris_idx = 0; ris_idx <= msc->ris_max; ris_idx++) {
852 		mutex_lock(&msc->part_sel_lock);
853 		__mpam_part_sel(ris_idx, 0, msc);
854 		idr = mpam_msc_read_idr(msc);
855 		mutex_unlock(&msc->part_sel_lock);
856 
857 		partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
858 		pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
859 		msc->partid_max = min(msc->partid_max, partid_max);
860 		msc->pmg_max = min(msc->pmg_max, pmg_max);
861 		msc->has_extd_esr = FIELD_GET(MPAMF_IDR_HAS_EXTD_ESR, idr);
862 
863 		mutex_lock(&mpam_list_lock);
864 		ris = mpam_get_or_create_ris(msc, ris_idx);
865 		mutex_unlock(&mpam_list_lock);
866 		if (IS_ERR(ris))
867 			return PTR_ERR(ris);
868 		ris->idr = idr;
869 
870 		mutex_lock(&msc->part_sel_lock);
871 		__mpam_part_sel(ris_idx, 0, msc);
872 		mpam_ris_hw_probe(ris);
873 		mutex_unlock(&msc->part_sel_lock);
874 	}
875 
876 	/* Clear any stale errors */
877 	mpam_msc_clear_esr(msc);
878 
879 	spin_lock(&partid_max_lock);
880 	mpam_partid_max = min(mpam_partid_max, msc->partid_max);
881 	mpam_pmg_max = min(mpam_pmg_max, msc->pmg_max);
882 	spin_unlock(&partid_max_lock);
883 
884 	msc->probed = true;
885 
886 	return 0;
887 }
888 
889 static void mpam_reset_msc_bitmap(struct mpam_msc *msc, u16 reg, u16 wd)
890 {
891 	u32 num_words, msb;
892 	u32 bm = ~0;
893 	int i;
894 
895 	lockdep_assert_held(&msc->part_sel_lock);
896 
897 	if (wd == 0)
898 		return;
899 
900 	/*
901 	 * Write all ~0 to all but the last 32bit-word, which may
902 	 * have fewer bits...
903 	 */
904 	num_words = DIV_ROUND_UP(wd, 32);
905 	for (i = 0; i < num_words - 1; i++, reg += sizeof(bm))
906 		__mpam_write_reg(msc, reg, bm);
907 
908 	/*
909 	 * ....and then the last (maybe) partial 32bit word. When wd is a
910 	 * multiple of 32, msb should be 31 to write a full 32bit word.
911 	 */
912 	msb = (wd - 1) % 32;
913 	bm = GENMASK(msb, 0);
914 	__mpam_write_reg(msc, reg, bm);
915 }
916 
917 /* Called via IPI. Call while holding an SRCU reference */
918 static void mpam_reprogram_ris_partid(struct mpam_msc_ris *ris, u16 partid,
919 				      struct mpam_config *cfg)
920 {
921 	u32 pri_val = 0;
922 	u16 cmax = MPAMCFG_CMAX_CMAX;
923 	struct mpam_msc *msc = ris->vmsc->msc;
924 	struct mpam_props *rprops = &ris->props;
925 	u16 dspri = GENMASK(rprops->dspri_wd, 0);
926 	u16 intpri = GENMASK(rprops->intpri_wd, 0);
927 
928 	mutex_lock(&msc->part_sel_lock);
929 	__mpam_part_sel(ris->ris_idx, partid, msc);
930 
931 	if (mpam_has_feature(mpam_feat_partid_nrw, rprops)) {
932 		/* Update the intpartid mapping */
933 		mpam_write_partsel_reg(msc, INTPARTID,
934 				       MPAMCFG_INTPARTID_INTERNAL | partid);
935 
936 		/*
937 		 * Then switch to the 'internal' partid to update the
938 		 * configuration.
939 		 */
940 		__mpam_intpart_sel(ris->ris_idx, partid, msc);
941 	}
942 
943 	if (mpam_has_feature(mpam_feat_cpor_part, rprops) &&
944 	    mpam_has_feature(mpam_feat_cpor_part, cfg)) {
945 		if (cfg->reset_cpbm)
946 			mpam_reset_msc_bitmap(msc, MPAMCFG_CPBM, rprops->cpbm_wd);
947 		else
948 			mpam_write_partsel_reg(msc, CPBM, cfg->cpbm);
949 	}
950 
951 	if (mpam_has_feature(mpam_feat_mbw_part, rprops) &&
952 	    mpam_has_feature(mpam_feat_mbw_part, cfg)) {
953 		if (cfg->reset_mbw_pbm)
954 			mpam_reset_msc_bitmap(msc, MPAMCFG_MBW_PBM, rprops->mbw_pbm_bits);
955 		else
956 			mpam_write_partsel_reg(msc, MBW_PBM, cfg->mbw_pbm);
957 	}
958 
959 	if (mpam_has_feature(mpam_feat_mbw_min, rprops) &&
960 	    mpam_has_feature(mpam_feat_mbw_min, cfg))
961 		mpam_write_partsel_reg(msc, MBW_MIN, 0);
962 
963 	if (mpam_has_feature(mpam_feat_mbw_max, rprops) &&
964 	    mpam_has_feature(mpam_feat_mbw_max, cfg)) {
965 		if (cfg->reset_mbw_max)
966 			mpam_write_partsel_reg(msc, MBW_MAX, MPAMCFG_MBW_MAX_MAX);
967 		else
968 			mpam_write_partsel_reg(msc, MBW_MAX, cfg->mbw_max);
969 	}
970 
971 	if (mpam_has_feature(mpam_feat_mbw_prop, rprops) &&
972 	    mpam_has_feature(mpam_feat_mbw_prop, cfg))
973 		mpam_write_partsel_reg(msc, MBW_PROP, 0);
974 
975 	if (mpam_has_feature(mpam_feat_cmax_cmax, rprops))
976 		mpam_write_partsel_reg(msc, CMAX, cmax);
977 
978 	if (mpam_has_feature(mpam_feat_cmax_cmin, rprops))
979 		mpam_write_partsel_reg(msc, CMIN, 0);
980 
981 	if (mpam_has_feature(mpam_feat_cmax_cassoc, rprops))
982 		mpam_write_partsel_reg(msc, CASSOC, MPAMCFG_CASSOC_CASSOC);
983 
984 	if (mpam_has_feature(mpam_feat_intpri_part, rprops) ||
985 	    mpam_has_feature(mpam_feat_dspri_part, rprops)) {
986 		/* aces high? */
987 		if (!mpam_has_feature(mpam_feat_intpri_part_0_low, rprops))
988 			intpri = 0;
989 		if (!mpam_has_feature(mpam_feat_dspri_part_0_low, rprops))
990 			dspri = 0;
991 
992 		if (mpam_has_feature(mpam_feat_intpri_part, rprops))
993 			pri_val |= FIELD_PREP(MPAMCFG_PRI_INTPRI, intpri);
994 		if (mpam_has_feature(mpam_feat_dspri_part, rprops))
995 			pri_val |= FIELD_PREP(MPAMCFG_PRI_DSPRI, dspri);
996 
997 		mpam_write_partsel_reg(msc, PRI, pri_val);
998 	}
999 
1000 	mutex_unlock(&msc->part_sel_lock);
1001 }
1002 
1003 static void mpam_init_reset_cfg(struct mpam_config *reset_cfg)
1004 {
1005 	*reset_cfg = (struct mpam_config) {
1006 		.reset_cpbm = true,
1007 		.reset_mbw_pbm = true,
1008 		.reset_mbw_max = true,
1009 	};
1010 	bitmap_fill(reset_cfg->features, MPAM_FEATURE_LAST);
1011 }
1012 
1013 /*
1014  * Called via smp_call_on_cpu() to prevent migration, while still being
1015  * pre-emptible. Caller must hold mpam_srcu.
1016  */
1017 static int mpam_reset_ris(void *arg)
1018 {
1019 	u16 partid, partid_max;
1020 	struct mpam_config reset_cfg;
1021 	struct mpam_msc_ris *ris = arg;
1022 
1023 	if (ris->in_reset_state)
1024 		return 0;
1025 
1026 	mpam_init_reset_cfg(&reset_cfg);
1027 
1028 	spin_lock(&partid_max_lock);
1029 	partid_max = mpam_partid_max;
1030 	spin_unlock(&partid_max_lock);
1031 	for (partid = 0; partid <= partid_max; partid++)
1032 		mpam_reprogram_ris_partid(ris, partid, &reset_cfg);
1033 
1034 	return 0;
1035 }
1036 
1037 /*
1038  * Get the preferred CPU for this MSC. If it is accessible from this CPU,
1039  * this CPU is preferred. This can be preempted/migrated, it will only result
1040  * in more work.
1041  */
1042 static int mpam_get_msc_preferred_cpu(struct mpam_msc *msc)
1043 {
1044 	int cpu = raw_smp_processor_id();
1045 
1046 	if (cpumask_test_cpu(cpu, &msc->accessibility))
1047 		return cpu;
1048 
1049 	return cpumask_first_and(&msc->accessibility, cpu_online_mask);
1050 }
1051 
1052 static int mpam_touch_msc(struct mpam_msc *msc, int (*fn)(void *a), void *arg)
1053 {
1054 	lockdep_assert_irqs_enabled();
1055 	lockdep_assert_cpus_held();
1056 	WARN_ON_ONCE(!srcu_read_lock_held((&mpam_srcu)));
1057 
1058 	return smp_call_on_cpu(mpam_get_msc_preferred_cpu(msc), fn, arg, true);
1059 }
1060 
1061 struct mpam_write_config_arg {
1062 	struct mpam_msc_ris *ris;
1063 	struct mpam_component *comp;
1064 	u16 partid;
1065 };
1066 
1067 static int __write_config(void *arg)
1068 {
1069 	struct mpam_write_config_arg *c = arg;
1070 
1071 	mpam_reprogram_ris_partid(c->ris, c->partid, &c->comp->cfg[c->partid]);
1072 
1073 	return 0;
1074 }
1075 
1076 static void mpam_reprogram_msc(struct mpam_msc *msc)
1077 {
1078 	u16 partid;
1079 	bool reset;
1080 	struct mpam_config *cfg;
1081 	struct mpam_msc_ris *ris;
1082 	struct mpam_write_config_arg arg;
1083 
1084 	/*
1085 	 * No lock for mpam_partid_max as partid_max_published has been
1086 	 * set by mpam_enabled(), so the values can no longer change.
1087 	 */
1088 	mpam_assert_partid_sizes_fixed();
1089 
1090 	mutex_lock(&msc->cfg_lock);
1091 	list_for_each_entry_srcu(ris, &msc->ris, msc_list,
1092 				 srcu_read_lock_held(&mpam_srcu)) {
1093 		if (!mpam_is_enabled() && !ris->in_reset_state) {
1094 			mpam_touch_msc(msc, &mpam_reset_ris, ris);
1095 			ris->in_reset_state = true;
1096 			continue;
1097 		}
1098 
1099 		arg.comp = ris->vmsc->comp;
1100 		arg.ris = ris;
1101 		reset = true;
1102 		for (partid = 0; partid <= mpam_partid_max; partid++) {
1103 			cfg = &ris->vmsc->comp->cfg[partid];
1104 			if (!bitmap_empty(cfg->features, MPAM_FEATURE_LAST))
1105 				reset = false;
1106 
1107 			arg.partid = partid;
1108 			mpam_touch_msc(msc, __write_config, &arg);
1109 		}
1110 		ris->in_reset_state = reset;
1111 	}
1112 	mutex_unlock(&msc->cfg_lock);
1113 }
1114 
1115 static void _enable_percpu_irq(void *_irq)
1116 {
1117 	int *irq = _irq;
1118 
1119 	enable_percpu_irq(*irq, IRQ_TYPE_NONE);
1120 }
1121 
1122 static int mpam_cpu_online(unsigned int cpu)
1123 {
1124 	struct mpam_msc *msc;
1125 
1126 	guard(srcu)(&mpam_srcu);
1127 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1128 				 srcu_read_lock_held(&mpam_srcu)) {
1129 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1130 			continue;
1131 
1132 		if (msc->reenable_error_ppi)
1133 			_enable_percpu_irq(&msc->reenable_error_ppi);
1134 
1135 		if (atomic_fetch_inc(&msc->online_refs) == 0)
1136 			mpam_reprogram_msc(msc);
1137 	}
1138 
1139 	return 0;
1140 }
1141 
1142 /* Before mpam is enabled, try to probe new MSC */
1143 static int mpam_discovery_cpu_online(unsigned int cpu)
1144 {
1145 	int err = 0;
1146 	struct mpam_msc *msc;
1147 	bool new_device_probed = false;
1148 
1149 	if (mpam_is_enabled())
1150 		return 0;
1151 
1152 	guard(srcu)(&mpam_srcu);
1153 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1154 				 srcu_read_lock_held(&mpam_srcu)) {
1155 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1156 			continue;
1157 
1158 		mutex_lock(&msc->probe_lock);
1159 		if (!msc->probed)
1160 			err = mpam_msc_hw_probe(msc);
1161 		mutex_unlock(&msc->probe_lock);
1162 
1163 		if (err)
1164 			break;
1165 		new_device_probed = true;
1166 	}
1167 
1168 	if (new_device_probed && !err)
1169 		schedule_work(&mpam_enable_work);
1170 	if (err) {
1171 		mpam_disable_reason = "error during probing";
1172 		schedule_work(&mpam_broken_work);
1173 	}
1174 
1175 	return err;
1176 }
1177 
1178 static int mpam_cpu_offline(unsigned int cpu)
1179 {
1180 	struct mpam_msc *msc;
1181 
1182 	guard(srcu)(&mpam_srcu);
1183 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1184 				 srcu_read_lock_held(&mpam_srcu)) {
1185 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
1186 			continue;
1187 
1188 		if (msc->reenable_error_ppi)
1189 			disable_percpu_irq(msc->reenable_error_ppi);
1190 
1191 		if (atomic_dec_and_test(&msc->online_refs)) {
1192 			struct mpam_msc_ris *ris;
1193 
1194 			mutex_lock(&msc->cfg_lock);
1195 			list_for_each_entry_srcu(ris, &msc->ris, msc_list,
1196 						 srcu_read_lock_held(&mpam_srcu)) {
1197 				mpam_touch_msc(msc, &mpam_reset_ris, ris);
1198 
1199 				/*
1200 				 * The reset state for non-zero partid may be
1201 				 * lost while the CPUs are offline.
1202 				 */
1203 				ris->in_reset_state = false;
1204 			}
1205 			mutex_unlock(&msc->cfg_lock);
1206 		}
1207 	}
1208 
1209 	return 0;
1210 }
1211 
1212 static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online),
1213 					  int (*offline)(unsigned int offline),
1214 					  char *name)
1215 {
1216 	mutex_lock(&mpam_cpuhp_state_lock);
1217 	if (mpam_cpuhp_state) {
1218 		cpuhp_remove_state(mpam_cpuhp_state);
1219 		mpam_cpuhp_state = 0;
1220 	}
1221 
1222 	mpam_cpuhp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, name, online,
1223 					     offline);
1224 	if (mpam_cpuhp_state <= 0) {
1225 		pr_err("Failed to register cpuhp callbacks");
1226 		mpam_cpuhp_state = 0;
1227 	}
1228 	mutex_unlock(&mpam_cpuhp_state_lock);
1229 }
1230 
1231 static int __setup_ppi(struct mpam_msc *msc)
1232 {
1233 	int cpu;
1234 
1235 	msc->error_dev_id = alloc_percpu(struct mpam_msc *);
1236 	if (!msc->error_dev_id)
1237 		return -ENOMEM;
1238 
1239 	for_each_cpu(cpu, &msc->accessibility)
1240 		*per_cpu_ptr(msc->error_dev_id, cpu) = msc;
1241 
1242 	return 0;
1243 }
1244 
1245 static int mpam_msc_setup_error_irq(struct mpam_msc *msc)
1246 {
1247 	int irq;
1248 
1249 	irq = platform_get_irq_byname_optional(msc->pdev, "error");
1250 	if (irq <= 0)
1251 		return 0;
1252 
1253 	/* Allocate and initialise the percpu device pointer for PPI */
1254 	if (irq_is_percpu(irq))
1255 		return __setup_ppi(msc);
1256 
1257 	/* sanity check: shared interrupts can be routed anywhere? */
1258 	if (!cpumask_equal(&msc->accessibility, cpu_possible_mask)) {
1259 		pr_err_once("msc:%u is a private resource with a shared error interrupt",
1260 			    msc->id);
1261 		return -EINVAL;
1262 	}
1263 
1264 	return 0;
1265 }
1266 
1267 /*
1268  * An MSC can control traffic from a set of CPUs, but may only be accessible
1269  * from a (hopefully wider) set of CPUs. The common reason for this is power
1270  * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
1271  * corresponding cache may also be powered off. By making accesses from
1272  * one of those CPUs, we ensure we don't access a cache that's powered off.
1273  */
1274 static void update_msc_accessibility(struct mpam_msc *msc)
1275 {
1276 	u32 affinity_id;
1277 	int err;
1278 
1279 	err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",
1280 				       &affinity_id);
1281 	if (err)
1282 		cpumask_copy(&msc->accessibility, cpu_possible_mask);
1283 	else
1284 		acpi_pptt_get_cpus_from_container(affinity_id, &msc->accessibility);
1285 }
1286 
1287 /*
1288  * There are two ways of reaching a struct mpam_msc_ris. Via the
1289  * class->component->vmsc->ris, or via the msc.
1290  * When destroying the msc, the other side needs unlinking and cleaning up too.
1291  */
1292 static void mpam_msc_destroy(struct mpam_msc *msc)
1293 {
1294 	struct platform_device *pdev = msc->pdev;
1295 	struct mpam_msc_ris *ris, *tmp;
1296 
1297 	lockdep_assert_held(&mpam_list_lock);
1298 
1299 	list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list)
1300 		mpam_ris_destroy(ris);
1301 
1302 	list_del_rcu(&msc->all_msc_list);
1303 	platform_set_drvdata(pdev, NULL);
1304 
1305 	add_to_garbage(msc);
1306 }
1307 
1308 static void mpam_msc_drv_remove(struct platform_device *pdev)
1309 {
1310 	struct mpam_msc *msc = platform_get_drvdata(pdev);
1311 
1312 	mutex_lock(&mpam_list_lock);
1313 	mpam_msc_destroy(msc);
1314 	mutex_unlock(&mpam_list_lock);
1315 
1316 	mpam_free_garbage();
1317 }
1318 
1319 static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
1320 {
1321 	int err;
1322 	u32 tmp;
1323 	struct mpam_msc *msc;
1324 	struct resource *msc_res;
1325 	struct device *dev = &pdev->dev;
1326 
1327 	lockdep_assert_held(&mpam_list_lock);
1328 
1329 	msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
1330 	if (!msc)
1331 		return ERR_PTR(-ENOMEM);
1332 	init_garbage(&msc->garbage);
1333 	msc->garbage.pdev = pdev;
1334 
1335 	err = devm_mutex_init(dev, &msc->probe_lock);
1336 	if (err)
1337 		return ERR_PTR(err);
1338 
1339 	err = devm_mutex_init(dev, &msc->part_sel_lock);
1340 	if (err)
1341 		return ERR_PTR(err);
1342 
1343 	err = devm_mutex_init(dev, &msc->error_irq_lock);
1344 	if (err)
1345 		return ERR_PTR(err);
1346 
1347 	err = devm_mutex_init(dev, &msc->cfg_lock);
1348 	if (err)
1349 		return ERR_PTR(err);
1350 
1351 	mpam_mon_sel_lock_init(msc);
1352 	msc->id = pdev->id;
1353 	msc->pdev = pdev;
1354 	INIT_LIST_HEAD_RCU(&msc->all_msc_list);
1355 	INIT_LIST_HEAD_RCU(&msc->ris);
1356 
1357 	update_msc_accessibility(msc);
1358 	if (cpumask_empty(&msc->accessibility)) {
1359 		dev_err_once(dev, "MSC is not accessible from any CPU!");
1360 		return ERR_PTR(-EINVAL);
1361 	}
1362 
1363 	err = mpam_msc_setup_error_irq(msc);
1364 	if (err)
1365 		return ERR_PTR(err);
1366 
1367 	if (device_property_read_u32(&pdev->dev, "pcc-channel", &tmp))
1368 		msc->iface = MPAM_IFACE_MMIO;
1369 	else
1370 		msc->iface = MPAM_IFACE_PCC;
1371 
1372 	if (msc->iface == MPAM_IFACE_MMIO) {
1373 		void __iomem *io;
1374 
1375 		io = devm_platform_get_and_ioremap_resource(pdev, 0,
1376 							    &msc_res);
1377 		if (IS_ERR(io)) {
1378 			dev_err_once(dev, "Failed to map MSC base address\n");
1379 			return ERR_CAST(io);
1380 		}
1381 		msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
1382 		msc->mapped_hwpage = io;
1383 	} else {
1384 		return ERR_PTR(-EINVAL);
1385 	}
1386 
1387 	list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
1388 	platform_set_drvdata(pdev, msc);
1389 
1390 	return msc;
1391 }
1392 
1393 static int fw_num_msc;
1394 
1395 static int mpam_msc_drv_probe(struct platform_device *pdev)
1396 {
1397 	int err;
1398 	struct mpam_msc *msc = NULL;
1399 	void *plat_data = pdev->dev.platform_data;
1400 
1401 	mutex_lock(&mpam_list_lock);
1402 	msc = do_mpam_msc_drv_probe(pdev);
1403 	mutex_unlock(&mpam_list_lock);
1404 
1405 	if (IS_ERR(msc))
1406 		return PTR_ERR(msc);
1407 
1408 	/* Create RIS entries described by firmware */
1409 	err = acpi_mpam_parse_resources(msc, plat_data);
1410 	if (err) {
1411 		mpam_msc_drv_remove(pdev);
1412 		return err;
1413 	}
1414 
1415 	if (atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
1416 		mpam_register_cpuhp_callbacks(mpam_discovery_cpu_online, NULL,
1417 					      "mpam:drv_probe");
1418 
1419 	return 0;
1420 }
1421 
1422 static struct platform_driver mpam_msc_driver = {
1423 	.driver = {
1424 		.name = "mpam_msc",
1425 	},
1426 	.probe = mpam_msc_drv_probe,
1427 	.remove = mpam_msc_drv_remove,
1428 };
1429 
1430 /* Any of these features mean the BWA_WD field is valid. */
1431 static bool mpam_has_bwa_wd_feature(struct mpam_props *props)
1432 {
1433 	if (mpam_has_feature(mpam_feat_mbw_min, props))
1434 		return true;
1435 	if (mpam_has_feature(mpam_feat_mbw_max, props))
1436 		return true;
1437 	if (mpam_has_feature(mpam_feat_mbw_prop, props))
1438 		return true;
1439 	return false;
1440 }
1441 
1442 /* Any of these features mean the CMAX_WD field is valid. */
1443 static bool mpam_has_cmax_wd_feature(struct mpam_props *props)
1444 {
1445 	if (mpam_has_feature(mpam_feat_cmax_cmax, props))
1446 		return true;
1447 	if (mpam_has_feature(mpam_feat_cmax_cmin, props))
1448 		return true;
1449 	return false;
1450 }
1451 
1452 #define MISMATCHED_HELPER(parent, child, helper, field, alias)		\
1453 	helper(parent) &&						\
1454 	((helper(child) && (parent)->field != (child)->field) ||	\
1455 	 (!helper(child) && !(alias)))
1456 
1457 #define MISMATCHED_FEAT(parent, child, feat, field, alias)		     \
1458 	mpam_has_feature((feat), (parent)) &&				     \
1459 	((mpam_has_feature((feat), (child)) && (parent)->field != (child)->field) || \
1460 	 (!mpam_has_feature((feat), (child)) && !(alias)))
1461 
1462 #define CAN_MERGE_FEAT(parent, child, feat, alias)			\
1463 	(alias) && !mpam_has_feature((feat), (parent)) &&		\
1464 	mpam_has_feature((feat), (child))
1465 
1466 /*
1467  * Combine two props fields.
1468  * If this is for controls that alias the same resource, it is safe to just
1469  * copy the values over. If two aliasing controls implement the same scheme
1470  * a safe value must be picked.
1471  * For non-aliasing controls, these control different resources, and the
1472  * resulting safe value must be compatible with both. When merging values in
1473  * the tree, all the aliasing resources must be handled first.
1474  * On mismatch, parent is modified.
1475  */
1476 static void __props_mismatch(struct mpam_props *parent,
1477 			     struct mpam_props *child, bool alias)
1478 {
1479 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_cpor_part, alias)) {
1480 		parent->cpbm_wd = child->cpbm_wd;
1481 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_cpor_part,
1482 				   cpbm_wd, alias)) {
1483 		pr_debug("cleared cpor_part\n");
1484 		mpam_clear_feature(mpam_feat_cpor_part, parent);
1485 		parent->cpbm_wd = 0;
1486 	}
1487 
1488 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_mbw_part, alias)) {
1489 		parent->mbw_pbm_bits = child->mbw_pbm_bits;
1490 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_mbw_part,
1491 				   mbw_pbm_bits, alias)) {
1492 		pr_debug("cleared mbw_part\n");
1493 		mpam_clear_feature(mpam_feat_mbw_part, parent);
1494 		parent->mbw_pbm_bits = 0;
1495 	}
1496 
1497 	/* bwa_wd is a count of bits, fewer bits means less precision */
1498 	if (alias && !mpam_has_bwa_wd_feature(parent) &&
1499 	    mpam_has_bwa_wd_feature(child)) {
1500 		parent->bwa_wd = child->bwa_wd;
1501 	} else if (MISMATCHED_HELPER(parent, child, mpam_has_bwa_wd_feature,
1502 				     bwa_wd, alias)) {
1503 		pr_debug("took the min bwa_wd\n");
1504 		parent->bwa_wd = min(parent->bwa_wd, child->bwa_wd);
1505 	}
1506 
1507 	if (alias && !mpam_has_cmax_wd_feature(parent) && mpam_has_cmax_wd_feature(child)) {
1508 		parent->cmax_wd = child->cmax_wd;
1509 	} else if (MISMATCHED_HELPER(parent, child, mpam_has_cmax_wd_feature,
1510 				     cmax_wd, alias)) {
1511 		pr_debug("%s took the min cmax_wd\n", __func__);
1512 		parent->cmax_wd = min(parent->cmax_wd, child->cmax_wd);
1513 	}
1514 
1515 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_cmax_cassoc, alias)) {
1516 		parent->cassoc_wd = child->cassoc_wd;
1517 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_cmax_cassoc,
1518 				   cassoc_wd, alias)) {
1519 		pr_debug("%s cleared cassoc_wd\n", __func__);
1520 		mpam_clear_feature(mpam_feat_cmax_cassoc, parent);
1521 		parent->cassoc_wd = 0;
1522 	}
1523 
1524 	/* For num properties, take the minimum */
1525 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_csu, alias)) {
1526 		parent->num_csu_mon = child->num_csu_mon;
1527 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_csu,
1528 				   num_csu_mon, alias)) {
1529 		pr_debug("took the min num_csu_mon\n");
1530 		parent->num_csu_mon = min(parent->num_csu_mon,
1531 					  child->num_csu_mon);
1532 	}
1533 
1534 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_mbwu, alias)) {
1535 		parent->num_mbwu_mon = child->num_mbwu_mon;
1536 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_mbwu,
1537 				   num_mbwu_mon, alias)) {
1538 		pr_debug("took the min num_mbwu_mon\n");
1539 		parent->num_mbwu_mon = min(parent->num_mbwu_mon,
1540 					   child->num_mbwu_mon);
1541 	}
1542 
1543 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_intpri_part, alias)) {
1544 		parent->intpri_wd = child->intpri_wd;
1545 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_intpri_part,
1546 				   intpri_wd, alias)) {
1547 		pr_debug("%s took the min intpri_wd\n", __func__);
1548 		parent->intpri_wd = min(parent->intpri_wd, child->intpri_wd);
1549 	}
1550 
1551 	if (CAN_MERGE_FEAT(parent, child, mpam_feat_dspri_part, alias)) {
1552 		parent->dspri_wd = child->dspri_wd;
1553 	} else if (MISMATCHED_FEAT(parent, child, mpam_feat_dspri_part,
1554 				   dspri_wd, alias)) {
1555 		pr_debug("%s took the min dspri_wd\n", __func__);
1556 		parent->dspri_wd = min(parent->dspri_wd, child->dspri_wd);
1557 	}
1558 
1559 	/* TODO: alias support for these two */
1560 	/* {int,ds}pri may not have differing 0-low behaviour */
1561 	if (mpam_has_feature(mpam_feat_intpri_part, parent) &&
1562 	    (!mpam_has_feature(mpam_feat_intpri_part, child) ||
1563 	     mpam_has_feature(mpam_feat_intpri_part_0_low, parent) !=
1564 	     mpam_has_feature(mpam_feat_intpri_part_0_low, child))) {
1565 		pr_debug("%s cleared intpri_part\n", __func__);
1566 		mpam_clear_feature(mpam_feat_intpri_part, parent);
1567 		mpam_clear_feature(mpam_feat_intpri_part_0_low, parent);
1568 	}
1569 	if (mpam_has_feature(mpam_feat_dspri_part, parent) &&
1570 	    (!mpam_has_feature(mpam_feat_dspri_part, child) ||
1571 	     mpam_has_feature(mpam_feat_dspri_part_0_low, parent) !=
1572 	     mpam_has_feature(mpam_feat_dspri_part_0_low, child))) {
1573 		pr_debug("%s cleared dspri_part\n", __func__);
1574 		mpam_clear_feature(mpam_feat_dspri_part, parent);
1575 		mpam_clear_feature(mpam_feat_dspri_part_0_low, parent);
1576 	}
1577 
1578 	if (alias) {
1579 		/* Merge features for aliased resources */
1580 		bitmap_or(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
1581 	} else {
1582 		/* Clear missing features for non aliasing */
1583 		bitmap_and(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
1584 	}
1585 }
1586 
1587 /*
1588  * If a vmsc doesn't match class feature/configuration, do the right thing(tm).
1589  * For 'num' properties we can just take the minimum.
1590  * For properties where the mismatched unused bits would make a difference, we
1591  * nobble the class feature, as we can't configure all the resources.
1592  * e.g. The L3 cache is composed of two resources with 13 and 17 portion
1593  * bitmaps respectively.
1594  */
1595 static void
1596 __class_props_mismatch(struct mpam_class *class, struct mpam_vmsc *vmsc)
1597 {
1598 	struct mpam_props *cprops = &class->props;
1599 	struct mpam_props *vprops = &vmsc->props;
1600 	struct device *dev = &vmsc->msc->pdev->dev;
1601 
1602 	lockdep_assert_held(&mpam_list_lock); /* we modify class */
1603 
1604 	dev_dbg(dev, "Merging features for class:0x%lx &= vmsc:0x%lx\n",
1605 		(long)cprops->features, (long)vprops->features);
1606 
1607 	/* Take the safe value for any common features */
1608 	__props_mismatch(cprops, vprops, false);
1609 }
1610 
1611 static void
1612 __vmsc_props_mismatch(struct mpam_vmsc *vmsc, struct mpam_msc_ris *ris)
1613 {
1614 	struct mpam_props *rprops = &ris->props;
1615 	struct mpam_props *vprops = &vmsc->props;
1616 	struct device *dev = &vmsc->msc->pdev->dev;
1617 
1618 	lockdep_assert_held(&mpam_list_lock); /* we modify vmsc */
1619 
1620 	dev_dbg(dev, "Merging features for vmsc:0x%lx |= ris:0x%lx\n",
1621 		(long)vprops->features, (long)rprops->features);
1622 
1623 	/*
1624 	 * Merge mismatched features - Copy any features that aren't common,
1625 	 * but take the safe value for any common features.
1626 	 */
1627 	__props_mismatch(vprops, rprops, true);
1628 }
1629 
1630 /*
1631  * Copy the first component's first vMSC's properties and features to the
1632  * class. __class_props_mismatch() will remove conflicts.
1633  * It is not possible to have a class with no components, or a component with
1634  * no resources. The vMSC properties have already been built.
1635  */
1636 static void mpam_enable_init_class_features(struct mpam_class *class)
1637 {
1638 	struct mpam_vmsc *vmsc;
1639 	struct mpam_component *comp;
1640 
1641 	comp = list_first_entry(&class->components,
1642 				struct mpam_component, class_list);
1643 	vmsc = list_first_entry(&comp->vmsc,
1644 				struct mpam_vmsc, comp_list);
1645 
1646 	class->props = vmsc->props;
1647 }
1648 
1649 static void mpam_enable_merge_vmsc_features(struct mpam_component *comp)
1650 {
1651 	struct mpam_vmsc *vmsc;
1652 	struct mpam_msc_ris *ris;
1653 	struct mpam_class *class = comp->class;
1654 
1655 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
1656 		list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
1657 			__vmsc_props_mismatch(vmsc, ris);
1658 			class->nrdy_usec = max(class->nrdy_usec,
1659 					       vmsc->msc->nrdy_usec);
1660 		}
1661 	}
1662 }
1663 
1664 static void mpam_enable_merge_class_features(struct mpam_component *comp)
1665 {
1666 	struct mpam_vmsc *vmsc;
1667 	struct mpam_class *class = comp->class;
1668 
1669 	list_for_each_entry(vmsc, &comp->vmsc, comp_list)
1670 		__class_props_mismatch(class, vmsc);
1671 }
1672 
1673 /*
1674  * Merge all the common resource features into class.
1675  * vmsc features are bitwise-or'd together by mpam_enable_merge_vmsc_features()
1676  * as the first step so that mpam_enable_init_class_features() can initialise
1677  * the class with a representative set of features.
1678  * Next the mpam_enable_merge_class_features() bitwise-and's all the vmsc
1679  * features to form the class features.
1680  * Other features are the min/max as appropriate.
1681  *
1682  * To avoid walking the whole tree twice, the class->nrdy_usec property is
1683  * updated when working with the vmsc as it is a max(), and doesn't need
1684  * initialising first.
1685  */
1686 static void mpam_enable_merge_features(struct list_head *all_classes_list)
1687 {
1688 	struct mpam_class *class;
1689 	struct mpam_component *comp;
1690 
1691 	lockdep_assert_held(&mpam_list_lock);
1692 
1693 	list_for_each_entry(class, all_classes_list, classes_list) {
1694 		list_for_each_entry(comp, &class->components, class_list)
1695 			mpam_enable_merge_vmsc_features(comp);
1696 
1697 		mpam_enable_init_class_features(class);
1698 
1699 		list_for_each_entry(comp, &class->components, class_list)
1700 			mpam_enable_merge_class_features(comp);
1701 	}
1702 }
1703 
1704 static char *mpam_errcode_names[16] = {
1705 	[MPAM_ERRCODE_NONE]			= "No error",
1706 	[MPAM_ERRCODE_PARTID_SEL_RANGE]		= "PARTID_SEL_Range",
1707 	[MPAM_ERRCODE_REQ_PARTID_RANGE]		= "Req_PARTID_Range",
1708 	[MPAM_ERRCODE_MSMONCFG_ID_RANGE]	= "MSMONCFG_ID_RANGE",
1709 	[MPAM_ERRCODE_REQ_PMG_RANGE]		= "Req_PMG_Range",
1710 	[MPAM_ERRCODE_MONITOR_RANGE]		= "Monitor_Range",
1711 	[MPAM_ERRCODE_INTPARTID_RANGE]		= "intPARTID_Range",
1712 	[MPAM_ERRCODE_UNEXPECTED_INTERNAL]	= "Unexpected_INTERNAL",
1713 	[MPAM_ERRCODE_UNDEFINED_RIS_PART_SEL]	= "Undefined_RIS_PART_SEL",
1714 	[MPAM_ERRCODE_RIS_NO_CONTROL]		= "RIS_No_Control",
1715 	[MPAM_ERRCODE_UNDEFINED_RIS_MON_SEL]	= "Undefined_RIS_MON_SEL",
1716 	[MPAM_ERRCODE_RIS_NO_MONITOR]		= "RIS_No_Monitor",
1717 	[12 ... 15] = "Reserved"
1718 };
1719 
1720 static int mpam_enable_msc_ecr(void *_msc)
1721 {
1722 	struct mpam_msc *msc = _msc;
1723 
1724 	__mpam_write_reg(msc, MPAMF_ECR, MPAMF_ECR_INTEN);
1725 
1726 	return 0;
1727 }
1728 
1729 /* This can run in mpam_disable(), and the interrupt handler on the same CPU */
1730 static int mpam_disable_msc_ecr(void *_msc)
1731 {
1732 	struct mpam_msc *msc = _msc;
1733 
1734 	__mpam_write_reg(msc, MPAMF_ECR, 0);
1735 
1736 	return 0;
1737 }
1738 
1739 static irqreturn_t __mpam_irq_handler(int irq, struct mpam_msc *msc)
1740 {
1741 	u64 reg;
1742 	u16 partid;
1743 	u8 errcode, pmg, ris;
1744 
1745 	if (WARN_ON_ONCE(!msc) ||
1746 	    WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(),
1747 					   &msc->accessibility)))
1748 		return IRQ_NONE;
1749 
1750 	reg = mpam_msc_read_esr(msc);
1751 
1752 	errcode = FIELD_GET(MPAMF_ESR_ERRCODE, reg);
1753 	if (!errcode)
1754 		return IRQ_NONE;
1755 
1756 	/* Clear level triggered irq */
1757 	mpam_msc_clear_esr(msc);
1758 
1759 	partid = FIELD_GET(MPAMF_ESR_PARTID_MON, reg);
1760 	pmg = FIELD_GET(MPAMF_ESR_PMG, reg);
1761 	ris = FIELD_GET(MPAMF_ESR_RIS, reg);
1762 
1763 	pr_err_ratelimited("error irq from msc:%u '%s', partid:%u, pmg: %u, ris: %u\n",
1764 			   msc->id, mpam_errcode_names[errcode], partid, pmg,
1765 			   ris);
1766 
1767 	/* Disable this interrupt. */
1768 	mpam_disable_msc_ecr(msc);
1769 
1770 	/* Are we racing with the thread disabling MPAM? */
1771 	if (!mpam_is_enabled())
1772 		return IRQ_HANDLED;
1773 
1774 	/*
1775 	 * Schedule the teardown work. Don't use a threaded IRQ as we can't
1776 	 * unregister the interrupt from the threaded part of the handler.
1777 	 */
1778 	mpam_disable_reason = "hardware error interrupt";
1779 	schedule_work(&mpam_broken_work);
1780 
1781 	return IRQ_HANDLED;
1782 }
1783 
1784 static irqreturn_t mpam_ppi_handler(int irq, void *dev_id)
1785 {
1786 	struct mpam_msc *msc = *(struct mpam_msc **)dev_id;
1787 
1788 	return __mpam_irq_handler(irq, msc);
1789 }
1790 
1791 static irqreturn_t mpam_spi_handler(int irq, void *dev_id)
1792 {
1793 	struct mpam_msc *msc = dev_id;
1794 
1795 	return __mpam_irq_handler(irq, msc);
1796 }
1797 
1798 static int mpam_register_irqs(void)
1799 {
1800 	int err, irq;
1801 	struct mpam_msc *msc;
1802 
1803 	lockdep_assert_cpus_held();
1804 
1805 	guard(srcu)(&mpam_srcu);
1806 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1807 				 srcu_read_lock_held(&mpam_srcu)) {
1808 		irq = platform_get_irq_byname_optional(msc->pdev, "error");
1809 		if (irq <= 0)
1810 			continue;
1811 
1812 		/* The MPAM spec says the interrupt can be SPI, PPI or LPI */
1813 		/* We anticipate sharing the interrupt with other MSCs */
1814 		if (irq_is_percpu(irq)) {
1815 			err = request_percpu_irq(irq, &mpam_ppi_handler,
1816 						 "mpam:msc:error",
1817 						 msc->error_dev_id);
1818 			if (err)
1819 				return err;
1820 
1821 			msc->reenable_error_ppi = irq;
1822 			smp_call_function_many(&msc->accessibility,
1823 					       &_enable_percpu_irq, &irq,
1824 					       true);
1825 		} else {
1826 			err = devm_request_irq(&msc->pdev->dev, irq,
1827 					       &mpam_spi_handler, IRQF_SHARED,
1828 					       "mpam:msc:error", msc);
1829 			if (err)
1830 				return err;
1831 		}
1832 
1833 		mutex_lock(&msc->error_irq_lock);
1834 		msc->error_irq_req = true;
1835 		mpam_touch_msc(msc, mpam_enable_msc_ecr, msc);
1836 		msc->error_irq_hw_enabled = true;
1837 		mutex_unlock(&msc->error_irq_lock);
1838 	}
1839 
1840 	return 0;
1841 }
1842 
1843 static void mpam_unregister_irqs(void)
1844 {
1845 	int irq;
1846 	struct mpam_msc *msc;
1847 
1848 	guard(cpus_read_lock)();
1849 	guard(srcu)(&mpam_srcu);
1850 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
1851 				 srcu_read_lock_held(&mpam_srcu)) {
1852 		irq = platform_get_irq_byname_optional(msc->pdev, "error");
1853 		if (irq <= 0)
1854 			continue;
1855 
1856 		mutex_lock(&msc->error_irq_lock);
1857 		if (msc->error_irq_hw_enabled) {
1858 			mpam_touch_msc(msc, mpam_disable_msc_ecr, msc);
1859 			msc->error_irq_hw_enabled = false;
1860 		}
1861 
1862 		if (msc->error_irq_req) {
1863 			if (irq_is_percpu(irq)) {
1864 				msc->reenable_error_ppi = 0;
1865 				free_percpu_irq(irq, msc->error_dev_id);
1866 			} else {
1867 				devm_free_irq(&msc->pdev->dev, irq, msc);
1868 			}
1869 			msc->error_irq_req = false;
1870 		}
1871 		mutex_unlock(&msc->error_irq_lock);
1872 	}
1873 }
1874 
1875 static void __destroy_component_cfg(struct mpam_component *comp)
1876 {
1877 	add_to_garbage(comp->cfg);
1878 }
1879 
1880 static void mpam_reset_component_cfg(struct mpam_component *comp)
1881 {
1882 	int i;
1883 	struct mpam_props *cprops = &comp->class->props;
1884 
1885 	mpam_assert_partid_sizes_fixed();
1886 
1887 	if (!comp->cfg)
1888 		return;
1889 
1890 	for (i = 0; i <= mpam_partid_max; i++) {
1891 		comp->cfg[i] = (struct mpam_config) {};
1892 		if (cprops->cpbm_wd)
1893 			comp->cfg[i].cpbm = GENMASK(cprops->cpbm_wd - 1, 0);
1894 		if (cprops->mbw_pbm_bits)
1895 			comp->cfg[i].mbw_pbm = GENMASK(cprops->mbw_pbm_bits - 1, 0);
1896 		if (cprops->bwa_wd)
1897 			comp->cfg[i].mbw_max = GENMASK(15, 16 - cprops->bwa_wd);
1898 	}
1899 }
1900 
1901 static int __allocate_component_cfg(struct mpam_component *comp)
1902 {
1903 	mpam_assert_partid_sizes_fixed();
1904 
1905 	if (comp->cfg)
1906 		return 0;
1907 
1908 	comp->cfg = kcalloc(mpam_partid_max + 1, sizeof(*comp->cfg), GFP_KERNEL);
1909 	if (!comp->cfg)
1910 		return -ENOMEM;
1911 
1912 	/*
1913 	 * The array is free()d in one go, so only cfg[0]'s structure needs
1914 	 * to be initialised.
1915 	 */
1916 	init_garbage(&comp->cfg[0].garbage);
1917 
1918 	mpam_reset_component_cfg(comp);
1919 
1920 	return 0;
1921 }
1922 
1923 static int mpam_allocate_config(void)
1924 {
1925 	struct mpam_class *class;
1926 	struct mpam_component *comp;
1927 
1928 	lockdep_assert_held(&mpam_list_lock);
1929 
1930 	list_for_each_entry(class, &mpam_classes, classes_list) {
1931 		list_for_each_entry(comp, &class->components, class_list) {
1932 			int err = __allocate_component_cfg(comp);
1933 			if (err)
1934 				return err;
1935 		}
1936 	}
1937 
1938 	return 0;
1939 }
1940 
1941 static void mpam_enable_once(void)
1942 {
1943 	int err;
1944 
1945 	/*
1946 	 * Once the cpuhp callbacks have been changed, mpam_partid_max can no
1947 	 * longer change.
1948 	 */
1949 	spin_lock(&partid_max_lock);
1950 	partid_max_published = true;
1951 	spin_unlock(&partid_max_lock);
1952 
1953 	/*
1954 	 * If all the MSC have been probed, enabling the IRQs happens next.
1955 	 * That involves cross-calling to a CPU that can reach the MSC, and
1956 	 * the locks must be taken in this order:
1957 	 */
1958 	cpus_read_lock();
1959 	mutex_lock(&mpam_list_lock);
1960 	do {
1961 		mpam_enable_merge_features(&mpam_classes);
1962 
1963 		err = mpam_register_irqs();
1964 		if (err) {
1965 			pr_warn("Failed to register irqs: %d\n", err);
1966 			break;
1967 		}
1968 
1969 		err = mpam_allocate_config();
1970 		if (err) {
1971 			pr_err("Failed to allocate configuration arrays.\n");
1972 			break;
1973 		}
1974 	} while (0);
1975 	mutex_unlock(&mpam_list_lock);
1976 	cpus_read_unlock();
1977 
1978 	if (err) {
1979 		mpam_disable_reason = "Failed to enable.";
1980 		schedule_work(&mpam_broken_work);
1981 		return;
1982 	}
1983 
1984 	static_branch_enable(&mpam_enabled);
1985 	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline,
1986 				      "mpam:online");
1987 
1988 	/* Use printk() to avoid the pr_fmt adding the function name. */
1989 	printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n",
1990 	       mpam_partid_max + 1, mpam_pmg_max + 1);
1991 }
1992 
1993 static void mpam_reset_component_locked(struct mpam_component *comp)
1994 {
1995 	struct mpam_vmsc *vmsc;
1996 
1997 	lockdep_assert_cpus_held();
1998 	mpam_assert_partid_sizes_fixed();
1999 
2000 	mpam_reset_component_cfg(comp);
2001 
2002 	guard(srcu)(&mpam_srcu);
2003 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
2004 				 srcu_read_lock_held(&mpam_srcu)) {
2005 		struct mpam_msc *msc = vmsc->msc;
2006 		struct mpam_msc_ris *ris;
2007 
2008 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
2009 					 srcu_read_lock_held(&mpam_srcu)) {
2010 			if (!ris->in_reset_state)
2011 				mpam_touch_msc(msc, mpam_reset_ris, ris);
2012 			ris->in_reset_state = true;
2013 		}
2014 	}
2015 }
2016 
2017 static void mpam_reset_class_locked(struct mpam_class *class)
2018 {
2019 	struct mpam_component *comp;
2020 
2021 	lockdep_assert_cpus_held();
2022 
2023 	guard(srcu)(&mpam_srcu);
2024 	list_for_each_entry_srcu(comp, &class->components, class_list,
2025 				 srcu_read_lock_held(&mpam_srcu))
2026 		mpam_reset_component_locked(comp);
2027 }
2028 
2029 static void mpam_reset_class(struct mpam_class *class)
2030 {
2031 	cpus_read_lock();
2032 	mpam_reset_class_locked(class);
2033 	cpus_read_unlock();
2034 }
2035 
2036 /*
2037  * Called in response to an error IRQ.
2038  * All of MPAMs errors indicate a software bug, restore any modified
2039  * controls to their reset values.
2040  */
2041 void mpam_disable(struct work_struct *ignored)
2042 {
2043 	int idx;
2044 	struct mpam_class *class;
2045 	struct mpam_msc *msc, *tmp;
2046 
2047 	mutex_lock(&mpam_cpuhp_state_lock);
2048 	if (mpam_cpuhp_state) {
2049 		cpuhp_remove_state(mpam_cpuhp_state);
2050 		mpam_cpuhp_state = 0;
2051 	}
2052 	mutex_unlock(&mpam_cpuhp_state_lock);
2053 
2054 	static_branch_disable(&mpam_enabled);
2055 
2056 	mpam_unregister_irqs();
2057 
2058 	idx = srcu_read_lock(&mpam_srcu);
2059 	list_for_each_entry_srcu(class, &mpam_classes, classes_list,
2060 				 srcu_read_lock_held(&mpam_srcu))
2061 		mpam_reset_class(class);
2062 	srcu_read_unlock(&mpam_srcu, idx);
2063 
2064 	mutex_lock(&mpam_list_lock);
2065 	list_for_each_entry_safe(msc, tmp, &mpam_all_msc, all_msc_list)
2066 		mpam_msc_destroy(msc);
2067 	mutex_unlock(&mpam_list_lock);
2068 	mpam_free_garbage();
2069 
2070 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
2071 }
2072 
2073 /*
2074  * Enable mpam once all devices have been probed.
2075  * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
2076  * Also scheduled when new devices are probed when new CPUs come online.
2077  */
2078 void mpam_enable(struct work_struct *work)
2079 {
2080 	static atomic_t once;
2081 	struct mpam_msc *msc;
2082 	bool all_devices_probed = true;
2083 
2084 	/* Have we probed all the hw devices? */
2085 	guard(srcu)(&mpam_srcu);
2086 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
2087 				 srcu_read_lock_held(&mpam_srcu)) {
2088 		mutex_lock(&msc->probe_lock);
2089 		if (!msc->probed)
2090 			all_devices_probed = false;
2091 		mutex_unlock(&msc->probe_lock);
2092 
2093 		if (!all_devices_probed)
2094 			break;
2095 	}
2096 
2097 	if (all_devices_probed && !atomic_fetch_inc(&once))
2098 		mpam_enable_once();
2099 }
2100 
2101 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
2102 	if (mpam_has_feature(feature, newcfg) &&			\
2103 	    (newcfg)->member != (cfg)->member) {			\
2104 		(cfg)->member = (newcfg)->member;			\
2105 		mpam_set_feature(feature, cfg);				\
2106 									\
2107 		(changes) = true;					\
2108 	}								\
2109 } while (0)
2110 
2111 static bool mpam_update_config(struct mpam_config *cfg,
2112 			       const struct mpam_config *newcfg)
2113 {
2114 	bool has_changes = false;
2115 
2116 	maybe_update_config(cfg, mpam_feat_cpor_part, newcfg, cpbm, has_changes);
2117 	maybe_update_config(cfg, mpam_feat_mbw_part, newcfg, mbw_pbm, has_changes);
2118 	maybe_update_config(cfg, mpam_feat_mbw_max, newcfg, mbw_max, has_changes);
2119 
2120 	return has_changes;
2121 }
2122 
2123 int mpam_apply_config(struct mpam_component *comp, u16 partid,
2124 		      struct mpam_config *cfg)
2125 {
2126 	struct mpam_write_config_arg arg;
2127 	struct mpam_msc_ris *ris;
2128 	struct mpam_vmsc *vmsc;
2129 	struct mpam_msc *msc;
2130 
2131 	lockdep_assert_cpus_held();
2132 
2133 	/* Don't pass in the current config! */
2134 	WARN_ON_ONCE(&comp->cfg[partid] == cfg);
2135 
2136 	if (!mpam_update_config(&comp->cfg[partid], cfg))
2137 		return 0;
2138 
2139 	arg.comp = comp;
2140 	arg.partid = partid;
2141 
2142 	guard(srcu)(&mpam_srcu);
2143 	list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
2144 				 srcu_read_lock_held(&mpam_srcu)) {
2145 		msc = vmsc->msc;
2146 
2147 		mutex_lock(&msc->cfg_lock);
2148 		list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
2149 					 srcu_read_lock_held(&mpam_srcu)) {
2150 			arg.ris = ris;
2151 			mpam_touch_msc(msc, __write_config, &arg);
2152 		}
2153 		mutex_unlock(&msc->cfg_lock);
2154 	}
2155 
2156 	return 0;
2157 }
2158 
2159 static int __init mpam_msc_driver_init(void)
2160 {
2161 	if (!system_supports_mpam())
2162 		return -EOPNOTSUPP;
2163 
2164 	init_srcu_struct(&mpam_srcu);
2165 
2166 	fw_num_msc = acpi_mpam_count_msc();
2167 	if (fw_num_msc <= 0) {
2168 		pr_err("No MSC devices found in firmware\n");
2169 		return -EINVAL;
2170 	}
2171 
2172 	return platform_driver_register(&mpam_msc_driver);
2173 }
2174 
2175 /* Must occur after arm64_mpam_register_cpus() from arch_initcall() */
2176 subsys_initcall(mpam_msc_driver_init);
2177