c9d8e2ec499ac8937a733739680553563fabe30e
[linux-2.6-microblaze.git] / drivers / perf / hisilicon / hisi_uncore_pmu.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HiSilicon SoC Hardware event counters support
4  *
5  * Copyright (C) 2017 Hisilicon Limited
6  * Author: Anurup M <anurup.m@huawei.com>
7  *         Shaokun Zhang <zhangshaokun@hisilicon.com>
8  *
9  * This code is based on the uncore PMUs like arm-cci and arm-ccn.
10  */
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>
17
18 #include <asm/cputype.h>
19 #include <asm/local64.h>
20
21 #include "hisi_uncore_pmu.h"
22
23 #define HISI_GET_EVENTID(ev) (ev->hw.config_base & 0xff)
24 #define HISI_MAX_PERIOD(nr) (BIT_ULL(nr) - 1)
25
26 /*
27  * PMU format attributes
28  */
29 ssize_t hisi_format_sysfs_show(struct device *dev,
30                                struct device_attribute *attr, char *buf)
31 {
32         struct dev_ext_attribute *eattr;
33
34         eattr = container_of(attr, struct dev_ext_attribute, attr);
35
36         return sysfs_emit(buf, "%s\n", (char *)eattr->var);
37 }
38 EXPORT_SYMBOL_GPL(hisi_format_sysfs_show);
39
40 /*
41  * PMU event attributes
42  */
43 ssize_t hisi_event_sysfs_show(struct device *dev,
44                               struct device_attribute *attr, char *page)
45 {
46         struct dev_ext_attribute *eattr;
47
48         eattr = container_of(attr, struct dev_ext_attribute, attr);
49
50         return sysfs_emit(page, "config=0x%lx\n", (unsigned long)eattr->var);
51 }
52 EXPORT_SYMBOL_GPL(hisi_event_sysfs_show);
53
54 /*
55  * sysfs cpumask attributes. For uncore PMU, we only have a single CPU to show
56  */
57 ssize_t hisi_cpumask_sysfs_show(struct device *dev,
58                                 struct device_attribute *attr, char *buf)
59 {
60         struct hisi_pmu *hisi_pmu = to_hisi_pmu(dev_get_drvdata(dev));
61
62         return sysfs_emit(buf, "%d\n", hisi_pmu->on_cpu);
63 }
64 EXPORT_SYMBOL_GPL(hisi_cpumask_sysfs_show);
65
66 static bool hisi_validate_event_group(struct perf_event *event)
67 {
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 */
71         int counters = 1;
72
73         if (!is_software_event(leader)) {
74                 /*
75                  * We must NOT create groups containing mixed PMUs, although
76                  * software events are acceptable
77                  */
78                 if (leader->pmu != event->pmu)
79                         return false;
80
81                 /* Increment counter for the leader */
82                 if (leader != event)
83                         counters++;
84         }
85
86         for_each_sibling_event(sibling, event->group_leader) {
87                 if (is_software_event(sibling))
88                         continue;
89                 if (sibling->pmu != event->pmu)
90                         return false;
91                 /* Increment counter for each sibling */
92                 counters++;
93         }
94
95         /* The group can not count events more than the counters in the HW */
96         return counters <= hisi_pmu->num_counters;
97 }
98
99 int hisi_uncore_pmu_get_event_idx(struct perf_event *event)
100 {
101         struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
102         unsigned long *used_mask = hisi_pmu->pmu_events.used_mask;
103         u32 num_counters = hisi_pmu->num_counters;
104         int idx;
105
106         idx = find_first_zero_bit(used_mask, num_counters);
107         if (idx == num_counters)
108                 return -EAGAIN;
109
110         set_bit(idx, used_mask);
111
112         return idx;
113 }
114 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_get_event_idx);
115
116 ssize_t hisi_uncore_pmu_identifier_attr_show(struct device *dev,
117                                              struct device_attribute *attr,
118                                              char *page)
119 {
120         struct hisi_pmu *hisi_pmu = to_hisi_pmu(dev_get_drvdata(dev));
121
122         return sysfs_emit(page, "0x%08x\n", hisi_pmu->identifier);
123 }
124 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_identifier_attr_show);
125
126 static void hisi_uncore_pmu_clear_event_idx(struct hisi_pmu *hisi_pmu, int idx)
127 {
128         clear_bit(idx, hisi_pmu->pmu_events.used_mask);
129 }
130
131 static irqreturn_t hisi_uncore_pmu_isr(int irq, void *data)
132 {
133         struct hisi_pmu *hisi_pmu = data;
134         struct perf_event *event;
135         unsigned long overflown;
136         int idx;
137
138         overflown = hisi_pmu->ops->get_int_status(hisi_pmu);
139         if (!overflown)
140                 return IRQ_NONE;
141
142         /*
143          * Find the counter index which overflowed if the bit was set
144          * and handle it.
145          */
146         for_each_set_bit(idx, &overflown, hisi_pmu->num_counters) {
147                 /* Write 1 to clear the IRQ status flag */
148                 hisi_pmu->ops->clear_int_status(hisi_pmu, idx);
149                 /* Get the corresponding event struct */
150                 event = hisi_pmu->pmu_events.hw_events[idx];
151                 if (!event)
152                         continue;
153
154                 hisi_uncore_pmu_event_update(event);
155                 hisi_uncore_pmu_set_event_period(event);
156         }
157
158         return IRQ_HANDLED;
159 }
160
161 int hisi_uncore_pmu_init_irq(struct hisi_pmu *hisi_pmu,
162                              struct platform_device *pdev)
163 {
164         int irq, ret;
165
166         irq = platform_get_irq(pdev, 0);
167         if (irq < 0)
168                 return irq;
169
170         ret = devm_request_irq(&pdev->dev, irq, hisi_uncore_pmu_isr,
171                                IRQF_NOBALANCING | IRQF_NO_THREAD,
172                                dev_name(&pdev->dev), hisi_pmu);
173         if (ret < 0) {
174                 dev_err(&pdev->dev,
175                         "Fail to request IRQ: %d ret: %d.\n", irq, ret);
176                 return ret;
177         }
178
179         hisi_pmu->irq = irq;
180
181         return 0;
182 }
183 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_init_irq);
184
185 int hisi_uncore_pmu_event_init(struct perf_event *event)
186 {
187         struct hw_perf_event *hwc = &event->hw;
188         struct hisi_pmu *hisi_pmu;
189
190         if (event->attr.type != event->pmu->type)
191                 return -ENOENT;
192
193         /*
194          * We do not support sampling as the counters are all
195          * shared by all CPU cores in a CPU die(SCCL). Also we
196          * do not support attach to a task(per-process mode)
197          */
198         if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
199                 return -EOPNOTSUPP;
200
201         /*
202          *  The uncore counters not specific to any CPU, so cannot
203          *  support per-task
204          */
205         if (event->cpu < 0)
206                 return -EINVAL;
207
208         /*
209          * Validate if the events in group does not exceed the
210          * available counters in hardware.
211          */
212         if (!hisi_validate_event_group(event))
213                 return -EINVAL;
214
215         hisi_pmu = to_hisi_pmu(event->pmu);
216         if (event->attr.config > hisi_pmu->check_event)
217                 return -EINVAL;
218
219         if (hisi_pmu->on_cpu == -1)
220                 return -EINVAL;
221         /*
222          * We don't assign an index until we actually place the event onto
223          * hardware. Use -1 to signify that we haven't decided where to put it
224          * yet.
225          */
226         hwc->idx                = -1;
227         hwc->config_base        = event->attr.config;
228
229         /* Enforce to use the same CPU for all events in this PMU */
230         event->cpu = hisi_pmu->on_cpu;
231
232         return 0;
233 }
234 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_event_init);
235
236 /*
237  * Set the counter to count the event that we're interested in,
238  * and enable interrupt and counter.
239  */
240 static void hisi_uncore_pmu_enable_event(struct perf_event *event)
241 {
242         struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
243         struct hw_perf_event *hwc = &event->hw;
244
245         hisi_pmu->ops->write_evtype(hisi_pmu, hwc->idx,
246                                     HISI_GET_EVENTID(event));
247
248         hisi_pmu->ops->enable_counter_int(hisi_pmu, hwc);
249         hisi_pmu->ops->enable_counter(hisi_pmu, hwc);
250 }
251
252 /*
253  * Disable counter and interrupt.
254  */
255 static void hisi_uncore_pmu_disable_event(struct perf_event *event)
256 {
257         struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
258         struct hw_perf_event *hwc = &event->hw;
259
260         hisi_pmu->ops->disable_counter(hisi_pmu, hwc);
261         hisi_pmu->ops->disable_counter_int(hisi_pmu, hwc);
262 }
263
264 void hisi_uncore_pmu_set_event_period(struct perf_event *event)
265 {
266         struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
267         struct hw_perf_event *hwc = &event->hw;
268
269         /*
270          * The HiSilicon PMU counters support 32 bits or 48 bits, depending on
271          * the PMU. We reduce it to 2^(counter_bits - 1) to account for the
272          * extreme interrupt latency. So we could hopefully handle the overflow
273          * interrupt before another 2^(counter_bits - 1) events occur and the
274          * counter overtakes its previous value.
275          */
276         u64 val = BIT_ULL(hisi_pmu->counter_bits - 1);
277
278         local64_set(&hwc->prev_count, val);
279         /* Write start value to the hardware event counter */
280         hisi_pmu->ops->write_counter(hisi_pmu, hwc, val);
281 }
282 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_set_event_period);
283
284 void hisi_uncore_pmu_event_update(struct perf_event *event)
285 {
286         struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
287         struct hw_perf_event *hwc = &event->hw;
288         u64 delta, prev_raw_count, new_raw_count;
289
290         do {
291                 /* Read the count from the counter register */
292                 new_raw_count = hisi_pmu->ops->read_counter(hisi_pmu, hwc);
293                 prev_raw_count = local64_read(&hwc->prev_count);
294         } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
295                                  new_raw_count) != prev_raw_count);
296         /*
297          * compute the delta
298          */
299         delta = (new_raw_count - prev_raw_count) &
300                 HISI_MAX_PERIOD(hisi_pmu->counter_bits);
301         local64_add(delta, &event->count);
302 }
303 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_event_update);
304
305 void hisi_uncore_pmu_start(struct perf_event *event, int flags)
306 {
307         struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
308         struct hw_perf_event *hwc = &event->hw;
309
310         if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
311                 return;
312
313         WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
314         hwc->state = 0;
315         hisi_uncore_pmu_set_event_period(event);
316
317         if (flags & PERF_EF_RELOAD) {
318                 u64 prev_raw_count =  local64_read(&hwc->prev_count);
319
320                 hisi_pmu->ops->write_counter(hisi_pmu, hwc, prev_raw_count);
321         }
322
323         hisi_uncore_pmu_enable_event(event);
324         perf_event_update_userpage(event);
325 }
326 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_start);
327
328 void hisi_uncore_pmu_stop(struct perf_event *event, int flags)
329 {
330         struct hw_perf_event *hwc = &event->hw;
331
332         hisi_uncore_pmu_disable_event(event);
333         WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
334         hwc->state |= PERF_HES_STOPPED;
335
336         if (hwc->state & PERF_HES_UPTODATE)
337                 return;
338
339         /* Read hardware counter and update the perf counter statistics */
340         hisi_uncore_pmu_event_update(event);
341         hwc->state |= PERF_HES_UPTODATE;
342 }
343 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_stop);
344
345 int hisi_uncore_pmu_add(struct perf_event *event, int flags)
346 {
347         struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
348         struct hw_perf_event *hwc = &event->hw;
349         int idx;
350
351         hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
352
353         /* Get an available counter index for counting */
354         idx = hisi_pmu->ops->get_event_idx(event);
355         if (idx < 0)
356                 return idx;
357
358         event->hw.idx = idx;
359         hisi_pmu->pmu_events.hw_events[idx] = event;
360
361         if (flags & PERF_EF_START)
362                 hisi_uncore_pmu_start(event, PERF_EF_RELOAD);
363
364         return 0;
365 }
366 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_add);
367
368 void hisi_uncore_pmu_del(struct perf_event *event, int flags)
369 {
370         struct hisi_pmu *hisi_pmu = to_hisi_pmu(event->pmu);
371         struct hw_perf_event *hwc = &event->hw;
372
373         hisi_uncore_pmu_stop(event, PERF_EF_UPDATE);
374         hisi_uncore_pmu_clear_event_idx(hisi_pmu, hwc->idx);
375         perf_event_update_userpage(event);
376         hisi_pmu->pmu_events.hw_events[hwc->idx] = NULL;
377 }
378 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_del);
379
380 void hisi_uncore_pmu_read(struct perf_event *event)
381 {
382         /* Read hardware counter and update the perf counter statistics */
383         hisi_uncore_pmu_event_update(event);
384 }
385 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_read);
386
387 void hisi_uncore_pmu_enable(struct pmu *pmu)
388 {
389         struct hisi_pmu *hisi_pmu = to_hisi_pmu(pmu);
390         int enabled = bitmap_weight(hisi_pmu->pmu_events.used_mask,
391                                     hisi_pmu->num_counters);
392
393         if (!enabled)
394                 return;
395
396         hisi_pmu->ops->start_counters(hisi_pmu);
397 }
398 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_enable);
399
400 void hisi_uncore_pmu_disable(struct pmu *pmu)
401 {
402         struct hisi_pmu *hisi_pmu = to_hisi_pmu(pmu);
403
404         hisi_pmu->ops->stop_counters(hisi_pmu);
405 }
406 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_disable);
407
408
409 /*
410  * The Super CPU Cluster (SCCL) and CPU Cluster (CCL) IDs can be
411  * determined from the MPIDR_EL1, but the encoding varies by CPU:
412  *
413  * - For MT variants of TSV110:
414  *   SCCL is Aff2[7:3], CCL is Aff2[2:0]
415  *
416  * - For other MT parts:
417  *   SCCL is Aff3[7:0], CCL is Aff2[7:0]
418  *
419  * - For non-MT parts:
420  *   SCCL is Aff2[7:0], CCL is Aff1[7:0]
421  */
422 static void hisi_read_sccl_and_ccl_id(int *scclp, int *cclp)
423 {
424         u64 mpidr = read_cpuid_mpidr();
425         int aff3 = MPIDR_AFFINITY_LEVEL(mpidr, 3);
426         int aff2 = MPIDR_AFFINITY_LEVEL(mpidr, 2);
427         int aff1 = MPIDR_AFFINITY_LEVEL(mpidr, 1);
428         bool mt = mpidr & MPIDR_MT_BITMASK;
429         int sccl, ccl;
430
431         if (mt && read_cpuid_part_number() == HISI_CPU_PART_TSV110) {
432                 sccl = aff2 >> 3;
433                 ccl = aff2 & 0x7;
434         } else if (mt) {
435                 sccl = aff3;
436                 ccl = aff2;
437         } else {
438                 sccl = aff2;
439                 ccl = aff1;
440         }
441
442         if (scclp)
443                 *scclp = sccl;
444         if (cclp)
445                 *cclp = ccl;
446 }
447
448 /*
449  * Check whether the CPU is associated with this uncore PMU
450  */
451 static bool hisi_pmu_cpu_is_associated_pmu(struct hisi_pmu *hisi_pmu)
452 {
453         int sccl_id, ccl_id;
454
455         if (hisi_pmu->ccl_id == -1) {
456                 /* If CCL_ID is -1, the PMU only shares the same SCCL */
457                 hisi_read_sccl_and_ccl_id(&sccl_id, NULL);
458
459                 return sccl_id == hisi_pmu->sccl_id;
460         }
461
462         hisi_read_sccl_and_ccl_id(&sccl_id, &ccl_id);
463
464         return sccl_id == hisi_pmu->sccl_id && ccl_id == hisi_pmu->ccl_id;
465 }
466
467 int hisi_uncore_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
468 {
469         struct hisi_pmu *hisi_pmu = hlist_entry_safe(node, struct hisi_pmu,
470                                                      node);
471
472         if (!hisi_pmu_cpu_is_associated_pmu(hisi_pmu))
473                 return 0;
474
475         cpumask_set_cpu(cpu, &hisi_pmu->associated_cpus);
476
477         /* If another CPU is already managing this PMU, simply return. */
478         if (hisi_pmu->on_cpu != -1)
479                 return 0;
480
481         /* Use this CPU in cpumask for event counting */
482         hisi_pmu->on_cpu = cpu;
483
484         /* Overflow interrupt also should use the same CPU */
485         WARN_ON(irq_set_affinity_hint(hisi_pmu->irq, cpumask_of(cpu)));
486
487         return 0;
488 }
489 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_online_cpu);
490
491 int hisi_uncore_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
492 {
493         struct hisi_pmu *hisi_pmu = hlist_entry_safe(node, struct hisi_pmu,
494                                                      node);
495         cpumask_t pmu_online_cpus;
496         unsigned int target;
497
498         if (!cpumask_test_and_clear_cpu(cpu, &hisi_pmu->associated_cpus))
499                 return 0;
500
501         /* Nothing to do if this CPU doesn't own the PMU */
502         if (hisi_pmu->on_cpu != cpu)
503                 return 0;
504
505         /* Give up ownership of the PMU */
506         hisi_pmu->on_cpu = -1;
507
508         /* Choose a new CPU to migrate ownership of the PMU to */
509         cpumask_and(&pmu_online_cpus, &hisi_pmu->associated_cpus,
510                     cpu_online_mask);
511         target = cpumask_any_but(&pmu_online_cpus, cpu);
512         if (target >= nr_cpu_ids)
513                 return 0;
514
515         perf_pmu_migrate_context(&hisi_pmu->pmu, cpu, target);
516         /* Use this CPU for event counting */
517         hisi_pmu->on_cpu = target;
518         WARN_ON(irq_set_affinity_hint(hisi_pmu->irq, cpumask_of(target)));
519
520         return 0;
521 }
522 EXPORT_SYMBOL_GPL(hisi_uncore_pmu_offline_cpu);
523
524 MODULE_LICENSE("GPL v2");