2 * Copyright 2010 ARM Ltd.
4 * Perf-events backend for OProfile.
6 #include <linux/perf_event.h>
7 #include <linux/oprofile.h>
8 #include <linux/slab.h>
11 * Per performance monitor configuration as set via oprofilefs.
13 struct op_counter_config
{
15 unsigned long enabled
;
17 unsigned long unit_mask
;
20 struct perf_event_attr attr
;
23 static int oprofile_perf_enabled
;
24 static DEFINE_MUTEX(oprofile_perf_mutex
);
26 static struct op_counter_config
*counter_config
;
27 static struct perf_event
**perf_events
[nr_cpumask_bits
];
28 static int num_counters
;
31 * Overflow callback for oprofile.
33 static void op_overflow_handler(struct perf_event
*event
, int unused
,
34 struct perf_sample_data
*data
, struct pt_regs
*regs
)
37 u32 cpu
= smp_processor_id();
39 for (id
= 0; id
< num_counters
; ++id
)
40 if (perf_events
[cpu
][id
] == event
)
43 if (id
!= num_counters
)
44 oprofile_add_sample(regs
, id
);
46 pr_warning("oprofile: ignoring spurious overflow "
51 * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile
52 * settings in counter_config. Attributes are created as `pinned' events and
53 * so are permanently scheduled on the PMU.
55 static void op_perf_setup(void)
58 u32 size
= sizeof(struct perf_event_attr
);
59 struct perf_event_attr
*attr
;
61 for (i
= 0; i
< num_counters
; ++i
) {
62 attr
= &counter_config
[i
].attr
;
63 memset(attr
, 0, size
);
64 attr
->type
= PERF_TYPE_RAW
;
66 attr
->config
= counter_config
[i
].event
;
67 attr
->sample_period
= counter_config
[i
].count
;
72 static int op_create_counter(int cpu
, int event
)
75 struct perf_event
*pevent
;
77 if (!counter_config
[event
].enabled
|| (perf_events
[cpu
][event
] != NULL
))
80 pevent
= perf_event_create_kernel_counter(&counter_config
[event
].attr
,
85 ret
= PTR_ERR(pevent
);
86 } else if (pevent
->state
!= PERF_EVENT_STATE_ACTIVE
) {
87 pr_warning("oprofile: failed to enable event %d "
88 "on CPU %d\n", event
, cpu
);
91 perf_events
[cpu
][event
] = pevent
;
97 static void op_destroy_counter(int cpu
, int event
)
99 struct perf_event
*pevent
= perf_events
[cpu
][event
];
102 perf_event_release_kernel(pevent
);
103 perf_events
[cpu
][event
] = NULL
;
108 * Called by oprofile_perf_start to create active perf events based on the
109 * perviously configured attributes.
111 static int op_perf_start(void)
113 int cpu
, event
, ret
= 0;
115 for_each_online_cpu(cpu
) {
116 for (event
= 0; event
< num_counters
; ++event
) {
117 ret
= op_create_counter(cpu
, event
);
128 * Called by oprofile_perf_stop at the end of a profiling run.
130 static void op_perf_stop(void)
134 for_each_online_cpu(cpu
)
135 for (event
= 0; event
< num_counters
; ++event
)
136 op_destroy_counter(cpu
, event
);
139 static int oprofile_perf_create_files(struct super_block
*sb
, struct dentry
*root
)
143 for (i
= 0; i
< num_counters
; i
++) {
147 snprintf(buf
, sizeof buf
, "%d", i
);
148 dir
= oprofilefs_mkdir(sb
, root
, buf
);
149 oprofilefs_create_ulong(sb
, dir
, "enabled", &counter_config
[i
].enabled
);
150 oprofilefs_create_ulong(sb
, dir
, "event", &counter_config
[i
].event
);
151 oprofilefs_create_ulong(sb
, dir
, "count", &counter_config
[i
].count
);
152 oprofilefs_create_ulong(sb
, dir
, "unit_mask", &counter_config
[i
].unit_mask
);
153 oprofilefs_create_ulong(sb
, dir
, "kernel", &counter_config
[i
].kernel
);
154 oprofilefs_create_ulong(sb
, dir
, "user", &counter_config
[i
].user
);
160 static int oprofile_perf_setup(void)
162 spin_lock(&oprofilefs_lock
);
164 spin_unlock(&oprofilefs_lock
);
168 static int oprofile_perf_start(void)
172 mutex_lock(&oprofile_perf_mutex
);
173 if (!oprofile_perf_enabled
) {
176 oprofile_perf_enabled
= 1;
178 mutex_unlock(&oprofile_perf_mutex
);
182 static void oprofile_perf_stop(void)
184 mutex_lock(&oprofile_perf_mutex
);
185 if (oprofile_perf_enabled
)
187 oprofile_perf_enabled
= 0;
188 mutex_unlock(&oprofile_perf_mutex
);
192 static int oprofile_perf_suspend(struct platform_device
*dev
, pm_message_t state
)
194 mutex_lock(&oprofile_perf_mutex
);
195 if (oprofile_perf_enabled
)
197 mutex_unlock(&oprofile_perf_mutex
);
201 static int oprofile_perf_resume(struct platform_device
*dev
)
203 mutex_lock(&oprofile_perf_mutex
);
204 if (oprofile_perf_enabled
&& op_perf_start())
205 oprofile_perf_enabled
= 0;
206 mutex_unlock(&oprofile_perf_mutex
);
210 static struct platform_driver oprofile_driver
= {
212 .name
= "oprofile-perf",
214 .resume
= oprofile_perf_resume
,
215 .suspend
= oprofile_perf_suspend
,
218 static struct platform_device
*oprofile_pdev
;
220 static int __init
init_driverfs(void)
224 ret
= platform_driver_register(&oprofile_driver
);
228 oprofile_pdev
= platform_device_register_simple(
229 oprofile_driver
.driver
.name
, 0, NULL
, 0);
230 if (IS_ERR(oprofile_pdev
)) {
231 ret
= PTR_ERR(oprofile_pdev
);
232 platform_driver_unregister(&oprofile_driver
);
239 static void __exit
exit_driverfs(void)
241 platform_device_unregister(oprofile_pdev
);
242 platform_driver_unregister(&oprofile_driver
);
245 static int __init
init_driverfs(void) { return 0; }
246 #define exit_driverfs() do { } while (0)
247 #endif /* CONFIG_PM */
249 int __init
oprofile_perf_init(struct oprofile_operations
*ops
)
253 memset(&perf_events
, 0, sizeof(perf_events
));
255 num_counters
= perf_num_counters();
256 if (num_counters
<= 0) {
257 pr_info("oprofile: no performance counters\n");
262 counter_config
= kcalloc(num_counters
,
263 sizeof(struct op_counter_config
), GFP_KERNEL
);
265 if (!counter_config
) {
266 pr_info("oprofile: failed to allocate %d "
267 "counters\n", num_counters
);
272 ret
= init_driverfs();
276 for_each_possible_cpu(cpu
) {
277 perf_events
[cpu
] = kcalloc(num_counters
,
278 sizeof(struct perf_event
*), GFP_KERNEL
);
279 if (!perf_events
[cpu
]) {
280 pr_info("oprofile: failed to allocate %d perf events "
281 "for cpu %d\n", num_counters
, cpu
);
287 ops
->create_files
= oprofile_perf_create_files
;
288 ops
->setup
= oprofile_perf_setup
;
289 ops
->start
= oprofile_perf_start
;
290 ops
->stop
= oprofile_perf_stop
;
291 ops
->shutdown
= oprofile_perf_stop
;
292 ops
->cpu_type
= op_name_from_perf_id();
297 pr_info("oprofile: using %s\n", ops
->cpu_type
);
301 for_each_possible_cpu(cpu
)
302 kfree(perf_events
[cpu
]);
303 kfree(counter_config
);
309 void __exit
oprofile_perf_exit(void)
312 struct perf_event
*event
;
314 for_each_possible_cpu(cpu
) {
315 for (id
= 0; id
< num_counters
; ++id
) {
316 event
= perf_events
[cpu
][id
];
318 perf_event_release_kernel(event
);
321 kfree(perf_events
[cpu
]);
324 kfree(counter_config
);