xref: /linux/arch/x86/events/intel/uncore.c (revision 1a308a168c9286a335a05926204ef3ebb68fba1c)
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 (!pmu->registered)
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 cpumap_print_to_pagebuf(true, buf, &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 		pmu->registered = true;
957 	return ret;
958 }
959 
960 static void uncore_pmu_unregister(struct intel_uncore_pmu *pmu)
961 {
962 	if (!pmu->registered)
963 		return;
964 	perf_pmu_unregister(&pmu->pmu);
965 	pmu->registered = false;
966 }
967 
968 static void uncore_free_boxes(struct intel_uncore_pmu *pmu)
969 {
970 	int die;
971 
972 	for (die = 0; die < uncore_max_dies(); die++)
973 		kfree(pmu->boxes[die]);
974 	kfree(pmu->boxes);
975 }
976 
977 static void uncore_type_exit(struct intel_uncore_type *type)
978 {
979 	struct intel_uncore_pmu *pmu = type->pmus;
980 	int i;
981 
982 	if (type->cleanup_mapping)
983 		type->cleanup_mapping(type);
984 
985 	if (type->cleanup_extra_boxes)
986 		type->cleanup_extra_boxes(type);
987 
988 	if (pmu) {
989 		for (i = 0; i < type->num_boxes; i++, pmu++) {
990 			uncore_pmu_unregister(pmu);
991 			uncore_free_boxes(pmu);
992 		}
993 		kfree(type->pmus);
994 		type->pmus = NULL;
995 	}
996 
997 	kfree(type->events_group);
998 	type->events_group = NULL;
999 }
1000 
1001 static void uncore_types_exit(struct intel_uncore_type **types)
1002 {
1003 	for (; *types; types++)
1004 		uncore_type_exit(*types);
1005 }
1006 
1007 static int __init uncore_type_init(struct intel_uncore_type *type)
1008 {
1009 	struct intel_uncore_pmu *pmus;
1010 	size_t size;
1011 	int i, j;
1012 
1013 	pmus = kzalloc_objs(*pmus, type->num_boxes);
1014 	if (!pmus)
1015 		return -ENOMEM;
1016 
1017 	size = uncore_max_dies() * sizeof(struct intel_uncore_box *);
1018 
1019 	for (i = 0; i < type->num_boxes; i++) {
1020 		pmus[i].pmu_idx	= i;
1021 		pmus[i].type	= type;
1022 		pmus[i].boxes	= kzalloc(size, GFP_KERNEL);
1023 		if (!pmus[i].boxes)
1024 			goto err;
1025 	}
1026 
1027 	type->pmus = pmus;
1028 	type->unconstrainted = (struct event_constraint)
1029 		__EVENT_CONSTRAINT(0, (1ULL << type->num_counters) - 1,
1030 				0, type->num_counters, 0, 0);
1031 
1032 	if (type->event_descs) {
1033 		struct {
1034 			struct attribute_group group;
1035 			struct attribute *attrs[];
1036 		} *attr_group;
1037 		for (i = 0; type->event_descs[i].attr.attr.name; i++);
1038 
1039 		attr_group = kzalloc_flex(*attr_group, attrs, i + 1);
1040 		if (!attr_group)
1041 			goto err;
1042 
1043 		attr_group->group.name = "events";
1044 		attr_group->group.attrs = attr_group->attrs;
1045 
1046 		for (j = 0; j < i; j++)
1047 			attr_group->attrs[j] = &type->event_descs[j].attr.attr;
1048 
1049 		type->events_group = &attr_group->group;
1050 	}
1051 
1052 	type->pmu_group = &uncore_pmu_attr_group;
1053 
1054 	if (type->set_mapping)
1055 		type->set_mapping(type);
1056 
1057 	return 0;
1058 
1059 err:
1060 	for (i = 0; i < type->num_boxes; i++)
1061 		kfree(pmus[i].boxes);
1062 	kfree(pmus);
1063 
1064 	return -ENOMEM;
1065 }
1066 
1067 static int __init
1068 uncore_types_init(struct intel_uncore_type **types)
1069 {
1070 	int ret;
1071 
1072 	for (; *types; types++) {
1073 		ret = uncore_type_init(*types);
1074 		if (ret)
1075 			return ret;
1076 	}
1077 	return 0;
1078 }
1079 
1080 /*
1081  * Get the die information of a PCI device.
1082  * @pdev: The PCI device.
1083  * @die: The die id which the device maps to.
1084  */
1085 static int uncore_pci_get_dev_die_info(struct pci_dev *pdev, int *die)
1086 {
1087 	*die = uncore_pcibus_to_dieid(pdev->bus);
1088 	if (*die < 0)
1089 		return -EINVAL;
1090 
1091 	return 0;
1092 }
1093 
1094 static struct intel_uncore_pmu *
1095 uncore_pci_find_dev_pmu_from_types(struct pci_dev *pdev)
1096 {
1097 	struct intel_uncore_type **types = uncore_pci_uncores;
1098 	struct intel_uncore_discovery_unit *unit;
1099 	struct intel_uncore_type *type;
1100 	struct rb_node *node;
1101 
1102 	for (; *types; types++) {
1103 		type = *types;
1104 
1105 		for (node = rb_first(type->boxes); node; node = rb_next(node)) {
1106 			unit = rb_entry(node, struct intel_uncore_discovery_unit, node);
1107 			if (pdev->devfn == UNCORE_DISCOVERY_PCI_DEVFN(unit->addr) &&
1108 			    pdev->bus->number == UNCORE_DISCOVERY_PCI_BUS(unit->addr) &&
1109 			    pci_domain_nr(pdev->bus) == UNCORE_DISCOVERY_PCI_DOMAIN(unit->addr))
1110 				return &type->pmus[unit->pmu_idx];
1111 		}
1112 	}
1113 
1114 	return NULL;
1115 }
1116 
1117 /*
1118  * Find the PMU of a PCI device.
1119  * @pdev: The PCI device.
1120  * @ids: The ID table of the available PCI devices with a PMU.
1121  *       If NULL, search the whole uncore_pci_uncores.
1122  */
1123 static struct intel_uncore_pmu *
1124 uncore_pci_find_dev_pmu(struct pci_dev *pdev, const struct pci_device_id *ids)
1125 {
1126 	struct intel_uncore_pmu *pmu = NULL;
1127 	struct intel_uncore_type *type;
1128 	kernel_ulong_t data;
1129 	unsigned int devfn;
1130 
1131 	if (!ids)
1132 		return uncore_pci_find_dev_pmu_from_types(pdev);
1133 
1134 	while (ids && ids->vendor) {
1135 		if ((ids->vendor == pdev->vendor) &&
1136 		    (ids->device == pdev->device)) {
1137 			data = ids->driver_data;
1138 			devfn = PCI_DEVFN(UNCORE_PCI_DEV_DEV(data),
1139 					  UNCORE_PCI_DEV_FUNC(data));
1140 			if (devfn == pdev->devfn) {
1141 				type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(data)];
1142 				pmu = &type->pmus[UNCORE_PCI_DEV_IDX(data)];
1143 				break;
1144 			}
1145 		}
1146 		ids++;
1147 	}
1148 	return pmu;
1149 }
1150 
1151 /*
1152  * Register the PMU for a PCI device
1153  * @pdev: The PCI device.
1154  * @type: The corresponding PMU type of the device.
1155  * @pmu: The corresponding PMU of the device.
1156  * @die: The die id which the device maps to.
1157  */
1158 static int uncore_pci_pmu_register(struct pci_dev *pdev,
1159 				   struct intel_uncore_type *type,
1160 				   struct intel_uncore_pmu *pmu,
1161 				   int die)
1162 {
1163 	struct intel_uncore_box *box;
1164 	int ret;
1165 
1166 	if (WARN_ON_ONCE(pmu->boxes[die] != NULL))
1167 		return -EINVAL;
1168 
1169 	box = uncore_alloc_box(type, NUMA_NO_NODE);
1170 	if (!box)
1171 		return -ENOMEM;
1172 
1173 	atomic_inc(&box->refcnt);
1174 	box->dieid = die;
1175 	box->pci_dev = pdev;
1176 	box->pmu = pmu;
1177 	uncore_box_init(box);
1178 
1179 	pmu->boxes[die] = box;
1180 	if (atomic_inc_return(&pmu->activeboxes) > 1)
1181 		return 0;
1182 
1183 	/* First active box registers the pmu */
1184 	ret = uncore_pmu_register(pmu);
1185 	if (ret) {
1186 		pmu->boxes[die] = NULL;
1187 		uncore_box_exit(box);
1188 		kfree(box);
1189 	}
1190 	return ret;
1191 }
1192 
1193 /*
1194  * add a pci uncore device
1195  */
1196 static int uncore_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1197 {
1198 	struct intel_uncore_type *type;
1199 	struct intel_uncore_pmu *pmu = NULL;
1200 	int die, ret;
1201 
1202 	ret = uncore_pci_get_dev_die_info(pdev, &die);
1203 	if (ret)
1204 		return ret;
1205 
1206 	if (UNCORE_PCI_DEV_TYPE(id->driver_data) == UNCORE_EXTRA_PCI_DEV) {
1207 		int idx = UNCORE_PCI_DEV_IDX(id->driver_data);
1208 
1209 		uncore_extra_pci_dev[die].dev[idx] = pdev;
1210 		pci_set_drvdata(pdev, NULL);
1211 		return 0;
1212 	}
1213 
1214 	type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(id->driver_data)];
1215 
1216 	/*
1217 	 * Some platforms, e.g.  Knights Landing, use a common PCI device ID
1218 	 * for multiple instances of an uncore PMU device type. We should check
1219 	 * PCI slot and func to indicate the uncore box.
1220 	 */
1221 	if (id->driver_data & ~0xffff) {
1222 		struct pci_driver *pci_drv = to_pci_driver(pdev->dev.driver);
1223 
1224 		pmu = uncore_pci_find_dev_pmu(pdev, pci_drv->id_table);
1225 		if (pmu == NULL)
1226 			return -ENODEV;
1227 	} else {
1228 		/*
1229 		 * for performance monitoring unit with multiple boxes,
1230 		 * each box has a different function id.
1231 		 */
1232 		pmu = &type->pmus[UNCORE_PCI_DEV_IDX(id->driver_data)];
1233 	}
1234 
1235 	ret = uncore_pci_pmu_register(pdev, type, pmu, die);
1236 
1237 	pci_set_drvdata(pdev, pmu->boxes[die]);
1238 
1239 	return ret;
1240 }
1241 
1242 /*
1243  * Unregister the PMU of a PCI device
1244  * @pmu: The corresponding PMU is unregistered.
1245  * @die: The die id which the device maps to.
1246  */
1247 static void uncore_pci_pmu_unregister(struct intel_uncore_pmu *pmu, int die)
1248 {
1249 	struct intel_uncore_box *box = pmu->boxes[die];
1250 
1251 	pmu->boxes[die] = NULL;
1252 	if (atomic_dec_return(&pmu->activeboxes) == 0)
1253 		uncore_pmu_unregister(pmu);
1254 	uncore_box_exit(box);
1255 	kfree(box);
1256 }
1257 
1258 static void uncore_pci_remove(struct pci_dev *pdev)
1259 {
1260 	struct intel_uncore_box *box;
1261 	struct intel_uncore_pmu *pmu;
1262 	int i, die;
1263 
1264 	if (uncore_pci_get_dev_die_info(pdev, &die))
1265 		return;
1266 
1267 	box = pci_get_drvdata(pdev);
1268 	if (!box) {
1269 		for (i = 0; i < UNCORE_EXTRA_PCI_DEV_MAX; i++) {
1270 			if (uncore_extra_pci_dev[die].dev[i] == pdev) {
1271 				uncore_extra_pci_dev[die].dev[i] = NULL;
1272 				break;
1273 			}
1274 		}
1275 		WARN_ON_ONCE(i >= UNCORE_EXTRA_PCI_DEV_MAX);
1276 		return;
1277 	}
1278 
1279 	pmu = box->pmu;
1280 
1281 	pci_set_drvdata(pdev, NULL);
1282 
1283 	uncore_pci_pmu_unregister(pmu, die);
1284 }
1285 
1286 static int uncore_bus_notify(struct notifier_block *nb,
1287 			     unsigned long action, void *data,
1288 			     const struct pci_device_id *ids)
1289 {
1290 	struct device *dev = data;
1291 	struct pci_dev *pdev = to_pci_dev(dev);
1292 	struct intel_uncore_pmu *pmu;
1293 	int die;
1294 
1295 	/* Unregister the PMU when the device is going to be deleted. */
1296 	if (action != BUS_NOTIFY_DEL_DEVICE)
1297 		return NOTIFY_DONE;
1298 
1299 	pmu = uncore_pci_find_dev_pmu(pdev, ids);
1300 	if (!pmu)
1301 		return NOTIFY_DONE;
1302 
1303 	if (uncore_pci_get_dev_die_info(pdev, &die))
1304 		return NOTIFY_DONE;
1305 
1306 	uncore_pci_pmu_unregister(pmu, die);
1307 
1308 	return NOTIFY_OK;
1309 }
1310 
1311 static int uncore_pci_sub_bus_notify(struct notifier_block *nb,
1312 				     unsigned long action, void *data)
1313 {
1314 	return uncore_bus_notify(nb, action, data,
1315 				 uncore_pci_sub_driver->id_table);
1316 }
1317 
1318 static struct notifier_block uncore_pci_sub_notifier = {
1319 	.notifier_call = uncore_pci_sub_bus_notify,
1320 };
1321 
1322 static void uncore_pci_sub_driver_init(void)
1323 {
1324 	const struct pci_device_id *ids = uncore_pci_sub_driver->id_table;
1325 	struct intel_uncore_type *type;
1326 	struct intel_uncore_pmu *pmu;
1327 	struct pci_dev *pci_sub_dev;
1328 	bool notify = false;
1329 	unsigned int devfn;
1330 	int die;
1331 
1332 	while (ids && ids->vendor) {
1333 		pci_sub_dev = NULL;
1334 		type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(ids->driver_data)];
1335 		/*
1336 		 * Search the available device, and register the
1337 		 * corresponding PMU.
1338 		 */
1339 		while ((pci_sub_dev = pci_get_device(PCI_VENDOR_ID_INTEL,
1340 						     ids->device, pci_sub_dev))) {
1341 			devfn = PCI_DEVFN(UNCORE_PCI_DEV_DEV(ids->driver_data),
1342 					  UNCORE_PCI_DEV_FUNC(ids->driver_data));
1343 			if (devfn != pci_sub_dev->devfn)
1344 				continue;
1345 
1346 			pmu = &type->pmus[UNCORE_PCI_DEV_IDX(ids->driver_data)];
1347 
1348 			if (uncore_pci_get_dev_die_info(pci_sub_dev, &die))
1349 				continue;
1350 
1351 			if (!uncore_pci_pmu_register(pci_sub_dev, type, pmu,
1352 						     die))
1353 				notify = true;
1354 		}
1355 		ids++;
1356 	}
1357 
1358 	if (notify && bus_register_notifier(&pci_bus_type, &uncore_pci_sub_notifier))
1359 		notify = false;
1360 
1361 	if (!notify)
1362 		uncore_pci_sub_driver = NULL;
1363 }
1364 
1365 static int uncore_pci_bus_notify(struct notifier_block *nb,
1366 				     unsigned long action, void *data)
1367 {
1368 	return uncore_bus_notify(nb, action, data, NULL);
1369 }
1370 
1371 static struct notifier_block uncore_pci_notifier = {
1372 	.notifier_call = uncore_pci_bus_notify,
1373 };
1374 
1375 
1376 static void uncore_pci_pmus_register(void)
1377 {
1378 	struct intel_uncore_type **types = uncore_pci_uncores;
1379 	struct intel_uncore_discovery_unit *unit;
1380 	struct intel_uncore_type *type;
1381 	struct intel_uncore_pmu *pmu;
1382 	struct rb_node *node;
1383 	struct pci_dev *pdev;
1384 
1385 	for (; *types; types++) {
1386 		type = *types;
1387 
1388 		for (node = rb_first(type->boxes); node; node = rb_next(node)) {
1389 			unit = rb_entry(node, struct intel_uncore_discovery_unit, node);
1390 			pdev = pci_get_domain_bus_and_slot(UNCORE_DISCOVERY_PCI_DOMAIN(unit->addr),
1391 							   UNCORE_DISCOVERY_PCI_BUS(unit->addr),
1392 							   UNCORE_DISCOVERY_PCI_DEVFN(unit->addr));
1393 
1394 			if (!pdev)
1395 				continue;
1396 			pmu = &type->pmus[unit->pmu_idx];
1397 			uncore_pci_pmu_register(pdev, type, pmu, unit->die);
1398 		}
1399 	}
1400 
1401 	bus_register_notifier(&pci_bus_type, &uncore_pci_notifier);
1402 }
1403 
1404 static int __init uncore_pci_init(void)
1405 {
1406 	size_t size;
1407 	int ret;
1408 
1409 	size = uncore_max_dies() * sizeof(struct pci_extra_dev);
1410 	uncore_extra_pci_dev = kzalloc(size, GFP_KERNEL);
1411 	if (!uncore_extra_pci_dev) {
1412 		ret = -ENOMEM;
1413 		goto err;
1414 	}
1415 
1416 	ret = uncore_types_init(uncore_pci_uncores);
1417 	if (ret)
1418 		goto errtype;
1419 
1420 	if (uncore_pci_driver) {
1421 		uncore_pci_driver->probe = uncore_pci_probe;
1422 		uncore_pci_driver->remove = uncore_pci_remove;
1423 
1424 		ret = pci_register_driver(uncore_pci_driver);
1425 		if (ret)
1426 			goto errtype;
1427 	} else
1428 		uncore_pci_pmus_register();
1429 
1430 	if (uncore_pci_sub_driver)
1431 		uncore_pci_sub_driver_init();
1432 
1433 	pcidrv_registered = true;
1434 	return 0;
1435 
1436 errtype:
1437 	uncore_types_exit(uncore_pci_uncores);
1438 	kfree(uncore_extra_pci_dev);
1439 	uncore_extra_pci_dev = NULL;
1440 	uncore_free_pcibus_map();
1441 err:
1442 	uncore_pci_uncores = empty_uncore;
1443 	return ret;
1444 }
1445 
1446 static void uncore_pci_exit(void)
1447 {
1448 	if (pcidrv_registered) {
1449 		pcidrv_registered = false;
1450 		if (uncore_pci_sub_driver)
1451 			bus_unregister_notifier(&pci_bus_type, &uncore_pci_sub_notifier);
1452 		if (uncore_pci_driver)
1453 			pci_unregister_driver(uncore_pci_driver);
1454 		else
1455 			bus_unregister_notifier(&pci_bus_type, &uncore_pci_notifier);
1456 		uncore_types_exit(uncore_pci_uncores);
1457 		kfree(uncore_extra_pci_dev);
1458 		uncore_free_pcibus_map();
1459 	}
1460 }
1461 
1462 static bool uncore_die_has_box(struct intel_uncore_type *type,
1463 			       int die, unsigned int pmu_idx)
1464 {
1465 	if (!type->boxes)
1466 		return true;
1467 
1468 	if (intel_uncore_find_discovery_unit_id(type->boxes, die, pmu_idx) < 0)
1469 		return false;
1470 
1471 	return true;
1472 }
1473 
1474 static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,
1475 				   int new_cpu)
1476 {
1477 	struct intel_uncore_pmu *pmu = type->pmus;
1478 	struct intel_uncore_box *box;
1479 	int i, die;
1480 
1481 	die = topology_logical_die_id(old_cpu < 0 ? new_cpu : old_cpu);
1482 	for (i = 0; i < type->num_boxes; i++, pmu++) {
1483 		box = pmu->boxes[die];
1484 		if (!box)
1485 			continue;
1486 
1487 		if (old_cpu < 0) {
1488 			WARN_ON_ONCE(box->cpu != -1);
1489 			if (uncore_die_has_box(type, die, pmu->pmu_idx)) {
1490 				box->cpu = new_cpu;
1491 				cpumask_set_cpu(new_cpu, &pmu->cpu_mask);
1492 			}
1493 			continue;
1494 		}
1495 
1496 		WARN_ON_ONCE(box->cpu != -1 && box->cpu != old_cpu);
1497 		box->cpu = -1;
1498 		cpumask_clear_cpu(old_cpu, &pmu->cpu_mask);
1499 		if (new_cpu < 0)
1500 			continue;
1501 
1502 		if (!uncore_die_has_box(type, die, pmu->pmu_idx))
1503 			continue;
1504 		uncore_pmu_cancel_hrtimer(box);
1505 		perf_pmu_migrate_context(&pmu->pmu, old_cpu, new_cpu);
1506 		box->cpu = new_cpu;
1507 		cpumask_set_cpu(new_cpu, &pmu->cpu_mask);
1508 	}
1509 }
1510 
1511 static void uncore_change_context(struct intel_uncore_type **uncores,
1512 				  int old_cpu, int new_cpu)
1513 {
1514 	for (; *uncores; uncores++)
1515 		uncore_change_type_ctx(*uncores, old_cpu, new_cpu);
1516 }
1517 
1518 static void uncore_box_unref(struct intel_uncore_type **types, int id)
1519 {
1520 	struct intel_uncore_type *type;
1521 	struct intel_uncore_pmu *pmu;
1522 	struct intel_uncore_box *box;
1523 	int i;
1524 
1525 	for (; *types; types++) {
1526 		type = *types;
1527 		pmu = type->pmus;
1528 		for (i = 0; i < type->num_boxes; i++, pmu++) {
1529 			box = pmu->boxes[id];
1530 			if (box && box->cpu >= 0 && atomic_dec_return(&box->refcnt) == 0)
1531 				uncore_box_exit(box);
1532 		}
1533 	}
1534 }
1535 
1536 static int uncore_event_cpu_offline(unsigned int cpu)
1537 {
1538 	int die, target;
1539 
1540 	/* Check if exiting cpu is used for collecting uncore events */
1541 	if (!cpumask_test_and_clear_cpu(cpu, &uncore_cpu_mask))
1542 		goto unref;
1543 	/* Find a new cpu to collect uncore events */
1544 	target = cpumask_any_but(topology_die_cpumask(cpu), cpu);
1545 
1546 	/* Migrate uncore events to the new target */
1547 	if (target < nr_cpu_ids)
1548 		cpumask_set_cpu(target, &uncore_cpu_mask);
1549 	else
1550 		target = -1;
1551 
1552 	uncore_change_context(uncore_msr_uncores, cpu, target);
1553 	uncore_change_context(uncore_mmio_uncores, cpu, target);
1554 	uncore_change_context(uncore_pci_uncores, cpu, target);
1555 
1556 unref:
1557 	/* Clear the references */
1558 	die = topology_logical_die_id(cpu);
1559 	uncore_box_unref(uncore_msr_uncores, die);
1560 	uncore_box_unref(uncore_mmio_uncores, die);
1561 	return 0;
1562 }
1563 
1564 static int allocate_boxes(struct intel_uncore_type **types,
1565 			 unsigned int die, unsigned int cpu)
1566 {
1567 	struct intel_uncore_box *box, *tmp;
1568 	struct intel_uncore_type *type;
1569 	struct intel_uncore_pmu *pmu;
1570 	LIST_HEAD(allocated);
1571 	int i;
1572 
1573 	/* Try to allocate all required boxes */
1574 	for (; *types; types++) {
1575 		type = *types;
1576 		pmu = type->pmus;
1577 		for (i = 0; i < type->num_boxes; i++, pmu++) {
1578 			if (pmu->boxes[die])
1579 				continue;
1580 			box = uncore_alloc_box(type, cpu_to_node(cpu));
1581 			if (!box)
1582 				goto cleanup;
1583 			box->pmu = pmu;
1584 			box->dieid = die;
1585 			list_add(&box->active_list, &allocated);
1586 		}
1587 	}
1588 	/* Install them in the pmus */
1589 	list_for_each_entry_safe(box, tmp, &allocated, active_list) {
1590 		list_del_init(&box->active_list);
1591 		box->pmu->boxes[die] = box;
1592 	}
1593 	return 0;
1594 
1595 cleanup:
1596 	list_for_each_entry_safe(box, tmp, &allocated, active_list) {
1597 		list_del_init(&box->active_list);
1598 		kfree(box);
1599 	}
1600 	return -ENOMEM;
1601 }
1602 
1603 static int uncore_box_ref(struct intel_uncore_type **types,
1604 			  int id, unsigned int cpu)
1605 {
1606 	struct intel_uncore_type *type;
1607 	struct intel_uncore_pmu *pmu;
1608 	struct intel_uncore_box *box;
1609 	int i, ret;
1610 
1611 	ret = allocate_boxes(types, id, cpu);
1612 	if (ret)
1613 		return ret;
1614 
1615 	for (; *types; types++) {
1616 		type = *types;
1617 		pmu = type->pmus;
1618 		for (i = 0; i < type->num_boxes; i++, pmu++) {
1619 			box = pmu->boxes[id];
1620 			if (box && box->cpu >= 0 && atomic_inc_return(&box->refcnt) == 1)
1621 				uncore_box_init(box);
1622 		}
1623 	}
1624 	return 0;
1625 }
1626 
1627 static int uncore_event_cpu_online(unsigned int cpu)
1628 {
1629 	int die, target, msr_ret, mmio_ret;
1630 
1631 	die = topology_logical_die_id(cpu);
1632 	msr_ret = uncore_box_ref(uncore_msr_uncores, die, cpu);
1633 	mmio_ret = uncore_box_ref(uncore_mmio_uncores, die, cpu);
1634 	if (msr_ret && mmio_ret)
1635 		return -ENOMEM;
1636 
1637 	/*
1638 	 * Check if there is an online cpu in the package
1639 	 * which collects uncore events already.
1640 	 */
1641 	target = cpumask_any_and(&uncore_cpu_mask, topology_die_cpumask(cpu));
1642 	if (target < nr_cpu_ids)
1643 		return 0;
1644 
1645 	cpumask_set_cpu(cpu, &uncore_cpu_mask);
1646 
1647 	if (!msr_ret)
1648 		uncore_change_context(uncore_msr_uncores, -1, cpu);
1649 	if (!mmio_ret)
1650 		uncore_change_context(uncore_mmio_uncores, -1, cpu);
1651 	uncore_change_context(uncore_pci_uncores, -1, cpu);
1652 	return 0;
1653 }
1654 
1655 static int __init type_pmu_register(struct intel_uncore_type *type)
1656 {
1657 	int i, ret;
1658 
1659 	for (i = 0; i < type->num_boxes; i++) {
1660 		ret = uncore_pmu_register(&type->pmus[i]);
1661 		if (ret)
1662 			return ret;
1663 	}
1664 	return 0;
1665 }
1666 
1667 static int __init uncore_msr_pmus_register(void)
1668 {
1669 	struct intel_uncore_type **types = uncore_msr_uncores;
1670 	int ret;
1671 
1672 	for (; *types; types++) {
1673 		ret = type_pmu_register(*types);
1674 		if (ret)
1675 			return ret;
1676 	}
1677 	return 0;
1678 }
1679 
1680 static int __init uncore_cpu_init(void)
1681 {
1682 	int ret;
1683 
1684 	ret = uncore_types_init(uncore_msr_uncores);
1685 	if (ret)
1686 		goto err;
1687 
1688 	ret = uncore_msr_pmus_register();
1689 	if (ret)
1690 		goto err;
1691 	return 0;
1692 err:
1693 	uncore_types_exit(uncore_msr_uncores);
1694 	uncore_msr_uncores = empty_uncore;
1695 	return ret;
1696 }
1697 
1698 static int __init uncore_mmio_init(void)
1699 {
1700 	struct intel_uncore_type **types = uncore_mmio_uncores;
1701 	int ret;
1702 
1703 	ret = uncore_types_init(types);
1704 	if (ret)
1705 		goto err;
1706 
1707 	for (; *types; types++) {
1708 		ret = type_pmu_register(*types);
1709 		if (ret)
1710 			goto err;
1711 	}
1712 	return 0;
1713 err:
1714 	uncore_types_exit(uncore_mmio_uncores);
1715 	uncore_mmio_uncores = empty_uncore;
1716 	return ret;
1717 }
1718 
1719 static int uncore_mmio_global_init(int die, u64 ctl)
1720 {
1721 	void __iomem *io_addr;
1722 
1723 	io_addr = ioremap(ctl, sizeof(ctl));
1724 	if (!io_addr)
1725 		return -ENOMEM;
1726 
1727 	/* Clear freeze bit (0) to enable all counters. */
1728 	writel(0, io_addr);
1729 
1730 	iounmap(io_addr);
1731 	return 0;
1732 }
1733 
1734 static int uncore_msr_global_init(int die, u64 msr)
1735 {
1736 	int cpu = uncore_die_to_cpu(die);
1737 
1738 	if (cpu == -1)
1739 		return -ENODEV;
1740 
1741 	return wrmsrq_on_cpu(cpu, msr, 0);
1742 }
1743 
1744 static const struct uncore_plat_init nhm_uncore_init __initconst = {
1745 	.cpu_init = nhm_uncore_cpu_init,
1746 };
1747 
1748 static const struct uncore_plat_init snb_uncore_init __initconst = {
1749 	.cpu_init = snb_uncore_cpu_init,
1750 	.pci_init = snb_uncore_pci_init,
1751 };
1752 
1753 static const struct uncore_plat_init ivb_uncore_init __initconst = {
1754 	.cpu_init = snb_uncore_cpu_init,
1755 	.pci_init = ivb_uncore_pci_init,
1756 };
1757 
1758 static const struct uncore_plat_init hsw_uncore_init __initconst = {
1759 	.cpu_init = snb_uncore_cpu_init,
1760 	.pci_init = hsw_uncore_pci_init,
1761 };
1762 
1763 static const struct uncore_plat_init bdw_uncore_init __initconst = {
1764 	.cpu_init = snb_uncore_cpu_init,
1765 	.pci_init = bdw_uncore_pci_init,
1766 };
1767 
1768 static const struct uncore_plat_init snbep_uncore_init __initconst = {
1769 	.cpu_init = snbep_uncore_cpu_init,
1770 	.pci_init = snbep_uncore_pci_init,
1771 };
1772 
1773 static const struct uncore_plat_init nhmex_uncore_init __initconst = {
1774 	.cpu_init = nhmex_uncore_cpu_init,
1775 };
1776 
1777 static const struct uncore_plat_init ivbep_uncore_init __initconst = {
1778 	.cpu_init = ivbep_uncore_cpu_init,
1779 	.pci_init = ivbep_uncore_pci_init,
1780 };
1781 
1782 static const struct uncore_plat_init hswep_uncore_init __initconst = {
1783 	.cpu_init = hswep_uncore_cpu_init,
1784 	.pci_init = hswep_uncore_pci_init,
1785 };
1786 
1787 static const struct uncore_plat_init bdx_uncore_init __initconst = {
1788 	.cpu_init = bdx_uncore_cpu_init,
1789 	.pci_init = bdx_uncore_pci_init,
1790 };
1791 
1792 static const struct uncore_plat_init knl_uncore_init __initconst = {
1793 	.cpu_init = knl_uncore_cpu_init,
1794 	.pci_init = knl_uncore_pci_init,
1795 };
1796 
1797 static const struct uncore_plat_init skl_uncore_init __initconst = {
1798 	.cpu_init = skl_uncore_cpu_init,
1799 	.pci_init = skl_uncore_pci_init,
1800 };
1801 
1802 static const struct uncore_plat_init skx_uncore_init __initconst = {
1803 	.cpu_init = skx_uncore_cpu_init,
1804 	.pci_init = skx_uncore_pci_init,
1805 };
1806 
1807 static const struct uncore_plat_init icl_uncore_init __initconst = {
1808 	.cpu_init = icl_uncore_cpu_init,
1809 	.pci_init = skl_uncore_pci_init,
1810 };
1811 
1812 static const struct uncore_plat_init tgl_uncore_init __initconst = {
1813 	.cpu_init = tgl_uncore_cpu_init,
1814 	.mmio_init = tgl_uncore_mmio_init,
1815 };
1816 
1817 static const struct uncore_plat_init tgl_l_uncore_init __initconst = {
1818 	.cpu_init = tgl_uncore_cpu_init,
1819 	.mmio_init = tgl_l_uncore_mmio_init,
1820 };
1821 
1822 static const struct uncore_plat_init rkl_uncore_init __initconst = {
1823 	.cpu_init = tgl_uncore_cpu_init,
1824 	.pci_init = skl_uncore_pci_init,
1825 };
1826 
1827 static const struct uncore_plat_init adl_uncore_init __initconst = {
1828 	.cpu_init = adl_uncore_cpu_init,
1829 	.mmio_init = adl_uncore_mmio_init,
1830 };
1831 
1832 static const struct uncore_plat_init mtl_uncore_init __initconst = {
1833 	.cpu_init = mtl_uncore_cpu_init,
1834 	.mmio_init = adl_uncore_mmio_init,
1835 };
1836 
1837 static const struct uncore_plat_init lnl_uncore_init __initconst = {
1838 	.cpu_init = lnl_uncore_cpu_init,
1839 	.mmio_init = lnl_uncore_mmio_init,
1840 };
1841 
1842 static const struct uncore_plat_init ptl_uncore_init __initconst = {
1843 	.cpu_init = ptl_uncore_cpu_init,
1844 	.mmio_init = ptl_uncore_mmio_init,
1845 	.domain[0].discovery_base = UNCORE_DISCOVERY_MSR,
1846 	.domain[0].global_init = uncore_mmio_global_init,
1847 };
1848 
1849 static const struct uncore_plat_init nvl_uncore_init __initconst = {
1850 	.cpu_init = nvl_uncore_cpu_init,
1851 	.mmio_init = ptl_uncore_mmio_init,
1852 	.domain[0].discovery_base = PACKAGE_UNCORE_DISCOVERY_MSR,
1853 	.domain[0].global_init = uncore_mmio_global_init,
1854 };
1855 
1856 static const struct uncore_plat_init icx_uncore_init __initconst = {
1857 	.cpu_init = icx_uncore_cpu_init,
1858 	.pci_init = icx_uncore_pci_init,
1859 	.mmio_init = icx_uncore_mmio_init,
1860 };
1861 
1862 static const struct uncore_plat_init snr_uncore_init __initconst = {
1863 	.cpu_init = snr_uncore_cpu_init,
1864 	.pci_init = snr_uncore_pci_init,
1865 	.mmio_init = snr_uncore_mmio_init,
1866 };
1867 
1868 static const struct uncore_plat_init spr_uncore_init __initconst = {
1869 	.cpu_init = spr_uncore_cpu_init,
1870 	.pci_init = spr_uncore_pci_init,
1871 	.mmio_init = spr_uncore_mmio_init,
1872 	.domain[0].base_is_pci = true,
1873 	.domain[0].discovery_base = UNCORE_DISCOVERY_TABLE_DEVICE,
1874 	.domain[0].units_ignore = spr_uncore_units_ignore,
1875 };
1876 
1877 static const struct uncore_plat_init gnr_uncore_init __initconst = {
1878 	.cpu_init = gnr_uncore_cpu_init,
1879 	.pci_init = gnr_uncore_pci_init,
1880 	.mmio_init = gnr_uncore_mmio_init,
1881 	.domain[0].base_is_pci = true,
1882 	.domain[0].discovery_base = UNCORE_DISCOVERY_TABLE_DEVICE,
1883 	.domain[0].units_ignore = gnr_uncore_units_ignore,
1884 	.domain[0].global_init = uncore_msr_global_init,
1885 };
1886 
1887 static const struct uncore_plat_init dmr_uncore_init __initconst = {
1888 	.pci_init = dmr_uncore_pci_init,
1889 	.mmio_init = dmr_uncore_mmio_init,
1890 	.domain[0].base_is_pci = true,
1891 	.domain[0].discovery_base = DMR_UNCORE_DISCOVERY_TABLE_DEVICE,
1892 	.domain[0].units_ignore = dmr_uncore_imh_units_ignore,
1893 	.domain[1].discovery_base = CBB_UNCORE_DISCOVERY_MSR,
1894 	.domain[1].units_ignore = dmr_uncore_cbb_units_ignore,
1895 	.domain[1].global_init = uncore_mmio_global_init,
1896 };
1897 
1898 static const struct uncore_plat_init generic_uncore_init __initconst = {
1899 	.cpu_init = intel_uncore_generic_uncore_cpu_init,
1900 	.pci_init = intel_uncore_generic_uncore_pci_init,
1901 	.mmio_init = intel_uncore_generic_uncore_mmio_init,
1902 	.domain[0].base_is_pci = true,
1903 	.domain[0].discovery_base = PCI_ANY_ID,
1904 	.domain[1].discovery_base = UNCORE_DISCOVERY_MSR,
1905 };
1906 
1907 static const struct x86_cpu_id intel_uncore_match[] __initconst = {
1908 	X86_MATCH_VFM(INTEL_NEHALEM_EP,		&nhm_uncore_init),
1909 	X86_MATCH_VFM(INTEL_NEHALEM,		&nhm_uncore_init),
1910 	X86_MATCH_VFM(INTEL_WESTMERE,		&nhm_uncore_init),
1911 	X86_MATCH_VFM(INTEL_WESTMERE_EP,	&nhm_uncore_init),
1912 	X86_MATCH_VFM(INTEL_SANDYBRIDGE,	&snb_uncore_init),
1913 	X86_MATCH_VFM(INTEL_IVYBRIDGE,		&ivb_uncore_init),
1914 	X86_MATCH_VFM(INTEL_HASWELL,		&hsw_uncore_init),
1915 	X86_MATCH_VFM(INTEL_HASWELL_L,		&hsw_uncore_init),
1916 	X86_MATCH_VFM(INTEL_HASWELL_G,		&hsw_uncore_init),
1917 	X86_MATCH_VFM(INTEL_BROADWELL,		&bdw_uncore_init),
1918 	X86_MATCH_VFM(INTEL_BROADWELL_G,	&bdw_uncore_init),
1919 	X86_MATCH_VFM(INTEL_SANDYBRIDGE_X,	&snbep_uncore_init),
1920 	X86_MATCH_VFM(INTEL_NEHALEM_EX,		&nhmex_uncore_init),
1921 	X86_MATCH_VFM(INTEL_WESTMERE_EX,	&nhmex_uncore_init),
1922 	X86_MATCH_VFM(INTEL_IVYBRIDGE_X,	&ivbep_uncore_init),
1923 	X86_MATCH_VFM(INTEL_HASWELL_X,		&hswep_uncore_init),
1924 	X86_MATCH_VFM(INTEL_BROADWELL_X,	&bdx_uncore_init),
1925 	X86_MATCH_VFM(INTEL_BROADWELL_D,	&bdx_uncore_init),
1926 	X86_MATCH_VFM(INTEL_XEON_PHI_KNL,	&knl_uncore_init),
1927 	X86_MATCH_VFM(INTEL_XEON_PHI_KNM,	&knl_uncore_init),
1928 	X86_MATCH_VFM(INTEL_SKYLAKE,		&skl_uncore_init),
1929 	X86_MATCH_VFM(INTEL_SKYLAKE_L,		&skl_uncore_init),
1930 	X86_MATCH_VFM(INTEL_SKYLAKE_X,		&skx_uncore_init),
1931 	X86_MATCH_VFM(INTEL_KABYLAKE_L,		&skl_uncore_init),
1932 	X86_MATCH_VFM(INTEL_KABYLAKE,		&skl_uncore_init),
1933 	X86_MATCH_VFM(INTEL_COMETLAKE_L,	&skl_uncore_init),
1934 	X86_MATCH_VFM(INTEL_COMETLAKE,		&skl_uncore_init),
1935 	X86_MATCH_VFM(INTEL_ICELAKE_L,		&icl_uncore_init),
1936 	X86_MATCH_VFM(INTEL_ICELAKE_NNPI,	&icl_uncore_init),
1937 	X86_MATCH_VFM(INTEL_ICELAKE,		&icl_uncore_init),
1938 	X86_MATCH_VFM(INTEL_ICELAKE_D,		&icx_uncore_init),
1939 	X86_MATCH_VFM(INTEL_ICELAKE_X,		&icx_uncore_init),
1940 	X86_MATCH_VFM(INTEL_TIGERLAKE_L,	&tgl_l_uncore_init),
1941 	X86_MATCH_VFM(INTEL_TIGERLAKE,		&tgl_uncore_init),
1942 	X86_MATCH_VFM(INTEL_ROCKETLAKE,		&rkl_uncore_init),
1943 	X86_MATCH_VFM(INTEL_ALDERLAKE,		&adl_uncore_init),
1944 	X86_MATCH_VFM(INTEL_ALDERLAKE_L,	&adl_uncore_init),
1945 	X86_MATCH_VFM(INTEL_RAPTORLAKE,		&adl_uncore_init),
1946 	X86_MATCH_VFM(INTEL_RAPTORLAKE_P,	&adl_uncore_init),
1947 	X86_MATCH_VFM(INTEL_RAPTORLAKE_S,	&adl_uncore_init),
1948 	X86_MATCH_VFM(INTEL_METEORLAKE,		&mtl_uncore_init),
1949 	X86_MATCH_VFM(INTEL_METEORLAKE_L,	&mtl_uncore_init),
1950 	X86_MATCH_VFM(INTEL_ARROWLAKE,		&mtl_uncore_init),
1951 	X86_MATCH_VFM(INTEL_ARROWLAKE_U,	&mtl_uncore_init),
1952 	X86_MATCH_VFM(INTEL_ARROWLAKE_H,	&mtl_uncore_init),
1953 	X86_MATCH_VFM(INTEL_LUNARLAKE_M,	&lnl_uncore_init),
1954 	X86_MATCH_VFM(INTEL_PANTHERLAKE_L,	&ptl_uncore_init),
1955 	X86_MATCH_VFM(INTEL_WILDCATLAKE_L,	&ptl_uncore_init),
1956 	X86_MATCH_VFM(INTEL_NOVALAKE,		&nvl_uncore_init),
1957 	X86_MATCH_VFM(INTEL_NOVALAKE_L,		&nvl_uncore_init),
1958 	X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X,	&spr_uncore_init),
1959 	X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X,	&spr_uncore_init),
1960 	X86_MATCH_VFM(INTEL_GRANITERAPIDS_X,	&gnr_uncore_init),
1961 	X86_MATCH_VFM(INTEL_GRANITERAPIDS_D,	&gnr_uncore_init),
1962 	X86_MATCH_VFM(INTEL_ATOM_TREMONT_D,	&snr_uncore_init),
1963 	X86_MATCH_VFM(INTEL_ATOM_GRACEMONT,	&adl_uncore_init),
1964 	X86_MATCH_VFM(INTEL_ATOM_CRESTMONT_X,	&gnr_uncore_init),
1965 	X86_MATCH_VFM(INTEL_ATOM_CRESTMONT,	&gnr_uncore_init),
1966 	X86_MATCH_VFM(INTEL_ATOM_DARKMONT_X,	&gnr_uncore_init),
1967 	X86_MATCH_VFM(INTEL_DIAMONDRAPIDS_X,	&dmr_uncore_init),
1968 	{},
1969 };
1970 MODULE_DEVICE_TABLE(x86cpu, intel_uncore_match);
1971 
1972 static bool uncore_use_discovery(struct uncore_plat_init *config)
1973 {
1974 	for (int i = 0; i < UNCORE_DISCOVERY_DOMAINS; i++) {
1975 		if (config->domain[i].discovery_base)
1976 			return true;
1977 	}
1978 
1979 	return false;
1980 }
1981 
1982 static int __init intel_uncore_init(void)
1983 {
1984 	const struct x86_cpu_id *id;
1985 	struct uncore_plat_init *uncore_init;
1986 	int pret = 0, cret = 0, mret = 0, ret;
1987 
1988 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
1989 		return -ENODEV;
1990 
1991 	__uncore_max_dies =
1992 		topology_max_packages() * topology_max_dies_per_package();
1993 
1994 	id = x86_match_cpu(intel_uncore_match);
1995 	if (!id) {
1996 		uncore_init = (struct uncore_plat_init *)&generic_uncore_init;
1997 		if (uncore_no_discover || !uncore_discovery(uncore_init))
1998 			return -ENODEV;
1999 	} else {
2000 		uncore_init = (struct uncore_plat_init *)id->driver_data;
2001 		if (uncore_no_discover && uncore_use_discovery(uncore_init))
2002 			return -ENODEV;
2003 		if (uncore_use_discovery(uncore_init) &&
2004 		    !uncore_discovery(uncore_init))
2005 			return -ENODEV;
2006 	}
2007 
2008 	if (uncore_init->pci_init) {
2009 		pret = uncore_init->pci_init();
2010 		if (!pret)
2011 			pret = uncore_pci_init();
2012 	}
2013 
2014 	if (uncore_init->cpu_init) {
2015 		uncore_init->cpu_init();
2016 		cret = uncore_cpu_init();
2017 	}
2018 
2019 	if (uncore_init->mmio_init) {
2020 		uncore_init->mmio_init();
2021 		mret = uncore_mmio_init();
2022 	}
2023 
2024 	if (cret && pret && mret) {
2025 		ret = -ENODEV;
2026 		goto free_discovery;
2027 	}
2028 
2029 	/* Install hotplug callbacks to setup the targets for each package */
2030 	ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_UNCORE_ONLINE,
2031 				"perf/x86/intel/uncore:online",
2032 				uncore_event_cpu_online,
2033 				uncore_event_cpu_offline);
2034 	if (ret)
2035 		goto err;
2036 	return 0;
2037 
2038 err:
2039 	uncore_types_exit(uncore_msr_uncores);
2040 	uncore_types_exit(uncore_mmio_uncores);
2041 	uncore_pci_exit();
2042 free_discovery:
2043 	intel_uncore_clear_discovery_tables();
2044 	return ret;
2045 }
2046 module_init(intel_uncore_init);
2047 
2048 static void __exit intel_uncore_exit(void)
2049 {
2050 	cpuhp_remove_state(CPUHP_AP_PERF_X86_UNCORE_ONLINE);
2051 	uncore_types_exit(uncore_msr_uncores);
2052 	uncore_types_exit(uncore_mmio_uncores);
2053 	uncore_pci_exit();
2054 	intel_uncore_clear_discovery_tables();
2055 }
2056 module_exit(intel_uncore_exit);
2057