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