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 overflow_val *= 64; 1201 1202 return overflow_val; 1203 } 1204 1205 static void __ris_msmon_read(void *arg) 1206 { 1207 u64 now; 1208 bool nrdy = false; 1209 bool config_mismatch; 1210 bool overflow = false; 1211 struct mon_read *m = arg; 1212 struct mon_cfg *ctx = m->ctx; 1213 bool reset_on_next_read = false; 1214 struct mpam_msc_ris *ris = m->ris; 1215 struct msmon_mbwu_state *mbwu_state; 1216 struct mpam_msc *msc = m->ris->vmsc->msc; 1217 u32 mon_sel, ctl_val, flt_val, cur_ctl, cur_flt; 1218 1219 if (!mpam_mon_sel_lock(msc)) { 1220 m->err = -EIO; 1221 return; 1222 } 1223 mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, ctx->mon) | 1224 FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx); 1225 mpam_write_monsel_reg(msc, CFG_MON_SEL, mon_sel); 1226 1227 switch (m->type) { 1228 case mpam_feat_msmon_mbwu_31counter: 1229 case mpam_feat_msmon_mbwu_44counter: 1230 case mpam_feat_msmon_mbwu_63counter: 1231 mbwu_state = &ris->mbwu_state[ctx->mon]; 1232 if (mbwu_state) { 1233 reset_on_next_read = mbwu_state->reset_on_next_read; 1234 mbwu_state->reset_on_next_read = false; 1235 } 1236 break; 1237 default: 1238 break; 1239 } 1240 1241 /* 1242 * Read the existing configuration to avoid re-writing the same values. 1243 * This saves waiting for 'nrdy' on subsequent reads. 1244 */ 1245 read_msmon_ctl_flt_vals(m, &cur_ctl, &cur_flt); 1246 1247 if (mpam_feat_msmon_mbwu_31counter == m->type) 1248 overflow = cur_ctl & MSMON_CFG_x_CTL_OFLOW_STATUS; 1249 else if (mpam_feat_msmon_mbwu_44counter == m->type || 1250 mpam_feat_msmon_mbwu_63counter == m->type) 1251 overflow = cur_ctl & MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L; 1252 1253 clean_msmon_ctl_val(&cur_ctl); 1254 gen_msmon_ctl_flt_vals(m, &ctl_val, &flt_val); 1255 config_mismatch = cur_flt != flt_val || 1256 cur_ctl != (ctl_val | MSMON_CFG_x_CTL_EN); 1257 1258 if (config_mismatch || reset_on_next_read) { 1259 write_msmon_ctl_flt_vals(m, ctl_val, flt_val); 1260 overflow = false; 1261 } else if (overflow) { 1262 mpam_write_monsel_reg(msc, CFG_MBWU_CTL, 1263 cur_ctl & 1264 ~(MSMON_CFG_x_CTL_OFLOW_STATUS | 1265 MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L)); 1266 } 1267 1268 switch (m->type) { 1269 case mpam_feat_msmon_csu: 1270 now = mpam_read_monsel_reg(msc, CSU); 1271 nrdy = now & MSMON___NRDY; 1272 now = FIELD_GET(MSMON___VALUE, now); 1273 1274 if (mpam_has_quirk(IGNORE_CSU_NRDY, msc) && m->waited_timeout) 1275 nrdy = false; 1276 1277 break; 1278 case mpam_feat_msmon_mbwu_31counter: 1279 case mpam_feat_msmon_mbwu_44counter: 1280 case mpam_feat_msmon_mbwu_63counter: 1281 if (m->type != mpam_feat_msmon_mbwu_31counter) { 1282 now = mpam_msc_read_mbwu_l(msc); 1283 nrdy = now & MSMON___L_NRDY; 1284 1285 if (m->type == mpam_feat_msmon_mbwu_63counter) 1286 now = FIELD_GET(MSMON___LWD_VALUE, now); 1287 else 1288 now = FIELD_GET(MSMON___L_VALUE, now); 1289 } else { 1290 now = mpam_read_monsel_reg(msc, MBWU); 1291 nrdy = now & MSMON___NRDY; 1292 now = FIELD_GET(MSMON___VALUE, now); 1293 } 1294 1295 if (mpam_has_quirk(T241_MBW_COUNTER_SCALE_64, msc)) 1296 now *= 64; 1297 1298 if (nrdy) 1299 break; 1300 1301 mbwu_state = &ris->mbwu_state[ctx->mon]; 1302 1303 if (overflow) 1304 mbwu_state->correction += mpam_msmon_overflow_val(m->type, msc); 1305 1306 /* 1307 * Include bandwidth consumed before the last hardware reset and 1308 * a counter size increment for each overflow. 1309 */ 1310 now += mbwu_state->correction; 1311 break; 1312 default: 1313 m->err = -EINVAL; 1314 } 1315 mpam_mon_sel_unlock(msc); 1316 1317 if (nrdy) 1318 m->err = -EBUSY; 1319 1320 if (m->err) 1321 return; 1322 1323 *m->val += now; 1324 } 1325 1326 static int _msmon_read(struct mpam_component *comp, struct mon_read *arg) 1327 { 1328 int err, any_err = 0; 1329 struct mpam_vmsc *vmsc; 1330 1331 guard(srcu)(&mpam_srcu); 1332 list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list, 1333 srcu_read_lock_held(&mpam_srcu)) { 1334 struct mpam_msc *msc = vmsc->msc; 1335 struct mpam_msc_ris *ris; 1336 1337 list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list, 1338 srcu_read_lock_held(&mpam_srcu)) { 1339 arg->ris = ris; 1340 1341 err = smp_call_function_any(&msc->accessibility, 1342 __ris_msmon_read, arg, 1343 true); 1344 if (!err && arg->err) 1345 err = arg->err; 1346 1347 /* 1348 * Save one error to be returned to the caller, but 1349 * keep reading counters so that get reprogrammed. On 1350 * platforms with NRDY this lets us wait once. 1351 */ 1352 if (err) 1353 any_err = err; 1354 } 1355 } 1356 1357 return any_err; 1358 } 1359 1360 static enum mpam_device_features mpam_msmon_choose_counter(struct mpam_class *class) 1361 { 1362 struct mpam_props *cprops = &class->props; 1363 1364 if (mpam_has_feature(mpam_feat_msmon_mbwu_63counter, cprops)) 1365 return mpam_feat_msmon_mbwu_63counter; 1366 if (mpam_has_feature(mpam_feat_msmon_mbwu_44counter, cprops)) 1367 return mpam_feat_msmon_mbwu_44counter; 1368 1369 return mpam_feat_msmon_mbwu_31counter; 1370 } 1371 1372 int mpam_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx, 1373 enum mpam_device_features type, u64 *val) 1374 { 1375 int err; 1376 struct mon_read arg; 1377 u64 wait_jiffies = 0; 1378 struct mpam_class *class = comp->class; 1379 struct mpam_props *cprops = &class->props; 1380 1381 might_sleep(); 1382 1383 if (!mpam_is_enabled()) 1384 return -EIO; 1385 1386 if (!mpam_has_feature(type, cprops)) 1387 return -EOPNOTSUPP; 1388 1389 if (type == mpam_feat_msmon_mbwu) 1390 type = mpam_msmon_choose_counter(class); 1391 1392 arg = (struct mon_read) { 1393 .ctx = ctx, 1394 .type = type, 1395 .val = val, 1396 }; 1397 *val = 0; 1398 1399 err = _msmon_read(comp, &arg); 1400 if (err == -EBUSY && class->nrdy_usec) 1401 wait_jiffies = usecs_to_jiffies(class->nrdy_usec); 1402 1403 while (wait_jiffies) 1404 wait_jiffies = schedule_timeout_uninterruptible(wait_jiffies); 1405 1406 if (err == -EBUSY) { 1407 arg = (struct mon_read) { 1408 .ctx = ctx, 1409 .type = type, 1410 .val = val, 1411 .waited_timeout = true, 1412 }; 1413 *val = 0; 1414 1415 err = _msmon_read(comp, &arg); 1416 } 1417 1418 return err; 1419 } 1420 1421 void mpam_msmon_reset_mbwu(struct mpam_component *comp, struct mon_cfg *ctx) 1422 { 1423 struct mpam_msc *msc; 1424 struct mpam_vmsc *vmsc; 1425 struct mpam_msc_ris *ris; 1426 1427 if (!mpam_is_enabled()) 1428 return; 1429 1430 guard(srcu)(&mpam_srcu); 1431 list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list, 1432 srcu_read_lock_held(&mpam_srcu)) { 1433 if (!mpam_has_feature(mpam_feat_msmon_mbwu, &vmsc->props)) 1434 continue; 1435 1436 msc = vmsc->msc; 1437 list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list, 1438 srcu_read_lock_held(&mpam_srcu)) { 1439 if (!mpam_has_feature(mpam_feat_msmon_mbwu, &ris->props)) 1440 continue; 1441 1442 if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc))) 1443 continue; 1444 1445 ris->mbwu_state[ctx->mon].correction = 0; 1446 ris->mbwu_state[ctx->mon].reset_on_next_read = true; 1447 mpam_mon_sel_unlock(msc); 1448 } 1449 } 1450 } 1451 1452 static void mpam_reset_msc_bitmap(struct mpam_msc *msc, u16 reg, u16 wd) 1453 { 1454 u32 num_words, msb; 1455 u32 bm = ~0; 1456 int i; 1457 1458 lockdep_assert_held(&msc->part_sel_lock); 1459 1460 if (wd == 0) 1461 return; 1462 1463 /* 1464 * Write all ~0 to all but the last 32bit-word, which may 1465 * have fewer bits... 1466 */ 1467 num_words = DIV_ROUND_UP(wd, 32); 1468 for (i = 0; i < num_words - 1; i++, reg += sizeof(bm)) 1469 __mpam_write_reg(msc, reg, bm); 1470 1471 /* 1472 * ....and then the last (maybe) partial 32bit word. When wd is a 1473 * multiple of 32, msb should be 31 to write a full 32bit word. 1474 */ 1475 msb = (wd - 1) % 32; 1476 bm = GENMASK(msb, 0); 1477 __mpam_write_reg(msc, reg, bm); 1478 } 1479 1480 static void mpam_apply_t241_erratum(struct mpam_msc_ris *ris, u16 partid) 1481 { 1482 int sidx, i, lcount = 1000; 1483 void __iomem *regs; 1484 u64 val0, val; 1485 1486 regs = t241_scratch_regs[ris->vmsc->msc->t241_id]; 1487 1488 for (i = 0; i < lcount; i++) { 1489 /* Read the shadow register at index 0 */ 1490 val0 = readq_relaxed(regs + T241_SHADOW_REG_OFF(0, partid)); 1491 1492 /* Check if all the shadow registers have the same value */ 1493 for (sidx = 1; sidx < T241_CHIP_NSLICES; sidx++) { 1494 val = readq_relaxed(regs + 1495 T241_SHADOW_REG_OFF(sidx, partid)); 1496 if (val != val0) 1497 break; 1498 } 1499 if (sidx == T241_CHIP_NSLICES) 1500 break; 1501 } 1502 1503 if (i == lcount) 1504 pr_warn_once("t241: inconsistent values in shadow regs"); 1505 1506 /* Write a value zero to spare registers to take effect of MBW conf */ 1507 writeq_relaxed(0, regs + T241_SPARE_REG0_OFF); 1508 writeq_relaxed(0, regs + T241_SPARE_REG1_OFF); 1509 } 1510 1511 static void mpam_quirk_post_config_change(struct mpam_msc_ris *ris, u16 partid, 1512 struct mpam_config *cfg) 1513 { 1514 if (mpam_has_quirk(T241_SCRUB_SHADOW_REGS, ris->vmsc->msc)) 1515 mpam_apply_t241_erratum(ris, partid); 1516 } 1517 1518 static u16 mpam_wa_t241_force_mbw_min_to_one(struct mpam_props *props) 1519 { 1520 u16 max_hw_value, min_hw_granule, res0_bits; 1521 1522 res0_bits = 16 - props->bwa_wd; 1523 max_hw_value = ((1 << props->bwa_wd) - 1) << res0_bits; 1524 min_hw_granule = ~max_hw_value; 1525 1526 return min_hw_granule + 1; 1527 } 1528 1529 static u16 mpam_wa_t241_calc_min_from_max(struct mpam_props *props, 1530 struct mpam_config *cfg) 1531 { 1532 u16 val = 0; 1533 u16 max; 1534 u16 delta = ((5 * MPAMCFG_MBW_MAX_MAX) / 100) - 1; 1535 1536 if (mpam_has_feature(mpam_feat_mbw_max, cfg)) { 1537 max = cfg->mbw_max; 1538 } else { 1539 /* Resetting. Hence, use the ris specific default. */ 1540 max = GENMASK(15, 16 - props->bwa_wd); 1541 } 1542 1543 if (max > delta) 1544 val = max - delta; 1545 1546 return val; 1547 } 1548 1549 /* Called via IPI. Call while holding an SRCU reference */ 1550 static void mpam_reprogram_ris_partid(struct mpam_msc_ris *ris, u16 partid, 1551 struct mpam_config *cfg) 1552 { 1553 u32 pri_val = 0; 1554 u16 cmax = MPAMCFG_CMAX_CMAX; 1555 struct mpam_msc *msc = ris->vmsc->msc; 1556 struct mpam_props *rprops = &ris->props; 1557 u16 dspri = GENMASK(rprops->dspri_wd, 0); 1558 u16 intpri = GENMASK(rprops->intpri_wd, 0); 1559 1560 mutex_lock(&msc->part_sel_lock); 1561 __mpam_part_sel(ris->ris_idx, partid, msc); 1562 1563 if (mpam_has_feature(mpam_feat_partid_nrw, rprops)) { 1564 /* Update the intpartid mapping */ 1565 mpam_write_partsel_reg(msc, INTPARTID, 1566 MPAMCFG_INTPARTID_INTERNAL | partid); 1567 1568 /* 1569 * Then switch to the 'internal' partid to update the 1570 * configuration. 1571 */ 1572 __mpam_intpart_sel(ris->ris_idx, partid, msc); 1573 } 1574 1575 if (mpam_has_feature(mpam_feat_cpor_part, rprops)) { 1576 if (mpam_has_feature(mpam_feat_cpor_part, cfg)) 1577 mpam_write_partsel_reg(msc, CPBM, cfg->cpbm); 1578 else 1579 mpam_reset_msc_bitmap(msc, MPAMCFG_CPBM, rprops->cpbm_wd); 1580 } 1581 1582 if (mpam_has_feature(mpam_feat_mbw_part, rprops)) { 1583 if (mpam_has_feature(mpam_feat_mbw_part, cfg)) 1584 mpam_reset_msc_bitmap(msc, MPAMCFG_MBW_PBM, rprops->mbw_pbm_bits); 1585 else 1586 mpam_write_partsel_reg(msc, MBW_PBM, cfg->mbw_pbm); 1587 } 1588 1589 if (mpam_has_feature(mpam_feat_mbw_min, rprops)) { 1590 u16 val = 0; 1591 1592 if (mpam_has_quirk(T241_FORCE_MBW_MIN_TO_ONE, msc)) { 1593 u16 min = mpam_wa_t241_force_mbw_min_to_one(rprops); 1594 1595 val = mpam_wa_t241_calc_min_from_max(rprops, cfg); 1596 val = max(val, min); 1597 } 1598 1599 mpam_write_partsel_reg(msc, MBW_MIN, val); 1600 } 1601 1602 if (mpam_has_feature(mpam_feat_mbw_max, rprops)) { 1603 if (mpam_has_feature(mpam_feat_mbw_max, cfg)) 1604 mpam_write_partsel_reg(msc, MBW_MAX, cfg->mbw_max); 1605 else 1606 mpam_write_partsel_reg(msc, MBW_MAX, MPAMCFG_MBW_MAX_MAX); 1607 } 1608 1609 if (mpam_has_feature(mpam_feat_mbw_prop, rprops)) 1610 mpam_write_partsel_reg(msc, MBW_PROP, 0); 1611 1612 if (mpam_has_feature(mpam_feat_cmax_cmax, rprops)) 1613 mpam_write_partsel_reg(msc, CMAX, cmax); 1614 1615 if (mpam_has_feature(mpam_feat_cmax_cmin, rprops)) 1616 mpam_write_partsel_reg(msc, CMIN, 0); 1617 1618 if (mpam_has_feature(mpam_feat_cmax_cassoc, rprops)) 1619 mpam_write_partsel_reg(msc, CASSOC, MPAMCFG_CASSOC_CASSOC); 1620 1621 if (mpam_has_feature(mpam_feat_intpri_part, rprops) || 1622 mpam_has_feature(mpam_feat_dspri_part, rprops)) { 1623 /* aces high? */ 1624 if (!mpam_has_feature(mpam_feat_intpri_part_0_low, rprops)) 1625 intpri = 0; 1626 if (!mpam_has_feature(mpam_feat_dspri_part_0_low, rprops)) 1627 dspri = 0; 1628 1629 if (mpam_has_feature(mpam_feat_intpri_part, rprops)) 1630 pri_val |= FIELD_PREP(MPAMCFG_PRI_INTPRI, intpri); 1631 if (mpam_has_feature(mpam_feat_dspri_part, rprops)) 1632 pri_val |= FIELD_PREP(MPAMCFG_PRI_DSPRI, dspri); 1633 1634 mpam_write_partsel_reg(msc, PRI, pri_val); 1635 } 1636 1637 mpam_quirk_post_config_change(ris, partid, cfg); 1638 1639 mutex_unlock(&msc->part_sel_lock); 1640 } 1641 1642 /* Call with msc cfg_lock held */ 1643 static int mpam_restore_mbwu_state(void *_ris) 1644 { 1645 int i; 1646 u64 val; 1647 struct mon_read mwbu_arg; 1648 struct mpam_msc_ris *ris = _ris; 1649 struct mpam_class *class = ris->vmsc->comp->class; 1650 1651 for (i = 0; i < ris->props.num_mbwu_mon; i++) { 1652 if (ris->mbwu_state[i].enabled) { 1653 mwbu_arg.ris = ris; 1654 mwbu_arg.ctx = &ris->mbwu_state[i].cfg; 1655 mwbu_arg.type = mpam_msmon_choose_counter(class); 1656 mwbu_arg.val = &val; 1657 1658 __ris_msmon_read(&mwbu_arg); 1659 } 1660 } 1661 1662 return 0; 1663 } 1664 1665 /* Call with MSC cfg_lock held */ 1666 static int mpam_save_mbwu_state(void *arg) 1667 { 1668 int i; 1669 u64 val; 1670 struct mon_cfg *cfg; 1671 u32 cur_flt, cur_ctl, mon_sel; 1672 struct mpam_msc_ris *ris = arg; 1673 struct msmon_mbwu_state *mbwu_state; 1674 struct mpam_msc *msc = ris->vmsc->msc; 1675 1676 for (i = 0; i < ris->props.num_mbwu_mon; i++) { 1677 mbwu_state = &ris->mbwu_state[i]; 1678 cfg = &mbwu_state->cfg; 1679 1680 if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc))) 1681 return -EIO; 1682 1683 mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, i) | 1684 FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx); 1685 mpam_write_monsel_reg(msc, CFG_MON_SEL, mon_sel); 1686 1687 cur_flt = mpam_read_monsel_reg(msc, CFG_MBWU_FLT); 1688 cur_ctl = mpam_read_monsel_reg(msc, CFG_MBWU_CTL); 1689 mpam_write_monsel_reg(msc, CFG_MBWU_CTL, 0); 1690 1691 if (mpam_ris_has_mbwu_long_counter(ris)) { 1692 val = mpam_msc_read_mbwu_l(msc); 1693 mpam_msc_zero_mbwu_l(msc); 1694 } else { 1695 val = mpam_read_monsel_reg(msc, MBWU); 1696 mpam_write_monsel_reg(msc, MBWU, 0); 1697 } 1698 1699 cfg->mon = i; 1700 cfg->pmg = FIELD_GET(MSMON_CFG_x_FLT_PMG, cur_flt); 1701 cfg->match_pmg = FIELD_GET(MSMON_CFG_x_CTL_MATCH_PMG, cur_ctl); 1702 cfg->partid = FIELD_GET(MSMON_CFG_x_FLT_PARTID, cur_flt); 1703 mbwu_state->correction += val; 1704 mbwu_state->enabled = FIELD_GET(MSMON_CFG_x_CTL_EN, cur_ctl); 1705 mpam_mon_sel_unlock(msc); 1706 } 1707 1708 return 0; 1709 } 1710 1711 /* 1712 * Called via smp_call_on_cpu() to prevent migration, while still being 1713 * pre-emptible. Caller must hold mpam_srcu. 1714 */ 1715 static int mpam_reset_ris(void *arg) 1716 { 1717 u16 partid, partid_max; 1718 struct mpam_config reset_cfg = {}; 1719 struct mpam_msc_ris *ris = arg; 1720 1721 if (ris->in_reset_state) 1722 return 0; 1723 1724 spin_lock(&partid_max_lock); 1725 partid_max = mpam_partid_max; 1726 spin_unlock(&partid_max_lock); 1727 for (partid = 0; partid <= partid_max; partid++) 1728 mpam_reprogram_ris_partid(ris, partid, &reset_cfg); 1729 1730 return 0; 1731 } 1732 1733 /* 1734 * Get the preferred CPU for this MSC. If it is accessible from this CPU, 1735 * this CPU is preferred. This can be preempted/migrated, it will only result 1736 * in more work. 1737 */ 1738 static int mpam_get_msc_preferred_cpu(struct mpam_msc *msc) 1739 { 1740 int cpu = raw_smp_processor_id(); 1741 1742 if (cpumask_test_cpu(cpu, &msc->accessibility)) 1743 return cpu; 1744 1745 return cpumask_first_and(&msc->accessibility, cpu_online_mask); 1746 } 1747 1748 static int mpam_touch_msc(struct mpam_msc *msc, int (*fn)(void *a), void *arg) 1749 { 1750 lockdep_assert_irqs_enabled(); 1751 lockdep_assert_cpus_held(); 1752 WARN_ON_ONCE(!srcu_read_lock_held((&mpam_srcu))); 1753 1754 return smp_call_on_cpu(mpam_get_msc_preferred_cpu(msc), fn, arg, true); 1755 } 1756 1757 struct mpam_write_config_arg { 1758 struct mpam_msc_ris *ris; 1759 struct mpam_component *comp; 1760 u16 partid; 1761 }; 1762 1763 static int __write_config(void *arg) 1764 { 1765 struct mpam_write_config_arg *c = arg; 1766 1767 mpam_reprogram_ris_partid(c->ris, c->partid, &c->comp->cfg[c->partid]); 1768 1769 return 0; 1770 } 1771 1772 static void mpam_reprogram_msc(struct mpam_msc *msc) 1773 { 1774 u16 partid; 1775 bool reset; 1776 struct mpam_config *cfg; 1777 struct mpam_msc_ris *ris; 1778 struct mpam_write_config_arg arg; 1779 1780 /* 1781 * No lock for mpam_partid_max as partid_max_published has been 1782 * set by mpam_enabled(), so the values can no longer change. 1783 */ 1784 mpam_assert_partid_sizes_fixed(); 1785 1786 mutex_lock(&msc->cfg_lock); 1787 list_for_each_entry_srcu(ris, &msc->ris, msc_list, 1788 srcu_read_lock_held(&mpam_srcu)) { 1789 if (!mpam_is_enabled() && !ris->in_reset_state) { 1790 mpam_touch_msc(msc, &mpam_reset_ris, ris); 1791 ris->in_reset_state = true; 1792 continue; 1793 } 1794 1795 arg.comp = ris->vmsc->comp; 1796 arg.ris = ris; 1797 reset = true; 1798 for (partid = 0; partid <= mpam_partid_max; partid++) { 1799 cfg = &ris->vmsc->comp->cfg[partid]; 1800 if (!bitmap_empty(cfg->features, MPAM_FEATURE_LAST)) 1801 reset = false; 1802 1803 arg.partid = partid; 1804 mpam_touch_msc(msc, __write_config, &arg); 1805 } 1806 ris->in_reset_state = reset; 1807 1808 if (mpam_has_feature(mpam_feat_msmon_mbwu, &ris->props)) 1809 mpam_touch_msc(msc, &mpam_restore_mbwu_state, ris); 1810 } 1811 mutex_unlock(&msc->cfg_lock); 1812 } 1813 1814 static void _enable_percpu_irq(void *_irq) 1815 { 1816 int *irq = _irq; 1817 1818 enable_percpu_irq(*irq, IRQ_TYPE_NONE); 1819 } 1820 1821 static int mpam_cpu_online(unsigned int cpu) 1822 { 1823 struct mpam_msc *msc; 1824 1825 guard(srcu)(&mpam_srcu); 1826 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 1827 srcu_read_lock_held(&mpam_srcu)) { 1828 if (!cpumask_test_cpu(cpu, &msc->accessibility)) 1829 continue; 1830 1831 if (msc->reenable_error_ppi) 1832 _enable_percpu_irq(&msc->reenable_error_ppi); 1833 1834 if (atomic_fetch_inc(&msc->online_refs) == 0) 1835 mpam_reprogram_msc(msc); 1836 } 1837 1838 if (mpam_resctrl_enabled) 1839 return mpam_resctrl_online_cpu(cpu); 1840 1841 return 0; 1842 } 1843 1844 /* Before mpam is enabled, try to probe new MSC */ 1845 static int mpam_discovery_cpu_online(unsigned int cpu) 1846 { 1847 int err = 0; 1848 struct mpam_msc *msc; 1849 bool new_device_probed = false; 1850 1851 if (mpam_is_enabled()) 1852 return 0; 1853 1854 guard(srcu)(&mpam_srcu); 1855 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 1856 srcu_read_lock_held(&mpam_srcu)) { 1857 if (!cpumask_test_cpu(cpu, &msc->accessibility)) 1858 continue; 1859 1860 mutex_lock(&msc->probe_lock); 1861 if (!msc->probed) 1862 err = mpam_msc_hw_probe(msc); 1863 mutex_unlock(&msc->probe_lock); 1864 1865 if (err) 1866 break; 1867 new_device_probed = true; 1868 } 1869 1870 if (new_device_probed && !err) 1871 schedule_work(&mpam_enable_work); 1872 if (err) { 1873 mpam_disable_reason = "error during probing"; 1874 schedule_work(&mpam_broken_work); 1875 } 1876 1877 return err; 1878 } 1879 1880 static int mpam_cpu_offline(unsigned int cpu) 1881 { 1882 struct mpam_msc *msc; 1883 1884 if (mpam_resctrl_enabled) 1885 mpam_resctrl_offline_cpu(cpu); 1886 1887 guard(srcu)(&mpam_srcu); 1888 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 1889 srcu_read_lock_held(&mpam_srcu)) { 1890 if (!cpumask_test_cpu(cpu, &msc->accessibility)) 1891 continue; 1892 1893 if (msc->reenable_error_ppi) 1894 disable_percpu_irq(msc->reenable_error_ppi); 1895 1896 if (atomic_dec_and_test(&msc->online_refs)) { 1897 struct mpam_msc_ris *ris; 1898 1899 mutex_lock(&msc->cfg_lock); 1900 list_for_each_entry_srcu(ris, &msc->ris, msc_list, 1901 srcu_read_lock_held(&mpam_srcu)) { 1902 mpam_touch_msc(msc, &mpam_reset_ris, ris); 1903 1904 /* 1905 * The reset state for non-zero partid may be 1906 * lost while the CPUs are offline. 1907 */ 1908 ris->in_reset_state = false; 1909 1910 if (mpam_is_enabled()) 1911 mpam_touch_msc(msc, &mpam_save_mbwu_state, ris); 1912 } 1913 mutex_unlock(&msc->cfg_lock); 1914 } 1915 } 1916 1917 return 0; 1918 } 1919 1920 static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online), 1921 int (*offline)(unsigned int offline), 1922 char *name) 1923 { 1924 mutex_lock(&mpam_cpuhp_state_lock); 1925 if (mpam_cpuhp_state) { 1926 cpuhp_remove_state(mpam_cpuhp_state); 1927 mpam_cpuhp_state = 0; 1928 } 1929 1930 mpam_cpuhp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, name, online, 1931 offline); 1932 if (mpam_cpuhp_state <= 0) { 1933 pr_err("Failed to register cpuhp callbacks"); 1934 mpam_cpuhp_state = 0; 1935 } 1936 mutex_unlock(&mpam_cpuhp_state_lock); 1937 } 1938 1939 static int __setup_ppi(struct mpam_msc *msc) 1940 { 1941 int cpu; 1942 1943 msc->error_dev_id = alloc_percpu(struct mpam_msc *); 1944 if (!msc->error_dev_id) 1945 return -ENOMEM; 1946 1947 for_each_cpu(cpu, &msc->accessibility) 1948 *per_cpu_ptr(msc->error_dev_id, cpu) = msc; 1949 1950 return 0; 1951 } 1952 1953 static int mpam_msc_setup_error_irq(struct mpam_msc *msc) 1954 { 1955 int irq; 1956 1957 irq = platform_get_irq_byname_optional(msc->pdev, "error"); 1958 if (irq <= 0) 1959 return 0; 1960 1961 /* Allocate and initialise the percpu device pointer for PPI */ 1962 if (irq_is_percpu(irq)) 1963 return __setup_ppi(msc); 1964 1965 /* sanity check: shared interrupts can be routed anywhere? */ 1966 if (!cpumask_equal(&msc->accessibility, cpu_possible_mask)) { 1967 pr_err_once("msc:%u is a private resource with a shared error interrupt", 1968 msc->id); 1969 return -EINVAL; 1970 } 1971 1972 return 0; 1973 } 1974 1975 /* 1976 * An MSC can control traffic from a set of CPUs, but may only be accessible 1977 * from a (hopefully wider) set of CPUs. The common reason for this is power 1978 * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the 1979 * corresponding cache may also be powered off. By making accesses from 1980 * one of those CPUs, we ensure we don't access a cache that's powered off. 1981 */ 1982 static void update_msc_accessibility(struct mpam_msc *msc) 1983 { 1984 u32 affinity_id; 1985 int err; 1986 1987 err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity", 1988 &affinity_id); 1989 if (err) 1990 cpumask_copy(&msc->accessibility, cpu_possible_mask); 1991 else 1992 acpi_pptt_get_cpus_from_container(affinity_id, &msc->accessibility); 1993 } 1994 1995 /* 1996 * There are two ways of reaching a struct mpam_msc_ris. Via the 1997 * class->component->vmsc->ris, or via the msc. 1998 * When destroying the msc, the other side needs unlinking and cleaning up too. 1999 */ 2000 static void mpam_msc_destroy(struct mpam_msc *msc) 2001 { 2002 struct platform_device *pdev = msc->pdev; 2003 struct mpam_msc_ris *ris, *tmp; 2004 2005 lockdep_assert_held(&mpam_list_lock); 2006 2007 list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list) 2008 mpam_ris_destroy(ris); 2009 2010 list_del_rcu(&msc->all_msc_list); 2011 platform_set_drvdata(pdev, NULL); 2012 2013 add_to_garbage(msc); 2014 } 2015 2016 static void mpam_msc_drv_remove(struct platform_device *pdev) 2017 { 2018 struct mpam_msc *msc = platform_get_drvdata(pdev); 2019 2020 mutex_lock(&mpam_list_lock); 2021 mpam_msc_destroy(msc); 2022 mutex_unlock(&mpam_list_lock); 2023 2024 mpam_free_garbage(); 2025 } 2026 2027 static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev) 2028 { 2029 int err; 2030 u32 tmp; 2031 struct mpam_msc *msc; 2032 struct resource *msc_res; 2033 struct device *dev = &pdev->dev; 2034 2035 lockdep_assert_held(&mpam_list_lock); 2036 2037 msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL); 2038 if (!msc) 2039 return ERR_PTR(-ENOMEM); 2040 init_garbage(&msc->garbage); 2041 msc->garbage.pdev = pdev; 2042 2043 err = devm_mutex_init(dev, &msc->probe_lock); 2044 if (err) 2045 return ERR_PTR(err); 2046 2047 err = devm_mutex_init(dev, &msc->part_sel_lock); 2048 if (err) 2049 return ERR_PTR(err); 2050 2051 err = devm_mutex_init(dev, &msc->error_irq_lock); 2052 if (err) 2053 return ERR_PTR(err); 2054 2055 err = devm_mutex_init(dev, &msc->cfg_lock); 2056 if (err) 2057 return ERR_PTR(err); 2058 2059 mpam_mon_sel_lock_init(msc); 2060 msc->id = pdev->id; 2061 msc->pdev = pdev; 2062 INIT_LIST_HEAD_RCU(&msc->all_msc_list); 2063 INIT_LIST_HEAD_RCU(&msc->ris); 2064 2065 update_msc_accessibility(msc); 2066 if (cpumask_empty(&msc->accessibility)) { 2067 dev_err_once(dev, "MSC is not accessible from any CPU!"); 2068 return ERR_PTR(-EINVAL); 2069 } 2070 2071 err = mpam_msc_setup_error_irq(msc); 2072 if (err) 2073 return ERR_PTR(err); 2074 2075 if (device_property_read_u32(&pdev->dev, "pcc-channel", &tmp)) 2076 msc->iface = MPAM_IFACE_MMIO; 2077 else 2078 msc->iface = MPAM_IFACE_PCC; 2079 2080 if (msc->iface == MPAM_IFACE_MMIO) { 2081 void __iomem *io; 2082 2083 io = devm_platform_get_and_ioremap_resource(pdev, 0, 2084 &msc_res); 2085 if (IS_ERR(io)) { 2086 dev_err_once(dev, "Failed to map MSC base address\n"); 2087 return ERR_CAST(io); 2088 } 2089 msc->mapped_hwpage_sz = msc_res->end - msc_res->start; 2090 msc->mapped_hwpage = io; 2091 } else { 2092 return ERR_PTR(-EINVAL); 2093 } 2094 2095 list_add_rcu(&msc->all_msc_list, &mpam_all_msc); 2096 platform_set_drvdata(pdev, msc); 2097 2098 return msc; 2099 } 2100 2101 static int fw_num_msc; 2102 2103 static int mpam_msc_drv_probe(struct platform_device *pdev) 2104 { 2105 int err; 2106 struct mpam_msc *msc = NULL; 2107 void *plat_data = pdev->dev.platform_data; 2108 2109 mutex_lock(&mpam_list_lock); 2110 msc = do_mpam_msc_drv_probe(pdev); 2111 mutex_unlock(&mpam_list_lock); 2112 2113 if (IS_ERR(msc)) 2114 return PTR_ERR(msc); 2115 2116 /* Create RIS entries described by firmware */ 2117 err = acpi_mpam_parse_resources(msc, plat_data); 2118 if (err) { 2119 mpam_msc_drv_remove(pdev); 2120 return err; 2121 } 2122 2123 if (atomic_add_return(1, &mpam_num_msc) == fw_num_msc) 2124 mpam_register_cpuhp_callbacks(mpam_discovery_cpu_online, NULL, 2125 "mpam:drv_probe"); 2126 2127 return 0; 2128 } 2129 2130 static struct platform_driver mpam_msc_driver = { 2131 .driver = { 2132 .name = "mpam_msc", 2133 }, 2134 .probe = mpam_msc_drv_probe, 2135 .remove = mpam_msc_drv_remove, 2136 }; 2137 2138 /* Any of these features mean the BWA_WD field is valid. */ 2139 static bool mpam_has_bwa_wd_feature(struct mpam_props *props) 2140 { 2141 if (mpam_has_feature(mpam_feat_mbw_min, props)) 2142 return true; 2143 if (mpam_has_feature(mpam_feat_mbw_max, props)) 2144 return true; 2145 if (mpam_has_feature(mpam_feat_mbw_prop, props)) 2146 return true; 2147 return false; 2148 } 2149 2150 /* Any of these features mean the CMAX_WD field is valid. */ 2151 static bool mpam_has_cmax_wd_feature(struct mpam_props *props) 2152 { 2153 if (mpam_has_feature(mpam_feat_cmax_cmax, props)) 2154 return true; 2155 if (mpam_has_feature(mpam_feat_cmax_cmin, props)) 2156 return true; 2157 return false; 2158 } 2159 2160 #define MISMATCHED_HELPER(parent, child, helper, field, alias) \ 2161 helper(parent) && \ 2162 ((helper(child) && (parent)->field != (child)->field) || \ 2163 (!helper(child) && !(alias))) 2164 2165 #define MISMATCHED_FEAT(parent, child, feat, field, alias) \ 2166 mpam_has_feature((feat), (parent)) && \ 2167 ((mpam_has_feature((feat), (child)) && (parent)->field != (child)->field) || \ 2168 (!mpam_has_feature((feat), (child)) && !(alias))) 2169 2170 #define CAN_MERGE_FEAT(parent, child, feat, alias) \ 2171 (alias) && !mpam_has_feature((feat), (parent)) && \ 2172 mpam_has_feature((feat), (child)) 2173 2174 /* 2175 * Combine two props fields. 2176 * If this is for controls that alias the same resource, it is safe to just 2177 * copy the values over. If two aliasing controls implement the same scheme 2178 * a safe value must be picked. 2179 * For non-aliasing controls, these control different resources, and the 2180 * resulting safe value must be compatible with both. When merging values in 2181 * the tree, all the aliasing resources must be handled first. 2182 * On mismatch, parent is modified. 2183 * Quirks on an MSC will apply to all MSC in that class. 2184 */ 2185 static void __props_mismatch(struct mpam_props *parent, 2186 struct mpam_props *child, bool alias) 2187 { 2188 if (CAN_MERGE_FEAT(parent, child, mpam_feat_cpor_part, alias)) { 2189 parent->cpbm_wd = child->cpbm_wd; 2190 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_cpor_part, 2191 cpbm_wd, alias)) { 2192 pr_debug("cleared cpor_part\n"); 2193 mpam_clear_feature(mpam_feat_cpor_part, parent); 2194 parent->cpbm_wd = 0; 2195 } 2196 2197 if (CAN_MERGE_FEAT(parent, child, mpam_feat_mbw_part, alias)) { 2198 parent->mbw_pbm_bits = child->mbw_pbm_bits; 2199 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_mbw_part, 2200 mbw_pbm_bits, alias)) { 2201 pr_debug("cleared mbw_part\n"); 2202 mpam_clear_feature(mpam_feat_mbw_part, parent); 2203 parent->mbw_pbm_bits = 0; 2204 } 2205 2206 /* bwa_wd is a count of bits, fewer bits means less precision */ 2207 if (alias && !mpam_has_bwa_wd_feature(parent) && 2208 mpam_has_bwa_wd_feature(child)) { 2209 parent->bwa_wd = child->bwa_wd; 2210 } else if (MISMATCHED_HELPER(parent, child, mpam_has_bwa_wd_feature, 2211 bwa_wd, alias)) { 2212 pr_debug("took the min bwa_wd\n"); 2213 parent->bwa_wd = min(parent->bwa_wd, child->bwa_wd); 2214 } 2215 2216 if (alias && !mpam_has_cmax_wd_feature(parent) && mpam_has_cmax_wd_feature(child)) { 2217 parent->cmax_wd = child->cmax_wd; 2218 } else if (MISMATCHED_HELPER(parent, child, mpam_has_cmax_wd_feature, 2219 cmax_wd, alias)) { 2220 pr_debug("%s took the min cmax_wd\n", __func__); 2221 parent->cmax_wd = min(parent->cmax_wd, child->cmax_wd); 2222 } 2223 2224 if (CAN_MERGE_FEAT(parent, child, mpam_feat_cmax_cassoc, alias)) { 2225 parent->cassoc_wd = child->cassoc_wd; 2226 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_cmax_cassoc, 2227 cassoc_wd, alias)) { 2228 pr_debug("%s cleared cassoc_wd\n", __func__); 2229 mpam_clear_feature(mpam_feat_cmax_cassoc, parent); 2230 parent->cassoc_wd = 0; 2231 } 2232 2233 /* For num properties, take the minimum */ 2234 if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_csu, alias)) { 2235 parent->num_csu_mon = child->num_csu_mon; 2236 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_csu, 2237 num_csu_mon, alias)) { 2238 pr_debug("took the min num_csu_mon\n"); 2239 parent->num_csu_mon = min(parent->num_csu_mon, 2240 child->num_csu_mon); 2241 } 2242 2243 if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_mbwu, alias)) { 2244 parent->num_mbwu_mon = child->num_mbwu_mon; 2245 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_mbwu, 2246 num_mbwu_mon, alias)) { 2247 pr_debug("took the min num_mbwu_mon\n"); 2248 parent->num_mbwu_mon = min(parent->num_mbwu_mon, 2249 child->num_mbwu_mon); 2250 } 2251 2252 if (CAN_MERGE_FEAT(parent, child, mpam_feat_intpri_part, alias)) { 2253 parent->intpri_wd = child->intpri_wd; 2254 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_intpri_part, 2255 intpri_wd, alias)) { 2256 pr_debug("%s took the min intpri_wd\n", __func__); 2257 parent->intpri_wd = min(parent->intpri_wd, child->intpri_wd); 2258 } 2259 2260 if (CAN_MERGE_FEAT(parent, child, mpam_feat_dspri_part, alias)) { 2261 parent->dspri_wd = child->dspri_wd; 2262 } else if (MISMATCHED_FEAT(parent, child, mpam_feat_dspri_part, 2263 dspri_wd, alias)) { 2264 pr_debug("%s took the min dspri_wd\n", __func__); 2265 parent->dspri_wd = min(parent->dspri_wd, child->dspri_wd); 2266 } 2267 2268 /* TODO: alias support for these two */ 2269 /* {int,ds}pri may not have differing 0-low behaviour */ 2270 if (mpam_has_feature(mpam_feat_intpri_part, parent) && 2271 (!mpam_has_feature(mpam_feat_intpri_part, child) || 2272 mpam_has_feature(mpam_feat_intpri_part_0_low, parent) != 2273 mpam_has_feature(mpam_feat_intpri_part_0_low, child))) { 2274 pr_debug("%s cleared intpri_part\n", __func__); 2275 mpam_clear_feature(mpam_feat_intpri_part, parent); 2276 mpam_clear_feature(mpam_feat_intpri_part_0_low, parent); 2277 } 2278 if (mpam_has_feature(mpam_feat_dspri_part, parent) && 2279 (!mpam_has_feature(mpam_feat_dspri_part, child) || 2280 mpam_has_feature(mpam_feat_dspri_part_0_low, parent) != 2281 mpam_has_feature(mpam_feat_dspri_part_0_low, child))) { 2282 pr_debug("%s cleared dspri_part\n", __func__); 2283 mpam_clear_feature(mpam_feat_dspri_part, parent); 2284 mpam_clear_feature(mpam_feat_dspri_part_0_low, parent); 2285 } 2286 2287 if (alias) { 2288 /* Merge features for aliased resources */ 2289 bitmap_or(parent->features, parent->features, child->features, MPAM_FEATURE_LAST); 2290 } else { 2291 /* Clear missing features for non aliasing */ 2292 bitmap_and(parent->features, parent->features, child->features, MPAM_FEATURE_LAST); 2293 } 2294 } 2295 2296 /* 2297 * If a vmsc doesn't match class feature/configuration, do the right thing(tm). 2298 * For 'num' properties we can just take the minimum. 2299 * For properties where the mismatched unused bits would make a difference, we 2300 * nobble the class feature, as we can't configure all the resources. 2301 * e.g. The L3 cache is composed of two resources with 13 and 17 portion 2302 * bitmaps respectively. 2303 * Quirks on an MSC will apply to all MSC in that class. 2304 */ 2305 static void 2306 __class_props_mismatch(struct mpam_class *class, struct mpam_vmsc *vmsc) 2307 { 2308 struct mpam_props *cprops = &class->props; 2309 struct mpam_props *vprops = &vmsc->props; 2310 struct device *dev = &vmsc->msc->pdev->dev; 2311 2312 lockdep_assert_held(&mpam_list_lock); /* we modify class */ 2313 2314 dev_dbg(dev, "Merging features for class:0x%lx &= vmsc:0x%lx\n", 2315 (long)cprops->features, (long)vprops->features); 2316 2317 /* Merge quirks */ 2318 class->quirks |= vmsc->msc->quirks; 2319 2320 /* Take the safe value for any common features */ 2321 __props_mismatch(cprops, vprops, false); 2322 } 2323 2324 static void 2325 __vmsc_props_mismatch(struct mpam_vmsc *vmsc, struct mpam_msc_ris *ris) 2326 { 2327 struct mpam_props *rprops = &ris->props; 2328 struct mpam_props *vprops = &vmsc->props; 2329 struct device *dev = &vmsc->msc->pdev->dev; 2330 2331 lockdep_assert_held(&mpam_list_lock); /* we modify vmsc */ 2332 2333 dev_dbg(dev, "Merging features for vmsc:0x%lx |= ris:0x%lx\n", 2334 (long)vprops->features, (long)rprops->features); 2335 2336 /* 2337 * Merge mismatched features - Copy any features that aren't common, 2338 * but take the safe value for any common features. 2339 */ 2340 __props_mismatch(vprops, rprops, true); 2341 } 2342 2343 /* 2344 * Copy the first component's first vMSC's properties and features to the 2345 * class. __class_props_mismatch() will remove conflicts. 2346 * It is not possible to have a class with no components, or a component with 2347 * no resources. The vMSC properties have already been built. 2348 */ 2349 static void mpam_enable_init_class_features(struct mpam_class *class) 2350 { 2351 struct mpam_vmsc *vmsc; 2352 struct mpam_component *comp; 2353 2354 comp = list_first_entry(&class->components, 2355 struct mpam_component, class_list); 2356 vmsc = list_first_entry(&comp->vmsc, 2357 struct mpam_vmsc, comp_list); 2358 2359 class->props = vmsc->props; 2360 } 2361 2362 static void mpam_enable_merge_vmsc_features(struct mpam_component *comp) 2363 { 2364 struct mpam_vmsc *vmsc; 2365 struct mpam_msc_ris *ris; 2366 struct mpam_class *class = comp->class; 2367 2368 list_for_each_entry(vmsc, &comp->vmsc, comp_list) { 2369 list_for_each_entry(ris, &vmsc->ris, vmsc_list) { 2370 __vmsc_props_mismatch(vmsc, ris); 2371 class->nrdy_usec = max(class->nrdy_usec, 2372 vmsc->msc->nrdy_usec); 2373 } 2374 } 2375 } 2376 2377 static void mpam_enable_merge_class_features(struct mpam_component *comp) 2378 { 2379 struct mpam_vmsc *vmsc; 2380 struct mpam_class *class = comp->class; 2381 2382 list_for_each_entry(vmsc, &comp->vmsc, comp_list) 2383 __class_props_mismatch(class, vmsc); 2384 2385 if (mpam_has_quirk(T241_FORCE_MBW_MIN_TO_ONE, class)) 2386 mpam_clear_feature(mpam_feat_mbw_min, &class->props); 2387 } 2388 2389 /* 2390 * Merge all the common resource features into class. 2391 * vmsc features are bitwise-or'd together by mpam_enable_merge_vmsc_features() 2392 * as the first step so that mpam_enable_init_class_features() can initialise 2393 * the class with a representative set of features. 2394 * Next the mpam_enable_merge_class_features() bitwise-and's all the vmsc 2395 * features to form the class features. 2396 * Other features are the min/max as appropriate. 2397 * 2398 * To avoid walking the whole tree twice, the class->nrdy_usec property is 2399 * updated when working with the vmsc as it is a max(), and doesn't need 2400 * initialising first. 2401 */ 2402 static void mpam_enable_merge_features(struct list_head *all_classes_list) 2403 { 2404 struct mpam_class *class; 2405 struct mpam_component *comp; 2406 2407 lockdep_assert_held(&mpam_list_lock); 2408 2409 list_for_each_entry(class, all_classes_list, classes_list) { 2410 list_for_each_entry(comp, &class->components, class_list) 2411 mpam_enable_merge_vmsc_features(comp); 2412 2413 mpam_enable_init_class_features(class); 2414 2415 list_for_each_entry(comp, &class->components, class_list) 2416 mpam_enable_merge_class_features(comp); 2417 } 2418 } 2419 2420 static char *mpam_errcode_names[16] = { 2421 [MPAM_ERRCODE_NONE] = "No error", 2422 [MPAM_ERRCODE_PARTID_SEL_RANGE] = "PARTID_SEL_Range", 2423 [MPAM_ERRCODE_REQ_PARTID_RANGE] = "Req_PARTID_Range", 2424 [MPAM_ERRCODE_MSMONCFG_ID_RANGE] = "MSMONCFG_ID_RANGE", 2425 [MPAM_ERRCODE_REQ_PMG_RANGE] = "Req_PMG_Range", 2426 [MPAM_ERRCODE_MONITOR_RANGE] = "Monitor_Range", 2427 [MPAM_ERRCODE_INTPARTID_RANGE] = "intPARTID_Range", 2428 [MPAM_ERRCODE_UNEXPECTED_INTERNAL] = "Unexpected_INTERNAL", 2429 [MPAM_ERRCODE_UNDEFINED_RIS_PART_SEL] = "Undefined_RIS_PART_SEL", 2430 [MPAM_ERRCODE_RIS_NO_CONTROL] = "RIS_No_Control", 2431 [MPAM_ERRCODE_UNDEFINED_RIS_MON_SEL] = "Undefined_RIS_MON_SEL", 2432 [MPAM_ERRCODE_RIS_NO_MONITOR] = "RIS_No_Monitor", 2433 [12 ... 15] = "Reserved" 2434 }; 2435 2436 static int mpam_enable_msc_ecr(void *_msc) 2437 { 2438 struct mpam_msc *msc = _msc; 2439 2440 __mpam_write_reg(msc, MPAMF_ECR, MPAMF_ECR_INTEN); 2441 2442 return 0; 2443 } 2444 2445 /* This can run in mpam_disable(), and the interrupt handler on the same CPU */ 2446 static int mpam_disable_msc_ecr(void *_msc) 2447 { 2448 struct mpam_msc *msc = _msc; 2449 2450 __mpam_write_reg(msc, MPAMF_ECR, 0); 2451 2452 return 0; 2453 } 2454 2455 static irqreturn_t __mpam_irq_handler(int irq, struct mpam_msc *msc) 2456 { 2457 u64 reg; 2458 u16 partid; 2459 u8 errcode, pmg, ris; 2460 2461 if (WARN_ON_ONCE(!msc) || 2462 WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), 2463 &msc->accessibility))) 2464 return IRQ_NONE; 2465 2466 reg = mpam_msc_read_esr(msc); 2467 2468 errcode = FIELD_GET(MPAMF_ESR_ERRCODE, reg); 2469 if (!errcode) 2470 return IRQ_NONE; 2471 2472 /* Clear level triggered irq */ 2473 mpam_msc_clear_esr(msc); 2474 2475 partid = FIELD_GET(MPAMF_ESR_PARTID_MON, reg); 2476 pmg = FIELD_GET(MPAMF_ESR_PMG, reg); 2477 ris = FIELD_GET(MPAMF_ESR_RIS, reg); 2478 2479 pr_err_ratelimited("error irq from msc:%u '%s', partid:%u, pmg: %u, ris: %u\n", 2480 msc->id, mpam_errcode_names[errcode], partid, pmg, 2481 ris); 2482 2483 /* Disable this interrupt. */ 2484 mpam_disable_msc_ecr(msc); 2485 2486 /* Are we racing with the thread disabling MPAM? */ 2487 if (!mpam_is_enabled()) 2488 return IRQ_HANDLED; 2489 2490 /* 2491 * Schedule the teardown work. Don't use a threaded IRQ as we can't 2492 * unregister the interrupt from the threaded part of the handler. 2493 */ 2494 mpam_disable_reason = "hardware error interrupt"; 2495 schedule_work(&mpam_broken_work); 2496 2497 return IRQ_HANDLED; 2498 } 2499 2500 static irqreturn_t mpam_ppi_handler(int irq, void *dev_id) 2501 { 2502 struct mpam_msc *msc = *(struct mpam_msc **)dev_id; 2503 2504 return __mpam_irq_handler(irq, msc); 2505 } 2506 2507 static irqreturn_t mpam_spi_handler(int irq, void *dev_id) 2508 { 2509 struct mpam_msc *msc = dev_id; 2510 2511 return __mpam_irq_handler(irq, msc); 2512 } 2513 2514 static int mpam_register_irqs(void) 2515 { 2516 int err, irq; 2517 struct mpam_msc *msc; 2518 2519 lockdep_assert_cpus_held(); 2520 2521 guard(srcu)(&mpam_srcu); 2522 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 2523 srcu_read_lock_held(&mpam_srcu)) { 2524 irq = platform_get_irq_byname_optional(msc->pdev, "error"); 2525 if (irq <= 0) 2526 continue; 2527 2528 /* The MPAM spec says the interrupt can be SPI, PPI or LPI */ 2529 /* We anticipate sharing the interrupt with other MSCs */ 2530 if (irq_is_percpu(irq)) { 2531 err = request_percpu_irq(irq, &mpam_ppi_handler, 2532 "mpam:msc:error", 2533 msc->error_dev_id); 2534 if (err) 2535 return err; 2536 2537 msc->reenable_error_ppi = irq; 2538 smp_call_function_many(&msc->accessibility, 2539 &_enable_percpu_irq, &irq, 2540 true); 2541 } else { 2542 err = devm_request_irq(&msc->pdev->dev, irq, 2543 &mpam_spi_handler, IRQF_SHARED, 2544 "mpam:msc:error", msc); 2545 if (err) 2546 return err; 2547 } 2548 2549 mutex_lock(&msc->error_irq_lock); 2550 msc->error_irq_req = true; 2551 mpam_touch_msc(msc, mpam_enable_msc_ecr, msc); 2552 msc->error_irq_hw_enabled = true; 2553 mutex_unlock(&msc->error_irq_lock); 2554 } 2555 2556 return 0; 2557 } 2558 2559 static void mpam_unregister_irqs(void) 2560 { 2561 int irq; 2562 struct mpam_msc *msc; 2563 2564 guard(cpus_read_lock)(); 2565 guard(srcu)(&mpam_srcu); 2566 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 2567 srcu_read_lock_held(&mpam_srcu)) { 2568 irq = platform_get_irq_byname_optional(msc->pdev, "error"); 2569 if (irq <= 0) 2570 continue; 2571 2572 mutex_lock(&msc->error_irq_lock); 2573 if (msc->error_irq_hw_enabled) { 2574 mpam_touch_msc(msc, mpam_disable_msc_ecr, msc); 2575 msc->error_irq_hw_enabled = false; 2576 } 2577 2578 if (msc->error_irq_req) { 2579 if (irq_is_percpu(irq)) { 2580 msc->reenable_error_ppi = 0; 2581 free_percpu_irq(irq, msc->error_dev_id); 2582 } else { 2583 devm_free_irq(&msc->pdev->dev, irq, msc); 2584 } 2585 msc->error_irq_req = false; 2586 } 2587 mutex_unlock(&msc->error_irq_lock); 2588 } 2589 } 2590 2591 static void __destroy_component_cfg(struct mpam_component *comp) 2592 { 2593 struct mpam_msc *msc; 2594 struct mpam_vmsc *vmsc; 2595 struct mpam_msc_ris *ris; 2596 2597 lockdep_assert_held(&mpam_list_lock); 2598 2599 if (!comp->cfg) 2600 return; 2601 2602 add_to_garbage(comp->cfg); 2603 list_for_each_entry(vmsc, &comp->vmsc, comp_list) { 2604 msc = vmsc->msc; 2605 2606 if (mpam_mon_sel_lock(msc)) { 2607 list_for_each_entry(ris, &vmsc->ris, vmsc_list) 2608 add_to_garbage(ris->mbwu_state); 2609 mpam_mon_sel_unlock(msc); 2610 } 2611 } 2612 } 2613 2614 static void mpam_reset_component_cfg(struct mpam_component *comp) 2615 { 2616 int i; 2617 struct mpam_props *cprops = &comp->class->props; 2618 2619 mpam_assert_partid_sizes_fixed(); 2620 2621 if (!comp->cfg) 2622 return; 2623 2624 for (i = 0; i <= mpam_partid_max; i++) { 2625 comp->cfg[i] = (struct mpam_config) {}; 2626 if (cprops->cpbm_wd) 2627 comp->cfg[i].cpbm = GENMASK(cprops->cpbm_wd - 1, 0); 2628 if (cprops->mbw_pbm_bits) 2629 comp->cfg[i].mbw_pbm = GENMASK(cprops->mbw_pbm_bits - 1, 0); 2630 if (cprops->bwa_wd) 2631 comp->cfg[i].mbw_max = GENMASK(15, 16 - cprops->bwa_wd); 2632 } 2633 } 2634 2635 static int __allocate_component_cfg(struct mpam_component *comp) 2636 { 2637 struct mpam_vmsc *vmsc; 2638 2639 mpam_assert_partid_sizes_fixed(); 2640 2641 if (comp->cfg) 2642 return 0; 2643 2644 comp->cfg = kzalloc_objs(*comp->cfg, mpam_partid_max + 1); 2645 if (!comp->cfg) 2646 return -ENOMEM; 2647 2648 /* 2649 * The array is free()d in one go, so only cfg[0]'s structure needs 2650 * to be initialised. 2651 */ 2652 init_garbage(&comp->cfg[0].garbage); 2653 2654 mpam_reset_component_cfg(comp); 2655 2656 list_for_each_entry(vmsc, &comp->vmsc, comp_list) { 2657 struct mpam_msc *msc; 2658 struct mpam_msc_ris *ris; 2659 struct msmon_mbwu_state *mbwu_state; 2660 2661 if (!vmsc->props.num_mbwu_mon) 2662 continue; 2663 2664 msc = vmsc->msc; 2665 list_for_each_entry(ris, &vmsc->ris, vmsc_list) { 2666 if (!ris->props.num_mbwu_mon) 2667 continue; 2668 2669 mbwu_state = kzalloc_objs(*ris->mbwu_state, 2670 ris->props.num_mbwu_mon); 2671 if (!mbwu_state) { 2672 __destroy_component_cfg(comp); 2673 return -ENOMEM; 2674 } 2675 2676 init_garbage(&mbwu_state[0].garbage); 2677 2678 if (mpam_mon_sel_lock(msc)) { 2679 ris->mbwu_state = mbwu_state; 2680 mpam_mon_sel_unlock(msc); 2681 } 2682 } 2683 } 2684 2685 return 0; 2686 } 2687 2688 static int mpam_allocate_config(void) 2689 { 2690 struct mpam_class *class; 2691 struct mpam_component *comp; 2692 2693 lockdep_assert_held(&mpam_list_lock); 2694 2695 list_for_each_entry(class, &mpam_classes, classes_list) { 2696 list_for_each_entry(comp, &class->components, class_list) { 2697 int err = __allocate_component_cfg(comp); 2698 if (err) 2699 return err; 2700 } 2701 } 2702 2703 return 0; 2704 } 2705 2706 static void mpam_enable_once(void) 2707 { 2708 int err; 2709 2710 /* 2711 * Once the cpuhp callbacks have been changed, mpam_partid_max can no 2712 * longer change. 2713 */ 2714 spin_lock(&partid_max_lock); 2715 partid_max_published = true; 2716 spin_unlock(&partid_max_lock); 2717 2718 /* 2719 * If all the MSC have been probed, enabling the IRQs happens next. 2720 * That involves cross-calling to a CPU that can reach the MSC, and 2721 * the locks must be taken in this order: 2722 */ 2723 cpus_read_lock(); 2724 mutex_lock(&mpam_list_lock); 2725 do { 2726 mpam_enable_merge_features(&mpam_classes); 2727 2728 err = mpam_register_irqs(); 2729 if (err) { 2730 pr_warn("Failed to register irqs: %d\n", err); 2731 break; 2732 } 2733 2734 err = mpam_allocate_config(); 2735 if (err) { 2736 pr_err("Failed to allocate configuration arrays.\n"); 2737 break; 2738 } 2739 } while (0); 2740 mutex_unlock(&mpam_list_lock); 2741 cpus_read_unlock(); 2742 2743 if (!err) { 2744 err = mpam_resctrl_setup(); 2745 if (err) 2746 pr_err("Failed to initialise resctrl: %d\n", err); 2747 } 2748 2749 if (err) { 2750 mpam_disable_reason = "Failed to enable."; 2751 schedule_work(&mpam_broken_work); 2752 return; 2753 } 2754 2755 static_branch_enable(&mpam_enabled); 2756 mpam_resctrl_enabled = true; 2757 mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline, 2758 "mpam:online"); 2759 2760 /* Use printk() to avoid the pr_fmt adding the function name. */ 2761 printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n", 2762 mpam_partid_max + 1, mpam_pmg_max + 1); 2763 } 2764 2765 static void mpam_reset_component_locked(struct mpam_component *comp) 2766 { 2767 struct mpam_vmsc *vmsc; 2768 2769 lockdep_assert_cpus_held(); 2770 mpam_assert_partid_sizes_fixed(); 2771 2772 mpam_reset_component_cfg(comp); 2773 2774 guard(srcu)(&mpam_srcu); 2775 list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list, 2776 srcu_read_lock_held(&mpam_srcu)) { 2777 struct mpam_msc *msc = vmsc->msc; 2778 struct mpam_msc_ris *ris; 2779 2780 list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list, 2781 srcu_read_lock_held(&mpam_srcu)) { 2782 if (!ris->in_reset_state) 2783 mpam_touch_msc(msc, mpam_reset_ris, ris); 2784 ris->in_reset_state = true; 2785 } 2786 } 2787 } 2788 2789 void mpam_reset_class_locked(struct mpam_class *class) 2790 { 2791 struct mpam_component *comp; 2792 2793 lockdep_assert_cpus_held(); 2794 2795 guard(srcu)(&mpam_srcu); 2796 list_for_each_entry_srcu(comp, &class->components, class_list, 2797 srcu_read_lock_held(&mpam_srcu)) 2798 mpam_reset_component_locked(comp); 2799 } 2800 2801 static void mpam_reset_class(struct mpam_class *class) 2802 { 2803 cpus_read_lock(); 2804 mpam_reset_class_locked(class); 2805 cpus_read_unlock(); 2806 } 2807 2808 /* 2809 * Called in response to an error IRQ. 2810 * All of MPAMs errors indicate a software bug, restore any modified 2811 * controls to their reset values. 2812 */ 2813 void mpam_disable(struct work_struct *ignored) 2814 { 2815 int idx; 2816 bool do_resctrl_exit; 2817 struct mpam_class *class; 2818 struct mpam_msc *msc, *tmp; 2819 2820 if (mpam_is_enabled()) 2821 static_branch_disable(&mpam_enabled); 2822 2823 mutex_lock(&mpam_cpuhp_state_lock); 2824 if (mpam_cpuhp_state) { 2825 cpuhp_remove_state(mpam_cpuhp_state); 2826 mpam_cpuhp_state = 0; 2827 } 2828 2829 /* 2830 * Removing the cpuhp state called mpam_cpu_offline() and told resctrl 2831 * all the CPUs are offline. 2832 */ 2833 do_resctrl_exit = mpam_resctrl_enabled; 2834 mpam_resctrl_enabled = false; 2835 mutex_unlock(&mpam_cpuhp_state_lock); 2836 2837 if (do_resctrl_exit) 2838 mpam_resctrl_exit(); 2839 2840 mpam_unregister_irqs(); 2841 2842 idx = srcu_read_lock(&mpam_srcu); 2843 list_for_each_entry_srcu(class, &mpam_classes, classes_list, 2844 srcu_read_lock_held(&mpam_srcu)) { 2845 mpam_reset_class(class); 2846 if (do_resctrl_exit) 2847 mpam_resctrl_teardown_class(class); 2848 } 2849 srcu_read_unlock(&mpam_srcu, idx); 2850 2851 mutex_lock(&mpam_list_lock); 2852 list_for_each_entry_safe(msc, tmp, &mpam_all_msc, all_msc_list) 2853 mpam_msc_destroy(msc); 2854 mutex_unlock(&mpam_list_lock); 2855 mpam_free_garbage(); 2856 2857 pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason); 2858 } 2859 2860 /* 2861 * Enable mpam once all devices have been probed. 2862 * Scheduled by mpam_discovery_cpu_online() once all devices have been created. 2863 * Also scheduled when new devices are probed when new CPUs come online. 2864 */ 2865 void mpam_enable(struct work_struct *work) 2866 { 2867 static atomic_t once; 2868 struct mpam_msc *msc; 2869 bool all_devices_probed = true; 2870 2871 /* Have we probed all the hw devices? */ 2872 guard(srcu)(&mpam_srcu); 2873 list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list, 2874 srcu_read_lock_held(&mpam_srcu)) { 2875 mutex_lock(&msc->probe_lock); 2876 if (!msc->probed) 2877 all_devices_probed = false; 2878 mutex_unlock(&msc->probe_lock); 2879 2880 if (!all_devices_probed) 2881 break; 2882 } 2883 2884 if (all_devices_probed && !atomic_fetch_inc(&once)) 2885 mpam_enable_once(); 2886 } 2887 2888 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \ 2889 if (mpam_has_feature(feature, newcfg) && \ 2890 (newcfg)->member != (cfg)->member) { \ 2891 (cfg)->member = (newcfg)->member; \ 2892 mpam_set_feature(feature, cfg); \ 2893 \ 2894 (changes) = true; \ 2895 } \ 2896 } while (0) 2897 2898 static bool mpam_update_config(struct mpam_config *cfg, 2899 const struct mpam_config *newcfg) 2900 { 2901 bool has_changes = false; 2902 2903 maybe_update_config(cfg, mpam_feat_cpor_part, newcfg, cpbm, has_changes); 2904 maybe_update_config(cfg, mpam_feat_mbw_part, newcfg, mbw_pbm, has_changes); 2905 maybe_update_config(cfg, mpam_feat_mbw_max, newcfg, mbw_max, has_changes); 2906 2907 return has_changes; 2908 } 2909 2910 int mpam_apply_config(struct mpam_component *comp, u16 partid, 2911 struct mpam_config *cfg) 2912 { 2913 struct mpam_write_config_arg arg; 2914 struct mpam_msc_ris *ris; 2915 struct mpam_vmsc *vmsc; 2916 struct mpam_msc *msc; 2917 2918 lockdep_assert_cpus_held(); 2919 2920 /* Don't pass in the current config! */ 2921 WARN_ON_ONCE(&comp->cfg[partid] == cfg); 2922 2923 if (!mpam_update_config(&comp->cfg[partid], cfg)) 2924 return 0; 2925 2926 arg.comp = comp; 2927 arg.partid = partid; 2928 2929 guard(srcu)(&mpam_srcu); 2930 list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list, 2931 srcu_read_lock_held(&mpam_srcu)) { 2932 msc = vmsc->msc; 2933 2934 mutex_lock(&msc->cfg_lock); 2935 list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list, 2936 srcu_read_lock_held(&mpam_srcu)) { 2937 arg.ris = ris; 2938 mpam_touch_msc(msc, __write_config, &arg); 2939 ris->in_reset_state = false; 2940 } 2941 mutex_unlock(&msc->cfg_lock); 2942 } 2943 2944 return 0; 2945 } 2946 2947 static int __init mpam_msc_driver_init(void) 2948 { 2949 if (!system_supports_mpam()) 2950 return -EOPNOTSUPP; 2951 2952 init_srcu_struct(&mpam_srcu); 2953 2954 fw_num_msc = acpi_mpam_count_msc(); 2955 if (fw_num_msc <= 0) { 2956 pr_err("No MSC devices found in firmware\n"); 2957 return -EINVAL; 2958 } 2959 2960 return platform_driver_register(&mpam_msc_driver); 2961 } 2962 2963 /* Must occur after arm64_mpam_register_cpus() from arch_initcall() */ 2964 subsys_initcall(mpam_msc_driver_init); 2965 2966 #ifdef CONFIG_MPAM_KUNIT_TEST 2967 #include "test_mpam_devices.c" 2968 #endif 2969