blob: 4f67cfab2c59457de2a56dacb03d96a89e9fcc48 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file common.c
3 *
4 * @remark Copyright 2004 Oprofile Authors
Will Deacon8c1fc962010-04-30 11:36:54 +01005 * @remark Copyright 2010 ARM Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * @remark Read the file COPYING
7 *
8 * @author Zwane Mwaikambo
Will Deacon8c1fc962010-04-30 11:36:54 +01009 * @author Will Deacon [move to perf]
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Will Deacon8c1fc962010-04-30 11:36:54 +010012#include <linux/cpumask.h>
Will Deacond1e86d62010-04-30 11:38:39 +010013#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
Will Deacon8c1fc962010-04-30 11:36:54 +010015#include <linux/init.h>
16#include <linux/mutex.h>
17#include <linux/oprofile.h>
18#include <linux/perf_event.h>
Will Deacond1e86d62010-04-30 11:38:39 +010019#include <linux/platform_device.h>
Russell Kingae92dc92006-03-16 11:32:51 +000020#include <linux/slab.h>
Will Deacon8c1fc962010-04-30 11:36:54 +010021#include <asm/stacktrace.h>
22#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Will Deacon8c1fc962010-04-30 11:36:54 +010024#include <asm/perf_event.h>
25#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Will Deacon8c1fc962010-04-30 11:36:54 +010027#ifdef CONFIG_HW_PERF_EVENTS
28/*
29 * Per performance monitor configuration as set via oprofilefs.
30 */
31struct op_counter_config {
32 unsigned long count;
33 unsigned long enabled;
34 unsigned long event;
35 unsigned long unit_mask;
36 unsigned long kernel;
37 unsigned long user;
38 struct perf_event_attr attr;
39};
40
Russell King55f05232005-10-28 14:54:21 +010041static int op_arm_enabled;
Russell King93ad7942006-03-16 11:38:16 +000042static DEFINE_MUTEX(op_arm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Will Deacon8c1fc962010-04-30 11:36:54 +010044static struct op_counter_config *counter_config;
45static struct perf_event **perf_events[nr_cpumask_bits];
Matt Fleming3bf101b2010-09-27 20:22:24 +010046static int num_counters;
Will Deacon8c1fc962010-04-30 11:36:54 +010047
48/*
49 * Overflow callback for oprofile.
50 */
51static void op_overflow_handler(struct perf_event *event, int unused,
52 struct perf_sample_data *data, struct pt_regs *regs)
53{
54 int id;
55 u32 cpu = smp_processor_id();
56
Matt Fleming3bf101b2010-09-27 20:22:24 +010057 for (id = 0; id < num_counters; ++id)
Will Deacon8c1fc962010-04-30 11:36:54 +010058 if (perf_events[cpu][id] == event)
59 break;
60
Matt Fleming3bf101b2010-09-27 20:22:24 +010061 if (id != num_counters)
Will Deacon8c1fc962010-04-30 11:36:54 +010062 oprofile_add_sample(regs, id);
63 else
64 pr_warning("oprofile: ignoring spurious overflow "
65 "on cpu %u\n", cpu);
66}
67
68/*
69 * Called by op_arm_setup to create perf attributes to mirror the oprofile
70 * settings in counter_config. Attributes are created as `pinned' events and
71 * so are permanently scheduled on the PMU.
72 */
73static void op_perf_setup(void)
74{
75 int i;
76 u32 size = sizeof(struct perf_event_attr);
77 struct perf_event_attr *attr;
78
Matt Fleming3bf101b2010-09-27 20:22:24 +010079 for (i = 0; i < num_counters; ++i) {
Will Deacon8c1fc962010-04-30 11:36:54 +010080 attr = &counter_config[i].attr;
81 memset(attr, 0, size);
82 attr->type = PERF_TYPE_RAW;
83 attr->size = size;
84 attr->config = counter_config[i].event;
85 attr->sample_period = counter_config[i].count;
86 attr->pinned = 1;
87 }
88}
89
90static int op_create_counter(int cpu, int event)
91{
92 int ret = 0;
93 struct perf_event *pevent;
94
95 if (!counter_config[event].enabled || (perf_events[cpu][event] != NULL))
96 return ret;
97
98 pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
99 cpu, -1,
100 op_overflow_handler);
101
102 if (IS_ERR(pevent)) {
103 ret = PTR_ERR(pevent);
104 } else if (pevent->state != PERF_EVENT_STATE_ACTIVE) {
105 pr_warning("oprofile: failed to enable event %d "
106 "on CPU %d\n", event, cpu);
107 ret = -EBUSY;
108 } else {
109 perf_events[cpu][event] = pevent;
110 }
111
112 return ret;
113}
114
115static void op_destroy_counter(int cpu, int event)
116{
117 struct perf_event *pevent = perf_events[cpu][event];
118
119 if (pevent) {
120 perf_event_release_kernel(pevent);
121 perf_events[cpu][event] = NULL;
122 }
123}
124
125/*
126 * Called by op_arm_start to create active perf events based on the
127 * perviously configured attributes.
128 */
129static int op_perf_start(void)
130{
131 int cpu, event, ret = 0;
132
133 for_each_online_cpu(cpu) {
Matt Fleming3bf101b2010-09-27 20:22:24 +0100134 for (event = 0; event < num_counters; ++event) {
Will Deacon8c1fc962010-04-30 11:36:54 +0100135 ret = op_create_counter(cpu, event);
136 if (ret)
137 goto out;
138 }
139 }
140
141out:
142 return ret;
143}
144
145/*
146 * Called by op_arm_stop at the end of a profiling run.
147 */
148static void op_perf_stop(void)
149{
150 int cpu, event;
151
152 for_each_online_cpu(cpu)
Matt Fleming3bf101b2010-09-27 20:22:24 +0100153 for (event = 0; event < num_counters; ++event)
Will Deacon8c1fc962010-04-30 11:36:54 +0100154 op_destroy_counter(cpu, event);
155}
156
157
Matt Fleming56946332010-10-08 21:42:17 +0100158char *op_name_from_perf_id(void)
Will Deacon8c1fc962010-04-30 11:36:54 +0100159{
Matt Fleming56946332010-10-08 21:42:17 +0100160 enum arm_perf_pmu_ids id = armpmu_get_pmu_id();
161
Will Deacon8c1fc962010-04-30 11:36:54 +0100162 switch (id) {
163 case ARM_PERF_PMU_ID_XSCALE1:
164 return "arm/xscale1";
165 case ARM_PERF_PMU_ID_XSCALE2:
166 return "arm/xscale2";
167 case ARM_PERF_PMU_ID_V6:
168 return "arm/armv6";
169 case ARM_PERF_PMU_ID_V6MP:
170 return "arm/mpcore";
171 case ARM_PERF_PMU_ID_CA8:
172 return "arm/armv7";
173 case ARM_PERF_PMU_ID_CA9:
174 return "arm/armv7-ca9";
175 default:
176 return NULL;
177 }
178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Russell King55f05232005-10-28 14:54:21 +0100180static int op_arm_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 unsigned int i;
183
Matt Fleming3bf101b2010-09-27 20:22:24 +0100184 for (i = 0; i < num_counters; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct dentry *dir;
Russell Kingae92dc92006-03-16 11:32:51 +0000186 char buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 snprintf(buf, sizeof buf, "%d", i);
189 dir = oprofilefs_mkdir(sb, root, buf);
190 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
191 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
192 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
193 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
194 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
195 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
196 }
197
198 return 0;
199}
200
Russell King55f05232005-10-28 14:54:21 +0100201static int op_arm_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 spin_lock(&oprofilefs_lock);
Will Deacon8c1fc962010-04-30 11:36:54 +0100204 op_perf_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 spin_unlock(&oprofilefs_lock);
Will Deacon8c1fc962010-04-30 11:36:54 +0100206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Russell King55f05232005-10-28 14:54:21 +0100209static int op_arm_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int ret = -EBUSY;
212
Russell King93ad7942006-03-16 11:38:16 +0000213 mutex_lock(&op_arm_mutex);
Russell King55f05232005-10-28 14:54:21 +0100214 if (!op_arm_enabled) {
Will Deacon8c1fc962010-04-30 11:36:54 +0100215 ret = 0;
216 op_perf_start();
217 op_arm_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Russell King93ad7942006-03-16 11:38:16 +0000219 mutex_unlock(&op_arm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return ret;
221}
222
Russell King55f05232005-10-28 14:54:21 +0100223static void op_arm_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Russell King93ad7942006-03-16 11:38:16 +0000225 mutex_lock(&op_arm_mutex);
Russell King55f05232005-10-28 14:54:21 +0100226 if (op_arm_enabled)
Will Deacon8c1fc962010-04-30 11:36:54 +0100227 op_perf_stop();
Russell King55f05232005-10-28 14:54:21 +0100228 op_arm_enabled = 0;
Russell King93ad7942006-03-16 11:38:16 +0000229 mutex_unlock(&op_arm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Russell Kingb5893c52005-10-28 14:51:15 +0100232#ifdef CONFIG_PM
Will Deacond1e86d62010-04-30 11:38:39 +0100233static int op_arm_suspend(struct platform_device *dev, pm_message_t state)
Russell Kingb5893c52005-10-28 14:51:15 +0100234{
Russell King93ad7942006-03-16 11:38:16 +0000235 mutex_lock(&op_arm_mutex);
Russell King55f05232005-10-28 14:54:21 +0100236 if (op_arm_enabled)
Will Deacon8c1fc962010-04-30 11:36:54 +0100237 op_perf_stop();
Russell King93ad7942006-03-16 11:38:16 +0000238 mutex_unlock(&op_arm_mutex);
Russell Kingb5893c52005-10-28 14:51:15 +0100239 return 0;
240}
241
Will Deacond1e86d62010-04-30 11:38:39 +0100242static int op_arm_resume(struct platform_device *dev)
Russell Kingb5893c52005-10-28 14:51:15 +0100243{
Russell King93ad7942006-03-16 11:38:16 +0000244 mutex_lock(&op_arm_mutex);
Will Deacon8c1fc962010-04-30 11:36:54 +0100245 if (op_arm_enabled && op_perf_start())
Russell King55f05232005-10-28 14:54:21 +0100246 op_arm_enabled = 0;
Russell King93ad7942006-03-16 11:38:16 +0000247 mutex_unlock(&op_arm_mutex);
Russell Kingb5893c52005-10-28 14:51:15 +0100248 return 0;
249}
250
Will Deacond1e86d62010-04-30 11:38:39 +0100251static struct platform_driver oprofile_driver = {
252 .driver = {
253 .name = "arm-oprofile",
254 },
Russell King55f05232005-10-28 14:54:21 +0100255 .resume = op_arm_resume,
256 .suspend = op_arm_suspend,
Russell Kingb5893c52005-10-28 14:51:15 +0100257};
258
Will Deacond1e86d62010-04-30 11:38:39 +0100259static struct platform_device *oprofile_pdev;
Russell Kingb5893c52005-10-28 14:51:15 +0100260
261static int __init init_driverfs(void)
262{
263 int ret;
264
Will Deacond1e86d62010-04-30 11:38:39 +0100265 ret = platform_driver_register(&oprofile_driver);
266 if (ret)
267 goto out;
Russell Kingb5893c52005-10-28 14:51:15 +0100268
Will Deacond1e86d62010-04-30 11:38:39 +0100269 oprofile_pdev = platform_device_register_simple(
270 oprofile_driver.driver.name, 0, NULL, 0);
271 if (IS_ERR(oprofile_pdev)) {
272 ret = PTR_ERR(oprofile_pdev);
273 platform_driver_unregister(&oprofile_driver);
274 }
275
276out:
Russell Kingb5893c52005-10-28 14:51:15 +0100277 return ret;
278}
279
Will Deaconc7fd2392010-08-29 14:52:00 -0400280static void __exit exit_driverfs(void)
Russell Kingb5893c52005-10-28 14:51:15 +0100281{
Will Deacond1e86d62010-04-30 11:38:39 +0100282 platform_device_unregister(oprofile_pdev);
283 platform_driver_unregister(&oprofile_driver);
Russell Kingb5893c52005-10-28 14:51:15 +0100284}
285#else
Will Deacond1e86d62010-04-30 11:38:39 +0100286static int __init init_driverfs(void) { return 0; }
Russell Kingb5893c52005-10-28 14:51:15 +0100287#define exit_driverfs() do { } while (0)
288#endif /* CONFIG_PM */
289
Will Deacon8c1fc962010-04-30 11:36:54 +0100290static int report_trace(struct stackframe *frame, void *d)
291{
292 unsigned int *depth = d;
293
294 if (*depth) {
295 oprofile_add_trace(frame->pc);
296 (*depth)--;
297 }
298
299 return *depth == 0;
300}
301
302/*
303 * The registers we're interested in are at the end of the variable
304 * length saved register structure. The fp points at the end of this
305 * structure so the address of this struct is:
306 * (struct frame_tail *)(xxx->fp)-1
307 */
308struct frame_tail {
309 struct frame_tail *fp;
310 unsigned long sp;
311 unsigned long lr;
312} __attribute__((packed));
313
314static struct frame_tail* user_backtrace(struct frame_tail *tail)
315{
316 struct frame_tail buftail[2];
317
318 /* Also check accessibility of one struct frame_tail beyond */
319 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
320 return NULL;
321 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
322 return NULL;
323
324 oprofile_add_trace(buftail[0].lr);
325
326 /* frame pointers should strictly progress back up the stack
327 * (towards higher addresses) */
328 if (tail >= buftail[0].fp)
329 return NULL;
330
331 return buftail[0].fp-1;
332}
333
334static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
335{
336 struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
337
338 if (!user_mode(regs)) {
339 struct stackframe frame;
340 frame.fp = regs->ARM_fp;
341 frame.sp = regs->ARM_sp;
342 frame.lr = regs->ARM_lr;
343 frame.pc = regs->ARM_pc;
344 walk_stackframe(&frame, report_trace, &depth);
345 return;
346 }
347
348 while (depth-- && tail && !((unsigned long) tail & 3))
349 tail = user_backtrace(tail);
350}
351
Russell Kingc6b9daf2005-10-28 14:56:04 +0100352int __init oprofile_arch_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Will Deacon8c1fc962010-04-30 11:36:54 +0100354 int cpu, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Robert Richter4cbe75b2010-08-30 18:21:55 +0200356 memset(&perf_events, 0, sizeof(perf_events));
357
Matt Fleming3bf101b2010-09-27 20:22:24 +0100358 num_counters = perf_num_counters();
359 if (num_counters <= 0) {
360 pr_info("oprofile: no performance counters\n");
361 ret = -ENODEV;
362 goto out;
363 }
Richard Purdie1b7b5692007-02-27 12:09:33 +0100364
Matt Fleming3bf101b2010-09-27 20:22:24 +0100365 counter_config = kcalloc(num_counters,
Will Deacon8c1fc962010-04-30 11:36:54 +0100366 sizeof(struct op_counter_config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Will Deacon8c1fc962010-04-30 11:36:54 +0100368 if (!counter_config) {
369 pr_info("oprofile: failed to allocate %d "
Matt Fleming3bf101b2010-09-27 20:22:24 +0100370 "counters\n", num_counters);
Will Deaconc7fd2392010-08-29 14:52:00 -0400371 ret = -ENOMEM;
372 goto out;
Russell Kingc6b9daf2005-10-28 14:56:04 +0100373 }
374
Will Deacond1e86d62010-04-30 11:38:39 +0100375 ret = init_driverfs();
Will Deaconc7fd2392010-08-29 14:52:00 -0400376 if (ret)
377 goto out;
Will Deacond1e86d62010-04-30 11:38:39 +0100378
Will Deacon8c1fc962010-04-30 11:36:54 +0100379 for_each_possible_cpu(cpu) {
Matt Fleming3bf101b2010-09-27 20:22:24 +0100380 perf_events[cpu] = kcalloc(num_counters,
Will Deacon8c1fc962010-04-30 11:36:54 +0100381 sizeof(struct perf_event *), GFP_KERNEL);
382 if (!perf_events[cpu]) {
383 pr_info("oprofile: failed to allocate %d perf events "
Matt Fleming3bf101b2010-09-27 20:22:24 +0100384 "for cpu %d\n", num_counters, cpu);
Will Deaconc7fd2392010-08-29 14:52:00 -0400385 ret = -ENOMEM;
386 goto out;
Will Deacon8c1fc962010-04-30 11:36:54 +0100387 }
388 }
389
Will Deacon8c1fc962010-04-30 11:36:54 +0100390 ops->backtrace = arm_backtrace;
391 ops->create_files = op_arm_create_files;
392 ops->setup = op_arm_setup;
393 ops->start = op_arm_start;
394 ops->stop = op_arm_stop;
395 ops->shutdown = op_arm_stop;
Matt Fleming56946332010-10-08 21:42:17 +0100396 ops->cpu_type = op_name_from_perf_id();
Will Deacon8c1fc962010-04-30 11:36:54 +0100397
398 if (!ops->cpu_type)
399 ret = -ENODEV;
400 else
401 pr_info("oprofile: using %s\n", ops->cpu_type);
402
Will Deaconc7fd2392010-08-29 14:52:00 -0400403out:
404 if (ret) {
405 for_each_possible_cpu(cpu)
406 kfree(perf_events[cpu]);
407 kfree(counter_config);
408 }
409
Russell Kingc6b9daf2005-10-28 14:56:04 +0100410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Will Deaconc7fd2392010-08-29 14:52:00 -0400413void __exit oprofile_arch_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Will Deacon8c1fc962010-04-30 11:36:54 +0100415 int cpu, id;
416 struct perf_event *event;
417
Will Deaconc7fd2392010-08-29 14:52:00 -0400418 for_each_possible_cpu(cpu) {
Matt Fleming3bf101b2010-09-27 20:22:24 +0100419 for (id = 0; id < num_counters; ++id) {
Will Deaconc7fd2392010-08-29 14:52:00 -0400420 event = perf_events[cpu][id];
421 if (event)
422 perf_event_release_kernel(event);
Will Deacon8c1fc962010-04-30 11:36:54 +0100423 }
Will Deaconc7fd2392010-08-29 14:52:00 -0400424
425 kfree(perf_events[cpu]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Will Deacon8c1fc962010-04-30 11:36:54 +0100427
Will Deaconc7fd2392010-08-29 14:52:00 -0400428 kfree(counter_config);
429 exit_driverfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
Will Deacon8c1fc962010-04-30 11:36:54 +0100431#else
432int __init oprofile_arch_init(struct oprofile_operations *ops)
433{
434 pr_info("oprofile: hardware counters not available\n");
435 return -ENODEV;
436}
Will Deaconc7fd2392010-08-29 14:52:00 -0400437void __exit oprofile_arch_exit(void) {}
Will Deacon8c1fc962010-04-30 11:36:54 +0100438#endif /* CONFIG_HW_PERF_EVENTS */