]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/x86/events/amd/core.c
Merge tag 'for-v5.1-rc' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux...
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / events / amd / core.c
CommitLineData
de0428a7 1#include <linux/perf_event.h>
1018faa6 2#include <linux/export.h>
de0428a7
KW
3#include <linux/types.h>
4#include <linux/init.h>
5#include <linux/slab.h>
914123fa 6#include <linux/delay.h>
d6eed550 7#include <asm/apicdef.h>
3966c3fe 8#include <asm/nmi.h>
de0428a7 9
27f6d22b 10#include "../perf_event.h"
f22f54f4 11
6d3edaae
LT
12static DEFINE_PER_CPU(unsigned int, perf_nmi_counter);
13
caaa8be3 14static __initconst const u64 amd_hw_cache_event_ids
f22f54f4
PZ
15 [PERF_COUNT_HW_CACHE_MAX]
16 [PERF_COUNT_HW_CACHE_OP_MAX]
17 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
18{
19 [ C(L1D) ] = {
20 [ C(OP_READ) ] = {
21 [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
83112e68 22 [ C(RESULT_MISS) ] = 0x0141, /* Data Cache Misses */
f22f54f4
PZ
23 },
24 [ C(OP_WRITE) ] = {
9cc2617d 25 [ C(RESULT_ACCESS) ] = 0,
f22f54f4
PZ
26 [ C(RESULT_MISS) ] = 0,
27 },
28 [ C(OP_PREFETCH) ] = {
29 [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
30 [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
31 },
32 },
33 [ C(L1I ) ] = {
34 [ C(OP_READ) ] = {
35 [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
36 [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
37 },
38 [ C(OP_WRITE) ] = {
39 [ C(RESULT_ACCESS) ] = -1,
40 [ C(RESULT_MISS) ] = -1,
41 },
42 [ C(OP_PREFETCH) ] = {
43 [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
44 [ C(RESULT_MISS) ] = 0,
45 },
46 },
47 [ C(LL ) ] = {
48 [ C(OP_READ) ] = {
49 [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
50 [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
51 },
52 [ C(OP_WRITE) ] = {
53 [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
54 [ C(RESULT_MISS) ] = 0,
55 },
56 [ C(OP_PREFETCH) ] = {
57 [ C(RESULT_ACCESS) ] = 0,
58 [ C(RESULT_MISS) ] = 0,
59 },
60 },
61 [ C(DTLB) ] = {
62 [ C(OP_READ) ] = {
63 [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
ba0cef3d 64 [ C(RESULT_MISS) ] = 0x0746, /* L1_DTLB_AND_L2_DLTB_MISS.ALL */
f22f54f4
PZ
65 },
66 [ C(OP_WRITE) ] = {
67 [ C(RESULT_ACCESS) ] = 0,
68 [ C(RESULT_MISS) ] = 0,
69 },
70 [ C(OP_PREFETCH) ] = {
71 [ C(RESULT_ACCESS) ] = 0,
72 [ C(RESULT_MISS) ] = 0,
73 },
74 },
75 [ C(ITLB) ] = {
76 [ C(OP_READ) ] = {
77 [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
ba0cef3d 78 [ C(RESULT_MISS) ] = 0x0385, /* L1_ITLB_AND_L2_ITLB_MISS.ALL */
f22f54f4
PZ
79 },
80 [ C(OP_WRITE) ] = {
81 [ C(RESULT_ACCESS) ] = -1,
82 [ C(RESULT_MISS) ] = -1,
83 },
84 [ C(OP_PREFETCH) ] = {
85 [ C(RESULT_ACCESS) ] = -1,
86 [ C(RESULT_MISS) ] = -1,
87 },
88 },
89 [ C(BPU ) ] = {
90 [ C(OP_READ) ] = {
91 [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
92 [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
93 },
94 [ C(OP_WRITE) ] = {
95 [ C(RESULT_ACCESS) ] = -1,
96 [ C(RESULT_MISS) ] = -1,
97 },
98 [ C(OP_PREFETCH) ] = {
99 [ C(RESULT_ACCESS) ] = -1,
100 [ C(RESULT_MISS) ] = -1,
101 },
102 },
89d6c0b5
PZ
103 [ C(NODE) ] = {
104 [ C(OP_READ) ] = {
105 [ C(RESULT_ACCESS) ] = 0xb8e9, /* CPU Request to Memory, l+r */
106 [ C(RESULT_MISS) ] = 0x98e9, /* CPU Request to Memory, r */
107 },
108 [ C(OP_WRITE) ] = {
109 [ C(RESULT_ACCESS) ] = -1,
110 [ C(RESULT_MISS) ] = -1,
111 },
112 [ C(OP_PREFETCH) ] = {
113 [ C(RESULT_ACCESS) ] = -1,
114 [ C(RESULT_MISS) ] = -1,
115 },
116 },
f22f54f4
PZ
117};
118
119/*
3fe3331b 120 * AMD Performance Monitor K7 and later, up to and including Family 16h:
f22f54f4 121 */
0a25556f 122static const u64 amd_perfmon_event_map[PERF_COUNT_HW_MAX] =
f22f54f4 123{
3fe3331b
KP
124 [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
125 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
126 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x077d,
127 [PERF_COUNT_HW_CACHE_MISSES] = 0x077e,
128 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c2,
129 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c3,
130 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00d0, /* "Decoder empty" event */
131 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 0x00d1, /* "Dispatch stalls" event */
132};
133
134/*
135 * AMD Performance Monitor Family 17h and later:
136 */
137static const u64 amd_f17h_perfmon_event_map[PERF_COUNT_HW_MAX] =
138{
139 [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
140 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
141 [PERF_COUNT_HW_CACHE_REFERENCES] = 0xff60,
142 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c2,
143 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c3,
144 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x0287,
145 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 0x0187,
f22f54f4
PZ
146};
147
148static u64 amd_pmu_event_map(int hw_event)
149{
3fe3331b
KP
150 if (boot_cpu_data.x86 >= 0x17)
151 return amd_f17h_perfmon_event_map[hw_event];
152
f22f54f4
PZ
153 return amd_perfmon_event_map[hw_event];
154}
155
4c1fd17a
JS
156/*
157 * Previously calculated offsets
158 */
159static unsigned int event_offsets[X86_PMC_IDX_MAX] __read_mostly;
160static unsigned int count_offsets[X86_PMC_IDX_MAX] __read_mostly;
161
162/*
163 * Legacy CPUs:
164 * 4 counters starting at 0xc0010000 each offset by 1
165 *
166 * CPUs with core performance counter extensions:
167 * 6 counters starting at 0xc0010200 each offset by 2
168 */
169static inline int amd_pmu_addr_offset(int index, bool eventsel)
170{
0cf5f432 171 int offset;
4c1fd17a
JS
172
173 if (!index)
174 return index;
175
176 if (eventsel)
177 offset = event_offsets[index];
178 else
179 offset = count_offsets[index];
180
181 if (offset)
182 return offset;
183
362f924b 184 if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
4c1fd17a
JS
185 offset = index;
186 else
187 offset = index << 1;
188
189 if (eventsel)
190 event_offsets[index] = offset;
191 else
192 count_offsets[index] = offset;
193
194 return offset;
195}
196
e259514e
JS
197static int amd_core_hw_config(struct perf_event *event)
198{
011af857
JR
199 if (event->attr.exclude_host && event->attr.exclude_guest)
200 /*
201 * When HO == GO == 1 the hardware treats that as GO == HO == 0
202 * and will count in both modes. We don't want to count in that
203 * case so we emulate no-counting by setting US = OS = 0.
204 */
205 event->hw.config &= ~(ARCH_PERFMON_EVENTSEL_USR |
206 ARCH_PERFMON_EVENTSEL_OS);
207 else if (event->attr.exclude_host)
9f19010a 208 event->hw.config |= AMD64_EVENTSEL_GUESTONLY;
011af857 209 else if (event->attr.exclude_guest)
9f19010a 210 event->hw.config |= AMD64_EVENTSEL_HOSTONLY;
011af857 211
e259514e
JS
212 return 0;
213}
b4cdc5c2 214
f22f54f4
PZ
215/*
216 * AMD64 events are detected based on their event codes.
217 */
4979d272
RR
218static inline unsigned int amd_get_event_code(struct hw_perf_event *hwc)
219{
220 return ((hwc->config >> 24) & 0x0f00) | (hwc->config & 0x00ff);
221}
222
f22f54f4
PZ
223static inline int amd_is_nb_event(struct hw_perf_event *hwc)
224{
225 return (hwc->config & 0xe0) == 0xe0;
226}
227
b38b24ea
PZ
228static inline int amd_has_nb(struct cpu_hw_events *cpuc)
229{
230 struct amd_nb *nb = cpuc->amd_nb;
231
232 return nb && nb->nb_id != -1;
233}
234
e259514e
JS
235static int amd_pmu_hw_config(struct perf_event *event)
236{
237 int ret;
238
239 /* pass precise event sampling to ibs: */
240 if (event->attr.precise_ip && get_ibs_caps())
241 return -ENOENT;
242
243 if (has_branch_stack(event))
244 return -EOPNOTSUPP;
245
246 ret = x86_pmu_hw_config(event);
247 if (ret)
248 return ret;
249
250 if (event->attr.type == PERF_TYPE_RAW)
251 event->hw.config |= event->attr.config & AMD64_RAW_EVENT_MASK;
252
e259514e
JS
253 return amd_core_hw_config(event);
254}
255
4dd4c2ae
RR
256static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
257 struct perf_event *event)
f22f54f4 258{
f22f54f4
PZ
259 struct amd_nb *nb = cpuc->amd_nb;
260 int i;
261
f22f54f4
PZ
262 /*
263 * need to scan whole list because event may not have
264 * been assigned during scheduling
265 *
266 * no race condition possible because event can only
267 * be removed on one CPU at a time AND PMU is disabled
268 * when we come here
269 */
948b1bb8 270 for (i = 0; i < x86_pmu.num_counters; i++) {
5f09fc68 271 if (cmpxchg(nb->owners + i, event, NULL) == event)
f22f54f4 272 break;
f22f54f4
PZ
273 }
274}
275
276 /*
277 * AMD64 NorthBridge events need special treatment because
278 * counter access needs to be synchronized across all cores
279 * of a package. Refer to BKDG section 3.12
280 *
281 * NB events are events measuring L3 cache, Hypertransport
282 * traffic. They are identified by an event code >= 0xe00.
283 * They measure events on the NorthBride which is shared
284 * by all cores on a package. NB events are counted on a
285 * shared set of counters. When a NB event is programmed
286 * in a counter, the data actually comes from a shared
287 * counter. Thus, access to those counters needs to be
288 * synchronized.
289 *
290 * We implement the synchronization such that no two cores
291 * can be measuring NB events using the same counters. Thus,
292 * we maintain a per-NB allocation table. The available slot
293 * is propagated using the event_constraint structure.
294 *
295 * We provide only one choice for each NB event based on
296 * the fact that only NB events have restrictions. Consequently,
297 * if a counter is available, there is a guarantee the NB event
298 * will be assigned to it. If no slot is available, an empty
299 * constraint is returned and scheduling will eventually fail
300 * for this event.
301 *
302 * Note that all cores attached the same NB compete for the same
303 * counters to host NB events, this is why we use atomic ops. Some
304 * multi-chip CPUs may have more than one NB.
305 *
306 * Given that resources are allocated (cmpxchg), they must be
307 * eventually freed for others to use. This is accomplished by
4dd4c2ae 308 * calling __amd_put_nb_event_constraints()
f22f54f4
PZ
309 *
310 * Non NB events are not impacted by this restriction.
311 */
312static struct event_constraint *
4dd4c2ae
RR
313__amd_get_nb_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
314 struct event_constraint *c)
f22f54f4
PZ
315{
316 struct hw_perf_event *hwc = &event->hw;
317 struct amd_nb *nb = cpuc->amd_nb;
2c53c3dd
RR
318 struct perf_event *old;
319 int idx, new = -1;
f22f54f4 320
e259514e
JS
321 if (!c)
322 c = &unconstrained;
323
324 if (cpuc->is_fake)
325 return c;
326
f22f54f4
PZ
327 /*
328 * detect if already present, if so reuse
329 *
330 * cannot merge with actual allocation
331 * because of possible holes
332 *
333 * event can already be present yet not assigned (in hwc->idx)
334 * because of successive calls to x86_schedule_events() from
335 * hw_perf_group_sched_in() without hw_perf_enable()
336 */
4dd4c2ae 337 for_each_set_bit(idx, c->idxmsk, x86_pmu.num_counters) {
2c53c3dd
RR
338 if (new == -1 || hwc->idx == idx)
339 /* assign free slot, prefer hwc->idx */
340 old = cmpxchg(nb->owners + idx, NULL, event);
341 else if (nb->owners[idx] == event)
342 /* event already present */
343 old = event;
344 else
345 continue;
346
347 if (old && old != event)
348 continue;
349
350 /* reassign to this slot */
351 if (new != -1)
352 cmpxchg(nb->owners + new, event, NULL);
353 new = idx;
f22f54f4
PZ
354
355 /* already present, reuse */
2c53c3dd 356 if (old == event)
f22f54f4 357 break;
2c53c3dd
RR
358 }
359
360 if (new == -1)
361 return &emptyconstraint;
362
363 return &nb->event_constraints[new];
f22f54f4
PZ
364}
365
c079c791 366static struct amd_nb *amd_alloc_nb(int cpu)
f22f54f4
PZ
367{
368 struct amd_nb *nb;
369 int i;
370
7bfb7e6b 371 nb = kzalloc_node(sizeof(struct amd_nb), GFP_KERNEL, cpu_to_node(cpu));
f22f54f4
PZ
372 if (!nb)
373 return NULL;
374
c079c791 375 nb->nb_id = -1;
f22f54f4
PZ
376
377 /*
378 * initialize all possible NB constraints
379 */
948b1bb8 380 for (i = 0; i < x86_pmu.num_counters; i++) {
34538ee7 381 __set_bit(i, nb->event_constraints[i].idxmsk);
f22f54f4
PZ
382 nb->event_constraints[i].weight = 1;
383 }
384 return nb;
385}
386
b38b24ea
PZ
387static int amd_pmu_cpu_prepare(int cpu)
388{
389 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
390
391 WARN_ON_ONCE(cpuc->amd_nb);
392
32b62f44 393 if (!x86_pmu.amd_nb_constraints)
95ca792c 394 return 0;
b38b24ea 395
c079c791 396 cpuc->amd_nb = amd_alloc_nb(cpu);
b38b24ea 397 if (!cpuc->amd_nb)
95ca792c 398 return -ENOMEM;
b38b24ea 399
95ca792c 400 return 0;
b38b24ea
PZ
401}
402
403static void amd_pmu_cpu_starting(int cpu)
f22f54f4 404{
b38b24ea 405 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
90413464 406 void **onln = &cpuc->kfree_on_online[X86_PERF_KFREE_SHARED];
b38b24ea 407 struct amd_nb *nb;
f22f54f4
PZ
408 int i, nb_id;
409
9f19010a 410 cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
1018faa6 411
32b62f44 412 if (!x86_pmu.amd_nb_constraints)
f22f54f4
PZ
413 return;
414
f22f54f4 415 nb_id = amd_get_nb_id(cpu);
b38b24ea 416 WARN_ON_ONCE(nb_id == BAD_APICID);
f22f54f4 417
f22f54f4 418 for_each_online_cpu(i) {
b38b24ea
PZ
419 nb = per_cpu(cpu_hw_events, i).amd_nb;
420 if (WARN_ON_ONCE(!nb))
f22f54f4 421 continue;
f22f54f4 422
b38b24ea 423 if (nb->nb_id == nb_id) {
90413464 424 *onln = cpuc->amd_nb;
b38b24ea
PZ
425 cpuc->amd_nb = nb;
426 break;
427 }
f22f54f4 428 }
b38b24ea
PZ
429
430 cpuc->amd_nb->nb_id = nb_id;
431 cpuc->amd_nb->refcnt++;
f22f54f4
PZ
432}
433
b38b24ea 434static void amd_pmu_cpu_dead(int cpu)
f22f54f4
PZ
435{
436 struct cpu_hw_events *cpuhw;
437
32b62f44 438 if (!x86_pmu.amd_nb_constraints)
f22f54f4
PZ
439 return;
440
441 cpuhw = &per_cpu(cpu_hw_events, cpu);
442
a90110c6 443 if (cpuhw->amd_nb) {
b38b24ea
PZ
444 struct amd_nb *nb = cpuhw->amd_nb;
445
446 if (nb->nb_id == -1 || --nb->refcnt == 0)
447 kfree(nb);
f22f54f4 448
a90110c6
RW
449 cpuhw->amd_nb = NULL;
450 }
f22f54f4
PZ
451}
452
914123fa
LT
453/*
454 * When a PMC counter overflows, an NMI is used to process the event and
455 * reset the counter. NMI latency can result in the counter being updated
456 * before the NMI can run, which can result in what appear to be spurious
457 * NMIs. This function is intended to wait for the NMI to run and reset
458 * the counter to avoid possible unhandled NMI messages.
459 */
460#define OVERFLOW_WAIT_COUNT 50
461
462static void amd_pmu_wait_on_overflow(int idx)
463{
464 unsigned int i;
465 u64 counter;
466
467 /*
468 * Wait for the counter to be reset if it has overflowed. This loop
469 * should exit very, very quickly, but just in case, don't wait
470 * forever...
471 */
472 for (i = 0; i < OVERFLOW_WAIT_COUNT; i++) {
473 rdmsrl(x86_pmu_event_addr(idx), counter);
474 if (counter & (1ULL << (x86_pmu.cntval_bits - 1)))
475 break;
476
477 /* Might be in IRQ context, so can't sleep */
478 udelay(1);
479 }
480}
481
482static void amd_pmu_disable_all(void)
483{
484 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
485 int idx;
486
487 x86_pmu_disable_all();
488
489 /*
490 * This shouldn't be called from NMI context, but add a safeguard here
491 * to return, since if we're in NMI context we can't wait for an NMI
492 * to reset an overflowed counter value.
493 */
494 if (in_nmi())
495 return;
496
497 /*
498 * Check each counter for overflow and wait for it to be reset by the
499 * NMI if it has overflowed. This relies on the fact that all active
500 * counters are always enabled when this function is caled and
501 * ARCH_PERFMON_EVENTSEL_INT is always set.
502 */
503 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
504 if (!test_bit(idx, cpuc->active_mask))
505 continue;
506
507 amd_pmu_wait_on_overflow(idx);
508 }
509}
510
3966c3fe
LT
511static void amd_pmu_disable_event(struct perf_event *event)
512{
513 x86_pmu_disable_event(event);
514
515 /*
516 * This can be called from NMI context (via x86_pmu_stop). The counter
517 * may have overflowed, but either way, we'll never see it get reset
518 * by the NMI if we're already in the NMI. And the NMI latency support
519 * below will take care of any pending NMI that might have been
520 * generated by the overflow.
521 */
522 if (in_nmi())
523 return;
524
525 amd_pmu_wait_on_overflow(event->hw.idx);
526}
527
6d3edaae
LT
528/*
529 * Because of NMI latency, if multiple PMC counters are active or other sources
530 * of NMIs are received, the perf NMI handler can handle one or more overflowed
531 * PMC counters outside of the NMI associated with the PMC overflow. If the NMI
532 * doesn't arrive at the LAPIC in time to become a pending NMI, then the kernel
533 * back-to-back NMI support won't be active. This PMC handler needs to take into
534 * account that this can occur, otherwise this could result in unknown NMI
535 * messages being issued. Examples of this is PMC overflow while in the NMI
536 * handler when multiple PMCs are active or PMC overflow while handling some
537 * other source of an NMI.
538 *
539 * Attempt to mitigate this by using the number of active PMCs to determine
540 * whether to return NMI_HANDLED if the perf NMI handler did not handle/reset
541 * any PMCs. The per-CPU perf_nmi_counter variable is set to a minimum of the
542 * number of active PMCs or 2. The value of 2 is used in case an NMI does not
543 * arrive at the LAPIC in time to be collapsed into an already pending NMI.
544 */
545static int amd_pmu_handle_irq(struct pt_regs *regs)
546{
547 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
548 int active, handled;
549
550 /*
551 * Obtain the active count before calling x86_pmu_handle_irq() since
552 * it is possible that x86_pmu_handle_irq() may make a counter
553 * inactive (through x86_pmu_stop).
554 */
555 active = __bitmap_weight(cpuc->active_mask, X86_PMC_IDX_MAX);
556
557 /* Process any counter overflows */
558 handled = x86_pmu_handle_irq(regs);
559
560 /*
561 * If a counter was handled, record the number of possible remaining
562 * NMIs that can occur.
563 */
564 if (handled) {
565 this_cpu_write(perf_nmi_counter,
566 min_t(unsigned int, 2, active));
567
568 return handled;
569 }
570
571 if (!this_cpu_read(perf_nmi_counter))
572 return NMI_DONE;
573
574 this_cpu_dec(perf_nmi_counter);
575
576 return NMI_HANDLED;
577}
578
4dd4c2ae 579static struct event_constraint *
79cba822
SE
580amd_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
581 struct perf_event *event)
4dd4c2ae
RR
582{
583 /*
584 * if not NB event or no NB, then no constraints
585 */
586 if (!(amd_has_nb(cpuc) && amd_is_nb_event(&event->hw)))
587 return &unconstrained;
588
0cf5f432 589 return __amd_get_nb_event_constraints(cpuc, event, NULL);
4dd4c2ae
RR
590}
591
592static void amd_put_event_constraints(struct cpu_hw_events *cpuc,
593 struct perf_event *event)
594{
595 if (amd_has_nb(cpuc) && amd_is_nb_event(&event->hw))
596 __amd_put_nb_event_constraints(cpuc, event);
597}
598
641cc938
JO
599PMU_FORMAT_ATTR(event, "config:0-7,32-35");
600PMU_FORMAT_ATTR(umask, "config:8-15" );
601PMU_FORMAT_ATTR(edge, "config:18" );
602PMU_FORMAT_ATTR(inv, "config:23" );
603PMU_FORMAT_ATTR(cmask, "config:24-31" );
604
605static struct attribute *amd_format_attr[] = {
606 &format_attr_event.attr,
607 &format_attr_umask.attr,
608 &format_attr_edge.attr,
609 &format_attr_inv.attr,
610 &format_attr_cmask.attr,
611 NULL,
612};
613
4979d272
RR
614/* AMD Family 15h */
615
616#define AMD_EVENT_TYPE_MASK 0x000000F0ULL
617
618#define AMD_EVENT_FP 0x00000000ULL ... 0x00000010ULL
619#define AMD_EVENT_LS 0x00000020ULL ... 0x00000030ULL
620#define AMD_EVENT_DC 0x00000040ULL ... 0x00000050ULL
621#define AMD_EVENT_CU 0x00000060ULL ... 0x00000070ULL
622#define AMD_EVENT_IC_DE 0x00000080ULL ... 0x00000090ULL
623#define AMD_EVENT_EX_LS 0x000000C0ULL
624#define AMD_EVENT_DE 0x000000D0ULL
625#define AMD_EVENT_NB 0x000000E0ULL ... 0x000000F0ULL
626
627/*
628 * AMD family 15h event code/PMC mappings:
629 *
630 * type = event_code & 0x0F0:
631 *
632 * 0x000 FP PERF_CTL[5:3]
633 * 0x010 FP PERF_CTL[5:3]
634 * 0x020 LS PERF_CTL[5:0]
635 * 0x030 LS PERF_CTL[5:0]
636 * 0x040 DC PERF_CTL[5:0]
637 * 0x050 DC PERF_CTL[5:0]
638 * 0x060 CU PERF_CTL[2:0]
639 * 0x070 CU PERF_CTL[2:0]
640 * 0x080 IC/DE PERF_CTL[2:0]
641 * 0x090 IC/DE PERF_CTL[2:0]
642 * 0x0A0 ---
643 * 0x0B0 ---
644 * 0x0C0 EX/LS PERF_CTL[5:0]
645 * 0x0D0 DE PERF_CTL[2:0]
646 * 0x0E0 NB NB_PERF_CTL[3:0]
647 * 0x0F0 NB NB_PERF_CTL[3:0]
648 *
649 * Exceptions:
650 *
855357a2 651 * 0x000 FP PERF_CTL[3], PERF_CTL[5:3] (*)
4979d272 652 * 0x003 FP PERF_CTL[3]
855357a2 653 * 0x004 FP PERF_CTL[3], PERF_CTL[5:3] (*)
4979d272
RR
654 * 0x00B FP PERF_CTL[3]
655 * 0x00D FP PERF_CTL[3]
656 * 0x023 DE PERF_CTL[2:0]
657 * 0x02D LS PERF_CTL[3]
658 * 0x02E LS PERF_CTL[3,0]
5bcdf5e4 659 * 0x031 LS PERF_CTL[2:0] (**)
4979d272
RR
660 * 0x043 CU PERF_CTL[2:0]
661 * 0x045 CU PERF_CTL[2:0]
662 * 0x046 CU PERF_CTL[2:0]
663 * 0x054 CU PERF_CTL[2:0]
664 * 0x055 CU PERF_CTL[2:0]
665 * 0x08F IC PERF_CTL[0]
666 * 0x187 DE PERF_CTL[0]
667 * 0x188 DE PERF_CTL[0]
668 * 0x0DB EX PERF_CTL[5:0]
669 * 0x0DC LS PERF_CTL[5:0]
670 * 0x0DD LS PERF_CTL[5:0]
671 * 0x0DE LS PERF_CTL[5:0]
672 * 0x0DF LS PERF_CTL[5:0]
5bcdf5e4 673 * 0x1C0 EX PERF_CTL[5:3]
4979d272
RR
674 * 0x1D6 EX PERF_CTL[5:0]
675 * 0x1D8 EX PERF_CTL[5:0]
855357a2 676 *
5bcdf5e4
RR
677 * (*) depending on the umask all FPU counters may be used
678 * (**) only one unitmask enabled at a time
4979d272
RR
679 */
680
681static struct event_constraint amd_f15_PMC0 = EVENT_CONSTRAINT(0, 0x01, 0);
682static struct event_constraint amd_f15_PMC20 = EVENT_CONSTRAINT(0, 0x07, 0);
683static struct event_constraint amd_f15_PMC3 = EVENT_CONSTRAINT(0, 0x08, 0);
bc1738f6 684static struct event_constraint amd_f15_PMC30 = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
4979d272
RR
685static struct event_constraint amd_f15_PMC50 = EVENT_CONSTRAINT(0, 0x3F, 0);
686static struct event_constraint amd_f15_PMC53 = EVENT_CONSTRAINT(0, 0x38, 0);
687
688static struct event_constraint *
79cba822
SE
689amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, int idx,
690 struct perf_event *event)
4979d272 691{
855357a2
RR
692 struct hw_perf_event *hwc = &event->hw;
693 unsigned int event_code = amd_get_event_code(hwc);
4979d272
RR
694
695 switch (event_code & AMD_EVENT_TYPE_MASK) {
696 case AMD_EVENT_FP:
697 switch (event_code) {
855357a2
RR
698 case 0x000:
699 if (!(hwc->config & 0x0000F000ULL))
700 break;
701 if (!(hwc->config & 0x00000F00ULL))
702 break;
703 return &amd_f15_PMC3;
704 case 0x004:
705 if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
706 break;
707 return &amd_f15_PMC3;
4979d272
RR
708 case 0x003:
709 case 0x00B:
710 case 0x00D:
711 return &amd_f15_PMC3;
4979d272 712 }
855357a2 713 return &amd_f15_PMC53;
4979d272
RR
714 case AMD_EVENT_LS:
715 case AMD_EVENT_DC:
716 case AMD_EVENT_EX_LS:
717 switch (event_code) {
718 case 0x023:
719 case 0x043:
720 case 0x045:
721 case 0x046:
722 case 0x054:
723 case 0x055:
724 return &amd_f15_PMC20;
725 case 0x02D:
726 return &amd_f15_PMC3;
727 case 0x02E:
728 return &amd_f15_PMC30;
5bcdf5e4
RR
729 case 0x031:
730 if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
731 return &amd_f15_PMC20;
732 return &emptyconstraint;
733 case 0x1C0:
734 return &amd_f15_PMC53;
4979d272
RR
735 default:
736 return &amd_f15_PMC50;
737 }
738 case AMD_EVENT_CU:
739 case AMD_EVENT_IC_DE:
740 case AMD_EVENT_DE:
741 switch (event_code) {
742 case 0x08F:
743 case 0x187:
744 case 0x188:
745 return &amd_f15_PMC0;
746 case 0x0DB ... 0x0DF:
747 case 0x1D6:
748 case 0x1D8:
749 return &amd_f15_PMC50;
750 default:
751 return &amd_f15_PMC20;
752 }
753 case AMD_EVENT_NB:
940b2f2f 754 /* moved to uncore.c */
0cf5f432 755 return &emptyconstraint;
4979d272
RR
756 default:
757 return &emptyconstraint;
758 }
759}
760
0bf79d44
JO
761static ssize_t amd_event_sysfs_show(char *page, u64 config)
762{
763 u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT) |
764 (config & AMD64_EVENTSEL_EVENT) >> 24;
765
766 return x86_event_sysfs_show(page, config, event);
767}
768
b1dc3c48
RR
769static __initconst const struct x86_pmu amd_pmu = {
770 .name = "AMD",
6d3edaae 771 .handle_irq = amd_pmu_handle_irq,
914123fa 772 .disable_all = amd_pmu_disable_all,
4979d272
RR
773 .enable_all = x86_pmu_enable_all,
774 .enable = x86_pmu_enable_event,
3966c3fe 775 .disable = amd_pmu_disable_event,
4979d272
RR
776 .hw_config = amd_pmu_hw_config,
777 .schedule_events = x86_schedule_events,
b1dc3c48
RR
778 .eventsel = MSR_K7_EVNTSEL0,
779 .perfctr = MSR_K7_PERFCTR0,
4c1fd17a 780 .addr_offset = amd_pmu_addr_offset,
4979d272
RR
781 .event_map = amd_pmu_event_map,
782 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
b1dc3c48 783 .num_counters = AMD64_NUM_COUNTERS,
4979d272
RR
784 .cntval_bits = 48,
785 .cntval_mask = (1ULL << 48) - 1,
786 .apic = 1,
787 /* use highest bit to detect overflow */
788 .max_period = (1ULL << 47) - 1,
b1dc3c48 789 .get_event_constraints = amd_get_event_constraints,
4979d272
RR
790 .put_event_constraints = amd_put_event_constraints,
791
b1dc3c48 792 .format_attrs = amd_format_attr,
0bf79d44 793 .events_sysfs_show = amd_event_sysfs_show,
b1dc3c48 794
4979d272 795 .cpu_prepare = amd_pmu_cpu_prepare,
1018faa6 796 .cpu_starting = amd_pmu_cpu_starting,
b1dc3c48 797 .cpu_dead = amd_pmu_cpu_dead,
32b62f44
PZ
798
799 .amd_nb_constraints = 1,
4979d272
RR
800};
801
1b45adcd 802static int __init amd_core_pmu_init(void)
b1dc3c48 803{
362f924b 804 if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
1b45adcd
PZ
805 return 0;
806
807 switch (boot_cpu_data.x86) {
808 case 0x15:
809 pr_cont("Fam15h ");
b1dc3c48 810 x86_pmu.get_event_constraints = amd_get_event_constraints_f15h;
1b45adcd 811 break;
e40ed154
JN
812 case 0x17:
813 pr_cont("Fam17h ");
814 /*
815 * In family 17h, there are no event constraints in the PMC hardware.
816 * We fallback to using default amd_get_event_constraints.
817 */
818 break;
6d0ef316
PW
819 case 0x18:
820 pr_cont("Fam18h ");
821 /* Using default amd_get_event_constraints. */
822 break;
1b45adcd
PZ
823 default:
824 pr_err("core perfctr but no constraints; unknown hardware!\n");
b1dc3c48
RR
825 return -ENODEV;
826 }
827
b1dc3c48
RR
828 /*
829 * If core performance counter extensions exists, we must use
830 * MSR_F15H_PERF_CTL/MSR_F15H_PERF_CTR msrs. See also
1b45adcd 831 * amd_pmu_addr_offset().
b1dc3c48
RR
832 */
833 x86_pmu.eventsel = MSR_F15H_PERF_CTL;
834 x86_pmu.perfctr = MSR_F15H_PERF_CTR;
835 x86_pmu.num_counters = AMD64_NUM_COUNTERS_CORE;
32b62f44
PZ
836 /*
837 * AMD Core perfctr has separate MSRs for the NB events, see
838 * the amd/uncore.c driver.
839 */
840 x86_pmu.amd_nb_constraints = 0;
b1dc3c48 841
1b45adcd 842 pr_cont("core perfctr, ");
b1dc3c48
RR
843 return 0;
844}
845
de0428a7 846__init int amd_pmu_init(void)
f22f54f4 847{
1b45adcd
PZ
848 int ret;
849
f22f54f4
PZ
850 /* Performance-monitoring supported from K7 and later: */
851 if (boot_cpu_data.x86 < 6)
852 return -ENODEV;
853
b1dc3c48
RR
854 x86_pmu = amd_pmu;
855
1b45adcd
PZ
856 ret = amd_core_pmu_init();
857 if (ret)
858 return ret;
f22f54f4 859
32b62f44
PZ
860 if (num_possible_cpus() == 1) {
861 /*
862 * No point in allocating data structures to serialize
863 * against other CPUs, when there is only the one CPU.
864 */
865 x86_pmu.amd_nb_constraints = 0;
866 }
867
f22f54f4
PZ
868 /* Events are common for all AMDs */
869 memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
870 sizeof(hw_cache_event_ids));
871
f22f54f4
PZ
872 return 0;
873}
1018faa6
JR
874
875void amd_pmu_enable_virt(void)
876{
89cbc767 877 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1018faa6
JR
878
879 cpuc->perf_ctr_virt_mask = 0;
880
881 /* Reload all events */
914123fa 882 amd_pmu_disable_all();
1018faa6
JR
883 x86_pmu_enable_all(0);
884}
885EXPORT_SYMBOL_GPL(amd_pmu_enable_virt);
886
887void amd_pmu_disable_virt(void)
888{
89cbc767 889 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1018faa6
JR
890
891 /*
892 * We only mask out the Host-only bit so that host-only counting works
893 * when SVM is disabled. If someone sets up a guest-only counter when
894 * SVM is disabled the Guest-only bits still gets set and the counter
895 * will not count anything.
896 */
9f19010a 897 cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
1018faa6
JR
898
899 /* Reload all events */
914123fa 900 amd_pmu_disable_all();
1018faa6
JR
901 x86_pmu_enable_all(0);
902}
903EXPORT_SYMBOL_GPL(amd_pmu_disable_virt);