1
2
3
4
5
6
7
8
9
10
11
12#include <linux/perf_event.h>
13#include <linux/kprobes.h>
14#include <linux/kernel.h>
15#include <linux/kdebug.h>
16#include <linux/mutex.h>
17#include <linux/init.h>
18
19#include <asm/hwrpb.h>
20#include <linux/atomic.h>
21#include <asm/irq.h>
22#include <asm/irq_regs.h>
23#include <asm/pal.h>
24#include <asm/wrperfmon.h>
25#include <asm/hw_irq.h>
26
27
28
29#define MAX_HWEVENTS 3
30#define PMC_NO_INDEX -1
31
32
33struct cpu_hw_events {
34 int enabled;
35
36 int n_events;
37
38 int n_added;
39
40 struct perf_event *event[MAX_HWEVENTS];
41
42 unsigned long evtype[MAX_HWEVENTS];
43
44
45
46 int current_idx[MAX_HWEVENTS];
47
48 unsigned long config;
49
50 unsigned long idx_mask;
51};
52DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
53
54
55
56
57
58
59
60struct alpha_pmu_t {
61
62 const int *event_map;
63
64 int max_events;
65
66 int num_pmcs;
67
68
69
70
71 int pmc_count_shift[MAX_HWEVENTS];
72
73
74
75
76 unsigned long pmc_count_mask[MAX_HWEVENTS];
77
78 unsigned long pmc_max_period[MAX_HWEVENTS];
79
80
81
82
83 long pmc_left[3];
84
85 int (*check_constraints)(struct perf_event **, unsigned long *, int);
86};
87
88
89
90
91
92static const struct alpha_pmu_t *alpha_pmu;
93
94
95#define HW_OP_UNSUPPORTED -1
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110enum ev67_pmc_event_type {
111 EV67_CYCLES = 1,
112 EV67_INSTRUCTIONS,
113 EV67_BCACHEMISS,
114 EV67_MBOXREPLAY,
115 EV67_LAST_ET
116};
117#define EV67_NUM_EVENT_TYPES (EV67_LAST_ET-EV67_CYCLES)
118
119
120
121static const int ev67_perfmon_event_map[] = {
122 [PERF_COUNT_HW_CPU_CYCLES] = EV67_CYCLES,
123 [PERF_COUNT_HW_INSTRUCTIONS] = EV67_INSTRUCTIONS,
124 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
125 [PERF_COUNT_HW_CACHE_MISSES] = EV67_BCACHEMISS,
126};
127
128struct ev67_mapping_t {
129 int config;
130 int idx;
131};
132
133
134
135
136
137static const struct ev67_mapping_t ev67_mapping[] = {
138 {EV67_PCTR_INSTR_CYCLES, 1},
139 {EV67_PCTR_INSTR_CYCLES, 0},
140 {EV67_PCTR_INSTR_BCACHEMISS, 1},
141 {EV67_PCTR_CYCLES_MBOX, 1}
142};
143
144
145
146
147
148
149static int ev67_check_constraints(struct perf_event **event,
150 unsigned long *evtype, int n_ev)
151{
152 int idx0;
153 unsigned long config;
154
155 idx0 = ev67_mapping[evtype[0]-1].idx;
156 config = ev67_mapping[evtype[0]-1].config;
157 if (n_ev == 1)
158 goto success;
159
160 BUG_ON(n_ev != 2);
161
162 if (evtype[0] == EV67_MBOXREPLAY || evtype[1] == EV67_MBOXREPLAY) {
163
164 idx0 = (evtype[0] == EV67_MBOXREPLAY) ? 1 : 0;
165
166 if (evtype[idx0] == EV67_CYCLES) {
167 config = EV67_PCTR_CYCLES_MBOX;
168 goto success;
169 }
170 }
171
172 if (evtype[0] == EV67_BCACHEMISS || evtype[1] == EV67_BCACHEMISS) {
173
174 idx0 = (evtype[0] == EV67_BCACHEMISS) ? 1 : 0;
175
176 if (evtype[idx0] == EV67_INSTRUCTIONS) {
177 config = EV67_PCTR_INSTR_BCACHEMISS;
178 goto success;
179 }
180 }
181
182 if (evtype[0] == EV67_INSTRUCTIONS || evtype[1] == EV67_INSTRUCTIONS) {
183
184 idx0 = (evtype[0] == EV67_INSTRUCTIONS) ? 0 : 1;
185
186 if (evtype[idx0^1] == EV67_CYCLES) {
187 config = EV67_PCTR_INSTR_CYCLES;
188 goto success;
189 }
190 }
191
192
193 return -1;
194
195success:
196 event[0]->hw.idx = idx0;
197 event[0]->hw.config_base = config;
198 if (n_ev == 2) {
199 event[1]->hw.idx = idx0 ^ 1;
200 event[1]->hw.config_base = config;
201 }
202 return 0;
203}
204
205
206static const struct alpha_pmu_t ev67_pmu = {
207 .event_map = ev67_perfmon_event_map,
208 .max_events = ARRAY_SIZE(ev67_perfmon_event_map),
209 .num_pmcs = 2,
210 .pmc_count_shift = {EV67_PCTR_0_COUNT_SHIFT, EV67_PCTR_1_COUNT_SHIFT, 0},
211 .pmc_count_mask = {EV67_PCTR_0_COUNT_MASK, EV67_PCTR_1_COUNT_MASK, 0},
212 .pmc_max_period = {(1UL<<20) - 1, (1UL<<20) - 1, 0},
213 .pmc_left = {16, 4, 0},
214 .check_constraints = ev67_check_constraints
215};
216
217
218
219
220
221
222
223static inline void alpha_write_pmc(int idx, unsigned long val)
224{
225 val &= alpha_pmu->pmc_count_mask[idx];
226 val <<= alpha_pmu->pmc_count_shift[idx];
227 val |= (1<<idx);
228 wrperfmon(PERFMON_CMD_WRITE, val);
229}
230
231static inline unsigned long alpha_read_pmc(int idx)
232{
233 unsigned long val;
234
235 val = wrperfmon(PERFMON_CMD_READ, 0);
236 val >>= alpha_pmu->pmc_count_shift[idx];
237 val &= alpha_pmu->pmc_count_mask[idx];
238 return val;
239}
240
241
242static int alpha_perf_event_set_period(struct perf_event *event,
243 struct hw_perf_event *hwc, int idx)
244{
245 long left = local64_read(&hwc->period_left);
246 long period = hwc->sample_period;
247 int ret = 0;
248
249 if (unlikely(left <= -period)) {
250 left = period;
251 local64_set(&hwc->period_left, left);
252 hwc->last_period = period;
253 ret = 1;
254 }
255
256 if (unlikely(left <= 0)) {
257 left += period;
258 local64_set(&hwc->period_left, left);
259 hwc->last_period = period;
260 ret = 1;
261 }
262
263
264
265
266
267 if (unlikely(left < alpha_pmu->pmc_left[idx]))
268 left = alpha_pmu->pmc_left[idx];
269
270 if (left > (long)alpha_pmu->pmc_max_period[idx])
271 left = alpha_pmu->pmc_max_period[idx];
272
273 local64_set(&hwc->prev_count, (unsigned long)(-left));
274
275 alpha_write_pmc(idx, (unsigned long)(-left));
276
277 perf_event_update_userpage(event);
278
279 return ret;
280}
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297static unsigned long alpha_perf_event_update(struct perf_event *event,
298 struct hw_perf_event *hwc, int idx, long ovf)
299{
300 long prev_raw_count, new_raw_count;
301 long delta;
302
303again:
304 prev_raw_count = local64_read(&hwc->prev_count);
305 new_raw_count = alpha_read_pmc(idx);
306
307 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
308 new_raw_count) != prev_raw_count)
309 goto again;
310
311 delta = (new_raw_count - (prev_raw_count & alpha_pmu->pmc_count_mask[idx])) + ovf;
312
313
314
315
316 if (unlikely(delta < 0)) {
317 delta += alpha_pmu->pmc_max_period[idx] + 1;
318 }
319
320 local64_add(delta, &event->count);
321 local64_sub(delta, &hwc->period_left);
322
323 return new_raw_count;
324}
325
326
327
328
329
330static int collect_events(struct perf_event *group, int max_count,
331 struct perf_event *event[], unsigned long *evtype,
332 int *current_idx)
333{
334 struct perf_event *pe;
335 int n = 0;
336
337 if (!is_software_event(group)) {
338 if (n >= max_count)
339 return -1;
340 event[n] = group;
341 evtype[n] = group->hw.event_base;
342 current_idx[n++] = PMC_NO_INDEX;
343 }
344 list_for_each_entry(pe, &group->sibling_list, group_entry) {
345 if (!is_software_event(pe) && pe->state != PERF_EVENT_STATE_OFF) {
346 if (n >= max_count)
347 return -1;
348 event[n] = pe;
349 evtype[n] = pe->hw.event_base;
350 current_idx[n++] = PMC_NO_INDEX;
351 }
352 }
353 return n;
354}
355
356
357
358
359
360
361static int alpha_check_constraints(struct perf_event **events,
362 unsigned long *evtypes, int n_ev)
363{
364
365
366 if (n_ev == 0)
367 return 0;
368
369 if (n_ev > alpha_pmu->num_pmcs)
370 return -1;
371
372 return alpha_pmu->check_constraints(events, evtypes, n_ev);
373}
374
375
376
377
378
379
380
381static void maybe_change_configuration(struct cpu_hw_events *cpuc)
382{
383 int j;
384
385 if (cpuc->n_added == 0)
386 return;
387
388
389 for (j = 0; j < cpuc->n_events; j++) {
390 struct perf_event *pe = cpuc->event[j];
391
392 if (cpuc->current_idx[j] != PMC_NO_INDEX &&
393 cpuc->current_idx[j] != pe->hw.idx) {
394 alpha_perf_event_update(pe, &pe->hw, cpuc->current_idx[j], 0);
395 cpuc->current_idx[j] = PMC_NO_INDEX;
396 }
397 }
398
399
400 cpuc->idx_mask = 0;
401 for (j = 0; j < cpuc->n_events; j++) {
402 struct perf_event *pe = cpuc->event[j];
403 struct hw_perf_event *hwc = &pe->hw;
404 int idx = hwc->idx;
405
406 if (cpuc->current_idx[j] == PMC_NO_INDEX) {
407 alpha_perf_event_set_period(pe, hwc, idx);
408 cpuc->current_idx[j] = idx;
409 }
410
411 if (!(hwc->state & PERF_HES_STOPPED))
412 cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
413 }
414 cpuc->config = cpuc->event[0]->hw.config_base;
415}
416
417
418
419
420
421
422
423static int alpha_pmu_add(struct perf_event *event, int flags)
424{
425 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
426 struct hw_perf_event *hwc = &event->hw;
427 int n0;
428 int ret;
429 unsigned long irq_flags;
430
431
432
433
434
435
436
437
438
439 perf_pmu_disable(event->pmu);
440 local_irq_save(irq_flags);
441
442
443 ret = -EAGAIN;
444
445
446 n0 = cpuc->n_events;
447 if (n0 < alpha_pmu->num_pmcs) {
448 cpuc->event[n0] = event;
449 cpuc->evtype[n0] = event->hw.event_base;
450 cpuc->current_idx[n0] = PMC_NO_INDEX;
451
452 if (!alpha_check_constraints(cpuc->event, cpuc->evtype, n0+1)) {
453 cpuc->n_events++;
454 cpuc->n_added++;
455 ret = 0;
456 }
457 }
458
459 hwc->state = PERF_HES_UPTODATE;
460 if (!(flags & PERF_EF_START))
461 hwc->state |= PERF_HES_STOPPED;
462
463 local_irq_restore(irq_flags);
464 perf_pmu_enable(event->pmu);
465
466 return ret;
467}
468
469
470
471
472
473
474
475static void alpha_pmu_del(struct perf_event *event, int flags)
476{
477 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
478 struct hw_perf_event *hwc = &event->hw;
479 unsigned long irq_flags;
480 int j;
481
482 perf_pmu_disable(event->pmu);
483 local_irq_save(irq_flags);
484
485 for (j = 0; j < cpuc->n_events; j++) {
486 if (event == cpuc->event[j]) {
487 int idx = cpuc->current_idx[j];
488
489
490
491
492 while (++j < cpuc->n_events) {
493 cpuc->event[j - 1] = cpuc->event[j];
494 cpuc->evtype[j - 1] = cpuc->evtype[j];
495 cpuc->current_idx[j - 1] =
496 cpuc->current_idx[j];
497 }
498
499
500 alpha_perf_event_update(event, hwc, idx, 0);
501 perf_event_update_userpage(event);
502
503 cpuc->idx_mask &= ~(1UL<<idx);
504 cpuc->n_events--;
505 break;
506 }
507 }
508
509 local_irq_restore(irq_flags);
510 perf_pmu_enable(event->pmu);
511}
512
513
514static void alpha_pmu_read(struct perf_event *event)
515{
516 struct hw_perf_event *hwc = &event->hw;
517
518 alpha_perf_event_update(event, hwc, hwc->idx, 0);
519}
520
521
522static void alpha_pmu_stop(struct perf_event *event, int flags)
523{
524 struct hw_perf_event *hwc = &event->hw;
525 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
526
527 if (!(hwc->state & PERF_HES_STOPPED)) {
528 cpuc->idx_mask &= ~(1UL<<hwc->idx);
529 hwc->state |= PERF_HES_STOPPED;
530 }
531
532 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
533 alpha_perf_event_update(event, hwc, hwc->idx, 0);
534 hwc->state |= PERF_HES_UPTODATE;
535 }
536
537 if (cpuc->enabled)
538 wrperfmon(PERFMON_CMD_DISABLE, (1UL<<hwc->idx));
539}
540
541
542static void alpha_pmu_start(struct perf_event *event, int flags)
543{
544 struct hw_perf_event *hwc = &event->hw;
545 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
546
547 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
548 return;
549
550 if (flags & PERF_EF_RELOAD) {
551 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
552 alpha_perf_event_set_period(event, hwc, hwc->idx);
553 }
554
555 hwc->state = 0;
556
557 cpuc->idx_mask |= 1UL<<hwc->idx;
558 if (cpuc->enabled)
559 wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
560}
561
562
563
564
565
566
567
568
569
570static int supported_cpu(void)
571{
572 struct percpu_struct *cpu;
573 unsigned long cputype;
574
575
576 cpu = (struct percpu_struct *)((char *)hwrpb + hwrpb->processor_offset);
577 cputype = cpu->type & 0xffffffff;
578
579 return (cputype >= EV67_CPU) && (cputype <= EV69_CPU);
580}
581
582
583
584static void hw_perf_event_destroy(struct perf_event *event)
585{
586
587 return;
588}
589
590
591
592static int __hw_perf_event_init(struct perf_event *event)
593{
594 struct perf_event_attr *attr = &event->attr;
595 struct hw_perf_event *hwc = &event->hw;
596 struct perf_event *evts[MAX_HWEVENTS];
597 unsigned long evtypes[MAX_HWEVENTS];
598 int idx_rubbish_bin[MAX_HWEVENTS];
599 int ev;
600 int n;
601
602
603
604
605 if (attr->type == PERF_TYPE_HARDWARE) {
606 if (attr->config >= alpha_pmu->max_events)
607 return -EINVAL;
608 ev = alpha_pmu->event_map[attr->config];
609 } else if (attr->type == PERF_TYPE_HW_CACHE) {
610 return -EOPNOTSUPP;
611 } else if (attr->type == PERF_TYPE_RAW) {
612 ev = attr->config & 0xff;
613 } else {
614 return -EOPNOTSUPP;
615 }
616
617 if (ev < 0) {
618 return ev;
619 }
620
621
622 if (attr->exclude_kernel || attr->exclude_user
623 || attr->exclude_hv || attr->exclude_idle) {
624 return -EPERM;
625 }
626
627
628
629
630
631
632
633
634
635
636 hwc->event_base = ev;
637
638
639
640
641
642 n = 0;
643 if (event->group_leader != event) {
644 n = collect_events(event->group_leader,
645 alpha_pmu->num_pmcs - 1,
646 evts, evtypes, idx_rubbish_bin);
647 if (n < 0)
648 return -EINVAL;
649 }
650 evtypes[n] = hwc->event_base;
651 evts[n] = event;
652
653 if (alpha_check_constraints(evts, evtypes, n + 1))
654 return -EINVAL;
655
656
657 hwc->config_base = 0;
658 hwc->idx = PMC_NO_INDEX;
659
660 event->destroy = hw_perf_event_destroy;
661
662
663
664
665
666
667
668
669
670
671
672 if (!hwc->sample_period) {
673 hwc->sample_period = alpha_pmu->pmc_max_period[0];
674 hwc->last_period = hwc->sample_period;
675 local64_set(&hwc->period_left, hwc->sample_period);
676 }
677
678 return 0;
679}
680
681
682
683
684static int alpha_pmu_event_init(struct perf_event *event)
685{
686 int err;
687
688
689 if (has_branch_stack(event))
690 return -EOPNOTSUPP;
691
692 switch (event->attr.type) {
693 case PERF_TYPE_RAW:
694 case PERF_TYPE_HARDWARE:
695 case PERF_TYPE_HW_CACHE:
696 break;
697
698 default:
699 return -ENOENT;
700 }
701
702 if (!alpha_pmu)
703 return -ENODEV;
704
705
706 err = __hw_perf_event_init(event);
707
708 return err;
709}
710
711
712
713
714static void alpha_pmu_enable(struct pmu *pmu)
715{
716 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
717
718 if (cpuc->enabled)
719 return;
720
721 cpuc->enabled = 1;
722 barrier();
723
724 if (cpuc->n_events > 0) {
725
726 maybe_change_configuration(cpuc);
727
728
729 wrperfmon(PERFMON_CMD_LOGGING_OPTIONS, EV67_PCTR_MODE_AGGREGATE);
730 wrperfmon(PERFMON_CMD_DESIRED_EVENTS, cpuc->config);
731 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
732 }
733}
734
735
736
737
738
739
740static void alpha_pmu_disable(struct pmu *pmu)
741{
742 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
743
744 if (!cpuc->enabled)
745 return;
746
747 cpuc->enabled = 0;
748 cpuc->n_added = 0;
749
750 wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
751}
752
753static struct pmu pmu = {
754 .pmu_enable = alpha_pmu_enable,
755 .pmu_disable = alpha_pmu_disable,
756 .event_init = alpha_pmu_event_init,
757 .add = alpha_pmu_add,
758 .del = alpha_pmu_del,
759 .start = alpha_pmu_start,
760 .stop = alpha_pmu_stop,
761 .read = alpha_pmu_read,
762};
763
764
765
766
767
768
769void perf_event_print_debug(void)
770{
771 unsigned long flags;
772 unsigned long pcr;
773 int pcr0, pcr1;
774 int cpu;
775
776 if (!supported_cpu())
777 return;
778
779 local_irq_save(flags);
780
781 cpu = smp_processor_id();
782
783 pcr = wrperfmon(PERFMON_CMD_READ, 0);
784 pcr0 = (pcr >> alpha_pmu->pmc_count_shift[0]) & alpha_pmu->pmc_count_mask[0];
785 pcr1 = (pcr >> alpha_pmu->pmc_count_shift[1]) & alpha_pmu->pmc_count_mask[1];
786
787 pr_info("CPU#%d: PCTR0[%06x] PCTR1[%06x]\n", cpu, pcr0, pcr1);
788
789 local_irq_restore(flags);
790}
791
792
793
794
795
796
797static void alpha_perf_event_irq_handler(unsigned long la_ptr,
798 struct pt_regs *regs)
799{
800 struct cpu_hw_events *cpuc;
801 struct perf_sample_data data;
802 struct perf_event *event;
803 struct hw_perf_event *hwc;
804 int idx, j;
805
806 __get_cpu_var(irq_pmi_count)++;
807 cpuc = &__get_cpu_var(cpu_hw_events);
808
809
810
811
812
813
814 wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
815
816
817 if (unlikely(la_ptr >= alpha_pmu->num_pmcs)) {
818
819 irq_err_count++;
820 pr_warning("PMI: silly index %ld\n", la_ptr);
821 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
822 return;
823 }
824
825 idx = la_ptr;
826
827 for (j = 0; j < cpuc->n_events; j++) {
828 if (cpuc->current_idx[j] == idx)
829 break;
830 }
831
832 if (unlikely(j == cpuc->n_events)) {
833
834 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
835 return;
836 }
837
838 event = cpuc->event[j];
839
840 if (unlikely(!event)) {
841
842 irq_err_count++;
843 pr_warning("PMI: No event at index %d!\n", idx);
844 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
845 return;
846 }
847
848 hwc = &event->hw;
849 alpha_perf_event_update(event, hwc, idx, alpha_pmu->pmc_max_period[idx]+1);
850 perf_sample_data_init(&data, 0, hwc->last_period);
851
852 if (alpha_perf_event_set_period(event, hwc, idx)) {
853 if (perf_event_overflow(event, &data, regs)) {
854
855
856
857 alpha_pmu_stop(event, 0);
858 }
859 }
860 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
861
862 return;
863}
864
865
866
867
868
869
870int __init init_hw_perf_events(void)
871{
872 pr_info("Performance events: ");
873
874 if (!supported_cpu()) {
875 pr_cont("No support for your CPU.\n");
876 return 0;
877 }
878
879 pr_cont("Supported CPU type!\n");
880
881
882
883 perf_irq = alpha_perf_event_irq_handler;
884
885
886 alpha_pmu = &ev67_pmu;
887
888 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
889
890 return 0;
891}
892early_initcall(init_hw_perf_events);
893