1 // SPDX-License-Identifier: GPL-2.0-only
3 * Xtensa Performance Monitor Module driver
4 * See Tensilica Debug User's Guide for PMU registers documentation.
6 * Copyright (C) 2015 Cadence Design Systems Inc.
9 #include <linux/interrupt.h>
10 #include <linux/irqdomain.h>
11 #include <linux/module.h>
13 #include <linux/perf_event.h>
14 #include <linux/platform_device.h>
17 #include <asm/processor.h>
18 #include <asm/stacktrace.h>
20 #define XTENSA_HWVERSION_RG_2015_0 260000
22 #if XCHAL_HW_MIN_VERSION >= XTENSA_HWVERSION_RG_2015_0
23 #define XTENSA_PMU_ERI_BASE 0x00101000
25 #define XTENSA_PMU_ERI_BASE 0x00001000
28 /* Global control/status for all perf counters */
29 #define XTENSA_PMU_PMG XTENSA_PMU_ERI_BASE
30 /* Perf counter values */
31 #define XTENSA_PMU_PM(i) (XTENSA_PMU_ERI_BASE + 0x80 + (i) * 4)
32 /* Perf counter control registers */
33 #define XTENSA_PMU_PMCTRL(i) (XTENSA_PMU_ERI_BASE + 0x100 + (i) * 4)
34 /* Perf counter status registers */
35 #define XTENSA_PMU_PMSTAT(i) (XTENSA_PMU_ERI_BASE + 0x180 + (i) * 4)
37 #define XTENSA_PMU_PMG_PMEN 0x1
39 #define XTENSA_PMU_COUNTER_MASK 0xffffffffULL
40 #define XTENSA_PMU_COUNTER_MAX 0x7fffffff
42 #define XTENSA_PMU_PMCTRL_INTEN 0x00000001
43 #define XTENSA_PMU_PMCTRL_KRNLCNT 0x00000008
44 #define XTENSA_PMU_PMCTRL_TRACELEVEL 0x000000f0
45 #define XTENSA_PMU_PMCTRL_SELECT_SHIFT 8
46 #define XTENSA_PMU_PMCTRL_SELECT 0x00001f00
47 #define XTENSA_PMU_PMCTRL_MASK_SHIFT 16
48 #define XTENSA_PMU_PMCTRL_MASK 0xffff0000
50 #define XTENSA_PMU_MASK(select, mask) \
51 (((select) << XTENSA_PMU_PMCTRL_SELECT_SHIFT) | \
52 ((mask) << XTENSA_PMU_PMCTRL_MASK_SHIFT) | \
53 XTENSA_PMU_PMCTRL_TRACELEVEL | \
54 XTENSA_PMU_PMCTRL_INTEN)
56 #define XTENSA_PMU_PMSTAT_OVFL 0x00000001
57 #define XTENSA_PMU_PMSTAT_INTASRT 0x00000010
59 struct xtensa_pmu_events
{
60 /* Array of events currently on this core */
61 struct perf_event
*event
[XCHAL_NUM_PERF_COUNTERS
];
62 /* Bitmap of used hardware counters */
63 unsigned long used_mask
[BITS_TO_LONGS(XCHAL_NUM_PERF_COUNTERS
)];
65 static DEFINE_PER_CPU(struct xtensa_pmu_events
, xtensa_pmu_events
);
67 static const u32 xtensa_hw_ctl
[] = {
68 [PERF_COUNT_HW_CPU_CYCLES
] = XTENSA_PMU_MASK(0, 0x1),
69 [PERF_COUNT_HW_INSTRUCTIONS
] = XTENSA_PMU_MASK(2, 0xffff),
70 [PERF_COUNT_HW_CACHE_REFERENCES
] = XTENSA_PMU_MASK(10, 0x1),
71 [PERF_COUNT_HW_CACHE_MISSES
] = XTENSA_PMU_MASK(12, 0x1),
72 /* Taken and non-taken branches + taken loop ends */
73 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = XTENSA_PMU_MASK(2, 0x490),
74 /* Instruction-related + other global stall cycles */
75 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
] = XTENSA_PMU_MASK(4, 0x1ff),
76 /* Data-related global stall cycles */
77 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND
] = XTENSA_PMU_MASK(3, 0x1ff),
80 #define C(_x) PERF_COUNT_HW_CACHE_##_x
82 static const u32 xtensa_cache_ctl
[][C(OP_MAX
)][C(RESULT_MAX
)] = {
85 [C(RESULT_ACCESS
)] = XTENSA_PMU_MASK(10, 0x1),
86 [C(RESULT_MISS
)] = XTENSA_PMU_MASK(10, 0x2),
89 [C(RESULT_ACCESS
)] = XTENSA_PMU_MASK(11, 0x1),
90 [C(RESULT_MISS
)] = XTENSA_PMU_MASK(11, 0x2),
95 [C(RESULT_ACCESS
)] = XTENSA_PMU_MASK(8, 0x1),
96 [C(RESULT_MISS
)] = XTENSA_PMU_MASK(8, 0x2),
101 [C(RESULT_ACCESS
)] = XTENSA_PMU_MASK(9, 0x1),
102 [C(RESULT_MISS
)] = XTENSA_PMU_MASK(9, 0x8),
107 [C(RESULT_ACCESS
)] = XTENSA_PMU_MASK(7, 0x1),
108 [C(RESULT_MISS
)] = XTENSA_PMU_MASK(7, 0x8),
113 static int xtensa_pmu_cache_event(u64 config
)
115 unsigned int cache_type
, cache_op
, cache_result
;
118 cache_type
= (config
>> 0) & 0xff;
119 cache_op
= (config
>> 8) & 0xff;
120 cache_result
= (config
>> 16) & 0xff;
122 if (cache_type
>= ARRAY_SIZE(xtensa_cache_ctl
) ||
123 cache_op
>= C(OP_MAX
) ||
124 cache_result
>= C(RESULT_MAX
))
127 ret
= xtensa_cache_ctl
[cache_type
][cache_op
][cache_result
];
135 static inline uint32_t xtensa_pmu_read_counter(int idx
)
137 return get_er(XTENSA_PMU_PM(idx
));
140 static inline void xtensa_pmu_write_counter(int idx
, uint32_t v
)
142 set_er(v
, XTENSA_PMU_PM(idx
));
145 static void xtensa_perf_event_update(struct perf_event
*event
,
146 struct hw_perf_event
*hwc
, int idx
)
148 uint64_t prev_raw_count
, new_raw_count
;
152 prev_raw_count
= local64_read(&hwc
->prev_count
);
153 new_raw_count
= xtensa_pmu_read_counter(event
->hw
.idx
);
154 } while (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
155 new_raw_count
) != prev_raw_count
);
157 delta
= (new_raw_count
- prev_raw_count
) & XTENSA_PMU_COUNTER_MASK
;
159 local64_add(delta
, &event
->count
);
160 local64_sub(delta
, &hwc
->period_left
);
163 static bool xtensa_perf_event_set_period(struct perf_event
*event
,
164 struct hw_perf_event
*hwc
, int idx
)
169 if (!is_sampling_event(event
)) {
170 left
= XTENSA_PMU_COUNTER_MAX
;
172 s64 period
= hwc
->sample_period
;
174 left
= local64_read(&hwc
->period_left
);
175 if (left
<= -period
) {
177 local64_set(&hwc
->period_left
, left
);
178 hwc
->last_period
= period
;
180 } else if (left
<= 0) {
182 local64_set(&hwc
->period_left
, left
);
183 hwc
->last_period
= period
;
186 if (left
> XTENSA_PMU_COUNTER_MAX
)
187 left
= XTENSA_PMU_COUNTER_MAX
;
190 local64_set(&hwc
->prev_count
, -left
);
191 xtensa_pmu_write_counter(idx
, -left
);
192 perf_event_update_userpage(event
);
197 static void xtensa_pmu_enable(struct pmu
*pmu
)
199 set_er(get_er(XTENSA_PMU_PMG
) | XTENSA_PMU_PMG_PMEN
, XTENSA_PMU_PMG
);
202 static void xtensa_pmu_disable(struct pmu
*pmu
)
204 set_er(get_er(XTENSA_PMU_PMG
) & ~XTENSA_PMU_PMG_PMEN
, XTENSA_PMU_PMG
);
207 static int xtensa_pmu_event_init(struct perf_event
*event
)
211 switch (event
->attr
.type
) {
212 case PERF_TYPE_HARDWARE
:
213 if (event
->attr
.config
>= ARRAY_SIZE(xtensa_hw_ctl
) ||
214 xtensa_hw_ctl
[event
->attr
.config
] == 0)
216 event
->hw
.config
= xtensa_hw_ctl
[event
->attr
.config
];
219 case PERF_TYPE_HW_CACHE
:
220 ret
= xtensa_pmu_cache_event(event
->attr
.config
);
223 event
->hw
.config
= ret
;
227 /* Not 'previous counter' select */
228 if ((event
->attr
.config
& XTENSA_PMU_PMCTRL_SELECT
) ==
229 (1 << XTENSA_PMU_PMCTRL_SELECT_SHIFT
))
231 event
->hw
.config
= (event
->attr
.config
&
232 (XTENSA_PMU_PMCTRL_KRNLCNT
|
233 XTENSA_PMU_PMCTRL_TRACELEVEL
|
234 XTENSA_PMU_PMCTRL_SELECT
|
235 XTENSA_PMU_PMCTRL_MASK
)) |
236 XTENSA_PMU_PMCTRL_INTEN
;
245 * Starts/Stops a counter present on the PMU. The PMI handler
246 * should stop the counter when perf_event_overflow() returns
247 * !0. ->start() will be used to continue.
249 static void xtensa_pmu_start(struct perf_event
*event
, int flags
)
251 struct hw_perf_event
*hwc
= &event
->hw
;
254 if (WARN_ON_ONCE(idx
== -1))
257 if (flags
& PERF_EF_RELOAD
) {
258 WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_UPTODATE
));
259 xtensa_perf_event_set_period(event
, hwc
, idx
);
264 set_er(hwc
->config
, XTENSA_PMU_PMCTRL(idx
));
267 static void xtensa_pmu_stop(struct perf_event
*event
, int flags
)
269 struct hw_perf_event
*hwc
= &event
->hw
;
272 if (!(hwc
->state
& PERF_HES_STOPPED
)) {
273 set_er(0, XTENSA_PMU_PMCTRL(idx
));
274 set_er(get_er(XTENSA_PMU_PMSTAT(idx
)),
275 XTENSA_PMU_PMSTAT(idx
));
276 hwc
->state
|= PERF_HES_STOPPED
;
279 if ((flags
& PERF_EF_UPDATE
) &&
280 !(event
->hw
.state
& PERF_HES_UPTODATE
)) {
281 xtensa_perf_event_update(event
, &event
->hw
, idx
);
282 event
->hw
.state
|= PERF_HES_UPTODATE
;
287 * Adds/Removes a counter to/from the PMU, can be done inside
288 * a transaction, see the ->*_txn() methods.
290 static int xtensa_pmu_add(struct perf_event
*event
, int flags
)
292 struct xtensa_pmu_events
*ev
= this_cpu_ptr(&xtensa_pmu_events
);
293 struct hw_perf_event
*hwc
= &event
->hw
;
296 if (__test_and_set_bit(idx
, ev
->used_mask
)) {
297 idx
= find_first_zero_bit(ev
->used_mask
,
298 XCHAL_NUM_PERF_COUNTERS
);
299 if (idx
== XCHAL_NUM_PERF_COUNTERS
)
302 __set_bit(idx
, ev
->used_mask
);
305 ev
->event
[idx
] = event
;
307 hwc
->state
= PERF_HES_UPTODATE
| PERF_HES_STOPPED
;
309 if (flags
& PERF_EF_START
)
310 xtensa_pmu_start(event
, PERF_EF_RELOAD
);
312 perf_event_update_userpage(event
);
316 static void xtensa_pmu_del(struct perf_event
*event
, int flags
)
318 struct xtensa_pmu_events
*ev
= this_cpu_ptr(&xtensa_pmu_events
);
320 xtensa_pmu_stop(event
, PERF_EF_UPDATE
);
321 __clear_bit(event
->hw
.idx
, ev
->used_mask
);
322 perf_event_update_userpage(event
);
325 static void xtensa_pmu_read(struct perf_event
*event
)
327 xtensa_perf_event_update(event
, &event
->hw
, event
->hw
.idx
);
330 static int callchain_trace(struct stackframe
*frame
, void *data
)
332 struct perf_callchain_entry_ctx
*entry
= data
;
334 perf_callchain_store(entry
, frame
->pc
);
338 void perf_callchain_kernel(struct perf_callchain_entry_ctx
*entry
,
339 struct pt_regs
*regs
)
341 xtensa_backtrace_kernel(regs
, entry
->max_stack
,
342 callchain_trace
, NULL
, entry
);
345 void perf_callchain_user(struct perf_callchain_entry_ctx
*entry
,
346 struct pt_regs
*regs
)
348 xtensa_backtrace_user(regs
, entry
->max_stack
,
349 callchain_trace
, entry
);
352 void perf_event_print_debug(void)
357 local_irq_save(flags
);
358 pr_info("CPU#%d: PMG: 0x%08lx\n", smp_processor_id(),
359 get_er(XTENSA_PMU_PMG
));
360 for (i
= 0; i
< XCHAL_NUM_PERF_COUNTERS
; ++i
)
361 pr_info("PM%d: 0x%08lx, PMCTRL%d: 0x%08lx, PMSTAT%d: 0x%08lx\n",
362 i
, get_er(XTENSA_PMU_PM(i
)),
363 i
, get_er(XTENSA_PMU_PMCTRL(i
)),
364 i
, get_er(XTENSA_PMU_PMSTAT(i
)));
365 local_irq_restore(flags
);
368 irqreturn_t
xtensa_pmu_irq_handler(int irq
, void *dev_id
)
370 irqreturn_t rc
= IRQ_NONE
;
371 struct xtensa_pmu_events
*ev
= this_cpu_ptr(&xtensa_pmu_events
);
374 for_each_set_bit(i
, ev
->used_mask
, XCHAL_NUM_PERF_COUNTERS
) {
375 uint32_t v
= get_er(XTENSA_PMU_PMSTAT(i
));
376 struct perf_event
*event
= ev
->event
[i
];
377 struct hw_perf_event
*hwc
= &event
->hw
;
380 if (!(v
& XTENSA_PMU_PMSTAT_OVFL
))
383 set_er(v
, XTENSA_PMU_PMSTAT(i
));
384 xtensa_perf_event_update(event
, hwc
, i
);
385 last_period
= hwc
->last_period
;
386 if (xtensa_perf_event_set_period(event
, hwc
, i
)) {
387 struct perf_sample_data data
;
388 struct pt_regs
*regs
= get_irq_regs();
390 perf_sample_data_init(&data
, 0, last_period
);
391 if (perf_event_overflow(event
, &data
, regs
))
392 xtensa_pmu_stop(event
, 0);
400 static struct pmu xtensa_pmu
= {
401 .pmu_enable
= xtensa_pmu_enable
,
402 .pmu_disable
= xtensa_pmu_disable
,
403 .event_init
= xtensa_pmu_event_init
,
404 .add
= xtensa_pmu_add
,
405 .del
= xtensa_pmu_del
,
406 .start
= xtensa_pmu_start
,
407 .stop
= xtensa_pmu_stop
,
408 .read
= xtensa_pmu_read
,
411 static int xtensa_pmu_setup(unsigned int cpu
)
415 set_er(0, XTENSA_PMU_PMG
);
416 for (i
= 0; i
< XCHAL_NUM_PERF_COUNTERS
; ++i
) {
417 set_er(0, XTENSA_PMU_PMCTRL(i
));
418 set_er(get_er(XTENSA_PMU_PMSTAT(i
)), XTENSA_PMU_PMSTAT(i
));
423 static int __init
xtensa_pmu_init(void)
426 int irq
= irq_create_mapping(NULL
, XCHAL_PROFILING_INTERRUPT
);
428 ret
= cpuhp_setup_state(CPUHP_AP_PERF_XTENSA_STARTING
,
429 "perf/xtensa:starting", xtensa_pmu_setup
,
432 pr_err("xtensa_pmu: failed to register CPU-hotplug.\n");
438 ret
= request_irq(irq
, xtensa_pmu_irq_handler
, IRQF_PERCPU
,
444 ret
= perf_pmu_register(&xtensa_pmu
, "cpu", PERF_TYPE_RAW
);
450 early_initcall(xtensa_pmu_init
);