2 * Copyright 2014 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
15 * Perf_events support for Tile processor.
17 * This code is based upon the x86 perf event
20 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
21 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
22 * Copyright (C) 2009 Jaswinder Singh Rajput
23 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
24 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra
25 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
26 * Copyright (C) 2009 Google, Inc., Stephane Eranian
29 #include <linux/kprobes.h>
30 #include <linux/kernel.h>
31 #include <linux/kdebug.h>
32 #include <linux/mutex.h>
33 #include <linux/bitmap.h>
34 #include <linux/irq.h>
35 #include <linux/interrupt.h>
36 #include <linux/perf_event.h>
37 #include <linux/atomic.h>
38 #include <asm/traps.h>
39 #include <asm/stack.h>
41 #include <hv/hypervisor.h>
43 #define TILE_MAX_COUNTERS 4
45 #define PERF_COUNT_0_IDX 0
46 #define PERF_COUNT_1_IDX 1
47 #define AUX_PERF_COUNT_0_IDX 2
48 #define AUX_PERF_COUNT_1_IDX 3
50 struct cpu_hw_events
{
52 struct perf_event
*events
[TILE_MAX_COUNTERS
]; /* counter order */
53 struct perf_event
*event_list
[TILE_MAX_COUNTERS
]; /* enabled
55 int assign
[TILE_MAX_COUNTERS
];
56 unsigned long active_mask
[BITS_TO_LONGS(TILE_MAX_COUNTERS
)];
57 unsigned long used_mask
;
60 /* TILE arch specific performance monitor unit */
64 const int *hw_events
; /* generic hw events table */
65 /* generic hw cache events table */
66 const int (*cache_events
)[PERF_COUNT_HW_CACHE_MAX
]
67 [PERF_COUNT_HW_CACHE_OP_MAX
]
68 [PERF_COUNT_HW_CACHE_RESULT_MAX
];
69 int (*map_hw_event
)(u64
); /*method used to map
71 int (*map_cache_event
)(u64
); /*method used to map
74 u64 max_period
; /* max sampling period */
75 u64 cntval_mask
; /* counter width mask */
76 int cntval_bits
; /* counter width */
77 int max_events
; /* max generic hw events
79 int num_counters
; /* number base + aux counters */
80 int num_base_counters
; /* number base counters */
83 DEFINE_PER_CPU(u64
, perf_irqs
);
84 static DEFINE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
);
86 #define TILE_OP_UNSUPP (-1)
89 /* TILEPro hardware events map */
90 static const int tile_hw_event_map
[] = {
91 [PERF_COUNT_HW_CPU_CYCLES
] = 0x01, /* ONE */
92 [PERF_COUNT_HW_INSTRUCTIONS
] = 0x06, /* MP_BUNDLE_RETIRED */
93 [PERF_COUNT_HW_CACHE_REFERENCES
] = TILE_OP_UNSUPP
,
94 [PERF_COUNT_HW_CACHE_MISSES
] = TILE_OP_UNSUPP
,
95 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = 0x16, /*
96 MP_CONDITIONAL_BRANCH_ISSUED */
97 [PERF_COUNT_HW_BRANCH_MISSES
] = 0x14, /*
98 MP_CONDITIONAL_BRANCH_MISSPREDICT */
99 [PERF_COUNT_HW_BUS_CYCLES
] = TILE_OP_UNSUPP
,
102 /* TILEGx hardware events map */
103 static const int tile_hw_event_map
[] = {
104 [PERF_COUNT_HW_CPU_CYCLES
] = 0x181, /* ONE */
105 [PERF_COUNT_HW_INSTRUCTIONS
] = 0xdb, /* INSTRUCTION_BUNDLE */
106 [PERF_COUNT_HW_CACHE_REFERENCES
] = TILE_OP_UNSUPP
,
107 [PERF_COUNT_HW_CACHE_MISSES
] = TILE_OP_UNSUPP
,
108 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = 0xd9, /*
109 COND_BRANCH_PRED_CORRECT */
110 [PERF_COUNT_HW_BRANCH_MISSES
] = 0xda, /*
111 COND_BRANCH_PRED_INCORRECT */
112 [PERF_COUNT_HW_BUS_CYCLES
] = TILE_OP_UNSUPP
,
116 #define C(x) PERF_COUNT_HW_CACHE_##x
119 * Generalized hw caching related hw_event table, filled
120 * in on a per model basis. A value of -1 means
121 * 'not supported', any other value means the
125 /* TILEPro hardware cache event map */
126 static const int tile_cache_event_map
[PERF_COUNT_HW_CACHE_MAX
]
127 [PERF_COUNT_HW_CACHE_OP_MAX
]
128 [PERF_COUNT_HW_CACHE_RESULT_MAX
] = {
131 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
132 [C(RESULT_MISS
)] = 0x21, /* RD_MISS */
135 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
136 [C(RESULT_MISS
)] = 0x22, /* WR_MISS */
139 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
140 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
145 [C(RESULT_ACCESS
)] = 0x12, /* MP_ICACHE_HIT_ISSUED */
146 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
149 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
150 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
153 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
154 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
159 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
160 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
163 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
164 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
167 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
168 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
173 [C(RESULT_ACCESS
)] = 0x1d, /* TLB_CNT */
174 [C(RESULT_MISS
)] = 0x20, /* TLB_EXCEPTION */
177 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
178 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
181 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
182 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
187 [C(RESULT_ACCESS
)] = 0x13, /* MP_ITLB_HIT_ISSUED */
188 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
191 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
192 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
195 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
196 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
201 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
202 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
205 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
206 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
209 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
210 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
215 /* TILEGx hardware events map */
216 static const int tile_cache_event_map
[PERF_COUNT_HW_CACHE_MAX
]
217 [PERF_COUNT_HW_CACHE_OP_MAX
]
218 [PERF_COUNT_HW_CACHE_RESULT_MAX
] = {
221 * Like some other architectures (e.g. ARM), the performance
222 * counters don't differentiate between read and write
223 * accesses/misses, so this isn't strictly correct, but it's the
224 * best we can do. Writes and reads get combined.
227 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
228 [C(RESULT_MISS
)] = 0x44, /* RD_MISS */
231 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
232 [C(RESULT_MISS
)] = 0x45, /* WR_MISS */
235 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
236 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
241 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
242 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
245 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
246 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
249 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
250 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
255 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
256 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
259 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
260 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
263 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
264 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
269 [C(RESULT_ACCESS
)] = 0x40, /* TLB_CNT */
270 [C(RESULT_MISS
)] = 0x43, /* TLB_EXCEPTION */
273 [C(RESULT_ACCESS
)] = 0x40, /* TLB_CNT */
274 [C(RESULT_MISS
)] = 0x43, /* TLB_EXCEPTION */
277 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
278 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
283 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
284 [C(RESULT_MISS
)] = 0xd4, /* ITLB_MISS_INT */
287 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
288 [C(RESULT_MISS
)] = 0xd4, /* ITLB_MISS_INT */
291 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
292 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
297 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
298 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
301 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
302 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
305 [C(RESULT_ACCESS
)] = TILE_OP_UNSUPP
,
306 [C(RESULT_MISS
)] = TILE_OP_UNSUPP
,
312 static atomic_t tile_active_events
;
313 static DEFINE_MUTEX(perf_intr_reserve_mutex
);
315 static int tile_map_hw_event(u64 config
);
316 static int tile_map_cache_event(u64 config
);
318 static int tile_pmu_handle_irq(struct pt_regs
*regs
, int fault
);
321 * To avoid new_raw_count getting larger then pre_raw_count
322 * in tile_perf_event_update(), we limit the value of max_period to 2^31 - 1.
324 static const struct tile_pmu tilepmu
= {
330 .max_events
= ARRAY_SIZE(tile_hw_event_map
),
331 .map_hw_event
= tile_map_hw_event
,
332 .hw_events
= tile_hw_event_map
,
333 .map_cache_event
= tile_map_cache_event
,
334 .cache_events
= &tile_cache_event_map
,
336 .cntval_mask
= (1ULL << 32) - 1,
337 .max_period
= (1ULL << 31) - 1,
338 .num_counters
= TILE_MAX_COUNTERS
,
339 .num_base_counters
= TILE_BASE_COUNTERS
,
342 static const struct tile_pmu
*tile_pmu __read_mostly
;
345 * Check whether perf event is enabled.
347 int tile_perf_enabled(void)
349 return atomic_read(&tile_active_events
) != 0;
353 * Read Performance Counters.
355 static inline u64
read_counter(int idx
)
359 /* __insn_mfspr() only takes an immediate argument */
361 case PERF_COUNT_0_IDX
:
362 val
= __insn_mfspr(SPR_PERF_COUNT_0
);
364 case PERF_COUNT_1_IDX
:
365 val
= __insn_mfspr(SPR_PERF_COUNT_1
);
367 case AUX_PERF_COUNT_0_IDX
:
368 val
= __insn_mfspr(SPR_AUX_PERF_COUNT_0
);
370 case AUX_PERF_COUNT_1_IDX
:
371 val
= __insn_mfspr(SPR_AUX_PERF_COUNT_1
);
374 WARN_ON_ONCE(idx
> AUX_PERF_COUNT_1_IDX
||
375 idx
< PERF_COUNT_0_IDX
);
382 * Write Performance Counters.
384 static inline void write_counter(int idx
, u64 value
)
386 /* __insn_mtspr() only takes an immediate argument */
388 case PERF_COUNT_0_IDX
:
389 __insn_mtspr(SPR_PERF_COUNT_0
, value
);
391 case PERF_COUNT_1_IDX
:
392 __insn_mtspr(SPR_PERF_COUNT_1
, value
);
394 case AUX_PERF_COUNT_0_IDX
:
395 __insn_mtspr(SPR_AUX_PERF_COUNT_0
, value
);
397 case AUX_PERF_COUNT_1_IDX
:
398 __insn_mtspr(SPR_AUX_PERF_COUNT_1
, value
);
401 WARN_ON_ONCE(idx
> AUX_PERF_COUNT_1_IDX
||
402 idx
< PERF_COUNT_0_IDX
);
407 * Enable performance event by setting
408 * Performance Counter Control registers.
410 static inline void tile_pmu_enable_event(struct perf_event
*event
)
412 struct hw_perf_event
*hwc
= &event
->hw
;
413 unsigned long cfg
, mask
;
414 int shift
, idx
= hwc
->idx
;
417 * prevent early activation from tile_pmu_start() in hw_perf_enable
420 if (WARN_ON_ONCE(idx
== -1))
423 if (idx
< tile_pmu
->num_base_counters
)
424 cfg
= __insn_mfspr(SPR_PERF_COUNT_CTL
);
426 cfg
= __insn_mfspr(SPR_AUX_PERF_COUNT_CTL
);
429 case PERF_COUNT_0_IDX
:
430 case AUX_PERF_COUNT_0_IDX
:
431 mask
= TILE_EVENT_MASK
;
434 case PERF_COUNT_1_IDX
:
435 case AUX_PERF_COUNT_1_IDX
:
436 mask
= TILE_EVENT_MASK
<< 16;
440 WARN_ON_ONCE(idx
< PERF_COUNT_0_IDX
||
441 idx
> AUX_PERF_COUNT_1_IDX
);
445 /* Clear mask bits to enable the event. */
447 cfg
|= hwc
->config
<< shift
;
449 if (idx
< tile_pmu
->num_base_counters
)
450 __insn_mtspr(SPR_PERF_COUNT_CTL
, cfg
);
452 __insn_mtspr(SPR_AUX_PERF_COUNT_CTL
, cfg
);
456 * Disable performance event by clearing
457 * Performance Counter Control registers.
459 static inline void tile_pmu_disable_event(struct perf_event
*event
)
461 struct hw_perf_event
*hwc
= &event
->hw
;
462 unsigned long cfg
, mask
;
468 if (idx
< tile_pmu
->num_base_counters
)
469 cfg
= __insn_mfspr(SPR_PERF_COUNT_CTL
);
471 cfg
= __insn_mfspr(SPR_AUX_PERF_COUNT_CTL
);
474 case PERF_COUNT_0_IDX
:
475 case AUX_PERF_COUNT_0_IDX
:
476 mask
= TILE_PLM_MASK
;
478 case PERF_COUNT_1_IDX
:
479 case AUX_PERF_COUNT_1_IDX
:
480 mask
= TILE_PLM_MASK
<< 16;
483 WARN_ON_ONCE(idx
< PERF_COUNT_0_IDX
||
484 idx
> AUX_PERF_COUNT_1_IDX
);
488 /* Set mask bits to disable the event. */
491 if (idx
< tile_pmu
->num_base_counters
)
492 __insn_mtspr(SPR_PERF_COUNT_CTL
, cfg
);
494 __insn_mtspr(SPR_AUX_PERF_COUNT_CTL
, cfg
);
498 * Propagate event elapsed time into the generic event.
499 * Can only be executed on the CPU where the event is active.
500 * Returns the delta events processed.
502 static u64
tile_perf_event_update(struct perf_event
*event
)
504 struct hw_perf_event
*hwc
= &event
->hw
;
505 int shift
= 64 - tile_pmu
->cntval_bits
;
506 u64 prev_raw_count
, new_raw_count
;
512 * Careful: an NMI might modify the previous event value.
514 * Our tactic to handle this is to first atomically read and
515 * exchange a new raw count - then add that new-prev delta
516 * count to the generic event atomically:
519 prev_raw_count
= local64_read(&hwc
->prev_count
);
520 new_raw_count
= read_counter(idx
);
522 oldval
= local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
524 if (oldval
!= prev_raw_count
)
528 * Now we have the new raw value and have updated the prev
529 * timestamp already. We can now calculate the elapsed delta
530 * (event-)time and add that to the generic event.
532 * Careful, not all hw sign-extends above the physical width
535 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
538 local64_add(delta
, &event
->count
);
539 local64_sub(delta
, &hwc
->period_left
);
541 return new_raw_count
;
545 * Set the next IRQ period, based on the hwc->period_left value.
546 * To be called with the event disabled in hw:
548 static int tile_event_set_period(struct perf_event
*event
)
550 struct hw_perf_event
*hwc
= &event
->hw
;
552 s64 left
= local64_read(&hwc
->period_left
);
553 s64 period
= hwc
->sample_period
;
557 * If we are way outside a reasonable range then just skip forward:
559 if (unlikely(left
<= -period
)) {
561 local64_set(&hwc
->period_left
, left
);
562 hwc
->last_period
= period
;
566 if (unlikely(left
<= 0)) {
568 local64_set(&hwc
->period_left
, left
);
569 hwc
->last_period
= period
;
572 if (left
> tile_pmu
->max_period
)
573 left
= tile_pmu
->max_period
;
576 * The hw event starts counting from this event offset,
577 * mark it to be able to extra future deltas:
579 local64_set(&hwc
->prev_count
, (u64
)-left
);
581 write_counter(idx
, (u64
)(-left
) & tile_pmu
->cntval_mask
);
583 perf_event_update_userpage(event
);
589 * Stop the event but do not release the PMU counter
591 static void tile_pmu_stop(struct perf_event
*event
, int flags
)
593 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
594 struct hw_perf_event
*hwc
= &event
->hw
;
597 if (__test_and_clear_bit(idx
, cpuc
->active_mask
)) {
598 tile_pmu_disable_event(event
);
599 cpuc
->events
[hwc
->idx
] = NULL
;
600 WARN_ON_ONCE(hwc
->state
& PERF_HES_STOPPED
);
601 hwc
->state
|= PERF_HES_STOPPED
;
604 if ((flags
& PERF_EF_UPDATE
) && !(hwc
->state
& PERF_HES_UPTODATE
)) {
606 * Drain the remaining delta count out of a event
607 * that we are disabling:
609 tile_perf_event_update(event
);
610 hwc
->state
|= PERF_HES_UPTODATE
;
615 * Start an event (without re-assigning counter)
617 static void tile_pmu_start(struct perf_event
*event
, int flags
)
619 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
620 int idx
= event
->hw
.idx
;
622 if (WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_STOPPED
)))
625 if (WARN_ON_ONCE(idx
== -1))
628 if (flags
& PERF_EF_RELOAD
) {
629 WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_UPTODATE
));
630 tile_event_set_period(event
);
635 cpuc
->events
[idx
] = event
;
636 __set_bit(idx
, cpuc
->active_mask
);
638 unmask_pmc_interrupts();
640 tile_pmu_enable_event(event
);
642 perf_event_update_userpage(event
);
646 * Add a single event to the PMU.
648 * The event is added to the group of enabled events
649 * but only if it can be scehduled with existing events.
651 static int tile_pmu_add(struct perf_event
*event
, int flags
)
653 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
654 struct hw_perf_event
*hwc
;
663 if (cpuc
->n_events
== tile_pmu
->num_counters
)
666 cpuc
->event_list
[cpuc
->n_events
] = event
;
669 hwc
->state
= PERF_HES_UPTODATE
| PERF_HES_STOPPED
;
670 if (!(flags
& PERF_EF_START
))
671 hwc
->state
|= PERF_HES_ARCH
;
674 * Find first empty counter.
676 max_cnt
= tile_pmu
->num_counters
;
677 mask
= ~cpuc
->used_mask
;
679 /* Find next free counter. */
680 b
= find_next_bit(&mask
, max_cnt
, 0);
682 /* Should not happen. */
683 if (WARN_ON_ONCE(b
== max_cnt
))
687 * Assign counter to event.
690 __set_bit(b
, &cpuc
->used_mask
);
693 * Start if requested.
695 if (flags
& PERF_EF_START
)
696 tile_pmu_start(event
, PERF_EF_RELOAD
);
702 * Delete a single event from the PMU.
704 * The event is deleted from the group of enabled events.
705 * If it is the last event, disable PMU interrupt.
707 static void tile_pmu_del(struct perf_event
*event
, int flags
)
709 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
713 * Remove event from list, compact list if necessary.
715 for (i
= 0; i
< cpuc
->n_events
; i
++) {
716 if (cpuc
->event_list
[i
] == event
) {
717 while (++i
< cpuc
->n_events
)
718 cpuc
->event_list
[i
-1] = cpuc
->event_list
[i
];
720 cpuc
->events
[event
->hw
.idx
] = NULL
;
721 __clear_bit(event
->hw
.idx
, &cpuc
->used_mask
);
722 tile_pmu_stop(event
, PERF_EF_UPDATE
);
727 * If there are no events left, then mask PMU interrupt.
729 if (cpuc
->n_events
== 0)
730 mask_pmc_interrupts();
731 perf_event_update_userpage(event
);
735 * Propagate event elapsed time into the event.
737 static inline void tile_pmu_read(struct perf_event
*event
)
739 tile_perf_event_update(event
);
743 * Map generic events to Tile PMU.
745 static int tile_map_hw_event(u64 config
)
747 if (config
>= tile_pmu
->max_events
)
749 return tile_pmu
->hw_events
[config
];
753 * Map generic hardware cache events to Tile PMU.
755 static int tile_map_cache_event(u64 config
)
757 unsigned int cache_type
, cache_op
, cache_result
;
760 if (!tile_pmu
->cache_events
)
763 cache_type
= (config
>> 0) & 0xff;
764 if (cache_type
>= PERF_COUNT_HW_CACHE_MAX
)
767 cache_op
= (config
>> 8) & 0xff;
768 if (cache_op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
771 cache_result
= (config
>> 16) & 0xff;
772 if (cache_result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
775 code
= (*tile_pmu
->cache_events
)[cache_type
][cache_op
][cache_result
];
776 if (code
== TILE_OP_UNSUPP
)
782 static void tile_event_destroy(struct perf_event
*event
)
784 if (atomic_dec_return(&tile_active_events
) == 0)
785 release_pmc_hardware();
788 static int __tile_event_init(struct perf_event
*event
)
790 struct perf_event_attr
*attr
= &event
->attr
;
791 struct hw_perf_event
*hwc
= &event
->hw
;
794 switch (attr
->type
) {
795 case PERF_TYPE_HARDWARE
:
796 code
= tile_pmu
->map_hw_event(attr
->config
);
798 case PERF_TYPE_HW_CACHE
:
799 code
= tile_pmu
->map_cache_event(attr
->config
);
802 code
= attr
->config
& TILE_EVENT_MASK
;
805 /* Should not happen. */
815 if (attr
->exclude_user
)
816 hwc
->config
|= TILE_CTL_EXCL_USER
;
818 if (attr
->exclude_kernel
)
819 hwc
->config
|= TILE_CTL_EXCL_KERNEL
;
821 if (attr
->exclude_hv
)
822 hwc
->config
|= TILE_CTL_EXCL_HV
;
824 if (!hwc
->sample_period
) {
825 hwc
->sample_period
= tile_pmu
->max_period
;
826 hwc
->last_period
= hwc
->sample_period
;
827 local64_set(&hwc
->period_left
, hwc
->sample_period
);
829 event
->destroy
= tile_event_destroy
;
833 static int tile_event_init(struct perf_event
*event
)
836 perf_irq_t old_irq_handler
= NULL
;
838 if (atomic_inc_return(&tile_active_events
) == 1)
839 old_irq_handler
= reserve_pmc_hardware(tile_pmu_handle_irq
);
841 if (old_irq_handler
) {
842 pr_warn("PMC hardware busy (reserved by oprofile)\n");
844 atomic_dec(&tile_active_events
);
848 switch (event
->attr
.type
) {
850 case PERF_TYPE_HARDWARE
:
851 case PERF_TYPE_HW_CACHE
:
858 err
= __tile_event_init(event
);
861 event
->destroy(event
);
866 static struct pmu tilera_pmu
= {
867 .event_init
= tile_event_init
,
871 .start
= tile_pmu_start
,
872 .stop
= tile_pmu_stop
,
874 .read
= tile_pmu_read
,
878 * PMU's IRQ handler, PMU has 2 interrupts, they share the same handler.
880 int tile_pmu_handle_irq(struct pt_regs
*regs
, int fault
)
882 struct perf_sample_data data
;
883 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
884 struct perf_event
*event
;
885 struct hw_perf_event
*hwc
;
887 unsigned long status
;
890 __this_cpu_inc(perf_irqs
);
892 if (!atomic_read(&tile_active_events
))
895 status
= pmc_get_overflow();
896 pmc_ack_overflow(status
);
898 for_each_set_bit(bit
, &status
, tile_pmu
->num_counters
) {
900 event
= cpuc
->events
[bit
];
905 if (!test_bit(bit
, cpuc
->active_mask
))
910 val
= tile_perf_event_update(event
);
911 if (val
& (1ULL << (tile_pmu
->cntval_bits
- 1)))
914 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
915 if (!tile_event_set_period(event
))
918 if (perf_event_overflow(event
, &data
, regs
))
919 tile_pmu_stop(event
, 0);
925 static bool __init
supported_pmu(void)
931 int __init
init_hw_perf_events(void)
934 perf_pmu_register(&tilera_pmu
, "cpu", PERF_TYPE_RAW
);
937 arch_initcall(init_hw_perf_events
);
939 /* Callchain handling code. */
942 * Tile specific backtracing code for perf_events.
944 static inline void perf_callchain(struct perf_callchain_entry_ctx
*entry
,
945 struct pt_regs
*regs
)
947 struct KBacktraceIterator kbt
;
951 * Get the address just after the "jalr" instruction that
952 * jumps to the handler for a syscall. When we find this
953 * address in a backtrace, we silently ignore it, which gives
954 * us a one-step backtrace connection from the sys_xxx()
955 * function in the kernel to the xxx() function in libc.
956 * Otherwise, we lose the ability to properly attribute time
957 * from the libc calls to the kernel implementations, since
958 * oprofile only considers PCs from backtraces a pair at a time.
960 unsigned long handle_syscall_pc
= handle_syscall_link_address();
962 KBacktraceIterator_init(&kbt
, NULL
, regs
);
966 * The sample for the pc is already recorded. Now we are adding the
967 * address of the callsites on the stack. Our iterator starts
968 * with the frame of the (already sampled) call site. If our
969 * iterator contained a "return address" field, we could have just
970 * used it and wouldn't have needed to skip the first
971 * frame. That's in effect what the arm and x86 versions do.
972 * Instead we peel off the first iteration to get the equivalent
976 if (KBacktraceIterator_end(&kbt
))
978 KBacktraceIterator_next(&kbt
);
981 * Set stack depth to 16 for user and kernel space respectively, that
982 * is, total 32 stack frames.
984 for (i
= 0; i
< 16; ++i
) {
986 if (KBacktraceIterator_end(&kbt
))
989 if (pc
!= handle_syscall_pc
)
990 perf_callchain_store(entry
, pc
);
991 KBacktraceIterator_next(&kbt
);
995 void perf_callchain_user(struct perf_callchain_entry_ctx
*entry
,
996 struct pt_regs
*regs
)
998 perf_callchain(entry
, regs
);
1001 void perf_callchain_kernel(struct perf_callchain_entry_ctx
*entry
,
1002 struct pt_regs
*regs
)
1004 perf_callchain(entry
, regs
);