1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ARM CoreSight Architecture PMU driver. 4 * 5 * This driver adds support for uncore PMU based on ARM CoreSight Performance 6 * Monitoring Unit Architecture. The PMU is accessible via MMIO registers and 7 * like other uncore PMUs, it does not support process specific events and 8 * cannot be used in sampling mode. 9 * 10 * This code is based on other uncore PMUs like ARM DSU PMU. It provides a 11 * generic implementation to operate the PMU according to CoreSight PMU 12 * architecture and ACPI ARM PMU table (APMT) documents below: 13 * - ARM CoreSight PMU architecture document number: ARM IHI 0091 A.a-00bet0. 14 * - APMT document number: ARM DEN0117. 15 * 16 * The user should refer to the vendor technical documentation to get details 17 * about the supported events. 18 * 19 * Copyright (c) 2022-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. 20 * 21 */ 22 23 #include <linux/acpi.h> 24 #include <linux/cacheinfo.h> 25 #include <linux/ctype.h> 26 #include <linux/interrupt.h> 27 #include <linux/io-64-nonatomic-lo-hi.h> 28 #include <linux/module.h> 29 #include <linux/mutex.h> 30 #include <linux/of.h> 31 #include <linux/perf_event.h> 32 #include <linux/platform_device.h> 33 34 #include "arm_cspmu.h" 35 36 #define PMUNAME "arm_cspmu" 37 #define DRVNAME "arm-cs-arch-pmu" 38 39 #define ARM_CSPMU_CPUMASK_ATTR(_name, _config) \ 40 ARM_CSPMU_EXT_ATTR(_name, arm_cspmu_cpumask_show, \ 41 (unsigned long)_config) 42 43 /* Each SET/CLR register supports up to 32 counters. */ 44 #define ARM_CSPMU_SET_CLR_COUNTER_SHIFT 5 45 #define ARM_CSPMU_SET_CLR_COUNTER_NUM \ 46 (1 << ARM_CSPMU_SET_CLR_COUNTER_SHIFT) 47 48 /* Convert counter idx into SET/CLR register number. */ 49 #define COUNTER_TO_SET_CLR_ID(idx) \ 50 (idx >> ARM_CSPMU_SET_CLR_COUNTER_SHIFT) 51 52 /* Convert counter idx into SET/CLR register bit. */ 53 #define COUNTER_TO_SET_CLR_BIT(idx) \ 54 (idx & (ARM_CSPMU_SET_CLR_COUNTER_NUM - 1)) 55 56 #define ARM_CSPMU_ACTIVE_CPU_MASK 0x0 57 #define ARM_CSPMU_ASSOCIATED_CPU_MASK 0x1 58 59 /* 60 * Maximum poll count for reading counter value using high-low-high sequence. 61 */ 62 #define HILOHI_MAX_POLL 1000 63 64 static unsigned long arm_cspmu_cpuhp_state; 65 66 static DEFINE_MUTEX(arm_cspmu_lock); 67 68 static void arm_cspmu_set_ev_filter(struct arm_cspmu *cspmu, 69 const struct perf_event *event); 70 static void arm_cspmu_set_cc_filter(struct arm_cspmu *cspmu, 71 const struct perf_event *event); 72 73 static struct acpi_apmt_node *arm_cspmu_apmt_node(struct device *dev) 74 { 75 struct acpi_apmt_node **ptr = dev_get_platdata(dev); 76 77 return ptr ? *ptr : NULL; 78 } 79 80 /* 81 * In CoreSight PMU architecture, all of the MMIO registers are 32-bit except 82 * counter register. The counter register can be implemented as 32-bit or 64-bit 83 * register depending on the value of PMCFGR.SIZE field. For 64-bit access, 84 * single-copy 64-bit atomic support is implementation defined. APMT node flag 85 * is used to identify if the PMU supports 64-bit single copy atomic. If 64-bit 86 * single copy atomic is not supported, the driver treats the register as a pair 87 * of 32-bit register. 88 */ 89 90 /* 91 * Read 64-bit register as a pair of 32-bit registers using hi-lo-hi sequence. 92 */ 93 static u64 read_reg64_hilohi(const void __iomem *addr, u32 max_poll_count) 94 { 95 u32 val_lo, val_hi; 96 u64 val; 97 98 /* Use high-low-high sequence to avoid tearing */ 99 do { 100 if (max_poll_count-- == 0) { 101 pr_err("ARM CSPMU: timeout hi-low-high sequence\n"); 102 return 0; 103 } 104 105 val_hi = readl(addr + 4); 106 val_lo = readl(addr); 107 } while (val_hi != readl(addr + 4)); 108 109 val = (((u64)val_hi << 32) | val_lo); 110 111 return val; 112 } 113 114 /* Check if cycle counter is supported. */ 115 static inline bool supports_cycle_counter(const struct arm_cspmu *cspmu) 116 { 117 return (cspmu->pmcfgr & PMCFGR_CC); 118 } 119 120 /* Get counter size, which is (PMCFGR_SIZE + 1). */ 121 static inline u32 counter_size(const struct arm_cspmu *cspmu) 122 { 123 return FIELD_GET(PMCFGR_SIZE, cspmu->pmcfgr) + 1; 124 } 125 126 /* Get counter mask. */ 127 static inline u64 counter_mask(const struct arm_cspmu *cspmu) 128 { 129 return GENMASK_ULL(counter_size(cspmu) - 1, 0); 130 } 131 132 /* Check if counter is implemented as 64-bit register. */ 133 static inline bool use_64b_counter_reg(const struct arm_cspmu *cspmu) 134 { 135 return (counter_size(cspmu) > 32); 136 } 137 138 ssize_t arm_cspmu_sysfs_event_show(struct device *dev, 139 struct device_attribute *attr, char *buf) 140 { 141 struct perf_pmu_events_attr *pmu_attr; 142 143 pmu_attr = container_of(attr, typeof(*pmu_attr), attr); 144 return sysfs_emit(buf, "event=0x%llx\n", pmu_attr->id); 145 } 146 EXPORT_SYMBOL_GPL(arm_cspmu_sysfs_event_show); 147 148 /* Default event list. */ 149 static struct attribute *arm_cspmu_event_attrs[] = { 150 ARM_CSPMU_EVENT_ATTR(cycles, ARM_CSPMU_EVT_CYCLES_DEFAULT), 151 NULL, 152 }; 153 154 static struct attribute ** 155 arm_cspmu_get_event_attrs(const struct arm_cspmu *cspmu) 156 { 157 struct attribute **attrs; 158 159 attrs = devm_kmemdup(cspmu->dev, arm_cspmu_event_attrs, 160 sizeof(arm_cspmu_event_attrs), GFP_KERNEL); 161 162 return attrs; 163 } 164 165 static umode_t 166 arm_cspmu_event_attr_is_visible(struct kobject *kobj, 167 struct attribute *attr, int unused) 168 { 169 struct device *dev = kobj_to_dev(kobj); 170 struct arm_cspmu *cspmu = to_arm_cspmu(dev_get_drvdata(dev)); 171 struct perf_pmu_events_attr *eattr; 172 173 eattr = container_of(attr, typeof(*eattr), attr.attr); 174 175 /* Hide cycle event if not supported */ 176 if (!supports_cycle_counter(cspmu) && 177 eattr->id == ARM_CSPMU_EVT_CYCLES_DEFAULT) 178 return 0; 179 180 return attr->mode; 181 } 182 183 static struct attribute *arm_cspmu_format_attrs[] = { 184 ARM_CSPMU_FORMAT_EVENT_ATTR, 185 ARM_CSPMU_FORMAT_FILTER_ATTR, 186 ARM_CSPMU_FORMAT_FILTER2_ATTR, 187 NULL, 188 }; 189 190 static struct attribute ** 191 arm_cspmu_get_format_attrs(const struct arm_cspmu *cspmu) 192 { 193 struct attribute **attrs; 194 195 attrs = devm_kmemdup(cspmu->dev, arm_cspmu_format_attrs, 196 sizeof(arm_cspmu_format_attrs), GFP_KERNEL); 197 198 return attrs; 199 } 200 201 static u32 arm_cspmu_event_type(const struct perf_event *event) 202 { 203 return event->attr.config & ARM_CSPMU_EVENT_MASK; 204 } 205 206 static bool arm_cspmu_is_cycle_counter_event(const struct perf_event *event) 207 { 208 return (event->attr.config == ARM_CSPMU_EVT_CYCLES_DEFAULT); 209 } 210 211 static ssize_t arm_cspmu_identifier_show(struct device *dev, 212 struct device_attribute *attr, 213 char *page) 214 { 215 struct arm_cspmu *cspmu = to_arm_cspmu(dev_get_drvdata(dev)); 216 217 return sysfs_emit(page, "%s\n", cspmu->identifier); 218 } 219 220 static struct device_attribute arm_cspmu_identifier_attr = 221 __ATTR(identifier, 0444, arm_cspmu_identifier_show, NULL); 222 223 static struct attribute *arm_cspmu_identifier_attrs[] = { 224 &arm_cspmu_identifier_attr.attr, 225 NULL, 226 }; 227 228 static struct attribute_group arm_cspmu_identifier_attr_group = { 229 .attrs = arm_cspmu_identifier_attrs, 230 }; 231 232 static const char *arm_cspmu_get_identifier(const struct arm_cspmu *cspmu) 233 { 234 const char *identifier = 235 devm_kasprintf(cspmu->dev, GFP_KERNEL, "%x", 236 cspmu->impl.pmiidr); 237 return identifier; 238 } 239 240 static const char *arm_cspmu_type_str[ACPI_APMT_NODE_TYPE_COUNT] = { 241 "mc", 242 "smmu", 243 "pcie", 244 "acpi", 245 "cache", 246 }; 247 248 static const char *arm_cspmu_get_name(const struct arm_cspmu *cspmu) 249 { 250 struct device *dev; 251 struct acpi_apmt_node *apmt_node; 252 u8 pmu_type; 253 char *name; 254 char acpi_hid_string[ACPI_ID_LEN] = { 0 }; 255 static atomic_t pmu_idx[ACPI_APMT_NODE_TYPE_COUNT] = { 0 }; 256 257 dev = cspmu->dev; 258 apmt_node = arm_cspmu_apmt_node(dev); 259 if (!apmt_node) 260 return devm_kasprintf(dev, GFP_KERNEL, PMUNAME "_%u", 261 atomic_fetch_inc(&pmu_idx[0])); 262 263 pmu_type = apmt_node->type; 264 265 if (pmu_type >= ACPI_APMT_NODE_TYPE_COUNT) { 266 dev_err(dev, "unsupported PMU type-%u\n", pmu_type); 267 return NULL; 268 } 269 270 if (pmu_type == ACPI_APMT_NODE_TYPE_ACPI) { 271 memcpy(acpi_hid_string, 272 &apmt_node->inst_primary, 273 sizeof(apmt_node->inst_primary)); 274 name = devm_kasprintf(dev, GFP_KERNEL, "%s_%s_%s_%u", PMUNAME, 275 arm_cspmu_type_str[pmu_type], 276 acpi_hid_string, 277 apmt_node->inst_secondary); 278 } else { 279 name = devm_kasprintf(dev, GFP_KERNEL, "%s_%s_%d", PMUNAME, 280 arm_cspmu_type_str[pmu_type], 281 atomic_fetch_inc(&pmu_idx[pmu_type])); 282 } 283 284 return name; 285 } 286 287 static ssize_t arm_cspmu_cpumask_show(struct device *dev, 288 struct device_attribute *attr, 289 char *buf) 290 { 291 struct pmu *pmu = dev_get_drvdata(dev); 292 struct arm_cspmu *cspmu = to_arm_cspmu(pmu); 293 struct dev_ext_attribute *eattr = 294 container_of(attr, struct dev_ext_attribute, attr); 295 unsigned long mask_id = (unsigned long)eattr->var; 296 const cpumask_t *cpumask; 297 298 switch (mask_id) { 299 case ARM_CSPMU_ACTIVE_CPU_MASK: 300 cpumask = &cspmu->active_cpu; 301 break; 302 case ARM_CSPMU_ASSOCIATED_CPU_MASK: 303 cpumask = &cspmu->associated_cpus; 304 break; 305 default: 306 return 0; 307 } 308 return cpumap_print_to_pagebuf(true, buf, cpumask); 309 } 310 311 static struct attribute *arm_cspmu_cpumask_attrs[] = { 312 ARM_CSPMU_CPUMASK_ATTR(cpumask, ARM_CSPMU_ACTIVE_CPU_MASK), 313 ARM_CSPMU_CPUMASK_ATTR(associated_cpus, ARM_CSPMU_ASSOCIATED_CPU_MASK), 314 NULL, 315 }; 316 317 static struct attribute_group arm_cspmu_cpumask_attr_group = { 318 .attrs = arm_cspmu_cpumask_attrs, 319 }; 320 321 static struct arm_cspmu_impl_match impl_match[] = { 322 { 323 .module_name = "nvidia_cspmu", 324 .pmiidr_val = ARM_CSPMU_IMPL_ID_NVIDIA, 325 .pmiidr_mask = ARM_CSPMU_PMIIDR_IMPLEMENTER, 326 .module = NULL, 327 .impl_init_ops = NULL, 328 }, 329 { 330 .module_name = "ampere_cspmu", 331 .pmiidr_val = ARM_CSPMU_IMPL_ID_AMPERE, 332 .pmiidr_mask = ARM_CSPMU_PMIIDR_IMPLEMENTER, 333 .module = NULL, 334 .impl_init_ops = NULL, 335 }, 336 337 {0} 338 }; 339 340 static struct arm_cspmu_impl_match *arm_cspmu_impl_match_get(u32 pmiidr) 341 { 342 struct arm_cspmu_impl_match *match = impl_match; 343 344 for (; match->pmiidr_val; match++) { 345 u32 mask = match->pmiidr_mask; 346 347 if ((match->pmiidr_val & mask) == (pmiidr & mask)) 348 return match; 349 } 350 351 return NULL; 352 } 353 354 #define DEFAULT_IMPL_OP(name) .name = arm_cspmu_##name 355 356 static int arm_cspmu_init_impl_ops(struct arm_cspmu *cspmu) 357 { 358 int ret = 0; 359 struct acpi_apmt_node *apmt_node = arm_cspmu_apmt_node(cspmu->dev); 360 struct arm_cspmu_impl_match *match; 361 362 /* Start with a default PMU implementation */ 363 cspmu->impl.module = THIS_MODULE; 364 cspmu->impl.pmiidr = readl(cspmu->base0 + PMIIDR); 365 cspmu->impl.ops = (struct arm_cspmu_impl_ops) { 366 DEFAULT_IMPL_OP(get_event_attrs), 367 DEFAULT_IMPL_OP(get_format_attrs), 368 DEFAULT_IMPL_OP(get_identifier), 369 DEFAULT_IMPL_OP(get_name), 370 DEFAULT_IMPL_OP(is_cycle_counter_event), 371 DEFAULT_IMPL_OP(event_type), 372 DEFAULT_IMPL_OP(set_cc_filter), 373 DEFAULT_IMPL_OP(set_ev_filter), 374 DEFAULT_IMPL_OP(event_attr_is_visible), 375 }; 376 377 /* Firmware may override implementer/product ID from PMIIDR */ 378 if (apmt_node && apmt_node->impl_id) 379 cspmu->impl.pmiidr = apmt_node->impl_id; 380 381 /* Find implementer specific attribute ops. */ 382 match = arm_cspmu_impl_match_get(cspmu->impl.pmiidr); 383 384 /* Load implementer module and initialize the callbacks. */ 385 if (match) { 386 mutex_lock(&arm_cspmu_lock); 387 388 if (match->impl_init_ops) { 389 /* Prevent unload until PMU registration is done. */ 390 if (try_module_get(match->module)) { 391 cspmu->impl.module = match->module; 392 cspmu->impl.match = match; 393 ret = match->impl_init_ops(cspmu); 394 if (ret) 395 module_put(match->module); 396 } else { 397 WARN(1, "arm_cspmu failed to get module: %s\n", 398 match->module_name); 399 ret = -EINVAL; 400 } 401 } else { 402 request_module_nowait(match->module_name); 403 ret = -EPROBE_DEFER; 404 } 405 406 mutex_unlock(&arm_cspmu_lock); 407 } 408 409 return ret; 410 } 411 412 static struct attribute_group * 413 arm_cspmu_alloc_event_attr_group(struct arm_cspmu *cspmu) 414 { 415 struct attribute_group *event_group; 416 struct device *dev = cspmu->dev; 417 const struct arm_cspmu_impl_ops *impl_ops = &cspmu->impl.ops; 418 419 event_group = 420 devm_kzalloc(dev, sizeof(struct attribute_group), GFP_KERNEL); 421 if (!event_group) 422 return NULL; 423 424 event_group->name = "events"; 425 event_group->is_visible = impl_ops->event_attr_is_visible; 426 event_group->attrs = impl_ops->get_event_attrs(cspmu); 427 428 if (!event_group->attrs) 429 return NULL; 430 431 return event_group; 432 } 433 434 static struct attribute_group * 435 arm_cspmu_alloc_format_attr_group(struct arm_cspmu *cspmu) 436 { 437 struct attribute_group *format_group; 438 struct device *dev = cspmu->dev; 439 440 format_group = 441 devm_kzalloc(dev, sizeof(struct attribute_group), GFP_KERNEL); 442 if (!format_group) 443 return NULL; 444 445 format_group->name = "format"; 446 format_group->attrs = cspmu->impl.ops.get_format_attrs(cspmu); 447 448 if (!format_group->attrs) 449 return NULL; 450 451 return format_group; 452 } 453 454 static int arm_cspmu_alloc_attr_groups(struct arm_cspmu *cspmu) 455 { 456 const struct attribute_group **attr_groups = cspmu->attr_groups; 457 const struct arm_cspmu_impl_ops *impl_ops = &cspmu->impl.ops; 458 459 cspmu->identifier = impl_ops->get_identifier(cspmu); 460 cspmu->name = impl_ops->get_name(cspmu); 461 462 if (!cspmu->identifier || !cspmu->name) 463 return -ENOMEM; 464 465 attr_groups[0] = arm_cspmu_alloc_event_attr_group(cspmu); 466 attr_groups[1] = arm_cspmu_alloc_format_attr_group(cspmu); 467 attr_groups[2] = &arm_cspmu_identifier_attr_group; 468 attr_groups[3] = &arm_cspmu_cpumask_attr_group; 469 470 if (!attr_groups[0] || !attr_groups[1]) 471 return -ENOMEM; 472 473 return 0; 474 } 475 476 static inline void arm_cspmu_reset_counters(struct arm_cspmu *cspmu) 477 { 478 writel(PMCR_C | PMCR_P, cspmu->base0 + PMCR); 479 } 480 481 static inline void arm_cspmu_start_counters(struct arm_cspmu *cspmu) 482 { 483 writel(PMCR_E, cspmu->base0 + PMCR); 484 } 485 486 static inline void arm_cspmu_stop_counters(struct arm_cspmu *cspmu) 487 { 488 writel(0, cspmu->base0 + PMCR); 489 } 490 491 static void arm_cspmu_enable(struct pmu *pmu) 492 { 493 bool disabled; 494 struct arm_cspmu *cspmu = to_arm_cspmu(pmu); 495 496 disabled = bitmap_empty(cspmu->hw_events.used_ctrs, 497 cspmu->num_logical_ctrs); 498 499 if (disabled) 500 return; 501 502 arm_cspmu_start_counters(cspmu); 503 } 504 505 static void arm_cspmu_disable(struct pmu *pmu) 506 { 507 struct arm_cspmu *cspmu = to_arm_cspmu(pmu); 508 509 arm_cspmu_stop_counters(cspmu); 510 } 511 512 static int arm_cspmu_get_event_idx(struct arm_cspmu_hw_events *hw_events, 513 struct perf_event *event) 514 { 515 int idx, ret; 516 struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu); 517 518 if (supports_cycle_counter(cspmu)) { 519 if (cspmu->impl.ops.is_cycle_counter_event(event)) { 520 /* Search for available cycle counter. */ 521 if (test_and_set_bit(cspmu->cycle_counter_logical_idx, 522 hw_events->used_ctrs)) 523 return -EAGAIN; 524 525 return cspmu->cycle_counter_logical_idx; 526 } 527 528 /* 529 * Search a regular counter from the used counter bitmap. 530 * The cycle counter divides the bitmap into two parts. Search 531 * the first then second half to exclude the cycle counter bit. 532 */ 533 idx = find_first_zero_bit(hw_events->used_ctrs, 534 cspmu->cycle_counter_logical_idx); 535 if (idx >= cspmu->cycle_counter_logical_idx) { 536 idx = find_next_zero_bit( 537 hw_events->used_ctrs, 538 cspmu->num_logical_ctrs, 539 cspmu->cycle_counter_logical_idx + 1); 540 } 541 } else { 542 idx = find_first_zero_bit(hw_events->used_ctrs, 543 cspmu->num_logical_ctrs); 544 } 545 546 if (idx >= cspmu->num_logical_ctrs) 547 return -EAGAIN; 548 549 if (cspmu->impl.ops.validate_event) { 550 ret = cspmu->impl.ops.validate_event(cspmu, event); 551 if (ret) 552 return ret; 553 } 554 555 set_bit(idx, hw_events->used_ctrs); 556 557 return idx; 558 } 559 560 static bool arm_cspmu_validate_event(struct pmu *pmu, 561 struct arm_cspmu_hw_events *hw_events, 562 struct perf_event *event) 563 { 564 if (is_software_event(event)) 565 return true; 566 567 /* Reject groups spanning multiple HW PMUs. */ 568 if (event->pmu != pmu) 569 return false; 570 571 return (arm_cspmu_get_event_idx(hw_events, event) >= 0); 572 } 573 574 /* 575 * Make sure the group of events can be scheduled at once 576 * on the PMU. 577 */ 578 static bool arm_cspmu_validate_group(struct perf_event *event) 579 { 580 struct perf_event *sibling, *leader = event->group_leader; 581 struct arm_cspmu_hw_events fake_hw_events; 582 583 if (event->group_leader == event) 584 return true; 585 586 memset(&fake_hw_events, 0, sizeof(fake_hw_events)); 587 588 if (!arm_cspmu_validate_event(event->pmu, &fake_hw_events, leader)) 589 return false; 590 591 for_each_sibling_event(sibling, leader) { 592 if (!arm_cspmu_validate_event(event->pmu, &fake_hw_events, 593 sibling)) 594 return false; 595 } 596 597 return arm_cspmu_validate_event(event->pmu, &fake_hw_events, event); 598 } 599 600 static int arm_cspmu_event_init(struct perf_event *event) 601 { 602 struct arm_cspmu *cspmu; 603 struct hw_perf_event *hwc = &event->hw; 604 605 cspmu = to_arm_cspmu(event->pmu); 606 607 if (event->attr.type != event->pmu->type) 608 return -ENOENT; 609 610 /* 611 * Following other "uncore" PMUs, we do not support sampling mode or 612 * attach to a task (per-process mode). 613 */ 614 if (is_sampling_event(event)) { 615 dev_dbg(cspmu->pmu.dev, 616 "Can't support sampling events\n"); 617 return -EOPNOTSUPP; 618 } 619 620 if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK) { 621 dev_dbg(cspmu->pmu.dev, 622 "Can't support per-task counters\n"); 623 return -EINVAL; 624 } 625 626 /* 627 * Make sure the CPU assignment is on one of the CPUs associated with 628 * this PMU. 629 */ 630 if (!cpumask_test_cpu(event->cpu, &cspmu->associated_cpus)) { 631 dev_dbg(cspmu->pmu.dev, 632 "Requested cpu is not associated with the PMU\n"); 633 return -EINVAL; 634 } 635 636 /* Enforce the current active CPU to handle the events in this PMU. */ 637 event->cpu = cpumask_first(&cspmu->active_cpu); 638 if (event->cpu >= nr_cpu_ids) 639 return -EINVAL; 640 641 if (!arm_cspmu_validate_group(event)) 642 return -EINVAL; 643 644 /* 645 * The logical counter id is tracked with hw_perf_event.extra_reg.idx. 646 * The physical counter id is tracked with hw_perf_event.idx. 647 * We don't assign an index until we actually place the event onto 648 * hardware. Use -1 to signify that we haven't decided where to put it 649 * yet. 650 */ 651 hwc->idx = -1; 652 hwc->extra_reg.idx = -1; 653 hwc->config = cspmu->impl.ops.event_type(event); 654 655 return 0; 656 } 657 658 static inline u32 counter_offset(u32 reg_sz, u32 ctr_idx) 659 { 660 return (PMEVCNTR_LO + (reg_sz * ctr_idx)); 661 } 662 663 static void arm_cspmu_write_counter(struct perf_event *event, u64 val) 664 { 665 u32 offset; 666 struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu); 667 668 if (use_64b_counter_reg(cspmu)) { 669 offset = counter_offset(sizeof(u64), event->hw.idx); 670 671 if (cspmu->has_atomic_dword) 672 writeq(val, cspmu->base1 + offset); 673 else 674 lo_hi_writeq(val, cspmu->base1 + offset); 675 } else { 676 offset = counter_offset(sizeof(u32), event->hw.idx); 677 678 writel(lower_32_bits(val), cspmu->base1 + offset); 679 } 680 } 681 682 static u64 arm_cspmu_read_counter(struct perf_event *event) 683 { 684 u32 offset; 685 const void __iomem *counter_addr; 686 struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu); 687 688 if (use_64b_counter_reg(cspmu)) { 689 offset = counter_offset(sizeof(u64), event->hw.idx); 690 counter_addr = cspmu->base1 + offset; 691 692 return cspmu->has_atomic_dword ? 693 readq(counter_addr) : 694 read_reg64_hilohi(counter_addr, HILOHI_MAX_POLL); 695 } 696 697 offset = counter_offset(sizeof(u32), event->hw.idx); 698 return readl(cspmu->base1 + offset); 699 } 700 701 /* 702 * arm_cspmu_set_event_period: Set the period for the counter. 703 * 704 * To handle cases of extreme interrupt latency, we program 705 * the counter with half of the max count for the counters. 706 */ 707 static void arm_cspmu_set_event_period(struct perf_event *event) 708 { 709 struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu); 710 u64 val = counter_mask(cspmu) >> 1ULL; 711 712 local64_set(&event->hw.prev_count, val); 713 arm_cspmu_write_counter(event, val); 714 } 715 716 static void arm_cspmu_enable_counter(struct arm_cspmu *cspmu, int idx) 717 { 718 u32 reg_id, reg_bit, inten_off, cnten_off; 719 720 reg_id = COUNTER_TO_SET_CLR_ID(idx); 721 reg_bit = COUNTER_TO_SET_CLR_BIT(idx); 722 723 inten_off = PMINTENSET + (4 * reg_id); 724 cnten_off = PMCNTENSET + (4 * reg_id); 725 726 writel(BIT(reg_bit), cspmu->base0 + inten_off); 727 writel(BIT(reg_bit), cspmu->base0 + cnten_off); 728 } 729 730 static void arm_cspmu_disable_counter(struct arm_cspmu *cspmu, int idx) 731 { 732 u32 reg_id, reg_bit, inten_off, cnten_off; 733 734 reg_id = COUNTER_TO_SET_CLR_ID(idx); 735 reg_bit = COUNTER_TO_SET_CLR_BIT(idx); 736 737 inten_off = PMINTENCLR + (4 * reg_id); 738 cnten_off = PMCNTENCLR + (4 * reg_id); 739 740 writel(BIT(reg_bit), cspmu->base0 + cnten_off); 741 writel(BIT(reg_bit), cspmu->base0 + inten_off); 742 } 743 744 static void arm_cspmu_event_update(struct perf_event *event) 745 { 746 struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu); 747 struct hw_perf_event *hwc = &event->hw; 748 u64 delta, prev, now; 749 750 do { 751 prev = local64_read(&hwc->prev_count); 752 now = arm_cspmu_read_counter(event); 753 } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev); 754 755 delta = (now - prev) & counter_mask(cspmu); 756 local64_add(delta, &event->count); 757 } 758 759 static inline void arm_cspmu_set_event(struct arm_cspmu *cspmu, 760 struct hw_perf_event *hwc) 761 { 762 u32 offset = PMEVTYPER + (4 * hwc->idx); 763 764 writel(hwc->config, cspmu->base0 + offset); 765 } 766 767 static void arm_cspmu_set_ev_filter(struct arm_cspmu *cspmu, 768 const struct perf_event *event) 769 { 770 u32 filter = event->attr.config1 & ARM_CSPMU_FILTER_MASK; 771 u32 filter2 = event->attr.config2 & ARM_CSPMU_FILTER_MASK; 772 u32 offset = 4 * event->hw.idx; 773 774 writel(filter, cspmu->base0 + PMEVFILTR + offset); 775 writel(filter2, cspmu->base0 + PMEVFILT2R + offset); 776 } 777 778 static void arm_cspmu_set_cc_filter(struct arm_cspmu *cspmu, 779 const struct perf_event *event) 780 { 781 u32 filter = event->attr.config1 & ARM_CSPMU_FILTER_MASK; 782 783 writel(filter, cspmu->base0 + PMCCFILTR); 784 } 785 786 static void arm_cspmu_start(struct perf_event *event, int pmu_flags) 787 { 788 struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu); 789 struct hw_perf_event *hwc = &event->hw; 790 791 /* We always reprogram the counter */ 792 if (pmu_flags & PERF_EF_RELOAD) 793 WARN_ON(!(hwc->state & PERF_HES_UPTODATE)); 794 795 arm_cspmu_set_event_period(event); 796 797 if (event->hw.extra_reg.idx == cspmu->cycle_counter_logical_idx) { 798 cspmu->impl.ops.set_cc_filter(cspmu, event); 799 } else { 800 arm_cspmu_set_event(cspmu, hwc); 801 cspmu->impl.ops.set_ev_filter(cspmu, event); 802 } 803 804 hwc->state = 0; 805 806 arm_cspmu_enable_counter(cspmu, hwc->idx); 807 } 808 809 static void arm_cspmu_stop(struct perf_event *event, int pmu_flags) 810 { 811 struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu); 812 struct hw_perf_event *hwc = &event->hw; 813 814 if (hwc->state & PERF_HES_STOPPED) 815 return; 816 817 arm_cspmu_disable_counter(cspmu, hwc->idx); 818 arm_cspmu_event_update(event); 819 820 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; 821 } 822 823 static inline u32 to_phys_idx(struct arm_cspmu *cspmu, u32 idx) 824 { 825 return (idx == cspmu->cycle_counter_logical_idx) ? 826 ARM_CSPMU_CYCLE_CNTR_IDX : idx; 827 } 828 829 static int arm_cspmu_add(struct perf_event *event, int flags) 830 { 831 struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu); 832 struct arm_cspmu_hw_events *hw_events = &cspmu->hw_events; 833 struct hw_perf_event *hwc = &event->hw; 834 int idx; 835 836 if (WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), 837 &cspmu->associated_cpus))) 838 return -ENOENT; 839 840 idx = arm_cspmu_get_event_idx(hw_events, event); 841 if (idx < 0) 842 return idx; 843 844 hw_events->events[idx] = event; 845 hwc->idx = to_phys_idx(cspmu, idx); 846 hwc->extra_reg.idx = idx; 847 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; 848 849 if (flags & PERF_EF_START) 850 arm_cspmu_start(event, PERF_EF_RELOAD); 851 852 /* Propagate changes to the userspace mapping. */ 853 perf_event_update_userpage(event); 854 855 return 0; 856 } 857 858 static void arm_cspmu_del(struct perf_event *event, int flags) 859 { 860 struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu); 861 struct arm_cspmu_hw_events *hw_events = &cspmu->hw_events; 862 struct hw_perf_event *hwc = &event->hw; 863 int idx = hwc->extra_reg.idx; 864 865 arm_cspmu_stop(event, PERF_EF_UPDATE); 866 867 hw_events->events[idx] = NULL; 868 869 clear_bit(idx, hw_events->used_ctrs); 870 871 perf_event_update_userpage(event); 872 } 873 874 static void arm_cspmu_read(struct perf_event *event) 875 { 876 arm_cspmu_event_update(event); 877 } 878 879 static struct arm_cspmu *arm_cspmu_alloc(struct platform_device *pdev) 880 { 881 struct acpi_apmt_node *apmt_node; 882 struct arm_cspmu *cspmu; 883 struct device *dev = &pdev->dev; 884 885 cspmu = devm_kzalloc(dev, sizeof(*cspmu), GFP_KERNEL); 886 if (!cspmu) 887 return NULL; 888 889 cspmu->dev = dev; 890 platform_set_drvdata(pdev, cspmu); 891 892 apmt_node = arm_cspmu_apmt_node(dev); 893 if (apmt_node) { 894 cspmu->has_atomic_dword = apmt_node->flags & ACPI_APMT_FLAGS_ATOMIC; 895 } else { 896 u32 width = 0; 897 898 device_property_read_u32(dev, "reg-io-width", &width); 899 cspmu->has_atomic_dword = (width == 8); 900 } 901 902 return cspmu; 903 } 904 905 static int arm_cspmu_init_mmio(struct arm_cspmu *cspmu) 906 { 907 struct device *dev; 908 struct platform_device *pdev; 909 910 dev = cspmu->dev; 911 pdev = to_platform_device(dev); 912 913 /* Base address for page 0. */ 914 cspmu->base0 = devm_platform_ioremap_resource(pdev, 0); 915 if (IS_ERR(cspmu->base0)) { 916 dev_err(dev, "ioremap failed for page-0 resource\n"); 917 return PTR_ERR(cspmu->base0); 918 } 919 920 /* Base address for page 1 if supported. Otherwise point to page 0. */ 921 cspmu->base1 = cspmu->base0; 922 if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) { 923 cspmu->base1 = devm_platform_ioremap_resource(pdev, 1); 924 if (IS_ERR(cspmu->base1)) { 925 dev_err(dev, "ioremap failed for page-1 resource\n"); 926 return PTR_ERR(cspmu->base1); 927 } 928 } 929 930 cspmu->pmcfgr = readl(cspmu->base0 + PMCFGR); 931 932 cspmu->num_logical_ctrs = FIELD_GET(PMCFGR_N, cspmu->pmcfgr) + 1; 933 934 cspmu->cycle_counter_logical_idx = ARM_CSPMU_MAX_HW_CNTRS; 935 936 if (supports_cycle_counter(cspmu)) { 937 /* 938 * The last logical counter is mapped to cycle counter if 939 * there is a gap between regular and cycle counter. Otherwise, 940 * logical and physical have 1-to-1 mapping. 941 */ 942 cspmu->cycle_counter_logical_idx = 943 (cspmu->num_logical_ctrs <= ARM_CSPMU_CYCLE_CNTR_IDX) ? 944 cspmu->num_logical_ctrs - 1 : 945 ARM_CSPMU_CYCLE_CNTR_IDX; 946 } 947 948 cspmu->num_set_clr_reg = 949 DIV_ROUND_UP(cspmu->num_logical_ctrs, 950 ARM_CSPMU_SET_CLR_COUNTER_NUM); 951 952 cspmu->hw_events.events = 953 devm_kcalloc(dev, cspmu->num_logical_ctrs, 954 sizeof(*cspmu->hw_events.events), GFP_KERNEL); 955 956 if (!cspmu->hw_events.events) 957 return -ENOMEM; 958 959 return 0; 960 } 961 962 static inline int arm_cspmu_get_reset_overflow(struct arm_cspmu *cspmu, 963 u32 *pmovs) 964 { 965 int i; 966 u32 pmovclr_offset = PMOVSCLR; 967 u32 has_overflowed = 0; 968 969 for (i = 0; i < cspmu->num_set_clr_reg; ++i) { 970 pmovs[i] = readl(cspmu->base1 + pmovclr_offset); 971 has_overflowed |= pmovs[i]; 972 writel(pmovs[i], cspmu->base1 + pmovclr_offset); 973 pmovclr_offset += sizeof(u32); 974 } 975 976 return has_overflowed != 0; 977 } 978 979 static irqreturn_t arm_cspmu_handle_irq(int irq_num, void *dev) 980 { 981 int idx, has_overflowed; 982 struct perf_event *event; 983 struct arm_cspmu *cspmu = dev; 984 DECLARE_BITMAP(pmovs, ARM_CSPMU_MAX_HW_CNTRS); 985 bool handled = false; 986 987 arm_cspmu_stop_counters(cspmu); 988 989 has_overflowed = arm_cspmu_get_reset_overflow(cspmu, (u32 *)pmovs); 990 if (!has_overflowed) 991 goto done; 992 993 for_each_set_bit(idx, cspmu->hw_events.used_ctrs, 994 cspmu->num_logical_ctrs) { 995 event = cspmu->hw_events.events[idx]; 996 997 if (!event) 998 continue; 999 1000 if (!test_bit(event->hw.idx, pmovs)) 1001 continue; 1002 1003 arm_cspmu_event_update(event); 1004 arm_cspmu_set_event_period(event); 1005 1006 handled = true; 1007 } 1008 1009 done: 1010 arm_cspmu_start_counters(cspmu); 1011 return IRQ_RETVAL(handled); 1012 } 1013 1014 static int arm_cspmu_request_irq(struct arm_cspmu *cspmu) 1015 { 1016 int irq, ret; 1017 struct device *dev; 1018 struct platform_device *pdev; 1019 1020 dev = cspmu->dev; 1021 pdev = to_platform_device(dev); 1022 1023 /* Skip IRQ request if the PMU does not support overflow interrupt. */ 1024 irq = platform_get_irq_optional(pdev, 0); 1025 if (irq < 0) 1026 return irq == -ENXIO ? 0 : irq; 1027 1028 ret = devm_request_irq(dev, irq, arm_cspmu_handle_irq, 1029 IRQF_NOBALANCING | IRQF_NO_THREAD, dev_name(dev), 1030 cspmu); 1031 if (ret) { 1032 dev_err(dev, "Could not request IRQ %d\n", irq); 1033 return ret; 1034 } 1035 1036 cspmu->irq = irq; 1037 1038 return 0; 1039 } 1040 1041 #if defined(CONFIG_ACPI) && defined(CONFIG_ARM64) 1042 #include <acpi/processor.h> 1043 1044 static inline int arm_cspmu_find_cpu_container(int cpu, u32 container_uid) 1045 { 1046 struct device *cpu_dev; 1047 struct acpi_device *acpi_dev; 1048 1049 cpu_dev = get_cpu_device(cpu); 1050 if (!cpu_dev) 1051 return -ENODEV; 1052 1053 acpi_dev = ACPI_COMPANION(cpu_dev); 1054 while (acpi_dev) { 1055 if (acpi_dev_hid_uid_match(acpi_dev, ACPI_PROCESSOR_CONTAINER_HID, container_uid)) 1056 return 0; 1057 1058 acpi_dev = acpi_dev_parent(acpi_dev); 1059 } 1060 1061 return -ENODEV; 1062 } 1063 1064 static int arm_cspmu_acpi_get_cpus(struct arm_cspmu *cspmu) 1065 { 1066 struct acpi_apmt_node *apmt_node; 1067 int affinity_flag; 1068 int cpu; 1069 1070 apmt_node = arm_cspmu_apmt_node(cspmu->dev); 1071 affinity_flag = apmt_node->flags & ACPI_APMT_FLAGS_AFFINITY; 1072 1073 if (affinity_flag == ACPI_APMT_FLAGS_AFFINITY_PROC) { 1074 for_each_possible_cpu(cpu) { 1075 if (apmt_node->proc_affinity == 1076 get_acpi_id_for_cpu(cpu)) { 1077 cpumask_set_cpu(cpu, &cspmu->associated_cpus); 1078 break; 1079 } 1080 } 1081 } else { 1082 for_each_possible_cpu(cpu) { 1083 if (arm_cspmu_find_cpu_container( 1084 cpu, apmt_node->proc_affinity)) 1085 continue; 1086 1087 cpumask_set_cpu(cpu, &cspmu->associated_cpus); 1088 } 1089 } 1090 1091 return 0; 1092 } 1093 #else 1094 static int arm_cspmu_acpi_get_cpus(struct arm_cspmu *cspmu) 1095 { 1096 return -ENODEV; 1097 } 1098 #endif 1099 1100 static int arm_cspmu_of_get_cpus(struct arm_cspmu *cspmu) 1101 { 1102 struct of_phandle_iterator it; 1103 int ret, cpu; 1104 1105 of_for_each_phandle(&it, ret, dev_of_node(cspmu->dev), "cpus", NULL, 0) { 1106 cpu = of_cpu_node_to_id(it.node); 1107 if (cpu < 0) 1108 continue; 1109 cpumask_set_cpu(cpu, &cspmu->associated_cpus); 1110 } 1111 return ret == -ENOENT ? 0 : ret; 1112 } 1113 1114 static int arm_cspmu_get_cpus(struct arm_cspmu *cspmu) 1115 { 1116 int ret = 0; 1117 1118 if (arm_cspmu_apmt_node(cspmu->dev)) 1119 ret = arm_cspmu_acpi_get_cpus(cspmu); 1120 else if (device_property_present(cspmu->dev, "cpus")) 1121 ret = arm_cspmu_of_get_cpus(cspmu); 1122 else 1123 cpumask_copy(&cspmu->associated_cpus, cpu_possible_mask); 1124 1125 if (!ret && cpumask_empty(&cspmu->associated_cpus)) { 1126 dev_dbg(cspmu->dev, "No cpu associated with the PMU\n"); 1127 ret = -ENODEV; 1128 } 1129 return ret; 1130 } 1131 1132 static int arm_cspmu_register_pmu(struct arm_cspmu *cspmu) 1133 { 1134 int ret, capabilities; 1135 1136 ret = arm_cspmu_alloc_attr_groups(cspmu); 1137 if (ret) 1138 return ret; 1139 1140 ret = cpuhp_state_add_instance(arm_cspmu_cpuhp_state, 1141 &cspmu->cpuhp_node); 1142 if (ret) 1143 return ret; 1144 1145 capabilities = PERF_PMU_CAP_NO_EXCLUDE; 1146 if (cspmu->irq == 0) 1147 capabilities |= PERF_PMU_CAP_NO_INTERRUPT; 1148 1149 cspmu->pmu = (struct pmu){ 1150 .task_ctx_nr = perf_invalid_context, 1151 .module = cspmu->impl.module, 1152 .parent = cspmu->dev, 1153 .pmu_enable = arm_cspmu_enable, 1154 .pmu_disable = arm_cspmu_disable, 1155 .event_init = arm_cspmu_event_init, 1156 .add = arm_cspmu_add, 1157 .del = arm_cspmu_del, 1158 .start = arm_cspmu_start, 1159 .stop = arm_cspmu_stop, 1160 .read = arm_cspmu_read, 1161 .attr_groups = cspmu->attr_groups, 1162 .capabilities = capabilities, 1163 }; 1164 1165 /* Hardware counter init */ 1166 arm_cspmu_reset_counters(cspmu); 1167 1168 ret = perf_pmu_register(&cspmu->pmu, cspmu->name, -1); 1169 if (ret) { 1170 cpuhp_state_remove_instance(arm_cspmu_cpuhp_state, 1171 &cspmu->cpuhp_node); 1172 } 1173 1174 return ret; 1175 } 1176 1177 static int arm_cspmu_device_probe(struct platform_device *pdev) 1178 { 1179 int ret; 1180 struct arm_cspmu *cspmu; 1181 1182 cspmu = arm_cspmu_alloc(pdev); 1183 if (!cspmu) 1184 return -ENOMEM; 1185 1186 ret = arm_cspmu_init_mmio(cspmu); 1187 if (ret) 1188 return ret; 1189 1190 ret = arm_cspmu_request_irq(cspmu); 1191 if (ret) 1192 return ret; 1193 1194 ret = arm_cspmu_get_cpus(cspmu); 1195 if (ret) 1196 return ret; 1197 1198 ret = arm_cspmu_init_impl_ops(cspmu); 1199 if (ret) 1200 return ret; 1201 1202 ret = arm_cspmu_register_pmu(cspmu); 1203 1204 /* Matches arm_cspmu_init_impl_ops() above. */ 1205 if (cspmu->impl.module != THIS_MODULE) 1206 module_put(cspmu->impl.module); 1207 1208 return ret; 1209 } 1210 1211 static void arm_cspmu_device_remove(struct platform_device *pdev) 1212 { 1213 struct arm_cspmu *cspmu = platform_get_drvdata(pdev); 1214 1215 perf_pmu_unregister(&cspmu->pmu); 1216 cpuhp_state_remove_instance(arm_cspmu_cpuhp_state, &cspmu->cpuhp_node); 1217 } 1218 1219 static const struct platform_device_id arm_cspmu_id[] = { 1220 {DRVNAME, 0}, 1221 { }, 1222 }; 1223 MODULE_DEVICE_TABLE(platform, arm_cspmu_id); 1224 1225 static const struct of_device_id arm_cspmu_of_match[] = { 1226 { .compatible = "arm,coresight-pmu" }, 1227 {} 1228 }; 1229 MODULE_DEVICE_TABLE(of, arm_cspmu_of_match); 1230 1231 static struct platform_driver arm_cspmu_driver = { 1232 .driver = { 1233 .name = DRVNAME, 1234 .of_match_table = arm_cspmu_of_match, 1235 .suppress_bind_attrs = true, 1236 }, 1237 .probe = arm_cspmu_device_probe, 1238 .remove = arm_cspmu_device_remove, 1239 .id_table = arm_cspmu_id, 1240 }; 1241 1242 static void arm_cspmu_set_active_cpu(int cpu, struct arm_cspmu *cspmu) 1243 { 1244 cpumask_set_cpu(cpu, &cspmu->active_cpu); 1245 if (cspmu->irq) 1246 WARN_ON(irq_set_affinity(cspmu->irq, &cspmu->active_cpu)); 1247 } 1248 1249 static int arm_cspmu_cpu_online(unsigned int cpu, struct hlist_node *node) 1250 { 1251 struct arm_cspmu *cspmu = 1252 hlist_entry_safe(node, struct arm_cspmu, cpuhp_node); 1253 1254 if (!cpumask_test_cpu(cpu, &cspmu->associated_cpus)) 1255 return 0; 1256 1257 /* If the PMU is already managed, there is nothing to do */ 1258 if (!cpumask_empty(&cspmu->active_cpu)) 1259 return 0; 1260 1261 /* Use this CPU for event counting */ 1262 arm_cspmu_set_active_cpu(cpu, cspmu); 1263 1264 return 0; 1265 } 1266 1267 static int arm_cspmu_cpu_teardown(unsigned int cpu, struct hlist_node *node) 1268 { 1269 unsigned int dst; 1270 1271 struct arm_cspmu *cspmu = 1272 hlist_entry_safe(node, struct arm_cspmu, cpuhp_node); 1273 1274 /* Nothing to do if this CPU doesn't own the PMU */ 1275 if (!cpumask_test_and_clear_cpu(cpu, &cspmu->active_cpu)) 1276 return 0; 1277 1278 /* Choose a new CPU to migrate ownership of the PMU to */ 1279 dst = cpumask_any_and_but(&cspmu->associated_cpus, 1280 cpu_online_mask, cpu); 1281 if (dst >= nr_cpu_ids) 1282 return 0; 1283 1284 /* Use this CPU for event counting */ 1285 perf_pmu_migrate_context(&cspmu->pmu, cpu, dst); 1286 arm_cspmu_set_active_cpu(dst, cspmu); 1287 1288 return 0; 1289 } 1290 1291 static int __init arm_cspmu_init(void) 1292 { 1293 int ret; 1294 1295 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, 1296 "perf/arm/cspmu:online", 1297 arm_cspmu_cpu_online, 1298 arm_cspmu_cpu_teardown); 1299 if (ret < 0) 1300 return ret; 1301 arm_cspmu_cpuhp_state = ret; 1302 return platform_driver_register(&arm_cspmu_driver); 1303 } 1304 1305 static void __exit arm_cspmu_exit(void) 1306 { 1307 platform_driver_unregister(&arm_cspmu_driver); 1308 cpuhp_remove_multi_state(arm_cspmu_cpuhp_state); 1309 } 1310 1311 int arm_cspmu_impl_register(const struct arm_cspmu_impl_match *impl_match) 1312 { 1313 struct arm_cspmu_impl_match *match; 1314 int ret = 0; 1315 1316 match = arm_cspmu_impl_match_get(impl_match->pmiidr_val); 1317 1318 if (match) { 1319 mutex_lock(&arm_cspmu_lock); 1320 1321 if (!match->impl_init_ops) { 1322 match->module = impl_match->module; 1323 match->impl_init_ops = impl_match->impl_init_ops; 1324 } else { 1325 /* Broken match table may contain non-unique entries */ 1326 WARN(1, "arm_cspmu backend already registered for module: %s, pmiidr: 0x%x, mask: 0x%x\n", 1327 match->module_name, 1328 match->pmiidr_val, 1329 match->pmiidr_mask); 1330 1331 ret = -EINVAL; 1332 } 1333 1334 mutex_unlock(&arm_cspmu_lock); 1335 1336 if (!ret) 1337 ret = driver_attach(&arm_cspmu_driver.driver); 1338 } else { 1339 pr_err("arm_cspmu reg failed, unable to find a match for pmiidr: 0x%x\n", 1340 impl_match->pmiidr_val); 1341 1342 ret = -EINVAL; 1343 } 1344 1345 return ret; 1346 } 1347 EXPORT_SYMBOL_GPL(arm_cspmu_impl_register); 1348 1349 static int arm_cspmu_match_device(struct device *dev, const void *match) 1350 { 1351 struct arm_cspmu *cspmu = platform_get_drvdata(to_platform_device(dev)); 1352 1353 return (cspmu && cspmu->impl.match == match) ? 1 : 0; 1354 } 1355 1356 void arm_cspmu_impl_unregister(const struct arm_cspmu_impl_match *impl_match) 1357 { 1358 struct device *dev; 1359 struct arm_cspmu_impl_match *match; 1360 1361 match = arm_cspmu_impl_match_get(impl_match->pmiidr_val); 1362 1363 if (WARN_ON(!match)) 1364 return; 1365 1366 /* Unbind the driver from all matching backend devices. */ 1367 while ((dev = driver_find_device(&arm_cspmu_driver.driver, NULL, 1368 match, arm_cspmu_match_device))) 1369 device_release_driver(dev); 1370 1371 mutex_lock(&arm_cspmu_lock); 1372 1373 match->module = NULL; 1374 match->impl_init_ops = NULL; 1375 1376 mutex_unlock(&arm_cspmu_lock); 1377 } 1378 EXPORT_SYMBOL_GPL(arm_cspmu_impl_unregister); 1379 1380 module_init(arm_cspmu_init); 1381 module_exit(arm_cspmu_exit); 1382 1383 MODULE_DESCRIPTION("ARM CoreSight Architecture Performance Monitor Driver"); 1384 MODULE_LICENSE("GPL v2"); 1385