2 * Performance counter support - powerpc architecture code
4 * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/perf_counter.h>
14 #include <linux/percpu.h>
15 #include <linux/hardirq.h>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
20 #include <asm/ptrace.h>
22 struct cpu_hw_counters {
29 struct perf_counter *counter[MAX_HWCOUNTERS];
30 u64 events[MAX_HWCOUNTERS];
31 unsigned int flags[MAX_HWCOUNTERS];
32 unsigned long mmcr[3];
33 struct perf_counter *limited_counter[MAX_LIMITED_HWCOUNTERS];
34 u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
36 DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters);
38 struct power_pmu *ppmu;
41 * Normally, to ignore kernel events we set the FCS (freeze counters
42 * in supervisor mode) bit in MMCR0, but if the kernel runs with the
43 * hypervisor bit set in the MSR, or if we are running on a processor
44 * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
45 * then we need to use the FCHV bit to ignore kernel events.
47 static unsigned int freeze_counters_kernel = MMCR0_FCS;
50 * 32-bit doesn't have MMCRA but does have an MMCR2,
51 * and a few other names are different.
56 #define MMCR0_PMCjCE MMCR0_PMCnCE
58 #define SPRN_MMCRA SPRN_MMCR2
59 #define MMCRA_SAMPLE_ENABLE 0
61 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
65 static inline void perf_set_pmu_inuse(int inuse) { }
66 static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
67 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
71 static inline void perf_read_regs(struct pt_regs *regs) { }
72 static inline int perf_intr_is_nmi(struct pt_regs *regs)
77 #endif /* CONFIG_PPC32 */
80 * Things that are specific to 64-bit implementations.
84 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
86 unsigned long mmcra = regs->dsisr;
88 if ((mmcra & MMCRA_SAMPLE_ENABLE) && !(ppmu->flags & PPMU_ALT_SIPR)) {
89 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
91 return 4 * (slot - 1);
96 static inline void perf_set_pmu_inuse(int inuse)
98 get_lppaca()->pmcregs_in_use = inuse;
102 * The user wants a data address recorded.
103 * If we're not doing instruction sampling, give them the SDAR
104 * (sampled data address). If we are doing instruction sampling, then
105 * only give them the SDAR if it corresponds to the instruction
106 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC
109 static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
111 unsigned long mmcra = regs->dsisr;
112 unsigned long sdsync = (ppmu->flags & PPMU_ALT_SIPR) ?
113 POWER6_MMCRA_SDSYNC : MMCRA_SDSYNC;
115 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync))
116 *addrp = mfspr(SPRN_SDAR);
119 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
121 unsigned long mmcra = regs->dsisr;
123 if (TRAP(regs) != 0xf00)
124 return 0; /* not a PMU interrupt */
126 if (ppmu->flags & PPMU_ALT_SIPR) {
127 if (mmcra & POWER6_MMCRA_SIHV)
128 return PERF_EVENT_MISC_HYPERVISOR;
129 return (mmcra & POWER6_MMCRA_SIPR) ?
130 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
132 if (mmcra & MMCRA_SIHV)
133 return PERF_EVENT_MISC_HYPERVISOR;
134 return (mmcra & MMCRA_SIPR) ? PERF_EVENT_MISC_USER :
135 PERF_EVENT_MISC_KERNEL;
139 * Overload regs->dsisr to store MMCRA so we only need to read it once
142 static inline void perf_read_regs(struct pt_regs *regs)
144 regs->dsisr = mfspr(SPRN_MMCRA);
148 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
151 static inline int perf_intr_is_nmi(struct pt_regs *regs)
156 #endif /* CONFIG_PPC64 */
158 static void perf_counter_interrupt(struct pt_regs *regs);
160 void perf_counter_print_debug(void)
165 * Read one performance monitor counter (PMC).
167 static unsigned long read_pmc(int idx)
173 val = mfspr(SPRN_PMC1);
176 val = mfspr(SPRN_PMC2);
179 val = mfspr(SPRN_PMC3);
182 val = mfspr(SPRN_PMC4);
185 val = mfspr(SPRN_PMC5);
188 val = mfspr(SPRN_PMC6);
192 val = mfspr(SPRN_PMC7);
195 val = mfspr(SPRN_PMC8);
197 #endif /* CONFIG_PPC64 */
199 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
208 static void write_pmc(int idx, unsigned long val)
212 mtspr(SPRN_PMC1, val);
215 mtspr(SPRN_PMC2, val);
218 mtspr(SPRN_PMC3, val);
221 mtspr(SPRN_PMC4, val);
224 mtspr(SPRN_PMC5, val);
227 mtspr(SPRN_PMC6, val);
231 mtspr(SPRN_PMC7, val);
234 mtspr(SPRN_PMC8, val);
236 #endif /* CONFIG_PPC64 */
238 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
243 * Check if a set of events can all go on the PMU at once.
244 * If they can't, this will look at alternative codes for the events
245 * and see if any combination of alternative codes is feasible.
246 * The feasible set is returned in event[].
248 static int power_check_constraints(u64 event[], unsigned int cflags[],
251 unsigned long mask, value, nv;
252 u64 alternatives[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
253 unsigned long amasks[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
254 unsigned long avalues[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
255 unsigned long smasks[MAX_HWCOUNTERS], svalues[MAX_HWCOUNTERS];
256 int n_alt[MAX_HWCOUNTERS], choice[MAX_HWCOUNTERS];
258 unsigned long addf = ppmu->add_fields;
259 unsigned long tadd = ppmu->test_adder;
261 if (n_ev > ppmu->n_counter)
264 /* First see if the events will go on as-is */
265 for (i = 0; i < n_ev; ++i) {
266 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
267 && !ppmu->limited_pmc_event(event[i])) {
268 ppmu->get_alternatives(event[i], cflags[i],
270 event[i] = alternatives[i][0];
272 if (ppmu->get_constraint(event[i], &amasks[i][0],
277 for (i = 0; i < n_ev; ++i) {
278 nv = (value | avalues[i][0]) + (value & avalues[i][0] & addf);
279 if ((((nv + tadd) ^ value) & mask) != 0 ||
280 (((nv + tadd) ^ avalues[i][0]) & amasks[i][0]) != 0)
283 mask |= amasks[i][0];
286 return 0; /* all OK */
288 /* doesn't work, gather alternatives... */
289 if (!ppmu->get_alternatives)
291 for (i = 0; i < n_ev; ++i) {
293 n_alt[i] = ppmu->get_alternatives(event[i], cflags[i],
295 for (j = 1; j < n_alt[i]; ++j)
296 ppmu->get_constraint(alternatives[i][j],
297 &amasks[i][j], &avalues[i][j]);
300 /* enumerate all possibilities and see if any will work */
303 value = mask = nv = 0;
306 /* we're backtracking, restore context */
312 * See if any alternative k for event i,
313 * where k > j, will satisfy the constraints.
315 while (++j < n_alt[i]) {
316 nv = (value | avalues[i][j]) +
317 (value & avalues[i][j] & addf);
318 if ((((nv + tadd) ^ value) & mask) == 0 &&
319 (((nv + tadd) ^ avalues[i][j])
320 & amasks[i][j]) == 0)
325 * No feasible alternative, backtrack
326 * to event i-1 and continue enumerating its
327 * alternatives from where we got up to.
333 * Found a feasible alternative for event i,
334 * remember where we got up to with this event,
335 * go on to the next event, and start with
336 * the first alternative for it.
342 mask |= amasks[i][j];
348 /* OK, we have a feasible combination, tell the caller the solution */
349 for (i = 0; i < n_ev; ++i)
350 event[i] = alternatives[i][choice[i]];
355 * Check if newly-added counters have consistent settings for
356 * exclude_{user,kernel,hv} with each other and any previously
359 static int check_excludes(struct perf_counter **ctrs, unsigned int cflags[],
360 int n_prev, int n_new)
362 int eu = 0, ek = 0, eh = 0;
364 struct perf_counter *counter;
371 for (i = 0; i < n; ++i) {
372 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
373 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
378 eu = counter->attr.exclude_user;
379 ek = counter->attr.exclude_kernel;
380 eh = counter->attr.exclude_hv;
382 } else if (counter->attr.exclude_user != eu ||
383 counter->attr.exclude_kernel != ek ||
384 counter->attr.exclude_hv != eh) {
390 for (i = 0; i < n; ++i)
391 if (cflags[i] & PPMU_LIMITED_PMC_OK)
392 cflags[i] |= PPMU_LIMITED_PMC_REQD;
397 static void power_pmu_read(struct perf_counter *counter)
399 s64 val, delta, prev;
401 if (!counter->hw.idx)
404 * Performance monitor interrupts come even when interrupts
405 * are soft-disabled, as long as interrupts are hard-enabled.
406 * Therefore we treat them like NMIs.
409 prev = atomic64_read(&counter->hw.prev_count);
411 val = read_pmc(counter->hw.idx);
412 } while (atomic64_cmpxchg(&counter->hw.prev_count, prev, val) != prev);
414 /* The counters are only 32 bits wide */
415 delta = (val - prev) & 0xfffffffful;
416 atomic64_add(delta, &counter->count);
417 atomic64_sub(delta, &counter->hw.period_left);
421 * On some machines, PMC5 and PMC6 can't be written, don't respect
422 * the freeze conditions, and don't generate interrupts. This tells
423 * us if `counter' is using such a PMC.
425 static int is_limited_pmc(int pmcnum)
427 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
428 && (pmcnum == 5 || pmcnum == 6);
431 static void freeze_limited_counters(struct cpu_hw_counters *cpuhw,
432 unsigned long pmc5, unsigned long pmc6)
434 struct perf_counter *counter;
435 u64 val, prev, delta;
438 for (i = 0; i < cpuhw->n_limited; ++i) {
439 counter = cpuhw->limited_counter[i];
440 if (!counter->hw.idx)
442 val = (counter->hw.idx == 5) ? pmc5 : pmc6;
443 prev = atomic64_read(&counter->hw.prev_count);
445 delta = (val - prev) & 0xfffffffful;
446 atomic64_add(delta, &counter->count);
450 static void thaw_limited_counters(struct cpu_hw_counters *cpuhw,
451 unsigned long pmc5, unsigned long pmc6)
453 struct perf_counter *counter;
457 for (i = 0; i < cpuhw->n_limited; ++i) {
458 counter = cpuhw->limited_counter[i];
459 counter->hw.idx = cpuhw->limited_hwidx[i];
460 val = (counter->hw.idx == 5) ? pmc5 : pmc6;
461 atomic64_set(&counter->hw.prev_count, val);
462 perf_counter_update_userpage(counter);
467 * Since limited counters don't respect the freeze conditions, we
468 * have to read them immediately after freezing or unfreezing the
469 * other counters. We try to keep the values from the limited
470 * counters as consistent as possible by keeping the delay (in
471 * cycles and instructions) between freezing/unfreezing and reading
472 * the limited counters as small and consistent as possible.
473 * Therefore, if any limited counters are in use, we read them
474 * both, and always in the same order, to minimize variability,
475 * and do it inside the same asm that writes MMCR0.
477 static void write_mmcr0(struct cpu_hw_counters *cpuhw, unsigned long mmcr0)
479 unsigned long pmc5, pmc6;
481 if (!cpuhw->n_limited) {
482 mtspr(SPRN_MMCR0, mmcr0);
487 * Write MMCR0, then read PMC5 and PMC6 immediately.
488 * To ensure we don't get a performance monitor interrupt
489 * between writing MMCR0 and freezing/thawing the limited
490 * counters, we first write MMCR0 with the counter overflow
491 * interrupt enable bits turned off.
493 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
494 : "=&r" (pmc5), "=&r" (pmc6)
495 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
497 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
499 if (mmcr0 & MMCR0_FC)
500 freeze_limited_counters(cpuhw, pmc5, pmc6);
502 thaw_limited_counters(cpuhw, pmc5, pmc6);
505 * Write the full MMCR0 including the counter overflow interrupt
506 * enable bits, if necessary.
508 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
509 mtspr(SPRN_MMCR0, mmcr0);
513 * Disable all counters to prevent PMU interrupts and to allow
514 * counters to be added or removed.
516 void hw_perf_disable(void)
518 struct cpu_hw_counters *cpuhw;
521 local_irq_save(flags);
522 cpuhw = &__get_cpu_var(cpu_hw_counters);
524 if (!cpuhw->disabled) {
529 * Check if we ever enabled the PMU on this cpu.
531 if (!cpuhw->pmcs_enabled) {
532 if (ppc_md.enable_pmcs)
533 ppc_md.enable_pmcs();
534 cpuhw->pmcs_enabled = 1;
538 * Disable instruction sampling if it was enabled
540 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
542 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
547 * Set the 'freeze counters' bit.
548 * The barrier is to make sure the mtspr has been
549 * executed and the PMU has frozen the counters
552 write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC);
555 local_irq_restore(flags);
559 * Re-enable all counters if disable == 0.
560 * If we were previously disabled and counters were added, then
561 * put the new config on the PMU.
563 void hw_perf_enable(void)
565 struct perf_counter *counter;
566 struct cpu_hw_counters *cpuhw;
571 unsigned int hwc_index[MAX_HWCOUNTERS];
575 local_irq_save(flags);
576 cpuhw = &__get_cpu_var(cpu_hw_counters);
577 if (!cpuhw->disabled) {
578 local_irq_restore(flags);
584 * If we didn't change anything, or only removed counters,
585 * no need to recalculate MMCR* settings and reset the PMCs.
586 * Just reenable the PMU with the current MMCR* settings
587 * (possibly updated for removal of counters).
589 if (!cpuhw->n_added) {
590 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
591 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
592 if (cpuhw->n_counters == 0)
593 perf_set_pmu_inuse(0);
598 * Compute MMCR* values for the new set of counters
600 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_counters, hwc_index,
602 /* shouldn't ever get here */
603 printk(KERN_ERR "oops compute_mmcr failed\n");
608 * Add in MMCR0 freeze bits corresponding to the
609 * attr.exclude_* bits for the first counter.
610 * We have already checked that all counters have the
611 * same values for these bits as the first counter.
613 counter = cpuhw->counter[0];
614 if (counter->attr.exclude_user)
615 cpuhw->mmcr[0] |= MMCR0_FCP;
616 if (counter->attr.exclude_kernel)
617 cpuhw->mmcr[0] |= freeze_counters_kernel;
618 if (counter->attr.exclude_hv)
619 cpuhw->mmcr[0] |= MMCR0_FCHV;
622 * Write the new configuration to MMCR* with the freeze
623 * bit set and set the hardware counters to their initial values.
624 * Then unfreeze the counters.
626 perf_set_pmu_inuse(1);
627 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
628 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
629 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
633 * Read off any pre-existing counters that need to move
636 for (i = 0; i < cpuhw->n_counters; ++i) {
637 counter = cpuhw->counter[i];
638 if (counter->hw.idx && counter->hw.idx != hwc_index[i] + 1) {
639 power_pmu_read(counter);
640 write_pmc(counter->hw.idx, 0);
646 * Initialize the PMCs for all the new and moved counters.
648 cpuhw->n_limited = n_lim = 0;
649 for (i = 0; i < cpuhw->n_counters; ++i) {
650 counter = cpuhw->counter[i];
653 idx = hwc_index[i] + 1;
654 if (is_limited_pmc(idx)) {
655 cpuhw->limited_counter[n_lim] = counter;
656 cpuhw->limited_hwidx[n_lim] = idx;
661 if (counter->hw.sample_period) {
662 left = atomic64_read(&counter->hw.period_left);
663 if (left < 0x80000000L)
664 val = 0x80000000L - left;
666 atomic64_set(&counter->hw.prev_count, val);
667 counter->hw.idx = idx;
669 perf_counter_update_userpage(counter);
671 cpuhw->n_limited = n_lim;
672 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
676 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
679 * Enable instruction sampling if necessary
681 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
683 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
687 local_irq_restore(flags);
690 static int collect_events(struct perf_counter *group, int max_count,
691 struct perf_counter *ctrs[], u64 *events,
695 struct perf_counter *counter;
697 if (!is_software_counter(group)) {
701 flags[n] = group->hw.counter_base;
702 events[n++] = group->hw.config;
704 list_for_each_entry(counter, &group->sibling_list, list_entry) {
705 if (!is_software_counter(counter) &&
706 counter->state != PERF_COUNTER_STATE_OFF) {
710 flags[n] = counter->hw.counter_base;
711 events[n++] = counter->hw.config;
717 static void counter_sched_in(struct perf_counter *counter, int cpu)
719 counter->state = PERF_COUNTER_STATE_ACTIVE;
720 counter->oncpu = cpu;
721 counter->tstamp_running += counter->ctx->time - counter->tstamp_stopped;
722 if (is_software_counter(counter))
723 counter->pmu->enable(counter);
727 * Called to enable a whole group of counters.
728 * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
729 * Assumes the caller has disabled interrupts and has
730 * frozen the PMU with hw_perf_save_disable.
732 int hw_perf_group_sched_in(struct perf_counter *group_leader,
733 struct perf_cpu_context *cpuctx,
734 struct perf_counter_context *ctx, int cpu)
736 struct cpu_hw_counters *cpuhw;
738 struct perf_counter *sub;
740 cpuhw = &__get_cpu_var(cpu_hw_counters);
741 n0 = cpuhw->n_counters;
742 n = collect_events(group_leader, ppmu->n_counter - n0,
743 &cpuhw->counter[n0], &cpuhw->events[n0],
747 if (check_excludes(cpuhw->counter, cpuhw->flags, n0, n))
749 i = power_check_constraints(cpuhw->events, cpuhw->flags, n + n0);
752 cpuhw->n_counters = n0 + n;
756 * OK, this group can go on; update counter states etc.,
757 * and enable any software counters
759 for (i = n0; i < n0 + n; ++i)
760 cpuhw->counter[i]->hw.config = cpuhw->events[i];
761 cpuctx->active_oncpu += n;
763 counter_sched_in(group_leader, cpu);
764 list_for_each_entry(sub, &group_leader->sibling_list, list_entry) {
765 if (sub->state != PERF_COUNTER_STATE_OFF) {
766 counter_sched_in(sub, cpu);
776 * Add a counter to the PMU.
777 * If all counters are not already frozen, then we disable and
778 * re-enable the PMU in order to get hw_perf_enable to do the
779 * actual work of reconfiguring the PMU.
781 static int power_pmu_enable(struct perf_counter *counter)
783 struct cpu_hw_counters *cpuhw;
788 local_irq_save(flags);
792 * Add the counter to the list (if there is room)
793 * and check whether the total set is still feasible.
795 cpuhw = &__get_cpu_var(cpu_hw_counters);
796 n0 = cpuhw->n_counters;
797 if (n0 >= ppmu->n_counter)
799 cpuhw->counter[n0] = counter;
800 cpuhw->events[n0] = counter->hw.config;
801 cpuhw->flags[n0] = counter->hw.counter_base;
802 if (check_excludes(cpuhw->counter, cpuhw->flags, n0, 1))
804 if (power_check_constraints(cpuhw->events, cpuhw->flags, n0 + 1))
807 counter->hw.config = cpuhw->events[n0];
814 local_irq_restore(flags);
819 * Remove a counter from the PMU.
821 static void power_pmu_disable(struct perf_counter *counter)
823 struct cpu_hw_counters *cpuhw;
827 local_irq_save(flags);
830 power_pmu_read(counter);
832 cpuhw = &__get_cpu_var(cpu_hw_counters);
833 for (i = 0; i < cpuhw->n_counters; ++i) {
834 if (counter == cpuhw->counter[i]) {
835 while (++i < cpuhw->n_counters)
836 cpuhw->counter[i-1] = cpuhw->counter[i];
838 ppmu->disable_pmc(counter->hw.idx - 1, cpuhw->mmcr);
839 if (counter->hw.idx) {
840 write_pmc(counter->hw.idx, 0);
843 perf_counter_update_userpage(counter);
847 for (i = 0; i < cpuhw->n_limited; ++i)
848 if (counter == cpuhw->limited_counter[i])
850 if (i < cpuhw->n_limited) {
851 while (++i < cpuhw->n_limited) {
852 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
853 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
857 if (cpuhw->n_counters == 0) {
858 /* disable exceptions if no counters are running */
859 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
863 local_irq_restore(flags);
867 * Re-enable interrupts on a counter after they were throttled
868 * because they were coming too fast.
870 static void power_pmu_unthrottle(struct perf_counter *counter)
875 if (!counter->hw.idx || !counter->hw.sample_period)
877 local_irq_save(flags);
879 power_pmu_read(counter);
880 left = counter->hw.sample_period;
881 counter->hw.last_period = left;
883 if (left < 0x80000000L)
884 val = 0x80000000L - left;
885 write_pmc(counter->hw.idx, val);
886 atomic64_set(&counter->hw.prev_count, val);
887 atomic64_set(&counter->hw.period_left, left);
888 perf_counter_update_userpage(counter);
890 local_irq_restore(flags);
893 struct pmu power_pmu = {
894 .enable = power_pmu_enable,
895 .disable = power_pmu_disable,
896 .read = power_pmu_read,
897 .unthrottle = power_pmu_unthrottle,
901 * Return 1 if we might be able to put counter on a limited PMC,
903 * A counter can only go on a limited PMC if it counts something
904 * that a limited PMC can count, doesn't require interrupts, and
905 * doesn't exclude any processor mode.
907 static int can_go_on_limited_pmc(struct perf_counter *counter, u64 ev,
911 u64 alt[MAX_EVENT_ALTERNATIVES];
913 if (counter->attr.exclude_user
914 || counter->attr.exclude_kernel
915 || counter->attr.exclude_hv
916 || counter->attr.sample_period)
919 if (ppmu->limited_pmc_event(ev))
923 * The requested event isn't on a limited PMC already;
924 * see if any alternative code goes on a limited PMC.
926 if (!ppmu->get_alternatives)
929 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
930 n = ppmu->get_alternatives(ev, flags, alt);
936 * Find an alternative event that goes on a normal PMC, if possible,
937 * and return the event code, or 0 if there is no such alternative.
938 * (Note: event code 0 is "don't count" on all machines.)
940 static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
942 u64 alt[MAX_EVENT_ALTERNATIVES];
945 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
946 n = ppmu->get_alternatives(ev, flags, alt);
952 /* Number of perf_counters counting hardware events */
953 static atomic_t num_counters;
954 /* Used to avoid races in calling reserve/release_pmc_hardware */
955 static DEFINE_MUTEX(pmc_reserve_mutex);
958 * Release the PMU if this is the last perf_counter.
960 static void hw_perf_counter_destroy(struct perf_counter *counter)
962 if (!atomic_add_unless(&num_counters, -1, 1)) {
963 mutex_lock(&pmc_reserve_mutex);
964 if (atomic_dec_return(&num_counters) == 0)
965 release_pmc_hardware();
966 mutex_unlock(&pmc_reserve_mutex);
971 * Translate a generic cache event config to a raw event code.
973 static int hw_perf_cache_event(u64 config, u64 *eventp)
975 unsigned long type, op, result;
978 if (!ppmu->cache_events)
982 type = config & 0xff;
983 op = (config >> 8) & 0xff;
984 result = (config >> 16) & 0xff;
986 if (type >= PERF_COUNT_HW_CACHE_MAX ||
987 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
988 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
991 ev = (*ppmu->cache_events)[type][op][result];
1000 const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
1003 unsigned long flags;
1004 struct perf_counter *ctrs[MAX_HWCOUNTERS];
1005 u64 events[MAX_HWCOUNTERS];
1006 unsigned int cflags[MAX_HWCOUNTERS];
1011 return ERR_PTR(-ENXIO);
1012 switch (counter->attr.type) {
1013 case PERF_TYPE_HARDWARE:
1014 ev = counter->attr.config;
1015 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
1016 return ERR_PTR(-EOPNOTSUPP);
1017 ev = ppmu->generic_events[ev];
1019 case PERF_TYPE_HW_CACHE:
1020 err = hw_perf_cache_event(counter->attr.config, &ev);
1022 return ERR_PTR(err);
1025 ev = counter->attr.config;
1028 return ERR_PTR(-EINVAL);
1030 counter->hw.config_base = ev;
1031 counter->hw.idx = 0;
1034 * If we are not running on a hypervisor, force the
1035 * exclude_hv bit to 0 so that we don't care what
1036 * the user set it to.
1038 if (!firmware_has_feature(FW_FEATURE_LPAR))
1039 counter->attr.exclude_hv = 0;
1042 * If this is a per-task counter, then we can use
1043 * PM_RUN_* events interchangeably with their non RUN_*
1044 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1045 * XXX we should check if the task is an idle task.
1048 if (counter->ctx->task)
1049 flags |= PPMU_ONLY_COUNT_RUN;
1052 * If this machine has limited counters, check whether this
1053 * event could go on a limited counter.
1055 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1056 if (can_go_on_limited_pmc(counter, ev, flags)) {
1057 flags |= PPMU_LIMITED_PMC_OK;
1058 } else if (ppmu->limited_pmc_event(ev)) {
1060 * The requested event is on a limited PMC,
1061 * but we can't use a limited PMC; see if any
1062 * alternative goes on a normal PMC.
1064 ev = normal_pmc_alternative(ev, flags);
1066 return ERR_PTR(-EINVAL);
1071 * If this is in a group, check if it can go on with all the
1072 * other hardware counters in the group. We assume the counter
1073 * hasn't been linked into its leader's sibling list at this point.
1076 if (counter->group_leader != counter) {
1077 n = collect_events(counter->group_leader, ppmu->n_counter - 1,
1078 ctrs, events, cflags);
1080 return ERR_PTR(-EINVAL);
1085 if (check_excludes(ctrs, cflags, n, 1))
1086 return ERR_PTR(-EINVAL);
1087 if (power_check_constraints(events, cflags, n + 1))
1088 return ERR_PTR(-EINVAL);
1090 counter->hw.config = events[n];
1091 counter->hw.counter_base = cflags[n];
1092 counter->hw.last_period = counter->hw.sample_period;
1093 atomic64_set(&counter->hw.period_left, counter->hw.last_period);
1096 * See if we need to reserve the PMU.
1097 * If no counters are currently in use, then we have to take a
1098 * mutex to ensure that we don't race with another task doing
1099 * reserve_pmc_hardware or release_pmc_hardware.
1102 if (!atomic_inc_not_zero(&num_counters)) {
1103 mutex_lock(&pmc_reserve_mutex);
1104 if (atomic_read(&num_counters) == 0 &&
1105 reserve_pmc_hardware(perf_counter_interrupt))
1108 atomic_inc(&num_counters);
1109 mutex_unlock(&pmc_reserve_mutex);
1111 counter->destroy = hw_perf_counter_destroy;
1114 return ERR_PTR(err);
1119 * A counter has overflowed; update its count and record
1120 * things if requested. Note that interrupts are hard-disabled
1121 * here so there is no possibility of being interrupted.
1123 static void record_and_restart(struct perf_counter *counter, unsigned long val,
1124 struct pt_regs *regs, int nmi)
1126 u64 period = counter->hw.sample_period;
1127 s64 prev, delta, left;
1130 /* we don't have to worry about interrupts here */
1131 prev = atomic64_read(&counter->hw.prev_count);
1132 delta = (val - prev) & 0xfffffffful;
1133 atomic64_add(delta, &counter->count);
1136 * See if the total period for this counter has expired,
1137 * and update for the next period.
1140 left = atomic64_read(&counter->hw.period_left) - delta;
1148 if (left < 0x80000000LL)
1149 val = 0x80000000LL - left;
1153 * Finally record data if requested.
1156 struct perf_sample_data data = {
1159 .period = counter->hw.last_period,
1162 if (counter->attr.sample_type & PERF_SAMPLE_ADDR)
1163 perf_get_data_addr(regs, &data.addr);
1165 if (perf_counter_overflow(counter, nmi, &data)) {
1167 * Interrupts are coming too fast - throttle them
1168 * by setting the counter to 0, so it will be
1169 * at least 2^30 cycles until the next interrupt
1170 * (assuming each counter counts at most 2 counts
1178 write_pmc(counter->hw.idx, val);
1179 atomic64_set(&counter->hw.prev_count, val);
1180 atomic64_set(&counter->hw.period_left, left);
1181 perf_counter_update_userpage(counter);
1185 * Called from generic code to get the misc flags (i.e. processor mode)
1188 unsigned long perf_misc_flags(struct pt_regs *regs)
1190 u32 flags = perf_get_misc_flags(regs);
1194 return user_mode(regs) ? PERF_EVENT_MISC_USER :
1195 PERF_EVENT_MISC_KERNEL;
1199 * Called from generic code to get the instruction pointer
1202 unsigned long perf_instruction_pointer(struct pt_regs *regs)
1206 if (TRAP(regs) != 0xf00)
1207 return regs->nip; /* not a PMU interrupt */
1209 ip = mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
1214 * Performance monitor interrupt stuff
1216 static void perf_counter_interrupt(struct pt_regs *regs)
1219 struct cpu_hw_counters *cpuhw = &__get_cpu_var(cpu_hw_counters);
1220 struct perf_counter *counter;
1225 if (cpuhw->n_limited)
1226 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
1229 perf_read_regs(regs);
1231 nmi = perf_intr_is_nmi(regs);
1237 for (i = 0; i < cpuhw->n_counters; ++i) {
1238 counter = cpuhw->counter[i];
1239 if (!counter->hw.idx || is_limited_pmc(counter->hw.idx))
1241 val = read_pmc(counter->hw.idx);
1243 /* counter has overflowed */
1245 record_and_restart(counter, val, regs, nmi);
1250 * In case we didn't find and reset the counter that caused
1251 * the interrupt, scan all counters and reset any that are
1252 * negative, to avoid getting continual interrupts.
1253 * Any that we processed in the previous loop will not be negative.
1256 for (i = 0; i < ppmu->n_counter; ++i) {
1257 if (is_limited_pmc(i + 1))
1259 val = read_pmc(i + 1);
1261 write_pmc(i + 1, 0);
1266 * Reset MMCR0 to its normal value. This will set PMXE and
1267 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
1268 * and thus allow interrupts to occur again.
1269 * XXX might want to use MSR.PM to keep the counters frozen until
1270 * we get back out of this interrupt.
1272 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1280 void hw_perf_counter_setup(int cpu)
1282 struct cpu_hw_counters *cpuhw = &per_cpu(cpu_hw_counters, cpu);
1284 memset(cpuhw, 0, sizeof(*cpuhw));
1285 cpuhw->mmcr[0] = MMCR0_FC;
1288 int register_power_pmu(struct power_pmu *pmu)
1291 return -EBUSY; /* something's already registered */
1294 pr_info("%s performance monitor hardware support registered\n",
1299 * Use FCHV to ignore kernel events if MSR.HV is set.
1301 if (mfmsr() & MSR_HV)
1302 freeze_counters_kernel = MMCR0_FCHV;
1303 #endif /* CONFIG_PPC64 */