1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/perf_event.h>
3 #include <linux/export.h>
4 #include <linux/types.h>
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/delay.h>
8 #include <linux/jiffies.h>
9 #include <asm/apicdef.h>
12 #include "../perf_event.h"
14 static DEFINE_PER_CPU(unsigned long, perf_nmi_tstamp);
15 static unsigned long perf_nmi_window;
17 /* AMD Event 0xFFF: Merge. Used with Large Increment per Cycle events */
18 #define AMD_MERGE_EVENT ((0xFULL << 32) | 0xFFULL)
19 #define AMD_MERGE_EVENT_ENABLE (AMD_MERGE_EVENT | ARCH_PERFMON_EVENTSEL_ENABLE)
21 static __initconst const u64 amd_hw_cache_event_ids
22 [PERF_COUNT_HW_CACHE_MAX]
23 [PERF_COUNT_HW_CACHE_OP_MAX]
24 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
28 [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
29 [ C(RESULT_MISS) ] = 0x0141, /* Data Cache Misses */
32 [ C(RESULT_ACCESS) ] = 0,
33 [ C(RESULT_MISS) ] = 0,
35 [ C(OP_PREFETCH) ] = {
36 [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
37 [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
42 [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
43 [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
46 [ C(RESULT_ACCESS) ] = -1,
47 [ C(RESULT_MISS) ] = -1,
49 [ C(OP_PREFETCH) ] = {
50 [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
51 [ C(RESULT_MISS) ] = 0,
56 [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
57 [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
60 [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
61 [ C(RESULT_MISS) ] = 0,
63 [ C(OP_PREFETCH) ] = {
64 [ C(RESULT_ACCESS) ] = 0,
65 [ C(RESULT_MISS) ] = 0,
70 [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
71 [ C(RESULT_MISS) ] = 0x0746, /* L1_DTLB_AND_L2_DLTB_MISS.ALL */
74 [ C(RESULT_ACCESS) ] = 0,
75 [ C(RESULT_MISS) ] = 0,
77 [ C(OP_PREFETCH) ] = {
78 [ C(RESULT_ACCESS) ] = 0,
79 [ C(RESULT_MISS) ] = 0,
84 [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
85 [ C(RESULT_MISS) ] = 0x0385, /* L1_ITLB_AND_L2_ITLB_MISS.ALL */
88 [ C(RESULT_ACCESS) ] = -1,
89 [ C(RESULT_MISS) ] = -1,
91 [ C(OP_PREFETCH) ] = {
92 [ C(RESULT_ACCESS) ] = -1,
93 [ C(RESULT_MISS) ] = -1,
98 [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
99 [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
102 [ C(RESULT_ACCESS) ] = -1,
103 [ C(RESULT_MISS) ] = -1,
105 [ C(OP_PREFETCH) ] = {
106 [ C(RESULT_ACCESS) ] = -1,
107 [ C(RESULT_MISS) ] = -1,
112 [ C(RESULT_ACCESS) ] = 0xb8e9, /* CPU Request to Memory, l+r */
113 [ C(RESULT_MISS) ] = 0x98e9, /* CPU Request to Memory, r */
116 [ C(RESULT_ACCESS) ] = -1,
117 [ C(RESULT_MISS) ] = -1,
119 [ C(OP_PREFETCH) ] = {
120 [ C(RESULT_ACCESS) ] = -1,
121 [ C(RESULT_MISS) ] = -1,
126 static __initconst const u64 amd_hw_cache_event_ids_f17h
127 [PERF_COUNT_HW_CACHE_MAX]
128 [PERF_COUNT_HW_CACHE_OP_MAX]
129 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
132 [C(RESULT_ACCESS)] = 0x0040, /* Data Cache Accesses */
133 [C(RESULT_MISS)] = 0xc860, /* L2$ access from DC Miss */
136 [C(RESULT_ACCESS)] = 0,
137 [C(RESULT_MISS)] = 0,
140 [C(RESULT_ACCESS)] = 0xff5a, /* h/w prefetch DC Fills */
141 [C(RESULT_MISS)] = 0,
146 [C(RESULT_ACCESS)] = 0x0080, /* Instruction cache fetches */
147 [C(RESULT_MISS)] = 0x0081, /* Instruction cache misses */
150 [C(RESULT_ACCESS)] = -1,
151 [C(RESULT_MISS)] = -1,
154 [C(RESULT_ACCESS)] = 0,
155 [C(RESULT_MISS)] = 0,
160 [C(RESULT_ACCESS)] = 0,
161 [C(RESULT_MISS)] = 0,
164 [C(RESULT_ACCESS)] = 0,
165 [C(RESULT_MISS)] = 0,
168 [C(RESULT_ACCESS)] = 0,
169 [C(RESULT_MISS)] = 0,
174 [C(RESULT_ACCESS)] = 0xff45, /* All L2 DTLB accesses */
175 [C(RESULT_MISS)] = 0xf045, /* L2 DTLB misses (PT walks) */
178 [C(RESULT_ACCESS)] = 0,
179 [C(RESULT_MISS)] = 0,
182 [C(RESULT_ACCESS)] = 0,
183 [C(RESULT_MISS)] = 0,
188 [C(RESULT_ACCESS)] = 0x0084, /* L1 ITLB misses, L2 ITLB hits */
189 [C(RESULT_MISS)] = 0xff85, /* L1 ITLB misses, L2 misses */
192 [C(RESULT_ACCESS)] = -1,
193 [C(RESULT_MISS)] = -1,
196 [C(RESULT_ACCESS)] = -1,
197 [C(RESULT_MISS)] = -1,
202 [C(RESULT_ACCESS)] = 0x00c2, /* Retired Branch Instr. */
203 [C(RESULT_MISS)] = 0x00c3, /* Retired Mispredicted BI */
206 [C(RESULT_ACCESS)] = -1,
207 [C(RESULT_MISS)] = -1,
210 [C(RESULT_ACCESS)] = -1,
211 [C(RESULT_MISS)] = -1,
216 [C(RESULT_ACCESS)] = 0,
217 [C(RESULT_MISS)] = 0,
220 [C(RESULT_ACCESS)] = -1,
221 [C(RESULT_MISS)] = -1,
224 [C(RESULT_ACCESS)] = -1,
225 [C(RESULT_MISS)] = -1,
231 * AMD Performance Monitor K7 and later, up to and including Family 16h:
233 static const u64 amd_perfmon_event_map[PERF_COUNT_HW_MAX] =
235 [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
236 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
237 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x077d,
238 [PERF_COUNT_HW_CACHE_MISSES] = 0x077e,
239 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c2,
240 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c3,
241 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00d0, /* "Decoder empty" event */
242 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 0x00d1, /* "Dispatch stalls" event */
246 * AMD Performance Monitor Family 17h and later:
248 static const u64 amd_f17h_perfmon_event_map[PERF_COUNT_HW_MAX] =
250 [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
251 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
252 [PERF_COUNT_HW_CACHE_REFERENCES] = 0xff60,
253 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c2,
254 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c3,
255 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x0287,
256 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 0x0187,
259 static u64 amd_pmu_event_map(int hw_event)
261 if (boot_cpu_data.x86 >= 0x17)
262 return amd_f17h_perfmon_event_map[hw_event];
264 return amd_perfmon_event_map[hw_event];
268 * Previously calculated offsets
270 static unsigned int event_offsets[X86_PMC_IDX_MAX] __read_mostly;
271 static unsigned int count_offsets[X86_PMC_IDX_MAX] __read_mostly;
275 * 4 counters starting at 0xc0010000 each offset by 1
277 * CPUs with core performance counter extensions:
278 * 6 counters starting at 0xc0010200 each offset by 2
280 static inline int amd_pmu_addr_offset(int index, bool eventsel)
288 offset = event_offsets[index];
290 offset = count_offsets[index];
295 if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
301 event_offsets[index] = offset;
303 count_offsets[index] = offset;
309 * AMD64 events are detected based on their event codes.
311 static inline unsigned int amd_get_event_code(struct hw_perf_event *hwc)
313 return ((hwc->config >> 24) & 0x0f00) | (hwc->config & 0x00ff);
316 static inline bool amd_is_pair_event_code(struct hw_perf_event *hwc)
318 if (!(x86_pmu.flags & PMU_FL_PAIR))
321 switch (amd_get_event_code(hwc)) {
322 case 0x003: return true; /* Retired SSE/AVX FLOPs */
323 default: return false;
327 static int amd_core_hw_config(struct perf_event *event)
329 if (event->attr.exclude_host && event->attr.exclude_guest)
331 * When HO == GO == 1 the hardware treats that as GO == HO == 0
332 * and will count in both modes. We don't want to count in that
333 * case so we emulate no-counting by setting US = OS = 0.
335 event->hw.config &= ~(ARCH_PERFMON_EVENTSEL_USR |
336 ARCH_PERFMON_EVENTSEL_OS);
337 else if (event->attr.exclude_host)
338 event->hw.config |= AMD64_EVENTSEL_GUESTONLY;
339 else if (event->attr.exclude_guest)
340 event->hw.config |= AMD64_EVENTSEL_HOSTONLY;
342 if ((x86_pmu.flags & PMU_FL_PAIR) && amd_is_pair_event_code(&event->hw))
343 event->hw.flags |= PERF_X86_EVENT_PAIR;
348 static inline int amd_is_nb_event(struct hw_perf_event *hwc)
350 return (hwc->config & 0xe0) == 0xe0;
353 static inline int amd_has_nb(struct cpu_hw_events *cpuc)
355 struct amd_nb *nb = cpuc->amd_nb;
357 return nb && nb->nb_id != -1;
360 static int amd_pmu_hw_config(struct perf_event *event)
364 /* pass precise event sampling to ibs: */
365 if (event->attr.precise_ip && get_ibs_caps())
368 if (has_branch_stack(event))
371 ret = x86_pmu_hw_config(event);
375 if (event->attr.type == PERF_TYPE_RAW)
376 event->hw.config |= event->attr.config & AMD64_RAW_EVENT_MASK;
378 return amd_core_hw_config(event);
381 static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
382 struct perf_event *event)
384 struct amd_nb *nb = cpuc->amd_nb;
388 * need to scan whole list because event may not have
389 * been assigned during scheduling
391 * no race condition possible because event can only
392 * be removed on one CPU at a time AND PMU is disabled
395 for (i = 0; i < x86_pmu.num_counters; i++) {
396 if (cmpxchg(nb->owners + i, event, NULL) == event)
402 * AMD64 NorthBridge events need special treatment because
403 * counter access needs to be synchronized across all cores
404 * of a package. Refer to BKDG section 3.12
406 * NB events are events measuring L3 cache, Hypertransport
407 * traffic. They are identified by an event code >= 0xe00.
408 * They measure events on the NorthBride which is shared
409 * by all cores on a package. NB events are counted on a
410 * shared set of counters. When a NB event is programmed
411 * in a counter, the data actually comes from a shared
412 * counter. Thus, access to those counters needs to be
415 * We implement the synchronization such that no two cores
416 * can be measuring NB events using the same counters. Thus,
417 * we maintain a per-NB allocation table. The available slot
418 * is propagated using the event_constraint structure.
420 * We provide only one choice for each NB event based on
421 * the fact that only NB events have restrictions. Consequently,
422 * if a counter is available, there is a guarantee the NB event
423 * will be assigned to it. If no slot is available, an empty
424 * constraint is returned and scheduling will eventually fail
427 * Note that all cores attached the same NB compete for the same
428 * counters to host NB events, this is why we use atomic ops. Some
429 * multi-chip CPUs may have more than one NB.
431 * Given that resources are allocated (cmpxchg), they must be
432 * eventually freed for others to use. This is accomplished by
433 * calling __amd_put_nb_event_constraints()
435 * Non NB events are not impacted by this restriction.
437 static struct event_constraint *
438 __amd_get_nb_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
439 struct event_constraint *c)
441 struct hw_perf_event *hwc = &event->hw;
442 struct amd_nb *nb = cpuc->amd_nb;
443 struct perf_event *old;
453 * detect if already present, if so reuse
455 * cannot merge with actual allocation
456 * because of possible holes
458 * event can already be present yet not assigned (in hwc->idx)
459 * because of successive calls to x86_schedule_events() from
460 * hw_perf_group_sched_in() without hw_perf_enable()
462 for_each_set_bit(idx, c->idxmsk, x86_pmu.num_counters) {
463 if (new == -1 || hwc->idx == idx)
464 /* assign free slot, prefer hwc->idx */
465 old = cmpxchg(nb->owners + idx, NULL, event);
466 else if (nb->owners[idx] == event)
467 /* event already present */
472 if (old && old != event)
475 /* reassign to this slot */
477 cmpxchg(nb->owners + new, event, NULL);
480 /* already present, reuse */
486 return &emptyconstraint;
488 return &nb->event_constraints[new];
491 static struct amd_nb *amd_alloc_nb(int cpu)
496 nb = kzalloc_node(sizeof(struct amd_nb), GFP_KERNEL, cpu_to_node(cpu));
503 * initialize all possible NB constraints
505 for (i = 0; i < x86_pmu.num_counters; i++) {
506 __set_bit(i, nb->event_constraints[i].idxmsk);
507 nb->event_constraints[i].weight = 1;
512 static int amd_pmu_cpu_prepare(int cpu)
514 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
516 WARN_ON_ONCE(cpuc->amd_nb);
518 if (!x86_pmu.amd_nb_constraints)
521 cpuc->amd_nb = amd_alloc_nb(cpu);
528 static void amd_pmu_cpu_starting(int cpu)
530 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
531 void **onln = &cpuc->kfree_on_online[X86_PERF_KFREE_SHARED];
535 cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
537 if (!x86_pmu.amd_nb_constraints)
540 nb_id = amd_get_nb_id(cpu);
541 WARN_ON_ONCE(nb_id == BAD_APICID);
543 for_each_online_cpu(i) {
544 nb = per_cpu(cpu_hw_events, i).amd_nb;
545 if (WARN_ON_ONCE(!nb))
548 if (nb->nb_id == nb_id) {
549 *onln = cpuc->amd_nb;
555 cpuc->amd_nb->nb_id = nb_id;
556 cpuc->amd_nb->refcnt++;
559 static void amd_pmu_cpu_dead(int cpu)
561 struct cpu_hw_events *cpuhw;
563 if (!x86_pmu.amd_nb_constraints)
566 cpuhw = &per_cpu(cpu_hw_events, cpu);
569 struct amd_nb *nb = cpuhw->amd_nb;
571 if (nb->nb_id == -1 || --nb->refcnt == 0)
574 cpuhw->amd_nb = NULL;
579 * When a PMC counter overflows, an NMI is used to process the event and
580 * reset the counter. NMI latency can result in the counter being updated
581 * before the NMI can run, which can result in what appear to be spurious
582 * NMIs. This function is intended to wait for the NMI to run and reset
583 * the counter to avoid possible unhandled NMI messages.
585 #define OVERFLOW_WAIT_COUNT 50
587 static void amd_pmu_wait_on_overflow(int idx)
593 * Wait for the counter to be reset if it has overflowed. This loop
594 * should exit very, very quickly, but just in case, don't wait
597 for (i = 0; i < OVERFLOW_WAIT_COUNT; i++) {
598 rdmsrl(x86_pmu_event_addr(idx), counter);
599 if (counter & (1ULL << (x86_pmu.cntval_bits - 1)))
602 /* Might be in IRQ context, so can't sleep */
607 static void amd_pmu_disable_all(void)
609 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
612 x86_pmu_disable_all();
615 * This shouldn't be called from NMI context, but add a safeguard here
616 * to return, since if we're in NMI context we can't wait for an NMI
617 * to reset an overflowed counter value.
623 * Check each counter for overflow and wait for it to be reset by the
624 * NMI if it has overflowed. This relies on the fact that all active
625 * counters are always enabled when this function is caled and
626 * ARCH_PERFMON_EVENTSEL_INT is always set.
628 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
629 if (!test_bit(idx, cpuc->active_mask))
632 amd_pmu_wait_on_overflow(idx);
636 static void amd_pmu_disable_event(struct perf_event *event)
638 x86_pmu_disable_event(event);
641 * This can be called from NMI context (via x86_pmu_stop). The counter
642 * may have overflowed, but either way, we'll never see it get reset
643 * by the NMI if we're already in the NMI. And the NMI latency support
644 * below will take care of any pending NMI that might have been
645 * generated by the overflow.
650 amd_pmu_wait_on_overflow(event->hw.idx);
654 * Because of NMI latency, if multiple PMC counters are active or other sources
655 * of NMIs are received, the perf NMI handler can handle one or more overflowed
656 * PMC counters outside of the NMI associated with the PMC overflow. If the NMI
657 * doesn't arrive at the LAPIC in time to become a pending NMI, then the kernel
658 * back-to-back NMI support won't be active. This PMC handler needs to take into
659 * account that this can occur, otherwise this could result in unknown NMI
660 * messages being issued. Examples of this is PMC overflow while in the NMI
661 * handler when multiple PMCs are active or PMC overflow while handling some
662 * other source of an NMI.
664 * Attempt to mitigate this by creating an NMI window in which un-handled NMIs
665 * received during this window will be claimed. This prevents extending the
666 * window past when it is possible that latent NMIs should be received. The
667 * per-CPU perf_nmi_tstamp will be set to the window end time whenever perf has
668 * handled a counter. When an un-handled NMI is received, it will be claimed
669 * only if arriving within that window.
671 static int amd_pmu_handle_irq(struct pt_regs *regs)
675 /* Process any counter overflows */
676 handled = x86_pmu_handle_irq(regs);
679 * If a counter was handled, record a timestamp such that un-handled
680 * NMIs will be claimed if arriving within that window.
683 this_cpu_write(perf_nmi_tstamp, jiffies + perf_nmi_window);
688 if (time_after(jiffies, this_cpu_read(perf_nmi_tstamp)))
694 static struct event_constraint *
695 amd_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
696 struct perf_event *event)
699 * if not NB event or no NB, then no constraints
701 if (!(amd_has_nb(cpuc) && amd_is_nb_event(&event->hw)))
702 return &unconstrained;
704 return __amd_get_nb_event_constraints(cpuc, event, NULL);
707 static void amd_put_event_constraints(struct cpu_hw_events *cpuc,
708 struct perf_event *event)
710 if (amd_has_nb(cpuc) && amd_is_nb_event(&event->hw))
711 __amd_put_nb_event_constraints(cpuc, event);
714 PMU_FORMAT_ATTR(event, "config:0-7,32-35");
715 PMU_FORMAT_ATTR(umask, "config:8-15" );
716 PMU_FORMAT_ATTR(edge, "config:18" );
717 PMU_FORMAT_ATTR(inv, "config:23" );
718 PMU_FORMAT_ATTR(cmask, "config:24-31" );
720 static struct attribute *amd_format_attr[] = {
721 &format_attr_event.attr,
722 &format_attr_umask.attr,
723 &format_attr_edge.attr,
724 &format_attr_inv.attr,
725 &format_attr_cmask.attr,
731 #define AMD_EVENT_TYPE_MASK 0x000000F0ULL
733 #define AMD_EVENT_FP 0x00000000ULL ... 0x00000010ULL
734 #define AMD_EVENT_LS 0x00000020ULL ... 0x00000030ULL
735 #define AMD_EVENT_DC 0x00000040ULL ... 0x00000050ULL
736 #define AMD_EVENT_CU 0x00000060ULL ... 0x00000070ULL
737 #define AMD_EVENT_IC_DE 0x00000080ULL ... 0x00000090ULL
738 #define AMD_EVENT_EX_LS 0x000000C0ULL
739 #define AMD_EVENT_DE 0x000000D0ULL
740 #define AMD_EVENT_NB 0x000000E0ULL ... 0x000000F0ULL
743 * AMD family 15h event code/PMC mappings:
745 * type = event_code & 0x0F0:
747 * 0x000 FP PERF_CTL[5:3]
748 * 0x010 FP PERF_CTL[5:3]
749 * 0x020 LS PERF_CTL[5:0]
750 * 0x030 LS PERF_CTL[5:0]
751 * 0x040 DC PERF_CTL[5:0]
752 * 0x050 DC PERF_CTL[5:0]
753 * 0x060 CU PERF_CTL[2:0]
754 * 0x070 CU PERF_CTL[2:0]
755 * 0x080 IC/DE PERF_CTL[2:0]
756 * 0x090 IC/DE PERF_CTL[2:0]
759 * 0x0C0 EX/LS PERF_CTL[5:0]
760 * 0x0D0 DE PERF_CTL[2:0]
761 * 0x0E0 NB NB_PERF_CTL[3:0]
762 * 0x0F0 NB NB_PERF_CTL[3:0]
766 * 0x000 FP PERF_CTL[3], PERF_CTL[5:3] (*)
767 * 0x003 FP PERF_CTL[3]
768 * 0x004 FP PERF_CTL[3], PERF_CTL[5:3] (*)
769 * 0x00B FP PERF_CTL[3]
770 * 0x00D FP PERF_CTL[3]
771 * 0x023 DE PERF_CTL[2:0]
772 * 0x02D LS PERF_CTL[3]
773 * 0x02E LS PERF_CTL[3,0]
774 * 0x031 LS PERF_CTL[2:0] (**)
775 * 0x043 CU PERF_CTL[2:0]
776 * 0x045 CU PERF_CTL[2:0]
777 * 0x046 CU PERF_CTL[2:0]
778 * 0x054 CU PERF_CTL[2:0]
779 * 0x055 CU PERF_CTL[2:0]
780 * 0x08F IC PERF_CTL[0]
781 * 0x187 DE PERF_CTL[0]
782 * 0x188 DE PERF_CTL[0]
783 * 0x0DB EX PERF_CTL[5:0]
784 * 0x0DC LS PERF_CTL[5:0]
785 * 0x0DD LS PERF_CTL[5:0]
786 * 0x0DE LS PERF_CTL[5:0]
787 * 0x0DF LS PERF_CTL[5:0]
788 * 0x1C0 EX PERF_CTL[5:3]
789 * 0x1D6 EX PERF_CTL[5:0]
790 * 0x1D8 EX PERF_CTL[5:0]
792 * (*) depending on the umask all FPU counters may be used
793 * (**) only one unitmask enabled at a time
796 static struct event_constraint amd_f15_PMC0 = EVENT_CONSTRAINT(0, 0x01, 0);
797 static struct event_constraint amd_f15_PMC20 = EVENT_CONSTRAINT(0, 0x07, 0);
798 static struct event_constraint amd_f15_PMC3 = EVENT_CONSTRAINT(0, 0x08, 0);
799 static struct event_constraint amd_f15_PMC30 = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
800 static struct event_constraint amd_f15_PMC50 = EVENT_CONSTRAINT(0, 0x3F, 0);
801 static struct event_constraint amd_f15_PMC53 = EVENT_CONSTRAINT(0, 0x38, 0);
803 static struct event_constraint *
804 amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, int idx,
805 struct perf_event *event)
807 struct hw_perf_event *hwc = &event->hw;
808 unsigned int event_code = amd_get_event_code(hwc);
810 switch (event_code & AMD_EVENT_TYPE_MASK) {
812 switch (event_code) {
814 if (!(hwc->config & 0x0000F000ULL))
816 if (!(hwc->config & 0x00000F00ULL))
818 return &amd_f15_PMC3;
820 if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
822 return &amd_f15_PMC3;
826 return &amd_f15_PMC3;
828 return &amd_f15_PMC53;
831 case AMD_EVENT_EX_LS:
832 switch (event_code) {
839 return &amd_f15_PMC20;
841 return &amd_f15_PMC3;
843 return &amd_f15_PMC30;
845 if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
846 return &amd_f15_PMC20;
847 return &emptyconstraint;
849 return &amd_f15_PMC53;
851 return &amd_f15_PMC50;
854 case AMD_EVENT_IC_DE:
856 switch (event_code) {
860 return &amd_f15_PMC0;
861 case 0x0DB ... 0x0DF:
864 return &amd_f15_PMC50;
866 return &amd_f15_PMC20;
869 /* moved to uncore.c */
870 return &emptyconstraint;
872 return &emptyconstraint;
876 static struct event_constraint pair_constraint;
878 static struct event_constraint *
879 amd_get_event_constraints_f17h(struct cpu_hw_events *cpuc, int idx,
880 struct perf_event *event)
882 struct hw_perf_event *hwc = &event->hw;
884 if (amd_is_pair_event_code(hwc))
885 return &pair_constraint;
887 return &unconstrained;
890 static void amd_put_event_constraints_f17h(struct cpu_hw_events *cpuc,
891 struct perf_event *event)
893 struct hw_perf_event *hwc = &event->hw;
895 if (is_counter_pair(hwc))
899 static ssize_t amd_event_sysfs_show(char *page, u64 config)
901 u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT) |
902 (config & AMD64_EVENTSEL_EVENT) >> 24;
904 return x86_event_sysfs_show(page, config, event);
907 static __initconst const struct x86_pmu amd_pmu = {
909 .handle_irq = amd_pmu_handle_irq,
910 .disable_all = amd_pmu_disable_all,
911 .enable_all = x86_pmu_enable_all,
912 .enable = x86_pmu_enable_event,
913 .disable = amd_pmu_disable_event,
914 .hw_config = amd_pmu_hw_config,
915 .schedule_events = x86_schedule_events,
916 .eventsel = MSR_K7_EVNTSEL0,
917 .perfctr = MSR_K7_PERFCTR0,
918 .addr_offset = amd_pmu_addr_offset,
919 .event_map = amd_pmu_event_map,
920 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
921 .num_counters = AMD64_NUM_COUNTERS,
923 .cntval_mask = (1ULL << 48) - 1,
925 /* use highest bit to detect overflow */
926 .max_period = (1ULL << 47) - 1,
927 .get_event_constraints = amd_get_event_constraints,
928 .put_event_constraints = amd_put_event_constraints,
930 .format_attrs = amd_format_attr,
931 .events_sysfs_show = amd_event_sysfs_show,
933 .cpu_prepare = amd_pmu_cpu_prepare,
934 .cpu_starting = amd_pmu_cpu_starting,
935 .cpu_dead = amd_pmu_cpu_dead,
937 .amd_nb_constraints = 1,
940 static int __init amd_core_pmu_init(void)
942 u64 even_ctr_mask = 0ULL;
945 if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
948 /* Avoid calculating the value each time in the NMI handler */
949 perf_nmi_window = msecs_to_jiffies(100);
952 * If core performance counter extensions exists, we must use
953 * MSR_F15H_PERF_CTL/MSR_F15H_PERF_CTR msrs. See also
954 * amd_pmu_addr_offset().
956 x86_pmu.eventsel = MSR_F15H_PERF_CTL;
957 x86_pmu.perfctr = MSR_F15H_PERF_CTR;
958 x86_pmu.num_counters = AMD64_NUM_COUNTERS_CORE;
960 * AMD Core perfctr has separate MSRs for the NB events, see
961 * the amd/uncore.c driver.
963 x86_pmu.amd_nb_constraints = 0;
965 if (boot_cpu_data.x86 == 0x15) {
967 x86_pmu.get_event_constraints = amd_get_event_constraints_f15h;
969 if (boot_cpu_data.x86 >= 0x17) {
972 * Family 17h and compatibles have constraints for Large
973 * Increment per Cycle events: they may only be assigned an
974 * even numbered counter that has a consecutive adjacent odd
975 * numbered counter following it.
977 for (i = 0; i < x86_pmu.num_counters - 1; i += 2)
978 even_ctr_mask |= 1 << i;
980 pair_constraint = (struct event_constraint)
981 __EVENT_CONSTRAINT(0, even_ctr_mask, 0,
982 x86_pmu.num_counters / 2, 0,
983 PERF_X86_EVENT_PAIR);
985 x86_pmu.get_event_constraints = amd_get_event_constraints_f17h;
986 x86_pmu.put_event_constraints = amd_put_event_constraints_f17h;
987 x86_pmu.perf_ctr_pair_en = AMD_MERGE_EVENT_ENABLE;
988 x86_pmu.flags |= PMU_FL_PAIR;
991 pr_cont("core perfctr, ");
995 __init int amd_pmu_init(void)
999 /* Performance-monitoring supported from K7 and later: */
1000 if (boot_cpu_data.x86 < 6)
1005 ret = amd_core_pmu_init();
1009 if (num_possible_cpus() == 1) {
1011 * No point in allocating data structures to serialize
1012 * against other CPUs, when there is only the one CPU.
1014 x86_pmu.amd_nb_constraints = 0;
1017 if (boot_cpu_data.x86 >= 0x17)
1018 memcpy(hw_cache_event_ids, amd_hw_cache_event_ids_f17h, sizeof(hw_cache_event_ids));
1020 memcpy(hw_cache_event_ids, amd_hw_cache_event_ids, sizeof(hw_cache_event_ids));
1025 void amd_pmu_enable_virt(void)
1027 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1029 cpuc->perf_ctr_virt_mask = 0;
1031 /* Reload all events */
1032 amd_pmu_disable_all();
1033 x86_pmu_enable_all(0);
1035 EXPORT_SYMBOL_GPL(amd_pmu_enable_virt);
1037 void amd_pmu_disable_virt(void)
1039 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1042 * We only mask out the Host-only bit so that host-only counting works
1043 * when SVM is disabled. If someone sets up a guest-only counter when
1044 * SVM is disabled the Guest-only bits still gets set and the counter
1045 * will not count anything.
1047 cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
1049 /* Reload all events */
1050 amd_pmu_disable_all();
1051 x86_pmu_enable_all(0);
1053 EXPORT_SYMBOL_GPL(amd_pmu_disable_virt);