1 #include "perf_event_intel_uncore.h"
3 static struct intel_uncore_type
*empty_uncore
[] = { NULL
, };
4 static struct intel_uncore_type
**msr_uncores
= empty_uncore
;
6 /* mask of cpus that collect uncore events */
7 static cpumask_t uncore_cpu_mask
;
9 /* constraint for the fixed counter */
10 static struct event_constraint constraint_fixed
=
11 EVENT_CONSTRAINT(~0ULL, 1 << UNCORE_PMC_IDX_FIXED
, ~0ULL);
13 DEFINE_UNCORE_FORMAT_ATTR(event
, event
, "config:0-7");
14 DEFINE_UNCORE_FORMAT_ATTR(umask
, umask
, "config:8-15");
15 DEFINE_UNCORE_FORMAT_ATTR(edge
, edge
, "config:18");
16 DEFINE_UNCORE_FORMAT_ATTR(inv
, inv
, "config:23");
17 DEFINE_UNCORE_FORMAT_ATTR(cmask5
, cmask
, "config:24-28");
18 DEFINE_UNCORE_FORMAT_ATTR(cmask8
, cmask
, "config:24-31");
20 /* Sandy Bridge uncore support */
21 static void snb_uncore_msr_enable_event(struct intel_uncore_box
*box
,
22 struct perf_event
*event
)
24 struct hw_perf_event
*hwc
= &event
->hw
;
26 if (hwc
->idx
< UNCORE_PMC_IDX_FIXED
)
27 wrmsrl(hwc
->config_base
, hwc
->config
| SNB_UNC_CTL_EN
);
29 wrmsrl(hwc
->config_base
, SNB_UNC_CTL_EN
);
32 static void snb_uncore_msr_disable_event(struct intel_uncore_box
*box
,
33 struct perf_event
*event
)
35 wrmsrl(event
->hw
.config_base
, 0);
38 static u64
snb_uncore_msr_read_counter(struct intel_uncore_box
*box
,
39 struct perf_event
*event
)
42 rdmsrl(event
->hw
.event_base
, count
);
46 static void snb_uncore_msr_init_box(struct intel_uncore_box
*box
)
48 if (box
->pmu
->pmu_idx
== 0) {
49 wrmsrl(SNB_UNC_PERF_GLOBAL_CTL
,
50 SNB_UNC_GLOBAL_CTL_EN
| SNB_UNC_GLOBAL_CTL_CORE_ALL
);
54 static struct attribute
*snb_uncore_formats_attr
[] = {
55 &format_attr_event
.attr
,
56 &format_attr_umask
.attr
,
57 &format_attr_edge
.attr
,
58 &format_attr_inv
.attr
,
59 &format_attr_cmask5
.attr
,
63 static struct attribute_group snb_uncore_format_group
= {
65 .attrs
= snb_uncore_formats_attr
,
68 static struct intel_uncore_ops snb_uncore_msr_ops
= {
69 .init_box
= snb_uncore_msr_init_box
,
70 .disable_event
= snb_uncore_msr_disable_event
,
71 .enable_event
= snb_uncore_msr_enable_event
,
72 .read_counter
= snb_uncore_msr_read_counter
,
75 static struct event_constraint snb_uncore_cbox_constraints
[] = {
76 UNCORE_EVENT_CONSTRAINT(0x80, 0x1),
77 UNCORE_EVENT_CONSTRAINT(0x83, 0x1),
81 static struct intel_uncore_type snb_uncore_cbox
= {
87 .perf_ctr
= SNB_UNC_CBO_0_PER_CTR0
,
88 .event_ctl
= SNB_UNC_CBO_0_PERFEVTSEL0
,
89 .fixed_ctr
= SNB_UNC_FIXED_CTR
,
90 .fixed_ctl
= SNB_UNC_FIXED_CTR_CTRL
,
92 .event_mask
= SNB_UNC_RAW_EVENT_MASK
,
93 .msr_offset
= SNB_UNC_CBO_MSR_OFFSET
,
94 .constraints
= snb_uncore_cbox_constraints
,
95 .ops
= &snb_uncore_msr_ops
,
96 .format_group
= &snb_uncore_format_group
,
99 static struct intel_uncore_type
*snb_msr_uncores
[] = {
103 /* end of Sandy Bridge uncore support */
105 /* Nehalem uncore support */
106 static void nhm_uncore_msr_disable_box(struct intel_uncore_box
*box
)
108 wrmsrl(NHM_UNC_PERF_GLOBAL_CTL
, 0);
111 static void nhm_uncore_msr_enable_box(struct intel_uncore_box
*box
)
113 wrmsrl(NHM_UNC_PERF_GLOBAL_CTL
,
114 NHM_UNC_GLOBAL_CTL_EN_PC_ALL
| NHM_UNC_GLOBAL_CTL_EN_FC
);
117 static void nhm_uncore_msr_enable_event(struct intel_uncore_box
*box
,
118 struct perf_event
*event
)
120 struct hw_perf_event
*hwc
= &event
->hw
;
122 if (hwc
->idx
< UNCORE_PMC_IDX_FIXED
)
123 wrmsrl(hwc
->config_base
, hwc
->config
| SNB_UNC_CTL_EN
);
125 wrmsrl(hwc
->config_base
, NHM_UNC_FIXED_CTR_CTL_EN
);
128 static struct attribute
*nhm_uncore_formats_attr
[] = {
129 &format_attr_event
.attr
,
130 &format_attr_umask
.attr
,
131 &format_attr_edge
.attr
,
132 &format_attr_inv
.attr
,
133 &format_attr_cmask8
.attr
,
137 static struct attribute_group nhm_uncore_format_group
= {
139 .attrs
= nhm_uncore_formats_attr
,
142 static struct uncore_event_desc nhm_uncore_events
[] = {
143 INTEL_UNCORE_EVENT_DESC(CLOCKTICKS
, "config=0xffff"),
144 /* full cache line writes to DRAM */
145 INTEL_UNCORE_EVENT_DESC(QMC_WRITES_FULL_ANY
, "event=0x2f,umask=0xf"),
146 /* Quickpath Memory Controller normal priority read requests */
147 INTEL_UNCORE_EVENT_DESC(QMC_NORMAL_READS_ANY
, "event=0x2c,umask=0xf"),
148 /* Quickpath Home Logic read requests from the IOH */
149 INTEL_UNCORE_EVENT_DESC(QHL_REQUEST_IOH_READS
,
150 "event=0x20,umask=0x1"),
151 /* Quickpath Home Logic write requests from the IOH */
152 INTEL_UNCORE_EVENT_DESC(QHL_REQUEST_IOH_WRITES
,
153 "event=0x20,umask=0x2"),
154 /* Quickpath Home Logic read requests from a remote socket */
155 INTEL_UNCORE_EVENT_DESC(QHL_REQUEST_REMOTE_READS
,
156 "event=0x20,umask=0x4"),
157 /* Quickpath Home Logic write requests from a remote socket */
158 INTEL_UNCORE_EVENT_DESC(QHL_REQUEST_REMOTE_WRITES
,
159 "event=0x20,umask=0x8"),
160 /* Quickpath Home Logic read requests from the local socket */
161 INTEL_UNCORE_EVENT_DESC(QHL_REQUEST_LOCAL_READS
,
162 "event=0x20,umask=0x10"),
163 /* Quickpath Home Logic write requests from the local socket */
164 INTEL_UNCORE_EVENT_DESC(QHL_REQUEST_LOCAL_WRITES
,
165 "event=0x20,umask=0x20"),
166 { /* end: all zeroes */ },
169 static struct intel_uncore_ops nhm_uncore_msr_ops
= {
170 .disable_box
= nhm_uncore_msr_disable_box
,
171 .enable_box
= nhm_uncore_msr_enable_box
,
172 .disable_event
= snb_uncore_msr_disable_event
,
173 .enable_event
= nhm_uncore_msr_enable_event
,
174 .read_counter
= snb_uncore_msr_read_counter
,
177 static struct intel_uncore_type nhm_uncore
= {
182 .fixed_ctr_bits
= 48,
183 .event_ctl
= NHM_UNC_PERFEVTSEL0
,
184 .perf_ctr
= NHM_UNC_UNCORE_PMC0
,
185 .fixed_ctr
= NHM_UNC_FIXED_CTR
,
186 .fixed_ctl
= NHM_UNC_FIXED_CTR_CTRL
,
187 .event_mask
= NHM_UNC_RAW_EVENT_MASK
,
188 .event_descs
= nhm_uncore_events
,
189 .ops
= &nhm_uncore_msr_ops
,
190 .format_group
= &nhm_uncore_format_group
,
193 static struct intel_uncore_type
*nhm_msr_uncores
[] = {
197 /* end of Nehalem uncore support */
199 static void uncore_assign_hw_event(struct intel_uncore_box
*box
,
200 struct perf_event
*event
, int idx
)
202 struct hw_perf_event
*hwc
= &event
->hw
;
205 hwc
->last_tag
= ++box
->tags
[idx
];
207 if (hwc
->idx
== UNCORE_PMC_IDX_FIXED
) {
208 hwc
->event_base
= uncore_msr_fixed_ctr(box
);
209 hwc
->config_base
= uncore_msr_fixed_ctl(box
);
213 hwc
->config_base
= uncore_msr_event_ctl(box
, hwc
->idx
);
214 hwc
->event_base
= uncore_msr_perf_ctr(box
, hwc
->idx
);
217 static void uncore_perf_event_update(struct intel_uncore_box
*box
,
218 struct perf_event
*event
)
220 u64 prev_count
, new_count
, delta
;
223 if (event
->hw
.idx
>= UNCORE_PMC_IDX_FIXED
)
224 shift
= 64 - uncore_fixed_ctr_bits(box
);
226 shift
= 64 - uncore_perf_ctr_bits(box
);
228 /* the hrtimer might modify the previous event value */
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
)
235 delta
= (new_count
<< shift
) - (prev_count
<< shift
);
238 local64_add(delta
, &event
->count
);
242 * The overflow interrupt is unavailable for SandyBridge-EP, is broken
243 * for SandyBridge. So we use hrtimer to periodically poll the counter
246 static enum hrtimer_restart
uncore_pmu_hrtimer(struct hrtimer
*hrtimer
)
248 struct intel_uncore_box
*box
;
252 box
= container_of(hrtimer
, struct intel_uncore_box
, hrtimer
);
253 if (!box
->n_active
|| box
->cpu
!= smp_processor_id())
254 return HRTIMER_NORESTART
;
256 * disable local interrupt to prevent uncore_pmu_event_start/stop
257 * to interrupt the update process
259 local_irq_save(flags
);
261 for_each_set_bit(bit
, box
->active_mask
, UNCORE_PMC_IDX_MAX
)
262 uncore_perf_event_update(box
, box
->events
[bit
]);
264 local_irq_restore(flags
);
266 hrtimer_forward_now(hrtimer
, ns_to_ktime(UNCORE_PMU_HRTIMER_INTERVAL
));
267 return HRTIMER_RESTART
;
270 static void uncore_pmu_start_hrtimer(struct intel_uncore_box
*box
)
272 __hrtimer_start_range_ns(&box
->hrtimer
,
273 ns_to_ktime(UNCORE_PMU_HRTIMER_INTERVAL
), 0,
274 HRTIMER_MODE_REL_PINNED
, 0);
277 static void uncore_pmu_cancel_hrtimer(struct intel_uncore_box
*box
)
279 hrtimer_cancel(&box
->hrtimer
);
282 static void uncore_pmu_init_hrtimer(struct intel_uncore_box
*box
)
284 hrtimer_init(&box
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
285 box
->hrtimer
.function
= uncore_pmu_hrtimer
;
288 struct intel_uncore_box
*uncore_alloc_box(int cpu
)
290 struct intel_uncore_box
*box
;
292 box
= kmalloc_node(sizeof(*box
), GFP_KERNEL
| __GFP_ZERO
,
297 uncore_pmu_init_hrtimer(box
);
298 atomic_set(&box
->refcnt
, 1);
305 static struct intel_uncore_box
*
306 uncore_pmu_to_box(struct intel_uncore_pmu
*pmu
, int cpu
)
308 return *per_cpu_ptr(pmu
->box
, cpu
);
311 static struct intel_uncore_pmu
*uncore_event_to_pmu(struct perf_event
*event
)
313 return container_of(event
->pmu
, struct intel_uncore_pmu
, pmu
);
316 static struct intel_uncore_box
*uncore_event_to_box(struct perf_event
*event
)
319 * perf core schedules event on the basis of cpu, uncore events are
320 * collected by one of the cpus inside a physical package.
322 return uncore_pmu_to_box(uncore_event_to_pmu(event
),
326 static int uncore_collect_events(struct intel_uncore_box
*box
,
327 struct perf_event
*leader
, bool dogrp
)
329 struct perf_event
*event
;
332 max_count
= box
->pmu
->type
->num_counters
;
333 if (box
->pmu
->type
->fixed_ctl
)
336 if (box
->n_events
>= max_count
)
340 box
->event_list
[n
] = leader
;
345 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
) {
346 if (event
->state
<= PERF_EVENT_STATE_OFF
)
352 box
->event_list
[n
] = event
;
358 static struct event_constraint
*
359 uncore_event_constraint(struct intel_uncore_type
*type
,
360 struct perf_event
*event
)
362 struct event_constraint
*c
;
364 if (event
->hw
.config
== ~0ULL)
365 return &constraint_fixed
;
367 if (type
->constraints
) {
368 for_each_event_constraint(c
, type
->constraints
) {
369 if ((event
->hw
.config
& c
->cmask
) == c
->code
)
374 return &type
->unconstrainted
;
377 static int uncore_assign_events(struct intel_uncore_box
*box
,
380 unsigned long used_mask
[BITS_TO_LONGS(UNCORE_PMC_IDX_MAX
)];
381 struct event_constraint
*c
, *constraints
[UNCORE_PMC_IDX_MAX
];
382 int i
, ret
, wmin
, wmax
;
383 struct hw_perf_event
*hwc
;
385 bitmap_zero(used_mask
, UNCORE_PMC_IDX_MAX
);
387 for (i
= 0, wmin
= UNCORE_PMC_IDX_MAX
, wmax
= 0; i
< n
; i
++) {
388 c
= uncore_event_constraint(box
->pmu
->type
,
391 wmin
= min(wmin
, c
->weight
);
392 wmax
= max(wmax
, c
->weight
);
395 /* fastpath, try to reuse previous register */
396 for (i
= 0; i
< n
; i
++) {
397 hwc
= &box
->event_list
[i
]->hw
;
404 /* constraint still honored */
405 if (!test_bit(hwc
->idx
, c
->idxmsk
))
408 /* not already used */
409 if (test_bit(hwc
->idx
, used_mask
))
412 __set_bit(hwc
->idx
, used_mask
);
413 assign
[i
] = hwc
->idx
;
419 ret
= perf_assign_events(constraints
, n
, wmin
, wmax
, assign
);
420 return ret
? -EINVAL
: 0;
423 static void uncore_pmu_event_start(struct perf_event
*event
, int flags
)
425 struct intel_uncore_box
*box
= uncore_event_to_box(event
);
426 int idx
= event
->hw
.idx
;
428 if (WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_STOPPED
)))
431 if (WARN_ON_ONCE(idx
== -1 || idx
>= UNCORE_PMC_IDX_MAX
))
435 box
->events
[idx
] = event
;
437 __set_bit(idx
, box
->active_mask
);
439 local64_set(&event
->hw
.prev_count
, uncore_read_counter(box
, event
));
440 uncore_enable_event(box
, event
);
442 if (box
->n_active
== 1) {
443 uncore_enable_box(box
);
444 uncore_pmu_start_hrtimer(box
);
448 static void uncore_pmu_event_stop(struct perf_event
*event
, int flags
)
450 struct intel_uncore_box
*box
= uncore_event_to_box(event
);
451 struct hw_perf_event
*hwc
= &event
->hw
;
453 if (__test_and_clear_bit(hwc
->idx
, box
->active_mask
)) {
454 uncore_disable_event(box
, event
);
456 box
->events
[hwc
->idx
] = NULL
;
457 WARN_ON_ONCE(hwc
->state
& PERF_HES_STOPPED
);
458 hwc
->state
|= PERF_HES_STOPPED
;
460 if (box
->n_active
== 0) {
461 uncore_disable_box(box
);
462 uncore_pmu_cancel_hrtimer(box
);
466 if ((flags
& PERF_EF_UPDATE
) && !(hwc
->state
& PERF_HES_UPTODATE
)) {
468 * Drain the remaining delta count out of a event
469 * that we are disabling:
471 uncore_perf_event_update(box
, event
);
472 hwc
->state
|= PERF_HES_UPTODATE
;
476 static int uncore_pmu_event_add(struct perf_event
*event
, int flags
)
478 struct intel_uncore_box
*box
= uncore_event_to_box(event
);
479 struct hw_perf_event
*hwc
= &event
->hw
;
480 int assign
[UNCORE_PMC_IDX_MAX
];
486 ret
= n
= uncore_collect_events(box
, event
, false);
490 hwc
->state
= PERF_HES_UPTODATE
| PERF_HES_STOPPED
;
491 if (!(flags
& PERF_EF_START
))
492 hwc
->state
|= PERF_HES_ARCH
;
494 ret
= uncore_assign_events(box
, assign
, n
);
498 /* save events moving to new counters */
499 for (i
= 0; i
< box
->n_events
; i
++) {
500 event
= box
->event_list
[i
];
503 if (hwc
->idx
== assign
[i
] &&
504 hwc
->last_tag
== box
->tags
[assign
[i
]])
507 * Ensure we don't accidentally enable a stopped
508 * counter simply because we rescheduled.
510 if (hwc
->state
& PERF_HES_STOPPED
)
511 hwc
->state
|= PERF_HES_ARCH
;
513 uncore_pmu_event_stop(event
, PERF_EF_UPDATE
);
516 /* reprogram moved events into new counters */
517 for (i
= 0; i
< n
; i
++) {
518 event
= box
->event_list
[i
];
521 if (hwc
->idx
!= assign
[i
] ||
522 hwc
->last_tag
!= box
->tags
[assign
[i
]])
523 uncore_assign_hw_event(box
, event
, assign
[i
]);
524 else if (i
< box
->n_events
)
527 if (hwc
->state
& PERF_HES_ARCH
)
530 uncore_pmu_event_start(event
, 0);
537 static void uncore_pmu_event_del(struct perf_event
*event
, int flags
)
539 struct intel_uncore_box
*box
= uncore_event_to_box(event
);
542 uncore_pmu_event_stop(event
, PERF_EF_UPDATE
);
544 for (i
= 0; i
< box
->n_events
; i
++) {
545 if (event
== box
->event_list
[i
]) {
546 while (++i
< box
->n_events
)
547 box
->event_list
[i
- 1] = box
->event_list
[i
];
555 event
->hw
.last_tag
= ~0ULL;
558 static void uncore_pmu_event_read(struct perf_event
*event
)
560 struct intel_uncore_box
*box
= uncore_event_to_box(event
);
561 uncore_perf_event_update(box
, event
);
565 * validation ensures the group can be loaded onto the
566 * PMU if it was the only group available.
568 static int uncore_validate_group(struct intel_uncore_pmu
*pmu
,
569 struct perf_event
*event
)
571 struct perf_event
*leader
= event
->group_leader
;
572 struct intel_uncore_box
*fake_box
;
573 int assign
[UNCORE_PMC_IDX_MAX
];
574 int ret
= -EINVAL
, n
;
576 fake_box
= uncore_alloc_box(smp_processor_id());
582 * the event is not yet connected with its
583 * siblings therefore we must first collect
584 * existing siblings, then add the new event
585 * before we can simulate the scheduling
587 n
= uncore_collect_events(fake_box
, leader
, true);
591 fake_box
->n_events
= n
;
592 n
= uncore_collect_events(fake_box
, event
, false);
596 fake_box
->n_events
= n
;
598 ret
= uncore_assign_events(fake_box
, assign
, n
);
604 int uncore_pmu_event_init(struct perf_event
*event
)
606 struct intel_uncore_pmu
*pmu
;
607 struct intel_uncore_box
*box
;
608 struct hw_perf_event
*hwc
= &event
->hw
;
611 if (event
->attr
.type
!= event
->pmu
->type
)
614 pmu
= uncore_event_to_pmu(event
);
615 /* no device found for this pmu */
616 if (pmu
->func_id
< 0)
620 * Uncore PMU does measure at all privilege level all the time.
621 * So it doesn't make sense to specify any exclude bits.
623 if (event
->attr
.exclude_user
|| event
->attr
.exclude_kernel
||
624 event
->attr
.exclude_hv
|| event
->attr
.exclude_idle
)
627 /* Sampling not supported yet */
628 if (hwc
->sample_period
)
632 * Place all uncore events for a particular physical package
637 box
= uncore_pmu_to_box(pmu
, event
->cpu
);
638 if (!box
|| box
->cpu
< 0)
640 event
->cpu
= box
->cpu
;
642 if (event
->attr
.config
== UNCORE_FIXED_EVENT
) {
643 /* no fixed counter */
644 if (!pmu
->type
->fixed_ctl
)
647 * if there is only one fixed counter, only the first pmu
648 * can access the fixed counter
650 if (pmu
->type
->single_fixed
&& pmu
->pmu_idx
> 0)
654 hwc
->config
= event
->attr
.config
& pmu
->type
->event_mask
;
658 event
->hw
.last_tag
= ~0ULL;
660 if (event
->group_leader
!= event
)
661 ret
= uncore_validate_group(pmu
, event
);
668 static int __init
uncore_pmu_register(struct intel_uncore_pmu
*pmu
)
672 pmu
->pmu
= (struct pmu
) {
673 .attr_groups
= pmu
->type
->attr_groups
,
674 .task_ctx_nr
= perf_invalid_context
,
675 .event_init
= uncore_pmu_event_init
,
676 .add
= uncore_pmu_event_add
,
677 .del
= uncore_pmu_event_del
,
678 .start
= uncore_pmu_event_start
,
679 .stop
= uncore_pmu_event_stop
,
680 .read
= uncore_pmu_event_read
,
683 if (pmu
->type
->num_boxes
== 1) {
684 if (strlen(pmu
->type
->name
) > 0)
685 sprintf(pmu
->name
, "uncore_%s", pmu
->type
->name
);
687 sprintf(pmu
->name
, "uncore");
689 sprintf(pmu
->name
, "uncore_%s_%d", pmu
->type
->name
,
693 ret
= perf_pmu_register(&pmu
->pmu
, pmu
->name
, -1);
697 static void __init
uncore_type_exit(struct intel_uncore_type
*type
)
701 for (i
= 0; i
< type
->num_boxes
; i
++)
702 free_percpu(type
->pmus
[i
].box
);
705 kfree(type
->attr_groups
[1]);
706 type
->attr_groups
[1] = NULL
;
709 static int __init
uncore_type_init(struct intel_uncore_type
*type
)
711 struct intel_uncore_pmu
*pmus
;
712 struct attribute_group
*events_group
;
713 struct attribute
**attrs
;
716 pmus
= kzalloc(sizeof(*pmus
) * type
->num_boxes
, GFP_KERNEL
);
720 type
->unconstrainted
= (struct event_constraint
)
721 __EVENT_CONSTRAINT(0, (1ULL << type
->num_counters
) - 1,
722 0, type
->num_counters
, 0);
724 for (i
= 0; i
< type
->num_boxes
; i
++) {
725 pmus
[i
].func_id
= -1;
728 pmus
[i
].box
= alloc_percpu(struct intel_uncore_box
*);
733 if (type
->event_descs
) {
735 while (type
->event_descs
[i
].attr
.attr
.name
)
738 events_group
= kzalloc(sizeof(struct attribute
*) * (i
+ 1) +
739 sizeof(*events_group
), GFP_KERNEL
);
743 attrs
= (struct attribute
**)(events_group
+ 1);
744 events_group
->name
= "events";
745 events_group
->attrs
= attrs
;
747 for (j
= 0; j
< i
; j
++)
748 attrs
[j
] = &type
->event_descs
[j
].attr
.attr
;
750 type
->attr_groups
[1] = events_group
;
756 uncore_type_exit(type
);
760 static int __init
uncore_types_init(struct intel_uncore_type
**types
)
764 for (i
= 0; types
[i
]; i
++) {
765 ret
= uncore_type_init(types
[i
]);
772 uncore_type_exit(types
[i
]);
776 static void __cpuinit
uncore_cpu_dying(int cpu
)
778 struct intel_uncore_type
*type
;
779 struct intel_uncore_pmu
*pmu
;
780 struct intel_uncore_box
*box
;
783 for (i
= 0; msr_uncores
[i
]; i
++) {
784 type
= msr_uncores
[i
];
785 for (j
= 0; j
< type
->num_boxes
; j
++) {
786 pmu
= &type
->pmus
[j
];
787 box
= *per_cpu_ptr(pmu
->box
, cpu
);
788 *per_cpu_ptr(pmu
->box
, cpu
) = NULL
;
789 if (box
&& atomic_dec_and_test(&box
->refcnt
))
795 static int __cpuinit
uncore_cpu_starting(int cpu
)
797 struct intel_uncore_type
*type
;
798 struct intel_uncore_pmu
*pmu
;
799 struct intel_uncore_box
*box
, *exist
;
800 int i
, j
, k
, phys_id
;
802 phys_id
= topology_physical_package_id(cpu
);
804 for (i
= 0; msr_uncores
[i
]; i
++) {
805 type
= msr_uncores
[i
];
806 for (j
= 0; j
< type
->num_boxes
; j
++) {
807 pmu
= &type
->pmus
[j
];
808 box
= *per_cpu_ptr(pmu
->box
, cpu
);
809 /* called by uncore_cpu_init? */
810 if (box
&& box
->phys_id
>= 0) {
811 uncore_box_init(box
);
815 for_each_online_cpu(k
) {
816 exist
= *per_cpu_ptr(pmu
->box
, k
);
817 if (exist
&& exist
->phys_id
== phys_id
) {
818 atomic_inc(&exist
->refcnt
);
819 *per_cpu_ptr(pmu
->box
, cpu
) = exist
;
827 box
->phys_id
= phys_id
;
828 uncore_box_init(box
);
835 static int __cpuinit
uncore_cpu_prepare(int cpu
, int phys_id
)
837 struct intel_uncore_type
*type
;
838 struct intel_uncore_pmu
*pmu
;
839 struct intel_uncore_box
*box
;
842 for (i
= 0; msr_uncores
[i
]; i
++) {
843 type
= msr_uncores
[i
];
844 for (j
= 0; j
< type
->num_boxes
; j
++) {
845 pmu
= &type
->pmus
[j
];
846 if (pmu
->func_id
< 0)
849 box
= uncore_alloc_box(cpu
);
854 box
->phys_id
= phys_id
;
855 *per_cpu_ptr(pmu
->box
, cpu
) = box
;
861 static void __cpuinit
uncore_change_context(struct intel_uncore_type
**uncores
,
862 int old_cpu
, int new_cpu
)
864 struct intel_uncore_type
*type
;
865 struct intel_uncore_pmu
*pmu
;
866 struct intel_uncore_box
*box
;
869 for (i
= 0; uncores
[i
]; i
++) {
871 for (j
= 0; j
< type
->num_boxes
; j
++) {
872 pmu
= &type
->pmus
[j
];
874 box
= uncore_pmu_to_box(pmu
, new_cpu
);
876 box
= uncore_pmu_to_box(pmu
, old_cpu
);
881 WARN_ON_ONCE(box
->cpu
!= -1);
886 WARN_ON_ONCE(box
->cpu
!= old_cpu
);
888 uncore_pmu_cancel_hrtimer(box
);
889 perf_pmu_migrate_context(&pmu
->pmu
,
899 static void __cpuinit
uncore_event_exit_cpu(int cpu
)
901 int i
, phys_id
, target
;
903 /* if exiting cpu is used for collecting uncore events */
904 if (!cpumask_test_and_clear_cpu(cpu
, &uncore_cpu_mask
))
907 /* find a new cpu to collect uncore events */
908 phys_id
= topology_physical_package_id(cpu
);
910 for_each_online_cpu(i
) {
913 if (phys_id
== topology_physical_package_id(i
)) {
919 /* migrate uncore events to the new cpu */
921 cpumask_set_cpu(target
, &uncore_cpu_mask
);
923 uncore_change_context(msr_uncores
, cpu
, target
);
926 static void __cpuinit
uncore_event_init_cpu(int cpu
)
930 phys_id
= topology_physical_package_id(cpu
);
931 for_each_cpu(i
, &uncore_cpu_mask
) {
932 if (phys_id
== topology_physical_package_id(i
))
936 cpumask_set_cpu(cpu
, &uncore_cpu_mask
);
938 uncore_change_context(msr_uncores
, -1, cpu
);
941 static int __cpuinit
uncore_cpu_notifier(struct notifier_block
*self
,
942 unsigned long action
, void *hcpu
)
944 unsigned int cpu
= (long)hcpu
;
946 /* allocate/free data structure for uncore box */
947 switch (action
& ~CPU_TASKS_FROZEN
) {
949 uncore_cpu_prepare(cpu
, -1);
952 uncore_cpu_starting(cpu
);
954 case CPU_UP_CANCELED
:
956 uncore_cpu_dying(cpu
);
962 /* select the cpu that collects uncore events */
963 switch (action
& ~CPU_TASKS_FROZEN
) {
964 case CPU_DOWN_FAILED
:
966 uncore_event_init_cpu(cpu
);
968 case CPU_DOWN_PREPARE
:
969 uncore_event_exit_cpu(cpu
);
978 static struct notifier_block uncore_cpu_nb __cpuinitdata
= {
979 .notifier_call
= uncore_cpu_notifier
,
981 * to migrate uncore events, our notifier should be executed
982 * before perf core's notifier.
984 .priority
= CPU_PRI_PERF
+ 1,
987 static void __init
uncore_cpu_setup(void *dummy
)
989 uncore_cpu_starting(smp_processor_id());
992 static int __init
uncore_cpu_init(void)
996 switch (boot_cpu_data
.x86_model
) {
997 case 26: /* Nehalem */
999 case 37: /* Westmere */
1001 msr_uncores
= nhm_msr_uncores
;
1003 case 42: /* Sandy Bridge */
1004 msr_uncores
= snb_msr_uncores
;
1010 ret
= uncore_types_init(msr_uncores
);
1016 for_each_online_cpu(cpu
) {
1017 int i
, phys_id
= topology_physical_package_id(cpu
);
1019 for_each_cpu(i
, &uncore_cpu_mask
) {
1020 if (phys_id
== topology_physical_package_id(i
)) {
1028 uncore_cpu_prepare(cpu
, phys_id
);
1029 uncore_event_init_cpu(cpu
);
1031 on_each_cpu(uncore_cpu_setup
, NULL
, 1);
1033 register_cpu_notifier(&uncore_cpu_nb
);
1040 static int __init
uncore_pmus_register(void)
1042 struct intel_uncore_pmu
*pmu
;
1043 struct intel_uncore_type
*type
;
1046 for (i
= 0; msr_uncores
[i
]; i
++) {
1047 type
= msr_uncores
[i
];
1048 for (j
= 0; j
< type
->num_boxes
; j
++) {
1049 pmu
= &type
->pmus
[j
];
1050 uncore_pmu_register(pmu
);
1057 static int __init
intel_uncore_init(void)
1061 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_INTEL
)
1064 ret
= uncore_cpu_init();
1068 uncore_pmus_register();
1073 device_initcall(intel_uncore_init
);