Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ARM DynamIQ Shared Unit (DSU) PMU driver
0004  *
0005  * Copyright (C) ARM Limited, 2017.
0006  *
0007  * Based on ARM CCI-PMU, ARMv8 PMU-v3 drivers.
0008  */
0009 
0010 #define PMUNAME     "arm_dsu"
0011 #define DRVNAME     PMUNAME "_pmu"
0012 #define pr_fmt(fmt) DRVNAME ": " fmt
0013 
0014 #include <linux/acpi.h>
0015 #include <linux/bitmap.h>
0016 #include <linux/bitops.h>
0017 #include <linux/bug.h>
0018 #include <linux/cpumask.h>
0019 #include <linux/device.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/kernel.h>
0022 #include <linux/module.h>
0023 #include <linux/of_device.h>
0024 #include <linux/perf_event.h>
0025 #include <linux/platform_device.h>
0026 #include <linux/spinlock.h>
0027 #include <linux/smp.h>
0028 #include <linux/sysfs.h>
0029 #include <linux/types.h>
0030 
0031 #include <asm/arm_dsu_pmu.h>
0032 #include <asm/local64.h>
0033 
0034 /* PMU event codes */
0035 #define DSU_PMU_EVT_CYCLES      0x11
0036 #define DSU_PMU_EVT_CHAIN       0x1e
0037 
0038 #define DSU_PMU_MAX_COMMON_EVENTS   0x40
0039 
0040 #define DSU_PMU_MAX_HW_CNTRS        32
0041 #define DSU_PMU_HW_COUNTER_MASK     (DSU_PMU_MAX_HW_CNTRS - 1)
0042 
0043 #define CLUSTERPMCR_E           BIT(0)
0044 #define CLUSTERPMCR_P           BIT(1)
0045 #define CLUSTERPMCR_C           BIT(2)
0046 #define CLUSTERPMCR_N_SHIFT     11
0047 #define CLUSTERPMCR_N_MASK      0x1f
0048 #define CLUSTERPMCR_IDCODE_SHIFT    16
0049 #define CLUSTERPMCR_IDCODE_MASK     0xff
0050 #define CLUSTERPMCR_IMP_SHIFT       24
0051 #define CLUSTERPMCR_IMP_MASK        0xff
0052 #define CLUSTERPMCR_RES_MASK        0x7e8
0053 #define CLUSTERPMCR_RES_VAL     0x40
0054 
0055 #define DSU_ACTIVE_CPU_MASK     0x0
0056 #define DSU_ASSOCIATED_CPU_MASK     0x1
0057 
0058 /*
0059  * We use the index of the counters as they appear in the counter
0060  * bit maps in the PMU registers (e.g CLUSTERPMSELR).
0061  * i.e,
0062  *  counter 0   - Bit 0
0063  *  counter 1   - Bit 1
0064  *  ...
0065  *  Cycle counter   - Bit 31
0066  */
0067 #define DSU_PMU_IDX_CYCLE_COUNTER   31
0068 
0069 /* All event counters are 32bit, with a 64bit Cycle counter */
0070 #define DSU_PMU_COUNTER_WIDTH(idx)  \
0071     (((idx) == DSU_PMU_IDX_CYCLE_COUNTER) ? 64 : 32)
0072 
0073 #define DSU_PMU_COUNTER_MASK(idx)   \
0074     GENMASK_ULL((DSU_PMU_COUNTER_WIDTH((idx)) - 1), 0)
0075 
0076 #define DSU_EXT_ATTR(_name, _func, _config)     \
0077     (&((struct dev_ext_attribute[]) {               \
0078         {                           \
0079             .attr = __ATTR(_name, 0444, _func, NULL),   \
0080             .var = (void *)_config              \
0081         }                           \
0082     })[0].attr.attr)
0083 
0084 #define DSU_EVENT_ATTR(_name, _config)      \
0085     DSU_EXT_ATTR(_name, dsu_pmu_sysfs_event_show, (unsigned long)_config)
0086 
0087 #define DSU_FORMAT_ATTR(_name, _config)     \
0088     DSU_EXT_ATTR(_name, dsu_pmu_sysfs_format_show, (char *)_config)
0089 
0090 #define DSU_CPUMASK_ATTR(_name, _config)    \
0091     DSU_EXT_ATTR(_name, dsu_pmu_cpumask_show, (unsigned long)_config)
0092 
0093 struct dsu_hw_events {
0094     DECLARE_BITMAP(used_mask, DSU_PMU_MAX_HW_CNTRS);
0095     struct perf_event   *events[DSU_PMU_MAX_HW_CNTRS];
0096 };
0097 
0098 /*
0099  * struct dsu_pmu   - DSU PMU descriptor
0100  *
0101  * @pmu_lock        : Protects accesses to DSU PMU register from normal vs
0102  *            interrupt handler contexts.
0103  * @hw_events       : Holds the event counter state.
0104  * @associated_cpus : CPUs attached to the DSU.
0105  * @active_cpu      : CPU to which the PMU is bound for accesses.
0106  * @cpuhp_node      : Node for CPU hotplug notifier link.
0107  * @num_counters    : Number of event counters implemented by the PMU,
0108  *            excluding the cycle counter.
0109  * @irq         : Interrupt line for counter overflow.
0110  * @cpmceid_bitmap  : Bitmap for the availability of architected common
0111  *            events (event_code < 0x40).
0112  */
0113 struct dsu_pmu {
0114     struct pmu          pmu;
0115     struct device           *dev;
0116     raw_spinlock_t          pmu_lock;
0117     struct dsu_hw_events        hw_events;
0118     cpumask_t           associated_cpus;
0119     cpumask_t           active_cpu;
0120     struct hlist_node       cpuhp_node;
0121     s8              num_counters;
0122     int             irq;
0123     DECLARE_BITMAP(cpmceid_bitmap, DSU_PMU_MAX_COMMON_EVENTS);
0124 };
0125 
0126 static unsigned long dsu_pmu_cpuhp_state;
0127 
0128 static inline struct dsu_pmu *to_dsu_pmu(struct pmu *pmu)
0129 {
0130     return container_of(pmu, struct dsu_pmu, pmu);
0131 }
0132 
0133 static ssize_t dsu_pmu_sysfs_event_show(struct device *dev,
0134                     struct device_attribute *attr,
0135                     char *buf)
0136 {
0137     struct dev_ext_attribute *eattr = container_of(attr,
0138                     struct dev_ext_attribute, attr);
0139     return sysfs_emit(buf, "event=0x%lx\n", (unsigned long)eattr->var);
0140 }
0141 
0142 static ssize_t dsu_pmu_sysfs_format_show(struct device *dev,
0143                      struct device_attribute *attr,
0144                      char *buf)
0145 {
0146     struct dev_ext_attribute *eattr = container_of(attr,
0147                     struct dev_ext_attribute, attr);
0148     return sysfs_emit(buf, "%s\n", (char *)eattr->var);
0149 }
0150 
0151 static ssize_t dsu_pmu_cpumask_show(struct device *dev,
0152                     struct device_attribute *attr,
0153                     char *buf)
0154 {
0155     struct pmu *pmu = dev_get_drvdata(dev);
0156     struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu);
0157     struct dev_ext_attribute *eattr = container_of(attr,
0158                     struct dev_ext_attribute, attr);
0159     unsigned long mask_id = (unsigned long)eattr->var;
0160     const cpumask_t *cpumask;
0161 
0162     switch (mask_id) {
0163     case DSU_ACTIVE_CPU_MASK:
0164         cpumask = &dsu_pmu->active_cpu;
0165         break;
0166     case DSU_ASSOCIATED_CPU_MASK:
0167         cpumask = &dsu_pmu->associated_cpus;
0168         break;
0169     default:
0170         return 0;
0171     }
0172     return cpumap_print_to_pagebuf(true, buf, cpumask);
0173 }
0174 
0175 static struct attribute *dsu_pmu_format_attrs[] = {
0176     DSU_FORMAT_ATTR(event, "config:0-31"),
0177     NULL,
0178 };
0179 
0180 static const struct attribute_group dsu_pmu_format_attr_group = {
0181     .name = "format",
0182     .attrs = dsu_pmu_format_attrs,
0183 };
0184 
0185 static struct attribute *dsu_pmu_event_attrs[] = {
0186     DSU_EVENT_ATTR(cycles, 0x11),
0187     DSU_EVENT_ATTR(bus_access, 0x19),
0188     DSU_EVENT_ATTR(memory_error, 0x1a),
0189     DSU_EVENT_ATTR(bus_cycles, 0x1d),
0190     DSU_EVENT_ATTR(l3d_cache_allocate, 0x29),
0191     DSU_EVENT_ATTR(l3d_cache_refill, 0x2a),
0192     DSU_EVENT_ATTR(l3d_cache, 0x2b),
0193     DSU_EVENT_ATTR(l3d_cache_wb, 0x2c),
0194     NULL,
0195 };
0196 
0197 static umode_t
0198 dsu_pmu_event_attr_is_visible(struct kobject *kobj, struct attribute *attr,
0199                 int unused)
0200 {
0201     struct pmu *pmu = dev_get_drvdata(kobj_to_dev(kobj));
0202     struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu);
0203     struct dev_ext_attribute *eattr = container_of(attr,
0204                     struct dev_ext_attribute, attr.attr);
0205     unsigned long evt = (unsigned long)eattr->var;
0206 
0207     return test_bit(evt, dsu_pmu->cpmceid_bitmap) ? attr->mode : 0;
0208 }
0209 
0210 static const struct attribute_group dsu_pmu_events_attr_group = {
0211     .name = "events",
0212     .attrs = dsu_pmu_event_attrs,
0213     .is_visible = dsu_pmu_event_attr_is_visible,
0214 };
0215 
0216 static struct attribute *dsu_pmu_cpumask_attrs[] = {
0217     DSU_CPUMASK_ATTR(cpumask, DSU_ACTIVE_CPU_MASK),
0218     DSU_CPUMASK_ATTR(associated_cpus, DSU_ASSOCIATED_CPU_MASK),
0219     NULL,
0220 };
0221 
0222 static const struct attribute_group dsu_pmu_cpumask_attr_group = {
0223     .attrs = dsu_pmu_cpumask_attrs,
0224 };
0225 
0226 static const struct attribute_group *dsu_pmu_attr_groups[] = {
0227     &dsu_pmu_cpumask_attr_group,
0228     &dsu_pmu_events_attr_group,
0229     &dsu_pmu_format_attr_group,
0230     NULL,
0231 };
0232 
0233 static int dsu_pmu_get_online_cpu_any_but(struct dsu_pmu *dsu_pmu, int cpu)
0234 {
0235     struct cpumask online_supported;
0236 
0237     cpumask_and(&online_supported,
0238              &dsu_pmu->associated_cpus, cpu_online_mask);
0239     return cpumask_any_but(&online_supported, cpu);
0240 }
0241 
0242 static inline bool dsu_pmu_counter_valid(struct dsu_pmu *dsu_pmu, u32 idx)
0243 {
0244     return (idx < dsu_pmu->num_counters) ||
0245            (idx == DSU_PMU_IDX_CYCLE_COUNTER);
0246 }
0247 
0248 static inline u64 dsu_pmu_read_counter(struct perf_event *event)
0249 {
0250     u64 val;
0251     unsigned long flags;
0252     struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
0253     int idx = event->hw.idx;
0254 
0255     if (WARN_ON(!cpumask_test_cpu(smp_processor_id(),
0256                  &dsu_pmu->associated_cpus)))
0257         return 0;
0258 
0259     if (!dsu_pmu_counter_valid(dsu_pmu, idx)) {
0260         dev_err(event->pmu->dev,
0261             "Trying reading invalid counter %d\n", idx);
0262         return 0;
0263     }
0264 
0265     raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
0266     if (idx == DSU_PMU_IDX_CYCLE_COUNTER)
0267         val = __dsu_pmu_read_pmccntr();
0268     else
0269         val = __dsu_pmu_read_counter(idx);
0270     raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
0271 
0272     return val;
0273 }
0274 
0275 static void dsu_pmu_write_counter(struct perf_event *event, u64 val)
0276 {
0277     unsigned long flags;
0278     struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
0279     int idx = event->hw.idx;
0280 
0281     if (WARN_ON(!cpumask_test_cpu(smp_processor_id(),
0282              &dsu_pmu->associated_cpus)))
0283         return;
0284 
0285     if (!dsu_pmu_counter_valid(dsu_pmu, idx)) {
0286         dev_err(event->pmu->dev,
0287             "writing to invalid counter %d\n", idx);
0288         return;
0289     }
0290 
0291     raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
0292     if (idx == DSU_PMU_IDX_CYCLE_COUNTER)
0293         __dsu_pmu_write_pmccntr(val);
0294     else
0295         __dsu_pmu_write_counter(idx, val);
0296     raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
0297 }
0298 
0299 static int dsu_pmu_get_event_idx(struct dsu_hw_events *hw_events,
0300                  struct perf_event *event)
0301 {
0302     int idx;
0303     unsigned long evtype = event->attr.config;
0304     struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
0305     unsigned long *used_mask = hw_events->used_mask;
0306 
0307     if (evtype == DSU_PMU_EVT_CYCLES) {
0308         if (test_and_set_bit(DSU_PMU_IDX_CYCLE_COUNTER, used_mask))
0309             return -EAGAIN;
0310         return DSU_PMU_IDX_CYCLE_COUNTER;
0311     }
0312 
0313     idx = find_first_zero_bit(used_mask, dsu_pmu->num_counters);
0314     if (idx >= dsu_pmu->num_counters)
0315         return -EAGAIN;
0316     set_bit(idx, hw_events->used_mask);
0317     return idx;
0318 }
0319 
0320 static void dsu_pmu_enable_counter(struct dsu_pmu *dsu_pmu, int idx)
0321 {
0322     __dsu_pmu_counter_interrupt_enable(idx);
0323     __dsu_pmu_enable_counter(idx);
0324 }
0325 
0326 static void dsu_pmu_disable_counter(struct dsu_pmu *dsu_pmu, int idx)
0327 {
0328     __dsu_pmu_disable_counter(idx);
0329     __dsu_pmu_counter_interrupt_disable(idx);
0330 }
0331 
0332 static inline void dsu_pmu_set_event(struct dsu_pmu *dsu_pmu,
0333                     struct perf_event *event)
0334 {
0335     int idx = event->hw.idx;
0336     unsigned long flags;
0337 
0338     if (!dsu_pmu_counter_valid(dsu_pmu, idx)) {
0339         dev_err(event->pmu->dev,
0340             "Trying to set invalid counter %d\n", idx);
0341         return;
0342     }
0343 
0344     raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
0345     __dsu_pmu_set_event(idx, event->hw.config_base);
0346     raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
0347 }
0348 
0349 static void dsu_pmu_event_update(struct perf_event *event)
0350 {
0351     struct hw_perf_event *hwc = &event->hw;
0352     u64 delta, prev_count, new_count;
0353 
0354     do {
0355         /* We may also be called from the irq handler */
0356         prev_count = local64_read(&hwc->prev_count);
0357         new_count = dsu_pmu_read_counter(event);
0358     } while (local64_cmpxchg(&hwc->prev_count, prev_count, new_count) !=
0359             prev_count);
0360     delta = (new_count - prev_count) & DSU_PMU_COUNTER_MASK(hwc->idx);
0361     local64_add(delta, &event->count);
0362 }
0363 
0364 static void dsu_pmu_read(struct perf_event *event)
0365 {
0366     dsu_pmu_event_update(event);
0367 }
0368 
0369 static inline u32 dsu_pmu_get_reset_overflow(void)
0370 {
0371     return __dsu_pmu_get_reset_overflow();
0372 }
0373 
0374 /**
0375  * dsu_pmu_set_event_period: Set the period for the counter.
0376  *
0377  * All DSU PMU event counters, except the cycle counter are 32bit
0378  * counters. To handle cases of extreme interrupt latency, we program
0379  * the counter with half of the max count for the counters.
0380  */
0381 static void dsu_pmu_set_event_period(struct perf_event *event)
0382 {
0383     int idx = event->hw.idx;
0384     u64 val = DSU_PMU_COUNTER_MASK(idx) >> 1;
0385 
0386     local64_set(&event->hw.prev_count, val);
0387     dsu_pmu_write_counter(event, val);
0388 }
0389 
0390 static irqreturn_t dsu_pmu_handle_irq(int irq_num, void *dev)
0391 {
0392     int i;
0393     bool handled = false;
0394     struct dsu_pmu *dsu_pmu = dev;
0395     struct dsu_hw_events *hw_events = &dsu_pmu->hw_events;
0396     unsigned long overflow;
0397 
0398     overflow = dsu_pmu_get_reset_overflow();
0399     if (!overflow)
0400         return IRQ_NONE;
0401 
0402     for_each_set_bit(i, &overflow, DSU_PMU_MAX_HW_CNTRS) {
0403         struct perf_event *event = hw_events->events[i];
0404 
0405         if (!event)
0406             continue;
0407         dsu_pmu_event_update(event);
0408         dsu_pmu_set_event_period(event);
0409         handled = true;
0410     }
0411 
0412     return IRQ_RETVAL(handled);
0413 }
0414 
0415 static void dsu_pmu_start(struct perf_event *event, int pmu_flags)
0416 {
0417     struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
0418 
0419     /* We always reprogram the counter */
0420     if (pmu_flags & PERF_EF_RELOAD)
0421         WARN_ON(!(event->hw.state & PERF_HES_UPTODATE));
0422     dsu_pmu_set_event_period(event);
0423     if (event->hw.idx != DSU_PMU_IDX_CYCLE_COUNTER)
0424         dsu_pmu_set_event(dsu_pmu, event);
0425     event->hw.state = 0;
0426     dsu_pmu_enable_counter(dsu_pmu, event->hw.idx);
0427 }
0428 
0429 static void dsu_pmu_stop(struct perf_event *event, int pmu_flags)
0430 {
0431     struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
0432 
0433     if (event->hw.state & PERF_HES_STOPPED)
0434         return;
0435     dsu_pmu_disable_counter(dsu_pmu, event->hw.idx);
0436     dsu_pmu_event_update(event);
0437     event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
0438 }
0439 
0440 static int dsu_pmu_add(struct perf_event *event, int flags)
0441 {
0442     struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
0443     struct dsu_hw_events *hw_events = &dsu_pmu->hw_events;
0444     struct hw_perf_event *hwc = &event->hw;
0445     int idx;
0446 
0447     if (WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(),
0448                        &dsu_pmu->associated_cpus)))
0449         return -ENOENT;
0450 
0451     idx = dsu_pmu_get_event_idx(hw_events, event);
0452     if (idx < 0)
0453         return idx;
0454 
0455     hwc->idx = idx;
0456     hw_events->events[idx] = event;
0457     hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
0458 
0459     if (flags & PERF_EF_START)
0460         dsu_pmu_start(event, PERF_EF_RELOAD);
0461 
0462     perf_event_update_userpage(event);
0463     return 0;
0464 }
0465 
0466 static void dsu_pmu_del(struct perf_event *event, int flags)
0467 {
0468     struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
0469     struct dsu_hw_events *hw_events = &dsu_pmu->hw_events;
0470     struct hw_perf_event *hwc = &event->hw;
0471     int idx = hwc->idx;
0472 
0473     dsu_pmu_stop(event, PERF_EF_UPDATE);
0474     hw_events->events[idx] = NULL;
0475     clear_bit(idx, hw_events->used_mask);
0476     perf_event_update_userpage(event);
0477 }
0478 
0479 static void dsu_pmu_enable(struct pmu *pmu)
0480 {
0481     u32 pmcr;
0482     unsigned long flags;
0483     struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu);
0484 
0485     /* If no counters are added, skip enabling the PMU */
0486     if (bitmap_empty(dsu_pmu->hw_events.used_mask, DSU_PMU_MAX_HW_CNTRS))
0487         return;
0488 
0489     raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
0490     pmcr = __dsu_pmu_read_pmcr();
0491     pmcr |= CLUSTERPMCR_E;
0492     __dsu_pmu_write_pmcr(pmcr);
0493     raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
0494 }
0495 
0496 static void dsu_pmu_disable(struct pmu *pmu)
0497 {
0498     u32 pmcr;
0499     unsigned long flags;
0500     struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu);
0501 
0502     raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
0503     pmcr = __dsu_pmu_read_pmcr();
0504     pmcr &= ~CLUSTERPMCR_E;
0505     __dsu_pmu_write_pmcr(pmcr);
0506     raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
0507 }
0508 
0509 static bool dsu_pmu_validate_event(struct pmu *pmu,
0510                   struct dsu_hw_events *hw_events,
0511                   struct perf_event *event)
0512 {
0513     if (is_software_event(event))
0514         return true;
0515     /* Reject groups spanning multiple HW PMUs. */
0516     if (event->pmu != pmu)
0517         return false;
0518     return dsu_pmu_get_event_idx(hw_events, event) >= 0;
0519 }
0520 
0521 /*
0522  * Make sure the group of events can be scheduled at once
0523  * on the PMU.
0524  */
0525 static bool dsu_pmu_validate_group(struct perf_event *event)
0526 {
0527     struct perf_event *sibling, *leader = event->group_leader;
0528     struct dsu_hw_events fake_hw;
0529 
0530     if (event->group_leader == event)
0531         return true;
0532 
0533     memset(fake_hw.used_mask, 0, sizeof(fake_hw.used_mask));
0534     if (!dsu_pmu_validate_event(event->pmu, &fake_hw, leader))
0535         return false;
0536     for_each_sibling_event(sibling, leader) {
0537         if (!dsu_pmu_validate_event(event->pmu, &fake_hw, sibling))
0538             return false;
0539     }
0540     return dsu_pmu_validate_event(event->pmu, &fake_hw, event);
0541 }
0542 
0543 static int dsu_pmu_event_init(struct perf_event *event)
0544 {
0545     struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
0546 
0547     if (event->attr.type != event->pmu->type)
0548         return -ENOENT;
0549 
0550     /* We don't support sampling */
0551     if (is_sampling_event(event)) {
0552         dev_dbg(dsu_pmu->pmu.dev, "Can't support sampling events\n");
0553         return -EOPNOTSUPP;
0554     }
0555 
0556     /* We cannot support task bound events */
0557     if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK) {
0558         dev_dbg(dsu_pmu->pmu.dev, "Can't support per-task counters\n");
0559         return -EINVAL;
0560     }
0561 
0562     if (has_branch_stack(event)) {
0563         dev_dbg(dsu_pmu->pmu.dev, "Can't support filtering\n");
0564         return -EINVAL;
0565     }
0566 
0567     if (!cpumask_test_cpu(event->cpu, &dsu_pmu->associated_cpus)) {
0568         dev_dbg(dsu_pmu->pmu.dev,
0569              "Requested cpu is not associated with the DSU\n");
0570         return -EINVAL;
0571     }
0572     /*
0573      * Choose the current active CPU to read the events. We don't want
0574      * to migrate the event contexts, irq handling etc to the requested
0575      * CPU. As long as the requested CPU is within the same DSU, we
0576      * are fine.
0577      */
0578     event->cpu = cpumask_first(&dsu_pmu->active_cpu);
0579     if (event->cpu >= nr_cpu_ids)
0580         return -EINVAL;
0581     if (!dsu_pmu_validate_group(event))
0582         return -EINVAL;
0583 
0584     event->hw.config_base = event->attr.config;
0585     return 0;
0586 }
0587 
0588 static struct dsu_pmu *dsu_pmu_alloc(struct platform_device *pdev)
0589 {
0590     struct dsu_pmu *dsu_pmu;
0591 
0592     dsu_pmu = devm_kzalloc(&pdev->dev, sizeof(*dsu_pmu), GFP_KERNEL);
0593     if (!dsu_pmu)
0594         return ERR_PTR(-ENOMEM);
0595 
0596     raw_spin_lock_init(&dsu_pmu->pmu_lock);
0597     /*
0598      * Initialise the number of counters to -1, until we probe
0599      * the real number on a connected CPU.
0600      */
0601     dsu_pmu->num_counters = -1;
0602     return dsu_pmu;
0603 }
0604 
0605 /**
0606  * dsu_pmu_dt_get_cpus: Get the list of CPUs in the cluster
0607  * from device tree.
0608  */
0609 static int dsu_pmu_dt_get_cpus(struct device *dev, cpumask_t *mask)
0610 {
0611     int i = 0, n, cpu;
0612     struct device_node *cpu_node;
0613 
0614     n = of_count_phandle_with_args(dev->of_node, "cpus", NULL);
0615     if (n <= 0)
0616         return -ENODEV;
0617     for (; i < n; i++) {
0618         cpu_node = of_parse_phandle(dev->of_node, "cpus", i);
0619         if (!cpu_node)
0620             break;
0621         cpu = of_cpu_node_to_id(cpu_node);
0622         of_node_put(cpu_node);
0623         /*
0624          * We have to ignore the failures here and continue scanning
0625          * the list to handle cases where the nr_cpus could be capped
0626          * in the running kernel.
0627          */
0628         if (cpu < 0)
0629             continue;
0630         cpumask_set_cpu(cpu, mask);
0631     }
0632     return 0;
0633 }
0634 
0635 /**
0636  * dsu_pmu_acpi_get_cpus: Get the list of CPUs in the cluster
0637  * from ACPI.
0638  */
0639 static int dsu_pmu_acpi_get_cpus(struct device *dev, cpumask_t *mask)
0640 {
0641 #ifdef CONFIG_ACPI
0642     int cpu;
0643 
0644     /*
0645      * A dsu pmu node is inside a cluster parent node along with cpu nodes.
0646      * We need to find out all cpus that have the same parent with this pmu.
0647      */
0648     for_each_possible_cpu(cpu) {
0649         struct acpi_device *acpi_dev;
0650         struct device *cpu_dev = get_cpu_device(cpu);
0651 
0652         if (!cpu_dev)
0653             continue;
0654 
0655         acpi_dev = ACPI_COMPANION(cpu_dev);
0656         if (acpi_dev &&
0657             acpi_dev->parent == ACPI_COMPANION(dev)->parent)
0658             cpumask_set_cpu(cpu, mask);
0659     }
0660 #endif
0661 
0662     return 0;
0663 }
0664 
0665 /*
0666  * dsu_pmu_probe_pmu: Probe the PMU details on a CPU in the cluster.
0667  */
0668 static void dsu_pmu_probe_pmu(struct dsu_pmu *dsu_pmu)
0669 {
0670     u64 num_counters;
0671     u32 cpmceid[2];
0672 
0673     num_counters = (__dsu_pmu_read_pmcr() >> CLUSTERPMCR_N_SHIFT) &
0674                         CLUSTERPMCR_N_MASK;
0675     /* We can only support up to 31 independent counters */
0676     if (WARN_ON(num_counters > 31))
0677         num_counters = 31;
0678     dsu_pmu->num_counters = num_counters;
0679     if (!dsu_pmu->num_counters)
0680         return;
0681     cpmceid[0] = __dsu_pmu_read_pmceid(0);
0682     cpmceid[1] = __dsu_pmu_read_pmceid(1);
0683     bitmap_from_arr32(dsu_pmu->cpmceid_bitmap, cpmceid,
0684               DSU_PMU_MAX_COMMON_EVENTS);
0685 }
0686 
0687 static void dsu_pmu_set_active_cpu(int cpu, struct dsu_pmu *dsu_pmu)
0688 {
0689     cpumask_set_cpu(cpu, &dsu_pmu->active_cpu);
0690     if (irq_set_affinity(dsu_pmu->irq, &dsu_pmu->active_cpu))
0691         pr_warn("Failed to set irq affinity to %d\n", cpu);
0692 }
0693 
0694 /*
0695  * dsu_pmu_init_pmu: Initialise the DSU PMU configurations if
0696  * we haven't done it already.
0697  */
0698 static void dsu_pmu_init_pmu(struct dsu_pmu *dsu_pmu)
0699 {
0700     if (dsu_pmu->num_counters == -1)
0701         dsu_pmu_probe_pmu(dsu_pmu);
0702     /* Reset the interrupt overflow mask */
0703     dsu_pmu_get_reset_overflow();
0704 }
0705 
0706 static int dsu_pmu_device_probe(struct platform_device *pdev)
0707 {
0708     int irq, rc;
0709     struct dsu_pmu *dsu_pmu;
0710     struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev);
0711     char *name;
0712     static atomic_t pmu_idx = ATOMIC_INIT(-1);
0713 
0714     dsu_pmu = dsu_pmu_alloc(pdev);
0715     if (IS_ERR(dsu_pmu))
0716         return PTR_ERR(dsu_pmu);
0717 
0718     if (is_of_node(fwnode))
0719         rc = dsu_pmu_dt_get_cpus(&pdev->dev, &dsu_pmu->associated_cpus);
0720     else if (is_acpi_device_node(fwnode))
0721         rc = dsu_pmu_acpi_get_cpus(&pdev->dev, &dsu_pmu->associated_cpus);
0722     else
0723         return -ENOENT;
0724 
0725     if (rc) {
0726         dev_warn(&pdev->dev, "Failed to parse the CPUs\n");
0727         return rc;
0728     }
0729 
0730     irq = platform_get_irq(pdev, 0);
0731     if (irq < 0)
0732         return -EINVAL;
0733 
0734     name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_%d",
0735                 PMUNAME, atomic_inc_return(&pmu_idx));
0736     if (!name)
0737         return -ENOMEM;
0738     rc = devm_request_irq(&pdev->dev, irq, dsu_pmu_handle_irq,
0739                   IRQF_NOBALANCING, name, dsu_pmu);
0740     if (rc) {
0741         dev_warn(&pdev->dev, "Failed to request IRQ %d\n", irq);
0742         return rc;
0743     }
0744 
0745     dsu_pmu->irq = irq;
0746     platform_set_drvdata(pdev, dsu_pmu);
0747     rc = cpuhp_state_add_instance(dsu_pmu_cpuhp_state,
0748                         &dsu_pmu->cpuhp_node);
0749     if (rc)
0750         return rc;
0751 
0752     dsu_pmu->pmu = (struct pmu) {
0753         .task_ctx_nr    = perf_invalid_context,
0754         .module     = THIS_MODULE,
0755         .pmu_enable = dsu_pmu_enable,
0756         .pmu_disable    = dsu_pmu_disable,
0757         .event_init = dsu_pmu_event_init,
0758         .add        = dsu_pmu_add,
0759         .del        = dsu_pmu_del,
0760         .start      = dsu_pmu_start,
0761         .stop       = dsu_pmu_stop,
0762         .read       = dsu_pmu_read,
0763 
0764         .attr_groups    = dsu_pmu_attr_groups,
0765         .capabilities   = PERF_PMU_CAP_NO_EXCLUDE,
0766     };
0767 
0768     rc = perf_pmu_register(&dsu_pmu->pmu, name, -1);
0769     if (rc) {
0770         cpuhp_state_remove_instance(dsu_pmu_cpuhp_state,
0771                          &dsu_pmu->cpuhp_node);
0772     }
0773 
0774     return rc;
0775 }
0776 
0777 static int dsu_pmu_device_remove(struct platform_device *pdev)
0778 {
0779     struct dsu_pmu *dsu_pmu = platform_get_drvdata(pdev);
0780 
0781     perf_pmu_unregister(&dsu_pmu->pmu);
0782     cpuhp_state_remove_instance(dsu_pmu_cpuhp_state, &dsu_pmu->cpuhp_node);
0783 
0784     return 0;
0785 }
0786 
0787 static const struct of_device_id dsu_pmu_of_match[] = {
0788     { .compatible = "arm,dsu-pmu", },
0789     {},
0790 };
0791 MODULE_DEVICE_TABLE(of, dsu_pmu_of_match);
0792 
0793 #ifdef CONFIG_ACPI
0794 static const struct acpi_device_id dsu_pmu_acpi_match[] = {
0795     { "ARMHD500", 0},
0796     {},
0797 };
0798 MODULE_DEVICE_TABLE(acpi, dsu_pmu_acpi_match);
0799 #endif
0800 
0801 static struct platform_driver dsu_pmu_driver = {
0802     .driver = {
0803         .name   = DRVNAME,
0804         .of_match_table = of_match_ptr(dsu_pmu_of_match),
0805         .acpi_match_table = ACPI_PTR(dsu_pmu_acpi_match),
0806         .suppress_bind_attrs = true,
0807     },
0808     .probe = dsu_pmu_device_probe,
0809     .remove = dsu_pmu_device_remove,
0810 };
0811 
0812 static int dsu_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
0813 {
0814     struct dsu_pmu *dsu_pmu = hlist_entry_safe(node, struct dsu_pmu,
0815                            cpuhp_node);
0816 
0817     if (!cpumask_test_cpu(cpu, &dsu_pmu->associated_cpus))
0818         return 0;
0819 
0820     /* If the PMU is already managed, there is nothing to do */
0821     if (!cpumask_empty(&dsu_pmu->active_cpu))
0822         return 0;
0823 
0824     dsu_pmu_init_pmu(dsu_pmu);
0825     dsu_pmu_set_active_cpu(cpu, dsu_pmu);
0826 
0827     return 0;
0828 }
0829 
0830 static int dsu_pmu_cpu_teardown(unsigned int cpu, struct hlist_node *node)
0831 {
0832     int dst;
0833     struct dsu_pmu *dsu_pmu = hlist_entry_safe(node, struct dsu_pmu,
0834                            cpuhp_node);
0835 
0836     if (!cpumask_test_and_clear_cpu(cpu, &dsu_pmu->active_cpu))
0837         return 0;
0838 
0839     dst = dsu_pmu_get_online_cpu_any_but(dsu_pmu, cpu);
0840     /* If there are no active CPUs in the DSU, leave IRQ disabled */
0841     if (dst >= nr_cpu_ids)
0842         return 0;
0843 
0844     perf_pmu_migrate_context(&dsu_pmu->pmu, cpu, dst);
0845     dsu_pmu_set_active_cpu(dst, dsu_pmu);
0846 
0847     return 0;
0848 }
0849 
0850 static int __init dsu_pmu_init(void)
0851 {
0852     int ret;
0853 
0854     ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
0855                     DRVNAME,
0856                     dsu_pmu_cpu_online,
0857                     dsu_pmu_cpu_teardown);
0858     if (ret < 0)
0859         return ret;
0860     dsu_pmu_cpuhp_state = ret;
0861     return platform_driver_register(&dsu_pmu_driver);
0862 }
0863 
0864 static void __exit dsu_pmu_exit(void)
0865 {
0866     platform_driver_unregister(&dsu_pmu_driver);
0867     cpuhp_remove_multi_state(dsu_pmu_cpuhp_state);
0868 }
0869 
0870 module_init(dsu_pmu_init);
0871 module_exit(dsu_pmu_exit);
0872 
0873 MODULE_DESCRIPTION("Perf driver for ARM DynamIQ Shared Unit");
0874 MODULE_AUTHOR("Suzuki K Poulose <suzuki.poulose@arm.com>");
0875 MODULE_LICENSE("GPL v2");