xref: /linux/arch/alpha/kernel/perf_event.c (revision 092e0e7e520a1fca03e13c9f2d157432a8657ff2)
1 /*
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. */
32 struct 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 };
51 DEFINE_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  */
59 struct 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  */
91 static 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  */
109 enum 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 */
120 static 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 
127 struct 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  */
136 static 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  */
148 static 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 
194 success:
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 
205 static 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  */
222 static 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 
230 static 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 */
241 static int alpha_perf_event_set_period(struct perf_event *event,
242 				struct hw_perf_event *hwc, int idx)
243 {
244 	long left = local64_read(&hwc->period_left);
245 	long period = hwc->sample_period;
246 	int ret = 0;
247 
248 	if (unlikely(left <= -period)) {
249 		left = period;
250 		local64_set(&hwc->period_left, left);
251 		hwc->last_period = period;
252 		ret = 1;
253 	}
254 
255 	if (unlikely(left <= 0)) {
256 		left += period;
257 		local64_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 	local64_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  */
296 static 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 
302 again:
303 	prev_raw_count = local64_read(&hwc->prev_count);
304 	new_raw_count = alpha_read_pmc(idx);
305 
306 	if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
307 			     new_raw_count) != prev_raw_count)
308 		goto again;
309 
310 	delta = (new_raw_count - (prev_raw_count & alpha_pmu->pmc_count_mask[idx])) + ovf;
311 
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 	local64_add(delta, &event->count);
320 	local64_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  */
329 static 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  */
360 static 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  */
380 static 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 
405 		if (cpuc->current_idx[j] == PMC_NO_INDEX) {
406 			alpha_perf_event_set_period(pe, hwc, idx);
407 			cpuc->current_idx[j] = idx;
408 		}
409 
410 		if (!(hwc->state & PERF_HES_STOPPED))
411 			cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
412 	}
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  */
422 static int alpha_pmu_add(struct perf_event *event, int flags)
423 {
424 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
425 	struct hw_perf_event *hwc = &event->hw;
426 	int n0;
427 	int ret;
428 	unsigned long irq_flags;
429 
430 	/*
431 	 * The Sparc code has the IRQ disable first followed by the perf
432 	 * disable, however this can lead to an overflowed counter with the
433 	 * PMI disabled on rare occasions.  The alpha_perf_event_update()
434 	 * routine should detect this situation by noting a negative delta,
435 	 * nevertheless we disable the PMCs first to enable a potential
436 	 * final PMI to occur before we disable interrupts.
437 	 */
438 	perf_pmu_disable(event->pmu);
439 	local_irq_save(irq_flags);
440 
441 	/* Default to error to be returned */
442 	ret = -EAGAIN;
443 
444 	/* Insert event on to PMU and if successful modify ret to valid return */
445 	n0 = cpuc->n_events;
446 	if (n0 < alpha_pmu->num_pmcs) {
447 		cpuc->event[n0] = event;
448 		cpuc->evtype[n0] = event->hw.event_base;
449 		cpuc->current_idx[n0] = PMC_NO_INDEX;
450 
451 		if (!alpha_check_constraints(cpuc->event, cpuc->evtype, n0+1)) {
452 			cpuc->n_events++;
453 			cpuc->n_added++;
454 			ret = 0;
455 		}
456 	}
457 
458 	hwc->state = PERF_HES_UPTODATE;
459 	if (!(flags & PERF_EF_START))
460 		hwc->state |= PERF_HES_STOPPED;
461 
462 	local_irq_restore(irq_flags);
463 	perf_pmu_enable(event->pmu);
464 
465 	return ret;
466 }
467 
468 
469 
470 /* Disable performance monitoring unit
471  *  - this function is called from outside this module via the pmu struct
472  *    returned from perf event initialisation.
473  */
474 static void alpha_pmu_del(struct perf_event *event, int flags)
475 {
476 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
477 	struct hw_perf_event *hwc = &event->hw;
478 	unsigned long irq_flags;
479 	int j;
480 
481 	perf_pmu_disable(event->pmu);
482 	local_irq_save(irq_flags);
483 
484 	for (j = 0; j < cpuc->n_events; j++) {
485 		if (event == cpuc->event[j]) {
486 			int idx = cpuc->current_idx[j];
487 
488 			/* Shift remaining entries down into the existing
489 			 * slot.
490 			 */
491 			while (++j < cpuc->n_events) {
492 				cpuc->event[j - 1] = cpuc->event[j];
493 				cpuc->evtype[j - 1] = cpuc->evtype[j];
494 				cpuc->current_idx[j - 1] =
495 					cpuc->current_idx[j];
496 			}
497 
498 			/* Absorb the final count and turn off the event. */
499 			alpha_perf_event_update(event, hwc, idx, 0);
500 			perf_event_update_userpage(event);
501 
502 			cpuc->idx_mask &= ~(1UL<<idx);
503 			cpuc->n_events--;
504 			break;
505 		}
506 	}
507 
508 	local_irq_restore(irq_flags);
509 	perf_pmu_enable(event->pmu);
510 }
511 
512 
513 static void alpha_pmu_read(struct perf_event *event)
514 {
515 	struct hw_perf_event *hwc = &event->hw;
516 
517 	alpha_perf_event_update(event, hwc, hwc->idx, 0);
518 }
519 
520 
521 static void alpha_pmu_stop(struct perf_event *event, int flags)
522 {
523 	struct hw_perf_event *hwc = &event->hw;
524 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
525 
526 	if (!(hwc->state & PERF_HES_STOPPED)) {
527 		cpuc->idx_mask &= ~(1UL<<hwc->idx);
528 		hwc->state |= PERF_HES_STOPPED;
529 	}
530 
531 	if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
532 		alpha_perf_event_update(event, hwc, hwc->idx, 0);
533 		hwc->state |= PERF_HES_UPTODATE;
534 	}
535 
536 	if (cpuc->enabled)
537 		wrperfmon(PERFMON_CMD_DISABLE, (1UL<<hwc->idx));
538 }
539 
540 
541 static void alpha_pmu_start(struct perf_event *event, int flags)
542 {
543 	struct hw_perf_event *hwc = &event->hw;
544 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
545 
546 	if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
547 		return;
548 
549 	if (flags & PERF_EF_RELOAD) {
550 		WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
551 		alpha_perf_event_set_period(event, hwc, hwc->idx);
552 	}
553 
554 	hwc->state = 0;
555 
556 	cpuc->idx_mask |= 1UL<<hwc->idx;
557 	if (cpuc->enabled)
558 		wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
559 }
560 
561 
562 /*
563  * Check that CPU performance counters are supported.
564  * - currently support EV67 and later CPUs.
565  * - actually some later revisions of the EV6 have the same PMC model as the
566  *     EV67 but we don't do suffiently deep CPU detection to detect them.
567  *     Bad luck to the very few people who might have one, I guess.
568  */
569 static int supported_cpu(void)
570 {
571 	struct percpu_struct *cpu;
572 	unsigned long cputype;
573 
574 	/* Get cpu type from HW */
575 	cpu = (struct percpu_struct *)((char *)hwrpb + hwrpb->processor_offset);
576 	cputype = cpu->type & 0xffffffff;
577 	/* Include all of EV67, EV68, EV7, EV79 and EV69 as supported. */
578 	return (cputype >= EV67_CPU) && (cputype <= EV69_CPU);
579 }
580 
581 
582 
583 static void hw_perf_event_destroy(struct perf_event *event)
584 {
585 	/* Nothing to be done! */
586 	return;
587 }
588 
589 
590 
591 static int __hw_perf_event_init(struct perf_event *event)
592 {
593 	struct perf_event_attr *attr = &event->attr;
594 	struct hw_perf_event *hwc = &event->hw;
595 	struct perf_event *evts[MAX_HWEVENTS];
596 	unsigned long evtypes[MAX_HWEVENTS];
597 	int idx_rubbish_bin[MAX_HWEVENTS];
598 	int ev;
599 	int n;
600 
601 	/* We only support a limited range of HARDWARE event types with one
602 	 * only programmable via a RAW event type.
603 	 */
604 	if (attr->type == PERF_TYPE_HARDWARE) {
605 		if (attr->config >= alpha_pmu->max_events)
606 			return -EINVAL;
607 		ev = alpha_pmu->event_map[attr->config];
608 	} else if (attr->type == PERF_TYPE_HW_CACHE) {
609 		return -EOPNOTSUPP;
610 	} else if (attr->type == PERF_TYPE_RAW) {
611 		ev = attr->config & 0xff;
612 	} else {
613 		return -EOPNOTSUPP;
614 	}
615 
616 	if (ev < 0) {
617 		return ev;
618 	}
619 
620 	/* The EV67 does not support mode exclusion */
621 	if (attr->exclude_kernel || attr->exclude_user
622 			|| attr->exclude_hv || attr->exclude_idle) {
623 		return -EPERM;
624 	}
625 
626 	/*
627 	 * We place the event type in event_base here and leave calculation
628 	 * of the codes to programme the PMU for alpha_pmu_enable() because
629 	 * it is only then we will know what HW events are actually
630 	 * scheduled on to the PMU.  At that point the code to programme the
631 	 * PMU is put into config_base and the PMC to use is placed into
632 	 * idx.  We initialise idx (below) to PMC_NO_INDEX to indicate that
633 	 * it is yet to be determined.
634 	 */
635 	hwc->event_base = ev;
636 
637 	/* Collect events in a group together suitable for calling
638 	 * alpha_check_constraints() to verify that the group as a whole can
639 	 * be scheduled on to the PMU.
640 	 */
641 	n = 0;
642 	if (event->group_leader != event) {
643 		n = collect_events(event->group_leader,
644 				alpha_pmu->num_pmcs - 1,
645 				evts, evtypes, idx_rubbish_bin);
646 		if (n < 0)
647 			return -EINVAL;
648 	}
649 	evtypes[n] = hwc->event_base;
650 	evts[n] = event;
651 
652 	if (alpha_check_constraints(evts, evtypes, n + 1))
653 		return -EINVAL;
654 
655 	/* Indicate that PMU config and idx are yet to be determined. */
656 	hwc->config_base = 0;
657 	hwc->idx = PMC_NO_INDEX;
658 
659 	event->destroy = hw_perf_event_destroy;
660 
661 	/*
662 	 * Most architectures reserve the PMU for their use at this point.
663 	 * As there is no existing mechanism to arbitrate usage and there
664 	 * appears to be no other user of the Alpha PMU we just assume
665 	 * that we can just use it, hence a NO-OP here.
666 	 *
667 	 * Maybe an alpha_reserve_pmu() routine should be implemented but is
668 	 * anything else ever going to use it?
669 	 */
670 
671 	if (!hwc->sample_period) {
672 		hwc->sample_period = alpha_pmu->pmc_max_period[0];
673 		hwc->last_period = hwc->sample_period;
674 		local64_set(&hwc->period_left, hwc->sample_period);
675 	}
676 
677 	return 0;
678 }
679 
680 /*
681  * Main entry point to initialise a HW performance event.
682  */
683 static int alpha_pmu_event_init(struct perf_event *event)
684 {
685 	int err;
686 
687 	switch (event->attr.type) {
688 	case PERF_TYPE_RAW:
689 	case PERF_TYPE_HARDWARE:
690 	case PERF_TYPE_HW_CACHE:
691 		break;
692 
693 	default:
694 		return -ENOENT;
695 	}
696 
697 	if (!alpha_pmu)
698 		return -ENODEV;
699 
700 	/* Do the real initialisation work. */
701 	err = __hw_perf_event_init(event);
702 
703 	return err;
704 }
705 
706 /*
707  * Main entry point - enable HW performance counters.
708  */
709 static void alpha_pmu_enable(struct pmu *pmu)
710 {
711 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
712 
713 	if (cpuc->enabled)
714 		return;
715 
716 	cpuc->enabled = 1;
717 	barrier();
718 
719 	if (cpuc->n_events > 0) {
720 		/* Update cpuc with information from any new scheduled events. */
721 		maybe_change_configuration(cpuc);
722 
723 		/* Start counting the desired events. */
724 		wrperfmon(PERFMON_CMD_LOGGING_OPTIONS, EV67_PCTR_MODE_AGGREGATE);
725 		wrperfmon(PERFMON_CMD_DESIRED_EVENTS, cpuc->config);
726 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
727 	}
728 }
729 
730 
731 /*
732  * Main entry point - disable HW performance counters.
733  */
734 
735 static void alpha_pmu_disable(struct pmu *pmu)
736 {
737 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
738 
739 	if (!cpuc->enabled)
740 		return;
741 
742 	cpuc->enabled = 0;
743 	cpuc->n_added = 0;
744 
745 	wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
746 }
747 
748 static struct pmu pmu = {
749 	.pmu_enable	= alpha_pmu_enable,
750 	.pmu_disable	= alpha_pmu_disable,
751 	.event_init	= alpha_pmu_event_init,
752 	.add		= alpha_pmu_add,
753 	.del		= alpha_pmu_del,
754 	.start		= alpha_pmu_start,
755 	.stop		= alpha_pmu_stop,
756 	.read		= alpha_pmu_read,
757 };
758 
759 
760 /*
761  * Main entry point - don't know when this is called but it
762  * obviously dumps debug info.
763  */
764 void perf_event_print_debug(void)
765 {
766 	unsigned long flags;
767 	unsigned long pcr;
768 	int pcr0, pcr1;
769 	int cpu;
770 
771 	if (!supported_cpu())
772 		return;
773 
774 	local_irq_save(flags);
775 
776 	cpu = smp_processor_id();
777 
778 	pcr = wrperfmon(PERFMON_CMD_READ, 0);
779 	pcr0 = (pcr >> alpha_pmu->pmc_count_shift[0]) & alpha_pmu->pmc_count_mask[0];
780 	pcr1 = (pcr >> alpha_pmu->pmc_count_shift[1]) & alpha_pmu->pmc_count_mask[1];
781 
782 	pr_info("CPU#%d: PCTR0[%06x] PCTR1[%06x]\n", cpu, pcr0, pcr1);
783 
784 	local_irq_restore(flags);
785 }
786 
787 
788 /*
789  * Performance Monitoring Interrupt Service Routine called when a PMC
790  * overflows.  The PMC that overflowed is passed in la_ptr.
791  */
792 static void alpha_perf_event_irq_handler(unsigned long la_ptr,
793 					struct pt_regs *regs)
794 {
795 	struct cpu_hw_events *cpuc;
796 	struct perf_sample_data data;
797 	struct perf_event *event;
798 	struct hw_perf_event *hwc;
799 	int idx, j;
800 
801 	__get_cpu_var(irq_pmi_count)++;
802 	cpuc = &__get_cpu_var(cpu_hw_events);
803 
804 	/* Completely counting through the PMC's period to trigger a new PMC
805 	 * overflow interrupt while in this interrupt routine is utterly
806 	 * disastrous!  The EV6 and EV67 counters are sufficiently large to
807 	 * prevent this but to be really sure disable the PMCs.
808 	 */
809 	wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
810 
811 	/* la_ptr is the counter that overflowed. */
812 	if (unlikely(la_ptr >= alpha_pmu->num_pmcs)) {
813 		/* This should never occur! */
814 		irq_err_count++;
815 		pr_warning("PMI: silly index %ld\n", la_ptr);
816 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
817 		return;
818 	}
819 
820 	idx = la_ptr;
821 
822 	perf_sample_data_init(&data, 0);
823 	for (j = 0; j < cpuc->n_events; j++) {
824 		if (cpuc->current_idx[j] == idx)
825 			break;
826 	}
827 
828 	if (unlikely(j == cpuc->n_events)) {
829 		/* This can occur if the event is disabled right on a PMC overflow. */
830 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
831 		return;
832 	}
833 
834 	event = cpuc->event[j];
835 
836 	if (unlikely(!event)) {
837 		/* This should never occur! */
838 		irq_err_count++;
839 		pr_warning("PMI: No event at index %d!\n", idx);
840 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
841 		return;
842 	}
843 
844 	hwc = &event->hw;
845 	alpha_perf_event_update(event, hwc, idx, alpha_pmu->pmc_max_period[idx]+1);
846 	data.period = event->hw.last_period;
847 
848 	if (alpha_perf_event_set_period(event, hwc, idx)) {
849 		if (perf_event_overflow(event, 1, &data, regs)) {
850 			/* Interrupts coming too quickly; "throttle" the
851 			 * counter, i.e., disable it for a little while.
852 			 */
853 			alpha_pmu_stop(event, 0);
854 		}
855 	}
856 	wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
857 
858 	return;
859 }
860 
861 
862 
863 /*
864  * Init call to initialise performance events at kernel startup.
865  */
866 void __init init_hw_perf_events(void)
867 {
868 	pr_info("Performance events: ");
869 
870 	if (!supported_cpu()) {
871 		pr_cont("No support for your CPU.\n");
872 		return;
873 	}
874 
875 	pr_cont("Supported CPU type!\n");
876 
877 	/* Override performance counter IRQ vector */
878 
879 	perf_irq = alpha_perf_event_irq_handler;
880 
881 	/* And set up PMU specification */
882 	alpha_pmu = &ev67_pmu;
883 
884 	perf_pmu_register(&pmu);
885 }
886 
887