blob: 380ef02d557af1630f64dd6524884e5fd38a9c1f [file] [log] [blame]
Michael Cree979f8672010-08-09 17:20:08 -07001/*
2 * Hardware performance events for the Alpha.
3 *
4 * We implement HW counts on the EV67 and subsequent CPUs only.
5 *
6 * (C) 2010 Michael J. Cree
7 *
8 * Somewhat based on the Sparc code, and to a lesser extent the PowerPC and
9 * ARM code, which are copyright by their respective authors.
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
18#include <asm/hwrpb.h>
19#include <asm/atomic.h>
20#include <asm/irq.h>
21#include <asm/irq_regs.h>
22#include <asm/pal.h>
23#include <asm/wrperfmon.h>
24#include <asm/hw_irq.h>
25
26
27/* The maximum number of PMCs on any Alpha CPU whatsoever. */
28#define MAX_HWEVENTS 3
29#define PMC_NO_INDEX -1
30
31/* For tracking PMCs and the hw events they monitor on each CPU. */
32struct cpu_hw_events {
33 int enabled;
34 /* Number of events scheduled; also number entries valid in arrays below. */
35 int n_events;
36 /* Number events added since last hw_perf_disable(). */
37 int n_added;
38 /* Events currently scheduled. */
39 struct perf_event *event[MAX_HWEVENTS];
40 /* Event type of each scheduled event. */
41 unsigned long evtype[MAX_HWEVENTS];
42 /* Current index of each scheduled event; if not yet determined
43 * contains PMC_NO_INDEX.
44 */
45 int current_idx[MAX_HWEVENTS];
46 /* The active PMCs' config for easy use with wrperfmon(). */
47 unsigned long config;
48 /* The active counters' indices for easy use with wrperfmon(). */
49 unsigned long idx_mask;
50};
51DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
52
53
54
55/*
56 * A structure to hold the description of the PMCs available on a particular
57 * type of Alpha CPU.
58 */
59struct alpha_pmu_t {
60 /* Mapping of the perf system hw event types to indigenous event types */
61 const int *event_map;
62 /* The number of entries in the event_map */
63 int max_events;
64 /* The number of PMCs on this Alpha */
65 int num_pmcs;
66 /*
67 * All PMC counters reside in the IBOX register PCTR. This is the
68 * LSB of the counter.
69 */
70 int pmc_count_shift[MAX_HWEVENTS];
71 /*
72 * The mask that isolates the PMC bits when the LSB of the counter
73 * is shifted to bit 0.
74 */
75 unsigned long pmc_count_mask[MAX_HWEVENTS];
76 /* The maximum period the PMC can count. */
77 unsigned long pmc_max_period[MAX_HWEVENTS];
78 /*
79 * The maximum value that may be written to the counter due to
80 * hardware restrictions is pmc_max_period - pmc_left.
81 */
82 long pmc_left[3];
83 /* Subroutine for allocation of PMCs. Enforces constraints. */
84 int (*check_constraints)(struct perf_event **, unsigned long *, int);
85};
86
87/*
88 * The Alpha CPU PMU description currently in operation. This is set during
89 * the boot process to the specific CPU of the machine.
90 */
91static const struct alpha_pmu_t *alpha_pmu;
92
93
94#define HW_OP_UNSUPPORTED -1
95
96/*
97 * The hardware description of the EV67, EV68, EV69, EV7 and EV79 PMUs
98 * follow. Since they are identical we refer to them collectively as the
99 * EV67 henceforth.
100 */
101
102/*
103 * EV67 PMC event types
104 *
105 * There is no one-to-one mapping of the possible hw event types to the
106 * actual codes that are used to program the PMCs hence we introduce our
107 * own hw event type identifiers.
108 */
109enum ev67_pmc_event_type {
110 EV67_CYCLES = 1,
111 EV67_INSTRUCTIONS,
112 EV67_BCACHEMISS,
113 EV67_MBOXREPLAY,
114 EV67_LAST_ET
115};
116#define EV67_NUM_EVENT_TYPES (EV67_LAST_ET-EV67_CYCLES)
117
118
119/* Mapping of the hw event types to the perf tool interface */
120static const int ev67_perfmon_event_map[] = {
121 [PERF_COUNT_HW_CPU_CYCLES] = EV67_CYCLES,
122 [PERF_COUNT_HW_INSTRUCTIONS] = EV67_INSTRUCTIONS,
123 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
124 [PERF_COUNT_HW_CACHE_MISSES] = EV67_BCACHEMISS,
125};
126
127struct ev67_mapping_t {
128 int config;
129 int idx;
130};
131
132/*
133 * The mapping used for one event only - these must be in same order as enum
134 * ev67_pmc_event_type definition.
135 */
136static const struct ev67_mapping_t ev67_mapping[] = {
137 {EV67_PCTR_INSTR_CYCLES, 1}, /* EV67_CYCLES, */
138 {EV67_PCTR_INSTR_CYCLES, 0}, /* EV67_INSTRUCTIONS */
139 {EV67_PCTR_INSTR_BCACHEMISS, 1}, /* EV67_BCACHEMISS */
140 {EV67_PCTR_CYCLES_MBOX, 1} /* EV67_MBOXREPLAY */
141};
142
143
144/*
145 * Check that a group of events can be simultaneously scheduled on to the
146 * EV67 PMU. Also allocate counter indices and config.
147 */
148static int ev67_check_constraints(struct perf_event **event,
149 unsigned long *evtype, int n_ev)
150{
151 int idx0;
152 unsigned long config;
153
154 idx0 = ev67_mapping[evtype[0]-1].idx;
155 config = ev67_mapping[evtype[0]-1].config;
156 if (n_ev == 1)
157 goto success;
158
159 BUG_ON(n_ev != 2);
160
161 if (evtype[0] == EV67_MBOXREPLAY || evtype[1] == EV67_MBOXREPLAY) {
162 /* MBOX replay traps must be on PMC 1 */
163 idx0 = (evtype[0] == EV67_MBOXREPLAY) ? 1 : 0;
164 /* Only cycles can accompany MBOX replay traps */
165 if (evtype[idx0] == EV67_CYCLES) {
166 config = EV67_PCTR_CYCLES_MBOX;
167 goto success;
168 }
169 }
170
171 if (evtype[0] == EV67_BCACHEMISS || evtype[1] == EV67_BCACHEMISS) {
172 /* Bcache misses must be on PMC 1 */
173 idx0 = (evtype[0] == EV67_BCACHEMISS) ? 1 : 0;
174 /* Only instructions can accompany Bcache misses */
175 if (evtype[idx0] == EV67_INSTRUCTIONS) {
176 config = EV67_PCTR_INSTR_BCACHEMISS;
177 goto success;
178 }
179 }
180
181 if (evtype[0] == EV67_INSTRUCTIONS || evtype[1] == EV67_INSTRUCTIONS) {
182 /* Instructions must be on PMC 0 */
183 idx0 = (evtype[0] == EV67_INSTRUCTIONS) ? 0 : 1;
184 /* By this point only cycles can accompany instructions */
185 if (evtype[idx0^1] == EV67_CYCLES) {
186 config = EV67_PCTR_INSTR_CYCLES;
187 goto success;
188 }
189 }
190
191 /* Otherwise, darn it, there is a conflict. */
192 return -1;
193
194success:
195 event[0]->hw.idx = idx0;
196 event[0]->hw.config_base = config;
197 if (n_ev == 2) {
198 event[1]->hw.idx = idx0 ^ 1;
199 event[1]->hw.config_base = config;
200 }
201 return 0;
202}
203
204
205static const struct alpha_pmu_t ev67_pmu = {
206 .event_map = ev67_perfmon_event_map,
207 .max_events = ARRAY_SIZE(ev67_perfmon_event_map),
208 .num_pmcs = 2,
209 .pmc_count_shift = {EV67_PCTR_0_COUNT_SHIFT, EV67_PCTR_1_COUNT_SHIFT, 0},
210 .pmc_count_mask = {EV67_PCTR_0_COUNT_MASK, EV67_PCTR_1_COUNT_MASK, 0},
211 .pmc_max_period = {(1UL<<20) - 1, (1UL<<20) - 1, 0},
212 .pmc_left = {16, 4, 0},
213 .check_constraints = ev67_check_constraints
214};
215
216
217
218/*
219 * Helper routines to ensure that we read/write only the correct PMC bits
220 * when calling the wrperfmon PALcall.
221 */
222static inline void alpha_write_pmc(int idx, unsigned long val)
223{
224 val &= alpha_pmu->pmc_count_mask[idx];
225 val <<= alpha_pmu->pmc_count_shift[idx];
226 val |= (1<<idx);
227 wrperfmon(PERFMON_CMD_WRITE, val);
228}
229
230static inline unsigned long alpha_read_pmc(int idx)
231{
232 unsigned long val;
233
234 val = wrperfmon(PERFMON_CMD_READ, 0);
235 val >>= alpha_pmu->pmc_count_shift[idx];
236 val &= alpha_pmu->pmc_count_mask[idx];
237 return val;
238}
239
240/* Set a new period to sample over */
241static int alpha_perf_event_set_period(struct perf_event *event,
242 struct hw_perf_event *hwc, int idx)
243{
244 long left = atomic64_read(&hwc->period_left);
245 long period = hwc->sample_period;
246 int ret = 0;
247
248 if (unlikely(left <= -period)) {
249 left = period;
250 atomic64_set(&hwc->period_left, left);
251 hwc->last_period = period;
252 ret = 1;
253 }
254
255 if (unlikely(left <= 0)) {
256 left += period;
257 atomic64_set(&hwc->period_left, left);
258 hwc->last_period = period;
259 ret = 1;
260 }
261
262 /*
263 * Hardware restrictions require that the counters must not be
264 * written with values that are too close to the maximum period.
265 */
266 if (unlikely(left < alpha_pmu->pmc_left[idx]))
267 left = alpha_pmu->pmc_left[idx];
268
269 if (left > (long)alpha_pmu->pmc_max_period[idx])
270 left = alpha_pmu->pmc_max_period[idx];
271
272 atomic64_set(&hwc->prev_count, (unsigned long)(-left));
273
274 alpha_write_pmc(idx, (unsigned long)(-left));
275
276 perf_event_update_userpage(event);
277
278 return ret;
279}
280
281
282/*
283 * Calculates the count (the 'delta') since the last time the PMC was read.
284 *
285 * As the PMCs' full period can easily be exceeded within the perf system
286 * sampling period we cannot use any high order bits as a guard bit in the
287 * PMCs to detect overflow as is done by other architectures. The code here
288 * calculates the delta on the basis that there is no overflow when ovf is
289 * zero. The value passed via ovf by the interrupt handler corrects for
290 * overflow.
291 *
292 * This can be racey on rare occasions -- a call to this routine can occur
293 * with an overflowed counter just before the PMI service routine is called.
294 * The check for delta negative hopefully always rectifies this situation.
295 */
296static unsigned long alpha_perf_event_update(struct perf_event *event,
297 struct hw_perf_event *hwc, int idx, long ovf)
298{
299 long prev_raw_count, new_raw_count;
300 long delta;
301
302again:
303 prev_raw_count = atomic64_read(&hwc->prev_count);
304 new_raw_count = alpha_read_pmc(idx);
305
306 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
307 new_raw_count) != prev_raw_count)
308 goto again;
309
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200310 delta = (new_raw_count - (prev_raw_count & alpha_pmu->pmc_count_mask[idx])) + ovf;
Michael Cree979f8672010-08-09 17:20:08 -0700311
312 /* It is possible on very rare occasions that the PMC has overflowed
313 * but the interrupt is yet to come. Detect and fix this situation.
314 */
315 if (unlikely(delta < 0)) {
316 delta += alpha_pmu->pmc_max_period[idx] + 1;
317 }
318
319 atomic64_add(delta, &event->count);
320 atomic64_sub(delta, &hwc->period_left);
321
322 return new_raw_count;
323}
324
325
326/*
327 * Collect all HW events into the array event[].
328 */
329static int collect_events(struct perf_event *group, int max_count,
330 struct perf_event *event[], unsigned long *evtype,
331 int *current_idx)
332{
333 struct perf_event *pe;
334 int n = 0;
335
336 if (!is_software_event(group)) {
337 if (n >= max_count)
338 return -1;
339 event[n] = group;
340 evtype[n] = group->hw.event_base;
341 current_idx[n++] = PMC_NO_INDEX;
342 }
343 list_for_each_entry(pe, &group->sibling_list, group_entry) {
344 if (!is_software_event(pe) && pe->state != PERF_EVENT_STATE_OFF) {
345 if (n >= max_count)
346 return -1;
347 event[n] = pe;
348 evtype[n] = pe->hw.event_base;
349 current_idx[n++] = PMC_NO_INDEX;
350 }
351 }
352 return n;
353}
354
355
356
357/*
358 * Check that a group of events can be simultaneously scheduled on to the PMU.
359 */
360static int alpha_check_constraints(struct perf_event **events,
361 unsigned long *evtypes, int n_ev)
362{
363
364 /* No HW events is possible from hw_perf_group_sched_in(). */
365 if (n_ev == 0)
366 return 0;
367
368 if (n_ev > alpha_pmu->num_pmcs)
369 return -1;
370
371 return alpha_pmu->check_constraints(events, evtypes, n_ev);
372}
373
374
375/*
376 * If new events have been scheduled then update cpuc with the new
377 * configuration. This may involve shifting cycle counts from one PMC to
378 * another.
379 */
380static void maybe_change_configuration(struct cpu_hw_events *cpuc)
381{
382 int j;
383
384 if (cpuc->n_added == 0)
385 return;
386
387 /* Find counters that are moving to another PMC and update */
388 for (j = 0; j < cpuc->n_events; j++) {
389 struct perf_event *pe = cpuc->event[j];
390
391 if (cpuc->current_idx[j] != PMC_NO_INDEX &&
392 cpuc->current_idx[j] != pe->hw.idx) {
393 alpha_perf_event_update(pe, &pe->hw, cpuc->current_idx[j], 0);
394 cpuc->current_idx[j] = PMC_NO_INDEX;
395 }
396 }
397
398 /* Assign to counters all unassigned events. */
399 cpuc->idx_mask = 0;
400 for (j = 0; j < cpuc->n_events; j++) {
401 struct perf_event *pe = cpuc->event[j];
402 struct hw_perf_event *hwc = &pe->hw;
403 int idx = hwc->idx;
404
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200405 if (cpuc->current_idx[j] == PMC_NO_INDEX) {
406 alpha_perf_event_set_period(pe, hwc, idx);
407 cpuc->current_idx[j] = idx;
Michael Cree979f8672010-08-09 17:20:08 -0700408 }
409
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200410 if (!(hwc->state & PERF_HES_STOPPED))
411 cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
Michael Cree979f8672010-08-09 17:20:08 -0700412 }
413 cpuc->config = cpuc->event[0]->hw.config_base;
414}
415
416
417
418/* Schedule perf HW event on to PMU.
419 * - this function is called from outside this module via the pmu struct
420 * returned from perf event initialisation.
421 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200422static int alpha_pmu_add(struct perf_event *event, int flags)
Michael Cree979f8672010-08-09 17:20:08 -0700423{
424 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
425 int n0;
426 int ret;
427 unsigned long flags;
428
429 /*
430 * The Sparc code has the IRQ disable first followed by the perf
431 * disable, however this can lead to an overflowed counter with the
432 * PMI disabled on rare occasions. The alpha_perf_event_update()
433 * routine should detect this situation by noting a negative delta,
434 * nevertheless we disable the PMCs first to enable a potential
435 * final PMI to occur before we disable interrupts.
436 */
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200437 perf_pmu_disable(event->pmu);
Michael Cree979f8672010-08-09 17:20:08 -0700438 local_irq_save(flags);
439
440 /* Default to error to be returned */
441 ret = -EAGAIN;
442
443 /* Insert event on to PMU and if successful modify ret to valid return */
444 n0 = cpuc->n_events;
445 if (n0 < alpha_pmu->num_pmcs) {
446 cpuc->event[n0] = event;
447 cpuc->evtype[n0] = event->hw.event_base;
448 cpuc->current_idx[n0] = PMC_NO_INDEX;
449
450 if (!alpha_check_constraints(cpuc->event, cpuc->evtype, n0+1)) {
451 cpuc->n_events++;
452 cpuc->n_added++;
453 ret = 0;
454 }
455 }
456
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200457 hwc->state = PERF_HES_UPTODATE;
458 if (!(flags & PERF_EF_START))
459 hwc->state |= PERF_HES_STOPPED;
460
Michael Cree979f8672010-08-09 17:20:08 -0700461 local_irq_restore(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200462 perf_pmu_enable(event->pmu);
Michael Cree979f8672010-08-09 17:20:08 -0700463
464 return ret;
465}
466
467
468
469/* Disable performance monitoring unit
470 * - this function is called from outside this module via the pmu struct
471 * returned from perf event initialisation.
472 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200473static void alpha_pmu_del(struct perf_event *event, int flags)
Michael Cree979f8672010-08-09 17:20:08 -0700474{
475 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
476 struct hw_perf_event *hwc = &event->hw;
477 unsigned long flags;
478 int j;
479
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200480 perf_pmu_disable(event->pmu);
Michael Cree979f8672010-08-09 17:20:08 -0700481 local_irq_save(flags);
482
483 for (j = 0; j < cpuc->n_events; j++) {
484 if (event == cpuc->event[j]) {
485 int idx = cpuc->current_idx[j];
486
487 /* Shift remaining entries down into the existing
488 * slot.
489 */
490 while (++j < cpuc->n_events) {
491 cpuc->event[j - 1] = cpuc->event[j];
492 cpuc->evtype[j - 1] = cpuc->evtype[j];
493 cpuc->current_idx[j - 1] =
494 cpuc->current_idx[j];
495 }
496
497 /* Absorb the final count and turn off the event. */
498 alpha_perf_event_update(event, hwc, idx, 0);
499 perf_event_update_userpage(event);
500
501 cpuc->idx_mask &= ~(1UL<<idx);
502 cpuc->n_events--;
503 break;
504 }
505 }
506
507 local_irq_restore(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200508 perf_pmu_enable(event->pmu);
Michael Cree979f8672010-08-09 17:20:08 -0700509}
510
511
512static void alpha_pmu_read(struct perf_event *event)
513{
514 struct hw_perf_event *hwc = &event->hw;
515
516 alpha_perf_event_update(event, hwc, hwc->idx, 0);
517}
518
519
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200520static void alpha_pmu_stop(struct perf_event *event, int flags)
Michael Cree979f8672010-08-09 17:20:08 -0700521{
522 struct hw_perf_event *hwc = &event->hw;
523 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
524
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200525 if (!(hwc->state & PERF_HES_STOPPED)) {
526 cpuc->idx_mask &= !(1UL<<hwc->idx);
527 hwc->state |= PERF_HES_STOPPED;
528 }
529
530 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
531 alpha_perf_event_update(event, hwc, hwc->idx, 0);
532 hwc->state |= PERF_HES_UPTODATE;
533 }
534
535 if (cpuc->enabled)
536 wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
537}
538
539
540static void alpha_pmu_start(struct perf_event *event, int flags)
541{
542 struct hw_perf_event *hwc = &event->hw;
543 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
544
545 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
546 return;
547
548 if (flags & PERF_EF_RELOAD) {
549 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
550 alpha_perf_event_set_period(event, hwc, hwc->idx);
551 }
552
553 hwc->state = 0;
554
Michael Cree979f8672010-08-09 17:20:08 -0700555 cpuc->idx_mask |= 1UL<<hwc->idx;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200556 if (cpuc->enabled)
557 wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
Michael Cree979f8672010-08-09 17:20:08 -0700558}
559
560
561/*
562 * Check that CPU performance counters are supported.
563 * - currently support EV67 and later CPUs.
564 * - actually some later revisions of the EV6 have the same PMC model as the
565 * EV67 but we don't do suffiently deep CPU detection to detect them.
566 * Bad luck to the very few people who might have one, I guess.
567 */
568static int supported_cpu(void)
569{
570 struct percpu_struct *cpu;
571 unsigned long cputype;
572
573 /* Get cpu type from HW */
574 cpu = (struct percpu_struct *)((char *)hwrpb + hwrpb->processor_offset);
575 cputype = cpu->type & 0xffffffff;
576 /* Include all of EV67, EV68, EV7, EV79 and EV69 as supported. */
577 return (cputype >= EV67_CPU) && (cputype <= EV69_CPU);
578}
579
580
581
582static void hw_perf_event_destroy(struct perf_event *event)
583{
584 /* Nothing to be done! */
585 return;
586}
587
588
589
590static int __hw_perf_event_init(struct perf_event *event)
591{
592 struct perf_event_attr *attr = &event->attr;
593 struct hw_perf_event *hwc = &event->hw;
594 struct perf_event *evts[MAX_HWEVENTS];
595 unsigned long evtypes[MAX_HWEVENTS];
596 int idx_rubbish_bin[MAX_HWEVENTS];
597 int ev;
598 int n;
599
600 /* We only support a limited range of HARDWARE event types with one
601 * only programmable via a RAW event type.
602 */
603 if (attr->type == PERF_TYPE_HARDWARE) {
604 if (attr->config >= alpha_pmu->max_events)
605 return -EINVAL;
606 ev = alpha_pmu->event_map[attr->config];
607 } else if (attr->type == PERF_TYPE_HW_CACHE) {
608 return -EOPNOTSUPP;
609 } else if (attr->type == PERF_TYPE_RAW) {
610 ev = attr->config & 0xff;
611 } else {
612 return -EOPNOTSUPP;
613 }
614
615 if (ev < 0) {
616 return ev;
617 }
618
619 /* The EV67 does not support mode exclusion */
620 if (attr->exclude_kernel || attr->exclude_user
621 || attr->exclude_hv || attr->exclude_idle) {
622 return -EPERM;
623 }
624
625 /*
626 * We place the event type in event_base here and leave calculation
627 * of the codes to programme the PMU for alpha_pmu_enable() because
628 * it is only then we will know what HW events are actually
629 * scheduled on to the PMU. At that point the code to programme the
630 * PMU is put into config_base and the PMC to use is placed into
631 * idx. We initialise idx (below) to PMC_NO_INDEX to indicate that
632 * it is yet to be determined.
633 */
634 hwc->event_base = ev;
635
636 /* Collect events in a group together suitable for calling
637 * alpha_check_constraints() to verify that the group as a whole can
638 * be scheduled on to the PMU.
639 */
640 n = 0;
641 if (event->group_leader != event) {
642 n = collect_events(event->group_leader,
643 alpha_pmu->num_pmcs - 1,
644 evts, evtypes, idx_rubbish_bin);
645 if (n < 0)
646 return -EINVAL;
647 }
648 evtypes[n] = hwc->event_base;
649 evts[n] = event;
650
651 if (alpha_check_constraints(evts, evtypes, n + 1))
652 return -EINVAL;
653
654 /* Indicate that PMU config and idx are yet to be determined. */
655 hwc->config_base = 0;
656 hwc->idx = PMC_NO_INDEX;
657
658 event->destroy = hw_perf_event_destroy;
659
660 /*
661 * Most architectures reserve the PMU for their use at this point.
662 * As there is no existing mechanism to arbitrate usage and there
663 * appears to be no other user of the Alpha PMU we just assume
664 * that we can just use it, hence a NO-OP here.
665 *
666 * Maybe an alpha_reserve_pmu() routine should be implemented but is
667 * anything else ever going to use it?
668 */
669
670 if (!hwc->sample_period) {
671 hwc->sample_period = alpha_pmu->pmc_max_period[0];
672 hwc->last_period = hwc->sample_period;
673 atomic64_set(&hwc->period_left, hwc->sample_period);
674 }
675
676 return 0;
677}
678
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200679/*
680 * Main entry point to initialise a HW performance event.
681 */
682static int alpha_pmu_event_init(struct perf_event *event)
683{
684 int err;
685
686 switch (event->attr.type) {
687 case PERF_TYPE_RAW:
688 case PERF_TYPE_HARDWARE:
689 case PERF_TYPE_HW_CACHE:
690 break;
691
692 default:
693 return -ENOENT;
694 }
695
696 if (!alpha_pmu)
697 return -ENODEV;
698
699 /* Do the real initialisation work. */
700 err = __hw_perf_event_init(event);
701
702 return err;
703}
704
Michael Cree979f8672010-08-09 17:20:08 -0700705/*
706 * Main entry point - enable HW performance counters.
707 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200708static void alpha_pmu_enable(struct pmu *pmu)
Michael Cree979f8672010-08-09 17:20:08 -0700709{
710 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
711
712 if (cpuc->enabled)
713 return;
714
715 cpuc->enabled = 1;
716 barrier();
717
718 if (cpuc->n_events > 0) {
719 /* Update cpuc with information from any new scheduled events. */
720 maybe_change_configuration(cpuc);
721
722 /* Start counting the desired events. */
723 wrperfmon(PERFMON_CMD_LOGGING_OPTIONS, EV67_PCTR_MODE_AGGREGATE);
724 wrperfmon(PERFMON_CMD_DESIRED_EVENTS, cpuc->config);
725 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
726 }
727}
728
729
730/*
731 * Main entry point - disable HW performance counters.
732 */
733
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200734static void alpha_pmu_disable(struct pmu *pmu)
Michael Cree979f8672010-08-09 17:20:08 -0700735{
736 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
737
738 if (!cpuc->enabled)
739 return;
740
741 cpuc->enabled = 0;
742 cpuc->n_added = 0;
743
744 wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
745}
746
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200747static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200748 .pmu_enable = alpha_pmu_enable,
749 .pmu_disable = alpha_pmu_disable,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200750 .event_init = alpha_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200751 .add = alpha_pmu_add,
752 .del = alpha_pmu_del,
753 .start = alpha_pmu_start,
754 .stop = alpha_pmu_stop,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200755 .read = alpha_pmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200756};
757
Michael Cree979f8672010-08-09 17:20:08 -0700758
759/*
760 * Main entry point - don't know when this is called but it
761 * obviously dumps debug info.
762 */
763void perf_event_print_debug(void)
764{
765 unsigned long flags;
766 unsigned long pcr;
767 int pcr0, pcr1;
768 int cpu;
769
770 if (!supported_cpu())
771 return;
772
773 local_irq_save(flags);
774
775 cpu = smp_processor_id();
776
777 pcr = wrperfmon(PERFMON_CMD_READ, 0);
778 pcr0 = (pcr >> alpha_pmu->pmc_count_shift[0]) & alpha_pmu->pmc_count_mask[0];
779 pcr1 = (pcr >> alpha_pmu->pmc_count_shift[1]) & alpha_pmu->pmc_count_mask[1];
780
781 pr_info("CPU#%d: PCTR0[%06x] PCTR1[%06x]\n", cpu, pcr0, pcr1);
782
783 local_irq_restore(flags);
784}
785
786
787/*
788 * Performance Monitoring Interrupt Service Routine called when a PMC
789 * overflows. The PMC that overflowed is passed in la_ptr.
790 */
791static void alpha_perf_event_irq_handler(unsigned long la_ptr,
792 struct pt_regs *regs)
793{
794 struct cpu_hw_events *cpuc;
795 struct perf_sample_data data;
796 struct perf_event *event;
797 struct hw_perf_event *hwc;
798 int idx, j;
799
800 __get_cpu_var(irq_pmi_count)++;
801 cpuc = &__get_cpu_var(cpu_hw_events);
802
803 /* Completely counting through the PMC's period to trigger a new PMC
804 * overflow interrupt while in this interrupt routine is utterly
805 * disastrous! The EV6 and EV67 counters are sufficiently large to
806 * prevent this but to be really sure disable the PMCs.
807 */
808 wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
809
810 /* la_ptr is the counter that overflowed. */
811 if (unlikely(la_ptr >= perf_max_events)) {
812 /* This should never occur! */
813 irq_err_count++;
814 pr_warning("PMI: silly index %ld\n", la_ptr);
815 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
816 return;
817 }
818
819 idx = la_ptr;
820
821 perf_sample_data_init(&data, 0);
822 for (j = 0; j < cpuc->n_events; j++) {
823 if (cpuc->current_idx[j] == idx)
824 break;
825 }
826
827 if (unlikely(j == cpuc->n_events)) {
828 /* This can occur if the event is disabled right on a PMC overflow. */
829 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
830 return;
831 }
832
833 event = cpuc->event[j];
834
835 if (unlikely(!event)) {
836 /* This should never occur! */
837 irq_err_count++;
838 pr_warning("PMI: No event at index %d!\n", idx);
839 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
840 return;
841 }
842
843 hwc = &event->hw;
844 alpha_perf_event_update(event, hwc, idx, alpha_pmu->pmc_max_period[idx]+1);
845 data.period = event->hw.last_period;
846
847 if (alpha_perf_event_set_period(event, hwc, idx)) {
848 if (perf_event_overflow(event, 1, &data, regs)) {
849 /* Interrupts coming too quickly; "throttle" the
850 * counter, i.e., disable it for a little while.
851 */
852 cpuc->idx_mask &= ~(1UL<<idx);
853 }
854 }
855 wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
856
857 return;
858}
859
860
861
862/*
863 * Init call to initialise performance events at kernel startup.
864 */
865void __init init_hw_perf_events(void)
866{
867 pr_info("Performance events: ");
868
869 if (!supported_cpu()) {
870 pr_cont("No support for your CPU.\n");
871 return;
872 }
873
874 pr_cont("Supported CPU type!\n");
875
876 /* Override performance counter IRQ vector */
877
878 perf_irq = alpha_perf_event_irq_handler;
879
880 /* And set up PMU specification */
881 alpha_pmu = &ev67_pmu;
882 perf_max_events = alpha_pmu->num_pmcs;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200883
884 perf_pmu_register(&pmu);
Michael Cree979f8672010-08-09 17:20:08 -0700885}
886