1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ARM DynamIQ Shared Unit (DSU) PMU driver 4 * 5 * Copyright (C) ARM Limited, 2017. 6 * 7 * Based on ARM CCI-PMU, ARMv8 PMU-v3 drivers. 8 */ 9 10 #define PMUNAME "arm_dsu" 11 #define DRVNAME PMUNAME "_pmu" 12 #define pr_fmt(fmt) DRVNAME ": " fmt 13 14 #include <linux/acpi.h> 15 #include <linux/bitmap.h> 16 #include <linux/bitops.h> 17 #include <linux/bug.h> 18 #include <linux/cpumask.h> 19 #include <linux/device.h> 20 #include <linux/interrupt.h> 21 #include <linux/kernel.h> 22 #include <linux/module.h> 23 #include <linux/of.h> 24 #include <linux/perf_event.h> 25 #include <linux/platform_device.h> 26 #include <linux/spinlock.h> 27 #include <linux/smp.h> 28 #include <linux/sysfs.h> 29 #include <linux/types.h> 30 31 #include <asm/arm_dsu_pmu.h> 32 #include <asm/local64.h> 33 34 /* PMU event codes */ 35 #define DSU_PMU_EVT_CYCLES 0x11 36 #define DSU_PMU_EVT_CHAIN 0x1e 37 38 #define DSU_PMU_MAX_COMMON_EVENTS 0x40 39 40 #define DSU_PMU_MAX_HW_CNTRS 32 41 #define DSU_PMU_HW_COUNTER_MASK (DSU_PMU_MAX_HW_CNTRS - 1) 42 43 #define CLUSTERPMCR_E BIT(0) 44 #define CLUSTERPMCR_P BIT(1) 45 #define CLUSTERPMCR_C BIT(2) 46 #define CLUSTERPMCR_N_SHIFT 11 47 #define CLUSTERPMCR_N_MASK 0x1f 48 #define CLUSTERPMCR_IDCODE_SHIFT 16 49 #define CLUSTERPMCR_IDCODE_MASK 0xff 50 #define CLUSTERPMCR_IMP_SHIFT 24 51 #define CLUSTERPMCR_IMP_MASK 0xff 52 #define CLUSTERPMCR_RES_MASK 0x7e8 53 #define CLUSTERPMCR_RES_VAL 0x40 54 55 #define DSU_ACTIVE_CPU_MASK 0x0 56 #define DSU_ASSOCIATED_CPU_MASK 0x1 57 58 /* 59 * We use the index of the counters as they appear in the counter 60 * bit maps in the PMU registers (e.g CLUSTERPMSELR). 61 * i.e, 62 * counter 0 - Bit 0 63 * counter 1 - Bit 1 64 * ... 65 * Cycle counter - Bit 31 66 */ 67 #define DSU_PMU_IDX_CYCLE_COUNTER 31 68 69 /* All event counters are 32bit, with a 64bit Cycle counter */ 70 #define DSU_PMU_COUNTER_WIDTH(idx) \ 71 (((idx) == DSU_PMU_IDX_CYCLE_COUNTER) ? 64 : 32) 72 73 #define DSU_PMU_COUNTER_MASK(idx) \ 74 GENMASK_ULL((DSU_PMU_COUNTER_WIDTH((idx)) - 1), 0) 75 76 #define DSU_EXT_ATTR(_name, _func, _config) \ 77 (&((struct dev_ext_attribute[]) { \ 78 { \ 79 .attr = __ATTR(_name, 0444, _func, NULL), \ 80 .var = (void *)_config \ 81 } \ 82 })[0].attr.attr) 83 84 #define DSU_EVENT_ATTR(_name, _config) \ 85 DSU_EXT_ATTR(_name, dsu_pmu_sysfs_event_show, (unsigned long)_config) 86 87 #define DSU_FORMAT_ATTR(_name, _config) \ 88 DSU_EXT_ATTR(_name, dsu_pmu_sysfs_format_show, (char *)_config) 89 90 #define DSU_CPUMASK_ATTR(_name, _config) \ 91 DSU_EXT_ATTR(_name, dsu_pmu_cpumask_show, (unsigned long)_config) 92 93 struct dsu_hw_events { 94 DECLARE_BITMAP(used_mask, DSU_PMU_MAX_HW_CNTRS); 95 struct perf_event *events[DSU_PMU_MAX_HW_CNTRS]; 96 }; 97 98 /* 99 * struct dsu_pmu - DSU PMU descriptor 100 * 101 * @pmu_lock : Protects accesses to DSU PMU register from normal vs 102 * interrupt handler contexts. 103 * @hw_events : Holds the event counter state. 104 * @associated_cpus : CPUs attached to the DSU. 105 * @active_cpu : CPU to which the PMU is bound for accesses. 106 * @cpuhp_node : Node for CPU hotplug notifier link. 107 * @num_counters : Number of event counters implemented by the PMU, 108 * excluding the cycle counter. 109 * @irq : Interrupt line for counter overflow. 110 * @cpmceid_bitmap : Bitmap for the availability of architected common 111 * events (event_code < 0x40). 112 */ 113 struct dsu_pmu { 114 struct pmu pmu; 115 struct device *dev; 116 raw_spinlock_t pmu_lock; 117 struct dsu_hw_events hw_events; 118 cpumask_t associated_cpus; 119 cpumask_t active_cpu; 120 struct hlist_node cpuhp_node; 121 s8 num_counters; 122 int irq; 123 DECLARE_BITMAP(cpmceid_bitmap, DSU_PMU_MAX_COMMON_EVENTS); 124 }; 125 126 static unsigned long dsu_pmu_cpuhp_state; 127 128 static inline struct dsu_pmu *to_dsu_pmu(struct pmu *pmu) 129 { 130 return container_of(pmu, struct dsu_pmu, pmu); 131 } 132 133 static ssize_t dsu_pmu_sysfs_event_show(struct device *dev, 134 struct device_attribute *attr, 135 char *buf) 136 { 137 struct dev_ext_attribute *eattr = container_of(attr, 138 struct dev_ext_attribute, attr); 139 return sysfs_emit(buf, "event=0x%lx\n", (unsigned long)eattr->var); 140 } 141 142 static ssize_t dsu_pmu_sysfs_format_show(struct device *dev, 143 struct device_attribute *attr, 144 char *buf) 145 { 146 struct dev_ext_attribute *eattr = container_of(attr, 147 struct dev_ext_attribute, attr); 148 return sysfs_emit(buf, "%s\n", (char *)eattr->var); 149 } 150 151 static ssize_t dsu_pmu_cpumask_show(struct device *dev, 152 struct device_attribute *attr, 153 char *buf) 154 { 155 struct pmu *pmu = dev_get_drvdata(dev); 156 struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu); 157 struct dev_ext_attribute *eattr = container_of(attr, 158 struct dev_ext_attribute, attr); 159 unsigned long mask_id = (unsigned long)eattr->var; 160 const cpumask_t *cpumask; 161 162 switch (mask_id) { 163 case DSU_ACTIVE_CPU_MASK: 164 cpumask = &dsu_pmu->active_cpu; 165 break; 166 case DSU_ASSOCIATED_CPU_MASK: 167 cpumask = &dsu_pmu->associated_cpus; 168 break; 169 default: 170 return 0; 171 } 172 return cpumap_print_to_pagebuf(true, buf, cpumask); 173 } 174 175 static struct attribute *dsu_pmu_format_attrs[] = { 176 DSU_FORMAT_ATTR(event, "config:0-31"), 177 NULL, 178 }; 179 180 static const struct attribute_group dsu_pmu_format_attr_group = { 181 .name = "format", 182 .attrs = dsu_pmu_format_attrs, 183 }; 184 185 static struct attribute *dsu_pmu_event_attrs[] = { 186 DSU_EVENT_ATTR(cycles, 0x11), 187 DSU_EVENT_ATTR(bus_access, 0x19), 188 DSU_EVENT_ATTR(memory_error, 0x1a), 189 DSU_EVENT_ATTR(bus_cycles, 0x1d), 190 DSU_EVENT_ATTR(l3d_cache_allocate, 0x29), 191 DSU_EVENT_ATTR(l3d_cache_refill, 0x2a), 192 DSU_EVENT_ATTR(l3d_cache, 0x2b), 193 DSU_EVENT_ATTR(l3d_cache_wb, 0x2c), 194 NULL, 195 }; 196 197 static umode_t 198 dsu_pmu_event_attr_is_visible(struct kobject *kobj, struct attribute *attr, 199 int unused) 200 { 201 struct pmu *pmu = dev_get_drvdata(kobj_to_dev(kobj)); 202 struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu); 203 struct dev_ext_attribute *eattr = container_of(attr, 204 struct dev_ext_attribute, attr.attr); 205 unsigned long evt = (unsigned long)eattr->var; 206 207 return test_bit(evt, dsu_pmu->cpmceid_bitmap) ? attr->mode : 0; 208 } 209 210 static const struct attribute_group dsu_pmu_events_attr_group = { 211 .name = "events", 212 .attrs = dsu_pmu_event_attrs, 213 .is_visible = dsu_pmu_event_attr_is_visible, 214 }; 215 216 static struct attribute *dsu_pmu_cpumask_attrs[] = { 217 DSU_CPUMASK_ATTR(cpumask, DSU_ACTIVE_CPU_MASK), 218 DSU_CPUMASK_ATTR(associated_cpus, DSU_ASSOCIATED_CPU_MASK), 219 NULL, 220 }; 221 222 static const struct attribute_group dsu_pmu_cpumask_attr_group = { 223 .attrs = dsu_pmu_cpumask_attrs, 224 }; 225 226 static const struct attribute_group *dsu_pmu_attr_groups[] = { 227 &dsu_pmu_cpumask_attr_group, 228 &dsu_pmu_events_attr_group, 229 &dsu_pmu_format_attr_group, 230 NULL, 231 }; 232 233 static inline bool dsu_pmu_counter_valid(struct dsu_pmu *dsu_pmu, u32 idx) 234 { 235 return (idx < dsu_pmu->num_counters) || 236 (idx == DSU_PMU_IDX_CYCLE_COUNTER); 237 } 238 239 static inline u64 dsu_pmu_read_counter(struct perf_event *event) 240 { 241 u64 val; 242 unsigned long flags; 243 struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu); 244 int idx = event->hw.idx; 245 246 if (WARN_ON(!cpumask_test_cpu(smp_processor_id(), 247 &dsu_pmu->associated_cpus))) 248 return 0; 249 250 if (!dsu_pmu_counter_valid(dsu_pmu, idx)) { 251 dev_err(event->pmu->dev, 252 "Trying reading invalid counter %d\n", idx); 253 return 0; 254 } 255 256 raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags); 257 if (idx == DSU_PMU_IDX_CYCLE_COUNTER) 258 val = __dsu_pmu_read_pmccntr(); 259 else 260 val = __dsu_pmu_read_counter(idx); 261 raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags); 262 263 return val; 264 } 265 266 static void dsu_pmu_write_counter(struct perf_event *event, u64 val) 267 { 268 unsigned long flags; 269 struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu); 270 int idx = event->hw.idx; 271 272 if (WARN_ON(!cpumask_test_cpu(smp_processor_id(), 273 &dsu_pmu->associated_cpus))) 274 return; 275 276 if (!dsu_pmu_counter_valid(dsu_pmu, idx)) { 277 dev_err(event->pmu->dev, 278 "writing to invalid counter %d\n", idx); 279 return; 280 } 281 282 raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags); 283 if (idx == DSU_PMU_IDX_CYCLE_COUNTER) 284 __dsu_pmu_write_pmccntr(val); 285 else 286 __dsu_pmu_write_counter(idx, val); 287 raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags); 288 } 289 290 static int dsu_pmu_get_event_idx(struct dsu_hw_events *hw_events, 291 struct perf_event *event) 292 { 293 int idx; 294 unsigned long evtype = event->attr.config; 295 struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu); 296 unsigned long *used_mask = hw_events->used_mask; 297 298 if (evtype == DSU_PMU_EVT_CYCLES) { 299 if (test_and_set_bit(DSU_PMU_IDX_CYCLE_COUNTER, used_mask)) 300 return -EAGAIN; 301 return DSU_PMU_IDX_CYCLE_COUNTER; 302 } 303 304 idx = find_first_zero_bit(used_mask, dsu_pmu->num_counters); 305 if (idx >= dsu_pmu->num_counters) 306 return -EAGAIN; 307 set_bit(idx, hw_events->used_mask); 308 return idx; 309 } 310 311 static void dsu_pmu_enable_counter(struct dsu_pmu *dsu_pmu, int idx) 312 { 313 __dsu_pmu_counter_interrupt_enable(idx); 314 __dsu_pmu_enable_counter(idx); 315 } 316 317 static void dsu_pmu_disable_counter(struct dsu_pmu *dsu_pmu, int idx) 318 { 319 __dsu_pmu_disable_counter(idx); 320 __dsu_pmu_counter_interrupt_disable(idx); 321 } 322 323 static inline void dsu_pmu_set_event(struct dsu_pmu *dsu_pmu, 324 struct perf_event *event) 325 { 326 int idx = event->hw.idx; 327 unsigned long flags; 328 329 if (!dsu_pmu_counter_valid(dsu_pmu, idx)) { 330 dev_err(event->pmu->dev, 331 "Trying to set invalid counter %d\n", idx); 332 return; 333 } 334 335 raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags); 336 __dsu_pmu_set_event(idx, event->hw.config_base); 337 raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags); 338 } 339 340 static void dsu_pmu_event_update(struct perf_event *event) 341 { 342 struct hw_perf_event *hwc = &event->hw; 343 u64 delta, prev_count, new_count; 344 345 do { 346 /* We may also be called from the irq handler */ 347 prev_count = local64_read(&hwc->prev_count); 348 new_count = dsu_pmu_read_counter(event); 349 } while (local64_cmpxchg(&hwc->prev_count, prev_count, new_count) != 350 prev_count); 351 delta = (new_count - prev_count) & DSU_PMU_COUNTER_MASK(hwc->idx); 352 local64_add(delta, &event->count); 353 } 354 355 static void dsu_pmu_read(struct perf_event *event) 356 { 357 dsu_pmu_event_update(event); 358 } 359 360 static inline u32 dsu_pmu_get_reset_overflow(void) 361 { 362 return __dsu_pmu_get_reset_overflow(); 363 } 364 365 /* 366 * dsu_pmu_set_event_period: Set the period for the counter. 367 * 368 * All DSU PMU event counters, except the cycle counter are 32bit 369 * counters. To handle cases of extreme interrupt latency, we program 370 * the counter with half of the max count for the counters. 371 */ 372 static void dsu_pmu_set_event_period(struct perf_event *event) 373 { 374 int idx = event->hw.idx; 375 u64 val = DSU_PMU_COUNTER_MASK(idx) >> 1; 376 377 local64_set(&event->hw.prev_count, val); 378 dsu_pmu_write_counter(event, val); 379 } 380 381 static irqreturn_t dsu_pmu_handle_irq(int irq_num, void *dev) 382 { 383 int i; 384 bool handled = false; 385 struct dsu_pmu *dsu_pmu = dev; 386 struct dsu_hw_events *hw_events = &dsu_pmu->hw_events; 387 unsigned long overflow; 388 389 overflow = dsu_pmu_get_reset_overflow(); 390 if (!overflow) 391 return IRQ_NONE; 392 393 for_each_set_bit(i, &overflow, DSU_PMU_MAX_HW_CNTRS) { 394 struct perf_event *event = hw_events->events[i]; 395 396 if (!event) 397 continue; 398 dsu_pmu_event_update(event); 399 dsu_pmu_set_event_period(event); 400 handled = true; 401 } 402 403 return IRQ_RETVAL(handled); 404 } 405 406 static void dsu_pmu_start(struct perf_event *event, int pmu_flags) 407 { 408 struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu); 409 410 /* We always reprogram the counter */ 411 if (pmu_flags & PERF_EF_RELOAD) 412 WARN_ON(!(event->hw.state & PERF_HES_UPTODATE)); 413 dsu_pmu_set_event_period(event); 414 if (event->hw.idx != DSU_PMU_IDX_CYCLE_COUNTER) 415 dsu_pmu_set_event(dsu_pmu, event); 416 event->hw.state = 0; 417 dsu_pmu_enable_counter(dsu_pmu, event->hw.idx); 418 } 419 420 static void dsu_pmu_stop(struct perf_event *event, int pmu_flags) 421 { 422 struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu); 423 424 if (event->hw.state & PERF_HES_STOPPED) 425 return; 426 dsu_pmu_disable_counter(dsu_pmu, event->hw.idx); 427 dsu_pmu_event_update(event); 428 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; 429 } 430 431 static int dsu_pmu_add(struct perf_event *event, int flags) 432 { 433 struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu); 434 struct dsu_hw_events *hw_events = &dsu_pmu->hw_events; 435 struct hw_perf_event *hwc = &event->hw; 436 int idx; 437 438 if (WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), 439 &dsu_pmu->associated_cpus))) 440 return -ENOENT; 441 442 idx = dsu_pmu_get_event_idx(hw_events, event); 443 if (idx < 0) 444 return idx; 445 446 hwc->idx = idx; 447 hw_events->events[idx] = event; 448 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; 449 450 if (flags & PERF_EF_START) 451 dsu_pmu_start(event, PERF_EF_RELOAD); 452 453 perf_event_update_userpage(event); 454 return 0; 455 } 456 457 static void dsu_pmu_del(struct perf_event *event, int flags) 458 { 459 struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu); 460 struct dsu_hw_events *hw_events = &dsu_pmu->hw_events; 461 struct hw_perf_event *hwc = &event->hw; 462 int idx = hwc->idx; 463 464 dsu_pmu_stop(event, PERF_EF_UPDATE); 465 hw_events->events[idx] = NULL; 466 clear_bit(idx, hw_events->used_mask); 467 perf_event_update_userpage(event); 468 } 469 470 static void dsu_pmu_enable(struct pmu *pmu) 471 { 472 u32 pmcr; 473 unsigned long flags; 474 struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu); 475 476 /* If no counters are added, skip enabling the PMU */ 477 if (bitmap_empty(dsu_pmu->hw_events.used_mask, DSU_PMU_MAX_HW_CNTRS)) 478 return; 479 480 raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags); 481 pmcr = __dsu_pmu_read_pmcr(); 482 pmcr |= CLUSTERPMCR_E; 483 __dsu_pmu_write_pmcr(pmcr); 484 raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags); 485 } 486 487 static void dsu_pmu_disable(struct pmu *pmu) 488 { 489 u32 pmcr; 490 unsigned long flags; 491 struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu); 492 493 raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags); 494 pmcr = __dsu_pmu_read_pmcr(); 495 pmcr &= ~CLUSTERPMCR_E; 496 __dsu_pmu_write_pmcr(pmcr); 497 raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags); 498 } 499 500 static bool dsu_pmu_validate_event(struct pmu *pmu, 501 struct dsu_hw_events *hw_events, 502 struct perf_event *event) 503 { 504 if (is_software_event(event)) 505 return true; 506 /* Reject groups spanning multiple HW PMUs. */ 507 if (event->pmu != pmu) 508 return false; 509 return dsu_pmu_get_event_idx(hw_events, event) >= 0; 510 } 511 512 /* 513 * Make sure the group of events can be scheduled at once 514 * on the PMU. 515 */ 516 static bool dsu_pmu_validate_group(struct perf_event *event) 517 { 518 struct perf_event *sibling, *leader = event->group_leader; 519 struct dsu_hw_events fake_hw; 520 521 if (event->group_leader == event) 522 return true; 523 524 memset(fake_hw.used_mask, 0, sizeof(fake_hw.used_mask)); 525 if (!dsu_pmu_validate_event(event->pmu, &fake_hw, leader)) 526 return false; 527 for_each_sibling_event(sibling, leader) { 528 if (!dsu_pmu_validate_event(event->pmu, &fake_hw, sibling)) 529 return false; 530 } 531 return dsu_pmu_validate_event(event->pmu, &fake_hw, event); 532 } 533 534 static int dsu_pmu_event_init(struct perf_event *event) 535 { 536 struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu); 537 538 if (event->attr.type != event->pmu->type) 539 return -ENOENT; 540 541 /* We don't support sampling */ 542 if (is_sampling_event(event)) { 543 dev_dbg(dsu_pmu->pmu.dev, "Can't support sampling events\n"); 544 return -EOPNOTSUPP; 545 } 546 547 /* We cannot support task bound events */ 548 if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK) { 549 dev_dbg(dsu_pmu->pmu.dev, "Can't support per-task counters\n"); 550 return -EINVAL; 551 } 552 553 if (has_branch_stack(event)) { 554 dev_dbg(dsu_pmu->pmu.dev, "Can't support filtering\n"); 555 return -EINVAL; 556 } 557 558 if (!cpumask_test_cpu(event->cpu, &dsu_pmu->associated_cpus)) { 559 dev_dbg(dsu_pmu->pmu.dev, 560 "Requested cpu is not associated with the DSU\n"); 561 return -EINVAL; 562 } 563 /* 564 * Choose the current active CPU to read the events. We don't want 565 * to migrate the event contexts, irq handling etc to the requested 566 * CPU. As long as the requested CPU is within the same DSU, we 567 * are fine. 568 */ 569 event->cpu = cpumask_first(&dsu_pmu->active_cpu); 570 if (event->cpu >= nr_cpu_ids) 571 return -EINVAL; 572 if (!dsu_pmu_validate_group(event)) 573 return -EINVAL; 574 575 event->hw.config_base = event->attr.config; 576 return 0; 577 } 578 579 static struct dsu_pmu *dsu_pmu_alloc(struct platform_device *pdev) 580 { 581 struct dsu_pmu *dsu_pmu; 582 583 dsu_pmu = devm_kzalloc(&pdev->dev, sizeof(*dsu_pmu), GFP_KERNEL); 584 if (!dsu_pmu) 585 return ERR_PTR(-ENOMEM); 586 587 raw_spin_lock_init(&dsu_pmu->pmu_lock); 588 /* 589 * Initialise the number of counters to -1, until we probe 590 * the real number on a connected CPU. 591 */ 592 dsu_pmu->num_counters = -1; 593 return dsu_pmu; 594 } 595 596 /* 597 * dsu_pmu_dt_get_cpus: Get the list of CPUs in the cluster 598 * from device tree. 599 */ 600 static int dsu_pmu_dt_get_cpus(struct device *dev, cpumask_t *mask) 601 { 602 int i = 0, n, cpu; 603 struct device_node *cpu_node; 604 605 n = of_count_phandle_with_args(dev->of_node, "cpus", NULL); 606 if (n <= 0) 607 return -ENODEV; 608 for (; i < n; i++) { 609 cpu_node = of_parse_phandle(dev->of_node, "cpus", i); 610 if (!cpu_node) 611 break; 612 cpu = of_cpu_node_to_id(cpu_node); 613 of_node_put(cpu_node); 614 /* 615 * We have to ignore the failures here and continue scanning 616 * the list to handle cases where the nr_cpus could be capped 617 * in the running kernel. 618 */ 619 if (cpu < 0) 620 continue; 621 cpumask_set_cpu(cpu, mask); 622 } 623 return 0; 624 } 625 626 /* 627 * dsu_pmu_acpi_get_cpus: Get the list of CPUs in the cluster 628 * from ACPI. 629 */ 630 static int dsu_pmu_acpi_get_cpus(struct device *dev, cpumask_t *mask) 631 { 632 #ifdef CONFIG_ACPI 633 struct acpi_device *parent_adev = acpi_dev_parent(ACPI_COMPANION(dev)); 634 int cpu; 635 636 /* 637 * A dsu pmu node is inside a cluster parent node along with cpu nodes. 638 * We need to find out all cpus that have the same parent with this pmu. 639 */ 640 for_each_possible_cpu(cpu) { 641 struct acpi_device *acpi_dev; 642 struct device *cpu_dev = get_cpu_device(cpu); 643 644 if (!cpu_dev) 645 continue; 646 647 acpi_dev = ACPI_COMPANION(cpu_dev); 648 if (acpi_dev && acpi_dev_parent(acpi_dev) == parent_adev) 649 cpumask_set_cpu(cpu, mask); 650 } 651 #endif 652 653 return 0; 654 } 655 656 /* 657 * dsu_pmu_probe_pmu: Probe the PMU details on a CPU in the cluster. 658 */ 659 static void dsu_pmu_probe_pmu(struct dsu_pmu *dsu_pmu) 660 { 661 u64 num_counters; 662 u32 cpmceid[2]; 663 664 num_counters = (__dsu_pmu_read_pmcr() >> CLUSTERPMCR_N_SHIFT) & 665 CLUSTERPMCR_N_MASK; 666 /* We can only support up to 31 independent counters */ 667 if (WARN_ON(num_counters > 31)) 668 num_counters = 31; 669 dsu_pmu->num_counters = num_counters; 670 if (!dsu_pmu->num_counters) 671 return; 672 cpmceid[0] = __dsu_pmu_read_pmceid(0); 673 cpmceid[1] = __dsu_pmu_read_pmceid(1); 674 bitmap_from_arr32(dsu_pmu->cpmceid_bitmap, cpmceid, 675 DSU_PMU_MAX_COMMON_EVENTS); 676 } 677 678 static void dsu_pmu_set_active_cpu(int cpu, struct dsu_pmu *dsu_pmu) 679 { 680 cpumask_set_cpu(cpu, &dsu_pmu->active_cpu); 681 if (irq_set_affinity(dsu_pmu->irq, &dsu_pmu->active_cpu)) 682 pr_warn("Failed to set irq affinity to %d\n", cpu); 683 } 684 685 /* 686 * dsu_pmu_init_pmu: Initialise the DSU PMU configurations if 687 * we haven't done it already. 688 */ 689 static void dsu_pmu_init_pmu(struct dsu_pmu *dsu_pmu) 690 { 691 if (dsu_pmu->num_counters == -1) 692 dsu_pmu_probe_pmu(dsu_pmu); 693 /* Reset the interrupt overflow mask */ 694 dsu_pmu_get_reset_overflow(); 695 } 696 697 static int dsu_pmu_device_probe(struct platform_device *pdev) 698 { 699 int irq, rc; 700 struct dsu_pmu *dsu_pmu; 701 struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev); 702 char *name; 703 static atomic_t pmu_idx = ATOMIC_INIT(-1); 704 705 dsu_pmu = dsu_pmu_alloc(pdev); 706 if (IS_ERR(dsu_pmu)) 707 return PTR_ERR(dsu_pmu); 708 709 if (is_of_node(fwnode)) 710 rc = dsu_pmu_dt_get_cpus(&pdev->dev, &dsu_pmu->associated_cpus); 711 else if (is_acpi_device_node(fwnode)) 712 rc = dsu_pmu_acpi_get_cpus(&pdev->dev, &dsu_pmu->associated_cpus); 713 else 714 return -ENOENT; 715 716 if (rc) { 717 dev_warn(&pdev->dev, "Failed to parse the CPUs\n"); 718 return rc; 719 } 720 721 irq = platform_get_irq(pdev, 0); 722 if (irq < 0) 723 return -EINVAL; 724 725 name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_%d", 726 PMUNAME, atomic_inc_return(&pmu_idx)); 727 if (!name) 728 return -ENOMEM; 729 rc = devm_request_irq(&pdev->dev, irq, dsu_pmu_handle_irq, 730 IRQF_NOBALANCING, name, dsu_pmu); 731 if (rc) { 732 dev_warn(&pdev->dev, "Failed to request IRQ %d\n", irq); 733 return rc; 734 } 735 736 dsu_pmu->irq = irq; 737 platform_set_drvdata(pdev, dsu_pmu); 738 rc = cpuhp_state_add_instance(dsu_pmu_cpuhp_state, 739 &dsu_pmu->cpuhp_node); 740 if (rc) 741 return rc; 742 743 dsu_pmu->pmu = (struct pmu) { 744 .task_ctx_nr = perf_invalid_context, 745 .parent = &pdev->dev, 746 .module = THIS_MODULE, 747 .pmu_enable = dsu_pmu_enable, 748 .pmu_disable = dsu_pmu_disable, 749 .event_init = dsu_pmu_event_init, 750 .add = dsu_pmu_add, 751 .del = dsu_pmu_del, 752 .start = dsu_pmu_start, 753 .stop = dsu_pmu_stop, 754 .read = dsu_pmu_read, 755 756 .attr_groups = dsu_pmu_attr_groups, 757 .capabilities = PERF_PMU_CAP_NO_EXCLUDE, 758 }; 759 760 rc = perf_pmu_register(&dsu_pmu->pmu, name, -1); 761 if (rc) { 762 cpuhp_state_remove_instance(dsu_pmu_cpuhp_state, 763 &dsu_pmu->cpuhp_node); 764 } 765 766 return rc; 767 } 768 769 static void dsu_pmu_device_remove(struct platform_device *pdev) 770 { 771 struct dsu_pmu *dsu_pmu = platform_get_drvdata(pdev); 772 773 perf_pmu_unregister(&dsu_pmu->pmu); 774 cpuhp_state_remove_instance(dsu_pmu_cpuhp_state, &dsu_pmu->cpuhp_node); 775 } 776 777 static const struct of_device_id dsu_pmu_of_match[] = { 778 { .compatible = "arm,dsu-pmu", }, 779 {}, 780 }; 781 MODULE_DEVICE_TABLE(of, dsu_pmu_of_match); 782 783 #ifdef CONFIG_ACPI 784 static const struct acpi_device_id dsu_pmu_acpi_match[] = { 785 { "ARMHD500", 0}, 786 {}, 787 }; 788 MODULE_DEVICE_TABLE(acpi, dsu_pmu_acpi_match); 789 #endif 790 791 static struct platform_driver dsu_pmu_driver = { 792 .driver = { 793 .name = DRVNAME, 794 .of_match_table = of_match_ptr(dsu_pmu_of_match), 795 .acpi_match_table = ACPI_PTR(dsu_pmu_acpi_match), 796 .suppress_bind_attrs = true, 797 }, 798 .probe = dsu_pmu_device_probe, 799 .remove_new = dsu_pmu_device_remove, 800 }; 801 802 static int dsu_pmu_cpu_online(unsigned int cpu, struct hlist_node *node) 803 { 804 struct dsu_pmu *dsu_pmu = hlist_entry_safe(node, struct dsu_pmu, 805 cpuhp_node); 806 807 if (!cpumask_test_cpu(cpu, &dsu_pmu->associated_cpus)) 808 return 0; 809 810 /* If the PMU is already managed, there is nothing to do */ 811 if (!cpumask_empty(&dsu_pmu->active_cpu)) 812 return 0; 813 814 dsu_pmu_init_pmu(dsu_pmu); 815 dsu_pmu_set_active_cpu(cpu, dsu_pmu); 816 817 return 0; 818 } 819 820 static int dsu_pmu_cpu_teardown(unsigned int cpu, struct hlist_node *node) 821 { 822 struct dsu_pmu *dsu_pmu; 823 unsigned int dst; 824 825 dsu_pmu = hlist_entry_safe(node, struct dsu_pmu, cpuhp_node); 826 827 if (!cpumask_test_and_clear_cpu(cpu, &dsu_pmu->active_cpu)) 828 return 0; 829 830 dst = cpumask_any_and_but(&dsu_pmu->associated_cpus, 831 cpu_online_mask, cpu); 832 /* If there are no active CPUs in the DSU, leave IRQ disabled */ 833 if (dst >= nr_cpu_ids) 834 return 0; 835 836 perf_pmu_migrate_context(&dsu_pmu->pmu, cpu, dst); 837 dsu_pmu_set_active_cpu(dst, dsu_pmu); 838 839 return 0; 840 } 841 842 static int __init dsu_pmu_init(void) 843 { 844 int ret; 845 846 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, 847 DRVNAME, 848 dsu_pmu_cpu_online, 849 dsu_pmu_cpu_teardown); 850 if (ret < 0) 851 return ret; 852 dsu_pmu_cpuhp_state = ret; 853 ret = platform_driver_register(&dsu_pmu_driver); 854 if (ret) 855 cpuhp_remove_multi_state(dsu_pmu_cpuhp_state); 856 857 return ret; 858 } 859 860 static void __exit dsu_pmu_exit(void) 861 { 862 platform_driver_unregister(&dsu_pmu_driver); 863 cpuhp_remove_multi_state(dsu_pmu_cpuhp_state); 864 } 865 866 module_init(dsu_pmu_init); 867 module_exit(dsu_pmu_exit); 868 869 MODULE_DESCRIPTION("Perf driver for ARM DynamIQ Shared Unit"); 870 MODULE_AUTHOR("Suzuki K Poulose <suzuki.poulose@arm.com>"); 871 MODULE_LICENSE("GPL v2"); 872