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