perfcounters: enable lowlevel pmc code to schedule counters
[deliverable/linux.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 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>
15 #include <linux/module.h>
16 #include <linux/kdebug.h>
17 #include <linux/sched.h>
18
19 #include <asm/perf_counter.h>
20 #include <asm/apic.h>
21
22 static bool perf_counters_initialized __read_mostly;
23
24 /*
25 * Number of (generic) HW counters:
26 */
27 static int nr_counters_generic __read_mostly;
28 static u64 perf_counter_mask __read_mostly;
29
30 static int nr_counters_fixed __read_mostly;
31
32 struct cpu_hw_counters {
33 struct perf_counter *counters[X86_PMC_IDX_MAX];
34 unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
35 };
36
37 /*
38 * Intel PerfMon v3. Used on Core2 and later.
39 */
40 static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters);
41
42 static const int intel_perfmon_event_map[] =
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
52 static const int max_intel_perfmon_events = ARRAY_SIZE(intel_perfmon_event_map);
53
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 */
59 static void
60 x86_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
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 */
72 again:
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);
89
90 atomic64_add(delta, &counter->count);
91 atomic64_sub(delta, &hwc->period_left);
92 }
93
94 /*
95 * Setup the hardware configuration for a given hw_event_type
96 */
97 static int __hw_perf_counter_init(struct perf_counter *counter)
98 {
99 struct perf_counter_hw_event *hw_event = &counter->hw_event;
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;
118 if (hw_event->nmi)
119 hwc->nmi = 1;
120 }
121
122 hwc->config_base = MSR_ARCH_PERFMON_EVENTSEL0;
123 hwc->counter_base = MSR_ARCH_PERFMON_PERFCTR0;
124
125 hwc->irq_period = hw_event->irq_period;
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 */
131 if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
132 hwc->irq_period = 0x7FFFFFFF;
133
134 atomic64_set(&hwc->period_left, hwc->irq_period);
135
136 /*
137 * Raw event type provide the config in the event structure
138 */
139 if (hw_event->raw) {
140 hwc->config |= hw_event->type;
141 } else {
142 if (hw_event->type >= max_intel_perfmon_events)
143 return -EINVAL;
144 /*
145 * The generic map:
146 */
147 hwc->config |= intel_perfmon_event_map[hw_event->type];
148 }
149 counter->wakeup_pending = 0;
150
151 return 0;
152 }
153
154 void hw_perf_enable_all(void)
155 {
156 if (unlikely(!perf_counters_initialized))
157 return;
158
159 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, perf_counter_mask);
160 }
161
162 u64 hw_perf_save_disable(void)
163 {
164 u64 ctrl;
165
166 if (unlikely(!perf_counters_initialized))
167 return 0;
168
169 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
170 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
171
172 return ctrl;
173 }
174 EXPORT_SYMBOL_GPL(hw_perf_save_disable);
175
176 void hw_perf_restore(u64 ctrl)
177 {
178 if (unlikely(!perf_counters_initialized))
179 return;
180
181 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
182 }
183 EXPORT_SYMBOL_GPL(hw_perf_restore);
184
185 static inline void
186 __pmc_generic_disable(struct perf_counter *counter,
187 struct hw_perf_counter *hwc, unsigned int idx)
188 {
189 int err;
190
191 err = wrmsr_safe(hwc->config_base + idx, hwc->config, 0);
192 }
193
194 static DEFINE_PER_CPU(u64, prev_left[X86_PMC_MAX_GENERIC]);
195
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 */
200 static void
201 __hw_perf_counter_set_period(struct perf_counter *counter,
202 struct hw_perf_counter *hwc, int idx)
203 {
204 s32 left = atomic64_read(&hwc->period_left);
205 s32 period = hwc->irq_period;
206
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 }
219
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);
229 }
230
231 static void
232 __pmc_generic_enable(struct perf_counter *counter,
233 struct hw_perf_counter *hwc, int idx)
234 {
235 wrmsr(hwc->config_base + idx,
236 hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE, 0);
237 }
238
239 static int fixed_mode_idx(struct hw_perf_counter *hwc)
240 {
241 return -1;
242 }
243
244 /*
245 * Find a PMC slot for the freshly enabled / scheduled in counter:
246 */
247 static int pmc_generic_enable(struct perf_counter *counter)
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)) {
255 idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
256 if (idx == nr_counters_generic)
257 return -EAGAIN;
258 set_bit(idx, cpuc->used);
259 hwc->idx = idx;
260 }
261
262 perf_counters_lapic_init(hwc->nmi);
263
264 __pmc_generic_disable(counter, hwc, idx);
265
266 cpuc->counters[idx] = counter;
267
268 __hw_perf_counter_set_period(counter, hwc, idx);
269 __pmc_generic_enable(counter, hwc, idx);
270
271 return 0;
272 }
273
274 void perf_counter_print_debug(void)
275 {
276 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left;
277 int cpu, idx;
278
279 if (!nr_counters_generic)
280 return;
281
282 local_irq_disable();
283
284 cpu = smp_processor_id();
285
286 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
287 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
288 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
289
290 printk(KERN_INFO "\n");
291 printk(KERN_INFO "CPU#%d: ctrl: %016llx\n", cpu, ctrl);
292 printk(KERN_INFO "CPU#%d: status: %016llx\n", cpu, status);
293 printk(KERN_INFO "CPU#%d: overflow: %016llx\n", cpu, overflow);
294
295 for (idx = 0; idx < nr_counters_generic; idx++) {
296 rdmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, pmc_ctrl);
297 rdmsrl(MSR_ARCH_PERFMON_PERFCTR0 + idx, pmc_count);
298
299 prev_left = per_cpu(prev_left[idx], cpu);
300
301 printk(KERN_INFO "CPU#%d: PMC%d ctrl: %016llx\n",
302 cpu, idx, pmc_ctrl);
303 printk(KERN_INFO "CPU#%d: PMC%d count: %016llx\n",
304 cpu, idx, pmc_count);
305 printk(KERN_INFO "CPU#%d: PMC%d left: %016llx\n",
306 cpu, idx, prev_left);
307 }
308 local_irq_enable();
309 }
310
311 static void pmc_generic_disable(struct perf_counter *counter)
312 {
313 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
314 struct hw_perf_counter *hwc = &counter->hw;
315 unsigned int idx = hwc->idx;
316
317 __pmc_generic_disable(counter, hwc, idx);
318
319 clear_bit(idx, cpuc->used);
320 cpuc->counters[idx] = NULL;
321
322 /*
323 * Drain the remaining delta count out of a counter
324 * that we are disabling:
325 */
326 x86_perf_counter_update(counter, hwc, idx);
327 }
328
329 static void perf_store_irq_data(struct perf_counter *counter, u64 data)
330 {
331 struct perf_data *irqdata = counter->irqdata;
332
333 if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
334 irqdata->overrun++;
335 } else {
336 u64 *p = (u64 *) &irqdata->data[irqdata->len];
337
338 *p = data;
339 irqdata->len += sizeof(u64);
340 }
341 }
342
343 /*
344 * Save and restart an expired counter. Called by NMI contexts,
345 * so it has to be careful about preempting normal counter ops:
346 */
347 static void perf_save_and_restart(struct perf_counter *counter)
348 {
349 struct hw_perf_counter *hwc = &counter->hw;
350 int idx = hwc->idx;
351 u64 pmc_ctrl;
352
353 rdmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, pmc_ctrl);
354
355 x86_perf_counter_update(counter, hwc, idx);
356 __hw_perf_counter_set_period(counter, hwc, idx);
357
358 if (pmc_ctrl & ARCH_PERFMON_EVENTSEL0_ENABLE)
359 __pmc_generic_enable(counter, hwc, idx);
360 }
361
362 static void
363 perf_handle_group(struct perf_counter *sibling, u64 *status, u64 *overflown)
364 {
365 struct perf_counter *counter, *group_leader = sibling->group_leader;
366
367 /*
368 * Store sibling timestamps (if any):
369 */
370 list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
371 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
372 perf_store_irq_data(sibling, counter->hw_event.type);
373 perf_store_irq_data(sibling, atomic64_read(&counter->count));
374 }
375 }
376
377 /*
378 * This handler is triggered by the local APIC, so the APIC IRQ handling
379 * rules apply:
380 */
381 static void __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi)
382 {
383 int bit, cpu = smp_processor_id();
384 u64 ack, status, saved_global;
385 struct cpu_hw_counters *cpuc;
386
387 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, saved_global);
388
389 /* Disable counters globally */
390 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
391 ack_APIC_irq();
392
393 cpuc = &per_cpu(cpu_hw_counters, cpu);
394
395 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
396 if (!status)
397 goto out;
398
399 again:
400 ack = status;
401 for_each_bit(bit, (unsigned long *) &status, nr_counters_generic) {
402 struct perf_counter *counter = cpuc->counters[bit];
403
404 clear_bit(bit, (unsigned long *) &status);
405 if (!counter)
406 continue;
407
408 perf_save_and_restart(counter);
409
410 switch (counter->hw_event.record_type) {
411 case PERF_RECORD_SIMPLE:
412 continue;
413 case PERF_RECORD_IRQ:
414 perf_store_irq_data(counter, instruction_pointer(regs));
415 break;
416 case PERF_RECORD_GROUP:
417 perf_handle_group(counter, &status, &ack);
418 break;
419 }
420 /*
421 * From NMI context we cannot call into the scheduler to
422 * do a task wakeup - but we mark these generic as
423 * wakeup_pending and initate a wakeup callback:
424 */
425 if (nmi) {
426 counter->wakeup_pending = 1;
427 set_tsk_thread_flag(current, TIF_PERF_COUNTERS);
428 } else {
429 wake_up(&counter->waitq);
430 }
431 }
432
433 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
434
435 /*
436 * Repeat if there is more work to be done:
437 */
438 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
439 if (status)
440 goto again;
441 out:
442 /*
443 * Restore - do not reenable when global enable is off:
444 */
445 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, saved_global);
446 }
447
448 void smp_perf_counter_interrupt(struct pt_regs *regs)
449 {
450 irq_enter();
451 inc_irq_stat(apic_perf_irqs);
452 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
453 __smp_perf_counter_interrupt(regs, 0);
454
455 irq_exit();
456 }
457
458 /*
459 * This handler is triggered by NMI contexts:
460 */
461 void perf_counter_notify(struct pt_regs *regs)
462 {
463 struct cpu_hw_counters *cpuc;
464 unsigned long flags;
465 int bit, cpu;
466
467 local_irq_save(flags);
468 cpu = smp_processor_id();
469 cpuc = &per_cpu(cpu_hw_counters, cpu);
470
471 for_each_bit(bit, cpuc->used, X86_PMC_IDX_MAX) {
472 struct perf_counter *counter = cpuc->counters[bit];
473
474 if (!counter)
475 continue;
476
477 if (counter->wakeup_pending) {
478 counter->wakeup_pending = 0;
479 wake_up(&counter->waitq);
480 }
481 }
482
483 local_irq_restore(flags);
484 }
485
486 void __cpuinit perf_counters_lapic_init(int nmi)
487 {
488 u32 apic_val;
489
490 if (!perf_counters_initialized)
491 return;
492 /*
493 * Enable the performance counter vector in the APIC LVT:
494 */
495 apic_val = apic_read(APIC_LVTERR);
496
497 apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
498 if (nmi)
499 apic_write(APIC_LVTPC, APIC_DM_NMI);
500 else
501 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
502 apic_write(APIC_LVTERR, apic_val);
503 }
504
505 static int __kprobes
506 perf_counter_nmi_handler(struct notifier_block *self,
507 unsigned long cmd, void *__args)
508 {
509 struct die_args *args = __args;
510 struct pt_regs *regs;
511
512 if (likely(cmd != DIE_NMI_IPI))
513 return NOTIFY_DONE;
514
515 regs = args->regs;
516
517 apic_write(APIC_LVTPC, APIC_DM_NMI);
518 __smp_perf_counter_interrupt(regs, 1);
519
520 return NOTIFY_STOP;
521 }
522
523 static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
524 .notifier_call = perf_counter_nmi_handler
525 };
526
527 void __init init_hw_perf_counters(void)
528 {
529 union cpuid10_eax eax;
530 unsigned int ebx;
531 unsigned int unused;
532 union cpuid10_edx edx;
533
534 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
535 return;
536
537 /*
538 * Check whether the Architectural PerfMon supports
539 * Branch Misses Retired Event or not.
540 */
541 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
542 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
543 return;
544
545 printk(KERN_INFO "Intel Performance Monitoring support detected.\n");
546
547 printk(KERN_INFO "... version: %d\n", eax.split.version_id);
548 printk(KERN_INFO "... num counters: %d\n", eax.split.num_counters);
549 nr_counters_generic = eax.split.num_counters;
550 if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
551 nr_counters_generic = X86_PMC_MAX_GENERIC;
552 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
553 nr_counters_generic, X86_PMC_MAX_GENERIC);
554 }
555 perf_counter_mask = (1 << nr_counters_generic) - 1;
556 perf_max_counters = nr_counters_generic;
557
558 printk(KERN_INFO "... bit width: %d\n", eax.split.bit_width);
559 printk(KERN_INFO "... mask length: %d\n", eax.split.mask_length);
560
561 nr_counters_fixed = edx.split.num_counters_fixed;
562 if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
563 nr_counters_fixed = X86_PMC_MAX_FIXED;
564 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
565 nr_counters_fixed, X86_PMC_MAX_FIXED);
566 }
567 printk(KERN_INFO "... fixed counters: %d\n", nr_counters_fixed);
568
569 perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
570
571 printk(KERN_INFO "... counter mask: %016Lx\n", perf_counter_mask);
572 perf_counters_initialized = true;
573
574 perf_counters_lapic_init(0);
575 register_die_notifier(&perf_counter_nmi_notifier);
576 }
577
578 static void pmc_generic_read(struct perf_counter *counter)
579 {
580 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
581 }
582
583 static const struct hw_perf_counter_ops x86_perf_counter_ops = {
584 .enable = pmc_generic_enable,
585 .disable = pmc_generic_disable,
586 .read = pmc_generic_read,
587 };
588
589 const struct hw_perf_counter_ops *
590 hw_perf_counter_init(struct perf_counter *counter)
591 {
592 int err;
593
594 err = __hw_perf_counter_init(counter);
595 if (err)
596 return NULL;
597
598 return &x86_perf_counter_ops;
599 }
This page took 0.042648 seconds and 5 git commands to generate.