xref: /openbmc/linux/arch/alpha/kernel/perf_event.c (revision 33696fc0d141bbbcb12f75b69608ea83282e3117)
1979f8671SMichael Cree /*
2979f8671SMichael Cree  * Hardware performance events for the Alpha.
3979f8671SMichael Cree  *
4979f8671SMichael Cree  * We implement HW counts on the EV67 and subsequent CPUs only.
5979f8671SMichael Cree  *
6979f8671SMichael Cree  * (C) 2010 Michael J. Cree
7979f8671SMichael Cree  *
8979f8671SMichael Cree  * Somewhat based on the Sparc code, and to a lesser extent the PowerPC and
9979f8671SMichael Cree  * ARM code, which are copyright by their respective authors.
10979f8671SMichael Cree  */
11979f8671SMichael Cree 
12979f8671SMichael Cree #include <linux/perf_event.h>
13979f8671SMichael Cree #include <linux/kprobes.h>
14979f8671SMichael Cree #include <linux/kernel.h>
15979f8671SMichael Cree #include <linux/kdebug.h>
16979f8671SMichael Cree #include <linux/mutex.h>
17979f8671SMichael Cree 
18979f8671SMichael Cree #include <asm/hwrpb.h>
19979f8671SMichael Cree #include <asm/atomic.h>
20979f8671SMichael Cree #include <asm/irq.h>
21979f8671SMichael Cree #include <asm/irq_regs.h>
22979f8671SMichael Cree #include <asm/pal.h>
23979f8671SMichael Cree #include <asm/wrperfmon.h>
24979f8671SMichael Cree #include <asm/hw_irq.h>
25979f8671SMichael Cree 
26979f8671SMichael Cree 
27979f8671SMichael Cree /* The maximum number of PMCs on any Alpha CPU whatsoever. */
28979f8671SMichael Cree #define MAX_HWEVENTS 3
29979f8671SMichael Cree #define PMC_NO_INDEX -1
30979f8671SMichael Cree 
31979f8671SMichael Cree /* For tracking PMCs and the hw events they monitor on each CPU. */
32979f8671SMichael Cree struct cpu_hw_events {
33979f8671SMichael Cree 	int			enabled;
34979f8671SMichael Cree 	/* Number of events scheduled; also number entries valid in arrays below. */
35979f8671SMichael Cree 	int			n_events;
36979f8671SMichael Cree 	/* Number events added since last hw_perf_disable(). */
37979f8671SMichael Cree 	int			n_added;
38979f8671SMichael Cree 	/* Events currently scheduled. */
39979f8671SMichael Cree 	struct perf_event	*event[MAX_HWEVENTS];
40979f8671SMichael Cree 	/* Event type of each scheduled event. */
41979f8671SMichael Cree 	unsigned long		evtype[MAX_HWEVENTS];
42979f8671SMichael Cree 	/* Current index of each scheduled event; if not yet determined
43979f8671SMichael Cree 	 * contains PMC_NO_INDEX.
44979f8671SMichael Cree 	 */
45979f8671SMichael Cree 	int			current_idx[MAX_HWEVENTS];
46979f8671SMichael Cree 	/* The active PMCs' config for easy use with wrperfmon(). */
47979f8671SMichael Cree 	unsigned long		config;
48979f8671SMichael Cree 	/* The active counters' indices for easy use with wrperfmon(). */
49979f8671SMichael Cree 	unsigned long		idx_mask;
50979f8671SMichael Cree };
51979f8671SMichael Cree DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
52979f8671SMichael Cree 
53979f8671SMichael Cree 
54979f8671SMichael Cree 
55979f8671SMichael Cree /*
56979f8671SMichael Cree  * A structure to hold the description of the PMCs available on a particular
57979f8671SMichael Cree  * type of Alpha CPU.
58979f8671SMichael Cree  */
59979f8671SMichael Cree struct alpha_pmu_t {
60979f8671SMichael Cree 	/* Mapping of the perf system hw event types to indigenous event types */
61979f8671SMichael Cree 	const int *event_map;
62979f8671SMichael Cree 	/* The number of entries in the event_map */
63979f8671SMichael Cree 	int  max_events;
64979f8671SMichael Cree 	/* The number of PMCs on this Alpha */
65979f8671SMichael Cree 	int  num_pmcs;
66979f8671SMichael Cree 	/*
67979f8671SMichael Cree 	 * All PMC counters reside in the IBOX register PCTR.  This is the
68979f8671SMichael Cree 	 * LSB of the counter.
69979f8671SMichael Cree 	 */
70979f8671SMichael Cree 	int  pmc_count_shift[MAX_HWEVENTS];
71979f8671SMichael Cree 	/*
72979f8671SMichael Cree 	 * The mask that isolates the PMC bits when the LSB of the counter
73979f8671SMichael Cree 	 * is shifted to bit 0.
74979f8671SMichael Cree 	 */
75979f8671SMichael Cree 	unsigned long pmc_count_mask[MAX_HWEVENTS];
76979f8671SMichael Cree 	/* The maximum period the PMC can count. */
77979f8671SMichael Cree 	unsigned long pmc_max_period[MAX_HWEVENTS];
78979f8671SMichael Cree 	/*
79979f8671SMichael Cree 	 * The maximum value that may be written to the counter due to
80979f8671SMichael Cree 	 * hardware restrictions is pmc_max_period - pmc_left.
81979f8671SMichael Cree 	 */
82979f8671SMichael Cree 	long pmc_left[3];
83979f8671SMichael Cree 	 /* Subroutine for allocation of PMCs.  Enforces constraints. */
84979f8671SMichael Cree 	int (*check_constraints)(struct perf_event **, unsigned long *, int);
85979f8671SMichael Cree };
86979f8671SMichael Cree 
87979f8671SMichael Cree /*
88979f8671SMichael Cree  * The Alpha CPU PMU description currently in operation.  This is set during
89979f8671SMichael Cree  * the boot process to the specific CPU of the machine.
90979f8671SMichael Cree  */
91979f8671SMichael Cree static const struct alpha_pmu_t *alpha_pmu;
92979f8671SMichael Cree 
93979f8671SMichael Cree 
94979f8671SMichael Cree #define HW_OP_UNSUPPORTED -1
95979f8671SMichael Cree 
96979f8671SMichael Cree /*
97979f8671SMichael Cree  * The hardware description of the EV67, EV68, EV69, EV7 and EV79 PMUs
98979f8671SMichael Cree  * follow. Since they are identical we refer to them collectively as the
99979f8671SMichael Cree  * EV67 henceforth.
100979f8671SMichael Cree  */
101979f8671SMichael Cree 
102979f8671SMichael Cree /*
103979f8671SMichael Cree  * EV67 PMC event types
104979f8671SMichael Cree  *
105979f8671SMichael Cree  * There is no one-to-one mapping of the possible hw event types to the
106979f8671SMichael Cree  * actual codes that are used to program the PMCs hence we introduce our
107979f8671SMichael Cree  * own hw event type identifiers.
108979f8671SMichael Cree  */
109979f8671SMichael Cree enum ev67_pmc_event_type {
110979f8671SMichael Cree 	EV67_CYCLES = 1,
111979f8671SMichael Cree 	EV67_INSTRUCTIONS,
112979f8671SMichael Cree 	EV67_BCACHEMISS,
113979f8671SMichael Cree 	EV67_MBOXREPLAY,
114979f8671SMichael Cree 	EV67_LAST_ET
115979f8671SMichael Cree };
116979f8671SMichael Cree #define EV67_NUM_EVENT_TYPES (EV67_LAST_ET-EV67_CYCLES)
117979f8671SMichael Cree 
118979f8671SMichael Cree 
119979f8671SMichael Cree /* Mapping of the hw event types to the perf tool interface */
120979f8671SMichael Cree static const int ev67_perfmon_event_map[] = {
121979f8671SMichael Cree 	[PERF_COUNT_HW_CPU_CYCLES]	 = EV67_CYCLES,
122979f8671SMichael Cree 	[PERF_COUNT_HW_INSTRUCTIONS]	 = EV67_INSTRUCTIONS,
123979f8671SMichael Cree 	[PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
124979f8671SMichael Cree 	[PERF_COUNT_HW_CACHE_MISSES]	 = EV67_BCACHEMISS,
125979f8671SMichael Cree };
126979f8671SMichael Cree 
127979f8671SMichael Cree struct ev67_mapping_t {
128979f8671SMichael Cree 	int config;
129979f8671SMichael Cree 	int idx;
130979f8671SMichael Cree };
131979f8671SMichael Cree 
132979f8671SMichael Cree /*
133979f8671SMichael Cree  * The mapping used for one event only - these must be in same order as enum
134979f8671SMichael Cree  * ev67_pmc_event_type definition.
135979f8671SMichael Cree  */
136979f8671SMichael Cree static const struct ev67_mapping_t ev67_mapping[] = {
137979f8671SMichael Cree 	{EV67_PCTR_INSTR_CYCLES, 1},	 /* EV67_CYCLES, */
138979f8671SMichael Cree 	{EV67_PCTR_INSTR_CYCLES, 0},	 /* EV67_INSTRUCTIONS */
139979f8671SMichael Cree 	{EV67_PCTR_INSTR_BCACHEMISS, 1}, /* EV67_BCACHEMISS */
140979f8671SMichael Cree 	{EV67_PCTR_CYCLES_MBOX, 1}	 /* EV67_MBOXREPLAY */
141979f8671SMichael Cree };
142979f8671SMichael Cree 
143979f8671SMichael Cree 
144979f8671SMichael Cree /*
145979f8671SMichael Cree  * Check that a group of events can be simultaneously scheduled on to the
146979f8671SMichael Cree  * EV67 PMU.  Also allocate counter indices and config.
147979f8671SMichael Cree  */
148979f8671SMichael Cree static int ev67_check_constraints(struct perf_event **event,
149979f8671SMichael Cree 				unsigned long *evtype, int n_ev)
150979f8671SMichael Cree {
151979f8671SMichael Cree 	int idx0;
152979f8671SMichael Cree 	unsigned long config;
153979f8671SMichael Cree 
154979f8671SMichael Cree 	idx0 = ev67_mapping[evtype[0]-1].idx;
155979f8671SMichael Cree 	config = ev67_mapping[evtype[0]-1].config;
156979f8671SMichael Cree 	if (n_ev == 1)
157979f8671SMichael Cree 		goto success;
158979f8671SMichael Cree 
159979f8671SMichael Cree 	BUG_ON(n_ev != 2);
160979f8671SMichael Cree 
161979f8671SMichael Cree 	if (evtype[0] == EV67_MBOXREPLAY || evtype[1] == EV67_MBOXREPLAY) {
162979f8671SMichael Cree 		/* MBOX replay traps must be on PMC 1 */
163979f8671SMichael Cree 		idx0 = (evtype[0] == EV67_MBOXREPLAY) ? 1 : 0;
164979f8671SMichael Cree 		/* Only cycles can accompany MBOX replay traps */
165979f8671SMichael Cree 		if (evtype[idx0] == EV67_CYCLES) {
166979f8671SMichael Cree 			config = EV67_PCTR_CYCLES_MBOX;
167979f8671SMichael Cree 			goto success;
168979f8671SMichael Cree 		}
169979f8671SMichael Cree 	}
170979f8671SMichael Cree 
171979f8671SMichael Cree 	if (evtype[0] == EV67_BCACHEMISS || evtype[1] == EV67_BCACHEMISS) {
172979f8671SMichael Cree 		/* Bcache misses must be on PMC 1 */
173979f8671SMichael Cree 		idx0 = (evtype[0] == EV67_BCACHEMISS) ? 1 : 0;
174979f8671SMichael Cree 		/* Only instructions can accompany Bcache misses */
175979f8671SMichael Cree 		if (evtype[idx0] == EV67_INSTRUCTIONS) {
176979f8671SMichael Cree 			config = EV67_PCTR_INSTR_BCACHEMISS;
177979f8671SMichael Cree 			goto success;
178979f8671SMichael Cree 		}
179979f8671SMichael Cree 	}
180979f8671SMichael Cree 
181979f8671SMichael Cree 	if (evtype[0] == EV67_INSTRUCTIONS || evtype[1] == EV67_INSTRUCTIONS) {
182979f8671SMichael Cree 		/* Instructions must be on PMC 0 */
183979f8671SMichael Cree 		idx0 = (evtype[0] == EV67_INSTRUCTIONS) ? 0 : 1;
184979f8671SMichael Cree 		/* By this point only cycles can accompany instructions */
185979f8671SMichael Cree 		if (evtype[idx0^1] == EV67_CYCLES) {
186979f8671SMichael Cree 			config = EV67_PCTR_INSTR_CYCLES;
187979f8671SMichael Cree 			goto success;
188979f8671SMichael Cree 		}
189979f8671SMichael Cree 	}
190979f8671SMichael Cree 
191979f8671SMichael Cree 	/* Otherwise, darn it, there is a conflict.  */
192979f8671SMichael Cree 	return -1;
193979f8671SMichael Cree 
194979f8671SMichael Cree success:
195979f8671SMichael Cree 	event[0]->hw.idx = idx0;
196979f8671SMichael Cree 	event[0]->hw.config_base = config;
197979f8671SMichael Cree 	if (n_ev == 2) {
198979f8671SMichael Cree 		event[1]->hw.idx = idx0 ^ 1;
199979f8671SMichael Cree 		event[1]->hw.config_base = config;
200979f8671SMichael Cree 	}
201979f8671SMichael Cree 	return 0;
202979f8671SMichael Cree }
203979f8671SMichael Cree 
204979f8671SMichael Cree 
205979f8671SMichael Cree static const struct alpha_pmu_t ev67_pmu = {
206979f8671SMichael Cree 	.event_map = ev67_perfmon_event_map,
207979f8671SMichael Cree 	.max_events = ARRAY_SIZE(ev67_perfmon_event_map),
208979f8671SMichael Cree 	.num_pmcs = 2,
209979f8671SMichael Cree 	.pmc_count_shift = {EV67_PCTR_0_COUNT_SHIFT, EV67_PCTR_1_COUNT_SHIFT, 0},
210979f8671SMichael Cree 	.pmc_count_mask = {EV67_PCTR_0_COUNT_MASK,  EV67_PCTR_1_COUNT_MASK,  0},
211979f8671SMichael Cree 	.pmc_max_period = {(1UL<<20) - 1, (1UL<<20) - 1, 0},
212979f8671SMichael Cree 	.pmc_left = {16, 4, 0},
213979f8671SMichael Cree 	.check_constraints = ev67_check_constraints
214979f8671SMichael Cree };
215979f8671SMichael Cree 
216979f8671SMichael Cree 
217979f8671SMichael Cree 
218979f8671SMichael Cree /*
219979f8671SMichael Cree  * Helper routines to ensure that we read/write only the correct PMC bits
220979f8671SMichael Cree  * when calling the wrperfmon PALcall.
221979f8671SMichael Cree  */
222979f8671SMichael Cree static inline void alpha_write_pmc(int idx, unsigned long val)
223979f8671SMichael Cree {
224979f8671SMichael Cree 	val &= alpha_pmu->pmc_count_mask[idx];
225979f8671SMichael Cree 	val <<= alpha_pmu->pmc_count_shift[idx];
226979f8671SMichael Cree 	val |= (1<<idx);
227979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_WRITE, val);
228979f8671SMichael Cree }
229979f8671SMichael Cree 
230979f8671SMichael Cree static inline unsigned long alpha_read_pmc(int idx)
231979f8671SMichael Cree {
232979f8671SMichael Cree 	unsigned long val;
233979f8671SMichael Cree 
234979f8671SMichael Cree 	val = wrperfmon(PERFMON_CMD_READ, 0);
235979f8671SMichael Cree 	val >>= alpha_pmu->pmc_count_shift[idx];
236979f8671SMichael Cree 	val &= alpha_pmu->pmc_count_mask[idx];
237979f8671SMichael Cree 	return val;
238979f8671SMichael Cree }
239979f8671SMichael Cree 
240979f8671SMichael Cree /* Set a new period to sample over */
241979f8671SMichael Cree static int alpha_perf_event_set_period(struct perf_event *event,
242979f8671SMichael Cree 				struct hw_perf_event *hwc, int idx)
243979f8671SMichael Cree {
244979f8671SMichael Cree 	long left = atomic64_read(&hwc->period_left);
245979f8671SMichael Cree 	long period = hwc->sample_period;
246979f8671SMichael Cree 	int ret = 0;
247979f8671SMichael Cree 
248979f8671SMichael Cree 	if (unlikely(left <= -period)) {
249979f8671SMichael Cree 		left = period;
250979f8671SMichael Cree 		atomic64_set(&hwc->period_left, left);
251979f8671SMichael Cree 		hwc->last_period = period;
252979f8671SMichael Cree 		ret = 1;
253979f8671SMichael Cree 	}
254979f8671SMichael Cree 
255979f8671SMichael Cree 	if (unlikely(left <= 0)) {
256979f8671SMichael Cree 		left += period;
257979f8671SMichael Cree 		atomic64_set(&hwc->period_left, left);
258979f8671SMichael Cree 		hwc->last_period = period;
259979f8671SMichael Cree 		ret = 1;
260979f8671SMichael Cree 	}
261979f8671SMichael Cree 
262979f8671SMichael Cree 	/*
263979f8671SMichael Cree 	 * Hardware restrictions require that the counters must not be
264979f8671SMichael Cree 	 * written with values that are too close to the maximum period.
265979f8671SMichael Cree 	 */
266979f8671SMichael Cree 	if (unlikely(left < alpha_pmu->pmc_left[idx]))
267979f8671SMichael Cree 		left = alpha_pmu->pmc_left[idx];
268979f8671SMichael Cree 
269979f8671SMichael Cree 	if (left > (long)alpha_pmu->pmc_max_period[idx])
270979f8671SMichael Cree 		left = alpha_pmu->pmc_max_period[idx];
271979f8671SMichael Cree 
272979f8671SMichael Cree 	atomic64_set(&hwc->prev_count, (unsigned long)(-left));
273979f8671SMichael Cree 
274979f8671SMichael Cree 	alpha_write_pmc(idx, (unsigned long)(-left));
275979f8671SMichael Cree 
276979f8671SMichael Cree 	perf_event_update_userpage(event);
277979f8671SMichael Cree 
278979f8671SMichael Cree 	return ret;
279979f8671SMichael Cree }
280979f8671SMichael Cree 
281979f8671SMichael Cree 
282979f8671SMichael Cree /*
283979f8671SMichael Cree  * Calculates the count (the 'delta') since the last time the PMC was read.
284979f8671SMichael Cree  *
285979f8671SMichael Cree  * As the PMCs' full period can easily be exceeded within the perf system
286979f8671SMichael Cree  * sampling period we cannot use any high order bits as a guard bit in the
287979f8671SMichael Cree  * PMCs to detect overflow as is done by other architectures.  The code here
288979f8671SMichael Cree  * calculates the delta on the basis that there is no overflow when ovf is
289979f8671SMichael Cree  * zero.  The value passed via ovf by the interrupt handler corrects for
290979f8671SMichael Cree  * overflow.
291979f8671SMichael Cree  *
292979f8671SMichael Cree  * This can be racey on rare occasions -- a call to this routine can occur
293979f8671SMichael Cree  * with an overflowed counter just before the PMI service routine is called.
294979f8671SMichael Cree  * The check for delta negative hopefully always rectifies this situation.
295979f8671SMichael Cree  */
296979f8671SMichael Cree static unsigned long alpha_perf_event_update(struct perf_event *event,
297979f8671SMichael Cree 					struct hw_perf_event *hwc, int idx, long ovf)
298979f8671SMichael Cree {
299979f8671SMichael Cree 	long prev_raw_count, new_raw_count;
300979f8671SMichael Cree 	long delta;
301979f8671SMichael Cree 
302979f8671SMichael Cree again:
303979f8671SMichael Cree 	prev_raw_count = atomic64_read(&hwc->prev_count);
304979f8671SMichael Cree 	new_raw_count = alpha_read_pmc(idx);
305979f8671SMichael Cree 
306979f8671SMichael Cree 	if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
307979f8671SMichael Cree 			     new_raw_count) != prev_raw_count)
308979f8671SMichael Cree 		goto again;
309979f8671SMichael Cree 
310979f8671SMichael Cree 	delta = (new_raw_count  - (prev_raw_count & alpha_pmu->pmc_count_mask[idx])) + ovf;
311979f8671SMichael Cree 
312979f8671SMichael Cree 	/* It is possible on very rare occasions that the PMC has overflowed
313979f8671SMichael Cree 	 * but the interrupt is yet to come.  Detect and fix this situation.
314979f8671SMichael Cree 	 */
315979f8671SMichael Cree 	if (unlikely(delta < 0)) {
316979f8671SMichael Cree 		delta += alpha_pmu->pmc_max_period[idx] + 1;
317979f8671SMichael Cree 	}
318979f8671SMichael Cree 
319979f8671SMichael Cree 	atomic64_add(delta, &event->count);
320979f8671SMichael Cree 	atomic64_sub(delta, &hwc->period_left);
321979f8671SMichael Cree 
322979f8671SMichael Cree 	return new_raw_count;
323979f8671SMichael Cree }
324979f8671SMichael Cree 
325979f8671SMichael Cree 
326979f8671SMichael Cree /*
327979f8671SMichael Cree  * Collect all HW events into the array event[].
328979f8671SMichael Cree  */
329979f8671SMichael Cree static int collect_events(struct perf_event *group, int max_count,
330979f8671SMichael Cree 			  struct perf_event *event[], unsigned long *evtype,
331979f8671SMichael Cree 			  int *current_idx)
332979f8671SMichael Cree {
333979f8671SMichael Cree 	struct perf_event *pe;
334979f8671SMichael Cree 	int n = 0;
335979f8671SMichael Cree 
336979f8671SMichael Cree 	if (!is_software_event(group)) {
337979f8671SMichael Cree 		if (n >= max_count)
338979f8671SMichael Cree 			return -1;
339979f8671SMichael Cree 		event[n] = group;
340979f8671SMichael Cree 		evtype[n] = group->hw.event_base;
341979f8671SMichael Cree 		current_idx[n++] = PMC_NO_INDEX;
342979f8671SMichael Cree 	}
343979f8671SMichael Cree 	list_for_each_entry(pe, &group->sibling_list, group_entry) {
344979f8671SMichael Cree 		if (!is_software_event(pe) && pe->state != PERF_EVENT_STATE_OFF) {
345979f8671SMichael Cree 			if (n >= max_count)
346979f8671SMichael Cree 				return -1;
347979f8671SMichael Cree 			event[n] = pe;
348979f8671SMichael Cree 			evtype[n] = pe->hw.event_base;
349979f8671SMichael Cree 			current_idx[n++] = PMC_NO_INDEX;
350979f8671SMichael Cree 		}
351979f8671SMichael Cree 	}
352979f8671SMichael Cree 	return n;
353979f8671SMichael Cree }
354979f8671SMichael Cree 
355979f8671SMichael Cree 
356979f8671SMichael Cree 
357979f8671SMichael Cree /*
358979f8671SMichael Cree  * Check that a group of events can be simultaneously scheduled on to the PMU.
359979f8671SMichael Cree  */
360979f8671SMichael Cree static int alpha_check_constraints(struct perf_event **events,
361979f8671SMichael Cree 				   unsigned long *evtypes, int n_ev)
362979f8671SMichael Cree {
363979f8671SMichael Cree 
364979f8671SMichael Cree 	/* No HW events is possible from hw_perf_group_sched_in(). */
365979f8671SMichael Cree 	if (n_ev == 0)
366979f8671SMichael Cree 		return 0;
367979f8671SMichael Cree 
368979f8671SMichael Cree 	if (n_ev > alpha_pmu->num_pmcs)
369979f8671SMichael Cree 		return -1;
370979f8671SMichael Cree 
371979f8671SMichael Cree 	return alpha_pmu->check_constraints(events, evtypes, n_ev);
372979f8671SMichael Cree }
373979f8671SMichael Cree 
374979f8671SMichael Cree 
375979f8671SMichael Cree /*
376979f8671SMichael Cree  * If new events have been scheduled then update cpuc with the new
377979f8671SMichael Cree  * configuration.  This may involve shifting cycle counts from one PMC to
378979f8671SMichael Cree  * another.
379979f8671SMichael Cree  */
380979f8671SMichael Cree static void maybe_change_configuration(struct cpu_hw_events *cpuc)
381979f8671SMichael Cree {
382979f8671SMichael Cree 	int j;
383979f8671SMichael Cree 
384979f8671SMichael Cree 	if (cpuc->n_added == 0)
385979f8671SMichael Cree 		return;
386979f8671SMichael Cree 
387979f8671SMichael Cree 	/* Find counters that are moving to another PMC and update */
388979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
389979f8671SMichael Cree 		struct perf_event *pe = cpuc->event[j];
390979f8671SMichael Cree 
391979f8671SMichael Cree 		if (cpuc->current_idx[j] != PMC_NO_INDEX &&
392979f8671SMichael Cree 			cpuc->current_idx[j] != pe->hw.idx) {
393979f8671SMichael Cree 			alpha_perf_event_update(pe, &pe->hw, cpuc->current_idx[j], 0);
394979f8671SMichael Cree 			cpuc->current_idx[j] = PMC_NO_INDEX;
395979f8671SMichael Cree 		}
396979f8671SMichael Cree 	}
397979f8671SMichael Cree 
398979f8671SMichael Cree 	/* Assign to counters all unassigned events. */
399979f8671SMichael Cree 	cpuc->idx_mask = 0;
400979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
401979f8671SMichael Cree 		struct perf_event *pe = cpuc->event[j];
402979f8671SMichael Cree 		struct hw_perf_event *hwc = &pe->hw;
403979f8671SMichael Cree 		int idx = hwc->idx;
404979f8671SMichael Cree 
405979f8671SMichael Cree 		if (cpuc->current_idx[j] != PMC_NO_INDEX) {
406979f8671SMichael Cree 			cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
407979f8671SMichael Cree 			continue;
408979f8671SMichael Cree 		}
409979f8671SMichael Cree 
410979f8671SMichael Cree 		alpha_perf_event_set_period(pe, hwc, idx);
411979f8671SMichael Cree 		cpuc->current_idx[j] = idx;
412979f8671SMichael Cree 		cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
413979f8671SMichael Cree 	}
414979f8671SMichael Cree 	cpuc->config = cpuc->event[0]->hw.config_base;
415979f8671SMichael Cree }
416979f8671SMichael Cree 
417979f8671SMichael Cree 
418979f8671SMichael Cree 
419979f8671SMichael Cree /* Schedule perf HW event on to PMU.
420979f8671SMichael Cree  *  - this function is called from outside this module via the pmu struct
421979f8671SMichael Cree  *    returned from perf event initialisation.
422979f8671SMichael Cree  */
423979f8671SMichael Cree static int alpha_pmu_enable(struct perf_event *event)
424979f8671SMichael Cree {
425979f8671SMichael Cree 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
426979f8671SMichael Cree 	int n0;
427979f8671SMichael Cree 	int ret;
428979f8671SMichael Cree 	unsigned long flags;
429979f8671SMichael Cree 
430979f8671SMichael Cree 	/*
431979f8671SMichael Cree 	 * The Sparc code has the IRQ disable first followed by the perf
432979f8671SMichael Cree 	 * disable, however this can lead to an overflowed counter with the
433979f8671SMichael Cree 	 * PMI disabled on rare occasions.  The alpha_perf_event_update()
434979f8671SMichael Cree 	 * routine should detect this situation by noting a negative delta,
435979f8671SMichael Cree 	 * nevertheless we disable the PMCs first to enable a potential
436979f8671SMichael Cree 	 * final PMI to occur before we disable interrupts.
437979f8671SMichael Cree 	 */
438*33696fc0SPeter Zijlstra 	perf_pmu_disable(event->pmu);
439979f8671SMichael Cree 	local_irq_save(flags);
440979f8671SMichael Cree 
441979f8671SMichael Cree 	/* Default to error to be returned */
442979f8671SMichael Cree 	ret = -EAGAIN;
443979f8671SMichael Cree 
444979f8671SMichael Cree 	/* Insert event on to PMU and if successful modify ret to valid return */
445979f8671SMichael Cree 	n0 = cpuc->n_events;
446979f8671SMichael Cree 	if (n0 < alpha_pmu->num_pmcs) {
447979f8671SMichael Cree 		cpuc->event[n0] = event;
448979f8671SMichael Cree 		cpuc->evtype[n0] = event->hw.event_base;
449979f8671SMichael Cree 		cpuc->current_idx[n0] = PMC_NO_INDEX;
450979f8671SMichael Cree 
451979f8671SMichael Cree 		if (!alpha_check_constraints(cpuc->event, cpuc->evtype, n0+1)) {
452979f8671SMichael Cree 			cpuc->n_events++;
453979f8671SMichael Cree 			cpuc->n_added++;
454979f8671SMichael Cree 			ret = 0;
455979f8671SMichael Cree 		}
456979f8671SMichael Cree 	}
457979f8671SMichael Cree 
458979f8671SMichael Cree 	local_irq_restore(flags);
459*33696fc0SPeter Zijlstra 	perf_pmu_enable(event->pmu);
460979f8671SMichael Cree 
461979f8671SMichael Cree 	return ret;
462979f8671SMichael Cree }
463979f8671SMichael Cree 
464979f8671SMichael Cree 
465979f8671SMichael Cree 
466979f8671SMichael Cree /* Disable performance monitoring unit
467979f8671SMichael Cree  *  - this function is called from outside this module via the pmu struct
468979f8671SMichael Cree  *    returned from perf event initialisation.
469979f8671SMichael Cree  */
470979f8671SMichael Cree static void alpha_pmu_disable(struct perf_event *event)
471979f8671SMichael Cree {
472979f8671SMichael Cree 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
473979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
474979f8671SMichael Cree 	unsigned long flags;
475979f8671SMichael Cree 	int j;
476979f8671SMichael Cree 
477*33696fc0SPeter Zijlstra 	perf_pmu_disable(event->pmu);
478979f8671SMichael Cree 	local_irq_save(flags);
479979f8671SMichael Cree 
480979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
481979f8671SMichael Cree 		if (event == cpuc->event[j]) {
482979f8671SMichael Cree 			int idx = cpuc->current_idx[j];
483979f8671SMichael Cree 
484979f8671SMichael Cree 			/* Shift remaining entries down into the existing
485979f8671SMichael Cree 			 * slot.
486979f8671SMichael Cree 			 */
487979f8671SMichael Cree 			while (++j < cpuc->n_events) {
488979f8671SMichael Cree 				cpuc->event[j - 1] = cpuc->event[j];
489979f8671SMichael Cree 				cpuc->evtype[j - 1] = cpuc->evtype[j];
490979f8671SMichael Cree 				cpuc->current_idx[j - 1] =
491979f8671SMichael Cree 					cpuc->current_idx[j];
492979f8671SMichael Cree 			}
493979f8671SMichael Cree 
494979f8671SMichael Cree 			/* Absorb the final count and turn off the event. */
495979f8671SMichael Cree 			alpha_perf_event_update(event, hwc, idx, 0);
496979f8671SMichael Cree 			perf_event_update_userpage(event);
497979f8671SMichael Cree 
498979f8671SMichael Cree 			cpuc->idx_mask &= ~(1UL<<idx);
499979f8671SMichael Cree 			cpuc->n_events--;
500979f8671SMichael Cree 			break;
501979f8671SMichael Cree 		}
502979f8671SMichael Cree 	}
503979f8671SMichael Cree 
504979f8671SMichael Cree 	local_irq_restore(flags);
505*33696fc0SPeter Zijlstra 	perf_pmu_enable(event->pmu);
506979f8671SMichael Cree }
507979f8671SMichael Cree 
508979f8671SMichael Cree 
509979f8671SMichael Cree static void alpha_pmu_read(struct perf_event *event)
510979f8671SMichael Cree {
511979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
512979f8671SMichael Cree 
513979f8671SMichael Cree 	alpha_perf_event_update(event, hwc, hwc->idx, 0);
514979f8671SMichael Cree }
515979f8671SMichael Cree 
516979f8671SMichael Cree 
517979f8671SMichael Cree static void alpha_pmu_unthrottle(struct perf_event *event)
518979f8671SMichael Cree {
519979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
520979f8671SMichael Cree 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
521979f8671SMichael Cree 
522979f8671SMichael Cree 	cpuc->idx_mask |= 1UL<<hwc->idx;
523979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
524979f8671SMichael Cree }
525979f8671SMichael Cree 
526979f8671SMichael Cree 
527979f8671SMichael Cree /*
528979f8671SMichael Cree  * Check that CPU performance counters are supported.
529979f8671SMichael Cree  * - currently support EV67 and later CPUs.
530979f8671SMichael Cree  * - actually some later revisions of the EV6 have the same PMC model as the
531979f8671SMichael Cree  *     EV67 but we don't do suffiently deep CPU detection to detect them.
532979f8671SMichael Cree  *     Bad luck to the very few people who might have one, I guess.
533979f8671SMichael Cree  */
534979f8671SMichael Cree static int supported_cpu(void)
535979f8671SMichael Cree {
536979f8671SMichael Cree 	struct percpu_struct *cpu;
537979f8671SMichael Cree 	unsigned long cputype;
538979f8671SMichael Cree 
539979f8671SMichael Cree 	/* Get cpu type from HW */
540979f8671SMichael Cree 	cpu = (struct percpu_struct *)((char *)hwrpb + hwrpb->processor_offset);
541979f8671SMichael Cree 	cputype = cpu->type & 0xffffffff;
542979f8671SMichael Cree 	/* Include all of EV67, EV68, EV7, EV79 and EV69 as supported. */
543979f8671SMichael Cree 	return (cputype >= EV67_CPU) && (cputype <= EV69_CPU);
544979f8671SMichael Cree }
545979f8671SMichael Cree 
546979f8671SMichael Cree 
547979f8671SMichael Cree 
548979f8671SMichael Cree static void hw_perf_event_destroy(struct perf_event *event)
549979f8671SMichael Cree {
550979f8671SMichael Cree 	/* Nothing to be done! */
551979f8671SMichael Cree 	return;
552979f8671SMichael Cree }
553979f8671SMichael Cree 
554979f8671SMichael Cree 
555979f8671SMichael Cree 
556979f8671SMichael Cree static int __hw_perf_event_init(struct perf_event *event)
557979f8671SMichael Cree {
558979f8671SMichael Cree 	struct perf_event_attr *attr = &event->attr;
559979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
560979f8671SMichael Cree 	struct perf_event *evts[MAX_HWEVENTS];
561979f8671SMichael Cree 	unsigned long evtypes[MAX_HWEVENTS];
562979f8671SMichael Cree 	int idx_rubbish_bin[MAX_HWEVENTS];
563979f8671SMichael Cree 	int ev;
564979f8671SMichael Cree 	int n;
565979f8671SMichael Cree 
566979f8671SMichael Cree 	/* We only support a limited range of HARDWARE event types with one
567979f8671SMichael Cree 	 * only programmable via a RAW event type.
568979f8671SMichael Cree 	 */
569979f8671SMichael Cree 	if (attr->type == PERF_TYPE_HARDWARE) {
570979f8671SMichael Cree 		if (attr->config >= alpha_pmu->max_events)
571979f8671SMichael Cree 			return -EINVAL;
572979f8671SMichael Cree 		ev = alpha_pmu->event_map[attr->config];
573979f8671SMichael Cree 	} else if (attr->type == PERF_TYPE_HW_CACHE) {
574979f8671SMichael Cree 		return -EOPNOTSUPP;
575979f8671SMichael Cree 	} else if (attr->type == PERF_TYPE_RAW) {
576979f8671SMichael Cree 		ev = attr->config & 0xff;
577979f8671SMichael Cree 	} else {
578979f8671SMichael Cree 		return -EOPNOTSUPP;
579979f8671SMichael Cree 	}
580979f8671SMichael Cree 
581979f8671SMichael Cree 	if (ev < 0) {
582979f8671SMichael Cree 		return ev;
583979f8671SMichael Cree 	}
584979f8671SMichael Cree 
585979f8671SMichael Cree 	/* The EV67 does not support mode exclusion */
586979f8671SMichael Cree 	if (attr->exclude_kernel || attr->exclude_user
587979f8671SMichael Cree 			|| attr->exclude_hv || attr->exclude_idle) {
588979f8671SMichael Cree 		return -EPERM;
589979f8671SMichael Cree 	}
590979f8671SMichael Cree 
591979f8671SMichael Cree 	/*
592979f8671SMichael Cree 	 * We place the event type in event_base here and leave calculation
593979f8671SMichael Cree 	 * of the codes to programme the PMU for alpha_pmu_enable() because
594979f8671SMichael Cree 	 * it is only then we will know what HW events are actually
595979f8671SMichael Cree 	 * scheduled on to the PMU.  At that point the code to programme the
596979f8671SMichael Cree 	 * PMU is put into config_base and the PMC to use is placed into
597979f8671SMichael Cree 	 * idx.  We initialise idx (below) to PMC_NO_INDEX to indicate that
598979f8671SMichael Cree 	 * it is yet to be determined.
599979f8671SMichael Cree 	 */
600979f8671SMichael Cree 	hwc->event_base = ev;
601979f8671SMichael Cree 
602979f8671SMichael Cree 	/* Collect events in a group together suitable for calling
603979f8671SMichael Cree 	 * alpha_check_constraints() to verify that the group as a whole can
604979f8671SMichael Cree 	 * be scheduled on to the PMU.
605979f8671SMichael Cree 	 */
606979f8671SMichael Cree 	n = 0;
607979f8671SMichael Cree 	if (event->group_leader != event) {
608979f8671SMichael Cree 		n = collect_events(event->group_leader,
609979f8671SMichael Cree 				alpha_pmu->num_pmcs - 1,
610979f8671SMichael Cree 				evts, evtypes, idx_rubbish_bin);
611979f8671SMichael Cree 		if (n < 0)
612979f8671SMichael Cree 			return -EINVAL;
613979f8671SMichael Cree 	}
614979f8671SMichael Cree 	evtypes[n] = hwc->event_base;
615979f8671SMichael Cree 	evts[n] = event;
616979f8671SMichael Cree 
617979f8671SMichael Cree 	if (alpha_check_constraints(evts, evtypes, n + 1))
618979f8671SMichael Cree 		return -EINVAL;
619979f8671SMichael Cree 
620979f8671SMichael Cree 	/* Indicate that PMU config and idx are yet to be determined. */
621979f8671SMichael Cree 	hwc->config_base = 0;
622979f8671SMichael Cree 	hwc->idx = PMC_NO_INDEX;
623979f8671SMichael Cree 
624979f8671SMichael Cree 	event->destroy = hw_perf_event_destroy;
625979f8671SMichael Cree 
626979f8671SMichael Cree 	/*
627979f8671SMichael Cree 	 * Most architectures reserve the PMU for their use at this point.
628979f8671SMichael Cree 	 * As there is no existing mechanism to arbitrate usage and there
629979f8671SMichael Cree 	 * appears to be no other user of the Alpha PMU we just assume
630979f8671SMichael Cree 	 * that we can just use it, hence a NO-OP here.
631979f8671SMichael Cree 	 *
632979f8671SMichael Cree 	 * Maybe an alpha_reserve_pmu() routine should be implemented but is
633979f8671SMichael Cree 	 * anything else ever going to use it?
634979f8671SMichael Cree 	 */
635979f8671SMichael Cree 
636979f8671SMichael Cree 	if (!hwc->sample_period) {
637979f8671SMichael Cree 		hwc->sample_period = alpha_pmu->pmc_max_period[0];
638979f8671SMichael Cree 		hwc->last_period = hwc->sample_period;
639979f8671SMichael Cree 		atomic64_set(&hwc->period_left, hwc->sample_period);
640979f8671SMichael Cree 	}
641979f8671SMichael Cree 
642979f8671SMichael Cree 	return 0;
643979f8671SMichael Cree }
644979f8671SMichael Cree 
645b0a873ebSPeter Zijlstra /*
646b0a873ebSPeter Zijlstra  * Main entry point to initialise a HW performance event.
647b0a873ebSPeter Zijlstra  */
648b0a873ebSPeter Zijlstra static int alpha_pmu_event_init(struct perf_event *event)
649b0a873ebSPeter Zijlstra {
650b0a873ebSPeter Zijlstra 	int err;
651b0a873ebSPeter Zijlstra 
652b0a873ebSPeter Zijlstra 	switch (event->attr.type) {
653b0a873ebSPeter Zijlstra 	case PERF_TYPE_RAW:
654b0a873ebSPeter Zijlstra 	case PERF_TYPE_HARDWARE:
655b0a873ebSPeter Zijlstra 	case PERF_TYPE_HW_CACHE:
656b0a873ebSPeter Zijlstra 		break;
657b0a873ebSPeter Zijlstra 
658b0a873ebSPeter Zijlstra 	default:
659b0a873ebSPeter Zijlstra 		return -ENOENT;
660b0a873ebSPeter Zijlstra 	}
661b0a873ebSPeter Zijlstra 
662b0a873ebSPeter Zijlstra 	if (!alpha_pmu)
663b0a873ebSPeter Zijlstra 		return -ENODEV;
664b0a873ebSPeter Zijlstra 
665b0a873ebSPeter Zijlstra 	/* Do the real initialisation work. */
666b0a873ebSPeter Zijlstra 	err = __hw_perf_event_init(event);
667b0a873ebSPeter Zijlstra 
668b0a873ebSPeter Zijlstra 	return err;
669b0a873ebSPeter Zijlstra }
670b0a873ebSPeter Zijlstra 
671979f8671SMichael Cree /*
672979f8671SMichael Cree  * Main entry point - enable HW performance counters.
673979f8671SMichael Cree  */
674*33696fc0SPeter Zijlstra static void alpha_pmu_pmu_enable(struct pmu *pmu)
675979f8671SMichael Cree {
676979f8671SMichael Cree 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
677979f8671SMichael Cree 
678979f8671SMichael Cree 	if (cpuc->enabled)
679979f8671SMichael Cree 		return;
680979f8671SMichael Cree 
681979f8671SMichael Cree 	cpuc->enabled = 1;
682979f8671SMichael Cree 	barrier();
683979f8671SMichael Cree 
684979f8671SMichael Cree 	if (cpuc->n_events > 0) {
685979f8671SMichael Cree 		/* Update cpuc with information from any new scheduled events. */
686979f8671SMichael Cree 		maybe_change_configuration(cpuc);
687979f8671SMichael Cree 
688979f8671SMichael Cree 		/* Start counting the desired events. */
689979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_LOGGING_OPTIONS, EV67_PCTR_MODE_AGGREGATE);
690979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_DESIRED_EVENTS, cpuc->config);
691979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
692979f8671SMichael Cree 	}
693979f8671SMichael Cree }
694979f8671SMichael Cree 
695979f8671SMichael Cree 
696979f8671SMichael Cree /*
697979f8671SMichael Cree  * Main entry point - disable HW performance counters.
698979f8671SMichael Cree  */
699979f8671SMichael Cree 
700*33696fc0SPeter Zijlstra static void alpha_pmu_pmu_disable(struct pmu *pmu)
701979f8671SMichael Cree {
702979f8671SMichael Cree 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
703979f8671SMichael Cree 
704979f8671SMichael Cree 	if (!cpuc->enabled)
705979f8671SMichael Cree 		return;
706979f8671SMichael Cree 
707979f8671SMichael Cree 	cpuc->enabled = 0;
708979f8671SMichael Cree 	cpuc->n_added = 0;
709979f8671SMichael Cree 
710979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
711979f8671SMichael Cree }
712979f8671SMichael Cree 
713*33696fc0SPeter Zijlstra static struct pmu pmu = {
714*33696fc0SPeter Zijlstra 	.pmu_enable	= alpha_pmu_pmu_enable,
715*33696fc0SPeter Zijlstra 	.pmu_disable	= alpha_pmu_pmu_disable,
716*33696fc0SPeter Zijlstra 	.event_init	= alpha_pmu_event_init,
717*33696fc0SPeter Zijlstra 	.enable		= alpha_pmu_enable,
718*33696fc0SPeter Zijlstra 	.disable	= alpha_pmu_disable,
719*33696fc0SPeter Zijlstra 	.read		= alpha_pmu_read,
720*33696fc0SPeter Zijlstra 	.unthrottle	= alpha_pmu_unthrottle,
721*33696fc0SPeter Zijlstra };
722*33696fc0SPeter Zijlstra 
723979f8671SMichael Cree 
724979f8671SMichael Cree /*
725979f8671SMichael Cree  * Main entry point - don't know when this is called but it
726979f8671SMichael Cree  * obviously dumps debug info.
727979f8671SMichael Cree  */
728979f8671SMichael Cree void perf_event_print_debug(void)
729979f8671SMichael Cree {
730979f8671SMichael Cree 	unsigned long flags;
731979f8671SMichael Cree 	unsigned long pcr;
732979f8671SMichael Cree 	int pcr0, pcr1;
733979f8671SMichael Cree 	int cpu;
734979f8671SMichael Cree 
735979f8671SMichael Cree 	if (!supported_cpu())
736979f8671SMichael Cree 		return;
737979f8671SMichael Cree 
738979f8671SMichael Cree 	local_irq_save(flags);
739979f8671SMichael Cree 
740979f8671SMichael Cree 	cpu = smp_processor_id();
741979f8671SMichael Cree 
742979f8671SMichael Cree 	pcr = wrperfmon(PERFMON_CMD_READ, 0);
743979f8671SMichael Cree 	pcr0 = (pcr >> alpha_pmu->pmc_count_shift[0]) & alpha_pmu->pmc_count_mask[0];
744979f8671SMichael Cree 	pcr1 = (pcr >> alpha_pmu->pmc_count_shift[1]) & alpha_pmu->pmc_count_mask[1];
745979f8671SMichael Cree 
746979f8671SMichael Cree 	pr_info("CPU#%d: PCTR0[%06x] PCTR1[%06x]\n", cpu, pcr0, pcr1);
747979f8671SMichael Cree 
748979f8671SMichael Cree 	local_irq_restore(flags);
749979f8671SMichael Cree }
750979f8671SMichael Cree 
751979f8671SMichael Cree 
752979f8671SMichael Cree /*
753979f8671SMichael Cree  * Performance Monitoring Interrupt Service Routine called when a PMC
754979f8671SMichael Cree  * overflows.  The PMC that overflowed is passed in la_ptr.
755979f8671SMichael Cree  */
756979f8671SMichael Cree static void alpha_perf_event_irq_handler(unsigned long la_ptr,
757979f8671SMichael Cree 					struct pt_regs *regs)
758979f8671SMichael Cree {
759979f8671SMichael Cree 	struct cpu_hw_events *cpuc;
760979f8671SMichael Cree 	struct perf_sample_data data;
761979f8671SMichael Cree 	struct perf_event *event;
762979f8671SMichael Cree 	struct hw_perf_event *hwc;
763979f8671SMichael Cree 	int idx, j;
764979f8671SMichael Cree 
765979f8671SMichael Cree 	__get_cpu_var(irq_pmi_count)++;
766979f8671SMichael Cree 	cpuc = &__get_cpu_var(cpu_hw_events);
767979f8671SMichael Cree 
768979f8671SMichael Cree 	/* Completely counting through the PMC's period to trigger a new PMC
769979f8671SMichael Cree 	 * overflow interrupt while in this interrupt routine is utterly
770979f8671SMichael Cree 	 * disastrous!  The EV6 and EV67 counters are sufficiently large to
771979f8671SMichael Cree 	 * prevent this but to be really sure disable the PMCs.
772979f8671SMichael Cree 	 */
773979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
774979f8671SMichael Cree 
775979f8671SMichael Cree 	/* la_ptr is the counter that overflowed. */
776979f8671SMichael Cree 	if (unlikely(la_ptr >= perf_max_events)) {
777979f8671SMichael Cree 		/* This should never occur! */
778979f8671SMichael Cree 		irq_err_count++;
779979f8671SMichael Cree 		pr_warning("PMI: silly index %ld\n", la_ptr);
780979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
781979f8671SMichael Cree 		return;
782979f8671SMichael Cree 	}
783979f8671SMichael Cree 
784979f8671SMichael Cree 	idx = la_ptr;
785979f8671SMichael Cree 
786979f8671SMichael Cree 	perf_sample_data_init(&data, 0);
787979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
788979f8671SMichael Cree 		if (cpuc->current_idx[j] == idx)
789979f8671SMichael Cree 			break;
790979f8671SMichael Cree 	}
791979f8671SMichael Cree 
792979f8671SMichael Cree 	if (unlikely(j == cpuc->n_events)) {
793979f8671SMichael Cree 		/* This can occur if the event is disabled right on a PMC overflow. */
794979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
795979f8671SMichael Cree 		return;
796979f8671SMichael Cree 	}
797979f8671SMichael Cree 
798979f8671SMichael Cree 	event = cpuc->event[j];
799979f8671SMichael Cree 
800979f8671SMichael Cree 	if (unlikely(!event)) {
801979f8671SMichael Cree 		/* This should never occur! */
802979f8671SMichael Cree 		irq_err_count++;
803979f8671SMichael Cree 		pr_warning("PMI: No event at index %d!\n", idx);
804979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
805979f8671SMichael Cree 		return;
806979f8671SMichael Cree 	}
807979f8671SMichael Cree 
808979f8671SMichael Cree 	hwc = &event->hw;
809979f8671SMichael Cree 	alpha_perf_event_update(event, hwc, idx, alpha_pmu->pmc_max_period[idx]+1);
810979f8671SMichael Cree 	data.period = event->hw.last_period;
811979f8671SMichael Cree 
812979f8671SMichael Cree 	if (alpha_perf_event_set_period(event, hwc, idx)) {
813979f8671SMichael Cree 		if (perf_event_overflow(event, 1, &data, regs)) {
814979f8671SMichael Cree 			/* Interrupts coming too quickly; "throttle" the
815979f8671SMichael Cree 			 * counter, i.e., disable it for a little while.
816979f8671SMichael Cree 			 */
817979f8671SMichael Cree 			cpuc->idx_mask &= ~(1UL<<idx);
818979f8671SMichael Cree 		}
819979f8671SMichael Cree 	}
820979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
821979f8671SMichael Cree 
822979f8671SMichael Cree 	return;
823979f8671SMichael Cree }
824979f8671SMichael Cree 
825979f8671SMichael Cree 
826979f8671SMichael Cree 
827979f8671SMichael Cree /*
828979f8671SMichael Cree  * Init call to initialise performance events at kernel startup.
829979f8671SMichael Cree  */
830979f8671SMichael Cree void __init init_hw_perf_events(void)
831979f8671SMichael Cree {
832979f8671SMichael Cree 	pr_info("Performance events: ");
833979f8671SMichael Cree 
834979f8671SMichael Cree 	if (!supported_cpu()) {
835979f8671SMichael Cree 		pr_cont("No support for your CPU.\n");
836979f8671SMichael Cree 		return;
837979f8671SMichael Cree 	}
838979f8671SMichael Cree 
839979f8671SMichael Cree 	pr_cont("Supported CPU type!\n");
840979f8671SMichael Cree 
841979f8671SMichael Cree 	/* Override performance counter IRQ vector */
842979f8671SMichael Cree 
843979f8671SMichael Cree 	perf_irq = alpha_perf_event_irq_handler;
844979f8671SMichael Cree 
845979f8671SMichael Cree 	/* And set up PMU specification */
846979f8671SMichael Cree 	alpha_pmu = &ev67_pmu;
847979f8671SMichael Cree 	perf_max_events = alpha_pmu->num_pmcs;
848b0a873ebSPeter Zijlstra 
849b0a873ebSPeter Zijlstra 	perf_pmu_register(&pmu);
850979f8671SMichael Cree }
851979f8671SMichael Cree 
852