Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Linux performance counter support for ARC700 series |
| 3 | * |
Vineet Gupta | fb7c572 | 2015-08-24 13:37:01 +0300 | [diff] [blame] | 4 | * Copyright (C) 2013-2015 Synopsys, Inc. (www.synopsys.com) |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 5 | * |
| 6 | * This code is inspired by the perf support of various other architectures. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | #include <linux/errno.h> |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/perf_event.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <asm/arcregs.h> |
Vineet Gupta | 389e316 | 2013-11-12 11:00:03 +0100 | [diff] [blame] | 20 | #include <asm/stacktrace.h> |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 21 | |
| 22 | struct arc_pmu { |
| 23 | struct pmu pmu; |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 24 | int n_counters; |
Vineet Gupta | fb7c572 | 2015-08-24 13:37:01 +0300 | [diff] [blame] | 25 | unsigned long used_mask[BITS_TO_LONGS(ARC_PERF_MAX_COUNTERS)]; |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 26 | u64 max_period; |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 27 | int ev_hw_idx[PERF_COUNT_ARC_HW_MAX]; |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 28 | struct perf_event *act_counter[ARC_PERF_MAX_COUNTERS]; |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
Vineet Gupta | 389e316 | 2013-11-12 11:00:03 +0100 | [diff] [blame] | 31 | struct arc_callchain_trace { |
| 32 | int depth; |
| 33 | void *perf_stuff; |
| 34 | }; |
| 35 | |
| 36 | static int callchain_trace(unsigned int addr, void *data) |
| 37 | { |
| 38 | struct arc_callchain_trace *ctrl = data; |
| 39 | struct perf_callchain_entry *entry = ctrl->perf_stuff; |
| 40 | perf_callchain_store(entry, addr); |
| 41 | |
| 42 | if (ctrl->depth++ < 3) |
| 43 | return 0; |
| 44 | |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs) |
| 50 | { |
| 51 | struct arc_callchain_trace ctrl = { |
| 52 | .depth = 0, |
| 53 | .perf_stuff = entry, |
| 54 | }; |
| 55 | |
| 56 | arc_unwind_core(NULL, regs, callchain_trace, &ctrl); |
| 57 | } |
| 58 | |
Vineet Gupta | 22f6b89 | 2013-07-12 15:55:54 +0200 | [diff] [blame] | 59 | void |
| 60 | perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs) |
| 61 | { |
| 62 | /* |
| 63 | * User stack can't be unwound trivially with kernel dwarf unwinder |
| 64 | * So for now just record the user PC |
| 65 | */ |
| 66 | perf_callchain_store(entry, instruction_pointer(regs)); |
| 67 | } |
| 68 | |
Vineet Gupta | 03c94fc | 2014-11-17 17:13:03 +0530 | [diff] [blame] | 69 | static struct arc_pmu *arc_pmu; |
| 70 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 71 | /* read counter #idx; note that counter# != event# on ARC! */ |
| 72 | static uint64_t arc_pmu_read_counter(int idx) |
| 73 | { |
| 74 | uint32_t tmp; |
| 75 | uint64_t result; |
| 76 | |
| 77 | /* |
| 78 | * ARC supports making 'snapshots' of the counters, so we don't |
| 79 | * need to care about counters wrapping to 0 underneath our feet |
| 80 | */ |
| 81 | write_aux_reg(ARC_REG_PCT_INDEX, idx); |
| 82 | tmp = read_aux_reg(ARC_REG_PCT_CONTROL); |
| 83 | write_aux_reg(ARC_REG_PCT_CONTROL, tmp | ARC_REG_PCT_CONTROL_SN); |
| 84 | result = (uint64_t) (read_aux_reg(ARC_REG_PCT_SNAPH)) << 32; |
| 85 | result |= read_aux_reg(ARC_REG_PCT_SNAPL); |
| 86 | |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | static void arc_perf_event_update(struct perf_event *event, |
| 91 | struct hw_perf_event *hwc, int idx) |
| 92 | { |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 93 | uint64_t prev_raw_count = local64_read(&hwc->prev_count); |
| 94 | uint64_t new_raw_count = arc_pmu_read_counter(idx); |
| 95 | int64_t delta = new_raw_count - prev_raw_count; |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 96 | |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 97 | /* |
| 98 | * We don't afaraid of hwc->prev_count changing beneath our feet |
| 99 | * because there's no way for us to re-enter this function anytime. |
| 100 | */ |
| 101 | local64_set(&hwc->prev_count, new_raw_count); |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 102 | local64_add(delta, &event->count); |
| 103 | local64_sub(delta, &hwc->period_left); |
| 104 | } |
| 105 | |
| 106 | static void arc_pmu_read(struct perf_event *event) |
| 107 | { |
| 108 | arc_perf_event_update(event, &event->hw, event->hw.idx); |
| 109 | } |
| 110 | |
| 111 | static int arc_pmu_cache_event(u64 config) |
| 112 | { |
| 113 | unsigned int cache_type, cache_op, cache_result; |
| 114 | int ret; |
| 115 | |
| 116 | cache_type = (config >> 0) & 0xff; |
| 117 | cache_op = (config >> 8) & 0xff; |
| 118 | cache_result = (config >> 16) & 0xff; |
| 119 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
| 120 | return -EINVAL; |
Vineet Gupta | da990a4 | 2013-11-28 15:49:59 +0530 | [diff] [blame] | 121 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 122 | return -EINVAL; |
Vineet Gupta | da990a4 | 2013-11-28 15:49:59 +0530 | [diff] [blame] | 123 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 124 | return -EINVAL; |
| 125 | |
| 126 | ret = arc_pmu_cache_map[cache_type][cache_op][cache_result]; |
| 127 | |
| 128 | if (ret == CACHE_OP_UNSUPPORTED) |
| 129 | return -ENOENT; |
| 130 | |
Vineet Gupta | bde80c2 | 2015-04-15 19:44:07 +0530 | [diff] [blame] | 131 | pr_debug("init cache event: type/op/result %d/%d/%d with h/w %d \'%s\'\n", |
| 132 | cache_type, cache_op, cache_result, ret, |
| 133 | arc_pmu_ev_hw_map[ret]); |
| 134 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /* initializes hw_perf_event structure if event is supported */ |
| 139 | static int arc_pmu_event_init(struct perf_event *event) |
| 140 | { |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 141 | struct hw_perf_event *hwc = &event->hw; |
| 142 | int ret; |
| 143 | |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 144 | if (!is_sampling_event(event)) { |
| 145 | hwc->sample_period = arc_pmu->max_period; |
| 146 | hwc->last_period = hwc->sample_period; |
| 147 | local64_set(&hwc->period_left, hwc->sample_period); |
| 148 | } |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 149 | |
Alexey Brodkin | e6b1d12 | 2015-08-24 13:53:36 +0300 | [diff] [blame^] | 150 | hwc->config = 0; |
| 151 | |
| 152 | if (is_isa_arcv2()) { |
| 153 | /* "exclude user" means "count only kernel" */ |
| 154 | if (event->attr.exclude_user) |
| 155 | hwc->config |= ARC_REG_PCT_CONFIG_KERN; |
| 156 | |
| 157 | /* "exclude kernel" means "count only user" */ |
| 158 | if (event->attr.exclude_kernel) |
| 159 | hwc->config |= ARC_REG_PCT_CONFIG_USER; |
| 160 | } |
| 161 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 162 | switch (event->attr.type) { |
| 163 | case PERF_TYPE_HARDWARE: |
| 164 | if (event->attr.config >= PERF_COUNT_HW_MAX) |
| 165 | return -ENOENT; |
| 166 | if (arc_pmu->ev_hw_idx[event->attr.config] < 0) |
| 167 | return -ENOENT; |
Alexey Brodkin | e6b1d12 | 2015-08-24 13:53:36 +0300 | [diff] [blame^] | 168 | hwc->config |= arc_pmu->ev_hw_idx[event->attr.config]; |
Vineet Gupta | bde80c2 | 2015-04-15 19:44:07 +0530 | [diff] [blame] | 169 | pr_debug("init event %d with h/w %d \'%s\'\n", |
| 170 | (int) event->attr.config, (int) hwc->config, |
| 171 | arc_pmu_ev_hw_map[event->attr.config]); |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 172 | return 0; |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 173 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 174 | case PERF_TYPE_HW_CACHE: |
| 175 | ret = arc_pmu_cache_event(event->attr.config); |
| 176 | if (ret < 0) |
| 177 | return ret; |
Alexey Brodkin | e6b1d12 | 2015-08-24 13:53:36 +0300 | [diff] [blame^] | 178 | hwc->config |= arc_pmu->ev_hw_idx[ret]; |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 179 | return 0; |
| 180 | default: |
| 181 | return -ENOENT; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* starts all counters */ |
| 186 | static void arc_pmu_enable(struct pmu *pmu) |
| 187 | { |
| 188 | uint32_t tmp; |
| 189 | tmp = read_aux_reg(ARC_REG_PCT_CONTROL); |
| 190 | write_aux_reg(ARC_REG_PCT_CONTROL, (tmp & 0xffff0000) | 0x1); |
| 191 | } |
| 192 | |
| 193 | /* stops all counters */ |
| 194 | static void arc_pmu_disable(struct pmu *pmu) |
| 195 | { |
| 196 | uint32_t tmp; |
| 197 | tmp = read_aux_reg(ARC_REG_PCT_CONTROL); |
| 198 | write_aux_reg(ARC_REG_PCT_CONTROL, (tmp & 0xffff0000) | 0x0); |
| 199 | } |
| 200 | |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 201 | static int arc_pmu_event_set_period(struct perf_event *event) |
| 202 | { |
| 203 | struct hw_perf_event *hwc = &event->hw; |
| 204 | s64 left = local64_read(&hwc->period_left); |
| 205 | s64 period = hwc->sample_period; |
| 206 | int idx = hwc->idx; |
| 207 | int overflow = 0; |
| 208 | u64 value; |
| 209 | |
| 210 | if (unlikely(left <= -period)) { |
| 211 | /* left underflowed by more than period. */ |
| 212 | left = period; |
| 213 | local64_set(&hwc->period_left, left); |
| 214 | hwc->last_period = period; |
| 215 | overflow = 1; |
| 216 | } else if (unlikely(left <= 0)) { |
| 217 | /* left underflowed by less than period. */ |
| 218 | left += period; |
| 219 | local64_set(&hwc->period_left, left); |
| 220 | hwc->last_period = period; |
| 221 | overflow = 1; |
| 222 | } |
| 223 | |
| 224 | if (left > arc_pmu->max_period) |
| 225 | left = arc_pmu->max_period; |
| 226 | |
| 227 | value = arc_pmu->max_period - left; |
| 228 | local64_set(&hwc->prev_count, value); |
| 229 | |
| 230 | /* Select counter */ |
| 231 | write_aux_reg(ARC_REG_PCT_INDEX, idx); |
| 232 | |
| 233 | /* Write value */ |
| 234 | write_aux_reg(ARC_REG_PCT_COUNTL, (u32)value); |
| 235 | write_aux_reg(ARC_REG_PCT_COUNTH, (value >> 32)); |
| 236 | |
| 237 | perf_event_update_userpage(event); |
| 238 | |
| 239 | return overflow; |
| 240 | } |
| 241 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 242 | /* |
| 243 | * Assigns hardware counter to hardware condition. |
| 244 | * Note that there is no separate start/stop mechanism; |
| 245 | * stopping is achieved by assigning the 'never' condition |
| 246 | */ |
| 247 | static void arc_pmu_start(struct perf_event *event, int flags) |
| 248 | { |
| 249 | struct hw_perf_event *hwc = &event->hw; |
| 250 | int idx = hwc->idx; |
| 251 | |
| 252 | if (WARN_ON_ONCE(idx == -1)) |
| 253 | return; |
| 254 | |
| 255 | if (flags & PERF_EF_RELOAD) |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 256 | WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE)); |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 257 | |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 258 | hwc->state = 0; |
| 259 | |
| 260 | arc_pmu_event_set_period(event); |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 261 | |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 262 | /* Enable interrupt for this counter */ |
| 263 | if (is_sampling_event(event)) |
| 264 | write_aux_reg(ARC_REG_PCT_INT_CTRL, |
| 265 | read_aux_reg(ARC_REG_PCT_INT_CTRL) | (1 << idx)); |
| 266 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 267 | /* enable ARC pmu here */ |
Vineet Gupta | 0907495 | 2015-08-19 17:23:58 +0530 | [diff] [blame] | 268 | write_aux_reg(ARC_REG_PCT_INDEX, idx); /* counter # */ |
| 269 | write_aux_reg(ARC_REG_PCT_CONFIG, hwc->config); /* condition */ |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static void arc_pmu_stop(struct perf_event *event, int flags) |
| 273 | { |
| 274 | struct hw_perf_event *hwc = &event->hw; |
| 275 | int idx = hwc->idx; |
| 276 | |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 277 | /* Disable interrupt for this counter */ |
| 278 | if (is_sampling_event(event)) { |
| 279 | /* |
| 280 | * Reset interrupt flag by writing of 1. This is required |
| 281 | * to make sure pending interrupt was not left. |
| 282 | */ |
| 283 | write_aux_reg(ARC_REG_PCT_INT_ACT, 1 << idx); |
| 284 | write_aux_reg(ARC_REG_PCT_INT_CTRL, |
| 285 | read_aux_reg(ARC_REG_PCT_INT_CTRL) & ~(1 << idx)); |
| 286 | } |
| 287 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 288 | if (!(event->hw.state & PERF_HES_STOPPED)) { |
| 289 | /* stop ARC pmu here */ |
| 290 | write_aux_reg(ARC_REG_PCT_INDEX, idx); |
| 291 | |
| 292 | /* condition code #0 is always "never" */ |
| 293 | write_aux_reg(ARC_REG_PCT_CONFIG, 0); |
| 294 | |
| 295 | event->hw.state |= PERF_HES_STOPPED; |
| 296 | } |
| 297 | |
| 298 | if ((flags & PERF_EF_UPDATE) && |
| 299 | !(event->hw.state & PERF_HES_UPTODATE)) { |
| 300 | arc_perf_event_update(event, &event->hw, idx); |
| 301 | event->hw.state |= PERF_HES_UPTODATE; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static void arc_pmu_del(struct perf_event *event, int flags) |
| 306 | { |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 307 | arc_pmu_stop(event, PERF_EF_UPDATE); |
| 308 | __clear_bit(event->hw.idx, arc_pmu->used_mask); |
| 309 | |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 310 | arc_pmu->act_counter[event->hw.idx] = 0; |
| 311 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 312 | perf_event_update_userpage(event); |
| 313 | } |
| 314 | |
| 315 | /* allocate hardware counter and optionally start counting */ |
| 316 | static int arc_pmu_add(struct perf_event *event, int flags) |
| 317 | { |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 318 | struct hw_perf_event *hwc = &event->hw; |
| 319 | int idx = hwc->idx; |
| 320 | |
| 321 | if (__test_and_set_bit(idx, arc_pmu->used_mask)) { |
| 322 | idx = find_first_zero_bit(arc_pmu->used_mask, |
| 323 | arc_pmu->n_counters); |
| 324 | if (idx == arc_pmu->n_counters) |
| 325 | return -EAGAIN; |
| 326 | |
| 327 | __set_bit(idx, arc_pmu->used_mask); |
| 328 | hwc->idx = idx; |
| 329 | } |
| 330 | |
| 331 | write_aux_reg(ARC_REG_PCT_INDEX, idx); |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 332 | |
| 333 | arc_pmu->act_counter[idx] = event; |
| 334 | |
| 335 | if (is_sampling_event(event)) { |
| 336 | /* Mimic full counter overflow as other arches do */ |
| 337 | write_aux_reg(ARC_REG_PCT_INT_CNTL, (u32)arc_pmu->max_period); |
| 338 | write_aux_reg(ARC_REG_PCT_INT_CNTH, |
| 339 | (arc_pmu->max_period >> 32)); |
| 340 | } |
| 341 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 342 | write_aux_reg(ARC_REG_PCT_CONFIG, 0); |
| 343 | write_aux_reg(ARC_REG_PCT_COUNTL, 0); |
| 344 | write_aux_reg(ARC_REG_PCT_COUNTH, 0); |
| 345 | local64_set(&hwc->prev_count, 0); |
| 346 | |
| 347 | hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED; |
| 348 | if (flags & PERF_EF_START) |
| 349 | arc_pmu_start(event, PERF_EF_RELOAD); |
| 350 | |
| 351 | perf_event_update_userpage(event); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 356 | #ifdef CONFIG_ISA_ARCV2 |
| 357 | static irqreturn_t arc_pmu_intr(int irq, void *dev) |
| 358 | { |
| 359 | struct perf_sample_data data; |
| 360 | struct arc_pmu *arc_pmu = (struct arc_pmu *)dev; |
| 361 | struct pt_regs *regs; |
| 362 | int active_ints; |
| 363 | int idx; |
| 364 | |
| 365 | arc_pmu_disable(&arc_pmu->pmu); |
| 366 | |
| 367 | active_ints = read_aux_reg(ARC_REG_PCT_INT_ACT); |
| 368 | |
| 369 | regs = get_irq_regs(); |
| 370 | |
| 371 | for (idx = 0; idx < arc_pmu->n_counters; idx++) { |
| 372 | struct perf_event *event = arc_pmu->act_counter[idx]; |
| 373 | struct hw_perf_event *hwc; |
| 374 | |
| 375 | if (!(active_ints & (1 << idx))) |
| 376 | continue; |
| 377 | |
| 378 | /* Reset interrupt flag by writing of 1 */ |
| 379 | write_aux_reg(ARC_REG_PCT_INT_ACT, 1 << idx); |
| 380 | |
| 381 | /* |
| 382 | * On reset of "interrupt active" bit corresponding |
| 383 | * "interrupt enable" bit gets automatically reset as well. |
| 384 | * Now we need to re-enable interrupt for the counter. |
| 385 | */ |
| 386 | write_aux_reg(ARC_REG_PCT_INT_CTRL, |
| 387 | read_aux_reg(ARC_REG_PCT_INT_CTRL) | (1 << idx)); |
| 388 | |
| 389 | hwc = &event->hw; |
| 390 | |
| 391 | WARN_ON_ONCE(hwc->idx != idx); |
| 392 | |
| 393 | arc_perf_event_update(event, &event->hw, event->hw.idx); |
| 394 | perf_sample_data_init(&data, 0, hwc->last_period); |
| 395 | if (!arc_pmu_event_set_period(event)) |
| 396 | continue; |
| 397 | |
| 398 | if (perf_event_overflow(event, &data, regs)) |
| 399 | arc_pmu_stop(event, 0); |
| 400 | } |
| 401 | |
| 402 | arc_pmu_enable(&arc_pmu->pmu); |
| 403 | |
| 404 | return IRQ_HANDLED; |
| 405 | } |
| 406 | #else |
| 407 | |
| 408 | static irqreturn_t arc_pmu_intr(int irq, void *dev) |
| 409 | { |
| 410 | return IRQ_NONE; |
| 411 | } |
| 412 | |
| 413 | #endif /* CONFIG_ISA_ARCV2 */ |
| 414 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 415 | static int arc_pmu_device_probe(struct platform_device *pdev) |
| 416 | { |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 417 | struct arc_reg_pct_build pct_bcr; |
| 418 | struct arc_reg_cc_build cc_bcr; |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 419 | int i, j, has_interrupts; |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 420 | int counter_size; /* in bits */ |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 421 | |
| 422 | union cc_name { |
| 423 | struct { |
| 424 | uint32_t word0, word1; |
| 425 | char sentinel; |
| 426 | } indiv; |
| 427 | char str[9]; |
| 428 | } cc_name; |
| 429 | |
| 430 | |
| 431 | READ_BCR(ARC_REG_PCT_BUILD, pct_bcr); |
| 432 | if (!pct_bcr.v) { |
| 433 | pr_err("This core does not have performance counters!\n"); |
| 434 | return -ENODEV; |
| 435 | } |
Vineet Gupta | fb7c572 | 2015-08-24 13:37:01 +0300 | [diff] [blame] | 436 | BUG_ON(pct_bcr.c > ARC_PERF_MAX_COUNTERS); |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 437 | |
Vineet Gupta | 5637208 | 2014-09-25 16:54:43 +0530 | [diff] [blame] | 438 | READ_BCR(ARC_REG_CC_BUILD, cc_bcr); |
Vineet Gupta | d8f6ad8 | 2015-04-20 16:49:30 +0530 | [diff] [blame] | 439 | BUG_ON(!cc_bcr.v); /* Counters exist but No countable conditions ? */ |
Vineet Gupta | 5637208 | 2014-09-25 16:54:43 +0530 | [diff] [blame] | 440 | |
| 441 | arc_pmu = devm_kzalloc(&pdev->dev, sizeof(struct arc_pmu), GFP_KERNEL); |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 442 | if (!arc_pmu) |
| 443 | return -ENOMEM; |
| 444 | |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 445 | has_interrupts = is_isa_arcv2() ? pct_bcr.i : 0; |
| 446 | |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 447 | arc_pmu->n_counters = pct_bcr.c; |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 448 | counter_size = 32 + (pct_bcr.s << 4); |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 449 | |
Alexey Brodkin | 1fe8bfa | 2015-08-24 13:42:27 +0300 | [diff] [blame] | 450 | arc_pmu->max_period = (1ULL << counter_size) / 2 - 1ULL; |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 451 | |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 452 | pr_info("ARC perf\t: %d counters (%d bits), %d conditions%s\n", |
| 453 | arc_pmu->n_counters, counter_size, cc_bcr.c, |
| 454 | has_interrupts ? ", [overflow IRQ support]":""); |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 455 | |
| 456 | cc_name.str[8] = 0; |
Vineet Gupta | bde80c2 | 2015-04-15 19:44:07 +0530 | [diff] [blame] | 457 | for (i = 0; i < PERF_COUNT_ARC_HW_MAX; i++) |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 458 | arc_pmu->ev_hw_idx[i] = -1; |
| 459 | |
Vineet Gupta | bde80c2 | 2015-04-15 19:44:07 +0530 | [diff] [blame] | 460 | /* loop thru all available h/w condition indexes */ |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 461 | for (j = 0; j < cc_bcr.c; j++) { |
| 462 | write_aux_reg(ARC_REG_CC_INDEX, j); |
| 463 | cc_name.indiv.word0 = read_aux_reg(ARC_REG_CC_NAME0); |
| 464 | cc_name.indiv.word1 = read_aux_reg(ARC_REG_CC_NAME1); |
Vineet Gupta | bde80c2 | 2015-04-15 19:44:07 +0530 | [diff] [blame] | 465 | |
| 466 | /* See if it has been mapped to a perf event_id */ |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 467 | for (i = 0; i < ARRAY_SIZE(arc_pmu_ev_hw_map); i++) { |
| 468 | if (arc_pmu_ev_hw_map[i] && |
| 469 | !strcmp(arc_pmu_ev_hw_map[i], cc_name.str) && |
| 470 | strlen(arc_pmu_ev_hw_map[i])) { |
Vineet Gupta | bde80c2 | 2015-04-15 19:44:07 +0530 | [diff] [blame] | 471 | pr_debug("mapping perf event %2d to h/w event \'%8s\' (idx %d)\n", |
| 472 | i, cc_name.str, j); |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 473 | arc_pmu->ev_hw_idx[i] = j; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | arc_pmu->pmu = (struct pmu) { |
| 479 | .pmu_enable = arc_pmu_enable, |
| 480 | .pmu_disable = arc_pmu_disable, |
| 481 | .event_init = arc_pmu_event_init, |
| 482 | .add = arc_pmu_add, |
| 483 | .del = arc_pmu_del, |
| 484 | .start = arc_pmu_start, |
| 485 | .stop = arc_pmu_stop, |
| 486 | .read = arc_pmu_read, |
| 487 | }; |
| 488 | |
Alexey Brodkin | 36481cf | 2015-08-24 13:48:06 +0300 | [diff] [blame] | 489 | if (has_interrupts) { |
| 490 | int irq = platform_get_irq(pdev, 0); |
| 491 | |
| 492 | if (irq < 0) { |
| 493 | pr_err("Cannot get IRQ number for the platform\n"); |
| 494 | return -ENODEV; |
| 495 | } |
| 496 | |
| 497 | ret = devm_request_irq(&pdev->dev, irq, arc_pmu_intr, 0, |
| 498 | "arc-pmu", arc_pmu); |
| 499 | if (ret) { |
| 500 | pr_err("could not allocate PMU IRQ\n"); |
| 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | /* Clean all pending interrupt flags */ |
| 505 | write_aux_reg(ARC_REG_PCT_INT_ACT, 0xffffffff); |
| 506 | } else |
| 507 | arc_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT; |
Vince Weaver | 2cc9e58 | 2014-06-15 02:00:18 -0400 | [diff] [blame] | 508 | |
Tobias Klauser | 082ae1e | 2015-04-24 10:27:34 +0200 | [diff] [blame] | 509 | return perf_pmu_register(&arc_pmu->pmu, pdev->name, PERF_TYPE_RAW); |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | #ifdef CONFIG_OF |
| 513 | static const struct of_device_id arc_pmu_match[] = { |
Vineet Gupta | 30fdd37 | 2015-04-15 16:35:38 +0530 | [diff] [blame] | 514 | { .compatible = "snps,arc700-pct" }, |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 515 | {}, |
| 516 | }; |
| 517 | MODULE_DEVICE_TABLE(of, arc_pmu_match); |
| 518 | #endif |
| 519 | |
| 520 | static struct platform_driver arc_pmu_driver = { |
| 521 | .driver = { |
Vineet Gupta | 30fdd37 | 2015-04-15 16:35:38 +0530 | [diff] [blame] | 522 | .name = "arc700-pct", |
Mischa Jonker | 0dd450f | 2013-11-07 14:55:11 +0100 | [diff] [blame] | 523 | .of_match_table = of_match_ptr(arc_pmu_match), |
| 524 | }, |
| 525 | .probe = arc_pmu_device_probe, |
| 526 | }; |
| 527 | |
| 528 | module_platform_driver(arc_pmu_driver); |
| 529 | |
| 530 | MODULE_LICENSE("GPL"); |
| 531 | MODULE_AUTHOR("Mischa Jonker <mjonker@synopsys.com>"); |
| 532 | MODULE_DESCRIPTION("ARC PMU driver"); |