2 * Performance event support - Freescale Embedded Performance Monitor
4 * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
5 * Copyright 2010 Freescale Semiconductor, Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/perf_event.h>
15 #include <linux/percpu.h>
16 #include <linux/hardirq.h>
17 #include <asm/reg_fsl_emb.h>
19 #include <asm/machdep.h>
20 #include <asm/firmware.h>
21 #include <asm/ptrace.h>
23 struct cpu_hw_events
{
27 struct perf_event
*event
[MAX_HWEVENTS
];
29 static DEFINE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
);
31 static struct fsl_emb_pmu
*ppmu
;
33 /* Number of perf_events counting hardware events */
34 static atomic_t num_events
;
35 /* Used to avoid races in calling reserve/release_pmc_hardware */
36 static DEFINE_MUTEX(pmc_reserve_mutex
);
39 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
42 static inline int perf_intr_is_nmi(struct pt_regs
*regs
)
51 static void perf_event_interrupt(struct pt_regs
*regs
);
54 * Read one performance monitor counter (PMC).
56 static unsigned long read_pmc(int idx
)
62 val
= mfpmr(PMRN_PMC0
);
65 val
= mfpmr(PMRN_PMC1
);
68 val
= mfpmr(PMRN_PMC2
);
71 val
= mfpmr(PMRN_PMC3
);
74 val
= mfpmr(PMRN_PMC4
);
77 val
= mfpmr(PMRN_PMC5
);
80 printk(KERN_ERR
"oops trying to read PMC%d\n", idx
);
89 static void write_pmc(int idx
, unsigned long val
)
93 mtpmr(PMRN_PMC0
, val
);
96 mtpmr(PMRN_PMC1
, val
);
99 mtpmr(PMRN_PMC2
, val
);
102 mtpmr(PMRN_PMC3
, val
);
105 mtpmr(PMRN_PMC4
, val
);
108 mtpmr(PMRN_PMC5
, val
);
111 printk(KERN_ERR
"oops trying to write PMC%d\n", idx
);
118 * Write one local control A register
120 static void write_pmlca(int idx
, unsigned long val
)
124 mtpmr(PMRN_PMLCA0
, val
);
127 mtpmr(PMRN_PMLCA1
, val
);
130 mtpmr(PMRN_PMLCA2
, val
);
133 mtpmr(PMRN_PMLCA3
, val
);
136 mtpmr(PMRN_PMLCA4
, val
);
139 mtpmr(PMRN_PMLCA5
, val
);
142 printk(KERN_ERR
"oops trying to write PMLCA%d\n", idx
);
149 * Write one local control B register
151 static void write_pmlcb(int idx
, unsigned long val
)
155 mtpmr(PMRN_PMLCB0
, val
);
158 mtpmr(PMRN_PMLCB1
, val
);
161 mtpmr(PMRN_PMLCB2
, val
);
164 mtpmr(PMRN_PMLCB3
, val
);
167 mtpmr(PMRN_PMLCB4
, val
);
170 mtpmr(PMRN_PMLCB5
, val
);
173 printk(KERN_ERR
"oops trying to write PMLCB%d\n", idx
);
179 static void fsl_emb_pmu_read(struct perf_event
*event
)
181 s64 val
, delta
, prev
;
183 if (event
->hw
.state
& PERF_HES_STOPPED
)
187 * Performance monitor interrupts come even when interrupts
188 * are soft-disabled, as long as interrupts are hard-enabled.
189 * Therefore we treat them like NMIs.
192 prev
= local64_read(&event
->hw
.prev_count
);
194 val
= read_pmc(event
->hw
.idx
);
195 } while (local64_cmpxchg(&event
->hw
.prev_count
, prev
, val
) != prev
);
197 /* The counters are only 32 bits wide */
198 delta
= (val
- prev
) & 0xfffffffful
;
199 local64_add(delta
, &event
->count
);
200 local64_sub(delta
, &event
->hw
.period_left
);
204 * Disable all events to prevent PMU interrupts and to allow
205 * events to be added or removed.
207 static void fsl_emb_pmu_disable(struct pmu
*pmu
)
209 struct cpu_hw_events
*cpuhw
;
212 local_irq_save(flags
);
213 cpuhw
= &__get_cpu_var(cpu_hw_events
);
215 if (!cpuhw
->disabled
) {
219 * Check if we ever enabled the PMU on this cpu.
221 if (!cpuhw
->pmcs_enabled
) {
223 cpuhw
->pmcs_enabled
= 1;
226 if (atomic_read(&num_events
)) {
228 * Set the 'freeze all counters' bit, and disable
229 * interrupts. The barrier is to make sure the
230 * mtpmr has been executed and the PMU has frozen
231 * the events before we return.
234 mtpmr(PMRN_PMGC0
, PMGC0_FAC
);
238 local_irq_restore(flags
);
242 * Re-enable all events if disable == 0.
243 * If we were previously disabled and events were added, then
244 * put the new config on the PMU.
246 static void fsl_emb_pmu_enable(struct pmu
*pmu
)
248 struct cpu_hw_events
*cpuhw
;
251 local_irq_save(flags
);
252 cpuhw
= &__get_cpu_var(cpu_hw_events
);
253 if (!cpuhw
->disabled
)
257 ppc_set_pmu_inuse(cpuhw
->n_events
!= 0);
259 if (cpuhw
->n_events
> 0) {
260 mtpmr(PMRN_PMGC0
, PMGC0_PMIE
| PMGC0_FCECE
);
265 local_irq_restore(flags
);
268 static int collect_events(struct perf_event
*group
, int max_count
,
269 struct perf_event
*ctrs
[])
272 struct perf_event
*event
;
274 if (!is_software_event(group
)) {
280 list_for_each_entry(event
, &group
->sibling_list
, group_entry
) {
281 if (!is_software_event(event
) &&
282 event
->state
!= PERF_EVENT_STATE_OFF
) {
292 /* context locked on entry */
293 static int fsl_emb_pmu_add(struct perf_event
*event
, int flags
)
295 struct cpu_hw_events
*cpuhw
;
297 int num_counters
= ppmu
->n_counter
;
301 perf_pmu_disable(event
->pmu
);
302 cpuhw
= &get_cpu_var(cpu_hw_events
);
304 if (event
->hw
.config
& FSL_EMB_EVENT_RESTRICTED
)
305 num_counters
= ppmu
->n_restricted
;
308 * Allocate counters from top-down, so that restricted-capable
309 * counters are kept free as long as possible.
311 for (i
= num_counters
- 1; i
>= 0; i
--) {
322 cpuhw
->event
[i
] = event
;
326 if (event
->hw
.sample_period
) {
327 s64 left
= local64_read(&event
->hw
.period_left
);
328 if (left
< 0x80000000L
)
329 val
= 0x80000000L
- left
;
331 local64_set(&event
->hw
.prev_count
, val
);
333 if (!(flags
& PERF_EF_START
)) {
334 event
->hw
.state
= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
339 perf_event_update_userpage(event
);
341 write_pmlcb(i
, event
->hw
.config
>> 32);
342 write_pmlca(i
, event
->hw
.config_base
);
346 put_cpu_var(cpu_hw_events
);
347 perf_pmu_enable(event
->pmu
);
351 /* context locked on entry */
352 static void fsl_emb_pmu_del(struct perf_event
*event
, int flags
)
354 struct cpu_hw_events
*cpuhw
;
355 int i
= event
->hw
.idx
;
357 perf_pmu_disable(event
->pmu
);
361 fsl_emb_pmu_read(event
);
363 cpuhw
= &get_cpu_var(cpu_hw_events
);
365 WARN_ON(event
!= cpuhw
->event
[event
->hw
.idx
]);
371 cpuhw
->event
[i
] = NULL
;
375 * TODO: if at least one restricted event exists, and we
376 * just freed up a non-restricted-capable counter, and
377 * there is a restricted-capable counter occupied by
378 * a non-restricted event, migrate that event to the
385 perf_pmu_enable(event
->pmu
);
386 put_cpu_var(cpu_hw_events
);
389 static void fsl_emb_pmu_start(struct perf_event
*event
, int ef_flags
)
394 if (event
->hw
.idx
< 0 || !event
->hw
.sample_period
)
397 if (!(event
->hw
.state
& PERF_HES_STOPPED
))
400 if (ef_flags
& PERF_EF_RELOAD
)
401 WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_UPTODATE
));
403 local_irq_save(flags
);
404 perf_pmu_disable(event
->pmu
);
407 left
= local64_read(&event
->hw
.period_left
);
408 write_pmc(event
->hw
.idx
, left
);
410 perf_event_update_userpage(event
);
411 perf_pmu_enable(event
->pmu
);
412 local_irq_restore(flags
);
415 static void fsl_emb_pmu_stop(struct perf_event
*event
, int ef_flags
)
419 if (event
->hw
.idx
< 0 || !event
->hw
.sample_period
)
422 if (event
->hw
.state
& PERF_HES_STOPPED
)
425 local_irq_save(flags
);
426 perf_pmu_disable(event
->pmu
);
428 fsl_emb_pmu_read(event
);
429 event
->hw
.state
|= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
430 write_pmc(event
->hw
.idx
, 0);
432 perf_event_update_userpage(event
);
433 perf_pmu_enable(event
->pmu
);
434 local_irq_restore(flags
);
438 * Release the PMU if this is the last perf_event.
440 static void hw_perf_event_destroy(struct perf_event
*event
)
442 if (!atomic_add_unless(&num_events
, -1, 1)) {
443 mutex_lock(&pmc_reserve_mutex
);
444 if (atomic_dec_return(&num_events
) == 0)
445 release_pmc_hardware();
446 mutex_unlock(&pmc_reserve_mutex
);
451 * Translate a generic cache event_id config to a raw event_id code.
453 static int hw_perf_cache_event(u64 config
, u64
*eventp
)
455 unsigned long type
, op
, result
;
458 if (!ppmu
->cache_events
)
462 type
= config
& 0xff;
463 op
= (config
>> 8) & 0xff;
464 result
= (config
>> 16) & 0xff;
466 if (type
>= PERF_COUNT_HW_CACHE_MAX
||
467 op
>= PERF_COUNT_HW_CACHE_OP_MAX
||
468 result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
471 ev
= (*ppmu
->cache_events
)[type
][op
][result
];
480 static int fsl_emb_pmu_event_init(struct perf_event
*event
)
483 struct perf_event
*events
[MAX_HWEVENTS
];
489 if (ppmu
->n_counter
> MAX_HWEVENTS
) {
490 WARN(1, "No. of perf counters (%d) is higher than max array size(%d)\n",
491 ppmu
->n_counter
, MAX_HWEVENTS
);
492 ppmu
->n_counter
= MAX_HWEVENTS
;
495 switch (event
->attr
.type
) {
496 case PERF_TYPE_HARDWARE
:
497 ev
= event
->attr
.config
;
498 if (ev
>= ppmu
->n_generic
|| ppmu
->generic_events
[ev
] == 0)
500 ev
= ppmu
->generic_events
[ev
];
503 case PERF_TYPE_HW_CACHE
:
504 err
= hw_perf_cache_event(event
->attr
.config
, &ev
);
510 ev
= event
->attr
.config
;
517 event
->hw
.config
= ppmu
->xlate_event(ev
);
518 if (!(event
->hw
.config
& FSL_EMB_EVENT_VALID
))
522 * If this is in a group, check if it can go on with all the
523 * other hardware events in the group. We assume the event
524 * hasn't been linked into its leader's sibling list at this point.
527 if (event
->group_leader
!= event
) {
528 n
= collect_events(event
->group_leader
,
529 ppmu
->n_counter
- 1, events
);
534 if (event
->hw
.config
& FSL_EMB_EVENT_RESTRICTED
) {
536 for (i
= 0; i
< n
; i
++) {
537 if (events
[i
]->hw
.config
& FSL_EMB_EVENT_RESTRICTED
)
541 if (num_restricted
>= ppmu
->n_restricted
)
547 event
->hw
.config_base
= PMLCA_CE
| PMLCA_FCM1
|
548 (u32
)((ev
<< 16) & PMLCA_EVENT_MASK
);
550 if (event
->attr
.exclude_user
)
551 event
->hw
.config_base
|= PMLCA_FCU
;
552 if (event
->attr
.exclude_kernel
)
553 event
->hw
.config_base
|= PMLCA_FCS
;
554 if (event
->attr
.exclude_idle
)
557 event
->hw
.last_period
= event
->hw
.sample_period
;
558 local64_set(&event
->hw
.period_left
, event
->hw
.last_period
);
561 * See if we need to reserve the PMU.
562 * If no events are currently in use, then we have to take a
563 * mutex to ensure that we don't race with another task doing
564 * reserve_pmc_hardware or release_pmc_hardware.
567 if (!atomic_inc_not_zero(&num_events
)) {
568 mutex_lock(&pmc_reserve_mutex
);
569 if (atomic_read(&num_events
) == 0 &&
570 reserve_pmc_hardware(perf_event_interrupt
))
573 atomic_inc(&num_events
);
574 mutex_unlock(&pmc_reserve_mutex
);
576 mtpmr(PMRN_PMGC0
, PMGC0_FAC
);
579 event
->destroy
= hw_perf_event_destroy
;
584 static struct pmu fsl_emb_pmu
= {
585 .pmu_enable
= fsl_emb_pmu_enable
,
586 .pmu_disable
= fsl_emb_pmu_disable
,
587 .event_init
= fsl_emb_pmu_event_init
,
588 .add
= fsl_emb_pmu_add
,
589 .del
= fsl_emb_pmu_del
,
590 .start
= fsl_emb_pmu_start
,
591 .stop
= fsl_emb_pmu_stop
,
592 .read
= fsl_emb_pmu_read
,
596 * A counter has overflowed; update its count and record
597 * things if requested. Note that interrupts are hard-disabled
598 * here so there is no possibility of being interrupted.
600 static void record_and_restart(struct perf_event
*event
, unsigned long val
,
601 struct pt_regs
*regs
)
603 u64 period
= event
->hw
.sample_period
;
604 s64 prev
, delta
, left
;
607 if (event
->hw
.state
& PERF_HES_STOPPED
) {
608 write_pmc(event
->hw
.idx
, 0);
612 /* we don't have to worry about interrupts here */
613 prev
= local64_read(&event
->hw
.prev_count
);
614 delta
= (val
- prev
) & 0xfffffffful
;
615 local64_add(delta
, &event
->count
);
618 * See if the total period for this event has expired,
619 * and update for the next period.
622 left
= local64_read(&event
->hw
.period_left
) - delta
;
629 event
->hw
.last_period
= event
->hw
.sample_period
;
631 if (left
< 0x80000000LL
)
632 val
= 0x80000000LL
- left
;
635 write_pmc(event
->hw
.idx
, val
);
636 local64_set(&event
->hw
.prev_count
, val
);
637 local64_set(&event
->hw
.period_left
, left
);
638 perf_event_update_userpage(event
);
641 * Finally record data if requested.
644 struct perf_sample_data data
;
646 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
648 if (perf_event_overflow(event
, &data
, regs
))
649 fsl_emb_pmu_stop(event
, 0);
653 static void perf_event_interrupt(struct pt_regs
*regs
)
656 struct cpu_hw_events
*cpuhw
= &__get_cpu_var(cpu_hw_events
);
657 struct perf_event
*event
;
662 nmi
= perf_intr_is_nmi(regs
);
668 for (i
= 0; i
< ppmu
->n_counter
; ++i
) {
669 event
= cpuhw
->event
[i
];
674 /* event has overflowed */
676 record_and_restart(event
, val
, regs
);
679 * Disabled counter is negative,
680 * reset it just in case.
687 /* PMM will keep counters frozen until we return from the interrupt. */
688 mtmsr(mfmsr() | MSR_PMM
);
689 mtpmr(PMRN_PMGC0
, PMGC0_PMIE
| PMGC0_FCECE
);
698 void hw_perf_event_setup(int cpu
)
700 struct cpu_hw_events
*cpuhw
= &per_cpu(cpu_hw_events
, cpu
);
702 memset(cpuhw
, 0, sizeof(*cpuhw
));
705 int register_fsl_emb_pmu(struct fsl_emb_pmu
*pmu
)
708 return -EBUSY
; /* something's already registered */
711 pr_info("%s performance monitor hardware support registered\n",
714 perf_pmu_register(&fsl_emb_pmu
, "cpu", PERF_TYPE_RAW
);