Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file common.c |
| 3 | * |
| 4 | * @remark Copyright 2004 Oprofile Authors |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 5 | * @remark Copyright 2010 ARM Ltd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * @remark Read the file COPYING |
| 7 | * |
| 8 | * @author Zwane Mwaikambo |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 9 | * @author Will Deacon [move to perf] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 12 | #include <linux/cpumask.h> |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 13 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/oprofile.h> |
| 18 | #include <linux/perf_event.h> |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Russell King | ae92dc9 | 2006-03-16 11:32:51 +0000 | [diff] [blame] | 20 | #include <linux/slab.h> |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 21 | #include <asm/stacktrace.h> |
| 22 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 24 | #include <asm/perf_event.h> |
| 25 | #include <asm/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 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 | }; |
| 40 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 41 | static int op_arm_enabled; |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame] | 42 | static DEFINE_MUTEX(op_arm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 178 | static int op_arm_create_files(struct super_block *sb, struct dentry *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
| 180 | unsigned int i; |
| 181 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 182 | for (i = 0; i < perf_num_counters; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | struct dentry *dir; |
Russell King | ae92dc9 | 2006-03-16 11:32:51 +0000 | [diff] [blame] | 184 | char buf[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | snprintf(buf, sizeof buf, "%d", i); |
| 187 | dir = oprofilefs_mkdir(sb, root, buf); |
| 188 | oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled); |
| 189 | oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event); |
| 190 | oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count); |
| 191 | oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask); |
| 192 | oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel); |
| 193 | oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user); |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 199 | static int op_arm_setup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | spin_lock(&oprofilefs_lock); |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 202 | op_perf_setup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | spin_unlock(&oprofilefs_lock); |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 204 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 207 | static int op_arm_start(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | int ret = -EBUSY; |
| 210 | |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame] | 211 | mutex_lock(&op_arm_mutex); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 212 | if (!op_arm_enabled) { |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 213 | ret = 0; |
| 214 | op_perf_start(); |
| 215 | op_arm_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame] | 217 | mutex_unlock(&op_arm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return ret; |
| 219 | } |
| 220 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 221 | static void op_arm_stop(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame] | 223 | mutex_lock(&op_arm_mutex); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 224 | if (op_arm_enabled) |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 225 | op_perf_stop(); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 226 | op_arm_enabled = 0; |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame] | 227 | mutex_unlock(&op_arm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 230 | #ifdef CONFIG_PM |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 231 | static int op_arm_suspend(struct platform_device *dev, pm_message_t state) |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 232 | { |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame] | 233 | mutex_lock(&op_arm_mutex); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 234 | if (op_arm_enabled) |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 235 | op_perf_stop(); |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame] | 236 | mutex_unlock(&op_arm_mutex); |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 240 | static int op_arm_resume(struct platform_device *dev) |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 241 | { |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame] | 242 | mutex_lock(&op_arm_mutex); |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 243 | if (op_arm_enabled && op_perf_start()) |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 244 | op_arm_enabled = 0; |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame] | 245 | mutex_unlock(&op_arm_mutex); |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 249 | static struct platform_driver oprofile_driver = { |
| 250 | .driver = { |
| 251 | .name = "arm-oprofile", |
| 252 | }, |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 253 | .resume = op_arm_resume, |
| 254 | .suspend = op_arm_suspend, |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 255 | }; |
| 256 | |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 257 | static struct platform_device *oprofile_pdev; |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 258 | |
| 259 | static int __init init_driverfs(void) |
| 260 | { |
| 261 | int ret; |
| 262 | |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 263 | ret = platform_driver_register(&oprofile_driver); |
| 264 | if (ret) |
| 265 | goto out; |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 266 | |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 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: |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 278 | static void __exit exit_driverfs(void) |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 279 | { |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 280 | platform_device_unregister(oprofile_pdev); |
| 281 | platform_driver_unregister(&oprofile_driver); |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 282 | } |
| 283 | #else |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 284 | static int __init init_driverfs(void) { return 0; } |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 285 | #define exit_driverfs() do { } while (0) |
| 286 | #endif /* CONFIG_PM */ |
| 287 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 288 | static int report_trace(struct stackframe *frame, void *d) |
| 289 | { |
| 290 | unsigned int *depth = d; |
| 291 | |
| 292 | if (*depth) { |
| 293 | oprofile_add_trace(frame->pc); |
| 294 | (*depth)--; |
| 295 | } |
| 296 | |
| 297 | return *depth == 0; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * The registers we're interested in are at the end of the variable |
| 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)); |
| 311 | |
| 312 | static struct frame_tail* user_backtrace(struct frame_tail *tail) |
| 313 | { |
| 314 | struct frame_tail buftail[2]; |
| 315 | |
| 316 | /* Also check accessibility of one struct frame_tail beyond */ |
| 317 | if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) |
| 318 | return NULL; |
| 319 | if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) |
| 320 | return NULL; |
| 321 | |
| 322 | oprofile_add_trace(buftail[0].lr); |
| 323 | |
| 324 | /* frame pointers should strictly progress back up the stack |
| 325 | * (towards higher addresses) */ |
| 326 | if (tail >= buftail[0].fp) |
| 327 | return NULL; |
| 328 | |
| 329 | return buftail[0].fp-1; |
| 330 | } |
| 331 | |
| 332 | static void arm_backtrace(struct pt_regs * const regs, unsigned int depth) |
| 333 | { |
| 334 | struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1; |
| 335 | |
| 336 | if (!user_mode(regs)) { |
| 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; |
| 344 | } |
| 345 | |
| 346 | while (depth-- && tail && !((unsigned long) tail & 3)) |
| 347 | tail = user_backtrace(tail); |
| 348 | } |
| 349 | |
Russell King | c6b9daf | 2005-10-28 14:56:04 +0100 | [diff] [blame] | 350 | int __init oprofile_arch_init(struct oprofile_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 352 | int cpu, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 354 | perf_num_counters = armpmu_get_max_events(); |
Richard Purdie | 1b7b569 | 2007-02-27 12:09:33 +0100 | [diff] [blame] | 355 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 356 | counter_config = kcalloc(perf_num_counters, |
| 357 | sizeof(struct op_counter_config), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 359 | if (!counter_config) { |
| 360 | pr_info("oprofile: failed to allocate %d " |
| 361 | "counters\n", perf_num_counters); |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 362 | ret = -ENOMEM; |
| 363 | goto out; |
Russell King | c6b9daf | 2005-10-28 14:56:04 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 366 | ret = init_driverfs(); |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 367 | if (ret) |
| 368 | goto out; |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 369 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 370 | for_each_possible_cpu(cpu) { |
| 371 | perf_events[cpu] = kcalloc(perf_num_counters, |
| 372 | sizeof(struct perf_event *), GFP_KERNEL); |
| 373 | if (!perf_events[cpu]) { |
| 374 | pr_info("oprofile: failed to allocate %d perf events " |
| 375 | "for cpu %d\n", perf_num_counters, cpu); |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 376 | ret = -ENOMEM; |
| 377 | goto out; |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 381 | ops->backtrace = arm_backtrace; |
| 382 | ops->create_files = op_arm_create_files; |
| 383 | ops->setup = op_arm_setup; |
| 384 | ops->start = op_arm_start; |
| 385 | ops->stop = op_arm_stop; |
| 386 | ops->shutdown = op_arm_stop; |
| 387 | ops->cpu_type = op_name_from_perf_id(armpmu_get_pmu_id()); |
| 388 | |
| 389 | if (!ops->cpu_type) |
| 390 | ret = -ENODEV; |
| 391 | else |
| 392 | pr_info("oprofile: using %s\n", ops->cpu_type); |
| 393 | |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 394 | out: |
| 395 | if (ret) { |
| 396 | for_each_possible_cpu(cpu) |
| 397 | kfree(perf_events[cpu]); |
| 398 | kfree(counter_config); |
| 399 | } |
| 400 | |
Russell King | c6b9daf | 2005-10-28 14:56:04 +0100 | [diff] [blame] | 401 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 404 | void __exit oprofile_arch_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 406 | int cpu, id; |
| 407 | struct perf_event *event; |
| 408 | |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 409 | for_each_possible_cpu(cpu) { |
| 410 | for (id = 0; id < perf_num_counters; ++id) { |
| 411 | event = perf_events[cpu][id]; |
| 412 | if (event) |
| 413 | perf_event_release_kernel(event); |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 414 | } |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 415 | |
| 416 | kfree(perf_events[cpu]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 418 | |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 419 | kfree(counter_config); |
| 420 | exit_driverfs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 422 | #else |
| 423 | int __init oprofile_arch_init(struct oprofile_operations *ops) |
| 424 | { |
| 425 | pr_info("oprofile: hardware counters not available\n"); |
| 426 | return -ENODEV; |
| 427 | } |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame^] | 428 | void __exit oprofile_arch_exit(void) {} |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 429 | #endif /* CONFIG_HW_PERF_EVENTS */ |