ARM: oprofile: Move non-ARM code into separate init/exit
[linux-2.6.git] / arch / arm / oprofile / common.c
blob8718311cb530ccfd237ec39a062f2951dba44601
1 /**
2 * @file common.c
4 * @remark Copyright 2004 Oprofile Authors
5 * @remark Copyright 2010 ARM Ltd.
6 * @remark Read the file COPYING
8 * @author Zwane Mwaikambo
9 * @author Will Deacon [move to perf]
12 #include <linux/cpumask.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/mutex.h>
17 #include <linux/oprofile.h>
18 #include <linux/perf_event.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <asm/stacktrace.h>
22 #include <linux/uaccess.h>
24 #include <asm/perf_event.h>
25 #include <asm/ptrace.h>
27 #ifdef CONFIG_HW_PERF_EVENTS
29 * Per performance monitor configuration as set via oprofilefs.
31 struct 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;
41 static int oprofile_perf_enabled;
42 static DEFINE_MUTEX(oprofile_perf_mutex);
44 static struct op_counter_config *counter_config;
45 static struct perf_event **perf_events[nr_cpumask_bits];
46 static int num_counters;
49 * Overflow callback for oprofile.
51 static void op_overflow_handler(struct perf_event *event, int unused,
52 struct perf_sample_data *data, struct pt_regs *regs)
54 int id;
55 u32 cpu = smp_processor_id();
57 for (id = 0; id < num_counters; ++id)
58 if (perf_events[cpu][id] == event)
59 break;
61 if (id != num_counters)
62 oprofile_add_sample(regs, id);
63 else
64 pr_warning("oprofile: ignoring spurious overflow "
65 "on cpu %u\n", cpu);
69 * Called by oprofile_perf_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.
73 static void op_perf_setup(void)
75 int i;
76 u32 size = sizeof(struct perf_event_attr);
77 struct perf_event_attr *attr;
79 for (i = 0; i < 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;
90 static int op_create_counter(int cpu, int event)
92 int ret = 0;
93 struct perf_event *pevent;
95 if (!counter_config[event].enabled || (perf_events[cpu][event] != NULL))
96 return ret;
98 pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
99 cpu, -1,
100 op_overflow_handler);
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;
112 return ret;
115 static void op_destroy_counter(int cpu, int event)
117 struct perf_event *pevent = perf_events[cpu][event];
119 if (pevent) {
120 perf_event_release_kernel(pevent);
121 perf_events[cpu][event] = NULL;
126 * Called by oprofile_perf_start to create active perf events based on the
127 * perviously configured attributes.
129 static int op_perf_start(void)
131 int cpu, event, ret = 0;
133 for_each_online_cpu(cpu) {
134 for (event = 0; event < num_counters; ++event) {
135 ret = op_create_counter(cpu, event);
136 if (ret)
137 goto out;
141 out:
142 return ret;
146 * Called by oprofile_perf_stop at the end of a profiling run.
148 static void op_perf_stop(void)
150 int cpu, event;
152 for_each_online_cpu(cpu)
153 for (event = 0; event < num_counters; ++event)
154 op_destroy_counter(cpu, event);
158 char *op_name_from_perf_id(void)
160 enum arm_perf_pmu_ids id = armpmu_get_pmu_id();
162 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;
180 static int oprofile_perf_create_files(struct super_block *sb, struct dentry *root)
182 unsigned int i;
184 for (i = 0; i < num_counters; i++) {
185 struct dentry *dir;
186 char buf[4];
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);
198 return 0;
201 static int oprofile_perf_setup(void)
203 spin_lock(&oprofilefs_lock);
204 op_perf_setup();
205 spin_unlock(&oprofilefs_lock);
206 return 0;
209 static int oprofile_perf_start(void)
211 int ret = -EBUSY;
213 mutex_lock(&oprofile_perf_mutex);
214 if (!oprofile_perf_enabled) {
215 ret = 0;
216 op_perf_start();
217 oprofile_perf_enabled = 1;
219 mutex_unlock(&oprofile_perf_mutex);
220 return ret;
223 static void oprofile_perf_stop(void)
225 mutex_lock(&oprofile_perf_mutex);
226 if (oprofile_perf_enabled)
227 op_perf_stop();
228 oprofile_perf_enabled = 0;
229 mutex_unlock(&oprofile_perf_mutex);
232 #ifdef CONFIG_PM
233 static int oprofile_perf_suspend(struct platform_device *dev, pm_message_t state)
235 mutex_lock(&oprofile_perf_mutex);
236 if (oprofile_perf_enabled)
237 op_perf_stop();
238 mutex_unlock(&oprofile_perf_mutex);
239 return 0;
242 static int oprofile_perf_resume(struct platform_device *dev)
244 mutex_lock(&oprofile_perf_mutex);
245 if (oprofile_perf_enabled && op_perf_start())
246 oprofile_perf_enabled = 0;
247 mutex_unlock(&oprofile_perf_mutex);
248 return 0;
251 static struct platform_driver oprofile_driver = {
252 .driver = {
253 .name = "oprofile-perf",
255 .resume = oprofile_perf_resume,
256 .suspend = oprofile_perf_suspend,
259 static struct platform_device *oprofile_pdev;
261 static int __init init_driverfs(void)
263 int ret;
265 ret = platform_driver_register(&oprofile_driver);
266 if (ret)
267 goto out;
269 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);
276 out:
277 return ret;
280 static void __exit exit_driverfs(void)
282 platform_device_unregister(oprofile_pdev);
283 platform_driver_unregister(&oprofile_driver);
285 #else
286 static int __init init_driverfs(void) { return 0; }
287 #define exit_driverfs() do { } while (0)
288 #endif /* CONFIG_PM */
290 static int report_trace(struct stackframe *frame, void *d)
292 unsigned int *depth = d;
294 if (*depth) {
295 oprofile_add_trace(frame->pc);
296 (*depth)--;
299 return *depth == 0;
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
308 struct frame_tail {
309 struct frame_tail *fp;
310 unsigned long sp;
311 unsigned long lr;
312 } __attribute__((packed));
314 static struct frame_tail* user_backtrace(struct frame_tail *tail)
316 struct frame_tail buftail[2];
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;
324 oprofile_add_trace(buftail[0].lr);
326 /* frame pointers should strictly progress back up the stack
327 * (towards higher addresses) */
328 if (tail >= buftail[0].fp)
329 return NULL;
331 return buftail[0].fp-1;
334 static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
336 struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
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;
348 while (depth-- && tail && !((unsigned long) tail & 3))
349 tail = user_backtrace(tail);
352 int __init oprofile_perf_init(struct oprofile_operations *ops)
354 int cpu, ret = 0;
356 memset(&perf_events, 0, sizeof(perf_events));
358 num_counters = perf_num_counters();
359 if (num_counters <= 0) {
360 pr_info("oprofile: no performance counters\n");
361 ret = -ENODEV;
362 goto out;
365 counter_config = kcalloc(num_counters,
366 sizeof(struct op_counter_config), GFP_KERNEL);
368 if (!counter_config) {
369 pr_info("oprofile: failed to allocate %d "
370 "counters\n", num_counters);
371 ret = -ENOMEM;
372 goto out;
375 ret = init_driverfs();
376 if (ret)
377 goto out;
379 for_each_possible_cpu(cpu) {
380 perf_events[cpu] = kcalloc(num_counters,
381 sizeof(struct perf_event *), GFP_KERNEL);
382 if (!perf_events[cpu]) {
383 pr_info("oprofile: failed to allocate %d perf events "
384 "for cpu %d\n", num_counters, cpu);
385 ret = -ENOMEM;
386 goto out;
390 ops->create_files = oprofile_perf_create_files;
391 ops->setup = oprofile_perf_setup;
392 ops->start = oprofile_perf_start;
393 ops->stop = oprofile_perf_stop;
394 ops->shutdown = oprofile_perf_stop;
395 ops->cpu_type = op_name_from_perf_id();
397 if (!ops->cpu_type)
398 ret = -ENODEV;
399 else
400 pr_info("oprofile: using %s\n", ops->cpu_type);
402 out:
403 if (ret) {
404 for_each_possible_cpu(cpu)
405 kfree(perf_events[cpu]);
406 kfree(counter_config);
409 return ret;
412 int __init oprofile_arch_init(struct oprofile_operations *ops)
414 ops->backtrace = arm_backtrace;
416 return oprofile_perf_init(ops);
419 void __exit oprofile_perf_exit(void)
421 int cpu, id;
422 struct perf_event *event;
424 for_each_possible_cpu(cpu) {
425 for (id = 0; id < num_counters; ++id) {
426 event = perf_events[cpu][id];
427 if (event)
428 perf_event_release_kernel(event);
431 kfree(perf_events[cpu]);
434 kfree(counter_config);
435 exit_driverfs();
438 void __exit oprofile_arch_exit(void)
440 oprofile_perf_exit();
442 #else
443 int __init oprofile_arch_init(struct oprofile_operations *ops)
445 pr_info("oprofile: hardware counters not available\n");
446 return -ENODEV;
448 void __exit oprofile_arch_exit(void) {}
449 #endif /* CONFIG_HW_PERF_EVENTS */