1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2010 ARM Ltd.
4 * Copyright 2012 Advanced Micro Devices, Inc., Robert Richter
6 * Perf-events backend for OProfile.
8 #include <linux/perf_event.h>
9 #include <linux/platform_device.h>
10 #include <linux/oprofile.h>
11 #include <linux/slab.h>
14 * Per performance monitor configuration as set via oprofilefs.
16 struct op_counter_config
{
18 unsigned long enabled
;
20 unsigned long unit_mask
;
23 struct perf_event_attr attr
;
26 static int oprofile_perf_enabled
;
27 static DEFINE_MUTEX(oprofile_perf_mutex
);
29 static struct op_counter_config
*counter_config
;
30 static DEFINE_PER_CPU(struct perf_event
**, perf_events
);
31 static int num_counters
;
34 * Overflow callback for oprofile.
36 static void op_overflow_handler(struct perf_event
*event
,
37 struct perf_sample_data
*data
, struct pt_regs
*regs
)
40 u32 cpu
= smp_processor_id();
42 for (id
= 0; id
< num_counters
; ++id
)
43 if (per_cpu(perf_events
, cpu
)[id
] == event
)
46 if (id
!= num_counters
)
47 oprofile_add_sample(regs
, id
);
49 pr_warn("oprofile: ignoring spurious overflow on cpu %u\n",
54 * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile
55 * settings in counter_config. Attributes are created as `pinned' events and
56 * so are permanently scheduled on the PMU.
58 static void op_perf_setup(void)
61 u32 size
= sizeof(struct perf_event_attr
);
62 struct perf_event_attr
*attr
;
64 for (i
= 0; i
< num_counters
; ++i
) {
65 attr
= &counter_config
[i
].attr
;
66 memset(attr
, 0, size
);
67 attr
->type
= PERF_TYPE_RAW
;
69 attr
->config
= counter_config
[i
].event
;
70 attr
->sample_period
= counter_config
[i
].count
;
75 static int op_create_counter(int cpu
, int event
)
77 struct perf_event
*pevent
;
79 if (!counter_config
[event
].enabled
|| per_cpu(perf_events
, cpu
)[event
])
82 pevent
= perf_event_create_kernel_counter(&counter_config
[event
].attr
,
84 op_overflow_handler
, NULL
);
87 return PTR_ERR(pevent
);
89 if (pevent
->state
!= PERF_EVENT_STATE_ACTIVE
) {
90 perf_event_release_kernel(pevent
);
91 pr_warn("oprofile: failed to enable event %d on CPU %d\n",
96 per_cpu(perf_events
, cpu
)[event
] = pevent
;
101 static void op_destroy_counter(int cpu
, int event
)
103 struct perf_event
*pevent
= per_cpu(perf_events
, cpu
)[event
];
106 perf_event_release_kernel(pevent
);
107 per_cpu(perf_events
, cpu
)[event
] = NULL
;
112 * Called by oprofile_perf_start to create active perf events based on the
113 * perviously configured attributes.
115 static int op_perf_start(void)
117 int cpu
, event
, ret
= 0;
119 for_each_online_cpu(cpu
) {
120 for (event
= 0; event
< num_counters
; ++event
) {
121 ret
= op_create_counter(cpu
, event
);
131 * Called by oprofile_perf_stop at the end of a profiling run.
133 static void op_perf_stop(void)
137 for_each_online_cpu(cpu
)
138 for (event
= 0; event
< num_counters
; ++event
)
139 op_destroy_counter(cpu
, event
);
142 static int oprofile_perf_create_files(struct dentry
*root
)
146 for (i
= 0; i
< num_counters
; i
++) {
150 snprintf(buf
, sizeof buf
, "%d", i
);
151 dir
= oprofilefs_mkdir(root
, buf
);
152 oprofilefs_create_ulong(dir
, "enabled", &counter_config
[i
].enabled
);
153 oprofilefs_create_ulong(dir
, "event", &counter_config
[i
].event
);
154 oprofilefs_create_ulong(dir
, "count", &counter_config
[i
].count
);
155 oprofilefs_create_ulong(dir
, "unit_mask", &counter_config
[i
].unit_mask
);
156 oprofilefs_create_ulong(dir
, "kernel", &counter_config
[i
].kernel
);
157 oprofilefs_create_ulong(dir
, "user", &counter_config
[i
].user
);
163 static int oprofile_perf_setup(void)
165 raw_spin_lock(&oprofilefs_lock
);
167 raw_spin_unlock(&oprofilefs_lock
);
171 static int oprofile_perf_start(void)
175 mutex_lock(&oprofile_perf_mutex
);
176 if (!oprofile_perf_enabled
) {
179 oprofile_perf_enabled
= 1;
181 mutex_unlock(&oprofile_perf_mutex
);
185 static void oprofile_perf_stop(void)
187 mutex_lock(&oprofile_perf_mutex
);
188 if (oprofile_perf_enabled
)
190 oprofile_perf_enabled
= 0;
191 mutex_unlock(&oprofile_perf_mutex
);
196 static int oprofile_perf_suspend(struct platform_device
*dev
, pm_message_t state
)
198 mutex_lock(&oprofile_perf_mutex
);
199 if (oprofile_perf_enabled
)
201 mutex_unlock(&oprofile_perf_mutex
);
205 static int oprofile_perf_resume(struct platform_device
*dev
)
207 mutex_lock(&oprofile_perf_mutex
);
208 if (oprofile_perf_enabled
&& op_perf_start())
209 oprofile_perf_enabled
= 0;
210 mutex_unlock(&oprofile_perf_mutex
);
214 static struct platform_driver oprofile_driver
= {
216 .name
= "oprofile-perf",
218 .resume
= oprofile_perf_resume
,
219 .suspend
= oprofile_perf_suspend
,
222 static struct platform_device
*oprofile_pdev
;
224 static int __init
init_driverfs(void)
228 ret
= platform_driver_register(&oprofile_driver
);
232 oprofile_pdev
= platform_device_register_simple(
233 oprofile_driver
.driver
.name
, 0, NULL
, 0);
234 if (IS_ERR(oprofile_pdev
)) {
235 ret
= PTR_ERR(oprofile_pdev
);
236 platform_driver_unregister(&oprofile_driver
);
242 static void exit_driverfs(void)
244 platform_device_unregister(oprofile_pdev
);
245 platform_driver_unregister(&oprofile_driver
);
250 static inline int init_driverfs(void) { return 0; }
251 static inline void exit_driverfs(void) { }
253 #endif /* CONFIG_PM */
255 void oprofile_perf_exit(void)
258 struct perf_event
*event
;
260 for_each_possible_cpu(cpu
) {
261 for (id
= 0; id
< num_counters
; ++id
) {
262 event
= per_cpu(perf_events
, cpu
)[id
];
264 perf_event_release_kernel(event
);
267 kfree(per_cpu(perf_events
, cpu
));
270 kfree(counter_config
);
274 int __init
oprofile_perf_init(struct oprofile_operations
*ops
)
278 ret
= init_driverfs();
282 num_counters
= perf_num_counters();
283 if (num_counters
<= 0) {
284 pr_info("oprofile: no performance counters\n");
289 counter_config
= kcalloc(num_counters
,
290 sizeof(struct op_counter_config
), GFP_KERNEL
);
292 if (!counter_config
) {
293 pr_info("oprofile: failed to allocate %d "
294 "counters\n", num_counters
);
300 for_each_possible_cpu(cpu
) {
301 per_cpu(perf_events
, cpu
) = kcalloc(num_counters
,
302 sizeof(struct perf_event
*), GFP_KERNEL
);
303 if (!per_cpu(perf_events
, cpu
)) {
304 pr_info("oprofile: failed to allocate %d perf events "
305 "for cpu %d\n", num_counters
, cpu
);
311 ops
->create_files
= oprofile_perf_create_files
;
312 ops
->setup
= oprofile_perf_setup
;
313 ops
->start
= oprofile_perf_start
;
314 ops
->stop
= oprofile_perf_stop
;
315 ops
->shutdown
= oprofile_perf_stop
;
316 ops
->cpu_type
= op_name_from_perf_id();
321 pr_info("oprofile: using %s\n", ops
->cpu_type
);
325 oprofile_perf_exit();