]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/metag/kernel/perf/perf_event.c
54fde35b4b9cc9da1830498209ab51007876cc81
[mirror_ubuntu-zesty-kernel.git] / arch / metag / kernel / perf / perf_event.c
1 /*
2 * Meta performance counter support.
3 * Copyright (C) 2012 Imagination Technologies Ltd
4 *
5 * This code is based on the sh pmu code:
6 * Copyright (C) 2009 Paul Mundt
7 *
8 * and on the arm pmu code:
9 * Copyright (C) 2009 picoChip Designs, Ltd., James Iles
10 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
11 *
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file "COPYING" in the main directory of this archive
14 * for more details.
15 */
16
17 #include <linux/atomic.h>
18 #include <linux/export.h>
19 #include <linux/init.h>
20 #include <linux/irqchip/metag.h>
21 #include <linux/perf_event.h>
22 #include <linux/slab.h>
23
24 #include <asm/core_reg.h>
25 #include <asm/io.h>
26 #include <asm/irq.h>
27 #include <asm/processor.h>
28
29 #include "perf_event.h"
30
31 static int _hw_perf_event_init(struct perf_event *);
32 static void _hw_perf_event_destroy(struct perf_event *);
33
34 /* Determines which core type we are */
35 static struct metag_pmu *metag_pmu __read_mostly;
36
37 /* Processor specific data */
38 static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
39
40 /* PMU admin */
41 const char *perf_pmu_name(void)
42 {
43 if (metag_pmu)
44 return metag_pmu->pmu.name;
45
46 return NULL;
47 }
48 EXPORT_SYMBOL_GPL(perf_pmu_name);
49
50 int perf_num_counters(void)
51 {
52 if (metag_pmu)
53 return metag_pmu->max_events;
54
55 return 0;
56 }
57 EXPORT_SYMBOL_GPL(perf_num_counters);
58
59 static inline int metag_pmu_initialised(void)
60 {
61 return !!metag_pmu;
62 }
63
64 static void release_pmu_hardware(void)
65 {
66 int irq;
67 unsigned int version = (metag_pmu->version &
68 (METAC_ID_MINOR_BITS | METAC_ID_REV_BITS)) >>
69 METAC_ID_REV_S;
70
71 /* Early cores don't have overflow interrupts */
72 if (version < 0x0104)
73 return;
74
75 irq = internal_irq_map(17);
76 if (irq >= 0)
77 free_irq(irq, (void *)1);
78
79 irq = internal_irq_map(16);
80 if (irq >= 0)
81 free_irq(irq, (void *)0);
82 }
83
84 static int reserve_pmu_hardware(void)
85 {
86 int err = 0, irq[2];
87 unsigned int version = (metag_pmu->version &
88 (METAC_ID_MINOR_BITS | METAC_ID_REV_BITS)) >>
89 METAC_ID_REV_S;
90
91 /* Early cores don't have overflow interrupts */
92 if (version < 0x0104)
93 goto out;
94
95 /*
96 * Bit 16 on HWSTATMETA is the interrupt for performance counter 0;
97 * similarly, 17 is the interrupt for performance counter 1.
98 * We can't (yet) interrupt on the cycle counter, because it's a
99 * register, however it holds a 32-bit value as opposed to 24-bit.
100 */
101 irq[0] = internal_irq_map(16);
102 if (irq[0] < 0) {
103 pr_err("unable to map internal IRQ %d\n", 16);
104 goto out;
105 }
106 err = request_irq(irq[0], metag_pmu->handle_irq, IRQF_NOBALANCING,
107 "metagpmu0", (void *)0);
108 if (err) {
109 pr_err("unable to request IRQ%d for metag PMU counters\n",
110 irq[0]);
111 goto out;
112 }
113
114 irq[1] = internal_irq_map(17);
115 if (irq[1] < 0) {
116 pr_err("unable to map internal IRQ %d\n", 17);
117 goto out_irq1;
118 }
119 err = request_irq(irq[1], metag_pmu->handle_irq, IRQF_NOBALANCING,
120 "metagpmu1", (void *)1);
121 if (err) {
122 pr_err("unable to request IRQ%d for metag PMU counters\n",
123 irq[1]);
124 goto out_irq1;
125 }
126
127 return 0;
128
129 out_irq1:
130 free_irq(irq[0], (void *)0);
131 out:
132 return err;
133 }
134
135 /* PMU operations */
136 static void metag_pmu_enable(struct pmu *pmu)
137 {
138 }
139
140 static void metag_pmu_disable(struct pmu *pmu)
141 {
142 }
143
144 static int metag_pmu_event_init(struct perf_event *event)
145 {
146 int err = 0;
147 atomic_t *active_events = &metag_pmu->active_events;
148
149 if (!metag_pmu_initialised()) {
150 err = -ENODEV;
151 goto out;
152 }
153
154 if (has_branch_stack(event))
155 return -EOPNOTSUPP;
156
157 event->destroy = _hw_perf_event_destroy;
158
159 if (!atomic_inc_not_zero(active_events)) {
160 mutex_lock(&metag_pmu->reserve_mutex);
161 if (atomic_read(active_events) == 0)
162 err = reserve_pmu_hardware();
163
164 if (!err)
165 atomic_inc(active_events);
166
167 mutex_unlock(&metag_pmu->reserve_mutex);
168 }
169
170 /* Hardware and caches counters */
171 switch (event->attr.type) {
172 case PERF_TYPE_HARDWARE:
173 case PERF_TYPE_HW_CACHE:
174 err = _hw_perf_event_init(event);
175 break;
176
177 default:
178 return -ENOENT;
179 }
180
181 if (err)
182 event->destroy(event);
183
184 out:
185 return err;
186 }
187
188 void metag_pmu_event_update(struct perf_event *event,
189 struct hw_perf_event *hwc, int idx)
190 {
191 u64 prev_raw_count, new_raw_count;
192 s64 delta;
193
194 /*
195 * If this counter is chained, it may be that the previous counter
196 * value has been changed beneath us.
197 *
198 * To get around this, we read and exchange the new raw count, then
199 * add the delta (new - prev) to the generic counter atomically.
200 *
201 * Without interrupts, this is the simplest approach.
202 */
203 again:
204 prev_raw_count = local64_read(&hwc->prev_count);
205 new_raw_count = metag_pmu->read(idx);
206
207 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
208 new_raw_count) != prev_raw_count)
209 goto again;
210
211 /*
212 * Calculate the delta and add it to the counter.
213 */
214 delta = (new_raw_count - prev_raw_count) & MAX_PERIOD;
215
216 local64_add(delta, &event->count);
217 local64_sub(delta, &hwc->period_left);
218 }
219
220 int metag_pmu_event_set_period(struct perf_event *event,
221 struct hw_perf_event *hwc, int idx)
222 {
223 s64 left = local64_read(&hwc->period_left);
224 s64 period = hwc->sample_period;
225 int ret = 0;
226
227 /* The period may have been changed */
228 if (unlikely(period != hwc->last_period))
229 left += period - hwc->last_period;
230
231 if (unlikely(left <= -period)) {
232 left = period;
233 local64_set(&hwc->period_left, left);
234 hwc->last_period = period;
235 ret = 1;
236 }
237
238 if (unlikely(left <= 0)) {
239 left += period;
240 local64_set(&hwc->period_left, left);
241 hwc->last_period = period;
242 ret = 1;
243 }
244
245 if (left > (s64)metag_pmu->max_period)
246 left = metag_pmu->max_period;
247
248 if (metag_pmu->write) {
249 local64_set(&hwc->prev_count, -(s32)left);
250 metag_pmu->write(idx, -left & MAX_PERIOD);
251 }
252
253 perf_event_update_userpage(event);
254
255 return ret;
256 }
257
258 static void metag_pmu_start(struct perf_event *event, int flags)
259 {
260 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
261 struct hw_perf_event *hwc = &event->hw;
262 int idx = hwc->idx;
263
264 if (WARN_ON_ONCE(idx == -1))
265 return;
266
267 /*
268 * We always have to reprogram the period, so ignore PERF_EF_RELOAD.
269 */
270 if (flags & PERF_EF_RELOAD)
271 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
272
273 hwc->state = 0;
274
275 /*
276 * Reset the period.
277 * Some counters can't be stopped (i.e. are core global), so when the
278 * counter was 'stopped' we merely disabled the IRQ. If we don't reset
279 * the period, then we'll either: a) get an overflow too soon;
280 * or b) too late if the overflow happened since disabling.
281 * Obviously, this has little bearing on cores without the overflow
282 * interrupt, as the performance counter resets to zero on write
283 * anyway.
284 */
285 if (metag_pmu->max_period)
286 metag_pmu_event_set_period(event, hwc, hwc->idx);
287 cpuc->events[idx] = event;
288 metag_pmu->enable(hwc, idx);
289 }
290
291 static void metag_pmu_stop(struct perf_event *event, int flags)
292 {
293 struct hw_perf_event *hwc = &event->hw;
294
295 /*
296 * We should always update the counter on stop; see comment above
297 * why.
298 */
299 if (!(hwc->state & PERF_HES_STOPPED)) {
300 metag_pmu_event_update(event, hwc, hwc->idx);
301 metag_pmu->disable(hwc, hwc->idx);
302 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
303 }
304 }
305
306 static int metag_pmu_add(struct perf_event *event, int flags)
307 {
308 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
309 struct hw_perf_event *hwc = &event->hw;
310 int idx = 0, ret = 0;
311
312 perf_pmu_disable(event->pmu);
313
314 /* check whether we're counting instructions */
315 if (hwc->config == 0x100) {
316 if (__test_and_set_bit(METAG_INST_COUNTER,
317 cpuc->used_mask)) {
318 ret = -EAGAIN;
319 goto out;
320 }
321 idx = METAG_INST_COUNTER;
322 } else {
323 /* Check whether we have a spare counter */
324 idx = find_first_zero_bit(cpuc->used_mask,
325 atomic_read(&metag_pmu->active_events));
326 if (idx >= METAG_INST_COUNTER) {
327 ret = -EAGAIN;
328 goto out;
329 }
330
331 __set_bit(idx, cpuc->used_mask);
332 }
333 hwc->idx = idx;
334
335 /* Make sure the counter is disabled */
336 metag_pmu->disable(hwc, idx);
337
338 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
339 if (flags & PERF_EF_START)
340 metag_pmu_start(event, PERF_EF_RELOAD);
341
342 perf_event_update_userpage(event);
343 out:
344 perf_pmu_enable(event->pmu);
345 return ret;
346 }
347
348 static void metag_pmu_del(struct perf_event *event, int flags)
349 {
350 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
351 struct hw_perf_event *hwc = &event->hw;
352 int idx = hwc->idx;
353
354 WARN_ON(idx < 0);
355 metag_pmu_stop(event, PERF_EF_UPDATE);
356 cpuc->events[idx] = NULL;
357 __clear_bit(idx, cpuc->used_mask);
358
359 perf_event_update_userpage(event);
360 }
361
362 static void metag_pmu_read(struct perf_event *event)
363 {
364 struct hw_perf_event *hwc = &event->hw;
365
366 /* Don't read disabled counters! */
367 if (hwc->idx < 0)
368 return;
369
370 metag_pmu_event_update(event, hwc, hwc->idx);
371 }
372
373 static struct pmu pmu = {
374 .pmu_enable = metag_pmu_enable,
375 .pmu_disable = metag_pmu_disable,
376
377 .event_init = metag_pmu_event_init,
378
379 .add = metag_pmu_add,
380 .del = metag_pmu_del,
381 .start = metag_pmu_start,
382 .stop = metag_pmu_stop,
383 .read = metag_pmu_read,
384 };
385
386 /* Core counter specific functions */
387 static const int metag_general_events[] = {
388 [PERF_COUNT_HW_CPU_CYCLES] = 0x03,
389 [PERF_COUNT_HW_INSTRUCTIONS] = 0x100,
390 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
391 [PERF_COUNT_HW_CACHE_MISSES] = -1,
392 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
393 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
394 [PERF_COUNT_HW_BUS_CYCLES] = -1,
395 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = -1,
396 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = -1,
397 [PERF_COUNT_HW_REF_CPU_CYCLES] = -1,
398 };
399
400 static const int metag_pmu_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
401 [C(L1D)] = {
402 [C(OP_READ)] = {
403 [C(RESULT_ACCESS)] = 0x08,
404 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
405 },
406 [C(OP_WRITE)] = {
407 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
408 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
409 },
410 [C(OP_PREFETCH)] = {
411 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
412 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
413 },
414 },
415 [C(L1I)] = {
416 [C(OP_READ)] = {
417 [C(RESULT_ACCESS)] = 0x09,
418 [C(RESULT_MISS)] = 0x0a,
419 },
420 [C(OP_WRITE)] = {
421 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
422 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
423 },
424 [C(OP_PREFETCH)] = {
425 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
426 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
427 },
428 },
429 [C(LL)] = {
430 [C(OP_READ)] = {
431 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
432 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
433 },
434 [C(OP_WRITE)] = {
435 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
436 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
437 },
438 [C(OP_PREFETCH)] = {
439 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
440 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
441 },
442 },
443 [C(DTLB)] = {
444 [C(OP_READ)] = {
445 [C(RESULT_ACCESS)] = 0xd0,
446 [C(RESULT_MISS)] = 0xd2,
447 },
448 [C(OP_WRITE)] = {
449 [C(RESULT_ACCESS)] = 0xd4,
450 [C(RESULT_MISS)] = 0xd5,
451 },
452 [C(OP_PREFETCH)] = {
453 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
454 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
455 },
456 },
457 [C(ITLB)] = {
458 [C(OP_READ)] = {
459 [C(RESULT_ACCESS)] = 0xd1,
460 [C(RESULT_MISS)] = 0xd3,
461 },
462 [C(OP_WRITE)] = {
463 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
464 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
465 },
466 [C(OP_PREFETCH)] = {
467 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
468 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
469 },
470 },
471 [C(BPU)] = {
472 [C(OP_READ)] = {
473 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
474 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
475 },
476 [C(OP_WRITE)] = {
477 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
478 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
479 },
480 [C(OP_PREFETCH)] = {
481 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
482 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
483 },
484 },
485 [C(NODE)] = {
486 [C(OP_READ)] = {
487 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
488 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
489 },
490 [C(OP_WRITE)] = {
491 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
492 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
493 },
494 [C(OP_PREFETCH)] = {
495 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
496 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
497 },
498 },
499 };
500
501
502 static void _hw_perf_event_destroy(struct perf_event *event)
503 {
504 atomic_t *active_events = &metag_pmu->active_events;
505 struct mutex *pmu_mutex = &metag_pmu->reserve_mutex;
506
507 if (atomic_dec_and_mutex_lock(active_events, pmu_mutex)) {
508 release_pmu_hardware();
509 mutex_unlock(pmu_mutex);
510 }
511 }
512
513 static int _hw_perf_cache_event(int config, int *evp)
514 {
515 unsigned long type, op, result;
516 int ev;
517
518 if (!metag_pmu->cache_events)
519 return -EINVAL;
520
521 /* Unpack config */
522 type = config & 0xff;
523 op = (config >> 8) & 0xff;
524 result = (config >> 16) & 0xff;
525
526 if (type >= PERF_COUNT_HW_CACHE_MAX ||
527 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
528 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
529 return -EINVAL;
530
531 ev = (*metag_pmu->cache_events)[type][op][result];
532 if (ev == 0)
533 return -EOPNOTSUPP;
534 if (ev == -1)
535 return -EINVAL;
536 *evp = ev;
537 return 0;
538 }
539
540 static int _hw_perf_event_init(struct perf_event *event)
541 {
542 struct perf_event_attr *attr = &event->attr;
543 struct hw_perf_event *hwc = &event->hw;
544 int mapping = 0, err;
545
546 switch (attr->type) {
547 case PERF_TYPE_HARDWARE:
548 if (attr->config >= PERF_COUNT_HW_MAX)
549 return -EINVAL;
550
551 mapping = metag_pmu->event_map(attr->config);
552 break;
553
554 case PERF_TYPE_HW_CACHE:
555 err = _hw_perf_cache_event(attr->config, &mapping);
556 if (err)
557 return err;
558 break;
559 }
560
561 /* Return early if the event is unsupported */
562 if (mapping == -1)
563 return -EINVAL;
564
565 /*
566 * Early cores have "limited" counters - they have no overflow
567 * interrupts - and so are unable to do sampling without extra work
568 * and timer assistance.
569 */
570 if (metag_pmu->max_period == 0) {
571 if (hwc->sample_period)
572 return -EINVAL;
573 }
574
575 /*
576 * Don't assign an index until the event is placed into the hardware.
577 * -1 signifies that we're still deciding where to put it. On SMP
578 * systems each core has its own set of counters, so we can't do any
579 * constraint checking yet.
580 */
581 hwc->idx = -1;
582
583 /* Store the event encoding */
584 hwc->config |= (unsigned long)mapping;
585
586 /*
587 * For non-sampling runs, limit the sample_period to half of the
588 * counter width. This way, the new counter value should be less
589 * likely to overtake the previous one (unless there are IRQ latency
590 * issues...)
591 */
592 if (metag_pmu->max_period) {
593 if (!hwc->sample_period) {
594 hwc->sample_period = metag_pmu->max_period >> 1;
595 hwc->last_period = hwc->sample_period;
596 local64_set(&hwc->period_left, hwc->sample_period);
597 }
598 }
599
600 return 0;
601 }
602
603 static void metag_pmu_enable_counter(struct hw_perf_event *event, int idx)
604 {
605 struct cpu_hw_events *events = &__get_cpu_var(cpu_hw_events);
606 unsigned int config = event->config;
607 unsigned int tmp = config & 0xf0;
608 unsigned long flags;
609
610 raw_spin_lock_irqsave(&events->pmu_lock, flags);
611
612 /*
613 * Check if we're enabling the instruction counter (index of
614 * MAX_HWEVENTS - 1)
615 */
616 if (METAG_INST_COUNTER == idx) {
617 WARN_ONCE((config != 0x100),
618 "invalid configuration (%d) for counter (%d)\n",
619 config, idx);
620
621 /* Reset the cycle count */
622 __core_reg_set(TXTACTCYC, 0);
623 goto unlock;
624 }
625
626 /* Check for a core internal or performance channel event. */
627 if (tmp) {
628 void *perf_addr = (void *)PERF_COUNT(idx);
629
630 /*
631 * Anything other than a cycle count will write the low-
632 * nibble to the correct counter register.
633 */
634 switch (tmp) {
635 case 0xd0:
636 perf_addr = (void *)PERF_ICORE(idx);
637 break;
638
639 case 0xf0:
640 perf_addr = (void *)PERF_CHAN(idx);
641 break;
642 }
643
644 metag_out32((config & 0x0f), perf_addr);
645
646 /*
647 * Now we use the high nibble as the performance event to
648 * to count.
649 */
650 config = tmp >> 4;
651 }
652
653 tmp = ((config & 0xf) << 28) |
654 ((1 << 24) << hard_processor_id());
655 if (metag_pmu->max_period)
656 /*
657 * Cores supporting overflow interrupts may have had the counter
658 * set to a specific value that needs preserving.
659 */
660 tmp |= metag_in32(PERF_COUNT(idx)) & 0x00ffffff;
661 else
662 /*
663 * Older cores reset the counter on write, so prev_count needs
664 * resetting too so we can calculate a correct delta.
665 */
666 local64_set(&event->prev_count, 0);
667
668 metag_out32(tmp, PERF_COUNT(idx));
669 unlock:
670 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
671 }
672
673 static void metag_pmu_disable_counter(struct hw_perf_event *event, int idx)
674 {
675 struct cpu_hw_events *events = &__get_cpu_var(cpu_hw_events);
676 unsigned int tmp = 0;
677 unsigned long flags;
678
679 /*
680 * The cycle counter can't be disabled per se, as it's a hardware
681 * thread register which is always counting. We merely return if this
682 * is the counter we're attempting to disable.
683 */
684 if (METAG_INST_COUNTER == idx)
685 return;
686
687 /*
688 * The counter value _should_ have been read prior to disabling,
689 * as if we're running on an early core then the value gets reset to
690 * 0, and any read after that would be useless. On the newer cores,
691 * however, it's better to read-modify-update this for purposes of
692 * the overflow interrupt.
693 * Here we remove the thread id AND the event nibble (there are at
694 * least two events that count events that are core global and ignore
695 * the thread id mask). This only works because we don't mix thread
696 * performance counts, and event 0x00 requires a thread id mask!
697 */
698 raw_spin_lock_irqsave(&events->pmu_lock, flags);
699
700 tmp = metag_in32(PERF_COUNT(idx));
701 tmp &= 0x00ffffff;
702 metag_out32(tmp, PERF_COUNT(idx));
703
704 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
705 }
706
707 static u64 metag_pmu_read_counter(int idx)
708 {
709 u32 tmp = 0;
710
711 /* The act of reading the cycle counter also clears it */
712 if (METAG_INST_COUNTER == idx) {
713 __core_reg_swap(TXTACTCYC, tmp);
714 goto out;
715 }
716
717 tmp = metag_in32(PERF_COUNT(idx)) & 0x00ffffff;
718 out:
719 return tmp;
720 }
721
722 static void metag_pmu_write_counter(int idx, u32 val)
723 {
724 struct cpu_hw_events *events = &__get_cpu_var(cpu_hw_events);
725 u32 tmp = 0;
726 unsigned long flags;
727
728 /*
729 * This _shouldn't_ happen, but if it does, then we can just
730 * ignore the write, as the register is read-only and clear-on-write.
731 */
732 if (METAG_INST_COUNTER == idx)
733 return;
734
735 /*
736 * We'll keep the thread mask and event id, and just update the
737 * counter itself. Also , we should bound the value to 24-bits.
738 */
739 raw_spin_lock_irqsave(&events->pmu_lock, flags);
740
741 val &= 0x00ffffff;
742 tmp = metag_in32(PERF_COUNT(idx)) & 0xff000000;
743 val |= tmp;
744 metag_out32(val, PERF_COUNT(idx));
745
746 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
747 }
748
749 static int metag_pmu_event_map(int idx)
750 {
751 return metag_general_events[idx];
752 }
753
754 static irqreturn_t metag_pmu_counter_overflow(int irq, void *dev)
755 {
756 int idx = (int)dev;
757 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
758 struct perf_event *event = cpuhw->events[idx];
759 struct hw_perf_event *hwc = &event->hw;
760 struct pt_regs *regs = get_irq_regs();
761 struct perf_sample_data sampledata;
762 unsigned long flags;
763 u32 counter = 0;
764
765 /*
766 * We need to stop the core temporarily from generating another
767 * interrupt while we disable this counter. However, we don't want
768 * to flag the counter as free
769 */
770 __global_lock2(flags);
771 counter = metag_in32(PERF_COUNT(idx));
772 metag_out32((counter & 0x00ffffff), PERF_COUNT(idx));
773 __global_unlock2(flags);
774
775 /* Update the counts and reset the sample period */
776 metag_pmu_event_update(event, hwc, idx);
777 perf_sample_data_init(&sampledata, 0, hwc->last_period);
778 metag_pmu_event_set_period(event, hwc, idx);
779
780 /*
781 * Enable the counter again once core overflow processing has
782 * completed. Note the counter value may have been modified while it was
783 * inactive to set it up ready for the next interrupt.
784 */
785 if (!perf_event_overflow(event, &sampledata, regs)) {
786 __global_lock2(flags);
787 counter = (counter & 0xff000000) |
788 (metag_in32(PERF_COUNT(idx)) & 0x00ffffff);
789 metag_out32(counter, PERF_COUNT(idx));
790 __global_unlock2(flags);
791 }
792
793 return IRQ_HANDLED;
794 }
795
796 static struct metag_pmu _metag_pmu = {
797 .handle_irq = metag_pmu_counter_overflow,
798 .enable = metag_pmu_enable_counter,
799 .disable = metag_pmu_disable_counter,
800 .read = metag_pmu_read_counter,
801 .write = metag_pmu_write_counter,
802 .event_map = metag_pmu_event_map,
803 .cache_events = &metag_pmu_cache_events,
804 .max_period = MAX_PERIOD,
805 .max_events = MAX_HWEVENTS,
806 };
807
808 /* PMU CPU hotplug notifier */
809 static int __cpuinit metag_pmu_cpu_notify(struct notifier_block *b,
810 unsigned long action, void *hcpu)
811 {
812 unsigned int cpu = (unsigned int)hcpu;
813 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
814
815 if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
816 return NOTIFY_DONE;
817
818 memset(cpuc, 0, sizeof(struct cpu_hw_events));
819 raw_spin_lock_init(&cpuc->pmu_lock);
820
821 return NOTIFY_OK;
822 }
823
824 static struct notifier_block __cpuinitdata metag_pmu_notifier = {
825 .notifier_call = metag_pmu_cpu_notify,
826 };
827
828 /* PMU Initialisation */
829 static int __init init_hw_perf_events(void)
830 {
831 int ret = 0, cpu;
832 u32 version = *(u32 *)METAC_ID;
833 int major = (version & METAC_ID_MAJOR_BITS) >> METAC_ID_MAJOR_S;
834 int min_rev = (version & (METAC_ID_MINOR_BITS | METAC_ID_REV_BITS))
835 >> METAC_ID_REV_S;
836
837 /* Not a Meta 2 core, then not supported */
838 if (0x02 > major) {
839 pr_info("no hardware counter support available\n");
840 goto out;
841 } else if (0x02 == major) {
842 metag_pmu = &_metag_pmu;
843
844 if (min_rev < 0x0104) {
845 /*
846 * A core without overflow interrupts, and clear-on-
847 * write counters.
848 */
849 metag_pmu->handle_irq = NULL;
850 metag_pmu->write = NULL;
851 metag_pmu->max_period = 0;
852 }
853
854 metag_pmu->name = "Meta 2";
855 metag_pmu->version = version;
856 metag_pmu->pmu = pmu;
857 }
858
859 pr_info("enabled with %s PMU driver, %d counters available\n",
860 metag_pmu->name, metag_pmu->max_events);
861
862 /* Initialise the active events and reservation mutex */
863 atomic_set(&metag_pmu->active_events, 0);
864 mutex_init(&metag_pmu->reserve_mutex);
865
866 /* Clear the counters */
867 metag_out32(0, PERF_COUNT(0));
868 metag_out32(0, PERF_COUNT(1));
869
870 for_each_possible_cpu(cpu) {
871 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
872
873 memset(cpuc, 0, sizeof(struct cpu_hw_events));
874 raw_spin_lock_init(&cpuc->pmu_lock);
875 }
876
877 register_cpu_notifier(&metag_pmu_notifier);
878 ret = perf_pmu_register(&pmu, (char *)metag_pmu->name, PERF_TYPE_RAW);
879 out:
880 return ret;
881 }
882 early_initcall(init_hw_perf_events);