Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2#undef DEBUG
3
4/*
5 * ARM performance counter support.
6 *
7 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
8 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
9 *
10 * This code is based on the sparc64 perf event code, which is in turn based
11 * on the x86 code.
12 */
13#define pr_fmt(fmt) "hw perfevents: " fmt
14
15#include <linux/bitmap.h>
16#include <linux/cpumask.h>
17#include <linux/cpu_pm.h>
18#include <linux/export.h>
19#include <linux/kernel.h>
20#include <linux/perf/arm_pmu.h>
21#include <linux/slab.h>
22#include <linux/sched/clock.h>
23#include <linux/spinlock.h>
24#include <linux/irq.h>
25#include <linux/irqdesc.h>
26
27#include <asm/irq_regs.h>
28
29static int armpmu_count_irq_users(const int irq);
30
31struct pmu_irq_ops {
32 void (*enable_pmuirq)(unsigned int irq);
33 void (*disable_pmuirq)(unsigned int irq);
34 void (*free_pmuirq)(unsigned int irq, int cpu, void __percpu *devid);
35};
36
37static void armpmu_free_pmuirq(unsigned int irq, int cpu, void __percpu *devid)
38{
39 free_irq(irq, per_cpu_ptr(devid, cpu));
40}
41
42static const struct pmu_irq_ops pmuirq_ops = {
43 .enable_pmuirq = enable_irq,
44 .disable_pmuirq = disable_irq_nosync,
45 .free_pmuirq = armpmu_free_pmuirq
46};
47
48static void armpmu_free_pmunmi(unsigned int irq, int cpu, void __percpu *devid)
49{
50 free_nmi(irq, per_cpu_ptr(devid, cpu));
51}
52
53static const struct pmu_irq_ops pmunmi_ops = {
54 .enable_pmuirq = enable_nmi,
55 .disable_pmuirq = disable_nmi_nosync,
56 .free_pmuirq = armpmu_free_pmunmi
57};
58
59static void armpmu_enable_percpu_pmuirq(unsigned int irq)
60{
61 enable_percpu_irq(irq, IRQ_TYPE_NONE);
62}
63
64static void armpmu_free_percpu_pmuirq(unsigned int irq, int cpu,
65 void __percpu *devid)
66{
67 if (armpmu_count_irq_users(irq) == 1)
68 free_percpu_irq(irq, devid);
69}
70
71static const struct pmu_irq_ops percpu_pmuirq_ops = {
72 .enable_pmuirq = armpmu_enable_percpu_pmuirq,
73 .disable_pmuirq = disable_percpu_irq,
74 .free_pmuirq = armpmu_free_percpu_pmuirq
75};
76
77static void armpmu_enable_percpu_pmunmi(unsigned int irq)
78{
79 if (!prepare_percpu_nmi(irq))
80 enable_percpu_nmi(irq, IRQ_TYPE_NONE);
81}
82
83static void armpmu_disable_percpu_pmunmi(unsigned int irq)
84{
85 disable_percpu_nmi(irq);
86 teardown_percpu_nmi(irq);
87}
88
89static void armpmu_free_percpu_pmunmi(unsigned int irq, int cpu,
90 void __percpu *devid)
91{
92 if (armpmu_count_irq_users(irq) == 1)
93 free_percpu_nmi(irq, devid);
94}
95
96static const struct pmu_irq_ops percpu_pmunmi_ops = {
97 .enable_pmuirq = armpmu_enable_percpu_pmunmi,
98 .disable_pmuirq = armpmu_disable_percpu_pmunmi,
99 .free_pmuirq = armpmu_free_percpu_pmunmi
100};
101
102DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
103static DEFINE_PER_CPU(int, cpu_irq);
104static DEFINE_PER_CPU(const struct pmu_irq_ops *, cpu_irq_ops);
105
106static bool has_nmi;
107
108static inline u64 arm_pmu_event_max_period(struct perf_event *event)
109{
110 if (event->hw.flags & ARMPMU_EVT_64BIT)
111 return GENMASK_ULL(63, 0);
112 else if (event->hw.flags & ARMPMU_EVT_63BIT)
113 return GENMASK_ULL(62, 0);
114 else if (event->hw.flags & ARMPMU_EVT_47BIT)
115 return GENMASK_ULL(46, 0);
116 else
117 return GENMASK_ULL(31, 0);
118}
119
120static int
121armpmu_map_cache_event(const unsigned (*cache_map)
122 [PERF_COUNT_HW_CACHE_MAX]
123 [PERF_COUNT_HW_CACHE_OP_MAX]
124 [PERF_COUNT_HW_CACHE_RESULT_MAX],
125 u64 config)
126{
127 unsigned int cache_type, cache_op, cache_result, ret;
128
129 cache_type = (config >> 0) & 0xff;
130 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
131 return -EINVAL;
132
133 cache_op = (config >> 8) & 0xff;
134 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
135 return -EINVAL;
136
137 cache_result = (config >> 16) & 0xff;
138 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
139 return -EINVAL;
140
141 if (!cache_map)
142 return -ENOENT;
143
144 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
145
146 if (ret == CACHE_OP_UNSUPPORTED)
147 return -ENOENT;
148
149 return ret;
150}
151
152static int
153armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
154{
155 int mapping;
156
157 if (config >= PERF_COUNT_HW_MAX)
158 return -EINVAL;
159
160 if (!event_map)
161 return -ENOENT;
162
163 mapping = (*event_map)[config];
164 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
165}
166
167static int
168armpmu_map_raw_event(u32 raw_event_mask, u64 config)
169{
170 return (int)(config & raw_event_mask);
171}
172
173int
174armpmu_map_event(struct perf_event *event,
175 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
176 const unsigned (*cache_map)
177 [PERF_COUNT_HW_CACHE_MAX]
178 [PERF_COUNT_HW_CACHE_OP_MAX]
179 [PERF_COUNT_HW_CACHE_RESULT_MAX],
180 u32 raw_event_mask)
181{
182 u64 config = event->attr.config;
183 int type = event->attr.type;
184
185 if (type == event->pmu->type)
186 return armpmu_map_raw_event(raw_event_mask, config);
187
188 switch (type) {
189 case PERF_TYPE_HARDWARE:
190 return armpmu_map_hw_event(event_map, config);
191 case PERF_TYPE_HW_CACHE:
192 return armpmu_map_cache_event(cache_map, config);
193 case PERF_TYPE_RAW:
194 return armpmu_map_raw_event(raw_event_mask, config);
195 }
196
197 return -ENOENT;
198}
199
200int armpmu_event_set_period(struct perf_event *event)
201{
202 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
203 struct hw_perf_event *hwc = &event->hw;
204 s64 left = local64_read(&hwc->period_left);
205 s64 period = hwc->sample_period;
206 u64 max_period;
207 int ret = 0;
208
209 max_period = arm_pmu_event_max_period(event);
210 if (unlikely(left <= -period)) {
211 left = period;
212 local64_set(&hwc->period_left, left);
213 hwc->last_period = period;
214 ret = 1;
215 }
216
217 if (unlikely(left <= 0)) {
218 left += period;
219 local64_set(&hwc->period_left, left);
220 hwc->last_period = period;
221 ret = 1;
222 }
223
224 /*
225 * Limit the maximum period to prevent the counter value
226 * from overtaking the one we are about to program. In
227 * effect we are reducing max_period to account for
228 * interrupt latency (and we are being very conservative).
229 */
230 if (left > (max_period >> 1))
231 left = (max_period >> 1);
232
233 local64_set(&hwc->prev_count, (u64)-left);
234
235 armpmu->write_counter(event, (u64)(-left) & max_period);
236
237 perf_event_update_userpage(event);
238
239 return ret;
240}
241
242u64 armpmu_event_update(struct perf_event *event)
243{
244 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
245 struct hw_perf_event *hwc = &event->hw;
246 u64 delta, prev_raw_count, new_raw_count;
247 u64 max_period = arm_pmu_event_max_period(event);
248
249again:
250 prev_raw_count = local64_read(&hwc->prev_count);
251 new_raw_count = armpmu->read_counter(event);
252
253 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
254 new_raw_count) != prev_raw_count)
255 goto again;
256
257 delta = (new_raw_count - prev_raw_count) & max_period;
258
259 local64_add(delta, &event->count);
260 local64_sub(delta, &hwc->period_left);
261
262 return new_raw_count;
263}
264
265static void
266armpmu_read(struct perf_event *event)
267{
268 armpmu_event_update(event);
269}
270
271static void
272armpmu_stop(struct perf_event *event, int flags)
273{
274 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
275 struct hw_perf_event *hwc = &event->hw;
276
277 /*
278 * ARM pmu always has to update the counter, so ignore
279 * PERF_EF_UPDATE, see comments in armpmu_start().
280 */
281 if (!(hwc->state & PERF_HES_STOPPED)) {
282 armpmu->disable(event);
283 armpmu_event_update(event);
284 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
285 }
286}
287
288static void armpmu_start(struct perf_event *event, int flags)
289{
290 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
291 struct hw_perf_event *hwc = &event->hw;
292
293 /*
294 * ARM pmu always has to reprogram the period, so ignore
295 * PERF_EF_RELOAD, see the comment below.
296 */
297 if (flags & PERF_EF_RELOAD)
298 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
299
300 hwc->state = 0;
301 /*
302 * Set the period again. Some counters can't be stopped, so when we
303 * were stopped we simply disabled the IRQ source and the counter
304 * may have been left counting. If we don't do this step then we may
305 * get an interrupt too soon or *way* too late if the overflow has
306 * happened since disabling.
307 */
308 armpmu_event_set_period(event);
309 armpmu->enable(event);
310}
311
312static void
313armpmu_del(struct perf_event *event, int flags)
314{
315 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
316 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
317 struct hw_perf_event *hwc = &event->hw;
318 int idx = hwc->idx;
319
320 armpmu_stop(event, PERF_EF_UPDATE);
321
322 if (has_branch_stack(event)) {
323 hw_events->branch_users--;
324 perf_sched_cb_dec(event->pmu);
325 }
326
327 hw_events->events[idx] = NULL;
328 armpmu->clear_event_idx(hw_events, event);
329 perf_event_update_userpage(event);
330 /* Clear the allocated counter */
331 hwc->idx = -1;
332}
333
334static int
335armpmu_add(struct perf_event *event, int flags)
336{
337 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
338 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
339 struct hw_perf_event *hwc = &event->hw;
340 int idx;
341
342 /* An event following a process won't be stopped earlier */
343 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
344 return -ENOENT;
345
346 /* If we don't have a space for the counter then finish early. */
347 idx = armpmu->get_event_idx(hw_events, event);
348 if (idx < 0)
349 return idx;
350
351 /* The newly-allocated counter should be empty */
352 WARN_ON_ONCE(hw_events->events[idx]);
353
354 if (has_branch_stack(event)) {
355 hw_events->branch_users++;
356 perf_sched_cb_inc(event->pmu);
357 }
358
359 event->hw.idx = idx;
360 hw_events->events[idx] = event;
361
362 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
363 if (flags & PERF_EF_START)
364 armpmu_start(event, PERF_EF_RELOAD);
365
366 /* Propagate our changes to the userspace mapping. */
367 perf_event_update_userpage(event);
368
369 return 0;
370}
371
372static int
373validate_event(struct pmu *pmu, struct pmu_hw_events *hw_events,
374 struct perf_event *event)
375{
376 struct arm_pmu *armpmu;
377
378 if (is_software_event(event))
379 return 1;
380
381 /*
382 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
383 * core perf code won't check that the pmu->ctx == leader->ctx
384 * until after pmu->event_init(event).
385 */
386 if (event->pmu != pmu)
387 return 0;
388
389 if (event->state < PERF_EVENT_STATE_OFF)
390 return 1;
391
392 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
393 return 1;
394
395 armpmu = to_arm_pmu(event->pmu);
396 return armpmu->get_event_idx(hw_events, event) >= 0;
397}
398
399static int
400validate_group(struct perf_event *event)
401{
402 struct perf_event *sibling, *leader = event->group_leader;
403 struct pmu_hw_events fake_pmu;
404
405 /*
406 * Initialise the fake PMU. We only need to populate the
407 * used_mask for the purposes of validation.
408 */
409 memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
410
411 if (!validate_event(event->pmu, &fake_pmu, leader))
412 return -EINVAL;
413
414 if (event == leader)
415 return 0;
416
417 for_each_sibling_event(sibling, leader) {
418 if (!validate_event(event->pmu, &fake_pmu, sibling))
419 return -EINVAL;
420 }
421
422 if (!validate_event(event->pmu, &fake_pmu, event))
423 return -EINVAL;
424
425 return 0;
426}
427
428static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
429{
430 struct arm_pmu *armpmu;
431 int ret;
432 u64 start_clock, finish_clock;
433
434 /*
435 * we request the IRQ with a (possibly percpu) struct arm_pmu**, but
436 * the handlers expect a struct arm_pmu*. The percpu_irq framework will
437 * do any necessary shifting, we just need to perform the first
438 * dereference.
439 */
440 armpmu = *(void **)dev;
441 if (WARN_ON_ONCE(!armpmu))
442 return IRQ_NONE;
443
444 start_clock = sched_clock();
445 ret = armpmu->handle_irq(armpmu);
446 finish_clock = sched_clock();
447
448 perf_sample_event_took(finish_clock - start_clock);
449 return ret;
450}
451
452static int
453__hw_perf_event_init(struct perf_event *event)
454{
455 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
456 struct hw_perf_event *hwc = &event->hw;
457 int mapping, ret;
458
459 hwc->flags = 0;
460 mapping = armpmu->map_event(event);
461
462 if (mapping < 0) {
463 pr_debug("event %x:%llx not supported\n", event->attr.type,
464 event->attr.config);
465 return mapping;
466 }
467
468 /*
469 * We don't assign an index until we actually place the event onto
470 * hardware. Use -1 to signify that we haven't decided where to put it
471 * yet. For SMP systems, each core has it's own PMU so we can't do any
472 * clever allocation or constraints checking at this point.
473 */
474 hwc->idx = -1;
475 hwc->config_base = 0;
476 hwc->config = 0;
477 hwc->event_base = 0;
478
479 /*
480 * Check whether we need to exclude the counter from certain modes.
481 */
482 if (armpmu->set_event_filter) {
483 ret = armpmu->set_event_filter(hwc, &event->attr);
484 if (ret)
485 return ret;
486 }
487
488 /*
489 * Store the event encoding into the config_base field.
490 */
491 hwc->config_base |= (unsigned long)mapping;
492
493 if (!is_sampling_event(event)) {
494 /*
495 * For non-sampling runs, limit the sample_period to half
496 * of the counter width. That way, the new counter value
497 * is far less likely to overtake the previous one unless
498 * you have some serious IRQ latency issues.
499 */
500 hwc->sample_period = arm_pmu_event_max_period(event) >> 1;
501 hwc->last_period = hwc->sample_period;
502 local64_set(&hwc->period_left, hwc->sample_period);
503 }
504
505 return validate_group(event);
506}
507
508static int armpmu_event_init(struct perf_event *event)
509{
510 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
511
512 /*
513 * Reject CPU-affine events for CPUs that are of a different class to
514 * that which this PMU handles. Process-following events (where
515 * event->cpu == -1) can be migrated between CPUs, and thus we have to
516 * reject them later (in armpmu_add) if they're scheduled on a
517 * different class of CPU.
518 */
519 if (event->cpu != -1 &&
520 !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
521 return -ENOENT;
522
523 if (has_branch_stack(event) && !armpmu->reg_brbidr)
524 return -EOPNOTSUPP;
525
526 return __hw_perf_event_init(event);
527}
528
529static void armpmu_enable(struct pmu *pmu)
530{
531 struct arm_pmu *armpmu = to_arm_pmu(pmu);
532 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
533 bool enabled = !bitmap_empty(hw_events->used_mask, ARMPMU_MAX_HWEVENTS);
534
535 /* For task-bound events we may be called on other CPUs */
536 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
537 return;
538
539 if (enabled)
540 armpmu->start(armpmu);
541}
542
543static void armpmu_disable(struct pmu *pmu)
544{
545 struct arm_pmu *armpmu = to_arm_pmu(pmu);
546
547 /* For task-bound events we may be called on other CPUs */
548 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
549 return;
550
551 armpmu->stop(armpmu);
552}
553
554/*
555 * In heterogeneous systems, events are specific to a particular
556 * microarchitecture, and aren't suitable for another. Thus, only match CPUs of
557 * the same microarchitecture.
558 */
559static bool armpmu_filter(struct pmu *pmu, int cpu)
560{
561 struct arm_pmu *armpmu = to_arm_pmu(pmu);
562 return !cpumask_test_cpu(cpu, &armpmu->supported_cpus);
563}
564
565static ssize_t cpus_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
568 struct arm_pmu *armpmu = to_arm_pmu(dev_get_drvdata(dev));
569 return cpumap_print_to_pagebuf(true, buf, &armpmu->supported_cpus);
570}
571
572static DEVICE_ATTR_RO(cpus);
573
574static struct attribute *armpmu_common_attrs[] = {
575 &dev_attr_cpus.attr,
576 NULL,
577};
578
579static const struct attribute_group armpmu_common_attr_group = {
580 .attrs = armpmu_common_attrs,
581};
582
583static int armpmu_count_irq_users(const int irq)
584{
585 int cpu, count = 0;
586
587 for_each_possible_cpu(cpu) {
588 if (per_cpu(cpu_irq, cpu) == irq)
589 count++;
590 }
591
592 return count;
593}
594
595static const struct pmu_irq_ops *armpmu_find_irq_ops(int irq)
596{
597 const struct pmu_irq_ops *ops = NULL;
598 int cpu;
599
600 for_each_possible_cpu(cpu) {
601 if (per_cpu(cpu_irq, cpu) != irq)
602 continue;
603
604 ops = per_cpu(cpu_irq_ops, cpu);
605 if (ops)
606 break;
607 }
608
609 return ops;
610}
611
612void armpmu_free_irq(int irq, int cpu)
613{
614 if (per_cpu(cpu_irq, cpu) == 0)
615 return;
616 if (WARN_ON(irq != per_cpu(cpu_irq, cpu)))
617 return;
618
619 per_cpu(cpu_irq_ops, cpu)->free_pmuirq(irq, cpu, &cpu_armpmu);
620
621 per_cpu(cpu_irq, cpu) = 0;
622 per_cpu(cpu_irq_ops, cpu) = NULL;
623}
624
625int armpmu_request_irq(int irq, int cpu)
626{
627 int err = 0;
628 const irq_handler_t handler = armpmu_dispatch_irq;
629 const struct pmu_irq_ops *irq_ops;
630
631 if (!irq)
632 return 0;
633
634 if (!irq_is_percpu_devid(irq)) {
635 unsigned long irq_flags;
636
637 err = irq_force_affinity(irq, cpumask_of(cpu));
638
639 if (err && num_possible_cpus() > 1) {
640 pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
641 irq, cpu);
642 goto err_out;
643 }
644
645 irq_flags = IRQF_PERCPU |
646 IRQF_NOBALANCING | IRQF_NO_AUTOEN |
647 IRQF_NO_THREAD;
648
649 err = request_nmi(irq, handler, irq_flags, "arm-pmu",
650 per_cpu_ptr(&cpu_armpmu, cpu));
651
652 /* If cannot get an NMI, get a normal interrupt */
653 if (err) {
654 err = request_irq(irq, handler, irq_flags, "arm-pmu",
655 per_cpu_ptr(&cpu_armpmu, cpu));
656 irq_ops = &pmuirq_ops;
657 } else {
658 has_nmi = true;
659 irq_ops = &pmunmi_ops;
660 }
661 } else if (armpmu_count_irq_users(irq) == 0) {
662 err = request_percpu_nmi(irq, handler, "arm-pmu", &cpu_armpmu);
663
664 /* If cannot get an NMI, get a normal interrupt */
665 if (err) {
666 err = request_percpu_irq(irq, handler, "arm-pmu",
667 &cpu_armpmu);
668 irq_ops = &percpu_pmuirq_ops;
669 } else {
670 has_nmi = true;
671 irq_ops = &percpu_pmunmi_ops;
672 }
673 } else {
674 /* Per cpudevid irq was already requested by another CPU */
675 irq_ops = armpmu_find_irq_ops(irq);
676
677 if (WARN_ON(!irq_ops))
678 err = -EINVAL;
679 }
680
681 if (err)
682 goto err_out;
683
684 per_cpu(cpu_irq, cpu) = irq;
685 per_cpu(cpu_irq_ops, cpu) = irq_ops;
686 return 0;
687
688err_out:
689 pr_err("unable to request IRQ%d for ARM PMU counters\n", irq);
690 return err;
691}
692
693static int armpmu_get_cpu_irq(struct arm_pmu *pmu, int cpu)
694{
695 struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
696 return per_cpu(hw_events->irq, cpu);
697}
698
699bool arm_pmu_irq_is_nmi(void)
700{
701 return has_nmi;
702}
703
704/*
705 * PMU hardware loses all context when a CPU goes offline.
706 * When a CPU is hotplugged back in, since some hardware registers are
707 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
708 * junk values out of them.
709 */
710static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
711{
712 struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
713 int irq;
714
715 if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
716 return 0;
717 if (pmu->reset)
718 pmu->reset(pmu);
719
720 per_cpu(cpu_armpmu, cpu) = pmu;
721
722 irq = armpmu_get_cpu_irq(pmu, cpu);
723 if (irq)
724 per_cpu(cpu_irq_ops, cpu)->enable_pmuirq(irq);
725
726 return 0;
727}
728
729static int arm_perf_teardown_cpu(unsigned int cpu, struct hlist_node *node)
730{
731 struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
732 int irq;
733
734 if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
735 return 0;
736
737 irq = armpmu_get_cpu_irq(pmu, cpu);
738 if (irq)
739 per_cpu(cpu_irq_ops, cpu)->disable_pmuirq(irq);
740
741 per_cpu(cpu_armpmu, cpu) = NULL;
742
743 return 0;
744}
745
746#ifdef CONFIG_CPU_PM
747static void cpu_pm_pmu_setup(struct arm_pmu *armpmu, unsigned long cmd)
748{
749 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
750 struct perf_event *event;
751 int idx;
752
753 for_each_set_bit(idx, armpmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
754 event = hw_events->events[idx];
755 if (!event)
756 continue;
757
758 switch (cmd) {
759 case CPU_PM_ENTER:
760 /*
761 * Stop and update the counter
762 */
763 armpmu_stop(event, PERF_EF_UPDATE);
764 break;
765 case CPU_PM_EXIT:
766 case CPU_PM_ENTER_FAILED:
767 /*
768 * Restore and enable the counter.
769 */
770 armpmu_start(event, PERF_EF_RELOAD);
771 break;
772 default:
773 break;
774 }
775 }
776}
777
778static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
779 void *v)
780{
781 struct arm_pmu *armpmu = container_of(b, struct arm_pmu, cpu_pm_nb);
782 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
783 bool enabled = !bitmap_empty(hw_events->used_mask, ARMPMU_MAX_HWEVENTS);
784
785 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
786 return NOTIFY_DONE;
787
788 /*
789 * Always reset the PMU registers on power-up even if
790 * there are no events running.
791 */
792 if (cmd == CPU_PM_EXIT && armpmu->reset)
793 armpmu->reset(armpmu);
794
795 if (!enabled)
796 return NOTIFY_OK;
797
798 switch (cmd) {
799 case CPU_PM_ENTER:
800 armpmu->stop(armpmu);
801 cpu_pm_pmu_setup(armpmu, cmd);
802 break;
803 case CPU_PM_EXIT:
804 case CPU_PM_ENTER_FAILED:
805 cpu_pm_pmu_setup(armpmu, cmd);
806 armpmu->start(armpmu);
807 break;
808 default:
809 return NOTIFY_DONE;
810 }
811
812 return NOTIFY_OK;
813}
814
815static int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu)
816{
817 cpu_pmu->cpu_pm_nb.notifier_call = cpu_pm_pmu_notify;
818 return cpu_pm_register_notifier(&cpu_pmu->cpu_pm_nb);
819}
820
821static void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu)
822{
823 cpu_pm_unregister_notifier(&cpu_pmu->cpu_pm_nb);
824}
825#else
826static inline int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu) { return 0; }
827static inline void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu) { }
828#endif
829
830static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
831{
832 int err;
833
834 err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_STARTING,
835 &cpu_pmu->node);
836 if (err)
837 goto out;
838
839 err = cpu_pm_pmu_register(cpu_pmu);
840 if (err)
841 goto out_unregister;
842
843 return 0;
844
845out_unregister:
846 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
847 &cpu_pmu->node);
848out:
849 return err;
850}
851
852static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
853{
854 cpu_pm_pmu_unregister(cpu_pmu);
855 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
856 &cpu_pmu->node);
857}
858
859struct arm_pmu *armpmu_alloc(void)
860{
861 struct arm_pmu *pmu;
862 int cpu;
863
864 pmu = kzalloc(sizeof(*pmu), GFP_KERNEL);
865 if (!pmu)
866 goto out;
867
868 pmu->hw_events = alloc_percpu_gfp(struct pmu_hw_events, GFP_KERNEL);
869 if (!pmu->hw_events) {
870 pr_info("failed to allocate per-cpu PMU data.\n");
871 goto out_free_pmu;
872 }
873
874 pmu->pmu = (struct pmu) {
875 .pmu_enable = armpmu_enable,
876 .pmu_disable = armpmu_disable,
877 .event_init = armpmu_event_init,
878 .add = armpmu_add,
879 .del = armpmu_del,
880 .start = armpmu_start,
881 .stop = armpmu_stop,
882 .read = armpmu_read,
883 .filter = armpmu_filter,
884 .attr_groups = pmu->attr_groups,
885 /*
886 * This is a CPU PMU potentially in a heterogeneous
887 * configuration (e.g. big.LITTLE) so
888 * PERF_PMU_CAP_EXTENDED_HW_TYPE is required to open
889 * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE events on a
890 * specific PMU.
891 */
892 .capabilities = PERF_PMU_CAP_EXTENDED_REGS |
893 PERF_PMU_CAP_EXTENDED_HW_TYPE,
894 };
895
896 pmu->attr_groups[ARMPMU_ATTR_GROUP_COMMON] =
897 &armpmu_common_attr_group;
898
899 for_each_possible_cpu(cpu) {
900 struct pmu_hw_events *events;
901
902 events = per_cpu_ptr(pmu->hw_events, cpu);
903 events->percpu_pmu = pmu;
904 }
905
906 return pmu;
907
908out_free_pmu:
909 kfree(pmu);
910out:
911 return NULL;
912}
913
914void armpmu_free(struct arm_pmu *pmu)
915{
916 free_percpu(pmu->hw_events);
917 kfree(pmu);
918}
919
920int armpmu_register(struct arm_pmu *pmu)
921{
922 int ret;
923
924 ret = cpu_pmu_init(pmu);
925 if (ret)
926 return ret;
927
928 if (!pmu->set_event_filter)
929 pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
930
931 ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
932 if (ret)
933 goto out_destroy;
934
935 pr_info("enabled with %s PMU driver, %d (%*pb) counters available%s\n",
936 pmu->name, bitmap_weight(pmu->cntr_mask, ARMPMU_MAX_HWEVENTS),
937 ARMPMU_MAX_HWEVENTS, &pmu->cntr_mask,
938 has_nmi ? ", using NMIs" : "");
939
940 kvm_host_pmu_init(pmu);
941
942 return 0;
943
944out_destroy:
945 cpu_pmu_destroy(pmu);
946 return ret;
947}
948
949static int arm_pmu_hp_init(void)
950{
951 int ret;
952
953 ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_STARTING,
954 "perf/arm/pmu:starting",
955 arm_perf_starting_cpu,
956 arm_perf_teardown_cpu);
957 if (ret)
958 pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
959 ret);
960 return ret;
961}
962subsys_initcall(arm_pmu_hp_init);