1 #include <linux/perf_event.h>
2 #include <linux/export.h>
3 #include <linux/types.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
6 #include <linux/delay.h>
7 #include <asm/apicdef.h>
10 #include "../perf_event.h"
12 static DEFINE_PER_CPU(unsigned int, perf_nmi_counter
);
14 static __initconst
const u64 amd_hw_cache_event_ids
15 [PERF_COUNT_HW_CACHE_MAX
]
16 [PERF_COUNT_HW_CACHE_OP_MAX
]
17 [PERF_COUNT_HW_CACHE_RESULT_MAX
] =
21 [ C(RESULT_ACCESS
) ] = 0x0040, /* Data Cache Accesses */
22 [ C(RESULT_MISS
) ] = 0x0141, /* Data Cache Misses */
25 [ C(RESULT_ACCESS
) ] = 0,
26 [ C(RESULT_MISS
) ] = 0,
28 [ C(OP_PREFETCH
) ] = {
29 [ C(RESULT_ACCESS
) ] = 0x0267, /* Data Prefetcher :attempts */
30 [ C(RESULT_MISS
) ] = 0x0167, /* Data Prefetcher :cancelled */
35 [ C(RESULT_ACCESS
) ] = 0x0080, /* Instruction cache fetches */
36 [ C(RESULT_MISS
) ] = 0x0081, /* Instruction cache misses */
39 [ C(RESULT_ACCESS
) ] = -1,
40 [ C(RESULT_MISS
) ] = -1,
42 [ C(OP_PREFETCH
) ] = {
43 [ C(RESULT_ACCESS
) ] = 0x014B, /* Prefetch Instructions :Load */
44 [ C(RESULT_MISS
) ] = 0,
49 [ C(RESULT_ACCESS
) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
50 [ C(RESULT_MISS
) ] = 0x037E, /* L2 Cache Misses : IC+DC */
53 [ C(RESULT_ACCESS
) ] = 0x017F, /* L2 Fill/Writeback */
54 [ C(RESULT_MISS
) ] = 0,
56 [ C(OP_PREFETCH
) ] = {
57 [ C(RESULT_ACCESS
) ] = 0,
58 [ C(RESULT_MISS
) ] = 0,
63 [ C(RESULT_ACCESS
) ] = 0x0040, /* Data Cache Accesses */
64 [ C(RESULT_MISS
) ] = 0x0746, /* L1_DTLB_AND_L2_DLTB_MISS.ALL */
67 [ C(RESULT_ACCESS
) ] = 0,
68 [ C(RESULT_MISS
) ] = 0,
70 [ C(OP_PREFETCH
) ] = {
71 [ C(RESULT_ACCESS
) ] = 0,
72 [ C(RESULT_MISS
) ] = 0,
77 [ C(RESULT_ACCESS
) ] = 0x0080, /* Instruction fecthes */
78 [ C(RESULT_MISS
) ] = 0x0385, /* L1_ITLB_AND_L2_ITLB_MISS.ALL */
81 [ C(RESULT_ACCESS
) ] = -1,
82 [ C(RESULT_MISS
) ] = -1,
84 [ C(OP_PREFETCH
) ] = {
85 [ C(RESULT_ACCESS
) ] = -1,
86 [ C(RESULT_MISS
) ] = -1,
91 [ C(RESULT_ACCESS
) ] = 0x00c2, /* Retired Branch Instr. */
92 [ C(RESULT_MISS
) ] = 0x00c3, /* Retired Mispredicted BI */
95 [ C(RESULT_ACCESS
) ] = -1,
96 [ C(RESULT_MISS
) ] = -1,
98 [ C(OP_PREFETCH
) ] = {
99 [ C(RESULT_ACCESS
) ] = -1,
100 [ C(RESULT_MISS
) ] = -1,
105 [ C(RESULT_ACCESS
) ] = 0xb8e9, /* CPU Request to Memory, l+r */
106 [ C(RESULT_MISS
) ] = 0x98e9, /* CPU Request to Memory, r */
109 [ C(RESULT_ACCESS
) ] = -1,
110 [ C(RESULT_MISS
) ] = -1,
112 [ C(OP_PREFETCH
) ] = {
113 [ C(RESULT_ACCESS
) ] = -1,
114 [ C(RESULT_MISS
) ] = -1,
119 static __initconst
const u64 amd_hw_cache_event_ids_f17h
120 [PERF_COUNT_HW_CACHE_MAX
]
121 [PERF_COUNT_HW_CACHE_OP_MAX
]
122 [PERF_COUNT_HW_CACHE_RESULT_MAX
] = {
125 [C(RESULT_ACCESS
)] = 0x0040, /* Data Cache Accesses */
126 [C(RESULT_MISS
)] = 0xc860, /* L2$ access from DC Miss */
129 [C(RESULT_ACCESS
)] = 0,
130 [C(RESULT_MISS
)] = 0,
133 [C(RESULT_ACCESS
)] = 0xff5a, /* h/w prefetch DC Fills */
134 [C(RESULT_MISS
)] = 0,
139 [C(RESULT_ACCESS
)] = 0x0080, /* Instruction cache fetches */
140 [C(RESULT_MISS
)] = 0x0081, /* Instruction cache misses */
143 [C(RESULT_ACCESS
)] = -1,
144 [C(RESULT_MISS
)] = -1,
147 [C(RESULT_ACCESS
)] = 0,
148 [C(RESULT_MISS
)] = 0,
153 [C(RESULT_ACCESS
)] = 0,
154 [C(RESULT_MISS
)] = 0,
157 [C(RESULT_ACCESS
)] = 0,
158 [C(RESULT_MISS
)] = 0,
161 [C(RESULT_ACCESS
)] = 0,
162 [C(RESULT_MISS
)] = 0,
167 [C(RESULT_ACCESS
)] = 0xff45, /* All L2 DTLB accesses */
168 [C(RESULT_MISS
)] = 0xf045, /* L2 DTLB misses (PT walks) */
171 [C(RESULT_ACCESS
)] = 0,
172 [C(RESULT_MISS
)] = 0,
175 [C(RESULT_ACCESS
)] = 0,
176 [C(RESULT_MISS
)] = 0,
181 [C(RESULT_ACCESS
)] = 0x0084, /* L1 ITLB misses, L2 ITLB hits */
182 [C(RESULT_MISS
)] = 0xff85, /* L1 ITLB misses, L2 misses */
185 [C(RESULT_ACCESS
)] = -1,
186 [C(RESULT_MISS
)] = -1,
189 [C(RESULT_ACCESS
)] = -1,
190 [C(RESULT_MISS
)] = -1,
195 [C(RESULT_ACCESS
)] = 0x00c2, /* Retired Branch Instr. */
196 [C(RESULT_MISS
)] = 0x00c3, /* Retired Mispredicted BI */
199 [C(RESULT_ACCESS
)] = -1,
200 [C(RESULT_MISS
)] = -1,
203 [C(RESULT_ACCESS
)] = -1,
204 [C(RESULT_MISS
)] = -1,
209 [C(RESULT_ACCESS
)] = 0,
210 [C(RESULT_MISS
)] = 0,
213 [C(RESULT_ACCESS
)] = -1,
214 [C(RESULT_MISS
)] = -1,
217 [C(RESULT_ACCESS
)] = -1,
218 [C(RESULT_MISS
)] = -1,
224 * AMD Performance Monitor K7 and later, up to and including Family 16h:
226 static const u64 amd_perfmon_event_map
[PERF_COUNT_HW_MAX
] =
228 [PERF_COUNT_HW_CPU_CYCLES
] = 0x0076,
229 [PERF_COUNT_HW_INSTRUCTIONS
] = 0x00c0,
230 [PERF_COUNT_HW_CACHE_REFERENCES
] = 0x077d,
231 [PERF_COUNT_HW_CACHE_MISSES
] = 0x077e,
232 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = 0x00c2,
233 [PERF_COUNT_HW_BRANCH_MISSES
] = 0x00c3,
234 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
] = 0x00d0, /* "Decoder empty" event */
235 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND
] = 0x00d1, /* "Dispatch stalls" event */
239 * AMD Performance Monitor Family 17h and later:
241 static const u64 amd_f17h_perfmon_event_map
[PERF_COUNT_HW_MAX
] =
243 [PERF_COUNT_HW_CPU_CYCLES
] = 0x0076,
244 [PERF_COUNT_HW_INSTRUCTIONS
] = 0x00c0,
245 [PERF_COUNT_HW_CACHE_REFERENCES
] = 0xff60,
246 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = 0x00c2,
247 [PERF_COUNT_HW_BRANCH_MISSES
] = 0x00c3,
248 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
] = 0x0287,
249 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND
] = 0x0187,
252 static u64
amd_pmu_event_map(int hw_event
)
254 if (boot_cpu_data
.x86
>= 0x17)
255 return amd_f17h_perfmon_event_map
[hw_event
];
257 return amd_perfmon_event_map
[hw_event
];
261 * Previously calculated offsets
263 static unsigned int event_offsets
[X86_PMC_IDX_MAX
] __read_mostly
;
264 static unsigned int count_offsets
[X86_PMC_IDX_MAX
] __read_mostly
;
268 * 4 counters starting at 0xc0010000 each offset by 1
270 * CPUs with core performance counter extensions:
271 * 6 counters starting at 0xc0010200 each offset by 2
273 static inline int amd_pmu_addr_offset(int index
, bool eventsel
)
281 offset
= event_offsets
[index
];
283 offset
= count_offsets
[index
];
288 if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE
))
294 event_offsets
[index
] = offset
;
296 count_offsets
[index
] = offset
;
301 static int amd_core_hw_config(struct perf_event
*event
)
303 if (event
->attr
.exclude_host
&& event
->attr
.exclude_guest
)
305 * When HO == GO == 1 the hardware treats that as GO == HO == 0
306 * and will count in both modes. We don't want to count in that
307 * case so we emulate no-counting by setting US = OS = 0.
309 event
->hw
.config
&= ~(ARCH_PERFMON_EVENTSEL_USR
|
310 ARCH_PERFMON_EVENTSEL_OS
);
311 else if (event
->attr
.exclude_host
)
312 event
->hw
.config
|= AMD64_EVENTSEL_GUESTONLY
;
313 else if (event
->attr
.exclude_guest
)
314 event
->hw
.config
|= AMD64_EVENTSEL_HOSTONLY
;
320 * AMD64 events are detected based on their event codes.
322 static inline unsigned int amd_get_event_code(struct hw_perf_event
*hwc
)
324 return ((hwc
->config
>> 24) & 0x0f00) | (hwc
->config
& 0x00ff);
327 static inline int amd_is_nb_event(struct hw_perf_event
*hwc
)
329 return (hwc
->config
& 0xe0) == 0xe0;
332 static inline int amd_has_nb(struct cpu_hw_events
*cpuc
)
334 struct amd_nb
*nb
= cpuc
->amd_nb
;
336 return nb
&& nb
->nb_id
!= -1;
339 static int amd_pmu_hw_config(struct perf_event
*event
)
343 /* pass precise event sampling to ibs: */
344 if (event
->attr
.precise_ip
&& get_ibs_caps())
347 if (has_branch_stack(event
))
350 ret
= x86_pmu_hw_config(event
);
354 if (event
->attr
.type
== PERF_TYPE_RAW
)
355 event
->hw
.config
|= event
->attr
.config
& AMD64_RAW_EVENT_MASK
;
357 return amd_core_hw_config(event
);
360 static void __amd_put_nb_event_constraints(struct cpu_hw_events
*cpuc
,
361 struct perf_event
*event
)
363 struct amd_nb
*nb
= cpuc
->amd_nb
;
367 * need to scan whole list because event may not have
368 * been assigned during scheduling
370 * no race condition possible because event can only
371 * be removed on one CPU at a time AND PMU is disabled
374 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
375 if (cmpxchg(nb
->owners
+ i
, event
, NULL
) == event
)
381 * AMD64 NorthBridge events need special treatment because
382 * counter access needs to be synchronized across all cores
383 * of a package. Refer to BKDG section 3.12
385 * NB events are events measuring L3 cache, Hypertransport
386 * traffic. They are identified by an event code >= 0xe00.
387 * They measure events on the NorthBride which is shared
388 * by all cores on a package. NB events are counted on a
389 * shared set of counters. When a NB event is programmed
390 * in a counter, the data actually comes from a shared
391 * counter. Thus, access to those counters needs to be
394 * We implement the synchronization such that no two cores
395 * can be measuring NB events using the same counters. Thus,
396 * we maintain a per-NB allocation table. The available slot
397 * is propagated using the event_constraint structure.
399 * We provide only one choice for each NB event based on
400 * the fact that only NB events have restrictions. Consequently,
401 * if a counter is available, there is a guarantee the NB event
402 * will be assigned to it. If no slot is available, an empty
403 * constraint is returned and scheduling will eventually fail
406 * Note that all cores attached the same NB compete for the same
407 * counters to host NB events, this is why we use atomic ops. Some
408 * multi-chip CPUs may have more than one NB.
410 * Given that resources are allocated (cmpxchg), they must be
411 * eventually freed for others to use. This is accomplished by
412 * calling __amd_put_nb_event_constraints()
414 * Non NB events are not impacted by this restriction.
416 static struct event_constraint
*
417 __amd_get_nb_event_constraints(struct cpu_hw_events
*cpuc
, struct perf_event
*event
,
418 struct event_constraint
*c
)
420 struct hw_perf_event
*hwc
= &event
->hw
;
421 struct amd_nb
*nb
= cpuc
->amd_nb
;
422 struct perf_event
*old
;
432 * detect if already present, if so reuse
434 * cannot merge with actual allocation
435 * because of possible holes
437 * event can already be present yet not assigned (in hwc->idx)
438 * because of successive calls to x86_schedule_events() from
439 * hw_perf_group_sched_in() without hw_perf_enable()
441 for_each_set_bit(idx
, c
->idxmsk
, x86_pmu
.num_counters
) {
442 if (new == -1 || hwc
->idx
== idx
)
443 /* assign free slot, prefer hwc->idx */
444 old
= cmpxchg(nb
->owners
+ idx
, NULL
, event
);
445 else if (nb
->owners
[idx
] == event
)
446 /* event already present */
451 if (old
&& old
!= event
)
454 /* reassign to this slot */
456 cmpxchg(nb
->owners
+ new, event
, NULL
);
459 /* already present, reuse */
465 return &emptyconstraint
;
467 return &nb
->event_constraints
[new];
470 static struct amd_nb
*amd_alloc_nb(int cpu
)
475 nb
= kzalloc_node(sizeof(struct amd_nb
), GFP_KERNEL
, cpu_to_node(cpu
));
482 * initialize all possible NB constraints
484 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
485 __set_bit(i
, nb
->event_constraints
[i
].idxmsk
);
486 nb
->event_constraints
[i
].weight
= 1;
491 static int amd_pmu_cpu_prepare(int cpu
)
493 struct cpu_hw_events
*cpuc
= &per_cpu(cpu_hw_events
, cpu
);
495 WARN_ON_ONCE(cpuc
->amd_nb
);
497 if (!x86_pmu
.amd_nb_constraints
)
500 cpuc
->amd_nb
= amd_alloc_nb(cpu
);
507 static void amd_pmu_cpu_starting(int cpu
)
509 struct cpu_hw_events
*cpuc
= &per_cpu(cpu_hw_events
, cpu
);
510 void **onln
= &cpuc
->kfree_on_online
[X86_PERF_KFREE_SHARED
];
514 cpuc
->perf_ctr_virt_mask
= AMD64_EVENTSEL_HOSTONLY
;
516 if (!x86_pmu
.amd_nb_constraints
)
519 nb_id
= amd_get_nb_id(cpu
);
520 WARN_ON_ONCE(nb_id
== BAD_APICID
);
522 for_each_online_cpu(i
) {
523 nb
= per_cpu(cpu_hw_events
, i
).amd_nb
;
524 if (WARN_ON_ONCE(!nb
))
527 if (nb
->nb_id
== nb_id
) {
528 *onln
= cpuc
->amd_nb
;
534 cpuc
->amd_nb
->nb_id
= nb_id
;
535 cpuc
->amd_nb
->refcnt
++;
538 static void amd_pmu_cpu_dead(int cpu
)
540 struct cpu_hw_events
*cpuhw
;
542 if (!x86_pmu
.amd_nb_constraints
)
545 cpuhw
= &per_cpu(cpu_hw_events
, cpu
);
548 struct amd_nb
*nb
= cpuhw
->amd_nb
;
550 if (nb
->nb_id
== -1 || --nb
->refcnt
== 0)
553 cpuhw
->amd_nb
= NULL
;
558 * When a PMC counter overflows, an NMI is used to process the event and
559 * reset the counter. NMI latency can result in the counter being updated
560 * before the NMI can run, which can result in what appear to be spurious
561 * NMIs. This function is intended to wait for the NMI to run and reset
562 * the counter to avoid possible unhandled NMI messages.
564 #define OVERFLOW_WAIT_COUNT 50
566 static void amd_pmu_wait_on_overflow(int idx
)
572 * Wait for the counter to be reset if it has overflowed. This loop
573 * should exit very, very quickly, but just in case, don't wait
576 for (i
= 0; i
< OVERFLOW_WAIT_COUNT
; i
++) {
577 rdmsrl(x86_pmu_event_addr(idx
), counter
);
578 if (counter
& (1ULL << (x86_pmu
.cntval_bits
- 1)))
581 /* Might be in IRQ context, so can't sleep */
586 static void amd_pmu_disable_all(void)
588 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
591 x86_pmu_disable_all();
594 * This shouldn't be called from NMI context, but add a safeguard here
595 * to return, since if we're in NMI context we can't wait for an NMI
596 * to reset an overflowed counter value.
602 * Check each counter for overflow and wait for it to be reset by the
603 * NMI if it has overflowed. This relies on the fact that all active
604 * counters are always enabled when this function is caled and
605 * ARCH_PERFMON_EVENTSEL_INT is always set.
607 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
608 if (!test_bit(idx
, cpuc
->active_mask
))
611 amd_pmu_wait_on_overflow(idx
);
615 static void amd_pmu_disable_event(struct perf_event
*event
)
617 x86_pmu_disable_event(event
);
620 * This can be called from NMI context (via x86_pmu_stop). The counter
621 * may have overflowed, but either way, we'll never see it get reset
622 * by the NMI if we're already in the NMI. And the NMI latency support
623 * below will take care of any pending NMI that might have been
624 * generated by the overflow.
629 amd_pmu_wait_on_overflow(event
->hw
.idx
);
633 * Because of NMI latency, if multiple PMC counters are active or other sources
634 * of NMIs are received, the perf NMI handler can handle one or more overflowed
635 * PMC counters outside of the NMI associated with the PMC overflow. If the NMI
636 * doesn't arrive at the LAPIC in time to become a pending NMI, then the kernel
637 * back-to-back NMI support won't be active. This PMC handler needs to take into
638 * account that this can occur, otherwise this could result in unknown NMI
639 * messages being issued. Examples of this is PMC overflow while in the NMI
640 * handler when multiple PMCs are active or PMC overflow while handling some
641 * other source of an NMI.
643 * Attempt to mitigate this by using the number of active PMCs to determine
644 * whether to return NMI_HANDLED if the perf NMI handler did not handle/reset
645 * any PMCs. The per-CPU perf_nmi_counter variable is set to a minimum of the
646 * number of active PMCs or 2. The value of 2 is used in case an NMI does not
647 * arrive at the LAPIC in time to be collapsed into an already pending NMI.
649 static int amd_pmu_handle_irq(struct pt_regs
*regs
)
651 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
655 * Obtain the active count before calling x86_pmu_handle_irq() since
656 * it is possible that x86_pmu_handle_irq() may make a counter
657 * inactive (through x86_pmu_stop).
659 active
= __bitmap_weight(cpuc
->active_mask
, X86_PMC_IDX_MAX
);
661 /* Process any counter overflows */
662 handled
= x86_pmu_handle_irq(regs
);
665 * If a counter was handled, record the number of possible remaining
666 * NMIs that can occur.
669 this_cpu_write(perf_nmi_counter
,
670 min_t(unsigned int, 2, active
));
675 if (!this_cpu_read(perf_nmi_counter
))
678 this_cpu_dec(perf_nmi_counter
);
683 static struct event_constraint
*
684 amd_get_event_constraints(struct cpu_hw_events
*cpuc
, int idx
,
685 struct perf_event
*event
)
688 * if not NB event or no NB, then no constraints
690 if (!(amd_has_nb(cpuc
) && amd_is_nb_event(&event
->hw
)))
691 return &unconstrained
;
693 return __amd_get_nb_event_constraints(cpuc
, event
, NULL
);
696 static void amd_put_event_constraints(struct cpu_hw_events
*cpuc
,
697 struct perf_event
*event
)
699 if (amd_has_nb(cpuc
) && amd_is_nb_event(&event
->hw
))
700 __amd_put_nb_event_constraints(cpuc
, event
);
703 PMU_FORMAT_ATTR(event
, "config:0-7,32-35");
704 PMU_FORMAT_ATTR(umask
, "config:8-15" );
705 PMU_FORMAT_ATTR(edge
, "config:18" );
706 PMU_FORMAT_ATTR(inv
, "config:23" );
707 PMU_FORMAT_ATTR(cmask
, "config:24-31" );
709 static struct attribute
*amd_format_attr
[] = {
710 &format_attr_event
.attr
,
711 &format_attr_umask
.attr
,
712 &format_attr_edge
.attr
,
713 &format_attr_inv
.attr
,
714 &format_attr_cmask
.attr
,
720 #define AMD_EVENT_TYPE_MASK 0x000000F0ULL
722 #define AMD_EVENT_FP 0x00000000ULL ... 0x00000010ULL
723 #define AMD_EVENT_LS 0x00000020ULL ... 0x00000030ULL
724 #define AMD_EVENT_DC 0x00000040ULL ... 0x00000050ULL
725 #define AMD_EVENT_CU 0x00000060ULL ... 0x00000070ULL
726 #define AMD_EVENT_IC_DE 0x00000080ULL ... 0x00000090ULL
727 #define AMD_EVENT_EX_LS 0x000000C0ULL
728 #define AMD_EVENT_DE 0x000000D0ULL
729 #define AMD_EVENT_NB 0x000000E0ULL ... 0x000000F0ULL
732 * AMD family 15h event code/PMC mappings:
734 * type = event_code & 0x0F0:
736 * 0x000 FP PERF_CTL[5:3]
737 * 0x010 FP PERF_CTL[5:3]
738 * 0x020 LS PERF_CTL[5:0]
739 * 0x030 LS PERF_CTL[5:0]
740 * 0x040 DC PERF_CTL[5:0]
741 * 0x050 DC PERF_CTL[5:0]
742 * 0x060 CU PERF_CTL[2:0]
743 * 0x070 CU PERF_CTL[2:0]
744 * 0x080 IC/DE PERF_CTL[2:0]
745 * 0x090 IC/DE PERF_CTL[2:0]
748 * 0x0C0 EX/LS PERF_CTL[5:0]
749 * 0x0D0 DE PERF_CTL[2:0]
750 * 0x0E0 NB NB_PERF_CTL[3:0]
751 * 0x0F0 NB NB_PERF_CTL[3:0]
755 * 0x000 FP PERF_CTL[3], PERF_CTL[5:3] (*)
756 * 0x003 FP PERF_CTL[3]
757 * 0x004 FP PERF_CTL[3], PERF_CTL[5:3] (*)
758 * 0x00B FP PERF_CTL[3]
759 * 0x00D FP PERF_CTL[3]
760 * 0x023 DE PERF_CTL[2:0]
761 * 0x02D LS PERF_CTL[3]
762 * 0x02E LS PERF_CTL[3,0]
763 * 0x031 LS PERF_CTL[2:0] (**)
764 * 0x043 CU PERF_CTL[2:0]
765 * 0x045 CU PERF_CTL[2:0]
766 * 0x046 CU PERF_CTL[2:0]
767 * 0x054 CU PERF_CTL[2:0]
768 * 0x055 CU PERF_CTL[2:0]
769 * 0x08F IC PERF_CTL[0]
770 * 0x187 DE PERF_CTL[0]
771 * 0x188 DE PERF_CTL[0]
772 * 0x0DB EX PERF_CTL[5:0]
773 * 0x0DC LS PERF_CTL[5:0]
774 * 0x0DD LS PERF_CTL[5:0]
775 * 0x0DE LS PERF_CTL[5:0]
776 * 0x0DF LS PERF_CTL[5:0]
777 * 0x1C0 EX PERF_CTL[5:3]
778 * 0x1D6 EX PERF_CTL[5:0]
779 * 0x1D8 EX PERF_CTL[5:0]
781 * (*) depending on the umask all FPU counters may be used
782 * (**) only one unitmask enabled at a time
785 static struct event_constraint amd_f15_PMC0
= EVENT_CONSTRAINT(0, 0x01, 0);
786 static struct event_constraint amd_f15_PMC20
= EVENT_CONSTRAINT(0, 0x07, 0);
787 static struct event_constraint amd_f15_PMC3
= EVENT_CONSTRAINT(0, 0x08, 0);
788 static struct event_constraint amd_f15_PMC30
= EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
789 static struct event_constraint amd_f15_PMC50
= EVENT_CONSTRAINT(0, 0x3F, 0);
790 static struct event_constraint amd_f15_PMC53
= EVENT_CONSTRAINT(0, 0x38, 0);
792 static struct event_constraint
*
793 amd_get_event_constraints_f15h(struct cpu_hw_events
*cpuc
, int idx
,
794 struct perf_event
*event
)
796 struct hw_perf_event
*hwc
= &event
->hw
;
797 unsigned int event_code
= amd_get_event_code(hwc
);
799 switch (event_code
& AMD_EVENT_TYPE_MASK
) {
801 switch (event_code
) {
803 if (!(hwc
->config
& 0x0000F000ULL
))
805 if (!(hwc
->config
& 0x00000F00ULL
))
807 return &amd_f15_PMC3
;
809 if (hweight_long(hwc
->config
& ARCH_PERFMON_EVENTSEL_UMASK
) <= 1)
811 return &amd_f15_PMC3
;
815 return &amd_f15_PMC3
;
817 return &amd_f15_PMC53
;
820 case AMD_EVENT_EX_LS
:
821 switch (event_code
) {
828 return &amd_f15_PMC20
;
830 return &amd_f15_PMC3
;
832 return &amd_f15_PMC30
;
834 if (hweight_long(hwc
->config
& ARCH_PERFMON_EVENTSEL_UMASK
) <= 1)
835 return &amd_f15_PMC20
;
836 return &emptyconstraint
;
838 return &amd_f15_PMC53
;
840 return &amd_f15_PMC50
;
843 case AMD_EVENT_IC_DE
:
845 switch (event_code
) {
849 return &amd_f15_PMC0
;
850 case 0x0DB ... 0x0DF:
853 return &amd_f15_PMC50
;
855 return &amd_f15_PMC20
;
858 /* moved to uncore.c */
859 return &emptyconstraint
;
861 return &emptyconstraint
;
865 static ssize_t
amd_event_sysfs_show(char *page
, u64 config
)
867 u64 event
= (config
& ARCH_PERFMON_EVENTSEL_EVENT
) |
868 (config
& AMD64_EVENTSEL_EVENT
) >> 24;
870 return x86_event_sysfs_show(page
, config
, event
);
873 static __initconst
const struct x86_pmu amd_pmu
= {
875 .handle_irq
= amd_pmu_handle_irq
,
876 .disable_all
= amd_pmu_disable_all
,
877 .enable_all
= x86_pmu_enable_all
,
878 .enable
= x86_pmu_enable_event
,
879 .disable
= amd_pmu_disable_event
,
880 .hw_config
= amd_pmu_hw_config
,
881 .schedule_events
= x86_schedule_events
,
882 .eventsel
= MSR_K7_EVNTSEL0
,
883 .perfctr
= MSR_K7_PERFCTR0
,
884 .addr_offset
= amd_pmu_addr_offset
,
885 .event_map
= amd_pmu_event_map
,
886 .max_events
= ARRAY_SIZE(amd_perfmon_event_map
),
887 .num_counters
= AMD64_NUM_COUNTERS
,
889 .cntval_mask
= (1ULL << 48) - 1,
891 /* use highest bit to detect overflow */
892 .max_period
= (1ULL << 47) - 1,
893 .get_event_constraints
= amd_get_event_constraints
,
894 .put_event_constraints
= amd_put_event_constraints
,
896 .format_attrs
= amd_format_attr
,
897 .events_sysfs_show
= amd_event_sysfs_show
,
899 .cpu_prepare
= amd_pmu_cpu_prepare
,
900 .cpu_starting
= amd_pmu_cpu_starting
,
901 .cpu_dead
= amd_pmu_cpu_dead
,
903 .amd_nb_constraints
= 1,
906 static int __init
amd_core_pmu_init(void)
908 if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE
))
911 switch (boot_cpu_data
.x86
) {
914 x86_pmu
.get_event_constraints
= amd_get_event_constraints_f15h
;
919 * In family 17h, there are no event constraints in the PMC hardware.
920 * We fallback to using default amd_get_event_constraints.
924 pr_err("core perfctr but no constraints; unknown hardware!\n");
929 * If core performance counter extensions exists, we must use
930 * MSR_F15H_PERF_CTL/MSR_F15H_PERF_CTR msrs. See also
931 * amd_pmu_addr_offset().
933 x86_pmu
.eventsel
= MSR_F15H_PERF_CTL
;
934 x86_pmu
.perfctr
= MSR_F15H_PERF_CTR
;
935 x86_pmu
.num_counters
= AMD64_NUM_COUNTERS_CORE
;
937 * AMD Core perfctr has separate MSRs for the NB events, see
938 * the amd/uncore.c driver.
940 x86_pmu
.amd_nb_constraints
= 0;
942 pr_cont("core perfctr, ");
946 __init
int amd_pmu_init(void)
950 /* Performance-monitoring supported from K7 and later: */
951 if (boot_cpu_data
.x86
< 6)
956 ret
= amd_core_pmu_init();
960 if (num_possible_cpus() == 1) {
962 * No point in allocating data structures to serialize
963 * against other CPUs, when there is only the one CPU.
965 x86_pmu
.amd_nb_constraints
= 0;
968 if (boot_cpu_data
.x86
>= 0x17)
969 memcpy(hw_cache_event_ids
, amd_hw_cache_event_ids_f17h
, sizeof(hw_cache_event_ids
));
971 memcpy(hw_cache_event_ids
, amd_hw_cache_event_ids
, sizeof(hw_cache_event_ids
));
976 void amd_pmu_enable_virt(void)
978 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
980 cpuc
->perf_ctr_virt_mask
= 0;
982 /* Reload all events */
983 amd_pmu_disable_all();
984 x86_pmu_enable_all(0);
986 EXPORT_SYMBOL_GPL(amd_pmu_enable_virt
);
988 void amd_pmu_disable_virt(void)
990 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
993 * We only mask out the Host-only bit so that host-only counting works
994 * when SVM is disabled. If someone sets up a guest-only counter when
995 * SVM is disabled the Guest-only bits still gets set and the counter
996 * will not count anything.
998 cpuc
->perf_ctr_virt_mask
= AMD64_EVENTSEL_HOSTONLY
;
1000 /* Reload all events */
1001 amd_pmu_disable_all();
1002 x86_pmu_enable_all(0);
1004 EXPORT_SYMBOL_GPL(amd_pmu_disable_virt
);