]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/cpu/perf_counter.c
perfcounters: fix init context lock
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / cpu / perf_counter.c
CommitLineData
241771ef
IM
1/*
2 * Performance counter x86 architecture code
3 *
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6 *
7 * For licencing details see kernel-base/COPYING
8 */
9
10#include <linux/perf_counter.h>
11#include <linux/capability.h>
12#include <linux/notifier.h>
13#include <linux/hardirq.h>
14#include <linux/kprobes.h>
4ac13294 15#include <linux/module.h>
241771ef
IM
16#include <linux/kdebug.h>
17#include <linux/sched.h>
18
5c167b85 19#include <asm/perf_counter.h>
241771ef
IM
20#include <asm/apic.h>
21
22static bool perf_counters_initialized __read_mostly;
23
24/*
25 * Number of (generic) HW counters:
26 */
862a1a5f
IM
27static int nr_counters_generic __read_mostly;
28static u64 perf_counter_mask __read_mostly;
241771ef 29
862a1a5f 30static int nr_counters_fixed __read_mostly;
703e937c 31
241771ef 32struct cpu_hw_counters {
862a1a5f
IM
33 struct perf_counter *counters[X86_PMC_IDX_MAX];
34 unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
241771ef
IM
35};
36
37/*
38 * Intel PerfMon v3. Used on Core2 and later.
39 */
40static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters);
41
94c46572 42static const int intel_perfmon_event_map[] =
241771ef
IM
43{
44 [PERF_COUNT_CYCLES] = 0x003c,
45 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
46 [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
47 [PERF_COUNT_CACHE_MISSES] = 0x412e,
48 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
49 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
50};
51
94c46572 52static const int max_intel_perfmon_events = ARRAY_SIZE(intel_perfmon_event_map);
241771ef 53
ee06094f
IM
54/*
55 * Propagate counter elapsed time into the generic counter.
56 * Can only be executed on the CPU where the counter is active.
57 * Returns the delta events processed.
58 */
59static void
60x86_perf_counter_update(struct perf_counter *counter,
61 struct hw_perf_counter *hwc, int idx)
62{
63 u64 prev_raw_count, new_raw_count, delta;
64
ee06094f
IM
65 /*
66 * Careful: an NMI might modify the previous counter value.
67 *
68 * Our tactic to handle this is to first atomically read and
69 * exchange a new raw count - then add that new-prev delta
70 * count to the generic counter atomically:
71 */
72again:
73 prev_raw_count = atomic64_read(&hwc->prev_count);
74 rdmsrl(hwc->counter_base + idx, new_raw_count);
75
76 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
77 new_raw_count) != prev_raw_count)
78 goto again;
79
80 /*
81 * Now we have the new raw value and have updated the prev
82 * timestamp already. We can now calculate the elapsed delta
83 * (counter-)time and add that to the generic counter.
84 *
85 * Careful, not all hw sign-extends above the physical width
86 * of the count, so we do that by clipping the delta to 32 bits:
87 */
88 delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
ee06094f
IM
89
90 atomic64_add(delta, &counter->count);
91 atomic64_sub(delta, &hwc->period_left);
92}
93
241771ef
IM
94/*
95 * Setup the hardware configuration for a given hw_event_type
96 */
621a01ea 97static int __hw_perf_counter_init(struct perf_counter *counter)
241771ef 98{
9f66a381 99 struct perf_counter_hw_event *hw_event = &counter->hw_event;
241771ef
IM
100 struct hw_perf_counter *hwc = &counter->hw;
101
102 if (unlikely(!perf_counters_initialized))
103 return -EINVAL;
104
105 /*
106 * Count user events, and generate PMC IRQs:
107 * (keep 'enabled' bit clear for now)
108 */
109 hwc->config = ARCH_PERFMON_EVENTSEL_USR | ARCH_PERFMON_EVENTSEL_INT;
110
111 /*
112 * If privileged enough, count OS events too, and allow
113 * NMI events as well:
114 */
115 hwc->nmi = 0;
116 if (capable(CAP_SYS_ADMIN)) {
117 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
9f66a381 118 if (hw_event->nmi)
241771ef
IM
119 hwc->nmi = 1;
120 }
121
9f66a381
IM
122 hwc->config_base = MSR_ARCH_PERFMON_EVENTSEL0;
123 hwc->counter_base = MSR_ARCH_PERFMON_PERFCTR0;
241771ef 124
9f66a381 125 hwc->irq_period = hw_event->irq_period;
241771ef
IM
126 /*
127 * Intel PMCs cannot be accessed sanely above 32 bit width,
128 * so we install an artificial 1<<31 period regardless of
129 * the generic counter period:
130 */
ee06094f 131 if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
241771ef
IM
132 hwc->irq_period = 0x7FFFFFFF;
133
ee06094f 134 atomic64_set(&hwc->period_left, hwc->irq_period);
241771ef
IM
135
136 /*
dfa7c899 137 * Raw event type provide the config in the event structure
241771ef 138 */
9f66a381
IM
139 if (hw_event->raw) {
140 hwc->config |= hw_event->type;
241771ef 141 } else {
9f66a381 142 if (hw_event->type >= max_intel_perfmon_events)
241771ef
IM
143 return -EINVAL;
144 /*
145 * The generic map:
146 */
9f66a381 147 hwc->config |= intel_perfmon_event_map[hw_event->type];
241771ef 148 }
241771ef
IM
149 counter->wakeup_pending = 0;
150
151 return 0;
152}
153
241771ef
IM
154void hw_perf_enable_all(void)
155{
2b9ff0db
IM
156 if (unlikely(!perf_counters_initialized))
157 return;
158
862a1a5f 159 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, perf_counter_mask);
241771ef
IM
160}
161
01b2838c 162u64 hw_perf_save_disable(void)
4ac13294
TG
163{
164 u64 ctrl;
165
2b9ff0db
IM
166 if (unlikely(!perf_counters_initialized))
167 return 0;
168
4ac13294 169 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
862a1a5f 170 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
2b9ff0db 171
4ac13294 172 return ctrl;
241771ef 173}
01b2838c 174EXPORT_SYMBOL_GPL(hw_perf_save_disable);
241771ef 175
ee06094f
IM
176void hw_perf_restore(u64 ctrl)
177{
2b9ff0db
IM
178 if (unlikely(!perf_counters_initialized))
179 return;
180
862a1a5f 181 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
ee06094f
IM
182}
183EXPORT_SYMBOL_GPL(hw_perf_restore);
184
7e2ae347 185static inline void
eb2b8618 186__pmc_generic_disable(struct perf_counter *counter,
ee06094f 187 struct hw_perf_counter *hwc, unsigned int idx)
7e2ae347 188{
ee06094f
IM
189 int err;
190
191 err = wrmsr_safe(hwc->config_base + idx, hwc->config, 0);
7e2ae347
IM
192}
193
eb2b8618 194static DEFINE_PER_CPU(u64, prev_left[X86_PMC_MAX_GENERIC]);
241771ef 195
ee06094f
IM
196/*
197 * Set the next IRQ period, based on the hwc->period_left value.
198 * To be called with the counter disabled in hw:
199 */
200static void
201__hw_perf_counter_set_period(struct perf_counter *counter,
202 struct hw_perf_counter *hwc, int idx)
241771ef 203{
ee06094f
IM
204 s32 left = atomic64_read(&hwc->period_left);
205 s32 period = hwc->irq_period;
206
ee06094f
IM
207 /*
208 * If we are way outside a reasoable range then just skip forward:
209 */
210 if (unlikely(left <= -period)) {
211 left = period;
212 atomic64_set(&hwc->period_left, left);
213 }
214
215 if (unlikely(left <= 0)) {
216 left += period;
217 atomic64_set(&hwc->period_left, left);
218 }
241771ef 219
ee06094f
IM
220 per_cpu(prev_left[idx], smp_processor_id()) = left;
221
222 /*
223 * The hw counter starts counting from this counter offset,
224 * mark it to be able to extra future deltas:
225 */
226 atomic64_set(&hwc->prev_count, (u64)(s64)-left);
227
228 wrmsr(hwc->counter_base + idx, -left, 0);
7e2ae347
IM
229}
230
ee06094f 231static void
eb2b8618 232__pmc_generic_enable(struct perf_counter *counter,
ee06094f 233 struct hw_perf_counter *hwc, int idx)
7e2ae347
IM
234{
235 wrmsr(hwc->config_base + idx,
236 hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE, 0);
241771ef
IM
237}
238
862a1a5f
IM
239static int fixed_mode_idx(struct hw_perf_counter *hwc)
240{
241 return -1;
242}
243
ee06094f
IM
244/*
245 * Find a PMC slot for the freshly enabled / scheduled in counter:
246 */
eb2b8618 247static void pmc_generic_enable(struct perf_counter *counter)
241771ef
IM
248{
249 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
250 struct hw_perf_counter *hwc = &counter->hw;
251 int idx = hwc->idx;
252
253 /* Try to get the previous counter again */
254 if (test_and_set_bit(idx, cpuc->used)) {
862a1a5f 255 idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
241771ef
IM
256 set_bit(idx, cpuc->used);
257 hwc->idx = idx;
258 }
259
260 perf_counters_lapic_init(hwc->nmi);
261
eb2b8618 262 __pmc_generic_disable(counter, hwc, idx);
241771ef 263
862a1a5f 264 cpuc->counters[idx] = counter;
7e2ae347 265
ee06094f 266 __hw_perf_counter_set_period(counter, hwc, idx);
eb2b8618 267 __pmc_generic_enable(counter, hwc, idx);
241771ef
IM
268}
269
270void perf_counter_print_debug(void)
271{
ee06094f 272 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left;
1e125676
IM
273 int cpu, idx;
274
862a1a5f 275 if (!nr_counters_generic)
1e125676 276 return;
241771ef
IM
277
278 local_irq_disable();
279
280 cpu = smp_processor_id();
281
1e125676
IM
282 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
283 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
284 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
241771ef
IM
285
286 printk(KERN_INFO "\n");
287 printk(KERN_INFO "CPU#%d: ctrl: %016llx\n", cpu, ctrl);
288 printk(KERN_INFO "CPU#%d: status: %016llx\n", cpu, status);
289 printk(KERN_INFO "CPU#%d: overflow: %016llx\n", cpu, overflow);
290
862a1a5f 291 for (idx = 0; idx < nr_counters_generic; idx++) {
1e125676
IM
292 rdmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, pmc_ctrl);
293 rdmsrl(MSR_ARCH_PERFMON_PERFCTR0 + idx, pmc_count);
241771ef 294
ee06094f 295 prev_left = per_cpu(prev_left[idx], cpu);
241771ef
IM
296
297 printk(KERN_INFO "CPU#%d: PMC%d ctrl: %016llx\n",
298 cpu, idx, pmc_ctrl);
299 printk(KERN_INFO "CPU#%d: PMC%d count: %016llx\n",
300 cpu, idx, pmc_count);
ee06094f
IM
301 printk(KERN_INFO "CPU#%d: PMC%d left: %016llx\n",
302 cpu, idx, prev_left);
241771ef
IM
303 }
304 local_irq_enable();
305}
306
eb2b8618 307static void pmc_generic_disable(struct perf_counter *counter)
241771ef
IM
308{
309 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
310 struct hw_perf_counter *hwc = &counter->hw;
311 unsigned int idx = hwc->idx;
312
eb2b8618 313 __pmc_generic_disable(counter, hwc, idx);
241771ef
IM
314
315 clear_bit(idx, cpuc->used);
862a1a5f 316 cpuc->counters[idx] = NULL;
241771ef 317
ee06094f
IM
318 /*
319 * Drain the remaining delta count out of a counter
320 * that we are disabling:
321 */
322 x86_perf_counter_update(counter, hwc, idx);
241771ef
IM
323}
324
325static void perf_store_irq_data(struct perf_counter *counter, u64 data)
326{
327 struct perf_data *irqdata = counter->irqdata;
328
329 if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
330 irqdata->overrun++;
331 } else {
332 u64 *p = (u64 *) &irqdata->data[irqdata->len];
333
334 *p = data;
335 irqdata->len += sizeof(u64);
336 }
337}
338
7e2ae347 339/*
ee06094f
IM
340 * Save and restart an expired counter. Called by NMI contexts,
341 * so it has to be careful about preempting normal counter ops:
7e2ae347 342 */
241771ef
IM
343static void perf_save_and_restart(struct perf_counter *counter)
344{
345 struct hw_perf_counter *hwc = &counter->hw;
346 int idx = hwc->idx;
7e2ae347 347 u64 pmc_ctrl;
241771ef 348
1e125676 349 rdmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, pmc_ctrl);
241771ef 350
ee06094f
IM
351 x86_perf_counter_update(counter, hwc, idx);
352 __hw_perf_counter_set_period(counter, hwc, idx);
7e2ae347
IM
353
354 if (pmc_ctrl & ARCH_PERFMON_EVENTSEL0_ENABLE)
eb2b8618 355 __pmc_generic_enable(counter, hwc, idx);
241771ef
IM
356}
357
358static void
04289bb9 359perf_handle_group(struct perf_counter *sibling, u64 *status, u64 *overflown)
241771ef 360{
04289bb9 361 struct perf_counter *counter, *group_leader = sibling->group_leader;
241771ef 362
04289bb9 363 /*
ee06094f 364 * Store sibling timestamps (if any):
04289bb9
IM
365 */
366 list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
ee06094f 367 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
04289bb9 368 perf_store_irq_data(sibling, counter->hw_event.type);
ee06094f 369 perf_store_irq_data(sibling, atomic64_read(&counter->count));
241771ef
IM
370 }
371}
372
373/*
374 * This handler is triggered by the local APIC, so the APIC IRQ handling
375 * rules apply:
376 */
377static void __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi)
378{
379 int bit, cpu = smp_processor_id();
43874d23 380 u64 ack, status, saved_global;
241771ef 381 struct cpu_hw_counters *cpuc;
43874d23
IM
382
383 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, saved_global);
241771ef 384
241771ef 385 /* Disable counters globally */
862a1a5f 386 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
241771ef
IM
387 ack_APIC_irq();
388
389 cpuc = &per_cpu(cpu_hw_counters, cpu);
390
87b9cf46
IM
391 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
392 if (!status)
393 goto out;
394
241771ef
IM
395again:
396 ack = status;
862a1a5f
IM
397 for_each_bit(bit, (unsigned long *) &status, nr_counters_generic) {
398 struct perf_counter *counter = cpuc->counters[bit];
241771ef
IM
399
400 clear_bit(bit, (unsigned long *) &status);
401 if (!counter)
402 continue;
403
404 perf_save_and_restart(counter);
405
9f66a381 406 switch (counter->hw_event.record_type) {
241771ef
IM
407 case PERF_RECORD_SIMPLE:
408 continue;
409 case PERF_RECORD_IRQ:
410 perf_store_irq_data(counter, instruction_pointer(regs));
411 break;
412 case PERF_RECORD_GROUP:
241771ef
IM
413 perf_handle_group(counter, &status, &ack);
414 break;
415 }
416 /*
417 * From NMI context we cannot call into the scheduler to
eb2b8618 418 * do a task wakeup - but we mark these generic as
241771ef
IM
419 * wakeup_pending and initate a wakeup callback:
420 */
421 if (nmi) {
422 counter->wakeup_pending = 1;
423 set_tsk_thread_flag(current, TIF_PERF_COUNTERS);
424 } else {
425 wake_up(&counter->waitq);
426 }
427 }
428
862a1a5f 429 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
241771ef
IM
430
431 /*
432 * Repeat if there is more work to be done:
433 */
434 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
435 if (status)
436 goto again;
87b9cf46 437out:
241771ef 438 /*
43874d23 439 * Restore - do not reenable when global enable is off:
241771ef 440 */
862a1a5f 441 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, saved_global);
241771ef
IM
442}
443
444void smp_perf_counter_interrupt(struct pt_regs *regs)
445{
446 irq_enter();
92bf73e9 447 inc_irq_stat(apic_perf_irqs);
241771ef
IM
448 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
449 __smp_perf_counter_interrupt(regs, 0);
450
451 irq_exit();
452}
453
454/*
455 * This handler is triggered by NMI contexts:
456 */
457void perf_counter_notify(struct pt_regs *regs)
458{
459 struct cpu_hw_counters *cpuc;
460 unsigned long flags;
461 int bit, cpu;
462
463 local_irq_save(flags);
464 cpu = smp_processor_id();
465 cpuc = &per_cpu(cpu_hw_counters, cpu);
466
862a1a5f
IM
467 for_each_bit(bit, cpuc->used, X86_PMC_IDX_MAX) {
468 struct perf_counter *counter = cpuc->counters[bit];
241771ef
IM
469
470 if (!counter)
471 continue;
472
473 if (counter->wakeup_pending) {
474 counter->wakeup_pending = 0;
475 wake_up(&counter->waitq);
476 }
477 }
478
479 local_irq_restore(flags);
480}
481
482void __cpuinit perf_counters_lapic_init(int nmi)
483{
484 u32 apic_val;
485
486 if (!perf_counters_initialized)
487 return;
488 /*
489 * Enable the performance counter vector in the APIC LVT:
490 */
491 apic_val = apic_read(APIC_LVTERR);
492
493 apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
494 if (nmi)
495 apic_write(APIC_LVTPC, APIC_DM_NMI);
496 else
497 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
498 apic_write(APIC_LVTERR, apic_val);
499}
500
501static int __kprobes
502perf_counter_nmi_handler(struct notifier_block *self,
503 unsigned long cmd, void *__args)
504{
505 struct die_args *args = __args;
506 struct pt_regs *regs;
507
508 if (likely(cmd != DIE_NMI_IPI))
509 return NOTIFY_DONE;
510
511 regs = args->regs;
512
513 apic_write(APIC_LVTPC, APIC_DM_NMI);
514 __smp_perf_counter_interrupt(regs, 1);
515
516 return NOTIFY_STOP;
517}
518
519static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
520 .notifier_call = perf_counter_nmi_handler
521};
522
523void __init init_hw_perf_counters(void)
524{
525 union cpuid10_eax eax;
241771ef 526 unsigned int ebx;
703e937c
IM
527 unsigned int unused;
528 union cpuid10_edx edx;
241771ef
IM
529
530 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
531 return;
532
533 /*
534 * Check whether the Architectural PerfMon supports
535 * Branch Misses Retired Event or not.
536 */
703e937c 537 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
241771ef
IM
538 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
539 return;
540
541 printk(KERN_INFO "Intel Performance Monitoring support detected.\n");
542
703e937c
IM
543 printk(KERN_INFO "... version: %d\n", eax.split.version_id);
544 printk(KERN_INFO "... num counters: %d\n", eax.split.num_counters);
862a1a5f
IM
545 nr_counters_generic = eax.split.num_counters;
546 if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
547 nr_counters_generic = X86_PMC_MAX_GENERIC;
241771ef 548 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
862a1a5f 549 nr_counters_generic, X86_PMC_MAX_GENERIC);
241771ef 550 }
862a1a5f
IM
551 perf_counter_mask = (1 << nr_counters_generic) - 1;
552 perf_max_counters = nr_counters_generic;
241771ef 553
703e937c
IM
554 printk(KERN_INFO "... bit width: %d\n", eax.split.bit_width);
555 printk(KERN_INFO "... mask length: %d\n", eax.split.mask_length);
556
862a1a5f
IM
557 nr_counters_fixed = edx.split.num_counters_fixed;
558 if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
559 nr_counters_fixed = X86_PMC_MAX_FIXED;
703e937c 560 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
862a1a5f 561 nr_counters_fixed, X86_PMC_MAX_FIXED);
703e937c 562 }
862a1a5f
IM
563 printk(KERN_INFO "... fixed counters: %d\n", nr_counters_fixed);
564
565 perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
241771ef 566
862a1a5f 567 printk(KERN_INFO "... counter mask: %016Lx\n", perf_counter_mask);
75f224cf
IM
568 perf_counters_initialized = true;
569
241771ef
IM
570 perf_counters_lapic_init(0);
571 register_die_notifier(&perf_counter_nmi_notifier);
241771ef 572}
621a01ea 573
eb2b8618 574static void pmc_generic_read(struct perf_counter *counter)
ee06094f
IM
575{
576 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
577}
578
5c92d124 579static const struct hw_perf_counter_ops x86_perf_counter_ops = {
7671581f
IM
580 .enable = pmc_generic_enable,
581 .disable = pmc_generic_disable,
582 .read = pmc_generic_read,
621a01ea
IM
583};
584
5c92d124
IM
585const struct hw_perf_counter_ops *
586hw_perf_counter_init(struct perf_counter *counter)
621a01ea
IM
587{
588 int err;
589
590 err = __hw_perf_counter_init(counter);
591 if (err)
592 return NULL;
593
594 return &x86_perf_counter_ops;
595}