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/list.h> 18 #include <linux/lockdep.h> 19 #include <linux/mutex.h> 20 #include <linux/platform_device.h> 21 #include <linux/printk.h> 22 #include <linux/srcu.h> 23 #include <linux/spinlock.h> 24 #include <linux/types.h> 25 #include <linux/workqueue.h> 26 27 #include "mpam_internal.h" 28 29 /* 30 * mpam_list_lock protects the SRCU lists when writing. Once the 31 * mpam_enabled key is enabled these lists are read-only, 32 * unless the error interrupt disables the driver. 33 */ 34 static DEFINE_MUTEX(mpam_list_lock); 35 static LIST_HEAD(mpam_all_msc); 36 37 struct srcu_struct mpam_srcu; 38 39 /* 40 * Number of MSCs that have been probed. Once all MSCs have been probed MPAM 41 * can be enabled. 42 */ 43 static atomic_t mpam_num_msc; 44 45 static int mpam_cpuhp_state; 46 static DEFINE_MUTEX(mpam_cpuhp_state_lock); 47 48 /* 49 * The smallest common values for any CPU or MSC in the system. 50 * Generating traffic outside this range will result in screaming interrupts. 51 */ 52 u16 mpam_partid_max; 53 u8 mpam_pmg_max; 54 static bool partid_max_init, partid_max_published; 55 static DEFINE_SPINLOCK(partid_max_lock); 56 57 /* 58 * mpam is enabled once all devices have been probed from CPU online callbacks, 59 * scheduled via this work_struct. If access to an MSC depends on a CPU that 60 * was not brought online at boot, this can happen surprisingly late. 61 */ 62 static DECLARE_WORK(mpam_enable_work, &mpam_enable); 63 64 /* 65 * All mpam error interrupts indicate a software bug. On receipt, disable the 66 * driver. 67 */ 68 static DECLARE_WORK(mpam_broken_work, &mpam_disable); 69 70 /* When mpam is disabled, the printed reason to aid debugging */ 71 static char *mpam_disable_reason; 72 73 /* 74 * An MSC is a physical container for controls and monitors, each identified by 75 * their RIS index. These share a base-address, interrupts and some MMIO 76 * registers. A vMSC is a virtual container for RIS in an MSC that control or 77 * monitor the same thing. Members of a vMSC are all RIS in the same MSC, but 78 * not all RIS in an MSC share a vMSC. 79 * 80 * Components are a group of vMSC that control or monitor the same thing but 81 * are from different MSC, so have different base-address, interrupts etc. 82 * Classes are the set components of the same type. 83 * 84 * The features of a vMSC is the union of the RIS it contains. 85 * The features of a Class and Component are the common subset of the vMSC 86 * they contain. 87 * 88 * e.g. The system cache may have bandwidth controls on multiple interfaces, 89 * for regulating traffic from devices independently of traffic from CPUs. 90 * If these are two RIS in one MSC, they will be treated as controlling 91 * different things, and will not share a vMSC/component/class. 92 * 93 * e.g. The L2 may have one MSC and two RIS, one for cache-controls another 94 * for bandwidth. These two RIS are members of the same vMSC. 95 * 96 * e.g. The set of RIS that make up the L2 are grouped as a component. These 97 * are sometimes termed slices. They should be configured the same, as if there 98 * were only one. 99 * 100 * e.g. The SoC probably has more than one L2, each attached to a distinct set 101 * of CPUs. All the L2 components are grouped as a class. 102 * 103 * When creating an MSC, struct mpam_msc is added to the all mpam_all_msc list, 104 * then linked via struct mpam_ris to a vmsc, component and class. 105 * The same MSC may exist under different class->component->vmsc paths, but the 106 * RIS index will be unique. 107 */ 108 LIST_HEAD(mpam_classes); 109 110 /* List of all objects that can be free()d after synchronise_srcu() */ 111 static LLIST_HEAD(mpam_garbage); 112 113 static inline void init_garbage(struct mpam_garbage *garbage) 114 { 115 init_llist_node(&garbage->llist); 116 } 117 118 #define add_to_garbage(x) \ 119 do { \ 120 __typeof__(x) _x = (x); \ 121 _x->garbage.to_free = _x; \ 122 llist_add(&_x->garbage.llist, &mpam_garbage); \ 123 } while (0) 124 125 static void mpam_free_garbage(void) 126 { 127 struct mpam_garbage *iter, *tmp; 128 struct llist_node *to_free = llist_del_all(&mpam_garbage); 129 130 if (!to_free) 131 return; 132 133 synchronize_srcu(&mpam_srcu); 134 135 llist_for_each_entry_safe(iter, tmp, to_free, llist) { 136 if (iter->pdev) 137 devm_kfree(&iter->pdev->dev, iter->to_free); 138 else 139 kfree(iter->to_free); 140 } 141 } 142 143 static u32 __mpam_read_reg(struct mpam_msc *msc, u16 reg) 144 { 145 WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility)); 146 147 return readl_relaxed(msc->mapped_hwpage + reg); 148 } 149 150 static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg) 151 { 152 lockdep_assert_held_once(&msc->part_sel_lock); 153 return __mpam_read_reg(msc, reg); 154 } 155 156 #define mpam_read_partsel_reg(msc, reg) _mpam_read_partsel_reg(msc, MPAMF_##reg) 157 158 static void __mpam_write_reg(struct mpam_msc *msc, u16 reg, u32 val) 159 { 160 WARN_ON_ONCE(reg + sizeof(u32) > msc->mapped_hwpage_sz); 161 WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility)); 162 163 writel_relaxed(val, msc->mapped_hwpage + reg); 164 } 165 166 static inline void _mpam_write_partsel_reg(struct mpam_msc *msc, u16 reg, u32 val) 167 { 168 lockdep_assert_held_once(&msc->part_sel_lock); 169 __mpam_write_reg(msc, reg, val); 170 } 171 172 #define mpam_write_partsel_reg(msc, reg, val) _mpam_write_partsel_reg(msc, MPAMCFG_##reg, val) 173 174 static inline u32 _mpam_read_monsel_reg(struct mpam_msc *msc, u16 reg) 175 { 176 mpam_mon_sel_lock_held(msc); 177 return __mpam_read_reg(msc, reg); 178 } 179 180 #define mpam_read_monsel_reg(msc, reg) _mpam_read_monsel_reg(msc, MSMON_##reg) 181 182 static inline void _mpam_write_monsel_reg(struct mpam_msc *msc, u16 reg, u32 val) 183 { 184 mpam_mon_sel_lock_held(msc); 185 __mpam_write_reg(msc, reg, val); 186 } 187 188 #define mpam_write_monsel_reg(msc, reg, val) _mpam_write_monsel_reg(msc, MSMON_##reg, val) 189 190 static u64 mpam_msc_read_idr(struct mpam_msc *msc) 191 { 192 u64 idr_high = 0, idr_low; 193 194 lockdep_assert_held(&msc->part_sel_lock); 195 196 idr_low = mpam_read_partsel_reg(msc, IDR); 197 if (FIELD_GET(MPAMF_IDR_EXT, idr_low)) 198 idr_high = mpam_read_partsel_reg(msc, IDR + 4); 199 200 return (idr_high << 32) | idr_low; 201 } 202 203 static void __mpam_part_sel_raw(u32 partsel, struct mpam_msc *msc) 204 { 205 lockdep_assert_held(&msc->part_sel_lock); 206 207 mpam_write_partsel_reg(msc, PART_SEL, partsel); 208 } 209 210 static void __mpam_part_sel(u8 ris_idx, u16 partid, struct mpam_msc *msc) 211 { 212 u32 partsel = FIELD_PREP(MPAMCFG_PART_SEL_RIS, ris_idx) | 213 FIELD_PREP(MPAMCFG_PART_SEL_PARTID_SEL, partid); 214 215 __mpam_part_sel_raw(partsel, msc); 216 } 217 218 int mpam_register_requestor(u16 partid_max, u8 pmg_max) 219 { 220 guard(spinlock)(&partid_max_lock); 221 if (!partid_max_init) { 222 mpam_partid_max = partid_max; 223 mpam_pmg_max = pmg_max; 224 partid_max_init = true; 225 } else if (!partid_max_published) { 226 mpam_partid_max = min(mpam_partid_max, partid_max); 227 mpam_pmg_max = min(mpam_pmg_max, pmg_max); 228 } else { 229 /* New requestors can't lower the values */ 230 if (partid_max < mpam_partid_max || pmg_max < mpam_pmg_max) 231 return -EBUSY; 232 } 233 234 return 0; 235 } 236 EXPORT_SYMBOL(mpam_register_requestor); 237 238 static struct mpam_class * 239 mpam_class_alloc(u8 level_idx, enum mpam_class_types type) 240 { 241 struct mpam_class *class; 242 243 lockdep_assert_held(&mpam_list_lock); 244 245 class = kzalloc(sizeof(*class), GFP_KERNEL); 246 if (!class) 247 return ERR_PTR(-ENOMEM); 248 init_garbage(&class->garbage); 249 250 INIT_LIST_HEAD_RCU(&class->components); 251 /* Affinity is updated when ris are added */ 252 class->level = level_idx; 253 class->type = type; 254 INIT_LIST_HEAD_RCU(&class->classes_list); 255 256 list_add_rcu(&class->classes_list, &mpam_classes); 257 258 return class; 259 } 260 261 static void mpam_class_destroy(struct mpam_class *class) 262 { 263 lockdep_assert_held(&mpam_list_lock); 264 265 list_del_rcu(&class->classes_list); 266 add_to_garbage(class); 267 } 268 269 static struct mpam_class * 270 mpam_class_find(u8 level_idx, enum mpam_class_types type) 271 { 272 struct mpam_class *class; 273 274 lockdep_assert_held(&mpam_list_lock); 275 276 list_for_each_entry(class, &mpam_classes, classes_list) { 277 if (class->type == type && class->level == level_idx) 278 return class; 279 } 280 281 return mpam_class_alloc(level_idx, type); 282 } 283 284 static struct mpam_component * 285 mpam_component_alloc(struct mpam_class *class, int id) 286 { 287 struct mpam_component *comp; 288 289 lockdep_assert_held(&mpam_list_lock); 290 291 comp = kzalloc(sizeof(*comp), GFP_KERNEL); 292 if (!comp) 293 return ERR_PTR(-ENOMEM); 294 init_garbage(&comp->garbage); 295 296 comp->comp_id = id; 297 INIT_LIST_HEAD_RCU(&comp->vmsc); 298 /* Affinity is updated when RIS are added */ 299 INIT_LIST_HEAD_RCU(&comp->class_list); 300 comp->class = class; 301 302 list_add_rcu(&comp->class_list, &class->components); 303 304 return comp; 305 } 306 307 static void mpam_component_destroy(struct mpam_component *comp) 308 { 309 struct mpam_class *class = comp->class; 310 311 lockdep_assert_held(&mpam_list_lock); 312 313 list_del_rcu(&comp->class_list); 314 add_to_garbage(comp); 315 316 if (list_empty(&class->components)) 317 mpam_class_destroy(class); 318 } 319 320 static struct mpam_component * 321 mpam_component_find(struct mpam_class *class, int id) 322 { 323 struct mpam_component *comp; 324 325 lockdep_assert_held(&mpam_list_lock); 326 327 list_for_each_entry(comp, &class->components, class_list) { 328 if (comp->comp_id == id) 329 return comp; 330 } 331 332 return mpam_component_alloc(class, id); 333 } 334 335 static struct mpam_vmsc * 336 mpam_vmsc_alloc(struct mpam_component *comp, struct mpam_msc *msc) 337 { 338 struct mpam_vmsc *vmsc; 339 340 lockdep_assert_held(&mpam_list_lock); 341 342 vmsc = kzalloc(sizeof(*vmsc), GFP_KERNEL); 343 if (!vmsc) 344 return ERR_PTR(-ENOMEM); 345 init_garbage(&vmsc->garbage); 346 347 INIT_LIST_HEAD_RCU(&vmsc->ris); 348 INIT_LIST_HEAD_RCU(&vmsc->comp_list); 349 vmsc->comp = comp; 350 vmsc->msc = msc; 351 352 list_add_rcu(&vmsc->comp_list, &comp->vmsc); 353 354 return vmsc; 355 } 356 357 static void mpam_vmsc_destroy(struct mpam_vmsc *vmsc) 358 { 359 struct mpam_component *comp = vmsc->comp; 360 361 lockdep_assert_held(&mpam_list_lock); 362 363 list_del_rcu(&vmsc->comp_list); 364 add_to_garbage(vmsc); 365 366 if (list_empty(&comp->vmsc)) 367 mpam_component_destroy(comp); 368 } 369 370 static struct mpam_vmsc * 371 mpam_vmsc_find(struct mpam_component *comp, struct mpam_msc *msc) 372 { 373 struct mpam_vmsc *vmsc; 374 375 lockdep_assert_held(&mpam_list_lock); 376 377 list_for_each_entry(vmsc, &comp->vmsc, comp_list) { 378 if (vmsc->msc->id == msc->id) 379 return vmsc; 380 } 381 382 return mpam_vmsc_alloc(comp, msc); 383 } 384 385 /* 386 * The cacheinfo structures are only populated when CPUs are online. 387 * This helper walks the acpi tables to include offline CPUs too. 388 */ 389 int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level, 390 cpumask_t *affinity) 391 { 392 return acpi_pptt_get_cpumask_from_cache_id(cache_id, affinity); 393 } 394 395 /* 396 * cpumask_of_node() only knows about online CPUs. This can't tell us whether 397 * a class is represented on all possible CPUs. 398 */ 399 static void get_cpumask_from_node_id(u32 node_id, cpumask_t *affinity) 400 { 401 int cpu; 402 403 for_each_possible_cpu(cpu) { 404 if (node_id == cpu_to_node(cpu)) 405 cpumask_set_cpu(cpu, affinity); 406 } 407 } 408 409 static int mpam_ris_get_affinity(struct mpam_msc *msc, cpumask_t *affinity, 410 enum mpam_class_types type, 411 struct mpam_class *class, 412 struct mpam_component *comp) 413 { 414 int err; 415 416 switch (type) { 417 case MPAM_CLASS_CACHE: 418 err = mpam_get_cpumask_from_cache_id(comp->comp_id, class->level, 419 affinity); 420 if (err) { 421 dev_warn_once(&msc->pdev->dev, 422 "Failed to determine CPU affinity\n"); 423 return err; 424 } 425 426 if (cpumask_empty(affinity)) 427 dev_warn_once(&msc->pdev->dev, "no CPUs associated with cache node\n"); 428 429 break; 430 case MPAM_CLASS_MEMORY: 431 get_cpumask_from_node_id(comp->comp_id, affinity); 432 /* affinity may be empty for CPU-less memory nodes */ 433 break; 434 case MPAM_CLASS_UNKNOWN: 435 return 0; 436 } 437 438 cpumask_and(affinity, affinity, &msc->accessibility); 439 440 return 0; 441 } 442 443 static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx, 444 enum mpam_class_types type, u8 class_id, 445 int component_id) 446 { 447 int err; 448 struct mpam_vmsc *vmsc; 449 struct mpam_msc_ris *ris; 450 struct mpam_class *class; 451 struct mpam_component *comp; 452 struct platform_device *pdev = msc->pdev; 453 454 lockdep_assert_held(&mpam_list_lock); 455 456 if (ris_idx > MPAM_MSC_MAX_NUM_RIS) 457 return -EINVAL; 458 459 if (test_and_set_bit(ris_idx, &msc->ris_idxs)) 460 return -EBUSY; 461 462 ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL); 463 if (!ris) 464 return -ENOMEM; 465 init_garbage(&ris->garbage); 466 ris->garbage.pdev = pdev; 467 468 class = mpam_class_find(class_id, type); 469 if (IS_ERR(class)) 470 return PTR_ERR(class); 471 472 comp = mpam_component_find(class, component_id); 473 if (IS_ERR(comp)) { 474 if (list_empty(&class->components)) 475 mpam_class_destroy(class); 476 return PTR_ERR(comp); 477 } 478 479 vmsc = mpam_vmsc_find(comp, msc); 480 if (IS_ERR(vmsc)) { 481 if (list_empty(&comp->vmsc)) 482 mpam_component_destroy(comp); 483 return PTR_ERR(vmsc); 484 } 485 486 err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp); 487 if (err) { 488 if (list_empty(&vmsc->ris)) 489 mpam_vmsc_destroy(vmsc); 490 return err; 491 } 492 493 ris->ris_idx = ris_idx; 494 INIT_LIST_HEAD_RCU(&ris->msc_list); 495 INIT_LIST_HEAD_RCU(&ris->vmsc_list); 496 ris->vmsc = vmsc; 497 498 cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity); 499 cpumask_or(&class->affinity, &class->affinity, &ris->affinity); 500 list_add_rcu(&ris->vmsc_list, &vmsc->ris); 501 list_add_rcu(&ris->msc_list, &msc->ris); 502 503 return 0; 504 } 505 506 static void mpam_ris_destroy(struct mpam_msc_ris *ris) 507 { 508 struct mpam_vmsc *vmsc = ris->vmsc; 509 struct mpam_msc *msc = vmsc->msc; 510 struct mpam_component *comp = vmsc->comp; 511 struct mpam_class *class = comp->class; 512 513 lockdep_assert_held(&mpam_list_lock); 514 515 /* 516 * It is assumed affinities don't overlap. If they do the class becomes 517 * unusable immediately. 518 */ 519 cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity); 520 cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity); 521 clear_bit(ris->ris_idx, &msc->ris_idxs); 522 list_del_rcu(&ris->msc_list); 523 list_del_rcu(&ris->vmsc_list); 524 add_to_garbage(ris); 525 526 if (list_empty(&vmsc->ris)) 527 mpam_vmsc_destroy(vmsc); 528 } 529 530 int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx, 531 enum mpam_class_types type, u8 class_id, int component_id) 532 { 533 int err; 534 535 mutex_lock(&mpam_list_lock); 536 err = mpam_ris_create_locked(msc, ris_idx, type, class_id, 537 component_id); 538 mutex_unlock(&mpam_list_lock); 539 if (err) 540 mpam_free_garbage(); 541 542 return err; 543 } 544 545 static struct mpam_msc_ris *mpam_get_or_create_ris(struct mpam_msc *msc, 546 u8 ris_idx) 547 { 548 int err; 549 struct mpam_msc_ris *ris; 550 551 lockdep_assert_held(&mpam_list_lock); 552 553 if (!test_bit(ris_idx, &msc->ris_idxs)) { 554 err = mpam_ris_create_locked(msc, ris_idx, MPAM_CLASS_UNKNOWN, 555 0, 0); 556 if (err) 557 return ERR_PTR(err); 558 } 559 560 list_for_each_entry(ris, &msc->ris, msc_list) { 561 if (ris->ris_idx == ris_idx) 562 return ris; 563 } 564 565 return ERR_PTR(-ENOENT); 566 } 567 568 /* 569 * IHI009A.a has this nugget: "If a monitor does not support automatic behaviour 570 * of NRDY, software can use this bit for any purpose" - so hardware might not 571 * implement this - but it isn't RES0. 572 * 573 * Try and see what values stick in this bit. If we can write either value, 574 * its probably not implemented by hardware. 575 */ 576 static bool _mpam_ris_hw_probe_hw_nrdy(struct mpam_msc_ris *ris, u32 mon_reg) 577 { 578 u32 now; 579 u64 mon_sel; 580 bool can_set, can_clear; 581 struct mpam_msc *msc = ris->vmsc->msc; 582 583 if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc))) 584 return false; 585 586 mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, 0) | 587 FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx); 588 _mpam_write_monsel_reg(msc, mon_reg, mon_sel); 589 590 _mpam_write_monsel_reg(msc, mon_reg, MSMON___NRDY); 591 now = _mpam_read_monsel_reg(msc, mon_reg); 592 can_set = now & MSMON___NRDY; 593 594 _mpam_write_monsel_reg(msc, mon_reg, 0); 595 now = _mpam_read_monsel_reg(msc, mon_reg); 596 can_clear = !(now & MSMON___NRDY); 597 mpam_mon_sel_unlock(msc); 598 599 return (!can_set || !can_clear); 600 } 601 602 #define mpam_ris_hw_probe_hw_nrdy(_ris, _mon_reg) \ 603 _mpam_ris_hw_probe_hw_nrdy(_ris, MSMON_##_mon_reg) 604 605 static void mpam_ris_hw_probe(struct mpam_msc_ris *ris) 606 { 607 int err; 608 struct mpam_msc *msc = ris->vmsc->msc; 609 struct device *dev = &msc->pdev->dev; 610 struct mpam_props *props = &ris->props; 611 612 lockdep_assert_held(&msc->probe_lock); 613 lockdep_assert_held(&msc->part_sel_lock); 614 615 /* Cache Portion partitioning */ 616 if (FIELD_GET(MPAMF_IDR_HAS_CPOR_PART, ris->idr)) { 617 u32 cpor_features = mpam_read_partsel_reg(msc, CPOR_IDR); 618 619 props->cpbm_wd = FIELD_GET(MPAMF_CPOR_IDR_CPBM_WD, cpor_features); 620 if (props->cpbm_wd) 621 mpam_set_feature(mpam_feat_cpor_part, props); 622 } 623 624 /* Memory bandwidth partitioning */ 625 if (FIELD_GET(MPAMF_IDR_HAS_MBW_PART, ris->idr)) { 626 u32 mbw_features = mpam_read_partsel_reg(msc, MBW_IDR); 627 628 /* portion bitmap resolution */ 629 props->mbw_pbm_bits = FIELD_GET(MPAMF_MBW_IDR_BWPBM_WD, mbw_features); 630 if (props->mbw_pbm_bits && 631 FIELD_GET(MPAMF_MBW_IDR_HAS_PBM, mbw_features)) 632 mpam_set_feature(mpam_feat_mbw_part, props); 633 634 props->bwa_wd = FIELD_GET(MPAMF_MBW_IDR_BWA_WD, mbw_features); 635 if (props->bwa_wd && FIELD_GET(MPAMF_MBW_IDR_HAS_MAX, mbw_features)) 636 mpam_set_feature(mpam_feat_mbw_max, props); 637 } 638 639 /* Performance Monitoring */ 640 if (FIELD_GET(MPAMF_IDR_HAS_MSMON, ris->idr)) { 641 u32 msmon_features = mpam_read_partsel_reg(msc, MSMON_IDR); 642 643 /* 644 * If the firmware max-nrdy-us property is missing, the 645 * CSU counters can't be used. Should we wait forever? 646 */ 647 err = device_property_read_u32(&msc->pdev->dev, 648 "arm,not-ready-us", 649 &msc->nrdy_usec); 650 651 if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_CSU, msmon_features)) { 652 u32 csumonidr; 653 654 csumonidr = mpam_read_partsel_reg(msc, CSUMON_IDR); 655 props->num_csu_mon = FIELD_GET(MPAMF_CSUMON_IDR_NUM_MON, csumonidr); 656 if (props->num_csu_mon) { 657 bool hw_managed; 658 659 mpam_set_feature(mpam_feat_msmon_csu, props); 660 661 /* Is NRDY hardware managed? */ 662 hw_managed = mpam_ris_hw_probe_hw_nrdy(ris, CSU); 663 if (hw_managed) 664 mpam_set_feature(mpam_feat_msmon_csu_hw_nrdy, props); 665 } 666 667 /* 668 * Accept the missing firmware property if NRDY appears 669 * un-implemented. 670 */ 671 if (err && mpam_has_feature(mpam_feat_msmon_csu_hw_nrdy, props)) 672 dev_err_once(dev, "Counters are not usable because not-ready timeout was not provided by firmware."); 673 } 674 if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_MBWU, msmon_features)) { 675 bool hw_managed; 676 u32 mbwumon_idr = mpam_read_partsel_reg(msc, MBWUMON_IDR); 677 678 props->num_mbwu_mon = FIELD_GET(MPAMF_MBWUMON_IDR_NUM_MON, mbwumon_idr); 679 if (props->num_mbwu_mon) 680 mpam_set_feature(mpam_feat_msmon_mbwu, props); 681 682 /* Is NRDY hardware managed? */ 683 hw_managed = mpam_ris_hw_probe_hw_nrdy(ris, MBWU); 684 if (hw_managed) 685 mpam_set_feature(mpam_feat_msmon_mbwu_hw_nrdy, props); 686 687 /* 688 * Don't warn about any missing firmware property for 689 * MBWU NRDY - it doesn't make any sense! 690 */ 691 } 692 } 693 } 694 695 static int mpam_msc_hw_probe(struct mpam_msc *msc) 696 { 697 u64 idr; 698 u16 partid_max; 699 u8 ris_idx, pmg_max; 700 struct mpam_msc_ris *ris; 701 struct device *dev = &msc->pdev->dev; 702 703 lockdep_assert_held(&msc->probe_lock); 704 705 idr = __mpam_read_reg(msc, MPAMF_AIDR); 706 if ((idr & MPAMF_AIDR_ARCH_MAJOR_REV) != MPAM_ARCHITECTURE_V1) { 707 dev_err_once(dev, "MSC does not match MPAM architecture v1.x\n"); 708 return -EIO; 709 } 710 711 /* Grab an IDR value to find out how many RIS there are */ 712 mutex_lock(&msc->part_sel_lock); 713 idr = mpam_msc_read_idr(msc); 714 mutex_unlock(&msc->part_sel_lock); 715 716 msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr); 717 718 /* Use these values so partid/pmg always starts with a valid value */ 719 msc->partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr); 720 msc->pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr); 721 722 for (ris_idx = 0; ris_idx <= msc->ris_max; ris_idx++) { 723 mutex_lock(&msc->part_sel_lock); 724 __mpam_part_sel(ris_idx, 0, msc); 725 idr = mpam_msc_read_idr(msc); 726 mutex_unlock(&msc->part_sel_lock); 727 728 partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr); 729 pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr); 730 msc->partid_max = min(msc->partid_max, partid_max); 731 msc->pmg_max = min(msc->pmg_max, pmg_max); 732 733 mutex_lock(&mpam_list_lock); 734 ris = mpam_get_or_create_ris(msc, ris_idx); 735 mutex_unlock(&mpam_list_lock); 736 if (IS_ERR(ris)) 737 return PTR_ERR(ris); 738 ris->idr = idr; 739 740 mutex_lock(&msc->part_sel_lock); 741 __mpam_part_sel(ris_idx, 0, msc); 742 mpam_ris_hw_probe(ris); 743 mutex_unlock(&msc->part_sel_lock); 744 } 745 746 spin_lock(&partid_max_lock); 747 mpam_partid_max = min(mpam_partid_max, msc->partid_max); 748 mpam_pmg_max = min(mpam_pmg_max, msc->pmg_max); 749 spin_unlock(&partid_max_lock); 750 751 msc->probed = true; 752 753 return 0; 754 } 755 756 static void mpam_reset_msc_bitmap(struct mpam_msc *msc, u16 reg, u16 wd) 757 { 758 u32 num_words, msb; 759 u32 bm = ~0; 760 int i; 761 762 lockdep_assert_held(&msc->part_sel_lock); 763 764 if (wd == 0) 765 return; 766 767 /* 768 * Write all ~0 to all but the last 32bit-word, which may 769 * have fewer bits... 770 */ 771 num_words = DIV_ROUND_UP(wd, 32); 772 for (i = 0; i < num_words - 1; i++, reg += sizeof(bm)) 773 __mpam_write_reg(msc, reg, bm); 774 775 /* 776 * ....and then the last (maybe) partial 32bit word. When wd is a 777 * multiple of 32, msb should be 31 to write a full 32bit word. 778 */ 779 msb = (wd - 1) % 32; 780 bm = GENMASK(msb, 0); 781 __mpam_write_reg(msc, reg, bm); 782 } 783 784 static void mpam_reset_ris_partid(struct mpam_msc_ris *ris, u16 partid) 785 { 786 struct mpam_msc *msc = ris->vmsc->msc; 787 struct mpam_props *rprops = &ris->props; 788 789 WARN_ON_ONCE(!srcu_read_lock_held((&mpam_srcu))); 790 791 mutex_lock(&msc->part_sel_lock); 792 __mpam_part_sel(ris->ris_idx, partid, msc); 793 794 if (mpam_has_feature(mpam_feat_cpor_part, rprops)) 795 mpam_reset_msc_bitmap(msc, MPAMCFG_CPBM, rprops->cpbm_wd); 796 797 if (mpam_has_feature(mpam_feat_mbw_part, rprops)) 798 mpam_reset_msc_bitmap(msc, MPAMCFG_MBW_PBM, rprops->mbw_pbm_bits); 799 800 if (mpam_has_feature(mpam_feat_mbw_min, rprops)) 801 mpam_write_partsel_reg(msc, MBW_MIN, 0); 802 803 if (mpam_has_feature(mpam_feat_mbw_max, rprops)) 804 mpam_write_partsel_reg(msc, MBW_MAX, MPAMCFG_MBW_MAX_MAX); 805 806 mutex_unlock(&msc->part_sel_lock); 807 } 808 809 /* 810 * Called via smp_call_on_cpu() to prevent migration, while still being 811 * pre-emptible. Caller must hold mpam_srcu. 812 */ 813 static int mpam_reset_ris(void *arg) 814 { 815 u16 partid, partid_max; 816 struct mpam_msc_ris *ris = arg; 817 818 if (ris->in_reset_state) 819 return 0; 820 821 spin_lock(&partid_max_lock); 822 partid_max = mpam_partid_max; 823 spin_unlock(&partid_max_lock); 824 for (partid = 0; partid <= partid_max; partid++) 825 mpam_reset_ris_partid(ris, partid); 826 827 return 0; 828 } 829 830 /* 831 * Get the preferred CPU for this MSC. If it is accessible from this CPU, 832 * this CPU is preferred. This can be preempted/migrated, it will only result 833 * in more work. 834 */ 835 static int mpam_get_msc_preferred_cpu(struct mpam_msc *msc) 836 { 837 int cpu = raw_smp_processor_id(); 838 839 if (cpumask_test_cpu(cpu, &msc->accessibility)) 840 return cpu; 841 842 return cpumask_first_and(&msc->accessibility, cpu_online_mask); 843 } 844 845 static int mpam_touch_msc(struct mpam_msc *msc, int (*fn)(void *a), void *arg) 846 { 847 lockdep_assert_irqs_enabled(); 848 lockdep_assert_cpus_held(); 849 WARN_ON_ONCE(!srcu_read_lock_held((&mpam_srcu))); 850 851 return smp_call_on_cpu(mpam_get_msc_preferred_cpu(msc), fn, arg, true); 852 } 853 854 static void mpam_reset_msc(struct mpam_msc *msc, bool online) 855 { 856 struct mpam_msc_ris *ris; 857 858 list_for_each_entry_srcu(ris, &msc->ris, msc_list, srcu_read_lock_held(&mpam_srcu)) { 859 mpam_touch_msc(msc, &mpam_reset_ris, ris); 860 861 /* 862 * Set in_reset_state when coming online. The reset state 863 * for non-zero partid may be lost while the CPUs are offline. 864 */ 865 ris->in_reset_state = online; 866 } 867 } 868 869 static int mpam_cpu_online(unsigned int cpu) 870 { 871 struct mpam_msc *msc; 872 873 guard(srcu)(&mpam_srcu); 874 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 875 srcu_read_lock_held(&mpam_srcu)) { 876 if (!cpumask_test_cpu(cpu, &msc->accessibility)) 877 continue; 878 879 if (atomic_fetch_inc(&msc->online_refs) == 0) 880 mpam_reset_msc(msc, true); 881 } 882 883 return 0; 884 } 885 886 /* Before mpam is enabled, try to probe new MSC */ 887 static int mpam_discovery_cpu_online(unsigned int cpu) 888 { 889 int err = 0; 890 struct mpam_msc *msc; 891 bool new_device_probed = false; 892 893 guard(srcu)(&mpam_srcu); 894 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 895 srcu_read_lock_held(&mpam_srcu)) { 896 if (!cpumask_test_cpu(cpu, &msc->accessibility)) 897 continue; 898 899 mutex_lock(&msc->probe_lock); 900 if (!msc->probed) 901 err = mpam_msc_hw_probe(msc); 902 mutex_unlock(&msc->probe_lock); 903 904 if (err) 905 break; 906 new_device_probed = true; 907 } 908 909 if (new_device_probed && !err) 910 schedule_work(&mpam_enable_work); 911 if (err) { 912 mpam_disable_reason = "error during probing"; 913 schedule_work(&mpam_broken_work); 914 } 915 916 return err; 917 } 918 919 static int mpam_cpu_offline(unsigned int cpu) 920 { 921 struct mpam_msc *msc; 922 923 guard(srcu)(&mpam_srcu); 924 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 925 srcu_read_lock_held(&mpam_srcu)) { 926 if (!cpumask_test_cpu(cpu, &msc->accessibility)) 927 continue; 928 929 if (atomic_dec_and_test(&msc->online_refs)) 930 mpam_reset_msc(msc, false); 931 } 932 933 return 0; 934 } 935 936 static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online), 937 int (*offline)(unsigned int offline), 938 char *name) 939 { 940 mutex_lock(&mpam_cpuhp_state_lock); 941 if (mpam_cpuhp_state) { 942 cpuhp_remove_state(mpam_cpuhp_state); 943 mpam_cpuhp_state = 0; 944 } 945 946 mpam_cpuhp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, name, online, 947 offline); 948 if (mpam_cpuhp_state <= 0) { 949 pr_err("Failed to register cpuhp callbacks"); 950 mpam_cpuhp_state = 0; 951 } 952 mutex_unlock(&mpam_cpuhp_state_lock); 953 } 954 955 /* 956 * An MSC can control traffic from a set of CPUs, but may only be accessible 957 * from a (hopefully wider) set of CPUs. The common reason for this is power 958 * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the 959 * corresponding cache may also be powered off. By making accesses from 960 * one of those CPUs, we ensure we don't access a cache that's powered off. 961 */ 962 static void update_msc_accessibility(struct mpam_msc *msc) 963 { 964 u32 affinity_id; 965 int err; 966 967 err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity", 968 &affinity_id); 969 if (err) 970 cpumask_copy(&msc->accessibility, cpu_possible_mask); 971 else 972 acpi_pptt_get_cpus_from_container(affinity_id, &msc->accessibility); 973 } 974 975 /* 976 * There are two ways of reaching a struct mpam_msc_ris. Via the 977 * class->component->vmsc->ris, or via the msc. 978 * When destroying the msc, the other side needs unlinking and cleaning up too. 979 */ 980 static void mpam_msc_destroy(struct mpam_msc *msc) 981 { 982 struct platform_device *pdev = msc->pdev; 983 struct mpam_msc_ris *ris, *tmp; 984 985 lockdep_assert_held(&mpam_list_lock); 986 987 list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list) 988 mpam_ris_destroy(ris); 989 990 list_del_rcu(&msc->all_msc_list); 991 platform_set_drvdata(pdev, NULL); 992 993 add_to_garbage(msc); 994 } 995 996 static void mpam_msc_drv_remove(struct platform_device *pdev) 997 { 998 struct mpam_msc *msc = platform_get_drvdata(pdev); 999 1000 mutex_lock(&mpam_list_lock); 1001 mpam_msc_destroy(msc); 1002 mutex_unlock(&mpam_list_lock); 1003 1004 mpam_free_garbage(); 1005 } 1006 1007 static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev) 1008 { 1009 int err; 1010 u32 tmp; 1011 struct mpam_msc *msc; 1012 struct resource *msc_res; 1013 struct device *dev = &pdev->dev; 1014 1015 lockdep_assert_held(&mpam_list_lock); 1016 1017 msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL); 1018 if (!msc) 1019 return ERR_PTR(-ENOMEM); 1020 init_garbage(&msc->garbage); 1021 msc->garbage.pdev = pdev; 1022 1023 err = devm_mutex_init(dev, &msc->probe_lock); 1024 if (err) 1025 return ERR_PTR(err); 1026 1027 err = devm_mutex_init(dev, &msc->part_sel_lock); 1028 if (err) 1029 return ERR_PTR(err); 1030 1031 mpam_mon_sel_lock_init(msc); 1032 msc->id = pdev->id; 1033 msc->pdev = pdev; 1034 INIT_LIST_HEAD_RCU(&msc->all_msc_list); 1035 INIT_LIST_HEAD_RCU(&msc->ris); 1036 1037 update_msc_accessibility(msc); 1038 if (cpumask_empty(&msc->accessibility)) { 1039 dev_err_once(dev, "MSC is not accessible from any CPU!"); 1040 return ERR_PTR(-EINVAL); 1041 } 1042 1043 if (device_property_read_u32(&pdev->dev, "pcc-channel", &tmp)) 1044 msc->iface = MPAM_IFACE_MMIO; 1045 else 1046 msc->iface = MPAM_IFACE_PCC; 1047 1048 if (msc->iface == MPAM_IFACE_MMIO) { 1049 void __iomem *io; 1050 1051 io = devm_platform_get_and_ioremap_resource(pdev, 0, 1052 &msc_res); 1053 if (IS_ERR(io)) { 1054 dev_err_once(dev, "Failed to map MSC base address\n"); 1055 return ERR_CAST(io); 1056 } 1057 msc->mapped_hwpage_sz = msc_res->end - msc_res->start; 1058 msc->mapped_hwpage = io; 1059 } else { 1060 return ERR_PTR(-EINVAL); 1061 } 1062 1063 list_add_rcu(&msc->all_msc_list, &mpam_all_msc); 1064 platform_set_drvdata(pdev, msc); 1065 1066 return msc; 1067 } 1068 1069 static int fw_num_msc; 1070 1071 static int mpam_msc_drv_probe(struct platform_device *pdev) 1072 { 1073 int err; 1074 struct mpam_msc *msc = NULL; 1075 void *plat_data = pdev->dev.platform_data; 1076 1077 mutex_lock(&mpam_list_lock); 1078 msc = do_mpam_msc_drv_probe(pdev); 1079 mutex_unlock(&mpam_list_lock); 1080 1081 if (IS_ERR(msc)) 1082 return PTR_ERR(msc); 1083 1084 /* Create RIS entries described by firmware */ 1085 err = acpi_mpam_parse_resources(msc, plat_data); 1086 if (err) { 1087 mpam_msc_drv_remove(pdev); 1088 return err; 1089 } 1090 1091 if (atomic_add_return(1, &mpam_num_msc) == fw_num_msc) 1092 mpam_register_cpuhp_callbacks(mpam_discovery_cpu_online, NULL, 1093 "mpam:drv_probe"); 1094 1095 return 0; 1096 } 1097 1098 static struct platform_driver mpam_msc_driver = { 1099 .driver = { 1100 .name = "mpam_msc", 1101 }, 1102 .probe = mpam_msc_drv_probe, 1103 .remove = mpam_msc_drv_remove, 1104 }; 1105 1106 /* Any of these features mean the BWA_WD field is valid. */ 1107 static bool mpam_has_bwa_wd_feature(struct mpam_props *props) 1108 { 1109 if (mpam_has_feature(mpam_feat_mbw_min, props)) 1110 return true; 1111 if (mpam_has_feature(mpam_feat_mbw_max, props)) 1112 return true; 1113 return false; 1114 } 1115 1116 #define MISMATCHED_HELPER(parent, child, helper, field, alias) \ 1117 helper(parent) && \ 1118 ((helper(child) && (parent)->field != (child)->field) || \ 1119 (!helper(child) && !(alias))) 1120 1121 #define MISMATCHED_FEAT(parent, child, feat, field, alias) \ 1122 mpam_has_feature((feat), (parent)) && \ 1123 ((mpam_has_feature((feat), (child)) && (parent)->field != (child)->field) || \ 1124 (!mpam_has_feature((feat), (child)) && !(alias))) 1125 1126 #define CAN_MERGE_FEAT(parent, child, feat, alias) \ 1127 (alias) && !mpam_has_feature((feat), (parent)) && \ 1128 mpam_has_feature((feat), (child)) 1129 1130 /* 1131 * Combine two props fields. 1132 * If this is for controls that alias the same resource, it is safe to just 1133 * copy the values over. If two aliasing controls implement the same scheme 1134 * a safe value must be picked. 1135 * For non-aliasing controls, these control different resources, and the 1136 * resulting safe value must be compatible with both. When merging values in 1137 * the tree, all the aliasing resources must be handled first. 1138 * On mismatch, parent is modified. 1139 */ 1140 static void __props_mismatch(struct mpam_props *parent, 1141 struct mpam_props *child, bool alias) 1142 { 1143 if (CAN_MERGE_FEAT(parent, child, mpam_feat_cpor_part, alias)) { 1144 parent->cpbm_wd = child->cpbm_wd; 1145 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_cpor_part, 1146 cpbm_wd, alias)) { 1147 pr_debug("cleared cpor_part\n"); 1148 mpam_clear_feature(mpam_feat_cpor_part, parent); 1149 parent->cpbm_wd = 0; 1150 } 1151 1152 if (CAN_MERGE_FEAT(parent, child, mpam_feat_mbw_part, alias)) { 1153 parent->mbw_pbm_bits = child->mbw_pbm_bits; 1154 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_mbw_part, 1155 mbw_pbm_bits, alias)) { 1156 pr_debug("cleared mbw_part\n"); 1157 mpam_clear_feature(mpam_feat_mbw_part, parent); 1158 parent->mbw_pbm_bits = 0; 1159 } 1160 1161 /* bwa_wd is a count of bits, fewer bits means less precision */ 1162 if (alias && !mpam_has_bwa_wd_feature(parent) && 1163 mpam_has_bwa_wd_feature(child)) { 1164 parent->bwa_wd = child->bwa_wd; 1165 } else if (MISMATCHED_HELPER(parent, child, mpam_has_bwa_wd_feature, 1166 bwa_wd, alias)) { 1167 pr_debug("took the min bwa_wd\n"); 1168 parent->bwa_wd = min(parent->bwa_wd, child->bwa_wd); 1169 } 1170 1171 /* For num properties, take the minimum */ 1172 if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_csu, alias)) { 1173 parent->num_csu_mon = child->num_csu_mon; 1174 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_csu, 1175 num_csu_mon, alias)) { 1176 pr_debug("took the min num_csu_mon\n"); 1177 parent->num_csu_mon = min(parent->num_csu_mon, 1178 child->num_csu_mon); 1179 } 1180 1181 if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_mbwu, alias)) { 1182 parent->num_mbwu_mon = child->num_mbwu_mon; 1183 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_mbwu, 1184 num_mbwu_mon, alias)) { 1185 pr_debug("took the min num_mbwu_mon\n"); 1186 parent->num_mbwu_mon = min(parent->num_mbwu_mon, 1187 child->num_mbwu_mon); 1188 } 1189 1190 if (alias) { 1191 /* Merge features for aliased resources */ 1192 bitmap_or(parent->features, parent->features, child->features, MPAM_FEATURE_LAST); 1193 } else { 1194 /* Clear missing features for non aliasing */ 1195 bitmap_and(parent->features, parent->features, child->features, MPAM_FEATURE_LAST); 1196 } 1197 } 1198 1199 /* 1200 * If a vmsc doesn't match class feature/configuration, do the right thing(tm). 1201 * For 'num' properties we can just take the minimum. 1202 * For properties where the mismatched unused bits would make a difference, we 1203 * nobble the class feature, as we can't configure all the resources. 1204 * e.g. The L3 cache is composed of two resources with 13 and 17 portion 1205 * bitmaps respectively. 1206 */ 1207 static void 1208 __class_props_mismatch(struct mpam_class *class, struct mpam_vmsc *vmsc) 1209 { 1210 struct mpam_props *cprops = &class->props; 1211 struct mpam_props *vprops = &vmsc->props; 1212 struct device *dev = &vmsc->msc->pdev->dev; 1213 1214 lockdep_assert_held(&mpam_list_lock); /* we modify class */ 1215 1216 dev_dbg(dev, "Merging features for class:0x%lx &= vmsc:0x%lx\n", 1217 (long)cprops->features, (long)vprops->features); 1218 1219 /* Take the safe value for any common features */ 1220 __props_mismatch(cprops, vprops, false); 1221 } 1222 1223 static void 1224 __vmsc_props_mismatch(struct mpam_vmsc *vmsc, struct mpam_msc_ris *ris) 1225 { 1226 struct mpam_props *rprops = &ris->props; 1227 struct mpam_props *vprops = &vmsc->props; 1228 struct device *dev = &vmsc->msc->pdev->dev; 1229 1230 lockdep_assert_held(&mpam_list_lock); /* we modify vmsc */ 1231 1232 dev_dbg(dev, "Merging features for vmsc:0x%lx |= ris:0x%lx\n", 1233 (long)vprops->features, (long)rprops->features); 1234 1235 /* 1236 * Merge mismatched features - Copy any features that aren't common, 1237 * but take the safe value for any common features. 1238 */ 1239 __props_mismatch(vprops, rprops, true); 1240 } 1241 1242 /* 1243 * Copy the first component's first vMSC's properties and features to the 1244 * class. __class_props_mismatch() will remove conflicts. 1245 * It is not possible to have a class with no components, or a component with 1246 * no resources. The vMSC properties have already been built. 1247 */ 1248 static void mpam_enable_init_class_features(struct mpam_class *class) 1249 { 1250 struct mpam_vmsc *vmsc; 1251 struct mpam_component *comp; 1252 1253 comp = list_first_entry(&class->components, 1254 struct mpam_component, class_list); 1255 vmsc = list_first_entry(&comp->vmsc, 1256 struct mpam_vmsc, comp_list); 1257 1258 class->props = vmsc->props; 1259 } 1260 1261 static void mpam_enable_merge_vmsc_features(struct mpam_component *comp) 1262 { 1263 struct mpam_vmsc *vmsc; 1264 struct mpam_msc_ris *ris; 1265 struct mpam_class *class = comp->class; 1266 1267 list_for_each_entry(vmsc, &comp->vmsc, comp_list) { 1268 list_for_each_entry(ris, &vmsc->ris, vmsc_list) { 1269 __vmsc_props_mismatch(vmsc, ris); 1270 class->nrdy_usec = max(class->nrdy_usec, 1271 vmsc->msc->nrdy_usec); 1272 } 1273 } 1274 } 1275 1276 static void mpam_enable_merge_class_features(struct mpam_component *comp) 1277 { 1278 struct mpam_vmsc *vmsc; 1279 struct mpam_class *class = comp->class; 1280 1281 list_for_each_entry(vmsc, &comp->vmsc, comp_list) 1282 __class_props_mismatch(class, vmsc); 1283 } 1284 1285 /* 1286 * Merge all the common resource features into class. 1287 * vmsc features are bitwise-or'd together by mpam_enable_merge_vmsc_features() 1288 * as the first step so that mpam_enable_init_class_features() can initialise 1289 * the class with a representative set of features. 1290 * Next the mpam_enable_merge_class_features() bitwise-and's all the vmsc 1291 * features to form the class features. 1292 * Other features are the min/max as appropriate. 1293 * 1294 * To avoid walking the whole tree twice, the class->nrdy_usec property is 1295 * updated when working with the vmsc as it is a max(), and doesn't need 1296 * initialising first. 1297 */ 1298 static void mpam_enable_merge_features(struct list_head *all_classes_list) 1299 { 1300 struct mpam_class *class; 1301 struct mpam_component *comp; 1302 1303 lockdep_assert_held(&mpam_list_lock); 1304 1305 list_for_each_entry(class, all_classes_list, classes_list) { 1306 list_for_each_entry(comp, &class->components, class_list) 1307 mpam_enable_merge_vmsc_features(comp); 1308 1309 mpam_enable_init_class_features(class); 1310 1311 list_for_each_entry(comp, &class->components, class_list) 1312 mpam_enable_merge_class_features(comp); 1313 } 1314 } 1315 1316 static void mpam_enable_once(void) 1317 { 1318 /* 1319 * Once the cpuhp callbacks have been changed, mpam_partid_max can no 1320 * longer change. 1321 */ 1322 spin_lock(&partid_max_lock); 1323 partid_max_published = true; 1324 spin_unlock(&partid_max_lock); 1325 1326 mutex_lock(&mpam_list_lock); 1327 mpam_enable_merge_features(&mpam_classes); 1328 mutex_unlock(&mpam_list_lock); 1329 1330 mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline, 1331 "mpam:online"); 1332 1333 /* Use printk() to avoid the pr_fmt adding the function name. */ 1334 printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n", 1335 mpam_partid_max + 1, mpam_pmg_max + 1); 1336 } 1337 1338 static void mpam_reset_component_locked(struct mpam_component *comp) 1339 { 1340 struct mpam_vmsc *vmsc; 1341 1342 lockdep_assert_cpus_held(); 1343 1344 guard(srcu)(&mpam_srcu); 1345 list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list, 1346 srcu_read_lock_held(&mpam_srcu)) { 1347 struct mpam_msc *msc = vmsc->msc; 1348 struct mpam_msc_ris *ris; 1349 1350 list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list, 1351 srcu_read_lock_held(&mpam_srcu)) { 1352 if (!ris->in_reset_state) 1353 mpam_touch_msc(msc, mpam_reset_ris, ris); 1354 ris->in_reset_state = true; 1355 } 1356 } 1357 } 1358 1359 static void mpam_reset_class_locked(struct mpam_class *class) 1360 { 1361 struct mpam_component *comp; 1362 1363 lockdep_assert_cpus_held(); 1364 1365 guard(srcu)(&mpam_srcu); 1366 list_for_each_entry_srcu(comp, &class->components, class_list, 1367 srcu_read_lock_held(&mpam_srcu)) 1368 mpam_reset_component_locked(comp); 1369 } 1370 1371 static void mpam_reset_class(struct mpam_class *class) 1372 { 1373 cpus_read_lock(); 1374 mpam_reset_class_locked(class); 1375 cpus_read_unlock(); 1376 } 1377 1378 /* 1379 * Called in response to an error IRQ. 1380 * All of MPAMs errors indicate a software bug, restore any modified 1381 * controls to their reset values. 1382 */ 1383 void mpam_disable(struct work_struct *ignored) 1384 { 1385 int idx; 1386 struct mpam_class *class; 1387 struct mpam_msc *msc, *tmp; 1388 1389 mutex_lock(&mpam_cpuhp_state_lock); 1390 if (mpam_cpuhp_state) { 1391 cpuhp_remove_state(mpam_cpuhp_state); 1392 mpam_cpuhp_state = 0; 1393 } 1394 mutex_unlock(&mpam_cpuhp_state_lock); 1395 1396 idx = srcu_read_lock(&mpam_srcu); 1397 list_for_each_entry_srcu(class, &mpam_classes, classes_list, 1398 srcu_read_lock_held(&mpam_srcu)) 1399 mpam_reset_class(class); 1400 srcu_read_unlock(&mpam_srcu, idx); 1401 1402 mutex_lock(&mpam_list_lock); 1403 list_for_each_entry_safe(msc, tmp, &mpam_all_msc, all_msc_list) 1404 mpam_msc_destroy(msc); 1405 mutex_unlock(&mpam_list_lock); 1406 mpam_free_garbage(); 1407 1408 pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason); 1409 } 1410 1411 /* 1412 * Enable mpam once all devices have been probed. 1413 * Scheduled by mpam_discovery_cpu_online() once all devices have been created. 1414 * Also scheduled when new devices are probed when new CPUs come online. 1415 */ 1416 void mpam_enable(struct work_struct *work) 1417 { 1418 static atomic_t once; 1419 struct mpam_msc *msc; 1420 bool all_devices_probed = true; 1421 1422 /* Have we probed all the hw devices? */ 1423 guard(srcu)(&mpam_srcu); 1424 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 1425 srcu_read_lock_held(&mpam_srcu)) { 1426 mutex_lock(&msc->probe_lock); 1427 if (!msc->probed) 1428 all_devices_probed = false; 1429 mutex_unlock(&msc->probe_lock); 1430 1431 if (!all_devices_probed) 1432 break; 1433 } 1434 1435 if (all_devices_probed && !atomic_fetch_inc(&once)) 1436 mpam_enable_once(); 1437 } 1438 1439 static int __init mpam_msc_driver_init(void) 1440 { 1441 if (!system_supports_mpam()) 1442 return -EOPNOTSUPP; 1443 1444 init_srcu_struct(&mpam_srcu); 1445 1446 fw_num_msc = acpi_mpam_count_msc(); 1447 if (fw_num_msc <= 0) { 1448 pr_err("No MSC devices found in firmware\n"); 1449 return -EINVAL; 1450 } 1451 1452 return platform_driver_register(&mpam_msc_driver); 1453 } 1454 1455 /* Must occur after arm64_mpam_register_cpus() from arch_initcall() */ 1456 subsys_initcall(mpam_msc_driver_init); 1457