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