1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/module.h> 3 4 #include <asm/cpu_device_id.h> 5 #include <asm/intel-family.h> 6 #include <asm/msr.h> 7 #include "uncore.h" 8 #include "uncore_discovery.h" 9 10 static bool uncore_no_discover; 11 module_param(uncore_no_discover, bool, 0); 12 MODULE_PARM_DESC(uncore_no_discover, "Don't enable the Intel uncore PerfMon discovery mechanism " 13 "(default: enable the discovery mechanism)."); 14 struct intel_uncore_type *empty_uncore[] = { NULL, }; 15 struct intel_uncore_type **uncore_msr_uncores = empty_uncore; 16 struct intel_uncore_type **uncore_pci_uncores = empty_uncore; 17 struct intel_uncore_type **uncore_mmio_uncores = empty_uncore; 18 19 static bool pcidrv_registered; 20 struct pci_driver *uncore_pci_driver; 21 /* The PCI driver for the device which the uncore doesn't own. */ 22 struct pci_driver *uncore_pci_sub_driver; 23 /* pci bus to socket mapping */ 24 DEFINE_RAW_SPINLOCK(pci2phy_map_lock); 25 struct list_head pci2phy_map_head = LIST_HEAD_INIT(pci2phy_map_head); 26 struct pci_extra_dev *uncore_extra_pci_dev; 27 int __uncore_max_dies; 28 29 /* mask of cpus that collect uncore events */ 30 static cpumask_t uncore_cpu_mask; 31 32 /* constraint for the fixed counter */ 33 static struct event_constraint uncore_constraint_fixed = 34 EVENT_CONSTRAINT(~0ULL, 1 << UNCORE_PMC_IDX_FIXED, ~0ULL); 35 struct event_constraint uncore_constraint_empty = 36 EVENT_CONSTRAINT(0, 0, 0); 37 38 MODULE_DESCRIPTION("Support for Intel uncore performance events"); 39 MODULE_LICENSE("GPL"); 40 41 int uncore_pcibus_to_dieid(struct pci_bus *bus) 42 { 43 struct pci2phy_map *map; 44 int die_id = -1; 45 46 raw_spin_lock(&pci2phy_map_lock); 47 list_for_each_entry(map, &pci2phy_map_head, list) { 48 if (map->segment == pci_domain_nr(bus)) { 49 die_id = map->pbus_to_dieid[bus->number]; 50 break; 51 } 52 } 53 raw_spin_unlock(&pci2phy_map_lock); 54 55 return die_id; 56 } 57 58 int uncore_die_to_segment(int die) 59 { 60 struct pci_bus *bus = NULL; 61 62 /* Find first pci bus which attributes to specified die. */ 63 while ((bus = pci_find_next_bus(bus)) && 64 (die != uncore_pcibus_to_dieid(bus))) 65 ; 66 67 return bus ? pci_domain_nr(bus) : -EINVAL; 68 } 69 70 /* Note: This API can only be used when NUMA information is available. */ 71 int uncore_device_to_die(struct pci_dev *dev) 72 { 73 int node = pcibus_to_node(dev->bus); 74 int cpu; 75 76 for_each_cpu(cpu, cpumask_of_pcibus(dev->bus)) { 77 struct cpuinfo_x86 *c = &cpu_data(cpu); 78 79 if (c->initialized && cpu_to_node(cpu) == node) 80 return c->topo.logical_die_id; 81 } 82 83 return -1; 84 } 85 86 /* 87 * Using cpus_read_lock() to ensure cpu is not going down between 88 * looking at cpu_online_mask. 89 * 90 * The lock must be held by the caller. 91 */ 92 int uncore_die_to_cpu(int die) 93 { 94 int res = -1, cpu; 95 96 for_each_online_cpu(cpu) { 97 if (topology_logical_die_id(cpu) == die) { 98 res = cpu; 99 break; 100 } 101 } 102 return res; 103 } 104 105 static void uncore_free_pcibus_map(void) 106 { 107 struct pci2phy_map *map, *tmp; 108 109 list_for_each_entry_safe(map, tmp, &pci2phy_map_head, list) { 110 list_del(&map->list); 111 kfree(map); 112 } 113 } 114 115 struct pci2phy_map *__find_pci2phy_map(int segment) 116 { 117 struct pci2phy_map *map, *alloc = NULL; 118 int i; 119 120 lockdep_assert_held(&pci2phy_map_lock); 121 122 lookup: 123 list_for_each_entry(map, &pci2phy_map_head, list) { 124 if (map->segment == segment) 125 goto end; 126 } 127 128 if (!alloc) { 129 raw_spin_unlock(&pci2phy_map_lock); 130 alloc = kmalloc_obj(struct pci2phy_map); 131 raw_spin_lock(&pci2phy_map_lock); 132 133 if (!alloc) 134 return NULL; 135 136 goto lookup; 137 } 138 139 map = alloc; 140 alloc = NULL; 141 map->segment = segment; 142 for (i = 0; i < 256; i++) 143 map->pbus_to_dieid[i] = -1; 144 list_add_tail(&map->list, &pci2phy_map_head); 145 146 end: 147 kfree(alloc); 148 return map; 149 } 150 151 ssize_t uncore_event_show(struct device *dev, 152 struct device_attribute *attr, char *buf) 153 { 154 struct uncore_event_desc *event = 155 container_of(attr, struct uncore_event_desc, attr); 156 return sprintf(buf, "%s", event->config); 157 } 158 159 struct intel_uncore_box *uncore_pmu_to_box(struct intel_uncore_pmu *pmu, int cpu) 160 { 161 unsigned int dieid = topology_logical_die_id(cpu); 162 163 /* 164 * The unsigned check also catches the '-1' return value for non 165 * existent mappings in the topology map. 166 */ 167 return dieid < uncore_max_dies() ? pmu->boxes[dieid] : NULL; 168 } 169 170 u64 uncore_msr_read_counter(struct intel_uncore_box *box, struct perf_event *event) 171 { 172 u64 count; 173 174 rdmsrq(event->hw.event_base, count); 175 176 return count; 177 } 178 179 void uncore_mmio_exit_box(struct intel_uncore_box *box) 180 { 181 if (box->io_addr) 182 iounmap(box->io_addr); 183 } 184 185 u64 uncore_mmio_read_counter(struct intel_uncore_box *box, 186 struct perf_event *event) 187 { 188 if (!box->io_addr) 189 return 0; 190 191 if (!uncore_mmio_is_valid_offset(box, event->hw.event_base)) 192 return 0; 193 194 return readq(box->io_addr + event->hw.event_base); 195 } 196 197 /* 198 * generic get constraint function for shared match/mask registers. 199 */ 200 struct event_constraint * 201 uncore_get_constraint(struct intel_uncore_box *box, struct perf_event *event) 202 { 203 struct intel_uncore_extra_reg *er; 204 struct hw_perf_event_extra *reg1 = &event->hw.extra_reg; 205 struct hw_perf_event_extra *reg2 = &event->hw.branch_reg; 206 unsigned long flags; 207 bool ok = false; 208 209 /* 210 * reg->alloc can be set due to existing state, so for fake box we 211 * need to ignore this, otherwise we might fail to allocate proper 212 * fake state for this extra reg constraint. 213 */ 214 if (reg1->idx == EXTRA_REG_NONE || 215 (!uncore_box_is_fake(box) && reg1->alloc)) 216 return NULL; 217 218 er = &box->shared_regs[reg1->idx]; 219 raw_spin_lock_irqsave(&er->lock, flags); 220 if (!atomic_read(&er->ref) || 221 (er->config1 == reg1->config && er->config2 == reg2->config)) { 222 atomic_inc(&er->ref); 223 er->config1 = reg1->config; 224 er->config2 = reg2->config; 225 ok = true; 226 } 227 raw_spin_unlock_irqrestore(&er->lock, flags); 228 229 if (ok) { 230 if (!uncore_box_is_fake(box)) 231 reg1->alloc = 1; 232 return NULL; 233 } 234 235 return &uncore_constraint_empty; 236 } 237 238 void uncore_put_constraint(struct intel_uncore_box *box, struct perf_event *event) 239 { 240 struct intel_uncore_extra_reg *er; 241 struct hw_perf_event_extra *reg1 = &event->hw.extra_reg; 242 243 /* 244 * Only put constraint if extra reg was actually allocated. Also 245 * takes care of event which do not use an extra shared reg. 246 * 247 * Also, if this is a fake box we shouldn't touch any event state 248 * (reg->alloc) and we don't care about leaving inconsistent box 249 * state either since it will be thrown out. 250 */ 251 if (uncore_box_is_fake(box) || !reg1->alloc) 252 return; 253 254 er = &box->shared_regs[reg1->idx]; 255 atomic_dec(&er->ref); 256 reg1->alloc = 0; 257 } 258 259 u64 uncore_shared_reg_config(struct intel_uncore_box *box, int idx) 260 { 261 struct intel_uncore_extra_reg *er; 262 unsigned long flags; 263 u64 config; 264 265 er = &box->shared_regs[idx]; 266 267 raw_spin_lock_irqsave(&er->lock, flags); 268 config = er->config; 269 raw_spin_unlock_irqrestore(&er->lock, flags); 270 271 return config; 272 } 273 274 static void uncore_assign_hw_event(struct intel_uncore_box *box, 275 struct perf_event *event, int idx) 276 { 277 struct hw_perf_event *hwc = &event->hw; 278 279 hwc->idx = idx; 280 hwc->last_tag = ++box->tags[idx]; 281 282 if (uncore_pmc_fixed(hwc->idx)) { 283 hwc->event_base = uncore_fixed_ctr(box); 284 hwc->config_base = uncore_fixed_ctl(box); 285 return; 286 } 287 288 if (intel_generic_uncore_assign_hw_event(event, box)) 289 return; 290 291 hwc->config_base = uncore_event_ctl(box, hwc->idx); 292 hwc->event_base = uncore_perf_ctr(box, hwc->idx); 293 } 294 295 void uncore_perf_event_update(struct intel_uncore_box *box, struct perf_event *event) 296 { 297 u64 prev_count, new_count, delta; 298 int shift; 299 300 if (uncore_pmc_freerunning(event->hw.idx)) 301 shift = 64 - uncore_freerunning_bits(box, event); 302 else if (uncore_pmc_fixed(event->hw.idx)) 303 shift = 64 - uncore_fixed_ctr_bits(box); 304 else 305 shift = 64 - uncore_perf_ctr_bits(box); 306 307 /* the hrtimer might modify the previous event value */ 308 again: 309 prev_count = local64_read(&event->hw.prev_count); 310 new_count = uncore_read_counter(box, event); 311 if (local64_xchg(&event->hw.prev_count, new_count) != prev_count) 312 goto again; 313 314 delta = (new_count << shift) - (prev_count << shift); 315 delta >>= shift; 316 317 local64_add(delta, &event->count); 318 } 319 320 /* 321 * The overflow interrupt is unavailable for SandyBridge-EP, is broken 322 * for SandyBridge. So we use hrtimer to periodically poll the counter 323 * to avoid overflow. 324 */ 325 static enum hrtimer_restart uncore_pmu_hrtimer(struct hrtimer *hrtimer) 326 { 327 struct intel_uncore_box *box; 328 struct perf_event *event; 329 int bit; 330 331 box = container_of(hrtimer, struct intel_uncore_box, hrtimer); 332 if (!box->n_active || box->cpu != smp_processor_id()) 333 return HRTIMER_NORESTART; 334 335 /* 336 * handle boxes with an active event list as opposed to active 337 * counters 338 */ 339 list_for_each_entry(event, &box->active_list, active_entry) { 340 uncore_perf_event_update(box, event); 341 } 342 343 for_each_set_bit(bit, box->active_mask, UNCORE_PMC_IDX_MAX) 344 uncore_perf_event_update(box, box->events[bit]); 345 346 hrtimer_forward_now(hrtimer, ns_to_ktime(box->hrtimer_duration)); 347 return HRTIMER_RESTART; 348 } 349 350 void uncore_pmu_start_hrtimer(struct intel_uncore_box *box) 351 { 352 hrtimer_start(&box->hrtimer, ns_to_ktime(box->hrtimer_duration), 353 HRTIMER_MODE_REL_PINNED_HARD); 354 } 355 356 void uncore_pmu_cancel_hrtimer(struct intel_uncore_box *box) 357 { 358 hrtimer_cancel(&box->hrtimer); 359 } 360 361 static void uncore_pmu_init_hrtimer(struct intel_uncore_box *box) 362 { 363 hrtimer_setup(&box->hrtimer, uncore_pmu_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 364 } 365 366 static struct intel_uncore_box *uncore_alloc_box(struct intel_uncore_type *type, 367 int node) 368 { 369 int i, size, numshared = type->num_shared_regs ; 370 struct intel_uncore_box *box; 371 372 size = sizeof(*box) + numshared * sizeof(struct intel_uncore_extra_reg); 373 374 box = kzalloc_node(size, GFP_KERNEL, node); 375 if (!box) 376 return NULL; 377 378 for (i = 0; i < numshared; i++) 379 raw_spin_lock_init(&box->shared_regs[i].lock); 380 381 uncore_pmu_init_hrtimer(box); 382 box->cpu = -1; 383 box->dieid = -1; 384 385 /* set default hrtimer timeout */ 386 box->hrtimer_duration = UNCORE_PMU_HRTIMER_INTERVAL; 387 388 INIT_LIST_HEAD(&box->active_list); 389 390 return box; 391 } 392 393 /* 394 * Using uncore_pmu_event_init pmu event_init callback 395 * as a detection point for uncore events. 396 */ 397 static int uncore_pmu_event_init(struct perf_event *event); 398 399 static bool is_box_event(struct intel_uncore_box *box, struct perf_event *event) 400 { 401 return &box->pmu->pmu == event->pmu; 402 } 403 404 static int 405 uncore_collect_events(struct intel_uncore_box *box, struct perf_event *leader, 406 bool dogrp) 407 { 408 struct perf_event *event; 409 int n, max_count; 410 411 max_count = box->pmu->type->num_counters; 412 if (box->pmu->type->fixed_ctl) 413 max_count++; 414 415 if (box->n_events >= max_count) 416 return -EINVAL; 417 418 n = box->n_events; 419 420 if (is_box_event(box, leader)) { 421 box->event_list[n] = leader; 422 n++; 423 } 424 425 if (!dogrp) 426 return n; 427 428 for_each_sibling_event(event, leader) { 429 if (!is_box_event(box, event) || 430 event->state <= PERF_EVENT_STATE_OFF) 431 continue; 432 433 if (n >= max_count) 434 return -EINVAL; 435 436 box->event_list[n] = event; 437 n++; 438 } 439 return n; 440 } 441 442 static struct event_constraint * 443 uncore_get_event_constraint(struct intel_uncore_box *box, struct perf_event *event) 444 { 445 struct intel_uncore_type *type = box->pmu->type; 446 struct event_constraint *c; 447 448 if (type->ops->get_constraint) { 449 c = type->ops->get_constraint(box, event); 450 if (c) 451 return c; 452 } 453 454 if (event->attr.config == UNCORE_FIXED_EVENT) 455 return &uncore_constraint_fixed; 456 457 if (type->constraints) { 458 for_each_event_constraint(c, type->constraints) { 459 if (constraint_match(c, event->hw.config)) 460 return c; 461 } 462 } 463 464 return &type->unconstrainted; 465 } 466 467 static void uncore_put_event_constraint(struct intel_uncore_box *box, 468 struct perf_event *event) 469 { 470 if (box->pmu->type->ops->put_constraint) 471 box->pmu->type->ops->put_constraint(box, event); 472 } 473 474 static int uncore_assign_events(struct intel_uncore_box *box, int assign[], int n) 475 { 476 unsigned long used_mask[BITS_TO_LONGS(UNCORE_PMC_IDX_MAX)]; 477 struct event_constraint *c; 478 int i, wmin, wmax, ret = 0; 479 struct hw_perf_event *hwc; 480 481 bitmap_zero(used_mask, UNCORE_PMC_IDX_MAX); 482 483 for (i = 0, wmin = UNCORE_PMC_IDX_MAX, wmax = 0; i < n; i++) { 484 c = uncore_get_event_constraint(box, box->event_list[i]); 485 box->event_constraint[i] = c; 486 wmin = min(wmin, c->weight); 487 wmax = max(wmax, c->weight); 488 } 489 490 /* fastpath, try to reuse previous register */ 491 for (i = 0; i < n; i++) { 492 hwc = &box->event_list[i]->hw; 493 c = box->event_constraint[i]; 494 495 /* never assigned */ 496 if (hwc->idx == -1) 497 break; 498 499 /* constraint still honored */ 500 if (!test_bit(hwc->idx, c->idxmsk)) 501 break; 502 503 /* not already used */ 504 if (test_bit(hwc->idx, used_mask)) 505 break; 506 507 __set_bit(hwc->idx, used_mask); 508 if (assign) 509 assign[i] = hwc->idx; 510 } 511 /* slow path */ 512 if (i != n) 513 ret = perf_assign_events(box->event_constraint, n, 514 wmin, wmax, n, assign); 515 516 if (!assign || ret) { 517 for (i = 0; i < n; i++) 518 uncore_put_event_constraint(box, box->event_list[i]); 519 } 520 return ret ? -EINVAL : 0; 521 } 522 523 void uncore_pmu_event_start(struct perf_event *event, int flags) 524 { 525 struct intel_uncore_box *box = uncore_event_to_box(event); 526 int idx = event->hw.idx; 527 528 if (WARN_ON_ONCE(idx == -1 || idx >= UNCORE_PMC_IDX_MAX)) 529 return; 530 531 /* 532 * Free running counter is read-only and always active. 533 * Use the current counter value as start point. 534 * There is no overflow interrupt for free running counter. 535 * Use hrtimer to periodically poll the counter to avoid overflow. 536 */ 537 if (uncore_pmc_freerunning(event->hw.idx)) { 538 list_add_tail(&event->active_entry, &box->active_list); 539 local64_set(&event->hw.prev_count, 540 uncore_read_counter(box, event)); 541 if (box->n_active++ == 0) 542 uncore_pmu_start_hrtimer(box); 543 return; 544 } 545 546 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED))) 547 return; 548 549 event->hw.state = 0; 550 box->events[idx] = event; 551 box->n_active++; 552 __set_bit(idx, box->active_mask); 553 554 local64_set(&event->hw.prev_count, uncore_read_counter(box, event)); 555 uncore_enable_event(box, event); 556 557 if (box->n_active == 1) 558 uncore_pmu_start_hrtimer(box); 559 } 560 561 void uncore_pmu_event_stop(struct perf_event *event, int flags) 562 { 563 struct intel_uncore_box *box = uncore_event_to_box(event); 564 struct hw_perf_event *hwc = &event->hw; 565 566 /* Cannot disable free running counter which is read-only */ 567 if (uncore_pmc_freerunning(hwc->idx)) { 568 list_del(&event->active_entry); 569 if (--box->n_active == 0) 570 uncore_pmu_cancel_hrtimer(box); 571 uncore_perf_event_update(box, event); 572 return; 573 } 574 575 if (__test_and_clear_bit(hwc->idx, box->active_mask)) { 576 uncore_disable_event(box, event); 577 box->n_active--; 578 box->events[hwc->idx] = NULL; 579 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); 580 hwc->state |= PERF_HES_STOPPED; 581 582 if (box->n_active == 0) 583 uncore_pmu_cancel_hrtimer(box); 584 } 585 586 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) { 587 /* 588 * Drain the remaining delta count out of a event 589 * that we are disabling: 590 */ 591 uncore_perf_event_update(box, event); 592 hwc->state |= PERF_HES_UPTODATE; 593 } 594 } 595 596 int uncore_pmu_event_add(struct perf_event *event, int flags) 597 { 598 struct intel_uncore_box *box = uncore_event_to_box(event); 599 struct hw_perf_event *hwc = &event->hw; 600 int assign[UNCORE_PMC_IDX_MAX]; 601 int i, n, ret; 602 603 if (!box) 604 return -ENODEV; 605 606 /* 607 * The free funning counter is assigned in event_init(). 608 * The free running counter event and free running counter 609 * are 1:1 mapped. It doesn't need to be tracked in event_list. 610 */ 611 if (uncore_pmc_freerunning(hwc->idx)) { 612 if (flags & PERF_EF_START) 613 uncore_pmu_event_start(event, 0); 614 return 0; 615 } 616 617 ret = n = uncore_collect_events(box, event, false); 618 if (ret < 0) 619 return ret; 620 621 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED; 622 if (!(flags & PERF_EF_START)) 623 hwc->state |= PERF_HES_ARCH; 624 625 ret = uncore_assign_events(box, assign, n); 626 if (ret) 627 return ret; 628 629 /* save events moving to new counters */ 630 for (i = 0; i < box->n_events; i++) { 631 event = box->event_list[i]; 632 hwc = &event->hw; 633 634 if (hwc->idx == assign[i] && 635 hwc->last_tag == box->tags[assign[i]]) 636 continue; 637 /* 638 * Ensure we don't accidentally enable a stopped 639 * counter simply because we rescheduled. 640 */ 641 if (hwc->state & PERF_HES_STOPPED) 642 hwc->state |= PERF_HES_ARCH; 643 644 uncore_pmu_event_stop(event, PERF_EF_UPDATE); 645 } 646 647 /* reprogram moved events into new counters */ 648 for (i = 0; i < n; i++) { 649 event = box->event_list[i]; 650 hwc = &event->hw; 651 652 if (hwc->idx != assign[i] || 653 hwc->last_tag != box->tags[assign[i]]) 654 uncore_assign_hw_event(box, event, assign[i]); 655 else if (i < box->n_events) 656 continue; 657 658 if (hwc->state & PERF_HES_ARCH) 659 continue; 660 661 uncore_pmu_event_start(event, 0); 662 } 663 box->n_events = n; 664 665 return 0; 666 } 667 668 void uncore_pmu_event_del(struct perf_event *event, int flags) 669 { 670 struct intel_uncore_box *box = uncore_event_to_box(event); 671 int i; 672 673 uncore_pmu_event_stop(event, PERF_EF_UPDATE); 674 675 /* 676 * The event for free running counter is not tracked by event_list. 677 * It doesn't need to force event->hw.idx = -1 to reassign the counter. 678 * Because the event and the free running counter are 1:1 mapped. 679 */ 680 if (uncore_pmc_freerunning(event->hw.idx)) 681 return; 682 683 for (i = 0; i < box->n_events; i++) { 684 if (event == box->event_list[i]) { 685 uncore_put_event_constraint(box, event); 686 687 for (++i; i < box->n_events; i++) 688 box->event_list[i - 1] = box->event_list[i]; 689 690 --box->n_events; 691 break; 692 } 693 } 694 695 event->hw.idx = -1; 696 event->hw.last_tag = ~0ULL; 697 } 698 699 void uncore_pmu_event_read(struct perf_event *event) 700 { 701 struct intel_uncore_box *box = uncore_event_to_box(event); 702 uncore_perf_event_update(box, event); 703 } 704 705 /* 706 * validation ensures the group can be loaded onto the 707 * PMU if it was the only group available. 708 */ 709 static int uncore_validate_group(struct intel_uncore_pmu *pmu, 710 struct perf_event *event) 711 { 712 struct perf_event *leader = event->group_leader; 713 struct intel_uncore_box *fake_box; 714 int ret = -EINVAL, n; 715 716 /* The free running counter is always active. */ 717 if (uncore_pmc_freerunning(event->hw.idx)) 718 return 0; 719 720 fake_box = uncore_alloc_box(pmu->type, NUMA_NO_NODE); 721 if (!fake_box) 722 return -ENOMEM; 723 724 fake_box->pmu = pmu; 725 /* 726 * the event is not yet connected with its 727 * siblings therefore we must first collect 728 * existing siblings, then add the new event 729 * before we can simulate the scheduling 730 */ 731 n = uncore_collect_events(fake_box, leader, true); 732 if (n < 0) 733 goto out; 734 735 fake_box->n_events = n; 736 n = uncore_collect_events(fake_box, event, false); 737 if (n < 0) 738 goto out; 739 740 fake_box->n_events = n; 741 742 ret = uncore_assign_events(fake_box, NULL, n); 743 out: 744 kfree(fake_box); 745 return ret; 746 } 747 748 static int uncore_pmu_event_init(struct perf_event *event) 749 { 750 struct intel_uncore_pmu *pmu; 751 struct intel_uncore_box *box; 752 struct hw_perf_event *hwc = &event->hw; 753 int ret; 754 755 if (event->attr.type != event->pmu->type) 756 return -ENOENT; 757 758 pmu = uncore_event_to_pmu(event); 759 /* no device found for this pmu */ 760 if (!uncore_pmu_available(pmu)) 761 return -ENOENT; 762 763 /* Sampling not supported yet */ 764 if (hwc->sample_period) 765 return -EINVAL; 766 767 /* 768 * Place all uncore events for a particular physical package 769 * onto a single cpu 770 */ 771 if (event->cpu < 0) 772 return -EINVAL; 773 box = uncore_pmu_to_box(pmu, event->cpu); 774 if (!box || box->cpu < 0) 775 return -EINVAL; 776 event->cpu = box->cpu; 777 event->pmu_private = box; 778 779 event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG; 780 781 event->hw.idx = -1; 782 event->hw.last_tag = ~0ULL; 783 event->hw.extra_reg.idx = EXTRA_REG_NONE; 784 event->hw.branch_reg.idx = EXTRA_REG_NONE; 785 786 if (event->attr.config == UNCORE_FIXED_EVENT) { 787 /* no fixed counter */ 788 if (!pmu->type->fixed_ctl) 789 return -EINVAL; 790 /* 791 * if there is only one fixed counter, only the first pmu 792 * can access the fixed counter 793 */ 794 if (pmu->type->single_fixed && pmu->pmu_idx > 0) 795 return -EINVAL; 796 797 /* fixed counters have event field hardcoded to zero */ 798 hwc->config = 0ULL; 799 } else if (is_freerunning_event(event)) { 800 hwc->config = event->attr.config; 801 if (!check_valid_freerunning_event(box, event)) 802 return -EINVAL; 803 event->hw.idx = UNCORE_PMC_IDX_FREERUNNING; 804 /* 805 * The free running counter event and free running counter 806 * are always 1:1 mapped. 807 * The free running counter is always active. 808 * Assign the free running counter here. 809 */ 810 event->hw.event_base = uncore_freerunning_counter(box, event); 811 } else { 812 hwc->config = event->attr.config & 813 (pmu->type->event_mask | ((u64)pmu->type->event_mask_ext << 32)); 814 if (pmu->type->ops->hw_config) { 815 ret = pmu->type->ops->hw_config(box, event); 816 if (ret) 817 return ret; 818 } 819 } 820 821 if (event->group_leader != event) 822 ret = uncore_validate_group(pmu, event); 823 else 824 ret = 0; 825 826 return ret; 827 } 828 829 static void uncore_pmu_enable(struct pmu *pmu) 830 { 831 struct intel_uncore_pmu *uncore_pmu; 832 struct intel_uncore_box *box; 833 834 uncore_pmu = container_of(pmu, struct intel_uncore_pmu, pmu); 835 836 box = uncore_pmu_to_box(uncore_pmu, smp_processor_id()); 837 if (!box) 838 return; 839 840 if (uncore_pmu->type->ops->enable_box) 841 uncore_pmu->type->ops->enable_box(box); 842 } 843 844 static void uncore_pmu_disable(struct pmu *pmu) 845 { 846 struct intel_uncore_pmu *uncore_pmu; 847 struct intel_uncore_box *box; 848 849 uncore_pmu = container_of(pmu, struct intel_uncore_pmu, pmu); 850 851 box = uncore_pmu_to_box(uncore_pmu, smp_processor_id()); 852 if (!box) 853 return; 854 855 if (uncore_pmu->type->ops->disable_box) 856 uncore_pmu->type->ops->disable_box(box); 857 } 858 859 static ssize_t uncore_get_attr_cpumask(struct device *dev, 860 struct device_attribute *attr, char *buf) 861 { 862 struct intel_uncore_pmu *pmu = container_of(dev_get_drvdata(dev), struct intel_uncore_pmu, pmu); 863 864 return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(&pmu->cpu_mask)); 865 } 866 867 static DEVICE_ATTR(cpumask, S_IRUGO, uncore_get_attr_cpumask, NULL); 868 869 static struct attribute *uncore_pmu_attrs[] = { 870 &dev_attr_cpumask.attr, 871 NULL, 872 }; 873 874 static const struct attribute_group uncore_pmu_attr_group = { 875 .attrs = uncore_pmu_attrs, 876 }; 877 878 static inline int uncore_get_box_id(struct intel_uncore_type *type, 879 struct intel_uncore_pmu *pmu) 880 { 881 if (type->boxes) 882 return intel_uncore_find_discovery_unit_id(type->boxes, -1, pmu->pmu_idx); 883 884 return pmu->pmu_idx; 885 } 886 887 void uncore_get_alias_name(char *pmu_name, struct intel_uncore_pmu *pmu) 888 { 889 struct intel_uncore_type *type = pmu->type; 890 891 if (type->num_boxes == 1) 892 sprintf(pmu_name, "uncore_type_%u", type->type_id); 893 else { 894 sprintf(pmu_name, "uncore_type_%u_%d", 895 type->type_id, uncore_get_box_id(type, pmu)); 896 } 897 } 898 899 static void uncore_get_pmu_name(struct intel_uncore_pmu *pmu) 900 { 901 struct intel_uncore_type *type = pmu->type; 902 903 /* 904 * No uncore block name in discovery table. 905 * Use uncore_type_&typeid_&boxid as name. 906 */ 907 if (!type->name) { 908 uncore_get_alias_name(pmu->name, pmu); 909 return; 910 } 911 912 if (type->num_boxes == 1) { 913 if (strlen(type->name) > 0) 914 sprintf(pmu->name, "uncore_%s", type->name); 915 else 916 sprintf(pmu->name, "uncore"); 917 } else { 918 /* 919 * Use the box ID from the discovery table if applicable. 920 */ 921 sprintf(pmu->name, "uncore_%s_%d", type->name, 922 uncore_get_box_id(type, pmu)); 923 } 924 } 925 926 static int uncore_pmu_register(struct intel_uncore_pmu *pmu) 927 { 928 int ret; 929 930 if (!pmu->type->pmu) { 931 pmu->pmu = (struct pmu) { 932 .attr_groups = pmu->type->attr_groups, 933 .task_ctx_nr = perf_invalid_context, 934 .pmu_enable = uncore_pmu_enable, 935 .pmu_disable = uncore_pmu_disable, 936 .event_init = uncore_pmu_event_init, 937 .add = uncore_pmu_event_add, 938 .del = uncore_pmu_event_del, 939 .start = uncore_pmu_event_start, 940 .stop = uncore_pmu_event_stop, 941 .read = uncore_pmu_event_read, 942 .module = THIS_MODULE, 943 .capabilities = PERF_PMU_CAP_NO_EXCLUDE, 944 .attr_update = pmu->type->attr_update, 945 }; 946 } else { 947 pmu->pmu = *pmu->type->pmu; 948 pmu->pmu.attr_groups = pmu->type->attr_groups; 949 pmu->pmu.attr_update = pmu->type->attr_update; 950 } 951 952 uncore_get_pmu_name(pmu); 953 954 ret = perf_pmu_register(&pmu->pmu, pmu->name, -1); 955 if (!ret) 956 uncore_pmu_set_registered(pmu); 957 return ret; 958 } 959 960 static void uncore_pmu_unregister(struct intel_uncore_pmu *pmu) 961 { 962 if (!uncore_pmu_registered(pmu)) 963 return; 964 perf_pmu_unregister(&pmu->pmu); 965 966 /* Keep PMU_BROKEN_BIT sticky. */ 967 uncore_pmu_clear_registered(pmu); 968 } 969 970 static void uncore_free_boxes(struct intel_uncore_pmu *pmu) 971 { 972 int die; 973 974 for (die = 0; die < uncore_max_dies(); die++) 975 kfree(pmu->boxes[die]); 976 kfree(pmu->boxes); 977 } 978 979 static void uncore_type_exit(struct intel_uncore_type *type) 980 { 981 struct intel_uncore_pmu *pmu = type->pmus; 982 int i; 983 984 if (type->cleanup_mapping) 985 type->cleanup_mapping(type); 986 987 if (type->cleanup_extra_boxes) 988 type->cleanup_extra_boxes(type); 989 990 if (pmu) { 991 for (i = 0; i < type->num_boxes; i++, pmu++) { 992 uncore_pmu_unregister(pmu); 993 uncore_free_boxes(pmu); 994 } 995 kfree(type->pmus); 996 type->pmus = NULL; 997 } 998 999 kfree(type->events_group); 1000 type->events_group = NULL; 1001 } 1002 1003 static void uncore_types_exit(struct intel_uncore_type **types) 1004 { 1005 for (; *types; types++) 1006 uncore_type_exit(*types); 1007 } 1008 1009 static int __init uncore_type_init(struct intel_uncore_type *type) 1010 { 1011 struct intel_uncore_pmu *pmus; 1012 size_t size; 1013 int i, j; 1014 1015 pmus = kzalloc_objs(*pmus, type->num_boxes); 1016 if (!pmus) 1017 return -ENOMEM; 1018 1019 size = uncore_max_dies() * sizeof(struct intel_uncore_box *); 1020 1021 for (i = 0; i < type->num_boxes; i++) { 1022 pmus[i].pmu_idx = i; 1023 pmus[i].type = type; 1024 pmus[i].boxes = kzalloc(size, GFP_KERNEL); 1025 if (!pmus[i].boxes) 1026 goto err; 1027 } 1028 1029 type->pmus = pmus; 1030 type->unconstrainted = (struct event_constraint) 1031 __EVENT_CONSTRAINT(0, (1ULL << type->num_counters) - 1, 1032 0, type->num_counters, 0, 0); 1033 1034 if (type->event_descs) { 1035 struct { 1036 struct attribute_group group; 1037 struct attribute *attrs[]; 1038 } *attr_group; 1039 for (i = 0; type->event_descs[i].attr.attr.name; i++); 1040 1041 attr_group = kzalloc_flex(*attr_group, attrs, i + 1); 1042 if (!attr_group) 1043 goto err; 1044 1045 attr_group->group.name = "events"; 1046 attr_group->group.attrs = attr_group->attrs; 1047 1048 for (j = 0; j < i; j++) 1049 attr_group->attrs[j] = &type->event_descs[j].attr.attr; 1050 1051 type->events_group = &attr_group->group; 1052 } 1053 1054 type->pmu_group = &uncore_pmu_attr_group; 1055 1056 if (type->set_mapping) 1057 type->set_mapping(type); 1058 1059 return 0; 1060 1061 err: 1062 for (i = 0; i < type->num_boxes; i++) 1063 kfree(pmus[i].boxes); 1064 kfree(pmus); 1065 1066 return -ENOMEM; 1067 } 1068 1069 static int __init 1070 uncore_types_init(struct intel_uncore_type **types) 1071 { 1072 int ret; 1073 1074 for (; *types; types++) { 1075 ret = uncore_type_init(*types); 1076 if (ret) 1077 return ret; 1078 } 1079 return 0; 1080 } 1081 1082 /* 1083 * Get the die information of a PCI device. 1084 * @pdev: The PCI device. 1085 * @die: The die id which the device maps to. 1086 */ 1087 static int uncore_pci_get_dev_die_info(struct pci_dev *pdev, int *die) 1088 { 1089 *die = uncore_pcibus_to_dieid(pdev->bus); 1090 if (*die < 0) 1091 return -EINVAL; 1092 1093 return 0; 1094 } 1095 1096 static struct intel_uncore_pmu * 1097 uncore_pci_find_dev_pmu_from_types(struct pci_dev *pdev) 1098 { 1099 struct intel_uncore_type **types = uncore_pci_uncores; 1100 struct intel_uncore_discovery_unit *unit; 1101 struct intel_uncore_type *type; 1102 struct rb_node *node; 1103 1104 for (; *types; types++) { 1105 type = *types; 1106 1107 for (node = rb_first(type->boxes); node; node = rb_next(node)) { 1108 unit = rb_entry(node, struct intel_uncore_discovery_unit, node); 1109 if (pdev->devfn == UNCORE_DISCOVERY_PCI_DEVFN(unit->addr) && 1110 pdev->bus->number == UNCORE_DISCOVERY_PCI_BUS(unit->addr) && 1111 pci_domain_nr(pdev->bus) == UNCORE_DISCOVERY_PCI_DOMAIN(unit->addr)) 1112 return &type->pmus[unit->pmu_idx]; 1113 } 1114 } 1115 1116 return NULL; 1117 } 1118 1119 /* 1120 * Find the PMU of a PCI device. 1121 * @pdev: The PCI device. 1122 * @ids: The ID table of the available PCI devices with a PMU. 1123 * If NULL, search the whole uncore_pci_uncores. 1124 */ 1125 static struct intel_uncore_pmu * 1126 uncore_pci_find_dev_pmu(struct pci_dev *pdev, const struct pci_device_id *ids) 1127 { 1128 struct intel_uncore_pmu *pmu = NULL; 1129 struct intel_uncore_type *type; 1130 kernel_ulong_t data; 1131 unsigned int devfn; 1132 1133 if (!ids) 1134 return uncore_pci_find_dev_pmu_from_types(pdev); 1135 1136 while (ids && ids->vendor) { 1137 if ((ids->vendor == pdev->vendor) && 1138 (ids->device == pdev->device)) { 1139 data = ids->driver_data; 1140 devfn = PCI_DEVFN(UNCORE_PCI_DEV_DEV(data), 1141 UNCORE_PCI_DEV_FUNC(data)); 1142 if (devfn == pdev->devfn) { 1143 type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(data)]; 1144 pmu = &type->pmus[UNCORE_PCI_DEV_IDX(data)]; 1145 break; 1146 } 1147 } 1148 ids++; 1149 } 1150 return pmu; 1151 } 1152 1153 static int uncore_box_setup(struct intel_uncore_pmu *pmu, 1154 struct intel_uncore_box *box) 1155 { 1156 int ret; 1157 1158 if (uncore_pmu_broken(pmu)) 1159 return -ENODEV; 1160 1161 ret = uncore_box_init(box); 1162 if (ret) 1163 goto err; 1164 1165 /* First active box registers the pmu. */ 1166 if (atomic_inc_return(&pmu->activeboxes) > 1) 1167 return 0; 1168 1169 ret = uncore_pmu_register(pmu); 1170 if (ret) { 1171 atomic_dec(&pmu->activeboxes); 1172 goto err; 1173 } 1174 1175 return 0; 1176 err: 1177 /* 1178 * If any box fails, mark the per-package PMU as broken regardless of 1179 * whether it was registered or not. 1180 * 1181 * Don't decrement refcnt to avoid other in-die CPUs from trying to set 1182 * up the PMU box again. 1183 * 1184 * Don't kfree box; MSR and MMIO boxes are freed at module exit only. 1185 */ 1186 uncore_pmu_set_broken(pmu); 1187 uncore_box_exit(box); 1188 return ret; 1189 } 1190 1191 /* 1192 * Register the PMU for a PCI device 1193 * @pdev: The PCI device. 1194 * @type: The corresponding PMU type of the device. 1195 * @pmu: The corresponding PMU of the device. 1196 * @die: The die id which the device maps to. 1197 */ 1198 static int uncore_pci_pmu_register(struct pci_dev *pdev, 1199 struct intel_uncore_type *type, 1200 struct intel_uncore_pmu *pmu, 1201 int die) 1202 { 1203 struct intel_uncore_box *box; 1204 int ret; 1205 1206 if (WARN_ON_ONCE(pmu->boxes[die] != NULL)) 1207 return -EINVAL; 1208 1209 box = uncore_alloc_box(type, NUMA_NO_NODE); 1210 if (!box) { 1211 uncore_pmu_set_broken(pmu); 1212 return -ENOMEM; 1213 } 1214 1215 atomic_inc(&box->refcnt); 1216 box->dieid = die; 1217 box->pci_dev = pdev; 1218 box->pmu = pmu; 1219 1220 ret = uncore_box_setup(pmu, box); 1221 if (!ret) 1222 pmu->boxes[die] = box; 1223 else 1224 kfree(box); 1225 1226 return ret; 1227 } 1228 1229 /* 1230 * add a pci uncore device 1231 */ 1232 static int uncore_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 1233 { 1234 struct intel_uncore_type *type; 1235 struct intel_uncore_pmu *pmu = NULL; 1236 int die, ret; 1237 1238 ret = uncore_pci_get_dev_die_info(pdev, &die); 1239 if (ret) 1240 return ret; 1241 1242 if (UNCORE_PCI_DEV_TYPE(id->driver_data) == UNCORE_EXTRA_PCI_DEV) { 1243 int idx = UNCORE_PCI_DEV_IDX(id->driver_data); 1244 1245 uncore_extra_pci_dev[die].dev[idx] = pdev; 1246 pci_set_drvdata(pdev, NULL); 1247 return 0; 1248 } 1249 1250 type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(id->driver_data)]; 1251 1252 /* 1253 * Some platforms, e.g. Knights Landing, use a common PCI device ID 1254 * for multiple instances of an uncore PMU device type. We should check 1255 * PCI slot and func to indicate the uncore box. 1256 */ 1257 if (id->driver_data & ~0xffff) { 1258 struct pci_driver *pci_drv = to_pci_driver(pdev->dev.driver); 1259 1260 pmu = uncore_pci_find_dev_pmu(pdev, pci_drv->id_table); 1261 if (pmu == NULL) 1262 return -ENODEV; 1263 } else { 1264 /* 1265 * for performance monitoring unit with multiple boxes, 1266 * each box has a different function id. 1267 */ 1268 pmu = &type->pmus[UNCORE_PCI_DEV_IDX(id->driver_data)]; 1269 } 1270 1271 ret = uncore_pci_pmu_register(pdev, type, pmu, die); 1272 1273 pci_set_drvdata(pdev, pmu->boxes[die]); 1274 1275 return ret; 1276 } 1277 1278 /* 1279 * Unregister the PMU of a PCI device 1280 * @pmu: The corresponding PMU is unregistered. 1281 * @die: The die id which the device maps to. 1282 */ 1283 static void uncore_pci_pmu_unregister(struct intel_uncore_pmu *pmu, int die) 1284 { 1285 struct intel_uncore_box *box = pmu->boxes[die]; 1286 1287 if (!box) 1288 return; 1289 1290 pmu->boxes[die] = NULL; 1291 if (atomic_dec_return(&pmu->activeboxes) == 0) 1292 uncore_pmu_unregister(pmu); 1293 if (atomic_dec_return(&box->refcnt) == 0) { 1294 uncore_box_exit(box); 1295 kfree(box); 1296 } 1297 } 1298 1299 static void uncore_pci_remove(struct pci_dev *pdev) 1300 { 1301 struct intel_uncore_box *box; 1302 struct intel_uncore_pmu *pmu; 1303 int i, die; 1304 1305 if (uncore_pci_get_dev_die_info(pdev, &die)) 1306 return; 1307 1308 box = pci_get_drvdata(pdev); 1309 if (!box) { 1310 for (i = 0; i < UNCORE_EXTRA_PCI_DEV_MAX; i++) { 1311 if (uncore_extra_pci_dev[die].dev[i] == pdev) { 1312 uncore_extra_pci_dev[die].dev[i] = NULL; 1313 break; 1314 } 1315 } 1316 return; 1317 } 1318 1319 pmu = box->pmu; 1320 1321 pci_set_drvdata(pdev, NULL); 1322 1323 uncore_pci_pmu_unregister(pmu, die); 1324 } 1325 1326 static int uncore_bus_notify(struct notifier_block *nb, 1327 unsigned long action, void *data, 1328 const struct pci_device_id *ids) 1329 { 1330 struct device *dev = data; 1331 struct pci_dev *pdev = to_pci_dev(dev); 1332 struct intel_uncore_pmu *pmu; 1333 int die; 1334 1335 /* Unregister the PMU when the device is going to be deleted. */ 1336 if (action != BUS_NOTIFY_DEL_DEVICE) 1337 return NOTIFY_DONE; 1338 1339 pmu = uncore_pci_find_dev_pmu(pdev, ids); 1340 if (!pmu) 1341 return NOTIFY_DONE; 1342 1343 if (uncore_pci_get_dev_die_info(pdev, &die)) 1344 return NOTIFY_DONE; 1345 1346 uncore_pci_pmu_unregister(pmu, die); 1347 1348 return NOTIFY_OK; 1349 } 1350 1351 static int uncore_pci_sub_bus_notify(struct notifier_block *nb, 1352 unsigned long action, void *data) 1353 { 1354 return uncore_bus_notify(nb, action, data, 1355 uncore_pci_sub_driver->id_table); 1356 } 1357 1358 static struct notifier_block uncore_pci_sub_notifier = { 1359 .notifier_call = uncore_pci_sub_bus_notify, 1360 }; 1361 1362 static void uncore_pci_sub_driver_init(void) 1363 { 1364 const struct pci_device_id *ids = uncore_pci_sub_driver->id_table; 1365 struct intel_uncore_type *type; 1366 struct intel_uncore_pmu *pmu; 1367 struct pci_dev *pci_sub_dev; 1368 bool notify = false; 1369 unsigned int devfn; 1370 int die; 1371 1372 while (ids && ids->vendor) { 1373 pci_sub_dev = NULL; 1374 type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(ids->driver_data)]; 1375 /* 1376 * Search the available device, and register the 1377 * corresponding PMU. 1378 */ 1379 while ((pci_sub_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 1380 ids->device, pci_sub_dev))) { 1381 devfn = PCI_DEVFN(UNCORE_PCI_DEV_DEV(ids->driver_data), 1382 UNCORE_PCI_DEV_FUNC(ids->driver_data)); 1383 if (devfn != pci_sub_dev->devfn) 1384 continue; 1385 1386 pmu = &type->pmus[UNCORE_PCI_DEV_IDX(ids->driver_data)]; 1387 1388 if (uncore_pci_get_dev_die_info(pci_sub_dev, &die)) 1389 continue; 1390 1391 if (!uncore_pci_pmu_register(pci_sub_dev, type, pmu, 1392 die)) 1393 notify = true; 1394 } 1395 ids++; 1396 } 1397 1398 if (notify && bus_register_notifier(&pci_bus_type, &uncore_pci_sub_notifier)) 1399 notify = false; 1400 1401 if (!notify) 1402 uncore_pci_sub_driver = NULL; 1403 } 1404 1405 static int uncore_pci_bus_notify(struct notifier_block *nb, 1406 unsigned long action, void *data) 1407 { 1408 return uncore_bus_notify(nb, action, data, NULL); 1409 } 1410 1411 static struct notifier_block uncore_pci_notifier = { 1412 .notifier_call = uncore_pci_bus_notify, 1413 }; 1414 1415 1416 static void uncore_pci_pmus_register(void) 1417 { 1418 struct intel_uncore_type **types = uncore_pci_uncores; 1419 struct intel_uncore_discovery_unit *unit; 1420 struct intel_uncore_type *type; 1421 struct intel_uncore_pmu *pmu; 1422 struct rb_node *node; 1423 struct pci_dev *pdev; 1424 1425 for (; *types; types++) { 1426 type = *types; 1427 1428 for (node = rb_first(type->boxes); node; node = rb_next(node)) { 1429 unit = rb_entry(node, struct intel_uncore_discovery_unit, node); 1430 pdev = pci_get_domain_bus_and_slot(UNCORE_DISCOVERY_PCI_DOMAIN(unit->addr), 1431 UNCORE_DISCOVERY_PCI_BUS(unit->addr), 1432 UNCORE_DISCOVERY_PCI_DEVFN(unit->addr)); 1433 1434 if (!pdev) 1435 continue; 1436 pmu = &type->pmus[unit->pmu_idx]; 1437 uncore_pci_pmu_register(pdev, type, pmu, unit->die); 1438 } 1439 } 1440 1441 bus_register_notifier(&pci_bus_type, &uncore_pci_notifier); 1442 } 1443 1444 static int __init uncore_pci_init(void) 1445 { 1446 size_t size; 1447 int ret; 1448 1449 size = uncore_max_dies() * sizeof(struct pci_extra_dev); 1450 uncore_extra_pci_dev = kzalloc(size, GFP_KERNEL); 1451 if (!uncore_extra_pci_dev) { 1452 ret = -ENOMEM; 1453 goto err; 1454 } 1455 1456 ret = uncore_types_init(uncore_pci_uncores); 1457 if (ret) 1458 goto errtype; 1459 1460 if (uncore_pci_driver) { 1461 uncore_pci_driver->probe = uncore_pci_probe; 1462 uncore_pci_driver->remove = uncore_pci_remove; 1463 1464 ret = pci_register_driver(uncore_pci_driver); 1465 if (ret) 1466 goto errtype; 1467 } else 1468 uncore_pci_pmus_register(); 1469 1470 if (uncore_pci_sub_driver) 1471 uncore_pci_sub_driver_init(); 1472 1473 pcidrv_registered = true; 1474 return 0; 1475 1476 errtype: 1477 uncore_types_exit(uncore_pci_uncores); 1478 kfree(uncore_extra_pci_dev); 1479 uncore_extra_pci_dev = NULL; 1480 uncore_free_pcibus_map(); 1481 err: 1482 uncore_pci_uncores = empty_uncore; 1483 return ret; 1484 } 1485 1486 static void uncore_pci_exit(void) 1487 { 1488 if (pcidrv_registered) { 1489 pcidrv_registered = false; 1490 if (uncore_pci_sub_driver) 1491 bus_unregister_notifier(&pci_bus_type, &uncore_pci_sub_notifier); 1492 if (uncore_pci_driver) 1493 pci_unregister_driver(uncore_pci_driver); 1494 else 1495 bus_unregister_notifier(&pci_bus_type, &uncore_pci_notifier); 1496 uncore_types_exit(uncore_pci_uncores); 1497 kfree(uncore_extra_pci_dev); 1498 uncore_free_pcibus_map(); 1499 } 1500 } 1501 1502 static bool uncore_die_has_box(struct intel_uncore_type *type, 1503 int die, unsigned int pmu_idx) 1504 { 1505 if (!type->boxes) 1506 return true; 1507 1508 if (intel_uncore_find_discovery_unit_id(type->boxes, die, pmu_idx) < 0) 1509 return false; 1510 1511 return true; 1512 } 1513 1514 static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu, 1515 int new_cpu) 1516 { 1517 struct intel_uncore_pmu *pmu = type->pmus; 1518 struct intel_uncore_box *box; 1519 int i, die; 1520 1521 die = topology_logical_die_id(old_cpu < 0 ? new_cpu : old_cpu); 1522 for (i = 0; i < type->num_boxes; i++, pmu++) { 1523 box = pmu->boxes[die]; 1524 if (!box) 1525 continue; 1526 1527 if (old_cpu < 0) { 1528 WARN_ON_ONCE(box->cpu != -1); 1529 if (uncore_die_has_box(type, die, pmu->pmu_idx) && 1530 !uncore_pmu_broken(pmu)) { 1531 box->cpu = new_cpu; 1532 cpumask_set_cpu(new_cpu, &pmu->cpu_mask); 1533 } 1534 continue; 1535 } 1536 1537 WARN_ON_ONCE(box->cpu != -1 && box->cpu != old_cpu); 1538 cpumask_clear_cpu(old_cpu, &pmu->cpu_mask); 1539 if (new_cpu < 0) { 1540 box->cpu = -1; 1541 continue; 1542 } 1543 1544 /* An inactive box doesn't need migration. */ 1545 if (box->cpu == -1) 1546 continue; 1547 uncore_pmu_cancel_hrtimer(box); 1548 perf_pmu_migrate_context(&pmu->pmu, old_cpu, new_cpu); 1549 box->cpu = new_cpu; 1550 cpumask_set_cpu(new_cpu, &pmu->cpu_mask); 1551 } 1552 } 1553 1554 static void uncore_change_context(struct intel_uncore_type **uncores, 1555 int old_cpu, int new_cpu) 1556 { 1557 for (; *uncores; uncores++) 1558 uncore_change_type_ctx(*uncores, old_cpu, new_cpu); 1559 } 1560 1561 static void uncore_box_unref(struct intel_uncore_type **types, int die) 1562 { 1563 struct intel_uncore_type *type; 1564 struct intel_uncore_pmu *pmu; 1565 struct intel_uncore_box *box; 1566 int i; 1567 1568 for (; *types; types++) { 1569 type = *types; 1570 pmu = type->pmus; 1571 for (i = 0; i < type->num_boxes; i++, pmu++) { 1572 box = pmu->boxes[die]; 1573 if (box && box->cpu >= 0 && 1574 atomic_dec_return(&box->refcnt) == 0) { 1575 if (uncore_box_active(box) && 1576 atomic_dec_return(&pmu->activeboxes) == 0) 1577 uncore_pmu_unregister(pmu); 1578 uncore_box_exit(box); 1579 } 1580 } 1581 } 1582 } 1583 1584 static int uncore_event_cpu_offline(unsigned int cpu) 1585 { 1586 int die, target; 1587 1588 /* Clear the references */ 1589 die = topology_logical_die_id(cpu); 1590 uncore_box_unref(uncore_msr_uncores, die); 1591 uncore_box_unref(uncore_mmio_uncores, die); 1592 1593 /* Check if exiting cpu is used for collecting uncore events */ 1594 if (!cpumask_test_and_clear_cpu(cpu, &uncore_cpu_mask)) 1595 return 0; 1596 1597 /* Find a new cpu to collect uncore events */ 1598 target = cpumask_any_but(topology_die_cpumask(cpu), cpu); 1599 1600 /* Migrate uncore events to the new target */ 1601 if (target < nr_cpu_ids) 1602 cpumask_set_cpu(target, &uncore_cpu_mask); 1603 else 1604 target = -1; 1605 1606 uncore_change_context(uncore_msr_uncores, cpu, target); 1607 uncore_change_context(uncore_mmio_uncores, cpu, target); 1608 uncore_change_context(uncore_pci_uncores, cpu, target); 1609 return 0; 1610 } 1611 1612 static void allocate_boxes(struct intel_uncore_type **types, 1613 unsigned int die, unsigned int cpu) 1614 { 1615 struct intel_uncore_box *box, *tmp; 1616 struct intel_uncore_type *type; 1617 struct intel_uncore_pmu *pmu; 1618 LIST_HEAD(allocated); 1619 int i; 1620 1621 /* Try to allocate all required boxes */ 1622 for (; *types; types++) { 1623 type = *types; 1624 pmu = type->pmus; 1625 for (i = 0; i < type->num_boxes; i++, pmu++) { 1626 if (pmu->boxes[die] || uncore_pmu_broken(pmu)) 1627 continue; 1628 box = uncore_alloc_box(type, cpu_to_node(cpu)); 1629 if (!box) { 1630 uncore_pmu_set_broken(pmu); 1631 goto cleanup; 1632 } 1633 box->pmu = pmu; 1634 box->dieid = die; 1635 list_add(&box->active_list, &allocated); 1636 } 1637 } 1638 /* Install them in the pmus */ 1639 list_for_each_entry_safe(box, tmp, &allocated, active_list) { 1640 list_del_init(&box->active_list); 1641 box->pmu->boxes[die] = box; 1642 } 1643 return; 1644 1645 cleanup: 1646 list_for_each_entry_safe(box, tmp, &allocated, active_list) { 1647 list_del_init(&box->active_list); 1648 kfree(box); 1649 } 1650 } 1651 1652 static int uncore_box_ref(struct intel_uncore_type **types, 1653 int die, unsigned int cpu) 1654 { 1655 struct intel_uncore_type *type; 1656 struct intel_uncore_pmu *pmu; 1657 struct intel_uncore_box *box; 1658 int i; 1659 1660 for (; *types; types++) { 1661 type = *types; 1662 pmu = type->pmus; 1663 for (i = 0; i < type->num_boxes; i++, pmu++) { 1664 box = pmu->boxes[die]; 1665 if (box && box->cpu >= 0 && atomic_inc_return(&box->refcnt) == 1) 1666 uncore_box_setup(pmu, box); 1667 } 1668 } 1669 return 0; 1670 } 1671 1672 static int uncore_event_cpu_online(unsigned int cpu) 1673 { 1674 int die, target; 1675 1676 die = topology_logical_die_id(cpu); 1677 allocate_boxes(uncore_msr_uncores, die, cpu); 1678 allocate_boxes(uncore_mmio_uncores, die, cpu); 1679 1680 /* 1681 * Check if there is an online cpu in the package 1682 * which collects uncore events already. 1683 */ 1684 target = cpumask_any_and(&uncore_cpu_mask, topology_die_cpumask(cpu)); 1685 if (target >= nr_cpu_ids) { 1686 cpumask_set_cpu(cpu, &uncore_cpu_mask); 1687 uncore_change_context(uncore_msr_uncores, -1, cpu); 1688 uncore_change_context(uncore_mmio_uncores, -1, cpu); 1689 uncore_change_context(uncore_pci_uncores, -1, cpu); 1690 } 1691 1692 uncore_box_ref(uncore_msr_uncores, die, cpu); 1693 uncore_box_ref(uncore_mmio_uncores, die, cpu); 1694 return 0; 1695 } 1696 1697 static int __init uncore_pmu_types_init(struct intel_uncore_type **types) 1698 { 1699 int ret = uncore_types_init(types); 1700 if (ret) 1701 uncore_types_exit(types); 1702 1703 return ret; 1704 } 1705 1706 static int uncore_mmio_global_init(int die, u64 ctl) 1707 { 1708 void __iomem *io_addr; 1709 1710 io_addr = ioremap(ctl, sizeof(ctl)); 1711 if (!io_addr) 1712 return -ENOMEM; 1713 1714 /* Clear freeze bit (0) to enable all counters. */ 1715 writel(0, io_addr); 1716 1717 iounmap(io_addr); 1718 return 0; 1719 } 1720 1721 static int uncore_msr_global_init(int die, u64 msr) 1722 { 1723 int cpu = uncore_die_to_cpu(die); 1724 1725 if (cpu == -1) 1726 return -ENODEV; 1727 1728 return wrmsrq_on_cpu(cpu, msr, 0); 1729 } 1730 1731 static const struct uncore_plat_init nhm_uncore_init __initconst = { 1732 .cpu_init = nhm_uncore_cpu_init, 1733 }; 1734 1735 static const struct uncore_plat_init snb_uncore_init __initconst = { 1736 .cpu_init = snb_uncore_cpu_init, 1737 .pci_init = snb_uncore_pci_init, 1738 }; 1739 1740 static const struct uncore_plat_init ivb_uncore_init __initconst = { 1741 .cpu_init = snb_uncore_cpu_init, 1742 .pci_init = ivb_uncore_pci_init, 1743 }; 1744 1745 static const struct uncore_plat_init hsw_uncore_init __initconst = { 1746 .cpu_init = snb_uncore_cpu_init, 1747 .pci_init = hsw_uncore_pci_init, 1748 }; 1749 1750 static const struct uncore_plat_init bdw_uncore_init __initconst = { 1751 .cpu_init = snb_uncore_cpu_init, 1752 .pci_init = bdw_uncore_pci_init, 1753 }; 1754 1755 static const struct uncore_plat_init snbep_uncore_init __initconst = { 1756 .cpu_init = snbep_uncore_cpu_init, 1757 .pci_init = snbep_uncore_pci_init, 1758 }; 1759 1760 static const struct uncore_plat_init nhmex_uncore_init __initconst = { 1761 .cpu_init = nhmex_uncore_cpu_init, 1762 }; 1763 1764 static const struct uncore_plat_init ivbep_uncore_init __initconst = { 1765 .cpu_init = ivbep_uncore_cpu_init, 1766 .pci_init = ivbep_uncore_pci_init, 1767 }; 1768 1769 static const struct uncore_plat_init hswep_uncore_init __initconst = { 1770 .cpu_init = hswep_uncore_cpu_init, 1771 .pci_init = hswep_uncore_pci_init, 1772 }; 1773 1774 static const struct uncore_plat_init bdx_uncore_init __initconst = { 1775 .cpu_init = bdx_uncore_cpu_init, 1776 .pci_init = bdx_uncore_pci_init, 1777 }; 1778 1779 static const struct uncore_plat_init knl_uncore_init __initconst = { 1780 .cpu_init = knl_uncore_cpu_init, 1781 .pci_init = knl_uncore_pci_init, 1782 }; 1783 1784 static const struct uncore_plat_init skl_uncore_init __initconst = { 1785 .cpu_init = skl_uncore_cpu_init, 1786 .pci_init = skl_uncore_pci_init, 1787 }; 1788 1789 static const struct uncore_plat_init skx_uncore_init __initconst = { 1790 .cpu_init = skx_uncore_cpu_init, 1791 .pci_init = skx_uncore_pci_init, 1792 }; 1793 1794 static const struct uncore_plat_init icl_uncore_init __initconst = { 1795 .cpu_init = icl_uncore_cpu_init, 1796 .pci_init = skl_uncore_pci_init, 1797 }; 1798 1799 static const struct uncore_plat_init tgl_uncore_init __initconst = { 1800 .cpu_init = tgl_uncore_cpu_init, 1801 .mmio_init = tgl_uncore_mmio_init, 1802 }; 1803 1804 static const struct uncore_plat_init tgl_l_uncore_init __initconst = { 1805 .cpu_init = tgl_uncore_cpu_init, 1806 .mmio_init = tgl_l_uncore_mmio_init, 1807 }; 1808 1809 static const struct uncore_plat_init rkl_uncore_init __initconst = { 1810 .cpu_init = tgl_uncore_cpu_init, 1811 .pci_init = skl_uncore_pci_init, 1812 }; 1813 1814 static const struct uncore_plat_init adl_uncore_init __initconst = { 1815 .cpu_init = adl_uncore_cpu_init, 1816 .mmio_init = adl_uncore_mmio_init, 1817 }; 1818 1819 static const struct uncore_plat_init mtl_uncore_init __initconst = { 1820 .cpu_init = mtl_uncore_cpu_init, 1821 .mmio_init = adl_uncore_mmio_init, 1822 }; 1823 1824 static const struct uncore_plat_init lnl_uncore_init __initconst = { 1825 .cpu_init = lnl_uncore_cpu_init, 1826 .mmio_init = lnl_uncore_mmio_init, 1827 }; 1828 1829 static const struct uncore_plat_init ptl_uncore_init __initconst = { 1830 .cpu_init = ptl_uncore_cpu_init, 1831 .mmio_init = ptl_uncore_mmio_init, 1832 .domain[0].discovery_base = UNCORE_DISCOVERY_MSR, 1833 .domain[0].global_init = uncore_mmio_global_init, 1834 }; 1835 1836 static const struct uncore_plat_init nvl_uncore_init __initconst = { 1837 .cpu_init = nvl_uncore_cpu_init, 1838 .mmio_init = ptl_uncore_mmio_init, 1839 .domain[0].discovery_base = PACKAGE_UNCORE_DISCOVERY_MSR, 1840 .domain[0].global_init = uncore_mmio_global_init, 1841 }; 1842 1843 static const struct uncore_plat_init icx_uncore_init __initconst = { 1844 .cpu_init = icx_uncore_cpu_init, 1845 .pci_init = icx_uncore_pci_init, 1846 .mmio_init = icx_uncore_mmio_init, 1847 }; 1848 1849 static const struct uncore_plat_init snr_uncore_init __initconst = { 1850 .cpu_init = snr_uncore_cpu_init, 1851 .pci_init = snr_uncore_pci_init, 1852 .mmio_init = snr_uncore_mmio_init, 1853 }; 1854 1855 static const struct uncore_plat_init spr_uncore_init __initconst = { 1856 .cpu_init = spr_uncore_cpu_init, 1857 .pci_init = spr_uncore_pci_init, 1858 .mmio_init = spr_uncore_mmio_init, 1859 .domain[0].base_is_pci = true, 1860 .domain[0].discovery_base = UNCORE_DISCOVERY_TABLE_DEVICE, 1861 .domain[0].units_ignore = spr_uncore_units_ignore, 1862 }; 1863 1864 static const struct uncore_plat_init gnr_uncore_init __initconst = { 1865 .cpu_init = gnr_uncore_cpu_init, 1866 .pci_init = gnr_uncore_pci_init, 1867 .mmio_init = gnr_uncore_mmio_init, 1868 .domain[0].base_is_pci = true, 1869 .domain[0].discovery_base = UNCORE_DISCOVERY_TABLE_DEVICE, 1870 .domain[0].units_ignore = gnr_uncore_units_ignore, 1871 .domain[0].global_init = uncore_msr_global_init, 1872 }; 1873 1874 static const struct uncore_plat_init dmr_uncore_init __initconst = { 1875 .pci_init = dmr_uncore_pci_init, 1876 .mmio_init = dmr_uncore_mmio_init, 1877 .domain[0].base_is_pci = true, 1878 .domain[0].discovery_base = DMR_UNCORE_DISCOVERY_TABLE_DEVICE, 1879 .domain[0].units_ignore = dmr_uncore_imh_units_ignore, 1880 .domain[1].discovery_base = CBB_UNCORE_DISCOVERY_MSR, 1881 .domain[1].units_ignore = dmr_uncore_cbb_units_ignore, 1882 .domain[1].global_init = uncore_mmio_global_init, 1883 }; 1884 1885 static const struct uncore_plat_init generic_uncore_init __initconst = { 1886 .cpu_init = intel_uncore_generic_uncore_cpu_init, 1887 .pci_init = intel_uncore_generic_uncore_pci_init, 1888 .mmio_init = intel_uncore_generic_uncore_mmio_init, 1889 .domain[0].base_is_pci = true, 1890 .domain[0].discovery_base = PCI_ANY_ID, 1891 .domain[1].discovery_base = UNCORE_DISCOVERY_MSR, 1892 }; 1893 1894 static const struct x86_cpu_id intel_uncore_match[] __initconst = { 1895 X86_MATCH_VFM(INTEL_NEHALEM_EP, &nhm_uncore_init), 1896 X86_MATCH_VFM(INTEL_NEHALEM, &nhm_uncore_init), 1897 X86_MATCH_VFM(INTEL_WESTMERE, &nhm_uncore_init), 1898 X86_MATCH_VFM(INTEL_WESTMERE_EP, &nhm_uncore_init), 1899 X86_MATCH_VFM(INTEL_SANDYBRIDGE, &snb_uncore_init), 1900 X86_MATCH_VFM(INTEL_IVYBRIDGE, &ivb_uncore_init), 1901 X86_MATCH_VFM(INTEL_HASWELL, &hsw_uncore_init), 1902 X86_MATCH_VFM(INTEL_HASWELL_L, &hsw_uncore_init), 1903 X86_MATCH_VFM(INTEL_HASWELL_G, &hsw_uncore_init), 1904 X86_MATCH_VFM(INTEL_BROADWELL, &bdw_uncore_init), 1905 X86_MATCH_VFM(INTEL_BROADWELL_G, &bdw_uncore_init), 1906 X86_MATCH_VFM(INTEL_SANDYBRIDGE_X, &snbep_uncore_init), 1907 X86_MATCH_VFM(INTEL_NEHALEM_EX, &nhmex_uncore_init), 1908 X86_MATCH_VFM(INTEL_WESTMERE_EX, &nhmex_uncore_init), 1909 X86_MATCH_VFM(INTEL_IVYBRIDGE_X, &ivbep_uncore_init), 1910 X86_MATCH_VFM(INTEL_HASWELL_X, &hswep_uncore_init), 1911 X86_MATCH_VFM(INTEL_BROADWELL_X, &bdx_uncore_init), 1912 X86_MATCH_VFM(INTEL_BROADWELL_D, &bdx_uncore_init), 1913 X86_MATCH_VFM(INTEL_XEON_PHI_KNL, &knl_uncore_init), 1914 X86_MATCH_VFM(INTEL_XEON_PHI_KNM, &knl_uncore_init), 1915 X86_MATCH_VFM(INTEL_SKYLAKE, &skl_uncore_init), 1916 X86_MATCH_VFM(INTEL_SKYLAKE_L, &skl_uncore_init), 1917 X86_MATCH_VFM(INTEL_SKYLAKE_X, &skx_uncore_init), 1918 X86_MATCH_VFM(INTEL_KABYLAKE_L, &skl_uncore_init), 1919 X86_MATCH_VFM(INTEL_KABYLAKE, &skl_uncore_init), 1920 X86_MATCH_VFM(INTEL_COMETLAKE_L, &skl_uncore_init), 1921 X86_MATCH_VFM(INTEL_COMETLAKE, &skl_uncore_init), 1922 X86_MATCH_VFM(INTEL_ICELAKE_L, &icl_uncore_init), 1923 X86_MATCH_VFM(INTEL_ICELAKE_NNPI, &icl_uncore_init), 1924 X86_MATCH_VFM(INTEL_ICELAKE, &icl_uncore_init), 1925 X86_MATCH_VFM(INTEL_ICELAKE_D, &icx_uncore_init), 1926 X86_MATCH_VFM(INTEL_ICELAKE_X, &icx_uncore_init), 1927 X86_MATCH_VFM(INTEL_TIGERLAKE_L, &tgl_l_uncore_init), 1928 X86_MATCH_VFM(INTEL_TIGERLAKE, &tgl_uncore_init), 1929 X86_MATCH_VFM(INTEL_ROCKETLAKE, &rkl_uncore_init), 1930 X86_MATCH_VFM(INTEL_ALDERLAKE, &adl_uncore_init), 1931 X86_MATCH_VFM(INTEL_ALDERLAKE_L, &adl_uncore_init), 1932 X86_MATCH_VFM(INTEL_RAPTORLAKE, &adl_uncore_init), 1933 X86_MATCH_VFM(INTEL_RAPTORLAKE_P, &adl_uncore_init), 1934 X86_MATCH_VFM(INTEL_RAPTORLAKE_S, &adl_uncore_init), 1935 X86_MATCH_VFM(INTEL_METEORLAKE, &mtl_uncore_init), 1936 X86_MATCH_VFM(INTEL_METEORLAKE_L, &mtl_uncore_init), 1937 X86_MATCH_VFM(INTEL_ARROWLAKE, &mtl_uncore_init), 1938 X86_MATCH_VFM(INTEL_ARROWLAKE_U, &mtl_uncore_init), 1939 X86_MATCH_VFM(INTEL_ARROWLAKE_H, &mtl_uncore_init), 1940 X86_MATCH_VFM(INTEL_LUNARLAKE_M, &lnl_uncore_init), 1941 X86_MATCH_VFM(INTEL_PANTHERLAKE_L, &ptl_uncore_init), 1942 X86_MATCH_VFM(INTEL_WILDCATLAKE_L, &ptl_uncore_init), 1943 X86_MATCH_VFM(INTEL_NOVALAKE, &nvl_uncore_init), 1944 X86_MATCH_VFM(INTEL_NOVALAKE_L, &nvl_uncore_init), 1945 X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, &spr_uncore_init), 1946 X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, &spr_uncore_init), 1947 X86_MATCH_VFM(INTEL_GRANITERAPIDS_X, &gnr_uncore_init), 1948 X86_MATCH_VFM(INTEL_GRANITERAPIDS_D, &gnr_uncore_init), 1949 X86_MATCH_VFM(INTEL_ATOM_TREMONT_D, &snr_uncore_init), 1950 X86_MATCH_VFM(INTEL_ATOM_GRACEMONT, &adl_uncore_init), 1951 X86_MATCH_VFM(INTEL_ATOM_CRESTMONT_X, &gnr_uncore_init), 1952 X86_MATCH_VFM(INTEL_ATOM_CRESTMONT, &gnr_uncore_init), 1953 X86_MATCH_VFM(INTEL_ATOM_DARKMONT_X, &gnr_uncore_init), 1954 X86_MATCH_VFM(INTEL_DIAMONDRAPIDS_X, &dmr_uncore_init), 1955 {}, 1956 }; 1957 MODULE_DEVICE_TABLE(x86cpu, intel_uncore_match); 1958 1959 static bool uncore_use_discovery(struct uncore_plat_init *config) 1960 { 1961 for (int i = 0; i < UNCORE_DISCOVERY_DOMAINS; i++) { 1962 if (config->domain[i].discovery_base) 1963 return true; 1964 } 1965 1966 return false; 1967 } 1968 1969 static int __init intel_uncore_init(void) 1970 { 1971 const struct x86_cpu_id *id; 1972 struct uncore_plat_init *uncore_init; 1973 int pret = 0, cret = 0, mret = 0, ret; 1974 1975 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) 1976 return -ENODEV; 1977 1978 __uncore_max_dies = 1979 topology_max_packages() * topology_max_dies_per_package(); 1980 1981 id = x86_match_cpu(intel_uncore_match); 1982 if (!id) { 1983 uncore_init = (struct uncore_plat_init *)&generic_uncore_init; 1984 if (uncore_no_discover || !uncore_discovery(uncore_init)) 1985 return -ENODEV; 1986 } else { 1987 uncore_init = (struct uncore_plat_init *)id->driver_data; 1988 if (uncore_no_discover && uncore_use_discovery(uncore_init)) 1989 return -ENODEV; 1990 if (uncore_use_discovery(uncore_init) && 1991 !uncore_discovery(uncore_init)) 1992 return -ENODEV; 1993 } 1994 1995 if (uncore_init->pci_init) { 1996 pret = uncore_init->pci_init(); 1997 if (!pret) 1998 pret = uncore_pci_init(); 1999 } 2000 2001 if (uncore_init->cpu_init) { 2002 uncore_init->cpu_init(); 2003 cret = uncore_pmu_types_init(uncore_msr_uncores); 2004 if (cret) 2005 uncore_msr_uncores = empty_uncore; 2006 } 2007 2008 if (uncore_init->mmio_init) { 2009 uncore_init->mmio_init(); 2010 mret = uncore_pmu_types_init(uncore_mmio_uncores); 2011 if (mret) 2012 uncore_mmio_uncores = empty_uncore; 2013 } 2014 2015 if (cret && pret && mret) { 2016 ret = -ENODEV; 2017 goto free_discovery; 2018 } 2019 2020 /* Install hotplug callbacks to setup the targets for each package */ 2021 ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_UNCORE_ONLINE, 2022 "perf/x86/intel/uncore:online", 2023 uncore_event_cpu_online, 2024 uncore_event_cpu_offline); 2025 if (ret) 2026 goto err; 2027 return 0; 2028 2029 err: 2030 uncore_types_exit(uncore_msr_uncores); 2031 uncore_types_exit(uncore_mmio_uncores); 2032 uncore_pci_exit(); 2033 free_discovery: 2034 intel_uncore_clear_discovery_tables(); 2035 return ret; 2036 } 2037 module_init(intel_uncore_init); 2038 2039 static void __exit intel_uncore_exit(void) 2040 { 2041 cpuhp_remove_state(CPUHP_AP_PERF_X86_UNCORE_ONLINE); 2042 uncore_types_exit(uncore_msr_uncores); 2043 uncore_types_exit(uncore_mmio_uncores); 2044 uncore_pci_exit(); 2045 intel_uncore_clear_discovery_tables(); 2046 } 2047 module_exit(intel_uncore_exit); 2048