d5c6d3b340c503cebeab5d79d683a3739fb362fe
[linux-2.6-microblaze.git] / arch / x86 / events / intel / uncore.c
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 "uncore.h"
7
8 static struct intel_uncore_type *empty_uncore[] = { NULL, };
9 struct intel_uncore_type **uncore_msr_uncores = empty_uncore;
10 struct intel_uncore_type **uncore_pci_uncores = empty_uncore;
11 struct intel_uncore_type **uncore_mmio_uncores = empty_uncore;
12
13 static bool pcidrv_registered;
14 struct pci_driver *uncore_pci_driver;
15 /* pci bus to socket mapping */
16 DEFINE_RAW_SPINLOCK(pci2phy_map_lock);
17 struct list_head pci2phy_map_head = LIST_HEAD_INIT(pci2phy_map_head);
18 struct pci_extra_dev *uncore_extra_pci_dev;
19 int __uncore_max_dies;
20
21 /* mask of cpus that collect uncore events */
22 static cpumask_t uncore_cpu_mask;
23
24 /* constraint for the fixed counter */
25 static struct event_constraint uncore_constraint_fixed =
26         EVENT_CONSTRAINT(~0ULL, 1 << UNCORE_PMC_IDX_FIXED, ~0ULL);
27 struct event_constraint uncore_constraint_empty =
28         EVENT_CONSTRAINT(0, 0, 0);
29
30 MODULE_LICENSE("GPL");
31
32 int uncore_pcibus_to_physid(struct pci_bus *bus)
33 {
34         struct pci2phy_map *map;
35         int phys_id = -1;
36
37         raw_spin_lock(&pci2phy_map_lock);
38         list_for_each_entry(map, &pci2phy_map_head, list) {
39                 if (map->segment == pci_domain_nr(bus)) {
40                         phys_id = map->pbus_to_physid[bus->number];
41                         break;
42                 }
43         }
44         raw_spin_unlock(&pci2phy_map_lock);
45
46         return phys_id;
47 }
48
49 static void uncore_free_pcibus_map(void)
50 {
51         struct pci2phy_map *map, *tmp;
52
53         list_for_each_entry_safe(map, tmp, &pci2phy_map_head, list) {
54                 list_del(&map->list);
55                 kfree(map);
56         }
57 }
58
59 struct pci2phy_map *__find_pci2phy_map(int segment)
60 {
61         struct pci2phy_map *map, *alloc = NULL;
62         int i;
63
64         lockdep_assert_held(&pci2phy_map_lock);
65
66 lookup:
67         list_for_each_entry(map, &pci2phy_map_head, list) {
68                 if (map->segment == segment)
69                         goto end;
70         }
71
72         if (!alloc) {
73                 raw_spin_unlock(&pci2phy_map_lock);
74                 alloc = kmalloc(sizeof(struct pci2phy_map), GFP_KERNEL);
75                 raw_spin_lock(&pci2phy_map_lock);
76
77                 if (!alloc)
78                         return NULL;
79
80                 goto lookup;
81         }
82
83         map = alloc;
84         alloc = NULL;
85         map->segment = segment;
86         for (i = 0; i < 256; i++)
87                 map->pbus_to_physid[i] = -1;
88         list_add_tail(&map->list, &pci2phy_map_head);
89
90 end:
91         kfree(alloc);
92         return map;
93 }
94
95 ssize_t uncore_event_show(struct kobject *kobj,
96                           struct kobj_attribute *attr, char *buf)
97 {
98         struct uncore_event_desc *event =
99                 container_of(attr, struct uncore_event_desc, attr);
100         return sprintf(buf, "%s", event->config);
101 }
102
103 struct intel_uncore_box *uncore_pmu_to_box(struct intel_uncore_pmu *pmu, int cpu)
104 {
105         unsigned int dieid = topology_logical_die_id(cpu);
106
107         /*
108          * The unsigned check also catches the '-1' return value for non
109          * existent mappings in the topology map.
110          */
111         return dieid < uncore_max_dies() ? pmu->boxes[dieid] : NULL;
112 }
113
114 u64 uncore_msr_read_counter(struct intel_uncore_box *box, struct perf_event *event)
115 {
116         u64 count;
117
118         rdmsrl(event->hw.event_base, count);
119
120         return count;
121 }
122
123 void uncore_mmio_exit_box(struct intel_uncore_box *box)
124 {
125         if (box->io_addr)
126                 iounmap(box->io_addr);
127 }
128
129 u64 uncore_mmio_read_counter(struct intel_uncore_box *box,
130                              struct perf_event *event)
131 {
132         if (!box->io_addr)
133                 return 0;
134
135         if (!uncore_mmio_is_valid_offset(box, event->hw.event_base))
136                 return 0;
137
138         return readq(box->io_addr + event->hw.event_base);
139 }
140
141 /*
142  * generic get constraint function for shared match/mask registers.
143  */
144 struct event_constraint *
145 uncore_get_constraint(struct intel_uncore_box *box, struct perf_event *event)
146 {
147         struct intel_uncore_extra_reg *er;
148         struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
149         struct hw_perf_event_extra *reg2 = &event->hw.branch_reg;
150         unsigned long flags;
151         bool ok = false;
152
153         /*
154          * reg->alloc can be set due to existing state, so for fake box we
155          * need to ignore this, otherwise we might fail to allocate proper
156          * fake state for this extra reg constraint.
157          */
158         if (reg1->idx == EXTRA_REG_NONE ||
159             (!uncore_box_is_fake(box) && reg1->alloc))
160                 return NULL;
161
162         er = &box->shared_regs[reg1->idx];
163         raw_spin_lock_irqsave(&er->lock, flags);
164         if (!atomic_read(&er->ref) ||
165             (er->config1 == reg1->config && er->config2 == reg2->config)) {
166                 atomic_inc(&er->ref);
167                 er->config1 = reg1->config;
168                 er->config2 = reg2->config;
169                 ok = true;
170         }
171         raw_spin_unlock_irqrestore(&er->lock, flags);
172
173         if (ok) {
174                 if (!uncore_box_is_fake(box))
175                         reg1->alloc = 1;
176                 return NULL;
177         }
178
179         return &uncore_constraint_empty;
180 }
181
182 void uncore_put_constraint(struct intel_uncore_box *box, struct perf_event *event)
183 {
184         struct intel_uncore_extra_reg *er;
185         struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
186
187         /*
188          * Only put constraint if extra reg was actually allocated. Also
189          * takes care of event which do not use an extra shared reg.
190          *
191          * Also, if this is a fake box we shouldn't touch any event state
192          * (reg->alloc) and we don't care about leaving inconsistent box
193          * state either since it will be thrown out.
194          */
195         if (uncore_box_is_fake(box) || !reg1->alloc)
196                 return;
197
198         er = &box->shared_regs[reg1->idx];
199         atomic_dec(&er->ref);
200         reg1->alloc = 0;
201 }
202
203 u64 uncore_shared_reg_config(struct intel_uncore_box *box, int idx)
204 {
205         struct intel_uncore_extra_reg *er;
206         unsigned long flags;
207         u64 config;
208
209         er = &box->shared_regs[idx];
210
211         raw_spin_lock_irqsave(&er->lock, flags);
212         config = er->config;
213         raw_spin_unlock_irqrestore(&er->lock, flags);
214
215         return config;
216 }
217
218 static void uncore_assign_hw_event(struct intel_uncore_box *box,
219                                    struct perf_event *event, int idx)
220 {
221         struct hw_perf_event *hwc = &event->hw;
222
223         hwc->idx = idx;
224         hwc->last_tag = ++box->tags[idx];
225
226         if (uncore_pmc_fixed(hwc->idx)) {
227                 hwc->event_base = uncore_fixed_ctr(box);
228                 hwc->config_base = uncore_fixed_ctl(box);
229                 return;
230         }
231
232         hwc->config_base = uncore_event_ctl(box, hwc->idx);
233         hwc->event_base  = uncore_perf_ctr(box, hwc->idx);
234 }
235
236 void uncore_perf_event_update(struct intel_uncore_box *box, struct perf_event *event)
237 {
238         u64 prev_count, new_count, delta;
239         int shift;
240
241         if (uncore_pmc_freerunning(event->hw.idx))
242                 shift = 64 - uncore_freerunning_bits(box, event);
243         else if (uncore_pmc_fixed(event->hw.idx))
244                 shift = 64 - uncore_fixed_ctr_bits(box);
245         else
246                 shift = 64 - uncore_perf_ctr_bits(box);
247
248         /* the hrtimer might modify the previous event value */
249 again:
250         prev_count = local64_read(&event->hw.prev_count);
251         new_count = uncore_read_counter(box, event);
252         if (local64_xchg(&event->hw.prev_count, new_count) != prev_count)
253                 goto again;
254
255         delta = (new_count << shift) - (prev_count << shift);
256         delta >>= shift;
257
258         local64_add(delta, &event->count);
259 }
260
261 /*
262  * The overflow interrupt is unavailable for SandyBridge-EP, is broken
263  * for SandyBridge. So we use hrtimer to periodically poll the counter
264  * to avoid overflow.
265  */
266 static enum hrtimer_restart uncore_pmu_hrtimer(struct hrtimer *hrtimer)
267 {
268         struct intel_uncore_box *box;
269         struct perf_event *event;
270         unsigned long flags;
271         int bit;
272
273         box = container_of(hrtimer, struct intel_uncore_box, hrtimer);
274         if (!box->n_active || box->cpu != smp_processor_id())
275                 return HRTIMER_NORESTART;
276         /*
277          * disable local interrupt to prevent uncore_pmu_event_start/stop
278          * to interrupt the update process
279          */
280         local_irq_save(flags);
281
282         /*
283          * handle boxes with an active event list as opposed to active
284          * counters
285          */
286         list_for_each_entry(event, &box->active_list, active_entry) {
287                 uncore_perf_event_update(box, event);
288         }
289
290         for_each_set_bit(bit, box->active_mask, UNCORE_PMC_IDX_MAX)
291                 uncore_perf_event_update(box, box->events[bit]);
292
293         local_irq_restore(flags);
294
295         hrtimer_forward_now(hrtimer, ns_to_ktime(box->hrtimer_duration));
296         return HRTIMER_RESTART;
297 }
298
299 void uncore_pmu_start_hrtimer(struct intel_uncore_box *box)
300 {
301         hrtimer_start(&box->hrtimer, ns_to_ktime(box->hrtimer_duration),
302                       HRTIMER_MODE_REL_PINNED);
303 }
304
305 void uncore_pmu_cancel_hrtimer(struct intel_uncore_box *box)
306 {
307         hrtimer_cancel(&box->hrtimer);
308 }
309
310 static void uncore_pmu_init_hrtimer(struct intel_uncore_box *box)
311 {
312         hrtimer_init(&box->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
313         box->hrtimer.function = uncore_pmu_hrtimer;
314 }
315
316 static struct intel_uncore_box *uncore_alloc_box(struct intel_uncore_type *type,
317                                                  int node)
318 {
319         int i, size, numshared = type->num_shared_regs ;
320         struct intel_uncore_box *box;
321
322         size = sizeof(*box) + numshared * sizeof(struct intel_uncore_extra_reg);
323
324         box = kzalloc_node(size, GFP_KERNEL, node);
325         if (!box)
326                 return NULL;
327
328         for (i = 0; i < numshared; i++)
329                 raw_spin_lock_init(&box->shared_regs[i].lock);
330
331         uncore_pmu_init_hrtimer(box);
332         box->cpu = -1;
333         box->pci_phys_id = -1;
334         box->dieid = -1;
335
336         /* set default hrtimer timeout */
337         box->hrtimer_duration = UNCORE_PMU_HRTIMER_INTERVAL;
338
339         INIT_LIST_HEAD(&box->active_list);
340
341         return box;
342 }
343
344 /*
345  * Using uncore_pmu_event_init pmu event_init callback
346  * as a detection point for uncore events.
347  */
348 static int uncore_pmu_event_init(struct perf_event *event);
349
350 static bool is_box_event(struct intel_uncore_box *box, struct perf_event *event)
351 {
352         return &box->pmu->pmu == event->pmu;
353 }
354
355 static int
356 uncore_collect_events(struct intel_uncore_box *box, struct perf_event *leader,
357                       bool dogrp)
358 {
359         struct perf_event *event;
360         int n, max_count;
361
362         max_count = box->pmu->type->num_counters;
363         if (box->pmu->type->fixed_ctl)
364                 max_count++;
365
366         if (box->n_events >= max_count)
367                 return -EINVAL;
368
369         n = box->n_events;
370
371         if (is_box_event(box, leader)) {
372                 box->event_list[n] = leader;
373                 n++;
374         }
375
376         if (!dogrp)
377                 return n;
378
379         for_each_sibling_event(event, leader) {
380                 if (!is_box_event(box, event) ||
381                     event->state <= PERF_EVENT_STATE_OFF)
382                         continue;
383
384                 if (n >= max_count)
385                         return -EINVAL;
386
387                 box->event_list[n] = event;
388                 n++;
389         }
390         return n;
391 }
392
393 static struct event_constraint *
394 uncore_get_event_constraint(struct intel_uncore_box *box, struct perf_event *event)
395 {
396         struct intel_uncore_type *type = box->pmu->type;
397         struct event_constraint *c;
398
399         if (type->ops->get_constraint) {
400                 c = type->ops->get_constraint(box, event);
401                 if (c)
402                         return c;
403         }
404
405         if (event->attr.config == UNCORE_FIXED_EVENT)
406                 return &uncore_constraint_fixed;
407
408         if (type->constraints) {
409                 for_each_event_constraint(c, type->constraints) {
410                         if ((event->hw.config & c->cmask) == c->code)
411                                 return c;
412                 }
413         }
414
415         return &type->unconstrainted;
416 }
417
418 static void uncore_put_event_constraint(struct intel_uncore_box *box,
419                                         struct perf_event *event)
420 {
421         if (box->pmu->type->ops->put_constraint)
422                 box->pmu->type->ops->put_constraint(box, event);
423 }
424
425 static int uncore_assign_events(struct intel_uncore_box *box, int assign[], int n)
426 {
427         unsigned long used_mask[BITS_TO_LONGS(UNCORE_PMC_IDX_MAX)];
428         struct event_constraint *c;
429         int i, wmin, wmax, ret = 0;
430         struct hw_perf_event *hwc;
431
432         bitmap_zero(used_mask, UNCORE_PMC_IDX_MAX);
433
434         for (i = 0, wmin = UNCORE_PMC_IDX_MAX, wmax = 0; i < n; i++) {
435                 c = uncore_get_event_constraint(box, box->event_list[i]);
436                 box->event_constraint[i] = c;
437                 wmin = min(wmin, c->weight);
438                 wmax = max(wmax, c->weight);
439         }
440
441         /* fastpath, try to reuse previous register */
442         for (i = 0; i < n; i++) {
443                 hwc = &box->event_list[i]->hw;
444                 c = box->event_constraint[i];
445
446                 /* never assigned */
447                 if (hwc->idx == -1)
448                         break;
449
450                 /* constraint still honored */
451                 if (!test_bit(hwc->idx, c->idxmsk))
452                         break;
453
454                 /* not already used */
455                 if (test_bit(hwc->idx, used_mask))
456                         break;
457
458                 __set_bit(hwc->idx, used_mask);
459                 if (assign)
460                         assign[i] = hwc->idx;
461         }
462         /* slow path */
463         if (i != n)
464                 ret = perf_assign_events(box->event_constraint, n,
465                                          wmin, wmax, n, assign);
466
467         if (!assign || ret) {
468                 for (i = 0; i < n; i++)
469                         uncore_put_event_constraint(box, box->event_list[i]);
470         }
471         return ret ? -EINVAL : 0;
472 }
473
474 void uncore_pmu_event_start(struct perf_event *event, int flags)
475 {
476         struct intel_uncore_box *box = uncore_event_to_box(event);
477         int idx = event->hw.idx;
478
479         if (WARN_ON_ONCE(idx == -1 || idx >= UNCORE_PMC_IDX_MAX))
480                 return;
481
482         /*
483          * Free running counter is read-only and always active.
484          * Use the current counter value as start point.
485          * There is no overflow interrupt for free running counter.
486          * Use hrtimer to periodically poll the counter to avoid overflow.
487          */
488         if (uncore_pmc_freerunning(event->hw.idx)) {
489                 list_add_tail(&event->active_entry, &box->active_list);
490                 local64_set(&event->hw.prev_count,
491                             uncore_read_counter(box, event));
492                 if (box->n_active++ == 0)
493                         uncore_pmu_start_hrtimer(box);
494                 return;
495         }
496
497         if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
498                 return;
499
500         event->hw.state = 0;
501         box->events[idx] = event;
502         box->n_active++;
503         __set_bit(idx, box->active_mask);
504
505         local64_set(&event->hw.prev_count, uncore_read_counter(box, event));
506         uncore_enable_event(box, event);
507
508         if (box->n_active == 1)
509                 uncore_pmu_start_hrtimer(box);
510 }
511
512 void uncore_pmu_event_stop(struct perf_event *event, int flags)
513 {
514         struct intel_uncore_box *box = uncore_event_to_box(event);
515         struct hw_perf_event *hwc = &event->hw;
516
517         /* Cannot disable free running counter which is read-only */
518         if (uncore_pmc_freerunning(hwc->idx)) {
519                 list_del(&event->active_entry);
520                 if (--box->n_active == 0)
521                         uncore_pmu_cancel_hrtimer(box);
522                 uncore_perf_event_update(box, event);
523                 return;
524         }
525
526         if (__test_and_clear_bit(hwc->idx, box->active_mask)) {
527                 uncore_disable_event(box, event);
528                 box->n_active--;
529                 box->events[hwc->idx] = NULL;
530                 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
531                 hwc->state |= PERF_HES_STOPPED;
532
533                 if (box->n_active == 0)
534                         uncore_pmu_cancel_hrtimer(box);
535         }
536
537         if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
538                 /*
539                  * Drain the remaining delta count out of a event
540                  * that we are disabling:
541                  */
542                 uncore_perf_event_update(box, event);
543                 hwc->state |= PERF_HES_UPTODATE;
544         }
545 }
546
547 int uncore_pmu_event_add(struct perf_event *event, int flags)
548 {
549         struct intel_uncore_box *box = uncore_event_to_box(event);
550         struct hw_perf_event *hwc = &event->hw;
551         int assign[UNCORE_PMC_IDX_MAX];
552         int i, n, ret;
553
554         if (!box)
555                 return -ENODEV;
556
557         /*
558          * The free funning counter is assigned in event_init().
559          * The free running counter event and free running counter
560          * are 1:1 mapped. It doesn't need to be tracked in event_list.
561          */
562         if (uncore_pmc_freerunning(hwc->idx)) {
563                 if (flags & PERF_EF_START)
564                         uncore_pmu_event_start(event, 0);
565                 return 0;
566         }
567
568         ret = n = uncore_collect_events(box, event, false);
569         if (ret < 0)
570                 return ret;
571
572         hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
573         if (!(flags & PERF_EF_START))
574                 hwc->state |= PERF_HES_ARCH;
575
576         ret = uncore_assign_events(box, assign, n);
577         if (ret)
578                 return ret;
579
580         /* save events moving to new counters */
581         for (i = 0; i < box->n_events; i++) {
582                 event = box->event_list[i];
583                 hwc = &event->hw;
584
585                 if (hwc->idx == assign[i] &&
586                         hwc->last_tag == box->tags[assign[i]])
587                         continue;
588                 /*
589                  * Ensure we don't accidentally enable a stopped
590                  * counter simply because we rescheduled.
591                  */
592                 if (hwc->state & PERF_HES_STOPPED)
593                         hwc->state |= PERF_HES_ARCH;
594
595                 uncore_pmu_event_stop(event, PERF_EF_UPDATE);
596         }
597
598         /* reprogram moved events into new counters */
599         for (i = 0; i < n; i++) {
600                 event = box->event_list[i];
601                 hwc = &event->hw;
602
603                 if (hwc->idx != assign[i] ||
604                         hwc->last_tag != box->tags[assign[i]])
605                         uncore_assign_hw_event(box, event, assign[i]);
606                 else if (i < box->n_events)
607                         continue;
608
609                 if (hwc->state & PERF_HES_ARCH)
610                         continue;
611
612                 uncore_pmu_event_start(event, 0);
613         }
614         box->n_events = n;
615
616         return 0;
617 }
618
619 void uncore_pmu_event_del(struct perf_event *event, int flags)
620 {
621         struct intel_uncore_box *box = uncore_event_to_box(event);
622         int i;
623
624         uncore_pmu_event_stop(event, PERF_EF_UPDATE);
625
626         /*
627          * The event for free running counter is not tracked by event_list.
628          * It doesn't need to force event->hw.idx = -1 to reassign the counter.
629          * Because the event and the free running counter are 1:1 mapped.
630          */
631         if (uncore_pmc_freerunning(event->hw.idx))
632                 return;
633
634         for (i = 0; i < box->n_events; i++) {
635                 if (event == box->event_list[i]) {
636                         uncore_put_event_constraint(box, event);
637
638                         for (++i; i < box->n_events; i++)
639                                 box->event_list[i - 1] = box->event_list[i];
640
641                         --box->n_events;
642                         break;
643                 }
644         }
645
646         event->hw.idx = -1;
647         event->hw.last_tag = ~0ULL;
648 }
649
650 void uncore_pmu_event_read(struct perf_event *event)
651 {
652         struct intel_uncore_box *box = uncore_event_to_box(event);
653         uncore_perf_event_update(box, event);
654 }
655
656 /*
657  * validation ensures the group can be loaded onto the
658  * PMU if it was the only group available.
659  */
660 static int uncore_validate_group(struct intel_uncore_pmu *pmu,
661                                 struct perf_event *event)
662 {
663         struct perf_event *leader = event->group_leader;
664         struct intel_uncore_box *fake_box;
665         int ret = -EINVAL, n;
666
667         /* The free running counter is always active. */
668         if (uncore_pmc_freerunning(event->hw.idx))
669                 return 0;
670
671         fake_box = uncore_alloc_box(pmu->type, NUMA_NO_NODE);
672         if (!fake_box)
673                 return -ENOMEM;
674
675         fake_box->pmu = pmu;
676         /*
677          * the event is not yet connected with its
678          * siblings therefore we must first collect
679          * existing siblings, then add the new event
680          * before we can simulate the scheduling
681          */
682         n = uncore_collect_events(fake_box, leader, true);
683         if (n < 0)
684                 goto out;
685
686         fake_box->n_events = n;
687         n = uncore_collect_events(fake_box, event, false);
688         if (n < 0)
689                 goto out;
690
691         fake_box->n_events = n;
692
693         ret = uncore_assign_events(fake_box, NULL, n);
694 out:
695         kfree(fake_box);
696         return ret;
697 }
698
699 static int uncore_pmu_event_init(struct perf_event *event)
700 {
701         struct intel_uncore_pmu *pmu;
702         struct intel_uncore_box *box;
703         struct hw_perf_event *hwc = &event->hw;
704         int ret;
705
706         if (event->attr.type != event->pmu->type)
707                 return -ENOENT;
708
709         pmu = uncore_event_to_pmu(event);
710         /* no device found for this pmu */
711         if (pmu->func_id < 0)
712                 return -ENOENT;
713
714         /* Sampling not supported yet */
715         if (hwc->sample_period)
716                 return -EINVAL;
717
718         /*
719          * Place all uncore events for a particular physical package
720          * onto a single cpu
721          */
722         if (event->cpu < 0)
723                 return -EINVAL;
724         box = uncore_pmu_to_box(pmu, event->cpu);
725         if (!box || box->cpu < 0)
726                 return -EINVAL;
727         event->cpu = box->cpu;
728         event->pmu_private = box;
729
730         event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG;
731
732         event->hw.idx = -1;
733         event->hw.last_tag = ~0ULL;
734         event->hw.extra_reg.idx = EXTRA_REG_NONE;
735         event->hw.branch_reg.idx = EXTRA_REG_NONE;
736
737         if (event->attr.config == UNCORE_FIXED_EVENT) {
738                 /* no fixed counter */
739                 if (!pmu->type->fixed_ctl)
740                         return -EINVAL;
741                 /*
742                  * if there is only one fixed counter, only the first pmu
743                  * can access the fixed counter
744                  */
745                 if (pmu->type->single_fixed && pmu->pmu_idx > 0)
746                         return -EINVAL;
747
748                 /* fixed counters have event field hardcoded to zero */
749                 hwc->config = 0ULL;
750         } else if (is_freerunning_event(event)) {
751                 hwc->config = event->attr.config;
752                 if (!check_valid_freerunning_event(box, event))
753                         return -EINVAL;
754                 event->hw.idx = UNCORE_PMC_IDX_FREERUNNING;
755                 /*
756                  * The free running counter event and free running counter
757                  * are always 1:1 mapped.
758                  * The free running counter is always active.
759                  * Assign the free running counter here.
760                  */
761                 event->hw.event_base = uncore_freerunning_counter(box, event);
762         } else {
763                 hwc->config = event->attr.config &
764                               (pmu->type->event_mask | ((u64)pmu->type->event_mask_ext << 32));
765                 if (pmu->type->ops->hw_config) {
766                         ret = pmu->type->ops->hw_config(box, event);
767                         if (ret)
768                                 return ret;
769                 }
770         }
771
772         if (event->group_leader != event)
773                 ret = uncore_validate_group(pmu, event);
774         else
775                 ret = 0;
776
777         return ret;
778 }
779
780 static void uncore_pmu_enable(struct pmu *pmu)
781 {
782         struct intel_uncore_pmu *uncore_pmu;
783         struct intel_uncore_box *box;
784
785         uncore_pmu = container_of(pmu, struct intel_uncore_pmu, pmu);
786         if (!uncore_pmu)
787                 return;
788
789         box = uncore_pmu_to_box(uncore_pmu, smp_processor_id());
790         if (!box)
791                 return;
792
793         if (uncore_pmu->type->ops->enable_box)
794                 uncore_pmu->type->ops->enable_box(box);
795 }
796
797 static void uncore_pmu_disable(struct pmu *pmu)
798 {
799         struct intel_uncore_pmu *uncore_pmu;
800         struct intel_uncore_box *box;
801
802         uncore_pmu = container_of(pmu, struct intel_uncore_pmu, pmu);
803         if (!uncore_pmu)
804                 return;
805
806         box = uncore_pmu_to_box(uncore_pmu, smp_processor_id());
807         if (!box)
808                 return;
809
810         if (uncore_pmu->type->ops->disable_box)
811                 uncore_pmu->type->ops->disable_box(box);
812 }
813
814 static ssize_t uncore_get_attr_cpumask(struct device *dev,
815                                 struct device_attribute *attr, char *buf)
816 {
817         return cpumap_print_to_pagebuf(true, buf, &uncore_cpu_mask);
818 }
819
820 static DEVICE_ATTR(cpumask, S_IRUGO, uncore_get_attr_cpumask, NULL);
821
822 static struct attribute *uncore_pmu_attrs[] = {
823         &dev_attr_cpumask.attr,
824         NULL,
825 };
826
827 static const struct attribute_group uncore_pmu_attr_group = {
828         .attrs = uncore_pmu_attrs,
829 };
830
831 static int uncore_pmu_register(struct intel_uncore_pmu *pmu)
832 {
833         int ret;
834
835         if (!pmu->type->pmu) {
836                 pmu->pmu = (struct pmu) {
837                         .attr_groups    = pmu->type->attr_groups,
838                         .task_ctx_nr    = perf_invalid_context,
839                         .pmu_enable     = uncore_pmu_enable,
840                         .pmu_disable    = uncore_pmu_disable,
841                         .event_init     = uncore_pmu_event_init,
842                         .add            = uncore_pmu_event_add,
843                         .del            = uncore_pmu_event_del,
844                         .start          = uncore_pmu_event_start,
845                         .stop           = uncore_pmu_event_stop,
846                         .read           = uncore_pmu_event_read,
847                         .module         = THIS_MODULE,
848                         .capabilities   = PERF_PMU_CAP_NO_EXCLUDE,
849                         .attr_update    = pmu->type->attr_update,
850                 };
851         } else {
852                 pmu->pmu = *pmu->type->pmu;
853                 pmu->pmu.attr_groups = pmu->type->attr_groups;
854                 pmu->pmu.attr_update = pmu->type->attr_update;
855         }
856
857         if (pmu->type->num_boxes == 1) {
858                 if (strlen(pmu->type->name) > 0)
859                         sprintf(pmu->name, "uncore_%s", pmu->type->name);
860                 else
861                         sprintf(pmu->name, "uncore");
862         } else {
863                 sprintf(pmu->name, "uncore_%s_%d", pmu->type->name,
864                         pmu->pmu_idx);
865         }
866
867         ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
868         if (!ret)
869                 pmu->registered = true;
870         return ret;
871 }
872
873 static void uncore_pmu_unregister(struct intel_uncore_pmu *pmu)
874 {
875         if (!pmu->registered)
876                 return;
877         perf_pmu_unregister(&pmu->pmu);
878         pmu->registered = false;
879 }
880
881 static void uncore_free_boxes(struct intel_uncore_pmu *pmu)
882 {
883         int die;
884
885         for (die = 0; die < uncore_max_dies(); die++)
886                 kfree(pmu->boxes[die]);
887         kfree(pmu->boxes);
888 }
889
890 static void uncore_type_exit(struct intel_uncore_type *type)
891 {
892         struct intel_uncore_pmu *pmu = type->pmus;
893         int i;
894
895         if (type->cleanup_mapping)
896                 type->cleanup_mapping(type);
897
898         if (pmu) {
899                 for (i = 0; i < type->num_boxes; i++, pmu++) {
900                         uncore_pmu_unregister(pmu);
901                         uncore_free_boxes(pmu);
902                 }
903                 kfree(type->pmus);
904                 type->pmus = NULL;
905         }
906         kfree(type->events_group);
907         type->events_group = NULL;
908 }
909
910 static void uncore_types_exit(struct intel_uncore_type **types)
911 {
912         for (; *types; types++)
913                 uncore_type_exit(*types);
914 }
915
916 static int __init uncore_type_init(struct intel_uncore_type *type, bool setid)
917 {
918         struct intel_uncore_pmu *pmus;
919         size_t size;
920         int i, j;
921
922         pmus = kcalloc(type->num_boxes, sizeof(*pmus), GFP_KERNEL);
923         if (!pmus)
924                 return -ENOMEM;
925
926         size = uncore_max_dies() * sizeof(struct intel_uncore_box *);
927
928         for (i = 0; i < type->num_boxes; i++) {
929                 pmus[i].func_id = setid ? i : -1;
930                 pmus[i].pmu_idx = i;
931                 pmus[i].type    = type;
932                 pmus[i].boxes   = kzalloc(size, GFP_KERNEL);
933                 if (!pmus[i].boxes)
934                         goto err;
935         }
936
937         type->pmus = pmus;
938         type->unconstrainted = (struct event_constraint)
939                 __EVENT_CONSTRAINT(0, (1ULL << type->num_counters) - 1,
940                                 0, type->num_counters, 0, 0);
941
942         if (type->event_descs) {
943                 struct {
944                         struct attribute_group group;
945                         struct attribute *attrs[];
946                 } *attr_group;
947                 for (i = 0; type->event_descs[i].attr.attr.name; i++);
948
949                 attr_group = kzalloc(struct_size(attr_group, attrs, i + 1),
950                                                                 GFP_KERNEL);
951                 if (!attr_group)
952                         goto err;
953
954                 attr_group->group.name = "events";
955                 attr_group->group.attrs = attr_group->attrs;
956
957                 for (j = 0; j < i; j++)
958                         attr_group->attrs[j] = &type->event_descs[j].attr.attr;
959
960                 type->events_group = &attr_group->group;
961         }
962
963         type->pmu_group = &uncore_pmu_attr_group;
964
965         if (type->set_mapping)
966                 type->set_mapping(type);
967
968         return 0;
969
970 err:
971         for (i = 0; i < type->num_boxes; i++)
972                 kfree(pmus[i].boxes);
973         kfree(pmus);
974
975         return -ENOMEM;
976 }
977
978 static int __init
979 uncore_types_init(struct intel_uncore_type **types, bool setid)
980 {
981         int ret;
982
983         for (; *types; types++) {
984                 ret = uncore_type_init(*types, setid);
985                 if (ret)
986                         return ret;
987         }
988         return 0;
989 }
990
991 /*
992  * add a pci uncore device
993  */
994 static int uncore_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
995 {
996         struct intel_uncore_type *type;
997         struct intel_uncore_pmu *pmu = NULL;
998         struct intel_uncore_box *box;
999         int phys_id, die, ret;
1000
1001         phys_id = uncore_pcibus_to_physid(pdev->bus);
1002         if (phys_id < 0)
1003                 return -ENODEV;
1004
1005         die = (topology_max_die_per_package() > 1) ? phys_id :
1006                                         topology_phys_to_logical_pkg(phys_id);
1007         if (die < 0)
1008                 return -EINVAL;
1009
1010         if (UNCORE_PCI_DEV_TYPE(id->driver_data) == UNCORE_EXTRA_PCI_DEV) {
1011                 int idx = UNCORE_PCI_DEV_IDX(id->driver_data);
1012
1013                 uncore_extra_pci_dev[die].dev[idx] = pdev;
1014                 pci_set_drvdata(pdev, NULL);
1015                 return 0;
1016         }
1017
1018         type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(id->driver_data)];
1019
1020         /*
1021          * Some platforms, e.g.  Knights Landing, use a common PCI device ID
1022          * for multiple instances of an uncore PMU device type. We should check
1023          * PCI slot and func to indicate the uncore box.
1024          */
1025         if (id->driver_data & ~0xffff) {
1026                 struct pci_driver *pci_drv = pdev->driver;
1027                 const struct pci_device_id *ids = pci_drv->id_table;
1028                 unsigned int devfn;
1029
1030                 while (ids && ids->vendor) {
1031                         if ((ids->vendor == pdev->vendor) &&
1032                             (ids->device == pdev->device)) {
1033                                 devfn = PCI_DEVFN(UNCORE_PCI_DEV_DEV(ids->driver_data),
1034                                                   UNCORE_PCI_DEV_FUNC(ids->driver_data));
1035                                 if (devfn == pdev->devfn) {
1036                                         pmu = &type->pmus[UNCORE_PCI_DEV_IDX(ids->driver_data)];
1037                                         break;
1038                                 }
1039                         }
1040                         ids++;
1041                 }
1042                 if (pmu == NULL)
1043                         return -ENODEV;
1044         } else {
1045                 /*
1046                  * for performance monitoring unit with multiple boxes,
1047                  * each box has a different function id.
1048                  */
1049                 pmu = &type->pmus[UNCORE_PCI_DEV_IDX(id->driver_data)];
1050         }
1051
1052         if (WARN_ON_ONCE(pmu->boxes[die] != NULL))
1053                 return -EINVAL;
1054
1055         box = uncore_alloc_box(type, NUMA_NO_NODE);
1056         if (!box)
1057                 return -ENOMEM;
1058
1059         if (pmu->func_id < 0)
1060                 pmu->func_id = pdev->devfn;
1061         else
1062                 WARN_ON_ONCE(pmu->func_id != pdev->devfn);
1063
1064         atomic_inc(&box->refcnt);
1065         box->pci_phys_id = phys_id;
1066         box->dieid = die;
1067         box->pci_dev = pdev;
1068         box->pmu = pmu;
1069         uncore_box_init(box);
1070         pci_set_drvdata(pdev, box);
1071
1072         pmu->boxes[die] = box;
1073         if (atomic_inc_return(&pmu->activeboxes) > 1)
1074                 return 0;
1075
1076         /* First active box registers the pmu */
1077         ret = uncore_pmu_register(pmu);
1078         if (ret) {
1079                 pci_set_drvdata(pdev, NULL);
1080                 pmu->boxes[die] = NULL;
1081                 uncore_box_exit(box);
1082                 kfree(box);
1083         }
1084         return ret;
1085 }
1086
1087 static void uncore_pci_remove(struct pci_dev *pdev)
1088 {
1089         struct intel_uncore_box *box;
1090         struct intel_uncore_pmu *pmu;
1091         int i, phys_id, die;
1092
1093         phys_id = uncore_pcibus_to_physid(pdev->bus);
1094
1095         box = pci_get_drvdata(pdev);
1096         if (!box) {
1097                 die = (topology_max_die_per_package() > 1) ? phys_id :
1098                                         topology_phys_to_logical_pkg(phys_id);
1099                 for (i = 0; i < UNCORE_EXTRA_PCI_DEV_MAX; i++) {
1100                         if (uncore_extra_pci_dev[die].dev[i] == pdev) {
1101                                 uncore_extra_pci_dev[die].dev[i] = NULL;
1102                                 break;
1103                         }
1104                 }
1105                 WARN_ON_ONCE(i >= UNCORE_EXTRA_PCI_DEV_MAX);
1106                 return;
1107         }
1108
1109         pmu = box->pmu;
1110         if (WARN_ON_ONCE(phys_id != box->pci_phys_id))
1111                 return;
1112
1113         pci_set_drvdata(pdev, NULL);
1114         pmu->boxes[box->dieid] = NULL;
1115         if (atomic_dec_return(&pmu->activeboxes) == 0)
1116                 uncore_pmu_unregister(pmu);
1117         uncore_box_exit(box);
1118         kfree(box);
1119 }
1120
1121 static int __init uncore_pci_init(void)
1122 {
1123         size_t size;
1124         int ret;
1125
1126         size = uncore_max_dies() * sizeof(struct pci_extra_dev);
1127         uncore_extra_pci_dev = kzalloc(size, GFP_KERNEL);
1128         if (!uncore_extra_pci_dev) {
1129                 ret = -ENOMEM;
1130                 goto err;
1131         }
1132
1133         ret = uncore_types_init(uncore_pci_uncores, false);
1134         if (ret)
1135                 goto errtype;
1136
1137         uncore_pci_driver->probe = uncore_pci_probe;
1138         uncore_pci_driver->remove = uncore_pci_remove;
1139
1140         ret = pci_register_driver(uncore_pci_driver);
1141         if (ret)
1142                 goto errtype;
1143
1144         pcidrv_registered = true;
1145         return 0;
1146
1147 errtype:
1148         uncore_types_exit(uncore_pci_uncores);
1149         kfree(uncore_extra_pci_dev);
1150         uncore_extra_pci_dev = NULL;
1151         uncore_free_pcibus_map();
1152 err:
1153         uncore_pci_uncores = empty_uncore;
1154         return ret;
1155 }
1156
1157 static void uncore_pci_exit(void)
1158 {
1159         if (pcidrv_registered) {
1160                 pcidrv_registered = false;
1161                 pci_unregister_driver(uncore_pci_driver);
1162                 uncore_types_exit(uncore_pci_uncores);
1163                 kfree(uncore_extra_pci_dev);
1164                 uncore_free_pcibus_map();
1165         }
1166 }
1167
1168 static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,
1169                                    int new_cpu)
1170 {
1171         struct intel_uncore_pmu *pmu = type->pmus;
1172         struct intel_uncore_box *box;
1173         int i, die;
1174
1175         die = topology_logical_die_id(old_cpu < 0 ? new_cpu : old_cpu);
1176         for (i = 0; i < type->num_boxes; i++, pmu++) {
1177                 box = pmu->boxes[die];
1178                 if (!box)
1179                         continue;
1180
1181                 if (old_cpu < 0) {
1182                         WARN_ON_ONCE(box->cpu != -1);
1183                         box->cpu = new_cpu;
1184                         continue;
1185                 }
1186
1187                 WARN_ON_ONCE(box->cpu != old_cpu);
1188                 box->cpu = -1;
1189                 if (new_cpu < 0)
1190                         continue;
1191
1192                 uncore_pmu_cancel_hrtimer(box);
1193                 perf_pmu_migrate_context(&pmu->pmu, old_cpu, new_cpu);
1194                 box->cpu = new_cpu;
1195         }
1196 }
1197
1198 static void uncore_change_context(struct intel_uncore_type **uncores,
1199                                   int old_cpu, int new_cpu)
1200 {
1201         for (; *uncores; uncores++)
1202                 uncore_change_type_ctx(*uncores, old_cpu, new_cpu);
1203 }
1204
1205 static void uncore_box_unref(struct intel_uncore_type **types, int id)
1206 {
1207         struct intel_uncore_type *type;
1208         struct intel_uncore_pmu *pmu;
1209         struct intel_uncore_box *box;
1210         int i;
1211
1212         for (; *types; types++) {
1213                 type = *types;
1214                 pmu = type->pmus;
1215                 for (i = 0; i < type->num_boxes; i++, pmu++) {
1216                         box = pmu->boxes[id];
1217                         if (box && atomic_dec_return(&box->refcnt) == 0)
1218                                 uncore_box_exit(box);
1219                 }
1220         }
1221 }
1222
1223 static int uncore_event_cpu_offline(unsigned int cpu)
1224 {
1225         int die, target;
1226
1227         /* Check if exiting cpu is used for collecting uncore events */
1228         if (!cpumask_test_and_clear_cpu(cpu, &uncore_cpu_mask))
1229                 goto unref;
1230         /* Find a new cpu to collect uncore events */
1231         target = cpumask_any_but(topology_die_cpumask(cpu), cpu);
1232
1233         /* Migrate uncore events to the new target */
1234         if (target < nr_cpu_ids)
1235                 cpumask_set_cpu(target, &uncore_cpu_mask);
1236         else
1237                 target = -1;
1238
1239         uncore_change_context(uncore_msr_uncores, cpu, target);
1240         uncore_change_context(uncore_mmio_uncores, cpu, target);
1241         uncore_change_context(uncore_pci_uncores, cpu, target);
1242
1243 unref:
1244         /* Clear the references */
1245         die = topology_logical_die_id(cpu);
1246         uncore_box_unref(uncore_msr_uncores, die);
1247         uncore_box_unref(uncore_mmio_uncores, die);
1248         return 0;
1249 }
1250
1251 static int allocate_boxes(struct intel_uncore_type **types,
1252                          unsigned int die, unsigned int cpu)
1253 {
1254         struct intel_uncore_box *box, *tmp;
1255         struct intel_uncore_type *type;
1256         struct intel_uncore_pmu *pmu;
1257         LIST_HEAD(allocated);
1258         int i;
1259
1260         /* Try to allocate all required boxes */
1261         for (; *types; types++) {
1262                 type = *types;
1263                 pmu = type->pmus;
1264                 for (i = 0; i < type->num_boxes; i++, pmu++) {
1265                         if (pmu->boxes[die])
1266                                 continue;
1267                         box = uncore_alloc_box(type, cpu_to_node(cpu));
1268                         if (!box)
1269                                 goto cleanup;
1270                         box->pmu = pmu;
1271                         box->dieid = die;
1272                         list_add(&box->active_list, &allocated);
1273                 }
1274         }
1275         /* Install them in the pmus */
1276         list_for_each_entry_safe(box, tmp, &allocated, active_list) {
1277                 list_del_init(&box->active_list);
1278                 box->pmu->boxes[die] = box;
1279         }
1280         return 0;
1281
1282 cleanup:
1283         list_for_each_entry_safe(box, tmp, &allocated, active_list) {
1284                 list_del_init(&box->active_list);
1285                 kfree(box);
1286         }
1287         return -ENOMEM;
1288 }
1289
1290 static int uncore_box_ref(struct intel_uncore_type **types,
1291                           int id, unsigned int cpu)
1292 {
1293         struct intel_uncore_type *type;
1294         struct intel_uncore_pmu *pmu;
1295         struct intel_uncore_box *box;
1296         int i, ret;
1297
1298         ret = allocate_boxes(types, id, cpu);
1299         if (ret)
1300                 return ret;
1301
1302         for (; *types; types++) {
1303                 type = *types;
1304                 pmu = type->pmus;
1305                 for (i = 0; i < type->num_boxes; i++, pmu++) {
1306                         box = pmu->boxes[id];
1307                         if (box && atomic_inc_return(&box->refcnt) == 1)
1308                                 uncore_box_init(box);
1309                 }
1310         }
1311         return 0;
1312 }
1313
1314 static int uncore_event_cpu_online(unsigned int cpu)
1315 {
1316         int die, target, msr_ret, mmio_ret;
1317
1318         die = topology_logical_die_id(cpu);
1319         msr_ret = uncore_box_ref(uncore_msr_uncores, die, cpu);
1320         mmio_ret = uncore_box_ref(uncore_mmio_uncores, die, cpu);
1321         if (msr_ret && mmio_ret)
1322                 return -ENOMEM;
1323
1324         /*
1325          * Check if there is an online cpu in the package
1326          * which collects uncore events already.
1327          */
1328         target = cpumask_any_and(&uncore_cpu_mask, topology_die_cpumask(cpu));
1329         if (target < nr_cpu_ids)
1330                 return 0;
1331
1332         cpumask_set_cpu(cpu, &uncore_cpu_mask);
1333
1334         if (!msr_ret)
1335                 uncore_change_context(uncore_msr_uncores, -1, cpu);
1336         if (!mmio_ret)
1337                 uncore_change_context(uncore_mmio_uncores, -1, cpu);
1338         uncore_change_context(uncore_pci_uncores, -1, cpu);
1339         return 0;
1340 }
1341
1342 static int __init type_pmu_register(struct intel_uncore_type *type)
1343 {
1344         int i, ret;
1345
1346         for (i = 0; i < type->num_boxes; i++) {
1347                 ret = uncore_pmu_register(&type->pmus[i]);
1348                 if (ret)
1349                         return ret;
1350         }
1351         return 0;
1352 }
1353
1354 static int __init uncore_msr_pmus_register(void)
1355 {
1356         struct intel_uncore_type **types = uncore_msr_uncores;
1357         int ret;
1358
1359         for (; *types; types++) {
1360                 ret = type_pmu_register(*types);
1361                 if (ret)
1362                         return ret;
1363         }
1364         return 0;
1365 }
1366
1367 static int __init uncore_cpu_init(void)
1368 {
1369         int ret;
1370
1371         ret = uncore_types_init(uncore_msr_uncores, true);
1372         if (ret)
1373                 goto err;
1374
1375         ret = uncore_msr_pmus_register();
1376         if (ret)
1377                 goto err;
1378         return 0;
1379 err:
1380         uncore_types_exit(uncore_msr_uncores);
1381         uncore_msr_uncores = empty_uncore;
1382         return ret;
1383 }
1384
1385 static int __init uncore_mmio_init(void)
1386 {
1387         struct intel_uncore_type **types = uncore_mmio_uncores;
1388         int ret;
1389
1390         ret = uncore_types_init(types, true);
1391         if (ret)
1392                 goto err;
1393
1394         for (; *types; types++) {
1395                 ret = type_pmu_register(*types);
1396                 if (ret)
1397                         goto err;
1398         }
1399         return 0;
1400 err:
1401         uncore_types_exit(uncore_mmio_uncores);
1402         uncore_mmio_uncores = empty_uncore;
1403         return ret;
1404 }
1405
1406 struct intel_uncore_init_fun {
1407         void    (*cpu_init)(void);
1408         int     (*pci_init)(void);
1409         void    (*mmio_init)(void);
1410 };
1411
1412 static const struct intel_uncore_init_fun nhm_uncore_init __initconst = {
1413         .cpu_init = nhm_uncore_cpu_init,
1414 };
1415
1416 static const struct intel_uncore_init_fun snb_uncore_init __initconst = {
1417         .cpu_init = snb_uncore_cpu_init,
1418         .pci_init = snb_uncore_pci_init,
1419 };
1420
1421 static const struct intel_uncore_init_fun ivb_uncore_init __initconst = {
1422         .cpu_init = snb_uncore_cpu_init,
1423         .pci_init = ivb_uncore_pci_init,
1424 };
1425
1426 static const struct intel_uncore_init_fun hsw_uncore_init __initconst = {
1427         .cpu_init = snb_uncore_cpu_init,
1428         .pci_init = hsw_uncore_pci_init,
1429 };
1430
1431 static const struct intel_uncore_init_fun bdw_uncore_init __initconst = {
1432         .cpu_init = snb_uncore_cpu_init,
1433         .pci_init = bdw_uncore_pci_init,
1434 };
1435
1436 static const struct intel_uncore_init_fun snbep_uncore_init __initconst = {
1437         .cpu_init = snbep_uncore_cpu_init,
1438         .pci_init = snbep_uncore_pci_init,
1439 };
1440
1441 static const struct intel_uncore_init_fun nhmex_uncore_init __initconst = {
1442         .cpu_init = nhmex_uncore_cpu_init,
1443 };
1444
1445 static const struct intel_uncore_init_fun ivbep_uncore_init __initconst = {
1446         .cpu_init = ivbep_uncore_cpu_init,
1447         .pci_init = ivbep_uncore_pci_init,
1448 };
1449
1450 static const struct intel_uncore_init_fun hswep_uncore_init __initconst = {
1451         .cpu_init = hswep_uncore_cpu_init,
1452         .pci_init = hswep_uncore_pci_init,
1453 };
1454
1455 static const struct intel_uncore_init_fun bdx_uncore_init __initconst = {
1456         .cpu_init = bdx_uncore_cpu_init,
1457         .pci_init = bdx_uncore_pci_init,
1458 };
1459
1460 static const struct intel_uncore_init_fun knl_uncore_init __initconst = {
1461         .cpu_init = knl_uncore_cpu_init,
1462         .pci_init = knl_uncore_pci_init,
1463 };
1464
1465 static const struct intel_uncore_init_fun skl_uncore_init __initconst = {
1466         .cpu_init = skl_uncore_cpu_init,
1467         .pci_init = skl_uncore_pci_init,
1468 };
1469
1470 static const struct intel_uncore_init_fun skx_uncore_init __initconst = {
1471         .cpu_init = skx_uncore_cpu_init,
1472         .pci_init = skx_uncore_pci_init,
1473 };
1474
1475 static const struct intel_uncore_init_fun icl_uncore_init __initconst = {
1476         .cpu_init = icl_uncore_cpu_init,
1477         .pci_init = skl_uncore_pci_init,
1478 };
1479
1480 static const struct intel_uncore_init_fun tgl_uncore_init __initconst = {
1481         .cpu_init = icl_uncore_cpu_init,
1482         .mmio_init = tgl_uncore_mmio_init,
1483 };
1484
1485 static const struct intel_uncore_init_fun tgl_l_uncore_init __initconst = {
1486         .cpu_init = icl_uncore_cpu_init,
1487         .mmio_init = tgl_l_uncore_mmio_init,
1488 };
1489
1490 static const struct intel_uncore_init_fun icx_uncore_init __initconst = {
1491         .cpu_init = icx_uncore_cpu_init,
1492         .pci_init = icx_uncore_pci_init,
1493         .mmio_init = icx_uncore_mmio_init,
1494 };
1495
1496 static const struct intel_uncore_init_fun snr_uncore_init __initconst = {
1497         .cpu_init = snr_uncore_cpu_init,
1498         .pci_init = snr_uncore_pci_init,
1499         .mmio_init = snr_uncore_mmio_init,
1500 };
1501
1502 static const struct x86_cpu_id intel_uncore_match[] __initconst = {
1503         X86_MATCH_INTEL_FAM6_MODEL(NEHALEM_EP,          &nhm_uncore_init),
1504         X86_MATCH_INTEL_FAM6_MODEL(NEHALEM,             &nhm_uncore_init),
1505         X86_MATCH_INTEL_FAM6_MODEL(WESTMERE,            &nhm_uncore_init),
1506         X86_MATCH_INTEL_FAM6_MODEL(WESTMERE_EP,         &nhm_uncore_init),
1507         X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE,         &snb_uncore_init),
1508         X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE,           &ivb_uncore_init),
1509         X86_MATCH_INTEL_FAM6_MODEL(HASWELL,             &hsw_uncore_init),
1510         X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L,           &hsw_uncore_init),
1511         X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G,           &hsw_uncore_init),
1512         X86_MATCH_INTEL_FAM6_MODEL(BROADWELL,           &bdw_uncore_init),
1513         X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G,         &bdw_uncore_init),
1514         X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X,       &snbep_uncore_init),
1515         X86_MATCH_INTEL_FAM6_MODEL(NEHALEM_EX,          &nhmex_uncore_init),
1516         X86_MATCH_INTEL_FAM6_MODEL(WESTMERE_EX,         &nhmex_uncore_init),
1517         X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X,         &ivbep_uncore_init),
1518         X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X,           &hswep_uncore_init),
1519         X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X,         &bdx_uncore_init),
1520         X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D,         &bdx_uncore_init),
1521         X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL,        &knl_uncore_init),
1522         X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM,        &knl_uncore_init),
1523         X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE,             &skl_uncore_init),
1524         X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L,           &skl_uncore_init),
1525         X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X,           &skx_uncore_init),
1526         X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L,          &skl_uncore_init),
1527         X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE,            &skl_uncore_init),
1528         X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE_L,         &skl_uncore_init),
1529         X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE,           &skl_uncore_init),
1530         X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L,           &icl_uncore_init),
1531         X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_NNPI,        &icl_uncore_init),
1532         X86_MATCH_INTEL_FAM6_MODEL(ICELAKE,             &icl_uncore_init),
1533         X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D,           &icx_uncore_init),
1534         X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X,           &icx_uncore_init),
1535         X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE_L,         &tgl_l_uncore_init),
1536         X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE,           &tgl_uncore_init),
1537         X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D,      &snr_uncore_init),
1538         {},
1539 };
1540 MODULE_DEVICE_TABLE(x86cpu, intel_uncore_match);
1541
1542 static int __init intel_uncore_init(void)
1543 {
1544         const struct x86_cpu_id *id;
1545         struct intel_uncore_init_fun *uncore_init;
1546         int pret = 0, cret = 0, mret = 0, ret;
1547
1548         id = x86_match_cpu(intel_uncore_match);
1549         if (!id)
1550                 return -ENODEV;
1551
1552         if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
1553                 return -ENODEV;
1554
1555         __uncore_max_dies =
1556                 topology_max_packages() * topology_max_die_per_package();
1557
1558         uncore_init = (struct intel_uncore_init_fun *)id->driver_data;
1559         if (uncore_init->pci_init) {
1560                 pret = uncore_init->pci_init();
1561                 if (!pret)
1562                         pret = uncore_pci_init();
1563         }
1564
1565         if (uncore_init->cpu_init) {
1566                 uncore_init->cpu_init();
1567                 cret = uncore_cpu_init();
1568         }
1569
1570         if (uncore_init->mmio_init) {
1571                 uncore_init->mmio_init();
1572                 mret = uncore_mmio_init();
1573         }
1574
1575         if (cret && pret && mret)
1576                 return -ENODEV;
1577
1578         /* Install hotplug callbacks to setup the targets for each package */
1579         ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_UNCORE_ONLINE,
1580                                 "perf/x86/intel/uncore:online",
1581                                 uncore_event_cpu_online,
1582                                 uncore_event_cpu_offline);
1583         if (ret)
1584                 goto err;
1585         return 0;
1586
1587 err:
1588         uncore_types_exit(uncore_msr_uncores);
1589         uncore_types_exit(uncore_mmio_uncores);
1590         uncore_pci_exit();
1591         return ret;
1592 }
1593 module_init(intel_uncore_init);
1594
1595 static void __exit intel_uncore_exit(void)
1596 {
1597         cpuhp_remove_state(CPUHP_AP_PERF_X86_UNCORE_ONLINE);
1598         uncore_types_exit(uncore_msr_uncores);
1599         uncore_types_exit(uncore_mmio_uncores);
1600         uncore_pci_exit();
1601 }
1602 module_exit(intel_uncore_exit);