21 #define pr_fmt(fmt) "hw perfevents: " fmt
23 #include <linux/bitmap.h>
25 #include <linux/kernel.h>
26 #include <linux/export.h>
27 #include <linux/perf_event.h>
32 #include <asm/cputype.h>
34 #include <asm/irq_regs.h>
36 #include <asm/stacktrace.h>
42 #define ARMPMU_MAX_HWEVENTS 32
48 #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu))
51 static struct arm_pmu *cpu_pmu;
59 max_events = cpu_pmu->num_events;
71 #define HW_OP_UNSUPPORTED 0xFFFF
74 PERF_COUNT_HW_CACHE_##_x
76 #define CACHE_OP_UNSUPPORTED 0xFFFF
79 armpmu_map_cache_event(
const unsigned (*cache_map)
87 cache_type = (config >> 0) & 0xff;
91 cache_op = (config >> 8) & 0xff;
95 cache_result = (config >> 16) & 0xff;
96 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
117 return (
int)(config & raw_event_mask);
122 const unsigned (*cache_map)
128 u64 config =
event->attr.config;
130 switch (event->
attr.type) {
134 return armpmu_map_cache_event(cache_map, config);
136 return armpmu_map_raw_event(raw_event_mask, config);
147 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
155 hwc->last_period =
period;
162 hwc->last_period =
period;
166 if (left > (
s64)armpmu->max_period)
167 left = armpmu->max_period;
171 armpmu->write_counter(idx, (
u64)(-left) & 0xffffffff);
183 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
184 u64 delta, prev_raw_count, new_raw_count;
188 new_raw_count = armpmu->read_counter(idx);
191 new_raw_count) != prev_raw_count)
194 delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
199 return new_raw_count;
217 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
225 armpmu->disable(hwc, hwc->idx);
233 armpmu_start(
struct perf_event *event,
int flags)
235 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
254 armpmu->enable(hwc, hwc->idx);
258 armpmu_del(
struct perf_event *event,
int flags)
260 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
261 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
275 armpmu_add(
struct perf_event *event,
int flags)
277 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
278 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
286 idx = armpmu->get_event_idx(hw_events, hwc);
297 armpmu->disable(hwc, idx);
302 armpmu_start(event, PERF_EF_RELOAD);
313 validate_event(
struct pmu_hw_events *hw_events,
316 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
318 struct pmu *leader_pmu =
event->group_leader->pmu;
323 return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
329 struct perf_event *sibling, *leader =
event->group_leader;
330 struct pmu_hw_events fake_pmu;
337 memset(fake_used_mask, 0,
sizeof(fake_used_mask));
338 fake_pmu.used_mask = fake_used_mask;
340 if (!validate_event(&fake_pmu, leader))
344 if (!validate_event(&fake_pmu, sibling))
348 if (!validate_event(&fake_pmu, event))
355 armpmu_release_hardware(
struct arm_pmu *armpmu)
362 for (i = 0; i < irqs; ++
i) {
363 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
372 armpmu_reserve_hardware(
struct arm_pmu *armpmu)
374 int i,
err, irq, irqs;
378 pr_err(
"no PMU device registered\n");
384 pr_err(
"no irqs for PMUs defined\n");
388 for (i = 0; i < irqs; ++
i) {
399 if (irq_set_affinity(irq,
cpumask_of(i)) && irqs > 1) {
400 pr_warning(
"unable to set irq affinity (irq=%d, cpu=%u)\n",
409 pr_err(
"unable to request IRQ%d for ARM PMU counters\n",
411 armpmu_release_hardware(armpmu);
415 cpumask_set_cpu(i, &armpmu->active_irqs);
422 hw_perf_event_destroy(
struct perf_event *event)
424 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
425 atomic_t *active_events = &armpmu->active_events;
426 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
429 armpmu_release_hardware(armpmu);
442 __hw_perf_event_init(
struct perf_event *event)
444 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
448 mapping = armpmu->map_event(event);
451 pr_debug(
"event %x:%llx not supported\n", event->
attr.type,
463 hwc->config_base = 0;
470 if ((!armpmu->set_event_filter ||
471 armpmu->set_event_filter(hwc, &event->
attr)) &&
472 event_requires_mode_exclusion(&event->
attr)) {
473 pr_debug(
"ARM performance counters do not support mode exclusion\n");
480 hwc->config_base |= (
unsigned long)mapping;
482 if (!hwc->sample_period) {
489 hwc->sample_period = armpmu->max_period >> 1;
490 hwc->last_period = hwc->sample_period;
491 local64_set(&hwc->period_left, hwc->sample_period);
495 if (event->group_leader != event) {
496 err = validate_group(event);
504 static int armpmu_event_init(
struct perf_event *event)
506 struct arm_pmu *armpmu =
to_arm_pmu(event->pmu);
508 atomic_t *active_events = &armpmu->active_events;
510 if (armpmu->map_event(event) == -
ENOENT)
513 event->destroy = hw_perf_event_destroy;
518 err = armpmu_reserve_hardware(armpmu);
528 err = __hw_perf_event_init(event);
530 hw_perf_event_destroy(event);
535 static void armpmu_enable(
struct pmu *
pmu)
538 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
539 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
545 static void armpmu_disable(
struct pmu *pmu)
551 static void __init armpmu_init(
struct arm_pmu *armpmu)
556 armpmu->pmu = (
struct pmu) {
557 .pmu_enable = armpmu_enable,
558 .pmu_disable = armpmu_disable,
559 .event_init = armpmu_event_init,
562 .start = armpmu_start,
737 #define ARMV8_IDX_CYCLE_COUNTER 0
738 #define ARMV8_IDX_COUNTER0 1
739 #define ARMV8_IDX_COUNTER_LAST (ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
741 #define ARMV8_MAX_COUNTERS 32
742 #define ARMV8_COUNTER_MASK (ARMV8_MAX_COUNTERS - 1)
751 #define ARMV8_IDX_TO_COUNTER(x) \
752 (((x) - ARMV8_IDX_COUNTER0) & ARMV8_COUNTER_MASK)
757 #define ARMV8_PMCR_E (1 << 0)
758 #define ARMV8_PMCR_P (1 << 1)
759 #define ARMV8_PMCR_C (1 << 2)
760 #define ARMV8_PMCR_D (1 << 3)
761 #define ARMV8_PMCR_X (1 << 4)
762 #define ARMV8_PMCR_DP (1 << 5)
763 #define ARMV8_PMCR_N_SHIFT 11
764 #define ARMV8_PMCR_N_MASK 0x1f
765 #define ARMV8_PMCR_MASK 0x3f
770 #define ARMV8_OVSR_MASK 0xffffffff
771 #define ARMV8_OVERFLOWED_MASK ARMV8_OVSR_MASK
776 #define ARMV8_EVTYPE_MASK 0xc00000ff
777 #define ARMV8_EVTYPE_EVENT 0xff
782 #define ARMV8_EXCLUDE_EL1 (1 << 31)
783 #define ARMV8_EXCLUDE_EL0 (1 << 30)
784 #define ARMV8_INCLUDE_EL2 (1 << 27)
786 static inline u32 armv8pmu_pmcr_read(
void)
789 asm volatile(
"mrs %0, pmcr_el0" :
"=r" (
val));
793 static inline void armv8pmu_pmcr_write(
u32 val)
797 asm volatile(
"msr pmcr_el0, %0" ::
"r" (
val));
800 static inline int armv8pmu_has_overflowed(
u32 pmovsr)
805 static inline int armv8pmu_counter_valid(
int idx)
810 static inline int armv8pmu_counter_has_overflowed(
u32 pmnc,
int idx)
815 if (!armv8pmu_counter_valid(idx)) {
816 pr_err(
"CPU%u checking wrong counter %d overflow status\n",
820 ret = pmnc &
BIT(counter);
826 static inline int armv8pmu_select_counter(
int idx)
830 if (!armv8pmu_counter_valid(idx)) {
831 pr_err(
"CPU%u selecting wrong PMNC counter %d\n",
837 asm volatile(
"msr pmselr_el0, %0" ::
"r" (
counter));
843 static inline u32 armv8pmu_read_counter(
int idx)
847 if (!armv8pmu_counter_valid(idx))
848 pr_err(
"CPU%u reading wrong counter %d\n",
851 asm volatile(
"mrs %0, pmccntr_el0" :
"=r" (
value));
852 else if (armv8pmu_select_counter(idx) ==
idx)
853 asm volatile(
"mrs %0, pmxevcntr_el0" :
"=r" (value));
858 static inline void armv8pmu_write_counter(
int idx,
u32 value)
860 if (!armv8pmu_counter_valid(idx))
861 pr_err(
"CPU%u writing wrong counter %d\n",
864 asm volatile(
"msr pmccntr_el0, %0" ::
"r" (
value));
865 else if (armv8pmu_select_counter(idx) ==
idx)
866 asm volatile(
"msr pmxevcntr_el0, %0" ::
"r" (value));
869 static inline void armv8pmu_write_evtype(
int idx,
u32 val)
871 if (armv8pmu_select_counter(idx) == idx) {
873 asm volatile(
"msr pmxevtyper_el0, %0" ::
"r" (
val));
877 static inline int armv8pmu_enable_counter(
int idx)
881 if (!armv8pmu_counter_valid(idx)) {
882 pr_err(
"CPU%u enabling wrong PMNC counter %d\n",
888 asm volatile(
"msr pmcntenset_el0, %0" ::
"r" (
BIT(counter)));
892 static inline int armv8pmu_disable_counter(
int idx)
896 if (!armv8pmu_counter_valid(idx)) {
897 pr_err(
"CPU%u disabling wrong PMNC counter %d\n",
903 asm volatile(
"msr pmcntenclr_el0, %0" ::
"r" (
BIT(counter)));
907 static inline int armv8pmu_enable_intens(
int idx)
911 if (!armv8pmu_counter_valid(idx)) {
912 pr_err(
"CPU%u enabling wrong PMNC counter IRQ enable %d\n",
918 asm volatile(
"msr pmintenset_el1, %0" ::
"r" (
BIT(counter)));
922 static inline int armv8pmu_disable_intens(
int idx)
926 if (!armv8pmu_counter_valid(idx)) {
927 pr_err(
"CPU%u disabling wrong PMNC counter IRQ enable %d\n",
933 asm volatile(
"msr pmintenclr_el1, %0" ::
"r" (
BIT(counter)));
936 asm volatile(
"msr pmovsclr_el0, %0" ::
"r" (
BIT(counter)));
941 static inline u32 armv8pmu_getreset_flags(
void)
946 asm volatile(
"mrs %0, pmovsclr_el0" :
"=r" (
value));
950 asm volatile(
"msr pmovsclr_el0, %0" ::
"r" (
value));
955 static void armv8pmu_enable_event(
struct hw_perf_event *hwc,
int idx)
958 struct pmu_hw_events *
events = cpu_pmu->get_hw_events();
969 armv8pmu_disable_counter(idx);
974 armv8pmu_write_evtype(idx, hwc->config_base);
979 armv8pmu_enable_intens(idx);
984 armv8pmu_enable_counter(idx);
989 static void armv8pmu_disable_event(
struct hw_perf_event *hwc,
int idx)
992 struct pmu_hw_events *events = cpu_pmu->get_hw_events();
1002 armv8pmu_disable_counter(idx);
1007 armv8pmu_disable_intens(idx);
1015 struct perf_sample_data
data;
1016 struct pmu_hw_events *cpuc;
1023 pmovsr = armv8pmu_getreset_flags();
1028 if (!armv8pmu_has_overflowed(pmovsr))
1037 for (idx = 0; idx < cpu_pmu->num_events; ++
idx) {
1049 if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
1054 perf_sample_data_init(&
data, 0, hwc->last_period);
1059 cpu_pmu->disable(hwc, idx);
1074 static void armv8pmu_start(
void)
1076 unsigned long flags;
1077 struct pmu_hw_events *events = cpu_pmu->get_hw_events();
1081 armv8pmu_pmcr_write(armv8pmu_pmcr_read() |
ARMV8_PMCR_E);
1085 static void armv8pmu_stop(
void)
1087 unsigned long flags;
1088 struct pmu_hw_events *events = cpu_pmu->get_hw_events();
1092 armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~
ARMV8_PMCR_E);
1096 static int armv8pmu_get_event_idx(
struct pmu_hw_events *cpuc,
1126 static int armv8pmu_set_event_filter(
struct hw_perf_event *event,
1129 unsigned long config_base = 0;
1144 event->config_base = config_base;
1149 static void armv8pmu_reset(
void *
info)
1151 u32 idx, nb_cnt = cpu_pmu->num_events;
1155 armv8pmu_disable_event(
NULL, idx);
1161 asm volatile(
"msr pmuserenr_el0, %0" ::
"r" (0));
1164 static int armv8_pmuv3_map_event(
struct perf_event *event)
1166 return map_cpu_event(event, &armv8_pmuv3_perf_map,
1167 &armv8_pmuv3_perf_cache_map, 0xFF);
1170 static struct arm_pmu armv8pmu = {
1171 .handle_irq = armv8pmu_handle_irq,
1172 .enable = armv8pmu_enable_event,
1173 .disable = armv8pmu_disable_event,
1174 .read_counter = armv8pmu_read_counter,
1175 .write_counter = armv8pmu_write_counter,
1176 .get_event_idx = armv8pmu_get_event_idx,
1177 .start = armv8pmu_start,
1178 .stop = armv8pmu_stop,
1179 .reset = armv8pmu_reset,
1180 .max_period = (1LLU << 32) - 1,
1183 static u32 __init armv8pmu_read_num_pmnc_events(
void)
1194 static struct arm_pmu *
__init armv8_pmuv3_pmu_init(
void)
1196 armv8pmu.name =
"arm/armv8-pmuv3";
1197 armv8pmu.map_event = armv8_pmuv3_map_event;
1198 armv8pmu.num_events = armv8pmu_read_num_pmnc_events();
1199 armv8pmu.set_event_filter = armv8pmu_set_event_filter;
1210 if (cpu_pmu && cpu_pmu->reset)
1220 {.compatible =
"arm,armv8-pmuv3"},
1229 cpu_pmu->plat_device = pdev;
1236 .of_match_table = armpmu_of_device_ids,
1238 .probe = armpmu_device_probe,
1241 static int __init register_pmu_driver(
void)
1247 static struct pmu_hw_events *armpmu_get_cpu_events(
void)
1252 static void __init cpu_pmu_init(
struct arm_pmu *armpmu)
1257 events->events =
per_cpu(hw_events, cpu);
1258 events->used_mask =
per_cpu(used_mask, cpu);
1261 armpmu->get_hw_events = armpmu_get_cpu_events;
1268 switch ((dfr >> 8) & 0xf) {
1270 cpu_pmu = armv8_pmuv3_pmu_init();
1275 pr_info(
"enabled with %s PMU driver, %d counters available\n",
1276 cpu_pmu->name, cpu_pmu->num_events);
1277 cpu_pmu_init(cpu_pmu);
1280 pr_info(
"no hardware support available\n");
1310 pagefault_disable();
1317 perf_callchain_store(entry, buftail.
lr);
1323 if (tail >= buftail.
fp)
1337 tail && !((
unsigned long)tail & 0xf))
1338 tail = user_backtrace(tail, entry);
1349 perf_callchain_store(entry, frame->
pc);
1358 frame.
fp = regs->
regs[29];
1359 frame.
sp = regs->
sp;
1360 frame.
pc = regs->
pc;