blob: 8d6147b2001f82eca02c738265f9477dc2dbaefa [file] [log] [blame]
Will Deacon5505b202012-07-29 13:09:14 +01001/*
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/export.h>
23#include <linux/kernel.h>
24#include <linux/of.h>
25#include <linux/platform_device.h>
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +010026#include <linux/slab.h>
Will Deacon5505b202012-07-29 13:09:14 +010027#include <linux/spinlock.h>
28
29#include <asm/cputype.h>
30#include <asm/irq_regs.h>
31#include <asm/pmu.h>
32
33/* Set at runtime when we know what CPU type we are. */
34static struct arm_pmu *cpu_pmu;
35
36static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
37static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
38static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
39
40/*
41 * Despite the names, these two functions are CPU-specific and are used
42 * by the OProfile/perf code.
43 */
44const char *perf_pmu_name(void)
45{
46 if (!cpu_pmu)
47 return NULL;
48
Will Deacon03052302012-09-21 14:23:47 +010049 return cpu_pmu->name;
Will Deacon5505b202012-07-29 13:09:14 +010050}
51EXPORT_SYMBOL_GPL(perf_pmu_name);
52
53int perf_num_counters(void)
54{
55 int max_events = 0;
56
57 if (cpu_pmu != NULL)
58 max_events = cpu_pmu->num_events;
59
60 return max_events;
61}
62EXPORT_SYMBOL_GPL(perf_num_counters);
63
64/* Include the PMU-specific implementations. */
65#include "perf_event_xscale.c"
66#include "perf_event_v6.c"
67#include "perf_event_v7.c"
68
69static struct pmu_hw_events *cpu_pmu_get_cpu_events(void)
70{
71 return &__get_cpu_var(cpu_hw_events);
72}
73
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +010074static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +010075{
76 int i, irq, irqs;
77 struct platform_device *pmu_device = cpu_pmu->plat_device;
78
79 irqs = min(pmu_device->num_resources, num_possible_cpus());
80
81 for (i = 0; i < irqs; ++i) {
82 if (!cpumask_test_and_clear_cpu(i, &cpu_pmu->active_irqs))
83 continue;
84 irq = platform_get_irq(pmu_device, i);
85 if (irq >= 0)
86 free_irq(irq, cpu_pmu);
87 }
88}
89
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +010090static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +010091{
92 int i, err, irq, irqs;
93 struct platform_device *pmu_device = cpu_pmu->plat_device;
94
95 if (!pmu_device)
96 return -ENODEV;
97
98 irqs = min(pmu_device->num_resources, num_possible_cpus());
99 if (irqs < 1) {
100 pr_err("no irqs for PMUs defined\n");
101 return -ENODEV;
102 }
103
104 for (i = 0; i < irqs; ++i) {
105 err = 0;
106 irq = platform_get_irq(pmu_device, i);
107 if (irq < 0)
108 continue;
109
110 /*
111 * If we have a single PMU interrupt that we can't shift,
112 * assume that we're running on a uniprocessor machine and
113 * continue. Otherwise, continue without this interrupt.
114 */
115 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
116 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
117 irq, i);
118 continue;
119 }
120
Thomas Gleixnerd9c33652013-08-14 11:07:47 +0100121 err = request_irq(irq, handler,
122 IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100123 cpu_pmu);
124 if (err) {
125 pr_err("unable to request IRQ%d for ARM PMU counters\n",
126 irq);
127 return err;
128 }
129
130 cpumask_set_cpu(i, &cpu_pmu->active_irqs);
131 }
132
133 return 0;
134}
135
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800136static void cpu_pmu_init(struct arm_pmu *cpu_pmu)
Will Deacon5505b202012-07-29 13:09:14 +0100137{
138 int cpu;
139 for_each_possible_cpu(cpu) {
140 struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
141 events->events = per_cpu(hw_events, cpu);
142 events->used_mask = per_cpu(used_mask, cpu);
143 raw_spin_lock_init(&events->pmu_lock);
144 }
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100145
146 cpu_pmu->get_hw_events = cpu_pmu_get_cpu_events;
147 cpu_pmu->request_irq = cpu_pmu_request_irq;
148 cpu_pmu->free_irq = cpu_pmu_free_irq;
Will Deacon5505b202012-07-29 13:09:14 +0100149
150 /* Ensure the PMU has sane values out of reset. */
Will Deacon1764c592013-01-14 17:27:35 +0000151 if (cpu_pmu->reset)
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100152 on_each_cpu(cpu_pmu->reset, cpu_pmu, 1);
Will Deacon5505b202012-07-29 13:09:14 +0100153}
154
155/*
156 * PMU hardware loses all context when a CPU goes offline.
157 * When a CPU is hotplugged back in, since some hardware registers are
158 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
159 * junk values out of them.
160 */
Paul Gortmaker8bd26e32013-06-17 15:43:14 -0400161static int cpu_pmu_notify(struct notifier_block *b, unsigned long action,
162 void *hcpu)
Will Deacon5505b202012-07-29 13:09:14 +0100163{
164 if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
165 return NOTIFY_DONE;
166
167 if (cpu_pmu && cpu_pmu->reset)
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100168 cpu_pmu->reset(cpu_pmu);
Will Deacon288700d2012-09-21 14:14:17 +0100169 else
170 return NOTIFY_DONE;
Will Deacon5505b202012-07-29 13:09:14 +0100171
172 return NOTIFY_OK;
173}
174
Paul Gortmaker8bd26e32013-06-17 15:43:14 -0400175static struct notifier_block cpu_pmu_hotplug_notifier = {
Will Deacon5505b202012-07-29 13:09:14 +0100176 .notifier_call = cpu_pmu_notify,
177};
178
179/*
180 * PMU platform driver and devicetree bindings.
181 */
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800182static struct of_device_id cpu_pmu_of_device_ids[] = {
Will Deacon5505b202012-07-29 13:09:14 +0100183 {.compatible = "arm,cortex-a15-pmu", .data = armv7_a15_pmu_init},
184 {.compatible = "arm,cortex-a9-pmu", .data = armv7_a9_pmu_init},
185 {.compatible = "arm,cortex-a8-pmu", .data = armv7_a8_pmu_init},
186 {.compatible = "arm,cortex-a7-pmu", .data = armv7_a7_pmu_init},
187 {.compatible = "arm,cortex-a5-pmu", .data = armv7_a5_pmu_init},
188 {.compatible = "arm,arm11mpcore-pmu", .data = armv6mpcore_pmu_init},
189 {.compatible = "arm,arm1176-pmu", .data = armv6pmu_init},
190 {.compatible = "arm,arm1136-pmu", .data = armv6pmu_init},
191 {},
192};
193
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800194static struct platform_device_id cpu_pmu_plat_device_ids[] = {
Will Deacon5505b202012-07-29 13:09:14 +0100195 {.name = "arm-pmu"},
196 {},
197};
198
199/*
200 * CPU PMU identification and probing.
201 */
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800202static int probe_current_pmu(struct arm_pmu *pmu)
Will Deacon5505b202012-07-29 13:09:14 +0100203{
Will Deacon5505b202012-07-29 13:09:14 +0100204 int cpu = get_cpu();
Christoffer Dall3b953c92012-12-18 04:06:38 +0000205 unsigned long implementor = read_cpuid_implementor();
206 unsigned long part_number = read_cpuid_part_number();
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100207 int ret = -ENODEV;
Will Deacon5505b202012-07-29 13:09:14 +0100208
209 pr_info("probing PMU on CPU %d\n", cpu);
210
211 /* ARM Ltd CPUs. */
Christoffer Dall3b953c92012-12-18 04:06:38 +0000212 if (implementor == ARM_CPU_IMP_ARM) {
Will Deacon5505b202012-07-29 13:09:14 +0100213 switch (part_number) {
Christoffer Dall3b953c92012-12-18 04:06:38 +0000214 case ARM_CPU_PART_ARM1136:
215 case ARM_CPU_PART_ARM1156:
216 case ARM_CPU_PART_ARM1176:
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100217 ret = armv6pmu_init(pmu);
Will Deacon5505b202012-07-29 13:09:14 +0100218 break;
Christoffer Dall3b953c92012-12-18 04:06:38 +0000219 case ARM_CPU_PART_ARM11MPCORE:
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100220 ret = armv6mpcore_pmu_init(pmu);
Will Deacon5505b202012-07-29 13:09:14 +0100221 break;
Christoffer Dall3b953c92012-12-18 04:06:38 +0000222 case ARM_CPU_PART_CORTEX_A8:
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100223 ret = armv7_a8_pmu_init(pmu);
Will Deacon5505b202012-07-29 13:09:14 +0100224 break;
Christoffer Dall3b953c92012-12-18 04:06:38 +0000225 case ARM_CPU_PART_CORTEX_A9:
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100226 ret = armv7_a9_pmu_init(pmu);
Will Deacon5505b202012-07-29 13:09:14 +0100227 break;
Christoffer Dall3b953c92012-12-18 04:06:38 +0000228 case ARM_CPU_PART_CORTEX_A5:
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100229 ret = armv7_a5_pmu_init(pmu);
Will Deacon5505b202012-07-29 13:09:14 +0100230 break;
Christoffer Dall3b953c92012-12-18 04:06:38 +0000231 case ARM_CPU_PART_CORTEX_A15:
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100232 ret = armv7_a15_pmu_init(pmu);
Will Deacon5505b202012-07-29 13:09:14 +0100233 break;
Christoffer Dall3b953c92012-12-18 04:06:38 +0000234 case ARM_CPU_PART_CORTEX_A7:
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100235 ret = armv7_a7_pmu_init(pmu);
Will Deacon5505b202012-07-29 13:09:14 +0100236 break;
237 }
238 /* Intel CPUs [xscale]. */
Christoffer Dall3b953c92012-12-18 04:06:38 +0000239 } else if (implementor == ARM_CPU_IMP_INTEL) {
240 switch (xscale_cpu_arch_version()) {
241 case ARM_CPU_XSCALE_ARCH_V1:
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100242 ret = xscale1pmu_init(pmu);
Will Deacon5505b202012-07-29 13:09:14 +0100243 break;
Christoffer Dall3b953c92012-12-18 04:06:38 +0000244 case ARM_CPU_XSCALE_ARCH_V2:
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100245 ret = xscale2pmu_init(pmu);
Will Deacon5505b202012-07-29 13:09:14 +0100246 break;
247 }
248 }
249
250 put_cpu();
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100251 return ret;
Will Deacon5505b202012-07-29 13:09:14 +0100252}
253
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800254static int cpu_pmu_device_probe(struct platform_device *pdev)
Will Deacon5505b202012-07-29 13:09:14 +0100255{
256 const struct of_device_id *of_id;
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100257 int (*init_fn)(struct arm_pmu *);
Will Deacon5505b202012-07-29 13:09:14 +0100258 struct device_node *node = pdev->dev.of_node;
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100259 struct arm_pmu *pmu;
260 int ret = -ENODEV;
Will Deacon5505b202012-07-29 13:09:14 +0100261
262 if (cpu_pmu) {
263 pr_info("attempt to register multiple PMU devices!");
264 return -ENOSPC;
265 }
266
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100267 pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
268 if (!pmu) {
269 pr_info("failed to allocate PMU device!");
270 return -ENOMEM;
Will Deacon5505b202012-07-29 13:09:14 +0100271 }
272
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100273 if (node && (of_id = of_match_node(cpu_pmu_of_device_ids, pdev->dev.of_node))) {
274 init_fn = of_id->data;
275 ret = init_fn(pmu);
276 } else {
277 ret = probe_current_pmu(pmu);
278 }
Will Deacon5505b202012-07-29 13:09:14 +0100279
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100280 if (ret) {
Mark Rutland76b8a0e2013-01-18 13:42:58 +0000281 pr_info("failed to probe PMU!");
282 goto out_free;
Sudeep KarkadaNagesha513c99c2012-07-31 10:11:23 +0100283 }
284
285 cpu_pmu = pmu;
Will Deacon5505b202012-07-29 13:09:14 +0100286 cpu_pmu->plat_device = pdev;
287 cpu_pmu_init(cpu_pmu);
Mark Rutland76b8a0e2013-01-18 13:42:58 +0000288 ret = armpmu_register(cpu_pmu, PERF_TYPE_RAW);
Will Deacon5505b202012-07-29 13:09:14 +0100289
Mark Rutland76b8a0e2013-01-18 13:42:58 +0000290 if (!ret)
291 return 0;
292
293out_free:
294 pr_info("failed to register PMU devices!");
295 kfree(pmu);
296 return ret;
Will Deacon5505b202012-07-29 13:09:14 +0100297}
298
299static struct platform_driver cpu_pmu_driver = {
300 .driver = {
301 .name = "arm-pmu",
302 .pm = &armpmu_dev_pm_ops,
303 .of_match_table = cpu_pmu_of_device_ids,
304 },
305 .probe = cpu_pmu_device_probe,
306 .id_table = cpu_pmu_plat_device_ids,
307};
308
309static int __init register_pmu_driver(void)
310{
Mark Rutland2a4961b2012-09-21 11:53:41 +0100311 int err;
312
313 err = register_cpu_notifier(&cpu_pmu_hotplug_notifier);
314 if (err)
315 return err;
316
317 err = platform_driver_register(&cpu_pmu_driver);
318 if (err)
319 unregister_cpu_notifier(&cpu_pmu_hotplug_notifier);
320
321 return err;
Will Deacon5505b202012-07-29 13:09:14 +0100322}
323device_initcall(register_pmu_driver);