xref: /linux/drivers/resctrl/mpam_devices.c (revision bd221f9f82afb616887e0b88b43fbb937479d744)
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/cacheinfo.h>
11 #include <linux/cpu.h>
12 #include <linux/cpumask.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/gfp.h>
16 #include <linux/list.h>
17 #include <linux/lockdep.h>
18 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/printk.h>
21 #include <linux/srcu.h>
22 #include <linux/types.h>
23 #include <linux/workqueue.h>
24 
25 #include "mpam_internal.h"
26 
27 /*
28  * mpam_list_lock protects the SRCU lists when writing. Once the
29  * mpam_enabled key is enabled these lists are read-only,
30  * unless the error interrupt disables the driver.
31  */
32 static DEFINE_MUTEX(mpam_list_lock);
33 static LIST_HEAD(mpam_all_msc);
34 
35 struct srcu_struct mpam_srcu;
36 
37 /*
38  * Number of MSCs that have been probed. Once all MSCs have been probed MPAM
39  * can be enabled.
40  */
41 static atomic_t mpam_num_msc;
42 
43 static int mpam_cpuhp_state;
44 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
45 
46 /*
47  * The smallest common values for any CPU or MSC in the system.
48  * Generating traffic outside this range will result in screaming interrupts.
49  */
50 u16 mpam_partid_max;
51 u8 mpam_pmg_max;
52 static bool partid_max_init, partid_max_published;
53 static DEFINE_SPINLOCK(partid_max_lock);
54 
55 /*
56  * mpam is enabled once all devices have been probed from CPU online callbacks,
57  * scheduled via this work_struct. If access to an MSC depends on a CPU that
58  * was not brought online at boot, this can happen surprisingly late.
59  */
60 static DECLARE_WORK(mpam_enable_work, &mpam_enable);
61 
62 /*
63  * All mpam error interrupts indicate a software bug. On receipt, disable the
64  * driver.
65  */
66 static DECLARE_WORK(mpam_broken_work, &mpam_disable);
67 
68 /* When mpam is disabled, the printed reason to aid debugging */
69 static char *mpam_disable_reason;
70 
71 /*
72  * An MSC is a physical container for controls and monitors, each identified by
73  * their RIS index. These share a base-address, interrupts and some MMIO
74  * registers. A vMSC is a virtual container for RIS in an MSC that control or
75  * monitor the same thing. Members of a vMSC are all RIS in the same MSC, but
76  * not all RIS in an MSC share a vMSC.
77  *
78  * Components are a group of vMSC that control or monitor the same thing but
79  * are from different MSC, so have different base-address, interrupts etc.
80  * Classes are the set components of the same type.
81  *
82  * The features of a vMSC is the union of the RIS it contains.
83  * The features of a Class and Component are the common subset of the vMSC
84  * they contain.
85  *
86  * e.g. The system cache may have bandwidth controls on multiple interfaces,
87  * for regulating traffic from devices independently of traffic from CPUs.
88  * If these are two RIS in one MSC, they will be treated as controlling
89  * different things, and will not share a vMSC/component/class.
90  *
91  * e.g. The L2 may have one MSC and two RIS, one for cache-controls another
92  * for bandwidth. These two RIS are members of the same vMSC.
93  *
94  * e.g. The set of RIS that make up the L2 are grouped as a component. These
95  * are sometimes termed slices. They should be configured the same, as if there
96  * were only one.
97  *
98  * e.g. The SoC probably has more than one L2, each attached to a distinct set
99  * of CPUs. All the L2 components are grouped as a class.
100  *
101  * When creating an MSC, struct mpam_msc is added to the all mpam_all_msc list,
102  * then linked via struct mpam_ris to a vmsc, component and class.
103  * The same MSC may exist under different class->component->vmsc paths, but the
104  * RIS index will be unique.
105  */
106 LIST_HEAD(mpam_classes);
107 
108 /* List of all objects that can be free()d after synchronise_srcu() */
109 static LLIST_HEAD(mpam_garbage);
110 
111 static inline void init_garbage(struct mpam_garbage *garbage)
112 {
113 	init_llist_node(&garbage->llist);
114 }
115 
116 #define add_to_garbage(x)				\
117 do {							\
118 	__typeof__(x) _x = (x);				\
119 	_x->garbage.to_free = _x;			\
120 	llist_add(&_x->garbage.llist, &mpam_garbage);	\
121 } while (0)
122 
123 static void mpam_free_garbage(void)
124 {
125 	struct mpam_garbage *iter, *tmp;
126 	struct llist_node *to_free = llist_del_all(&mpam_garbage);
127 
128 	if (!to_free)
129 		return;
130 
131 	synchronize_srcu(&mpam_srcu);
132 
133 	llist_for_each_entry_safe(iter, tmp, to_free, llist) {
134 		if (iter->pdev)
135 			devm_kfree(&iter->pdev->dev, iter->to_free);
136 		else
137 			kfree(iter->to_free);
138 	}
139 }
140 
141 static u32 __mpam_read_reg(struct mpam_msc *msc, u16 reg)
142 {
143 	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
144 
145 	return readl_relaxed(msc->mapped_hwpage + reg);
146 }
147 
148 static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg)
149 {
150 	lockdep_assert_held_once(&msc->part_sel_lock);
151 	return __mpam_read_reg(msc, reg);
152 }
153 
154 #define mpam_read_partsel_reg(msc, reg) _mpam_read_partsel_reg(msc, MPAMF_##reg)
155 
156 static void __mpam_write_reg(struct mpam_msc *msc, u16 reg, u32 val)
157 {
158 	WARN_ON_ONCE(reg + sizeof(u32) > msc->mapped_hwpage_sz);
159 	WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
160 
161 	writel_relaxed(val, msc->mapped_hwpage + reg);
162 }
163 
164 static inline void _mpam_write_partsel_reg(struct mpam_msc *msc, u16 reg, u32 val)
165 {
166 	lockdep_assert_held_once(&msc->part_sel_lock);
167 	__mpam_write_reg(msc, reg, val);
168 }
169 
170 #define mpam_write_partsel_reg(msc, reg, val)  _mpam_write_partsel_reg(msc, MPAMCFG_##reg, val)
171 
172 static u64 mpam_msc_read_idr(struct mpam_msc *msc)
173 {
174 	u64 idr_high = 0, idr_low;
175 
176 	lockdep_assert_held(&msc->part_sel_lock);
177 
178 	idr_low = mpam_read_partsel_reg(msc, IDR);
179 	if (FIELD_GET(MPAMF_IDR_EXT, idr_low))
180 		idr_high = mpam_read_partsel_reg(msc, IDR + 4);
181 
182 	return (idr_high << 32) | idr_low;
183 }
184 
185 static void __mpam_part_sel_raw(u32 partsel, struct mpam_msc *msc)
186 {
187 	lockdep_assert_held(&msc->part_sel_lock);
188 
189 	mpam_write_partsel_reg(msc, PART_SEL, partsel);
190 }
191 
192 static void __mpam_part_sel(u8 ris_idx, u16 partid, struct mpam_msc *msc)
193 {
194 	u32 partsel = FIELD_PREP(MPAMCFG_PART_SEL_RIS, ris_idx) |
195 		      FIELD_PREP(MPAMCFG_PART_SEL_PARTID_SEL, partid);
196 
197 	__mpam_part_sel_raw(partsel, msc);
198 }
199 
200 int mpam_register_requestor(u16 partid_max, u8 pmg_max)
201 {
202 	guard(spinlock)(&partid_max_lock);
203 	if (!partid_max_init) {
204 		mpam_partid_max = partid_max;
205 		mpam_pmg_max = pmg_max;
206 		partid_max_init = true;
207 	} else if (!partid_max_published) {
208 		mpam_partid_max = min(mpam_partid_max, partid_max);
209 		mpam_pmg_max = min(mpam_pmg_max, pmg_max);
210 	} else {
211 		/* New requestors can't lower the values */
212 		if (partid_max < mpam_partid_max || pmg_max < mpam_pmg_max)
213 			return -EBUSY;
214 	}
215 
216 	return 0;
217 }
218 EXPORT_SYMBOL(mpam_register_requestor);
219 
220 static struct mpam_class *
221 mpam_class_alloc(u8 level_idx, enum mpam_class_types type)
222 {
223 	struct mpam_class *class;
224 
225 	lockdep_assert_held(&mpam_list_lock);
226 
227 	class = kzalloc(sizeof(*class), GFP_KERNEL);
228 	if (!class)
229 		return ERR_PTR(-ENOMEM);
230 	init_garbage(&class->garbage);
231 
232 	INIT_LIST_HEAD_RCU(&class->components);
233 	/* Affinity is updated when ris are added */
234 	class->level = level_idx;
235 	class->type = type;
236 	INIT_LIST_HEAD_RCU(&class->classes_list);
237 
238 	list_add_rcu(&class->classes_list, &mpam_classes);
239 
240 	return class;
241 }
242 
243 static void mpam_class_destroy(struct mpam_class *class)
244 {
245 	lockdep_assert_held(&mpam_list_lock);
246 
247 	list_del_rcu(&class->classes_list);
248 	add_to_garbage(class);
249 }
250 
251 static struct mpam_class *
252 mpam_class_find(u8 level_idx, enum mpam_class_types type)
253 {
254 	struct mpam_class *class;
255 
256 	lockdep_assert_held(&mpam_list_lock);
257 
258 	list_for_each_entry(class, &mpam_classes, classes_list) {
259 		if (class->type == type && class->level == level_idx)
260 			return class;
261 	}
262 
263 	return mpam_class_alloc(level_idx, type);
264 }
265 
266 static struct mpam_component *
267 mpam_component_alloc(struct mpam_class *class, int id)
268 {
269 	struct mpam_component *comp;
270 
271 	lockdep_assert_held(&mpam_list_lock);
272 
273 	comp = kzalloc(sizeof(*comp), GFP_KERNEL);
274 	if (!comp)
275 		return ERR_PTR(-ENOMEM);
276 	init_garbage(&comp->garbage);
277 
278 	comp->comp_id = id;
279 	INIT_LIST_HEAD_RCU(&comp->vmsc);
280 	/* Affinity is updated when RIS are added */
281 	INIT_LIST_HEAD_RCU(&comp->class_list);
282 	comp->class = class;
283 
284 	list_add_rcu(&comp->class_list, &class->components);
285 
286 	return comp;
287 }
288 
289 static void mpam_component_destroy(struct mpam_component *comp)
290 {
291 	struct mpam_class *class = comp->class;
292 
293 	lockdep_assert_held(&mpam_list_lock);
294 
295 	list_del_rcu(&comp->class_list);
296 	add_to_garbage(comp);
297 
298 	if (list_empty(&class->components))
299 		mpam_class_destroy(class);
300 }
301 
302 static struct mpam_component *
303 mpam_component_find(struct mpam_class *class, int id)
304 {
305 	struct mpam_component *comp;
306 
307 	lockdep_assert_held(&mpam_list_lock);
308 
309 	list_for_each_entry(comp, &class->components, class_list) {
310 		if (comp->comp_id == id)
311 			return comp;
312 	}
313 
314 	return mpam_component_alloc(class, id);
315 }
316 
317 static struct mpam_vmsc *
318 mpam_vmsc_alloc(struct mpam_component *comp, struct mpam_msc *msc)
319 {
320 	struct mpam_vmsc *vmsc;
321 
322 	lockdep_assert_held(&mpam_list_lock);
323 
324 	vmsc = kzalloc(sizeof(*vmsc), GFP_KERNEL);
325 	if (!vmsc)
326 		return ERR_PTR(-ENOMEM);
327 	init_garbage(&vmsc->garbage);
328 
329 	INIT_LIST_HEAD_RCU(&vmsc->ris);
330 	INIT_LIST_HEAD_RCU(&vmsc->comp_list);
331 	vmsc->comp = comp;
332 	vmsc->msc = msc;
333 
334 	list_add_rcu(&vmsc->comp_list, &comp->vmsc);
335 
336 	return vmsc;
337 }
338 
339 static void mpam_vmsc_destroy(struct mpam_vmsc *vmsc)
340 {
341 	struct mpam_component *comp = vmsc->comp;
342 
343 	lockdep_assert_held(&mpam_list_lock);
344 
345 	list_del_rcu(&vmsc->comp_list);
346 	add_to_garbage(vmsc);
347 
348 	if (list_empty(&comp->vmsc))
349 		mpam_component_destroy(comp);
350 }
351 
352 static struct mpam_vmsc *
353 mpam_vmsc_find(struct mpam_component *comp, struct mpam_msc *msc)
354 {
355 	struct mpam_vmsc *vmsc;
356 
357 	lockdep_assert_held(&mpam_list_lock);
358 
359 	list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
360 		if (vmsc->msc->id == msc->id)
361 			return vmsc;
362 	}
363 
364 	return mpam_vmsc_alloc(comp, msc);
365 }
366 
367 /*
368  * The cacheinfo structures are only populated when CPUs are online.
369  * This helper walks the acpi tables to include offline CPUs too.
370  */
371 int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
372 				   cpumask_t *affinity)
373 {
374 	return acpi_pptt_get_cpumask_from_cache_id(cache_id, affinity);
375 }
376 
377 /*
378  * cpumask_of_node() only knows about online CPUs. This can't tell us whether
379  * a class is represented on all possible CPUs.
380  */
381 static void get_cpumask_from_node_id(u32 node_id, cpumask_t *affinity)
382 {
383 	int cpu;
384 
385 	for_each_possible_cpu(cpu) {
386 		if (node_id == cpu_to_node(cpu))
387 			cpumask_set_cpu(cpu, affinity);
388 	}
389 }
390 
391 static int mpam_ris_get_affinity(struct mpam_msc *msc, cpumask_t *affinity,
392 				 enum mpam_class_types type,
393 				 struct mpam_class *class,
394 				 struct mpam_component *comp)
395 {
396 	int err;
397 
398 	switch (type) {
399 	case MPAM_CLASS_CACHE:
400 		err = mpam_get_cpumask_from_cache_id(comp->comp_id, class->level,
401 						     affinity);
402 		if (err) {
403 			dev_warn_once(&msc->pdev->dev,
404 				      "Failed to determine CPU affinity\n");
405 			return err;
406 		}
407 
408 		if (cpumask_empty(affinity))
409 			dev_warn_once(&msc->pdev->dev, "no CPUs associated with cache node\n");
410 
411 		break;
412 	case MPAM_CLASS_MEMORY:
413 		get_cpumask_from_node_id(comp->comp_id, affinity);
414 		/* affinity may be empty for CPU-less memory nodes */
415 		break;
416 	case MPAM_CLASS_UNKNOWN:
417 		return 0;
418 	}
419 
420 	cpumask_and(affinity, affinity, &msc->accessibility);
421 
422 	return 0;
423 }
424 
425 static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
426 				  enum mpam_class_types type, u8 class_id,
427 				  int component_id)
428 {
429 	int err;
430 	struct mpam_vmsc *vmsc;
431 	struct mpam_msc_ris *ris;
432 	struct mpam_class *class;
433 	struct mpam_component *comp;
434 	struct platform_device *pdev = msc->pdev;
435 
436 	lockdep_assert_held(&mpam_list_lock);
437 
438 	if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
439 		return -EINVAL;
440 
441 	if (test_and_set_bit(ris_idx, &msc->ris_idxs))
442 		return -EBUSY;
443 
444 	ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL);
445 	if (!ris)
446 		return -ENOMEM;
447 	init_garbage(&ris->garbage);
448 	ris->garbage.pdev = pdev;
449 
450 	class = mpam_class_find(class_id, type);
451 	if (IS_ERR(class))
452 		return PTR_ERR(class);
453 
454 	comp = mpam_component_find(class, component_id);
455 	if (IS_ERR(comp)) {
456 		if (list_empty(&class->components))
457 			mpam_class_destroy(class);
458 		return PTR_ERR(comp);
459 	}
460 
461 	vmsc = mpam_vmsc_find(comp, msc);
462 	if (IS_ERR(vmsc)) {
463 		if (list_empty(&comp->vmsc))
464 			mpam_component_destroy(comp);
465 		return PTR_ERR(vmsc);
466 	}
467 
468 	err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
469 	if (err) {
470 		if (list_empty(&vmsc->ris))
471 			mpam_vmsc_destroy(vmsc);
472 		return err;
473 	}
474 
475 	ris->ris_idx = ris_idx;
476 	INIT_LIST_HEAD_RCU(&ris->msc_list);
477 	INIT_LIST_HEAD_RCU(&ris->vmsc_list);
478 	ris->vmsc = vmsc;
479 
480 	cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
481 	cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
482 	list_add_rcu(&ris->vmsc_list, &vmsc->ris);
483 	list_add_rcu(&ris->msc_list, &msc->ris);
484 
485 	return 0;
486 }
487 
488 static void mpam_ris_destroy(struct mpam_msc_ris *ris)
489 {
490 	struct mpam_vmsc *vmsc = ris->vmsc;
491 	struct mpam_msc *msc = vmsc->msc;
492 	struct mpam_component *comp = vmsc->comp;
493 	struct mpam_class *class = comp->class;
494 
495 	lockdep_assert_held(&mpam_list_lock);
496 
497 	/*
498 	 * It is assumed affinities don't overlap. If they do the class becomes
499 	 * unusable immediately.
500 	 */
501 	cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity);
502 	cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity);
503 	clear_bit(ris->ris_idx, &msc->ris_idxs);
504 	list_del_rcu(&ris->msc_list);
505 	list_del_rcu(&ris->vmsc_list);
506 	add_to_garbage(ris);
507 
508 	if (list_empty(&vmsc->ris))
509 		mpam_vmsc_destroy(vmsc);
510 }
511 
512 int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
513 		    enum mpam_class_types type, u8 class_id, int component_id)
514 {
515 	int err;
516 
517 	mutex_lock(&mpam_list_lock);
518 	err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
519 				     component_id);
520 	mutex_unlock(&mpam_list_lock);
521 	if (err)
522 		mpam_free_garbage();
523 
524 	return err;
525 }
526 
527 static struct mpam_msc_ris *mpam_get_or_create_ris(struct mpam_msc *msc,
528 						   u8 ris_idx)
529 {
530 	int err;
531 	struct mpam_msc_ris *ris;
532 
533 	lockdep_assert_held(&mpam_list_lock);
534 
535 	if (!test_bit(ris_idx, &msc->ris_idxs)) {
536 		err = mpam_ris_create_locked(msc, ris_idx, MPAM_CLASS_UNKNOWN,
537 					     0, 0);
538 		if (err)
539 			return ERR_PTR(err);
540 	}
541 
542 	list_for_each_entry(ris, &msc->ris, msc_list) {
543 		if (ris->ris_idx == ris_idx)
544 			return ris;
545 	}
546 
547 	return ERR_PTR(-ENOENT);
548 }
549 
550 static int mpam_msc_hw_probe(struct mpam_msc *msc)
551 {
552 	u64 idr;
553 	u16 partid_max;
554 	u8 ris_idx, pmg_max;
555 	struct mpam_msc_ris *ris;
556 	struct device *dev = &msc->pdev->dev;
557 
558 	lockdep_assert_held(&msc->probe_lock);
559 
560 	idr = __mpam_read_reg(msc, MPAMF_AIDR);
561 	if ((idr & MPAMF_AIDR_ARCH_MAJOR_REV) != MPAM_ARCHITECTURE_V1) {
562 		dev_err_once(dev, "MSC does not match MPAM architecture v1.x\n");
563 		return -EIO;
564 	}
565 
566 	/* Grab an IDR value to find out how many RIS there are */
567 	mutex_lock(&msc->part_sel_lock);
568 	idr = mpam_msc_read_idr(msc);
569 	mutex_unlock(&msc->part_sel_lock);
570 
571 	msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr);
572 
573 	/* Use these values so partid/pmg always starts with a valid value */
574 	msc->partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
575 	msc->pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
576 
577 	for (ris_idx = 0; ris_idx <= msc->ris_max; ris_idx++) {
578 		mutex_lock(&msc->part_sel_lock);
579 		__mpam_part_sel(ris_idx, 0, msc);
580 		idr = mpam_msc_read_idr(msc);
581 		mutex_unlock(&msc->part_sel_lock);
582 
583 		partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
584 		pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
585 		msc->partid_max = min(msc->partid_max, partid_max);
586 		msc->pmg_max = min(msc->pmg_max, pmg_max);
587 
588 		mutex_lock(&mpam_list_lock);
589 		ris = mpam_get_or_create_ris(msc, ris_idx);
590 		mutex_unlock(&mpam_list_lock);
591 		if (IS_ERR(ris))
592 			return PTR_ERR(ris);
593 	}
594 
595 	spin_lock(&partid_max_lock);
596 	mpam_partid_max = min(mpam_partid_max, msc->partid_max);
597 	mpam_pmg_max = min(mpam_pmg_max, msc->pmg_max);
598 	spin_unlock(&partid_max_lock);
599 
600 	msc->probed = true;
601 
602 	return 0;
603 }
604 
605 static int mpam_cpu_online(unsigned int cpu)
606 {
607 	return 0;
608 }
609 
610 /* Before mpam is enabled, try to probe new MSC */
611 static int mpam_discovery_cpu_online(unsigned int cpu)
612 {
613 	int err = 0;
614 	struct mpam_msc *msc;
615 	bool new_device_probed = false;
616 
617 	guard(srcu)(&mpam_srcu);
618 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
619 				 srcu_read_lock_held(&mpam_srcu)) {
620 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
621 			continue;
622 
623 		mutex_lock(&msc->probe_lock);
624 		if (!msc->probed)
625 			err = mpam_msc_hw_probe(msc);
626 		mutex_unlock(&msc->probe_lock);
627 
628 		if (err)
629 			break;
630 		new_device_probed = true;
631 	}
632 
633 	if (new_device_probed && !err)
634 		schedule_work(&mpam_enable_work);
635 	if (err) {
636 		mpam_disable_reason = "error during probing";
637 		schedule_work(&mpam_broken_work);
638 	}
639 
640 	return err;
641 }
642 
643 static int mpam_cpu_offline(unsigned int cpu)
644 {
645 	return 0;
646 }
647 
648 static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online),
649 					  int (*offline)(unsigned int offline),
650 					  char *name)
651 {
652 	mutex_lock(&mpam_cpuhp_state_lock);
653 	if (mpam_cpuhp_state) {
654 		cpuhp_remove_state(mpam_cpuhp_state);
655 		mpam_cpuhp_state = 0;
656 	}
657 
658 	mpam_cpuhp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, name, online,
659 					     offline);
660 	if (mpam_cpuhp_state <= 0) {
661 		pr_err("Failed to register cpuhp callbacks");
662 		mpam_cpuhp_state = 0;
663 	}
664 	mutex_unlock(&mpam_cpuhp_state_lock);
665 }
666 
667 /*
668  * An MSC can control traffic from a set of CPUs, but may only be accessible
669  * from a (hopefully wider) set of CPUs. The common reason for this is power
670  * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
671  * corresponding cache may also be powered off. By making accesses from
672  * one of those CPUs, we ensure we don't access a cache that's powered off.
673  */
674 static void update_msc_accessibility(struct mpam_msc *msc)
675 {
676 	u32 affinity_id;
677 	int err;
678 
679 	err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",
680 				       &affinity_id);
681 	if (err)
682 		cpumask_copy(&msc->accessibility, cpu_possible_mask);
683 	else
684 		acpi_pptt_get_cpus_from_container(affinity_id, &msc->accessibility);
685 }
686 
687 /*
688  * There are two ways of reaching a struct mpam_msc_ris. Via the
689  * class->component->vmsc->ris, or via the msc.
690  * When destroying the msc, the other side needs unlinking and cleaning up too.
691  */
692 static void mpam_msc_destroy(struct mpam_msc *msc)
693 {
694 	struct platform_device *pdev = msc->pdev;
695 	struct mpam_msc_ris *ris, *tmp;
696 
697 	lockdep_assert_held(&mpam_list_lock);
698 
699 	list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list)
700 		mpam_ris_destroy(ris);
701 
702 	list_del_rcu(&msc->all_msc_list);
703 	platform_set_drvdata(pdev, NULL);
704 
705 	add_to_garbage(msc);
706 }
707 
708 static void mpam_msc_drv_remove(struct platform_device *pdev)
709 {
710 	struct mpam_msc *msc = platform_get_drvdata(pdev);
711 
712 	mutex_lock(&mpam_list_lock);
713 	mpam_msc_destroy(msc);
714 	mutex_unlock(&mpam_list_lock);
715 
716 	mpam_free_garbage();
717 }
718 
719 static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
720 {
721 	int err;
722 	u32 tmp;
723 	struct mpam_msc *msc;
724 	struct resource *msc_res;
725 	struct device *dev = &pdev->dev;
726 
727 	lockdep_assert_held(&mpam_list_lock);
728 
729 	msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
730 	if (!msc)
731 		return ERR_PTR(-ENOMEM);
732 	init_garbage(&msc->garbage);
733 	msc->garbage.pdev = pdev;
734 
735 	err = devm_mutex_init(dev, &msc->probe_lock);
736 	if (err)
737 		return ERR_PTR(err);
738 
739 	err = devm_mutex_init(dev, &msc->part_sel_lock);
740 	if (err)
741 		return ERR_PTR(err);
742 
743 	msc->id = pdev->id;
744 	msc->pdev = pdev;
745 	INIT_LIST_HEAD_RCU(&msc->all_msc_list);
746 	INIT_LIST_HEAD_RCU(&msc->ris);
747 
748 	update_msc_accessibility(msc);
749 	if (cpumask_empty(&msc->accessibility)) {
750 		dev_err_once(dev, "MSC is not accessible from any CPU!");
751 		return ERR_PTR(-EINVAL);
752 	}
753 
754 	if (device_property_read_u32(&pdev->dev, "pcc-channel", &tmp))
755 		msc->iface = MPAM_IFACE_MMIO;
756 	else
757 		msc->iface = MPAM_IFACE_PCC;
758 
759 	if (msc->iface == MPAM_IFACE_MMIO) {
760 		void __iomem *io;
761 
762 		io = devm_platform_get_and_ioremap_resource(pdev, 0,
763 							    &msc_res);
764 		if (IS_ERR(io)) {
765 			dev_err_once(dev, "Failed to map MSC base address\n");
766 			return ERR_CAST(io);
767 		}
768 		msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
769 		msc->mapped_hwpage = io;
770 	} else {
771 		return ERR_PTR(-EINVAL);
772 	}
773 
774 	list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
775 	platform_set_drvdata(pdev, msc);
776 
777 	return msc;
778 }
779 
780 static int fw_num_msc;
781 
782 static int mpam_msc_drv_probe(struct platform_device *pdev)
783 {
784 	int err;
785 	struct mpam_msc *msc = NULL;
786 	void *plat_data = pdev->dev.platform_data;
787 
788 	mutex_lock(&mpam_list_lock);
789 	msc = do_mpam_msc_drv_probe(pdev);
790 	mutex_unlock(&mpam_list_lock);
791 
792 	if (IS_ERR(msc))
793 		return PTR_ERR(msc);
794 
795 	/* Create RIS entries described by firmware */
796 	err = acpi_mpam_parse_resources(msc, plat_data);
797 	if (err) {
798 		mpam_msc_drv_remove(pdev);
799 		return err;
800 	}
801 
802 	if (atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
803 		mpam_register_cpuhp_callbacks(mpam_discovery_cpu_online, NULL,
804 					      "mpam:drv_probe");
805 
806 	return 0;
807 }
808 
809 static struct platform_driver mpam_msc_driver = {
810 	.driver = {
811 		.name = "mpam_msc",
812 	},
813 	.probe = mpam_msc_drv_probe,
814 	.remove = mpam_msc_drv_remove,
815 };
816 
817 static void mpam_enable_once(void)
818 {
819 	/*
820 	 * Once the cpuhp callbacks have been changed, mpam_partid_max can no
821 	 * longer change.
822 	 */
823 	spin_lock(&partid_max_lock);
824 	partid_max_published = true;
825 	spin_unlock(&partid_max_lock);
826 
827 	mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline,
828 				      "mpam:online");
829 
830 	/* Use printk() to avoid the pr_fmt adding the function name. */
831 	printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n",
832 	       mpam_partid_max + 1, mpam_pmg_max + 1);
833 }
834 
835 void mpam_disable(struct work_struct *ignored)
836 {
837 	struct mpam_msc *msc, *tmp;
838 
839 	mutex_lock(&mpam_cpuhp_state_lock);
840 	if (mpam_cpuhp_state) {
841 		cpuhp_remove_state(mpam_cpuhp_state);
842 		mpam_cpuhp_state = 0;
843 	}
844 	mutex_unlock(&mpam_cpuhp_state_lock);
845 
846 	mutex_lock(&mpam_list_lock);
847 	list_for_each_entry_safe(msc, tmp, &mpam_all_msc, all_msc_list)
848 		mpam_msc_destroy(msc);
849 	mutex_unlock(&mpam_list_lock);
850 	mpam_free_garbage();
851 
852 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
853 }
854 
855 /*
856  * Enable mpam once all devices have been probed.
857  * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
858  * Also scheduled when new devices are probed when new CPUs come online.
859  */
860 void mpam_enable(struct work_struct *work)
861 {
862 	static atomic_t once;
863 	struct mpam_msc *msc;
864 	bool all_devices_probed = true;
865 
866 	/* Have we probed all the hw devices? */
867 	guard(srcu)(&mpam_srcu);
868 	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
869 				 srcu_read_lock_held(&mpam_srcu)) {
870 		mutex_lock(&msc->probe_lock);
871 		if (!msc->probed)
872 			all_devices_probed = false;
873 		mutex_unlock(&msc->probe_lock);
874 
875 		if (!all_devices_probed)
876 			break;
877 	}
878 
879 	if (all_devices_probed && !atomic_fetch_inc(&once))
880 		mpam_enable_once();
881 }
882 
883 static int __init mpam_msc_driver_init(void)
884 {
885 	if (!system_supports_mpam())
886 		return -EOPNOTSUPP;
887 
888 	init_srcu_struct(&mpam_srcu);
889 
890 	fw_num_msc = acpi_mpam_count_msc();
891 	if (fw_num_msc <= 0) {
892 		pr_err("No MSC devices found in firmware\n");
893 		return -EINVAL;
894 	}
895 
896 	return platform_driver_register(&mpam_msc_driver);
897 }
898 
899 /* Must occur after arm64_mpam_register_cpus() from arch_initcall() */
900 subsys_initcall(mpam_msc_driver_init);
901