xref: /linux/drivers/perf/arm_pmu_platform.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * platform_device probing code for ARM performance counters.
4  *
5  * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
6  * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
7  */
8 #define pr_fmt(fmt) "hw perfevents: " fmt
9 #define dev_fmt pr_fmt
10 
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>
18 #include <linux/of.h>
19 #include <linux/percpu.h>
20 #include <linux/perf/arm_pmu.h>
21 #include <linux/platform_device.h>
22 #include <linux/printk.h>
23 #include <linux/smp.h>
24 
25 static int probe_current_pmu(struct arm_pmu *pmu,
26 			     const struct pmu_probe_info *info)
27 {
28 	int cpu = get_cpu();
29 	unsigned int cpuid = read_cpuid_id();
30 	int ret = -ENODEV;
31 
32 	pr_info("probing PMU on CPU %d\n", cpu);
33 
34 	for (; info->init != NULL; info++) {
35 		if ((cpuid & info->mask) != info->cpuid)
36 			continue;
37 		ret = info->init(pmu);
38 		break;
39 	}
40 
41 	put_cpu();
42 	return ret;
43 }
44 
45 static int pmu_parse_percpu_irq(struct arm_pmu *pmu, int irq,
46 				const struct cpumask *affinity)
47 {
48 	struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
49 	int cpu;
50 
51 	cpumask_copy(&pmu->supported_cpus, affinity);
52 
53 	for_each_cpu(cpu, &pmu->supported_cpus)
54 		per_cpu(hw_events->irq, cpu) = irq;
55 
56 	return 0;
57 }
58 
59 static bool pmu_has_irq_affinity(struct device_node *node)
60 {
61 	return of_property_present(node, "interrupt-affinity");
62 }
63 
64 static int pmu_parse_irq_affinity(struct device *dev, int i)
65 {
66 	struct device_node *dn;
67 	int cpu;
68 
69 	/*
70 	 * If we don't have an interrupt-affinity property, we guess irq
71 	 * affinity matches our logical CPU order, as we used to assume.
72 	 * This is fragile, so we'll warn in pmu_parse_irqs().
73 	 */
74 	if (!pmu_has_irq_affinity(dev->of_node))
75 		return i;
76 
77 	dn = of_parse_phandle(dev->of_node, "interrupt-affinity", i);
78 	if (!dn) {
79 		dev_warn(dev, "failed to parse interrupt-affinity[%d]\n", i);
80 		return -EINVAL;
81 	}
82 
83 	cpu = of_cpu_node_to_id(dn);
84 	if (cpu < 0) {
85 		dev_warn(dev, "failed to find logical CPU for %pOFn\n", dn);
86 		cpu = nr_cpu_ids;
87 	}
88 
89 	of_node_put(dn);
90 
91 	return cpu;
92 }
93 
94 static int pmu_parse_irqs(struct arm_pmu *pmu)
95 {
96 	int i = 0, num_irqs;
97 	struct platform_device *pdev = pmu->plat_device;
98 	struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
99 	struct device *dev = &pdev->dev;
100 
101 	num_irqs = platform_irq_count(pdev);
102 	if (num_irqs < 0)
103 		return dev_err_probe(dev, num_irqs, "unable to count PMU IRQs\n");
104 
105 	/*
106 	 * In this case we have no idea which CPUs are covered by the PMU.
107 	 * To match our prior behaviour, we assume all CPUs in this case.
108 	 */
109 	if (num_irqs == 0) {
110 		dev_warn(dev, "no irqs for PMU, sampling events not supported\n");
111 		pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
112 		cpumask_setall(&pmu->supported_cpus);
113 		return 0;
114 	}
115 
116 	if (num_irqs == 1) {
117 		const struct cpumask *affinity;
118 		int irq;
119 
120 		irq = platform_get_irq_affinity(pdev, 0, &affinity);
121 		if ((irq > 0) && irq_is_percpu_devid(irq))
122 			return pmu_parse_percpu_irq(pmu, irq, affinity);
123 	}
124 
125 	if (nr_cpu_ids != 1 && !pmu_has_irq_affinity(dev->of_node))
126 		dev_warn(dev, "no interrupt-affinity property, guessing.\n");
127 
128 	for (i = 0; i < num_irqs; i++) {
129 		int cpu, irq;
130 
131 		irq = platform_get_irq(pdev, i);
132 		if (WARN_ON(irq <= 0))
133 			continue;
134 
135 		if (irq_is_percpu_devid(irq)) {
136 			dev_warn(dev, "multiple PPIs or mismatched SPI/PPI detected\n");
137 			return -EINVAL;
138 		}
139 
140 		cpu = pmu_parse_irq_affinity(dev, i);
141 		if (cpu < 0)
142 			return cpu;
143 		if (cpu >= nr_cpu_ids)
144 			continue;
145 
146 		if (per_cpu(hw_events->irq, cpu)) {
147 			dev_warn(dev, "multiple PMU IRQs for the same CPU detected\n");
148 			return -EINVAL;
149 		}
150 
151 		per_cpu(hw_events->irq, cpu) = irq;
152 		cpumask_set_cpu(cpu, &pmu->supported_cpus);
153 	}
154 
155 	return 0;
156 }
157 
158 static int armpmu_request_irqs(struct arm_pmu *armpmu)
159 {
160 	struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
161 	int cpu, err = 0;
162 
163 	for_each_cpu(cpu, &armpmu->supported_cpus) {
164 		int irq = per_cpu(hw_events->irq, cpu);
165 		if (!irq)
166 			continue;
167 
168 		err = armpmu_request_irq(&hw_events->percpu_pmu, irq, cpu);
169 		if (err)
170 			break;
171 	}
172 
173 	return err;
174 }
175 
176 static void armpmu_free_irqs(struct arm_pmu *armpmu)
177 {
178 	int cpu;
179 	struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
180 
181 	for_each_cpu(cpu, &armpmu->supported_cpus) {
182 		int irq = per_cpu(hw_events->irq, cpu);
183 
184 		armpmu_free_irq(&hw_events->percpu_pmu, irq, cpu);
185 	}
186 }
187 
188 int arm_pmu_device_probe(struct platform_device *pdev,
189 			 const struct of_device_id *of_table,
190 			 const struct pmu_probe_info *probe_table)
191 {
192 	armpmu_init_fn init_fn;
193 	struct device *dev = &pdev->dev;
194 	struct arm_pmu *pmu;
195 	int ret = -ENODEV;
196 
197 	pmu = armpmu_alloc();
198 	if (!pmu)
199 		return -ENOMEM;
200 
201 	pmu->pmu.parent = &pdev->dev;
202 	pmu->plat_device = pdev;
203 
204 	ret = pmu_parse_irqs(pmu);
205 	if (ret)
206 		goto out_free;
207 
208 	init_fn = of_device_get_match_data(dev);
209 	if (init_fn) {
210 		pmu->secure_access = of_property_read_bool(dev->of_node,
211 							   "secure-reg-access");
212 
213 		/* arm64 systems boot only as non-secure */
214 		if (IS_ENABLED(CONFIG_ARM64) && pmu->secure_access) {
215 			dev_warn(dev, "ignoring \"secure-reg-access\" property for arm64\n");
216 			pmu->secure_access = false;
217 		}
218 
219 		ret = init_fn(pmu);
220 	} else if (probe_table) {
221 		cpumask_setall(&pmu->supported_cpus);
222 		ret = probe_current_pmu(pmu, probe_table);
223 	}
224 
225 	if (ret) {
226 		dev_err(dev, "failed to probe PMU!\n");
227 		goto out_free;
228 	}
229 
230 	ret = armpmu_request_irqs(pmu);
231 	if (ret)
232 		goto out_free_irqs;
233 
234 	ret = armpmu_register(pmu);
235 	if (ret) {
236 		dev_err(dev, "failed to register PMU devices!\n");
237 		goto out_free_irqs;
238 	}
239 
240 	return 0;
241 
242 out_free_irqs:
243 	armpmu_free_irqs(pmu);
244 out_free:
245 	armpmu_free(pmu);
246 	return ret;
247 }
248