1 #include "perf_event_intel_uncore.h"
3 static struct intel_uncore_type *empty_uncore[] = { NULL, };
4 struct intel_uncore_type **uncore_msr_uncores = empty_uncore;
5 struct intel_uncore_type **uncore_pci_uncores = empty_uncore;
7 static bool pcidrv_registered;
8 struct pci_driver *uncore_pci_driver;
9 /* pci bus to socket mapping */
10 int uncore_pcibus_to_physid[256] = { [0 ... 255] = -1, };
11 struct pci_dev *uncore_extra_pci_dev[UNCORE_SOCKET_MAX][UNCORE_EXTRA_PCI_DEV_MAX];
13 static DEFINE_RAW_SPINLOCK(uncore_box_lock);
14 /* mask of cpus that collect uncore events */
15 static cpumask_t uncore_cpu_mask;
17 /* constraint for the fixed counter */
18 static struct event_constraint uncore_constraint_fixed =
19 EVENT_CONSTRAINT(~0ULL, 1 << UNCORE_PMC_IDX_FIXED, ~0ULL);
20 struct event_constraint uncore_constraint_empty =
21 EVENT_CONSTRAINT(0, 0, 0);
23 ssize_t uncore_event_show(struct kobject *kobj,
24 struct kobj_attribute *attr, char *buf)
26 struct uncore_event_desc *event =
27 container_of(attr, struct uncore_event_desc, attr);
28 return sprintf(buf, "%s", event->config);
31 struct intel_uncore_pmu *uncore_event_to_pmu(struct perf_event *event)
33 return container_of(event->pmu, struct intel_uncore_pmu, pmu);
36 struct intel_uncore_box *uncore_pmu_to_box(struct intel_uncore_pmu *pmu, int cpu)
38 struct intel_uncore_box *box;
40 box = *per_cpu_ptr(pmu->box, cpu);
44 raw_spin_lock(&uncore_box_lock);
45 /* Recheck in lock to handle races. */
46 if (*per_cpu_ptr(pmu->box, cpu))
48 list_for_each_entry(box, &pmu->box_list, list) {
49 if (box->phys_id == topology_physical_package_id(cpu)) {
50 atomic_inc(&box->refcnt);
51 *per_cpu_ptr(pmu->box, cpu) = box;
56 raw_spin_unlock(&uncore_box_lock);
58 return *per_cpu_ptr(pmu->box, cpu);
61 struct intel_uncore_box *uncore_event_to_box(struct perf_event *event)
64 * perf core schedules event on the basis of cpu, uncore events are
65 * collected by one of the cpus inside a physical package.
67 return uncore_pmu_to_box(uncore_event_to_pmu(event), smp_processor_id());
70 u64 uncore_msr_read_counter(struct intel_uncore_box *box, struct perf_event *event)
74 rdmsrl(event->hw.event_base, count);
80 * generic get constraint function for shared match/mask registers.
82 struct event_constraint *
83 uncore_get_constraint(struct intel_uncore_box *box, struct perf_event *event)
85 struct intel_uncore_extra_reg *er;
86 struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
87 struct hw_perf_event_extra *reg2 = &event->hw.branch_reg;
92 * reg->alloc can be set due to existing state, so for fake box we
93 * need to ignore this, otherwise we might fail to allocate proper
94 * fake state for this extra reg constraint.
96 if (reg1->idx == EXTRA_REG_NONE ||
97 (!uncore_box_is_fake(box) && reg1->alloc))
100 er = &box->shared_regs[reg1->idx];
101 raw_spin_lock_irqsave(&er->lock, flags);
102 if (!atomic_read(&er->ref) ||
103 (er->config1 == reg1->config && er->config2 == reg2->config)) {
104 atomic_inc(&er->ref);
105 er->config1 = reg1->config;
106 er->config2 = reg2->config;
109 raw_spin_unlock_irqrestore(&er->lock, flags);
112 if (!uncore_box_is_fake(box))
117 return &uncore_constraint_empty;
120 void uncore_put_constraint(struct intel_uncore_box *box, struct perf_event *event)
122 struct intel_uncore_extra_reg *er;
123 struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
126 * Only put constraint if extra reg was actually allocated. Also
127 * takes care of event which do not use an extra shared reg.
129 * Also, if this is a fake box we shouldn't touch any event state
130 * (reg->alloc) and we don't care about leaving inconsistent box
131 * state either since it will be thrown out.
133 if (uncore_box_is_fake(box) || !reg1->alloc)
136 er = &box->shared_regs[reg1->idx];
137 atomic_dec(&er->ref);
141 u64 uncore_shared_reg_config(struct intel_uncore_box *box, int idx)
143 struct intel_uncore_extra_reg *er;
147 er = &box->shared_regs[idx];
149 raw_spin_lock_irqsave(&er->lock, flags);
151 raw_spin_unlock_irqrestore(&er->lock, flags);
156 static void uncore_assign_hw_event(struct intel_uncore_box *box, struct perf_event *event, int idx)
158 struct hw_perf_event *hwc = &event->hw;
161 hwc->last_tag = ++box->tags[idx];
163 if (hwc->idx == UNCORE_PMC_IDX_FIXED) {
164 hwc->event_base = uncore_fixed_ctr(box);
165 hwc->config_base = uncore_fixed_ctl(box);
169 hwc->config_base = uncore_event_ctl(box, hwc->idx);
170 hwc->event_base = uncore_perf_ctr(box, hwc->idx);
173 void uncore_perf_event_update(struct intel_uncore_box *box, struct perf_event *event)
175 u64 prev_count, new_count, delta;
178 if (event->hw.idx >= UNCORE_PMC_IDX_FIXED)
179 shift = 64 - uncore_fixed_ctr_bits(box);
181 shift = 64 - uncore_perf_ctr_bits(box);
183 /* the hrtimer might modify the previous event value */
185 prev_count = local64_read(&event->hw.prev_count);
186 new_count = uncore_read_counter(box, event);
187 if (local64_xchg(&event->hw.prev_count, new_count) != prev_count)
190 delta = (new_count << shift) - (prev_count << shift);
193 local64_add(delta, &event->count);
197 * The overflow interrupt is unavailable for SandyBridge-EP, is broken
198 * for SandyBridge. So we use hrtimer to periodically poll the counter
201 static enum hrtimer_restart uncore_pmu_hrtimer(struct hrtimer *hrtimer)
203 struct intel_uncore_box *box;
204 struct perf_event *event;
208 box = container_of(hrtimer, struct intel_uncore_box, hrtimer);
209 if (!box->n_active || box->cpu != smp_processor_id())
210 return HRTIMER_NORESTART;
212 * disable local interrupt to prevent uncore_pmu_event_start/stop
213 * to interrupt the update process
215 local_irq_save(flags);
218 * handle boxes with an active event list as opposed to active
221 list_for_each_entry(event, &box->active_list, active_entry) {
222 uncore_perf_event_update(box, event);
225 for_each_set_bit(bit, box->active_mask, UNCORE_PMC_IDX_MAX)
226 uncore_perf_event_update(box, box->events[bit]);
228 local_irq_restore(flags);
230 hrtimer_forward_now(hrtimer, ns_to_ktime(box->hrtimer_duration));
231 return HRTIMER_RESTART;
234 void uncore_pmu_start_hrtimer(struct intel_uncore_box *box)
236 __hrtimer_start_range_ns(&box->hrtimer,
237 ns_to_ktime(box->hrtimer_duration), 0,
238 HRTIMER_MODE_REL_PINNED, 0);
241 void uncore_pmu_cancel_hrtimer(struct intel_uncore_box *box)
243 hrtimer_cancel(&box->hrtimer);
246 static void uncore_pmu_init_hrtimer(struct intel_uncore_box *box)
248 hrtimer_init(&box->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
249 box->hrtimer.function = uncore_pmu_hrtimer;
252 static struct intel_uncore_box *uncore_alloc_box(struct intel_uncore_type *type, int node)
254 struct intel_uncore_box *box;
257 size = sizeof(*box) + type->num_shared_regs * sizeof(struct intel_uncore_extra_reg);
259 box = kzalloc_node(size, GFP_KERNEL, node);
263 for (i = 0; i < type->num_shared_regs; i++)
264 raw_spin_lock_init(&box->shared_regs[i].lock);
266 uncore_pmu_init_hrtimer(box);
267 atomic_set(&box->refcnt, 1);
271 /* set default hrtimer timeout */
272 box->hrtimer_duration = UNCORE_PMU_HRTIMER_INTERVAL;
274 INIT_LIST_HEAD(&box->active_list);
280 * Using uncore_pmu_event_init pmu event_init callback
281 * as a detection point for uncore events.
283 static int uncore_pmu_event_init(struct perf_event *event);
285 static bool is_uncore_event(struct perf_event *event)
287 return event->pmu->event_init == uncore_pmu_event_init;
291 uncore_collect_events(struct intel_uncore_box *box, struct perf_event *leader, bool dogrp)
293 struct perf_event *event;
296 max_count = box->pmu->type->num_counters;
297 if (box->pmu->type->fixed_ctl)
300 if (box->n_events >= max_count)
305 if (is_uncore_event(leader)) {
306 box->event_list[n] = leader;
313 list_for_each_entry(event, &leader->sibling_list, group_entry) {
314 if (!is_uncore_event(event) ||
315 event->state <= PERF_EVENT_STATE_OFF)
321 box->event_list[n] = event;
327 static struct event_constraint *
328 uncore_get_event_constraint(struct intel_uncore_box *box, struct perf_event *event)
330 struct intel_uncore_type *type = box->pmu->type;
331 struct event_constraint *c;
333 if (type->ops->get_constraint) {
334 c = type->ops->get_constraint(box, event);
339 if (event->attr.config == UNCORE_FIXED_EVENT)
340 return &uncore_constraint_fixed;
342 if (type->constraints) {
343 for_each_event_constraint(c, type->constraints) {
344 if ((event->hw.config & c->cmask) == c->code)
349 return &type->unconstrainted;
352 static void uncore_put_event_constraint(struct intel_uncore_box *box, struct perf_event *event)
354 if (box->pmu->type->ops->put_constraint)
355 box->pmu->type->ops->put_constraint(box, event);
358 static int uncore_assign_events(struct intel_uncore_box *box, int assign[], int n)
360 unsigned long used_mask[BITS_TO_LONGS(UNCORE_PMC_IDX_MAX)];
361 struct event_constraint *c;
362 int i, wmin, wmax, ret = 0;
363 struct hw_perf_event *hwc;
365 bitmap_zero(used_mask, UNCORE_PMC_IDX_MAX);
367 for (i = 0, wmin = UNCORE_PMC_IDX_MAX, wmax = 0; i < n; i++) {
368 hwc = &box->event_list[i]->hw;
369 c = uncore_get_event_constraint(box, box->event_list[i]);
371 wmin = min(wmin, c->weight);
372 wmax = max(wmax, c->weight);
375 /* fastpath, try to reuse previous register */
376 for (i = 0; i < n; i++) {
377 hwc = &box->event_list[i]->hw;
384 /* constraint still honored */
385 if (!test_bit(hwc->idx, c->idxmsk))
388 /* not already used */
389 if (test_bit(hwc->idx, used_mask))
392 __set_bit(hwc->idx, used_mask);
394 assign[i] = hwc->idx;
398 ret = perf_assign_events(box->event_list, n,
401 if (!assign || ret) {
402 for (i = 0; i < n; i++)
403 uncore_put_event_constraint(box, box->event_list[i]);
405 return ret ? -EINVAL : 0;
408 static void uncore_pmu_event_start(struct perf_event *event, int flags)
410 struct intel_uncore_box *box = uncore_event_to_box(event);
411 int idx = event->hw.idx;
413 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
416 if (WARN_ON_ONCE(idx == -1 || idx >= UNCORE_PMC_IDX_MAX))
420 box->events[idx] = event;
422 __set_bit(idx, box->active_mask);
424 local64_set(&event->hw.prev_count, uncore_read_counter(box, event));
425 uncore_enable_event(box, event);
427 if (box->n_active == 1) {
428 uncore_enable_box(box);
429 uncore_pmu_start_hrtimer(box);
433 static void uncore_pmu_event_stop(struct perf_event *event, int flags)
435 struct intel_uncore_box *box = uncore_event_to_box(event);
436 struct hw_perf_event *hwc = &event->hw;
438 if (__test_and_clear_bit(hwc->idx, box->active_mask)) {
439 uncore_disable_event(box, event);
441 box->events[hwc->idx] = NULL;
442 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
443 hwc->state |= PERF_HES_STOPPED;
445 if (box->n_active == 0) {
446 uncore_disable_box(box);
447 uncore_pmu_cancel_hrtimer(box);
451 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
453 * Drain the remaining delta count out of a event
454 * that we are disabling:
456 uncore_perf_event_update(box, event);
457 hwc->state |= PERF_HES_UPTODATE;
461 static int uncore_pmu_event_add(struct perf_event *event, int flags)
463 struct intel_uncore_box *box = uncore_event_to_box(event);
464 struct hw_perf_event *hwc = &event->hw;
465 int assign[UNCORE_PMC_IDX_MAX];
471 ret = n = uncore_collect_events(box, event, false);
475 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
476 if (!(flags & PERF_EF_START))
477 hwc->state |= PERF_HES_ARCH;
479 ret = uncore_assign_events(box, assign, n);
483 /* save events moving to new counters */
484 for (i = 0; i < box->n_events; i++) {
485 event = box->event_list[i];
488 if (hwc->idx == assign[i] &&
489 hwc->last_tag == box->tags[assign[i]])
492 * Ensure we don't accidentally enable a stopped
493 * counter simply because we rescheduled.
495 if (hwc->state & PERF_HES_STOPPED)
496 hwc->state |= PERF_HES_ARCH;
498 uncore_pmu_event_stop(event, PERF_EF_UPDATE);
501 /* reprogram moved events into new counters */
502 for (i = 0; i < n; i++) {
503 event = box->event_list[i];
506 if (hwc->idx != assign[i] ||
507 hwc->last_tag != box->tags[assign[i]])
508 uncore_assign_hw_event(box, event, assign[i]);
509 else if (i < box->n_events)
512 if (hwc->state & PERF_HES_ARCH)
515 uncore_pmu_event_start(event, 0);
522 static void uncore_pmu_event_del(struct perf_event *event, int flags)
524 struct intel_uncore_box *box = uncore_event_to_box(event);
527 uncore_pmu_event_stop(event, PERF_EF_UPDATE);
529 for (i = 0; i < box->n_events; i++) {
530 if (event == box->event_list[i]) {
531 uncore_put_event_constraint(box, event);
533 while (++i < box->n_events)
534 box->event_list[i - 1] = box->event_list[i];
542 event->hw.last_tag = ~0ULL;
545 void uncore_pmu_event_read(struct perf_event *event)
547 struct intel_uncore_box *box = uncore_event_to_box(event);
548 uncore_perf_event_update(box, event);
552 * validation ensures the group can be loaded onto the
553 * PMU if it was the only group available.
555 static int uncore_validate_group(struct intel_uncore_pmu *pmu,
556 struct perf_event *event)
558 struct perf_event *leader = event->group_leader;
559 struct intel_uncore_box *fake_box;
560 int ret = -EINVAL, n;
562 fake_box = uncore_alloc_box(pmu->type, NUMA_NO_NODE);
568 * the event is not yet connected with its
569 * siblings therefore we must first collect
570 * existing siblings, then add the new event
571 * before we can simulate the scheduling
573 n = uncore_collect_events(fake_box, leader, true);
577 fake_box->n_events = n;
578 n = uncore_collect_events(fake_box, event, false);
582 fake_box->n_events = n;
584 ret = uncore_assign_events(fake_box, NULL, n);
590 static int uncore_pmu_event_init(struct perf_event *event)
592 struct intel_uncore_pmu *pmu;
593 struct intel_uncore_box *box;
594 struct hw_perf_event *hwc = &event->hw;
597 if (event->attr.type != event->pmu->type)
600 pmu = uncore_event_to_pmu(event);
601 /* no device found for this pmu */
602 if (pmu->func_id < 0)
606 * Uncore PMU does measure at all privilege level all the time.
607 * So it doesn't make sense to specify any exclude bits.
609 if (event->attr.exclude_user || event->attr.exclude_kernel ||
610 event->attr.exclude_hv || event->attr.exclude_idle)
613 /* Sampling not supported yet */
614 if (hwc->sample_period)
618 * Place all uncore events for a particular physical package
623 box = uncore_pmu_to_box(pmu, event->cpu);
624 if (!box || box->cpu < 0)
626 event->cpu = box->cpu;
629 event->hw.last_tag = ~0ULL;
630 event->hw.extra_reg.idx = EXTRA_REG_NONE;
631 event->hw.branch_reg.idx = EXTRA_REG_NONE;
633 if (event->attr.config == UNCORE_FIXED_EVENT) {
634 /* no fixed counter */
635 if (!pmu->type->fixed_ctl)
638 * if there is only one fixed counter, only the first pmu
639 * can access the fixed counter
641 if (pmu->type->single_fixed && pmu->pmu_idx > 0)
644 /* fixed counters have event field hardcoded to zero */
647 hwc->config = event->attr.config & pmu->type->event_mask;
648 if (pmu->type->ops->hw_config) {
649 ret = pmu->type->ops->hw_config(box, event);
655 if (event->group_leader != event)
656 ret = uncore_validate_group(pmu, event);
663 static ssize_t uncore_get_attr_cpumask(struct device *dev,
664 struct device_attribute *attr, char *buf)
666 return cpumap_print_to_pagebuf(true, buf, &uncore_cpu_mask);
669 static DEVICE_ATTR(cpumask, S_IRUGO, uncore_get_attr_cpumask, NULL);
671 static struct attribute *uncore_pmu_attrs[] = {
672 &dev_attr_cpumask.attr,
676 static struct attribute_group uncore_pmu_attr_group = {
677 .attrs = uncore_pmu_attrs,
680 static int uncore_pmu_register(struct intel_uncore_pmu *pmu)
684 if (!pmu->type->pmu) {
685 pmu->pmu = (struct pmu) {
686 .attr_groups = pmu->type->attr_groups,
687 .task_ctx_nr = perf_invalid_context,
688 .event_init = uncore_pmu_event_init,
689 .add = uncore_pmu_event_add,
690 .del = uncore_pmu_event_del,
691 .start = uncore_pmu_event_start,
692 .stop = uncore_pmu_event_stop,
693 .read = uncore_pmu_event_read,
696 pmu->pmu = *pmu->type->pmu;
697 pmu->pmu.attr_groups = pmu->type->attr_groups;
700 if (pmu->type->num_boxes == 1) {
701 if (strlen(pmu->type->name) > 0)
702 sprintf(pmu->name, "uncore_%s", pmu->type->name);
704 sprintf(pmu->name, "uncore");
706 sprintf(pmu->name, "uncore_%s_%d", pmu->type->name,
710 ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
714 static void __init uncore_type_exit(struct intel_uncore_type *type)
718 for (i = 0; i < type->num_boxes; i++)
719 free_percpu(type->pmus[i].box);
722 kfree(type->events_group);
723 type->events_group = NULL;
726 static void __init uncore_types_exit(struct intel_uncore_type **types)
729 for (i = 0; types[i]; i++)
730 uncore_type_exit(types[i]);
733 static int __init uncore_type_init(struct intel_uncore_type *type)
735 struct intel_uncore_pmu *pmus;
736 struct attribute_group *attr_group;
737 struct attribute **attrs;
740 pmus = kzalloc(sizeof(*pmus) * type->num_boxes, GFP_KERNEL);
746 type->unconstrainted = (struct event_constraint)
747 __EVENT_CONSTRAINT(0, (1ULL << type->num_counters) - 1,
748 0, type->num_counters, 0, 0);
750 for (i = 0; i < type->num_boxes; i++) {
751 pmus[i].func_id = -1;
754 INIT_LIST_HEAD(&pmus[i].box_list);
755 pmus[i].box = alloc_percpu(struct intel_uncore_box *);
760 if (type->event_descs) {
762 while (type->event_descs[i].attr.attr.name)
765 attr_group = kzalloc(sizeof(struct attribute *) * (i + 1) +
766 sizeof(*attr_group), GFP_KERNEL);
770 attrs = (struct attribute **)(attr_group + 1);
771 attr_group->name = "events";
772 attr_group->attrs = attrs;
774 for (j = 0; j < i; j++)
775 attrs[j] = &type->event_descs[j].attr.attr;
777 type->events_group = attr_group;
780 type->pmu_group = &uncore_pmu_attr_group;
783 uncore_type_exit(type);
787 static int __init uncore_types_init(struct intel_uncore_type **types)
791 for (i = 0; types[i]; i++) {
792 ret = uncore_type_init(types[i]);
799 uncore_type_exit(types[i]);
804 * add a pci uncore device
806 static int uncore_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
808 struct intel_uncore_pmu *pmu;
809 struct intel_uncore_box *box;
810 struct intel_uncore_type *type;
812 bool first_box = false;
814 phys_id = uncore_pcibus_to_physid[pdev->bus->number];
818 if (UNCORE_PCI_DEV_TYPE(id->driver_data) == UNCORE_EXTRA_PCI_DEV) {
819 int idx = UNCORE_PCI_DEV_IDX(id->driver_data);
820 uncore_extra_pci_dev[phys_id][idx] = pdev;
821 pci_set_drvdata(pdev, NULL);
825 type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(id->driver_data)];
826 box = uncore_alloc_box(type, NUMA_NO_NODE);
831 * for performance monitoring unit with multiple boxes,
832 * each box has a different function id.
834 pmu = &type->pmus[UNCORE_PCI_DEV_IDX(id->driver_data)];
835 if (pmu->func_id < 0)
836 pmu->func_id = pdev->devfn;
838 WARN_ON_ONCE(pmu->func_id != pdev->devfn);
840 box->phys_id = phys_id;
843 pci_set_drvdata(pdev, box);
845 raw_spin_lock(&uncore_box_lock);
846 if (list_empty(&pmu->box_list))
848 list_add_tail(&box->list, &pmu->box_list);
849 raw_spin_unlock(&uncore_box_lock);
852 uncore_pmu_register(pmu);
856 static void uncore_pci_remove(struct pci_dev *pdev)
858 struct intel_uncore_box *box = pci_get_drvdata(pdev);
859 struct intel_uncore_pmu *pmu;
860 int i, cpu, phys_id = uncore_pcibus_to_physid[pdev->bus->number];
861 bool last_box = false;
863 box = pci_get_drvdata(pdev);
865 for (i = 0; i < UNCORE_EXTRA_PCI_DEV_MAX; i++) {
866 if (uncore_extra_pci_dev[phys_id][i] == pdev) {
867 uncore_extra_pci_dev[phys_id][i] = NULL;
871 WARN_ON_ONCE(i >= UNCORE_EXTRA_PCI_DEV_MAX);
876 if (WARN_ON_ONCE(phys_id != box->phys_id))
879 pci_set_drvdata(pdev, NULL);
881 raw_spin_lock(&uncore_box_lock);
882 list_del(&box->list);
883 if (list_empty(&pmu->box_list))
885 raw_spin_unlock(&uncore_box_lock);
887 for_each_possible_cpu(cpu) {
888 if (*per_cpu_ptr(pmu->box, cpu) == box) {
889 *per_cpu_ptr(pmu->box, cpu) = NULL;
890 atomic_dec(&box->refcnt);
894 WARN_ON_ONCE(atomic_read(&box->refcnt) != 1);
898 perf_pmu_unregister(&pmu->pmu);
901 static int __init uncore_pci_init(void)
905 switch (boot_cpu_data.x86_model) {
906 case 45: /* Sandy Bridge-EP */
907 ret = snbep_uncore_pci_init();
909 case 62: /* Ivy Bridge-EP */
910 ret = ivbep_uncore_pci_init();
912 case 63: /* Haswell-EP */
913 ret = hswep_uncore_pci_init();
915 case 42: /* Sandy Bridge */
916 ret = snb_uncore_pci_init();
918 case 58: /* Ivy Bridge */
919 ret = ivb_uncore_pci_init();
921 case 60: /* Haswell */
922 case 69: /* Haswell Celeron */
923 ret = hsw_uncore_pci_init();
932 ret = uncore_types_init(uncore_pci_uncores);
936 uncore_pci_driver->probe = uncore_pci_probe;
937 uncore_pci_driver->remove = uncore_pci_remove;
939 ret = pci_register_driver(uncore_pci_driver);
941 pcidrv_registered = true;
943 uncore_types_exit(uncore_pci_uncores);
948 static void __init uncore_pci_exit(void)
950 if (pcidrv_registered) {
951 pcidrv_registered = false;
952 pci_unregister_driver(uncore_pci_driver);
953 uncore_types_exit(uncore_pci_uncores);
957 /* CPU hot plug/unplug are serialized by cpu_add_remove_lock mutex */
958 static LIST_HEAD(boxes_to_free);
960 static void uncore_kfree_boxes(void)
962 struct intel_uncore_box *box;
964 while (!list_empty(&boxes_to_free)) {
965 box = list_entry(boxes_to_free.next,
966 struct intel_uncore_box, list);
967 list_del(&box->list);
972 static void uncore_cpu_dying(int cpu)
974 struct intel_uncore_type *type;
975 struct intel_uncore_pmu *pmu;
976 struct intel_uncore_box *box;
979 for (i = 0; uncore_msr_uncores[i]; i++) {
980 type = uncore_msr_uncores[i];
981 for (j = 0; j < type->num_boxes; j++) {
982 pmu = &type->pmus[j];
983 box = *per_cpu_ptr(pmu->box, cpu);
984 *per_cpu_ptr(pmu->box, cpu) = NULL;
985 if (box && atomic_dec_and_test(&box->refcnt))
986 list_add(&box->list, &boxes_to_free);
991 static int uncore_cpu_starting(int cpu)
993 struct intel_uncore_type *type;
994 struct intel_uncore_pmu *pmu;
995 struct intel_uncore_box *box, *exist;
996 int i, j, k, phys_id;
998 phys_id = topology_physical_package_id(cpu);
1000 for (i = 0; uncore_msr_uncores[i]; i++) {
1001 type = uncore_msr_uncores[i];
1002 for (j = 0; j < type->num_boxes; j++) {
1003 pmu = &type->pmus[j];
1004 box = *per_cpu_ptr(pmu->box, cpu);
1005 /* called by uncore_cpu_init? */
1006 if (box && box->phys_id >= 0)
1009 for_each_online_cpu(k) {
1010 exist = *per_cpu_ptr(pmu->box, k);
1011 if (exist && exist->phys_id == phys_id) {
1012 atomic_inc(&exist->refcnt);
1013 *per_cpu_ptr(pmu->box, cpu) = exist;
1015 list_add(&box->list,
1024 box->phys_id = phys_id;
1030 static int uncore_cpu_prepare(int cpu, int phys_id)
1032 struct intel_uncore_type *type;
1033 struct intel_uncore_pmu *pmu;
1034 struct intel_uncore_box *box;
1037 for (i = 0; uncore_msr_uncores[i]; i++) {
1038 type = uncore_msr_uncores[i];
1039 for (j = 0; j < type->num_boxes; j++) {
1040 pmu = &type->pmus[j];
1041 if (pmu->func_id < 0)
1044 box = uncore_alloc_box(type, cpu_to_node(cpu));
1049 box->phys_id = phys_id;
1050 *per_cpu_ptr(pmu->box, cpu) = box;
1057 uncore_change_context(struct intel_uncore_type **uncores, int old_cpu, int new_cpu)
1059 struct intel_uncore_type *type;
1060 struct intel_uncore_pmu *pmu;
1061 struct intel_uncore_box *box;
1064 for (i = 0; uncores[i]; i++) {
1066 for (j = 0; j < type->num_boxes; j++) {
1067 pmu = &type->pmus[j];
1069 box = uncore_pmu_to_box(pmu, new_cpu);
1071 box = uncore_pmu_to_box(pmu, old_cpu);
1076 WARN_ON_ONCE(box->cpu != -1);
1081 WARN_ON_ONCE(box->cpu != old_cpu);
1083 uncore_pmu_cancel_hrtimer(box);
1084 perf_pmu_migrate_context(&pmu->pmu,
1094 static void uncore_event_exit_cpu(int cpu)
1096 int i, phys_id, target;
1098 /* if exiting cpu is used for collecting uncore events */
1099 if (!cpumask_test_and_clear_cpu(cpu, &uncore_cpu_mask))
1102 /* find a new cpu to collect uncore events */
1103 phys_id = topology_physical_package_id(cpu);
1105 for_each_online_cpu(i) {
1108 if (phys_id == topology_physical_package_id(i)) {
1114 /* migrate uncore events to the new cpu */
1116 cpumask_set_cpu(target, &uncore_cpu_mask);
1118 uncore_change_context(uncore_msr_uncores, cpu, target);
1119 uncore_change_context(uncore_pci_uncores, cpu, target);
1122 static void uncore_event_init_cpu(int cpu)
1126 phys_id = topology_physical_package_id(cpu);
1127 for_each_cpu(i, &uncore_cpu_mask) {
1128 if (phys_id == topology_physical_package_id(i))
1132 cpumask_set_cpu(cpu, &uncore_cpu_mask);
1134 uncore_change_context(uncore_msr_uncores, -1, cpu);
1135 uncore_change_context(uncore_pci_uncores, -1, cpu);
1138 static int uncore_cpu_notifier(struct notifier_block *self,
1139 unsigned long action, void *hcpu)
1141 unsigned int cpu = (long)hcpu;
1143 /* allocate/free data structure for uncore box */
1144 switch (action & ~CPU_TASKS_FROZEN) {
1145 case CPU_UP_PREPARE:
1146 uncore_cpu_prepare(cpu, -1);
1149 uncore_cpu_starting(cpu);
1151 case CPU_UP_CANCELED:
1153 uncore_cpu_dying(cpu);
1157 uncore_kfree_boxes();
1163 /* select the cpu that collects uncore events */
1164 switch (action & ~CPU_TASKS_FROZEN) {
1165 case CPU_DOWN_FAILED:
1167 uncore_event_init_cpu(cpu);
1169 case CPU_DOWN_PREPARE:
1170 uncore_event_exit_cpu(cpu);
1179 static struct notifier_block uncore_cpu_nb = {
1180 .notifier_call = uncore_cpu_notifier,
1182 * to migrate uncore events, our notifier should be executed
1183 * before perf core's notifier.
1185 .priority = CPU_PRI_PERF + 1,
1188 static void __init uncore_cpu_setup(void *dummy)
1190 uncore_cpu_starting(smp_processor_id());
1193 static int __init uncore_cpu_init(void)
1197 switch (boot_cpu_data.x86_model) {
1198 case 26: /* Nehalem */
1200 case 37: /* Westmere */
1202 nhm_uncore_cpu_init();
1204 case 42: /* Sandy Bridge */
1205 case 58: /* Ivy Bridge */
1206 snb_uncore_cpu_init();
1208 case 45: /* Sandy Bridge-EP */
1209 snbep_uncore_cpu_init();
1211 case 46: /* Nehalem-EX */
1212 case 47: /* Westmere-EX aka. Xeon E7 */
1213 nhmex_uncore_cpu_init();
1215 case 62: /* Ivy Bridge-EP */
1216 ivbep_uncore_cpu_init();
1218 case 63: /* Haswell-EP */
1219 hswep_uncore_cpu_init();
1225 ret = uncore_types_init(uncore_msr_uncores);
1232 static int __init uncore_pmus_register(void)
1234 struct intel_uncore_pmu *pmu;
1235 struct intel_uncore_type *type;
1238 for (i = 0; uncore_msr_uncores[i]; i++) {
1239 type = uncore_msr_uncores[i];
1240 for (j = 0; j < type->num_boxes; j++) {
1241 pmu = &type->pmus[j];
1242 uncore_pmu_register(pmu);
1249 static void __init uncore_cpumask_init(void)
1254 * ony invoke once from msr or pci init code
1256 if (!cpumask_empty(&uncore_cpu_mask))
1259 cpu_notifier_register_begin();
1261 for_each_online_cpu(cpu) {
1262 int i, phys_id = topology_physical_package_id(cpu);
1264 for_each_cpu(i, &uncore_cpu_mask) {
1265 if (phys_id == topology_physical_package_id(i)) {
1273 uncore_cpu_prepare(cpu, phys_id);
1274 uncore_event_init_cpu(cpu);
1276 on_each_cpu(uncore_cpu_setup, NULL, 1);
1278 __register_cpu_notifier(&uncore_cpu_nb);
1280 cpu_notifier_register_done();
1284 static int __init intel_uncore_init(void)
1288 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
1291 if (cpu_has_hypervisor)
1294 ret = uncore_pci_init();
1297 ret = uncore_cpu_init();
1302 uncore_cpumask_init();
1304 uncore_pmus_register();
1309 device_initcall(intel_uncore_init);