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/errno.h>
14 #include <linux/init.h>
15 #include <linux/mutex.h>
16 #include <linux/oprofile.h>
17 #include <linux/perf_event.h>
18 #include <linux/slab.h>
19 #include <linux/sysdev.h>
20 #include <asm/stacktrace.h>
21 #include <linux/uaccess.h>
23 #include <asm/perf_event.h>
24 #include <asm/ptrace.h>
26 #ifdef CONFIG_HW_PERF_EVENTS
28 * Per performance monitor configuration as set via oprofilefs.
30 struct op_counter_config
{
32 unsigned long enabled
;
34 unsigned long unit_mask
;
37 struct perf_event_attr attr
;
40 static int op_arm_enabled
;
41 static DEFINE_MUTEX(op_arm_mutex
);
43 static struct op_counter_config
*counter_config
;
44 static struct perf_event
**perf_events
[nr_cpumask_bits
];
45 static int perf_num_counters
;
48 * Overflow callback for oprofile.
50 static void op_overflow_handler(struct perf_event
*event
, int unused
,
51 struct perf_sample_data
*data
, struct pt_regs
*regs
)
54 u32 cpu
= smp_processor_id();
56 for (id
= 0; id
< perf_num_counters
; ++id
)
57 if (perf_events
[cpu
][id
] == event
)
60 if (id
!= perf_num_counters
)
61 oprofile_add_sample(regs
, id
);
63 pr_warning("oprofile: ignoring spurious overflow "
68 * Called by op_arm_setup to create perf attributes to mirror the oprofile
69 * settings in counter_config. Attributes are created as `pinned' events and
70 * so are permanently scheduled on the PMU.
72 static void op_perf_setup(void)
75 u32 size
= sizeof(struct perf_event_attr
);
76 struct perf_event_attr
*attr
;
78 for (i
= 0; i
< perf_num_counters
; ++i
) {
79 attr
= &counter_config
[i
].attr
;
80 memset(attr
, 0, size
);
81 attr
->type
= PERF_TYPE_RAW
;
83 attr
->config
= counter_config
[i
].event
;
84 attr
->sample_period
= counter_config
[i
].count
;
89 static int op_create_counter(int cpu
, int event
)
92 struct perf_event
*pevent
;
94 if (!counter_config
[event
].enabled
|| (perf_events
[cpu
][event
] != NULL
))
97 pevent
= perf_event_create_kernel_counter(&counter_config
[event
].attr
,
101 if (IS_ERR(pevent
)) {
102 ret
= PTR_ERR(pevent
);
103 } else if (pevent
->state
!= PERF_EVENT_STATE_ACTIVE
) {
104 pr_warning("oprofile: failed to enable event %d "
105 "on CPU %d\n", event
, cpu
);
108 perf_events
[cpu
][event
] = pevent
;
114 static void op_destroy_counter(int cpu
, int event
)
116 struct perf_event
*pevent
= perf_events
[cpu
][event
];
119 perf_event_release_kernel(pevent
);
120 perf_events
[cpu
][event
] = NULL
;
125 * Called by op_arm_start to create active perf events based on the
126 * perviously configured attributes.
128 static int op_perf_start(void)
130 int cpu
, event
, ret
= 0;
132 for_each_online_cpu(cpu
) {
133 for (event
= 0; event
< perf_num_counters
; ++event
) {
134 ret
= op_create_counter(cpu
, event
);
145 * Called by op_arm_stop at the end of a profiling run.
147 static void op_perf_stop(void)
151 for_each_online_cpu(cpu
)
152 for (event
= 0; event
< perf_num_counters
; ++event
)
153 op_destroy_counter(cpu
, event
);
157 static char *op_name_from_perf_id(enum arm_perf_pmu_ids id
)
160 case ARM_PERF_PMU_ID_XSCALE1
:
161 return "arm/xscale1";
162 case ARM_PERF_PMU_ID_XSCALE2
:
163 return "arm/xscale2";
164 case ARM_PERF_PMU_ID_V6
:
166 case ARM_PERF_PMU_ID_V6MP
:
168 case ARM_PERF_PMU_ID_CA8
:
170 case ARM_PERF_PMU_ID_CA9
:
171 return "arm/armv7-ca9";
177 static int op_arm_create_files(struct super_block
*sb
, struct dentry
*root
)
181 for (i
= 0; i
< perf_num_counters
; i
++) {
185 snprintf(buf
, sizeof buf
, "%d", i
);
186 dir
= oprofilefs_mkdir(sb
, root
, buf
);
187 oprofilefs_create_ulong(sb
, dir
, "enabled", &counter_config
[i
].enabled
);
188 oprofilefs_create_ulong(sb
, dir
, "event", &counter_config
[i
].event
);
189 oprofilefs_create_ulong(sb
, dir
, "count", &counter_config
[i
].count
);
190 oprofilefs_create_ulong(sb
, dir
, "unit_mask", &counter_config
[i
].unit_mask
);
191 oprofilefs_create_ulong(sb
, dir
, "kernel", &counter_config
[i
].kernel
);
192 oprofilefs_create_ulong(sb
, dir
, "user", &counter_config
[i
].user
);
198 static int op_arm_setup(void)
200 spin_lock(&oprofilefs_lock
);
202 spin_unlock(&oprofilefs_lock
);
206 static int op_arm_start(void)
210 mutex_lock(&op_arm_mutex
);
211 if (!op_arm_enabled
) {
216 mutex_unlock(&op_arm_mutex
);
220 static void op_arm_stop(void)
222 mutex_lock(&op_arm_mutex
);
226 mutex_unlock(&op_arm_mutex
);
230 static int op_arm_suspend(struct sys_device
*dev
, pm_message_t state
)
232 mutex_lock(&op_arm_mutex
);
235 mutex_unlock(&op_arm_mutex
);
239 static int op_arm_resume(struct sys_device
*dev
)
241 mutex_lock(&op_arm_mutex
);
242 if (op_arm_enabled
&& op_perf_start())
244 mutex_unlock(&op_arm_mutex
);
248 static struct sysdev_class oprofile_sysclass
= {
250 .resume
= op_arm_resume
,
251 .suspend
= op_arm_suspend
,
254 static struct sys_device device_oprofile
= {
256 .cls
= &oprofile_sysclass
,
259 static int __init
init_driverfs(void)
263 if (!(ret
= sysdev_class_register(&oprofile_sysclass
)))
264 ret
= sysdev_register(&device_oprofile
);
269 static void exit_driverfs(void)
271 sysdev_unregister(&device_oprofile
);
272 sysdev_class_unregister(&oprofile_sysclass
);
275 #define init_driverfs() do { } while (0)
276 #define exit_driverfs() do { } while (0)
277 #endif /* CONFIG_PM */
279 static int report_trace(struct stackframe
*frame
, void *d
)
281 unsigned int *depth
= d
;
284 oprofile_add_trace(frame
->pc
);
292 * The registers we're interested in are at the end of the variable
293 * length saved register structure. The fp points at the end of this
294 * structure so the address of this struct is:
295 * (struct frame_tail *)(xxx->fp)-1
298 struct frame_tail
*fp
;
301 } __attribute__((packed
));
303 static struct frame_tail
* user_backtrace(struct frame_tail
*tail
)
305 struct frame_tail buftail
[2];
307 /* Also check accessibility of one struct frame_tail beyond */
308 if (!access_ok(VERIFY_READ
, tail
, sizeof(buftail
)))
310 if (__copy_from_user_inatomic(buftail
, tail
, sizeof(buftail
)))
313 oprofile_add_trace(buftail
[0].lr
);
315 /* frame pointers should strictly progress back up the stack
316 * (towards higher addresses) */
317 if (tail
>= buftail
[0].fp
)
320 return buftail
[0].fp
-1;
323 static void arm_backtrace(struct pt_regs
* const regs
, unsigned int depth
)
325 struct frame_tail
*tail
= ((struct frame_tail
*) regs
->ARM_fp
) - 1;
327 if (!user_mode(regs
)) {
328 struct stackframe frame
;
329 frame
.fp
= regs
->ARM_fp
;
330 frame
.sp
= regs
->ARM_sp
;
331 frame
.lr
= regs
->ARM_lr
;
332 frame
.pc
= regs
->ARM_pc
;
333 walk_stackframe(&frame
, report_trace
, &depth
);
337 while (depth
-- && tail
&& !((unsigned long) tail
& 3))
338 tail
= user_backtrace(tail
);
341 int __init
oprofile_arch_init(struct oprofile_operations
*ops
)
345 perf_num_counters
= armpmu_get_max_events();
347 counter_config
= kcalloc(perf_num_counters
,
348 sizeof(struct op_counter_config
), GFP_KERNEL
);
350 if (!counter_config
) {
351 pr_info("oprofile: failed to allocate %d "
352 "counters\n", perf_num_counters
);
356 for_each_possible_cpu(cpu
) {
357 perf_events
[cpu
] = kcalloc(perf_num_counters
,
358 sizeof(struct perf_event
*), GFP_KERNEL
);
359 if (!perf_events
[cpu
]) {
360 pr_info("oprofile: failed to allocate %d perf events "
361 "for cpu %d\n", perf_num_counters
, cpu
);
363 kfree(perf_events
[cpu
]);
369 ops
->backtrace
= arm_backtrace
;
370 ops
->create_files
= op_arm_create_files
;
371 ops
->setup
= op_arm_setup
;
372 ops
->start
= op_arm_start
;
373 ops
->stop
= op_arm_stop
;
374 ops
->shutdown
= op_arm_stop
;
375 ops
->cpu_type
= op_name_from_perf_id(armpmu_get_pmu_id());
380 pr_info("oprofile: using %s\n", ops
->cpu_type
);
385 void oprofile_arch_exit(void)
388 struct perf_event
*event
;
392 for_each_possible_cpu(cpu
) {
393 for (id
= 0; id
< perf_num_counters
; ++id
) {
394 event
= perf_events
[cpu
][id
];
396 perf_event_release_kernel(event
);
398 kfree(perf_events
[cpu
]);
403 kfree(counter_config
);
406 int __init
oprofile_arch_init(struct oprofile_operations
*ops
)
408 pr_info("oprofile: hardware counters not available\n");
411 void oprofile_arch_exit(void) {}
412 #endif /* CONFIG_HW_PERF_EVENTS */