1 // SPDX-License-Identifier: GPL-2.0
3 * platform_device probing code for ARM performance counters.
5 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
6 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
8 #define pr_fmt(fmt) "hw perfevents: " fmt
11 #include <linux/bug.h>
12 #include <linux/cpumask.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/irq.h>
16 #include <linux/irqdesc.h>
17 #include <linux/kconfig.h>
19 #include <linux/of_device.h>
20 #include <linux/percpu.h>
21 #include <linux/perf/arm_pmu.h>
22 #include <linux/platform_device.h>
23 #include <linux/printk.h>
24 #include <linux/smp.h>
26 static int probe_current_pmu(struct arm_pmu *pmu,
27 const struct pmu_probe_info *info)
30 unsigned int cpuid = read_cpuid_id();
33 pr_info("probing PMU on CPU %d\n", cpu);
35 for (; info->init != NULL; info++) {
36 if ((cpuid & info->mask) != info->cpuid)
38 ret = info->init(pmu);
46 static int pmu_parse_percpu_irq(struct arm_pmu *pmu, int irq)
49 struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
51 ret = irq_get_percpu_devid_partition(irq, &pmu->supported_cpus);
55 for_each_cpu(cpu, &pmu->supported_cpus)
56 per_cpu(hw_events->irq, cpu) = irq;
61 static bool pmu_has_irq_affinity(struct device_node *node)
63 return !!of_find_property(node, "interrupt-affinity", NULL);
66 static int pmu_parse_irq_affinity(struct device_node *node, int i)
68 struct device_node *dn;
72 * If we don't have an interrupt-affinity property, we guess irq
73 * affinity matches our logical CPU order, as we used to assume.
74 * This is fragile, so we'll warn in pmu_parse_irqs().
76 if (!pmu_has_irq_affinity(node))
79 dn = of_parse_phandle(node, "interrupt-affinity", i);
81 pr_warn("failed to parse interrupt-affinity[%d] for %pOFn\n",
86 cpu = of_cpu_node_to_id(dn);
88 pr_warn("failed to find logical CPU for %pOFn\n", dn);
97 static int pmu_parse_irqs(struct arm_pmu *pmu)
100 struct platform_device *pdev = pmu->plat_device;
101 struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
103 num_irqs = platform_irq_count(pdev);
105 return dev_err_probe(&pdev->dev, num_irqs, "unable to count PMU IRQs\n");
108 * In this case we have no idea which CPUs are covered by the PMU.
109 * To match our prior behaviour, we assume all CPUs in this case.
112 pr_warn("no irqs for PMU, sampling events not supported\n");
113 pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
114 cpumask_setall(&pmu->supported_cpus);
119 int irq = platform_get_irq(pdev, 0);
120 if ((irq > 0) && irq_is_percpu_devid(irq))
121 return pmu_parse_percpu_irq(pmu, irq);
124 if (nr_cpu_ids != 1 && !pmu_has_irq_affinity(pdev->dev.of_node)) {
125 pr_warn("no interrupt-affinity property for %pOF, guessing.\n",
129 for (i = 0; i < num_irqs; i++) {
132 irq = platform_get_irq(pdev, i);
133 if (WARN_ON(irq <= 0))
136 if (irq_is_percpu_devid(irq)) {
137 pr_warn("multiple PPIs or mismatched SPI/PPI detected\n");
141 cpu = pmu_parse_irq_affinity(pdev->dev.of_node, i);
144 if (cpu >= nr_cpu_ids)
147 if (per_cpu(hw_events->irq, cpu)) {
148 pr_warn("multiple PMU IRQs for the same CPU detected\n");
152 per_cpu(hw_events->irq, cpu) = irq;
153 cpumask_set_cpu(cpu, &pmu->supported_cpus);
159 static int armpmu_request_irqs(struct arm_pmu *armpmu)
161 struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
164 for_each_cpu(cpu, &armpmu->supported_cpus) {
165 int irq = per_cpu(hw_events->irq, cpu);
169 err = armpmu_request_irq(irq, cpu);
177 static void armpmu_free_irqs(struct arm_pmu *armpmu)
180 struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
182 for_each_cpu(cpu, &armpmu->supported_cpus) {
183 int irq = per_cpu(hw_events->irq, cpu);
185 armpmu_free_irq(irq, cpu);
189 int arm_pmu_device_probe(struct platform_device *pdev,
190 const struct of_device_id *of_table,
191 const struct pmu_probe_info *probe_table)
193 const struct of_device_id *of_id;
194 armpmu_init_fn init_fn;
195 struct device_node *node = pdev->dev.of_node;
199 pmu = armpmu_alloc();
203 pmu->plat_device = pdev;
205 ret = pmu_parse_irqs(pmu);
209 if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
210 init_fn = of_id->data;
212 pmu->secure_access = of_property_read_bool(pdev->dev.of_node,
213 "secure-reg-access");
215 /* arm64 systems boot only as non-secure */
216 if (IS_ENABLED(CONFIG_ARM64) && pmu->secure_access) {
217 pr_warn("ignoring \"secure-reg-access\" property for arm64\n");
218 pmu->secure_access = false;
222 } else if (probe_table) {
223 cpumask_setall(&pmu->supported_cpus);
224 ret = probe_current_pmu(pmu, probe_table);
228 pr_info("%pOF: failed to probe PMU!\n", node);
232 ret = armpmu_request_irqs(pmu);
236 ret = armpmu_register(pmu);
243 armpmu_free_irqs(pmu);
245 pr_info("%pOF: failed to register PMU devices!\n", node);