]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - arch/x86/kernel/cpu/perf_counter.c
perf_counter: frequency based adaptive irq_period
[mirror_ubuntu-eoan-kernel.git] / arch / x86 / kernel / cpu / perf_counter.c
1 /*
2 * Performance counter x86 architecture code
3 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2009 Jaswinder Singh Rajput
7 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
8 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
9 *
10 * For licencing details see kernel-base/COPYING
11 */
12
13 #include <linux/perf_counter.h>
14 #include <linux/capability.h>
15 #include <linux/notifier.h>
16 #include <linux/hardirq.h>
17 #include <linux/kprobes.h>
18 #include <linux/module.h>
19 #include <linux/kdebug.h>
20 #include <linux/sched.h>
21 #include <linux/uaccess.h>
22
23 #include <asm/apic.h>
24 #include <asm/stacktrace.h>
25 #include <asm/nmi.h>
26
27 static u64 perf_counter_mask __read_mostly;
28
29 struct cpu_hw_counters {
30 struct perf_counter *counters[X86_PMC_IDX_MAX];
31 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
32 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
33 unsigned long interrupts;
34 int enabled;
35 };
36
37 /*
38 * struct x86_pmu - generic x86 pmu
39 */
40 struct x86_pmu {
41 const char *name;
42 int version;
43 int (*handle_irq)(struct pt_regs *, int);
44 void (*disable_all)(void);
45 void (*enable_all)(void);
46 void (*enable)(struct hw_perf_counter *, int);
47 void (*disable)(struct hw_perf_counter *, int);
48 unsigned eventsel;
49 unsigned perfctr;
50 u64 (*event_map)(int);
51 u64 (*raw_event)(u64);
52 int max_events;
53 int num_counters;
54 int num_counters_fixed;
55 int counter_bits;
56 u64 counter_mask;
57 u64 max_period;
58 u64 intel_ctrl;
59 };
60
61 static struct x86_pmu x86_pmu __read_mostly;
62
63 static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
64 .enabled = 1,
65 };
66
67 /*
68 * Intel PerfMon v3. Used on Core2 and later.
69 */
70 static const u64 intel_perfmon_event_map[] =
71 {
72 [PERF_COUNT_CPU_CYCLES] = 0x003c,
73 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
74 [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
75 [PERF_COUNT_CACHE_MISSES] = 0x412e,
76 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
77 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
78 [PERF_COUNT_BUS_CYCLES] = 0x013c,
79 };
80
81 static u64 intel_pmu_event_map(int event)
82 {
83 return intel_perfmon_event_map[event];
84 }
85
86 static u64 intel_pmu_raw_event(u64 event)
87 {
88 #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
89 #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
90 #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
91
92 #define CORE_EVNTSEL_MASK \
93 (CORE_EVNTSEL_EVENT_MASK | \
94 CORE_EVNTSEL_UNIT_MASK | \
95 CORE_EVNTSEL_COUNTER_MASK)
96
97 return event & CORE_EVNTSEL_MASK;
98 }
99
100 /*
101 * AMD Performance Monitor K7 and later.
102 */
103 static const u64 amd_perfmon_event_map[] =
104 {
105 [PERF_COUNT_CPU_CYCLES] = 0x0076,
106 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
107 [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
108 [PERF_COUNT_CACHE_MISSES] = 0x0081,
109 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
110 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
111 };
112
113 static u64 amd_pmu_event_map(int event)
114 {
115 return amd_perfmon_event_map[event];
116 }
117
118 static u64 amd_pmu_raw_event(u64 event)
119 {
120 #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
121 #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
122 #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
123
124 #define K7_EVNTSEL_MASK \
125 (K7_EVNTSEL_EVENT_MASK | \
126 K7_EVNTSEL_UNIT_MASK | \
127 K7_EVNTSEL_COUNTER_MASK)
128
129 return event & K7_EVNTSEL_MASK;
130 }
131
132 /*
133 * Propagate counter elapsed time into the generic counter.
134 * Can only be executed on the CPU where the counter is active.
135 * Returns the delta events processed.
136 */
137 static u64
138 x86_perf_counter_update(struct perf_counter *counter,
139 struct hw_perf_counter *hwc, int idx)
140 {
141 int shift = 64 - x86_pmu.counter_bits;
142 u64 prev_raw_count, new_raw_count;
143 s64 delta;
144
145 /*
146 * Careful: an NMI might modify the previous counter value.
147 *
148 * Our tactic to handle this is to first atomically read and
149 * exchange a new raw count - then add that new-prev delta
150 * count to the generic counter atomically:
151 */
152 again:
153 prev_raw_count = atomic64_read(&hwc->prev_count);
154 rdmsrl(hwc->counter_base + idx, new_raw_count);
155
156 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
157 new_raw_count) != prev_raw_count)
158 goto again;
159
160 /*
161 * Now we have the new raw value and have updated the prev
162 * timestamp already. We can now calculate the elapsed delta
163 * (counter-)time and add that to the generic counter.
164 *
165 * Careful, not all hw sign-extends above the physical width
166 * of the count.
167 */
168 delta = (new_raw_count << shift) - (prev_raw_count << shift);
169 delta >>= shift;
170
171 atomic64_add(delta, &counter->count);
172 atomic64_sub(delta, &hwc->period_left);
173
174 return new_raw_count;
175 }
176
177 static atomic_t active_counters;
178 static DEFINE_MUTEX(pmc_reserve_mutex);
179
180 static bool reserve_pmc_hardware(void)
181 {
182 int i;
183
184 if (nmi_watchdog == NMI_LOCAL_APIC)
185 disable_lapic_nmi_watchdog();
186
187 for (i = 0; i < x86_pmu.num_counters; i++) {
188 if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
189 goto perfctr_fail;
190 }
191
192 for (i = 0; i < x86_pmu.num_counters; i++) {
193 if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
194 goto eventsel_fail;
195 }
196
197 return true;
198
199 eventsel_fail:
200 for (i--; i >= 0; i--)
201 release_evntsel_nmi(x86_pmu.eventsel + i);
202
203 i = x86_pmu.num_counters;
204
205 perfctr_fail:
206 for (i--; i >= 0; i--)
207 release_perfctr_nmi(x86_pmu.perfctr + i);
208
209 if (nmi_watchdog == NMI_LOCAL_APIC)
210 enable_lapic_nmi_watchdog();
211
212 return false;
213 }
214
215 static void release_pmc_hardware(void)
216 {
217 int i;
218
219 for (i = 0; i < x86_pmu.num_counters; i++) {
220 release_perfctr_nmi(x86_pmu.perfctr + i);
221 release_evntsel_nmi(x86_pmu.eventsel + i);
222 }
223
224 if (nmi_watchdog == NMI_LOCAL_APIC)
225 enable_lapic_nmi_watchdog();
226 }
227
228 static void hw_perf_counter_destroy(struct perf_counter *counter)
229 {
230 if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) {
231 release_pmc_hardware();
232 mutex_unlock(&pmc_reserve_mutex);
233 }
234 }
235
236 static inline int x86_pmu_initialized(void)
237 {
238 return x86_pmu.handle_irq != NULL;
239 }
240
241 /*
242 * Setup the hardware configuration for a given hw_event_type
243 */
244 static int __hw_perf_counter_init(struct perf_counter *counter)
245 {
246 struct perf_counter_hw_event *hw_event = &counter->hw_event;
247 struct hw_perf_counter *hwc = &counter->hw;
248 int err;
249
250 if (!x86_pmu_initialized())
251 return -ENODEV;
252
253 err = 0;
254 if (!atomic_inc_not_zero(&active_counters)) {
255 mutex_lock(&pmc_reserve_mutex);
256 if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware())
257 err = -EBUSY;
258 else
259 atomic_inc(&active_counters);
260 mutex_unlock(&pmc_reserve_mutex);
261 }
262 if (err)
263 return err;
264
265 /*
266 * Generate PMC IRQs:
267 * (keep 'enabled' bit clear for now)
268 */
269 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
270
271 /*
272 * Count user and OS events unless requested not to.
273 */
274 if (!hw_event->exclude_user)
275 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
276 if (!hw_event->exclude_kernel)
277 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
278
279 /*
280 * If privileged enough, allow NMI events:
281 */
282 hwc->nmi = 0;
283 if (hw_event->nmi) {
284 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
285 return -EACCES;
286 hwc->nmi = 1;
287 }
288
289 atomic64_set(&hwc->period_left,
290 min(x86_pmu.max_period, hwc->irq_period));
291
292 /*
293 * Raw event type provide the config in the event structure
294 */
295 if (perf_event_raw(hw_event)) {
296 hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event));
297 } else {
298 if (perf_event_id(hw_event) >= x86_pmu.max_events)
299 return -EINVAL;
300 /*
301 * The generic map:
302 */
303 hwc->config |= x86_pmu.event_map(perf_event_id(hw_event));
304 }
305
306 counter->destroy = hw_perf_counter_destroy;
307
308 return 0;
309 }
310
311 static void intel_pmu_disable_all(void)
312 {
313 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
314 }
315
316 static void amd_pmu_disable_all(void)
317 {
318 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
319 int idx;
320
321 if (!cpuc->enabled)
322 return;
323
324 cpuc->enabled = 0;
325 /*
326 * ensure we write the disable before we start disabling the
327 * counters proper, so that amd_pmu_enable_counter() does the
328 * right thing.
329 */
330 barrier();
331
332 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
333 u64 val;
334
335 if (!test_bit(idx, cpuc->active_mask))
336 continue;
337 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
338 if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
339 continue;
340 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
341 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
342 }
343 }
344
345 void hw_perf_disable(void)
346 {
347 if (!x86_pmu_initialized())
348 return;
349 return x86_pmu.disable_all();
350 }
351
352 static void intel_pmu_enable_all(void)
353 {
354 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
355 }
356
357 static void amd_pmu_enable_all(void)
358 {
359 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
360 int idx;
361
362 if (cpuc->enabled)
363 return;
364
365 cpuc->enabled = 1;
366 barrier();
367
368 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
369 u64 val;
370
371 if (!test_bit(idx, cpuc->active_mask))
372 continue;
373 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
374 if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
375 continue;
376 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
377 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
378 }
379 }
380
381 void hw_perf_enable(void)
382 {
383 if (!x86_pmu_initialized())
384 return;
385 x86_pmu.enable_all();
386 }
387
388 static inline u64 intel_pmu_get_status(void)
389 {
390 u64 status;
391
392 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
393
394 return status;
395 }
396
397 static inline void intel_pmu_ack_status(u64 ack)
398 {
399 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
400 }
401
402 static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
403 {
404 int err;
405 err = checking_wrmsrl(hwc->config_base + idx,
406 hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
407 }
408
409 static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
410 {
411 int err;
412 err = checking_wrmsrl(hwc->config_base + idx,
413 hwc->config);
414 }
415
416 static inline void
417 intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
418 {
419 int idx = __idx - X86_PMC_IDX_FIXED;
420 u64 ctrl_val, mask;
421 int err;
422
423 mask = 0xfULL << (idx * 4);
424
425 rdmsrl(hwc->config_base, ctrl_val);
426 ctrl_val &= ~mask;
427 err = checking_wrmsrl(hwc->config_base, ctrl_val);
428 }
429
430 static inline void
431 intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
432 {
433 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
434 intel_pmu_disable_fixed(hwc, idx);
435 return;
436 }
437
438 x86_pmu_disable_counter(hwc, idx);
439 }
440
441 static inline void
442 amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
443 {
444 x86_pmu_disable_counter(hwc, idx);
445 }
446
447 static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
448
449 /*
450 * Set the next IRQ period, based on the hwc->period_left value.
451 * To be called with the counter disabled in hw:
452 */
453 static void
454 x86_perf_counter_set_period(struct perf_counter *counter,
455 struct hw_perf_counter *hwc, int idx)
456 {
457 s64 left = atomic64_read(&hwc->period_left);
458 s64 period = min(x86_pmu.max_period, hwc->irq_period);
459 int err;
460
461 /*
462 * If we are way outside a reasoable range then just skip forward:
463 */
464 if (unlikely(left <= -period)) {
465 left = period;
466 atomic64_set(&hwc->period_left, left);
467 }
468
469 if (unlikely(left <= 0)) {
470 left += period;
471 atomic64_set(&hwc->period_left, left);
472 }
473 /*
474 * Quirk: certain CPUs dont like it if just 1 event is left:
475 */
476 if (unlikely(left < 2))
477 left = 2;
478
479 per_cpu(prev_left[idx], smp_processor_id()) = left;
480
481 /*
482 * The hw counter starts counting from this counter offset,
483 * mark it to be able to extra future deltas:
484 */
485 atomic64_set(&hwc->prev_count, (u64)-left);
486
487 err = checking_wrmsrl(hwc->counter_base + idx,
488 (u64)(-left) & x86_pmu.counter_mask);
489 }
490
491 static inline void
492 intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
493 {
494 int idx = __idx - X86_PMC_IDX_FIXED;
495 u64 ctrl_val, bits, mask;
496 int err;
497
498 /*
499 * Enable IRQ generation (0x8),
500 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
501 * if requested:
502 */
503 bits = 0x8ULL;
504 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
505 bits |= 0x2;
506 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
507 bits |= 0x1;
508 bits <<= (idx * 4);
509 mask = 0xfULL << (idx * 4);
510
511 rdmsrl(hwc->config_base, ctrl_val);
512 ctrl_val &= ~mask;
513 ctrl_val |= bits;
514 err = checking_wrmsrl(hwc->config_base, ctrl_val);
515 }
516
517 static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
518 {
519 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
520 intel_pmu_enable_fixed(hwc, idx);
521 return;
522 }
523
524 x86_pmu_enable_counter(hwc, idx);
525 }
526
527 static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
528 {
529 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
530
531 if (cpuc->enabled)
532 x86_pmu_enable_counter(hwc, idx);
533 else
534 x86_pmu_disable_counter(hwc, idx);
535 }
536
537 static int
538 fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
539 {
540 unsigned int event;
541
542 if (!x86_pmu.num_counters_fixed)
543 return -1;
544
545 if (unlikely(hwc->nmi))
546 return -1;
547
548 event = hwc->config & ARCH_PERFMON_EVENT_MASK;
549
550 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
551 return X86_PMC_IDX_FIXED_INSTRUCTIONS;
552 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
553 return X86_PMC_IDX_FIXED_CPU_CYCLES;
554 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
555 return X86_PMC_IDX_FIXED_BUS_CYCLES;
556
557 return -1;
558 }
559
560 /*
561 * Find a PMC slot for the freshly enabled / scheduled in counter:
562 */
563 static int x86_pmu_enable(struct perf_counter *counter)
564 {
565 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
566 struct hw_perf_counter *hwc = &counter->hw;
567 int idx;
568
569 idx = fixed_mode_idx(counter, hwc);
570 if (idx >= 0) {
571 /*
572 * Try to get the fixed counter, if that is already taken
573 * then try to get a generic counter:
574 */
575 if (test_and_set_bit(idx, cpuc->used_mask))
576 goto try_generic;
577
578 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
579 /*
580 * We set it so that counter_base + idx in wrmsr/rdmsr maps to
581 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
582 */
583 hwc->counter_base =
584 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
585 hwc->idx = idx;
586 } else {
587 idx = hwc->idx;
588 /* Try to get the previous generic counter again */
589 if (test_and_set_bit(idx, cpuc->used_mask)) {
590 try_generic:
591 idx = find_first_zero_bit(cpuc->used_mask,
592 x86_pmu.num_counters);
593 if (idx == x86_pmu.num_counters)
594 return -EAGAIN;
595
596 set_bit(idx, cpuc->used_mask);
597 hwc->idx = idx;
598 }
599 hwc->config_base = x86_pmu.eventsel;
600 hwc->counter_base = x86_pmu.perfctr;
601 }
602
603 perf_counters_lapic_init(hwc->nmi);
604
605 x86_pmu.disable(hwc, idx);
606
607 cpuc->counters[idx] = counter;
608 set_bit(idx, cpuc->active_mask);
609
610 x86_perf_counter_set_period(counter, hwc, idx);
611 x86_pmu.enable(hwc, idx);
612
613 return 0;
614 }
615
616 void perf_counter_print_debug(void)
617 {
618 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
619 struct cpu_hw_counters *cpuc;
620 unsigned long flags;
621 int cpu, idx;
622
623 if (!x86_pmu.num_counters)
624 return;
625
626 local_irq_save(flags);
627
628 cpu = smp_processor_id();
629 cpuc = &per_cpu(cpu_hw_counters, cpu);
630
631 if (x86_pmu.version >= 2) {
632 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
633 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
634 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
635 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
636
637 pr_info("\n");
638 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
639 pr_info("CPU#%d: status: %016llx\n", cpu, status);
640 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
641 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
642 }
643 pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
644
645 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
646 rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
647 rdmsrl(x86_pmu.perfctr + idx, pmc_count);
648
649 prev_left = per_cpu(prev_left[idx], cpu);
650
651 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
652 cpu, idx, pmc_ctrl);
653 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
654 cpu, idx, pmc_count);
655 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
656 cpu, idx, prev_left);
657 }
658 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
659 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
660
661 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
662 cpu, idx, pmc_count);
663 }
664 local_irq_restore(flags);
665 }
666
667 static void x86_pmu_disable(struct perf_counter *counter)
668 {
669 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
670 struct hw_perf_counter *hwc = &counter->hw;
671 int idx = hwc->idx;
672
673 /*
674 * Must be done before we disable, otherwise the nmi handler
675 * could reenable again:
676 */
677 clear_bit(idx, cpuc->active_mask);
678 x86_pmu.disable(hwc, idx);
679
680 /*
681 * Make sure the cleared pointer becomes visible before we
682 * (potentially) free the counter:
683 */
684 barrier();
685
686 /*
687 * Drain the remaining delta count out of a counter
688 * that we are disabling:
689 */
690 x86_perf_counter_update(counter, hwc, idx);
691 cpuc->counters[idx] = NULL;
692 clear_bit(idx, cpuc->used_mask);
693 }
694
695 /*
696 * Save and restart an expired counter. Called by NMI contexts,
697 * so it has to be careful about preempting normal counter ops:
698 */
699 static void intel_pmu_save_and_restart(struct perf_counter *counter)
700 {
701 struct hw_perf_counter *hwc = &counter->hw;
702 int idx = hwc->idx;
703
704 x86_perf_counter_update(counter, hwc, idx);
705 x86_perf_counter_set_period(counter, hwc, idx);
706
707 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
708 intel_pmu_enable_counter(hwc, idx);
709 }
710
711 /*
712 * Maximum interrupt frequency of 100KHz per CPU
713 */
714 #define PERFMON_MAX_INTERRUPTS (100000/HZ)
715
716 /*
717 * This handler is triggered by the local APIC, so the APIC IRQ handling
718 * rules apply:
719 */
720 static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
721 {
722 struct cpu_hw_counters *cpuc;
723 struct cpu_hw_counters;
724 int bit, cpu, loops;
725 u64 ack, status;
726
727 cpu = smp_processor_id();
728 cpuc = &per_cpu(cpu_hw_counters, cpu);
729
730 perf_disable();
731 status = intel_pmu_get_status();
732 if (!status) {
733 perf_enable();
734 return 0;
735 }
736
737 loops = 0;
738 again:
739 if (++loops > 100) {
740 WARN_ONCE(1, "perfcounters: irq loop stuck!\n");
741 return 1;
742 }
743
744 inc_irq_stat(apic_perf_irqs);
745 ack = status;
746 for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
747 struct perf_counter *counter = cpuc->counters[bit];
748
749 clear_bit(bit, (unsigned long *) &status);
750 if (!test_bit(bit, cpuc->active_mask))
751 continue;
752
753 intel_pmu_save_and_restart(counter);
754 if (perf_counter_overflow(counter, nmi, regs, 0))
755 intel_pmu_disable_counter(&counter->hw, bit);
756 }
757
758 intel_pmu_ack_status(ack);
759
760 /*
761 * Repeat if there is more work to be done:
762 */
763 status = intel_pmu_get_status();
764 if (status)
765 goto again;
766
767 if (++cpuc->interrupts != PERFMON_MAX_INTERRUPTS)
768 perf_enable();
769
770 return 1;
771 }
772
773 static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi)
774 {
775 int cpu, idx, throttle = 0, handled = 0;
776 struct cpu_hw_counters *cpuc;
777 struct perf_counter *counter;
778 struct hw_perf_counter *hwc;
779 u64 val;
780
781 cpu = smp_processor_id();
782 cpuc = &per_cpu(cpu_hw_counters, cpu);
783
784 if (++cpuc->interrupts == PERFMON_MAX_INTERRUPTS) {
785 throttle = 1;
786 __perf_disable();
787 cpuc->enabled = 0;
788 barrier();
789 }
790
791 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
792 int disable = 0;
793
794 if (!test_bit(idx, cpuc->active_mask))
795 continue;
796
797 counter = cpuc->counters[idx];
798 hwc = &counter->hw;
799
800 if (counter->hw_event.nmi != nmi)
801 goto next;
802
803 val = x86_perf_counter_update(counter, hwc, idx);
804 if (val & (1ULL << (x86_pmu.counter_bits - 1)))
805 goto next;
806
807 /* counter overflow */
808 x86_perf_counter_set_period(counter, hwc, idx);
809 handled = 1;
810 inc_irq_stat(apic_perf_irqs);
811 disable = perf_counter_overflow(counter, nmi, regs, 0);
812
813 next:
814 if (disable || throttle)
815 amd_pmu_disable_counter(hwc, idx);
816 }
817
818 return handled;
819 }
820
821 void perf_counter_unthrottle(void)
822 {
823 struct cpu_hw_counters *cpuc;
824
825 if (!x86_pmu_initialized())
826 return;
827
828 cpuc = &__get_cpu_var(cpu_hw_counters);
829 if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
830 /*
831 * Clear them before re-enabling irqs/NMIs again:
832 */
833 cpuc->interrupts = 0;
834 perf_enable();
835 } else {
836 cpuc->interrupts = 0;
837 }
838 }
839
840 void smp_perf_counter_interrupt(struct pt_regs *regs)
841 {
842 irq_enter();
843 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
844 ack_APIC_irq();
845 x86_pmu.handle_irq(regs, 0);
846 irq_exit();
847 }
848
849 void smp_perf_pending_interrupt(struct pt_regs *regs)
850 {
851 irq_enter();
852 ack_APIC_irq();
853 inc_irq_stat(apic_pending_irqs);
854 perf_counter_do_pending();
855 irq_exit();
856 }
857
858 void set_perf_counter_pending(void)
859 {
860 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
861 }
862
863 void perf_counters_lapic_init(int nmi)
864 {
865 u32 apic_val;
866
867 if (!x86_pmu_initialized())
868 return;
869
870 /*
871 * Enable the performance counter vector in the APIC LVT:
872 */
873 apic_val = apic_read(APIC_LVTERR);
874
875 apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
876 if (nmi)
877 apic_write(APIC_LVTPC, APIC_DM_NMI);
878 else
879 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
880 apic_write(APIC_LVTERR, apic_val);
881 }
882
883 static int __kprobes
884 perf_counter_nmi_handler(struct notifier_block *self,
885 unsigned long cmd, void *__args)
886 {
887 struct die_args *args = __args;
888 struct pt_regs *regs;
889
890 if (!atomic_read(&active_counters))
891 return NOTIFY_DONE;
892
893 switch (cmd) {
894 case DIE_NMI:
895 case DIE_NMI_IPI:
896 break;
897
898 default:
899 return NOTIFY_DONE;
900 }
901
902 regs = args->regs;
903
904 apic_write(APIC_LVTPC, APIC_DM_NMI);
905 /*
906 * Can't rely on the handled return value to say it was our NMI, two
907 * counters could trigger 'simultaneously' raising two back-to-back NMIs.
908 *
909 * If the first NMI handles both, the latter will be empty and daze
910 * the CPU.
911 */
912 x86_pmu.handle_irq(regs, 1);
913
914 return NOTIFY_STOP;
915 }
916
917 static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
918 .notifier_call = perf_counter_nmi_handler,
919 .next = NULL,
920 .priority = 1
921 };
922
923 static struct x86_pmu intel_pmu = {
924 .name = "Intel",
925 .handle_irq = intel_pmu_handle_irq,
926 .disable_all = intel_pmu_disable_all,
927 .enable_all = intel_pmu_enable_all,
928 .enable = intel_pmu_enable_counter,
929 .disable = intel_pmu_disable_counter,
930 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
931 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
932 .event_map = intel_pmu_event_map,
933 .raw_event = intel_pmu_raw_event,
934 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
935 /*
936 * Intel PMCs cannot be accessed sanely above 32 bit width,
937 * so we install an artificial 1<<31 period regardless of
938 * the generic counter period:
939 */
940 .max_period = (1ULL << 31) - 1,
941 };
942
943 static struct x86_pmu amd_pmu = {
944 .name = "AMD",
945 .handle_irq = amd_pmu_handle_irq,
946 .disable_all = amd_pmu_disable_all,
947 .enable_all = amd_pmu_enable_all,
948 .enable = amd_pmu_enable_counter,
949 .disable = amd_pmu_disable_counter,
950 .eventsel = MSR_K7_EVNTSEL0,
951 .perfctr = MSR_K7_PERFCTR0,
952 .event_map = amd_pmu_event_map,
953 .raw_event = amd_pmu_raw_event,
954 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
955 .num_counters = 4,
956 .counter_bits = 48,
957 .counter_mask = (1ULL << 48) - 1,
958 /* use highest bit to detect overflow */
959 .max_period = (1ULL << 47) - 1,
960 };
961
962 static int intel_pmu_init(void)
963 {
964 union cpuid10_edx edx;
965 union cpuid10_eax eax;
966 unsigned int unused;
967 unsigned int ebx;
968 int version;
969
970 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
971 return -ENODEV;
972
973 /*
974 * Check whether the Architectural PerfMon supports
975 * Branch Misses Retired Event or not.
976 */
977 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
978 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
979 return -ENODEV;
980
981 version = eax.split.version_id;
982 if (version < 2)
983 return -ENODEV;
984
985 x86_pmu = intel_pmu;
986 x86_pmu.version = version;
987 x86_pmu.num_counters = eax.split.num_counters;
988
989 /*
990 * Quirk: v2 perfmon does not report fixed-purpose counters, so
991 * assume at least 3 counters:
992 */
993 x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
994
995 x86_pmu.counter_bits = eax.split.bit_width;
996 x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
997
998 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
999
1000 return 0;
1001 }
1002
1003 static int amd_pmu_init(void)
1004 {
1005 x86_pmu = amd_pmu;
1006 return 0;
1007 }
1008
1009 void __init init_hw_perf_counters(void)
1010 {
1011 int err;
1012
1013 switch (boot_cpu_data.x86_vendor) {
1014 case X86_VENDOR_INTEL:
1015 err = intel_pmu_init();
1016 break;
1017 case X86_VENDOR_AMD:
1018 err = amd_pmu_init();
1019 break;
1020 default:
1021 return;
1022 }
1023 if (err != 0)
1024 return;
1025
1026 pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
1027 pr_info("... version: %d\n", x86_pmu.version);
1028 pr_info("... bit width: %d\n", x86_pmu.counter_bits);
1029
1030 pr_info("... num counters: %d\n", x86_pmu.num_counters);
1031 if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
1032 x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
1033 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
1034 x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
1035 }
1036 perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
1037 perf_max_counters = x86_pmu.num_counters;
1038
1039 pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
1040 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
1041
1042 if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
1043 x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
1044 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
1045 x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
1046 }
1047 pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
1048
1049 perf_counter_mask |=
1050 ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
1051
1052 pr_info("... counter mask: %016Lx\n", perf_counter_mask);
1053
1054 perf_counters_lapic_init(0);
1055 register_die_notifier(&perf_counter_nmi_notifier);
1056 }
1057
1058 static inline void x86_pmu_read(struct perf_counter *counter)
1059 {
1060 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
1061 }
1062
1063 static const struct pmu pmu = {
1064 .enable = x86_pmu_enable,
1065 .disable = x86_pmu_disable,
1066 .read = x86_pmu_read,
1067 };
1068
1069 const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
1070 {
1071 int err;
1072
1073 err = __hw_perf_counter_init(counter);
1074 if (err)
1075 return ERR_PTR(err);
1076
1077 return &pmu;
1078 }
1079
1080 /*
1081 * callchain support
1082 */
1083
1084 static inline
1085 void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
1086 {
1087 if (entry->nr < MAX_STACK_DEPTH)
1088 entry->ip[entry->nr++] = ip;
1089 }
1090
1091 static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
1092 static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
1093
1094
1095 static void
1096 backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1097 {
1098 /* Ignore warnings */
1099 }
1100
1101 static void backtrace_warning(void *data, char *msg)
1102 {
1103 /* Ignore warnings */
1104 }
1105
1106 static int backtrace_stack(void *data, char *name)
1107 {
1108 /* Don't bother with IRQ stacks for now */
1109 return -1;
1110 }
1111
1112 static void backtrace_address(void *data, unsigned long addr, int reliable)
1113 {
1114 struct perf_callchain_entry *entry = data;
1115
1116 if (reliable)
1117 callchain_store(entry, addr);
1118 }
1119
1120 static const struct stacktrace_ops backtrace_ops = {
1121 .warning = backtrace_warning,
1122 .warning_symbol = backtrace_warning_symbol,
1123 .stack = backtrace_stack,
1124 .address = backtrace_address,
1125 };
1126
1127 static void
1128 perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1129 {
1130 unsigned long bp;
1131 char *stack;
1132 int nr = entry->nr;
1133
1134 callchain_store(entry, instruction_pointer(regs));
1135
1136 stack = ((char *)regs + sizeof(struct pt_regs));
1137 #ifdef CONFIG_FRAME_POINTER
1138 bp = frame_pointer(regs);
1139 #else
1140 bp = 0;
1141 #endif
1142
1143 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
1144
1145 entry->kernel = entry->nr - nr;
1146 }
1147
1148
1149 struct stack_frame {
1150 const void __user *next_fp;
1151 unsigned long return_address;
1152 };
1153
1154 static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
1155 {
1156 int ret;
1157
1158 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
1159 return 0;
1160
1161 ret = 1;
1162 pagefault_disable();
1163 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
1164 ret = 0;
1165 pagefault_enable();
1166
1167 return ret;
1168 }
1169
1170 static void
1171 perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1172 {
1173 struct stack_frame frame;
1174 const void __user *fp;
1175 int nr = entry->nr;
1176
1177 regs = (struct pt_regs *)current->thread.sp0 - 1;
1178 fp = (void __user *)regs->bp;
1179
1180 callchain_store(entry, regs->ip);
1181
1182 while (entry->nr < MAX_STACK_DEPTH) {
1183 frame.next_fp = NULL;
1184 frame.return_address = 0;
1185
1186 if (!copy_stack_frame(fp, &frame))
1187 break;
1188
1189 if ((unsigned long)fp < user_stack_pointer(regs))
1190 break;
1191
1192 callchain_store(entry, frame.return_address);
1193 fp = frame.next_fp;
1194 }
1195
1196 entry->user = entry->nr - nr;
1197 }
1198
1199 static void
1200 perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1201 {
1202 int is_user;
1203
1204 if (!regs)
1205 return;
1206
1207 is_user = user_mode(regs);
1208
1209 if (!current || current->pid == 0)
1210 return;
1211
1212 if (is_user && current->state != TASK_RUNNING)
1213 return;
1214
1215 if (!is_user)
1216 perf_callchain_kernel(regs, entry);
1217
1218 if (current->mm)
1219 perf_callchain_user(regs, entry);
1220 }
1221
1222 struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1223 {
1224 struct perf_callchain_entry *entry;
1225
1226 if (in_nmi())
1227 entry = &__get_cpu_var(nmi_entry);
1228 else
1229 entry = &__get_cpu_var(irq_entry);
1230
1231 entry->nr = 0;
1232 entry->hv = 0;
1233 entry->kernel = 0;
1234 entry->user = 0;
1235
1236 perf_do_callchain(regs, entry);
1237
1238 return entry;
1239 }