blob: 72e09eb642dd7c7a00d47fce210bb1290e926fd9 [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];
46static int perf_num_counters;
47
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
57 for (id = 0; id < perf_num_counters; ++id)
58 if (perf_events[cpu][id] == event)
59 break;
60
61 if (id != perf_num_counters)
62 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
79 for (i = 0; i < perf_num_counters; ++i) {
80 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) {
Robert Richter98d943b2010-09-29 16:52:25 +0200105 perf_event_release_kernel(pevent);
Will Deacon8c1fc962010-04-30 11:36:54 +0100106 pr_warning("oprofile: failed to enable event %d "
107 "on CPU %d\n", event, cpu);
108 ret = -EBUSY;
109 } else {
110 perf_events[cpu][event] = pevent;
111 }
112
113 return ret;
114}
115
116static void op_destroy_counter(int cpu, int event)
117{
118 struct perf_event *pevent = perf_events[cpu][event];
119
120 if (pevent) {
121 perf_event_release_kernel(pevent);
122 perf_events[cpu][event] = NULL;
123 }
124}
125
126/*
127 * Called by op_arm_start to create active perf events based on the
128 * perviously configured attributes.
129 */
130static int op_perf_start(void)
131{
132 int cpu, event, ret = 0;
133
134 for_each_online_cpu(cpu) {
135 for (event = 0; event < perf_num_counters; ++event) {
136 ret = op_create_counter(cpu, event);
137 if (ret)
138 goto out;
139 }
140 }
141
142out:
143 return ret;
144}
145
146/*
147 * Called by op_arm_stop at the end of a profiling run.
148 */
149static void op_perf_stop(void)
150{
151 int cpu, event;
152
153 for_each_online_cpu(cpu)
154 for (event = 0; event < perf_num_counters; ++event)
155 op_destroy_counter(cpu, event);
156}
157
158
159static char *op_name_from_perf_id(enum arm_perf_pmu_ids id)
160{
161 switch (id) {
162 case ARM_PERF_PMU_ID_XSCALE1:
163 return "arm/xscale1";
164 case ARM_PERF_PMU_ID_XSCALE2:
165 return "arm/xscale2";
166 case ARM_PERF_PMU_ID_V6:
167 return "arm/armv6";
168 case ARM_PERF_PMU_ID_V6MP:
169 return "arm/mpcore";
170 case ARM_PERF_PMU_ID_CA8:
171 return "arm/armv7";
172 case ARM_PERF_PMU_ID_CA9:
173 return "arm/armv7-ca9";
174 default:
175 return NULL;
176 }
177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Russell King55f05232005-10-28 14:54:21 +0100179static int op_arm_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 unsigned int i;
182
Will Deacon8c1fc962010-04-30 11:36:54 +0100183 for (i = 0; i < perf_num_counters; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct dentry *dir;
Russell Kingae92dc92006-03-16 11:32:51 +0000185 char buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 snprintf(buf, sizeof buf, "%d", i);
188 dir = oprofilefs_mkdir(sb, root, buf);
189 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
190 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
191 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
192 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
193 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
194 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
195 }
196
197 return 0;
198}
199
Russell King55f05232005-10-28 14:54:21 +0100200static int op_arm_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 spin_lock(&oprofilefs_lock);
Will Deacon8c1fc962010-04-30 11:36:54 +0100203 op_perf_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 spin_unlock(&oprofilefs_lock);
Will Deacon8c1fc962010-04-30 11:36:54 +0100205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Russell King55f05232005-10-28 14:54:21 +0100208static int op_arm_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 int ret = -EBUSY;
211
Russell King93ad7942006-03-16 11:38:16 +0000212 mutex_lock(&op_arm_mutex);
Russell King55f05232005-10-28 14:54:21 +0100213 if (!op_arm_enabled) {
Will Deacon8c1fc962010-04-30 11:36:54 +0100214 ret = 0;
215 op_perf_start();
216 op_arm_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Russell King93ad7942006-03-16 11:38:16 +0000218 mutex_unlock(&op_arm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return ret;
220}
221
Russell King55f05232005-10-28 14:54:21 +0100222static void op_arm_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Russell King93ad7942006-03-16 11:38:16 +0000224 mutex_lock(&op_arm_mutex);
Russell King55f05232005-10-28 14:54:21 +0100225 if (op_arm_enabled)
Will Deacon8c1fc962010-04-30 11:36:54 +0100226 op_perf_stop();
Russell King55f05232005-10-28 14:54:21 +0100227 op_arm_enabled = 0;
Russell King93ad7942006-03-16 11:38:16 +0000228 mutex_unlock(&op_arm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Russell Kingb5893c52005-10-28 14:51:15 +0100231#ifdef CONFIG_PM
Will Deacond1e86d62010-04-30 11:38:39 +0100232static int op_arm_suspend(struct platform_device *dev, pm_message_t state)
Russell Kingb5893c52005-10-28 14:51:15 +0100233{
Russell King93ad7942006-03-16 11:38:16 +0000234 mutex_lock(&op_arm_mutex);
Russell King55f05232005-10-28 14:54:21 +0100235 if (op_arm_enabled)
Will Deacon8c1fc962010-04-30 11:36:54 +0100236 op_perf_stop();
Russell King93ad7942006-03-16 11:38:16 +0000237 mutex_unlock(&op_arm_mutex);
Russell Kingb5893c52005-10-28 14:51:15 +0100238 return 0;
239}
240
Will Deacond1e86d62010-04-30 11:38:39 +0100241static int op_arm_resume(struct platform_device *dev)
Russell Kingb5893c52005-10-28 14:51:15 +0100242{
Russell King93ad7942006-03-16 11:38:16 +0000243 mutex_lock(&op_arm_mutex);
Will Deacon8c1fc962010-04-30 11:36:54 +0100244 if (op_arm_enabled && op_perf_start())
Russell King55f05232005-10-28 14:54:21 +0100245 op_arm_enabled = 0;
Russell King93ad7942006-03-16 11:38:16 +0000246 mutex_unlock(&op_arm_mutex);
Russell Kingb5893c52005-10-28 14:51:15 +0100247 return 0;
248}
249
Will Deacond1e86d62010-04-30 11:38:39 +0100250static struct platform_driver oprofile_driver = {
251 .driver = {
252 .name = "arm-oprofile",
253 },
Russell King55f05232005-10-28 14:54:21 +0100254 .resume = op_arm_resume,
255 .suspend = op_arm_suspend,
Russell Kingb5893c52005-10-28 14:51:15 +0100256};
257
Will Deacond1e86d62010-04-30 11:38:39 +0100258static struct platform_device *oprofile_pdev;
Russell Kingb5893c52005-10-28 14:51:15 +0100259
260static int __init init_driverfs(void)
261{
262 int ret;
263
Will Deacond1e86d62010-04-30 11:38:39 +0100264 ret = platform_driver_register(&oprofile_driver);
265 if (ret)
266 goto out;
Russell Kingb5893c52005-10-28 14:51:15 +0100267
Will Deacond1e86d62010-04-30 11:38:39 +0100268 oprofile_pdev = platform_device_register_simple(
269 oprofile_driver.driver.name, 0, NULL, 0);
270 if (IS_ERR(oprofile_pdev)) {
271 ret = PTR_ERR(oprofile_pdev);
272 platform_driver_unregister(&oprofile_driver);
273 }
274
275out:
Russell Kingb5893c52005-10-28 14:51:15 +0100276 return ret;
277}
278
279static void exit_driverfs(void)
280{
Will Deacond1e86d62010-04-30 11:38:39 +0100281 platform_device_unregister(oprofile_pdev);
282 platform_driver_unregister(&oprofile_driver);
Russell Kingb5893c52005-10-28 14:51:15 +0100283}
284#else
Will Deacond1e86d62010-04-30 11:38:39 +0100285static int __init init_driverfs(void) { return 0; }
Russell Kingb5893c52005-10-28 14:51:15 +0100286#define exit_driverfs() do { } while (0)
287#endif /* CONFIG_PM */
288
Will Deacon8c1fc962010-04-30 11:36:54 +0100289static int report_trace(struct stackframe *frame, void *d)
290{
291 unsigned int *depth = d;
292
293 if (*depth) {
294 oprofile_add_trace(frame->pc);
295 (*depth)--;
296 }
297
298 return *depth == 0;
299}
300
301/*
302 * The registers we're interested in are at the end of the variable
303 * length saved register structure. The fp points at the end of this
304 * structure so the address of this struct is:
305 * (struct frame_tail *)(xxx->fp)-1
306 */
307struct frame_tail {
308 struct frame_tail *fp;
309 unsigned long sp;
310 unsigned long lr;
311} __attribute__((packed));
312
313static struct frame_tail* user_backtrace(struct frame_tail *tail)
314{
315 struct frame_tail buftail[2];
316
317 /* Also check accessibility of one struct frame_tail beyond */
318 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
319 return NULL;
320 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
321 return NULL;
322
323 oprofile_add_trace(buftail[0].lr);
324
325 /* frame pointers should strictly progress back up the stack
326 * (towards higher addresses) */
327 if (tail >= buftail[0].fp)
328 return NULL;
329
330 return buftail[0].fp-1;
331}
332
333static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
334{
335 struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
336
337 if (!user_mode(regs)) {
338 struct stackframe frame;
339 frame.fp = regs->ARM_fp;
340 frame.sp = regs->ARM_sp;
341 frame.lr = regs->ARM_lr;
342 frame.pc = regs->ARM_pc;
343 walk_stackframe(&frame, report_trace, &depth);
344 return;
345 }
346
347 while (depth-- && tail && !((unsigned long) tail & 3))
348 tail = user_backtrace(tail);
349}
350
Russell Kingc6b9daf2005-10-28 14:56:04 +0100351int __init oprofile_arch_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Will Deacon8c1fc962010-04-30 11:36:54 +0100353 int cpu, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Will Deacon8c1fc962010-04-30 11:36:54 +0100355 perf_num_counters = armpmu_get_max_events();
Richard Purdie1b7b5692007-02-27 12:09:33 +0100356
Will Deacon8c1fc962010-04-30 11:36:54 +0100357 counter_config = kcalloc(perf_num_counters,
358 sizeof(struct op_counter_config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Will Deacon8c1fc962010-04-30 11:36:54 +0100360 if (!counter_config) {
361 pr_info("oprofile: failed to allocate %d "
362 "counters\n", perf_num_counters);
363 return -ENOMEM;
Russell Kingc6b9daf2005-10-28 14:56:04 +0100364 }
365
Will Deacond1e86d62010-04-30 11:38:39 +0100366 ret = init_driverfs();
367 if (ret) {
368 kfree(counter_config);
Robert Richter98d943b2010-09-29 16:52:25 +0200369 counter_config = NULL;
Will Deacond1e86d62010-04-30 11:38:39 +0100370 return ret;
371 }
372
Will Deacon8c1fc962010-04-30 11:36:54 +0100373 for_each_possible_cpu(cpu) {
374 perf_events[cpu] = kcalloc(perf_num_counters,
375 sizeof(struct perf_event *), GFP_KERNEL);
376 if (!perf_events[cpu]) {
377 pr_info("oprofile: failed to allocate %d perf events "
378 "for cpu %d\n", perf_num_counters, cpu);
379 while (--cpu >= 0)
380 kfree(perf_events[cpu]);
381 return -ENOMEM;
382 }
383 }
384
Will Deacon8c1fc962010-04-30 11:36:54 +0100385 ops->backtrace = arm_backtrace;
386 ops->create_files = op_arm_create_files;
387 ops->setup = op_arm_setup;
388 ops->start = op_arm_start;
389 ops->stop = op_arm_stop;
390 ops->shutdown = op_arm_stop;
391 ops->cpu_type = op_name_from_perf_id(armpmu_get_pmu_id());
392
393 if (!ops->cpu_type)
394 ret = -ENODEV;
395 else
396 pr_info("oprofile: using %s\n", ops->cpu_type);
397
Russell Kingc6b9daf2005-10-28 14:56:04 +0100398 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Russell Kingc6b9daf2005-10-28 14:56:04 +0100401void oprofile_arch_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Will Deacon8c1fc962010-04-30 11:36:54 +0100403 int cpu, id;
404 struct perf_event *event;
405
406 if (*perf_events) {
Will Deacon8c1fc962010-04-30 11:36:54 +0100407 for_each_possible_cpu(cpu) {
408 for (id = 0; id < perf_num_counters; ++id) {
409 event = perf_events[cpu][id];
410 if (event != NULL)
411 perf_event_release_kernel(event);
412 }
413 kfree(perf_events[cpu]);
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Will Deacon8c1fc962010-04-30 11:36:54 +0100416
Robert Richter98d943b2010-09-29 16:52:25 +0200417 if (counter_config) {
Will Deacon8c1fc962010-04-30 11:36:54 +0100418 kfree(counter_config);
Robert Richter98d943b2010-09-29 16:52:25 +0200419 exit_driverfs();
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
Will Deacon8c1fc962010-04-30 11:36:54 +0100422#else
423int __init oprofile_arch_init(struct oprofile_operations *ops)
424{
425 pr_info("oprofile: hardware counters not available\n");
426 return -ENODEV;
427}
428void oprofile_arch_exit(void) {}
429#endif /* CONFIG_HW_PERF_EVENTS */