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