diff options
Diffstat (limited to 'arch/arm/oprofile/common.c')
-rw-r--r-- | arch/arm/oprofile/common.c | 375 |
1 files changed, 309 insertions, 66 deletions
diff --git a/arch/arm/oprofile/common.c b/arch/arm/oprofile/common.c index 3fcd752d6146..0691176899ff 100644 --- a/arch/arm/oprofile/common.c +++ b/arch/arm/oprofile/common.c | |||
@@ -2,32 +2,184 @@ | |||
2 | * @file common.c | 2 | * @file common.c |
3 | * | 3 | * |
4 | * @remark Copyright 2004 Oprofile Authors | 4 | * @remark Copyright 2004 Oprofile Authors |
5 | * @remark Copyright 2010 ARM Ltd. | ||
5 | * @remark Read the file COPYING | 6 | * @remark Read the file COPYING |
6 | * | 7 | * |
7 | * @author Zwane Mwaikambo | 8 | * @author Zwane Mwaikambo |
9 | * @author Will Deacon [move to perf] | ||
8 | */ | 10 | */ |
9 | 11 | ||
12 | #include <linux/cpumask.h> | ||
13 | #include <linux/err.h> | ||
14 | #include <linux/errno.h> | ||
10 | #include <linux/init.h> | 15 | #include <linux/init.h> |
16 | #include <linux/mutex.h> | ||
11 | #include <linux/oprofile.h> | 17 | #include <linux/oprofile.h> |
12 | #include <linux/errno.h> | 18 | #include <linux/perf_event.h> |
19 | #include <linux/platform_device.h> | ||
13 | #include <linux/slab.h> | 20 | #include <linux/slab.h> |
14 | #include <linux/sysdev.h> | 21 | #include <asm/stacktrace.h> |
15 | #include <linux/mutex.h> | 22 | #include <linux/uaccess.h> |
16 | 23 | ||
17 | #include "op_counter.h" | 24 | #include <asm/perf_event.h> |
18 | #include "op_arm_model.h" | 25 | #include <asm/ptrace.h> |
26 | |||
27 | #ifdef CONFIG_HW_PERF_EVENTS | ||
28 | /* | ||
29 | * Per performance monitor configuration as set via oprofilefs. | ||
30 | */ | ||
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; | ||
39 | }; | ||
19 | 40 | ||
20 | static struct op_arm_model_spec *op_arm_model; | ||
21 | static int op_arm_enabled; | 41 | static int op_arm_enabled; |
22 | static DEFINE_MUTEX(op_arm_mutex); | 42 | static DEFINE_MUTEX(op_arm_mutex); |
23 | 43 | ||
24 | struct op_counter_config *counter_config; | 44 | static struct op_counter_config *counter_config; |
45 | static struct perf_event **perf_events[nr_cpumask_bits]; | ||
46 | static int perf_num_counters; | ||
47 | |||
48 | /* | ||
49 | * Overflow callback for oprofile. | ||
50 | */ | ||
51 | static 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 | */ | ||
73 | static 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 | |||
90 | static 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 | |||
115 | static 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 | */ | ||
129 | static int op_perf_start(void) | ||
130 | { | ||
131 | int cpu, event, ret = 0; | ||
132 | |||
133 | for_each_online_cpu(cpu) { | ||
134 | for (event = 0; event < perf_num_counters; ++event) { | ||
135 | ret = op_create_counter(cpu, event); | ||
136 | if (ret) | ||
137 | goto out; | ||
138 | } | ||
139 | } | ||
140 | |||
141 | out: | ||
142 | return ret; | ||
143 | } | ||
144 | |||
145 | /* | ||
146 | * Called by op_arm_stop at the end of a profiling run. | ||
147 | */ | ||
148 | static void op_perf_stop(void) | ||
149 | { | ||
150 | int cpu, event; | ||
151 | |||
152 | for_each_online_cpu(cpu) | ||
153 | for (event = 0; event < perf_num_counters; ++event) | ||
154 | op_destroy_counter(cpu, event); | ||
155 | } | ||
156 | |||
157 | |||
158 | static char *op_name_from_perf_id(enum arm_perf_pmu_ids id) | ||
159 | { | ||
160 | switch (id) { | ||
161 | case ARM_PERF_PMU_ID_XSCALE1: | ||
162 | return "arm/xscale1"; | ||
163 | case ARM_PERF_PMU_ID_XSCALE2: | ||
164 | return "arm/xscale2"; | ||
165 | case ARM_PERF_PMU_ID_V6: | ||
166 | return "arm/armv6"; | ||
167 | case ARM_PERF_PMU_ID_V6MP: | ||
168 | return "arm/mpcore"; | ||
169 | case ARM_PERF_PMU_ID_CA8: | ||
170 | return "arm/armv7"; | ||
171 | case ARM_PERF_PMU_ID_CA9: | ||
172 | return "arm/armv7-ca9"; | ||
173 | default: | ||
174 | return NULL; | ||
175 | } | ||
176 | } | ||
25 | 177 | ||
26 | static int op_arm_create_files(struct super_block *sb, struct dentry *root) | 178 | static int op_arm_create_files(struct super_block *sb, struct dentry *root) |
27 | { | 179 | { |
28 | unsigned int i; | 180 | unsigned int i; |
29 | 181 | ||
30 | for (i = 0; i < op_arm_model->num_counters; i++) { | 182 | for (i = 0; i < perf_num_counters; i++) { |
31 | struct dentry *dir; | 183 | struct dentry *dir; |
32 | char buf[4]; | 184 | char buf[4]; |
33 | 185 | ||
@@ -46,12 +198,10 @@ static int op_arm_create_files(struct super_block *sb, struct dentry *root) | |||
46 | 198 | ||
47 | static int op_arm_setup(void) | 199 | static int op_arm_setup(void) |
48 | { | 200 | { |
49 | int ret; | ||
50 | |||
51 | spin_lock(&oprofilefs_lock); | 201 | spin_lock(&oprofilefs_lock); |
52 | ret = op_arm_model->setup_ctrs(); | 202 | op_perf_setup(); |
53 | spin_unlock(&oprofilefs_lock); | 203 | spin_unlock(&oprofilefs_lock); |
54 | return ret; | 204 | return 0; |
55 | } | 205 | } |
56 | 206 | ||
57 | static int op_arm_start(void) | 207 | static int op_arm_start(void) |
@@ -60,8 +210,9 @@ static int op_arm_start(void) | |||
60 | 210 | ||
61 | mutex_lock(&op_arm_mutex); | 211 | mutex_lock(&op_arm_mutex); |
62 | if (!op_arm_enabled) { | 212 | if (!op_arm_enabled) { |
63 | ret = op_arm_model->start(); | 213 | ret = 0; |
64 | op_arm_enabled = !ret; | 214 | op_perf_start(); |
215 | op_arm_enabled = 1; | ||
65 | } | 216 | } |
66 | mutex_unlock(&op_arm_mutex); | 217 | mutex_unlock(&op_arm_mutex); |
67 | return ret; | 218 | return ret; |
@@ -71,113 +222,205 @@ static void op_arm_stop(void) | |||
71 | { | 222 | { |
72 | mutex_lock(&op_arm_mutex); | 223 | mutex_lock(&op_arm_mutex); |
73 | if (op_arm_enabled) | 224 | if (op_arm_enabled) |
74 | op_arm_model->stop(); | 225 | op_perf_stop(); |
75 | op_arm_enabled = 0; | 226 | op_arm_enabled = 0; |
76 | mutex_unlock(&op_arm_mutex); | 227 | mutex_unlock(&op_arm_mutex); |
77 | } | 228 | } |
78 | 229 | ||
79 | #ifdef CONFIG_PM | 230 | #ifdef CONFIG_PM |
80 | static int op_arm_suspend(struct sys_device *dev, pm_message_t state) | 231 | static int op_arm_suspend(struct platform_device *dev, pm_message_t state) |
81 | { | 232 | { |
82 | mutex_lock(&op_arm_mutex); | 233 | mutex_lock(&op_arm_mutex); |
83 | if (op_arm_enabled) | 234 | if (op_arm_enabled) |
84 | op_arm_model->stop(); | 235 | op_perf_stop(); |
85 | mutex_unlock(&op_arm_mutex); | 236 | mutex_unlock(&op_arm_mutex); |
86 | return 0; | 237 | return 0; |
87 | } | 238 | } |
88 | 239 | ||
89 | static int op_arm_resume(struct sys_device *dev) | 240 | static int op_arm_resume(struct platform_device *dev) |
90 | { | 241 | { |
91 | mutex_lock(&op_arm_mutex); | 242 | mutex_lock(&op_arm_mutex); |
92 | if (op_arm_enabled && op_arm_model->start()) | 243 | if (op_arm_enabled && op_perf_start()) |
93 | op_arm_enabled = 0; | 244 | op_arm_enabled = 0; |
94 | mutex_unlock(&op_arm_mutex); | 245 | mutex_unlock(&op_arm_mutex); |
95 | return 0; | 246 | return 0; |
96 | } | 247 | } |
97 | 248 | ||
98 | static struct sysdev_class oprofile_sysclass = { | 249 | static struct platform_driver oprofile_driver = { |
99 | .name = "oprofile", | 250 | .driver = { |
251 | .name = "arm-oprofile", | ||
252 | }, | ||
100 | .resume = op_arm_resume, | 253 | .resume = op_arm_resume, |
101 | .suspend = op_arm_suspend, | 254 | .suspend = op_arm_suspend, |
102 | }; | 255 | }; |
103 | 256 | ||
104 | static struct sys_device device_oprofile = { | 257 | static struct platform_device *oprofile_pdev; |
105 | .id = 0, | ||
106 | .cls = &oprofile_sysclass, | ||
107 | }; | ||
108 | 258 | ||
109 | static int __init init_driverfs(void) | 259 | static int __init init_driverfs(void) |
110 | { | 260 | { |
111 | int ret; | 261 | int ret; |
112 | 262 | ||
113 | if (!(ret = sysdev_class_register(&oprofile_sysclass))) | 263 | ret = platform_driver_register(&oprofile_driver); |
114 | ret = sysdev_register(&device_oprofile); | 264 | if (ret) |
265 | goto out; | ||
115 | 266 | ||
267 | oprofile_pdev = platform_device_register_simple( | ||
268 | oprofile_driver.driver.name, 0, NULL, 0); | ||
269 | if (IS_ERR(oprofile_pdev)) { | ||
270 | ret = PTR_ERR(oprofile_pdev); | ||
271 | platform_driver_unregister(&oprofile_driver); | ||
272 | } | ||
273 | |||
274 | out: | ||
116 | return ret; | 275 | return ret; |
117 | } | 276 | } |
118 | 277 | ||
119 | static void exit_driverfs(void) | 278 | static void exit_driverfs(void) |
120 | { | 279 | { |
121 | sysdev_unregister(&device_oprofile); | 280 | platform_device_unregister(oprofile_pdev); |
122 | sysdev_class_unregister(&oprofile_sysclass); | 281 | platform_driver_unregister(&oprofile_driver); |
123 | } | 282 | } |
124 | #else | 283 | #else |
125 | #define init_driverfs() do { } while (0) | 284 | static int __init init_driverfs(void) { return 0; } |
126 | #define exit_driverfs() do { } while (0) | 285 | #define exit_driverfs() do { } while (0) |
127 | #endif /* CONFIG_PM */ | 286 | #endif /* CONFIG_PM */ |
128 | 287 | ||
129 | int __init oprofile_arch_init(struct oprofile_operations *ops) | 288 | static int report_trace(struct stackframe *frame, void *d) |
130 | { | 289 | { |
131 | struct op_arm_model_spec *spec = NULL; | 290 | unsigned int *depth = d; |
132 | int ret = -ENODEV; | ||
133 | 291 | ||
134 | ops->backtrace = arm_backtrace; | 292 | if (*depth) { |
293 | oprofile_add_trace(frame->pc); | ||
294 | (*depth)--; | ||
295 | } | ||
135 | 296 | ||
136 | #ifdef CONFIG_CPU_XSCALE | 297 | return *depth == 0; |
137 | spec = &op_xscale_spec; | 298 | } |
138 | #endif | ||
139 | 299 | ||
140 | #ifdef CONFIG_OPROFILE_ARMV6 | 300 | /* |
141 | spec = &op_armv6_spec; | 301 | * The registers we're interested in are at the end of the variable |
142 | #endif | 302 | * length saved register structure. The fp points at the end of this |
303 | * structure so the address of this struct is: | ||
304 | * (struct frame_tail *)(xxx->fp)-1 | ||
305 | */ | ||
306 | struct frame_tail { | ||
307 | struct frame_tail *fp; | ||
308 | unsigned long sp; | ||
309 | unsigned long lr; | ||
310 | } __attribute__((packed)); | ||
143 | 311 | ||
144 | #ifdef CONFIG_OPROFILE_MPCORE | 312 | static struct frame_tail* user_backtrace(struct frame_tail *tail) |
145 | spec = &op_mpcore_spec; | 313 | { |
146 | #endif | 314 | struct frame_tail buftail[2]; |
147 | 315 | ||
148 | #ifdef CONFIG_OPROFILE_ARMV7 | 316 | /* Also check accessibility of one struct frame_tail beyond */ |
149 | spec = &op_armv7_spec; | 317 | if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) |
150 | #endif | 318 | return NULL; |
319 | if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) | ||
320 | return NULL; | ||
151 | 321 | ||
152 | if (spec) { | 322 | oprofile_add_trace(buftail[0].lr); |
153 | ret = spec->init(); | ||
154 | if (ret < 0) | ||
155 | return ret; | ||
156 | 323 | ||
157 | counter_config = kcalloc(spec->num_counters, sizeof(struct op_counter_config), | 324 | /* frame pointers should strictly progress back up the stack |
158 | GFP_KERNEL); | 325 | * (towards higher addresses) */ |
159 | if (!counter_config) | 326 | if (tail >= buftail[0].fp) |
160 | return -ENOMEM; | 327 | return NULL; |
161 | 328 | ||
162 | op_arm_model = spec; | 329 | return buftail[0].fp-1; |
163 | init_driverfs(); | 330 | } |
164 | ops->create_files = op_arm_create_files; | 331 | |
165 | ops->setup = op_arm_setup; | 332 | static void arm_backtrace(struct pt_regs * const regs, unsigned int depth) |
166 | ops->shutdown = op_arm_stop; | 333 | { |
167 | ops->start = op_arm_start; | 334 | struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1; |
168 | ops->stop = op_arm_stop; | 335 | |
169 | ops->cpu_type = op_arm_model->name; | 336 | if (!user_mode(regs)) { |
170 | printk(KERN_INFO "oprofile: using %s\n", spec->name); | 337 | struct stackframe frame; |
338 | frame.fp = regs->ARM_fp; | ||
339 | frame.sp = regs->ARM_sp; | ||
340 | frame.lr = regs->ARM_lr; | ||
341 | frame.pc = regs->ARM_pc; | ||
342 | walk_stackframe(&frame, report_trace, &depth); | ||
343 | return; | ||
171 | } | 344 | } |
172 | 345 | ||
346 | while (depth-- && tail && !((unsigned long) tail & 3)) | ||
347 | tail = user_backtrace(tail); | ||
348 | } | ||
349 | |||
350 | int __init oprofile_arch_init(struct oprofile_operations *ops) | ||
351 | { | ||
352 | int cpu, ret = 0; | ||
353 | |||
354 | perf_num_counters = armpmu_get_max_events(); | ||
355 | |||
356 | counter_config = kcalloc(perf_num_counters, | ||
357 | sizeof(struct op_counter_config), GFP_KERNEL); | ||
358 | |||
359 | if (!counter_config) { | ||
360 | pr_info("oprofile: failed to allocate %d " | ||
361 | "counters\n", perf_num_counters); | ||
362 | return -ENOMEM; | ||
363 | } | ||
364 | |||
365 | ret = init_driverfs(); | ||
366 | if (ret) { | ||
367 | kfree(counter_config); | ||
368 | return ret; | ||
369 | } | ||
370 | |||
371 | for_each_possible_cpu(cpu) { | ||
372 | perf_events[cpu] = kcalloc(perf_num_counters, | ||
373 | sizeof(struct perf_event *), GFP_KERNEL); | ||
374 | if (!perf_events[cpu]) { | ||
375 | pr_info("oprofile: failed to allocate %d perf events " | ||
376 | "for cpu %d\n", perf_num_counters, cpu); | ||
377 | while (--cpu >= 0) | ||
378 | kfree(perf_events[cpu]); | ||
379 | return -ENOMEM; | ||
380 | } | ||
381 | } | ||
382 | |||
383 | ops->backtrace = arm_backtrace; | ||
384 | ops->create_files = op_arm_create_files; | ||
385 | ops->setup = op_arm_setup; | ||
386 | ops->start = op_arm_start; | ||
387 | ops->stop = op_arm_stop; | ||
388 | ops->shutdown = op_arm_stop; | ||
389 | ops->cpu_type = op_name_from_perf_id(armpmu_get_pmu_id()); | ||
390 | |||
391 | if (!ops->cpu_type) | ||
392 | ret = -ENODEV; | ||
393 | else | ||
394 | pr_info("oprofile: using %s\n", ops->cpu_type); | ||
395 | |||
173 | return ret; | 396 | return ret; |
174 | } | 397 | } |
175 | 398 | ||
176 | void oprofile_arch_exit(void) | 399 | void oprofile_arch_exit(void) |
177 | { | 400 | { |
178 | if (op_arm_model) { | 401 | int cpu, id; |
402 | struct perf_event *event; | ||
403 | |||
404 | if (*perf_events) { | ||
179 | exit_driverfs(); | 405 | exit_driverfs(); |
180 | op_arm_model = NULL; | 406 | for_each_possible_cpu(cpu) { |
407 | for (id = 0; id < perf_num_counters; ++id) { | ||
408 | event = perf_events[cpu][id]; | ||
409 | if (event != NULL) | ||
410 | perf_event_release_kernel(event); | ||
411 | } | ||
412 | kfree(perf_events[cpu]); | ||
413 | } | ||
181 | } | 414 | } |
182 | kfree(counter_config); | 415 | |
416 | if (counter_config) | ||
417 | kfree(counter_config); | ||
418 | } | ||
419 | #else | ||
420 | int __init oprofile_arch_init(struct oprofile_operations *ops) | ||
421 | { | ||
422 | pr_info("oprofile: hardware counters not available\n"); | ||
423 | return -ENODEV; | ||
183 | } | 424 | } |
425 | void oprofile_arch_exit(void) {} | ||
426 | #endif /* CONFIG_HW_PERF_EVENTS */ | ||