1 // SPDX-License-Identifier: GPL-2.0-only
3 * HiSilicon SoC Hardware event counters support
5 * Copyright (C) 2017 HiSilicon Limited
6 * Author: Anurup M <anurup.m@huawei.com>
7 * Shaokun Zhang <zhangshaokun@hisilicon.com>
9 * This code is based on the uncore PMUs like arm-cci and arm-ccn.
11 #include <linux/bitmap.h>
12 #include <linux/bitops.h>
13 #include <linux/bug.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/interrupt.h>
18 #include <asm/cputype.h>
19 #include <asm/local64.h>
21 #include "hisi_uncore_pmu.h"
23 #define HISI_MAX_PERIOD(nr) (GENMASK_ULL((nr) - 1, 0))
26 * PMU event attributes
28 ssize_t
hisi_event_sysfs_show(struct device
*dev
,
29 struct device_attribute
*attr
, char *page
)
31 struct dev_ext_attribute
*eattr
;
33 eattr
= container_of(attr
, struct dev_ext_attribute
, attr
);
35 return sysfs_emit(page
, "config=0x%lx\n", (unsigned long)eattr
->var
);
37 EXPORT_SYMBOL_GPL(hisi_event_sysfs_show
);
40 * sysfs cpumask attributes. For uncore PMU, we only have a single CPU to show
42 ssize_t
hisi_cpumask_sysfs_show(struct device
*dev
,
43 struct device_attribute
*attr
, char *buf
)
45 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(dev_get_drvdata(dev
));
47 return sysfs_emit(buf
, "%d\n", hisi_pmu
->on_cpu
);
49 EXPORT_SYMBOL_GPL(hisi_cpumask_sysfs_show
);
51 static bool hisi_validate_event_group(struct perf_event
*event
)
53 struct perf_event
*sibling
, *leader
= event
->group_leader
;
54 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
55 /* Include count for the event */
58 if (!is_software_event(leader
)) {
60 * We must NOT create groups containing mixed PMUs, although
61 * software events are acceptable
63 if (leader
->pmu
!= event
->pmu
)
66 /* Increment counter for the leader */
71 for_each_sibling_event(sibling
, event
->group_leader
) {
72 if (is_software_event(sibling
))
74 if (sibling
->pmu
!= event
->pmu
)
76 /* Increment counter for each sibling */
80 /* The group can not count events more than the counters in the HW */
81 return counters
<= hisi_pmu
->num_counters
;
84 int hisi_uncore_pmu_get_event_idx(struct perf_event
*event
)
86 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
87 unsigned long *used_mask
= hisi_pmu
->pmu_events
.used_mask
;
88 u32 num_counters
= hisi_pmu
->num_counters
;
91 idx
= find_first_zero_bit(used_mask
, num_counters
);
92 if (idx
== num_counters
)
95 set_bit(idx
, used_mask
);
99 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_get_event_idx
);
101 ssize_t
hisi_uncore_pmu_identifier_attr_show(struct device
*dev
,
102 struct device_attribute
*attr
,
105 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(dev_get_drvdata(dev
));
107 return sysfs_emit(page
, "0x%08x\n", hisi_pmu
->identifier
);
109 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_identifier_attr_show
);
111 static void hisi_uncore_pmu_clear_event_idx(struct hisi_pmu
*hisi_pmu
, int idx
)
113 clear_bit(idx
, hisi_pmu
->pmu_events
.used_mask
);
116 static irqreturn_t
hisi_uncore_pmu_isr(int irq
, void *data
)
118 struct hisi_pmu
*hisi_pmu
= data
;
119 struct perf_event
*event
;
120 unsigned long overflown
;
123 overflown
= hisi_pmu
->ops
->get_int_status(hisi_pmu
);
128 * Find the counter index which overflowed if the bit was set
131 for_each_set_bit(idx
, &overflown
, hisi_pmu
->num_counters
) {
132 /* Write 1 to clear the IRQ status flag */
133 hisi_pmu
->ops
->clear_int_status(hisi_pmu
, idx
);
134 /* Get the corresponding event struct */
135 event
= hisi_pmu
->pmu_events
.hw_events
[idx
];
139 hisi_uncore_pmu_event_update(event
);
140 hisi_uncore_pmu_set_event_period(event
);
146 int hisi_uncore_pmu_init_irq(struct hisi_pmu
*hisi_pmu
,
147 struct platform_device
*pdev
)
151 irq
= platform_get_irq(pdev
, 0);
155 ret
= devm_request_irq(&pdev
->dev
, irq
, hisi_uncore_pmu_isr
,
156 IRQF_NOBALANCING
| IRQF_NO_THREAD
,
157 dev_name(&pdev
->dev
), hisi_pmu
);
160 "Fail to request IRQ: %d ret: %d.\n", irq
, ret
);
168 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_init_irq
);
170 int hisi_uncore_pmu_event_init(struct perf_event
*event
)
172 struct hw_perf_event
*hwc
= &event
->hw
;
173 struct hisi_pmu
*hisi_pmu
;
175 if (event
->attr
.type
!= event
->pmu
->type
)
179 * We do not support sampling as the counters are all
180 * shared by all CPU cores in a CPU die(SCCL). Also we
181 * do not support attach to a task(per-process mode)
183 if (is_sampling_event(event
) || event
->attach_state
& PERF_ATTACH_TASK
)
187 * The uncore counters not specific to any CPU, so cannot
194 * Validate if the events in group does not exceed the
195 * available counters in hardware.
197 if (!hisi_validate_event_group(event
))
200 hisi_pmu
= to_hisi_pmu(event
->pmu
);
201 if (event
->attr
.config
> hisi_pmu
->check_event
)
204 if (hisi_pmu
->on_cpu
== -1)
207 * We don't assign an index until we actually place the event onto
208 * hardware. Use -1 to signify that we haven't decided where to put it
212 hwc
->config_base
= event
->attr
.config
;
214 if (hisi_pmu
->ops
->check_filter
&& hisi_pmu
->ops
->check_filter(event
))
217 /* Enforce to use the same CPU for all events in this PMU */
218 event
->cpu
= hisi_pmu
->on_cpu
;
222 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_event_init
);
225 * Set the counter to count the event that we're interested in,
226 * and enable interrupt and counter.
228 static void hisi_uncore_pmu_enable_event(struct perf_event
*event
)
230 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
231 struct hw_perf_event
*hwc
= &event
->hw
;
233 hisi_pmu
->ops
->write_evtype(hisi_pmu
, hwc
->idx
,
234 HISI_GET_EVENTID(event
));
236 if (hisi_pmu
->ops
->enable_filter
)
237 hisi_pmu
->ops
->enable_filter(event
);
239 hisi_pmu
->ops
->enable_counter_int(hisi_pmu
, hwc
);
240 hisi_pmu
->ops
->enable_counter(hisi_pmu
, hwc
);
244 * Disable counter and interrupt.
246 static void hisi_uncore_pmu_disable_event(struct perf_event
*event
)
248 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
249 struct hw_perf_event
*hwc
= &event
->hw
;
251 hisi_pmu
->ops
->disable_counter(hisi_pmu
, hwc
);
252 hisi_pmu
->ops
->disable_counter_int(hisi_pmu
, hwc
);
254 if (hisi_pmu
->ops
->disable_filter
)
255 hisi_pmu
->ops
->disable_filter(event
);
258 void hisi_uncore_pmu_set_event_period(struct perf_event
*event
)
260 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
261 struct hw_perf_event
*hwc
= &event
->hw
;
264 * The HiSilicon PMU counters support 32 bits or 48 bits, depending on
265 * the PMU. We reduce it to 2^(counter_bits - 1) to account for the
266 * extreme interrupt latency. So we could hopefully handle the overflow
267 * interrupt before another 2^(counter_bits - 1) events occur and the
268 * counter overtakes its previous value.
270 u64 val
= BIT_ULL(hisi_pmu
->counter_bits
- 1);
272 local64_set(&hwc
->prev_count
, val
);
273 /* Write start value to the hardware event counter */
274 hisi_pmu
->ops
->write_counter(hisi_pmu
, hwc
, val
);
276 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_set_event_period
);
278 void hisi_uncore_pmu_event_update(struct perf_event
*event
)
280 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
281 struct hw_perf_event
*hwc
= &event
->hw
;
282 u64 delta
, prev_raw_count
, new_raw_count
;
285 /* Read the count from the counter register */
286 new_raw_count
= hisi_pmu
->ops
->read_counter(hisi_pmu
, hwc
);
287 prev_raw_count
= local64_read(&hwc
->prev_count
);
288 } while (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
289 new_raw_count
) != prev_raw_count
);
293 delta
= (new_raw_count
- prev_raw_count
) &
294 HISI_MAX_PERIOD(hisi_pmu
->counter_bits
);
295 local64_add(delta
, &event
->count
);
297 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_event_update
);
299 void hisi_uncore_pmu_start(struct perf_event
*event
, int flags
)
301 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
302 struct hw_perf_event
*hwc
= &event
->hw
;
304 if (WARN_ON_ONCE(!(hwc
->state
& PERF_HES_STOPPED
)))
307 WARN_ON_ONCE(!(hwc
->state
& PERF_HES_UPTODATE
));
309 hisi_uncore_pmu_set_event_period(event
);
311 if (flags
& PERF_EF_RELOAD
) {
312 u64 prev_raw_count
= local64_read(&hwc
->prev_count
);
314 hisi_pmu
->ops
->write_counter(hisi_pmu
, hwc
, prev_raw_count
);
317 hisi_uncore_pmu_enable_event(event
);
318 perf_event_update_userpage(event
);
320 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_start
);
322 void hisi_uncore_pmu_stop(struct perf_event
*event
, int flags
)
324 struct hw_perf_event
*hwc
= &event
->hw
;
326 hisi_uncore_pmu_disable_event(event
);
327 WARN_ON_ONCE(hwc
->state
& PERF_HES_STOPPED
);
328 hwc
->state
|= PERF_HES_STOPPED
;
330 if (hwc
->state
& PERF_HES_UPTODATE
)
333 /* Read hardware counter and update the perf counter statistics */
334 hisi_uncore_pmu_event_update(event
);
335 hwc
->state
|= PERF_HES_UPTODATE
;
337 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_stop
);
339 int hisi_uncore_pmu_add(struct perf_event
*event
, int flags
)
341 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
342 struct hw_perf_event
*hwc
= &event
->hw
;
345 hwc
->state
= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
347 /* Get an available counter index for counting */
348 idx
= hisi_pmu
->ops
->get_event_idx(event
);
353 hisi_pmu
->pmu_events
.hw_events
[idx
] = event
;
355 if (flags
& PERF_EF_START
)
356 hisi_uncore_pmu_start(event
, PERF_EF_RELOAD
);
360 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_add
);
362 void hisi_uncore_pmu_del(struct perf_event
*event
, int flags
)
364 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
365 struct hw_perf_event
*hwc
= &event
->hw
;
367 hisi_uncore_pmu_stop(event
, PERF_EF_UPDATE
);
368 hisi_uncore_pmu_clear_event_idx(hisi_pmu
, hwc
->idx
);
369 perf_event_update_userpage(event
);
370 hisi_pmu
->pmu_events
.hw_events
[hwc
->idx
] = NULL
;
372 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_del
);
374 void hisi_uncore_pmu_read(struct perf_event
*event
)
376 /* Read hardware counter and update the perf counter statistics */
377 hisi_uncore_pmu_event_update(event
);
379 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_read
);
381 void hisi_uncore_pmu_enable(struct pmu
*pmu
)
383 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(pmu
);
384 bool enabled
= !bitmap_empty(hisi_pmu
->pmu_events
.used_mask
,
385 hisi_pmu
->num_counters
);
390 hisi_pmu
->ops
->start_counters(hisi_pmu
);
392 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_enable
);
394 void hisi_uncore_pmu_disable(struct pmu
*pmu
)
396 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(pmu
);
398 hisi_pmu
->ops
->stop_counters(hisi_pmu
);
400 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_disable
);
404 * The Super CPU Cluster (SCCL) and CPU Cluster (CCL) IDs can be
405 * determined from the MPIDR_EL1, but the encoding varies by CPU:
407 * - For MT variants of TSV110:
408 * SCCL is Aff2[7:3], CCL is Aff2[2:0]
410 * - For other MT parts:
411 * SCCL is Aff3[7:0], CCL is Aff2[7:0]
413 * - For non-MT parts:
414 * SCCL is Aff2[7:0], CCL is Aff1[7:0]
416 static void hisi_read_sccl_and_ccl_id(int *scclp
, int *cclp
)
418 u64 mpidr
= read_cpuid_mpidr();
419 int aff3
= MPIDR_AFFINITY_LEVEL(mpidr
, 3);
420 int aff2
= MPIDR_AFFINITY_LEVEL(mpidr
, 2);
421 int aff1
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
422 bool mt
= mpidr
& MPIDR_MT_BITMASK
;
425 if (mt
&& read_cpuid_part_number() == HISI_CPU_PART_TSV110
) {
443 * Check whether the CPU is associated with this uncore PMU
445 static bool hisi_pmu_cpu_is_associated_pmu(struct hisi_pmu
*hisi_pmu
)
449 /* If SCCL_ID is -1, the PMU is in a SICL and has no CPU affinity */
450 if (hisi_pmu
->sccl_id
== -1)
453 if (hisi_pmu
->ccl_id
== -1) {
454 /* If CCL_ID is -1, the PMU only shares the same SCCL */
455 hisi_read_sccl_and_ccl_id(&sccl_id
, NULL
);
457 return sccl_id
== hisi_pmu
->sccl_id
;
460 hisi_read_sccl_and_ccl_id(&sccl_id
, &ccl_id
);
462 return sccl_id
== hisi_pmu
->sccl_id
&& ccl_id
== hisi_pmu
->ccl_id
;
465 int hisi_uncore_pmu_online_cpu(unsigned int cpu
, struct hlist_node
*node
)
467 struct hisi_pmu
*hisi_pmu
= hlist_entry_safe(node
, struct hisi_pmu
,
470 if (!hisi_pmu_cpu_is_associated_pmu(hisi_pmu
))
473 cpumask_set_cpu(cpu
, &hisi_pmu
->associated_cpus
);
475 /* If another CPU is already managing this PMU, simply return. */
476 if (hisi_pmu
->on_cpu
!= -1)
479 /* Use this CPU in cpumask for event counting */
480 hisi_pmu
->on_cpu
= cpu
;
482 /* Overflow interrupt also should use the same CPU */
483 WARN_ON(irq_set_affinity(hisi_pmu
->irq
, cpumask_of(cpu
)));
487 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_online_cpu
);
489 int hisi_uncore_pmu_offline_cpu(unsigned int cpu
, struct hlist_node
*node
)
491 struct hisi_pmu
*hisi_pmu
= hlist_entry_safe(node
, struct hisi_pmu
,
495 if (!cpumask_test_and_clear_cpu(cpu
, &hisi_pmu
->associated_cpus
))
498 /* Nothing to do if this CPU doesn't own the PMU */
499 if (hisi_pmu
->on_cpu
!= cpu
)
502 /* Give up ownership of the PMU */
503 hisi_pmu
->on_cpu
= -1;
505 /* Choose a new CPU to migrate ownership of the PMU to */
506 target
= cpumask_any_and_but(&hisi_pmu
->associated_cpus
,
507 cpu_online_mask
, cpu
);
508 if (target
>= nr_cpu_ids
)
511 perf_pmu_migrate_context(&hisi_pmu
->pmu
, cpu
, target
);
512 /* Use this CPU for event counting */
513 hisi_pmu
->on_cpu
= target
;
514 WARN_ON(irq_set_affinity(hisi_pmu
->irq
, cpumask_of(target
)));
518 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_offline_cpu
);
520 void hisi_pmu_init(struct hisi_pmu
*hisi_pmu
, struct module
*module
)
522 struct pmu
*pmu
= &hisi_pmu
->pmu
;
524 pmu
->module
= module
;
525 pmu
->parent
= hisi_pmu
->dev
;
526 pmu
->task_ctx_nr
= perf_invalid_context
;
527 pmu
->event_init
= hisi_uncore_pmu_event_init
;
528 pmu
->pmu_enable
= hisi_uncore_pmu_enable
;
529 pmu
->pmu_disable
= hisi_uncore_pmu_disable
;
530 pmu
->add
= hisi_uncore_pmu_add
;
531 pmu
->del
= hisi_uncore_pmu_del
;
532 pmu
->start
= hisi_uncore_pmu_start
;
533 pmu
->stop
= hisi_uncore_pmu_stop
;
534 pmu
->read
= hisi_uncore_pmu_read
;
535 pmu
->attr_groups
= hisi_pmu
->pmu_events
.attr_groups
;
536 pmu
->capabilities
= PERF_PMU_CAP_NO_EXCLUDE
;
538 EXPORT_SYMBOL_GPL(hisi_pmu_init
);
540 MODULE_DESCRIPTION("HiSilicon SoC uncore Performance Monitor driver framework");
541 MODULE_LICENSE("GPL v2");