1 /* Copyright (c) 2015-2017 The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 #include <linux/acpi.h>
13 #include <linux/bitops.h>
14 #include <linux/bug.h>
15 #include <linux/cpuhotplug.h>
16 #include <linux/cpumask.h>
17 #include <linux/device.h>
18 #include <linux/errno.h>
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/percpu.h>
24 #include <linux/perf_event.h>
25 #include <linux/platform_device.h>
26 #include <linux/smp.h>
27 #include <linux/spinlock.h>
28 #include <linux/sysfs.h>
29 #include <linux/types.h>
31 #include <asm/barrier.h>
32 #include <asm/local64.h>
33 #include <asm/sysreg.h>
37 #define L2PMCR_NUM_EV_SHIFT 11
38 #define L2PMCR_NUM_EV_MASK 0x1F
41 #define L2PMCNTENCLR 0x403
42 #define L2PMCNTENSET 0x404
43 #define L2PMINTENCLR 0x405
44 #define L2PMINTENSET 0x406
45 #define L2PMOVSCLR 0x407
46 #define L2PMOVSSET 0x408
47 #define L2PMCCNTCR 0x409
48 #define L2PMCCNTR 0x40A
49 #define L2PMCCNTSR 0x40C
50 #define L2PMRESR 0x410
51 #define IA_L2PMXEVCNTCR_BASE 0x420
52 #define IA_L2PMXEVCNTR_BASE 0x421
53 #define IA_L2PMXEVFILTER_BASE 0x423
54 #define IA_L2PMXEVTYPER_BASE 0x424
56 #define IA_L2_REG_OFFSET 0x10
58 #define L2PMXEVFILTER_SUFILTER_ALL 0x000E0000
59 #define L2PMXEVFILTER_ORGFILTER_IDINDEP 0x00000004
60 #define L2PMXEVFILTER_ORGFILTER_ALL 0x00000003
62 #define L2EVTYPER_REG_SHIFT 3
64 #define L2PMRESR_GROUP_BITS 8
65 #define L2PMRESR_GROUP_MASK GENMASK(7, 0)
67 #define L2CYCLE_CTR_BIT 31
68 #define L2CYCLE_CTR_RAW_CODE 0xFE
70 #define L2PMCR_RESET_ALL 0x6
71 #define L2PMCR_COUNTERS_ENABLE 0x1
72 #define L2PMCR_COUNTERS_DISABLE 0x0
74 #define L2PMRESR_EN BIT_ULL(63)
76 #define L2_EVT_MASK 0x00000FFF
77 #define L2_EVT_CODE_MASK 0x00000FF0
78 #define L2_EVT_GRP_MASK 0x0000000F
79 #define L2_EVT_CODE_SHIFT 4
80 #define L2_EVT_GRP_SHIFT 0
82 #define L2_EVT_CODE(event) (((event) & L2_EVT_CODE_MASK) >> L2_EVT_CODE_SHIFT)
83 #define L2_EVT_GROUP(event) (((event) & L2_EVT_GRP_MASK) >> L2_EVT_GRP_SHIFT)
85 #define L2_EVT_GROUP_MAX 7
87 #define L2_COUNTER_RELOAD BIT_ULL(31)
88 #define L2_CYCLE_COUNTER_RELOAD BIT_ULL(63)
90 #define L2CPUSRSELR_EL1 sys_reg(3, 3, 15, 0, 6)
91 #define L2CPUSRDR_EL1 sys_reg(3, 3, 15, 0, 7)
93 #define reg_idx(reg, i) (((i) * IA_L2_REG_OFFSET) + reg##_BASE)
95 static DEFINE_RAW_SPINLOCK(l2_access_lock);
98 * set_l2_indirect_reg: write value to an L2 register
99 * @reg: Address of L2 register.
100 * @value: Value to be written to register.
102 * Use architecturally required barriers for ordering between system register
105 static void set_l2_indirect_reg(u64 reg, u64 val)
109 raw_spin_lock_irqsave(&l2_access_lock, flags);
110 write_sysreg_s(reg, L2CPUSRSELR_EL1);
112 write_sysreg_s(val, L2CPUSRDR_EL1);
114 raw_spin_unlock_irqrestore(&l2_access_lock, flags);
118 * get_l2_indirect_reg: read an L2 register value
119 * @reg: Address of L2 register.
121 * Use architecturally required barriers for ordering between system register
124 static u64 get_l2_indirect_reg(u64 reg)
129 raw_spin_lock_irqsave(&l2_access_lock, flags);
130 write_sysreg_s(reg, L2CPUSRSELR_EL1);
132 val = read_sysreg_s(L2CPUSRDR_EL1);
133 raw_spin_unlock_irqrestore(&l2_access_lock, flags);
141 * Aggregate PMU. Implements the core pmu functions and manages
145 struct hlist_node node;
150 struct platform_device *pdev;
151 struct cluster_pmu * __percpu *pmu_cluster;
152 struct list_head clusters;
156 * The cache is made up of one or more clusters, each cluster has its own PMU.
157 * Each cluster is associated with one or more CPUs.
158 * This structure represents one of the hardware PMUs.
160 * Events can be envisioned as a 2-dimensional array. Each column represents
161 * a group of events. There are 8 groups. Only one entry from each
162 * group can be in use at a time.
164 * Events are specified as 0xCCG, where CC is 2 hex digits specifying
165 * the code (array row) and G specifies the group (column).
167 * In addition there is a cycle counter event specified by L2CYCLE_CTR_RAW_CODE
168 * which is outside the above scheme.
171 struct list_head next;
172 struct perf_event *events[MAX_L2_CTRS];
173 struct l2cache_pmu *l2cache_pmu;
174 DECLARE_BITMAP(used_counters, MAX_L2_CTRS);
175 DECLARE_BITMAP(used_groups, L2_EVT_GROUP_MAX + 1);
178 /* The CPU that is used for collecting events on this cluster */
180 /* All the CPUs associated with this cluster */
181 cpumask_t cluster_cpus;
185 #define to_l2cache_pmu(p) (container_of(p, struct l2cache_pmu, pmu))
187 static u32 l2_cycle_ctr_idx;
188 static u32 l2_counter_present_mask;
190 static inline u32 idx_to_reg_bit(u32 idx)
192 if (idx == l2_cycle_ctr_idx)
193 return BIT(L2CYCLE_CTR_BIT);
198 static inline struct cluster_pmu *get_cluster_pmu(
199 struct l2cache_pmu *l2cache_pmu, int cpu)
201 return *per_cpu_ptr(l2cache_pmu->pmu_cluster, cpu);
204 static void cluster_pmu_reset(void)
206 /* Reset all counters */
207 set_l2_indirect_reg(L2PMCR, L2PMCR_RESET_ALL);
208 set_l2_indirect_reg(L2PMCNTENCLR, l2_counter_present_mask);
209 set_l2_indirect_reg(L2PMINTENCLR, l2_counter_present_mask);
210 set_l2_indirect_reg(L2PMOVSCLR, l2_counter_present_mask);
213 static inline void cluster_pmu_enable(void)
215 set_l2_indirect_reg(L2PMCR, L2PMCR_COUNTERS_ENABLE);
218 static inline void cluster_pmu_disable(void)
220 set_l2_indirect_reg(L2PMCR, L2PMCR_COUNTERS_DISABLE);
223 static inline void cluster_pmu_counter_set_value(u32 idx, u64 value)
225 if (idx == l2_cycle_ctr_idx)
226 set_l2_indirect_reg(L2PMCCNTR, value);
228 set_l2_indirect_reg(reg_idx(IA_L2PMXEVCNTR, idx), value);
231 static inline u64 cluster_pmu_counter_get_value(u32 idx)
235 if (idx == l2_cycle_ctr_idx)
236 value = get_l2_indirect_reg(L2PMCCNTR);
238 value = get_l2_indirect_reg(reg_idx(IA_L2PMXEVCNTR, idx));
243 static inline void cluster_pmu_counter_enable(u32 idx)
245 set_l2_indirect_reg(L2PMCNTENSET, idx_to_reg_bit(idx));
248 static inline void cluster_pmu_counter_disable(u32 idx)
250 set_l2_indirect_reg(L2PMCNTENCLR, idx_to_reg_bit(idx));
253 static inline void cluster_pmu_counter_enable_interrupt(u32 idx)
255 set_l2_indirect_reg(L2PMINTENSET, idx_to_reg_bit(idx));
258 static inline void cluster_pmu_counter_disable_interrupt(u32 idx)
260 set_l2_indirect_reg(L2PMINTENCLR, idx_to_reg_bit(idx));
263 static inline void cluster_pmu_set_evccntcr(u32 val)
265 set_l2_indirect_reg(L2PMCCNTCR, val);
268 static inline void cluster_pmu_set_evcntcr(u32 ctr, u32 val)
270 set_l2_indirect_reg(reg_idx(IA_L2PMXEVCNTCR, ctr), val);
273 static inline void cluster_pmu_set_evtyper(u32 ctr, u32 val)
275 set_l2_indirect_reg(reg_idx(IA_L2PMXEVTYPER, ctr), val);
278 static void cluster_pmu_set_resr(struct cluster_pmu *cluster,
279 u32 event_group, u32 event_cc)
286 shift = L2PMRESR_GROUP_BITS * event_group;
287 field = ((u64)(event_cc & L2PMRESR_GROUP_MASK) << shift);
289 spin_lock_irqsave(&cluster->pmu_lock, flags);
291 resr_val = get_l2_indirect_reg(L2PMRESR);
292 resr_val &= ~(L2PMRESR_GROUP_MASK << shift);
294 resr_val |= L2PMRESR_EN;
295 set_l2_indirect_reg(L2PMRESR, resr_val);
297 spin_unlock_irqrestore(&cluster->pmu_lock, flags);
301 * Hardware allows filtering of events based on the originating
302 * CPU. Turn this off by setting filter bits to allow events from
303 * all CPUS, subunits and ID independent events in this cluster.
305 static inline void cluster_pmu_set_evfilter_sys_mode(u32 ctr)
307 u32 val = L2PMXEVFILTER_SUFILTER_ALL |
308 L2PMXEVFILTER_ORGFILTER_IDINDEP |
309 L2PMXEVFILTER_ORGFILTER_ALL;
311 set_l2_indirect_reg(reg_idx(IA_L2PMXEVFILTER, ctr), val);
314 static inline u32 cluster_pmu_getreset_ovsr(void)
316 u32 result = get_l2_indirect_reg(L2PMOVSSET);
318 set_l2_indirect_reg(L2PMOVSCLR, result);
322 static inline bool cluster_pmu_has_overflowed(u32 ovsr)
324 return !!(ovsr & l2_counter_present_mask);
327 static inline bool cluster_pmu_counter_has_overflowed(u32 ovsr, u32 idx)
329 return !!(ovsr & idx_to_reg_bit(idx));
332 static void l2_cache_event_update(struct perf_event *event)
334 struct hw_perf_event *hwc = &event->hw;
335 u64 delta, prev, now;
339 prev = local64_read(&hwc->prev_count);
340 now = cluster_pmu_counter_get_value(idx);
341 } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
344 * The cycle counter is 64-bit, but all other counters are
345 * 32-bit, and we must handle 32-bit overflow explicitly.
348 if (idx != l2_cycle_ctr_idx)
351 local64_add(delta, &event->count);
354 static void l2_cache_cluster_set_period(struct cluster_pmu *cluster,
355 struct hw_perf_event *hwc)
361 * We limit the max period to half the max counter value so
362 * that even in the case of extreme interrupt latency the
363 * counter will (hopefully) not wrap past its initial value.
365 if (idx == l2_cycle_ctr_idx)
366 new = L2_CYCLE_COUNTER_RELOAD;
368 new = L2_COUNTER_RELOAD;
370 local64_set(&hwc->prev_count, new);
371 cluster_pmu_counter_set_value(idx, new);
374 static int l2_cache_get_event_idx(struct cluster_pmu *cluster,
375 struct perf_event *event)
377 struct hw_perf_event *hwc = &event->hw;
379 int num_ctrs = cluster->l2cache_pmu->num_counters - 1;
382 if (hwc->config_base == L2CYCLE_CTR_RAW_CODE) {
383 if (test_and_set_bit(l2_cycle_ctr_idx, cluster->used_counters))
386 return l2_cycle_ctr_idx;
389 idx = find_first_zero_bit(cluster->used_counters, num_ctrs);
391 /* The counters are all in use. */
395 * Check for column exclusion: event column already in use by another
396 * event. This is for events which are not in the same group.
397 * Conflicting events in the same group are detected in event_init.
399 group = L2_EVT_GROUP(hwc->config_base);
400 if (test_bit(group, cluster->used_groups))
403 set_bit(idx, cluster->used_counters);
404 set_bit(group, cluster->used_groups);
409 static void l2_cache_clear_event_idx(struct cluster_pmu *cluster,
410 struct perf_event *event)
412 struct hw_perf_event *hwc = &event->hw;
415 clear_bit(idx, cluster->used_counters);
416 if (hwc->config_base != L2CYCLE_CTR_RAW_CODE)
417 clear_bit(L2_EVT_GROUP(hwc->config_base), cluster->used_groups);
420 static irqreturn_t l2_cache_handle_irq(int irq_num, void *data)
422 struct cluster_pmu *cluster = data;
423 int num_counters = cluster->l2cache_pmu->num_counters;
427 ovsr = cluster_pmu_getreset_ovsr();
428 if (!cluster_pmu_has_overflowed(ovsr))
431 for_each_set_bit(idx, cluster->used_counters, num_counters) {
432 struct perf_event *event = cluster->events[idx];
433 struct hw_perf_event *hwc;
435 if (WARN_ON_ONCE(!event))
438 if (!cluster_pmu_counter_has_overflowed(ovsr, idx))
441 l2_cache_event_update(event);
444 l2_cache_cluster_set_period(cluster, hwc);
451 * Implementation of abstract pmu functionality required by
452 * the core perf events code.
455 static void l2_cache_pmu_enable(struct pmu *pmu)
458 * Although there is only one PMU (per socket) controlling multiple
459 * physical PMUs (per cluster), because we do not support per-task mode
460 * each event is associated with a CPU. Each event has pmu_enable
461 * called on its CPU, so here it is only necessary to enable the
462 * counters for the current CPU.
465 cluster_pmu_enable();
468 static void l2_cache_pmu_disable(struct pmu *pmu)
470 cluster_pmu_disable();
473 static int l2_cache_event_init(struct perf_event *event)
475 struct hw_perf_event *hwc = &event->hw;
476 struct cluster_pmu *cluster;
477 struct perf_event *sibling;
478 struct l2cache_pmu *l2cache_pmu;
480 if (event->attr.type != event->pmu->type)
483 l2cache_pmu = to_l2cache_pmu(event->pmu);
485 if (hwc->sample_period) {
486 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
487 "Sampling not supported\n");
491 if (event->cpu < 0) {
492 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
493 "Per-task mode not supported\n");
497 /* We cannot filter accurately so we just don't allow it. */
498 if (event->attr.exclude_user || event->attr.exclude_kernel ||
499 event->attr.exclude_hv || event->attr.exclude_idle) {
500 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
501 "Can't exclude execution levels\n");
505 if (((L2_EVT_GROUP(event->attr.config) > L2_EVT_GROUP_MAX) ||
506 ((event->attr.config & ~L2_EVT_MASK) != 0)) &&
507 (event->attr.config != L2CYCLE_CTR_RAW_CODE)) {
508 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
509 "Invalid config %llx\n",
514 /* Don't allow groups with mixed PMUs, except for s/w events */
515 if (event->group_leader->pmu != event->pmu &&
516 !is_software_event(event->group_leader)) {
517 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
518 "Can't create mixed PMU group\n");
522 list_for_each_entry(sibling, &event->group_leader->sibling_list,
524 if (sibling->pmu != event->pmu &&
525 !is_software_event(sibling)) {
526 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
527 "Can't create mixed PMU group\n");
531 cluster = get_cluster_pmu(l2cache_pmu, event->cpu);
533 /* CPU has not been initialised */
534 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
535 "CPU%d not associated with L2 cluster\n", event->cpu);
539 /* Ensure all events in a group are on the same cpu */
540 if ((event->group_leader != event) &&
541 (cluster->on_cpu != event->group_leader->cpu)) {
542 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
543 "Can't create group on CPUs %d and %d",
544 event->cpu, event->group_leader->cpu);
548 if ((event != event->group_leader) &&
549 (L2_EVT_GROUP(event->group_leader->attr.config) ==
550 L2_EVT_GROUP(event->attr.config))) {
551 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
552 "Column exclusion: conflicting events %llx %llx\n",
553 event->group_leader->attr.config,
558 list_for_each_entry(sibling, &event->group_leader->sibling_list,
560 if ((sibling != event) &&
561 (L2_EVT_GROUP(sibling->attr.config) ==
562 L2_EVT_GROUP(event->attr.config))) {
563 dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
564 "Column exclusion: conflicting events %llx %llx\n",
565 sibling->attr.config,
572 hwc->config_base = event->attr.config;
575 * Ensure all events are on the same cpu so all events are in the
576 * same cpu context, to avoid races on pmu_enable etc.
578 event->cpu = cluster->on_cpu;
583 static void l2_cache_event_start(struct perf_event *event, int flags)
585 struct cluster_pmu *cluster;
586 struct hw_perf_event *hwc = &event->hw;
589 u32 event_cc, event_group;
593 cluster = get_cluster_pmu(to_l2cache_pmu(event->pmu), event->cpu);
595 l2_cache_cluster_set_period(cluster, hwc);
597 if (hwc->config_base == L2CYCLE_CTR_RAW_CODE) {
598 cluster_pmu_set_evccntcr(0);
600 config = hwc->config_base;
601 event_cc = L2_EVT_CODE(config);
602 event_group = L2_EVT_GROUP(config);
604 cluster_pmu_set_evcntcr(idx, 0);
605 cluster_pmu_set_evtyper(idx, event_group);
606 cluster_pmu_set_resr(cluster, event_group, event_cc);
607 cluster_pmu_set_evfilter_sys_mode(idx);
610 cluster_pmu_counter_enable_interrupt(idx);
611 cluster_pmu_counter_enable(idx);
614 static void l2_cache_event_stop(struct perf_event *event, int flags)
616 struct hw_perf_event *hwc = &event->hw;
619 if (hwc->state & PERF_HES_STOPPED)
622 cluster_pmu_counter_disable_interrupt(idx);
623 cluster_pmu_counter_disable(idx);
625 if (flags & PERF_EF_UPDATE)
626 l2_cache_event_update(event);
627 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
630 static int l2_cache_event_add(struct perf_event *event, int flags)
632 struct hw_perf_event *hwc = &event->hw;
635 struct cluster_pmu *cluster;
637 cluster = get_cluster_pmu(to_l2cache_pmu(event->pmu), event->cpu);
639 idx = l2_cache_get_event_idx(cluster, event);
644 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
645 cluster->events[idx] = event;
646 local64_set(&hwc->prev_count, 0);
648 if (flags & PERF_EF_START)
649 l2_cache_event_start(event, flags);
651 /* Propagate changes to the userspace mapping. */
652 perf_event_update_userpage(event);
657 static void l2_cache_event_del(struct perf_event *event, int flags)
659 struct hw_perf_event *hwc = &event->hw;
660 struct cluster_pmu *cluster;
663 cluster = get_cluster_pmu(to_l2cache_pmu(event->pmu), event->cpu);
665 l2_cache_event_stop(event, flags | PERF_EF_UPDATE);
666 cluster->events[idx] = NULL;
667 l2_cache_clear_event_idx(cluster, event);
669 perf_event_update_userpage(event);
672 static void l2_cache_event_read(struct perf_event *event)
674 l2_cache_event_update(event);
677 static ssize_t l2_cache_pmu_cpumask_show(struct device *dev,
678 struct device_attribute *attr,
681 struct l2cache_pmu *l2cache_pmu = to_l2cache_pmu(dev_get_drvdata(dev));
683 return cpumap_print_to_pagebuf(true, buf, &l2cache_pmu->cpumask);
686 static struct device_attribute l2_cache_pmu_cpumask_attr =
687 __ATTR(cpumask, S_IRUGO, l2_cache_pmu_cpumask_show, NULL);
689 static struct attribute *l2_cache_pmu_cpumask_attrs[] = {
690 &l2_cache_pmu_cpumask_attr.attr,
694 static struct attribute_group l2_cache_pmu_cpumask_group = {
695 .attrs = l2_cache_pmu_cpumask_attrs,
698 /* CCG format for perf RAW codes. */
699 PMU_FORMAT_ATTR(l2_code, "config:4-11");
700 PMU_FORMAT_ATTR(l2_group, "config:0-3");
701 static struct attribute *l2_cache_pmu_formats[] = {
702 &format_attr_l2_code.attr,
703 &format_attr_l2_group.attr,
707 static struct attribute_group l2_cache_pmu_format_group = {
709 .attrs = l2_cache_pmu_formats,
712 static const struct attribute_group *l2_cache_pmu_attr_grps[] = {
713 &l2_cache_pmu_format_group,
714 &l2_cache_pmu_cpumask_group,
719 * Generic device handlers
722 static const struct acpi_device_id l2_cache_pmu_acpi_match[] = {
727 static int get_num_counters(void)
731 val = get_l2_indirect_reg(L2PMCR);
734 * Read number of counters from L2PMCR and add 1
735 * for the cycle counter.
737 return ((val >> L2PMCR_NUM_EV_SHIFT) & L2PMCR_NUM_EV_MASK) + 1;
740 static struct cluster_pmu *l2_cache_associate_cpu_with_cluster(
741 struct l2cache_pmu *l2cache_pmu, int cpu)
745 struct cluster_pmu *cluster = NULL;
748 * This assumes that the cluster_id is in MPIDR[aff1] for
749 * single-threaded cores, and MPIDR[aff2] for multi-threaded
750 * cores. This logic will have to be updated if this changes.
752 mpidr = read_cpuid_mpidr();
753 if (mpidr & MPIDR_MT_BITMASK)
754 cpu_cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
756 cpu_cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
758 list_for_each_entry(cluster, &l2cache_pmu->clusters, next) {
759 if (cluster->cluster_id != cpu_cluster_id)
762 dev_info(&l2cache_pmu->pdev->dev,
763 "CPU%d associated with cluster %d\n", cpu,
764 cluster->cluster_id);
765 cpumask_set_cpu(cpu, &cluster->cluster_cpus);
766 *per_cpu_ptr(l2cache_pmu->pmu_cluster, cpu) = cluster;
773 static int l2cache_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
775 struct cluster_pmu *cluster;
776 struct l2cache_pmu *l2cache_pmu;
778 l2cache_pmu = hlist_entry_safe(node, struct l2cache_pmu, node);
779 cluster = get_cluster_pmu(l2cache_pmu, cpu);
781 /* First time this CPU has come online */
782 cluster = l2_cache_associate_cpu_with_cluster(l2cache_pmu, cpu);
784 /* Only if broken firmware doesn't list every cluster */
785 WARN_ONCE(1, "No L2 cache cluster for CPU%d\n", cpu);
790 /* If another CPU is managing this cluster, we're done */
791 if (cluster->on_cpu != -1)
795 * All CPUs on this cluster were down, use this one.
796 * Reset to put it into sane state.
798 cluster->on_cpu = cpu;
799 cpumask_set_cpu(cpu, &l2cache_pmu->cpumask);
802 WARN_ON(irq_set_affinity(cluster->irq, cpumask_of(cpu)));
803 enable_irq(cluster->irq);
808 static int l2cache_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
810 struct cluster_pmu *cluster;
811 struct l2cache_pmu *l2cache_pmu;
812 cpumask_t cluster_online_cpus;
815 l2cache_pmu = hlist_entry_safe(node, struct l2cache_pmu, node);
816 cluster = get_cluster_pmu(l2cache_pmu, cpu);
820 /* If this CPU is not managing the cluster, we're done */
821 if (cluster->on_cpu != cpu)
824 /* Give up ownership of cluster */
825 cpumask_clear_cpu(cpu, &l2cache_pmu->cpumask);
826 cluster->on_cpu = -1;
828 /* Any other CPU for this cluster which is still online */
829 cpumask_and(&cluster_online_cpus, &cluster->cluster_cpus,
831 target = cpumask_any_but(&cluster_online_cpus, cpu);
832 if (target >= nr_cpu_ids) {
833 disable_irq(cluster->irq);
837 perf_pmu_migrate_context(&l2cache_pmu->pmu, cpu, target);
838 cluster->on_cpu = target;
839 cpumask_set_cpu(target, &l2cache_pmu->cpumask);
840 WARN_ON(irq_set_affinity(cluster->irq, cpumask_of(target)));
845 static int l2_cache_pmu_probe_cluster(struct device *dev, void *data)
847 struct platform_device *pdev = to_platform_device(dev->parent);
848 struct platform_device *sdev = to_platform_device(dev);
849 struct l2cache_pmu *l2cache_pmu = data;
850 struct cluster_pmu *cluster;
851 struct acpi_device *device;
852 unsigned long fw_cluster_id;
856 if (acpi_bus_get_device(ACPI_HANDLE(dev), &device))
859 if (kstrtoul(device->pnp.unique_id, 10, &fw_cluster_id) < 0) {
860 dev_err(&pdev->dev, "unable to read ACPI uid\n");
864 cluster = devm_kzalloc(&pdev->dev, sizeof(*cluster), GFP_KERNEL);
868 INIT_LIST_HEAD(&cluster->next);
869 list_add(&cluster->next, &l2cache_pmu->clusters);
870 cluster->cluster_id = fw_cluster_id;
872 irq = platform_get_irq(sdev, 0);
875 "Failed to get valid irq for cluster %ld\n",
879 irq_set_status_flags(irq, IRQ_NOAUTOEN);
882 cluster->l2cache_pmu = l2cache_pmu;
883 cluster->on_cpu = -1;
885 err = devm_request_irq(&pdev->dev, irq, l2_cache_handle_irq,
886 IRQF_NOBALANCING | IRQF_NO_THREAD,
887 "l2-cache-pmu", cluster);
890 "Unable to request IRQ%d for L2 PMU counters\n", irq);
895 "Registered L2 cache PMU cluster %ld\n", fw_cluster_id);
897 spin_lock_init(&cluster->pmu_lock);
899 l2cache_pmu->num_pmus++;
904 static int l2_cache_pmu_probe(struct platform_device *pdev)
907 struct l2cache_pmu *l2cache_pmu;
910 devm_kzalloc(&pdev->dev, sizeof(*l2cache_pmu), GFP_KERNEL);
914 INIT_LIST_HEAD(&l2cache_pmu->clusters);
916 platform_set_drvdata(pdev, l2cache_pmu);
917 l2cache_pmu->pmu = (struct pmu) {
918 /* suffix is instance id for future use with multiple sockets */
920 .task_ctx_nr = perf_invalid_context,
921 .pmu_enable = l2_cache_pmu_enable,
922 .pmu_disable = l2_cache_pmu_disable,
923 .event_init = l2_cache_event_init,
924 .add = l2_cache_event_add,
925 .del = l2_cache_event_del,
926 .start = l2_cache_event_start,
927 .stop = l2_cache_event_stop,
928 .read = l2_cache_event_read,
929 .attr_groups = l2_cache_pmu_attr_grps,
932 l2cache_pmu->num_counters = get_num_counters();
933 l2cache_pmu->pdev = pdev;
934 l2cache_pmu->pmu_cluster = devm_alloc_percpu(&pdev->dev,
935 struct cluster_pmu *);
936 if (!l2cache_pmu->pmu_cluster)
939 l2_cycle_ctr_idx = l2cache_pmu->num_counters - 1;
940 l2_counter_present_mask = GENMASK(l2cache_pmu->num_counters - 2, 0) |
941 BIT(L2CYCLE_CTR_BIT);
943 cpumask_clear(&l2cache_pmu->cpumask);
945 /* Read cluster info and initialize each cluster */
946 err = device_for_each_child(&pdev->dev, l2cache_pmu,
947 l2_cache_pmu_probe_cluster);
951 if (l2cache_pmu->num_pmus == 0) {
952 dev_err(&pdev->dev, "No hardware L2 cache PMUs found\n");
956 err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
959 dev_err(&pdev->dev, "Error %d registering hotplug", err);
963 err = perf_pmu_register(&l2cache_pmu->pmu, l2cache_pmu->pmu.name, -1);
965 dev_err(&pdev->dev, "Error %d registering L2 cache PMU\n", err);
969 dev_info(&pdev->dev, "Registered L2 cache PMU using %d HW PMUs\n",
970 l2cache_pmu->num_pmus);
975 cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
980 static int l2_cache_pmu_remove(struct platform_device *pdev)
982 struct l2cache_pmu *l2cache_pmu =
983 to_l2cache_pmu(platform_get_drvdata(pdev));
985 perf_pmu_unregister(&l2cache_pmu->pmu);
986 cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
991 static struct platform_driver l2_cache_pmu_driver = {
993 .name = "qcom-l2cache-pmu",
994 .acpi_match_table = ACPI_PTR(l2_cache_pmu_acpi_match),
996 .probe = l2_cache_pmu_probe,
997 .remove = l2_cache_pmu_remove,
1000 static int __init register_l2_cache_pmu_driver(void)
1004 err = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
1005 "AP_PERF_ARM_QCOM_L2_ONLINE",
1006 l2cache_pmu_online_cpu,
1007 l2cache_pmu_offline_cpu);
1011 return platform_driver_register(&l2_cache_pmu_driver);
1013 device_initcall(register_l2_cache_pmu_driver);