Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (c) 2015-2017 The Linux Foundation. All rights reserved.
0003  */
0004 #include <linux/acpi.h>
0005 #include <linux/bitops.h>
0006 #include <linux/bug.h>
0007 #include <linux/cpuhotplug.h>
0008 #include <linux/cpumask.h>
0009 #include <linux/device.h>
0010 #include <linux/errno.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/irq.h>
0013 #include <linux/kernel.h>
0014 #include <linux/list.h>
0015 #include <linux/percpu.h>
0016 #include <linux/perf_event.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/smp.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/sysfs.h>
0021 #include <linux/types.h>
0022 
0023 #include <asm/barrier.h>
0024 #include <asm/local64.h>
0025 #include <asm/sysreg.h>
0026 #include <soc/qcom/kryo-l2-accessors.h>
0027 
0028 #define MAX_L2_CTRS             9
0029 
0030 #define L2PMCR_NUM_EV_SHIFT     11
0031 #define L2PMCR_NUM_EV_MASK      0x1F
0032 
0033 #define L2PMCR                  0x400
0034 #define L2PMCNTENCLR            0x403
0035 #define L2PMCNTENSET            0x404
0036 #define L2PMINTENCLR            0x405
0037 #define L2PMINTENSET            0x406
0038 #define L2PMOVSCLR              0x407
0039 #define L2PMOVSSET              0x408
0040 #define L2PMCCNTCR              0x409
0041 #define L2PMCCNTR               0x40A
0042 #define L2PMCCNTSR              0x40C
0043 #define L2PMRESR                0x410
0044 #define IA_L2PMXEVCNTCR_BASE    0x420
0045 #define IA_L2PMXEVCNTR_BASE     0x421
0046 #define IA_L2PMXEVFILTER_BASE   0x423
0047 #define IA_L2PMXEVTYPER_BASE    0x424
0048 
0049 #define IA_L2_REG_OFFSET        0x10
0050 
0051 #define L2PMXEVFILTER_SUFILTER_ALL      0x000E0000
0052 #define L2PMXEVFILTER_ORGFILTER_IDINDEP 0x00000004
0053 #define L2PMXEVFILTER_ORGFILTER_ALL     0x00000003
0054 
0055 #define L2EVTYPER_REG_SHIFT     3
0056 
0057 #define L2PMRESR_GROUP_BITS     8
0058 #define L2PMRESR_GROUP_MASK     GENMASK(7, 0)
0059 
0060 #define L2CYCLE_CTR_BIT         31
0061 #define L2CYCLE_CTR_RAW_CODE    0xFE
0062 
0063 #define L2PMCR_RESET_ALL        0x6
0064 #define L2PMCR_COUNTERS_ENABLE  0x1
0065 #define L2PMCR_COUNTERS_DISABLE 0x0
0066 
0067 #define L2PMRESR_EN             BIT_ULL(63)
0068 
0069 #define L2_EVT_MASK             0x00000FFF
0070 #define L2_EVT_CODE_MASK        0x00000FF0
0071 #define L2_EVT_GRP_MASK         0x0000000F
0072 #define L2_EVT_CODE_SHIFT       4
0073 #define L2_EVT_GRP_SHIFT        0
0074 
0075 #define L2_EVT_CODE(event)   (((event) & L2_EVT_CODE_MASK) >> L2_EVT_CODE_SHIFT)
0076 #define L2_EVT_GROUP(event)  (((event) & L2_EVT_GRP_MASK) >> L2_EVT_GRP_SHIFT)
0077 
0078 #define L2_EVT_GROUP_MAX        7
0079 
0080 #define L2_COUNTER_RELOAD       BIT_ULL(31)
0081 #define L2_CYCLE_COUNTER_RELOAD BIT_ULL(63)
0082 
0083 
0084 #define reg_idx(reg, i)         (((i) * IA_L2_REG_OFFSET) + reg##_BASE)
0085 
0086 /*
0087  * Events
0088  */
0089 #define L2_EVENT_CYCLES                    0xfe
0090 #define L2_EVENT_DCACHE_OPS                0x400
0091 #define L2_EVENT_ICACHE_OPS                0x401
0092 #define L2_EVENT_TLBI                      0x402
0093 #define L2_EVENT_BARRIERS                  0x403
0094 #define L2_EVENT_TOTAL_READS               0x405
0095 #define L2_EVENT_TOTAL_WRITES              0x406
0096 #define L2_EVENT_TOTAL_REQUESTS            0x407
0097 #define L2_EVENT_LDREX                     0x420
0098 #define L2_EVENT_STREX                     0x421
0099 #define L2_EVENT_CLREX                     0x422
0100 
0101 
0102 
0103 struct cluster_pmu;
0104 
0105 /*
0106  * Aggregate PMU. Implements the core pmu functions and manages
0107  * the hardware PMUs.
0108  */
0109 struct l2cache_pmu {
0110     struct hlist_node node;
0111     u32 num_pmus;
0112     struct pmu pmu;
0113     int num_counters;
0114     cpumask_t cpumask;
0115     struct platform_device *pdev;
0116     struct cluster_pmu * __percpu *pmu_cluster;
0117     struct list_head clusters;
0118 };
0119 
0120 /*
0121  * The cache is made up of one or more clusters, each cluster has its own PMU.
0122  * Each cluster is associated with one or more CPUs.
0123  * This structure represents one of the hardware PMUs.
0124  *
0125  * Events can be envisioned as a 2-dimensional array. Each column represents
0126  * a group of events. There are 8 groups. Only one entry from each
0127  * group can be in use at a time.
0128  *
0129  * Events are specified as 0xCCG, where CC is 2 hex digits specifying
0130  * the code (array row) and G specifies the group (column).
0131  *
0132  * In addition there is a cycle counter event specified by L2CYCLE_CTR_RAW_CODE
0133  * which is outside the above scheme.
0134  */
0135 struct cluster_pmu {
0136     struct list_head next;
0137     struct perf_event *events[MAX_L2_CTRS];
0138     struct l2cache_pmu *l2cache_pmu;
0139     DECLARE_BITMAP(used_counters, MAX_L2_CTRS);
0140     DECLARE_BITMAP(used_groups, L2_EVT_GROUP_MAX + 1);
0141     int irq;
0142     int cluster_id;
0143     /* The CPU that is used for collecting events on this cluster */
0144     int on_cpu;
0145     /* All the CPUs associated with this cluster */
0146     cpumask_t cluster_cpus;
0147     spinlock_t pmu_lock;
0148 };
0149 
0150 #define to_l2cache_pmu(p) (container_of(p, struct l2cache_pmu, pmu))
0151 
0152 static u32 l2_cycle_ctr_idx;
0153 static u32 l2_counter_present_mask;
0154 
0155 static inline u32 idx_to_reg_bit(u32 idx)
0156 {
0157     if (idx == l2_cycle_ctr_idx)
0158         return BIT(L2CYCLE_CTR_BIT);
0159 
0160     return BIT(idx);
0161 }
0162 
0163 static inline struct cluster_pmu *get_cluster_pmu(
0164     struct l2cache_pmu *l2cache_pmu, int cpu)
0165 {
0166     return *per_cpu_ptr(l2cache_pmu->pmu_cluster, cpu);
0167 }
0168 
0169 static void cluster_pmu_reset(void)
0170 {
0171     /* Reset all counters */
0172     kryo_l2_set_indirect_reg(L2PMCR, L2PMCR_RESET_ALL);
0173     kryo_l2_set_indirect_reg(L2PMCNTENCLR, l2_counter_present_mask);
0174     kryo_l2_set_indirect_reg(L2PMINTENCLR, l2_counter_present_mask);
0175     kryo_l2_set_indirect_reg(L2PMOVSCLR, l2_counter_present_mask);
0176 }
0177 
0178 static inline void cluster_pmu_enable(void)
0179 {
0180     kryo_l2_set_indirect_reg(L2PMCR, L2PMCR_COUNTERS_ENABLE);
0181 }
0182 
0183 static inline void cluster_pmu_disable(void)
0184 {
0185     kryo_l2_set_indirect_reg(L2PMCR, L2PMCR_COUNTERS_DISABLE);
0186 }
0187 
0188 static inline void cluster_pmu_counter_set_value(u32 idx, u64 value)
0189 {
0190     if (idx == l2_cycle_ctr_idx)
0191         kryo_l2_set_indirect_reg(L2PMCCNTR, value);
0192     else
0193         kryo_l2_set_indirect_reg(reg_idx(IA_L2PMXEVCNTR, idx), value);
0194 }
0195 
0196 static inline u64 cluster_pmu_counter_get_value(u32 idx)
0197 {
0198     u64 value;
0199 
0200     if (idx == l2_cycle_ctr_idx)
0201         value = kryo_l2_get_indirect_reg(L2PMCCNTR);
0202     else
0203         value = kryo_l2_get_indirect_reg(reg_idx(IA_L2PMXEVCNTR, idx));
0204 
0205     return value;
0206 }
0207 
0208 static inline void cluster_pmu_counter_enable(u32 idx)
0209 {
0210     kryo_l2_set_indirect_reg(L2PMCNTENSET, idx_to_reg_bit(idx));
0211 }
0212 
0213 static inline void cluster_pmu_counter_disable(u32 idx)
0214 {
0215     kryo_l2_set_indirect_reg(L2PMCNTENCLR, idx_to_reg_bit(idx));
0216 }
0217 
0218 static inline void cluster_pmu_counter_enable_interrupt(u32 idx)
0219 {
0220     kryo_l2_set_indirect_reg(L2PMINTENSET, idx_to_reg_bit(idx));
0221 }
0222 
0223 static inline void cluster_pmu_counter_disable_interrupt(u32 idx)
0224 {
0225     kryo_l2_set_indirect_reg(L2PMINTENCLR, idx_to_reg_bit(idx));
0226 }
0227 
0228 static inline void cluster_pmu_set_evccntcr(u32 val)
0229 {
0230     kryo_l2_set_indirect_reg(L2PMCCNTCR, val);
0231 }
0232 
0233 static inline void cluster_pmu_set_evcntcr(u32 ctr, u32 val)
0234 {
0235     kryo_l2_set_indirect_reg(reg_idx(IA_L2PMXEVCNTCR, ctr), val);
0236 }
0237 
0238 static inline void cluster_pmu_set_evtyper(u32 ctr, u32 val)
0239 {
0240     kryo_l2_set_indirect_reg(reg_idx(IA_L2PMXEVTYPER, ctr), val);
0241 }
0242 
0243 static void cluster_pmu_set_resr(struct cluster_pmu *cluster,
0244                    u32 event_group, u32 event_cc)
0245 {
0246     u64 field;
0247     u64 resr_val;
0248     u32 shift;
0249     unsigned long flags;
0250 
0251     shift = L2PMRESR_GROUP_BITS * event_group;
0252     field = ((u64)(event_cc & L2PMRESR_GROUP_MASK) << shift);
0253 
0254     spin_lock_irqsave(&cluster->pmu_lock, flags);
0255 
0256     resr_val = kryo_l2_get_indirect_reg(L2PMRESR);
0257     resr_val &= ~(L2PMRESR_GROUP_MASK << shift);
0258     resr_val |= field;
0259     resr_val |= L2PMRESR_EN;
0260     kryo_l2_set_indirect_reg(L2PMRESR, resr_val);
0261 
0262     spin_unlock_irqrestore(&cluster->pmu_lock, flags);
0263 }
0264 
0265 /*
0266  * Hardware allows filtering of events based on the originating
0267  * CPU. Turn this off by setting filter bits to allow events from
0268  * all CPUS, subunits and ID independent events in this cluster.
0269  */
0270 static inline void cluster_pmu_set_evfilter_sys_mode(u32 ctr)
0271 {
0272     u32 val =  L2PMXEVFILTER_SUFILTER_ALL |
0273            L2PMXEVFILTER_ORGFILTER_IDINDEP |
0274            L2PMXEVFILTER_ORGFILTER_ALL;
0275 
0276     kryo_l2_set_indirect_reg(reg_idx(IA_L2PMXEVFILTER, ctr), val);
0277 }
0278 
0279 static inline u32 cluster_pmu_getreset_ovsr(void)
0280 {
0281     u32 result = kryo_l2_get_indirect_reg(L2PMOVSSET);
0282 
0283     kryo_l2_set_indirect_reg(L2PMOVSCLR, result);
0284     return result;
0285 }
0286 
0287 static inline bool cluster_pmu_has_overflowed(u32 ovsr)
0288 {
0289     return !!(ovsr & l2_counter_present_mask);
0290 }
0291 
0292 static inline bool cluster_pmu_counter_has_overflowed(u32 ovsr, u32 idx)
0293 {
0294     return !!(ovsr & idx_to_reg_bit(idx));
0295 }
0296 
0297 static void l2_cache_event_update(struct perf_event *event)
0298 {
0299     struct hw_perf_event *hwc = &event->hw;
0300     u64 delta, prev, now;
0301     u32 idx = hwc->idx;
0302 
0303     do {
0304         prev = local64_read(&hwc->prev_count);
0305         now = cluster_pmu_counter_get_value(idx);
0306     } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
0307 
0308     /*
0309      * The cycle counter is 64-bit, but all other counters are
0310      * 32-bit, and we must handle 32-bit overflow explicitly.
0311      */
0312     delta = now - prev;
0313     if (idx != l2_cycle_ctr_idx)
0314         delta &= 0xffffffff;
0315 
0316     local64_add(delta, &event->count);
0317 }
0318 
0319 static void l2_cache_cluster_set_period(struct cluster_pmu *cluster,
0320                        struct hw_perf_event *hwc)
0321 {
0322     u32 idx = hwc->idx;
0323     u64 new;
0324 
0325     /*
0326      * We limit the max period to half the max counter value so
0327      * that even in the case of extreme interrupt latency the
0328      * counter will (hopefully) not wrap past its initial value.
0329      */
0330     if (idx == l2_cycle_ctr_idx)
0331         new = L2_CYCLE_COUNTER_RELOAD;
0332     else
0333         new = L2_COUNTER_RELOAD;
0334 
0335     local64_set(&hwc->prev_count, new);
0336     cluster_pmu_counter_set_value(idx, new);
0337 }
0338 
0339 static int l2_cache_get_event_idx(struct cluster_pmu *cluster,
0340                    struct perf_event *event)
0341 {
0342     struct hw_perf_event *hwc = &event->hw;
0343     int idx;
0344     int num_ctrs = cluster->l2cache_pmu->num_counters - 1;
0345     unsigned int group;
0346 
0347     if (hwc->config_base == L2CYCLE_CTR_RAW_CODE) {
0348         if (test_and_set_bit(l2_cycle_ctr_idx, cluster->used_counters))
0349             return -EAGAIN;
0350 
0351         return l2_cycle_ctr_idx;
0352     }
0353 
0354     idx = find_first_zero_bit(cluster->used_counters, num_ctrs);
0355     if (idx == num_ctrs)
0356         /* The counters are all in use. */
0357         return -EAGAIN;
0358 
0359     /*
0360      * Check for column exclusion: event column already in use by another
0361      * event. This is for events which are not in the same group.
0362      * Conflicting events in the same group are detected in event_init.
0363      */
0364     group = L2_EVT_GROUP(hwc->config_base);
0365     if (test_bit(group, cluster->used_groups))
0366         return -EAGAIN;
0367 
0368     set_bit(idx, cluster->used_counters);
0369     set_bit(group, cluster->used_groups);
0370 
0371     return idx;
0372 }
0373 
0374 static void l2_cache_clear_event_idx(struct cluster_pmu *cluster,
0375                       struct perf_event *event)
0376 {
0377     struct hw_perf_event *hwc = &event->hw;
0378     int idx = hwc->idx;
0379 
0380     clear_bit(idx, cluster->used_counters);
0381     if (hwc->config_base != L2CYCLE_CTR_RAW_CODE)
0382         clear_bit(L2_EVT_GROUP(hwc->config_base), cluster->used_groups);
0383 }
0384 
0385 static irqreturn_t l2_cache_handle_irq(int irq_num, void *data)
0386 {
0387     struct cluster_pmu *cluster = data;
0388     int num_counters = cluster->l2cache_pmu->num_counters;
0389     u32 ovsr;
0390     int idx;
0391 
0392     ovsr = cluster_pmu_getreset_ovsr();
0393     if (!cluster_pmu_has_overflowed(ovsr))
0394         return IRQ_NONE;
0395 
0396     for_each_set_bit(idx, cluster->used_counters, num_counters) {
0397         struct perf_event *event = cluster->events[idx];
0398         struct hw_perf_event *hwc;
0399 
0400         if (WARN_ON_ONCE(!event))
0401             continue;
0402 
0403         if (!cluster_pmu_counter_has_overflowed(ovsr, idx))
0404             continue;
0405 
0406         l2_cache_event_update(event);
0407         hwc = &event->hw;
0408 
0409         l2_cache_cluster_set_period(cluster, hwc);
0410     }
0411 
0412     return IRQ_HANDLED;
0413 }
0414 
0415 /*
0416  * Implementation of abstract pmu functionality required by
0417  * the core perf events code.
0418  */
0419 
0420 static void l2_cache_pmu_enable(struct pmu *pmu)
0421 {
0422     /*
0423      * Although there is only one PMU (per socket) controlling multiple
0424      * physical PMUs (per cluster), because we do not support per-task mode
0425      * each event is associated with a CPU. Each event has pmu_enable
0426      * called on its CPU, so here it is only necessary to enable the
0427      * counters for the current CPU.
0428      */
0429 
0430     cluster_pmu_enable();
0431 }
0432 
0433 static void l2_cache_pmu_disable(struct pmu *pmu)
0434 {
0435     cluster_pmu_disable();
0436 }
0437 
0438 static int l2_cache_event_init(struct perf_event *event)
0439 {
0440     struct hw_perf_event *hwc = &event->hw;
0441     struct cluster_pmu *cluster;
0442     struct perf_event *sibling;
0443     struct l2cache_pmu *l2cache_pmu;
0444 
0445     if (event->attr.type != event->pmu->type)
0446         return -ENOENT;
0447 
0448     l2cache_pmu = to_l2cache_pmu(event->pmu);
0449 
0450     if (hwc->sample_period) {
0451         dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
0452                     "Sampling not supported\n");
0453         return -EOPNOTSUPP;
0454     }
0455 
0456     if (event->cpu < 0) {
0457         dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
0458                     "Per-task mode not supported\n");
0459         return -EOPNOTSUPP;
0460     }
0461 
0462     if (((L2_EVT_GROUP(event->attr.config) > L2_EVT_GROUP_MAX) ||
0463          ((event->attr.config & ~L2_EVT_MASK) != 0)) &&
0464         (event->attr.config != L2CYCLE_CTR_RAW_CODE)) {
0465         dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
0466                     "Invalid config %llx\n",
0467                     event->attr.config);
0468         return -EINVAL;
0469     }
0470 
0471     /* Don't allow groups with mixed PMUs, except for s/w events */
0472     if (event->group_leader->pmu != event->pmu &&
0473         !is_software_event(event->group_leader)) {
0474         dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
0475              "Can't create mixed PMU group\n");
0476         return -EINVAL;
0477     }
0478 
0479     for_each_sibling_event(sibling, event->group_leader) {
0480         if (sibling->pmu != event->pmu &&
0481             !is_software_event(sibling)) {
0482             dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
0483                  "Can't create mixed PMU group\n");
0484             return -EINVAL;
0485         }
0486     }
0487 
0488     cluster = get_cluster_pmu(l2cache_pmu, event->cpu);
0489     if (!cluster) {
0490         /* CPU has not been initialised */
0491         dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
0492             "CPU%d not associated with L2 cluster\n", event->cpu);
0493         return -EINVAL;
0494     }
0495 
0496     /* Ensure all events in a group are on the same cpu */
0497     if ((event->group_leader != event) &&
0498         (cluster->on_cpu != event->group_leader->cpu)) {
0499         dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
0500              "Can't create group on CPUs %d and %d",
0501              event->cpu, event->group_leader->cpu);
0502         return -EINVAL;
0503     }
0504 
0505     if ((event != event->group_leader) &&
0506         !is_software_event(event->group_leader) &&
0507         (L2_EVT_GROUP(event->group_leader->attr.config) ==
0508          L2_EVT_GROUP(event->attr.config))) {
0509         dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
0510              "Column exclusion: conflicting events %llx %llx\n",
0511                event->group_leader->attr.config,
0512                event->attr.config);
0513         return -EINVAL;
0514     }
0515 
0516     for_each_sibling_event(sibling, event->group_leader) {
0517         if ((sibling != event) &&
0518             !is_software_event(sibling) &&
0519             (L2_EVT_GROUP(sibling->attr.config) ==
0520              L2_EVT_GROUP(event->attr.config))) {
0521             dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
0522                  "Column exclusion: conflicting events %llx %llx\n",
0523                         sibling->attr.config,
0524                         event->attr.config);
0525             return -EINVAL;
0526         }
0527     }
0528 
0529     hwc->idx = -1;
0530     hwc->config_base = event->attr.config;
0531 
0532     /*
0533      * Ensure all events are on the same cpu so all events are in the
0534      * same cpu context, to avoid races on pmu_enable etc.
0535      */
0536     event->cpu = cluster->on_cpu;
0537 
0538     return 0;
0539 }
0540 
0541 static void l2_cache_event_start(struct perf_event *event, int flags)
0542 {
0543     struct cluster_pmu *cluster;
0544     struct hw_perf_event *hwc = &event->hw;
0545     int idx = hwc->idx;
0546     u32 config;
0547     u32 event_cc, event_group;
0548 
0549     hwc->state = 0;
0550 
0551     cluster = get_cluster_pmu(to_l2cache_pmu(event->pmu), event->cpu);
0552 
0553     l2_cache_cluster_set_period(cluster, hwc);
0554 
0555     if (hwc->config_base == L2CYCLE_CTR_RAW_CODE) {
0556         cluster_pmu_set_evccntcr(0);
0557     } else {
0558         config = hwc->config_base;
0559         event_cc    = L2_EVT_CODE(config);
0560         event_group = L2_EVT_GROUP(config);
0561 
0562         cluster_pmu_set_evcntcr(idx, 0);
0563         cluster_pmu_set_evtyper(idx, event_group);
0564         cluster_pmu_set_resr(cluster, event_group, event_cc);
0565         cluster_pmu_set_evfilter_sys_mode(idx);
0566     }
0567 
0568     cluster_pmu_counter_enable_interrupt(idx);
0569     cluster_pmu_counter_enable(idx);
0570 }
0571 
0572 static void l2_cache_event_stop(struct perf_event *event, int flags)
0573 {
0574     struct hw_perf_event *hwc = &event->hw;
0575     int idx = hwc->idx;
0576 
0577     if (hwc->state & PERF_HES_STOPPED)
0578         return;
0579 
0580     cluster_pmu_counter_disable_interrupt(idx);
0581     cluster_pmu_counter_disable(idx);
0582 
0583     if (flags & PERF_EF_UPDATE)
0584         l2_cache_event_update(event);
0585     hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
0586 }
0587 
0588 static int l2_cache_event_add(struct perf_event *event, int flags)
0589 {
0590     struct hw_perf_event *hwc = &event->hw;
0591     int idx;
0592     int err = 0;
0593     struct cluster_pmu *cluster;
0594 
0595     cluster = get_cluster_pmu(to_l2cache_pmu(event->pmu), event->cpu);
0596 
0597     idx = l2_cache_get_event_idx(cluster, event);
0598     if (idx < 0)
0599         return idx;
0600 
0601     hwc->idx = idx;
0602     hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
0603     cluster->events[idx] = event;
0604     local64_set(&hwc->prev_count, 0);
0605 
0606     if (flags & PERF_EF_START)
0607         l2_cache_event_start(event, flags);
0608 
0609     /* Propagate changes to the userspace mapping. */
0610     perf_event_update_userpage(event);
0611 
0612     return err;
0613 }
0614 
0615 static void l2_cache_event_del(struct perf_event *event, int flags)
0616 {
0617     struct hw_perf_event *hwc = &event->hw;
0618     struct cluster_pmu *cluster;
0619     int idx = hwc->idx;
0620 
0621     cluster = get_cluster_pmu(to_l2cache_pmu(event->pmu), event->cpu);
0622 
0623     l2_cache_event_stop(event, flags | PERF_EF_UPDATE);
0624     cluster->events[idx] = NULL;
0625     l2_cache_clear_event_idx(cluster, event);
0626 
0627     perf_event_update_userpage(event);
0628 }
0629 
0630 static void l2_cache_event_read(struct perf_event *event)
0631 {
0632     l2_cache_event_update(event);
0633 }
0634 
0635 static ssize_t l2_cache_pmu_cpumask_show(struct device *dev,
0636                      struct device_attribute *attr,
0637                      char *buf)
0638 {
0639     struct l2cache_pmu *l2cache_pmu = to_l2cache_pmu(dev_get_drvdata(dev));
0640 
0641     return cpumap_print_to_pagebuf(true, buf, &l2cache_pmu->cpumask);
0642 }
0643 
0644 static struct device_attribute l2_cache_pmu_cpumask_attr =
0645         __ATTR(cpumask, S_IRUGO, l2_cache_pmu_cpumask_show, NULL);
0646 
0647 static struct attribute *l2_cache_pmu_cpumask_attrs[] = {
0648     &l2_cache_pmu_cpumask_attr.attr,
0649     NULL,
0650 };
0651 
0652 static const struct attribute_group l2_cache_pmu_cpumask_group = {
0653     .attrs = l2_cache_pmu_cpumask_attrs,
0654 };
0655 
0656 /* CCG format for perf RAW codes. */
0657 PMU_FORMAT_ATTR(l2_code,   "config:4-11");
0658 PMU_FORMAT_ATTR(l2_group,  "config:0-3");
0659 PMU_FORMAT_ATTR(event,     "config:0-11");
0660 
0661 static struct attribute *l2_cache_pmu_formats[] = {
0662     &format_attr_l2_code.attr,
0663     &format_attr_l2_group.attr,
0664     &format_attr_event.attr,
0665     NULL,
0666 };
0667 
0668 static const struct attribute_group l2_cache_pmu_format_group = {
0669     .name = "format",
0670     .attrs = l2_cache_pmu_formats,
0671 };
0672 
0673 static ssize_t l2cache_pmu_event_show(struct device *dev,
0674                       struct device_attribute *attr, char *page)
0675 {
0676     struct perf_pmu_events_attr *pmu_attr;
0677 
0678     pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
0679     return sysfs_emit(page, "event=0x%02llx\n", pmu_attr->id);
0680 }
0681 
0682 #define L2CACHE_EVENT_ATTR(_name, _id)              \
0683     PMU_EVENT_ATTR_ID(_name, l2cache_pmu_event_show, _id)
0684 
0685 static struct attribute *l2_cache_pmu_events[] = {
0686     L2CACHE_EVENT_ATTR(cycles, L2_EVENT_CYCLES),
0687     L2CACHE_EVENT_ATTR(dcache-ops, L2_EVENT_DCACHE_OPS),
0688     L2CACHE_EVENT_ATTR(icache-ops, L2_EVENT_ICACHE_OPS),
0689     L2CACHE_EVENT_ATTR(tlbi, L2_EVENT_TLBI),
0690     L2CACHE_EVENT_ATTR(barriers, L2_EVENT_BARRIERS),
0691     L2CACHE_EVENT_ATTR(total-reads, L2_EVENT_TOTAL_READS),
0692     L2CACHE_EVENT_ATTR(total-writes, L2_EVENT_TOTAL_WRITES),
0693     L2CACHE_EVENT_ATTR(total-requests, L2_EVENT_TOTAL_REQUESTS),
0694     L2CACHE_EVENT_ATTR(ldrex, L2_EVENT_LDREX),
0695     L2CACHE_EVENT_ATTR(strex, L2_EVENT_STREX),
0696     L2CACHE_EVENT_ATTR(clrex, L2_EVENT_CLREX),
0697     NULL
0698 };
0699 
0700 static const struct attribute_group l2_cache_pmu_events_group = {
0701     .name = "events",
0702     .attrs = l2_cache_pmu_events,
0703 };
0704 
0705 static const struct attribute_group *l2_cache_pmu_attr_grps[] = {
0706     &l2_cache_pmu_format_group,
0707     &l2_cache_pmu_cpumask_group,
0708     &l2_cache_pmu_events_group,
0709     NULL,
0710 };
0711 
0712 /*
0713  * Generic device handlers
0714  */
0715 
0716 static const struct acpi_device_id l2_cache_pmu_acpi_match[] = {
0717     { "QCOM8130", },
0718     { }
0719 };
0720 
0721 static int get_num_counters(void)
0722 {
0723     int val;
0724 
0725     val = kryo_l2_get_indirect_reg(L2PMCR);
0726 
0727     /*
0728      * Read number of counters from L2PMCR and add 1
0729      * for the cycle counter.
0730      */
0731     return ((val >> L2PMCR_NUM_EV_SHIFT) & L2PMCR_NUM_EV_MASK) + 1;
0732 }
0733 
0734 static struct cluster_pmu *l2_cache_associate_cpu_with_cluster(
0735     struct l2cache_pmu *l2cache_pmu, int cpu)
0736 {
0737     u64 mpidr;
0738     int cpu_cluster_id;
0739     struct cluster_pmu *cluster;
0740 
0741     /*
0742      * This assumes that the cluster_id is in MPIDR[aff1] for
0743      * single-threaded cores, and MPIDR[aff2] for multi-threaded
0744      * cores. This logic will have to be updated if this changes.
0745      */
0746     mpidr = read_cpuid_mpidr();
0747     if (mpidr & MPIDR_MT_BITMASK)
0748         cpu_cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
0749     else
0750         cpu_cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
0751 
0752     list_for_each_entry(cluster, &l2cache_pmu->clusters, next) {
0753         if (cluster->cluster_id != cpu_cluster_id)
0754             continue;
0755 
0756         dev_info(&l2cache_pmu->pdev->dev,
0757              "CPU%d associated with cluster %d\n", cpu,
0758              cluster->cluster_id);
0759         cpumask_set_cpu(cpu, &cluster->cluster_cpus);
0760         *per_cpu_ptr(l2cache_pmu->pmu_cluster, cpu) = cluster;
0761         return cluster;
0762     }
0763 
0764     return NULL;
0765 }
0766 
0767 static int l2cache_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
0768 {
0769     struct cluster_pmu *cluster;
0770     struct l2cache_pmu *l2cache_pmu;
0771 
0772     l2cache_pmu = hlist_entry_safe(node, struct l2cache_pmu, node);
0773     cluster = get_cluster_pmu(l2cache_pmu, cpu);
0774     if (!cluster) {
0775         /* First time this CPU has come online */
0776         cluster = l2_cache_associate_cpu_with_cluster(l2cache_pmu, cpu);
0777         if (!cluster) {
0778             /* Only if broken firmware doesn't list every cluster */
0779             WARN_ONCE(1, "No L2 cache cluster for CPU%d\n", cpu);
0780             return 0;
0781         }
0782     }
0783 
0784     /* If another CPU is managing this cluster, we're done */
0785     if (cluster->on_cpu != -1)
0786         return 0;
0787 
0788     /*
0789      * All CPUs on this cluster were down, use this one.
0790      * Reset to put it into sane state.
0791      */
0792     cluster->on_cpu = cpu;
0793     cpumask_set_cpu(cpu, &l2cache_pmu->cpumask);
0794     cluster_pmu_reset();
0795 
0796     WARN_ON(irq_set_affinity(cluster->irq, cpumask_of(cpu)));
0797     enable_irq(cluster->irq);
0798 
0799     return 0;
0800 }
0801 
0802 static int l2cache_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
0803 {
0804     struct cluster_pmu *cluster;
0805     struct l2cache_pmu *l2cache_pmu;
0806     cpumask_t cluster_online_cpus;
0807     unsigned int target;
0808 
0809     l2cache_pmu = hlist_entry_safe(node, struct l2cache_pmu, node);
0810     cluster = get_cluster_pmu(l2cache_pmu, cpu);
0811     if (!cluster)
0812         return 0;
0813 
0814     /* If this CPU is not managing the cluster, we're done */
0815     if (cluster->on_cpu != cpu)
0816         return 0;
0817 
0818     /* Give up ownership of cluster */
0819     cpumask_clear_cpu(cpu, &l2cache_pmu->cpumask);
0820     cluster->on_cpu = -1;
0821 
0822     /* Any other CPU for this cluster which is still online */
0823     cpumask_and(&cluster_online_cpus, &cluster->cluster_cpus,
0824             cpu_online_mask);
0825     target = cpumask_any_but(&cluster_online_cpus, cpu);
0826     if (target >= nr_cpu_ids) {
0827         disable_irq(cluster->irq);
0828         return 0;
0829     }
0830 
0831     perf_pmu_migrate_context(&l2cache_pmu->pmu, cpu, target);
0832     cluster->on_cpu = target;
0833     cpumask_set_cpu(target, &l2cache_pmu->cpumask);
0834     WARN_ON(irq_set_affinity(cluster->irq, cpumask_of(target)));
0835 
0836     return 0;
0837 }
0838 
0839 static int l2_cache_pmu_probe_cluster(struct device *dev, void *data)
0840 {
0841     struct platform_device *pdev = to_platform_device(dev->parent);
0842     struct platform_device *sdev = to_platform_device(dev);
0843     struct acpi_device *adev = ACPI_COMPANION(dev);
0844     struct l2cache_pmu *l2cache_pmu = data;
0845     struct cluster_pmu *cluster;
0846     unsigned long fw_cluster_id;
0847     int err;
0848     int irq;
0849 
0850     if (!adev || kstrtoul(adev->pnp.unique_id, 10, &fw_cluster_id) < 0) {
0851         dev_err(&pdev->dev, "unable to read ACPI uid\n");
0852         return -ENODEV;
0853     }
0854 
0855     cluster = devm_kzalloc(&pdev->dev, sizeof(*cluster), GFP_KERNEL);
0856     if (!cluster)
0857         return -ENOMEM;
0858 
0859     INIT_LIST_HEAD(&cluster->next);
0860     list_add(&cluster->next, &l2cache_pmu->clusters);
0861     cluster->cluster_id = fw_cluster_id;
0862 
0863     irq = platform_get_irq(sdev, 0);
0864     if (irq < 0)
0865         return irq;
0866     cluster->irq = irq;
0867 
0868     cluster->l2cache_pmu = l2cache_pmu;
0869     cluster->on_cpu = -1;
0870 
0871     err = devm_request_irq(&pdev->dev, irq, l2_cache_handle_irq,
0872                    IRQF_NOBALANCING | IRQF_NO_THREAD |
0873                    IRQF_NO_AUTOEN,
0874                    "l2-cache-pmu", cluster);
0875     if (err) {
0876         dev_err(&pdev->dev,
0877             "Unable to request IRQ%d for L2 PMU counters\n", irq);
0878         return err;
0879     }
0880 
0881     dev_info(&pdev->dev,
0882         "Registered L2 cache PMU cluster %ld\n", fw_cluster_id);
0883 
0884     spin_lock_init(&cluster->pmu_lock);
0885 
0886     l2cache_pmu->num_pmus++;
0887 
0888     return 0;
0889 }
0890 
0891 static int l2_cache_pmu_probe(struct platform_device *pdev)
0892 {
0893     int err;
0894     struct l2cache_pmu *l2cache_pmu;
0895 
0896     l2cache_pmu =
0897         devm_kzalloc(&pdev->dev, sizeof(*l2cache_pmu), GFP_KERNEL);
0898     if (!l2cache_pmu)
0899         return -ENOMEM;
0900 
0901     INIT_LIST_HEAD(&l2cache_pmu->clusters);
0902 
0903     platform_set_drvdata(pdev, l2cache_pmu);
0904     l2cache_pmu->pmu = (struct pmu) {
0905         /* suffix is instance id for future use with multiple sockets */
0906         .name       = "l2cache_0",
0907         .task_ctx_nr    = perf_invalid_context,
0908         .pmu_enable = l2_cache_pmu_enable,
0909         .pmu_disable    = l2_cache_pmu_disable,
0910         .event_init = l2_cache_event_init,
0911         .add        = l2_cache_event_add,
0912         .del        = l2_cache_event_del,
0913         .start      = l2_cache_event_start,
0914         .stop       = l2_cache_event_stop,
0915         .read       = l2_cache_event_read,
0916         .attr_groups    = l2_cache_pmu_attr_grps,
0917         .capabilities   = PERF_PMU_CAP_NO_EXCLUDE,
0918     };
0919 
0920     l2cache_pmu->num_counters = get_num_counters();
0921     l2cache_pmu->pdev = pdev;
0922     l2cache_pmu->pmu_cluster = devm_alloc_percpu(&pdev->dev,
0923                              struct cluster_pmu *);
0924     if (!l2cache_pmu->pmu_cluster)
0925         return -ENOMEM;
0926 
0927     l2_cycle_ctr_idx = l2cache_pmu->num_counters - 1;
0928     l2_counter_present_mask = GENMASK(l2cache_pmu->num_counters - 2, 0) |
0929         BIT(L2CYCLE_CTR_BIT);
0930 
0931     cpumask_clear(&l2cache_pmu->cpumask);
0932 
0933     /* Read cluster info and initialize each cluster */
0934     err = device_for_each_child(&pdev->dev, l2cache_pmu,
0935                     l2_cache_pmu_probe_cluster);
0936     if (err)
0937         return err;
0938 
0939     if (l2cache_pmu->num_pmus == 0) {
0940         dev_err(&pdev->dev, "No hardware L2 cache PMUs found\n");
0941         return -ENODEV;
0942     }
0943 
0944     err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
0945                        &l2cache_pmu->node);
0946     if (err) {
0947         dev_err(&pdev->dev, "Error %d registering hotplug", err);
0948         return err;
0949     }
0950 
0951     err = perf_pmu_register(&l2cache_pmu->pmu, l2cache_pmu->pmu.name, -1);
0952     if (err) {
0953         dev_err(&pdev->dev, "Error %d registering L2 cache PMU\n", err);
0954         goto out_unregister;
0955     }
0956 
0957     dev_info(&pdev->dev, "Registered L2 cache PMU using %d HW PMUs\n",
0958          l2cache_pmu->num_pmus);
0959 
0960     return err;
0961 
0962 out_unregister:
0963     cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
0964                     &l2cache_pmu->node);
0965     return err;
0966 }
0967 
0968 static int l2_cache_pmu_remove(struct platform_device *pdev)
0969 {
0970     struct l2cache_pmu *l2cache_pmu =
0971         to_l2cache_pmu(platform_get_drvdata(pdev));
0972 
0973     perf_pmu_unregister(&l2cache_pmu->pmu);
0974     cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
0975                     &l2cache_pmu->node);
0976     return 0;
0977 }
0978 
0979 static struct platform_driver l2_cache_pmu_driver = {
0980     .driver = {
0981         .name = "qcom-l2cache-pmu",
0982         .acpi_match_table = ACPI_PTR(l2_cache_pmu_acpi_match),
0983         .suppress_bind_attrs = true,
0984     },
0985     .probe = l2_cache_pmu_probe,
0986     .remove = l2_cache_pmu_remove,
0987 };
0988 
0989 static int __init register_l2_cache_pmu_driver(void)
0990 {
0991     int err;
0992 
0993     err = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
0994                       "AP_PERF_ARM_QCOM_L2_ONLINE",
0995                       l2cache_pmu_online_cpu,
0996                       l2cache_pmu_offline_cpu);
0997     if (err)
0998         return err;
0999 
1000     return platform_driver_register(&l2_cache_pmu_driver);
1001 }
1002 device_initcall(register_l2_cache_pmu_driver);