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