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
);
40 * PMU event attributes
42 ssize_t
hisi_event_sysfs_show(struct device
*dev
,
43 struct device_attribute
*attr
, char *page
)
45 struct dev_ext_attribute
*eattr
;
47 eattr
= container_of(attr
, struct dev_ext_attribute
, attr
);
49 return sprintf(page
, "config=0x%lx\n", (unsigned long)eattr
->var
);
53 * sysfs cpumask attributes. For uncore PMU, we only have a single CPU to show
55 ssize_t
hisi_cpumask_sysfs_show(struct device
*dev
,
56 struct device_attribute
*attr
, char *buf
)
58 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(dev_get_drvdata(dev
));
60 return sprintf(buf
, "%d\n", hisi_pmu
->on_cpu
);
63 static bool hisi_validate_event_group(struct perf_event
*event
)
65 struct perf_event
*sibling
, *leader
= event
->group_leader
;
66 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
67 /* Include count for the event */
70 if (!is_software_event(leader
)) {
72 * We must NOT create groups containing mixed PMUs, although
73 * software events are acceptable
75 if (leader
->pmu
!= event
->pmu
)
78 /* Increment counter for the leader */
83 for_each_sibling_event(sibling
, event
->group_leader
) {
84 if (is_software_event(sibling
))
86 if (sibling
->pmu
!= event
->pmu
)
88 /* Increment counter for each sibling */
92 /* The group can not count events more than the counters in the HW */
93 return counters
<= hisi_pmu
->num_counters
;
96 int hisi_uncore_pmu_counter_valid(struct hisi_pmu
*hisi_pmu
, int idx
)
98 return idx
>= 0 && idx
< hisi_pmu
->num_counters
;
101 int hisi_uncore_pmu_get_event_idx(struct perf_event
*event
)
103 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
104 unsigned long *used_mask
= hisi_pmu
->pmu_events
.used_mask
;
105 u32 num_counters
= hisi_pmu
->num_counters
;
108 idx
= find_first_zero_bit(used_mask
, num_counters
);
109 if (idx
== num_counters
)
112 set_bit(idx
, used_mask
);
117 static void hisi_uncore_pmu_clear_event_idx(struct hisi_pmu
*hisi_pmu
, int idx
)
119 if (!hisi_uncore_pmu_counter_valid(hisi_pmu
, idx
)) {
120 dev_err(hisi_pmu
->dev
, "Unsupported event index:%d!\n", idx
);
124 clear_bit(idx
, hisi_pmu
->pmu_events
.used_mask
);
127 int hisi_uncore_pmu_event_init(struct perf_event
*event
)
129 struct hw_perf_event
*hwc
= &event
->hw
;
130 struct hisi_pmu
*hisi_pmu
;
132 if (event
->attr
.type
!= event
->pmu
->type
)
136 * We do not support sampling as the counters are all
137 * shared by all CPU cores in a CPU die(SCCL). Also we
138 * do not support attach to a task(per-process mode)
140 if (is_sampling_event(event
) || event
->attach_state
& PERF_ATTACH_TASK
)
144 * The uncore counters not specific to any CPU, so cannot
151 * Validate if the events in group does not exceed the
152 * available counters in hardware.
154 if (!hisi_validate_event_group(event
))
157 hisi_pmu
= to_hisi_pmu(event
->pmu
);
158 if (event
->attr
.config
> hisi_pmu
->check_event
)
161 if (hisi_pmu
->on_cpu
== -1)
164 * We don't assign an index until we actually place the event onto
165 * hardware. Use -1 to signify that we haven't decided where to put it
169 hwc
->config_base
= event
->attr
.config
;
171 /* Enforce to use the same CPU for all events in this PMU */
172 event
->cpu
= hisi_pmu
->on_cpu
;
178 * Set the counter to count the event that we're interested in,
179 * and enable interrupt and counter.
181 static void hisi_uncore_pmu_enable_event(struct perf_event
*event
)
183 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
184 struct hw_perf_event
*hwc
= &event
->hw
;
186 hisi_pmu
->ops
->write_evtype(hisi_pmu
, hwc
->idx
,
187 HISI_GET_EVENTID(event
));
189 hisi_pmu
->ops
->enable_counter_int(hisi_pmu
, hwc
);
190 hisi_pmu
->ops
->enable_counter(hisi_pmu
, hwc
);
194 * Disable counter and interrupt.
196 static void hisi_uncore_pmu_disable_event(struct perf_event
*event
)
198 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
199 struct hw_perf_event
*hwc
= &event
->hw
;
201 hisi_pmu
->ops
->disable_counter(hisi_pmu
, hwc
);
202 hisi_pmu
->ops
->disable_counter_int(hisi_pmu
, hwc
);
205 void hisi_uncore_pmu_set_event_period(struct perf_event
*event
)
207 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
208 struct hw_perf_event
*hwc
= &event
->hw
;
211 * The HiSilicon PMU counters support 32 bits or 48 bits, depending on
212 * the PMU. We reduce it to 2^(counter_bits - 1) to account for the
213 * extreme interrupt latency. So we could hopefully handle the overflow
214 * interrupt before another 2^(counter_bits - 1) events occur and the
215 * counter overtakes its previous value.
217 u64 val
= BIT_ULL(hisi_pmu
->counter_bits
- 1);
219 local64_set(&hwc
->prev_count
, val
);
220 /* Write start value to the hardware event counter */
221 hisi_pmu
->ops
->write_counter(hisi_pmu
, hwc
, val
);
224 void hisi_uncore_pmu_event_update(struct perf_event
*event
)
226 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
227 struct hw_perf_event
*hwc
= &event
->hw
;
228 u64 delta
, prev_raw_count
, new_raw_count
;
231 /* Read the count from the counter register */
232 new_raw_count
= hisi_pmu
->ops
->read_counter(hisi_pmu
, hwc
);
233 prev_raw_count
= local64_read(&hwc
->prev_count
);
234 } while (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
235 new_raw_count
) != prev_raw_count
);
239 delta
= (new_raw_count
- prev_raw_count
) &
240 HISI_MAX_PERIOD(hisi_pmu
->counter_bits
);
241 local64_add(delta
, &event
->count
);
244 void hisi_uncore_pmu_start(struct perf_event
*event
, int flags
)
246 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
247 struct hw_perf_event
*hwc
= &event
->hw
;
249 if (WARN_ON_ONCE(!(hwc
->state
& PERF_HES_STOPPED
)))
252 WARN_ON_ONCE(!(hwc
->state
& PERF_HES_UPTODATE
));
254 hisi_uncore_pmu_set_event_period(event
);
256 if (flags
& PERF_EF_RELOAD
) {
257 u64 prev_raw_count
= local64_read(&hwc
->prev_count
);
259 hisi_pmu
->ops
->write_counter(hisi_pmu
, hwc
, prev_raw_count
);
262 hisi_uncore_pmu_enable_event(event
);
263 perf_event_update_userpage(event
);
266 void hisi_uncore_pmu_stop(struct perf_event
*event
, int flags
)
268 struct hw_perf_event
*hwc
= &event
->hw
;
270 hisi_uncore_pmu_disable_event(event
);
271 WARN_ON_ONCE(hwc
->state
& PERF_HES_STOPPED
);
272 hwc
->state
|= PERF_HES_STOPPED
;
274 if (hwc
->state
& PERF_HES_UPTODATE
)
277 /* Read hardware counter and update the perf counter statistics */
278 hisi_uncore_pmu_event_update(event
);
279 hwc
->state
|= PERF_HES_UPTODATE
;
282 int hisi_uncore_pmu_add(struct perf_event
*event
, int flags
)
284 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
285 struct hw_perf_event
*hwc
= &event
->hw
;
288 hwc
->state
= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
290 /* Get an available counter index for counting */
291 idx
= hisi_pmu
->ops
->get_event_idx(event
);
296 hisi_pmu
->pmu_events
.hw_events
[idx
] = event
;
298 if (flags
& PERF_EF_START
)
299 hisi_uncore_pmu_start(event
, PERF_EF_RELOAD
);
304 void hisi_uncore_pmu_del(struct perf_event
*event
, int flags
)
306 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(event
->pmu
);
307 struct hw_perf_event
*hwc
= &event
->hw
;
309 hisi_uncore_pmu_stop(event
, PERF_EF_UPDATE
);
310 hisi_uncore_pmu_clear_event_idx(hisi_pmu
, hwc
->idx
);
311 perf_event_update_userpage(event
);
312 hisi_pmu
->pmu_events
.hw_events
[hwc
->idx
] = NULL
;
315 void hisi_uncore_pmu_read(struct perf_event
*event
)
317 /* Read hardware counter and update the perf counter statistics */
318 hisi_uncore_pmu_event_update(event
);
321 void hisi_uncore_pmu_enable(struct pmu
*pmu
)
323 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(pmu
);
324 int enabled
= bitmap_weight(hisi_pmu
->pmu_events
.used_mask
,
325 hisi_pmu
->num_counters
);
330 hisi_pmu
->ops
->start_counters(hisi_pmu
);
333 void hisi_uncore_pmu_disable(struct pmu
*pmu
)
335 struct hisi_pmu
*hisi_pmu
= to_hisi_pmu(pmu
);
337 hisi_pmu
->ops
->stop_counters(hisi_pmu
);
342 * The Super CPU Cluster (SCCL) and CPU Cluster (CCL) IDs can be
343 * determined from the MPIDR_EL1, but the encoding varies by CPU:
345 * - For MT variants of TSV110:
346 * SCCL is Aff2[7:3], CCL is Aff2[2:0]
348 * - For other MT parts:
349 * SCCL is Aff3[7:0], CCL is Aff2[7:0]
351 * - For non-MT parts:
352 * SCCL is Aff2[7:0], CCL is Aff1[7:0]
354 static void hisi_read_sccl_and_ccl_id(int *scclp
, int *cclp
)
356 u64 mpidr
= read_cpuid_mpidr();
357 int aff3
= MPIDR_AFFINITY_LEVEL(mpidr
, 3);
358 int aff2
= MPIDR_AFFINITY_LEVEL(mpidr
, 2);
359 int aff1
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
360 bool mt
= mpidr
& MPIDR_MT_BITMASK
;
363 if (mt
&& read_cpuid_part_number() == HISI_CPU_PART_TSV110
) {
381 * Check whether the CPU is associated with this uncore PMU
383 static bool hisi_pmu_cpu_is_associated_pmu(struct hisi_pmu
*hisi_pmu
)
387 if (hisi_pmu
->ccl_id
== -1) {
388 /* If CCL_ID is -1, the PMU only shares the same SCCL */
389 hisi_read_sccl_and_ccl_id(&sccl_id
, NULL
);
391 return sccl_id
== hisi_pmu
->sccl_id
;
394 hisi_read_sccl_and_ccl_id(&sccl_id
, &ccl_id
);
396 return sccl_id
== hisi_pmu
->sccl_id
&& ccl_id
== hisi_pmu
->ccl_id
;
399 int hisi_uncore_pmu_online_cpu(unsigned int cpu
, struct hlist_node
*node
)
401 struct hisi_pmu
*hisi_pmu
= hlist_entry_safe(node
, struct hisi_pmu
,
404 if (!hisi_pmu_cpu_is_associated_pmu(hisi_pmu
))
407 cpumask_set_cpu(cpu
, &hisi_pmu
->associated_cpus
);
409 /* If another CPU is already managing this PMU, simply return. */
410 if (hisi_pmu
->on_cpu
!= -1)
413 /* Use this CPU in cpumask for event counting */
414 hisi_pmu
->on_cpu
= cpu
;
416 /* Overflow interrupt also should use the same CPU */
417 WARN_ON(irq_set_affinity(hisi_pmu
->irq
, cpumask_of(cpu
)));
422 int hisi_uncore_pmu_offline_cpu(unsigned int cpu
, struct hlist_node
*node
)
424 struct hisi_pmu
*hisi_pmu
= hlist_entry_safe(node
, struct hisi_pmu
,
426 cpumask_t pmu_online_cpus
;
429 if (!cpumask_test_and_clear_cpu(cpu
, &hisi_pmu
->associated_cpus
))
432 /* Nothing to do if this CPU doesn't own the PMU */
433 if (hisi_pmu
->on_cpu
!= cpu
)
436 /* Give up ownership of the PMU */
437 hisi_pmu
->on_cpu
= -1;
439 /* Choose a new CPU to migrate ownership of the PMU to */
440 cpumask_and(&pmu_online_cpus
, &hisi_pmu
->associated_cpus
,
442 target
= cpumask_any_but(&pmu_online_cpus
, cpu
);
443 if (target
>= nr_cpu_ids
)
446 perf_pmu_migrate_context(&hisi_pmu
->pmu
, cpu
, target
);
447 /* Use this CPU for event counting */
448 hisi_pmu
->on_cpu
= target
;
449 WARN_ON(irq_set_affinity(hisi_pmu
->irq
, cpumask_of(target
)));