Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #undef DEBUG
0003 
0004 /*
0005  * ARM performance counter support.
0006  *
0007  * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
0008  * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
0009  *
0010  * This code is based on the sparc64 perf event code, which is in turn based
0011  * on the x86 code.
0012  */
0013 #define pr_fmt(fmt) "hw perfevents: " fmt
0014 
0015 #include <linux/bitmap.h>
0016 #include <linux/cpumask.h>
0017 #include <linux/cpu_pm.h>
0018 #include <linux/export.h>
0019 #include <linux/kernel.h>
0020 #include <linux/perf/arm_pmu.h>
0021 #include <linux/slab.h>
0022 #include <linux/sched/clock.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/irq.h>
0025 #include <linux/irqdesc.h>
0026 
0027 #include <asm/irq_regs.h>
0028 
0029 static int armpmu_count_irq_users(const int irq);
0030 
0031 struct pmu_irq_ops {
0032     void (*enable_pmuirq)(unsigned int irq);
0033     void (*disable_pmuirq)(unsigned int irq);
0034     void (*free_pmuirq)(unsigned int irq, int cpu, void __percpu *devid);
0035 };
0036 
0037 static void armpmu_free_pmuirq(unsigned int irq, int cpu, void __percpu *devid)
0038 {
0039     free_irq(irq, per_cpu_ptr(devid, cpu));
0040 }
0041 
0042 static const struct pmu_irq_ops pmuirq_ops = {
0043     .enable_pmuirq = enable_irq,
0044     .disable_pmuirq = disable_irq_nosync,
0045     .free_pmuirq = armpmu_free_pmuirq
0046 };
0047 
0048 static void armpmu_free_pmunmi(unsigned int irq, int cpu, void __percpu *devid)
0049 {
0050     free_nmi(irq, per_cpu_ptr(devid, cpu));
0051 }
0052 
0053 static const struct pmu_irq_ops pmunmi_ops = {
0054     .enable_pmuirq = enable_nmi,
0055     .disable_pmuirq = disable_nmi_nosync,
0056     .free_pmuirq = armpmu_free_pmunmi
0057 };
0058 
0059 static void armpmu_enable_percpu_pmuirq(unsigned int irq)
0060 {
0061     enable_percpu_irq(irq, IRQ_TYPE_NONE);
0062 }
0063 
0064 static void armpmu_free_percpu_pmuirq(unsigned int irq, int cpu,
0065                    void __percpu *devid)
0066 {
0067     if (armpmu_count_irq_users(irq) == 1)
0068         free_percpu_irq(irq, devid);
0069 }
0070 
0071 static const struct pmu_irq_ops percpu_pmuirq_ops = {
0072     .enable_pmuirq = armpmu_enable_percpu_pmuirq,
0073     .disable_pmuirq = disable_percpu_irq,
0074     .free_pmuirq = armpmu_free_percpu_pmuirq
0075 };
0076 
0077 static void armpmu_enable_percpu_pmunmi(unsigned int irq)
0078 {
0079     if (!prepare_percpu_nmi(irq))
0080         enable_percpu_nmi(irq, IRQ_TYPE_NONE);
0081 }
0082 
0083 static void armpmu_disable_percpu_pmunmi(unsigned int irq)
0084 {
0085     disable_percpu_nmi(irq);
0086     teardown_percpu_nmi(irq);
0087 }
0088 
0089 static void armpmu_free_percpu_pmunmi(unsigned int irq, int cpu,
0090                       void __percpu *devid)
0091 {
0092     if (armpmu_count_irq_users(irq) == 1)
0093         free_percpu_nmi(irq, devid);
0094 }
0095 
0096 static const struct pmu_irq_ops percpu_pmunmi_ops = {
0097     .enable_pmuirq = armpmu_enable_percpu_pmunmi,
0098     .disable_pmuirq = armpmu_disable_percpu_pmunmi,
0099     .free_pmuirq = armpmu_free_percpu_pmunmi
0100 };
0101 
0102 static DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
0103 static DEFINE_PER_CPU(int, cpu_irq);
0104 static DEFINE_PER_CPU(const struct pmu_irq_ops *, cpu_irq_ops);
0105 
0106 static bool has_nmi;
0107 
0108 static inline u64 arm_pmu_event_max_period(struct perf_event *event)
0109 {
0110     if (event->hw.flags & ARMPMU_EVT_64BIT)
0111         return GENMASK_ULL(63, 0);
0112     else if (event->hw.flags & ARMPMU_EVT_47BIT)
0113         return GENMASK_ULL(46, 0);
0114     else
0115         return GENMASK_ULL(31, 0);
0116 }
0117 
0118 static int
0119 armpmu_map_cache_event(const unsigned (*cache_map)
0120                       [PERF_COUNT_HW_CACHE_MAX]
0121                       [PERF_COUNT_HW_CACHE_OP_MAX]
0122                       [PERF_COUNT_HW_CACHE_RESULT_MAX],
0123                u64 config)
0124 {
0125     unsigned int cache_type, cache_op, cache_result, ret;
0126 
0127     cache_type = (config >>  0) & 0xff;
0128     if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
0129         return -EINVAL;
0130 
0131     cache_op = (config >>  8) & 0xff;
0132     if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
0133         return -EINVAL;
0134 
0135     cache_result = (config >> 16) & 0xff;
0136     if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
0137         return -EINVAL;
0138 
0139     if (!cache_map)
0140         return -ENOENT;
0141 
0142     ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
0143 
0144     if (ret == CACHE_OP_UNSUPPORTED)
0145         return -ENOENT;
0146 
0147     return ret;
0148 }
0149 
0150 static int
0151 armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
0152 {
0153     int mapping;
0154 
0155     if (config >= PERF_COUNT_HW_MAX)
0156         return -EINVAL;
0157 
0158     if (!event_map)
0159         return -ENOENT;
0160 
0161     mapping = (*event_map)[config];
0162     return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
0163 }
0164 
0165 static int
0166 armpmu_map_raw_event(u32 raw_event_mask, u64 config)
0167 {
0168     return (int)(config & raw_event_mask);
0169 }
0170 
0171 int
0172 armpmu_map_event(struct perf_event *event,
0173          const unsigned (*event_map)[PERF_COUNT_HW_MAX],
0174          const unsigned (*cache_map)
0175                 [PERF_COUNT_HW_CACHE_MAX]
0176                 [PERF_COUNT_HW_CACHE_OP_MAX]
0177                 [PERF_COUNT_HW_CACHE_RESULT_MAX],
0178          u32 raw_event_mask)
0179 {
0180     u64 config = event->attr.config;
0181     int type = event->attr.type;
0182 
0183     if (type == event->pmu->type)
0184         return armpmu_map_raw_event(raw_event_mask, config);
0185 
0186     switch (type) {
0187     case PERF_TYPE_HARDWARE:
0188         return armpmu_map_hw_event(event_map, config);
0189     case PERF_TYPE_HW_CACHE:
0190         return armpmu_map_cache_event(cache_map, config);
0191     case PERF_TYPE_RAW:
0192         return armpmu_map_raw_event(raw_event_mask, config);
0193     }
0194 
0195     return -ENOENT;
0196 }
0197 
0198 int armpmu_event_set_period(struct perf_event *event)
0199 {
0200     struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
0201     struct hw_perf_event *hwc = &event->hw;
0202     s64 left = local64_read(&hwc->period_left);
0203     s64 period = hwc->sample_period;
0204     u64 max_period;
0205     int ret = 0;
0206 
0207     max_period = arm_pmu_event_max_period(event);
0208     if (unlikely(left <= -period)) {
0209         left = period;
0210         local64_set(&hwc->period_left, left);
0211         hwc->last_period = period;
0212         ret = 1;
0213     }
0214 
0215     if (unlikely(left <= 0)) {
0216         left += period;
0217         local64_set(&hwc->period_left, left);
0218         hwc->last_period = period;
0219         ret = 1;
0220     }
0221 
0222     /*
0223      * Limit the maximum period to prevent the counter value
0224      * from overtaking the one we are about to program. In
0225      * effect we are reducing max_period to account for
0226      * interrupt latency (and we are being very conservative).
0227      */
0228     if (left > (max_period >> 1))
0229         left = (max_period >> 1);
0230 
0231     local64_set(&hwc->prev_count, (u64)-left);
0232 
0233     armpmu->write_counter(event, (u64)(-left) & max_period);
0234 
0235     perf_event_update_userpage(event);
0236 
0237     return ret;
0238 }
0239 
0240 u64 armpmu_event_update(struct perf_event *event)
0241 {
0242     struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
0243     struct hw_perf_event *hwc = &event->hw;
0244     u64 delta, prev_raw_count, new_raw_count;
0245     u64 max_period = arm_pmu_event_max_period(event);
0246 
0247 again:
0248     prev_raw_count = local64_read(&hwc->prev_count);
0249     new_raw_count = armpmu->read_counter(event);
0250 
0251     if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
0252                  new_raw_count) != prev_raw_count)
0253         goto again;
0254 
0255     delta = (new_raw_count - prev_raw_count) & max_period;
0256 
0257     local64_add(delta, &event->count);
0258     local64_sub(delta, &hwc->period_left);
0259 
0260     return new_raw_count;
0261 }
0262 
0263 static void
0264 armpmu_read(struct perf_event *event)
0265 {
0266     armpmu_event_update(event);
0267 }
0268 
0269 static void
0270 armpmu_stop(struct perf_event *event, int flags)
0271 {
0272     struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
0273     struct hw_perf_event *hwc = &event->hw;
0274 
0275     /*
0276      * ARM pmu always has to update the counter, so ignore
0277      * PERF_EF_UPDATE, see comments in armpmu_start().
0278      */
0279     if (!(hwc->state & PERF_HES_STOPPED)) {
0280         armpmu->disable(event);
0281         armpmu_event_update(event);
0282         hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
0283     }
0284 }
0285 
0286 static void armpmu_start(struct perf_event *event, int flags)
0287 {
0288     struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
0289     struct hw_perf_event *hwc = &event->hw;
0290 
0291     /*
0292      * ARM pmu always has to reprogram the period, so ignore
0293      * PERF_EF_RELOAD, see the comment below.
0294      */
0295     if (flags & PERF_EF_RELOAD)
0296         WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
0297 
0298     hwc->state = 0;
0299     /*
0300      * Set the period again. Some counters can't be stopped, so when we
0301      * were stopped we simply disabled the IRQ source and the counter
0302      * may have been left counting. If we don't do this step then we may
0303      * get an interrupt too soon or *way* too late if the overflow has
0304      * happened since disabling.
0305      */
0306     armpmu_event_set_period(event);
0307     armpmu->enable(event);
0308 }
0309 
0310 static void
0311 armpmu_del(struct perf_event *event, int flags)
0312 {
0313     struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
0314     struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
0315     struct hw_perf_event *hwc = &event->hw;
0316     int idx = hwc->idx;
0317 
0318     armpmu_stop(event, PERF_EF_UPDATE);
0319     hw_events->events[idx] = NULL;
0320     armpmu->clear_event_idx(hw_events, event);
0321     perf_event_update_userpage(event);
0322     /* Clear the allocated counter */
0323     hwc->idx = -1;
0324 }
0325 
0326 static int
0327 armpmu_add(struct perf_event *event, int flags)
0328 {
0329     struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
0330     struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
0331     struct hw_perf_event *hwc = &event->hw;
0332     int idx;
0333 
0334     /* An event following a process won't be stopped earlier */
0335     if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
0336         return -ENOENT;
0337 
0338     /* If we don't have a space for the counter then finish early. */
0339     idx = armpmu->get_event_idx(hw_events, event);
0340     if (idx < 0)
0341         return idx;
0342 
0343     /*
0344      * If there is an event in the counter we are going to use then make
0345      * sure it is disabled.
0346      */
0347     event->hw.idx = idx;
0348     armpmu->disable(event);
0349     hw_events->events[idx] = event;
0350 
0351     hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
0352     if (flags & PERF_EF_START)
0353         armpmu_start(event, PERF_EF_RELOAD);
0354 
0355     /* Propagate our changes to the userspace mapping. */
0356     perf_event_update_userpage(event);
0357 
0358     return 0;
0359 }
0360 
0361 static int
0362 validate_event(struct pmu *pmu, struct pmu_hw_events *hw_events,
0363                    struct perf_event *event)
0364 {
0365     struct arm_pmu *armpmu;
0366 
0367     if (is_software_event(event))
0368         return 1;
0369 
0370     /*
0371      * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
0372      * core perf code won't check that the pmu->ctx == leader->ctx
0373      * until after pmu->event_init(event).
0374      */
0375     if (event->pmu != pmu)
0376         return 0;
0377 
0378     if (event->state < PERF_EVENT_STATE_OFF)
0379         return 1;
0380 
0381     if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
0382         return 1;
0383 
0384     armpmu = to_arm_pmu(event->pmu);
0385     return armpmu->get_event_idx(hw_events, event) >= 0;
0386 }
0387 
0388 static int
0389 validate_group(struct perf_event *event)
0390 {
0391     struct perf_event *sibling, *leader = event->group_leader;
0392     struct pmu_hw_events fake_pmu;
0393 
0394     /*
0395      * Initialise the fake PMU. We only need to populate the
0396      * used_mask for the purposes of validation.
0397      */
0398     memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
0399 
0400     if (!validate_event(event->pmu, &fake_pmu, leader))
0401         return -EINVAL;
0402 
0403     if (event == leader)
0404         return 0;
0405 
0406     for_each_sibling_event(sibling, leader) {
0407         if (!validate_event(event->pmu, &fake_pmu, sibling))
0408             return -EINVAL;
0409     }
0410 
0411     if (!validate_event(event->pmu, &fake_pmu, event))
0412         return -EINVAL;
0413 
0414     return 0;
0415 }
0416 
0417 static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
0418 {
0419     struct arm_pmu *armpmu;
0420     int ret;
0421     u64 start_clock, finish_clock;
0422 
0423     /*
0424      * we request the IRQ with a (possibly percpu) struct arm_pmu**, but
0425      * the handlers expect a struct arm_pmu*. The percpu_irq framework will
0426      * do any necessary shifting, we just need to perform the first
0427      * dereference.
0428      */
0429     armpmu = *(void **)dev;
0430     if (WARN_ON_ONCE(!armpmu))
0431         return IRQ_NONE;
0432 
0433     start_clock = sched_clock();
0434     ret = armpmu->handle_irq(armpmu);
0435     finish_clock = sched_clock();
0436 
0437     perf_sample_event_took(finish_clock - start_clock);
0438     return ret;
0439 }
0440 
0441 static int
0442 __hw_perf_event_init(struct perf_event *event)
0443 {
0444     struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
0445     struct hw_perf_event *hwc = &event->hw;
0446     int mapping;
0447 
0448     hwc->flags = 0;
0449     mapping = armpmu->map_event(event);
0450 
0451     if (mapping < 0) {
0452         pr_debug("event %x:%llx not supported\n", event->attr.type,
0453              event->attr.config);
0454         return mapping;
0455     }
0456 
0457     /*
0458      * We don't assign an index until we actually place the event onto
0459      * hardware. Use -1 to signify that we haven't decided where to put it
0460      * yet. For SMP systems, each core has it's own PMU so we can't do any
0461      * clever allocation or constraints checking at this point.
0462      */
0463     hwc->idx        = -1;
0464     hwc->config_base    = 0;
0465     hwc->config     = 0;
0466     hwc->event_base     = 0;
0467 
0468     /*
0469      * Check whether we need to exclude the counter from certain modes.
0470      */
0471     if (armpmu->set_event_filter &&
0472         armpmu->set_event_filter(hwc, &event->attr)) {
0473         pr_debug("ARM performance counters do not support "
0474              "mode exclusion\n");
0475         return -EOPNOTSUPP;
0476     }
0477 
0478     /*
0479      * Store the event encoding into the config_base field.
0480      */
0481     hwc->config_base        |= (unsigned long)mapping;
0482 
0483     if (!is_sampling_event(event)) {
0484         /*
0485          * For non-sampling runs, limit the sample_period to half
0486          * of the counter width. That way, the new counter value
0487          * is far less likely to overtake the previous one unless
0488          * you have some serious IRQ latency issues.
0489          */
0490         hwc->sample_period  = arm_pmu_event_max_period(event) >> 1;
0491         hwc->last_period    = hwc->sample_period;
0492         local64_set(&hwc->period_left, hwc->sample_period);
0493     }
0494 
0495     return validate_group(event);
0496 }
0497 
0498 static int armpmu_event_init(struct perf_event *event)
0499 {
0500     struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
0501 
0502     /*
0503      * Reject CPU-affine events for CPUs that are of a different class to
0504      * that which this PMU handles. Process-following events (where
0505      * event->cpu == -1) can be migrated between CPUs, and thus we have to
0506      * reject them later (in armpmu_add) if they're scheduled on a
0507      * different class of CPU.
0508      */
0509     if (event->cpu != -1 &&
0510         !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
0511         return -ENOENT;
0512 
0513     /* does not support taken branch sampling */
0514     if (has_branch_stack(event))
0515         return -EOPNOTSUPP;
0516 
0517     if (armpmu->map_event(event) == -ENOENT)
0518         return -ENOENT;
0519 
0520     return __hw_perf_event_init(event);
0521 }
0522 
0523 static void armpmu_enable(struct pmu *pmu)
0524 {
0525     struct arm_pmu *armpmu = to_arm_pmu(pmu);
0526     struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
0527     bool enabled = !bitmap_empty(hw_events->used_mask, armpmu->num_events);
0528 
0529     /* For task-bound events we may be called on other CPUs */
0530     if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
0531         return;
0532 
0533     if (enabled)
0534         armpmu->start(armpmu);
0535 }
0536 
0537 static void armpmu_disable(struct pmu *pmu)
0538 {
0539     struct arm_pmu *armpmu = to_arm_pmu(pmu);
0540 
0541     /* For task-bound events we may be called on other CPUs */
0542     if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
0543         return;
0544 
0545     armpmu->stop(armpmu);
0546 }
0547 
0548 /*
0549  * In heterogeneous systems, events are specific to a particular
0550  * microarchitecture, and aren't suitable for another. Thus, only match CPUs of
0551  * the same microarchitecture.
0552  */
0553 static int armpmu_filter_match(struct perf_event *event)
0554 {
0555     struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
0556     unsigned int cpu = smp_processor_id();
0557     int ret;
0558 
0559     ret = cpumask_test_cpu(cpu, &armpmu->supported_cpus);
0560     if (ret && armpmu->filter_match)
0561         return armpmu->filter_match(event);
0562 
0563     return ret;
0564 }
0565 
0566 static ssize_t cpus_show(struct device *dev,
0567              struct device_attribute *attr, char *buf)
0568 {
0569     struct arm_pmu *armpmu = to_arm_pmu(dev_get_drvdata(dev));
0570     return cpumap_print_to_pagebuf(true, buf, &armpmu->supported_cpus);
0571 }
0572 
0573 static DEVICE_ATTR_RO(cpus);
0574 
0575 static struct attribute *armpmu_common_attrs[] = {
0576     &dev_attr_cpus.attr,
0577     NULL,
0578 };
0579 
0580 static const struct attribute_group armpmu_common_attr_group = {
0581     .attrs = armpmu_common_attrs,
0582 };
0583 
0584 static int armpmu_count_irq_users(const int irq)
0585 {
0586     int cpu, count = 0;
0587 
0588     for_each_possible_cpu(cpu) {
0589         if (per_cpu(cpu_irq, cpu) == irq)
0590             count++;
0591     }
0592 
0593     return count;
0594 }
0595 
0596 static const struct pmu_irq_ops *armpmu_find_irq_ops(int irq)
0597 {
0598     const struct pmu_irq_ops *ops = NULL;
0599     int cpu;
0600 
0601     for_each_possible_cpu(cpu) {
0602         if (per_cpu(cpu_irq, cpu) != irq)
0603             continue;
0604 
0605         ops = per_cpu(cpu_irq_ops, cpu);
0606         if (ops)
0607             break;
0608     }
0609 
0610     return ops;
0611 }
0612 
0613 void armpmu_free_irq(int irq, int cpu)
0614 {
0615     if (per_cpu(cpu_irq, cpu) == 0)
0616         return;
0617     if (WARN_ON(irq != per_cpu(cpu_irq, cpu)))
0618         return;
0619 
0620     per_cpu(cpu_irq_ops, cpu)->free_pmuirq(irq, cpu, &cpu_armpmu);
0621 
0622     per_cpu(cpu_irq, cpu) = 0;
0623     per_cpu(cpu_irq_ops, cpu) = NULL;
0624 }
0625 
0626 int armpmu_request_irq(int irq, int cpu)
0627 {
0628     int err = 0;
0629     const irq_handler_t handler = armpmu_dispatch_irq;
0630     const struct pmu_irq_ops *irq_ops;
0631 
0632     if (!irq)
0633         return 0;
0634 
0635     if (!irq_is_percpu_devid(irq)) {
0636         unsigned long irq_flags;
0637 
0638         err = irq_force_affinity(irq, cpumask_of(cpu));
0639 
0640         if (err && num_possible_cpus() > 1) {
0641             pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
0642                 irq, cpu);
0643             goto err_out;
0644         }
0645 
0646         irq_flags = IRQF_PERCPU |
0647                 IRQF_NOBALANCING | IRQF_NO_AUTOEN |
0648                 IRQF_NO_THREAD;
0649 
0650         err = request_nmi(irq, handler, irq_flags, "arm-pmu",
0651                   per_cpu_ptr(&cpu_armpmu, cpu));
0652 
0653         /* If cannot get an NMI, get a normal interrupt */
0654         if (err) {
0655             err = request_irq(irq, handler, irq_flags, "arm-pmu",
0656                       per_cpu_ptr(&cpu_armpmu, cpu));
0657             irq_ops = &pmuirq_ops;
0658         } else {
0659             has_nmi = true;
0660             irq_ops = &pmunmi_ops;
0661         }
0662     } else if (armpmu_count_irq_users(irq) == 0) {
0663         err = request_percpu_nmi(irq, handler, "arm-pmu", &cpu_armpmu);
0664 
0665         /* If cannot get an NMI, get a normal interrupt */
0666         if (err) {
0667             err = request_percpu_irq(irq, handler, "arm-pmu",
0668                          &cpu_armpmu);
0669             irq_ops = &percpu_pmuirq_ops;
0670         } else {
0671             has_nmi = true;
0672             irq_ops = &percpu_pmunmi_ops;
0673         }
0674     } else {
0675         /* Per cpudevid irq was already requested by another CPU */
0676         irq_ops = armpmu_find_irq_ops(irq);
0677 
0678         if (WARN_ON(!irq_ops))
0679             err = -EINVAL;
0680     }
0681 
0682     if (err)
0683         goto err_out;
0684 
0685     per_cpu(cpu_irq, cpu) = irq;
0686     per_cpu(cpu_irq_ops, cpu) = irq_ops;
0687     return 0;
0688 
0689 err_out:
0690     pr_err("unable to request IRQ%d for ARM PMU counters\n", irq);
0691     return err;
0692 }
0693 
0694 static int armpmu_get_cpu_irq(struct arm_pmu *pmu, int cpu)
0695 {
0696     struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
0697     return per_cpu(hw_events->irq, cpu);
0698 }
0699 
0700 /*
0701  * PMU hardware loses all context when a CPU goes offline.
0702  * When a CPU is hotplugged back in, since some hardware registers are
0703  * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
0704  * junk values out of them.
0705  */
0706 static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
0707 {
0708     struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
0709     int irq;
0710 
0711     if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
0712         return 0;
0713     if (pmu->reset)
0714         pmu->reset(pmu);
0715 
0716     per_cpu(cpu_armpmu, cpu) = pmu;
0717 
0718     irq = armpmu_get_cpu_irq(pmu, cpu);
0719     if (irq)
0720         per_cpu(cpu_irq_ops, cpu)->enable_pmuirq(irq);
0721 
0722     return 0;
0723 }
0724 
0725 static int arm_perf_teardown_cpu(unsigned int cpu, struct hlist_node *node)
0726 {
0727     struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
0728     int irq;
0729 
0730     if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
0731         return 0;
0732 
0733     irq = armpmu_get_cpu_irq(pmu, cpu);
0734     if (irq)
0735         per_cpu(cpu_irq_ops, cpu)->disable_pmuirq(irq);
0736 
0737     per_cpu(cpu_armpmu, cpu) = NULL;
0738 
0739     return 0;
0740 }
0741 
0742 #ifdef CONFIG_CPU_PM
0743 static void cpu_pm_pmu_setup(struct arm_pmu *armpmu, unsigned long cmd)
0744 {
0745     struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
0746     struct perf_event *event;
0747     int idx;
0748 
0749     for (idx = 0; idx < armpmu->num_events; idx++) {
0750         event = hw_events->events[idx];
0751         if (!event)
0752             continue;
0753 
0754         switch (cmd) {
0755         case CPU_PM_ENTER:
0756             /*
0757              * Stop and update the counter
0758              */
0759             armpmu_stop(event, PERF_EF_UPDATE);
0760             break;
0761         case CPU_PM_EXIT:
0762         case CPU_PM_ENTER_FAILED:
0763              /*
0764               * Restore and enable the counter.
0765               * armpmu_start() indirectly calls
0766               *
0767               * perf_event_update_userpage()
0768               *
0769               * that requires RCU read locking to be functional,
0770               * wrap the call within RCU_NONIDLE to make the
0771               * RCU subsystem aware this cpu is not idle from
0772               * an RCU perspective for the armpmu_start() call
0773               * duration.
0774               */
0775             RCU_NONIDLE(armpmu_start(event, PERF_EF_RELOAD));
0776             break;
0777         default:
0778             break;
0779         }
0780     }
0781 }
0782 
0783 static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
0784                  void *v)
0785 {
0786     struct arm_pmu *armpmu = container_of(b, struct arm_pmu, cpu_pm_nb);
0787     struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
0788     bool enabled = !bitmap_empty(hw_events->used_mask, armpmu->num_events);
0789 
0790     if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
0791         return NOTIFY_DONE;
0792 
0793     /*
0794      * Always reset the PMU registers on power-up even if
0795      * there are no events running.
0796      */
0797     if (cmd == CPU_PM_EXIT && armpmu->reset)
0798         armpmu->reset(armpmu);
0799 
0800     if (!enabled)
0801         return NOTIFY_OK;
0802 
0803     switch (cmd) {
0804     case CPU_PM_ENTER:
0805         armpmu->stop(armpmu);
0806         cpu_pm_pmu_setup(armpmu, cmd);
0807         break;
0808     case CPU_PM_EXIT:
0809     case CPU_PM_ENTER_FAILED:
0810         cpu_pm_pmu_setup(armpmu, cmd);
0811         armpmu->start(armpmu);
0812         break;
0813     default:
0814         return NOTIFY_DONE;
0815     }
0816 
0817     return NOTIFY_OK;
0818 }
0819 
0820 static int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu)
0821 {
0822     cpu_pmu->cpu_pm_nb.notifier_call = cpu_pm_pmu_notify;
0823     return cpu_pm_register_notifier(&cpu_pmu->cpu_pm_nb);
0824 }
0825 
0826 static void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu)
0827 {
0828     cpu_pm_unregister_notifier(&cpu_pmu->cpu_pm_nb);
0829 }
0830 #else
0831 static inline int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu) { return 0; }
0832 static inline void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu) { }
0833 #endif
0834 
0835 static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
0836 {
0837     int err;
0838 
0839     err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_STARTING,
0840                        &cpu_pmu->node);
0841     if (err)
0842         goto out;
0843 
0844     err = cpu_pm_pmu_register(cpu_pmu);
0845     if (err)
0846         goto out_unregister;
0847 
0848     return 0;
0849 
0850 out_unregister:
0851     cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
0852                         &cpu_pmu->node);
0853 out:
0854     return err;
0855 }
0856 
0857 static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
0858 {
0859     cpu_pm_pmu_unregister(cpu_pmu);
0860     cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
0861                         &cpu_pmu->node);
0862 }
0863 
0864 static struct arm_pmu *__armpmu_alloc(gfp_t flags)
0865 {
0866     struct arm_pmu *pmu;
0867     int cpu;
0868 
0869     pmu = kzalloc(sizeof(*pmu), flags);
0870     if (!pmu)
0871         goto out;
0872 
0873     pmu->hw_events = alloc_percpu_gfp(struct pmu_hw_events, flags);
0874     if (!pmu->hw_events) {
0875         pr_info("failed to allocate per-cpu PMU data.\n");
0876         goto out_free_pmu;
0877     }
0878 
0879     pmu->pmu = (struct pmu) {
0880         .pmu_enable = armpmu_enable,
0881         .pmu_disable    = armpmu_disable,
0882         .event_init = armpmu_event_init,
0883         .add        = armpmu_add,
0884         .del        = armpmu_del,
0885         .start      = armpmu_start,
0886         .stop       = armpmu_stop,
0887         .read       = armpmu_read,
0888         .filter_match   = armpmu_filter_match,
0889         .attr_groups    = pmu->attr_groups,
0890         /*
0891          * This is a CPU PMU potentially in a heterogeneous
0892          * configuration (e.g. big.LITTLE). This is not an uncore PMU,
0893          * and we have taken ctx sharing into account (e.g. with our
0894          * pmu::filter_match callback and pmu::event_init group
0895          * validation).
0896          */
0897         .capabilities   = PERF_PMU_CAP_HETEROGENEOUS_CPUS,
0898     };
0899 
0900     pmu->attr_groups[ARMPMU_ATTR_GROUP_COMMON] =
0901         &armpmu_common_attr_group;
0902 
0903     for_each_possible_cpu(cpu) {
0904         struct pmu_hw_events *events;
0905 
0906         events = per_cpu_ptr(pmu->hw_events, cpu);
0907         raw_spin_lock_init(&events->pmu_lock);
0908         events->percpu_pmu = pmu;
0909     }
0910 
0911     return pmu;
0912 
0913 out_free_pmu:
0914     kfree(pmu);
0915 out:
0916     return NULL;
0917 }
0918 
0919 struct arm_pmu *armpmu_alloc(void)
0920 {
0921     return __armpmu_alloc(GFP_KERNEL);
0922 }
0923 
0924 struct arm_pmu *armpmu_alloc_atomic(void)
0925 {
0926     return __armpmu_alloc(GFP_ATOMIC);
0927 }
0928 
0929 
0930 void armpmu_free(struct arm_pmu *pmu)
0931 {
0932     free_percpu(pmu->hw_events);
0933     kfree(pmu);
0934 }
0935 
0936 int armpmu_register(struct arm_pmu *pmu)
0937 {
0938     int ret;
0939 
0940     ret = cpu_pmu_init(pmu);
0941     if (ret)
0942         return ret;
0943 
0944     if (!pmu->set_event_filter)
0945         pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
0946 
0947     ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
0948     if (ret)
0949         goto out_destroy;
0950 
0951     pr_info("enabled with %s PMU driver, %d counters available%s\n",
0952         pmu->name, pmu->num_events,
0953         has_nmi ? ", using NMIs" : "");
0954 
0955     kvm_host_pmu_init(pmu);
0956 
0957     return 0;
0958 
0959 out_destroy:
0960     cpu_pmu_destroy(pmu);
0961     return ret;
0962 }
0963 
0964 static int arm_pmu_hp_init(void)
0965 {
0966     int ret;
0967 
0968     ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_STARTING,
0969                       "perf/arm/pmu:starting",
0970                       arm_perf_starting_cpu,
0971                       arm_perf_teardown_cpu);
0972     if (ret)
0973         pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
0974                ret);
0975     return ret;
0976 }
0977 subsys_initcall(arm_pmu_hp_init);