Branch data Line data Source code
1 : : /*
2 : : * This program is free software; you can redistribute it and/or modify
3 : : * it under the terms of the GNU General Public License version 2 as
4 : : * published by the Free Software Foundation.
5 : : *
6 : : * This program is distributed in the hope that it will be useful,
7 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 : : * GNU General Public License for more details.
10 : : *
11 : : * You should have received a copy of the GNU General Public License
12 : : * along with this program; if not, write to the Free Software
13 : : * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14 : : *
15 : : * Copyright (C) 2012 ARM Limited
16 : : *
17 : : * Author: Will Deacon <will.deacon@arm.com>
18 : : */
19 : : #define pr_fmt(fmt) "CPU PMU: " fmt
20 : :
21 : : #include <linux/bitmap.h>
22 : : #include <linux/cpu_pm.h>
23 : : #include <linux/export.h>
24 : : #include <linux/kernel.h>
25 : : #include <linux/of.h>
26 : : #include <linux/platform_device.h>
27 : : #include <linux/slab.h>
28 : : #include <linux/spinlock.h>
29 : :
30 : : #include <asm/cputype.h>
31 : : #include <asm/irq_regs.h>
32 : : #include <asm/pmu.h>
33 : :
34 : : /* Set at runtime when we know what CPU type we are. */
35 : : static DEFINE_PER_CPU(struct arm_pmu *, cpu_pmu);
36 : :
37 : : static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
38 : : static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
39 : : static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
40 : :
41 : : static DEFINE_PER_CPU(struct cpupmu_regs, cpu_pmu_regs);
42 : :
43 : : /*
44 : : * Despite the names, these two functions are CPU-specific and are used
45 : : * by the OProfile/perf code.
46 : : */
47 : 0 : const char *perf_pmu_name(void)
48 : : {
49 : 0 : struct arm_pmu *pmu = per_cpu(cpu_pmu, 0);
50 [ # # ]: 0 : if (!pmu)
51 : : return NULL;
52 : :
53 : 0 : return pmu->name;
54 : : }
55 : : EXPORT_SYMBOL_GPL(perf_pmu_name);
56 : :
57 : 0 : int perf_num_counters(void)
58 : : {
59 : 0 : struct arm_pmu *pmu = per_cpu(cpu_pmu, 0);
60 : :
61 [ # # ]: 0 : if (!pmu)
62 : : return 0;
63 : :
64 : 0 : return pmu->num_events;
65 : : }
66 : : EXPORT_SYMBOL_GPL(perf_num_counters);
67 : :
68 : : /* Include the PMU-specific implementations. */
69 : : #include "perf_event_xscale.c"
70 : : #include "perf_event_v6.c"
71 : : #include "perf_event_v7.c"
72 : :
73 : 0 : static struct pmu_hw_events *cpu_pmu_get_cpu_events(void)
74 : : {
75 : 0 : return this_cpu_ptr(&cpu_hw_events);
76 : : }
77 : :
78 : 0 : static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
79 : : {
80 : : int i, irq, irqs;
81 : 0 : struct platform_device *pmu_device = cpu_pmu->plat_device;
82 : : int cpu = -1;
83 : :
84 : 0 : irqs = min(pmu_device->num_resources, num_possible_cpus());
85 : :
86 [ # # ]: 0 : for (i = 0; i < irqs; ++i) {
87 : : cpu = cpumask_next(cpu, &cpu_pmu->valid_cpus);
88 [ # # ]: 0 : if (!cpumask_test_and_clear_cpu(cpu, &cpu_pmu->active_irqs))
89 : 0 : continue;
90 : 0 : irq = platform_get_irq(pmu_device, i);
91 [ # # ]: 0 : if (irq >= 0)
92 : 0 : free_irq(irq, cpu_pmu);
93 : : }
94 : 0 : }
95 : :
96 : 0 : static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
97 : : {
98 : : int i, err, irq, irqs;
99 : 0 : struct platform_device *pmu_device = cpu_pmu->plat_device;
100 : : int cpu = -1;
101 : :
102 [ # # ]: 0 : if (!pmu_device)
103 : : return -ENODEV;
104 : :
105 : 0 : irqs = min(pmu_device->num_resources, num_possible_cpus());
106 [ # # ]: 0 : if (irqs < 1) {
107 : 0 : pr_err("no irqs for PMUs defined\n");
108 : 0 : return -ENODEV;
109 : : }
110 : :
111 [ # # ]: 0 : for (i = 0; i < irqs; ++i) {
112 : : err = 0;
113 : : cpu = cpumask_next(cpu, &cpu_pmu->valid_cpus);
114 : 0 : irq = platform_get_irq(pmu_device, i);
115 [ # # ]: 0 : if (irq < 0)
116 : 0 : continue;
117 : :
118 : : /*
119 : : * If we have a single PMU interrupt that we can't shift,
120 : : * assume that we're running on a uniprocessor machine and
121 : : * continue. Otherwise, continue without this interrupt.
122 : : */
123 [ # # ][ # # ]: 0 : if (irq_set_affinity(irq, cpumask_of(cpu)) && irqs > 1) {
124 : 0 : pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
125 : : irq, i);
126 : 0 : continue;
127 : : }
128 : :
129 : : err = request_irq(irq, handler,
130 : : IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
131 : : cpu_pmu);
132 [ # # ]: 0 : if (err) {
133 : 0 : pr_err("unable to request IRQ%d for ARM PMU counters\n",
134 : : irq);
135 : 0 : return err;
136 : : }
137 : :
138 : : cpumask_set_cpu(cpu, &cpu_pmu->active_irqs);
139 : : }
140 : :
141 : : return 0;
142 : : }
143 : :
144 : 0 : static void cpu_pmu_init(struct arm_pmu *cpu_pmu)
145 : : {
146 : : int cpu;
147 [ # # ]: 0 : for_each_cpu_mask(cpu, cpu_pmu->valid_cpus) {
148 : 0 : struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
149 : 0 : events->events = per_cpu(hw_events, cpu);
150 : 0 : events->used_mask = per_cpu(used_mask, cpu);
151 : 0 : raw_spin_lock_init(&events->pmu_lock);
152 : : }
153 : :
154 : 0 : cpu_pmu->get_hw_events = cpu_pmu_get_cpu_events;
155 : 0 : cpu_pmu->request_irq = cpu_pmu_request_irq;
156 : 0 : cpu_pmu->free_irq = cpu_pmu_free_irq;
157 : :
158 : : /* Ensure the PMU has sane values out of reset. */
159 [ # # ]: 0 : if (cpu_pmu->reset)
160 : 0 : on_each_cpu_mask(&cpu_pmu->valid_cpus, cpu_pmu->reset, cpu_pmu, 1);
161 : 0 : }
162 : :
163 : : /*
164 : : * PMU hardware loses all context when a CPU goes offline.
165 : : * When a CPU is hotplugged back in, since some hardware registers are
166 : : * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
167 : : * junk values out of them.
168 : : */
169 : 0 : static int cpu_pmu_notify(struct notifier_block *b, unsigned long action,
170 : : void *hcpu)
171 : : {
172 : 555 : struct arm_pmu *pmu = per_cpu(cpu_pmu, (long)hcpu);
173 : :
174 [ + + ]: 555 : if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
175 : : return NOTIFY_DONE;
176 : :
177 [ + - ][ + - ]: 81 : if (pmu && pmu->reset)
178 : 81 : pmu->reset(pmu);
179 : : else
180 : : return NOTIFY_DONE;
181 : :
182 : 81 : return NOTIFY_OK;
183 : : }
184 : :
185 : 0 : static int cpu_pmu_pm_notify(struct notifier_block *b,
186 : : unsigned long action, void *hcpu)
187 : : {
188 : 9166347 : int cpu = smp_processor_id();
189 : 9166347 : struct arm_pmu *pmu = per_cpu(cpu_pmu, cpu);
190 : 9166347 : struct cpupmu_regs *pmuregs = &per_cpu(cpu_pmu_regs, cpu);
191 : :
192 [ + ]: 9166347 : if (!pmu)
193 : : return NOTIFY_DONE;
194 : :
195 [ + + ][ + ]: 9423879 : if (action == CPU_PM_ENTER && pmu->save_regs) {
196 : 4967025 : pmu->save_regs(pmu, pmuregs);
197 [ + + ][ + ]: 4456854 : } else if (action == CPU_PM_EXIT && pmu->restore_regs) {
198 : 4408419 : pmu->restore_regs(pmu, pmuregs);
199 : : }
200 : :
201 : : return NOTIFY_OK;
202 : : }
203 : :
204 : : static struct notifier_block cpu_pmu_hotplug_notifier = {
205 : : .notifier_call = cpu_pmu_notify,
206 : : };
207 : :
208 : : static struct notifier_block cpu_pmu_pm_notifier = {
209 : : .notifier_call = cpu_pmu_pm_notify,
210 : : };
211 : :
212 : : /*
213 : : * PMU platform driver and devicetree bindings.
214 : : */
215 : : static struct of_device_id cpu_pmu_of_device_ids[] = {
216 : : {.compatible = "arm,cortex-a15-pmu", .data = armv7_a15_pmu_init},
217 : : {.compatible = "arm,cortex-a9-pmu", .data = armv7_a9_pmu_init},
218 : : {.compatible = "arm,cortex-a8-pmu", .data = armv7_a8_pmu_init},
219 : : {.compatible = "arm,cortex-a7-pmu", .data = armv7_a7_pmu_init},
220 : : {.compatible = "arm,cortex-a5-pmu", .data = armv7_a5_pmu_init},
221 : : {.compatible = "arm,arm11mpcore-pmu", .data = armv6mpcore_pmu_init},
222 : : {.compatible = "arm,arm1176-pmu", .data = armv6pmu_init},
223 : : {.compatible = "arm,arm1136-pmu", .data = armv6pmu_init},
224 : : {},
225 : : };
226 : :
227 : : static struct platform_device_id cpu_pmu_plat_device_ids[] = {
228 : : {.name = "arm-pmu"},
229 : : {},
230 : : };
231 : :
232 : : /*
233 : : * CPU PMU identification and probing.
234 : : */
235 : 0 : static int probe_current_pmu(struct arm_pmu *pmu)
236 : : {
237 : 0 : int cpu = get_cpu();
238 : : unsigned long implementor = read_cpuid_implementor();
239 : : unsigned long part_number = read_cpuid_part_number();
240 : : int ret = -ENODEV;
241 : :
242 : 0 : pr_info("probing PMU on CPU %d\n", cpu);
243 : :
244 : : /* ARM Ltd CPUs. */
245 [ # # ]: 0 : if (implementor == ARM_CPU_IMP_ARM) {
246 [ # # # # : 0 : switch (part_number) {
# # ]
247 : : case ARM_CPU_PART_ARM1136:
248 : : case ARM_CPU_PART_ARM1156:
249 : : case ARM_CPU_PART_ARM1176:
250 : : ret = armv6pmu_init(pmu);
251 : : break;
252 : : case ARM_CPU_PART_ARM11MPCORE:
253 : : ret = armv6mpcore_pmu_init(pmu);
254 : : break;
255 : : case ARM_CPU_PART_CORTEX_A8:
256 : : ret = armv7_a8_pmu_init(pmu);
257 : 0 : break;
258 : : case ARM_CPU_PART_CORTEX_A9:
259 : : ret = armv7_a9_pmu_init(pmu);
260 : 0 : break;
261 : : case ARM_CPU_PART_CORTEX_A5:
262 : : ret = armv7_a5_pmu_init(pmu);
263 : 0 : break;
264 : : case ARM_CPU_PART_CORTEX_A15:
265 : : ret = armv7_a15_pmu_init(pmu);
266 : 0 : break;
267 : : case ARM_CPU_PART_CORTEX_A7:
268 : : ret = armv7_a7_pmu_init(pmu);
269 : 0 : break;
270 : : }
271 : : /* Intel CPUs [xscale]. */
272 [ # # ]: 0 : } else if (implementor == ARM_CPU_IMP_INTEL) {
273 : : switch (xscale_cpu_arch_version()) {
274 : : case ARM_CPU_XSCALE_ARCH_V1:
275 : : ret = xscale1pmu_init(pmu);
276 : : break;
277 : : case ARM_CPU_XSCALE_ARCH_V2:
278 : : ret = xscale2pmu_init(pmu);
279 : : break;
280 : : }
281 : : }
282 : :
283 : : /* assume PMU support all the CPUs in this case */
284 : : cpumask_setall(&pmu->valid_cpus);
285 : :
286 : 0 : put_cpu();
287 : 0 : return ret;
288 : : }
289 : :
290 : 0 : static int cpu_pmu_device_probe(struct platform_device *pdev)
291 : : {
292 : : const struct of_device_id *of_id;
293 : 0 : struct device_node *node = pdev->dev.of_node;
294 : : struct arm_pmu *pmu;
295 : : int ret = 0;
296 : : int cpu;
297 : :
298 : : pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
299 [ # # ]: 0 : if (!pmu) {
300 : 0 : pr_info("failed to allocate PMU device!");
301 : 0 : return -ENOMEM;
302 : : }
303 : :
304 [ # # ][ # # ]: 0 : if (node && (of_id = of_match_node(cpu_pmu_of_device_ids, pdev->dev.of_node))) {
305 : 0 : smp_call_func_t init_fn = (smp_call_func_t)of_id->data;
306 : : struct device_node *ncluster;
307 : : int cluster = -1;
308 : : cpumask_t sibling_mask;
309 : :
310 : 0 : ncluster = of_parse_phandle(node, "cluster", 0);
311 [ # # ]: 0 : if (ncluster) {
312 : : int len;
313 : 0 : const u32 *hwid;
314 : 0 : hwid = of_get_property(ncluster, "reg", &len);
315 [ # # ][ # # ]: 0 : if (hwid && len == 4)
316 : 0 : cluster = be32_to_cpup(hwid);
317 : : }
318 : : /* set sibling mask to all cpu mask if socket is not specified */
319 [ # # # # ]: 0 : if (cluster == -1 ||
320 : 0 : cluster_to_logical_mask(cluster, &sibling_mask))
321 : : cpumask_setall(&sibling_mask);
322 : :
323 : 0 : smp_call_function_any(&sibling_mask, init_fn, pmu, 1);
324 : :
325 : : /* now set the valid_cpus after init */
326 : : cpumask_copy(&pmu->valid_cpus, &sibling_mask);
327 : : } else {
328 : 0 : ret = probe_current_pmu(pmu);
329 : : }
330 : :
331 [ # # ]: 0 : if (ret) {
332 : 0 : pr_info("failed to probe PMU!");
333 : 0 : goto out_free;
334 : : }
335 : :
336 [ # # ]: 0 : for_each_cpu_mask(cpu, pmu->valid_cpus)
337 : 0 : per_cpu(cpu_pmu, cpu) = pmu;
338 : :
339 : 0 : pmu->plat_device = pdev;
340 : 0 : cpu_pmu_init(pmu);
341 : 0 : ret = armpmu_register(pmu, -1);
342 : :
343 [ # # ]: 0 : if (!ret)
344 : : return 0;
345 : :
346 : : out_free:
347 : 0 : pr_info("failed to register PMU devices!");
348 : 0 : kfree(pmu);
349 : 0 : return ret;
350 : : }
351 : :
352 : : static struct platform_driver cpu_pmu_driver = {
353 : : .driver = {
354 : : .name = "arm-pmu",
355 : : .pm = &armpmu_dev_pm_ops,
356 : : .of_match_table = cpu_pmu_of_device_ids,
357 : : },
358 : : .probe = cpu_pmu_device_probe,
359 : : .id_table = cpu_pmu_plat_device_ids,
360 : : };
361 : :
362 : 0 : static int __init register_pmu_driver(void)
363 : : {
364 : : int err;
365 : :
366 : 0 : err = register_cpu_notifier(&cpu_pmu_hotplug_notifier);
367 [ # # ]: 0 : if (err)
368 : : return err;
369 : :
370 : 0 : err = cpu_pm_register_notifier(&cpu_pmu_pm_notifier);
371 [ # # ]: 0 : if (err) {
372 : 0 : unregister_cpu_notifier(&cpu_pmu_hotplug_notifier);
373 : 0 : return err;
374 : : }
375 : :
376 : 0 : err = platform_driver_register(&cpu_pmu_driver);
377 [ # # ]: 0 : if (err) {
378 : 0 : cpu_pm_unregister_notifier(&cpu_pmu_pm_notifier);
379 : 0 : unregister_cpu_notifier(&cpu_pmu_hotplug_notifier);
380 : : }
381 : :
382 : 0 : return err;
383 : : }
384 : : device_initcall(register_pmu_driver);
|