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/local64.h>
20 #include "hisi_uncore_pmu.h"
22 #define HISI_GET_EVENTID(ev) (ev->hw.config_base & 0xff)
23 #define HISI_MAX_PERIOD(nr) (BIT_ULL(nr) - 1)
26 * PMU format attributes
28 ssize_t
hisi_format_sysfs_show(struct device
*dev
,
29 struct device_attribute
*attr
, char *buf
)
31 struct dev_ext_attribute
*eattr
;
33 eattr
= container_of(attr
, struct dev_ext_attribute
, attr
);
35 return sprintf(buf
, "%s\n", (char *)eattr
->var
);
39 * PMU event attributes
41 ssize_t
hisi_event_sysfs_show(struct device
*dev
,
42 struct device_attribute
*attr
, char *page
)
44 struct dev_ext_attribute
*eattr
;
46 eattr
= container_of(attr
, struct dev_ext_attribute
, attr
);
48 return sprintf(page
, "config=0x%lx\n", (unsigned long)eattr
->var
);
52 * sysfs cpumask attributes. For uncore PMU, we only have a single CPU to show
54 ssize_t
hisi_cpumask_sysfs_show(struct device
*dev
,
55 struct device_attribute
*attr
, char *buf
)
57 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(dev_get_drvdata(dev
));
59 return sprintf(buf
, "%d\n", hisi_pmu
->on_cpu
);
62 static bool hisi_validate_event_group(struct perf_event
*event
)
64 struct perf_event
*sibling
, *leader
= event
->group_leader
;
65 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
66 /* Include count for the event */
69 if (!is_software_event(leader
)) {
71 * We must NOT create groups containing mixed PMUs, although
72 * software events are acceptable
74 if (leader
->pmu
!= event
->pmu
)
77 /* Increment counter for the leader */
82 for_each_sibling_event(sibling
, event
->group_leader
) {
83 if (is_software_event(sibling
))
85 if (sibling
->pmu
!= event
->pmu
)
87 /* Increment counter for each sibling */
91 /* The group can not count events more than the counters in the HW */
92 return counters
<= hisi_pmu
->num_counters
;
95 int hisi_uncore_pmu_counter_valid(struct hisi_pmu
*hisi_pmu
, int idx
)
97 return idx
>= 0 && idx
< hisi_pmu
->num_counters
;
100 int hisi_uncore_pmu_get_event_idx(struct perf_event
*event
)
102 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
103 unsigned long *used_mask
= hisi_pmu
->pmu_events
.used_mask
;
104 u32 num_counters
= hisi_pmu
->num_counters
;
107 idx
= find_first_zero_bit(used_mask
, num_counters
);
108 if (idx
== num_counters
)
111 set_bit(idx
, used_mask
);
116 static void hisi_uncore_pmu_clear_event_idx(struct hisi_pmu
*hisi_pmu
, int idx
)
118 if (!hisi_uncore_pmu_counter_valid(hisi_pmu
, idx
)) {
119 dev_err(hisi_pmu
->dev
, "Unsupported event index:%d!\n", idx
);
123 clear_bit(idx
, hisi_pmu
->pmu_events
.used_mask
);
126 int hisi_uncore_pmu_event_init(struct perf_event
*event
)
128 struct hw_perf_event
*hwc
= &event
->hw
;
129 struct hisi_pmu
*hisi_pmu
;
131 if (event
->attr
.type
!= event
->pmu
->type
)
135 * We do not support sampling as the counters are all
136 * shared by all CPU cores in a CPU die(SCCL). Also we
137 * do not support attach to a task(per-process mode)
139 if (is_sampling_event(event
) || event
->attach_state
& PERF_ATTACH_TASK
)
143 * The uncore counters not specific to any CPU, so cannot
150 * Validate if the events in group does not exceed the
151 * available counters in hardware.
153 if (!hisi_validate_event_group(event
))
156 hisi_pmu
= to_hisi_pmu(event
->pmu
);
157 if (event
->attr
.config
> hisi_pmu
->check_event
)
160 if (hisi_pmu
->on_cpu
== -1)
163 * We don't assign an index until we actually place the event onto
164 * hardware. Use -1 to signify that we haven't decided where to put it
168 hwc
->config_base
= event
->attr
.config
;
170 /* Enforce to use the same CPU for all events in this PMU */
171 event
->cpu
= hisi_pmu
->on_cpu
;
177 * Set the counter to count the event that we're interested in,
178 * and enable interrupt and counter.
180 static void hisi_uncore_pmu_enable_event(struct perf_event
*event
)
182 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
183 struct hw_perf_event
*hwc
= &event
->hw
;
185 hisi_pmu
->ops
->write_evtype(hisi_pmu
, hwc
->idx
,
186 HISI_GET_EVENTID(event
));
188 hisi_pmu
->ops
->enable_counter_int(hisi_pmu
, hwc
);
189 hisi_pmu
->ops
->enable_counter(hisi_pmu
, hwc
);
193 * Disable counter and interrupt.
195 static void hisi_uncore_pmu_disable_event(struct perf_event
*event
)
197 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
198 struct hw_perf_event
*hwc
= &event
->hw
;
200 hisi_pmu
->ops
->disable_counter(hisi_pmu
, hwc
);
201 hisi_pmu
->ops
->disable_counter_int(hisi_pmu
, hwc
);
204 void hisi_uncore_pmu_set_event_period(struct perf_event
*event
)
206 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
207 struct hw_perf_event
*hwc
= &event
->hw
;
210 * The HiSilicon PMU counters support 32 bits or 48 bits, depending on
211 * the PMU. We reduce it to 2^(counter_bits - 1) to account for the
212 * extreme interrupt latency. So we could hopefully handle the overflow
213 * interrupt before another 2^(counter_bits - 1) events occur and the
214 * counter overtakes its previous value.
216 u64 val
= BIT_ULL(hisi_pmu
->counter_bits
- 1);
218 local64_set(&hwc
->prev_count
, val
);
219 /* Write start value to the hardware event counter */
220 hisi_pmu
->ops
->write_counter(hisi_pmu
, hwc
, val
);
223 void hisi_uncore_pmu_event_update(struct perf_event
*event
)
225 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
226 struct hw_perf_event
*hwc
= &event
->hw
;
227 u64 delta
, prev_raw_count
, new_raw_count
;
230 /* Read the count from the counter register */
231 new_raw_count
= hisi_pmu
->ops
->read_counter(hisi_pmu
, hwc
);
232 prev_raw_count
= local64_read(&hwc
->prev_count
);
233 } while (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
234 new_raw_count
) != prev_raw_count
);
238 delta
= (new_raw_count
- prev_raw_count
) &
239 HISI_MAX_PERIOD(hisi_pmu
->counter_bits
);
240 local64_add(delta
, &event
->count
);
243 void hisi_uncore_pmu_start(struct perf_event
*event
, int flags
)
245 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
246 struct hw_perf_event
*hwc
= &event
->hw
;
248 if (WARN_ON_ONCE(!(hwc
->state
& PERF_HES_STOPPED
)))
251 WARN_ON_ONCE(!(hwc
->state
& PERF_HES_UPTODATE
));
253 hisi_uncore_pmu_set_event_period(event
);
255 if (flags
& PERF_EF_RELOAD
) {
256 u64 prev_raw_count
= local64_read(&hwc
->prev_count
);
258 hisi_pmu
->ops
->write_counter(hisi_pmu
, hwc
, prev_raw_count
);
261 hisi_uncore_pmu_enable_event(event
);
262 perf_event_update_userpage(event
);
265 void hisi_uncore_pmu_stop(struct perf_event
*event
, int flags
)
267 struct hw_perf_event
*hwc
= &event
->hw
;
269 hisi_uncore_pmu_disable_event(event
);
270 WARN_ON_ONCE(hwc
->state
& PERF_HES_STOPPED
);
271 hwc
->state
|= PERF_HES_STOPPED
;
273 if (hwc
->state
& PERF_HES_UPTODATE
)
276 /* Read hardware counter and update the perf counter statistics */
277 hisi_uncore_pmu_event_update(event
);
278 hwc
->state
|= PERF_HES_UPTODATE
;
281 int hisi_uncore_pmu_add(struct perf_event
*event
, int flags
)
283 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
284 struct hw_perf_event
*hwc
= &event
->hw
;
287 hwc
->state
= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
289 /* Get an available counter index for counting */
290 idx
= hisi_pmu
->ops
->get_event_idx(event
);
295 hisi_pmu
->pmu_events
.hw_events
[idx
] = event
;
297 if (flags
& PERF_EF_START
)
298 hisi_uncore_pmu_start(event
, PERF_EF_RELOAD
);
303 void hisi_uncore_pmu_del(struct perf_event
*event
, int flags
)
305 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
306 struct hw_perf_event
*hwc
= &event
->hw
;
308 hisi_uncore_pmu_stop(event
, PERF_EF_UPDATE
);
309 hisi_uncore_pmu_clear_event_idx(hisi_pmu
, hwc
->idx
);
310 perf_event_update_userpage(event
);
311 hisi_pmu
->pmu_events
.hw_events
[hwc
->idx
] = NULL
;
314 void hisi_uncore_pmu_read(struct perf_event
*event
)
316 /* Read hardware counter and update the perf counter statistics */
317 hisi_uncore_pmu_event_update(event
);
320 void hisi_uncore_pmu_enable(struct pmu
*pmu
)
322 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(pmu
);
323 int enabled
= bitmap_weight(hisi_pmu
->pmu_events
.used_mask
,
324 hisi_pmu
->num_counters
);
329 hisi_pmu
->ops
->start_counters(hisi_pmu
);
332 void hisi_uncore_pmu_disable(struct pmu
*pmu
)
334 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(pmu
);
336 hisi_pmu
->ops
->stop_counters(hisi_pmu
);
340 * Read Super CPU cluster and CPU cluster ID from MPIDR_EL1.
341 * If multi-threading is supported, CCL_ID is the low 3-bits in MPIDR[Aff2]
342 * and SCCL_ID is the upper 5-bits of Aff2 field; if not, SCCL_ID
343 * is in MPIDR[Aff2] and CCL_ID is in MPIDR[Aff1].
345 static void hisi_read_sccl_and_ccl_id(int *sccl_id
, int *ccl_id
)
347 u64 mpidr
= read_cpuid_mpidr();
349 if (mpidr
& MPIDR_MT_BITMASK
) {
350 int aff2
= MPIDR_AFFINITY_LEVEL(mpidr
, 2);
353 *sccl_id
= aff2
>> 3;
355 *ccl_id
= aff2
& 0x7;
358 *sccl_id
= MPIDR_AFFINITY_LEVEL(mpidr
, 2);
360 *ccl_id
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
365 * Check whether the CPU is associated with this uncore PMU
367 static bool hisi_pmu_cpu_is_associated_pmu(struct hisi_pmu
*hisi_pmu
)
371 if (hisi_pmu
->ccl_id
== -1) {
372 /* If CCL_ID is -1, the PMU only shares the same SCCL */
373 hisi_read_sccl_and_ccl_id(&sccl_id
, NULL
);
375 return sccl_id
== hisi_pmu
->sccl_id
;
378 hisi_read_sccl_and_ccl_id(&sccl_id
, &ccl_id
);
380 return sccl_id
== hisi_pmu
->sccl_id
&& ccl_id
== hisi_pmu
->ccl_id
;
383 int hisi_uncore_pmu_online_cpu(unsigned int cpu
, struct hlist_node
*node
)
385 struct hisi_pmu
*hisi_pmu
= hlist_entry_safe(node
, struct hisi_pmu
,
388 if (!hisi_pmu_cpu_is_associated_pmu(hisi_pmu
))
391 cpumask_set_cpu(cpu
, &hisi_pmu
->associated_cpus
);
393 /* If another CPU is already managing this PMU, simply return. */
394 if (hisi_pmu
->on_cpu
!= -1)
397 /* Use this CPU in cpumask for event counting */
398 hisi_pmu
->on_cpu
= cpu
;
400 /* Overflow interrupt also should use the same CPU */
401 WARN_ON(irq_set_affinity(hisi_pmu
->irq
, cpumask_of(cpu
)));
406 int hisi_uncore_pmu_offline_cpu(unsigned int cpu
, struct hlist_node
*node
)
408 struct hisi_pmu
*hisi_pmu
= hlist_entry_safe(node
, struct hisi_pmu
,
410 cpumask_t pmu_online_cpus
;
413 if (!cpumask_test_and_clear_cpu(cpu
, &hisi_pmu
->associated_cpus
))
416 /* Nothing to do if this CPU doesn't own the PMU */
417 if (hisi_pmu
->on_cpu
!= cpu
)
420 /* Give up ownership of the PMU */
421 hisi_pmu
->on_cpu
= -1;
423 /* Choose a new CPU to migrate ownership of the PMU to */
424 cpumask_and(&pmu_online_cpus
, &hisi_pmu
->associated_cpus
,
426 target
= cpumask_any_but(&pmu_online_cpus
, cpu
);
427 if (target
>= nr_cpu_ids
)
430 perf_pmu_migrate_context(&hisi_pmu
->pmu
, cpu
, target
);
431 /* Use this CPU for event counting */
432 hisi_pmu
->on_cpu
= target
;
433 WARN_ON(irq_set_affinity(hisi_pmu
->irq
, cpumask_of(target
)));