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