2 * Copyright 2010 ARM Ltd.
3 * Copyright 2012 Advanced Micro Devices, Inc., Robert Richter
5 * Perf-events backend for OProfile.
7 #include <linux/perf_event.h>
8 #include <linux/platform_device.h>
9 #include <linux/oprofile.h>
10 #include <linux/slab.h>
13 * Per performance monitor configuration as set via oprofilefs.
15 struct op_counter_config
{
17 unsigned long enabled
;
19 unsigned long unit_mask
;
22 struct perf_event_attr attr
;
25 static int oprofile_perf_enabled
;
26 static DEFINE_MUTEX(oprofile_perf_mutex
);
28 static struct op_counter_config
*counter_config
;
29 static DEFINE_PER_CPU(struct perf_event
**, perf_events
);
30 static int num_counters
;
33 * Overflow callback for oprofile.
35 static void op_overflow_handler(struct perf_event
*event
,
36 struct perf_sample_data
*data
, struct pt_regs
*regs
)
39 u32 cpu
= smp_processor_id();
41 for (id
= 0; id
< num_counters
; ++id
)
42 if (per_cpu(perf_events
, cpu
)[id
] == event
)
45 if (id
!= num_counters
)
46 oprofile_add_sample(regs
, id
);
48 pr_warning("oprofile: ignoring spurious overflow "
53 * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile
54 * settings in counter_config. Attributes are created as `pinned' events and
55 * so are permanently scheduled on the PMU.
57 static void op_perf_setup(void)
60 u32 size
= sizeof(struct perf_event_attr
);
61 struct perf_event_attr
*attr
;
63 for (i
= 0; i
< num_counters
; ++i
) {
64 attr
= &counter_config
[i
].attr
;
65 memset(attr
, 0, size
);
66 attr
->type
= PERF_TYPE_RAW
;
68 attr
->config
= counter_config
[i
].event
;
69 attr
->sample_period
= counter_config
[i
].count
;
74 static int op_create_counter(int cpu
, int event
)
76 struct perf_event
*pevent
;
78 if (!counter_config
[event
].enabled
|| per_cpu(perf_events
, cpu
)[event
])
81 pevent
= perf_event_create_kernel_counter(&counter_config
[event
].attr
,
83 op_overflow_handler
, NULL
);
86 return PTR_ERR(pevent
);
88 if (pevent
->state
!= PERF_EVENT_STATE_ACTIVE
) {
89 perf_event_release_kernel(pevent
);
90 pr_warning("oprofile: failed to enable event %d "
91 "on CPU %d\n", event
, cpu
);
95 per_cpu(perf_events
, cpu
)[event
] = pevent
;
100 static void op_destroy_counter(int cpu
, int event
)
102 struct perf_event
*pevent
= per_cpu(perf_events
, cpu
)[event
];
105 perf_event_release_kernel(pevent
);
106 per_cpu(perf_events
, cpu
)[event
] = NULL
;
111 * Called by oprofile_perf_start to create active perf events based on the
112 * perviously configured attributes.
114 static int op_perf_start(void)
116 int cpu
, event
, ret
= 0;
118 for_each_online_cpu(cpu
) {
119 for (event
= 0; event
< num_counters
; ++event
) {
120 ret
= op_create_counter(cpu
, event
);
130 * Called by oprofile_perf_stop at the end of a profiling run.
132 static void op_perf_stop(void)
136 for_each_online_cpu(cpu
)
137 for (event
= 0; event
< num_counters
; ++event
)
138 op_destroy_counter(cpu
, event
);
141 static int oprofile_perf_create_files(struct dentry
*root
)
145 for (i
= 0; i
< num_counters
; i
++) {
149 snprintf(buf
, sizeof buf
, "%d", i
);
150 dir
= oprofilefs_mkdir(root
, buf
);
151 oprofilefs_create_ulong(dir
, "enabled", &counter_config
[i
].enabled
);
152 oprofilefs_create_ulong(dir
, "event", &counter_config
[i
].event
);
153 oprofilefs_create_ulong(dir
, "count", &counter_config
[i
].count
);
154 oprofilefs_create_ulong(dir
, "unit_mask", &counter_config
[i
].unit_mask
);
155 oprofilefs_create_ulong(dir
, "kernel", &counter_config
[i
].kernel
);
156 oprofilefs_create_ulong(dir
, "user", &counter_config
[i
].user
);
162 static int oprofile_perf_setup(void)
164 raw_spin_lock(&oprofilefs_lock
);
166 raw_spin_unlock(&oprofilefs_lock
);
170 static int oprofile_perf_start(void)
174 mutex_lock(&oprofile_perf_mutex
);
175 if (!oprofile_perf_enabled
) {
178 oprofile_perf_enabled
= 1;
180 mutex_unlock(&oprofile_perf_mutex
);
184 static void oprofile_perf_stop(void)
186 mutex_lock(&oprofile_perf_mutex
);
187 if (oprofile_perf_enabled
)
189 oprofile_perf_enabled
= 0;
190 mutex_unlock(&oprofile_perf_mutex
);
195 static int oprofile_perf_suspend(struct platform_device
*dev
, pm_message_t state
)
197 mutex_lock(&oprofile_perf_mutex
);
198 if (oprofile_perf_enabled
)
200 mutex_unlock(&oprofile_perf_mutex
);
204 static int oprofile_perf_resume(struct platform_device
*dev
)
206 mutex_lock(&oprofile_perf_mutex
);
207 if (oprofile_perf_enabled
&& op_perf_start())
208 oprofile_perf_enabled
= 0;
209 mutex_unlock(&oprofile_perf_mutex
);
213 static struct platform_driver oprofile_driver
= {
215 .name
= "oprofile-perf",
217 .resume
= oprofile_perf_resume
,
218 .suspend
= oprofile_perf_suspend
,
221 static struct platform_device
*oprofile_pdev
;
223 static int __init
init_driverfs(void)
227 ret
= platform_driver_register(&oprofile_driver
);
231 oprofile_pdev
= platform_device_register_simple(
232 oprofile_driver
.driver
.name
, 0, NULL
, 0);
233 if (IS_ERR(oprofile_pdev
)) {
234 ret
= PTR_ERR(oprofile_pdev
);
235 platform_driver_unregister(&oprofile_driver
);
241 static void exit_driverfs(void)
243 platform_device_unregister(oprofile_pdev
);
244 platform_driver_unregister(&oprofile_driver
);
249 static inline int init_driverfs(void) { return 0; }
250 static inline void exit_driverfs(void) { }
252 #endif /* CONFIG_PM */
254 void oprofile_perf_exit(void)
257 struct perf_event
*event
;
259 for_each_possible_cpu(cpu
) {
260 for (id
= 0; id
< num_counters
; ++id
) {
261 event
= per_cpu(perf_events
, cpu
)[id
];
263 perf_event_release_kernel(event
);
266 kfree(per_cpu(perf_events
, cpu
));
269 kfree(counter_config
);
273 int __init
oprofile_perf_init(struct oprofile_operations
*ops
)
277 ret
= init_driverfs();
281 num_counters
= perf_num_counters();
282 if (num_counters
<= 0) {
283 pr_info("oprofile: no performance counters\n");
288 counter_config
= kcalloc(num_counters
,
289 sizeof(struct op_counter_config
), GFP_KERNEL
);
291 if (!counter_config
) {
292 pr_info("oprofile: failed to allocate %d "
293 "counters\n", num_counters
);
299 for_each_possible_cpu(cpu
) {
300 per_cpu(perf_events
, cpu
) = kcalloc(num_counters
,
301 sizeof(struct perf_event
*), GFP_KERNEL
);
302 if (!per_cpu(perf_events
, cpu
)) {
303 pr_info("oprofile: failed to allocate %d perf events "
304 "for cpu %d\n", num_counters
, cpu
);
310 ops
->create_files
= oprofile_perf_create_files
;
311 ops
->setup
= oprofile_perf_setup
;
312 ops
->start
= oprofile_perf_start
;
313 ops
->stop
= oprofile_perf_stop
;
314 ops
->shutdown
= oprofile_perf_stop
;
315 ops
->cpu_type
= op_name_from_perf_id();
320 pr_info("oprofile: using %s\n", ops
->cpu_type
);
324 oprofile_perf_exit();