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_GET_EVENTID(ev) (ev->hw.config_base & 0xff)
24 #define HISI_MAX_PERIOD(nr) (BIT_ULL(nr) - 1)
27 * PMU format attributes
29 ssize_t
hisi_format_sysfs_show(struct device
*dev
,
30 struct device_attribute
*attr
, char *buf
)
32 struct dev_ext_attribute
*eattr
;
34 eattr
= container_of(attr
, struct dev_ext_attribute
, attr
);
36 return sprintf(buf
, "%s\n", (char *)eattr
->var
);
38 EXPORT_SYMBOL_GPL(hisi_format_sysfs_show
);
41 * PMU event attributes
43 ssize_t
hisi_event_sysfs_show(struct device
*dev
,
44 struct device_attribute
*attr
, char *page
)
46 struct dev_ext_attribute
*eattr
;
48 eattr
= container_of(attr
, struct dev_ext_attribute
, attr
);
50 return sprintf(page
, "config=0x%lx\n", (unsigned long)eattr
->var
);
52 EXPORT_SYMBOL_GPL(hisi_event_sysfs_show
);
55 * sysfs cpumask attributes. For uncore PMU, we only have a single CPU to show
57 ssize_t
hisi_cpumask_sysfs_show(struct device
*dev
,
58 struct device_attribute
*attr
, char *buf
)
60 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(dev_get_drvdata(dev
));
62 return sprintf(buf
, "%d\n", hisi_pmu
->on_cpu
);
64 EXPORT_SYMBOL_GPL(hisi_cpumask_sysfs_show
);
66 static bool hisi_validate_event_group(struct perf_event
*event
)
68 struct perf_event
*sibling
, *leader
= event
->group_leader
;
69 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
70 /* Include count for the event */
73 if (!is_software_event(leader
)) {
75 * We must NOT create groups containing mixed PMUs, although
76 * software events are acceptable
78 if (leader
->pmu
!= event
->pmu
)
81 /* Increment counter for the leader */
86 for_each_sibling_event(sibling
, event
->group_leader
) {
87 if (is_software_event(sibling
))
89 if (sibling
->pmu
!= event
->pmu
)
91 /* Increment counter for each sibling */
95 /* The group can not count events more than the counters in the HW */
96 return counters
<= hisi_pmu
->num_counters
;
99 int hisi_uncore_pmu_counter_valid(struct hisi_pmu
*hisi_pmu
, int idx
)
101 return idx
>= 0 && idx
< hisi_pmu
->num_counters
;
103 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_counter_valid
);
105 int hisi_uncore_pmu_get_event_idx(struct perf_event
*event
)
107 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
108 unsigned long *used_mask
= hisi_pmu
->pmu_events
.used_mask
;
109 u32 num_counters
= hisi_pmu
->num_counters
;
112 idx
= find_first_zero_bit(used_mask
, num_counters
);
113 if (idx
== num_counters
)
116 set_bit(idx
, used_mask
);
120 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_get_event_idx
);
122 ssize_t
hisi_uncore_pmu_identifier_attr_show(struct device
*dev
,
123 struct device_attribute
*attr
,
126 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(dev_get_drvdata(dev
));
128 return snprintf(page
, PAGE_SIZE
, "0x%08x\n", hisi_pmu
->identifier
);
130 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_identifier_attr_show
);
132 static void hisi_uncore_pmu_clear_event_idx(struct hisi_pmu
*hisi_pmu
, int idx
)
134 if (!hisi_uncore_pmu_counter_valid(hisi_pmu
, idx
)) {
135 dev_err(hisi_pmu
->dev
, "Unsupported event index:%d!\n", idx
);
139 clear_bit(idx
, hisi_pmu
->pmu_events
.used_mask
);
142 int hisi_uncore_pmu_event_init(struct perf_event
*event
)
144 struct hw_perf_event
*hwc
= &event
->hw
;
145 struct hisi_pmu
*hisi_pmu
;
147 if (event
->attr
.type
!= event
->pmu
->type
)
151 * We do not support sampling as the counters are all
152 * shared by all CPU cores in a CPU die(SCCL). Also we
153 * do not support attach to a task(per-process mode)
155 if (is_sampling_event(event
) || event
->attach_state
& PERF_ATTACH_TASK
)
159 * The uncore counters not specific to any CPU, so cannot
166 * Validate if the events in group does not exceed the
167 * available counters in hardware.
169 if (!hisi_validate_event_group(event
))
172 hisi_pmu
= to_hisi_pmu(event
->pmu
);
173 if (event
->attr
.config
> hisi_pmu
->check_event
)
176 if (hisi_pmu
->on_cpu
== -1)
179 * We don't assign an index until we actually place the event onto
180 * hardware. Use -1 to signify that we haven't decided where to put it
184 hwc
->config_base
= event
->attr
.config
;
186 /* Enforce to use the same CPU for all events in this PMU */
187 event
->cpu
= hisi_pmu
->on_cpu
;
191 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_event_init
);
194 * Set the counter to count the event that we're interested in,
195 * and enable interrupt and counter.
197 static void hisi_uncore_pmu_enable_event(struct perf_event
*event
)
199 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
200 struct hw_perf_event
*hwc
= &event
->hw
;
202 hisi_pmu
->ops
->write_evtype(hisi_pmu
, hwc
->idx
,
203 HISI_GET_EVENTID(event
));
205 hisi_pmu
->ops
->enable_counter_int(hisi_pmu
, hwc
);
206 hisi_pmu
->ops
->enable_counter(hisi_pmu
, hwc
);
210 * Disable counter and interrupt.
212 static void hisi_uncore_pmu_disable_event(struct perf_event
*event
)
214 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
215 struct hw_perf_event
*hwc
= &event
->hw
;
217 hisi_pmu
->ops
->disable_counter(hisi_pmu
, hwc
);
218 hisi_pmu
->ops
->disable_counter_int(hisi_pmu
, hwc
);
221 void hisi_uncore_pmu_set_event_period(struct perf_event
*event
)
223 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
224 struct hw_perf_event
*hwc
= &event
->hw
;
227 * The HiSilicon PMU counters support 32 bits or 48 bits, depending on
228 * the PMU. We reduce it to 2^(counter_bits - 1) to account for the
229 * extreme interrupt latency. So we could hopefully handle the overflow
230 * interrupt before another 2^(counter_bits - 1) events occur and the
231 * counter overtakes its previous value.
233 u64 val
= BIT_ULL(hisi_pmu
->counter_bits
- 1);
235 local64_set(&hwc
->prev_count
, val
);
236 /* Write start value to the hardware event counter */
237 hisi_pmu
->ops
->write_counter(hisi_pmu
, hwc
, val
);
239 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_set_event_period
);
241 void hisi_uncore_pmu_event_update(struct perf_event
*event
)
243 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
244 struct hw_perf_event
*hwc
= &event
->hw
;
245 u64 delta
, prev_raw_count
, new_raw_count
;
248 /* Read the count from the counter register */
249 new_raw_count
= hisi_pmu
->ops
->read_counter(hisi_pmu
, hwc
);
250 prev_raw_count
= local64_read(&hwc
->prev_count
);
251 } while (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
252 new_raw_count
) != prev_raw_count
);
256 delta
= (new_raw_count
- prev_raw_count
) &
257 HISI_MAX_PERIOD(hisi_pmu
->counter_bits
);
258 local64_add(delta
, &event
->count
);
260 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_event_update
);
262 void hisi_uncore_pmu_start(struct perf_event
*event
, int flags
)
264 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
265 struct hw_perf_event
*hwc
= &event
->hw
;
267 if (WARN_ON_ONCE(!(hwc
->state
& PERF_HES_STOPPED
)))
270 WARN_ON_ONCE(!(hwc
->state
& PERF_HES_UPTODATE
));
272 hisi_uncore_pmu_set_event_period(event
);
274 if (flags
& PERF_EF_RELOAD
) {
275 u64 prev_raw_count
= local64_read(&hwc
->prev_count
);
277 hisi_pmu
->ops
->write_counter(hisi_pmu
, hwc
, prev_raw_count
);
280 hisi_uncore_pmu_enable_event(event
);
281 perf_event_update_userpage(event
);
283 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_start
);
285 void hisi_uncore_pmu_stop(struct perf_event
*event
, int flags
)
287 struct hw_perf_event
*hwc
= &event
->hw
;
289 hisi_uncore_pmu_disable_event(event
);
290 WARN_ON_ONCE(hwc
->state
& PERF_HES_STOPPED
);
291 hwc
->state
|= PERF_HES_STOPPED
;
293 if (hwc
->state
& PERF_HES_UPTODATE
)
296 /* Read hardware counter and update the perf counter statistics */
297 hisi_uncore_pmu_event_update(event
);
298 hwc
->state
|= PERF_HES_UPTODATE
;
300 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_stop
);
302 int hisi_uncore_pmu_add(struct perf_event
*event
, int flags
)
304 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
305 struct hw_perf_event
*hwc
= &event
->hw
;
308 hwc
->state
= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
310 /* Get an available counter index for counting */
311 idx
= hisi_pmu
->ops
->get_event_idx(event
);
316 hisi_pmu
->pmu_events
.hw_events
[idx
] = event
;
318 if (flags
& PERF_EF_START
)
319 hisi_uncore_pmu_start(event
, PERF_EF_RELOAD
);
323 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_add
);
325 void hisi_uncore_pmu_del(struct perf_event
*event
, int flags
)
327 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
328 struct hw_perf_event
*hwc
= &event
->hw
;
330 hisi_uncore_pmu_stop(event
, PERF_EF_UPDATE
);
331 hisi_uncore_pmu_clear_event_idx(hisi_pmu
, hwc
->idx
);
332 perf_event_update_userpage(event
);
333 hisi_pmu
->pmu_events
.hw_events
[hwc
->idx
] = NULL
;
335 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_del
);
337 void hisi_uncore_pmu_read(struct perf_event
*event
)
339 /* Read hardware counter and update the perf counter statistics */
340 hisi_uncore_pmu_event_update(event
);
342 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_read
);
344 void hisi_uncore_pmu_enable(struct pmu
*pmu
)
346 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(pmu
);
347 int enabled
= bitmap_weight(hisi_pmu
->pmu_events
.used_mask
,
348 hisi_pmu
->num_counters
);
353 hisi_pmu
->ops
->start_counters(hisi_pmu
);
355 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_enable
);
357 void hisi_uncore_pmu_disable(struct pmu
*pmu
)
359 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(pmu
);
361 hisi_pmu
->ops
->stop_counters(hisi_pmu
);
363 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_disable
);
367 * The Super CPU Cluster (SCCL) and CPU Cluster (CCL) IDs can be
368 * determined from the MPIDR_EL1, but the encoding varies by CPU:
370 * - For MT variants of TSV110:
371 * SCCL is Aff2[7:3], CCL is Aff2[2:0]
373 * - For other MT parts:
374 * SCCL is Aff3[7:0], CCL is Aff2[7:0]
376 * - For non-MT parts:
377 * SCCL is Aff2[7:0], CCL is Aff1[7:0]
379 static void hisi_read_sccl_and_ccl_id(int *scclp
, int *cclp
)
381 u64 mpidr
= read_cpuid_mpidr();
382 int aff3
= MPIDR_AFFINITY_LEVEL(mpidr
, 3);
383 int aff2
= MPIDR_AFFINITY_LEVEL(mpidr
, 2);
384 int aff1
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
385 bool mt
= mpidr
& MPIDR_MT_BITMASK
;
388 if (mt
&& read_cpuid_part_number() == HISI_CPU_PART_TSV110
) {
406 * Check whether the CPU is associated with this uncore PMU
408 static bool hisi_pmu_cpu_is_associated_pmu(struct hisi_pmu
*hisi_pmu
)
412 if (hisi_pmu
->ccl_id
== -1) {
413 /* If CCL_ID is -1, the PMU only shares the same SCCL */
414 hisi_read_sccl_and_ccl_id(&sccl_id
, NULL
);
416 return sccl_id
== hisi_pmu
->sccl_id
;
419 hisi_read_sccl_and_ccl_id(&sccl_id
, &ccl_id
);
421 return sccl_id
== hisi_pmu
->sccl_id
&& ccl_id
== hisi_pmu
->ccl_id
;
424 int hisi_uncore_pmu_online_cpu(unsigned int cpu
, struct hlist_node
*node
)
426 struct hisi_pmu
*hisi_pmu
= hlist_entry_safe(node
, struct hisi_pmu
,
429 if (!hisi_pmu_cpu_is_associated_pmu(hisi_pmu
))
432 cpumask_set_cpu(cpu
, &hisi_pmu
->associated_cpus
);
434 /* If another CPU is already managing this PMU, simply return. */
435 if (hisi_pmu
->on_cpu
!= -1)
438 /* Use this CPU in cpumask for event counting */
439 hisi_pmu
->on_cpu
= cpu
;
441 /* Overflow interrupt also should use the same CPU */
442 WARN_ON(irq_set_affinity_hint(hisi_pmu
->irq
, cpumask_of(cpu
)));
446 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_online_cpu
);
448 int hisi_uncore_pmu_offline_cpu(unsigned int cpu
, struct hlist_node
*node
)
450 struct hisi_pmu
*hisi_pmu
= hlist_entry_safe(node
, struct hisi_pmu
,
452 cpumask_t pmu_online_cpus
;
455 if (!cpumask_test_and_clear_cpu(cpu
, &hisi_pmu
->associated_cpus
))
458 /* Nothing to do if this CPU doesn't own the PMU */
459 if (hisi_pmu
->on_cpu
!= cpu
)
462 /* Give up ownership of the PMU */
463 hisi_pmu
->on_cpu
= -1;
465 /* Choose a new CPU to migrate ownership of the PMU to */
466 cpumask_and(&pmu_online_cpus
, &hisi_pmu
->associated_cpus
,
468 target
= cpumask_any_but(&pmu_online_cpus
, cpu
);
469 if (target
>= nr_cpu_ids
)
472 perf_pmu_migrate_context(&hisi_pmu
->pmu
, cpu
, target
);
473 /* Use this CPU for event counting */
474 hisi_pmu
->on_cpu
= target
;
475 WARN_ON(irq_set_affinity_hint(hisi_pmu
->irq
, cpumask_of(target
)));
479 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_offline_cpu
);
481 MODULE_LICENSE("GPL v2");